2020-03-20 12:31:21 +01:00
|
|
|
#!/bin/sh
|
2021-02-15 08:31:11 +01:00
|
|
|
set -e
|
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-02-11 13:54:07 +01:00
|
|
|
if [ -x /usr/bin/systemd-sysusers ] && [ -e /proc/version ]; then
|
2020-03-20 12:31:21 +01:00
|
|
|
|
2021-06-07 15:14:59 +02:00
|
|
|
if [ -n "$1" ] && [ "$1" != "%3" ]; then
|
2021-02-11 14:54:52 +01:00
|
|
|
REPLACE_ARG="--replace=/usr/lib/sysusers.d/$1" ||:
|
|
|
|
fi
|
2021-01-15 11:59:15 +01:00
|
|
|
# Use systemd-sysusers and let it read the input directly from stdin
|
2021-06-07 15:14:59 +02:00
|
|
|
run /usr/bin/systemd-sysusers $REPLACE_ARG -
|
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
|
|
|
\#*|"")
|
2021-02-11 13:54:07 +01:00
|
|
|
;;
|
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
|
|
|
|
|
2021-09-08 16:31:16 +02:00
|
|
|
# Set shell only if not null, empty nor '-'
|
|
|
|
if [ "${5:--}" != "-" ]; then
|
|
|
|
ARGUMENTS="$ARGUMENTS -s $5"
|
|
|
|
else
|
|
|
|
ARGUMENTS="$ARGUMENTS -s /usr/sbin/nologin"
|
|
|
|
fi
|
|
|
|
|
2020-03-20 12:31:21 +01:00
|
|
|
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
|
|
|
|
2021-09-08 16:31:16 +02:00
|
|
|
run /usr/sbin/useradd -r -c "$3" -d "${homedir}" $ARGUMENTS
|
2021-01-15 11:59:15 +01:00
|
|
|
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
|
2021-09-08 16:31:16 +02:00
|
|
|
run $busybox adduser -S -H -g "$3" -G "$1" -h "${homedir}" $ARGUMENTS
|
2021-01-15 11:59:15 +01:00
|
|
|
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
|