From aa51a9e2ee91a9b328f4519cddbccaa4403ca19e Mon Sep 17 00:00:00 2001
From: Stephen Gallagher <sgallagh@redhat.com>
Date: Tue, 30 Aug 2011 10:51:19 -0400
Subject: [PATCH] Add option to specify the kerberos replay cache dir

Adds a configure option to set the distribution default as well as
an sssd.conf option to override it.

https://fedorahosted.org/sssd/ticket/980
---
 Makefile.am                  |    2 ++
 configure.ac                 |    1 +
 contrib/sssd.spec.in         |    1 +
 src/conf_macros.m4           |   17 +++++++++++++++++
 src/confdb/confdb.h          |    1 +
 src/config/SSSDConfig.py     |    1 +
 src/config/SSSDConfigTest.py |    1 +
 src/config/etc/sssd.api.conf |    1 +
 src/man/sssd.conf.5.xml      |   14 ++++++++++++++
 src/monitor/monitor.c        |   23 +++++++++++++++++++++++
 10 files changed, 62 insertions(+), 0 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 4e2e26bae9ba9b388998d3c1af84f8a7aa2e51ed..1d381612eb1c43c4494331cb205d0d88a5f3a477 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -34,6 +34,7 @@ systemdunitdir = @systemdunitdir@
 logpath = @logpath@
 pubconfpath = @pubconfpath@
 pkgconfigdir = $(libdir)/pkgconfig
+krb5rcachedir = @krb5rcachedir@
 
 AM_CFLAGS =
 if WANT_AUX_INFO
@@ -1141,6 +1142,7 @@ install-data-hook:
 	rm $(DESTDIR)/$(nsslibdir)/libnss_sss.so.2 \
        $(DESTDIR)/$(nsslibdir)/libnss_sss.so
 	mv $(DESTDIR)/$(nsslibdir)/libnss_sss.so.2.0.0 $(DESTDIR)/$(nsslibdir)/libnss_sss.so.2
+	mkdir -p $(krb5rcachedir)
 
 uninstall-hook:
 	if [ -f $(abs_builddir)/src/config/.files ]; then \
diff --git a/configure.ac b/configure.ac
index 770371d54a9d6e29833ecc92f4c4ae7d7c4073e5..230afa21dbfd902ecdeb855051d774eaedbe81e4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -82,6 +82,7 @@ WITH_TEST_DIR
 WITH_MANPAGES
 WITH_XML_CATALOG
 WITH_KRB5_PLUGIN_PATH
+WITH_KRB5_RCACHE_DIR
 WITH_PYTHON_BINDINGS
 WITH_SELINUX
 WITH_NSCD
diff --git a/contrib/sssd.spec.in b/contrib/sssd.spec.in
index e489068d4e745b0782ac33f9e5c0a4dbb7fc3c24..3ff45a1b72020a207663933a16e06631a406de6b 100644
--- a/contrib/sssd.spec.in
+++ b/contrib/sssd.spec.in
@@ -256,6 +256,7 @@ rm -rf $RPM_BUILD_ROOT
 %{_libdir}/%{name}/
 %{ldb_modulesdir}/memberof.so
 %dir %{sssdstatedir}
+%dir %{_localstatedir}/cache/krb5rcache
 %attr(700,root,root) %dir %{dbpath}
 %attr(755,root,root) %dir %{pipepath}
 %attr(755,root,root) %dir %{pubconfpath}
diff --git a/src/conf_macros.m4 b/src/conf_macros.m4
index 1e503b3e397dd5e7e29763713a72d0e6bcdf92b6..5628146e548e49fd004a968b88f9d0ae52fc4d4e 100644
--- a/src/conf_macros.m4
+++ b/src/conf_macros.m4
@@ -204,6 +204,23 @@ AC_DEFUN([WITH_KRB5_PLUGIN_PATH],
     AC_SUBST(krb5pluginpath)
   ])
 
