set_suexec_users_nologin.sh: backup /etc/passwd only before the first real change.

A run without changes no longer creates (and removes) a backup file, and
a second run within the same minute can no longer overwrite the backup of
the previous run. Backup timestamp now includes seconds; an existing backup
file is never overwritten.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrisandClaude Opus 5.5 committed 2026-11-16 13:40:02 +01:00
1 parent 5da75e1fc0
commit 95034598b0
1 file changed
+28 -48
+28 -48
View File
@@ -18,7 +18,8 @@ script_dir="$(dirname $(realpath $0))"
log_file="$(mktemp)" log_file="$(mktemp)"
backup_date=$(date +%Y-%m-%d-%H%M) backup_date=$(date +%Y-%m-%d-%H%M%S)
backup_file="/etc/passwd.${backup_date}"
new_shell="/usr/sbin/nologin" new_shell="/usr/sbin/nologin"
old_shell="/bin/bash" old_shell="/bin/bash"
@@ -266,33 +267,6 @@ echo " Dry-run............................: $dry_run"
echo "" echo ""
# ----------
# - Some pre-script tasks ..
# ----------
if ! $dry_run ; then
if $terminal ; then
echo ""
echo -e " \033[32m-----\033[m"
echo -e " \033[1mDoing some pre-script tasks ..\033[m"
echo -e " \033[32m-----\033[m"
echo ""
fi
# - Backup /etc/passwd (removed again, if nothing has changed)
# -
echononl " Backup '/etc/passwd' to \033[1m/etc/passwd.${backup_date}\033[m"
cp -a /etc/passwd "/etc/passwd.${backup_date}" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $log_file)"
fi
fi
# ---------- # ----------
# - Main part of script # - Main part of script
# ---------- # ----------
@@ -352,6 +326,28 @@ for _user in "${suexec_user_arr[@]}" ; do
continue continue
fi fi
# - Backup /etc/passwd before the first real change (only once per run).
# -
# - So a run without changes leaves no backup file behind and can never
# - overwrite the backup of a previous run.
# -
if ! $_changed ; then
echo ""
echononl " Backup '/etc/passwd' to \033[1m${backup_file}\033[m"
if [[ -e "$backup_file" ]] ; then
echo_failed
fatal "Backup file '${backup_file}' already exists."
fi
cp -a /etc/passwd "$backup_file" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $log_file)"
fi
echononl " User \033[1m${_user}\033[m"
fi
usermod -s "$new_shell" "$_user" > $log_file 2>&1 usermod -s "$new_shell" "$_user" > $log_file 2>&1
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
echo_ok echo_ok
@@ -369,26 +365,10 @@ done
# - Some post-script tasks .. # - Some post-script tasks ..
# ---------- # ----------
if ! $dry_run && ! $_changed ; then if $_changed ; then
info "Backup of original '/etc/passwd': ${backup_file}"
if $terminal ; then elif ! $dry_run ; then
echo "" info "Nothing has changed."
echo ""
echo -e " \033[32m-----\033[m"
echo -e " \033[1mDoing some post-script tasks ..\033[m"
echo -e " \033[32m-----\033[m"
echo ""
fi
echo " Nothing has changed"
echononl " Remove previously created backup '/etc/passwd.${backup_date}'"
rm -f "/etc/passwd.${backup_date}" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $log_file)"
fi
fi fi
clean_up 0 clean_up 0