From 1ccdf84ace58141b60e7924e57bbb0f0193c7cfa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Mon, 18 Feb 2013 10:28:07 +0100
Subject: [PATCH 2/2] refactor nested group processing: replace old code

https://fedorahosted.org/sssd/ticket/1784
---
 Makefile.am                             |   1 +
 src/providers/ldap/sdap_async_groups.c  | 130 ++++++++++++--------------------
 src/providers/ldap/sdap_async_private.h |  17 +++++
 3 files changed, 66 insertions(+), 82 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 64708216103e48facf780f246de5668c8e0b26a7..116f1ec390836e7218b35770c436c3f68fc98626 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1264,6 +1264,7 @@ libsss_ldap_common_la_SOURCES = \
     src/providers/ldap/sdap_async.c \
     src/providers/ldap/sdap_async_users.c \
     src/providers/ldap/sdap_async_groups.c \
+    src/providers/ldap/sdap_async_nested_groups.c \
     src/providers/ldap/sdap_async_groups_ad.c \
     src/providers/ldap/sdap_async_initgroups.c \
     src/providers/ldap/sdap_async_initgroups_ad.c \
diff --git a/src/providers/ldap/sdap_async_groups.c b/src/providers/ldap/sdap_async_groups.c
index 76c077d83be4a0ca0a5da3cf35889dbcf0c7e87d..ac5bb99f021206adeecc511f68e9efc12434b463 100644
--- a/src/providers/ldap/sdap_async_groups.c
+++ b/src/providers/ldap/sdap_async_groups.c
@@ -1642,56 +1642,63 @@ static void sdap_get_groups_process(struct tevent_req *subreq)
                 && (dp_opt_get_int(state->opts->basic, SDAP_NESTING_LEVEL) != 0)
                 && !dp_opt_get_bool(state->opts->basic, SDAP_AD_MATCHING_RULE_GROUPS)) {
 
-            /* Prepare hashes for nested user processing */
-            ret = sss_hash_create(state, 32, &state->user_hash);
-            if (ret != EOK) {
-                tevent_req_error(req, ret);
-                return;
-            }
-
-            ret = sss_hash_create(state, 32, &state->group_hash);
-            if (ret != EOK) {
-                tevent_req_error(req, ret);
-                return;
-            }
+            if (false) {
+                /* Prepare hashes for nested user processing */
+                ret = sss_hash_create(state, 32, &state->user_hash);
+                if (ret != EOK) {
+                    tevent_req_error(req, ret);
+                    return;
+                }
 
-            /*
-             * If any search base contains filter, disable dereference.
-             */
-            enable_deref = true;
-            for (i = 0; state->opts->user_search_bases[i] != NULL; i++) {
-                if (state->opts->user_search_bases[i]->filter != NULL) {
-                    DEBUG(SSSDBG_TRACE_FUNC,
-                          ("User search base contains filter, "
-                           "dereference will be disabled\n"));
-                    enable_deref = false;
-                    break;
+                ret = sss_hash_create(state, 32, &state->group_hash);
+                if (ret != EOK) {
+                    tevent_req_error(req, ret);
+                    return;
                 }
-            }
 
-            if (enable_deref) {
-                for (i = 0; state->opts->group_search_bases[i] != NULL; i++) {
-                    if (state->opts->group_search_bases[i]->filter != NULL) {
+                /*
+                 * If any search base contains filter, disable dereference.
+                 */
+                enable_deref = true;
+                for (i = 0; state->opts->user_search_bases[i] != NULL; i++) {
+                    if (state->opts->user_search_bases[i]->filter != NULL) {
                         DEBUG(SSSDBG_TRACE_FUNC,
-                              ("Group search base contains filter, "
+                              ("User search base contains filter, "
                                "dereference will be disabled\n"));
                         enable_deref = false;
                         break;
                     }
                 }
+
+                if (enable_deref) {
+                    for (i = 0; state->opts->group_search_bases[i] != NULL; i++) {
+                        if (state->opts->group_search_bases[i]->filter != NULL) {
+                            DEBUG(SSSDBG_TRACE_FUNC,
+                                  ("Group search base contains filter, "
+                                   "dereference will be disabled\n"));
+                            enable_deref = false;
+                            break;
+                        }
+                    }
+                }
+
+                subreq = sdap_nested_group_process_send(state,
+                                                        state->ev,
+                                                        state->dom,
+                                                        state->sysdb,
+                                                        state->groups[0],
+                                                        state->user_hash,
+                                                        state->group_hash,
+                                                        state->opts,
+                                                        state->sh,
+                                                        enable_deref,
+                                                        0);
             }
 
-            subreq = sdap_nested_group_process_send(state,
-                                                    state->ev,
-                                                    state->dom,
-                                                    state->sysdb,
-                                                    state->groups[0],
-                                                    state->user_hash,
-                                                    state->group_hash,
-                                                    state->opts,
-                                                    state->sh,
-                                                    enable_deref,
-                                                    0);
+            subreq = sdap_nested_group_send(state, state->ev, state->dom,
+                                                            state->opts, state->sh,
+                                                            state->groups[0]);
+
             if (!subreq) {
                 tevent_req_error(req, EIO);
                 return;
@@ -1956,11 +1963,8 @@ int sdap_get_groups_recv(struct tevent_req *req,
 static void sdap_nested_done(struct tevent_req *subreq)
 {
     errno_t ret, tret;
-    int hret;
-    unsigned long i;
     unsigned long user_count;
     unsigned long group_count;
-    hash_value_t *values;
     bool in_transaction = false;
     struct sysdb_attrs **users = NULL;
     struct sysdb_attrs **groups = NULL;
@@ -1970,7 +1974,8 @@ static void sdap_nested_done(struct tevent_req *subreq)
     struct sdap_get_groups_state *state = tevent_req_data(req,
                                             struct sdap_get_groups_state);
 
-    ret = sdap_nested_group_process_recv(subreq);
+    ret = sdap_nested_group_recv(state, subreq, &user_count, &users,
+                                 &group_count, &groups);
     talloc_zfree(subreq);
     if (ret != EOK) {
         DEBUG(1, ("Nested group processing failed: [%d][%s]\n",
@@ -1978,45 +1983,6 @@ static void sdap_nested_done(struct tevent_req *subreq)
         goto fail;
     }
 
-    hret = hash_values(state->user_hash, &user_count, &values);
-    if (hret != HASH_SUCCESS) {
-        ret = EIO;
-        goto fail;
-    }
-
-    if (user_count) {
-        users = talloc_array(state, struct sysdb_attrs *, user_count);
-        if (!users) {
-            talloc_free(values);
-            ret = ENOMEM;
-            goto fail;
-        }
-
-        for (i = 0; i < user_count; i++) {
-            users[i] = talloc_get_type(values[i].ptr, struct sysdb_attrs);
-        }
-        talloc_zfree(values);
-    }
-
-    /* Users are all retrieved. Now retrieve groups */
-    hret = hash_values(state->group_hash, &group_count, &values);
-    if (hret != HASH_SUCCESS) {
-        ret = EIO;
-        goto fail;
-    }
-
-    groups = talloc_array(state, struct sysdb_attrs *, group_count);
-    if (!groups) {
-        talloc_free(values);
-        ret = ENOMEM;
-        goto fail;
-    }
-
-    for (i = 0; i < group_count; i++) {
-        groups[i] = talloc_get_type(values[i].ptr, struct sysdb_attrs);
-    }
-    talloc_zfree(values);
-
     /* Save all of the users first so that they are in
      * place for the groups to add them.
      */
diff --git a/src/providers/ldap/sdap_async_private.h b/src/providers/ldap/sdap_async_private.h
index 156255b97a67973bb0bba690d18d934a9d18a026..ea0616dff4450598d242d9848d6e3540e54274c6 100644
--- a/src/providers/ldap/sdap_async_private.h
+++ b/src/providers/ldap/sdap_async_private.h
@@ -120,4 +120,21 @@ errno_t get_sysdb_grouplist(TALLOC_CTX *mem_ctx,
                             struct sss_domain_info *domain,
                             const char *name,
                             char ***grouplist);
+
+/* from sdap_async_nested_groups.c */
+
+struct tevent_req *sdap_nested_group_send(TALLOC_CTX *mem_ctx,
+                                          struct tevent_context *ev,
+                                          struct sss_domain_info *domain,
+                                          struct sdap_options *opts,
+                                          struct sdap_handle *sh,
+                                          struct sysdb_attrs *group);
+
+errno_t sdap_nested_group_recv(TALLOC_CTX *mem_ctx,
+                               struct tevent_req *req,
+                               unsigned long *_num_users,
+                               struct sysdb_attrs ***_users,
+                               unsigned long *_num_groups,
+                               struct sysdb_attrs ***_groups);
+
 #endif /* _SDAP_ASYNC_PRIVATE_H_ */
-- 
1.7.11.7

