diff --git a/bin/omarchy b/bin/omarchy index 35b8094a65b3..13ff8b212078 100755 --- a/bin/omarchy +++ b/bin/omarchy @@ -89,6 +89,7 @@ GROUP_DESCRIPTIONS[toggle]="Toggle Omarchy features" GROUP_DESCRIPTIONS[transcode]="Image and video transcoding" GROUP_DESCRIPTIONS[tui]="Terminal UI launchers" GROUP_DESCRIPTIONS[update]="Omarchy and system updates" +GROUP_DESCRIPTIONS[user]="Login user management" GROUP_DESCRIPTIONS[version]="Version and channel information" GROUP_DESCRIPTIONS[voxtype]="Voxtype dictation" GROUP_DESCRIPTIONS[weather]="Weather status" diff --git a/bin/omarchy-user-add b/bin/omarchy-user-add new file mode 100755 index 000000000000..bee05d335ebb --- /dev/null +++ b/bin/omarchy-user-add @@ -0,0 +1,189 @@ +#!/bin/bash + +# omarchy:summary=Create a login user with groups and password (sudo managed via 'omarchy user privileges') +# omarchy:args=[username] [--groups g1,g2] [--skip-password] [--yes] +# omarchy:examples=omarchy user add | omarchy user add alice --groups wheel --yes +# omarchy:requires-sudo=true + +set -euo pipefail + +source "${BASH_SOURCE[0]%/*}/omarchy-user-functions" || exit 1 + +PROG="omarchy user add" +ASSUME_YES=0 +SKIP_PASSWORD=0 +USERNAME="" +GROUPS_ARG="" +GROUPS_SET=0 + +fail() { + echo "$PROG: $*" >&2 + exit 1 +} + +interactive() { + [[ -t 0 && -t 1 ]] +} + +show_logo() { + if interactive && [[ -n ${OMARCHY_PATH:-} && -f $OMARCHY_PATH/logo.txt ]]; then + echo -e "\033[32m" + cat <"$OMARCHY_PATH/logo.txt" + echo -e "\033[0m" + echo + fi +} + +confirm() { + local prompt="$1" + if ((ASSUME_YES)); then + return 0 + fi + if interactive; then + gum confirm "$prompt" + else + fail "refusing to continue without confirmation; pass --yes" + fi +} + +need_gum() { + omarchy-cmd-present gum || fail "gum is required for the interactive menu" +} + +# Groups worth offering on an Omarchy desktop. wheel is the admin group; +# the rest grant hardware/device access. Filtered at runtime to groups +# that actually exist on this machine. +# Note: wheel is omitted here; sudo is managed exclusively via 'omarchy user privileges'. +CURATED_GROUPS="video audio optical storage input scanner network power uucp" + +existing_groups() { + local g out="" + for g in $CURATED_GROUPS; do + if getent group "$g" >/dev/null; then + out+="$g"$'\n' + fi + done + printf '%s' "$out" +} + +while (($# > 0)); do + case "$1" in + --groups) + if (($# < 2)) || [[ $2 == -* ]]; then + fail "--groups needs a comma-separated list" + fi + GROUPS_ARG="$2" + GROUPS_SET=1 + shift 2 + ;; + --skip-password) + SKIP_PASSWORD=1 + shift + ;; + --yes | -y) + ASSUME_YES=1 + shift + ;; + -h | --help) + echo "Usage: $PROG [username] [--groups g1,g2] [--skip-password] [--yes]" + echo + echo "Creates a login user, puts it in the given groups (default: none)," + echo "sets its password via passwd (or skip it with --skip-password). Sudo is" + echo "managed separately via 'omarchy user privileges'." + echo "Use --groups '' to explicitly select no supplementary groups." + echo + echo "With no arguments it walks you through each step with gum." + exit 0 + ;; + -*) + fail "unknown option: $1 (try --help)" + ;; + *) + if [[ -n $USERNAME ]]; then + fail "unexpected argument: $1" + fi + USERNAME="$1" + shift + ;; + esac +done + +show_logo + +if [[ -z $USERNAME ]]; then + interactive || fail "a username is required" + need_gum + USERNAME=$(gum input --placeholder "alice" --prompt "Username: ") || fail "cancelled" + [[ -n $USERNAME ]] || fail "nothing entered" +fi +valid_username() { + [[ $1 =~ ^[a-z_][a-z0-9_-]{0,31}$ ]] +} +valid_username "$USERNAME" || fail "invalid username '$USERNAME' (lowercase letters, digits, _ or -)" +is_reserved() { + local reserved='^(root|bin|daemon|mail|ftp|http|nobody|dbus|systemd-coredump|systemd-network|systemd-oom|systemd-journal-remote|systemd-resolve|systemd-timesync|tss|uuidd|alpm|git|avahi|cups|cups-browsed|lp|_talkd|polkitd|rtkit|qemu|brltty|gluster|rpc|libvirt-qemu|pcscd|nvidia-persistenced|sddm)$' + [[ $1 =~ $reserved ]] +} +is_reserved "$USERNAME" && fail "username '$USERNAME' is reserved for the system" +id "$USERNAME" &>/dev/null && fail "user '$USERNAME' already exists (see omarchy user groups)" + +# Older standalone versions used this path for per-user grants. Do not let a +# leftover rule silently give a newly created account sudo, even without wheel. +sudoers_file="/etc/sudoers.d/$USERNAME" +if sudo test -e "$sudoers_file" || sudo test -L "$sudoers_file"; then + fail "$sudoers_file already exists; inspect it with 'sudo visudo -f $sudoers_file' before reusing this login name" +fi + +if ((GROUPS_SET == 0)); then + if interactive; then + need_gum + GROUPS_ARG=$(existing_groups | gum choose --no-limit --header="Groups for $USERNAME? (x to toggle)") || fail "cancelled" + GROUPS_ARG=$(printf '%s' "$GROUPS_ARG" | tr '\n' ',' | sed 's/,$//') + else + GROUPS_ARG="" + fi +fi +GROUPS_CSV="" +if [[ -n $GROUPS_ARG ]]; then + IFS=',' read -ra GROUP_ARR <<<"$GROUPS_ARG" + for g in "${GROUP_ARR[@]}"; do + [[ -n $g ]] || fail "empty group name in '$GROUPS_ARG'" + getent group "$g" >/dev/null || fail "unknown group: $g" + done + GROUPS_CSV=$(IFS=,; printf '%s' "${GROUP_ARR[*]}") +fi + +if ((SKIP_PASSWORD == 0)) && ! interactive; then + fail "setting a password needs a terminal; pass --skip-password or run interactively" +fi + +if [[ -x /usr/bin/bash ]]; then + USER_SHELL=/usr/bin/bash +else + USER_SHELL=/bin/bash +fi + +echo +echo "About to create:" +echo " user: $USERNAME (shell $USER_SHELL)" +echo " groups: ${GROUPS_CSV:-}" +if [[ ,$GROUPS_CSV, == *,wheel,* ]]; then + warn_wheel_admin +fi +confirm "Create user $USERNAME?" || fail "aborted" + +sudo useradd -m -s "$USER_SHELL" "$USERNAME" +if [[ -n $GROUPS_CSV ]]; then + sudo usermod -aG "$GROUPS_CSV" "$USERNAME" +fi + +if ((SKIP_PASSWORD)); then + echo "Skipped password (--skip-password). The greeter needs one: run 'sudo passwd $USERNAME'." +else + echo "Set the password for $USERNAME:" + sudo passwd "$USERNAME" +fi + +echo +echo "Created $USERNAME: $(id "$USERNAME")" +echo "First graphical login provisions the desktop (or run 'omarchy finalize user' as $USERNAME)." diff --git a/bin/omarchy-user-functions b/bin/omarchy-user-functions new file mode 100755 index 000000000000..cf0b73851c50 --- /dev/null +++ b/bin/omarchy-user-functions @@ -0,0 +1,91 @@ +#!/bin/bash + +# omarchy:hidden=true +# omarchy:summary=Provide internal helpers for managing login users and their sudo access + +if [[ ${BASH_SOURCE[0]} == "$0" ]]; then + echo "omarchy-user-functions is an internal function library." >&2 + exit 64 +fi + +# Callers define fail(). Login users are real accounts (UID 1000..59999). + +caller_uid() { + id -u "${SUDO_USER:-$(id -un)}" +} + +is_login_uid() { + (($1 >= 1000 && $1 < 60000)) +} + +in_group() { + [[ " $(id -nG "$1") " == *" $2 "* ]] +} + +# Resolve only login names, not numeric UIDs accepted by id/getent. Passing a +# UID on to gpasswd/usermod/userdel does not identify a login name. +require_login_user() { + local entry name uid + entry=$(getent passwd "$1") || fail "no such user: $1" + IFS=: read -r name _ uid _ <<<"$entry" + [[ $name == "$1" ]] || fail "use the login name, not a UID: $1" + is_login_uid "$uid" || fail "refusing to manage system account '$name' (UID $uid)" +} + +full_sudo_grant() { + local list + list=$(LC_ALL=C sudo -l -U "$1" 2>/dev/null) || return 1 + grep -Eq '^[[:space:]]*\((ALL|root)([[:space:]]*:[^)]*)?\)[[:space:]]+([A-Z]+:[[:space:]]*)*ALL[[:space:]]*$' <<<"$list" +} + +# Enumerating passwd and asking id includes primary wheel members, which are +# absent from getent group's supplementary-member list. +other_admin_exists() { + local excluded_uid=$1 name uid + while IFS=: read -r name _ uid _; do + is_login_uid "$uid" || continue + ((uid != excluded_uid)) || continue + if in_group "$name" wheel || full_sudo_grant "$name"; then + return 0 + fi + done < <(getent passwd) + return 1 +} + +guard_last_admin() { + local username=$1 uid + uid=$(id -u "$username") + if in_group "$username" wheel || full_sudo_grant "$username"; then + other_admin_exists "$uid" || fail "refusing to remove the last administrator; no other login user has wheel membership or unrestricted sudo" + fi +} + +guard_wheel_removal() { + local username=$1 uid + + uid=$(id -u "$username") + is_login_uid "$uid" || fail "refusing to change wheel for system account '$username' (UID $uid)" + if ((uid == $(caller_uid))); then + fail "refusing to remove wheel from your own account" + fi + + other_admin_exists "$uid" || fail "refusing to remove the last administrator; no other login user has wheel membership or unrestricted sudo" +} + +warn_wheel_admin() { + echo "wheel grants administrator access through sudo and polkit (including pkexec/run0)." +} + +# Removing wheel cannot revoke an independent sudoers rule. A failed query is +# inconclusive unless sudo explicitly says the account has no grant. +report_sudo_access() { + local username=$1 output + if output=$(LC_ALL=C sudo -l -U "$username" 2>&1); then + echo "WARNING: $username still has effective sudo privileges. Review /etc/sudoers and /etc/sudoers.d with sudo visudo." >&2 + elif [[ $output == *"is not allowed to run sudo"* || $output == *"may not run sudo"* ]]; then + echo "No sudo access remains for $username." + else + echo "Could not verify remaining sudo access for $username: $output" >&2 + echo "Run 'sudo -l -U $username' from a root or another administrator session." >&2 + fi +} diff --git a/bin/omarchy-user-groups b/bin/omarchy-user-groups new file mode 100755 index 000000000000..9149080340d0 --- /dev/null +++ b/bin/omarchy-user-groups @@ -0,0 +1,241 @@ +#!/bin/bash + +# omarchy:summary=Change a user's supplementary groups (pick, add, remove, or set) +# omarchy:args=[username] [--add g1,g2] [--remove g1,g2] [--set g1,g2] [--yes] +# omarchy:examples=omarchy user groups | omarchy user groups alice --add wheel --yes +# omarchy:requires-sudo=true + +set -euo pipefail + +source "${BASH_SOURCE[0]%/*}/omarchy-user-functions" || exit 1 + +PROG="omarchy user groups" +ASSUME_YES=0 +USERNAME="" +ADD_ARG="" +REMOVE_ARG="" +SET_ARG="" +SET_GIVEN=0 + +fail() { + echo "$PROG: $*" >&2 + exit 1 +} + +interactive() { + [[ -t 0 && -t 1 ]] +} + +show_logo() { + if interactive && [[ -n ${OMARCHY_PATH:-} && -f $OMARCHY_PATH/logo.txt ]]; then + echo -e "\033[32m" + cat <"$OMARCHY_PATH/logo.txt" + echo -e "\033[0m" + echo + fi +} + +confirm() { + local prompt="$1" + if ((ASSUME_YES)); then + return 0 + fi + if interactive; then + gum confirm "$prompt" + else + fail "refusing to continue without confirmation; pass --yes" + fi +} + +need_gum() { + omarchy-cmd-present gum || fail "gum is required for the interactive menu" +} + +login_users() { + getent passwd | awk -F: '$3 >= 1000 && $3 < 60000 { print $1 }' | sort +} + +CURATED_GROUPS="wheel video audio optical storage input scanner network power uucp" + +existing_groups() { + local g out="" + for g in $CURATED_GROUPS; do + if getent group "$g" >/dev/null; then + out+="$g"$'\n' + fi + done + printf '%s' "$out" +} + +# Primary group never changes here; only supplementary groups are managed. +primary_group() { + id -gn "$1" +} + +supplementary_groups() { + local primary current g out="" + primary=$(primary_group "$1") + current=$(id -nG "$1") + for g in $current; do + if [[ $g != "$primary" ]]; then + out+="$g " + fi + done + printf '%s' "${out% }" +} + +check_groups_exist() { + local csv="$1" g + IFS=',' read -ra arr <<<"$csv" + for g in "${arr[@]}"; do + [[ -n $g ]] || fail "empty group name in '$csv'" + getent group "$g" >/dev/null || fail "unknown group: $g" + done +} + +while (($# > 0)); do + case "$1" in + --add) + if [[ -z ${2:-} || $2 == -* ]]; then + fail "--add needs a comma-separated list" + fi + if [[ -n $ADD_ARG ]] || ((SET_GIVEN)); then + fail "combine --add/--remove, or use --set alone" + fi + SET_ARG="" + ADD_ARG="$2" + shift 2 + ;; + --remove) + if [[ -z ${2:-} || $2 == -* ]]; then + fail "--remove needs a comma-separated list" + fi + if [[ -n $REMOVE_ARG ]] || ((SET_GIVEN)); then + fail "combine --add/--remove, or use --set alone" + fi + SET_ARG="" + REMOVE_ARG="$2" + shift 2 + ;; + --set) + if (($# < 2)) || [[ $2 == -* ]]; then + fail "--set needs a comma-separated list" + fi + if [[ -n $ADD_ARG || -n $REMOVE_ARG ]] || ((SET_GIVEN)); then + fail "combine --add/--remove, or use --set alone" + fi + SET_ARG="$2" + SET_GIVEN=1 + ADD_ARG="" + REMOVE_ARG="" + shift 2 + ;; + --yes | -y) + ASSUME_YES=1 + shift + ;; + -h | --help) + echo "Usage: $PROG [username] [--add g1,g2] [--remove g1,g2] [--set g1,g2] [--yes]" + echo + echo "Changes a user's supplementary groups. The primary group is never" + echo "touched. --add/--remove adjust the current set, --set replaces it." + echo "Use --set '' to clear all supplementary groups. wheel grants admin access." + echo + echo "With no group flags it shows a gum checklist preselected with the" + echo "user's current groups; the checked set becomes the new set." + exit 0 + ;; + -*) + fail "unknown option: $1 (try --help)" + ;; + *) + if [[ -n $USERNAME ]]; then + fail "unexpected argument: $1" + fi + USERNAME="$1" + shift + ;; + esac +done + +show_logo + +if [[ -z $USERNAME ]]; then + interactive || fail "a username is required" + need_gum + USERNAME=$(login_users | gum choose --header="Change groups for which user?") || fail "cancelled" + [[ -n $USERNAME ]] || fail "nothing selected" +fi +require_login_user "$USERNAME" + +current=$(supplementary_groups "$USERNAME") +final="" + +if ((SET_GIVEN)); then + if [[ -n $SET_ARG ]]; then + check_groups_exist "$SET_ARG" + fi + final="$SET_ARG" +elif [[ -n $ADD_ARG || -n $REMOVE_ARG ]]; then + if [[ -n $ADD_ARG ]]; then + check_groups_exist "$ADD_ARG" + fi + if [[ -n $REMOVE_ARG ]]; then + check_groups_exist "$REMOVE_ARG" + fi + # Union of current + added, minus removed. + declare -A keep=() + g="" + for g in $current; do keep[$g]=1; done + IFS=',' read -ra arr <<<"${ADD_ARG:-}" + for g in ${arr[@]+"${arr[@]}"}; do + if [[ -n $g ]]; then + keep[$g]=1 + fi + done + IFS=',' read -ra arr <<<"${REMOVE_ARG:-}" + for g in ${arr[@]+"${arr[@]}"}; do + if [[ -n $g ]]; then + unset "keep[$g]" + fi + done + final=$(printf '%s,' "${!keep[@]}") + final="${final%,}" +else + interactive || fail "a group change is required (--add, --remove, or --set)" + need_gum + options=$(printf '%s\n%s' "$(existing_groups)" "$current" | tr ' ' '\n' | awk NF | awk '!seen[$0]++') + selected="" + if [[ -n $current ]]; then + want="--selected=$(printf '%s' "$current" | tr ' ' ',')" + selected=$(printf '%s' "$options" | gum choose --no-limit "$want" --header="Groups for $USERNAME? (x to toggle)") || fail "cancelled" + else + selected=$(printf '%s' "$options" | gum choose --no-limit --header="Groups for $USERNAME? (x to toggle)") || fail "cancelled" + fi + final=$(printf '%s' "$selected" | tr '\n' ',' | sed 's/,$//') +fi + +if [[ " $current " == *" wheel "* && ,$final, != *,wheel,* ]]; then + guard_wheel_removal "$USERNAME" +fi + +echo +echo "Group change for $USERNAME (primary: $(primary_group "$USERNAME") untouched):" +echo " before: ${current:-}" +echo " after: ${final:-}" +if [[ ,$final, == *,wheel,* ]]; then + warn_wheel_admin +fi +current_csv=$(printf '%s' "$current" | tr ' ' '\n' | awk NF | sort | tr '\n' ',') +final_csv=$(printf '%s' "$final" | tr ',' '\n' | awk NF | sort | tr '\n' ',') +if [[ $current_csv == "$final_csv" ]]; then + fail "no change - groups already match" +fi +confirm "Apply this group change?" || fail "aborted" + +# -G replaces the supplementary set; the primary group is unaffected. +# An empty set clears supplementary groups. +sudo usermod -G "$final" -- "$USERNAME" + +echo "Groups for $USERNAME now: $(supplementary_groups "$USERNAME" | tr ' ' ',' | sed 's/^$//')" +echo "Takes effect on next login." diff --git a/bin/omarchy-user-privileges b/bin/omarchy-user-privileges new file mode 100755 index 000000000000..9aceac9fc6f7 --- /dev/null +++ b/bin/omarchy-user-privileges @@ -0,0 +1,146 @@ +#!/bin/bash + +# omarchy:summary=Set a user's sudo level: password sudo or none (via wheel group) +# omarchy:args=[username] [--level password|none] [--yes] +# omarchy:examples=omarchy user privileges | omarchy user privileges alice --level password --yes +# omarchy:requires-sudo=true + +set -euo pipefail + +source "${BASH_SOURCE[0]%/*}/omarchy-user-functions" || exit 1 + +PROG="omarchy user privileges" +ASSUME_YES=0 +USERNAME="" +LEVEL="" + +fail() { + echo "$PROG: $*" >&2 + exit 1 +} + +interactive() { + [[ -t 0 && -t 1 ]] +} + +show_logo() { + if interactive && [[ -n ${OMARCHY_PATH:-} && -f $OMARCHY_PATH/logo.txt ]]; then + echo -e "\033[32m" + cat <"$OMARCHY_PATH/logo.txt" + echo -e "\033[0m" + echo + fi +} + +confirm() { + local prompt="$1" + if ((ASSUME_YES)); then + return 0 + fi + if interactive; then + gum confirm "$prompt" + else + fail "refusing to continue without confirmation; pass --yes" + fi +} + +need_gum() { + omarchy-cmd-present gum || fail "gum is required for the interactive menu" +} + +login_users() { + getent passwd | awk -F: '$3 >= 1000 && $3 < 60000 { print $1 }' | sort +} + +while (($# > 0)); do + case "$1" in + --level) + if [[ -z ${2:-} || $2 == -* ]]; then + fail "--level needs one of: password, none" + fi + LEVEL="$2" + shift 2 + ;; + --yes | -y) + ASSUME_YES=1 + shift + ;; + -h | --help) + echo "Usage: $PROG [username] [--level password|none] [--yes]" + echo + echo "Sets a user's sudo level via the wheel group: password sudo (add to wheel) or no sudo (remove from wheel)." + echo "'none' removes the user from the wheel group only when they are a member; anything else is left alone with a warning." + echo "wheel also grants polkit administrator access. Other sudo rules are left in place and checked afterward." + echo + echo "With no arguments it walks you through each step with gum." + exit 0 + ;; + -*) + fail "unknown option: $1 (try --help)" + ;; + *) + if [[ -n $USERNAME ]]; then + fail "unexpected argument: $1" + fi + USERNAME="$1" + shift + ;; + esac +done + +if [[ -n $LEVEL && $LEVEL != password && $LEVEL != none ]]; then + fail "--level must be one of: password, none" +fi + +show_logo + +if [[ -z $USERNAME ]]; then + interactive || fail "a username is required" + need_gum + USERNAME=$(login_users | gum choose --header="Set privileges for which user?") || fail "cancelled" + [[ -n $USERNAME ]] || fail "nothing selected" +fi +require_login_user "$USERNAME" + +if [[ -z $LEVEL ]]; then + interactive || fail "--level is required (password, none)" + need_gum + choice=$(gum choose --header="Sudo level for $USERNAME?" "Password sudo" "No sudo") || fail "cancelled" + if [[ $choice == "Password sudo" ]]; then + LEVEL="password" + else + LEVEL="none" + fi +fi + +if [[ $LEVEL == "none" ]] && in_group "$USERNAME" wheel; then + [[ $(id -gn "$USERNAME") != "wheel" ]] || fail "wheel is the primary group for $USERNAME; change the primary group before removing administrator access" + guard_wheel_removal "$USERNAME" +fi + +echo +echo "About to set:" +echo " user: $USERNAME" +echo " level: $LEVEL" +if [[ $LEVEL == "password" ]]; then + warn_wheel_admin +fi +confirm "Grant $LEVEL sudo to $USERNAME?" || fail "aborted" + +if [[ $LEVEL == "password" ]]; then + # Add user to wheel group for sudo + sudo gpasswd -a "$USERNAME" wheel + echo "Sudo (password) granted to $USERNAME via wheel group." +elif in_group "$USERNAME" wheel; then + # Remove user from wheel group + sudo gpasswd -d "$USERNAME" wheel + echo "Removed $USERNAME from wheel." +else + echo "$USERNAME is not in wheel; no wheel membership to remove." +fi + +if [[ $LEVEL == "none" ]]; then + report_sudo_access "$USERNAME" +fi + +echo "Change takes effect on next login." diff --git a/bin/omarchy-user-remove b/bin/omarchy-user-remove new file mode 100755 index 000000000000..843d11976ebd --- /dev/null +++ b/bin/omarchy-user-remove @@ -0,0 +1,158 @@ +#!/bin/bash + +# omarchy:summary=Remove a login user, optionally its home (sudo managed via wheel group) +# omarchy:args=[username] [--remove-home|--keep-home] [--yes] +# omarchy:examples=omarchy user remove | omarchy user remove alice --remove-home --yes +# omarchy:requires-sudo=true + +set -euo pipefail + +source "${BASH_SOURCE[0]%/*}/omarchy-user-functions" || exit 1 + +PROG="omarchy user remove" +ASSUME_YES=0 +USERNAME="" +HOME_MODE="" + +fail() { + echo "$PROG: $*" >&2 + exit 1 +} + +interactive() { + [[ -t 0 && -t 1 ]] +} + +show_logo() { + if interactive && [[ -n ${OMARCHY_PATH:-} && -f $OMARCHY_PATH/logo.txt ]]; then + echo -e "\033[32m" + cat <"$OMARCHY_PATH/logo.txt" + echo -e "\033[0m" + echo + fi +} + +confirm() { + local prompt="$1" + if ((ASSUME_YES)); then + return 0 + fi + if interactive; then + gum confirm "$prompt" + else + fail "refusing to continue without confirmation; pass --yes" + fi +} + +need_gum() { + omarchy-cmd-present gum || fail "gum is required for the interactive menu" +} + +# Login users: real accounts (UID 1000..59999), one per line. +login_users() { + getent passwd | awk -F: '$3 >= 1000 && $3 < 60000 { print $1 }' | sort +} + +while (($# > 0)); do + case "$1" in + --remove-home) + if [[ -n $HOME_MODE ]]; then + fail "pass only one of --remove-home, --keep-home" + fi + HOME_MODE="remove" + shift + ;; + --keep-home) + if [[ -n $HOME_MODE ]]; then + fail "pass only one of --remove-home, --keep-home" + fi + HOME_MODE="keep" + shift + ;; + --yes | -y) + ASSUME_YES=1 + shift + ;; + -h | --help) + echo "Usage: $PROG [username] [--remove-home|--keep-home] [--yes]" + echo + echo "Removes a login user. Never touches root, system accounts (UID < 1000)," + echo "or the account running this script. The user's sudo access (via wheel" + echo "group) is removed automatically when the user is removed." + echo + echo "With no arguments it lets you pick the user with gum." + exit 0 + ;; + -*) + fail "unknown option: $1 (try --help)" + ;; + *) + if [[ -n $USERNAME ]]; then + fail "unexpected argument: $1" + fi + USERNAME="$1" + shift + ;; + esac +done + +show_logo + +if [[ -z $USERNAME ]]; then + interactive || fail "a username is required" + need_gum + USERNAME=$(login_users | gum choose --header="Remove which user?") || fail "cancelled" + [[ -n $USERNAME ]] || fail "nothing selected" +fi + +entry=$(getent passwd "$USERNAME") || fail "no such user: $USERNAME" +IFS=: read -r _ _ uid _ _ home _ <<<"$entry" +if ((uid == 0)); then + fail "refusing to remove root" +fi +if ((uid < 1000 || uid >= 60000)); then + fail "refusing to remove system account '$USERNAME' (UID $uid)" +fi +if ((uid == $(caller_uid))); then + fail "refusing to remove your own account (UID $uid)" +fi +require_login_user "$USERNAME" +guard_last_admin "$USERNAME" + +if [[ -z $HOME_MODE ]]; then + if interactive; then + need_gum + if gum choose --header="Home directory $home?" "Keep home directory" "Remove home directory" | grep -q Remove; then + HOME_MODE="remove" + else + HOME_MODE="keep" + fi + else + HOME_MODE="keep" + fi +fi + +echo +echo "About to remove:" +echo " user: $USERNAME (UID $uid)" +echo " home: $home ($HOME_MODE)" +confirm "Remove user $USERNAME? This cannot be undone." || fail "aborted" + +# Log the user out first so userdel doesn't fail on a live session. +sudo loginctl terminate-user "$USERNAME" >/dev/null 2>&1 || true + +userdel_flags=() +if [[ $HOME_MODE == "remove" ]]; then + userdel_flags=(-r) +fi +if ! userdel_output=$(sudo userdel "${userdel_flags[@]}" -- "$USERNAME" 2>&1); then + printf '%s\n' "$userdel_output" >&2 + fail "could not remove $USERNAME" +fi +# userdel warns when the mail spool was never created. That is not a failure. +printf '%s\n' "$userdel_output" | grep -vF 'mail spool' | awk NF >&2 || true + +# Note: The user's sudo access (via wheel group) is removed automatically by userdel. + +id "$USERNAME" &>/dev/null && fail "user '$USERNAME' still exists" +echo "Removed $USERNAME." diff --git a/manual/49-managing-users.md b/manual/49-managing-users.md new file mode 100644 index 000000000000..17279c79ef89 --- /dev/null +++ b/manual/49-managing-users.md @@ -0,0 +1,60 @@ +# Managing Additional Users + +Omarchy sets up one owner account on first boot. When you need a second login on the same machine — a family member, a handoff, a separate work account — manage it with the `user` command group. Every command works interactively (gum prompts) or scripted with explicit flags plus `--yes`. + +## Create a user + +```bash +omarchy user add +omarchy user add alice --groups '' --yes +``` + +This creates the account, puts it in the given groups (default: none), and sets its password. Sudo is managed separately via the `omarchy user privileges` command. The greeter needs a password, so if you pass `--skip-password`, set one later with `sudo passwd `. The first graphical login provisions the desktop automatically. + +A plain user needs no supplementary groups. `--groups ''` explicitly selects none, including in a terminal. Add only trusted administrators to `wheel`: Omarchy grants wheel members password sudo, and polkit also treats them as administrators, allowing root access through `pkexec` or `run0` with their own password. `omarchy user add alice --groups wheel --yes` therefore creates an administrator. + +If `/etc/sudoers.d/alice` already exists, creation is refused to avoid inheriting an old sudo grant. Inspect it with `sudo visudo -f /etc/sudoers.d/alice` and resolve it before reusing the name. This check covers the per-user path used by the older standalone tools; independently configured grants elsewhere in sudoers need separate review. + +## Change groups + +```bash +omarchy user groups +omarchy user groups alice --add wheel --yes +``` + +Adjusts supplementary groups (`--add`/`--remove` combine, `--set` replaces). The primary group is never touched, and changes take effect on next login. With no flags it shows a gum checklist preselected with the user's current groups. + +Use `omarchy user groups alice --set '' --yes` to clear supplementary groups. Removing your own `wheel` membership or removing it from the last administrator is refused, including with `--yes`. Another login user counts as an administrator through primary or supplementary `wheel` membership, or an unrestricted sudo grant; a command-specific sudo grant does not count. Sign in as another administrator before changing your own access. + +## Set sudo level + +```bash +omarchy user privileges +omarchy user privileges alice --level password --yes +``` + +Sets administrator access via the wheel group: `password` adds the user to `wheel`, granting password sudo and polkit administrator access; `none` removes supplementary `wheel` membership. This is a permanent grant — different from the temporary Passwordless Sudo toggle under Setup > Security, which expires automatically (see Security). + +The same self and last-administrator protections apply here. If `wheel` is the primary group, change that group separately first; `gpasswd -d` cannot remove primary membership. Existing sessions retain their group access until logout. + +Removing `wheel` does not remove independent sudoers rules. After `--level none`, the command checks effective sudo access and reports whether a grant remains, no grant remains, or the check failed. For remaining grants, run `sudo -l -U alice` from an administrator session and review `/etc/sudoers` and `/etc/sudoers.d` with `sudo visudo`. A failed query does not prove that access was removed. + +## Remove a user + +```bash +omarchy user remove +omarchy user remove alice --remove-home --yes +``` + +Removes the account (never root, system accounts, your own, or the last administrator). Safety checks run before terminating the user's session. Logs the user out first, then optionally removes the home directory. Wheel membership is removed automatically with the account. Independent sudoers rules are left in place; review them before reusing the login name. + +All account-management commands require the login name rather than a numeric UID, and group/privilege changes refuse system accounts as well. + +## First login as a plain user + +A plain user can log in and gets a separate desktop configuration. System administration still requires an administrator. Omarchy may show an "Update system" notification that the plain user cannot act on; an administrator has to run the update. Polkit prompts for system actions expect an administrator's password, so the plain user's own password will not authorize those actions. + +## See also + +- Security: temporary Passwordless Sudo vs the permanent `user privileges` grant above. +- Passing on a machine you've already used: for a full handoff, Reset Computer wipes every account; the commands above are for adding or removing individual users. diff --git a/test/shell.d/user-admin-test.sh b/test/shell.d/user-admin-test.sh new file mode 100755 index 000000000000..efaa45839db8 --- /dev/null +++ b/test/shell.d/user-admin-test.sh @@ -0,0 +1,331 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +for script in omarchy-user-add omarchy-user-remove omarchy-user-groups omarchy-user-privileges omarchy-user-functions; do + bash -n "$ROOT/bin/$script" || fail "$script parses" +done +pass "omarchy user scripts parse" + +unset SUDO_USER +test_tmp=$(mktemp -d) +trap 'rm -rf "$test_tmp"' EXIT + +mock_bin="$test_tmp/bin" +mkdir -p "$mock_bin" +export FAKE_LOG="$test_tmp/sudo.log" +export FAKE_STATE="$test_tmp/state" +mkdir -p "$FAKE_STATE" + +# Stubs read the fixture: USERS as name:uid:primary:supplementary-groups and +# FAKE_GROUPS as name:members. sudo only records what the script would have run. +cat >"$mock_bin/id" <<'STUB' +#!/bin/bash +source "$FAKE_DB" +flag="" +if [[ ${1:-} == -* ]]; then + flag=$1 + shift +fi +name=${1:-$FAKE_CURRENT} +rec="" +for entry in "${USERS[@]}"; do + [[ ${entry%%:*} == "$name" ]] && rec=$entry +done +[[ ! -e $FAKE_STATE/deleted-$name ]] || exit 1 +[[ -n $rec ]] || { echo "id: '$name': no such user" >&2; exit 1; } +IFS=: read -r _ uid primary groups <<<"$rec" +case $flag in + -u) echo "$uid" ;; + -un) echo "$name" ;; + -gn) echo "$primary" ;; + -nG) echo "$primary $groups" ;; + "") echo "uid=$uid($name)" ;; +esac +STUB + +cat >"$mock_bin/getent" <<'STUB' +#!/bin/bash +source "$FAKE_DB" +case ${1:-} in + passwd) + found=0 + for entry in "${USERS[@]}"; do + IFS=: read -r name uid _ _ <<<"$entry" + if [[ -z ${2:-} || $2 == "$name" || $2 == "$uid" ]]; then + echo "$name:x:$uid:$uid::/home/$name:/bin/bash" + found=1 + fi + done + ((found)) || exit 2 + exit 0 + ;; + group) + for entry in "${FAKE_GROUPS[@]}"; do + IFS=: read -r name members <<<"$entry" + [[ $name == "${2:-}" ]] && { echo "$name:x:1:$members"; exit 0; } + done + exit 2 + ;; +esac +exit 2 +STUB + +cat >"$mock_bin/sudo" <<'STUB' +#!/bin/bash +source "$FAKE_DB" +case $1 in + test) + # All sudoers reads stay inside the fixture, including dangling symlinks. + /usr/bin/test "$2" "$FAKE_STATE/${3##*/}" + ;; + -l) + if [[ ${FAKE_SUDO_ERROR:-0} == 1 ]]; then + echo 'sudo: unable to read policy' >&2 + exit 1 + elif [[ ${FAKE_FULL_SUDO:-} == "$3" ]]; then + printf 'User %s may run the following commands on host:\n (ALL : ALL) ALL\n' "$3" + elif [[ ${FAKE_RESTRICTED_SUDO:-} == "$3" ]]; then + printf 'User %s may run the following commands on host:\n (root) /usr/bin/true\n' "$3" + else + echo "User $3 is not allowed to run sudo on host." + exit 1 + fi + ;; + userdel) + echo "$*" >>"$FAKE_LOG" + touch "$FAKE_STATE/deleted-${*: -1}" + ;; + *) echo "$*" >>"$FAKE_LOG" ;; +esac +STUB +chmod +x "$mock_bin"/* + +fixture_admins="$test_tmp/admins.sh" +cat >"$fixture_admins" <<'FIXTURE' +USERS=("alice:1000:alice:wheel video" "bob:1001:bob:wheel" "carol:1002:carol:wheel-helper") +FAKE_GROUPS=("wheel:alice,bob" "video:alice" "audio:" "input:" "wheel-helper:carol") +FAKE_CURRENT=alice +FIXTURE + +fixture_one_admin="$test_tmp/one-admin.sh" +cat >"$fixture_one_admin" <<'FIXTURE' +USERS=("alice:1000:alice:video" "bob:1001:bob:wheel") +FAKE_GROUPS=("wheel:bob" "video:alice" "audio:") +FAKE_CURRENT=alice +FIXTURE + +# Runs a user script as the given caller fixture, stdin closed so it never +# goes interactive. Prints combined output and returns the script's status. +run_as() { + local fixture=$1 + shift + : >"$FAKE_LOG" + rm -f -- "$FAKE_STATE"/deleted-* + PATH="$mock_bin:$PATH" FAKE_DB="$fixture" bash "$ROOT/bin/$1" "${@:2}" &1 +} + +expect_refused() { + local description=$1 fixture=$2 expected=$3 out status=0 + shift 3 + out=$(run_as "$fixture" "$@") || status=$? + [[ $status -ne 0 && $out == *"$expected"* && ! -s $FAKE_LOG ]] || + fail "$description" "status=$status output: $out sudo: $(cat "$FAKE_LOG")" + pass "$description" +} + +expect_applied() { + local description=$1 fixture=$2 expected=$3 out status=0 + shift 3 + out=$(run_as "$fixture" "$@") || status=$? + [[ $status -eq 0 ]] && grep -qF -- "$expected" "$FAKE_LOG" || + fail "$description" "status=$status output: $out sudo: $(cat "$FAKE_LOG")" + pass "$description" +} + +expect_refused "groups refuses to drop wheel from the caller" "$fixture_admins" \ + "refusing to remove wheel from your own account" omarchy-user-groups alice --remove wheel --yes + +expect_applied "groups drops wheel from another admin when another login admin remains" "$fixture_admins" \ + "usermod -G video -- bob" omarchy-user-groups bob --add video --remove wheel --yes + +expect_refused "groups refuses to drop wheel from the last login admin" "$fixture_one_admin" \ + "refusing to remove the last administrator" omarchy-user-groups bob --remove wheel --yes + +expect_applied "groups leaves similarly named groups alone" "$fixture_admins" \ + "usermod -G video,wheel-helper -- carol" omarchy-user-groups carol --set video,wheel-helper --yes + +expect_applied "groups allows unrelated edits to the caller's own account" "$fixture_one_admin" \ + "usermod -G video,audio -- alice" omarchy-user-groups alice --set video,audio --yes + +expect_refused "privileges refuses to remove the caller's own sudo" "$fixture_admins" \ + "refusing to remove wheel from your own account" omarchy-user-privileges alice --level none --yes + +expect_refused "privileges refuses to remove the last login admin's sudo" "$fixture_one_admin" \ + "refusing to remove the last administrator" omarchy-user-privileges bob --level none --yes + +expect_applied "privileges removes sudo from another admin" "$fixture_admins" \ + "gpasswd -d bob wheel" omarchy-user-privileges bob --level none --yes + +expect_applied "privileges grants password sudo" "$fixture_one_admin" \ + "gpasswd -a alice wheel" omarchy-user-privileges alice --level password --yes + +expect_applied "user add creates an account with no groups by default" "$fixture_admins" \ + "useradd -m -s" omarchy-user-add dave --skip-password --yes + +if [[ $(cat "$FAKE_LOG") == *usermod* ]]; then + fail "user add skips usermod when no groups are given" "$(cat "$FAKE_LOG")" +fi +pass "user add skips usermod when no groups are given" + +expect_applied "user add puts the new account in the requested groups" "$fixture_admins" \ + "usermod -aG video dave" omarchy-user-add dave --groups video --skip-password --yes + +expect_refused "user remove refuses the caller's own account" "$fixture_admins" \ + "refusing to remove your own account" omarchy-user-remove alice --keep-home --yes + +expect_refused "user remove refuses to continue without confirmation" "$fixture_admins" \ + "refusing to continue without confirmation" omarchy-user-remove bob --keep-home + +# Safety-check coverage: system accounts and self-removal by numeric UID are +# refused before any privileged command runs. +fixture_remove_safety="$test_tmp/remove-safety.sh" +cat >"$fixture_remove_safety" <<'FIXTURE' +USERS=("alice:1000:alice:wheel video" "sysacct:500:sysacct:") +FAKE_GROUPS=("wheel:alice" "video:alice" "audio:") +FAKE_CURRENT=alice +FIXTURE + +expect_refused "user remove refuses system accounts" "$fixture_remove_safety" \ + "system account" omarchy-user-remove sysacct --keep-home --yes + +expect_refused "user remove blocks removing yourself by numeric UID" "$fixture_remove_safety" \ + "your own account" omarchy-user-remove 1000 --keep-home --yes + + +fixture_primary="$test_tmp/primary-wheel.sh" +cat >"$fixture_primary" <<'FIXTURE' +USERS=("alice:1000:alice:video" "bob:1001:bob:wheel" "carol:1002:wheel:video") +FAKE_GROUPS=("wheel:bob" "video:alice,carol" "audio:") +FAKE_CURRENT=alice +FIXTURE + +expect_applied "primary wheel member counts as another administrator" "$fixture_primary" \ + "gpasswd -d bob wheel" omarchy-user-privileges bob --level none --yes + +expect_applied "unrelated supplementary edit leaves primary wheel untouched" "$fixture_primary" \ + "usermod -G video,audio -- carol" omarchy-user-groups carol --set video,audio --yes + +expect_refused "privileges cannot remove primary wheel with gpasswd" "$fixture_primary" \ + "wheel is the primary group" omarchy-user-privileges carol --level none --yes + +expect_refused "removing the last administrator is blocked before session termination" "$fixture_one_admin" \ + "refusing to remove the last administrator" omarchy-user-remove bob --keep-home --yes + +expect_applied "removing an administrator succeeds when primary wheel admin remains" "$fixture_primary" \ + "userdel -- bob" omarchy-user-remove bob --keep-home --yes + +expect_refused "groups rejects numeric UIDs" "$fixture_admins" \ + "use the login name, not a UID" omarchy-user-groups 1001 --add video --yes + +expect_refused "privileges rejects numeric UIDs" "$fixture_admins" \ + "use the login name, not a UID" omarchy-user-privileges 1001 --level password --yes + +expect_refused "remove rejects another account's numeric UID before terminating it" "$fixture_admins" \ + "use the login name, not a UID" omarchy-user-remove 1001 --keep-home --yes + +expect_refused "groups refuses system accounts" "$fixture_remove_safety" \ + "system account" omarchy-user-groups sysacct --add video --yes + +expect_refused "privileges refuses system accounts when granting sudo" "$fixture_remove_safety" \ + "system account" omarchy-user-privileges sysacct --level password --yes + +expect_refused "privileges refuses system accounts when removing sudo" "$fixture_remove_safety" \ + "system account" omarchy-user-privileges sysacct --level none --yes + +expect_applied "explicit empty groups creates a plain user" "$fixture_admins" \ + "useradd -m -s" omarchy-user-add dave --groups '' --skip-password --yes +[[ $(cat "$FAKE_LOG") != *usermod* ]] || fail "empty groups do not add supplementary membership" +pass "empty groups do not add supplementary membership" + +expect_applied "explicit empty set clears supplementary groups" "$fixture_one_admin" \ + "usermod -G -- alice" omarchy-user-groups alice --set '' --yes + +expect_refused "empty set cannot be combined with add" "$fixture_one_admin" \ + "combine --add/--remove" omarchy-user-groups alice --set '' --add audio --yes + +expect_refused "empty set still protects the caller's wheel membership" "$fixture_admins" \ + "refusing to remove wheel from your own account" omarchy-user-groups alice --set '' --yes + +printf 'dave ALL=(ALL) ALL\n' >"$FAKE_STATE/dave" +expect_refused "stale sudoers file blocks plain account creation" "$fixture_admins" \ + "/etc/sudoers.d/dave already exists" omarchy-user-add dave --skip-password --yes +rm -- "$FAKE_STATE/dave" +ln -s "$FAKE_STATE/missing" "$FAKE_STATE/dave" +expect_refused "dangling sudoers symlink blocks account creation" "$fixture_admins" \ + "/etc/sudoers.d/dave already exists" omarchy-user-add dave --skip-password --yes +rm -- "$FAKE_STATE/dave" + +fixture_sudo_admin="$test_tmp/sudo-admin.sh" +cat >"$fixture_sudo_admin" <<'FIXTURE' +USERS=("alice:1000:alice:" "bob:1001:bob:wheel") +FAKE_GROUPS=("wheel:bob") +FAKE_CURRENT=alice +FAKE_FULL_SUDO=alice +FIXTURE +expect_applied "unrestricted sudo counts as an alternative administrator" "$fixture_sudo_admin" \ + "gpasswd -d bob wheel" omarchy-user-privileges bob --level none --yes +expect_applied "account removal accepts an alternative unrestricted sudo admin" "$fixture_sudo_admin" \ + "userdel -- bob" omarchy-user-remove bob --keep-home --yes + +fixture_only_sudo="$test_tmp/only-sudo.sh" +cat >"$fixture_only_sudo" <<'FIXTURE' +USERS=("alice:1000:alice:" "bob:1001:bob:") +FAKE_GROUPS=("wheel:") +FAKE_CURRENT=alice +FAKE_FULL_SUDO=bob +FIXTURE +expect_refused "account removal protects the last administrator with only sudo" "$fixture_only_sudo" \ + "refusing to remove the last administrator" omarchy-user-remove bob --keep-home --yes + +fixture_restricted="$test_tmp/restricted-sudo.sh" +cat >"$fixture_restricted" <<'FIXTURE' +USERS=("alice:1000:alice:" "bob:1001:bob:wheel") +FAKE_GROUPS=("wheel:bob") +FAKE_CURRENT=alice +FAKE_RESTRICTED_SUDO=alice +FIXTURE +expect_refused "command-limited sudo does not count as another administrator" "$fixture_restricted" \ + "refusing to remove the last administrator" omarchy-user-privileges bob --level none --yes + +check_report() { + local description=$1 fixture=$2 expected=$3 out + out=$(run_as "$fixture" omarchy-user-privileges bob --level none --yes) || fail "$description" "$out" + [[ $out == *"$expected"* ]] || fail "$description" "$out" + if [[ $expected != "No sudo access remains" && $out == *"No sudo access remains"* ]]; then + fail "$description must not confirm revocation" "$out" + fi + pass "$description" +} + +fixture_external="$test_tmp/external-sudo.sh" +cat >"$fixture_external" <<'FIXTURE' +USERS=("alice:1000:alice:wheel" "bob:1001:bob:wheel") +FAKE_GROUPS=("wheel:alice,bob") +FAKE_CURRENT=alice +FAKE_FULL_SUDO=bob +FIXTURE +check_report "removing wheel warns about an independent sudo grant" "$fixture_external" "still has effective sudo privileges" +check_report "none also checks sudo when the account is already outside wheel" "$fixture_only_sudo" "still has effective sudo privileges" +check_report "an explicit policy denial confirms no sudo remains" "$fixture_admins" "No sudo access remains" + +fixture_error="$test_tmp/sudo-error.sh" +cat >"$fixture_error" <<'FIXTURE' +USERS=("alice:1000:alice:wheel" "bob:1001:bob:wheel") +FAKE_GROUPS=("wheel:alice,bob") +FAKE_CURRENT=alice +FAKE_SUDO_ERROR=1 +FIXTURE +check_report "a failed sudo query reports uncertainty" "$fixture_error" "Could not verify remaining sudo access"