anomy-list

Perl filter script

From: Chris Wik (122769@xyz.molar.is)
Date: Sat 17 Apr 2004 - 14:56:30 GMT

  • Next message: Alan Munday: "RE: Perl filter script"

    Dear List,

    Pardon me if this is not an anomy specific question, but I thought
    this would probably be the best place to ask for assistance of this
    nature.

    I've written a Perl script which calls Anomy, and SpamAssassin. I'm
    invoking this script from Postfix with only one argument, the "to"
    address. I'm reading my mail message from STDIN, as the standard
    Anomy filter.sh script does. The script runs as the user "filter", as
    does spamd. In postfix's main.cf, "mydomain" is set to "anu.net"

    My problem is this: when my script passes the message back to Postfix
    with the sendmail command, the message appears to be coming from
    "122803@xyz.molar.is". How can I make the message appear to come from a
    local user, as it does when using the filter.sh script?

    Apr 17 04:43:54 mx3 postfix/pipe[21399]: 7F0F113A74A:
    to=<122769@xyz.molar.is>, relay=filter, delay=4, status=sent (dummy)
    Apr 17 04:43:54 mx3 postfix/qmgr[12225]: 2234E13A74F:
    from=<122803@xyz.molar.is>, size=3091, nrcpt=1 (queue active)
    Apr 17 04:43:54 mx3 postfix/smtp[21415]: 2234E13A74F:
    to=<122769@xyz.molar.is>, relay=mail2.anu.net[213.247.49.242], delay=0,
    status=bounced (host mail2.anu.net[213.247.49.242] said: 550 5.1.1
    user not known (in reply to MAIL FROM command))

    My mail server is bouncing this message because "122803@xyz.molar.is" did
    not exist, not because "122769@xyz.molar.is" does not exist. If I create a
    user "filter" in my anu.net domain, whose login is disabled and mail
    goes to /dev/null, then the script works.

    Comments, suggestions welcome!

    TIA,
    Chris

    #!/usr/bin/perl

    # define paths
    $spamc = '/usr/bin/spamc -f';
    $sendmail = '/usr/sbin/sendmail -i';
    $workdir = '/var/spool/filter/';

    # anomy setup
    $anomy = '/usr/local/anomy/bin/sanitizer.pl';
    $anomyconf = '/usr/local/anomy/sanitizer.cfg';
    $anomylog = '/var/log/anomy.log';
    $ENV{'ANOMY'} = '/usr/local/anomy';

    # define script variables
    $to = $ARGV[0];
    $message = '';
    $spamc = $spamc . ' -u "' . $to . '"';
    $parsed_file = $workdir . 'parsed' . $$;
    $anomy_file = $workdir . 'anomy' . $$;
    $delete_msg = "no";
    $spam_score = '0';
    $spam_level = '';

    # read in message
    while(<STDIN>) { $message .= $_ }

    # pass message to spamc for processing with spamd
    # output redirected to file $parsed_file
    open(SPAMC, "|$spamc > $parsed_file") or die "Could not open spamc
    for piping: $!";
    print SPAMC "$message";
    close SPAMC;

    # run the messsage through Anomy sanitizer script
    `cat $parsed_file | $anomy $anomyconf 2>>$anomylog > $anomy_file`;

    # read in parsed message to determine if we need to take further action
    open(PMSG, "$anomy_file") or die "Could not open parsed message: $!";
    while(<PMSG>) {
            if(/^X-Spam-Delete: /) { $delete_msg = substr($_, 15) }
            if(/^X-Spam-Level: /) { $spam_level = substr($_, 15) }
            if(/^X-Spam-Redirect: /) { $to = substr($_, 17) }
    }
    # find out how many stars (hits) the message has
    $spam_score = length $spam_level;

    if(($delete_msg eq 'no') or ($delete_msg <= $spam_score)) {
            # pass on parsed message to sendmail
            system("cat $anomy_file | $sendmail $to");
    }

    -- 
    Chris Wik
    Systems Admin
    ANU Internet Services
    http://www.anu.net/
    



    hosted by molar.is