Blame SOURCES/Properly-locate-credentials-in-collection-caches-in-.patch

c530df
From 51721282ae021e57888b38720a4acd69e88a8f4f Mon Sep 17 00:00:00 2001
c530df
From: Robbie Harwood <rharwood@redhat.com>
c530df
Date: Mon, 20 Nov 2017 14:09:04 -0500
c530df
Subject: [PATCH] Properly locate credentials in collection caches in mechglue
c530df
c530df
Previously, we would just put the credentials in the default cache for
c530df
a collection type, which lead to some mysterious failures.
c530df
c530df
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
c530df
Reviewed-by: Simo Sorce <simo@redhat.com>
c530df
Merges: #221
c530df
(cherry picked from commit 670240a6cd4d5e2ecf13e481621098693cdbaa89)
c530df
---
c530df
 proxy/src/mechglue/gpp_creds.c  | 81 +++++++++++++++++++++++++++++------------
c530df
 proxy/src/mechglue/gss_plugin.h |  2 +-
c530df
 2 files changed, 59 insertions(+), 24 deletions(-)
c530df
c530df
diff --git a/proxy/src/mechglue/gpp_creds.c b/proxy/src/mechglue/gpp_creds.c
c530df
index 3ebd726..187ada7 100644
c530df
--- a/proxy/src/mechglue/gpp_creds.c
c530df
+++ b/proxy/src/mechglue/gpp_creds.c
c530df
@@ -170,7 +170,16 @@ static krb5_error_code gpp_construct_cred(gssx_cred *creds, krb5_context ctx,
c530df
     return 0;
c530df
 }
c530df
 
