From d0f72573337479d0b2cc9866be1e193522944673 Mon Sep 17 00:00:00 2001 From: youtous Date: Wed, 6 May 2020 19:26:47 +0200 Subject: [PATCH] support comments in .cf files --- Makefile | 2 ++ target/bin/generate-dkim-config | 8 ++++---- target/bin/listalias | 2 +- target/bin/listmailuser | 2 +- target/check-for-changes.sh | 22 +++++++++++----------- target/start-mailserver.sh | 4 ++-- test/config/postfix-master.cf | 2 ++ test/config/postfix-regexp.cf | 2 ++ test/config/postfix-virtual.cf | 2 ++ 9 files changed, 27 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index dcd0e122..0bdfe785 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,8 @@ backup: generate-accounts: docker run --rm -e MAIL_USER=user1@localhost.localdomain -e MAIL_PASS=mypassword -t $(NAME) /bin/sh -c 'echo "$$MAIL_USER|$$(doveadm pw -s SHA512-CRYPT -u $$MAIL_USER -p $$MAIL_PASS)"' > test/config/postfix-accounts.cf docker run --rm -e MAIL_USER=user2@otherdomain.tld -e MAIL_PASS=mypassword -t $(NAME) /bin/sh -c 'echo "$$MAIL_USER|$$(doveadm pw -s SHA512-CRYPT -u $$MAIL_USER -p $$MAIL_PASS)"' >> test/config/postfix-accounts.cf + echo "# this is a test comment, please don't delete me :'(" >> test/config/postfix-accounts.cf + echo " # this is also a test comment, :O" >> test/config/postfix-accounts.cf run: # Run containers diff --git a/target/bin/generate-dkim-config b/target/bin/generate-dkim-config index 9e52f017..d27d7a55 100755 --- a/target/bin/generate-dkim-config +++ b/target/bin/generate-dkim-config @@ -7,23 +7,23 @@ keysize=${1:-2048} # Getting domains from mail accounts if [ -f /tmp/docker-mailserver/postfix-accounts.cf ]; then - while IFS=$'|' read login pass + (grep -v "^\s*$\|^\s*\#" /tmp/docker-mailserver/postfix-accounts.cf || true) | while IFS=$'|' read login pass do domain=$(echo ${login} | cut -d @ -f2) echo ${domain} >> /tmp/vhost.tmp - done < /tmp/docker-mailserver/postfix-accounts.cf + done fi # Getting domains from mail aliases if [ -f /tmp/docker-mailserver/postfix-virtual.cf ]; then - while read from to + (grep -v "^\s*$\|^\s*\#" /tmp/docker-mailserver/postfix-virtual.cf || true) | while read from to do # 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" test "$uname" != "$domain" && echo ${domain} >> /tmp/vhost.tmp - done < /tmp/docker-mailserver/postfix-virtual.cf + done fi # Keeping unique entries diff --git a/target/bin/listalias b/target/bin/listalias index 4db49e85..8e655bbe 100755 --- a/target/bin/listalias +++ b/target/bin/listalias @@ -10,4 +10,4 @@ errex() { [ -f $DATABASE ] || errex "No postfix-virtual.cf file" [ -s $DATABASE ] || errex "Empty postfix-virtual.cf - no aliases have been added" -cat $DATABASE +(grep -v "^\s*$\|^\s*\#" $DATABASE || true) diff --git a/target/bin/listmailuser b/target/bin/listmailuser index 658c52eb..be6a4935 100755 --- a/target/bin/listmailuser +++ b/target/bin/listmailuser @@ -13,6 +13,6 @@ errex() { # Lock database even though we are only reading ( flock -e 200 - awk -F '|' '{ print $1; }' $DATABASE + (grep -v "^\s*$\|^\s*\#" "$DATABASE" || true) | awk -F '|' '{ print $1; }' ) 200<$DATABASE diff --git a/target/check-for-changes.sh b/target/check-for-changes.sh index f11f8f7d..804d7070 100755 --- a/target/check-for-changes.sh +++ b/target/check-for-changes.sh @@ -91,11 +91,11 @@ if [[ $chksum == *"FAIL"* ]]; then fi # add domain-specific auth from config file if [ -f /tmp/docker-mailserver/postfix-sasl-password.cf ]; then - while read line; do + (grep -v "^\s*$\|^\s*\#" /tmp/docker-mailserver/postfix-sasl-password.cf || true) | while read line; do if ! echo "$line" | grep -q -e "\s*#"; then echo "$line" >> /etc/postfix/sasl_passwd fi - done < /tmp/docker-mailserver/postfix-sasl-password.cf + done fi # add default relay if [ ! -z "$RELAY_USER" ] && [ ! -z "$RELAY_PASSWORD" ]; then @@ -103,11 +103,11 @@ if [[ $chksum == *"FAIL"* ]]; then fi # add relay maps from file if [ -f /tmp/docker-mailserver/postfix-relaymap.cf ]; then - while read line; do + (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 < /tmp/docker-mailserver/postfix-relaymap.cf + done fi fi @@ -165,14 +165,14 @@ if [[ $chksum == *"FAIL"* ]]; then if [ -f /tmp/docker-mailserver/postfix-virtual.cf ]; then # Copying virtual file cp -f /tmp/docker-mailserver/postfix-virtual.cf /etc/postfix/virtual - while read from to + (grep -v "^\s*$\|^\s*\#" /tmp/docker-mailserver/postfix-virtual.cf || true) | while read from to do # 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" test "$uname" != "$domain" && echo ${domain} >> /tmp/vhost.tmp - done < /tmp/docker-mailserver/postfix-virtual.cf + done fi if [ -f /tmp/docker-mailserver/postfix-regexp.cf ]; then # Copying regexp alias file @@ -183,23 +183,23 @@ if [[ $chksum == *"FAIL"* ]]; 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 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 + fi echo "${log_date} Update checksum" sha512sum ${cf_files[@]/#/--tag } >$CHKSUM_FILE diff --git a/target/start-mailserver.sh b/target/start-mailserver.sh index a1f75abc..8178844d 100644 --- a/target/start-mailserver.sh +++ b/target/start-mailserver.sh @@ -940,14 +940,14 @@ function _setup_postfix_aliases() { [[ $(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 - while read from to + (grep -v "^\s*$\|^\s*\#" /tmp/docker-mailserver/postfix-virtual.cf || true) | while read from to do # 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" test "$uname" != "$domain" && echo ${domain} >> /tmp/vhost.tmp - done < /tmp/docker-mailserver/postfix-virtual.cf + done else notify 'inf' "Warning 'config/postfix-virtual.cf' is not provided. No mail alias/forward created." fi diff --git a/test/config/postfix-master.cf b/test/config/postfix-master.cf index a6c1d80d..4ebc7d21 100644 --- a/test/config/postfix-master.cf +++ b/test/config/postfix-master.cf @@ -1 +1,3 @@ submission/inet/smtpd_sasl_security_options=noanonymous +# this is a test comment, please don't delete me :'( + # this is also a test comment, :O diff --git a/test/config/postfix-regexp.cf b/test/config/postfix-regexp.cf index 27954d6f..6bbb6fa7 100644 --- a/test/config/postfix-regexp.cf +++ b/test/config/postfix-regexp.cf @@ -1,4 +1,6 @@ /^test[0-9][0-9]*@localhost.localdomain/ user1@localhost.localdomain +# this is a test comment, please don't delete me :'( + # this is also a test comment, :O /^bounce.*@.*/ external1@otherdomain.tld /^postmaster@/ user1@localhost.localdomain diff --git a/test/config/postfix-virtual.cf b/test/config/postfix-virtual.cf index 57d8158d..67a58cff 100644 --- a/test/config/postfix-virtual.cf +++ b/test/config/postfix-virtual.cf @@ -1,3 +1,5 @@ alias1@localhost.localdomain user1@localhost.localdomain +# this is a test comment, please don't delete me :'( + # this is also a test comment, :O alias2@localhost.localdomain external1@otherdomain.tld @localdomain2.com user1@localhost.localdomain