1162271Srwatson/*-
2162271Srwatson * Copyright (c) 2006 nCircle Network Security, Inc.
3172106Srwatson * Copyright (c) 2007 Robert N. M. Watson
4162271Srwatson * All rights reserved.
5162271Srwatson *
6162271Srwatson * This software was developed by Robert N. M. Watson for the TrustedBSD
7162271Srwatson * Project under contract to nCircle Network Security, Inc.
8162271Srwatson *
9162271Srwatson * Redistribution and use in source and binary forms, with or without
10162271Srwatson * modification, are permitted provided that the following conditions
11162271Srwatson * are met:
12162271Srwatson * 1. Redistributions of source code must retain the above copyright
13162271Srwatson *    notice, this list of conditions and the following disclaimer.
14162271Srwatson * 2. Redistributions in binary form must reproduce the above copyright
15162271Srwatson *    notice, this list of conditions and the following disclaimer in the
16162271Srwatson *    documentation and/or other materials provided with the distribution.
17162271Srwatson *
18162271Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19162271Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20162271Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21162271Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
22162271Srwatson * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23162271Srwatson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24162271Srwatson * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25162271Srwatson * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26162271Srwatson * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27162271Srwatson * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28162271Srwatson * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29162271Srwatson *
30162271Srwatson * $FreeBSD: releng/10.3/tools/regression/priv/priv_sysctl_write.c 172106 2007-09-09 23:08:39Z rwatson $
31162271Srwatson */
32162271Srwatson
33162271Srwatson/*
34172106Srwatson * Two privileges exist for writing sysctls -- one for sysctls writable only
35172106Srwatson * outside of jail (PRIV_SYSCTL_WRITE) and one for those also writable inside
36172106Srwatson * jail (PRIV_SYSCTL_WRITEJAIL).
37172106Srwatson *
38172106Srwatson * Test the prior by attempting to write to kern.domainname, and the latter
39172106Srwatson * by attempting to write to kern.hostname.
40162271Srwatson */
41162271Srwatson
42162271Srwatson#include <sys/types.h>
43162271Srwatson#include <sys/sysctl.h>
44162271Srwatson
45162271Srwatson#include <err.h>
46162271Srwatson#include <errno.h>
47162271Srwatson#include <string.h>
48162271Srwatson#include <unistd.h>
49162271Srwatson
50162271Srwatson#include "main.h"
51162271Srwatson
52162271Srwatson#define KERN_HOSTNAME_STRING	"kern.hostname"
53172106Srwatson#define	KERN_DOMAINNAME_STRING	"kern.domainname"
54162271Srwatson
55172106Srwatsonstatic char stored_hostname[1024];
56172106Srwatsonstatic char stored_domainname[1024];
57172106Srwatson
58172106Srwatsonint
59172106Srwatsonpriv_sysctl_write_setup(int asroot, int injail, struct test *test)
60162271Srwatson{
61162271Srwatson	size_t len;
62162271Srwatson	int error;
63162271Srwatson
64172106Srwatson	len = sizeof(stored_hostname);
65172106Srwatson	error = sysctlbyname(KERN_HOSTNAME_STRING, stored_hostname, &len,
66172106Srwatson	    NULL, 0);
67172106Srwatson	if (error) {
68172106Srwatson		warn("priv_sysctl_write_setup: sysctlbyname(\"%s\")",
69172106Srwatson		    KERN_HOSTNAME_STRING);
70172106Srwatson		return (-1);
71172106Srwatson	}
72162271Srwatson
73172106Srwatson	len = sizeof(stored_hostname);
74172106Srwatson	error = sysctlbyname(KERN_DOMAINNAME_STRING, stored_domainname, &len,
75172106Srwatson	    NULL, 0);
76172106Srwatson	if (error) {
77172106Srwatson		warn("priv_sysctl_write_setup: sysctlbyname(\"%s\")",
78172106Srwatson		    KERN_DOMAINNAME_STRING);
79172106Srwatson		return (-1);
80172106Srwatson	}
81162271Srwatson
82172106Srwatson	return (0);
83172106Srwatson}
84162271Srwatson
85172106Srwatsonvoid
86172106Srwatsonpriv_sysctl_write(int asroot, int injail, struct test *test)
87172106Srwatson{
88172106Srwatson	int error;
89162271Srwatson
90172106Srwatson	error = sysctlbyname(KERN_DOMAINNAME_STRING, NULL, NULL,
91172106Srwatson	    stored_domainname, strlen(stored_domainname));
92172106Srwatson	if (asroot && injail)
93172106Srwatson		expect("priv_sysctl_write(asroot, injail)", error, -1,
94172106Srwatson		    EPERM);
95172106Srwatson	if (asroot && !injail)
96172106Srwatson		expect("priv_sysctl_write(asroot, !injail)", error, 0, 0);
97172106Srwatson	if (!asroot && injail)
98172106Srwatson		expect("priv_sysctl_write(!asroot, injail)", error, -1,
99172106Srwatson		    EPERM);
100172106Srwatson	if (!asroot && !injail)
101172106Srwatson		expect("priv_sysctl_write(!asroot, !injail)", error, -1,
102172106Srwatson		    EPERM);
103162271Srwatson}
104172106Srwatson
105172106Srwatsonvoid
106172106Srwatsonpriv_sysctl_writejail(int asroot, int injail, struct test *test)
107172106Srwatson{
108172106Srwatson	int error;
109172106Srwatson
110172106Srwatson	error = sysctlbyname(KERN_HOSTNAME_STRING, NULL, NULL,
111172106Srwatson	    stored_hostname, strlen(stored_hostname));
112172106Srwatson	if (asroot && injail)
113172106Srwatson		expect("priv_sysctl_writejail(asroot, injail)", error, 0, 0);
114172106Srwatson	if (asroot && !injail)
115172106Srwatson		expect("priv_sysctl_writejail(asroot, !injail)", error, 0, 0);
116172106Srwatson	if (!asroot && injail)
117172106Srwatson		expect("priv_sysctl_writejail(!asroot, injail)", error, -1,
118172106Srwatson		    EPERM);
119172106Srwatson	if (!asroot && !injail)
120172106Srwatson		expect("priv_sysctl_writejail(!asroot, !injail)", error, -1,
121172106Srwatson		    EPERM);
122172106Srwatson}
123172106Srwatson
124172106Srwatsonvoid
125172106Srwatsonpriv_sysctl_write_cleanup(int asroot, int injail, struct test *test)
126172106Srwatson{
127172106Srwatson
128172106Srwatson}
129