smtp/init

114 lines
4.3 KiB
Plaintext
Raw Normal View History

2023-04-25 10:39:41 +00:00
#!/bin/sh
2017-07-23 07:56:38 +00:00
set -e
2023-05-02 07:16:10 +00:00
# @NOTE: Directories in /var/mail/vhosts/* need to be owned by the UID/GID defined in custom/41-virtual.cf
DEFAULTS=/defaults
POSTFIX=/etc/postfix
CUSTOM=${POSTFIX}/custom
INCLUDE_DIR=${POSTFIX}/include
2017-07-23 07:56:38 +00:00
NAME="SMTP"
function stop {
echo "Stopping ${NAME}"
2023-05-02 07:16:10 +00:00
postfix stop
2018-01-31 11:56:02 +00:00
kill $(cat /run/saslauthd/saslauthd.pid)
2023-05-02 07:16:10 +00:00
kill $(cat /run/opendkim/opendkim.pid)
2017-07-23 07:56:38 +00:00
}
trap 'stop' SIGTERM
2023-04-25 10:39:41 +00:00
if [ -z "$@" ]; then
if [ -z `hostname -d` ]; then
echo "You must start this container with --hostname= specifying a domain name"
exit 1
fi
2017-07-23 07:56:38 +00:00
2023-05-02 07:16:10 +00:00
# Start some supporting daemons
2023-04-28 05:16:31 +00:00
/usr/sbin/saslauthd -m /run/saslauthd -ca shadow
2023-05-02 07:16:10 +00:00
/usr/sbin/opendkim -u opendkim -P /run/opendkim/opendkim.pid
if [ -n "${SPAMHAUS_DQS_KEY}" ]; then
sed -i -e "s/@SPAMHAUS_DQS_KEY@/${SPAMHAUS_DQS_KEY}/g" ${DEFAULTS}/dnsbl-reply-map ${INCLUDE_DIR}/01-custom.cf
sed -i -e "s/^#reject/ reject/" ${INCLUDE_DIR}/01-custom.cf
if [ ! -f ${CUSTOM}/dnsbl-reply-map ]; then
cp ${DEFAULTS}/dnsbl-reply-map ${CUSTOM}/
postmap -o lmdb:${CUSTOM}/dnsbl-reply-map
fi
cp ${DEFAULTS}/60-spamhaus.cf ${INCLUDE_DIR}/
fi
2023-05-02 07:16:10 +00:00
# Setup our postfix environment
if [ -d ${INCLUDE_DIR} ]; then
if ls -1 ${INCLUDE_DIR}/*.cf >/dev/null 2>&1; then
echo "* Adding to main.cf"
echo '##### CUSTOM CONFIGURATION ####' >> ${POSTFIX}/main.cf
for i in ${INCLUDE_DIR}/*.cf; do
echo "* Adding [${i}] to main.cf"
echo "# - ${i}" >> ${POSTFIX}/main.cf
cat $i >> ${POSTFIX}/main.cf
done
echo '##### END CUSTOM CONFIGURATION ####' >> ${POSTFIX}/main.cf
fi
fi
[ ! -f ${CUSTOM}/relay_domains ] && cp ${DEFAULTS}/relay_domains ${CUSTOM}/
postmap -o lmdb:${CUSTOM}/relay_domains
[ ! -f ${CUSTOM}/relay_recipient_maps ] && cp ${DEFAULTS}/relay_recipient_maps ${CUSTOM}/
postmap -o lmdb:${CUSTOM}/relay_recipient_maps
touch ${CUSTOM}/sasl_passwd
postmap -o lmdb:${CUSTOM}/sasl_passwd
[ -n "${VIRTUAL_ALIAS_DOMAINS}" ] && sed -i -e "s%#virtual_alias_domains = @VIRTUAL_ALIAS_DOMAINS@%virtual_alias_domains = ${VIRTUAL_ALIAS_DOMAINS}%" ${POSTFIX}/main.cf
[ ! -f ${CUSTOM}/virtual_alias_maps ] && cp ${DEFAULTS}/virtual_alias_maps ${CUSTOM}/
postmap -o lmdb:${CUSTOM}/virtual_alias_maps
[ -n "${VIRTUAL_DOMAINS}" ] && sed -i -e "s%#virtual_mailbox_domains = @VIRTUAL_DOMAINS@%virtual_mailbox_domains = ${VIRTUAL_DOMAINS}%" ${POSTFIX}/main.cf
[ ! -f ${CUSTOM}/virtual_mailbox_maps ] && cp ${DEFAULTS}/virtual_mailbox_maps ${CUSTOM}/
postmap -o lmdb:${CUSTOM}/virtual_mailbox_maps
# Create some default LDAP templates
if [ -n "${LDAP_SEARCH_BASE}" ]; then
sed -i -e "s%^# ldap:% ldap:%" ${POSTFIX}/main.cf
if [ ! -f ${CUSTOM}/ldap_virtual_alias_maps ]; then
cp ${DEFAULTS}/ldap_virtual_alias_maps ${CUSTOM}/
sed -i -e "s%@THIS_HOST@%${HOSTNAME}%" -e "s%@LDAP_SEARCH_BASE@%${LDAP_SEARCH_BASE}%" ${CUSTOM}/ldap_virtual_alias_maps
fi
if [ ! -f ${CUSTOM}/ldap_virtual_mailbox_maps ]; then
cp ${DEFAULTS}/ldap_virtual_mailbox_maps ${CUSTOM}/
sed -i -e "s%@THIS_HOST@%${HOSTNAME}%" -e "s%@LDAP_SEARCH_BASE@%${LDAP_SEARCH_BASE}%" ${CUSTOM}/ldap_virtual_mailbox_maps
fi
if [ ! -f ${CUSTOM}/ldap_relay_domains ]; then
cp ${DEFAULTS}/ldap_relay_domains ${CUSTOM}/
sed -i -e "s%@THIS_HOST@%${HOSTNAME}%" -e "s%@LDAP_SEARCH_BASE@%${LDAP_SEARCH_BASE}%" ${CUSTOM}/ldap_relay_domains
fi
if [ ! -f ${CUSTOM}/ldap_relay_recipient_maps ]; then
cp ${DEFAULTS}/ldap_relay_recipient_maps ${CUSTOM}/
sed -i -e "s%@THIS_HOST@%${HOSTNAME}%" -e "s%@LDAP_SEARCH_BASE@%${LDAP_SEARCH_BASE}%" ${CUSTOM}/ldap_relay_recipient_maps
fi
2023-05-02 07:16:10 +00:00
fi
[ ! -f ${CUSTOM}/header_checks ] && cp ${DEFAULTS}/header_checks ${CUSTOM}/ && postmap -o lmdb:${CUSTOM}/header_checks
[ ! -f ${CUSTOM}/helo_access ] && cp ${DEFAULTS}/helo_access ${CUSTOM}/ && postmap -o lmdb:${CUSTOM}/helo_access
[ ! -f ${CUSTOM}/recipient_access ] && cp ${DEFAULTS}/recipient_access ${CUSTOM}/ && postmap -o lmdb:${CUSTOM}/recipient_access
[ ! -f ${CUSTOM}/recipient_canonical ] && cp ${DEFAULTS}/recipient_canonical ${CUSTOM}/ && postmap -o lmdb:${CUSTOM}/recipient_canonical
[ ! -f ${CUSTOM}/sender_access ] && cp ${DEFAULTS}/sender_access ${CUSTOM}/ && postmap -o lmdb:${CUSTOM}/sender_access
[ ! -f ${CUSTOM}/sender_canonical ] && cp ${DEFAULTS}/sender_canonical ${CUSTOM}/ && postmap -o lmdb:${CUSTOM}/sender_canonical
2023-04-25 10:39:41 +00:00
newaliases
postfix start
2017-07-23 07:56:38 +00:00
2023-05-02 07:16:10 +00:00
# Sleep,enabling our SIGTERM to shut us down gracefully
(while true; do sleep 3600; done) &
2017-07-23 07:56:38 +00:00
wait
else
exec $@
fi