Files
chandra/docs/auth-dialog.md
T
Claude 80af76fb15
CI / test (push) Canceled after 21m14s
Password dialog rebuilt: a separate polkit agent, one state machine, explicit errors
The first version ran polkit registration, polkit-agent-helper-1, the PAM
conversation and the drawing inside the compositor's frame loop, with
overlapping state in two layers. A helper that quit before asking was taken
for a wrong password and restarted in a loop that wiped the field (typing
seemed dead), a refused registration quietly started a third-party agent,
and the dialog and the Face unlock window each grabbed the keyboard.

Now:
- chandra-polkit-agent (compositor/authagent, /usr/local/lib/chandra) does
  polkit and PAM. Each request runs one state machine (polkit/machine.mbt:
  starting, asking, checking, failed, done). A check that quits or can't
  start is Failed with the reason and waits for Try again; only a wrong
  password starts the next check by itself. It logs each step to stderr
  (the session log), never the typed text.
- The compositor starts it on a socket pair and only draws what it says
  (host/auth.mbt, auth_draw.mbt; line protocol in polkit/wire.mbt). One
  input owner (grab_input): the dialog, else the Face unlock window.
- No silent fallback: if the agent can't run, polkit refuses it, polkit
  doesn't answer in 10 s, or it dies, a notice on screen says why (a crash
  after it ran restarts it, up to 3 times, and says so). The third-party
  agent runs only when the Settings switch is off.
- chandra-ctl auth-demo [password|face|face-ok|die|slow|users] shows the
  real dialog with a pretend check (password "demo"), no root needed;
  chandra-ctl auth-status says whether the agent is registered.
- just install builds and installs the agent (just auth-agent).

Tests: polkit_test (link, machine, view, helper Gone) and auth_test end to
end through handle_input: the agent loop in-process against a mock polkit
on a private dbus-daemon plus a mock helper, and the real agent program
spawned by the compositor (demo, and registered with the mock polkit).
Typing on a real xkb keymap, wrong password, cancel, withdrawal, helper
quitting early or missing, polkit refusing, agent not starting or crashing,
demo modes, Face unlock Apply. 170/170 host and 20/20 polkit tests pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hgkRPrAzXECBUGmNEE
2026-11-26 05:09:49 +00:00

125 lines
7.3 KiB
Markdown

