mirror of
https://github.com/ChrisTitusTech/linutil.git
synced 2024-11-05 21:28:48 +00:00
Fix account manager (#553)
* Fix account manager * Fix missing escalation tools and checkenv --------- Co-authored-by: nnyyxxxx <nnyyxxxx@users.noreply.github.com>
This commit is contained in:
parent
28533b9f38
commit
ef9307dd83
|
@ -1,6 +1,6 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ./utility_functions.sh
|
. ../utility_functions.sh
|
||||||
|
|
||||||
. ../../common-script.sh
|
. ../../common-script.sh
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ./utility_functions.sh
|
. ../utility_functions.sh
|
||||||
|
|
||||||
. ../../common-script.sh
|
. ../../common-script.sh
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ./utility_functions.sh
|
. ../utility_functions.sh
|
||||||
|
|
||||||
. ../../common-script.sh
|
. ../../common-script.sh
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ./utility_functions.sh
|
. ../utility_functions.sh
|
||||||
|
|
||||||
. ../../common-script.sh
|
. ../../common-script.sh
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ./utility_functions.sh
|
. ../utility_functions.sh
|
||||||
|
|
||||||
. ../../common-script.sh
|
. ../../common-script.sh
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ./utility_functions.sh
|
. ../utility_functions.sh
|
||||||
|
|
||||||
. ../../common-script.sh
|
. ../../common-script.sh
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ./utility_functions.sh
|
. ../utility_functions.sh
|
||||||
|
|
||||||
. ../../common-script.sh
|
. ../../common-script.sh
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ./utility_functions.sh
|
. ../utility_functions.sh
|
||||||
|
|
||||||
. ../../common-script.sh
|
. ../../common-script.sh
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ./utility_functions.sh
|
. ../utility_functions.sh
|
||||||
|
|
||||||
# Function to adjust brightness for a selected monitor
|
# Function to adjust brightness for a selected monitor
|
||||||
adjust_monitor_brightness() {
|
adjust_monitor_brightness() {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ./utility_functions.sh
|
. ../utility_functions.sh
|
||||||
|
|
||||||
. ../../common-script.sh
|
. ../../common-script.sh
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ./utility_functions.sh
|
. ../utility_functions.sh
|
||||||
|
|
||||||
. ../../common-script.sh
|
. ../../common-script.sh
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../../common-script.sh
|
. ../../common-script.sh
|
||||||
. ./utility_functions.sh
|
|
||||||
|
|
||||||
|
. ../utility_functions.sh
|
||||||
|
|
||||||
|
addToGroup() {
|
||||||
clear
|
clear
|
||||||
printf "%b\n" "${YELLOW}Add to group${RC}"
|
printf "%b\n" "${YELLOW}Add to group${RC}"
|
||||||
printf "%b\n" "${YELLOW}=================${RC}"
|
printf "%b\n" "${YELLOW}=================${RC}"
|
||||||
|
|
||||||
username=$(promptUsername "" "non-root") || exit 1
|
printf "%b" "${YELLOW}Enter the username: ${RC}"
|
||||||
|
read -r username
|
||||||
user_groups=$(groups "$username" | cut -d: -f2 | sort | tr '\n' ' ')
|
user_groups=$(groups "$username" | cut -d: -f2 | sort | tr '\n' ' ')
|
||||||
|
|
||||||
printf "%b\n" "${YELLOW}Groups user $username is in:${RC} $user_groups"
|
printf "%b\n" "${YELLOW}Groups user $username is in:${RC} $user_groups"
|
||||||
|
@ -18,20 +21,27 @@ available_groups=$(cut -d: -f1 /etc/group | sort | tr '\n' ' ')
|
||||||
printf "%b\n" "${YELLOW}Available groups:${RC} $available_groups"
|
printf "%b\n" "${YELLOW}Available groups:${RC} $available_groups"
|
||||||
printf "%b\n" "${YELLOW}=================${RC}"
|
printf "%b\n" "${YELLOW}=================${RC}"
|
||||||
|
|
||||||
printf "%b\n" "${YELLOW}Enter the groups you want to add user $username to (space-separated):${RC} "
|
printf "%b" "${YELLOW}Enter the groups you want to add user $username to (space-separated): ${RC}"
|
||||||
read -r groups
|
read -r groups
|
||||||
|
|
||||||
checkEmpty "$groups" || exit 1
|
checkEmpty "$groups" || exit 1
|
||||||
checkGroupAvailabe "$groups" "$available_groups" || exit 1
|
if ! checkGroups "$groups" "$available_groups"; then
|
||||||
|
printf "%b\n" "${RED}One or more groups are not available.${RC}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
groups_to_add=$(echo "$groups" | tr ' ' ',')
|
groups_to_add=$(echo "$groups" | tr ' ' ',')
|
||||||
|
|
||||||
printf "Are you sure you want to add user $username to $groups_to_add? [Y/N]: "
|
printf "%b" "${YELLOW}Are you sure you want to add user $username to $groups_to_add? [Y/n]: ${RC}"
|
||||||
read -r confirm
|
read -r confirm
|
||||||
confirmAction || exit 1
|
confirmAction || exit 1
|
||||||
|
|
||||||
$ESCALATION_TOOL usermod -aG $groups_to_add "$username"
|
"$ESCALATION_TOOL" usermod -aG "$groups_to_add" "$username"
|
||||||
|
|
||||||
printf "%b\n" "${GREEN}User successfully added to the $groups_to_add${RC}"
|
printf "%b\n" "${GREEN}User successfully added to the $groups_to_add${RC}"
|
||||||
|
}
|
||||||
|
|
||||||
checkEnv
|
checkEnv
|
||||||
|
checkEscalationTool
|
||||||
|
checkGroups
|
||||||
|
addToGroup
|
|
@ -1,26 +1,38 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../../common-script.sh
|
. ../../common-script.sh
|
||||||
. ./utility_functions.sh
|
|
||||||
|
|
||||||
|
. ../utility_functions.sh
|
||||||
|
|
||||||
|
createUser() {
|
||||||
clear
|
clear
|
||||||
printf "%b\n" "${YELLOW}Create a new user${RC}"
|
printf "%b\n" "${YELLOW}Create a new user${RC}"
|
||||||
printf "%b\n" "${YELLOW}=================${RC}"
|
printf "%b\n" "${YELLOW}=================${RC}"
|
||||||
|
printf "%b" "${YELLOW}Enter the username: ${RC}"
|
||||||
|
read -r username
|
||||||
|
|
||||||
username=$(promptUsername "add" "non-root") || exit 1
|
if ! echo "$username" | grep '^[a-zA-Z]*$' > /dev/null; then
|
||||||
|
printf "%b\n" "${RED}Username must only contain letters and cannot contain spaces.${RC}"
|
||||||
# Check if username is valid
|
|
||||||
if ! echo "$username" | grep '^[a-z][-a-z0-9_]*$' > /dev/null; then
|
|
||||||
printf "%b\n" "${RED}Username must only contain letters, numbers, hyphens, and underscores. It cannot start with a number or contain spaces.${RC}"
|
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
password=$(promptPassword) || exit 1
|
printf "%b" "${YELLOW}Enter the password: ${RC}"
|
||||||
|
read -r password
|
||||||
|
printf "%b" "${YELLOW}Enter the password again: ${RC}"
|
||||||
|
read -r password_confirmation
|
||||||
|
|
||||||
$ESCALATION_TOOL useradd -m "$username" -g users -s /bin/bash
|
if [ "$password" != "$password_confirmation" ]; then
|
||||||
|
printf "%b\n" "${RED}Passwords do not match${RC}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
"$ESCALATION_TOOL" useradd -m "$username" -g users -s /bin/bash
|
||||||
echo "$username:$password" | "$ESCALATION_TOOL" chpasswd
|
echo "$username:$password" | "$ESCALATION_TOOL" chpasswd
|
||||||
|
|
||||||
printf "%b\n" "${GREEN}User $username created successfully${RC}"
|
printf "%b\n" "${GREEN}User $username created successfully${RC}"
|
||||||
printf "%b\n" "${GREEN}To add additional groups use Add User To Groups${RC}"
|
printf "%b\n" "${GREEN}To add additional groups use Add User To Groups${RC}"
|
||||||
|
}
|
||||||
|
|
||||||
checkEnv
|
checkEnv
|
||||||
|
checkEscalationTool
|
||||||
|
createUser
|
||||||
|
|
|
@ -1,20 +1,33 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../../common-script.sh
|
. ../../common-script.sh
|
||||||
. ./utility_functions.sh
|
|
||||||
|
|
||||||
|
. ../utility_functions.sh
|
||||||
|
|
||||||
|
changePassword() {
|
||||||
clear
|
clear
|
||||||
printf "%b\n" "${YELLOW}Change password${RC}"
|
printf "%b\n" "${YELLOW}Change password${RC}"
|
||||||
printf "%b\n" "${YELLOW}=================${RC}"
|
printf "%b\n" "${YELLOW}=================${RC}"
|
||||||
|
|
||||||
username=$(promptUsername "" "root") || exit 1
|
printf "%b" "${YELLOW}Enter the username: ${RC}"
|
||||||
password=$(promptPassword) || exit 1
|
read -r username
|
||||||
|
|
||||||
printf "Are you sure you want to change password for $username? [Y/N]: "
|
if id "$username" > /dev/null 2>&1; then
|
||||||
|
printf "%b" "${YELLOW}Enter new password: ${RC}"
|
||||||
|
read -r password
|
||||||
|
|
||||||
|
printf "%b" "${YELLOW}Are you sure you want to change password for ""$username""? [Y/n]: ${RC}"
|
||||||
read -r confirm
|
read -r confirm
|
||||||
confirmAction || exit 1
|
confirmAction || exit 1
|
||||||
|
|
||||||
echo "$username:$password" | "$ESCALATION_TOOL" chpasswd
|
echo "$username:$password" | "$ESCALATION_TOOL" chpasswd
|
||||||
printf "%b\n" "${GREEN}Password changed successfully${RC}"
|
printf "%b\n" "${GREEN}Password changed successfully${RC}"
|
||||||
|
else
|
||||||
|
printf "%b\n" "${RED}User $username does not exist.${RC}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
checkEnv
|
checkEnv
|
||||||
|
checkEscalationTool
|
||||||
|
changePassword
|
|
@ -1,27 +1,30 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../../common-script.sh
|
. ../../common-script.sh
|
||||||
. ./utility_functions.sh
|
|
||||||
|
|
||||||
|
. ../utility_functions.sh
|
||||||
|
|
||||||
|
deleteUser() {
|
||||||
clear
|
clear
|
||||||
printf "%b\n" "${YELLOW}Delete a user${RC}"
|
printf "%b\n" "${YELLOW}Delete a user${RC}"
|
||||||
printf "%b\n" "${YELLOW}=================${RC}"
|
printf "%b\n" "${YELLOW}=================${RC}"
|
||||||
|
|
||||||
username=$(promptUsername "" "non-root") || exit 1
|
printf "%b" "${YELLOW}Enter the username: ${RC}"
|
||||||
|
read -r username
|
||||||
|
|
||||||
# Check if current user
|
if id "$username" > /dev/null 2>&1; then
|
||||||
if [ "$username" = "$USER" ]; then
|
printf "%b" "${YELLOW}Are you sure you want to delete user ""$username""? [Y/n]: ${RC}"
|
||||||
printf "%b\n" "${RED}Cannot delete the current user${RC}"
|
|
||||||
printf "%b\n" "${RED}Press [Enter] to continue...${RC}"
|
|
||||||
read -r dummy
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
|
|
||||||
printf "Are you sure you want to delete user $username? [Y/N]: "
|
|
||||||
read -r confirm
|
read -r confirm
|
||||||
confirmAction || exit 1
|
confirmAction || exit 1
|
||||||
|
|
||||||
$ESCALATION_TOOL userdel --remove "$username" 2>/dev/null
|
$ESCALATION_TOOL userdel --remove "$username" 2>/dev/null
|
||||||
printf "%b\n" "${GREEN}User $username deleted successfully${RC}"
|
printf "%b\n" "${GREEN}User $username deleted successfully${RC}"
|
||||||
|
else
|
||||||
|
printf "%b\n" "${RED}User $username does not exist.${RC}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
checkEnv
|
checkEnv
|
||||||
|
checkEscalationTool
|
||||||
|
deleteUser
|
|
@ -1,32 +1,47 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
|
|
||||||
. ../../common-script.sh
|
. ../../common-script.sh
|
||||||
. ./utility_functions.sh
|
|
||||||
|
|
||||||
|
. ../utility_functions.sh
|
||||||
|
|
||||||
|
removeFromGroup() {
|
||||||
clear
|
clear
|
||||||
printf "%b\n" "${YELLOW}Remove from group${RC}"
|
printf "%b\n" "${YELLOW}Remove from group${RC}"
|
||||||
printf "%b\n" "${YELLOW}=================${RC}"
|
printf "%b\n" "${YELLOW}=================${RC}"
|
||||||
|
|
||||||
username=$(promptUsername "" "non-root") || exit 1
|
printf "%b" "${YELLOW}Enter the username: ${RC}"
|
||||||
|
read -r username
|
||||||
|
|
||||||
|
if ! id "$username" > /dev/null 2>&1; then
|
||||||
|
printf "%b\n" "${RED}User $username does not exist.${RC}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
user_groups=$(groups "$username" | cut -d: -f2 | sort | tr '\n' ' ')
|
user_groups=$(groups "$username" | cut -d: -f2 | sort | tr '\n' ' ')
|
||||||
|
|
||||||
printf "%b\n" "${YELLOW}Groups user $username is in:${RC} $user_groups"
|
printf "%b\n" "${YELLOW}Groups user $username is in:${RC} $user_groups"
|
||||||
printf "%b\n" "${YELLOW}=================${RC}"
|
printf "%b\n" "${YELLOW}=================${RC}"
|
||||||
|
|
||||||
printf "%b\n" "${YELLOW}Enter the groups you want to remove user $username from (space-separated):${RC} "
|
printf "%b" "${YELLOW}Enter the groups you want to remove user $username from (space-separated): ${RC} "
|
||||||
read -r groups
|
read -r groups
|
||||||
|
|
||||||
checkEmpty "$groups" || exit 1
|
checkEmpty "$groups" || exit 1
|
||||||
checkGroupAvailabe "$groups" "$user_groups" || exit 1
|
if ! checkGroups "$groups" "$user_groups"; then
|
||||||
|
printf "%b\n" "${RED}One or more specified groups do not exist.${RC}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
groups_to_remove=$(echo "$groups" | tr ' ' ',')
|
groups_to_remove=$(echo "$groups" | tr ' ' ',')
|
||||||
|
|
||||||
printf "Are you sure you want to remove user $username from $groups_to_remove? [Y/N]: "
|
printf "%b" "${YELLOW}Are you sure you want to remove user $username from $groups_to_remove? [Y/n]: ${RC}"
|
||||||
read -r confirm
|
read -r confirm
|
||||||
confirmAction || exit 1
|
confirmAction || exit 1
|
||||||
|
|
||||||
$ESCALATION_TOOL usermod -rG $groups_to_remove "$username"
|
$ESCALATION_TOOL usermod -rG $groups_to_remove "$username"
|
||||||
|
|
||||||
printf "%b\n" "${GREEN}User successfully removed from $groups_to_remove${RC}"
|
printf "%b\n" "${GREEN}User successfully removed from $groups_to_remove${RC}"
|
||||||
|
}
|
||||||
|
|
||||||
checkEnv
|
checkEnv
|
||||||
|
checkEscalationTool
|
||||||
|
removeFromGroup
|
|
@ -1,103 +0,0 @@
|
||||||
#!/bin/sh -e
|
|
||||||
|
|
||||||
. ../../common-script.sh
|
|
||||||
|
|
||||||
# Prompt for username
|
|
||||||
promptUsername() {
|
|
||||||
printf "Enter the username: "
|
|
||||||
read -r username
|
|
||||||
|
|
||||||
checkEmpty "$username";
|
|
||||||
|
|
||||||
if [ "$1" = "add" ]; then
|
|
||||||
checkUserExistence "$username" "$1"
|
|
||||||
else
|
|
||||||
checkUserExistence "$username" "$1"
|
|
||||||
checkReservedUsername "$username" "$2"
|
|
||||||
fi
|
|
||||||
echo "$username"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
# Prompt for password
|
|
||||||
promptPassword() {
|
|
||||||
stty -echo
|
|
||||||
printf "Enter the password (PASSWORD IS HIDDEN): "
|
|
||||||
read -r password1
|
|
||||||
echo >&2
|
|
||||||
printf "Re-enter the password (PASSWORD IS HIDDEN): "
|
|
||||||
read -r password2
|
|
||||||
echo >&2
|
|
||||||
stty echo
|
|
||||||
|
|
||||||
if ! checkEmpty "$password1"; then
|
|
||||||
promptPassword
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$password1" != "$password2" ]; then
|
|
||||||
printf "%b\n" "${RED}Passwords do not match${RC}" >&2
|
|
||||||
promptPassword
|
|
||||||
else
|
|
||||||
echo $password1
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Check if input is empty
|
|
||||||
checkEmpty() {
|
|
||||||
if [ -z "$1" ]; then
|
|
||||||
printf "%b\n" "${RED}Empty value is not allowed${RC}" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Check if user exists
|
|
||||||
checkUserExistence() {
|
|
||||||
if [ "$2" = "add" ]; then
|
|
||||||
if id "$1" > /dev/null 2>&1; then
|
|
||||||
printf "%b\n" "${RED}User already exists${RC}" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
if ! id "$1" > /dev/null 2>&1; then
|
|
||||||
printf "%b\n" "${RED}User does not exist${RC}" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Check if user is reserved
|
|
||||||
checkReservedUsername() {
|
|
||||||
uid=$(id -u "$1")
|
|
||||||
if [ "$2" = "root" ]; then
|
|
||||||
if [ "$uid" -le 999 ] && [ "$uid" -ne 0 ]; then
|
|
||||||
printf "%b\n" "${RED}Cannot modify system users${RC}" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
if [ "$(id -u "$1")" -le 999 ]; then
|
|
||||||
printf "%b\n" "${RED}Cannot modify system users${RC}" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Check if user is reserved
|
|
||||||
confirmAction() {
|
|
||||||
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
|
|
||||||
printf "%b\n" "${RED}Cancelled operation...${RC}" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Check if group is available
|
|
||||||
checkGroupAvailabe() {
|
|
||||||
for group in $1; do
|
|
||||||
if ! echo "$2" | grep -wq "$group"; then
|
|
||||||
printf "%b\n" "${RED}Group $group not avaiable${RC}" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
checkEnv
|
|
||||||
checkEscalationTool
|
|
|
@ -84,6 +84,31 @@ confirm_action() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
checkEmpty() {
|
||||||
|
if [ -z "$1" ]; then
|
||||||
|
printf "%b\n" "${RED}Empty value is not allowed${RC}" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
checkGroups() {
|
||||||
|
groups="$1"
|
||||||
|
available_groups="$2"
|
||||||
|
for group in $groups; do
|
||||||
|
if ! echo "$available_groups" | grep -q -w "$group"; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
confirmAction() {
|
||||||
|
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
|
||||||
|
printf "%b\n" "${RED}Cancelled operation...${RC}" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
checkEnv
|
checkEnv
|
||||||
checkEscalationTool
|
checkEscalationTool
|
||||||
setup_xrandr
|
setup_xrandr
|
Loading…
Reference in New Issue
Block a user