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 "$@"
|
|
|
|
"$@"
|
|
|
|
}
|
|
|
|
|
2021-01-15 11:59:15 +01:00
|
|
|
if [ -x /usr/bin/systemd-sysusers ]; then
|
2020-03-20 12:31:21 +01:00
|
|
|
|
2021-01-15 11:59:15 +01:00
|
|
|
# Use systemd-sysusers and let it read the input directly from stdin
|
|
|
|
/usr/bin/systemd-sysusers -
|
2021-01-19 16:20:59 +01:00
|
|
|
# We need to clear the nscd cache, as systemd-sysusers could have
|
|
|
|
# triggered a negative cache entry
|
|
|
|
if [ -x /usr/sbin/nscd ]; then
|
2021-01-20 16:08:26 +01:00
|
|
|
/usr/sbin/nscd -i passwd ||:
|
|
|
|
/usr/sbin/nscd -i group ||:
|
2021-01-19 16:20:59 +01:00
|
|
|
fi
|
2021-01-15 11:59:15 +01:00
|
|
|
else
|
|
|
|
|
|
|
|
# Absolute path to busybox, if found
|
|
|
|
busybox=
|
|
|
|
for i in /bin/busybox /usr/bin/busybox; do [ -x "$i" ] && busybox=$i; done
|
|
|
|
|
|
|
|
while read LINE
|
|
|
|
do
|
2020-09-22 10:24:31 +02:00
|
|
|
# "eval set" to do proper splitting while respecting quotes
|
2020-03-24 12:54:36 +01:00
|
|
|
eval set -- $LINE
|
|
|
|
case "${1-}" in
|
2021-01-15 11:59:15 +01:00
|
|
|
\#*|"")
|
|
|
|
;;
|
|
|
|
g)
|
2020-03-24 12:54:36 +01:00
|
|
|
shift
|
|
|
|
ARGUMENTS="$1"
|
|
|
|
if [ -n "${2-}" ] && [ "$2" != "-" ]; then
|
2021-01-15 11:59:15 +01:00
|
|
|
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
|
2021-01-15 11:59:15 +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
|
|
|
;;
|
2021-01-15 11:59:15 +01:00
|
|
|
u)
|
2020-03-24 12:54:36 +01:00
|
|
|
shift
|
|
|
|
ARGUMENTS="$1"
|
|
|
|
if [ -n "${2-}" ] && [ "$2" != "-" ]; then
|
2021-01-15 11:59:15 +01:00
|
|
|
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
|
2021-01-15 11:59:15 +01:00
|
|
|
homedir="$4"
|
2020-03-20 12:31:21 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -x /usr/sbin/useradd ]; then
|
2021-01-15 11:59:15 +01:00
|
|
|
if ! /usr/bin/getent passwd "$1" >> /dev/null; then
|
|
|
|
# this is useradd/shadow specific
|
|
|
|
if /usr/bin/getent group "$1" >> /dev/null; then
|
|
|
|
ARGUMENTS="-g $1 $ARGUMENTS"
|
|
|
|
else
|
|
|
|
ARGUMENTS="-U $ARGUMENTS"
|
2020-03-20 12:31:21 +01:00
|
|
|
fi
|
2021-01-15 11:59:15 +01:00
|
|
|
|
|
|
|
run /usr/sbin/useradd -r -s /sbin/nologin -c "$3" -d "${homedir}" $ARGUMENTS
|
|
|
|
fi
|
2020-03-20 12:31:21 +01:00
|
|
|
elif [ -x "$busybox" ]; then
|
2021-01-15 11:59:15 +01:00
|
|
|
/usr/bin/getent group "$1" >> /dev/null || $busybox addgroup -S "$1"
|
2020-03-20 12:31:21 +01:00
|
|
|
|
2021-01-15 11:59:15 +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
|
|
|
|
fi
|
2020-03-20 12:31:21 +01:00
|
|
|
else
|
2021-01-15 11:59:15 +01:00
|
|
|
echo "ERROR: neither useradd nor busybox found!"
|
|
|
|
exit 1
|
2019-12-29 22:30:37 +01:00
|
|
|
fi
|
2021-01-15 11:59:15 +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
|
2021-01-15 11:59:15 +01:00
|
|
|
run /usr/sbin/usermod -a -G $2 $1
|
2020-03-20 12:31:21 +01:00
|
|
|
elif [ -x "$busybox" ]; then
|
2021-01-15 11:59:15 +01:00
|
|
|
run $busybox addgroup $1 $2
|
2020-03-20 12:31:21 +01:00
|
|
|
else
|
2021-01-15 11:59:15 +01:00
|
|
|
echo "ERROR: neither usermod nor busybox found!"
|
|
|
|
exit 1
|
2020-03-20 12:31:21 +01:00
|
|
|
fi
|
2021-01-15 11:59:15 +01:00
|
|
|
;;
|
|
|
|
r)
|
2020-03-24 12:54:36 +01:00
|
|
|
echo "range option ignored: \"$LINE\""
|
2020-03-20 12:31:21 +01:00
|
|
|
;;
|
2021-01-15 11:59:15 +01:00
|
|
|
*)
|
|
|
|
echo "Syntax Error: \"$LINE\""
|
|
|
|
exit 1
|
|
|
|
;;
|
2020-03-20 12:31:21 +01:00
|
|
|
esac
|
2021-01-15 11:59:15 +01:00
|
|
|
done
|
|
|
|
fi
|