Blob Blame History Raw
From ccf14f490985b96bd6f5871b55c1398024e34af0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Tue, 13 Aug 2019 12:21:47 +0200
Subject: [PATCH 68/90] sysdb: add expiration time to autofs entries
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This will be later used to expire single entries during
`get entry by name` operation.

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

Reviewed-by: Tomáš Halman <thalman@redhat.com>
---
 src/db/sysdb_autofs.c                  | 13 ++++++++++++-
 src/db/sysdb_autofs.h                  |  4 +++-
 src/db/sysdb_upgrade.c                 |  2 +-
 src/providers/ldap/sdap_async_autofs.c | 11 ++++++++---
 src/tests/sysdb-tests.c                |  4 ++--
 5 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/src/db/sysdb_autofs.c b/src/db/sysdb_autofs.c
index 89803a778..c92a51658 100644
--- a/src/db/sysdb_autofs.c
+++ b/src/db/sysdb_autofs.c
@@ -248,7 +248,9 @@ sysdb_save_autofsentry(struct sss_domain_info *domain,
                        const char *map,
                        const char *key,
                        const char *value,
-                       struct sysdb_attrs *attrs)
+                       struct sysdb_attrs *attrs,
+                       int cache_timeout,
+                       time_t now)
 {
     errno_t ret;
     TALLOC_CTX *tmp_ctx;
@@ -307,6 +309,15 @@ sysdb_save_autofsentry(struct sss_domain_info *domain,
         goto done;
     }
 
+    ret = sysdb_attrs_add_time_t(attrs, SYSDB_CACHE_EXPIRE,
+                                 ((cache_timeout) ?
+                                  (now + cache_timeout) : 0));
+    if (ret) {
+        DEBUG(SSSDBG_OP_FAILURE, "Could not set sysdb cache expire [%d]: %s\n",
+              ret, strerror(ret));
+        goto done;
+    }
+
     dn = sysdb_autofsentry_dn(tmp_ctx, domain, map, key, value);
     if (!dn) {
         ret = ENOMEM;
diff --git a/src/db/sysdb_autofs.h b/src/db/sysdb_autofs.h
index d1ef8412e..a3aba726c 100644
--- a/src/db/sysdb_autofs.h
+++ b/src/db/sysdb_autofs.h
@@ -58,7 +58,9 @@ sysdb_save_autofsentry(struct sss_domain_info *domain,
                        const char *map,
                        const char *key,
                        const char *value,
-                       struct sysdb_attrs *attrs);
+                       struct sysdb_attrs *attrs,
+                       int cache_timeout,
+                       time_t now);
 errno_t
 sysdb_del_autofsentry(struct sss_domain_info *domain,
                       const char *entry_dn);
diff --git a/src/db/sysdb_upgrade.c b/src/db/sysdb_upgrade.c
index f6a481147..392d04b07 100644
--- a/src/db/sysdb_upgrade.c
+++ b/src/db/sysdb_upgrade.c
@@ -1255,7 +1255,7 @@ int sysdb_upgrade_11(struct sysdb_ctx *sysdb, struct sss_domain_info *domain,
 
                 ret = sysdb_save_autofsentry(domain,
                                              (const char *) val->data,
-                                             key, value, NULL);
+                                             key, value, NULL, 0, 0);
                 if (ret != EOK) {
                     DEBUG(SSSDBG_OP_FAILURE,
                           "Cannot save autofs entry [%s]-[%s] into map %s\n",
diff --git a/src/providers/ldap/sdap_async_autofs.c b/src/providers/ldap/sdap_async_autofs.c
index 8d1742ad1..787283872 100644
--- a/src/providers/ldap/sdap_async_autofs.c
+++ b/src/providers/ldap/sdap_async_autofs.c
@@ -86,7 +86,8 @@ static errno_t
 add_autofs_entry(struct sss_domain_info *domain,
                  const char *map,
                  struct sdap_options *opts,
-                 struct sysdb_attrs *entry)
+                 struct sysdb_attrs *entry,
+                 time_t now)
 {
     const char *key;
     const char *value;
@@ -103,7 +104,8 @@ add_autofs_entry(struct sss_domain_info *domain,
         return EINVAL;
     }
 
-    return sysdb_save_autofsentry(domain, map, key, value, NULL);
+    return sysdb_save_autofsentry(domain, map, key, value, NULL,
+                                  domain->autofsmap_timeout, now);
 }
 
 static errno_t
@@ -119,11 +121,14 @@ save_autofs_entries(struct sss_domain_info *domain,
     int hret;
     errno_t ret;
     struct sysdb_attrs *entry;
+    time_t now;
 
     if (!add_dn_list) {
         return EOK;
     }
 
+    now = time(NULL);
+
     for (i=0; add_dn_list[i]; i++) {
         key.type = HASH_KEY_STRING;
         key.str = (char *) add_dn_list[i];
@@ -144,7 +149,7 @@ save_autofs_entries(struct sss_domain_info *domain,
 
         DEBUG(SSSDBG_TRACE_FUNC,
               "Saving autofs entry [%s]\n", add_dn_list[i]);
-        ret = add_autofs_entry(domain, map, opts, entry);
+        ret = add_autofs_entry(domain, map, opts, entry, now);
         if (ret) {
             DEBUG(SSSDBG_MINOR_FAILURE,
                   "Cannot save entry [%s] to cache\n", add_dn_list[i]);
diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c
index 832d60466..23f2bd383 100644
--- a/src/tests/sysdb-tests.c
+++ b/src/tests/sysdb-tests.c
@@ -6466,7 +6466,7 @@ START_TEST(test_autofs_store_entry_in_map)
 
         ret = sysdb_save_autofsentry(test_ctx->domain,
                                      autofsmapname, autofskey,
-                                     autofsval, NULL);
+                                     autofsval, NULL, 0, 0);
         fail_if(ret != EOK, "Could not save autofs entry %s", autofskey);
     }
 
@@ -6521,7 +6521,7 @@ START_TEST(test_autofs_key_duplicate)
 
     ret = sysdb_save_autofsentry(test_ctx->domain,
                                  autofsmapname, autofskey,
-                                 autofsval, NULL);
+                                 autofsval, NULL, 0, 0);
     fail_if(ret != EOK, "Could not save autofs entry %s", autofskey);
     talloc_free(test_ctx);
 }
-- 
2.20.1