Coverage Report

Created: 2025-10-10 07:08

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
 * 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
0
{
50
0
    char tcopy[sizeof("yyyymmddHHMMSS")];
51
0
    const char *cp;
52
0
    time_t result;
53
0
    struct tm tm;
54
0
    size_t len;
55
0
    int items, tzoff = 0;
56
0
    bool islocal = false;
57
0
    debug_decl(parse_gentime, SUDOERS_DEBUG_PARSER);
58
59
    /* Make a copy of the non-fractional time without zone for easy parsing. */
60
0
    len = strspn(timestr, "0123456789");
61
0
    if (len >= sizeof(tcopy) || len < sizeof("yyyymmddHH") -1 || (len & 1)) {
62
0
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
63
0
      "unable to parse general time string %s", timestr);
64
0
  debug_return_time_t(-1);
65
0
    }
66
0
    memcpy(tcopy, timestr, len);
67
0
    tcopy[len] = '\0';
68
69
    /* Parse general time, ignoring the timezone for now. */
70
0
    memset(&tm, 0, sizeof(tm));
71
0
    items = sscanf(tcopy, "%4d%2d%2d%2d%2d%2d", &tm.tm_year, &tm.tm_mon,
72
0
  &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
73
0
    if (items == EOF || items < 4) {
74
0
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
75
0
      "only parsed %d items in general time string %s", items, timestr); 
76
0
  debug_return_time_t(-1);
77
0
    }
78
79
    /* Parse optional fractional hours/minute/second if present. */
80
0
    cp = timestr + len;
81
0
    if ((cp[0] == '.' || cp[0] == ',') && isdigit((unsigned char)cp[1])) {
82
0
  int frac = cp[1] - '0';
83
0
  switch (items) {
84
0
  case 4:
85
      /* convert fractional hour -> minutes */
86
0
      tm.tm_min += 60 / 10 * frac;
87
0
      break;
88
0
  case 5:
89
      /* convert fractional minute -> seconds */
90
0
      tm.tm_sec += 60 / 10 * frac;
91
0
      break;
92
0
  case 6:
93
      /* ignore fractional second */
94
0
      break;
95
0
  }
96
0
  cp += 2;  /* skip over radix and fraction */
97
0
    }
98
99
    /* Parse optional time zone. */
100
0
    switch (*cp) {
101
0
    case '-':
102
0
    case '+': {
103
0
  int hour = 0, min = 0;
104
105
  /* No DST */
106
0
  tm.tm_isdst = 0;
107
  /* time zone offset must be hh or hhmm */
108
0
  len = strspn(cp + 1, "0123456789");
109
0
  if (len != 2 && len != 4) {
110
0
      sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
111
0
    "unable to parse time zone offset in %s, bad tz offset",
112
0
    timestr);
113
0
      debug_return_time_t(-1);
114
0
  }
115
  /* parse time zone offset */
116
0
  items = sscanf(cp + 1, "%2d%2d", &hour, &min);
117
0
  if (items == EOF || items < 1) {
118
0
      sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
119
0
    "unable to parse time zone offset in %s, items %d",
120
0
    timestr, items);
121
0
      debug_return_time_t(-1);
122
0
  }
123
0
  if (*cp == '-')
124
0
      tzoff = -((hour * 60) + min) * 60;
125
0
  else
126
0
      tzoff = ((hour * 60) + min) * 60;
127
0
  cp += 1 + (items * 2);
128
0
  break;
129
0
    }
130
0
    case 'Z':
131
  /* GMT/UTC, no DST */
132
0
  tm.tm_isdst = 0;
133
0
  cp++;
134
0
  break;
135
0
    case '\0':
136
  /* no zone specified, use local time */
137
0
  tm.tm_isdst = -1;
138
0
  islocal = true;
139
0
  break;
140
0
    default:
141
0
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
142
0
      "unable to parse general time string %s", timestr);
143
0
  debug_return_time_t(-1);
144
0
    }
145
0
    if (*cp != '\0') {
146
0
  sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
147
0
      "trailing garbage in general time string %s", timestr);
148
0
  debug_return_time_t(-1);
149
0
    }
150
151
    /* Adjust from Generalized Time to struct tm */
152
0
    tm.tm_year -= 1900;
153
0
    tm.tm_mon--;
154
155
0
    if (islocal) {
156
0
  result = mktime(&tm);
157
0
    } else {
158
0
  result = timegm(&tm);
159
0
  if (result != -1) {
160
      /* Adjust time based on supplied GMT offset. */
161
0
      result -= tzoff;
162
0
  }
163
0
    }
164
165
0
    debug_return_time_t(result);
166
0
}