Blame SOURCES/0126-CACHE_REQ-Make-use-of-cache_req_ncache_filter_fn.patch

ecf709
From 4c3780ced1b1507ebd8c3d0b91a3ef50b74e0b52 Mon Sep 17 00:00:00 2001
ecf709
From: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= <fidencio@redhat.com>
ecf709
Date: Tue, 25 Apr 2017 16:33:58 +0200
ecf709
Subject: [PATCH 126/127] CACHE_REQ: Make use of cache_req_ncache_filter_fn()
ecf709
MIME-Version: 1.0
ecf709
Content-Type: text/plain; charset=UTF-8
ecf709
Content-Transfer-Encoding: 8bit
ecf709
ecf709
This patch makes use of cache_req_ncache_filter_fn() in order to process
ecf709
the result of a cache_req search and then filter out all the results
ecf709
that are present in the negative cache.
ecf709
ecf709
The "post cache_req search" result processing is done basically in two
ecf709
different cases:
ecf709
- plugins which don't use name as an input token (group_by_id, user_by_id
ecf709
  and object_by_id), but still can be affected by filter_{users,groups}
ecf709
  options;
ecf709
- plugins responsible for groups and users enumeration (enum_groups and
ecf709
  enum_users);
ecf709
ecf709
Resolves:
ecf709
https://pagure.io/SSSD/sssd/issue/3362
ecf709
ecf709
Signed-off-by: Fabiano Fidêncio <fidencio@redhat.com>
ecf709
ecf709
Reviewed-by: Pavel Březina <pbrezina@redhat.com>
ecf709
(cherry picked from commit 4ef0b19a5e8a327443d027e57487c8a1e4f654ce)
ecf709
---
ecf709
 src/responder/common/cache_req/cache_req_search.c  | 124 +++++++++++++++++++--
ecf709
 .../cache_req/plugins/cache_req_enum_groups.c      |  10 +-
ecf709
 .../cache_req/plugins/cache_req_enum_users.c       |  10 +-
ecf709
 .../cache_req/plugins/cache_req_group_by_id.c      |  10 +-
ecf709
 .../cache_req/plugins/cache_req_object_by_id.c     |  17 ++-
ecf709
 .../cache_req/plugins/cache_req_user_by_id.c       |  10 +-
ecf709
 src/responder/nss/nss_protocol_grent.c             |  12 --
ecf709
 src/responder/nss/nss_protocol_pwent.c             |  11 --
ecf709
 8 files changed, 165 insertions(+), 39 deletions(-)
ecf709
ecf709
diff --git a/src/responder/common/cache_req/cache_req_search.c b/src/responder/common/cache_req/cache_req_search.c
ecf709
index 8bc1530b341f587cb502fdf0ca3ed8d37cfb7d13..793dbc5042ae329b2cade5d1eb5a6d41102e264f 100644
ecf709
--- a/src/responder/common/cache_req/cache_req_search.c
ecf709
+++ b/src/responder/common/cache_req/cache_req_search.c
ecf709
@@ -84,6 +84,87 @@ static void cache_req_search_ncache_add(struct cache_req *cr)
ecf709
     return;
ecf709
 }
ecf709
 
