diff --git a/set_suexec_users_nologin.sh b/set_suexec_users_nologin.sh index ea8ee7f..8234a7a 100755 --- a/set_suexec_users_nologin.sh +++ b/set_suexec_users_nologin.sh @@ -18,7 +18,8 @@ script_dir="$(dirname $(realpath $0))" 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" old_shell="/bin/bash" @@ -266,33 +267,6 @@ echo " Dry-run............................: $dry_run" 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 # ---------- @@ -352,6 +326,28 @@ for _user in "${suexec_user_arr[@]}" ; do continue 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 if [[ $? -eq 0 ]]; then echo_ok @@ -369,26 +365,10 @@ done # - Some post-script tasks .. # ---------- -if ! $dry_run && ! $_changed ; then - - if $terminal ; then - echo "" - 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 +if $_changed ; then + info "Backup of original '/etc/passwd': ${backup_file}" +elif ! $dry_run ; then + info "Nothing has changed." fi clean_up 0