update userqueue to address race condition hopefully

This commit is contained in:
deepend 2023-08-25 07:05:04 +00:00
parent 6cfe6cadb8
commit fb6241e209
1 changed files with 50 additions and 69 deletions

119
userqueue
View File

@ -6,97 +6,78 @@
source setenv
source include/functions
USERQUEUE_DIR='/dev/shm/userqueue' # Using a directory now
usage() {
echo "Usage: $0 [u][f][d][a] username"
echo " (u) Print users in queue"
echo " (f) Shows the fields of the selected user"
echo " (a) Accept sign up request"
echo " (d) Delete selected user from queue"
echo "Usage: $0 [u][f][d][a] username"
echo " (u) Print users in queue"
echo " (f) Shows the fields of the selected user"
echo " (a) Accept sign up request"
echo " (d) Delete selected user from queue"
}
confirm_delete() {
echo "Are you sure you want to delete user $1 from the queue? (y/n)"
read response
[[ "$response" == "y" || "$response" == "Y" ]]
}
escape_string() {
echo $1 | sed 's/[]\/$*.^|[]/\\&/g'
}
if [ $# -eq 0 ]; then
usage
exit 1
if [ ! -d $USERQUEUE_DIR ]; then
echo "User queue directory does not exist."
exit 1
fi
case $1 in
"u")
awk -F, '{print $1}' $USERQUEUE
ls $USERQUEUE_DIR
;;
"f")
if [ $# -ne 2 ]; then
usage
exit 1
if [ -f "$USERQUEUE_DIR/$2" ]; then
cat "$USERQUEUE_DIR/$2"
else
echo "User $2 not found in the queue."
fi
grep -E "^$(escape_string $2)," $USERQUEUE | awk -F, '{print $1, $2, $3}' || echo "Error fetching user details."
;;
"d")
if [ $# -ne 2 ]; then
usage
exit 1
fi
if confirm_delete $2; then
sudo sed "/^$(escape_string $2),/d" -i $USERQUEUE || echo "Error deleting user from queue."
if [ -f "$USERQUEUE_DIR/$2" ]; then
sudo rm "$USERQUEUE_DIR/$2"
else
echo "User $2 not found in the queue."
fi
;;
"a")
if [ $# -ne 2 ]; then
usage
exit 1
fi
if [ -f "$USERQUEUE_DIR/$2" ]; then
IFS=',' read -r USER EMAIL PUBKEY < "$USERQUEUE_DIR/$2"
# Ensure user data isn't empty or incomplete
if [ -z "$USER" ] || [ -z "$EMAIL" ] || [ -z "$PUBKEY" ]; then
echo "User data not found or incomplete."
exit 1
fi
LINE_MATCH=$(grep -E "^$(escape_string $2)," $USERQUEUE)
if [[ $? -ne 0 ]]; then
echo "Error: User not found in the queue."
exit 1
fi
makeuser_no_ansible $USER $EMAIL $PUBKEY
if [ $? -ne 0 ]; then
echo "Error in user creation."
exit 1
fi
USER=$(echo "$LINE_MATCH" | awk -F, '{print $1}')
EMAIL=$(echo "$LINE_MATCH" | awk -F, '{print $2}')
PUBKEY=$(echo "$LINE_MATCH" | awk -F, '{print $3}')
add_account_recovery $USER $EMAIL
if [ $? -ne 0 ]; then
echo "Error in adding account recovery."
exit 1
fi
if [ -z "$USER" ] || [ -z "$EMAIL" ] || [ -z "$PUBKEY" ]; then
echo "User data not found or incomplete."
exit 1
fi
makeuser_no_ansible $USER $EMAIL $PUBKEY
if [ $? -ne 0 ]; then
echo "Error in user creation."
exit 1
fi
add_account_recovery $USER $EMAIL
if [ $? -ne 0 ]; then
echo "Error in adding account recovery."
exit 1
fi
echo "Do you want to run the Ansible steps for $USER? (y/n)"
read response
if [[ "$response" = "y" || "$response" = "Y" ]]; then
source include/ansible.sh
if [ $? -eq 0 ]; then
sudo sed "/^$(escape_string $2),/d" -i $USERQUEUE
else
echo "Error in Ansible steps. User not removed from queue."
exit 1
fi
echo "Do you want to run the Ansible steps for $USER? (y/n)"
read response
if [[ "$response" = "y" || "$response" = "Y" ]]; then
source include/ansible.sh
if [ $? -eq 0 ]; then
sudo rm "$USERQUEUE_DIR/$2"
else
echo "Error in Ansible steps. User not removed from queue."
fi
else
echo "Skipped Ansible steps."
fi
else
echo "Skipped Ansible steps."
echo "User $2 not found in the queue."
fi
;;