From 4e66072760c5fa3b108fb19cbcc403ef100c944d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Wed, 24 Feb 2016 12:15:51 +0100
Subject: [PATCH 13/15] cache_req: use struct instead of parameter list

---
 src/responder/common/responder_cache_req.c | 91 ++++++++++++++++--------------
 src/responder/common/responder_cache_req.h | 42 ++++++++++++--
 src/responder/ifp/ifpsrv_cmd.c             |  7 ++-
 3 files changed, 92 insertions(+), 48 deletions(-)

diff --git a/src/responder/common/responder_cache_req.c b/src/responder/common/responder_cache_req.c
index 6edecea44f82d6e2f1639202f6519c72dc63887f..3964a5f900e1bcdb01501e45e0c6e88fa0c55d1c 100644
--- a/src/responder/common/responder_cache_req.c
+++ b/src/responder/common/responder_cache_req.c
@@ -74,17 +74,7 @@ struct cache_req_input {
     enum cache_req_type type;
 
     /* Provided input. */
-    struct {
-        struct {
-            const char *input;  /* Original input. */
-            const char *name;   /* Parsed name or UPN. */
-            const char *lookup; /* Converted per domain rules. */
-        } name;
-        uint32_t id;
-        const char *cert;
-        const char *sid;
-        const char **attrs;
-    } data;
+    struct cache_req_data data;
 
     /* Data Provider request type resolved from @type.
      * FIXME: This is currently needed for data provider calls. We should
@@ -106,11 +96,7 @@ struct cache_req_input {
 static errno_t
 cache_req_input_set_data(struct cache_req_input *input,
                          enum cache_req_type type,
-                         uint32_t id,
-                         const char *name,
-                         const char *cert,
-                         const char *sid,
-                         const char **attrs)
+                         struct cache_req_data *data)
 {
     switch (input->type) {
     case CACHE_REQ_USER_BY_NAME:
@@ -120,51 +106,51 @@ cache_req_input_set_data(struct cache_req_input *input,
     case CACHE_REQ_GROUP_BY_FILTER:
     case CACHE_REQ_INITGROUPS:
     case CACHE_REQ_INITGROUPS_BY_UPN:
-        if (name == NULL) {
+        if (data->name.input == NULL) {
             DEBUG(SSSDBG_CRIT_FAILURE, "Bug: name cannot be NULL!\n");
             return ERR_INTERNAL;
         }
 
-        input->data.name.input = talloc_strdup(input, name);
+        input->data.name.input = talloc_strdup(input, data->name.input);
         if (input->data.name.input == NULL) {
             return ENOMEM;
         }
         break;
     case CACHE_REQ_USER_BY_CERT:
-        if (cert == NULL) {
+        if (data->cert == NULL) {
             DEBUG(SSSDBG_CRIT_FAILURE, "Bug: certificate cannot be NULL!\n");
             return ERR_INTERNAL;
         }
 
-        input->data.cert = talloc_strdup(input, cert);
+        input->data.cert = talloc_strdup(input, data->cert);
         if (input->data.cert == NULL) {
             return ENOMEM;
         }
         break;
     case CACHE_REQ_USER_BY_ID:
     case CACHE_REQ_GROUP_BY_ID:
-        if (id == 0) {
+        if (data->id == 0) {
             DEBUG(SSSDBG_CRIT_FAILURE, "Bug: id cannot be 0!\n");
             return ERR_INTERNAL;
         }
 
-        input->data.id = id;
+        input->data.id = data->id;
         break;
     case CACHE_REQ_OBJECT_BY_SID:
-        if (sid == NULL) {
+        if (data->sid == NULL) {
             DEBUG(SSSDBG_CRIT_FAILURE, "Bug: SID cannot be NULL!\n");
             return ERR_INTERNAL;
         }
 
-        input->data.sid = talloc_strdup(input, sid);
+        input->data.sid = talloc_strdup(input, data->sid);
         if (input->data.sid == NULL) {
             return ENOMEM;
         }
         break;
     }
 
-    if (attrs != NULL) {
-        input->data.attrs = dup_string_list(input, attrs);
+    if (data->attrs != NULL) {
+        input->data.attrs = dup_string_list(input, data->attrs);
         if (input->data.attrs == NULL) {
             return ENOMEM;
         }
@@ -260,11 +246,7 @@ struct cache_req_input *
 cache_req_input_create(TALLOC_CTX *mem_ctx,
                        struct resp_ctx *rctx,
                        enum cache_req_type type,
-                       const char *name,
-                       uint32_t id,
-                       const char *cert,
-                       const char *sid,
-                       const char **attrs)
+                       struct cache_req_data *data)
 {
     struct cache_req_input *input;
     errno_t ret;
@@ -280,7 +262,7 @@ cache_req_input_create(TALLOC_CTX *mem_ctx,
     /* It is perfectly fine to just overflow here. */
     input->reqid = rctx->cache_req_num++;
 
-    ret = cache_req_input_set_data(input, type, id, name, cert, sid, attrs);
+    ret = cache_req_input_set_data(input, type, data);
     if (ret != EOK) {
         DEBUG(SSSDBG_CRIT_FAILURE, "Unable to set input data!\n");
         goto fail;
@@ -1405,10 +1387,13 @@ cache_req_user_by_name_send(TALLOC_CTX *mem_ctx,
                             const char *domain,
                             const char *name)
 {
+    struct cache_req_data data;
     struct cache_req_input *input;
 
+    CACHE_REQ_DATA_SET_NAME(&data, name);
+
     input = cache_req_input_create(mem_ctx, rctx, CACHE_REQ_USER_BY_NAME,
-                                   name, 0, NULL, NULL, NULL);
+                                   &data);
     if (input == NULL) {
         return NULL;
     }
@@ -1428,10 +1413,13 @@ cache_req_user_by_id_send(TALLOC_CTX *mem_ctx,
                           const char *domain,
                           uid_t uid)
 {
+    struct cache_req_data data;
     struct cache_req_input *input;
 
+    CACHE_REQ_DATA_SET_ID(&data, uid);
+
     input = cache_req_input_create(mem_ctx, rctx, CACHE_REQ_USER_BY_ID,
-                                   NULL, uid, NULL, NULL, NULL);
+                                   &data);
     if (input == NULL) {
         return NULL;
     }
@@ -1451,10 +1439,13 @@ cache_req_user_by_cert_send(TALLOC_CTX *mem_ctx,
                             const char *domain,
                             const char *pem_cert)
 {
+    struct cache_req_data data;
     struct cache_req_input *input;
 
+    CACHE_REQ_DATA_SET_CERT(&data, pem_cert);
+
     input = cache_req_input_create(mem_ctx, rctx, CACHE_REQ_USER_BY_CERT,
-                                   NULL, 0, pem_cert, NULL, NULL);
+                                   &data);
     if (input == NULL) {
         return NULL;
     }
@@ -1474,10 +1465,13 @@ cache_req_group_by_name_send(TALLOC_CTX *mem_ctx,
                              const char *domain,
                              const char *name)
 {
+    struct cache_req_data data;
     struct cache_req_input *input;
 
+    CACHE_REQ_DATA_SET_NAME(&data, name);
+
     input = cache_req_input_create(mem_ctx, rctx, CACHE_REQ_GROUP_BY_NAME,
-                                   name, 0, NULL, NULL, NULL);
+                                   &data);
     if (input == NULL) {
         return NULL;
     }
@@ -1497,10 +1491,13 @@ cache_req_group_by_id_send(TALLOC_CTX *mem_ctx,
                            const char *domain,
                            gid_t gid)
 {
+    struct cache_req_data data;
     struct cache_req_input *input;
 
+    CACHE_REQ_DATA_SET_ID(&data, gid);
+
     input = cache_req_input_create(mem_ctx, rctx, CACHE_REQ_GROUP_BY_ID,
-                                   NULL, gid, NULL, NULL, NULL);
+                                   &data);
     if (input == NULL) {
         return NULL;
     }
@@ -1520,10 +1517,13 @@ cache_req_initgr_by_name_send(TALLOC_CTX *mem_ctx,
                               const char *domain,
                               const char *name)
 {
+    struct cache_req_data data;
     struct cache_req_input *input;
 
+    CACHE_REQ_DATA_SET_NAME(&data, name);
+
     input = cache_req_input_create(mem_ctx, rctx, CACHE_REQ_INITGROUPS,
-                                   name, 0, NULL, NULL, NULL);
+                                   &data);
     if (input == NULL) {
         return NULL;
     }
@@ -1540,10 +1540,13 @@ cache_req_user_by_filter_send(TALLOC_CTX *mem_ctx,
                               const char *domain,
                               const char *filter)
 {
+    struct cache_req_data data;
     struct cache_req_input *input;
 
+    CACHE_REQ_DATA_SET_NAME(&data, filter);
+
     input = cache_req_input_create(mem_ctx, rctx, CACHE_REQ_USER_BY_FILTER,
-                                   filter, 0, NULL, NULL, NULL);
+                                   &data);
     if (input == NULL) {
         return NULL;
     }
@@ -1559,10 +1562,13 @@ cache_req_group_by_filter_send(TALLOC_CTX *mem_ctx,
                                const char *domain,
                                const char *filter)
 {
+    struct cache_req_data data;
     struct cache_req_input *input;
 
+    CACHE_REQ_DATA_SET_NAME(&data, filter);
+
     input = cache_req_input_create(mem_ctx, rctx, CACHE_REQ_GROUP_BY_FILTER,
-                                   filter, 0, NULL, NULL, NULL);
+                                   &data);
     if (input == NULL) {
         return NULL;
     }
@@ -1582,10 +1588,13 @@ cache_req_object_by_sid_send(TALLOC_CTX *mem_ctx,
                              const char *sid,
                              const char **attrs)
 {
+    struct cache_req_data data;
     struct cache_req_input *input;
 
+    CACHE_REQ_DATA_SET_SID(&data, sid, attrs);
+
     input = cache_req_input_create(mem_ctx, rctx, CACHE_REQ_OBJECT_BY_SID,
-                                   NULL, 0, NULL, sid, attrs);
+                                   &data);
     if (input == NULL) {
         return NULL;
     }
diff --git a/src/responder/common/responder_cache_req.h b/src/responder/common/responder_cache_req.h
index a9388149170a486a509b1d92520d7fe8d21172d3..5b21d6d3e84469cbdb7223d467738ef156b94e5c 100644
--- a/src/responder/common/responder_cache_req.h
+++ b/src/responder/common/responder_cache_req.h
@@ -27,6 +27,42 @@
 #include "responder/common/responder.h"
 #include "responder/common/negcache.h"
 
+struct cache_req_data {
+    struct {
+        const char *input;  /* Original input. */
+        const char *name;   /* Parsed name or UPN. */
+        const char *lookup; /* Converted per domain rules. */
+    } name;
+    uint32_t id;
+    const char *cert;
+    const char *sid;
+    const char **attrs;
+};
+
+#define CACHE_REQ_DATA_INIT(data) \
+    memset((data), '\0', sizeof(struct cache_req_data))
+
+#define CACHE_REQ_DATA_SET_NAME(data, _name) do {         \
+    CACHE_REQ_DATA_INIT(data);                            \
+    (data)->name.input = (_name);                          \
+} while (0)
+
+#define CACHE_REQ_DATA_SET_ID(data, _id) do {             \
+    CACHE_REQ_DATA_INIT(data);                            \
+    (data)->id = (_id);                                    \
+} while (0)
+
+#define CACHE_REQ_DATA_SET_CERT(data, _cert) do {         \
+    CACHE_REQ_DATA_INIT(data);                            \
+    (data)->cert = (_cert);                                \
+} while (0)
+
+#define CACHE_REQ_DATA_SET_SID(data, _sid, _attrs) do {   \
+    CACHE_REQ_DATA_INIT(data);                            \
+    (data)->sid = (_sid);                                  \
+    (data)->attrs = (_attrs);                              \
+} while (0)
+
 enum cache_req_type {
     CACHE_REQ_USER_BY_NAME,
     CACHE_REQ_USER_BY_UPN,
@@ -47,11 +83,7 @@ struct cache_req_input *
 cache_req_input_create(TALLOC_CTX *mem_ctx,
                        struct resp_ctx *rctx,
                        enum cache_req_type type,
-                       const char *name,
-                       uint32_t id,
-                       const char *cert,
-                       const char *sid,
-                       const char **attrs);
+                       struct cache_req_data *data);
 
 /**
  * Currently only SSS_DP_USER and SSS_DP_INITGROUPS are supported.
diff --git a/src/responder/ifp/ifpsrv_cmd.c b/src/responder/ifp/ifpsrv_cmd.c
index 93da572a1b9e077dd19a904a26eab105dad18369..1bdcc7b25cb3f2659b94bc4135702656f05f5988 100644
--- a/src/responder/ifp/ifpsrv_cmd.c
+++ b/src/responder/ifp/ifpsrv_cmd.c
@@ -482,6 +482,7 @@ ifp_user_get_attr_lookup(struct tevent_req *subreq)
     struct ifp_user_get_attr_state *state = NULL;
     struct cache_req_input *input = NULL;
     struct tevent_req *req = NULL;
+    struct cache_req_data data;
     errno_t ret;
 
     req = tevent_req_callback_data(subreq, struct tevent_req);
@@ -494,16 +495,18 @@ ifp_user_get_attr_lookup(struct tevent_req *subreq)
         return;
     }
 
+    CACHE_REQ_DATA_SET_NAME(&data, state->name);
+
     switch (state->search_type) {
     case SSS_DP_USER:
         input = cache_req_input_create(state, state->rctx,
                                        CACHE_REQ_USER_BY_NAME,
-                                       state->name, 0, NULL, NULL, NULL);
+                                       &data);
         break;
     case SSS_DP_INITGROUPS:
         input = cache_req_input_create(state, state->rctx,
                                        CACHE_REQ_INITGROUPS,
-                                       state->name, 0, NULL, NULL, NULL);
+                                       &data);
         break;
     default:
         DEBUG(SSSDBG_CRIT_FAILURE, "Unsupported search type [%d]!\n",
-- 
2.1.0

