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