Repository navigation
user: add/remove users, groups, sudo level + managing-users manual #14366
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
Open
nightdevil00
wants to merge
7
commits into
omacom:quattro
Choose a base branch
from
nightdevil00:user-admin-scripts
base: quattro
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2952f0d
user: add/remove users, groups, sudo level + managing-users manual
nightdevil00 c2b659a
Fix user-admin-scripts PR: manage sudo via wheel group, fix self-prot…
a5f4802
user: address review on user-admin scripts
nightdevil00 3841b41
test: cover system-account and numeric-UID self-removal refusal
nightdevil00 48f8f9b
user: hide harmless userdel mail-spool warning; fix group toggle hint
nightdevil00 9f49b2a
user: preserve account safety in wheel-based administration
EFrMG c36c928
Merge pull request #3 from EFrMG/port-user-admin-safeguards
nightdevil00 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| #!/bin/bash | ||
|
|
||
| # omarchy:summary=Create a login user with groups and password (sudo managed via 'omarchy user privileges') | ||
| # omarchy:args=[username] [--groups g1,g2] [--yes] | ||
| # omarchy:examples=omarchy user add | omarchy user add alice --groups wheel --yes | ||
| # omarchy:requires-sudo=true | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| PROG="omarchy user add" | ||
| ASSUME_YES=0 | ||
| SKIP_PASSWORD=0 | ||
| USERNAME="" | ||
| GROUPS_ARG="" | ||
|
|
||
| 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 [[ -z ${2:-} || $2 == -* ]]; then | ||
| fail "--groups needs a comma-separated list" | ||
| fi | ||
| GROUPS_ARG="$2" | ||
| 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 | ||
| 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)" | ||
|
|
||
| if [[ -z $GROUPS_ARG ]]; then | ||
| if interactive; then | ||
| need_gum | ||
| GROUPS_ARG=$(existing_groups | gum choose --no-limit --header="Groups for $USERNAME? (space to toggle)") || fail "cancelled" | ||
| GROUPS_ARG=$(printf '%s' "$GROUPS_ARG" | tr '\n' ',' | sed 's/,$//') | ||
| else | ||
| GROUPS_ARG="" | ||
|
greptile-apps[bot] marked this conversation as resolved.
Resolved
|
||
| 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:-<none>}" | ||
| 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)." | ||
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,45 @@ | ||
| #!/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 "* ]] | ||
| } | ||
|
|
||
| # Refuses to drop wheel from the account running the command, from a system | ||
| # account, or from the last login user in wheel. | ||
| guard_wheel_removal() { | ||
| local username=$1 member uid count=0 | ||
|
|
||
| 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 | ||
|
|
||
| for member in $(getent group wheel | cut -d: -f4 | tr ',' ' '); do | ||
| uid=$(id -u "$member" 2>/dev/null) || continue | ||
| if is_login_uid "$uid"; then | ||
| ((++count)) | ||
| fi | ||
| done | ||
| if ((count <= 1)); then | ||
| fail "refusing to remove wheel: would leave the wheel group with no login users" | ||
| fi | ||
| } |
Oops, something went wrong.
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.