Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions bin/omarchy-sudo-passwordless
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/bin/bash -p

# omarchy:summary=Toggle passwordless sudo for the current user.
# omarchy:summary=Toggle passwordless sudo for the current user, asking how long when turning it on.
# omarchy:args=[MINUTES|permanent|--active|--disable]
# omarchy:examples=omarchy sudo passwordless | omarchy sudo passwordless 30 | omarchy sudo passwordless permanent | omarchy sudo passwordless --disable
# omarchy:requires-sudo=true

if [[ $- != *p* && ${BASH_SOURCE[0]} == "$0" ]]; then
Expand Down Expand Up @@ -33,8 +34,17 @@ readonly INSTALLED_SELF=/usr/bin/omarchy-sudo-passwordless
readonly STATUS_INACTIVE=3

usage() {
echo "Usage: omarchy-sudo-passwordless [MINUTES|permanent|--active|--disable]" >&2
echo "MINUTES must be between 1 and $MAX_MINUTES." >&2
echo "Usage: omarchy-sudo-passwordless [MINUTES|permanent|--active|--disable]"
echo " (no args) turn passwordless sudo off when it is on; otherwise ask how long"
echo " to allow it (15 minutes, 1 hour, 1 day, or permanently)"
echo " MINUTES allow it for 1-$MAX_MINUTES minutes, or change how long an active grant lasts"
echo " permanent allow it until disabled, surviving reboots and updates"
echo " --disable turn it off without asking"
echo " --active exit 0 while it is on and 1 otherwise, printing nothing"
}

invalid_usage() {
usage >&2
exit 1
}

Expand Down Expand Up @@ -498,6 +508,10 @@ show_enabled() {
}

case "${1:-}" in
-h|--help)
usage
exit 0
;;
__status|__enable|__disable|__expire|__cleanup-all|__package-removing|__migrate|__migration-complete)
action=$1
shift
Expand All @@ -511,7 +525,7 @@ esac
# listpw settings require the root-status fallback. Neither path prompts or
# trusts cached credentials as evidence of passwordless access.
if [[ ${1:-} == "--active" ]]; then
(($# == 1)) || usage
(($# == 1)) || invalid_usage
uid=$(/usr/bin/id -u)
if policy=$(/usr/bin/sudo -n -N -l -l -- "$INSTALLED_SELF" __status "$uid" 2>/dev/null); then
/usr/bin/grep -q '!authenticate' <<<"$policy"
Expand All @@ -527,9 +541,9 @@ if [[ ${1:-} == "--active" ]]; then
exit
fi

(($# <= 1)) || usage
(($# <= 1)) || invalid_usage
minutes=${1:-}
(($# == 0)) || [[ $minutes == "--disable" ]] || valid_duration "$minutes" || usage
(($# == 0)) || [[ $minutes == "--disable" ]] || valid_duration "$minutes" || invalid_usage
uid=$(/usr/bin/id -u)
valid_uid "$uid" || {
echo "omarchy-sudo-passwordless: cannot grant passwordless sudo to this account" >&2
Expand Down
7 changes: 7 additions & 0 deletions test/shell.d/nopasswd-sudo-expiry-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ done
assert_status 1 env TEST_POLICY_FAILURE=1 /usr/bin/bash -p "$test_tmp/omarchy-sudo-passwordless" --active
pass "menu probe reads policy tags without executing a privileged action or changing the timestamp"

: >"$test_tmp/commands"
/usr/bin/bash -p "$test_tmp/omarchy-sudo-passwordless" --help >"$test_tmp/public.log"
grep -q '^ (no args)' "$test_tmp/public.log" || fail "help must explain the bare invocation"
[[ ! -s $test_tmp/commands ]] || fail "help must not run sudo or gum"
assert_status 1 /usr/bin/bash -p "$test_tmp/omarchy-sudo-passwordless" 0 2>/dev/null
pass "help prints the modes and exits cleanly while invalid durations still fail"

for status in 0 3; do
: >"$test_tmp/commands"
TEST_STATUS=$status /usr/bin/bash -p "$test_tmp/omarchy-sudo-passwordless" --disable >"$test_tmp/public.log" 2>&1
Expand Down