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