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