dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone
Blob Blame History Raw
From 2e452583728b2596aba65a96d25f7f8312765538 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Thu, 15 Aug 2019 14:07:01 +0200
Subject: [PATCH 86/90] ldap: implement autofs get entry
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>
---
 src/providers/ipa/ipa_autofs.c                |   4 +
 src/providers/ldap/ldap_common.h              |  11 +
 src/providers/ldap/sdap_async_autofs.c        | 235 +++++++++++++++++-
 src/providers/ldap/sdap_autofs.c              |  88 +++++++
 src/providers/ldap/sdap_autofs.h              |   8 +
 .../plugins/cache_req_autofs_entry_by_name.c  |   4 +-
 6 files changed, 347 insertions(+), 3 deletions(-)

diff --git a/src/providers/ipa/ipa_autofs.c b/src/providers/ipa/ipa_autofs.c
index 19d74071f..5f72d60cd 100644
--- a/src/providers/ipa/ipa_autofs.c
+++ b/src/providers/ipa/ipa_autofs.c
@@ -55,5 +55,9 @@ errno_t ipa_autofs_init(TALLOC_CTX *mem_ctx,
                   sdap_autofs_get_map_handler_send, sdap_autofs_get_map_handler_recv, id_ctx,
                   struct sdap_id_ctx, struct dp_autofs_data, struct dp_reply_std);
 
+    dp_set_method(dp_methods, DPM_AUTOFS_GET_ENTRY,
+                  sdap_autofs_get_entry_handler_send, sdap_autofs_get_entry_handler_recv, id_ctx,
+                  struct sdap_id_ctx, struct dp_autofs_data, struct dp_reply_std);
+
     return ret;
 }
diff --git a/src/providers/ldap/ldap_common.h b/src/providers/ldap/ldap_common.h
index 36623aca8..893f29a38 100644
--- a/src/providers/ldap/ldap_common.h
+++ b/src/providers/ldap/ldap_common.h
@@ -177,6 +177,17 @@ sdap_autofs_get_map_handler_recv(TALLOC_CTX *mem_ctx,
                                  struct tevent_req *req,
                                  struct dp_reply_std *data);
 
