Setup SPAM/HAM tagging

This is a simple method for SPAM/HAM tagging for use within DTC/Postfix/SpamAssassin.

It is based off the following HOWTO http://gtmp.org/doku.php/publications/sa-postfix-en, but adapted slightly for DTC.

Note that the current version of DTC do the setup automatically out of the box and that there is nothing more to do.

1. Create a file /usr/local/bin/sa-wrapper (with a chmod +x) with the following contents:

#!/usr/bin/perl -w
# Time-stamp: <05 April 2004, 13:37 home>
#
# sa-wrapper.pl
#
# SpamAssassin sa-learn wrapper
# (c) Alexandre Jousset, 2004
# This script is GPL'd
#
# Thanks to: Chung-Kie Tung for the removal of the dir
#            Adam Gent for bug report
#
# v1.2

use strict;
use MIME::Tools;
use MIME::Parser;

my $DEBUG = 1;
my $UNPACK_DIR = '/var/lib/amavis/tmp';
my $SA_LEARN = '/usr/bin/sa-learn';
# my @DOMAINS = qw/example.com example.org/;
# read from file instead
my $file='/var/lib/dtc/etc/local_domains';
open (FH, "< $file") or die "Can't open $file for read: $!";
my @DOMAINS = <FH>;
close FH or die "Cannot close $file: $!"; 
@DOMAINS = trim(@DOMAINS);

# trim the whitespace off the array or string
sub trim 
{
    my @out = @_;
    for (@out) 
    {
        s/^\s+//;
        s/\s+$//;
    }
    return wantarray ? @out : $out[0];
}


my ($spamham, $sender) = @ARGV;

sub recurs
{
    my $ent = shift;

    if ($ent->head->mime_type eq 'message/rfc822') {
        if ($DEBUG) {
            unlink "/tmp/spam.log.$$" if -e "/tmp/spam.log.$$";
            open(OUT, "|$SA_LEARN -D --$spamham --single >>/tmp/spam.log.$$ 2>&1") or die "Cannot pipe $SA_LEARN: $!";
        } else {
            open(OUT, "|$SA_LEARN --$spamham --single") or die "Cannot pipe $SA_LEARN: $!";
        }

        $ent->bodyhandle->print(\*OUT);

        close(OUT);
        return;
    }

    my @parts = $ent->parts;

    if (@parts) {
        map { recurs($_) } @parts;
    }
}

my ($domain) = $sender =~ /\@(.*)$/;
unless (grep { $_ eq $domain } @DOMAINS) {
    die "$sender, I don't recognize your domain ($domain)!";
}

if ($DEBUG) {
    MIME::Tools->debugging(1);
    open(STDERR, ">/tmp/spam_err.log");
}
my $parser = new MIME::Parser;
$parser->extract_nested_messages(0);
$parser->output_under($UNPACK_DIR);

my $entity;
eval {
    $entity = $parser->parse(\*STDIN);
};

if ($@) {
    die $@;
} else {
    recurs($entity);
}

$parser->filer->purge;
rmdir $parser->output_dir;

2. Add the following two entries to /etc/postfix/master.cf

sa-spam unix    -       n       n       -       -       pipe
    -o smtpd_client_restrictions=permit_sasl_authenticated,permit_mynetworks,reject
    user=amavis:amavis argv=/usr/local/bin/sa-wrapper spam ${sender}
sa-ham  unix    -       n       n       -       -       pipe
    -o smtpd_client_restrictions=permit_sasl_authenticated,permit_mynetworks,reject
    user=amavis:amavis argv=/usr/local/bin/sa-wrapper ham  ${sender}

3. Add the following entry to /etc/postfix/main.cf

# spam learning transport maps
transport_maps = hash:/etc/postfix/transport

4. Create the file /etc/postfix/transport with the following contents:

spam.spam       sa-spam:
ham.ham         sa-ham:

5. postmap /etc/postfix/transport

6. Add the following entries to /etc/aliases

# auto-spam-learning
spam:           spam@spam.spam
ham:            ham@ham.ham
notspam:        ham@ham.ham

7. newaliases

8. Using the system

Once it's setup, you should send mail to the system user spam or ham. That is you should NOT use any domain installed in DTC, but instead the one setup as system accounts. Let's say your mx name is mx.xenXXXXYY.gplhost.com, then you should send a mail to spam at mx.xenXXXXYY.gplhost.com with the spam as attachment (so it preserves the headers). DO NOT send it to any of the installed domains in DTC, it wont work. It has to reach the mail server as if you were sending to system account, and NOT to a virtual domain.

Also, as we have set -o smtpd_client_restrictions=permit_sasl_authenticated,permit_mynetworks,reject you can send email to spam / ham only if you are authenticated using SMTP with password, or if you are sending from localhost (for example using a webmail).

Eventually, you can install the following plug-in with thunderbird:

https://addons.mozilla.org/en-US/thunderbird/addon/2672

then configure your icon toolbar to add the icon, and set the custom email in the configuration screen of the plugin.

9. Add IMAP support (not mandatory)

See here: http://wiki.boum.org/pub/TechStdOut/SpamAssassinCollectiveEducation/sa-education-false-negatives(approve sites)

Editing this page means accepting its license.

Page last modified on May 15, 2008, at 12:48 PM EST