Blame SOURCES/Add-three-kvno-options-from-Heimdal-kgetcred.patch

27013b
From a1f38973435b60c7f147abfca12b95c6a0a64406 Mon Sep 17 00:00:00 2001
27013b
From: Greg Hudson <ghudson@mit.edu>
27013b
Date: Wed, 17 Jun 2020 20:48:38 -0400
27013b
Subject: [PATCH] Add three kvno options from Heimdal kgetcred
27013b
27013b
Add the flags --cached-only and --no-store, which pass the
27013b
corresponding options to krb5_get_credentials().  Add the option
27013b
--out-cache to write the retrieved credentials to a specified output
27013b
cache.
27013b
27013b
Add a Python test script for kvno command-line options, including
27013b
tests for the new options.
27013b
27013b
ticket: 8917 (new)
27013b
---
27013b
 doc/user/user_commands/kvno.rst |  13 ++++
27013b
 src/clients/kvno/Makefile.in    |   3 +
27013b
 src/clients/kvno/kvno.c         | 115 +++++++++++++++++++++++---------
27013b
 src/clients/kvno/t_kvno.py      |  75 +++++++++++++++++++++
27013b
 src/man/kvno.man                |  13 ++++
27013b
 5 files changed, 187 insertions(+), 32 deletions(-)
27013b
 create mode 100644 src/clients/kvno/t_kvno.py
27013b
27013b
diff --git a/doc/user/user_commands/kvno.rst b/doc/user/user_commands/kvno.rst
27013b
index 3892f0ca5..718313576 100644
27013b
--- a/doc/user/user_commands/kvno.rst
27013b
+++ b/doc/user/user_commands/kvno.rst
27013b
@@ -74,6 +74,19 @@ OPTIONS
27013b
     client principal with the X.509 certificate in *cert_file*.  The
27013b
     certificate file must be in PEM format.
27013b
 
27013b
+**--cached-only**
27013b
+    Only retrieve credentials already present in the cache, not from
27013b
+    the KDC.
27013b
+
27013b
+**--no-store**
27013b
+    Do not store retrieved credentials in the cache.  If
27013b
+    **--out-cache** is also specified, credentials will still be
27013b
+    stored into the output credential cache.
27013b
+
27013b
+**--out-cache** *ccache*
27013b
+    Initialize *ccache* and store all retrieved credentials into it.
27013b
+    Do not store acquired credentials in the input cache.
27013b
+
27013b
 **--u2u** *ccache*
27013b
     Requests a user-to-user ticket.  *ccache* must contain a local
27013b
     krbtgt ticket for the server principal.  The reported version
27013b
diff --git a/src/clients/kvno/Makefile.in b/src/clients/kvno/Makefile.in
27013b
index 1c3f79392..5ba877271 100644
27013b
--- a/src/clients/kvno/Makefile.in
27013b
+++ b/src/clients/kvno/Makefile.in
27013b
@@ -26,6 +26,9 @@ kvno: kvno.o $(KRB5_BASE_DEPLIBS)
27013b
 ##WIN32##	link $(EXE_LINKOPTS) /out:$@ $**
27013b
 ##WIN32##	$(_VC_MANIFEST_EMBED_EXE)
27013b
 
27013b
+check-pytests: kvno
27013b
+	$(RUNPYTEST) $(srcdir)/t_kvno.py $(PYTESTFLAGS)
27013b
+
27013b
 clean-unix::
27013b
 	$(RM) kvno.o kvno
27013b
 
27013b
diff --git a/src/clients/kvno/kvno.c b/src/clients/kvno/kvno.c
27013b
index 2472c0cfe..9d85864f6 100644
27013b
--- a/src/clients/kvno/kvno.c
27013b
+++ b/src/clients/kvno/kvno.c
27013b
@@ -44,14 +44,17 @@ xusage()
27013b
     fprintf(stderr, _("usage: %s [-C] [-u] [-c ccache] [-e etype]\n"), prog);
27013b
     fprintf(stderr, _("\t[-k keytab] [-S sname] [{-I | -U} for_user | "
27013b
                       "[-F cert_file] [-P]]\n"));
27013b
-    fprintf(stderr, _("\t[--u2u ccache] service1 service2 ...\n"));
27013b
+    fprintf(stderr, _("\t[--cached-only] [--no-store] [--out-cache ccache] "
27013b
+                      "[--u2u ccache]\n"));
27013b
+    fprintf(stderr, _("\tservice1 service2 ...\n"));
27013b
     exit(1);
