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

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