1172106Srwatson/*-
2172106Srwatson * Copyright (c) 2006 nCircle Network Security, Inc.
3172106Srwatson * Copyright (c) 2007 Robert N. M. Watson
4172106Srwatson * All rights reserved.
5172106Srwatson *
6172106Srwatson * This software was developed by Robert N. M. Watson for the TrustedBSD
7172106Srwatson * Project under contract to nCircle Network Security, Inc.
8172106Srwatson *
9172106Srwatson * Redistribution and use in source and binary forms, with or without
10172106Srwatson * modification, are permitted provided that the following conditions
11172106Srwatson * are met:
12172106Srwatson * 1. Redistributions of source code must retain the above copyright
13172106Srwatson *    notice, this list of conditions and the following disclaimer.
14172106Srwatson * 2. Redistributions in binary form must reproduce the above copyright
15172106Srwatson *    notice, this list of conditions and the following disclaimer in the
16172106Srwatson *    documentation and/or other materials provided with the distribution.
17172106Srwatson *
18172106Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19172106Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20172106Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21172106Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
22172106Srwatson * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23172106Srwatson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24172106Srwatson * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25172106Srwatson * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26172106Srwatson * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27172106Srwatson * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28172106Srwatson * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29172106Srwatson *
30172106Srwatson * $FreeBSD$
31172106Srwatson */
32172106Srwatson
33172106Srwatson/*
34172106Srwatson * Check that we must either be running as root or the owner of the file in
35172106Srwatson * order to modify permissions on the file.
36172106Srwatson */
37172106Srwatson
38172106Srwatson#include <sys/types.h>
39172106Srwatson#include <sys/stat.h>
40172106Srwatson
41172106Srwatson#include <err.h>
42172106Srwatson#include <errno.h>
43172106Srwatson#include <stdlib.h>
44172106Srwatson#include <string.h>
45172106Srwatson#include <unistd.h>
46172106Srwatson
47172106Srwatson#include "main.h"
48172106Srwatson
49172106Srwatsonstatic char fpath[1024];
50172106Srwatsonstatic int fpath_initialized;
51172106Srwatson
52172106Srwatsonint
53172106Srwatsonpriv_vfs_chmod_froot_setup(int asroot, int injail, struct test *test)
54172106Srwatson{
55172106Srwatson
56172106Srwatson	setup_file("priv_vfs_chmod_setup: fpath", fpath, UID_ROOT, GID_WHEEL,
57172106Srwatson	    0600);
58172106Srwatson	fpath_initialized = 1;
59172106Srwatson	return (0);
60172106Srwatson}
61172106Srwatson
62172106Srwatsonint
63172106Srwatsonpriv_vfs_chmod_fowner_setup(int asroot, int injail, struct test *test)
64172106Srwatson{
65172106Srwatson
66172106Srwatson	setup_file("priv_vfs_chmod_setup: fpath", fpath, UID_OWNER,
67172106Srwatson	    GID_OWNER, 0600);
68172106Srwatson	fpath_initialized = 1;
69172106Srwatson	return (0);
70172106Srwatson}
71172106Srwatson
72172106Srwatsonint
73172106Srwatsonpriv_vfs_chmod_fother_setup(int asroot, int injail, struct test *test)
74172106Srwatson{
75172106Srwatson
76172106Srwatson	setup_file("priv_vfs_chmod_setup: fpath", fpath, UID_OTHER,
77172106Srwatson	    GID_OTHER, 0600);
78172106Srwatson	fpath_initialized = 1;
79172106Srwatson	return (0);
80172106Srwatson}
81172106Srwatson
82172106Srwatsonvoid
83172106Srwatsonpriv_vfs_chmod_froot(int asroot, int injail, struct test *test)
84172106Srwatson{
85172106Srwatson	int error;
86172106Srwatson
87172106Srwatson	error = chmod(fpath, 0640);
88172106Srwatson	if (asroot && injail)
89172106Srwatson		expect("priv_vfs_chmod_froot(asroot, injail)", error, 0, 0);
90172106Srwatson	if (asroot && !injail)
91172106Srwatson		expect("priv_vfs_chmod_froot(asroot, !injail)", error, 0, 0);
92172106Srwatson	if (!asroot && injail)
93172106Srwatson		expect("priv_vfs_chmod_froot(!asroot, injail)", error, -1,
94172106Srwatson		    EPERM);
95172106Srwatson	if (!asroot && !injail)
96172106Srwatson		expect("priv_vfs_chmod_froot(!asroot, !injail)", error, -1,
97172106Srwatson		    EPERM);
98172106Srwatson}
99172106Srwatson
100172106Srwatsonvoid
101172106Srwatsonpriv_vfs_chmod_fowner(int asroot, int injail, struct test *test)
102172106Srwatson{
103172106Srwatson	int error;
104172106Srwatson
105172106Srwatson	error = chmod(fpath, 0640);
106172106Srwatson	if (asroot && injail)
107172106Srwatson		expect("priv_vfs_chmod_fowner(asroot, injail)", error, 0, 0);
108172106Srwatson	if (asroot && !injail)
109172106Srwatson		expect("priv_vfs_chmod_fowner(asroot, !injail)", error, 0,
110172106Srwatson		    0);
111172106Srwatson	if (!asroot && injail)
112172106Srwatson		expect("priv_vfs_chmod_fowner(!asroot, injail)", error, 0,
113172106Srwatson		    0);
114172106Srwatson	if (!asroot && !injail)
115172106Srwatson		expect("priv_vfs_chmod_fowner(!asroot, !injail)", error, 0,
116172106Srwatson		    0);
117172106Srwatson}
118172106Srwatson
119172106Srwatsonvoid
120172106Srwatsonpriv_vfs_chmod_fother(int asroot, int injail, struct test *test)
121172106Srwatson{
122172106Srwatson	int error;
123172106Srwatson
124172106Srwatson	error = chmod(fpath, 0640);
125172106Srwatson	if (asroot && injail)
126172106Srwatson		expect("priv_vfs_chmod_fother(asroot, injail)", error, 0, 0);
127172106Srwatson	if (asroot && !injail)
128172106Srwatson		expect("priv_vfs_chmod_fother(asroot, !injail)", error, 0,
129172106Srwatson		    0);
130172106Srwatson	if (!asroot && injail)
131172106Srwatson		expect("priv_vfs_chmod_fother(!asroot, injail)", error, -1,
132172106Srwatson		    EPERM);
133172106Srwatson	if (!asroot && !injail)
134172106Srwatson		expect("priv_vfs_chmod_fother(!asroot, !injail)", error, -1,
135172106Srwatson		    EPERM);
136172106Srwatson}
137172106Srwatson
138172106Srwatsonvoid
139172106Srwatsonpriv_vfs_chmod_cleanup(int asroot, int injail, struct test *test)
140172106Srwatson{
141172106Srwatson
142172106Srwatson	if (fpath_initialized) {
143172106Srwatson		(void)unlink(fpath);
144172106Srwatson		fpath_initialized = 0;
145172106Srwatson	}
146172106Srwatson}
147