# The password dialog (polkit agent)
When run0, pkexec, a disk mount or any other polkit-checked action needs an administrator's
password, Chandra asks in its own dialog: a card in the middle of a dimmed screen, drawn by the
compositor above every window and client menu.
## Try it
```sh
chandra-ctl auth-demo # the dialog with a pretend check: the password is "demo"
chandra-ctl auth-demo face # looks for a face, gives up, then asks for the password
chandra-ctl auth-demo die # the check quits before asking (a broken polkit setup); Try again
chandra-ctl auth-status # is the agent running and registered with polkit, or why not
run0 true # the real thing: your password, through polkit and PAM
```
Demo modes: `password` (default), `face`, `face-ok`, `die`, `slow`, `users`. A demo needs no
root and runs nothing; it uses the same agent program, link, state machine and drawing as a real
request, only the password check is pretend.
If something goes wrong, the session log says what happened step by step:
```sh
grep -E 'chandra-polkit-agent|password dialog' ~/.local/state/chandra/session.log
```
## What it shows
- **What it says.** For run0 the dialog shows the command that will run (`gparted /dev/sda`) and as
whom, instead of polkit's "start transient unit 'run-p15802-i1990.service'". pkexec's command
comes from polkit's details. Everything else shows polkit's own message. Details (Alt+D) lists
who asked, the target user, the unit and the polkit action id.
- **Who answers.** The account polkit wants: you when you are an administrator, else one of the
admins (members of `wheel`/`sudo` are expanded). With several, Tab to the account row and use
← →.
- **Keyboard.** The field has the focus. Enter authenticates, Esc cancels, Tab / Shift+Tab move
between the account, Show, Details, Cancel and Authenticate, Space or Enter activates them.
Alt+R shows or hides the password, Ctrl+U clears it. Typing goes through the xkb keymap apps
get (groups, Shift, AltGr, Caps Lock), so the password is typed on your layout. Caps Lock on
shows a warning.
- **PAM.** Password prompts, visible prompts (a one-time code), information (a fingerprint reader)
and errors (faillock's "account is locked") are shown as PAM says them. A wrong password shakes
the card, says so, and PAM asks again. Face unlock gets its own row ("Looking for your face…",
then "Face recognized" or why not); the password can be typed meanwhile and goes in when PAM
asks for it. See [face-unlock.md](face-unlock.md).
- **When the check fails without a verdict** (polkit's helper can't be reached, quits before
asking, or quits while checking), the dialog says exactly that and waits: the main button
becomes **Try again**. Nothing is retried behind your back.
- **Safety.** While the dialog is up no client has keyboard focus, input goes to the dialog only,
and client popups are hidden. The password is kept as characters wiped after sending, the
buffers it passes through are zeroed, and nothing of it is logged.
## How it works
Two parts, one process each:
- **`chandra-polkit-agent`** (`compositor/authagent`, installed in `/usr/local/lib/chandra/`)
does everything polkit and PAM: it registers on the system bus as this login session's
authentication agent (`RegisterAuthenticationAgent` for `XDG_SESSION_ID`), serves
`BeginAuthentication` / `CancelAuthentication` at `/org/chandra/PolicyKit1/AuthenticationAgent`,
runs polkit's `polkit-agent-helper-1` (polkit 126's `/run/polkit/agent-helper.socket`, else the
setuid program), and keeps each request in **one state machine**
(`compositor/polkit/machine.mbt`):
```
Starting --PAM asks--> Asking --answer--> Checking --SUCCESS--> Done
^ |
+---- new check <----+ FAILURE (wrong password: counted, card shakes)
Starting/Checking --check quits, can't start, ends without asking--> Failed --Try again--> Starting
any --cancel--> Done
```
It logs every step to stderr, which is the session log (never what was typed).
- **The compositor** (`compositor/host/auth.mbt`, `auth_draw.mbt`) starts the agent on a socket
pair (its stdin/stdout), draws the dialog from the state the agent sends, owns the keyboard
and the pointer while it is up (`grab_input`: the dialog, else the Face unlock window, never
both), and sends back the answer, the account picked, Try again, or Cancel. The link is a
line protocol (`compositor/polkit/wire.mbt`); only the compositor holds the other end, so no
other program can open the dialog or read what is typed into it.
### When it can't work, you are told
There is no silent fallback. A notice at the top of the screen (click to dismiss) and the session
log say what happened:
- the agent program isn't installed or can't be run;
- polkit refused the agent (say, another agent already has the session) or there is no system
bus;
- polkit hasn't answered within 10 seconds;
- the agent stopped after it was running: it is started again (up to three times) and the notice
says so.
Settings > Session > **Chandra password dialog** off is the only way another polkit agent runs
(the first one `chandra-session` finds, passed as `CHANDRA_POLKIT_FALLBACK`); it is saved as
state.edn `:auth-agent :system`. Turning it back on while that agent runs takes effect at the next
login (polkit allows one agent per session), and the notice says so. `CHANDRA_POLKIT_AGENT=none`
starts no agent; `=PATH` starts that agent instead. `CHANDRA_POLKIT_AGENT_BIN` points at another
build of chandra-polkit-agent, and `CHANDRA_POLKIT_HELPER=socket:/path` or `=/path/to/helper`
picks polkit's helper (both for debugging).
### Why it was rebuilt (2026-11-26)
The first version ran polkit, the helper, PAM and the drawing inside the compositor's frame loop,
with overlapping state in two layers. A helper that quit before asking was taken for a wrong
password and restarted in a tight loop that wiped the field, so typing seemed to do nothing; a
refused registration quietly started another agent; the dialog and the Face unlock window each
grabbed the keyboard. The split above gives one state machine, one input owner, and an explicit
error for every failure.
## Tests
- `compositor/polkit/polkit_test.mbt`: keymaps, helper lines (and `Gone` when the helper quits),
run0 parsing, descriptions, the link encoding, the state machine (wrong password, early quit,
no retry until asked, face unlock, account picker) and the view (keys, Try again).
- `compositor/host/auth_test.mbt`, end to end through `handle_input`: the agent loop in-process
against a mock polkit Authority on a private `dbus-daemon` and a mock helper socket, and the
**real agent program** spawned by the compositor (demo, and registered with the mock polkit
through its own process). Covers typing on a real xkbcommon keymap, wrong password, cancel,
polkit withdrawing a request, the helper quitting early, the helper missing, polkit refusing the
agent, the agent not starting or crashing (notices), the demo modes, and Face unlock's Apply.
`CHANDRA_AUTH_SHOTS=/some/dir` writes a PNG of each state. Build the agent first
(`just auth-agent`), or the real-program test is skipped.
- Not covered: a real polkitd and PAM (a real logind session, the real helper's socket and setuid
paths). `run0 true` in the session is that test.