From f575a6483c9b04abb6d84c20d2264cea49e5e803 Mon Sep 17 00:00:00 2001
From: Stephen Gallagher <sgallagh@redhat.com>
Date: Wed, 28 Sep 2011 11:26:09 -0400
Subject: [PATCH 12/14] LDAP: Convert ldap_*_search_filter

Instead of making this a global option for all user lookups, make
it only used if the search base is passed without an explicit
filter.
---
 src/providers/ldap/ldap_common.c  |    6 ++++-
 src/providers/ldap/ldap_id.c      |   44 ++++++++++--------------------------
 src/providers/ldap/ldap_id_enum.c |   32 +++++---------------------
 3 files changed, 23 insertions(+), 59 deletions(-)

diff --git a/src/providers/ldap/ldap_common.c b/src/providers/ldap/ldap_common.c
index 233d01947a77a86df93b58cf1abb4aa49d60f8af..8f5b8ac4f151ff4733f57aeabf26ce878586a76c 100644
--- a/src/providers/ldap/ldap_common.c
+++ b/src/providers/ldap/ldap_common.c
@@ -508,6 +508,7 @@ errno_t sdap_parse_search_base(TALLOC_CTX *mem_ctx,
     char *unparsed_base;
     char **split_bases;
     char *filter;
+    const char *old_filter = NULL;
     int count;
     int i, c;
 
@@ -519,9 +520,11 @@ errno_t sdap_parse_search_base(TALLOC_CTX *mem_ctx,
         break;
     case SDAP_USER_SEARCH_BASE:
         class_name = "USER";
+        old_filter = dp_opt_get_string(opts->basic, SDAP_USER_SEARCH_FILTER);
         break;
     case SDAP_GROUP_SEARCH_BASE:
         class_name = "GROUP";
+        old_filter = dp_opt_get_string(opts->basic, SDAP_GROUP_SEARCH_FILTER);
         break;
     case SDAP_NETGROUP_SEARCH_BASE:
         class_name = "NETGROUP";
@@ -601,8 +604,9 @@ errno_t sdap_parse_search_base(TALLOC_CTX *mem_ctx,
         talloc_zfree(ldn);
 
         search_bases[0]->scope = LDAP_SCOPE_SUBTREE;
-        search_bases[0]->filter = NULL;
 
+        /* Use a search filter specified in the old style if available */
+        search_bases[0]->filter = old_filter;
 
         DEBUG(SSSDBG_CONF_SETTINGS,
               ("Search base added: [%s][%s][%s][%s]\n",
diff --git a/src/providers/ldap/ldap_id.c b/src/providers/ldap/ldap_id.c
index 607fd27841cb34abb8684923f5bd68e92e3f7f79..3e93bb8525bf1c9a9bbb51a348941c09d8c0c3da 100644
--- a/src/providers/ldap/ldap_id.c
+++ b/src/providers/ldap/ldap_id.c
@@ -65,7 +65,6 @@ struct tevent_req *users_get_send(TALLOC_CTX *memctx,
     struct users_get_state *state;
     const char *attr_name;
     char *clean_name;
-    char *base_filter;
     int ret;
 
     req = tevent_req_create(memctx, &state, struct users_get_state);
@@ -104,25 +103,16 @@ struct tevent_req *users_get_send(TALLOC_CTX *memctx,
         goto fail;
     }
 
-    base_filter = talloc_asprintf(state, "(&(%s=%s)(objectclass=%s))",
-                                  attr_name, clean_name,
-                                  ctx->opts->user_map[SDAP_OC_USER].name);
+    state->filter = talloc_asprintf(state, "(&(%s=%s)(objectclass=%s))",
+                                    attr_name, clean_name,
+                                    ctx->opts->user_map[SDAP_OC_USER].name);
     talloc_zfree(clean_name);
-    if (!base_filter) {
+    if (!state->filter) {
         DEBUG(2, ("Failed to build the base filter\n"));
         ret = ENOMEM;
         goto fail;
     }
 
-    state->filter = sdap_get_id_specific_filter(state, base_filter,
-           dp_opt_get_string(ctx->opts->basic, SDAP_USER_SEARCH_FILTER));
-    talloc_zfree(base_filter);
-    if (!state->filter) {
-        DEBUG(2, ("Failed to build user filter\n"));
-        ret = ENOMEM;
-        goto fail;
-    }
-
     /* TODO: handle attrs_type */
     ret = build_attrs_from_map(state, ctx->opts->user_map,
                                SDAP_OPTS_USER, &state->attrs);
@@ -306,7 +296,6 @@ struct tevent_req *groups_get_send(TALLOC_CTX *memctx,
     struct groups_get_state *state;
     const char *attr_name;
     char *clean_name;
-    char *base_filter;
     int ret;
 
     req = tevent_req_create(memctx, &state, struct groups_get_state);
@@ -345,29 +334,20 @@ struct tevent_req *groups_get_send(TALLOC_CTX *memctx,
         goto fail;
     }
 
-    base_filter = talloc_asprintf(state,
-                                "(&(%s=%s)(objectclass=%s)(%s=*)(&(%s=*)(!(%s=0))))",
-                                attr_name, clean_name,
-                                ctx->opts->group_map[SDAP_OC_GROUP].name,
-                                ctx->opts->group_map[SDAP_AT_GROUP_NAME].name,
-                                ctx->opts->group_map[SDAP_AT_GROUP_GID].name,
-                                ctx->opts->group_map[SDAP_AT_GROUP_GID].name);
+    state->filter = talloc_asprintf(state,
+                                    "(&(%s=%s)(objectclass=%s)(%s=*)(&(%s=*)(!(%s=0))))",
+                                    attr_name, clean_name,
+                                    ctx->opts->group_map[SDAP_OC_GROUP].name,
+                                    ctx->opts->group_map[SDAP_AT_GROUP_NAME].name,
+                                    ctx->opts->group_map[SDAP_AT_GROUP_GID].name,
+                                    ctx->opts->group_map[SDAP_AT_GROUP_GID].name);
     talloc_zfree(clean_name);
-    if (!base_filter) {
+    if (!state->filter) {
         DEBUG(2, ("Failed to build filter\n"));
         ret = ENOMEM;
         goto fail;
     }
 
-    state->filter = sdap_get_id_specific_filter(state, base_filter,
-           dp_opt_get_string(ctx->opts->basic, SDAP_GROUP_SEARCH_FILTER));
-    talloc_zfree(base_filter);
-    if (!state->filter) {
-        DEBUG(2, ("Failed to build group-specific filter\n"));
-        ret = ENOMEM;
-        goto fail;
-    }
-
     /* TODO: handle attrs_type */
     ret = build_attrs_from_map(state, ctx->opts->group_map,
                                SDAP_OPTS_GROUP, &state->attrs);
diff --git a/src/providers/ldap/ldap_id_enum.c b/src/providers/ldap/ldap_id_enum.c
index d49b64dd8663854e258f714b93325a0b162189a7..45f4ef78a9caa38e4f4d77b23b4e55e65f3f559d 100644
--- a/src/providers/ldap/ldap_id_enum.c
+++ b/src/providers/ldap/ldap_id_enum.c
@@ -429,7 +429,6 @@ static struct tevent_req *enum_users_send(TALLOC_CTX *memctx,
 {
     struct tevent_req *req, *subreq;
     struct enum_users_state *state;
-    char *base_filter;
     int ret;
 
     req = tevent_req_create(memctx, &state, struct enum_users_state);
@@ -440,7 +439,7 @@ static struct tevent_req *enum_users_send(TALLOC_CTX *memctx,
     state->op = op;
 
     if (ctx->srv_opts && ctx->srv_opts->max_user_value && !purge) {
-        base_filter = talloc_asprintf(
+        state->filter = talloc_asprintf(
                 state,
                 "(&(objectclass=%s)(%s=*)(%s=*)(%s=*)(%s>=%s)(!(%s=%s)))",
                 ctx->opts->user_map[SDAP_OC_USER].name,
@@ -452,7 +451,7 @@ static struct tevent_req *enum_users_send(TALLOC_CTX *memctx,
                 ctx->opts->user_map[SDAP_AT_USER_USN].name,
                 ctx->srv_opts->max_user_value);
     } else {
-        base_filter = talloc_asprintf(
+        state->filter = talloc_asprintf(
                 state,
                 "(&(objectclass=%s)(%s=*)(%s=*)(%s=*))",
                 ctx->opts->user_map[SDAP_OC_USER].name,
@@ -460,21 +459,12 @@ static struct tevent_req *enum_users_send(TALLOC_CTX *memctx,
                 ctx->opts->user_map[SDAP_AT_USER_UID].name,
                 ctx->opts->user_map[SDAP_AT_USER_GID].name);
     }
-    if (!base_filter) {
+    if (!state->filter) {
         DEBUG(2, ("Failed to build base filter\n"));
         ret = ENOMEM;
         goto fail;
     }
 
-    state->filter = sdap_get_id_specific_filter(state, base_filter,
-           dp_opt_get_string(ctx->opts->basic, SDAP_USER_SEARCH_FILTER));
-    talloc_zfree(base_filter);
-    if (!state->filter) {
-        DEBUG(2, ("Failed to build user filter\n"));
-        ret = ENOMEM;
-        goto fail;
-    }
-
     /* TODO: handle attrs_type */
     ret = build_attrs_from_map(state, ctx->opts->user_map,
                                SDAP_OPTS_USER, &state->attrs);
@@ -563,7 +553,6 @@ static struct tevent_req *enum_groups_send(TALLOC_CTX *memctx,
 {
     struct tevent_req *req, *subreq;
     struct enum_groups_state *state;
-    char *base_filter;
     int ret;
 
     req = tevent_req_create(memctx, &state, struct enum_groups_state);
@@ -574,7 +563,7 @@ static struct tevent_req *enum_groups_send(TALLOC_CTX *memctx,
     state->op = op;
 
     if (ctx->srv_opts && ctx->srv_opts->max_group_value && !purge) {
-        base_filter = talloc_asprintf(
+        state->filter = talloc_asprintf(
                 state,
                 "(&(objectclass=%s)(%s=*)(&(%s=*)(!(%s=0)))(%s>=%s)(!(%s=%s)))",
                 ctx->opts->group_map[SDAP_OC_GROUP].name,
@@ -586,7 +575,7 @@ static struct tevent_req *enum_groups_send(TALLOC_CTX *memctx,
                 ctx->opts->group_map[SDAP_AT_GROUP_USN].name,
                 ctx->srv_opts->max_group_value);
     } else {
-        base_filter = talloc_asprintf(
+        state->filter = talloc_asprintf(
                 state,
                 "(&(objectclass=%s)(%s=*)(&(%s=*)(!(%s=0))))",
                 ctx->opts->group_map[SDAP_OC_GROUP].name,
@@ -594,21 +583,12 @@ static struct tevent_req *enum_groups_send(TALLOC_CTX *memctx,
                 ctx->opts->group_map[SDAP_AT_GROUP_GID].name,
                 ctx->opts->group_map[SDAP_AT_GROUP_GID].name);
     }
-    if (!base_filter) {
+    if (!state->filter) {
         DEBUG(2, ("Failed to build filter\n"));
         ret = ENOMEM;
         goto fail;
     }
 
-    state->filter = sdap_get_id_specific_filter(state, base_filter,
-           dp_opt_get_string(ctx->opts->basic, SDAP_GROUP_SEARCH_FILTER));
-    talloc_zfree(base_filter);
-    if (!state->filter) {
-        DEBUG(2, ("Failed to build group filter\n"));
-        ret = ENOMEM;
-        goto fail;
-    }
-
     /* TODO: handle attrs_type */
     ret = build_attrs_from_map(state, ctx->opts->group_map,
                                SDAP_OPTS_GROUP, &state->attrs);
-- 
1.7.7

