Nuclear refactoring
Finally got rid of the pointless dryrun functions and implemented dry-running directly into the original functions
This commit is contained in:
parent
d4518906b5
commit
bb6dd68dd2
333
autouser.sh
333
autouser.sh
|
@ -1,10 +1,14 @@
|
|||
#!/bin/bash
|
||||
|
||||
FILE="${@: -1}"
|
||||
|
||||
checkFile ()
|
||||
{
|
||||
if [[ ! -f "$FILE" ]]; then
|
||||
echo "File \"$FILE\" doesn't exist!"
|
||||
exit
|
||||
fi
|
||||
}
|
||||
|
||||
# User '.csv' columns
|
||||
COL_USERNAME="1"
|
||||
|
@ -23,11 +27,8 @@ COL_GROUPGID="2"
|
|||
|
||||
# Control variables
|
||||
DRYRUN=false
|
||||
FILE_CONTENTS="$(cat ${@: -1})"
|
||||
CREATE_USERS=false
|
||||
DELETE_USERS=false
|
||||
CREATE_GROUPS=false
|
||||
DELETE_GROUPS=false
|
||||
FILE_CONTENTS="$(cat ${@: -1} 2>/dev/null)"
|
||||
ACTION_TO_TAKE=""
|
||||
DELIM=";"
|
||||
|
||||
usage ()
|
||||
|
@ -41,9 +42,75 @@ usage ()
|
|||
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)"
|
||||
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 ()
|
||||
{
|
||||
if [[ "$(id -u)" -ne 0 ]]; then
|
||||
|
@ -64,51 +131,63 @@ turnLowercase()
|
|||
|
||||
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..."
|
||||
groupadd $GROUPNAME
|
||||
$COMMAND $GROUPNAME
|
||||
else
|
||||
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
|
||||
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..."
|
||||
groupdel $GROUPNAME
|
||||
done <<< "$FILE_CONTENTS"
|
||||
}
|
||||
|
||||
deleteGroupsDry ()
|
||||
{
|
||||
while read line; do
|
||||
GROUPNAME=$(turnLowercase $(cutter "${line}" "$COL_GROUPNAME"))
|
||||
echo "groupdel $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")
|
||||
|
@ -121,117 +200,66 @@ createUsers ()
|
|||
USERLOCKED=$(cutter "${line}" "$COL_USERLOCKED")
|
||||
|
||||
echo "[+] Adding user $USERNAME..."
|
||||
useradd -m $USERNAME
|
||||
echo $USERNAME:$USERNAME | chpasswd
|
||||
$USERADDER -m $USERNAME
|
||||
|
||||
if [[ "$DRYRUN" == false ]]; then
|
||||
echo $USERNAME:$USERNAME | chpasswd
|
||||
fi
|
||||
|
||||
if [[ ! -z "$FULLNAME" ]]; then
|
||||
chfn -f "$FULLNAME" "$USERNAME" 1>/dev/null
|
||||
$CHFNER -f "$FULLNAME" "$USERNAME" 1>/dev/null
|
||||
fi
|
||||
|
||||
if [[ ! -z "$USERUID" ]]; then
|
||||
usermod -u $USERUID $USERNAME
|
||||
$USERMODDER -u $USERUID $USERNAME
|
||||
fi
|
||||
|
||||
if [[ ! -z "$USERGID" ]]; then
|
||||
usermod -g $USERGID $USERNAME
|
||||
$USERMODDER -g $USERGID $USERNAME
|
||||
fi
|
||||
|
||||
if [[ ! -z "$USERGROUPS" ]]; then
|
||||
usermod -G $(echo $USERGROUPS | tr "|" ",") $USERNAME
|
||||
$USERMODDER -G $(echo $USERGROUPS | tr "|" ",") $USERNAME
|
||||
fi
|
||||
|
||||
if [[ ! -z "$TLFN" ]]; then
|
||||
chfn -p "$TLFN" $USERNAME 1>/dev/null
|
||||
$CHFNER -p "$TLFN" $USERNAME 1>/dev/null
|
||||
fi
|
||||
|
||||
if [[ ! -z "$EXTRAINFO" ]]; then
|
||||
chfn -o "$EXTRAINFO" $USERNAME 1>/dev/null
|
||||
$CHFNER -o "$EXTRAINFO" $USERNAME 1>/dev/null
|
||||
fi
|
||||
|
||||
if [[ ! -z "$USERSHELL" ]]; then
|
||||
usermod -s $USERSHELL $USERNAME
|
||||
$USERMODDER -s $USERSHELL $USERNAME
|
||||
else
|
||||
usermod -s /bin/bash $USERNAME
|
||||
$USERMODDER -s /bin/bash $USERNAME
|
||||
fi
|
||||
|
||||
if [[ $USERLOCKED == "SI" ]]; then
|
||||
usermod -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"
|
||||
$USERMODDER -L $USERNAME
|
||||
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
|
||||
while read line; do
|
||||
USERNAME=$(turnLowercase $(cutter "${line}" "$COL_USERNAME"))
|
||||
echo "[+] Deleting user $USERNAME..."
|
||||
userdel -r $USERNAME 2>/dev/null
|
||||
groupdel $USERNAME 2>/dev/null
|
||||
if [[ "$DRYRUN" == true ]]; then
|
||||
echo "userdel -r $USERNAME"
|
||||
echo "groupdel $USERNAME"
|
||||
else
|
||||
userdel -r $USERNAME 2>/dev/null
|
||||
groupdel $USERNAME 2>/dev/null
|
||||
fi
|
||||
done <<< "$FILE_CONTENTS"
|
||||
}
|
||||
|
||||
deleteUsersDry ()
|
||||
{
|
||||
while read line; do
|
||||
USERNAME=$(turnLowercase $(cutter "${line}" "$COL_USERNAME"))
|
||||
echo "userdel -r $USERNAME"
|
||||
echo "groupdel $USERNAME"
|
||||
done <<< "$FILE_CONTENTS"
|
||||
}
|
||||
|
||||
|
||||
for arg in "$@"
|
||||
do
|
||||
if [[ "$arg" == "-h" || "$arg" == "--help" ]]; then
|
||||
|
@ -242,57 +270,60 @@ do
|
|||
DRYRUN=true
|
||||
fi
|
||||
if [[ "$arg" == "--create-groups" ]]; then
|
||||
CREATE_GROUPS=true
|
||||
ACTION_TO_TAKE="create-groups"
|
||||
fi
|
||||
if [[ "$arg" == "--delete-groups" ]]; then
|
||||
DELETE_GROUPS=true
|
||||
ACTION_TO_TAKE="delete-groups"
|
||||
fi
|
||||
if [[ "$arg" == "--create-users" ]]; then
|
||||
CREATE_USERS=true
|
||||
ACTION_TO_TAKE="create-users"
|
||||
fi
|
||||
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
|
||||
done
|
||||
|
||||
if [[ "$CREATE_GROUPS" == true ]]; then
|
||||
if [[ "$DRYRUN" == true ]]; then
|
||||
createGroupsDry
|
||||
else
|
||||
rootCheck
|
||||
case "$ACTION_TO_TAKE" in
|
||||
create-groups)
|
||||
createGroups
|
||||
fi
|
||||
exit
|
||||
fi
|
||||
|
||||
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
|
||||
exit
|
||||
;;
|
||||
create-users)
|
||||
createUsers
|
||||
fi
|
||||
exit
|
||||
fi
|
||||
|
||||
if [[ "$DELETE_USERS" == true ]]; then
|
||||
if [[ "$DRYRUN" == true ]]; then
|
||||
deleteUsersDry
|
||||
else
|
||||
rootCheck
|
||||
exit
|
||||
;;
|
||||
delete-groups)
|
||||
deleteGroups
|
||||
exit
|
||||
;;
|
||||
delete-users)
|
||||
deleteUsers
|
||||
fi
|
||||
exit
|
||||
fi
|
||||
exit
|
||||
;;
|
||||
create-group)
|
||||
createGroup
|
||||
exit
|
||||
;;
|
||||
create-user)
|
||||
createUser
|
||||
exit
|
||||
;;
|
||||
*)
|
||||
echo "Invalid parameter! Exiting..."
|
||||
exit
|
||||
;;
|
||||
esac
|
||||
|
||||
usage
|
||||
|
|
Loading…
Reference in New Issue