>From de54ed10c63f058643c853edf024251a24022e32 Mon Sep 17 00:00:00 2001
From: Pavel Reichl <preichl@redhat.com>
Date: Thu, 9 Apr 2015 11:33:28 -0400
Subject: [PATCH 2/5] PAM: refac. pam_reply: extract func. produce_packet

Extracting produce_packet() from pam_reply() makes it shorter and removes
6 local variables which makes it more comprehensible.

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

diff --git a/src/responder/pam/pamsrv_cmd.c b/src/responder/pam/pamsrv_cmd.c
index 058b87ebecb38001ebe09a8dd72de93a4a84b8cd..7054bfbafc274e234d3eeb29cae3cc67f9854593 100644
--- a/src/responder/pam/pamsrv_cmd.c
+++ b/src/responder/pam/pamsrv_cmd.c
@@ -567,6 +567,72 @@ done:
     return ret;
 }
 
+static errno_t produce_packet(TALLOC_CTX *mem_ctx,
+                              struct pam_data *pd,
+                              enum sss_cli_command cmd,
+                              struct sss_packet **_out)
+{
+    errno_t ret;
+    uint8_t *body;
+    size_t blen;
+    int32_t resp_c;
+    int32_t resp_size;
+    struct response_data *resp;
+    int p;
+    struct sss_packet *out;
+
+    ret = sss_packet_new(mem_ctx, 0, cmd, &out);
+    if (ret != EOK) {
+        goto done;
+    }
+
+    resp_c = 0;
+    resp_size = 0;
+    resp = pd->resp_list;
+    while(resp != NULL) {
+        if (!resp->do_not_send_to_client) {
+            resp_c++;
+            resp_size += resp->len;
+        }
+        resp = resp->next;
+    }
+
+    ret = sss_packet_grow(out, sizeof(int32_t) + sizeof(int32_t) +
+                          resp_c * 2 * sizeof(int32_t) + resp_size);
+    if (ret != EOK) {
+        goto done;
+    }
+
+    sss_packet_get_body(out, &body, &blen);
+    DEBUG(SSSDBG_FUNC_DATA, "blen: %zu\n", blen);
+    p = 0;
+
+    memcpy(&body[p], &pd->pam_status, sizeof(int32_t));
+    p += sizeof(int32_t);
+
+    memcpy(&body[p], &resp_c, sizeof(int32_t));
+    p += sizeof(int32_t);
+
+    resp = pd->resp_list;
+    while(resp != NULL) {
+        if (!resp->do_not_send_to_client) {
+            memcpy(&body[p], &resp->type, sizeof(int32_t));
+            p += sizeof(int32_t);
+            memcpy(&body[p], &resp->len, sizeof(int32_t));
+            p += sizeof(int32_t);
+            memcpy(&body[p], resp->data, resp->len);
+            p += resp->len;
+        }
+
+        resp = resp->next;
+    }
+
+    *_out = out;
+    ret = EOK;
+
+done:
+    return ret;
+}
 
 static int pam_forwarder(struct cli_ctx *cctx, int pam_cmd);
 static void pam_handle_cached_login(struct pam_auth_req *preq, int ret,
@@ -575,13 +641,7 @@ static void pam_handle_cached_login(struct pam_auth_req *preq, int ret,
 static void pam_reply(struct pam_auth_req *preq)
 {
     struct cli_ctx *cctx;
-    uint8_t *body;
-    size_t blen;
     int ret;
-    int32_t resp_c;
-    int32_t resp_size;
-    struct response_data *resp;
-    int p;
     struct timeval tv;
     struct tevent_timer *te;
     struct pam_data *pd;
@@ -703,12 +763,6 @@ static void pam_reply(struct pam_auth_req *preq)
         goto done;
     }
 
-    ret = sss_packet_new(cctx->creq, 0, sss_packet_get_cmd(cctx->creq->in),
-                         &cctx->creq->out);
-    if (ret != EOK) {
-        goto done;
-    }
-
     ret = filter_responses(pctx->rctx->cdb, pd->resp_list);
     if (ret != EOK) {
         DEBUG(SSSDBG_CRIT_FAILURE, "filter_responses failed, not fatal.\n");
@@ -723,49 +777,12 @@ static void pam_reply(struct pam_auth_req *preq)
         }
     }
 
-    resp_c = 0;
-    resp_size = 0;
-    resp = pd->resp_list;
-    while(resp != NULL) {
-        if (!resp->do_not_send_to_client) {
-            resp_c++;
-            resp_size += resp->len;
-        }
-        resp = resp->next;
-    }
-
-    ret = sss_packet_grow(cctx->creq->out, sizeof(int32_t) +
-                                           sizeof(int32_t) +
-                                           resp_c * 2* sizeof(int32_t) +
-                                           resp_size);
+    ret = produce_packet(cctx->creq, pd, sss_packet_get_cmd(cctx->creq->in),
+                         &cctx->creq->out);
     if (ret != EOK) {
         goto done;
     }
 
-    sss_packet_get_body(cctx->creq->out, &body, &blen);
-    DEBUG(SSSDBG_FUNC_DATA, "blen: %zu\n", blen);
-    p = 0;
-
-    memcpy(&body[p], &pd->pam_status, sizeof(int32_t));
-    p += sizeof(int32_t);
-
-    memcpy(&body[p], &resp_c, sizeof(int32_t));
-    p += sizeof(int32_t);
-
-    resp = pd->resp_list;
-    while(resp != NULL) {
-        if (!resp->do_not_send_to_client) {
-            memcpy(&body[p], &resp->type, sizeof(int32_t));
-            p += sizeof(int32_t);
-            memcpy(&body[p], &resp->len, sizeof(int32_t));
-            p += sizeof(int32_t);
-            memcpy(&body[p], resp->data, resp->len);
-            p += resp->len;
-        }
-
-        resp = resp->next;
-    }
-
 done:
     sss_cmd_done(cctx, preq);
 }
-- 
2.1.0

