Blame SOURCES/0043-ldap-use-member-DN-to-create-ghost-user-hash-table.patch

bcb322
From 4d6be3c36169c954c4d61399607fde229902cb07 Mon Sep 17 00:00:00 2001
bcb322
From: Sumit Bose <sbose@redhat.com>
bcb322
Date: Wed, 26 Aug 2020 15:40:53 +0200
bcb322
Subject: [PATCH] ldap: use member DN to create ghost user hash table
bcb322
bcb322
---
bcb322
 src/db/sysdb.h                                |  1 +
bcb322
 src/providers/ldap/sdap.c                     | 10 ++++++++++
bcb322
 src/providers/ldap/sdap_async_groups.c        | 17 +++++++++++++++-
bcb322
 src/providers/ldap/sdap_async_nested_groups.c | 20 +++++++++++++++++--
bcb322
 4 files changed, 45 insertions(+), 3 deletions(-)
bcb322
bcb322
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
bcb322
index a2bc8ed3b..679763bad 100644
bcb322
--- a/src/db/sysdb.h
bcb322
+++ b/src/db/sysdb.h
bcb322
@@ -129,6 +129,7 @@
bcb322
 #define SYSDB_UPN "userPrincipalName"
bcb322
 #define SYSDB_CANONICAL_UPN "canonicalUserPrincipalName"
bcb322
 #define SYSDB_CCACHE_FILE "ccacheFile"
bcb322
+#define SYSDB_DN_FOR_MEMBER_HASH_TABLE "dnForMemberHashTable"
bcb322
 
bcb322
 #define SYSDB_ORIG_DN "originalDN"
bcb322
 #define SYSDB_ORIG_MODSTAMP "originalModifyTimestamp"
bcb322
diff --git a/src/providers/ldap/sdap.c b/src/providers/ldap/sdap.c
bcb322
index a9c8b92b8..a1a00df56 100644
bcb322
--- a/src/providers/ldap/sdap.c
bcb322
+++ b/src/providers/ldap/sdap.c
bcb322
@@ -771,6 +771,16 @@ errno_t sdap_parse_deref(TALLOC_CTX *mem_ctx,
bcb322
             goto done;
bcb322
         }
bcb322
 
