From 30ed8fbf0ebd85e2787c30ab59f45a271e90d26f Mon Sep 17 00:00:00 2001 From: Peter Hartmann <20216585+peter-hartmann@users.noreply.github.com> Date: Thu, 1 Nov 2018 14:17:07 -0500 Subject: [PATCH] Configuration support for /etc/aliases(Closes: #988, #1065) * Update check-for-changes.sh * add postfix-aliases.cf and handling of runtime updates --- config/postfix-aliases.cf | 0 target/check-for-changes.sh | 69 +++++++++++++++++++------------------ target/start-mailserver.sh | 12 +++++-- 3 files changed, 44 insertions(+), 37 deletions(-) create mode 100644 config/postfix-aliases.cf diff --git a/config/postfix-aliases.cf b/config/postfix-aliases.cf new file mode 100644 index 00000000..e69de29b diff --git a/target/check-for-changes.sh b/target/check-for-changes.sh index 23533a55..388f2cb3 100755 --- a/target/check-for-changes.sh +++ b/target/check-for-changes.sh @@ -15,13 +15,16 @@ if [ ! -f postfix-accounts.cf ]; then exit fi +# create an array of files to monitor (perhaps simple *.cf would be ok here) +declare -a cf_files=() +for file in postfix-accounts.cf postfix-virtual.cf postfix-aliases.cf; do + [ -f "$file" ] && cf_files+=("$file") +done + # Update / generate after start echo "${log_date} Makeing new checksum file." -if [ -f postfix-virtual.cf ]; then - sha512sum --tag postfix-accounts.cf --tag postfix-virtual.cf > chksum -else - sha512sum --tag postfix-accounts.cf > chksum -fi +sha512sum ${cf_files[@]/#/--tag } > chksum + # Run forever while true; do @@ -30,16 +33,18 @@ log_date=$(date +"%Y-%m-%d %H:%M:%S ") # Get chksum and check it. chksum=$(sha512sum -c --ignore-missing chksum) -resu_acc=${chksum:21:2} -if [ -f postfix-virtual.cf ]; then - resu_vir=${chksum:44:2} -else - resu_vir="OK" -fi -if ! [ $resu_acc = "OK" ] || ! [ $resu_vir = "OK" ]; then - echo "${log_date} Change detected" - #regen postfix accounts. +if [[ $chksum == *"FAIL"* ]]; then + echo "${log_date} Change detected" + + #regen postix aliases. + echo "root: ${POSTMASTER_ADDRESS}" > /etc/aliases + if [ -f /tmp/docker-mailserver/postfix-aliases.cf ]; then + cat /tmp/docker-mailserver/postfix-aliases.cf>>/etc/aliases + fi + postalias /etc/aliases + + #regen postfix accounts. echo -n > /etc/postfix/vmailbox echo -n > /etc/dovecot/userdb if [ -f /tmp/docker-mailserver/postfix-accounts.cf -a "$ENABLE_LDAP" != 1 ]; then @@ -126,8 +131,8 @@ if ! [ $resu_acc = "OK" ] || ! [ $resu_vir = "OK" ]; then chmod 0600 /etc/postfix/relayhost_map fi if [ -f postfix-virtual.cf ]; then - # regen postfix aliases - echo -n > /etc/postfix/virtual + # regen postfix aliases + echo -n > /etc/postfix/virtual echo -n > /etc/postfix/regexp if [ -f /tmp/docker-mailserver/postfix-virtual.cf ]; then # Copying virtual file @@ -137,7 +142,7 @@ if ! [ $resu_acc = "OK" ] || ! [ $resu_vir = "OK" ]; then # Setting variables for better readability uname=$(echo ${from} | cut -d @ -f1) domain=$(echo ${from} | cut -d @ -f2) - # if they are equal it means the line looks like: "user1 other@domain.tld" + # if they are equal it means the line looks like: "user1 other@domain.tld" test "$uname" != "$domain" && echo ${domain} >> /tmp/vhost.tmp done < /tmp/docker-mailserver/postfix-virtual.cf fi @@ -150,30 +155,26 @@ if ! [ $resu_acc = "OK" ] || ! [ $resu_vir = "OK" ]; then }' /etc/postfix/main.cf fi fi - # Set vhost + # Set vhost if [ -f /tmp/vhost.tmp ]; then cat /tmp/vhost.tmp | sort | uniq > /etc/postfix/vhost && rm /tmp/vhost.tmp fi - - # Set right new if needed + + # Set right new if needed if [ `find /var/mail -maxdepth 3 -a \( \! -user 5000 -o \! -group 5000 \) | grep -c .` != 0 ]; then chown -R 5000:5000 /var/mail fi - - # Restart of the postfix - supervisorctl restart postfix - - # Prevent restart of dovecot when smtp_only=1 - if [ ! $SMTP_ONLY = 1 ]; then - supervisorctl restart dovecot - fi + + # Restart of the postfix + supervisorctl restart postfix + + # Prevent restart of dovecot when smtp_only=1 + if [ ! $SMTP_ONLY = 1 ]; then + supervisorctl restart dovecot + fi - echo "${log_date} Update checksum" - if [ -f postfix-virtual.cf ]; then - sha512sum --tag postfix-accounts.cf --tag postfix-virtual.cf > chksum - else - sha512sum --tag postfix-accounts.cf > chksum - fi + echo "${log_date} Update checksum" + sha512sum ${cf_files[@]/#/--tag } > chksum fi sleep 1 diff --git a/target/start-mailserver.sh b/target/start-mailserver.sh index 3c97ef83..2e8d71f0 100644 --- a/target/start-mailserver.sh +++ b/target/start-mailserver.sh @@ -751,7 +751,7 @@ function _setup_postfix_aliases() { echo -n > /etc/postfix/virtual echo -n > /etc/postfix/regexp if [ -f /tmp/docker-mailserver/postfix-virtual.cf ]; then - # fixing old virtual user file + # fixing old virtual user file [[ $(grep ",$" /tmp/docker-mailserver/postfix-virtual.cf) ]] && sed -i -e "s/, /,/g" -e "s/,$//g" /tmp/docker-mailserver/postfix-virtual.cf # Copying virtual file cp -f /tmp/docker-mailserver/postfix-virtual.cf /etc/postfix/virtual @@ -778,6 +778,12 @@ function _setup_postfix_aliases() { notify 'inf' "Configuring root alias" echo "root: ${POSTMASTER_ADDRESS}" > /etc/aliases + if [ -f /tmp/docker-mailserver/postfix-aliases.cf ]; then + cat /tmp/docker-mailserver/postfix-aliases.cf>>/etc/aliases + else + notify 'inf' "'config/postfix-aliases.cf' is not provided and will be auto created." + echo -n >/tmp/docker-mailserver/postfix-aliases.cf + fi postalias /etc/aliases } @@ -805,8 +811,8 @@ function _setup_dkim() { else notify 'warn' "No DKIM key provided. Check the documentation to find how to get your keys." - local _f_keytable="/etc/opendkim/KeyTable" - [ ! -f "$_f_keytable" ] && touch "$_f_keytable" + local _f_keytable="/etc/opendkim/KeyTable" + [ ! -f "$_f_keytable" ] && touch "$_f_keytable" fi }