14 KiB
emailwiz (Debian 13 fork)
Set up and maintain a single-domain Postfix + Dovecot + OpenDKIM + SpamAssassin mail server on Debian 13 (trixie).
This is a fork and rewrite of Luke Smith's emailwiz
(reference revision aefc7a8, 2026-10-17). It is not the upstream
project. It keeps upstream's architecture and mailbox layout, so servers
installed with upstream emailwiz.sh migrate in place. The one-shot script is
replaced by a tool you can rerun safely: guided setup, plan/apply with
backups, validation and rollback. Upstream's script does not work on Debian 13
(see docs/UPSTREAM-REVIEW.md). Licensed GPL-3.0-or-later, like the original
(see LICENSE).
- Version: 2.0.0
- Tested on: Debian 13.7, postfix 3.10.13, dovecot 2.4.1, opendkim 2.11.0~beta2,
spamd 4.0.1, fail2ban 1.1.0 (see
docs/COMPATIBILITY.md) - Differences from upstream:
docs/UPSTREAM-REVIEW.md
Quick start: a new server
Requirements: a Debian 13 server you can SSH into with sudo, a domain, and an
A record mail.DOMAIN → server IP (needed for the certbot certificate).
# from your workstation, in this repository
make remote-setup HOST=admin@203.0.113.10 DOMAIN=example.org
# or on the server itself
sudo apt install make # or skip make: sudo sh install.sh && sudo emailwiz setup
sudo make setup DOMAIN=example.org
setup walks through 8 steps and asks before each optional one:
| # | Step | Asks |
|---|---|---|
| 1 | configuration (/etc/emailwiz/emailwiz.conf) |
domain, mail host name, postmaster user, certificate mode (certbot or existing files) |
| 2 | install missing packages (never removes any) | – |
| 3 | DNS pre-check (read-only, warns only) | – |
| 4 | UFW: allow 25, 465, 587, 993 (+995, +80 for certbot), TCP only; SSH rules and policies untouched; skipped if UFW is inactive | yes/no |
| 5 | certificate: certbot certonly (--nginx/--apache if a web server owns port 80, which only adds the challenge temporarily; --standalone otherwise), dry run first |
– |
| 6 | plan, then apply (backup, validation, automatic restore on failure) |
yes/no |
| 7 | first mail account, password typed into passwd |
yes/no, name |
| 8 | validation incl. a local delivery test; DNS records saved to /root/emailwiz-dns.txt |
– |
Then publish the DNS records, ask your provider for the PTR record, and run
sudo emailwiz validate --external-test you@gmail.com. Setup is safe to
rerun; an existing, different config is only replaced after confirmation.
Unattended (CI, provisioning):
sudo env DOMAIN=example.org ROOT_ALIAS=admin TLS_MODE=certbot \
SETUP_UFW=yes SETUP_USER=alice emailwiz setup --non-interactive
sudo passwd alice
Makefile targets
On the server (sudo make …) |
From a workstation (make … HOST=user@server) |
Development |
|---|---|---|
install, uninstall |
remote-install (sends committed files via git archive) |
lint |
setup [DOMAIN=] |
remote-setup [DOMAIN=] |
test, test-migration, test-clean, test-setup, test-certbot |
plan, apply, validate, dns, backup, audit |
remote-plan, remote-apply, remote-validate, remote-dns |
release (tagged, clean tree → dist/*.tar.gz + .sha256) |
user NAME= |
remote-user NAME= |
help |
Remote targets only need ssh + sudo on the server, not make.
Architecture
| Function | Component | Notes |
|---|---|---|
| Inbound SMTP (MX) | Postfix :25 | STARTTLS optional (RFC 3207), no AUTH |
| Client submission | Postfix :587 (STARTTLS required) and :465 (implicit TLS) | SASL via Dovecot; sender must match login |
| Spam filtering | spamd via spamc pipe on :25 |
spam is filed into Junk by a default Sieve script |
| DKIM | OpenDKIM on 127.0.0.1:12301 | signs @DOMAIN, verifies inbound |
| Mailbox access | Dovecot IMAP :993 (+ POP3 :995 optional) | TLS required; 143/110 only with STARTTLS |
| Accounts | local system users in group mail, passwords via PAM |
Maildir in ~/Mail |
| Brute force | fail2ban jails postfix, postfix-sasl, dovecot (+ sshd from Debian) |
systemd journal backend |
There is no database, webmail, or virtual-user layer. One domain per install.
Files emailwiz owns
| Path | How |
|---|---|
/etc/postfix/main.cf, master.cf |
edited only with postconf -e/-M/-P/-X on a copy, then swapped in atomically; unmanaged settings are preserved |
/etc/postfix/login_maps.pcre, emailwiz_outbound_header_checks |
rendered |
/etc/dovecot/dovecot.conf |
rendered from templates/dovecot.conf.in (whole file) |
/etc/opendkim.conf, $DKIM_DIR/{keytable,signingtable,trustedhosts} |
rendered |
$DKIM_DIR/$DOMAIN/$SELECTOR.private |
generated only if missing; never overwritten |
/etc/default/spamd |
only the OPTIONS= line |
/etc/fail2ban/jail.d/emailwiz.local |
rendered |
/etc/aliases |
only adds postmaster:/root: when missing |
/var/lib/dovecot/sieve/default.sieve |
rendered + compiled |
/etc/systemd/system/emailwiz-{certcheck,backup}.{service,timer} |
rendered |
emailwiz never purges packages, never touches mailboxes, SSH or DNS, and never
deletes a file without first copying it into the run's backup ("retire").
Firewall rules and certificates are only changed by setup, firewall and
tls-obtain, and only with your confirmation (or explicit SETUP_*
variables in non-interactive mode).
Install or upgrade the tool only
sudo sh install.sh # or: sudo make install / make remote-install HOST=…
Installs to /usr/local/lib/emailwiz (previous version kept as emailwiz.prev)
with /usr/local/sbin/emailwiz. Mail configuration is not touched; run
sudo emailwiz plan afterwards.
Usage
sudo emailwiz setup # guided first install (see Quick start)
sudo emailwiz audit # read-only report
sudo emailwiz init example.org # writes /etc/emailwiz/emailwiz.conf; review it
sudo emailwiz install-packages # clean installs only: apt-get install, never purge
sudo emailwiz plan # show exact diffs; changes nothing
sudo emailwiz apply # backup -> install -> validate -> reload (asks first)
sudo emailwiz validate # config, services, TLS, relay policy, DNS
sudo emailwiz validate --deliver-test # + local delivery & DKIM signing
sudo emailwiz validate --external-test you@gmail.com # + real outbound delivery (opt-in)
sudo emailwiz dns # DNS records this host needs
sudo emailwiz tls-obtain # (re)obtain the certbot certificate (TLS_MODE=certbot)
sudo emailwiz firewall # add missing UFW rules for mail ports (asks)
sudo emailwiz validate --renew-test # + certbot renew --dry-run
sudo emailwiz user add alice && sudo passwd alice
plan and apply share one code path: every component renders its desired
files into a temporary staging tree. plan diffs that tree against the live
system and deletes it. apply additionally:
- creates a verified backup in
/var/backups/emailwiz/<timestamp>-apply/(0700); - generates the DKIM key if it is missing;
- installs changed files atomically (temp file +
rename); - runs
postfix check,doveconf -n,opendkim -n,fail2ban-client -t; - reloads/restarts only the services whose inputs changed (a failed reload falls back to a restart; a failed restart fails the apply);
- checks that every service is active.
If step 4, 5 or 6 fails, the files from this run are restored automatically and the command exits 3.
Configuration (/etc/emailwiz/emailwiz.conf)
Sourced by /bin/sh. Must be owned by root and not group/world writable.
| Variable | Default | Meaning |
|---|---|---|
DOMAIN |
required | mail domain (user@DOMAIN) |
MAIL_HOSTNAME |
mail.$DOMAIN |
MX host name, HELO, certificate name |
ROOT_ALIAS |
– | local user receiving root/postmaster mail (strongly recommended) |
TLS_MODE |
external |
external: cert managed elsewhere; certbot: obtained by setup/tls-obtain into /etc/letsencrypt/live/$MAIL_HOSTNAME, renewed by certbot.timer, reload hook installed |
TLS_CERT, TLS_KEY |
– | full chain / private key paths |
CERT_WARN_DAYS |
21 | expiry warning threshold (daily timer mails postmaster) |
DKIM_SELECTOR |
mail |
DKIM selector |
DKIM_DIR |
/etc/postfix/dkim |
emailwiz-compatible key location |
DKIM_BITS |
2048 | size of a newly generated key |
IMAP_PROTOCOLS |
imap pop3 |
set to imap to disable POP3 |
SPAM_FILTER |
spamc |
none disables the SpamAssassin pipe |
SPAMD_MAX_CHILDREN |
2 | spamd children (~80–100 MiB each) |
STRIP_OUTBOUND_RECEIVED |
yes |
remove Received: on outbound SMTP (hides client IPs); inbound headers are kept |
SMTP_ADDRESS_PREFERENCE |
auto |
auto: IPv4 unless the host has global IPv6 |
FAIL2BAN |
yes |
manage mail jails |
EMAILWIZ_TIMERS |
yes |
cert-expiry (daily) and config backup (weekly) timers |
Exit codes
0 ok · 1 error · 2 usage/config error · 3 validation failed (and was
rolled back during apply) · 4 unsupported OS · 10 plan --detailed-exitcode
found pending changes.
Rerunning safely
plan,validate,audit,dns,cert-check,backupsare read-only: run them any time.applyis idempotent: a second run reports "nothing to do". Runplanfirst and read the diff.backupis safe to repeat. Scheduled backups are pruned toBACKUP_KEEP, manual and apply backups are never pruned automatically.- One-time operations:
migrate-upstream --fix-pam(idempotent, but changes authentication: test an IMAP login afterwards); the firstapplyon a host installed by upstream emailwiz (replaces its leftovers); DKIM key generation (happens once per selector; new selector = new DNS record). - After
apt upgrade, runsudo emailwiz plan. Package conffile prompts can restore Debian defaults, whichplanwill show.
Migrating a server installed by upstream emailwiz.sh
sudo emailwiz auditlists upstream leftovers.sudo emailwiz init DOMAIN. It picks up the current cert paths from Postfix.sudo emailwiz planshows, among others, replacing upstream's/etc/fail2ban/jail.d/emailwiz.local(drops[sieve]) and retiring the OpenDKIMoverride.conf.sudo emailwiz apply, thensudo emailwiz validate --deliver-test.- Optional:
sudo emailwiz migrate-upstream --fix-pam(removes upstream'snullokPAM line, which permits empty passwords).
The DKIM key, mailboxes, Sieve scripts, aliases and users stay as they are.
Upgrading from mailwiz 1.x
Releases 1.x of this fork were called mailwiz (/etc/mailwiz,
/usr/local/sbin/mailwiz, mailwiz-* units). Upgrade path:
sudo install -D -m 0644 /etc/mailwiz/mailwiz.conf /etc/emailwiz/emailwiz.conf
sudo sh install.sh # installs /usr/local/sbin/emailwiz
sudo emailwiz plan # retires the mailwiz-* files, installs the emailwiz-* ones
sudo emailwiz apply
sudo emailwiz validate --deliver-test
# afterwards, remove the old tool (backups in /var/backups/mailwiz are kept):
sudo rm -rf /usr/local/lib/mailwiz /usr/local/lib/mailwiz.prev /usr/local/sbin/mailwiz /etc/mailwiz
apply disables the old mailwiz-* timers before retiring their unit
files. Old backups remain readable with tar, but emailwiz rollback only
lists /var/backups/emailwiz.
Backup and rollback
See docs/ROLLBACK.md. In short:
sudo emailwiz backups # list
sudo emailwiz backup --with-mail # include ~/Mail of group-mail users
sudo emailwiz rollback 20261028-150102-apply # restore config from that snapshot
Backups stay on the server. Copy /var/backups/emailwiz off-host regularly;
it contains the DKIM private key, so keep the copy encrypted.
Testing
tests/run.sh # shellcheck + sh -n + all container scenarios (needs docker)
tests/run.sh migration # reproduce an upstream emailwiz install, migrate, roll back
tests/run.sh clean # fresh Debian 13 install
tests/run.sh setup # make install + setup --non-interactive, UFW, first user, rerun, config protection
tests/run.sh certbot # certbot path with a stub certbot (authenticator choice, hook, renew check)
Real Let's Encrypt issuance cannot run in the offline test container: the certbot scenario checks the exact certbot invocations instead.
The containers run systemd as PID 1 with the real Debian packages and a
test-only CA. They cover plan read-onlyness, refusal paths, apply,
idempotence, DKIM signing (verified with dkimpy), SASL submission on 587/465,
sender spoofing, unauthenticated submission, AUTH exposure on 25, IMAPS
login, rollback, and the PAM migration. They cannot test real DNS, PTR,
external delivery, or provider port filtering; run emailwiz validate with
--external-test on the server for those.
Troubleshooting
| Symptom | Check |
|---|---|
apply exits 3 |
the message names the failing check; config was restored. journalctl -u postfix -u dovecot -u opendkim -n 50 |
| mail to Gmail/Outlook rejected | sudo emailwiz validate (PTR, SPF, DKIM DNS match); journalctl -t postfix/smtp -n 50 |
| no DKIM signature | journalctl -t opendkim; sender domain must be @DOMAIN; ss -ltn sport = :12301 |
| client cannot log in | use port 993/465/587 with TLS and the bare user name; journalctl -u dovecot -n 50; fail2ban-client status dovecot |
| "Sender address rejected: not owned by user" | the From address local part must equal the login name (alice → alice@ / alice+tag@) |
| local mail deferred "temporary failure" | root has no alias, so mail to it is written as nobody: set ROOT_ALIAS, apply |
| spam not filtered | systemctl status spamd; header X-Spam-Status on inbound mail |
| port 25 outbound blocked | sudo emailwiz validate: "Outbound SMTP reachability"; ask the provider |