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