xref: /freebsd/usr.sbin/bsdinstall/scripts/netconfig_ipv4 (revision e01a4776d5c96bcab3eb6e3db57a85635395df27)
17986af23SBjoern A. Zeeb#!/bin/sh
27986af23SBjoern A. Zeeb#-
37986af23SBjoern A. Zeeb# Copyright (c) 2011 Nathan Whitehorn
47986af23SBjoern A. Zeeb# All rights reserved.
57986af23SBjoern A. Zeeb#
67986af23SBjoern A. Zeeb# Redistribution and use in source and binary forms, with or without
77986af23SBjoern A. Zeeb# modification, are permitted provided that the following conditions
87986af23SBjoern A. Zeeb# are met:
97986af23SBjoern A. Zeeb# 1. Redistributions of source code must retain the above copyright
107986af23SBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer.
117986af23SBjoern A. Zeeb# 2. Redistributions in binary form must reproduce the above copyright
127986af23SBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer in the
137986af23SBjoern A. Zeeb#    documentation and/or other materials provided with the distribution.
147986af23SBjoern A. Zeeb#
157986af23SBjoern A. Zeeb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
167986af23SBjoern A. Zeeb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
177986af23SBjoern A. Zeeb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
187986af23SBjoern A. Zeeb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
197986af23SBjoern A. Zeeb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
207986af23SBjoern A. Zeeb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
217986af23SBjoern A. Zeeb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
227986af23SBjoern A. Zeeb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
237986af23SBjoern A. Zeeb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
247986af23SBjoern A. Zeeb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
257986af23SBjoern A. Zeeb# SUCH DAMAGE.
267986af23SBjoern A. Zeeb#
277986af23SBjoern A. Zeeb# $FreeBSD$
287986af23SBjoern A. Zeeb
297986af23SBjoern A. Zeeb: ${DIALOG_OK=0}
307986af23SBjoern A. Zeeb: ${DIALOG_CANCEL=1}
317986af23SBjoern A. Zeeb: ${DIALOG_HELP=2}
327986af23SBjoern A. Zeeb: ${DIALOG_EXTRA=3}
337986af23SBjoern A. Zeeb: ${DIALOG_ITEM_HELP=4}
347986af23SBjoern A. Zeeb: ${DIALOG_ESC=255}
357986af23SBjoern A. Zeeb
367986af23SBjoern A. ZeebINTERFACE=$1
377986af23SBjoern A. ZeebIFCONFIG_PREFIX="$2"
383d50f942SNathan Whitehorntest -z "$IFCONFIG_PREFIX" || IFCONFIG_PREFIX="$2 "
397986af23SBjoern A. Zeebcase "${INTERFACE}" in
407986af23SBjoern A. Zeeb"")	dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' \
417986af23SBjoern A. Zeeb	    --msgbox 'No interface specified for IPv4 configuration.' 0 0
427986af23SBjoern A. Zeeb	exit 1
437986af23SBjoern A. Zeeb	;;
447986af23SBjoern A. Zeebesac
457986af23SBjoern A. Zeeb
467986af23SBjoern A. Zeebdialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' --yesno 'Would you like to use DHCP to configure this interface?' 0 0
477986af23SBjoern A. Zeebif [ $? -eq $DIALOG_OK ]; then
48d63d020eSBjoern A. Zeeb	echo ifconfig_$INTERFACE=\"${IFCONFIG_PREFIX}DHCP\" >> $BSDINSTALL_TMPETC/._rc.conf.net
497986af23SBjoern A. Zeeb
507986af23SBjoern A. Zeeb	if [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
517986af23SBjoern A. Zeeb		dialog --backtitle 'FreeBSD Installer' --infobox "Acquiring DHCP lease..." 0 0
527986af23SBjoern A. Zeeb		dhclient $INTERFACE 2>> $BSDINSTALL_LOG
537986af23SBjoern A. Zeeb		if [ $? -ne 0 ]; then
547986af23SBjoern A. Zeeb			dialog --backtitle 'FreeBSD Installer' --msgbox "DHCP lease acquisition failed." 0 0
557986af23SBjoern A. Zeeb			exec $0 ${INTERFACE} "${IFCONFIG_PREFIX}"
567986af23SBjoern A. Zeeb		fi
577986af23SBjoern A. Zeeb	fi
587986af23SBjoern A. Zeeb	exit 0
597986af23SBjoern A. Zeebfi
607986af23SBjoern A. Zeeb
617986af23SBjoern A. ZeebIP_ADDRESS=`ifconfig $INTERFACE inet | awk '/inet/ {printf("%s\n", $2); }'`
627986af23SBjoern A. ZeebNETMASK=`ifconfig $INTERFACE inet | awk '/inet/ {printf("%s\n", $4); }'`
637986af23SBjoern A. ZeebROUTER=`netstat -rn -f inet | awk '/default/ {printf("%s\n", $2);}'`
647986af23SBjoern A. Zeeb
657986af23SBjoern A. Zeebexec 3>&1
667986af23SBjoern A. ZeebIF_CONFIG=$(dialog --backtitle 'FreeBSD Installer' --title 'Network Configuration' --form 'Static Network Interface Configuration' 0 0 0 \
677986af23SBjoern A. Zeeb	'IP Address' 1 0 "$IP_ADDRESS" 1 20 16 0 \
687986af23SBjoern A. Zeeb	'Subnet Mask' 2 0 "$NETMASK" 2 20 16 0 \
697986af23SBjoern A. Zeeb	'Default Router' 3 0 "$ROUTER" 3 20 16 0 \
707986af23SBjoern A. Zeeb2>&1 1>&3)
717986af23SBjoern A. Zeebif [ $? -eq $DIALOG_CANCEL ]; then exit 1; fi
727986af23SBjoern A. Zeebexec 3>&-
737986af23SBjoern A. Zeeb
747986af23SBjoern A. Zeebecho $INTERFACE $IF_CONFIG |
757986af23SBjoern A. Zeeb    awk -v prefix="$IFCONFIG_PREFIX" '{
76*e01a4776SKevin Lo	printf("ifconfig_%s=\"%s\inet %s netmask %s\"\n", $1, prefix, $2, $3);
777986af23SBjoern A. Zeeb	printf("defaultrouter=\"%s\"\n", $4);
78d63d020eSBjoern A. Zeeb    }' >> $BSDINSTALL_TMPETC/._rc.conf.net
797986af23SBjoern A. Zeeb
807986af23SBjoern A. Zeebif [ ! -z $BSDINSTALL_CONFIGCURRENT ]; then
81d63d020eSBjoern A. Zeeb	. $BSDINSTALL_TMPETC/._rc.conf.net
82b5cb9d4fSBjoern A. Zeeb	ifconfig $INTERFACE `eval echo \\\$ifconfig_$INTERFACE`
835b046b47SBjoern A. Zeeb	if [ -n "${defaultrouter}" ]; then
847986af23SBjoern A. Zeeb		route delete -inet default
857986af23SBjoern A. Zeeb		route add -inet default $defaultrouter
867986af23SBjoern A. Zeeb	fi
875b046b47SBjoern A. Zeebfi
887986af23SBjoern A. Zeeb
89