netconfig revision 225430
1218799Snwhitehorn#!/bin/sh
2218799Snwhitehorn#-
3218799Snwhitehorn# Copyright (c) 2011 Nathan Whitehorn
4218799Snwhitehorn# All rights reserved.
5218799Snwhitehorn# Copyright (c) 2011 The FreeBSD Foundation
6218799Snwhitehorn# All rights reserved.
7218799Snwhitehorn#
8218799Snwhitehorn# Portions of this software were developed by Bjoern Zeeb
9218799Snwhitehorn# under sponsorship from the FreeBSD Foundation.
10218799Snwhitehorn#
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 225430 2011-09-07 00:48:58Z 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
57218799Snwhitehorn: > $BSDINSTALL_TMPETC/._rc.conf.net
58218799Snwhitehorn
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
74218799SnwhitehornIPV6_AVAIL=0
75218799SnwhitehornIPV4_AVAIL=0
76218799Snwhitehornsysctl -N kern.features.inet6 > /dev/null 2>&1
77218799Snwhitehorncase $? in
78218799Snwhitehorn0)	IPV6_AVAIL=1 ;;
79218799Snwhitehornesac
80218799Snwhitehornsysctl -N kern.features.inet > /dev/null 2>&1
81218799Snwhitehorncase $? in
82218799Snwhitehorn0)	IPV4_AVAIL=1 ;;
83218799Snwhitehornesac
84218799Snwhitehorn
85218799Snwhitehornif [ ${IPV4_AVAIL} -eq 1 ]; then
86218799Snwhitehorn	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
87218799Snwhitehorn	    --yesno 'Would you like to configure IPv4 for this interface?' 0 0
88218799Snwhitehorn	if [ $? -eq $DIALOG_OK ]; then
89218799Snwhitehorn		bsdinstall netconfig_ipv4 ${INTERFACE} "${IFCONFIG_PREFIX}" || \
90218799Snwhitehorn		exec $0
91218799Snwhitehorn	else
92218799Snwhitehorn		IPV4_AVAIL=0
93218799Snwhitehorn	fi
94218799Snwhitehornfi
95218799Snwhitehorn# In case wlanconfig left an option and we do not support IPv4 we need to write
96218799Snwhitehorn# it out on its own.  We cannot write it out with IPv6 as that suffix.
97218799Snwhitehornif [ ${IPV4_AVAIL} -eq 0 -a -n ${IFCONFIG_PREFIX} ]; then
98218799Snwhitehorn	echo ifconfig_${INTERFACE}=\"${IFCONFIG_PREFIX}\" >> $BSDINSTALL_TMPETC/._rc.conf.net
99218799Snwhitehornfi
100218799Snwhitehornif [ ${IPV6_AVAIL} -eq 1 ]; then
101218799Snwhitehorn	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
102218799Snwhitehorn	    --yesno 'Would you like to configure IPv6 for this interface?' 0 0
103218799Snwhitehorn	if [ $? -eq $DIALOG_OK ]; then
104218799Snwhitehorn		bsdinstall netconfig_ipv6 ${INTERFACE} || exec $0
105218799Snwhitehorn	else
106218799Snwhitehorn		IPV6_AVAIL=0
107218799Snwhitehorn	fi
108218799Snwhitehornfi
109218799Snwhitehorn
110218799SnwhitehornSEARCH=""
111218799SnwhitehornIP4_1=""
112218799SnwhitehornIP4_2=""
113218799SnwhitehornIP6_1=""
114218799SnwhitehornIP6_2=""
115218799Snwhitehornwhile read key value; do
116218799Snwhitehorn	case "${key}" in
117218799Snwhitehorn	search)		SEARCH="${value}" ;;
118218799Snwhitehorn	nameserver)	# is more trick as we have to distinguish v4 and v6
119218799Snwhitehorn		case "${value}" in
120218799Snwhitehorn		[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*)
121218799Snwhitehorn			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 'Resolver 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}
178{
179	if (/^[[:space:]]+$/) {
180		next;
181	}
182	if (/^Nameserver$/) {
183		printf "\n";
184		search=0;
185		next;
186	}
187	if (search == -1) {
188		printf "search ";
189		search=1;
190	}
191	if (search > 0) {
192		printf "%s%s", (search > 1) ? " " : "", $1;
193		search++;
194		next;
195	}
196	printf "nameserver %s\n", $1;
197}' > ${BSDINSTALL_TMPETC}/resolv.conf
198
199mv $BSDINSTALL_TMPETC/._rc.conf.net $BSDINSTALL_TMPETC/rc.conf.net
200