diff --git a/target/start-mailserver.sh b/target/start-mailserver.sh index 6a725874..b6103e93 100644 --- a/target/start-mailserver.sh +++ b/target/start-mailserver.sh @@ -316,6 +316,50 @@ function display_startup_daemon() { return $res } +function override_config() { + notify "task" "Starting do do overrides" + + declare -A config_overrides + + _env_variable_prefix=$1 + [ -z ${_env_variable_prefix} ] && return 1 + + + IFS=" " read -r -a _config_files <<< $2 + + # dispatch env variables + for env_variable in $(printenv | grep $_env_variable_prefix);do + # get key + # IFS not working because values like ldap_query_filter or search base consists of several '=' + # IFS="=" read -r -a __values <<< $env_variable + # key="${__values[0]}" + # value="${__values[1]}" + key=$(echo $env_variable | cut -d "=" -f1) + key=${key#"${_env_variable_prefix}"} + # make key lowercase + key=${key,,} + # get value + value=$(echo $env_variable | cut -d "=" -f2-) + + config_overrides[$key]=$value + done + + for f in "${_config_files[@]}" + do + if [ ! -f "${f}" ];then + echo "Can not find ${f}. Skipping override" + else + 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 + fi + done +} + # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # ! CARE --> DON'T CHANGE, except you know exactly what you are doing # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -497,21 +541,27 @@ function _setup_dovecot_local_user() { function _setup_ldap() { notify 'task' 'Setting up Ldap' + + notify 'inf' 'Checking for custom configs' + # cp config files if in place for i in 'users' 'groups' 'aliases'; do - sed -i -e 's|^server_host.*|server_host = '${LDAP_SERVER_HOST:="mail.domain.com"}'|g' \ - -e 's|^search_base.*|search_base = '${LDAP_SEARCH_BASE:="ou=people,dc=domain,dc=com"}'|g' \ - -e 's|^bind_dn.*|bind_dn = '${LDAP_BIND_DN:="cn=admin,dc=domain,dc=com"}'|g' \ - -e 's|^bind_pw.*|bind_pw = '${LDAP_BIND_PW:="admin"}'|g' \ - /etc/postfix/ldap-${i}.cf + fpath="/tmp/docker-mailserver/ldap-${i}.cf" + if [ -f $fpath ]; then + 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 - + # Add domainname to vhost. echo $DOMAINNAME >> /tmp/vhost.tmp @@ -962,7 +1012,7 @@ function _fix_var_mail_permissions() { } function _fix_var_amavis_permissions() { - if [ "$ONE_DIR" -eq 0 ]; then + if [[ "$ONE_DIR" -eq 0 ]]; then amavis_state_dir=/var/lib/amavis else amavis_state_dir=/var/mail-state/lib-amavis diff --git a/test/config/ldap-aliases.cf b/test/config/ldap-aliases.cf new file mode 100644 index 00000000..f51f2d08 --- /dev/null +++ b/test/config/ldap-aliases.cf @@ -0,0 +1,9 @@ +# Testconfig for ldap integration +bind = yes +bind_dn = cn=admin,dc=domain,dc=com +bind_pw = admin +query_filter = (&(mailAlias=%s)(mailEnabled=TRUE)) +result_attribute = mail +search_base = ou=people,dc=domain,dc=com +server_host = mail.domain.com +version = 3 diff --git a/test/config/ldap-groups.cf b/test/config/ldap-groups.cf new file mode 100644 index 00000000..b51d96c6 --- /dev/null +++ b/test/config/ldap-groups.cf @@ -0,0 +1,9 @@ +# Testconfig for ldap integration +bind = yes +bind_dn = cn=admin,dc=domain,dc=com +bind_pw = admin +query_filter = (&(mailGroupMember=%s)(mailEnabled=TRUE)) +result_attribute = mail +search_base = ou=people,dc=domain,dc=com +server_host = mail.domain.com +version = 3 diff --git a/test/config/ldap-users.cf b/test/config/ldap-users.cf new file mode 100644 index 00000000..fa915ccb --- /dev/null +++ b/test/config/ldap-users.cf @@ -0,0 +1,9 @@ +# Testconfig for ldap integration +bind = yes +bind_dn = cn=admin,dc=domain,dc=com +bind_pw = admin +query_filter = (&(mail=%s)(mailEnabled=TRUE)) +result_attribute = mail +search_base = ou=people,dc=domain,dc=com +server_host = mail.domain.com +version = 3 diff --git a/test/tests.bats b/test/tests.bats index 39fd6efa..e4199259 100644 --- a/test/tests.bats +++ b/test/tests.bats @@ -1012,6 +1012,38 @@ load 'test_helper/bats-assert/load' assert_output "some.user@localhost.localdomain" } +@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" + assert_success + 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" + 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" + 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" + 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" + assert_success + + 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" + 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" + assert_success + + 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" + 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" + assert_success +} + # dovecot @test "checking dovecot: ldap imap connection and authentication works" { run docker exec mail_with_ldap /bin/sh -c "nc -w 1 0.0.0.0 143 < /tmp/docker-mailserver-test/auth/imap-ldap-auth.txt" @@ -1037,6 +1069,7 @@ load 'test_helper/bats-assert/load' assert_success } + # # RIMAP #