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 that configuring accounting requires privilege. We test four cases
35 * across {!jail, jail}:
37 * priv_acct_enable - enable accounting from a disabled state
38 * priv_acct_disable - disable accounting from an enabled state
39 * priv_acct_rotate - rotate the accounting file
40 * priv_acct_noopdisable - disable accounting when already disabled
43 #include <sys/types.h>
45 #include <sys/sysctl.h>
54 #define SYSCTL_NAME "kern.acct_configured"
57 * Actual filenames used across all of the tests.
59 static int fpath1_initialized
;
60 static char fpath1
[1024];
61 static int fpath2_initialized
;
62 static char fpath2
[1024];
65 priv_acct_setup(int asroot
, int injail
, struct test
*test
)
71 if (sysctlbyname(SYSCTL_NAME
, &i
, &len
, NULL
, 0) < 0) {
72 warn("priv_acct_setup: sysctlbyname(%s)", SYSCTL_NAME
);
76 warnx("sysctlbyname(%s) indicates accounting configured",
80 setup_file("priv_acct_setup: fpath1", fpath1
, 0, 0, 0666);
81 fpath1_initialized
= 1;
82 setup_file("priv_acct_setup: fpath2", fpath2
, 0, 0, 0666);
83 fpath2_initialized
= 1;
85 if (test
->t_test_func
== priv_acct_enable
||
86 test
->t_test_func
== priv_acct_noopdisable
) {
87 if (acct(NULL
) != 0) {
88 warn("priv_acct_setup: acct(NULL)");
91 } else if (test
->t_test_func
== priv_acct_disable
||
92 test
->t_test_func
== priv_acct_rotate
) {
93 if (acct(fpath1
) != 0) {
94 warn("priv_acct_setup: acct(\"%s\")", fpath1
);
102 priv_acct_cleanup(int asroot
, int injail
, struct test
*test
)
106 if (fpath1_initialized
) {
107 (void)unlink(fpath1
);
108 fpath1_initialized
= 0;
110 if (fpath2_initialized
) {
111 (void)unlink(fpath2
);
112 fpath2_initialized
= 0;
117 priv_acct_enable(int asroot
, int injail
, struct test
*test
)
121 error
= acct(fpath1
);
122 if (asroot
&& injail
)
123 expect("priv_acct_enable(root, jail)", error
, -1, EPERM
);
124 if (asroot
&& !injail
)
125 expect("priv_acct_enable(root, !jail)", error
, 0, 0);
126 if (!asroot
&& injail
)
127 expect("priv_acct_enable(!root, jail)", error
, -1, EPERM
);
128 if (!asroot
&& !injail
)
129 expect("priv_acct_enable(!root, !jail)", error
, -1, EPERM
);
133 priv_acct_disable(int asroot
, int injail
, struct test
*test
)
138 if (asroot
&& injail
)
139 expect("priv_acct_disable(root, jail)", error
, -1, EPERM
);
140 if (asroot
&& !injail
)
141 expect("priv_acct_disable(root, !jail)", error
, 0, 0);
142 if (!asroot
&& injail
)
143 expect("priv_acct_disable(!root, jail)", error
, -1, EPERM
);
144 if (!asroot
&& !injail
)
145 expect("priv_acct_disable(!root, !jail)", error
, -1, EPERM
);
149 priv_acct_rotate(int asroot
, int injail
, struct test
*test
)
153 error
= acct(fpath2
);
154 if (asroot
&& injail
)
155 expect("priv_acct_rotate(root, jail)", error
, -1, EPERM
);
156 if (asroot
&& !injail
)
157 expect("priv_acct_rotate(root, !jail)", error
, 0, 0);
158 if (!asroot
&& injail
)
159 expect("priv_acct_rotate(!root, jail)", error
, -1, EPERM
);
160 if (!asroot
&& !injail
)
161 expect("priv_acct_rotate(!root, !jail)", error
, -1, EPERM
);
165 priv_acct_noopdisable(int asroot
, int injail
, struct test
*test
)
170 if (asroot
&& injail
)
171 expect("priv_acct_noopdisable(root, jail)", error
, -1, EPERM
);
172 if (asroot
&& !injail
)
173 expect("priv_acct_noopdisable(root, !jail)", error
, 0, 0);
174 if (!asroot
&& injail
)
175 expect("priv_acct_noopdisable(!root, jail)", error
, -1, EPERM
);
176 if (!asroot
&& !injail
)
177 expect("priv_acct_noopdisable(!root, !jail)", error
, -1, EPERM
);