27013b
 }
27013b
 
27013b
 static void do_v5_kvno(int argc, char *argv[], char *ccachestr, char *etypestr,
27013b
-                       char *keytab_name, char *sname, int canon, int unknown,
27013b
-                       char *for_user, int for_user_enterprise,
27013b
-                       char *for_user_cert_file, int proxy,
27013b
+                       char *keytab_name, char *sname, int cached_only,
27013b
+                       int canon, int no_store, int unknown, char *for_user,
27013b
+                       int for_user_enterprise, char *for_user_cert_file,
27013b
+                       int proxy, const char *out_ccname,
27013b
                        const char *u2u_ccname);
27013b
 
27013b
 #include <com_err.h>
27013b
@@ -61,18 +64,21 @@ static void extended_com_err_fn(const char *myprog, errcode_t code,
27013b
 int
27013b
 main(int argc, char *argv[])
27013b
 {
27013b
-    enum { OPTION_U2U = 256 };
27013b
-    struct option lopts[] = {
27013b
-        { "u2u", 1, NULL, OPTION_U2U },
27013b
-        { NULL, 0, NULL, 0 }
27013b
-    };
27013b
+    enum { OPTION_U2U = 256, OPTION_OUT_CACHE = 257 };
27013b
     const char *shopts = "uCc:e:hk:qPS:I:U:F:";
27013b
     int option;
27013b
     char *etypestr = NULL, *ccachestr = NULL, *keytab_name = NULL;
27013b
     char *sname = NULL, *for_user = NULL, *u2u_ccname = NULL;
27013b
-    char *for_user_cert_file = NULL;
27013b
+    char *for_user_cert_file = NULL, *out_ccname = NULL;
27013b
     int canon = 0, unknown = 0, proxy = 0, for_user_enterprise = 0;
27013b
-    int impersonate = 0;
27013b
+    int impersonate = 0, cached_only = 0, no_store = 0;
27013b
+    struct option lopts[] = {
27013b
+        { "cached-only", 0, &cached_only, 1 },
27013b
+        { "no-store", 0, &no_store, 1 },
27013b
+        { "out-cache", 1, NULL, OPTION_OUT_CACHE },
27013b
+        { "u2u", 1, NULL, OPTION_U2U },
27013b
+        { NULL, 0, NULL, 0 }
27013b
+    };
27013b
 
27013b
     setlocale(LC_ALL, "");
27013b
     set_com_err_hook(extended_com_err_fn);
27013b
@@ -135,6 +141,12 @@ main(int argc, char *argv[])
27013b
         case OPTION_U2U:
27013b
             u2u_ccname = optarg;
27013b
             break;
27013b
+        case OPTION_OUT_CACHE:
27013b
+            out_ccname = optarg;
27013b
+            break;
27013b
+        case 0:
27013b
+            /* If this option set a flag, do nothing else now. */
27013b
+            break;
27013b
         default:
27013b
             xusage();
27013b
             break;
27013b
@@ -159,8 +171,9 @@ main(int argc, char *argv[])
27013b
         xusage();
27013b
 
27013b
     do_v5_kvno(argc - optind, argv + optind, ccachestr, etypestr, keytab_name,
27013b
-               sname, canon, unknown, for_user, for_user_enterprise,
27013b
-               for_user_cert_file, proxy, u2u_ccname);
27013b
+               sname, cached_only, canon, no_store, unknown, for_user,
27013b
+               for_user_enterprise, for_user_cert_file, proxy, out_ccname,
27013b
+               u2u_ccname);
27013b
     return 0;
27013b
 }
27013b
 
