source: trunk/src/trustfile.c@ 365

Last change on this file since 365 was 230, checked in by katerina, 16 years ago

Fix gcc 4.4 compiler warnings. Release 2.5.5.

File size: 29.2 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_EMEM -1032 /* Out of memory. */
138#define SL_EBADNAME -1040 /* Invalid name. */
139#define SL_ESTAT -1041 /* stat of file failed. Check errno. */
140#define SL_EBADUID -1050 /* Owner not trustworthy. */
141#define SL_EBADGID -1051 /* Group writeable and not trustworthy.*/
142#define SL_EBADOTH -1052 /* World writeable. */
143
144#endif
145
146
147#if defined(__linux__) || defined(__FreeBSD__)
148#define STICKY
149#endif
150
151#undef FIL__
152#define FIL__ _("trustfile.c")
153
154/*
155 * the get current working directory function
156 * every version of UNIX seems to have its own
157 * idea of how to do this, so we group them by
158 * arguments ...
159 * all must return a pointer to the right name
160 */
161
162
163#ifndef TRUST_MAIN
164
165#if defined(HAVE_GETCWD) && !defined(HAVE_BROKEN_GETCWD)
166#define CURDIR(buf,nbuf) getcwd((buf), (nbuf))
167#elif defined(HAVE_GETWD)
168#define CURDIR(buf,nbuf) getwd((buf))
169#endif
170
171#else
172
173#define CURDIR(buf,nbuf) getcwd((buf), (nbuf))
174
175#endif
176
177
178
179/*
180 * this checks to see if there are symbolic links
181 * assumes the link bit in the protection mask is called S_IFLNK
182 * (seems to be true on all UNIXes with them)
183 */
184#ifndef S_IFLNK
185#define lstat stat
186#endif
187
188
189/*
190 * these are useful global variables
191 *
192 * first set: who you gonna trust, by default?
193 * if the user does not specify a trusted or untrusted set of users,
194 * all users are considered untrusted EXCEPT:
195 * UID 0 -- root as root can do anything on most UNIX systems, this
196 * seems reasonable
197 * tf_euid -- programmer-selectable UID
198 * if the caller specifies a specific UID by putting
199 * it in this variable, it will be trusted; this is
200 * typically used to trust the effective UID of the
201 * process (note: NOT the real UID, which will cause all
202 * sorts of problems!) By default, this is set to -1,
203 * so if it's not set, root is the only trusted user
204 */
205
206/* modified Tue Feb 22 10:36:44 NFT 2000 Rainer Wichmann */
207
208
209#ifndef SL_ALWAYS_TRUSTED
210#define SL_ALWAYS_TRUSTED 0
211#endif
212static uid_t test_rootonly[] = { SL_ALWAYS_TRUSTED };
213
214#define tf_uid_neg ((uid_t)-1)
215
216uid_t rootonly[] = { SL_ALWAYS_TRUSTED,
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 tf_uid_neg, tf_uid_neg, tf_uid_neg, tf_uid_neg };
221
222uid_t tf_euid = tf_uid_neg;
223int EUIDSLOT = sizeof(test_rootonly)/sizeof(uid_t);
224int ORIG_EUIDSLOT = sizeof(test_rootonly)/sizeof(uid_t);
225
226char tf_path[MAXFILENAME]; /* error path for trust function */
227uid_t tf_baduid;
228gid_t tf_badgid;
229
230static
231int dirz(char *path)
232{
233 register char *p = path;/* points to rest of path to clean up */
234 register char *q; /* temp pointer for skipping over stuff */
235
236 static char swp[MAXFILENAME];
237
238 SL_ENTER(_("dirz"));
239 /*
240 * standard error checking
241 */
242 if (path == NULL)
243 SL_IRETURN(SL_ENULL, _("dirz"));
244 if (path[0] == '.')
245 SL_IRETURN(SL_EINTERNAL, _("dirz"));
246
247
248 /*
249 * loop over the path name until everything is checked
250 */
251 while(*p)
252 {
253 /* skip
254 */
255 if (*p != '/')
256 {
257 p++;
258 continue;
259 }
260
261 /* "/./" or "/."
262 */
263 if (p[1] == '.' && (p[2] == '/' || p[2] == '\0'))
264 {
265 /* yes -- delete "/."
266 */
267 (void) strcpy(swp, &p[2]); /* known to fit */
268 (void) strcpy(p, swp); /* known to fit */
269
270 /* special case "/." as full path name
271 */
272 if (p == path && *p == '\0')
273 {
274 *p++ = '/';
275 *p = '\0';
276 }
277 }
278
279 /* "//"
280 */
281 else if (p[1] == '/')
282 {
283 /* yes -- skip
284 */
285 for(q = &p[2]; *q == '/'; q++)
286 ;
287 (void) strcpy(swp, q); /* known to fit */
288 (void) strcpy(&p[1], swp); /* known to fit */
289 }
290
291 /* "/../" or "/.."
292 */
293 else if (p[1] == '.' && p[2] == '.' && (p[3] == '/' || p[3] == '\0'))
294 {
295 /* yes -- if it's root, delete .. only
296 */
297 if (p == path)
298 {
299 (void) strcpy(swp, &p[3]); /* known to fit */
300 (void) strcpy(p, swp); /* known to fit */
301 }
302 else
303 {
304 /* back up over previous component
305 */
306 q = p - 1;
307 while(q != path && *q != '/')
308 q--;
309 /* now wipe it out
310 */
311 (void) strcpy(swp, &p[3]); /* known to fit */
312 (void) strcpy(q, swp); /* known to fit */
313 p = q;
314 }
315 }
316 else
317 p++;
318 }
319 SL_IRETURN(SL_ENONE, _("dirz"));
320}
321
322
323
324/* not static to circumvent stupid gcc 4 bug */
325int getfname(const char *fname, char *rbuf, int rsz)
326{
327#ifndef TRUST_MAIN
328 register int status;
329#endif
330
331 SL_ENTER(_("getfname"));
332 /*
333 * do the initial checking
334 * NULL pointer
335 */
336 if (fname == NULL || rbuf == NULL)
337 SL_IRETURN(SL_ENULL, _("getfname"));
338 if (rsz <= 0)
339 SL_IRETURN(SL_ERANGE, _("getfname"));
340
341
342 /* already a full path name */
343 if (*fname == '/')
344 rbuf[0] = '\0';
345 else
346 {
347 if (CURDIR(rbuf, rsz) == NULL)
348 {
349#ifdef TRUST_DEBUG
350 fprintf(stderr, "trustfile: getcwd failed\n");
351#endif
352 SL_IRETURN(SL_EBADNAME, _("getfname"));
353 }
354 }
355
356 /*
357 * append the file name and reduce
358 */
359 if (fname != NULL && *fname != '\0')
360 {
361#ifndef TRUST_MAIN
362 status = sl_strlcat(rbuf, "/", rsz);
363 if (status == SL_ENONE)
364 status = sl_strlcat(rbuf, fname, rsz);
365 if (status != SL_ENONE)
366 SL_IRETURN(status, _("getfname"));
367#else
368 strncat(rbuf, "/", rsz-strlen(rbuf)-1);
369 rbuf[rsz-1] = '\0';
370 strncat(rbuf, fname, rsz-strlen(rbuf)-1);
371 rbuf[rsz-1] = '\0';
372#endif
373 }
374 SL_IRETURN(dirz(rbuf), _("getfname"));
375}
376
377static
378int isin(uid_t n, uid_t *list)
379{
380 SL_ENTER(_("isin"));
381 if (list == NULL)
382 SL_IRETURN(SL_FALSE, _("isin"));
383
384 while(*list != tf_uid_neg && *list != n)
385 {
386#ifdef TRUST_DEBUG
387 fprintf (stderr,
388 "trustfile: owner_uid=%ld, trusted uid=%ld, no match\n",
389 (UID_CAST) n, (UID_CAST) *list);
390#endif
391 list++;
392 }
393
394 if (*list == tf_uid_neg)
395 {
396#ifdef TRUST_DEBUG
397 fprintf (stderr,
398 "trustfile: owner_uid=%ld, no match with any trusted user --> ERROR\n",
399 (UID_CAST) n);
400#endif
401 SL_IRETURN(SL_FALSE, _("isin"));
402 }
403
404#ifdef TRUST_DEBUG
405 fprintf (stderr,
406 "trustfile: owner_uid=%ld, trusted_uid=%ld, match found --> OK\n",
407 (UID_CAST)n, (UID_CAST)*list);
408#endif
409 SL_IRETURN(SL_TRUE, _("isin"));
410}
411
412/* comment added by R. Wichmann
413 * RETURN TRUE if ANYONE in ulist is group member
414 */
415/* not static to circumvent stupid gcc 4 bug */
416int isingrp(gid_t grp, uid_t *ulist)
417{
418 struct passwd *w; /* info about group member */
419 register uid_t *u; /* points to current ulist member */
420 register char **p; /* points to current group member */
421 struct group *g; /* pointer to group information */
422
423#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
424 struct group gr;
425 char * buffer = NULL;
426 struct passwd pwd;
427 char * pbuffer = NULL;
428#endif
429
430 SL_ENTER(_("isingrp"));
431
432#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
433 buffer = malloc(SH_GRBUF_SIZE);
434 sh_getgrgid_r(grp, &gr, buffer, SH_GRBUF_SIZE, &g);
435#else
436 g = sh_getgrgid(grp);
437#endif
438
439 if (g == NULL)
440 {
441 goto end_false;
442 }
443
444 /* this will return at the first match
445 */
446#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
447 pbuffer = malloc(SH_PWBUF_SIZE);
448#endif
449
450 for(p = g->gr_mem; *p != NULL; p++)
451 {
452 for(u = ulist; *u != tf_uid_neg; u++)
453 {
454 /* map user name to UID and compare */
455#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
456 sh_getpwnam_r(*p, &pwd, pbuffer, SH_PWBUF_SIZE, &w);
457#else
458 w = sh_getpwnam(*p);
459#endif
460
461#ifdef TRUST_MAIN
462 if (w != NULL && *u == (uid_t)(w->pw_uid) )
463 goto end_true;
464#else
465 if (w != NULL && *u == (uid_t)(w->pw_uid) )
466 {
467 goto end_true;
468 }
469#endif
470 }
471 }
472 /* added by R. Wichmann Fri Mar 30 08:16:14 CEST 2001:
473 * a user can have a GID but no entry in /etc/group
474 */
475 for(u = ulist; *u != tf_uid_neg; u++)
476 {
477#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWUID_R)
478 sh_getpwuid_r(*u, &pwd, pbuffer, SH_PWBUF_SIZE, &w);
479#else
480 w = sh_getpwuid(*u);
481#endif
482#ifdef TRUST_MAIN
483 if (w != NULL && grp == (gid_t)(w->pw_gid) )
484 goto end_true;
485#else
486 if (w != NULL && grp == (gid_t)(w->pw_gid) )
487 {
488 goto end_true;
489 }
490#endif
491 }
492
493 end_false:
494#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
495 if (buffer) free(buffer);
496 if (pbuffer) free(pbuffer);
497#endif
498 SL_IRETURN(SL_FALSE, _("isingrp"));
499
500 end_true:
501#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
502 if (buffer) free(buffer);
503 if (pbuffer) free(pbuffer);
504#endif
505 SL_IRETURN(SL_TRUE, _("isingrp"));
506}
507
508/* added by R. Wichmann Fri Mar 30 08:16:14 CEST 2001
509 * RETURN TRUE only if ALL group members are trusted
510 */
511/* not static to circumvent stupid gcc 4 bug */
512int onlytrustedingrp(gid_t grp, uid_t *ulist)
513{
514 struct passwd *w; /* info about group member */
515 register uid_t *u; /* points to current ulist member */
516 register char **p; /* points to current group member */
517 struct group *g; /* pointer to group information */
518 register int flag = -1; /* group member found */
519
520#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
521 struct group gr;
522 char * buffer = NULL;
523 struct passwd pw;
524 char * pbuffer = NULL;
525#endif
526
527 int retval = SL_FALSE;
528
529 SL_ENTER(_("onlytrustedingrp"));
530
531#ifdef TRUST_DEBUG
532 fprintf(stderr, "trustfile: group writeable, group_gid: %ld\n",
533 (UID_CAST)grp);
534#endif
535
536#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
537 buffer = malloc(SH_GRBUF_SIZE);
538 sh_getgrgid_r(grp, &gr, buffer, SH_GRBUF_SIZE, &g);
539#else
540 g = sh_getgrgid(grp);
541#endif
542
543 if (g == NULL)
544 {
545#ifdef TRUST_DEBUG
546 fprintf(stderr,
547 "trustfile: group_gid: %ld, no such group --> ERROR\n",
548 (UID_CAST)grp);
549#endif
550 retval = SL_FALSE;
551 goto end_retval;
552 }
553
554 /* empty group -> no problem
555
556 if(g->gr_mem == NULL || g->gr_mem[0] == NULL )
557 SL_IRETURN(SL_TRUE, _("onlytrustedingrp") );
558 */
559
560 /* check for untrusted members of the group
561 */
562#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
563 pbuffer = malloc(SH_PWBUF_SIZE);
564#endif
565
566 for(p = g->gr_mem; *p != NULL; p++)
567 {
568 flag = -1;
569#ifdef TRUST_DEBUG
570 fprintf(stderr, "trustfile: group_member: %s\n", *p);
571#endif
572 /* map user name to UID and compare
573 */
574#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETPWNAM_R)
575 sh_getpwnam_r(*p, &pw, pbuffer, SH_PWBUF_SIZE, &w);
576#else
577 w = sh_getpwnam(*p);
578#endif
579
580 if (w == NULL) /* not a valid user, ignore */
581 {
582 flag = 0;
583 }
584 else /* check list of trusted users */
585 {
586#ifdef TRUST_DEBUG
587 fprintf (stderr,
588 "trustfile: uid=%ld, checking whether it is trusted\n",
589 (UID_CAST)(w->pw_uid));
590#endif
591 for(u = ulist; *u != tf_uid_neg; u++)
592 {
593 if (*u == (w->pw_uid) )
594 {
595#ifdef TRUST_DEBUG
596 fprintf (stderr,
597 "trustfile: uid=%ld, trusted_uid=%ld, match found --> OK\n",
598 (UID_CAST)(w->pw_uid), (UID_CAST)*u);
599#endif
600 flag = 0;
601 break;
602 }
603 else
604 {
605#ifdef TRUST_DEBUG
606 fprintf (stderr,
607 "trustfile: uid=%ld, trusted_uid=%ld, no match\n",
608 (UID_CAST)(w->pw_uid), (UID_CAST)*u);
609#endif
610 ;
611 }
612 }
613 }
614 /* not found
615 */
616 if (flag == -1)
617 {
618#ifdef TRUST_DEBUG
619 fprintf (stderr,
620 "trustfile: user=%s (gid %ld), not a trusted user --> ERROR\n", *p, (UID_CAST)grp);
621#endif
622 tf_baduid = w->pw_uid;
623 retval = SL_FALSE;
624#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
625 if (pbuffer) free(pbuffer);
626#endif
627 goto end_retval;
628 }
629 }
630
631#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
632 if (pbuffer) free(pbuffer);
633#endif
634
635#ifndef TEST_ONLY
636#ifdef HAVE_GETPWENT
637 /* now check ALL users for their GID !!!
638 */
639 SH_MUTEX_LOCK(mutex_pwent);
640
641 while (NULL != (w = sh_getpwent()))
642 {
643 if (grp == (gid_t)(w->pw_gid))
644 {
645#ifdef TRUST_DEBUG
646 fprintf(stderr, "trustfile: checking group member %s, uid %ld\n",
647 w->pw_name, (UID_CAST)w->pw_uid);
648#endif
649 /* is it a trusted user ?
650 */
651 flag = -1;
652 for(u = ulist; *u != tf_uid_neg; u++)
653 {
654 if (*u == (uid_t)(w->pw_uid))
655 {
656#ifdef TRUST_DEBUG
657 fprintf (stderr,
658 "trustfile: uid=%ld, trusted_uid=%ld, match found --> OK\n",
659 (UID_CAST)(w->pw_uid), (UID_CAST)(*u));
660#endif
661 flag = 0;
662 break;
663 }
664 else
665 {
666#ifdef TRUST_DEBUG
667 fprintf (stderr,
668 "trustfile: uid=%ld, trusted_uid=%ld, no match\n",
669 (UID_CAST)(w->pw_uid), (UID_CAST)*u);
670#endif
671 ;
672 }
673 }
674 /* not found */
675 if (flag == -1)
676 {
677#ifdef TRUST_DEBUG
678 fprintf(stderr,"trustfile: group member %s not found in trusted users --> ERROR\n", w->pw_name);
679#endif
680 tf_baduid = w->pw_uid;
681 retval = SL_FALSE;
682 goto out;
683 /* SL_IRETURN(SL_FALSE, _("onlytrustedingrp")); */
684 }
685 }
686 }
687 retval = SL_TRUE;
688
689 out:
690
691#ifdef HAVE_ENDPWENT
692 sh_endpwent();
693#endif
694
695 SH_MUTEX_UNLOCK(mutex_pwent);
696
697 /* TEST_ONLY */
698#endif
699 /* #ifdef HAVE_GETPWENT */
700#endif
701
702#ifdef TRUST_DEBUG
703 if (retval == SL_TRUE)
704 fprintf(stderr,
705 "trustfile: group %ld: all members are trusted users --> OK\n",
706 (UID_CAST)grp);
707#endif
708 /* all found
709 */
710 end_retval:
711#if defined(HAVE_PTHREAD) && defined (_POSIX_THREAD_SAFE_FUNCTIONS) && defined(HAVE_GETGRGID_R)
712 if (buffer) free(buffer);
713#endif
714 SL_IRETURN(retval, _("onlytrustedingrp"));
715}
716
717int sl_trustfile(const char *fname, uid_t *okusers, uid_t *badusers)
718{
719 char * fexp = NULL; /* file name fully expanded */
720 register char *p; /* used to hold name to be checked */
721 struct stat stbuf; /* used to check file permissions */
722 char c; /* used to hold temp char */
723
724 SL_ENTER(_("sl_trustfile"));
725 if (fname == NULL)
726 SL_IRETURN(SL_EBADFILE, _("sl_trustfile"));
727
728 fexp = malloc( MAXFILENAME );
729 if (!fexp)
730 SL_IRETURN(SL_EMEM, _("sl_trustfile"));
731
732 p = fexp;
733
734 /*
735 * next expand to the full file name
736 * getfname sets sl_errno as appropriate
737 */
738#ifdef TRUST_MAIN
739 sl_errno = getfname(fname, fexp, MAXFILENAME);
740 if (sl_errno != 0)
741 {
742 free(fexp);
743 return sl_errno;
744 }
745#else
746 if (SL_ISERROR(getfname(fname, fexp, MAXFILENAME)))
747 {
748 free(fexp);
749 SL_IRETURN(sl_errno, _("sl_trustfile"));
750 }
751#endif
752
753 if (okusers == NULL && badusers == NULL)
754 {
755 okusers = rootonly;
756 rootonly[EUIDSLOT] = tf_euid;
757 }
758
759 /*
760 * now loop through the path a component at a time
761 * note we have to special-case root
762 */
763 while(*p)
764 {
765 /*
766 * get next component
767 */
768 while(*p && *p != '/')
769 p++;
770
771 /* save where you are
772 */
773 if (p == fexp)
774 {
775 /* keep the / if it's the root dir
776 */
777 c = p[1];
778 p[1] = '\0';
779 }
780 else
781 {
782 /* clobber the / if it isn't the root dir
783 */
784 c = *p;
785 *p = '\0';
786 }
787
788 /*
789 * now get the information
790 */
791 if (retry_lstat(FIL__, __LINE__, fexp, &stbuf) < 0)
792 {
793 (void) strncpy(tf_path, fexp, sizeof(tf_path));
794 tf_path[sizeof(tf_path)-1] = '\0';
795#ifdef TRUST_MAIN
796 fprintf(stderr, "---------------------------------------------\n");
797 fprintf(stderr, "trustfile: ESTAT: stat(%s) failed,\n", fexp);
798 fprintf(stderr, "maybe the file does not exist\n");
799 fprintf(stderr, "---------------------------------------------\n");
800#endif
801 free(fexp);
802 SL_IRETURN(SL_ESTAT, _("sl_trustfile"));
803 }
804
805#ifdef S_IFLNK
806 /*
807 * if it's a symbolic link, recurse
808 */
809 if ((stbuf.st_mode & S_IFLNK) == S_IFLNK)
810 {
811 /*
812 * this is tricky
813 * if the symlink is to an absolute path
814 * name, just call trustfile on it; but
815 * if it's a relative path name, it's
816 * interpreted WRT the current working
817 * directory AND NOT THE FILE NAME!!!!!
818 * so, we simply put /../ at the end of
819 * the file name, then append the symlink
820 * contents; trustfile will canonicalize
821 * this, and the /../ we added "undoes"
822 * the name of the symlink to put us in
823 * the current working directory, at
824 * which point the symlink contents (appended
825 * to the CWD) are interpreted correctly.
826 * got it?
827 */
828 char * csym; /* contents of symlink file */
829 char * full; /* "full" name of symlink */
830 register char *b, *t; /* used to copy stuff around */
831 register int lsym; /* num chars in symlink ref */
832 register int i; /* trustworthy or not? */
833 const char * t_const;
834 char *end;
835
836 /*
837 * get what the symbolic link points to
838 *
839 * The original code does not check the return code of readlink(),
840 * and has an off-by-one error
841 * (MAXFILENAME instead of MAXFILENAME-1)
842 * R.W. Tue May 29 22:05:16 CEST 2001
843 */
844 csym = malloc( MAXFILENAME );
845 if (!csym)
846 {
847 free(fexp);
848 SL_IRETURN(SL_EMEM, _("sl_trustfile"));
849 }
850
851 lsym = readlink(fexp, csym, MAXFILENAME-1);
852 if (lsym >= 0)
853 csym[lsym] = '\0';
854 else
855 {
856#ifdef TRUST_MAIN
857 fprintf(stderr, "---------------------------------------------\n");
858 fprintf(stderr, "trustfile: EBADNAME: readlink(%s) failed\n",
859 fexp);
860 fprintf(stderr, "---------------------------------------------\n");
861#endif
862 free(csym);
863 free(fexp);
864 SL_IRETURN(SL_EBADNAME, _("sl_trustfile"));
865 }
866
867 full = malloc( MAXFILENAME );
868 if (!full)
869 {
870 free(csym);
871 free(fexp);
872 SL_IRETURN(SL_EMEM, _("sl_trustfile"));
873 }
874
875 /*
876 * relative or absolute referent?
877 */
878 if (csym[0] != '/')
879 {
880 /* pointer to one above last element
881 */
882 end = &full[MAXFILENAME-1]; ++end;
883
884 /* initialize pointers
885 */
886 b = full;
887
888 /* copy in base path
889 */
890 t = fexp;
891 while(*t && b < end)
892 *b++ = *t++;
893
894 /* smack on the /../
895 */
896 t_const = "/../"; t = (char *)t_const;
897 while(*t && b < end)
898 *b++ = *t++;
899
900 /* append the symlink referent
901 */
902 t = csym;
903 while(*t && b < end)
904 *b++ = *t++;
905
906 /* see if we're too big
907 */
908 if (*t || b == end)
909 {
910 /* yes -- error
911 */
912 (void) strncpy(tf_path, fexp, sizeof(tf_path));
913 tf_path[sizeof(tf_path)-1] = '\0';
914#ifdef TRUST_MAIN
915 fprintf(stderr, "---------------------------------------------\n");
916 fprintf(stderr,
917 "trustfile: ETRUNC: normalized path too long (%s)\n",
918 fexp);
919 fprintf(stderr, "---------------------------------------------\n");
920#endif
921 free(full);
922 free(csym);
923 free(fexp);
924 SL_IRETURN(SL_ETRUNC, _("sl_trustfile"));
925 }
926 *b = '\0';
927 }
928 else
929 {
930 /* absolute -- just copy */
931 /* overflow can't occur as the arrays */
932 /* are the same size */
933 (void) strcpy(full, csym); /* known to fit */
934 }
935 /*
936 * now check out this file and its ancestors
937 */
938 if ((i = sl_trustfile(full, okusers, badusers)) != SL_ENONE)
939 {
940 free(full);
941 free(csym);
942 free(fexp);
943 SL_IRETURN(i, _("sl_trustfile"));
944 }
945
946 /*
947 * okay, this part is valid ... let's check the rest
948 * put the / back
949 */
950 if (p == fexp)
951 {
952 /* special case for root */
953 p[1] = c;
954 p++;
955 }
956 else
957 {
958 /* ordinary case for everything else */
959 *p = c;
960 if (*p)
961 p++;
962 }
963 free(full);
964 free(csym);
965 continue;
966 }
967#endif
968
969
970#ifdef TRUST_DEBUG
971 fprintf(stderr, "\ntrustfile: checking path=%s\n", fexp);
972#endif
973 /*
974 * if the owner is not trusted then -- as the owner can
975 * change protection modes -- he/she can write to the
976 * file regardless of permissions, so bomb
977 */
978 if (((okusers != NULL && SL_FALSE == isin((uid_t)stbuf.st_uid,okusers))||
979 (badusers != NULL && SL_TRUE == isin((uid_t)stbuf.st_uid,badusers))))
980 {
981#ifdef TRUST_DEBUG
982 fprintf(stderr, "---------------------------------------------\n");
983 fprintf(stderr, "trustfile: EBADUID %s (owner not trusted)\n",
984 fexp);
985 fprintf(stderr, "The owner of this file/directory is not in samhains\n");
986 fprintf(stderr, "list of trusted users.\n");
987 fprintf(stderr, "Please run ./configure again with the option\n");
988 fprintf(stderr, " ./configure [more options] --with-trusted=0,...,UID\n");
989 fprintf(stderr, "where UID is the UID of the (yet) untrusted user.\n");
990 fprintf(stderr, "---------------------------------------------\n");
991#endif
992 (void) strncpy(tf_path, fexp, sizeof(tf_path));
993 tf_path[sizeof(tf_path)-1] = '\0';
994
995 tf_baduid = (uid_t) stbuf.st_uid;
996 free(fexp);
997 SL_IRETURN(SL_EBADUID, _("sl_trustfile"));
998 }
999
1000 /*
1001 * if a group member can write but the
1002 * member is not trusted, bomb; but if
1003 * sticky bit semantics are honored, it's
1004 * okay
1005 */
1006 /* Thu Mar 29 21:10:28 CEST 2001 Rainer Wichmann
1007 * replace !isingrp() with onlytrustedingrp(), as isingrp()
1008 * will return at the first trusted user, even if there are additional
1009 * (untrusted) users in the group
1010 */
1011 if (((stbuf.st_mode & S_IWGRP) == S_IWGRP) &&
1012 ((okusers != NULL && !onlytrustedingrp((gid_t)stbuf.st_gid,okusers))||
1013 (badusers != NULL && isingrp((gid_t)stbuf.st_gid, badusers)))
1014#ifdef STICKY
1015 && ((stbuf.st_mode&S_IFDIR) != S_IFDIR ||
1016 (stbuf.st_mode&S_ISVTX) != S_ISVTX)
1017#endif
1018 )
1019 {
1020#ifdef TRUST_DEBUG
1021 fprintf(stderr, "---------------------------------------------\n");
1022 fprintf(stderr,
1023 "trustfile: EBADGID %ld %s (group member not trusted)\n",
1024 (UID_CAST)stbuf.st_gid, fexp);
1025 fprintf(stderr, "This file/directory is group writeable, and one of the group members\n");
1026 fprintf(stderr, "is not in samhains list of trusted users.\n");
1027 fprintf(stderr, "Please run ./configure again with the option\n");
1028 fprintf(stderr, " ./configure [more options] --with-trusted=0,...,UID\n");
1029 fprintf(stderr, "where UID is the UID of the (yet) untrusted user.\n");
1030 fprintf(stderr, "---------------------------------------------\n");
1031#endif
1032 (void) strncpy(tf_path, fexp, sizeof(tf_path));
1033 tf_path[sizeof(tf_path)-1] = '\0';
1034
1035 tf_badgid = (gid_t) stbuf.st_gid;
1036 free(fexp);
1037 SL_IRETURN(SL_EBADGID, _("sl_trustfile"));
1038 }
1039 /*
1040 * if other can write, bomb; but if the sticky
1041 * bit semantics are honored, it's okay
1042 */
1043 if (((stbuf.st_mode & S_IWOTH) == S_IWOTH)
1044#ifdef STICKY
1045 && ((stbuf.st_mode&S_IFDIR) != S_IFDIR ||
1046 (stbuf.st_mode&S_ISVTX) != S_ISVTX)
1047#endif
1048 )
1049 {
1050#ifdef TRUST_DEBUG
1051 fprintf(stderr, "---------------------------------------------\n");
1052 fprintf(stderr, "trustfile: EBADOTH (world writeable): %s\n",
1053 fexp);
1054 fprintf(stderr, "This file/directory is world writeable.\n");
1055 fprintf(stderr, "---------------------------------------------\n");
1056#endif
1057 (void) strncpy(tf_path, fexp, sizeof(tf_path));
1058 tf_path[sizeof(tf_path)-1] = '\0';
1059
1060 free(fexp);
1061 SL_IRETURN(SL_EBADOTH, _("sl_trustfile"));
1062 }
1063 /*
1064 * put the / back
1065 */
1066 if (p == fexp)
1067 {
1068 /* special case for root */
1069 p[1] = c;
1070 p++;
1071 }
1072 else
1073 {
1074 /* ordinary case for everything else */
1075 *p = c;
1076 if (*p)
1077 p++;
1078 }
1079 }
1080 /*
1081 * yes, it can be trusted
1082 */
1083 (void) strncpy(tf_path, fexp, sizeof(tf_path));
1084 tf_path[sizeof(tf_path)-1] = '\0';
1085
1086 free(fexp);
1087 SL_IRETURN(SL_ENONE, _("sl_trustfile"));
1088}
1089
1090#ifdef TRUST_MAIN
1091
1092#if defined(HOST_IS_CYGWIN) || defined(__cygwin__) || defined(__CYGWIN32__) || defined(__CYGWIN__)
1093int main()
1094{
1095 return 0;
1096}
1097#else
1098int main (int argc, char * argv[])
1099{
1100 int status;
1101#if defined(SH_WITH_SERVER)
1102 struct passwd * pass;
1103#endif
1104
1105 if (argc < 2) {
1106 fprintf(stderr, "%s: Usage: %s <fullpath>\n", argv[0], argv[0]);
1107 return 1;
1108 }
1109
1110 tf_path[0] = '\0';
1111#if defined(SH_WITH_SERVER)
1112 pass = sh_getpwnam(SH_IDENT); /* TESTONLY */
1113 if (pass != NULL)
1114 tf_euid = pass->pw_uid;
1115 else
1116 {
1117 fprintf(stderr, "trustfile: ERROR: getpwnam(%s) failed\n",
1118 SH_IDENT);
1119 return 1;
1120 }
1121#else
1122 tf_euid = geteuid();
1123#endif
1124
1125 status = sl_trustfile(argv[1], NULL, NULL);
1126 if (status != SL_ENONE)
1127 {
1128 fprintf(stderr, "trustfile: ERROR: not a trusted path: %s\n",
1129 argv[1]);
1130 return 1;
1131 }
1132 return 0;
1133}
1134#endif
1135#endif
1136
1137
1138
Note: See TracBrowser for help on using the repository browser.