Blame SOURCES/0088-autofs-always-refresh-auto.master.patch

8d3578
From ac712654f07be9e9c05156c89af54eac483d75d6 Mon Sep 17 00:00:00 2001
8d3578
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
8d3578
Date: Mon, 2 Sep 2019 13:05:14 +0200
8d3578
Subject: [PATCH 88/90] autofs: always refresh auto.master
8d3578
MIME-Version: 1.0
8d3578
Content-Type: text/plain; charset=UTF-8
8d3578
Content-Transfer-Encoding: 8bit
8d3578
8d3578
Reviewed-by: Tomáš Halman <thalman@redhat.com>
8d3578
---
8d3578
 src/responder/autofs/autofs_private.h | 10 ++++-
8d3578
 src/responder/autofs/autofssrv.c      | 25 ++++++++----
8d3578
 src/responder/autofs/autofssrv_cmd.c  | 56 ++++++++++++++++++++++-----
8d3578
 3 files changed, 74 insertions(+), 17 deletions(-)
8d3578
8d3578
diff --git a/src/responder/autofs/autofs_private.h b/src/responder/autofs/autofs_private.h
8d3578
index 3be25d4d9..175a7768f 100644
8d3578
--- a/src/responder/autofs/autofs_private.h
8d3578
+++ b/src/responder/autofs/autofs_private.h
8d3578
@@ -21,6 +21,8 @@
8d3578
 #ifndef _AUTOFSSRV_PRIVATE_H_
8d3578
 #define _AUTOFSSRV_PRIVATE_H_
8d3578
 
8d3578
+#include <dhash.h>
8d3578
+
8d3578
 #include "responder/common/responder.h"
8d3578
 #include "responder/common/responder_sbus.h"
8d3578
 #include "responder/common/cache_req/cache_req.h"
8d3578
@@ -55,6 +57,12 @@ struct autofs_enum_ctx {
8d3578
     /* False if the result is being created. */
8d3578
     bool ready;
8d3578
 
8d3578
+    /* Enumeration context key. */
8d3578
+    const char *key;
8d3578
+
8d3578
+    /* Hash table that contains this enumeration context. */
8d3578
+    hash_table_t *table;
8d3578
+
8d3578
     /* Requests that awaits the data. */
8d3578
     struct setent_req_list *notify_list;
8d3578
 };
8d3578
@@ -62,6 +70,6 @@ struct autofs_enum_ctx {
8d3578
 struct sss_cmd_table *get_autofs_cmds(void);
8d3578
 int autofs_connection_setup(struct cli_ctx *cctx);
8d3578
 
8d3578
-errno_t autofs_orphan_maps(struct autofs_ctx *actx);
8d3578
+void autofs_orphan_maps(struct autofs_ctx *actx);
8d3578
 
8d3578
 #endif /* _AUTOFSSRV_PRIVATE_H_ */
8d3578
diff --git a/src/responder/autofs/autofssrv.c b/src/responder/autofs/autofssrv.c
8d3578
index 309ed76b1..922da5fd4 100644
8d3578
--- a/src/responder/autofs/autofssrv.c
8d3578
+++ b/src/responder/autofs/autofssrv.c
8d3578
@@ -85,17 +85,26 @@ static int autofs_clean_hash_table(struct sbus_request *dbus_req, void *data)
8d3578
     struct resp_ctx *rctx = talloc_get_type(data, struct resp_ctx);
8d3578
     struct autofs_ctx *actx =
8d3578
             talloc_get_type(rctx->pvt_ctx, struct autofs_ctx);
8d3578
-    errno_t ret;
8d3578
 
8d3578
-    ret = autofs_orphan_maps(actx);
8d3578
-    if (ret != EOK) {
8d3578
-        DEBUG(SSSDBG_OP_FAILURE, "Could not invalidate maps\n");
8d3578
-        return ret;
8d3578
-    }
8d3578
+    autofs_orphan_maps(actx);
8d3578
 
8d3578
     return sbus_request_return_and_finish(dbus_req, DBUS_TYPE_INVALID);
8d3578
 }
