clone/update: mark project changed on failure
[girocco.git] / toolbox / remove-user.sh
blobf6b61d02e7e2fe8b4ba3574b4f8c337e9280e8d5
1 #!/bin/sh
3 set -e
5 . @basedir@/shlib.sh
7 if [ -z "$1" ]; then
8 echo "Usage: $0 <username>"
9 exit 1
11 u="$1"
12 qu="$(printf '%s' "$u" | sed -e 's/\./\\./g' -e 's/+/[+]/g')"
13 ETC="$cfg_chroot/etc"
14 COUNT="$(grep -E -c "^$qu:" "$ETC/passwd")" || :
15 if [ "$COUNT" -ne "1" ]; then
16 echo "fatal: user '$u' doesn't appear to exist (or exists multiple times, or contains regexpy characters)."
17 exit 1
19 ENTRY="$(grep -E "^$qu:" "$ETC/passwd" | cut -d : -f 1-5)"
20 GRPS="$( (grep -E '^[^:]+:[^:]+:[^:]+.*(:|,)'"$qu"'(,|:|$)' "$ETC/group" || :) | cut -d : -f 1 )" || :
21 if [ "$GRPS" ]; then
22 echo "User '$u' is still part of these groups:" $GRPS
23 echo "fatal: this simplistic script cannot remove users from groups."
24 exit 1
26 sed -e "/^$qu:/ d" "$ETC/passwd" > "$ETC/passwd.$$"
27 mv -f "$ETC/passwd.$$" "$ETC/passwd" || :
28 rm -f "$ETC/passwd.$$" || :
29 rm -f "$ETC/sshkeys/$u" || :
30 rm -f "$ETC/sshcerts/$cfg_nickname"_"$u"_user_*.pem || :
31 rm -f "$ETC/sshactive/$u" || :
32 rm -f "$ETC/sshactive/$u",* || :
33 ! [ -e "$ETC/sshkeys/$u" ] || echo "Warning: unable to remove $ETC/sshkeys/$u" >&2
34 [ "$(echo "$ETC/sshcerts/$cfg_nickname"_"$u"_user_*.pem)" = "$ETC/sshcerts/$cfg_nickname"_"$u"_user_"*".pem ] ||
35 echo "Warning: unable to remove $(echo "$ETC/sshcerts/$cfg_nickname"_"$u"_user_*.pem)" >&2
36 if [ -n "$cfg_update_pwd_db" ] && [ "$cfg_update_pwd_db" != "0" ]; then
37 "$cfg_basedir/bin/update-pwd-db" "$ETC/passwd" "$u"
39 echo "User \"$ENTRY\" (+SSH key/certs) removed."