No description
  • Shell 61.3%
  • Go Template 20.4%
  • Dockerfile 11.2%
  • Smarty 7.1%
Find a file
2026-09-29 22:02:29 +00:00
clamav feat: hardened email server stack (postfix+dovecot+clamav+spamassassin+dkim+fail2ban) 2026-09-29 22:02:29 +00:00
clamav-milter feat: hardened email server stack (postfix+dovecot+clamav+spamassassin+dkim+fail2ban) 2026-09-29 22:02:29 +00:00
dovecot feat: hardened email server stack (postfix+dovecot+clamav+spamassassin+dkim+fail2ban) 2026-09-29 22:02:29 +00:00
fail2ban feat: hardened email server stack (postfix+dovecot+clamav+spamassassin+dkim+fail2ban) 2026-09-29 22:02:29 +00:00
opendkim feat: hardened email server stack (postfix+dovecot+clamav+spamassassin+dkim+fail2ban) 2026-09-29 22:02:29 +00:00
postfix feat: hardened email server stack (postfix+dovecot+clamav+spamassassin+dkim+fail2ban) 2026-09-29 22:02:29 +00:00
scripts feat: hardened email server stack (postfix+dovecot+clamav+spamassassin+dkim+fail2ban) 2026-09-29 22:02:29 +00:00
spamassassin feat: hardened email server stack (postfix+dovecot+clamav+spamassassin+dkim+fail2ban) 2026-09-29 22:02:29 +00:00
.dockerignore feat: hardened email server stack (postfix+dovecot+clamav+spamassassin+dkim+fail2ban) 2026-09-29 22:02:29 +00:00
.env.example feat: hardened email server stack (postfix+dovecot+clamav+spamassassin+dkim+fail2ban) 2026-09-29 22:02:29 +00:00
.gitignore feat: hardened email server stack (postfix+dovecot+clamav+spamassassin+dkim+fail2ban) 2026-09-29 22:02:29 +00:00
docker-compose.yml feat: hardened email server stack (postfix+dovecot+clamav+spamassassin+dkim+fail2ban) 2026-09-29 22:02:29 +00:00
README.md feat: hardened email server stack (postfix+dovecot+clamav+spamassassin+dkim+fail2ban) 2026-09-29 22:02:29 +00:00

BCS Email Project — Hardened Docker Mail Server

Standalone, self-hosted email stack for deployment on any Docker host. Deliberately decoupled from other BCS projects.

Stack

Component Role
Postfix SMTP: port 25 (inbound MX, opportunistic TLS), 587 + 465 (submission, TLS required + auth)
Dovecot IMAP 993 + POP3 995 (TLS required), SASL auth for Postfix, LMTP delivery to maildir
ClamAV Antivirus daemon + daily freshclam signature updates
clamav-milter Postfix milter → rejects infected mail at SMTP time
SpamAssassin Spam scoring via spamd; scores ≥ 15 rejected, 5–15 delivered with X-Spam-* headers
OpenDKIM 2048-bit DKIM signing of outbound mail + inbound verification
fail2ban Bans brute-force SMTP/SASL/IMAP attacks reading the shared mail log volume

All milters hook Postfix through its smtpd_milters chain: clamav-milter → spamassassin → opendkim. A dead milter tempfails mail (fail closed) rather than letting it pass unfiltered.

Quick start

cp .env.example .env       # then edit: MAIL_DOMAIN, MAIL_HOSTNAME, POSTMASTER_EMAIL
./scripts/gen-tls.sh       # generates a self-signed placeholder cert into tls/ (replace later)
docker compose up -d --build
docker compose logs -f opendkim    # grab the DKIM TXT record from the log (printed once)

Add / remove a mailbox

./scripts/add-mail-user.sh alice@yourdomain.com              # auto-generates password, prints once
./scripts/add-mail-user.sh bob@yourdomain.com 's3cretPass!'  # or supply one
./scripts/del-mail-user.sh alice@yourdomain.com

Users live in dovecot/users (SSHA512, bind-mounted into the container — survives rebuilds). Postfix relay acceptance comes from the generated virtual map, persisted in a named volume.

Required DNS records (replace example.com / mail / selector=mail)

