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
|
|
|
|
# named by the single argument that this executable accepts. It checks whether or not
|
|
|
|
# there is already a user / group with the name present on the system before running
|
|
|
|
# any code. On macOS, it assigns the user an ID that equal to the maximum user ID present
|
|
|
|
# on the system plus one.
|
2023-06-22 21:39:22 -07:00
|
|
|
|
|
|
|
# Check if the script is being run as root
|
|
|
|
if [[ $EUID -ne 0 ]]; then
|
2023-12-24 22:50:02 -08:00
|
|
|
if command -v logg > /dev/null; then
|
|
|
|
logg error "This script must be run as root"
|
|
|
|
else
|
|
|
|
echo -e "\e[91mThis script must be run as root\e[0m"
|
|
|
|
fi
|
|
|
|
exit 1
|
2023-06-22 21:39:22 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if the correct number of arguments is provided
|
2023-12-09 04:42:09 -08:00
|
|
|
if [[ $# -ne 2 ]]; then
|
2023-12-24 22:50:02 -08:00
|
|
|
if command -v logg > /dev/null; then
|
|
|
|
logg info "Usage: $0 <user> <group>"
|
|
|
|
else
|
|
|
|
echo -e "\e[93mUsage: $0 <user> <group>\e[0m"
|
|
|
|
fi
|
|
|
|
exit 1
|
2023-06-22 21:39:22 -07:00
|
|
|
fi
|
|
|
|
|
2023-12-09 04:42:09 -08:00
|
|
|
USER=$1
|
|
|
|
GROUP=$2
|
2023-06-22 21:39:22 -07:00
|
|
|
|
|
|
|
# Check if the operating system is macOS
|
2023-12-09 04:42:09 -08:00
|
|
|
if [ -d /Applications ] && [ -d /System ]; then
|
2023-12-24 22:50:02 -08:00
|
|
|
if command -v logg > /dev/null; then
|
|
|
|
logg info "Creating group and user ${GROUP} on macOS..."
|
|
|
|
else
|
|
|
|
echo -e "\e[96mCreating group and user ${GROUP} on macOS...\e[0m"
|
|
|
|
fi
|
2023-06-22 21:39:22 -07:00
|
|
|
|
2023-12-24 22:50:02 -08:00
|
|
|
# Ensure group exists
|
|
|
|
if ! dscl . read "/Groups/$GROUP" gid &> /dev/null; then
|
|
|
|
MAX_ID_GROUP="$(dscl . -list /Groups gid | awk '{print $2}' | sort -ug | tail -1)"
|
|
|
|
PRIMARY_GROUP_ID="$((MAX_ID_GROUP+1))"
|
|
|
|
dscl . create "/Groups/$GROUP"
|
|
|
|
# This also sets the PrimaryGroupID
|
|
|
|
sudo dscl . create "/Groups/$GROUP" gid "$PRIMARY_GROUP_ID"
|
|
|
|
sudo dscl . append "/Groups/$GROUP" GroupMembership "$USER"
|
|
|
|
else
|
|
|
|
PRIMARY_GROUP_ID="$(dscl . read "/Groups/$GROUP" gid | awk '{print $2}')"
|
|
|
|
fi
|
2023-06-22 21:39:22 -07:00
|
|
|
|
2023-12-24 22:50:02 -08:00
|
|
|
# Ensure user exists
|
|
|
|
if ! dscl . read "/Users/$GROUP" UniqueID &> /dev/null; then
|
|
|
|
MAX_ID_USER="$(dscl . -list /Users UniqueID | sort -nr -k 2 | head -1 | grep -oE "[0-9]+$")"
|
|
|
|
UNIQUE_ID="$((MAX_ID_USER+1))"
|
|
|
|
sudo dscl . create "/Users/$GROUP"
|
|
|
|
sudo dscl . create "/Users/$GROUP" UniqueID "$UNIQUE_ID"
|
|
|
|
sudo dscl . create "/Users/$GROUP" PrimaryGroupID "$PRIMARY_GROUP_ID"
|
|
|
|
else
|
|
|
|
UNIQUE_ID="$(dscl . read "/Users/$GROUP" UniqueID | awk '{print $2}')"
|
|
|
|
fi
|
2023-06-22 21:39:22 -07:00
|
|
|
|
2023-12-24 22:50:02 -08:00
|
|
|
# Add the user to the group
|
|
|
|
sudo dseditgroup -o edit -t user -a "$GROUP" "$GROUP"
|
|
|
|
# Add the current user to the group
|
|
|
|
sudo dseditgroup -o edit -t user -a "$USER" "$GROUP"
|
|
|
|
# Add USER group to the group
|
|
|
|
sudo dseditgroup -o edit -t group -a "$USER" "$GROUP"
|
2023-11-27 17:03:00 -08:00
|
|
|
|
|
|
|
|
2023-12-24 22:50:02 -08:00
|
|
|
if command -v logg > /dev/null; then
|
|
|
|
logg info "Group and user ${GROUP} created successfully on macOS"
|
|
|
|
else
|
|
|
|
echo -e "\e[92mGroup and user ${GROUP} created successfully on macOS\e[0m"
|
|
|
|
fi
|
2023-06-22 21:39:22 -07:00
|
|
|
elif [[ "$(uname)" == "Linux" ]]; then
|
2023-12-24 22:50:02 -08:00
|
|
|
if command -v logg > /dev/null; then
|
|
|
|
logg info "Creating group and user ${GROUP} on Linux..."
|
|
|
|
else
|
|
|
|
echo -e "\e[96mCreating group and user ${GROUP} on Linux...\e[0m"
|
|
|
|
fi
|
2023-06-22 21:39:22 -07:00
|
|
|
|
2023-12-24 22:50:02 -08:00
|
|
|
# Check if the group already exists
|
|
|
|
if ! grep -qE "^${GROUP}:" /etc/group; then
|
|
|
|
# Create the group
|
|
|
|
groupadd "${GROUP}"
|
|
|
|
else
|
|
|
|
if command -v logg > /dev/null; then
|
|
|
|
logg info "Group ${GROUP} already exists"
|
2023-06-22 21:39:22 -07:00
|
|
|
else
|
2023-12-24 22:50:02 -08:00
|
|
|
echo -e "\e[93mGroup ${GROUP} already exists\e[0m"
|
2023-06-22 21:39:22 -07:00
|
|
|
fi
|
2023-12-24 22:50:02 -08:00
|
|
|
fi
|
2023-06-22 21:39:22 -07:00
|
|
|
|
2023-12-24 22:50:02 -08:00
|
|
|
# Check if the user already exists
|
|
|
|
if ! id -u "${GROUP}" >/dev/null 2>&1; then
|
|
|
|
# Create the user and assign it to the group
|
|
|
|
useradd -g "${GROUP}" "${GROUP}"
|
|
|
|
else
|
2023-07-14 22:28:48 -07:00
|
|
|
if command -v logg > /dev/null; then
|
2023-12-24 22:50:02 -08:00
|
|
|
logg info "User ${GROUP} already exists"
|
2023-07-14 22:28:48 -07:00
|
|
|
else
|
2023-12-24 22:50:02 -08:00
|
|
|
echo -e "\e[93mUser ${GROUP} already exists\e[0m"
|
2023-07-14 22:28:48 -07:00
|
|
|
fi
|
2023-12-24 22:50:02 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
if command -v logg > /dev/null; then
|
|
|
|
logg success "Group and user ${GROUP} created successfully on Linux"
|
|
|
|
else
|
|
|
|
echo -e "\e[92mGroup and user ${GROUP} created successfully on Linux\e[0m"
|
|
|
|
fi
|
2023-06-22 21:39:22 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
# If the operating system is neither macOS nor Linux, display an error message
|
2023-07-14 22:28:48 -07:00
|
|
|
if command -v logg > /dev/null; then
|
2023-12-24 22:50:02 -08:00
|
|
|
logg error "Unsupported operating system"
|
2023-07-14 22:28:48 -07:00
|
|
|
else
|
2023-12-24 22:50:02 -08:00
|
|
|
echo -e "\e[91mUnsupported operating system\e[0m"
|
2023-07-14 22:28:48 -07:00
|
|
|
fi
|