xref: /freebsd/usr.sbin/bsdinstall/scripts/netconfig (revision 970ea0b2cb6942b4fba088031e3b1968e056d6c9)
12118f387SNathan Whitehorn#!/bin/sh
22118f387SNathan Whitehorn#-
32118f387SNathan Whitehorn# Copyright (c) 2011 Nathan Whitehorn
42118f387SNathan Whitehorn# All rights reserved.
57986af23SBjoern A. Zeeb# Copyright (c) 2011 The FreeBSD Foundation
67986af23SBjoern A. Zeeb# All rights reserved.
77986af23SBjoern A. Zeeb#
87986af23SBjoern A. Zeeb# Portions of this software were developed by Bjoern Zeeb
97986af23SBjoern A. Zeeb# under sponsorship from the FreeBSD Foundation.
102118f387SNathan Whitehorn#
112118f387SNathan Whitehorn# Redistribution and use in source and binary forms, with or without
122118f387SNathan Whitehorn# modification, are permitted provided that the following conditions
132118f387SNathan Whitehorn# are met:
142118f387SNathan Whitehorn# 1. Redistributions of source code must retain the above copyright
152118f387SNathan Whitehorn#    notice, this list of conditions and the following disclaimer.
162118f387SNathan Whitehorn# 2. Redistributions in binary form must reproduce the above copyright
172118f387SNathan Whitehorn#    notice, this list of conditions and the following disclaimer in the
182118f387SNathan Whitehorn#    documentation and/or other materials provided with the distribution.
192118f387SNathan Whitehorn#
202118f387SNathan Whitehorn# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
212118f387SNathan Whitehorn# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222118f387SNathan Whitehorn# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232118f387SNathan Whitehorn# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
242118f387SNathan Whitehorn# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
252118f387SNathan Whitehorn# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
262118f387SNathan Whitehorn# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
272118f387SNathan Whitehorn# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
282118f387SNathan Whitehorn# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
292118f387SNathan Whitehorn# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
302118f387SNathan Whitehorn# SUCH DAMAGE.
312118f387SNathan Whitehorn#
322118f387SNathan Whitehorn
33cc42ef53SBrad DavisBSDCFG_SHARE="/usr/share/bsdconfig"
34cc42ef53SBrad Davis. $BSDCFG_SHARE/common.subr || exit 1
35cc42ef53SBrad Davis
362118f387SNathan WhitehornINTERFACES=""
376833ac67SAlfonso S. SicilianoBSDDIALOG_ITEMS=""
382118f387SNathan Whitehorn
396833ac67SAlfonso S. Siciliano: ${BSDDIALOG_OK=0}
406833ac67SAlfonso S. Siciliano: ${BSDDIALOG_CANCEL=1}
416833ac67SAlfonso S. Siciliano: ${BSDDIALOG_HELP=2}
426833ac67SAlfonso S. Siciliano: ${BSDDIALOG_EXTRA=3}
436833ac67SAlfonso S. Siciliano: ${BSDDIALOG_ESC=5}
446833ac67SAlfonso S. Siciliano: ${BSDDIALOG_ERROR=255}
452118f387SNathan Whitehorn
462118f387SNathan Whitehornfor IF in `ifconfig -l`; do
472118f387SNathan Whitehorn	test "$IF" = "lo0" && continue
482118f387SNathan Whitehorn	(ifconfig -g wlan | egrep -wq $IF) && continue
492118f387SNathan Whitehorn	INTERFACES="$INTERFACES $IF"
50159ca5c8SNathan Whitehorndone
51159ca5c8SNathan Whitehorn
52b809c7d6SJessica ClarkeWIRELESS_INTERFACES="$(sysctl -in net.wlan.devices)"
53b809c7d6SJessica ClarkeINTERFACES="$INTERFACES${WIRELESS_INTERFACES:+ }$WIRELESS_INTERFACES"
54159ca5c8SNathan Whitehornis_wireless_if() {
55159ca5c8SNathan Whitehorn	for IF in $(sysctl -in net.wlan.devices); do
56159ca5c8SNathan Whitehorn		if [ $IF = $1 ]; then
57159ca5c8SNathan Whitehorn			return 0
58159ca5c8SNathan Whitehorn		fi
59159ca5c8SNathan Whitehorn	done
60159ca5c8SNathan Whitehorn	return 1
61159ca5c8SNathan Whitehorn}
62159ca5c8SNathan Whitehorn
63159ca5c8SNathan Whitehornfor IF in $INTERFACES; do
642118f387SNathan Whitehorn	DESC=`sysctl -n dev.$(echo $IF | sed -E 's/([[:alpha:]]*)([[:digit:]]*)/\1.\2/g').%desc`
656833ac67SAlfonso S. Siciliano	BSDDIALOG_ITEMS="$BSDDIALOG_ITEMS $IF \"$DESC\""
662118f387SNathan Whitehorndone
672118f387SNathan Whitehorn
6841f2f866SNathan Whitehornif [ -z "$INTERFACES" ]; then
69cc42ef53SBrad Davis	bsddialog --backtitle "$OSNAME Installer" \
707414d14bSJessica Clarke	    --title 'Network Configuration' \
7141f2f866SNathan Whitehorn	    --msgbox 'No network interfaces present to configure.' 0 0
7241f2f866SNathan Whitehorn	exit 1
7341f2f866SNathan Whitehornfi
7441f2f866SNathan Whitehorn
75c0e249d3SLars Kellogg-Stedmanexec 5>&1
76c994f226SJessica ClarkeINTERFACE=$(echo $BSDDIALOG_ITEMS | xargs -o bsddialog \
77c994f226SJessica Clarke    --backtitle "$OSNAME Installer" --title 'Network Configuration' \
78c994f226SJessica Clarke    --ok-label 'Auto' --extra-button --extra-label 'Manual' \
79*970ea0b2SMike Karels    --menu 'Please select a network interface and configuration mode:' 0 0 0 2>&1 1>&5)
80c994f226SJessica Clarke# xargs collapses exit codes to 0/1 (ignoring signals and 255), so exploit
81c994f226SJessica Clarke# bsddialog output being empty when cancelling to distinguish Manual (Extra)
82c994f226SJessica Clarke# from Cancel.
83c994f226SJessica Clarkeif [ $? -eq $BSDDIALOG_OK ]; then
84c994f226SJessica Clarke	AUTO=auto
85c994f226SJessica Clarkeelse
86c994f226SJessica Clarke	if [ -z "$INTERFACE" ]; then
87c994f226SJessica Clarke		exit 1
88c994f226SJessica Clarke	fi
89c994f226SJessica Clarke	AUTO=
90c994f226SJessica Clarkefi
91c0e249d3SLars Kellogg-Stedmanexec 5>&-
922118f387SNathan Whitehorn
93d63d020eSBjoern A. Zeeb: > $BSDINSTALL_TMPETC/._rc.conf.net
94f0cdc181SBjoern A. Zeeb
952118f387SNathan WhitehornIFCONFIG_PREFIX=""
96bce09b12SDevin Teskeif is_wireless_if $INTERFACE; then
972118f387SNathan Whitehorn	NEXT_WLAN_IFACE=wlan0	# XXX
981e75e61dSJessica Clarke	sysrc -f $BSDINSTALL_TMPETC/._rc.conf.net wlans_$INTERFACE="$NEXT_WLAN_IFACE"
992118f387SNathan Whitehorn	IFCONFIG_PREFIX="WPA "
1002118f387SNathan Whitehorn	if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
1012118f387SNathan Whitehorn		ifconfig $NEXT_WLAN_IFACE create wlandev $INTERFACE
1022118f387SNathan Whitehorn		ifconfig $NEXT_WLAN_IFACE up
1032118f387SNathan Whitehorn	fi
1042118f387SNathan Whitehorn	bsdinstall wlanconfig $NEXT_WLAN_IFACE || exec $0
1052118f387SNathan Whitehorn	INTERFACE="$NEXT_WLAN_IFACE"
1062118f387SNathan Whitehornfi
1072118f387SNathan Whitehorn
1087986af23SBjoern A. ZeebIPV6_AVAIL=0
1097986af23SBjoern A. ZeebIPV4_AVAIL=0
1107986af23SBjoern A. Zeebsysctl -N kern.features.inet6 > /dev/null 2>&1
1117986af23SBjoern A. Zeebcase $? in
1127986af23SBjoern A. Zeeb0)	IPV6_AVAIL=1 ;;
1137986af23SBjoern A. Zeebesac
1147986af23SBjoern A. Zeebsysctl -N kern.features.inet > /dev/null 2>&1
1157986af23SBjoern A. Zeebcase $? in
1167986af23SBjoern A. Zeeb0)	IPV4_AVAIL=1 ;;
1177986af23SBjoern A. Zeebesac
1182118f387SNathan Whitehorn
119c994f226SJessica ClarkeAUTO_FAIL=
120c994f226SJessica Clarkeif [ ${IPV4_AVAIL} -eq 1 -a -z "$AUTO" ]; then
121cc42ef53SBrad Davis	bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' \
1227986af23SBjoern A. Zeeb	    --yesno 'Would you like to configure IPv4 for this interface?' 0 0
123c994f226SJessica Clarke	if [ $? -ne $BSDDIALOG_OK ]; then
1247986af23SBjoern A. Zeeb		IPV4_AVAIL=0
1252118f387SNathan Whitehorn	fi
1262118f387SNathan Whitehornfi
127c994f226SJessica Clarkeif [ ${IPV4_AVAIL} -eq 1 ]; then
128c994f226SJessica Clarke	bsdinstall netconfig_ipv4 ${INTERFACE} "${IFCONFIG_PREFIX}" $AUTO
129c994f226SJessica Clarke	if [ $? -ne $BSDDIALOG_OK ]; then
130c994f226SJessica Clarke		if [ -z "$AUTO" ]; then
131c994f226SJessica Clarke			exec $0
132c994f226SJessica Clarke		fi
133c994f226SJessica Clarke		IPV4_AVAIL=0
134c994f226SJessica Clarke		AUTO_FAIL="$AUTO_FAIL${AUTO_FAIL:+, }IPv4"
135c994f226SJessica Clarke	fi
136c994f226SJessica Clarkefi
1377986af23SBjoern A. Zeeb# In case wlanconfig left an option and we do not support IPv4 we need to write
1387986af23SBjoern A. Zeeb# it out on its own.  We cannot write it out with IPv6 as that suffix.
1397986af23SBjoern A. Zeebif [ ${IPV4_AVAIL} -eq 0 -a -n ${IFCONFIG_PREFIX} ]; then
1401e75e61dSJessica Clarke	sysrc -f $BSDINSTALL_TMPETC/._rc.conf.net ifconfig_$INTERFACE="${IFCONFIG_PREFIX}"
1417986af23SBjoern A. Zeebfi
142c994f226SJessica Clarkeif [ ${IPV6_AVAIL} -eq 1 -a -z "$AUTO" ]; then
143cc42ef53SBrad Davis	bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' \
1447986af23SBjoern A. Zeeb	    --yesno 'Would you like to configure IPv6 for this interface?' 0 0
145c994f226SJessica Clarke	if [ $? -ne $BSDDIALOG_OK ]; then
1467986af23SBjoern A. Zeeb		IPV6_AVAIL=0
1477986af23SBjoern A. Zeeb	fi
1487986af23SBjoern A. Zeebfi
149c994f226SJessica Clarkeif [ ${IPV6_AVAIL} -eq 1 ]; then
150c994f226SJessica Clarke	bsdinstall netconfig_ipv6 ${INTERFACE} $AUTO
151c994f226SJessica Clarke	if [ $? -ne $BSDDIALOG_OK ]; then
152c994f226SJessica Clarke		if [ -z "$AUTO" ]; then
153c994f226SJessica Clarke			exec $0
154c994f226SJessica Clarke		fi
155c994f226SJessica Clarke		IPV6_AVAIL=0
156c994f226SJessica Clarke		AUTO_FAIL="$AUTO_FAIL${AUTO_FAIL:+, }IPv6"
157c994f226SJessica Clarke	fi
158c994f226SJessica Clarkefi
1597986af23SBjoern A. Zeeb
1607986af23SBjoern A. ZeebSEARCH=""
1617986af23SBjoern A. ZeebIP4_1=""
1627986af23SBjoern A. ZeebIP4_2=""
1637986af23SBjoern A. ZeebIP6_1=""
1647986af23SBjoern A. ZeebIP6_2=""
1657986af23SBjoern A. Zeebwhile read key value; do
1667986af23SBjoern A. Zeeb	case "${key}" in
1677986af23SBjoern A. Zeeb	search)		SEARCH="${value}" ;;
1687986af23SBjoern A. Zeeb	nameserver)	# is more trick as we have to distinguish v4 and v6
1697986af23SBjoern A. Zeeb		case "${value}" in
1707986af23SBjoern A. Zeeb		[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*)
1717986af23SBjoern A. Zeeb			if [ -z "${IP4_1}" ] ; then
1727986af23SBjoern A. Zeeb				IP4_1="${value}"
1737986af23SBjoern A. Zeeb			elif [ -z "${IP4_2}" ]; then
1747986af23SBjoern A. Zeeb				IP4_2="${value}"
1757986af23SBjoern A. Zeeb			fi
1767986af23SBjoern A. Zeeb			;;
1777986af23SBjoern A. Zeeb		[0-9A-Fa-f:]*:*)
1787986af23SBjoern A. Zeeb			if [ -z "${IP6_1}" ] ; then
1797986af23SBjoern A. Zeeb				IP6_1="${value}"
1807986af23SBjoern A. Zeeb			elif [ -z "${IP6_2}" ]; then
1817986af23SBjoern A. Zeeb				IP6_2="${value}"
1827986af23SBjoern A. Zeeb			fi
1837986af23SBjoern A. Zeeb			;;
1847986af23SBjoern A. Zeeb		esac
1857986af23SBjoern A. Zeeb		;;
1867986af23SBjoern A. Zeeb	# ignore others
1877986af23SBjoern A. Zeeb	esac
1887986af23SBjoern A. Zeebdone < ${BSDINSTALL_TMPETC}/resolv.conf
1897986af23SBjoern A. Zeeb
1907986af23SBjoern A. ZeebRESOLV=""
1917986af23SBjoern A. Zeebif [ ${IPV6_AVAIL} -eq 1 -a ${IPV4_AVAIL} -eq 1 ];  then
1927986af23SBjoern A. Zeeb	RESOLV="
1936833ac67SAlfonso S. Siciliano	    'Search' 1 1 \"${SEARCH}\" 1 16 50 50 0
1946833ac67SAlfonso S. Siciliano	    'Nameserver' 2 1 \"Nameserver\" 2 1 11 11 2
1956833ac67SAlfonso S. Siciliano	    'IPv6 DNS #1' 2 1 \"${IP6_1}\" 2 16 50 50 0
1966833ac67SAlfonso S. Siciliano	    'IPv6 DNS #2' 3 1 \"${IP6_2}\" 3 16 50 50 0
1976833ac67SAlfonso S. Siciliano	    'IPv4 DNS #1' 4 1 \"${IP4_1}\" 4 16 16 16 0
1986833ac67SAlfonso S. Siciliano	    'IPv4 DNS #2' 5 1 \"${IP4_2}\" 5 16 16 16 0"
1997986af23SBjoern A. Zeebelif [ ${IPV6_AVAIL} -eq 1 ]; then
2007986af23SBjoern A. Zeeb	RESOLV="
2016833ac67SAlfonso S. Siciliano	    'Search' 1 1 \"${SEARCH}\" 1 16 50 50 0
2026833ac67SAlfonso S. Siciliano	    'Nameserver' 2 1 \"Nameserver\" 2 1 11 11 2
2036833ac67SAlfonso S. Siciliano	    'IPv6 DNS #1' 2 1 \"${IP6_1}\" 2 16 50 50 0
2046833ac67SAlfonso S. Siciliano	    'IPv6 DNS #2' 3 1 \"${IP6_2}\" 3 16 50 50 0"
2057986af23SBjoern A. Zeebelif [ ${IPV4_AVAIL} -eq 1 ]; then
2067986af23SBjoern A. Zeeb	RESOLV="
2076833ac67SAlfonso S. Siciliano	    'Search' 1 1 \"${SEARCH}\" 1 16 50 50 0
2086833ac67SAlfonso S. Siciliano	    'Nameserver' 2 1 \"Nameserver\" 2 1 11 11 2
2096833ac67SAlfonso S. Siciliano	    'IPv4 DNS #1' 2 1 \"${IP4_1}\" 2 16 16 16 0
2106833ac67SAlfonso S. Siciliano	    'IPv4 DNS #2' 3 1 \"${IP4_2}\" 3 16 16 16 0"
2117986af23SBjoern A. Zeebelse
212c994f226SJessica Clarke	if [ -n "$AUTO_FAIL" ]; then
213c994f226SJessica Clarke		bsddialog --backtitle "$OSNAME Installer" \
214c994f226SJessica Clarke		    --msgbox "Failed to automatically configure interface (tried $AUTO_FAIL)." 0 0
215c994f226SJessica Clarke		exec $0
216c994f226SJessica Clarke	fi
2172118f387SNathan Whitehorn	exit 0
2182118f387SNathan Whitehornfi
2192118f387SNathan Whitehorn
220c994f226SJessica Clarke# Auto only guaranteed to have IPv4 and/or IPv6 address; may not have
221c994f226SJessica Clarke# nameserver available
222c994f226SJessica Clarkeif [ -n "$AUTO" ] && [ -n "${IP4_1}" -o -n "${IP6_1}" ]; then
223c994f226SJessica Clarke	# Convert from bsddialog arguments to default output
224c994f226SJessica Clarke	RESOLV=$(echo "${RESOLV}" | xargs -n9 sh -c 'echo "$4"' '')
225c994f226SJessica Clarkeelse
226c0e249d3SLars Kellogg-Stedman	exec 5>&1
227cc42ef53SBrad Davis	RESOLV=$(echo "${RESOLV}" | xargs -o bsddialog --backtitle "$OSNAME Installer" \
2287986af23SBjoern A. Zeeb		--title 'Network Configuration' \
229092b5f7cSBjoern A. Zeeb		--mixedform 'Resolver Configuration' 0 0 0 \
230c0e249d3SLars Kellogg-Stedman	2>&1 1>&5)
2316833ac67SAlfonso S. Siciliano	if [ $? -eq $BSDDIALOG_CANCEL ]; then exec $0; fi
232c0e249d3SLars Kellogg-Stedman	exec 5>&-
233c994f226SJessica Clarkefi
2342118f387SNathan Whitehorn
2357986af23SBjoern A. Zeebecho ${RESOLV} | tr ' ' '\n' | \
2367986af23SBjoern A. Zeebawk '
2377986af23SBjoern A. ZeebBEGIN {
2389ab173deSBjoern A. Zeeb	search=-1;
2397986af23SBjoern A. Zeeb}
2407986af23SBjoern A. Zeeb{
2417986af23SBjoern A. Zeeb	if (/^[[:space:]]+$/) {
2427986af23SBjoern A. Zeeb		next;
2437986af23SBjoern A. Zeeb	}
2447986af23SBjoern A. Zeeb	if (/^Nameserver$/) {
2457986af23SBjoern A. Zeeb		printf "\n";
2467986af23SBjoern A. Zeeb		search=0;
2477986af23SBjoern A. Zeeb		next;
2487986af23SBjoern A. Zeeb	}
2499ab173deSBjoern A. Zeeb	if (search == -1) {
2509ab173deSBjoern A. Zeeb		printf "search ";
2519ab173deSBjoern A. Zeeb		search=1;
2529ab173deSBjoern A. Zeeb	}
2537986af23SBjoern A. Zeeb	if (search > 0) {
2549ab173deSBjoern A. Zeeb		printf "%s%s", (search > 1) ? " " : "", $1;
2559ab173deSBjoern A. Zeeb		search++;
2567986af23SBjoern A. Zeeb		next;
2577986af23SBjoern A. Zeeb	}
2587986af23SBjoern A. Zeeb	printf "nameserver %s\n", $1;
2597986af23SBjoern A. Zeeb}' > ${BSDINSTALL_TMPETC}/resolv.conf
2602118f387SNathan Whitehorn
261d63d020eSBjoern A. Zeebmv $BSDINSTALL_TMPETC/._rc.conf.net $BSDINSTALL_TMPETC/rc.conf.net
262