install.fairie/home/dot_local/bin/executable_add-usergroup

113 lines
4.1 KiB
Text
Raw Normal View History

2023-06-22 21:39:22 -07:00
#!/usr/bin/env bash
2023-12-09 04:42:09 -08:00
# @file ~/.local/bin/add-usergroup
2023-06-22 21:39:22 -07:00
# @brief Add a user and a group with the same name on either Linux or macOS
# @description
2023-12-24 22:50:02 -08:00
# This script is utilized by other scripts to ensure that there is both a user and group
2024-05-20 00:11:48 -07:00
# named by the two arguments that this executable accepts. It checks whether or not
2023-12-24 22:50:02 -08:00
# there is already a user / group with the name present on the system before running
2024-05-20 00:11:48 -07:00
# any code.
2023-06-22 21:39:22 -07:00
2024-05-27 04:15:03 -07:00
set -euo pipefail
2024-05-20 00:11:48 -07:00
### Check if the script is being run as root
2023-06-22 21:39:22 -07:00
if [[ $EUID -ne 0 ]]; then
2024-05-27 20:50:11 -07:00
gum log -sl error "This script must be run as root"
2023-12-24 22:50:02 -08:00
exit 1
2023-06-22 21:39:22 -07:00
fi
2024-05-20 00:11:48 -07:00
### Check if the correct number of arguments is provided
if [ "$#" -ne 2 ]; then
2024-05-27 20:50:11 -07:00
gum log -sl error "Usage: $0 <username> <groupname>"
2024-05-20 00:11:48 -07:00
exit 1
2023-06-22 21:39:22 -07:00
fi
2024-05-20 00:11:48 -07:00
### Assign arguments to variables
USERNAME="$1"
GROUPNAME="$2"
### Function to find the next available system ID on macOS
find_next_system_id_macos() {
local id_type="$1"
local id_tag="$2"
local current_ids="$(dscl . -list /$id_type "$id_tag" | awk '{print $2}')"
local min_id=20 # Start from 20 to avoid conflict with default system users/groups
for id in $current_ids; do
if [ "$id" -ge "$min_id" ] && [ "$id" -lt 500 ]; then
min_id="$((id + 1))"
fi
done
echo "$min_id"
}
### Detect the operating system
OS="$(uname)"
if [ "$OS" == "Darwin" ]; then
### macOS
### Create the group if it does not exist
if ! dscl . -list /Groups | grep -q "^$GROUPNAME\$"; then
2024-05-27 20:50:11 -07:00
gum log -sl info "Creating system group: $GROUPNAME"
2024-05-20 00:11:48 -07:00
SYSTEM_GID="$(find_next_system_id_macos "Groups" "PrimaryGroupID")"
2024-05-27 20:50:11 -07:00
gum log -sl info "Initializing $GROUPNAME group"
2024-05-20 00:11:48 -07:00
sudo dscl . -create "/Groups/$GROUPNAME"
2024-05-27 20:50:11 -07:00
gum log -sl info "Assigning $SYSTEM_GID PrimaryGroupID to group"
2024-05-20 00:11:48 -07:00
sudo dscl . -create "/Groups/$GROUPNAME" PrimaryGroupID "$SYSTEM_GID"
else
2024-05-27 20:50:11 -07:00
gum log -sl info "Group $GROUPNAME already exists"
2024-05-20 00:11:48 -07:00
SYSTEM_GID=$(dscl . -read "/Groups/$GROUPNAME" PrimaryGroupID | awk '{print $2}')
fi
### Create the user if it does not exist
if ! id -u "$USERNAME" > /dev/null 2>&1; then
2024-05-27 20:50:11 -07:00
gum log -sl info "Creating system user: $USERNAME"
2024-05-20 00:11:48 -07:00
SYSTEM_UID="$(find_next_system_id_macos "Users" "UniqueID")"
2024-05-27 20:50:11 -07:00
gum log -sl info "Initializing $USERNAME user"
2024-05-20 00:11:48 -07:00
sudo dscl . -create "/Users/$USERNAME"
2024-05-27 20:50:11 -07:00
gum log -sl info "Assigning $USERNAME user attributes"
2024-05-20 00:11:48 -07:00
sudo dscl . -create "/Users/$USERNAME" UserShell /bin/bash
sudo dscl . -create "/Users/$USERNAME" RealName "$USERNAME"
sudo dscl . -create "/Users/$USERNAME" UniqueID "$SYSTEM_UID"
sudo dscl . -create "/Users/$USERNAME" PrimaryGroupID "$SYSTEM_GID"
sudo dscl . -create "/Users/$USERNAME" NFSHomeDirectory /var/empty
2024-05-27 20:50:11 -07:00
gum log -sl info "Finished assigning $USERNAME user attributes"
2023-06-22 21:39:22 -07:00
else
2024-05-27 20:50:11 -07:00
gum log -sl info "User $USERNAME already exists"
2023-06-22 21:39:22 -07:00
fi
2024-05-20 00:11:48 -07:00
### Add the user to the group
2024-05-27 20:50:11 -07:00
gum log -sl info "Adding user $USERNAME to group $GROUPNAME"
2024-05-20 00:11:48 -07:00
sudo dscl . -append "/Groups/$GROUPNAME" GroupMembership "$USERNAME"
2024-05-27 20:50:11 -07:00
gum log -sl info "System user $USERNAME added to system group $GROUPNAME successfully."
2024-05-20 00:11:48 -07:00
elif [ "$OS" == "Linux" ]; then
### Linux
### Create the group if it does not exist
if ! getent group "$GROUPNAME" > /dev/null 2>&1; then
2024-05-27 20:50:11 -07:00
gum log -sl info "Creating system group: $GROUPNAME"
2024-05-20 00:11:48 -07:00
sudo groupadd -r "$GROUPNAME"
2023-07-14 22:28:48 -07:00
else
2024-05-27 20:50:11 -07:00
gum log -sl info "Group $GROUPNAME already exists"
2023-07-14 22:28:48 -07:00
fi
2023-12-24 22:50:02 -08:00
2024-05-20 00:11:48 -07:00
### Create the user if it does not exist
if ! id -u "$USERNAME" > /dev/null 2>&1; then
2024-05-27 20:50:11 -07:00
gum log -sl info "Creating system user: $USERNAME"
2024-05-20 00:11:48 -07:00
sudo useradd -r -g "$GROUPNAME" -s /bin/bash -M -N "$USERNAME"
else
2024-05-27 20:50:11 -07:00
gum log -sl info "User $USERNAME already exists"
2024-05-20 00:11:48 -07:00
fi
### Add the user to the group (redundant on Linux since user is already added to the group during creation)
sudo usermod -a -G "$GROUPNAME" "$USERNAME"
2024-05-27 20:50:11 -07:00
gum log -sl info "System user $USERNAME added to system group $GROUPNAME successfully."
2024-05-20 00:11:48 -07:00
else
2024-05-27 20:50:11 -07:00
gum log -sl info "Unsupported operating system: $OS"
2024-05-20 00:11:48 -07:00
exit 1
2023-06-22 21:39:22 -07:00
fi