2002-12-09 09:47:15 +01:00
|
|
|
=head1 NAME
|
|
|
|
|
2003-03-18 10:53:37 +01:00
|
|
|
spamassassin - SpamAssassin integration for qpsmtpd
|
2002-12-09 09:47:15 +01:00
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
Plugin that checks if the mail is spam by using the "spamd" daemon
|
2003-03-18 10:53:37 +01:00
|
|
|
from the SpamAssassin package. F<http://www.spamassassin.org>
|
2002-12-09 09:47:15 +01:00
|
|
|
|
|
|
|
SpamAssassin 2.40 or newer is required.
|
|
|
|
|
2003-03-18 10:53:37 +01:00
|
|
|
B<WARNING>: SpamAssassin 2.50 is incompatible with qpsmtpd.
|
|
|
|
See F<http://nntp.x.perl.org/group/perl.qpsmtpd/188>
|
|
|
|
F<http://bugzilla.spamassassin.org/show_bug.cgi?id=1640>
|
|
|
|
F<http://bugzilla.spamassassin.org/show_bug.cgi?id=1614>
|
|
|
|
|
2002-12-09 09:47:15 +01:00
|
|
|
=head1 CONFIG
|
|
|
|
|
|
|
|
Configured in the plugins file without any parameters, the
|
|
|
|
spamassassin plugin will add relevant headers from the spamd
|
|
|
|
(X-Spam-Status etc).
|
|
|
|
|
|
|
|
The format goes like
|
|
|
|
|
|
|
|
spamassassin option value [option value]
|
|
|
|
|
|
|
|
Options being those listed below and the values being parameters to
|
|
|
|
the options. Confused yet? :-)
|
|
|
|
|
|
|
|
=over 4
|
|
|
|
|
|
|
|
=item reject_threshold [threshold]
|
|
|
|
|
|
|
|
Set the threshold over which the plugin will reject the mail. Some
|
|
|
|
mail servers are so useless that they ignore 55x responses not coming
|
|
|
|
after RCPT TO, so they might just keep retrying and retrying and
|
|
|
|
retrying until the mail expires from their queue.
|
|
|
|
|
|
|
|
I like to configure this with 15 or 20 as the threshold.
|
|
|
|
|
|
|
|
The default is to never reject mail based on the SpamAssassin score.
|
|
|
|
|
|
|
|
=item munge_subject_threshold [threshold]
|
|
|
|
|
|
|
|
Set the threshold over which we will prefix the subject with
|
|
|
|
'***SPAM***'. A messed up subject is easier to filter on than the
|
|
|
|
other headers for many people with not so clever mail clients. You
|
|
|
|
might want to make another plugin that does this on a per user basis.
|
|
|
|
|
|
|
|
The default is to never munge the subject based on the SpamAssassin score.
|
|
|
|
|
|
|
|
=back
|
|
|
|
|
|
|
|
With both options the configuration line will look like the following
|
|
|
|
|
|
|
|
spamasssasin reject_threshold 18 munge_subject_threshold 8
|
|
|
|
|
|
|
|
=cut
|
2002-08-06 14:27:35 +02:00
|
|
|
|
|
|
|
|
2002-08-06 14:01:22 +02:00
|
|
|
use Socket qw(:DEFAULT :crlf);
|
|
|
|
use IO::Handle;
|
|
|
|
|
|
|
|
sub register {
|
2002-12-09 09:47:15 +01:00
|
|
|
my ($self, $qp, @args) = @_;
|
2002-08-06 14:01:22 +02:00
|
|
|
$self->register_hook("data_post", "check_spam");
|
|
|
|
|
2002-12-09 09:47:15 +01:00
|
|
|
$self->log(0, "Bad parameters for the spamassassin plugin")
|
|
|
|
if @_ % 2;
|
|
|
|
|
|
|
|
%{$self->{_args}} = @args;
|
|
|
|
|
|
|
|
$self->register_hook("data_post", "check_spam_reject")
|
|
|
|
if $self->{_args}->{reject_threshold};
|
|
|
|
|
|
|
|
$self->register_hook("data_post", "check_spam_munge_subject")
|
|
|
|
if $self->{_args}->{munge_subject_threshold};
|
|
|
|
|
|
|
|
}
|
2002-08-06 14:01:22 +02:00
|
|
|
|
|
|
|
sub check_spam {
|
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
|
2002-08-06 14:57:59 +02:00
|
|
|
return (DECLINED) if $transaction->body_size > 500_000;
|
|
|
|
|
2002-08-06 14:01:22 +02:00
|
|
|
my $remote = 'localhost';
|
|
|
|
my $port = 783;
|
|
|
|
if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
|
|
|
|
die "No port" unless $port;
|
2002-09-08 12:02:10 +02:00
|
|
|
my $iaddr = inet_aton($remote) or
|
|
|
|
$self->log(1, "Could not resolve host: $remote") and return (DECLINED);
|
2002-08-06 14:01:22 +02:00
|
|
|
my $paddr = sockaddr_in($port, $iaddr);
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2002-08-06 14:01:22 +02:00
|
|
|
my $proto = getprotobyname('tcp');
|
2002-09-08 12:02:10 +02:00
|
|
|
socket(SPAMD, PF_INET, SOCK_STREAM, $proto)
|
|
|
|
or $self->log(1, "Could not open socket: $!") and return (DECLINED);
|
|
|
|
|
|
|
|
connect(SPAMD, $paddr)
|
|
|
|
or $self->log(1, "Could not connect to spamassassin daemon: $!") and return DECLINED;
|
2002-08-06 14:01:22 +02:00
|
|
|
|
|
|
|
SPAMD->autoflush(1);
|
|
|
|
|
|
|
|
$transaction->body_resetpos;
|
|
|
|
|
2002-08-29 05:15:34 +02:00
|
|
|
print SPAMD "REPORT_IFSPAM SPAMC/1.0" . CRLF;
|
2002-08-06 14:01:22 +02:00
|
|
|
# or CHECK or REPORT or SYMBOLS
|
|
|
|
|
2002-12-09 09:47:15 +01:00
|
|
|
print SPAMD join CRLF, split /\n/, $transaction->header->as_string
|
|
|
|
or warn "Could not print to spamd: $!";
|
|
|
|
|
|
|
|
print SPAMD CRLF
|
|
|
|
or warn "Could not print to spamd: $!";
|
2002-08-06 15:39:44 +02:00
|
|
|
|
2002-08-06 14:01:22 +02:00
|
|
|
while (my $line = $transaction->body_getline) {
|
|
|
|
chomp $line;
|
2002-12-09 09:47:15 +01:00
|
|
|
print SPAMD $line, CRLF
|
|
|
|
or warn "Could not print to spamd: $!";
|
2002-08-06 14:01:22 +02:00
|
|
|
}
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2002-08-06 14:01:22 +02:00
|
|
|
print SPAMD CRLF;
|
|
|
|
shutdown(SPAMD, 1);
|
|
|
|
my $line0 = <SPAMD>; # get the first protocol lines out
|
|
|
|
if ($line0) {
|
|
|
|
$transaction->header->add("X-Spam-Check-By", $self->qp->config('me'));
|
|
|
|
}
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2002-08-06 14:01:22 +02:00
|
|
|
while (<SPAMD>) {
|
2002-12-09 09:47:15 +01:00
|
|
|
#warn "GOT FROM SPAMD1: $_";
|
2002-08-06 14:01:22 +02:00
|
|
|
next unless m/\S/;
|
|
|
|
s/\r?\n$/\n/;
|
|
|
|
my @h = split /: /, $_, 2;
|
|
|
|
|
|
|
|
$transaction->header->add(@h);
|
|
|
|
last if $h[0] eq "Spam" and $h[1] =~ m/^False/;
|
|
|
|
|
|
|
|
}
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2002-11-06 12:03:00 +01:00
|
|
|
return (DECLINED);
|
2002-08-06 14:01:22 +02:00
|
|
|
}
|
2002-12-09 09:47:15 +01:00
|
|
|
|
|
|
|
sub check_spam_reject {
|
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
|
|
|
|
my $score = $self->get_spam_score($transaction) or return DECLINED;
|
|
|
|
|
|
|
|
return (DENY, "spam score exceeded threshold")
|
|
|
|
if $score >= $self->{_args}->{reject_threshold};
|
|
|
|
|
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub check_spam_munge_subject {
|
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
my $score = $self->get_spam_score($transaction) or return DECLINED;
|
|
|
|
|
|
|
|
return DECLINED unless $score >= $self->{_args}->{munge_subject_threshold};
|
|
|
|
|
|
|
|
my $subject = $transaction->header->get('Subject') || '';
|
|
|
|
$transaction->header->replace('Subject', "***SPAM*** $subject");
|
|
|
|
|
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_spam_score {
|
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
my $status = $transaction->header->get('X-Spam-Status') or return;
|
|
|
|
my ($score) = ($status =~ m/hits=(\d+\.\d+)/)[0];
|
|
|
|
return $score;
|
|
|
|
}
|