>From c7be52ab737d5b660ebe38dfba20d1cd6cd80357 Mon Sep 17 00:00:00 2001
From: Simo Sorce <simo@redhat.com>
Date: Mon, 26 Aug 2013 14:29:30 -0400
Subject: [PATCH 2/2] krb5: Fetch ccname template from krb5.conf

In order to use the same defaults in all system daemons that needs to know how
to generate or search for ccaches we introduce ode here to take advantage of
the new option called default_ccache_name provided by libkrb5.

If set this variable we establish the same default for all programs that surce
it out of krb5.conf therefore providing a consistent experience across the
system.

Related:
https://fedorahosted.org/sssd/ticket/2036
---
 src/conf_macros.m4               |  2 +-
 src/providers/ad/ad_opts.h       |  2 +-
 src/providers/ipa/ipa_opts.h     |  2 +-
 src/providers/krb5/krb5_auth.c   |  4 +-
 src/providers/krb5/krb5_common.c | 88 ++++++++++++++++++++++++++++++++++++----
 src/providers/krb5/krb5_opts.h   |  2 +-
 src/providers/krb5/krb5_utils.c  | 74 ++++++++++++++++++++++++++++++++-
 7 files changed, 159 insertions(+), 15 deletions(-)

diff --git a/src/conf_macros.m4 b/src/conf_macros.m4
index 1834f3cb716b5d5f836ae1b1605966b457a5f851..433e99cef7bb415d9da775dfdba7c0ec5ca3e193 100644
--- a/src/conf_macros.m4
+++ b/src/conf_macros.m4
@@ -278,7 +278,7 @@ AC_DEFUN([WITH_DEFAULT_CCACHE_DIR],
 AC_DEFUN([WITH_DEFAULT_CCNAME_TEMPLATE],
   [ AC_ARG_WITH([default-ccname-template],
                 [AC_HELP_STRING([--with-default-ccname-template=CCACHE],
-                                [The default value of krb5_ccname_template [FILE:%d/krb5cc_%U_XXXXXX]]
+                                [The default fallback value of krb5_ccname_template [FILE:%d/krb5cc_%U_XXXXXX]]
                                )
                 ]
                )
diff --git a/src/providers/ad/ad_opts.h b/src/providers/ad/ad_opts.h
index 197b97e222367ba80af017fe635fceffdd9a9faa..f3b6cd61632601ec92a408a6a73cff6446fd48bf 100644
--- a/src/providers/ad/ad_opts.h
+++ b/src/providers/ad/ad_opts.h
@@ -134,7 +134,7 @@ struct dp_option ad_def_krb5_opts[] = {
     { "krb5_backup_server", DP_OPT_STRING, NULL_STRING, NULL_STRING },
     { "krb5_realm", DP_OPT_STRING, NULL_STRING, NULL_STRING },
     { "krb5_ccachedir", DP_OPT_STRING, { DEFAULT_CCACHE_DIR }, NULL_STRING },
-    { "krb5_ccname_template", DP_OPT_STRING, { DEFAULT_CCNAME_TEMPLATE }, NULL_STRING},
+    { "krb5_ccname_template", DP_OPT_STRING, NULL_STRING, NULL_STRING},
     { "krb5_auth_timeout", DP_OPT_NUMBER, { .number = 6 }, NULL_NUMBER },
     { "krb5_keytab", DP_OPT_STRING, { "/etc/krb5.keytab" }, NULL_STRING },
     { "krb5_validate", DP_OPT_BOOL, BOOL_TRUE, BOOL_TRUE },
diff --git a/src/providers/ipa/ipa_opts.h b/src/providers/ipa/ipa_opts.h
index 9babca73f4cf2e48ec7dff085870fa69429053df..5ec36c550b166e07a9ed2f2c31474c55d0ecdaee 100644
--- a/src/providers/ipa/ipa_opts.h
+++ b/src/providers/ipa/ipa_opts.h
@@ -263,7 +263,7 @@ struct dp_option ipa_def_krb5_opts[] = {
     { "krb5_backup_server", DP_OPT_STRING, NULL_STRING, NULL_STRING },
     { "krb5_realm", DP_OPT_STRING, NULL_STRING, NULL_STRING },
     { "krb5_ccachedir", DP_OPT_STRING, { DEFAULT_CCACHE_DIR }, NULL_STRING },
-    { "krb5_ccname_template", DP_OPT_STRING, { DEFAULT_CCNAME_TEMPLATE }, NULL_STRING},
+    { "krb5_ccname_template", DP_OPT_STRING, NULL_STRING, NULL_STRING},
     { "krb5_auth_timeout", DP_OPT_NUMBER, { .number = 6 }, NULL_NUMBER },
     { "krb5_keytab", DP_OPT_STRING, { "/etc/krb5.keytab" }, NULL_STRING },
     { "krb5_validate", DP_OPT_BOOL, BOOL_TRUE, BOOL_TRUE },
diff --git a/src/providers/krb5/krb5_auth.c b/src/providers/krb5/krb5_auth.c
index 506ca520a933e0eecfdf9ac1f5fe8b749f35aca0..db0aa936ff5577afedf72cb43f1c71485e16505c 100644
--- a/src/providers/krb5/krb5_auth.c
+++ b/src/providers/krb5/krb5_auth.c
@@ -315,7 +315,7 @@ static void krb5_auth_cache_creds(struct krb5_ctx *krb5_ctx,
     *dp_err = DP_ERR_OFFLINE;
 }
 
-static errno_t krb5_auth_prepare_ccache_file(struct krb5child_req *kr,
+static errno_t krb5_auth_prepare_ccache_name(struct krb5child_req *kr,
                                              struct be_ctx *be_ctx,
                                              int *pam_status, int *dp_err)
 {
@@ -783,7 +783,7 @@ static void krb5_auth_resolve_done(struct tevent_req *subreq)
         }
     }
 
-    ret = krb5_auth_prepare_ccache_file(kr, state->be_ctx,
+    ret = krb5_auth_prepare_ccache_name(kr, state->be_ctx,
                                         &state->pam_status, &state->dp_err);
     if (ret) {
         goto done;
diff --git a/src/providers/krb5/krb5_common.c b/src/providers/krb5/krb5_common.c
index 03c09d78e07944aa23b8a6af56c34e221a516fe4..3936e42818a1b90432075dcd7ea6e05f8eb0a3d6 100644
--- a/src/providers/krb5/krb5_common.c
+++ b/src/providers/krb5/krb5_common.c
@@ -33,6 +33,11 @@
 #include "providers/krb5/krb5_opts.h"
 #include "providers/krb5/krb5_utils.h"
 
+#ifdef HAVE_KRB5_CC_COLLECTION
+/* krb5 profile functions */
+#include <profile.h>
+#endif
+
 errno_t check_and_export_lifetime(struct dp_option *opts, const int opt_id,
                                   const char *env_name)
 {
@@ -86,6 +91,47 @@ done:
     return ret;
 }
 
+#ifdef HAVE_KRB5_CC_COLLECTION
+/* source default_ccache_name from krb5.conf */
+errno_t sss_get_system_ccname_template(TALLOC_CTX *mem_ctx, char **ccname)
+{
+    krb5_context ctx;
+    profile_t p;
+    char *value = NULL;
+    long ret;
+
+    *ccname = NULL;
+
+    ret = krb5_init_context(&ctx);
+    if (ret) return ret;
+
+    ret = krb5_get_profile(ctx, &p);
+    if (ret) goto done;
+
+    ret = profile_get_string(p, "libdefaults", "default_ccache_name",
+                             NULL, NULL, &value);
+    if (ret) goto done;
+
+    if (value) {
+        *ccname = talloc_strdup(mem_ctx, value);
+    }
+
+done:
+    krb5_free_context(ctx);
+    free(value);
+    return ret;
+}
+#else
+errno_t sss_get_system_ccname_template(TALLOC_CTX *mem_ctx, char **ccname)
+{
+    DEBUG(SSSDBG_CONF_SETTINGS,
+          ("Your kerberos library does not support the default_ccache_name "
+           "option or the profile library. Please use krb5_ccname_template "
+           "in sssd.conf if you want to change the default\n"));
+    *ccname = NULL;
+    return 0;
+}
+#endif
 
 errno_t check_and_export_options(struct dp_option *opts,
                                  struct sss_domain_info *dom,
@@ -188,19 +234,45 @@ errno_t check_and_export_options(struct dp_option *opts,
                                      "using the KDC or defaults.\n"));
     }
 
-    dummy = dp_opt_get_cstring(opts, KRB5_CCNAME_TMPL);
-    if (dummy == NULL) {
-        DEBUG(1, ("Missing credential cache name template.\n"));
-        ret = EINVAL;
-        goto done;
+    ccname = dp_opt_get_string(opts, KRB5_CCNAME_TMPL);
+    if (ccname != NULL) {
+        DEBUG(SSSDBG_CONF_SETTINGS,
+              ("The credential ccache name template has been explicitly set "
+               "in sssd.conf, it is recommended to set default_ccache_name "
+               "in krb5.conf instead so that a system default is used\n"));
+        ccname = talloc_strdup(tmp_ctx, ccname);
+        if (!ccname) {
+            ret = ENOMEM;
+            goto done;
+        }
+    } else {
+        ret = sss_get_system_ccname_template(tmp_ctx, &ccname);
+        if (ret) {
+            goto done;
+        }
+        if (!ccname) {
+            /* Use fallback default */
+            ccname = talloc_strdup(tmp_ctx, DEFAULT_CCNAME_TEMPLATE);
+            if (!ccname) {
+                ret = ENOMEM;
+                goto done;
+            }
+        }
+
+        /* set back in opts */
+        ret = dp_opt_set_string(opts, KRB5_CCNAME_TMPL, ccname);
+        if (ret != EOK) {
+            DEBUG(SSSDBG_CRIT_FAILURE, ("dp_opt_set_string failed.\n"));
+            goto done;
+        }
     }
 
-    cc_be = sss_krb5_get_type(dummy);
+    cc_be = sss_krb5_get_type(ccname);
     switch (cc_be) {
     case SSS_KRB5_TYPE_FILE:
         DEBUG(SSSDBG_CONF_SETTINGS, ("ccache is of type FILE\n"));
         krb5_ctx->cc_be = &file_cc;
-        if (dummy[0] != '/') {
+        if (ccname[0] != '/') {
             /* FILE:/path/to/cc */
             break;
         }
@@ -209,7 +281,7 @@ errno_t check_and_export_options(struct dp_option *opts,
               "missing an explicit type, but is an absolute "
               "path specifier. Assuming FILE:\n"));
 
-        ccname = talloc_asprintf(tmp_ctx, "FILE:%s", dummy);
+        ccname = talloc_asprintf(tmp_ctx, "FILE:%s", ccname);
         if (!ccname) {
             ret = ENOMEM;
             goto done;
diff --git a/src/providers/krb5/krb5_opts.h b/src/providers/krb5/krb5_opts.h
index 400b7e3381c594b6861f549bf64d60f80d3d7f37..db62cc3b23d8a5eadd3df97a102fb84128b740da 100644
--- a/src/providers/krb5/krb5_opts.h
+++ b/src/providers/krb5/krb5_opts.h
@@ -30,7 +30,7 @@ struct dp_option default_krb5_opts[] = {
     { "krb5_backup_server", DP_OPT_STRING, NULL_STRING, NULL_STRING },
     { "krb5_realm", DP_OPT_STRING, NULL_STRING, NULL_STRING },
     { "krb5_ccachedir", DP_OPT_STRING, { DEFAULT_CCACHE_DIR }, NULL_STRING },
-    { "krb5_ccname_template", DP_OPT_STRING, { DEFAULT_CCNAME_TEMPLATE }, NULL_STRING},
+    { "krb5_ccname_template", DP_OPT_STRING, NULL_STRING, NULL_STRING},
     { "krb5_auth_timeout", DP_OPT_NUMBER, { .number = 6 }, NULL_NUMBER },
     { "krb5_keytab", DP_OPT_STRING, { "/etc/krb5.keytab" }, NULL_STRING },
     { "krb5_validate", DP_OPT_BOOL, BOOL_FALSE, BOOL_FALSE },
diff --git a/src/providers/krb5/krb5_utils.c b/src/providers/krb5/krb5_utils.c
index 7a4d287ac934e5b45e182029d69e415f5144a54f..01df3c675e0bb6ab10ff60b9eee45b495e2485b2 100644
--- a/src/providers/krb5/krb5_utils.c
+++ b/src/providers/krb5/krb5_utils.c
@@ -157,6 +157,25 @@ done:
     return ret;
 }
 
+#define S_EXP_TEMP "{TEMP}"
+#define L_EXP_TEMP (sizeof(S_EXP_TEMP) - 1)
+#define S_EXP_UID "{uid}"
+#define L_EXP_UID (sizeof(S_EXP_UID) - 1)
+#define S_EXP_USERID "{USERID}"
+#define L_EXP_USERID (sizeof(S_EXP_USERID) - 1)
+#define S_EXP_EUID "{euid}"
+#define L_EXP_EUID (sizeof(S_EXP_EUID) - 1)
+#define S_EXP_NULL "{null}"
+#define L_EXP_NULL (sizeof(S_EXP_NULL) - 1)
+#define S_EXP_USERNAME "{username}"
+#define L_EXP_USERNAME (sizeof(S_EXP_USERNAME) - 1)
+#define S_EXP_LIBDIR "{LIBDIR}"
+#define L_EXP_LIBDIR (sizeof(S_EXP_LIBDIR) - 1)
+#define S_EXP_BINDIR "{BINDIR}"
+#define L_EXP_BINDIR (sizeof(S_EXP_BINDIR) - 1)
+#define S_EXP_SBINDIR "{SBINDIR}"
+#define L_EXP_SBINDIR (sizeof(S_EXP_SBINDIR) - 1)
+
 char *expand_ccname_template(TALLOC_CTX *mem_ctx, struct krb5child_req *kr,
                              const char *template, bool file_mode,
                              bool case_sensitive, bool *private_path)
@@ -170,6 +189,8 @@ char *expand_ccname_template(TALLOC_CTX *mem_ctx, struct krb5child_req *kr,
     char *res = NULL;
     const char *cache_dir_tmpl;
     TALLOC_CTX *tmp_ctx = NULL;
+    char action;
+    bool rewind;
 
     *private_path = false;
 
@@ -202,7 +223,11 @@ char *expand_ccname_template(TALLOC_CTX *mem_ctx, struct krb5child_req *kr,
             goto done;
         }
 
-        switch( *n ) {
+        rewind = true;
+        action = *n;
+        while (rewind) {
+            rewind = false;
+            switch (action) {
             case 'u':
                 if (kr->pd->user == NULL) {
                     DEBUG(1, ("Cannot expand user name template "
@@ -297,9 +322,56 @@ char *expand_ccname_template(TALLOC_CTX *mem_ctx, struct krb5child_req *kr,
                 result = talloc_asprintf_append(result, "%s%d", p,
                                                 kr->pd->cli_pid);
                 break;
+
+            /* Additional syntax from krb5.conf default_ccache_name */
+            case '{':
+                if (strncmp(n, S_EXP_TEMP, L_EXP_TEMP) == 0) {
+                    /* let the libkrb5 library resolve this */
+                    result = talloc_asprintf_append(result, "%%"S_EXP_TEMP);
+                    n += L_EXP_TEMP - 1;
+                } else if (strncmp(n , S_EXP_UID, L_EXP_UID) == 0) {
+                    action = 'U';
+                    n += L_EXP_UID - 1;
+                    rewind = true;
+                    continue;
+                } else if (strncmp(n , S_EXP_USERID, L_EXP_USERID) == 0) {
+                    action = 'U';
+                    n += L_EXP_USERID - 1;
+                    rewind = true;
+                    continue;
+                } else if (strncmp(n , S_EXP_EUID, L_EXP_EUID) == 0) {
+                    /* SSSD does not distinguish betwen uid and euid,
+                     * so we treat both the same way */
+                    action = 'U';
+                    n += L_EXP_EUID - 1;
+                    rewind = true;
+                    continue;
+                } else if (strncmp(n , S_EXP_NULL, L_EXP_NULL) == 0) {
+                    /* skip immediately */
+                    n += L_EXP_NULL - 1;
+                } else if (strncmp(n , S_EXP_USERNAME, L_EXP_USERNAME) == 0) {
+                    action = 'u';
+                    n += L_EXP_USERNAME - 1;
+                    rewind = true;
+                    continue;
+                } else if (strncmp(n , S_EXP_LIBDIR, L_EXP_LIBDIR) == 0) {
+                    /* skip, only the libkrb5 library can resolve this */
+                    result = talloc_asprintf_append(result, "%%"S_EXP_LIBDIR);
+                    n += L_EXP_LIBDIR - 1;
+                } else if (strncmp(n , S_EXP_BINDIR, L_EXP_BINDIR) == 0) {
+                    /* skip, only the libkrb5 library can resolve this */
+                    result = talloc_asprintf_append(result, "%%"S_EXP_BINDIR);
+                    n += L_EXP_BINDIR - 1;
+                } else if (strncmp(n , S_EXP_SBINDIR, L_EXP_SBINDIR) == 0) {
+                    /* skip, only the libkrb5 library can resolve this */
+                    result = talloc_asprintf_append(result, "%%"S_EXP_SBINDIR);
+                    n += L_EXP_SBINDIR - 1;
+                }
+                break;
             default:
                 DEBUG(1, ("format error, unknown template [%%%c].\n", *n));
                 goto done;
+            }
         }
 
         if (result == NULL) {
-- 
1.8.3.1

