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

+64 -81
View File
@@ -19,6 +19,9 @@ pub(all) enum HelperEvent {
TextInfo(String)
Success
Failure
/// The helper went away without saying SUCCESS or FAILURE (it crashed, was refused, or
/// couldn't run PAM at all).
Gone
} derive(Eq, Debug)
///| Undo g_strescape: \n \t \r \b \f \v \\ \" and octal \NNN (bytes, re-read as UTF-8).
@@ -101,138 +104,118 @@ pub(all) enum HelperRoute {
Program(String)
} derive(Eq, Debug)
///| A route, for the log.
fn route_text(r : HelperRoute) -> String {
///| A route, for the log and for errors.
pub fn route_text(r : HelperRoute) -> String {
match r {
Socket(p) => "socket " + p
Program(p) => p
}
}
///| One run of the helper: the connection and the bytes read but not yet a whole line.
///| A PAM conversation: polkit's helper, or the demo's stand-in (demo.mbt).
pub(open) trait Conversation {
/// What was said since the last call (non-blocking); ends with Success, Failure or Gone.
poll(Self) -> Array[HelperEvent]
/// Answer the prompt (the bytes, without a newline); false when it can't be sent.
answer(Self, FixedArray[Byte]) -> Bool
/// The fd to wait on, -1 for none (the caller then polls on a timer).
fd(Self) -> Int
/// End the conversation.
close(Self) -> Unit
}
///| One run of polkit-agent-helper-1.
pub struct HelperSession {
mut fd : Int
inbuf : Array[Byte]
/// Set once SUCCESS or FAILURE came, or the helper went away.
conn : LineConn
/// SUCCESS, FAILURE or Gone was reported.
mut ended : Bool
}
///| Start the helper to authenticate `user` for `cookie` by the first route that works. None
/// when none does.
pub fn HelperSession::start_any(routes : Array[HelperRoute], user : String, cookie : String) -> HelperSession? {
///| Start the helper to authenticate `user` for `cookie` by the first route that works; Err
/// says which routes were tried and how each failed.
pub fn HelperSession::start_any(routes : Array[HelperRoute], user : String, cookie : String) -> Result[HelperSession, String] {
let tried : Array[String] = []
for r in routes {
match HelperSession::start(r, user, cookie) {
Some(s) => {
@sys.log("chandra: polkit helper for " + user + " via " + route_text(r))
return Some(s)
alog("password check for " + user + " via " + route_text(r))
return Ok(s)
}
None => @sys.log("chandra: polkit helper not reachable via " + route_text(r))
None => tried.push(route_text(r))
}
}
None
if tried.length() == 0 {
Err("polkit's password checker (polkit-agent-helper-1) is not installed")
} else {
Err("polkit's password checker could not be started (tried " + tried.join(", ") + ")")
}
}
///| Start the helper to authenticate `user` for `cookie`. None when it can't be started.
///| Start the helper to authenticate `user` for `cookie`. None when it can't be reached.
pub fn HelperSession::start(route : HelperRoute, user : String, cookie : String) -> HelperSession? {
let (fd, first) = match route {
Socket(path) => (@sys.unix_connect(path), user + "\n" + cookie + "\n")
Program(path) => (@sys.spawn_socket([path, user]), cookie + "\n")
Program(path) => if @sys.has_exe(path) { (@sys.spawn_socket([path, user]), cookie + "\n") } else { (-1, "") }
}
if fd < 0 {
return None
}
let b = @utf8.encode(first)
if @sys.sendmsg_fd(fd, b, -1) != b.length() {
@sys.close_fd(fd)
let conn = LineConn::new(fd)
if !conn.send_raw(@utf8.encode(first)) {
conn.close()
return None
}
Some({ fd, inbuf: [], ended: false })
Some({ conn, ended: false })
}
///| A session over an already-connected fd (tests).
pub fn HelperSession::over(fd : Int) -> HelperSession {
{ fd, inbuf: [], ended: false }
{ conn: LineConn::new(fd), ended: false }
}
///| What arrived since the last call (non-blocking). The helper going away without an answer
/// reads as Failure.
pub fn HelperSession::read(self : HelperSession) -> Array[HelperEvent] {
///|
pub impl Conversation for HelperSession with poll(self) {
let out : Array[HelperEvent] = []
if self.fd < 0 {
if self.ended {
return out
}
let buf : FixedArray[Byte] = FixedArray::make(4096, b'\x00')
let mut eof = false
while @sys.poll_read(self.fd, 0) > 0 {
let (n, extra) = @sys.recvmsg_fd(self.fd, buf, buf.length())
if extra >= 0 {
@sys.close_fd(extra)
}
if n == -1 {
break
}
if n <= 0 {
eof = true
break
}
for i = 0; i < n; i = i + 1 {
self.inbuf.push(buf[i])
}
}
while true {
let mut nl = -1
for i, b in self.inbuf {
if b == b'\n' {
nl = i
break
}
}
if nl < 0 {
break
}
let line = @utf8.decode_lossy(Bytes::from_array(self.inbuf[:nl].to_array()))
let rest = self.inbuf[nl + 1:].to_array()
self.inbuf.clear()
self.inbuf.append(rest)
match parse_helper_line(line) {
for line in self.conn.read_lines() {
match parse_helper_line(field_text(line)) {
Some(ev) => {
out.push(ev)
if ev == Success || ev == Failure {
self.ended = true
self.conn.close()
return out
}
}
None => ()
}
}
if eof {
if !self.ended {
out.push(Failure)
self.ended = true
}
self.close()
if self.conn.eof {
out.push(Gone)
self.ended = true
self.conn.close()
}
out
}
///| Answer the prompt with `answer` (its bytes then a newline); the caller zeroes its copy.
pub fn HelperSession::respond(self : HelperSession, answer : FixedArray[Byte]) -> Bool {
if self.fd < 0 {
return false
}
let n = answer.length()
let b = Bytes::makei(n + 1, i => if i < n { answer[i] } else { b'\n' })
@sys.sendmsg_fd(self.fd, b, -1) == b.length()
///|
pub impl Conversation for HelperSession with answer(self, a) {
let n = a.length()
let b = FixedArray::makei(n + 1, i => if i < n { a[i] } else { b'\n' })
let ok = self.conn.send_raw(Bytes::makei(b.length(), i => b[i]))
zero(b)
ok
}
///| The connection, for the main loop to wait on (-1 once closed).
pub fn HelperSession::fd(self : HelperSession) -> Int {
self.fd
///|
pub impl Conversation for HelperSession with fd(self) {
self.conn.fd()
}
///| Stop: closing the connection ends the helper's PAM conversation.
pub fn HelperSession::close(self : HelperSession) -> Unit {
if self.fd >= 0 {
@sys.close_fd(self.fd)
self.fd = -1
}
///| Closing the connection ends the helper's PAM conversation.
pub impl Conversation for HelperSession with close(self) {
self.ended = true
self.conn.close()
}