bcb322
+        /* The dereference control seems to return the DN from the dereference
bcb322
+         * attribute (e.g. member) so we can use it as key for the hash table
bcb322
+         * later. */
bcb322
+        ret = sysdb_attrs_add_string(res[mi]->attrs,
bcb322
+                                     SYSDB_DN_FOR_MEMBER_HASH_TABLE, orig_dn);
bcb322
+        if (ret) {
bcb322
+            DEBUG(SSSDBG_OP_FAILURE, "sysdb_attrs_add_string failed.\n");
bcb322
+            goto done;
bcb322
+        }
bcb322
+
bcb322
         for (dval = dref->attrVals; dval != NULL; dval = dval->next) {
bcb322
             DEBUG(SSSDBG_TRACE_INTERNAL,
bcb322
                   "Dereferenced attribute: %s\n", dval->type);
bcb322
diff --git a/src/providers/ldap/sdap_async_groups.c b/src/providers/ldap/sdap_async_groups.c
bcb322
index abe2ed275..4e3c524a4 100644
bcb322
--- a/src/providers/ldap/sdap_async_groups.c
bcb322
+++ b/src/providers/ldap/sdap_async_groups.c
bcb322
@@ -2509,6 +2509,7 @@ static errno_t sdap_nested_group_populate_users(TALLOC_CTX *mem_ctx,
bcb322
     struct ldb_message_element *el;
bcb322
     const char *username;
bcb322
     const char *original_dn;
bcb322
+    const char *hash_key_dn;
bcb322
     struct sss_domain_info *user_dom;
bcb322
     struct sdap_domain *sdap_dom;
bcb322
 
bcb322
@@ -2607,8 +2608,22 @@ static errno_t sdap_nested_group_populate_users(TALLOC_CTX *mem_ctx,
bcb322
                                        SYSDB_MOD_REP);
bcb322
             if (ret != EOK) goto done;
bcb322
         } else {
bcb322
+            /* The DN of the user object and the DN in the member attribute
bcb322
+             * might differ, e.g. in case. Since we later search the hash with
bcb322
+             * DNs from the member attribute we should try to use DN from the
bcb322
+             * member attribute here as well. This should be added earlier in
bcb322
+             * the SYSDB_DN_FOR_MEMBER_HASH_TABLE attribute. If this does not
bcb322
+             * exists we fall-back to original_dn which should work in the
bcb322
+             * most cases as well. */
bcb322
+            ret = sysdb_attrs_get_string(users[i],
bcb322
+                                         SYSDB_DN_FOR_MEMBER_HASH_TABLE,
bcb322
+                                         &hash_key_dn);
bcb322
+            if (ret != EOK) {
bcb322
+                hash_key_dn = original_dn;
bcb322
+            }
bcb322
+
bcb322
             key.type = HASH_KEY_STRING;
bcb322
-            key.str = talloc_steal(ghosts, discard_const(original_dn));
bcb322
+            key.str = talloc_steal(ghosts, discard_const(hash_key_dn));
bcb322
             value.type = HASH_VALUE_PTR;
bcb322
             /* Already qualified from sdap_get_user_primary_name() */
bcb322
             value.ptr = talloc_steal(ghosts, discard_const(username));
bcb322
diff --git a/src/providers/ldap/sdap_async_nested_groups.c b/src/providers/ldap/sdap_async_nested_groups.c
bcb322
index 055de29ca..635b46403 100644
bcb322
--- a/src/providers/ldap/sdap_async_nested_groups.c
bcb322
+++ b/src/providers/ldap/sdap_async_nested_groups.c
bcb322
@@ -241,9 +241,12 @@ static errno_t sdap_nested_group_hash_entry(hash_table_t *table,
bcb322
     const char *name = NULL;
bcb322
     errno_t ret;
bcb322
 
bcb322
-    ret = sysdb_attrs_get_string(entry, SYSDB_ORIG_DN, &name);
bcb322
+    ret = sysdb_attrs_get_string(entry, SYSDB_DN_FOR_MEMBER_HASH_TABLE, &name);
bcb322
     if (ret != EOK) {
bcb322
-        return ret;
bcb322
+        ret = sysdb_attrs_get_string(entry, SYSDB_ORIG_DN, &name);
bcb322
+        if (ret != EOK) {
bcb322
+            return ret;
bcb322
+        }
bcb322
     }
bcb322
 
bcb322
     return sdap_nested_group_hash_insert(table, name, entry, false, table_name);
bcb322
@@ -1495,6 +1498,19 @@ sdap_nested_group_single_step_process(struct tevent_req *subreq)
bcb322
             }
bcb322
         }
bcb322
 
bcb322
+        /* The original DN of the user object itself might differ from the one
bcb322
+         * used inthe member attribute, e.g. different case. To make sure if
bcb322
+         * can be found in a hash table when iterating over group members the
bcb322
+         * DN from the member attribute used for the search as saved as well.
bcb322
+         */
bcb322
+        ret = sysdb_attrs_add_string(entry,
bcb322
+                                     SYSDB_DN_FOR_MEMBER_HASH_TABLE,
bcb322
+                                     state->current_member->dn);
bcb322
+        if (ret != EOK) {
bcb322
+            DEBUG(SSSDBG_OP_FAILURE, "sysdb_attrs_add_string failed.\n");
bcb322
+            goto done;
bcb322
+        }
bcb322
+
bcb322
         /* save user in hash table */
bcb322
         ret = sdap_nested_group_hash_user(state->group_ctx, entry);
bcb322
         if (ret == EEXIST) {
bcb322
-- 
bcb322
2.21.3
bcb322