Files
chandra/compositor/polkit/wire.mbt
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

215 lines
6.1 KiB
MoonBit

///| The link between Chandra's polkit agent (chandra-polkit-agent, agent_loop.mbt) and the
/// compositor that draws its dialog (compositor/host/auth.mbt).
///
/// One message per line; fields are separated by tabs, and a backslash, tab or newline inside a
/// field is written \\, \t or \n. The compositor starts the agent on a socket pair (sys
/// spawn_socket): the agent's stdin and stdout are the compositor's end, so no other program
/// can open the dialog or read what is typed into it.
///
/// Agent to compositor:
/// ready registered with polkit; requests may follow
/// error <text> the agent can't serve (polkit refused, no bus); it exits
/// request <id> <title> <subtitle> <command> <program> <icon> <polkit message>
/// detail <id> <label> <value> a Details row (repeated)
/// user <id> <login> <real name> <uid> an account that may answer (repeated)
/// state <id> <phase> <prompt> <echo 0|1> <error> <info> <face> <face text> <failures> <user>
/// close <id> the request is over (answered, cancelled, withdrawn)
/// Compositor to agent:
/// answer <id> <text> what was typed (the field is wiped once it is sent)
/// user <id> <index> answer as another account
/// retry <id> start the password check again after it failed
/// cancel <id> the user dismissed the dialog
///| Append field byte `b`, escaped.
fn push_escaped(out : Array[Byte], b : Byte) -> Unit {
if b == b'\\' {
out.push(b'\\')
out.push(b'\\')
} else if b == b'\t' {
out.push(b'\\')
out.push(b't')
} else if b == b'\n' {
out.push(b'\\')
out.push(b'n')
} else {
out.push(b)
}
}
///| One message: the fields, escaped, joined by tabs, and a newline.
pub fn encode_line(fields : Array[String]) -> Bytes {
let out : Array[Byte] = []
for i, f in fields {
if i > 0 {
out.push(b'\t')
}
let b = @utf8.encode(f)
for k = 0; k < b.length(); k = k + 1 {
push_escaped(out, b[k])
}
}
out.push(b'\n')
Bytes::from_array(out)
}
///| `head` fields then `secret` as the last field, in a buffer the caller zeroes after sending.
pub fn encode_secret_line(head : Array[String], secret : FixedArray[Byte]) -> FixedArray[Byte] {
let out : Array[Byte] = []
for f in head {
let b = @utf8.encode(f)
for k = 0; k < b.length(); k = k + 1 {
push_escaped(out, b[k])
}
out.push(b'\t')
}
for k = 0; k < secret.length(); k = k + 1 {
push_escaped(out, secret[k])
}
out.push(b'\n')
let buf = FixedArray::makei(out.length(), i => out[i])
for k = 0; k < out.length(); k = k + 1 {
out[k] = b'\x00'
}
buf
}
///| A line's fields as raw bytes (escapes undone), each in its own buffer.
pub fn split_fields(line : FixedArray[Byte]) -> Array[FixedArray[Byte]] {
let out : Array[FixedArray[Byte]] = []
let cur : Array[Byte] = []
let flush = fn() {
out.push(FixedArray::makei(cur.length(), i => cur[i]))
for k = 0; k < cur.length(); k = k + 1 {
cur[k] = b'\x00'
}
cur.clear()
}
let mut i = 0
while i < line.length() {
let b = line[i]
if b == b'\t' {
flush()
} else if b == b'\\' && i + 1 < line.length() {
let n = line[i + 1]
cur.push(if n == b't' { b'\t' } else if n == b'n' { b'\n' } else { n })
i = i + 1
} else {
cur.push(b)
}
i = i + 1
}
flush()
out
}
///| A field as text.
pub fn field_text(b : FixedArray[Byte]) -> String {
@utf8.decode_lossy(Bytes::makei(b.length(), i => b[i]))
}
///| A line's fields as text.
pub fn decode_line(line : FixedArray[Byte]) -> Array[String] {
split_fields(line).map(field_text)
}
///| A stream socket read and written one line at a time, without blocking on reads.
pub struct LineConn {
mut fd : Int
inbuf : Array[Byte]
/// The other end closed it.
mut eof : Bool
}
///|
pub fn LineConn::new(fd : Int) -> LineConn {
{ fd, inbuf: [], eof: fd < 0 }
}
///| Whole lines that arrived (without their newlines); sets `eof` when the other end closed.
/// The bytes read are wiped from the buffer as lines are taken (answers pass through here).
pub fn LineConn::read_lines(self : LineConn) -> Array[FixedArray[Byte]] {
let out : Array[FixedArray[Byte]] = []
if self.fd < 0 {
return out
}
let buf : FixedArray[Byte] = FixedArray::make(4096, b'\x00')
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 {
self.eof = true
break
}
for i = 0; i < n; i = i + 1 {
self.inbuf.push(buf[i])
buf[i] = b'\x00'
}
}
let mut start = 0
for i = 0; i < self.inbuf.length(); i = i + 1 {
if self.inbuf[i] == b'\n' {
out.push(FixedArray::makei(i - start, k => self.inbuf[start + k]))
start = i + 1
}
}
if start > 0 {
for k = 0; k < start; k = k + 1 {
self.inbuf[k] = b'\x00'
}
let rest = self.inbuf[start:].to_array()
self.inbuf.clear()
self.inbuf.append(rest)
}
out
}
///| Send raw bytes (a whole line). False when the other end is gone.
pub fn LineConn::send_raw(self : LineConn, b : Bytes) -> Bool {
if self.fd < 0 {
return false
}
@sys.sendmsg_fd(self.fd, b, -1) == b.length()
}
///| Send one message.
pub fn LineConn::send(self : LineConn, fields : Array[String]) -> Bool {
self.send_raw(encode_line(fields))
}
///| Send a line built by `encode_secret_line`, then zero it.
pub fn LineConn::send_secret(self : LineConn, line : FixedArray[Byte]) -> Bool {
let b = Bytes::makei(line.length(), i => line[i])
let ok = self.send_raw(b)
zero(line)
ok
}
///| The socket (-1 once closed).
pub fn LineConn::fd(self : LineConn) -> Int {
self.fd
}
///|
pub fn LineConn::close(self : LineConn) -> Unit {
if self.fd >= 0 {
@sys.close_fd(self.fd)
self.fd = -1
}
for k = 0; k < self.inbuf.length(); k = k + 1 {
self.inbuf[k] = b'\x00'
}
self.inbuf.clear()
}
///| Zero a buffer a password was in.
pub fn zero(b : FixedArray[Byte]) -> Unit {
for i = 0; i < b.length(); i = i + 1 {
b[i] = b'\x00'
}
}