Files
emailwiz/lib/postfix.sh
T

147 lines
6.2 KiB
Bash

# emailwiz - Postfix (rendered by running postconf against a copy of /etc/postfix)
# SPDX-License-Identifier: GPL-3.0-or-later
# shellcheck shell=sh
PF_HDR_CHECKS=/etc/postfix/emailwiz_outbound_header_checks
PF_LOGIN_MAPS=/etc/postfix/login_maps.pcre
# pf_service NAME TYPE "MASTER.CF LINE" "param=value" ...
# Replaces the service entry and all of its -o overrides with exactly the
# given ones, so stale overrides (e.g. emailwiz's smtpd_enforce_tls) go away.
pf_service() {
_n=$1 _t=$2 _line=$3; shift 3
postconf -c "$PFDIR" -M "$_n/$_t=$_line"
postconf -c "$PFDIR" -P 2>/dev/null | sed -n "s|^$_n/$_t/\([^=]*\)=.*|\1|p" |
while read -r _param; do
postconf -c "$PFDIR" -PX "$_n/$_t/$_param"
done
for _kv in "$@"; do
postconf -c "$PFDIR" -P "$_n/$_t/$_kv"
done
}
postfix_render() {
PFDIR=$(mktemp -d "$STAGE/.postfix.XXXXXX")
cp -p /etc/postfix/main.cf /etc/postfix/master.cf "$PFDIR/"
_pref=$SMTP_ADDRESS_PREFERENCE
if [ "$_pref" = auto ]; then
if has_global_ipv6; then _pref=any; else _pref=ipv4; fi
fi
_milter="inet:127.0.0.1:$DKIM_PORT"
postconf -c "$PFDIR" -e \
"myhostname = $MAIL_HOSTNAME" \
"mydomain = $DOMAIN" \
"mydestination = \$myhostname, \$mydomain, localhost.\$mydomain, localhost" \
"smtpd_banner = \$myhostname ESMTP" \
"smtp_address_preference = $_pref" \
"smtpd_tls_cert_file = $TLS_CERT" \
"smtpd_tls_key_file = $TLS_KEY" \
"smtpd_tls_security_level = may" \
"smtp_tls_security_level = may" \
"smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt" \
"smtpd_tls_protocols = >=TLSv1.2" \
"smtpd_tls_mandatory_protocols = >=TLSv1.2" \
"smtp_tls_protocols = >=TLSv1.2" \
"smtp_tls_mandatory_protocols = >=TLSv1.2" \
"smtpd_tls_auth_only = yes" \
"smtpd_sasl_type = dovecot" \
"smtpd_sasl_path = private/auth" \
"smtpd_sasl_auth_enable = no" \
"smtpd_sasl_security_options = noanonymous, noplaintext" \
"smtpd_sasl_tls_security_options = noanonymous" \
"smtpd_sender_login_maps = pcre:$PF_LOGIN_MAPS" \
"smtpd_helo_required = yes" \
"smtpd_helo_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_invalid_helo_hostname, reject_non_fqdn_helo_hostname, reject_unknown_helo_hostname" \
"smtpd_sender_restrictions = permit_mynetworks, reject_sender_login_mismatch, permit_sasl_authenticated, reject_unknown_reverse_client_hostname, reject_unknown_sender_domain" \
"smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination" \
"smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination, reject_unknown_recipient_domain" \
"smtpd_forbid_bare_newline = normalize" \
"smtpd_forbid_bare_newline_exclusions = \$mynetworks" \
"milter_default_action = accept" \
"milter_protocol = 6" \
"smtpd_milters = $_milter" \
"non_smtpd_milters = $_milter" \
"home_mailbox = Mail/Inbox/" \
"mailbox_command = /usr/lib/dovecot/dovecot-lda -f \"\$SENDER\" -a \"\$RECIPIENT\"" \
"recipient_delimiter = +"
# emailwiz stripped Received: from *all* mail in cleanup (inbound too).
# Strip only on outbound SMTP so inbound trace headers reach SpamAssassin.
postconf -c "$PFDIR" -X header_checks
if [ "$STRIP_OUTBOUND_RECEIVED" = yes ]; then
postconf -c "$PFDIR" -e "smtp_header_checks = regexp:$PF_HDR_CHECKS"
stage_file "$PF_HDR_CHECKS" 644 root root postfix <<'EOF'
# Managed by emailwiz. Applied by the SMTP client to outbound mail only, to
# avoid leaking submitting clients' IP addresses.
/^Received:/ IGNORE
/^X-Originating-IP:/ IGNORE
EOF
else
postconf -c "$PFDIR" -X smtp_header_checks
fi
# Senders @DOMAIN must be logged in as the matching local user.
_re=$(printf '%s' "$DOMAIN" | sed 's/\./\\./g')
stage_file "$PF_LOGIN_MAPS" 644 root root postfix <<EOF
# Managed by emailwiz: localpart@$DOMAIN may only be used by SASL user localpart.
/^([^@+]+)(\+[^@]*)?@$_re\$/ \${1}
EOF
_sub="smtpd_tls_auth_only=yes smtpd_sasl_auth_enable=yes
smtpd_client_restrictions=permit_sasl_authenticated,reject
smtpd_sender_restrictions=reject_sender_login_mismatch,permit_sasl_authenticated,reject
smtpd_relay_restrictions=permit_sasl_authenticated,reject
smtpd_recipient_restrictions=permit_sasl_authenticated,reject
milter_macro_daemon_name=ORIGINATING"
if [ "$SPAM_FILTER" = spamc ]; then
pf_service smtp inet "smtp inet n - y - - smtpd" "content_filter=spamassassin"
pf_service spamassassin unix \
"spamassassin unix - n n - - pipe user=debian-spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f \${sender} \${recipient}"
else
pf_service smtp inet "smtp inet n - y - - smtpd"
postconf -c "$PFDIR" -MX spamassassin/unix 2>/dev/null || :
fi
# shellcheck disable=SC2086
pf_service submission inet "submission inet n - y - - smtpd" \
"syslog_name=postfix/submission" "smtpd_tls_security_level=encrypt" $_sub
# shellcheck disable=SC2086
pf_service smtps inet "smtps inet n - y - - smtpd" \
"syslog_name=postfix/smtps" "smtpd_tls_wrappermode=yes" $_sub
# Postfix warns about unknown/obsolete parameters; treat that as fatal.
_w=$(postconf -c "$PFDIR" -n 2>&1 >/dev/null | grep -i warning || :)
[ -z "$_w" ] || die "postconf warnings in rendered config: $_w" "$EX_VALIDATION"
stage_file /etc/postfix/main.cf 644 root root postfix <"$PFDIR/main.cf"
stage_file /etc/postfix/master.cf 644 root root postfix-restart <"$PFDIR/master.cf"
postfix_render_aliases
}
postfix_render_aliases() {
_al=/etc/aliases
_tmp=$STAGE/.aliases
if [ -f "$_al" ]; then cp "$_al" "$_tmp"; else : >"$_tmp"; fi
grep -Eq '^postmaster:' "$_tmp" || printf 'postmaster:\troot\n' >>"$_tmp"
if [ -n "$ROOT_ALIAS" ] && ! grep -Eq '^root:' "$_tmp"; then
printf 'root:\t%s\n' "$ROOT_ALIAS" >>"$_tmp"
fi
stage_file "$_al" 644 root root newaliases <"$_tmp"
if [ ! -f "$_al.db" ] || [ -n "$(find "$_al" -newer "$_al.db" 2>/dev/null)" ]; then
stage_action newaliases "$_al.db is missing or older than $_al"
fi
# Postfix delivers root's mail with default_privs (nobody), which cannot
# write a mailbox; root must be aliased to a real user.
grep -Eq '^root:' "$_tmp" ||
note "root has no alias in $_al: set ROOT_ALIAS so postmaster/DMARC mail is deliverable"
}
postfix_validate_live() {
postfix check 2>&1 | grep -v 'not owned by root: /var/spool/postfix/etc/resolv.conf' |
grep -iE 'warning|fatal|error' && return 1
return 0
}