Blame SOURCES/Add-timestamp-tests.patch

963210
From 47999bb8735f653f06e0eb46e7eced600210b9da Mon Sep 17 00:00:00 2001
963210
From: Greg Hudson <ghudson@mit.edu>
963210
Date: Sat, 29 Apr 2017 17:30:36 -0400
963210
Subject: [PATCH] Add timestamp tests
963210
963210
Add a test program for krb5int_validate_times() covering cases before
963210
and across the y2038 boundary.  Add a GSSAPI test program to exercise
963210
lifetime queries, and tests using it in t_gssapi.py for ticket end
963210
times after y2038.  Add a new test script t_y2038.py which only runs
963210
on platforms with 64-bit time_t to exercise end-user operations across
963210
and after y2038.  Add an LDAP test case to test storage of post-y2038
963210
timestamps.
963210
963210
ticket: 8352
963210
(cherry picked from commit 8ca62e54e89e2fbd6a089e8ab20b4e374a486003)
963210
[rharwood@redhat.com: prune gitignore]
963210
---
963210
 src/Makefile.in                  |   1 +
963210
 src/config/pre.in                |   2 +
963210
 src/configure.in                 |   3 +
167778
 src/lib/krb5/krb/Makefile.in     |  14 +++-
167778
 src/lib/krb5/krb/t_valid_times.c | 109 ++++++++++++++++++++++++
963210
 src/tests/Makefile.in            |   1 +
167778
 src/tests/gssapi/Makefile.in     |  27 +++---
167778
 src/tests/gssapi/t_gssapi.py     |  32 +++++++
167778
 src/tests/gssapi/t_lifetime.c    | 140 +++++++++++++++++++++++++++++++
963210
 src/tests/t_kdb.py               |   7 ++
167778
 src/tests/t_y2038.py             |  75 +++++++++++++++++
963210
 11 files changed, 395 insertions(+), 16 deletions(-)
963210
 create mode 100644 src/lib/krb5/krb/t_valid_times.c
963210
 create mode 100644 src/tests/gssapi/t_lifetime.c
963210
 create mode 100644 src/tests/t_y2038.py
963210
963210
diff --git a/src/Makefile.in b/src/Makefile.in
963210
index b0249778c..ad8565056 100644
963210
--- a/src/Makefile.in
963210
+++ b/src/Makefile.in
963210
@@ -521,6 +521,7 @@ pyrunenv.vals: Makefile
963210
 	done > $@
963210
 	echo "tls_impl = '$(TLS_IMPL)'" >> $@
963210
 	echo "have_sasl = '$(HAVE_SASL)'" >> $@
963210
+	echo "sizeof_time_t = $(SIZEOF_TIME_T)" >> $@
963210
 
963210
 runenv.py: pyrunenv.vals
963210
 	echo 'env = {}' > $@
963210
diff --git a/src/config/pre.in b/src/config/pre.in
963210
index d961b5621..f23c07d9d 100644
963210
--- a/src/config/pre.in
963210
+++ b/src/config/pre.in
963210
@@ -452,6 +452,8 @@ HAVE_SASL = @HAVE_SASL@
963210
 # Whether we have libresolv 1.1.5 for URI discovery tests
963210
 HAVE_RESOLV_WRAPPER = @HAVE_RESOLV_WRAPPER@
963210
 
963210
+SIZEOF_TIME_T = @SIZEOF_TIME_T@
963210
+
963210
 # error table rules
963210
 #
963210
 ### /* these are invoked as $(...) foo.et, which works, but could be better */
963210
diff --git a/src/configure.in b/src/configure.in
963210
index 24f653f0d..4ae2c07d5 100644
963210
--- a/src/configure.in
963210
+++ b/src/configure.in
963210
@@ -744,6 +744,9 @@ fi
963210
 
963210
 AC_HEADER_TIME
963210
 AC_CHECK_TYPE(time_t, long)
963210
+AC_CHECK_SIZEOF(time_t)
963210
+SIZEOF_TIME_T=$ac_cv_sizeof_time_t
963210
+AC_SUBST(SIZEOF_TIME_T)
963210
 
963210
 # Determine where to put the replay cache.
963210
 
963210
diff --git a/src/lib/krb5/krb/Makefile.in b/src/lib/krb5/krb/Makefile.in
963210
index 0fe02a95d..55f82b147 100644
963210
--- a/src/lib/krb5/krb/Makefile.in
963210
+++ b/src/lib/krb5/krb/Makefile.in
963210
@@ -364,6 +364,7 @@ SRCS=	$(srcdir)/addr_comp.c	\
963210
 	$(srcdir)/t_in_ccache.c	\
