Blame SOURCES/Use-KCM_OP_RETRIEVE-in-KCM-client.patch

57b2bb
From 00a2ccfeaeac7a0019a73a97cfe33063ba90c7f3 Mon Sep 17 00:00:00 2001
57b2bb
From: Greg Hudson <ghudson@mit.edu>
57b2bb
Date: Fri, 26 Mar 2021 23:38:54 -0400
57b2bb
Subject: [PATCH] Use KCM_OP_RETRIEVE in KCM client
57b2bb
57b2bb
In kcm_retrieve(), try KCM_OP_RETRIEVE.  Fall back to iteration if the
57b2bb
server doesn't implement it, or if we can an answer incompatible with
57b2bb
KRB5_TC_SUPPORTED_KTYPES.
57b2bb
57b2bb
In kcmserver.py, implement partial decoding for creds and cred tags so
57b2bb
that we can do a basic principal name match.
57b2bb
57b2bb
ticket: 8997 (new)
57b2bb
(cherry picked from commit 795ebba8c039be172ab93cd41105c73ffdba0fdb)
57b2bb
(cherry picked from commit c56d4b87de0f30a38dc61d374ad225d02d581eb3)
57b2bb
(cherry picked from commit ac0a117096324fa73afae291ed467f2ea66e279b)
57b2bb
---
57b2bb
 src/include/kcm.h            |  2 +-
57b2bb
 src/lib/krb5/ccache/cc_kcm.c | 52 +++++++++++++++++++++++++++++++++---
57b2bb
 src/tests/kcmserver.py       | 44 +++++++++++++++++++++++++++---
57b2bb
 src/tests/t_ccache.py        | 11 +++++---
57b2bb
 4 files changed, 99 insertions(+), 10 deletions(-)
