167778
From 996c0089cf2e3240e1b331555897e5bf83b023e7 Mon Sep 17 00:00:00 2001
167778
From: Greg Hudson <ghudson@mit.edu>
167778
Date: Wed, 4 Jan 2017 18:31:15 -0500
167778
Subject: [PATCH] Add tests for per-request preauth data scoping
167778
167778
Add a test harness which interleaves calls for multiple initial creds
167778
contexts using the same library context.  Add a test case to
167778
t_preauth.py using the new harness and the test preauth module to
167778
verify that modreq pointers are correctly tracked.
167778
167778
ticket: 7877
167778
(cherry picked from commit c0b25fe282355d4f329418956b9c6295780af633)
167778
[rharwood@redhat.com: drop .gitignore]
167778
---
167778
 src/tests/Makefile.in    |  23 +++++---
167778
 src/tests/icinterleave.c | 124 +++++++++++++++++++++++++++++++++++++++
167778
 src/tests/t_preauth.py   |  13 ++++
167778
 3 files changed, 151 insertions(+), 9 deletions(-)
167778
 create mode 100644 src/tests/icinterleave.c
167778
167778
diff --git a/src/tests/Makefile.in b/src/tests/Makefile.in
167778
index a2093108b..bd1b21346 100644
167778
--- a/src/tests/Makefile.in
167778
+++ b/src/tests/Makefile.in
167778
@@ -6,12 +6,12 @@ SUBDIRS = resolve asn.1 create hammer verify gssapi dejagnu shlib \
167778
 RUN_DB_TEST = $(RUN_SETUP) KRB5_KDC_PROFILE=kdc.conf KRB5_CONFIG=krb5.conf \
167778
 	LC_ALL=C $(VALGRIND)
167778
 
167778
-OBJS= adata.o etinfo.o forward.o gcred.o hist.o hooks.o hrealm.o icred.o \
167778
-	kdbtest.o localauth.o plugorder.o rdreq.o responder.o s2p.o \
167778
-	s4u2proxy.o unlockiter.o
167778
+OBJS= adata.o etinfo.o forward.o gcred.o hist.o hooks.o hrealm.o \
167778
+	icinterleave.o icred.o kdbtest.o localauth.o plugorder.o rdreq.o \
167778
+	responder.o s2p.o s4u2proxy.o unlockiter.o
167778
 EXTRADEPSRCS= adata.c etinfo.c forward.c gcred.c hist.c hooks.c hrealm.c \
167778
-	icred.c kdbtest.c localauth.c plugorder.c rdreq.o responder.c s2p.c \
167778
-	s4u2proxy.c unlockiter.c
167778
+	icinterleave.c icred.c kdbtest.c localauth.c plugorder.c rdreq.o \
167778
+	responder.c s2p.c s4u2proxy.c unlockiter.c
167778
 
167778
 TEST_DB = ./testdb
167778
 TEST_REALM = FOO.TEST.REALM
167778
@@ -44,6 +44,9 @@ hooks: hooks.o $(KRB5_BASE_DEPLIBS)
167778
 hrealm: hrealm.o $(KRB5_BASE_DEPLIBS)
167778
 	$(CC_LINK) -o $@ hrealm.o $(KRB5_BASE_LIBS)
167778
 
167778
+icinterleave: icinterleave.o $(KRB5_BASE_DEPLIBS)
167778
+	$(CC_LINK) -o $@ icinterleave.o $(KRB5_BASE_LIBS)
167778
+
167778
 icred: icred.o $(KRB5_BASE_DEPLIBS)
167778
 	$(CC_LINK) -o $@ icred.o $(KRB5_BASE_LIBS)
167778
 
167778
@@ -115,8 +118,9 @@ kdb_check: kdc.conf krb5.conf
167778
 	$(RUN_DB_TEST) ../kadmin/dbutil/kdb5_util $(KADMIN_OPTS) destroy -f
167778
 	$(RM) $(TEST_DB)* stash_file
167778
 
