From 2afc3e11c2a2981c576dbb64048052f006c9863d Mon Sep 17 00:00:00 2001 From: alinmear Date: Fri, 30 Jun 2017 11:34:54 +0200 Subject: [PATCH 1/5] Fix Dovecot Ldap Problems --- target/start-mailserver.sh | 43 ++++++++++++++++---------------------- test/tests.bats | 35 +++++++++++++++++++------------ 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/target/start-mailserver.sh b/target/start-mailserver.sh index 56a7ac6c..78e42cc4 100644 --- a/target/start-mailserver.sh +++ b/target/start-mailserver.sh @@ -22,6 +22,15 @@ DEFAULT_VARS["ENABLE_SASLAUTHD"]="${ENABLE_SASLAUTHD:="0"}" DEFAULT_VARS["SMTP_ONLY"]="${SMTP_ONLY:="0"}" DEFAULT_VARS["DMS_DEBUG"]="${DMS_DEBUG:="0"}" DEFAULT_VARS["OVERRIDE_HOSTNAME"]="${OVERRIDE_HOSTNAME}" + +# DOVECOT LDAP +DEFAULT_VARS["DOVECOT_BASE"]="${DOVECOT_BASE:="${LDAP_SEARCH_BASE}"}" +DEFAULT_VARS["DOVECOT_DN"]="${DOVECOT_DN:="${LDAP_BIND_DN}"}" +DEFAULT_VARS["DOVECOT_DNPASS"]="${DOVECOT_DNPASS:="${LDAP_BIND_PW}"}" +DEFAULT_VARS["DOVECOT_HOSTS"]="${DOVECOT_HOSTS:="${LDAP_SERVER_HOST}"}" +DEFAULT_VARS["DOVECOT_PASS_FILTER"]="${DOVECOT_PASS_FILTER}="${LDAP_QUERY_FILTER}"}" +DEFAULT_VARS["DOVECOT_USER_FILTER"]="${DOVECOT_USER_FILTER}="${LDAP_QUERY_FILTER}"}" + ########################################################################## # << DEFAULT VARS ########################################################################## @@ -324,7 +333,7 @@ function override_config() { _env_variable_prefix=$1 [ -z ${_env_variable_prefix} ] && return 1 - + IFS=" " read -r -a _config_files <<< $2 # dispatch env variables @@ -347,12 +356,12 @@ function override_config() { for f in "${_config_files[@]}" do if [ ! -f "${f}" ];then - echo "Can not find ${f}. Skipping override" + echo "Can not find ${f}. Skipping override" else - for key in ${!config_overrides[@]} + for key in ${!config_overrides[@]} do [ -z $key ] && echo -e "\t no key provided" && return 1 - + sed -i -e "s|^${key}[[:space:]]\+.*|${key} = "${config_overrides[$key]}'|g' \ ${f} done @@ -569,21 +578,16 @@ function _setup_ldap() { for i in 'users' 'groups' 'aliases'; do fpath="/tmp/docker-mailserver/ldap-${i}.cf" if [ -f $fpath ]; then - cp ${fpath} /etc/postfix/ldap-${i}.cf + cp ${fpath} /etc/postfix/ldap-${i}.cf fi done notify 'inf' 'Starting to override configs' override_config "LDAP_" "/etc/postfix/ldap-users.cf /etc/postfix/ldap-groups.cf /etc/postfix/ldap-aliases.cf" - # @TODO: Environment Variables for DOVECOT ldap integration to configure for better control - notify 'inf' "Configuring dovecot LDAP authentification" - sed -i -e 's|^hosts.*|hosts = '${LDAP_SERVER_HOST:="mail.domain.com"}'|g' \ - -e 's|^base.*|base = '${LDAP_SEARCH_BASE:="ou=people,dc=domain,dc=com"}'|g' \ - -e 's|^dn\s*=.*|dn = '${LDAP_BIND_DN:="cn=admin,dc=domain,dc=com"}'|g' \ - -e 's|^dnpass\s*=.*|dnpass = '${LDAP_BIND_PW:="admin"}'|g' \ - /etc/dovecot/dovecot-ldap.conf.ext - + notify 'inf' "Configuring dovecot LDAP" + override_config "DOVECOT_" "/etc/dovecot/dovecot-ldap.conf.ext" + # Add domainname to vhost. echo $DOMAINNAME >> /tmp/vhost.tmp @@ -629,7 +633,7 @@ EOF # cyrus sasl or dovecot sasl if [[ ${ENABLE_SASLAUTHD} == 1 ]] || [[ ${SMTP_ONLY} == 0 ]];then sed -i -e 's|^smtpd_sasl_auth_enable[[:space:]]\+.*|smtpd_sasl_auth_enable = yes|g' /etc/postfix/main.cf - else + else sed -i -e 's|^smtpd_sasl_auth_enable[[:space:]]\+.*|smtpd_sasl_auth_enable = no|g' /etc/postfix/main.cf fi @@ -904,16 +908,6 @@ function _setup_postfix_override_configuration() { else notify 'inf' "No extra postfix settings loaded because optional '/tmp/docker-mailserver/postfix-main.cf' not provided." fi - if [ -f /tmp/docker-mailserver/postfix-master.cf ]; then - while read line; do - if [[ "$line" =~ ^[a-z] ]]; then - postconf -P "$line" - fi - done < /tmp/docker-mailserver/postfix-master.cf - notify 'inf' "Loaded 'config/postfix-master.cf'" - else - notify 'inf' "No extra postfix settings loaded because optional '/tmp/docker-mailserver/postfix-master.cf' not provided." - fi } function _setup_postfix_sasl_password() { @@ -970,7 +964,6 @@ function _setup_security_stack() { SA_TAG=${SA_TAG:="2.0"} && sed -i -r 's/^\$sa_tag_level_deflt (.*);/\$sa_tag_level_deflt = '$SA_TAG';/g' /etc/amavis/conf.d/20-debian_defaults SA_TAG2=${SA_TAG2:="6.31"} && sed -i -r 's/^\$sa_tag2_level_deflt (.*);/\$sa_tag2_level_deflt = '$SA_TAG2';/g' /etc/amavis/conf.d/20-debian_defaults SA_KILL=${SA_KILL:="6.31"} && sed -i -r 's/^\$sa_kill_level_deflt (.*);/\$sa_kill_level_deflt = '$SA_KILL';/g' /etc/amavis/conf.d/20-debian_defaults - SA_SPAM_SUBJECT=${SA_SPAM_SUBJECT:="***SPAM*** "} && sed -i -r 's/^\$sa_spam_subject_tag (.*);/\$sa_spam_subject_tag = '"'$SA_SPAM_SUBJECT'"';/g' /etc/amavis/conf.d/20-debian_defaults test -e /tmp/docker-mailserver/spamassassin-rules.cf && cp /tmp/docker-mailserver/spamassassin-rules.cf /etc/spamassassin/ fi diff --git a/test/tests.bats b/test/tests.bats index c27872b0..bd991261 100644 --- a/test/tests.bats +++ b/test/tests.bats @@ -341,7 +341,7 @@ load 'test_helper/bats-assert/load' run docker exec mail_smtponly /bin/sh -c 'grep -cE "to=.*status\=sent" /var/log/mail/mail.log' [ "$status" -ge 0 ] } - + # @@ -1051,34 +1051,34 @@ load 'test_helper/bats-assert/load' } @test "checking postfix: ldap custom config files copied" { - run docker exec mail_with_ldap /bin/sh -c "grep '# Testconfig for ldap integration' /etc/postfix/ldap-users.cf" + run docker exec mail_with_ldap /bin/sh -c "grep '# Testconfig for ldap integration' /etc/postfix/ldap-users.cf" assert_success - run docker exec mail_with_ldap /bin/sh -c "grep '# Testconfig for ldap integration' /etc/postfix/ldap-groups.cf" + run docker exec mail_with_ldap /bin/sh -c "grep '# Testconfig for ldap integration' /etc/postfix/ldap-groups.cf" assert_success - run docker exec mail_with_ldap /bin/sh -c "grep '# Testconfig for ldap integration' /etc/postfix/ldap-aliases.cf" + run docker exec mail_with_ldap /bin/sh -c "grep '# Testconfig for ldap integration' /etc/postfix/ldap-aliases.cf" assert_success } @test "checking postfix: ldap config overwrites success" { - run docker exec mail_with_ldap /bin/sh -c "grep 'server_host = ldap' /etc/postfix/ldap-users.cf" + run docker exec mail_with_ldap /bin/sh -c "grep 'server_host = ldap' /etc/postfix/ldap-users.cf" assert_success - run docker exec mail_with_ldap /bin/sh -c "grep 'search_base = ou=people,dc=localhost,dc=localdomain' /etc/postfix/ldap-users.cf" + run docker exec mail_with_ldap /bin/sh -c "grep 'search_base = ou=people,dc=localhost,dc=localdomain' /etc/postfix/ldap-users.cf" assert_success - run docker exec mail_with_ldap /bin/sh -c "grep 'bind_dn = cn=admin,dc=localhost,dc=localdomain' /etc/postfix/ldap-users.cf" + run docker exec mail_with_ldap /bin/sh -c "grep 'bind_dn = cn=admin,dc=localhost,dc=localdomain' /etc/postfix/ldap-users.cf" assert_success - run docker exec mail_with_ldap /bin/sh -c "grep 'server_host = ldap' /etc/postfix/ldap-groups.cf" + run docker exec mail_with_ldap /bin/sh -c "grep 'server_host = ldap' /etc/postfix/ldap-groups.cf" assert_success - run docker exec mail_with_ldap /bin/sh -c "grep 'search_base = ou=people,dc=localhost,dc=localdomain' /etc/postfix/ldap-groups.cf" + run docker exec mail_with_ldap /bin/sh -c "grep 'search_base = ou=people,dc=localhost,dc=localdomain' /etc/postfix/ldap-groups.cf" assert_success - run docker exec mail_with_ldap /bin/sh -c "grep 'bind_dn = cn=admin,dc=localhost,dc=localdomain' /etc/postfix/ldap-groups.cf" + run docker exec mail_with_ldap /bin/sh -c "grep 'bind_dn = cn=admin,dc=localhost,dc=localdomain' /etc/postfix/ldap-groups.cf" assert_success - run docker exec mail_with_ldap /bin/sh -c "grep 'server_host = ldap' /etc/postfix/ldap-aliases.cf" + run docker exec mail_with_ldap /bin/sh -c "grep 'server_host = ldap' /etc/postfix/ldap-aliases.cf" assert_success - run docker exec mail_with_ldap /bin/sh -c "grep 'search_base = ou=people,dc=localhost,dc=localdomain' /etc/postfix/ldap-aliases.cf" + run docker exec mail_with_ldap /bin/sh -c "grep 'search_base = ou=people,dc=localhost,dc=localdomain' /etc/postfix/ldap-aliases.cf" assert_success - run docker exec mail_with_ldap /bin/sh -c "grep 'bind_dn = cn=admin,dc=localhost,dc=localdomain' /etc/postfix/ldap-aliases.cf" + run docker exec mail_with_ldap /bin/sh -c "grep 'bind_dn = cn=admin,dc=localhost,dc=localdomain' /etc/postfix/ldap-aliases.cf" assert_success } @@ -1096,6 +1096,15 @@ load 'test_helper/bats-assert/load' assert_output 1 } +@test "checking dovecot: ldap config overwrites success" { + run docker exec mail_with_ldap /bin/sh -c "grep 'hosts = ldap-host' /etc/dovecot/dovecot-ldap.conf.ext" + assert_success + run docker exec mail_with_ldap /bin/sh -c "grep 'base = ou=people,dc=localhost,dc=localdomain' /etc/dovecot/dovecot-ldap.conf.ext" + assert_success + run docker exec mail_with_ldap /bin/sh -c "grep 'dn = cn=admin,dc=localhost,dc=localdomain' /etc/dovecot/dovecot-ldap.conf.ext" + assert_success +} + # saslauthd @test "checking saslauthd: sasl ldap authentication works" { run docker exec mail_with_ldap bash -c "testsaslauthd -u some.user -p secret" From bc11ead67df9d6b4d9ea981c09e0e463b066124d Mon Sep 17 00:00:00 2001 From: alinmear Date: Fri, 30 Jun 2017 23:01:17 +0200 Subject: [PATCH 2/5] Fix typo within DEFAULT_VARS Definitions --- target/start-mailserver.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/target/start-mailserver.sh b/target/start-mailserver.sh index 78e42cc4..cfec849d 100644 --- a/target/start-mailserver.sh +++ b/target/start-mailserver.sh @@ -28,8 +28,8 @@ DEFAULT_VARS["DOVECOT_BASE"]="${DOVECOT_BASE:="${LDAP_SEARCH_BASE}"}" DEFAULT_VARS["DOVECOT_DN"]="${DOVECOT_DN:="${LDAP_BIND_DN}"}" DEFAULT_VARS["DOVECOT_DNPASS"]="${DOVECOT_DNPASS:="${LDAP_BIND_PW}"}" DEFAULT_VARS["DOVECOT_HOSTS"]="${DOVECOT_HOSTS:="${LDAP_SERVER_HOST}"}" -DEFAULT_VARS["DOVECOT_PASS_FILTER"]="${DOVECOT_PASS_FILTER}="${LDAP_QUERY_FILTER}"}" -DEFAULT_VARS["DOVECOT_USER_FILTER"]="${DOVECOT_USER_FILTER}="${LDAP_QUERY_FILTER}"}" +DEFAULT_VARS["DOVECOT_PASS_FILTER"]="${DOVECOT_PASS_FILTER:="${LDAP_QUERY_FILTER}"}" +DEFAULT_VARS["DOVECOT_USER_FILTER"]="${DOVECOT_USER_FILTER:="${LDAP_QUERY_FILTER}"}" ########################################################################## # << DEFAULT VARS From 6cd603c0d6adcb0fece62601b10a3b974d7c434e Mon Sep 17 00:00:00 2001 From: alinmear Date: Fri, 30 Jun 2017 23:25:47 +0200 Subject: [PATCH 3/5] Fix wrong ldap hosts value within the bats test --- test/tests.bats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/tests.bats b/test/tests.bats index bd991261..879e5a0d 100644 --- a/test/tests.bats +++ b/test/tests.bats @@ -1097,7 +1097,7 @@ load 'test_helper/bats-assert/load' } @test "checking dovecot: ldap config overwrites success" { - run docker exec mail_with_ldap /bin/sh -c "grep 'hosts = ldap-host' /etc/dovecot/dovecot-ldap.conf.ext" + run docker exec mail_with_ldap /bin/sh -c "grep 'hosts = ldap' /etc/dovecot/dovecot-ldap.conf.ext" assert_success run docker exec mail_with_ldap /bin/sh -c "grep 'base = ou=people,dc=localhost,dc=localdomain' /etc/dovecot/dovecot-ldap.conf.ext" assert_success From a5877e4d5953b02d454ed3a6052c203fdf6a8592 Mon Sep 17 00:00:00 2001 From: alinmear Date: Sat, 1 Jul 2017 20:47:28 +0200 Subject: [PATCH 4/5] Fix override_config for strings containing & --- target/start-mailserver.sh | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/target/start-mailserver.sh b/target/start-mailserver.sh index cfec849d..e3147e12 100644 --- a/target/start-mailserver.sh +++ b/target/start-mailserver.sh @@ -22,15 +22,6 @@ DEFAULT_VARS["ENABLE_SASLAUTHD"]="${ENABLE_SASLAUTHD:="0"}" DEFAULT_VARS["SMTP_ONLY"]="${SMTP_ONLY:="0"}" DEFAULT_VARS["DMS_DEBUG"]="${DMS_DEBUG:="0"}" DEFAULT_VARS["OVERRIDE_HOSTNAME"]="${OVERRIDE_HOSTNAME}" - -# DOVECOT LDAP -DEFAULT_VARS["DOVECOT_BASE"]="${DOVECOT_BASE:="${LDAP_SEARCH_BASE}"}" -DEFAULT_VARS["DOVECOT_DN"]="${DOVECOT_DN:="${LDAP_BIND_DN}"}" -DEFAULT_VARS["DOVECOT_DNPASS"]="${DOVECOT_DNPASS:="${LDAP_BIND_PW}"}" -DEFAULT_VARS["DOVECOT_HOSTS"]="${DOVECOT_HOSTS:="${LDAP_SERVER_HOST}"}" -DEFAULT_VARS["DOVECOT_PASS_FILTER"]="${DOVECOT_PASS_FILTER:="${LDAP_QUERY_FILTER}"}" -DEFAULT_VARS["DOVECOT_USER_FILTER"]="${DOVECOT_USER_FILTER:="${LDAP_QUERY_FILTER}"}" - ########################################################################## # << DEFAULT VARS ########################################################################## @@ -362,7 +353,7 @@ function override_config() { do [ -z $key ] && echo -e "\t no key provided" && return 1 - sed -i -e "s|^${key}[[:space:]]\+.*|${key} = "${config_overrides[$key]}'|g' \ + sed -i -e "s|^${key}[[:space:]]\+.*|${key} = ${config_overrides[$key]//&/\\&}|g" \ ${f} done fi @@ -586,6 +577,20 @@ function _setup_ldap() { override_config "LDAP_" "/etc/postfix/ldap-users.cf /etc/postfix/ldap-groups.cf /etc/postfix/ldap-aliases.cf" notify 'inf' "Configuring dovecot LDAP" + + declare -A _dovecot_ldap_mapping + + _dovecot_ldap_mapping["DOVECOT_BASE"]="${DOVECOT_BASE:="${LDAP_SEARCH_BASE}"}" + _dovecot_ldap_mapping["DOVECOT_DN"]="${DOVECOT_DN:="${LDAP_BIND_DN}"}" + _dovecot_ldap_mapping["DOVECOT_DNPASS"]="${DOVECOT_DNPASS:="${LDAP_BIND_PW}"}" + _dovecot_ldap_mapping["DOVECOT_HOSTS"]="${DOVECOT_HOSTS:="${LDAP_SERVER_HOST}"}" + _dovecot_ldap_mapping["DOVECOT_PASS_FILTER"]="${DOVECOT_PASS_FILTER:="${LDAP_QUERY_FILTER}"}" + _dovecot_ldap_mapping["DOVECOT_USER_FILTER"]="${DOVECOT_USER_FILTER:="${LDAP_QUERY_FILTER}"}" + + for var in ${!_dovecot_ldap_mapping[@]}; do + export $var=${_dovecot_ldap_mapping[$var]} + done + override_config "DOVECOT_" "/etc/dovecot/dovecot-ldap.conf.ext" # Add domainname to vhost. From a88b7ce2f333ba1fd7428bcfacb6642492f019a3 Mon Sep 17 00:00:00 2001 From: alinmear Date: Sat, 1 Jul 2017 21:25:28 +0200 Subject: [PATCH 5/5] Fix erroneous removal of an conditional within the postfix override function --- target/start-mailserver.sh | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/target/start-mailserver.sh b/target/start-mailserver.sh index e3147e12..b99ab53d 100644 --- a/target/start-mailserver.sh +++ b/target/start-mailserver.sh @@ -353,7 +353,7 @@ function override_config() { do [ -z $key ] && echo -e "\t no key provided" && return 1 - sed -i -e "s|^${key}[[:space:]]\+.*|${key} = ${config_overrides[$key]//&/\\&}|g" \ + sed -i -e "s|^${key}[[:space:]]\+.*|g${key} = ${config_overrides[$key]//&/\\&}|g" \ ${f} done fi @@ -913,6 +913,16 @@ function _setup_postfix_override_configuration() { else notify 'inf' "No extra postfix settings loaded because optional '/tmp/docker-mailserver/postfix-main.cf' not provided." fi + if [ -f /tmp/docker-mailserver/postfix-master.cf ]; then + while read line; do + if [[ "$line" =~ ^[a-z] ]]; then + postconf -P "$line" + fi + done < /tmp/docker-mailserver/postfix-master.cf + notify 'inf' "Loaded 'config/postfix-master.cf'" + else + notify 'inf' "No extra postfix settings loaded because optional '/tmp/docker-mailserver/postfix-master.cf' not provided." + fi } function _setup_postfix_sasl_password() {