///| One authentication request, from the moment polkit asks until it is answered: the one state /// machine of Chandra's password dialog. It runs in the agent process (agent_loop.mbt); the /// compositor only shows its state and sends back what the user typed or picked. /// /// Starting --prompt--> Asking --answer--> Checking --SUCCESS--> Done /// | ^ | /// | +---- a new check <--+ FAILURE: wrong password (shake, count it) /// +--- the check ends without asking, can't start, or quits while checking ---> Failed /// Failed --retry (Enter, or the Try again button)--> Starting /// any --cancel--> Done /// /// Nothing is retried behind the user's back: a check that ends before PAM asked anything is /// shown as Failed with the reason, and only the user starts it again. A wrong password starts /// the next check at once, because the user just answered and is waiting for the next prompt. ///| Where the request is. pub(all) enum Phase { /// The password check (PAM, through polkit's helper) runs and hasn't asked anything yet; it /// may be looking for a face or a fingerprint meanwhile. Starting /// PAM asked (`prompt`); the answer is being typed. Asking /// The answer went to PAM; waiting for the verdict. Checking /// The check stopped without a verdict on the answer; `error` says why. Waits for the user. Failed /// Over: authorized, or dismissed. Done } derive(Eq, Debug) ///| The phase's name on the link. pub fn phase_name(p : Phase) -> String { match p { Starting => "starting" Asking => "asking" Checking => "checking" Failed => "failed" Done => "done" } } ///| The phase a link name stands for. pub fn phase_of(s : String) -> Phase { match s { "asking" => Asking "checking" => Checking "failed" => Failed "done" => Done _ => Starting } } ///| Starts a conversation for (user, cookie), or says why it can't. pub type Starter = (String, String) -> Result[&Conversation, String] ///| The request and where it is. pub struct AuthMachine { req : AuthRequest desc : Description start : Starter mut user_sel : Int mut phase : Phase /// PAM's prompt, tidied ("Password: " -> "Password"). mut prompt : String /// The answer is shown as typed (PAM_PROMPT_ECHO_ON: a user name, a one-time code). mut echo : Bool mut error : String mut info : String mut face : FaceState /// Wrong answers so far. mut failures : Int /// An answer typed before PAM asked (while a face was being looked for): sent when it asks. mut queued : FixedArray[Byte]? mut conv : &Conversation? /// Done, and authorized. mut authorized : Bool /// Something changed since `take_changed`. mut changed : Bool } ///| A machine for `req`; `begin` starts it. pub fn AuthMachine::new(req : AuthRequest, desc : Description, start : Starter) -> AuthMachine { { req, desc, start, user_sel: 0, phase: Starting, prompt: "Password", echo: false, error: "", info: "", face: NoFace, failures: 0, queued: None, conv: None, authorized: false, changed: true, } } ///| "Password: " -> "Password"; "Password for nandi: " stays readable. pub fn tidy_prompt(p : String) -> String { let t = p.trim(chars=" \t\n").to_string() let t = if t.has_suffix(":") { t[:t.length() - 1].to_string().trim(chars=" ").to_string() } else { t } if t == "" { "Password" } else { t } } ///| The account answering, if any. pub fn AuthMachine::user(self : AuthMachine) -> AuthUser? { self.req.users.get(self.user_sel) } ///| Drop the conversation and any queued answer. fn AuthMachine::stop_conv(self : AuthMachine) -> Unit { match self.conv { Some(c) => c.close() None => () } self.conv = None match self.queued { Some(q) => zero(q) None => () } self.queued = None } ///| Start a password check for the selected account: Starting, or Failed when it can't start. pub fn AuthMachine::begin(self : AuthMachine) -> Unit { self.stop_conv() self.changed = true self.info = "" self.face = NoFace self.prompt = "Password" self.echo = false let user = match self.user() { Some(u) => u None => { self.fail("No account that can approve this is known here.") return } } match (self.start)(user.name, self.req.cookie) { Ok(c) => { self.conv = Some(c) self.phase = Starting } Err(why) => self.fail(why) } } ///| Stop with `why` (and log it): the user decides what next. fn AuthMachine::fail(self : AuthMachine, why : String) -> Unit { self.stop_conv() self.phase = Failed self.error = why if self.face == Scanning { self.face = NoFace } self.changed = true alog(why) } ///| Send `a` to PAM now (it is asking) and zero it. fn AuthMachine::send_answer(self : AuthMachine, a : FixedArray[Byte]) -> Unit { let ok = match self.conv { Some(c) => c.answer(a) None => false } zero(a) if ok { self.phase = Checking self.error = "" self.info = "" } else { self.fail("The password check quit before it got the answer.") } self.changed = true } ///| The user's answer (the machine zeroes it). Asking: it goes to PAM. Starting: it waits for /// PAM's prompt. Failed: a new check starts and gets it. pub fn AuthMachine::answer(self : AuthMachine, given : FixedArray[Byte]) -> Unit { // Our own copy: the caller's buffer is wiped when it is done with it. let a = FixedArray::makei(given.length(), i => given[i]) zero(given) match self.phase { Asking => self.send_answer(a) Starting => { match self.queued { Some(q) => zero(q) None => () } self.queued = Some(a) } Failed => { self.begin() if self.phase == Starting { self.queued = Some(a) } else { zero(a) } } Checking | Done => zero(a) } } ///| Answer as account `i` instead: a new check for it. pub fn AuthMachine::pick_user(self : AuthMachine, i : Int) -> Unit { if i < 0 || i >= self.req.users.length() || i == self.user_sel || self.phase == Done { return } self.user_sel = i self.error = "" self.failures = 0 self.begin() } ///| Start again after Failed. pub fn AuthMachine::retry(self : AuthMachine) -> Unit { if self.phase == Failed { self.error = "" self.begin() } } ///| The user dismissed it. pub fn AuthMachine::cancel(self : AuthMachine) -> Unit { self.stop_conv() self.phase = Done self.authorized = false self.changed = true } ///| Read what PAM said and move on. pub fn AuthMachine::pump(self : AuthMachine) -> Unit { let c = match self.conv { Some(c) => c None => return } for ev in c.poll() { alog( "PAM " + (match ev { PromptEchoOff(t) => "asks (hidden) \"" + t + "\"" PromptEchoOn(t) => "asks (shown) \"" + t + "\"" ErrorMsg(t) => "error \"" + t + "\"" TextInfo(t) => "info \"" + t + "\"" Success => "SUCCESS" Failure => "FAILURE" Gone => "check ended without a verdict" }), ) self.event(ev) if self.conv is None { return } } } ///| One thing PAM said. pub fn AuthMachine::event(self : AuthMachine, ev : HelperEvent) -> Unit { self.changed = true match ev { TextInfo(m) | ErrorMsg(m) => match face_message(m, ev is ErrorMsg(_)) { Some(f) => { self.face = f return } None => () } _ => () } match ev { PromptEchoOff(p) | PromptEchoOn(p) => { // The face module gave up without saying why (a timeout, or Howdy's silent failures). if self.face == Scanning { self.face = FaceFailed("Face not recognized") } self.phase = Asking self.prompt = tidy_prompt(p) self.echo = ev is PromptEchoOn(_) self.info = "" match self.queued { Some(q) => { self.queued = None self.send_answer(q) } None => () } } ErrorMsg(m) => self.error = m.trim(chars=" \n").to_string() TextInfo(m) => self.info = m.trim(chars=" \n").to_string() Success => { self.stop_conv() self.phase = Done self.authorized = true } Failure if self.phase == Checking => { // A wrong answer: count it and ask again with a new check. self.failures = self.failures + 1 let pam_said = self.error let was_echo = self.echo self.begin() if self.phase == Starting { self.error = if pam_said != "" { pam_said } else if was_echo { "That didn't work. Try again." } else { "Wrong password. Try again." } } } Failure => self.fail( if self.error != "" { self.error } else { "The password check ended before asking for a password." }, ) Gone => self.fail( if self.phase == Checking { "The password check quit while checking the password." } else { "The password check quit before asking for a password." }, ) } } ///| The fd to wait on (-1: none, or a conversation without one). pub fn AuthMachine::fd(self : AuthMachine) -> Int { match self.conv { Some(c) => c.fd() None => -1 } } ///| Whether a conversation without an fd runs (the main loop polls it on a timer). pub fn AuthMachine::needs_timer(self : AuthMachine) -> Bool { match self.conv { Some(c) => c.fd() < 0 None => false } } ///| Whether something changed since the last call. pub fn AuthMachine::take_changed(self : AuthMachine) -> Bool { let c = self.changed self.changed = false c } ///| The face state's name and text on the link. pub fn face_fields(f : FaceState) -> (String, String) { match f { NoFace => ("", "") Scanning => ("scanning", "") Matched => ("matched", "") FaceFailed(why) => ("failed", why) } } ///| The face state a link name and text stand for. pub fn face_of(name : String, text : String) -> FaceState { match name { "scanning" => Scanning "matched" => Matched "failed" => FaceFailed(text) _ => NoFace } } ///| The `state` message for request `id`. pub fn AuthMachine::state_fields(self : AuthMachine, id : String) -> Array[String] { let (f, ft) = face_fields(self.face) [ "state", id, phase_name(self.phase), self.prompt, if self.echo { "1" } else { "0" }, self.error, self.info, f, ft, self.failures.to_string(), self.user_sel.to_string(), ] }