167778
-check-pytests: adata etinfo forward gcred hist hooks hrealm icred kdbtest
167778
-check-pytests: localauth plugorder rdreq responder s2p s4u2proxy unlockiter
167778
+check-pytests: adata etinfo forward gcred hist hooks hrealm icinterleave icred
167778
+check-pytests: kdbtest localauth plugorder rdreq responder s2p s4u2proxy
167778
+check-pytests: unlockiter
167778
 	$(RUNPYTEST) $(srcdir)/t_general.py $(PYTESTFLAGS)
167778
 	$(RUNPYTEST) $(srcdir)/t_hooks.py $(PYTESTFLAGS)
167778
 	$(RUNPYTEST) $(srcdir)/t_dump.py $(PYTESTFLAGS)
167778
@@ -172,8 +176,9 @@ check-pytests: localauth plugorder rdreq responder s2p s4u2proxy unlockiter
167778
 	$(RUNPYTEST) $(srcdir)/t_kdcpolicy.py $(PYTESTFLAGS)
167778
 
167778
 clean:
167778
-	$(RM) adata etinfo forward gcred hist hooks hrealm icred kdbtest
167778
-	$(RM) localauth plugorder rdreq responder s2p s4u2proxy unlockiter
167778
+	$(RM) adata etinfo forward gcred hist hooks hrealm icinterleave icred
167778
+	$(RM) kdbtest localauth plugorder rdreq responder s2p s4u2proxy
167778
+	$(RM) unlockiter
167778
 	$(RM) krb5.conf kdc.conf
167778
 	$(RM) -rf kdc_realm/sandbox ldap
167778
 	$(RM) au.log
