autouser/autouser.sh

340 lines
7.0 KiB
Bash
Executable File

#!/bin/bash
FILE="${@: -1}"
checkFile ()
{
if [[ ! -f "$FILE" ]]; then
echo "File \"$FILE\" doesn't exist!"
exit
fi
}
# 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
DRYRUN=false
FILE_CONTENTS="$(cat ${@: -1} 2>/dev/null)"
ACTION_TO_TAKE=""
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 ""
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 ""
echo " --create-user (Create user manually)"
echo " --create-group (Create group manually)"
echo " --delete-user (Delete user manually)"
echo " --delete-group (Delete group manually)"
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() {
COMMAND=""
if [[ "$DRYRUN" == false ]]; then
rootCheck
COMMAND="groupdel"
else
COMMAND="echo groupdel"
fi
read -p "Enter group name: " GROUPNAME
$COMMAND $GROUPNAME
}
deleteUser() {
COMMAND=""
if [[ "$DRYRUN" == false ]]; then
rootCheck
COMMAND="userdel"
else
COMMAND="echo userdel"
fi
read -p "Enter username: " USERNAME
$COMMAND -r $USERNAME
}
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"
}
turnLowercase()
{
echo "$1" | tr "[:upper:]" "[:lower:]" | sed 's/ñ/n/g'
}
createGroups ()
{
checkFile
COMMAND=""
if [[ "$DRYRUN" == false ]]; then
rootCheck
COMMAND="groupadd"
else
COMMAND="echo groupadd"
fi
while read line; do
GID=$(cutter "${line}" "$COL_GROUPGID")
GROUPNAME=$(turnLowercase $(cutter "${line}" "$COL_GROUPNAME"))
if [[ -z "$GID" ]]; then
echo "[+] Adding group $GROUPNAME..."
$COMMAND $GROUPNAME
else
echo "[+] Adding group $GROUPNAME with GID $GID..."
$COMMAND $GROUPNAME -g $GID
fi
done <<< "$FILE_CONTENTS"
}
deleteGroups ()
{
checkFile
COMMAND=""
if [[ "$DRYRUN" == false ]]; then
rootCheck
COMMAND="groupdel"
else
COMMAND="echo groupdel"
fi
while read line; do
GROUPNAME=$(turnLowercase $(cutter "${line}" "$COL_GROUPNAME"))
echo "[+] Deleting group $GROUPNAME..."
$COMMAND $GROUPNAME
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
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")
echo "[+] Adding user $USERNAME..."
$USERADDER -m $USERNAME
if [[ "$DRYRUN" == false ]]; then
echo $USERNAME:$USERNAME | chpasswd
fi
if [[ ! -z "$FULLNAME" ]]; then
$CHFNER -f "$FULLNAME" "$USERNAME" 1>/dev/null
fi
if [[ ! -z "$USERUID" ]]; then
$USERMODDER -u $USERUID $USERNAME
fi
if [[ ! -z "$USERGID" ]]; then
$USERMODDER -g $USERGID $USERNAME
fi
if [[ ! -z "$USERGROUPS" ]]; then
$USERMODDER -G $(echo $USERGROUPS | tr "|" ",") $USERNAME
fi
if [[ ! -z "$TLFN" ]]; then
$CHFNER -p "$TLFN" $USERNAME 1>/dev/null
fi
if [[ ! -z "$EXTRAINFO" ]]; then
$CHFNER -o "$EXTRAINFO" $USERNAME 1>/dev/null
fi
if [[ ! -z "$USERSHELL" ]]; then
$USERMODDER -s $USERSHELL $USERNAME
else
$USERMODDER -s /bin/bash $USERNAME
fi
if [[ $USERLOCKED == "SI" ]]; then
$USERMODDER -L $USERNAME
fi
done <<< "$FILE_CONTENTS"
}
deleteUsers ()
{
checkFile
COMMAND=""
if [[ "$DRYRUN" == false ]]; then
rootCheck
USERDELER="userdel"
GROUPDELER="groupdel"
else
USERDELER="echo userdel"
GROUPDELER="echo groupdel"
fi
while read line; do
USERNAME=$(turnLowercase $(cutter "${line}" "$COL_USERNAME"))
echo "[+] Deleting user $USERNAME..."
$USERDELER -r $USERNAME 2>/dev/null
$GROUPDELER $USERNAME 2>/dev/null
done <<< "$FILE_CONTENTS"
}
for arg in "$@"
do
if [[ "$arg" == "-h" || "$arg" == "--help" ]]; then
usage
exit
fi
if [[ "$arg" == "--dry-run" ]]; then
DRYRUN=true
fi
if [[ "$arg" == "--create-groups" ]]; then
ACTION_TO_TAKE="create-groups"
fi
if [[ "$arg" == "--delete-groups" ]]; then
ACTION_TO_TAKE="delete-groups"
fi
if [[ "$arg" == "--create-users" ]]; then
ACTION_TO_TAKE="create-users"
fi
if [[ "$arg" == "--delete-users" ]]; then
ACTION_TO_TAKE="delete-users"
fi
if [[ "$arg" == "--create-group" ]]; then
ACTION_TO_TAKE="create-group"
fi
if [[ "$arg" == "--create-user" ]]; then
ACTION_TO_TAKE="create-user"
fi
if [[ "$arg" == "--delete-user" ]]; then
ACTION_TO_TAKE="delete-user"
fi
if [[ "$arg" == "--delete-group" ]]; then
ACTION_TO_TAKE="create-group"
fi
done
if [[ "$ACTION_TO_TAKE" == "" ]]; then
usage
exit
fi
case "$ACTION_TO_TAKE" in
create-groups)
createGroups
exit
;;
create-users)
createUsers
exit
;;
delete-groups)
deleteGroups
exit
;;
delete-users)
deleteUsers
exit
;;
create-group)
createGroup
exit
;;
create-user)
createUser
exit
;;
delete-group)
deleteGroup
exit
;;
delete-user)
deleteUser
exit
;;
*)
echo "Invalid parameter! Exiting..."
exit
;;
esac