diff --git a/bin/omarchy-sudo-passwordless b/bin/omarchy-sudo-passwordless index badf65ea477f..e5f60b838f9f 100755 --- a/bin/omarchy-sudo-passwordless +++ b/bin/omarchy-sudo-passwordless @@ -31,6 +31,16 @@ readonly MIGRATION_MARKER=/var/lib/omarchy/migrations/1788163635 readonly QUARANTINE_DIR=/var/lib/omarchy/sudoers-quarantine readonly INSTALLED_SELF=/usr/bin/omarchy-sudo-passwordless readonly STATUS_INACTIVE=3 +# The published grant carries this sudoers default above its rule. Under +# sudo's default verifypw=all, one password-requiring entry anywhere in the +# policy is enough to make `sudo -v` prompt, so a user who asked for +# passwordless sudo could not validate their credentials without a password. +# The default names the account that asked for the grant: an unqualified +# Defaults line would change the rule for every account on the host, and +# would displace what an administrator configured. +verifypw_default() { + printf 'Defaults:%s verifypw = any' "$1" +} usage() { echo "Usage: omarchy-sudo-passwordless [MINUTES|permanent|--active|--disable]" >&2 @@ -107,6 +117,25 @@ rule_file() { fi } +# The published policy carries the sudoers default above the rule. A grant +# published before that default existed carries the rule alone, and both forms +# are this command's own grant. The default names the account the grant +# belongs to, so a file whose default names another account is not this +# command's grant and is left alone. +strip_grant_default() { + local contents=$1 default_account rule_account + if [[ $contents == "Defaults:"*" verifypw = any"$'\n'* ]]; then + default_account=${contents%%$'\n'*} + default_account=${default_account#Defaults:} + default_account=${default_account%" verifypw = any"} + rule_account=${contents#*$'\n'} + rule_account=${rule_account%% *} + [[ $default_account == "$rule_account" ]] || return 1 + contents=${contents#*$'\n'} + fi + printf '%s' "$contents" +} + # The sudoers rule is the only grant record. A missing file is distinct from # an unreadable, unsafe, or administrator-modified file. read_grant() { @@ -116,6 +145,7 @@ read_grant() { if [[ -e $permanent || -L $permanent ]]; then verify_root_path "$permanent" && [[ -f $permanent ]] || return 2 contents=$(/usr/bin/cat -- "$permanent") || return 2 + contents=$(strip_grant_default "$contents") || return 2 [[ $contents =~ ^([a-z_][a-z0-9_-]*\$?)\ ALL=\(ALL\)\ NOPASSWD:\ ALL$ ]] || return 2 GRANT_NAME=${BASH_REMATCH[1]} GRANT_DEADLINE="" @@ -127,6 +157,7 @@ read_grant() { # both files, and the next duration change finishes their replacement. verify_root_path "$file" && [[ -f $file ]] || return 2 contents=$(/usr/bin/cat -- "$file") || return 2 + contents=$(strip_grant_default "$contents") || return 2 [[ $contents =~ ^([a-z_][a-z0-9_-]*\$?)\ ALL=\(ALL\)\ NOTAFTER=([0-9]{14}Z)\ NOPASSWD:\ ALL$ ]] || return 2 [[ ${BASH_REMATCH[1]} == "$GRANT_NAME" ]] || return 2 fi @@ -135,6 +166,7 @@ read_grant() { [[ -e $file || -L $file ]] || return "$STATUS_INACTIVE" verify_root_path "$file" && [[ -f $file ]] || return 2 contents=$(/usr/bin/cat -- "$file") || return 2 + contents=$(strip_grant_default "$contents") || return 2 [[ $contents =~ ^([a-z_][a-z0-9_-]*\$?)\ ALL=\(ALL\)\ NOTAFTER=([0-9]{14}Z)\ NOPASSWD:\ ALL$ ]] || return 2 GRANT_NAME=${BASH_REMATCH[1]} GRANT_DEADLINE=${BASH_REMATCH[2]} @@ -148,6 +180,7 @@ classify_generated_rule() { [[ -f $file && ! -L $file ]] || return 1 contents=$(/usr/bin/cat -- "$file") || return 2 + contents=$(strip_grant_default "$contents") || return 2 suffix=${file##*/99-omarchy-nopasswd-} if [[ $file == */99-omarchy-permanent-nopasswd-* ]]; then suffix=${file##*/99-omarchy-permanent-nopasswd-} @@ -372,12 +405,12 @@ enable_locked() ( omarchy_security_install_signal_exit_traps pending=$(/usr/bin/mktemp /etc/sudoers.d/.omarchy-nopasswd.XXXXXX) || return 1 if [[ $minutes == "permanent" ]]; then - /usr/bin/printf '%s ALL=(ALL) NOPASSWD: ALL\n' "$ACCOUNT_NAME" >"$pending" || return 1 + /usr/bin/printf '%s\n%s ALL=(ALL) NOPASSWD: ALL\n' "$(verifypw_default "$ACCOUNT_NAME")" "$ACCOUNT_NAME" >"$pending" || return 1 else now=$(/usr/bin/date +%s) || return 1 expires=$((now + 10#$minutes * 60)) deadline=$(/usr/bin/date -u -d "@$expires" +%Y%m%d%H%M%SZ) || return 1 - /usr/bin/printf '%s ALL=(ALL) NOTAFTER=%s NOPASSWD: ALL\n' "$ACCOUNT_NAME" "$deadline" >"$pending" || return 1 + /usr/bin/printf '%s\n%s ALL=(ALL) NOTAFTER=%s NOPASSWD: ALL\n' "$(verifypw_default "$ACCOUNT_NAME")" "$ACCOUNT_NAME" "$deadline" >"$pending" || return 1 fi /usr/bin/chown root:root "$pending" && /usr/bin/chmod 0440 "$pending" || return 1 /usr/sbin/visudo -cf "$pending" >/dev/null || return 1 @@ -408,6 +441,39 @@ enable_locked() ( fi ) +# Rewrite the grants this command published before the sudoers default +# existed, so a user who already asked for passwordless sudo gets the same +# credential validation as one who enables it now. A grant that already +# carries the default, and any policy this command did not write, are left +# alone: the first needs no change, and the second belongs to the +# administrator. +upgrade_grant_format_locked() { + local file contents account pending failed=0 + verify_root_path /etc/sudoers.d || return 1 + for file in /etc/sudoers.d/99-omarchy-nopasswd-* /etc/sudoers.d/99-omarchy-permanent-nopasswd-*; do + [[ -e $file ]] || continue + verify_root_path "$file" && [[ -f $file && ! -L $file ]] || { failed=1; continue; } + contents=$(/usr/bin/cat -- "$file") || { failed=1; continue; } + [[ $contents == "Defaults:"*" verifypw = any"$'\n'* ]] && continue + classify_generated_rule "$file" || continue + # The default names the account the grant belongs to, and the rule names + # it too. An account name the policy would not accept is left alone. + account=${contents%% *} + valid_account_name "$account" || continue + pending=$(/usr/bin/mktemp /etc/sudoers.d/.omarchy-nopasswd.XXXXXX) || { failed=1; continue; } + if ! /usr/bin/printf '%s\n%s\n' "$(verifypw_default "$account")" "$contents" >"$pending" || + ! /usr/bin/chown root:root "$pending" || + ! /usr/bin/chmod 0440 "$pending" || + ! /usr/sbin/visudo -cf "$pending" >/dev/null || + ! /usr/bin/mv -fT -- "$pending" "$file"; then + /usr/bin/rm -f -- "$pending" + failed=1 + continue + fi + done + (( failed == 0 )) +} + root_dispatch() { local action="$1" status shift @@ -450,6 +516,10 @@ root_dispatch() { (($# == 0)) && ((EUID == 0)) || return 1 with_root_lock migrate_locked ;; + __upgrade-grant-format) + (($# == 0)) && ((EUID == 0)) || return 1 + with_root_lock upgrade_grant_format_locked + ;; __cleanup-all) (($# == 0)) && ((EUID == 0)) || return 1 with_root_lock cleanup_all_locked @@ -498,7 +568,7 @@ show_enabled() { } case "${1:-}" in - __status|__enable|__disable|__expire|__cleanup-all|__package-removing|__migrate|__migration-complete) + __status|__enable|__disable|__expire|__cleanup-all|__package-removing|__migrate|__migration-complete|__upgrade-grant-format) action=$1 shift root_dispatch "$action" "$@" diff --git a/migrations/1792527479.sh b/migrations/1792527479.sh new file mode 100644 index 000000000000..5f464980c8de --- /dev/null +++ b/migrations/1792527479.sh @@ -0,0 +1,8 @@ +echo "Publish the sudoers default that lets \`sudo -v\` validate against the passwordless grant" + +# The grant file is the only record of the passwordless sudo policy, and it +# now carries the sudoers default that makes `sudo -v` validate against the +# grant instead of every other entry in the policy. A grant published before +# that default existed is rewritten in place, keeping its rule and deadline. +helper="$OMARCHY_PATH/bin/omarchy-sudo-passwordless" +sudo "$helper" __upgrade-grant-format diff --git a/test/shell.d/passwordless-sudo-verifypw-test.sh b/test/shell.d/passwordless-sudo-verifypw-test.sh new file mode 100755 index 000000000000..213e2f11585c --- /dev/null +++ b/test/shell.d/passwordless-sudo-verifypw-test.sh @@ -0,0 +1,111 @@ +#!/bin/bash + +set -euo pipefail +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" +source "$SHELL_TEST_DIR/fixtures/passwordless-sudo-test.sh" + +# Under sudo's default verifypw=all, `sudo -v` asks for a password unless every +# entry the user has is passwordless. The installer's own admin grant is +# password-requiring, so a user who asked for passwordless sudo could not +# validate credentials without a password. The published grant therefore +# carries the sudoers default that makes validation read the grant instead of +# every other entry in the policy. The default names the account that asked +# for the grant, so it changes the rule for that account alone. + +( + source "$library" + enable_locked 1000 15 + head -1 "$(rule_file 1000)" | grep -qx 'Defaults:audituser verifypw = any' + tail -1 "$(rule_file 1000)" | grep -qxE 'audituser ALL=\(ALL\) NOTAFTER=[0-9]{14}Z NOPASSWD: ALL' +) +pass "the published grant carries the sudoers default above its rule" + +( + source "$library" + enable_locked 1000 permanent + head -1 "$(rule_file 1000 permanent)" | grep -qx 'Defaults:audituser verifypw = any' + tail -1 "$(rule_file 1000 permanent)" | grep -qx 'audituser ALL=(ALL) NOPASSWD: ALL' +) +pass "a permanent grant carries the sudoers default above its rule" + +# The default is scoped to the account that asked for the grant, so it never +# changes the rule for another account on the host. +( + source "$library" + enable_locked 1000 15 + ! grep -q '^Defaults verifypw' "$(rule_file 1000)" +) +pass "the published default is scoped to the account that asked for the grant" + +# A grant published before the default existed is still this command's grant, +# so it keeps working until it is rewritten. +( + source "$library" + reset_grant + printf 'audituser ALL=(ALL) NOPASSWD: ALL\n' >"$(rule_file 1000 permanent)" + read_grant 1000 + [[ $GRANT_NAME == "audituser" ]] +) +pass "a grant published before the default existed is still read as the user's grant" + +# A default that names another account is not this command's grant. +( + source "$library" + reset_grant + printf 'Defaults:root verifypw = any\naudituser ALL=(ALL) NOPASSWD: ALL\n' >"$(rule_file 1000 permanent)" + assert_status 2 read_grant 1000 +) +pass "a default that names another account is not read as the user's grant" + +# The upgrade rewrites such a grant in place, keeping the rule and its +# deadline exactly as they were. +( + source "$library" + reset_grant + printf 'audituser ALL=(ALL) NOTAFTER=20261231235959Z NOPASSWD: ALL\n' >"$(rule_file 1000)" + upgrade_grant_format_locked + head -1 "$(rule_file 1000)" | grep -qx 'Defaults:audituser verifypw = any' + tail -1 "$(rule_file 1000)" | grep -qx 'audituser ALL=(ALL) NOTAFTER=20261231235959Z NOPASSWD: ALL' + [[ $(wc -l <"$(rule_file 1000)") == 2 ]] +) +pass "the upgrade rewrites a grant published before the default existed" + +( + source "$library" + reset_grant + printf 'audituser ALL=(ALL) NOPASSWD: ALL\n' >"$(rule_file 1000 permanent)" + upgrade_grant_format_locked + upgrade_grant_format_locked + [[ $(wc -l <"$(rule_file 1000 permanent)") == 2 ]] + head -1 "$(rule_file 1000 permanent)" | grep -qx 'Defaults:audituser verifypw = any' + tail -1 "$(rule_file 1000 permanent)" | grep -qx 'audituser ALL=(ALL) NOPASSWD: ALL' +) +pass "the upgrade leaves a grant that already carries the default alone" + +# Policy this command did not generate stays untouched: it belongs to the +# administrator, and rewriting it would publish a rule nobody asked for. +( + source "$library" + printf 'admin ALL=(ALL) NOPASSWD: /usr/bin/true\n' >"$test_tmp/etc/sudoers.d/99-omarchy-nopasswd-custom" + upgrade_grant_format_locked + [[ $(cat "$test_tmp/etc/sudoers.d/99-omarchy-nopasswd-custom") == 'admin ALL=(ALL) NOPASSWD: /usr/bin/true' ]] +) +pass "the upgrade leaves administrator policy under the generated prefix alone" + +# The migration reaches the upgrade through the command's own dispatch, so the +# action has to be reachable from the executable and not only from inside it. +( + PATH="$test_tmp/bin:$PATH" TEST_EUID=0 "$test_tmp/omarchy-sudo-passwordless" __upgrade-grant-format +) +pass "the upgrade action is reachable through the command's own dispatch" + +# The migration itself has to run: it reaches the upgrade through that +# dispatch, and a failure there would stop the queue and leave every later +# migration pending. +mkdir -p "$test_tmp/source/bin" +ln -sf "$test_tmp/omarchy-sudo-passwordless" "$test_tmp/source/bin/omarchy-sudo-passwordless" +( + PATH="$test_tmp/bin:$PATH" TEST_MIGRATION=1 OMARCHY_PATH="$test_tmp/source" \ + /usr/bin/bash "$ROOT/migrations/1792527479.sh" +) +pass "the migration runs the upgrade through the command's own dispatch"