Blame SOURCES/Add-support-for-start_realm-cache-config.patch

27013b
From bb5552ece2a351dc3ccab52cceea1eaffeacd768 Mon Sep 17 00:00:00 2001
27013b
From: Greg Hudson <ghudson@mit.edu>
27013b
Date: Mon, 14 Dec 2020 13:16:17 -0500
27013b
Subject: [PATCH] Add support for start_realm cache config
27013b
27013b
When making TGS requests, if start_realm is set in the cache, use the
27013b
named realm to look up the initial TGT for referral or cross-realm
27013b
requests.  (Also correct a comment in struct _tkt_creds_context: the
27013b
ccache field is an owner pointer, not an alias.)
27013b
27013b
Add an internal API k5_cc_store_primary_cred(), which sets start_realm
27013b
if the cred being stored is a TGT for a realm other than the client
27013b
realm.  Use this API when acquiring initial tickets with a
27013b
caller-specified output ccache, when renewing or validating tickets
27013b
with kinit, when accepting a delegated credential in a GSS context,
27013b
and when storing a single cred with kvno --out-cache.
27013b
27013b
ticket: 8332
27013b
tags: pullup
27013b
target_version: 1.19
27013b
27013b
(cherry picked from commit 0d56740ab9fcc40dc7f46c6fbebdf8f1214f9d96)
27013b
[rharwood@redhat.com: backport around spelling and canonicalization fallback]
27013b
---
27013b
 doc/formats/ccache_file_format.rst       |  6 +++++
27013b
 src/clients/kinit/kinit.c                |  2 +-
27013b
 src/clients/kvno/kvno.c                  |  5 ++++-
27013b
 src/include/k5-int.h                     |  4 ++++
27013b
 src/lib/gssapi/krb5/accept_sec_context.c |  2 +-
27013b
 src/lib/krb5/ccache/ccfns.c              | 20 +++++++++++++++++
27013b
 src/lib/krb5/krb/get_creds.c             | 28 ++++++++++++++++++------
27013b
 src/lib/krb5/krb/get_in_tkt.c            |  2 +-
27013b
 src/lib/krb5/libkrb5.exports             |  1 +
27013b
 src/lib/krb5_32.def                      |  3 +++
27013b
 src/tests/t_crossrealm.py                |  8 +++++++
27013b
 src/tests/t_pkinit.py                    |  3 +++
27013b
 12 files changed, 73 insertions(+), 11 deletions(-)
27013b
27013b
diff --git a/doc/formats/ccache_file_format.rst b/doc/formats/ccache_file_format.rst
27013b
index 6349e0d29..6138c1b58 100644
27013b
--- a/doc/formats/ccache_file_format.rst
27013b
+++ b/doc/formats/ccache_file_format.rst
27013b
@@ -174,3 +174,9 @@ refresh_time
27013b
     decimal representation of a timestamp at which the GSS mechanism
27013b
     should attempt to refresh the credential cache from the client
27013b
     keytab.
27013b
+
27013b
+start_realm
27013b
+    This key indicates the realm of the ticket-granting ticket to be
27013b
+    used for TGS requests, when making a referrals request or
27013b
+    beginning a cross-realm request.  If it is not present, the client
27013b
+    realm is used.
27013b
diff --git a/src/clients/kinit/kinit.c b/src/clients/kinit/kinit.c
27013b
index 3fdae2878..e5ebeb895 100644
27013b
--- a/src/clients/kinit/kinit.c
27013b
+++ b/src/clients/kinit/kinit.c
27013b
@@ -828,7 +828,7 @@ k5_kinit(struct k_opts *opts, struct k5_data *k5)
27013b
         if (opts->verbose)
27013b
             fprintf(stderr, _("Initialized cache\n"));
27013b
 
27013b
-        ret = krb5_cc_store_cred(k5->ctx, k5->out_cc, &my_creds);
27013b
+        ret = k5_cc_store_primary_cred(k5->ctx, k5->out_cc, &my_creds);
27013b
         if (ret) {
27013b
             com_err(progname, ret, _("while storing credentials"));
27013b
             goto cleanup;
27013b
diff --git a/src/clients/kvno/kvno.c b/src/clients/kvno/kvno.c
27013b
index c5f6bf700..f83c68a99 100644
27013b
--- a/src/clients/kvno/kvno.c
27013b
+++ b/src/clients/kvno/kvno.c
27013b
@@ -561,7 +561,10 @@ do_v5_kvno(int count, char *names[], char * ccachestr, char *etypestr,
27013b
                 }
27013b
                 initialized = 1;
27013b
             }
