Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: My bash's test recommends = over ==, and its trap demands TERM over SIGTERM

...

No Format
#!/bin/sh
SENDMAIL="/usr/sbin/sendmail -i"
SPAMASSASSIN=/usr/bin/spamc

# Exit codes from <sysexits.h>
EX_TEMPFAIL=75
EX_UNAVAILABLE=69

umask 077

OUTPUT="`mktemp mailfilter.XXXXXXXXXX`"
if [ "$?" != 0 ]; then
    /usr/bin/logger -s -p mail.warning -t filter \
        "Unable to create temporary file."
    exit $EX_TEMPFAIL
fi

# Clean up when done or when aborting.
trap "rm -f $OUTPUT" EXIT SIGTERMTERM

$SPAMASSASSIN -x > $OUTPUT
return="$?"
if [ "$return" == 1 ]; then
    echo "Message content rejected"
    exit $EX_UNAVAILABLE
elif [ "$return" != 0 ]; then
    /usr/bin/logger -s -p mail.warning -t filter \
        "Temporary SpamAssassin failure (spamc returnreturned $return)"
    exit $EX_TEMPFAIL
fi

$SENDMAIL "$@" < $OUTPUT
exit $?

...

No Format
#!/bin/sh

# filter.sh
#
# This script redirects mail flagged as spam to a separate account
# You must first create a user account named "spamvac" to hold the flagged mail

SENDMAIL="/usr/sbin/sendmail -i"
SPAMASSASSIN=/usr/bin/spamc
COMMAND="$SENDMAIL $@"
USER=`echo $COMMAND | awk '{ print $NF }' | sed 's/@.*$//'`
NEW_COMMAND=`echo $COMMAND | awk '{ $6 = "spamvac"; NF = 6; print }'`

# Exit codes from <sysexits.h>
EX_TEMPFAIL=75
EX_UNAVAILABLE=69

umask 077

OUTPUT="`mktemp /tmp/mailfilter.XXXXXXXXXX`"

if [ "$?" != 0 ]; then
    /usr/bin/logger -s -p mail.warning -t filter "Unable to create temporary file."
    exit $EX_TEMPFAIL
fi

# Clean up when done or when aborting.
trap "rm -f $OUTPUT" EXIT SIGTERMTERM

$SPAMASSASSIN -x -E -u $USER > $OUTPUT
return="$?"
if [ "$return" == 1 ]; then
    $NEW_COMMAND < $OUTPUT
    exit $?
elif [ "$return" != 0 ]; then
    /usr/bin/logger -s -p mail.warning -t filter "Temporary SpamAssassin failure (spamc returnreturned $return)"
    exit $EX_TEMPFAIL
fi

$SENDMAIL "$@" < $OUTPUT
exit $?

This causes incoming smtp mail to be checked for spam. If the mail's spam score exceeds the threshold (set in local.cf, or in ~/.spamassassin/user_prefs) then it spamc -E returns 1 and the mail is redirected to the spamvac account. Otherwise it is passed on to the intended recipient. Bounces no longer occur except on a true error condition.