Howto: getmail, dovecot, postfix, spamassassin und procmail

Dieses kleine Howto soll kurz erklären, wie die genannten Programme reibungslos Hand in Hand arbeiten und so einen IMAP-Dienst mit Spamfilterung und Mailverteilung realisieren können. Das Tutorial basiert auf dem Artikel “Mailhamster mit FreeBSD” und erweitert ihn um die Konfiguration von Spamassassin und Procmail. User, die noch mit sendmail arbeiten, sollten auf postfix umstellen. Ein simples Howto dazu gibt es hier.

Das Mailhamster Tutorial sollte zunächst wie beschrieben nachvollzogen werden, lediglich mit der Konfiguration von procmail kann gewartet werden, da wir procmail an spamassassin anpassen müssen.

Nachdem also getmail und dovecot (wie im Tutorial beschrieben) zusammenarbeiten, machen wir uns an die Installation von Spamassassin:

cd /usr/ports/mail/p5-Mail-SpamAssassin && make install clean

Unter /usr/local/etc/mail/spamassassin gibt es nun eine local.cf.sample, die man in local.cf umbenennt. Diese Datei steuert das Verhalten von spamassassin. Sie kann nun angepasst werden (wenn man weiß, was man tut) oder mit Hilfe eines selbsterklärenden Webinterfaces erstellt werden: http://www.yrex.com/spam/spamconfig.php. Es ist noch anzumerken, dass in dem genannten Verzeichnis die globale Config-File von Spamassassin liegt. Wird spamassassin mit Hilfe des -u Flags im Kontext eines bestimmten Users ausgeführt, so ist die Konfigurationsdatei für diesen User so anzulegen: ~/.spamassassin/user_prefs (mit gleichem Inhalt).

Der Inhalt meiner Datei sieht so aus:
# SpamAssassin config file for version 3.x
# NOTE: NOT COMPATIBLE WITH VERSIONS 2.5 or 2.6
# See http://www.yrex.com/spam/spamconfig25.php for earlier versions
# Generated by http://www.yrex.com/spam/spamconfig.php (version 1.50)

# How many hits before a message is considered spam.
required_score 10.0

# Change the subject of suspected spam
rewrite_header subject *****SPAM*****

# Encapsulate spam in an attachment (0=no, 1=yes, 2=safe)
report_safe 2

# Enable the Bayes system
use_bayes 1

# Enable Bayes auto-learning
bayes_auto_learn 1
bayes_file_mode 777

# Enable or disable network checks
skip_rbl_checks 0
use_razor2 1
use_dcc 1
use_pyzor 1

# Mail using languages used in these country codes will not be marked
# as being possibly spam in a foreign language.
# - dutch english french german spanish
ok_languages nl en fr de es

# Mail using locales used in these country codes will not be marked
# as being possibly spam in a foreign language.
ok_locales en

Der Score Treshold kann beliebig gewählt werden, ich habe ihn zunächst hoch gesetzt, um das System zu testen und werde ihn nach weiteren Analysen der eingehenden eMails noch weiter anpassen. Mittlerweile arbeite ich sehr gut mit einem Score von 8.6. Diesen Wert habe ich gewählt, da ich einen Newsletter beziehe, der leider mit 8.5 Spam-Punkten bewertet wird.

Jetzt machen wir uns an die Bearbeitung einer geeigneten .procmailrc Datei, die procmail steuert und so die Mails je nach Spameinstufung in Mailverzeichnisse sortieren kann. Dazu erstellen wir im HOME-Verzeichnis des Users, unter dem getmail läuft (hier: bjoumail), eine Datei namens .procmailrc mit folgendem Inhalt:
# SpamAssassin sample procmailrc
# ==============================

# The following line is only used if you use a system-wide /etc/procmailrc.
# See procmailrc(5) for infos on what it exactly does, the short version:
# * It ensures that the correct user is passed to spamd if spamc is used
# * The folders the mail is filed to later on is owned by the user, not
# root.
DROPPRIVS=yes

# Your procmail logfile will grow rapidly so remember to comment out
# the first line "LOGFILE=$HOME/.procmailrc.log" once you've verified
# that procmail is performing as expected

LOGFILE=$HOME/.procmailrc.log
VERBOSE=yes
COMSAT=no
MAILDIR=/home/bjoumail/mail
DEFAULT=$MAILDIR
PATH=/usr/local/bin:/usr/bin

# Pipe the mail through spamassassin (replace 'spamassassin' with 'spamc'
# if you use the spamc/spamd combination)
#
# The condition line ensures that only messages smaller than 250 kB
# (250 * 1024 = 256000 bytes) are processed by SpamAssassin. Most spam
# isn't bigger than a few k and working with big messages can bring
# SpamAssassin to its knees.
#
# The lock file ensures that only 1 spamassassin invocation happens
# at 1 time, to keep the load down.
#
:0fw: spamassassin.lock
* < 256000
| /usr/local/bin/spamc -s 256000 -u bjoumail

# All mail tagged as spam (eg. with a score higher than the set threshold)
# is moved to a Spam Folder
:0:
* ^X-Spam-Status: Yes
/home/bjoumail/mail/.Spam/

# Work around procmail bug: any output on stderr will cause the "F" in
"From"
# to be dropped. This will re-add it.
# NOTE: This is probably NOT needed in recent versions of procmail
:0
* ^^rom[ ]
{
LOG="*** Dropped F off From_ header! Fixing up. "

:0 fhw
| sed -e '1s/^/F/'
}

