#!/bin/bash
#
# $Id: testdhcp,v 1.8 2002/02/27 06:35:25 andrew Exp $
#
# by Andrew McMillan, Catalyst IT Ltd, (c) 2001 licensed
# for use under the GPL version 2
#
# This script tests whether we are at a specific DHCP-assigned IP address.
# DHCP IP address assignment should be done if it hasn't already.
#
###############################################
# NOTE:  This script is something of a work in progress.  I can't have experience
# with all possible DHCP clients, so if you need help getting it working with what
# you happen to use, feel free to call upon me.
###############################################
#
# Since whereami tests accept a single parameter, the command line should be like:
#     testdhcp eth1,192.168.3.7
# You may also use bash pattern matching characters.  Examples:
#     testdhcp 192.168.3.*
#     testdhcp 10.*
#     testdhcp 192.168.1.[01][0-9]

# Turn on execution tracing, for debugging...
[ "$DEBUGWHEREAMI" = "1" ] && set -o xtrace

# We split our single parameter into two parts using funky bash pattern matching...
IFACE=${1/,*}
IP_ADDRESS=${1/$IFACE,}
# All this mucking around means that we can default _or_ set INTERFACE letting callers say:
#     testdhcp 192.168.3.7
# if they use eth0, as 99.9% of us do, or have "set INTERFACE eth2" in their detect.conf
if [ "$IFACE" = "$IP_ADDRESS" ] ; then
  # We can also set $INTERFACE externally and that will be used as the default.
  INTERFACE=${INTERFACE:-"eth0"}
else
  INTERFACE=${IFACE}
fi

BASEDIR=${BASEDIR:-"/etc/whereami"}

PUMPFILE=/sbin/pump
DHCLIENT=/sbin/dhclient

#
# Now deal with what we have to do using whatever DHCP client is available
# Since we may call this multiple times in a short period, we don't want to
# actually be always _getting_ a DHCP address, so we have to understand
# how the various clients work...
#
if [ -x $PUMPFILE ]; then
  # We prefer this one at the moment, and it is the Debian (and RH) default
  # Also consider: It might be advisable to set a timeout in your /etc/pump.conf
  if [ ! -s $BASEDIR/whereami.pump.$INTERFACE -o ! -f $LOCKDIR/whereami.started -o $BASEDIR/whereami.pump.$INTERFACE -ot $LOCKDIR/whereami.started ]; then
    # Prod the DHCP server for a new address
    $PUMPFILE --interface $INTERFACE
    # Get the status into a file.
    if ! $PUMPFILE --interface $INTERFACE --status >$BASEDIR/whereami.pump.$INTERFACE; then
      # OK, looks like it's got it's knickers knotted and we need to nuke it and try again..
      killall $PUMPFILE
      sleep 1
      $PUMPFILE --interface $INTERFACE
      $PUMPFILE --interface $INTERFACE --status >$BASEDIR/whereami.pump.$INTERFACE
    fi
  fi
  DHCP_ADDRESS=`grep "IP: " $BASEDIR/whereami.pump.$INTERFACE | tr -d " " | cut -f2 -d:`
elif [ -x $DHCLIENT ]; then
  # ISC dhcp client.
  # Use the -e flag to make sure that dhclient only runs in the background if it
  # has a lease.  Thus we know if we see dhclient in the background, that the
  # interface has got an IP address from a DHCP server.

  # Should we kill an old instance?  Yes if we have no IP address, or dhclient
  #  is running without -e
  if /sbin/ifconfig $INTERFACE | grep -q "inet addr"; then
    # look for running dhclient without the -e flag
    dhcli_pid="`ps ax | grep dhclient | grep $INTERFACE | grep -v -- "-e" \
          | cut -c-5 `"
  else
    # We have no IP address - kill dhclient regardless
    dhcli_pid="`ps ax | grep dhclient | grep $INTERFACE | cut -c-5`"
  fi

  [ -n "$dhcli_pid" ] && kill $dhcli_pid

  # if dhclient is not running now, start it
  if ! ps ax | grep dhclient | grep -q $INTERFACE; then
    $DHCLIENT -e $INTERFACE
  fi

  # if dhclient is still running, we have an IP address
  if ps ax | grep dhclient | grep -q $INTERFACE ; then
    DHCP_ADDRESS="`/sbin/ifconfig $INTERFACE | head -2 | tail -1 | cut -d: -f2 | cut -d\  -f1`"
  else
    DHCP_ADDRESS=
  fi

#
# If you figure out what to do to make this client work.  It's not available in current
# Debian, causing me to skip it for the now...
#
# elif [ -x /sbin/dhcpcd ]; then
  # # -- we probably need to do this :-)
  # if [ /var/dhcp/dhclient.leases -ot $LOCKDIR/whereami.started ]; then
    # # Ensure DHCP client isn't running
    # /etc/init.d/dhcpcd stop >>/dev/null 2>&1
  # fi
  # grep "IP: " /var/dhcp/dhclient.leases | tr -d " " | cut -f2 -d:
else
  logger -p user.error -t whereami -i "No suitable DHCP client found!"
  exit 1;
fi

# echo "DHCP=$DHCP_ADDRESS"
[ "${DHCP_ADDRESS/#$IP_ADDRESS/FOUND}" = "FOUND" ] && exit 0

# echo "Sorry $IP_ADDRESS not found"
exit 1
