From: Mirko Günther <renegart@gmx.de>
Subject: IntegratedLightDM: keep a pending PAM conversation for the same user

The integrated greeter restarts authentication whenever the user is
(re)selected, e.g. twice when the screen gets locked. Restarting cancels
the pending conversation by answering the password prompt with an empty
string, which pam_unix reports as an authentication failure. With
pam_faillock and its default of three attempts, a single wrong password
then locked the account.

If a conversation for the same user is still waiting for its first
answer, keep it and show its prompts again instead of restarting it.

diff --git a/plugins/LightDM/IntegratedLightDM/liblightdm/GreeterPrivate.cpp b/plugins/LightDM/IntegratedLightDM/liblightdm/GreeterPrivate.cpp
index 3464702..5c9d929 100644
--- a/plugins/LightDM/IntegratedLightDM/liblightdm/GreeterPrivate.cpp
+++ b/plugins/LightDM/IntegratedLightDM/liblightdm/GreeterPrivate.cpp
@@ -66,6 +66,14 @@ public:
 
     void start(QString username)
     {
+        // Cancelling a conversation that waits for an answer counts as a
+        // failed attempt (e.g. for pam_faillock), so keep it for the same user.
+        if (pamHandle != nullptr && username == pamUser && !pamResponded) {
+            for (const auto &prompt : pendingPrompts)
+                Q_EMIT greeter->showPrompt(prompt.first, prompt.second);
+            return;
+        }
+
         // Clear out any existing PAM interactions first
         cancelPam();
         if (pamHandle != nullptr) {
@@ -86,6 +94,9 @@ public:
 
         if (pam_start("lightdm", username.toUtf8(), &conversation, &pamHandle) == PAM_SUCCESS) {
             appData->handle = pamHandle;
+            pamUser = username;
+            pamResponded = false;
+            pendingPrompts.clear();
             futureWatcher.setFuture(QtConcurrent::mapped(QList<pam_handle*>() << pamHandle, authenticateWithPam));
         } else {
             delete appData;
@@ -200,6 +211,8 @@ public Q_SLOTS:
     {
         if (!futures.isEmpty()) {
             futures.dequeue().reportFinished(&response);
+            pamResponded = true;
+            pendingPrompts.clear();
             return true;
         } else {
             return false;
@@ -260,6 +273,7 @@ private Q_SLOTS:
         }
 
         futures.enqueue(future);
+        pendingPrompts.append(qMakePair(text, type));
         Q_EMIT greeter->showPrompt(text, type);
     }
 
@@ -269,6 +283,9 @@ private:
     pam_handle* pamHandle;
     QFutureWatcher<int> futureWatcher;
     QQueue<ResponseFuture> futures;
+    QString pamUser;
+    bool pamResponded = false;
+    QList<QPair<QString, QLightDM::Greeter::PromptType>> pendingPrompts;
 };
 
 GreeterPrivate::GreeterPrivate(Greeter* parent)
