| 1 |
#!perl -w |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
spamassassin - SpamAssassin integration for qpsmtpd |
| 6 |
|
| 7 |
=head1 DESCRIPTION |
| 8 |
|
| 9 |
Plugin that checks if the mail is spam by using the "spamd" daemon |
| 10 |
from the SpamAssassin package. F<http://www.spamassassin.org> |
| 11 |
|
| 12 |
SpamAssassin 2.6 or newer is required. |
| 13 |
|
| 14 |
Stores the results in a note named spamassassin (for other plugins). The note |
| 15 |
is a hashref with whatever fields are defined in your spamassassin config. |
| 16 |
These are the common ones: score,required,autolearn,tests,version |
| 17 |
|
| 18 |
=head1 CONFIG |
| 19 |
|
| 20 |
Configured in the plugins file without any parameters, the |
| 21 |
spamassassin plugin will add relevant headers from spamd |
| 22 |
(X-Spam-Status etc). |
| 23 |
|
| 24 |
The format goes like |
| 25 |
|
| 26 |
spamassassin option value [option value] |
| 27 |
|
| 28 |
Options being those listed below and the values being parameters to |
| 29 |
the options. Confused yet? :-) It looks like this in practice: |
| 30 |
|
| 31 |
spamassassin reject 7 leave_old_headers keep |
| 32 |
|
| 33 |
=over 4 |
| 34 |
|
| 35 |
=item reject [threshold] |
| 36 |
|
| 37 |
Set the threshold where the plugin will reject the mail. Some |
| 38 |
mail servers are so useless that they ignore 55x responses not coming |
| 39 |
after RCPT TO, so they might just keep retrying and retrying and |
| 40 |
retrying until the mail expires from their queue. |
| 41 |
|
| 42 |
Depending on your spamassassin configuration a reasonable setting is |
| 43 |
typically somewhere between 12 to 20. |
| 44 |
|
| 45 |
The default is to never reject mail based on the SpamAssassin score. |
| 46 |
|
| 47 |
=item munge_subject_threshold [threshold] |
| 48 |
|
| 49 |
Set the threshold where the plugin will prefix the subject with the |
| 50 |
value of C<subject_prefix>. A modified subject is easier to filter on |
| 51 |
than the other headers for many people with not so clever mail |
| 52 |
clients. You might want to make another plugin that does this on a |
| 53 |
per user basis. |
| 54 |
|
| 55 |
The default is to never munge the subject based on the SpamAssassin score. |
| 56 |
|
| 57 |
=item subject_prefix [prefix] |
| 58 |
|
| 59 |
What to prefix the subject with if the message is detected as spam |
| 60 |
(i.e. if score is greater than C<munge_subject_threshold>. Defaults to |
| 61 |
C<*** SPAM ***> |
| 62 |
|
| 63 |
=item spamd_socket [/path/to/socket|spamd.host:port] |
| 64 |
|
| 65 |
Beginning with Mail::SpamAssassin 2.60, it is possible to use Unix |
| 66 |
domain sockets for spamd. This is faster and more secure than using a |
| 67 |
TCP connection, but if you run spamd on a remote machine, you need to |
| 68 |
use a TCP connection. |
| 69 |
|
| 70 |
=item headers [none] |
| 71 |
|
| 72 |
By default, spamassasin headers are added to messages. To suppress header |
| 73 |
insertion, use 'headers none'. |
| 74 |
|
| 75 |
=item leave_old_headers [drop|rename|keep] |
| 76 |
|
| 77 |
Another mail server before might have checked this mail already and may have |
| 78 |
added X-Spam-Status, X-Spam-Flag and X-Spam-Check-By lines. Normally you can |
| 79 |
not trust such headers and should either rename them to X-Old-... (default, |
| 80 |
parameter 'rename') or have them removed (parameter 'drop'). If you know |
| 81 |
what you are doing, you can also leave them intact (parameter 'keep'). |
| 82 |
|
| 83 |
=item spamd_user [username] |
| 84 |
|
| 85 |
The username to pass to spamd, if different from the user qpsmtpd runs as. |
| 86 |
|
| 87 |
=item relayclient skip |
| 88 |
|
| 89 |
What special treatment is offered to connection with relay permission? Relay |
| 90 |
permissions are granted when the connecting IP is listed in the relayclients |
| 91 |
file and/or when the user has authenticated. The only valid option at present |
| 92 |
is 'skip', which skips SA scoring. |
| 93 |
|
| 94 |
If SpamAssasin has certain network tests enabled, users may get elevated spam |
| 95 |
scores because their dynamic IP space is properly listed on DUL blocking lists. |
| 96 |
If the user is authenticated or coming from a trusted IP, odds are we don't |
| 97 |
want to be reject their messages. Especially when running qpsmtpd on port 587. |
| 98 |
|
| 99 |
=back |
| 100 |
|
| 101 |
With both of the first options the configuration line will look like the following |
| 102 |
|
| 103 |
spamasssasin reject 18 munge_subject_threshold 8 |
| 104 |
|
| 105 |
|
| 106 |
=head1 MULTIPLE RECIPIENT BEHAVIOR |
| 107 |
|
| 108 |
This plugin supports per-user SpamAssassin preferences. When per-user SA prefs |
| 109 |
are enabled (by setting spamd_user = vpopmail), the message recipient is used |
| 110 |
as the spamd username. If SpamAssassin has per-user preferences enabled, it |
| 111 |
will consult the users spam preferences when scoring the message. |
| 112 |
|
| 113 |
When a message has multiple recipients, we do not change the spamd username. |
| 114 |
The message is still scored by SA, but per-user preferences are not |
| 115 |
consulted. To aid in debugging, messages with multiple recipents will |
| 116 |
have an X-Spam-User header inserted. Admins and savvy users can look for |
| 117 |
that header to confirm the reason their personal prefs were not consulted. |
| 118 |
|
| 119 |
To get per-user SA prefs to work for messages with multiple recipients, the |
| 120 |
LDA should be configured to check for the presence of the X-Spam-User header. |
| 121 |
If the X-Spam-User header is present, the LDA should submit the message to |
| 122 |
spamd for re-processing with the recipients address. |
| 123 |
|
| 124 |
|
| 125 |
=head1 TODO |
| 126 |
|
| 127 |
Make the "subject munge string" configurable |
| 128 |
|
| 129 |
=head1 CHANGES |
| 130 |
|
| 131 |
2012.04.02 - Matt Simerson |
| 132 |
|
| 133 |
* refactored for ease of maintenance |
| 134 |
* added support for per-user SpamAssassin preferences |
| 135 |
* updated get_spam_results so that score=N.N works (as well as hits=N.N) |
| 136 |
* rewrote the X-Spam-* header additions so that SA generated headers are |
| 137 |
preserved. Admins can alter SA headers with add_header in their SA |
| 138 |
config. Subverting their changes there is unexpected. Making them read |
| 139 |
code to figure out why is an unnecessary hurdle. |
| 140 |
* added assemble_message, so we can calc content size which spamd wants |
| 141 |
|
| 142 |
=cut |
| 143 |
|
| 144 |
use strict; |
| 145 |
use warnings; |
| 146 |
|
| 147 |
use Qpsmtpd::Constants; |
| 148 |
use Qpsmtpd::DSN; |
| 149 |
|
| 150 |
use Socket qw(:DEFAULT :crlf); |
| 151 |
use IO::Handle; |
| 152 |
|
| 153 |
sub register { |
| 154 |
my ($self, $qp, %args) = @_; |
| 155 |
|
| 156 |
$self->log(LOGERROR, "Bad parameters for the spamassassin plugin") |
| 157 |
if @_ % 2; |
| 158 |
|
| 159 |
$self->{_args} = {%args}; |
| 160 |
|
| 161 |
# backwards compatibility with previous config syntax |
| 162 |
if ( !defined $self->{_args}{reject} |
| 163 |
&& defined $self->{_args}{reject_threshold}) |
| 164 |
{ |
| 165 |
$self->{_args}{reject} = $self->{_args}{reject_threshold}; |
| 166 |
} |
| 167 |
if (!defined $self->{_args}{reject_type}) { |
| 168 |
$self->{_args}{reject_type} = 'perm'; |
| 169 |
} |
| 170 |
|
| 171 |
$self->register_hook('data_post', 'data_post_handler'); |
| 172 |
} |
| 173 |
|
| 174 |
sub data_post_handler { |
| 175 |
my ($self, $transaction) = @_; |
| 176 |
|
| 177 |
return (DECLINED) if $self->is_immune(); |
| 178 |
|
| 179 |
if ($transaction->data_size > 500_000) { |
| 180 |
$self->log(LOGINFO, |
| 181 |
"skip, too large (" . $transaction->data_size . ")"); |
| 182 |
return (DECLINED); |
| 183 |
} |
| 184 |
|
| 185 |
my $SPAMD = $self->connect_to_spamd() or return (DECLINED); |
| 186 |
my $username = $self->select_spamd_username($transaction); |
| 187 |
my $message = $self->assemble_message($transaction); |
| 188 |
my $length = length $message; |
| 189 |
|
| 190 |
$self->print_to_spamd($SPAMD, $message, $length, $username); |
| 191 |
shutdown($SPAMD, 1); # close our side of the socket (tell spamd we're done) |
| 192 |
my $headers = $self->parse_spamd_response($SPAMD) or return (DECLINED); |
| 193 |
|
| 194 |
$self->insert_spam_headers($transaction, $headers, $username); |
| 195 |
$self->munge_subject($transaction); |
| 196 |
return $self->reject($transaction); |
| 197 |
} |
| 198 |
|
| 199 |
sub select_spamd_username { |
| 200 |
my ($self, $transaction) = @_; |
| 201 |
|
| 202 |
my $username = $self->{_args}{spamd_user} || getpwuid($>); |
| 203 |
|
| 204 |
my $recipient_count = scalar $transaction->recipients; |
| 205 |
if ($recipient_count > 1) { |
| 206 |
$self->log(LOGDEBUG, "Message has $recipient_count recipients"); |
| 207 |
return $username; |
| 208 |
} |
| 209 |
|
| 210 |
if ($username eq 'vpopmail') { |
| 211 |
|
| 212 |
# use the recipients email address as username. This enables per-user SA prefs |
| 213 |
$username = ($transaction->recipients)[0]->address; |
| 214 |
} |
| 215 |
else { |
| 216 |
$self->log(LOGDEBUG, "skipping per-user SA prefs"); |
| 217 |
} |
| 218 |
|
| 219 |
return $username; |
| 220 |
} |
| 221 |
|
| 222 |
sub parse_spamd_response { |
| 223 |
my ($self, $SPAMD) = @_; |
| 224 |
|
| 225 |
my $line0 = <$SPAMD>; # get the first protocol line |
| 226 |
if ($line0 !~ /EX_OK/) { |
| 227 |
$self->log(LOGERROR, "invalid response from spamd: $line0"); |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
my (%new_headers, $last_header); |
| 232 |
while (<$SPAMD>) { |
| 233 |
s/[\r\n]//g; |
| 234 |
if (m/^(X-Spam-.*?): (.*)?/) { |
| 235 |
$new_headers{$1} = $2 || ''; |
| 236 |
$last_header = $1; |
| 237 |
next; |
| 238 |
} |
| 239 |
if ($last_header && m/^(\s+.*)/) { # a folded line, append to last |
| 240 |
$new_headers{$last_header} .= CRLF . "\t" . $1; |
| 241 |
next; |
| 242 |
} |
| 243 |
$last_header = undef; |
| 244 |
} |
| 245 |
close $SPAMD; |
| 246 |
$self->log(LOGDEBUG, "finished reading from spamd"); |
| 247 |
|
| 248 |
return scalar keys %new_headers ? \%new_headers : undef; |
| 249 |
} |
| 250 |
|
| 251 |
sub insert_spam_headers { |
| 252 |
my ($self, $transaction, $new_headers, $username) = @_; |
| 253 |
|
| 254 |
if ($self->{_args}{headers} && $self->{_args}{headers} eq 'none') { |
| 255 |
my $r = $self->parse_spam_header($new_headers->{'X-Spam-Status'}); |
| 256 |
$transaction->notes('spamassassin', $r); |
| 257 |
return; |
| 258 |
} |
| 259 |
|
| 260 |
my $recipient_count = scalar $transaction->recipients; |
| 261 |
|
| 262 |
$self->_cleanup_spam_header($transaction, 'X-Spam-User'); # always clean up |
| 263 |
if ($recipient_count > 1) { # add for multiple recipients |
| 264 |
$transaction->header->add('X-Spam-User', |
| 265 |
$username . ", $recipient_count recipients", |
| 266 |
0); |
| 267 |
} |
| 268 |
|
| 269 |
foreach my $name (keys %$new_headers) { |
| 270 |
next |
| 271 |
if $name eq 'X-Spam-Prev-Subject'; # might exist if SA rewrote subject |
| 272 |
if ($name eq 'X-Spam-Report') { |
| 273 |
next; # Mail::Header mangles this prefolded header |
| 274 |
|
| 275 |
# $self->log(LOGDEBUG, $new_headers->{$name} ); |
| 276 |
} |
| 277 |
if ($name eq 'X-Spam-Status') { |
| 278 |
$self->parse_spam_header($new_headers->{$name}); |
| 279 |
} |
| 280 |
$new_headers->{$name} =~ s/\015//; # hack for outlook |
| 281 |
$self->_cleanup_spam_header($transaction, $name); |
| 282 |
$transaction->header->add($name, $new_headers->{$name}, 0); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
sub assemble_message { |
| 287 |
my ($self, $transaction) = @_; |
| 288 |
|
| 289 |
$transaction->body_resetpos; |
| 290 |
|
| 291 |
my $message = |
| 292 |
"X-Envelope-From: " |
| 293 |
. $transaction->sender->format . "\n" |
| 294 |
. $transaction->header->as_string; |
| 295 |
|
| 296 |
while (my $line = $transaction->body_getline) { $message .= $line; } |
| 297 |
|
| 298 |
$message = join(CRLF, split /\n/, $message); |
| 299 |
return $message . CRLF; |
| 300 |
} |
| 301 |
|
| 302 |
sub connect_to_spamd { |
| 303 |
my $self = shift; |
| 304 |
my $socket = $self->{_args}{spamd_socket}; |
| 305 |
my $SPAMD; |
| 306 |
if ($socket && $socket =~ /\//) { # file path |
| 307 |
$SPAMD = $self->connect_to_spamd_socket($socket); |
| 308 |
} |
| 309 |
else { |
| 310 |
$SPAMD = $self->connect_to_spamd_tcpip($socket); |
| 311 |
} |
| 312 |
|
| 313 |
return if !$SPAMD; |
| 314 |
$SPAMD->autoflush(1); |
| 315 |
return $SPAMD; |
| 316 |
} |
| 317 |
|
| 318 |
sub connect_to_spamd_socket { |
| 319 |
my ($self, $socket) = @_; |
| 320 |
|
| 321 |
if (!$socket || $socket !~ /^([\w\/.-]+)$/) { # Unix Domain Socket |
| 322 |
$self->log(LOGERROR, "not a valid path"); |
| 323 |
return; |
| 324 |
} |
| 325 |
|
| 326 |
# Sanitize for use with taint mode |
| 327 |
$socket =~ /^([\w\/.-]+)$/; |
| 328 |
$socket = $1; |
| 329 |
|
| 330 |
socket(my $SPAMD, PF_UNIX, SOCK_STREAM, 0) or do { |
| 331 |
$self->log(LOGERROR, "Could not open socket: $!"); |
| 332 |
return; |
| 333 |
}; |
| 334 |
my $paddr = sockaddr_un($socket); |
| 335 |
|
| 336 |
connect($SPAMD, $paddr) or do { |
| 337 |
$self->log(LOGERROR, "Could not connect to spamd socket: $!"); |
| 338 |
return; |
| 339 |
}; |
| 340 |
|
| 341 |
$self->log(LOGDEBUG, "connected to spamd"); |
| 342 |
return $SPAMD; |
| 343 |
} |
| 344 |
|
| 345 |
sub connect_to_spamd_tcpip { |
| 346 |
my ($self, $socket) = @_; |
| 347 |
|
| 348 |
my $remote = 'localhost'; |
| 349 |
my $port = 783; |
| 350 |
|
| 351 |
if (defined $socket && $socket =~ /^([\w.-]+):(\d+)$/) { |
| 352 |
$remote = $1; |
| 353 |
$port = $2; |
| 354 |
} |
| 355 |
if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') } |
| 356 |
if (!$port) { |
| 357 |
$self->log(LOGERROR, "No spamd port, check your spamd_socket config."); |
| 358 |
return; |
| 359 |
} |
| 360 |
my $iaddr = inet_aton($remote) or do { |
| 361 |
$self->log(LOGERROR, "Could not resolve host: $remote"); |
| 362 |
return; |
| 363 |
}; |
| 364 |
my $paddr = sockaddr_in($port, $iaddr); |
| 365 |
my $proto = getprotobyname('tcp'); |
| 366 |
|
| 367 |
socket(my $SPAMD, PF_INET, SOCK_STREAM, $proto) or do { |
| 368 |
$self->log(LOGERROR, "Could not open socket: $!"); |
| 369 |
return; |
| 370 |
}; |
| 371 |
|
| 372 |
connect($SPAMD, $paddr) or do { |
| 373 |
$self->log(LOGERROR, "Could not connect to spamd: $!"); |
| 374 |
return; |
| 375 |
}; |
| 376 |
|
| 377 |
$self->log(LOGDEBUG, "connected to spamd"); |
| 378 |
return $SPAMD; |
| 379 |
} |
| 380 |
|
| 381 |
sub print_to_spamd { |
| 382 |
my ($self, $SPAMD, $message, $length, $username) = @_; |
| 383 |
|
| 384 |
print $SPAMD "HEADERS SPAMC/1.4" . CRLF; |
| 385 |
print $SPAMD "Content-length: $length" . CRLF; |
| 386 |
print $SPAMD "User: $username" . CRLF; |
| 387 |
print $SPAMD CRLF; |
| 388 |
print $SPAMD $message |
| 389 |
or $self->log(LOGWARN, "Could not print to spamd: $!"); |
| 390 |
|
| 391 |
$self->log(LOGDEBUG, "check_spam: finished sending to spamd"); |
| 392 |
} |
| 393 |
|
| 394 |
sub reject { |
| 395 |
my ($self, $transaction) = @_; |
| 396 |
|
| 397 |
my $sa_results = $self->get_spam_results($transaction) or do { |
| 398 |
$self->log(LOGNOTICE, "error, no results"); |
| 399 |
return DECLINED; |
| 400 |
}; |
| 401 |
my $score = $sa_results->{score}; |
| 402 |
if (!defined $score) { |
| 403 |
$self->log(LOGERROR, "error, error getting score"); |
| 404 |
return DECLINED; |
| 405 |
} |
| 406 |
|
| 407 |
my $ham_or_spam = $sa_results->{is_spam} eq 'Yes' ? 'Spam' : 'Ham'; |
| 408 |
if ($ham_or_spam eq 'Spam') { |
| 409 |
$self->adjust_karma(-1); |
| 410 |
} |
| 411 |
my $status = "$ham_or_spam, $score"; |
| 412 |
my $learn = ''; |
| 413 |
my $al = $sa_results->{autolearn}; # subject to local SA learn scores |
| 414 |
if ($al) { |
| 415 |
$self->adjust_karma(1) if $al eq 'ham'; |
| 416 |
$self->adjust_karma(-1) if $al eq 'spam'; |
| 417 |
$learn = "learn=" . $al; |
| 418 |
} |
| 419 |
|
| 420 |
my $reject = $self->{_args}{reject} or do { |
| 421 |
$self->log(LOGERROR, "error, reject disabled ($status, $learn)"); |
| 422 |
return DECLINED; |
| 423 |
}; |
| 424 |
|
| 425 |
if ($score < $reject) { |
| 426 |
if ($ham_or_spam eq 'Spam') { |
| 427 |
$self->log(LOGINFO, "fail, tolerated, $status < $reject, $learn"); |
| 428 |
return DECLINED; |
| 429 |
} |
| 430 |
else { |
| 431 |
$self->log(LOGINFO, "pass, $status < $reject, $learn"); |
| 432 |
return DECLINED; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
$self->log(LOGINFO, "fail, $status, > $reject, $learn"); |
| 437 |
return ($self->get_reject_type(), "spam score exceeded threshold"); |
| 438 |
} |
| 439 |
|
| 440 |
sub munge_subject { |
| 441 |
my ($self, $transaction) = @_; |
| 442 |
|
| 443 |
return if ($self->{_args}{headers} && $self->{_args}{headers} eq 'none'); |
| 444 |
|
| 445 |
my $sa = $self->get_spam_results($transaction) or return; |
| 446 |
my $qp_num = $self->{_args}{munge_subject_threshold}; |
| 447 |
my $required = $sa->{required}; |
| 448 |
|
| 449 |
if (!$qp_num) { |
| 450 |
$self->log(LOGDEBUG, "skipping munge, no user or qpsmtpd pref set"); |
| 451 |
return; |
| 452 |
}; |
| 453 |
return unless $sa->{score} > $required; |
| 454 |
return unless $sa->{score} > $qp_num; |
| 455 |
|
| 456 |
my $subject_prefix = $self->qp->config('subject_prefix') || '*** SPAM ***'; |
| 457 |
my $subject = $transaction->header->get('Subject') || ''; |
| 458 |
$transaction->header->replace('Subject', "$subject_prefix $subject"); |
| 459 |
} |
| 460 |
|
| 461 |
sub get_spam_results { |
| 462 |
my ($self, $transaction) = @_; |
| 463 |
|
| 464 |
if (defined $transaction->notes('spamassassin')) { |
| 465 |
return $transaction->notes('spamassassin'); |
| 466 |
} |
| 467 |
|
| 468 |
my $header = $transaction->header->get('X-Spam-Status') or return; |
| 469 |
my $r = $self->parse_spam_header($header); |
| 470 |
|
| 471 |
$self->log(LOGDEBUG, "$r->{is_spam}, $r->{score}"); |
| 472 |
$transaction->notes('spamassassin', $r); |
| 473 |
|
| 474 |
return $r; |
| 475 |
} |
| 476 |
|
| 477 |
sub parse_spam_header { |
| 478 |
my ($self, $string) = @_; |
| 479 |
|
| 480 |
# the X-Spam-Score header contents vary based on the settings in |
| 481 |
# the spamassassin *.cf files. Rather than parse via regexp, split |
| 482 |
# on the consistent whitespace and = delimiters. More reliable and |
| 483 |
# likely faster. |
| 484 |
my @parts = split(/\s+/, $string); |
| 485 |
my $is_spam = shift @parts; |
| 486 |
chomp @parts; |
| 487 |
chop $is_spam; # remove trailing , |
| 488 |
|
| 489 |
my %r; |
| 490 |
foreach (@parts) { |
| 491 |
my ($key, $val) = split(/=/, $_); |
| 492 |
$r{$key} = $val; |
| 493 |
} |
| 494 |
$r{is_spam} = $is_spam; |
| 495 |
|
| 496 |
# compatibility for SA versions < 3 |
| 497 |
if (defined $r{hits} && !defined $r{score}) { |
| 498 |
$r{score} = delete $r{hits}; |
| 499 |
} |
| 500 |
return \%r; |
| 501 |
} |
| 502 |
|
| 503 |
sub _cleanup_spam_header { |
| 504 |
my ($self, $transaction, $header_name) = @_; |
| 505 |
|
| 506 |
my $action = 'rename'; |
| 507 |
if ($self->{_args}->{leave_old_headers}) { |
| 508 |
$action = lc($self->{_args}->{leave_old_headers}); |
| 509 |
} |
| 510 |
|
| 511 |
return unless $action eq 'drop' || $action eq 'rename'; |
| 512 |
|
| 513 |
my $old_header_name = $header_name; |
| 514 |
$old_header_name = |
| 515 |
($old_header_name =~ s/^X-//) |
| 516 |
? "X-Old-$old_header_name" |
| 517 |
: "Old-$old_header_name"; |
| 518 |
|
| 519 |
for my $header ($transaction->header->get($header_name)) { |
| 520 |
$transaction->header->add($old_header_name, $header, 0) |
| 521 |
if $action eq 'rename'; |
| 522 |
$transaction->header->delete($header_name); |
| 523 |
} |
| 524 |
} |