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