27013b
@@ -274,14 +287,16 @@ static krb5_error_code
27013b
 kvno(const char *name, krb5_ccache ccache, krb5_principal me,
27013b
      krb5_enctype etype, krb5_keytab keytab, const char *sname,
27013b
      krb5_flags options, int unknown, krb5_principal for_user_princ,
27013b
-     krb5_data *for_user_cert, int proxy, krb5_data *u2u_ticket)
27013b
+     krb5_data *for_user_cert, int proxy, krb5_data *u2u_ticket,
27013b
+     krb5_creds **creds_out)
27013b
 {
27013b
     krb5_error_code ret;
27013b
     krb5_principal server = NULL;
27013b
     krb5_ticket *ticket = NULL;
27013b
-    krb5_creds in_creds, *out_creds = NULL;
27013b
+    krb5_creds in_creds, *creds = NULL;
27013b
     char *princ = NULL;
27013b
 
27013b
+    *creds_out = NULL;
27013b
     memset(&in_creds, 0, sizeof(in_creds));
27013b
 
27013b
     if (sname != NULL) {
27013b
@@ -321,13 +336,12 @@ kvno(const char *name, krb5_ccache ccache, krb5_principal me,
27013b
         in_creds.client = for_user_princ;
27013b
         in_creds.server = me;
27013b
         ret = krb5_get_credentials_for_user(context, options, ccache,
27013b
-                                            &in_creds, for_user_cert,
27013b
-                                            &out_creds);
27013b
+                                            &in_creds, for_user_cert, &creds);
27013b
     } else {
27013b
         in_creds.client = me;
27013b
         in_creds.server = server;
27013b
         ret = krb5_get_credentials(context, options, ccache, &in_creds,
27013b
-                                   &out_creds);
27013b
+                                   &creds);
27013b
     }
27013b
 
