#!/bin/sh -e CONF_PATH=/etc/dma # set in Makefile # Determine mailname if test -f "${__object}/parameter/mailname" then mailname=$(cat "${__object}/parameter/mailname") else case $(cat "${__global}/explorer/os") in (debian|devuan|ubuntu) # On Debian-like systems use /etc/mailname unless --mailname is used mailname='/etc/mailname' ;; (*) mailname=$__target_fqdn ;; esac fi # Generate config conf_should=$( if test -s "${__object}/parameter/smart-host" then printf 'SMARTHOST %s\n' "$(cat "${__object}/parameter/smart-host")" fi printf 'MAILNAME %s\n' "${mailname}" if test -s "${__object}/explorer/auth_conf" then printf "AUTHPATH %s\n" "$(cat "${__object}/explorer/auth_conf")" fi case $(cat "${__object}/parameter/security") in (ssl|tls) default_smtp_port=465 echo 'SECURETRANSFER' ;; (starttls) default_smtp_port=587 echo 'SECURETRANSFER' echo 'STARTTLS' ;; (opportunistic) default_smtp_port=25 # XXX: correct? echo 'SECURETRANSFER' echo 'STARTTLS' echo 'OPPORTUNISTIC_TLS' ;; (insecure) default_smtp_port=25 echo 'INSECURE' ;; esac if test -s "${__object}/parameter/port" then printf 'PORT %u\n' "$(cat "${__object}/parameter/port")" elif test "${default_smtp_port}" -ne 25 then printf 'PORT %u\n' "${default_smtp_port}" fi if test -f "${__object}/parameter/masquerade" then while read -r line do printf 'MASQUERADE %s\n' "${line}" done <"${__object}/parameter/masquerade" fi if test -f "${__object}/parameter/defer" then echo 'DEFER' fi if test -f "${__object}/parameter/full-bounce" then echo 'FULLBOUNCE' fi if test -f "${__object}/parameter/null-client" then test -s "${__object}/parameter/smart-host" || { echo '--null-client requires a --smart-host to be defined' >&2 exit 1 } echo 'NULLCLIENT' fi ) conf_should=$(echo "$conf_should" | sort -s -k 1,1) config_updated=false if ! echo "$conf_should" | cmp -s "${__object}/explorer/conf" - then # config needs to be updated echo "dma_conf='${CONF_PATH:?}/dma.conf'" cat <<'EOF' awk -F '\n' ' function comment_line(line) { return match(line, /^[ \t]*#+[ \t]*/) } function empty_line(line) { return match(line, /^[ \t]*$/) } function is_word(s) { return s ~ /^[A-Z_]+$/ } function first(line, sep) { if (!sep) sep = SUBSEP return index(line, sep) ? substr(line, 1, index(line, sep) - 1) : line } function rest(line, sep) { if (!sep) sep = SUBSEP if (index(line, sep)) return substr(line, index(line, sep) + 1) } function conf_pop(word, value) { if (!(word in conf)) return 0 if (!value) { if (index(conf[word], SUBSEP)) # more than one element? value = substr(conf[word], 1, index(conf[word], SUBSEP) - 1) else value = conf[word] } if (index(conf[word], SUBSEP)) { if (index(conf[word], value SUBSEP) != 1) return 0 conf[word] = substr(conf[word], length(value) + 2) } else { if (conf[word] != value) return 0 delete conf[word] } return value } function print_conf(word, value) { printf "%s", word if (value) printf " %s", value printf "\n" } function print_confs(word, value) { if (!(word in conf)) return if (conf[word]) { while (value = conf_pop(word)) print_conf(word, value) } else { print_conf(word) delete conf[word] } } BEGIN { while (getline < "/dev/stdin") { word = first($0, " ") if ((word in conf)) conf[word] = conf[word] SUBSEP rest($0, " ") else conf[word] = rest($0, " ") } } # first pass, gather information NR == FNR { if (comment_line($0)) { # comment line word = first(substr($0, RLENGTH + 1), " ") if (is_word(word)) last_occ["#" word] = FNR } else { word = first($0, " ") if (is_word(word)) last_occ[word] = FNR } } NR > FNR && FNR == 1 { # before second pass prepare hashes for (k in last_occ) if (k ~ /^\#/ && (substr(k, 2) in last_occ)) delete last_occ[k] for (k in last_occ) { line_map[last_occ[k]] = k } } # second pass, output new config NR > FNR { if (comment_line($0) || empty_line($0)) { # comment or empty line print if ((FNR in line_map)) { if (line_map[FNR] ~ /^\#/) { # the "matching" comment line is here k = substr(line_map[FNR], 2) if ((k in conf)) print_confs(k) } if (("INSECURE" in conf) && line_map[FNR] ~ /^\#?SECURE$/) { # INSECURE goes where SECURE comment is print_confs("INSECURE") } } } else { sub(/[ \t]*\#.*$/, "", $0) # ignore comments word = first($0, " ") if ((word in conf) && rest($0, " ") == first(conf[word])) { # keep config options we want conf_pop(word) print } if ((FNR in line_map) && line_map[FNR] == word) { # rest of config options should be here print_confs(word) } } } END { # print rest of config options for (word in conf) print_confs(word) } ' "${dma_conf}" "${dma_conf}" <<'EOF' >"${dma_conf}.tmp" \ && mv "${dma_conf}.tmp" "${dma_conf}" EOF echo "${conf_should}" echo 'EOF' config_updated=true echo 'config updated' >>"${__messages_out}" fi if test -f "${__object}/parameter/send-test-email" then modified=false if grep -q '^__mail_alias/root:' "${__messages_in}" then modified=true elif grep -q '^__dma_auth/' "${__messages_in}" then modified=true elif $config_updated then modified=true fi if $modified then cat <<-EOF sendmail root <