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