Imported Upstream version 1.7.4p6
[debian/sudo] / check.c
diff --git a/check.c b/check.c
index 242aa0054683b23fc5dad715c2a9a752dc081cd6..badf3e4007735df5ec8ad5e454bd3cefc811cf9b 100644 (file)
--- a/check.c
+++ b/check.c
@@ -1,43 +1,39 @@
 /*
- * Copyright (c) 1993-1996,1998-2001 Todd C. Miller <Todd.Miller@courtesan.com>
- * All rights reserved.
+ * Copyright (c) 1993-1996,1998-2005, 2007-2010
+ *     Todd C. Miller <Todd.Miller@courtesan.com>
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
  *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * 3. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
- *
- * 4. Products derived from this software may not be called "Sudo" nor
- *    may "Sudo" appear in their names without specific prior written
- *    permission from the author.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
- * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
- * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * Sponsored in part by the Defense Advanced Research Projects
+ * Agency (DARPA) and Air Force Research Laboratory, Air Force
+ * Materiel Command, USAF, under agreement number F39502-99-1-0512.
  */
 
-#include "config.h"
+#include <config.h>
 
 #include <sys/types.h>
 #include <sys/param.h>
+#include <sys/time.h>
 #include <sys/stat.h>
-#include <sys/file.h>
+#ifdef __linux__
+# include <sys/vfs.h>
+#endif
+#if defined(__sun) && defined(__SVR4)
+# include <sys/statvfs.h>
+#endif
+#ifndef __TANDEM
+# include <sys/file.h>
+#endif
 #include <stdio.h>
 #ifdef STDC_HEADERS
 # include <stdlib.h>
 #endif /* STDC_HEADERS */
 #ifdef HAVE_STRING_H
 # include <string.h>
-#else
-# ifdef HAVE_STRINGS_H
-#  include <strings.h>
-# endif
 #endif /* HAVE_STRING_H */
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif /* HAVE_STRINGS_H */
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif /* HAVE_UNISTD_H */
+#if TIME_WITH_SYS_TIME
+# include <time.h>
+#endif
 #include <errno.h>
 #include <fcntl.h>
 #include <signal.h>
-#include <time.h>
 #include <pwd.h>
 #include <grp.h>
 
 #include "sudo.h"
 
-#ifndef lint
-static const char rcsid[] = "$Sudo: check.c,v 1.203 2002/04/25 15:30:12 millert Exp $";
-#endif /* lint */
-
 /* Status codes for timestamp_status() */
 #define TS_CURRENT             0
 #define TS_OLD                 1
@@ -77,62 +70,135 @@ static const char rcsid[] = "$Sudo: check.c,v 1.203 2002/04/25 15:30:12 millert
 #define TS_NOFILE              3
 #define TS_ERROR               4
 
+/* Flags for timestamp_status() */
+#define TS_MAKE_DIRS           1
+#define TS_REMOVE              2
+
+/*
+ * Info stored in tty ticket from stat(2) to help with tty matching.
+ */
+static struct tty_info {
+    dev_t dev;                 /* ID of device tty resides on */
+    dev_t rdev;                        /* tty device ID */
+    ino_t ino;                 /* tty inode number */
+    struct timeval ctime;      /* tty inode change time */
+} tty_info;
+
 static void  build_timestamp   __P((char **, char **));
 static int   timestamp_status  __P((char *, char *, char *, int));
 static char *expand_prompt     __P((char *, char *, char *));
-static void  lecture           __P((void));
+static void  lecture           __P((int));
 static void  update_timestamp  __P((char *, char *));
+static int   tty_is_devpts     __P((const char *));
 
 /*
  * This function only returns if the user can successfully
- * verify who he/she is.  
+ * verify who he/she is.
  */
 void
