From de9a560f6aaf0c986a190f94c1f63c0307ca9158 Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Sat, 21 Nov 2026 21:00:42 +0100 Subject: [PATCH 1/2] Integrate SpamAssassin with Postfix mail delivery --- .../spamassassin/debian-milter.service.conf | 3 + deploy/spamassassin/postfix-main.cf.example | 2 + .../spamass-milter.default.example | 6 ++ deploy/spamassassin/spamd.default.example | 2 + docs/deployment.md | 82 ++++++++++++++++++- 5 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 deploy/spamassassin/debian-milter.service.conf create mode 100644 deploy/spamassassin/postfix-main.cf.example create mode 100644 deploy/spamassassin/spamass-milter.default.example create mode 100644 deploy/spamassassin/spamd.default.example diff --git a/deploy/spamassassin/debian-milter.service.conf b/deploy/spamassassin/debian-milter.service.conf new file mode 100644 index 0000000..a293e89 --- /dev/null +++ b/deploy/spamassassin/debian-milter.service.conf @@ -0,0 +1,3 @@ +[Service] +PIDFile=/run/spamass/spamass.pid +RemainAfterExit=no diff --git a/deploy/spamassassin/postfix-main.cf.example b/deploy/spamassassin/postfix-main.cf.example new file mode 100644 index 0000000..a4cd818 --- /dev/null +++ b/deploy/spamassassin/postfix-main.cf.example @@ -0,0 +1,2 @@ +# Merge into /etc/postfix/main.cf. Keep any other smtpd_milters on this list. +smtpd_milters = { unix:spamass/spamass.sock, connect_timeout=3s, content_timeout=30s, default_action=accept } diff --git a/deploy/spamassassin/spamass-milter.default.example b/deploy/spamassassin/spamass-milter.default.example new file mode 100644 index 0000000..0e7fd75 --- /dev/null +++ b/deploy/spamassassin/spamass-milter.default.example @@ -0,0 +1,6 @@ +# /etc/default/spamass-milter +# Tag mail without rewriting the subject or body. Skip authenticated SMTP. +OPTIONS="-m -I -- -d 127.0.0.1 -n 2 -t 15 --connect-retries=1" +SOCKET="/var/spool/postfix/spamass/spamass.sock" +SOCKETOWNER="spamass-milter:postfix" +SOCKETMODE="0660" diff --git a/deploy/spamassassin/spamd.default.example b/deploy/spamassassin/spamd.default.example new file mode 100644 index 0000000..c09cb96 --- /dev/null +++ b/deploy/spamassassin/spamd.default.example @@ -0,0 +1,2 @@ +# /etc/default/spamd +OPTIONS="--listen=127.0.0.1 --allowed-ips=127.0.0.1 --username=debian-spamd --nouser-config --max-children=5 --timeout-child=15 --helper-home-dir=/var/lib/spamassassin" diff --git a/docs/deployment.md b/docs/deployment.md index 033ed42..269e658 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -993,6 +993,78 @@ Back up mail on the node that hosts the mailbox. The archive can be used to recover messages and folders into a newly provisioned mailbox with the same address. [Mailbox backup and restore](development.md#back-up-and-restore-a-mailbox) +## Filter incoming mail with SpamAssassin + +Install the distribution's SpamAssassin and `spamass-milter` packages on +the host where Postfix receives mail. Use the same setup for a co-located +mail service and a separate mail node. A node shared by multiple Coulobre +installations needs one filter configuration. + +```sh +sudo apt install spamassassin spamd spamc spamass-milter +sudo adduser spamass-milter postfix +sudoedit /etc/default/spamd /etc/default/spamass-milter /etc/postfix/main.cf +``` + +Use the settings from `deploy/spamassassin/spamd.default.example` and +`deploy/spamassassin/spamass-milter.default.example` in the corresponding +defaults files. The scanner listens on loopback and uses site-wide rules. +Postfix reaches the milter through a group-restricted socket inside its +queue directory. + +Merge the `smtpd_milters` entry in +`deploy/spamassassin/postfix-main.cf.example` into the existing list. If a +receiving service overrides `smtpd_milters` in `master.cf`, add the entry +there too. The example skips authenticated SMTP and covers messages +received by an SMTP service configured with this milter. Deliveries from +the local `sendmail` command use Postfix's separate non-SMTP path. +[Postfix Milter setup](https://www.postfix.org/MILTER_README.html) + +If Debian 13's `spamass-milter.service` is generated from +`/etc/init.d/spamass-milter`, install this drop-in so systemd tracks the +milter process. + +```sh +sudo install -D -m 0644 deploy/spamassassin/debian-milter.service.conf \ + /etc/systemd/system/spamass-milter.service.d/coulobre.conf +sudo systemctl daemon-reload +``` + +```sh +sudo spamassassin --lint +sudo systemctl enable spamd.service spamass-milter.service +sudo systemctl restart spamd.service spamass-milter.service +sudo postfix check +sudo postfix reload +``` + +The example delivers scanned messages with an `X-Spam-Status` header and +preserves the subject and body. Set the tagging threshold with +`required_score` in `/etc/spamassassin/local.cf`. Restart `spamd.service` +after editing its rules. The example lets Postfix accept mail during a +milter outage and lets the milter pass mail through during a scanner +outage. Messages above the `spamc` size limit also pass through unscanned. +The per-milter fallback preserves the failure policy of any other filter. +[SpamAssassin settings](https://spamassassin.apache.org/full/4.0.x/doc/Mail_SpamAssassin_Conf.html) + +On a test mail node, check delivery through SMTP to a Coulobre mailbox and +inspect the full headers over IMAP. + +1. Send a normal message and look for `X-Spam-Status: No`. +2. Send the [GTUBE test message](https://spamassassin.apache.org/gtube/) + and look for `X-Spam-Status: Yes` with `GTUBE` in the matched tests. +3. Stop `spamd.service` and send another test message. Confirm it arrives + without a scanner-added status. Start the scanner and repeat with + `spamass-milter.service` stopped. Start both services after testing. + +Use `spamc -d 127.0.0.1 -K` to check the scanner from the mail node. Read +scan results with `journalctl -u spamd.service -u spamass-milter.service` +and Postfix's filter connection errors with `journalctl -t postfix/smtpd` +or in the mail log. +Add `spamd` and `spamass-milter` to the mail node's +[service health checks](#monitor-local-and-remote-services) to monitor the +two units. + ## Provision mail and DNS on separate servers Choose one destination per service. For example, run mail locally and publish @@ -1066,6 +1138,8 @@ Set up [BIND9](development.md#apply-a-zone-manifest) or the corresponding [DNS socket](development.md#enable-local-dns-publishing) or [mailbox socket](development.md#enable-local-mailbox-provisioning). The destination runs the local socket service shipped with Coulobre. +[Enable SpamAssassin on the mail node](#filter-incoming-mail-with-spamassassin) +for incoming spam filtering. Save the panel's public key from the previous step as `panel-node.pub` on the destination. Create a receiver with only the socket group for that @@ -1304,9 +1378,11 @@ COULOBRE_SERVICE_HEALTH='{"panel-01":{"local":["panel","worker","scheduler","apa Choose an ID for each node using lower-case letters, numbers, and hyphens, starting with a letter. Keep the IDs stable for the same hosts. List one `local` node per panel installation and use `ssh` for each additional host. -Use `bind9`, `postfix`, and `dovecot` under `local` for co-located services. -Remote checks accept those same three service names. The SSH client matches -each remote node ID to a `Host` entry. +Use `bind9`, `postfix`, `dovecot`, `spamd`, and `spamass-milter` under +`local` for co-located services or under `ssh` for separate nodes. Only +list the spam filter units where filtering is configured. Update the +Coulobre package on a remote node before adding its filter units. The +SSH client matches each remote node ID to a `Host` entry. Install the collector with a dedicated service identity after updating the panel package. -- 2.43.0 From 7d380f093d64681e87ea69d05164f46666f40101 Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Sat, 21 Nov 2026 21:00:42 +0100 Subject: [PATCH 2/2] Monitor SpamAssassin services on mail nodes --- src/coulobre/hosting/test_service_health.py | 33 +++++++++++++++++++++ src/coulobre/service_health.py | 7 +++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/coulobre/hosting/test_service_health.py b/src/coulobre/hosting/test_service_health.py index 96ff76b..bdfb6a8 100644 --- a/src/coulobre/hosting/test_service_health.py +++ b/src/coulobre/hosting/test_service_health.py @@ -1,6 +1,7 @@ import json import subprocess from datetime import timedelta +from io import StringIO from unittest.mock import patch from django.contrib.auth import get_user_model @@ -16,6 +17,7 @@ from coulobre.service_health import ( check_remote, check_unit, load_health_targets, + main, ) from .models import Customer, Reseller, ServiceHealth @@ -99,6 +101,37 @@ class ServiceHealthProbeTests(SimpleTestCase): self.assertIn("StrictHostKeyChecking=yes", command) self.assertEqual(command[-2:], ("--", "mail-02")) + def test_spam_filter_units_can_be_checked_locally_and_remotely(self): + filters = ("spamd", "spamass-milter") + self.assertEqual( + load_health_targets( + '{"panel":{"local":["spamd","spamass-milter"]},' + '"mail":{"ssh":["spamd","spamass-milter"]}}' + ), + ( + HealthTarget("panel", "local", filters), + HealthTarget("mail", "ssh", filters), + ), + ) + output = StringIO() + with ( + patch("sys.stdout", output), + patch( + "coulobre.service_health.subprocess.run", + return_value=subprocess.CompletedProcess( + (), 0, b"LoadState=loaded\nActiveState=active\n" + ), + ) as run, + ): + self.assertEqual(main(), 0) + units = {call.args[0][-1] for call in run.call_args_list} + self.assertTrue({"spamd.service", "spamass-milter.service"} <= units) + report = json.loads(output.getvalue()) + self.assertEqual( + {name: report["services"][name] for name in filters}, + dict.fromkeys(filters, "active"), + ) + @override_settings(SERVICE_HEALTH_TARGETS=TARGETS) class ServiceHealthPanelTests(TestCase): diff --git a/src/coulobre/service_health.py b/src/coulobre/service_health.py index 6c9f08c..4033dc7 100644 --- a/src/coulobre/service_health.py +++ b/src/coulobre/service_health.py @@ -15,8 +15,10 @@ SERVICES = { "bind9": ("BIND9", "named.service"), "postfix": ("Postfix", "postfix.service"), "dovecot": ("Dovecot", "dovecot.service"), + "spamd": ("SpamAssassin", "spamd.service"), + "spamass-milter": ("SpamAssassin milter", "spamass-milter.service"), } -REMOTE_SERVICES = ("bind9", "postfix", "dovecot") +REMOTE_SERVICES = ("bind9", "postfix", "dovecot", "spamd", "spamass-milter") REMOTE_VERSION = 1 SSH_CONFIG = "/etc/coulobre-health/ssh_config" NODE_NAME = re.compile(r"[a-z][a-z0-9-]{0,62}") @@ -172,7 +174,8 @@ def check_remote(target): stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, - timeout=15, + # Allow for SSH setup and sequential unit checks. + timeout=22, check=True, ) except (OSError, subprocess.SubprocessError): -- 2.43.0