xref: /freebsd/usr.sbin/bsdconfig/share/variable.subr (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1if [ ! "$_VARIABLE_SUBR" ]; then _VARIABLE_SUBR=1
2#
3# Copyright (c) 2012-2014 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..." variable.subr
33f_include $BSDCFG_SHARE/dialog.subr
34f_include $BSDCFG_SHARE/strings.subr
35
36############################################################ GLOBALS
37
38VARIABLES=
39
40#
41# Default behavior is to call f_variable_set_defaults() when loaded.
42#
43: ${VARIABLE_SELF_INITIALIZE=1}
44
45#
46# File to write when f_dump_variables() is called.
47#
48: ${VARIABLE_DUMPFILE:=/etc/bsdconfig.vars}
49
50############################################################ FUNCTIONS
51
52# f_variable_new $handle $variable
53#
54# Register a new variable named $variable with the given reference-handle
55# $handle. The environment variable $handle is set to $variable allowing you to
56# use the f_getvar() function (from common.subr) with $handle to get the value
57# of environment variable $variable. For example:
58#
59# 	f_variable_new VAR_ABC abc
60#
61# allows the later indirection:
62#
63# 	f_getvar $VAR_ABC
64#
65# to return the value of environment variable `abc'. Variables registered in
66# this manner are recorded in the $VARIABLES environment variable for later
67# allowing dynamic enumeration of so-called `registered/advertised' variables.
68#
69f_variable_new()
70{
71	local handle="$1" variable="$2"
72	[ "$handle" ] || return $FAILURE
73	f_dprintf "variable.subr: New variable %s -> %s" "$handle" "$variable"
74	setvar $handle $variable
75	VARIABLES="$VARIABLES${VARIABLES:+ }$handle"
76}
77
78# f_variable_get_value $var [ $fmt [ $opts ... ] ]
79#
80# Unless nonInteractive is set, prompt the user with a given value (pre-filled
81# with the value of $var) and give them the chance to change the value.
82#
83# Unlike f_getvar() (from common.subr) which can return a variable to the
84# caller on standard output, this function has no [meaningful] output.
85#
86# Returns success unless $var is either NULL or missing.
87#
88f_variable_get_value()
89{
90	local var="$1" cp
91
92	[ "$var" ] || return $FAILURE
93
94	if ! { f_getvar $var cp && ! f_interactive; }; then
95		shift 1 # var
96		f_dialog_input cp "$( printf "$@" )" "$cp" && setvar $var "$cp"
97	fi
98
99	return $SUCCESS
100}
101
102# f_variable_set_defaults
103#
104# Installs sensible defaults for registered/advertised variables.
105#
106f_variable_set_defaults()
107{
108	f_dprintf "f_variable_set_defaults: Initializing defaults..."
109
110	#
111	# Initialize various user-edittable values to their defaults
112	#
113	setvar $VAR_EDITOR		"${EDITOR:-/usr/bin/ee}"
114	setvar $VAR_FTP_STATE		"auto"
115	setvar $VAR_FTP_USER		"ftp"
116	setvar $VAR_HOSTNAME		"$( hostname )"
117	setvar $VAR_MEDIA_TIMEOUT	"300"
118	setvar $VAR_NFS_SECURE		"NO"
119	setvar $VAR_NFS_TCP		"NO"
120	setvar $VAR_NFS_V3		"YES"
121	setvar $VAR_PKG_TMPDIR		"/var/tmp"
122	setvar $VAR_RELNAME		"$UNAME_R"
123
124	#
125	# Debugging
126	#
127	if f_debugging; then
128		local var
129		for var in \
130			$VAR_EDITOR		\
131			$VAR_FTP_STATE		\
132			$VAR_FTP_USER		\
133			$VAR_HOSTNAME		\
134			$VAR_MEDIA_TIMEOUT	\
135			$VAR_NFS_SECURE		\
136			$VAR_NFS_TCP		\
137			$VAR_NFS_V3		\
138			$VAR_PKG_TMPDIR		\
139			$VAR_RELNAME		\
140		; do
141			f_quietly f_getvar $var
142		done
143	fi
144
145	f_dprintf "f_variable_set_defaults: Defaults initialized."
146}
147
148# f_dump_variables
149#
150# Dump a list of registered/advertised variables and their respective values to
151# $VARIABLE_DUMPFILE. Returns success unless the file couldn't be written. If
152# an error occurs, it is displayed using f_dialog_msgbox() (from dialog.subr).
153#
154f_dump_variables()
155{
156	local err
157	if ! err=$(
158		( for handle in $VARIABLES; do
159			f_getvar $handle var || continue
160			f_getvar $var value || continue
161			f_shell_escape "$value" value
162			printf "%s='%s'\n" "$var" "$value"
163		  done > "$VARIABLE_DUMPFILE" ) 2>&1
164	); then
165		f_dialog_msgbox "$err"
166		return $FAILURE
167	fi
168}
169
170# f_debugging
171#
172# Are we in debug mode? Returns success if extra DEBUG information has been
173# requested (by setting $debug to non-NULL), otherwise false.
174#
175f_debugging()
176{
177	local value
178	f_getvar $VAR_DEBUG value && [ "$value" ]
179}
180
181# f_interactive
182#
183# Are we running interactively? Return error if $nonInteractive is set and non-
184# NULL, otherwise return success.
185#
186f_interactive()
187{
188	local value
189	! f_getvar $VAR_NONINTERACTIVE value || [ ! "$value" ]
190}
191
192# f_netinteractive
193#
194# Has the user specifically requested the network-portion of configuration and
195# setup to be performed interactively? Returns success if the user has asked
196# for the network configuration to be done interactively even if perhaps over-
197# all non-interactive mode has been requested (by setting nonInteractive).
198#
199# Returns success if $netInteractive is set and non-NULL.
200#
201f_netinteractive()
202{
203	local value
204	f_getvar $VAR_NETINTERACTIVE value && [ "$value" ]
205}
206
207# f_zfsinteractive
208#
209# Has the user specifically requested the ZFS-portion of configuration and
210# setup to be performed interactively? Returns success if the user has asked
211# for the ZFS configuration to be done interactively even if perhaps overall
212# non-interactive mode has been requested (by setting nonInteractive).
213#
214# Returns success if $zfsInteractive is set and non-NULL.
215#
216f_zfsinteractive()
217{
218	local value
219	f_getvar $VAR_ZFSINTERACTIVE value && [ "$value" ]
220}
221
222############################################################ MAIN
223
224#
225# Variables that can be tweaked from config files
226#
227#              Handle                   Variable Name
228f_variable_new VAR_CONFIG_FILE		configFile
229f_variable_new VAR_DEBUG		debug
230f_variable_new VAR_DEBUG_FILE		debugFile
231f_variable_new VAR_DIRECTORY_PATH	_directoryPath
232f_variable_new VAR_DOMAINNAME		domainname
233f_variable_new VAR_EDITOR		editor
234f_variable_new VAR_EXTRAS		ifconfig_
235f_variable_new VAR_FTP_DIR		ftpDirectory
236f_variable_new VAR_FTP_HOST		ftpHost
237f_variable_new VAR_FTP_PASS		ftpPass
238f_variable_new VAR_FTP_PATH		_ftpPath
239f_variable_new VAR_FTP_PORT		ftpPort
240f_variable_new VAR_FTP_STATE		ftpState
241f_variable_new VAR_FTP_USER		ftpUser
242f_variable_new VAR_GATEWAY		defaultrouter
243f_variable_new VAR_GROUP		group
244f_variable_new VAR_GROUP_GID		groupGid
245f_variable_new VAR_GROUP_MEMBERS	groupMembers
246f_variable_new VAR_GROUP_PASSWORD	groupPassword
247f_variable_new VAR_HOSTNAME		hostname
248f_variable_new VAR_HTTP_DIR		httpDirectory
249f_variable_new VAR_HTTP_FTP_MODE	httpFtpMode
250f_variable_new VAR_HTTP_HOST		httpHost
251f_variable_new VAR_HTTP_PATH		_httpPath
252f_variable_new VAR_HTTP_PORT		httpPort
253f_variable_new VAR_HTTP_PROXY		httpProxy
254f_variable_new VAR_HTTP_PROXY_HOST	httpProxyHost
255f_variable_new VAR_HTTP_PROXY_PATH	_httpProxyPath
256f_variable_new VAR_HTTP_PROXY_PORT	httpProxyPort
257f_variable_new VAR_IFCONFIG		ifconfig_
258f_variable_new VAR_IPADDR		ipaddr
259f_variable_new VAR_IPV6ADDR		ipv6addr
260f_variable_new VAR_IPV6_ENABLE		ipv6_activate_all_interfaces
261f_variable_new VAR_KEYMAP		keymap
262f_variable_new VAR_MEDIA_TIMEOUT	MEDIA_TIMEOUT
263f_variable_new VAR_MEDIA_TYPE		mediaType
264f_variable_new VAR_NAMESERVER		nameserver
265f_variable_new VAR_NETINTERACTIVE	netInteractive
266f_variable_new VAR_NETMASK		netmask
267f_variable_new VAR_NETWORK_DEVICE	netDev
268f_variable_new VAR_NFS_HOST		nfsHost
269f_variable_new VAR_NFS_PATH		nfsPath
270f_variable_new VAR_NFS_SECURE		nfs_reserved_port_only
271f_variable_new VAR_NFS_TCP		nfs_use_tcp
272f_variable_new VAR_NFS_V3		nfs_use_v3
273f_variable_new VAR_NONINTERACTIVE	nonInteractive
274f_variable_new VAR_NO_CONFIRM		noConfirm
275f_variable_new VAR_NO_ERROR		noError
276f_variable_new VAR_NO_INET6		noInet6
277f_variable_new VAR_PACKAGE		package
278f_variable_new VAR_PKG_TMPDIR		PKG_TMPDIR
279f_variable_new VAR_PORTS_PATH		ports
280f_variable_new VAR_RELNAME		releaseName
281f_variable_new VAR_SLOW_ETHER		slowEthernetCard
282f_variable_new VAR_TRY_DHCP		tryDHCP
283f_variable_new VAR_TRY_RTSOL		tryRTSOL
284f_variable_new VAR_UFS_PATH		ufs
285f_variable_new VAR_USER			user
286f_variable_new VAR_USER_ACCOUNT_EXPIRE	userAccountExpire
287f_variable_new VAR_USER_DOTFILES_CREATE	userDotfilesCreate
288f_variable_new VAR_USER_GECOS		userGecos
289f_variable_new VAR_USER_GID		userGid
290f_variable_new VAR_USER_GROUPS		userGroups
291f_variable_new VAR_USER_GROUP_DELETE	userGroupDelete
292f_variable_new VAR_USER_HOME		userHome
293f_variable_new VAR_USER_HOME_CREATE	userHomeCreate
294f_variable_new VAR_USER_HOME_DELETE	userHomeDelete
295f_variable_new VAR_USER_LOGIN_CLASS	userLoginClass
296f_variable_new VAR_USER_PASSWORD	userPassword
297f_variable_new VAR_USER_PASSWORD_EXPIRE	userPasswordExpire
298f_variable_new VAR_USER_SHELL		userShell
299f_variable_new VAR_USER_UID		userUid
300f_variable_new VAR_ZFSINTERACTIVE	zfsInteractive
301
302#
303# Self-initialize unless requested otherwise
304#
305f_dprintf "%s: VARIABLE_SELF_INITIALIZE=[%s]" \
306          variable.subr "$VARIABLE_SELF_INITIALIZE"
307case "$VARIABLE_SELF_INITIALIZE" in
308""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;;
309*) f_variable_set_defaults
310esac
311
312f_dprintf "%s: Successfully loaded." variable.subr
313
314fi # ! $_VARIABLE_SUBR
315