Fixed add-usergroup and post-postfix

This commit is contained in:
Brian Zalewski 2024-05-20 07:11:48 +00:00
parent a63add883f
commit 94ad04c7c6
2 changed files with 122 additions and 101 deletions

View file

@ -3,112 +3,108 @@
# @brief Add a user and a group with the same name on either Linux or macOS # @brief Add a user and a group with the same name on either Linux or macOS
# @description # @description
# This script is utilized by other scripts to ensure that there is both a user and group # 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 # named by the two arguments that this executable accepts. It checks whether or not
# there is already a user / group with the name present on the system before running # 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 # any code.
# on the system plus one.
# Check if the script is being run as root ### Check if the script is being run as root
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
if command -v logg > /dev/null; then logg error "This script must be run as root"
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 exit 1
fi fi
# Check if the correct number of arguments is provided ### Check if the correct number of arguments is provided
if [[ $# -ne 2 ]]; then if [ "$#" -ne 2 ]; then
if command -v logg > /dev/null; then logg error "Usage: $0 <username> <groupname>"
logg info "Usage: $0 <user> <group>" exit 1
else
echo -e "\e[93mUsage: $0 <user> <group>\e[0m"
fi
exit 1
fi fi
USER=$1 ### Assign arguments to variables
GROUP=$2 USERNAME="$1"
GROUPNAME="$2"
# Check if the operating system is macOS ### Function to find the next available system ID on macOS
if [ -d /Applications ] && [ -d /System ]; then find_next_system_id_macos() {
if command -v logg > /dev/null; then local id_type="$1"
logg info "Creating group and user ${GROUP} on macOS..." local id_tag="$2"
else local current_ids="$(dscl . -list /$id_type "$id_tag" | awk '{print $2}')"
echo -e "\e[96mCreating group and user ${GROUP} on macOS...\e[0m" local min_id=20 # Start from 20 to avoid conflict with default system users/groups
fi
# Ensure group exists for id in $current_ids; do
if ! dscl . read "/Groups/$GROUP" PrimaryGroupID &> /dev/null; then if [ "$id" -ge "$min_id" ] && [ "$id" -lt 500 ]; then
MAX_ID_GROUP="$(dscl . -list /Groups PrimaryGroupID | awk '{print $2}' | sort -ug | tail -1)" min_id="$((id + 1))"
PRIMARY_GROUP_ID="$((MAX_ID_GROUP+1))" fi
dscl . create "/Groups/$GROUP" done
# This also sets the PrimaryGroupID echo "$min_id"
sudo dscl . create "/Groups/$GROUP" PrimaryGroupID "$PRIMARY_GROUP_ID" }
sudo dscl . append "/Groups/$GROUP" GroupMembership "$USER"
else
PRIMARY_GROUP_ID="$(dscl . read "/Groups/$GROUP" PrimaryGroupID | awk '{print $2}')"
fi
# Ensure user exists ### Detect the operating system
if ! dscl . read "/Users/$GROUP" UniqueID &> /dev/null; then OS="$(uname)"
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
# Add the user to the group if [ "$OS" == "Darwin" ]; then
sudo dseditgroup -o edit -t user -a "$GROUP" "$GROUP" ### macOS
# 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"
### Create the group if it does not exist
if command -v logg > /dev/null; then if ! dscl . -list /Groups | grep -q "^$GROUPNAME\$"; then
logg info "Group and user ${GROUP} created successfully on macOS" logg info "Creating system group: $GROUPNAME"
else SYSTEM_GID="$(find_next_system_id_macos "Groups" "PrimaryGroupID")"
echo -e "\e[92mGroup and user ${GROUP} created successfully on macOS\e[0m" logg info "Initializing $GROUPNAME group"
fi sudo dscl . -create "/Groups/$GROUPNAME"
elif [[ "$(uname)" == "Linux" ]]; then logg info "Assigning $SYSTEM_GID PrimaryGroupID to group"
if command -v logg > /dev/null; then sudo dscl . -create "/Groups/$GROUPNAME" PrimaryGroupID "$SYSTEM_GID"
logg info "Creating group and user ${GROUP} on Linux..."
else
echo -e "\e[96mCreating group and user ${GROUP} on Linux...\e[0m"
fi
# 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"
else else
echo -e "\e[93mGroup ${GROUP} already exists\e[0m" logg info "Group $GROUPNAME already exists"
SYSTEM_GID=$(dscl . -read "/Groups/$GROUPNAME" PrimaryGroupID | awk '{print $2}')
fi fi
fi
# Check if the user already exists ### Create the user if it does not exist
if ! id -u "${GROUP}" >/dev/null 2>&1; then if ! id -u "$USERNAME" > /dev/null 2>&1; then
# Create the user and assign it to the group logg info "Creating system user: $USERNAME"
useradd -g "${GROUP}" "${GROUP}" SYSTEM_UID="$(find_next_system_id_macos "Users" "UniqueID")"
else logg info "Initializing $USERNAME user"
if command -v logg > /dev/null; then sudo dscl . -create "/Users/$USERNAME"
logg info "User ${GROUP} already exists" logg info "Assigning $USERNAME user attributes"
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
logg info "Finished assigning $USERNAME user attributes"
else else
echo -e "\e[93mUser ${GROUP} already exists\e[0m" logg info "User $USERNAME already exists"
fi fi
fi
if command -v logg > /dev/null; then ### Add the user to the group
logg success "Group and user ${GROUP} created successfully on Linux" logg info "Adding user $USERNAME to group $GROUPNAME"
else sudo dscl . -append "/Groups/$GROUPNAME" GroupMembership "$USERNAME"
echo -e "\e[92mGroup and user ${GROUP} created successfully on Linux\e[0m"
fi logg info "System user $USERNAME added to system group $GROUPNAME successfully."
elif [ "$OS" == "Linux" ]; then
### Linux
### Create the group if it does not exist
if ! getent group "$GROUPNAME" > /dev/null 2>&1; then
logg info "Creating system group: $GROUPNAME"
sudo groupadd -r "$GROUPNAME"
else
logg info "Group $GROUPNAME already exists"
fi
### Create the user if it does not exist
if ! id -u "$USERNAME" > /dev/null 2>&1; then
logg info "Creating system user: $USERNAME"
sudo useradd -r -g "$GROUPNAME" -s /bin/bash -M -N "$USERNAME"
else
logg info "User $USERNAME already exists"
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"
logg info "System user $USERNAME added to system group $GROUPNAME successfully."
else
logg info "Unsupported operating system: $OS"
exit 1
fi fi

View file

@ -24,6 +24,19 @@ else
logg warn "SENDGRID_API_KEY is missing from ${XDG_DATA_HOME:-$HOME/.local/share}/chezmoi/home/.chezmoitemplates/secrets" logg warn "SENDGRID_API_KEY is missing from ${XDG_DATA_HOME:-$HOME/.local/share}/chezmoi/home/.chezmoitemplates/secrets"
fi fi
### Acquire PUBLIC_SERVICES_DOMAIN and PRIMARY_EMAIL
if command -v yq > /dev/null; then
if [ -f "${XDG_CONFIG_HOME:-$HOME/.config}/chezmoi/chezmoi.yaml" ]; then
PUBLIC_SERVICES_DOMAIN="$(yq '.data.host.domain' "${XDG_CONFIG_HOME:-$HOME/.config}/chezmoi/chezmoi.yaml")"
PRIMARY_EMAIL="$(yq '.data.user.email' "${XDG_CONFIG_HOME:-$HOME/.config}/chezmoi/chezmoi.yaml")"
else
logg warn "${XDG_CONFIG_HOME:-$HOME/.config}/chezmoi/chezmoi.yaml is missing and is required for acquiring the PUBLIC_SERVICES_DOMAIN and PRIMARY_EMAIL"
fi
else
logg warn 'yq is not installed on the system and is required for populating the PUBLIC_SERVICES_DOMAIN and PRIMARY_EMAIL'
fi
### Setup Postfix if SENDGRID_API_KEY is retrieved ### Setup Postfix if SENDGRID_API_KEY is retrieved
if [ -n "$SENDGRID_API_KEY" ] && [ "$SENDGRID_API_KEY" != "" ]; then if [ -n "$SENDGRID_API_KEY" ] && [ "$SENDGRID_API_KEY" != "" ]; then
if command -v postfix > /dev/null; then if command -v postfix > /dev/null; then
@ -74,28 +87,40 @@ if [ -n "$SENDGRID_API_KEY" ] && [ "$SENDGRID_API_KEY" != "" ]; then
else else
logg warn '~/.config/postfix/sasl_passwd file is missing' logg warn '~/.config/postfix/sasl_passwd file is missing'
fi fi
### Forward root e-mails ### Forward root e-mails
if [ -d /root ]; then if [ -n "$PRIMARY_EMAIL" ]; then
logg info "Forwarding root e-mails to $PRIMARY_EMAIL" if [ -d /root ]; then
echo "$PRIMARY_EMAIL" | sudo tee /root/.forward > /dev/null || logg error 'Failed to set root user .forward file' logg info "Forwarding root e-mails to $PRIMARY_EMAIL"
elif [ -d /var/root ]; then echo "$PRIMARY_EMAIL" | sudo tee /root/.forward > /dev/null || logg error 'Failed to set root user .forward file'
logg info "Forwarding root e-mails to $PRIMARY_EMAIL" elif [ -d /var/root ]; then
echo "$PRIMARY_EMAIL" | sudo tee /var/root/.forward > /dev/null || logg error 'Failed to set root user .forward file' logg info "Forwarding root e-mails to $PRIMARY_EMAIL"
echo "$PRIMARY_EMAIL" | sudo tee /var/root/.forward > /dev/null || logg error 'Failed to set root user .forward file'
else
logg warn 'Unable to identify root user home directory'
fi
else else
logg warn 'Unable to identify root user home directory' logg warn 'PRIMARY_EMAIL is undefined so cannot setup root email forwarding'
fi fi
### Ensure /etc/postfix/header_checks exists ### Ensure /etc/postfix/header_checks exists
if [ ! -d /etc/postfix/header_checks ]; then if [ ! -d /etc/postfix/header_checks ]; then
logg info 'Creating /etc/postfix/header_checks since it does not exist' logg info 'Creating /etc/postfix/header_checks since it does not exist'
sudo touch /etc/postfix/header_checks sudo touch /etc/postfix/header_checks
fi fi
### Re-write header From for SendGrid ### Re-write header From for SendGrid
if ! cat /etc/postfix/header_checks | grep "no-reply@${PUBLIC_SERVICES_DOMAIN}" > /dev/null; then if [ -n "$PUBLIC_SERVICES_DOMAIN" ]; then
logg info 'Added From REPLACE to /etc/postfix/header_checks' if ! cat /etc/postfix/header_checks | grep "no-reply@${PUBLIC_SERVICES_DOMAIN}" > /dev/null; then
echo "/^From:.*@${PUBLIC_SERVICES_DOMAIN}/ REPLACE From: no-reply@${PUBLIC_SERVICES_DOMAIN}" | sudo tee -a /etc/postfix/header_checks > /dev/null logg info 'Added From REPLACE to /etc/postfix/header_checks'
echo "/^From:.*@${PUBLIC_SERVICES_DOMAIN}/ REPLACE From: no-reply@${PUBLIC_SERVICES_DOMAIN}" | sudo tee -a /etc/postfix/header_checks > /dev/null
fi
else
logg warn 'PUBLIC_SERVICES_DOMAIN is undefined'
fi fi
### Update aliases ### Update aliases
if [ -f /etc/aliases ]; then if [ -f /etc/aliases ] && [ -n "$PRIMARY_EMAIL" ]; then
logg info "Forward root e-mails to $PRIMARY_EMAIL" logg info "Forward root e-mails to $PRIMARY_EMAIL"
ALIASES_TMP="$(mktemp)" ALIASES_TMP="$(mktemp)"
logg info "Setting $PRIMARY_EMAIL as root e-mail in temporary file" logg info "Setting $PRIMARY_EMAIL as root e-mail in temporary file"
@ -129,7 +154,7 @@ if [ -n "$SENDGRID_API_KEY" ] && [ "$SENDGRID_API_KEY" != "" ]; then
# but since we are removing it to ensure proper permissions, this method is commented out. # but since we are removing it to ensure proper permissions, this method is commented out.
# logg info 'Running newaliases to regenerate the alias database' && sudo newaliases # logg info 'Running newaliases to regenerate the alias database' && sudo newaliases
else else
logg warn '/etc/aliases does not appear to exist' logg warn '/etc/aliases does not appear to exist or PRIMARY_EMAIL is undefined'
fi fi
if [ -d /Applications ] && [ -d /System ]; then if [ -d /Applications ] && [ -d /System ]; then
### macOS ### macOS