From ea6a7557bb380ab42a2ae73ad08d8279e9c88152 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Fri, 18 Jul 2014 11:21:28 +0200
Subject: [PATCH 2/2] ad: update membership after SIDs are resolved

Resolves:
https://fedorahosted.org/sssd/ticket/2385
---
 src/providers/ldap/sdap_async_initgroups_ad.c | 87 +++++++++++++++++++--------
 1 file changed, 62 insertions(+), 25 deletions(-)

diff --git a/src/providers/ldap/sdap_async_initgroups_ad.c b/src/providers/ldap/sdap_async_initgroups_ad.c
index 4c5f40bdf2c6235fdeb1cb88261ec17dd06226a1..dee5e9a33741e6c74d0afe896c02d0c5fe4fe8bf 100644
--- a/src/providers/ldap/sdap_async_initgroups_ad.c
+++ b/src/providers/ldap/sdap_async_initgroups_ad.c
@@ -980,6 +980,8 @@ struct sdap_ad_tokengroups_initgr_posix_state {
     const char *username;
 
     struct sdap_id_op *op;
+    char **missing_sids;
+    size_t num_missing_sids;
 };
 
 static void
@@ -1106,20 +1108,21 @@ sdap_ad_tokengroups_initgr_posix_sids_connect_done(struct tevent_req *subreq)
     return;
 }
 
-static void
-sdap_ad_tokengroups_initgr_posix_tg_done(struct tevent_req *subreq)
+static errno_t
+sdap_ad_tokengroups_update_posix_members(TALLOC_CTX *mem_ctx,
+                         struct sdap_ad_tokengroups_initgr_posix_state *state,
+                         size_t num_sids,
+                         char **sids,
+                         size_t *_num_missing,
+                         char ***_missing)
 {
     TALLOC_CTX *tmp_ctx = NULL;
-    struct sdap_ad_tokengroups_initgr_posix_state *state = NULL;
-    struct tevent_req *req = NULL;
     struct sss_domain_info *domain = NULL;
     struct ldb_message *msg = NULL;
     const char *attrs[] = {SYSDB_NAME, SYSDB_POSIX, NULL};
     const char *is_posix = NULL;
     const char *name = NULL;
     char *sid = NULL;
-    char **sids = NULL;
-    size_t num_sids = 0;
     char **valid_groups = NULL;
     size_t num_valid_groups;
     char **missing_sids = NULL;
@@ -1134,18 +1137,6 @@ sdap_ad_tokengroups_initgr_posix_tg_done(struct tevent_req *subreq)
         goto done;
     }
 
-    req = tevent_req_callback_data(subreq, struct tevent_req);
-    state = tevent_req_data(req,
-                            struct sdap_ad_tokengroups_initgr_posix_state);
-
-    ret = sdap_get_ad_tokengroups_recv(state, subreq, &num_sids, &sids);
-    talloc_zfree(subreq);
-    if (ret != EOK) {
-        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to acquire tokengroups [%d]: %s\n",
-                                    ret, strerror(ret));
-        goto done;
-    }
-
     num_valid_groups = 0;
     valid_groups = talloc_zero_array(tmp_ctx, char*, num_sids + 1);
     if (valid_groups == NULL) {
@@ -1198,7 +1189,7 @@ sdap_ad_tokengroups_initgr_posix_tg_done(struct tevent_req *subreq)
                 goto done;
             }
             num_valid_groups++;
-        } else if (ret == ENOENT) {
+        } else if (ret == ENOENT && _missing != NULL) {
             /* we need to download this group */
             missing_sids[num_missing_sids] = talloc_steal(missing_sids, sid);
             num_missing_sids++;
@@ -1206,8 +1197,8 @@ sdap_ad_tokengroups_initgr_posix_tg_done(struct tevent_req *subreq)
             DEBUG(SSSDBG_TRACE_FUNC, "Missing SID %s will be downloaded\n",
                                       sid);
         } else {
-            DEBUG(SSSDBG_MINOR_FAILURE, "Could not look up group in sysdb: "
-                                         "[%s]\n", strerror(ret));
+            DEBUG(SSSDBG_MINOR_FAILURE, "Could not look up SID %s in sysdb: "
+                                         "[%s]\n", sid, strerror(ret));
             goto done;
         }
     }
@@ -1225,12 +1216,52 @@ sdap_ad_tokengroups_initgr_posix_tg_done(struct tevent_req *subreq)
         goto done;
     }
 
+    /* return list of missing groups */
+    if (_missing != NULL) {
+        *_missing = talloc_steal(mem_ctx, missing_sids);
+        *_num_missing = num_missing_sids;
+    }
+
+    ret = EOK;
+
+done:
+    talloc_free(tmp_ctx);
+    return ret;
+}
+
+static void
+sdap_ad_tokengroups_initgr_posix_tg_done(struct tevent_req *subreq)
+{
+    struct sdap_ad_tokengroups_initgr_posix_state *state = NULL;
+    struct tevent_req *req = NULL;
+    char **sids = NULL;
+    size_t num_sids = 0;
+    errno_t ret;
+
+    req = tevent_req_callback_data(subreq, struct tevent_req);
+    state = tevent_req_data(req, struct sdap_ad_tokengroups_initgr_posix_state);
+
+    ret = sdap_get_ad_tokengroups_recv(state, subreq, &num_sids, &sids);
+    talloc_zfree(subreq);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to acquire tokengroups [%d]: %s\n",
+                                    ret, strerror(ret));
+        goto done;
+    }
+
+    ret = sdap_ad_tokengroups_update_posix_members(state, state,
+                                                   num_sids, sids,
+                                                   &state->num_missing_sids,
+                                                   &state->missing_sids);
+    if (ret != EOK) {
+        goto done;
+    }
+
     /* download missing SIDs */
-    missing_sids = talloc_steal(state, missing_sids);
     subreq = sdap_ad_resolve_sids_send(state, state->ev, state->id_ctx,
                                        state->conn,
                                        state->opts, state->domain,
-                                       missing_sids);
+                                       state->missing_sids);
     if (subreq == NULL) {
         ret = ENOMEM;
         goto done;
@@ -1242,7 +1273,6 @@ sdap_ad_tokengroups_initgr_posix_tg_done(struct tevent_req *subreq)
     return;
 
 done:
-    talloc_free(tmp_ctx);
     if (ret != EOK) {
         tevent_req_error(req, ret);
         return;
@@ -1254,19 +1284,26 @@ done:
 static void
 sdap_ad_tokengroups_initgr_posix_sids_done(struct tevent_req *subreq)
 {
+    struct sdap_ad_tokengroups_initgr_posix_state *state = NULL;
     struct tevent_req *req = NULL;
     errno_t ret;
 
     req = tevent_req_callback_data(subreq, struct tevent_req);
+    state = tevent_req_data(req, struct sdap_ad_tokengroups_initgr_posix_state);
 
     ret = sdap_ad_resolve_sids_recv(subreq);
     talloc_zfree(subreq);
     if (ret != EOK) {
         DEBUG(SSSDBG_CRIT_FAILURE, "Unable to resolve missing SIDs "
-                                    "[%d]: %s\n", ret, strerror(ret));
+                                   "[%d]: %s\n", ret, strerror(ret));
         goto done;
     }
 
+    ret = sdap_ad_tokengroups_update_posix_members(state, state,
+                                                   state->num_missing_sids,
+                                                   state->missing_sids,
+                                                   NULL, NULL);
+
 done:
     if (ret != EOK) {
         tevent_req_error(req, ret);
-- 
1.7.11.7