27013b
-            ret = krb5_cc_store_cred(context, out_ccache, creds);
27013b
+            if (count == 1)
27013b
+                ret = k5_cc_store_primary_cred(context, out_ccache, creds);
27013b
+            else
27013b
+                ret = krb5_cc_store_cred(context, out_ccache, creds);
27013b
             if (ret) {
27013b
                 com_err(prog, ret, _("while storing creds in output ccache"));
27013b
                 exit(1);
27013b
diff --git a/src/include/k5-int.h b/src/include/k5-int.h
27013b
index eb18a4cd6..912aaedac 100644
27013b
--- a/src/include/k5-int.h
27013b
+++ b/src/include/k5-int.h
27013b
@@ -307,6 +307,7 @@ typedef unsigned char   u_char;
27013b
 #define KRB5_CC_CONF_PA_TYPE                   "pa_type"
27013b
 #define KRB5_CC_CONF_PROXY_IMPERSONATOR        "proxy_impersonator"
27013b
 #define KRB5_CC_CONF_REFRESH_TIME              "refresh_time"
27013b
+#define KRB5_CC_CONF_START_REALM               "start_realm"
27013b
 
27013b
 /* Error codes used in KRB_ERROR protocol messages.
27013b
    Return values of library routines are based on a different error table
27013b
@@ -1910,6 +1911,9 @@ krb5_ser_unpack_bytes(krb5_octet *, size_t, krb5_octet **, size_t *);
27013b
 krb5_error_code KRB5_CALLCONV
27013b
 krb5int_cc_default(krb5_context, krb5_ccache *);
27013b
 
27013b
+krb5_error_code
27013b
+k5_cc_store_primary_cred(krb5_context, krb5_ccache, krb5_creds *);
27013b
+
27013b
 /* Fill in the buffer with random alpha-numeric data. */
27013b
 krb5_error_code
27013b
 krb5int_random_string(krb5_context, char *string, unsigned int length);
27013b
diff --git a/src/lib/gssapi/krb5/accept_sec_context.c b/src/lib/gssapi/krb5/accept_sec_context.c
27013b
index 3d5b84b15..abccb5d11 100644
27013b
--- a/src/lib/gssapi/krb5/accept_sec_context.c
27013b
+++ b/src/lib/gssapi/krb5/accept_sec_context.c
27013b
@@ -216,7 +216,7 @@ rd_and_store_for_creds(context, auth_context, inbuf, out_cred)
27013b
     if ((retval = krb5_cc_initialize(context, ccache, creds[0]->client)))
27013b
         goto cleanup;
27013b
 
27013b
-    if ((retval = krb5_cc_store_cred(context, ccache, creds[0])))
27013b
+    if ((retval = k5_cc_store_primary_cred(context, ccache, creds[0])))
27013b
         goto cleanup;
27013b
 
27013b
     /* generate a delegated credential handle */
27013b
diff --git a/src/lib/krb5/ccache/ccfns.c b/src/lib/krb5/ccache/ccfns.c
27013b
index 62a6983d8..23edc2578 100644
27013b
--- a/src/lib/krb5/ccache/ccfns.c
27013b
+++ b/src/lib/krb5/ccache/ccfns.c
27013b
@@ -297,3 +297,23 @@ krb5_cc_switch(krb5_context context, krb5_ccache cache)
27013b
         return 0;
27013b
     return cache->ops->switch_to(context, cache);
27013b
 }