c530df
-uint32_t gpp_store_remote_creds(uint32_t *min, bool default_creds,
c530df
+/* Store creds from remote in a local ccache, updating where possible.
c530df
+ *
c530df
+ * If store_as_default_cred is true, the cred is made default for its
c530df
+ * collection, if there is one.  Note that if the ccache is not of a
c530df
+ * collection type, the creds will overwrite the ccache.
c530df
+ *
c530df
+ * If no "ccache" entry is specified in cred_store, the default ccache for a
c530df
+ * new context will be used.
c530df
+ */
c530df
+uint32_t gpp_store_remote_creds(uint32_t *min, bool store_as_default_cred,
c530df
                                 gss_const_key_value_set_t cred_store,
c530df
                                 gssx_cred *creds)
c530df
 {
c530df
@@ -179,7 +188,7 @@ uint32_t gpp_store_remote_creds(uint32_t *min, bool default_creds,
c530df
     krb5_creds cred;
c530df
     krb5_error_code ret;
c530df
     char cred_name[creds->desired_name.display_name.octet_string_len + 1];
c530df
-    const char *cc_type;
c530df
+    const char *cc_name;
c530df
 
c530df
     *min = 0;
c530df
 
c530df
@@ -191,38 +200,64 @@ uint32_t gpp_store_remote_creds(uint32_t *min, bool default_creds,
c530df
         goto done;
c530df
     }
c530df
 
c530df
-    if (cred_store) {
c530df
-        for (unsigned i = 0; i < cred_store->count; i++) {
c530df
-            if (strcmp(cred_store->elements[i].key, "ccache") == 0) {
c530df
-                ret = krb5_cc_resolve(ctx, cred_store->elements[i].value,
c530df
-                                      &ccache);
c530df
-                if (ret) goto done;
c530df
-                break;
c530df
-            }
c530df
+    for (unsigned i = 0; cred_store && i < cred_store->count; i++) {
c530df
+        if (strcmp(cred_store->elements[i].key, "ccache") == 0) {
c530df
+            /* krb5 creates new ccaches based off the default name. */
c530df
+            ret = krb5_cc_set_default_name(ctx,
c530df
+                                           cred_store->elements[i].value);
c530df
+            if (ret)
c530df
+                goto done;
c530df
+
c530df
+            break;
c530df
         }
c530df
     }
c530df
-    if (!ccache) {
c530df
-        if (!default_creds) {
c530df
-            ret = ENOMEDIUM;
c530df
-            goto done;
c530df
-        }
c530df
-        ret = krb5_cc_default(ctx, &ccache);
c530df
-        if (ret) goto done;
c530df
-    }
c530df
 
c530df
-    cc_type = krb5_cc_get_type(ctx, ccache);
c530df
-    if (strcmp(cc_type, "FILE") == 0) {
c530df
+    cc_name = krb5_cc_default_name(ctx);
c530df
+    if (strncmp(cc_name, "FILE:", 5) == 0 || !strchr(cc_name, ':')) {
c530df
         /* FILE ccaches don't handle updates properly: if they have the same
c530df
          * principal name, they are blackholed.  We either have to change the
c530df
          * name (at which point the file grows forever) or flash the cache on
c530df
          * every update. */
c530df
-        ret = krb5_cc_initialize(ctx, ccache, cred.client);
c530df
-        if (ret != 0) {
c530df
+        ret = krb5_cc_default(ctx, &ccache);
c530df
+        if (ret)
c530df
             goto done;
c530df
-        }
c530df
+
c530df
+        ret = krb5_cc_initialize(ctx, ccache, cred.client);
c530df
+        if (ret != 0)
c530df
+            goto done;
c530df
+
c530df
+        ret = krb5_cc_store_cred(ctx, ccache, &cred);
c530df
+        goto done;
c530df
     }
c530df
 
c530df
+    ret = krb5_cc_cache_match(ctx, cred.client, &ccache);
c530df
+    if (ret == KRB5_CC_NOTFOUND) {
c530df
+        /* A new ccache within the collection whose name is based off the
c530df
+         * default_name for the context.  krb5_cc_new_unique only accepts the
c530df
+         * leading component of a name as a type. */
c530df
+        char *cc_type;
c530df
+        const char *p;
c530df
+
c530df
+        p = strchr(cc_name, ':'); /* can't be FILE here */
c530df
+        cc_type = strndup(cc_name, p - cc_name);
c530df
+        if (!cc_type) {
c530df
+            ret = ENOMEM;
c530df
+            goto done;
c530df
+        }
c530df
+
c530df
+        ret = krb5_cc_new_unique(ctx, cc_type, NULL, &ccache);
c530df
+        free(cc_type);
c530df
+    }
c530df
+    if (ret)
c530df
+        goto done;
c530df
+
c530df
     ret = krb5_cc_store_cred(ctx, ccache, &cred);
c530df
+    if (ret)
c530df
+        goto done;
c530df
+
c530df
+    if (store_as_default_cred) {
c530df
+        ret = krb5_cc_switch(ctx, ccache);
c530df
+    }
c530df
 
c530df
 done:
c530df
     if (ctx) {
c530df
diff --git a/proxy/src/mechglue/gss_plugin.h b/proxy/src/mechglue/gss_plugin.h
c530df
index 333d63c..c0e8870 100644
c530df
--- a/proxy/src/mechglue/gss_plugin.h
c530df
+++ b/proxy/src/mechglue/gss_plugin.h
c530df
@@ -76,7 +76,7 @@ uint32_t gpp_cred_handle_init(uint32_t *min, bool defcred, const char *ccache,
c530df
                               struct gpp_cred_handle **out_handle);
c530df
 uint32_t gpp_cred_handle_free(uint32_t *min, struct gpp_cred_handle *handle);
c530df
 bool gpp_creds_are_equal(gssx_cred *a, gssx_cred *b);
c530df
-uint32_t gpp_store_remote_creds(uint32_t *min, bool default_creds,
c530df
+uint32_t gpp_store_remote_creds(uint32_t *min, bool store_as_default_cred,
c530df
                                 gss_const_key_value_set_t cred_store,
c530df
                                 gssx_cred *creds);
c530df