c4016b4
From 2d2bb9a14613b3283dabdd40c3ee28e5b680cf93 Mon Sep 17 00:00:00 2001
4df0096
From: Greg Hudson <ghudson@mit.edu>
4df0096
Date: Mon, 7 Jun 2021 15:00:41 -0400
4df0096
Subject: [PATCH] Fix kadmin -k with fallback or referral realm
4df0096
4df0096
kadmin -k produces a client principal name with
4df0096
krb5_sname_to_principal(), but it gets converted to a string and back
4df0096
due to the signature of kadm5_init_with_skey(), which loses track of
4df0096
the name type, so no canonicalization is performed.
4df0096
4df0096
In libkadm5clnt initialization, recognize the important subset of this
4df0096
case--an empty realm indicates either fallback processing or the
4df0096
referral realm--and restore the host-based name type so that the
4df0096
client principal can be canonicalized against the keytab.
4df0096
4df0096
ticket: 9013 (new)
4df0096
(cherry picked from commit dcb79089276624d7ddf44e08d35bd6d7d7e557d2)
4df0096
---
4df0096
 src/lib/kadm5/clnt/client_init.c |  7 +++++++
4df0096
 src/tests/t_kadmin.py            | 12 ++++++++++++
4df0096
 2 files changed, 19 insertions(+)
4df0096
4df0096
diff --git a/src/lib/kadm5/clnt/client_init.c b/src/lib/kadm5/clnt/client_init.c
4df0096
index aa1223bb3..0aaca701f 100644
4df0096
--- a/src/lib/kadm5/clnt/client_init.c
4df0096
+++ b/src/lib/kadm5/clnt/client_init.c
4df0096
@@ -221,9 +221,16 @@ init_any(krb5_context context, char *client_name, enum init_type init_type,
4df0096
         return KADM5_MISSING_KRB5_CONF_PARAMS;
4df0096
     }
4df0096
 
4df0096
+    /*
4df0096
+     * Parse the client name.  If it has an empty realm, it is almost certainly
4df0096
+     * a host-based principal using DNS fallback processing or the referral
4df0096
+     * realm, so give it the appropriate name type for canonicalization.
4df0096
+     */
4df0096
     code = krb5_parse_name(handle->context, client_name, &client);
4df0096
     if (code)
4df0096
         goto error;
4df0096
+    if (init_type == INIT_SKEY && client->realm.length == 0)
4df0096
+        client->type = KRB5_NT_SRV_HST;
4df0096
 
4df0096
     /*
4df0096
      * Get credentials.  Also does some fallbacks in case kadmin/fqdn
4df0096
diff --git a/src/tests/t_kadmin.py b/src/tests/t_kadmin.py
4df0096
index fe6a3cc2e..98453d92e 100644
4df0096
--- a/src/tests/t_kadmin.py
4df0096
+++ b/src/tests/t_kadmin.py
4df0096
@@ -51,4 +51,16 @@ for i in range(200):
4df0096
     realm.run_kadmin(['addprinc', '-randkey', 'foo%d' % i])
4df0096
 realm.run_kadmin(['listprincs'], expected_msg='foo199')
4df0096
 
4df0096
+# Test kadmin -k with the default principal, with and without
4df0096
+# fallback.  This operation requires canonicalization against the
4df0096
+# keytab in krb5_get_init_creds_keytab() as the
4df0096
+# krb5_sname_to_principal() result won't have a realm.  Try with and
4df0096
+# without without fallback processing since the code paths are
4df0096
+# different.
4df0096
+mark('kadmin -k')
4df0096
+realm.run([kadmin, '-k', 'getprinc', realm.host_princ])
4df0096
+no_canon_conf = {'libdefaults': {'dns_canonicalize_hostname': 'false'}}
4df0096
+no_canon = realm.special_env('no_canon', False, krb5_conf=no_canon_conf)
4df0096
+realm.run([kadmin, '-k', 'getprinc', realm.host_princ], env=no_canon)
4df0096
+
4df0096
 success('kadmin and kpasswd tests')