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