#!/usr/bin/perl -w use File::Spec; my $DEF_RULES_DIR = '@@DEF_RULES_DIR@@'; # substituted at install time my $LOCAL_RULES_DIR = '@@LOCAL_RULES_DIR@@'; # substituted at install time use lib '@@INSTALLSITELIB@@'; # substituted at install time BEGIN { # Locate locally installed SA libraries *without* using FindBin, which generates # warnings and causes more trouble than its worth. We don't need to be too # smart about this BTW. my @bin = File::Spec->splitpath($0); my $bin = ($bin[0] ? File::Spec->joinpath(@bin[0..1]) : $bin[1]) # /home/jm/foo -> /home/jm || File::Spec->curdir; # foo -> . # These are common paths where the SA libs might be found. foreach (qw(lib ../lib ../lib/site_perl ../lib/spamassassin ../share/spamassassin/lib)) { my $dir = File::Spec->catdir($bin, split('/', $_)); if(-f File::Spec->catfile($dir, "Mail", "SpamAssassin.pm")) { unshift(@INC, $dir); last; } } } use Getopt::Long; use Pod::Usage; sub usage { my ($verbose, $message) = @_; my $ver = Mail::SpamAssassin::Version(); print "SpamAssassin version $ver\n"; pod2usage(-verbose => $verbose, -message => $message, -exitval => 64); } my %opt = ( 'create-prefs' => 1); eval { require Mail::SpamAssassin; require Mail::SpamAssassin::NoMailAudit; Getopt::Long::Configure(qw(gnu_getopt no_auto_abbrev no_ignore_case)); GetOptions( 'whitelist-factory|M=s' => \$opt{'whitelist-factory'}, 'auto-whitelist|a' => \$opt{'auto-whitelist'}, 'add-to-whitelist|W' => \$opt{'add-to-whitelist'}, 'remove-from-whitelist|R' => \$opt{'remove-from-whitelist'}, 'add-to-blacklist' => \$opt{'add-to-blacklist'}, 'warning-from|w=s' => \$opt{'warning-from'}, 'local-only|local|L' => \$opt{'local'}, 'stop-at-threshold|S' => \$opt{'stop-at-threshold'}, 'remove-markup|despamassassinify|d' => \$opt{'remove-markup'}, 'report|r' => \$opt{'report'}, 'config-file|config-dir|c|C=s' => \$opt{'config-file'}, 'prefs-file|p=s' => \$opt{'prefs-file'}, 'create-prefs!' => \$opt{'create-prefs'}, 'x' => sub { $opt{'create-prefs'} = 0 }, 'log-to-mbox|l=s' => \$opt{'log-to-mbox'}, 'error-code|exit-code|e:i' => \$opt{'error-code'}, 'test-mode|test|t' => \$opt{'test-mode'}, 'debug-level|debug|D:s' => \$opt{'debug-level'}, 'version|V' => \$opt{'version'}, 'help|h|?' => \$opt{'help'}, # NOTE: ignored for backwards compatibility. will be stripped # in a future release. 'pipe|P' => sub { warn "The -P option has been removed.\n" }, 'F=i' => sub { warn "The -F option has been removed.\n" }, 'add-from!' => sub { warn "The --add-from option has been removed.\n" }, ) or usage(0, "Unknown option!"); if (defined $opt{'help'}) { usage(0, "For more information read the spamassassin man page"); } if (defined $opt{'version'}) { print "SpamAssassin version " . Mail::SpamAssassin::Version() . "\n"; exit 0; } # 1. # Standard Mail::Audit start. No longer used due to bugs in M:A, regarding # handling of slightly misformatted input. # # require Mail::Audit; # my $mail = Mail::Audit->new(); # 2. # No use of Mail::Audit at all, apart from the accept(), reject() and # resend() methods (which are proxied transparently). Lovely. # use Mail::SpamAssassin::NoMailAudit; my $mail = Mail::SpamAssassin::NoMailAudit->new (); # For Mail::Audit users -- this is the magic. Just create a Mail::SpamAssassin # object like this, then run the check() method as below; if it returns a # non-undef value, then you've got spam, otherwise it's normal mail. # # You can then use the rewrite() method (passing in the Mail::Audit object) to # rewrite the spam. # # (This implementation does other stuff though, such as -t support; ignore that # stuff.) # create the tester factory my $spamtest = new Mail::SpamAssassin ({ rules_filename => $opt{'config-file'}, userprefs_filename => $opt{'prefs-file'}, local_tests_only => $opt{'local'}, stop_at_threshold => $opt{'stop-at-threshold'}, debug => defined($opt{'debug-level'}), dont_copy_prefs => ($opt{'create-prefs'} ? 0 : 1), DEF_RULES_DIR => $DEF_RULES_DIR, LOCAL_RULES_DIR => $LOCAL_RULES_DIR, }); # set debug levels, if any specified (only useful for this command-line # tool really) if (defined $opt{'debug-level'} && $opt{'debug-level'} ne '') { my $levels = $opt{'debug-level'}; while ($levels =~ s/^([a-z]+)=([+-]?\d+)[,;:]*//s) { $Mail::SpamAssassin::DEBUG->{$1} = $2 + 0; } if ($levels !~ /^\s*$/) { usage(0, "bad areas in --debug option ($levels)!"); } } # handle logging of received mails if ($opt{'log-to-mbox'}) { $mail->{noexit} = 1; $mail->accept ($opt{'log-to-mbox'}); $mail->{noexit} = 0; } # handle removing reports if ($opt{'remove-markup'}) { print $spamtest->remove_spamassassin_markup ($mail); $mail->ignore(); # will exit } # handle unconditional reportage if ($opt{'report'}) { $spamtest->report_as_spam ($mail); if ($opt{'warning-from'}) { $spamtest->reply_with_warning ($mail, $opt{'warning-from'}); } if ($opt{'log-to-mbox'}) { $mail->{noexit} = 1; $mail->accept ($opt{'log-to-mbox'}); $mail->{noexit} = 0; } $mail->ignore(); # will exit } ($opt{'auto-whitelist'} or $opt{'remove-from-whitelist'} or $opt{'add-to-whitelist'} or $opt{'add-to-blacklist'}) and eval { # create a factory for the persistent address list. # choose one of these implementations! # The -M "Mail::SpamAssassin::ImplClassAddrList" flag can be used # to switch between them. my $addrlistfactory; if ($opt{'whitelist-factory'}) { eval ' require '.$opt{'whitelist-factory'}.'; $addrlistfactory = '.$opt{'whitelist-factory'}.'->new(); '; if ($@) { warn $@; undef $addrlistfactory; } } else { require Mail::SpamAssassin::DBBasedAddrList; $addrlistfactory = Mail::SpamAssassin::DBBasedAddrList->new(); } $spamtest->set_persistent_address_list_factory ($addrlistfactory); }; if ($opt{'add-to-whitelist'}) { $spamtest->add_all_addresses_to_whitelist ($mail); if ($opt{'log-to-mbox'}) { $mail->{noexit} = 1; $mail->accept ($opt{'log-to-mbox'}); $mail->{noexit} = 0; } $mail->ignore(); # will exit } if ($opt{'remove-from-whitelist'}) { $spamtest->remove_all_addresses_from_whitelist ($mail); if ($opt{'log-to-mbox'}) { $mail->{noexit} = 1; $mail->accept ($opt{'log-to-mbox'}); $mail->{noexit} = 0; } $mail->ignore(); # will exit } if ($opt{'add-to-blacklist'}) { $spamtest->add_all_addresses_to_blacklist ($mail); if ($opt{'log-to-mbox'}) { $mail->{noexit} = 1; $mail->accept ($opt{'log-to-mbox'}); $mail->{noexit} = 0; } $mail->ignore(); # will exit } # not reporting? OK, do checks instead. Create a status object which # holds details of the message's spam/not-spam status. my $status = $spamtest->check ($mail); $status->rewrite_mail (); if ($opt{'test-mode'}) { # add the spam report to the end of the body as well, if testing. my $lines = $mail->body(); push (@{$lines}, split (/$/, $status->get_report())); $mail->body ($lines); } # if we're piping it, deliver it to stdout. print $mail->header(), "\n", join ('', @{$mail->body()}); if (defined $opt{'error-code'} && $status->is_spam ()) { exit ($opt{'error-code'} || 5) ; } exit; }; if ($@) { # eval failed; we died somewhere in there. warn $@; exit 70; # == EX_SOFTWARE in sysexits.h. caught by MTA } # this is never called, it's just used to shut up the warnings sub NEVERCALLED { $Mail::SpamAssassin::DEBUG = { }; } # --------------------------------------------------------------------------- =head1 NAME spamassassin - mail filter to identify spam using text analysis =head1 SYNOPSIS B [options] < I > I B B<-d> < I > B B<-r> [B<-w> I] < I B B<-W>|B<-R> < I Options: -P, --pipe Deliver to STDOUT (now default) -L, --local Local tests only (no online tests) -S, --stop-at-threshold Stop tests when the threshold is reached -a, --auto-whitelist Use auto-whitelists -M, --whitelist-factory Select whitelist factory -W, --add-to-whitelist Add addresses in mail to whitelist -R, --remove-from-whitelist Remove all addresses found in mail from whitelist -r, --report Report message as spam -w addr, --warning-from=addr Send a warning mail to sender from addr -d, --remove-markup Remove spam reports from a message -C file, --config-file=file Set configuration file -p prefs, --prefs-file=file Set user preferences file -x, --nouser-config Disable user config files -e, --exit-code Exit with a non-zero exit code if the tested message was spam -l filename, --log-to-mbox=file Log messages to a mbox file -t, --test-mode Pipe message through and add extra report to the bottom -D, --debug [area=n,...] Print debugging messages -V, --version Print version -h, --help Print usage message =head1 OPTIONS =over 4 =item B<-P>, B<--pipe> The B<-P> parameter will cause SpamAssassin to pipe the output to STDOUT. This is now the default mode of operation. =item B<-a>, B<--auto-whitelist>, B<--whitelist> Use auto-whitelists. Auto-whitelists track the long-term average score for each sender and then shift the score of new messages toward that long-term average. This can increase or decrease the score for messages, depending on the long-term behavior of the particular correspondent. See the README file for more details. =item B<-e>, B<--error-code>, B<--exit-code> Exit with a non-zero error code, if the message is determined to be spam. =item B<-h>, B<--help> Print help message and exit. =item B<-t>, B<--test-mode> Test mode. Pipe message through and add extra report. =item B<-r>, B<--report> Report this message as verified spam. This will submit the mail message read from STDIN to various spam-blocker databases. Currently, these are Vipul's Razor ( http://razor.sourceforge.net/ ) and the Distributed Checksum Clearinghouse ( http://www.rhyolite.com/anti-spam/dcc/ ). If the message contains SpamAssassin markup, this will be stripped out automatically before submission. The support modules for DCC and Razor must be installed for spam to be reported to each service. =item B<-W>, B<--add-to-whitelist> Add all email addresses, in the headers and body of the mail message read from STDIN, to the automatic whitelist. =item B<-R>, B<--remove-from-whitelist> Remove all email addresses, in the headers and body of the mail message read from STDIN, from the automatic whitelist. STDIN must get a full email message, so to remove a single address you have to get creative, eg: C or C =item B<-w> I, B<--warning-from>=I This flag is only useful in conjunction with B<-r>. It will send a reply mail to the sender of the tested mail, notifying them that their message has been trapped as spam, from the address supplied in I. See L. =item B<-l> I, B<--log-to-mbox>=I Log all mail messages that pass through the filter, to an mbox-format file named by I. Handy for use with B<-r> and B<-w>. =item B<-L>, B<--local> Do only the ''local'' tests, ones that do not require an internet connection to operate. Normally, SpamAssassin will try to detect whether you are connected to the net before doing these tests anyway, but for faster checks you may wish to use this. =item B<-S>, B<--stop-at-threshold> Stop spam checking as soon as the spam threshold is reached, to increase performance. This option also turns off Razor reporting. =item B<-d>, B<--remove-markup> Remove SpamAssassin markup (the "SpamAssassin results" report, X-Spam-Status headers, etc.) from the mail message. The resulting message, which will be more or less identical to the original, pre-SpamAssassin input, will be output to stdout. (Note: the message will not be exactly identical; some headers will be reformatted due to some features of the Mail::Internet package, but the body text will be.) =item B<-C> I, B<--config-file>=I, B<-c> I (deprecated) Read configuration from I. =item B<-p> I, B<--prefs-file>=I Read user score preferences from I. =item B<-D> [I], B<--debug> [I] Produce diagnostic output. The level of diagnostic output can be set for each area separately; I is the area of the code to instrument, and I is a positive or negative number indicating the debug level or bitmask for that area of code. For example, to produce diagnostic output on all rules that hit, use: spamassassin -D rulesrun=255 =item B<-x>, B<--nouser-config> Disable per-user configuration files. =item B<-M> I, B<--whitelist-factory>=I Select alternative whitelist factory. =back =head1 DESCRIPTION SpamAssassin is a mail filter to identify spam using text analysis and several internet-based realtime blacklists. Using its rule base, it uses a wide range of heuristic tests on mail headers and body text to identify "spam", also known as unsolicited commercial email. Once identified, the mail is then tagged as spam for later filtering using the user's own mail user-agent application. SpamAssassin also includes support for reporting spam messages to collaborative filtering databases, such as Vipul's Razor ( http://razor.sourceforge.net/ ). The default tagging operations that take place are detailed in L. =head1 CONFIGURATION FILES The rule base, text templates, and rule description text are loaded from the configuration files. By default, configuration data is loaded from the first existing directory in: F;F;F<./rules>;F<../rules> The configuration data in the first existing directory in: F;F;F;F;F are used to override any values which had already been set Spamassassin will read *.cf in these directories, in alphanumeric order within each directory (similar to SysV-style startup scripts). In other words, it will read F<10_misc.cf> before F<50_scores.cf> and F<20_body_tests.cf> before F<20_head_test.cf>. Options in later files will override earlier files. The user preferences (such as scores to attach to each rule), are loaded from the file specified in the B<-p> argument. If this is not specified, F<~/.spamassassin/user_prefs> is used if it exists. C will create this file if it does not exist, using F as a template. This file will be looked for in F;F;F =head1 TAGGING The following two sections detail the tagging that takes place for spam messages, first of all, and for non-spam messages. Note that if you use the B<-t> argument, all mails will be tagged as if they are spam messages. =head2 TAGGING FOR SPAM MAILS The modifications made are as follows: =over 4 =item Subject: header The string C<*****SPAM*****> is prepended to the subject, unless the C configuration option is given. =item X-Spam-Status: header A string, C is set in this header to reflect the filter status. =item X-Spam-Flag: header Set to C. =item X-Spam-Report: header for spam mails The SpamAssassin report is added to the mail header if the C configuration option is given. =item Content-Type: header Set to C, in order to defang HTML mail or other active content that could "call back" to the spammer. =item spam mail body text The SpamAssassin report is added to top of the mail message body, unless the C configuration option is given. =back =head2 TAGGING FOR NON-SPAM MAILS =over 4 =item X-Spam-Status: header A string, C is set in this header to reflect the filter status. =back =head1 SPAM TRAPPING Quite often, if you've been on the internet for a while, you'll have accumulated a few old email accounts that nowadays get nothing but spam. SpamAssassin lets you set them up as aliases, as follows: =over 4 =item spamtrap1: "| /path/to/spamassassin -r -w spamtrap1" =back This will add any incoming mail messages straight into spam-tracking databases, such as Vipul's Razor; send an explanatory reply message to the sender, from the I address; then drop the mail into the bit-bucket. The explanatory reply text is taken from the SpamAssassin configuration file, where it is stored in the C lines. If you want to keep a copy of the mails, use something like this: =over 4 =item spamtrap1: "| /path/to/spamassassin -r -w spamtrap1 -l /var/spam/caught" =back It is suggested you familiarise yourself with how MTAs run programs specified in aliases, if you plan to do this; for one thing, B will not run under your user id in this case. If you are nervous about this, create a user for spamtrapping, and set up spamassassin in its F<.forward> file. =head1 INSTALLATION The B command is part of the B Perl module. Install this as a normal Perl module, using C, or by hand. =head1 ENVIRONMENT No environment variables, aside from those used by perl, are required to be set. =head1 SEE ALSO Mail::SpamAssassin(3) Mail::SpamAssassin::Conf(3) Mail::Audit(3) Razor(3) =head1 AUTHOR Justin Mason Ejm /at/ jmason.orgE =head1 PREREQUISITES C =head1 COREQUISITES C C =cut