2 * Copyright (c) 1993-1996,1998-2005 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 * Sponsored in part by the Defense Advanced Research Projects
17 * Agency (DARPA) and Air Force Research Laboratory, Air Force
18 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
23 #include <sys/types.h>
24 #include <sys/param.h>
27 # include <sys/file.h>
37 #endif /* STDC_HEADERS */
41 # ifdef HAVE_STRINGS_H
44 #endif /* HAVE_STRING_H */
47 #endif /* HAVE_UNISTD_H */
51 # include "emul/err.h"
52 #endif /* HAVE_ERR_H */
60 # include <emul/timespec.h>
66 __unused static const char rcsid[] = "$Sudo: check.c,v 1.223.2.9 2007/07/06 19:52:13 millert Exp $";
69 /* Status codes for timestamp_status() */
76 /* Flags for timestamp_status() */
77 #define TS_MAKE_DIRS 1
80 static void build_timestamp __P((char **, char **));
81 static int timestamp_status __P((char *, char *, char *, int));
82 static char *expand_prompt __P((char *, char *, char *));
83 static void lecture __P((int));
84 static void update_timestamp __P((char *, char *));
87 * This function only returns if the user can successfully
88 * verify who he/she is.
94 char *timestampdir = NULL;
95 char *timestampfile = NULL;
99 if (user_uid == 0 || user_uid == runas_pw->pw_uid || user_is_exempt())
102 build_timestamp(×tampdir, ×tampfile);
103 status = timestamp_status(timestampdir, timestampfile, user_name,
105 if (status != TS_CURRENT || ISSET(validated, FLAG_CHECK_USER)) {
108 /* Expand any escapes in the prompt. */
109 prompt = expand_prompt(user_prompt ? user_prompt : def_passprompt,
110 user_name, user_shost);
112 verify_user(auth_pw, prompt);
114 /* Only update timestamp if user was validated. */
115 if (status != TS_ERROR && ISSET(validated, VALIDATE_OK))
116 update_timestamp(timestampdir, timestampfile);
118 efree(timestampfile);
122 * Standard sudo lecture.
123 * TODO: allow the user to specify a file name instead.
133 if (def_lecture == never ||
134 (def_lecture == once && status != TS_MISSING && status != TS_ERROR))
137 if (def_lecture_file && (fp = fopen(def_lecture_file, "r")) != NULL) {
138 while ((nread = fread(buf, sizeof(char), sizeof(buf), fp)) != 0)
139 fwrite(buf, nread, 1, stderr);
143 We trust you have received the usual lecture from the local System\n\
144 Administrator. It usually boils down to these three things:\n\
146 #1) Respect the privacy of others.\n\
147 #2) Think before you type.\n\
148 #3) With great power comes great responsibility.\n\n",
154 * Update the time on the timestamp file/dir or create it if necessary.
157 update_timestamp(timestampdir, timestampfile)
161 if (timestamp_uid != 0)
162 set_perms(PERM_TIMESTAMP);
163 if (touch(-1, timestampfile ? timestampfile : timestampdir, NULL) == -1) {
165 int fd = open(timestampfile, O_WRONLY|O_CREAT|O_TRUNC, 0600);
168 log_error(NO_EXIT|USE_ERRNO, "Can't open %s", timestampfile);
172 if (mkdir(timestampdir, 0700) == -1)
173 log_error(NO_EXIT|USE_ERRNO, "Can't mkdir %s", timestampdir);
176 if (timestamp_uid != 0)
177 set_perms(PERM_ROOT);
181 * Expand %h and %u escapes in the prompt and pass back the dynamically
182 * allocated result. Returns the same string if there are no escapes.
185 expand_prompt(old_prompt, user, host)
192 char *p, *np, *new_prompt, *endp;
194 /* How much space do we need to malloc for the prompt? */
196 for (p = old_prompt, len = strlen(old_prompt); *p; p++) {
201 len += strlen(user_shost) - 2;
206 len += strlen(user_host) - 2;
211 len += strlen(user_name) - 2;
216 len += strlen(*user_runas) - 2;
224 else if (def_targetpw || def_runaspw) {
225 len += strlen(*user_runas) - 2;
228 len += strlen(user_name) - 2;
244 new_prompt = (char *) emalloc(++len);
245 endp = new_prompt + len;
246 for (p = old_prompt, np = new_prompt; *p; p++) {
251 n = strlcpy(np, user_shost, np - endp);
258 n = strlcpy(np, user_host, np - endp);
265 n = strlcpy(np, user_name, np - endp);
272 n = strlcpy(np, *user_runas, np - endp);
280 n = strlcpy(np, "root", np - endp);
282 else if (def_targetpw || def_runaspw) {
283 n = strlcpy(np, *user_runas, np - endp);
286 n = strlcpy(np, user_name, np - endp);
293 /* convert %% -> % */
307 new_prompt = old_prompt;
312 /* We pre-allocate enough space, so this should never happen. */
313 errx(1, "internal error, expand_prompt() overflow");
317 * Checks if the user is exempt from supplying a password.
325 if (!def_exempt_group)
328 if (!(grp = getgrnam(def_exempt_group)))
331 if (user_gid == grp->gr_gid)
334 for (gr_mem = grp->gr_mem; *gr_mem; gr_mem++) {
335 if (strcmp(user_name, *gr_mem) == 0)
343 * Fills in timestampdir as well as timestampfile if using tty tickets.
346 build_timestamp(timestampdir, timestampfile)
348 char **timestampfile;
353 dirparent = def_timestampdir;
354 len = easprintf(timestampdir, "%s/%s", dirparent, user_name);
356 log_error(0, "timestamp path too long: %s", *timestampdir);
359 * Timestamp file may be a file in the directory or NUL to use
360 * the directory as the timestamp.
362 if (def_tty_tickets) {
365 if ((p = strrchr(user_tty, '/')))
370 len = easprintf(timestampfile, "%s/%s/%s:%s", dirparent, user_name,
373 len = easprintf(timestampfile, "%s/%s/%s", dirparent, user_name, p);
375 log_error(0, "timestamp path too long: %s", *timestampfile);
376 } else if (def_targetpw) {
377 len = easprintf(timestampfile, "%s/%s/%s", dirparent, user_name,
380 log_error(0, "timestamp path too long: %s", *timestampfile);
382 *timestampfile = NULL;
386 * Check the timestamp file and directory and return their status.
389 timestamp_status(timestampdir, timestampfile, user, flags)
397 char *dirparent = def_timestampdir;
398 int status = TS_ERROR; /* assume the worst */
400 if (timestamp_uid != 0)
401 set_perms(PERM_TIMESTAMP);
404 * Sanity check dirparent and make it if it doesn't already exist.
405 * We start out assuming the worst (that the dir is not sane) and
406 * if it is ok upgrade the status to ``no timestamp file''.
407 * Note that we don't check the parent(s) of dirparent for
408 * sanity since the sudo dir is often just located in /tmp.
410 if (lstat(dirparent, &sb) == 0) {
411 if (!S_ISDIR(sb.st_mode))
412 log_error(NO_EXIT, "%s exists but is not a directory (0%o)",
413 dirparent, (unsigned int) sb.st_mode);
414 else if (sb.st_uid != timestamp_uid)
415 log_error(NO_EXIT, "%s owned by uid %lu, should be uid %lu",
416 dirparent, (unsigned long) sb.st_uid,
417 (unsigned long) timestamp_uid);
418 else if ((sb.st_mode & 0000022))
420 "%s writable by non-owner (0%o), should be mode 0700",
421 dirparent, (unsigned int) sb.st_mode);
423 if ((sb.st_mode & 0000777) != 0700)
424 (void) chmod(dirparent, 0700);
427 } else if (errno != ENOENT) {
428 log_error(NO_EXIT|USE_ERRNO, "can't stat %s", dirparent);
430 /* No dirparent, try to make one. */
431 if (ISSET(flags, TS_MAKE_DIRS)) {
432 if (mkdir(dirparent, S_IRWXU))
433 log_error(NO_EXIT|USE_ERRNO, "can't mkdir %s",
439 if (status == TS_ERROR) {
440 if (timestamp_uid != 0)
441 set_perms(PERM_ROOT);
446 * Sanity check the user's ticket dir. We start by downgrading
447 * the status to TS_ERROR. If the ticket dir exists and is sane
448 * this will be upgraded to TS_OLD. If the dir does not exist,
449 * it will be upgraded to TS_MISSING.
451 status = TS_ERROR; /* downgrade status again */
452 if (lstat(timestampdir, &sb) == 0) {
453 if (!S_ISDIR(sb.st_mode)) {
454 if (S_ISREG(sb.st_mode)) {
455 /* convert from old style */
456 if (unlink(timestampdir) == 0)
459 log_error(NO_EXIT, "%s exists but is not a directory (0%o)",
460 timestampdir, (unsigned int) sb.st_mode);
461 } else if (sb.st_uid != timestamp_uid)
462 log_error(NO_EXIT, "%s owned by uid %lu, should be uid %lu",
463 timestampdir, (unsigned long) sb.st_uid,
464 (unsigned long) timestamp_uid);
465 else if ((sb.st_mode & 0000022))
467 "%s writable by non-owner (0%o), should be mode 0700",
468 timestampdir, (unsigned int) sb.st_mode);
470 if ((sb.st_mode & 0000777) != 0700)
471 (void) chmod(timestampdir, 0700);
472 status = TS_OLD; /* do date check later */
474 } else if (errno != ENOENT) {
475 log_error(NO_EXIT|USE_ERRNO, "can't stat %s", timestampdir);
480 * If there is no user ticket dir, AND we are in tty ticket mode,
481 * AND the TS_MAKE_DIRS flag is set, create the user ticket dir.
483 if (status == TS_MISSING && timestampfile && ISSET(flags, TS_MAKE_DIRS)) {
484 if (mkdir(timestampdir, S_IRWXU) == -1) {
486 log_error(NO_EXIT|USE_ERRNO, "can't mkdir %s", timestampdir);
491 * Sanity check the tty ticket file if it exists.
493 if (timestampfile && status != TS_ERROR) {
494 if (status != TS_MISSING)
495 status = TS_NOFILE; /* dir there, file missing */
496 if (lstat(timestampfile, &sb) == 0) {
497 if (!S_ISREG(sb.st_mode)) {
499 log_error(NO_EXIT, "%s exists but is not a regular file (0%o)",
500 timestampfile, (unsigned int) sb.st_mode);
502 /* If bad uid or file mode, complain and kill the bogus file. */
503 if (sb.st_uid != timestamp_uid) {
505 "%s owned by uid %lu, should be uid %lu",
506 timestampfile, (unsigned long) sb.st_uid,
507 (unsigned long) timestamp_uid);
508 (void) unlink(timestampfile);
509 } else if ((sb.st_mode & 0000022)) {
511 "%s writable by non-owner (0%o), should be mode 0600",
512 timestampfile, (unsigned int) sb.st_mode);
513 (void) unlink(timestampfile);
515 /* If not mode 0600, fix it. */
516 if ((sb.st_mode & 0000777) != 0600)
517 (void) chmod(timestampfile, 0600);
519 status = TS_OLD; /* actually check mtime below */
522 } else if (errno != ENOENT) {
523 log_error(NO_EXIT|USE_ERRNO, "can't stat %s", timestampfile);
529 * If the file/dir exists and we are not removing it, check its mtime.
531 if (status == TS_OLD && !ISSET(flags, TS_REMOVE)) {
532 /* Negative timeouts only expire manually (sudo -k). */
533 if (def_timestamp_timeout < 0 && sb.st_mtime != 0)
536 /* XXX - should use timespec here */
538 if (def_timestamp_timeout &&
539 now - sb.st_mtime < 60 * def_timestamp_timeout) {
541 * Check for bogus time on the stampfile. The clock may
542 * have been set back or someone could be trying to spoof us.
544 if (sb.st_mtime > now + 60 * def_timestamp_timeout * 2) {
546 "timestamp too far in the future: %20.20s",
547 4 + ctime(&sb.st_mtime));
549 (void) unlink(timestampfile);
551 (void) rmdir(timestampdir);
559 if (timestamp_uid != 0)
560 set_perms(PERM_ROOT);
565 * Remove the timestamp ticket file/dir.
568 remove_timestamp(remove)
572 char *timestampdir, *timestampfile, *path;
575 build_timestamp(×tampdir, ×tampfile);
576 status = timestamp_status(timestampdir, timestampfile, user_name,
578 if (status == TS_OLD || status == TS_CURRENT) {
579 path = timestampfile ? timestampfile : timestampdir;
582 status = unlink(timestampfile);
584 status = rmdir(timestampdir);
585 if (status == -1 && errno != ENOENT) {
586 log_error(NO_EXIT, "can't remove %s (%s), will reset to Epoch",
587 path, strerror(errno));
592 if (touch(-1, path, &ts) == -1)
593 err(1, "can't reset %s to Epoch", path);
598 efree(timestampfile);