-check_user()
+check_user(validated, mode)
+    int validated;
+    int mode;
 {
     char *timestampdir = NULL;
     char *timestampfile = NULL;
     char *prompt;
+    struct stat sb;
     int status;
 
-    if (user_uid == 0 || user_is_exempt())
-       return;
+    /* Stash the tty's ctime for tty ticket comparison. */
+    if (def_tty_tickets && user_ttypath && stat(user_ttypath, &sb) == 0) {
+       tty_info.dev = sb.st_dev;
+       tty_info.ino = sb.st_ino;
+       tty_info.rdev = sb.st_rdev;
+       if (tty_is_devpts(user_ttypath))
+           ctim_get(&sb, &tty_info.ctime);
+    }
+
+    /* Always prompt for a password when -k was specified with the command. */
+    if (ISSET(mode, MODE_INVALIDATE)) {
+       SET(validated, FLAG_CHECK_USER);
+    } else {
+       /*
+        * Don't prompt for the root passwd or if the user is exempt.
+        * If the user is not changing uid/gid, no need for a password.
+        */
+       if (user_uid == 0 || (user_uid == runas_pw->pw_uid &&
+           (!runas_gr || user_in_group(sudo_user.pw, runas_gr->gr_name))) ||
+           user_is_exempt())
+           return;
+    }
 
     build_timestamp(&timestampdir, &timestampfile);
-    status = timestamp_status(timestampdir, timestampfile, user_name, TRUE);
-    if (status != TS_CURRENT) {
-       if (status == TS_MISSING || status == TS_ERROR)
-           lecture();          /* first time through they get a lecture */
+    status = timestamp_status(timestampdir, timestampfile, user_name,
+       TS_MAKE_DIRS);
+
+    if (status != TS_CURRENT || ISSET(validated, FLAG_CHECK_USER)) {
+       /* Bail out if we are non-interactive and a password is required */
+       if (ISSET(mode, MODE_NONINTERACTIVE))
+           errorx(1, "sorry, a password is required to run %s", getprogname());
+
+       /* If user specified -A, make sure we have an askpass helper. */
+       if (ISSET(tgetpass_flags, TGP_ASKPASS)) {
+           if (user_askpass == NULL)
+               log_error(NO_MAIL,
+                   "no askpass program specified, try setting SUDO_ASKPASS");
+       } else if (!ISSET(tgetpass_flags, TGP_STDIN)) {
+           /* If no tty but DISPLAY is set, use askpass if we have it. */
+           if (!user_ttypath && !tty_present()) {
+               if (user_askpass && user_display && *user_display != '\0') {
+                   SET(tgetpass_flags, TGP_ASKPASS);
+               } else if (!def_visiblepw) {
+                   log_error(NO_MAIL,
+                       "no tty present and no askpass program specified");
+               }
+           }
+       }
+
+       if (!ISSET(tgetpass_flags, TGP_ASKPASS))
+           lecture(status);
 
        /* Expand any escapes in the prompt. */
-       prompt = expand_prompt(user_prompt ? user_prompt : def_str(I_PASSPROMPT),
+       prompt = expand_prompt(user_prompt ? user_prompt : def_passprompt,
            user_name, user_shost);
 
        verify_user(auth_pw, prompt);
     }
-    if (status != TS_ERROR)
+    /* Only update timestamp if user was validated. */
+    if (ISSET(validated, VALIDATE_OK) && !ISSET(mode, MODE_INVALIDATE) && status != TS_ERROR)
        update_timestamp(timestampdir, timestampfile);
-    free(timestampdir);
-    if (timestampfile)
-       free(timestampfile);
+    efree(timestampdir);
+    efree(timestampfile);
 }
 
 /*
  * Standard sudo lecture.
- * TODO: allow the user to specify a file name instead.
  */
 static void
-lecture()
+lecture(status)
+    int status;
 {
+    FILE *fp;
+    char buf[BUFSIZ];
+    ssize_t nread;
+
+    if (def_lecture == never ||
+       (def_lecture == once && status != TS_MISSING && status != TS_ERROR))
+       return;
 
-    if (def_flag(I_LECTURE)) {
+    if (def_lecture_file && (fp = fopen(def_lecture_file, "r")) != NULL) {
+       while ((nread = fread(buf, sizeof(char), sizeof(buf), fp)) != 0)
+           fwrite(buf, nread, 1, stderr);
+       fclose(fp);
+    } else {
        (void) fputs("\n\
 We trust you have received the usual lecture from the local System\n\
-Administrator. It usually boils down to these two things:\n\
+Administrator. It usually boils down to these three things:\n\
 \n\
-       #1) Respect the privacy of others.\n\
-       #2) Think before you type.\n\n",
-       stderr);
+    #1) Respect the privacy of others.\n\
+    #2) Think before you type.\n\
+    #3) With great power comes great responsibility.\n\n",
+    stderr);
     }
 }
 
@@ -144,20 +210,32 @@ update_timestamp(timestampdir, timestampfile)
     char *timestampdir;
     char *timestampfile;
 {
+    /* If using tty timestamps but we have no tty there is nothing to do. */
+    if (def_tty_tickets && !user_ttypath)
+       return;
 
-    if (touch(timestampfile ? timestampfile : timestampdir, time(NULL)) == -1) {
-       if (timestampfile) {
-           int fd = open(timestampfile, O_WRONLY|O_CREAT|O_TRUNC, 0600);
-
-           if (fd == -1)
-               log_error(NO_EXIT|USE_ERRNO, "Can't open %s", timestampfile);
-           else
-               close(fd);
-       } else {
+    if (timestamp_uid != 0)
+       set_perms(PERM_TIMESTAMP);
+    if (timestampfile) {
+       /*
+        * Store tty info in timestamp file
+        */
+       int fd = open(timestampfile, O_WRONLY|O_CREAT, 0600);
+       if (fd == -1)
+           log_error(NO_EXIT|USE_ERRNO, "Can't open %s", timestampfile);
+       else {
+           lock_file(fd, SUDO_LOCK);
+           write(fd, &tty_info, sizeof(tty_info));
+           close(fd);
+       }
+    } else {
+       if (touch(-1, timestampdir, NULL) == -1) {
            if (mkdir(timestampdir, 0700) == -1)
                log_error(NO_EXIT|USE_ERRNO, "Can't mkdir %s", timestampdir);
        }
     }
+    if (timestamp_uid != 0)
+       set_perms(PERM_ROOT);
 }
 
 /*
@@ -170,57 +248,124 @@ expand_prompt(old_prompt, user, host)
     char *user;
     char *host;
 {
-    size_t len;
+    size_t len, n;
     int subst;
-    char *p, *np, *new_prompt, lastchar;
+    char *p, *np, *new_prompt, *endp;
 
     /* How much space do we need to malloc for the prompt? */
     subst = 0;
-    for (p = old_prompt, len = strlen(old_prompt), lastchar = '\0'; *p; p++) {
-       if (lastchar == '%') {
-           if (*p == 'h') {
-               len += strlen(user_shost) - 2;
-               subst = 1;
-           } else if (*p == 'u') {
-               len += strlen(user_name) - 2;
-               subst = 1;
+    for (p = old_prompt, len = strlen(old_prompt); *p; p++) {
+       if (p[0] =='%') {
+           switch (p[1]) {
+               case 'h':
+                   p++;
+                   len += strlen(user_shost) - 2;
+                   subst = 1;
+                   break;
+               case 'H':
+                   p++;
+                   len += strlen(user_host) - 2;
+                   subst = 1;
+                   break;
+               case 'p':
+                   p++;
+                   if (def_rootpw)
+                           len += 2;
+                   else if (def_targetpw || def_runaspw)
+                           len += strlen(runas_pw->pw_name) - 2;
+                   else
+                           len += strlen(user_name) - 2;
+                   subst = 1;
+                   break;
+               case 'u':
+                   p++;
+                   len += strlen(user_name) - 2;
+                   subst = 1;
+                   break;
+               case 'U':
+                   p++;
+                   len += strlen(runas_pw->pw_name) - 2;
+                   subst = 1;
+                   break;
+               case '%':
+                   p++;
+                   len--;
+                   subst = 1;
+                   break;
+               default:
+                   break;
            }
        }
-
-       if (lastchar == '%' && *p == '%') {
-           lastchar = '\0';
-           len--;
-       } else
-           lastchar = *p;
     }
 
     if (subst) {
-       new_prompt = (char *) emalloc(len + 1);
-       for (p = old_prompt, np = new_prompt, lastchar = '\0'; *p; p++) {
-           if (lastchar == '%' && (*p == 'h' || *p == 'u' || *p == '%')) {
-               /* substitute user/host name */
-               if (*p == 'h') {
-                   np--;
-                   strcpy(np, user_shost);
-                   np += strlen(user_shost);
-               } else if (*p == 'u') {
-                   np--;
-                   strcpy(np, user_name);
-                   np += strlen(user_name);
+       new_prompt = (char *) emalloc(++len);
+       endp = new_prompt + len;
+       for (p = old_prompt, np = new_prompt; *p; p++) {
+           if (p[0] =='%') {
+               switch (p[1]) {
+                   case 'h':
+                       p++;
+                       n = strlcpy(np, user_shost, np - endp);
+                       if (n >= np - endp)
+                           goto oflow;
+                       np += n;
+                       continue;
+                   case 'H':
+                       p++;
+                       n = strlcpy(np, user_host, np - endp);
+                       if (n >= np - endp)
+                           goto oflow;
+                       np += n;
+                       continue;
+                   case 'p':
+                       p++;
+                       if (def_rootpw)
+                               n = strlcpy(np, "root", np - endp);
+                       else if (def_targetpw || def_runaspw)
+                               n = strlcpy(np, runas_pw->pw_name, np - endp);
+                       else
+                               n = strlcpy(np, user_name, np - endp);
+                       if (n >= np - endp)
+                               goto oflow;
+                       np += n;
+                       continue;
+                   case 'u':
+                       p++;
+                       n = strlcpy(np, user_name, np - endp);
+                       if (n >= np - endp)
+                           goto oflow;
+                       np += n;
+                       continue;
+                   case 'U':
+                       p++;
+                       n = strlcpy(np,  runas_pw->pw_name, np - endp);
+                       if (n >= np - endp)
+                           goto oflow;
+                       np += n;
+                       continue;
+                   case '%':
+                       /* convert %% -> % */
+                       p++;
+                       break;
+                   default:
+                       /* no conversion */
+                       break;
                }
-           } else
-               *np++ = *p;
-
-           if (lastchar == '%' && *p == '%')
-               lastchar = '\0';
-           else
-               lastchar = *p;
+           }
+           *np++ = *p;
+           if (np >= endp)
+               goto oflow;
        }
        *np = '\0';
     } else
        new_prompt = old_prompt;
 
     return(new_prompt);
+
+oflow:
+    /* We pre-allocate enough space, so this should never happen. */
+    errorx(1, "internal error, expand_prompt() overflow");
 }
 
 /*
@@ -229,24 +374,9 @@ expand_prompt(old_prompt, user, host)
 int
 user_is_exempt()
 {
-    struct group *grp;
-    char **gr_mem;
-
-    if (!def_str(I_EXEMPT_GROUP))
-       return(FALSE);
-
-    if (!(grp = getgrnam(def_str(I_EXEMPT_GROUP))))
+    if (!def_exempt_group)
        return(FALSE);
-
-    if (user_gid == grp->gr_gid)
-       return(TRUE);
-
-    for (gr_mem = grp->gr_mem; *gr_mem; gr_mem++) {
-       if (strcmp(user_name, *gr_mem) == 0)
-           return(TRUE);
-    }
-
-    return(FALSE);
+    return(user_in_group(sudo_user.pw, def_exempt_group));
 }
 
 /*
@@ -260,34 +390,34 @@ build_timestamp(timestampdir, timestampfile)
     char *dirparent;
     int len;
 
-    dirparent = def_str(I_TIMESTAMPDIR);
+    dirparent = def_timestampdir;
     len = easprintf(timestampdir, "%s/%s", dirparent, user_name);
-    if (len >= MAXPATHLEN)
-       log_error(0, "timestamp path too long: %s", timestampdir);
+    if (len >= PATH_MAX)
+       log_error(0, "timestamp path too long: %s", *timestampdir);
 
     /*
      * Timestamp file may be a file in the directory or NUL to use
      * the directory as the timestamp.
      */
-    if (def_flag(I_TTY_TICKETS)) {
+    if (def_tty_tickets) {
        char *p;
 
        if ((p = strrchr(user_tty, '/')))
            p++;
        else
            p = user_tty;
-       if (def_flag(I_TARGETPW))
+       if (def_targetpw)
            len = easprintf(timestampfile, "%s/%s/%s:%s", dirparent, user_name,
-               p, *user_runas);
+               p, runas_pw->pw_name);
        else
            len = easprintf(timestampfile, "%s/%s/%s", dirparent, user_name, p);
-       if (len >= MAXPATHLEN)
-           log_error(0, "timestamp path too long: %s", timestampfile);
-    } else if (def_flag(I_TARGETPW)) {
+       if (len >= PATH_MAX)
+           log_error(0, "timestamp path too long: %s", *timestampfile);
+    } else if (def_targetpw) {
        len = easprintf(timestampfile, "%s/%s/%s", dirparent, user_name,
-           *user_runas);
-       if (len >= MAXPATHLEN)
-           log_error(0, "timestamp path too long: %s", timestampfile);
+           runas_pw->pw_name);
+       if (len >= PATH_MAX)
+           log_error(0, "timestamp path too long: %s", *timestampfile);
     } else
        *timestampfile = NULL;
 }
@@ -296,17 +426,21 @@ build_timestamp(timestampdir, timestampfile)
  * Check the timestamp file and directory and return their status.
  */
 static int
-timestamp_status(timestampdir, timestampfile, user, make_dirs)
+timestamp_status(timestampdir, timestampfile, user, flags)
     char *timestampdir;
     char *timestampfile;
     char *user;
-    int make_dirs;
+    int flags;
 {
     struct stat sb;
+    struct timeval boottime, mtime;
     time_t now;
-    char *dirparent = def_str(I_TIMESTAMPDIR);
+    char *dirparent = def_timestampdir;
     int status = TS_ERROR;             /* assume the worst */
 
+    if (timestamp_uid != 0)
+       set_perms(PERM_TIMESTAMP);
+
     /*
      * Sanity check dirparent and make it if it doesn't already exist.
      * We start out assuming the worst (that the dir is not sane) and
@@ -317,14 +451,15 @@ timestamp_status(timestampdir, timestampfile, user, make_dirs)
     if (lstat(dirparent, &sb) == 0) {
        if (!S_ISDIR(sb.st_mode))
            log_error(NO_EXIT, "%s exists but is not a directory (0%o)",
-               dirparent, sb.st_mode);
-       else if (sb.st_uid != 0)
-           log_error(NO_EXIT, "%s owned by uid %ld, should be owned by root",
-               dirparent, (long) sb.st_uid);
+               dirparent, (unsigned int) sb.st_mode);
+       else if (sb.st_uid != timestamp_uid)
+           log_error(NO_EXIT, "%s owned by uid %lu, should be uid %lu",
+               dirparent, (unsigned long) sb.st_uid,
+               (unsigned long) timestamp_uid);
        else if ((sb.st_mode & 0000022))
            log_error(NO_EXIT,
                "%s writable by non-owner (0%o), should be mode 0700",
-               dirparent, sb.st_mode);
+               dirparent, (unsigned int) sb.st_mode);
        else {
            if ((sb.st_mode & 0000777) != 0700)
                (void) chmod(dirparent, 0700);
@@ -334,7 +469,7 @@ timestamp_status(timestampdir, timestampfile, user, make_dirs)
        log_error(NO_EXIT|USE_ERRNO, "can't stat %s", dirparent);
     } else {
        /* No dirparent, try to make one. */
-       if (make_dirs) {
+       if (ISSET(flags, TS_MAKE_DIRS)) {
            if (mkdir(dirparent, S_IRWXU))
                log_error(NO_EXIT|USE_ERRNO, "can't mkdir %s",
                    dirparent);
@@ -342,8 +477,11 @@ timestamp_status(timestampdir, timestampfile, user, make_dirs)
                status = TS_MISSING;
        }
     }
-    if (status == TS_ERROR)
+    if (status == TS_ERROR) {
+       if (timestamp_uid != 0)
+           set_perms(PERM_ROOT);
        return(status);
+    }
 
     /*
      * Sanity check the user's ticket dir.  We start by downgrading
@@ -360,14 +498,15 @@ timestamp_status(timestampdir, timestampfile, user, make_dirs)
                    status = TS_MISSING;
            } else
                log_error(NO_EXIT, "%s exists but is not a directory (0%o)",
-                   timestampdir, sb.st_mode);
-       } else if (sb.st_uid != 0)
-           log_error(NO_EXIT, "%s owned by uid %ld, should be owned by root",
-               timestampdir, (long) sb.st_uid);
+                   timestampdir, (unsigned int) sb.st_mode);
+       } else if (sb.st_uid != timestamp_uid)
+           log_error(NO_EXIT, "%s owned by uid %lu, should be uid %lu",
+               timestampdir, (unsigned long) sb.st_uid,
+               (unsigned long) timestamp_uid);
        else if ((sb.st_mode & 0000022))
            log_error(NO_EXIT,
                "%s writable by non-owner (0%o), should be mode 0700",
-               timestampdir, sb.st_mode);
+               timestampdir, (unsigned int) sb.st_mode);
        else {
            if ((sb.st_mode & 0000777) != 0700)
                (void) chmod(timestampdir, 0700);
@@ -380,9 +519,9 @@ timestamp_status(timestampdir, timestampfile, user, make_dirs)
 
     /*
      * If there is no user ticket dir, AND we are in tty ticket mode,
-     * AND the make_dirs flag is set, create the user ticket dir.
+     * AND the TS_MAKE_DIRS flag is set, create the user ticket dir.
      */
-    if (status == TS_MISSING && timestampfile && make_dirs) {
+    if (status == TS_MISSING && timestampfile && ISSET(flags, TS_MAKE_DIRS)) {
        if (mkdir(timestampdir, S_IRWXU) == -1) {
            status = TS_ERROR;
            log_error(NO_EXIT|USE_ERRNO, "can't mkdir %s", timestampdir);
@@ -395,29 +534,50 @@ timestamp_status(timestampdir, timestampfile, user, make_dirs)
     if (timestampfile && status != TS_ERROR) {
        if (status != TS_MISSING)
            status = TS_NOFILE;                 /* dir there, file missing */
+       if (def_tty_tickets && !user_ttypath)
+           goto done;                          /* no tty, always prompt */
        if (lstat(timestampfile, &sb) == 0) {
            if (!S_ISREG(sb.st_mode)) {
                status = TS_ERROR;
                log_error(NO_EXIT, "%s exists but is not a regular file (0%o)",
-                   timestampfile, sb.st_mode);
+                   timestampfile, (unsigned int) sb.st_mode);
            } else {
                /* If bad uid or file mode, complain and kill the bogus file. */
-               if (sb.st_uid != 0) {
+               if (sb.st_uid != timestamp_uid) {
                    log_error(NO_EXIT,
-                       "%s owned by uid %ld, should be owned by root",
-                       timestampfile, (long) sb.st_uid);
+                       "%s owned by uid %lu, should be uid %lu",
+                       timestampfile, (unsigned long) sb.st_uid,
+                       (unsigned long) timestamp_uid);
                    (void) unlink(timestampfile);
                } else if ((sb.st_mode & 0000022)) {
                    log_error(NO_EXIT,
                        "%s writable by non-owner (0%o), should be mode 0600",
-                       timestampfile, sb.st_mode);
+                       timestampfile, (unsigned int) sb.st_mode);
                    (void) unlink(timestampfile);
                } else {
                    /* If not mode 0600, fix it. */
                    if ((sb.st_mode & 0000777) != 0600)
                        (void) chmod(timestampfile, 0600);
 
-                   status = TS_OLD;    /* actually check mtime below */
+                   /*
+                    * Check for stored tty info.  If the file is zero-sized
+                    * it is an old-style timestamp with no tty info in it.
+                    * If removing, we don't care about the contents.
+                    * The actual mtime check is done later.
+                    */
+                   if (ISSET(flags, TS_REMOVE)) {
+                       status = TS_OLD;
+                   } else if (sb.st_size != 0) {
+                       struct tty_info info;
+                       int fd = open(timestampfile, O_RDONLY, 0644);
+                       if (fd != -1) {
+                           if (read(fd, &info, sizeof(info)) == sizeof(info) &&
+                               memcmp(&info, &tty_info, sizeof(info)) == 0) {
+                               status = TS_OLD;
+                           }
+                           close(fd);
+                       }
+                   }
                }
            }
        } else if (errno != ENOENT) {
@@ -427,35 +587,43 @@ timestamp_status(timestampdir, timestampfile, user, make_dirs)
     }
 
     /*
-     * If the file/dir exists, check its mtime.
+     * If the file/dir exists and we are not removing it, check its mtime.
      */
-    if (status == TS_OLD) {
+    if (status == TS_OLD && !ISSET(flags, TS_REMOVE)) {
+       mtim_get(&sb, &mtime);
        /* Negative timeouts only expire manually (sudo -k). */
-       if (def_ival(I_TIMESTAMP_TIMEOUT) < 0 && sb.st_mtime != 0)
+       if (def_timestamp_timeout < 0 && mtime.tv_sec != 0)
            status = TS_CURRENT;
        else {
            now = time(NULL);
-           if (def_ival(I_TIMESTAMP_TIMEOUT) && 
-               now - sb.st_mtime < 60 * def_ival(I_TIMESTAMP_TIMEOUT)) {
+           if (def_timestamp_timeout &&
+               now - mtime.tv_sec < 60 * def_timestamp_timeout) {
                /*
                 * Check for bogus time on the stampfile.  The clock may
                 * have been set back or someone could be trying to spoof us.
                 */
-               if (sb.st_mtime > now + 60 * def_ival(I_TIMESTAMP_TIMEOUT) * 2) {
+               if (mtime.tv_sec > now + 60 * def_timestamp_timeout * 2) {
+                   time_t tv_sec = (time_t)mtime.tv_sec;
                    log_error(NO_EXIT,
                        "timestamp too far in the future: %20.20s",
-                       4 + ctime(&sb.st_mtime));
+                       4 + ctime(&tv_sec));
                    if (timestampfile)
                        (void) unlink(timestampfile);
                    else
                        (void) rmdir(timestampdir);
                    status = TS_MISSING;
-               } else
+               } else if (get_boottime(&boottime) && timevalcmp(&mtime, &boottime, <)) {
+                   status = TS_OLD;
+               } else {
                    status = TS_CURRENT;
+               }
            }
        }
     }
 
+done:
+    if (timestamp_uid != 0)
+       set_perms(PERM_ROOT);
     return(status);
 }
 
@@ -466,33 +634,67 @@ void
 remove_timestamp(remove)
     int remove;
 {
-    char *timestampdir;
-    char *timestampfile;
-    char *ts;
+    struct timeval tv;
+    char *timestampdir, *timestampfile, *path;
     int status;
 
     build_timestamp(&timestampdir, &timestampfile);
-    status = timestamp_status(timestampdir, timestampfile, user_name, FALSE);
+    status = timestamp_status(timestampdir, timestampfile, user_name,
+       TS_REMOVE);
     if (status == TS_OLD || status == TS_CURRENT) {
-       ts = timestampfile ? timestampfile : timestampdir;
+       path = timestampfile ? timestampfile : timestampdir;
        if (remove) {
            if (timestampfile)
                status = unlink(timestampfile);
            else
                status = rmdir(timestampdir);
            if (status == -1 && errno != ENOENT) {
-               log_error(NO_EXIT, "can't remove %s (%s), will reset to epoch",
-                   ts, strerror(errno));
+               log_error(NO_EXIT, "can't remove %s (%s), will reset to Epoch",
+                   path, strerror(errno));
                remove = FALSE;
            }
+       } else {
+           timevalclear(&tv);
+           if (touch(-1, path, &tv) == -1 && errno != ENOENT)
+               error(1, "can't reset %s to Epoch", path);
        }
-       if (!remove && touch(ts, 0) == -1) {
-           (void) fprintf(stderr, "%s: can't reset %s to epoch: %s\n",
-               Argv[0], ts, strerror(errno));
-       }
     }
 
-    free(timestampdir);
-    if (timestampfile)
-       free(timestampfile);
+    efree(timestampdir);
+    efree(timestampfile);
+}
+
+/*
+ * Returns TRUE if tty lives on a devpts or /devices filesystem, else FALSE.
+ * Unlike most filesystems, the ctime of devpts nodes is not updated when
+ * the device node is written to, only when the inode's status changes,
+ * typically via the chmod, chown, link, rename, or utimes system calls.
+ * Since the ctime is "stable" in this case, we can stash it the tty ticket
+ * file and use it to determine whether the tty ticket file is stale.
+ */
+static int
+tty_is_devpts(tty)
+    const char *tty;
+{
+    int retval = FALSE;
+#ifdef __linux__
+    struct statfs sfs;
+
+#ifndef DEVPTS_SUPER_MAGIC
+# define DEVPTS_SUPER_MAGIC 0x1cd1
+#endif
+
+    if (statfs(tty, &sfs) == 0) {
+       if (sfs.f_type == DEVPTS_SUPER_MAGIC)
+           retval = TRUE;
+    }
+#elif defined(__sun) && defined(__SVR4)
+    struct statvfs sfs;
+
+    if (statvfs(tty, &sfs) == 0) {
+       if (strcmp(sfs.f_fstr, "devices") == 0)
+           retval = TRUE;
+    }
+#endif /* __linux__ */
+    return retval;
 }