smtp/init
2023-05-02 17:16:10 +10:00

91 lines
3.2 KiB
Bash
Executable File

#!/bin/sh
set -e
# @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
NAME="SMTP"
function stop {
echo "Stopping ${NAME}"
postfix stop
kill $(cat /run/saslauthd/saslauthd.pid)
kill $(cat /run/opendkim/opendkim.pid)
}
trap 'stop' SIGTERM
if [ -z "$@" ]; then
if [ -z `hostname -d` ]; then
echo "You must start this container with --hostname= specifying a domain name"
exit 1
fi
# Start some supporting daemons
/usr/sbin/saslauthd -m /run/saslauthd -ca shadow
/usr/sbin/opendkim -u opendkim -P /run/opendkim/opendkim.pid
# 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 [ ! -f ${CUSTOM}/ldap_virtual_alias_maps -a -n "${LDAP_SEARCH_BASE}" ]; 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 -a -n "${LDAP_SEARCH_BASE}" ]; 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 -a -n "${LDAP_SEARCH_BASE}" ]; 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 -a -n "${LDAP_SEARCH_BASE}" ]; 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
newaliases
postfix start
# Sleep,enabling our SIGTERM to shut us down gracefully
(while true; do sleep 3600; done) &
wait
else
exec $@
fi