Type Name Value
A mail your server's IP
MX @ mail.example.com. (priority 10)
TXT (SPF) @ v=spf1 mx -all
TXT (DKIM) mail._domainkey v=DKIM1; k=rsa; p=<key printed by opendkim on first start>
TXT (DMARC) _dmarc v=DMARC1; p=quarantine; rua=mailto:postmaster@example.com
rDNS (PTR) your IP must match MAIL_HOSTNAME or major providers reject/bunk you

Deliverability warning: without a matching rDNS, Gmail/Outlook will likely junk or reject you. If your VPS provider can't set rDNS, use an SMTP relay (SES/Postmark/Mailgun) for outbound instead of direct delivery.

Ports

Port Service Exposure
25 Postfix MX public required
465 SMTP implicit-TLS submission public if clients need it
587 SMTP submission public if clients need it
993 IMAP TLS public if clients need it
995 POP3 TLS optional

Firewall everything else; do not put admin/management surfaces on the public internet.

Hardening summary

  • No open relay: smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, defer_unauth_destination
  • Submission (587/465): TLS mandatory, auth mandatory, relay only
  • 25: strict HELO/client checks, VRFY disabled, DNSBL checks, unknown recipients rejected via virtual maps
  • Dovecot: ssl = required, disable_plaintext_auth = yes, TLS 1.2+
  • ClamAV-infected mail: rejected at SMTP (milter action)
  • Spam ≥ 15: rejected; 5–15: delivered with X-Spam-* headers for client-side filtering
  • DKIM: 2048-bit, relaxed/simple
  • fail2ban: 3 failures = 1 h ban (postfix, postfix-sasl, dovecot jails)
  • milter_default_action = tempfail — a dead AV/milter bounces mail rather than passing unfiltered

DNSBL caveat: zen.spamhaus.org returns blocked/bogus results for resolvers using public DNS (your server currently uses 1.1.1.1/8.8.8.8 per resolv.conf). If DNSBL hits look wrong, switch hosts' resolver to something sanctioned by Spamhaus (or drop the zen line in postfix/main.cf.tpl and rebuild).

fail2ban

Jails read the shared mail log volume: postfix and postfix-sasl (postfix's postlogd file) and dovecot via a dedicated dovecot-mail filter that understands Dovecot's own file-logger format (those lines carry no syslog dovecot tag, so the stock filter would never match).

  • 3 failures in 10 min → 1h DROP; bans persist across restarts (f2b-data volume).
  • The service runs with network_mode: host so its iptables actions land in the host's DOCKER-USER chain — the forward path Docker uses for every published port (25/465/587/993/995). Inside the bridge network it could only police its own (empty) namespace. Linux only; gated by the linux compose profile (see COMPOSE_PROFILES in .env.example — remove linux on non-Linux test hosts).
  • Unban manually:
docker compose exec fail2ban fail2ban-client unban <ip>

Logs

  • Postfix: /var/log/mail/postfix.log (persistent mail-logs volume; written by postlogd via maillog_file)
  • Dovecot: /var/log/mail/dovecot.log + /var/log/mail/dovecot-info.log (same volume, owned by the dovecot user)
  • ClamAV: docker compose logs clamav clamav-milter
  • Spam scores: docker compose logs spamassassin
  • fail2ban bans: docker compose logs fail2ban
  • Rotating the volume log files: move them aside, then docker compose restart postfix (and for dovecot: docker compose exec dovecot doveadm log reopen)

Test it (scripts/smoke-test.sh documents the commands)

From a machine on RELAY_NETWORKS or with valid credentials (swaks):

# Inbound to a real user (expect 250)
swaks --to alice@yourdomain.com --server mail.yourdomain.com:25
# Relay attempt without auth (expect 554 Relay access denied)
swaks --to someone@gnu.org --server mail.yourdomain.com:25
# Submission with auth (expect 250)
swaks --to you@gmail.com --server mail.yourdomain.com:587 --auth PLAIN --auth-user alice@yourdomain.com --auth-password 'pass' --tls

Future upgrades

  • Rspamd replaces SpamAssassin + the AV milter plumbing (faster, Bayesian learning, web UI)
  • policyd-spf for SPF enforcement at SMTP time
  • Real certs via Let's Encrypt (certbot dns-* or manual fullchain.pem/privkey.pem swap into tls/)