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