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.
34 * Test privileges associated with setting file flags on files; whether or
35 * not it requires privilege depends on the flag, and some flags cannot be
39 #include <sys/types.h>
50 static char fpath[1024];
51 static int fpath_initialized;
54 * For chflags, we consider three dimmensions: process owner, file owner, and
55 * flag type. The calling framework handles variations in process owner; the
56 * rest are handled via multiple tests. One cleanup function is used.
59 getflags(char *fpathp)
63 if (stat(fpathp, &sb) < 0)
64 err(-1, "stat(%s)", fpathp);
70 priv_vfs_chflags_froot_setup(int asroot, int injail, struct test *test)
73 setup_file("priv_vfs_chflags_froot_setup: fpath", fpath, UID_ROOT,
75 fpath_initialized = 1;
80 priv_vfs_chflags_fowner_setup(int asroot, int injail,
84 setup_file("priv_vfs_chflags_fowner_setup: fpath", fpath, UID_OWNER,
86 fpath_initialized = 1;
91 priv_vfs_chflags_fother_setup(int asroot, int injail,
95 setup_file("priv_vfs_chflags_fowner_setup: fpath", fpath, UID_OTHER,
97 fpath_initialized = 1;
102 priv_vfs_chflags_froot_uflags(int asroot, int injail,
108 flags = getflags(fpath);
110 error = chflags(fpath, flags);
111 if (asroot && injail)
112 expect("priv_vfs_chflags_froot_uflags(asroot, injail)",
114 if (asroot && !injail)
115 expect("priv_vfs_chflags_froot_uflags(asroot, !injail)",
117 if (!asroot && injail)
118 expect("priv_vfs_chflags_froot_uflags(!asroot, injail)",
120 if (!asroot && !injail)
121 expect("priv_vfs_chflags_froot_uflags(!asroot, !injail)",
126 priv_vfs_chflags_fowner_uflags(int asroot, int injail,
132 flags = getflags(fpath);
134 error = chflags(fpath, flags);
135 if (asroot && injail)
136 expect("priv_vfs_chflags_fowner_uflags(asroot, injail)",
138 if (asroot && !injail)
139 expect("priv_vfs_chflags_fowner_uflags(asroot, !injail)",
141 if (!asroot && injail)
142 expect("priv_vfs_chflags_fowner_uflags(!asroot, injail)",
144 if (!asroot && !injail)
145 expect("priv_vfs_chflags_fowner_uflags(!asroot, !injail)",
150 priv_vfs_chflags_fother_uflags(int asroot, int injail,
156 flags = getflags(fpath);
158 error = chflags(fpath, flags);
159 if (asroot && injail)
160 expect("priv_vfs_chflags_fother_uflags(asroot, injail)",
162 if (asroot && !injail)
163 expect("priv_vfs_chflags_fother_uflags(asroot, !injail)",
165 if (!asroot && injail)
166 expect("priv_vfs_chflags_fother_uflags(!asroot, injail)",
168 if (!asroot && !injail)
169 expect("priv_vfs_chflags_fother_uflags(!asroot, !injail)",
174 priv_vfs_chflags_froot_sflags(int asroot, int injail,
180 flags = getflags(fpath);
181 flags |= SF_ARCHIVED;
182 error = chflags(fpath, flags);
183 if (asroot && injail)
184 expect("priv_vfs_chflags_froot_sflags(asroot, injail)",
186 if (asroot && !injail)
187 expect("priv_vfs_chflags_froot_sflags(asroot, !injail)",
189 if (!asroot && injail)
190 expect("priv_vfs_chflags_froot_sflags(!asroot, injail)",
192 if (!asroot && !injail)
193 expect("priv_vfs_chflags_froot_sflags(!asroot, !injail)",
198 priv_vfs_chflags_fowner_sflags(int asroot, int injail,
204 flags = getflags(fpath);
205 flags |= SF_ARCHIVED;
206 error = chflags(fpath, flags);
207 if (asroot && injail)
208 expect("priv_vfs_chflags_fowner_sflags(asroot, injail)",
210 if (asroot && !injail)
211 expect("priv_vfs_chflags_fowner_sflags(asroot, !injail)",
213 if (!asroot && injail)
214 expect("priv_vfs_chflags_fowner_sflags(!asroot, injail)",
216 if (!asroot && !injail)
217 expect("priv_vfs_chflags_fowner_sflags(!asroot, !injail)",
222 priv_vfs_chflags_fother_sflags(int asroot, int injail,
228 flags = getflags(fpath);
229 flags |= SF_ARCHIVED;
230 error = chflags(fpath, flags);
231 if (asroot && injail)
232 expect("priv_vfs_chflags_fother_sflags(asroot, injail)",
234 if (asroot && !injail)
235 expect("priv_vfs_chflags_fother_sflags(asroot, !injail)",
237 if (!asroot && injail)
238 expect("priv_vfs_chflags_fother_sflags(!asroot, injail)",
240 if (!asroot && !injail)
241 expect("priv_vfs_chflags_fother_sflags(!asroot, !injail)",
246 priv_vfs_chflags_cleanup(int asroot, int injail, struct test *test)
249 if (fpath_initialized) {
250 (void)chflags(fpath, 0);
252 fpath_initialized = 0;