2015-04-20 00:30:41 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2018-05-09 09:06:30 +02:00
|
|
|
[ "${DEBUG}" == "yes" ] && set -x
|
|
|
|
|
|
|
|
function add_config_value() {
|
|
|
|
local key=${1}
|
|
|
|
local value=${2}
|
2020-03-24 00:30:00 +01:00
|
|
|
# local config_file=${3:-/etc/postfix/main.cf}
|
2018-05-09 09:06:30 +02:00
|
|
|
[ "${key}" == "" ] && echo "ERROR: No key set !!" && exit 1
|
|
|
|
[ "${value}" == "" ] && echo "ERROR: No value set !!" && exit 1
|
|
|
|
|
|
|
|
echo "Setting configuration option ${key} with value: ${value}"
|
2019-02-17 18:50:21 +01:00
|
|
|
postconf -e "${key} = ${value}"
|
2018-05-09 09:06:30 +02:00
|
|
|
}
|
|
|
|
|
2020-06-19 23:52:21 +02:00
|
|
|
# Read password from file to avoid unsecure env variables
|
|
|
|
if [ -n "${SMTP_PASSWORD_FILE}" ]; then [ -f "${SMTP_PASSWORD_FILE}" ] && read SMTP_PASSWORD < ${SMTP_PASSWORD_FILE} || echo "SMTP_PASSWORD_FILE defined, but file not existing, skipping."; fi
|
|
|
|
|
2015-04-20 00:30:41 +02:00
|
|
|
[ -z "${SMTP_SERVER}" ] && echo "SMTP_SERVER is not set" && exit 1
|
|
|
|
[ -z "${SMTP_USERNAME}" ] && echo "SMTP_USERNAME is not set" && exit 1
|
|
|
|
[ -z "${SMTP_PASSWORD}" ] && echo "SMTP_PASSWORD is not set" && exit 1
|
2018-03-06 07:18:30 +01:00
|
|
|
[ -z "${SERVER_HOSTNAME}" ] && echo "SERVER_HOSTNAME is not set" && exit 1
|
2015-04-20 00:30:41 +02:00
|
|
|
|
2020-01-21 10:47:27 +01:00
|
|
|
SMTP_PORT="${SMTP_PORT:-587}"
|
2018-05-24 05:14:45 +02:00
|
|
|
|
2015-05-23 06:45:58 +02:00
|
|
|
#Get the domain from the server host name
|
2020-03-24 00:30:00 +01:00
|
|
|
DOMAIN=`echo ${SERVER_HOSTNAME} | awk 'BEGIN{FS=OFS="."}{print $(NF-1),$NF}'`
|
2015-05-23 06:45:58 +02:00
|
|
|
|
2018-05-09 09:06:30 +02:00
|
|
|
# Set needed config options
|
|
|
|
add_config_value "myhostname" ${SERVER_HOSTNAME}
|
|
|
|
add_config_value "mydomain" ${DOMAIN}
|
|
|
|
add_config_value "mydestination" '$myhostname'
|
|
|
|
add_config_value "myorigin" '$mydomain'
|
2018-05-24 05:14:45 +02:00
|
|
|
add_config_value "relayhost" "[${SMTP_SERVER}]:${SMTP_PORT}"
|
2018-05-09 09:06:30 +02:00
|
|
|
add_config_value "smtp_use_tls" "yes"
|
|
|
|
add_config_value "smtp_sasl_auth_enable" "yes"
|
2019-03-10 19:39:33 +01:00
|
|
|
add_config_value "smtp_sasl_password_maps" "hash:/etc/postfix/sasl_passwd"
|
2018-05-09 09:06:30 +02:00
|
|
|
add_config_value "smtp_sasl_security_options" "noanonymous"
|
2020-08-24 21:41:08 +02:00
|
|
|
add_config_value "always_add_missing_headers" "${ALWAYS_ADD_MISSING_HEADERS:-no}"
|
2015-05-13 08:06:38 +02:00
|
|
|
|
2018-05-09 09:06:30 +02:00
|
|
|
# Create sasl_passwd file with auth credentials
|
|
|
|
if [ ! -f /etc/postfix/sasl_passwd ]; then
|
|
|
|
grep -q "${SMTP_SERVER}" /etc/postfix/sasl_passwd > /dev/null 2>&1
|
|
|
|
if [ $? -gt 0 ]; then
|
|
|
|
echo "Adding SASL authentication configuration"
|
2018-05-24 05:14:45 +02:00
|
|
|
echo "[${SMTP_SERVER}]:${SMTP_PORT} ${SMTP_USERNAME}:${SMTP_PASSWORD}" >> /etc/postfix/sasl_passwd
|
2018-05-09 09:06:30 +02:00
|
|
|
postmap /etc/postfix/sasl_passwd
|
|
|
|
fi
|
|
|
|
fi
|
2015-04-20 00:30:41 +02:00
|
|
|
|
2020-03-23 05:17:24 +01:00
|
|
|
#Set header tag
|
2019-01-27 02:50:34 +01:00
|
|
|
if [ ! -z "${SMTP_HEADER_TAG}" ]; then
|
2019-01-20 05:58:15 +01:00
|
|
|
postconf -e "header_checks = regexp:/etc/postfix/header_tag"
|
|
|
|
echo -e "/^MIME-Version:/i PREPEND RelayTag: $SMTP_HEADER_TAG\n/^Content-Transfer-Encoding:/i PREPEND RelayTag: $SMTP_HEADER_TAG" > /etc/postfix/header_tag
|
2019-01-26 23:12:50 +01:00
|
|
|
echo "Setting configuration option SMTP_HEADER_TAG with value: ${SMTP_HEADER_TAG}"
|
2019-01-16 18:34:48 +01:00
|
|
|
fi
|
|
|
|
|
2019-03-17 22:17:51 +01:00
|
|
|
#Check for subnet restrictions
|
|
|
|
nets='10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16'
|
|
|
|
if [ ! -z "${SMTP_NETWORKS}" ]; then
|
|
|
|
for i in $(sed 's/,/\ /g' <<<$SMTP_NETWORKS); do
|
|
|
|
if grep -Eq "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/[0-9]{1,2}" <<<$i ; then
|
|
|
|
nets+=", $i"
|
|
|
|
else
|
|
|
|
echo "$i is not in proper IPv4 subnet format. Ignoring."
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
add_config_value "mynetworks" "${nets}"
|
|
|
|
|
2020-09-29 10:33:52 +02:00
|
|
|
if [ ! -z "${OVERWRITE_FROM}" ]; then
|
|
|
|
echo -e "/^From:.*$/ REPLACE From: $OVERWRITE_FROM" > /etc/postfix/smtp_header_checks
|
|
|
|
postmap /etc/postfix/smtp_header_checks
|
|
|
|
postconf -e 'smtp_header_checks = regexp:/etc/postfix/smtp_header_checks'
|
|
|
|
echo "Setting configuration option OVERWRITE_FROM with value: ${OVERWRITE_FROM}"
|
|
|
|
fi
|
|
|
|
|
2018-05-09 09:06:30 +02:00
|
|
|
#Start services
|
2019-10-24 00:20:20 +02:00
|
|
|
|
|
|
|
# If host mounting /var/spool/postfix, we need to delete old pid file before
|
|
|
|
# starting services
|
|
|
|
rm -f /var/spool/postfix/pid/master.pid
|
|
|
|
|
2020-03-23 05:17:24 +01:00
|
|
|
exec supervisord -c /etc/supervisord.conf
|