From 42352a32592a27d4df2154558866654c987b808d Mon Sep 17 00:00:00 2001 From: mwnx Date: Mon, 24 Aug 2020 22:53:54 +0200 Subject: [PATCH] Update relayhost_map with virtual accounts too Previously, only postfix-relaymap.cf and postfix-accounts.cf would be used to populate the relayhost_map file. Now, also use postfix-virtual.cf when present. To me, there is nothing absurd about sending mail "From:" a virtual account (or more specifically its domain) so it makes sense that when a $RELAY_HOST is defined it should be used for virtual accounts as well. --- target/check-for-changes.sh | 1 + target/helper_functions.sh | 9 ++++++--- test/config/relay-hosts/postfix-virtual.cf | 1 + test/mail_with_relays.bats | 17 +++++++++++++++++ 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 test/config/relay-hosts/postfix-virtual.cf diff --git a/target/check-for-changes.sh b/target/check-for-changes.sh index 3ff8ffa1..c04ca6e0 100755 --- a/target/check-for-changes.sh +++ b/target/check-for-changes.sh @@ -81,6 +81,7 @@ if ! cmp --silent -- "$CHKSUM_FILE" "$CHKSUM_FILE.new"; then #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 sed -i 's/\r//g' /tmp/docker-mailserver/postfix-accounts.cf echo "# WARNING: this file is auto-generated. Modify config/postfix-accounts.cf to edit user list." > /etc/postfix/vmailbox diff --git a/target/helper_functions.sh b/target/helper_functions.sh index 50f829f5..bfb50c1a 100644 --- a/target/helper_functions.sh +++ b/target/helper_functions.sh @@ -149,9 +149,12 @@ function populate_relayhost_map() { 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 + { + # 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 + [ -f /tmp/docker-mailserver/postfix-virtual.cf ] && + sed -n '/^\s*[^#[:space:]]/ s/^\s*[^@[:space:]]*@\(\S\+\)\s.*/\1/p' /tmp/docker-mailserver/postfix-virtual.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. diff --git a/test/config/relay-hosts/postfix-virtual.cf b/test/config/relay-hosts/postfix-virtual.cf new file mode 100644 index 00000000..e428c154 --- /dev/null +++ b/test/config/relay-hosts/postfix-virtual.cf @@ -0,0 +1 @@ +@domain1.tld user1@domainone.tld diff --git a/test/mail_with_relays.bats b/test/mail_with_relays.bats index f57b749c..5ae3e82d 100644 --- a/test/mail_with_relays.bats +++ b/test/mail_with_relays.bats @@ -42,6 +42,11 @@ function teardown_file() { assert_output -e '^@domainone.tld\s+\[default.relay.com\]:2525$' } +@test "checking relay hosts: default mapping is added from env vars for virtual user entry" { + run docker exec mail_with_relays grep -e domain1.tld /etc/postfix/relayhost_map + assert_output -e '^@domain1.tld\s+\[default.relay.com\]:2525$' +} + @test "checking relay hosts: default mapping is added from env vars for new user entry" { run docker exec mail_with_relays grep -e domainzero.tld /etc/postfix/relayhost_map assert_output '' @@ -54,6 +59,18 @@ function teardown_file() { assert_output -e '^@domainzero.tld\s+\[default.relay.com\]:2525$' } +@test "checking relay hosts: default mapping is added from env vars for new virtual user entry" { + run docker exec mail_with_relays grep -e domain2.tld /etc/postfix/relayhost_map + assert_output '' + run ./setup.sh -c mail_with_relays alias add user2@domain2.tld user2@domaintwo.tld + for i in {1..10}; do + sleep 1 + run docker exec mail_with_relays grep -e domain2.tld /etc/postfix/relayhost_map + [[ $status == 0 ]] && break + done + assert_output -e '^@domain2.tld\s+\[default.relay.com\]:2525$' +} + @test "checking relay hosts: custom mapping is added from file" { run docker exec mail_with_relays grep -e domaintwo.tld /etc/postfix/relayhost_map assert_output -e '^@domaintwo.tld\s+\[other.relay.com\]:587$'