2012-04-29 10:35:59 +02:00
|
|
|
#!perl -w
|
2008-03-18 16:18:19 +01:00
|
|
|
|
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
|
|
|
|
2004-02-22 03:17:29 +01:00
|
|
|
SpamAssassin 2.6 or newer is required.
|
2003-03-18 10:53:37 +01:00
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
Stores the results in a note named spamassassin (for other plugins). The note
|
|
|
|
is a hashref with whatever fields are defined in your spamassassin config.
|
|
|
|
These are the common ones: score,required,autolearn,tests,version
|
|
|
|
|
2002-12-09 09:47:15 +01:00
|
|
|
=head1 CONFIG
|
|
|
|
|
|
|
|
Configured in the plugins file without any parameters, the
|
2012-11-14 21:21:44 +01:00
|
|
|
spamassassin plugin will add relevant headers from spamd
|
2002-12-09 09:47:15 +01:00
|
|
|
(X-Spam-Status etc).
|
|
|
|
|
|
|
|
The format goes like
|
|
|
|
|
|
|
|
spamassassin option value [option value]
|
|
|
|
|
|
|
|
Options being those listed below and the values being parameters to
|
2009-11-05 06:44:43 +01:00
|
|
|
the options. Confused yet? :-) It looks like this in practice:
|
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
spamassassin reject 7 leave_old_headers keep
|
2002-12-09 09:47:15 +01:00
|
|
|
|
|
|
|
=over 4
|
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
=item reject [threshold]
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2008-07-31 22:32:32 +02:00
|
|
|
Set the threshold where the plugin will reject the mail. Some
|
2002-12-09 09:47:15 +01:00
|
|
|
mail servers are so useless that they ignore 55x responses not coming
|
|
|
|
after RCPT TO, so they might just keep retrying and retrying and
|
2012-05-06 08:01:00 +02:00
|
|
|
retrying until the mail expires from their queue.
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2008-07-31 22:32:32 +02:00
|
|
|
Depending on your spamassassin configuration a reasonable setting is
|
|
|
|
typically somewhere between 12 to 20.
|
2002-12-09 09:47:15 +01:00
|
|
|
|
|
|
|
The default is to never reject mail based on the SpamAssassin score.
|
|
|
|
|
|
|
|
=item munge_subject_threshold [threshold]
|
|
|
|
|
2009-11-05 06:43:38 +01:00
|
|
|
Set the threshold where the plugin will prefix the subject with the
|
|
|
|
value of C<subject_prefix>. A modified 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.
|
2002-12-09 09:47:15 +01:00
|
|
|
|
|
|
|
The default is to never munge the subject based on the SpamAssassin score.
|
|
|
|
|
2009-11-05 06:43:38 +01:00
|
|
|
=item subject_prefix [prefix]
|
|
|
|
|
|
|
|
What to prefix the subject with if the message is detected as spam
|
|
|
|
(i.e. if score is greater than C<munge_subject_threshold>. Defaults to
|
|
|
|
C<*** SPAM ***>
|
|
|
|
|
2006-07-09 11:34:51 +02:00
|
|
|
=item spamd_socket [/path/to/socket|spamd.host:port]
|
2004-03-05 10:12:20 +01:00
|
|
|
|
2006-07-09 11:34:51 +02:00
|
|
|
Beginning with Mail::SpamAssassin 2.60, it is possible to use Unix
|
2012-05-06 08:01:00 +02:00
|
|
|
domain sockets for spamd. This is faster and more secure than using a
|
2006-07-09 11:34:51 +02:00
|
|
|
TCP connection, but if you run spamd on a remote machine, you need to
|
|
|
|
use a TCP connection.
|
2004-03-05 10:12:20 +01:00
|
|
|
|
2012-11-14 21:21:44 +01:00
|
|
|
=item headers [none]
|
|
|
|
|
|
|
|
By default, spamassasin headers are added to messages. To suppress header
|
|
|
|
insertion, use 'headers none'.
|
|
|
|
|
2004-10-13 03:52:35 +02:00
|
|
|
=item leave_old_headers [drop|rename|keep]
|
|
|
|
|
|
|
|
Another mail server before might have checked this mail already and may have
|
|
|
|
added X-Spam-Status, X-Spam-Flag and X-Spam-Check-By lines. Normally you can
|
|
|
|
not trust such headers and should either rename them to X-Old-... (default,
|
|
|
|
parameter 'rename') or have them removed (parameter 'drop'). If you know
|
|
|
|
what you are doing, you can also leave them intact (parameter 'keep').
|
|
|
|
|
2007-11-17 09:22:00 +01:00
|
|
|
=item spamd_user [username]
|
|
|
|
|
|
|
|
The username to pass to spamd, if different from the user qpsmtpd runs as.
|
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
=item relayclient skip
|
|
|
|
|
|
|
|
What special treatment is offered to connection with relay permission? Relay
|
|
|
|
permissions are granted when the connecting IP is listed in the relayclients
|
|
|
|
file and/or when the user has authenticated. The only valid option at present
|
|
|
|
is 'skip', which skips SA scoring.
|
|
|
|
|
|
|
|
If SpamAssasin has certain network tests enabled, users may get elevated spam
|
|
|
|
scores because their dynamic IP space is properly listed on DUL blocking lists.
|
|
|
|
If the user is authenticated or coming from a trusted IP, odds are we don't
|
|
|
|
want to be reject their messages. Especially when running qpsmtpd on port 587.
|
|
|
|
|
2002-12-09 09:47:15 +01:00
|
|
|
=back
|
|
|
|
|
2004-03-05 10:12:20 +01:00
|
|
|
With both of the first options the configuration line will look like the following
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
spamasssasin reject 18 munge_subject_threshold 8
|
|
|
|
|
|
|
|
|
|
|
|
=head1 MULTIPLE RECIPIENT BEHAVIOR
|
|
|
|
|
|
|
|
This plugin supports per-user SpamAssassin preferences. When per-user SA prefs
|
|
|
|
are enabled (by setting spamd_user = vpopmail), the message recipient is used
|
|
|
|
as the spamd username. If SpamAssassin has per-user preferences enabled, it
|
|
|
|
will consult the users spam preferences when scoring the message.
|
|
|
|
|
|
|
|
When a message has multiple recipients, we do not change the spamd username.
|
|
|
|
The message is still scored by SA, but per-user preferences are not
|
|
|
|
consulted. To aid in debugging, messages with multiple recipents will
|
|
|
|
have an X-Spam-User header inserted. Admins and savvy users can look for
|
|
|
|
that header to confirm the reason their personal prefs were not consulted.
|
|
|
|
|
|
|
|
To get per-user SA prefs to work for messages with multiple recipients, the
|
|
|
|
LDA should be configured to check for the presence of the X-Spam-User header.
|
|
|
|
If the X-Spam-User header is present, the LDA should submit the message to
|
|
|
|
spamd for re-processing with the recipients address.
|
|
|
|
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
=head1 CHANGES
|
|
|
|
|
|
|
|
2012.04.02 - Matt Simerson
|
|
|
|
|
|
|
|
* refactored for ease of maintenance
|
|
|
|
* added support for per-user SpamAssassin preferences
|
|
|
|
* updated get_spam_results so that score=N.N works (as well as hits=N.N)
|
|
|
|
* rewrote the X-Spam-* header additions so that SA generated headers are
|
2013-03-23 06:24:02 +01:00
|
|
|
preserved. Admins can alter SA headers with add_header in their SA
|
2012-05-06 08:01:00 +02:00
|
|
|
config. Subverting their changes there is unexpected. Making them read
|
|
|
|
code to figure out why is an unnecessary hurdle.
|
|
|
|
* added assemble_message, so we can calc content size which spamd wants
|
|
|
|
|
2002-12-09 09:47:15 +01:00
|
|
|
=cut
|
2002-08-06 14:27:35 +02:00
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
2002-08-06 14:27:35 +02:00
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
use Qpsmtpd::Constants;
|
2006-01-25 03:59:31 +01:00
|
|
|
use Qpsmtpd::DSN;
|
2012-06-23 00:29:28 +02:00
|
|
|
|
2002-08-06 14:01:22 +02:00
|
|
|
use Socket qw(:DEFAULT :crlf);
|
|
|
|
use IO::Handle;
|
|
|
|
|
|
|
|
sub register {
|
2012-05-06 08:01:00 +02:00
|
|
|
my ($self, $qp, %args) = @_;
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->log(LOGERROR, "Bad parameters for the spamassassin plugin")
|
|
|
|
if @_ % 2;
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->{_args} = {%args};
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
# backwards compatibility with previous config syntax
|
2013-04-21 06:50:39 +02:00
|
|
|
if ( !defined $self->{_args}{reject}
|
|
|
|
&& defined $self->{_args}{reject_threshold})
|
|
|
|
{
|
2012-05-06 08:01:00 +02:00
|
|
|
$self->{_args}{reject} = $self->{_args}{reject_threshold};
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
|
|
|
if (!defined $self->{_args}{reject_type}) {
|
2012-06-23 00:29:28 +02:00
|
|
|
$self->{_args}{reject_type} = 'perm';
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2014-12-24 03:40:07 +01:00
|
|
|
$self->register_hook('data_post', 'data_post_handler');
|
2002-12-09 09:47:15 +01:00
|
|
|
}
|
2002-08-06 14:01:22 +02:00
|
|
|
|
2012-06-23 00:29:28 +02:00
|
|
|
sub data_post_handler {
|
2012-05-06 08:01:00 +02:00
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
|
2014-09-18 03:28:51 +02:00
|
|
|
return DECLINED if $self->is_immune();
|
2012-06-03 03:44:46 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($transaction->data_size > 500_000) {
|
|
|
|
$self->log(LOGINFO,
|
2013-04-24 22:25:31 +02:00
|
|
|
"skip, too large (" . $transaction->data_size . ")");
|
2014-09-18 03:28:51 +02:00
|
|
|
return DECLINED;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-06 08:01:00 +02:00
|
|
|
|
2014-09-18 03:28:51 +02:00
|
|
|
my $SPAMD = $self->connect_to_spamd() or return DECLINED;
|
2013-04-21 06:50:39 +02:00
|
|
|
my $username = $self->select_spamd_username($transaction);
|
2012-05-06 08:01:00 +02:00
|
|
|
my $message = $self->assemble_message($transaction);
|
|
|
|
my $length = length $message;
|
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->print_to_spamd($SPAMD, $message, $length, $username);
|
|
|
|
shutdown($SPAMD, 1); # close our side of the socket (tell spamd we're done)
|
2014-09-18 03:28:51 +02:00
|
|
|
my $headers = $self->parse_spamd_response($SPAMD) or return DECLINED;
|
2012-05-06 08:01:00 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->insert_spam_headers($transaction, $headers, $username);
|
|
|
|
$self->munge_subject($transaction);
|
|
|
|
return $self->reject($transaction);
|
|
|
|
}
|
2012-05-06 08:01:00 +02:00
|
|
|
|
|
|
|
sub select_spamd_username {
|
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
|
|
|
|
my $username = $self->{_args}{spamd_user} || getpwuid($>);
|
|
|
|
|
|
|
|
my $recipient_count = scalar $transaction->recipients;
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($recipient_count > 1) {
|
2012-05-06 08:01:00 +02:00
|
|
|
$self->log(LOGDEBUG, "Message has $recipient_count recipients");
|
|
|
|
return $username;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($username eq 'vpopmail') {
|
2012-05-06 08:01:00 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
# use the recipients email address as username. This enables per-user SA prefs
|
2012-05-06 08:01:00 +02:00
|
|
|
$username = ($transaction->recipients)[0]->address;
|
2004-02-22 03:17:29 +01:00
|
|
|
}
|
2012-05-06 08:01:00 +02:00
|
|
|
else {
|
|
|
|
$self->log(LOGDEBUG, "skipping per-user SA prefs");
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-06 08:01:00 +02:00
|
|
|
|
|
|
|
return $username;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-06 08:01:00 +02:00
|
|
|
|
|
|
|
sub parse_spamd_response {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $SPAMD) = @_;
|
2012-05-06 08:01:00 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
my $line0 = <$SPAMD>; # get the first protocol line
|
|
|
|
if ($line0 !~ /EX_OK/) {
|
|
|
|
$self->log(LOGERROR, "invalid response from spamd: $line0");
|
|
|
|
return;
|
|
|
|
}
|
2012-05-06 08:01:00 +02:00
|
|
|
|
|
|
|
my (%new_headers, $last_header);
|
|
|
|
while (<$SPAMD>) {
|
|
|
|
s/[\r\n]//g;
|
2013-04-21 06:50:39 +02:00
|
|
|
if (m/^(X-Spam-.*?): (.*)?/) {
|
2012-05-06 08:01:00 +02:00
|
|
|
$new_headers{$1} = $2 || '';
|
|
|
|
$last_header = $1;
|
|
|
|
next;
|
|
|
|
}
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($last_header && m/^(\s+.*)/) { # a folded line, append to last
|
2012-05-06 08:01:00 +02:00
|
|
|
$new_headers{$last_header} .= CRLF . "\t" . $1;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
$last_header = undef;
|
|
|
|
}
|
|
|
|
close $SPAMD;
|
|
|
|
$self->log(LOGDEBUG, "finished reading from spamd");
|
|
|
|
|
|
|
|
return scalar keys %new_headers ? \%new_headers : undef;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-06 08:01:00 +02:00
|
|
|
|
|
|
|
sub insert_spam_headers {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $transaction, $new_headers, $username) = @_;
|
2012-05-06 08:01:00 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($self->{_args}{headers} && $self->{_args}{headers} eq 'none') {
|
|
|
|
my $r = $self->parse_spam_header($new_headers->{'X-Spam-Status'});
|
2012-11-14 21:21:44 +01:00
|
|
|
$transaction->notes('spamassassin', $r);
|
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-11-14 21:21:44 +01:00
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
my $recipient_count = scalar $transaction->recipients;
|
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->_cleanup_spam_header($transaction, 'X-Spam-User'); # always clean up
|
|
|
|
if ($recipient_count > 1) { # add for multiple recipients
|
|
|
|
$transaction->header->add('X-Spam-User',
|
|
|
|
$username . ", $recipient_count recipients",
|
|
|
|
0);
|
|
|
|
}
|
2012-05-06 08:01:00 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
foreach my $name (keys %$new_headers) {
|
|
|
|
next
|
|
|
|
if $name eq 'X-Spam-Prev-Subject'; # might exist if SA rewrote subject
|
|
|
|
if ($name eq 'X-Spam-Report') {
|
|
|
|
next; # Mail::Header mangles this prefolded header
|
|
|
|
|
|
|
|
# $self->log(LOGDEBUG, $new_headers->{$name} );
|
|
|
|
}
|
|
|
|
if ($name eq 'X-Spam-Status') {
|
|
|
|
$self->parse_spam_header($new_headers->{$name});
|
|
|
|
}
|
|
|
|
$new_headers->{$name} =~ s/\015//; # hack for outlook
|
2012-05-06 08:01:00 +02:00
|
|
|
$self->_cleanup_spam_header($transaction, $name);
|
|
|
|
$transaction->header->add($name, $new_headers->{$name}, 0);
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-06 08:01:00 +02:00
|
|
|
}
|
2002-08-06 14:01:22 +02:00
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
sub assemble_message {
|
|
|
|
my ($self, $transaction) = @_;
|
2004-02-22 03:17:29 +01:00
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
$transaction->body_resetpos;
|
2004-10-13 03:52:35 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
my $message =
|
|
|
|
"X-Envelope-From: "
|
|
|
|
. $transaction->sender->format . "\n"
|
2014-06-03 09:24:29 +02:00
|
|
|
. $transaction->header->as_string;
|
2007-09-24 22:45:09 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
while (my $line = $transaction->body_getline) { $message .= $line; }
|
2007-09-24 22:45:09 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
$message = join(CRLF, split /\n/, $message);
|
2012-05-06 08:01:00 +02:00
|
|
|
return $message . CRLF;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
sub connect_to_spamd {
|
2013-04-21 06:50:39 +02:00
|
|
|
my $self = shift;
|
2012-05-06 08:01:00 +02:00
|
|
|
my $socket = $self->{_args}{spamd_socket};
|
|
|
|
my $SPAMD;
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($socket && $socket =~ /\//) { # file path
|
|
|
|
$SPAMD = $self->connect_to_spamd_socket($socket);
|
2012-05-06 08:01:00 +02:00
|
|
|
}
|
|
|
|
else {
|
2013-04-21 06:50:39 +02:00
|
|
|
$SPAMD = $self->connect_to_spamd_tcpip($socket);
|
|
|
|
}
|
2012-05-06 08:01:00 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
return if !$SPAMD;
|
2012-05-06 08:01:00 +02:00
|
|
|
$SPAMD->autoflush(1);
|
|
|
|
return $SPAMD;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-06 08:01:00 +02:00
|
|
|
|
|
|
|
sub connect_to_spamd_socket {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $socket) = @_;
|
2012-05-06 08:01:00 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if (!$socket || $socket !~ /^([\w\/.-]+)$/) { # Unix Domain Socket
|
2012-05-06 08:01:00 +02:00
|
|
|
$self->log(LOGERROR, "not a valid path");
|
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-06 08:01:00 +02:00
|
|
|
|
2013-03-20 01:16:09 +01:00
|
|
|
# Sanitize for use with taint mode
|
|
|
|
$socket =~ /^([\w\/.-]+)$/;
|
|
|
|
$socket = $1;
|
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
socket(my $SPAMD, PF_UNIX, SOCK_STREAM, 0) or do {
|
|
|
|
$self->log(LOGERROR, "Could not open socket: $!");
|
|
|
|
return;
|
|
|
|
};
|
2013-04-21 06:50:39 +02:00
|
|
|
my $paddr = sockaddr_un($socket);
|
2012-05-06 08:01:00 +02:00
|
|
|
|
|
|
|
connect($SPAMD, $paddr) or do {
|
|
|
|
$self->log(LOGERROR, "Could not connect to spamd socket: $!");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
$self->log(LOGDEBUG, "connected to spamd");
|
|
|
|
return $SPAMD;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-06 08:01:00 +02:00
|
|
|
|
|
|
|
sub connect_to_spamd_tcpip {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $socket) = @_;
|
2012-05-06 08:01:00 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
my $remote = 'localhost';
|
|
|
|
my $port = 783;
|
2012-05-06 08:01:00 +02:00
|
|
|
|
|
|
|
if (defined $socket && $socket =~ /^([\w.-]+):(\d+)$/) {
|
2013-04-21 06:50:39 +02:00
|
|
|
$remote = $1;
|
|
|
|
$port = $2;
|
2012-05-06 08:01:00 +02:00
|
|
|
}
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
|
|
|
|
if (!$port) {
|
2012-05-06 08:01:00 +02:00
|
|
|
$self->log(LOGERROR, "No spamd port, check your spamd_socket config.");
|
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-06 08:01:00 +02:00
|
|
|
my $iaddr = inet_aton($remote) or do {
|
|
|
|
$self->log(LOGERROR, "Could not resolve host: $remote");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
my $paddr = sockaddr_in($port, $iaddr);
|
|
|
|
my $proto = getprotobyname('tcp');
|
|
|
|
|
|
|
|
socket(my $SPAMD, PF_INET, SOCK_STREAM, $proto) or do {
|
|
|
|
$self->log(LOGERROR, "Could not open socket: $!");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
connect($SPAMD, $paddr) or do {
|
|
|
|
$self->log(LOGERROR, "Could not connect to spamd: $!");
|
2013-04-21 06:50:39 +02:00
|
|
|
return;
|
2012-05-06 08:01:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
$self->log(LOGDEBUG, "connected to spamd");
|
|
|
|
return $SPAMD;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-06 08:01:00 +02:00
|
|
|
|
|
|
|
sub print_to_spamd {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $SPAMD, $message, $length, $username) = @_;
|
2012-05-06 08:01:00 +02:00
|
|
|
|
|
|
|
print $SPAMD "HEADERS SPAMC/1.4" . CRLF;
|
|
|
|
print $SPAMD "Content-length: $length" . CRLF;
|
|
|
|
print $SPAMD "User: $username" . CRLF;
|
|
|
|
print $SPAMD CRLF;
|
2013-04-21 06:50:39 +02:00
|
|
|
print $SPAMD $message
|
|
|
|
or $self->log(LOGWARN, "Could not print to spamd: $!");
|
2012-05-06 08:01:00 +02:00
|
|
|
|
|
|
|
$self->log(LOGDEBUG, "check_spam: finished sending to spamd");
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2012-06-23 00:29:28 +02:00
|
|
|
sub reject {
|
2012-05-06 08:01:00 +02:00
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
|
|
|
|
my $sa_results = $self->get_spam_results($transaction) or do {
|
2012-06-28 02:18:16 +02:00
|
|
|
$self->log(LOGNOTICE, "error, no results");
|
2012-05-06 08:01:00 +02:00
|
|
|
return DECLINED;
|
|
|
|
};
|
2013-04-21 06:50:39 +02:00
|
|
|
my $score = $sa_results->{score};
|
|
|
|
if (!defined $score) {
|
2012-06-28 02:18:16 +02:00
|
|
|
$self->log(LOGERROR, "error, error getting score");
|
2012-05-06 08:01:00 +02:00
|
|
|
return DECLINED;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-11 07:50:08 +02:00
|
|
|
|
|
|
|
my $ham_or_spam = $sa_results->{is_spam} eq 'Yes' ? 'Spam' : 'Ham';
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($ham_or_spam eq 'Spam') {
|
|
|
|
$self->adjust_karma(-1);
|
|
|
|
}
|
2012-06-23 00:29:28 +02:00
|
|
|
my $status = "$ham_or_spam, $score";
|
2013-04-21 06:50:39 +02:00
|
|
|
my $learn = '';
|
|
|
|
my $al = $sa_results->{autolearn}; # subject to local SA learn scores
|
|
|
|
if ($al) {
|
|
|
|
$self->adjust_karma(1) if $al eq 'ham';
|
|
|
|
$self->adjust_karma(-1) if $al eq 'spam';
|
|
|
|
$learn = "learn=" . $al;
|
|
|
|
}
|
2012-05-11 07:50:08 +02:00
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
my $reject = $self->{_args}{reject} or do {
|
2013-03-28 00:04:38 +01:00
|
|
|
$self->log(LOGERROR, "error, reject disabled ($status, $learn)");
|
2012-05-06 08:01:00 +02:00
|
|
|
return DECLINED;
|
|
|
|
};
|
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($score < $reject) {
|
|
|
|
if ($ham_or_spam eq 'Spam') {
|
2013-04-24 22:25:31 +02:00
|
|
|
$self->log(LOGINFO, "fail, tolerated, $status < $reject, $learn");
|
2012-06-23 00:29:28 +02:00
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->log(LOGINFO, "pass, $status < $reject, $learn");
|
|
|
|
return DECLINED;
|
|
|
|
}
|
|
|
|
}
|
2012-05-06 08:01:00 +02:00
|
|
|
|
2012-06-25 08:55:58 +02:00
|
|
|
$self->log(LOGINFO, "fail, $status, > $reject, $learn");
|
2014-09-18 03:28:51 +02:00
|
|
|
return $self->get_reject_type(), "spam score exceeded threshold";
|
2012-05-06 08:01:00 +02:00
|
|
|
}
|
|
|
|
|
2012-06-23 00:29:28 +02:00
|
|
|
sub munge_subject {
|
2012-05-06 08:01:00 +02:00
|
|
|
my ($self, $transaction) = @_;
|
|
|
|
|
2012-11-14 21:21:44 +01:00
|
|
|
return if ($self->{_args}{headers} && $self->{_args}{headers} eq 'none');
|
|
|
|
|
2012-06-23 00:29:28 +02:00
|
|
|
my $sa = $self->get_spam_results($transaction) or return;
|
2012-05-06 08:01:00 +02:00
|
|
|
my $qp_num = $self->{_args}{munge_subject_threshold};
|
2014-05-15 14:21:09 +02:00
|
|
|
my $required = $sa->{required};
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2014-05-15 14:21:09 +02:00
|
|
|
if (!$qp_num) {
|
2012-05-06 08:01:00 +02:00
|
|
|
$self->log(LOGDEBUG, "skipping munge, no user or qpsmtpd pref set");
|
2012-06-23 00:29:28 +02:00
|
|
|
return;
|
2012-05-06 08:01:00 +02:00
|
|
|
};
|
2014-09-09 23:24:33 +02:00
|
|
|
return if !defined $sa->{score};
|
|
|
|
return if $required && $sa->{score} <= $required;
|
|
|
|
return if $sa->{score} <= $qp_num;
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2014-09-09 23:24:33 +02:00
|
|
|
my $subject_prefix = $self->{_args}{subject_prefix} || $self->qp->config('subject_prefix') || '*** SPAM ***';
|
2013-04-21 06:50:39 +02:00
|
|
|
my $subject = $transaction->header->get('Subject') || '';
|
2012-05-06 08:01:00 +02:00
|
|
|
$transaction->header->replace('Subject', "$subject_prefix $subject");
|
2002-12-09 09:47:15 +01:00
|
|
|
}
|
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
sub get_spam_results {
|
|
|
|
my ($self, $transaction) = @_;
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if (defined $transaction->notes('spamassassin')) {
|
2012-05-06 08:01:00 +02:00
|
|
|
return $transaction->notes('spamassassin');
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
my $header = $transaction->header->get('X-Spam-Status') or return;
|
2013-04-21 06:50:39 +02:00
|
|
|
my $r = $self->parse_spam_header($header);
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
$self->log(LOGDEBUG, "$r->{is_spam}, $r->{score}");
|
|
|
|
$transaction->notes('spamassassin', $r);
|
2002-12-09 09:47:15 +01:00
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
return $r;
|
2002-12-09 09:47:15 +01:00
|
|
|
}
|
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
sub parse_spam_header {
|
|
|
|
my ($self, $string) = @_;
|
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
# the X-Spam-Score header contents vary based on the settings in
|
|
|
|
# the spamassassin *.cf files. Rather than parse via regexp, split
|
|
|
|
# on the consistent whitespace and = delimiters. More reliable and
|
|
|
|
# likely faster.
|
2012-05-06 08:01:00 +02:00
|
|
|
my @parts = split(/\s+/, $string);
|
|
|
|
my $is_spam = shift @parts;
|
|
|
|
chomp @parts;
|
2013-04-21 06:50:39 +02:00
|
|
|
chop $is_spam; # remove trailing ,
|
2012-05-06 08:01:00 +02:00
|
|
|
|
|
|
|
my %r;
|
2013-04-21 06:50:39 +02:00
|
|
|
foreach (@parts) {
|
|
|
|
my ($key, $val) = split(/=/, $_);
|
2012-05-06 08:01:00 +02:00
|
|
|
$r{$key} = $val;
|
|
|
|
}
|
|
|
|
$r{is_spam} = $is_spam;
|
|
|
|
|
2013-03-23 06:24:02 +01:00
|
|
|
# compatibility for SA versions < 3
|
2013-04-21 06:50:39 +02:00
|
|
|
if (defined $r{hits} && !defined $r{score}) {
|
2012-05-06 08:01:00 +02:00
|
|
|
$r{score} = delete $r{hits};
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-06 08:01:00 +02:00
|
|
|
return \%r;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2007-09-24 22:21:24 +02:00
|
|
|
|
|
|
|
sub _cleanup_spam_header {
|
2012-05-06 08:01:00 +02:00
|
|
|
my ($self, $transaction, $header_name) = @_;
|
2007-09-24 22:21:24 +02:00
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
my $action = 'rename';
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($self->{_args}->{leave_old_headers}) {
|
2012-05-06 08:01:00 +02:00
|
|
|
$action = lc($self->{_args}->{leave_old_headers});
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2007-09-24 22:21:24 +02:00
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
return unless $action eq 'drop' || $action eq 'rename';
|
2007-09-24 22:21:24 +02:00
|
|
|
|
2012-05-06 08:01:00 +02:00
|
|
|
my $old_header_name = $header_name;
|
2013-04-21 06:50:39 +02:00
|
|
|
$old_header_name =
|
|
|
|
($old_header_name =~ s/^X-//)
|
|
|
|
? "X-Old-$old_header_name"
|
|
|
|
: "Old-$old_header_name";
|
|
|
|
|
|
|
|
for my $header ($transaction->header->get($header_name)) {
|
|
|
|
$transaction->header->add($old_header_name, $header, 0)
|
|
|
|
if $action eq 'rename';
|
2012-05-06 08:01:00 +02:00
|
|
|
$transaction->header->delete($header_name);
|
|
|
|
}
|
2002-12-09 09:47:15 +01:00
|
|
|
}
|