xref: /freebsd/usr.sbin/bsdconfig/share/media/tcpip.subr (revision a7408e5cc126257ffb66c01f841c0ff03cb7749b)
17323adacSDevin Teskeif [ ! "$_MEDIA_TCPIP_SUBR" ]; then _MEDIA_TCPIP_SUBR=1
27323adacSDevin Teske#
37323adacSDevin Teske# Copyright (c) 2012-2013 Devin Teske
47323adacSDevin Teske# All Rights Reserved.
57323adacSDevin Teske#
67323adacSDevin Teske# Redistribution and use in source and binary forms, with or without
77323adacSDevin Teske# modification, are permitted provided that the following conditions
87323adacSDevin Teske# are met:
97323adacSDevin Teske# 1. Redistributions of source code must retain the above copyright
107323adacSDevin Teske#    notice, this list of conditions and the following disclaimer.
117323adacSDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
127323adacSDevin Teske#    notice, this list of conditions and the following disclaimer in the
137323adacSDevin Teske#    documentation and/or other materials provided with the distribution.
147323adacSDevin Teske#
157323adacSDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
167323adacSDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
177323adacSDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
187323adacSDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
197323adacSDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
207323adacSDevin Teske# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
217323adacSDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
227323adacSDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
237323adacSDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
247323adacSDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
257323adacSDevin Teske# SUCH DAMAGE.
267323adacSDevin Teske#
277323adacSDevin Teske# $FreeBSD$
287323adacSDevin Teske#
297323adacSDevin Teske############################################################ INCLUDES
307323adacSDevin Teske
317323adacSDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
327323adacSDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
337323adacSDevin Teskef_dprintf "%s: loading includes..." media/tcpip.subr
347323adacSDevin Teskef_include $BSDCFG_SHARE/device.subr
357323adacSDevin Teskef_include $BSDCFG_SHARE/dialog.subr
361de60ff0SDevin Teskef_include $BSDCFG_SHARE/struct.subr
377323adacSDevin Teskef_include $BSDCFG_SHARE/variable.subr
387323adacSDevin Teske
397323adacSDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig"
407323adacSDevin Teskef_include_lang $BSDCFG_LIBE/include/messages.subr
417323adacSDevin Teske
427323adacSDevin TeskeTCP_HELPFILE=$BSDCFG_LIBE/include/tcp.hlp
437323adacSDevin TeskeNETWORK_DEVICE_HELPFILE=$BSDCFG_LIBE/include/network_device.hlp
447323adacSDevin Teske
457323adacSDevin Teske############################################################ GLOBALS
467323adacSDevin Teske
477323adacSDevin Teske#
487323adacSDevin Teske# Path to resolv.conf(5).
497323adacSDevin Teske#
507323adacSDevin Teske: ${RESOLV_CONF:="/etc/resolv.conf"}
517323adacSDevin Teske
527323adacSDevin Teske#
537323adacSDevin Teske# Path to nsswitch.conf(5).
547323adacSDevin Teske#
557323adacSDevin Teske: ${NSSWITCH_CONF:="/etc/nsswitch.conf"}
567323adacSDevin Teske
577323adacSDevin Teske#
587323adacSDevin Teske# Path to hosts(5)
597323adacSDevin Teske#
607323adacSDevin Teske: ${ETC_HOSTS:="/etc/hosts"}
617323adacSDevin Teske
627323adacSDevin Teske#
637323adacSDevin Teske# Structure of dhclient.leases(5) lease { ... } entry
647323adacSDevin Teske#
657323adacSDevin Teskef_struct_define DHCP_LEASE \
667323adacSDevin Teske	interface		\
677323adacSDevin Teske	fixed_address		\
687323adacSDevin Teske	filename		\
697323adacSDevin Teske	server_name		\
707323adacSDevin Teske	script			\
717323adacSDevin Teske	medium			\
727323adacSDevin Teske	host_name		\
737323adacSDevin Teske	subnet_mask		\
747323adacSDevin Teske	routers			\
757323adacSDevin Teske	domain_name_servers	\
767323adacSDevin Teske	domain_name		\
777323adacSDevin Teske	broadcast_address	\
787323adacSDevin Teske	dhcp_lease_time		\
797323adacSDevin Teske	dhcp_message_type	\
807323adacSDevin Teske	dhcp_server_identifier	\
817323adacSDevin Teske	dhcp_renewal_time	\
827323adacSDevin Teske	dhcp_rebinding_time	\
837323adacSDevin Teske	renew			\
847323adacSDevin Teske	rebind			\
857323adacSDevin Teske	expire
867323adacSDevin Teske
877323adacSDevin Teske############################################################ FUNCTIONS
887323adacSDevin Teske
897323adacSDevin Teske# f_validate_hostname $hostname
907323adacSDevin Teske#
917323adacSDevin Teske# Returns zero if the given argument (a fully-qualified hostname) is compliant
927323adacSDevin Teske# with standards set-forth in RFC's 952 and 1123 of the Network Working Group:
937323adacSDevin Teske#
947323adacSDevin Teske# RFC 952 - DoD Internet host table specification
957323adacSDevin Teske# http://tools.ietf.org/html/rfc952
967323adacSDevin Teske#
977323adacSDevin Teske# RFC 1123 - Requirements for Internet Hosts - Application and Support
987323adacSDevin Teske# http://tools.ietf.org/html/rfc1123
997323adacSDevin Teske#
1007323adacSDevin Teske# See http://en.wikipedia.org/wiki/Hostname for a brief overview.
1017323adacSDevin Teske#
1027323adacSDevin Teske# The return status for invalid hostnames is one of:
1037323adacSDevin Teske# 	255	Entire hostname exceeds the maximum length of 255 characters.
1047323adacSDevin Teske# 	 63	One or more individual labels within the hostname (separated by
1057323adacSDevin Teske# 	   	dots) exceeds the maximum of 63 characters.
1067323adacSDevin Teske# 	  1	One or more individual labels within the hostname contains one
1077323adacSDevin Teske# 	   	or more invalid characters.
1087323adacSDevin Teske# 	  2	One or more individual labels within the hostname starts or
1097323adacSDevin Teske# 	   	ends with a hyphen (hyphens are allowed, but a label cannot
1107323adacSDevin Teske# 	   	begin or end with a hyphen).
1117323adacSDevin Teske# 	  3	One or more individual labels within the hostname are null.
1127323adacSDevin Teske#
1137323adacSDevin Teske# f_dialog_validate_hostname $hostname
1147323adacSDevin Teske#
1157323adacSDevin Teske# If the hostname is determined to be invalid, the appropriate error will be
1167323adacSDevin Teske# displayed using the f_show_msg function.
1177323adacSDevin Teske#
1187323adacSDevin Teskef_validate_hostname()
1197323adacSDevin Teske{
1207323adacSDevin Teske	local fqhn="$1"
1217323adacSDevin Teske
1227323adacSDevin Teske	# Return error if the hostname exceeds 255 characters
1237323adacSDevin Teske	[ ${#fqhn} -gt 255 ] && return 255
1247323adacSDevin Teske
1257323adacSDevin Teske	local IFS="." # Split on `dot'
1267323adacSDevin Teske	for label in $fqhn; do
1277323adacSDevin Teske		# Return error if the label exceeds 63 characters
1287323adacSDevin Teske		[ ${#label} -gt 63 ] && return 63
1297323adacSDevin Teske
1307323adacSDevin Teske		# Return error if the label is null
1317323adacSDevin Teske		[ "$label" ] || return 3
1327323adacSDevin Teske
1337323adacSDevin Teske		# Return error if label begins/ends with dash
1347323adacSDevin Teske		case "$label" in -*|*-) return 2; esac
1357323adacSDevin Teske
1367323adacSDevin Teske		# Return error if the label contains any invalid chars
1377323adacSDevin Teske		case "$label" in *[!0-9a-zA-Z-]*) return 1; esac
1387323adacSDevin Teske	done
1397323adacSDevin Teske
1407323adacSDevin Teske	return $SUCCESS
1417323adacSDevin Teske}
1427323adacSDevin Teske
1437323adacSDevin Teske# f_inet_atoi $ipv4_address [$var_to_set]
1447323adacSDevin Teske#
1457323adacSDevin Teske# Convert an IPv4 address or mask from dotted-quad notation (e.g., `127.0.0.1'
1467323adacSDevin Teske# or `255.255.255.0') to a 32-bit unsigned integer for the purpose of network
1477323adacSDevin Teske# and broadcast calculations. For example, one can validate that two addresses
1487323adacSDevin Teske# are on the same network:
1497323adacSDevin Teske#
1507323adacSDevin Teske# 	f_inet_atoi 1.2.3.4 ip1num
1517323adacSDevin Teske# 	f_inet_atoi 1.2.4.5 ip2num
1527323adacSDevin Teske# 	f_inet_atoi 255.255.0.0 masknum
1537323adacSDevin Teske# 	if [ $(( $ip1num & $masknum )) -eq \
1547323adacSDevin Teske# 	     $(( $ip2num & $masknum )) ]
1557323adacSDevin Teske# 	then
1567323adacSDevin Teske# 		: IP addresses are on same network
1577323adacSDevin Teske# 	fi
1587323adacSDevin Teske#
1597323adacSDevin Teske# See f_validate_ipaddr() below for an additional example usage, on calculating
1607323adacSDevin Teske# network and broadcast addresses.
1617323adacSDevin Teske#
1627323adacSDevin Teske# If $var_to_set is missing or NULL, the converted IP address is printed to
1637323adacSDevin Teske# standard output for capturing in a sub-shell (which is less-recommended
1647323adacSDevin Teske# because of performance degredation; for example, when called in a loop).
1657323adacSDevin Teske#
1667323adacSDevin Teskef_inet_atoi()
1677323adacSDevin Teske{
1687323adacSDevin Teske	local __addr="$1" __var_to_set="$2" __num=0
1697323adacSDevin Teske	if f_validate_ipaddr "$__addr"; then
1707323adacSDevin Teske		__num=$( IFS=.;  set -- $__addr; \
1717323adacSDevin Teske			echo $(( ($1 << 24) + ($2 << 16) + ($3 << 8) + $4 )) )
1727323adacSDevin Teske	fi
1737323adacSDevin Teske	if [ "$__var_to_set" ]; then
1747323adacSDevin Teske		setvar "$__var_to_set" $__num
1757323adacSDevin Teske	else
1767323adacSDevin Teske		echo $__num
1777323adacSDevin Teske	fi
1787323adacSDevin Teske}
1797323adacSDevin Teske
1807323adacSDevin Teske# f_validate_ipaddr $ipaddr [$netmask]
1817323adacSDevin Teske#
1827323adacSDevin Teske# Returns zero if the given argument (an IP address) is of the proper format.
1837323adacSDevin Teske#
1847323adacSDevin Teske# The return status for invalid IP address is one of:
1857323adacSDevin Teske# 	1	One or more individual octets within the IP address (separated
1867323adacSDevin Teske# 	 	by dots) contains one or more invalid characters.
1877323adacSDevin Teske# 	2	One or more individual octets within the IP address are null
1887323adacSDevin Teske# 	 	and/or missing.
1897323adacSDevin Teske# 	3	One or more individual octets within the IP address exceeds the
1907323adacSDevin Teske# 	 	maximum of 255 (or 2^8, being an octet comprised of 8 bits).
1917323adacSDevin Teske# 	4	The IP address has either too few or too many octets.
1927323adacSDevin Teske#
1937323adacSDevin Teske# If a netmask is provided, the IP address is checked further:
1947323adacSDevin Teske#
1957323adacSDevin Teske# 	5	The IP address must not be the network or broadcast address.
1967323adacSDevin Teske#
1977323adacSDevin Teskef_validate_ipaddr()
1987323adacSDevin Teske{
1997323adacSDevin Teske	local ip="$1" mask="$2"
2007323adacSDevin Teske
2017323adacSDevin Teske	# Track number of octets for error checking
2027323adacSDevin Teske	local noctets=0
2037323adacSDevin Teske
2047323adacSDevin Teske	local oldIFS="$IFS"
2057323adacSDevin Teske	local IFS="." # Split on `dot'
2067323adacSDevin Teske	for octet in $ip; do
2077323adacSDevin Teske		# Return error if the octet is null
2087323adacSDevin Teske		[ "$octet" ] || return 2
2097323adacSDevin Teske
2107323adacSDevin Teske		# Return error if not a whole integer
2117323adacSDevin Teske		f_isinteger "$octet" || return 1
2127323adacSDevin Teske
2137323adacSDevin Teske		# Return error if not a positive integer
2147323adacSDevin Teske		[ $octet -ge 0 ] || return 1
2157323adacSDevin Teske
2167323adacSDevin Teske		# Return error if the octet exceeds 255
2177323adacSDevin Teske		[ $octet -gt 255 ] && return 3
2187323adacSDevin Teske
2197323adacSDevin Teske		noctets=$(( $noctets + 1 ))
2207323adacSDevin Teske	done
2217323adacSDevin Teske	IFS="$oldIFS"
2227323adacSDevin Teske
2237323adacSDevin Teske	[ $noctets -eq 4 ] || return 4
2247323adacSDevin Teske
2257323adacSDevin Teske	#
2267323adacSDevin Teske	# The IP address must not be network or broadcast address.
2277323adacSDevin Teske	#
2287323adacSDevin Teske	if [ "$mask" ]; then
2297323adacSDevin Teske		local ipnum masknum netnum bcastnum
2307323adacSDevin Teske		local max_addr=4294967295 # 255.255.255.255
2317323adacSDevin Teske
2327323adacSDevin Teske		f_inet_atoi $ip ipnum
2337323adacSDevin Teske		f_inet_atoi $mask masknum
2347323adacSDevin Teske
2357323adacSDevin Teske		netnum=$(( $ipnum & $masknum ))
2367323adacSDevin Teske		bcastnum=$(( ($ipnum & $masknum)+$max_addr-$masknum ))
2377323adacSDevin Teske
2387323adacSDevin Teske		if [ "$masknum" ] &&
2397323adacSDevin Teske		   [ $ipnum -eq $netnum -o $ipnum -eq $bcastnum ]
2407323adacSDevin Teske		then
2417323adacSDevin Teske			return 5
2427323adacSDevin Teske		fi
2437323adacSDevin Teske	fi
2447323adacSDevin Teske
2457323adacSDevin Teske	return $SUCCESS
2467323adacSDevin Teske}
2477323adacSDevin Teske
2487323adacSDevin Teske# f_validate_ipaddr6 $ipv6_addr
2497323adacSDevin Teske#
2507323adacSDevin Teske# Returns zero if the given argument (an IPv6 address) is of the proper format.
2517323adacSDevin Teske#
2527323adacSDevin Teske# The return status for invalid IP address is one of:
2537323adacSDevin Teske# 	1	One or more individual segments within the IP address
2547323adacSDevin Teske# 	 	(separated by colons) contains one or more invalid characters.
2557323adacSDevin Teske# 	 	Segments must contain only combinations of the characters 0-9,
2567323adacSDevin Teske# 	 	A-F, or a-f.
2577323adacSDevin Teske# 	2	Too many/incorrect null segments. A single null segment is
2587323adacSDevin Teske# 	 	allowed within the IP address (separated by colons) but not
2597323adacSDevin Teske# 	 	allowed at the beginning or end (unless a double-null segment;
2607323adacSDevin Teske# 	 	i.e., "::*" or "*::").
2617323adacSDevin Teske# 	3	One or more individual segments within the IP address
2627323adacSDevin Teske# 	 	(separated by colons) exceeds the length of 4 hex-digits.
2637323adacSDevin Teske# 	4	The IP address entered has either too few (less than 3), too
2647323adacSDevin Teske# 	 	many (more than 8), or not enough segments, separated by
2657323adacSDevin Teske# 	 	colons.
2667323adacSDevin Teske# 	5*	The IPv4 address at the end of the IPv6 address is invalid.
2677323adacSDevin Teske# 	*	When there is an error with the dotted-quad IPv4 address at the
2687323adacSDevin Teske# 	 	end of the IPv6 address, the return value of 5 is OR'd with a
2697323adacSDevin Teske# 	 	bit-shifted (<< 4) return of f_validate_ipaddr.
2707323adacSDevin Teske#
2717323adacSDevin Teskef_validate_ipaddr6()
2727323adacSDevin Teske{
2737323adacSDevin Teske	local ip="${1%\%*}" # removing the interface specification if-present
2747323adacSDevin Teske
2757323adacSDevin Teske	local IFS=":" # Split on `colon'
2767323adacSDevin Teske	set -- $ip:
2777323adacSDevin Teske
2787323adacSDevin Teske	# Return error if too many or too few segments
2797323adacSDevin Teske	# Using 9 as max in case of leading or trailing null spanner
2807323adacSDevin Teske	[ $# -gt 9 -o $# -lt 3 ] && return 4
2817323adacSDevin Teske
2827323adacSDevin Teske	local h="[0-9A-Fa-f]"
2837323adacSDevin Teske	local nulls=0 nsegments=$# contains_ipv4_segment=
2847323adacSDevin Teske
2857323adacSDevin Teske	while [ $# -gt 0 ]; do
2867323adacSDevin Teske
2877323adacSDevin Teske		segment="${1%:}"
2887323adacSDevin Teske		shift
2897323adacSDevin Teske
2907323adacSDevin Teske		#
2917323adacSDevin Teske		# Return error if this segment makes one null too-many. A
2927323adacSDevin Teske		# single null segment is allowed anywhere in the middle as well
2937323adacSDevin Teske		# as double null segments are allowed at the beginning or end
2947323adacSDevin Teske		# (but not both).
2957323adacSDevin Teske		#
2967323adacSDevin Teske		if [ ! "$segment" ]; then
2977323adacSDevin Teske			nulls=$(( $nulls + 1 ))
2987323adacSDevin Teske			if [ $nulls -eq 3 ]; then
2997323adacSDevin Teske				# Only valid syntax for 3 nulls is `::'
3007323adacSDevin Teske				[ "$ip" = "::" ] || return 2
3017323adacSDevin Teske			elif [ $nulls -eq 2 ]; then
3027323adacSDevin Teske				# Only valid if begins/ends with `::'
3037323adacSDevin Teske				case "$ip" in
3047323adacSDevin Teske				::*|*::) : fall thru ;;
3057323adacSDevin Teske				*) return 2
3067323adacSDevin Teske				esac
3077323adacSDevin Teske			fi
3087323adacSDevin Teske			continue
3097323adacSDevin Teske		fi
3107323adacSDevin Teske
3117323adacSDevin Teske		#
3127323adacSDevin Teske		# Return error if not a valid hexadecimal short
3137323adacSDevin Teske		#
3147323adacSDevin Teske		case "$segment" in
3157323adacSDevin Teske		$h|$h$h|$h$h$h|$h$h$h$h)
3167323adacSDevin Teske			: valid segment of 1-4 hexadecimal digits
3177323adacSDevin Teske			;;
3187323adacSDevin Teske		*[!0-9A-Fa-f]*)
3197323adacSDevin Teske			# Segment contains at least one invalid char
3207323adacSDevin Teske
3217323adacSDevin Teske			# Return error immediately if not last segment
3227323adacSDevin Teske			[ $# -eq 0 ] || return 1
3237323adacSDevin Teske
3247323adacSDevin Teske			# Otherwise, check for legacy IPv4 notation
3257323adacSDevin Teske			case "$segment" in
3267323adacSDevin Teske			*[!0-9.]*)
3277323adacSDevin Teske				# Segment contains at least one invalid
3287323adacSDevin Teske				# character even for an IPv4 address
3297323adacSDevin Teske				return 1
3307323adacSDevin Teske			esac
3317323adacSDevin Teske
3327323adacSDevin Teske			# Return error if not enough segments
3337323adacSDevin Teske			if [ $nulls -eq 0 ]; then
3347323adacSDevin Teske				[ $nsegments -eq 7 ] || return 4
3357323adacSDevin Teske			fi
3367323adacSDevin Teske
3377323adacSDevin Teske			contains_ipv4_segment=1
3387323adacSDevin Teske
3397323adacSDevin Teske			# Validate the IPv4 address
3407323adacSDevin Teske			f_validate_ipaddr "$segment" ||
3417323adacSDevin Teske				return $(( 5 | $? << 4 ))
3427323adacSDevin Teske			;;
3437323adacSDevin Teske		*)
3447323adacSDevin Teske			# Segment characters are all valid but too many
3457323adacSDevin Teske			return 3
3467323adacSDevin Teske		esac
3477323adacSDevin Teske
3487323adacSDevin Teske	done
3497323adacSDevin Teske
3507323adacSDevin Teske	if [ $nulls -eq 1 ]; then
3517323adacSDevin Teske		# Single null segment cannot be at beginning/end
3527323adacSDevin Teske		case "$ip" in
3537323adacSDevin Teske		:*|*:) return 2
3547323adacSDevin Teske		esac
3557323adacSDevin Teske	fi
3567323adacSDevin Teske
3577323adacSDevin Teske	#
3587323adacSDevin Teske	# A legacy IPv4 address can span the last two 16-bit segments,
3597323adacSDevin Teske	# reducing the amount of maximum allowable segments by-one.
3607323adacSDevin Teske	#
3617323adacSDevin Teske	maxsegments=8
3627323adacSDevin Teske	if [ "$contains_ipv4_segment" ]; then
3637323adacSDevin Teske		maxsegments=7
3647323adacSDevin Teske	fi
3657323adacSDevin Teske
3667323adacSDevin Teske	case $nulls in
3677323adacSDevin Teske	# Return error if missing segments with no null spanner
3687323adacSDevin Teske	0) [ $nsegments -eq $maxsegments ] || return 4 ;;
3697323adacSDevin Teske	# Return error if null spanner with too many segments
3707323adacSDevin Teske	1) [ $nsegments -le $maxsegments ] || return 4 ;;
3717323adacSDevin Teske	# Return error if leading/trailing `::' with too many segments
3727323adacSDevin Teske	2) [ $nsegments -le $(( $maxsegments + 1 )) ] || return 4 ;;
3737323adacSDevin Teske	esac
3747323adacSDevin Teske
3757323adacSDevin Teske	return $SUCCESS
3767323adacSDevin Teske}
3777323adacSDevin Teske
3787323adacSDevin Teske# f_validate_netmask $netmask
3797323adacSDevin Teske#
3807323adacSDevin Teske# Returns zero if the given argument (a subnet mask) is of the proper format.
3817323adacSDevin Teske#
3827323adacSDevin Teske# The return status for invalid netmask is one of:
3837323adacSDevin Teske# 	1	One or more individual fields within the subnet mask (separated
3847323adacSDevin Teske# 	 	by dots) contains one or more invalid characters.
3857323adacSDevin Teske# 	2	One or more individual fields within the subnet mask are null
3867323adacSDevin Teske# 	 	and/or missing.
3877323adacSDevin Teske# 	3	One or more individual fields within the subnet mask exceeds
3887323adacSDevin Teske# 	 	the maximum of 255 (a full 8-bit register).
3897323adacSDevin Teske# 	4	The subnet mask has either too few or too many fields.
3907323adacSDevin Teske# 	5	One or more individual fields within the subnet mask is an
3917323adacSDevin Teske# 	 	invalid integer (only 0,128,192,224,240,248,252,254,255 are
3927323adacSDevin Teske# 	 	valid integers).
3937323adacSDevin Teske#
3947323adacSDevin Teskef_validate_netmask()
3957323adacSDevin Teske{
3967323adacSDevin Teske	local mask="$1"
3977323adacSDevin Teske
3987323adacSDevin Teske	# Track number of fields for error checking
3997323adacSDevin Teske	local nfields=0
4007323adacSDevin Teske
4017323adacSDevin Teske	local IFS="." # Split on `dot'
4027323adacSDevin Teske	for field in $mask; do
4037323adacSDevin Teske		# Return error if the field is null
4047323adacSDevin Teske		[ "$field" ] || return 2
4057323adacSDevin Teske
4067323adacSDevin Teske		# Return error if not a whole positive integer
4077323adacSDevin Teske		f_isinteger "$field" || return 1
4087323adacSDevin Teske
4097323adacSDevin Teske		# Return error if the field exceeds 255
4107323adacSDevin Teske		[ $field -gt 255 ] && return 3
4117323adacSDevin Teske
4127323adacSDevin Teske		# Return error if the field is an invalid integer
4137323adacSDevin Teske		case "$field" in
4147323adacSDevin Teske		0|128|192|224|240|248|252|254|255) :;;
4157323adacSDevin Teske		*) return 5;;
4167323adacSDevin Teske		esac
4177323adacSDevin Teske
4187323adacSDevin Teske		nfields=$(( $nfields + 1 ))
4197323adacSDevin Teske	done
4207323adacSDevin Teske
4217323adacSDevin Teske	[ $nfields -eq 4 ] || return 4
4227323adacSDevin Teske}
4237323adacSDevin Teske
4247323adacSDevin Teske# f_validate_gateway $gateway $ipaddr $netmask
4257323adacSDevin Teske#
4267323adacSDevin Teske# Validate an IPv4 default gateway (aka router) address for a given IP address
4277323adacSDevin Teske# making sure the two are in the same network (able to ``talk'' to each other).
4287323adacSDevin Teske# Returns success if $ipaddr and $gateway are in the same network given subnet
4297323adacSDevin Teske# mask $netmask.
4307323adacSDevin Teske#
4317323adacSDevin Teskef_validate_gateway()
4327323adacSDevin Teske{
4337323adacSDevin Teske	local gateway="$1" ipaddr="$2" netmask="$3"
4347323adacSDevin Teske	local gwnum ipnum masknum
4357323adacSDevin Teske
4367323adacSDevin Teske	f_validate_ipaddr "$gateway" "$netmask" || return $FAILURE
4377323adacSDevin Teske
4387323adacSDevin Teske	f_inet_atoi "$netmask" masknum
4397323adacSDevin Teske	f_inet_atoi "$ipaddr"  ipnum
4407323adacSDevin Teske	f_inet_atoi "$gateway" gwnum
4417323adacSDevin Teske
4427323adacSDevin Teske	# Gateway must be within set of IPs reachable through interface
4437323adacSDevin Teske	[ $(( $ipnum & $masknum )) -eq \
4447323adacSDevin Teske	  $(( $gwnum & $masknum )) ] # Return status
4457323adacSDevin Teske}
4467323adacSDevin Teske
4477323adacSDevin Teske# f_dialog_validate_tcpip $hostname $gateway $nameserver $ipaddr $netmask
4487323adacSDevin Teske#
4497323adacSDevin Teske# Returns success if the arguments provided are valid for accessing a TCP/IP
4507323adacSDevin Teske# network, otherwise returns failure.
4517323adacSDevin Teske#
4527323adacSDevin Teskef_dialog_validate_tcpip()
4537323adacSDevin Teske{
4547323adacSDevin Teske	local hostname="$1" gateway="$2" nameserver="$3"
4557323adacSDevin Teske	local ipaddr="$4" netmask="$5"
4567323adacSDevin Teske	local ipnum masknum
4577323adacSDevin Teske
4587323adacSDevin Teske	if [ ! "$hostname" ]; then
4597323adacSDevin Teske		f_dialog_msgbox "$msg_must_specify_a_host_name_of_some_sort"
4607323adacSDevin Teske	elif ! f_validate_hostname "$hostname"; then
4617323adacSDevin Teske		f_dialog_msgbox "$msg_invalid_hostname_value"
4627323adacSDevin Teske	elif [ "$netmask" ] && ! f_validate_netmask "$netmask"; then
4637323adacSDevin Teske		f_dialog_msgbox "$msg_invalid_netmask_value"
4647323adacSDevin Teske	elif [ "$nameserver" ] &&
4657323adacSDevin Teske	      ! f_validate_ipaddr "$nameserver" &&
4667323adacSDevin Teske	      ! f_validate_ipaddr6 "$nameserver"; then
4677323adacSDevin Teske		f_dialog_msgbox "$msg_invalid_name_server_ip_address_specified"
4687323adacSDevin Teske	elif [ "$ipaddr" ] && ! f_validate_ipaddr "$ipaddr" "$netmask"; then
4697323adacSDevin Teske		f_dialog_msgbox "$msg_invalid_ipv4_address"
4707323adacSDevin Teske	elif [ "$gateway" -a "$gateway" != "NO" ] &&
4717323adacSDevin Teske	      ! f_validate_gateway "$gateway" "$ipaddr" "$netmask"; then
4727323adacSDevin Teske		f_dialog_msgbox "$msg_invalid_gateway_ipv4_address_specified"
4737323adacSDevin Teske	else
4747323adacSDevin Teske		return $SUCCESS
4757323adacSDevin Teske	fi
4767323adacSDevin Teske
4777323adacSDevin Teske	return $FAILURE
4787323adacSDevin Teske}
4797323adacSDevin Teske
4807323adacSDevin Teske# f_ifconfig_inet $interface [$var_to_set]
4817323adacSDevin Teske#
4827323adacSDevin Teske# Returns the IPv4 address associated with $interface. If $var_to_set is
4837323adacSDevin Teske# missing or NULL, the IP address is printed to standard output for capturing
4847323adacSDevin Teske# in a sub-shell (which is less-recommended because of performance degredation;
4857323adacSDevin Teske# for example, when called in a loop).
4867323adacSDevin Teske#
4877323adacSDevin Teske# This function is a two-parter. Below is the awk(1) portion of the function,
4887323adacSDevin Teske# afterward is the sh(1) function which utilizes the below awk script.
4897323adacSDevin Teske#
4907323adacSDevin Teskef_ifconfig_inet_awk='
4917323adacSDevin TeskeBEGIN { found = 0 }
4927323adacSDevin Teske( $1 == "inet" ) \
4937323adacSDevin Teske{
4947323adacSDevin Teske	print $2
4957323adacSDevin Teske	found = 1
4967323adacSDevin Teske	exit
4977323adacSDevin Teske}
4987323adacSDevin TeskeEND { exit ! found }
4997323adacSDevin Teske'
5007323adacSDevin Teskef_ifconfig_inet()
5017323adacSDevin Teske{
5027323adacSDevin Teske	local __interface="$1" __var_to_set="$2"
5037323adacSDevin Teske	if [ "$__var_to_set" ]; then
5047323adacSDevin Teske		local __ip
5057323adacSDevin Teske		__ip=$( ifconfig "$__interface" 2> /dev/null |
5067323adacSDevin Teske			awk "$f_ifconfig_inet_awk" )
5077323adacSDevin Teske		setvar "$__var_to_set" "$__ip"
5087323adacSDevin Teske	else
5097323adacSDevin Teske		ifconfig "$__interface" 2> /dev/null |
5107323adacSDevin Teske			awk "$f_ifconfig_inet_awk"
5117323adacSDevin Teske	fi
5127323adacSDevin Teske}
5137323adacSDevin Teske
5147323adacSDevin Teske# f_ifconfig_inet6 $interface [$var_to_set]
5157323adacSDevin Teske#
5167323adacSDevin Teske# Returns the IPv6 address associated with $interface. If $var_to_set is
5177323adacSDevin Teske# missing or NULL, the IP address is printed to standard output for capturing
5187323adacSDevin Teske# in a sub-shell (which is less-recommended because of performance degredation;
5197323adacSDevin Teske# for example, when called in a loop).
5207323adacSDevin Teske#
5217323adacSDevin Teske# This function is a two-parter. Below is the awk(1) portion of the function,
5227323adacSDevin Teske# afterward is the sh(1) function which utilizes the below awk script.
5237323adacSDevin Teske#
5247323adacSDevin Teskef_ifconfig_inet6_awk='
5257323adacSDevin TeskeBEGIN { found = 0 }
5267323adacSDevin Teske( $1 == "inet6" ) \
5277323adacSDevin Teske{
5287323adacSDevin Teske	print $2
5297323adacSDevin Teske	found = 1
5307323adacSDevin Teske	exit
5317323adacSDevin Teske}
5327323adacSDevin TeskeEND { exit ! found }
5337323adacSDevin Teske'
5347323adacSDevin Teskef_ifconfig_inet6()
5357323adacSDevin Teske{
5367323adacSDevin Teske	local __interface="$1" __var_to_set="$2"
5377323adacSDevin Teske	if [ "$__var_to_set" ]; then
5387323adacSDevin Teske		local __ip6
5397323adacSDevin Teske		__ip6=$( ifconfig "$__interface" 2> /dev/null |
5407323adacSDevin Teske			awk "$f_ifconfig_inet6_awk" )
5417323adacSDevin Teske		setvar "$__var_to_set" "$__ip6"
5427323adacSDevin Teske	else
5437323adacSDevin Teske		ifconfig "$__interface" 2> /dev/null |
5447323adacSDevin Teske			awk "$f_ifconfig_inet6_awk"
5457323adacSDevin Teske	fi
5467323adacSDevin Teske}
5477323adacSDevin Teske
5487323adacSDevin Teske# f_ifconfig_netmask $interface [$var_to_set]
5497323adacSDevin Teske#
5507323adacSDevin Teske# Returns the IPv4 subnet mask associated with $interface. If $var_to_set is
5517323adacSDevin Teske# missing or NULL, the netmask is printed to standard output for capturing in a
5527323adacSDevin Teske# sub-shell (which is less-recommended because of performance degredation; for
5537323adacSDevin Teske# example, when called in a loop).
5547323adacSDevin Teske#
5557323adacSDevin Teskef_ifconfig_netmask()
5567323adacSDevin Teske{
5577323adacSDevin Teske	local __interface="$1" __var_to_set="$2" __octets
5587323adacSDevin Teske	__octets=$( ifconfig "$__interface" 2> /dev/null | awk \
5597323adacSDevin Teske	'
5607323adacSDevin Teske		BEGIN { found = 0 }
5617323adacSDevin Teske		( $1 == "inet" ) \
5627323adacSDevin Teske		{
5637323adacSDevin Teske			printf "%s %s %s %s\n",
5647323adacSDevin Teske				substr($4,3,2),
5657323adacSDevin Teske				substr($4,5,2),
5667323adacSDevin Teske				substr($4,7,2),
5677323adacSDevin Teske				substr($4,9,2)
5687323adacSDevin Teske			found = 1
5697323adacSDevin Teske			exit
5707323adacSDevin Teske		}
5717323adacSDevin Teske		END { exit ! found }
5727323adacSDevin Teske	' ) || return $FAILURE
5737323adacSDevin Teske
5747323adacSDevin Teske	local __octet __netmask=
5757323adacSDevin Teske	for __octet in $__octets; do
5767323adacSDevin Teske		__netmask="$__netmask.$( printf "%u" "0x$__octet" )"
5777323adacSDevin Teske	done
5787323adacSDevin Teske	__netmask="${__netmask#.}"
5797323adacSDevin Teske	if [ "$__var_to_set" ]; then
5807323adacSDevin Teske		setvar "$__var_to_set" "$__netmask"
5817323adacSDevin Teske	else
5827323adacSDevin Teske		echo $__netmask
5837323adacSDevin Teske	fi
5847323adacSDevin Teske}
5857323adacSDevin Teske
5867323adacSDevin Teske# f_route_get_default [$var_to_set]
5877323adacSDevin Teske#
5887323adacSDevin Teske# Returns the IP address of the currently active default router. If $var_to_set
5897323adacSDevin Teske# is missing or NULL, the IP address is printed to standard output for
5907323adacSDevin Teske# capturing in a sub-shell (which is less-recommended because of performance
5917323adacSDevin Teske# degredation; for example, when called in a loop).
5927323adacSDevin Teske#
5937323adacSDevin Teske# This function is a two-parter. Below is the awk(1) portion of the function,
5947323adacSDevin Teske# afterward is the sh(1) function which utilizes the below awk script.
5957323adacSDevin Teske#
596*a7408e5cSDevin Teskef_route_get_default_awk='
5977323adacSDevin TeskeBEGIN { found = 0 }
5987323adacSDevin Teske( $1 == "gateway:" ) \
5997323adacSDevin Teske{
6007323adacSDevin Teske	print $2
6017323adacSDevin Teske	found = 1
6027323adacSDevin Teske	exit
6037323adacSDevin Teske}
6047323adacSDevin TeskeEND { exit ! found }
6057323adacSDevin Teske'
6067323adacSDevin Teskef_route_get_default()
6077323adacSDevin Teske{
6087323adacSDevin Teske	local __var_to_set="$1"
6097323adacSDevin Teske	if [ "$__var_to_set" ]; then
6107323adacSDevin Teske		local __ip
6117323adacSDevin Teske		__ip=$( route -n get default 2> /dev/null |
6127323adacSDevin Teske			awk "$f_route_get_default_awk" )
6137323adacSDevin Teske		setvar "$__var_to_set" "$__ip"
6147323adacSDevin Teske	else
6157323adacSDevin Teske		route -n get default 2> /dev/null |
6167323adacSDevin Teske			awk "$f_route_get_default_awk"
6177323adacSDevin Teske	fi
6187323adacSDevin Teske}
6197323adacSDevin Teske
6207323adacSDevin Teske# f_resolv_conf_nameservers [$var_to_set]
6217323adacSDevin Teske#
6227323adacSDevin Teske# Returns nameserver(s) configured in resolv.conf(5). If $var_to_set is missing
6237323adacSDevin Teske# or NULL, the list of nameservers is printed to standard output for capturing
6247323adacSDevin Teske# in a sub-shell (which is less-recommended because of performance degredation;
6257323adacSDevin Teske# for example, when called in a loop).
6267323adacSDevin Teske#
6277323adacSDevin Teske# This function is a two-parter. Below is the awk(1) portion of the function,
6287323adacSDevin Teske# afterward is the sh(1) function which utilizes the below awk script.
6297323adacSDevin Teske#
6307323adacSDevin Teskef_resolv_conf_nameservers_awk='
6317323adacSDevin TeskeBEGIN { found = 0 }
6327323adacSDevin Teske( $1 == "nameserver" ) \
6337323adacSDevin Teske{
6347323adacSDevin Teske	print $2
6357323adacSDevin Teske	found = 1
6367323adacSDevin Teske}
6377323adacSDevin TeskeEND { exit ! found }
6387323adacSDevin Teske'
6397323adacSDevin Teskef_resolv_conf_nameservers()
6407323adacSDevin Teske{
6417323adacSDevin Teske	local __var_to_set="$1"
6427323adacSDevin Teske	if [ "$__var_to_set" ]; then
6437323adacSDevin Teske		local __ns
6447323adacSDevin Teske		__ns=$( awk "$f_resolv_conf_nameservers_awk" "$RESOLV_CONF" \
6457323adacSDevin Teske			2> /dev/null )
6467323adacSDevin Teske		setvar "$__var_to_set" "$__ns"
6477323adacSDevin Teske	else
6487323adacSDevin Teske		awk "$f_resolv_conf_nameservers_awk" "$RESOLV_CONF" \
6497323adacSDevin Teske			2> /dev/null
6507323adacSDevin Teske	fi
6517323adacSDevin Teske}
6527323adacSDevin Teske
6537323adacSDevin Teske# f_config_resolv
6547323adacSDevin Teske#
6557323adacSDevin Teske# Attempts to configure resolv.conf(5) and ilk. Returns success if able to
6567323adacSDevin Teske# write the file(s), otherwise returns error status.
6577323adacSDevin Teske#
6587323adacSDevin Teske# Variables from variable.subr that are used in configuring resolv.conf(5) are
6597323adacSDevin Teske# as follows (all of which can be configured automatically through functions
6607323adacSDevin Teske# like f_dhcp_get_info() or manually):
6617323adacSDevin Teske#
6627323adacSDevin Teske# 	VAR_NAMESERVER
6637323adacSDevin Teske#		The nameserver to add in resolv.conf(5).
6647323adacSDevin Teske# 	VAR_DOMAINNAME
6657323adacSDevin Teske# 		The domain to configure in resolv.conf(5). Also used in the
6667323adacSDevin Teske# 		configuration of hosts(5).
6677323adacSDevin Teske# 	VAR_IPADDR
6687323adacSDevin Teske# 		The IPv4 address to configure in hosts(5).
6697323adacSDevin Teske# 	VAR_IPV6ADDR
6707323adacSDevin Teske# 		The IPv6 address to configure in hosts(5).
6717323adacSDevin Teske# 	VAR_HOSTNAME
6727323adacSDevin Teske# 		The hostname to associate with the IPv4 and/or IPv6 address in
6737323adacSDevin Teske# 		hosts(5).
6747323adacSDevin Teske#
6757323adacSDevin Teskef_config_resolv()
6767323adacSDevin Teske{
6777323adacSDevin Teske	local cp c6p dp hp
6787323adacSDevin Teske
6797323adacSDevin Teske	f_getvar $VAR_NAMESERVER cp
6807323adacSDevin Teske	if [ "$cp" ]; then
6817323adacSDevin Teske		case "$RESOLV_CONF" in
6827323adacSDevin Teske		*/*) f_quietly mkdir -p "${RESOLV_CONF%/*}" ;;
6837323adacSDevin Teske		esac
6847323adacSDevin Teske
6857323adacSDevin Teske		# Attempt to create/truncate the file
6867323adacSDevin Teske		( :> "$RESOLV_CONF" ) 2> /dev/null || return $FAILURE
6877323adacSDevin Teske
6887323adacSDevin Teske		f_getvar $VAR_DOMAINNAME dp &&
6897323adacSDevin Teske			printf "domain\t%s\n" "$dp" >> "$RESOLV_CONF"
6907323adacSDevin Teske		printf "nameserver\t%s\n" "$cp" >> "$RESOLV_CONF"
6917323adacSDevin Teske
6927323adacSDevin Teske		f_dprintf "Wrote out %s" "$RESOLV_CONF"
6937323adacSDevin Teske	fi
6947323adacSDevin Teske
6957323adacSDevin Teske	f_getvar $VAR_DOMAINNAME dp
6967323adacSDevin Teske	f_getvar $VAR_IPADDR cp
6977323adacSDevin Teske	f_getvar $VAR_IPV6ADDR c6p
6987323adacSDevin Teske	f_getvar $VAR_HOSTNAME hp
6997323adacSDevin Teske
7007323adacSDevin Teske	# Attempt to create the file if it doesn't already exist
7017323adacSDevin Teske	if [ ! -e "$ETC_HOSTS" ]; then
7027323adacSDevin Teske		case "$ETC_HOSTS" in
7037323adacSDevin Teske		*/*) f_quietly mkdir -p "${ETC_HOSTS%/*}" ;;
7047323adacSDevin Teske		esac
7057323adacSDevin Teske
7067323adacSDevin Teske		( :> "$ETC_HOSTS" ) 2> /dev/null || return $FAILURE
7077323adacSDevin Teske	fi
7087323adacSDevin Teske
7097323adacSDevin Teske	# Scan the file and add ourselves if not already configured
7107323adacSDevin Teske	awk -v dn="$dp" -v ip4="$cp" -v ip6="$c6p" -v hn="$hp" '
7117323adacSDevin Teske		BEGIN {
7127323adacSDevin Teske			local4found = local6found = 0
7137323adacSDevin Teske			hn4found = hn6found = h4found = h6found = 0
7147323adacSDevin Teske			h = ( match(hn, /\./) ? substr(hn, 0, RSTART-1) : "" )
7157323adacSDevin Teske		}
7167323adacSDevin Teske		($1 == "127.0.0.1") { local4found = 1 }
7177323adacSDevin Teske		($1 == "::1") { local6found = 1 }
7187323adacSDevin Teske		{
7197323adacSDevin Teske			for (n = 2; n <= NF; n++)
7207323adacSDevin Teske			{
7217323adacSDevin Teske				if ( $1 == ip4 ) {
7227323adacSDevin Teske					if ( $n == h ) h4found = 1
7237323adacSDevin Teske					if ( $n == hn ) hn4found = 1
7247323adacSDevin Teske					if ( $n == hn "." ) hn4found = 1
7257323adacSDevin Teske				}
7267323adacSDevin Teske				if ( $1 == ip6 ) {
7277323adacSDevin Teske					if ( $n == h ) h6found = 1
7287323adacSDevin Teske					if ( $n == hn ) hn6found = 1
7297323adacSDevin Teske					if ( $n == hn "." ) hn6found = 1
7307323adacSDevin Teske				}
7317323adacSDevin Teske			}
7327323adacSDevin Teske		}
7337323adacSDevin Teske		END {
7347323adacSDevin Teske			hosts = FILENAME
7357323adacSDevin Teske
7367323adacSDevin Teske			if ( ! local6found )
7377323adacSDevin Teske				printf "::1\t\t\tlocalhost%s\n",
7387323adacSDevin Teske				       ( dn ? " localhost." dn : "" ) >> hosts
7397323adacSDevin Teske			if ( ! local4found )
7407323adacSDevin Teske				printf "127.0.0.1\t\tlocalhost%s\n",
7417323adacSDevin Teske				       ( dn ? " localhost." dn : "" ) >> hosts
7427323adacSDevin Teske
7437323adacSDevin Teske			if ( ip6 && ! (h6found && hn6found))
7447323adacSDevin Teske			{
7457323adacSDevin Teske				printf "%s\t%s %s\n", ip6, hn, h >> hosts
7467323adacSDevin Teske				printf "%s\t%s.\n", ip6, hn >> hosts
7477323adacSDevin Teske			}
7487323adacSDevin Teske			else if ( ip6 )
7497323adacSDevin Teske			{
7507323adacSDevin Teske				if ( ! h6found )
7517323adacSDevin Teske					printf "%s\t%s.\n", ip6, h >> hosts
7527323adacSDevin Teske				if ( ! hn6found )
7537323adacSDevin Teske					printf "%s\t%s\n", ip6, hn >> hosts
7547323adacSDevin Teske			}
7557323adacSDevin Teske
7567323adacSDevin Teske			if ( ip4 && ! (h4found && hn4found))
7577323adacSDevin Teske			{
7587323adacSDevin Teske				printf "%s\t\t%s %s\n", ip4, hn, h >> hosts
7597323adacSDevin Teske				printf "%s\t\t%s.\n", ip4, hn >> hosts
7607323adacSDevin Teske			}
7617323adacSDevin Teske			else if ( ip4 )
7627323adacSDevin Teske			{
7637323adacSDevin Teske				if ( ! h4found )
7647323adacSDevin Teske					printf "%s\t\t%s.\n", ip4, h >> hosts
7657323adacSDevin Teske				if ( ! hn4found )
7667323adacSDevin Teske					printf "%s\t\t%s\n", ip4, hn >> hosts
7677323adacSDevin Teske			}
7687323adacSDevin Teske		}
7697323adacSDevin Teske	' "$ETC_HOSTS" 2> /dev/null || return $FAILURE
7707323adacSDevin Teske
7717323adacSDevin Teske	f_dprintf "Wrote out %s" "$ETC_HOSTS"
7727323adacSDevin Teske	return $SUCCESS
7737323adacSDevin Teske}
7747323adacSDevin Teske
7757323adacSDevin Teske# f_dhcp_parse_leases $leasefile struct_name
7767323adacSDevin Teske#
7777323adacSDevin Teske# Parse $leasefile and store the information for the most recent lease in a
7787323adacSDevin Teske# struct (see struct.subr for additional details) named `struct_name'. See
7797323adacSDevin Teske# DHCP_LEASE struct definition in the GLOBALS section above.
7807323adacSDevin Teske#
7817323adacSDevin Teskef_dhcp_parse_leases()
7827323adacSDevin Teske{
7837323adacSDevin Teske	local leasefile="$1" struct_name="$2"
7847323adacSDevin Teske
7857323adacSDevin Teske	[ "$struct_name" ] || return $FAILURE
7867323adacSDevin Teske
7877323adacSDevin Teske	if [ ! -e "$leasefile" ]; then
7887323adacSDevin Teske		f_dprintf "%s: No such file or directory" "$leasefile"
7897323adacSDevin Teske		return $FAILURE
7907323adacSDevin Teske	fi
7917323adacSDevin Teske
7927323adacSDevin Teske	f_struct "$struct_name" && f_struct_free "$struct_name"
7937323adacSDevin Teske	f_struct_new DHCP_LEASE "$struct_name"
7947323adacSDevin Teske
7957323adacSDevin Teske	eval "$( awk -v struct="$struct_name" '
7967323adacSDevin Teske		BEGIN {
7977323adacSDevin Teske			lease_found = 0
7987323adacSDevin Teske			keyword_list = " \
7997323adacSDevin Teske				interface	\
8007323adacSDevin Teske				fixed-address	\
8017323adacSDevin Teske				filename	\
8027323adacSDevin Teske				server-name	\
8037323adacSDevin Teske				script		\
8047323adacSDevin Teske				medium		\
8057323adacSDevin Teske			"
8067323adacSDevin Teske			split(keyword_list, keywords, FS)
8077323adacSDevin Teske
8087323adacSDevin Teske			time_list = "renew rebind expire"
8097323adacSDevin Teske			split(time_list, times, FS)
8107323adacSDevin Teske
8117323adacSDevin Teske			option_list = " \
8127323adacSDevin Teske				host-name		\
8137323adacSDevin Teske				subnet-mask		\
8147323adacSDevin Teske				routers			\
8157323adacSDevin Teske				domain-name-servers	\
8167323adacSDevin Teske				domain-name		\
8177323adacSDevin Teske				broadcast-address	\
8187323adacSDevin Teske				dhcp-lease-time		\
8197323adacSDevin Teske				dhcp-message-type	\
8207323adacSDevin Teske				dhcp-server-identifier	\
8217323adacSDevin Teske				dhcp-renewal-time	\
8227323adacSDevin Teske				dhcp-rebinding-time	\
8237323adacSDevin Teske			"
8247323adacSDevin Teske			split(option_list, options, FS)
8257323adacSDevin Teske		}
8267323adacSDevin Teske		function set_value(prop,value)
8277323adacSDevin Teske		{
8287323adacSDevin Teske			lease_found = 1
8297323adacSDevin Teske			gsub(/[^[:alnum:]_]/, "_", prop)
8307323adacSDevin Teske			sub(/;$/, "", value)
8317323adacSDevin Teske			sub(/^"/, "", value)
8327323adacSDevin Teske			sub(/"$/, "", value)
8337323adacSDevin Teske			sub(/,.*/, "", value)
8347323adacSDevin Teske			printf "%s set %s \"%s\"\n", struct, prop, value
8357323adacSDevin Teske		}
8367323adacSDevin Teske		/^lease {$/, /^}$/ \
8377323adacSDevin Teske		{
8387323adacSDevin Teske			if ( $0 ~ /^lease {$/ ) next
8397323adacSDevin Teske			if ( $0 ~ /^}$/ ) exit
8407323adacSDevin Teske
8417323adacSDevin Teske			for (k in keywords)
8427323adacSDevin Teske			{
8437323adacSDevin Teske				keyword = keywords[k]
8447323adacSDevin Teske				if ( $1 == keyword )
8457323adacSDevin Teske				{
8467323adacSDevin Teske					set_value(keyword, $2)
8477323adacSDevin Teske					next
8487323adacSDevin Teske				}
8497323adacSDevin Teske			}
8507323adacSDevin Teske
8517323adacSDevin Teske			for (t in times)
8527323adacSDevin Teske			{
8537323adacSDevin Teske				time = times[t]
8547323adacSDevin Teske				if ( $1 == time )
8557323adacSDevin Teske				{
8567323adacSDevin Teske					set_value(time, $2 " " $3 " " $4)
8577323adacSDevin Teske					next
8587323adacSDevin Teske				}
8597323adacSDevin Teske			}
8607323adacSDevin Teske
8617323adacSDevin Teske			if ( $1 != "option" ) next
8627323adacSDevin Teske			for (o in options)
8637323adacSDevin Teske			{
8647323adacSDevin Teske				option = options[o]
8657323adacSDevin Teske				if ( $2 == option )
8667323adacSDevin Teske				{
8677323adacSDevin Teske					set_value(option, $3)
8687323adacSDevin Teske					next
8697323adacSDevin Teske				}
8707323adacSDevin Teske			}
8717323adacSDevin Teske		}
8727323adacSDevin Teske		EXIT {
8737323adacSDevin Teske			if ( ! lease_found )
8747323adacSDevin Teske			{
8757323adacSDevin Teske				printf "f_struct_free \"%s\"\n", struct
8767323adacSDevin Teske				print "return $FAILURE"
8777323adacSDevin Teske			}
8787323adacSDevin Teske		}
8797323adacSDevin Teske	' "$leasefile" )"
8807323adacSDevin Teske}
8817323adacSDevin Teske
8827323adacSDevin Teske# f_dhcp_get_info $interface
8837323adacSDevin Teske#
8847323adacSDevin Teske# Parse the dhclient(8) lease database for $interface to obtain all the
8857323adacSDevin Teske# necessary IPv4 details necessary to communicate on the network. The retrieved
8867323adacSDevin Teske# information is stored in VAR_IPADDR, VAR_NETMASK, VAR_GATEWAY, and
8877323adacSDevin Teske# VAR_NAMESERVER.
8887323adacSDevin Teske#
8897323adacSDevin Teske# If reading the lease database fails, values are obtained from ifconfig(8) and
8907323adacSDevin Teske# route(8). If the DHCP lease did not provide a nameserver (or likewise, we
8917323adacSDevin Teske# were unable to parse the lease database), fall-back to resolv.conf(5) for
8927323adacSDevin Teske# obtaining the nameserver. Always returns success.
8937323adacSDevin Teske#
8947323adacSDevin Teskef_dhcp_get_info()
8957323adacSDevin Teske{
8967323adacSDevin Teske	local interface="$1" cp
8977323adacSDevin Teske	local leasefile="/var/db/dhclient.leases.$interface"
8987323adacSDevin Teske
8997323adacSDevin Teske	# If it fails, do it the old-fashioned way
9007323adacSDevin Teske	if f_dhcp_parse_leases "$leasefile" lease; then
9017323adacSDevin Teske		lease get fixed_address $VAR_IPADDR
9027323adacSDevin Teske		lease get subnet_mask $VAR_NETMASK
9037323adacSDevin Teske		lease get routers cp
9047323adacSDevin Teske		setvar $VAR_GATEWAY "${cp%%,*}"
9057323adacSDevin Teske		lease get domain_name_servers cp
9067323adacSDevin Teske		setvar $VAR_NAMESERVER "${cp%%,*}"
9077323adacSDevin Teske		lease get host_name cp &&
9087323adacSDevin Teske			setvar $VAR_HOSTNAME "$cp"
9097323adacSDevin Teske		f_struct_free lease
9107323adacSDevin Teske	else
9117323adacSDevin Teske		# Bah, now we have to get the information from ifconfig
9127323adacSDevin Teske		if f_debugging; then
9137323adacSDevin Teske			f_dprintf "DHCP configured interface returns %s" \
9147323adacSDevin Teske			          "$( ifconfig "$interface" )"
9157323adacSDevin Teske		fi
9167323adacSDevin Teske		f_ifconfig_inet "$interface" $VAR_IPADDR
9177323adacSDevin Teske		f_ifconfig_netmask "$interface" $VAR_NETMASK
9187323adacSDevin Teske		f_route_get_default $VAR_GATEWAY
9197323adacSDevin Teske	fi
9207323adacSDevin Teske
9217323adacSDevin Teske	# If we didn't get a name server value, hunt for it in resolv.conf
9227323adacSDevin Teske	local ns
9237323adacSDevin Teske	if [ -r "$RESOLV_CONF" ] && ! {
9247323adacSDevin Teske		f_getvar $VAR_NAMESERVER ns || [ "$ns" ]
9257323adacSDevin Teske	}; then
9267323adacSDevin Teske		f_resolv_conf_nameservers cp &&
9277323adacSDevin Teske			setvar $VAR_NAMESERVER ${cp%%[$IFS]*}
9287323adacSDevin Teske	fi
9297323adacSDevin Teske
9307323adacSDevin Teske	return $SUCCESS
9317323adacSDevin Teske}
9327323adacSDevin Teske
9337323adacSDevin Teske# f_rtsol_get_info $interface
9347323adacSDevin Teske#
9357323adacSDevin Teske# Returns the rtsol-provided IPv6 address associated with $interface. The
9367323adacSDevin Teske# retrieved IP address is stored in VAR_IPV6ADDR. Always returns success.
9377323adacSDevin Teske#
9387323adacSDevin Teskef_rtsol_get_info()
9397323adacSDevin Teske{
9407323adacSDevin Teske	local interface="$1" cp
9417323adacSDevin Teske	cp=$( ifconfig "$interface" 2> /dev/null | awk \
9427323adacSDevin Teske	'
9437323adacSDevin Teske		BEGIN { found = 0 }
9447323adacSDevin Teske		( $1 == "inet6" ) && ( $2 ~ /^fe80:/ ) \
9457323adacSDevin Teske		{
9467323adacSDevin Teske			print $2
9477323adacSDevin Teske			found = 1
9487323adacSDevin Teske			exit
9497323adacSDevin Teske		}
9507323adacSDevin Teske		END { exit ! found }
9517323adacSDevin Teske	' ) && setvar $VAR_IPV6ADDR "$cp"
9527323adacSDevin Teske}
9537323adacSDevin Teske
9547323adacSDevin Teske# f_host_lookup $host [$var_to_set]
9557323adacSDevin Teske#
9567323adacSDevin Teske# Use host(1) to lookup (or reverse) an Internet number from (or to) a name.
9577323adacSDevin Teske# Multiple answers are returned separated by a single space. If host(1) does
9587323adacSDevin Teske# not exit cleanly, its full output is provided and the return status is 1.
9597323adacSDevin Teske#
9607323adacSDevin Teske# If nsswitch.conf(5) has been configured to query local access first for the
9617323adacSDevin Teske# `hosts' database, we'll manually check hosts(5) first (preventing host(1)
9627323adacSDevin Teske# from hanging in the event that DNS goes awry).
9637323adacSDevin Teske#
9647323adacSDevin Teske# If $var_to_set is missing or NULL, the list of IP addresses is printed to
9657323adacSDevin Teske# standard output for capturing in a sub-shell (which is less-recommended
9667323adacSDevin Teske# because of performance degredation; for example, when called in a loop).
9677323adacSDevin Teske#
9687323adacSDevin Teske# The variables from variable.subr used in looking up the host are as follows
9697323adacSDevin Teske# (which are set manually):
9707323adacSDevin Teske#
9717323adacSDevin Teske# 	VAR_IPV6_ENABLE [Optional]
9727323adacSDevin Teske# 		If set to "YES", enables the lookup of IPv6 addresses and IPv4
9737323adacSDevin Teske# 		address. IPv6 addresses, if any, will come before IPv4. Note
9747323adacSDevin Teske# 		that if nsswitch.conf(5) shows an affinity for "files" for the
9757323adacSDevin Teske# 		"host" database and there is a valid entry in hosts(5) for
9767323adacSDevin Teske# 		$host, this setting currently has no effect (an IPv4 address
9777323adacSDevin Teske# 		can supersede an IPv6 address). By design, hosts(5) overrides
9787323adacSDevin Teske# 		any preferential treatment. Otherwise, if this variable is not
9797323adacSDevin Teske# 		set, IPv6 addresses will not be used (IPv4 addresses will
9807323adacSDevin Teske# 		specifically be requested from DNS).
9817323adacSDevin Teske#
9827323adacSDevin Teske# This function is a two-parter. Below is the awk(1) portion of the function,
9837323adacSDevin Teske# afterward is the sh(1) function which utilizes the below awk script.
9847323adacSDevin Teske#
9857323adacSDevin Teskef_host_lookup_awk='
9867323adacSDevin TeskeBEGIN{ addrs = "" }
9877323adacSDevin Teske!/^[[:space:]]*(#|$)/ \
9887323adacSDevin Teske{
9897323adacSDevin Teske	for (n=1; n++ < NF;) if ($n == name)
9907323adacSDevin Teske		addrs = addrs (addrs ? " " : "") $1
9917323adacSDevin Teske}
9927323adacSDevin TeskeEND {
9937323adacSDevin Teske	if (addrs) print addrs
9947323adacSDevin Teske	exit !addrs
9957323adacSDevin Teske}
9967323adacSDevin Teske'
9977323adacSDevin Teskef_host_lookup()
9987323adacSDevin Teske{
9997323adacSDevin Teske	local __host="$1" __var_to_set="$2"
10007323adacSDevin Teske	f_dprintf "f_host_lookup: host=[%s]" "$__host"
10017323adacSDevin Teske
10027323adacSDevin Teske	# If we're configured to look at local files first, do that
10037323adacSDevin Teske	if awk '/^hosts:/{exit !($2=="files")}' "$NSSWITCH_CONF"; then
10047323adacSDevin Teske		if [ "$__var_to_set" ]; then
10057323adacSDevin Teske			local __cp
10067323adacSDevin Teske			if __cp=$( awk -v name="$__host" \
10077323adacSDevin Teske				"$f_host_lookup_awk" "$ETC_HOSTS" )
10087323adacSDevin Teske			then
10097323adacSDevin Teske				setvar "$__var_to_set" "$__cp"
10107323adacSDevin Teske				return $SUCCESS
10117323adacSDevin Teske			fi
10127323adacSDevin Teske		else
10137323adacSDevin Teske			awk -v name="$__host" \
10147323adacSDevin Teske				"$f_host_lookup_awk" "$ETC_HOSTS" &&
10157323adacSDevin Teske				return $SUCCESS
10167323adacSDevin Teske		fi
10177323adacSDevin Teske	fi
10187323adacSDevin Teske
10197323adacSDevin Teske	#
10207323adacSDevin Teske	# Fall back to host(1) -- which is further governed by nsswitch.conf(5)
10217323adacSDevin Teske	#
10227323adacSDevin Teske
10237323adacSDevin Teske	local __output __ip6 __addrs="" __wait=""
10247323adacSDevin Teske	f_getvar $VAR_MEDIA_TIMEOUT __wait
10257323adacSDevin Teske	[ "$__wait" ] && __wait="-W $(( $__wait / 2 ))"
10267323adacSDevin Teske	f_getvar $VAR_IPV6_ENABLE __ip6
10277323adacSDevin Teske	if [ "$__ip6" = "YES" ]; then
10287323adacSDevin Teske		if ! __output=$( host -t AAAA $__wait -- "$__host" 2>&1 ); then
10297323adacSDevin Teske			# An error occurred, display in-full and return error
10307323adacSDevin Teske			[ "$__var_to_set" ] &&
10317323adacSDevin Teske				setvar "$__var_to_set" "$__output"
10327323adacSDevin Teske			return $FAILURE
10337323adacSDevin Teske		fi
10347323adacSDevin Teske		__addrs=$( echo "$__output" | awk '/ address /{print $NF}' )
10357323adacSDevin Teske	fi
10367323adacSDevin Teske	if ! __output=$( host -t A $__wait -- "$__host" 2>&1 ); then
10377323adacSDevin Teske		# An error occurred, display it in-full and return error
10387323adacSDevin Teske		[ "$__var_to_set" ] && setvar "$__var_to_set" "$__output"
10397323adacSDevin Teske		return $FAILURE
10407323adacSDevin Teske	fi
10417323adacSDevin Teske	__addrs="$__addrs${__addrs:+ }$(
10427323adacSDevin Teske		echo "$__output" | awk '/ address /{print $NF}' )"
10437323adacSDevin Teske	if [ "$__var_to_set" ]; then
10447323adacSDevin Teske		setvar "$__var_to_set" "$__addrs"
10457323adacSDevin Teske	else
10467323adacSDevin Teske		echo $__addrs
10477323adacSDevin Teske	fi
10487323adacSDevin Teske}
10497323adacSDevin Teske
10507323adacSDevin Teske# f_device_dialog_tcp $device
10517323adacSDevin Teske#
10527323adacSDevin Teske# This is it - how to get TCP setup values. Prompt the user to edit/confirm the
10537323adacSDevin Teske# interface, gateway, nameserver, and hostname settings -- all required for
10547323adacSDevin Teske# general TCP/IP access.
10557323adacSDevin Teske#
10567323adacSDevin Teske# Variables from variable.subr that can be used to sript user input:
10577323adacSDevin Teske#
10587323adacSDevin Teske# 	VAR_NO_INET6
10597323adacSDevin Teske# 		If set, prevents asking the user if they would like to use
10607323adacSDevin Teske# 		rtsol(8) to check for an IPv6 router.
10617323adacSDevin Teske# 	VAR_TRY_RTSOL
10627323adacSDevin Teske# 		If set to "YES" (and VAR_NONINTERACTIVE is unset), asks the
10637323adacSDevin Teske# 		user if they would like to try the IPv6 RouTer SOLicitation
10647323adacSDevin Teske# 		utility (rtsol(8)) to get IPv6 information. Ignored if
10657323adacSDevin Teske# 		VAR_NO_INET6 is set.
10667323adacSDevin Teske# 	VAR_TRY_DHCP
10677323adacSDevin Teske# 		If set to "YES" (and VAR_NONINTERACTIVE is unset), asks the
10687323adacSDevin Teske# 		user if they would like to try to acquire IPv4 connection
10697323adacSDevin Teske# 		settings from a DHCP server using dhclient(8).
10707323adacSDevin Teske#
10717323adacSDevin Teske# 	VAR_GATEWAY	Default gateway to use.
10727323adacSDevin Teske# 	VAR_IPADDR	Interface address to assign.
10737323adacSDevin Teske# 	VAR_NETMASK	Interface subnet mask.
10747323adacSDevin Teske# 	VAR_EXTRAS	Extra interface options to ifconfig(8).
10757323adacSDevin Teske# 	VAR_HOSTNAME	Hostname to set.
10767323adacSDevin Teske# 	VAR_DOMAINNAME	Domain name to use.
10777323adacSDevin Teske# 	VAR_NAMESERVER	DNS nameserver to use when making lookups.
10787323adacSDevin Teske# 	VAR_IPV6ADDR	IPv6 interface address.
10797323adacSDevin Teske#
10807323adacSDevin Teske# In addition, the following variables are used in acquiring network settings
10817323adacSDevin Teske# from the user:
10827323adacSDevin Teske#
10837323adacSDevin Teske# 	VAR_NONINTERACTIVE
10847323adacSDevin Teske# 		If set (such as when running in a script), prevents asking the
10857323adacSDevin Teske# 		user questions or displaying the usual prompts, etc.
10867323adacSDevin Teske# 	VAR_NETINTERACTIVE
10877323adacSDevin Teske# 		The one exception to VAR_NONINTERACTIVE is VAR_NETINTERACTIVE,
10887323adacSDevin Teske# 		which if set will prompt the user to try RTSOL (unless
10897323adacSDevin Teske# 		VAR_TRY_RTSOL has been set), try DHCP (unless VAR_TRY_DHCP has
10907323adacSDevin Teske# 		been set), and display the network verification dialog. This
10917323adacSDevin Teske# 		allows you to have a mostly non-interactive script that still
10927323adacSDevin Teske# 		prompts for network setup/confirmation.
10937323adacSDevin Teske#
10947323adacSDevin Teske# After successfull execution, the following variables are set:
10957323adacSDevin Teske#
10967323adacSDevin Teske# 	VAR_IFCONFIG + $device (e.g., `ifconfig_em0')
10977323adacSDevin Teske#               Defines the ifconfig(8) properties specific to $device.
10987323adacSDevin Teske#
10997323adacSDevin Teskef_device_dialog_tcp()
11007323adacSDevin Teske{
11017323adacSDevin Teske	local dev="$1" cp n
11027323adacSDevin Teske	local use_dhcp="" use_rtsol=""
11037323adacSDevin Teske	local _ipaddr _netmask _extras
11047323adacSDevin Teske
11057323adacSDevin Teske	[ "$dev" ] || return $FAILURE
11067323adacSDevin Teske
11077323adacSDevin Teske	# Initialize vars from previous device values
11087323adacSDevin Teske	local private
11097323adacSDevin Teske	device_$dev get private private
11107323adacSDevin Teske	if [ "$private" ] && f_struct "$private"; then
11117323adacSDevin Teske		$private get ipaddr    _ipaddr
11127323adacSDevin Teske		$private get netmask   _netmask
11137323adacSDevin Teske		$private get extras    _extras
11147323adacSDevin Teske		$private get use_dhcp  use_dhcp
11157323adacSDevin Teske		$private get use_rtsol use_rtsol
11167323adacSDevin Teske	else # See if there are any defaults
11177323adacSDevin Teske
11187323adacSDevin Teske		#
11197323adacSDevin Teske		# This is a hack so that the dialogs below are interactive in a
11207323adacSDevin Teske		# script if we have requested interactive behavior.
11217323adacSDevin Teske		#
11227323adacSDevin Teske		local old_interactive=
11237323adacSDevin Teske		if ! f_interactive && f_netinteractive; then
11247323adacSDevin Teske			f_getvar $VAR_NONINTERACTIVE old_interactive
11257323adacSDevin Teske			unset $VAR_NONINTERACTIVE
11267323adacSDevin Teske		fi
11277323adacSDevin Teske
11287323adacSDevin Teske
11297323adacSDevin Teske		#
11307323adacSDevin Teske		# Try a RTSOL scan if such behavior is desired.
11317323adacSDevin Teske		# If the variable was configured and is YES, do it.
11327323adacSDevin Teske		# If it was configured to anything else, treat it as NO.
11337323adacSDevin Teske		# Otherwise, ask the question interactively.
11347323adacSDevin Teske		#
11357323adacSDevin Teske		local try6
113680433743SDevin Teske		if ! f_isset $VAR_NO_INET6 && {
11377323adacSDevin Teske		   { f_getvar $VAR_TRY_RTSOL try6 && [ "$try6" = "YES" ]; } ||
11387323adacSDevin Teske		   {
113980433743SDevin Teske			# Only prompt the user when VAR_TRY_RTSOL is unset
114080433743SDevin Teske			! f_isset $VAR_TRY_RTSOL &&
11417323adacSDevin Teske				f_dialog_noyes "$msg_try_ipv6_configuration"
11427323adacSDevin Teske		   }
11437323adacSDevin Teske		}; then
11447323adacSDevin Teske			local i
11457323adacSDevin Teske
11467323adacSDevin Teske			f_quietly sysctl net.inet6.ip6.forwarding=0
11477323adacSDevin Teske			f_quietly sysctl net.inet6.ip6.accept_rtadv=1
11487323adacSDevin Teske			f_quietly ifconfig $dev up
11497323adacSDevin Teske
11507323adacSDevin Teske			i=$( sysctl -n net.inet6.ip6.dad_count )
11517323adacSDevin Teske			sleep $(( $i + 1 ))
11527323adacSDevin Teske
11537323adacSDevin Teske			f_quietly mkdir -p /var/run
11547323adacSDevin Teske			f_dialog_info "$msg_scanning_for_ra_servers"
11557323adacSDevin Teske			if f_quietly rtsol $dev; then
11567323adacSDevin Teske				i=$( sysctl -n net.inet6.ip6.dad_count )
11577323adacSDevin Teske				sleep $(( $i + 1 ))
11587323adacSDevin Teske				f_rtsol_get_info $dev
11597323adacSDevin Teske				use_rtsol=1
11607323adacSDevin Teske			else
11617323adacSDevin Teske				use_rtsol=
11627323adacSDevin Teske			fi
11637323adacSDevin Teske		fi
11647323adacSDevin Teske
11657323adacSDevin Teske		#
11667323adacSDevin Teske		# Try a DHCP scan if such behavior is desired.
11677323adacSDevin Teske		# If the variable was configured and is YES, do it.
11687323adacSDevin Teske		# If it was configured to anything else, treat it as NO.
11697323adacSDevin Teske		# Otherwise, ask the question interactively.
11707323adacSDevin Teske		#
11717323adacSDevin Teske		local try4
11727323adacSDevin Teske		if { f_getvar $VAR_TRY_DHCP try4 && [ "$try4" = "YES" ]; } || {
117380433743SDevin Teske			# Only prompt the user when VAR_TRY_DHCP is unset
117480433743SDevin Teske			! f_isset $VAR_TRY_DHCP &&
11757323adacSDevin Teske				f_dialog_noyes "$msg_try_dhcp_configuration"
11767323adacSDevin Teske		}; then
11777323adacSDevin Teske			f_quietly ifconfig $dev delete
11787323adacSDevin Teske			f_quietly mkdir -p /var/db
11797323adacSDevin Teske			f_quietly mkdir -p /var/run
11807323adacSDevin Teske			f_quietly mkdir -p /tmp
11817323adacSDevin Teske
11827323adacSDevin Teske			local msg="$msg_scanning_for_dhcp_servers"
11837323adacSDevin Teske			trap - SIGINT
11847323adacSDevin Teske			( # Execute in sub-shell to allow/catch Ctrl-C
11857323adacSDevin Teske			  trap 'exit $FAILURE' SIGINT
11867323adacSDevin Teske			  if [ "$USE_XDIALOG" ]; then
11877323adacSDevin Teske			  	f_quietly dhclient $dev |
11887323adacSDevin Teske			  			f_xdialog_info "$msg"
11897323adacSDevin Teske			  else
11907323adacSDevin Teske			  	f_dialog_info "$msg"
11917323adacSDevin Teske			  	f_quietly dhclient $dev
11927323adacSDevin Teske			  fi
11937323adacSDevin Teske			)
11947323adacSDevin Teske			local retval=$?
11957323adacSDevin Teske			trap 'f_interrupt' SIGINT
11967323adacSDevin Teske			if [ $retval -eq $SUCCESS ]; then
11977323adacSDevin Teske				f_dhcp_get_info $dev
11987323adacSDevin Teske				use_dhcp=1
11997323adacSDevin Teske			else
12007323adacSDevin Teske				use_dhcp=
12017323adacSDevin Teske			fi
12027323adacSDevin Teske		fi
12037323adacSDevin Teske
12047323adacSDevin Teske		# Restore old VAR_NONINTERACTIVE if needed.
12057323adacSDevin Teske		[ "$old_interactive" ] &&
12067323adacSDevin Teske			setvar $VAR_NONINTERACTIVE "$old_interactive"
12077323adacSDevin Teske
12087323adacSDevin Teske		# Special hack so it doesn't show up oddly in the menu
12097323adacSDevin Teske		local gw
12107323adacSDevin Teske		if f_getvar $VAR_GATEWAY gw && [ "$gw" = "NO" ]; then
12117323adacSDevin Teske			setvar $VAR_GATEWAY ""
12127323adacSDevin Teske		fi
12137323adacSDevin Teske
12147323adacSDevin Teske		# Get old IP address from variable space, if available
12157323adacSDevin Teske		if [ ! "$_ipaddr" ]; then
12167323adacSDevin Teske			if f_getvar $VAR_IPADDR cp; then
12177323adacSDevin Teske				_ipaddr="$cp"
12187323adacSDevin Teske			elif f_getvar ${dev}_$VAR_IPADDR cp; then
12197323adacSDevin Teske				_ipaddr="$cp"
12207323adacSDevin Teske			fi
12217323adacSDevin Teske		fi
12227323adacSDevin Teske
12237323adacSDevin Teske		# Get old netmask from variable space, if available
12247323adacSDevin Teske		if [ ! "$_netmask" ]; then
12257323adacSDevin Teske			if f_getvar $VAR_NETMASK cp; then
12267323adacSDevin Teske				_netmask="$cp"
12277323adacSDevin Teske			elif f_getvar ${dev}_$VAR_NETMASK cp; then
12287323adacSDevin Teske				_netmask="$cp"
12297323adacSDevin Teske			fi
12307323adacSDevin Teske		fi
12317323adacSDevin Teske
12327323adacSDevin Teske		# Get old extras string from variable space, if available
12337323adacSDevin Teske		if [ ! "$_extras" ]; then
12347323adacSDevin Teske			if f_getvar $VAR_EXTRAS cp; then
12357323adacSDevin Teske				_extras="$cp"
12367323adacSDevin Teske			elif f_getvar ${dev}_$VAR_EXTRAS cp; then
12377323adacSDevin Teske				_extras="$cp"
12387323adacSDevin Teske			fi
12397323adacSDevin Teske		fi
12407323adacSDevin Teske	fi
12417323adacSDevin Teske
12427323adacSDevin Teske	# Look up values already recorded with the system, or blank the string
12437323adacSDevin Teske	# variables ready to accept some new data
12447323adacSDevin Teske	local _hostname _gateway _nameserver
12457323adacSDevin Teske	f_getvar $VAR_HOSTNAME _hostname
12467323adacSDevin Teske	case "$_hostname" in
12477323adacSDevin Teske	*.*) : do nothing ;; # Already fully-qualified
12487323adacSDevin Teske	*)
12497323adacSDevin Teske		f_getvar $VAR_DOMAINNAME cp
12507323adacSDevin Teske		[ "$cp" ] && _hostname="$_hostname.$cp"
12517323adacSDevin Teske	esac
12527323adacSDevin Teske	f_getvar $VAR_GATEWAY _gateway
12537323adacSDevin Teske	f_getvar $VAR_NAMESERVER _nameserver
12547323adacSDevin Teske
12557323adacSDevin Teske	# Re-check variables for initial inheritance before heading into dialog
12567323adacSDevin Teske	[ "$_hostname" ] || _hostname="${HOSTNAME:-$( hostname )}"
12577323adacSDevin Teske	[ "$_gateway" ] || f_route_get_default _gateway
12587323adacSDevin Teske	[ ! "$_nameserver" ] &&
12597323adacSDevin Teske		f_resolv_conf_nameservers cp && _nameserver=${cp%%[$IFS]*}
12607323adacSDevin Teske	[ "$_ipaddr" ] || f_ifconfig_inet $dev _ipaddr
12617323adacSDevin Teske	[ "$_netmask" ] || f_ifconfig_netmask $dev _netmask
12627323adacSDevin Teske
12637323adacSDevin Teske	# If non-interactive, jump over dialog section and into config section
12647323adacSDevin Teske	if f_netinteractive || f_interactive || [ ! "$_hostname" ]
12657323adacSDevin Teske	then
12667323adacSDevin Teske		[ ! "$_hostname" ] && f_interactive &&
12677323adacSDevin Teske			f_dialog_msgbox "$msg_hostname_variable_not_set"
12687323adacSDevin Teske
12697323adacSDevin Teske		local title=" $msg_network_configuration "
12707323adacSDevin Teske		local hline="$hline_alnum_arrows_punc_tab_enter"
12717323adacSDevin Teske		local extras_help="$tcplayout_extras_help"
12727323adacSDevin Teske
12737323adacSDevin Teske		# Modify the help line for PLIP config
12747323adacSDevin Teske		[ "${dev#plip}" != "$dev" ] &&
12757323adacSDevin Teske			extras_help="$tcplayout_extras_help_for_plip"
12767323adacSDevin Teske
12777323adacSDevin Teske		f_getvar $VAR_IPV6ADDR cp && [ "$cp" ] &&
12787323adacSDevin Teske			title="$title($msg_ipv6_ready) "
12797323adacSDevin Teske
12807323adacSDevin Teske		if [ ! "$USE_XDIALOG" ]; then
12817323adacSDevin Teske			local prompt="$msg_dialog_mixedform_navigation_help"
12827323adacSDevin Teske			# Calculate center position for displaying device label
12837323adacSDevin Teske			local devlabel="$msg_configuration_for_interface $dev"
12847323adacSDevin Teske			local width=54
12857323adacSDevin Teske			local n=$(( $width/2 - (${#devlabel} + 4)/2 - 2 ))
12867323adacSDevin Teske
12877323adacSDevin Teske			while :; do
12887323adacSDevin Teske				cp=$( $DIALOG \
12897323adacSDevin Teske					--title "$title"                     \
12907323adacSDevin Teske					--backtitle "$DIALOG_BACKTITLE"      \
12917323adacSDevin Teske					--hline "$hline"                     \
12927323adacSDevin Teske					--item-help                          \
12937323adacSDevin Teske					--ok-label "$msg_ok"                 \
12947323adacSDevin Teske					--cancel-label "$msg_cancel"         \
12957323adacSDevin Teske					--help-button                        \
12967323adacSDevin Teske					--help-label "$msg_help"             \
12977323adacSDevin Teske					--mixedform "$prompt" 16 $width 9    \
12987323adacSDevin Teske					"$msg_host_name_including_domain:" 1 2 \
12997323adacSDevin Teske						"$_hostname" 2 3 45 255 0    \
13007323adacSDevin Teske						"$tcplayout_hostname_help"   \
13017323adacSDevin Teske					"$msg_ipv4_gateway:" 3 2             \
13027323adacSDevin Teske						"$_gateway" 4 3 16 15 0      \
13037323adacSDevin Teske						"$tcplayout_gateway_help"    \
13047323adacSDevin Teske					"$msg_name_server:" 3 31             \
13057323adacSDevin Teske						"$_nameserver" 4 32 16 15 0  \
13067323adacSDevin Teske						"$tcplayout_nameserver_help" \
13077323adacSDevin Teske					"- $devlabel -" 5 $n "" 0 0 0 0 3 "" \
13087323adacSDevin Teske					"$msg_ipv4_address:" 6 6             \
13097323adacSDevin Teske						"$_ipaddr" 7 7 16 15 0       \
13107323adacSDevin Teske						"$tcplayout_ipaddr_help"     \
13117323adacSDevin Teske					"$msg_netmask:" 6 31                 \
13127323adacSDevin Teske						"$_netmask" 7 32 16 15 0     \
13137323adacSDevin Teske						"$tcplayout_netmask_help"    \
13147323adacSDevin Teske					"$msg_extra_options_to_ifconfig" 8 6 \
13157323adacSDevin Teske						"$_extras" 9 7 41 2048 0     \
13167323adacSDevin Teske						"$extras_help"               \
13177323adacSDevin Teske					2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD )
13187323adacSDevin Teske
13197323adacSDevin Teske				# --mixed-form always returns 0, we have to
13207323adacSDevin Teske				# use the returned data to determine button
13217323adacSDevin Teske				if [ ! "$cp" ]; then
13227323adacSDevin Teske					# User either chose "Cancel", pressed
13237323adacSDevin Teske					# ESC, or blanked every form field
13247323adacSDevin Teske					return $FAILURE
13257323adacSDevin Teske				else
13267323adacSDevin Teske					n=$( echo "$cp" | f_number_of_lines )
13277323adacSDevin Teske					[ $n -eq 1 ] && case "$cp" in HELP*)
13287323adacSDevin Teske						# User chose "Help"
13297323adacSDevin Teske						f_show_help "$TCP_HELPFILE"
13307323adacSDevin Teske						continue
13317323adacSDevin Teske					esac
13327323adacSDevin Teske				fi
13337323adacSDevin Teske
13347323adacSDevin Teske				# Turn mixed-form results into env variables
13357323adacSDevin Teske				eval "$( echo "$cp" | awk '
13367323adacSDevin Teske				BEGIN {
13377323adacSDevin Teske					n = 0
13387323adacSDevin Teske					field[++n] = "_hostname"
13397323adacSDevin Teske					field[++n] = "_gateway"
13407323adacSDevin Teske					field[++n] = "_nameserver"
13417323adacSDevin Teske					field[++n] = "_ipaddr"
13427323adacSDevin Teske					field[++n] = "_netmask"
13437323adacSDevin Teske					field[++n] = "_extras"
13447323adacSDevin Teske					nfields = n
13457323adacSDevin Teske					n = 0
13467323adacSDevin Teske				}
13477323adacSDevin Teske				{
13487323adacSDevin Teske					gsub(/'\''/, "'\'\\\\\'\''")
13497323adacSDevin Teske					sub(/[[:space:]]*$/, "")
13507323adacSDevin Teske					value[field[++n]] = $0
13517323adacSDevin Teske				}
13527323adacSDevin Teske				END {
13537323adacSDevin Teske					for ( n = 1; n <= nfields; n++ )
13547323adacSDevin Teske					{
13557323adacSDevin Teske						printf "%s='\''%s'\'';\n",
13567323adacSDevin Teske						       field[n],
13577323adacSDevin Teske						       value[field[n]]
13587323adacSDevin Teske					}
13597323adacSDevin Teske				}' )"
13607323adacSDevin Teske
13617323adacSDevin Teske				f_dialog_validate_tcpip \
13627323adacSDevin Teske					"$_hostname" \
13637323adacSDevin Teske					"$_gateway" \
13647323adacSDevin Teske					"$_nameserver" \
13657323adacSDevin Teske					"$_ipaddr" \
13667323adacSDevin Teske					"$_netmask" \
13677323adacSDevin Teske					&& break
13687323adacSDevin Teske			done
13697323adacSDevin Teske		else
13707323adacSDevin Teske			# Xdialog(1) does not support --mixed-form
13717323adacSDevin Teske			# Create a persistent menu instead
13727323adacSDevin Teske
13737323adacSDevin Teske			f_dialog_title "$msg_network_configuration"
137451c41087SDevin Teske			local prompt=
13757323adacSDevin Teske
13767323adacSDevin Teske			while :; do
13777323adacSDevin Teske				cp=$( $DIALOG \
13787323adacSDevin Teske					--title "$DIALOG_TITLE"               \
13797323adacSDevin Teske					--backtitle "$DIALOG_BACKTITLE"       \
13807323adacSDevin Teske					--hline "$hline"                      \
13817323adacSDevin Teske					--item-help                           \
13827323adacSDevin Teske					--ok-label "$msg_ok"                  \
13837323adacSDevin Teske					--cancel-label "$msg_cancel"          \
13847323adacSDevin Teske					--help ""                             \
13857323adacSDevin Teske					--menu "$prompt" 21 60 8              \
13867323adacSDevin Teske					"$msg_accept_continue" ""             \
13877323adacSDevin Teske						"$tcplayout_accept_cont_help" \
13887323adacSDevin Teske					"$msg_host_name_including_domain:"    \
13897323adacSDevin Teske						"$_hostname"                  \
13907323adacSDevin Teske						"$tcplayout_hostname_help"    \
13917323adacSDevin Teske					"$msg_ipv4_gateway:" "$_gateway"      \
13927323adacSDevin Teske						"$tcplayout_gateway_help"     \
13937323adacSDevin Teske					"$msg_name_server:" "$_nameserver"    \
13947323adacSDevin Teske						"$tcplayout_nameserver_help"  \
13957323adacSDevin Teske					"$msg_ipv4_address:" "$_ipaddr"       \
13967323adacSDevin Teske						"$tcplayout_ipaddr_help"      \
13977323adacSDevin Teske					"$msg_netmask:" "$_netmask"           \
13987323adacSDevin Teske						"$tcplayout_netmask_help"     \
13997323adacSDevin Teske					"$msg_extra_options_to_ifconfig"      \
14007323adacSDevin Teske						"$_extras" "$extras_help"     \
14017323adacSDevin Teske					2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
14027323adacSDevin Teske				)
14037323adacSDevin Teske				local retval=$?
1404fd962ac6SDevin Teske				f_dialog_data_sanitize cp
14057323adacSDevin Teske				f_dprintf "retval=%u mtag=[%s]" $retval "$cp"
14067323adacSDevin Teske
14077323adacSDevin Teske				if [ $retval -eq 2 ]; then
14087323adacSDevin Teske					# The Help button was pressed
14097323adacSDevin Teske					f_show_help "$TCP_HELPFILE"
14107323adacSDevin Teske					continue
14117323adacSDevin Teske				elif [ $retval -ne 0 ]; then
14127323adacSDevin Teske					# User chose "Cancel" or pressed ESC
14137323adacSDevin Teske					f_dialog_title_restore
14147323adacSDevin Teske					return $FAILURE
14157323adacSDevin Teske				fi
14167323adacSDevin Teske
14177323adacSDevin Teske				case "$cp" in
14187323adacSDevin Teske				"$msg_accept_continue")
14197323adacSDevin Teske					f_dialog_validate_tcpip \
14207323adacSDevin Teske						"$_hostname" \
14217323adacSDevin Teske						"$_gateway" \
14227323adacSDevin Teske						"$_nameserver" \
14237323adacSDevin Teske						"$_ipaddr" \
14247323adacSDevin Teske						"$_netmask" \
14257323adacSDevin Teske						&& break ;;
14267323adacSDevin Teske				"$msg_host_name_including_domain:")
1427ec7120b5SDevin Teske					f_dialog_input cp "$cp" "$_hostname" \
1428ec7120b5SDevin Teske						&& _hostname="$cp" ;;
14297323adacSDevin Teske				"$msg_ipv4_gateway:")
1430ec7120b5SDevin Teske					f_dialog_input cp "$cp" "$_gateway" \
1431ec7120b5SDevin Teske						&& _gateway="$cp" ;;
14327323adacSDevin Teske				"$msg_name_server:")
1433ec7120b5SDevin Teske					f_dialog_input cp "$cp" "$_nameserver" \
1434ec7120b5SDevin Teske						&& _nameserver="$cp" ;;
14357323adacSDevin Teske				"$msg_ipv4_address:")
1436ec7120b5SDevin Teske					f_dialog_input cp "$cp" "$_ipaddr" \
1437ec7120b5SDevin Teske						&& _ipaddr="$cp" ;;
14387323adacSDevin Teske				"$msg_netmask:")
1439ec7120b5SDevin Teske					f_dialog_input cp "$cp" "$_netmask" \
1440ec7120b5SDevin Teske						&& _netmask="$cp" ;;
14417323adacSDevin Teske				"$msg_extra_options_to_ifconfig")
1442ec7120b5SDevin Teske					f_dialog_input cp "$cp" "$_extras" \
1443ec7120b5SDevin Teske						&& _extras="$cp" ;;
14447323adacSDevin Teske				esac
14457323adacSDevin Teske			done
14467323adacSDevin Teske
14477323adacSDevin Teske			f_dialog_title_restore
14487323adacSDevin Teske
14497323adacSDevin Teske		fi # XDIALOG
14507323adacSDevin Teske
14517323adacSDevin Teske	fi # interactive
14527323adacSDevin Teske
14537323adacSDevin Teske	# We actually need to inform the rest of bsdconfig about this
14547323adacSDevin Teske	# data now if the user hasn't selected cancel.
14557323adacSDevin Teske
14567323adacSDevin Teske	if [ "$_hostname" ]; then
14577323adacSDevin Teske		setvar $VAR_HOSTNAME "$_hostname"
14587323adacSDevin Teske		f_quietly hostname "$_hostname"
14597323adacSDevin Teske		case "$_hostname" in
14607323adacSDevin Teske		*.*) setvar $VAR_DOMAINNAME "${_hostname#*.}" ;;
14617323adacSDevin Teske		esac
14627323adacSDevin Teske	fi
14637323adacSDevin Teske	[ "$_gateway"    ] && setvar $VAR_GATEWAY    "$_gateway"
14647323adacSDevin Teske	[ "$_nameserver" ] && setvar $VAR_NAMESERVER "$_nameserver"
14657323adacSDevin Teske	[ "$_ipaddr"     ] && setvar $VAR_IPADDR     "$_ipaddr"
14667323adacSDevin Teske	[ "$_netmask"    ] && setvar $VAR_NETMASK    "$_netmask"
14677323adacSDevin Teske	[ "$_extras"     ] && setvar $VAR_EXTRAS     "$_extras"
14687323adacSDevin Teske
14697323adacSDevin Teske	f_dprintf "Creating struct DEVICE_INFO devinfo_%s" "$dev"
14707323adacSDevin Teske	f_struct_new DEVICE_INFO devinfo_$dev
14717323adacSDevin Teske	device_$dev set private devinfo_$dev
14727323adacSDevin Teske
14737323adacSDevin Teske	devinfo_$dev set ipaddr    $_ipaddr
14747323adacSDevin Teske	devinfo_$dev set netmask   $_netmask
14757323adacSDevin Teske	devinfo_$dev set extras    $_extras
14767323adacSDevin Teske	devinfo_$dev set use_rtsol $use_rtsol
14777323adacSDevin Teske	devinfo_$dev set use_dhcp  $use_dhcp
14787323adacSDevin Teske
14797323adacSDevin Teske	if [ "$use_dhcp" -o "$_ipaddr" ]; then
14807323adacSDevin Teske		if [ "$use_dhcp" ]; then
14817323adacSDevin Teske			cp="DHCP${extras:+ $extras}"
14827323adacSDevin Teske		else
14837323adacSDevin Teske			cp="inet $_ipaddr netmask $_netmask${extras:+ $extras}"
14847323adacSDevin Teske		fi
14857323adacSDevin Teske		setvar $VAR_IFCONFIG$dev "$cp"
14867323adacSDevin Teske	fi
14877323adacSDevin Teske	[ "$use_rtsol" ] &&
14887323adacSDevin Teske		setvar $VAR_IPV6_ENABLE "YES"
14897323adacSDevin Teske
14907323adacSDevin Teske	[ "$use_dhcp" ] ||
14917323adacSDevin Teske		f_config_resolv # XXX this will do it on the MFS copy
14927323adacSDevin Teske
14937323adacSDevin Teske	return $SUCCESS
14947323adacSDevin Teske}
14957323adacSDevin Teske
14967323adacSDevin Teske# f_device_scan_tcp [$var_to_set]
14977323adacSDevin Teske#
14987323adacSDevin Teske# Scan for the first active/configured TCP/IP device. The name of the interface
14997323adacSDevin Teske# is printed to stderr like other dialog(1)-based functions (stdout is reserved
15007323adacSDevin Teske# for dialog(1) interaction) if $var_to_set is missing or NULL. Returns failure
15017323adacSDevin Teske# if no active/configured interface
15027323adacSDevin Teske#
15037323adacSDevin Teskef_device_scan_tcp()
15047323adacSDevin Teske{
15057323adacSDevin Teske	local __var_to_set="$1" __iface
15067323adacSDevin Teske	for __iface in $( ifconfig -l ); do
15077323adacSDevin Teske		if ifconfig $__iface | awk '
15087323adacSDevin Teske		BEGIN {
15097323adacSDevin Teske			has_inet = has_inet6 = is_ethernet = 0
15107323adacSDevin Teske			is_usable = 1
15117323adacSDevin Teske		}
15127323adacSDevin Teske		( $1 == "status:" && $2 != "active" ) { is_usable = 0; exit }
15137323adacSDevin Teske		( $1 == "inet" ) {
15147323adacSDevin Teske			if ($2 == "0.0.0.0") { is_usable = 0; exit }
15157323adacSDevin Teske			has_inet++
15167323adacSDevin Teske		}
15177323adacSDevin Teske		( $1 == "inet6") { has_inet6++ }
15187323adacSDevin Teske		( $1 == "media:" ) {
15197323adacSDevin Teske			if ($2 != "Ethernet") { is_usable = 0; exit }
15207323adacSDevin Teske			is_ethernet = 1
15217323adacSDevin Teske		}
15227323adacSDevin Teske		END {
15237323adacSDevin Teske			if (!(is_ethernet && (has_inet || has_inet6)))
15247323adacSDevin Teske				is_usable = 0
15257323adacSDevin Teske			exit ! is_usable
15267323adacSDevin Teske		}'; then
15277323adacSDevin Teske			f_interactive &&
15287323adacSDevin Teske				f_show_msg "$msg_using_interface" "$__iface"
15297323adacSDevin Teske			f_dprintf "f_device_scan_tcp found %s" "$__iface"
15307323adacSDevin Teske			if [ "$__var_to_set" ]; then
15317323adacSDevin Teske				setvar "$__var_to_set" "$__iface"
15327323adacSDevin Teske			else
15337323adacSDevin Teske				echo "$__iface" >&2
15347323adacSDevin Teske			fi
15357323adacSDevin Teske			return $SUCCESS
15367323adacSDevin Teske		fi
15377323adacSDevin Teske	done
15387323adacSDevin Teske
15397323adacSDevin Teske	return $FAILURE
15407323adacSDevin Teske}
15417323adacSDevin Teske
15427323adacSDevin Teske# f_device_select_tcp
15437323adacSDevin Teske#
15447323adacSDevin Teske# Prompt the user to select network interface to use for TCP/IP access.
15457323adacSDevin Teske# Variables from variable.subr that can be used to script user input:
15467323adacSDevin Teske#
15477323adacSDevin Teske# 	VAR_NETWORK_DEVICE [Optional]
15487323adacSDevin Teske# 		Either a comma-separated list of network interfaces to try when
15497323adacSDevin Teske# 		setting up network access (e.g., "fxp0,em0") or "ANY" (case-
15507323adacSDevin Teske# 		sensitive) to indicate that the first active and configured
15517323adacSDevin Teske# 		interface is acceptable. If unset, the user is presented with a
15527323adacSDevin Teske# 		menu of all available network interfaces.
15537323adacSDevin Teske#
15547323adacSDevin Teske# Returns success if a valid network interface has been selected.
15557323adacSDevin Teske#
15567323adacSDevin Teskef_device_select_tcp()
15577323adacSDevin Teske{
15587323adacSDevin Teske	local devs dev cnt network_dev
15597323adacSDevin Teske	f_getvar $VAR_NETWORK_DEVICE network_dev
15607323adacSDevin Teske
15617323adacSDevin Teske	f_dprintf "f_device_select_tcp: %s=[%s]" \
15627323adacSDevin Teske	          VAR_NETWORK_DEVICE "$network_dev"
15637323adacSDevin Teske
15647323adacSDevin Teske	if [ "$network_dev" ]; then
15657323adacSDevin Teske		#
15667323adacSDevin Teske		# This can be set to several types of values. If set to ANY,
15677323adacSDevin Teske		# scan all network devices looking for a valid link, and go
15687323adacSDevin Teske		# with the first device found. Can also be specified as a
15697323adacSDevin Teske		# comma delimited list, with each network device tried in
15707323adacSDevin Teske		# order. Can also be set to a single network device.
15717323adacSDevin Teske		#
15727323adacSDevin Teske		[ "$network_dev" = "ANY" ] && f_device_scan_tcp network_dev
15737323adacSDevin Teske
15747323adacSDevin Teske		while [ "$network_dev" ]; do
15757323adacSDevin Teske			case "$network_dev" in
15767323adacSDevin Teske			*,*) dev="${network_dev%%,*}"
15777323adacSDevin Teske			     network_dev="${network_dev#*,}"
15787323adacSDevin Teske			     ;;
15797323adacSDevin Teske			  *) dev="$network_dev"
15807323adacSDevin Teske			     network_dev=
15817323adacSDevin Teske			esac
15827323adacSDevin Teske
15837323adacSDevin Teske			f_device_find "$dev" $DEVICE_TYPE_NETWORK devs
15847323adacSDevin Teske			cnt=$( set -- $devs; echo $# )
15857323adacSDevin Teske
15867323adacSDevin Teske			if [ ${cnt:=0} -gt 0 ]; then
15877323adacSDevin Teske				dev="${devs%%[$IFS]*}"
15887323adacSDevin Teske				f_device_dialog_tcp $dev
15897323adacSDevin Teske				if [ $? -eq $SUCCESS ]; then
15907323adacSDevin Teske					setvar $VAR_NETWORK_DEVICE $dev
15917323adacSDevin Teske					return $SUCCESS
15927323adacSDevin Teske				fi
15937323adacSDevin Teske			fi
15947323adacSDevin Teske		done
15957323adacSDevin Teske
15967323adacSDevin Teske		f_interactive && f_dialog_msgbox "$msg_no_network_devices"
15977323adacSDevin Teske		return $FAILURE
15987323adacSDevin Teske
15997323adacSDevin Teske	fi # $network_dev
16007323adacSDevin Teske
16017323adacSDevin Teske	f_device_find "" $DEVICE_TYPE_NETWORK devs
16027323adacSDevin Teske	cnt=$( set -- $devs; echo $# )
16037323adacSDevin Teske	dev="${devs%%[$IFS]*}"
16047323adacSDevin Teske
16057323adacSDevin Teske	f_quietly f_getvar NETWORK_CONFIGURED # for debugging info
16067323adacSDevin Teske	if ! f_running_as_init &&
16077323adacSDevin Teske	   ! [ "${NETWORK_CONFIGURED+set}" -a "$NETWORK_CONFIGURED" = "NO" ]
16087323adacSDevin Teske	then
16097323adacSDevin Teske		trap 'f_interrupt' SIGINT
16107323adacSDevin Teske		if f_dialog_yesno "$msg_assume_network_is_already_configured"
16117323adacSDevin Teske		then
16127323adacSDevin Teske			setvar $VAR_NETWORK_DEVICE $dev
16137323adacSDevin Teske			return $SUCCESS
16147323adacSDevin Teske		fi
16157323adacSDevin Teske	fi
16167323adacSDevin Teske
16177323adacSDevin Teske	local retval=$SUCCESS
16187323adacSDevin Teske	if [ ${cnt:=0} -eq 0 ]; then
16197323adacSDevin Teske		f_dialog_msgbox "$msg_no_network_devices"
16207323adacSDevin Teske		retval=$FAILURE
16217323adacSDevin Teske	elif [ $cnt -eq 1 ]; then
16227323adacSDevin Teske		f_device_dialog_tcp $dev
16237323adacSDevin Teske		retval=$?
16247323adacSDevin Teske		[ $retval -eq $SUCCESS ] && setvar $VAR_NETWORK_DEVICE $dev
16257323adacSDevin Teske	else
16267323adacSDevin Teske		local title="$msg_network_interface_information_required"
16277323adacSDevin Teske		local prompt="$msg_please_select_ethernet_device_to_configure"
16287323adacSDevin Teske		local hline="$hline_arrows_tab_enter"
16297323adacSDevin Teske
16307323adacSDevin Teske		dev=$( f_device_menu \
16317323adacSDevin Teske			"$title" "$prompt" "$hline" $DEVICE_TYPE_NETWORK \
16327323adacSDevin Teske			"$NETWORK_DEVICE_HELPFILE" \
16337323adacSDevin Teske			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD )
16347323adacSDevin Teske		retval=$?
16357323adacSDevin Teske		[ "$dev" ] || return $FAILURE
16367323adacSDevin Teske
16377323adacSDevin Teske		f_device_find "$dev" $DEVICE_TYPE_NETWORK devs
16387323adacSDevin Teske		[ "$devs" ] || return $FAILURE
16397323adacSDevin Teske		dev="${devs%%[$IFS]*}"
16407323adacSDevin Teske
16417323adacSDevin Teske		f_device_dialog_tcp $dev
16427323adacSDevin Teske		retval=$?
16437323adacSDevin Teske		if [ $retval -eq $SUCCESS ]; then
16447323adacSDevin Teske			f_struct_copy device_$dev device_network
16457323adacSDevin Teske			setvar $VAR_NETWORK_DEVICE network
16467323adacSDevin Teske		else
16477323adacSDevin Teske			f_struct_free device_network
16487323adacSDevin Teske		fi
16497323adacSDevin Teske	fi
16507323adacSDevin Teske
16517323adacSDevin Teske	return $retval
16527323adacSDevin Teske}
16537323adacSDevin Teske
16547323adacSDevin Teske# f_dialog_menu_select_tcp
16557323adacSDevin Teske#
16567323adacSDevin Teske# Like f_dialog_select_tcp() above, but do it from a menu that doesn't care
16577323adacSDevin Teske# about status. In other words, where f_dialog_select_tcp() will not display a
16587323adacSDevin Teske# menu if scripted, this function will always display the menu of available
16597323adacSDevin Teske# network interfaces.
16607323adacSDevin Teske#
16617323adacSDevin Teskef_dialog_menu_select_tcp()
16627323adacSDevin Teske{
16637323adacSDevin Teske	local private use_dhcp name
16647323adacSDevin Teske	NETWORK_CONFIGURED=NO f_device_select_tcp
16657323adacSDevin Teske	if f_struct device_network &&
16667323adacSDevin Teske	   device_network get private private &&
16677323adacSDevin Teske	   f_struct_copy "$private" di &&
16687323adacSDevin Teske	   di get use_dhcp use_dhcp &&
16697323adacSDevin Teske	   [ ! "$use_dhcp" ] &&
16707323adacSDevin Teske	   device_network get name name &&
16717323adacSDevin Teske	   f_yesno "$msg_would_you_like_to_bring_interface_up" "$name"
16727323adacSDevin Teske	then
16737323adacSDevin Teske		if ! f_device_init network; then
16747323adacSDevin Teske			f_show_msg "$msg_initialization_of_device_failed" \
16757323adacSDevin Teske			           "$name"
16767323adacSDevin Teske		fi
16777323adacSDevin Teske	fi
16787323adacSDevin Teske	return $SUCCESS
16797323adacSDevin Teske}
16807323adacSDevin Teske
16817323adacSDevin Teske############################################################ MAIN
16827323adacSDevin Teske
16837323adacSDevin Teskef_dprintf "%s: Successfully loaded." media/tcpip.subr
16847323adacSDevin Teske
16857323adacSDevin Teskefi # ! $_MEDIA_TCPIP_SUBR
1686