Blame SOURCES/0077-cache_req-add-autofs-entry-by-name-plugin.patch

5fca41
From 6fe479a210adb53d419ca75512dec3a584296289 Mon Sep 17 00:00:00 2001
5fca41
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
5fca41
Date: Tue, 13 Aug 2019 13:00:23 +0200
5fca41
Subject: [PATCH 77/90] cache_req: add autofs entry by name plugin
5fca41
MIME-Version: 1.0
5fca41
Content-Type: text/plain; charset=UTF-8
5fca41
Content-Transfer-Encoding: 8bit
5fca41
5fca41
We will use the current data provider call that downloads all map entries
5fca41
but we will replace it later when new call is available.
5fca41
5fca41
Resolves:
5fca41
https://pagure.io/SSSD/sssd/issue/2607
5fca41
5fca41
Reviewed-by: Tomáš Halman <thalman@redhat.com>
5fca41
---
5fca41
 Makefile.am                                   |   1 +
5fca41
 src/responder/common/cache_req/cache_req.c    |   3 +-
5fca41
 src/responder/common/cache_req/cache_req.h    |  21 +++
5fca41
 .../common/cache_req/cache_req_data.c         |  33 +++++
5fca41
 .../common/cache_req/cache_req_plugin.h       |   1 +
5fca41
 .../common/cache_req/cache_req_private.h      |   1 +
5fca41
 .../plugins/cache_req_autofs_entry_by_name.c  | 129 ++++++++++++++++++
5fca41
 src/tests/cwrap/Makefile.am                   |   1 +
5fca41
 8 files changed, 189 insertions(+), 1 deletion(-)
5fca41
 create mode 100644 src/responder/common/cache_req/plugins/cache_req_autofs_entry_by_name.c
5fca41
5fca41
diff --git a/Makefile.am b/Makefile.am
5fca41
index 461a9355f..bc74906a7 100644
5fca41
--- a/Makefile.am
5fca41
+++ b/Makefile.am
5fca41
@@ -561,6 +561,7 @@ SSSD_CACHE_REQ_OBJ = \
5fca41
 	src/responder/common/cache_req/plugins/cache_req_host_by_name.c \
5fca41
 	src/responder/common/cache_req/plugins/cache_req_autofs_map_entries.c \
5fca41
 	src/responder/common/cache_req/plugins/cache_req_autofs_map_by_name.c \
5fca41
+	src/responder/common/cache_req/plugins/cache_req_autofs_entry_by_name.c \
5fca41
 	$(NULL)
5fca41
 
5fca41
 SSSD_RESPONDER_IFACE_OBJ = \
5fca41
diff --git a/src/responder/common/cache_req/cache_req.c b/src/responder/common/cache_req/cache_req.c
5fca41
index 75f396a23..febca2468 100644
5fca41
--- a/src/responder/common/cache_req/cache_req.c
5fca41
+++ b/src/responder/common/cache_req/cache_req.c
5fca41
@@ -62,7 +62,8 @@ cache_req_get_plugin(enum cache_req_type type)
5fca41
         &cache_req_host_by_name,
5fca41
 
5fca41
         &cache_req_autofs_map_entries,
5fca41
-        &cache_req_autofs_map_by_name
5fca41
+        &cache_req_autofs_map_by_name,
5fca41
+        &cache_req_autofs_entry_by_name
5fca41
     };
5fca41
 