8d3578
 
8d3578
+static void
8d3578
+autofs_maps_delete_cb(hash_entry_t *item,
8d3578
+                      hash_destroy_enum deltype,
8d3578
+                      void *pvt)
8d3578
+{
8d3578
+    struct autofs_ctx *autofs_ctx;
8d3578
+    struct autofs_enum_ctx *enum_ctx;
8d3578
+
8d3578
+    autofs_ctx = talloc_get_type(pvt, struct autofs_ctx);
8d3578
+
8d3578
+    enum_ctx = sss_ptr_get_value(&item->value, struct autofs_enum_ctx);
8d3578
+    talloc_unlink(autofs_ctx->maps, enum_ctx);
8d3578
+}
8d3578
+
8d3578
 static int
8d3578
 autofs_process_init(TALLOC_CTX *mem_ctx,
8d3578
                     struct tevent_context *ev,
8d3578
@@ -158,7 +167,9 @@ autofs_process_init(TALLOC_CTX *mem_ctx,
8d3578
     }
8d3578
 
8d3578
     /* Create the lookup table for setautomntent results */
8d3578
-    autofs_ctx->maps = sss_ptr_hash_create(autofs_ctx, NULL, NULL);
8d3578
+    autofs_ctx->maps = sss_ptr_hash_create(autofs_ctx,
8d3578
+                                           autofs_maps_delete_cb,
8d3578
+                                           autofs_ctx);
8d3578
     if (autofs_ctx->maps == NULL) {
8d3578
         DEBUG(SSSDBG_CRIT_FAILURE,
8d3578
               "Unable to initialize automount maps hash table\n");
8d3578
diff --git a/src/responder/autofs/autofssrv_cmd.c b/src/responder/autofs/autofssrv_cmd.c
8d3578
index d413f8570..71938399e 100644
8d3578
--- a/src/responder/autofs/autofssrv_cmd.c
8d3578
+++ b/src/responder/autofs/autofssrv_cmd.c
8d3578
@@ -131,12 +131,12 @@ autofs_fill_entry(struct ldb_message *entry, struct sss_packet *packet, size_t *
8d3578
     return EOK;
8d3578
 }
8d3578
 
8d3578
-errno_t
8d3578
+void
8d3578
 autofs_orphan_maps(struct autofs_ctx *autofs_ctx)
8d3578
 {
8d3578
-    sss_ptr_hash_delete_all(autofs_ctx->maps, true);
8d3578
-
8d3578
-    return EOK;
8d3578
+    /* It will automatically decrease the refcount of enum_ctx through
8d3578
+     * delete callback. */
8d3578
+    sss_ptr_hash_delete_all(autofs_ctx->maps, false);
8d3578
 }
8d3578
 
8d3578
 static void
8d3578
@@ -149,8 +149,8 @@ autofs_enumctx_lifetime_timeout(struct tevent_context *ev,
8d3578
 
8d3578
     enum_ctx = talloc_get_type(pvt, struct autofs_enum_ctx);
8d3578
 
8d3578
-    /* Free the context. It will be automatically removed from the hash table. */
8d3578
-    talloc_free(enum_ctx);
8d3578
+    /* Remove it from the table. It will automatically decrease the refcount. */
8d3578
+    sss_ptr_hash_delete(enum_ctx->table, enum_ctx->key, false);
8d3578
 }
8d3578
 
8d3578
 static void
8d3578
@@ -185,6 +185,13 @@ autofs_create_enumeration_context(TALLOC_CTX *mem_ctx,
8d3578
     }
8d3578
 
8d3578
     enum_ctx->ready = false;
8d3578
+    enum_ctx->table = autofs_ctx->maps;
8d3578
+
8d3578
+    enum_ctx->key = talloc_strdup(enum_ctx, mapname);
8d3578
+    if (enum_ctx->key == NULL) {
8d3578
+        talloc_free(enum_ctx);
8d3578
+        return NULL;
8d3578
+    }
8d3578
 
8d3578
     ret = sss_ptr_hash_add(autofs_ctx->maps, mapname,
8d3578
                            enum_ctx, struct autofs_enum_ctx);
8d3578
@@ -196,6 +203,34 @@ autofs_create_enumeration_context(TALLOC_CTX *mem_ctx,
8d3578
     return enum_ctx;
8d3578
 }
8d3578
 
8d3578
+static void
8d3578
+autofs_orphan_master_map(struct autofs_ctx *autofs_ctx,
8d3578
+                         const char *mapname)
8d3578
+{
8d3578
+    struct sss_domain_info *dom;
8d3578
+    errno_t ret;
8d3578
+
8d3578
+    if (strcmp(mapname, "auto.master") != 0) {
8d3578
+        return;
8d3578
+    }
8d3578
+
8d3578
+    DEBUG(SSSDBG_TRACE_FUNC, "Invalidating master map\n");
8d3578
+
8d3578
+    /* Remove and invalidate all maps. */
8d3578
+    autofs_orphan_maps(autofs_ctx);
8d3578
+
8d3578
+    DEBUG(SSSDBG_TRACE_FUNC, "Invalidating autofs maps\n");
8d3578
+    for (dom = autofs_ctx->rctx->domains;
8d3578
+         dom != NULL;
8d3578
+         dom = get_next_domain(dom, SSS_GND_DESCEND)) {
8d3578
+        ret = sysdb_invalidate_autofs_maps(dom);
8d3578
+        if (ret != EOK) {
8d3578
+            DEBUG(SSSDBG_MINOR_FAILURE, "Unable to invalidate maps in "
8d3578
+                  "%s [%d]: %s\n", dom->name, ret, sss_strerror(ret));
8d3578
+        }
8d3578
+    }
8d3578
+}
8d3578
+
8d3578
 struct autofs_setent_state {
8d3578
     struct autofs_ctx *autofs_ctx;
8d3578
     struct autofs_enum_ctx *enum_ctx;
8d3578
@@ -323,7 +358,8 @@ static void autofs_setent_done(struct tevent_req *subreq)
8d3578
 }
8d3578
 
8d3578
 static errno_t
8d3578
-autofs_setent_recv(struct tevent_req *req,
8d3578
+autofs_setent_recv(TALLOC_CTX *mem_ctx,
8d3578
+                   struct tevent_req *req,
8d3578
                    struct autofs_enum_ctx **_enum_ctx)
8d3578
 {
8d3578
     struct autofs_setent_state *state;
8d3578
@@ -331,7 +367,7 @@ autofs_setent_recv(struct tevent_req *req,
8d3578
 
8d3578
     TEVENT_REQ_RETURN_ON_ERROR(req);
8d3578
 
8d3578
-    *_enum_ctx = state->enum_ctx;
8d3578
+    *_enum_ctx = talloc_reference(mem_ctx, state->enum_ctx);
8d3578
 
8d3578
     return EOK;
8d3578
 }
8d3578
@@ -430,6 +466,8 @@ sss_autofs_cmd_setautomntent(struct cli_ctx *cli_ctx)
8d3578
         goto done;
8d3578
     }
8d3578
 
8d3578
+    autofs_orphan_master_map(autofs_ctx, cmd_ctx->mapname);
8d3578
+
8d3578
     DEBUG(SSSDBG_TRACE_FUNC, "Obtaining autofs map %s\n",
8d3578
           cmd_ctx->mapname);
8d3578
 
8d3578
@@ -668,7 +706,7 @@ sss_autofs_cmd_getautomntent_done(struct tevent_req *req)
8d3578
 
8d3578
     cmd_ctx = tevent_req_callback_data(req, struct autofs_cmd_ctx);
8d3578
 
8d3578
-    ret = autofs_setent_recv(req, &enum_ctx);
8d3578
+    ret = autofs_setent_recv(cmd_ctx, req, &enum_ctx);
8d3578
     talloc_zfree(req);
8d3578
     if (ret != EOK) {
8d3578
         autofs_cmd_done(cmd_ctx, ret);
8d3578
-- 
8d3578
2.20.1
8d3578