ecf709
+static errno_t cache_req_search_ncache_filter(TALLOC_CTX *mem_ctx,
ecf709
+                                              struct cache_req *cr,
ecf709
+                                              struct ldb_result *result,
ecf709
+                                              struct ldb_result **_result)
ecf709
+{
ecf709
+    TALLOC_CTX *tmp_ctx;
ecf709
+    struct ldb_result *filtered_result;
ecf709
+    struct ldb_message **msgs;
ecf709
+    size_t msg_count;
ecf709
+    const char *name;
ecf709
+    errno_t ret;
ecf709
+
ecf709
+    tmp_ctx = talloc_new(NULL);
ecf709
+    if (tmp_ctx == NULL) {
ecf709
+        return ENOMEM;
ecf709
+    }
ecf709
+
ecf709
+    if (cr->plugin->ncache_filter_fn == NULL) {
ecf709
+        CACHE_REQ_DEBUG(SSSDBG_TRACE_FUNC, cr,
ecf709
+                        "This request type does not support filtering "
ecf709
+                        "result by negative cache\n");
ecf709
+
ecf709
+        *_result = talloc_steal(mem_ctx, result);
ecf709
+
ecf709
+        ret = EOK;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    CACHE_REQ_DEBUG(SSSDBG_TRACE_FUNC, cr,
ecf709
+                    "Filtering out results by negative cache\n");
ecf709
+
ecf709
+    msgs = talloc_zero_array(tmp_ctx, struct ldb_message *, result->count);
ecf709
+    msg_count = 0;
ecf709
+
ecf709
+    for (size_t i = 0; i < result->count; i++) {
ecf709
+        name = sss_get_name_from_msg(cr->domain, result->msgs[i]);
ecf709
+        if (name == NULL) {
ecf709
+            CACHE_REQ_DEBUG(SSSDBG_CRIT_FAILURE, cr,
ecf709
+                  "sss_get_name_from_msg() returned NULL, which should never "
ecf709
+                  "happen in this scenario!\n");
ecf709
+            ret = ERR_INTERNAL;
ecf709
+            goto done;
ecf709
+        }
ecf709
+
ecf709
+        ret = cr->plugin->ncache_filter_fn(cr->ncache, cr->domain, name);
ecf709
+        if (ret == EEXIST) {
ecf709
+            CACHE_REQ_DEBUG(SSSDBG_TRACE_FUNC, cr,
ecf709
+                            "[%s] filtered out! (negative cache)\n",
ecf709
+                            name);
ecf709
+            continue;
ecf709
+        } else if (ret != EOK && ret != ENOENT) {
ecf709
+            CACHE_REQ_DEBUG(SSSDBG_CRIT_FAILURE, cr,
ecf709
+                            "Unable to check negative cache [%d]: %s\n",
ecf709
+                            ret, sss_strerror(ret));
ecf709
+            goto done;
ecf709
+        }
ecf709
+
ecf709
+        msgs[msg_count] = talloc_steal(msgs, result->msgs[i]);
ecf709
+        msg_count++;
ecf709
+    }
ecf709
+
ecf709
+    if (msg_count == 0) {
ecf709
+        ret = ENOENT;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    filtered_result = cache_req_create_ldb_result_from_msg_list(tmp_ctx, msgs,
ecf709
+                                                                msg_count);
ecf709
+    if (filtered_result == NULL) {
ecf709
+        ret = ENOMEM;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    *_result = talloc_steal(mem_ctx, filtered_result);
ecf709
+    ret = EOK;
ecf709
+
ecf709
+done:
ecf709
+    talloc_free(tmp_ctx);
ecf709
+    return ret;
ecf709
+}
ecf709
+
ecf709
 static errno_t cache_req_search_cache(TALLOC_CTX *mem_ctx,
ecf709
                                       struct cache_req *cr,
ecf709
                                       struct ldb_result **_result)
ecf709
@@ -338,10 +419,18 @@ static void cache_req_search_oob_done(struct tevent_req *subreq)
ecf709
 
ecf709
 static void cache_req_search_done(struct tevent_req *subreq)
ecf709
 {
ecf709
+    TALLOC_CTX *tmp_ctx;
ecf709
     struct cache_req_search_state *state;
ecf709
     struct tevent_req *req;
ecf709
+    struct ldb_result *result = NULL;
ecf709
     errno_t ret;
ecf709
 
ecf709
+    tmp_ctx = talloc_new(NULL);
ecf709
+    if (tmp_ctx == NULL) {
ecf709
+        ret = ENOMEM;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
     req = tevent_req_callback_data(subreq, struct tevent_req);
ecf709
     state = tevent_req_data(req, struct cache_req_search_state);
ecf709
 
ecf709
@@ -349,23 +438,36 @@ static void cache_req_search_done(struct tevent_req *subreq)
ecf709
     talloc_zfree(subreq);
ecf709
 
ecf709
     /* Get result from cache again. */
ecf709
-    ret = cache_req_search_cache(state, state->cr, &state->result);
ecf709
-    if (ret == ENOENT) {
ecf709
-        /* Only store entry in negative cache if DP request succeeded
ecf709
-         * because only then we know that the entry does not exist. */
ecf709
-        if (state->dp_success) {
ecf709
-            cache_req_search_ncache_add(state->cr);
ecf709
+    ret = cache_req_search_cache(tmp_ctx, state->cr, &result);
ecf709
+    if (ret != EOK) {
ecf709
+        if (ret == ENOENT) {
ecf709
+            /* Only store entry in negative cache if DP request succeeded
ecf709
+             * because only then we know that the entry does not exist. */
ecf709
+            if (state->dp_success) {
ecf709
+                cache_req_search_ncache_add(state->cr);
ecf709
+            }
ecf709
         }
ecf709
-        tevent_req_error(req, ENOENT);
ecf709
-        return;
ecf709
-    } else if (ret != EOK) {
ecf709
-        tevent_req_error(req, ret);
ecf709
-        return;
ecf709
+        goto done;
ecf709
+    }
ecf709
+
ecf709
+    /* ret == EOK */
ecf709
+    ret = cache_req_search_ncache_filter(state, state->cr, result,
ecf709
+                                         &state->result);
ecf709
+    if (ret != EOK) {
ecf709
+        goto done;
ecf709
     }
ecf709
 
ecf709
     CACHE_REQ_DEBUG(SSSDBG_TRACE_FUNC, state->cr,
ecf709
                     "Returning updated object [%s]\n", state->cr->debugobj);
ecf709
 
ecf709
+done:
ecf709
+    talloc_free(tmp_ctx);
ecf709
+
ecf709
+    if (ret != EOK) {
ecf709
+        tevent_req_error(req, ret);
ecf709
+        return;
ecf709
+    }
ecf709
+
ecf709
     tevent_req_done(req);
ecf709
     return;
ecf709
 }
ecf709
diff --git a/src/responder/common/cache_req/plugins/cache_req_enum_groups.c b/src/responder/common/cache_req/plugins/cache_req_enum_groups.c
ecf709
index 11ce9e90ff28f77078b025a44593a44be8f1f5c5..15350ca8279bc77c73bcc4abe51c97a8a37cb8c8 100644
ecf709
--- a/src/responder/common/cache_req/plugins/cache_req_enum_groups.c
ecf709
+++ b/src/responder/common/cache_req/plugins/cache_req_enum_groups.c
ecf709
@@ -55,6 +55,14 @@ cache_req_enum_groups_dp_send(TALLOC_CTX *mem_ctx,
ecf709
                                    SSS_DP_GROUP, NULL, 0, NULL);
ecf709
 }
ecf709
 
ecf709
+static errno_t
ecf709
+cache_req_enum_groups_ncache_filter(struct sss_nc_ctx *ncache,
ecf709
+                                    struct sss_domain_info *domain,
ecf709
+                                    const char *name)
ecf709
+{
ecf709
+    return sss_ncache_check_group(ncache, domain, name);
ecf709
+}
ecf709
+
ecf709
 const struct cache_req_plugin cache_req_enum_groups = {
ecf709
     .name = "Enumerate groups",
ecf709
     .attr_expiration = SYSDB_CACHE_EXPIRE,
ecf709
@@ -75,7 +83,7 @@ const struct cache_req_plugin cache_req_enum_groups = {
ecf709
     .global_ncache_add_fn = NULL,
ecf709
     .ncache_check_fn = NULL,
ecf709
     .ncache_add_fn = NULL,
ecf709
-    .ncache_filter_fn = NULL,
ecf709
+    .ncache_filter_fn = cache_req_enum_groups_ncache_filter,
ecf709
     .lookup_fn = cache_req_enum_groups_lookup,
ecf709
     .dp_send_fn = cache_req_enum_groups_dp_send,
ecf709
     .dp_recv_fn = cache_req_common_dp_recv
ecf709
diff --git a/src/responder/common/cache_req/plugins/cache_req_enum_users.c b/src/responder/common/cache_req/plugins/cache_req_enum_users.c
ecf709
index e0647a0102d9568abdcebfbf0fb99fc2624d5565..a3ddcdd45548a2fa7c367f3fb3be103c115dedb4 100644
ecf709
--- a/src/responder/common/cache_req/plugins/cache_req_enum_users.c
ecf709
+++ b/src/responder/common/cache_req/plugins/cache_req_enum_users.c
ecf709
@@ -55,6 +55,14 @@ cache_req_enum_users_dp_send(TALLOC_CTX *mem_ctx,
ecf709
                                    SSS_DP_USER, NULL, 0, NULL);
ecf709
 }
ecf709
 
ecf709
+static errno_t
ecf709
+cache_req_enum_users_ncache_filter(struct sss_nc_ctx *ncache,
ecf709
+                                   struct sss_domain_info *domain,
ecf709
+                                   const char *name)
ecf709
+{
ecf709
+    return sss_ncache_check_user(ncache, domain, name);
ecf709
+}
ecf709
+
ecf709
 const struct cache_req_plugin cache_req_enum_users = {
ecf709
     .name = "Enumerate users",
ecf709
     .attr_expiration = SYSDB_CACHE_EXPIRE,
ecf709
@@ -75,7 +83,7 @@ const struct cache_req_plugin cache_req_enum_users = {
ecf709
     .global_ncache_add_fn = NULL,
ecf709
     .ncache_check_fn = NULL,
ecf709
     .ncache_add_fn = NULL,
ecf709
-    .ncache_filter_fn = NULL,
ecf709
+    .ncache_filter_fn = cache_req_enum_users_ncache_filter,
ecf709
     .lookup_fn = cache_req_enum_users_lookup,
ecf709
     .dp_send_fn = cache_req_enum_users_dp_send,
ecf709
     .dp_recv_fn = cache_req_common_dp_recv
ecf709
diff --git a/src/responder/common/cache_req/plugins/cache_req_group_by_id.c b/src/responder/common/cache_req/plugins/cache_req_group_by_id.c
ecf709
index 5613bf67c6acd1b2ace00cf75221462f45ef6743..5ca64283a781318bc4e4d6920fff989c3f3919b4 100644
ecf709
--- a/src/responder/common/cache_req/plugins/cache_req_group_by_id.c
ecf709
+++ b/src/responder/common/cache_req/plugins/cache_req_group_by_id.c
ecf709
@@ -43,6 +43,14 @@ cache_req_group_by_id_ncache_check(struct sss_nc_ctx *ncache,
ecf709
 }
ecf709
 
ecf709
 static errno_t
ecf709
+cache_req_group_by_id_ncache_filter(struct sss_nc_ctx *ncache,
ecf709
+                                    struct sss_domain_info *domain,
ecf709
+                                    const char *name)
ecf709
+{
ecf709
+    return sss_ncache_check_group(ncache, domain, name);
ecf709
+}
ecf709
+
ecf709
+static errno_t
ecf709
 cache_req_group_by_id_global_ncache_add(struct sss_nc_ctx *ncache,
ecf709
                                         struct cache_req_data *data)
ecf709
 {
ecf709
@@ -144,7 +152,7 @@ const struct cache_req_plugin cache_req_group_by_id = {
ecf709
     .global_ncache_add_fn = cache_req_group_by_id_global_ncache_add,
ecf709
     .ncache_check_fn = cache_req_group_by_id_ncache_check,
ecf709
     .ncache_add_fn = NULL,
ecf709
-    .ncache_filter_fn = NULL,
ecf709
+    .ncache_filter_fn = cache_req_group_by_id_ncache_filter,
ecf709
     .lookup_fn = cache_req_group_by_id_lookup,
ecf709
     .dp_send_fn = cache_req_group_by_id_dp_send,
ecf709
     .dp_recv_fn = cache_req_common_dp_recv
ecf709
diff --git a/src/responder/common/cache_req/plugins/cache_req_object_by_id.c b/src/responder/common/cache_req/plugins/cache_req_object_by_id.c
ecf709
index ff3d0e67862be365c56ab24396b4982e8addded0..339bd4f5fef827acc1aa3c123d041e426d9e4782 100644
ecf709
--- a/src/responder/common/cache_req/plugins/cache_req_object_by_id.c
ecf709
+++ b/src/responder/common/cache_req/plugins/cache_req_object_by_id.c
ecf709
@@ -50,6 +50,21 @@ cache_req_object_by_id_ncache_check(struct sss_nc_ctx *ncache,
ecf709
 }
ecf709
 
ecf709
 static errno_t
ecf709
+cache_req_object_by_id_ncache_filter(struct sss_nc_ctx *ncache,
ecf709
+                                     struct sss_domain_info *domain,
ecf709
+                                     const char *name)
ecf709
+{
ecf709
+    errno_t ret;
ecf709
+
ecf709
+    ret = sss_ncache_check_user(ncache, domain, name);
ecf709
+    if (ret == EEXIST) {
ecf709
+        ret = sss_ncache_check_group(ncache, domain, name);
ecf709
+    }
ecf709
+
ecf709
+    return ret;
ecf709
+}
ecf709
+
ecf709
+static errno_t
ecf709
 cache_req_object_by_id_global_ncache_add(struct sss_nc_ctx *ncache,
ecf709
                                          struct cache_req_data *data)
ecf709
 {
ecf709
@@ -111,7 +126,7 @@ const struct cache_req_plugin cache_req_object_by_id = {
ecf709
     .global_ncache_add_fn = cache_req_object_by_id_global_ncache_add,
ecf709
     .ncache_check_fn = cache_req_object_by_id_ncache_check,
ecf709
     .ncache_add_fn = NULL,
ecf709
-    .ncache_filter_fn = NULL,
ecf709
+    .ncache_filter_fn = cache_req_object_by_id_ncache_filter,
ecf709
     .lookup_fn = cache_req_object_by_id_lookup,
ecf709
     .dp_send_fn = cache_req_object_by_id_dp_send,
ecf709
     .dp_recv_fn = cache_req_common_dp_recv
ecf709
diff --git a/src/responder/common/cache_req/plugins/cache_req_user_by_id.c b/src/responder/common/cache_req/plugins/cache_req_user_by_id.c
ecf709
index b14b3738aa7721723f524ebd46301a3a9a1c712f..913f9be5bcc2dfd074b52cb3b15fb6948826e831 100644
ecf709
--- a/src/responder/common/cache_req/plugins/cache_req_user_by_id.c
ecf709
+++ b/src/responder/common/cache_req/plugins/cache_req_user_by_id.c
ecf709
@@ -43,6 +43,14 @@ cache_req_user_by_id_ncache_check(struct sss_nc_ctx *ncache,
ecf709
 }
ecf709
 
ecf709
 static errno_t
ecf709
+cache_req_user_by_id_ncache_filter(struct sss_nc_ctx *ncache,
ecf709
+                                   struct sss_domain_info *domain,
ecf709
+                                   const char *name)
ecf709
+{
ecf709
+    return sss_ncache_check_user(ncache, domain, name);
ecf709
+}
ecf709
+
ecf709
+static errno_t
ecf709
 cache_req_user_by_id_global_ncache_add(struct sss_nc_ctx *ncache,
ecf709
                                        struct cache_req_data *data)
ecf709
 {
ecf709
@@ -144,7 +152,7 @@ const struct cache_req_plugin cache_req_user_by_id = {
ecf709
     .global_ncache_add_fn = cache_req_user_by_id_global_ncache_add,
ecf709
     .ncache_check_fn = cache_req_user_by_id_ncache_check,
ecf709
     .ncache_add_fn = NULL,
ecf709
-    .ncache_filter_fn = NULL,
ecf709
+    .ncache_filter_fn = cache_req_user_by_id_ncache_filter,
ecf709
     .lookup_fn = cache_req_user_by_id_lookup,
ecf709
     .dp_send_fn = cache_req_user_by_id_dp_send,
ecf709
     .dp_recv_fn = cache_req_common_dp_recv
ecf709
diff --git a/src/responder/nss/nss_protocol_grent.c b/src/responder/nss/nss_protocol_grent.c
ecf709
index 947463df93e188729959737efa4ac4f44a8459c4..ee228c722a153a1ba7aa8a1b30a1e551108424bb 100644
ecf709
--- a/src/responder/nss/nss_protocol_grent.c
ecf709
+++ b/src/responder/nss/nss_protocol_grent.c
ecf709
@@ -241,18 +241,6 @@ nss_protocol_fill_grent(struct nss_ctx *nss_ctx,
ecf709
             continue;
ecf709
         }
ecf709
 
ecf709
-        /* Check negative cache during enumeration. */
ecf709
-        if (cmd_ctx->enumeration) {
ecf709
-            ret = sss_ncache_check_group(nss_ctx->rctx->ncache,
ecf709
-                                         result->domain, name->str);
ecf709
-            if (ret == EEXIST) {
ecf709
-                DEBUG(SSSDBG_TRACE_FUNC,
ecf709
-                      "User [%s] filtered out! (negative cache)\n",
ecf709
-                      name->str);
ecf709
-                continue;
ecf709
-            }
ecf709
-        }
ecf709
-
ecf709
         /* Adjust packet size: gid, num_members + string fields. */
ecf709
 
ecf709
         ret = sss_packet_grow(packet, 2 * sizeof(uint32_t)
ecf709
diff --git a/src/responder/nss/nss_protocol_pwent.c b/src/responder/nss/nss_protocol_pwent.c
ecf709
index cb643f29e2d5f0a0c55c51afd9def73813061aa7..b355d4fc90397f51e82545e56940be850f144d49 100644
ecf709
--- a/src/responder/nss/nss_protocol_pwent.c
ecf709
+++ b/src/responder/nss/nss_protocol_pwent.c
ecf709
@@ -309,17 +309,6 @@ nss_protocol_fill_pwent(struct nss_ctx *nss_ctx,
ecf709
             continue;
ecf709
         }
ecf709
 
ecf709
-        /* Check negative cache during enumeration. */
ecf709
-        if (cmd_ctx->enumeration) {
ecf709
-            ret = sss_ncache_check_user(nss_ctx->rctx->ncache,
ecf709
-                                        result->domain, name->str);
ecf709
-            if (ret == EEXIST) {
ecf709
-                DEBUG(SSSDBG_TRACE_FUNC,
ecf709
-                      "User [%s] filtered out! (negative cache)\n", name->str);
ecf709
-                continue;
ecf709
-            }
ecf709
-        }
ecf709
-
ecf709
         /* Adjust packet size: uid, gid + string fields. */
ecf709
 
ecf709
         ret = sss_packet_grow(packet, 2 * sizeof(uint32_t)
ecf709
-- 
ecf709
2.9.3
ecf709