963210
 	$(srcdir)/t_response_items.c \
963210
 	$(srcdir)/t_sname_match.c \
963210
+	$(srcdir)/t_valid_times.c \
963210
 	$(srcdir)/t_vfy_increds.c
963210
 
963210
 # Someday, when we have a "maintainer mode", do this right:
963210
@@ -457,9 +458,12 @@ t_response_items: t_response_items.o response_items.o $(KRB5_BASE_DEPLIBS)
963210
 t_sname_match: t_sname_match.o sname_match.o $(KRB5_BASE_DEPLIBS)
963210
 	$(CC_LINK) -o $@ t_sname_match.o sname_match.o $(KRB5_BASE_LIBS)
963210
 
963210
+t_valid_times: t_valid_times.o valid_times.o $(KRB5_BASE_DEPLIBS)
963210
+	$(CC_LINK) -o $@ t_valid_times.o valid_times.o $(KRB5_BASE_LIBS)
963210
+
963210
 TEST_PROGS= t_walk_rtree t_kerb t_ser t_deltat t_expand t_authdata t_pac \
963210
-	t_in_ccache t_cc_config t_copy_context \
963210
-	t_princ t_etypes t_vfy_increds t_response_items t_sname_match
963210
+	t_in_ccache t_cc_config t_copy_context t_princ t_etypes t_vfy_increds \
963210
+	t_response_items t_sname_match t_valid_times
963210
 
963210
 check-unix: $(TEST_PROGS)
963210
 	$(RUN_TEST_LOCAL_CONF) ./t_kerb \
963210
@@ -496,6 +500,7 @@ check-unix: $(TEST_PROGS)
963210
 	$(RUN_TEST) ./t_response_items
963210
 	$(RUN_TEST) ./t_copy_context
963210
 	$(RUN_TEST) ./t_sname_match
963210
+	$(RUN_TEST) ./t_valid_times
963210
 
963210
 check-pytests: t_expire_warn t_vfy_increds
963210
 	$(RUNPYTEST) $(srcdir)/t_expire_warn.py $(PYTESTFLAGS)
963210
@@ -522,8 +527,9 @@ clean:
963210
 	$(OUTPRE)t_ad_fx_armor$(EXEEXT) $(OUTPRE)t_ad_fx_armor.$(OBJEXT) \
963210
 	$(OUTPRE)t_vfy_increds$(EXEEXT) $(OUTPRE)t_vfy_increds.$(OBJEXT) \
963210
 	$(OUTPRE)t_response_items$(EXEEXT) \
963210
-	$(OUTPRE)t_response_items.$(OBJEXT) $(OUTPRE)t_sname_match$(EXEEXT) \
963210
-	$(OUTPRE)t_sname_match.$(OBJEXT) \
963210
+	$(OUTPRE)t_response_items.$(OBJEXT) \
963210
+	$(OUTPRE)t_sname_match$(EXEEXT) $(OUTPRE)t_sname_match.$(OBJEXT) \
963210
+	$(OUTPRE)t_valid_times$(EXEEXT) $(OUTPRE)t_valid_times.$(OBJECT) \
963210
 	$(OUTPRE)t_parse_host_string$(EXEEXT) \
963210
 	$(OUTPRE)t_parse_host_string.$(OBJEXT)
963210
 
