From 49bf0cc84bd583b3559657cc736b2b30a875618a Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Fri, 17 Sep 2021 14:01:29 -0600 Subject: [PATCH] Use localtime_r() not gmtime_r() when formatting the local time. This is consistent with how sudo formatted time stamps prior to the logging code being split off into libeventlog. We only need to use gmtime_r() for ISO 8601 time. --- lib/eventlog/eventlog.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/eventlog/eventlog.c b/lib/eventlog/eventlog.c index a6abc61d9..49636ebc6 100644 --- a/lib/eventlog/eventlog.c +++ b/lib/eventlog/eventlog.c @@ -424,7 +424,7 @@ send_mail(const struct eventlog *evlog, const char *fmt, ...) const struct eventlog_config *evl_conf = eventlog_getconf(); const char *cp, *timefmt = evl_conf->time_fmt; char timebuf[1024]; - struct tm gmt; + struct tm tm; time_t now; FILE *mail; int fd, len, pfd[2], status; @@ -445,7 +445,7 @@ send_mail(const struct eventlog *evlog, const char *fmt, ...) debug_return_bool(false); time(&now); - if (gmtime_r(&now, &gmt) == NULL) + if (localtime_r(&now, &tm) == NULL) debug_return_bool(false); /* Fork and return, child will daemonize. */ @@ -569,13 +569,13 @@ send_mail(const struct eventlog *evlog, const char *fmt, ...) #endif /* HAVE_NL_LANGINFO && CODESET */ timebuf[sizeof(timebuf) - 1] = '\0'; - len = strftime(timebuf, sizeof(timebuf), timefmt, &gmt); + len = strftime(timebuf, sizeof(timebuf), timefmt, &tm); if (len == 0 || timebuf[sizeof(timebuf) - 1] != '\0') { sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_ERROR, "strftime() failed to format time: %s", timefmt); /* Fall back to default time format string. */ timebuf[sizeof(timebuf) - 1] = '\0'; - len = strftime(timebuf, sizeof(timebuf), "%h %e %T", &gmt); + len = strftime(timebuf, sizeof(timebuf), "%h %e %T", &tm); if (len == 0 || timebuf[sizeof(timebuf) - 1] != '\0') { timebuf[0] = '\0'; /* give up */ } @@ -631,20 +631,22 @@ json_add_timestamp(struct json_container *json, const char *name, const char *timefmt = evl_conf->time_fmt; time_t secs = ts->tv_sec; char timebuf[1024]; - struct tm gmt; + struct tm tm; - if (gmtime_r(&secs, &gmt) != NULL) { + if (gmtime_r(&secs, &tm) != NULL) { timebuf[sizeof(timebuf) - 1] = '\0'; - len = strftime(timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", &gmt); + len = strftime(timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", &tm); if (len != 0 && timebuf[sizeof(timebuf) - 1] == '\0') { json_value.type = JSON_STRING; json_value.u.string = timebuf; // -V507 if (!sudo_json_add_value(json, "iso8601", &json_value)) goto oom; } + } + if (localtime_r(&secs, &tm) != NULL) { timebuf[sizeof(timebuf) - 1] = '\0'; - len = strftime(timebuf, sizeof(timebuf), timefmt, &gmt); + len = strftime(timebuf, sizeof(timebuf), timefmt, &tm); if (len != 0 && timebuf[sizeof(timebuf) - 1] == '\0') { json_value.type = JSON_STRING; json_value.u.string = timebuf; // -V507