From b70d440a75c03a0395c317bc26770a8e34ae6154 Mon Sep 17 00:00:00 2001
From: Stephen Gallagher <sgallagh@redhat.com>
Date: Wed, 16 Mar 2011 12:59:33 -0400
Subject: [PATCH 5/5] RFC2307bis: Ignore aliases for groups

Groups in ldap with multiple values for their groupname attribute
will now be compared against the RDN of the entry to determine the
"primary" group name. We will save only this primary group name to
the ldb cache.
---
 src/providers/ldap/sdap_async_accounts.c |   68 +++++++++++++++++++++++++-----
 1 files changed, 57 insertions(+), 11 deletions(-)

diff --git a/src/providers/ldap/sdap_async_accounts.c b/src/providers/ldap/sdap_async_accounts.c
index 61860857958868a38b2dfb4df674f9bb0d03623d..73fe84dd527fd45748c9516dce0a93298844c94d 100644
--- a/src/providers/ldap/sdap_async_accounts.c
+++ b/src/providers/ldap/sdap_async_accounts.c
@@ -848,9 +848,16 @@ static int sdap_save_grpmem(TALLOC_CTX *memctx,
     const char *name;
     int ret;
 
-    ret = sysdb_attrs_get_string(attrs,
-                                opts->group_map[SDAP_AT_GROUP_NAME].sys_name,
-                                &name);
+    ret = sysdb_attrs_get_el(attrs,
+                             opts->group_map[SDAP_AT_GROUP_NAME].sys_name,
+                             &el);
+    if (ret != EOK) {
+        goto fail;
+    }
+
+    ret = sysdb_attrs_primary_name(ctx, attrs,
+                                   opts->group_map[SDAP_AT_GROUP_NAME].name,
+                                   el, &name);
     if (ret != EOK) {
         goto fail;
     }
@@ -2252,13 +2259,21 @@ static struct tevent_req *sdap_initgr_nested_send(TALLOC_CTX *memctx,
     state->grp_attrs = grp_attrs;
     state->op = NULL;
 
-    ret = sysdb_attrs_get_string(user, SYSDB_NAME, &state->username);
+    ret = sysdb_attrs_get_el(user, SYSDB_NAME, &el);
     if (ret != EOK) {
         DEBUG(1, ("User entry had no username\n"));
         talloc_free(req);
         return NULL;
     }
 
+    ret = sysdb_attrs_primary_name(sysdb, user,
+                                   opts->user_map[SDAP_AT_USER_NAME].name,
+                                   el, &state->username);
+    if (ret != EOK) {
+        talloc_free(req);
+        return NULL;
+    }
+
     state->filter = talloc_asprintf(state, "(&(objectclass=%s)(%s=*)(%s=*))",
                                     opts->group_map[SDAP_OC_GROUP].name,
                                     opts->group_map[SDAP_AT_GROUP_NAME].name,
@@ -2828,6 +2843,7 @@ static struct tevent_req *sdap_nested_group_process_send(
     const char *groupname;
     hash_key_t key;
     hash_value_t value;
+    struct ldb_message_element *el;
 
     req = tevent_req_create(mem_ctx, &state, struct sdap_nested_group_ctx);
     if (!req) {
@@ -2854,12 +2870,19 @@ static struct tevent_req *sdap_nested_group_process_send(
      */
     key.type = HASH_KEY_STRING;
 
-    ret = sysdb_attrs_get_string(
+    ret = sysdb_attrs_get_el(
             group,
             opts->group_map[SDAP_AT_GROUP_NAME].sys_name,
-            &groupname);
+            &el);
     if (ret != EOK) goto immediate;
 
+    ret = sysdb_attrs_primary_name(sysdb, group,
+                                   opts->group_map[SDAP_AT_GROUP_NAME].name,
+                                   el, &groupname);
+    if (ret != EOK) {
+        goto immediate;
+    }
+
     key.str = talloc_strdup(state, groupname);
     if (!key.str) {
         ret = ENOMEM;
@@ -3813,6 +3836,7 @@ static errno_t rfc2307bis_nested_groups_step(struct tevent_req *req)
     const char *orig_dn;
     const char **attrs;
     char *clean_orig_dn;
+    struct ldb_message_element *el;
     struct sdap_rfc2307bis_nested_ctx *state =
             tevent_req_data(req, struct sdap_rfc2307bis_nested_ctx);
 
@@ -3822,8 +3846,17 @@ static errno_t rfc2307bis_nested_groups_step(struct tevent_req *req)
         goto error;
     }
 
-    ret = sysdb_attrs_get_string(state->groups[state->group_iter],
-                                 SYSDB_NAME, &name);
+    ret = sysdb_attrs_get_el(state->groups[state->group_iter],
+                             SYSDB_NAME, &el);
+    if (ret != EOK) {
+        goto error;
+    }
+
+    ret = sysdb_attrs_primary_name(
+            state->sysdb,
+            state->groups[state->group_iter],
+            state->opts->group_map[SDAP_AT_GROUP_NAME].name,
+            el, &name);
     if (ret != EOK) {
         goto error;
     }
@@ -3862,7 +3895,8 @@ static errno_t rfc2307bis_nested_groups_step(struct tevent_req *req)
 
     DEBUG(6, ("Saving incomplete group [%s] to the sysdb\n",
               groupnamelist[0]));
-    ret = sdap_add_incomplete_groups(state->sysdb, state->dom, groupnamelist,
+    ret = sdap_add_incomplete_groups(state->sysdb, state->opts,
+                                     state->dom, groupnamelist,
                                      grouplist, 1);
     if (ret != EOK) {
         goto error;
@@ -4056,6 +4090,7 @@ static errno_t rfc2307bis_nested_groups_update_sysdb(
     char **add_groups;
     char **del_groups;
     const char *tmp_str;
+    struct ldb_message_element *el;
 
     TALLOC_CTX *tmp_ctx = talloc_new(state);
     if (!tmp_ctx) {
@@ -4071,12 +4106,23 @@ static errno_t rfc2307bis_nested_groups_update_sysdb(
     }
     in_transaction = true;
 
-    ret = sysdb_attrs_get_string(state->groups[state->group_iter],
-                                 SYSDB_NAME, &name);
+    ret = sysdb_attrs_get_el(state->groups[state->group_iter],
+                             SYSDB_NAME, &el);
     if (ret != EOK) {
         goto error;
     }
 
+    ret = sysdb_attrs_primary_name(
+            state->sysdb,
+            state->groups[state->group_iter],
+            state->opts->group_map[SDAP_AT_GROUP_NAME].name,
+            el, &name);
+    if (ret != EOK) {
+        goto error;
+    }
+
+    DEBUG(6, ("Processing group [%s]\n", name));
+
     attrs = talloc_array(tmp_ctx, const char *, 2);
     if (!attrs) {
         ret = ENOMEM;
-- 
1.7.4

