Blob Blame History Raw
From 6fe479a210adb53d419ca75512dec3a584296289 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Tue, 13 Aug 2019 13:00:23 +0200
Subject: [PATCH 77/90] cache_req: add autofs entry by name plugin
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

We will use the current data provider call that downloads all map entries
but we will replace it later when new call is available.

Resolves:
https://pagure.io/SSSD/sssd/issue/2607

Reviewed-by: Tomáš Halman <thalman@redhat.com>
---
 Makefile.am                                   |   1 +
 src/responder/common/cache_req/cache_req.c    |   3 +-
 src/responder/common/cache_req/cache_req.h    |  21 +++
 .../common/cache_req/cache_req_data.c         |  33 +++++
 .../common/cache_req/cache_req_plugin.h       |   1 +
 .../common/cache_req/cache_req_private.h      |   1 +
 .../plugins/cache_req_autofs_entry_by_name.c  | 129 ++++++++++++++++++
 src/tests/cwrap/Makefile.am                   |   1 +
 8 files changed, 189 insertions(+), 1 deletion(-)
 create mode 100644 src/responder/common/cache_req/plugins/cache_req_autofs_entry_by_name.c

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