Nuclear refactoring

Finally got rid of the pointless dryrun functions and implemented
dry-running directly into the original functions
This commit is contained in:
raul 2024-12-16 10:14:53 +01:00
parent d4518906b5
commit bb6dd68dd2
Signed by: raul
GPG Key ID: C1AA797073F17129
1 changed files with 182 additions and 151 deletions

View File

@ -1,10 +1,14 @@
#!/bin/bash #!/bin/bash
FILE="${@: -1}" FILE="${@: -1}"
checkFile ()
{
if [[ ! -f "$FILE" ]]; then if [[ ! -f "$FILE" ]]; then
echo "File \"$FILE\" doesn't exist!" echo "File \"$FILE\" doesn't exist!"
exit exit
fi fi
}
# User '.csv' columns # User '.csv' columns
COL_USERNAME="1" COL_USERNAME="1"
@ -23,11 +27,8 @@ COL_GROUPGID="2"
# Control variables # Control variables
DRYRUN=false DRYRUN=false
FILE_CONTENTS="$(cat ${@: -1})" FILE_CONTENTS="$(cat ${@: -1} 2>/dev/null)"
CREATE_USERS=false ACTION_TO_TAKE=""
DELETE_USERS=false
CREATE_GROUPS=false
DELETE_GROUPS=false
DELIM=";" DELIM=";"
usage () usage ()
@ -41,9 +42,75 @@ usage ()
echo " --create-groups groups.csv (Create groups 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-users users.csv (Delete users from a .csv file)"
echo " --delete-groups groups.csv (Delete groups 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)"
exit 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
}
rootCheck () rootCheck ()
{ {
if [[ "$(id -u)" -ne 0 ]]; then if [[ "$(id -u)" -ne 0 ]]; then
@ -64,51 +131,63 @@ turnLowercase()
createGroups () createGroups ()
{ {
checkFile
COMMAND=""
if [[ "$DRYRUN" == false ]]; then
rootCheck
COMMAND="groupadd"
else
COMMAND="echo groupadd"
fi
while read line; do while read line; do
GID=$(cutter "${line}" "$COL_GROUPGID") GID=$(cutter "${line}" "$COL_GROUPGID")
GROUPNAME=$(turnLowercase $(cutter "${line}" "$COL_GROUPNAME")) GROUPNAME=$(turnLowercase $(cutter "${line}" "$COL_GROUPNAME"))
if [[ -z "$GID" ]]; then if [[ -z "$GID" ]]; then
echo "[+] Adding group $GROUPNAME..." echo "[+] Adding group $GROUPNAME..."
groupadd $GROUPNAME $COMMAND $GROUPNAME
else else
echo "[+] Adding group $GROUPNAME with GID $GID..." echo "[+] Adding group $GROUPNAME with GID $GID..."
groupadd $GROUPNAME -g $GID $COMMAND $GROUPNAME -g $GID
fi
done <<< "$FILE_CONTENTS"
}
createGroupsDry ()
{
while read line; do
GID=$(cutter "${line}" "$COL_GROUPGID")
GROUPNAME=$(turnLowercase $(cutter "${line}" "$COL_GROUPNAME"))
if [[ -z "$GID" ]]; then
echo "groupadd $GROUPNAME"
else
echo "groupadd $GROUPNAME -g $GID"
fi fi
done <<< "$FILE_CONTENTS" done <<< "$FILE_CONTENTS"
} }
deleteGroups () deleteGroups ()
{ {
checkFile
COMMAND=""
if [[ "$DRYRUN" == false ]]; then
rootCheck
COMMAND="groupdel"
else
COMMAND="echo groupdel"
fi
while read line; do while read line; do
GROUPNAME=$(turnLowercase $(cutter "${line}" "$COL_GROUPNAME")) GROUPNAME=$(turnLowercase $(cutter "${line}" "$COL_GROUPNAME"))
echo "[+] Deleting group $GROUPNAME..." echo "[+] Deleting group $GROUPNAME..."
groupdel $GROUPNAME $COMMAND $GROUPNAME
done <<< "$FILE_CONTENTS"
}
deleteGroupsDry ()
{
while read line; do
GROUPNAME=$(turnLowercase $(cutter "${line}" "$COL_GROUPNAME"))
echo "groupdel $GROUPNAME"
done <<< "$FILE_CONTENTS" done <<< "$FILE_CONTENTS"
} }
createUsers () 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 while read line; do
USERNAME=$(turnLowercase $(cutter "${line}" "$COL_USERNAME")) USERNAME=$(turnLowercase $(cutter "${line}" "$COL_USERNAME"))
FULLNAME=$(cutter "${line}" "$COL_FULLNAME") FULLNAME=$(cutter "${line}" "$COL_FULLNAME")
@ -121,117 +200,66 @@ createUsers ()
USERLOCKED=$(cutter "${line}" "$COL_USERLOCKED") USERLOCKED=$(cutter "${line}" "$COL_USERLOCKED")
echo "[+] Adding user $USERNAME..." echo "[+] Adding user $USERNAME..."
useradd -m $USERNAME $USERADDER -m $USERNAME
if [[ "$DRYRUN" == false ]]; then
echo $USERNAME:$USERNAME | chpasswd echo $USERNAME:$USERNAME | chpasswd
fi
if [[ ! -z "$FULLNAME" ]]; then if [[ ! -z "$FULLNAME" ]]; then
chfn -f "$FULLNAME" "$USERNAME" 1>/dev/null $CHFNER -f "$FULLNAME" "$USERNAME" 1>/dev/null
fi fi
if [[ ! -z "$USERUID" ]]; then if [[ ! -z "$USERUID" ]]; then
usermod -u $USERUID $USERNAME $USERMODDER -u $USERUID $USERNAME
fi fi
if [[ ! -z "$USERGID" ]]; then if [[ ! -z "$USERGID" ]]; then
usermod -g $USERGID $USERNAME $USERMODDER -g $USERGID $USERNAME
fi fi
if [[ ! -z "$USERGROUPS" ]]; then if [[ ! -z "$USERGROUPS" ]]; then
usermod -G $(echo $USERGROUPS | tr "|" ",") $USERNAME $USERMODDER -G $(echo $USERGROUPS | tr "|" ",") $USERNAME
fi fi
if [[ ! -z "$TLFN" ]]; then if [[ ! -z "$TLFN" ]]; then
chfn -p "$TLFN" $USERNAME 1>/dev/null $CHFNER -p "$TLFN" $USERNAME 1>/dev/null
fi fi
if [[ ! -z "$EXTRAINFO" ]]; then if [[ ! -z "$EXTRAINFO" ]]; then
chfn -o "$EXTRAINFO" $USERNAME 1>/dev/null $CHFNER -o "$EXTRAINFO" $USERNAME 1>/dev/null
fi fi
if [[ ! -z "$USERSHELL" ]]; then if [[ ! -z "$USERSHELL" ]]; then
usermod -s $USERSHELL $USERNAME $USERMODDER -s $USERSHELL $USERNAME
else else
usermod -s /bin/bash $USERNAME $USERMODDER -s /bin/bash $USERNAME
fi fi
if [[ $USERLOCKED == "SI" ]]; then if [[ $USERLOCKED == "SI" ]]; then
usermod -L $USERNAME $USERMODDER -L $USERNAME
fi
done <<< "$FILE_CONTENTS"
}
createUsersDry ()
{
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 "useradd -m $USERNAME"
echo "echo \"$USERNAME:$USERNAME | chpasswd\""
if [[ ! -z "$FULLNAME" ]]; then
echo "chfn -f \"$FULLNAME\" $USERNAME"
fi
if [[ ! -z "$USERUID" ]]; then
echo "usermod -u $USERUID $USERNAME"
fi
if [[ ! -z "$USERGID" ]]; then
echo "usermod -g $USERGID $USERNAME"
fi
if [[ ! -z "$USERGROUPS" ]]; then
echo "usermod -G $(echo $USERGROUPS | tr "|" ",") $USERNAME"
fi
if [[ ! -z "$TLFN" ]]; then
echo "chfn -p \"$TLFN\" $USERNAME"
fi
if [[ ! -z "$EXTRAINFO" ]]; then
echo "usermod -c \"$EXTRAINFO\" $USERNAME"
fi
if [[ ! -z "$USERSHELL" ]]; then
echo "usermod -s $USERSHELL $USERNAME"
else
echo "usermod -s /bin/bash $USERNAME"
fi
if [[ $USERLOCKED == "SI" ]]; then
echo "usermod -L $USERNAME"
fi fi
done <<< "$FILE_CONTENTS" done <<< "$FILE_CONTENTS"
} }
deleteUsers () deleteUsers ()
{ {
checkFile
if [[ "$DRYRUN" == false ]]; then
rootCheck
USERADDER="useradd"
CHFNER="chfn"
else
USERADDER="echo useradd"
CHFNER="echo chfn"
USERMOD="echo usermod"
fi
while read line; do while read line; do
USERNAME=$(turnLowercase $(cutter "${line}" "$COL_USERNAME")) USERNAME=$(turnLowercase $(cutter "${line}" "$COL_USERNAME"))
echo "[+] Deleting user $USERNAME..." echo "[+] Deleting user $USERNAME..."
userdel -r $USERNAME 2>/dev/null if [[ "$DRYRUN" == true ]]; then
groupdel $USERNAME 2>/dev/null
done <<< "$FILE_CONTENTS"
}
deleteUsersDry ()
{
while read line; do
USERNAME=$(turnLowercase $(cutter "${line}" "$COL_USERNAME"))
echo "userdel -r $USERNAME" echo "userdel -r $USERNAME"
echo "groupdel $USERNAME" echo "groupdel $USERNAME"
else
userdel -r $USERNAME 2>/dev/null
groupdel $USERNAME 2>/dev/null
fi
done <<< "$FILE_CONTENTS" done <<< "$FILE_CONTENTS"
} }
for arg in "$@" for arg in "$@"
do do
if [[ "$arg" == "-h" || "$arg" == "--help" ]]; then if [[ "$arg" == "-h" || "$arg" == "--help" ]]; then
@ -242,57 +270,60 @@ do
DRYRUN=true DRYRUN=true
fi fi
if [[ "$arg" == "--create-groups" ]]; then if [[ "$arg" == "--create-groups" ]]; then
CREATE_GROUPS=true ACTION_TO_TAKE="create-groups"
fi fi
if [[ "$arg" == "--delete-groups" ]]; then if [[ "$arg" == "--delete-groups" ]]; then
DELETE_GROUPS=true ACTION_TO_TAKE="delete-groups"
fi fi
if [[ "$arg" == "--create-users" ]]; then if [[ "$arg" == "--create-users" ]]; then
CREATE_USERS=true ACTION_TO_TAKE="create-users"
fi fi
if [[ "$arg" == "--delete-users" ]]; then if [[ "$arg" == "--delete-users" ]]; then
DELETE_USERS=true 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 fi
done done
if [[ "$CREATE_GROUPS" == true ]]; then case "$ACTION_TO_TAKE" in
if [[ "$DRYRUN" == true ]]; then create-groups)
createGroupsDry
else
rootCheck
createGroups createGroups
fi
exit exit
fi ;;
create-users)
if [[ "$DELETE_GROUPS" == true ]]; then
if [[ "$DRYRUN" == true ]]; then
deleteGroupsDry
else
rootCheck
deleteGroups
fi
exit
fi
if [[ "$CREATE_USERS" == true ]]; then
if [[ "$DRYRUN" == true ]]; then
createUsersDry
else
rootCheck
createUsers createUsers
fi
exit exit
fi ;;
delete-groups)
if [[ "$DELETE_USERS" == true ]]; then deleteGroups
if [[ "$DRYRUN" == true ]]; then exit
deleteUsersDry ;;
else delete-users)
rootCheck
deleteUsers deleteUsers
fi
exit exit
fi ;;
create-group)
createGroup
exit
;;
create-user)
createUser
exit
;;
*)
echo "Invalid parameter! Exiting..."
exit
;;
esac
usage usage