dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone
Blob Blame History Raw
From 01b7dc92180cf80080f657b80f72cec20eafa0e7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Tue, 13 Aug 2019 13:31:21 +0200
Subject: [PATCH 75/90] cache_req: add autofs map entries plugin
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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    |   2 +
 src/responder/common/cache_req/cache_req.h    |  14 ++
 .../common/cache_req/cache_req_data.c         |   1 +
 .../common/cache_req/cache_req_plugin.h       |   1 +
 .../plugins/cache_req_autofs_map_entries.c    | 156 ++++++++++++++++++
 src/tests/cwrap/Makefile.am                   |   1 +
 7 files changed, 176 insertions(+)
 create mode 100644 src/responder/common/cache_req/plugins/cache_req_autofs_map_entries.c

diff --git a/Makefile.am b/Makefile.am
index c109afe56..879f38311 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -559,6 +559,7 @@ SSSD_CACHE_REQ_OBJ = \
 	src/responder/common/cache_req/plugins/cache_req_svc_by_port.c \
 	src/responder/common/cache_req/plugins/cache_req_netgroup_by_name.c \
 	src/responder/common/cache_req/plugins/cache_req_host_by_name.c \
+	src/responder/common/cache_req/plugins/cache_req_autofs_map_entries.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 28b563392..2ce9deca0 100644
--- a/src/responder/common/cache_req/cache_req.c
+++ b/src/responder/common/cache_req/cache_req.c
@@ -60,6 +60,8 @@ cache_req_get_plugin(enum cache_req_type type)
         &cache_req_netgroup_by_name,
 
         &cache_req_host_by_name,
+
+        &cache_req_autofs_map_entries,
     };
 
     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 84dd22c25..0c214a483 100644
--- a/src/responder/common/cache_req/cache_req.h
+++ b/src/responder/common/cache_req/cache_req.h
@@ -54,6 +54,8 @@ enum cache_req_type {
 
     CACHE_REQ_HOST_BY_NAME,
 
+    CACHE_REQ_AUTOFS_MAP_ENTRIES,
+
     CACHE_REQ_SENTINEL
 };
 
@@ -430,4 +432,16 @@ cache_req_host_by_name_send(TALLOC_CTX *mem_ctx,
 #define cache_req_host_by_name_recv(mem_ctx, req, _result) \
     cache_req_single_domain_recv(mem_ctx, req, _result)
 
+struct tevent_req *
+cache_req_autofs_map_entries_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 *name);
+
+#define cache_req_autofs_map_entries_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 77959f9fe..20d73ebfd 100644
--- a/src/responder/common/cache_req/cache_req_data.c
+++ b/src/responder/common/cache_req/cache_req_data.c
@@ -93,6 +93,7 @@ cache_req_data_create(TALLOC_CTX *mem_ctx,
     case CACHE_REQ_INITGROUPS_BY_UPN:
     case CACHE_REQ_NETGROUP_BY_NAME:
     case CACHE_REQ_OBJECT_BY_NAME:
+    case CACHE_REQ_AUTOFS_MAP_ENTRIES:
         if (input->name.input == NULL) {
             DEBUG(SSSDBG_CRIT_FAILURE, "Bug: name cannot be NULL!\n");
             ret = ERR_INTERNAL;
diff --git a/src/responder/common/cache_req/cache_req_plugin.h b/src/responder/common/cache_req/cache_req_plugin.h
index d547c9bf3..1071cd889 100644
--- a/src/responder/common/cache_req/cache_req_plugin.h
+++ b/src/responder/common/cache_req/cache_req_plugin.h
@@ -314,5 +314,6 @@ extern const struct cache_req_plugin cache_req_svc_by_name;
 extern const struct cache_req_plugin cache_req_svc_by_port;
 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;
 
 #endif /* _CACHE_REQ_PLUGIN_H_ */
diff --git a/src/responder/common/cache_req/plugins/cache_req_autofs_map_entries.c b/src/responder/common/cache_req/plugins/cache_req_autofs_map_entries.c
new file mode 100644
index 000000000..73d2b3cf2
--- /dev/null
+++ b/src/responder/common/cache_req/plugins/cache_req_autofs_map_entries.c
@@ -0,0 +1,156 @@
+/*
+    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_map_entries_create_debug_name(TALLOC_CTX *mem_ctx,
+                                               struct cache_req_data *data,
+                                               struct sss_domain_info *domain)
+{
+    return talloc_strdup(mem_ctx, data->name.name);
+}
+
+static errno_t
+cache_req_autofs_map_entries_lookup(TALLOC_CTX *mem_ctx,
+                                    struct cache_req *cr,
+                                    struct cache_req_data *data,
+                                    struct sss_domain_info *domain,
+                                    struct ldb_result **_result)
+{
+    TALLOC_CTX *tmp_ctx;
+    struct ldb_message *map;
+    struct ldb_message **mounts;
+    struct ldb_message **msgs;
+    struct ldb_result *result;
+    size_t count;
+    size_t i;
+    errno_t ret;
+
+    tmp_ctx = talloc_new(NULL);
+    if (tmp_ctx == NULL) {
+        return ENOMEM;
+    }
+
+    ret = sysdb_get_map_byname(tmp_ctx, domain, data->name.name, &map);
+    if (ret != EOK) {
+        goto done;
+    }
+
+    ret = sysdb_autofs_entries_by_map(tmp_ctx, domain, data->name.name,
+                                      &count, &mounts);
+    if (ret != EOK && ret != ENOENT) {
+        goto done;
+    }
+
+    msgs = talloc_zero_array(tmp_ctx, struct ldb_message *, count + 1);
+    if (msgs == NULL) {
+        ret = ENOMEM;
+        goto done;
+    }
+
+    msgs[0] = talloc_steal(msgs, map);
+    for (i = 0; i < count; i++) {
+        msgs[i + 1] = talloc_steal(msgs, mounts[i]);
+    }
+
+    result = cache_req_create_ldb_result_from_msg_list(tmp_ctx, msgs, count + 1);
+    if (result == NULL) {
+        ret = ENOMEM;
+        goto done;
+    }
+
+    *_result = talloc_steal(mem_ctx, result);
+    ret = EOK;
+
+done:
+    talloc_free(tmp_ctx);
+
+    return ret;
+}
+
+static struct tevent_req *
+cache_req_autofs_map_entries_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_map_entries = {
+    .name = "Get autofs entries",
+    .attr_expiration = SYSDB_ENUM_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_map_entries_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_map_entries_lookup,
+    .dp_send_fn = cache_req_autofs_map_entries_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_map_entries_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 *name)
+{
+    struct cache_req_data *data;
+
+    data = cache_req_data_name(mem_ctx, CACHE_REQ_AUTOFS_MAP_ENTRIES, name);
+    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 1edefc678..dfaf3f770 100644
--- a/src/tests/cwrap/Makefile.am
+++ b/src/tests/cwrap/Makefile.am
@@ -64,6 +64,7 @@ SSSD_CACHE_REQ_OBJ = \
     ../../../src/responder/common/cache_req/plugins/cache_req_svc_by_port.c \
     ../../../src/responder/common/cache_req/plugins/cache_req_netgroup_by_name.c \
     ../../../src/responder/common/cache_req/plugins/cache_req_host_by_name.c \
+    ../../../src/responder/common/cache_req/plugins/cache_req_autofs_map_entries.c \
     $(NULL)
 
 SSSD_RESPONDER_IFACE_OBJ = \
-- 
2.20.1