2 * Copyright (c) 1993-1996,1998-2004 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 */
63 static const char rcsid[] = "$Sudo: check.c,v 1.226 2004/09/08 15:48:23 millert Exp $";
66 /* Status codes for timestamp_status() */
73 static void build_timestamp __P((char **, char **));
74 static int timestamp_status __P((char *, char *, char *, int));
75 static char *expand_prompt __P((char *, char *, char *));
76 static void lecture __P((int));
77 static void update_timestamp __P((char *, char *));
80 * This function only returns if the user can successfully
81 * verify who he/she is.
87 char *timestampdir = NULL;
88 char *timestampfile = NULL;
92 if (user_uid == 0 || user_uid == runas_pw->pw_uid || user_is_exempt())
95 build_timestamp(×tampdir, ×tampfile);
96 status = timestamp_status(timestampdir, timestampfile, user_name, TRUE);
97 if (override || status != TS_CURRENT) {
100 /* Expand any escapes in the prompt. */
101 prompt = expand_prompt(user_prompt ? user_prompt : def_passprompt,
102 user_name, user_shost);
104 verify_user(auth_pw, prompt);
106 if (status != TS_ERROR)
107 update_timestamp(timestampdir, timestampfile);
114 * Standard sudo lecture.
115 * TODO: allow the user to specify a file name instead.
125 if (def_lecture == never ||
126 (def_lecture == once && status != TS_MISSING && status != TS_ERROR))
129 if (def_lecture_file && (fp = fopen(def_lecture_file, "r")) != NULL) {
130 while ((nread = fread(buf, sizeof(char), sizeof(buf), fp)) != 0)
131 fwrite(buf, nread, 1, stderr);
134 We trust you have received the usual lecture from the local System\n\
135 Administrator. It usually boils down to these three things:\n\
137 #1) Respect the privacy of others.\n\
138 #2) Think before you type.\n\
139 #3) With great power comes great responsibility.\n\n",
145 * Update the time on the timestamp file/dir or create it if necessary.
148 update_timestamp(timestampdir, timestampfile)
152 if (timestamp_uid != 0)
153 set_perms(PERM_TIMESTAMP);
154 if (touch(-1, timestampfile ? timestampfile : timestampdir, NULL) == -1) {
156 int fd = open(timestampfile, O_WRONLY|O_CREAT|O_TRUNC, 0600);
159 log_error(NO_EXIT|USE_ERRNO, "Can't open %s", timestampfile);
163 if (mkdir(timestampdir, 0700) == -1)
164 log_error(NO_EXIT|USE_ERRNO, "Can't mkdir %s", timestampdir);
167 if (timestamp_uid != 0)
168 set_perms(PERM_ROOT);
172 * Expand %h and %u escapes in the prompt and pass back the dynamically
173 * allocated result. Returns the same string if there are no escapes.
176 expand_prompt(old_prompt, user, host)
183 char *p, *np, *new_prompt, *endp;
185 /* How much space do we need to malloc for the prompt? */
187 for (p = old_prompt, len = strlen(old_prompt); *p; p++) {
192 len += strlen(user_shost) - 2;
197 len += strlen(user_host) - 2;
202 len += strlen(user_name) - 2;
207 len += strlen(*user_runas) - 2;
222 new_prompt = (char *) emalloc(++len);
223 endp = new_prompt + len;
224 for (p = old_prompt, np = new_prompt; *p; p++) {
229 n = strlcpy(np, user_shost, np - endp);
236 n = strlcpy(np, user_host, np - endp);
243 n = strlcpy(np, user_name, np - endp);
250 n = strlcpy(np, *user_runas, np - endp);
256 /* convert %% -> % */
270 new_prompt = old_prompt;
275 /* We pre-allocate enough space, so this should never happen. */
276 errx(1, "internal error, expand_prompt() overflow");
280 * Checks if the user is exempt from supplying a password.
288 if (!def_exempt_group)
291 if (!(grp = getgrnam(def_exempt_group)))
294 if (user_gid == grp->gr_gid)
297 for (gr_mem = grp->gr_mem; *gr_mem; gr_mem++) {
298 if (strcmp(user_name, *gr_mem) == 0)
306 * Fills in timestampdir as well as timestampfile if using tty tickets.
309 build_timestamp(timestampdir, timestampfile)
311 char **timestampfile;
316 dirparent = def_timestampdir;
317 len = easprintf(timestampdir, "%s/%s", dirparent, user_name);
319 log_error(0, "timestamp path too long: %s", *timestampdir);
322 * Timestamp file may be a file in the directory or NUL to use
323 * the directory as the timestamp.
325 if (def_tty_tickets) {
328 if ((p = strrchr(user_tty, '/')))
333 len = easprintf(timestampfile, "%s/%s/%s:%s", dirparent, user_name,
336 len = easprintf(timestampfile, "%s/%s/%s", dirparent, user_name, p);
338 log_error(0, "timestamp path too long: %s", *timestampfile);
339 } else if (def_targetpw) {
340 len = easprintf(timestampfile, "%s/%s/%s", dirparent, user_name,
343 log_error(0, "timestamp path too long: %s", *timestampfile);
345 *timestampfile = NULL;
349 * Check the timestamp file and directory and return their status.
352 timestamp_status(timestampdir, timestampfile, user, make_dirs)
360 char *dirparent = def_timestampdir;
361 int status = TS_ERROR; /* assume the worst */
363 if (timestamp_uid != 0)
364 set_perms(PERM_TIMESTAMP);
367 * Sanity check dirparent and make it if it doesn't already exist.
368 * We start out assuming the worst (that the dir is not sane) and
369 * if it is ok upgrade the status to ``no timestamp file''.
370 * Note that we don't check the parent(s) of dirparent for
371 * sanity since the sudo dir is often just located in /tmp.
373 if (lstat(dirparent, &sb) == 0) {
374 if (!S_ISDIR(sb.st_mode))
375 log_error(NO_EXIT, "%s exists but is not a directory (0%o)",
376 dirparent, sb.st_mode);
377 else if (sb.st_uid != timestamp_uid)
378 log_error(NO_EXIT, "%s owned by uid %lu, should be uid %lu",
379 dirparent, (unsigned long) sb.st_uid,
380 (unsigned long) timestamp_uid);
381 else if ((sb.st_mode & 0000022))
383 "%s writable by non-owner (0%o), should be mode 0700",
384 dirparent, sb.st_mode);
386 if ((sb.st_mode & 0000777) != 0700)
387 (void) chmod(dirparent, 0700);
390 } else if (errno != ENOENT) {
391 log_error(NO_EXIT|USE_ERRNO, "can't stat %s", dirparent);
393 /* No dirparent, try to make one. */
395 if (mkdir(dirparent, S_IRWXU))
396 log_error(NO_EXIT|USE_ERRNO, "can't mkdir %s",
402 if (status == TS_ERROR) {
403 if (timestamp_uid != 0)
404 set_perms(PERM_ROOT);
409 * Sanity check the user's ticket dir. We start by downgrading
410 * the status to TS_ERROR. If the ticket dir exists and is sane
411 * this will be upgraded to TS_OLD. If the dir does not exist,
412 * it will be upgraded to TS_MISSING.
414 status = TS_ERROR; /* downgrade status again */
415 if (lstat(timestampdir, &sb) == 0) {
416 if (!S_ISDIR(sb.st_mode)) {
417 if (S_ISREG(sb.st_mode)) {
418 /* convert from old style */
419 if (unlink(timestampdir) == 0)
422 log_error(NO_EXIT, "%s exists but is not a directory (0%o)",
423 timestampdir, sb.st_mode);
424 } else if (sb.st_uid != timestamp_uid)
425 log_error(NO_EXIT, "%s owned by uid %lu, should be uid %lu",
426 timestampdir, (unsigned long) sb.st_uid,
427 (unsigned long) timestamp_uid);
428 else if ((sb.st_mode & 0000022))
430 "%s writable by non-owner (0%o), should be mode 0700",
431 timestampdir, sb.st_mode);
433 if ((sb.st_mode & 0000777) != 0700)
434 (void) chmod(timestampdir, 0700);
435 status = TS_OLD; /* do date check later */
437 } else if (errno != ENOENT) {
438 log_error(NO_EXIT|USE_ERRNO, "can't stat %s", timestampdir);
443 * If there is no user ticket dir, AND we are in tty ticket mode,
444 * AND the make_dirs flag is set, create the user ticket dir.
446 if (status == TS_MISSING && timestampfile && make_dirs) {
447 if (mkdir(timestampdir, S_IRWXU) == -1) {
449 log_error(NO_EXIT|USE_ERRNO, "can't mkdir %s", timestampdir);
454 * Sanity check the tty ticket file if it exists.
456 if (timestampfile && status != TS_ERROR) {
457 if (status != TS_MISSING)
458 status = TS_NOFILE; /* dir there, file missing */
459 if (lstat(timestampfile, &sb) == 0) {
460 if (!S_ISREG(sb.st_mode)) {
462 log_error(NO_EXIT, "%s exists but is not a regular file (0%o)",
463 timestampfile, sb.st_mode);
465 /* If bad uid or file mode, complain and kill the bogus file. */
466 if (sb.st_uid != timestamp_uid) {
468 "%s owned by uid %lu, should be uid %lu",
469 timestampfile, (unsigned long) sb.st_uid,
470 (unsigned long) timestamp_uid);
471 (void) unlink(timestampfile);
472 } else if ((sb.st_mode & 0000022)) {
474 "%s writable by non-owner (0%o), should be mode 0600",
475 timestampfile, sb.st_mode);
476 (void) unlink(timestampfile);
478 /* If not mode 0600, fix it. */
479 if ((sb.st_mode & 0000777) != 0600)
480 (void) chmod(timestampfile, 0600);
482 status = TS_OLD; /* actually check mtime below */
485 } else if (errno != ENOENT) {
486 log_error(NO_EXIT|USE_ERRNO, "can't stat %s", timestampfile);
492 * If the file/dir exists, check its mtime.
494 if (status == TS_OLD) {
495 /* Negative timeouts only expire manually (sudo -k). */
496 if (def_timestamp_timeout < 0 && sb.st_mtime != 0)
499 /* XXX - should use timespec here */
501 if (def_timestamp_timeout &&
502 now - sb.st_mtime < 60 * def_timestamp_timeout) {
504 * Check for bogus time on the stampfile. The clock may
505 * have been set back or someone could be trying to spoof us.
507 if (sb.st_mtime > now + 60 * def_timestamp_timeout * 2) {
509 "timestamp too far in the future: %20.20s",
510 4 + ctime(&sb.st_mtime));
512 (void) unlink(timestampfile);
514 (void) rmdir(timestampdir);
522 if (timestamp_uid != 0)
523 set_perms(PERM_ROOT);
528 * Remove the timestamp ticket file/dir.
531 remove_timestamp(remove)
535 char *timestampdir, *timestampfile, *path;
538 build_timestamp(×tampdir, ×tampfile);
539 status = timestamp_status(timestampdir, timestampfile, user_name, FALSE);
540 if (status == TS_OLD || status == TS_CURRENT) {
541 path = timestampfile ? timestampfile : timestampdir;
544 status = unlink(timestampfile);
546 status = rmdir(timestampdir);
547 if (status == -1 && errno != ENOENT) {
548 log_error(NO_EXIT, "can't remove %s (%s), will reset to Epoch",
549 path, strerror(errno));
554 if (touch(-1, path, &ts) == -1)
555 err(1, "can't reset %s to Epoch", path);