From c422d907ebc253b393c60e4c262295c3c0f13ad2 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Thu, 4 Jul 2013 19:04:34 -0700 Subject: [PATCH] remove-user.sh: improve robustness * remove gnuisms * warn if the sshkey or any sshcerts cannot be removed --- toolbox/remove-user.sh | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/toolbox/remove-user.sh b/toolbox/remove-user.sh index 14f244c..17636d9 100755 --- a/toolbox/remove-user.sh +++ b/toolbox/remove-user.sh @@ -1,5 +1,7 @@ #!/bin/sh +set -e + . @basedir@/shlib.sh if [ -z "$1" ]; then @@ -7,18 +9,22 @@ if [ -z "$1" ]; then exit 1 fi ETC="$cfg_chroot/etc" -COUNT=`grep -E -c "^$1:" $ETC/passwd` +COUNT=`grep -E -c "^$1:" "$ETC/passwd" || :` if [ "$COUNT" -ne "1" ]; then echo "fatal: user '$1' doesn't appear to exist (or exists multiple times, or contains regexpy characters)." exit 1 fi -GROUPS=`sed -rn '/[,:]'$1'(,|$)/ s/:.*$// p' $ETC/group` -if [ "$GROUPS" ]; then - echo "User '$1' is still part of these groups:" $GROUPS +GRPS=`(grep -E '^[^:]+:[^:]+:[^:]+.*(:|,)'"$1"'(,|$)' "$ETC/group" || :) | cut -d : -f 1` +if [ "$GRPS" ]; then + echo "User '$1' is still part of these groups:" $GRPS echo "fatal: this simplistic script cannot remove users from groups." exit 1 fi -sed -i "/^$1:/ d" $ETC/passwd -rm $ETC/sshkeys/$1 -rm -f $ETC/sshcerts/"$cfg_nickname"_"$1"_user_*.pem +sed -i.$$ -e "/^$1:/ d" "$ETC/passwd" +rm -f "$ETC/passwd.$$" || : +rm -f "$ETC/sshkeys/$1" || : +rm -f "$ETC/sshcerts/$cfg_nickname"_"$1"_user_*.pem || : +! [ -e "$ETC/sshkeys/$1" ] || echo "Warning: unable to remove $ETC/sshkeys/$1" >&2 +[ "$(echo "$ETC/sshcerts/$cfg_nickname"_"$1"_user_*.pem)" = "$ETC/sshcerts/$cfg_nickname"_"$1"_user_"*".pem ] || + echo "Warning: unable to remove $(echo "$ETC/sshcerts/$cfg_nickname"_"$1"_user_*.pem)" >&2 echo "User (+SSH key/certs) removed." -- 2.11.4.GIT