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
|
|
|
|
|
|
|
|
while read arg0 arg1 arg2 arg3 arg4
|
2017-06-19 08:26:55 +02:00
|
|
|
do
|
2020-03-20 12:31:21 +01:00
|
|
|
case "$arg0" in
|
|
|
|
g)
|
|
|
|
ARGUMENTS="${arg1}"
|
|
|
|
if [ -n "${arg2}" -a "${arg2}" != "-" ]; then
|
|
|
|
ARGUMENTS="-g ${arg2} $ARGUMENTS"
|
2019-12-29 22:30:37 +01:00
|
|
|
fi
|
2020-03-20 12:31:21 +01:00
|
|
|
|
|
|
|
if ! /usr/bin/getent group "${arg1}" >> /dev/null; then
|
|
|
|
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)
|
|
|
|
ARGUMENTS="${arg1}"
|
|
|
|
if [ -n "${arg2}" ] && [ "${arg2}" != "-" ]; then
|
|
|
|
ARGUMENTS="-u ${arg2} $ARGUMENTS"
|
|
|
|
fi
|
|
|
|
if [ -n "${arg4}" ] && [ "${arg4}" != "-" ]; then
|
|
|
|
ARGUMENTS="-d ${arg4} $ARGUMENTS"
|
|
|
|
else
|
|
|
|
ARGUMENTS="-d / $ARGUMENTS"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -x /usr/sbin/useradd ]; then
|
|
|
|
if ! /usr/bin/getent passwd "${arg1}" >> /dev/null; then
|
|
|
|
# this is useradd/shadow specific
|
|
|
|
ARGUMENTS="-g ${arg1} $ARGUMENTS"
|
|
|
|
/usr/bin/getent group "${arg1}" >> /dev/null || ARGUMENTS="-U $ARGUMENTS"
|
|
|
|
|
|
|
|
run /usr/sbin/useradd -r -s /sbin/nologin -c "${arg3}" $ARGUMENTS
|
|
|
|
fi
|
|
|
|
elif [ -x "$busybox" ]; then
|
|
|
|
/usr/bin/getent group "${arg1}" >> /dev/null || $busybox addgroup "${arg1}"
|
|
|
|
|
|
|
|
if ! /usr/bin/getent passwd ${arg1} >> /dev/null; then
|
|
|
|
ARGUMENTS="$(echo -G ${arg1} $ARGUMENTS | sed -e 's|-d|-h|g' -e 's|-g|-G|g')"
|
|
|
|
run $busybox adduser -S -H -s /sbin/nologin -g "${arg3}" $ARGUMENTS
|
|
|
|
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)
|
|
|
|
if [ -x /usr/sbin/usermod ] ; then
|
|
|
|
run /usr/sbin/usermod -a -G ${arg2} ${arg1}
|
|
|
|
elif [ -x "$busybox" ]; then
|
|
|
|
run $busybox addgroup ${arg1} ${arg2}
|
|
|
|
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)
|
|
|
|
echo "range option ignored: \"$arg0 $arg1 $arg2 $arg3\""
|
|
|
|
;;
|
2017-06-19 08:26:55 +02:00
|
|
|
*)
|
2020-03-20 12:31:21 +01:00
|
|
|
echo "Syntax Error: \"$arg0\""
|
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
|