xref: /freebsd/usr.sbin/bsdconfig/startup/misc (revision 3c5ba95ad12285ad37c182a4bfc1b240ec6d18a7)
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# $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="140.startup"
39f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
40
41f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" ipgm &&
42	pgm="${ipgm:-$pgm}"
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 prompt="$msg_miscellaneous_menu_text"
53	local menu_list="
54		'X $msg_exit' '$msg_exit_this_menu'
55	" # END-QUOTE
56	local defaultitem= # Calculated below
57	local hline="$hline_arrows_tab_enter"
58
59	# List of variables we'll need from rc.conf(5)
60	local var_list="
61		accounting_enable
62		local_startup
63		lpd_enable
64		named_enable
65		named_flags
66		nis_client_enable
67		nis_server_enable
68		nisdomainname
69		startup_dirs
70	" # END-QUOTE
71
72	# Add i386-specific variables if appropriate
73	if [ "$UNAME_P" = "i386" ]; then
74		var_list="$var_list
75			apm_enable
76			ibcs2_enable
77		" # END-QUOTE
78	fi
79
80	# Obtain default-item (adjusted below for dynamic tags)
81	f_dialog_default_fetch defaultitem
82	local ditem="${defaultitem%%[$IFS]*}"
83
84	eval "$(
85		. "$RC_DEFAULTS"
86		source_rc_confs
87		export $var_list
88		export msg_apm            msg_apm_desc
89		export msg_startup_dirs   msg_startup_dirs_desc
90		export msg_named          msg_named_desc
91		export msg_named_flags    msg_named_flags_desc
92		export msg_nis_client     msg_nis_client_desc
93		export msg_nis_domainname msg_nis_domainname_desc
94		export msg_nis_server     msg_nis_server_desc
95		export msg_accounting     msg_accounting_desc
96		export msg_lpd            msg_lpd_desc
97		export msg_sco            msg_sco_desc
98		:| awk \
99			-v uname_p="$UNAME_P"            \
100			-v menu_tags="$DIALOG_MENU_TAGS" \
101			-v menu_fmt="'%s' '%s'\n"        \
102			-v mtag_fmt="%c [%c] %s"         \
103			-v separator="' ' ' -- '\n"      \
104			-v ditem="$ditem"                \
105		'
106		function mprint(tag,item)
107		{
108			printf menu_fmt, tag, item
109		}
110		END {
111			i = 1
112			defaultitem = ""
113
114			printf "menu_list=\"$menu_list\n"
115
116			if ( uname_p == "i386" )
117			{
118				#
119				# APM: Auto-power management services
120				#      (typically laptops)
121				#
122				char = substr(menu_tags, i++, 1)
123				mark = ( ENVIRON["apm_enable"] ~ \
124						/^[Yy][Ee][Ss]$/ ? "X" : " " )
125				subtag = ENVIRON["msg_apm"]
126				tag = sprintf(mtag_fmt, char, mark, subtag)
127				mprint(tag, ENVIRON["msg_apm_desc"])
128				if (ditem == char) defaultitem = tag
129			}
130
131			printf separator
132
133			#
134			# Startup dirs: Set the list of dirs to look for
135			#               startup scripts
136			#
137			char = substr(menu_tags, i++, 1)
138			mark = ( length(ENVIRON["local_startup"]) > 0 \
139					? "X" : " " )
140			subtag = ENVIRON["msg_startup_dirs"]
141			tag = sprintf(mtag_fmt, char, mark, subtag)
142			mprint(tag, ENVIRON["msg_startup_dirs_desc"])
143			if (ditem == char) defaultitem = tag
144
145			#
146			# named: Run a local name server on this host
147			#
148			char = substr(menu_tags, i++, 1)
149			mark = ( ENVIRON["named_enable"] ~ \
150					/^[Yy][Ee][Ss]$/ ? "X" : " " )
151			subtag = ENVIRON["msg_named"]
152			tag = sprintf(mtag_fmt, char, mark, subtag)
153			mprint(tag, ENVIRON["msg_named_desc"])
154			if (ditem == char) defaultitem = tag
155
156			#
157			# named flags: Set default flags to named (if enabled)
158			#
159			char = substr(menu_tags, i++, 1)
160			mark = ( length(ENVIRON["named_flags"]) > 0 \
161					? "X" : " " )
162			subtag = ENVIRON["msg_named_flags"]
163			tag = sprintf(mtag_fmt, char, mark, subtag)
164			mprint(tag, ENVIRON["msg_named_flags_desc"])
165			if (ditem == char) defaultitem = tag
166
167			#
168			# NIS client: This host wishes to be an NIS client.
169			#
170			char = substr(menu_tags, i++, 1)
171			mark = ( ENVIRON["nis_client_enable"] ~ \
172					/^[Yy][Ee][Ss]$/ ? "X" : " " )
173			subtag = ENVIRON["msg_nis_client"]
174			tag = sprintf(mtag_fmt, char, mark, subtag)
175			mprint(tag, ENVIRON["msg_nis_client_desc"])
176			if (ditem == char) defaultitem = tag
177
178			#
179			# NIS domainname: Set NIS domainname (if enabled)
180			#
181			char = substr(menu_tags, i++, 1)
182			mark = ( length(ENVIRON["nisdomainname"]) > 0 && \
183					ENVIRON["nisdomainname"] != "NO" \
184					? "X" : " " )
185			subtag = ENVIRON["msg_nis_domainname"]
186			tag = sprintf(mtag_fmt, char, mark, subtag)
187			mprint(tag, ENVIRON["msg_nis_domainname_desc"])
188			if (ditem == char) defaultitem = tag
189
190			#
191			# NIS server: This host wishes to be an NIS server.
192			#
193			char = substr(menu_tags, i++, 1)
194			mark = ( ENVIRON["nis_server_enable"] ~ \
195					/^[Yy][Ee][Ss]$/ ? "X" : " " )
196			subtag = ENVIRON["msg_nis_server"]
197			tag = sprintf(mtag_fmt, char, mark, subtag)
198			mprint(tag, ENVIRON["msg_nis_server_desc"])
199			if (ditem == char) defaultitem = tag
200
201			printf separator
202
203			#
204			# Accounting: This host wishes to run process
205			#             accounting.
206			#
207			char = substr(menu_tags, i++, 1)
208			mark = ( ENVIRON["accounting_enable"] ~ \
209					/^[Yy][Ee][Ss]$/ ? "X" : " " )
210			subtag = ENVIRON["msg_accounting"]
211			tag = sprintf(mtag_fmt, char, mark, subtag)
212			mprint(tag, ENVIRON["msg_accounting_desc"])
213			if (ditem == char) defaultitem = tag
214
215			#
216			# lpd: This host has a printer and wants to run lpd.
217			#
218			char = substr(menu_tags, i++, 1)
219			mark = ( ENVIRON["lpd_enable"] ~ \
220					/^[Yy][Ee][Ss]$/ ? "X" : " " )
221			subtag = ENVIRON["msg_lpd"]
222			tag = sprintf(mtag_fmt, char, mark, subtag)
223			mprint(tag, ENVIRON["msg_lpd_desc"])
224			if (ditem == char) defaultitem = tag
225
226			if ( uname_p == "i386" )
227			{
228				#
229				# SCO: This host wants to be able to run IBCS2
230				#      binaries.
231				#
232				char = substr(menu_tags, i++, 1)
233				mark = ( ENVIRON["ibcs2_enable"] ~ \
234						/^[Yy][Ee][Ss]$/ ? "X" : " " )
235				subtag = ENVIRON["msg_sco"]
236				tag = sprintf(mtag_fmt, char, mark, subtag)
237				mprint(tag, ENVIRON["msg_sco_desc"])
238				if (ditem == char) defaultitem = tag
239			}
240
241			printf "\"\n"
242
243			if ( defaultitem )
244				printf "defaultitem=\"%s\"\n", defaultitem
245		}'
246	)"
247
248	local height width rows
249	eval f_dialog_menu_size height width rows \
250	                        \"\$DIALOG_TITLE\"     \
251	                        \"\$DIALOG_BACKTITLE\" \
252	                        \"\$prompt\"           \
253	                        \"\$hline\"            \
254	                        $menu_list
255
256	local menu_choice
257	menu_choice=$( eval $DIALOG \
258		--title \"\$DIALOG_TITLE\"         \
259		--backtitle \"\$DIALOG_BACKTITLE\" \
260		--hline \"\$hline\"                \
261		--ok-label \"\$msg_ok\"            \
262		--cancel-label \"\$msg_cancel\"    \
263		--default-item \"\$defaultitem\"   \
264		--menu \"\$prompt\"                \
265		$height $width $rows               \
266		$menu_list                         \
267		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
268	)
269	local retval=$?
270	f_dialog_data_sanitize menu_choice
271	f_dialog_menutag_store "$menu_choice"
272	f_dialog_default_store "$menu_choice"
273	return $retval
274}
275
276# dialog_input_value [ $prompt [ $init ] ]
277#
278# Prompt the user to input a value. If the user does not cancel or press ESC,
279# the return value is zero ($SUCCESS) and $value holds the user's input.
280#
281dialog_input_value()
282{
283	local prompt="$1" _input="$2"
284
285	f_dialog_title "$msg_value_required"
286	f_dialog_input _input "$prompt" "$_input" "$hline_alnum_tab_enter"
287	local retval=$?
288	f_dialog_title_restore
289
290	# Return if user has either pressed ESC or chosen Cancel/No
291	[ $retval -eq $DIALOG_OK ] || return $retval
292
293	value="$_input"
294	return $DIALOG_OK
295}
296
297############################################################ MAIN
298
299# Incorporate rc-file if it exists
300[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
301
302#
303# Process command-line arguments
304#
305while getopts h$GETOPTS_STDARGS flag; do
306	case "$flag" in
307	h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;;
308	esac
309done
310shift $(( $OPTIND - 1 ))
311
312#
313# Initialize
314#
315f_dialog_title "$msg_miscellaneous_startup_services"
316f_dialog_backtitle "${ipgm:+bsdconfig }$pgm"
317f_mustberoot_init
318
319#
320# Launch application main menu
321#
322while :; do
323	dialog_menu_main || f_die
324	f_dialog_menutag_fetch mtag
325
326	case "$mtag" in
327	"X $msg_exit") break ;;
328	?" [X] "*) toggled=1 reverse=NO ;;
329	        *) toggled= reverse=YES
330	esac
331
332	case "$mtag" in
333	# Simple On/Off toggle bits
334	?" ["?"] $msg_apm")
335		f_eval_catch -dk err "$0" f_sysrc_set \
336			'f_sysrc_set apm_enable "%s"' "$reverse" ;;
337	?" ["?"] $msg_named")
338		f_eval_catch -dk err "$0" f_sysrc_set \
339			'f_sysrc_set named_enable "%s"' "$reverse" ;;
340	?" ["?"] $msg_accounting")
341		f_eval_catch -dk err "$0" f_sysrc_set \
342			'f_sysrc_set accounting_enable "%s"' "$reverse" ;;
343	?" ["?"] $msg_lpd")
344		f_eval_catch -dk err "$0" f_sysrc_set \
345			'f_sysrc_set lpd_enable "%s"' "$reverse" ;;
346	?" ["?"] $msg_sco")
347		f_eval_catch -dk err "$0" f_sysrc_set \
348			'f_sysrc_set ibcs2_enable "%s"' "$reverse" ;;
349	# Multi-variable On/Off toggle bits
350	?" ["?"] $msg_nis_client")
351		if f_eval_catch -dk err "$0" f_sysrc_set \
352			'f_sysrc_set nis_client_enable "%s"' "$reverse"
353		then
354			[ "$reverse" = "NO" ] || f_eval_catch -dk err "$0" \
355				f_sysrc_set 'f_sysrc_set rpcbind_enable YES'
356		fi ;;
357	?" ["?"] $msg_nis_server")
358		if f_eval_catch -dk err "$0" f_sysrc_set \
359			'f_sysrc_set nis_server_enable "%s"' "$reverse"
360		then
361			[ "$reverse" = "NO" ] || f_eval_catch -dk err "$0" \
362				f_sysrc_set 'f_sysrc_set rpcbind_enable YES'
363		fi ;;
364	# Checkboxes for non-boolean options
365	?" ["?"] $msg_nis_domainname")
366		dialog_input_value "$msg_nis_domainname_desc" \
367		                   "$( f_sysrc_get nisdomainname )" || continue
368		f_eval_catch -dk err "$0" f_sysrc_set \
369			'f_sysrc_set nisdomainname "%s"' "$value" ;;
370	?" ["?"] $msg_startup_dirs")
371		dialog_input_value "$msg_startup_dirs_desc" \
372		                   "$( f_sysrc_get local_startup )" || continue
373		f_eval_catch -dk err "$0" f_sysrc_set \
374			'f_sysrc_set local_startup "%s"' "$value" ;;
375	?" ["?"] $msg_named_flags")
376		dialog_input_value "$msg_named_flags_desc" \
377		                   "$( f_sysrc_get named_flags )" || continue
378		f_eval_catch -dk err "$0" f_sysrc_set \
379			'f_sysrc_set named_flags "%s"' "$value" ;;
380	esac
381
382	[ $? -eq $DIALOG_OK ] || f_dialog_msgbox "$err\n"
383done
384
385exit $SUCCESS
386
387################################################################################
388# END
389################################################################################
390