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 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 11:10:49 +01:00
co-authored by Claude Opus 5.5
parent 39909a5b30
commit 95c8ebbe0e
3 changed files with 69 additions and 38 deletions
+20 -9
View File
@@ -1213,19 +1213,30 @@ set_postfwd_default() {
_tmp_file="$(mktemp)" _tmp_file="$(mktemp)"
awk -v var="$_var" -v val="$_val" ' awk -v var="$_var" -v val="$_val" '
function unq(s) { 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) if (s ~ /^".*"$/ || s ~ /^\047.*\047$/) s = substr(s, 2, length(s) - 2)
return s return s
} }
$0 ~ "^[ \t]*" var "=" { function is_set(s) { return s ~ ("^[ \t]*" var "=") }
cur = $0 ; sub("^[ \t]*" var "=", "", cur) function is_wanted(s) {
if (! done && unq(cur) == unq(val)) { print ; done = 1 ; next } sub("^[ \t]*" var "=", "", s)
print "#" $0 return unq(s) == unq(val)
if (! done) { print var "=" val ; done = 1 } }
next # - 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" ' "$postfwd_default_file" > "$_tmp_file" 2> "$log_file"
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
+20 -9
View File
@@ -120,19 +120,30 @@ set_postfwd_default() {
_tmp_file="$(mktemp)" _tmp_file="$(mktemp)"
awk -v var="$_var" -v val="$_val" ' awk -v var="$_var" -v val="$_val" '
function unq(s) { 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) if (s ~ /^".*"$/ || s ~ /^\047.*\047$/) s = substr(s, 2, length(s) - 2)
return s return s
} }
$0 ~ "^[ \t]*" var "=" { function is_set(s) { return s ~ ("^[ \t]*" var "=") }
cur = $0 ; sub("^[ \t]*" var "=", "", cur) function is_wanted(s) {
if (! done && unq(cur) == unq(val)) { print ; done = 1 ; next } sub("^[ \t]*" var "=", "", s)
print "#" $0 return unq(s) == unq(val)
if (! done) { print var "=" val ; done = 1 } }
next # - 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" ' "$postfwd_default_file" > "$_tmp_file" 2> "$log_file"
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
+30 -21
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/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: # For every variable below:
# - already set to the wanted value -> nothing to do # - already set to the wanted value -> nothing to do
@@ -14,8 +14,7 @@
# - file changed -> 'systemctl restart postfwd' # - file changed -> 'systemctl restart postfwd'
# (ARGS, INET and PORT are start parameters of the # (ARGS, INET and PORT are start parameters of the
# daemon - a reload does NOT apply them) # daemon - a reload does NOT apply them)
# - file unchanged -> 'systemctl reload postfwd' # - file unchanged -> postfwd is left alone (no restart, no reload)
# (rereads postfwd.cf and the file: lists)
# #
postfwd_default_file="/etc/default/postfwd" postfwd_default_file="/etc/default/postfwd"
@@ -80,19 +79,30 @@ set_postfwd_default() {
_tmp_file="$(mktemp)" _tmp_file="$(mktemp)"
awk -v var="$_var" -v val="$_val" ' awk -v var="$_var" -v val="$_val" '
function unq(s) { 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) if (s ~ /^".*"$/ || s ~ /^\047.*\047$/) s = substr(s, 2, length(s) - 2)
return s return s
} }
$0 ~ "^[ \t]*" var "=" { function is_set(s) { return s ~ ("^[ \t]*" var "=") }
cur = $0 ; sub("^[ \t]*" var "=", "", cur) function is_wanted(s) {
if (! done && unq(cur) == unq(val)) { print ; done = 1 ; next } sub("^[ \t]*" var "=", "", s)
print "#" $0 return unq(s) == unq(val)
if (! done) { print var "=" val ; done = 1 } }
next # - 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" ' "$postfwd_default_file" > "$_tmp_file" 2> "$log_file"
if [[ $? -ne 0 ]] ; then if [[ $? -ne 0 ]] ; then
@@ -149,18 +159,17 @@ done
echo "" echo ""
if $errors ; then 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
echo " Nothing changed - postfwd is left alone."
echo ""
clean_up 0
fi fi
if $file_changed ; then
echononl " Restart postfwd (start parameters changed)" echononl " Restart postfwd (start parameters changed)"
_action="restart" if systemctl restart postfwd > "$log_file" 2>&1 ; then
else
echononl " Reload postfwd"
_action="reload"
fi
if systemctl $_action postfwd > "$log_file" 2>&1 ; then
echo_ok echo_ok
else else
echo_failed echo_failed