Blame SOURCES/0026-PAM-Also-cache-SSS_PAM_PREAUTH.patch

841ac7
From 0a637fff4fe575916bdae0eb17b7c36e8427308a Mon Sep 17 00:00:00 2001
841ac7
From: Jakub Hrozek <jhrozek@redhat.com>
841ac7
Date: Wed, 17 Apr 2019 15:07:43 +0200
841ac7
Subject: [PATCH] PAM: Also cache SSS_PAM_PREAUTH
841ac7
841ac7
Related: https://pagure.io/SSSD/sssd/issue/3960
841ac7
841ac7
Even if cached_auth_timeout was set, the pam responder would still
841ac7
forward the preauthentication requests to the back end. This could
841ac7
trigger unwanted traffic towards the KDCs.
841ac7
841ac7
Reviewed-by: Sumit Bose <sbose@redhat.com>
841ac7
(cherry picked from commit c911562d1bea8ae44e45e564c9df5df43d87b035)
841ac7
---
841ac7
 src/man/sssd.conf.5.xml        |  4 +++-
841ac7
 src/responder/pam/pamsrv_cmd.c | 40 +++++++++++++++-------------------
841ac7
 2 files changed, 21 insertions(+), 23 deletions(-)
841ac7
841ac7
diff --git a/src/man/sssd.conf.5.xml b/src/man/sssd.conf.5.xml
841ac7
index 274809e24..1ab7af00b 100644
841ac7
--- a/src/man/sssd.conf.5.xml
841ac7
+++ b/src/man/sssd.conf.5.xml
841ac7
@@ -2960,7 +2960,9 @@ subdomain_inherit = ldap_purge_cache_timeout
841ac7
                             Specifies time in seconds since last successful
841ac7
                             online authentication for which user will be
841ac7
                             authenticated using cached credentials while
841ac7
-                            SSSD is in the online mode.
841ac7
+                            SSSD is in the online mode. If the credentials
841ac7
+                            are incorrect, SSSD falls back to online
841ac7
+                            authentication.
841ac7
                         </para>
841ac7
                         <para>
841ac7
                             This option's value is inherited by all trusted
841ac7
diff --git a/src/responder/pam/pamsrv_cmd.c b/src/responder/pam/pamsrv_cmd.c
841ac7
index 6b2dc5bdc..00302be75 100644
841ac7
--- a/src/responder/pam/pamsrv_cmd.c
841ac7
+++ b/src/responder/pam/pamsrv_cmd.c
841ac7
@@ -803,8 +803,9 @@ static void pam_reply(struct pam_auth_req *preq)
841ac7
         pam_verbosity = DEFAULT_PAM_VERBOSITY;
841ac7
     }
841ac7
 
841ac7
-    DEBUG(SSSDBG_FUNC_DATA,
841ac7
-          "pam_reply called with result [%d]: %s.\n",
841ac7
+    DEBUG(SSSDBG_TRACE_ALL,
841ac7
+          "pam_reply initially called with result [%d]: %s. "
841ac7
+          "this result might be changed during processing\n",
841ac7
           pd->pam_status, pam_strerror(NULL, pd->pam_status));
841ac7
 
841ac7
     if (pd->cmd == SSS_PAM_AUTHENTICATE
841ac7
@@ -886,6 +887,7 @@ static void pam_reply(struct pam_auth_req *preq)
841ac7
             break;
841ac7
 /* TODO: we need the pam session cookie here to make sure that cached
841ac7
  * authentication was successful */
841ac7
+        case SSS_PAM_PREAUTH:
841ac7
         case SSS_PAM_SETCRED:
841ac7
         case SSS_PAM_ACCT_MGMT:
841ac7
         case SSS_PAM_OPEN_SESSION:
841ac7
@@ -1067,6 +1069,8 @@ static void pam_reply(struct pam_auth_req *preq)
841ac7
     }
841ac7
 
841ac7
 done:
841ac7
+    DEBUG(SSSDBG_FUNC_DATA, "Returning [%d]: %s to the client\n",
841ac7
+          pd->pam_status, pam_strerror(NULL, pd->pam_status));
841ac7
     sss_cmd_done(cctx, preq);
841ac7
 }
841ac7
 
841ac7
@@ -1949,21 +1953,6 @@ done:
841ac7
     return ret;
841ac7
 }
841ac7
 
841ac7
-static bool pam_is_cmd_cachable(int cmd)
841ac7
-{
841ac7
-    bool is_cachable;
841ac7
-
841ac7
-    switch(cmd) {
841ac7
-    case SSS_PAM_AUTHENTICATE:
841ac7
-        is_cachable = true;
841ac7
-        break;
841ac7
-    default:
841ac7
-        is_cachable = false;
841ac7
-    }
841ac7
-
841ac7
-    return is_cachable;
841ac7
-}
841ac7
-
841ac7
 static bool pam_is_authtok_cachable(struct sss_auth_token *authtok)
841ac7
 {
841ac7
     enum sss_authtok_type type;
841ac7
@@ -1988,11 +1977,18 @@ static bool pam_can_user_cache_auth(struct sss_domain_info *domain,
841ac7
     errno_t ret;
841ac7
     bool result = false;
841ac7
 
841ac7
-    if (!cached_auth_failed /* don't try cached auth again */
841ac7
-            && domain->cache_credentials
841ac7
-            && domain->cached_auth_timeout > 0
841ac7
-            && pam_is_authtok_cachable(authtok)
841ac7
-            && pam_is_cmd_cachable(pam_cmd)) {
841ac7
+    if (cached_auth_failed) {
841ac7
+        /* Do not retry indefinitely */
841ac7
+        return false;
841ac7
+    }
841ac7
+
841ac7
+    if (!domain->cache_credentials || domain->cached_auth_timeout <= 0) {
841ac7
+        return false;
841ac7
+    }
841ac7
+
841ac7
+    if (pam_cmd == SSS_PAM_PREAUTH
841ac7
+        || (pam_cmd == SSS_PAM_AUTHENTICATE
841ac7
+            && pam_is_authtok_cachable(authtok))) {
841ac7
 
841ac7
         ret = pam_is_last_online_login_fresh(domain, user,
841ac7
                                              domain->cached_auth_timeout,
841ac7
-- 
841ac7
2.19.2
841ac7