Files
K D Hedger 371c740efa ...
2021-10-11 12:09:01 +01:00

173 lines
3.0 KiB
Bash
Executable File

#!/bin/bash -e
#©keithhedger Mon 18 Nov 12:41:23 GMT 2019 kdhedger68713@gmail.com
. /usr/share/LFSPkg/LFSFunctions
getShell ()
{
local upreply
read -p "Select shell ( /bin/bash ) "
if [ "$REPLY" = "" ];then
USERSHELL="/bin/bash"
else
USERSHELL="$REPLY"
fi
}
getUIDS ()
{
local upreply
local uids
read -p "Is this a system user ? (y/N) "
upreply=${REPLY^^}
if [ "$upreply" = "Y" ];then
uids=$(findnextpair "system")
USERHOME="/dev/null"
USERSHELL=/bin/false
ISSYSTEM="Y"
else
uids=$(findnextpair)
USERHOME="/home/$LOGINNAME"
getShell
addGroups
fi
read -p "Enter User uid ( ${uids} ) "
if [ "$REPLY" = "" ];then
USEUIDS=$uids
else
USEUIDS=$REPLY
fi
}
getGids ()
{
local upreply
local uids
read -p "Is this a system group ? (y/N) "
upreply=${REPLY^^}
if [ "$upreply" = "Y" ];then
uids=$(findnextgid "system")
ISSYSTEM="-r"
else
ISSYSTEM=""
uids=$(findnextgid)
fi
read -p "Enter Group gid ( ${uids} ) "
if [ "$REPLY" = "" ];then
USEUIDS=$uids
else
USEUIDS=$REPLY
fi
}
addGroups ()
{
local upreply
read -p "Add new user to all groups? (y/N) "
upreply=${REPLY^^}
if [ "$upreply" = "Y" ];then
XTRAGROUPS="$(sed -n '4,999p' /etc/group|sed '/^[[:space:]]*$/d'| awk -F: '{print $1}'|tr "\n" ",")"
XTRAGROUPS=${XTRAGROUPS%*,}
else
XTRAGROUPS="audio,cdrom,floppy,plugdev,video,usb,tty,users"
fi
}
if [ "$1" = "" ];then
echo "Must supply login name ..."
exit 1
fi
LOGINNAME="${1,,}"
DRYRUN=$2
ISSYSTEM="N"
TYPE=""
DELETEFILES="N"
select arg in "Add User" "Delete User" "Add Group" "Delete Group" "Quit"
do
case $arg in
"Add User")
getUIDS
TYPE="ADDUSER"
break
;;
"Delete User")
TYPE="DELUSER"
read -p "Remove Files? (y/N) "
if [ "${REPLY^^}" = "Y" ];then
DELETEFILES="r"
else
DELETEFILES=""
fi
break
;;
"Add Group")
TYPE="ADDGROUP"
break
;;
"Delete Group")
TYPE="DELGROUP"
break
;;
"Quit")
exit 0
;;
esac
done
if [ ! "$DRYRUN" = "" ];then
case $TYPE in
"ADDUSER")
if [ "$ISSYSTEM" = "Y" ];then
echo useradd -r -d /dev/null -U -s /bin/false -u \"$USEUIDS\" \"$LOGINNAME\"||true
else
echo useradd -d \"$USERHOME\" -m -U -u \"$USEUIDS\" -G \"$XTRAGROUPS\" -s \"$USERSHELL\" \"$LOGINNAME\"
echo passwd $LOGINNAME
fi
;;
"DELUSER")
echo userdel -f$DELETEFILES \"$LOGINNAME\"
;;
"ADDGROUP")
getGids
echo groupadd -g \"$USEUIDS\" $ISSYSTEM \"$LOGINNAME\"
;;
"DELGROUP")
echo groupdel \"$LOGINNAME\"
;;
esac
else
case $TYPE in
"ADDUSER")
if [ "$ISSYSTEM" = "Y" ];then
useradd -r -d /dev/null -U -s /bin/false -u "$USEUIDS" "$LOGINNAME"||true
else
useradd -d "$USERHOME" -m -u "$USEUIDS" -U -G "$XTRAGROUPS" -s "$USERSHELL" "$LOGINNAME"
passwd $LOGINNAME
fi
;;
"DELUSER")
userdel -f$DELETEFILES "$LOGINNAME"
;;
"ADDGROUP")
getGids
groupadd -g "$USEUIDS" $ISSYSTEM "$LOGINNAME"
;;
"DELGROUP")
groupdel "$LOGINNAME"
;;
esac
fi