#!/bin/sh set -e cd $(dirname $0) # Rational: # Test the -u option of useradd (we can specify the new user UID) # Check that we can't create an user with the same UID without the -o # option. save() { [ ! -d tmp ] && mkdir tmp for i in passwd group shadow gshadow do [ -f /etc/$i ] && cp /etc/$i tmp/$i done true } restore() { for i in passwd group shadow gshadow do [ -f tmp/$i ] && cp tmp/$i /etc/$i && rm tmp/$i done rmdir tmp } save # restore the files on exit trap 'restore' 0 cp data/{passwd,shadow,group,gshadow} /etc/ lines_passwd=$(wc -l /etc/passwd | cut -f1 -d" ") lines_shadow=$(wc -l /etc/shadow | cut -f1 -d" ") lines_group=$(wc -l /etc/group | cut -f1 -d" ") lines_gshadow=$(wc -l /etc/gshadow | cut -f1 -d" ") echo -n "Create user test4..." useradd -u 4242 test4 echo "OK" echo "test if the user was added with the right uid" echo -n " passwd..." getent passwd test4 | egrep "^test4:x:4242:4242::/home/test4:/bin/sh$" echo " OK" echo -n " group..." getent group test4 | egrep "^test4:x:4242:$" echo " OK" echo -n " shadow..." getent shadow test4 | egrep "^test4:!:[0-9]+:0:99999:7:::$" echo " OK" echo -n " gshadow..." egrep "^test4:!::$" /etc/gshadow echo " OK" # the home directory should not exist echo -n " no homedir..." test -d /home/test4 && exit 1 || true echo " OK" echo -n " number of lines" test $(( lines_passwd + 1 )) = $(wc -l /etc/passwd | cut -f1 -d" ") echo -n "." test $(( lines_group + 1 )) = $(wc -l /etc/group | cut -f1 -d" ") echo -n "." test $(( lines_shadow + 1 )) = $(wc -l /etc/shadow | cut -f1 -d" ") echo -n "." test $(( lines_gshadow + 1 )) = $(wc -l /etc/gshadow | cut -f1 -d" ") echo -n "." echo " OK" echo "do NOT delete this user" echo -n "Create user test5 with the same uid..." useradd -u 4242 test5 && exit 1 || true echo "All right, useradd return an error." echo "This user should not exist" echo -n " no entries..." egrep "^test5:" \ /etc/passwd /etc/group /etc/shadow /etc/gshadow && exit 1 || true echo " OK" echo -n " no homedir..." test -d /home/test5 && exit 1 || true echo " OK" echo -n " number of lines" test $(( lines_passwd + 1 )) = $(wc -l /etc/passwd | cut -f1 -d" ") echo -n "." test $(( lines_group + 1 )) = $(wc -l /etc/group | cut -f1 -d" ") echo -n "." test $(( lines_shadow + 1 )) = $(wc -l /etc/shadow | cut -f1 -d" ") echo -n "." test $(( lines_gshadow + 1 )) = $(wc -l /etc/gshadow | cut -f1 -d" ") echo -n "." echo " OK" echo -n "Create user test6 with the same uid..." useradd -o -u 4242 test6 echo "OK" echo "test if the user was added with the right uid" echo -n " passwd..." getent passwd test6 | egrep "^test6:x:4242:4243::/home/test6:/bin/sh$" echo " OK" echo -n " group..." getent group test6 | egrep "^test6:x:4243:$" echo " OK" echo -n " shadow..." getent shadow test6 | egrep "^test6:!:[0-9]+:0:99999:7:::$" echo " OK" echo -n " gshadow..." egrep "^test6:!::$" /etc/gshadow echo " OK" # the home directory should not exist echo -n " no homedir..." test -d /home/test6 && exit 1 || true echo " OK" echo -n " number of lines" test $(( lines_passwd + 2 )) = $(wc -l /etc/passwd | cut -f1 -d" ") echo -n "." test $(( lines_group + 2 )) = $(wc -l /etc/group | cut -f1 -d" ") echo -n "." test $(( lines_shadow + 2 )) = $(wc -l /etc/shadow | cut -f1 -d" ") echo -n "." test $(( lines_gshadow + 2 )) = $(wc -l /etc/gshadow | cut -f1 -d" ") echo -n "." echo " OK" echo -n "delete user test4..." userdel test4 echo "OK" echo "check if the user was deleted" echo -n " no entries..." egrep "^test4:" \ /etc/passwd /etc/group /etc/shadow /etc/gshadow && exit 1 || true echo " OK" echo -n " no homedir..." test -d /home/test4 && exit 1 || true echo " OK" echo -n " number of lines" test $(( lines_passwd + 1 )) = $(wc -l /etc/passwd | cut -f1 -d" ") echo -n "." test $(( lines_group + 1 )) = $(wc -l /etc/group | cut -f1 -d" ") echo -n "." test $(( lines_shadow + 1 )) = $(wc -l /etc/shadow | cut -f1 -d" ") echo -n "." test $(( lines_gshadow + 1 )) = $(wc -l /etc/gshadow | cut -f1 -d" ") echo -n "." echo " OK" echo -n "delete user test6..." userdel test6 echo "OK" echo "check if the user was deleted" echo -n " no entries..." egrep "^test6:" \ /etc/passwd /etc/group /etc/shadow /etc/gshadow && exit 1 || true echo " OK" echo -n " no homedir..." test -d /home/test6 && exit 1 || true echo " OK" echo -n " number of lines" test $lines_passwd = $(wc -l /etc/passwd | cut -f1 -d" ") echo -n "." test $lines_group = $(wc -l /etc/group | cut -f1 -d" ") echo -n "." test $lines_shadow = $(wc -l /etc/shadow | cut -f1 -d" ") echo -n "." test $lines_gshadow = $(wc -l /etc/gshadow | cut -f1 -d" ") echo -n "." echo " OK"