963210
diff --git a/src/lib/krb5/krb/t_valid_times.c b/src/lib/krb5/krb/t_valid_times.c
963210
new file mode 100644
963210
index 000000000..1b469ffc2
963210
--- /dev/null
963210
+++ b/src/lib/krb5/krb/t_valid_times.c
963210
@@ -0,0 +1,109 @@
963210
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
963210
+/* lib/krb5/krb/t_valid_times.c - test program for krb5int_validate_times() */
963210
+/*
963210
+ * Copyright (C) 2017 by the Massachusetts Institute of Technology.
963210
+ * All rights reserved.
963210
+ *
963210
+ * Redistribution and use in source and binary forms, with or without
963210
+ * modification, are permitted provided that the following conditions
963210
+ * are met:
963210
+ *
963210
+ * * Redistributions of source code must retain the above copyright
963210
+ *   notice, this list of conditions and the following disclaimer.
963210
+ *
963210
+ * * Redistributions in binary form must reproduce the above copyright
963210
+ *   notice, this list of conditions and the following disclaimer in
963210
+ *   the documentation and/or other materials provided with the
963210
+ *   distribution.
963210
+ *
963210
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
963210
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
963210
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
963210
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
963210
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
963210
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
963210
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
963210
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
963210
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
963210
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
963210
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
963210
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
963210
+ */
963210
+
963210
+#include "k5-int.h"
963210
+#include "int-proto.h"
963210
+
963210
+#define BOUNDARY (uint32_t)INT32_MIN
963210
+
963210
+int
963210
+main()
963210
+{
963210
+    krb5_error_code ret;
963210
+    krb5_context context;
963210
+    krb5_ticket_times times = { 0, 0, 0, 0 };
963210
+
963210
+    ret = krb5_init_context(&context);
963210
+    assert(!ret);
963210
+
963210
+    /* Current time is within authtime and end time. */
963210
+    ret = krb5_set_debugging_time(context, 1000, 0);
963210
+    times.authtime = 500;
963210
+    times.endtime = 1500;
963210
+    ret = krb5int_validate_times(context, ×);
963210
+    assert(!ret);
963210
+
963210
+    /* Current time is before starttime, but within clock skew. */
963210
+    times.starttime = 1100;
963210
+    ret = krb5int_validate_times(context, ×);
963210
+    assert(!ret);
963210
+
963210
+    /* Current time is before starttime by more than clock skew. */
963210
+    times.starttime = 1400;
963210
+    ret = krb5int_validate_times(context, ×);
963210
+    assert(ret == KRB5KRB_AP_ERR_TKT_NYV);
963210
+
963210
+    /* Current time is after end time, but within clock skew. */
963210
+    times.starttime = 500;
963210
+    times.endtime = 800;
963210
+    ret = krb5int_validate_times(context, ×);
963210
+    assert(!ret);
963210
+
963210
+    /* Current time is after end time by more than clock skew. */
963210
+    times.endtime = 600;
963210
+    ret = krb5int_validate_times(context, ×);
963210
+    assert(ret == KRB5KRB_AP_ERR_TKT_EXPIRED);
963210
+
963210
+    /* Current time is within starttime and endtime; current time and
963210
+     * endtime are across y2038 boundary. */
963210
+    ret = krb5_set_debugging_time(context, BOUNDARY - 100, 0);
963210
+    assert(!ret);
963210
+    times.starttime = BOUNDARY - 200;
963210
+    times.endtime = BOUNDARY + 500;
963210
+    ret = krb5int_validate_times(context, ×);
963210
+    assert(!ret);
963210
+
963210
+    /* Current time is before starttime, but by less than clock skew. */
963210
+    times.starttime = BOUNDARY + 100;
963210
+    ret = krb5int_validate_times(context, ×);
963210
+    assert(!ret);
963210
+
963210
+    /* Current time is before starttime by more than clock skew. */
963210
+    times.starttime = BOUNDARY + 250;
963210
+    ret = krb5int_validate_times(context, ×);
963210
+    assert(ret == KRB5KRB_AP_ERR_TKT_NYV);
963210
+
963210
+    /* Current time is after endtime, but by less than clock skew. */
963210
+    ret = krb5_set_debugging_time(context, BOUNDARY + 100, 0);
963210
+    assert(!ret);
963210
+    times.starttime = BOUNDARY - 1000;
963210
+    times.endtime = BOUNDARY - 100;
963210
+    ret = krb5int_validate_times(context, ×);
963210
+    assert(!ret);
963210
+
963210
+    /* Current time is after endtime by more than clock skew. */
963210
+    times.endtime = BOUNDARY - 300;
963210
+    ret = krb5int_validate_times(context, ×);
963210
+    assert(ret == KRB5KRB_AP_ERR_TKT_EXPIRED);
963210
+
963210
+    return 0;
963210
+}
963210
diff --git a/src/tests/Makefile.in b/src/tests/Makefile.in
963210
index 0e93d6b59..2b3112537 100644
963210
--- a/src/tests/Makefile.in
963210
+++ b/src/tests/Makefile.in
963210
@@ -168,6 +168,7 @@ check-pytests: localauth plugorder rdreq responder s2p s4u2proxy unlockiter
963210
 	$(RUNPYTEST) $(srcdir)/t_princflags.py $(PYTESTFLAGS)
963210
 	$(RUNPYTEST) $(srcdir)/t_tabdump.py $(PYTESTFLAGS)
963210
 	$(RUNPYTEST) $(srcdir)/t_certauth.py $(PYTESTFLAGS)
963210
+	$(RUNPYTEST) $(srcdir)/t_y2038.py $(PYTESTFLAGS)
963210
 
963210
 clean:
