2 * Copyright (c) 2006 nCircle Network Security, Inc.
3 * Copyright (c) 2007 Robert N. M. Watson
6 * This software was developed by Robert N. M. Watson for the TrustedBSD
7 * Project under contract to nCircle Network Security, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
22 * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Test privileges associated with setting file flags on files; whether or
33 * not it requires privilege depends on the flag, and some flags cannot be
37 #include <sys/types.h>
48 static char fpath[1024];
49 static int fpath_initialized;
52 * For chflags, we consider three dimmensions: process owner, file owner, and
53 * flag type. The calling framework handles variations in process owner; the
54 * rest are handled via multiple tests. One cleanup function is used.
57 getflags(char *fpathp)
61 if (stat(fpathp, &sb) < 0)
62 err(-1, "stat(%s)", fpathp);
68 priv_vfs_chflags_froot_setup(int asroot, int injail, struct test *test)
71 setup_file("priv_vfs_chflags_froot_setup: fpath", fpath, UID_ROOT,
73 fpath_initialized = 1;
78 priv_vfs_chflags_fowner_setup(int asroot, int injail,
82 setup_file("priv_vfs_chflags_fowner_setup: fpath", fpath, UID_OWNER,
84 fpath_initialized = 1;
89 priv_vfs_chflags_fother_setup(int asroot, int injail,
93 setup_file("priv_vfs_chflags_fowner_setup: fpath", fpath, UID_OTHER,
95 fpath_initialized = 1;
100 priv_vfs_chflags_froot_uflags(int asroot, int injail,
106 flags = getflags(fpath);
108 error = chflags(fpath, flags);
109 if (asroot && injail)
110 expect("priv_vfs_chflags_froot_uflags(asroot, injail)",
112 if (asroot && !injail)
113 expect("priv_vfs_chflags_froot_uflags(asroot, !injail)",
115 if (!asroot && injail)
116 expect("priv_vfs_chflags_froot_uflags(!asroot, injail)",
118 if (!asroot && !injail)
119 expect("priv_vfs_chflags_froot_uflags(!asroot, !injail)",
124 priv_vfs_chflags_fowner_uflags(int asroot, int injail,
130 flags = getflags(fpath);
132 error = chflags(fpath, flags);
133 if (asroot && injail)
134 expect("priv_vfs_chflags_fowner_uflags(asroot, injail)",
136 if (asroot && !injail)
137 expect("priv_vfs_chflags_fowner_uflags(asroot, !injail)",
139 if (!asroot && injail)
140 expect("priv_vfs_chflags_fowner_uflags(!asroot, injail)",
142 if (!asroot && !injail)
143 expect("priv_vfs_chflags_fowner_uflags(!asroot, !injail)",
148 priv_vfs_chflags_fother_uflags(int asroot, int injail,
154 flags = getflags(fpath);
156 error = chflags(fpath, flags);
157 if (asroot && injail)
158 expect("priv_vfs_chflags_fother_uflags(asroot, injail)",
160 if (asroot && !injail)
161 expect("priv_vfs_chflags_fother_uflags(asroot, !injail)",
163 if (!asroot && injail)
164 expect("priv_vfs_chflags_fother_uflags(!asroot, injail)",
166 if (!asroot && !injail)
167 expect("priv_vfs_chflags_fother_uflags(!asroot, !injail)",
172 priv_vfs_chflags_froot_sflags(int asroot, int injail,
178 flags = getflags(fpath);
179 flags |= SF_ARCHIVED;
180 error = chflags(fpath, flags);
181 if (asroot && injail)
182 expect("priv_vfs_chflags_froot_sflags(asroot, injail)",
184 if (asroot && !injail)
185 expect("priv_vfs_chflags_froot_sflags(asroot, !injail)",
187 if (!asroot && injail)
188 expect("priv_vfs_chflags_froot_sflags(!asroot, injail)",
190 if (!asroot && !injail)
191 expect("priv_vfs_chflags_froot_sflags(!asroot, !injail)",
196 priv_vfs_chflags_fowner_sflags(int asroot, int injail,
202 flags = getflags(fpath);
203 flags |= SF_ARCHIVED;
204 error = chflags(fpath, flags);
205 if (asroot && injail)
206 expect("priv_vfs_chflags_fowner_sflags(asroot, injail)",
208 if (asroot && !injail)
209 expect("priv_vfs_chflags_fowner_sflags(asroot, !injail)",
211 if (!asroot && injail)
212 expect("priv_vfs_chflags_fowner_sflags(!asroot, injail)",
214 if (!asroot && !injail)
215 expect("priv_vfs_chflags_fowner_sflags(!asroot, !injail)",
220 priv_vfs_chflags_fother_sflags(int asroot, int injail,
226 flags = getflags(fpath);
227 flags |= SF_ARCHIVED;
228 error = chflags(fpath, flags);
229 if (asroot && injail)
230 expect("priv_vfs_chflags_fother_sflags(asroot, injail)",
232 if (asroot && !injail)
233 expect("priv_vfs_chflags_fother_sflags(asroot, !injail)",
235 if (!asroot && injail)
236 expect("priv_vfs_chflags_fother_sflags(!asroot, injail)",
238 if (!asroot && !injail)
239 expect("priv_vfs_chflags_fother_sflags(!asroot, !injail)",
244 priv_vfs_chflags_cleanup(int asroot, int injail, struct test *test)
247 if (fpath_initialized) {
248 (void)chflags(fpath, 0);
250 fpath_initialized = 0;