5fca41
     if (type >= CACHE_REQ_SENTINEL) {
5fca41
diff --git a/src/responder/common/cache_req/cache_req.h b/src/responder/common/cache_req/cache_req.h
5fca41
index 66a13445c..412f6221b 100644
5fca41
--- a/src/responder/common/cache_req/cache_req.h
5fca41
+++ b/src/responder/common/cache_req/cache_req.h
5fca41
@@ -56,6 +56,7 @@ enum cache_req_type {
5fca41
 
5fca41
     CACHE_REQ_AUTOFS_MAP_ENTRIES,
5fca41
     CACHE_REQ_AUTOFS_MAP_BY_NAME,
5fca41
+    CACHE_REQ_AUTOFS_ENTRY_BY_NAME,
5fca41
 
5fca41
     CACHE_REQ_SENTINEL
5fca41
 };
5fca41
@@ -126,6 +127,13 @@ cache_req_data_host(TALLOC_CTX *mem_ctx,
5fca41
                     const char *name,
5fca41
                     const char *alias,
5fca41
                     const char **attrs);
5fca41
+
5fca41
+struct cache_req_data *
5fca41
+cache_req_data_autofs_entry(TALLOC_CTX *mem_ctx,
5fca41
+                            enum cache_req_type type,
5fca41
+                            const char *mapname,
5fca41
+                            const char *entryname);
5fca41
+
5fca41
 void
5fca41
 cache_req_data_set_bypass_cache(struct cache_req_data *data,
5fca41
                                 bool bypass_cache);
5fca41
@@ -457,4 +465,17 @@ cache_req_autofs_map_by_name_send(TALLOC_CTX *mem_ctx,
5fca41
 #define cache_req_autofs_map_by_name_recv(mem_ctx, req, _result) \
5fca41
     cache_req_single_domain_recv(mem_ctx, req, _result)
5fca41
 
5fca41
+struct tevent_req *
5fca41
+cache_req_autofs_entry_by_name_send(TALLOC_CTX *mem_ctx,
5fca41
+                                    struct tevent_context *ev,
5fca41
+                                    struct resp_ctx *rctx,
5fca41
+                                    struct sss_nc_ctx *ncache,
5fca41
+                                    int cache_refresh_percent,
5fca41
+                                    const char *domain,
5fca41
+                                    const char *mapname,
5fca41
+                                    const char *entryname);
5fca41
+
5fca41
+#define cache_req_autofs_entry_by_name_recv(mem_ctx, req, _result) \
5fca41
+    cache_req_single_domain_recv(mem_ctx, req, _result)
5fca41
+
5fca41
 #endif /* _CACHE_REQ_H_ */
5fca41
diff --git a/src/responder/common/cache_req/cache_req_data.c b/src/responder/common/cache_req/cache_req_data.c
5fca41
index ad6176641..01eeedbc4 100644
5fca41
--- a/src/responder/common/cache_req/cache_req_data.c
5fca41
+++ b/src/responder/common/cache_req/cache_req_data.c
5fca41
@@ -209,6 +209,25 @@ cache_req_data_create(TALLOC_CTX *mem_ctx,
5fca41
             goto done;
5fca41
         }
5fca41
         break;
5fca41
+    case CACHE_REQ_AUTOFS_ENTRY_BY_NAME:
5fca41
+        if (input->name.input == NULL) {
5fca41
+            DEBUG(SSSDBG_CRIT_FAILURE, "Bug: name cannot be NULL!\n");
5fca41
+            ret = ERR_INTERNAL;
5fca41
+            goto done;
5fca41
+        }
5fca41
+
5fca41
+        data->name.input = talloc_strdup(data, input->name.input);
5fca41
+        if (data->name.input == NULL) {
5fca41
+            ret = ENOMEM;
5fca41
+            goto done;
5fca41
+        }
5fca41
+
5fca41
+        data->autofs_entry_name = talloc_strdup(data, input->autofs_entry_name);
5fca41
+        if (data->autofs_entry_name == NULL) {
5fca41
+            ret = ENOMEM;
5fca41
+            goto done;
5fca41
+        }
5fca41
+        break;
5fca41
     case CACHE_REQ_SENTINEL:
5fca41
         DEBUG(SSSDBG_CRIT_FAILURE, "Invalid cache request type!\n");
5fca41
         ret = ERR_INTERNAL;
5fca41
@@ -356,6 +375,20 @@ cache_req_data_host(TALLOC_CTX *mem_ctx,
5fca41
     return cache_req_data_create(mem_ctx, type, &input);
5fca41
 }
5fca41
 
5fca41
+struct cache_req_data *
5fca41
+cache_req_data_autofs_entry(TALLOC_CTX *mem_ctx,
5fca41
+                            enum cache_req_type type,
5fca41
+                            const char *mapname,
5fca41
+                            const char *entryname)
5fca41
+{
5fca41
+    struct cache_req_data input = {0};
5fca41
+
5fca41
+    input.name.input = mapname;
5fca41
+    input.autofs_entry_name = entryname;
5fca41
+
5fca41
+    return cache_req_data_create(mem_ctx, type, &input);
5fca41
+}
5fca41
+
5fca41
 void
5fca41
 cache_req_data_set_bypass_cache(struct cache_req_data *data,
5fca41
                                 bool bypass_cache)
5fca41
diff --git a/src/responder/common/cache_req/cache_req_plugin.h b/src/responder/common/cache_req/cache_req_plugin.h
5fca41
index c040bf380..745a36c0e 100644
5fca41
--- a/src/responder/common/cache_req/cache_req_plugin.h
5fca41
+++ b/src/responder/common/cache_req/cache_req_plugin.h
5fca41
@@ -316,5 +316,6 @@ extern const struct cache_req_plugin cache_req_netgroup_by_name;
5fca41
 extern const struct cache_req_plugin cache_req_host_by_name;
5fca41
 extern const struct cache_req_plugin cache_req_autofs_map_entries;
5fca41
 extern const struct cache_req_plugin cache_req_autofs_map_by_name;
5fca41
+extern const struct cache_req_plugin cache_req_autofs_entry_by_name;
5fca41
 
5fca41
 #endif /* _CACHE_REQ_PLUGIN_H_ */
5fca41
diff --git a/src/responder/common/cache_req/cache_req_private.h b/src/responder/common/cache_req/cache_req_private.h
5fca41
index a88c838d9..34bd3ce15 100644
5fca41
--- a/src/responder/common/cache_req/cache_req_private.h
5fca41
+++ b/src/responder/common/cache_req/cache_req_private.h
5fca41
@@ -83,6 +83,7 @@ struct cache_req_data {
5fca41
     const char *sid;
5fca41
     const char *alias;
5fca41
     const char **attrs;
5fca41
+    const char *autofs_entry_name;
5fca41
 
5fca41
     struct {
5fca41
         struct cache_req_parsed_name *name;
5fca41
diff --git a/src/responder/common/cache_req/plugins/cache_req_autofs_entry_by_name.c b/src/responder/common/cache_req/plugins/cache_req_autofs_entry_by_name.c
5fca41
new file mode 100644
5fca41
index 000000000..17b0b508e
5fca41
--- /dev/null
5fca41
+++ b/src/responder/common/cache_req/plugins/cache_req_autofs_entry_by_name.c
5fca41
@@ -0,0 +1,129 @@
5fca41
+/*
5fca41
+    Authors:
5fca41
+        Pavel Březina <pbrezina@redhat.com>
5fca41
+
5fca41
+    Copyright (C) 2019 Red Hat
5fca41
+
5fca41
+    This program is free software; you can redistribute it and/or modify
5fca41
+    it under the terms of the GNU General Public License as published by
5fca41
+    the Free Software Foundation; either version 3 of the License, or
5fca41
+    (at your option) any later version.
5fca41
+
5fca41
+    This program is distributed in the hope that it will be useful,
5fca41
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
5fca41
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5fca41
+    GNU General Public License for more details.
5fca41
+
5fca41
+    You should have received a copy of the GNU General Public License
5fca41
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
5fca41
+*/
5fca41
+
5fca41
+#include <talloc.h>
5fca41
+#include <ldb.h>
5fca41
+
5fca41
+#include "db/sysdb.h"
5fca41
+#include "db/sysdb_autofs.h"
5fca41
+#include "util/util.h"
5fca41
+#include "providers/data_provider.h"
5fca41
+#include "responder/common/cache_req/cache_req_plugin.h"
5fca41
+
5fca41
+static const char *
5fca41
+cache_req_autofs_entry_by_name_create_debug_name(TALLOC_CTX *mem_ctx,
5fca41
+                                                 struct cache_req_data *data,
5fca41
+                                                 struct sss_domain_info *domain)
5fca41
+{
5fca41
+    return talloc_asprintf(mem_ctx, "%s:%s",
5fca41
+                           data->name.name,
5fca41
+                           data->autofs_entry_name);
5fca41
+}
5fca41
+
5fca41
+static errno_t
5fca41
+cache_req_autofs_entry_by_name_lookup(TALLOC_CTX *mem_ctx,
5fca41
+                                      struct cache_req *cr,
5fca41
+                                      struct cache_req_data *data,
5fca41
+                                      struct sss_domain_info *domain,
5fca41
+                                      struct ldb_result **_result)
5fca41
+{
5fca41
+    struct ldb_message *entry;
5fca41
+    struct ldb_result *result;
5fca41
+    errno_t ret;
5fca41
+
5fca41
+    ret = sysdb_get_autofsentry(mem_ctx, domain, data->name.name,
5fca41
+                                data->autofs_entry_name, &entry);
5fca41
+    if (ret != EOK) {
5fca41
+        return ret;
5fca41
+    }
5fca41
+
5fca41
+    result = cache_req_create_ldb_result_from_msg(mem_ctx, entry);
5fca41
+    if (result == NULL) {
5fca41
+        talloc_free(entry);
5fca41
+        return ENOMEM;
5fca41
+    }
5fca41
+
5fca41
+    *_result = talloc_steal(mem_ctx, result);
5fca41
+    return EOK;
5fca41
+}
5fca41
+
5fca41
+static struct tevent_req *
5fca41
+cache_req_autofs_entry_by_name_dp_send(TALLOC_CTX *mem_ctx,
5fca41
+                                       struct cache_req *cr,
5fca41
+                                       struct cache_req_data *data,
5fca41
+                                       struct sss_domain_info *domain,
5fca41
+                                       struct ldb_result *result)
5fca41
+{
5fca41
+    return sss_dp_get_autofs_send(mem_ctx, cr->rctx, domain, true,
5fca41
+                                  SSS_DP_AUTOFS, data->name.name);
5fca41
+}
5fca41
+
5fca41
+const struct cache_req_plugin cache_req_autofs_entry_by_name = {
5fca41
+    .name = "Get autofs entry",
5fca41
+    .attr_expiration = SYSDB_CACHE_EXPIRE,
5fca41
+    .parse_name = true,
5fca41
+    .ignore_default_domain = true,
5fca41
+    .bypass_cache = false,
5fca41
+    .only_one_result = false,
5fca41
+    .search_all_domains = false,
5fca41
+    .require_enumeration = false,
5fca41
+    .allow_missing_fqn = true,
5fca41
+    .allow_switch_to_upn = false,
5fca41
+    .upn_equivalent = CACHE_REQ_SENTINEL,
5fca41
+    .get_next_domain_flags = 0,
5fca41
+
5fca41
+    .is_well_known_fn = NULL,
5fca41
+    .prepare_domain_data_fn = NULL,
5fca41
+    .create_debug_name_fn = cache_req_autofs_entry_by_name_create_debug_name,
5fca41
+    .global_ncache_add_fn = NULL,
5fca41
+    .ncache_check_fn = NULL,
5fca41
+    .ncache_add_fn = NULL,
5fca41
+    .ncache_filter_fn = NULL,
5fca41
+    .lookup_fn = cache_req_autofs_entry_by_name_lookup,
5fca41
+    .dp_send_fn = cache_req_autofs_entry_by_name_dp_send,
5fca41
+    .dp_recv_fn = cache_req_common_dp_recv,
5fca41
+    .dp_get_domain_check_fn = NULL,
5fca41
+    .dp_get_domain_send_fn = NULL,
5fca41
+    .dp_get_domain_recv_fn = NULL,
5fca41
+};
5fca41
+
5fca41
+struct tevent_req *
5fca41
+cache_req_autofs_entry_by_name_send(TALLOC_CTX *mem_ctx,
5fca41
+                                    struct tevent_context *ev,
5fca41
+                                    struct resp_ctx *rctx,
5fca41
+                                    struct sss_nc_ctx *ncache,
5fca41
+                                    int cache_refresh_percent,
5fca41
+                                    const char *domain,
5fca41
+                                    const char *mapname,
5fca41
+                                    const char *entryname)
5fca41
+{
5fca41
+    struct cache_req_data *data;
5fca41
+
5fca41
+    data = cache_req_data_autofs_entry(mem_ctx, CACHE_REQ_AUTOFS_ENTRY_BY_NAME,
5fca41
+                                       mapname, entryname);
5fca41
+    if (data == NULL) {
5fca41
+        return NULL;
5fca41
+    }
5fca41
+
5fca41
+    return cache_req_steal_data_and_send(mem_ctx, ev, rctx, ncache,
5fca41
+                                         cache_refresh_percent,
5fca41
+                                         CACHE_REQ_POSIX_DOM, domain,
5fca41
+                                         data);
5fca41
+}
5fca41
diff --git a/src/tests/cwrap/Makefile.am b/src/tests/cwrap/Makefile.am
5fca41
index 3fbcebf3c..aaec6c700 100644
5fca41
--- a/src/tests/cwrap/Makefile.am
5fca41
+++ b/src/tests/cwrap/Makefile.am
5fca41
@@ -66,6 +66,7 @@ SSSD_CACHE_REQ_OBJ = \
5fca41
     ../../../src/responder/common/cache_req/plugins/cache_req_host_by_name.c \
5fca41
     ../../../src/responder/common/cache_req/plugins/cache_req_autofs_map_entries.c \
5fca41
     ../../../src/responder/common/cache_req/plugins/cache_req_autofs_map_by_name.c \
5fca41
+    ../../../src/responder/common/cache_req/plugins/cache_req_autofs_entry_by_name.c \
5fca41
     $(NULL)
5fca41
 
5fca41
 SSSD_RESPONDER_IFACE_OBJ = \
5fca41
-- 
5fca41
2.20.1
5fca41