Password dialog rebuilt: a separate polkit agent, one state machine, explicit errors
CI / test (push) Canceled after 21m14s

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
This commit is contained in:
Claude committed 2026-11-26 05:09:49 +00:00
1 parent 2c33c4a847
commit 80af76fb15
22 files changed
+3462 -1806

No files matched your search

+311
View File
@@ -0,0 +1,311 @@
///| chandra-polkit-agent's work (compositor/authagent is its main): be the session's polkit
/// agent and run each request's state machine, talking to the compositor over the link
/// (wire.mbt). In demo mode there is no polkit: one pretend request, then it exits.
///
/// Everything it logs goes to stderr, which is the session log; stdout is the link.
///| Log a line to stderr (the session log), never stdout (the link).
pub fn alog(msg : String) -> Unit {
eprintln("chandra-polkit-agent: " + msg)
}
///| `describe` with the real process lookups.
fn describe_default(r : AuthRequest) -> Description {
describe(r)
}
///| The agent's state.
pub struct AgentLoop {
link : LineConn
/// The polkit side; None in demo mode.
polkit : PolkitAgent?
/// Demo mode ("" for the real agent).
demo : String
start : Starter
describe : (AuthRequest) -> Description
/// The request being shown and its link id.
mut machine : AuthMachine?
mut id : String
mut next_id : Int
mut was_registered : Bool
/// Done: the main loop exits.
mut finished : Bool
}
///| The real agent on `link_fd`. `routes` reach polkit's helper (empty: the usual ones).
pub fn AgentLoop::new(
link_fd : Int,
polkit : PolkitAgent,
routes? : Array[HelperRoute] = [],
describer? : (AuthRequest) -> Description = describe_default,
) -> AgentLoop {
let start : Starter = fn(user, cookie) {
let rs = if routes.length() > 0 { routes } else { helper_routes() }
match HelperSession::start_any(rs, user, cookie) {
Ok(h) => Ok(h as &Conversation)
Err(e) => Err(e)
}
}
{
link: LineConn::new(link_fd),
polkit: Some(polkit),
demo: "",
start,
describe: describer,
machine: None,
id: "",
next_id: 1,
was_registered: false,
finished: false,
}
}
///| The demo agent on `link_fd`.
pub fn AgentLoop::new_demo(link_fd : Int, mode : String) -> AgentLoop {
let mode = if demo_modes.contains(mode) { mode } else { "password" }
{
link: LineConn::new(link_fd),
polkit: None,
demo: mode,
start: demo_starter(mode),
describe: describe_default,
machine: None,
id: "",
next_id: 1,
was_registered: false,
finished: false,
}
}
///| Begin: ask polkit to make us the session's agent (the answer comes in `step`), or open the
/// demo request. On failure the compositor is told why and the loop finishes.
pub fn AgentLoop::start(self : AgentLoop) -> Unit {
match self.polkit {
Some(p) =>
if p.start() {
alog("registering with polkit for this session")
} else {
self.give_up("polkit could not be asked: " + p.error())
}
None => {
ignore(self.link.send(["ready"]))
let (req, desc) = demo_request(self.demo)
alog("demo (" + self.demo + ")")
self.open(req, desc)
}
}
}
///| Tell the compositor why we can't serve, and finish.
fn AgentLoop::give_up(self : AgentLoop, why : String) -> Unit {
alog(why)
ignore(self.link.send(["error", why]))
self.finished = true
self.link.close()
}
///| Show `req`: its words, accounts, and first state; its check starts.
fn AgentLoop::open(self : AgentLoop, req : AuthRequest, desc : Description) -> Unit {
let id = self.next_id.to_string()
self.next_id = self.next_id + 1
self.id = id
let m = AuthMachine::new(req, desc, self.start)
self.machine = Some(m)
alog("request " + id + ": " + req.action_id + " (" + desc.title + ")")
ignore(self.link.send(["request", id, desc.title, desc.subtitle, desc.command, desc.program, req.icon_name, req.message]))
for d in desc.details {
ignore(self.link.send(["detail", id, d.0, d.1]))
}
for u in req.users {
ignore(self.link.send(["user", id, u.name, u.real_name, u.uid.to_string()]))
}
m.begin()
self.send_state()
}
///| Send the state if it changed.
fn AgentLoop::send_state(self : AgentLoop) -> Unit {
match self.machine {
Some(m) => if m.take_changed() { ignore(self.link.send(m.state_fields(self.id))) }
None => ()
}
}
///| The request is over: answer polkit, tell the compositor.
fn AgentLoop::close_request(self : AgentLoop, result : Result[Unit, String]?) -> Unit {
match self.machine {
Some(m) => {
m.cancel()
match (self.polkit, result) {
(Some(p), Some(r)) => p.finish(m.req.cookie, r)
_ => ()
}
alog(
"request " + self.id + " " +
(match result {
Some(Ok(_)) => "authorized"
Some(Err(_)) => "dismissed"
None => "withdrawn by polkit"
}),
)
ignore(self.link.send(["close", self.id]))
self.machine = None
if self.polkit is None {
// The demo shows one request.
self.finished = true
self.link.close()
}
}
None => ()
}
}
///| A message from the compositor.
fn AgentLoop::on_link(self : AgentLoop, line : FixedArray[Byte]) -> Unit {
let f = split_fields(line)
zero(line)
if f.length() < 2 {
return
}
let cmd = field_text(f[0])
let id = field_text(f[1])
let m = match self.machine {
Some(m) if id == self.id => m
_ => {
for b in f {
zero(b)
}
return
}
}
match cmd {
"answer" =>
if f.length() >= 3 {
m.answer(f[2])
}
"user" => m.pick_user(parse_uint(f.get(2).map(field_text).unwrap_or("")).unwrap_or(-1))
"retry" => m.retry()
"cancel" => {
self.close_request(Some(Err("The user dismissed the authentication dialog")))
return
}
_ => ()
}
for b in f {
zero(b)
}
}
///| The fds to wait on.
pub fn AgentLoop::fds(self : AgentLoop) -> Array[Int] {
let out = [self.link.fd()]
match self.polkit {
Some(p) => if p.fd() >= 0 { out.push(p.fd()) }
None => ()
}
match self.machine {
Some(m) => if m.fd() >= 0 { out.push(m.fd()) }
None => ()
}
out
}
///| How long to wait for the fds before stepping anyway (ms; -1: no limit).
pub fn AgentLoop::timeout(self : AgentLoop) -> Int {
match self.machine {
Some(m) if m.needs_timer() => 50
_ => -1
}
}
///| Serve whatever is ready.
pub fn AgentLoop::step(self : AgentLoop) -> Unit {
if self.finished {
return
}
for line in self.link.read_lines() {
self.on_link(line)
}
if self.link.eof {
// The compositor closed the link (the Settings switch, or the session ended).
alog("the compositor closed the link; stopping")
self.shutdown()
return
}
match self.polkit {
Some(p) => {
ignore(p.tick())
if p.registered() && !self.was_registered {
self.was_registered = true
alog("registered: polkit asks this agent for this session's passwords")
ignore(self.link.send(["ready"]))
}
if p.failed() {
self.close_request(None)
self.give_up(
if self.was_registered {
"lost the system bus: " + p.error()
} else {
"polkit refused the agent: " + p.error()
},
)
return
}
match self.machine {
Some(m) => if !p.waiting(m.req.cookie) { self.close_request(None) }
None => ()
}
}
None => ()
}
match self.machine {
Some(m) => {
m.pump()
self.send_state()
if m.phase == Done {
self.close_request(Some(if m.authorized { Ok(()) } else { Err("The user dismissed the authentication dialog") }))
}
}
None => ()
}
if self.machine is None && !self.finished {
match self.polkit {
Some(p) =>
match p.current() {
Some(req) => self.open(req, (self.describe)(req))
None => ()
}
None => ()
}
}
}
///| Stop: dismiss what waits, leave polkit, close the link.
pub fn AgentLoop::shutdown(self : AgentLoop) -> Unit {
match self.machine {
Some(m) => m.cancel()
None => ()
}
self.machine = None
match self.polkit {
Some(p) => p.stop()
None => ()
}
self.link.close()
self.finished = true
}
///| Whether the loop is done.
pub fn AgentLoop::finished(self : AgentLoop) -> Bool {
self.finished
}
///| Run until done (the agent's main).
pub fn AgentLoop::run(self : AgentLoop) -> Unit {
self.start()
while !self.finished {
ignore(@sys.wait_readable(self.fds(), self.timeout()))
self.step()
}
self.link.close()
}