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