xref: /freebsd/tools/regression/priv/priv_acct.c (revision aa0a1e58)
1 /*-
2  * Copyright (c) 2006 nCircle Network Security, Inc.
3  * Copyright (c) 2007 Robert N. M. Watson
4  * All rights reserved.
5  *
6  * This software was developed by Robert N. M. Watson for the TrustedBSD
7  * Project under contract to nCircle Network Security, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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.
29  *
30  * $FreeBSD$
31  */
32 
33 /*
34  * Test that configuring accounting requires privilege.  We test four cases
35  * across {!jail, jail}:
36  *
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
41  */
42 
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/sysctl.h>
46 
47 #include <err.h>
48 #include <errno.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 
52 #include "main.h"
53 
54 #define	SYSCTL_NAME	"kern.acct_configured"
55 
56 /*
57  * Actual filenames used across all of the tests.
58  */
59 static int	fpath1_initialized;
60 static char	fpath1[1024];
61 static int	fpath2_initialized;
62 static char	fpath2[1024];
63 
64 int
65 priv_acct_setup(int asroot, int injail, struct test *test)
66 {
67 	size_t len;
68 	int i;
69 
70 	len = sizeof(i);
71 	if (sysctlbyname(SYSCTL_NAME, &i, &len, NULL, 0) < 0) {
72 		warn("priv_acct_setup: sysctlbyname(%s)", SYSCTL_NAME);
73 		return (-1);
74 	}
75 	if (i != 0) {
76 		warnx("sysctlbyname(%s) indicates accounting configured",
77 		    SYSCTL_NAME);
78 		return (-1);
79 	}
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;
84 
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)");
89 			return (-1);
90 		}
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);
95 			return (-1);
96 		}
97 	}
98 	return (0);
99 }
100 
101 void
102 priv_acct_cleanup(int asroot, int injail, struct test *test)
103 {
104 
105 	(void)acct(NULL);
106 	if (fpath1_initialized) {
107 		(void)unlink(fpath1);
108 		fpath1_initialized = 0;
109 	}
110 	if (fpath2_initialized) {
111 		(void)unlink(fpath2);
112 		fpath2_initialized = 0;
113 	}
114 }
115 
116 void
117 priv_acct_enable(int asroot, int injail, struct test *test)
118 {
119 	int error;
120 
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);
130 }
131 
132 void
133 priv_acct_disable(int asroot, int injail, struct test *test)
134 {
135 	int error;
136 
137 	error = acct(NULL);
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);
146 }
147 
148 void
149 priv_acct_rotate(int asroot, int injail, struct test *test)
150 {
151 	int error;
152 
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);
162 }
163 
164 void
165 priv_acct_noopdisable(int asroot, int injail, struct test *test)
166 {
167 	int error;
168 
169 	error = acct(NULL);
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);
178 }
179