#!/bin/bash
# update/extend grommunio postfix settings as part of grommunio-update

if [ ! -e /etc/postfix/grommunio-virtual-mailbox-domains.cf ]; then
exit 1
fi

MYSQL_USER=$(grep "^user" /etc/postfix/grommunio-virtual-mailbox-domains.cf | awk -F'^user = ' '{ print $2 }')
MYSQL_PASS=$(grep "^password" /etc/postfix/grommunio-virtual-mailbox-domains.cf | awk -F'^password = ' '{ print $2 }')
MYSQL_HOST=$(grep "^hosts" /etc/postfix/grommunio-virtual-mailbox-domains.cf | awk -F'^hosts = ' '{ print $2 }')
MYSQL_DB=$(grep "^dbname" /etc/postfix/grommunio-virtual-mailbox-domains.cf | awk -F'^dbname = ' '{ print $2 }')

# update base config grommunio-virtual-mailbox-domains.cf (query might have changed)

cat > /etc/postfix/grommunio-virtual-mailbox-domains.cf <<EOF
user = ${MYSQL_USER}
password = ${MYSQL_PASS}
hosts = ${MYSQL_HOST}
dbname = ${MYSQL_DB}
query = SELECT 1 FROM domains WHERE domain_status=0 AND domainname=_utf8mb4'%s' COLLATE utf8mb4_general_ci
EOF

postconf -e virtual_mailbox_domains="mysql:/etc/postfix/grommunio-virtual-mailbox-domains.cf"

# update/extend other postfix configs which have been extended over the time (grommunio-setup)

if [ ! -e /etc/postfix/grommunio-virtual-mailbox-alias-maps.cf ]; then

cat > /etc/postfix/grommunio-virtual-mailbox-alias-maps.cf <<EOF
user = ${MYSQL_USER}
password = ${MYSQL_PASS}
hosts = ${MYSQL_HOST}
dbname = ${MYSQL_DB}
query = SELECT mainname FROM aliases WHERE aliasname=_utf8mb4'%s' COLLATE utf8mb4_general_ci UNION SELECT destination FROM forwards WHERE username=_utf8mb4'%s' COLLATE utf8mb4_general_ci AND forward_type = 1
EOF

postconf -e virtual_alias_maps="mysql:/etc/postfix/grommunio-virtual-mailbox-alias-maps.cf"

fi

if [ ! -e /etc/postfix/grommunio-virtual-mailbox-maps.cf ]; then

cat > /etc/postfix/grommunio-virtual-mailbox-maps.cf <<EOF
user = ${MYSQL_USER}
password = ${MYSQL_PASS}
hosts = ${MYSQL_HOST}
dbname = ${MYSQL_DB}
query = SELECT 1 FROM users WHERE username=_utf8mb4'%s' COLLATE utf8mb4_general_ci
EOF

postconf -e virtual_mailbox_maps="mysql:/etc/postfix/grommunio-virtual-mailbox-maps.cf"

fi

if [ ! -e /etc/postfix/grommunio-bcc-forwards.cf ]; then

cat > /etc/postfix/grommunio-bcc-forwards.cf <<EOF
user = ${MYSQL_USER}
password = ${MYSQL_PASS}
hosts = ${MYSQL_HOST}
dbname = ${MYSQL_DB}
query = SELECT destination FROM forwards WHERE username=_utf8mb4'%s' COLLATE utf8mb4_general_ci AND forward_type = 0
EOF

postconf -e recipient_bcc_maps="mysql:/etc/postfix/grommunio-bcc-forwards.cf"

fi

# replace obsolete TLS/DNS parameters (Postfix 3.11 warns) by equivalents
migrate_obsolete_tls() {
	local p="$1" level use enforce
	use=$(postconf -nh "${p}_use_tls" 2>/dev/null)
	enforce=$(postconf -nh "${p}_enforce_tls" 2>/dev/null)
	[ -z "${use}${enforce}" ] && return 0
	level=$(postconf -nh "${p}_tls_security_level" 2>/dev/null)
	if [ -z "${level}" ]; then
		if [ "${enforce,,}" = "yes" ]; then
			level=encrypt
			# client enforce_tls also verified the peer name
			if [ "${p}" != "smtpd" ] && [ "$(postconf -h "${p}_tls_enforce_peername" 2>/dev/null)" != "no" ]; then
				level=verify
			fi
		elif [ "${use,,}" = "yes" ]; then
			level=may
		else
			level=none
		fi
		postconf -e "${p}_tls_security_level=${level}"
	fi
	postconf -X "${p}_use_tls" "${p}_enforce_tls"
	[ "${p}" != "smtpd" ] && postconf -X "${p}_tls_enforce_peername"
}

migrate_obsolete_tls smtpd
migrate_obsolete_tls smtp
migrate_obsolete_tls lmtp

if [ -n "$(postconf -nh disable_dns_lookups 2>/dev/null)" ]; then
	if [ "$(postconf -nh disable_dns_lookups 2>/dev/null)" = "yes" ]; then
		[ -z "$(postconf -nh smtp_dns_support_level 2>/dev/null)" ] && postconf -e smtp_dns_support_level=disabled
		[ -z "$(postconf -nh lmtp_dns_support_level 2>/dev/null)" ] && postconf -e lmtp_dns_support_level=disabled
	fi
	postconf -X disable_dns_lookups
fi

# old master.cf files still use fifo for pickup/qmgr
if grep -qE '^(pickup|qmgr)[[:space:]]+fifo[[:space:]]' /etc/postfix/master.cf; then
	sed -i -E 's/^((pickup|qmgr)[[:space:]]+)fifo([[:space:]])/\1unix\3/' /etc/postfix/master.cf
	if [[ "$(systemctl is-active postfix)" == "active" ]]; then
		postfix reload >/dev/null 2>&1
	fi
fi
