From 9096bd7b9d3e84b34a129b00f1479bb5400c6425 Mon Sep 17 00:00:00 2001
From: Jan Zeleny <jzeleny@redhat.com>
Date: Mon, 30 May 2011 06:45:12 -0400
Subject: [PATCH 1/8] Support excluding some attributes when building from map

---
 src/providers/ldap/ldap_id.c             |   12 +++++-----
 src/providers/ldap/ldap_id_enum.c        |    8 +++---
 src/providers/ldap/ldap_id_netgroup.c    |    2 +-
 src/providers/ldap/sdap.c                |   24 ++++++++++++++++++-
 src/providers/ldap/sdap.h                |    2 +-
 src/providers/ldap/sdap_async_accounts.c |   35 +++++++++++++++++++----------
 6 files changed, 57 insertions(+), 26 deletions(-)

diff --git a/src/providers/ldap/ldap_id.c b/src/providers/ldap/ldap_id.c
index a3c9c0cd451cb7290cefcd5f18d6c17c2e71319e..234cfd26d2c7eb4553175aceaa9ddd9064bff450 100644
--- a/src/providers/ldap/ldap_id.c
+++ b/src/providers/ldap/ldap_id.c
@@ -124,8 +124,8 @@ struct tevent_req *users_get_send(TALLOC_CTX *memctx,
     }
 
     /* TODO: handle attrs_type */
-    ret = build_attrs_from_map(state, ctx->opts->user_map,
-                               SDAP_OPTS_USER, &state->attrs);
+    ret = build_attrs_from_map(state, ctx->opts->user_map, SDAP_OPTS_USER,
+                               NULL, &state->attrs);
     if (ret != EOK) goto fail;
 
     ret = users_get_retry(req);
@@ -368,8 +368,8 @@ struct tevent_req *groups_get_send(TALLOC_CTX *memctx,
     }
 
     /* TODO: handle attrs_type */
-    ret = build_attrs_from_map(state, ctx->opts->group_map,
-                               SDAP_OPTS_GROUP, &state->attrs);
+    ret = build_attrs_from_map(state, ctx->opts->group_map, SDAP_OPTS_GROUP,
+                               NULL, &state->attrs);
     if (ret != EOK) goto fail;
 
     ret = groups_get_retry(req);
