xref: /onnv-gate/usr/src/lib/libast/common/preroot/ispreroot.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin  * AT&T Bell Laboratories
254887Schin  * return 1 if dir [any dir] is the preroot
264887Schin  */
274887Schin 
284887Schin #include <ast.h>
294887Schin #include <preroot.h>
304887Schin 
314887Schin #if FS_PREROOT
324887Schin 
334887Schin #include <ls.h>
344887Schin 
354887Schin /*
364887Schin  * return 1 if files a and b are the same under preroot
374887Schin  *
384887Schin  * NOTE: the kernel disables preroot for set-uid processes
394887Schin  */
404887Schin 
414887Schin static int
same(const char * a,const char * b)424887Schin same(const char* a, const char* b)
434887Schin {
444887Schin 	int		i;
454887Schin 	int		euid;
464887Schin 	int		ruid;
474887Schin 
484887Schin 	struct stat	ast;
494887Schin 	struct stat	bst;
504887Schin 
514887Schin 	if ((ruid = getuid()) != (euid = geteuid())) setuid(ruid);
524887Schin 	i = !stat(a, &ast) && !stat(b, &bst) && ast.st_dev == bst.st_dev && ast.st_ino == bst.st_ino;
534887Schin 	if (ruid != euid) setuid(euid);
544887Schin 	return(i);
554887Schin }
564887Schin 
574887Schin int
ispreroot(const char * dir)584887Schin ispreroot(const char* dir)
594887Schin {
604887Schin 	static int	prerooted = -1;
614887Schin 
624887Schin 	if (dir) return(same("/", dir));
634887Schin 	if (prerooted < 0) prerooted = !same("/", PR_REAL);
644887Schin 	return(prerooted);
654887Schin }
664887Schin 
674887Schin #else
684887Schin 
694887Schin NoN(ispreroot)
704887Schin 
714887Schin #endif
72