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

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