1#!/bin/sh
2#-
3# Copyright (c) 2011 Nathan Whitehorn
4# All rights reserved.
5# Copyright (c) 2011 The FreeBSD Foundation
6# All rights reserved.
7#
8# Portions of this software were developed by Bjoern Zeeb
9# under sponsorship from the FreeBSD Foundation.
10#
11# Redistribution and use in source and binary forms, with or without
12# modification, are permitted provided that the following conditions
13# are met:
14# 1. Redistributions of source code must retain the above copyright
15#    notice, this list of conditions and the following disclaimer.
16# 2. Redistributions in binary form must reproduce the above copyright
17#    notice, this list of conditions and the following disclaimer in the
18#    documentation and/or other materials provided with the distribution.
19#
20# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30# SUCH DAMAGE.
31#
32# $FreeBSD$
33
34INTERFACES=""
35DIALOG_TAGS=""
36
37: ${DIALOG_OK=0}
38: ${DIALOG_CANCEL=1}
39: ${DIALOG_HELP=2}
40: ${DIALOG_EXTRA=3}
41: ${DIALOG_ITEM_HELP=4}
42: ${DIALOG_ESC=255}
43
44# Do a dirty check to see if this a wireless interface -- there should be a
45# better way
46is_wireless_if() {
47	ifconfig $1 | grep -q 'media: IEEE 802.11 Wireless'
48}
49
50for IF in `ifconfig -l`; do
51	test "$IF" = "lo0" && continue
52	(ifconfig -g wlan | egrep -wq $IF) && continue
53	INTERFACES="$INTERFACES $IF"
54	DESC=`sysctl -n dev.$(echo $IF | sed -E 's/([[:alpha:]]*)([[:digit:]]*)/\1.\2/g').%desc`
55	is_wireless_if $IF && echo $DESC |
56		grep -iqv wireless && DESC="Wireless $DESC"
57	DIALOG_TAGS="$DIALOG_TAGS $IF \"$DESC\""
58done
59
60if [ -z "$INTERFACES" ]; then
61	dialog --backtitle 'FreeBSD Installer' \
62	    --title 'Network Configuration Error' \
63	    --msgbox 'No network interfaces present to configure.' 0 0
64	exit 1
65fi
66
67exec 3>&1
68INTERFACE=`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`
69if [ $? -eq $DIALOG_CANCEL ]; then exit 1; fi
70exec 3>&-
71
72: > $BSDINSTALL_TMPETC/._rc.conf.net
73
74IFCONFIG_PREFIX=""
75if is_wireless_if $INTERFACE; then
76	NEXT_WLAN_IFACE=wlan0	# XXX
77	echo wlans_$INTERFACE=\"$NEXT_WLAN_IFACE\" >> $BSDINSTALL_TMPETC/._rc.conf.net
78	IFCONFIG_PREFIX="WPA "
79	if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
80		ifconfig $NEXT_WLAN_IFACE create wlandev $INTERFACE
81		ifconfig $NEXT_WLAN_IFACE up
82	fi
83	bsdinstall wlanconfig $NEXT_WLAN_IFACE || exec $0
84	INTERFACE="$NEXT_WLAN_IFACE"
85fi
86
87IPV6_AVAIL=0
88IPV4_AVAIL=0
89sysctl -N kern.features.inet6 > /dev/null 2>&1
90case $? in
910)	IPV6_AVAIL=1 ;;
92esac
93sysctl -N kern.features.inet > /dev/null 2>&1
94case $? in
950)	IPV4_AVAIL=1 ;;
96esac
97
98if [ ${IPV4_AVAIL} -eq 1 ]; then
99	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
100	    --yesno 'Would you like to configure IPv4 for this interface?' 0 0
101	if [ $? -eq $DIALOG_OK ]; then
102		bsdinstall netconfig_ipv4 ${INTERFACE} "${IFCONFIG_PREFIX}" || \
103		exec $0
104	else
105		IPV4_AVAIL=0
106	fi
107fi
108# In case wlanconfig left an option and we do not support IPv4 we need to write
109# it out on its own.  We cannot write it out with IPv6 as that suffix.
110if [ ${IPV4_AVAIL} -eq 0 -a -n ${IFCONFIG_PREFIX} ]; then
111	echo ifconfig_${INTERFACE}=\"${IFCONFIG_PREFIX}\" >> $BSDINSTALL_TMPETC/._rc.conf.net
112fi
113if [ ${IPV6_AVAIL} -eq 1 ]; then
114	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
115	    --yesno 'Would you like to configure IPv6 for this interface?' 0 0
116	if [ $? -eq $DIALOG_OK ]; then
117		bsdinstall netconfig_ipv6 ${INTERFACE} || exec $0
118	else
119		IPV6_AVAIL=0
120	fi
121fi
122
123SEARCH=""
124IP4_1=""
125IP4_2=""
126IP6_1=""
127IP6_2=""
128while read key value; do
129	case "${key}" in
130	search)		SEARCH="${value}" ;;
131	nameserver)	# is more trick as we have to distinguish v4 and v6
132		case "${value}" in
133		[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*)
134			if [ -z "${IP4_1}" ] ; then
135				IP4_1="${value}"
136			elif [ -z "${IP4_2}" ]; then
137				IP4_2="${value}"
138			fi
139			;;
140		[0-9A-Fa-f:]*:*)
141			if [ -z "${IP6_1}" ] ; then
142				IP6_1="${value}"
143			elif [ -z "${IP6_2}" ]; then
144				IP6_2="${value}"
145			fi
146			;;
147		esac
148		;;
149	# ignore others
150	esac
151done < ${BSDINSTALL_TMPETC}/resolv.conf
152
153RESOLV=""
154if [ ${IPV6_AVAIL} -eq 1 -a ${IPV4_AVAIL} -eq 1 ];  then
155	RESOLV="
156	    'Search' 1 0 \"${SEARCH}\" 1 16 50 0 0
157	    'Nameserver' 2 0 \"Nameserver\" 2 16 50 0 2
158	    'IPv6 DNS #1' 2 0 \"${IP6_1}\" 2 16 50 0 0
159	    'IPv6 DNS #2' 3 0 \"${IP6_2}\" 3 16 50 0 0
160	    'IPv4 DNS #1' 4 0 \"${IP4_1}\" 4 16 16 0 0
161	    'IPv4 DNS #2' 5 0 \"${IP4_2}\" 5 16 16 0 0"
162elif [ ${IPV6_AVAIL} -eq 1 ]; then
163	RESOLV="
164	    'Search' 1 0 \"${SEARCH}\" 1 16 50 0 0
165	    'Nameserver' 2 0 \"Nameserver\" 2 16 50 0 2
166	    'IPv6 DNS #1' 2 0 \"${IP6_1}\" 2 16 50 0 0
167	    'IPv6 DNS #2' 3 0 \"${IP6_2}\" 3 16 50 0 0"
168elif [ ${IPV4_AVAIL} -eq 1 ]; then
169	RESOLV="
170	    'Search' 1 0 \"${SEARCH}\" 1 16 50 0 0
171	    'Nameserver' 2 0 \"Nameserver\" 2 16 50 0 2
172	    'IPv4 DNS #1' 2 0 \"${IP4_1}\" 2 16 16 0 0
173	    'IPv4 DNS #2' 3 0 \"${IP4_2}\" 3 16 16 0 0"
174else
175	exit 0
176fi
177
178exec 3>&1
179RESOLV=$(echo "${RESOLV}" | xargs dialog --backtitle 'FreeBSD Installer' \
180	--title 'Network Configuration' \
181	--mixedform 'Resolver Configuration' 0 0 0 \
1822>&1 1>&3)
183if [ $? -eq $DIALOG_CANCEL ]; then exec $0; fi
184exec 3>&-
185
186echo ${RESOLV} | tr ' ' '\n' | \
187awk '
188BEGIN {
189	search=-1;
190}
191{
192	if (/^[[:space:]]+$/) {
193		next;
194	}
195	if (/^Nameserver$/) {
196		printf "\n";
197		search=0;
198		next;
199	}
200	if (search == -1) {
201		printf "search ";
202		search=1;
203	}
204	if (search > 0) {
205		printf "%s%s", (search > 1) ? " " : "", $1;
206		search++;
207		next;
208	}
209	printf "nameserver %s\n", $1;
210}' > ${BSDINSTALL_TMPETC}/resolv.conf
211
212mv $BSDINSTALL_TMPETC/._rc.conf.net $BSDINSTALL_TMPETC/rc.conf.net
213