963210
 	$(RM) adata etinfo forward gcred hist hooks hrealm icred kdbtest
963210
diff --git a/src/tests/gssapi/Makefile.in b/src/tests/gssapi/Makefile.in
963210
index 6c1464297..604f926de 100644
963210
--- a/src/tests/gssapi/Makefile.in
963210
+++ b/src/tests/gssapi/Makefile.in
963210
@@ -15,15 +15,16 @@ SRCS=	$(srcdir)/ccinit.c $(srcdir)/ccrefresh.c $(srcdir)/common.c \
963210
 	$(srcdir)/t_gssexts.c $(srcdir)/t_imp_cred.c $(srcdir)/t_imp_name.c \
963210
 	$(srcdir)/t_invalid.c $(srcdir)/t_inq_cred.c $(srcdir)/t_inq_ctx.c \
963210
 	$(srcdir)/t_inq_mechs_name.c $(srcdir)/t_iov.c \
963210
-	$(srcdir)/t_namingexts.c $(srcdir)/t_oid.c $(srcdir)/t_pcontok.c \
963210
-	$(srcdir)/t_prf.c $(srcdir)/t_s4u.c $(srcdir)/t_s4u2proxy_krb5.c \
963210
-	$(srcdir)/t_saslname.c $(srcdir)/t_spnego.c $(srcdir)/t_srcattrs.c
963210
+	$(srcdir)/t_lifetime.c $(srcdir)/t_namingexts.c $(srcdir)/t_oid.c \
963210
+	$(srcdir)/t_pcontok.c $(srcdir)/t_prf.c $(srcdir)/t_s4u.c \
963210
+	$(srcdir)/t_s4u2proxy_krb5.c $(srcdir)/t_saslname.c \
963210
+	$(srcdir)/t_spnego.c $(srcdir)/t_srcattrs.c
963210
 
963210
 OBJS=	ccinit.o ccrefresh.o common.o t_accname.o t_ccselect.o t_ciflags.o \
963210
 	t_credstore.o t_enctypes.o t_err.o t_export_cred.o t_export_name.o \
963210
 	t_gssexts.o t_imp_cred.o t_imp_name.o t_invalid.o t_inq_cred.o \
963210
-	t_inq_ctx.o t_inq_mechs_name.o t_iov.o t_namingexts.o t_oid.o \
963210
-	t_pcontok.o t_prf.o t_s4u.o t_s4u2proxy_krb5.o t_saslname.o \
963210
+	t_inq_ctx.o t_inq_mechs_name.o t_iov.o t_lifetime.o t_namingexts.o \
963210
+	t_oid.o t_pcontok.o t_prf.o t_s4u.o t_s4u2proxy_krb5.o t_saslname.o \
963210
 	t_spnego.o t_srcattrs.o
963210
 
963210
 COMMON_DEPS= common.o $(GSS_DEPLIBS) $(KRB5_BASE_DEPLIBS)
963210
@@ -31,9 +32,9 @@ COMMON_LIBS= common.o $(GSS_LIBS) $(KRB5_BASE_LIBS)
963210
 
963210
 all: ccinit ccrefresh t_accname t_ccselect t_ciflags t_credstore t_enctypes \
963210
 	t_err t_export_cred t_export_name t_gssexts t_imp_cred t_imp_name \
963210
-	t_invalid t_inq_cred t_inq_ctx t_inq_mechs_name t_iov t_namingexts \
963210
-	t_oid t_pcontok t_prf t_s4u t_s4u2proxy_krb5 t_saslname t_spnego \
963210
-	t_srcattrs
963210
+	t_invalid t_inq_cred t_inq_ctx t_inq_mechs_name t_iov t_lifetime \
963210
+	t_namingexts t_oid t_pcontok t_prf t_s4u t_s4u2proxy_krb5 t_saslname \
963210
+	t_spnego t_srcattrs
963210
 
963210
 check-unix: t_oid
963210
 	$(RUN_TEST) ./t_invalid
963210
@@ -42,8 +43,8 @@ check-unix: t_oid
963210
 
963210
 check-pytests: ccinit ccrefresh t_accname t_ccselect t_ciflags t_credstore \
963210
 	t_enctypes t_err t_export_cred t_export_name t_imp_cred t_inq_cred \
963210
-	t_inq_ctx t_inq_mechs_name t_iov t_pcontok t_s4u t_s4u2proxy_krb5 \
963210
-	t_spnego t_srcattrs
963210
+	t_inq_ctx t_inq_mechs_name t_iov t_lifetime t_pcontok t_s4u \
963210
+	t_s4u2proxy_krb5 t_spnego t_srcattrs
963210
 	$(RUNPYTEST) $(srcdir)/t_gssapi.py $(PYTESTFLAGS)
