xref: /freebsd/contrib/netbsd-tests/sbin/sysctl/t_perm.sh (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
1*57718be8SEnji Cooper# $NetBSD: t_perm.sh,v 1.6 2012/03/18 09:46:50 jruoho Exp $
2*57718be8SEnji Cooper#
3*57718be8SEnji Cooper# Copyright (c) 2011 The NetBSD Foundation, Inc.
4*57718be8SEnji Cooper# All rights reserved.
5*57718be8SEnji Cooper#
6*57718be8SEnji Cooper# This code is derived from software contributed to The NetBSD Foundation
7*57718be8SEnji Cooper# by Jukka Ruohonen.
8*57718be8SEnji Cooper#
9*57718be8SEnji Cooper# Redistribution and use in source and binary forms, with or without
10*57718be8SEnji Cooper# modification, are permitted provided that the following conditions
11*57718be8SEnji Cooper# are met:
12*57718be8SEnji Cooper# 1. Redistributions of source code must retain the above copyright
13*57718be8SEnji Cooper#    notice, this list of conditions and the following disclaimer.
14*57718be8SEnji Cooper# 2. Redistributions in binary form must reproduce the above copyright
15*57718be8SEnji Cooper#    notice, this list of conditions and the following disclaimer in the
16*57718be8SEnji Cooper#    documentation and/or other materials provided with the distribution.
17*57718be8SEnji Cooper#
18*57718be8SEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19*57718be8SEnji Cooper# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20*57718be8SEnji Cooper# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*57718be8SEnji Cooper# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22*57718be8SEnji Cooper# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*57718be8SEnji Cooper# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*57718be8SEnji Cooper# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*57718be8SEnji Cooper# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*57718be8SEnji Cooper# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*57718be8SEnji Cooper# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*57718be8SEnji Cooper# POSSIBILITY OF SUCH DAMAGE.
29*57718be8SEnji Cooper#
30*57718be8SEnji Cooperfile="/tmp/d_sysctl.out"
31*57718be8SEnji Cooper
32*57718be8SEnji Cooperclean() {
33*57718be8SEnji Cooper
34*57718be8SEnji Cooper	if [ -f $file ]; then
35*57718be8SEnji Cooper		rm $file
36*57718be8SEnji Cooper	fi
37*57718be8SEnji Cooper}
38*57718be8SEnji Cooper
39*57718be8SEnji Coopersysctl_write() {
40*57718be8SEnji Cooper
41*57718be8SEnji Cooper	deadbeef="3735928559"
42*57718be8SEnji Cooper
43*57718be8SEnji Cooper	sysctl $1 | cut -d= -f1 > $file
44*57718be8SEnji Cooper
45*57718be8SEnji Cooper	if [ ! -f $file ]; then
46*57718be8SEnji Cooper		atf_fail "sysctl failed"
47*57718be8SEnji Cooper	fi
48*57718be8SEnji Cooper
49*57718be8SEnji Cooper	while read line; do
50*57718be8SEnji Cooper
51*57718be8SEnji Cooper		node=$(echo $line)
52*57718be8SEnji Cooper
53*57718be8SEnji Cooper		case $node in
54*57718be8SEnji Cooper
55*57718be8SEnji Cooper		"$1."*)
56*57718be8SEnji Cooper			atf_check -s not-exit:0 -e ignore \
57*57718be8SEnji Cooper				-x sysctl -w $node=$deadbeef
58*57718be8SEnji Cooper			;;
59*57718be8SEnji Cooper		esac
60*57718be8SEnji Cooper
61*57718be8SEnji Cooper	done < $file
62*57718be8SEnji Cooper
63*57718be8SEnji Cooper	# A functional verification that $deadbeef
64*57718be8SEnji Cooper	# was not actually written to the node.
65*57718be8SEnji Cooper	#
66*57718be8SEnji Cooper	if [ ! -z $(sysctl $1 | grep $deadbeef) ]; then
67*57718be8SEnji Cooper		atf_fail "value was written"
68*57718be8SEnji Cooper	fi
69*57718be8SEnji Cooper}
70*57718be8SEnji Cooper
71*57718be8SEnji Cooper# ddb.
72*57718be8SEnji Cooper#
73*57718be8SEnji Cooperatf_test_case sysctl_ddb cleanup
74*57718be8SEnji Coopersysctl_ddb_head() {
75*57718be8SEnji Cooper	atf_set "require.user" "unprivileged"
76*57718be8SEnji Cooper	atf_set "descr" "Test writing to 'ddb' sysctl node as an user"
77*57718be8SEnji Cooper}
78*57718be8SEnji Cooper
79*57718be8SEnji Coopersysctl_ddb_body() {
80*57718be8SEnji Cooper	sysctl_write "ddb"
81*57718be8SEnji Cooper}
82*57718be8SEnji Cooper
83*57718be8SEnji Coopersysctl_ddb_cleanup() {
84*57718be8SEnji Cooper	clean
85*57718be8SEnji Cooper}
86*57718be8SEnji Cooper
87*57718be8SEnji Cooper# hw.
88*57718be8SEnji Cooper#
89*57718be8SEnji Cooperatf_test_case sysctl_hw cleanup
90*57718be8SEnji Coopersysctl_hw_head() {
91*57718be8SEnji Cooper	atf_set "require.user" "unprivileged"
92*57718be8SEnji Cooper	atf_set "descr" "Test writing to 'hw' sysctl node as an user"
93*57718be8SEnji Cooper}
94*57718be8SEnji Cooper
95*57718be8SEnji Coopersysctl_hw_body() {
96*57718be8SEnji Cooper	sysctl_write "hw"
97*57718be8SEnji Cooper}
98*57718be8SEnji Cooper
99*57718be8SEnji Coopersysctl_hw_cleanup() {
100*57718be8SEnji Cooper	clean
101*57718be8SEnji Cooper}
102*57718be8SEnji Cooper
103*57718be8SEnji Cooper# kern.
104*57718be8SEnji Cooper#
105*57718be8SEnji Cooperatf_test_case sysctl_kern cleanup
106*57718be8SEnji Coopersysctl_kern_head() {
107*57718be8SEnji Cooper	atf_set "require.user" "unprivileged"
108*57718be8SEnji Cooper	atf_set "descr" "Test writing to 'kern' " \
109*57718be8SEnji Cooper		"sysctl node as an user (PR kern/44946)"
110*57718be8SEnji Cooper}
111*57718be8SEnji Cooper
112*57718be8SEnji Coopersysctl_kern_body() {
113*57718be8SEnji Cooper	sysctl_write "kern"
114*57718be8SEnji Cooper}
115*57718be8SEnji Cooper
116*57718be8SEnji Coopersysctl_kern_cleanup() {
117*57718be8SEnji Cooper	clean
118*57718be8SEnji Cooper}
119*57718be8SEnji Cooper
120*57718be8SEnji Cooper# machdep.
121*57718be8SEnji Cooper#
122*57718be8SEnji Cooperatf_test_case sysctl_machdep cleanup
123*57718be8SEnji Coopersysctl_machdep_head() {
124*57718be8SEnji Cooper	atf_set "require.user" "unprivileged"
125*57718be8SEnji Cooper	atf_set "descr" "Test writing to 'machdep' sysctl node as an user"
126*57718be8SEnji Cooper}
127*57718be8SEnji Cooper
128*57718be8SEnji Coopersysctl_machdep_body() {
129*57718be8SEnji Cooper	sysctl_write "machdep"
130*57718be8SEnji Cooper}
131*57718be8SEnji Cooper
132*57718be8SEnji Coopersysctl_machdep_cleanup() {
133*57718be8SEnji Cooper	clean
134*57718be8SEnji Cooper}
135*57718be8SEnji Cooper
136*57718be8SEnji Cooper# net.
137*57718be8SEnji Cooper#
138*57718be8SEnji Cooperatf_test_case sysctl_net cleanup
139*57718be8SEnji Coopersysctl_net_head() {
140*57718be8SEnji Cooper	atf_set "require.user" "unprivileged"
141*57718be8SEnji Cooper	atf_set "descr" "Test writing to 'net' sysctl node as an user"
142*57718be8SEnji Cooper}
143*57718be8SEnji Cooper
144*57718be8SEnji Coopersysctl_net_body() {
145*57718be8SEnji Cooper	sysctl_write "net"
146*57718be8SEnji Cooper}
147*57718be8SEnji Cooper
148*57718be8SEnji Coopersysctl_net_cleanup() {
149*57718be8SEnji Cooper	clean
150*57718be8SEnji Cooper}
151*57718be8SEnji Cooper
152*57718be8SEnji Cooper# security.
153*57718be8SEnji Cooper#
154*57718be8SEnji Cooperatf_test_case sysctl_security cleanup
155*57718be8SEnji Coopersysctl_security_head() {
156*57718be8SEnji Cooper	atf_set "require.user" "unprivileged"
157*57718be8SEnji Cooper	atf_set "descr" "Test writing to 'security' sysctl node as an user"
158*57718be8SEnji Cooper}
159*57718be8SEnji Cooper
160*57718be8SEnji Coopersysctl_security_body() {
161*57718be8SEnji Cooper	sysctl_write "security"
162*57718be8SEnji Cooper}
163*57718be8SEnji Cooper
164*57718be8SEnji Coopersysctl_security_cleanup() {
165*57718be8SEnji Cooper	clean
166*57718be8SEnji Cooper}
167*57718be8SEnji Cooper
168*57718be8SEnji Cooper# vfs.
169*57718be8SEnji Cooper#
170*57718be8SEnji Cooperatf_test_case sysctl_vfs cleanup
171*57718be8SEnji Coopersysctl_vfs_head() {
172*57718be8SEnji Cooper	atf_set "require.user" "unprivileged"
173*57718be8SEnji Cooper	atf_set "descr" "Test writing to 'vfs' sysctl node as an user"
174*57718be8SEnji Cooper}
175*57718be8SEnji Cooper
176*57718be8SEnji Coopersysctl_vfs_body() {
177*57718be8SEnji Cooper	sysctl_write "vfs"
178*57718be8SEnji Cooper}
179*57718be8SEnji Cooper
180*57718be8SEnji Coopersysctl_vfs_cleanup() {
181*57718be8SEnji Cooper	clean
182*57718be8SEnji Cooper}
183*57718be8SEnji Cooper
184*57718be8SEnji Cooper# vm.
185*57718be8SEnji Cooper#
186*57718be8SEnji Cooperatf_test_case sysctl_vm cleanup
187*57718be8SEnji Coopersysctl_vm_head() {
188*57718be8SEnji Cooper	atf_set "require.user" "unprivileged"
189*57718be8SEnji Cooper	atf_set "descr" "Test writing to 'vm' sysctl node as an user"
190*57718be8SEnji Cooper}
191*57718be8SEnji Cooper
192*57718be8SEnji Coopersysctl_vm_body() {
193*57718be8SEnji Cooper	sysctl_write "vm"
194*57718be8SEnji Cooper}
195*57718be8SEnji Cooper
196*57718be8SEnji Coopersysctl_vm_cleanup() {
197*57718be8SEnji Cooper	clean
198*57718be8SEnji Cooper}
199*57718be8SEnji Cooper
200*57718be8SEnji Cooperatf_init_test_cases() {
201*57718be8SEnji Cooper	atf_add_test_case sysctl_ddb
202*57718be8SEnji Cooper	atf_add_test_case sysctl_hw
203*57718be8SEnji Cooper	atf_add_test_case sysctl_kern
204*57718be8SEnji Cooper	atf_add_test_case sysctl_machdep
205*57718be8SEnji Cooper	atf_add_test_case sysctl_net
206*57718be8SEnji Cooper	atf_add_test_case sysctl_security
207*57718be8SEnji Cooper	atf_add_test_case sysctl_vfs
208*57718be8SEnji Cooper	atf_add_test_case sysctl_vm
209*57718be8SEnji Cooper}
210