+struct tevent_req *
+sdap_autofs_get_entry_handler_send(TALLOC_CTX *mem_ctx,
+                                 struct sdap_id_ctx *id_ctx,
+                                 struct dp_autofs_data *data,
+                                 struct dp_req_params *params);
+
+errno_t
+sdap_autofs_get_entry_handler_recv(TALLOC_CTX *mem_ctx,
+                                   struct tevent_req *req,
+                                   struct dp_reply_std *data);
+
 int sdap_service_init(TALLOC_CTX *memctx, struct be_ctx *ctx,
                       const char *service_name, const char *dns_service_name,
                       const char *urls, const char *backup_urls,
diff --git a/src/providers/ldap/sdap_async_autofs.c b/src/providers/ldap/sdap_async_autofs.c
index 52ceb84ac..c31df2f59 100644
--- a/src/providers/ldap/sdap_async_autofs.c
+++ b/src/providers/ldap/sdap_async_autofs.c
@@ -1100,7 +1100,7 @@ static void sdap_autofs_get_map_connect_done(struct tevent_req *subreq)
                     state->opts->sdom->autofs_search_bases,
                     state->opts->autofs_mobject_map, false,
                     dp_opt_get_int(state->opts->basic, SDAP_SEARCH_TIMEOUT),
-                    filter, attrs);
+                    filter, attrs, NULL);
     if (subreq == NULL) {
         state->dp_error = DP_ERR_FATAL;
         tevent_req_error(req, ENOMEM);
@@ -1177,3 +1177,236 @@ errno_t sdap_autofs_get_map_recv(struct tevent_req *req,
 
     return EOK;
 }
+
+struct sdap_autofs_get_entry_state {
+    struct sdap_id_ctx *id_ctx;
+    struct sdap_options *opts;
+    struct sdap_id_op *sdap_op;
+    const char *mapname;
+    const char *entryname;
+    int dp_error;
+};
+
+static errno_t sdap_autofs_get_entry_retry(struct tevent_req *req);
+static void sdap_autofs_get_entry_connect_done(struct tevent_req *subreq);
+static void sdap_autofs_get_entry_done(struct tevent_req *subreq);
+
+struct tevent_req *sdap_autofs_get_entry_send(TALLOC_CTX *mem_ctx,
+                                              struct sdap_id_ctx *id_ctx,
+                                              const char *mapname,
+                                              const char *entryname)
+{
+    struct tevent_req *req;
+    struct sdap_autofs_get_entry_state *state;
+    int ret;
+
+    req = tevent_req_create(mem_ctx, &state, struct sdap_autofs_get_entry_state);
+    if (!req) {
+        return NULL;
+    }
+
+    state->id_ctx = id_ctx;
+    state->opts = id_ctx->opts;
+    state->mapname = mapname;
+    state->entryname = entryname;
+    state->dp_error = DP_ERR_FATAL;
+
+    state->sdap_op = sdap_id_op_create(state, id_ctx->conn->conn_cache);
+    if (!state->sdap_op) {
+        DEBUG(SSSDBG_OP_FAILURE, "sdap_id_op_create() failed\n");
+        ret = ENOMEM;
+        goto done;
+    }
+
+    ret = sdap_autofs_get_entry_retry(req);
+    if (ret == EAGAIN) {
+        /* asynchronous processing */
+        return req;
+    }
+
+done:
+    if (ret == EOK) {
+        tevent_req_done(req);
+    } else {
+        tevent_req_error(req, ret);
+    }
+    tevent_req_post(req, id_ctx->be->ev);
+
+    return req;
+}
+
+static errno_t sdap_autofs_get_entry_retry(struct tevent_req *req)
+{
+    struct sdap_autofs_get_entry_state *state;
+    struct tevent_req *subreq;
+    int ret;
+
+    state = tevent_req_data(req, struct sdap_autofs_get_entry_state);
+
+    subreq = sdap_id_op_connect_send(state->sdap_op, state, &ret);
+    if (subreq == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "sdap_id_op_connect_send() failed: "
+                                   "%d(%s)\n", ret, strerror(ret));
+        return ret;
+    }
+
+    tevent_req_set_callback(subreq, sdap_autofs_get_entry_connect_done, req);
+
+    return EAGAIN;
+}
+
+static void sdap_autofs_get_entry_connect_done(struct tevent_req *subreq)
+{
+    struct tevent_req *req;
+    struct sdap_autofs_get_entry_state *state;
+    struct ldb_message *map;
+    char *filter;
+    char *safe_entryname;
+    const char **attrs;
+    const char *base_dn;
+    int dp_error;
+    int ret;
+
+    req = tevent_req_callback_data(subreq, struct tevent_req);
+    state = tevent_req_data(req, struct sdap_autofs_get_entry_state);
+
+    ret = sdap_id_op_connect_recv(subreq, &dp_error);
+    talloc_zfree(subreq);
+
+    if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "LDAP connection failed "
+                                   "[%d]: %s\n", ret, strerror(ret));
+        state->dp_error = dp_error;
+        tevent_req_error(req, ret);
+        return;
+    }
+
+    DEBUG(SSSDBG_TRACE_FUNC, "LDAP connection successful\n");
+
+    ret = sysdb_get_map_byname(state, state->id_ctx->be->domain,
+                               state->mapname, &map);
+    if (ret == ENOENT) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Map %s does not exist!\n", state->mapname);
+        tevent_req_error(req, ret);
+        return;
+    } else if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to get map %s [%d]: %s\n",
+              state->mapname, ret, sss_strerror(ret));
+        tevent_req_error(req, ret);
+        return;
+    }
+
+    base_dn = ldb_msg_find_attr_as_string(map, SYSDB_ORIG_DN, NULL);
+    if (base_dn == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Cannot get originalDN\n");
+        tevent_req_error(req, ERR_INTERNAL);
+        return;
+    }
+
+    ret = sss_filter_sanitize(state, state->entryname, &safe_entryname);
+    if (ret != EOK) {
+        tevent_req_error(req, ret);
+        return;
+    }
+
+    filter = talloc_asprintf(state, "(&(%s=%s)(objectclass=%s))",
+                 state->opts->autofs_entry_map[SDAP_AT_AUTOFS_ENTRY_KEY].name,
+                 safe_entryname,
+                 state->opts->autofs_entry_map[SDAP_OC_AUTOFS_ENTRY].name);
+    if (filter == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Failed to build filter\n");
+        tevent_req_error(req, ret);
+        return;
+    }
+
+    ret = build_attrs_from_map(state, state->opts->autofs_entry_map,
+                               SDAP_OPTS_AUTOFS_ENTRY, NULL, &attrs, NULL);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Failed to build attributes from map\n");
+        tevent_req_error(req, ret);
+        return;
+    }
+
+    subreq = sdap_search_bases_return_first_send(state, state->id_ctx->be->ev,
+                    state->opts, sdap_id_op_handle(state->sdap_op),
+                    state->opts->sdom->autofs_search_bases,
+                    state->opts->autofs_entry_map, false,
+                    dp_opt_get_int(state->opts->basic, SDAP_SEARCH_TIMEOUT),
+                    filter, attrs, base_dn);
+    if (subreq == NULL) {
+        state->dp_error = DP_ERR_FATAL;
+        tevent_req_error(req, ENOMEM);
+        return;
+    }
+
+    tevent_req_set_callback(subreq, sdap_autofs_get_entry_done, req);
+}
+
+static void sdap_autofs_get_entry_done(struct tevent_req *subreq)
+{
+    struct tevent_req *req;
+    struct sdap_autofs_get_entry_state *state;
+    struct sysdb_attrs **reply;
+    size_t reply_count;
+    errno_t ret;
+
+    req = tevent_req_callback_data(subreq, struct tevent_req);
+    state = tevent_req_data(req, struct sdap_autofs_get_entry_state);
+
+    ret = sdap_search_bases_return_first_recv(subreq, state, &reply_count,
+                                              &reply);
+    talloc_zfree(subreq);
+
+    ret = sdap_id_op_done(state->sdap_op, ret, &state->dp_error);
+    if (state->dp_error == DP_ERR_OK && ret != EOK) {
+        /* retry */
+        ret = sdap_autofs_get_entry_retry(req);
+        if (ret != EOK) {
+            tevent_req_error(req, ret);
+        }
+        return;
+    } else if (ret != EOK) {
+        tevent_req_error(req, ret);
+        return;
+    }
+
+    if (reply_count == 0) {
+        ret = sysdb_del_autofsentry_by_key(state->id_ctx->be->domain,
+                                           state->mapname, state->entryname);
+        if (ret != EOK) {
+            DEBUG(SSSDBG_MINOR_FAILURE, "Cannot delete entry %s:%s\n",
+                  state->mapname, state->entryname);
+            tevent_req_error(req, ret);
+            return;
+        }
+
+        tevent_req_done(req);
+        return;
+    }
+
+    ret = add_autofs_entry(state->id_ctx->be->domain, state->mapname,
+                           state->opts, reply[0], time(NULL));
+    if (ret != EOK) {
+        DEBUG(SSSDBG_OP_FAILURE,
+             "Cannot save autofs entry %s:%s [%d]: %s\n",
+              state->mapname, state->entryname, ret, strerror(ret));
+        tevent_req_error(req, ret);
+        return;
+    }
+
+    tevent_req_done(req);
+}
+
+errno_t sdap_autofs_get_entry_recv(struct tevent_req *req,
+                                   int *dp_error)
+{
+    struct sdap_autofs_get_entry_state *state;
+
+    state = tevent_req_data(req, struct sdap_autofs_get_entry_state);
+
+    TEVENT_REQ_RETURN_ON_ERROR(req);
+
+    *dp_error = state->dp_error;
+
+    return EOK;
+}
diff --git a/src/providers/ldap/sdap_autofs.c b/src/providers/ldap/sdap_autofs.c
index 9a5ed11e8..b1e4b5aef 100644
--- a/src/providers/ldap/sdap_autofs.c
+++ b/src/providers/ldap/sdap_autofs.c
@@ -391,6 +391,90 @@ sdap_autofs_get_map_handler_recv(TALLOC_CTX *mem_ctx,
     return EOK;
 }
 