+AC_DEFUN([WITH_KRB5_RCACHE_DIR],
+  [ AC_ARG_WITH([krb5-rcache-dir],
+                [AC_HELP_STRING([--with-krb5-rcache-dir=PATH],
+                                [Path to store Kerberos replay caches [/var/cache/krb5rcache]]
+                               )
+                ]
+               )
+    config_rcachedir="\"VARDIR\"/cache/krb5rcache"
+    krb5rcachedir="${localstatedir}/cache/krb5rcache"
+    if test x"$with_krb5_rcache_dir" != x; then
+        krb5rcachedir=$with_krb5_rcache_dir
+        config_rcachedir=$with_krb5_rcache_dir
+    fi
+    AC_SUBST(krb5rcachedir)
+    AC_DEFINE_UNQUOTED(KRB5_RCACHE_DIR, "$config_rcachedir", [Directory used for storing Kerberos replay caches])
+  ])
+
 AC_DEFUN([WITH_PYTHON_BINDINGS],
   [ AC_ARG_WITH([python-bindings],
                 [AC_HELP_STRING([--with-python-bindings],
diff --git a/src/confdb/confdb.h b/src/confdb/confdb.h
index 5e50991f3014019b0c8fd065f4728827d959f06b..e982d17983eb68b3cde4d58b4289a447c723f90e 100644
--- a/src/confdb/confdb.h
+++ b/src/confdb/confdb.h
@@ -63,6 +63,7 @@
 #define CONFDB_MONITOR_NAME_REGEX   "re_expression"
 #define CONFDB_MONITOR_FULL_NAME_FORMAT "full_name_format"
 #define CONFDB_MONITOR_TRY_INOTIFY "try_inotify"
+#define CONFDB_MONITOR_KRB5_RCACHEDIR "krb5_rcachedir"
 
 /* NSS */
 #define CONFDB_NSS_CONF_ENTRY "config/nss"
diff --git a/src/config/SSSDConfig.py b/src/config/SSSDConfig.py
index 77a57c66e845527222158de3f98ea3f1616be09e..94d1f603e2f67288d7b88be87833ec1faf49cb82 100644
--- a/src/config/SSSDConfig.py
+++ b/src/config/SSSDConfig.py
@@ -49,6 +49,7 @@ option_strings = {
     'sbus_timeout' : _('Timeout for messages sent over the SBUS'),
     're_expression' : _('Regex to parse username and domain'),
     'full_name_format' : _('Printf-compatible format for displaying fully-qualified names'),
+    'krb5_rcache_dir' : _('Directory on the filesystem where SSSD should store Kerberos replay cache files.'),
 
     # [nss]
     'enum_cache_timeout' : _('Enumeration cache timeout length (seconds)'),
diff --git a/src/config/SSSDConfigTest.py b/src/config/SSSDConfigTest.py
index 5b01b9902ea1868f50f366ac0a3a556a6d7a925c..93b241b70b4b60f917b0d62cc0f1c46cd4f57c2c 100755
--- a/src/config/SSSDConfigTest.py
+++ b/src/config/SSSDConfigTest.py
@@ -266,6 +266,7 @@ class SSSDConfigTestSSSDService(unittest.TestCase):
             'sbus_timeout',
             're_expression',
             'full_name_format',
+            'krb5_rcache_dir',
             'debug_level',
             'debug_timestamps',
             'debug_to_files',
diff --git a/src/config/etc/sssd.api.conf b/src/config/etc/sssd.api.conf
index ae9ba559f1fab76b3ec08b2a34ddc69c2f4ba948..216766a530522b136cacfc07a8ddfe6354f6d499 100644
--- a/src/config/etc/sssd.api.conf
+++ b/src/config/etc/sssd.api.conf
@@ -17,6 +17,7 @@ timeout = int, None, false
 sbus_timeout = int, None, false
 re_expression = str, None, false
 full_name_format = str, None, false
+krb5_rcache_dir = str, None, false
 
 [nss]
 # Name service
diff --git a/src/man/sssd.conf.5.xml b/src/man/sssd.conf.5.xml
index 6f85dda9e38a99bde3886666dfeefc9312592d2c..5b7ad1202d9b9b84593c4e3b2eb279e52042954a 100644
--- a/src/man/sssd.conf.5.xml
+++ b/src/man/sssd.conf.5.xml
@@ -185,6 +185,20 @@
                             </para>
                         </listitem>
                     </varlistentry>
+                    <varlistentry>
+                        <term>krb5_rcache_dir (string)</term>
+                        <listitem>
+                            <para>
+                                Directory on the filesystem where SSSD should
+                                store Kerberos replay cache files.
+                            </para>
+                            <para>
+                                Default: Distribution-specific and specified
+                                at build-time. (/var/cache/krb5rcache on
+                                Fedora)
+                            </para>
+                        </listitem>
+                    </varlistentry>
                 </variablelist>
             </para>
         </refsect2>
diff --git a/src/monitor/monitor.c b/src/monitor/monitor.c
index dd099841815b1620b1a64014c92a221d38cb5c4f..ff3dff2b989ec286cb78da37f548f6d86244f2e1 100644
--- a/src/monitor/monitor.c
+++ b/src/monitor/monitor.c
@@ -1926,8 +1926,31 @@ int monitor_process_init(struct mt_ctx *ctx,
     struct sysdb_ctx_list *db_list;
     struct tevent_signal *tes;
     struct sss_domain_info *dom;
+    char *rcachedir;
     int num_providers;
     int ret;
+    int error;
+
+    /* Set up the environment variable for the Kerberos Replay Cache */
+    ret = confdb_get_string(ctx->cdb, ctx,
+                            CONFDB_MONITOR_CONF_ENTRY,
+                            CONFDB_MONITOR_KRB5_RCACHEDIR,
+                            KRB5_RCACHE_DIR,
+                            &rcachedir);
+    if (ret != EOK) {
+        return ret;
+    }
+
+    errno = 0;
+    ret = setenv("KRB5RCACHEDIR", rcachedir, 1);
+    if (ret < 0) {
+        error = errno;
+        DEBUG(1,
+              ("Unable to set KRB5RCACHEDIR: %s."
+               "Will attempt to use libkrb5 defaults\n",
+               strerror(error)));
+    }
+    talloc_zfree(rcachedir);
 
     /* Set up an event handler for a SIGHUP */
     tes = tevent_add_signal(ctx->ev, ctx, SIGHUP, 0,
-- 
1.7.6

