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
41ipgm=$( f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" )
42[ $? -eq $SUCCESS -a "$ipgm" ] && pgm="$ipgm"
43
44############################################################ FUNCTIONS
45
46# dialog_menu_main
47#
48# Display the dialog(1)-based application main menu.
49#
50dialog_menu_main()
51{
52	local menu_list size mark=" " defaultitem=
53	local hline="$hline_arrows_tab_enter"
54	local prompt="$msg_menu_text"
55
56	# Obtain default-item (adjusted below for dynamic tags)
57	f_getvar DEFAULTITEM_$$ defaultitem
58	local ditem="${defaultitem%%[$IFS]*}"
59
60	menu_list="
61		'X <<< $msg_exit' '$msg_exit_this_menu'
62	" # END-QUOTE
63
64	#
65	# Add dynamically tagged entry for kern_securelevels
66	#
67	case "$( f_sysrc_get kern_securelevel_enable )" in
68	[Yy][Ee][Ss])
69		local kern_securelevel="$( f_sysrc_get kern_securelevel )"
70		if [ ${#kern_securelevel} -eq 1 ] &&
71		   f_isinteger "$kern_securelevel" &&
72		   [ $kern_securelevel -lt 9 ]
73		then
74			mark="$kern_securelevel"
75		else
76			mark="X"
77		fi ;;
78	*)
79		mark=" "
80	esac
81	menu_list="$menu_list
82		'2 [$mark] $msg_securelevel' '$msg_securelevel_desc'"
83
84	# Update default-item if appropriate
85	[ "$ditem" = 2 ] && defaultitem="2 [$mark] $msg_securelevel"
86
87	#
88	# Add dynamically tagged entry for nfs_reserved_port_only
89	#
90	case "$( f_sysrc_get nfs_reserved_port_only )" in
91	[Yy][Ee][Ss]) mark="X" ;;
92	           *) mark=" " ;;
93	esac
94	menu_list="$menu_list
95		'3 [$mark] $msg_nfs_port' '$msg_nfs_port_desc'"
96
97	# Update default-item if appropriate
98	[ "$ditem" = 3 ] && defaultitem="3 [$mark] $msg_nfs_port"
99
100	size=$( eval f_dialog_menu_size \
101	        	\"\$DIALOG_TITLE\"     \
102	        	\"\$DIALOG_BACKTITLE\" \
103	                \"\$prompt\"           \
104	        	\"\$hline\"            \
105	        	$menu_list             )
106
107	local dialog_menu
108	dialog_menu=$( 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\" $size          \
116		$menu_list                         \
117		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
118	)
119	local retval=$?
120	setvar DIALOG_MENU_$$ "$dialog_menu"
121
122	# Only update default-item on success
123	[ $retval -eq 0 ] && setvar DEFAULTITEM_$$ "$dialog_menu"
124
125	return $retval
126}
127
128############################################################ MAIN
129
130# Incorporate rc-file if it exists
131[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
132
133#
134# Process command-line arguments
135#
136while getopts h$GETOPTS_STDARGS flag; do
137	case "$flag" in
138	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm";;
139	esac
140done
141shift $(( $OPTIND - 1 ))
142
143#
144# Initialize
145#
146f_dialog_title "$msg_system_security_options_menu"
147f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
148f_mustberoot_init
149
150#
151# Launch application main menu
152#
153while :; do
154	dialog_menu_main
155	retval=$?
156	mtag=$( f_dialog_menutag )
157
158	[ $retval -eq 0 ] || f_die
159
160	case "$mtag" in
161	"X <<< $msg_exit") break ;;
162	"2 ["?"] $msg_securelevel") # Configure securelevels for the system
163		$BSDCFG_LIBE/$APP_DIR/kern_securelevel ${USE_XDIALOG:+-X} ;;
164	"3 [X] $msg_nfs_port") # Require that NFS clients use reserved ports
165		f_sysrc_set nfs_reserved_port_only "NO";;
166	"3 [ ] $msg_nfs_port") # Same; Toggle value
167		f_sysrc_set nfs_reserved_port_only "YES";;
168	esac
169
170done
171
172exit $SUCCESS
173
174################################################################################
175# END
176################################################################################
177