167778
diff --git a/src/tests/icinterleave.c b/src/tests/icinterleave.c
167778
new file mode 100644
167778
index 000000000..d76ecf361
167778
--- /dev/null
167778
+++ b/src/tests/icinterleave.c
167778
@@ -0,0 +1,124 @@
167778
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
167778
+/* tests/icinterleave.c - interleaved init_creds_step test harness */
167778
+/*
167778
+ * Copyright (C) 2017 by the Massachusetts Institute of Technology.
167778
+ * All rights reserved.
167778
+ *
167778
+ * Redistribution and use in source and binary forms, with or without
167778
+ * modification, are permitted provided that the following conditions
167778
+ * are met:
167778
+ *
167778
+ * * Redistributions of source code must retain the above copyright
167778
+ *   notice, this list of conditions and the following disclaimer.
167778
+ *
167778
+ * * Redistributions in binary form must reproduce the above copyright
167778
+ *   notice, this list of conditions and the following disclaimer in
167778
+ *   the documentation and/or other materials provided with the
167778
+ *   distribution.
167778
+ *
167778
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
167778
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
167778
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
167778
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
167778
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
167778
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
167778
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
167778
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
167778
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
167778
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
167778
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
167778
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
167778
+ */
167778
+
167778
+/*
167778
+ * This test harness performs multiple initial creds operations using
167778
+ * krb5_init_creds_step(), interleaving the operations to test the scoping of
167778
+ * the preauth state.  All principals must have the same password (or not
167778
+ * require a password).
167778
+ */
167778
+
167778
+#include "k5-int.h"
167778
+
167778
+static krb5_context ctx;
167778
+
167778
+static void
167778
+check(krb5_error_code code)
167778
+{
167778
+    const char *errmsg;
167778
+
167778
+    if (code) {
167778
+        errmsg = krb5_get_error_message(ctx, code);
167778
+        fprintf(stderr, "%s\n", errmsg);
167778
+        krb5_free_error_message(ctx, errmsg);
167778
+        exit(1);
167778
+    }
167778
+}
167778
+
167778
+int
167778
+main(int argc, char **argv)
167778
+{
167778
+    const char *password;
167778
+    char **princstrs;
167778
+    krb5_principal client;
167778
+    krb5_init_creds_context *iccs;
167778
+    krb5_data req, *reps, realm;
167778
+    krb5_boolean any_left;
167778
+    int i, nclients, master;
167778
+    unsigned int flags;
167778
+
167778
+    if (argc < 3) {
167778
+        fprintf(stderr, "Usage: icinterleave password princ1 princ2 ...\n");
167778
+        exit(1);
167778
+    }
167778
+    password = argv[1];
167778
+    princstrs = argv + 2;
167778
+    nclients = argc - 2;
167778
+
167778
+    check(krb5_init_context(&ctx));
167778
+
167778
+    /* Create an initial creds context for each client principal. */
167778
+    iccs = calloc(nclients, sizeof(*iccs));
167778
+    assert(iccs != NULL);
167778
+    for (i = 0; i < nclients; i++) {
167778
+        check(krb5_parse_name(ctx, princstrs[i], &client));
167778
+        check(krb5_init_creds_init(ctx, client, NULL, NULL, 0, NULL,
167778
+                                   &iccs[i]));
167778
+        check(krb5_init_creds_set_password(ctx, iccs[i], password));
167778
+        krb5_free_principal(ctx, client);
167778
+    }
167778
+
167778
+    reps = calloc(nclients, sizeof(*reps));
167778
+    assert(reps != NULL);
167778
+
167778
+    any_left = TRUE;
167778
+    while (any_left) {
167778
+        any_left = FALSE;
167778
+        for (i = 0; i < nclients; i++)  {
167778
+            if (iccs[i] == NULL)
167778
+                continue;
167778
+            any_left = TRUE;
167778
+
167778
+            printf("step %d\n", i + 1);
167778
+
167778
+            req = empty_data();
167778
+            realm = empty_data();
167778
+            check(krb5_init_creds_step(ctx, iccs[i], &reps[i], &req, &realm,
167778
+                                       &flags));
167778
+            if (!(flags & KRB5_INIT_CREDS_STEP_FLAG_CONTINUE)) {
167778
+                printf("finish %d\n", i + 1);
167778
+                krb5_init_creds_free(ctx, iccs[i]);
167778
+                iccs[i] = NULL;
167778
+                continue;
167778
+            }
167778
+
167778
+            master = 0;
167778
+            krb5_free_data_contents(ctx, &reps[i]);
167778
+            check(krb5_sendto_kdc(ctx, &req, &realm, &reps[i], &master, 0));
167778
+            krb5_free_data_contents(ctx, &req;;
167778
+            krb5_free_data_contents(ctx, &realm;;
167778
+        }
167778
+    }
167778
+
167778
+    krb5_free_context(ctx);
167778
+    return 0;
167778
+}
167778
diff --git a/src/tests/t_preauth.py b/src/tests/t_preauth.py
167778
index 0ef8bbca4..9b6da5a96 100644
167778
--- a/src/tests/t_preauth.py
167778
+++ b/src/tests/t_preauth.py
167778
@@ -24,4 +24,17 @@ out = realm.run([kinit, realm.user_princ], input=password('user')+'\n')
167778
 if '2rt: secondtrip' not in out:
167778
     fail('multi round-trip cookie test')
167778
 
167778
+# Test that multiple stepwise initial creds operations can be
167778
+# performed with the same krb5_context, with proper tracking of
167778
+# clpreauth module request handles.
167778
+realm.run([kadminl, 'addprinc', '-pw', 'pw', 'u1'])
167778
+realm.run([kadminl, 'addprinc', '+requires_preauth', '-pw', 'pw', 'u2'])
167778
+realm.run([kadminl, 'addprinc', '+requires_preauth', '-pw', 'pw', 'u3'])
167778
+realm.run([kadminl, 'setstr', 'u2', '2rt', 'extra'])
167778
+out = realm.run(['./icinterleave', 'pw', 'u1', 'u2', 'u3'])
167778
+if out != ('step 1\nstep 2\nstep 3\nstep 1\nfinish 1\nstep 2\nno attr\n'
167778
+           'step 3\nno attr\nstep 2\n2rt: extra\nstep 3\nfinish 3\nstep 2\n'
167778
+           'finish 2\n'):
167778
+    fail('unexpected output from icinterleave')
167778
+
167778
 success('Pre-authentication framework tests')