From f4a2e60cf7f9ac93b98450ec31c33afc045ec428 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Mon, 13 May 2013 10:30:48 +0200
Subject: [PATCH 3/4] back end: add refresh expired records periodic task

https://fedorahosted.org/sssd/ticket/1713

Add new option refresh_expired_interval.
---
 src/confdb/confdb.c                  | 11 +++++++++++
 src/confdb/confdb.h                  |  3 +++
 src/config/SSSDConfig/__init__.py.in |  1 +
 src/config/etc/sssd.api.conf         |  1 +
 src/man/sssd.conf.5.xml              | 18 ++++++++++++++++++
 src/providers/data_provider_be.c     | 23 +++++++++++++++++++++++
 src/providers/dp_backend.h           |  3 +++
 src/providers/dp_ptask.h             |  3 +++
 src/providers/dp_refresh.h           |  3 +++
 9 files changed, 66 insertions(+)

diff --git a/src/confdb/confdb.c b/src/confdb/confdb.c
index d70dc36207b74daaf9ea388fb36272237a00cb27..5ff5cc185fd752ec2a6c1666e45848619a822a79 100644
--- a/src/confdb/confdb.c
+++ b/src/confdb/confdb.c
@@ -1008,6 +1008,17 @@ static int confdb_get_domain_internal(struct confdb_ctx *cdb,
         goto done;
     }
 
+    /* Set refresh_expired_interval, if specified */
+    ret = get_entry_as_uint32(res->msgs[0], &domain->refresh_expired_interval,
+                              CONFDB_DOMAIN_REFRESH_EXPIRED_INTERVAL,
+                              0);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_FATAL_FAILURE,
+              ("Invalid value for [%s]\n",
+               CONFDB_DOMAIN_REFRESH_EXPIRED_INTERVAL));
+        goto done;
+    }
+
     /* Set the PAM warning time, if specified. If not specified, pass on
      * the "not set" value of "-1" which means "use provider default". The
      * value 0 means "always display the warning if server sends one" */
diff --git a/src/confdb/confdb.h b/src/confdb/confdb.h
index 88e80c17dd3a3b3349f90e919332e3a886b7ee83..71fc3678592b6d15c42bae91ca65acf92c29c631 100644
--- a/src/confdb/confdb.h
+++ b/src/confdb/confdb.h
@@ -170,6 +170,7 @@
 #define CONFDB_DOMAIN_AUTOFS_CACHE_TIMEOUT "entry_cache_autofs_timeout"
 #define CONFDB_DOMAIN_SUDO_CACHE_TIMEOUT "entry_cache_sudo_timeout"
 #define CONFDB_DOMAIN_PWD_EXPIRATION_WARNING "pwd_expiration_warning"
+#define CONFDB_DOMAIN_REFRESH_EXPIRED_INTERVAL "refresh_expired_interval"
 
 /* Local Provider */
 #define CONFDB_LOCAL_DEFAULT_SHELL   "default_shell"
