spamassassin.sh 2.23 KB
Newer Older
1 2
# Spam filtering with spamassassin via spampd
#############################################
Joshua Tauberer's avatar
Joshua Tauberer committed
3

4 5 6 7 8
# spampd sits between postfix and dovecot. It takes mail from postfix
# over the LMTP protocol, runs spamassassin on it, and then passes the
# message over LMTP to dovecot for local delivery.

# In order to move spam automatically into the Spam folder we use the dovecot sieve
9 10
# plugin. The tools/mail.py tool creates the necessary sieve script for each mail
# user when the mail user is created.
11

12
source setup/functions.sh # load our functions
13

14
# Install packages.
15
apt_install spampd razor pyzor dovecot-antispam
16 17 18 19 20 21

# Allow spamassassin to download new rules.
tools/editconf.py /etc/default/spamassassin \
	CRON=1

# Configure pyzor.
22
hide_output pyzor discover
Joshua Tauberer's avatar
Joshua Tauberer committed
23

24 25
# Pass messages on to docevot on port 10026.
# This is actually the default setting but we don't want to lose track of it.
26
# We've already configured Dovecot to listen on this port.
Joshua Tauberer's avatar
Joshua Tauberer committed
27 28
tools/editconf.py /etc/default/spampd DESTPORT=10026

29
# Enable the Dovecot antispam plugin to detect when a message moves between folders so we can
Joshua Tauberer's avatar
Joshua Tauberer committed
30 31 32
# pass it to sa-learn for training. (Be careful if we use multiple plugins later.)
sudo sed -i "s/#mail_plugins = .*/mail_plugins = \$mail_plugins antispam/" /etc/dovecot/conf.d/20-imap.conf

33
# When mail is moved in or out of the Dovecot Spam folder, re-train using this script
34
# that sends the mail to spamassassin.
Joshua Tauberer's avatar
Joshua Tauberer committed
35 36 37 38 39 40 41 42 43
# from http://wiki2.dovecot.org/Plugins/Antispam
cat > /usr/bin/sa-learn-pipe.sh << EOF;
cat<&0 >> /tmp/sendmail-msg-\$\$.txt
/usr/bin/sa-learn \$* /tmp/sendmail-msg-\$\$.txt > /dev/null
rm -f /tmp/sendmail-msg-\$\$.txt
exit 0
EOF
chmod a+x /usr/bin/sa-learn-pipe.sh

44
# Configure the antispam plugin to call sa-learn-pipe.sh.
Joshua Tauberer's avatar
Joshua Tauberer committed
45 46 47 48 49
cat > /etc/dovecot/conf.d/99-local-spampd.conf << EOF;
plugin {
    antispam_backend = pipe
    antispam_spam_pattern_ignorecase = SPAM
    antispam_allow_append_to_spam = yes
50 51
    antispam_pipe_program_spam_args = /usr/bin/sa-learn-pipe.sh;--spam
    antispam_pipe_program_notspam_args = /usr/bin/sa-learn-pipe.sh;--ham
Joshua Tauberer's avatar
Joshua Tauberer committed
52 53 54 55 56 57 58 59
    antispam_pipe_program = /bin/bash
}
EOF

# Initial training?
# sa-learn --ham storage/mail/mailboxes/*/*/cur/
# sa-learn --spam storage/mail/mailboxes/*/*/.Spam/cur/

60
# Kick services.
61 62
restart_service spampd
restart_service dovecot
Joshua Tauberer's avatar
Joshua Tauberer committed
63