2014-09-27 16:16:15 +00:00
|
|
|
#!/bin/bash
|
2013-09-01 14:13:51 +00:00
|
|
|
# Spam filtering with spamassassin via spampd
|
2014-10-04 21:57:26 +00:00
|
|
|
# ===========================================
|
|
|
|
#
|
2013-09-01 14:13:51 +00:00
|
|
|
# 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.
|
2014-10-04 21:57:26 +00:00
|
|
|
#
|
2013-09-01 14:13:51 +00:00
|
|
|
# In order to move spam automatically into the Spam folder we use the dovecot sieve
|
2014-10-04 21:57:26 +00:00
|
|
|
# plugin.
|
2013-09-01 14:13:51 +00:00
|
|
|
|
2014-09-27 16:16:15 +00:00
|
|
|
source /etc/mailinabox.conf # get global vars
|
2014-06-03 11:12:38 +00:00
|
|
|
source setup/functions.sh # load our functions
|
2014-05-01 19:13:00 +00:00
|
|
|
|
2013-09-01 14:13:51 +00:00
|
|
|
# Install packages.
|
2014-07-10 07:18:01 +00:00
|
|
|
apt_install spampd razor pyzor dovecot-antispam
|
2014-04-23 16:45:46 +00:00
|
|
|
|
|
|
|
# Allow spamassassin to download new rules.
|
|
|
|
tools/editconf.py /etc/default/spamassassin \
|
|
|
|
CRON=1
|
|
|
|
|
|
|
|
# Configure pyzor.
|
2014-07-16 13:06:45 +00:00
|
|
|
hide_output pyzor discover
|
2013-08-23 15:59:28 +00:00
|
|
|
|
2013-09-01 14:13:51 +00:00
|
|
|
# Pass messages on to docevot on port 10026.
|
|
|
|
# This is actually the default setting but we don't want to lose track of it.
|
2014-04-23 16:45:46 +00:00
|
|
|
# We've already configured Dovecot to listen on this port.
|
2013-08-23 15:59:28 +00:00
|
|
|
tools/editconf.py /etc/default/spampd DESTPORT=10026
|
|
|
|
|
2014-04-23 16:45:46 +00:00
|
|
|
# Enable the Dovecot antispam plugin to detect when a message moves between folders so we can
|
2014-10-04 21:57:26 +00:00
|
|
|
# pass it to sa-learn for training.
|
|
|
|
# (Be careful if we use multiple plugins later.) #NODOC
|
2014-10-04 18:06:08 +00:00
|
|
|
sed -i "s/#mail_plugins = .*/mail_plugins = \$mail_plugins antispam/" /etc/dovecot/conf.d/20-imap.conf
|
2013-08-23 15:59:28 +00:00
|
|
|
|
2014-04-23 16:45:46 +00:00
|
|
|
# When mail is moved in or out of the Dovecot Spam folder, re-train using this script
|
2013-09-01 14:13:51 +00:00
|
|
|
# that sends the mail to spamassassin.
|
2013-08-23 15:59:28 +00:00
|
|
|
# from http://wiki2.dovecot.org/Plugins/Antispam
|
2014-10-04 21:57:26 +00:00
|
|
|
rm -f /usr/bin/sa-learn-pipe.sh # legacy location #NODOC
|
2014-09-27 16:06:11 +00:00
|
|
|
cat > /usr/local/bin/sa-learn-pipe.sh << EOF;
|
2013-08-23 15:59:28 +00:00
|
|
|
cat<&0 >> /tmp/sendmail-msg-\$\$.txt
|
2014-09-27 16:16:15 +00:00
|
|
|
/usr/bin/sa-learn \$* /tmp/sendmail-msg-\$\$.txt > /dev/null
|
2013-08-23 15:59:28 +00:00
|
|
|
rm -f /tmp/sendmail-msg-\$\$.txt
|
|
|
|
exit 0
|
|
|
|
EOF
|
2014-09-27 16:06:11 +00:00
|
|
|
chmod a+x /usr/local/bin/sa-learn-pipe.sh
|
2013-08-23 15:59:28 +00:00
|
|
|
|
2013-09-01 14:13:51 +00:00
|
|
|
# Configure the antispam plugin to call sa-learn-pipe.sh.
|
2013-08-23 15:59:28 +00:00
|
|
|
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
|
2014-09-27 16:06:11 +00:00
|
|
|
antispam_pipe_program_spam_args = /usr/local/bin/sa-learn-pipe.sh;--spam
|
|
|
|
antispam_pipe_program_notspam_args = /usr/local/bin/sa-learn-pipe.sh;--ham
|
2013-08-23 15:59:28 +00:00
|
|
|
antispam_pipe_program = /bin/bash
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
|
2014-09-27 16:16:15 +00:00
|
|
|
# Tell spamassassin where to load and store site-wide bayesean filtering data.
|
2014-09-20 15:07:30 +00:00
|
|
|
|
|
|
|
mkdir -p $STORAGE_ROOT/mail/spamassassin
|
2014-10-11 17:00:22 +00:00
|
|
|
chown -R spampd:spampd $STORAGE_ROOT/mail/spamassassin
|
2014-09-20 15:07:30 +00:00
|
|
|
chmod -R 775 $STORAGE_ROOT/mail/spamassassin
|
|
|
|
|
2014-09-27 16:16:15 +00:00
|
|
|
tools/editconf.py /etc/spamassassin/local.cf -s \
|
|
|
|
bayes_path=$STORAGE_ROOT/mail/spamassassin/bayes
|
2014-09-20 15:07:30 +00:00
|
|
|
|
2013-08-23 15:59:28 +00:00
|
|
|
# Initial training?
|
|
|
|
# sa-learn --ham storage/mail/mailboxes/*/*/cur/
|
|
|
|
# sa-learn --spam storage/mail/mailboxes/*/*/.Spam/cur/
|
|
|
|
|
2013-09-01 14:13:51 +00:00
|
|
|
# Kick services.
|
2014-07-16 13:06:45 +00:00
|
|
|
restart_service spampd
|
|
|
|
restart_service dovecot
|
2013-08-23 15:59:28 +00:00
|
|
|
|