1
0
forked from pool/sysuser-tools
sysuser-tools/sysusers2shadow.sh

84 lines
2.0 KiB
Bash
Raw Normal View History

#!/bin/sh
set -eu
# 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
do
case "$arg0" in
g)
ARGUMENTS="${arg1}"
if [ -n "${arg2}" -a "${arg2}" != "-" ]; then
ARGUMENTS="-g ${arg2} $ARGUMENTS"
fi
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
fi
;;
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
fi
;;
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
;;
r)
echo "range option ignored: \"$arg0 $arg1 $arg2 $arg3\""
;;
*)
echo "Syntax Error: \"$arg0\""
exit 1
;;
esac
done