netconfig revision 226741
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: head/usr.sbin/bsdinstall/scripts/netconfig 226741 2011-10-25 16:35:48Z nwhitehorn $
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
44for IF in `ifconfig -l`; do
45	test "$IF" = "lo0" && continue
46	(ifconfig -g wlan | egrep -wq $IF) && continue
47	INTERFACES="$INTERFACES $IF"
48	DESC=`sysctl -n dev.$(echo $IF | sed -E 's/([[:alpha:]]*)([[:digit:]]*)/\1.\2/g').%desc`
49	DIALOG_TAGS="$DIALOG_TAGS $IF \"$DESC\""
50done
51
52if [ -z "$INTERFACES" ]; then
53	dialog --backtitle 'FreeBSD Installer' \
54	    --title 'Network Configuration Error' \
55	    --msgbox 'No network interfaces present to configure.' 0 0
56	exit 1
57fi
58
59exec 3>&1
60INTERFACE=`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`
61if [ $? -eq $DIALOG_CANCEL ]; then exit 1; fi
62exec 3>&-
63
64: > $BSDINSTALL_TMPETC/._rc.conf.net
65
66# Do a dirty check to see if this a wireless interface -- there should be a
67# better way
68IFCONFIG_PREFIX=""
69if ifconfig $INTERFACE | grep -q 'media: IEEE 802.11 Wireless'; then
70	NEXT_WLAN_IFACE=wlan0	# XXX
71	echo wlans_$INTERFACE=\"$NEXT_WLAN_IFACE\" >> $BSDINSTALL_TMPETC/._rc.conf.net
72	IFCONFIG_PREFIX="WPA "
73	if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
74		ifconfig $NEXT_WLAN_IFACE create wlandev $INTERFACE
75		ifconfig $NEXT_WLAN_IFACE up
76	fi
77	bsdinstall wlanconfig $NEXT_WLAN_IFACE || exec $0
78	INTERFACE="$NEXT_WLAN_IFACE"
79fi
80
81IPV6_AVAIL=0
82IPV4_AVAIL=0
83sysctl -N kern.features.inet6 > /dev/null 2>&1
84case $? in
850)	IPV6_AVAIL=1 ;;
86esac
87sysctl -N kern.features.inet > /dev/null 2>&1
88case $? in
890)	IPV4_AVAIL=1 ;;
90esac
91
92if [ ${IPV4_AVAIL} -eq 1 ]; then
93	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
94	    --yesno 'Would you like to configure IPv4 for this interface?' 0 0
95	if [ $? -eq $DIALOG_OK ]; then
96		bsdinstall netconfig_ipv4 ${INTERFACE} "${IFCONFIG_PREFIX}" || \
97		exec $0
98	else
99		IPV4_AVAIL=0
100	fi
101fi
102# In case wlanconfig left an option and we do not support IPv4 we need to write
103# it out on its own.  We cannot write it out with IPv6 as that suffix.
104if [ ${IPV4_AVAIL} -eq 0 -a -n ${IFCONFIG_PREFIX} ]; then
105	echo ifconfig_${INTERFACE}=\"${IFCONFIG_PREFIX}\" >> $BSDINSTALL_TMPETC/._rc.conf.net
106fi
107if [ ${IPV6_AVAIL} -eq 1 ]; then
108	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
109	    --yesno 'Would you like to configure IPv6 for this interface?' 0 0
110	if [ $? -eq $DIALOG_OK ]; then
111		bsdinstall netconfig_ipv6 ${INTERFACE} || exec $0
112	else
113		IPV6_AVAIL=0
114	fi
115fi
116
117SEARCH=""
118IP4_1=""
119IP4_2=""
120IP6_1=""
121IP6_2=""
122while read key value; do
123	case "${key}" in
124	search)		SEARCH="${value}" ;;
125	nameserver)	# is more trick as we have to distinguish v4 and v6
126		case "${value}" in
127		[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*)
128			if [ -z "${IP4_1}" ] ; then
129				IP4_1="${value}"
130			elif [ -z "${IP4_2}" ]; then
131				IP4_2="${value}"
132			fi
133			;;
134		[0-9A-Fa-f:]*:*)
135			if [ -z "${IP6_1}" ] ; then
136				IP6_1="${value}"
137			elif [ -z "${IP6_2}" ]; then
138				IP6_2="${value}"
139			fi
140			;;
141		esac
142		;;
143	# ignore others
144	esac
145done < ${BSDINSTALL_TMPETC}/resolv.conf
146
147RESOLV=""
148if [ ${IPV6_AVAIL} -eq 1 -a ${IPV4_AVAIL} -eq 1 ];  then
149	RESOLV="
150	    'Search' 1 0 \"${SEARCH}\" 1 16 50 0 0
151	    'Nameserver' 2 0 \"Nameserver\" 2 16 50 0 2
152	    'IPv6 DNS #1' 2 0 \"${IP6_1}\" 2 16 50 0 0
153	    'IPv6 DNS #2' 3 0 \"${IP6_2}\" 3 16 50 0 0
154	    'IPv4 DNS #1' 4 0 \"${IP4_1}\" 4 16 16 0 0
155	    'IPv4 DNS #2' 5 0 \"${IP4_2}\" 5 16 16 0 0"
156elif [ ${IPV6_AVAIL} -eq 1 ]; then
157	RESOLV="
158	    'Search' 1 0 \"${SEARCH}\" 1 16 50 0 0
159	    'Nameserver' 2 0 \"Nameserver\" 2 16 50 0 2
160	    'IPv6 DNS #1' 2 0 \"${IP6_1}\" 2 16 50 0 0
161	    'IPv6 DNS #2' 3 0 \"${IP6_2}\" 3 16 50 0 0"
162elif [ ${IPV4_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	    'IPv4 DNS #1' 2 0 \"${IP4_1}\" 2 16 16 0 0
167	    'IPv4 DNS #2' 3 0 \"${IP4_2}\" 3 16 16 0 0"
168else
169	exit 0
170fi
171
172exec 3>&1
173RESOLV=$(echo "${RESOLV}" | xargs dialog --backtitle 'FreeBSD Installer' \
174	--title 'Network Configuration' \
175	--mixedform 'Resolver Configuration' 0 0 0 \
1762>&1 1>&3)
177if [ $? -eq $DIALOG_CANCEL ]; then exec $0; fi
178exec 3>&-
179
180echo ${RESOLV} | tr ' ' '\n' | \
181awk '
182BEGIN {
183	search=-1;
184}
185{
186	if (/^[[:space:]]+$/) {
187		next;
188	}
189	if (/^Nameserver$/) {
190		printf "\n";
191		search=0;
192		next;
193	}
194	if (search == -1) {
195		printf "search ";
196		search=1;
197	}
198	if (search > 0) {
199		printf "%s%s", (search > 1) ? " " : "", $1;
200		search++;
201		next;
202	}
203	printf "nameserver %s\n", $1;
204}' > ${BSDINSTALL_TMPETC}/resolv.conf
205
206mv $BSDINSTALL_TMPETC/._rc.conf.net $BSDINSTALL_TMPETC/rc.conf.net
207