Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added rmassuseradd #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions setup/qos_scripts/useradd_mod/rmassuseradd
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

# File to be placed in /usr/local/sbin/
# Custom script to onboard new users onto RECON
# Funtions:
# -
# Author: Gurram Siddarth Reddy
# Last modified: 29th Aug, 2022

# $1 - csvpath
# $2 - group name
csvpath=$1
csvfile=$(basename "$csvpath")
extension="${csvfile##*.}"
if [ "$extension" != "csv" ]; then
echo "NOT A CSV FILE , EXITING........"
exit
fi

#group=$2
while IFS="" read -r data || [ -n "$data" ]
do
userid=$(printf '%s\n' "$data" | cut -d\, -f1)
user_name=$(printf '%s\n' "$data" | cut -d\, -f2)
ssh_key=$(printf '%s\n' "$data" | cut -d\, -f3)
ruseradd "$userid" "$user_name" "$ssh_key" "$group"
done < $csvpath
70 changes: 55 additions & 15 deletions setup/qos_scripts/useradd_mod/ruseradd
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,61 @@
# Last modified: 4th Aug, 2022

# $1 - username
# $2 - ssh key
# $3 - Name of the user
# $4 - group of the user

MOD_DIR=/usr/local/sbin/useradd_mod
while getopts ":k:i:n:u:" options; do
case "${options}" in
i)
username=${OPTARG}
;;
k)
SSH_KEY=${OPTARG}
;;
g)
group=${OPTARG}
;;
n)
name=${OPTARG}
;;
:)
echo "unknown FLAG error"
;;
esac
done

# Check if the username was provided
if ! [ -n "$1" ]; then
echo "No username provided, exiting..."
if id "$username" &>/dev/null; then
echo 'user found'
echo 'quitting...'
exit
else
echo "creating user $username with username $name"
fi

valid='s[1-9][0-9][meca][semi][0-9][0-9][0-9]' # regex for valid username
if [[ ! $1 =~ $valid ]]; then
# Check if the username was provided
if ! [ -n "$username" ]; then
echo "No username provided"
echo -n "Enter username: "
read username
fi
if ! [ -n "$SSH_KEY" ]; then # If ssh key is an empty string,
# Getting SSH keys from user
echo -n "Enter user's SSH keys: "
read SSH_KEY
fi
if [ "$name" = "" ]; then # If ssh key is an empty string,
# Getting SSH keys from user
echo -n "Enter full name of the user: "
read FULL_USERNAME
else
FULL_USERNAME=$name
fi

student='s[1-9][0-9][meca][semic][0-9][0-9][0-9]' # regex for valid student username
faculty='f[0-9][0-9][0-9][0-9]' # regex for valid faculty ID
if [[ ! $username =~ $student && ! $username =~ $faculty ]]; then
echo "Invalid username, exiting..."
exit # exit if the username is invalid
fi
Expand All @@ -32,22 +76,18 @@ if [ "$USER" != "root" ]; then
exit
fi

# Input user full name
echo -n "Enter full name of the user: "
read FULL_USERNAME

# Create new user with home directory
useradd -m $1 --comment "$FULL_USERNAME"
useradd -m $username --comment "$FULL_USERNAME"

# Change home permission to 750 for increased privacy
chmod 750 /home/$1
chmod 750 /home/$username

# Get new UID and GID
rUID=$(id $1 -u)
rGID=$(id $1 -g)
rUID=$(id $username -u)
rGID=$(id $username -g)

# Add user to LDAP
$MOD_DIR/ldif_editors/genLDIF.sh $1 $rUID $rGID
$MOD_DIR/ldif_editors/genLDIF.sh $username $rUID $rGID

# Generate and add ssh-keys for new user
$MOD_DIR/ssh-keymgmt/gen_newuser_keys.sh $1 $rUID $rGID
gen_newuser_keys.sh -i $username -u $rUID -g $rGID -k "$SSH_KEY"
40 changes: 31 additions & 9 deletions setup/qos_scripts/useradd_mod/ssh-keymgmt/gen_newuser_keys.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,31 @@
# Author: Srikar (aka epoch101)

# $1 - username
# $2 - UID
# $2 - UID
# $3 - GID
# SSH_DIR - /home/$1/.ssh

SSH_DIR=/home/$1/.ssh
echo "$1 $2 $3 $4 $5 $6 $7 $8"
while getopts ":i:k:u:G:" options; do
echo $OPTARG
case "${options}" in
i)
username=${OPTARG}
;;
k)
SSH_KEY=${OPTARG}
;;
u)
rUID=${OPTARG}
;;
G)
rGID=${OPTARG}
;;
:)
echo "unknown FLAG error"
;;
esac
done
SSH_DIR=/home/$username/.ssh

# Making a new ssh directory
mkdir $SSH_DIR
Expand All @@ -22,14 +42,16 @@ ssh-keygen -t ed25519 -f $SSH_DIR/id_ed25519 -q -N ""
# Adding the public key of new keys for inter-node access
cat $SSH_DIR/id_ed25519.pub > $SSH_DIR/authorized_keys

# Getting SSH keys from user
echo -n "Enter user's SSH keys: "
read user_sshkeys
if ! [ -n "$SSH_KEY" ]; then # If ssh key is an empty string,
# Getting SSH keys from user
echo -n "Enter user's SSH keys please: "
read SSH_KEY
fi

# Adding user keys to username for easy access
echo $user_sshkeys >> $SSH_DIR/authorized_keys
echo $SSH_KEY >> $SSH_DIR/authorized_keys

echo "Added keys for user $1!"
echo "Added keys for user $username!"

# Changing file permissions to ensure proper access
chown -R $2:$3 $SSH_DIR
chown -R $rUID:$rGID $SSH_DIR