mirror of
https://github.com/docker-mailserver/docker-mailserver.git
synced 2025-08-05 02:27:15 +02:00
Fix/refactor relayhost_map update when dynamically adding account
check-for-changes.sh did not have a special case to handle lines in postfix-relaymap.cf consisting of only a domain (indicating that said domain should never be relayed). This case is handled by start-mailserver.sh so when such a line existed, things would work well until a config file update was detected by check-for-changes.sh. After that, the generated relayhost_map file would be corrupted. Fixed by factoring a 'populate_relayhost_map' function out of start-mailserver.sh and into helper_functions.sh and reusing it in check-for-changes.sh. Note: There are certainly quite a few more pieces of code that could be refactored in a similar fashion. Note2: check-for-changes.sh would previously never update the relayhost_map file when $ENABLE_LDAP was set to 1. I don't think this was intended —there is after all no such condition in start-mailserver.sh— and so this condition no longer applies.
This commit is contained in:
parent
2a70f33a4b
commit
1286a1266b
4 changed files with 123 additions and 116 deletions
|
@ -95,7 +95,6 @@ if ! cmp --silent -- "$CHKSUM_FILE" "$CHKSUM_FILE.new"; then
|
|||
if [ ! -z "$RELAY_HOST" ]; then
|
||||
# keep old config
|
||||
echo -n > /etc/postfix/sasl_passwd
|
||||
echo -n > /etc/postfix/relayhost_map
|
||||
if [ ! -z "$SASL_PASSWD" ]; then
|
||||
echo "$SASL_PASSWD" >> /etc/postfix/sasl_passwd
|
||||
fi
|
||||
|
@ -111,14 +110,6 @@ if ! cmp --silent -- "$CHKSUM_FILE" "$CHKSUM_FILE.new"; then
|
|||
if [ ! -z "$RELAY_USER" ] && [ ! -z "$RELAY_PASSWORD" ]; then
|
||||
echo "[$RELAY_HOST]:$RELAY_PORT $RELAY_USER:$RELAY_PASSWORD" >> /etc/postfix/sasl_passwd
|
||||
fi
|
||||
# add relay maps from file
|
||||
if [ -f /tmp/docker-mailserver/postfix-relaymap.cf ]; then
|
||||
(grep -v "^\s*$\|^\s*\#" /tmp/docker-mailserver/postfix-relaymap.cf || true) | while read line; do
|
||||
if ! echo "$line" | grep -q -e "\s*#"; then
|
||||
echo "$line" >> /etc/postfix/relayhost_map
|
||||
fi
|
||||
done
|
||||
fi
|
||||
fi
|
||||
|
||||
# Creating users
|
||||
|
@ -152,22 +143,15 @@ if ! cmp --silent -- "$CHKSUM_FILE" "$CHKSUM_FILE.new"; then
|
|||
# Copy user provided sieve file, if present
|
||||
test -e /tmp/docker-mailserver/${login}.dovecot.sieve && cp /tmp/docker-mailserver/${login}.dovecot.sieve /var/mail/${domain}/${user}/.dovecot.sieve
|
||||
echo ${domain} >> /tmp/vhost.tmp
|
||||
# add domains to relayhost_map
|
||||
if [ ! -z "$RELAY_HOST" ]; then
|
||||
if ! grep -q -e "^@${domain}\s" /etc/postfix/relayhost_map; then
|
||||
echo "@${domain} [$RELAY_HOST]:$RELAY_PORT" >> /etc/postfix/relayhost_map
|
||||
fi
|
||||
fi
|
||||
done
|
||||
fi
|
||||
if [ ! -z "$RELAY_HOST" ]; then
|
||||
populate_relayhost_map
|
||||
fi
|
||||
if [ -f /etc/postfix/sasl_passwd ]; then
|
||||
chown root:root /etc/postfix/sasl_passwd
|
||||
chmod 0600 /etc/postfix/sasl_passwd
|
||||
fi
|
||||
if [ -f /etc/postfix/relayhost_map ]; then
|
||||
chown root:root /etc/postfix/relayhost_map
|
||||
chmod 0600 /etc/postfix/relayhost_map
|
||||
fi
|
||||
if [ -f postfix-virtual.cf ]; then
|
||||
# regen postfix aliases
|
||||
echo -n > /etc/postfix/virtual
|
||||
|
|
|
@ -74,6 +74,93 @@ for key, value in acme.items():
|
|||
fi
|
||||
}
|
||||
|
||||
declare -A DEFAULT_VARS
|
||||
DEFAULT_VARS["DMS_DEBUG"]="${DMS_DEBUG:="0"}"
|
||||
|
||||
function notify () {
|
||||
c_red="\e[0;31m"
|
||||
c_green="\e[0;32m"
|
||||
c_brown="\e[0;33m"
|
||||
c_blue="\e[0;34m"
|
||||
c_bold="\033[1m"
|
||||
c_reset="\e[0m"
|
||||
|
||||
notification_type=$1
|
||||
notification_msg=$2
|
||||
notification_format=$3
|
||||
msg=""
|
||||
|
||||
case "${notification_type}" in
|
||||
'taskgrp')
|
||||
msg="${c_bold}${notification_msg}${c_reset}"
|
||||
;;
|
||||
'task')
|
||||
if [[ ${DEFAULT_VARS["DMS_DEBUG"]} == 1 ]]; then
|
||||
msg=" ${notification_msg}${c_reset}"
|
||||
fi
|
||||
;;
|
||||
'inf')
|
||||
if [[ ${DEFAULT_VARS["DMS_DEBUG"]} == 1 ]]; then
|
||||
msg="${c_green} * ${notification_msg}${c_reset}"
|
||||
fi
|
||||
;;
|
||||
'started')
|
||||
msg="${c_green} ${notification_msg}${c_reset}"
|
||||
;;
|
||||
'warn')
|
||||
msg="${c_brown} * ${notification_msg}${c_reset}"
|
||||
;;
|
||||
'err')
|
||||
msg="${c_red} * ${notification_msg}${c_reset}"
|
||||
;;
|
||||
'fatal')
|
||||
msg="${c_red}Error: ${notification_msg}${c_reset}"
|
||||
;;
|
||||
*)
|
||||
msg=""
|
||||
;;
|
||||
esac
|
||||
|
||||
case "${notification_format}" in
|
||||
'n')
|
||||
options="-ne"
|
||||
;;
|
||||
*)
|
||||
options="-e"
|
||||
;;
|
||||
esac
|
||||
|
||||
[[ ! -z "${msg}" ]] && echo $options "${msg}"
|
||||
}
|
||||
|
||||
# setup /etc/postfix/relayhost_map
|
||||
# --
|
||||
# @domain1.com [smtp.mailgun.org]:587
|
||||
# @domain2.com [smtp.mailgun.org]:587
|
||||
# @domain3.com [smtp.mailgun.org]:587
|
||||
function populate_relayhost_map() {
|
||||
echo -n > /etc/postfix/relayhost_map
|
||||
chown root:root /etc/postfix/relayhost_map
|
||||
chmod 0600 /etc/postfix/relayhost_map
|
||||
|
||||
if [ -f /tmp/docker-mailserver/postfix-relaymap.cf ]; then
|
||||
notify 'inf' "Adding relay mappings from postfix-relaymap.cf"
|
||||
# Keep lines which are not a comment *and* have a destination.
|
||||
sed -n '/^\s*[^#[:space:]]\S*\s\+\S/p' /tmp/docker-mailserver/postfix-relaymap.cf \
|
||||
>> /etc/postfix/relayhost_map
|
||||
fi
|
||||
# Note: Won't detect domains when lhs has spaces (but who does that?!).
|
||||
sed -n '/^\s*[^#[:space:]]/ s/^[^@|]*@\([^|]\+\)|.*$/\1/p' /tmp/docker-mailserver/postfix-accounts.cf |
|
||||
while read domain; do
|
||||
if ! grep -q -e "^@${domain}\b" /etc/postfix/relayhost_map &&
|
||||
! grep -qs -e "^\s*@${domain}\s*$" /tmp/docker-mailserver/postfix-relaymap.cf; then
|
||||
# Domain not already present *and* not ignored.
|
||||
notify 'inf' "Adding relay mapping for ${domain}"
|
||||
echo "@${domain} [$RELAY_HOST]:$RELAY_PORT" >> /etc/postfix/relayhost_map
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# File storing the checksums of the monitored files.
|
||||
CHKSUM_FILE=/tmp/docker-mailserver-config-chksum
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ DEFAULT_VARS["POSTFIX_MAILBOX_SIZE_LIMIT"]="${POSTFIX_MAILBOX_SIZE_LIMIT:="0"}"
|
|||
DEFAULT_VARS["POSTFIX_INET_PROTOCOLS"]="${POSTFIX_INET_PROTOCOLS:="all"}"
|
||||
DEFAULT_VARS["ENABLE_SASLAUTHD"]="${ENABLE_SASLAUTHD:="0"}"
|
||||
DEFAULT_VARS["SMTP_ONLY"]="${SMTP_ONLY:="0"}"
|
||||
DEFAULT_VARS["DMS_DEBUG"]="${DMS_DEBUG:="0"}"
|
||||
# DEFAULT_VARS["DMS_DEBUG"] defined in helper_functions.sh
|
||||
DEFAULT_VARS["OVERRIDE_HOSTNAME"]="${OVERRIDE_HOSTNAME}"
|
||||
DEFAULT_VARS["POSTSCREEN_ACTION"]="${POSTSCREEN_ACTION:="enforce"}"
|
||||
DEFAULT_VARS["SPOOF_PROTECTION"]="${SPOOF_PROTECTION:="0"}"
|
||||
|
@ -309,62 +309,6 @@ function _register_misc_function() {
|
|||
##########################################################################
|
||||
|
||||
|
||||
function notify () {
|
||||
c_red="\e[0;31m"
|
||||
c_green="\e[0;32m"
|
||||
c_brown="\e[0;33m"
|
||||
c_blue="\e[0;34m"
|
||||
c_bold="\033[1m"
|
||||
c_reset="\e[0m"
|
||||
|
||||
notification_type=$1
|
||||
notification_msg=$2
|
||||
notification_format=$3
|
||||
msg=""
|
||||
|
||||
case "${notification_type}" in
|
||||
'taskgrp')
|
||||
msg="${c_bold}${notification_msg}${c_reset}"
|
||||
;;
|
||||
'task')
|
||||
if [[ ${DEFAULT_VARS["DMS_DEBUG"]} == 1 ]]; then
|
||||
msg=" ${notification_msg}${c_reset}"
|
||||
fi
|
||||
;;
|
||||
'inf')
|
||||
if [[ ${DEFAULT_VARS["DMS_DEBUG"]} == 1 ]]; then
|
||||
msg="${c_green} * ${notification_msg}${c_reset}"
|
||||
fi
|
||||
;;
|
||||
'started')
|
||||
msg="${c_green} ${notification_msg}${c_reset}"
|
||||
;;
|
||||
'warn')
|
||||
msg="${c_brown} * ${notification_msg}${c_reset}"
|
||||
;;
|
||||
'err')
|
||||
msg="${c_red} * ${notification_msg}${c_reset}"
|
||||
;;
|
||||
'fatal')
|
||||
msg="${c_red}Error: ${notification_msg}${c_reset}"
|
||||
;;
|
||||
*)
|
||||
msg=""
|
||||
;;
|
||||
esac
|
||||
|
||||
case "${notification_format}" in
|
||||
'n')
|
||||
options="-ne"
|
||||
;;
|
||||
*)
|
||||
options="-e"
|
||||
;;
|
||||
esac
|
||||
|
||||
[[ ! -z "${msg}" ]] && echo $options "${msg}"
|
||||
}
|
||||
|
||||
function defunc() {
|
||||
notify 'fatal' "Please fix your configuration. Exiting..."
|
||||
exit 1
|
||||
|
@ -1339,36 +1283,7 @@ function _setup_postfix_relay_hosts() {
|
|||
fi
|
||||
# end /etc/postfix/sasl_passwd
|
||||
|
||||
# setup /etc/postfix/relayhost_map
|
||||
# --
|
||||
# @domain1.com [smtp.mailgun.org]:587
|
||||
# @domain2.com [smtp.mailgun.org]:587
|
||||
# @domain3.com [smtp.mailgun.org]:587
|
||||
|
||||
echo -n > /etc/postfix/relayhost_map
|
||||
|
||||
if [ -f /tmp/docker-mailserver/postfix-relaymap.cf ]; then
|
||||
notify 'inf' "Adding relay mappings from postfix-relaymap.cf"
|
||||
while read line; do
|
||||
if ! echo "$line" | grep -q -e "\s*#"; then
|
||||
echo "$line" >> /etc/postfix/relayhost_map
|
||||
fi
|
||||
done < /tmp/docker-mailserver/postfix-relaymap.cf
|
||||
fi
|
||||
grep -v "^\s*$\|^\s*\#" /tmp/docker-mailserver/postfix-accounts.cf | while IFS=$'|' read login pass
|
||||
do
|
||||
domain=$(echo ${login} | cut -d @ -f2)
|
||||
if ! grep -q -e "^@${domain}\b" /etc/postfix/relayhost_map; then
|
||||
notify 'inf' "Adding relay mapping for ${domain}"
|
||||
echo "@${domain} [$RELAY_HOST]:$RELAY_PORT" >> /etc/postfix/relayhost_map
|
||||
fi
|
||||
done
|
||||
# remove lines with no destination
|
||||
sed -i '/^@\S*\s*$/d' /etc/postfix/relayhost_map
|
||||
|
||||
chown root:root /etc/postfix/relayhost_map
|
||||
chmod 0600 /etc/postfix/relayhost_map
|
||||
# end /etc/postfix/relayhost_map
|
||||
populate_relayhost_map
|
||||
|
||||
postconf -e \
|
||||
"smtp_sasl_auth_enable = yes" \
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue