Imported Upstream version 1.8.6p8
[debian/sudo] / plugins / sudoers / check.c
1 /*
2  * Copyright (c) 1993-1996,1998-2005, 2007-2011
3  *      Todd C. Miller <Todd.Miller@courtesan.com>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  *
17  * Sponsored in part by the Defense Advanced Research Projects
18  * Agency (DARPA) and Air Force Research Laboratory, Air Force
19  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
20  */
21
22 #include <config.h>
23
24 #include <sys/types.h>
25 #include <sys/param.h>
26 #include <sys/time.h>
27 #include <sys/stat.h>
28 #ifdef __linux__
29 # include <sys/vfs.h>
30 #endif
31 #if defined(__sun) && defined(__SVR4)
32 # include <sys/statvfs.h>
33 #endif
34 #ifndef __TANDEM
35 # include <sys/file.h>
36 #endif
37 #include <stdio.h>
38 #ifdef STDC_HEADERS
39 # include <stdlib.h>
40 # include <stddef.h>
41 #else
42 # ifdef HAVE_STDLIB_H
43 #  include <stdlib.h>
44 # endif
45 #endif /* STDC_HEADERS */
46 #ifdef HAVE_STRING_H
47 # include <string.h>
48 #endif /* HAVE_STRING_H */
49 #ifdef HAVE_STRINGS_H
50 # include <strings.h>
51 #endif /* HAVE_STRINGS_H */
52 #ifdef HAVE_UNISTD_H
53 # include <unistd.h>
54 #endif /* HAVE_UNISTD_H */
55 #if TIME_WITH_SYS_TIME
56 # include <time.h>
57 #endif
58 #include <errno.h>
59 #include <fcntl.h>
60 #include <signal.h>
61 #include <pwd.h>
62 #include <grp.h>
63
64 #include "sudoers.h"
65
66 /* Status codes for timestamp_status() */
67 #define TS_CURRENT              0
68 #define TS_OLD                  1
69 #define TS_MISSING              2
70 #define TS_NOFILE               3
71 #define TS_ERROR                4
72
73 /* Flags for timestamp_status() */
74 #define TS_MAKE_DIRS            1
75 #define TS_REMOVE               2
76
77 /*
78  * Info stored in tty ticket from stat(2) to help with tty matching.
79  */
80 static struct tty_info {
81     dev_t dev;                  /* ID of device tty resides on */
82     dev_t rdev;                 /* tty device ID */
83     ino_t ino;                  /* tty inode number */
84     struct timeval ctime;       /* tty inode change time */
85     pid_t sid;                  /* ID of session with controlling tty */
86 } tty_info;
87
88 static int   build_timestamp(char **, char **);
89 static int   timestamp_status(char *, char *, char *, int);
90 static char *expand_prompt(char *, char *, char *);
91 static void  lecture(int);
92 static void  update_timestamp(char *, char *);
93 static bool  tty_is_devpts(const char *);
94 static struct passwd *get_authpw(void);
95
96 /*
97  * Returns true if the user successfully authenticates, false if not
98  * or -1 on error.
99  */
100 int
101 check_user(int validated, int mode)
102 {
103     struct passwd *auth_pw;
104     char *timestampdir = NULL;
105     char *timestampfile = NULL;
106     char *prompt;
107     struct stat sb;
108     int status, rval = true;
109     debug_decl(check_user, SUDO_DEBUG_AUTH)
110
111     /*
112      * Init authentication system regardless of whether we need a password.
113      * Required for proper PAM session support.
114      */
115     auth_pw = get_authpw();
116     if (sudo_auth_init(auth_pw) == -1) {
117         rval = -1;
118         goto done;
119     }
120
121     /*
122      * Don't prompt for the root passwd or if the user is exempt.
123      * If the user is not changing uid/gid, no need for a password.
124      */
125     if (!def_authenticate || user_uid == 0 || user_is_exempt())
126         goto done;
127     if (user_uid == runas_pw->pw_uid &&
128         (!runas_gr || user_in_group(sudo_user.pw, runas_gr->gr_name))) {
129 #ifdef HAVE_SELINUX
130         if (user_role == NULL && user_type == NULL)
131 #endif
132 #ifdef HAVE_PRIV_SET
133         if (runas_privs == NULL && runas_limitprivs == NULL)
134 #endif
135             goto done;
136     }
137
138     /* Always need a password when -k was specified with the command. */
139     if (ISSET(mode, MODE_IGNORE_TICKET))
140         SET(validated, FLAG_CHECK_USER);
141
142     /* Stash the tty's device, session ID and ctime for ticket comparison. */
143     if (def_tty_tickets && user_ttypath && stat(user_ttypath, &sb) == 0) {
144         tty_info.dev = sb.st_dev;
145         tty_info.ino = sb.st_ino;
146         tty_info.rdev = sb.st_rdev;
147         if (tty_is_devpts(user_ttypath))
148             ctim_get(&sb, &tty_info.ctime);
149         tty_info.sid = user_sid;
150     }
151
152     if (build_timestamp(&timestampdir, &timestampfile) == -1) {
153         rval = -1;
154         goto done;
155     }
156
157     status = timestamp_status(timestampdir, timestampfile, user_name,
158         TS_MAKE_DIRS);
159
160     if (status != TS_CURRENT || ISSET(validated, FLAG_CHECK_USER)) {
161         /* Bail out if we are non-interactive and a password is required */
162         if (ISSET(mode, MODE_NONINTERACTIVE)) {
163             validated |= FLAG_NON_INTERACTIVE;
164             log_auth_failure(validated, 0);
165             rval = -1;
166             goto done;
167         }
168
169         /* XXX - should not lecture if askpass helper is being used. */
170         lecture(status);
171
172         /* Expand any escapes in the prompt. */
173         prompt = expand_prompt(user_prompt ? user_prompt : def_passprompt,
174             user_name, user_shost);
175
176         rval = verify_user(auth_pw, prompt, validated);
177     }
178     /* Only update timestamp if user was validated. */
179     if (rval == true && ISSET(validated, VALIDATE_OK) &&
180         !ISSET(mode, MODE_IGNORE_TICKET) && status != TS_ERROR)
181         update_timestamp(timestampdir, timestampfile);
182     efree(timestampdir);
183     efree(timestampfile);
184
185 done:
186     sudo_auth_cleanup(auth_pw);
187     sudo_pw_delref(auth_pw);
188
189     debug_return_bool(rval);
190 }
191
192 #define DEFAULT_LECTURE "\n" \
193     "We trust you have received the usual lecture from the local System\n" \
194     "Administrator. It usually boils down to these three things:\n\n" \
195     "    #1) Respect the privacy of others.\n" \
196     "    #2) Think before you type.\n" \
197     "    #3) With great power comes great responsibility.\n\n"
198
199 /*
200  * Standard sudo lecture.
201  */
202 static void
203 lecture(int status)
204 {
205     FILE *fp;
206     char buf[BUFSIZ];
207     ssize_t nread;
208     struct sudo_conv_message msg;
209     struct sudo_conv_reply repl;
210     debug_decl(lecture, SUDO_DEBUG_AUTH)
211
212     if (def_lecture == never ||
213         (def_lecture == once && status != TS_MISSING && status != TS_ERROR))
214         debug_return;
215
216     memset(&msg, 0, sizeof(msg));
217     memset(&repl, 0, sizeof(repl));
218
219     if (def_lecture_file && (fp = fopen(def_lecture_file, "r")) != NULL) {
220         while ((nread = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) != 0) {
221             buf[nread] = '\0';
222             msg.msg_type = SUDO_CONV_ERROR_MSG;
223             msg.msg = buf;
224             sudo_conv(1, &msg, &repl);
225         }
226         fclose(fp);
227     } else {
228         msg.msg_type = SUDO_CONV_ERROR_MSG;
229         msg.msg = _(DEFAULT_LECTURE);
230         sudo_conv(1, &msg, &repl);
231     }
232     debug_return;
233 }
234
235 /*
236  * Update the time on the timestamp file/dir or create it if necessary.
237  */
238 static void
239 update_timestamp(char *timestampdir, char *timestampfile)
240 {
241     debug_decl(update_timestamp, SUDO_DEBUG_AUTH)
242
243     /* If using tty timestamps but we have no tty there is nothing to do. */
244     if (def_tty_tickets && !user_ttypath)
245         debug_return;
246
247     if (timestamp_uid != 0)
248         set_perms(PERM_TIMESTAMP);
249     if (timestampfile) {
250         /*
251          * Store tty info in timestamp file
252          */
253         int fd = open(timestampfile, O_WRONLY|O_CREAT, 0600);
254         if (fd == -1)
255             log_error(USE_ERRNO, _("unable to open %s"), timestampfile);
256         else {
257             lock_file(fd, SUDO_LOCK);
258             if (write(fd, &tty_info, sizeof(tty_info)) != sizeof(tty_info)) {
259                 log_error(USE_ERRNO, _("unable to write to %s"),
260                     timestampfile);
261             }
262             close(fd);
263         }
264     } else {
265         if (touch(-1, timestampdir, NULL) == -1) {
266             if (mkdir(timestampdir, 0700) == -1) {
267                 log_error(USE_ERRNO, _("unable to mkdir %s"),
268                     timestampdir);
269             }
270         }
271     }
272     if (timestamp_uid != 0)
273         restore_perms();
274     debug_return;
275 }
276
277 /*
278  * Expand %h and %u escapes in the prompt and pass back the dynamically
279  * allocated result.  Returns the same string if there are no escapes.
280  */
281 static char *
282 expand_prompt(char *old_prompt, char *user, char *host)
283 {
284     size_t len, n;
285     int subst;
286     char *p, *np, *new_prompt, *endp;
287     debug_decl(expand_prompt, SUDO_DEBUG_AUTH)
288
289     /* How much space do we need to malloc for the prompt? */
290     subst = 0;
291     for (p = old_prompt, len = strlen(old_prompt); *p; p++) {
292         if (p[0] =='%') {
293             switch (p[1]) {
294                 case 'h':
295                     p++;
296                     len += strlen(user_shost) - 2;
297                     subst = 1;
298                     break;
299                 case 'H':
300                     p++;
301                     len += strlen(user_host) - 2;
302                     subst = 1;
303                     break;
304                 case 'p':
305                     p++;
306                     if (def_rootpw)
307                             len += 2;
308                     else if (def_targetpw || def_runaspw)
309                             len += strlen(runas_pw->pw_name) - 2;
310                     else
311                             len += strlen(user_name) - 2;
312                     subst = 1;
313                     break;
314                 case 'u':
315                     p++;
316                     len += strlen(user_name) - 2;
317                     subst = 1;
318                     break;
319                 case 'U':
320                     p++;
321                     len += strlen(runas_pw->pw_name) - 2;
322                     subst = 1;
323                     break;
324                 case '%':
325                     p++;
326                     len--;
327                     subst = 1;
328                     break;
329                 default:
330                     break;
331             }
332         }
333     }
334
335     if (subst) {
336         new_prompt = emalloc(++len);
337         endp = new_prompt + len;
338         for (p = old_prompt, np = new_prompt; *p; p++) {
339             if (p[0] =='%') {
340                 switch (p[1]) {
341                     case 'h':
342                         p++;
343                         n = strlcpy(np, user_shost, np - endp);
344                         if (n >= np - endp)
345                             goto oflow;
346                         np += n;
347                         continue;
348                     case 'H':
349                         p++;
350                         n = strlcpy(np, user_host, np - endp);
351                         if (n >= np - endp)
352                             goto oflow;
353                         np += n;
354                         continue;
355                     case 'p':
356                         p++;
357                         if (def_rootpw)
358                                 n = strlcpy(np, "root", np - endp);
359                         else if (def_targetpw || def_runaspw)
360                                 n = strlcpy(np, runas_pw->pw_name, np - endp);
361                         else
362                                 n = strlcpy(np, user_name, np - endp);
363                         if (n >= np - endp)
364                                 goto oflow;
365                         np += n;
366                         continue;
367                     case 'u':
368                         p++;
369                         n = strlcpy(np, user_name, np - endp);
370                         if (n >= np - endp)
371                             goto oflow;
372                         np += n;
373                         continue;
374                     case 'U':
375                         p++;
376                         n = strlcpy(np,  runas_pw->pw_name, np - endp);
377                         if (n >= np - endp)
378                             goto oflow;
379                         np += n;
380                         continue;
381                     case '%':
382                         /* convert %% -> % */
383                         p++;
384                         break;
385                     default:
386                         /* no conversion */
387                         break;
388                 }
389             }
390             *np++ = *p;
391             if (np >= endp)
392                 goto oflow;
393         }
394         *np = '\0';
395     } else
396         new_prompt = old_prompt;
397
398     debug_return_str(new_prompt);
399
400 oflow:
401     /* We pre-allocate enough space, so this should never happen. */
402     errorx(1, _("internal error, %s overflow"), "expand_prompt()");
403 }
404
405 /*
406  * Checks if the user is exempt from supplying a password.
407  */
408 bool
409 user_is_exempt(void)
410 {
411     bool rval = false;
412     debug_decl(user_is_exempt, SUDO_DEBUG_AUTH)
413
414     if (def_exempt_group)
415         rval = user_in_group(sudo_user.pw, def_exempt_group);
416     debug_return_bool(rval);
417 }
418
419 /*
420  * Fills in timestampdir as well as timestampfile if using tty tickets.
421  */
422 static int
423 build_timestamp(char **timestampdir, char **timestampfile)
424 {
425     char *dirparent;
426     int len;
427     debug_decl(build_timestamp, SUDO_DEBUG_AUTH)
428
429     dirparent = def_timestampdir;
430     *timestampfile = NULL;
431     len = easprintf(timestampdir, "%s/%s", dirparent, user_name);
432     if (len >= PATH_MAX)
433         goto bad;
434
435     /*
436      * Timestamp file may be a file in the directory or NUL to use
437      * the directory as the timestamp.
438      */
439     if (def_tty_tickets) {
440         char *p;
441
442         if ((p = strrchr(user_tty, '/')))
443             p++;
444         else
445             p = user_tty;
446         if (def_targetpw)
447             len = easprintf(timestampfile, "%s/%s/%s:%s", dirparent, user_name,
448                 p, runas_pw->pw_name);
449         else
450             len = easprintf(timestampfile, "%s/%s/%s", dirparent, user_name, p);
451         if (len >= PATH_MAX)
452             goto bad;
453     } else if (def_targetpw) {
454         len = easprintf(timestampfile, "%s/%s/%s", dirparent, user_name,
455             runas_pw->pw_name);
456         if (len >= PATH_MAX)
457             goto bad;
458     } else
459         *timestampfile = NULL;
460
461     debug_return_int(len);
462 bad:
463     log_fatal(0, _("timestamp path too long: %s"),
464         *timestampfile ? *timestampfile : *timestampdir);
465     /* NOTREACHED */
466     debug_return_int(-1);
467 }
468
469 /*
470  * Check the timestamp file and directory and return their status.
471  */
472 static int
473 timestamp_status(char *timestampdir, char *timestampfile, char *user, int flags)
474 {
475     struct stat sb;
476     struct timeval boottime, mtime;
477     time_t now;
478     char *dirparent = def_timestampdir;
479     int status = TS_ERROR;              /* assume the worst */
480     debug_decl(timestamp_status, SUDO_DEBUG_AUTH)
481
482     if (timestamp_uid != 0)
483         set_perms(PERM_TIMESTAMP);
484
485     /*
486      * Sanity check dirparent and make it if it doesn't already exist.
487      * We start out assuming the worst (that the dir is not sane) and
488      * if it is ok upgrade the status to ``no timestamp file''.
489      * Note that we don't check the parent(s) of dirparent for
490      * sanity since the sudo dir is often just located in /tmp.
491      */
492     if (lstat(dirparent, &sb) == 0) {
493         if (!S_ISDIR(sb.st_mode))
494             log_error(0, _("%s exists but is not a directory (0%o)"),
495                 dirparent, (unsigned int) sb.st_mode);
496         else if (sb.st_uid != timestamp_uid)
497             log_error(0, _("%s owned by uid %u, should be uid %u"),
498                 dirparent, (unsigned int) sb.st_uid,
499                 (unsigned int) timestamp_uid);
500         else if ((sb.st_mode & 0000022))
501             log_error(0,
502                 _("%s writable by non-owner (0%o), should be mode 0700"),
503                 dirparent, (unsigned int) sb.st_mode);
504         else {
505             if ((sb.st_mode & 0000777) != 0700)
506                 (void) chmod(dirparent, 0700);
507             status = TS_MISSING;
508         }
509     } else if (errno != ENOENT) {
510         log_error(USE_ERRNO, _("unable to stat %s"), dirparent);
511     } else {
512         /* No dirparent, try to make one. */
513         if (ISSET(flags, TS_MAKE_DIRS)) {
514             if (mkdir(dirparent, S_IRWXU))
515                 log_error(USE_ERRNO, _("unable to mkdir %s"),
516                     dirparent);
517             else
518                 status = TS_MISSING;
519         }
520     }
521     if (status == TS_ERROR)
522         goto done;
523
524     /*
525      * Sanity check the user's ticket dir.  We start by downgrading
526      * the status to TS_ERROR.  If the ticket dir exists and is sane
527      * this will be upgraded to TS_OLD.  If the dir does not exist,
528      * it will be upgraded to TS_MISSING.
529      */
530     status = TS_ERROR;                  /* downgrade status again */
531     if (lstat(timestampdir, &sb) == 0) {
532         if (!S_ISDIR(sb.st_mode)) {
533             if (S_ISREG(sb.st_mode)) {
534                 /* convert from old style */
535                 if (unlink(timestampdir) == 0)
536                     status = TS_MISSING;
537             } else
538                 log_error(0, _("%s exists but is not a directory (0%o)"),
539                     timestampdir, (unsigned int) sb.st_mode);
540         } else if (sb.st_uid != timestamp_uid)
541             log_error(0, _("%s owned by uid %u, should be uid %u"),
542                 timestampdir, (unsigned int) sb.st_uid,
543                 (unsigned int) timestamp_uid);
544         else if ((sb.st_mode & 0000022))
545             log_error(0,
546                 _("%s writable by non-owner (0%o), should be mode 0700"),
547                 timestampdir, (unsigned int) sb.st_mode);
548         else {
549             if ((sb.st_mode & 0000777) != 0700)
550                 (void) chmod(timestampdir, 0700);
551             status = TS_OLD;            /* do date check later */
552         }
553     } else if (errno != ENOENT) {
554         log_error(USE_ERRNO, _("unable to stat %s"), timestampdir);
555     } else
556         status = TS_MISSING;
557
558     /*
559      * If there is no user ticket dir, AND we are in tty ticket mode,
560      * AND the TS_MAKE_DIRS flag is set, create the user ticket dir.
561      */
562     if (status == TS_MISSING && timestampfile && ISSET(flags, TS_MAKE_DIRS)) {
563         if (mkdir(timestampdir, S_IRWXU) == -1) {
564             status = TS_ERROR;
565             log_error(USE_ERRNO, _("unable to mkdir %s"), timestampdir);
566         }
567     }
568
569     /*
570      * Sanity check the tty ticket file if it exists.
571      */
572     if (timestampfile && status != TS_ERROR) {
573         if (status != TS_MISSING)
574             status = TS_NOFILE;                 /* dir there, file missing */
575         if (def_tty_tickets && !user_ttypath)
576             goto done;                          /* no tty, always prompt */
577         if (lstat(timestampfile, &sb) == 0) {
578             if (!S_ISREG(sb.st_mode)) {
579                 status = TS_ERROR;
580                 log_error(0, _("%s exists but is not a regular file (0%o)"),
581                     timestampfile, (unsigned int) sb.st_mode);
582             } else {
583                 /* If bad uid or file mode, complain and kill the bogus file. */
584                 if (sb.st_uid != timestamp_uid) {
585                     log_error(0,
586                         _("%s owned by uid %u, should be uid %u"),
587                         timestampfile, (unsigned int) sb.st_uid,
588                         (unsigned int) timestamp_uid);
589                     (void) unlink(timestampfile);
590                 } else if ((sb.st_mode & 0000022)) {
591                     log_error(0,
592                         _("%s writable by non-owner (0%o), should be mode 0600"),
593                         timestampfile, (unsigned int) sb.st_mode);
594                     (void) unlink(timestampfile);
595                 } else {
596                     /* If not mode 0600, fix it. */
597                     if ((sb.st_mode & 0000777) != 0600)
598                         (void) chmod(timestampfile, 0600);
599
600                     /*
601                      * Check for stored tty info.  If the file is zero-sized
602                      * it is an old-style timestamp with no tty info in it.
603                      * If removing, we don't care about the contents.
604                      * The actual mtime check is done later.
605                      */
606                     if (ISSET(flags, TS_REMOVE)) {
607                         status = TS_OLD;
608                     } else if (sb.st_size != 0) {
609                         struct tty_info info;
610                         int fd = open(timestampfile, O_RDONLY, 0644);
611                         if (fd != -1) {
612                             if (read(fd, &info, sizeof(info)) == sizeof(info) &&
613                                 memcmp(&info, &tty_info, sizeof(info)) == 0) {
614                                 status = TS_OLD;
615                             }
616                             close(fd);
617                         }
618                     }
619                 }
620             }
621         } else if (errno != ENOENT) {
622             log_error(USE_ERRNO, _("unable to stat %s"), timestampfile);
623             status = TS_ERROR;
624         }
625     }
626
627     /*
628      * If the file/dir exists and we are not removing it, check its mtime.
629      */
630     if (status == TS_OLD && !ISSET(flags, TS_REMOVE)) {
631         mtim_get(&sb, &mtime);
632         if (timevalisset(&mtime)) {
633             /* Negative timeouts only expire manually (sudo -k). */
634             if (def_timestamp_timeout < 0) {
635                 status = TS_CURRENT;
636             } else {
637                 now = time(NULL);
638                 if (def_timestamp_timeout &&
639                     now - mtime.tv_sec < 60 * def_timestamp_timeout) {
640                     /*
641                      * Check for bogus time on the stampfile.  The clock may
642                      * have been set back or user could be trying to spoof us.
643                      */
644                     if (mtime.tv_sec > now + 60 * def_timestamp_timeout * 2) {
645                         time_t tv_sec = (time_t)mtime.tv_sec;
646                         log_error(0,
647                             _("timestamp too far in the future: %20.20s"),
648                             4 + ctime(&tv_sec));
649                         if (timestampfile)
650                             (void) unlink(timestampfile);
651                         else
652                             (void) rmdir(timestampdir);
653                         status = TS_MISSING;
654                     } else if (get_boottime(&boottime) &&
655                         timevalcmp(&mtime, &boottime, <)) {
656                         status = TS_OLD;
657                     } else {
658                         status = TS_CURRENT;
659                     }
660                 }
661             }
662         }
663     }
664
665 done:
666     if (timestamp_uid != 0)
667         restore_perms();
668     debug_return_int(status);
669 }
670
671 /*
672  * Remove the timestamp ticket file/dir.
673  */
674 void
675 remove_timestamp(bool remove)
676 {
677     struct timeval tv;
678     char *timestampdir, *timestampfile, *path;
679     int status;
680     debug_decl(remove_timestamp, SUDO_DEBUG_AUTH)
681
682     if (build_timestamp(&timestampdir, &timestampfile) == -1)
683         debug_return;
684
685     status = timestamp_status(timestampdir, timestampfile, user_name,
686         TS_REMOVE);
687     if (status != TS_MISSING && status != TS_ERROR) {
688         path = timestampfile ? timestampfile : timestampdir;
689         if (remove) {
690             if (timestampfile)
691                 status = unlink(timestampfile);
692             else
693                 status = rmdir(timestampdir);
694             if (status == -1 && errno != ENOENT) {
695                 log_error(0,
696                     _("unable to remove %s (%s), will reset to the epoch"),
697                     path, strerror(errno));
698                 remove = false;
699             }
700         }
701         if (!remove) {
702             timevalclear(&tv);
703             if (touch(-1, path, &tv) == -1 && errno != ENOENT)
704                 error(1, _("unable to reset %s to the epoch"), path);
705         }
706     }
707     efree(timestampdir);
708     efree(timestampfile);
709
710     debug_return;
711 }
712
713 /*
714  * Returns true if tty lives on a devpts, /dev or /devices filesystem, else
715  * false.  Unlike most filesystems, the ctime of devpts nodes is not updated
716  * when the device node is written to, only when the inode's status changes,
717  * typically via the chmod, chown, link, rename, or utimes system calls.
718  * Since the ctime is "stable" in this case, we can stash it the tty ticket
719  * file and use it to determine whether the tty ticket file is stale.
720  */
721 static bool
722 tty_is_devpts(const char *tty)
723 {
724     bool retval = false;
725 #ifdef __linux__
726     struct statfs sfs;
727     debug_decl(tty_is_devpts, SUDO_DEBUG_PTY)
728
729 #ifndef DEVPTS_SUPER_MAGIC
730 # define DEVPTS_SUPER_MAGIC 0x1cd1
731 #endif
732
733     if (statfs(tty, &sfs) == 0) {
734         if (sfs.f_type == DEVPTS_SUPER_MAGIC)
735             retval = true;
736     }
737 #elif defined(__sun) && defined(__SVR4)
738     struct statvfs sfs;
739     debug_decl(tty_is_devpts, SUDO_DEBUG_PTY)
740
741     if (statvfs(tty, &sfs) == 0) {
742         if (strcmp(sfs.f_fstr, "dev") == 0 || strcmp(sfs.f_fstr, "devices") == 0)
743             retval = true;
744     }
745 #else
746     debug_decl(tty_is_devpts, SUDO_DEBUG_PTY)
747 #endif /* __linux__ */
748     debug_return_bool(retval);
749 }
750
751 /*
752  * Get passwd entry for the user we are going to authenticate as.
753  * By default, this is the user invoking sudo.  In the most common
754  * case, this matches sudo_user.pw or runas_pw.
755  */
756 static struct passwd *
757 get_authpw(void)
758 {
759     struct passwd *pw;
760     debug_decl(get_authpw, SUDO_DEBUG_AUTH)
761
762     if (def_rootpw) {
763         if ((pw = sudo_getpwuid(ROOT_UID)) == NULL)
764             log_fatal(0, _("unknown uid: %u"), ROOT_UID);
765     } else if (def_runaspw) {
766         if ((pw = sudo_getpwnam(def_runas_default)) == NULL)
767             log_fatal(0, _("unknown user: %s"), def_runas_default);
768     } else if (def_targetpw) {
769         if (runas_pw->pw_name == NULL)
770             log_fatal(NO_MAIL|MSG_ONLY, _("unknown uid: %u"),
771                 (unsigned int) runas_pw->pw_uid);
772         sudo_pw_addref(runas_pw);
773         pw = runas_pw;
774     } else {
775         sudo_pw_addref(sudo_user.pw);
776         pw = sudo_user.pw;
777     }
778
779     debug_return_ptr(pw);
780 }