From 7e4e3662b35f30fb09c3deeaf9c8dedf3e821c20 Mon Sep 17 00:00:00 2001 From: Florian Date: Mon, 13 Feb 2017 11:07:30 +0100 Subject: [PATCH] Hostname override fixes for docker option --net=host in conjunction with OVERRIDE_HOSTNAME (#517) * Fixed wrong mail headers when using OVERRIDE_HOSTNAME by setting the the hostname explicitly * Added tests and fixed hostname in dovecot conf * Added missing tests * Improved function naming and task notification message --- Makefile | 13 ++++++++++++- target/start-mailserver.sh | 26 ++++++++++++++++++++++++++ test/tests.bats | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index c77d2067..8eece1f9 100644 --- a/Makefile +++ b/Makefile @@ -51,6 +51,14 @@ run: -e OVERRIDE_HOSTNAME=mail.my-domain.com \ -t $(NAME) sleep 15 + docker run -d --name mail_override_hostname \ + -v "`pwd`/test/config":/tmp/docker-mailserver \ + -v "`pwd`/test":/tmp/docker-mailserver-test \ + -e PERMIT_DOCKER=network \ + -e OVERRIDE_HOSTNAME=mail.my-domain.com \ + -h unknown.domain.tld \ + -t $(NAME) + sleep 15 docker run -d --name mail_fail2ban \ -v "`pwd`/test/config":/tmp/docker-mailserver \ -v "`pwd`/test":/tmp/docker-mailserver-test \ @@ -151,6 +159,8 @@ fixtures: docker exec mail_disabled_clamav_spamassassin /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user.txt" # postfix virtual transport lmtp docker exec mail_lmtp_ip /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user.txt" + + docker exec mail_override_hostname /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user.txt" # Wait for mails to be analyzed sleep 20 @@ -173,7 +183,8 @@ clean: mail_with_ldap \ mail_with_imap \ mail_lmtp_ip \ - mail_with_postgrey + mail_with_postgrey \ + mail_override_hostname @if [ -f config/postfix-accounts.cf.bak ]; then\ rm -f config/postfix-accounts.cf ;\ diff --git a/target/start-mailserver.sh b/target/start-mailserver.sh index 4b6ae0b3..cfe6efbf 100644 --- a/target/start-mailserver.sh +++ b/target/start-mailserver.sh @@ -104,6 +104,9 @@ function register_functions() { _register_setup_function "_setup_mailname" _register_setup_function "_setup_amavis" + _register_setup_function "_setup_dmarc_hostname" + _register_setup_function "_setup_postfix_hostname" + _register_setup_function "_setup_dovecot_hostname" _register_setup_function "_setup_postfix_override_configuration" _register_setup_function "_setup_postfix_sasl_password" @@ -395,6 +398,29 @@ function _setup_amavis() { sed -i 's/^#\$myhostname = "mail.example.com";/\$myhostname = "'$HOSTNAME'";/' /etc/amavis/conf.d/05-node_id } +function _setup_dmarc_hostname() { + notify 'task' 'Setting up dmarc' + + notify 'inf' "Applying hostname to /etc/opendmarc.conf" + sed -i -e 's/^AuthservID.*$/AuthservID '$HOSTNAME'/g' \ + -e 's/^TrustedAuthservIDs.*$/TrustedAuthservIDs '$HOSTNAME'/g' /etc/opendmarc.conf +} + +function _setup_postfix_hostname() { + notify 'task' 'Applying hostname and domainname to Postfix' + + notify 'inf' "Applying hostname to /etc/postfix/main.cf" + postconf -e "myhostname = $HOSTNAME" + postconf -e "mydomain = $DOMAINNAME" +} + +function _setup_dovecot_hostname() { + notify 'task' 'Applying hostname to Dovecot' + + notify 'inf' "Applying hostname to /etc/dovecot/conf.d/15-lda.conf" + sed -i 's/^#hostname =.*$/hostname = '$HOSTNAME'/g' /etc/dovecot/conf.d/15-lda.conf +} + function _setup_dovecot() { notify 'task' 'Setting up Dovecot' diff --git a/test/tests.bats b/test/tests.bats index 2706da59..1cd3538d 100644 --- a/test/tests.bats +++ b/test/tests.bats @@ -14,6 +14,44 @@ load 'test_helper/bats-assert/load' assert_success } +@test "checking configuration: hostname/domainname override: check container hostname is applied correctly" { + run docker exec mail_override_hostname /bin/bash -c "hostname | grep unknown.domain.tld" + assert_success +} + +@test "checking configuration: hostname/domainname override: check overriden hostname is applied to all configs" { + run docker exec mail_override_hostname /bin/bash -c "cat /etc/mailname | grep my-domain.com" + assert_success + run docker exec mail_override_hostname /bin/bash -c "postconf -n | grep mydomain | grep my-domain.com" + assert_success + run docker exec mail_override_hostname /bin/bash -c "postconf -n | grep myhostname | grep mail.my-domain.com" + assert_success + run docker exec mail_override_hostname /bin/bash -c "doveconf | grep hostname | grep mail.my-domain.com" + assert_success + run docker exec mail_override_hostname /bin/bash -c "cat /etc/opendmarc.conf | grep AuthservID | grep mail.my-domain.com" + assert_success + run docker exec mail_override_hostname /bin/bash -c "cat /etc/opendmarc.conf | grep TrustedAuthservIDs | grep mail.my-domain.com" + assert_success + run docker exec mail_override_hostname /bin/bash -c "cat /etc/amavis/conf.d/05-node_id | grep myhostname | grep mail.my-domain.com" + assert_success +} + +@test "checking configuration: hostname/domainname override: check hostname in postfix HELO message" { + run docker exec mail_override_hostname /bin/bash -c "nc -w 1 0.0.0.0 25 | grep mail.my-domain.com" + assert_success +} + +@test "checking configuration: hostname/domainname override: check headers of received mail" { + run docker exec mail_override_hostname /bin/sh -c "ls -A /var/mail/localhost.localdomain/user1/new | wc -l | grep 1" + assert_success + run docker exec mail_override_hostname /bin/sh -c "cat /var/mail/localhost.localdomain/user1/new/* | grep mail.my-domain.com" + assert_success + + # test whether the container hostname is not found in received mail + run docker exec mail_override_hostname /bin/sh -c "cat /var/mail/localhost.localdomain/user1/new/* | grep unknown.domain.tld" + assert_failure +} + # # processes #