>From d2e3a6b58748c4147b84c450654daa1c4654b2ee Mon Sep 17 00:00:00 2001
From: Pavel Reichl <preichl@redhat.com>
Date: Thu, 9 Apr 2015 13:16:18 -0400
Subject: [PATCH 5/5] PAM: pam_handle_cached_login - remove duplicate code

Resolves:
https://fedorahosted.org/sssd/ticket/2615
---
 src/responder/pam/pamsrv_cmd.c | 66 +++++++++++++++++++-----------------------
 1 file changed, 30 insertions(+), 36 deletions(-)

diff --git a/src/responder/pam/pamsrv_cmd.c b/src/responder/pam/pamsrv_cmd.c
index fac9a4b08c9248bc3884c2b4d0484c70702f5e40..bd0c91c9a4314eb9bddffae29bca8eec293c5980 100644
--- a/src/responder/pam/pamsrv_cmd.c
+++ b/src/responder/pam/pamsrv_cmd.c
@@ -809,54 +809,48 @@ done:
     sss_cmd_done(cctx, preq);
 }
 
-static void pam_handle_cached_login(struct pam_auth_req *preq, int ret,
-                                    time_t expire_date, time_t delayed_until)
+static void add_cached_login_response(struct pam_data *pd,
+                                      uint32_t resp_type,
+                                      time_t time_val)
 {
-    uint32_t resp_type;
     size_t resp_len;
     uint8_t *resp;
     int64_t dummy;
+    int ret;
 
+    resp_len = sizeof(uint32_t) + sizeof(int64_t);
+    resp = talloc_size(pd, resp_len);
+    if (resp == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE,
+              "talloc_size failed, cannot prepare user info.\n");
+    } else {
+        memcpy(resp, &resp_type, sizeof(uint32_t));
+        dummy = (int64_t) time_val;
+        memcpy(resp+sizeof(uint32_t), &dummy, sizeof(int64_t));
+        ret = pam_add_response(pd, SSS_PAM_USER_INFO, resp_len,
+                               (const uint8_t *) resp);
+        if (ret != EOK) {
+            DEBUG(SSSDBG_CRIT_FAILURE, "pam_add_response failed.\n");
+        }
+    }
+}
+
+static void pam_handle_cached_login(struct pam_auth_req *preq, int ret,
+                                    time_t expire_date, time_t delayed_until)
+{
     preq->pd->pam_status = cached_login_pam_status(ret);
 
     switch (preq->pd->pam_status) {
         case PAM_SUCCESS:
-            resp_type = SSS_PAM_USER_INFO_OFFLINE_AUTH;
-            resp_len = sizeof(uint32_t) + sizeof(int64_t);
-            resp = talloc_size(preq->pd, resp_len);
-            if (resp == NULL) {
-                DEBUG(SSSDBG_CRIT_FAILURE,
-                      "talloc_size failed, cannot prepare user info.\n");
-            } else {
-                memcpy(resp, &resp_type, sizeof(uint32_t));
-                dummy = (int64_t) expire_date;
-                memcpy(resp+sizeof(uint32_t), &dummy, sizeof(int64_t));
-                ret = pam_add_response(preq->pd, SSS_PAM_USER_INFO, resp_len,
-                                       (const uint8_t *) resp);
-                if (ret != EOK) {
-                    DEBUG(SSSDBG_CRIT_FAILURE, "pam_add_response failed.\n");
-                }
-            }
+            add_cached_login_response(preq->pd,
+                                      SSS_PAM_USER_INFO_OFFLINE_AUTH,
+                                      expire_date);
             break;
         case PAM_PERM_DENIED:
             if (delayed_until >= 0) {
-                resp_type = SSS_PAM_USER_INFO_OFFLINE_AUTH_DELAYED;
-                resp_len = sizeof(uint32_t) + sizeof(int64_t);
-                resp = talloc_size(preq->pd, resp_len);
-                if (resp == NULL) {
-                    DEBUG(SSSDBG_CRIT_FAILURE,
-                          "talloc_size failed, cannot prepare user info.\n");
-                } else {
-                    memcpy(resp, &resp_type, sizeof(uint32_t));
-                    dummy = (int64_t) delayed_until;
-                    memcpy(resp+sizeof(uint32_t), &dummy, sizeof(int64_t));
-                    ret = pam_add_response(preq->pd, SSS_PAM_USER_INFO, resp_len,
-                                           (const uint8_t *) resp);
-                    if (ret != EOK) {
-                        DEBUG(SSSDBG_CRIT_FAILURE,
-                              "pam_add_response failed.\n");
-                    }
-                }
+                add_cached_login_response(preq->pd,
+                                          SSS_PAM_USER_INFO_OFFLINE_AUTH_DELAYED,
+                                          delayed_until);
             }
             break;
         default:
-- 
2.1.0