@@ -559,8 +559,8 @@ static struct tevent_req *groups_by_user_send(TALLOC_CTX *memctx,
 
     state->name = name;
 
-    ret = build_attrs_from_map(state, ctx->opts->group_map,
-                               SDAP_OPTS_GROUP, &state->attrs);
+    ret = build_attrs_from_map(state, ctx->opts->group_map, SDAP_OPTS_GROUP,
+                               NULL, &state->attrs);
     if (ret != EOK) goto fail;
 
     ret = groups_by_user_retry(req);
diff --git a/src/providers/ldap/ldap_id_enum.c b/src/providers/ldap/ldap_id_enum.c
index 68d113bf96a6db226c8a9541f5dbbd543ef043ae..8f7563fb688bce45c2027729208ef84c2208634d 100644
--- a/src/providers/ldap/ldap_id_enum.c
+++ b/src/providers/ldap/ldap_id_enum.c
@@ -478,8 +478,8 @@ static struct tevent_req *enum_users_send(TALLOC_CTX *memctx,
     }
 
     /* TODO: handle attrs_type */
-    ret = build_attrs_from_map(state, ctx->opts->user_map,
-                               SDAP_OPTS_USER, &state->attrs);
+    ret = build_attrs_from_map(state, ctx->opts->user_map, SDAP_OPTS_USER,
+                               NULL, &state->attrs);
     if (ret != EOK) goto fail;
 
     subreq = sdap_get_users_send(state, state->ev,
@@ -605,8 +605,8 @@ static struct tevent_req *enum_groups_send(TALLOC_CTX *memctx,
     }
 
     /* TODO: handle attrs_type */
-    ret = build_attrs_from_map(state, ctx->opts->group_map,
-                               SDAP_OPTS_GROUP, &state->attrs);
+    ret = build_attrs_from_map(state, ctx->opts->group_map, SDAP_OPTS_GROUP,
+                               NULL, &state->attrs);
     if (ret != EOK) goto fail;
 
     subreq = sdap_get_groups_send(state, state->ev,
diff --git a/src/providers/ldap/ldap_id_netgroup.c b/src/providers/ldap/ldap_id_netgroup.c
index 7b9ad0f1cd69c0ae3d43a5b2337400843c2cf6d9..542335494cfd9ea5d9c5b8fbf6cd124b1437c835 100644
--- a/src/providers/ldap/ldap_id_netgroup.c
+++ b/src/providers/ldap/ldap_id_netgroup.c
@@ -99,7 +99,7 @@ struct tevent_req *netgroup_get_send(TALLOC_CTX *memctx,
     talloc_zfree(clean_name);
 
     ret = build_attrs_from_map(state, ctx->opts->netgroup_map,
-                               SDAP_OPTS_NETGROUP, &state->attrs);
+                               SDAP_OPTS_NETGROUP, NULL, &state->attrs);
     if (ret != EOK) goto fail;
 
     ret = netgroup_get_retry(req);
diff --git a/src/providers/ldap/sdap.c b/src/providers/ldap/sdap.c
index 61044bb58451df434cc020601325a255653624ee..0a4ccc3d5787bab95c2fa29bda8367383d6a458b 100644
--- a/src/providers/ldap/sdap.c
+++ b/src/providers/ldap/sdap.c
@@ -873,9 +873,28 @@ void sdap_steal_server_opts(struct sdap_id_ctx *id_ctx,
 }
 
 
+static bool attr_is_filtered(const char *attr, const char **filter)
+{
+    int i;
+
+    if (filter) {
+        i = 0;
+        while (filter[i]) {
+            if (filter[i] == attr ||
+                strcmp(filter[i], attr) == 0) {
+                return true;
+            }
+            i++;
+        }
+    }
+
+    return false;
+}
+
 int build_attrs_from_map(TALLOC_CTX *memctx,
                          struct sdap_attr_map *map,
-                         size_t size, const char ***_attrs)
+                         size_t size, const char **filter,
+                         const char ***_attrs)
 {
     const char **attrs;
     int i, j;
@@ -889,7 +908,8 @@ int build_attrs_from_map(TALLOC_CTX *memctx,
 
     /* add the others */
     for (i = j = 1; i < size; i++) {
-        if (map[i].name) {
+        if (map[i].name &&
+            attr_is_filtered(map[i].name, filter) == false) {
             attrs[j] = map[i].name;
             j++;
         }
diff --git a/src/providers/ldap/sdap.h b/src/providers/ldap/sdap.h
index 4b662c15daaaa960a89da1b81e186fe3fe707c8d..ce1bac16ad3ce588af53dafdcde5be34a29a08cf 100644
--- a/src/providers/ldap/sdap.h
+++ b/src/providers/ldap/sdap.h
@@ -363,7 +363,7 @@ bool sdap_check_sup_list(struct sup_list *l, const char *val);
 
 int build_attrs_from_map(TALLOC_CTX *memctx,
                          struct sdap_attr_map *map,
-                         size_t size, const char ***_attrs);
+                         size_t size, const char **filter, const char ***_attrs);
 
 int sdap_control_create(struct sdap_handle *sh, const char *oid, int iscritical,
                         struct berval *value, int dupval, LDAPControl **ctrlp);
diff --git a/src/providers/ldap/sdap_async_accounts.c b/src/providers/ldap/sdap_async_accounts.c
index a8f95fdd171726c35b03104ffd41c65bdbc12ee5..084d5fa78d7933a71bc3dfb0fe1bb74b62e456ea 100644
--- a/src/providers/ldap/sdap_async_accounts.c
+++ b/src/providers/ldap/sdap_async_accounts.c
@@ -1080,7 +1080,8 @@ struct tevent_req *sdap_process_group_send(TALLOC_CTX *memctx,
                             struct sdap_process_group_state);
     if (!req) return NULL;
 
-    ret = build_attrs_from_map(grp_state, opts->user_map, SDAP_OPTS_USER, &attrs);
+    ret = build_attrs_from_map(grp_state, opts->user_map, SDAP_OPTS_USER, NULL,
+                               &attrs);
     if (ret) {
         goto done;
     }
@@ -2289,6 +2290,7 @@ struct tevent_req *sdap_initgr_rfc2307_send(TALLOC_CTX *memctx,
     const char **attrs;
     char *clean_name;
     errno_t ret;
+    char **attr_filter;
 
     req = tevent_req_create(memctx, &state, struct sdap_initgr_rfc2307_state);
     if (!req) return NULL;
@@ -2305,8 +2307,17 @@ struct tevent_req *sdap_initgr_rfc2307_send(TALLOC_CTX *memctx,
         return NULL;
     }
 
-    ret = build_attrs_from_map(state, opts->group_map,
-                               SDAP_OPTS_GROUP, &attrs);
+    attr_filter = talloc_array(state, char *, 2);
+    if (!attr_filter) {
+        talloc_free(req);
+        return NULL;
+    }
+
+    attr_filter[0] = opts->group_map[SDAP_AT_GROUP_MEMBER].name;
+    attr_filter[1] = NULL;
+
+    ret = build_attrs_from_map(state, opts->group_map, SDAP_OPTS_GROUP,
+                               (const char **)attr_filter, &attrs);
     if (ret != EOK) {
         talloc_free(req);
         return NULL;
@@ -3064,8 +3075,8 @@ struct tevent_req *sdap_get_initgr_send(TALLOC_CTX *memctx,
         return NULL;
     }
 
-    ret = build_attrs_from_map(state, state->opts->user_map,
-                               SDAP_OPTS_USER, &state->ldap_attrs);
+    ret = build_attrs_from_map(state, state->opts->user_map, SDAP_OPTS_USER,
+                               NULL, &state->ldap_attrs);
     if (ret) {
         talloc_zfree(req);
         return NULL;
@@ -3905,8 +3916,8 @@ sdap_nested_group_process_deref_call(struct tevent_req *req)
 
     /* Pull down the whole group map, but only pull down username
      * and originalDN for users. */
-    ret = build_attrs_from_map(state, state->opts->group_map,
-                               SDAP_OPTS_GROUP, &sdap_attrs);
+    ret = build_attrs_from_map(state, state->opts->group_map, SDAP_OPTS_GROUP,
+                               NULL, &sdap_attrs);
     if (ret != EOK) goto fail;
 
     sdap_attrs = talloc_realloc(NULL, sdap_attrs, const char *,
@@ -4037,8 +4048,8 @@ static errno_t sdap_nested_group_lookup_group(struct tevent_req *req)
     struct sdap_nested_group_ctx *state =
             tevent_req_data(req, struct sdap_nested_group_ctx);
 
-    ret = build_attrs_from_map(state, state->opts->group_map,
-                               SDAP_OPTS_GROUP, &sdap_attrs);
+    ret = build_attrs_from_map(state, state->opts->group_map, SDAP_OPTS_GROUP,
+                               NULL,  &sdap_attrs);
     if (ret != EOK) {
         return ret;
     }
@@ -4543,8 +4554,8 @@ static struct tevent_req *sdap_initgr_rfc2307bis_send(
     state->op = NULL;
     state->name = name;
 
-    ret = build_attrs_from_map(state, opts->group_map,
-                               SDAP_OPTS_GROUP, &attrs);
+    ret = build_attrs_from_map(state, opts->group_map, SDAP_OPTS_GROUP, NULL,
+                               &attrs);
     if (ret != EOK) {
         talloc_free(req);
         return NULL;
@@ -4956,7 +4967,7 @@ static errno_t rfc2307bis_nested_groups_step(struct tevent_req *req)
     }
 
     ret = build_attrs_from_map(tmp_ctx, state->opts->group_map,
-                               SDAP_OPTS_GROUP, &attrs);
+                               SDAP_OPTS_GROUP, NULL, &attrs);
     if (ret != EOK) {
         goto error;
     }
-- 
1.7.4.1

