Files

256 lines
8.0 KiB
Bash

# emailwiz - plan/apply engine
# SPDX-License-Identifier: GPL-3.0-or-later
# shellcheck shell=sh
#
# plan and apply share one code path: every component renders its desired
# files into $STAGE. plan only compares; apply backs up, installs atomically,
# validates the live result and reloads only the units whose inputs changed.
# If live validation fails, the files from this run are restored.
COMPONENTS="postfix dovecot opendkim spamd fail2ban tls timers legacy"
render_all() {
_only=$1
for _c in $COMPONENTS; do
[ -z "$_only" ] || [ "$_only" = "$_c" ] || continue
"${_c}_render"
done
}
# Prints "STATE PATH" lines for staged files and "retire PATH" lines.
compute_changes() {
while read -r _p _m _o _g _u; do
_s=$(file_state "$_p" "$_m" "$_o" "$_g")
[ "$_s" = same ] || printf '%s %s\n' "$_s" "$_p"
done <"$STAGE/.meta"
while read -r _p _u _why; do
printf 'retire %s\n' "$_p"
done <"$STAGE/.retire"
while read -r _u _why; do
printf 'action %s\n' "$_u"
done <"$STAGE/.actions"
}
cmd_plan() {
_only=; _detail=no; _diff=yes
while [ $# -gt 0 ]; do
case $1 in
--component) _only=$2; shift ;;
--detailed-exitcode) _detail=yes ;;
--no-diff) _diff=no ;;
*) die "plan: unknown option $1" "$EX_USAGE" ;;
esac
shift
done
need_root plan
MODE=plan
preflight
stage_init
render_all "$_only"
firewall_note
_changes=$(compute_changes)
if [ -z "$_changes" ]; then
ok "no changes: live configuration matches desired state"
else
log "pending changes (nothing has been modified):"
printf '%s\n' "$_changes" | while read -r _s _p; do
case $_s in
action)
_why=$(awk -v u="$_p" '$1 == u { $1 = ""; sub(/^ +/, ""); print }' "$STAGE/.actions")
printf ' %-8s %s (%s)\n' "$_s" "$_p" "$_why"
;;
retire)
_why=$(awk -v p="$_p" '$1 == p { $1 = $2 = ""; sub(/^ +/, ""); print }' "$STAGE/.retire")
printf ' %-8s %s (%s)\n' "$_s" "$_p" "$_why"
;;
*) printf ' %-8s %s\n' "$_s" "$_p" ;;
esac
if [ "$_diff" = yes ] && [ "$_s" = changed ]; then
diff -u --label "live:$_p" --label "desired:$_p" "$_p" "$STAGE$_p" |
grep -v '^\(-\|+\)\s*#' | sed 's/^/ /' || :
elif [ "$_diff" = yes ] && [ "$_s" = meta ]; then
_want=$(awk -v p="$_p" '$1 == p {print $2, $3, $4}' "$STAGE/.meta")
printf ' mode/owner %s -> %s\n' "$(stat -c '%a %U %G' "$_p")" "$_want"
fi
done
fi
[ -s "$STAGE/.notes" ] && { log "notes:"; sed 's/^/ - /' "$STAGE/.notes" >&2; }
if [ -n "$_changes" ] && [ "$_detail" = yes ]; then exit "$EX_PENDING"; fi
return 0
}
# Map changed files to the service actions they require.
_actions_for() {
_acts=
for _p in $1; do
_u=$(awk -v p="$_p" '$1 == p {print $5}' "$STAGE/.meta")
[ -n "$_u" ] || _u=$(awk -v p="$_p" '$1 == p {print $2}' "$STAGE/.retire")
[ -n "$_u" ] || _u=$(awk -v p="$_p" '$1 == p {print $1}' "$STAGE/.actions")
case " $_acts " in *" $_u "*) ;; *) _acts="$_acts $_u" ;; esac
done
printf '%s\n' "$_acts"
}
# _reload UNIT - reload; if the reload itself fails (e.g. the unit's
# PrivateTmp was deleted, 226/NAMESPACE), restart. Config is already validated.
_reload() {
systemctl reload "$1" && return 0
warn "reload of $1 failed; restarting it instead"
systemctl restart "$1"
}
# Every step is checked explicitly: this runs inside `if !`, where set -e
# does not apply.
_run_actions() {
_acts=" $1 "
case $_acts in *" spamd-restart "* | *" opendkim "* | *" timers "*)
systemctl daemon-reload || return 1 ;;
esac
case $_acts in *" newaliases "*) newaliases || return 1 ;; esac
case $_acts in *" sieve "*) dovecot_post_sieve || return 1 ;; esac
case $_acts in *" opendkim "*)
systemctl restart opendkim || return 1
_wait_port "127.0.0.1" "$DKIM_PORT" || return 1 ;;
esac
case $_acts in
*" postfix-restart "*) systemctl restart postfix || return 1 ;;
*" postfix "*) _reload postfix || return 1 ;;
esac
case $_acts in *" dovecot "*) _reload dovecot || return 1 ;; esac
case $_acts in *" spamd"*) systemctl restart spamd || return 1 ;; esac
case $_acts in *" fail2ban "*) fail2ban-client reload >/dev/null || return 1 ;; esac
case $_acts in *" timers "*)
systemctl enable --now emailwiz-certcheck.timer emailwiz-backup.timer >/dev/null 2>&1 || return 1 ;;
esac
return 0
}
_wait_port() {
_i=0
while [ $_i -lt 20 ]; do
ss -Hltn "sport = :$2" 2>/dev/null | grep -q "$1:$2" && return 0
_i=$((_i + 1)); sleep 0.5
done
err "nothing listening on $1:$2"
return 1
}
# Live checks run after files are in place, before services are touched.
_validate_installed() {
_rc=0
if ! _o=$(postfix_validate_live); then err "postfix check: $_o"; _rc=1; fi
if ! _o=$(doveconf -n 2>&1 >/dev/null); then err "doveconf: $_o"; _rc=1; fi
if ! _o=$(opendkim -n 2>&1); then err "opendkim -n: $_o"; _rc=1; fi
if [ "$FAIL2BAN" = yes ] && ! _o=$(fail2ban-client -t 2>&1); then err "fail2ban -t: $_o"; _rc=1; fi
return $_rc
}
_restore_from() {
_bk=$1
warn "restoring files from $_bk"
# Remove files this run created, then restore the pre-run snapshot.
while read -r _f; do [ -n "$_f" ] && rm -f -- "$_f"; done <"$_bk/created"
tar -C / --numeric-owner -xpzf "$_bk/config.tar.gz"
systemctl daemon-reload
}
cmd_apply() {
_only=; _yes=no
while [ $# -gt 0 ]; do
case $1 in
--component) _only=$2; shift ;;
--yes) _yes=yes ;;
*) die "apply: unknown option $1" "$EX_USAGE" ;;
esac
shift
done
need_root apply
MODE=apply
preflight
stage_init
render_all "$_only"
_changes=$(compute_changes)
_key_missing=no
if [ ! -f "$(dkim_key_path)" ] && { [ -z "$_only" ] || [ "$_only" = opendkim ]; }; then
_key_missing=yes
fi
if [ -z "$_changes" ] && [ "$_key_missing" = no ]; then
ok "nothing to do: live configuration matches desired state"
return 0
fi
printf '%s\n' "$_changes" | sed '/^$/d; s/^/ /' >&2
if [ "$_yes" != yes ]; then
[ -t 0 ] || die "refusing to apply non-interactively without --yes" "$EX_USAGE"
printf 'Apply these changes? [y/N] ' >&2
read -r _ans
case $_ans in y | Y | yes) ;; *) die "aborted by user" "$EX_ERR" ;; esac
fi
_bk=$(backup_create apply)
CREATED_LIST=$_bk/created
log "backup: $_bk"
[ "$_key_missing" = yes ] && opendkim_ensure_key
printf '%s\n' "$_changes" | while read -r _s _p; do
[ -n "$_p" ] || continue
case $_s in
action) ;;
retire)
# The backup already holds a copy; keep a second one next to it.
install -d -m 0700 "$_bk/retired"
case $_p in
/etc/systemd/system/*.timer | /etc/systemd/system/*.service)
systemctl disable --now "$(basename "$_p")" >/dev/null 2>&1 || : ;;
esac
cp -p -- "$_p" "$_bk/retired/$(printf '%s' "$_p" | tr / _)"
rm -f -- "$_p"
info "retired $_p"
;;
*)
_meta=$(awk -v p="$_p" '$1 == p {print $2, $3, $4}' "$STAGE/.meta")
# shellcheck disable=SC2086
set -- $_meta
[ -e "$_p" ] || echo "$_p" >>"$CREATED_LIST"
install_atomic "$STAGE$_p" "$_p" "$1" "$2" "$3"
info "installed $_p ($_s)"
;;
esac
done
_paths=$(printf '%s\n' "$_changes" | awk '{print $2}')
if [ -f "$(dkim_key_path)" ]; then
chown root:opendkim "$(dkim_key_path)"; chmod 0640 "$(dkim_key_path)"
fi
if ! _validate_installed; then
_restore_from "$_bk"
die "live validation failed; previous configuration restored (backup: $_bk)" "$EX_VALIDATION"
fi
_acts=$(_actions_for "$_paths")
[ "$_key_missing" = yes ] && case " $_acts " in *" opendkim "*) ;; *) _acts="$_acts opendkim" ;; esac
log "service actions:${_acts}"
if ! _run_actions "$_acts"; then
_restore_from "$_bk"
_run_actions "$_acts" || :
die "service reload failed; previous configuration restored (backup: $_bk)" "$EX_VALIDATION"
fi
sleep 1
_down=
for _svc in postfix dovecot opendkim $( [ "$SPAM_FILTER" = spamc ] && echo spamd ) \
$( [ "$FAIL2BAN" = yes ] && echo fail2ban ); do
systemctl is-active -q "$_svc" || _down="$_down $_svc"
done
if [ -n "$_down" ]; then
err "inactive after apply:$_down"
_restore_from "$_bk"
_run_actions "$_acts" || :
die "services failed; previous configuration restored (backup: $_bk)" "$EX_VALIDATION"
fi
ok "applied; backup $_bk"
[ "$_key_missing" = yes ] && warn "new DKIM key generated: publish it (emailwiz dns) before relying on DKIM"
info "next: sudo emailwiz validate"
}