From 23daf1ae79ac214f8c12c80b9bcbcaa525f5113e Mon Sep 17 00:00:00 2001
From: Stephen Gallagher <sgallagh@redhat.com>
Date: Tue, 22 Mar 2011 17:42:55 -0400
Subject: [PATCH 4/4] Add sysdb_attrs_primary_name_list() routine

This routine will replace the use of sysdb_attrs_to_list() for any
case where we're trying to get the name of the entry. It's a
necessary precaution in case the name is multi-valued.
---
 src/db/sysdb.c                           |   53 ++++++++++++++++++++++++++++++
 src/db/sysdb.h                           |    6 +++
 src/providers/ldap/sdap_async_accounts.c |   40 ++++++++++++----------
 3 files changed, 81 insertions(+), 18 deletions(-)

diff --git a/src/db/sysdb.c b/src/db/sysdb.c
index ef9d0a237645f8b52e9a64eedd54d7d9f3762f05..94738c606a9c4b69ce2cda1a1629aba9524cd68b 100644
--- a/src/db/sysdb.c
+++ b/src/db/sysdb.c
@@ -2256,3 +2256,56 @@ done:
     talloc_free(tmpctx);
     return ret;
 }
+
+errno_t sysdb_attrs_primary_name_list(struct sysdb_ctx *sysdb,
+                                      TALLOC_CTX *mem_ctx,
+                                      struct sysdb_attrs **attr_list,
+                                      size_t attr_count,
+                                      const char *ldap_attr,
+                                      char ***name_list)
+{
+    errno_t ret;
+    size_t i, j;
+    char **list;
+    const char *name;
+
+    /* Assume that every entry has a primary name */
+    list = talloc_array(mem_ctx, char *, attr_count+1);
+    if (!list) {
+        return ENOMEM;
+    }
+
+    j = 0;
+    for (i = 0; i < attr_count; i++) {
+        ret = sysdb_attrs_primary_name(sysdb,
+                                       attr_list[i],
+                                       ldap_attr,
+                                       &name);
+        if (ret != EOK) {
+            DEBUG(1, ("Could not determine primary name\n"));
+            /* Skip and continue. Don't advance 'j' */
+            continue;
+        }
+
+        list[j] = talloc_strdup(list, name);
+        if (!list[j]) {
+            ret = ENOMEM;
+            goto done;
+        }
+
+        j++;
+    }
+
+    /* NULL-terminate the list */
+    list[j] = NULL;
+
+    *name_list = list;
+
+    ret = EOK;
+
+done:
+    if (ret != EOK) {
+        talloc_free(list);
+    }
+    return ret;
+}
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index 971a35f99d07264a9d4950e5b852ab450340ddc5..b7256911a793c575f716a8728503b6c58bb6dce3 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -219,6 +219,12 @@ errno_t sysdb_attrs_primary_name(struct sysdb_ctx *sysdb,
                                  struct sysdb_attrs *attrs,
                                  const char *ldap_attr,
                                  const char **_primary);
+errno_t sysdb_attrs_primary_name_list(struct sysdb_ctx *sysdb,
+                                      TALLOC_CTX *mem_ctx,
+                                      struct sysdb_attrs **attr_list,
+                                      size_t attr_count,
+                                      const char *ldap_attr,
+                                      char ***name_list);
 
 /* convert an ldb error into an errno error */
 int sysdb_error_to_errno(int ldberr);
diff --git a/src/providers/ldap/sdap_async_accounts.c b/src/providers/ldap/sdap_async_accounts.c
index 80f2b3a33b6853b4821efb8e2875bbfa6b979e5a..b6e392b206a527fdf189d18bf7a41a5e76d864bd 100644
--- a/src/providers/ldap/sdap_async_accounts.c
+++ b/src/providers/ldap/sdap_async_accounts.c
@@ -1998,12 +1998,13 @@ static int sdap_initgr_common_store(struct sysdb_ctx *sysdb,
          */
         ldap_grouplist = NULL;
     } else {
-        ret = sysdb_attrs_to_list(tmp_ctx, ldap_groups,
-                                  ldap_groups_count,
-                                  SYSDB_NAME,
-                                  &ldap_grouplist);
+        ret = sysdb_attrs_primary_name_list(
+                sysdb, tmp_ctx,
+                ldap_groups, ldap_groups_count,
+                opts->group_map[SDAP_AT_GROUP_NAME].name,
+                &ldap_grouplist);
         if (ret != EOK) {
-            DEBUG(1, ("sysdb_attrs_to_list failed [%d]: %s\n",
+            DEBUG(1, ("sysdb_attrs_primary_name_list failed [%d]: %s\n",
                       ret, strerror(ret)));
             goto done;
         }
@@ -2530,12 +2531,13 @@ static void sdap_initgr_nested_store(struct tevent_req *req)
 
     /* Not all indirect groups may be cached.
      * Add fake entries for those that are not */
-    ret = sysdb_attrs_to_list(state, state->groups,
-                              state->groups_cur,
-                              SYSDB_NAME,
-                              &ldap_grouplist);
+    ret = sysdb_attrs_primary_name_list(
+            state->sysdb, state,
+            state->groups, state->groups_cur,
+            state->opts->group_map[SDAP_AT_GROUP_NAME].name,
+            &ldap_grouplist);
     if (ret != EOK) {
-        DEBUG(1, ("sysdb_attrs_to_list failed [%d]: %s\n",
+        DEBUG(1, ("sysdb_attrs_primary_name_list failed [%d]: %s\n",
                     ret, strerror(ret)));
         goto done;
     }
@@ -3957,10 +3959,11 @@ errno_t save_rfc2307bis_user_memberships(
         ldap_grouplist = NULL;
     }
     else {
-        ret = sysdb_attrs_to_list(tmp_ctx,
-                                  state->ldap_groups, state->ldap_groups_count,
-                                  SYSDB_NAME,
-                                  &ldap_grouplist);
+        ret = sysdb_attrs_primary_name_list(
+                state->sysdb, tmp_ctx,
+                state->ldap_groups, state->ldap_groups_count,
+                state->opts->group_map[SDAP_AT_GROUP_NAME].name,
+                &ldap_grouplist);
         if (ret != EOK) {
             goto error;
         }
@@ -4446,10 +4449,11 @@ static errno_t rfc2307bis_nested_groups_update_sysdb(
         ldap_grouplist = NULL;
     }
     else {
-        ret = sysdb_attrs_to_list(tmp_ctx,
-                                  state->ldap_groups, state->ldap_groups_count,
-                                  SYSDB_NAME,
-                                  &ldap_grouplist);
+        ret = sysdb_attrs_primary_name_list(
+                state->sysdb, tmp_ctx,
+                state->ldap_groups, state->ldap_groups_count,
+                state->opts->group_map[SDAP_AT_GROUP_NAME].name,
+                &ldap_grouplist);
         if (ret != EOK) {
             goto error;
         }
-- 
1.7.4

