#!/bin/sh
#
# Copyright (c) 2000-2023 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
. /etc/emulab/paths.sh

echo "`date`: ${interface}: ${reason}" >>$LOGDIR/dhclient-exit.log 2>&1

if [ x$reason != xREBOOT -a x$reason != xBOUND -a x$reason != xRENEW -a x$reason != xREBIND ]
then
    exit 0
fi

#
# ElabinElab support (inner boss and ops)
#
if [ -e "$ETCDIR/outer_bossnode" ]; then
    #
    # Inner boss or ops. The outer network has now been configured.
    # Call rc.inelab to get the inner control network configured (we
    # have to ask outer boss in the usual manner).
    #
    $BINDIR/rc/rc.inelab >$LOGDIR/dhclient.log 2>&1

    # Then kill as we normally do.
    killall dhclient >>$LOGDIR/dhclient.log 2>&1
    exit 0
fi    

#
# ElabinElab support
#
# The enter-hooks script should weed out outer boss calls, so there
# should be nothing to do here.  However, just in case we smack em down
# again here.
#
if [ x"$new_domain_name_servers" = "x1.1.1.1" ]; then
    exit_status=1
    exit 1
fi

#
# Remember our server IP, real hostname, router IP, etc.
#
echo $new_dhcp_server_identifier > $BOOTDIR/bossip
echo $new_host_name > $BOOTDIR/realname
echo $new_routers > $BOOTDIR/routerip
echo $new_ip_address > $BOOTDIR/myip
echo $new_subnet_mask > $BOOTDIR/mynetmask
echo $new_domain_name > $BOOTDIR/mydomain
if [ -n "$interface" ]; then
    echo $interface > $BOOTDIR/controlif
    mac=`ifconfig $interface 2>/dev/null | grep ether | awk '{print $2}'`
    if [ -n $mac ]; then
	echo $mac > $BOOTDIR/controlmac
    fi
fi

#
# For Xen-based vnodes we record the vnode name where the scripts expect it.
# XXX this works because only Xen-based vnodes DHCP.
#
case "$new_host_name" in
    pcvm*)
	echo $new_host_name > $BOOTDIR/vmname
	;;
esac   

#
# If this is a newnode boot, boss (inner or outer) will have returned with
# no hostname.  We don't need to record anything in this case, so skip it.
#
if [ x"$new_host_name" = x ]; then
    exit 0
fi

#
# XXX compenstate for a stupid long time bug in the dhclient-script
#
curhname=`hostname`
if [ $reason = BOUND -a -z "$curhname" -a -n "$new_host_name" ]; then
    echo "`date`: hostname is $new_host_name" >>$LOGDIR/dhclient-exit.log 2>&1
    hostname $new_host_name
fi

#
# XXX hmm...in FreeBSD 10, the first interface we probe will cause the
# default route to get set to that interface. This will cause any subsequent
# attempt to set the default route to fail and we may wind up without a
# valid default route at this point. So we double check here to make sure
# the default route is correct.
#
if [ -n "$new_routers" ]; then
    gw=`route -n get -inet default | grep gateway`
    gw=${gw##*: }
    if [ "$gw" != "$new_routers" ]; then
	echo "reset default route from \"$gw\" to \"$new_routers\""
	route delete default >/dev/null 2>&1
	route add default $new_routers >/dev/null 2>&1
    fi
fi

##
## This is now handled in sethostname.
##
## We have observed problems where changing the speed/duplex of a link
## leaves DNS a little wonky.  So we whack on it til it responds so that
## the sethostname script won't fail.
##
#if [ "$new_network_number" = "10.200.1.0" ]; then
#    for i in 0 1 2; do
#        if `$BINDIR/tmcc bossinfo >/dev/null 2>&1`; then
#	    break
#	fi
#        echo "`date`: ${interface}: waiting for DNS.." >>$LOGDIR/dhclient-exit.log 2>&1
#        sleep $i
#    done
#fi

#
# See if the Testbed configuration software wants to change the hostname.
# Installed into /etc.  Note: frisbee MFS doesn't have perl and hence
# doesn't have this script.
# 
if [ -x $BINDIR/sethostname ]
then
    $BINDIR/sethostname $new_domain_name >$LOGDIR/dhclient.log 2>&1
fi

#
# Once we have an IP address, we can stop dhclient so that it doesn't get
# in the way.
#
# I'd love to use the dhclient.pid file, but it helpfully has not been
# created at this point
#
killall dhclient >>$LOGDIR/dhclient.log 2>&1

#
# And shutdown all the other interfaces that dhclient was trying
#
for _if in `ifconfig -lu`
do
    if [ $_if != "lo0" -a x$_if != x$interface ]
    then
	echo "taking $_if down"
        ifconfig $_if down remove
    fi >>$LOGDIR/dhclient.log 2>&1
done

echo "`date`: ${interface}: ${reason}: done" >>$LOGDIR/dhclient-exit.log 2>&1

exit 0
