Blob Blame History Raw
From 58f3d546954d7c2298a0b57865f7d5ad51f69aec Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Thu, 5 Sep 2019 11:32:11 +0200
Subject: [PATCH 89/90] sysdb: invalidate also autofs entries
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Autofs entries got data expiration attribute so we must
make sure it is invalidated as well.

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

Reviewed-by: Tomáš Halman <thalman@redhat.com>
---
 src/db/sysdb_autofs.c | 131 ++++++++++++++++++++++++++++++++++++++++++
 src/db/sysdb_autofs.h |   4 ++
 2 files changed, 135 insertions(+)

diff --git a/src/db/sysdb_autofs.c b/src/db/sysdb_autofs.c
index 11841d50d..413b00722 100644
--- a/src/db/sysdb_autofs.c
+++ b/src/db/sysdb_autofs.c
@@ -530,6 +530,37 @@ done:
     return ret;
 }
 
+errno_t
+sysdb_set_autofsentry_attr(struct sss_domain_info *domain,
+                           const char *mapname,
+                           const char *key,
+                           const char *value,
+                           struct sysdb_attrs *attrs,
+                           int mod_op)
+{
+    TALLOC_CTX *tmp_ctx;
+    struct ldb_dn *dn;
+    errno_t ret;
+
+    tmp_ctx = talloc_new(NULL);
+    if (tmp_ctx == NULL) {
+        DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory!\n");
+        return ENOMEM;
+    }
+
+    dn = sysdb_autofsentry_dn(tmp_ctx, domain, mapname, key, value);
+    if (dn == NULL) {
+        ret = ENOMEM;
+        goto done;
+    }
+
+    ret = sysdb_set_entry_attr(domain->sysdb, dn, attrs, mod_op);
+
+done:
+    talloc_free(tmp_ctx);
+    return ret;
+}
+
 errno_t
 sysdb_set_autofsmap_attr(struct sss_domain_info *domain,
                          const char *name,
@@ -558,6 +589,99 @@ done:
     return ret;
 }
 
+errno_t
+sysdb_invalidate_autofs_entries(struct sss_domain_info *domain,
+                                const char *mapname)
+{
+    TALLOC_CTX *tmp_ctx;
+    bool in_transaction = false;
+    struct ldb_message **entries;
+    struct sysdb_attrs *attrs;
+    const char *value;
+    const char *key;
+    size_t count;
+    errno_t ret;
+    size_t i;
+    int sret;
+
+    tmp_ctx = talloc_new(NULL);
+    if (tmp_ctx == NULL) {
+        DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory!\n");
+        return ENOMEM;
+    }
+
+    ret = sysdb_autofs_entries_by_map(tmp_ctx, domain, mapname,
+                                      &count, &entries);
+    if (ret == ENOENT) {
+        ret = EOK;
+        goto done;
+    } else if (ret != EOK) {
+        goto done;
+    }
+
+    attrs = sysdb_new_attrs(tmp_ctx);
+    if (attrs == NULL) {
+        ret = ENOMEM;
+        goto done;
+    }
+
+    ret = sysdb_attrs_add_time_t(attrs, SYSDB_CACHE_EXPIRE, 1);
+    if (ret != EOK) {
+        goto done;
+    }
+
+    ret = sysdb_transaction_start(domain->sysdb);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Failed to start transaction\n");
+        goto done;
+    }
+    in_transaction = true;
+
+    for (i = 0; i < count; i++) {
+        key = ldb_msg_find_attr_as_string(entries[i], SYSDB_AUTOFS_ENTRY_KEY,
+                                          NULL);
+        if (key == NULL) {
+            DEBUG(SSSDBG_MINOR_FAILURE, "An entry with no key?\n");
+            continue;
+        }
+
+        value = ldb_msg_find_attr_as_string(entries[i],
+                                            SYSDB_AUTOFS_ENTRY_VALUE,
+                                            NULL);
+        if (value == NULL) {
+            DEBUG(SSSDBG_MINOR_FAILURE, "An entry with no value?\n");
+            continue;
+        }
+
+        ret = sysdb_set_autofsentry_attr(domain, mapname, key, value,
+                                         attrs, SYSDB_MOD_REP);
+        if (ret != EOK) {
+            DEBUG(SSSDBG_MINOR_FAILURE, "Could not expire entry %s\n", key);
+            continue;
+        }
+    }
+
+    ret = sysdb_transaction_commit(domain->sysdb);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_OP_FAILURE, "Could not commit transaction\n");
+        goto done;
+    }
+    in_transaction = false;
+
+    ret = EOK;
+
+done:
+    if (in_transaction) {
+        sret = sysdb_transaction_cancel(domain->sysdb);
+        if (sret != EOK) {
+            DEBUG(SSSDBG_OP_FAILURE, "Could not cancel transaction\n");
+        }
+    }
+
+    talloc_free(tmp_ctx);
+    return ret;
+}
+
 errno_t
 sysdb_invalidate_autofs_maps(struct sss_domain_info *domain)
 {
@@ -634,6 +758,13 @@ sysdb_invalidate_autofs_maps(struct sss_domain_info *domain)
             DEBUG(SSSDBG_MINOR_FAILURE, "Could not expire map %s\n", name);
             continue;
         }
+
+        ret =  sysdb_invalidate_autofs_entries(domain, name);
+        if (ret != EOK) {
+            DEBUG(SSSDBG_MINOR_FAILURE, "Could not expire map entries %s\n",
+                  name);
+            continue;
+        }
     }
 
     ret = sysdb_transaction_commit(domain->sysdb);
diff --git a/src/db/sysdb_autofs.h b/src/db/sysdb_autofs.h
index 3775e2a17..37489f2e8 100644
--- a/src/db/sysdb_autofs.h
+++ b/src/db/sysdb_autofs.h
@@ -93,6 +93,10 @@ sysdb_set_autofsmap_attr(struct sss_domain_info *domain,
                          struct sysdb_attrs *attrs,
                          int mod_op);
 
+errno_t
+sysdb_invalidate_autofs_entries(struct sss_domain_info *domain,
+                                const char *mapname);
+
 errno_t
 sysdb_invalidate_autofs_maps(struct sss_domain_info *domain);
 
-- 
2.20.1