xref: /freebsd/usr.sbin/bsdconfig/security/kern_securelevel (revision fd962ac6997aba7e64007113ed1354e0a88b6e38)
1#!/bin/sh
2#-
3# Copyright (c) 2012-2013 Devin Teske
4# All Rights Reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD$
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." "$0"
34f_include $BSDCFG_SHARE/dialog.subr
35f_include $BSDCFG_SHARE/mustberoot.subr
36f_include $BSDCFG_SHARE/sysrc.subr
37
38BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="130.security"
39f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
40
41SECURELEVEL_HELPFILE=$BSDCFG_LIBE/$APP_DIR/include/securelevel.hlp
42
43ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
44[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
45
46############################################################ FUNCTIONS
47
48# dialog_menu_main
49#
50# Display the dialog(1)-based application main menu.
51#
52dialog_menu_main()
53{
54	local menu_list defaultitem=
55	local hline="$hline_select_securelevel_to_operate_at"
56	local prompt="$msg_securelevels_menu_text"
57
58	menu_list="
59		'$msg_disabled'       '$msg_disable_securelevels'
60		'$msg_secure'         '$msg_secure_mode'
61		'$msg_highly_secure'  '$msg_highly_secure_mode'
62		'$msg_network_secure' '$msg_network_secure_mode'
63	" # END-QUOTE
64
65	local height width rows
66	eval f_dialog_menu_size height width rows \
67	                        \"\$DIALOG_TITLE\"     \
68	                        \"\$DIALOG_BACKTITLE\" \
69	                        \"\$prompt\"           \
70	                        \"\$hline\"            \
71	                        $menu_list
72
73	case "$( f_sysrc_get kern_securelevel_enable )" in
74	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
75		case "$( f_sysrc_get kern_securelevel )" in
76		1) defaultitem="$msg_secure"         ;;
77		2) defaultitem="$msg_highly_secure"  ;;
78		3) defaultitem="$msg_network_secure" ;;
79		esac ;;
80	*)
81		defaultitem="$msg_disabled"
82	esac
83
84	local menu_choice
85	menu_choice=$( eval $DIALOG \
86		--title \"\$DIALOG_TITLE\"         \
87		--backtitle \"\$DIALOG_BACKTITLE\" \
88		--hline \"\$hline\"                \
89		--ok-label \"\$msg_ok\"            \
90		--cancel-label \"\$msg_cancel\"    \
91		--help-button                      \
92		--help-label \"\$msg_help\"        \
93		${USE_XDIALOG:+--help \"\"}        \
94		--default-item \"\$defaultitem\"   \
95		--menu \"\$prompt\"                \
96		$height $width $rows               \
97		$menu_list                         \
98		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
99	)
100	local retval=$?
101	f_dialog_menutag_store -s "$menu_choice"
102	return $retval
103}
104
105############################################################ MAIN
106
107# Incorporate rc-file if it exists
108[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
109
110#
111# Process command-line arguments
112#
113while getopts h$GETOPTS_STDARGS flag; do
114	case "$flag" in
115	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
116	esac
117done
118shift $(( $OPTIND - 1 ))
119
120#
121# Initialize
122#
123f_dialog_title "$msg_securelevels_menu_title"
124f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
125f_mustberoot_init
126
127#
128# Launch application main menu
129#
130while :; do
131	dialog_menu_main
132	retval=$?
133	f_dialog_menutag_fetch mtag
134
135	if [ $retval -eq 2 ]; then
136		# The Help button was pressed
137		f_show_help "$SECURELEVEL_HELPFILE"
138		continue
139	elif [ $retval -ne 0 ]; then
140		f_die
141	fi
142
143	break
144done
145
146case "$mtag" in
147"$msg_disabled")
148	f_sysrc_set kern_securelevel_enable "NO"
149	;;
150"$msg_secure")
151	f_sysrc_set kern_securelevel_enable "YES"
152	f_sysrc_set kern_securelevel "1"
153	;;
154"$msg_highly_secure")
155	f_sysrc_set kern_securelevel_enable "YES"
156	f_sysrc_set kern_securelevel "2"
157	;;
158"$msg_network_secure")
159	f_sysrc_set kern_securelevel_enable "YES"
160	f_sysrc_set kern_securelevel "3"
161	;;
162esac
163
164exit $SUCCESS
165
166################################################################################
167# END
168################################################################################
169