Repository navigation
Switch DNS providers without a password prompt #7472
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,5 @@ | ||
| # Switching between the stock providers is a one-click toggle in the network | ||
| # panel, so it must not stop for a password. Custom is deliberately absent: it | ||
| # points the machine at servers the caller supplies, and it already runs in a | ||
| # terminal that can ask. | ||
| %wheel ALL=(root) NOPASSWD: /usr/bin/omarchy-dns Cloudflare, /usr/bin/omarchy-dns Google, /usr/bin/omarchy-dns DHCP |
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,110 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| source "$(dirname "$0")/base-test.sh" | ||
|
|
||
| dns="$ROOT/bin/omarchy-dns" | ||
| sudoers_file="$ROOT/etc/sudoers.d/omarchy-dns" | ||
| rule='%wheel ALL=(root) NOPASSWD: /usr/bin/omarchy-dns Cloudflare, /usr/bin/omarchy-dns Google, /usr/bin/omarchy-dns DHCP' | ||
|
|
||
| # Exactly one rule, matched whole. A second line -- or the same command with | ||
| # its arguments dropped, which sudoers reads as "any arguments" -- would widen | ||
| # the grant while leaving this line in place. | ||
| rules=$(grep -vE '^[[:space:]]*(#|$)' "$sudoers_file") | ||
| [[ $rules == "$rule" ]] || | ||
| fail "dns sudoers file carries exactly the stock-provider rule and nothing else" "got: $rules" | ||
|
|
||
| if command -v visudo >/dev/null; then | ||
| visudo -cf "$sudoers_file" >/dev/null || fail "dns sudoers rule parses" | ||
| fi | ||
|
|
||
| grep -Fx 'PACKAGED_PATH=/usr/bin/omarchy-dns' "$dns" >/dev/null || | ||
| fail "omarchy-dns compares against the path the sudoers rule names" | ||
|
|
||
| # sudo -l answers whether a command is permitted, not whether it is | ||
| # passwordless, and Omarchy ships a blanket %wheel rule that permits | ||
| # everything. A probe built on it sends Custom into `sudo -n`, which fails | ||
| # outright instead of falling through to pkexec. | ||
| ! grep -E '^[[:space:]]*[^#[:space:]].*sudo -n -l' "$dns" >/dev/null || | ||
| fail "omarchy-dns does not decide elevation with a sudo -l probe" | ||
|
|
||
| grep -Fx ' PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin' "$dns" >/dev/null || | ||
| fail "omarchy-dns pins a root-owned PATH once elevated, so a dev-linked checkout on sudo's secure_path cannot supply its helpers" | ||
|
|
||
| pass "dns sudoers rule is scoped to the stock providers" | ||
|
|
||
| # require_root returns immediately for root, so the stubs below would not stand | ||
| # between the script and the host's real NetworkManager and resolved config. | ||
| if (( EUID == 0 )); then | ||
| pass "running as root; skipping the elevation checks, which would rewrite this machine's DNS" | ||
| exit 0 | ||
| fi | ||
|
|
||
| test_tmp=$(mktemp -d) | ||
| trap 'rm -rf "$test_tmp"' EXIT | ||
|
|
||
| stub_bin="$test_tmp/bin" | ||
| mkdir -p "$stub_bin" | ||
|
|
||
| # Both stubs stand in for the exec at the end of require_root, so the DNS | ||
| # writes below it never run, and neither real sudo nor real pkexec is reached. | ||
| for command in sudo pkexec; do | ||
| cat >"$stub_bin/$command" <<SH | ||
| #!/bin/bash | ||
| printf '$command %s\n' "\$*" >"\$ELEVATION_LOG" | ||
|
omarchybot marked this conversation as resolved.
Resolved
|
||
| SH | ||
| chmod +x "$stub_bin/$command" | ||
| done | ||
|
|
||
| # self_path canonicalizes \$OMARCHY_PATH/bin/omarchy-dns, which is how the | ||
| # script decides whether the sudoers rule can name it. On a packaged install | ||
| # that link lands on /usr/bin/omarchy-dns; a dev-linked checkout stays inside | ||
| # the checkout, where no rule covers it. | ||
| mkdir -p "$test_tmp/packaged/bin" "$test_tmp/checkout/bin" | ||
| ln -sfn /usr/bin/omarchy-dns "$test_tmp/packaged/bin/omarchy-dns" | ||
| cp "$dns" "$test_tmp/checkout/bin/omarchy-dns" | ||
|
|
||
| # The rule is %wheel-scoped, so group membership decides the route as much as | ||
| # the path and the provider do. Stub id rather than reading the real groups: | ||
| # the suite has to give the same answer on a build user outside wheel. | ||
| cat >"$stub_bin/id" <<'SH' | ||
| #!/bin/bash | ||
| [[ ${1:-} == -nG ]] || exec /usr/bin/id "$@" | ||
| printf '%s\n' "${STUB_GROUPS-users wheel}" | ||
| SH | ||
| chmod +x "$stub_bin/id" | ||
|
|
||
| elevation_for() { | ||
| local omarchy_path="$1" | ||
| local provider="$2" | ||
|
|
||
| : >"$test_tmp/elevation" | ||
| ELEVATION_LOG="$test_tmp/elevation" \ | ||
| OMARCHY_PATH="$omarchy_path" \ | ||
| PATH="$stub_bin:$PATH" \ | ||
| bash "$dns" "$provider" </dev/null >/dev/null | ||
| cat "$test_tmp/elevation" | ||
| } | ||
|
|
||
| for provider in Cloudflare Google DHCP; do | ||
| elevation=$(elevation_for "$test_tmp/packaged" "$provider") | ||
| [[ $elevation == "sudo /usr/bin/omarchy-dns $provider" ]] || | ||
| fail "omarchy-dns takes the passwordless sudo grant for $provider without a terminal" "got: $elevation" | ||
| done | ||
|
|
||
| pass "omarchy-dns elevates the stock providers through sudo, not polkit" | ||
|
|
||
| custom=$(elevation_for "$test_tmp/packaged" Custom) | ||
| [[ $custom == "pkexec /usr/bin/omarchy-dns Custom" ]] || | ||
| fail "omarchy-dns leaves Custom on the polkit path, since no sudoers rule covers it" "got: $custom" | ||
|
|
||
| non_wheel=$(STUB_GROUPS="users" elevation_for "$test_tmp/packaged" Cloudflare) | ||
| [[ $non_wheel == "pkexec /usr/bin/omarchy-dns Cloudflare" ]] || | ||
| fail "omarchy-dns leaves a user outside %wheel on the polkit path, since the rule cannot match them" "got: $non_wheel" | ||
|
|
||
| dev_linked=$(elevation_for "$test_tmp/checkout" Cloudflare) | ||
| [[ $dev_linked == "pkexec $test_tmp/checkout/bin/omarchy-dns Cloudflare" ]] || | ||
| fail "omarchy-dns falls back to polkit where the sudoers rule cannot name the script" "got: $dev_linked" | ||
|
|
||
| pass "omarchy-dns falls back to polkit wherever the grant does not reach" | ||
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.