xref: /minix3/lib/libutil/secure_path.c (revision 0c3983b25a88161cf074524e5c94585a2582ae82)
1*0c3983b2SBen Gras /*	$NetBSD: secure_path.c,v 1.2 2003/01/06 20:30:30 wiz Exp $	*/
2*0c3983b2SBen Gras 
3*0c3983b2SBen Gras /*-
4*0c3983b2SBen Gras  * Copyright (c) 1995,1997 Berkeley Software Design, Inc. All rights reserved.
5*0c3983b2SBen Gras  *
6*0c3983b2SBen Gras  * Redistribution and use in source and binary forms, with or without
7*0c3983b2SBen Gras  * modification, are permitted provided that the following conditions
8*0c3983b2SBen Gras  * are met:
9*0c3983b2SBen Gras  * 1. Redistributions of source code must retain the above copyright
10*0c3983b2SBen Gras  *    notice, this list of conditions and the following disclaimer.
11*0c3983b2SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
12*0c3983b2SBen Gras  *    notice, this list of conditions and the following disclaimer in the
13*0c3983b2SBen Gras  *    documentation and/or other materials provided with the distribution.
14*0c3983b2SBen Gras  * 3. All advertising materials mentioning features or use of this software
15*0c3983b2SBen Gras  *    must display the following acknowledgement:
16*0c3983b2SBen Gras  *	This product includes software developed by Berkeley Software Design,
17*0c3983b2SBen Gras  *	Inc.
18*0c3983b2SBen Gras  * 4. The name of Berkeley Software Design, Inc.  may not be used to endorse
19*0c3983b2SBen Gras  *    or promote products derived from this software without specific prior
20*0c3983b2SBen Gras  *    written permission.
21*0c3983b2SBen Gras  *
22*0c3983b2SBen Gras  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND
23*0c3983b2SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*0c3983b2SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*0c3983b2SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE
26*0c3983b2SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*0c3983b2SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*0c3983b2SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*0c3983b2SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*0c3983b2SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*0c3983b2SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*0c3983b2SBen Gras  * SUCH DAMAGE.
33*0c3983b2SBen Gras  *
34*0c3983b2SBen Gras  *	BSDI login_cap.c,v 2.13 1998/02/07 03:17:05 prb Exp
35*0c3983b2SBen Gras  */
36*0c3983b2SBen Gras 
37*0c3983b2SBen Gras #include <sys/cdefs.h>
38*0c3983b2SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
39*0c3983b2SBen Gras __RCSID("$NetBSD: secure_path.c,v 1.2 2003/01/06 20:30:30 wiz Exp $");
40*0c3983b2SBen Gras #endif /* LIBC_SCCS and not lint */
41*0c3983b2SBen Gras 
42*0c3983b2SBen Gras #include <sys/types.h>
43*0c3983b2SBen Gras #include <sys/stat.h>
44*0c3983b2SBen Gras 
45*0c3983b2SBen Gras #include <assert.h>
46*0c3983b2SBen Gras #include <syslog.h>
47*0c3983b2SBen Gras #include <util.h>
48*0c3983b2SBen Gras 
49*0c3983b2SBen Gras int
secure_path(const char * path)50*0c3983b2SBen Gras secure_path(const char *path)
51*0c3983b2SBen Gras {
52*0c3983b2SBen Gras 	struct stat sb;
53*0c3983b2SBen Gras 
54*0c3983b2SBen Gras 	_DIAGASSERT(path != NULL);
55*0c3983b2SBen Gras 
56*0c3983b2SBen Gras 	/*
57*0c3983b2SBen Gras 	 * If not a regular file, or is owned/writable by someone
58*0c3983b2SBen Gras 	 * other than root, quit.
59*0c3983b2SBen Gras 	 */
60*0c3983b2SBen Gras 	if (lstat(path, &sb) < 0)
61*0c3983b2SBen Gras 		/* syslog(LOG_ERR, "cannot stat %s: %m", path) */;
62*0c3983b2SBen Gras 	else if (!S_ISREG(sb.st_mode))
63*0c3983b2SBen Gras 		syslog(LOG_ERR, "%s: not a regular file", path);
64*0c3983b2SBen Gras 	else if (sb.st_uid != 0)
65*0c3983b2SBen Gras 		syslog(LOG_ERR, "%s: not owned by root", path);
66*0c3983b2SBen Gras 	else if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0)
67*0c3983b2SBen Gras 		syslog(LOG_ERR, "%s: writable by non-root", path);
68*0c3983b2SBen Gras 	else
69*0c3983b2SBen Gras 		return (0);
70*0c3983b2SBen Gras 
71*0c3983b2SBen Gras 	return (-1);
72*0c3983b2SBen Gras }
73