Annotation of src/usr.bin/sudo/check.c, Revision 1.3
1.1 millert 1: /*
2: * Copyright (c) 1994-1996,1998-1999 Todd C. Miller <Todd.Miller@courtesan.com>
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:
37: #include <stdio.h>
38: #ifdef STDC_HEADERS
39: #include <stdlib.h>
40: #endif /* STDC_HEADERS */
41: #ifdef HAVE_UNISTD_H
42: #include <unistd.h>
43: #endif /* HAVE_UNISTD_H */
44: #ifdef HAVE_STRING_H
45: #include <string.h>
46: #endif /* HAVE_STRING_H */
47: #ifdef HAVE_STRINGS_H
48: #include <strings.h>
49: #endif /* HAVE_STRINGS_H */
50: #include <errno.h>
51: #include <fcntl.h>
52: #include <signal.h>
53: #include <time.h>
54: #include <sys/param.h>
55: #include <sys/types.h>
56: #include <sys/stat.h>
57: #include <sys/file.h>
58: #include <pwd.h>
59: #include <grp.h>
60:
61: #include "sudo.h"
62:
63: #ifndef lint
1.3 ! millert 64: static const char rcsid[] = "$Sudo: check.c,v 1.194 2000/02/15 23:36:03 millert Exp $";
1.1 millert 65: #endif /* lint */
66:
67: /* Status codes for timestamp_status() */
68: #define TS_CURRENT 0
69: #define TS_OLD 1
70: #define TS_MISSING 2
71: #define TS_NOFILE 3
72: #define TS_ERROR 4
73:
74: static void build_timestamp __P((char **, char **));
75: static int timestamp_status __P((char *, char *, char *, int));
76: static char *expand_prompt __P((char *, char *, char *));
77: static void lecture __P((void));
78: static void update_timestamp __P((char *, char *));
79:
80: /*
81: * This function only returns if the user can successfully
82: * verify who he/she is.
83: */
84: void
85: check_user()
86: {
87: char *timestampdir = NULL;
88: char *timestampfile = NULL;
89: char *prompt;
90: int status;
91:
92: if (user_uid == 0 || user_is_exempt())
93: return;
94:
95: build_timestamp(×tampdir, ×tampfile);
96: status = timestamp_status(timestampdir, timestampfile, user_name, TRUE);
97: if (status != TS_CURRENT) {
98: if (status == TS_MISSING || status == TS_ERROR)
99: lecture(); /* first time through they get a lecture */
100:
101: /* Expand any escapes in the prompt. */
102: prompt = expand_prompt(user_prompt ? user_prompt : def_str(I_PASSPROMPT),
103: user_name, user_shost);
104:
105: verify_user(prompt);
106: }
107: if (status != TS_ERROR)
108: update_timestamp(timestampdir, timestampfile);
109: free(timestampdir);
110: if (timestampfile)
111: free(timestampfile);
112: }
113:
114: /*
115: * Standard sudo lecture.
116: * TODO: allow the user to specify a file name instead.
117: */
118: static void
119: lecture()
120: {
121:
122: if (def_flag(I_LECTURE)) {
123: (void) fputs("\n\
124: We trust you have received the usual lecture from the local System\n\
125: Administrator. It usually boils down to these two things:\n\
126: \n\
127: #1) Respect the privacy of others.\n\
128: #2) Think before you type.\n\n",
129: stderr);
130: }
131: }
132:
133: /*
134: * Update the time on the timestamp file/dir or create it if necessary.
135: */
136: static void
137: update_timestamp(timestampdir, timestampfile)
138: char *timestampdir;
139: char *timestampfile;
140: {
141:
142: if (touch(timestampfile ? timestampfile : timestampdir, time(NULL)) == -1) {
143: if (timestampfile) {
144: int fd = open(timestampfile, O_WRONLY|O_CREAT|O_TRUNC, 0600);
145:
146: if (fd == -1)
147: log_error(NO_EXIT|USE_ERRNO, "Can't open %s", timestampfile);
148: else
149: close(fd);
150: } else {
151: if (mkdir(timestampdir, 0700) == -1)
152: log_error(NO_EXIT|USE_ERRNO, "Can't mkdir %s", timestampdir);
153: }
154: }
155: }
156:
157: /*
158: * Expand %h and %u escapes in the prompt and pass back the dynamically
159: * allocated result. Returns the same string if there are no escapes.
160: */
161: static char *
162: expand_prompt(old_prompt, user, host)
163: char *old_prompt;
164: char *user;
165: char *host;
166: {
167: size_t len;
168: int subst;
169: char *p, *np, *new_prompt, lastchar;
170:
171: /* How much space do we need to malloc for the prompt? */
172: subst = 0;
173: for (p = old_prompt, len = strlen(old_prompt), lastchar = '\0'; *p; p++) {
174: if (lastchar == '%') {
175: if (*p == 'h') {
176: len += strlen(user_shost) - 2;
177: subst = 1;
178: } else if (*p == 'u') {
179: len += strlen(user_name) - 2;
180: subst = 1;
181: }
182: }
183:
184: if (lastchar == '%' && *p == '%') {
185: lastchar = '\0';
186: len--;
187: } else
188: lastchar = *p;
189: }
190:
191: if (subst) {
192: new_prompt = (char *) emalloc(len + 1);
193: for (p = old_prompt, np = new_prompt; *p; p++) {
194: if (lastchar == '%' && (*p == 'h' || *p == 'u' || *p == '%')) {
195: /* substiture user/host name */
196: if (*p == 'h') {
197: np--;
198: strcpy(np, user_shost);
199: np += strlen(user_shost);
200: } else if (*p == 'u') {
201: np--;
202: strcpy(np, user_name);
203: np += strlen(user_name);
204: }
205: } else
206: *np++ = *p;
207:
208: if (lastchar == '%' && *p == '%')
209: lastchar = '\0';
210: else
211: lastchar = *p;
212: }
213: *np = '\0';
214: } else
215: new_prompt = old_prompt;
216:
217: return(new_prompt);
218: }
219:
220: /*
221: * Checks if the user is exempt from supplying a password.
222: */
223: int
224: user_is_exempt()
225: {
226: struct group *grp;
227: char **gr_mem;
228:
229: if (!def_str(I_EXEMPT_GRP))
230: return(FALSE);
231:
232: if (!(grp = getgrnam(def_str(I_EXEMPT_GRP))))
233: return(FALSE);
234:
235: if (getgid() == grp->gr_gid)
236: return(TRUE);
237:
238: for (gr_mem = grp->gr_mem; *gr_mem; gr_mem++) {
239: if (strcmp(user_name, *gr_mem) == 0)
240: return(TRUE);
241: }
242:
243: return(FALSE);
244: }
245:
246: /*
247: * Fills in timestampdir as well as timestampfile if using tty tickets.
248: */
249: static void
250: build_timestamp(timestampdir, timestampfile)
251: char **timestampdir;
252: char **timestampfile;
253: {
254: char *dirparent = def_str(I_TIMESTAMPDIR);
255:
256: if (def_flag(I_TTY_TICKETS)) {
257: char *p;
258:
259: if ((p = strrchr(user_tty, '/')))
260: p++;
261: else
262: p = user_tty;
263: if (strlen(dirparent) + strlen(user_name) + strlen(p) + 3 > MAXPATHLEN)
264: log_error(0, "timestamp path too long: %s/%s/%s", dirparent,
265: user_name, p);
266: easprintf(timestampdir, "%s/%s", dirparent, user_name);
267: easprintf(timestampfile, "%s/%s/%s", dirparent, user_name, p);
268: } else {
269: if (strlen(dirparent) + strlen(user_name) + 2 > MAXPATHLEN)
270: log_error(0, "timestamp path too long: %s/%s", dirparent, user_name);
271: easprintf(timestampdir, "%s/%s", dirparent, user_name);
272: *timestampfile = NULL;
273: }
274: }
275:
276: /*
277: * Check the timestamp file and directory and return their status.
278: */
279: static int
280: timestamp_status(timestampdir, timestampfile, user, make_dirs)
281: char *timestampdir;
282: char *timestampfile;
283: char *user;
284: int make_dirs;
285: {
286: struct stat sb;
287: time_t now;
288: char *dirparent = def_str(I_TIMESTAMPDIR);
289: int status = TS_ERROR; /* assume the worst */
290:
291: /*
292: * Sanity check dirparent and make it if it doesn't already exist.
293: * We start out assuming the worst (that the dir is not sane) and
294: * if it is ok upgrade the status to ``no timestamp file''.
295: * Note that we don't check the parent(s) of dirparent for
296: * sanity since the sudo dir is often just located in /tmp.
297: */
298: if (lstat(dirparent, &sb) == 0) {
299: if (!S_ISDIR(sb.st_mode))
300: log_error(NO_EXIT, "%s exists but is not a directory (0%o)",
301: dirparent, sb.st_mode);
302: else if (sb.st_uid != 0)
303: log_error(NO_EXIT, "%s owned by uid %ld, should be owned by root",
304: dirparent, (long) sb.st_uid);
305: else if ((sb.st_mode & 0000022))
306: log_error(NO_EXIT,
307: "%s writable by non-owner (0%o), should be mode 0700",
308: dirparent, sb.st_mode);
309: else {
310: if ((sb.st_mode & 0000777) != 0700)
311: (void) chmod(dirparent, 0700);
312: status = TS_MISSING;
313: }
314: } else if (errno != ENOENT) {
315: log_error(NO_EXIT|USE_ERRNO, "can't stat %s", dirparent);
316: } else {
317: /* No dirparent, try to make one. */
318: if (make_dirs) {
319: if (mkdir(dirparent, S_IRWXU))
320: log_error(NO_EXIT|USE_ERRNO, "can't mkdir %s",
321: dirparent);
322: else
323: status = TS_MISSING;
324: }
325: }
326: if (status == TS_ERROR)
327: return(status);
328:
329: /*
330: * Sanity check the user's ticket dir. We start by downgrading
331: * the status to TS_ERROR. If the ticket dir exists and is sane
332: * this will be upgraded to TS_OLD. If the dir does not exist,
333: * it will be upgraded to TS_MISSING.
334: */
335: status = TS_ERROR; /* downgrade status again */
336: if (lstat(timestampdir, &sb) == 0) {
337: if (!S_ISDIR(sb.st_mode)) {
338: if (S_ISREG(sb.st_mode)) {
339: /* convert from old style */
340: if (unlink(timestampdir) == 0)
341: status = TS_MISSING;
342: } else
343: log_error(NO_EXIT, "%s exists but is not a directory (0%o)",
344: timestampdir, sb.st_mode);
345: } else if (sb.st_uid != 0)
346: log_error(NO_EXIT, "%s owned by uid %ld, should be owned by root",
347: timestampdir, (long) sb.st_uid);
348: else if ((sb.st_mode & 0000022))
349: log_error(NO_EXIT,
350: "%s writable by non-owner (0%o), should be mode 0700",
351: timestampdir, sb.st_mode);
352: else {
353: if ((sb.st_mode & 0000777) != 0700)
354: (void) chmod(timestampdir, 0700);
355: status = TS_OLD; /* do date check later */
356: }
357: } else if (errno != ENOENT) {
358: log_error(NO_EXIT|USE_ERRNO, "can't stat %s", timestampdir);
359: } else
360: status = TS_MISSING;
361:
362: /*
363: * If there is no user ticket dir, AND we are in tty ticket mode,
364: * AND the make_dirs flag is set, create the user ticket dir.
365: */
366: if (status == TS_MISSING && timestampfile && make_dirs) {
367: if (mkdir(timestampdir, S_IRWXU) == -1) {
368: status = TS_ERROR;
369: log_error(NO_EXIT|USE_ERRNO, "can't mkdir %s", timestampdir);
370: }
371: }
372:
373: /*
374: * Sanity check the tty ticket file if it exists.
375: */
376: if (timestampfile && status != TS_ERROR) {
377: if (status != TS_MISSING)
378: status = TS_NOFILE; /* dir there, file missing */
379: if (lstat(timestampfile, &sb) == 0) {
380: if (!S_ISREG(sb.st_mode)) {
381: status = TS_ERROR;
382: log_error(NO_EXIT, "%s exists but is not a regular file (0%o)",
383: timestampfile, sb.st_mode);
384: } else {
385: /* If bad uid or file mode, complain and kill the bogus file. */
386: if (sb.st_uid != 0) {
387: log_error(NO_EXIT,
388: "%s owned by uid %ld, should be owned by root",
389: timestampfile, (long) sb.st_uid);
390: (void) unlink(timestampfile);
391: } else if ((sb.st_mode & 0000022)) {
392: log_error(NO_EXIT,
393: "%s writable by non-owner (0%o), should be mode 0600",
394: timestampfile, sb.st_mode);
395: (void) unlink(timestampfile);
396: } else {
397: /* If not mode 0600, fix it. */
398: if ((sb.st_mode & 0000777) != 0600)
399: (void) chmod(timestampfile, 0600);
400:
401: status = TS_OLD; /* actually check mtime below */
402: }
403: }
404: } else if (errno != ENOENT) {
405: log_error(NO_EXIT|USE_ERRNO, "can't stat %s", timestampfile);
406: status = TS_ERROR;
407: }
408: }
409:
410: /*
411: * If the file/dir exists, check its mtime.
412: */
413: if (status == TS_OLD) {
414: now = time(NULL);
415: if (def_ival(I_TS_TIMEOUT) &&
416: now - sb.st_mtime < 60 * def_ival(I_TS_TIMEOUT)) {
417: /*
418: * Check for bogus time on the stampfile. The clock may
419: * have been set back or someone could be trying to spoof us.
420: */
421: if (sb.st_mtime > now + 60 * def_ival(I_TS_TIMEOUT) * 2) {
422: log_error(NO_EXIT,
423: "timestamp too far in the future: %20.20s",
424: 4 + ctime(&sb.st_mtime));
425: if (timestampfile)
426: (void) unlink(timestampfile);
427: else
428: (void) rmdir(timestampdir);
429: status = TS_MISSING;
430: } else
431: status = TS_CURRENT;
432: }
433: }
434:
435: return(status);
436: }
437:
438: /*
439: * Remove the timestamp ticket file/dir.
440: */
441: void
442: remove_timestamp(remove)
443: int remove;
444: {
445: char *timestampdir;
446: char *timestampfile;
447: char *ts;
448: int status;
449:
450: build_timestamp(×tampdir, ×tampfile);
451: status = timestamp_status(timestampdir, timestampfile, user_name, FALSE);
452: if (status == TS_OLD || status == TS_CURRENT) {
453: ts = timestampfile ? timestampfile : timestampdir;
454: if (remove) {
455: if (timestampfile)
456: status = unlink(timestampfile);
457: else
458: status = rmdir(timestampdir);
1.2 millert 459: if (status == -1 && errno != ENOENT) {
1.1 millert 460: log_error(NO_EXIT, "can't remove %s (%s), will reset to epoch",
1.2 millert 461: ts, strerror(errno));
1.1 millert 462: remove = FALSE;
463: }
464: }
465: if (!remove && touch(ts, 0) == -1) {
466: (void) fprintf(stderr, "%s: can't reset %s to epoch: %s\n",
467: Argv[0], ts, strerror(errno));
468: }
469: }
470:
471: free(timestampdir);
472: if (timestampfile)
473: free(timestampfile);
474: }