Pass read/write CRx registers to userspace
[freebsd-src/fkvm-freebsd.git] / tools / regression / priv / priv_acct.c
blobd1324bbeb49c7140c6564da885e8a0c37c8d9115
1 /*-
2 * Copyright (c) 2006 nCircle Network Security, Inc.
3 * Copyright (c) 2007 Robert N. M. Watson
4 * All rights reserved.
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
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.
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.
30 * $FreeBSD$
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>
44 #include <sys/stat.h>
45 #include <sys/sysctl.h>
47 #include <err.h>
48 #include <errno.h>
49 #include <stdlib.h>
50 #include <unistd.h>
52 #include "main.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];
64 int
65 priv_acct_setup(int asroot, int injail, struct test *test)
67 size_t len;
68 int i;
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);
75 if (i != 0) {
76 warnx("sysctlbyname(%s) indicates accounting configured",
77 SYSCTL_NAME);
78 return (-1);
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)");
89 return (-1);
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);
98 return (0);
101 void
102 priv_acct_cleanup(int asroot, int injail, struct test *test)
105 (void)acct(NULL);
106 if (fpath1_initialized) {
107 (void)unlink(fpath1);
108 fpath1_initialized = 0;
110 if (fpath2_initialized) {
111 (void)unlink(fpath2);
112 fpath2_initialized = 0;
116 void
117 priv_acct_enable(int asroot, int injail, struct test *test)
119 int error;
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);
132 void
133 priv_acct_disable(int asroot, int injail, struct test *test)
135 int error;
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);
148 void
149 priv_acct_rotate(int asroot, int injail, struct test *test)
151 int error;
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);
164 void
165 priv_acct_noopdisable(int asroot, int injail, struct test *test)
167 int error;
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);