xref: /freebsd/usr.sbin/bsdconfig/share/media/tcpip.subr (revision 5e7aa7574445f1fc90140bc93ec75de83e850fbe)
17323adacSDevin Teskeif [ ! "$_MEDIA_TCPIP_SUBR" ]; then _MEDIA_TCPIP_SUBR=1
27323adacSDevin Teske#
37323adacSDevin Teske# Copyright (c) 2012-2013 Devin Teske
4f8ea072aSDevin 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
168e37a7c8SDevin 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
208e37a7c8SDevin Teske# DAMAGES (INCLUDING, 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#
287323adacSDevin Teske############################################################ INCLUDES
297323adacSDevin Teske
307323adacSDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
317323adacSDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
327323adacSDevin Teskef_dprintf "%s: loading includes..." media/tcpip.subr
337323adacSDevin Teskef_include $BSDCFG_SHARE/device.subr
347323adacSDevin Teskef_include $BSDCFG_SHARE/dialog.subr
35d4ae33f0SDevin Teskef_include $BSDCFG_SHARE/strings.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#
113c5f2a20eSDevin Teske# To call this function and display an appropriate error message to the user
114c5f2a20eSDevin Teske# based on the above error codes, use the following function defined in
115c5f2a20eSDevin Teske# dialog.subr:
1167323adacSDevin Teske#
117c5f2a20eSDevin Teske# 	f_dialog_validate_hostname $hostname
1187323adacSDevin Teske#
1197323adacSDevin Teskef_validate_hostname()
1207323adacSDevin Teske{
1217323adacSDevin Teske	local fqhn="$1"
1227323adacSDevin Teske
1237323adacSDevin Teske	# Return error if the hostname exceeds 255 characters
1247323adacSDevin Teske	[ ${#fqhn} -gt 255 ] && return 255
1257323adacSDevin Teske
1267323adacSDevin Teske	local IFS="." # Split on `dot'
1277323adacSDevin Teske	for label in $fqhn; do
1287323adacSDevin Teske		# Return error if the label exceeds 63 characters
1297323adacSDevin Teske		[ ${#label} -gt 63 ] && return 63
1307323adacSDevin Teske
1317323adacSDevin Teske		# Return error if the label is null
1327323adacSDevin Teske		[ "$label" ] || return 3
1337323adacSDevin Teske
1347323adacSDevin Teske		# Return error if label begins/ends with dash
1357323adacSDevin Teske		case "$label" in -*|*-) return 2; esac
1367323adacSDevin Teske
1377323adacSDevin Teske		# Return error if the label contains any invalid chars
1387323adacSDevin Teske		case "$label" in *[!0-9a-zA-Z-]*) return 1; esac
1397323adacSDevin Teske	done
1407323adacSDevin Teske
1417323adacSDevin Teske	return $SUCCESS
1427323adacSDevin Teske}
1437323adacSDevin Teske
1447323adacSDevin Teske# f_inet_atoi $ipv4_address [$var_to_set]
1457323adacSDevin Teske#
1467323adacSDevin Teske# Convert an IPv4 address or mask from dotted-quad notation (e.g., `127.0.0.1'
1477323adacSDevin Teske# or `255.255.255.0') to a 32-bit unsigned integer for the purpose of network
1487323adacSDevin Teske# and broadcast calculations. For example, one can validate that two addresses
1497323adacSDevin Teske# are on the same network:
1507323adacSDevin Teske#
1517323adacSDevin Teske# 	f_inet_atoi 1.2.3.4 ip1num
1527323adacSDevin Teske# 	f_inet_atoi 1.2.4.5 ip2num
1537323adacSDevin Teske# 	f_inet_atoi 255.255.0.0 masknum
1547323adacSDevin Teske# 	if [ $(( $ip1num & $masknum )) -eq \
1557323adacSDevin Teske# 	     $(( $ip2num & $masknum )) ]
1567323adacSDevin Teske# 	then
1577323adacSDevin Teske# 		: IP addresses are on same network
1587323adacSDevin Teske# 	fi
1597323adacSDevin Teske#
1607323adacSDevin Teske# See f_validate_ipaddr() below for an additional example usage, on calculating
1617323adacSDevin Teske# network and broadcast addresses.
1627323adacSDevin Teske#
1637323adacSDevin Teske# If $var_to_set is missing or NULL, the converted IP address is printed to
1647323adacSDevin Teske# standard output for capturing in a sub-shell (which is less-recommended
1657323adacSDevin Teske# because of performance degredation; for example, when called in a loop).
1667323adacSDevin Teske#
1677323adacSDevin Teskef_inet_atoi()
1687323adacSDevin Teske{
1697323adacSDevin Teske	local __addr="$1" __var_to_set="$2" __num=0
1707323adacSDevin Teske	if f_validate_ipaddr "$__addr"; then
1719ecd54f2SDevin Teske		local IFS=.
172d4ae33f0SDevin Teske		set -- $__addr
173d4ae33f0SDevin Teske		__num=$(( ($1 << 24) + ($2 << 16) + ($3 << 8) + $4 ))
1747323adacSDevin Teske	fi
1757323adacSDevin Teske	if [ "$__var_to_set" ]; then
1767323adacSDevin Teske		setvar "$__var_to_set" $__num
1777323adacSDevin Teske	else
1787323adacSDevin Teske		echo $__num
1797323adacSDevin Teske	fi
1807323adacSDevin Teske}
1817323adacSDevin Teske
1827323adacSDevin Teske# f_validate_ipaddr $ipaddr [$netmask]
1837323adacSDevin Teske#
1847323adacSDevin Teske# Returns zero if the given argument (an IP address) is of the proper format.
1857323adacSDevin Teske#
1867323adacSDevin Teske# The return status for invalid IP address is one of:
1877323adacSDevin Teske# 	1	One or more individual octets within the IP address (separated
1887323adacSDevin Teske# 	 	by dots) contains one or more invalid characters.
1897323adacSDevin Teske# 	2	One or more individual octets within the IP address are null
1907323adacSDevin Teske# 	 	and/or missing.
1917323adacSDevin Teske# 	3	One or more individual octets within the IP address exceeds the
1927323adacSDevin Teske# 	 	maximum of 255 (or 2^8, being an octet comprised of 8 bits).
1937323adacSDevin Teske# 	4	The IP address has either too few or too many octets.
1947323adacSDevin Teske#
1957323adacSDevin Teske# If a netmask is provided, the IP address is checked further:
1967323adacSDevin Teske#
1977323adacSDevin Teske# 	5	The IP address must not be the network or broadcast address.
1987323adacSDevin Teske#
1997323adacSDevin Teskef_validate_ipaddr()
2007323adacSDevin Teske{
2017323adacSDevin Teske	local ip="$1" mask="$2"
2027323adacSDevin Teske
2037323adacSDevin Teske	# Track number of octets for error checking
2047323adacSDevin Teske	local noctets=0
2057323adacSDevin Teske
2069ecd54f2SDevin Teske	local oldIFS="$IFS" IFS="." # Split on `dot'
2077323adacSDevin Teske	for octet in $ip; do
2087323adacSDevin Teske		# Return error if the octet is null
2097323adacSDevin Teske		[ "$octet" ] || return 2
2107323adacSDevin Teske
2117323adacSDevin Teske		# Return error if not a whole integer
2127323adacSDevin Teske		f_isinteger "$octet" || return 1
2137323adacSDevin Teske
2147323adacSDevin Teske		# Return error if not a positive integer
2157323adacSDevin Teske		[ $octet -ge 0 ] || return 1
2167323adacSDevin Teske
2177323adacSDevin Teske		# Return error if the octet exceeds 255
2187323adacSDevin Teske		[ $octet -gt 255 ] && return 3
2197323adacSDevin Teske
2207323adacSDevin Teske		noctets=$(( $noctets + 1 ))
2217323adacSDevin Teske	done
2227323adacSDevin Teske	IFS="$oldIFS"
2237323adacSDevin Teske
2247323adacSDevin Teske	[ $noctets -eq 4 ] || return 4
2257323adacSDevin Teske
2267323adacSDevin Teske	#
2277323adacSDevin Teske	# The IP address must not be network or broadcast address.
2287323adacSDevin Teske	#
2297323adacSDevin Teske	if [ "$mask" ]; then
2307323adacSDevin Teske		local ipnum masknum netnum bcastnum
2317323adacSDevin Teske		local max_addr=4294967295 # 255.255.255.255
2327323adacSDevin Teske
2337323adacSDevin Teske		f_inet_atoi $ip ipnum
2347323adacSDevin Teske		f_inet_atoi $mask masknum
2357323adacSDevin Teske
2367323adacSDevin Teske		netnum=$(( $ipnum & $masknum ))
2377323adacSDevin Teske		bcastnum=$(( ($ipnum & $masknum)+$max_addr-$masknum ))
2387323adacSDevin Teske
2397323adacSDevin Teske		if [ "$masknum" ] &&
2407323adacSDevin Teske		   [ $ipnum -eq $netnum -o $ipnum -eq $bcastnum ]
2417323adacSDevin Teske		then
2427323adacSDevin Teske			return 5
2437323adacSDevin Teske		fi
2447323adacSDevin Teske	fi
2457323adacSDevin Teske
2467323adacSDevin Teske	return $SUCCESS
2477323adacSDevin Teske}
2487323adacSDevin Teske
2497323adacSDevin Teske# f_validate_ipaddr6 $ipv6_addr
2507323adacSDevin Teske#
2517323adacSDevin Teske# Returns zero if the given argument (an IPv6 address) is of the proper format.
2527323adacSDevin Teske#
2537323adacSDevin Teske# The return status for invalid IP address is one of:
2547323adacSDevin Teske# 	1	One or more individual segments within the IP address
2557323adacSDevin Teske# 	 	(separated by colons) contains one or more invalid characters.
2567323adacSDevin Teske# 	 	Segments must contain only combinations of the characters 0-9,
2577323adacSDevin Teske# 	 	A-F, or a-f.
2587323adacSDevin Teske# 	2	Too many/incorrect null segments. A single null segment is
2597323adacSDevin Teske# 	 	allowed within the IP address (separated by colons) but not
2607323adacSDevin Teske# 	 	allowed at the beginning or end (unless a double-null segment;
2617323adacSDevin Teske# 	 	i.e., "::*" or "*::").
2627323adacSDevin Teske# 	3	One or more individual segments within the IP address
2637323adacSDevin Teske# 	 	(separated by colons) exceeds the length of 4 hex-digits.
2647323adacSDevin Teske# 	4	The IP address entered has either too few (less than 3), too
2657323adacSDevin Teske# 	 	many (more than 8), or not enough segments, separated by
2667323adacSDevin Teske# 	 	colons.
2677323adacSDevin Teske# 	5*	The IPv4 address at the end of the IPv6 address is invalid.
2687323adacSDevin Teske# 	*	When there is an error with the dotted-quad IPv4 address at the
2697323adacSDevin Teske# 	 	end of the IPv6 address, the return value of 5 is OR'd with a
2707323adacSDevin Teske# 	 	bit-shifted (<< 4) return of f_validate_ipaddr.
2717323adacSDevin Teske#
2727323adacSDevin Teskef_validate_ipaddr6()
2737323adacSDevin Teske{
2747323adacSDevin Teske	local ip="${1%\%*}" # removing the interface specification if-present
2757323adacSDevin Teske
2767323adacSDevin Teske	local IFS=":" # Split on `colon'
2777323adacSDevin Teske	set -- $ip:
2787323adacSDevin Teske
2797323adacSDevin Teske	# Return error if too many or too few segments
2807323adacSDevin Teske	# Using 9 as max in case of leading or trailing null spanner
2817323adacSDevin Teske	[ $# -gt 9 -o $# -lt 3 ] && return 4
2827323adacSDevin Teske
2837323adacSDevin Teske	local h="[0-9A-Fa-f]"
2847323adacSDevin Teske	local nulls=0 nsegments=$# contains_ipv4_segment=
2857323adacSDevin Teske
2867323adacSDevin Teske	while [ $# -gt 0 ]; do
2877323adacSDevin Teske
2887323adacSDevin Teske		segment="${1%:}"
2897323adacSDevin Teske		shift
2907323adacSDevin Teske
2917323adacSDevin Teske		#
2927323adacSDevin Teske		# Return error if this segment makes one null too-many. A
2937323adacSDevin Teske		# single null segment is allowed anywhere in the middle as well
2947323adacSDevin Teske		# as double null segments are allowed at the beginning or end
2957323adacSDevin Teske		# (but not both).
2967323adacSDevin Teske		#
2977323adacSDevin Teske		if [ ! "$segment" ]; then
2987323adacSDevin Teske			nulls=$(( $nulls + 1 ))
2997323adacSDevin Teske			if [ $nulls -eq 3 ]; then
3007323adacSDevin Teske				# Only valid syntax for 3 nulls is `::'
3017323adacSDevin Teske				[ "$ip" = "::" ] || return 2
3027323adacSDevin Teske			elif [ $nulls -eq 2 ]; then
3037323adacSDevin Teske				# Only valid if begins/ends with `::'
3047323adacSDevin Teske				case "$ip" in
3057323adacSDevin Teske				::*|*::) : fall thru ;;
3067323adacSDevin Teske				*) return 2
3077323adacSDevin Teske				esac
3087323adacSDevin Teske			fi
3097323adacSDevin Teske			continue
3107323adacSDevin Teske		fi
3117323adacSDevin Teske
3127323adacSDevin Teske		#
3137323adacSDevin Teske		# Return error if not a valid hexadecimal short
3147323adacSDevin Teske		#
3157323adacSDevin Teske		case "$segment" in
3167323adacSDevin Teske		$h|$h$h|$h$h$h|$h$h$h$h)
3177323adacSDevin Teske			: valid segment of 1-4 hexadecimal digits
3187323adacSDevin Teske			;;
3197323adacSDevin Teske		*[!0-9A-Fa-f]*)
3207323adacSDevin Teske			# Segment contains at least one invalid char
3217323adacSDevin Teske
3227323adacSDevin Teske			# Return error immediately if not last segment
3237323adacSDevin Teske			[ $# -eq 0 ] || return 1
3247323adacSDevin Teske
3257323adacSDevin Teske			# Otherwise, check for legacy IPv4 notation
3267323adacSDevin Teske			case "$segment" in
3277323adacSDevin Teske			*[!0-9.]*)
3287323adacSDevin Teske				# Segment contains at least one invalid
3297323adacSDevin Teske				# character even for an IPv4 address
3307323adacSDevin Teske				return 1
3317323adacSDevin Teske			esac
3327323adacSDevin Teske
3337323adacSDevin Teske			# Return error if not enough segments
3347323adacSDevin Teske			if [ $nulls -eq 0 ]; then
3357323adacSDevin Teske				[ $nsegments -eq 7 ] || return 4
3367323adacSDevin Teske			fi
3377323adacSDevin Teske
3387323adacSDevin Teske			contains_ipv4_segment=1
3397323adacSDevin Teske
3407323adacSDevin Teske			# Validate the IPv4 address
3417323adacSDevin Teske			f_validate_ipaddr "$segment" ||
3427323adacSDevin Teske				return $(( 5 | $? << 4 ))
3437323adacSDevin Teske			;;
3447323adacSDevin Teske		*)
3457323adacSDevin Teske			# Segment characters are all valid but too many
3467323adacSDevin Teske			return 3
3477323adacSDevin Teske		esac
3487323adacSDevin Teske
3497323adacSDevin Teske	done
3507323adacSDevin Teske
3517323adacSDevin Teske	if [ $nulls -eq 1 ]; then
3527323adacSDevin Teske		# Single null segment cannot be at beginning/end
3537323adacSDevin Teske		case "$ip" in
3547323adacSDevin Teske		:*|*:) return 2
3557323adacSDevin Teske		esac
3567323adacSDevin Teske	fi
3577323adacSDevin Teske
3587323adacSDevin Teske	#
3597323adacSDevin Teske	# A legacy IPv4 address can span the last two 16-bit segments,
3607323adacSDevin Teske	# reducing the amount of maximum allowable segments by-one.
3617323adacSDevin Teske	#
3627323adacSDevin Teske	maxsegments=8
3637323adacSDevin Teske	if [ "$contains_ipv4_segment" ]; then
3647323adacSDevin Teske		maxsegments=7
3657323adacSDevin Teske	fi
3667323adacSDevin Teske
3677323adacSDevin Teske	case $nulls in
3687323adacSDevin Teske	# Return error if missing segments with no null spanner
3697323adacSDevin Teske	0) [ $nsegments -eq $maxsegments ] || return 4 ;;
3707323adacSDevin Teske	# Return error if null spanner with too many segments
3717323adacSDevin Teske	1) [ $nsegments -le $maxsegments ] || return 4 ;;
3727323adacSDevin Teske	# Return error if leading/trailing `::' with too many segments
3737323adacSDevin Teske	2) [ $nsegments -le $(( $maxsegments + 1 )) ] || return 4 ;;
3747323adacSDevin Teske	esac
3757323adacSDevin Teske
3767323adacSDevin Teske	return $SUCCESS
3777323adacSDevin Teske}
3787323adacSDevin Teske
3797323adacSDevin Teske# f_validate_netmask $netmask
3807323adacSDevin Teske#
3817323adacSDevin Teske# Returns zero if the given argument (a subnet mask) is of the proper format.
3827323adacSDevin Teske#
3837323adacSDevin Teske# The return status for invalid netmask is one of:
3847323adacSDevin Teske# 	1	One or more individual fields within the subnet mask (separated
3857323adacSDevin Teske# 	 	by dots) contains one or more invalid characters.
3867323adacSDevin Teske# 	2	One or more individual fields within the subnet mask are null
3877323adacSDevin Teske# 	 	and/or missing.
3887323adacSDevin Teske# 	3	One or more individual fields within the subnet mask exceeds
3897323adacSDevin Teske# 	 	the maximum of 255 (a full 8-bit register).
3907323adacSDevin Teske# 	4	The subnet mask has either too few or too many fields.
3917323adacSDevin Teske# 	5	One or more individual fields within the subnet mask is an
3927323adacSDevin Teske# 	 	invalid integer (only 0,128,192,224,240,248,252,254,255 are
3937323adacSDevin Teske# 	 	valid integers).
3947323adacSDevin Teske#
3957323adacSDevin Teskef_validate_netmask()
3967323adacSDevin Teske{
3977323adacSDevin Teske	local mask="$1"
3987323adacSDevin Teske
3997323adacSDevin Teske	# Track number of fields for error checking
4007323adacSDevin Teske	local nfields=0
4017323adacSDevin Teske
4027323adacSDevin Teske	local IFS="." # Split on `dot'
4037323adacSDevin Teske	for field in $mask; do
4047323adacSDevin Teske		# Return error if the field is null
4057323adacSDevin Teske		[ "$field" ] || return 2
4067323adacSDevin Teske
4077323adacSDevin Teske		# Return error if not a whole positive integer
4087323adacSDevin Teske		f_isinteger "$field" || return 1
4097323adacSDevin Teske
4107323adacSDevin Teske		# Return error if the field exceeds 255
4117323adacSDevin Teske		[ $field -gt 255 ] && return 3
4127323adacSDevin Teske
4137323adacSDevin Teske		# Return error if the field is an invalid integer
4147323adacSDevin Teske		case "$field" in
4157323adacSDevin Teske		0|128|192|224|240|248|252|254|255) : ;;
4167323adacSDevin Teske		*) return 5 ;;
4177323adacSDevin Teske		esac
4187323adacSDevin Teske
4197323adacSDevin Teske		nfields=$(( $nfields + 1 ))
4207323adacSDevin Teske	done
4217323adacSDevin Teske
4227323adacSDevin Teske	[ $nfields -eq 4 ] || return 4
4237323adacSDevin Teske}
4247323adacSDevin Teske
4257323adacSDevin Teske# f_validate_gateway $gateway $ipaddr $netmask
4267323adacSDevin Teske#
4277323adacSDevin Teske# Validate an IPv4 default gateway (aka router) address for a given IP address
4287323adacSDevin Teske# making sure the two are in the same network (able to ``talk'' to each other).
4297323adacSDevin Teske# Returns success if $ipaddr and $gateway are in the same network given subnet
4307323adacSDevin Teske# mask $netmask.
4317323adacSDevin Teske#
4327323adacSDevin Teskef_validate_gateway()
4337323adacSDevin Teske{
4347323adacSDevin Teske	local gateway="$1" ipaddr="$2" netmask="$3"
4357323adacSDevin Teske	local gwnum ipnum masknum
4367323adacSDevin Teske
4377323adacSDevin Teske	f_validate_ipaddr "$gateway" "$netmask" || return $FAILURE
4387323adacSDevin Teske
4397323adacSDevin Teske	f_inet_atoi "$netmask" masknum
4407323adacSDevin Teske	f_inet_atoi "$ipaddr"  ipnum
4417323adacSDevin Teske	f_inet_atoi "$gateway" gwnum
4427323adacSDevin Teske
4437323adacSDevin Teske	# Gateway must be within set of IPs reachable through interface
4447323adacSDevin Teske	[ $(( $ipnum & $masknum )) -eq \
4457323adacSDevin Teske	  $(( $gwnum & $masknum )) ] # Return status
4467323adacSDevin Teske}
4477323adacSDevin Teske
4487323adacSDevin Teske# f_dialog_validate_tcpip $hostname $gateway $nameserver $ipaddr $netmask
4497323adacSDevin Teske#
4507323adacSDevin Teske# Returns success if the arguments provided are valid for accessing a TCP/IP
4517323adacSDevin Teske# network, otherwise returns failure.
4527323adacSDevin Teske#
4537323adacSDevin Teskef_dialog_validate_tcpip()
4547323adacSDevin Teske{
4557323adacSDevin Teske	local hostname="$1" gateway="$2" nameserver="$3"
4567323adacSDevin Teske	local ipaddr="$4" netmask="$5"
4577323adacSDevin Teske	local ipnum masknum
4587323adacSDevin Teske
4597323adacSDevin Teske	if [ ! "$hostname" ]; then
4607079fc4eSDevin Teske		f_show_msg "$msg_must_specify_a_host_name_of_some_sort"
4617323adacSDevin Teske	elif ! f_validate_hostname "$hostname"; then
4627079fc4eSDevin Teske		f_show_msg "$msg_invalid_hostname_value"
4637323adacSDevin Teske	elif [ "$netmask" ] && ! f_validate_netmask "$netmask"; then
4647079fc4eSDevin Teske		f_show_msg "$msg_invalid_netmask_value"
4657323adacSDevin Teske	elif [ "$nameserver" ] &&
4667323adacSDevin Teske	     ! f_validate_ipaddr "$nameserver" &&
4677323adacSDevin Teske	     ! f_validate_ipaddr6 "$nameserver"; then
4687079fc4eSDevin Teske		f_show_msg "$msg_invalid_name_server_ip_address_specified"
4697323adacSDevin Teske	elif [ "$ipaddr" ] && ! f_validate_ipaddr "$ipaddr" "$netmask"; then
4707079fc4eSDevin Teske		f_show_msg "$msg_invalid_ipv4_address"
4717323adacSDevin Teske	elif [ "$gateway" -a "$gateway" != "NO" ] &&
4727323adacSDevin Teske	     ! f_validate_gateway "$gateway" "$ipaddr" "$netmask"; then
4737079fc4eSDevin Teske		f_show_msg "$msg_invalid_gateway_ipv4_address_specified"
4747323adacSDevin Teske	else
475f677a9e2SDevin Teske		return $DIALOG_OK
4767323adacSDevin Teske	fi
4777323adacSDevin Teske
478f677a9e2SDevin Teske	return $DIALOG_CANCEL
4797323adacSDevin Teske}
4807323adacSDevin Teske
4817323adacSDevin Teske# f_ifconfig_inet $interface [$var_to_set]
4827323adacSDevin Teske#
4837323adacSDevin Teske# Returns the IPv4 address associated with $interface. If $var_to_set is
4847323adacSDevin Teske# missing or NULL, the IP address is printed to standard output for capturing
4857323adacSDevin Teske# in a sub-shell (which is less-recommended because of performance degredation;
4867323adacSDevin Teske# for example, when called in a loop).
4877323adacSDevin Teske#
4887323adacSDevin Teske# This function is a two-parter. Below is the awk(1) portion of the function,
4897323adacSDevin Teske# afterward is the sh(1) function which utilizes the below awk script.
4907323adacSDevin Teske#
4917323adacSDevin Teskef_ifconfig_inet_awk='
4927323adacSDevin TeskeBEGIN { found = 0 }
4937323adacSDevin Teske( $1 == "inet" ) \
4947323adacSDevin Teske{
4957323adacSDevin Teske	print $2
4967323adacSDevin Teske	found = 1
4977323adacSDevin Teske	exit
4987323adacSDevin Teske}
4997323adacSDevin TeskeEND { exit ! found }
5007323adacSDevin Teske'
5017323adacSDevin Teskef_ifconfig_inet()
5027323adacSDevin Teske{
5037323adacSDevin Teske	local __interface="$1" __var_to_set="$2"
5047323adacSDevin Teske	if [ "$__var_to_set" ]; then
5057323adacSDevin Teske		local __ip
5067323adacSDevin Teske		__ip=$( ifconfig "$__interface" 2> /dev/null |
5077323adacSDevin Teske			awk "$f_ifconfig_inet_awk" )
5087323adacSDevin Teske		setvar "$__var_to_set" "$__ip"
5097323adacSDevin Teske	else
5107323adacSDevin Teske		ifconfig "$__interface" 2> /dev/null |
5117323adacSDevin Teske			awk "$f_ifconfig_inet_awk"
5127323adacSDevin Teske	fi
5137323adacSDevin Teske}
5147323adacSDevin Teske
5157323adacSDevin Teske# f_ifconfig_inet6 $interface [$var_to_set]
5167323adacSDevin Teske#
5177323adacSDevin Teske# Returns the IPv6 address associated with $interface. If $var_to_set is
5187323adacSDevin Teske# missing or NULL, the IP address is printed to standard output for capturing
5197323adacSDevin Teske# in a sub-shell (which is less-recommended because of performance degredation;
5207323adacSDevin Teske# for example, when called in a loop).
5217323adacSDevin Teske#
5227323adacSDevin Teske# This function is a two-parter. Below is the awk(1) portion of the function,
5237323adacSDevin Teske# afterward is the sh(1) function which utilizes the below awk script.
5247323adacSDevin Teske#
5257323adacSDevin Teskef_ifconfig_inet6_awk='
5267323adacSDevin TeskeBEGIN { found = 0 }
5277323adacSDevin Teske( $1 == "inet6" ) \
5287323adacSDevin Teske{
5297323adacSDevin Teske	print $2
5307323adacSDevin Teske	found = 1
5317323adacSDevin Teske	exit
5327323adacSDevin Teske}
5337323adacSDevin TeskeEND { exit ! found }
5347323adacSDevin Teske'
5357323adacSDevin Teskef_ifconfig_inet6()
5367323adacSDevin Teske{
5377323adacSDevin Teske	local __interface="$1" __var_to_set="$2"
5387323adacSDevin Teske	if [ "$__var_to_set" ]; then
5397323adacSDevin Teske		local __ip6
5407323adacSDevin Teske		__ip6=$( ifconfig "$__interface" 2> /dev/null |
5417323adacSDevin Teske			awk "$f_ifconfig_inet6_awk" )
5427323adacSDevin Teske		setvar "$__var_to_set" "$__ip6"
5437323adacSDevin Teske	else
5447323adacSDevin Teske		ifconfig "$__interface" 2> /dev/null |
5457323adacSDevin Teske			awk "$f_ifconfig_inet6_awk"
5467323adacSDevin Teske	fi
5477323adacSDevin Teske}
5487323adacSDevin Teske
5497323adacSDevin Teske# f_ifconfig_netmask $interface [$var_to_set]
5507323adacSDevin Teske#
5517323adacSDevin Teske# Returns the IPv4 subnet mask associated with $interface. If $var_to_set is
5527323adacSDevin Teske# missing or NULL, the netmask is printed to standard output for capturing in a
5537323adacSDevin Teske# sub-shell (which is less-recommended because of performance degredation; for
5547323adacSDevin Teske# example, when called in a loop).
5557323adacSDevin Teske#
5567323adacSDevin Teskef_ifconfig_netmask()
5577323adacSDevin Teske{
5587323adacSDevin Teske	local __interface="$1" __var_to_set="$2" __octets
5597323adacSDevin Teske	__octets=$( ifconfig "$__interface" 2> /dev/null | awk \
5607323adacSDevin Teske	'
5617323adacSDevin Teske		BEGIN { found = 0 }
5627323adacSDevin Teske		( $1 == "inet" ) \
5637323adacSDevin Teske		{
5647323adacSDevin Teske			printf "%s %s %s %s\n",
5657323adacSDevin Teske				substr($4,3,2),
5667323adacSDevin Teske				substr($4,5,2),
5677323adacSDevin Teske				substr($4,7,2),
5687323adacSDevin Teske				substr($4,9,2)
5697323adacSDevin Teske			found = 1
5707323adacSDevin Teske			exit
5717323adacSDevin Teske		}
5727323adacSDevin Teske		END { exit ! found }
5737323adacSDevin Teske	' ) || return $FAILURE
5747323adacSDevin Teske
5757323adacSDevin Teske	local __octet __netmask=
5767323adacSDevin Teske	for __octet in $__octets; do
577d4ae33f0SDevin Teske		f_sprintf __netmask "%s.%u" "$__netmask" "0x$__octet"
5787323adacSDevin Teske	done
5797323adacSDevin Teske	__netmask="${__netmask#.}"
5807323adacSDevin Teske	if [ "$__var_to_set" ]; then
5817323adacSDevin Teske		setvar "$__var_to_set" "$__netmask"
5827323adacSDevin Teske	else
5837323adacSDevin Teske		echo $__netmask
5847323adacSDevin Teske	fi
5857323adacSDevin Teske}
5867323adacSDevin Teske
5877323adacSDevin Teske# f_route_get_default [$var_to_set]
5887323adacSDevin Teske#
5897323adacSDevin Teske# Returns the IP address of the currently active default router. If $var_to_set
5907323adacSDevin Teske# is missing or NULL, the IP address is printed to standard output for
5917323adacSDevin Teske# capturing in a sub-shell (which is less-recommended because of performance
5927323adacSDevin Teske# degredation; for example, when called in a loop).
5937323adacSDevin Teske#
5947323adacSDevin Teske# This function is a two-parter. Below is the awk(1) portion of the function,
5957323adacSDevin Teske# afterward is the sh(1) function which utilizes the below awk script.
5967323adacSDevin Teske#
597a7408e5cSDevin Teskef_route_get_default_awk='
5987323adacSDevin TeskeBEGIN { found = 0 }
5997323adacSDevin Teske( $1 == "gateway:" ) \
6007323adacSDevin Teske{
6017323adacSDevin Teske	print $2
6027323adacSDevin Teske	found = 1
6037323adacSDevin Teske	exit
6047323adacSDevin Teske}
6057323adacSDevin TeskeEND { exit ! found }
6067323adacSDevin Teske'
6077323adacSDevin Teskef_route_get_default()
6087323adacSDevin Teske{
6097323adacSDevin Teske	local __var_to_set="$1"
6107323adacSDevin Teske	if [ "$__var_to_set" ]; then
6117323adacSDevin Teske		local __ip
6127323adacSDevin Teske		__ip=$( route -n get default 2> /dev/null |
6137323adacSDevin Teske			awk "$f_route_get_default_awk" )
6147323adacSDevin Teske		setvar "$__var_to_set" "$__ip"
6157323adacSDevin Teske	else
6167323adacSDevin Teske		route -n get default 2> /dev/null |
6177323adacSDevin Teske			awk "$f_route_get_default_awk"
6187323adacSDevin Teske	fi
6197323adacSDevin Teske}
6207323adacSDevin Teske
6217323adacSDevin Teske# f_resolv_conf_nameservers [$var_to_set]
6227323adacSDevin Teske#
6237323adacSDevin Teske# Returns nameserver(s) configured in resolv.conf(5). If $var_to_set is missing
6247323adacSDevin Teske# or NULL, the list of nameservers is printed to standard output for capturing
6257323adacSDevin Teske# in a sub-shell (which is less-recommended because of performance degredation;
6267323adacSDevin Teske# for example, when called in a loop).
6277323adacSDevin Teske#
6287323adacSDevin Teske# This function is a two-parter. Below is the awk(1) portion of the function,
6297323adacSDevin Teske# afterward is the sh(1) function which utilizes the below awk script.
6307323adacSDevin Teske#
6317323adacSDevin Teskef_resolv_conf_nameservers_awk='
6327323adacSDevin TeskeBEGIN { found = 0 }
6337323adacSDevin Teske( $1 == "nameserver" ) \
6347323adacSDevin Teske{
6357323adacSDevin Teske	print $2
6367323adacSDevin Teske	found = 1
6377323adacSDevin Teske}
6387323adacSDevin TeskeEND { exit ! found }
6397323adacSDevin Teske'
6407323adacSDevin Teskef_resolv_conf_nameservers()
6417323adacSDevin Teske{
6427323adacSDevin Teske	local __var_to_set="$1"
6437323adacSDevin Teske	if [ "$__var_to_set" ]; then
6447323adacSDevin Teske		local __ns
6457323adacSDevin Teske		__ns=$( awk "$f_resolv_conf_nameservers_awk" "$RESOLV_CONF" \
6467323adacSDevin Teske			2> /dev/null )
6477323adacSDevin Teske		setvar "$__var_to_set" "$__ns"
6487323adacSDevin Teske	else
6497323adacSDevin Teske		awk "$f_resolv_conf_nameservers_awk" "$RESOLV_CONF" \
6507323adacSDevin Teske			2> /dev/null
6517323adacSDevin Teske	fi
6527323adacSDevin Teske}
6537323adacSDevin Teske
6547323adacSDevin Teske# f_config_resolv
6557323adacSDevin Teske#
6567323adacSDevin Teske# Attempts to configure resolv.conf(5) and ilk. Returns success if able to
6577323adacSDevin Teske# write the file(s), otherwise returns error status.
6587323adacSDevin Teske#
6597323adacSDevin Teske# Variables from variable.subr that are used in configuring resolv.conf(5) are
6607323adacSDevin Teske# as follows (all of which can be configured automatically through functions
6617323adacSDevin Teske# like f_dhcp_get_info() or manually):
6627323adacSDevin Teske#
6637323adacSDevin Teske# 	VAR_NAMESERVER
6647323adacSDevin Teske#		The nameserver to add in resolv.conf(5).
6657323adacSDevin Teske# 	VAR_DOMAINNAME
6667323adacSDevin Teske# 		The domain to configure in resolv.conf(5). Also used in the
6677323adacSDevin Teske# 		configuration of hosts(5).
6687323adacSDevin Teske# 	VAR_IPADDR
6697323adacSDevin Teske# 		The IPv4 address to configure in hosts(5).
6707323adacSDevin Teske# 	VAR_IPV6ADDR
6717323adacSDevin Teske# 		The IPv6 address to configure in hosts(5).
6727323adacSDevin Teske# 	VAR_HOSTNAME
6737323adacSDevin Teske# 		The hostname to associate with the IPv4 and/or IPv6 address in
6747323adacSDevin Teske# 		hosts(5).
6757323adacSDevin Teske#
6767323adacSDevin Teskef_config_resolv()
6777323adacSDevin Teske{
6787323adacSDevin Teske	local cp c6p dp hp
6797323adacSDevin Teske
6807323adacSDevin Teske	f_getvar $VAR_NAMESERVER cp
6817323adacSDevin Teske	if [ "$cp" ]; then
6827323adacSDevin Teske		case "$RESOLV_CONF" in
6837323adacSDevin Teske		*/*) f_quietly mkdir -p "${RESOLV_CONF%/*}" ;;
6847323adacSDevin Teske		esac
6857323adacSDevin Teske
6867323adacSDevin Teske		# Attempt to create/truncate the file
6877323adacSDevin Teske		( :> "$RESOLV_CONF" ) 2> /dev/null || return $FAILURE
6887323adacSDevin Teske
6897323adacSDevin Teske		f_getvar $VAR_DOMAINNAME dp &&
6907323adacSDevin Teske			printf "domain\t%s\n" "$dp" >> "$RESOLV_CONF"
6917323adacSDevin Teske		printf "nameserver\t%s\n" "$cp" >> "$RESOLV_CONF"
6927323adacSDevin Teske
6937323adacSDevin Teske		f_dprintf "Wrote out %s" "$RESOLV_CONF"
6947323adacSDevin Teske	fi
6957323adacSDevin Teske
6967323adacSDevin Teske	f_getvar $VAR_DOMAINNAME dp
6977323adacSDevin Teske	f_getvar $VAR_IPADDR cp
6987323adacSDevin Teske	f_getvar $VAR_IPV6ADDR c6p
6997323adacSDevin Teske	f_getvar $VAR_HOSTNAME hp
7007323adacSDevin Teske
7017323adacSDevin Teske	# Attempt to create the file if it doesn't already exist
7027323adacSDevin Teske	if [ ! -e "$ETC_HOSTS" ]; then
7037323adacSDevin Teske		case "$ETC_HOSTS" in
7047323adacSDevin Teske		*/*) f_quietly mkdir -p "${ETC_HOSTS%/*}" ;;
7057323adacSDevin Teske		esac
7067323adacSDevin Teske
7077323adacSDevin Teske		( :> "$ETC_HOSTS" ) 2> /dev/null || return $FAILURE
7087323adacSDevin Teske	fi
7097323adacSDevin Teske
7107323adacSDevin Teske	# Scan the file and add ourselves if not already configured
7117323adacSDevin Teske	awk -v dn="$dp" -v ip4="$cp" -v ip6="$c6p" -v hn="$hp" '
7127323adacSDevin Teske		BEGIN {
7137323adacSDevin Teske			local4found = local6found = 0
7147323adacSDevin Teske			hn4found = hn6found = h4found = h6found = 0
7157323adacSDevin Teske			h = ( match(hn, /\./) ? substr(hn, 0, RSTART-1) : "" )
7167323adacSDevin Teske		}
7177323adacSDevin Teske		($1 == "127.0.0.1") { local4found = 1 }
7187323adacSDevin Teske		($1 == "::1") { local6found = 1 }
7197323adacSDevin Teske		{
7207323adacSDevin Teske			for (n = 2; n <= NF; n++)
7217323adacSDevin Teske			{
7227323adacSDevin Teske				if ( $1 == ip4 ) {
7237323adacSDevin Teske					if ( $n == h ) h4found = 1
7247323adacSDevin Teske					if ( $n == hn ) hn4found = 1
7257323adacSDevin Teske					if ( $n == hn "." ) hn4found = 1
7267323adacSDevin Teske				}
7277323adacSDevin Teske				if ( $1 == ip6 ) {
7287323adacSDevin Teske					if ( $n == h ) h6found = 1
7297323adacSDevin Teske					if ( $n == hn ) hn6found = 1
7307323adacSDevin Teske					if ( $n == hn "." ) hn6found = 1
7317323adacSDevin Teske				}
7327323adacSDevin Teske			}
7337323adacSDevin Teske		}
7347323adacSDevin Teske		END {
7357323adacSDevin Teske			hosts = FILENAME
7367323adacSDevin Teske
7377323adacSDevin Teske			if ( ! local6found )
7387323adacSDevin Teske				printf "::1\t\t\tlocalhost%s\n",
7397323adacSDevin Teske				       ( dn ? " localhost." dn : "" ) >> hosts
7407323adacSDevin Teske			if ( ! local4found )
7417323adacSDevin Teske				printf "127.0.0.1\t\tlocalhost%s\n",
7427323adacSDevin Teske				       ( dn ? " localhost." dn : "" ) >> hosts
7437323adacSDevin Teske
7447323adacSDevin Teske			if ( ip6 && ! (h6found && hn6found))
7457323adacSDevin Teske			{
7467323adacSDevin Teske				printf "%s\t%s %s\n", ip6, hn, h >> hosts
7477323adacSDevin Teske				printf "%s\t%s.\n", ip6, hn >> hosts
7487323adacSDevin Teske			}
7497323adacSDevin Teske			else if ( ip6 )
7507323adacSDevin Teske			{
7517323adacSDevin Teske				if ( ! h6found )
7527323adacSDevin Teske					printf "%s\t%s.\n", ip6, h >> hosts
7537323adacSDevin Teske				if ( ! hn6found )
7547323adacSDevin Teske					printf "%s\t%s\n", ip6, hn >> hosts
7557323adacSDevin Teske			}
7567323adacSDevin Teske
7577323adacSDevin Teske			if ( ip4 && ! (h4found && hn4found))
7587323adacSDevin Teske			{
7597323adacSDevin Teske				printf "%s\t\t%s %s\n", ip4, hn, h >> hosts
7607323adacSDevin Teske				printf "%s\t\t%s.\n", ip4, hn >> hosts
7617323adacSDevin Teske			}
7627323adacSDevin Teske			else if ( ip4 )
7637323adacSDevin Teske			{
7647323adacSDevin Teske				if ( ! h4found )
7657323adacSDevin Teske					printf "%s\t\t%s.\n", ip4, h >> hosts
7667323adacSDevin Teske				if ( ! hn4found )
7677323adacSDevin Teske					printf "%s\t\t%s\n", ip4, hn >> hosts
7687323adacSDevin Teske			}
7697323adacSDevin Teske		}
7707323adacSDevin Teske	' "$ETC_HOSTS" 2> /dev/null || return $FAILURE
7717323adacSDevin Teske
7727323adacSDevin Teske	f_dprintf "Wrote out %s" "$ETC_HOSTS"
7737323adacSDevin Teske	return $SUCCESS
7747323adacSDevin Teske}
7757323adacSDevin Teske
7767323adacSDevin Teske# f_dhcp_parse_leases $leasefile struct_name
7777323adacSDevin Teske#
7787323adacSDevin Teske# Parse $leasefile and store the information for the most recent lease in a
7797323adacSDevin Teske# struct (see struct.subr for additional details) named `struct_name'. See
7807323adacSDevin Teske# DHCP_LEASE struct definition in the GLOBALS section above.
7817323adacSDevin Teske#
7827323adacSDevin Teskef_dhcp_parse_leases()
7837323adacSDevin Teske{
7847323adacSDevin Teske	local leasefile="$1" struct_name="$2"
7857323adacSDevin Teske
7867323adacSDevin Teske	[ "$struct_name" ] || return $FAILURE
7877323adacSDevin Teske
7887323adacSDevin Teske	if [ ! -e "$leasefile" ]; then
7897323adacSDevin Teske		f_dprintf "%s: No such file or directory" "$leasefile"
7907323adacSDevin Teske		return $FAILURE
7917323adacSDevin Teske	fi
7927323adacSDevin Teske
7937323adacSDevin Teske	f_struct "$struct_name" && f_struct_free "$struct_name"
7947323adacSDevin Teske	f_struct_new DHCP_LEASE "$struct_name"
7957323adacSDevin Teske
7967323adacSDevin Teske	eval "$( awk -v struct="$struct_name" '
7977323adacSDevin Teske		BEGIN {
7987323adacSDevin Teske			lease_found = 0
7997323adacSDevin Teske			keyword_list = " \
8007323adacSDevin Teske				interface	\
8017323adacSDevin Teske				fixed-address	\
8027323adacSDevin Teske				filename	\
8037323adacSDevin Teske				server-name	\
8047323adacSDevin Teske				script		\
8057323adacSDevin Teske				medium		\
8067323adacSDevin Teske			"
8077323adacSDevin Teske			split(keyword_list, keywords, FS)
8087323adacSDevin Teske
8097323adacSDevin Teske			time_list = "renew rebind expire"
8107323adacSDevin Teske			split(time_list, times, FS)
8117323adacSDevin Teske
8127323adacSDevin Teske			option_list = " \
8137323adacSDevin Teske				host-name		\
8147323adacSDevin Teske				subnet-mask		\
8157323adacSDevin Teske				routers			\
8167323adacSDevin Teske				domain-name-servers	\
8177323adacSDevin Teske				domain-name		\
8187323adacSDevin Teske				broadcast-address	\
8197323adacSDevin Teske				dhcp-lease-time		\
8207323adacSDevin Teske				dhcp-message-type	\
8217323adacSDevin Teske				dhcp-server-identifier	\
8227323adacSDevin Teske				dhcp-renewal-time	\
8237323adacSDevin Teske				dhcp-rebinding-time	\
8247323adacSDevin Teske			"
8257323adacSDevin Teske			split(option_list, options, FS)
8267323adacSDevin Teske		}
8277323adacSDevin Teske		function set_value(prop,value)
8287323adacSDevin Teske		{
8297323adacSDevin Teske			lease_found = 1
8307323adacSDevin Teske			gsub(/[^[:alnum:]_]/, "_", prop)
8317323adacSDevin Teske			sub(/;$/, "", value)
8327323adacSDevin Teske			sub(/^"/, "", value)
8337323adacSDevin Teske			sub(/"$/, "", value)
8347323adacSDevin Teske			sub(/,.*/, "", value)
8357323adacSDevin Teske			printf "%s set %s \"%s\"\n", struct, prop, value
8367323adacSDevin Teske		}
8377323adacSDevin Teske		/^lease {$/, /^}$/ \
8387323adacSDevin Teske		{
8397323adacSDevin Teske			if ( $0 ~ /^lease {$/ ) next
8407323adacSDevin Teske			if ( $0 ~ /^}$/ ) exit
8417323adacSDevin Teske
8427323adacSDevin Teske			for (k in keywords)
8437323adacSDevin Teske			{
8447323adacSDevin Teske				keyword = keywords[k]
8457323adacSDevin Teske				if ( $1 == keyword )
8467323adacSDevin Teske				{
8477323adacSDevin Teske					set_value(keyword, $2)
8487323adacSDevin Teske					next
8497323adacSDevin Teske				}
8507323adacSDevin Teske			}
8517323adacSDevin Teske
8527323adacSDevin Teske			for (t in times)
8537323adacSDevin Teske			{
8547323adacSDevin Teske				time = times[t]
8557323adacSDevin Teske				if ( $1 == time )
8567323adacSDevin Teske				{
8577323adacSDevin Teske					set_value(time, $2 " " $3 " " $4)
8587323adacSDevin Teske					next
8597323adacSDevin Teske				}
8607323adacSDevin Teske			}
8617323adacSDevin Teske
8627323adacSDevin Teske			if ( $1 != "option" ) next
8637323adacSDevin Teske			for (o in options)
8647323adacSDevin Teske			{
8657323adacSDevin Teske				option = options[o]
8667323adacSDevin Teske				if ( $2 == option )
8677323adacSDevin Teske				{
8687323adacSDevin Teske					set_value(option, $3)
8697323adacSDevin Teske					next
8707323adacSDevin Teske				}
8717323adacSDevin Teske			}
8727323adacSDevin Teske		}
8737323adacSDevin Teske		EXIT {
8747323adacSDevin Teske			if ( ! lease_found )
8757323adacSDevin Teske			{
8767323adacSDevin Teske				printf "f_struct_free \"%s\"\n", struct
8777323adacSDevin Teske				print "return $FAILURE"
8787323adacSDevin Teske			}
8797323adacSDevin Teske		}
8807323adacSDevin Teske	' "$leasefile" )"
8817323adacSDevin Teske}
8827323adacSDevin Teske
8837323adacSDevin Teske# f_dhcp_get_info $interface
8847323adacSDevin Teske#
8857323adacSDevin Teske# Parse the dhclient(8) lease database for $interface to obtain all the
8867323adacSDevin Teske# necessary IPv4 details necessary to communicate on the network. The retrieved
8877323adacSDevin Teske# information is stored in VAR_IPADDR, VAR_NETMASK, VAR_GATEWAY, and
8887323adacSDevin Teske# VAR_NAMESERVER.
8897323adacSDevin Teske#
8907323adacSDevin Teske# If reading the lease database fails, values are obtained from ifconfig(8) and
8917323adacSDevin Teske# route(8). If the DHCP lease did not provide a nameserver (or likewise, we
8927323adacSDevin Teske# were unable to parse the lease database), fall-back to resolv.conf(5) for
8937323adacSDevin Teske# obtaining the nameserver. Always returns success.
8947323adacSDevin Teske#
8957323adacSDevin Teskef_dhcp_get_info()
8967323adacSDevin Teske{
8977323adacSDevin Teske	local interface="$1" cp
8987323adacSDevin Teske	local leasefile="/var/db/dhclient.leases.$interface"
8997323adacSDevin Teske
9007323adacSDevin Teske	# If it fails, do it the old-fashioned way
9017323adacSDevin Teske	if f_dhcp_parse_leases "$leasefile" lease; then
9027323adacSDevin Teske		lease get fixed_address $VAR_IPADDR
9037323adacSDevin Teske		lease get subnet_mask $VAR_NETMASK
9047323adacSDevin Teske		lease get routers cp
9057323adacSDevin Teske		setvar $VAR_GATEWAY "${cp%%,*}"
9067323adacSDevin Teske		lease get domain_name_servers cp
9077323adacSDevin Teske		setvar $VAR_NAMESERVER "${cp%%,*}"
9087323adacSDevin Teske		lease get host_name cp &&
9097323adacSDevin Teske			setvar $VAR_HOSTNAME "$cp"
9107323adacSDevin Teske		f_struct_free lease
9117323adacSDevin Teske	else
9127323adacSDevin Teske		# Bah, now we have to get the information from ifconfig
9137323adacSDevin Teske		if f_debugging; then
9147323adacSDevin Teske			f_dprintf "DHCP configured interface returns %s" \
9157323adacSDevin Teske			          "$( ifconfig "$interface" )"
9167323adacSDevin Teske		fi
9177323adacSDevin Teske		f_ifconfig_inet "$interface" $VAR_IPADDR
9187323adacSDevin Teske		f_ifconfig_netmask "$interface" $VAR_NETMASK
9197323adacSDevin Teske		f_route_get_default $VAR_GATEWAY
9207323adacSDevin Teske	fi
9217323adacSDevin Teske
9227323adacSDevin Teske	# If we didn't get a name server value, hunt for it in resolv.conf
9237323adacSDevin Teske	local ns
9247323adacSDevin Teske	if [ -r "$RESOLV_CONF" ] && ! {
9257323adacSDevin Teske		f_getvar $VAR_NAMESERVER ns || [ "$ns" ]
9267323adacSDevin Teske	}; then
9277323adacSDevin Teske		f_resolv_conf_nameservers cp &&
9287323adacSDevin Teske			setvar $VAR_NAMESERVER ${cp%%[$IFS]*}
9297323adacSDevin Teske	fi
9307323adacSDevin Teske
9317323adacSDevin Teske	return $SUCCESS
9327323adacSDevin Teske}
9337323adacSDevin Teske
9347323adacSDevin Teske# f_rtsol_get_info $interface
9357323adacSDevin Teske#
9367323adacSDevin Teske# Returns the rtsol-provided IPv6 address associated with $interface. The
9377323adacSDevin Teske# retrieved IP address is stored in VAR_IPV6ADDR. Always returns success.
9387323adacSDevin Teske#
9397323adacSDevin Teskef_rtsol_get_info()
9407323adacSDevin Teske{
9417323adacSDevin Teske	local interface="$1" cp
9427323adacSDevin Teske	cp=$( ifconfig "$interface" 2> /dev/null | awk \
9437323adacSDevin Teske	'
9447323adacSDevin Teske		BEGIN { found = 0 }
9457323adacSDevin Teske		( $1 == "inet6" ) && ( $2 ~ /^fe80:/ ) \
9467323adacSDevin Teske		{
9477323adacSDevin Teske			print $2
9487323adacSDevin Teske			found = 1
9497323adacSDevin Teske			exit
9507323adacSDevin Teske		}
9517323adacSDevin Teske		END { exit ! found }
9527323adacSDevin Teske	' ) && setvar $VAR_IPV6ADDR "$cp"
9537323adacSDevin Teske}
9547323adacSDevin Teske
9557323adacSDevin Teske# f_host_lookup $host [$var_to_set]
9567323adacSDevin Teske#
9577323adacSDevin Teske# Use host(1) to lookup (or reverse) an Internet number from (or to) a name.
9587323adacSDevin Teske# Multiple answers are returned separated by a single space. If host(1) does
9597323adacSDevin Teske# not exit cleanly, its full output is provided and the return status is 1.
9607323adacSDevin Teske#
9617323adacSDevin Teske# If nsswitch.conf(5) has been configured to query local access first for the
9627323adacSDevin Teske# `hosts' database, we'll manually check hosts(5) first (preventing host(1)
9637323adacSDevin Teske# from hanging in the event that DNS goes awry).
9647323adacSDevin Teske#
9657323adacSDevin Teske# If $var_to_set is missing or NULL, the list of IP addresses is printed to
9667323adacSDevin Teske# standard output for capturing in a sub-shell (which is less-recommended
9677323adacSDevin Teske# because of performance degredation; for example, when called in a loop).
9687323adacSDevin Teske#
9697323adacSDevin Teske# The variables from variable.subr used in looking up the host are as follows
9707323adacSDevin Teske# (which are set manually):
9717323adacSDevin Teske#
9727323adacSDevin Teske# 	VAR_IPV6_ENABLE [Optional]
9737323adacSDevin Teske# 		If set to "YES", enables the lookup of IPv6 addresses and IPv4
9747323adacSDevin Teske# 		address. IPv6 addresses, if any, will come before IPv4. Note
9757323adacSDevin Teske# 		that if nsswitch.conf(5) shows an affinity for "files" for the
9767323adacSDevin Teske# 		"host" database and there is a valid entry in hosts(5) for
9777323adacSDevin Teske# 		$host, this setting currently has no effect (an IPv4 address
9787323adacSDevin Teske# 		can supersede an IPv6 address). By design, hosts(5) overrides
9797323adacSDevin Teske# 		any preferential treatment. Otherwise, if this variable is not
9807323adacSDevin Teske# 		set, IPv6 addresses will not be used (IPv4 addresses will
9817323adacSDevin Teske# 		specifically be requested from DNS).
9827323adacSDevin Teske#
9837323adacSDevin Teske# This function is a two-parter. Below is the awk(1) portion of the function,
9847323adacSDevin Teske# afterward is the sh(1) function which utilizes the below awk script.
9857323adacSDevin Teske#
9867323adacSDevin Teskef_host_lookup_awk='
9877323adacSDevin TeskeBEGIN{ addrs = "" }
9887323adacSDevin Teske!/^[[:space:]]*(#|$)/ \
9897323adacSDevin Teske{
9907323adacSDevin Teske	for (n=1; n++ < NF;) if ($n == name)
9917323adacSDevin Teske		addrs = addrs (addrs ? " " : "") $1
9927323adacSDevin Teske}
9937323adacSDevin TeskeEND {
9947323adacSDevin Teske	if (addrs) print addrs
9957323adacSDevin Teske	exit !addrs
9967323adacSDevin Teske}
9977323adacSDevin Teske'
9987323adacSDevin Teskef_host_lookup()
9997323adacSDevin Teske{
10007323adacSDevin Teske	local __host="$1" __var_to_set="$2"
10017323adacSDevin Teske	f_dprintf "f_host_lookup: host=[%s]" "$__host"
10027323adacSDevin Teske
10037323adacSDevin Teske	# If we're configured to look at local files first, do that
10047323adacSDevin Teske	if awk '/^hosts:/{exit !($2=="files")}' "$NSSWITCH_CONF"; then
10057323adacSDevin Teske		if [ "$__var_to_set" ]; then
10067323adacSDevin Teske			local __cp
10077323adacSDevin Teske			if __cp=$( awk -v name="$__host" \
10087323adacSDevin Teske				"$f_host_lookup_awk" "$ETC_HOSTS" )
10097323adacSDevin Teske			then
10107323adacSDevin Teske				setvar "$__var_to_set" "$__cp"
10117323adacSDevin Teske				return $SUCCESS
10127323adacSDevin Teske			fi
10137323adacSDevin Teske		else
10147323adacSDevin Teske			awk -v name="$__host" \
10157323adacSDevin Teske				"$f_host_lookup_awk" "$ETC_HOSTS" &&
10167323adacSDevin Teske				return $SUCCESS
10177323adacSDevin Teske		fi
10187323adacSDevin Teske	fi
10197323adacSDevin Teske
10207323adacSDevin Teske	#
10217323adacSDevin Teske	# Fall back to host(1) -- which is further governed by nsswitch.conf(5)
10227323adacSDevin Teske	#
10237323adacSDevin Teske
10244b939b11SDevin Teske	local __output __ip6 __addrs=
10257323adacSDevin Teske	f_getvar $VAR_IPV6_ENABLE __ip6
10264b939b11SDevin Teske
10274b939b11SDevin Teske	# If we have a TCP media type configured, check for an SRV record
10284b939b11SDevin Teske	local __srvtypes=
10294b939b11SDevin Teske	{ f_quietly f_getvar $VAR_HTTP_PATH ||
10304b939b11SDevin Teske	  f_quietly f_getvar $VAR_HTTP_PROXY_PATH
10314b939b11SDevin Teske	} && __srvtypes="$__srvtypes _http._tcp"
10324b939b11SDevin Teske	f_quietly f_getvar $VAR_NFS_PATH &&
10334b939b11SDevin Teske		__srvtypes="$__srvtypes _nfs._tcp _nfs._udp"
10344b939b11SDevin Teske
10354b939b11SDevin Teske	# Calculate wait time as dividend of total time and host(1) invocations
10364b939b11SDevin Teske	local __host_runs __wait
1037d4ae33f0SDevin Teske	f_count __host_runs $__srvtypes
10384b939b11SDevin Teske	if [ "$__ip6" = "YES" ]; then
1039d4ae33f0SDevin Teske		__host_runs=$(( $__host_runs + 2 ))
10404b939b11SDevin Teske	else
1041d4ae33f0SDevin Teske		__host_runs=$(( $__host_runs + 1 ))
10424b939b11SDevin Teske	fi
10434b939b11SDevin Teske	f_getvar $VAR_MEDIA_TIMEOUT __wait
10444b939b11SDevin Teske	[ "$__wait" ] && __wait="-W $(( $__wait / $__host_runs ))"
10454b939b11SDevin Teske
10464b939b11SDevin Teske	# Query SRV types first (1st host response taken as new host to query)
10474b939b11SDevin Teske	for __type in $__srvtypes; do
10484b939b11SDevin Teske		if __output=$(
10494b939b11SDevin Teske			host -t SRV $__wait -- "$__type.$__host" \
10504b939b11SDevin Teske			2> /dev/null
10514b939b11SDevin Teske		); then
10524b939b11SDevin Teske			__host=$( echo "$__output" |
10534b939b11SDevin Teske					awk '/ SRV /{print $NF;exit}' )
10544b939b11SDevin Teske			break
10554b939b11SDevin Teske		fi
10564b939b11SDevin Teske	done
10574b939b11SDevin Teske
10584b939b11SDevin Teske	# Try IPv6 first (if enabled)
10597323adacSDevin Teske	if [ "$__ip6" = "YES" ]; then
10607323adacSDevin Teske		if ! __output=$( host -t AAAA $__wait -- "$__host" 2>&1 ); then
10617323adacSDevin Teske			# An error occurred, display in-full and return error
10627323adacSDevin Teske			[ "$__var_to_set" ] &&
10637323adacSDevin Teske				setvar "$__var_to_set" "$__output"
10647323adacSDevin Teske			return $FAILURE
10657323adacSDevin Teske		fi
10664b939b11SDevin Teske		# Add the IPv6 addresses and fall-through to collect IPv4 too
10677323adacSDevin Teske		__addrs=$( echo "$__output" | awk '/ address /{print $NF}' )
10687323adacSDevin Teske	fi
10694b939b11SDevin Teske
10704b939b11SDevin Teske	# Good ol' IPv4
10717323adacSDevin Teske	if ! __output=$( host -t A $__wait -- "$__host" 2>&1 ); then
10727323adacSDevin Teske		# An error occurred, display it in-full and return error
10737323adacSDevin Teske		[ "$__var_to_set" ] && setvar "$__var_to_set" "$__output"
10747323adacSDevin Teske		return $FAILURE
10757323adacSDevin Teske	fi
10764b939b11SDevin Teske
10777323adacSDevin Teske	__addrs="$__addrs${__addrs:+ }$(
10787323adacSDevin Teske		echo "$__output" | awk '/ address /{print $NF}' )"
10797323adacSDevin Teske	if [ "$__var_to_set" ]; then
10807323adacSDevin Teske		setvar "$__var_to_set" "$__addrs"
10817323adacSDevin Teske	else
10827323adacSDevin Teske		echo $__addrs
10837323adacSDevin Teske	fi
10847323adacSDevin Teske}
10857323adacSDevin Teske
10867323adacSDevin Teske# f_device_dialog_tcp $device
10877323adacSDevin Teske#
10887323adacSDevin Teske# This is it - how to get TCP setup values. Prompt the user to edit/confirm the
10897323adacSDevin Teske# interface, gateway, nameserver, and hostname settings -- all required for
10907323adacSDevin Teske# general TCP/IP access.
10917323adacSDevin Teske#
10927323adacSDevin Teske# Variables from variable.subr that can be used to sript user input:
10937323adacSDevin Teske#
10947323adacSDevin Teske# 	VAR_NO_INET6
10957323adacSDevin Teske# 		If set, prevents asking the user if they would like to use
10967323adacSDevin Teske# 		rtsol(8) to check for an IPv6 router.
10977323adacSDevin Teske# 	VAR_TRY_RTSOL
10987323adacSDevin Teske# 		If set to "YES" (and VAR_NONINTERACTIVE is unset), asks the
10997323adacSDevin Teske# 		user if they would like to try the IPv6 RouTer SOLicitation
11007323adacSDevin Teske# 		utility (rtsol(8)) to get IPv6 information. Ignored if
11017323adacSDevin Teske# 		VAR_NO_INET6 is set.
11027323adacSDevin Teske# 	VAR_TRY_DHCP
11037323adacSDevin Teske# 		If set to "YES" (and VAR_NONINTERACTIVE is unset), asks the
11047323adacSDevin Teske# 		user if they would like to try to acquire IPv4 connection
11057323adacSDevin Teske# 		settings from a DHCP server using dhclient(8).
11067323adacSDevin Teske#
11077323adacSDevin Teske# 	VAR_GATEWAY	Default gateway to use.
11087323adacSDevin Teske# 	VAR_IPADDR	Interface address to assign.
11097323adacSDevin Teske# 	VAR_NETMASK	Interface subnet mask.
11107323adacSDevin Teske# 	VAR_EXTRAS	Extra interface options to ifconfig(8).
11117323adacSDevin Teske# 	VAR_HOSTNAME	Hostname to set.
11127323adacSDevin Teske# 	VAR_DOMAINNAME	Domain name to use.
11137323adacSDevin Teske# 	VAR_NAMESERVER	DNS nameserver to use when making lookups.
11147323adacSDevin Teske# 	VAR_IPV6ADDR	IPv6 interface address.
11157323adacSDevin Teske#
11167323adacSDevin Teske# In addition, the following variables are used in acquiring network settings
11177323adacSDevin Teske# from the user:
11187323adacSDevin Teske#
11197323adacSDevin Teske# 	VAR_NONINTERACTIVE
11207323adacSDevin Teske# 		If set (such as when running in a script), prevents asking the
11217323adacSDevin Teske# 		user questions or displaying the usual prompts, etc.
11227323adacSDevin Teske# 	VAR_NETINTERACTIVE
11237323adacSDevin Teske# 		The one exception to VAR_NONINTERACTIVE is VAR_NETINTERACTIVE,
11247323adacSDevin Teske# 		which if set will prompt the user to try RTSOL (unless
11257323adacSDevin Teske# 		VAR_TRY_RTSOL has been set), try DHCP (unless VAR_TRY_DHCP has
11267323adacSDevin Teske# 		been set), and display the network verification dialog. This
11277323adacSDevin Teske# 		allows you to have a mostly non-interactive script that still
11287323adacSDevin Teske# 		prompts for network setup/confirmation.
11297323adacSDevin Teske#
1130*ec65e4f8SPedro F. Giffuni# After successful execution, the following variables are set:
11317323adacSDevin Teske#
11327323adacSDevin Teske# 	VAR_IFCONFIG + $device (e.g., `ifconfig_em0')
11337323adacSDevin Teske#               Defines the ifconfig(8) properties specific to $device.
11347323adacSDevin Teske#
11357323adacSDevin Teskef_device_dialog_tcp()
11367323adacSDevin Teske{
11379ecd54f2SDevin Teske	local dev="$1" devname cp n
11387323adacSDevin Teske	local use_dhcp="" use_rtsol=""
11397323adacSDevin Teske	local _ipaddr _netmask _extras
11407323adacSDevin Teske
1141f677a9e2SDevin Teske	[ "$dev" ] || return $DIALOG_CANCEL
11429ecd54f2SDevin Teske	f_struct "$dev" get name devname || return $DIALOG_CANCEL
11437323adacSDevin Teske
11447323adacSDevin Teske	# Initialize vars from previous device values
11457323adacSDevin Teske	local private
11469ecd54f2SDevin Teske	$dev get private private
11477323adacSDevin Teske	if [ "$private" ] && f_struct "$private"; then
11487323adacSDevin Teske		$private get ipaddr    _ipaddr
11497323adacSDevin Teske		$private get netmask   _netmask
11507323adacSDevin Teske		$private get extras    _extras
11517323adacSDevin Teske		$private get use_dhcp  use_dhcp
11527323adacSDevin Teske		$private get use_rtsol use_rtsol
11537323adacSDevin Teske	else # See if there are any defaults
11547323adacSDevin Teske
11557323adacSDevin Teske		#
11567323adacSDevin Teske		# This is a hack so that the dialogs below are interactive in a
11577323adacSDevin Teske		# script if we have requested interactive behavior.
11587323adacSDevin Teske		#
11597323adacSDevin Teske		local old_interactive=
11607323adacSDevin Teske		if ! f_interactive && f_netinteractive; then
11617323adacSDevin Teske			f_getvar $VAR_NONINTERACTIVE old_interactive
11627323adacSDevin Teske			unset $VAR_NONINTERACTIVE
11637323adacSDevin Teske		fi
11647323adacSDevin Teske
11657323adacSDevin Teske		#
11667323adacSDevin Teske		# Try a RTSOL 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 try6
117280433743SDevin Teske		if ! f_isset $VAR_NO_INET6 && {
11737323adacSDevin Teske		   { f_getvar $VAR_TRY_RTSOL try6 && [ "$try6" = "YES" ]; } ||
11747323adacSDevin Teske		   {
117580433743SDevin Teske			# Only prompt the user when VAR_TRY_RTSOL is unset
117680433743SDevin Teske			! f_isset $VAR_TRY_RTSOL &&
11777323adacSDevin Teske				f_dialog_noyes "$msg_try_ipv6_configuration"
11787323adacSDevin Teske		   }
11797323adacSDevin Teske		}; then
11807323adacSDevin Teske			local i
11817323adacSDevin Teske
11827323adacSDevin Teske			f_quietly sysctl net.inet6.ip6.forwarding=0
11837323adacSDevin Teske			f_quietly sysctl net.inet6.ip6.accept_rtadv=1
11849ecd54f2SDevin Teske			f_quietly ifconfig $devname up
11857323adacSDevin Teske
11867323adacSDevin Teske			i=$( sysctl -n net.inet6.ip6.dad_count )
11877323adacSDevin Teske			sleep $(( $i + 1 ))
11887323adacSDevin Teske
11897323adacSDevin Teske			f_quietly mkdir -p /var/run
11907323adacSDevin Teske			f_dialog_info "$msg_scanning_for_ra_servers"
11919ecd54f2SDevin Teske			if f_quietly rtsol $devname; then
11927323adacSDevin Teske				i=$( sysctl -n net.inet6.ip6.dad_count )
11937323adacSDevin Teske				sleep $(( $i + 1 ))
11949ecd54f2SDevin Teske				f_rtsol_get_info $devname
11957323adacSDevin Teske				use_rtsol=1
11967323adacSDevin Teske			else
11977323adacSDevin Teske				use_rtsol=
11987323adacSDevin Teske			fi
11997323adacSDevin Teske		fi
12007323adacSDevin Teske
12017323adacSDevin Teske		#
12027323adacSDevin Teske		# Try a DHCP scan if such behavior is desired.
12037323adacSDevin Teske		# If the variable was configured and is YES, do it.
12047323adacSDevin Teske		# If it was configured to anything else, treat it as NO.
12057323adacSDevin Teske		# Otherwise, ask the question interactively.
12067323adacSDevin Teske		#
12077323adacSDevin Teske		local try4
12087323adacSDevin Teske		if { f_getvar $VAR_TRY_DHCP try4 && [ "$try4" = "YES" ]; } || {
120980433743SDevin Teske			# Only prompt the user when VAR_TRY_DHCP is unset
121080433743SDevin Teske			! f_isset $VAR_TRY_DHCP &&
12117323adacSDevin Teske				f_dialog_noyes "$msg_try_dhcp_configuration"
12127323adacSDevin Teske		}; then
12139ecd54f2SDevin Teske			f_quietly ifconfig $devname delete
12147323adacSDevin Teske			f_quietly mkdir -p /var/db
12157323adacSDevin Teske			f_quietly mkdir -p /var/run
12167323adacSDevin Teske			f_quietly mkdir -p /tmp
12177323adacSDevin Teske
12187323adacSDevin Teske			local msg="$msg_scanning_for_dhcp_servers"
12197323adacSDevin Teske			trap - SIGINT
12207323adacSDevin Teske			( # Execute in sub-shell to allow/catch Ctrl-C
12217323adacSDevin Teske			  trap 'exit $FAILURE' SIGINT
12227323adacSDevin Teske			  if [ "$USE_XDIALOG" ]; then
12239ecd54f2SDevin Teske			  	f_quietly dhclient $devname |
12247323adacSDevin Teske			  			f_xdialog_info "$msg"
12257323adacSDevin Teske			  else
12267323adacSDevin Teske			  	f_dialog_info "$msg"
12279ecd54f2SDevin Teske			  	f_quietly dhclient $devname
12287323adacSDevin Teske			  fi
12297323adacSDevin Teske			)
12307323adacSDevin Teske			local retval=$?
12317323adacSDevin Teske			trap 'f_interrupt' SIGINT
12327323adacSDevin Teske			if [ $retval -eq $SUCCESS ]; then
12339ecd54f2SDevin Teske				f_dhcp_get_info $devname
12347323adacSDevin Teske				use_dhcp=1
12357323adacSDevin Teske			else
12367323adacSDevin Teske				use_dhcp=
12377323adacSDevin Teske			fi
12387323adacSDevin Teske		fi
12397323adacSDevin Teske
12407323adacSDevin Teske		# Restore old VAR_NONINTERACTIVE if needed.
12417323adacSDevin Teske		[ "$old_interactive" ] &&
12427323adacSDevin Teske			setvar $VAR_NONINTERACTIVE "$old_interactive"
12437323adacSDevin Teske
12447323adacSDevin Teske		# Special hack so it doesn't show up oddly in the menu
12457323adacSDevin Teske		local gw
12467323adacSDevin Teske		if f_getvar $VAR_GATEWAY gw && [ "$gw" = "NO" ]; then
12477323adacSDevin Teske			setvar $VAR_GATEWAY ""
12487323adacSDevin Teske		fi
12497323adacSDevin Teske
12507323adacSDevin Teske		# Get old IP address from variable space, if available
12517323adacSDevin Teske		if [ ! "$_ipaddr" ]; then
12527323adacSDevin Teske			if f_getvar $VAR_IPADDR cp; then
12537323adacSDevin Teske				_ipaddr="$cp"
12549ecd54f2SDevin Teske			elif f_getvar ${devname}_$VAR_IPADDR cp; then
12557323adacSDevin Teske				_ipaddr="$cp"
12567323adacSDevin Teske			fi
12577323adacSDevin Teske		fi
12587323adacSDevin Teske
12597323adacSDevin Teske		# Get old netmask from variable space, if available
12607323adacSDevin Teske		if [ ! "$_netmask" ]; then
12617323adacSDevin Teske			if f_getvar $VAR_NETMASK cp; then
12627323adacSDevin Teske				_netmask="$cp"
12639ecd54f2SDevin Teske			elif f_getvar ${devname}_$VAR_NETMASK cp; then
12647323adacSDevin Teske				_netmask="$cp"
12657323adacSDevin Teske			fi
12667323adacSDevin Teske		fi
12677323adacSDevin Teske
12687323adacSDevin Teske		# Get old extras string from variable space, if available
12697323adacSDevin Teske		if [ ! "$_extras" ]; then
12707323adacSDevin Teske			if f_getvar $VAR_EXTRAS cp; then
12717323adacSDevin Teske				_extras="$cp"
12729ecd54f2SDevin Teske			elif f_getvar ${devname}_$VAR_EXTRAS cp; then
12737323adacSDevin Teske				_extras="$cp"
12747323adacSDevin Teske			fi
12757323adacSDevin Teske		fi
12767323adacSDevin Teske	fi
12777323adacSDevin Teske
12787323adacSDevin Teske	# Look up values already recorded with the system, or blank the string
12797323adacSDevin Teske	# variables ready to accept some new data
12807323adacSDevin Teske	local _hostname _gateway _nameserver
12817323adacSDevin Teske	f_getvar $VAR_HOSTNAME _hostname
12827323adacSDevin Teske	case "$_hostname" in
12837323adacSDevin Teske	*.*) : do nothing ;; # Already fully-qualified
12847323adacSDevin Teske	*)
12857323adacSDevin Teske		f_getvar $VAR_DOMAINNAME cp
12867323adacSDevin Teske		[ "$cp" ] && _hostname="$_hostname.$cp"
12877323adacSDevin Teske	esac
12887323adacSDevin Teske	f_getvar $VAR_GATEWAY _gateway
12897323adacSDevin Teske	f_getvar $VAR_NAMESERVER _nameserver
12907323adacSDevin Teske
12917323adacSDevin Teske	# Re-check variables for initial inheritance before heading into dialog
12927323adacSDevin Teske	[ "$_hostname" ] || _hostname="${HOSTNAME:-$( hostname )}"
12937323adacSDevin Teske	[ "$_gateway" ] || f_route_get_default _gateway
12947323adacSDevin Teske	[ ! "$_nameserver" ] &&
12957323adacSDevin Teske		f_resolv_conf_nameservers cp && _nameserver=${cp%%[$IFS]*}
12969ecd54f2SDevin Teske	[ "$_ipaddr" ] || f_ifconfig_inet $devname _ipaddr
12979ecd54f2SDevin Teske	[ "$_netmask" ] || f_ifconfig_netmask $devname _netmask
12987323adacSDevin Teske
12997323adacSDevin Teske	# If non-interactive, jump over dialog section and into config section
13007323adacSDevin Teske	if f_netinteractive || f_interactive || [ ! "$_hostname" ]
13017323adacSDevin Teske	then
13027323adacSDevin Teske		[ ! "$_hostname" ] && f_interactive &&
13037079fc4eSDevin Teske			f_show_msg "$msg_hostname_variable_not_set"
13047323adacSDevin Teske
13057323adacSDevin Teske		local title=" $msg_network_configuration "
13067323adacSDevin Teske		local hline="$hline_alnum_arrows_punc_tab_enter"
13077323adacSDevin Teske		local extras_help="$tcplayout_extras_help"
13087323adacSDevin Teske
13097323adacSDevin Teske		# Modify the help line for PLIP config
13109ecd54f2SDevin Teske		[ "${devname#plip}" != "$devname" ] &&
13117323adacSDevin Teske			extras_help="$tcplayout_extras_help_for_plip"
13127323adacSDevin Teske
13137323adacSDevin Teske		f_getvar $VAR_IPV6ADDR cp && [ "$cp" ] &&
13147323adacSDevin Teske			title="$title($msg_ipv6_ready) "
13157323adacSDevin Teske
13167323adacSDevin Teske		if [ ! "$USE_XDIALOG" ]; then
13177323adacSDevin Teske			local prompt="$msg_dialog_mixedform_navigation_help"
13187323adacSDevin Teske			# Calculate center position for displaying device label
13199ecd54f2SDevin Teske			local devlabel="$msg_configuration_for_interface"
13209ecd54f2SDevin Teske			devlabel="$devlabel $devname"
13217323adacSDevin Teske			local width=54
13227323adacSDevin Teske			local n=$(( $width/2 - (${#devlabel} + 4)/2 - 2 ))
13237323adacSDevin Teske
13247323adacSDevin Teske			while :; do
13257323adacSDevin Teske				cp=$( $DIALOG \
13267323adacSDevin Teske					--title "$title"                     \
13277323adacSDevin Teske					--backtitle "$DIALOG_BACKTITLE"      \
13287323adacSDevin Teske					--hline "$hline"                     \
13297323adacSDevin Teske					--item-help                          \
13307323adacSDevin Teske					--ok-label "$msg_ok"                 \
13317323adacSDevin Teske					--cancel-label "$msg_cancel"         \
13327323adacSDevin Teske					--help-button                        \
13337323adacSDevin Teske					--help-label "$msg_help"             \
13347323adacSDevin Teske					--mixedform "$prompt" 16 $width 9    \
13357323adacSDevin Teske					"$msg_host_name_including_domain:" 1 2 \
13367323adacSDevin Teske						"$_hostname" 2 3 45 255 0    \
13377323adacSDevin Teske						"$tcplayout_hostname_help"   \
13387323adacSDevin Teske					"$msg_ipv4_gateway:" 3 2             \
13397323adacSDevin Teske						"$_gateway" 4 3 16 15 0      \
13407323adacSDevin Teske						"$tcplayout_gateway_help"    \
13417323adacSDevin Teske					"$msg_name_server:" 3 31             \
13427323adacSDevin Teske						"$_nameserver" 4 32 16 15 0  \
13437323adacSDevin Teske						"$tcplayout_nameserver_help" \
13447323adacSDevin Teske					"- $devlabel -" 5 $n "" 0 0 0 0 3 "" \
13457323adacSDevin Teske					"$msg_ipv4_address:" 6 6             \
13467323adacSDevin Teske						"$_ipaddr" 7 7 16 15 0       \
13477323adacSDevin Teske						"$tcplayout_ipaddr_help"     \
13487323adacSDevin Teske					"$msg_netmask:" 6 31                 \
13497323adacSDevin Teske						"$_netmask" 7 32 16 15 0     \
13507323adacSDevin Teske						"$tcplayout_netmask_help"    \
13517323adacSDevin Teske					"$msg_extra_options_to_ifconfig" 8 6 \
13527323adacSDevin Teske						"$_extras" 9 7 41 2048 0     \
13537323adacSDevin Teske						"$extras_help"               \
13547323adacSDevin Teske					2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD )
13557323adacSDevin Teske
13567323adacSDevin Teske				# --mixed-form always returns 0, we have to
13577323adacSDevin Teske				# use the returned data to determine button
13587323adacSDevin Teske				if [ ! "$cp" ]; then
13597323adacSDevin Teske					# User either chose "Cancel", pressed
13607323adacSDevin Teske					# ESC, or blanked every form field
1361f677a9e2SDevin Teske					return $DIALOG_CANCEL
13627323adacSDevin Teske				else
13637323adacSDevin Teske					n=$( echo "$cp" | f_number_of_lines )
13647323adacSDevin Teske					[ $n -eq 1 ] && case "$cp" in HELP*)
13657323adacSDevin Teske						# User chose "Help"
13667323adacSDevin Teske						f_show_help "$TCP_HELPFILE"
13677323adacSDevin Teske						continue
13687323adacSDevin Teske					esac
13697323adacSDevin Teske				fi
13707323adacSDevin Teske
13717323adacSDevin Teske				# Turn mixed-form results into env variables
13727323adacSDevin Teske				eval "$( echo "$cp" | awk '
13737323adacSDevin Teske				BEGIN {
13747323adacSDevin Teske					n = 0
13757323adacSDevin Teske					field[++n] = "_hostname"
13767323adacSDevin Teske					field[++n] = "_gateway"
13777323adacSDevin Teske					field[++n] = "_nameserver"
13787323adacSDevin Teske					field[++n] = "_ipaddr"
13797323adacSDevin Teske					field[++n] = "_netmask"
13807323adacSDevin Teske					field[++n] = "_extras"
13817323adacSDevin Teske					nfields = n
13827323adacSDevin Teske					n = 0
13837323adacSDevin Teske				}
13847323adacSDevin Teske				{
13857323adacSDevin Teske					gsub(/'\''/, "'\'\\\\\'\''")
13867323adacSDevin Teske					sub(/[[:space:]]*$/, "")
13877323adacSDevin Teske					value[field[++n]] = $0
13887323adacSDevin Teske				}
13897323adacSDevin Teske				END {
13907323adacSDevin Teske					for ( n = 1; n <= nfields; n++ )
13917323adacSDevin Teske					{
13927323adacSDevin Teske						printf "%s='\''%s'\'';\n",
13937323adacSDevin Teske						       field[n],
13947323adacSDevin Teske						       value[field[n]]
13957323adacSDevin Teske					}
13967323adacSDevin Teske				}' )"
13977323adacSDevin Teske
13987323adacSDevin Teske				f_dialog_validate_tcpip \
13997323adacSDevin Teske					"$_hostname" \
14007323adacSDevin Teske					"$_gateway" \
14017323adacSDevin Teske					"$_nameserver" \
14027323adacSDevin Teske					"$_ipaddr" \
14037323adacSDevin Teske					"$_netmask" \
14047323adacSDevin Teske					&& break
14057323adacSDevin Teske			done
14067323adacSDevin Teske		else
14077323adacSDevin Teske			# Xdialog(1) does not support --mixed-form
14087323adacSDevin Teske			# Create a persistent menu instead
14097323adacSDevin Teske
14107323adacSDevin Teske			f_dialog_title "$msg_network_configuration"
141151c41087SDevin Teske			local prompt=
14127323adacSDevin Teske
14137323adacSDevin Teske			while :; do
14147323adacSDevin Teske				cp=$( $DIALOG \
14157323adacSDevin Teske					--title "$DIALOG_TITLE"               \
14167323adacSDevin Teske					--backtitle "$DIALOG_BACKTITLE"       \
14177323adacSDevin Teske					--hline "$hline"                      \
14187323adacSDevin Teske					--item-help                           \
14197323adacSDevin Teske					--ok-label "$msg_ok"                  \
14207323adacSDevin Teske					--cancel-label "$msg_cancel"          \
14217323adacSDevin Teske					--help ""                             \
14227323adacSDevin Teske					--menu "$prompt" 21 60 8              \
14237323adacSDevin Teske					"$msg_accept_continue" ""             \
14247323adacSDevin Teske						"$tcplayout_accept_cont_help" \
14257323adacSDevin Teske					"$msg_host_name_including_domain:"    \
14267323adacSDevin Teske						"$_hostname"                  \
14277323adacSDevin Teske						"$tcplayout_hostname_help"    \
14287323adacSDevin Teske					"$msg_ipv4_gateway:" "$_gateway"      \
14297323adacSDevin Teske						"$tcplayout_gateway_help"     \
14307323adacSDevin Teske					"$msg_name_server:" "$_nameserver"    \
14317323adacSDevin Teske						"$tcplayout_nameserver_help"  \
14327323adacSDevin Teske					"$msg_ipv4_address:" "$_ipaddr"       \
14337323adacSDevin Teske						"$tcplayout_ipaddr_help"      \
14347323adacSDevin Teske					"$msg_netmask:" "$_netmask"           \
14357323adacSDevin Teske						"$tcplayout_netmask_help"     \
14367323adacSDevin Teske					"$msg_extra_options_to_ifconfig"      \
14377323adacSDevin Teske						"$_extras" "$extras_help"     \
14387323adacSDevin Teske					2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD
14397323adacSDevin Teske				)
14407323adacSDevin Teske				local retval=$?
1441fd962ac6SDevin Teske				f_dialog_data_sanitize cp
14427323adacSDevin Teske				f_dprintf "retval=%u mtag=[%s]" $retval "$cp"
14437323adacSDevin Teske
1444f677a9e2SDevin Teske				if [ $retval -eq $DIALOG_HELP ]; then
14457323adacSDevin Teske					f_show_help "$TCP_HELPFILE"
14467323adacSDevin Teske					continue
1447f677a9e2SDevin Teske				elif [ $retval -ne $DIALOG_OK ]; then
14487323adacSDevin Teske					f_dialog_title_restore
1449f677a9e2SDevin Teske					return $DIALOG_CANCEL
14507323adacSDevin Teske				fi
14517323adacSDevin Teske
14527323adacSDevin Teske				case "$cp" in
14537323adacSDevin Teske				"$msg_accept_continue")
14547323adacSDevin Teske					f_dialog_validate_tcpip \
14557323adacSDevin Teske						"$_hostname" \
14567323adacSDevin Teske						"$_gateway" \
14577323adacSDevin Teske						"$_nameserver" \
14587323adacSDevin Teske						"$_ipaddr" \
14597323adacSDevin Teske						"$_netmask" \
14607323adacSDevin Teske						&& break ;;
14617323adacSDevin Teske				"$msg_host_name_including_domain:")
1462ec7120b5SDevin Teske					f_dialog_input cp "$cp" "$_hostname" \
1463ec7120b5SDevin Teske						&& _hostname="$cp" ;;
14647323adacSDevin Teske				"$msg_ipv4_gateway:")
1465ec7120b5SDevin Teske					f_dialog_input cp "$cp" "$_gateway" \
1466ec7120b5SDevin Teske						&& _gateway="$cp" ;;
14677323adacSDevin Teske				"$msg_name_server:")
1468ec7120b5SDevin Teske					f_dialog_input cp "$cp" "$_nameserver" \
1469ec7120b5SDevin Teske						&& _nameserver="$cp" ;;
14707323adacSDevin Teske				"$msg_ipv4_address:")
1471ec7120b5SDevin Teske					f_dialog_input cp "$cp" "$_ipaddr" \
1472ec7120b5SDevin Teske						&& _ipaddr="$cp" ;;
14737323adacSDevin Teske				"$msg_netmask:")
1474ec7120b5SDevin Teske					f_dialog_input cp "$cp" "$_netmask" \
1475ec7120b5SDevin Teske						&& _netmask="$cp" ;;
14767323adacSDevin Teske				"$msg_extra_options_to_ifconfig")
1477ec7120b5SDevin Teske					f_dialog_input cp "$cp" "$_extras" \
1478ec7120b5SDevin Teske						&& _extras="$cp" ;;
14797323adacSDevin Teske				esac
14807323adacSDevin Teske			done
14817323adacSDevin Teske
14827323adacSDevin Teske			f_dialog_title_restore
14837323adacSDevin Teske
14847323adacSDevin Teske		fi # XDIALOG
14857323adacSDevin Teske
14867323adacSDevin Teske	fi # interactive
14877323adacSDevin Teske
14887323adacSDevin Teske	# We actually need to inform the rest of bsdconfig about this
14897323adacSDevin Teske	# data now if the user hasn't selected cancel.
14907323adacSDevin Teske
14917323adacSDevin Teske	if [ "$_hostname" ]; then
14927323adacSDevin Teske		setvar $VAR_HOSTNAME "$_hostname"
14937323adacSDevin Teske		f_quietly hostname "$_hostname"
14947323adacSDevin Teske		case "$_hostname" in
14957323adacSDevin Teske		*.*) setvar $VAR_DOMAINNAME "${_hostname#*.}" ;;
14967323adacSDevin Teske		esac
14977323adacSDevin Teske	fi
14987323adacSDevin Teske	[ "$_gateway"    ] && setvar $VAR_GATEWAY    "$_gateway"
14997323adacSDevin Teske	[ "$_nameserver" ] && setvar $VAR_NAMESERVER "$_nameserver"
15007323adacSDevin Teske	[ "$_ipaddr"     ] && setvar $VAR_IPADDR     "$_ipaddr"
15017323adacSDevin Teske	[ "$_netmask"    ] && setvar $VAR_NETMASK    "$_netmask"
15027323adacSDevin Teske	[ "$_extras"     ] && setvar $VAR_EXTRAS     "$_extras"
15037323adacSDevin Teske
15047323adacSDevin Teske	f_dprintf "Creating struct DEVICE_INFO devinfo_%s" "$dev"
15057323adacSDevin Teske	f_struct_new DEVICE_INFO devinfo_$dev
15069ecd54f2SDevin Teske	$dev set private devinfo_$dev
15077323adacSDevin Teske
15087323adacSDevin Teske	devinfo_$dev set ipaddr    $_ipaddr
15097323adacSDevin Teske	devinfo_$dev set netmask   $_netmask
15107323adacSDevin Teske	devinfo_$dev set extras    $_extras
15117323adacSDevin Teske	devinfo_$dev set use_rtsol $use_rtsol
15127323adacSDevin Teske	devinfo_$dev set use_dhcp  $use_dhcp
15137323adacSDevin Teske
15147323adacSDevin Teske	if [ "$use_dhcp" -o "$_ipaddr" ]; then
15157323adacSDevin Teske		if [ "$use_dhcp" ]; then
15167323adacSDevin Teske			cp="DHCP${extras:+ $extras}"
15177323adacSDevin Teske		else
15187323adacSDevin Teske			cp="inet $_ipaddr netmask $_netmask${extras:+ $extras}"
15197323adacSDevin Teske		fi
15209ecd54f2SDevin Teske		setvar $VAR_IFCONFIG$devname "$cp"
15217323adacSDevin Teske	fi
15227323adacSDevin Teske	[ "$use_rtsol" ] &&
15237323adacSDevin Teske		setvar $VAR_IPV6_ENABLE "YES"
15247323adacSDevin Teske
15257323adacSDevin Teske	[ "$use_dhcp" ] ||
15267323adacSDevin Teske		f_config_resolv # XXX this will do it on the MFS copy
15277323adacSDevin Teske
1528f677a9e2SDevin Teske	return $DIALOG_OK
15297323adacSDevin Teske}
15307323adacSDevin Teske
15317323adacSDevin Teske# f_device_scan_tcp [$var_to_set]
15327323adacSDevin Teske#
15337323adacSDevin Teske# Scan for the first active/configured TCP/IP device. The name of the interface
15347323adacSDevin Teske# is printed to stderr like other dialog(1)-based functions (stdout is reserved
15357323adacSDevin Teske# for dialog(1) interaction) if $var_to_set is missing or NULL. Returns failure
15367323adacSDevin Teske# if no active/configured interface
15377323adacSDevin Teske#
15387323adacSDevin Teskef_device_scan_tcp()
15397323adacSDevin Teske{
15407323adacSDevin Teske	local __var_to_set="$1" __iface
15417323adacSDevin Teske	for __iface in $( ifconfig -l ); do
15427323adacSDevin Teske		if ifconfig $__iface | awk '
15437323adacSDevin Teske		BEGIN {
15447323adacSDevin Teske			has_inet = has_inet6 = is_ethernet = 0
15457323adacSDevin Teske			is_usable = 1
15467323adacSDevin Teske		}
15477323adacSDevin Teske		( $1 == "status:" && $2 != "active" ) { is_usable = 0; exit }
15487323adacSDevin Teske		( $1 == "inet" ) {
15497323adacSDevin Teske			if ($2 == "0.0.0.0") { is_usable = 0; exit }
15507323adacSDevin Teske			has_inet++
15517323adacSDevin Teske		}
15527323adacSDevin Teske		( $1 == "inet6") { has_inet6++ }
15537323adacSDevin Teske		( $1 == "media:" ) {
15547323adacSDevin Teske			if ($2 != "Ethernet") { is_usable = 0; exit }
15557323adacSDevin Teske			is_ethernet = 1
15567323adacSDevin Teske		}
15577323adacSDevin Teske		END {
15587323adacSDevin Teske			if (!(is_ethernet && (has_inet || has_inet6)))
15597323adacSDevin Teske				is_usable = 0
15607323adacSDevin Teske			exit ! is_usable
15617323adacSDevin Teske		}'; then
15627323adacSDevin Teske			f_interactive &&
15637323adacSDevin Teske				f_show_msg "$msg_using_interface" "$__iface"
15647323adacSDevin Teske			f_dprintf "f_device_scan_tcp found %s" "$__iface"
15657323adacSDevin Teske			if [ "$__var_to_set" ]; then
15667323adacSDevin Teske				setvar "$__var_to_set" "$__iface"
15677323adacSDevin Teske			else
15687323adacSDevin Teske				echo "$__iface" >&2
15697323adacSDevin Teske			fi
15707323adacSDevin Teske			return $SUCCESS
15717323adacSDevin Teske		fi
15727323adacSDevin Teske	done
15737323adacSDevin Teske
15747323adacSDevin Teske	return $FAILURE
15757323adacSDevin Teske}
15767323adacSDevin Teske
15777323adacSDevin Teske# f_device_select_tcp
15787323adacSDevin Teske#
15797323adacSDevin Teske# Prompt the user to select network interface to use for TCP/IP access.
15807323adacSDevin Teske# Variables from variable.subr that can be used to script user input:
15817323adacSDevin Teske#
15827323adacSDevin Teske# 	VAR_NETWORK_DEVICE [Optional]
15837323adacSDevin Teske# 		Either a comma-separated list of network interfaces to try when
15847323adacSDevin Teske# 		setting up network access (e.g., "fxp0,em0") or "ANY" (case-
15857323adacSDevin Teske# 		sensitive) to indicate that the first active and configured
15867323adacSDevin Teske# 		interface is acceptable. If unset, the user is presented with a
15877323adacSDevin Teske# 		menu of all available network interfaces.
15887323adacSDevin Teske#
15897323adacSDevin Teske# Returns success if a valid network interface has been selected.
15907323adacSDevin Teske#
15917323adacSDevin Teskef_device_select_tcp()
15927323adacSDevin Teske{
15939ecd54f2SDevin Teske	local devs dev cnt if network_dev
15947323adacSDevin Teske	f_getvar $VAR_NETWORK_DEVICE network_dev
15957323adacSDevin Teske
15967323adacSDevin Teske	f_dprintf "f_device_select_tcp: %s=[%s]" \
15977323adacSDevin Teske	          VAR_NETWORK_DEVICE "$network_dev"
15987323adacSDevin Teske
15997323adacSDevin Teske	if [ "$network_dev" ]; then
16007323adacSDevin Teske		#
16017323adacSDevin Teske		# This can be set to several types of values. If set to ANY,
16027323adacSDevin Teske		# scan all network devices looking for a valid link, and go
16037323adacSDevin Teske		# with the first device found. Can also be specified as a
16047323adacSDevin Teske		# comma delimited list, with each network device tried in
16057323adacSDevin Teske		# order. Can also be set to a single network device.
16067323adacSDevin Teske		#
16077323adacSDevin Teske		[ "$network_dev" = "ANY" ] && f_device_scan_tcp network_dev
16087323adacSDevin Teske
16097323adacSDevin Teske		while [ "$network_dev" ]; do
16107323adacSDevin Teske			case "$network_dev" in
16119ecd54f2SDevin Teske			*,*) if="${network_dev%%,*}"
16127323adacSDevin Teske			     network_dev="${network_dev#*,}"
16137323adacSDevin Teske			     ;;
16149ecd54f2SDevin Teske			  *) if="$network_dev"
16157323adacSDevin Teske			     network_dev=
16167323adacSDevin Teske			esac
16177323adacSDevin Teske
16189ecd54f2SDevin Teske			f_device_find -1 "$if" $DEVICE_TYPE_NETWORK dev
16197323adacSDevin Teske			f_device_dialog_tcp $dev
1620f677a9e2SDevin Teske			if [ $? -eq $DIALOG_OK ]; then
16219ecd54f2SDevin Teske				setvar $VAR_NETWORK_DEVICE $if
1622f677a9e2SDevin Teske				return $DIALOG_OK
16237323adacSDevin Teske			fi
16247323adacSDevin Teske		done
16257323adacSDevin Teske
16267079fc4eSDevin Teske		f_interactive && f_show_msg "$msg_no_network_devices"
1627f677a9e2SDevin Teske		return $DIALOG_CANCEL
16287323adacSDevin Teske
16297323adacSDevin Teske	fi # $network_dev
16307323adacSDevin Teske
16317323adacSDevin Teske	f_device_find "" $DEVICE_TYPE_NETWORK devs
1632d4ae33f0SDevin Teske	f_count cnt $devs
16337323adacSDevin Teske	dev="${devs%%[$IFS]*}"
16349ecd54f2SDevin Teske	$dev get name if
16357323adacSDevin Teske
16367323adacSDevin Teske	f_quietly f_getvar NETWORK_CONFIGURED # for debugging info
16377323adacSDevin Teske	if ! f_running_as_init &&
16387323adacSDevin Teske	   ! [ "${NETWORK_CONFIGURED+set}" -a "$NETWORK_CONFIGURED" = "NO" ]
16397323adacSDevin Teske	then
16407323adacSDevin Teske		trap 'f_interrupt' SIGINT
16417323adacSDevin Teske		if f_dialog_yesno "$msg_assume_network_is_already_configured"
16427323adacSDevin Teske		then
16439ecd54f2SDevin Teske			setvar $VAR_NETWORK_DEVICE $if
1644f677a9e2SDevin Teske			return $DIALOG_OK
16457323adacSDevin Teske		fi
16467323adacSDevin Teske	fi
16477323adacSDevin Teske
16487323adacSDevin Teske	local retval=$SUCCESS
16497323adacSDevin Teske	if [ ${cnt:=0} -eq 0 ]; then
16507079fc4eSDevin Teske		f_show_msg "$msg_no_network_devices"
1651f677a9e2SDevin Teske		retval=$DIALOG_CANCEL
16527323adacSDevin Teske	elif [ $cnt -eq 1 ]; then
16537323adacSDevin Teske		f_device_dialog_tcp $dev
16547323adacSDevin Teske		retval=$?
16559ecd54f2SDevin Teske		[ $retval -eq $DIALOG_OK ] && setvar $VAR_NETWORK_DEVICE $if
16567323adacSDevin Teske	else
16577323adacSDevin Teske		local title="$msg_network_interface_information_required"
16587323adacSDevin Teske		local prompt="$msg_please_select_ethernet_device_to_configure"
16597323adacSDevin Teske		local hline="$hline_arrows_tab_enter"
16607323adacSDevin Teske
16617323adacSDevin Teske		dev=$( f_device_menu \
16627323adacSDevin Teske			"$title" "$prompt" "$hline" $DEVICE_TYPE_NETWORK \
16637323adacSDevin Teske			"$NETWORK_DEVICE_HELPFILE" \
16649ecd54f2SDevin Teske			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD ) ||
16659ecd54f2SDevin Teske			return $DIALOG_CANCEL
16667323adacSDevin Teske
16677323adacSDevin Teske		f_device_dialog_tcp $dev
16687323adacSDevin Teske		retval=$?
1669f677a9e2SDevin Teske		if [ $retval -eq $DIALOG_OK ]; then
16709ecd54f2SDevin Teske			f_struct_copy "$dev" device_network
16719ecd54f2SDevin Teske			setvar $VAR_NETWORK_DEVICE device_network
16727323adacSDevin Teske		else
16737323adacSDevin Teske			f_struct_free device_network
16747323adacSDevin Teske		fi
16757323adacSDevin Teske	fi
16767323adacSDevin Teske
16777323adacSDevin Teske	return $retval
16787323adacSDevin Teske}
16797323adacSDevin Teske
16807323adacSDevin Teske# f_dialog_menu_select_tcp
16817323adacSDevin Teske#
16827323adacSDevin Teske# Like f_dialog_select_tcp() above, but do it from a menu that doesn't care
16837323adacSDevin Teske# about status. In other words, where f_dialog_select_tcp() will not display a
16847323adacSDevin Teske# menu if scripted, this function will always display the menu of available
16857323adacSDevin Teske# network interfaces.
16867323adacSDevin Teske#
16877323adacSDevin Teskef_dialog_menu_select_tcp()
16887323adacSDevin Teske{
16897323adacSDevin Teske	local private use_dhcp name
16907323adacSDevin Teske	NETWORK_CONFIGURED=NO f_device_select_tcp
16917323adacSDevin Teske	if f_struct device_network &&
16927323adacSDevin Teske	   device_network get private private &&
16937323adacSDevin Teske	   f_struct_copy "$private" di &&
16947323adacSDevin Teske	   di get use_dhcp use_dhcp &&
16957323adacSDevin Teske	   [ ! "$use_dhcp" ] &&
16967323adacSDevin Teske	   device_network get name name &&
16977323adacSDevin Teske	   f_yesno "$msg_would_you_like_to_bring_interface_up" "$name"
16987323adacSDevin Teske	then
16999ecd54f2SDevin Teske		if ! f_device_init device_network; then
17007323adacSDevin Teske			f_show_msg "$msg_initialization_of_device_failed" \
17017323adacSDevin Teske			           "$name"
17027323adacSDevin Teske		fi
17037323adacSDevin Teske	fi
1704f677a9e2SDevin Teske	return $DIALOG_OK
17057323adacSDevin Teske}
17067323adacSDevin Teske
17077323adacSDevin Teske############################################################ MAIN
17087323adacSDevin Teske
17097323adacSDevin Teskef_dprintf "%s: Successfully loaded." media/tcpip.subr
17107323adacSDevin Teske
17117323adacSDevin Teskefi # ! $_MEDIA_TCPIP_SUBR
1712