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"
43test -z "$IFCONFIG_PREFIX" || IFCONFIG_PREFIX="$2 "
44case "${INTERFACE}" in
45"")	bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' \
46	    --msgbox 'No interface specified for IPv4 configuration.' 0 0
47	exit 1
48	;;
49esac
50
51bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' --yesno 'Would you like to use DHCP to configure this interface?' 0 0
52if [ $? -eq $BSDDIALOG_OK ]; then
53	if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
54		# XXX: get interface down otherwise after installation restart
55		# dhclient does not build a new resolv.conf (see PR262262).
56		ifconfig $INTERFACE down
57		ifconfig $INTERFACE up
58		bsddialog --backtitle "$OSNAME Installer" --infobox "Acquiring DHCP lease..." 0 0
59		err=$( pkill -F /var/run/dhclient/dhclient.${INTERFACE}.pid; dhclient $INTERFACE 2>&1 )
60		if [ $? -ne 0 ]; then
61			f_dprintf "%s" "$err"
62			bsddialog --backtitle "$OSNAME Installer" --msgbox "DHCP lease acquisition failed." 0 0
63			exec $0 ${INTERFACE} "${IFCONFIG_PREFIX}"
64		fi
65	fi
66	sysrc -f $BSDINSTALL_TMPETC/._rc.conf.net ifconfig_$INTERFACE "${IFCONFIG_PREFIX}DHCP"
67	exit 0
68fi
69
70IP_ADDRESS=`ifconfig $INTERFACE inet | awk '/inet/ {printf("%s\n", $2); }'`
71NETMASK=`ifconfig $INTERFACE inet | awk '/inet/ {printf("%s\n", $4); }'`
72ROUTER=`netstat -rn -f inet | awk '/default/ {printf("%s\n", $2);}'`
73
74exec 5>&1
75IF_CONFIG=$(bsddialog --backtitle "$OSNAME Installer" --title 'Network Configuration' --form 'Static Network Interface Configuration' 0 0 0 \
76	'IP Address' 1 1 "$IP_ADDRESS" 1 20 16 0 \
77	'Subnet Mask' 2 1 "$NETMASK" 2 20 16 0 \
78	'Default Router' 3 1 "$ROUTER" 3 20 16 0 \
792>&1 1>&5)
80if [ $? -eq $BSDDIALOG_CANCEL ]; then exit 1; fi
81exec 5>&-
82
83echo $INTERFACE $IF_CONFIG |
84    awk -v prefix="$IFCONFIG_PREFIX" '{
85	printf("ifconfig_%s=\"%s\inet %s netmask %s\"\n", $1, prefix, $2, $3);
86	printf("defaultrouter=\"%s\"\n", $4);
87    }' >> $BSDINSTALL_TMPETC/._rc.conf.net
88retval=$?
89
90if [ "$BSDINSTALL_CONFIGCURRENT" ]; then
91	. $BSDINSTALL_TMPETC/._rc.conf.net
92	if [ -n "$2" ]; then
93		ifconfig $INTERFACE `eval echo \\\$ifconfig_$INTERFACE | sed "s|$2||"`
94	else
95		ifconfig $INTERFACE `eval echo \\\$ifconfig_$INTERFACE`
96	fi
97	if [ "$defaultrouter" ]; then
98		route delete -inet default
99		route add -inet default $defaultrouter
100		retval=$?
101	fi
102fi
103
104exit $retval
105
106################################################################################
107# END
108################################################################################
109