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

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