963210
 	$(RUNPYTEST) $(srcdir)/t_ccselect.py $(PYTESTFLAGS)
963210
 	$(RUNPYTEST) $(srcdir)/t_client_keytab.py $(PYTESTFLAGS)
963210
@@ -88,6 +89,8 @@ t_inq_mechs_name: t_inq_mechs_name.o $(COMMON_DEPS)
963210
 	$(CC_LINK) -o $@ t_inq_mechs_name.o $(COMMON_LIBS)
963210
 t_iov: t_iov.o $(COMMON_DEPS)
963210
 	$(CC_LINK) -o $@ t_iov.o $(COMMON_LIBS)
963210
+t_lifetime: t_lifetime.o $(COMMON_DEPS)
963210
+	$(CC_LINK) -o $@ t_lifetime.o $(COMMON_LIBS)
963210
 t_namingexts: t_namingexts.o $(COMMON_DEPS)
963210
 	$(CC_LINK) -o $@ t_namingexts.o $(COMMON_LIBS)
963210
 t_pcontok: t_pcontok.o $(COMMON_DEPS)
963210
@@ -111,5 +114,5 @@ clean:
963210
 	$(RM) ccinit ccrefresh t_accname t_ccselect t_ciflags t_credstore
963210
 	$(RM) t_enctypes t_err t_export_cred t_export_name t_gssexts t_imp_cred
963210
 	$(RM) t_imp_name t_invalid t_inq_cred t_inq_ctx t_inq_mechs_name t_iov
963210
-	$(RM) t_namingexts t_oid t_pcontok t_prf t_s4u t_s4u2proxy_krb5
963210
-	$(RM) t_saslname t_spnego t_srcattrs
963210
+	$(RM) t_lifetime t_namingexts t_oid t_pcontok t_prf t_s4u
963210
+	$(RM) t_s4u2proxy_krb5 t_saslname t_spnego t_srcattrs
963210
diff --git a/src/tests/gssapi/t_gssapi.py b/src/tests/gssapi/t_gssapi.py
963210
index e23c936d7..fa214242f 100755
963210
--- a/src/tests/gssapi/t_gssapi.py
963210
+++ b/src/tests/gssapi/t_gssapi.py
963210
@@ -220,4 +220,36 @@ realm.run(['./t_ciflags', 'p:' + realm.host_princ])
963210
 # contexts.
963210
 realm.run(['./t_inq_ctx', 'user', password('user'), 'p:%s' % realm.host_princ])
963210
 
963210
+# Test lifetime results, using a realm with a large maximum lifetime
963210
+# so that we can test ticket end dates after y2038.  There are no
963210
+# time_t conversions involved, so we can run these tests on platforms
963210
+# with 32-bit time_t.
963210
+realm.stop()
963210
+conf = {'realms': {'$realm': {'max_life': '9000d'}}}
963210
+realm = K5Realm(kdc_conf=conf, get_creds=False)
963210
+
963210
+# Check a lifetime string result against an expected number value (or None).
963210
+# Allow some variance due to time elapsed during the tests.
963210
+def check_lifetime(msg, val, expected):
963210
+    if expected is None and val != 'indefinite':
963210
+        fail('%s: expected indefinite, got %s' % (msg, val))
963210
+    if expected is not None and val == 'indefinite':
963210
+        fail('%s: expected %d, got indefinite' % (msg, expected))
963210
+    if expected is not None and abs(int(val) - expected) > 100:
963210
+        fail('%s: expected %d, got %s' % (msg, expected, val))
963210
+
963210
+realm.kinit(realm.user_princ, password('user'), flags=['-l', '8500d'])
963210
+out = realm.run(['./t_lifetime', 'p:' + realm.host_princ, str(8000 * 86400)])
963210
+ln = out.split('\n')
963210
+check_lifetime('icred gss_acquire_cred', ln[0], 8500 * 86400)
963210
+check_lifetime('icred gss_inquire_cred', ln[1], 8500 * 86400)
963210
+check_lifetime('acred gss_acquire_cred', ln[2], None)
963210
+check_lifetime('acred gss_inquire_cred', ln[3], None)
963210
+check_lifetime('ictx gss_init_sec_context', ln[4], 8000 * 86400)
963210
+check_lifetime('ictx gss_inquire_context', ln[5], 8000 * 86400)
963210
+check_lifetime('ictx gss_context_time', ln[6], 8000 * 86400)
963210
+check_lifetime('actx gss_accept_sec_context', ln[7], 8000 * 86400 + 300)
963210
+check_lifetime('actx gss_inquire_context', ln[8], 8000 * 86400 + 300)
963210
+check_lifetime('actx gss_context_time', ln[9], 8000 * 86400 + 300)
963210
+
963210
 success('GSSAPI tests')
