Imported Upstream version 1.8.3
[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 } tty_info;
86
87 static int   build_timestamp(char **, char **);
88 static int   timestamp_status(char *, char *, char *, int);
89 static char *expand_prompt(char *, char *, char *);
90 static void  lecture(int);
91 static void  update_timestamp(char *, char *);
92 static int   tty_is_devpts(const char *);
93 static struct passwd *get_authpw(void);
94
95 /*
96  * Returns TRUE if the user successfully authenticates, else FALSE.
97  */
98 int
99 check_user(int validated, int mode)
100 {
101     struct passwd *auth_pw;
102     char *timestampdir = NULL;
103     char *timestampfile = NULL;
104     char *prompt;
105     struct stat sb;
106     int status, rval = TRUE;
107
108     /* Stash the tty's ctime for tty ticket comparison. */
109     if (def_tty_tickets && user_ttypath && stat(user_ttypath, &sb) == 0) {
110         tty_info.dev = sb.st_dev;
111         tty_info.ino = sb.st_ino;
112         tty_info.rdev = sb.st_rdev;
113         if (tty_is_devpts(user_ttypath))
114             ctim_get(&sb, &tty_info.ctime);
115     }
116
117     /* Init authentication system regardless of whether we need a password. */
118     auth_pw = get_authpw();
119     if (sudo_auth_init(auth_pw) == -1) {
120         rval = -1;
121         goto done;
122     }
123
124     /* Always prompt for a password when -k was specified with the command. */
125     if (ISSET(mode, MODE_IGNORE_TICKET)) {
126         SET(validated, FLAG_CHECK_USER);
127     } else {
128         /*
129          * Don't prompt for the root passwd or if the user is exempt.
130          * If the user is not changing uid/gid, no need for a password.
131          */
132         if (user_uid == 0 || (user_uid == runas_pw->pw_uid &&
133             (!runas_gr || user_in_group(sudo_user.pw, runas_gr->gr_name))) ||
134             user_is_exempt())
135             goto done;
136     }
137
138     if (build_timestamp(&timestampdir, &timestampfile) == -1) {
139         rval = -1;
140         goto done;
141     }
142
143     status = timestamp_status(timestampdir, timestampfile, user_name,
144         TS_MAKE_DIRS);
145
146     if (status != TS_CURRENT || ISSET(validated, FLAG_CHECK_USER)) {
147         /* Bail out if we are non-interactive and a password is required */
148         if (ISSET(mode, MODE_NONINTERACTIVE)) {
149             warningx(_("sorry, a password is required to run %s"), getprogname());
150             rval = -1;
151             goto done;
152         }
153
154         /* XXX - should not lecture if askpass helper is being used. */
155         lecture(status);
156
157         /* Expand any escapes in the prompt. */
158         prompt = expand_prompt(user_prompt ? user_prompt : def_passprompt,
159             user_name, user_shost);
160
161         rval = verify_user(auth_pw, prompt);
162     }
163     /* Only update timestamp if user was validated. */
164     if (rval == TRUE && ISSET(validated, VALIDATE_OK) &&
165         !ISSET(mode, MODE_IGNORE_TICKET) && status != TS_ERROR)
166         update_timestamp(timestampdir, timestampfile);
167     efree(timestampdir);
168     efree(timestampfile);
169
170 done:
171     sudo_auth_cleanup(auth_pw);
172     pw_delref(auth_pw);
173
174     return rval;
175 }
176
177 #define DEFAULT_LECTURE "\n" \
178     "We trust you have received the usual lecture from the local System\n" \
179     "Administrator. It usually boils down to these three things:\n\n" \
180     "    #1) Respect the privacy of others.\n" \
181     "    #2) Think before you type.\n" \
182     "    #3) With great power comes great responsibility.\n\n"
183
184 /*
185  * Standard sudo lecture.
186  */
187 static void
188 lecture(int status)
189 {
190     FILE *fp;
191     char buf[BUFSIZ];
192     ssize_t nread;
193     struct sudo_conv_message msg;
194     struct sudo_conv_reply repl;
195
196     if (def_lecture == never ||
197         (def_lecture == once && status != TS_MISSING && status != TS_ERROR))
198         return;
199
200     memset(&msg, 0, sizeof(msg));
201     memset(&repl, 0, sizeof(repl));
202
203     if (def_lecture_file && (fp = fopen(def_lecture_file, "r")) != NULL) {
204         while ((nread = fread(buf, sizeof(char), sizeof(buf) - 1, fp)) != 0) {
205             buf[sizeof(buf) - 1] = '\0';
206             msg.msg_type = SUDO_CONV_ERROR_MSG;
207             msg.msg = buf;
208             sudo_conv(1, &msg, &repl);
209         }
210         fclose(fp);
211     } else {
212         msg.msg_type = SUDO_CONV_ERROR_MSG;
213         msg.msg = _(DEFAULT_LECTURE);
214         sudo_conv(1, &msg, &repl);
215     }
216 }
217
218 /*
219  * Update the time on the timestamp file/dir or create it if necessary.
220  */
221 static void
222 update_timestamp(char *timestampdir, char *timestampfile)
223 {
224     /* If using tty timestamps but we have no tty there is nothing to do. */
225     if (def_tty_tickets && !user_ttypath)
226         return;
227
228     if (timestamp_uid != 0)
229         set_perms(PERM_TIMESTAMP);
230     if (timestampfile) {
231         /*
232          * Store tty info in timestamp file
233          */
234         int fd = open(timestampfile, O_WRONLY|O_CREAT, 0600);
235         if (fd == -1)
236             log_error(NO_EXIT|USE_ERRNO, _("unable to open %s"), timestampfile);
237         else {
238             lock_file(fd, SUDO_LOCK);
239             if (write(fd, &tty_info, sizeof(tty_info)) != sizeof(tty_info)) {
240                 log_error(NO_EXIT|USE_ERRNO, _("unable to write to %s"),
241                     timestampfile);
242             }
243             close(fd);
244         }
245     } else {
246         if (touch(-1, timestampdir, NULL) == -1) {
247             if (mkdir(timestampdir, 0700) == -1) {
248                 log_error(NO_EXIT|USE_ERRNO, _("unable to mkdir %s"),
249                     timestampdir);
250             }
251         }
252     }
253     if (timestamp_uid != 0)
254         restore_perms();
255 }
256
257 /*
258  * Expand %h and %u escapes in the prompt and pass back the dynamically
259  * allocated result.  Returns the same string if there are no escapes.
260  */
261 static char *
262 expand_prompt(char *old_prompt, char *user, char *host)
263 {
264     size_t len, n;
265     int subst;
266     char *p, *np, *new_prompt, *endp;
267
268     /* How much space do we need to malloc for the prompt? */
269     subst = 0;
270     for (p = old_prompt, len = strlen(old_prompt); *p; p++) {
271         if (p[0] =='%') {
272             switch (p[1]) {
273                 case 'h':
274                     p++;
275                     len += strlen(user_shost) - 2;
276                     subst = 1;
277                     break;
278                 case 'H':
279                     p++;
280                     len += strlen(user_host) - 2;
281                     subst = 1;
282                     break;
283                 case 'p':
284                     p++;
285                     if (def_rootpw)
286                             len += 2;
287                     else if (def_targetpw || def_runaspw)
288                             len += strlen(runas_pw->pw_name) - 2;
289                     else
290                             len += strlen(user_name) - 2;
291                     subst = 1;
292                     break;
293                 case 'u':
294                     p++;
295                     len += strlen(user_name) - 2;
296                     subst = 1;
297                     break;
298                 case 'U':
299                     p++;
300                     len += strlen(runas_pw->pw_name) - 2;
301                     subst = 1;
302                     break;
303                 case '%':
304                     p++;
305                     len--;
306                     subst = 1;
307                     break;
308                 default:
309                     break;
310             }
311         }
312     }
313
314     if (subst) {
315         new_prompt = emalloc(++len);
316         endp = new_prompt + len;
317         for (p = old_prompt, np = new_prompt; *p; p++) {
318             if (p[0] =='%') {
319                 switch (p[1]) {
320                     case 'h':
321                         p++;
322                         n = strlcpy(np, user_shost, np - endp);
323                         if (n >= np - endp)
324                             goto oflow;
325                         np += n;
326                         continue;
327                     case 'H':
328                         p++;
329                         n = strlcpy(np, user_host, np - endp);
330                         if (n >= np - endp)
331                             goto oflow;
332                         np += n;
333                         continue;
334                     case 'p':
335                         p++;
336                         if (def_rootpw)
337                                 n = strlcpy(np, "root", np - endp);
338                         else if (def_targetpw || def_runaspw)
339                                 n = strlcpy(np, runas_pw->pw_name, np - endp);
340                         else
341                                 n = strlcpy(np, user_name, np - endp);
342                         if (n >= np - endp)
343                                 goto oflow;
344                         np += n;
345                         continue;
346                     case 'u':
347                         p++;
348                         n = strlcpy(np, user_name, np - endp);
349                         if (n >= np - endp)
350                             goto oflow;
351                         np += n;
352                         continue;
353                     case 'U':
354                         p++;
355                         n = strlcpy(np,  runas_pw->pw_name, np - endp);
356                         if (n >= np - endp)
357                             goto oflow;
358                         np += n;
359                         continue;
360                     case '%':
361                         /* convert %% -> % */
362                         p++;
363                         break;
364                     default:
365                         /* no conversion */
366                         break;
367                 }
368             }
369             *np++ = *p;
370             if (np >= endp)
371                 goto oflow;
372         }
373         *np = '\0';
374     } else
375         new_prompt = old_prompt;
376
377     return new_prompt;
378
379 oflow:
380     /* We pre-allocate enough space, so this should never happen. */
381     errorx(1, _("internal error, expand_prompt() overflow"));
382 }
383
384 /*
385  * Checks if the user is exempt from supplying a password.
386  */
387 int
388 user_is_exempt(void)
389 {
390     if (!def_exempt_group)
391         return FALSE;
392     return user_in_group(sudo_user.pw, def_exempt_group);
393 }
394
395 /*
396  * Fills in timestampdir as well as timestampfile if using tty tickets.
397  */
398 static int
399 build_timestamp(char **timestampdir, char **timestampfile)
400 {
401     char *dirparent;
402     int len;
403
404     dirparent = def_timestampdir;
405     len = easprintf(timestampdir, "%s/%s", dirparent, user_name);
406     if (len >= PATH_MAX)
407         goto bad;
408
409     /*
410      * Timestamp file may be a file in the directory or NUL to use
411      * the directory as the timestamp.
412      */
413     if (def_tty_tickets) {
414         char *p;
415
416         if ((p = strrchr(user_tty, '/')))
417             p++;
418         else
419             p = user_tty;
420         if (def_targetpw)
421             len = easprintf(timestampfile, "%s/%s/%s:%s", dirparent, user_name,
422                 p, runas_pw->pw_name);
423         else
424             len = easprintf(timestampfile, "%s/%s/%s", dirparent, user_name, p);
425         if (len >= PATH_MAX)
426             goto bad;
427     } else if (def_targetpw) {
428         len = easprintf(timestampfile, "%s/%s/%s", dirparent, user_name,
429             runas_pw->pw_name);
430         if (len >= PATH_MAX)
431             goto bad;
432     } else
433         *timestampfile = NULL;
434
435     return len;
436 bad:
437     log_error(0, _("timestamp path too long: %s"), *timestampfile);
438     return -1;
439 }
440
441 /*
442  * Check the timestamp file and directory and return their status.
443  */
444 static int
445 timestamp_status(char *timestampdir, char *timestampfile, char *user, int flags)
446 {
447     struct stat sb;
448     struct timeval boottime, mtime;
449     time_t now;
450     char *dirparent = def_timestampdir;
451     int status = TS_ERROR;              /* assume the worst */
452
453     if (timestamp_uid != 0)
454         set_perms(PERM_TIMESTAMP);
455
456     /*
457      * Sanity check dirparent and make it if it doesn't already exist.
458      * We start out assuming the worst (that the dir is not sane) and
459      * if it is ok upgrade the status to ``no timestamp file''.
460      * Note that we don't check the parent(s) of dirparent for
461      * sanity since the sudo dir is often just located in /tmp.
462      */
463     if (lstat(dirparent, &sb) == 0) {
464         if (!S_ISDIR(sb.st_mode))
465             log_error(NO_EXIT, _("%s exists but is not a directory (0%o)"),
466                 dirparent, (unsigned int) sb.st_mode);
467         else if (sb.st_uid != timestamp_uid)
468             log_error(NO_EXIT, _("%s owned by uid %u, should be uid %u"),
469                 dirparent, (unsigned int) sb.st_uid,
470                 (unsigned int) timestamp_uid);
471         else if ((sb.st_mode & 0000022))
472             log_error(NO_EXIT,
473                 _("%s writable by non-owner (0%o), should be mode 0700"),
474                 dirparent, (unsigned int) sb.st_mode);
475         else {
476             if ((sb.st_mode & 0000777) != 0700)
477                 (void) chmod(dirparent, 0700);
478             status = TS_MISSING;
479         }
480     } else if (errno != ENOENT) {
481         log_error(NO_EXIT|USE_ERRNO, _("unable to stat %s"), dirparent);
482     } else {
483         /* No dirparent, try to make one. */
484         if (ISSET(flags, TS_MAKE_DIRS)) {
485             if (mkdir(dirparent, S_IRWXU))
486                 log_error(NO_EXIT|USE_ERRNO, _("unable to mkdir %s"),
487                     dirparent);
488             else
489                 status = TS_MISSING;
490         }
491     }
492     if (status == TS_ERROR)
493         goto done;
494
495     /*
496      * Sanity check the user's ticket dir.  We start by downgrading
497      * the status to TS_ERROR.  If the ticket dir exists and is sane
498      * this will be upgraded to TS_OLD.  If the dir does not exist,
499      * it will be upgraded to TS_MISSING.
500      */
501     status = TS_ERROR;                  /* downgrade status again */
502     if (lstat(timestampdir, &sb) == 0) {
503         if (!S_ISDIR(sb.st_mode)) {
504             if (S_ISREG(sb.st_mode)) {
505                 /* convert from old style */
506                 if (unlink(timestampdir) == 0)
507                     status = TS_MISSING;
508             } else
509                 log_error(NO_EXIT, _("%s exists but is not a directory (0%o)"),
510                     timestampdir, (unsigned int) sb.st_mode);
511         } else if (sb.st_uid != timestamp_uid)
512             log_error(NO_EXIT, _("%s owned by uid %u, should be uid %u"),
513                 timestampdir, (unsigned int) sb.st_uid,
514                 (unsigned int) timestamp_uid);
515         else if ((sb.st_mode & 0000022))
516             log_error(NO_EXIT,
517                 _("%s writable by non-owner (0%o), should be mode 0700"),
518                 timestampdir, (unsigned int) sb.st_mode);
519         else {
520             if ((sb.st_mode & 0000777) != 0700)
521                 (void) chmod(timestampdir, 0700);
522             status = TS_OLD;            /* do date check later */
523         }
524     } else if (errno != ENOENT) {
525         log_error(NO_EXIT|USE_ERRNO, _("unable to stat %s"), timestampdir);
526     } else
527         status = TS_MISSING;
528
529     /*
530      * If there is no user ticket dir, AND we are in tty ticket mode,
531      * AND the TS_MAKE_DIRS flag is set, create the user ticket dir.
532      */
533     if (status == TS_MISSING && timestampfile && ISSET(flags, TS_MAKE_DIRS)) {
534         if (mkdir(timestampdir, S_IRWXU) == -1) {
535             status = TS_ERROR;
536             log_error(NO_EXIT|USE_ERRNO, _("unable to mkdir %s"), timestampdir);
537         }
538     }
539
540     /*
541      * Sanity check the tty ticket file if it exists.
542      */
543     if (timestampfile && status != TS_ERROR) {
544         if (status != TS_MISSING)
545             status = TS_NOFILE;                 /* dir there, file missing */
546         if (def_tty_tickets && !user_ttypath)
547             goto done;                          /* no tty, always prompt */
548         if (lstat(timestampfile, &sb) == 0) {
549             if (!S_ISREG(sb.st_mode)) {
550                 status = TS_ERROR;
551                 log_error(NO_EXIT, _("%s exists but is not a regular file (0%o)"),
552                     timestampfile, (unsigned int) sb.st_mode);
553             } else {
554                 /* If bad uid or file mode, complain and kill the bogus file. */
555                 if (sb.st_uid != timestamp_uid) {
556                     log_error(NO_EXIT,
557                         _("%s owned by uid %u, should be uid %u"),
558                         timestampfile, (unsigned int) sb.st_uid,
559                         (unsigned int) timestamp_uid);
560                     (void) unlink(timestampfile);
561                 } else if ((sb.st_mode & 0000022)) {
562                     log_error(NO_EXIT,
563                         _("%s writable by non-owner (0%o), should be mode 0600"),
564                         timestampfile, (unsigned int) sb.st_mode);
565                     (void) unlink(timestampfile);
566                 } else {
567                     /* If not mode 0600, fix it. */
568                     if ((sb.st_mode & 0000777) != 0600)
569                         (void) chmod(timestampfile, 0600);
570
571                     /*
572                      * Check for stored tty info.  If the file is zero-sized
573                      * it is an old-style timestamp with no tty info in it.
574                      * If removing, we don't care about the contents.
575                      * The actual mtime check is done later.
576                      */
577                     if (ISSET(flags, TS_REMOVE)) {
578                         status = TS_OLD;
579                     } else if (sb.st_size != 0) {
580                         struct tty_info info;
581                         int fd = open(timestampfile, O_RDONLY, 0644);
582                         if (fd != -1) {
583                             if (read(fd, &info, sizeof(info)) == sizeof(info) &&
584                                 memcmp(&info, &tty_info, sizeof(info)) == 0) {
585                                 status = TS_OLD;
586                             }
587                             close(fd);
588                         }
589                     }
590                 }
591             }
592         } else if (errno != ENOENT) {
593             log_error(NO_EXIT|USE_ERRNO, _("unable to stat %s"), timestampfile);
594             status = TS_ERROR;
595         }
596     }
597
598     /*
599      * If the file/dir exists and we are not removing it, check its mtime.
600      */
601     if (status == TS_OLD && !ISSET(flags, TS_REMOVE)) {
602         mtim_get(&sb, &mtime);
603         /* Negative timeouts only expire manually (sudo -k). */
604         if (def_timestamp_timeout < 0 && mtime.tv_sec != 0)
605             status = TS_CURRENT;
606         else {
607             now = time(NULL);
608             if (def_timestamp_timeout &&
609                 now - mtime.tv_sec < 60 * def_timestamp_timeout) {
610                 /*
611                  * Check for bogus time on the stampfile.  The clock may
612                  * have been set back or someone could be trying to spoof us.
613                  */
614                 if (mtime.tv_sec > now + 60 * def_timestamp_timeout * 2) {
615                     time_t tv_sec = (time_t)mtime.tv_sec;
616                     log_error(NO_EXIT,
617                         _("timestamp too far in the future: %20.20s"),
618                         4 + ctime(&tv_sec));
619                     if (timestampfile)
620                         (void) unlink(timestampfile);
621                     else
622                         (void) rmdir(timestampdir);
623                     status = TS_MISSING;
624                 } else if (get_boottime(&boottime) && timevalcmp(&mtime, &boottime, <)) {
625                     status = TS_OLD;
626                 } else {
627                     status = TS_CURRENT;
628                 }
629             }
630         }
631     }
632
633 done:
634     if (timestamp_uid != 0)
635         restore_perms();
636     return status;
637 }
638
639 /*
640  * Remove the timestamp ticket file/dir.
641  */
642 void
643 remove_timestamp(int remove)
644 {
645     struct timeval tv;
646     char *timestampdir, *timestampfile, *path;
647     int status;
648
649     if (build_timestamp(&timestampdir, &timestampfile) == -1)
650         return;
651
652     status = timestamp_status(timestampdir, timestampfile, user_name,
653         TS_REMOVE);
654     if (status != TS_MISSING && status != TS_ERROR) {
655         path = timestampfile ? timestampfile : timestampdir;
656         if (remove) {
657             if (timestampfile)
658                 status = unlink(timestampfile);
659             else
660                 status = rmdir(timestampdir);
661             if (status == -1 && errno != ENOENT) {
662                 log_error(NO_EXIT,
663                     _("unable to remove %s (%s), will reset to the epoch"),
664                     path, strerror(errno));
665                 remove = FALSE;
666             }
667         }
668         if (!remove) {
669             timevalclear(&tv);
670             if (touch(-1, path, &tv) == -1 && errno != ENOENT)
671                 error(1, _("unable to reset %s to the epoch"), path);
672         }
673     }
674
675     efree(timestampdir);
676     efree(timestampfile);
677 }
678
679 /*
680  * Returns TRUE if tty lives on a devpts or /devices filesystem, else FALSE.
681  * Unlike most filesystems, the ctime of devpts nodes is not updated when
682  * the device node is written to, only when the inode's status changes,
683  * typically via the chmod, chown, link, rename, or utimes system calls.
684  * Since the ctime is "stable" in this case, we can stash it the tty ticket
685  * file and use it to determine whether the tty ticket file is stale.
686  */
687 static int
688 tty_is_devpts(const char *tty)
689 {
690     int retval = FALSE;
691 #ifdef __linux__
692     struct statfs sfs;
693
694 #ifndef DEVPTS_SUPER_MAGIC
695 # define DEVPTS_SUPER_MAGIC 0x1cd1
696 #endif
697
698     if (statfs(tty, &sfs) == 0) {
699         if (sfs.f_type == DEVPTS_SUPER_MAGIC)
700             retval = TRUE;
701     }
702 #elif defined(__sun) && defined(__SVR4)
703     struct statvfs sfs;
704
705     if (statvfs(tty, &sfs) == 0) {
706         if (strcmp(sfs.f_fstr, "devices") == 0)
707             retval = TRUE;
708     }
709 #endif /* __linux__ */
710     return retval;
711 }
712
713 /*
714  * Get passwd entry for the user we are going to authenticate as.
715  * By default, this is the user invoking sudo.  In the most common
716  * case, this matches sudo_user.pw or runas_pw.
717  */
718 static struct passwd *
719 get_authpw(void)
720 {
721     struct passwd *pw;
722
723     if (def_rootpw) {
724         if ((pw = sudo_getpwuid(ROOT_UID)) == NULL)
725             log_error(0, _("unknown uid: %u"), ROOT_UID);
726     } else if (def_runaspw) {
727         if ((pw = sudo_getpwnam(def_runas_default)) == NULL)
728             log_error(0, _("unknown user: %s"), def_runas_default);
729     } else if (def_targetpw) {
730         if (runas_pw->pw_name == NULL)
731             log_error(NO_MAIL|MSG_ONLY, _("unknown uid: %u"),
732                 (unsigned int) runas_pw->pw_uid);
733         pw_addref(runas_pw);
734         pw = runas_pw;
735     } else {
736         pw_addref(sudo_user.pw);
737         pw = sudo_user.pw;
738     }
739
740     return pw;
741 }