3 * $Id: path.c,v 1.3 1999/05/21 22:07:20 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 --------------------------------------------------*
32 * Revision 1.3 1999/05/21 22:07:20 mdw
33 * Take advantage of new dynamic string macros.
35 * Revision 1.2 1999/05/18 20:49:12 mdw
36 * Use a dynamic string for reading symlinks.
38 * Revision 1.1.1.1 1999/04/06 20:12:07 mdw
43 /*----- Header files ------------------------------------------------------*/
51 #include <sys/types.h>
58 #include <mLib/alloc.h>
59 #include <mLib/dstr.h>
63 /*----- Data structures ---------------------------------------------------*/
65 /* --- An item in the directory list --- *
67 * Each directory becomes an element on a list which is manipulated in a
72 struct elt
*e_link
; /* Pointer to the next one along */
73 size_t e_offset
; /* Offset of name in path string */
74 unsigned e_flags
; /* Various useful flags */
75 char e_name
[1]; /* Name of the directory */
79 f_sticky
= 1 /* Directory has sticky bit set */
83 f_last
= 1 /* This is the final item to check */
86 /*----- Static variables --------------------------------------------------*/
88 static struct elt rootnode
= { 0, 0, 0 }; /* Root of the list */
89 static struct elt
*sp
; /* Stack pointer for list */
90 static dstr d
= DSTR_INIT
; /* Current path string */
92 /*----- Main code ---------------------------------------------------------*/
94 /* --- @splitpath@ --- *
96 * Arguments: @const char *path@ = path string to break apart
97 * @struct elt *tail@ = tail block to attach to end of list
99 * Returns: Pointer to the new list head.
101 * Use: Breaks a path string into directories and adds each one
102 * as a node on the list, in the right order. These can then
103 * be pushed onto the directory stack as required.
106 static struct elt
*splitpath(const char *path
, struct elt
*tail
)
108 struct elt
*head
, **ee
= &head
, *e
;
113 /* --- Either a leading `/', or a doubled one --- *
115 * Either way, ignore it.
123 /* --- Skip to the next directory separator --- *
125 * Build a list element for it, and link it on.
128 n
= strcspn(path
, "/");
129 e
= xmalloc(sizeof(struct elt
) + n
+ 1);
130 memcpy(e
->e_name
, path
, n
);
150 * Use: Removes the top item from the directory stack.
153 static void pop(void)
156 struct elt
*e
= sp
->e_link
;
157 d
.len
= sp
->e_offset
;
163 /* --- @popall@ --- *
169 * Use: Removes all the items from the directory stack.
172 static void popall(void)
180 * Arguments: @struct elt *e@ = pointer to directory element
184 * Use: Pushes a new subdirectory onto the stack.
187 static void push(struct elt
*e
)
192 DPUTS(&d
, e
->e_name
);
196 /* --- @report@ --- *
198 * Arguments: @struct chkpath *cp@ = pointer to context
199 * @int what@ = what sort of report is this?
200 * @int verbose@ = how verbose is this?
201 * @const char *p@ = what path does it refer to?
202 * @const char *msg@ = the message to give to the user
206 * Use: Formats and presents messages to the client.
209 static void report(struct chkpath
*cp
, int what
, int verbose
,
210 const char *p
, const char *msg
, ...)
212 /* --- Decide whether to bin this message --- */
214 if (!cp
->cp_report
|| verbose
> cp
->cp_verbose
|| !(cp
->cp_what
& what
))
217 /* --- Format the message nicely --- */
219 if (cp
->cp_what
& CP_REPORT
) {
230 dstr_putf(&d
, "Path: %s: ", p
);
236 dstr_puts(&d
, strerror(e
));
239 uid_t u
= (uid_t
)va_arg(ap
, int);
240 struct passwd
*pw
= getpwuid(u
);
242 dstr_putf(&d
, "`%s'", pw
->pw_name
);
244 dstr_putf(&d
, "%i", (int)u
);
247 gid_t g
= (gid_t
)va_arg(ap
, int);
248 struct group
*gr
= getgrgid(g
);
250 dstr_putf(&d
, "`%s'", gr
->gr_name
);
252 dstr_putf(&d
, "%i", (int)g
);
255 const char *s
= va_arg(ap
, const char *);
276 cp
->cp_report(what
, verbose
, p
, d
.buf
, cp
->cp_arg
);
280 cp
->cp_report(what
, verbose
, p
, 0, cp
->cp_arg
);
283 /* --- @sanity@ --- *
285 * Arguments: @const char *p@ = name of directory to check
286 * @struct stat *st@ = pointer to @stat@(2) block for it
287 * @struct chkpath *cp@ = pointer to caller parameters
288 * @unsigned f@ = various flags
290 * Returns: Zero if everything's OK, else bitmask of problems.
292 * Use: Performs the main load of sanity-checking on a directory.
295 static int sanity(const char *p
, struct stat
*st
,
296 struct chkpath
*cp
, unsigned f
)
299 int sticky
= (cp
->cp_what
& CP_STICKYOK
) || !(f
& f_last
) ?
01000 : 0;
301 /* --- Check for world-writability --- */
303 if ((cp
->cp_what
& CP_WRWORLD
) &&
304 (st
->st_mode
& (0002 | sticky
)) == 0002) {
306 report(cp
, CP_WRWORLD
, 1, p
, "** world writable **");
309 /* --- Check for group-writability --- */
311 if ((cp
->cp_what
& (CP_WRGRP
| CP_WROTHGRP
)) &&
312 (st
->st_mode
& (0020 | sticky
)) == 0020) {
313 if (cp
->cp_what
& CP_WRGRP
) {
315 report(cp
, CP_WRGRP
, 1, p
, "writable by group %g", st
->st_gid
);
318 for (i
= 0; i
< cp
->cp_gids
; i
++) {
319 if (st
->st_gid
== cp
->cp_gid
[i
])
323 report(cp
, CP_WROTHGRP
, 1, p
, "writable by group %g", st
->st_gid
);
328 /* --- Check for user-writability --- */
330 if ((cp
->cp_what
& CP_WROTHUSR
) &&
331 st
->st_uid
!= cp
->cp_uid
&&
334 report(cp
, CP_WROTHUSR
, 1, p
, "owner is user %u", st
->st_uid
);
337 /* --- Done sanity check --- */
342 /* --- @path_check@ --- *
344 * Arguments: @const char *p@ = directory name which needs checking
345 * @struct chkpath *cp@ = caller parameters for the check
347 * Returns: Zero if all is well, otherwise bitmask of problems.
349 * Use: Scrutinises a directory path to see what evil things other
350 * users could do to it.
353 int path_check(const char *p
, struct chkpath
*cp
)
360 /* --- Initialize stack pointer and path string --- */
365 /* --- Try to find the current directory --- */
367 if (!getcwd(cwd
, sizeof(cwd
))) {
368 report(cp
, CP_ERROR
, 0, 0, "can't find current directory: %e");
372 /* --- Check that the root directory is OK --- */
374 if (stat("/", &st
)) {
375 report(cp
, CP_ERROR
, 0, 0, "can't stat root directory: %e");
379 report(cp
, CP_REPORT
, 3, p
, "begin scan");
380 bad
|= sanity("/", &st
, cp
, 0);
382 /* --- Get the initial list of things to process --- */
384 ee
= splitpath(p
, 0);
386 ee
= splitpath(cwd
, ee
);
388 /* --- While there are list items which still need doing --- */
393 /* --- Strip off simple `.' elements --- */
395 if (strcmp(ee
->e_name
, ".") == 0) {
401 /* --- Backtrack on `..' elements --- */
403 else if (strcmp(ee
->e_name
, "..") == 0) {
410 /* --- Everything else gets pushed on the end --- */
415 /* --- Find out what sort of a thing this is --- */
417 if (lstat(d
.buf
, &st
)) {
418 report(cp
, CP_ERROR
, 0, d
.buf
, "can't stat: %e");
423 /* --- Handle symbolic links specially --- */
425 if (S_ISLNK(st
.st_mode
)) {
426 dstr buf
= DSTR_INIT
;
429 /* --- Resolve the link --- */
431 dstr_ensure(&buf
, st
.st_size
+ 1);
432 if ((i
= readlink(d
.buf
, buf
.buf
, buf
.sz
)) < 0) {
433 report(cp
, CP_ERROR
, 0, d
.buf
, "can't readlink: %e");
438 report(cp
, CP_SYMLINK
, 2, d
.buf
, "symlink -> `%s'", buf
.buf
);
440 /* --- Handle sticky parents --- *
442 * If I make a symlink in a sticky directory, I can later modify it.
443 * However, nobody else can (except the owner of the directory, and
444 * we'll already have noticed that if we care).
447 if ((cp
->cp_what
& CP_WROTHUSR
) &&
448 (sp
->e_link
->e_flags
& f_sticky
) &&
449 st
.st_uid
!= cp
->cp_uid
&& st
.st_uid
!= 0) {
451 report(cp
, CP_WROTHUSR
, 1, d
.buf
,
452 "symlink modifiable by user %u", st
.st_uid
);
455 /* --- Sort out what to do from here --- */
457 if (buf
.buf
[0] == '/')
461 ee
= splitpath(buf
.buf
, ee
);
466 /* --- Run the sanity check on this path element --- */
468 bad
|= sanity(d
.buf
, &st
, cp
, ee ?
0 : f_last
);
470 if (S_ISDIR(st
.st_mode
)) {
471 if (st
.st_mode
& 01000)
472 sp
->e_flags
|= f_sticky
;
473 report(cp
, CP_REPORT
, 4, d
.buf
, "directory");
477 /* --- Something else I don't understand --- */
482 /* --- Check for leftover junk --- */
485 if (!(bad
& CP_ERROR
))
486 report(cp
, CP_ERROR
, 0, 0, "junk left over after reaching leaf");
498 /* --- @path_setids@ --- *
500 * Arguments: @struct chkpath *cp@ = pointer to block to fill in
502 * Returns: Zero if OK, else @-1@.
504 * Use: Fills in the user ids and things in the structure.
507 void path_setids(struct chkpath
*cp
)
512 cp
->cp_uid
= getuid();
513 n
= getgroups(sizeof(cp
->cp_gid
) / sizeof(cp
->cp_gid
[0]), cp
->cp_gid
);
515 for (i
= 0; i
< n
; i
++) {
516 if (cp
->cp_gid
[i
] == g
)
524 /*----- That's all, folks -------------------------------------------------*/