xref: /freebsd/usr.sbin/bsdconfig/security/security (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
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 (INCLUDING, 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#
28############################################################ INCLUDES
29
30BSDCFG_SHARE="/usr/share/bsdconfig"
31. $BSDCFG_SHARE/common.subr || exit 1
32f_dprintf "%s: loading includes..." "$0"
33f_include $BSDCFG_SHARE/dialog.subr
34f_include $BSDCFG_SHARE/mustberoot.subr
35f_include $BSDCFG_SHARE/sysrc.subr
36
37BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="130.security"
38f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
39
40f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" ipgm &&
41	pgm="${ipgm:-$pgm}"
42
43############################################################ FUNCTIONS
44
45# dialog_menu_main
46#
47# Display the dialog(1)-based application main menu.
48#
49dialog_menu_main()
50{
51	local prompt="$msg_menu_text"
52	local menu_list="
53		'X $msg_exit' '$msg_exit_this_menu'
54	" # END-QUOTE
55	local defaultitem= # Calculated below
56	local hline="$hline_arrows_tab_enter"
57
58	# Obtain default-item (adjusted below for dynamic tags)
59	f_dialog_default_fetch defaultitem
60	local ditem="${defaultitem%%[$IFS]*}"
61
62	#
63	# Add dynamically tagged entry for kern_securelevels
64	#
65	local mark=" "
66	case "$( f_sysrc_get kern_securelevel_enable )" in
67	[Yy][Ee][Ss])
68		local kern_securelevel="$( f_sysrc_get kern_securelevel )"
69		if [ ${#kern_securelevel} -eq 1 ] &&
70		   f_isinteger "$kern_securelevel" &&
71		   [ $kern_securelevel -lt 9 ]
72		then
73			mark="$kern_securelevel"
74		else
75			mark="X"
76		fi ;;
77	*)
78		mark=" "
79	esac
80	menu_list="$menu_list
81		'2 [$mark] $msg_securelevel' '$msg_securelevel_desc'"
82
83	# Update default-item if appropriate
84	[ "$ditem" = 2 ] && defaultitem="2 [$mark] $msg_securelevel"
85
86	#
87	# Add dynamically tagged entry for nfs_reserved_port_only
88	#
89	case "$( f_sysrc_get nfs_reserved_port_only )" in
90	[Yy][Ee][Ss]) mark="X" ;;
91	           *) mark=" " ;;
92	esac
93	menu_list="$menu_list
94		'3 [$mark] $msg_nfs_port' '$msg_nfs_port_desc'"
95
96	# Update default-item if appropriate
97	[ "$ditem" = 3 ] && defaultitem="3 [$mark] $msg_nfs_port"
98
99	local height width rows
100	eval f_dialog_menu_size height width rows \
101	                        \"\$DIALOG_TITLE\"     \
102	                        \"\$DIALOG_BACKTITLE\" \
103	                        \"\$prompt\"           \
104	                        \"\$hline\"            \
105	                        $menu_list
106
107	local menu_choice
108	menu_choice=$( eval $DIALOG \
109		--title \"\$DIALOG_TITLE\"         \
110		--backtitle \"\$DIALOG_BACKTITLE\" \
111		--hline \"\$hline\"                \
112		--ok-label \"\$msg_ok\"            \
113		--cancel-label \"\$msg_cancel\"    \
114		--default-item \"\$defaultitem\"   \
115		--menu \"\$prompt\"                \
116		$height $width $rows               \
117		$menu_list                         \
118		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
119	)
120	local retval=$?
121	f_dialog_data_sanitize menu_choice
122	f_dialog_menutag_store "$menu_choice"
123
124	# Only update default-item on success
125	[ $retval -eq $DIALOG_OK ] && f_dialog_default_store "$menu_choice"
126
127	return $retval
128}
129
130############################################################ MAIN
131
132# Incorporate rc-file if it exists
133[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
134
135#
136# Process command-line arguments
137#
138while getopts h$GETOPTS_STDARGS flag; do
139	case "$flag" in
140	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;;
141	esac
142done
143shift $(( $OPTIND - 1 ))
144
145#
146# Initialize
147#
148f_dialog_title "$msg_system_security_options_menu"
149f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
150f_mustberoot_init
151
152#
153# Launch application main menu (loop for menu update after selection)
154#
155while :; do
156	dialog_menu_main || f_die
157	f_dialog_menutag_fetch mtag
158
159	case "$mtag" in
160	"X $msg_exit") break ;;
161	"2 ["?"] $msg_securelevel") # Configure securelevels for the system
162		$BSDCFG_LIBE/$APP_DIR/kern_securelevel ${USE_XDIALOG:+-X} ;;
163	"3 [X] $msg_nfs_port") # Require that NFS clients use reserved ports
164		f_eval_catch "$0" f_sysrc_set \
165			'f_sysrc_set nfs_reserved_port_only NO' ;;
166	"3 [ ] $msg_nfs_port") # Same; Toggle value
167		f_eval_catch "$0" f_sysrc_set \
168			'f_sysrc_set nfs_reserved_port_only YES' ;;
169	*)
170		f_die 1 "$msg_unknown_security_menu_selection"
171	esac
172done
173
174exit $SUCCESS
175
176################################################################################
177# END
178################################################################################
179