xref: /freebsd/usr.sbin/bsdinstall/scripts/netconfig_ipv4 (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1#!/bin/sh
2#-
3# Copyright (c) 2011 Nathan Whitehorn
4# Copyright (c) 2013-2015 Devin Teske
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." "$0"
34f_include $BSDCFG_SHARE/dialog.subr
35
36############################################################ MAIN
37
38: ${BSDDIALOG_OK=0}
39: ${BSDDIALOG_CANCEL=1}
40
41INTERFACE=$1
42IFCONFIG_PREFIX="$2"
43AUTO="${3:-}"
44test -z "$IFCONFIG_PREFIX" || IFCONFIG_PREFIX="$2 "
45case "${INTERFACE}" in
46"")	bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' \
47	    --msgbox 'No interface specified for IPv4 configuration.' 0 0
48	exit 1
49	;;
50esac
51case "$AUTO" in
52""|auto)
53	;;
54*)
55	bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' \
56	    --msgbox "Bad auto option '$AUTO'." 0 0
57	exit 1
58	;;
59esac
60
61if [ -n "$AUTO" ]; then
62	DHCP=1
63else
64	bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' --yesno 'Would you like to use DHCP to configure this interface?' 0 0
65	if [ $? -eq $BSDDIALOG_OK ]; then
66		DHCP=1
67	else
68		DHCP=0
69	fi
70fi
71if [ $DHCP -eq 1 ]; then
72	if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
73		# XXX: get interface down otherwise after installation restart
74		# dhclient does not build a new resolv.conf (see PR262262).
75		ifconfig $INTERFACE down
76		ifconfig $INTERFACE up
77		bsddialog --backtitle "$OSNAME Installer" --infobox "Acquiring DHCP lease..." 0 0
78		err=$( pkill -F /var/run/dhclient/dhclient.${INTERFACE}.pid; dhclient $INTERFACE 2>&1 )
79		if [ $? -ne 0 ]; then
80			f_dprintf "%s" "$err"
81			if [ -n "$AUTO" ]; then
82				exit 1
83			fi
84			bsddialog --backtitle "$OSNAME Installer" --msgbox "DHCP lease acquisition failed." 0 0
85			exec $0 ${INTERFACE} "${IFCONFIG_PREFIX}"
86		fi
87	fi
88	sysrc -f $BSDINSTALL_TMPETC/._rc.conf.net ifconfig_$INTERFACE="${IFCONFIG_PREFIX}DHCP"
89	exit 0
90fi
91
92IP_ADDRESS=`ifconfig $INTERFACE inet | awk '/inet/ {printf("%s\n", $2); }'`
93NETMASK=`ifconfig $INTERFACE inet | awk '/inet/ {printf("%s\n", $4); }'`
94ROUTER=`netstat -rn -f inet | awk '/default/ {printf("%s\n", $2);}'`
95
96exec 5>&1
97IF_CONFIG=$(bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' --form 'Static Network Interface Configuration' 0 0 0 \
98	'IP Address' 1 1 "$IP_ADDRESS" 1 20 16 0 \
99	'Subnet Mask' 2 1 "$NETMASK" 2 20 16 0 \
100	'Default Router' 3 1 "$ROUTER" 3 20 16 0 \
1012>&1 1>&5)
102if [ $? -eq $BSDDIALOG_CANCEL ]; then exit 1; fi
103exec 5>&-
104
105echo $INTERFACE $IF_CONFIG |
106    awk -v prefix="$IFCONFIG_PREFIX" '{
107	printf("ifconfig_%s=\"%s\inet %s netmask %s\"\n", $1, prefix, $2, $3);
108	printf("defaultrouter=\"%s\"\n", $4);
109    }' >> $BSDINSTALL_TMPETC/._rc.conf.net
110retval=$?
111
112if [ "$BSDINSTALL_CONFIGCURRENT" ]; then
113	. $BSDINSTALL_TMPETC/._rc.conf.net
114	if [ -n "$2" ]; then
115		ifconfig $INTERFACE `eval echo \\\$ifconfig_$INTERFACE | sed "s|$2||"`
116	else
117		ifconfig $INTERFACE `eval echo \\\$ifconfig_$INTERFACE`
118	fi
119	if [ "$defaultrouter" ]; then
120		route delete -inet default
121		route add -inet default $defaultrouter
122		retval=$?
123	fi
124fi
125
126exit $retval
127
128################################################################################
129# END
130################################################################################
131