sysuser-tools/sysusers2shadow.sh
2019-12-29 21:30:37 +00:00

97 lines
3.2 KiB
Bash

#!/bin/bash
while read LINE
do
case "$LINE" in
\#*|"")
;;
g*)
eval arr=( $LINE )
ARGUMENTS="${arr[1]}"
if [ ! -z "${arr[2]}" -a "${arr[2]}" != "-" ]; then
ARGUMENTS="-g ${arr[2]} $ARGUMENTS"
fi
if [ -x /usr/sbin/groupadd ]; then
echo "groupadd -r $ARGUMENTS"
/usr/bin/getent group "${arr[1]}" >> /dev/null || /usr/sbin/groupadd -r $ARGUMENTS || exit $?
elif [ -x /usr/bin/busybox ]; then
echo "addgroup -S $ARGUMENTS"
/usr/bin/getent group "${arr[1]}" >> /dev/null || /usr/bin/busybox addgroup -S $ARGUMENTS || exit $?
elif [ -x /bin/busybox ]; then
echo "addgroup -S $ARGUMENTS"
/usr/bin/getent group "${arr[1]}" >> /dev/null || /bin/busybox addgroup -S $ARGUMENTS || exit $?
else
echo "ERROR: neither groupadd nor busybox found!"
exit 1
fi
;;
u*)
eval arr=( $LINE )
ARGUMENTS="${arr[1]}"
if [ ! -z "${arr[2]}" -a "${arr[2]}" != "-" ]; then
ARGUMENTS="-u ${arr[2]} $ARGUMENTS"
fi
if [ ! -z "${arr[4]}" -a "${arr[4]}" != "-" ]; then
ARGUMENTS="-d ${arr[4]} $ARGUMENTS"
else
ARGUMENTS="-d / $ARGUMENTS"
fi
if [ -x /usr/sbin/useradd ]; then
# this is useradd/shadow specific
/usr/bin/getent group ${arr[1]} >> /dev/null
if [ $? -eq 0 ]; then
ARGUMENTS="-g ${arr[1]} $ARGUMENTS"
else
ARGUMENTS="-U $ARGUMENTS"
fi
echo "useradd -r -s /sbin/nologin -c \"${arr[3]}\" $ARGUMENTS"
/usr/bin/getent passwd ${arr[1]} >> /dev/null || /usr/sbin/useradd -r -s /sbin/nologin -c "${arr[3]}" $ARGUMENTS || exit $?
elif [ -x /usr/bin/busybox ]; then
/usr/bin/getent group ${arr[1]} >> /dev/null
if [ $? -ne 0 ]; then
/usr/bin/busybox addgroup ${arr[1]}
fi
ARGUMENTS="-G ${arr[1]} $ARGUMENTS"
ARGUMENTS=`echo $ARGUMENTS | sed -e 's|-d|-h|g' -e 's|-g|-G|g'`
echo "adduser -S -s /sbin/nologin -g \"${arr[3]}\" $ARGUMENTS"
/usr/bin/getent passwd ${arr[1]} >> /dev/null || /usr/bin/busybox adduser -S -s /sbin/nologin -g "${arr[3]}" $ARGUMENTS || exit $?
elif [ -x /bin/busybox ]; then
/usr/bin/getent group ${arr[1]} >> /dev/null
if [ $? -ne 0 ]; then
/bin/busybox addgroup ${arr[1]}
fi
ARGUMENTS="-G ${arr[1]} $ARGUMENTS"
ARGUMENTS=`echo $ARGUMENTS | sed -e 's|-d|-h|g' -e 's|-g|-G|g'`
echo "adduser -S -s /sbin/nologin -g \"${arr[3]}\" $ARGUMENTS"
/usr/bin/getent passwd ${arr[1]} >> /dev/null || /bin/busybox adduser -S -s /sbin/nologin -g "${arr[3]}" $ARGUMENTS || exit $?
else
echo "ERROR: neither useradd nor busybox found!"
exit 1
fi
;;
m*)
eval arr=( $LINE )
if [ -x /usr/sbin/usermod ] ; then
echo "usermod -a -G ${arr[2]} ${arr[1]}"
/usr/sbin/usermod -a -G ${arr[2]} ${arr[1]} || exit $?
elif [ -x /usr/bin/busybox ]; then
echo "addgroup ${arr[1]} ${arr[2]}"
/usr/bin/busybox addgroup ${arr[1]} ${arr[2]} || exit $?
elif [ -x /bin/busybox ]; then
echo "addgroup ${arr[1]} ${arr[2]}"
/bin/busybox addgroup ${arr[1]} ${arr[2]} || exit $?
else
echo "ERROR: neither usermod nor busybox found!"
exit 1
fi
;;
r*)
echo "range option ignored: \"$LINE\""
;;
*)
echo "Syntax Error: \"$LINE\""
exit 1
;;
esac
done