From 95b50f84af6eb3f4cecb5990210ee949f19453d2 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Tue, 23 Apr 2013 13:15:22 -0400 Subject: [PATCH] Use time(&now) instead of now = time(NULL) when storing the current time in a time_t (better compiler error checking). Better parsing and printing of 64-bit time_t on 32-bit platforms. --- common/sudo_debug.c | 4 ++-- config.h.in | 3 +++ configure | 4 ++-- configure.in | 5 ++--- plugins/sudoers/boottime.c | 22 +++++++++++++++++----- plugins/sudoers/iolog.c | 4 ++-- plugins/sudoers/ldap.c | 16 ++++++++-------- plugins/sudoers/logging.c | 4 ++-- plugins/sudoers/sudoreplay.c | 6 +++--- plugins/sudoers/timestamp.c | 2 +- 10 files changed, 42 insertions(+), 28 deletions(-) diff --git a/common/sudo_debug.c b/common/sudo_debug.c index f5c73d2e8..10251cf24 100644 --- a/common/sudo_debug.c +++ b/common/sudo_debug.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2012 Todd C. Miller + * Copyright (c) 2011-2013 Todd C. Miller * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -381,7 +381,7 @@ sudo_debug_write_file(const char *func, const char *file, int lineno, } /* Do timestamp last due to ctime's static buffer. */ - now = time(NULL); + time(&now); timestr = ctime(&now) + 4; timestr[15] = ' '; /* replace year with a space */ timestr[16] = '\0'; diff --git a/config.h.in b/config.h.in index 5b0ded496..2ee54c458 100644 --- a/config.h.in +++ b/config.h.in @@ -551,6 +551,9 @@ /* Define to 1 if you have the `strsignal' function. */ #undef HAVE_STRSIGNAL +/* Define to 1 if you have the `strtoll' function. */ +#undef HAVE_STRTOLL + /* Define to 1 if `d_type' is a member of `struct dirent'. */ #undef HAVE_STRUCT_DIRENT_D_TYPE diff --git a/configure b/configure index 139e045b9..8bc0f8391 100755 --- a/configure +++ b/configure @@ -16739,8 +16739,8 @@ $as_echo "#define HAVE_GETGROUPS 1" >>confdefs.h fi LIBS=$ac_save_LIBS -for ac_func in glob strrchr sysconf tzset strftime setenv \ - regcomp nl_langinfo +for ac_func in glob nl_langinfo regcomp setenv strftime strrchr strtoll \ + sysconf tzset do : as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" diff --git a/configure.in b/configure.in index 7cab1b1fb..c3e6f73b6 100644 --- a/configure.in +++ b/configure.in @@ -2229,9 +2229,8 @@ dnl dnl Function checks dnl AC_FUNC_GETGROUPS -AC_CHECK_FUNCS(glob strrchr sysconf tzset strftime setenv \ - regcomp nl_langinfo) -dnl AC_REPLACE_FUNCS(getgrouplist) +AC_CHECK_FUNCS(glob nl_langinfo regcomp setenv strftime strrchr strtoll \ + sysconf tzset) AC_CHECK_FUNCS(getgrouplist, [], [ case "$host_os" in aix*) diff --git a/plugins/sudoers/boottime.c b/plugins/sudoers/boottime.c index 6c8f28225..1ee052ce6 100644 --- a/plugins/sudoers/boottime.c +++ b/plugins/sudoers/boottime.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009-2011 Todd C. Miller + * Copyright (c) 2009-2013 Todd C. Miller * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -63,7 +63,7 @@ int get_boottime(struct timeval *tv) { - char *line = NULL; + char *ep, *line = NULL; size_t linesize = 0; ssize_t len; FILE * fp; @@ -74,9 +74,21 @@ get_boottime(struct timeval *tv) if (fp != NULL) { while ((len = getline(&line, &linesize, fp)) != -1) { if (strncmp(line, "btime ", 6) == 0) { - tv->tv_sec = atoi(line + 6); - tv->tv_usec = 0; - debug_return_bool(1); +#ifdef HAVE_STRTOLL + long long llval = strtoll(line + 6, &ep, 10); + if (line[6] != '\0' && *ep == '\0' && (time_t)llval == llval) { + tv->tv_sec = (time_t)llval; + tv->tv_usec = 0; + debug_return_bool(1); + } +#else + long lval = strtol(line + 6, &ep, 10); + if (line[6] != '\0' && *ep == '\0' && (time_t)lval == lval) { + tv->tv_sec = (time_t)llval; + tv->tv_usec = 0; + debug_return_bool(1); + } +#endif } } fclose(fp); diff --git a/plugins/sudoers/iolog.c b/plugins/sudoers/iolog.c index 8611314e0..b75aa7789 100644 --- a/plugins/sudoers/iolog.c +++ b/plugins/sudoers/iolog.c @@ -592,8 +592,8 @@ sudoers_io_open(unsigned int version, sudo_conv_t conversation, } gettimeofday(&last_time, NULL); - fprintf(io_log_files[IOFD_LOG].fd.f, "%ld:%s:%s:%s:%s:%d:%d\n%s\n%s", - (long)last_time.tv_sec, + fprintf(io_log_files[IOFD_LOG].fd.f, "%lld:%s:%s:%s:%s:%d:%d\n%s\n%s", + (long long)last_time.tv_sec, details.user ? details.user : "unknown", details.runas_pw->pw_name, details.runas_gr ? details.runas_gr->gr_name : "", details.tty ? details.tty : "unknown", details.lines, details.cols, diff --git a/plugins/sudoers/ldap.c b/plugins/sudoers/ldap.c index c487cdc71..939881567 100644 --- a/plugins/sudoers/ldap.c +++ b/plugins/sudoers/ldap.c @@ -2096,12 +2096,12 @@ sudo_ldap_set_options_conn(LDAP *ld) struct timeval tv; tv.tv_sec = ldap_conf.timeout; tv.tv_usec = 0; - DPRINTF(("ldap_set_option(LDAP_OPT_TIMEOUT, %ld)", - (long)tv.tv_sec), 1); + DPRINTF(("ldap_set_option(LDAP_OPT_TIMEOUT, %d)", + ldap_conf.timeout), 1); rc = ldap_set_option(ld, LDAP_OPT_TIMEOUT, &tv); if (rc != LDAP_OPT_SUCCESS) { - warningx("ldap_set_option(TIMEOUT, %ld): %s", - (long)tv.tv_sec, ldap_err2string(rc)); + warningx("ldap_set_option(TIMEOUT, %d): %s", + ldap_conf.timeout, ldap_err2string(rc)); } } #endif @@ -2111,14 +2111,14 @@ sudo_ldap_set_options_conn(LDAP *ld) struct timeval tv; tv.tv_sec = ldap_conf.bind_timelimit / 1000; tv.tv_usec = 0; - DPRINTF(("ldap_set_option(LDAP_OPT_NETWORK_TIMEOUT, %ld)", - (long)tv.tv_sec), 1); + DPRINTF(("ldap_set_option(LDAP_OPT_NETWORK_TIMEOUT, %d)", + ldap_conf.bind_timelimit / 1000), 1); rc = ldap_set_option(ld, LDAP_OPT_NETWORK_TIMEOUT, &tv); # if !defined(LDAP_OPT_CONNECT_TIMEOUT) || LDAP_VENDOR_VERSION != 510 /* Tivoli Directory Server 6.3 libs always return a (bogus) error. */ if (rc != LDAP_OPT_SUCCESS) { - warningx("ldap_set_option(NETWORK_TIMEOUT, %ld): %s", - (long)tv.tv_sec, ldap_err2string(rc)); + warningx("ldap_set_option(NETWORK_TIMEOUT, %d): %s", + ldap_conf.bind_timelimit / 1000, ldap_err2string(rc)); } # endif } diff --git a/plugins/sudoers/logging.c b/plugins/sudoers/logging.c index fde39c49d..d7256a5f3 100644 --- a/plugins/sudoers/logging.c +++ b/plugins/sudoers/logging.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1994-1996, 1998-2012 Todd C. Miller + * Copyright (c) 1994-1996, 1998-2013 Todd C. Miller * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -197,7 +197,7 @@ do_logfile(char *msg) send_mail(_("unable to lock log file: %s: %s"), def_logfile, strerror(errno)); } else { - now = time(NULL); + time(&now); if (def_loglinelen < sizeof(LOG_INDENT)) { /* Don't pretty-print long log file lines (hard to grep) */ if (def_log_host) diff --git a/plugins/sudoers/sudoreplay.c b/plugins/sudoers/sudoreplay.c index 7180696b3..17da538d3 100644 --- a/plugins/sudoers/sudoreplay.c +++ b/plugins/sudoers/sudoreplay.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009-2012 Todd C. Miller + * Copyright (c) 2009-2013 Todd C. Miller * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -525,8 +525,8 @@ delay(double secs) rval = nanosleep(&ts, &rts); } while (rval == -1 && errno == EINTR); if (rval == -1) { - fatal_nodebug(_("nanosleep: tv_sec %ld, tv_nsec %ld"), - (long)ts.tv_sec, (long)ts.tv_nsec); + fatal_nodebug(_("nanosleep: tv_sec %lld, tv_nsec %ld"), + (long long)ts.tv_sec, (long)ts.tv_nsec); } } diff --git a/plugins/sudoers/timestamp.c b/plugins/sudoers/timestamp.c index b9b52d99c..99d589a56 100644 --- a/plugins/sudoers/timestamp.c +++ b/plugins/sudoers/timestamp.c @@ -340,7 +340,7 @@ timestamp_status_internal(bool removing) if (def_timestamp_timeout < 0) { status = TS_CURRENT; } else { - now = time(NULL); + time(&now); if (def_timestamp_timeout && now - mtime.tv_sec < 60 * def_timestamp_timeout) { /*