2 * Copyright (c) 1993-1996,1998-2005, 2007-2011
3 * Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
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.
24 #include <sys/types.h>
25 #include <sys/param.h>
31 #if defined(__sun) && defined(__SVR4)
32 # include <sys/statvfs.h>
35 # include <sys/file.h>
45 #endif /* STDC_HEADERS */
48 #endif /* HAVE_STRING_H */
51 #endif /* HAVE_STRINGS_H */
54 #endif /* HAVE_UNISTD_H */
55 #if TIME_WITH_SYS_TIME
66 /* Status codes for timestamp_status() */
73 /* Flags for timestamp_status() */
74 #define TS_MAKE_DIRS 1
78 * Info stored in tty ticket from stat(2) to help with tty matching.
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 */
87 static void build_timestamp __P((char **, char **));
88 static int timestamp_status __P((char *, char *, char *, int));
89 static char *expand_prompt __P((char *, char *, char *));
90 static void lecture __P((int));
91 static void update_timestamp __P((char *, char *));
92 static int tty_is_devpts __P((const char *));
93 static struct passwd *get_authpw __P((void));
96 * This function only returns if the user can successfully
97 * verify who he/she is.
100 check_user(validated, mode)
104 char *timestampdir = NULL;
105 char *timestampfile = NULL;
110 /* Stash the tty's ctime for tty ticket comparison. */
111 if (def_tty_tickets && user_ttypath && stat(user_ttypath, &sb) == 0) {
112 tty_info.dev = sb.st_dev;
113 tty_info.ino = sb.st_ino;
114 tty_info.rdev = sb.st_rdev;
115 if (tty_is_devpts(user_ttypath))
116 ctim_get(&sb, &tty_info.ctime);
119 /* Always prompt for a password when -k was specified with the command. */
120 if (ISSET(mode, MODE_INVALIDATE)) {
121 SET(validated, FLAG_CHECK_USER);
124 * Don't prompt for the root passwd or if the user is exempt.
125 * If the user is not changing uid/gid, no need for a password.
127 if (user_uid == 0 || (user_uid == runas_pw->pw_uid &&
128 (!runas_gr || user_in_group(sudo_user.pw, runas_gr->gr_name))) ||
133 build_timestamp(×tampdir, ×tampfile);
134 status = timestamp_status(timestampdir, timestampfile, user_name,
137 if (status != TS_CURRENT || ISSET(validated, FLAG_CHECK_USER)) {
138 struct passwd *auth_pw;
140 /* Bail out if we are non-interactive and a password is required */
141 if (ISSET(mode, MODE_NONINTERACTIVE))
142 errorx(1, "sorry, a password is required to run %s", getprogname());
144 /* If user specified -A, make sure we have an askpass helper. */
145 if (ISSET(tgetpass_flags, TGP_ASKPASS)) {
146 if (user_askpass == NULL)
148 "no askpass program specified, try setting SUDO_ASKPASS");
149 } else if (!ISSET(tgetpass_flags, TGP_STDIN)) {
150 /* If no tty but DISPLAY is set, use askpass if we have it. */
151 if (!user_ttypath && !tty_present()) {
152 if (user_askpass && user_display && *user_display != '\0') {
153 SET(tgetpass_flags, TGP_ASKPASS);
154 } else if (!def_visiblepw) {
156 "no tty present and no askpass program specified");
161 if (!ISSET(tgetpass_flags, TGP_ASKPASS))
164 /* Expand any escapes in the prompt. */
165 prompt = expand_prompt(user_prompt ? user_prompt : def_passprompt,
166 user_name, user_shost);
168 auth_pw = get_authpw();
169 verify_user(auth_pw, prompt);
172 /* Only update timestamp if user was validated. */
173 if (ISSET(validated, VALIDATE_OK) && !ISSET(mode, MODE_INVALIDATE) && status != TS_ERROR)
174 update_timestamp(timestampdir, timestampfile);
176 efree(timestampfile);
180 * Standard sudo lecture.
190 if (def_lecture == never ||
191 (def_lecture == once && status != TS_MISSING && status != TS_ERROR))
194 if (def_lecture_file && (fp = fopen(def_lecture_file, "r")) != NULL) {
195 while ((nread = fread(buf, sizeof(char), sizeof(buf), fp)) != 0)
196 fwrite(buf, nread, 1, stderr);
200 We trust you have received the usual lecture from the local System\n\
201 Administrator. It usually boils down to these three things:\n\
203 #1) Respect the privacy of others.\n\
204 #2) Think before you type.\n\
205 #3) With great power comes great responsibility.\n\n",
211 * Update the time on the timestamp file/dir or create it if necessary.
214 update_timestamp(timestampdir, timestampfile)
218 /* If using tty timestamps but we have no tty there is nothing to do. */
219 if (def_tty_tickets && !user_ttypath)
222 if (timestamp_uid != 0)
223 set_perms(PERM_TIMESTAMP);
226 * Store tty info in timestamp file
228 int fd = open(timestampfile, O_WRONLY|O_CREAT, 0600);
230 log_error(NO_EXIT|USE_ERRNO, "Can't open %s", timestampfile);
232 lock_file(fd, SUDO_LOCK);
233 if (write(fd, &tty_info, sizeof(tty_info)) != sizeof(tty_info))
234 log_error(NO_EXIT|USE_ERRNO, "Can't write %s", timestampfile);
238 if (touch(-1, timestampdir, NULL) == -1) {
239 if (mkdir(timestampdir, 0700) == -1)
240 log_error(NO_EXIT|USE_ERRNO, "Can't mkdir %s", timestampdir);
243 if (timestamp_uid != 0)
244 set_perms(PERM_ROOT);
248 * Expand %h and %u escapes in the prompt and pass back the dynamically
249 * allocated result. Returns the same string if there are no escapes.
252 expand_prompt(old_prompt, user, host)
259 char *p, *np, *new_prompt, *endp;
261 /* How much space do we need to malloc for the prompt? */
263 for (p = old_prompt, len = strlen(old_prompt); *p; p++) {
268 len += strlen(user_shost) - 2;
273 len += strlen(user_host) - 2;
280 else if (def_targetpw || def_runaspw)
281 len += strlen(runas_pw->pw_name) - 2;
283 len += strlen(user_name) - 2;
288 len += strlen(user_name) - 2;
293 len += strlen(runas_pw->pw_name) - 2;
308 new_prompt = emalloc(++len);
309 endp = new_prompt + len;
310 for (p = old_prompt, np = new_prompt; *p; p++) {
315 n = strlcpy(np, user_shost, np - endp);
322 n = strlcpy(np, user_host, np - endp);
330 n = strlcpy(np, "root", np - endp);
331 else if (def_targetpw || def_runaspw)
332 n = strlcpy(np, runas_pw->pw_name, np - endp);
334 n = strlcpy(np, user_name, np - endp);
341 n = strlcpy(np, user_name, np - endp);
348 n = strlcpy(np, runas_pw->pw_name, np - endp);
354 /* convert %% -> % */
368 new_prompt = old_prompt;
373 /* We pre-allocate enough space, so this should never happen. */
374 errorx(1, "internal error, expand_prompt() overflow");
378 * Checks if the user is exempt from supplying a password.
383 if (!def_exempt_group)
385 return user_in_group(sudo_user.pw, def_exempt_group);
389 * Fills in timestampdir as well as timestampfile if using tty tickets.
392 build_timestamp(timestampdir, timestampfile)
394 char **timestampfile;
399 dirparent = def_timestampdir;
400 len = easprintf(timestampdir, "%s/%s", dirparent, user_name);
402 log_error(0, "timestamp path too long: %s", *timestampdir);
405 * Timestamp file may be a file in the directory or NUL to use
406 * the directory as the timestamp.
408 if (def_tty_tickets) {
411 if ((p = strrchr(user_tty, '/')))
416 len = easprintf(timestampfile, "%s/%s/%s:%s", dirparent, user_name,
417 p, runas_pw->pw_name);
419 len = easprintf(timestampfile, "%s/%s/%s", dirparent, user_name, p);
421 log_error(0, "timestamp path too long: %s", *timestampfile);
422 } else if (def_targetpw) {
423 len = easprintf(timestampfile, "%s/%s/%s", dirparent, user_name,
426 log_error(0, "timestamp path too long: %s", *timestampfile);
428 *timestampfile = NULL;
432 * Check the timestamp file and directory and return their status.
435 timestamp_status(timestampdir, timestampfile, user, flags)
442 struct timeval boottime, mtime;
444 char *dirparent = def_timestampdir;
445 int status = TS_ERROR; /* assume the worst */
447 if (timestamp_uid != 0)
448 set_perms(PERM_TIMESTAMP);
451 * Sanity check dirparent and make it if it doesn't already exist.
452 * We start out assuming the worst (that the dir is not sane) and
453 * if it is ok upgrade the status to ``no timestamp file''.
454 * Note that we don't check the parent(s) of dirparent for
455 * sanity since the sudo dir is often just located in /tmp.
457 if (lstat(dirparent, &sb) == 0) {
458 if (!S_ISDIR(sb.st_mode))
459 log_error(NO_EXIT, "%s exists but is not a directory (0%o)",
460 dirparent, (unsigned int) sb.st_mode);
461 else if (sb.st_uid != timestamp_uid)
462 log_error(NO_EXIT, "%s owned by uid %u, should be uid %u",
463 dirparent, (unsigned int) sb.st_uid,
464 (unsigned int) timestamp_uid);
465 else if ((sb.st_mode & 0000022))
467 "%s writable by non-owner (0%o), should be mode 0700",
468 dirparent, (unsigned int) sb.st_mode);
470 if ((sb.st_mode & 0000777) != 0700)
471 (void) chmod(dirparent, 0700);
474 } else if (errno != ENOENT) {
475 log_error(NO_EXIT|USE_ERRNO, "can't stat %s", dirparent);
477 /* No dirparent, try to make one. */
478 if (ISSET(flags, TS_MAKE_DIRS)) {
479 if (mkdir(dirparent, S_IRWXU))
480 log_error(NO_EXIT|USE_ERRNO, "can't mkdir %s",
486 if (status == TS_ERROR) {
487 if (timestamp_uid != 0)
488 set_perms(PERM_ROOT);
493 * Sanity check the user's ticket dir. We start by downgrading
494 * the status to TS_ERROR. If the ticket dir exists and is sane
495 * this will be upgraded to TS_OLD. If the dir does not exist,
496 * it will be upgraded to TS_MISSING.
498 status = TS_ERROR; /* downgrade status again */
499 if (lstat(timestampdir, &sb) == 0) {
500 if (!S_ISDIR(sb.st_mode)) {
501 if (S_ISREG(sb.st_mode)) {
502 /* convert from old style */
503 if (unlink(timestampdir) == 0)
506 log_error(NO_EXIT, "%s exists but is not a directory (0%o)",
507 timestampdir, (unsigned int) sb.st_mode);
508 } else if (sb.st_uid != timestamp_uid)
509 log_error(NO_EXIT, "%s owned by uid %u, should be uid %u",
510 timestampdir, (unsigned int) sb.st_uid,
511 (unsigned int) timestamp_uid);
512 else if ((sb.st_mode & 0000022))
514 "%s writable by non-owner (0%o), should be mode 0700",
515 timestampdir, (unsigned int) sb.st_mode);
517 if ((sb.st_mode & 0000777) != 0700)
518 (void) chmod(timestampdir, 0700);
519 status = TS_OLD; /* do date check later */
521 } else if (errno != ENOENT) {
522 log_error(NO_EXIT|USE_ERRNO, "can't stat %s", timestampdir);
527 * If there is no user ticket dir, AND we are in tty ticket mode,
528 * AND the TS_MAKE_DIRS flag is set, create the user ticket dir.
530 if (status == TS_MISSING && timestampfile && ISSET(flags, TS_MAKE_DIRS)) {
531 if (mkdir(timestampdir, S_IRWXU) == -1) {
533 log_error(NO_EXIT|USE_ERRNO, "can't mkdir %s", timestampdir);
538 * Sanity check the tty ticket file if it exists.
540 if (timestampfile && status != TS_ERROR) {
541 if (status != TS_MISSING)
542 status = TS_NOFILE; /* dir there, file missing */
543 if (def_tty_tickets && !user_ttypath)
544 goto done; /* no tty, always prompt */
545 if (lstat(timestampfile, &sb) == 0) {
546 if (!S_ISREG(sb.st_mode)) {
548 log_error(NO_EXIT, "%s exists but is not a regular file (0%o)",
549 timestampfile, (unsigned int) sb.st_mode);
551 /* If bad uid or file mode, complain and kill the bogus file. */
552 if (sb.st_uid != timestamp_uid) {
554 "%s owned by uid %u, should be uid %u",
555 timestampfile, (unsigned int) sb.st_uid,
556 (unsigned int) timestamp_uid);
557 (void) unlink(timestampfile);
558 } else if ((sb.st_mode & 0000022)) {
560 "%s writable by non-owner (0%o), should be mode 0600",
561 timestampfile, (unsigned int) sb.st_mode);
562 (void) unlink(timestampfile);
564 /* If not mode 0600, fix it. */
565 if ((sb.st_mode & 0000777) != 0600)
566 (void) chmod(timestampfile, 0600);
569 * Check for stored tty info. If the file is zero-sized
570 * it is an old-style timestamp with no tty info in it.
571 * If removing, we don't care about the contents.
572 * The actual mtime check is done later.
574 if (ISSET(flags, TS_REMOVE)) {
576 } else if (sb.st_size != 0) {
577 struct tty_info info;
578 int fd = open(timestampfile, O_RDONLY, 0644);
580 if (read(fd, &info, sizeof(info)) == sizeof(info) &&
581 memcmp(&info, &tty_info, sizeof(info)) == 0) {
589 } else if (errno != ENOENT) {
590 log_error(NO_EXIT|USE_ERRNO, "can't stat %s", timestampfile);
596 * If the file/dir exists and we are not removing it, check its mtime.
598 if (status == TS_OLD && !ISSET(flags, TS_REMOVE)) {
599 mtim_get(&sb, &mtime);
600 /* Negative timeouts only expire manually (sudo -k). */
601 if (def_timestamp_timeout < 0 && mtime.tv_sec != 0)
605 if (def_timestamp_timeout &&
606 now - mtime.tv_sec < 60 * def_timestamp_timeout) {
608 * Check for bogus time on the stampfile. The clock may
609 * have been set back or someone could be trying to spoof us.
611 if (mtime.tv_sec > now + 60 * def_timestamp_timeout * 2) {
612 time_t tv_sec = (time_t)mtime.tv_sec;
614 "timestamp too far in the future: %20.20s",
617 (void) unlink(timestampfile);
619 (void) rmdir(timestampdir);
621 } else if (get_boottime(&boottime) && timevalcmp(&mtime, &boottime, <)) {
631 if (timestamp_uid != 0)
632 set_perms(PERM_ROOT);
637 * Remove the timestamp ticket file/dir.
640 remove_timestamp(remove)
644 char *timestampdir, *timestampfile, *path;
647 build_timestamp(×tampdir, ×tampfile);
648 status = timestamp_status(timestampdir, timestampfile, user_name,
650 if (status == TS_OLD || status == TS_CURRENT) {
651 path = timestampfile ? timestampfile : timestampdir;
654 status = unlink(timestampfile);
656 status = rmdir(timestampdir);
657 if (status == -1 && errno != ENOENT) {
658 log_error(NO_EXIT, "can't remove %s (%s), will reset to Epoch",
659 path, strerror(errno));
664 if (touch(-1, path, &tv) == -1 && errno != ENOENT)
665 error(1, "can't reset %s to Epoch", path);
670 efree(timestampfile);
674 * Returns TRUE if tty lives on a devpts or /devices filesystem, else FALSE.
675 * Unlike most filesystems, the ctime of devpts nodes is not updated when
676 * the device node is written to, only when the inode's status changes,
677 * typically via the chmod, chown, link, rename, or utimes system calls.
678 * Since the ctime is "stable" in this case, we can stash it the tty ticket
679 * file and use it to determine whether the tty ticket file is stale.
689 #ifndef DEVPTS_SUPER_MAGIC
690 # define DEVPTS_SUPER_MAGIC 0x1cd1
693 if (statfs(tty, &sfs) == 0) {
694 if (sfs.f_type == DEVPTS_SUPER_MAGIC)
697 #elif defined(__sun) && defined(__SVR4)
700 if (statvfs(tty, &sfs) == 0) {
701 if (strcmp(sfs.f_fstr, "devices") == 0)
704 #endif /* __linux__ */
709 * Get passwd entry for the user we are going to authenticate as.
710 * By default, this is the user invoking sudo. In the most common
711 * case, this matches sudo_user.pw or runas_pw.
713 static struct passwd *
719 if ((pw = sudo_getpwuid(0)) == NULL)
720 log_error(0, "unknown uid: 0");
721 } else if (def_runaspw) {
722 if ((pw = sudo_getpwnam(def_runas_default)) == NULL)
723 log_error(0, "unknown user: %s", def_runas_default);
724 } else if (def_targetpw) {
725 if (runas_pw->pw_name == NULL)
726 log_error(NO_MAIL|MSG_ONLY, "unknown uid: %u",
727 (unsigned int) runas_pw->pw_uid);
731 pw_addref(sudo_user.pw);