xref: /freebsd/usr.sbin/bsdinstall/scripts/netconfig (revision 970ea0b2cb6942b4fba088031e3b1968e056d6c9)
1 2118f387SNathan Whitehorn#!/bin/sh
2 2118f387SNathan Whitehorn#-
3 2118f387SNathan Whitehorn# Copyright (c) 2011 Nathan Whitehorn
4 2118f387SNathan Whitehorn# All rights reserved.
5 7986af23SBjoern A. Zeeb# Copyright (c) 2011 The FreeBSD Foundation
6 7986af23SBjoern A. Zeeb# All rights reserved.
7 7986af23SBjoern A. Zeeb#
8 7986af23SBjoern A. Zeeb# Portions of this software were developed by Bjoern Zeeb
9 7986af23SBjoern A. Zeeb# under sponsorship from the FreeBSD Foundation.
10 2118f387SNathan Whitehorn#
11 2118f387SNathan Whitehorn# Redistribution and use in source and binary forms, with or without
12 2118f387SNathan Whitehorn# modification, are permitted provided that the following conditions
13 2118f387SNathan Whitehorn# are met:
14 2118f387SNathan Whitehorn# 1. Redistributions of source code must retain the above copyright
15 2118f387SNathan Whitehorn#    notice, this list of conditions and the following disclaimer.
16 2118f387SNathan Whitehorn# 2. Redistributions in binary form must reproduce the above copyright
17 2118f387SNathan Whitehorn#    notice, this list of conditions and the following disclaimer in the
18 2118f387SNathan Whitehorn#    documentation and/or other materials provided with the distribution.
19 2118f387SNathan Whitehorn#
20 2118f387SNathan Whitehorn# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 2118f387SNathan Whitehorn# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 2118f387SNathan Whitehorn# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 2118f387SNathan Whitehorn# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 2118f387SNathan Whitehorn# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 2118f387SNathan Whitehorn# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 2118f387SNathan Whitehorn# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 2118f387SNathan Whitehorn# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 2118f387SNathan Whitehorn# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 2118f387SNathan Whitehorn# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 2118f387SNathan Whitehorn# SUCH DAMAGE.
31 2118f387SNathan Whitehorn#
32 2118f387SNathan Whitehorn
33 cc42ef53SBrad DavisBSDCFG_SHARE="/usr/share/bsdconfig"
34 cc42ef53SBrad Davis. $BSDCFG_SHARE/common.subr || exit 1
35 cc42ef53SBrad Davis
36 2118f387SNathan WhitehornINTERFACES=""
37 6833ac67SAlfonso S. SicilianoBSDDIALOG_ITEMS=""
38 2118f387SNathan Whitehorn
39 6833ac67SAlfonso S. Siciliano: ${BSDDIALOG_OK=0}
40 6833ac67SAlfonso S. Siciliano: ${BSDDIALOG_CANCEL=1}
41 6833ac67SAlfonso S. Siciliano: ${BSDDIALOG_HELP=2}
42 6833ac67SAlfonso S. Siciliano: ${BSDDIALOG_EXTRA=3}
43 6833ac67SAlfonso S. Siciliano: ${BSDDIALOG_ESC=5}
44 6833ac67SAlfonso S. Siciliano: ${BSDDIALOG_ERROR=255}
45 2118f387SNathan Whitehorn
46 2118f387SNathan Whitehornfor IF in `ifconfig -l`; do
47 2118f387SNathan Whitehorn	test "$IF" = "lo0" && continue
48 2118f387SNathan Whitehorn	(ifconfig -g wlan | egrep -wq $IF) && continue
49 2118f387SNathan Whitehorn	INTERFACES="$INTERFACES $IF"
50 159ca5c8SNathan Whitehorndone
51 159ca5c8SNathan Whitehorn
52 b809c7d6SJessica ClarkeWIRELESS_INTERFACES="$(sysctl -in net.wlan.devices)"
53 b809c7d6SJessica ClarkeINTERFACES="$INTERFACES${WIRELESS_INTERFACES:+ }$WIRELESS_INTERFACES"
54 159ca5c8SNathan Whitehornis_wireless_if() {
55 159ca5c8SNathan Whitehorn	for IF in $(sysctl -in net.wlan.devices); do
56 159ca5c8SNathan Whitehorn		if [ $IF = $1 ]; then
57 159ca5c8SNathan Whitehorn			return 0
58 159ca5c8SNathan Whitehorn		fi
59 159ca5c8SNathan Whitehorn	done
60 159ca5c8SNathan Whitehorn	return 1
61 159ca5c8SNathan Whitehorn}
62 159ca5c8SNathan Whitehorn
63 159ca5c8SNathan Whitehornfor IF in $INTERFACES; do
64 2118f387SNathan Whitehorn	DESC=`sysctl -n dev.$(echo $IF | sed -E 's/([[:alpha:]]*)([[:digit:]]*)/\1.\2/g').%desc`
65 6833ac67SAlfonso S. Siciliano	BSDDIALOG_ITEMS="$BSDDIALOG_ITEMS $IF \"$DESC\""
66 2118f387SNathan Whitehorndone
67 2118f387SNathan Whitehorn
68 41f2f866SNathan Whitehornif [ -z "$INTERFACES" ]; then
69 cc42ef53SBrad Davis	bsddialog --backtitle "$OSNAME Installer" \
70 7414d14bSJessica Clarke	    --title 'Network Configuration' \
71 41f2f866SNathan Whitehorn	    --msgbox 'No network interfaces present to configure.' 0 0
72 41f2f866SNathan Whitehorn	exit 1
73 41f2f866SNathan Whitehornfi
74 41f2f866SNathan Whitehorn
75 c0e249d3SLars Kellogg-Stedmanexec 5>&1
76 c994f226SJessica ClarkeINTERFACE=$(echo $BSDDIALOG_ITEMS | xargs -o bsddialog \
77 c994f226SJessica Clarke    --backtitle "$OSNAME Installer" --title 'Network Configuration' \
78 c994f226SJessica 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)
80 c994f226SJessica Clarke# xargs collapses exit codes to 0/1 (ignoring signals and 255), so exploit
81 c994f226SJessica Clarke# bsddialog output being empty when cancelling to distinguish Manual (Extra)
82 c994f226SJessica Clarke# from Cancel.
83 c994f226SJessica Clarkeif [ $? -eq $BSDDIALOG_OK ]; then
84 c994f226SJessica Clarke	AUTO=auto
85 c994f226SJessica Clarkeelse
86 c994f226SJessica Clarke	if [ -z "$INTERFACE" ]; then
87 c994f226SJessica Clarke		exit 1
88 c994f226SJessica Clarke	fi
89 c994f226SJessica Clarke	AUTO=
90 c994f226SJessica Clarkefi
91 c0e249d3SLars Kellogg-Stedmanexec 5>&-
92 2118f387SNathan Whitehorn
93 d63d020eSBjoern A. Zeeb: > $BSDINSTALL_TMPETC/._rc.conf.net
94 f0cdc181SBjoern A. Zeeb
95 2118f387SNathan WhitehornIFCONFIG_PREFIX=""
96 bce09b12SDevin Teskeif is_wireless_if $INTERFACE; then
97 2118f387SNathan Whitehorn	NEXT_WLAN_IFACE=wlan0	# XXX
98 1e75e61dSJessica Clarke	sysrc -f $BSDINSTALL_TMPETC/._rc.conf.net wlans_$INTERFACE="$NEXT_WLAN_IFACE"
99 2118f387SNathan Whitehorn	IFCONFIG_PREFIX="WPA "
100 2118f387SNathan Whitehorn	if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
101 2118f387SNathan Whitehorn		ifconfig $NEXT_WLAN_IFACE create wlandev $INTERFACE
102 2118f387SNathan Whitehorn		ifconfig $NEXT_WLAN_IFACE up
103 2118f387SNathan Whitehorn	fi
104 2118f387SNathan Whitehorn	bsdinstall wlanconfig $NEXT_WLAN_IFACE || exec $0
105 2118f387SNathan Whitehorn	INTERFACE="$NEXT_WLAN_IFACE"
106 2118f387SNathan Whitehornfi
107 2118f387SNathan Whitehorn
108 7986af23SBjoern A. ZeebIPV6_AVAIL=0
109 7986af23SBjoern A. ZeebIPV4_AVAIL=0
110 7986af23SBjoern A. Zeebsysctl -N kern.features.inet6 > /dev/null 2>&1
111 7986af23SBjoern A. Zeebcase $? in
112 7986af23SBjoern A. Zeeb0)	IPV6_AVAIL=1 ;;
113 7986af23SBjoern A. Zeebesac
114 7986af23SBjoern A. Zeebsysctl -N kern.features.inet > /dev/null 2>&1
115 7986af23SBjoern A. Zeebcase $? in
116 7986af23SBjoern A. Zeeb0)	IPV4_AVAIL=1 ;;
117 7986af23SBjoern A. Zeebesac
118 2118f387SNathan Whitehorn
119 c994f226SJessica ClarkeAUTO_FAIL=
120 c994f226SJessica Clarkeif [ ${IPV4_AVAIL} -eq 1 -a -z "$AUTO" ]; then
121 cc42ef53SBrad Davis	bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' \
122 7986af23SBjoern A. Zeeb	    --yesno 'Would you like to configure IPv4 for this interface?' 0 0
123 c994f226SJessica Clarke	if [ $? -ne $BSDDIALOG_OK ]; then
124 7986af23SBjoern A. Zeeb		IPV4_AVAIL=0
125 2118f387SNathan Whitehorn	fi
126 2118f387SNathan Whitehornfi
127 c994f226SJessica Clarkeif [ ${IPV4_AVAIL} -eq 1 ]; then
128 c994f226SJessica Clarke	bsdinstall netconfig_ipv4 ${INTERFACE} "${IFCONFIG_PREFIX}" $AUTO
129 c994f226SJessica Clarke	if [ $? -ne $BSDDIALOG_OK ]; then
130 c994f226SJessica Clarke		if [ -z "$AUTO" ]; then
131 c994f226SJessica Clarke			exec $0
132 c994f226SJessica Clarke		fi
133 c994f226SJessica Clarke		IPV4_AVAIL=0
134 c994f226SJessica Clarke		AUTO_FAIL="$AUTO_FAIL${AUTO_FAIL:+, }IPv4"
135 c994f226SJessica Clarke	fi
136 c994f226SJessica Clarkefi
137 7986af23SBjoern A. Zeeb# In case wlanconfig left an option and we do not support IPv4 we need to write
138 7986af23SBjoern A. Zeeb# it out on its own.  We cannot write it out with IPv6 as that suffix.
139 7986af23SBjoern A. Zeebif [ ${IPV4_AVAIL} -eq 0 -a -n ${IFCONFIG_PREFIX} ]; then
140 1e75e61dSJessica Clarke	sysrc -f $BSDINSTALL_TMPETC/._rc.conf.net ifconfig_$INTERFACE="${IFCONFIG_PREFIX}"
141 7986af23SBjoern A. Zeebfi
142 c994f226SJessica Clarkeif [ ${IPV6_AVAIL} -eq 1 -a -z "$AUTO" ]; then
143 cc42ef53SBrad Davis	bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' \
144 7986af23SBjoern A. Zeeb	    --yesno 'Would you like to configure IPv6 for this interface?' 0 0
145 c994f226SJessica Clarke	if [ $? -ne $BSDDIALOG_OK ]; then
146 7986af23SBjoern A. Zeeb		IPV6_AVAIL=0
147 7986af23SBjoern A. Zeeb	fi
148 7986af23SBjoern A. Zeebfi
149 c994f226SJessica Clarkeif [ ${IPV6_AVAIL} -eq 1 ]; then
150 c994f226SJessica Clarke	bsdinstall netconfig_ipv6 ${INTERFACE} $AUTO
151 c994f226SJessica Clarke	if [ $? -ne $BSDDIALOG_OK ]; then
152 c994f226SJessica Clarke		if [ -z "$AUTO" ]; then
153 c994f226SJessica Clarke			exec $0
154 c994f226SJessica Clarke		fi
155 c994f226SJessica Clarke		IPV6_AVAIL=0
156 c994f226SJessica Clarke		AUTO_FAIL="$AUTO_FAIL${AUTO_FAIL:+, }IPv6"
157 c994f226SJessica Clarke	fi
158 c994f226SJessica Clarkefi
159 7986af23SBjoern A. Zeeb
160 7986af23SBjoern A. ZeebSEARCH=""
161 7986af23SBjoern A. ZeebIP4_1=""
162 7986af23SBjoern A. ZeebIP4_2=""
163 7986af23SBjoern A. ZeebIP6_1=""
164 7986af23SBjoern A. ZeebIP6_2=""
165 7986af23SBjoern A. Zeebwhile read key value; do
166 7986af23SBjoern A. Zeeb	case "${key}" in
167 7986af23SBjoern A. Zeeb	search)		SEARCH="${value}" ;;
168 7986af23SBjoern A. Zeeb	nameserver)	# is more trick as we have to distinguish v4 and v6
169 7986af23SBjoern A. Zeeb		case "${value}" in
170 7986af23SBjoern A. Zeeb		[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*)
171 7986af23SBjoern A. Zeeb			if [ -z "${IP4_1}" ] ; then
172 7986af23SBjoern A. Zeeb				IP4_1="${value}"
173 7986af23SBjoern A. Zeeb			elif [ -z "${IP4_2}" ]; then
174 7986af23SBjoern A. Zeeb				IP4_2="${value}"
175 7986af23SBjoern A. Zeeb			fi
176 7986af23SBjoern A. Zeeb			;;
177 7986af23SBjoern A. Zeeb		[0-9A-Fa-f:]*:*)
178 7986af23SBjoern A. Zeeb			if [ -z "${IP6_1}" ] ; then
179 7986af23SBjoern A. Zeeb				IP6_1="${value}"
180 7986af23SBjoern A. Zeeb			elif [ -z "${IP6_2}" ]; then
181 7986af23SBjoern A. Zeeb				IP6_2="${value}"
182 7986af23SBjoern A. Zeeb			fi
183 7986af23SBjoern A. Zeeb			;;
184 7986af23SBjoern A. Zeeb		esac
185 7986af23SBjoern A. Zeeb		;;
186 7986af23SBjoern A. Zeeb	# ignore others
187 7986af23SBjoern A. Zeeb	esac
188 7986af23SBjoern A. Zeebdone < ${BSDINSTALL_TMPETC}/resolv.conf
189 7986af23SBjoern A. Zeeb
190 7986af23SBjoern A. ZeebRESOLV=""
191 7986af23SBjoern A. Zeebif [ ${IPV6_AVAIL} -eq 1 -a ${IPV4_AVAIL} -eq 1 ];  then
192 7986af23SBjoern A. Zeeb	RESOLV="
193 6833ac67SAlfonso S. Siciliano	    'Search' 1 1 \"${SEARCH}\" 1 16 50 50 0
194 6833ac67SAlfonso S. Siciliano	    'Nameserver' 2 1 \"Nameserver\" 2 1 11 11 2
195 6833ac67SAlfonso S. Siciliano	    'IPv6 DNS #1' 2 1 \"${IP6_1}\" 2 16 50 50 0
196 6833ac67SAlfonso S. Siciliano	    'IPv6 DNS #2' 3 1 \"${IP6_2}\" 3 16 50 50 0
197 6833ac67SAlfonso S. Siciliano	    'IPv4 DNS #1' 4 1 \"${IP4_1}\" 4 16 16 16 0
198 6833ac67SAlfonso S. Siciliano	    'IPv4 DNS #2' 5 1 \"${IP4_2}\" 5 16 16 16 0"
199 7986af23SBjoern A. Zeebelif [ ${IPV6_AVAIL} -eq 1 ]; then
200 7986af23SBjoern A. Zeeb	RESOLV="
201 6833ac67SAlfonso S. Siciliano	    'Search' 1 1 \"${SEARCH}\" 1 16 50 50 0
202 6833ac67SAlfonso S. Siciliano	    'Nameserver' 2 1 \"Nameserver\" 2 1 11 11 2
203 6833ac67SAlfonso S. Siciliano	    'IPv6 DNS #1' 2 1 \"${IP6_1}\" 2 16 50 50 0
204 6833ac67SAlfonso S. Siciliano	    'IPv6 DNS #2' 3 1 \"${IP6_2}\" 3 16 50 50 0"
205 7986af23SBjoern A. Zeebelif [ ${IPV4_AVAIL} -eq 1 ]; then
206 7986af23SBjoern A. Zeeb	RESOLV="
207 6833ac67SAlfonso S. Siciliano	    'Search' 1 1 \"${SEARCH}\" 1 16 50 50 0
208 6833ac67SAlfonso S. Siciliano	    'Nameserver' 2 1 \"Nameserver\" 2 1 11 11 2
209 6833ac67SAlfonso S. Siciliano	    'IPv4 DNS #1' 2 1 \"${IP4_1}\" 2 16 16 16 0
210 6833ac67SAlfonso S. Siciliano	    'IPv4 DNS #2' 3 1 \"${IP4_2}\" 3 16 16 16 0"
211 7986af23SBjoern A. Zeebelse
212 c994f226SJessica Clarke	if [ -n "$AUTO_FAIL" ]; then
213 c994f226SJessica Clarke		bsddialog --backtitle "$OSNAME Installer" \
214 c994f226SJessica Clarke		    --msgbox "Failed to automatically configure interface (tried $AUTO_FAIL)." 0 0
215 c994f226SJessica Clarke		exec $0
216 c994f226SJessica Clarke	fi
217 2118f387SNathan Whitehorn	exit 0
218 2118f387SNathan Whitehornfi
219 2118f387SNathan Whitehorn
220 c994f226SJessica Clarke# Auto only guaranteed to have IPv4 and/or IPv6 address; may not have
221 c994f226SJessica Clarke# nameserver available
222 c994f226SJessica Clarkeif [ -n "$AUTO" ] && [ -n "${IP4_1}" -o -n "${IP6_1}" ]; then
223 c994f226SJessica Clarke	# Convert from bsddialog arguments to default output
224 c994f226SJessica Clarke	RESOLV=$(echo "${RESOLV}" | xargs -n9 sh -c 'echo "$4"' '')
225 c994f226SJessica Clarkeelse
226 c0e249d3SLars Kellogg-Stedman	exec 5>&1
227 cc42ef53SBrad Davis	RESOLV=$(echo "${RESOLV}" | xargs -o bsddialog --backtitle "$OSNAME Installer" \
228 7986af23SBjoern A. Zeeb		--title 'Network Configuration' \
229 092b5f7cSBjoern A. Zeeb		--mixedform 'Resolver Configuration' 0 0 0 \
230 c0e249d3SLars Kellogg-Stedman	2>&1 1>&5)
231 6833ac67SAlfonso S. Siciliano	if [ $? -eq $BSDDIALOG_CANCEL ]; then exec $0; fi
232 c0e249d3SLars Kellogg-Stedman	exec 5>&-
233 c994f226SJessica Clarkefi
234 2118f387SNathan Whitehorn
235 7986af23SBjoern A. Zeebecho ${RESOLV} | tr ' ' '\n' | \
236 7986af23SBjoern A. Zeebawk '
237 7986af23SBjoern A. ZeebBEGIN {
238 9ab173deSBjoern A. Zeeb	search=-1;
239 7986af23SBjoern A. Zeeb}
240 7986af23SBjoern A. Zeeb{
241 7986af23SBjoern A. Zeeb	if (/^[[:space:]]+$/) {
242 7986af23SBjoern A. Zeeb		next;
243 7986af23SBjoern A. Zeeb	}
244 7986af23SBjoern A. Zeeb	if (/^Nameserver$/) {
245 7986af23SBjoern A. Zeeb		printf "\n";
246 7986af23SBjoern A. Zeeb		search=0;
247 7986af23SBjoern A. Zeeb		next;
248 7986af23SBjoern A. Zeeb	}
249 9ab173deSBjoern A. Zeeb	if (search == -1) {
250 9ab173deSBjoern A. Zeeb		printf "search ";
251 9ab173deSBjoern A. Zeeb		search=1;
252 9ab173deSBjoern A. Zeeb	}
253 7986af23SBjoern A. Zeeb	if (search > 0) {
254 9ab173deSBjoern A. Zeeb		printf "%s%s", (search > 1) ? " " : "", $1;
255 9ab173deSBjoern A. Zeeb		search++;
256 7986af23SBjoern A. Zeeb		next;
257 7986af23SBjoern A. Zeeb	}
258 7986af23SBjoern A. Zeeb	printf "nameserver %s\n", $1;
259 7986af23SBjoern A. Zeeb}' > ${BSDINSTALL_TMPETC}/resolv.conf
260 2118f387SNathan Whitehorn
261 d63d020eSBjoern A. Zeebmv $BSDINSTALL_TMPETC/._rc.conf.net $BSDINSTALL_TMPETC/rc.conf.net
262