27013b
     if (ret) {
27013b
@@ -336,7 +350,7 @@ kvno(const char *name, krb5_ccache ccache, krb5_principal me,
27013b
     }
27013b
 
27013b
     /* We need a native ticket. */
27013b
-    ret = krb5_decode_ticket(&out_creds->ticket, &ticket);
27013b
+    ret = krb5_decode_ticket(&creds->ticket, &ticket);
27013b
     if (ret) {
27013b
         com_err(prog, ret, _("while decoding ticket for %s"), princ);
27013b
         goto cleanup;
27013b
@@ -362,15 +376,15 @@ kvno(const char *name, krb5_ccache ccache, krb5_principal me,
27013b
     }
27013b
 
27013b
     if (proxy) {
27013b
-        in_creds.client = out_creds->client;
27013b
-        out_creds->client = NULL;
27013b
-        krb5_free_creds(context, out_creds);
27013b
-        out_creds = NULL;
27013b
+        in_creds.client = creds->client;
27013b
+        creds->client = NULL;
27013b
+        krb5_free_creds(context, creds);
27013b
+        creds = NULL;
27013b
         in_creds.server = server;
27013b
 
27013b
         ret = krb5_get_credentials_for_proxy(context, KRB5_GC_CANONICALIZE,
27013b
                                              ccache, &in_creds, ticket,
27013b
-                                             &out_creds);
27013b
+                                             &creds);
27013b
         krb5_free_principal(context, in_creds.client);
27013b
         if (ret) {
27013b
             com_err(prog, ret, _("%s: constrained delegation failed"),
27013b
@@ -379,10 +393,13 @@ kvno(const char *name, krb5_ccache ccache, krb5_principal me,
27013b
         }
27013b
     }
27013b
 
27013b
+    *creds_out = creds;
27013b
+    creds = NULL;
27013b
+
27013b
 cleanup:
27013b
     krb5_free_principal(context, server);
27013b
     krb5_free_ticket(context, ticket);
27013b
-    krb5_free_creds(context, out_creds);
27013b
+    krb5_free_creds(context, creds);
27013b
     krb5_free_unparsed_name(context, princ);
27013b
     return ret;
27013b
 }
27013b
@@ -428,19 +445,28 @@ cleanup:
27013b
 
27013b
 static void
27013b
 do_v5_kvno(int count, char *names[], char * ccachestr, char *etypestr,
27013b
-           char *keytab_name, char *sname, int canon, int unknown,
27013b
-           char *for_user, int for_user_enterprise,
27013b
-           char *for_user_cert_file, int proxy, const char *u2u_ccname)
27013b
+           char *keytab_name, char *sname, int cached_only, int canon,
27013b
+           int no_store, int unknown, char *for_user, int for_user_enterprise,
27013b
+           char *for_user_cert_file, int proxy, const char *out_ccname,
27013b
+           const char *u2u_ccname)
27013b
 {
27013b
     krb5_error_code ret;
27013b
-    int i, errors, flags;
27013b
+    int i, errors, flags, initialized = 0;
27013b
     krb5_enctype etype;
27013b
-    krb5_ccache ccache;
27013b
+    krb5_ccache ccache, out_ccache = NULL;
27013b
     krb5_principal me;
27013b
     krb5_keytab keytab = NULL;
27013b
     krb5_principal for_user_princ = NULL;
27013b
-    krb5_flags options = canon ? KRB5_GC_CANONICALIZE : 0;
27013b
+    krb5_flags options = 0;
27013b
     krb5_data cert_data = empty_data(), *user_cert = NULL, *u2u_ticket = NULL;
27013b
+    krb5_creds *creds;
27013b
+
27013b
+    if (canon)
27013b
+        options |= KRB5_GC_CANONICALIZE;
27013b
+    if (cached_only)
27013b
+        options |= KRB5_GC_CACHED;
27013b
+    if (no_store || out_ccname != NULL)
27013b
+        options |= KRB5_GC_NO_STORE;
27013b
 
27013b
     ret = krb5_init_context(&context);
27013b
     if (ret) {
27013b
@@ -467,6 +493,14 @@ do_v5_kvno(int count, char *names[], char * ccachestr, char *etypestr,
27013b
         exit(1);
27013b
     }
27013b
 
27013b
+    if (out_ccname != NULL) {
27013b
+        ret = krb5_cc_resolve(context, out_ccname, &out_ccache);
27013b
+        if (ret) {
27013b
+            com_err(prog, ret, _("while resolving output ccache"));
27013b
+            exit(1);
27013b
+        }
27013b
+    }
27013b
+
27013b
     if (keytab_name != NULL) {
27013b
         ret = krb5_kt_resolve(context, keytab_name, &keytab);
27013b
         if (ret) {
27013b
@@ -513,8 +547,25 @@ do_v5_kvno(int count, char *names[], char * ccachestr, char *etypestr,
27013b
     errors = 0;
27013b
     for (i = 0; i < count; i++) {
27013b
         if (kvno(names[i], ccache, me, etype, keytab, sname, options, unknown,
27013b
-                 for_user_princ, user_cert, proxy, u2u_ticket) != 0)
27013b
+                 for_user_princ, user_cert, proxy, u2u_ticket, &creds) != 0) {
27013b
             errors++;
27013b
+        } else if (out_ccache != NULL) {
27013b
+            if (!initialized) {
27013b
+                ret = krb5_cc_initialize(context, out_ccache, creds->client);
27013b
+                if (ret) {
27013b
+                    com_err(prog, ret, _("while initializing output ccache"));
27013b
+                    exit(1);
27013b
+                }
27013b
+                initialized = 1;
27013b
+            }
27013b
+            ret = krb5_cc_store_cred(context, out_ccache, creds);
27013b
+            if (ret) {
27013b
+                com_err(prog, ret, _("while storing creds in output ccache"));
27013b
+                exit(1);
27013b
+            }
27013b
+        }
27013b
+
27013b
+        krb5_free_creds(context, creds);
27013b
     }
27013b
 
27013b
     if (keytab != NULL)
27013b
diff --git a/src/clients/kvno/t_kvno.py b/src/clients/kvno/t_kvno.py
27013b
new file mode 100644
27013b
index 000000000..e98b90e8a
27013b
--- /dev/null
27013b
+++ b/src/clients/kvno/t_kvno.py
27013b
@@ -0,0 +1,75 @@
27013b
+from k5test import *
27013b
+
27013b
+realm = K5Realm()
27013b
+
27013b
+def check_cache(ccache, expected_services):
27013b
+    # Fetch the klist output and skip past the header.
27013b
+    lines = realm.run([klist, '-c', ccache]).splitlines()
27013b
+    lines = lines[4:]
27013b
+
27013b
+    # For each line not beginning with an indent, match against the
27013b
+    # expected service principals.
27013b
+    svcs = {x: True for x in expected_services}
27013b
+    for l in lines:
27013b
+        if not l.startswith('\t'):
27013b
+            svcprinc = l.split()[4]
27013b
+            if svcprinc in svcs:
27013b
+                del svcs[svcprinc]
27013b
+            else:
27013b
+                fail('unexpected service princ ' + svcprinc)
27013b
+
27013b
+    if svcs:
27013b
+        fail('services not found in klist output: ' + ' '.join(svcs.keys()))
27013b
+
27013b
+
27013b
+mark('no options')
27013b
+realm.run([kvno, realm.user_princ], expected_msg='user@KRBTEST.COM: kvno = 1')
27013b
+check_cache(realm.ccache, [realm.krbtgt_princ, realm.user_princ])
27013b
+
27013b
+mark('-e')
27013b
+msgs = ('etypes requested in TGS request: camellia128-cts',
27013b
+        '/KDC has no support for encryption type')
27013b
+realm.run([kvno, '-e', 'camellia128-cts', realm.host_princ],
27013b
+          expected_code=1, expected_trace=msgs)
27013b
+
27013b
+mark('--cached-only')
27013b
+realm.run([kvno, '--cached-only', realm.user_princ], expected_msg='kvno = 1')
27013b
+realm.run([kvno, '--cached-only', realm.host_princ],
27013b
+          expected_code=1, expected_msg='Matching credential not found')
27013b
+check_cache(realm.ccache, [realm.krbtgt_princ, realm.user_princ])
27013b
+
27013b
+mark('--no-store')
27013b
+realm.run([kvno, '--no-store', realm.host_princ], expected_msg='kvno = 1')
27013b
+check_cache(realm.ccache, [realm.krbtgt_princ, realm.user_princ])
27013b
+
27013b
+mark('--out-cache') # and multiple services
27013b
+out_ccache = os.path.join(realm.testdir, 'ccache.out')
27013b
+realm.run([kvno, '--out-cache', out_ccache,
27013b
+           realm.host_princ, realm.admin_princ])
27013b
+check_cache(realm.ccache, [realm.krbtgt_princ, realm.user_princ])
27013b
+check_cache(out_ccache, [realm.host_princ, realm.admin_princ])
27013b
+
27013b
+mark('--out-cache --cached-only') # tests out-cache overwriting, and -q
27013b
+realm.run([kvno, '--out-cache', out_ccache, '--cached-only', realm.host_princ],
27013b
+          expected_code=1, expected_msg='Matching credential not found')
27013b
+out = realm.run([kvno, '-q', '--out-cache', out_ccache, '--cached-only',
27013b
+                 realm.user_princ])
27013b
+if out:
27013b
+    fail('unexpected kvno output with -q')
27013b
+check_cache(out_ccache, [realm.user_princ])
27013b
+
27013b
+mark('-U') # and -c
27013b
+svc_ccache = os.path.join(realm.testdir, 'ccache.svc')
27013b
+realm.run([kinit, '-k', '-c', svc_ccache, realm.host_princ])
27013b
+realm.run([kvno, '-c', svc_ccache, '-U', 'user', realm.host_princ])
27013b
+realm.run([klist, '-c', svc_ccache], expected_msg='for client user@')
27013b
+realm.run([kvno, '-c', svc_ccache, '-U', 'user', '--out-cache', out_ccache,
27013b
+           realm.host_princ])
27013b
+out = realm.run([klist, '-c', out_ccache])
27013b
+if ('Default principal: user@KRBTEST.COM' not in out):
27013b
+    fail('wrong default principal in klist output')
27013b
+
27013b
+# More S4U options are tested in tests/gssapi/t_s4u.py.
27013b
+# --u2u is tested in tests/t_u2u.py.
27013b
+
27013b
+success('kvno tests')
27013b
diff --git a/src/man/kvno.man b/src/man/kvno.man
27013b
index 005a2ec97..b9f6739eb 100644
27013b
--- a/src/man/kvno.man
27013b
+++ b/src/man/kvno.man
27013b
@@ -95,6 +95,19 @@ Specifies that protocol transition is to be used, identifying the
27013b
 client principal with the X.509 certificate in \fIcert_file\fP\&.  The
27013b
 certificate file must be in PEM format.
27013b
 .TP
27013b
+\fB\-\-cached\-only\fP
27013b
+Only retrieve credentials already present in the cache, not from
27013b
+the KDC.
27013b
+.TP
27013b
+\fB\-\-no\-store\fP
27013b
+Do not store retrieved credentials in the cache.  If
27013b
+\fB\-\-out\-cache\fP is also specified, credentials will still be
27013b
+stored into the output credential cache.
27013b
+.TP
27013b
+\fB\-\-out\-cache\fP \fIccache\fP
27013b
+Initialize \fIccache\fP and store all retrieved credentials into it.
27013b
+Do not store acquired credentials in the input cache.
27013b
+.TP
27013b
 \fB\-\-u2u\fP \fIccache\fP
27013b
 Requests a user\-to\-user ticket.  \fIccache\fP must contain a local
27013b
 krbtgt ticket for the server principal.  The reported version