Blame SOURCES/0071-LDAP-Relax-search-filters-in-application-domains.patch

ecf709
From b2a823cf415a12416dca9ff019666906d61cfc2f Mon Sep 17 00:00:00 2001
ecf709
From: Jakub Hrozek <jhrozek@redhat.com>
ecf709
Date: Wed, 22 Mar 2017 13:06:14 +0100
ecf709
Subject: [PATCH 71/72] LDAP: Relax search filters in application domains
ecf709
ecf709
Related to:
ecf709
https://pagure.io/SSSD/sssd/issue/3310
ecf709
ecf709
If a request comes towards an application domain, we can drop the part
ecf709
of the filter that asserts that the object has a valid UID/GID. Instead,
ecf709
we just search by name.
ecf709
ecf709
Reviewed-by: Sumit Bose <sbose@redhat.com>
ecf709
---
ecf709
 src/providers/ldap/ldap_id.c               | 35 ++++++++++++++++++++++++----
ecf709
 src/providers/ldap/sdap_async_enum.c       |  7 +++++-
ecf709
 src/providers/ldap/sdap_async_initgroups.c | 37 ++++++++++++++++++++++++------
ecf709
 3 files changed, 66 insertions(+), 13 deletions(-)
ecf709
ecf709
diff --git a/src/providers/ldap/ldap_id.c b/src/providers/ldap/ldap_id.c
ecf709
index 0bee0ca8d71abece6749fdb8393b9ceacb64417d..7400dc1f57e30cc6ae5f939ffa628a1e9dd47e06 100644
ecf709
--- a/src/providers/ldap/ldap_id.c
ecf709
+++ b/src/providers/ldap/ldap_id.c
ecf709
@@ -56,6 +56,7 @@ struct users_get_state {
ecf709
     char *filter;
ecf709
     const char **attrs;
ecf709
     bool use_id_mapping;
ecf709
+    bool non_posix;
ecf709
 
ecf709
     int dp_error;
ecf709
     int sdap_ret;
ecf709
@@ -114,6 +115,10 @@ struct tevent_req *users_get_send(TALLOC_CTX *memctx,
ecf709
     state->filter_value = filter_value;
ecf709
     state->filter_type = filter_type;
ecf709
 
ecf709
+    if (state->domain->type == DOM_TYPE_APPLICATION) {
ecf709
+        state->non_posix = true;
ecf709
+    }
ecf709
+
ecf709
     state->use_id_mapping = sdap_idmap_domain_has_algorithmic_mapping(
ecf709
                                                           ctx->opts->idmap_ctx,
ecf709
                                                           sdom->dom->name,
ecf709
@@ -292,7 +297,13 @@ struct tevent_req *users_get_send(TALLOC_CTX *memctx,
ecf709
         }
ecf709
     }
ecf709
 
ecf709
-    if (state->use_id_mapping || filter_type == BE_FILTER_SECID) {
ecf709
+    if (state->non_posix) {
ecf709
+        state->filter = talloc_asprintf(state,
ecf709
+                                        "(&%s(objectclass=%s)(%s=*))",
ecf709
+                                        user_filter,
ecf709
+                                        ctx->opts->user_map[SDAP_OC_USER].name,
ecf709
+                                        ctx->opts->user_map[SDAP_AT_USER_NAME].name);
ecf709
+    } else if (state->use_id_mapping || filter_type == BE_FILTER_SECID) {
ecf709
         /* When mapping IDs or looking for SIDs, we don't want to limit
ecf709
          * ourselves to users with a UID value. But there must be a SID to map
ecf709
          * from.
ecf709
@@ -304,7 +315,8 @@ struct tevent_req *users_get_send(TALLOC_CTX *memctx,
ecf709
                                         ctx->opts->user_map[SDAP_AT_USER_NAME].name,
ecf709
                                         ctx->opts->user_map[SDAP_AT_USER_OBJECTSID].name);
ecf709
     } else {
ecf709
-        /* When not ID-mapping, make sure there is a non-NULL UID */
ecf709
+        /* When not ID-mapping or looking up POSIX users,
ecf709
+         * make sure there is a non-NULL UID */
ecf709
         state->filter = talloc_asprintf(state,
ecf709
                                         "(&%s(objectclass=%s)(%s=*)(&(%s=*)(!(%s=0))))",
ecf709
                                         user_filter,
ecf709
@@ -380,6 +392,7 @@ static void users_get_connect_done(struct tevent_req *subreq)
ecf709
      * have no idea about POSIX attributes support, run a one-time check
ecf709
      */
ecf709
     if (state->use_id_mapping == false &&
ecf709
+            state->non_posix == false &&
ecf709
             state->ctx->opts->schema_type == SDAP_SCHEMA_AD &&
ecf709
             state->ctx->srv_opts &&
ecf709
             state->ctx->srv_opts->posix_checked == false) {
ecf709
@@ -650,6 +663,7 @@ struct groups_get_state {
ecf709
     char *filter;
ecf709
     const char **attrs;
ecf709
     bool use_id_mapping;
ecf709
+    bool non_posix;
ecf709
 
ecf709
     int dp_error;
ecf709
     int sdap_ret;
ecf709
@@ -709,6 +723,10 @@ struct tevent_req *groups_get_send(TALLOC_CTX *memctx,
ecf709
     state->filter_value = filter_value;
ecf709
     state->filter_type = filter_type;
ecf709
 
ecf709
+    if (state->domain->type == DOM_TYPE_APPLICATION) {
ecf709
+        state->non_posix = true;
ecf709
+    }
ecf709
+
ecf709
     state->use_id_mapping = sdap_idmap_domain_has_algorithmic_mapping(
ecf709
                                                           ctx->opts->idmap_ctx,
ecf709
                                                           sdom->dom->name,
ecf709
@@ -827,9 +845,11 @@ struct tevent_req *groups_get_send(TALLOC_CTX *memctx,
ecf709
         goto done;
ecf709
     }
ecf709
 
ecf709
-    if (state->use_id_mapping || filter_type == BE_FILTER_SECID) {
ecf709
-        /* When mapping IDs or looking for SIDs, we don't want to limit
ecf709
-         * ourselves to groups with a GID value
ecf709
+    if (state->non_posix
ecf709
+            || state->use_id_mapping
ecf709
+            || filter_type == BE_FILTER_SECID) {
ecf709
+        /* When mapping IDs or looking for SIDs, or when in a non-POSIX domain,
ecf709
+         * we don't want to limit ourselves to groups with a GID value
ecf709
          */
ecf709
 
ecf709
         state->filter = talloc_asprintf(state,
ecf709
@@ -1123,6 +1143,7 @@ struct groups_by_user_state {
ecf709
     int filter_type;
ecf709
     const char *extra_value;
ecf709
     const char **attrs;
ecf709
+    bool non_posix;
ecf709
 
ecf709
     int dp_error;
ecf709
     int sdap_ret;
ecf709
@@ -1204,6 +1225,10 @@ static struct tevent_req *groups_by_user_send(TALLOC_CTX *memctx,
ecf709
     state->domain = sdom->dom;
ecf709
     state->sysdb = sdom->dom->sysdb;
ecf709
 
ecf709
+    if (state->domain->type == DOM_TYPE_APPLICATION) {
ecf709
+        state->non_posix = true;
ecf709
+    }
ecf709
+
ecf709
     ret = build_attrs_from_map(state, ctx->opts->group_map, SDAP_OPTS_GROUP,
ecf709
                                NULL, &state->attrs, NULL);
ecf709
     if (ret != EOK) goto fail;
ecf709
diff --git a/src/providers/ldap/sdap_async_enum.c b/src/providers/ldap/sdap_async_enum.c
ecf709
index 3f65059e18d5c8b548da0babec867d27c3a64198..91e481c4e694126900c729e86d187fba355de0b8 100644
ecf709
--- a/src/providers/ldap/sdap_async_enum.c
ecf709
+++ b/src/providers/ldap/sdap_async_enum.c
ecf709
@@ -717,6 +717,7 @@ static struct tevent_req *enum_groups_send(TALLOC_CTX *memctx,
ecf709
     struct enum_groups_state *state;
ecf709
     int ret;
ecf709
     bool use_mapping;
ecf709
+    bool non_posix = false;
ecf709
     char *oc_list;
ecf709
 
ecf709
     req = tevent_req_create(memctx, &state, struct enum_groups_state);
ecf709
@@ -727,6 +728,10 @@ static struct tevent_req *enum_groups_send(TALLOC_CTX *memctx,
ecf709
     state->ctx = ctx;
ecf709
     state->op = op;
ecf709
 
ecf709
+    if (sdom->dom->type == DOM_TYPE_APPLICATION) {
ecf709
+        non_posix = true;
ecf709
+    }
ecf709
+
ecf709
     use_mapping = sdap_idmap_domain_has_algorithmic_mapping(
ecf709
                                                         ctx->opts->idmap_ctx,
ecf709
                                                         sdom->dom->name,
ecf709
@@ -749,7 +754,7 @@ static struct tevent_req *enum_groups_send(TALLOC_CTX *memctx,
ecf709
         goto fail;
ecf709
     }
ecf709
 
ecf709
-    if (use_mapping) {
ecf709
+    if (!non_posix && use_mapping) {
ecf709
         /* If we're ID-mapping, check for the objectSID as well */
ecf709
         state->filter = talloc_asprintf_append_buffer(
ecf709
                 state->filter, "(%s=*)",
ecf709
diff --git a/src/providers/ldap/sdap_async_initgroups.c b/src/providers/ldap/sdap_async_initgroups.c
ecf709
index 79af7a3eda3fe8533933535c98c2b4b4698dfda2..c926ddcbefe471daa80505e139c3f19efa33b9ba 100644
ecf709
--- a/src/providers/ldap/sdap_async_initgroups.c
ecf709
+++ b/src/providers/ldap/sdap_async_initgroups.c
ecf709
@@ -376,7 +376,7 @@ struct sdap_initgr_rfc2307_state {
ecf709
     struct sdap_handle *sh;
ecf709
     const char **attrs;
ecf709
     const char *name;
ecf709
-    const char *base_filter;
ecf709
+    char *base_filter;
ecf709
     const char *orig_dn;
ecf709
     char *filter;
ecf709
     int timeout;
ecf709
@@ -473,18 +473,32 @@ struct tevent_req *sdap_initgr_rfc2307_send(TALLOC_CTX *memctx,
ecf709
     }
ecf709
 
ecf709
     state->base_filter = talloc_asprintf(state,
ecf709
-                             "(&(%s=%s)(%s)(%s=*)(&(%s=*)(!(%s=0))))",
ecf709
+                             "(&(%s=%s)(%s)(%s=*)",
ecf709
                              opts->group_map[SDAP_AT_GROUP_MEMBER].name,
ecf709
                              clean_name, oc_list,
ecf709
-                             opts->group_map[SDAP_AT_GROUP_NAME].name,
ecf709
-                             opts->group_map[SDAP_AT_GROUP_GID].name,
ecf709
-                             opts->group_map[SDAP_AT_GROUP_GID].name);
ecf709
+                             opts->group_map[SDAP_AT_GROUP_NAME].name);
ecf709
     if (!state->base_filter) {
ecf709
         talloc_zfree(req);
ecf709
         return NULL;
ecf709
     }
ecf709
     talloc_zfree(clean_name);
ecf709
 
ecf709
+    switch (domain->type) {
ecf709
+    case DOM_TYPE_APPLICATION:
ecf709
+        state->base_filter = talloc_asprintf_append(state->base_filter, ")");
ecf709
+        break;
ecf709
+    case DOM_TYPE_POSIX:
ecf709
+        state->base_filter = talloc_asprintf_append(state->base_filter,
ecf709
+                                        "(&(%s=*)(!(%s=0))))",
ecf709
+                                        opts->group_map[SDAP_AT_GROUP_GID].name,
ecf709
+                                        opts->group_map[SDAP_AT_GROUP_GID].name);
ecf709
+        break;
ecf709
+    }
ecf709
+    if (!state->base_filter) {
ecf709
+        ret = ENOMEM;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
     ret = sdap_initgr_rfc2307_next_base(req);
ecf709
 
ecf709
 done:
ecf709
@@ -2666,6 +2680,7 @@ struct sdap_get_initgr_state {
ecf709
     char *shortname;
ecf709
     char *filter;
ecf709
     int timeout;
ecf709
+    bool non_posix;
ecf709
 
ecf709
     struct sysdb_attrs *orig_user;
ecf709
 
ecf709
@@ -2724,6 +2739,10 @@ struct tevent_req *sdap_get_initgr_send(TALLOC_CTX *memctx,
ecf709
         goto done;
ecf709
     }
ecf709
 
ecf709
+    if (state->dom->type == DOM_TYPE_APPLICATION) {
ecf709
+        state->non_posix = true;
ecf709
+    }
ecf709
+
ecf709
     use_id_mapping = sdap_idmap_domain_has_algorithmic_mapping(
ecf709
                                                           id_ctx->opts->idmap_ctx,
ecf709
                                                           sdom->dom->name,
ecf709
@@ -2813,7 +2832,10 @@ struct tevent_req *sdap_get_initgr_send(TALLOC_CTX *memctx,
ecf709
         }
ecf709
     }
ecf709
 
ecf709
-    if (use_id_mapping) {
ecf709
+    if (state->non_posix) {
ecf709
+        state->user_base_filter = talloc_asprintf_append(state->user_base_filter,
ecf709
+                                                         ")");
ecf709
+    } else if (use_id_mapping) {
ecf709
         /* When mapping IDs or looking for SIDs, we don't want to limit
ecf709
          * ourselves to users with a UID value. But there must be a SID to map
ecf709
          * from.
ecf709
@@ -2822,7 +2844,8 @@ struct tevent_req *sdap_get_initgr_send(TALLOC_CTX *memctx,
ecf709
                                         "(%s=*))",
ecf709
                                         id_ctx->opts->user_map[SDAP_AT_USER_OBJECTSID].name);
ecf709
     } else {
ecf709
-        /* When not ID-mapping, make sure there is a non-NULL UID */
ecf709
+        /* When not ID-mapping or looking up app users, make sure there
ecf709
+         * is a non-NULL UID */
ecf709
         state->user_base_filter = talloc_asprintf_append(state->user_base_filter,
ecf709
                                         "(&(%s=*)(!(%s=0))))",
ecf709
                                         id_ctx->opts->user_map[SDAP_AT_USER_UID].name,
ecf709
-- 
ecf709
2.9.3
ecf709