1218799Snwhitehorn#!/bin/sh
2218799Snwhitehorn#-
3218799Snwhitehorn# Copyright (c) 2011 Nathan Whitehorn
4218799Snwhitehorn# All rights reserved.
5222468Sbz# Copyright (c) 2011 The FreeBSD Foundation
6222468Sbz# All rights reserved.
7218799Snwhitehorn#
8222468Sbz# Portions of this software were developed by Bjoern Zeeb
9222468Sbz# under sponsorship from the FreeBSD Foundation.
10222468Sbz#
11218799Snwhitehorn# Redistribution and use in source and binary forms, with or without
12218799Snwhitehorn# modification, are permitted provided that the following conditions
13218799Snwhitehorn# are met:
14218799Snwhitehorn# 1. Redistributions of source code must retain the above copyright
15218799Snwhitehorn#    notice, this list of conditions and the following disclaimer.
16218799Snwhitehorn# 2. Redistributions in binary form must reproduce the above copyright
17218799Snwhitehorn#    notice, this list of conditions and the following disclaimer in the
18218799Snwhitehorn#    documentation and/or other materials provided with the distribution.
19218799Snwhitehorn#
20218799Snwhitehorn# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21218799Snwhitehorn# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22218799Snwhitehorn# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23218799Snwhitehorn# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24218799Snwhitehorn# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25218799Snwhitehorn# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26218799Snwhitehorn# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27218799Snwhitehorn# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28218799Snwhitehorn# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29218799Snwhitehorn# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30218799Snwhitehorn# SUCH DAMAGE.
31218799Snwhitehorn#
32218799Snwhitehorn# $FreeBSD$
33218799Snwhitehorn
34218799SnwhitehornINTERFACES=""
35218799SnwhitehornDIALOG_TAGS=""
36218799Snwhitehorn
37218799Snwhitehorn: ${DIALOG_OK=0}
38218799Snwhitehorn: ${DIALOG_CANCEL=1}
39218799Snwhitehorn: ${DIALOG_HELP=2}
40218799Snwhitehorn: ${DIALOG_EXTRA=3}
41218799Snwhitehorn: ${DIALOG_ITEM_HELP=4}
42218799Snwhitehorn: ${DIALOG_ESC=255}
43218799Snwhitehorn
44256361Sdteske# Do a dirty check to see if this a wireless interface -- there should be a
45256361Sdteske# better way
46256361Sdteskeis_wireless_if() {
47256361Sdteske	ifconfig $1 | grep -q 'media: IEEE 802.11 Wireless'
48256361Sdteske}
49256361Sdteske
50218799Snwhitehornfor IF in `ifconfig -l`; do
51218799Snwhitehorn	test "$IF" = "lo0" && continue
52218799Snwhitehorn	(ifconfig -g wlan | egrep -wq $IF) && continue
53218799Snwhitehorn	INTERFACES="$INTERFACES $IF"
54218799Snwhitehorn	DESC=`sysctl -n dev.$(echo $IF | sed -E 's/([[:alpha:]]*)([[:digit:]]*)/\1.\2/g').%desc`
55256361Sdteske	is_wireless_if $IF && echo $DESC |
56256361Sdteske		grep -iqv wireless && DESC="Wireless $DESC"
57218799Snwhitehorn	DIALOG_TAGS="$DIALOG_TAGS $IF \"$DESC\""
58218799Snwhitehorndone
59218799Snwhitehorn
60226741Snwhitehornif [ -z "$INTERFACES" ]; then
61226741Snwhitehorn	dialog --backtitle 'FreeBSD Installer' \
62226741Snwhitehorn	    --title 'Network Configuration Error' \
63226741Snwhitehorn	    --msgbox 'No network interfaces present to configure.' 0 0
64226741Snwhitehorn	exit 1
65226741Snwhitehornfi
66226741Snwhitehorn
67218799Snwhitehornexec 3>&1
68218799SnwhitehornINTERFACE=`echo $DIALOG_TAGS | xargs dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' --menu 'Please select a network interface to configure:' 0 0 0 2>&1 1>&3`
69218799Snwhitehornif [ $? -eq $DIALOG_CANCEL ]; then exit 1; fi
70218799Snwhitehornexec 3>&-
71218799Snwhitehorn
72222619Sbz: > $BSDINSTALL_TMPETC/._rc.conf.net
73222611Sbz
74218799SnwhitehornIFCONFIG_PREFIX=""
75256361Sdteskeif is_wireless_if $INTERFACE; then
76218799Snwhitehorn	NEXT_WLAN_IFACE=wlan0	# XXX
77222619Sbz	echo wlans_$INTERFACE=\"$NEXT_WLAN_IFACE\" >> $BSDINSTALL_TMPETC/._rc.conf.net
78218799Snwhitehorn	IFCONFIG_PREFIX="WPA "
79218799Snwhitehorn	if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
80218799Snwhitehorn		ifconfig $NEXT_WLAN_IFACE create wlandev $INTERFACE
81218799Snwhitehorn		ifconfig $NEXT_WLAN_IFACE up
82218799Snwhitehorn	fi
83218799Snwhitehorn	bsdinstall wlanconfig $NEXT_WLAN_IFACE || exec $0
84218799Snwhitehorn	INTERFACE="$NEXT_WLAN_IFACE"
85218799Snwhitehornfi
86218799Snwhitehorn
87222468SbzIPV6_AVAIL=0
88222468SbzIPV4_AVAIL=0
89222468Sbzsysctl -N kern.features.inet6 > /dev/null 2>&1
90222468Sbzcase $? in
91222468Sbz0)	IPV6_AVAIL=1 ;;
92222468Sbzesac
93222468Sbzsysctl -N kern.features.inet > /dev/null 2>&1
94222468Sbzcase $? in
95222468Sbz0)	IPV4_AVAIL=1 ;;
96222468Sbzesac
97218799Snwhitehorn
98222468Sbzif [ ${IPV4_AVAIL} -eq 1 ]; then
99222468Sbz	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
100222468Sbz	    --yesno 'Would you like to configure IPv4 for this interface?' 0 0
101222468Sbz	if [ $? -eq $DIALOG_OK ]; then
102222468Sbz		bsdinstall netconfig_ipv4 ${INTERFACE} "${IFCONFIG_PREFIX}" || \
103222468Sbz		exec $0
104222468Sbz	else
105222468Sbz		IPV4_AVAIL=0
106218799Snwhitehorn	fi
107222468Sbzfi
108222468Sbz# In case wlanconfig left an option and we do not support IPv4 we need to write
109222468Sbz# it out on its own.  We cannot write it out with IPv6 as that suffix.
110222468Sbzif [ ${IPV4_AVAIL} -eq 0 -a -n ${IFCONFIG_PREFIX} ]; then
111222619Sbz	echo ifconfig_${INTERFACE}=\"${IFCONFIG_PREFIX}\" >> $BSDINSTALL_TMPETC/._rc.conf.net
112222468Sbzfi
113222468Sbzif [ ${IPV6_AVAIL} -eq 1 ]; then
114222468Sbz	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
115222468Sbz	    --yesno 'Would you like to configure IPv6 for this interface?' 0 0
116222468Sbz	if [ $? -eq $DIALOG_OK ]; then
117222468Sbz		bsdinstall netconfig_ipv6 ${INTERFACE} || exec $0
118222468Sbz	else
119222468Sbz		IPV6_AVAIL=0
120222468Sbz	fi
121222468Sbzfi
122222468Sbz
123222468SbzSEARCH=""
124222468SbzIP4_1=""
125222468SbzIP4_2=""
126222468SbzIP6_1=""
127222468SbzIP6_2=""
128222468Sbzwhile read key value; do
129222468Sbz	case "${key}" in
130222468Sbz	search)		SEARCH="${value}" ;;
131222468Sbz	nameserver)	# is more trick as we have to distinguish v4 and v6
132222468Sbz		case "${value}" in
133222468Sbz		[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*)
134222468Sbz			if [ -z "${IP4_1}" ] ; then
135222468Sbz				IP4_1="${value}"
136222468Sbz			elif [ -z "${IP4_2}" ]; then
137222468Sbz				IP4_2="${value}"
138222468Sbz			fi
139222468Sbz			;;
140222468Sbz		[0-9A-Fa-f:]*:*)
141222468Sbz			if [ -z "${IP6_1}" ] ; then
142222468Sbz				IP6_1="${value}"
143222468Sbz			elif [ -z "${IP6_2}" ]; then
144222468Sbz				IP6_2="${value}"
145222468Sbz			fi
146222468Sbz			;;
147222468Sbz		esac
148222468Sbz		;;
149222468Sbz	# ignore others
150222468Sbz	esac
151222468Sbzdone < ${BSDINSTALL_TMPETC}/resolv.conf
152222468Sbz
153222468SbzRESOLV=""
154222468Sbzif [ ${IPV6_AVAIL} -eq 1 -a ${IPV4_AVAIL} -eq 1 ];  then
155222468Sbz	RESOLV="
156222468Sbz	    'Search' 1 0 \"${SEARCH}\" 1 16 50 0 0
157222468Sbz	    'Nameserver' 2 0 \"Nameserver\" 2 16 50 0 2
158222468Sbz	    'IPv6 DNS #1' 2 0 \"${IP6_1}\" 2 16 50 0 0
159222468Sbz	    'IPv6 DNS #2' 3 0 \"${IP6_2}\" 3 16 50 0 0
160222468Sbz	    'IPv4 DNS #1' 4 0 \"${IP4_1}\" 4 16 16 0 0
161222468Sbz	    'IPv4 DNS #2' 5 0 \"${IP4_2}\" 5 16 16 0 0"
162222468Sbzelif [ ${IPV6_AVAIL} -eq 1 ]; then
163222468Sbz	RESOLV="
164222468Sbz	    'Search' 1 0 \"${SEARCH}\" 1 16 50 0 0
165222468Sbz	    'Nameserver' 2 0 \"Nameserver\" 2 16 50 0 2
166222468Sbz	    'IPv6 DNS #1' 2 0 \"${IP6_1}\" 2 16 50 0 0
167222468Sbz	    'IPv6 DNS #2' 3 0 \"${IP6_2}\" 3 16 50 0 0"
168222468Sbzelif [ ${IPV4_AVAIL} -eq 1 ]; then
169222468Sbz	RESOLV="
170222468Sbz	    'Search' 1 0 \"${SEARCH}\" 1 16 50 0 0
171222468Sbz	    'Nameserver' 2 0 \"Nameserver\" 2 16 50 0 2
172222468Sbz	    'IPv4 DNS #1' 2 0 \"${IP4_1}\" 2 16 16 0 0
173222468Sbz	    'IPv4 DNS #2' 3 0 \"${IP4_2}\" 3 16 16 0 0"
174222468Sbzelse
175218799Snwhitehorn	exit 0
176218799Snwhitehornfi
177218799Snwhitehorn
178218799Snwhitehornexec 3>&1
179222468SbzRESOLV=$(echo "${RESOLV}" | xargs dialog --backtitle 'FreeBSD Installer' \
180222468Sbz	--title 'Network Configuration' \
181225430Sbz	--mixedform 'Resolver Configuration' 0 0 0 \
182218799Snwhitehorn2>&1 1>&3)
183218799Snwhitehornif [ $? -eq $DIALOG_CANCEL ]; then exec $0; fi
184218799Snwhitehornexec 3>&-
185218799Snwhitehorn
186222468Sbzecho ${RESOLV} | tr ' ' '\n' | \
187222468Sbzawk '
188222468SbzBEGIN {
189222681Sbz	search=-1;
190222468Sbz}
191222468Sbz{
192222468Sbz	if (/^[[:space:]]+$/) {
193222468Sbz		next;
194222468Sbz	}
195222468Sbz	if (/^Nameserver$/) {
196222468Sbz		printf "\n";
197222468Sbz		search=0;
198222468Sbz		next;
199222468Sbz	}
200222681Sbz	if (search == -1) {
201222681Sbz		printf "search ";
202222681Sbz		search=1;
203222681Sbz	}
204222468Sbz	if (search > 0) {
205222681Sbz		printf "%s%s", (search > 1) ? " " : "", $1;
206222681Sbz		search++;
207222468Sbz		next;
208222468Sbz	}
209222468Sbz	printf "nameserver %s\n", $1;
210222468Sbz}' > ${BSDINSTALL_TMPETC}/resolv.conf
211218799Snwhitehorn
212222619Sbzmv $BSDINSTALL_TMPETC/._rc.conf.net $BSDINSTALL_TMPETC/rc.conf.net
213