autouser/autouser.sh

330 lines
6.9 KiB
Bash
Raw Normal View History

2024-11-12 17:09:38 +01:00
#!/bin/bash
FILE="${@: -1}"
checkFile ()
{
2024-11-12 17:09:38 +01:00
if [[ ! -f "$FILE" ]]; then
echo "File \"$FILE\" doesn't exist!"
exit
fi
}
2024-11-12 17:09:38 +01:00
# User '.csv' columns
COL_USERNAME="1"
COL_FULLNAME="2"
COL_USERUID="3"
COL_USERGID="4"
COL_USERGROUPS="5"
COL_TLFN="6"
COL_EXTRAINFO="7"
COL_USERSHELL="8"
COL_USERLOCKED="9"
# Group '.csv' columns
COL_GROUPNAME="1"
COL_GROUPGID="2"
# Control variables
2024-11-12 17:09:38 +01:00
DRYRUN=false
FILE_CONTENTS="$(cat ${@: -1} 2>/dev/null)"
ACTION_TO_TAKE=""
2024-11-12 17:09:38 +01:00
DELIM=";"
usage ()
{
echo "Usage: ./autouser.sh \$PARAMETER file.csv"
echo ""
echo "PARAMETERS"
echo " --help || -h (Show this help prompt)"
echo " --dry-run (Test without executing any changes)"
echo " --create-users users.csv (Create users from a .csv file)"
echo " --create-groups groups.csv (Create groups from a .csv file)"
echo " --delete-users users.csv (Delete users from a .csv file)"
echo " --delete-groups groups.csv (Delete groups from a .csv file)"
echo " --create-group (Create group manually)"
echo " --create-user (Create user manually)"
echo " --delete-group (Delete group manually)"
echo " --delete-user (Delete user manually)"
2024-11-12 17:09:38 +01:00
exit
}
createGroup() {
if [[ "$DRYRUN" == true ]]; then
read -p "Enter group name: " GROUPNAME
echo "groupadd $GROUPNAME"
else
rootCheck
read -p "Enter group name: " GROUPNAME
groupadd $GROUPNAME
fi
}
createUser() {
COMMAND="useradd -m "
read -p "Username [Obligatory]: " USERNAME
if [[ "$USERNAME" == "" ]]; then
echo "Username cannot be left blank!"
exit
fi
read -p "UID [Optional]: " uid
read -p "Primary group [Optional]: " PGROUP
read -p "Secondary groups [Optional]: " SGROUPS
if [[ "$uid" != "" ]]; then
COMMAND+="-u $uid "
fi
if [[ "$PGROUP" != "" ]]; then
COMMAND+="-g $PGROUP "
fi
if [[ "$SGROUPS" != "" ]]; then
COMMAND+="-G $SGROUPS "
fi
COMMAND+="$USERNAME"
if [[ "$DRYRUN" == true ]]; then
echo "$COMMAND"
else
rootCheck
$COMMAND
fi
}
deleteGroup() {
if [[ "$DRYRUN" == true ]]; then
read -p "Enter group name: " GROUPNAME
echo "groupadd $GROUPNAME"
else
rootCheck
read -p "Enter group name: " GROUPNAME
groupadd $GROUPNAME
fi
}
deleteUser() {
if [[ "$DRYRUN" == true ]]; then
read -p "Enter username: " USERNAME
echo "userdel -r $USERNAME"
else
rootCheck
read -p "Enter username: " USERNAME
userdel -r $USERNAME
fi
}
2024-11-12 17:09:38 +01:00
rootCheck ()
{
if [[ "$(id -u)" -ne 0 ]]; then
echo "You must be root to run this parameter"
exit
fi
}
cutter ()
{
echo "$1" | cut -d "$DELIM" -f "$2"
}
2024-11-18 16:31:20 +01:00
turnLowercase()
{
2024-11-12 17:09:38 +01:00
echo "$1" | tr "[:upper:]" "[:lower:]" | sed 's/ñ/n/g'
}
createGroups ()
{
checkFile
COMMAND=""
if [[ "$DRYRUN" == false ]]; then
rootCheck
COMMAND="groupadd"
else
COMMAND="echo groupadd"
fi
2024-11-12 17:09:38 +01:00
while read line; do
GID=$(cutter "${line}" "$COL_GROUPGID")
GROUPNAME=$(turnLowercase $(cutter "${line}" "$COL_GROUPNAME"))
2024-11-12 17:09:38 +01:00
if [[ -z "$GID" ]]; then
echo "[+] Adding group $GROUPNAME..."
$COMMAND $GROUPNAME
2024-11-12 17:09:38 +01:00
else
2024-11-18 16:31:20 +01:00
echo "[+] Adding group $GROUPNAME with GID $GID..."
$COMMAND $GROUPNAME -g $GID
2024-11-12 17:09:38 +01:00
fi
done <<< "$FILE_CONTENTS"
}
deleteGroups ()
{
checkFile
COMMAND=""
if [[ "$DRYRUN" == false ]]; then
rootCheck
COMMAND="groupdel"
else
COMMAND="echo groupdel"
fi
2024-11-12 17:09:38 +01:00
while read line; do
GROUPNAME=$(turnLowercase $(cutter "${line}" "$COL_GROUPNAME"))
echo "[+] Deleting group $GROUPNAME..."
$COMMAND $GROUPNAME
2024-11-12 17:09:38 +01:00
done <<< "$FILE_CONTENTS"
}
createUsers ()
{
checkFile
USERADDER=""
CHFNER=""
USERMODDER=""
if [[ "$DRYRUN" == false ]]; then
rootCheck
USERADDER="useradd"
CHFNER="chfn"
USERMODDER="usermod"
else
USERADDER="echo useradd"
CHFNER="echo chfn"
USERMODDER="echo usermod"
fi
2024-11-12 17:09:38 +01:00
while read line; do
USERNAME=$(turnLowercase $(cutter "${line}" "$COL_USERNAME"))
FULLNAME=$(cutter "${line}" "$COL_FULLNAME")
USERUID=$(cutter "${line}" "$COL_USERUID")
USERGID=$(turnLowercase $(cutter "${line}" "$COL_USERGID"))
USERGROUPS=$(turnLowercase $(cutter "${line}" "$COL_USERGROUPS"))
TLFN=$(cutter "${line}" "$COL_TLFN")
EXTRAINFO=$(cutter "${line}" "$COL_EXTRAINFO")
USERSHELL=$(cutter "${line}" "$COL_USERSHELL")
USERLOCKED=$(cutter "${line}" "$COL_USERLOCKED")
2024-11-12 17:09:38 +01:00
echo "[+] Adding user $USERNAME..."
$USERADDER -m $USERNAME
2024-11-12 17:09:38 +01:00
if [[ "$DRYRUN" == false ]]; then
echo $USERNAME:$USERNAME | chpasswd
2024-11-12 17:09:38 +01:00
fi
if [[ ! -z "$FULLNAME" ]]; then
$CHFNER -f "$FULLNAME" "$USERNAME" 1>/dev/null
2024-11-12 17:09:38 +01:00
fi
if [[ ! -z "$USERUID" ]]; then
$USERMODDER -u $USERUID $USERNAME
2024-11-12 17:09:38 +01:00
fi
if [[ ! -z "$USERGID" ]]; then
$USERMODDER -g $USERGID $USERNAME
2024-11-12 17:09:38 +01:00
fi
if [[ ! -z "$USERGROUPS" ]]; then
$USERMODDER -G $(echo $USERGROUPS | tr "|" ",") $USERNAME
2024-11-12 17:09:38 +01:00
fi
if [[ ! -z "$TLFN" ]]; then
$CHFNER -p "$TLFN" $USERNAME 1>/dev/null
2024-11-12 17:09:38 +01:00
fi
if [[ ! -z "$EXTRAINFO" ]]; then
$CHFNER -o "$EXTRAINFO" $USERNAME 1>/dev/null
2024-11-12 17:09:38 +01:00
fi
if [[ ! -z "$USERSHELL" ]]; then
$USERMODDER -s $USERSHELL $USERNAME
2024-11-12 17:09:38 +01:00
else
$USERMODDER -s /bin/bash $USERNAME
2024-11-12 17:09:38 +01:00
fi
if [[ $USERLOCKED == "SI" ]]; then
$USERMODDER -L $USERNAME
2024-11-12 17:09:38 +01:00
fi
done <<< "$FILE_CONTENTS"
}
deleteUsers ()
{
checkFile
if [[ "$DRYRUN" == false ]]; then
rootCheck
USERADDER="useradd"
CHFNER="chfn"
else
USERADDER="echo useradd"
CHFNER="echo chfn"
USERMOD="echo usermod"
fi
2024-11-12 17:09:38 +01:00
while read line; do
USERNAME=$(turnLowercase $(cutter "${line}" "$COL_USERNAME"))
2024-11-12 17:09:38 +01:00
echo "[+] Deleting user $USERNAME..."
if [[ "$DRYRUN" == true ]]; then
echo "userdel -r $USERNAME"
echo "groupdel $USERNAME"
else
userdel -r $USERNAME 2>/dev/null
groupdel $USERNAME 2>/dev/null
fi
2024-11-12 17:09:38 +01:00
done <<< "$FILE_CONTENTS"
}
for arg in "$@"
do
if [[ "$arg" == "-h" || "$arg" == "--help" ]]; then
usage
2024-11-18 16:31:20 +01:00
exit
2024-11-12 17:09:38 +01:00
fi
if [[ "$arg" == "--dry-run" ]]; then
DRYRUN=true
fi
if [[ "$arg" == "--create-groups" ]]; then
ACTION_TO_TAKE="create-groups"
2024-11-12 17:09:38 +01:00
fi
if [[ "$arg" == "--delete-groups" ]]; then
ACTION_TO_TAKE="delete-groups"
2024-11-12 17:09:38 +01:00
fi
if [[ "$arg" == "--create-users" ]]; then
ACTION_TO_TAKE="create-users"
2024-11-12 17:09:38 +01:00
fi
if [[ "$arg" == "--delete-users" ]]; then
ACTION_TO_TAKE="delete-users"
2024-11-12 17:09:38 +01:00
fi
if [[ "$arg" == "--create-group" ]]; then
ACTION_TO_TAKE="create-group"
2024-11-12 17:09:38 +01:00
fi
if [[ "$arg" == "--create-user" ]]; then
ACTION_TO_TAKE="create-user"
2024-11-12 17:09:38 +01:00
fi
if [[ "$arg" == "--delete-user" ]]; then
ACTION_TO_TAKE="delete-user"
2024-11-12 17:09:38 +01:00
fi
if [[ "$arg" == "--delete-group" ]]; then
ACTION_TO_TAKE="create-group"
fi
done
2024-11-12 17:09:38 +01:00
case "$ACTION_TO_TAKE" in
create-groups)
createGroups
exit
;;
create-users)
createUsers
exit
;;
delete-groups)
deleteGroups
exit
;;
delete-users)
2024-11-12 17:09:38 +01:00
deleteUsers
exit
;;
create-group)
createGroup
exit
;;
create-user)
createUser
exit
;;
*)
echo "Invalid parameter! Exiting..."
exit
;;
esac
2024-11-18 16:31:20 +01:00
usage