diff --git a/compositor/host/auth.mbt b/compositor/host/auth.mbt index e25a5a4..ba66913 100644 --- a/compositor/host/auth.mbt +++ b/compositor/host/auth.mbt @@ -22,6 +22,10 @@ pub(all) struct AuthUi { hits : Array[(@scene.Rect, @polkit.Focus)] /// When it opened (the spinner's clock). opened : UInt64 + /// Helper runs in a row that ended before anything was answered, and when to try again + /// (monotonic ms, 0: not waiting). + mut quick_failures : Int + mut retry_at : UInt64 } ///| The dialog that is up, if any. @@ -125,6 +129,12 @@ fn ChandraCompositor::auth_tick(self : ChandraCompositor) -> Unit { self.close_auth(None) } else { self.auth_helper_events(ui) + // A helper that ended early is tried again after a pause. + if ui.helper is None && ui.retry_at != 0UL && @sys.monotonic_ms() >= ui.retry_at { + ui.retry_at = 0UL + self.auth_start_helper(ui) + self.damage.mark_full() + } } None => () } @@ -157,6 +167,8 @@ fn ChandraCompositor::open_auth(self : ChandraCompositor, req : @polkit.AuthRequ icon, hits: [], opened: @sys.monotonic_ms(), + quick_failures: 0, + retry_at: 0UL, } self.auth = Some(ui) // Nothing else gets the keyboard or the pointer while it is up. @@ -191,6 +203,27 @@ fn ChandraCompositor::auth_helper_events(self : ChandraCompositor, ui : AuthUi) Some(h) => { let evs = h.read() for ev in evs { + // What PAM said, for the session log (never what was typed: that only goes out). + @sys.log( + "chandra: polkit helper: " + + (match ev { + PromptEchoOff(t) => "password prompt \"" + t + "\"" + PromptEchoOn(t) => "prompt \"" + t + "\"" + ErrorMsg(t) => "error \"" + t + "\"" + TextInfo(t) => "info \"" + t + "\"" + Success => "SUCCESS" + Failure => "FAILURE" + }), + ) + match ev { + PromptEchoOff(_) | PromptEchoOn(_) => { + ui.quick_failures = 0 + if ui.dialog.info == "Press Enter to try again." { + ui.dialog.info = "" + } + } + _ => () + } self.auth_effect(ui, ui.dialog.helper(ev, @sys.monotonic_ms())) if self.auth is None { return @@ -215,7 +248,27 @@ fn ChandraCompositor::auth_effect(self : ChandraCompositor, ui : AuthUi, e : @po } @polkit.zero(b) } - Restart => self.auth_start_helper(ui) + Restart => { + ui.quick_failures = 0 + ui.retry_at = 0UL + self.auth_start_helper(ui) + } + RetryLater => { + match ui.helper { + Some(h) => h.close() + None => () + } + ui.helper = None + ui.quick_failures = ui.quick_failures + 1 + @sys.log("chandra: polkit helper ended before asking anything (" + ui.quick_failures.to_string() + " in a row): " + ui.dialog.error) + // Try again after 1, 2, 4 s; then wait for Enter. + if ui.quick_failures <= 3 { + ui.retry_at = @sys.monotonic_ms() + (1000UL << (ui.quick_failures - 1)) + } else { + ui.retry_at = 0UL + ui.dialog.info = "Press Enter to try again." + } + } Cancelled => self.close_auth(Some(Err("The user dismissed the authentication dialog"))) Authorized => self.close_auth(Some(Ok(()))) } @@ -283,6 +336,16 @@ fn ChandraCompositor::auth_input(self : ChandraCompositor, ev : @nested.InputEve let mods = self.mods | self.mods_latched | self.mods_locked let ch = self.auth_keymap().char_for(code, mods, self.mods_group) self.auth_effect(ui, ui.dialog.key(code, self.mods, ch)) + // Enter with no helper running (it ended, or couldn't start): try again now. + if (code == @polkit.key_enter || code == @polkit.key_kp_enter) && self.auth is Some(_) && + ui.helper is None { + ui.retry_at = 0UL + ui.quick_failures = 0 + if ui.dialog.info == "Press Enter to try again." { + ui.dialog.info = "" + } + self.auth_start_helper(ui) + } } true } @@ -335,7 +398,11 @@ fn ChandraCompositor::auth_cursor(self : ChandraCompositor, ui : AuthUi) -> UInt ///| Whether the dialog animates (a failed attempt shakes it; a spinner while PAM checks). fn ChandraCompositor::auth_animating(self : ChandraCompositor) -> Bool { match self.auth { - Some(ui) => ui.dialog.shaking(@sys.monotonic_ms()) || ui.dialog.phase == Checking || ui.dialog.face == Scanning + Some(ui) => + ui.dialog.shaking(@sys.monotonic_ms()) || + ui.dialog.phase == Checking || + ui.dialog.face == Scanning || + ui.retry_at != 0UL None => false } } diff --git a/compositor/host/auth_test.mbt b/compositor/host/auth_test.mbt index 374299b..d9a3bf7 100644 --- a/compositor/host/auth_test.mbt +++ b/compositor/host/auth_test.mbt @@ -430,3 +430,136 @@ test "Settings > Session lists the password dialog switch, saved as :auth-agent assert_false(comp.shell.native_auth_agent) comp.close() } + +///| +test "typing reaches the dialog through handle_input, and a helper that dies at once doesn't wipe it in a loop" { + let bus = match auth_bus("typing") { + Some(b) => b + None => return + } + let a = MockAuthority::new(bus) + let hpath = "/tmp/chandra-auth-helper-typing-" + @sys.getpid().to_string() + let listen = @sys.unix_listen(hpath) + let comp = auth_comp("typing", bus, hpath) + // The keymap the real session hands clients (xkbcommon's text format), not the US fallback. + comp.server.set_keymap(@utf8.encode(real_keymap)) + assert_true(settle(comp, a, fn() { comp.polkit.map(p => p.registered()).unwrap_or(false) })) + a.begin("t1", run0_details) + assert_true(settle(comp, a, fn() { comp.auth_dialog() is Some(_) })) + let d = comp.auth_dialog().unwrap() + // The helper goes away without a word (socket helper refusing, account locked, ...). + let (h, _) = helper_accept(listen, comp, a) + @sys.close_fd(h) + assert_true(settle(comp, a, fn() { d.error != "" })) + assert_eq(d.failures, 0) // not a wrong password + // Typing still lands in the field and stays there. + auth_type(comp, "Secret9") + for i = 0; i < 20; i = i + 1 { + a.bus.pump() + comp.step() + } + assert_eq(d.typed(), 7) + // Shift+digit through the real keymap: '(' on 9. + comp.handle_input(@nested.InputEvent::Modifiers(1U, 0U, 0U, 0U), 1U) + auth_press(comp, 10U) + comp.handle_input(@nested.InputEvent::Modifiers(0U, 0U, 0U, 0U), 1U) + assert_eq(d.typed(), 8) + auth_press(comp, 28U) // Enter: kept until PAM asks + // A second later the helper is started again; this time it asks. + let (h2, hello) = helper_accept(listen, comp, a) + assert_eq(hello, "nandi|t1") + helper_say(h2, "PAM_PROMPT_ECHO_OFF Password: ") + assert_true(settle(comp, a, fn() { d.phase == Checking })) + assert_eq(helper_lines(h2, 1), "Secret9(") + // The pointer works too: a click on Cancel dismisses it. + helper_say(h2, "FAILURE") + assert_true(settle(comp, a, fn() { d.failures == 1 })) + ignore(comp.compose_frame()) + let cancel = comp.auth.unwrap().hits.filter(x => x.1 == Cancel)[0].0 + comp.handle_input(@nested.InputEvent::PointerMove((cancel.x + 4).to_double(), (cancel.y + 4).to_double()), 1U) + comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 2U) + comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 3U) + assert_true(settle(comp, a, fn() { a.answers.length() == 1 })) + @sys.close_fd(h2) + @sys.close_fd(listen) + @sys.unlink(hpath) + comp.close() +} + +///| An xkbcommon 1.6 keymap (xkbcli compile-keymap --layout us), cut down to what typing needs. +let real_keymap : String = + #|xkb_keymap { + #|xkb_keycodes "(unnamed)" { + #| minimum = 8; + #| maximum = 708; + #| = 9; + #| = 10; + #| = 11; + #| = 18; + #| = 19; + #| = 24; + #| = 25; + #| = 26; + #| = 27; + #| = 28; + #| = 38; + #| = 39; + #| = 54; + #| = 58; + #| = 57; + #| = 65; + #| indicator 1 = "Caps Lock"; + #| alias = ; + #|}; + #| + #|xkb_types "(unnamed)" { + #| virtual_modifiers NumLock,Alt,LevelThree,LevelFive,Meta,Super,Hyper,ScrollLock; + #| type "ONE_LEVEL" { + #| modifiers= none; + #| level_name[1]= "Any"; + #| }; + #| type "ALPHABETIC" { + #| modifiers= Shift+Lock; + #| map[Shift]= 2; + #| map[Lock]= 2; + #| level_name[1]= "Base"; + #| level_name[2]= "Caps"; + #| }; + #|}; + #| + #|xkb_compatibility "(unnamed)" { + #| virtual_modifiers NumLock,Alt,LevelThree,LevelFive,Meta,Super,Hyper,ScrollLock; + #| interpret.useModMapMods= AnyLevel; + #| interpret Shift_L+AnyOf(all) { + #| action= SetMods(modifiers=Shift,clearLocks); + #| }; + #| indicator "Caps Lock" { + #| whichModState= locked; + #| modifiers= Lock; + #| }; + #|}; + #| + #|xkb_symbols "(unnamed)" { + #| name[Group1]="English (US)"; + #| + #| key { [ Escape ] }; + #| key { [ 1, exclam ] }; + #| key { [ 2, at ] }; + #| key { [ 9, parenleft ] }; + #| key { [ 0, parenright ] }; + #| key { [ q, Q ] }; + #| key { [ w, W ] }; + #| key { [ e, E ] }; + #| key { [ r, R ] }; + #| key { [ t, T ] }; + #| key { [ a, A ] }; + #| key { [ s, S ] }; + #| key { [ c, C ] }; + #| key { [ n, N ] }; + #| key { [ m, M ] }; + #| key { [ space ] }; + #| modifier_map Shift { , }; + #| modifier_map Lock { }; + #|}; + #| + #|}; diff --git a/compositor/polkit/dialog.mbt b/compositor/polkit/dialog.mbt index dc4c4ac..e2b9909 100644 --- a/compositor/polkit/dialog.mbt +++ b/compositor/polkit/dialog.mbt @@ -91,6 +91,10 @@ pub(all) enum DialogEffect { Answer(FixedArray[Byte]) /// Run the helper again (another user picked, or the last attempt failed). Restart + /// The helper ended before anything was answered (it couldn't run, PAM refused at once, the + /// account is locked): run it again later, not at once, or a helper that fails straight away + /// restarts in a loop and wipes the field under the user's fingers. + RetryLater /// The user dismissed the dialog. Cancelled /// The helper said SUCCESS. @@ -161,6 +165,8 @@ pub(all) struct AuthDialog { mut focus : Focus /// Enter came before PAM's prompt: answer as soon as it does. mut submit_queued : Bool + /// An answer went to the helper in this attempt (a failure then means a wrong answer). + mut answered : Bool /// Face unlock in this attempt (NoFace when no face module spoke). mut face : FaceState /// When face unlock started looking (monotonic ms; the scan animation's clock). @@ -186,6 +192,7 @@ pub fn AuthDialog::new(req : AuthRequest, desc : Description) -> AuthDialog { shake_at: 0UL, focus: Field, submit_queued: false, + answered: false, face: NoFace, face_at: 0UL, } @@ -261,6 +268,7 @@ fn AuthDialog::submit(self : AuthDialog) -> DialogEffect { } else { self.phase = Checking self.error = "" + self.answered = true Answer(self.take_answer()) } Starting | Waiting => { @@ -318,7 +326,20 @@ pub fn AuthDialog::helper(self : AuthDialog, ev : HelperEvent, now : UInt64) -> Nothing } Success => Authorized + Failure if !self.answered => { + // Nothing was asked or answered: not a wrong password. Keep what was typed. + if self.error == "" { + self.error = "The authentication service stopped before asking for a password." + } + self.phase = Starting + self.info = "" + if self.face == Scanning { + self.face = NoFace + } + RetryLater + } Failure => { + self.answered = false self.failures = self.failures + 1 self.shake_at = if now == 0UL { 1UL } else { now } if self.error == "" { @@ -373,6 +394,7 @@ fn AuthDialog::cycle_user(self : AuthDialog, delta : Int) -> DialogEffect { self.face = NoFace self.phase = Starting self.submit_queued = false + self.answered = false Restart } diff --git a/compositor/polkit/helper.mbt b/compositor/polkit/helper.mbt index 07073fa..351a77c 100644 --- a/compositor/polkit/helper.mbt +++ b/compositor/polkit/helper.mbt @@ -101,6 +101,14 @@ pub(all) enum HelperRoute { Program(String) } derive(Eq, Debug) +///| A route, for the log. +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. pub struct HelperSession { mut fd : Int @@ -114,8 +122,11 @@ pub struct HelperSession { pub fn HelperSession::start_any(routes : Array[HelperRoute], user : String, cookie : String) -> HelperSession? { for r in routes { match HelperSession::start(r, user, cookie) { - Some(s) => return Some(s) - None => () + Some(s) => { + @sys.log("chandra: polkit helper for " + user + " via " + route_text(r)) + return Some(s) + } + None => @sys.log("chandra: polkit helper not reachable via " + route_text(r)) } } None