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