3 * $Id: checkpath.c,v 1.5 2003/01/25 23:58:44 mdw Exp $
5 * Check a path for safety
7 * (c) 1999 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of chkpath.
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
31 * $Log: checkpath.c,v $
32 * Revision 1.5 2003/01/25 23:58:44 mdw
33 * Make guts into official library.
35 * Revision 1.4 2001/01/25 22:16:02 mdw
36 * Make flags be unsigned.
38 * Revision 1.3 1999/05/21 22:07:20 mdw
39 * Take advantage of new dynamic string macros.
41 * Revision 1.2 1999/05/18 20:49:12 mdw
42 * Use a dynamic string for reading symlinks.
44 * Revision 1.1.1.1 1999/04/06 20:12:07 mdw
49 /*----- Header files ------------------------------------------------------*/
57 #include <sys/types.h>
64 #include <mLib/alloc.h>
65 #include <mLib/dstr.h>
67 #include "checkpath.h"
69 /*----- Data structures ---------------------------------------------------*/
71 /* --- An item in the directory list --- *
73 * Each directory becomes an element on a list which is manipulated in a
78 struct elt *e_link; /* Pointer to the next one along */
79 size_t e_offset; /* Offset of name in path string */
80 unsigned e_flags; /* Various useful flags */
81 char e_name[1]; /* Name of the directory */
84 #define f_sticky 1u /* Directory has sticky bit set */
86 #define f_last 1u /* This is the final item to check */
88 /*----- Static variables --------------------------------------------------*/
90 static struct elt rootnode = { 0, 0, 0 }; /* Root of the list */
91 static struct elt *sp; /* Stack pointer for list */
92 static dstr d = DSTR_INIT; /* Current path string */
94 /*----- Main code ---------------------------------------------------------*/
96 /* --- @splitpath@ --- *
98 * Arguments: @const char *path@ = path string to break apart
99 * @struct elt *tail@ = tail block to attach to end of list
101 * Returns: Pointer to the new list head.
103 * Use: Breaks a path string into directories and adds each one
104 * as a node on the list, in the right order. These can then
105 * be pushed onto the directory stack as required.
108 static struct elt *splitpath(const char *path, struct elt *tail)
110 struct elt *head, **ee = &head, *e;
115 /* --- Either a leading `/', or a doubled one --- *
117 * Either way, ignore it.
125 /* --- Skip to the next directory separator --- *
127 * Build a list element for it, and link it on.
130 n = strcspn(path, "/");
131 e = xmalloc(sizeof(struct elt) + n + 1);
132 memcpy(e->e_name, path, n);
152 * Use: Removes the top item from the directory stack.
155 static void pop(void)
158 struct elt *e = sp->e_link;
159 d.len = sp->e_offset;
165 /* --- @popall@ --- *
171 * Use: Removes all the items from the directory stack.
174 static void popall(void)
182 * Arguments: @struct elt *e@ = pointer to directory element
186 * Use: Pushes a new subdirectory onto the stack.
189 static void push(struct elt *e)
194 DPUTS(&d, e->e_name);
198 /* --- @report@ --- *
200 * Arguments: @const struct checkpath *cp@ = pointer to context
201 * @unsigned what@ = what sort of report is this?
202 * @int verbose@ = how verbose is this?
203 * @const char *p@ = what path does it refer to?
204 * @const char *msg@ = the message to give to the user
208 * Use: Formats and presents messages to the client.
211 static void report(const struct checkpath *cp, unsigned what, int verbose,
212 const char *p, const char *msg, ...)
214 /* --- Decide whether to bin this message --- */
216 if (!cp->cp_report || verbose > cp->cp_verbose || !(cp->cp_what & what))
219 /* --- Format the message nicely --- */
221 if (cp->cp_what & CP_REPORT) {
232 dstr_putf(&d, "Path: %s: ", p);
238 dstr_puts(&d, strerror(e));
241 uid_t u = (uid_t)va_arg(ap, int);
242 struct passwd *pw = getpwuid(u);
244 dstr_putf(&d, "`%s'", pw->pw_name);
246 dstr_putf(&d, "%i", (int)u);
249 gid_t g = (gid_t)va_arg(ap, int);
250 struct group *gr = getgrgid(g);
252 dstr_putf(&d, "`%s'", gr->gr_name);
254 dstr_putf(&d, "%i", (int)g);
257 const char *s = va_arg(ap, const char *);
278 cp->cp_report(what, verbose, p, d.buf, cp->cp_arg);
282 cp->cp_report(what, verbose, p, 0, cp->cp_arg);
285 /* --- @sanity@ --- *
287 * Arguments: @const char *p@ = name of directory to check
288 * @struct stat *st@ = pointer to @stat@(2) block for it
289 * @const struct checkpath *cp@ = pointer to caller parameters
290 * @unsigned f@ = various flags
292 * Returns: Zero if everything's OK, else bitmask of problems.
294 * Use: Performs the main load of sanity-checking on a directory.
297 static unsigned sanity(const char *p, struct stat *st,
298 const struct checkpath *cp, unsigned f)
303 if (S_ISDIR(st->st_mode) &&
304 (!(f & f_last) || (cp->cp_what & CP_STICKYOK)))
307 /* --- Check for world-writability --- */
309 if ((cp->cp_what & CP_WRWORLD) &&
310 (st->st_mode & (0002 | stickyok)) == 0002) {
312 report(cp, CP_WRWORLD, 1, p, "** world writable **");
315 /* --- Check for group-writability --- */
317 if ((cp->cp_what & (CP_WRGRP | CP_WROTHGRP)) &&
318 (st->st_mode & (0020 | stickyok)) == 0020) {
320 unsigned b = CP_WRGRP;
322 if (cp->cp_what & CP_WROTHGRP) {
324 for (i = 0; i < cp->cp_gids; i++) {
325 if (st->st_gid == cp->cp_gid[i])
326 b = cp->cp_what & CP_WRGRP;
331 report(cp, b, 1, p, "writable by %sgroup %g",
332 (b == CP_WROTHGRP) ? "other " : "", st->st_gid);
336 /* --- Check for user-writability --- */
338 if ((cp->cp_what & CP_WROTHUSR) &&
339 st->st_uid != cp->cp_uid &&
342 report(cp, CP_WROTHUSR, 1, p, "owner is user %u", st->st_uid);
345 /* --- Done sanity check --- */
350 /* --- @checkpath@ --- *
352 * Arguments: @const char *p@ = directory name which needs checking
353 * @const struct checkpath *cp@ = parameters for the check
355 * Returns: Zero if all is well, otherwise bitmask of problems.
357 * Use: Scrutinises a directory path to see what evil things other
358 * users could do to it.
361 unsigned checkpath(const char *p, const struct checkpath *cp)
368 /* --- Initialize stack pointer and path string --- */
373 /* --- Try to find the current directory --- */
375 if (!getcwd(cwd, sizeof(cwd))) {
376 report(cp, CP_ERROR, 0, 0, "can't find current directory: %e");
380 /* --- Check that the root directory is OK --- */
382 if (stat("/", &st)) {
383 report(cp, CP_ERROR, 0, 0, "can't stat root directory: %e");
387 report(cp, CP_REPORT, 3, p, "begin scan");
388 bad |= sanity("/", &st, cp, 0);
390 /* --- Get the initial list of things to process --- */
392 ee = splitpath(p, 0);
394 ee = splitpath(cwd, ee);
396 /* --- While there are list items which still need doing --- */
401 /* --- Strip off simple `.' elements --- */
403 if (strcmp(ee->e_name, ".") == 0) {
409 /* --- Backtrack on `..' elements --- */
411 else if (strcmp(ee->e_name, "..") == 0) {
418 /* --- Everything else gets pushed on the end --- */
423 /* --- Find out what sort of a thing this is --- */
425 if (lstat(d.buf, &st)) {
426 report(cp, CP_ERROR, 0, d.buf, "can't stat: %e");
431 /* --- Handle symbolic links specially --- */
433 if (S_ISLNK(st.st_mode)) {
434 dstr buf = DSTR_INIT;
437 /* --- Resolve the link --- */
439 dstr_ensure(&buf, st.st_size + 1);
440 if ((i = readlink(d.buf, buf.buf, buf.sz)) < 0) {
441 report(cp, CP_ERROR, 0, d.buf, "can't readlink: %e");
446 report(cp, CP_SYMLINK, 2, d.buf, "symlink -> `%s'", buf.buf);
448 /* --- Handle sticky parents --- *
450 * If I make a symlink in a sticky directory, I can later modify it.
451 * However, nobody else can (except the owner of the directory, and
452 * we'll already have noticed that if we care).
455 if ((cp->cp_what & CP_WROTHUSR) &&
456 (sp->e_link->e_flags & f_sticky) &&
457 st.st_uid != cp->cp_uid && st.st_uid != 0) {
459 report(cp, CP_WROTHUSR, 1, d.buf,
460 "symlink modifiable by user %u", st.st_uid);
463 /* --- Sort out what to do from here --- */
465 if (buf.buf[0] == '/')
469 ee = splitpath(buf.buf, ee);
474 /* --- Run the sanity check on this path element --- */
476 bad |= sanity(d.buf, &st, cp, ee ? 0 : f_last);
478 if (S_ISDIR(st.st_mode)) {
479 if (st.st_mode & 01000)
480 sp->e_flags |= f_sticky;
481 report(cp, CP_REPORT, 4, d.buf, "directory");
485 /* --- Something else I don't understand --- */
490 /* --- Check for leftover junk --- */
493 if (!(bad & CP_ERROR))
494 report(cp, CP_ERROR, 0, 0, "junk left over after reaching leaf");
506 /* --- @checkpath_setids@ --- *
508 * Arguments: @struct checkpath *cp@ = pointer to block to fill in
512 * Use: Fills in the user ids and things in the structure.
515 void checkpath_setids(struct checkpath *cp)
520 cp->cp_uid = getuid();
521 n = getgroups(sizeof(cp->cp_gid) / sizeof(cp->cp_gid[0]), cp->cp_gid);
523 for (i = 0; i < n; i++) {
524 if (cp->cp_gid[i] == g)
532 /*----- That's all, folks -------------------------------------------------*/