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