2023-02-26 10:42:14 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2023-02-27 19:21:45 +00:00
|
|
|
# Set up OpenDKIM
|
2023-02-26 10:42:14 +00:00
|
|
|
#
|
|
|
|
# ## Attention
|
|
|
|
#
|
2023-02-27 19:21:45 +00:00
|
|
|
# The OpenDKIM milter must come before the OpenDMARC milter in Postfix's
|
2023-02-26 10:42:14 +00:00
|
|
|
# `smtpd_milters` milters options.
|
2023-02-27 19:21:45 +00:00
|
|
|
function _setup_opendkim
|
2023-02-26 10:42:14 +00:00
|
|
|
{
|
|
|
|
if [[ ${ENABLE_OPENDKIM} -eq 1 ]]
|
|
|
|
then
|
2023-02-27 19:21:45 +00:00
|
|
|
_log 'debug' 'Configuring DKIM'
|
2023-02-26 10:42:14 +00:00
|
|
|
|
|
|
|
mkdir -p /etc/opendkim/keys/
|
|
|
|
touch /etc/opendkim/{SigningTable,TrustedHosts,KeyTable}
|
|
|
|
|
|
|
|
_log 'trace' "Adding OpenDKIM to Postfix's milters"
|
|
|
|
postconf 'dkim_milter = inet:localhost:8891'
|
|
|
|
# shellcheck disable=SC2016
|
|
|
|
sed -i -E \
|
|
|
|
-e 's|^(smtpd_milters =.*)|\1 \$dkim_milter|g' \
|
|
|
|
-e 's|^(non_smtpd_milters =.*)|\1 \$dkim_milter|g' \
|
|
|
|
/etc/postfix/main.cf
|
|
|
|
|
|
|
|
# check if any keys are available
|
|
|
|
if [[ -e /tmp/docker-mailserver/opendkim/KeyTable ]]
|
|
|
|
then
|
|
|
|
cp -a /tmp/docker-mailserver/opendkim/* /etc/opendkim/
|
|
|
|
_log 'trace' "DKIM keys added for: $(find /etc/opendkim/keys/ -maxdepth 1 -type f -printf '%f ')"
|
|
|
|
chown -R opendkim:opendkim /etc/opendkim/
|
|
|
|
chmod -R 0700 /etc/opendkim/keys/
|
|
|
|
else
|
|
|
|
_log 'debug' 'OpenDKIM enabled but no DKIM key(s) provided'
|
|
|
|
fi
|
|
|
|
|
|
|
|
# setup nameservers parameter from /etc/resolv.conf if not defined
|
|
|
|
if ! grep -q '^Nameservers' /etc/opendkim.conf
|
|
|
|
then
|
|
|
|
local NAMESERVER_IPS
|
|
|
|
NAMESERVER_IPS=$(grep '^nameserver' /etc/resolv.conf | awk -F " " '{print $2}' | paste -sd ',' -)
|
|
|
|
echo "Nameservers ${NAMESERVER_IPS}" >>/etc/opendkim.conf
|
|
|
|
_log 'trace' "Nameservers added to '/etc/opendkim.conf'"
|
|
|
|
fi
|
2023-02-27 19:21:45 +00:00
|
|
|
else
|
|
|
|
# Even though we do nothing here and the message suggests we perform some action, the
|
|
|
|
# message is due to the default value being `1`, i.e. enabled. If the default were `0`,
|
|
|
|
# we could have said `OpenDKIM is disabled`, but we need to make it uniform with all
|
|
|
|
# other functions.
|
|
|
|
_log 'debug' 'Disabling OpenDKIM'
|
2023-02-26 10:42:14 +00:00
|
|
|
fi
|
2023-02-27 19:21:45 +00:00
|
|
|
}
|
2023-02-26 10:42:14 +00:00
|
|
|
|
2023-05-11 16:10:51 +00:00
|
|
|
# Set up OpenDMARC
|
2023-02-27 19:21:45 +00:00
|
|
|
#
|
|
|
|
# ## Attention
|
|
|
|
#
|
|
|
|
# The OpenDMARC milter must come after the OpenDKIM milter in Postfix's
|
|
|
|
# `smtpd_milters` milters options.
|
|
|
|
function _setup_opendmarc
|
|
|
|
{
|
2023-02-26 10:42:14 +00:00
|
|
|
if [[ ${ENABLE_OPENDMARC} -eq 1 ]]
|
|
|
|
then
|
2023-02-27 19:21:45 +00:00
|
|
|
# TODO When disabling SPF is possible, add a check whether DKIM and SPF is disabled
|
2023-02-26 10:42:14 +00:00
|
|
|
# for DMARC to work, you should have at least one enabled
|
|
|
|
# (see RFC 7489 https://www.rfc-editor.org/rfc/rfc7489#page-24)
|
2023-02-27 19:21:45 +00:00
|
|
|
_log 'debug' 'Configuring OpenDMARC'
|
|
|
|
|
2023-02-26 10:42:14 +00:00
|
|
|
_log 'trace' "Adding OpenDMARC to Postfix's milters"
|
|
|
|
postconf 'dmarc_milter = inet:localhost:8893'
|
|
|
|
# Make sure to append the OpenDMARC milter _after_ the OpenDKIM milter!
|
|
|
|
# shellcheck disable=SC2016
|
|
|
|
sed -i -E 's|^(smtpd_milters =.*)|\1 \$dmarc_milter|g' /etc/postfix/main.cf
|
|
|
|
|
2023-02-27 19:21:45 +00:00
|
|
|
sed -i \
|
|
|
|
-e "s|^AuthservID.*$|AuthservID ${HOSTNAME}|g" \
|
|
|
|
-e "s|^TrustedAuthservIDs.*$|TrustedAuthservIDs ${HOSTNAME}|g" \
|
|
|
|
/etc/opendmarc.conf
|
|
|
|
else
|
|
|
|
# Even though we do nothing here and the message suggests we perform some action, the
|
|
|
|
# message is due to the default value being `1`, i.e. enabled. If the default were `0`,
|
|
|
|
# we could have said `OpenDKIM is disabled`, but we need to make it uniform with all
|
|
|
|
# other functions.
|
|
|
|
_log 'debug' 'Disabling OpenDMARC'
|
|
|
|
fi
|
2023-02-26 10:42:14 +00:00
|
|
|
}
|
2023-04-11 06:52:43 +00:00
|
|
|
|
|
|
|
# Configures the SPF check inside Postfix's configuration via policyd-spf. When
|
|
|
|
# using Rspamd, you will likely want to turn that off.
|
|
|
|
function _setup_policyd_spf
|
|
|
|
{
|
|
|
|
if [[ ${ENABLE_POLICYD_SPF} -eq 1 ]]
|
|
|
|
then
|
|
|
|
_log 'debug' 'Configuring policyd-spf'
|
|
|
|
cat >>/etc/postfix/master.cf <<EOF
|
|
|
|
|
|
|
|
policyd-spf unix - n n - 0 spawn
|
|
|
|
user=policyd-spf argv=/usr/bin/policyd-spf
|
|
|
|
EOF
|
2023-04-14 22:40:42 +00:00
|
|
|
|
2023-04-23 10:16:53 +00:00
|
|
|
# SPF policy settings
|
|
|
|
postconf 'policyd-spf_time_limit = 3600'
|
|
|
|
sedfile -i -E \
|
|
|
|
's|^(smtpd_recipient_restrictions.*reject_unauth_destination)(.*)|\1, check_policy_service unix:private/policyd-spf\2|' \
|
|
|
|
/etc/postfix/main.cf
|
2023-04-11 06:52:43 +00:00
|
|
|
else
|
|
|
|
_log 'debug' 'Disabling policyd-spf'
|
|
|
|
fi
|
|
|
|
}
|