OpenSMTPD Mail Filtering
The title of this post should be "Spam Filtering on OpenSMTPD", but i didn't want to define the my home page with that word. Running an e-mail server is a challanging and low-reward task, so we need to keep this as simple as we possibly can. This is I classify mail using SpamAssasin these days.
On OpenBSD, the spam processing toolchain will require three packages
p5-Mail-SpamAssassin spampd procmail
SpamAssassin provides the filtering agent, SpamPD connects to the SpamAssassin daemon and forwards the mail to an MTA, and Procmail writes the messages in the mbox format.
SpamAssassin & SpamPD
Creating a basic SpamAssassin proxy that will grade and each message add X-Spam- headers will require the following daemons
# rc.conf.local pkg_scripts="spamassassin spampd" spampd_flags="--port=10025 --relayhost=127.0.0.1:10026 --tagall --log-rules-hit --maxsize=2048"
These SpamPD options only need to match the MTA configuration. Setting --maxsize allows messages with largish attachments to be processed. --tagall and --log-rules-hit are not required, but useful for diagnostics.
Procmail can be configured per user or per site with /etc/procmailrc. The following rules will cause suspect mail to be delivered to an mbox named "spam".
:0: * ^X-Spam-Flag: YES $HOME/mail/spam
Extending the ideas presented in Run Your Own Server, this adds routing rules that forward incoming mail to the mail proxy for evaluation
# smtpd.conf listen on localhost port 25 listen on egress port 25 listen on localhost port 10028 tag DKIM listen on localhost port 10026 tag SPAMD table aliases db:/etc/mail/aliases.db accept tagged SPAMD from any for domain "eradman.com" deliver to mda "procmail -f -" accept from any for domain "eradman.com" relay via smtp://127.0.0.1:10025 accept for local alias <aliases> deliver to mbox accept tagged DKIM for any relay accept from local for any relay via smtp://127.0.0.1:10027
Incoming mail on port 25 is not tagged, which allows it to match the rule relay via smtp://127.0.0.1:10025. After SpamAssassin grades the mail SpamPD relays it back to OpenSMTPD on port 10026 where it matches the tag SPAMD and is handed over to procmail for delivery.