From f029e3e7446a8c703305d12b43bd1cf5959f9f8f Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Thu, 26 Jun 2014 15:51:15 -0600 Subject: [PATCH] Remove touch() from fileops.c and just call utimes/futimes directly. Rename lock_file -> sudo_lock_file to avoid namespace pollution --- include/fileops.h | 6 ++--- lib/util/fileops.c | 45 ++++++------------------------------- lib/util/util.exp | 3 +-- plugins/sudoers/iolog.c | 2 +- plugins/sudoers/ldap.c | 2 +- plugins/sudoers/logging.c | 4 ++-- plugins/sudoers/timestamp.c | 6 ++--- plugins/sudoers/visudo.c | 14 +++++++----- src/sudo_edit.c | 16 +++++++------ 9 files changed, 35 insertions(+), 63 deletions(-) diff --git a/include/fileops.h b/include/fileops.h index 44cf9d6de..c1a364016 100644 --- a/include/fileops.h +++ b/include/fileops.h @@ -1,5 +1,6 @@ /* - * Copyright (c) 2010, 2011, 2013 Todd C. Miller + * Copyright (c) 2010, 2011, 2013, 2014 + * 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 @@ -26,8 +27,7 @@ struct timeval; -__dso_public bool lock_file(int, int); -__dso_public int touch(int, char *, struct timeval *); +__dso_public bool sudo_lock_file(int, int); __dso_public ssize_t sudo_parseln(char **buf, size_t *bufsize, unsigned int *lineno, FILE *fp); #endif /* _SUDO_FILEOPS_H */ diff --git a/lib/util/fileops.c b/lib/util/fileops.c index c0f34519c..3f715cea4 100644 --- a/lib/util/fileops.c +++ b/lib/util/fileops.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999-2005, 2007, 2009-2013 + * Copyright (c) 1999-2005, 2007, 2009-2014 * Todd C. Miller * * Permission to use, copy, modify, and distribute this software for any @@ -56,51 +56,20 @@ #else # include "compat/stdbool.h" #endif -#ifdef TIME_WITH_SYS_TIME -# include -#endif -#ifndef HAVE_STRUCT_TIMESPEC -# include "compat/timespec.h" -#endif #include "missing.h" #include "fileops.h" #include "sudo_debug.h" -/* - * Update the access and modify times on an fd or file. - */ -int -touch(int fd, char *path, struct timeval *tvp) -{ - struct timeval times[2]; - int rval = -1; - debug_decl(touch, SUDO_DEBUG_UTIL) - - if (tvp != NULL) { - times[0].tv_sec = times[1].tv_sec = tvp->tv_sec; - times[0].tv_usec = times[1].tv_usec = tvp->tv_usec; - } - -#if defined(HAVE_FUTIME) || defined(HAVE_FUTIMES) - if (fd != -1) - rval = futimes(fd, tvp ? times : NULL); - else -#endif - if (path != NULL) - rval = utimes(path, tvp ? times : NULL); - debug_return_int(rval); -} - /* * Lock/unlock a file. */ #ifdef HAVE_LOCKF bool -lock_file(int fd, int lockit) +sudo_lock_file(int fd, int lockit) { int op = 0; - debug_decl(lock_file, SUDO_DEBUG_UTIL) + debug_decl(sudo_lock_file, SUDO_DEBUG_UTIL) switch (lockit) { case SUDO_LOCK: @@ -117,10 +86,10 @@ lock_file(int fd, int lockit) } #elif defined(HAVE_FLOCK) bool -lock_file(int fd, int lockit) +sudo_lock_file(int fd, int lockit) { int op = 0; - debug_decl(lock_file, SUDO_DEBUG_UTIL) + debug_decl(sudo_lock_file, SUDO_DEBUG_UTIL) switch (lockit) { case SUDO_LOCK: @@ -137,12 +106,12 @@ lock_file(int fd, int lockit) } #else bool -lock_file(int fd, int lockit) +sudo_lock_file(int fd, int lockit) { #ifdef F_SETLK int func; struct flock lock; - debug_decl(lock_file, SUDO_DEBUG_UTIL) + debug_decl(sudo_lock_file, SUDO_DEBUG_UTIL) lock.l_start = 0; lock.l_len = 0; diff --git a/lib/util/util.exp b/lib/util/util.exp index 981e5d848..7c05a4f3b 100644 --- a/lib/util/util.exp +++ b/lib/util/util.exp @@ -50,7 +50,6 @@ lbuf_append_quoted lbuf_destroy lbuf_init lbuf_print -lock_file parse_gid_list sudo_asprintf sudo_clock_gettime @@ -116,6 +115,7 @@ sudo_getopt_long_only sudo_glob sudo_globfree sudo_inet_pton +sudo_lock_file sudo_memrchr sudo_memset_s sudo_mkdtemp @@ -142,7 +142,6 @@ term_kill term_noecho term_raw term_restore -touch vfatal_nodebug vfatalx_nodebug vwarning_nodebug diff --git a/plugins/sudoers/iolog.c b/plugins/sudoers/iolog.c index 18200d96c..9c8a7e72a 100644 --- a/plugins/sudoers/iolog.c +++ b/plugins/sudoers/iolog.c @@ -209,7 +209,7 @@ io_nextid(char *iolog_dir, char *iolog_dir_fallback, char sessid[7]) log_warning(SLOG_SEND_MAIL, N_("unable to open %s"), pathbuf); debug_return_bool(false); } - lock_file(fd, SUDO_LOCK); + sudo_lock_file(fd, SUDO_LOCK); /* * If there is no seq file in iolog_dir and a fallback dir was diff --git a/plugins/sudoers/ldap.c b/plugins/sudoers/ldap.c index 5890e4b96..c2d04a139 100644 --- a/plugins/sudoers/ldap.c +++ b/plugins/sudoers/ldap.c @@ -2118,7 +2118,7 @@ sudo_krb5_copy_cc_file(const char *old_ccname) if (ofd != -1) { (void) fcntl(ofd, F_SETFL, 0); - if (lock_file(ofd, SUDO_LOCK)) { + if (sudo_lock_file(ofd, SUDO_LOCK)) { snprintf(new_ccname, sizeof(new_ccname), "%s%s", _PATH_TMP, "sudocc_XXXXXXXX"); nfd = mkstemp(new_ccname); diff --git a/plugins/sudoers/logging.c b/plugins/sudoers/logging.c index a84a13a25..18fe3a124 100644 --- a/plugins/sudoers/logging.c +++ b/plugins/sudoers/logging.c @@ -186,7 +186,7 @@ do_logfile(char *msg) if (fp == NULL) { send_mail(_("unable to open log file: %s: %s"), def_logfile, strerror(errno)); - } else if (!lock_file(fileno(fp), SUDO_LOCK)) { + } else if (!sudo_lock_file(fileno(fp), SUDO_LOCK)) { send_mail(_("unable to lock log file: %s: %s"), def_logfile, strerror(errno)); } else { @@ -217,7 +217,7 @@ do_logfile(char *msg) efree(full_line); } (void) fflush(fp); - (void) lock_file(fileno(fp), SUDO_UNLOCK); + (void) sudo_lock_file(fileno(fp), SUDO_UNLOCK); (void) fclose(fp); } sudoers_setlocale(oldlocale, NULL); diff --git a/plugins/sudoers/timestamp.c b/plugins/sudoers/timestamp.c index 8f75607ad..c41d2a36d 100644 --- a/plugins/sudoers/timestamp.c +++ b/plugins/sudoers/timestamp.c @@ -360,7 +360,7 @@ update_timestamp(struct passwd *pw) } /* Update record or append a new one. */ - lock_file(fd, SUDO_LOCK); + sudo_lock_file(fd, SUDO_LOCK); ts_update_record(fd, &entry, timestamp_hint); close(fd); @@ -444,7 +444,7 @@ timestamp_status(struct passwd *pw) status = TS_MISSING; goto done; } - lock_file(fd, SUDO_LOCK); + sudo_lock_file(fd, SUDO_LOCK); /* Ignore and clear time stamp file if mtime predates boot time. */ if (fstat(fd, &sb) == 0) { @@ -574,7 +574,7 @@ remove_timestamp(bool unlink_it) (void) restore_perms(); if (fd == -1) goto done; - lock_file(fd, SUDO_LOCK); + sudo_lock_file(fd, SUDO_LOCK); /* * Find matching entries and invalidate them. diff --git a/plugins/sudoers/visudo.c b/plugins/sudoers/visudo.c index eef661533..65696da48 100644 --- a/plugins/sudoers/visudo.c +++ b/plugins/sudoers/visudo.c @@ -315,7 +315,7 @@ edit_sudoers(struct sudoersfile *sp, char *editor, char *args, int lineno) char *cp; /* scratch char pointer */ char buf[PATH_MAX*2]; /* buffer used for copying files */ char linestr[64]; /* string version of lineno */ - struct timeval tv, tv1, tv2; /* time before and after edit */ + struct timeval tv, times[2]; /* time before and after edit */ struct timeval orig_mtim; /* starting mtime of sudoers file */ off_t orig_size; /* starting size of sudoers file */ ssize_t nread; /* number of bytes read */ @@ -351,7 +351,9 @@ edit_sudoers(struct sudoersfile *sp, char *editor, char *args, int lineno) } (void) close(tfd); } - (void) touch(-1, sp->tpath, &orig_mtim); + times[0].tv_sec = times[1].tv_sec = orig_mtim.tv_sec; + times[0].tv_usec = times[1].tv_usec = orig_mtim.tv_usec; + (void) utimes(sp->tpath, times); /* Does the editor support +lineno? */ if (lineno > 0) @@ -421,9 +423,9 @@ edit_sudoers(struct sudoersfile *sp, char *editor, char *args, int lineno) * XPG4 specifies that vi's exit value is a function of the * number of errors during editing (?!?!). */ - gettimeofday(&tv1, NULL); + gettimeofday(×[0], NULL); if (run_command(editor, av) != -1) { - gettimeofday(&tv2, NULL); + gettimeofday(×[1], NULL); /* * Sanity checks. */ @@ -451,7 +453,7 @@ edit_sudoers(struct sudoersfile *sp, char *editor, char *args, int lineno) * If mtime and size match but the user spent no measurable * time in the editor we can't tell if the file was changed. */ - if (sudo_timevalcmp(&tv1, &tv2, !=)) + if (sudo_timevalcmp(×[0], ×[1], !=)) modified = false; } @@ -906,7 +908,7 @@ open_sudoers(const char *path, bool doedit, bool *keepopen) efree(entry); debug_return_ptr(NULL); } - if (!checkonly && !lock_file(entry->fd, SUDO_TLOCK)) + if (!checkonly && !sudo_lock_file(entry->fd, SUDO_TLOCK)) fatalx(U_("%s busy, try again later"), entry->path); if ((fp = fdopen(entry->fd, "r")) == NULL) fatal("%s", entry->path); diff --git a/src/sudo_edit.c b/src/sudo_edit.c index 6073c79ff..7a4ee3a44 100644 --- a/src/sudo_edit.c +++ b/src/sudo_edit.c @@ -93,7 +93,7 @@ sudo_edit(struct command_details *command_details) int rc, i, j, ac, ofd, tfd, nargc, rval, tmplen; int editor_argc = 0, nfiles = 0; struct stat sb; - struct timeval tv, tv1, tv2; + struct timeval tv, times[2]; struct tempfile { char *tfile; char *ofile; @@ -211,10 +211,12 @@ sudo_edit(struct command_details *command_details) * We always update the stashed mtime because the time * resolution of the filesystem the temporary file is on may * not match that of the filesystem where the file to be edited - * resides. It is OK if touch() fails since we only use the info - * to determine whether or not a file has been modified. + * resides. It is OK if futimes() fails since we only use the + * info to determine whether or not a file has been modified. */ - (void) touch(tfd, NULL, &tf[j].omtim); + times[0].tv_sec = times[1].tv_sec = tf[j].omtim.tv_sec; + times[0].tv_usec = times[1].tv_usec = tf[j].omtim.tv_usec; + (void) futimes(tfd, times); rc = fstat(tfd, &sb); if (!rc) mtim_get(&sb, &tf[j].omtim); @@ -241,7 +243,7 @@ sudo_edit(struct command_details *command_details) * Run the editor with the invoking user's creds, * keeping track of the time spent in the editor. */ - gettimeofday(&tv1, NULL); + gettimeofday(×[0], NULL); memcpy(&editor_details, command_details, sizeof(editor_details)); editor_details.uid = user_details.uid; editor_details.euid = user_details.uid; @@ -251,7 +253,7 @@ sudo_edit(struct command_details *command_details) editor_details.groups = user_details.groups; editor_details.argv = nargv; rval = run_command(&editor_details); - gettimeofday(&tv2, NULL); + gettimeofday(×[1], NULL); /* Copy contents of temp files to real ones */ for (i = 0; i < nfiles; i++) { @@ -279,7 +281,7 @@ sudo_edit(struct command_details *command_details) * If mtime and size match but the user spent no measurable * time in the editor we can't tell if the file was changed. */ - if (sudo_timevalcmp(&tv1, &tv2, !=)) { + if (sudo_timevalcmp(×[0], ×[1], !=)) { warningx(U_("%s unchanged"), tf[i].ofile); unlink(tf[i].tfile); close(tfd); -- 2.40.0