1162271Srwatson/*-
2162271Srwatson * Copyright (c) 2006 nCircle Network Security, Inc.
3172106Srwatson * Copyright (c) 2007 Robert N. M. Watson
4162271Srwatson * All rights reserved.
5162271Srwatson *
6162271Srwatson * This software was developed by Robert N. M. Watson for the TrustedBSD
7162271Srwatson * Project under contract to nCircle Network Security, Inc.
8162271Srwatson *
9162271Srwatson * Redistribution and use in source and binary forms, with or without
10162271Srwatson * modification, are permitted provided that the following conditions
11162271Srwatson * are met:
12162271Srwatson * 1. Redistributions of source code must retain the above copyright
13162271Srwatson *    notice, this list of conditions and the following disclaimer.
14162271Srwatson * 2. Redistributions in binary form must reproduce the above copyright
15162271Srwatson *    notice, this list of conditions and the following disclaimer in the
16162271Srwatson *    documentation and/or other materials provided with the distribution.
17162271Srwatson *
18162271Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19162271Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20162271Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21162271Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
22162271Srwatson * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23162271Srwatson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24162271Srwatson * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25162271Srwatson * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26162271Srwatson * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27162271Srwatson * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28162271Srwatson * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29162271Srwatson *
30162271Srwatson * $FreeBSD$
31162271Srwatson */
32162271Srwatson
33162271Srwatson/*
34172106Srwatson * Check that privilege is required to set the sticky bit on a file but not a
35172106Srwatson * directory.
36162271Srwatson */
37162271Srwatson
38162271Srwatson#include <sys/stat.h>
39162271Srwatson
40162271Srwatson#include <err.h>
41162271Srwatson#include <errno.h>
42162271Srwatson#include <unistd.h>
43162271Srwatson
44162271Srwatson#include "main.h"
45162271Srwatson
46172106Srwatsonchar fpath[1024];
47172106Srwatsonint fpath_initialized;
48172106Srwatson
49172106Srwatsonchar dpath[1024];
50172106Srwatsonint dpath_initialized;
51172106Srwatson
52172106Srwatsonint
53172106Srwatsonpriv_vfs_stickyfile_dir_fowner_setup(int asroot, int injail,
54172106Srwatson    struct test *test)
55162271Srwatson{
56162271Srwatson
57172106Srwatson	setup_dir("priv_vfs_stickyfile_fowner_setup: dpath", dpath,
58172106Srwatson	    UID_OWNER, GID_OWNER, 0700);
59172106Srwatson	dpath_initialized = 1;
60172106Srwatson	return (0);
61162271Srwatson}
62162271Srwatson
63172106Srwatsonint
64172106Srwatsonpriv_vfs_stickyfile_dir_fother_setup(int asroot, int injail,
65172106Srwatson    struct test *test)
66172106Srwatson{
67172106Srwatson
68172106Srwatson	setup_dir("priv_vfs_stickyfile_fother_setup: dpath", dpath,
69172106Srwatson	    UID_OTHER, GID_OTHER, 0700);
70172106Srwatson	dpath_initialized = 1;
71172106Srwatson	return (0);
72172106Srwatson}
73172106Srwatson
74172106Srwatsonint
75172106Srwatsonpriv_vfs_stickyfile_file_fowner_setup(int asroot, int injail,
76172106Srwatson    struct test *test)
77172106Srwatson{
78172106Srwatson
79172106Srwatson	setup_file("priv_vfs_stickyfile_fowner_setup: fpath", fpath,
80172106Srwatson	    UID_OWNER, GID_OWNER, 0600);
81172106Srwatson	fpath_initialized = 1;
82172106Srwatson	return (0);
83172106Srwatson}
84172106Srwatson
85172106Srwatsonint
86172106Srwatsonpriv_vfs_stickyfile_file_fother_setup(int asroot, int injail,
87172106Srwatson    struct test *test)
88172106Srwatson{
89172106Srwatson
90172106Srwatson	setup_file("priv_vfs_stickyfile_fother_setup: fpath", fpath,
91172106Srwatson	    UID_OTHER, GID_OTHER, 0600);
92172106Srwatson	fpath_initialized = 1;
93172106Srwatson	return (0);
94172106Srwatson}
95172106Srwatson
96162271Srwatsonvoid
97172106Srwatsonpriv_vfs_stickyfile_dir_fowner(int asroot, int injail, struct test *test)
98162271Srwatson{
99172106Srwatson	int error;
100162271Srwatson
101172106Srwatson	error = chmod(dpath, 0700 | S_ISTXT);
102172106Srwatson	if (asroot && injail)
103172106Srwatson		expect("priv_vfs_stickyfile_dir_fowner(root, jail)", error,
104172106Srwatson		    0, 0);
105172106Srwatson	if (asroot && !injail)
106172106Srwatson		expect("priv_vfs_stickyfile_dir_fowner(root, !jail)", error,
107172106Srwatson		    0, 0);
108172106Srwatson	if (!asroot && injail)
109172106Srwatson		expect("priv_vfs_stickyfile_dir_fowner(!root, jail)", error,
110172106Srwatson		    0, 0);
111172106Srwatson	if (!asroot && !injail)
112172106Srwatson		expect("priv_vfs_stickyfile_dir_fowner(!root, !jail)", error,
113172106Srwatson		    0, 0);
114172106Srwatson}
115162271Srwatson
116172106Srwatsonvoid
117172106Srwatsonpriv_vfs_stickyfile_dir_fother(int asroot, int injail, struct test *test)
118172106Srwatson{
119172106Srwatson	int error;
120162271Srwatson
121172106Srwatson	error = chmod(dpath, 0700 | S_ISTXT);
122172106Srwatson	if (asroot && injail)
123172106Srwatson		expect("priv_vfs_stickyfile_dir_fother(root, jail)", error,
124172106Srwatson		    0, 0);
125172106Srwatson	if (asroot && !injail)
126172106Srwatson		expect("priv_vfs_stickyfile_dir_fother(root, !jail)", error,
127172106Srwatson		    0, 0);
128172106Srwatson	if (!asroot && injail)
129172106Srwatson		expect("priv_vfs_stickyfile_dir_fother(!root, jail)", error,
130172106Srwatson		    -1, EPERM);
131172106Srwatson	if (!asroot && !injail)
132172106Srwatson		expect("priv_vfs_stickyfile_dir_fother(!root, !jail)", error,
133172106Srwatson		    -1, EPERM);
134172106Srwatson}
135162271Srwatson
136172106Srwatsonvoid
137172106Srwatsonpriv_vfs_stickyfile_file_fowner(int asroot, int injail, struct test *test)
138172106Srwatson{
139172106Srwatson	int error;
140162271Srwatson
141172106Srwatson	error = chmod(fpath, 0600 | S_ISTXT);
142172106Srwatson	if (asroot && injail)
143172106Srwatson		expect("priv_vfs_stickyfile_file_fowner(root, jail)", error,
144172106Srwatson		    0, 0);
145172106Srwatson	if (asroot && !injail)
146172106Srwatson		expect("priv_vfs_stickyfile_file_fowner(root, !jail)", error,
147172106Srwatson		    0, 0);
148172106Srwatson	if (!asroot && injail)
149172106Srwatson		expect("priv_vfs_stickyfile_file_fowner(!root, jail)", error,
150172106Srwatson		    -1, EFTYPE);
151172106Srwatson	if (!asroot && !injail)
152172106Srwatson		expect("priv_vfs_stickyfile_file_fowner(!root, !jail)", error,
153172106Srwatson		    -1, EFTYPE);
154172106Srwatson}
155162271Srwatson
156172106Srwatsonvoid
157172106Srwatsonpriv_vfs_stickyfile_file_fother(int asroot, int injail, struct test *test)
158172106Srwatson{
159172106Srwatson	int error;
160162271Srwatson
161172106Srwatson	error = chmod(fpath, 0600 | S_ISTXT);
162172106Srwatson	if (asroot && injail)
163172106Srwatson		expect("priv_vfs_stickyfile_file_fother(root, jail)", error,
164172106Srwatson		    0, 0);
165172106Srwatson	if (asroot && !injail)
166172106Srwatson		expect("priv_vfs_stickyfile_file_fother(root, !jail)", error,
167172106Srwatson		    0, 0);
168172106Srwatson	if (!asroot && injail)
169172106Srwatson		expect("priv_vfs_stickyfile_file_fother(!root, jail)", error,
170172106Srwatson		    -1, EPERM);
171172106Srwatson	if (!asroot && !injail)
172172106Srwatson		expect("priv_vfs_stickyfile_file_fother(!root, !jail)", error,
173172106Srwatson		    -1, EPERM);
174172106Srwatson}
175162271Srwatson
176172106Srwatsonvoid
177172106Srwatsonpriv_vfs_stickyfile_dir_cleanup(int asroot, int injail, struct test *test)
178172106Srwatson{
179162271Srwatson
180172106Srwatson	if (dpath_initialized) {
181172106Srwatson		(void)rmdir(dpath);
182172106Srwatson		dpath_initialized = 0;
183162271Srwatson	}
184172106Srwatson}
185162271Srwatson
186172106Srwatsonvoid
187172106Srwatsonpriv_vfs_stickyfile_file_cleanup(int asroot, int injail, struct test *test)
188172106Srwatson{
189162271Srwatson
190172106Srwatson	if (fpath_initialized) {
191172106Srwatson		(void)unlink(fpath);
192172106Srwatson		fpath_initialized = 0;
193162271Srwatson	}
194162271Srwatson}
195