xref: /freebsd/usr.sbin/bsdinstall/scripts/netconfig (revision 38a52bd3b5cac3da6f7f6eef3dd050e6aa08ebb3)
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
34BSDCFG_SHARE="/usr/share/bsdconfig"
35. $BSDCFG_SHARE/common.subr || exit 1
36
37INTERFACES=""
38BSDDIALOG_ITEMS=""
39
40: ${BSDDIALOG_OK=0}
41: ${BSDDIALOG_CANCEL=1}
42: ${BSDDIALOG_HELP=2}
43: ${BSDDIALOG_EXTRA=3}
44: ${BSDDIALOG_ESC=5}
45: ${BSDDIALOG_ERROR=255}
46
47for IF in `ifconfig -l`; do
48	test "$IF" = "lo0" && continue
49	(ifconfig -g wlan | egrep -wq $IF) && continue
50	INTERFACES="$INTERFACES $IF"
51done
52
53INTERFACES="$INTERFACES $(sysctl -in net.wlan.devices)"
54is_wireless_if() {
55	for IF in $(sysctl -in net.wlan.devices); do
56		if [ $IF = $1 ]; then
57			return 0
58		fi
59	done
60	return 1
61}
62
63for IF in $INTERFACES; do
64	DESC=`sysctl -n dev.$(echo $IF | sed -E 's/([[:alpha:]]*)([[:digit:]]*)/\1.\2/g').%desc`
65	BSDDIALOG_ITEMS="$BSDDIALOG_ITEMS $IF \"$DESC\""
66done
67
68if [ -z "$INTERFACES" ]; then
69	bsddialog --backtitle "$OSNAME Installer" \
70	    --title 'Network Configuration Error' \
71	    --msgbox 'No network interfaces present to configure.' 0 0
72	exit 1
73fi
74
75exec 3>&1
76INTERFACE=`echo $BSDDIALOG_ITEMS | xargs -o bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' --menu 'Please select a network interface to configure:' 0 0 0 2>&1 1>&3`
77if [ $? -eq $BSDDIALOG_CANCEL ]; then exit 1; fi
78exec 3>&-
79
80: > $BSDINSTALL_TMPETC/._rc.conf.net
81
82IFCONFIG_PREFIX=""
83if is_wireless_if $INTERFACE; then
84	NEXT_WLAN_IFACE=wlan0	# XXX
85	echo wlans_$INTERFACE=\"$NEXT_WLAN_IFACE\" >> $BSDINSTALL_TMPETC/._rc.conf.net
86	IFCONFIG_PREFIX="WPA "
87	if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
88		ifconfig $NEXT_WLAN_IFACE create wlandev $INTERFACE
89		ifconfig $NEXT_WLAN_IFACE up
90	fi
91	bsdinstall wlanconfig $NEXT_WLAN_IFACE || exec $0
92	INTERFACE="$NEXT_WLAN_IFACE"
93fi
94
95IPV6_AVAIL=0
96IPV4_AVAIL=0
97sysctl -N kern.features.inet6 > /dev/null 2>&1
98case $? in
990)	IPV6_AVAIL=1 ;;
100esac
101sysctl -N kern.features.inet > /dev/null 2>&1
102case $? in
1030)	IPV4_AVAIL=1 ;;
104esac
105
106if [ ${IPV4_AVAIL} -eq 1 ]; then
107	bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' \
108	    --yesno 'Would you like to configure IPv4 for this interface?' 0 0
109	if [ $? -eq $BSDDIALOG_OK ]; then
110		bsdinstall netconfig_ipv4 ${INTERFACE} "${IFCONFIG_PREFIX}" || \
111		exec $0
112	else
113		IPV4_AVAIL=0
114	fi
115fi
116# In case wlanconfig left an option and we do not support IPv4 we need to write
117# it out on its own.  We cannot write it out with IPv6 as that suffix.
118if [ ${IPV4_AVAIL} -eq 0 -a -n ${IFCONFIG_PREFIX} ]; then
119	echo ifconfig_${INTERFACE}=\"${IFCONFIG_PREFIX}\" >> $BSDINSTALL_TMPETC/._rc.conf.net
120fi
121if [ ${IPV6_AVAIL} -eq 1 ]; then
122	bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' \
123	    --yesno 'Would you like to configure IPv6 for this interface?' 0 0
124	if [ $? -eq $BSDDIALOG_OK ]; then
125		bsdinstall netconfig_ipv6 ${INTERFACE} || exec $0
126	else
127		IPV6_AVAIL=0
128	fi
129fi
130
131SEARCH=""
132IP4_1=""
133IP4_2=""
134IP6_1=""
135IP6_2=""
136while read key value; do
137	case "${key}" in
138	search)		SEARCH="${value}" ;;
139	nameserver)	# is more trick as we have to distinguish v4 and v6
140		case "${value}" in
141		[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*)
142			if [ -z "${IP4_1}" ] ; then
143				IP4_1="${value}"
144			elif [ -z "${IP4_2}" ]; then
145				IP4_2="${value}"
146			fi
147			;;
148		[0-9A-Fa-f:]*:*)
149			if [ -z "${IP6_1}" ] ; then
150				IP6_1="${value}"
151			elif [ -z "${IP6_2}" ]; then
152				IP6_2="${value}"
153			fi
154			;;
155		esac
156		;;
157	# ignore others
158	esac
159done < ${BSDINSTALL_TMPETC}/resolv.conf
160
161RESOLV=""
162if [ ${IPV6_AVAIL} -eq 1 -a ${IPV4_AVAIL} -eq 1 ];  then
163	RESOLV="
164	    'Search' 1 1 \"${SEARCH}\" 1 16 50 50 0
165	    'Nameserver' 2 1 \"Nameserver\" 2 1 11 11 2
166	    'IPv6 DNS #1' 2 1 \"${IP6_1}\" 2 16 50 50 0
167	    'IPv6 DNS #2' 3 1 \"${IP6_2}\" 3 16 50 50 0
168	    'IPv4 DNS #1' 4 1 \"${IP4_1}\" 4 16 16 16 0
169	    'IPv4 DNS #2' 5 1 \"${IP4_2}\" 5 16 16 16 0"
170elif [ ${IPV6_AVAIL} -eq 1 ]; then
171	RESOLV="
172	    'Search' 1 1 \"${SEARCH}\" 1 16 50 50 0
173	    'Nameserver' 2 1 \"Nameserver\" 2 1 11 11 2
174	    'IPv6 DNS #1' 2 1 \"${IP6_1}\" 2 16 50 50 0
175	    'IPv6 DNS #2' 3 1 \"${IP6_2}\" 3 16 50 50 0"
176elif [ ${IPV4_AVAIL} -eq 1 ]; then
177	RESOLV="
178	    'Search' 1 1 \"${SEARCH}\" 1 16 50 50 0
179	    'Nameserver' 2 1 \"Nameserver\" 2 1 11 11 2
180	    'IPv4 DNS #1' 2 1 \"${IP4_1}\" 2 16 16 16 0
181	    'IPv4 DNS #2' 3 1 \"${IP4_2}\" 3 16 16 16 0"
182else
183	exit 0
184fi
185
186exec 3>&1
187RESOLV=$(echo "${RESOLV}" | xargs -o bsddialog --backtitle "$OSNAME Installer" \
188	--title 'Network Configuration' \
189	--mixedform 'Resolver Configuration' 0 0 0 \
1902>&1 1>&3)
191if [ $? -eq $BSDDIALOG_CANCEL ]; then exec $0; fi
192exec 3>&-
193
194echo ${RESOLV} | tr ' ' '\n' | \
195awk '
196BEGIN {
197	search=-1;
198}
199{
200	if (/^[[:space:]]+$/) {
201		next;
202	}
203	if (/^Nameserver$/) {
204		printf "\n";
205		search=0;
206		next;
207	}
208	if (search == -1) {
209		printf "search ";
210		search=1;
211	}
212	if (search > 0) {
213		printf "%s%s", (search > 1) ? " " : "", $1;
214		search++;
215		next;
216	}
217	printf "nameserver %s\n", $1;
218}' > ${BSDINSTALL_TMPETC}/resolv.conf
219
220mv $BSDINSTALL_TMPETC/._rc.conf.net $BSDINSTALL_TMPETC/rc.conf.net
221