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