-
Notifications
You must be signed in to change notification settings - Fork 335
feat: Add User Manager #393
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
Merged
ChrisTitusTech
merged 9 commits into
ChrisTitusTech:main
from
jeevithakannan2:user-manipulation
Sep 19, 2024
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8a199a4
Add User Control Panel
jeevithakannan2 427c4bc
Fix non posix
jeevithakannan2 fe115f5
Split script
jeevithakannan2 c252f6d
Merge remote-tracking branch 'upstream/main' into user-manipulation
jeevithakannan2 4b1fc28
Update userguide.md
jeevithakannan2 9cb6a6c
Update utility_functions.sh
jeevithakannan2 e412569
Remove clear
jeevithakannan2 5fcb0f2
Add checkEnv
jeevithakannan2 50f62a9
Merge branch 'main' into user-manipulation
jeevithakannan2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/bin/sh -e | ||
|
||
. ./utility_functions.sh | ||
|
||
clear | ||
printf "%b\n" "${YELLOW}Add to group${RC}" | ||
printf "%b\n" "${YELLOW}=================${RC}" | ||
|
||
username=$(promptUsername "" "non-root") || exit 1 | ||
user_groups=$(groups "$username" | cut -d: -f2 | sort | tr '\n' ' ') | ||
|
||
clear | ||
jeevithakannan2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
printf "%b\n" "${YELLOW}Groups user $username is in:${RC} $user_groups" | ||
printf "%b\n" "${YELLOW}=================${RC}" | ||
|
||
available_groups=$(cut -d: -f1 /etc/group | sort | tr '\n' ' ') | ||
|
||
printf "%b\n" "${YELLOW}Available groups:${RC} $available_groups" | ||
printf "%b\n" "${YELLOW}=================${RC}" | ||
|
||
read -p "Enter the groups you want to add user $username to (space-separated): " groups | ||
|
||
checkEmpty "$groups" || exit 1 | ||
checkGroupAvailabe "$groups" "$available_groups" || exit 1 | ||
|
||
groups_to_add=$(echo "$groups" | tr ' ' ',') | ||
|
||
read -p "Are you sure you want to add user $username to $groups_to_add? [Y/N]: " confirm | ||
confirmAction || exit 1 | ||
|
||
$ESCALATION_TOOL usermod -aG $groups_to_add "$username" | ||
|
||
printf "%b\n" "${GREEN}User successfully added to the $groups_to_add${RC}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/sh -e | ||
|
||
. ./utility_functions.sh | ||
|
||
clear | ||
printf "%b\n" "${YELLOW}Create a new user${RC}" | ||
printf "%b\n" "${YELLOW}=================${RC}" | ||
|
||
username=$(promptUsername "add" "non-root") || exit 1 | ||
|
||
# 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 | ||
fi | ||
|
||
password=$(promptPassword) || exit 1 | ||
|
||
$ESCALATION_TOOL useradd -m "$username" -g users -s /bin/bash | ||
echo "$username:$password" | $ESCALATION_TOOL chpasswd | ||
|
||
printf "%b\n" "${GREEN}User $username created successfully${RC}" | ||
printf "%b\n" "${GREEN}To add additional groups use Add User To Groups${RC}" | ||
jeevithakannan2 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/bin/sh -e | ||
|
||
. ./utility_functions.sh | ||
|
||
clear | ||
printf "%b\n" "${YELLOW}Change password${RC}" | ||
printf "%b\n" "${YELLOW}=================${RC}" | ||
|
||
username=$(promptUsername "" "root") || exit 1 | ||
password=$(promptPassword) || exit 1 | ||
|
||
read -p "Are you sure you want to change password for $username? [Y/N]: " confirm | ||
confirmAction || exit 1 | ||
|
||
echo "$username:$password" | $ESCALATION_TOOL chpasswd | ||
printf "%b\n" "${GREEN}Password changed successfully${RC}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/sh -e | ||
|
||
. ./utility_functions.sh | ||
|
||
clear | ||
printf "%b\n" "${YELLOW}Delete a user${RC}" | ||
printf "%b\n" "${YELLOW}=================${RC}" | ||
|
||
username=$(promptUsername "" "non-root") || exit 1 | ||
|
||
# Check if current user | ||
if [ "$username" = "$USER" ]; then | ||
printf "%b\n" "${RED}Cannot delete the current user${RC}" | ||
printf "%b\n" "${RED}Press [Enter] to continue...${RC}" | ||
read dummy | ||
return | ||
fi | ||
|
||
read -p "Are you sure you want to delete user $username? [Y/N]: " confirm | ||
confirmAction || exit 1 | ||
|
||
$ESCALATION_TOOL userdel --remove "$username" 2>/dev/null | ||
printf "%b\n" "${GREEN}User $username deleted successfully${RC}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/bin/sh -e | ||
|
||
. ./utility_functions.sh | ||
|
||
clear | ||
printf "%b\n" "${YELLOW}Remove from group${RC}" | ||
printf "%b\n" "${YELLOW}=================${RC}" | ||
|
||
username=$(promptUsername "" "non-root") || exit 1 | ||
user_groups=$(groups "$username" | cut -d: -f2 | sort | tr '\n' ' ') | ||
|
||
clear | ||
printf "%b\n" "${YELLOW}Groups user $username is in:${RC} $user_groups" | ||
printf "%b\n" "${YELLOW}=================${RC}" | ||
|
||
read -p "Enter the groups you want to remove user from $username (space-separated): " groups | ||
|
||
checkEmpty "$groups" || exit 1 | ||
checkGroupAvailabe "$groups" "$user_groups" || exit 1 | ||
|
||
groups_to_remove=$(echo "$groups" | tr ' ' ',') | ||
|
||
read -p "Are you sure you want to remove user $username from $groups_to_remove? [Y/N]: " confirm | ||
confirmAction || exit 1 | ||
|
||
$ESCALATION_TOOL usermod -rG $groups_to_remove "$username" | ||
|
||
printf "%b\n" "${GREEN}User successfully removed from $groups_to_remove${RC}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#!/bin/sh -e | ||
|
||
. ../../common-script.sh | ||
|
||
# Prompt for username | ||
promptUsername() { | ||
read -p "Enter the username: " 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 | ||
read -p "Enter the password (PASSWORD IS HIDDEN): " password1 | ||
echo >&2 | ||
read -p "Re-enter the password (PASSWORD IS HIDDEN): " 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}" | ||
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}" | ||
exit 1 | ||
fi | ||
done | ||
} | ||
|
||
checkEscalationTool | ||
jeevithakannan2 marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.