From 95c8ebbe0e20a44b3c41725c26706a6bed69f316 Mon Sep 17 00:00:00 2001 From: Christoph Date: Sun, 8 Nov 2026 11:10:49 +0100 Subject: [PATCH] postfwd defaults: keep lines with the wanted value, no reload if unchanged - Never comment out a line that already has the wanted value, even if a differing setting of the same variable precedes it. - update_postfwd_defaults.sh: leave postfwd alone if nothing changed. Co-Authored-By: Claude Opus 5.5 --- install_postfix_advanced.sh | 29 +++++++++++++++------- install_postfwd.sh | 29 +++++++++++++++------- update_postfwd_defaults.sh | 49 ++++++++++++++++++++++--------------- 3 files changed, 69 insertions(+), 38 deletions(-) diff --git a/install_postfix_advanced.sh b/install_postfix_advanced.sh index dc5c865..0b9b765 100755 --- a/install_postfix_advanced.sh +++ b/install_postfix_advanced.sh @@ -1213,19 +1213,30 @@ set_postfwd_default() { _tmp_file="$(mktemp)" awk -v var="$_var" -v val="$_val" ' function unq(s) { - gsub(/^[ \t]+|[ \t]+$/, "", s) + gsub(/^[ \t\r]+|[ \t\r]+$/, "", s) if (s ~ /^".*"$/ || s ~ /^\047.*\047$/) s = substr(s, 2, length(s) - 2) return s } - $0 ~ "^[ \t]*" var "=" { - cur = $0 ; sub("^[ \t]*" var "=", "", cur) - if (! done && unq(cur) == unq(val)) { print ; done = 1 ; next } - print "#" $0 - if (! done) { print var "=" val ; done = 1 } - next + function is_set(s) { return s ~ ("^[ \t]*" var "=") } + function is_wanted(s) { + sub("^[ \t]*" var "=", "", s) + return unq(s) == unq(val) + } + # - Read the whole file first: a line with the wanted value must stay + # - untouched, no matter which other settings of VAR precede it. + { + line[NR] = $0 + if (is_set($0) && is_wanted($0)) found = 1 + } + END { + for (i = 1; i <= NR; i++) { + if (! is_set(line[i])) { print line[i] ; continue } + if (is_wanted(line[i])) { print line[i] ; continue } + print "#" line[i] + if (! found) { print var "=" val ; found = 1 } + } + if (! found) print var "=" val } - { print } - END { if (! done) print var "=" val } ' "$postfwd_default_file" > "$_tmp_file" 2> "$log_file" if [[ $? -ne 0 ]] ; then diff --git a/install_postfwd.sh b/install_postfwd.sh index 62c2a1c..062adb0 100755 --- a/install_postfwd.sh +++ b/install_postfwd.sh @@ -120,19 +120,30 @@ set_postfwd_default() { _tmp_file="$(mktemp)" awk -v var="$_var" -v val="$_val" ' function unq(s) { - gsub(/^[ \t]+|[ \t]+$/, "", s) + gsub(/^[ \t\r]+|[ \t\r]+$/, "", s) if (s ~ /^".*"$/ || s ~ /^\047.*\047$/) s = substr(s, 2, length(s) - 2) return s } - $0 ~ "^[ \t]*" var "=" { - cur = $0 ; sub("^[ \t]*" var "=", "", cur) - if (! done && unq(cur) == unq(val)) { print ; done = 1 ; next } - print "#" $0 - if (! done) { print var "=" val ; done = 1 } - next + function is_set(s) { return s ~ ("^[ \t]*" var "=") } + function is_wanted(s) { + sub("^[ \t]*" var "=", "", s) + return unq(s) == unq(val) + } + # - Read the whole file first: a line with the wanted value must stay + # - untouched, no matter which other settings of VAR precede it. + { + line[NR] = $0 + if (is_set($0) && is_wanted($0)) found = 1 + } + END { + for (i = 1; i <= NR; i++) { + if (! is_set(line[i])) { print line[i] ; continue } + if (is_wanted(line[i])) { print line[i] ; continue } + print "#" line[i] + if (! found) { print var "=" val ; found = 1 } + } + if (! found) print var "=" val } - { print } - END { if (! done) print var "=" val } ' "$postfwd_default_file" > "$_tmp_file" 2> "$log_file" if [[ $? -ne 0 ]] ; then diff --git a/update_postfwd_defaults.sh b/update_postfwd_defaults.sh index 765aa94..a870be3 100755 --- a/update_postfwd_defaults.sh +++ b/update_postfwd_defaults.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash # -# Update /etc/default/postfwd (idempotent) and reload postfwd. +# Update /etc/default/postfwd (idempotent) and restart postfwd if needed. # # For every variable below: # - already set to the wanted value -> nothing to do @@ -14,8 +14,7 @@ # - file changed -> 'systemctl restart postfwd' # (ARGS, INET and PORT are start parameters of the # daemon - a reload does NOT apply them) -# - file unchanged -> 'systemctl reload postfwd' -# (rereads postfwd.cf and the file: lists) +# - file unchanged -> postfwd is left alone (no restart, no reload) # postfwd_default_file="/etc/default/postfwd" @@ -80,19 +79,30 @@ set_postfwd_default() { _tmp_file="$(mktemp)" awk -v var="$_var" -v val="$_val" ' function unq(s) { - gsub(/^[ \t]+|[ \t]+$/, "", s) + gsub(/^[ \t\r]+|[ \t\r]+$/, "", s) if (s ~ /^".*"$/ || s ~ /^\047.*\047$/) s = substr(s, 2, length(s) - 2) return s } - $0 ~ "^[ \t]*" var "=" { - cur = $0 ; sub("^[ \t]*" var "=", "", cur) - if (! done && unq(cur) == unq(val)) { print ; done = 1 ; next } - print "#" $0 - if (! done) { print var "=" val ; done = 1 } - next + function is_set(s) { return s ~ ("^[ \t]*" var "=") } + function is_wanted(s) { + sub("^[ \t]*" var "=", "", s) + return unq(s) == unq(val) + } + # - Read the whole file first: a line with the wanted value must stay + # - untouched, no matter which other settings of VAR precede it. + { + line[NR] = $0 + if (is_set($0) && is_wanted($0)) found = 1 + } + END { + for (i = 1; i <= NR; i++) { + if (! is_set(line[i])) { print line[i] ; continue } + if (is_wanted(line[i])) { print line[i] ; continue } + print "#" line[i] + if (! found) { print var "=" val ; found = 1 } + } + if (! found) print var "=" val } - { print } - END { if (! done) print var "=" val } ' "$postfwd_default_file" > "$_tmp_file" 2> "$log_file" if [[ $? -ne 0 ]] ; then @@ -149,18 +159,17 @@ done echo "" if $errors ; then - fatal "Errors while updating '${postfwd_default_file}' - postfwd NOT reloaded." + fatal "Errors while updating '${postfwd_default_file}' - postfwd NOT restarted." fi -if $file_changed ; then - echononl " Restart postfwd (start parameters changed)" - _action="restart" -else - echononl " Reload postfwd" - _action="reload" +if ! $file_changed ; then + echo " Nothing changed - postfwd is left alone." + echo "" + clean_up 0 fi -if systemctl $_action postfwd > "$log_file" 2>&1 ; then +echononl " Restart postfwd (start parameters changed)" +if systemctl restart postfwd > "$log_file" 2>&1 ; then echo_ok else echo_failed