Coverage Report

Created: 2026-12-01 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/sudo/plugins/sudoers/gentime.c
Line
Count
Source
1
/*
2
 * SPDX-License-Identifier: ISC
3
 *
4
 * Copyright (c) 2017, 2021 Todd C. Miller <Todd.Miller@sudo.ws>
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
 * Number of days in month mon (1-12) of year (Gregorian calendar).
43
 */
44
static int
45
days_in_month(int year, int mon)
46
0
{
47
0
    static const int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
48
49
0
    if (mon == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
50
0
  return 29;
51
0
    return mdays[mon - 1];
52
0
}
53
54
/*
55
 * Parse a timestamp in Generalized Time format as per RFC4517.
56
 * E.g. yyyymmddHHMMSS.FZ or yyyymmddHHMMSS.F[+-]TZOFF
57
 * where minutes, seconds and fraction are optional.
58
 * Returns the time in Unix time format or -1 on error.
59
 */
60
time_t
61
parse_gentime(const char *timestr)
62
0
{
63
0
    char tcopy[sizeof("yyyymmddHHMMSS")];
64
0
    const char *cp;
65
0
    time_t result;
66
0
    struct tm tm;
67
0
    size_t len;
68
0
    int items, tzoff = 0;
69
0
    bool islocal = false;
70
0
    debug_decl(parse_gentime, SUDOERS_DEBUG_PARSER);
71
72
    /* Make a copy of the non-fractional time without zone for easy parsing. */
73
0
    len = strspn(timestr, "0123456789");
74
0
    if (len >= sizeof(tcopy) || len < sizeof("yyyymmddHH") -1 || (len & 1)) {
75
0
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
76
0
      "unable to parse general time string %s", timestr);
77
0
  debug_return_time_t(-1);
78
0
    }
79
0
    memcpy(tcopy, timestr, len);
80
0
    tcopy[len] = '\0';
81
82
    /* Parse general time, ignoring the timezone for now. */
83
0
    memset(&tm, 0, sizeof(tm));
84
0
    items = sscanf(tcopy, "%4d%2d%2d%2d%2d%2d", &tm.tm_year, &tm.tm_mon,
85
0
  &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
86
0
    if (items == EOF || items < 4) {
87
0
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
88
0
      "only parsed %d items in general time string %s", items, timestr); 
89
0
  debug_return_time_t(-1);
90
0
    }
91
92
    /*
93
     * Reject out-of-range fields (RFC 4517): timegm()/mktime() would
94
     * otherwise silently normalize them, e.g. February 30 to March 2.
95
     * Checked before any fraction is applied; 60 seconds is a leap second.
96
     * As in ISO 8601, 24:00:00 is accepted as the end of the day.
97
     */
98
0
    if (tm.tm_mon < 1 || tm.tm_mon > 12 || tm.tm_mday < 1 ||
99
0
      tm.tm_mday > days_in_month(tm.tm_year, tm.tm_mon) ||
100
0
      tm.tm_hour > 24 || tm.tm_min > 59 || tm.tm_sec > 60 ||
101
0
      (tm.tm_hour == 24 && (tm.tm_min != 0 || tm.tm_sec != 0))) {
102
0
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
103
0
      "out of range value in general time string %s", timestr);
104
0
  debug_return_time_t(-1);
105
0
    }
106
107
    /* Parse optional fractional hours/minute/second if present. */
108
0
    cp = timestr + len;
109
0
    if ((cp[0] == '.' || cp[0] == ',') && isdigit((unsigned char)cp[1])) {
110
0
  int frac = cp[1] - '0';
111
0
  if (tm.tm_hour == 24) {
112
      /* 24:00:00 is the end of the day, there is nothing after it. */
113
0
      sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
114
0
    "fraction after 24:00:00 in general time string %s", timestr);
115
0
      debug_return_time_t(-1);
116
0
  }
117
0
  switch (items) {
118
0
  case 4:
119
      /* convert fractional hour -> minutes */
120
0
      tm.tm_min += 60 / 10 * frac;
121
0
      break;
122
0
  case 5:
123
      /* convert fractional minute -> seconds */
124
0
      tm.tm_sec += 60 / 10 * frac;
125
0
      break;
126
0
  case 6:
127
      /* ignore fractional second */
128
0
      break;
129
0
  }
130
  /* skip over radix and fraction, only the first digit is used */
131
0
  for (cp += 2; isdigit((unsigned char)*cp); cp++)
132
0
      continue;
133
0
    }
134
135
    /* Parse optional time zone. */
136
0
    switch (*cp) {
137
0
    case '-':
138
0
    case '+': {
139
0
  int hour = 0, min = 0;
140
141
  /* No DST */
142
0
  tm.tm_isdst = 0;
143
  /* time zone offset must be hh or hhmm */
144
0
  len = strspn(cp + 1, "0123456789");
145
0
  if (len != 2 && len != 4) {
146
0
      sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
147
0
    "unable to parse time zone offset in %s, bad tz offset",
148
0
    timestr);
149
0
      debug_return_time_t(-1);
150
0
  }
151
  /* parse time zone offset */
152
0
  items = sscanf(cp + 1, "%2d%2d", &hour, &min);
153
0
  if (items == EOF || items < 1) {
154
0
      sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
155
0
    "unable to parse time zone offset in %s, items %d",
156
0
    timestr, items);
157
0
      debug_return_time_t(-1);
158
0
  }
159
0
  if (hour > 23 || min > 59) {
160
0
      sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
161
0
    "time zone offset out of range in %s", timestr);
162
0
      debug_return_time_t(-1);
163
0
  }
164
0
  if (*cp == '-')
165
0
      tzoff = -((hour * 60) + min) * 60;
166
0
  else
167
0
      tzoff = ((hour * 60) + min) * 60;
168
0
  cp += 1 + (items * 2);
169
0
  break;
170
0
    }
171
0
    case 'Z':
172
  /* GMT/UTC, no DST */
173
0
  tm.tm_isdst = 0;
174
0
  cp++;
175
0
  break;
176
0
    case '\0':
177
  /* no zone specified, use local time */
178
0
  tm.tm_isdst = -1;
179
0
  islocal = true;
180
0
  break;
181
0
    default:
182
0
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
183
0
      "unable to parse general time string %s", timestr);
184
0
  debug_return_time_t(-1);
185
0
    }
186
0
    if (*cp != '\0') {
187
0
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
188
0
      "trailing garbage in general time string %s", timestr);
189
0
  debug_return_time_t(-1);
190
0
    }
191
192
    /* Adjust from Generalized Time to struct tm */
193
0
    tm.tm_year -= 1900;
194
0
    tm.tm_mon--;
195
196
0
    if (islocal) {
197
0
  result = mktime(&tm);
198
0
    } else {
199
0
  result = timegm(&tm);
200
0
  if (result != -1) {
201
      /* Adjust time based on supplied GMT offset. */
202
0
      result -= tzoff;
203
0
  }
204
0
    }
205
206
0
    debug_return_time_t(result);
207
0
}