From 7dc329563673ca549c1d49460faf4925e2ccec47 Mon Sep 17 00:00:00 2001 From: Theodore Tso Date: Sat, 9 May 1998 03:19:46 +0000 Subject: [PATCH] str_conv.c (krb5_string_to_timestamp, strptime): Fix routines to be able to properly parse Y2K dates. t_kerb.c: Add ability to test krb5_string_to_timestamp git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@10559 dc483132-0cff-0310-8789-dd5450dbe970 --- src/lib/krb5/krb/ChangeLog | 7 +++ src/lib/krb5/krb/Makefile.in | 2 +- src/lib/krb5/krb/str_conv.c | 89 +++++++++++++++++++++++++++--------- src/lib/krb5/krb/t_kerb.c | 22 +++++++++ 4 files changed, 98 insertions(+), 22 deletions(-) diff --git a/src/lib/krb5/krb/ChangeLog b/src/lib/krb5/krb/ChangeLog index 434459f9a..9cc9d761c 100644 --- a/src/lib/krb5/krb/ChangeLog +++ b/src/lib/krb5/krb/ChangeLog @@ -1,3 +1,10 @@ +1998-05-08 Theodore Ts'o + + * str_conv.c (krb5_string_to_timestamp, strptime): Fix routines to + be able to properly parse Y2K dates. + + * t_kerb.c: Add ability to test krb5_string_to_timestamp + 1998-05-06 Theodore Ts'o * t_ser.c (main): POSIX states that getopt returns -1 diff --git a/src/lib/krb5/krb/Makefile.in b/src/lib/krb5/krb/Makefile.in index a13099cf5..ec6384e07 100644 --- a/src/lib/krb5/krb/Makefile.in +++ b/src/lib/krb5/krb/Makefile.in @@ -268,7 +268,7 @@ COMERRLIB=$(TOPLIBD)/libcom_err.a T_WALK_RTREE_OBJS= t_walk_rtree.o walk_rtree.o tgtname.o unparse.o \ free_rtree.o bld_pr_ext.o -T_KERB_OBJS= t_kerb.o conv_princ.o unparse.o set_realm.o +T_KERB_OBJS= t_kerb.o conv_princ.o unparse.o set_realm.o str_conv.o T_SER_OBJS= t_ser.o ser_actx.o ser_adata.o ser_addr.o ser_auth.o ser_cksum.o \ ser_ctx.o ser_eblk.o ser_key.o ser_princ.o serialize.o diff --git a/src/lib/krb5/krb/str_conv.c b/src/lib/krb5/krb/str_conv.c index d8b2e6d94..c059f838e 100644 --- a/src/lib/krb5/krb/str_conv.c +++ b/src/lib/krb5/krb/str_conv.c @@ -133,6 +133,8 @@ static const char cstype_hmacsha_out[] = "HMAC-SHA"; /* Absolute time strings */ static const char atime_full_digits[] = "%y%m%d%H%M%S"; static const char atime_full_digits_d[] = "%y.%m.%d.%H.%M.%S"; +static const char atime_full_digits_Y[] = "%Y%m%d%H%M%S"; +static const char atime_full_digits_Yd[]= "%Y.%m.%d.%H.%M.%S"; static const char atime_nsec_digits[] = "%y%m%d%H%M"; static const char atime_rel_hms[] = "%H%M%S"; static const char atime_rel_hm[] = "%H%M"; @@ -144,6 +146,8 @@ static const char atime_full_text_nos[] = "%d-%b-%Y:%R"; #if !HAVE_STRPTIME static const char ascan_full_digits[] = "%02d%02d%02d%02d%02d%02d"; static const char ascan_full_digits_d[] = "%02d.%02d.%02d.%02d.%02d.%02d"; +static const char ascan_full_digits_Y[] = "%4d%02d%02d%02d%02d%02d"; +static const char ascan_full_digits_Yd[]= "%4d.%02d.%02d.%02d.%02d.%02d"; static const char ascan_nsec_digits[] = "%02d%02d%02d%02d%02d"; static const char ascan_rel_hms[] = "%02d%02d%02d"; static const char ascan_rel_hm[] = "%02d%02d"; @@ -223,6 +227,8 @@ static const int cksumtype_table_nents = sizeof(cksumtype_table)/ sizeof(cksumtype_table[0]); static const char * const atime_format_table[] = { +atime_full_digits_Y, /* yyyymmddhhmmss */ +atime_full_digits_Yd, /* yyyy.mm.dd.hh.mm.ss */ atime_full_digits, /* yymmddhhmmss */ atime_full_digits_d, /* yy.mm.dd.hh.mm.ss */ atime_nsec_digits, /* yymmddhhmm */ @@ -282,6 +288,8 @@ strptime(buf, format, tm) /* * We only understand the following fixed formats: + * %Y%m%d%H%M%S + * %Y.%m.%d.%H.%M.%S * %y%m%d%H%M%S * %y.%m.%d.%H.%M.%S * %y%m%d%H%M @@ -291,9 +299,37 @@ strptime(buf, format, tm) * %R */ bp = (char *) NULL; - if (!strcmp(format, atime_full_digits) && + if (!strcmp(format, atime_full_digits_Y) && + (sscanf(buf, ascan_full_digits_Y, + &year, &month, &day, &hour, &minute, &second) == 6)) { + if (year < 1900) + return NULL; + tm->tm_year = year-1900; + tm->tm_mon = month - 1; + tm->tm_mday = day; + tm->tm_hour = hour; + tm->tm_min = minute; + tm->tm_sec = second; + bp = &buf[strlen(atime_full_digits)]; + } + else if (!strcmp(format,atime_full_digits_Yd) && + (sscanf(buf, ascan_full_digits_Yd, + &year, &month, &day, &hour, &minute, &second) == 6)) { + if (year < 1900) + return NULL; + tm->tm_year = year-1900; + tm->tm_mon = month - 1; + tm->tm_mday = day; + tm->tm_hour = hour; + tm->tm_min = minute; + tm->tm_sec = second; + bp = &buf[strlen(atime_full_digits_d)]; + } + else if (!strcmp(format, atime_full_digits) && (sscanf(buf, ascan_full_digits, &year, &month, &day, &hour, &minute, &second) == 6)) { + if (year <= 68) + year += 100; tm->tm_year = year; tm->tm_mon = month - 1; tm->tm_mday = day; @@ -305,6 +341,8 @@ strptime(buf, format, tm) else if (!strcmp(format,atime_full_digits_d) && (sscanf(buf, ascan_full_digits_d, &year, &month, &day, &hour, &minute, &second) == 6)) { + if (year <= 68) + year += 100; tm->tm_year = year; tm->tm_mon = month - 1; tm->tm_mday = day; @@ -316,6 +354,8 @@ strptime(buf, format, tm) else if (!strcmp(format, atime_nsec_digits) && (sscanf(buf, ascan_nsec_digits, &year, &month, &day, &hour, &minute) == 5)) { + if (year <= 68) + year += 100; tm->tm_year = year; tm->tm_mon = month - 1; tm->tm_mday = day; @@ -357,8 +397,9 @@ strptime(buf, format, tm) tm->tm_hour = hour; tm->tm_min = minute; bp = &buf[strlen(atime_rel_col_hm)]; - } - return(bp); + } else + return NULL; + return bp; } #endif /* HAVE_STRPTIME */ @@ -430,12 +471,10 @@ krb5_string_to_timestamp(string, timestampp) krb5_timestamp FAR * timestampp; { int i; - int found; struct tm timebuf; - time_t now; + time_t now, ret_time; char *s; - found = 0; now = time((time_t *) NULL); for (i=0; i\n\0 */ - buffer[strlen(buffer)-1] = '\0'; - return(0); - } - return(ENOMEM); + char *cp; + time_t t = timestamp; + + cp = ctime(&t); + if (strlen(cp) >= buflen) + return ENOMEM; + strcpy(buffer, cp); + /* ctime returns \n\0 */ + buffer[strlen(buffer)-1] = '\0'; + return(0); #endif /* HAVE_STRFTIME */ } diff --git a/src/lib/krb5/krb/t_kerb.c b/src/lib/krb5/krb/t_kerb.c index 45c5e2e2d..2feef39dd 100644 --- a/src/lib/krb5/krb/t_kerb.c +++ b/src/lib/krb5/krb/t_kerb.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -14,6 +15,22 @@ #include "com_err.h" +void test_string_to_timestamp(ctx, time) + krb5_context ctx; + char *time; +{ + krb5_timestamp timestamp; + time_t t; + krb5_error_code retval; + + retval = krb5_string_to_timestamp(time, ×tamp); + if (retval) { + com_err("krb5_string_to_timestamp", retval, 0); + return; + } + t = (time_t) timestamp; + printf("Parsed time was %s", ctime(&t)); +} void test_425_conv_principal(ctx, name, inst, realm) krb5_context ctx; @@ -116,6 +133,7 @@ void usage(progname) progname, progname); fprintf(stderr, "\t%s parse_name \n", progname); fprintf(stderr, "\t%s set_realm \n", progname); + fprintf(stderr, "\t%s string_to_timestamp