smtp/init

91 lines
3.2 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
# 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
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