Annotation of src/usr.bin/sudo/check.c, Revision 1.9
1.1 millert 1: /*
1.8 millert 2: * Copyright (c) 1993-1996,1998-2003 Todd C. Miller <Todd.Miller@courtesan.com>
1.1 millert 3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: *
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: *
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: *
16: * 3. The name of the author may not be used to endorse or promote products
17: * derived from this software without specific prior written permission.
18: *
19: * 4. Products derived from this software may not be called "Sudo" nor
20: * may "Sudo" appear in their names without specific prior written
21: * permission from the author.
22: *
23: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
26: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33: */
34:
35: #include "config.h"
36:
1.6 millert 37: #include <sys/types.h>
38: #include <sys/param.h>
39: #include <sys/stat.h>
40: #include <sys/file.h>
1.1 millert 41: #include <stdio.h>
42: #ifdef STDC_HEADERS
1.6 millert 43: # include <stdlib.h>
44: # include <stddef.h>
45: #else
46: # ifdef HAVE_STDLIB_H
47: # include <stdlib.h>
48: # endif
1.1 millert 49: #endif /* STDC_HEADERS */
1.6 millert 50: #ifdef HAVE_STRING_H
51: # include <string.h>
52: #else
53: # ifdef HAVE_STRINGS_H
54: # include <strings.h>
55: # endif
56: #endif /* HAVE_STRING_H */
1.1 millert 57: #ifdef HAVE_UNISTD_H
1.6 millert 58: # include <unistd.h>
1.1 millert 59: #endif /* HAVE_UNISTD_H */
60: #include <errno.h>
61: #include <fcntl.h>
62: #include <signal.h>
63: #include <time.h>
64: #include <pwd.h>
65: #include <grp.h>
66:
67: #include "sudo.h"
68:
69: #ifndef lint
1.9 ! millert 70: static const char rcsid[] = "$Sudo: check.c,v 1.211 2003/04/01 14:58:55 millert Exp $";
1.1 millert 71: #endif /* lint */
72:
73: /* Status codes for timestamp_status() */
74: #define TS_CURRENT 0
75: #define TS_OLD 1
76: #define TS_MISSING 2
77: #define TS_NOFILE 3
78: #define TS_ERROR 4
79:
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((void));
84: static void update_timestamp __P((char *, char *));
85:
86: /*
87: * This function only returns if the user can successfully
88: * verify who he/she is.
89: */
90: void
91: check_user()
92: {
93: char *timestampdir = NULL;
94: char *timestampfile = NULL;
95: char *prompt;
96: int status;
97:
98: if (user_uid == 0 || user_is_exempt())
99: return;
100:
101: build_timestamp(×tampdir, ×tampfile);
102: status = timestamp_status(timestampdir, timestampfile, user_name, TRUE);
103: if (status != TS_CURRENT) {
104: if (status == TS_MISSING || status == TS_ERROR)
105: lecture(); /* first time through they get a lecture */
106:
107: /* Expand any escapes in the prompt. */
108: prompt = expand_prompt(user_prompt ? user_prompt : def_str(I_PASSPROMPT),
109: user_name, user_shost);
110:
1.4 millert 111: verify_user(auth_pw, prompt);
1.1 millert 112: }
113: if (status != TS_ERROR)
114: update_timestamp(timestampdir, timestampfile);
115: free(timestampdir);
116: if (timestampfile)
117: free(timestampfile);
118: }
119:
120: /*
121: * Standard sudo lecture.
122: * TODO: allow the user to specify a file name instead.
123: */
124: static void
125: lecture()
126: {
127:
128: if (def_flag(I_LECTURE)) {
129: (void) fputs("\n\
130: We trust you have received the usual lecture from the local System\n\
131: Administrator. It usually boils down to these two things:\n\
132: \n\
133: #1) Respect the privacy of others.\n\
134: #2) Think before you type.\n\n",
135: stderr);
136: }
137: }
138:
139: /*
140: * Update the time on the timestamp file/dir or create it if necessary.
141: */
142: static void
143: update_timestamp(timestampdir, timestampfile)
144: char *timestampdir;
145: char *timestampfile;
146: {
147:
1.8 millert 148: if (timestamp_uid != 0)
149: set_perms(PERM_TIMESTAMP);
1.1 millert 150: if (touch(timestampfile ? timestampfile : timestampdir, time(NULL)) == -1) {
151: if (timestampfile) {
152: int fd = open(timestampfile, O_WRONLY|O_CREAT|O_TRUNC, 0600);
153:
154: if (fd == -1)
155: log_error(NO_EXIT|USE_ERRNO, "Can't open %s", timestampfile);
156: else
157: close(fd);
158: } else {
159: if (mkdir(timestampdir, 0700) == -1)
160: log_error(NO_EXIT|USE_ERRNO, "Can't mkdir %s", timestampdir);
161: }
162: }
1.8 millert 163: if (timestamp_uid != 0)
164: set_perms(PERM_ROOT);
1.1 millert 165: }
166:
167: /*
168: * Expand %h and %u escapes in the prompt and pass back the dynamically
169: * allocated result. Returns the same string if there are no escapes.
170: */
171: static char *
172: expand_prompt(old_prompt, user, host)
173: char *old_prompt;
174: char *user;
175: char *host;
176: {
1.8 millert 177: size_t len, n;
1.1 millert 178: int subst;
1.8 millert 179: char *p, *np, *new_prompt, *endp;
1.1 millert 180:
181: /* How much space do we need to malloc for the prompt? */
182: subst = 0;
1.8 millert 183: for (p = old_prompt, len = strlen(old_prompt); *p; p++) {
184: if (p[0] =='%') {
185: switch (p[1]) {
186: case 'h':
187: p++;
188: len += strlen(user_shost) - 2;
189: subst = 1;
190: break;
191: case 'H':
192: p++;
193: len += strlen(user_host) - 2;
194: subst = 1;
195: break;
196: case 'u':
197: p++;
198: len += strlen(user_name) - 2;
199: subst = 1;
200: break;
201: case 'U':
202: p++;
203: len += strlen(*user_runas) - 2;
204: subst = 1;
205: break;
206: case '%':
207: p++;
208: len--;
209: subst = 1;
210: break;
211: default:
212: break;
1.1 millert 213: }
214: }
215: }
216:
217: if (subst) {
1.8 millert 218: new_prompt = (char *) emalloc(++len);
1.9 ! millert 219: endp = new_prompt + len;
1.8 millert 220: for (p = old_prompt, np = new_prompt; *p; p++) {
221: if (p[0] =='%') {
222: switch (p[1]) {
223: case 'h':
224: p++;
1.9 ! millert 225: n = strlcpy(np, user_shost, np - endp);
! 226: if (n >= np - endp)
1.8 millert 227: goto oflow;
228: np += n;
229: continue;
230: case 'H':
231: p++;
1.9 ! millert 232: n = strlcpy(np, user_host, np - endp);
! 233: if (n >= np - endp)
1.8 millert 234: goto oflow;
235: np += n;
236: continue;
237: case 'u':
238: p++;
1.9 ! millert 239: n = strlcpy(np, user_name, np - endp);
! 240: if (n >= np - endp)
1.8 millert 241: goto oflow;
242: np += n;
243: continue;
244: case 'U':
245: p++;
1.9 ! millert 246: n = strlcpy(np, *user_runas, np - endp);
! 247: if (n >= np - endp)
1.8 millert 248: goto oflow;
249: np += n;
250: continue;
251: case '%':
252: /* convert %% -> % */
253: p++;
254: break;
255: default:
256: /* no conversion */
257: break;
1.1 millert 258: }
1.8 millert 259: }
1.9 ! millert 260: *np++ = *p;
1.8 millert 261: if (np >= endp)
262: goto oflow;
1.1 millert 263: }
264: *np = '\0';
265: } else
266: new_prompt = old_prompt;
267:
268: return(new_prompt);
1.8 millert 269:
270: oflow:
271: /* We pre-allocate enough space, so this should never happen. */
272: (void) fprintf(stderr, "%s: internal error, expand_prompt() overflow\n",
273: Argv[0]);
274: exit(1);
1.1 millert 275: }
276:
277: /*
278: * Checks if the user is exempt from supplying a password.
279: */
280: int
281: user_is_exempt()
282: {
283: struct group *grp;
284: char **gr_mem;
285:
1.6 millert 286: if (!def_str(I_EXEMPT_GROUP))
1.1 millert 287: return(FALSE);
288:
1.6 millert 289: if (!(grp = getgrnam(def_str(I_EXEMPT_GROUP))))
1.1 millert 290: return(FALSE);
291:
1.5 millert 292: if (user_gid == grp->gr_gid)
1.1 millert 293: return(TRUE);
294:
295: for (gr_mem = grp->gr_mem; *gr_mem; gr_mem++) {
296: if (strcmp(user_name, *gr_mem) == 0)
297: return(TRUE);
298: }
299:
300: return(FALSE);
301: }
302:
303: /*
304: * Fills in timestampdir as well as timestampfile if using tty tickets.
305: */
306: static void
307: build_timestamp(timestampdir, timestampfile)
308: char **timestampdir;
309: char **timestampfile;
310: {
1.4 millert 311: char *dirparent;
312: int len;
1.1 millert 313:
1.4 millert 314: dirparent = def_str(I_TIMESTAMPDIR);
315: len = easprintf(timestampdir, "%s/%s", dirparent, user_name);
316: if (len >= MAXPATHLEN)
317: log_error(0, "timestamp path too long: %s", timestampdir);
318:
319: /*
320: * Timestamp file may be a file in the directory or NUL to use
321: * the directory as the timestamp.
322: */
1.1 millert 323: if (def_flag(I_TTY_TICKETS)) {
324: char *p;
325:
326: if ((p = strrchr(user_tty, '/')))
327: p++;
328: else
329: p = user_tty;
1.4 millert 330: if (def_flag(I_TARGETPW))
331: len = easprintf(timestampfile, "%s/%s/%s:%s", dirparent, user_name,
332: p, *user_runas);
333: else
334: len = easprintf(timestampfile, "%s/%s/%s", dirparent, user_name, p);
335: if (len >= MAXPATHLEN)
336: log_error(0, "timestamp path too long: %s", timestampfile);
337: } else if (def_flag(I_TARGETPW)) {
338: len = easprintf(timestampfile, "%s/%s/%s", dirparent, user_name,
339: *user_runas);
340: if (len >= MAXPATHLEN)
341: log_error(0, "timestamp path too long: %s", timestampfile);
342: } else
1.1 millert 343: *timestampfile = NULL;
344: }
345:
346: /*
347: * Check the timestamp file and directory and return their status.
348: */
349: static int
350: timestamp_status(timestampdir, timestampfile, user, make_dirs)
351: char *timestampdir;
352: char *timestampfile;
353: char *user;
354: int make_dirs;
355: {
356: struct stat sb;
357: time_t now;
358: char *dirparent = def_str(I_TIMESTAMPDIR);
359: int status = TS_ERROR; /* assume the worst */
360:
1.8 millert 361: if (timestamp_uid != 0)
362: set_perms(PERM_TIMESTAMP);
363:
1.1 millert 364: /*
365: * Sanity check dirparent and make it if it doesn't already exist.
366: * We start out assuming the worst (that the dir is not sane) and
367: * if it is ok upgrade the status to ``no timestamp file''.
368: * Note that we don't check the parent(s) of dirparent for
369: * sanity since the sudo dir is often just located in /tmp.
370: */
371: if (lstat(dirparent, &sb) == 0) {
372: if (!S_ISDIR(sb.st_mode))
373: log_error(NO_EXIT, "%s exists but is not a directory (0%o)",
374: dirparent, sb.st_mode);
1.8 millert 375: else if (sb.st_uid != timestamp_uid)
376: log_error(NO_EXIT, "%s owned by uid %lu, should be uid %lu",
377: dirparent, (unsigned long) sb.st_uid,
378: (unsigned long) timestamp_uid);
1.1 millert 379: else if ((sb.st_mode & 0000022))
380: log_error(NO_EXIT,
381: "%s writable by non-owner (0%o), should be mode 0700",
382: dirparent, sb.st_mode);
383: else {
384: if ((sb.st_mode & 0000777) != 0700)
385: (void) chmod(dirparent, 0700);
386: status = TS_MISSING;
387: }
388: } else if (errno != ENOENT) {
389: log_error(NO_EXIT|USE_ERRNO, "can't stat %s", dirparent);
390: } else {
391: /* No dirparent, try to make one. */
392: if (make_dirs) {
393: if (mkdir(dirparent, S_IRWXU))
394: log_error(NO_EXIT|USE_ERRNO, "can't mkdir %s",
395: dirparent);
396: else
397: status = TS_MISSING;
398: }
399: }
1.8 millert 400: if (status == TS_ERROR) {
401: if (timestamp_uid != 0)
402: set_perms(PERM_ROOT);
1.1 millert 403: return(status);
1.8 millert 404: }
1.1 millert 405:
406: /*
407: * Sanity check the user's ticket dir. We start by downgrading
408: * the status to TS_ERROR. If the ticket dir exists and is sane
409: * this will be upgraded to TS_OLD. If the dir does not exist,
410: * it will be upgraded to TS_MISSING.
411: */
412: status = TS_ERROR; /* downgrade status again */
413: if (lstat(timestampdir, &sb) == 0) {
414: if (!S_ISDIR(sb.st_mode)) {
415: if (S_ISREG(sb.st_mode)) {
416: /* convert from old style */
417: if (unlink(timestampdir) == 0)
418: status = TS_MISSING;
419: } else
420: log_error(NO_EXIT, "%s exists but is not a directory (0%o)",
421: timestampdir, sb.st_mode);
1.8 millert 422: } else if (sb.st_uid != timestamp_uid)
423: log_error(NO_EXIT, "%s owned by uid %lu, should be uid %lu",
424: timestampdir, (unsigned long) sb.st_uid,
425: (unsigned long) timestamp_uid);
1.1 millert 426: else if ((sb.st_mode & 0000022))
427: log_error(NO_EXIT,
428: "%s writable by non-owner (0%o), should be mode 0700",
429: timestampdir, sb.st_mode);
430: else {
431: if ((sb.st_mode & 0000777) != 0700)
432: (void) chmod(timestampdir, 0700);
433: status = TS_OLD; /* do date check later */
434: }
435: } else if (errno != ENOENT) {
436: log_error(NO_EXIT|USE_ERRNO, "can't stat %s", timestampdir);
437: } else
438: status = TS_MISSING;
439:
440: /*
441: * If there is no user ticket dir, AND we are in tty ticket mode,
442: * AND the make_dirs flag is set, create the user ticket dir.
443: */
444: if (status == TS_MISSING && timestampfile && make_dirs) {
445: if (mkdir(timestampdir, S_IRWXU) == -1) {
446: status = TS_ERROR;
447: log_error(NO_EXIT|USE_ERRNO, "can't mkdir %s", timestampdir);
448: }
449: }
450:
451: /*
452: * Sanity check the tty ticket file if it exists.
453: */
454: if (timestampfile && status != TS_ERROR) {
455: if (status != TS_MISSING)
456: status = TS_NOFILE; /* dir there, file missing */
457: if (lstat(timestampfile, &sb) == 0) {
458: if (!S_ISREG(sb.st_mode)) {
459: status = TS_ERROR;
460: log_error(NO_EXIT, "%s exists but is not a regular file (0%o)",
461: timestampfile, sb.st_mode);
462: } else {
463: /* If bad uid or file mode, complain and kill the bogus file. */
1.8 millert 464: if (sb.st_uid != timestamp_uid) {
1.1 millert 465: log_error(NO_EXIT,
1.8 millert 466: "%s owned by uid %ud, should be uid %lu",
467: timestampfile, (unsigned long) sb.st_uid,
468: (unsigned long) timestamp_uid);
1.1 millert 469: (void) unlink(timestampfile);
470: } else if ((sb.st_mode & 0000022)) {
471: log_error(NO_EXIT,
472: "%s writable by non-owner (0%o), should be mode 0600",
473: timestampfile, sb.st_mode);
474: (void) unlink(timestampfile);
475: } else {
476: /* If not mode 0600, fix it. */
477: if ((sb.st_mode & 0000777) != 0600)
478: (void) chmod(timestampfile, 0600);
479:
480: status = TS_OLD; /* actually check mtime below */
481: }
482: }
483: } else if (errno != ENOENT) {
484: log_error(NO_EXIT|USE_ERRNO, "can't stat %s", timestampfile);
485: status = TS_ERROR;
486: }
487: }
488:
489: /*
490: * If the file/dir exists, check its mtime.
491: */
492: if (status == TS_OLD) {
1.6 millert 493: /* Negative timeouts only expire manually (sudo -k). */
494: if (def_ival(I_TIMESTAMP_TIMEOUT) < 0 && sb.st_mtime != 0)
495: status = TS_CURRENT;
496: else {
497: now = time(NULL);
498: if (def_ival(I_TIMESTAMP_TIMEOUT) &&
499: now - sb.st_mtime < 60 * def_ival(I_TIMESTAMP_TIMEOUT)) {
500: /*
501: * Check for bogus time on the stampfile. The clock may
502: * have been set back or someone could be trying to spoof us.
503: */
504: if (sb.st_mtime > now + 60 * def_ival(I_TIMESTAMP_TIMEOUT) * 2) {
505: log_error(NO_EXIT,
506: "timestamp too far in the future: %20.20s",
507: 4 + ctime(&sb.st_mtime));
508: if (timestampfile)
509: (void) unlink(timestampfile);
510: else
511: (void) rmdir(timestampdir);
512: status = TS_MISSING;
513: } else
514: status = TS_CURRENT;
515: }
1.1 millert 516: }
517: }
518:
1.8 millert 519: if (timestamp_uid != 0)
520: set_perms(PERM_ROOT);
1.1 millert 521: return(status);
522: }
523:
524: /*
525: * Remove the timestamp ticket file/dir.
526: */
527: void
528: remove_timestamp(remove)
529: int remove;
530: {
531: char *timestampdir;
532: char *timestampfile;
533: char *ts;
534: int status;
535:
536: build_timestamp(×tampdir, ×tampfile);
537: status = timestamp_status(timestampdir, timestampfile, user_name, FALSE);
538: if (status == TS_OLD || status == TS_CURRENT) {
539: ts = timestampfile ? timestampfile : timestampdir;
540: if (remove) {
541: if (timestampfile)
542: status = unlink(timestampfile);
543: else
544: status = rmdir(timestampdir);
1.2 millert 545: if (status == -1 && errno != ENOENT) {
1.1 millert 546: log_error(NO_EXIT, "can't remove %s (%s), will reset to epoch",
1.2 millert 547: ts, strerror(errno));
1.1 millert 548: remove = FALSE;
549: }
550: }
551: if (!remove && touch(ts, 0) == -1) {
552: (void) fprintf(stderr, "%s: can't reset %s to epoch: %s\n",
553: Argv[0], ts, strerror(errno));
554: }
555: }
556:
557: free(timestampdir);
558: if (timestampfile)
559: free(timestampfile);
560: }