963210
diff --git a/src/tests/gssapi/t_lifetime.c b/src/tests/gssapi/t_lifetime.c
963210
new file mode 100644
963210
index 000000000..8dcf18621
963210
--- /dev/null
963210
+++ b/src/tests/gssapi/t_lifetime.c
963210
@@ -0,0 +1,140 @@
963210
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
963210
+/* tests/gssapi/t_lifetime.c - display cred and context lifetimes */
963210
+/*
963210
+ * Copyright (C) 2017 by the Massachusetts Institute of Technology.
963210
+ * All rights reserved.
963210
+ *
963210
+ * Redistribution and use in source and binary forms, with or without
963210
+ * modification, are permitted provided that the following conditions
963210
+ * are met:
963210
+ *
963210
+ * * Redistributions of source code must retain the above copyright
963210
+ *   notice, this list of conditions and the following disclaimer.
963210
+ *
963210
+ * * Redistributions in binary form must reproduce the above copyright
963210
+ *   notice, this list of conditions and the following disclaimer in
963210
+ *   the documentation and/or other materials provided with the
963210
+ *   distribution.
963210
+ *
963210
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
963210
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
963210
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
963210
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
963210
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
963210
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
963210
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
963210
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
963210
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
963210
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
963210
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
963210
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
963210
+ */
963210
+
963210
+#include <stdio.h>
963210
+#include <stdlib.h>
963210
+#include <assert.h>
963210
+#include "common.h"
963210
+
963210
+/*
963210
+ * Using the default credential, exercise the GSS functions which accept or
963210
+ * produce lifetimes.  Display the following results, one per line, as ASCII
963210
+ * integers or the string "indefinite":
963210
+ *
963210
+ *   initiator cred lifetime according to gss_acquire_cred()
963210
+ *   initiator cred lifetime according to gss_inquire_cred()
963210
+ *   acceptor cred lifetime according to gss_acquire_cred()
963210
+ *   acceptor cred lifetime according to gss_inquire_cred()
963210
+ *   initiator context lifetime according to gss_init_sec_context()
963210
+ *   initiator context lifetime according to gss_inquire_context()
963210
+ *   initiator context lifetime according to gss_context_time()
963210
+ *   acceptor context lifetime according to gss_init_sec_context()
963210
+ *   acceptor context lifetime according to gss_inquire_context()
963210
+ *   acceptor context lifetime according to gss_context_time()
963210
+ */
963210
+
963210
+static void
963210
+display_time(OM_uint32 tval)
963210
+{
963210
+    if (tval == GSS_C_INDEFINITE)
963210
+        puts("indefinite");
963210
+    else
963210
+        printf("%u\n", (unsigned int)tval);
963210
+}
963210
+
963210
+int
963210
+main(int argc, char *argv[])
963210
+{
963210
+    OM_uint32 minor, major;
963210
+    gss_cred_id_t icred, acred;
963210
+    gss_name_t tname;
963210
+    gss_ctx_id_t ictx = GSS_C_NO_CONTEXT, actx = GSS_C_NO_CONTEXT;
963210
+    gss_buffer_desc itok = GSS_C_EMPTY_BUFFER, atok = GSS_C_EMPTY_BUFFER;
963210
+    OM_uint32 time_req = GSS_C_INDEFINITE, time_rec;
963210
+
963210
+    if (argc < 2 || argc > 3) {
963210
+        fprintf(stderr, "Usage: %s targetname [time_req]\n", argv[0]);
963210
+        return 1;
963210
+    }
963210
+    tname = import_name(argv[1]);
963210
+    if (argc >= 3)
963210
+        time_req = atoll(argv[2]);
963210
+
963210
+    /* Get initiator cred and display its lifetime according to
963210
+     * gss_acquire_cred and gss_inquire_cred. */
963210
+    major = gss_acquire_cred(&minor, GSS_C_NO_NAME, time_req, &mechset_krb5,
963210
+                             GSS_C_INITIATE, &icred, NULL, &time_rec);
963210
+    check_gsserr("gss_acquire_cred(initiate)", major, minor);
963210
+    display_time(time_rec);
963210
+    major = gss_inquire_cred(&minor, icred, NULL, &time_rec, NULL, NULL);
963210
+    check_gsserr("gss_inquire_cred(initiate)", major, minor);
963210
+    display_time(time_rec);
963210
+
963210
+    /* Get acceptor cred and display its lifetime according to gss_acquire_cred
963210
+     * and gss_inquire_cred. */
963210
+    major = gss_acquire_cred(&minor, GSS_C_NO_NAME, time_req, &mechset_krb5,
963210
+                             GSS_C_ACCEPT, &acred, NULL, &time_rec);
963210
+    check_gsserr("gss_acquire_cred(accept)", major, minor);
963210
+    display_time(time_rec);
963210
+    major = gss_inquire_cred(&minor, acred, NULL, &time_rec, NULL, NULL);
963210
+    check_gsserr("gss_inquire_cred(accept)", major, minor);
963210
+    display_time(time_rec);
963210
+
963210
+    /* Make an initiator context and display its lifetime according to
963210
+     * gss_init_sec_context, gss_inquire_context, and gss_context_time. */
963210
+    major = gss_init_sec_context(&minor, icred, &ictx, tname, &mech_krb5, 0,
963210
+                                 time_req, GSS_C_NO_CHANNEL_BINDINGS, &atok,
963210
+                                 NULL, &itok, NULL, &time_rec);
963210
+    check_gsserr("gss_init_sec_context", major, minor);
963210
+    assert(major == GSS_S_COMPLETE);
963210
+    display_time(time_rec);
963210
+    major = gss_inquire_context(&minor, ictx, NULL, NULL, &time_rec, NULL,
963210
+                                NULL, NULL, NULL);
963210
+    check_gsserr("gss_inquire_context(initiate)", major, minor);
963210
+    display_time(time_rec);
963210
+    major = gss_context_time(&minor, ictx, &time_rec);
963210
+    check_gsserr("gss_context_time(initiate)", major, minor);
963210
+    display_time(time_rec);
963210
+
963210
+    major = gss_accept_sec_context(&minor, &actx, acred, &itok,
963210
+                                   GSS_C_NO_CHANNEL_BINDINGS, NULL,
963210
+                                   NULL, &atok, NULL, &time_rec, NULL);
963210
+    check_gsserr("gss_accept_sec_context", major, minor);
963210
+    assert(major == GSS_S_COMPLETE);
963210
+    display_time(time_rec);
963210
+    major = gss_inquire_context(&minor, actx, NULL, NULL, &time_rec, NULL,
963210
+                                NULL, NULL, NULL);
963210
+    check_gsserr("gss_inquire_context(accept)", major, minor);
963210
+    display_time(time_rec);
963210
+    major = gss_context_time(&minor, actx, &time_rec);
963210
+    check_gsserr("gss_context_time(accept)", major, minor);
963210
+    display_time(time_rec);
963210
+
963210
+    (void)gss_release_buffer(&minor, &itok);
963210
+    (void)gss_release_buffer(&minor, &atok);
963210
+    (void)gss_release_name(&minor, &tname);
963210
+    (void)gss_release_cred(&minor, &icred);
963210
+    (void)gss_release_cred(&minor, &acred);
963210
+    (void)gss_delete_sec_context(&minor, &ictx, NULL);
963210
+    (void)gss_delete_sec_context(&minor, &actx, NULL);
963210
+    return 0;
963210
+}
963210
diff --git a/src/tests/t_kdb.py b/src/tests/t_kdb.py
963210
index 185225afa..c0eeb0118 100755
963210
--- a/src/tests/t_kdb.py
963210
+++ b/src/tests/t_kdb.py
963210
@@ -446,6 +446,13 @@ realm.run([kadminl, 'addprinc', '-policy', 'keepoldpasspol', '-pw', 'aaaa',
963210
 for p in ('bbbb', 'cccc', 'aaaa'):
963210
     realm.run([kadminl, 'cpw', '-keepold', '-pw', p, 'keepoldpassprinc'])
963210
 
963210
+if runenv.sizeof_time_t <= 4:
963210
+    skipped('y2038 LDAP test', 'platform has 32-bit time_t')
963210
+else:
963210
+    # Test storage of timestamps after y2038.
963210
+    realm.run([kadminl, 'modprinc', '-pwexpire', '2040-02-03', 'user'])
963210
+    realm.run([kadminl, 'getprinc', 'user'], expected_msg=' 2040\n')
963210
+
963210
 realm.stop()
963210
 
963210
 # Briefly test dump and load.
963210
diff --git a/src/tests/t_y2038.py b/src/tests/t_y2038.py
963210
new file mode 100644
963210
index 000000000..02e946df4
963210
--- /dev/null
963210
+++ b/src/tests/t_y2038.py
963210
@@ -0,0 +1,75 @@
963210
+#!/usr/bin/python
963210
+from k5test import *
963210
+
963210
+# These tests will become much less important after the y2038 boundary
963210
+# has elapsed, and may start exhibiting problems around the year 2075.
963210
+
963210
+if runenv.sizeof_time_t <= 4:
963210
+    skip_rest('y2038 timestamp tests', 'platform has 32-bit time_t')
963210
+
963210
+# Start a KDC running roughly 21 years in the future, after the y2038
963210
+# boundary.  Set long maximum lifetimes for later tests.
963210
+conf = {'realms': {'$realm': {'max_life': '9000d',
963210
+                              'max_renewable_life': '9000d'}}}
963210
+realm = K5Realm(start_kdc=False, kdc_conf=conf)
963210
+realm.start_kdc(['-T', '662256000'])
963210
+
963210
+# kinit without preauth should succeed with clock skew correction, but
963210
+# will result in an expired ticket, because we sent an absolute end
963210
+# time and didn't get a chance to correct it..
963210
+realm.kinit(realm.user_princ, password('user'))
963210
+realm.run([kvno, realm.host_princ], expected_code=1,
963210
+          expected_msg='Ticket expired')
963210
+
963210
+# kinit with preauth should succeed and result in a valid ticket, as
963210
+# we get a chance to correct the end time based on the KDC time.  Try
963210
+# with encrypted timestamp and encrypted challenge.
963210
+realm.run([kadminl, 'modprinc', '+requires_preauth', 'user'])
963210
+realm.kinit(realm.user_princ, password('user'))
963210
+realm.run([kvno, realm.host_princ])
963210
+realm.kinit(realm.user_princ, password('user'), flags=['-T', realm.ccache])
963210
+realm.run([kvno, realm.host_princ])
963210
+
963210
+# Test that expiration warning works after y2038, by setting a
963210
+# password expiration time ten minutes after the KDC time.
963210
+realm.run([kadminl, 'modprinc', '-pwexpire', '662256600 seconds', 'user'])
963210
+out = realm.kinit(realm.user_princ, password('user'))
963210
+if 'will expire in less than one hour' not in out:
963210
+    fail('password expiration message')
963210
+year = int(out.split()[-1])
963210
+if year < 2038 or year > 9999:
963210
+    fail('password expiration year')
963210
+
963210
+realm.stop_kdc()
963210
+realm.start_kdc()
963210
+realm.start_kadmind()
963210
+realm.prep_kadmin()
963210
+
963210
+# Test getdate parsing of absolute timestamps after 2038 and
963210
+# marshalling over the kadmin protocol.  The local time zone will
963210
+# affect the display time by a little bit, so just look for the year.
963210
+realm.run_kadmin(['modprinc', '-pwexpire', '2040-02-03', realm.host_princ])
963210
+realm.run_kadmin(['getprinc', realm.host_princ], expected_msg=' 2040\n')
963210
+
963210
+# Get a ticket whose lifetime crosses the y2038 boundary and
963210
+# range-check the expiration year as reported by klist.
963210
+realm.kinit(realm.user_princ, password('user'),
963210
+            flags=['-l', '8000d', '-r', '8500d'])
963210
+realm.run([kvno, realm.host_princ])
963210
+out = realm.run([klist])
963210
+if int(out.split('\n')[4].split()[2].split('/')[2]) < 39:
963210
+    fail('unexpected tgt expiration year')
963210
+if int(out.split('\n')[5].split()[2].split('/')[2]) < 40:
963210
+    fail('unexpected tgt rtill year')
963210
+if int(out.split('\n')[6].split()[2].split('/')[2]) < 39:
963210
+    fail('unexpected service ticket expiration year')
963210
+if int(out.split('\n')[7].split()[2].split('/')[2]) < 40:
963210
+    fail('unexpected service ticket rtill year')
963210
+realm.kinit(realm.user_princ, None, ['-R'])
963210
+out = realm.run([klist])
963210
+if int(out.split('\n')[4].split()[2].split('/')[2]) < 39:
963210
+    fail('unexpected renewed tgt expiration year')
963210
+if int(out.split('\n')[5].split()[2].split('/')[2]) < 40:
963210
+    fail('unexpected renewed tgt rtill year')
963210
+
963210
+success('y2038 tests')