Spamassassin trainieren mit Dovecot

Das Dovecot Antispam-Plugin bewirkt, daß manuell in den Spam-Ordner verschobene Nachrichten den Spamfilter von Spamassassin trainieren. Allerdings wirkt sich das immer serverweit aus. Was also für den einen Nutzer gut ist, kann für den anderen schlecht sein – kein Vorteil ohne Nachteil! Spielt aber letztlich keine Rolle, da das Entfernen der Nachricht aus dem Spam-Ordner wiederum den Spamfilter trainiert.

Nachfolgende Installationsanleitung ist für Ubuntu 18.04 mit ISPCongfig 3.1.

Pakete installieren

apt update && apt -y upgrade
apt -y install dovecot-antispam

Plugin in Dovecot einbinden

vi /etc/dovecot/dovecot.conf
protocol imap {
  mail_plugins = quota imap_quota listescape antispam
  namespace {
    type = private
    separator = /
    inbox = yes
  }
  plugin {
    antispam_debug_target = syslog
    antispam_verbose_debug = 1
    antispam_backend = pipe
    antispam_trash = Trash
    antispam_spam = Junk;Spam;SPAM
    antispam_allow_append_to_spam = "no"
    antispam_pipe_program = /etc/dovecot/sa-learn-pipe.sh
    antispam_pipe_program_spam_arg = --spam
    antispam_pipe_program_notspam_arg = --ham
  }
}

Die IMAP-Ordner Junk, Spam und SPAM werden “lernfähig”, also alles was hier hin verschoben wird, trainiert den Spamfilter.

Näheres zu den Parametern, wie z. B. antispam_allow_append_to_spam:

man antispam

Dovecot neu starten

service dovecot restart

sa-learn-pipe.sh erstellen

vi /etc/dovecot/sa-learn-pipe.sh
#!/bin/bash
#
# Wrapper script for dovecot-antispam without using temporary files
# Look mom, no temporary files!
#
# Security is provided by locking the vmail user (dovecot-imap/antispam)
# only run this script via the sudoers line.  The script checks arguments
# to stay safe.  Log everything to syslog and return intelligent codes.
#
# sudoers:
#   vmail   ALL= (amavis) NOPASSWD: /etc/dovecot/sa-learn-pipe.sh
#
# dovecot/conf.d/90-antispam.conf:
#   plugin {
#     antispam_debug_target = syslog
#     #antispam_verbose_debug = 1
#     antispam_backend = pipe
#     antispam_trash = Trash
#     antispam_spam = Junk;Spam;SPAM
#     antispam_allow_append_to_spam = no
#     antispam_pipe_program = /etc/dovecot/sa-learn-pipe.sh
#     antispam_pipe_program_spam_arg = --spam
#     antispam_pipe_program_notspam_arg = --ham
#   }
#
if [ "$1" != "wrap" ]; then
        out=$(sudo -Hu amavis "$0" wrap "$@" < /proc/self/fd/0 2>&1)
        ret=$?
        lvl="info"
        if [ "$ret" -ne 0 ]; then
                lvl="err"
        fi
        logger -p mail.$lvl -i -t sa-learn-pipe "${1//[^a-z]/}: $out"
        exit $ret
fi

# sa-learn gets upset if in another user's home dir
cd "$HOME"

# Sanitize input to be only a-z, space and dash
cmd=${2//[^a-z\- ]/}

# Bash magic: turn stdin into a fd that sa-learn can read
sa-learn "$cmd" <(cat -)
ret=$?
exit $ret
chmod +x /etc/dovecot/sa-learn-pipe.sh

sudo für Benutzer vmail

sudo visudo
vmail   ALL= (amavis) NOPASSWD: /etc/dovecot/sa-learn-pipe.sh

Fertig!

Zum Abschluß noch sicherheitshalber den IMAP-Ordner Junk für alle Konten abonnieren

doveadm mailbox subscribe -A Junk

Überprüfen

sa-learn --dbpath=/var/lib/amavis/.spamassassin --dump magic

Wurde der Spamfilter trainiert, ändern sich die Werte von nspam und/oder nham.

Siehe auch