summaryrefslogtreecommitdiffstats
path: root/usr.sbin/bsdconfig/startup/share/rcconf.subr
blob: 4c3e0d6760034f07c685c2777f617a1d7aaae802 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
if [ ! "$_STARTUP_RCCONF_SUBR" ]; then _STARTUP_RCCONF_SUBR=1
#
# Copyright (c) 2006-2013 Devin Teske
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$
#
############################################################ INCLUDES

BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
f_dprintf "%s: loading includes..." startup/rcconf.subr
f_include $BSDCFG_SHARE/sysrc.subr

BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="140.startup"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr

############################################################ GLOBALS

#
# Initialize in-memory cache variables
#
STARTUP_RCCONF_MAP=
_STARTUP_RCCONF_MAP=

#
# Define what a variable looks like
#
STARTUP_RCCONF_REGEX="^[[:alpha:]_][[:alnum:]_]*="

#
# Default path to on-disk cache file(s)
#
STARTUP_RCCONF_MAP_CACHEFILE="/var/run/bsdconfig/startup_rcconf_map.cache"

############################################################ FUNCTIONS

# f_startup_rcconf_list
#
# Produce a list of non-default configuration variables configured in the
# rc.conf(5) collection of files.
#
f_startup_rcconf_list()
{
	( # Operate within a sub-shell to protect the parent environment
		. "$RC_DEFAULTS" > /dev/null
		f_clean_env --except PATH STARTUP_RCCONF_REGEX rc_conf_files
		source_rc_confs > /dev/null
		export _rc_conf_files_file="$( f_sysrc_find rc_conf_files )"
		export RC_DEFAULTS
		set | awk -F= "
		function test_print(var)
		{
			if ( var == \"OPTIND\" ) return
			if ( var == \"PATH\" ) return
			if ( var == \"RC_DEFAULTS\" ) return
			if ( var == \"STARTUP_RCCONF_REGEX\" ) return
			if ( var == \"_rc_conf_files_file\" ) return
			if ( var == \"rc_conf_files\" )
			{
				if ( ENVIRON[\"_rc_conf_files_file\"] == \
				     ENVIRON[\"RC_DEFAULTS\"] ) return
			}
			print var
		}
		/$STARTUP_RCCONF_REGEX/ { test_print(\$1) }"
	)
}

# f_startup_rcconf_map [$var_to_set]
#
# Produce a map (beit from in-memory cache or on-disk cache) of rc.conf(5)
# variables and their descriptions. The map returned has the following format:
#
# 	var description
#
# With each as follows:
#
# 	var           the rc.conf(5) variable
# 	description   description of the variable
#
# If $var_to_set is missing or NULL, the map is printed to standard output for
# capturing in a sub-shell (which is less-recommended because of performance
# degredation; for example, when called in a loop).
#
f_startup_rcconf_map()
{
	local __var_to_set="$1"

	# If the in-memory cached value is available, return it immediately
	if [ "$_STARTUP_RCCONF_MAP" ]; then
		if [ "$__var_to_set" ]; then
			setvar "$__var_to_set" "$STARTUP_RCCONF_MAP"
		else
			echo "$STARTUP_RCCONF_MAP"
		fi
		return $SUCCESS
	fi

	#
	# create the in-memory cache (potentially from validated on-disk cache)
	#

	#
	# Calculate digest used to determine if the on-disk global persistant
	# cache file (containing this digest on the first line) is valid and
	# can be used to quickly populate the cache value for immediate return.
	#
	local __rc_defaults_digest
	__rc_defaults_digest=$( md5 < "$RC_DEFAULTS" )

	#
	# Check to see if the global persistant cache file exists
	#
	if [ -f "$STARTUP_RCCONF_MAP_CACHEFILE" ]; then
		#
		# Attempt to populate the in-memory cache with the (soon to be)
		# validated on-disk cache. If validation fails, fall-back to
		# the current value and provide error exit status.
		#
		STARTUP_RCCONF_MAP=$(
			(	# Get digest as the first word on the first line
				read digest rest_ignored

				#
				# If the stored digest matches the calculated-
				# one populate the in-memory cache from the on-
				# disk cache and provide success exit status.
				#
				if [ "$digest" = "$__rc_defaults_digest" ]
				then
					cat
					exit $SUCCESS
				else
					# Otherwise, return the current value
					echo "$STARTUP_RCCONF_MAP"
					exit $FAILURE
				fi
			) < "$STARTUP_RCCONF_MAP_CACHEFILE"
		)
		local __retval=$?
		export STARTUP_RCCONF_MAP # Make children faster (export cache)
		if [ $__retval -eq $SUCCESS ]; then
			export _STARTUP_RCCONF_MAP=1
			if [ "$__var_to_set" ]; then
				setvar "$__var_to_set" "$STARTUP_RCCONF_MAP"
			else
				echo "$STARTUP_RCCONF_MAP"
			fi
			return $SUCCESS
		fi
		# Otherwise, fall-thru to create in-memory cache from scratch
	fi

	#
	# If we reach this point, we need to generate the data from scratch
	# (and after we do, we'll attempt to create the global persistant
	# cache file to speed up future executions).
	#

	STARTUP_RCCONF_MAP=$(
		f_clean_env --except \
			PATH                 \
			RC_DEFAULTS          \
			STARTUP_RCCONF_REGEX \
			f_sysrc_desc_awk
		. "$RC_DEFAULTS"

		# Unset variables we don't want reported
		unset source_rc_confs_defined

		for var in $( set | awk -F= "
			function test_print(var)
			{
				if ( var == \"OPTIND\" ) return 
				if ( var == \"PATH\" ) return 
				if ( var == \"RC_DEFAULTS\" ) return 
				if ( var == \"STARTUP_RCCONF_REGEX\" ) return 
				if ( var == \"f_sysrc_desc_awk\" ) return
				print var
			}
			/$STARTUP_RCCONF_REGEX/ { test_print(\$1) }
		" ); do
			echo $var "$( f_sysrc_desc $var )"
		done
	)
	export STARTUP_RCCONF_MAP
	export _STARTUP_RCCONF_MAP=1
	if [ "$__var_to_set" ]; then
		setvar "$__var_to_set" "$STARTUP_RCCONF_MAP"
	else
		echo "$STARTUP_RCCONF_MAP"
	fi

	#
	# Attempt to create the persistant global cache
	#

	# Create a new temporary file to write to
	local __tmpfile="$( mktemp -t "$pgm" )"
	[ "$__tmpfile" ] || return $FAILURE

	# Write the temporary file contents
	echo "$__rc_defaults_digest" > "$__tmpfile"
	echo "$STARTUP_RCCONF_MAP" >> "$__tmpfile"

	# Finally, move the temporary file into place
	case "$STARTUP_RCCONF_MAP_CACHEFILE" in
	*/*) f_quietly mkdir -p "${STARTUP_RCCONF_MAP_CACHEFILE%/*}"
	esac
	mv "$__tmpfile" "$STARTUP_RCCONF_MAP_CACHEFILE"
}

# f_startup_rcconf_map_expand $var_to_get
#
# Expands the map ($var_to_get) into the shell environment namespace by
# creating _${var}_desc variables containing the description of each variable
# encountered.
#
# NOTE: Variables are exported for later-required awk(1) ENVIRON visibility.
#
f_startup_rcconf_map_expand()
{
	local var_to_get="$1"
	eval "$( f_getvar "$var_to_get" | awk '
	BEGIN {
		rword = "^[[:space:]]*[^[:space:]]*[[:space:]]*"
	}
	{
		var  = $1
		desc = $0
		sub(rword, "", desc)
		gsub(/'\''/, "'\''\\'\'\''", desc)
		printf "_%s_desc='\''%s'\''\n", var, desc
		printf "export _%s_desc\n", var
	}' )"
}

# f_dialog_input_view_details
#
# Display a menu for selecting which details are to be displayed. The following
# variables are tracked/modified by the menu/user's selection:
#
# 	SHOW_DESC		Show or hide descriptions
#
# Mutually exclusive options:
#
# 	SHOW_VALUE		Show the value (default; override only)
# 	SHOW_DEFAULT_VALUE	Show both value and default
# 	SHOW_CONFIGURED		Show rc.conf(5) file variable is configured in
#
# Each variable is treated as a boolean (NULL for false, non-NULL for true).
#
# Variables are exported for later-required awk(1) ENVIRON visibility. Returns
# success unless the user chose `Cancel' or pressed Escape.
#
f_dialog_input_view_details()
{
	local prompt=
	local menu_list # calculated below
	local defaultitem= # calculated below
	local hline="$hline_arrows_tab_enter"

	# Calculate marks for checkboxes and radio buttons
	local md=" "
	if [ "$SHOW_DESC" ]; then
		md="X"
	fi
	local m1=" " m2=" " m3=" "
	if [ "$SHOW_VALUE" ]; then
		m1="*"
		defaultitem="1 ($m1) $msg_show_value"
	elif [ "$SHOW_DEFAULT_VALUE" ]; then
		m2="*"
		defaultitem="2 ($m2) $msg_show_default_value"
	elif [ "$SHOW_CONFIGURED" ]; then
		m3="*"
		defaultitem="3 ($m3) $msg_show_configured"
	fi

	# Create the menu list with the above-calculated marks
	menu_list="
		'R $msg_reset'                    '$msg_reset_desc'
		'D [$md] $msg_desc'               '$msg_desc_desc'
		'1 ($m1) $msg_show_value'         '$msg_show_value_desc'
		'2 ($m2) $msg_show_default_value' '$msg_show_default_value_desc'
		'3 ($m3) $msg_show_configured'    '$msg_show_configured_desc'
	" # END-QUOTE

	local height width rows
	eval f_dialog_menu_size height width rows \
	                        \"\$DIALOG_TITLE\"     \
	                        \"\$DIALOG_BACKTITLE\" \
	                        \"\$prompt\"           \
	                        \"\$hline\"            \
	                        $menu_list

	f_dialog_title "$msg_choose_view_details"

	local mtag
	mtag=$( eval $DIALOG \
		--title \"\$DIALOG_TITLE\" \
		--backtitle \"\$DIALOG_BACKTITLE\" \
		--hline \"\$hline\"                \
		--ok-label \"\$msg_ok\"            \
		--cancel-label \"\$msg_cancel\"    \
		--default-item \"\$defaultitem\"   \
		--menu \"\$prompt\"                \
		$height $width $rows               \
		$menu_list                         \
		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
	)
	local retval=$?
	f_dialog_data_sanitize mtag

	f_dialog_title_restore

	[ $retval -eq 0 ] || return $FAILURE

	case "$mtag" in
	"R $msg_reset")
		SHOW_VALUE=1
		SHOW_DESC=1
		SHOW_DEFAULT_VALUE=
		SHOW_CONFIGURED=
		;;
	"D [X] $msg_desc") SHOW_DESC=  ;;
	"D [ ] $msg_desc") SHOW_DESC=1 ;;
	"1 ("?") $msg_show_value")
		SHOW_VALUE=1
		SHOW_DEFAULT_VALUE=
		SHOW_CONFIGURED=
		;;
	"2 ("?") $msg_show_default_value")
		SHOW_VALUE=
		SHOW_DEFAULT_VALUE=1
		SHOW_CONFIGURED=
		;;
	"3 ("?") $msg_show_configured")
		SHOW_VALUE=
		SHOW_DEFAULT_VALUE=
		SHOW_CONFIGURED=1
		;;
	esac
}

# f_dialog_input_rclist [$default]
#
# Presents a menu of rc.conf(5) defaults (with, or without descriptions). This
# function should be treated like a call to dialog(1) (the exit status should
# be captured and f_dialog_menutag_fetch() should be used to get the user's
# response). Optionally if present and non-null, highlight $default rcvar.
#
f_dialog_input_rclist()
{
	local prompt="$msg_please_select_an_rcconf_directive"
	local menu_list="
		'X $msg_exit' '' ${SHOW_DESC:+'$msg_exit_this_menu'}
	" # END-QUOTE
	local defaultitem="$1"
	local hline="$hline_arrows_tab_enter"

	if [ ! "$_RCCONF_MAP" ]; then
		# Generate RCCONF_MAP of `var desc ...' per-line
		f_dialog_info "$msg_creating_rcconf_map"
		RCCONF_MAP=$( f_startup_rcconf_map )
		export RCCONF_MAP
		# Generate _${var}_desc variables from $RCCONF_MAP
		f_startup_rcconf_map_expand
		export _RCCONF_MAP=1
	fi

	menu_list="$menu_list $(
		export SHOW_DESC
		echo "$RCCONF_MAP" | awk '
		BEGIN {
			prefix = ""
			rword  = "^[[:space:]]*[^[:space:]]*[[:space:]]*"
		}
		{
			cur_prefix = tolower(substr($1, 1, 1))
			printf "'\''"
			if ( prefix != cur_prefix )
				prefix = cur_prefix
			else
				printf " "
			rcvar  = $1
			printf "%s'\'' '\'\''", rcvar
			if ( ENVIRON["SHOW_DESC"] ) {
				desc = $0
				sub(rword, "", desc)
				gsub(/'\''/, "'\''\\'\'\''", desc)
				printf " '\''%s'\''", desc
			}
			printf "\n"
		}'
	)"

	set -f # set noglob because descriptions in the $menu_list may contain
	       # `*' and get expanded by dialog(1) (doesn't affect Xdialog(1)).
	       # This prevents dialog(1) from expanding wildcards in help line.

	local height width rows
	eval f_dialog_menu${SHOW_DESC:+_with_help}_size \
		height width rows \
		\"\$DIALOG_TITLE\"     \
		\"\$DIALOG_BACKTITLE\" \
		\"\$prompt\"           \
		\"\$hline\"            \
		$menu_list

	local menu_choice
	menu_choice=$( eval $DIALOG \
		--title \"\$DIALOG_TITLE\"         \
		--backtitle \"\$DIALOG_BACKTITLE\" \
		--hline \"\$hline\"                \
		--default-item \"\$defaultitem\"   \
		--ok-label \"\$msg_ok\"            \
		--cancel-label \"\$msg_cancel\"    \
		${SHOW_DESC:+--item-help}          \
		--menu \"\$prompt\"                \
		$height $width $rows               \
		$menu_list                         \
		2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
	)
	local retval=$?
	f_dialog_menutag_store -s "$menu_choice"
	return $retval
}

# f_dialog_input_rcvar [$init]
#
# Allows the user to enter the name for a new rc.conf(5) variable. If the user
# does not cancel or press ESC, the $rcvar variable will hold the newly-
# configured value upon return.
#
f_dialog_input_rcvar()
{
	#
	# Loop until the user provides taint-free/valid input
	#
	local _input="$1"
	while :; do

		# Return if user either pressed ESC or chosen Cancel/No
		f_dialog_input _input "$msg_please_enter_rcvar_name" \
		               "$_input" "$hline_alnum_tab_enter" || return

		# Check for invalid entry (1of2)
		if ! echo "$_input" | grep -q "^[[:alpha:]_]"; then
			f_show_msg "$msg_rcvar_must_start_with"
			continue
		fi

		# Check for invalid entry (2of2)
		if ! echo "$_input" | grep -q "^[[:alpha:]_][[:alnum:]_]*$"
		then
			f_show_msg "$msg_rcvar_contains_invalid_chars"
			continue
		fi

		rcvar="$_input"
		break
	done

	f_dprintf "f_dialog_input_rcvar: rcvar->[%s]" "$rcvar"

	return $SUCCESS
}

############################################################ MAIN

f_dprintf "%s: Successfully loaded." startup/rcconf.subr

fi # ! $_STARTUP_RCCONF_SUBR
OpenPOWER on IntegriCloud