netconfig revision 222611
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: head/usr.sbin/bsdinstall/scripts/netconfig 222611 2011-06-02 14:08:50Z bz $
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
44218799Snwhitehornfor IF in `ifconfig -l`; do
45218799Snwhitehorn	test "$IF" = "lo0" && continue
46218799Snwhitehorn	(ifconfig -g wlan | egrep -wq $IF) && continue
47218799Snwhitehorn	INTERFACES="$INTERFACES $IF"
48218799Snwhitehorn	DESC=`sysctl -n dev.$(echo $IF | sed -E 's/([[:alpha:]]*)([[:digit:]]*)/\1.\2/g').%desc`
49218799Snwhitehorn	DIALOG_TAGS="$DIALOG_TAGS $IF \"$DESC\""
50218799Snwhitehorndone
51218799Snwhitehorn
52218799Snwhitehornexec 3>&1
53218799SnwhitehornINTERFACE=`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`
54218799Snwhitehornif [ $? -eq $DIALOG_CANCEL ]; then exit 1; fi
55218799Snwhitehornexec 3>&-
56218799Snwhitehorn
57222611Sbz: > $BSDINSTALL_TMPETC/rc.conf.net
58222611Sbz
59218799Snwhitehorn# Do a dirty check to see if this a wireless interface -- there should be a
60218799Snwhitehorn# better way
61218799SnwhitehornIFCONFIG_PREFIX=""
62218799Snwhitehornif ifconfig $INTERFACE | grep -q 'media: IEEE 802.11 Wireless'; then
63218799Snwhitehorn	NEXT_WLAN_IFACE=wlan0	# XXX
64218799Snwhitehorn	echo wlans_$INTERFACE=\"$NEXT_WLAN_IFACE\" >> $BSDINSTALL_TMPETC/rc.conf.net
65218799Snwhitehorn	IFCONFIG_PREFIX="WPA "
66218799Snwhitehorn	if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
67218799Snwhitehorn		ifconfig $NEXT_WLAN_IFACE create wlandev $INTERFACE
68218799Snwhitehorn		ifconfig $NEXT_WLAN_IFACE up
69218799Snwhitehorn	fi
70218799Snwhitehorn	bsdinstall wlanconfig $NEXT_WLAN_IFACE || exec $0
71218799Snwhitehorn	INTERFACE="$NEXT_WLAN_IFACE"
72218799Snwhitehornfi
73218799Snwhitehorn
74222468SbzIPV6_AVAIL=0
75222468SbzIPV4_AVAIL=0
76222468Sbzsysctl -N kern.features.inet6 > /dev/null 2>&1
77222468Sbzcase $? in
78222468Sbz0)	IPV6_AVAIL=1 ;;
79222468Sbzesac
80222468Sbzsysctl -N kern.features.inet > /dev/null 2>&1
81222468Sbzcase $? in
82222468Sbz0)	IPV4_AVAIL=1 ;;
83222468Sbzesac
84218799Snwhitehorn
85222468Sbzif [ ${IPV4_AVAIL} -eq 1 ]; then
86222468Sbz	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
87222468Sbz	    --yesno 'Would you like to configure IPv4 for this interface?' 0 0
88222468Sbz	if [ $? -eq $DIALOG_OK ]; then
89222468Sbz		bsdinstall netconfig_ipv4 ${INTERFACE} "${IFCONFIG_PREFIX}" || \
90222468Sbz		exec $0
91222468Sbz	else
92222468Sbz		IPV4_AVAIL=0
93218799Snwhitehorn	fi
94222468Sbzfi
95222468Sbz# In case wlanconfig left an option and we do not support IPv4 we need to write
96222468Sbz# it out on its own.  We cannot write it out with IPv6 as that suffix.
97222468Sbzif [ ${IPV4_AVAIL} -eq 0 -a -n ${IFCONFIG_PREFIX} ]; then
98222468Sbz	echo ifconfig_${INTERFACE}=\"${IFCONFIG_PREFIX}\" >> $BSDINSTALL_TMPETC/rc.conf.net
99222468Sbzfi
100222468Sbzif [ ${IPV6_AVAIL} -eq 1 ]; then
101222468Sbz	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
102222468Sbz	    --yesno 'Would you like to configure IPv6 for this interface?' 0 0
103222468Sbz	if [ $? -eq $DIALOG_OK ]; then
104222468Sbz		bsdinstall netconfig_ipv6 ${INTERFACE} || exec $0
105222468Sbz	else
106222468Sbz		IPV6_AVAIL=0
107222468Sbz	fi
108222468Sbzfi
109222468Sbz
110222468SbzSEARCH=""
111222468SbzIP4_1=""
112222468SbzIP4_2=""
113222468SbzIP6_1=""
114222468SbzIP6_2=""
115222468Sbzwhile read key value; do
116222468Sbz	case "${key}" in
117222468Sbz	search)		SEARCH="${value}" ;;
118222468Sbz	nameserver)	# is more trick as we have to distinguish v4 and v6
119222468Sbz		case "${value}" in
120222468Sbz		[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*)
121222468Sbz			if [ -z "${IP4_1}" ] ; then
122222468Sbz				IP4_1="${value}"
123222468Sbz			elif [ -z "${IP4_2}" ]; then
124222468Sbz				IP4_2="${value}"
125222468Sbz			fi
126222468Sbz			;;
127222468Sbz		[0-9A-Fa-f:]*:*)
128222468Sbz			if [ -z "${IP6_1}" ] ; then
129222468Sbz				IP6_1="${value}"
130222468Sbz			elif [ -z "${IP6_2}" ]; then
131222468Sbz				IP6_2="${value}"
132222468Sbz			fi
133222468Sbz			;;
134222468Sbz		esac
135222468Sbz		;;
136222468Sbz	# ignore others
137222468Sbz	esac
138222468Sbzdone < ${BSDINSTALL_TMPETC}/resolv.conf
139222468Sbz
140222468SbzRESOLV=""
141222468Sbzif [ ${IPV6_AVAIL} -eq 1 -a ${IPV4_AVAIL} -eq 1 ];  then
142222468Sbz	RESOLV="
143222468Sbz	    'Search' 1 0 \"${SEARCH}\" 1 16 50 0 0
144222468Sbz	    'Nameserver' 2 0 \"Nameserver\" 2 16 50 0 2
145222468Sbz	    'IPv6 DNS #1' 2 0 \"${IP6_1}\" 2 16 50 0 0
146222468Sbz	    'IPv6 DNS #2' 3 0 \"${IP6_2}\" 3 16 50 0 0
147222468Sbz	    'IPv4 DNS #1' 4 0 \"${IP4_1}\" 4 16 16 0 0
148222468Sbz	    'IPv4 DNS #2' 5 0 \"${IP4_2}\" 5 16 16 0 0"
149222468Sbzelif [ ${IPV6_AVAIL} -eq 1 ]; then
150222468Sbz	RESOLV="
151222468Sbz	    'Search' 1 0 \"${SEARCH}\" 1 16 50 0 0
152222468Sbz	    'Nameserver' 2 0 \"Nameserver\" 2 16 50 0 2
153222468Sbz	    'IPv6 DNS #1' 2 0 \"${IP6_1}\" 2 16 50 0 0
154222468Sbz	    'IPv6 DNS #2' 3 0 \"${IP6_2}\" 3 16 50 0 0"
155222468Sbzelif [ ${IPV4_AVAIL} -eq 1 ]; then
156222468Sbz	RESOLV="
157222468Sbz	    'Search' 1 0 \"${SEARCH}\" 1 16 50 0 0
158222468Sbz	    'Nameserver' 2 0 \"Nameserver\" 2 16 50 0 2
159222468Sbz	    'IPv4 DNS #1' 2 0 \"${IP4_1}\" 2 16 16 0 0
160222468Sbz	    'IPv4 DNS #2' 3 0 \"${IP4_2}\" 3 16 16 0 0"
161222468Sbzelse
162218799Snwhitehorn	exit 0
163218799Snwhitehornfi
164218799Snwhitehorn
165218799Snwhitehornexec 3>&1
166222468SbzRESOLV=$(echo "${RESOLV}" | xargs dialog --backtitle 'FreeBSD Installer' \
167222468Sbz	--title 'Network Configuration' \
168222468Sbz	--mixedform 'Resovler Configuration' 0 0 0 \
169218799Snwhitehorn2>&1 1>&3)
170218799Snwhitehornif [ $? -eq $DIALOG_CANCEL ]; then exec $0; fi
171218799Snwhitehornexec 3>&-
172218799Snwhitehorn
173222468Sbzecho ${RESOLV} | tr ' ' '\n' | \
174222468Sbzawk '
175222468SbzBEGIN {
176222468Sbz	search=1
177222468Sbz	printf "search ";
178222468Sbz}
179222468Sbz{
180222468Sbz	if (/^[[:space:]]+$/) {
181222468Sbz		next;
182222468Sbz	}
183222468Sbz	if (/^Nameserver$/) {
184222468Sbz		printf "\n";
185222468Sbz		search=0;
186222468Sbz		next;
187222468Sbz	}
188222468Sbz	if (search > 0) {
189222468Sbz		printf "%s%s", (search > 1) ? "," : "", $1;
190222468Sbz		next;
191222468Sbz	}
192222468Sbz	printf "nameserver %s\n", $1;
193222468Sbz}' > ${BSDINSTALL_TMPETC}/resolv.conf
194218799Snwhitehorn
195