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

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