57b2bb
57b2bb
diff --git a/src/include/kcm.h b/src/include/kcm.h
57b2bb
index 9b66f1cbd..85c20d345 100644
57b2bb
--- a/src/include/kcm.h
57b2bb
+++ b/src/include/kcm.h
57b2bb
@@ -87,7 +87,7 @@ typedef enum kcm_opcode {
57b2bb
     KCM_OP_INITIALIZE,          /*          (name, princ) -> ()          */
57b2bb
     KCM_OP_DESTROY,             /*                 (name) -> ()          */
57b2bb
     KCM_OP_STORE,               /*           (name, cred) -> ()          */
57b2bb
-    KCM_OP_RETRIEVE,
57b2bb
+    KCM_OP_RETRIEVE,            /* (name, flags, credtag) -> (cred)      */
57b2bb
     KCM_OP_GET_PRINCIPAL,       /*                 (name) -> (princ)     */
57b2bb
     KCM_OP_GET_CRED_UUID_LIST,  /*                 (name) -> (uuid, ...) */
57b2bb
     KCM_OP_GET_CRED_BY_UUID,    /*           (name, uuid) -> (cred)      */
57b2bb
diff --git a/src/lib/krb5/ccache/cc_kcm.c b/src/lib/krb5/ccache/cc_kcm.c
57b2bb
index dae622feb..b600c6f15 100644
57b2bb
--- a/src/lib/krb5/ccache/cc_kcm.c
57b2bb
+++ b/src/lib/krb5/ccache/cc_kcm.c
57b2bb
@@ -826,9 +826,55 @@ static krb5_error_code KRB5_CALLCONV
57b2bb
 kcm_retrieve(krb5_context context, krb5_ccache cache, krb5_flags flags,
57b2bb
              krb5_creds *mcred, krb5_creds *cred_out)
57b2bb
 {
57b2bb
-    /* There is a KCM opcode for retrieving creds, but Heimdal's client doesn't
57b2bb
-     * use it.  It causes the KCM daemon to actually make a TGS request. */
57b2bb
-    return k5_cc_retrieve_cred_default(context, cache, flags, mcred, cred_out);
57b2bb
+    krb5_error_code ret;
57b2bb
+    struct kcmreq req = EMPTY_KCMREQ;
57b2bb
+    krb5_creds cred;
57b2bb
+    krb5_enctype *enctypes = NULL;
57b2bb
+
57b2bb
+    memset(&cred, 0, sizeof(cred));
57b2bb
+
57b2bb
+    /* Include KCM_GC_CACHED in flags to prevent Heimdal's sssd from making a
57b2bb
+     * TGS request itself. */
57b2bb
+    kcmreq_init(&req, KCM_OP_RETRIEVE, cache);
57b2bb
+    k5_buf_add_uint32_be(&req.reqbuf, map_tcflags(flags) | KCM_GC_CACHED);
57b2bb
+    k5_marshal_mcred(&req.reqbuf, mcred);
57b2bb
+    ret = cache_call(context, cache, &req;;
57b2bb
+
57b2bb
+    /* Fall back to iteration if the server does not support retrieval. */
57b2bb
+    if (ret == KRB5_FCC_INTERNAL || ret == KRB5_CC_IO) {
57b2bb
+        ret = k5_cc_retrieve_cred_default(context, cache, flags, mcred,
57b2bb
+                                          cred_out);
57b2bb
+        goto cleanup;
57b2bb
+    }
57b2bb
+    if (ret)
57b2bb
+        goto cleanup;
57b2bb
+
57b2bb
+    ret = k5_unmarshal_cred(req.reply.ptr, req.reply.len, 4, &cred);
57b2bb
+    if (ret)
57b2bb
+        goto cleanup;
57b2bb
+
57b2bb
+    /* In rare cases we might retrieve a credential with a session key this
57b2bb
+     * context can't support, in which case we must retry using iteration. */
57b2bb
+    if (flags & KRB5_TC_SUPPORTED_KTYPES) {
57b2bb
+        ret = krb5_get_tgs_ktypes(context, cred.server, &enctypes);
57b2bb
+        if (ret)
57b2bb
+            goto cleanup;
57b2bb
+        if (!k5_etypes_contains(enctypes, cred.keyblock.enctype)) {
57b2bb
+            ret = k5_cc_retrieve_cred_default(context, cache, flags, mcred,
57b2bb
+                                              cred_out);
57b2bb
+            goto cleanup;
57b2bb
+        }
57b2bb
+    }
57b2bb
+
57b2bb
+    *cred_out = cred;
57b2bb
+    memset(&cred, 0, sizeof(cred));
57b2bb
+
57b2bb
+cleanup:
57b2bb
+    kcmreq_free(&req;;
57b2bb
+    krb5_free_cred_contents(context, &cred);
57b2bb
+    free(enctypes);
57b2bb
+    /* Heimdal's KCM returns KRB5_CC_END if no cred is found. */
57b2bb
+    return (ret == KRB5_CC_END) ? KRB5_CC_NOTFOUND : map_invalid(ret);
57b2bb
 }
57b2bb
 
57b2bb
 static krb5_error_code KRB5_CALLCONV
57b2bb
diff --git a/src/tests/kcmserver.py b/src/tests/kcmserver.py
57b2bb
index 8c5e66ff1..25e6f2bbe 100644
57b2bb
--- a/src/tests/kcmserver.py
57b2bb
+++ b/src/tests/kcmserver.py
57b2bb
@@ -40,6 +40,7 @@ class KCMOpcodes(object):
57b2bb
     INITIALIZE = 4
57b2bb
     DESTROY = 5
57b2bb
     STORE = 6
57b2bb
+    RETRIEVE = 7
57b2bb
     GET_PRINCIPAL = 8
57b2bb
     GET_CRED_UUID_LIST = 9
57b2bb
     GET_CRED_BY_UUID = 10
57b2bb
@@ -54,6 +55,7 @@ class KCMOpcodes(object):
57b2bb
 
57b2bb
 
57b2bb
 class KRB5Errors(object):
57b2bb
+    KRB5_CC_NOTFOUND = -1765328243
57b2bb
     KRB5_CC_END = -1765328242
57b2bb
     KRB5_CC_NOSUPP = -1765328137
57b2bb
     KRB5_FCC_NOFILE = -1765328189
57b2bb
@@ -86,11 +88,29 @@ def get_cache(name):
57b2bb
     return cache
57b2bb
 
57b2bb
 
57b2bb
+def unpack_data(argbytes):
57b2bb
+    dlen, = struct.unpack('>L', argbytes[:4])
57b2bb
+    return argbytes[4:dlen+4], argbytes[dlen+4:]
57b2bb
+
57b2bb
+
57b2bb
 def unmarshal_name(argbytes):
57b2bb
     offset = argbytes.find(b'\0')
57b2bb
     return argbytes[0:offset], argbytes[offset+1:]
57b2bb
 
57b2bb
 
57b2bb
+def unmarshal_princ(argbytes):
57b2bb
+    # Ignore the type at argbytes[0:4].
57b2bb
+    ncomps, = struct.unpack('>L', argbytes[4:8])
57b2bb
+    realm, rest = unpack_data(argbytes[8:])
57b2bb
+    comps = []
57b2bb
+    for i in range(ncomps):
57b2bb
+        comp, rest = unpack_data(rest)
57b2bb
+        comps.append(comp)
57b2bb
+    # Asssume no quoting is needed.
57b2bb
+    princ = b'/'.join(comps) + b'@' + realm
57b2bb
+    return princ, rest
57b2bb
+
57b2bb
+
57b2bb
 def op_gen_new(argbytes):
57b2bb
     # Does not actually check for uniqueness.
57b2bb
     global next_unique
57b2bb
@@ -126,6 +146,22 @@ def op_store(argbytes):
57b2bb
     return 0, b''
57b2bb
 
57b2bb
 
57b2bb
+def op_retrieve(argbytes):
57b2bb
+    name, rest = unmarshal_name(argbytes)
57b2bb
+    # Ignore the flags at rest[0:4] and the header at rest[4:8].
57b2bb
+    # Assume there are client and server creds in the tag and match
57b2bb
+    # only against them.
57b2bb
+    cprinc, rest = unmarshal_princ(rest[8:])
57b2bb
+    sprinc, rest = unmarshal_princ(rest)
57b2bb
+    cache = get_cache(name)
57b2bb
+    for cred in (cache.creds[u] for u in cache.cred_uuids):
57b2bb
+        cred_cprinc, rest = unmarshal_princ(cred)
57b2bb
+        cred_sprinc, rest = unmarshal_princ(rest)
57b2bb
+        if cred_cprinc == cprinc and cred_sprinc == sprinc:
57b2bb
+            return 0, cred
57b2bb
+    return KRB5Errors.KRB5_CC_NOTFOUND, b''
57b2bb
+
57b2bb
+
57b2bb
 def op_get_principal(argbytes):
57b2bb
     name, rest = unmarshal_name(argbytes)
57b2bb
     cache = get_cache(name)
57b2bb
@@ -199,6 +235,7 @@ ophandlers = {
57b2bb
     KCMOpcodes.INITIALIZE : op_initialize,
57b2bb
     KCMOpcodes.DESTROY : op_destroy,
57b2bb
     KCMOpcodes.STORE : op_store,
57b2bb
+    KCMOpcodes.RETRIEVE : op_retrieve,
57b2bb
     KCMOpcodes.GET_PRINCIPAL : op_get_principal,
57b2bb
     KCMOpcodes.GET_CRED_UUID_LIST : op_get_cred_uuid_list,
57b2bb
     KCMOpcodes.GET_CRED_BY_UUID : op_get_cred_by_uuid,
57b2bb
@@ -243,10 +280,11 @@ def service_request(s):
57b2bb
     return True
57b2bb
 
57b2bb
 parser = optparse.OptionParser()
57b2bb
-parser.add_option('-c', '--credlist', action='store_true', dest='credlist',
57b2bb
-                  default=False, help='Support KCM_OP_GET_CRED_LIST')
57b2bb
+parser.add_option('-f', '--fallback', action='store_true', dest='fallback',
57b2bb
+                  default=False, help='Do not support RETRIEVE/GET_CRED_LIST')
57b2bb
 (options, args) = parser.parse_args()
57b2bb
-if not options.credlist:
57b2bb
+if options.fallback:
57b2bb
+    del ophandlers[KCMOpcodes.RETRIEVE]
57b2bb
     del ophandlers[KCMOpcodes.GET_CRED_LIST]
57b2bb
 
57b2bb
 server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
57b2bb
diff --git a/src/tests/t_ccache.py b/src/tests/t_ccache.py
57b2bb
index 90040fb7b..6ea9fb969 100755
57b2bb
--- a/src/tests/t_ccache.py
57b2bb
+++ b/src/tests/t_ccache.py
57b2bb
@@ -25,7 +25,7 @@ from k5test import *
57b2bb
 kcm_socket_path = os.path.join(os.getcwd(), 'testdir', 'kcm')
57b2bb
 conf = {'libdefaults': {'kcm_socket': kcm_socket_path,
57b2bb
                         'kcm_mach_service': '-'}}
57b2bb
-realm = K5Realm(create_host=False, krb5_conf=conf)
57b2bb
+realm = K5Realm(krb5_conf=conf)
57b2bb
 
57b2bb
 keyctl = which('keyctl')
57b2bb
 out = realm.run([klist, '-c', 'KEYRING:process:abcd'], expected_code=1)
57b2bb
@@ -71,6 +71,11 @@ def collection_test(realm, ccname):
57b2bb
     realm.kinit('alice', password('alice'))
57b2bb
     realm.run([klist], expected_msg='Default principal: alice@')
57b2bb
     realm.run([klist, '-A', '-s'])
57b2bb
+    realm.run([kvno, realm.host_princ], expected_msg = 'kvno = 1')
57b2bb
+    realm.run([kvno, realm.host_princ], expected_msg = 'kvno = 1')
57b2bb
+    out = realm.run([klist])
57b2bb
+    if out.count(realm.host_princ) != 1:
57b2bb
+        fail('Wrong number of service tickets in cache')
57b2bb
     realm.run([kdestroy])
57b2bb
     output = realm.run([klist], expected_code=1)
57b2bb
     if 'No credentials cache' not in output and 'not found' not in output:
57b2bb
@@ -126,14 +131,14 @@ def collection_test(realm, ccname):
57b2bb
 
57b2bb
 collection_test(realm, 'DIR:' + os.path.join(realm.testdir, 'cc'))
57b2bb
 
57b2bb
-# Test KCM without and with GET_CRED_LIST support.
57b2bb
+# Test KCM with and without RETRIEVE and GET_CRED_LIST support.
57b2bb
 kcmserver_path = os.path.join(srctop, 'tests', 'kcmserver.py')
57b2bb
 kcmd = realm.start_server([sys.executable, kcmserver_path, kcm_socket_path],
57b2bb
                           'starting...')
57b2bb
 collection_test(realm, 'KCM:')
57b2bb
 stop_daemon(kcmd)
57b2bb
 os.remove(kcm_socket_path)
57b2bb
-realm.start_server([sys.executable, kcmserver_path, '-c', kcm_socket_path],
57b2bb
+realm.start_server([sys.executable, kcmserver_path, '-f', kcm_socket_path],
57b2bb
                    'starting...')
57b2bb
 collection_test(realm, 'KCM:')
57b2bb