phoebus_os/installer/lib/users.sh
Phoebus 38214ea9e7 chore: import Phoebus OS installer and Rust migration
Initial public import of the proven Bash installer (installer/) and the production Rust workspace (installer-rs/). The reference ISO, build output and local agent transcripts are gitignored.

Verified: installer/tests/run_tests.sh and cargo test --workspace both pass.
2026-09-20 02:07:33 -07:00

127 lines
4.3 KiB
Bash

#!/usr/bin/env bash
# shellcheck shell=bash
#
# User account configuration.
#
# Passwords are hashed immediately and held only as hashes. Plaintext is never
# written to disk, the config file or the log.
declare -g USER_PASSWORD="${USER_PASSWORD:-}"
declare -g ROOT_PASSWORD="${ROOT_PASSWORD:-}"
# users_hash PASSWORD - SHA-512 crypt hash (openssl present in the live image)
users_hash() {
local pw="$1" hash
if command -v openssl >/dev/null 2>&1; then
hash="$(printf '%s' "$pw" | openssl passwd -6 -stdin 2>/dev/null)"
fi
[[ -n "$hash" ]] || return 1
printf '%s' "$hash"
}
# users_prompt_passwords - collect and hash passwords (TUI or tty)
users_prompt_passwords() {
local user rootpw
if declare -F ui_passwordbox >/dev/null 2>&1; then
ui_passwordbox "User password" "Password for $(config_get USERNAME):" || return 1
user="$UI_RESULT"
ui_passwordbox "User password" "Repeat password:" || return 1
[[ "$user" == "$UI_RESULT" ]] || { log_error "user passwords did not match"; return 1; }
ui_passwordbox "Root password" "Password for root (leave blank to lock root):" || return 1
rootpw="$UI_RESULT"
else
read -r -s -p "User password: " user; printf '\n' >&2
read -r -s -p "Root password (blank to lock): " rootpw; printf '\n' >&2
fi
secret_register "$user"
secret_register "$rootpw"
USER_PASSWORD="$user"
ROOT_PASSWORD="$rootpw"
return 0
}
# users_create ROOT - create the primary account and lock root by default
# users_ensure_groups ROOT GROUPS
# Create missing supplementary groups and return the subset that now exists.
# A minimal stage3 does not define render/libinput/plugdev/power, and
# useradd -G fails outright if any listed group is absent.
users_ensure_groups() {
local root="$1" groups="$2" g out=""
local saved_ifs="$IFS"
IFS=,
for g in $groups; do
[[ -z "$g" ]] && continue
if ! grep -q "^$g:" "$root/etc/group" 2>/dev/null; then
portage_chroot "$root" groupadd -r "$g" || log_warn "could not create group $g"
fi
if grep -q "^$g:" "$root/etc/group" 2>/dev/null; then
out="${out:+$out,}$g"
else
log_warn "group $g not available; skipping"
fi
done
IFS="$saved_ifs"
printf '%s' "$out"
}
users_create() {
local root="$1" username fullname groups shell userhash roothash
username="$(config_get USERNAME)"
fullname="$(config_get USER_FULLNAME)"
shell="$(config_get ROOT_SHELL)"
[[ -n "$username" ]] || { log_error "no username configured"; return 1; }
groups="$(users_ensure_groups "$root" "$(config_get USER_GROUPS)")"
local -a uargs=(-m -U -s "$shell" -c "$fullname")
[[ -n "$groups" ]] && uargs+=(-G "$groups")
uargs+=("$username")
portage_chroot "$root" useradd "${uargs[@]}" || {
log_error "useradd failed"; return 1; }
userhash="$(users_hash "$USER_PASSWORD")"
if [[ -n "$userhash" ]]; then
portage_chroot_stdin "$root" "$username:$userhash" chpasswd -e || {
log_error "could not set password for $username"; return 1; }
log_info "password set for $username"
else
log_warn "no password set for $username (account locked)"
fi
if [[ -n "$ROOT_PASSWORD" ]]; then
roothash="$(users_hash "$ROOT_PASSWORD")"
portage_chroot_stdin "$root" "root:$roothash" chpasswd -e || {
log_warn "could not set root password"; }
log_info "root password set"
else
portage_chroot "$root" passwd -l root || true
log_info "root account locked (no root password supplied)"
fi
USER_PASSWORD="" ROOT_PASSWORD=""
}
# users_configure_sudo ROOT - wheel group may sudo, with a password
users_configure_sudo() {
local root="$1"
local dir="$root/etc/sudoers.d"
mkdir -p "$dir"
{
printf '# Members of the wheel group may run commands as root.\n'
printf '%%wheel ALL=(ALL:ALL) ALL\n'
} >"$dir/10-wheel"
chmod 0440 "$dir/10-wheel"
if [[ -x "$root/usr/sbin/visudo" || -x "$root/sbin/visudo" ]]; then
portage_chroot "$root" visudo -cf /etc/sudoers.d/10-wheel || {
log_error "invalid sudoers file"; return 1; }
fi
log_info "sudo configured for wheel"
}
# users_verify ROOT
users_verify() {
local root="$1" username
username="$(config_get USERNAME)"
grep -q "^$username:" "$root/etc/passwd" || {
log_error "user $username missing from target"; return 1; }
[[ -d "$root/home/$username" ]] || log_warn "home directory for $username not found"
return 0
}