source: trunk/src/trustfile.c@ 222

Last change on this file since 222 was 221, checked in by katerina, 16 years ago

Fix MacOS X problems (ticket #148).

File size: 27.6 KB
Line 
1/* debug problems */
2/* #define TRUST_DEBUG */
3
4/* switch off full check */
5/* #define TEST_ONLY */
6
7/* standalone */
8/* #define TRUST_MAIN */
9/* $(CC) -DTRUST_MAIN -DSL_ALWAYS_TRUSTED=... */
10
11/* LINTLIBRARY */
12/*
13 * This is the file with all the library routines in it
14 *
15 * Author information:
16 * Matt Bishop
17 * Department of Computer Science
18 * University of California at Davis
19 * Davis, CA 95616-8562
20 * phone (916) 752-8060
21 * email bishop@cs.ucdavis.edu
22 *
23 * This code is placed in the public domain. I do ask that
24 * you keep my name associated with it, that you not represent
25 * it as written by you, and that you preserve these comments.
26 * This software is provided "as is" and without any guarantees
27 * of any sort.
28 *
29 * Compilation notes:
30 * * this does NOT use malloc(3), but fixed storage. this means we
31 * do lots of bounds checking, but it still is faster, and smaller,
32 * than forcing inclusion of malloc. All buffers etc. are of size
33 * MAXFILENAME (defined in trustfile.h); to get more room, recompile
34 * with this set larger.
35 * * if you support the following directory semantics, define STICKY;
36 * otherwise, undefine it
37 * "if a directory is both world-writeable AND has the sticky bit
38 * set, then ONLY the owner of an existing file may delete it"
39 * On some systems (eg, IRIX), you can delete the file under these
40 * conditions if the file is world writeable. Foor our purposes,
41 * this is irrelevant since if the file is world-writeable it is
42 * untrustworthy; either it can be replaced with another file (the
43 * IRIX version) or it can be altered (all versions).
44 * if this is true and STICKY is not set, the sticky bit is ignored
45 * and the directory will be flagged as untrustworthy, even when only
46 * a trusted user could delete the file
47 * * this uses a library call to get the name of the current working
48 * directory. Define the following to get the various versions:
49 * GETCWD for Solaris 2.x, SunOS 4.1.x, IRIX 5.x
50 * char *getcwd(char *buf, int bufsz);
51 * where buf is a buffer for the path name, and bufsz is
52 * the size of the buffer; if the size if too small, you
53 * get an error return (NULL)
54 * GETWD for Ultrix 4.4
55 * char *getwd(char *buf)
56 * where buf is a buffer for the path name, and it is
57 * assumed to be at lease as big as MAXPATHLEN.
58 * *** IMPORTANT NOTE ***
59 * Ultrix supports getcwd as well, but it uses popen to
60 * run the command "pwd" (according to the manual). This
61 * means it's vulnerable to a number of attacks if used
62 * in a privileged program. YOU DON'T WANT THIS.
63 * * the debugging flag DEBUG prints out each step of the file name
64 * checking, as well as info on symbolic links (if S_IFLNK defined),
65 * file name canonicalization, and user, group, and permission for
66 * each file or directory; this is useful if you want to be sure
67 * you're checking the right file
68 *
69 * Version information:
70 * 1.0 December 28, 1995 Matt Bishop
71 *
72 * 2.0 March 26, 2000 Rainer Wichmann -- adapted for slib.
73 */
74
75/* --- Why strcpy is safe here: ---- */
76
77/* The input path is checked once, and then either shortened [in dirz()],
78 * or safely expanded (symlinks) with bound checking.
79 * I.e., the path length can never exceed (MAXFILENAME-1), while the path
80 * is always copied between buffers of length MAXFILENAME.
81 */
82
83#ifndef TRUST_MAIN
84#include "config_xor.h"
85#include "sh_calls.h"
86#else
87#define UID_CAST long
88#define HAVE_GETPWENT
89#define SH_MUTEX_LOCK(a) ((void)0)
90#define SH_MUTEX_UNLOCK(a) ((void)0)
91#endif
92
93#include <stdio.h>
94#include <stdlib.h>
95#include <sys/types.h>
96#include <sys/stat.h>
97#include <grp.h>
98#include <pwd.h>
99#include <string.h>
100#include <unistd.h>
101
102
103#ifndef TRUST_MAIN
104
105#include "slib.h"
106#define SH_NEED_PWD_GRP 1
107#include "sh_static.h"
108#include "sh_pthread.h"
109
110#else
111
112#define sh_getgrgid getgrgid
113#define sh_getgrgid_r getgrgid_r
114#define sh_getpwnam getpwnam
115#define sh_getpwnam_r getpwnam_r
116#define sh_getpwuid getpwuid
117#define sh_getpwuid_r getpwuid_r
118#define sh_getpwent getpwent
119#define sh_endpwent endpwent
120
121#define TRUST_DEBUG
122#define SL_FALSE 0
123#define SL_TRUE 1
124#define SL_ENTER(string)
125#define SL_IRETURN(a, b) return a
126#define retry_lstat(a,b,c,d) lstat(c,d)
127#define _(string) string
128#define N_(string) string
129#define MAXFILENAME 4096
130static int sl_errno = 0;
131#define SL_ENONE 0
132#define SL_ENULL -1024 /* Invalid use of NULL pointer. */
133#define SL_ERANGE -1025 /* Argument out of range. */
134#define SL_ETRUNC -1026 /* Result truncated. */
135#define SL_EINTERNAL -1028 /* Internal error. */
136#define SL_EBADFILE -1030 /* File access error. Check errno. */
137#define SL_EBADNAME -1040 /* Invalid name. */
138#define SL_ESTAT -1041 /* stat of file failed. Check errno. */
139#define SL_EBADUID -1050 /* Owner not trustworthy. */
140#define SL_EBADGID -1051 /* Group writeable and not trustworthy.*/
141#define SL_EBADOTH -1052 /* World writeable. */
142
143#endif
144
145
146#if defined(__linux__) || defined(__FreeBSD__)
147#define STICKY
148#endif
149
150#undef FIL__
151#define FIL__ _("trustfile.c")
152
153/*
154 * the get current working directory function
155 * every version of UNIX seems to have its own
156 * idea of how to do this, so we group them by
157 * arguments ...
158 * all must return a pointer to the right name
159 */
160
161
162#ifndef TRUST_MAIN
163
164#if defined(HAVE_GETCWD) && !defined(HAVE_BROKEN_GETCWD)
165#define CURDIR(buf,nbuf) getcwd((buf), (nbuf))
166#elif defined(HAVE_GETWD)
167#define CURDIR(buf,nbuf) getwd((buf))
168#endif
169
170#else
171
172#define CURDIR(buf,nbuf) getcwd((buf), (nbuf))
173
174#endif
175
176
177
178/*
179 * this checks to see if there are symbolic links
180 * assumes the link bit in the protection mask is called S_IFLNK
181 * (seems to be true on all UNIXes with them)
182 */
183#ifndef S_IFLNK
184#define lstat stat
185#endif
186
187
188/*
189 * these are useful global variables
190 *
191 * first set: who you gonna trust, by default?
192 * if the user does not specify a trusted or untrusted set of users,
193 * all users are considered untrusted EXCEPT:
194 * UID 0 -- root as root can do anything on most UNIX systems, this
195 * seems reasonable
196 * tf_euid -- programmer-selectable UID
197 * if the caller specifies a specific UID by putting
198 * it in this variable, it will be trusted; this is
199 * typically used to trust the effective UID of the
200 * process (note: NOT the real UID, which will cause all
201 * sorts of problems!) By default, this is set to -1,
202 * so if it's not set, root is the only trusted user
203 */
204
205/* modified Tue Feb 22 10:36:44 NFT 2000 Rainer Wichmann */
206
207
208#ifndef SL_ALWAYS_TRUSTED
209#define SL_ALWAYS_TRUSTED 0
210#endif
211static uid_t test_rootonly[] = { SL_ALWAYS_TRUSTED };
212
213#define tf_uid_neg ((uid_t)-1)
214
215uid_t rootonly[] = { SL_ALWAYS_TRUSTED,
216 tf_uid_neg, tf_uid_neg, tf_uid_neg, tf_uid_neg,
217 tf_uid_neg, tf_uid_neg, tf_uid_neg, tf_uid_neg,
218 tf_uid_neg, tf_uid_neg, tf_uid_neg, tf_uid_neg,
219 tf_uid_neg, tf_uid_neg, tf_uid_neg, tf_uid_neg };
220
221uid_t tf_euid = tf_uid_neg;
222int EUIDSLOT = sizeof(test_rootonly)/sizeof(uid_t);
223int ORIG_EUIDSLOT = sizeof(test_rootonly)/sizeof(uid_t);
224
225char tf_path[MAXFILENAME]; /* error path for trust function */
226uid_t tf_baduid;
227gid_t tf_badgid;
228
229static
230int dirz(char *path)
231{
232 register char *p = path;/* points to rest of path to clean up */
233 register char *q; /* temp pointer for skipping over stuff */
234
235 static char swp[MAXFILENAME];
236
237 SL_ENTER(_("dirz"));
238 /*
239 * standard error checking
240 */
241 if (path == NULL)
242 SL_IRETURN(SL_ENULL, _("dirz"));
243 if (path[0] == '.')
244 SL_IRETURN(SL_EINTERNAL, _("dirz"));
245
246
247 /*
248 * loop over the path name until everything is checked
249 */
250 while(*p)
251 {
252 /* skip
253 */
254 if (*p != '/')
255 {
256 p++;
257 continue;
258 }
259
260 /* "/./" or "/."
261 */
262 if (p[1] == '.' && (p[2] == '/' || p[2] == '\0'))
263 {
264 /* yes -- delete "/."
265 */
266 (void) strcpy(swp, &p[2]); /* known to fit */
267 (void) strcpy(p, swp); /* known to fit */
268
269 /* special case "/." as full path name
270 */
271 if (p == path && *p == '\0')
272 {
273 *p++ = '/';
274 *p = '\0';
275 }
276 }
277
278 /* "//"
279 */
280 else if (p[1] == '/')
281 {
282 /* yes -- skip
283 */
284 for(q = &p[2]; *q == '/'; q++)
285 ;
286 (void) strcpy(swp, q); /* known to fit */
287 (void) strcpy(&p[1], swp); /* known to fit */
288 }
289
290 /* "/../" or "/.."
291 */
292 else if (p[1] == '.' && p[2] == '.' && (p[3] == '/' || p[3] == '\0'))
293 {
294 /* yes -- if it's root, delete .. only
295 */
296 if (p == path)
297 {
298 (void) strcpy(swp, &p[3]); /* known to fit */
299 (void) strcpy(p, swp); /* known to fit */
300 }
301 else
302 {
303 /* back up over previous component
304 */
305 q = p - 1;
306 while(q != path && *q != '/')
307 q--;
308 /* now wipe it out
309 */
310 (void) strcpy(swp, &p[3]); /* known to fit */
311 (void) strcpy(q, swp); /* known to fit */
312 p = q;
313 }
314 }
315 else
316 p++;
317 }
318 SL_IRETURN(SL_ENONE, _("dirz"));
319}
320
321
322
323/* not static to circumvent stupid gcc 4 bug */
324int getfname(const char *fname, char *rbuf, int rsz)
325{
326#ifndef TRUST_MAIN
327 register int status;
328#endif
329
330 SL_ENTER(_("getfname"));
331 /*
332 * do the initial checking
333 * NULL pointer
334 */
335 if (fname == NULL || rbuf == NULL)
336 SL_IRETURN(SL_ENULL, _("getfname"));
337 if (rsz <= 0)
338 SL_IRETURN(SL_ERANGE, _("getfname"));
339
340
341 /* already a full path name */
342 if (*fname == '/')
343 rbuf[0] = '\0';
344 else
345 {
346 if (CURDIR(rbuf, rsz) == NULL)
347 {
348#ifdef TRUST_DEBUG
349 fprintf(stderr, "trustfile: getcwd failed\n");
350#endif
351 SL_IRETURN(SL_EBADNAME, _("getfname"));
352 }
353 }
354
355 /*
356 * append the file name and reduce
357 */
358 if (fname != NULL && *fname != '\0')
359 {
360#ifndef TRUST_MAIN
361 status = sl_strlcat(rbuf, "/", rsz);
362 if (status == SL_ENONE)
363 status = sl_strlcat(rbuf, fname, rsz);
364 if (status != SL_ENONE)
365 SL_IRETURN(status, _("getfname"));
366#else
367 strncat(rbuf, "/", rsz-strlen(rbuf)-1);
368 rbuf[rsz-1] = '\0';
369 strncat(rbuf, fname, rsz-strlen(rbuf)-1);
370 rbuf[rsz-1] = '\0';
371#endif
372 }
373 SL_IRETURN(dirz(rbuf), _("getfname"));
374}
375
376static
377int isin(uid_t n, uid_t *list)
378{
379 SL_ENTER(_("isin"));
380 if (list == NULL)
381 SL_IRETURN(SL_FALSE, _("isin"));
382
383 while(*list != tf_uid_neg && *list != n)
384 {
385#ifdef TRUST_DEBUG
386 fprintf (stderr,
387 "trustfile: owner_uid=%ld, trusted uid=%ld, no match\n",
388 (UID_CAST) n, (UID_CAST) *list);
389#endif
390 list++;
391 }
392
393 if (*list == tf_uid_neg)
394 {
395#ifdef TRUST_DEBUG
396 fprintf (stderr,
397 "trustfile: owner_uid=%ld, no match with any trusted user --> ERROR\n",
398 (UID_CAST) n);
399#endif
400 SL_IRETURN(SL_FALSE, _("isin"));
401 }
402
403#ifdef TRUST_DEBUG
404 fprintf (stderr,
405 "trustfile: owner_uid=%ld, trusted_uid=%ld, match found --> OK\n",
406 (UID_CAST)n, (UID_CAST)*list);
407#endif
408 SL_IRETURN(SL_TRUE, _("isin"));
409}
410
411/* comment added by R. Wichmann
412 * RETURN TRUE if ANYONE in ulist is group member
413 */
414/* not static to circumvent stupid gcc 4 bug */
415int isingrp(gid_t grp, uid_t *ulist)
416{
417 struct passwd *w; /* info about group member */
418 register uid_t *u; /* points to current ulist member */
419 register char **p; /* points to current group member */
420 struct group *g; /* pointer to group information */
421
422#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
423 struct group gr;
424 char buffer[SH_GRBUF_SIZE];
425 struct passwd pwd;
426 char pbuffer[SH_PWBUF_SIZE];
427#endif
428
429 SL_ENTER(_("isingrp"));
430
431#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
432 sh_getgrgid_r(grp, &gr, buffer, sizeof(buffer), &g);
433#else
434 g = sh_getgrgid(grp);
435#endif
436
437 if (g == NULL)
438 {
439 SL_IRETURN(SL_FALSE, _("isingrp") );
440 }
441 /*
442 if(g->gr_mem == NULL || g->gr_mem[0] == NULL )
443 SL_IRETURN(SL_FALSE, _("isingrp") );
444 */
445
446 /* this will return at the first match
447 */
448 for(p = g->gr_mem; *p != NULL; p++)
449 {
450 for(u = ulist; *u != tf_uid_neg; u++)
451 {
452 /* map user name to UID and compare */
453#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
454 sh_getpwnam_r(*p, &pwd, pbuffer, sizeof(pbuffer), &w);
455#else
456 w = sh_getpwnam(*p);
457#endif
458
459#ifdef TRUST_MAIN
460 if (w != NULL && *u == (uid_t)(w->pw_uid) )
461 SL_IRETURN(SL_TRUE, _("isingrp"));
462#else
463 if (w != NULL && *u == (uid_t)(w->pw_uid) )
464 {
465 SL_IRETURN(SL_TRUE, _("isingrp"));
466 }
467#endif
468 }
469 }
470 /* added by R. Wichmann Fri Mar 30 08:16:14 CEST 2001:
471 * a user can have a GID but no entry in /etc/group
472 */
473 for(u = ulist; *u != tf_uid_neg; u++)
474 {
475#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
476 sh_getpwuid_r(*u, &pwd, pbuffer, sizeof(pbuffer), &w);
477#else
478 w = sh_getpwuid(*u);
479#endif
480#ifdef TRUST_MAIN
481 if (w != NULL && grp == (gid_t)(w->pw_gid) )
482 SL_IRETURN(SL_TRUE, _("isingrp"));
483#else
484 if (w != NULL && grp == (gid_t)(w->pw_gid) )
485 {
486 SL_IRETURN(SL_TRUE, _("isingrp"));
487 }
488#endif
489 }
490
491 SL_IRETURN(SL_FALSE, _("isingrp"));
492}
493
494/* added by R. Wichmann Fri Mar 30 08:16:14 CEST 2001
495 * RETURN TRUE only if ALL group members are trusted
496 */
497/* not static to circumvent stupid gcc 4 bug */
498int onlytrustedingrp(gid_t grp, uid_t *ulist)
499{
500 struct passwd *w; /* info about group member */
501 register uid_t *u; /* points to current ulist member */
502 register char **p; /* points to current group member */
503 struct group *g; /* pointer to group information */
504 register int flag = -1; /* group member found */
505
506#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
507 struct group gr;
508 char buffer[SH_GRBUF_SIZE];
509 struct passwd pw;
510 char pbuffer[SH_PWBUF_SIZE];
511#endif
512
513 int retval = SL_FALSE;
514
515 SL_ENTER(_("onlytrustedingrp"));
516
517#ifdef TRUST_DEBUG
518 fprintf(stderr, "trustfile: group writeable, group_gid: %ld\n",
519 (UID_CAST)grp);
520#endif
521
522#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
523 sh_getgrgid_r(grp, &gr, buffer, sizeof(buffer), &g);
524#else
525 g = sh_getgrgid(grp);
526#endif
527
528 if (g == NULL)
529 {
530#ifdef TRUST_DEBUG
531 fprintf(stderr,
532 "trustfile: group_gid: %ld, no such group --> ERROR\n",
533 (UID_CAST)grp);
534#endif
535 SL_IRETURN(SL_FALSE, _("onlytrustedingrp") );
536 }
537
538 /* empty group -> no problem
539
540 if(g->gr_mem == NULL || g->gr_mem[0] == NULL )
541 SL_IRETURN(SL_TRUE, _("onlytrustedingrp") );
542 */
543
544 /* check for untrusted members of the group
545 */
546 for(p = g->gr_mem; *p != NULL; p++)
547 {
548 flag = -1;
549#ifdef TRUST_DEBUG
550 fprintf(stderr, "trustfile: group_member: %s\n", *p);
551#endif
552 /* map user name to UID and compare
553 */
554#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
555 sh_getpwnam_r(*p, &pw, pbuffer, sizeof(pbuffer), &w);
556#else
557 w = sh_getpwnam(*p);
558#endif
559
560 if (w == NULL) /* not a valid user, ignore */
561 {
562 flag = 0;
563 }
564 else /* check list of trusted users */
565 {
566#ifdef TRUST_DEBUG
567 fprintf (stderr,
568 "trustfile: uid=%ld, checking whether it is trusted\n",
569 (UID_CAST)(w->pw_uid));
570#endif
571 for(u = ulist; *u != tf_uid_neg; u++)
572 {
573 if (*u == (w->pw_uid) )
574 {
575#ifdef TRUST_DEBUG
576 fprintf (stderr,
577 "trustfile: uid=%ld, trusted_uid=%ld, match found --> OK\n",
578 (UID_CAST)(w->pw_uid), (UID_CAST)*u);
579#endif
580 flag = 0;
581 break;
582 }
583 else
584 {
585#ifdef TRUST_DEBUG
586 fprintf (stderr,
587 "trustfile: uid=%ld, trusted_uid=%ld, no match\n",
588 (UID_CAST)(w->pw_uid), (UID_CAST)*u);
589#endif
590 ;
591 }
592 }
593 }
594 /* not found
595 */
596 if (flag == -1)
597 {
598#ifdef TRUST_DEBUG
599 fprintf (stderr,
600 "trustfile: user=%s (gid %ld), not a trusted user --> ERROR\n", *p, (UID_CAST)grp);
601#endif
602 tf_baduid = w->pw_uid;
603 SL_IRETURN(SL_FALSE, _("onlytrustedingrp"));
604 }
605 }
606
607#ifndef TEST_ONLY
608#ifdef HAVE_GETPWENT
609 /* now check ALL users for their GID !!!
610 */
611 SH_MUTEX_LOCK(mutex_pwent);
612
613 while (NULL != (w = sh_getpwent()))
614 {
615 if (grp == (gid_t)(w->pw_gid))
616 {
617#ifdef TRUST_DEBUG
618 fprintf(stderr, "trustfile: checking group member %s, uid %ld\n",
619 w->pw_name, (UID_CAST)w->pw_uid);
620#endif
621 /* is it a trusted user ?
622 */
623 flag = -1;
624 for(u = ulist; *u != tf_uid_neg; u++)
625 {
626 if (*u == (uid_t)(w->pw_uid))
627 {
628#ifdef TRUST_DEBUG
629 fprintf (stderr,
630 "trustfile: uid=%ld, trusted_uid=%ld, match found --> OK\n",
631 (UID_CAST)(w->pw_uid), (UID_CAST)(*u));
632#endif
633 flag = 0;
634 break;
635 }
636 else
637 {
638#ifdef TRUST_DEBUG
639 fprintf (stderr,
640 "trustfile: uid=%ld, trusted_uid=%ld, no match\n",
641 (UID_CAST)(w->pw_uid), (UID_CAST)*u);
642#endif
643 ;
644 }
645 }
646 /* not found */
647 if (flag == -1)
648 {
649#ifdef TRUST_DEBUG
650 fprintf(stderr,"trustfile: group member %s not found in trusted users --> ERROR\n", w->pw_name);
651#endif
652 tf_baduid = w->pw_uid;
653 retval = SL_FALSE;
654 goto out;
655 /* SL_IRETURN(SL_FALSE, _("onlytrustedingrp")); */
656 }
657 }
658 }
659 retval = SL_TRUE;
660
661 out:
662
663#ifdef HAVE_ENDPWENT
664 sh_endpwent();
665#endif
666
667 SH_MUTEX_UNLOCK(mutex_pwent);
668
669 /* TEST_ONLY */
670#endif
671 /* #ifdef HAVE_GETPWENT */
672#endif
673
674#ifdef TRUST_DEBUG
675 if (retval == SL_TRUE)
676 fprintf(stderr,
677 "trustfile: group %ld: all members are trusted users --> OK\n",
678 (UID_CAST)grp);
679#endif
680 /* all found
681 */
682 SL_IRETURN(retval, _("onlytrustedingrp"));
683}
684
685int sl_trustfile(const char *fname, uid_t *okusers, uid_t *badusers)
686{
687 char fexp[MAXFILENAME]; /* file name fully expanded */
688 register char *p = fexp; /* used to hold name to be checked */
689 struct stat stbuf; /* used to check file permissions */
690 char c; /* used to hold temp char */
691
692 SL_ENTER(_("sl_trustfile"));
693 if (fname == NULL)
694 SL_IRETURN(SL_EBADFILE, _("sl_trustfile"));
695
696 /*
697 * next expand to the full file name
698 * getfname sets sl_errno as appropriate
699 */
700#ifdef TRUST_MAIN
701 sl_errno = getfname(fname, fexp, MAXFILENAME);
702 if (sl_errno != 0)
703 return sl_errno;
704#else
705 if (SL_ISERROR(getfname(fname, fexp, MAXFILENAME)))
706 SL_IRETURN(sl_errno, _("sl_trustfile"));
707#endif
708
709 if (okusers == NULL && badusers == NULL)
710 {
711 okusers = rootonly;
712 rootonly[EUIDSLOT] = tf_euid;
713 }
714
715 /*
716 * now loop through the path a component at a time
717 * note we have to special-case root
718 */
719 while(*p)
720 {
721 /*
722 * get next component
723 */
724 while(*p && *p != '/')
725 p++;
726
727 /* save where you are
728 */
729 if (p == fexp)
730 {
731 /* keep the / if it's the root dir
732 */
733 c = p[1];
734 p[1] = '\0';
735 }
736 else
737 {
738 /* clobber the / if it isn't the root dir
739 */
740 c = *p;
741 *p = '\0';
742 }
743
744 /*
745 * now get the information
746 */
747 if (retry_lstat(FIL__, __LINE__, fexp, &stbuf) < 0)
748 {
749 (void) strncpy(tf_path, fexp, sizeof(tf_path));
750 tf_path[sizeof(tf_path)-1] = '\0';
751#ifdef TRUST_MAIN
752 fprintf(stderr, "---------------------------------------------\n");
753 fprintf(stderr, "trustfile: ESTAT: stat(%s) failed,\n", fexp);
754 fprintf(stderr, "maybe the file does not exist\n");
755 fprintf(stderr, "---------------------------------------------\n");
756#endif
757 SL_IRETURN(SL_ESTAT, _("sl_trustfile"));
758 }
759
760#ifdef S_IFLNK
761 /*
762 * if it's a symbolic link, recurse
763 */
764 if ((stbuf.st_mode & S_IFLNK) == S_IFLNK)
765 {
766 /*
767 * this is tricky
768 * if the symlink is to an absolute path
769 * name, just call trustfile on it; but
770 * if it's a relative path name, it's
771 * interpreted WRT the current working
772 * directory AND NOT THE FILE NAME!!!!!
773 * so, we simply put /../ at the end of
774 * the file name, then append the symlink
775 * contents; trustfile will canonicalize
776 * this, and the /../ we added "undoes"
777 * the name of the symlink to put us in
778 * the current working directory, at
779 * which point the symlink contents (appended
780 * to the CWD) are interpreted correctly.
781 * got it?
782 */
783 char csym[MAXFILENAME]; /* contents of symlink file */
784 char full[MAXFILENAME]; /* "full" name of symlink */
785 register char *b, *t; /* used to copy stuff around */
786 register int lsym; /* num chars in symlink ref */
787 register int i; /* trustworthy or not? */
788 const char * t_const;
789 char *end;
790
791 /*
792 * get what the symbolic link points to
793 *
794 * The original code does not check the return code of readlink(),
795 * and has an off-by-one error
796 * (MAXFILENAME instead of MAXFILENAME-1)
797 * R.W. Tue May 29 22:05:16 CEST 2001
798 */
799 lsym = readlink(fexp, csym, MAXFILENAME-1);
800 if (lsym >= 0)
801 csym[lsym] = '\0';
802 else
803 {
804#ifdef TRUST_MAIN
805 fprintf(stderr, "---------------------------------------------\n");
806 fprintf(stderr, "trustfile: EBADNAME: readlink(%s) failed\n",
807 fexp);
808 fprintf(stderr, "---------------------------------------------\n");
809#endif
810 SL_IRETURN(SL_EBADNAME, _("sl_trustfile"));
811 }
812
813 /*
814 * relative or absolute referent?
815 */
816 if (csym[0] != '/')
817 {
818 /* pointer to one above last element
819 */
820 end = &full[MAXFILENAME-1]; ++end;
821
822 /* initialize pointers
823 */
824 b = full;
825
826 /* copy in base path
827 */
828 t = fexp;
829 while(*t && b < end)
830 *b++ = *t++;
831
832 /* smack on the /../
833 */
834 t_const = "/../"; t = (char *)t_const;
835 while(*t && b < end)
836 *b++ = *t++;
837
838 /* append the symlink referent
839 */
840 t = csym;
841 while(*t && b < end)
842 *b++ = *t++;
843
844 /* see if we're too big
845 */
846 if (*t || b == end)
847 {
848 /* yes -- error
849 */
850 (void) strncpy(tf_path, fexp, sizeof(tf_path));
851 tf_path[sizeof(tf_path)-1] = '\0';
852#ifdef TRUST_MAIN
853 fprintf(stderr, "---------------------------------------------\n");
854 fprintf(stderr,
855 "trustfile: ETRUNC: normalized path too long (%s)\n",
856 fexp);
857 fprintf(stderr, "---------------------------------------------\n");
858#endif
859 SL_IRETURN(SL_ETRUNC, _("sl_trustfile"));
860 }
861 *b = '\0';
862 }
863 else
864 {
865 /* absolute -- just copy */
866 /* overflow can't occur as the arrays */
867 /* are the same size */
868 (void) strcpy(full, csym); /* known to fit */
869 }
870 /*
871 * now check out this file and its ancestors
872 */
873 if ((i = sl_trustfile(full, okusers, badusers)) != SL_ENONE)
874 SL_IRETURN(i, _("sl_trustfile"));
875
876 /*
877 * okay, this part is valid ... let's check the rest
878 * put the / back
879 */
880 if (p == fexp)
881 {
882 /* special case for root */
883 p[1] = c;
884 p++;
885 }
886 else
887 {
888 /* ordinary case for everything else */
889 *p = c;
890 if (*p)
891 p++;
892 }
893 continue;
894 }
895#endif
896
897
898#ifdef TRUST_DEBUG
899 fprintf(stderr, "\ntrustfile: checking path=%s\n", fexp);
900#endif
901 /*
902 * if the owner is not trusted then -- as the owner can
903 * change protection modes -- he/she can write to the
904 * file regardless of permissions, so bomb
905 */
906 if (((okusers != NULL && SL_FALSE == isin((uid_t)stbuf.st_uid,okusers))||
907 (badusers != NULL && SL_TRUE == isin((uid_t)stbuf.st_uid,badusers))))
908 {
909#ifdef TRUST_DEBUG
910 fprintf(stderr, "---------------------------------------------\n");
911 fprintf(stderr, "trustfile: EBADUID %s (owner not trusted)\n",
912 fexp);
913 fprintf(stderr, "The owner of this file/directory is not in samhains\n");
914 fprintf(stderr, "list of trusted users.\n");
915 fprintf(stderr, "Please run ./configure again with the option\n");
916 fprintf(stderr, " ./configure [more options] --with-trusted=0,...,UID\n");
917 fprintf(stderr, "where UID is the UID of the (yet) untrusted user.\n");
918 fprintf(stderr, "---------------------------------------------\n");
919#endif
920 (void) strncpy(tf_path, fexp, sizeof(tf_path));
921 tf_path[sizeof(tf_path)-1] = '\0';
922
923 tf_baduid = (uid_t) stbuf.st_uid;
924 SL_IRETURN(SL_EBADUID, _("sl_trustfile"));
925 }
926
927 /*
928 * if a group member can write but the
929 * member is not trusted, bomb; but if
930 * sticky bit semantics are honored, it's
931 * okay
932 */
933 /* Thu Mar 29 21:10:28 CEST 2001 Rainer Wichmann
934 * replace !isingrp() with onlytrustedingrp(), as isingrp()
935 * will return at the first trusted user, even if there are additional
936 * (untrusted) users in the group
937 */
938 if (((stbuf.st_mode & S_IWGRP) == S_IWGRP) &&
939 ((okusers != NULL && !onlytrustedingrp((gid_t)stbuf.st_gid,okusers))||
940 (badusers != NULL && isingrp((gid_t)stbuf.st_gid, badusers)))
941#ifdef STICKY
942 && ((stbuf.st_mode&S_IFDIR) != S_IFDIR ||
943 (stbuf.st_mode&S_ISVTX) != S_ISVTX)
944#endif
945 )
946 {
947#ifdef TRUST_DEBUG
948 fprintf(stderr, "---------------------------------------------\n");
949 fprintf(stderr,
950 "trustfile: EBADGID %ld %s (group member not trusted)\n",
951 (UID_CAST)stbuf.st_gid, fexp);
952 fprintf(stderr, "This file/directory is group writeable, and one of the group members\n");
953 fprintf(stderr, "is not in samhains list of trusted users.\n");
954 fprintf(stderr, "Please run ./configure again with the option\n");
955 fprintf(stderr, " ./configure [more options] --with-trusted=0,...,UID\n");
956 fprintf(stderr, "where UID is the UID of the (yet) untrusted user.\n");
957 fprintf(stderr, "---------------------------------------------\n");
958#endif
959 (void) strncpy(tf_path, fexp, sizeof(tf_path));
960 tf_path[sizeof(tf_path)-1] = '\0';
961
962 tf_badgid = (gid_t) stbuf.st_gid;
963 SL_IRETURN(SL_EBADGID, _("sl_trustfile"));
964 }
965 /*
966 * if other can write, bomb; but if the sticky
967 * bit semantics are honored, it's okay
968 */
969 if (((stbuf.st_mode & S_IWOTH) == S_IWOTH)
970#ifdef STICKY
971 && ((stbuf.st_mode&S_IFDIR) != S_IFDIR ||
972 (stbuf.st_mode&S_ISVTX) != S_ISVTX)
973#endif
974 )
975 {
976#ifdef TRUST_DEBUG
977 fprintf(stderr, "---------------------------------------------\n");
978 fprintf(stderr, "trustfile: EBADOTH (world writeable): %s\n",
979 fexp);
980 fprintf(stderr, "This file/directory is world writeable.\n");
981 fprintf(stderr, "---------------------------------------------\n");
982#endif
983 (void) strncpy(tf_path, fexp, sizeof(tf_path));
984 tf_path[sizeof(tf_path)-1] = '\0';
985
986 SL_IRETURN(SL_EBADOTH, _("sl_trustfile"));
987 }
988 /*
989 * put the / back
990 */
991 if (p == fexp)
992 {
993 /* special case for root */
994 p[1] = c;
995 p++;
996 }
997 else
998 {
999 /* ordinary case for everything else */
1000 *p = c;
1001 if (*p)
1002 p++;
1003 }
1004 }
1005 /*
1006 * yes, it can be trusted
1007 */
1008 (void) strncpy(tf_path, fexp, sizeof(tf_path));
1009 tf_path[sizeof(tf_path)-1] = '\0';
1010
1011 SL_IRETURN(SL_ENONE, _("sl_trustfile"));
1012}
1013
1014#ifdef TRUST_MAIN
1015
1016#if defined(HOST_IS_CYGWIN) || defined(__cygwin__) || defined(__CYGWIN32__) || defined(__CYGWIN__)
1017int main()
1018{
1019 return 0;
1020}
1021#else
1022int main (int argc, char * argv[])
1023{
1024 int status;
1025#if defined(SH_WITH_SERVER)
1026 struct passwd * pass;
1027#endif
1028
1029 if (argc < 2) {
1030 fprintf(stderr, "%s: Usage: %s <fullpath>\n", argv[0], argv[0]);
1031 return 1;
1032 }
1033
1034 tf_path[0] = '\0';
1035#if defined(SH_WITH_SERVER)
1036 pass = sh_getpwnam(SH_IDENT); /* TESTONLY */
1037 if (pass != NULL)
1038 tf_euid = pass->pw_uid;
1039 else
1040 {
1041 fprintf(stderr, "trustfile: ERROR: getpwnam(%s) failed\n",
1042 SH_IDENT);
1043 return 1;
1044 }
1045#else
1046 tf_euid = geteuid();
1047#endif
1048
1049 status = sl_trustfile(argv[1], NULL, NULL);
1050 if (status != SL_ENONE)
1051 {
1052 fprintf(stderr, "trustfile: ERROR: not a trusted path: %s\n",
1053 argv[1]);
1054 return 1;
1055 }
1056 return 0;
1057}
1058#endif
1059#endif
1060
1061
1062
Note: See TracBrowser for help on using the repository browser.