diff --git a/Dockerfile b/Dockerfile index 4d0e3568..f6b40124 100644 --- a/Dockerfile +++ b/Dockerfile @@ -120,6 +120,9 @@ COPY \ target/postfix/ldap-senders.cf \ /etc/postfix/ +# LDAP config support: +COPY --link target/features/ldap/ /etc/dms/ldap/ + # hadolint ignore=SC2016 RUN < Removes lines where keys have no value assigned. +# - tac + sort => Remove any duplicate keys (keeps the last instance found). +# +# @param ${1} = A delimiter between key and value columns +# @param ${2} = Input filepath to clean +# @output = The transformed file content +function _cleanse_config() { + local KV_DELIMITER=${1:?KV Delimiter is required} + local INPUT_FILE=${2?:Input file is required} + + sed "/^[^${KV_DELIMITER}]*${KV_DELIMITER}\s*$/d" ${INPUT_FILE} \ + | tac | sort -u -t"${KV_DELIMITER}" -k1,1 +} diff --git a/target/scripts/startup/setup.d/saslauthd.sh b/target/scripts/startup/setup.d/saslauthd.sh index eb33a243..daddb852 100644 --- a/target/scripts/startup/setup.d/saslauthd.sh +++ b/target/scripts/startup/setup.d/saslauthd.sh @@ -9,24 +9,7 @@ function _setup_saslauthd() { if [[ ${ACCOUNT_PROVISIONER} == 'LDAP' ]] \ && [[ ! -f /etc/saslauthd.conf ]]; then _log 'trace' 'Creating /etc/saslauthd.conf' - - # Create a config based on ENV - sed '/^.*: $/d'> /etc/saslauthd.conf << EOF -ldap_servers: ${SASLAUTHD_LDAP_SERVER:=${LDAP_SERVER_HOST}} -ldap_auth_method: ${SASLAUTHD_LDAP_AUTH_METHOD:=bind} -ldap_bind_dn: ${SASLAUTHD_LDAP_BIND_DN:=${LDAP_BIND_DN}} -ldap_bind_pw: ${SASLAUTHD_LDAP_PASSWORD:=${LDAP_BIND_PW}} -ldap_search_base: ${SASLAUTHD_LDAP_SEARCH_BASE:=${LDAP_SEARCH_BASE}} -ldap_filter: ${SASLAUTHD_LDAP_FILTER:=(&(uniqueIdentifier=%u)(mailEnabled=TRUE))} -ldap_start_tls: ${SASLAUTHD_LDAP_START_TLS:=no} -ldap_tls_check_peer: ${SASLAUTHD_LDAP_TLS_CHECK_PEER:=no} -ldap_tls_cacert_file: ${SASLAUTHD_LDAP_TLS_CACERT_FILE} -ldap_tls_cacert_dir: ${SASLAUTHD_LDAP_TLS_CACERT_DIR} -ldap_password_attr: ${SASLAUTHD_LDAP_PASSWORD_ATTR} -ldap_mech: ${SASLAUTHD_LDAP_MECH} -ldap_referrals: yes -log_level: 10 -EOF + _create_config_saslauthd fi sed -i \ @@ -42,3 +25,20 @@ EOF gpasswd -a postfix sasl >/dev/null } + +function _create_config_saslauthd() { + local SASLAUTHD_LDAP_SERVER=${SASLAUTHD_LDAP_SERVER:=${LDAP_SERVER_HOST}} + local SASLAUTHD_LDAP_BIND_DN=${SASLAUTHD_LDAP_BIND_DN:=${LDAP_BIND_DN}} + local SASLAUTHD_LDAP_PASSWORD=${SASLAUTHD_LDAP_PASSWORD:=${LDAP_BIND_PW}} + local SASLAUTHD_LDAP_SEARCH_BASE=${SASLAUTHD_LDAP_SEARCH_BASE:=${LDAP_SEARCH_BASE}} + local SASLAUTHD_LDAP_FILTER=${SASLAUTHD_LDAP_FILTER:=(&(uniqueIdentifier=%u)(mailEnabled=TRUE))} + local SASLAUTHD_LDAP_REFERRALS=${SASLAUTHD_LDAP_REFERRALS:=yes} + + # Generates a config from an ENV template while layering several other sources + # into a single temporary file, used as input into `_cleanse_config` which + # prepares the final output config. + _cleanse_config ':' <(cat 2>/dev/null \ + /tmp/docker-mailserver/ldap/saslauthd.conf \ + <(_template_with_env 'SASLAUTHD_' /etc/dms/ldap/saslauthd.tmpl) \ + ) > /etc/saslauthd.conf +}