Make flags be unsigned.
[checkpath] / path.c
1 /* -*-c-*-
2 *
3 * $Id: path.c,v 1.4 2001/01/25 22:16:02 mdw Exp $
4 *
5 * Check a path for safety
6 *
7 * (c) 1999 Mark Wooding
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of chkpath.
13 *
14 * chkpath is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * chkpath is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with chkpath; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29 /*----- Revision history --------------------------------------------------*
30 *
31 * $Log: path.c,v $
32 * Revision 1.4 2001/01/25 22:16:02 mdw
33 * Make flags be unsigned.
34 *
35 * Revision 1.3 1999/05/21 22:07:20 mdw
36 * Take advantage of new dynamic string macros.
37 *
38 * Revision 1.2 1999/05/18 20:49:12 mdw
39 * Use a dynamic string for reading symlinks.
40 *
41 * Revision 1.1.1.1 1999/04/06 20:12:07 mdw
42 * Import new project.
43 *
44 */
45
46 /*----- Header files ------------------------------------------------------*/
47
48 #include <errno.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53
54 #include <sys/types.h>
55 #include <sys/stat.h>
56 #include <unistd.h>
57
58 #include <pwd.h>
59 #include <grp.h>
60
61 #include <mLib/alloc.h>
62 #include <mLib/dstr.h>
63
64 #include "path.h"
65
66 /*----- Data structures ---------------------------------------------------*/
67
68 /* --- An item in the directory list --- *
69 *
70 * Each directory becomes an element on a list which is manipulated in a
71 * stack-like way.
72 */
73
74 struct elt {
75 struct elt *e_link; /* Pointer to the next one along */
76 size_t e_offset; /* Offset of name in path string */
77 unsigned e_flags; /* Various useful flags */
78 char e_name[1]; /* Name of the directory */
79 };
80
81 #define f_sticky 1u /* Directory has sticky bit set */
82
83 #define f_last 1u /* This is the final item to check */
84
85 /*----- Static variables --------------------------------------------------*/
86
87 static struct elt rootnode = { 0, 0, 0 }; /* Root of the list */
88 static struct elt *sp; /* Stack pointer for list */
89 static dstr d = DSTR_INIT; /* Current path string */
90
91 /*----- Main code ---------------------------------------------------------*/
92
93 /* --- @splitpath@ --- *
94 *
95 * Arguments: @const char *path@ = path string to break apart
96 * @struct elt *tail@ = tail block to attach to end of list
97 *
98 * Returns: Pointer to the new list head.
99 *
100 * Use: Breaks a path string into directories and adds each one
101 * as a node on the list, in the right order. These can then
102 * be pushed onto the directory stack as required.
103 */
104
105 static struct elt *splitpath(const char *path, struct elt *tail)
106 {
107 struct elt *head, **ee = &head, *e;
108
109 while (*path) {
110 size_t n;
111
112 /* --- Either a leading `/', or a doubled one --- *
113 *
114 * Either way, ignore it.
115 */
116
117 if (*path == '/') {
118 path++;
119 continue;
120 }
121
122 /* --- Skip to the next directory separator --- *
123 *
124 * Build a list element for it, and link it on.
125 */
126
127 n = strcspn(path, "/");
128 e = xmalloc(sizeof(struct elt) + n + 1);
129 memcpy(e->e_name, path, n);
130 e->e_name[n] = 0;
131 e->e_flags = 0;
132 *ee = e;
133 ee = &e->e_link;
134 path += n;
135 }
136
137 /* --- Done --- */
138
139 *ee = tail;
140 return (head);
141 }
142
143 /* --- @pop@ --- *
144 *
145 * Arguments: ---
146 *
147 * Returns: ---
148 *
149 * Use: Removes the top item from the directory stack.
150 */
151
152 static void pop(void)
153 {
154 if (sp->e_link) {
155 struct elt *e = sp->e_link;
156 d.len = sp->e_offset;
157 DPUTZ(&d);
158 sp = e;
159 }
160 }
161
162 /* --- @popall@ --- *
163 *
164 * Arguments: ---
165 *
166 * Returns: ---
167 *
168 * Use: Removes all the items from the directory stack.
169 */
170
171 static void popall(void)
172 {
173 while (sp->e_link)
174 pop();
175 }
176
177 /* --- @push@ --- *
178 *
179 * Arguments: @struct elt *e@ = pointer to directory element
180 *
181 * Returns: ---
182 *
183 * Use: Pushes a new subdirectory onto the stack.
184 */
185
186 static void push(struct elt *e)
187 {
188 e->e_link = sp;
189 e->e_offset = d.len;
190 DPUTC(&d, '/');
191 DPUTS(&d, e->e_name);
192 sp = e;
193 }
194
195 /* --- @report@ --- *
196 *
197 * Arguments: @struct chkpath *cp@ = pointer to context
198 * @int what@ = what sort of report is this?
199 * @int verbose@ = how verbose is this?
200 * @const char *p@ = what path does it refer to?
201 * @const char *msg@ = the message to give to the user
202 *
203 * Returns: ---
204 *
205 * Use: Formats and presents messages to the client.
206 */
207
208 static void report(struct chkpath *cp, int what, int verbose,
209 const char *p, const char *msg, ...)
210 {
211 /* --- Decide whether to bin this message --- */
212
213 if (!cp->cp_report || verbose > cp->cp_verbose || !(cp->cp_what & what))
214 return;
215
216 /* --- Format the message nicely --- */
217
218 if (cp->cp_what & CP_REPORT) {
219 dstr d = DSTR_INIT;
220 va_list ap;
221 const char *q = msg;
222 size_t n;
223 int e = errno;
224
225 va_start(ap, msg);
226 if (verbose > 1)
227 dstr_puts(&d, "[ ");
228 if (p)
229 dstr_putf(&d, "Path: %s: ", p);
230 while (*q) {
231 if (*q == '%') {
232 q++;
233 switch (*q) {
234 case 'e':
235 dstr_puts(&d, strerror(e));
236 break;
237 case 'u': {
238 uid_t u = (uid_t)va_arg(ap, int);
239 struct passwd *pw = getpwuid(u);
240 if (pw)
241 dstr_putf(&d, "`%s'", pw->pw_name);
242 else
243 dstr_putf(&d, "%i", (int)u);
244 } break;
245 case 'g': {
246 gid_t g = (gid_t)va_arg(ap, int);
247 struct group *gr = getgrgid(g);
248 if (gr)
249 dstr_putf(&d, "`%s'", gr->gr_name);
250 else
251 dstr_putf(&d, "%i", (int)g);
252 } break;
253 case 's': {
254 const char *s = va_arg(ap, const char *);
255 dstr_puts(&d, s);
256 } break;
257 case '%':
258 dstr_putc(&d, '%');
259 break;
260 default:
261 dstr_putc(&d, '%');
262 dstr_putc(&d, *q);
263 break;
264 }
265 q++;
266 } else {
267 n = strcspn(q, "%");
268 DPUTM(&d, q, n);
269 q += n;
270 }
271 }
272 if (verbose > 1)
273 dstr_puts(&d, " ]");
274 DPUTZ(&d);
275 cp->cp_report(what, verbose, p, d.buf, cp->cp_arg);
276 dstr_destroy(&d);
277 va_end(ap);
278 } else
279 cp->cp_report(what, verbose, p, 0, cp->cp_arg);
280 }
281
282 /* --- @sanity@ --- *
283 *
284 * Arguments: @const char *p@ = name of directory to check
285 * @struct stat *st@ = pointer to @stat@(2) block for it
286 * @struct chkpath *cp@ = pointer to caller parameters
287 * @unsigned f@ = various flags
288 *
289 * Returns: Zero if everything's OK, else bitmask of problems.
290 *
291 * Use: Performs the main load of sanity-checking on a directory.
292 */
293
294 static int sanity(const char *p, struct stat *st,
295 struct chkpath *cp, unsigned f)
296 {
297 int bad = 0;
298 int sticky = (cp->cp_what & CP_STICKYOK) || !(f & f_last) ? 01000 : 0;
299
300 /* --- Check for world-writability --- */
301
302 if ((cp->cp_what & CP_WRWORLD) &&
303 (st->st_mode & (0002 | sticky)) == 0002) {
304 bad |= CP_WRWORLD;
305 report(cp, CP_WRWORLD, 1, p, "** world writable **");
306 }
307
308 /* --- Check for group-writability --- */
309
310 if ((cp->cp_what & (CP_WRGRP | CP_WROTHGRP)) &&
311 (st->st_mode & (0020 | sticky)) == 0020) {
312 if (cp->cp_what & CP_WRGRP) {
313 bad |= CP_WRGRP;
314 report(cp, CP_WRGRP, 1, p, "writable by group %g", st->st_gid);
315 } else {
316 int i;
317 for (i = 0; i < cp->cp_gids; i++) {
318 if (st->st_gid == cp->cp_gid[i])
319 goto good_gid;
320 }
321 bad |= CP_WROTHGRP;
322 report(cp, CP_WROTHGRP, 1, p, "writable by group %g", st->st_gid);
323 good_gid:;
324 }
325 }
326
327 /* --- Check for user-writability --- */
328
329 if ((cp->cp_what & CP_WROTHUSR) &&
330 st->st_uid != cp->cp_uid &&
331 st->st_uid != 0) {
332 bad |= CP_WROTHUSR;
333 report(cp, CP_WROTHUSR, 1, p, "owner is user %u", st->st_uid);
334 }
335
336 /* --- Done sanity check --- */
337
338 return (bad);
339 }
340
341 /* --- @path_check@ --- *
342 *
343 * Arguments: @const char *p@ = directory name which needs checking
344 * @struct chkpath *cp@ = caller parameters for the check
345 *
346 * Returns: Zero if all is well, otherwise bitmask of problems.
347 *
348 * Use: Scrutinises a directory path to see what evil things other
349 * users could do to it.
350 */
351
352 int path_check(const char *p, struct chkpath *cp)
353 {
354 char cwd[PATH_MAX];
355 struct elt *e, *ee;
356 struct stat st;
357 int bad = 0;
358
359 /* --- Initialize stack pointer and path string --- */
360
361 sp = &rootnode;
362 dstr_destroy(&d);
363
364 /* --- Try to find the current directory --- */
365
366 if (!getcwd(cwd, sizeof(cwd))) {
367 report(cp, CP_ERROR, 0, 0, "can't find current directory: %e");
368 return (CP_ERROR);
369 }
370
371 /* --- Check that the root directory is OK --- */
372
373 if (stat("/", &st)) {
374 report(cp, CP_ERROR, 0, 0, "can't stat root directory: %e");
375 return (CP_ERROR);
376 }
377
378 report(cp, CP_REPORT, 3, p, "begin scan");
379 bad |= sanity("/", &st, cp, 0);
380
381 /* --- Get the initial list of things to process --- */
382
383 ee = splitpath(p, 0);
384 if (*p != '/')
385 ee = splitpath(cwd, ee);
386
387 /* --- While there are list items which still need doing --- */
388
389 while (ee) {
390 e = ee->e_link;
391
392 /* --- Strip off simple `.' elements --- */
393
394 if (strcmp(ee->e_name, ".") == 0) {
395 free(ee);
396 ee = e;
397 continue;
398 }
399
400 /* --- Backtrack on `..' elements --- */
401
402 else if (strcmp(ee->e_name, "..") == 0) {
403 pop();
404 free(ee);
405 ee = e;
406 continue;
407 }
408
409 /* --- Everything else gets pushed on the end --- */
410
411 push(ee);
412 ee = e;
413
414 /* --- Find out what sort of a thing this is --- */
415
416 if (lstat(d.buf, &st)) {
417 report(cp, CP_ERROR, 0, d.buf, "can't stat: %e");
418 bad |= CP_ERROR;
419 break;
420 }
421
422 /* --- Handle symbolic links specially --- */
423
424 if (S_ISLNK(st.st_mode)) {
425 dstr buf = DSTR_INIT;
426 int i;
427
428 /* --- Resolve the link --- */
429
430 dstr_ensure(&buf, st.st_size + 1);
431 if ((i = readlink(d.buf, buf.buf, buf.sz)) < 0) {
432 report(cp, CP_ERROR, 0, d.buf, "can't readlink: %e");
433 bad |= CP_ERROR;
434 break;
435 }
436 buf.buf[i] = 0;
437 report(cp, CP_SYMLINK, 2, d.buf, "symlink -> `%s'", buf.buf);
438
439 /* --- Handle sticky parents --- *
440 *
441 * If I make a symlink in a sticky directory, I can later modify it.
442 * However, nobody else can (except the owner of the directory, and
443 * we'll already have noticed that if we care).
444 */
445
446 if ((cp->cp_what & CP_WROTHUSR) &&
447 (sp->e_link->e_flags & f_sticky) &&
448 st.st_uid != cp->cp_uid && st.st_uid != 0) {
449 bad |= CP_WROTHUSR;
450 report(cp, CP_WROTHUSR, 1, d.buf,
451 "symlink modifiable by user %u", st.st_uid);
452 }
453
454 /* --- Sort out what to do from here --- */
455
456 if (buf.buf[0] == '/')
457 popall();
458 else
459 pop();
460 ee = splitpath(buf.buf, ee);
461 dstr_destroy(&buf);
462 continue;
463 }
464
465 /* --- Run the sanity check on this path element --- */
466
467 bad |= sanity(d.buf, &st, cp, ee ? 0 : f_last);
468
469 if (S_ISDIR(st.st_mode)) {
470 if (st.st_mode & 01000)
471 sp->e_flags |= f_sticky;
472 report(cp, CP_REPORT, 4, d.buf, "directory");
473 continue;
474 }
475
476 /* --- Something else I don't understand --- */
477
478 break;
479 }
480
481 /* --- Check for leftover junk --- */
482
483 if (ee) {
484 if (!(bad & CP_ERROR))
485 report(cp, CP_ERROR, 0, 0, "junk left over after reaching leaf");
486 while (ee) {
487 e = ee->e_link;
488 free(ee);
489 ee = e;
490 }
491 }
492
493 popall();
494 return (bad);
495 }
496
497 /* --- @path_setids@ --- *
498 *
499 * Arguments: @struct chkpath *cp@ = pointer to block to fill in
500 *
501 * Returns: Zero if OK, else @-1@.
502 *
503 * Use: Fills in the user ids and things in the structure.
504 */
505
506 void path_setids(struct chkpath *cp)
507 {
508 int n, i;
509 gid_t g = getgid();
510
511 cp->cp_uid = getuid();
512 n = getgroups(sizeof(cp->cp_gid) / sizeof(cp->cp_gid[0]), cp->cp_gid);
513
514 for (i = 0; i < n; i++) {
515 if (cp->cp_gid[i] == g)
516 goto gid_ok;
517 }
518 cp->cp_gid[n++] = g;
519 gid_ok:
520 cp->cp_gids = n;
521 }
522
523 /*----- That's all, folks -------------------------------------------------*/