+struct sdap_autofs_get_entry_handler_state {
+    struct dp_reply_std reply;
+};
+
+static void sdap_autofs_get_entry_handler_done(struct tevent_req *subreq);
+
+struct tevent_req *
+sdap_autofs_get_entry_handler_send(TALLOC_CTX *mem_ctx,
+                                 struct sdap_id_ctx *id_ctx,
+                                 struct dp_autofs_data *data,
+                                 struct dp_req_params *params)
+{
+    struct sdap_autofs_get_entry_handler_state *state;
+    struct tevent_req *subreq;
+    struct tevent_req *req;
+    errno_t ret;
+
+    req = tevent_req_create(mem_ctx, &state,
+                            struct sdap_autofs_get_entry_handler_state);
+    if (req == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "tevent_req_create() failed\n");
+        return NULL;
+    }
+
+    DEBUG(SSSDBG_FUNC_DATA, "Requested refresh for: %s:%s\n",
+          data->mapname, data->entryname);
+
+    subreq = sdap_autofs_get_entry_send(mem_ctx, id_ctx,
+                                        data->mapname, data->entryname);
+    if (subreq == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to send request for %s:%s.\n",
+              data->mapname, data->entryname);
+        ret = ENOMEM;
+        goto immediately;
+    }
+
+    tevent_req_set_callback(subreq, sdap_autofs_get_entry_handler_done, req);
+
+    return req;
+
+immediately:
+    dp_reply_std_set(&state->reply, DP_ERR_DECIDE, ret, NULL);
+
+    /* TODO For backward compatibility we always return EOK to DP now. */
+    tevent_req_done(req);
+    tevent_req_post(req, params->ev);
+
+    return req;
+}
+
+static void sdap_autofs_get_entry_handler_done(struct tevent_req *subreq)
+{
+    struct sdap_autofs_get_entry_handler_state *state;
+    struct tevent_req *req;
+    int dp_error;
+    errno_t ret;
+
+    req = tevent_req_callback_data(subreq, struct tevent_req);
+    state = tevent_req_data(req, struct sdap_autofs_get_entry_handler_state);
+
+    ret = sdap_autofs_get_entry_recv(subreq, &dp_error);
+    talloc_zfree(subreq);
+
+    /* TODO For backward compatibility we always return EOK to DP now. */
+    dp_reply_std_set(&state->reply, dp_error, ret, NULL);
+    tevent_req_done(req);
+}
+
+errno_t
+sdap_autofs_get_entry_handler_recv(TALLOC_CTX *mem_ctx,
+                                   struct tevent_req *req,
+                                   struct dp_reply_std *data)
+{
+    struct sdap_autofs_get_entry_handler_state *state = NULL;
+
+    state = tevent_req_data(req, struct sdap_autofs_get_entry_handler_state);
+
+    TEVENT_REQ_RETURN_ON_ERROR(req);
+
+    *data = state->reply;
+
+    return EOK;
+}
+
 errno_t sdap_autofs_init(TALLOC_CTX *mem_ctx,
                          struct be_ctx *be_ctx,
                          struct sdap_id_ctx *id_ctx,
@@ -414,5 +498,9 @@ errno_t sdap_autofs_init(TALLOC_CTX *mem_ctx,
                   sdap_autofs_get_map_handler_send, sdap_autofs_get_map_handler_recv, id_ctx,
                   struct sdap_id_ctx, struct dp_autofs_data, struct dp_reply_std);
 
+    dp_set_method(dp_methods, DPM_AUTOFS_GET_ENTRY,
+                  sdap_autofs_get_entry_handler_send, sdap_autofs_get_entry_handler_recv, id_ctx,
+                  struct sdap_id_ctx, struct dp_autofs_data, struct dp_reply_std);
+
     return EOK;
 }
