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

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