1/*
2 * Copyright 1995, 1999, 2007 by the Massachusetts Institute of Technology.
3 * All Rights Reserved.
4 *
5 * Export of this software from the United States of America may
6 *   require a specific license from the United States Government.
7 *   It is the responsibility of any person or organization contemplating
8 *   export to obtain such a license before exporting.
9 *
10 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
11 * distribute this software and its documentation for any purpose and
12 * without fee is hereby granted, provided that the above copyright
13 * notice appear in all copies and that both that copyright notice and
14 * this permission notice appear in supporting documentation, and that
15 * the name of M.I.T. not be used in advertising or publicity pertaining
16 * to distribution of the software without specific, written prior
17 * permission.  Furthermore if you modify this software you must label
18 * your software as modified software and not distribute it in such a
19 * fashion that it might be confused with the original M.I.T. software.
20 * M.I.T. makes no representations about the suitability of
21 * this software for any purpose.  It is provided "as is" without express
22 * or implied warranty.
23 *
24 */
25
26#include "heim.h"
27#include <string.h>
28#include <errno.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <ctype.h>
32
33mit_krb5_error_code KRB5_CALLCONV
34krb5_string_to_timestamp(char *string, mit_krb5_timestamp *timestampp)
35{
36    int i;
37    struct tm timebuf, timebuf2;
38    time_t now, ret_time;
39    char *s;
40    static const char * const atime_format_table[] = {
41	"%Y%m%d%H%M%S",		/* yyyymmddhhmmss		*/
42	"%Y.%m.%d.%H.%M.%S",	/* yyyy.mm.dd.hh.mm.ss		*/
43	"%y%m%d%H%M%S",		/* yymmddhhmmss			*/
44	"%y.%m.%d.%H.%M.%S",	/* yy.mm.dd.hh.mm.ss		*/
45	"%y%m%d%H%M",		/* yymmddhhmm			*/
46	"%H%M%S",		/* hhmmss			*/
47	"%H%M",			/* hhmm				*/
48	"%T",			/* hh:mm:ss			*/
49	"%R",			/* hh:mm			*/
50	/* The following not really supported unless native strptime present */
51	"%x:%X",		/* locale-dependent short format */
52	"%d-%b-%Y:%T",		/* dd-month-yyyy:hh:mm:ss	*/
53	"%d-%b-%Y:%R"		/* dd-month-yyyy:hh:mm		*/
54    };
55    static const int atime_format_table_nents =
56	sizeof(atime_format_table)/sizeof(atime_format_table[0]);
57
58
59    now = time((time_t *) NULL);
60    if (localtime_r(&now, &timebuf2) == NULL)
61	return EINVAL;
62    for (i=0; i<atime_format_table_nents; i++) {
63        /* We reset every time throughout the loop as the manual page
64	 * indicated that no guarantees are made as to preserving timebuf
65	 * when parsing fails
66	 */
67	timebuf = timebuf2;
68	if ((s = strptime(string, atime_format_table[i], &timebuf))
69	    && (s != string)) {
70 	    /* See if at end of buffer - otherwise partial processing */
71	    while(*s != 0 && isspace((unsigned char) *s)) s++;
72	    if (*s != 0)
73	        continue;
74	    if (timebuf.tm_year <= 0)
75		continue;	/* clearly confused */
76	    ret_time = mktime(&timebuf);
77	    if (ret_time == (time_t) -1)
78		continue;	/* clearly confused */
79	    *timestampp = (krb5_timestamp) ret_time;
80	    return 0;
81	}
82    }
83    return(EINVAL);
84}
85