///| 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 the agent can't serve (polkit refused, no bus); it exits /// request <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' } }