|
|
|
@ -0,0 +1,282 @@
|
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
FILE="${@: -1}"
|
|
|
|
|
if [[ ! -f "$FILE" ]]; then
|
|
|
|
|
echo "File \"$FILE\" doesn't exist!"
|
|
|
|
|
exit
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
DRYRUN=false
|
|
|
|
|
FILE_CONTENTS="$(cat ${@: -1})"
|
|
|
|
|
CREATE_USERS=false
|
|
|
|
|
DELETE_USERS=false
|
|
|
|
|
CREATE_GROUPS=false
|
|
|
|
|
DELETE_GROUPS=false
|
|
|
|
|
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)"
|
|
|
|
|
exit
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 ()
|
|
|
|
|
{
|
|
|
|
|
while read line; do
|
|
|
|
|
GID=$(cutter "${line}" "2")
|
|
|
|
|
GROUPNAME=$(turnLowercase $(cutter "${line}" "1"))
|
|
|
|
|
if [[ -z "$GID" ]]; then
|
|
|
|
|
echo "[+] Adding group $GROUPNAME..."
|
|
|
|
|
groupadd $GROUPNAME
|
|
|
|
|
else
|
|
|
|
|
echo "[+] Adding group $GROUPNAME..."
|
|
|
|
|
groupadd $GROUPNAME -g $GID
|
|
|
|
|
fi
|
|
|
|
|
done <<< "$FILE_CONTENTS"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createGroupsDry ()
|
|
|
|
|
{
|
|
|
|
|
while read line; do
|
|
|
|
|
GID=$(cutter "${line}" "2")
|
|
|
|
|
GROUPNAME=$(turnLowercase $(cutter "${line}" "1"))
|
|
|
|
|
if [[ -z "$GID" ]]; then
|
|
|
|
|
echo "groupadd $GROUPNAME"
|
|
|
|
|
else
|
|
|
|
|
echo "groupadd $GROUPNAME -g $GID"
|
|
|
|
|
fi
|
|
|
|
|
done <<< "$FILE_CONTENTS"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deleteGroups ()
|
|
|
|
|
{
|
|
|
|
|
while read line; do
|
|
|
|
|
GROUPNAME=$(turnLowercase $(cutter "${line}" "1"))
|
|
|
|
|
echo "[+] Deleting group $GROUPNAME..."
|
|
|
|
|
groupdel $GROUPNAME
|
|
|
|
|
done <<< "$FILE_CONTENTS"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deleteGroupsDry ()
|
|
|
|
|
{
|
|
|
|
|
while read line; do
|
|
|
|
|
GROUPNAME=$(turnLowercase $(cutter "${line}" "1"))
|
|
|
|
|
echo "groupdel $GROUPNAME"
|
|
|
|
|
done <<< "$FILE_CONTENTS"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createUsers ()
|
|
|
|
|
{
|
|
|
|
|
while read line; do
|
|
|
|
|
USERNAME=$(turnLowercase $(cutter "${line}" "1"))
|
|
|
|
|
FULLNAME=$(cutter "${line}" "2")
|
|
|
|
|
USERUID=$(cutter "${line}" "3")
|
|
|
|
|
USERGID=$(turnLowercase $(cutter "${line}" "4"))
|
|
|
|
|
USERGROUPS=$(turnLowercase $(cutter "${line}" "5"))
|
|
|
|
|
TLFN=$(cutter "${line}" "6")
|
|
|
|
|
EXTRAINFO=$(cutter "${line}" "7")
|
|
|
|
|
USERSHELL=$(cutter "${line}" "8")
|
|
|
|
|
USERLOCKED=$(cutter "${line}" "9")
|
|
|
|
|
|
|
|
|
|
echo "[+] Adding user $USERNAME..."
|
|
|
|
|
useradd -m $USERNAME
|
|
|
|
|
echo $USERNAME:$USERNAME | chpasswd
|
|
|
|
|
|
|
|
|
|
if [[ ! -z "$FULLNAME" ]]; then
|
|
|
|
|
chfn -f "$FULLNAME" "$USERNAME" 1>/dev/null
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ ! -z "$USERUID" ]]; then
|
|
|
|
|
usermod -u $USERUID $USERNAME
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ ! -z "$USERGID" ]]; then
|
|
|
|
|
usermod -g $USERGID $USERNAME
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ ! -z "$USERGROUPS" ]]; then
|
|
|
|
|
usermod -G $(echo $USERGROUPS | tr "|" ",") $USERNAME
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ ! -z "$TLFN" ]]; then
|
|
|
|
|
chfn -p "$TLFN" $USERNAME 1>/dev/null
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ ! -z "$EXTRAINFO" ]]; then
|
|
|
|
|
chfn -o "$EXTRAINFO" $USERNAME 1>/dev/null
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ ! -z "$USERSHELL" ]]; then
|
|
|
|
|
usermod -s $USERSHELL $USERNAME
|
|
|
|
|
else
|
|
|
|
|
usermod -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}" "1"))
|
|
|
|
|
FULLNAME=$(cutter "${line}" "2")
|
|
|
|
|
USERUID=$(cutter "${line}" "3")
|
|
|
|
|
USERGID=$(turnLowercase $(cutter "${line}" "4"))
|
|
|
|
|
USERGROUPS=$(turnLowercase $(cutter "${line}" "5"))
|
|
|
|
|
TLFN=$(cutter "${line}" "6")
|
|
|
|
|
EXTRAINFO=$(cutter "${line}" "7")
|
|
|
|
|
USERSHELL=$(cutter "${line}" "8")
|
|
|
|
|
USERLOCKED=$(cutter "${line}" "9")
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
done <<< "$FILE_CONTENTS"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deleteUsers ()
|
|
|
|
|
{
|
|
|
|
|
while read line; do
|
|
|
|
|
USERNAME=$(turnLowercase $(cutter "${line}" "1"))
|
|
|
|
|
echo "[+] Deleting user $USERNAME..."
|
|
|
|
|
userdel -r $USERNAME 2>/dev/null
|
|
|
|
|
groupdel $USERNAME 2>/dev/null
|
|
|
|
|
done <<< "$FILE_CONTENTS"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deleteUsersDry ()
|
|
|
|
|
{
|
|
|
|
|
while read line; do
|
|
|
|
|
USERNAME=$(turnLowercase $(cutter "${line}" "1"))
|
|
|
|
|
echo "userdel -r $USERNAME"
|
|
|
|
|
echo "groupdel $USERNAME"
|
|
|
|
|
done <<< "$FILE_CONTENTS"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for arg in "$@"
|
|
|
|
|
do
|
|
|
|
|
if [[ "$arg" == "-h" || "$arg" == "--help" ]]; then
|
|
|
|
|
usage
|
|
|
|
|
fi
|
|
|
|
|
if [[ "$arg" == "--dry-run" ]]; then
|
|
|
|
|
DRYRUN=true
|
|
|
|
|
fi
|
|
|
|
|
if [[ "$arg" == "--create-groups" ]]; then
|
|
|
|
|
CREATE_GROUPS=true
|
|
|
|
|
fi
|
|
|
|
|
if [[ "$arg" == "--delete-groups" ]]; then
|
|
|
|
|
DELETE_GROUPS=true
|
|
|
|
|
fi
|
|
|
|
|
if [[ "$arg" == "--create-users" ]]; then
|
|
|
|
|
CREATE_USERS=true
|
|
|
|
|
fi
|
|
|
|
|
if [[ "$arg" == "--delete-users" ]]; then
|
|
|
|
|
DELETE_USERS=true
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
if [[ -z "$1" ]]; then
|
|
|
|
|
usage
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ "$CREATE_GROUPS" == true ]]; then
|
|
|
|
|
if [[ "$DRYRUN" == true ]]; then
|
|
|
|
|
createGroupsDry
|
|
|
|
|
else
|
|
|
|
|
rootCheck
|
|
|
|
|
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
|
|
|
|
|
createUsers
|
|
|
|
|
fi
|
|
|
|
|
exit
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ "$DELETE_USERS" == true ]]; then
|
|
|
|
|
if [[ "$DRYRUN" == true ]]; then
|
|
|
|
|
deleteUsersDry
|
|
|
|
|
else
|
|
|
|
|
rootCheck
|
|
|
|
|
deleteUsers
|
|
|
|
|
fi
|
|
|
|
|
exit
|
|
|
|
|
fi
|