2020-03-20 12:31:21 +01:00
|
|
|
#!/bin/sh
|
|
|
|
set -eu
|
2017-06-19 08:26:55 +02:00
|
|
|
|
2020-03-20 12:31:21 +01:00
|
|
|
# Print the command and run it
|
|
|
|
run() {
|
|
|
|
echo "$@"
|
|
|
|
"$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Absolute path to busybox, if found
|
|
|
|
busybox=
|
|
|
|
for i in /bin/busybox /usr/bin/busybox; do [ -x "$i" ] && busybox=$i; done
|
|
|
|
|
2020-03-24 12:54:36 +01:00
|
|
|
while read LINE
|
2017-06-19 08:26:55 +02:00
|
|
|
do
|
2020-03-24 12:54:36 +01:00
|
|
|
eval set -- $LINE
|
|
|
|
case "${1-}" in
|
|
|
|
\#*|"")
|
|
|
|
;;
|
2020-03-20 12:31:21 +01:00
|
|
|
g)
|
2020-03-24 12:54:36 +01:00
|
|
|
shift
|
|
|
|
ARGUMENTS="$1"
|
|
|
|
if [ -n "${2-}" ] && [ "$2" != "-" ]; then
|
|
|
|
ARGUMENTS="-g $2 $ARGUMENTS"
|
2019-12-29 22:30:37 +01:00
|
|
|
fi
|
2020-03-20 12:31:21 +01:00
|
|
|
|
2020-03-24 12:54:36 +01:00
|
|
|
if ! /usr/bin/getent group "$1" >> /dev/null; then
|
2020-03-20 12:31:21 +01:00
|
|
|
if [ -x "/usr/sbin/groupadd" ]; then
|
|
|
|
run /usr/sbin/groupadd -r $ARGUMENTS
|
|
|
|
elif [ -x "$busybox" ]; then
|
|
|
|
run $busybox addgroup -S $ARGUMENTS
|
|
|
|
else
|
|
|
|
echo "ERROR: neither groupadd nor busybox found!"
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-12-29 22:30:37 +01:00
|
|
|
fi
|
2020-03-20 12:31:21 +01:00
|
|
|
;;
|
|
|
|
u)
|
2020-03-24 12:54:36 +01:00
|
|
|
shift
|
|
|
|
ARGUMENTS="$1"
|
|
|
|
if [ -n "${2-}" ] && [ "$2" != "-" ]; then
|
|
|
|
ARGUMENTS="-u $2 $ARGUMENTS"
|
2020-03-20 12:31:21 +01:00
|
|
|
fi
|
2020-03-25 11:07:14 +01:00
|
|
|
homedir="/" # If null, empty or '-'
|
|
|
|
if [ "${4:--}" != "-" ]; then
|
|
|
|
homedir="$4"
|
2020-03-20 12:31:21 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -x /usr/sbin/useradd ]; then
|
2020-03-24 12:54:36 +01:00
|
|
|
if ! /usr/bin/getent passwd "$1" >> /dev/null; then
|
2020-03-20 12:31:21 +01:00
|
|
|
# this is useradd/shadow specific
|
2020-03-25 11:07:14 +01:00
|
|
|
if /usr/bin/getent group "$1" >> /dev/null; then
|
|
|
|
ARGUMENTS="-g $1 $ARGUMENTS"
|
|
|
|
else
|
|
|
|
ARGUMENTS="-U $ARGUMENTS"
|
|
|
|
fi
|
2020-03-20 12:31:21 +01:00
|
|
|
|
2020-03-25 11:07:14 +01:00
|
|
|
run /usr/sbin/useradd -r -s /sbin/nologin -c "$3" -d "${homedir}" $ARGUMENTS
|
2020-03-20 12:31:21 +01:00
|
|
|
fi
|
|
|
|
elif [ -x "$busybox" ]; then
|
2020-03-24 12:54:36 +01:00
|
|
|
/usr/bin/getent group "$1" >> /dev/null || $busybox addgroup "$1"
|
2020-03-20 12:31:21 +01:00
|
|
|
|
2020-03-25 11:07:14 +01:00
|
|
|
if ! /usr/bin/getent passwd "$1" >> /dev/null; then
|
|
|
|
run $busybox adduser -S -H -s /sbin/nologin -g "$3" -G "$1" -h "${homedir}" $ARGUMENTS
|
2020-03-20 12:31:21 +01:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "ERROR: neither useradd nor busybox found!"
|
|
|
|
exit 1
|
2019-12-29 22:30:37 +01:00
|
|
|
fi
|
2017-06-19 08:26:55 +02:00
|
|
|
;;
|
2020-03-20 12:31:21 +01:00
|
|
|
m)
|
2020-03-24 12:54:36 +01:00
|
|
|
shift
|
2020-03-20 12:31:21 +01:00
|
|
|
if [ -x /usr/sbin/usermod ] ; then
|
2020-03-24 12:54:36 +01:00
|
|
|
run /usr/sbin/usermod -a -G $2 $1
|
2020-03-20 12:31:21 +01:00
|
|
|
elif [ -x "$busybox" ]; then
|
2020-03-24 12:54:36 +01:00
|
|
|
run $busybox addgroup $1 $2
|
2020-03-20 12:31:21 +01:00
|
|
|
else
|
|
|
|
echo "ERROR: neither usermod nor busybox found!"
|
|
|
|
exit 1
|
|
|
|
fi
|
2017-06-19 08:26:55 +02:00
|
|
|
;;
|
2020-03-20 12:31:21 +01:00
|
|
|
r)
|
2020-03-24 12:54:36 +01:00
|
|
|
echo "range option ignored: \"$LINE\""
|
2020-03-20 12:31:21 +01:00
|
|
|
;;
|
2017-06-19 08:26:55 +02:00
|
|
|
*)
|
2020-03-24 12:54:36 +01:00
|
|
|
echo "Syntax Error: \"$LINE\""
|
2017-06-19 08:26:55 +02:00
|
|
|
exit 1
|
|
|
|
;;
|
2020-03-20 12:31:21 +01:00
|
|
|
esac
|
2017-06-19 08:26:55 +02:00
|
|
|
done
|