290 lines
13 KiB
Bash
290 lines
13 KiB
Bash
# emailwiz - validation (local config, TLS, SMTP policy, DNS, delivery tests)
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
# shellcheck shell=sh
|
|
|
|
V_FAIL=0; V_WARN=0
|
|
pass() { printf ' %sPASS%s %s\n' "$C_GRN" "$C_OFF" "$*"; }
|
|
fail() { printf ' %sFAIL%s %s\n' "$C_RED" "$C_OFF" "$*"; V_FAIL=$((V_FAIL + 1)); }
|
|
vwarn() { printf ' %sWARN%s %s\n' "$C_YEL" "$C_OFF" "$*"; V_WARN=$((V_WARN + 1)); }
|
|
skip() { printf ' SKIP %s\n' "$*"; }
|
|
section() { printf '\n%s\n' "$*"; }
|
|
|
|
dns_q() { host -W 5 -t "$1" "$2" 2>/dev/null || :; }
|
|
dns_txt() { dns_q TXT "$1" | sed -n 's/.*descriptive text //p' | sed 's/" "//g; s/"//g'; }
|
|
|
|
# smtp_dialog HOST PORT MODE - MODE plain|starttls|tls; feeds an EHLO.
|
|
smtp_ehlo() {
|
|
case $3 in
|
|
plain) (sleep 2; printf 'EHLO validate.invalid\r\n'; sleep 2; printf 'QUIT\r\n') |
|
|
timeout 15 nc -w 8 "$1" "$2" 2>/dev/null | tr -d '\r' ;;
|
|
starttls) (sleep 2; printf 'EHLO validate.invalid\r\n'; sleep 2; printf 'QUIT\r\n') |
|
|
timeout 15 openssl s_client -quiet -connect "$1:$2" -starttls smtp 2>/dev/null | tr -d '\r' ;;
|
|
tls) (sleep 2; printf 'EHLO validate.invalid\r\n'; sleep 2; printf 'QUIT\r\n') |
|
|
timeout 15 openssl s_client -quiet -connect "$1:$2" 2>/dev/null | tr -d '\r' ;;
|
|
esac
|
|
}
|
|
|
|
tls_verify() { # HOST PORT [starttls-proto]
|
|
_st=; [ -n "${3:-}" ] && _st="-starttls $3"
|
|
# shellcheck disable=SC2086
|
|
echo Q | timeout 15 openssl s_client -connect "$1:$2" $_st -servername "$MAIL_HOSTNAME" \
|
|
-verify_hostname "$MAIL_HOSTNAME" -verify_return_error -brief 2>&1 |
|
|
grep -q '^Verification: OK'
|
|
}
|
|
|
|
cert_days_left() {
|
|
_end=$(openssl x509 -in "$TLS_CERT" -noout -enddate 2>/dev/null | cut -d= -f2)
|
|
[ -n "$_end" ] || { echo -1; return; }
|
|
echo $(( ($(date -d "$_end" +%s) - $(date +%s)) / 86400 ))
|
|
}
|
|
|
|
validate_local() {
|
|
section "Services"
|
|
for _s in postfix dovecot opendkim $( [ "$SPAM_FILTER" = spamc ] && echo spamd ) \
|
|
$( [ "$FAIL2BAN" = yes ] && echo fail2ban ); do
|
|
systemctl is-active -q "$_s" && pass "$_s active" || fail "$_s not active"
|
|
done
|
|
if [ "$EMAILWIZ_TIMERS" = yes ]; then
|
|
for _t in emailwiz-certcheck.timer emailwiz-backup.timer; do
|
|
systemctl is-active -q "$_t" && pass "$_t active" || vwarn "$_t not active"
|
|
done
|
|
fi
|
|
|
|
section "Configuration syntax"
|
|
_o=$(postfix_validate_live) && pass "postfix check" || fail "postfix check: $_o"
|
|
_o=$(postconf -n 2>&1 >/dev/null | grep -i warning) && fail "postconf: $_o" || pass "postconf: no warnings"
|
|
_o=$(doveconf -n 2>&1 >/dev/null) && pass "doveconf -n" || fail "doveconf: $_o"
|
|
_o=$(opendkim -n 2>&1) && pass "opendkim -n" || fail "opendkim: $_o"
|
|
if [ "$FAIL2BAN" = yes ]; then
|
|
_o=$(fail2ban-client -t 2>&1) && pass "fail2ban-client -t" || fail "fail2ban: $_o"
|
|
for _j in postfix postfix-sasl dovecot; do
|
|
fail2ban-client status "$_j" >/dev/null 2>&1 && pass "fail2ban jail $_j" || fail "fail2ban jail $_j missing"
|
|
done
|
|
fi
|
|
|
|
section "Listening ports"
|
|
_ports="25 465 587 993"
|
|
case " $IMAP_PROTOCOLS " in *" pop3 "*) _ports="$_ports 995" ;; esac
|
|
for _p in $_ports; do
|
|
ss -Hltn "sport = :$_p" | grep -q . && pass "tcp/$_p listening" || fail "tcp/$_p not listening"
|
|
done
|
|
ss -Hltn "sport = :$DKIM_PORT" | grep -q "127.0.0.1:$DKIM_PORT" &&
|
|
pass "opendkim on 127.0.0.1:$DKIM_PORT" || fail "opendkim not on 127.0.0.1:$DKIM_PORT"
|
|
ss -Hltn "sport = :$DKIM_PORT" | grep -vq "127.0.0.1:$DKIM_PORT" &&
|
|
fail "opendkim listening beyond loopback" || :
|
|
|
|
section "TLS"
|
|
_kp=$(openssl pkey -in "$TLS_KEY" -pubout 2>/dev/null | sha256sum)
|
|
_cp=$(openssl x509 -in "$TLS_CERT" -noout -pubkey 2>/dev/null | sha256sum)
|
|
[ "$_kp" = "$_cp" ] && pass "certificate matches key" || fail "certificate/key mismatch"
|
|
openssl x509 -in "$TLS_CERT" -noout -checkhost "$MAIL_HOSTNAME" 2>/dev/null | grep -q 'does match' &&
|
|
pass "certificate valid for $MAIL_HOSTNAME" || fail "certificate does not cover $MAIL_HOSTNAME"
|
|
_d=$(cert_days_left)
|
|
if [ "$_d" -lt 0 ]; then fail "certificate expired or unreadable"
|
|
elif [ "$_d" -lt "$CERT_WARN_DAYS" ]; then vwarn "certificate expires in $_d days"
|
|
else pass "certificate valid for $_d more days"; fi
|
|
if [ "$TLS_MODE" = certbot ]; then
|
|
systemctl is-active -q certbot.timer && pass "certbot.timer active (automatic renewal)" ||
|
|
fail "certbot.timer not active: certificate will not renew"
|
|
[ -x /etc/letsencrypt/renewal-hooks/deploy/emailwiz-reload ] && pass "renewal reload hook installed" ||
|
|
fail "renewal hook /etc/letsencrypt/renewal-hooks/deploy/emailwiz-reload missing"
|
|
fi
|
|
_ko=$(stat -c '%a' "$TLS_KEY")
|
|
case $_ko in ?[0-7]0 | ?00) pass "private key not world-readable ($_ko)" ;; *) fail "private key mode $_ko" ;; esac
|
|
tls_verify 127.0.0.1 465 && pass "smtps/465 TLS verifies" || fail "smtps/465 TLS"
|
|
tls_verify 127.0.0.1 587 smtp && pass "submission/587 STARTTLS verifies" || fail "submission/587 STARTTLS"
|
|
tls_verify 127.0.0.1 25 smtp && pass "smtp/25 STARTTLS verifies" || fail "smtp/25 STARTTLS"
|
|
tls_verify 127.0.0.1 993 && pass "imaps/993 TLS verifies" || fail "imaps/993 TLS"
|
|
if echo Q | timeout 10 openssl s_client -connect 127.0.0.1:993 -tls1_1 2>&1 | grep -q '^Protocol *: TLSv1.1'; then
|
|
fail "imaps accepts TLS 1.1"
|
|
else
|
|
pass "imaps refuses TLS 1.1"
|
|
fi
|
|
|
|
section "SMTP policy"
|
|
if have nc; then
|
|
smtp_ehlo 127.0.0.1 25 plain | grep -q '^250.AUTH' && fail "AUTH offered on port 25 (plaintext)" ||
|
|
pass "no AUTH on port 25 before TLS"
|
|
smtp_ehlo 127.0.0.1 587 plain | grep -q '^250.AUTH' && fail "AUTH offered on 587 before STARTTLS" ||
|
|
pass "no AUTH on 587 before STARTTLS"
|
|
else
|
|
skip "nc not installed: plaintext EHLO checks"
|
|
fi
|
|
smtp_ehlo 127.0.0.1 25 starttls | grep -q '^250.AUTH' && fail "AUTH offered on port 25 after STARTTLS" ||
|
|
pass "no AUTH on port 25 after STARTTLS"
|
|
smtp_ehlo 127.0.0.1 587 starttls | grep -q '^250.AUTH' && pass "AUTH offered on 587 after STARTTLS" ||
|
|
fail "no AUTH on 587 after STARTTLS"
|
|
smtp_ehlo 127.0.0.1 465 tls | grep -q '^250.AUTH' && pass "AUTH offered on 465" || fail "no AUTH on 465"
|
|
validate_relay
|
|
|
|
section "DKIM key"
|
|
_k=$(dkim_key_path)
|
|
if [ -f "$_k" ]; then
|
|
[ "$(stat -c '%a %U %G' "$_k")" = "640 root opendkim" ] && pass "key $_k is 0640 root:opendkim" ||
|
|
fail "key permissions: $(stat -c '%a %U %G' "$_k")"
|
|
_o=$(su -s /bin/sh opendkim -c "test -r '$_k'" 2>&1) && pass "opendkim user can read key" ||
|
|
fail "opendkim cannot read key"
|
|
else
|
|
fail "DKIM key missing: $_k"
|
|
fi
|
|
}
|
|
|
|
# Probe relaying from the public address so the client is not in mynetworks.
|
|
validate_relay() {
|
|
have nc || { skip "nc not installed: open-relay probe"; return; }
|
|
_ip=$(primary_ipv4)
|
|
[ -n "$_ip" ] || { skip "no IPv4 address for relay probe"; return; }
|
|
_r=$( (sleep 2; printf 'EHLO %s\r\n' "$MAIL_HOSTNAME"; sleep 1
|
|
printf 'MAIL FROM:<relay-probe@gmail.com>\r\n'; sleep 1
|
|
printf 'RCPT TO:<relay-probe@outlook.com>\r\n'; sleep 2; printf 'QUIT\r\n') |
|
|
timeout 20 nc -w 8 "$_ip" 25 2>/dev/null | tr -d '\r')
|
|
if printf '%s\n' "$_r" | grep -Eq '^(554|454) .*Relay access denied'; then
|
|
pass "unauthenticated relay refused on $_ip:25"
|
|
elif printf '%s\n' "$_r" | grep -Eq '^250 2\.1\.5'; then
|
|
fail "OPEN RELAY: $_ip:25 accepted an external recipient without auth"
|
|
else
|
|
vwarn "relay probe inconclusive: $(printf '%s\n' "$_r" | tail -2 | tr '\n' ' ')"
|
|
fi
|
|
}
|
|
|
|
validate_dns() {
|
|
section "DNS (as seen by this host's resolver)"
|
|
have host || { skip "host(1) missing (bind9-host)"; return; }
|
|
_ip4=$(primary_ipv4); _ip6=$(primary_ipv6)
|
|
_mx=$(dns_q MX "$DOMAIN" | awk '/mail is handled by/ {print $NF}' | sed 's/\.$//')
|
|
printf '%s\n' "$_mx" | grep -qx "$MAIL_HOSTNAME" && pass "MX $DOMAIN -> $MAIL_HOSTNAME" ||
|
|
fail "MX for $DOMAIN does not point to $MAIL_HOSTNAME (got: ${_mx:-none})"
|
|
_a=$(dns_q A "$MAIL_HOSTNAME" | awk '/has address/ {print $NF}')
|
|
printf '%s\n' "$_a" | grep -qx "$_ip4" && pass "A $MAIL_HOSTNAME -> $_ip4" ||
|
|
fail "A $MAIL_HOSTNAME is '${_a:-none}', host IPv4 is $_ip4"
|
|
_aaaa=$(dns_q AAAA "$MAIL_HOSTNAME" | awk '/has IPv6 address/ {print $NF}')
|
|
if [ -n "$_aaaa" ] && [ -z "$_ip6" ]; then
|
|
fail "AAAA $MAIL_HOSTNAME published ($_aaaa) but host has no global IPv6"
|
|
elif [ -n "$_ip6" ] && [ -z "$_aaaa" ]; then
|
|
vwarn "host has IPv6 $_ip6 but no AAAA for $MAIL_HOSTNAME (IPv4-only mail is fine)"
|
|
elif [ -n "$_aaaa" ]; then
|
|
[ "$_aaaa" = "$_ip6" ] && pass "AAAA $MAIL_HOSTNAME -> $_ip6" || fail "AAAA $_aaaa != host $_ip6"
|
|
else
|
|
pass "no AAAA and no global IPv6 (consistent)"
|
|
fi
|
|
_ptr=$(dns_q PTR "$_ip4" | awk '/domain name pointer/ {print $NF}' | sed 's/\.$//')
|
|
[ "$_ptr" = "$MAIL_HOSTNAME" ] && pass "PTR $_ip4 -> $MAIL_HOSTNAME (forward-confirmed above)" ||
|
|
fail "PTR $_ip4 is '${_ptr:-none}', expected $MAIL_HOSTNAME (ask your hosting provider)"
|
|
if [ -n "$_ip6" ]; then
|
|
_ptr6=$(dns_q PTR "$_ip6" | awk '/domain name pointer/ {print $NF}' | sed 's/\.$//')
|
|
[ "$_ptr6" = "$MAIL_HOSTNAME" ] && pass "PTR $_ip6 -> $MAIL_HOSTNAME" ||
|
|
vwarn "IPv6 PTR is '${_ptr6:-none}' (outbound prefers IPv4 if SMTP_ADDRESS_PREFERENCE=auto)"
|
|
fi
|
|
_spf=$(dns_txt "$DOMAIN" | grep '^v=spf1' || :)
|
|
if [ -z "$_spf" ]; then fail "no SPF record on $DOMAIN"
|
|
elif [ "$(printf '%s\n' "$_spf" | wc -l)" -gt 1 ]; then fail "multiple SPF records on $DOMAIN"
|
|
else
|
|
case " $_spf " in
|
|
*" ip4:$_ip4 "* | *" mx "* | *" a:$MAIL_HOSTNAME "*) pass "SPF authorises this host: $_spf" ;;
|
|
*) fail "SPF does not cover $_ip4: $_spf" ;;
|
|
esac
|
|
case " $_spf " in *" ip6:"*) [ -z "$_ip6" ] && vwarn "SPF lists ip6: but host has no global IPv6" ;; esac
|
|
fi
|
|
_dk=$(dns_txt "$DKIM_SELECTOR._domainkey.$DOMAIN" | sed -n 's/.*p=\([A-Za-z0-9+/=]*\).*/\1/p')
|
|
if [ -z "$_dk" ]; then fail "no DKIM record at $DKIM_SELECTOR._domainkey.$DOMAIN"
|
|
elif [ -f "$(dkim_key_path)" ] && [ "$_dk" = "$(dkim_public_b64)" ]; then pass "DKIM DNS key matches local private key"
|
|
else fail "DKIM DNS key does NOT match $(dkim_key_path)"; fi
|
|
_dm=$(dns_txt "_dmarc.$DOMAIN" | grep '^v=DMARC1' || :)
|
|
[ -n "$_dm" ] && pass "DMARC: $_dm" || fail "no DMARC record at _dmarc.$DOMAIN"
|
|
}
|
|
|
|
validate_outbound_port() {
|
|
section "Outbound SMTP reachability"
|
|
have nc || { skip "nc not installed"; return; }
|
|
if timeout 10 nc -4 -z -w 6 gmail-smtp-in.l.google.com 25 2>/dev/null; then
|
|
pass "TCP 25 outbound to gmail-smtp-in.l.google.com (IPv4)"
|
|
else
|
|
fail "cannot reach port 25 outbound over IPv4 (provider block?)"
|
|
fi
|
|
}
|
|
|
|
# Wait for a message (by Message-ID) to reach a final status; print status line.
|
|
_await_delivery() {
|
|
_mid=$1; _since=$2; _i=0
|
|
while [ $_i -lt 60 ]; do
|
|
_qid=$(journalctl --since "$_since" -o cat -t postfix/cleanup 2>/dev/null |
|
|
awk -v m="message-id=<$_mid>" 'index($0, m) {sub(":", "", $1); print $1}' | tail -1)
|
|
if [ -n "$_qid" ]; then
|
|
_st=$(journalctl --since "$_since" -o cat 2>/dev/null | grep "^$_qid: " | grep -E 'status=(sent|bounced|deferred)' | tail -1)
|
|
[ -n "$_st" ] && { printf '%s\n%s\n' "$_qid" "$_st"; return 0; }
|
|
fi
|
|
_i=$((_i + 1)); sleep 2
|
|
done
|
|
return 1
|
|
}
|
|
|
|
_send_test() { # TO
|
|
_mid="emailwiz-test.$(date +%s).$$@$MAIL_HOSTNAME"
|
|
{
|
|
printf 'From: emailwiz test <postmaster@%s>\n' "$DOMAIN"
|
|
printf 'To: <%s>\nSubject: emailwiz delivery test %s\n' "$1" "$(date -u +%FT%TZ)"
|
|
printf 'Message-ID: <%s>\nDate: %s\nMIME-Version: 1.0\n' "$_mid" "$(date -R)"
|
|
printf 'Content-Type: text/plain; charset=us-ascii\n\n'
|
|
printf 'Automated test from emailwiz on %s. No action needed.\n' "$MAIL_HOSTNAME"
|
|
} | /usr/sbin/sendmail -oi -f "postmaster@$DOMAIN" -- "$1"
|
|
printf '%s\n' "$_mid"
|
|
}
|
|
|
|
validate_delivery() { # TO LABEL
|
|
section "Delivery test: $2"
|
|
_since=$(date '+%Y-%m-%d %H:%M:%S')
|
|
_mid=$(_send_test "$1")
|
|
if ! _r=$(_await_delivery "$_mid" "$_since"); then
|
|
fail "no final delivery status after 120s (Message-ID $_mid)"; return
|
|
fi
|
|
_qid=$(printf '%s\n' "$_r" | head -1); _st=$(printf '%s\n' "$_r" | tail -1)
|
|
_st=$(printf '%s\n' "$_st" | sed -E 's/to=<[^>]*>/to=<...>/')
|
|
case $_st in
|
|
*status=sent*) pass "delivered: $(printf '%s' "$_st" | sed 's/^[^ ]* //' | cut -c1-160)" ;;
|
|
*) fail "$(printf '%s' "$_st" | cut -c1-200)" ;;
|
|
esac
|
|
if journalctl --since "$_since" -o cat -t opendkim 2>/dev/null |
|
|
grep -q "^$_qid: DKIM-Signature field added (s=$DKIM_SELECTOR, d=$DOMAIN)"; then
|
|
pass "DKIM signature added (s=$DKIM_SELECTOR d=$DOMAIN)"
|
|
else
|
|
fail "no DKIM signature logged for $_qid"
|
|
fi
|
|
}
|
|
|
|
cmd_validate() {
|
|
_dns=yes; _local_deliver=no; _ext=; _renew=no
|
|
while [ $# -gt 0 ]; do
|
|
case $1 in
|
|
--no-dns) _dns=no ;;
|
|
--deliver-test) _local_deliver=yes ;;
|
|
--external-test) _ext=$2; shift ;;
|
|
--renew-test) _renew=yes ;;
|
|
*) die "validate: unknown option $1" "$EX_USAGE" ;;
|
|
esac
|
|
shift
|
|
done
|
|
need_root validate
|
|
MODE=validate
|
|
preflight
|
|
validate_local
|
|
if [ "$_dns" = yes ]; then validate_dns; validate_outbound_port; fi
|
|
if [ "$_renew" = yes ]; then
|
|
section "Certificate renewal"
|
|
if [ "$TLS_MODE" != certbot ]; then
|
|
skip "TLS_MODE=$TLS_MODE: renewal is managed outside emailwiz"
|
|
elif certbot renew --dry-run --cert-name "$MAIL_HOSTNAME" >/dev/null 2>&1; then
|
|
pass "certbot renew --dry-run succeeded"
|
|
else
|
|
fail "certbot renew --dry-run failed (run it by hand to see why)"
|
|
fi
|
|
fi
|
|
[ "$_local_deliver" = yes ] && validate_delivery "postmaster@$DOMAIN" "local (postmaster@$DOMAIN)"
|
|
if [ -n "$_ext" ]; then
|
|
printf '%s' "$_ext" | grep -q '@' || die "--external-test needs an email address" "$EX_USAGE"
|
|
validate_delivery "$_ext" "external ($_ext)"
|
|
info "check the received message headers for dkim=pass spf=pass dmarc=pass"
|
|
fi
|
|
printf '\n%s failure(s), %s warning(s)\n' "$V_FAIL" "$V_WARN"
|
|
[ "$V_FAIL" -eq 0 ] || exit "$EX_VALIDATION"
|
|
}
|