#---------------------------------------------------------------------#
# (part 2)
# Add your own filtering commands here, if you want.

#---------------------------------------------------------------------#
# (part 3) How to set up vacation message
# 1. No need to set up .forward file if you use this method.
# 2. Prepare .vacation.msg in your home directory.
# 3. Run "vacation -I" command.
# 4. Uncomment the 2 lines below (Did you replace your_loginname with yours?)
# 5. Remember to comment out the 2 lines when you come back from vacation.

#:0 c
#| /usr/bin/vacation your_loginname

Anmerkungen: Den Workaround habe ich nicht getestet, sondern einfach so aus einer Vorlage übernommen. Den Folder "Spam" müssen wir in unserem eMailprogramm, welches auf den dovecot IMAP-Server zugreift, natürlich noch erstellen. Da dovecot nicht mit mboxes, sondern mit Maildirs arbeitet, ist der trailing slash bei /home/bjoumail/mail/.Spam/ besonders wichtig.

Nun muss noch die /usr/local/etc/postfix/master.cf angepasst werden. Dazu ändert mann die Zeile

smtp inet n - n - - smtpd

in

smtp inet n - n - - smtpd -o content_filter=spamassassin

Man fügt also einen content_filter hinzu. Am Ende der Datei muss man postfix nun mittweilen, was dieser Filter tun soll:

spamassassin
         unix - n n - - pipe
     user=nobody argv=/usr/local/bin/spamc -e /usr/local/sbin/postfix -oi -f ${sender} ${recipient}

Dabei ist sichrzustellen, dass die drei obigen zeilen entweder in einer Zeile in der Datei stehen, oder die zwei unteren Zeilen mit Leerzeichen eingeleitet werden. Der Uer 'nobody' ist ggf. anzupassen.
Jetzt kann man das System testen. Eingehende Mails sollten im Header nun stehen haben, dass sie erfolgreich durch spamassassin gepiped und analysiert wurden und je nachdem als Spam oder Ham eingestuft wurden:

Ham:
X-Spam-Checker-Version: SpamAssassin 3.1.1 (2006-03-10) on
my-server.domain
X-Spam-Level:
X-Spam-Status: No, score=-2.6 required=10.0 tests=BAYES_00 autolearn=ham
version=3.1.1

Spam:

X-Spam-Flag: YES
X-Spam-Checker-Version: SpamAssassin 3.1.1 (2006-03-10) on
my-server.domain
X-Spam-Level: ********************************
X-Spam-Status: Yes, score=32.9 required=10.0 tests=BAYES_99,
DATE_IN_FUTURE_03_06,FROM_ENDS_IN_NUMS,FUZZY_PHARMACY,
HTML_IMAGE_ONLY_24,HTML_MESSAGE,INFO_TLD,RCVD_NUMERIC_HELO,
UPPERCASE_25_50,URIBL_AB_SURBL,URIBL_JP_SURBL,URIBL_OB_SURBL,
URIBL_SBL,URIBL_SC_SURBL autolearn=spam version=3.1.1

Hat man noch einen Ordner voll mit Spam, so kann (und sollte) man Spamassassin trainieren, um Mails besser klassifizieren zu können. Das geht mit sa-learn, bspw
sa-learn --progress --spam /home/bjoumail/mail/.Spam/cur/
Der Unterordner cur ist der Ordner, wo dovecot die (gelesenen) Mails speichert. Mit dem Trigger --ham kann man spamassassin natürlich auch auf gewollte Mails trainieren. Mehr dazu über man sa-learn.

7 Responses to “Howto: getmail, dovecot, postfix, spamassassin und procmail”

  1. Types Of Computer Viruses Says:

    Types Of Computer Viruses…

    I couldn’t understand some parts of this article, but it sounds interesting…

  2. Renate Says:

    Sehr hilfreich! Vielen Dank dafür!

  3. Peastemealo Says:

    Good day All people, Just joined up with http://www.bjou.de. I hope here is the right place to reveal our olpinions as well as views. Btw is this is the right place to posting introduction topic?

    _______________________________
    London Escorts

  4. thee Says:

    I’m impressed, I need to say. Actually not often do I encounter a weblog that’s each educative and entertaining, and let me tell you, you may have hit the nail on the head. Your thought is excellent; the difficulty is something that not sufficient individuals are talking intelligently about. I’m very blissful that I stumbled throughout this in my seek for one thing regarding this.

  5. Shaun Kinne Says:

    of course like your web-site but you need to take a look at the spelling on quite a few of your posts. A number of them are rife with spelling issues and I to find it very bothersome to tell the reality nevertheless I’ll surely come back again.

  6. Katharine Says:

    I precisely wished to say thanks once more. I do not know the things that I might have achieved without these tactics revealed by you regarding such subject. Completely was a real horrifying issue for me, however , seeing the very well-written mode you dealt with that forced me to cry with joy. Now i’m happy for this support as well as pray you recognize what a powerful job you are doing instructing many people all through your web page. More than likely you have never got to know all of us.

  7. Douglas Says:

    I as well as my friends were actually analyzing the best helpful tips found on your website while before long got an awful feeling I never expressed respect to the site owner for them. These women appeared to be consequently glad to learn all of them and have unquestionably been taking advantage of them. Thank you for turning out to be simply helpful and then for settling on varieties of essential things most people are really desperate to know about. Our honest regret for not saying thanks to you earlier.

Leave a Reply