2015-04-19 22:30:41 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2018-05-09 07:06:30 +00:00
|
|
|
[ "${DEBUG}" == "yes" ] && set -x
|
|
|
|
|
2024-07-07 17:46:37 +00:00
|
|
|
# GPG/PMB Area -------------------
|
2024-07-07 17:12:32 +00:00
|
|
|
gpg-agent --daemon --batch --disable-scdaemon
|
2024-07-07 17:46:37 +00:00
|
|
|
# ProtonMail bridge has really bad timeout/retry logic:
|
|
|
|
sleep 2
|
|
|
|
/pmb/bridge -n &
|
2024-07-07 17:12:32 +00:00
|
|
|
|
2024-07-07 17:46:37 +00:00
|
|
|
# Postfix area ------------------
|
2024-07-07 17:12:32 +00:00
|
|
|
|
2018-05-09 07:06:30 +00:00
|
|
|
function add_config_value() {
|
|
|
|
local key=${1}
|
|
|
|
local value=${2}
|
2020-03-23 23:30:00 +00:00
|
|
|
# local config_file=${3:-/etc/postfix/main.cf}
|
2018-05-09 07:06:30 +00: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 17:50:21 +00:00
|
|
|
postconf -e "${key} = ${value}"
|
2018-05-09 07:06:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 23:03:01 +00:00
|
|
|
# Read password and username from file to avoid unsecure env variables
|
2021-11-02 09:36:20 +00:00
|
|
|
if [ -n "${SMTP_PASSWORD_FILE}" ]; then [ -e "${SMTP_PASSWORD_FILE}" ] && SMTP_PASSWORD=$(cat "${SMTP_PASSWORD_FILE}") || echo "SMTP_PASSWORD_FILE defined, but file not existing, skipping."; fi
|
|
|
|
if [ -n "${SMTP_USERNAME_FILE}" ]; then [ -e "${SMTP_USERNAME_FILE}" ] && SMTP_USERNAME=$(cat "${SMTP_USERNAME_FILE}") || echo "SMTP_USERNAME_FILE defined, but file not existing, skipping."; fi
|
2020-06-19 21:52:21 +00:00
|
|
|
|
2015-04-19 22:30:41 +00:00
|
|
|
[ -z "${SMTP_SERVER}" ] && echo "SMTP_SERVER is not set" && exit 1
|
2018-03-06 06:18:30 +00:00
|
|
|
[ -z "${SERVER_HOSTNAME}" ] && echo "SERVER_HOSTNAME is not set" && exit 1
|
2021-03-23 21:41:01 +00:00
|
|
|
[ ! -z "${SMTP_USERNAME}" -a -z "${SMTP_PASSWORD}" ] && echo "SMTP_USERNAME is set but SMTP_PASSWORD is not set" && exit 1
|
2015-04-19 22:30:41 +00:00
|
|
|
|
2020-01-21 09:47:27 +00:00
|
|
|
SMTP_PORT="${SMTP_PORT:-587}"
|
2018-05-24 03:14:45 +00:00
|
|
|
|
2015-05-23 04:45:58 +00:00
|
|
|
#Get the domain from the server host name
|
2020-03-23 23:30:00 +00:00
|
|
|
DOMAIN=`echo ${SERVER_HOSTNAME} | awk 'BEGIN{FS=OFS="."}{print $(NF-1),$NF}'`
|
2015-05-23 04:45:58 +00:00
|
|
|
|
2018-05-09 07:06:30 +00:00
|
|
|
# Set needed config options
|
2021-04-27 21:44:39 +00:00
|
|
|
add_config_value "maillog_file" "/dev/stdout"
|
2018-05-09 07:06:30 +00:00
|
|
|
add_config_value "myhostname" ${SERVER_HOSTNAME}
|
|
|
|
add_config_value "mydomain" ${DOMAIN}
|
2021-06-30 09:05:26 +00:00
|
|
|
add_config_value "mydestination" "${DESTINATION:-localhost}"
|
2018-05-09 07:06:30 +00:00
|
|
|
add_config_value "myorigin" '$mydomain'
|
2018-05-24 03:14:45 +00:00
|
|
|
add_config_value "relayhost" "[${SMTP_SERVER}]:${SMTP_PORT}"
|
2018-05-09 07:06:30 +00:00
|
|
|
add_config_value "smtp_use_tls" "yes"
|
2021-03-23 21:41:01 +00:00
|
|
|
if [ ! -z "${SMTP_USERNAME}" ]; then
|
2024-07-07 17:46:37 +00:00
|
|
|
# Following line added for proton bridge
|
2024-07-07 17:12:32 +00:00
|
|
|
add_config_value "smtp_sasl_mechanism_filter" "login"
|
2021-03-23 21:41:01 +00:00
|
|
|
add_config_value "smtp_sasl_auth_enable" "yes"
|
|
|
|
add_config_value "smtp_sasl_password_maps" "lmdb:/etc/postfix/sasl_passwd"
|
|
|
|
add_config_value "smtp_sasl_security_options" "noanonymous"
|
|
|
|
fi
|
2020-08-24 19:41:08 +00:00
|
|
|
add_config_value "always_add_missing_headers" "${ALWAYS_ADD_MISSING_HEADERS:-no}"
|
2021-03-17 22:17:39 +00:00
|
|
|
#Also use "native" option to allow looking up hosts added to /etc/hosts via
|
|
|
|
# docker options (issue #51)
|
|
|
|
add_config_value "smtp_host_lookup" "native,dns"
|
2015-05-13 06:06:38 +00:00
|
|
|
|
2020-12-16 16:18:27 +00:00
|
|
|
if [ "${SMTP_PORT}" = "465" ]; then
|
|
|
|
add_config_value "smtp_tls_wrappermode" "yes"
|
|
|
|
add_config_value "smtp_tls_security_level" "encrypt"
|
|
|
|
fi
|
|
|
|
|
2022-04-28 15:49:08 +00:00
|
|
|
# Bind to both IPv4 and IPv4
|
|
|
|
add_config_value "inet_protocols" "all"
|
|
|
|
|
2018-05-09 07:06:30 +00:00
|
|
|
# Create sasl_passwd file with auth credentials
|
2021-03-23 21:41:01 +00:00
|
|
|
if [ ! -f /etc/postfix/sasl_passwd -a ! -z "${SMTP_USERNAME}" ]; then
|
2018-05-09 07:06:30 +00:00
|
|
|
grep -q "${SMTP_SERVER}" /etc/postfix/sasl_passwd > /dev/null 2>&1
|
|
|
|
if [ $? -gt 0 ]; then
|
|
|
|
echo "Adding SASL authentication configuration"
|
2021-07-08 14:11:22 +00:00
|
|
|
echo "[${SMTP_SERVER}]:${SMTP_PORT} ${SMTP_USERNAME}:${SMTP_PASSWORD}" >> /etc/postfix/sasl_passwd
|
2018-05-09 07:06:30 +00:00
|
|
|
postmap /etc/postfix/sasl_passwd
|
|
|
|
fi
|
|
|
|
fi
|
2015-04-19 22:30:41 +00:00
|
|
|
|
2020-03-23 04:17:24 +00:00
|
|
|
#Set header tag
|
2019-01-27 01:50:34 +00:00
|
|
|
if [ ! -z "${SMTP_HEADER_TAG}" ]; then
|
2021-10-27 15:42:03 +00:00
|
|
|
postconf -e "header_checks = regexp:/etc/postfix/header_checks"
|
|
|
|
echo -e "/^MIME-Version:/i PREPEND RelayTag: $SMTP_HEADER_TAG\n/^Content-Transfer-Encoding:/i PREPEND RelayTag: $SMTP_HEADER_TAG" >> /etc/postfix/header_checks
|
2019-01-26 22:12:50 +00:00
|
|
|
echo "Setting configuration option SMTP_HEADER_TAG with value: ${SMTP_HEADER_TAG}"
|
2019-01-16 17:34:48 +00:00
|
|
|
fi
|
|
|
|
|
2021-10-27 15:42:03 +00:00
|
|
|
#Enable logging of subject line
|
|
|
|
if [ "${LOG_SUBJECT}" == "yes" ]; then
|
|
|
|
postconf -e "header_checks = regexp:/etc/postfix/header_checks"
|
|
|
|
echo -e "/^Subject:/ WARN" >> /etc/postfix/header_checks
|
|
|
|
echo "Enabling logging of subject line"
|
|
|
|
fi
|
|
|
|
|
2019-03-17 21:17:51 +00: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
|
2022-04-28 15:49:08 +00:00
|
|
|
declare ipv6re="^((([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|\
|
|
|
|
([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|\
|
|
|
|
([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|\
|
|
|
|
([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|\
|
|
|
|
:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}|\
|
|
|
|
::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|\
|
|
|
|
(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|\
|
|
|
|
(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/[0-9]{1,3})$"
|
|
|
|
|
|
|
|
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"
|
|
|
|
elif grep -Eq "$ipv6re" <<<$i ; then
|
|
|
|
readarray -d \/ -t arr < <(printf '%s' "$i")
|
|
|
|
nets+=", [${arr[0]}]/${arr[1]}"
|
|
|
|
else
|
|
|
|
echo "$i is not in proper IPv4 or IPv6 subnet format. Ignoring."
|
|
|
|
fi
|
|
|
|
done
|
2019-03-17 21:17:51 +00:00
|
|
|
fi
|
|
|
|
add_config_value "mynetworks" "${nets}"
|
|
|
|
|
2021-11-19 09:31:40 +00:00
|
|
|
# Set SMTPUTF8
|
|
|
|
if [ ! -z "${SMTPUTF8_ENABLE}" ]; then
|
|
|
|
postconf -e "smtputf8_enable = ${SMTPUTF8_ENABLE}"
|
|
|
|
echo "Setting configuration option smtputf8_enable with value: ${SMTPUTF8_ENABLE}"
|
|
|
|
fi
|
|
|
|
|
2020-09-29 08:33:52 +00: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
|
|
|
|
|
2022-06-20 21:38:09 +00:00
|
|
|
# Set message_size_limit
|
|
|
|
if [ ! -z "${MESSAGE_SIZE_LIMIT}" ]; then
|
|
|
|
postconf -e "message_size_limit = ${MESSAGE_SIZE_LIMIT}"
|
|
|
|
echo "Setting configuration option message_size_limit with value: ${MESSAGE_SIZE_LIMIT}"
|
|
|
|
fi
|
|
|
|
|
2018-05-09 07:06:30 +00:00
|
|
|
#Start services
|
2019-10-23 22:20:20 +00: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
|
|
|
|
|
2021-04-27 21:44:39 +00:00
|
|
|
exec /usr/sbin/postfix -c /etc/postfix start-fg
|