@@ -221,6 +222,8 @@ struct sss_domain_info {
     uint32_t autofsmap_timeout;
     uint32_t sudo_timeout;
 
+    uint32_t refresh_expired_interval;
+
     int pwd_expiration_warning;
 
     struct sysdb_ctx *sysdb;
diff --git a/src/config/SSSDConfig/__init__.py.in b/src/config/SSSDConfig/__init__.py.in
index 3c6d84c5daeaee7dcbf45a6f2f99c5b9dc136227..f34d92dbedd246a46b498a5b344e7ec963fb06b8 100644
--- a/src/config/SSSDConfig/__init__.py.in
+++ b/src/config/SSSDConfig/__init__.py.in
@@ -124,6 +124,7 @@ option_strings = {
     'entry_cache_service_timeout' : _('Entry cache timeout length (seconds)'),
     'entry_cache_autofs_timeout' : _('Entry cache timeout length (seconds)'),
     'entry_cache_sudo_timeout' : _('Entry cache timeout length (seconds)'),
+    'refresh_expired_interval' : _('How often should expired rules be refreshed in background'),
 
     # [provider/ipa]
     'ipa_domain' : _('IPA domain'),
diff --git a/src/config/etc/sssd.api.conf b/src/config/etc/sssd.api.conf
index 48fe7ebf0bfa22ced85df612e35b3b14186b80a7..2214d6283f42a1069dae6ec3778720f42848a708 100644
--- a/src/config/etc/sssd.api.conf
+++ b/src/config/etc/sssd.api.conf
@@ -119,6 +119,7 @@ entry_cache_netgroup_timeout = int, None, false
 entry_cache_service_timeout = int, None, false
 entry_cache_autofs_timeout = int, None, false
 entry_cache_sudo_timeout = int, None, false
+refresh_expired_interval = int, None, false
 
 # Special providers
 [provider/permit]
diff --git a/src/man/sssd.conf.5.xml b/src/man/sssd.conf.5.xml
index 39ef2bbe1d198cc09069aed3b01c6a6a53351c7f..dc494ae951f19b0b50a6d195c83a751f7eb5c393 100644
--- a/src/man/sssd.conf.5.xml
+++ b/src/man/sssd.conf.5.xml
@@ -1100,6 +1100,24 @@ override_homedir = /home/%u
                 </varlistentry>
 
                 <varlistentry>
+                    <term>refresh_expired_interval (integer)</term>
+                    <listitem>
+                        <para>
+                            Specifies how many seconds SSSD has to wait
+                            before refreshing expired records. Currently
+                            only refreshing expired netgroups is supported.
+                        </para>
+                        <para>
+                            You can consider setting this value to
+                            3/4 * entry_cache_timeout.
+                        </para>
+                        <para>
+                            Default: 0 (disabled)
+                        </para>
+                    </listitem>
+                </varlistentry>
+
+                <varlistentry>
                     <term>cache_credentials (bool)</term>
                     <listitem>
                         <para>
diff --git a/src/providers/data_provider_be.c b/src/providers/data_provider_be.c
index 33590aeef0231427642916c6a2f9bc391c165c21..c34b601feacee9c4782dc49143869e38647de1de 100644
--- a/src/providers/data_provider_be.c
+++ b/src/providers/data_provider_be.c
@@ -42,6 +42,8 @@
 #include "sbus/sssd_dbus.h"
 #include "providers/dp_backend.h"
 #include "providers/fail_over.h"
+#include "providers/dp_refresh.h"
+#include "providers/dp_ptask.h"
 #include "util/child_common.h"
 #include "resolv/async_resolv.h"
 #include "monitor/monitor_interfaces.h"
@@ -2494,6 +2496,27 @@ int be_process_init(TALLOC_CTX *mem_ctx,
         goto fail;
     }
 
+    /* Initialize be_refresh periodic task. */
+    ctx->refresh_ctx = be_refresh_ctx_init(ctx);
+    if (ctx->refresh_ctx == NULL) {
+        DEBUG(SSSDBG_FATAL_FAILURE, ("Unable to initialize refresh_ctx\n"));
+        ret = ENOMEM;
+        goto fail;
+    }
+
+    if (ctx->domain->refresh_expired_interval > 0) {
+        ret = be_ptask_create(ctx, ctx, ctx->domain->refresh_expired_interval,
+                              60, 10, ctx->domain->refresh_expired_interval,
+                              BE_PTASK_OFFLINE_SKIP,
+                              be_refresh_send, be_refresh_recv,
+                              ctx->refresh_ctx, "Refresh Records", NULL);
+        if (ret != EOK) {
+            DEBUG(SSSDBG_FATAL_FAILURE,
+                  ("Unable to initialize refresh periodic task\n"));
+            goto fail;
+        }
+    }
+
     ret = load_backend_module(ctx, BET_ID,
                               &ctx->bet_info[BET_ID], NULL);
     if (ret != EOK) {
diff --git a/src/providers/dp_backend.h b/src/providers/dp_backend.h
index 743b6f4ffe73fe9ec7404218184a7133aba054c6..a2af785ca4adc0762a7b4a7cb072f31219e09e01 100644
--- a/src/providers/dp_backend.h
+++ b/src/providers/dp_backend.h
@@ -24,6 +24,7 @@
 
 #include "providers/data_provider.h"
 #include "providers/fail_over.h"
+#include "providers/dp_refresh.h"
 #include "util/child_common.h"
 #include "db/sysdb.h"
 
@@ -123,6 +124,8 @@ struct be_ctx {
     struct loaded_be loaded_be[BET_MAX];
     struct bet_info bet_info[BET_MAX];
 
+    struct be_refresh_ctx *refresh_ctx;
+
     size_t check_online_ref_count;
 };
 
diff --git a/src/providers/dp_ptask.h b/src/providers/dp_ptask.h
index cbd74485bc20c80709434bca136bd9e419b590fa..c64da596a603a0f38634c2feb5ad8bcf84412140 100644
--- a/src/providers/dp_ptask.h
+++ b/src/providers/dp_ptask.h
@@ -27,6 +27,9 @@
 
 #include "providers/dp_backend.h"
 
+/* solve circular dependency */
+struct be_ctx;
+
 struct be_ptask;
 
 /**
diff --git a/src/providers/dp_refresh.h b/src/providers/dp_refresh.h
index 54c6aac99866415d4f33f61ac05d487f71b8c49c..d93a932637804dfbf743c395af3f8e640f79c12f 100644
--- a/src/providers/dp_refresh.h
+++ b/src/providers/dp_refresh.h
@@ -27,6 +27,9 @@
 #include "providers/dp_backend.h"
 #include "providers/dp_ptask.h"
 
+/* solve circular dependency */
+struct be_ctx;
+
 /**
  * name_list contains SYSDB_NAME of all expired records.
  */
-- 
1.7.11.7