27013b
+
27013b
+krb5_error_code
27013b
+k5_cc_store_primary_cred(krb5_context context, krb5_ccache cache,
27013b
+                         krb5_creds *creds)
27013b
+{
27013b
+    krb5_error_code ret;
27013b
+
27013b
+    /* Write a start realm if we're writing a TGT and the client realm isn't
27013b
+     * the same as the TGS realm. */
27013b
+    if (IS_TGS_PRINC(creds->server) &&
27013b
+        !data_eq(creds->client->realm, creds->server->data[1])) {
27013b
+        ret = krb5_cc_set_config(context, cache, NULL,
27013b
+                                 KRB5_CC_CONF_START_REALM,
27013b
+                                 &creds->server->data[1]);
27013b
+        if (ret)
27013b
+            return ret;
27013b
+    }
27013b
+
27013b
+    return krb5_cc_store_cred(context, cache, creds);
27013b
+}
27013b
diff --git a/src/lib/krb5/krb/get_creds.c b/src/lib/krb5/krb/get_creds.c
27013b
index e0a3b5cd8..b40f705fc 100644
27013b
--- a/src/lib/krb5/krb/get_creds.c
27013b
+++ b/src/lib/krb5/krb/get_creds.c
27013b
@@ -149,7 +149,8 @@ struct _krb5_tkt_creds_context {
27013b
     krb5_principal client;      /* Caller-requested client principal (alias) */
27013b
     krb5_principal server;      /* Server principal (alias) */
27013b
     krb5_principal req_server;  /* Caller-requested server principal */
27013b
-    krb5_ccache ccache;         /* Caller-provided ccache (alias) */
27013b
+    krb5_ccache ccache;         /* Caller-provided ccache */
27013b
+    krb5_data start_realm;      /* Realm of starting TGT in ccache */
27013b
     krb5_flags req_options;     /* Caller-requested KRB5_GC_* options */
27013b
     krb5_flags req_kdcopt;      /* Caller-requested options as KDC options */
27013b
     krb5_authdata **authdata;   /* Caller-requested authdata */
27013b
@@ -783,7 +784,7 @@ get_cached_local_tgt(krb5_context context, krb5_tkt_creds_context ctx,
27013b
         return code;
27013b
 
27013b
     /* Construct the principal name. */
27013b
-    code = krb5int_tgtname(context, &ctx->client->realm, &ctx->client->realm,
27013b
+    code = krb5int_tgtname(context, &ctx->start_realm, &ctx->start_realm,
27013b
                            &tgtname);
27013b
     if (code != 0)
27013b
         return code;
27013b
@@ -821,7 +822,7 @@ init_realm_path(krb5_context context, krb5_tkt_creds_context ctx)
27013b
     size_t nrealms;
27013b
 
27013b
     /* Get the client realm path and count its length. */
27013b
-    code = k5_client_realm_path(context, &ctx->client->realm,
27013b
+    code = k5_client_realm_path(context, &ctx->start_realm,
27013b
                                 &ctx->server->realm, &realm_path);
27013b
     if (code != 0)
27013b
         return code;
27013b
@@ -933,7 +934,7 @@ step_get_tgt(krb5_context context, krb5_tkt_creds_context ctx)
27013b
                 ctx->cur_realm = path_realm;
27013b
                 ctx->next_realm = ctx->last_realm;
27013b
             }
27013b
-        } else if (data_eq(*tgt_realm, ctx->client->realm)) {
27013b
+        } else if (data_eq(*tgt_realm, ctx->start_realm)) {
27013b
             /* We were referred back to the local realm, which is bad. */
27013b
             return KRB5_KDCREP_MODIFIED;
27013b
         } else {
27013b
@@ -963,7 +964,7 @@ begin_get_tgt(krb5_context context, krb5_tkt_creds_context ctx)
27013b
 
27013b
     ctx->state = STATE_GET_TGT;
27013b
 
27013b
-    is_local_service = data_eq(ctx->client->realm, ctx->server->realm);
27013b
+    is_local_service = data_eq(ctx->start_realm, ctx->server->realm);
27013b
     if (!is_local_service) {
27013b
         /* See if we have a cached TGT for the server realm. */
27013b
         code = get_cached_tgt(context, ctx, &ctx->server->realm, &cached_tgt);
27013b
@@ -1048,10 +1049,10 @@ begin(krb5_context context, krb5_tkt_creds_context ctx)
27013b
     if (code != 0 || ctx->state == STATE_COMPLETE)
27013b
         return code;
27013b
 
27013b
-    /* If the server realm is unspecified, start with the client realm. */
27013b
+    /* If the server realm is unspecified, start with the TGT realm. */
27013b
     if (krb5_is_referral_realm(&ctx->server->realm)) {
27013b
         krb5_free_data_contents(context, &ctx->server->realm);
27013b
-        code = krb5int_copy_data_contents(context, &ctx->client->realm,
27013b
+        code = krb5int_copy_data_contents(context, &ctx->start_realm,
27013b
                                           &ctx->server->realm);
27013b
         TRACE_TKT_CREDS_REFERRAL_REALM(context, ctx->server);
27013b
         if (code != 0)
27013b
@@ -1100,6 +1101,18 @@ krb5_tkt_creds_init(krb5_context context, krb5_ccache ccache,
27013b
     code = krb5_cc_dup(context, ccache, &ctx->ccache);
27013b
     if (code != 0)
27013b
         goto cleanup;
27013b
+
27013b
+    /* Get the start realm from the cache config, defaulting to the client
27013b
+     * realm. */
27013b
+    code = krb5_cc_get_config(context, ccache, NULL, "start_realm",
27013b
+                              &ctx->start_realm);
27013b
+    if (code != 0) {
27013b
+        code = krb5int_copy_data_contents(context, &ctx->client->realm,
27013b
+                                          &ctx->start_realm);
27013b
+        if (code != 0)
27013b
+            goto cleanup;
27013b
+    }
27013b
+
27013b
     code = krb5_copy_authdata(context, in_creds->authdata, &ctx->authdata);
27013b
     if (code != 0)
27013b
         goto cleanup;
27013b
@@ -1139,6 +1152,7 @@ krb5_tkt_creds_free(krb5_context context, krb5_tkt_creds_context ctx)
27013b
     krb5int_fast_free_state(context, ctx->fast_state);
27013b
     krb5_free_creds(context, ctx->in_creds);
27013b
     krb5_cc_close(context, ctx->ccache);
27013b
+    krb5_free_data_contents(context, &ctx->start_realm);
27013b
     krb5_free_principal(context, ctx->req_server);
27013b
     krb5_free_authdata(context, ctx->authdata);
27013b
     krb5_free_creds(context, ctx->cur_tgt);
27013b
diff --git a/src/lib/krb5/krb/get_in_tkt.c b/src/lib/krb5/krb/get_in_tkt.c
27013b
index cc0f70e83..f5dd7518b 100644
27013b
--- a/src/lib/krb5/krb/get_in_tkt.c
27013b
+++ b/src/lib/krb5/krb/get_in_tkt.c
27013b
@@ -1779,7 +1779,7 @@ init_creds_step_reply(krb5_context context,
27013b
         code = krb5_cc_initialize(context, out_ccache, ctx->cred.client);
27013b
         if (code != 0)
27013b
             goto cc_cleanup;
27013b
-        code = krb5_cc_store_cred(context, out_ccache, &ctx->cred);
27013b
+        code = k5_cc_store_primary_cred(context, out_ccache, &ctx->cred);
27013b
         if (code != 0)
27013b
             goto cc_cleanup;
27013b
         if (fast_avail) {
27013b
diff --git a/src/lib/krb5/libkrb5.exports b/src/lib/krb5/libkrb5.exports
27013b
index 5aba29ee4..cab5b3b17 100644
27013b
--- a/src/lib/krb5/libkrb5.exports
27013b
+++ b/src/lib/krb5/libkrb5.exports
27013b
@@ -125,6 +125,7 @@ k5_add_pa_data_from_data
27013b
 k5_alloc_pa_data
27013b
 k5_authind_decode
27013b
 k5_build_conf_principals
27013b
+k5_cc_store_primary_cred
27013b
 k5_ccselect_free_context
27013b
 k5_change_error_message_code
27013b
 k5_etypes_contains
27013b
diff --git a/src/lib/krb5_32.def b/src/lib/krb5_32.def
27013b
index a0734c729..de5823c17 100644
27013b
--- a/src/lib/krb5_32.def
27013b
+++ b/src/lib/krb5_32.def
27013b
@@ -499,3 +499,6 @@ EXPORTS
27013b
 	k5_size_context					@467 ; PRIVATE GSSAPI
27013b
 	k5_size_keyblock				@468 ; PRIVATE GSSAPI
27013b
 	k5_size_principal				@469 ; PRIVATE GSSAPI
27013b
+
27013b
+; new in 1.19
27013b
+	k5_cc_store_primary_cred			@470 ; PRIVATE
27013b
diff --git a/src/tests/t_crossrealm.py b/src/tests/t_crossrealm.py
27013b
index fa7fd2604..28b397cfb 100755
27013b
--- a/src/tests/t_crossrealm.py
27013b
+++ b/src/tests/t_crossrealm.py
27013b
@@ -77,6 +77,14 @@ r1, r2, r3 = cross_realms(3, xtgts=((0,1), (1,2)),
27013b
                                 {'realm': 'B.X'}))
27013b
 test_kvno(r1, r3.host_princ, 'KDC domain walk')
27013b
 check_klist(r1, (tgt(r1, r1), r3.host_princ))
27013b
+
27013b
+# Test start_realm in this setup.
27013b
+r1.run([kvno, '--out-cache', r1.ccache, r2.krbtgt_princ])
27013b
+r1.run([klist, '-C'], expected_msg='config: start_realm = X')
27013b
+msgs = ('Requesting TGT krbtgt/B.X@X using TGT krbtgt/X@X',
27013b
+        'Received TGT for service realm: krbtgt/B.X@X')
27013b
+r1.run([kvno, r3.host_princ], expected_trace=msgs)
27013b
+
27013b
 stop(r1, r2, r3)
27013b
 
27013b
 # Test client capaths.  The client in A will ask for a cross TGT to D,
27013b
diff --git a/src/tests/t_pkinit.py b/src/tests/t_pkinit.py
27013b
index ecd450e8a..f224383c8 100755
27013b
--- a/src/tests/t_pkinit.py
27013b
+++ b/src/tests/t_pkinit.py
27013b
@@ -130,6 +130,9 @@ realm.run([kvno, realm.host_princ])
27013b
 out = realm.run(['./adata', realm.host_princ])
27013b
 if '97:' in out:
27013b
     fail('auth indicators seen in anonymous PKINIT ticket')
27013b
+# Verify start_realm setting and test referrals TGS request.
27013b
+realm.run([klist, '-C'], expected_msg='start_realm = KRBTEST.COM')
27013b
+realm.run([kvno, '-S', 'host', hostname])
27013b
 
27013b
 # Test anonymous kadmin.
27013b
 mark('anonymous kadmin')