Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/plugins/sudoers/gentime.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2017, 2021 Todd C. Miller <[email protected]>
5
*
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
9
*
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
19
#include <config.h>
20
21
#include <stdio.h>
22
#include <stdlib.h>
23
#ifdef HAVE_STDBOOL_H
24
# include <stdbool.h>
25
#else
26
# include <compat/stdbool.h>
27
#endif /* HAVE_STDBOOL_H */
28
#include <string.h>
29
#include <ctype.h>
30
#include <time.h>
31
32
#include <sudo_compat.h>
33
#include <sudoers_debug.h>
34
#include <parse.h>
35
36
/* Since timegm() is only used in one place we keep the macro local. */
37
#ifndef HAVE_TIMEGM
38
# define timegm(_t) sudo_timegm(_t)
39
#endif
40
41
/*
42
* Parse a timestamp in Generalized Time format as per RFC4517.
43
* E.g. yyyymmddHHMMSS.FZ or yyyymmddHHMMSS.F[+-]TZOFF
44
* where minutes, seconds and fraction are optional.
45
* Returns the time in Unix time format or -1 on error.
46
*/
47
time_t
48
parse_gentime(const char *timestr)
49
{
50
char tcopy[sizeof("yyyymmddHHMMSS")];
51
const char *cp;
52
time_t result;
53
struct tm tm;
54
size_t len;
55
int items, tzoff = 0;
56
bool islocal = false;
57
debug_decl(parse_gentime, SUDOERS_DEBUG_PARSER);
58
59
/* Make a copy of the non-fractional time without zone for easy parsing. */
60
len = strspn(timestr, "0123456789");
61
if (len >= sizeof(tcopy) || len < sizeof("yyyymmddHH") -1 || (len & 1)) {
62
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
63
"unable to parse general time string %s", timestr);
64
debug_return_time_t(-1);
65
}
66
memcpy(tcopy, timestr, len);
67
tcopy[len] = '\0';
68
69
/* Parse general time, ignoring the timezone for now. */
70
memset(&tm, 0, sizeof(tm));
71
items = sscanf(tcopy, "%4d%2d%2d%2d%2d%2d", &tm.tm_year, &tm.tm_mon,
72
&tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
73
if (items == EOF || items < 4) {
74
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
75
"only parsed %d items in general time string %s", items, timestr);
76
debug_return_time_t(-1);
77
}
78
79
/* Parse optional fractional hours/minute/second if present. */
80
cp = timestr + len;
81
if ((cp[0] == '.' || cp[0] == ',') && isdigit((unsigned char)cp[1])) {
82
int frac = cp[1] - '0';
83
switch (items) {
84
case 4:
85
/* convert fractional hour -> minutes */
86
tm.tm_min += 60 / 10 * frac;
87
break;
88
case 5:
89
/* convert fractional minute -> seconds */
90
tm.tm_sec += 60 / 10 * frac;
91
break;
92
case 6:
93
/* ignore fractional second */
94
break;
95
}
96
cp += 2; /* skip over radix and fraction */
97
}
98
99
/* Parse optional time zone. */
100
switch (*cp) {
101
case '-':
102
case '+': {
103
int hour = 0, min = 0;
104
105
/* No DST */
106
tm.tm_isdst = 0;
107
/* time zone offset must be hh or hhmm */
108
len = strspn(cp + 1, "0123456789");
109
if (len != 2 && len != 4) {
110
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
111
"unable to parse time zone offset in %s, bad tz offset",
112
timestr);
113
debug_return_time_t(-1);
114
}
115
/* parse time zone offset */
116
items = sscanf(cp + 1, "%2d%2d", &hour, &min);
117
if (items == EOF || items < 1) {
118
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
119
"unable to parse time zone offset in %s, items %d",
120
timestr, items);
121
debug_return_time_t(-1);
122
}
123
if (*cp == '-')
124
tzoff = -((hour * 60) + min) * 60;
125
else
126
tzoff = ((hour * 60) + min) * 60;
127
cp += 1 + (items * 2);
128
break;
129
}
130
case 'Z':
131
/* GMT/UTC, no DST */
132
tm.tm_isdst = 0;
133
cp++;
134
break;
135
case '\0':
136
/* no zone specified, use local time */
137
tm.tm_isdst = -1;
138
islocal = true;
139
break;
140
default:
141
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
142
"unable to parse general time string %s", timestr);
143
debug_return_time_t(-1);
144
}
145
if (*cp != '\0') {
146
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
147
"trailing garbage in general time string %s", timestr);
148
debug_return_time_t(-1);
149
}
150
151
/* Adjust from Generalized Time to struct tm */
152
tm.tm_year -= 1900;
153
tm.tm_mon--;
154
155
if (islocal) {
156
result = mktime(&tm);
157
} else {
158
result = timegm(&tm);
159
if (result != -1) {
160
/* Adjust time based on supplied GMT offset. */
161
result -= tzoff;
162
}
163
}
164
165
debug_return_time_t(result);
166
}
167
168