///| `chandra-ctl auth-demo [MODE]`: the real dialog, link and state machine with a pretend /// password check in place of polkit and PAM, so the dialog can be tried without root and /// without anything actually running. The password is "demo". /// /// Modes: password (the default), face (looks for a face, gives up, asks for the password), /// face-ok (a face is recognized), die (the first check quits before asking, like a broken /// polkit setup; Try again works), slow (asks after three seconds), users (two accounts). ///| pub let demo_password : String = "demo" ///| pub let demo_modes : Array[String] = ["password", "face", "face-ok", "die", "slow", "users"] ///| The pretend PAM conversation: events on a timer, and a verdict on the answer. pub struct FakePam { /// Events still to come: (due time in monotonic ms, event). script : Array[(UInt64, HelperEvent)] mut closed : Bool } ///| A pretend check in `mode`; `attempt` counts from 0 (the `die` mode only fails the first). pub fn FakePam::new(mode : String, attempt : Int) -> FakePam { let now = @sys.monotonic_ms() let at = fn(ms : Int, ev : HelperEvent) { (now + ms.to_uint64(), ev) } let ask = PromptEchoOff("Password: ") let script = match mode { "face" => [ at(0, TextInfo("Face unlock: looking for your face")), at(2500, ErrorMsg("Face unlock: face not recognized")), at(2500, ask), ] "face-ok" => [at(0, TextInfo("Face unlock: looking for your face")), at(2000, TextInfo("Face unlock: recognized")), at(2600, Success)] "die" => if attempt == 0 { [at(400, Gone)] } else { [at(0, ask)] } "slow" => [at(3000, ask)] _ => [at(0, ask)] } { script, closed: false } } ///| pub impl Conversation for FakePam with poll(self) { let out : Array[HelperEvent] = [] if self.closed { return out } let now = @sys.monotonic_ms() while self.script.length() > 0 && self.script[0].0 <= now { let ev = self.script.remove(0).1 out.push(ev) if ev == Success || ev == Failure || ev == Gone { self.closed = true break } } out } ///| pub impl Conversation for FakePam with answer(self, a) { if self.closed { return false } let ok = field_text(a) == demo_password let now = @sys.monotonic_ms() self.script.push((now + 700UL, if ok { Success } else { Failure })) true } ///| pub impl Conversation for FakePam with fd(_self) { -1 } ///| pub impl Conversation for FakePam with close(self) { self.closed = true self.script.clear() } ///| The account at the screen: $USER (with its real name from /etc/passwd when there). fn demo_user() -> AuthUser { let uid = own_uid() let passwd = parse_passwd(@utf8.decode_lossy(@sys.read_file_bytes("/etc/passwd"))) match passwd.get(uid) { Some((name, real)) => { uid, name, real_name: real } None => { let n = @sys.getenv("USER") { uid, name: if n != "" { n } else { "you" }, real_name: "" } } } } ///| The demo request and how the dialog words it. pub fn demo_request(mode : String) -> (AuthRequest, Description) { let me = demo_user() let users = if mode == "users" { [me, { uid: 0, name: "root", real_name: "root" }] } else { [me] } let req : AuthRequest = { action_id: "org.freedesktop.systemd1.manage-units", message: "Authentication is required to start transient unit 'run-p1-i1.service'.", icon_name: "", details: {}, cookie: "demo", users, } let desc : Description = { title: "Run a command as root", subtitle: "A demo: nothing runs. The password is \"" + demo_password + "\".", command: "run0 systemctl restart chandra-demo.service", program: "", details: [("Demo", "chandra-ctl auth-demo " + mode), ("Runs as", "root"), ("Action", req.action_id)], } (req, desc) } ///| Starts the demo's pretend checks, counting attempts. pub fn demo_starter(mode : String) -> Starter { let attempts : Ref[Int] = { val: 0 } fn(_user, _cookie) { let n = attempts.val attempts.val = n + 1 Ok(FakePam::new(mode, n) as &Conversation) } }