=head1 NAME spamassassin_spamc - SpamAssassin integration for qpsmtpd via spamc $Id: spamassassin_spamc 443 2005-01-16 21:40:19Z aqua $ =head1 DESCRIPTION Plugin that checks if the mail is spam by using the "spamc" client application from SpamAssassin F. =head1 CONFIGURATION Configuration as to spamassassin's actual scoring should be done in the spamassassin user_prefs file. The spamassassin plugin supplies configuration options relevant to executing spamc and directing it to the proper spamd host. The format (from qpsmtpd's F) consists of pairs of option names and their values: spamassassin_spamc [option value] [...] Options being those listed below and the values being parameters to the options. Confused yet? :-) =over 4 =item host I Host to which spamc should connect to reach a spamd. By default spamc will connect to localhost. Equates to spamc -u. =item port I Port on which spamc should connect to spamd. By default spamc uses port 783. Equates to spamc -p. =item user I User as which spamd should run when scoring. By default spamd will run as the user running spamc (e.g. qmaild). Equates to spamc -u. =item timeout I Timeout duration when spamc should abort the connection. Mail will not be rejected because of timeouts. =head1 AUTHOR The spamassassin plugin was originally written by Ask Bjorn Hansen; this rewrite by Devin Carraway. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. =head1 SEE ALSO spamc(1), spamd(1) =cut use File::Temp qw/tempfile/; sub register { my ($self, $qp, @args) = @_; $self->register_hook("data_post", "check_spam"); $self->log(LOGERROR, "Bad parameters for the spamassassin plugin") if @_ % 2; %{$self->{_args}} = @args; } sub check_spam { my ($self, $transaction) = @_; my $spool_dir = $self->qp->config('spool_dir') ? $self->qp->config('spool_dir') : Qpsmtpd::Utils::tildeexp('~/tmp/'); $spool_dir =~ /(.*)/ and $spool_dir = $1; for (keys %{$self->{_args}}) { $self->{_args}->{$_} = $1 if $self->{_args}->{$_} =~ /(.*)/; } $self->{_args}->{max_size} ||= 1024 * 64; if ($transaction->body_size > $self->{_args}->{max_size}) { $self->log(LOGWARN, 'Mail too large for spamc scan ('. $transaction->body_size . " vs $self->{_args}->{max_size})"); return (DECLINED); } $transaction->header->delete($_) for ('X-Spam-Flag', 'X-Spam-Status', 'X-Spam-Level', 'X-Spam-Checker-Version'); my ($tmp, $tmp_fn) = tempfile(DIR => $spool_dir, SUFFIX => '.spamc'); ($tmp && $tmp_fn) or return DECLINED; print $tmp $transaction->header->as_string, "\n"; my $line; print $tmp $line while $line = $transaction->body_getline; $transaction->body_resetpos; close $tmp; my $cmd = join(' ', '/usr/bin/spamc ', (($self->{_args}->{host}) ? ('-d',$self->{_args}->{host}) : ()), (($self->{_args}->{port}) ? ('-p',$self->{_args}->{port}) : ()), (($self->{_args}->{ssl}) ? ('-S') : ()), (($self->{_args}->{max_size}) ? ('-s',$self->{_args}->{max_size}) : ()), (($self->{_args}->{user}) ? ('-u',$self->{_args}->{user}) : ()), (($self->{_args}->{timeout}) ? ('-t',$self->{_args}->{timeout}) : ()), ); open(RD, "$cmd < '$tmp_fn'|") or do { $self->log(LOGCRIT,"$cmd: $!"); unlink $tmp_fn or $self->log(1, "unlink: $tmp_fn: $!"); return DECLINED; }; my $h = new Mail::Header; $h->read(\*RD); while () { chomp; # $self->log(LOGDEBUG,"spamc returned body: [$_]"); } close RD; unlink $tmp_fn or $self->log(LOGERROR, "unlink: $tmp_fn: $!"); my $status = $h->get('X-Spam-Status'); unless ($status) { $self->log(LOGWARN, "spamc didn't return an X-Spam-Status header; ". "doing nothing"); return DECLINED; } chomp $status; $transaction->header($h); if ($status =~ /(?:hits|score)=(-?[0-9.]+)\s+required=(-?[0-9.]+)/) { $self->log(LOGNOTICE,"mail earned score $1, needed $2"); if ($1 >= $2) { $self->log(LOGNOTICE,"report: $status"); return (DENY, 'spam score exceeded'); } } return DECLINED; }