diff --git a/src/providers/ldap/sdap_autofs.h b/src/providers/ldap/sdap_autofs.h
index 34b9ca953..210393746 100644
--- a/src/providers/ldap/sdap_autofs.h
+++ b/src/providers/ldap/sdap_autofs.h
@@ -50,5 +50,13 @@ struct tevent_req *sdap_autofs_get_map_send(TALLOC_CTX *mem_ctx,
 errno_t sdap_autofs_get_map_recv(struct tevent_req *req,
                                  int *dp_error);
 
+struct tevent_req *sdap_autofs_get_entry_send(TALLOC_CTX *mem_ctx,
+                                              struct sdap_id_ctx *id_ctx,
+                                              const char *mapname,
+                                              const char *entryname);
+
+errno_t sdap_autofs_get_entry_recv(struct tevent_req *req,
+                                   int *dp_error);
+
 #endif /* _SDAP_AUTOFS_H_ */
 
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
index f4d0cb140..422fe90c4 100644
--- 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
@@ -72,8 +72,8 @@ cache_req_autofs_entry_by_name_dp_send(TALLOC_CTX *mem_ctx,
                                        struct ldb_result *result)
 {
     return sss_dp_get_autofs_send(mem_ctx, cr->rctx, domain, true,
-                                  SSS_DP_AUTOFS_ENUMERATE,
-                                  data->name.name, NULL);
+                                  SSS_DP_AUTOFS_GET_ENTRY,
+                                  data->name.name, data->autofs_entry_name);
 }
 
 const struct cache_req_plugin cache_req_autofs_entry_by_name = {
-- 
2.20.1