Ben's Blog

I.T., unix / linux ben June 06, 2009

Postfix & Spamassassin integration allowing for custom processing

This assumes that you have postfix installed and running as your SMTP server

First, make sure that you’re root

[bash]whoami[/bash]

I probably shouldn’t have to explain that if you’re reading this but just in case; if that last command returned something else than ‘root’ issue the following command

[bash]sudo su[/bash]

and enter your password

step 1: Let’s install the packages we’re gonna need

[bash]apt-get update
apt-get install spamassassin spamc[/bash]

step 2: Now we configure spamassasin

[bash]cat /etc/default/spamassassin | sed -e ‘s#ENABLED=0#ENABLED=1#g’ > /etc/default/spamassassin
cat /etc/default/spamassassin | sed -e ‘s#CRON=0#CRON=1#g’ > /etc/default/spamassassin
cat /etc/spamassassin/local.cf | set -e ‘s## rewrite_header Subject *****SPAM*****#rewrite_header Subject [*****SPAM*****] > /etc/spamassassin/local.cf[/bash]

and we start/restart it

[bash]/etc/init.d/spamassassin restart[/bash]

step 3: We create a little script that will take desired action upon spamassassin flagging

create a user called spamassassin (or whatever you want as long as you keep it consistent)

[bash]useradd -m spamassassin[/bash]

then edit the script file /home/spamassassin/spamcheck and throw the following in it

[bash] # variables
 
SENDMAIL="/usr/sbin/sendmail -i"
 
EGREP=/bin/egrep
 
SPAMLIMIT=10
 
# exit codes from <sysexits.h>
 
EX_UNAVAILABLE=69
 
# clean up when done or when aborting.
 
trap "rm -f /tmp/out.$$" 0 1 2 3 15
 
# pipe message to spamc
 
cat | /usr/bin/spamc -u spamd > /tmp/out.$$
 
# are there more than $SPAMLIMIT stars in X-Spam-Level header? :
 
if $EGREP -q "^X-Spam-Level: *{$SPAMLIMIT,}" < /tmp/out.$$
 
then
 
# option 1: move spam messages to sideline dir so a human can look at them later:
 
mv /tmp/out.$$ /home/spamassassin/`date +%Y-%m-%d_%R`-$$
 
# option 2: divert spam message to an alternate e-mail address:
 
#$SENDMAIL xyz@xxxx.xx < /tmp/out.$$
 
# option 3: just delete the spam message
 
# rm -f /tmp/out.$$
 
# option 4: still relay the email to the recipient with the subject of the email now containing [*****SPAM*****]
 
# $SENDMAIL "$@" < /tmp/out.$$
 
else
 
$SENDMAIL "$@" < /tmp/out.$$
 
fi
 
# Postfix returns the exit status of the Postfix sendmail command.
 
exit $?[/bash]

make sure that you

[bash]chown spamassassin:spamassassin /home/spamassassin/spamcheck
chmod 750 /home/spamassassin/spamcheck[/bash]

step 4: Ok, so we got spamassassin going and a little script that will take an email and throw it in /home/spamassassin if it’s spam (if you chose option1) now we just need to tell postfix to pass all messages to that script

edit /etc/postfix/master.cf and replace

[code]smtp inet n – – – – smtpd[/code]

with

[code]smtp inet n – n – – smtpd -o content_filter=spamcheck:dummy[/code]

also add the following 2 lines at the bottom of the file (the indentation is important)

[code]spamcheck unix – n n – 10 pipe

flags=Rq user=spamassassin argv=/bin/spamcheck -f ${sender} — ${recipient}[/code]

We’re almost there, just restart postfix and you’re good to go!

[bash]/etc/init.d/postfix restart[/bash]

If you wanna test that out, watch the log while you send emails to your servers

[bash]tail -f /var/log/syslog[/bash]

send a clean mail, make sure that it reaches destination, then send something you know will get flagged as spam and make sure it ends up in /home/spamassasin instead of the intended recipient.

The reason we choose option 1 here is because there’s no point in still relaying a flagged email as it will still clog the recipient’s mailbox. On the other hand we don’t want to just delete it if spamassassin makes a mistake we want to play it safe and keep every emails should something arise, we quarantine the bad ones in /home/spamassassin

Lastly, as long as you have postfix just feeding the emails to a script like we just did, it’s easy to become fancier and do all kinds of processing to the email, on my server I actually call a php script that throws emails in a DB.

Leave a Comment

Cancel reply

This blog is solar powered

Interactive

Handwriting Capture
Mandalagaba
IPv6 link-local to MAC converter
IPv6 MAC to link-local converter
Markov Text Generation
Markov Word Generation
Markov Music Generation
Duplogrifier
Flood Fill Algorithms
Homestead Metrics
RGB Playground
Web Games

Categories

  • aesthetics111
    • plots54
    • specular holography6
  • Books3
  • I.T.202
    • 3D modeling / printing21
    • AI6
    • all out geekery36
    • electronics27
    • homestead automation6
    • maniacal paranoia25
    • plotters49
    • unix / linux29
    • video games4
    • web development29
    • web games3
  • Lego / Duplo67
  • life in the U.S.42
  • miscellaneous202
  • nature encounters114
  • old vinyls3
  • organs2
  • self sustainability560
    • agriculture105
    • apiculture38
    • apple20
    • building131
    • canning3
    • crochet6
    • foraging6
    • hunting10
    • maple syrup47
    • poultry39
    • preserving2
    • solar power28
    • water23
    • wood84
  • trip to a new life6
Theme by Bloompixel. Proudly Powered by WordPress