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