#!/bin/bash
# dhclient-script for Linux. Dan Halbert, March, 1997.
# Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
# No guarantees about this. I'm a novice at the details of Linux
# networking.

# Notes:

# 0. This script is based on the netbsd script supplied with dhcp-970306.

# 1. ifconfig down apparently deletes all relevant routes and flushes
# the arp cache, so this doesn't need to be done explicitly.

# 2. The alias address handling here has not been tested AT ALL.
# I'm just going by the doc of modern Linux ip aliasing, which uses
# notations like eth0:0, eth0:1, for each alias.

# 3. I have to calculate the network address, and calculate the broadcast
# address if it is not supplied. This might be much more easily done
# by the dhclient C code, and passed on.

# 4. TIMEOUT not tested. ping has a flag I don't know, and I'm suspicious
# of the $1 in its args.


# Debugging:
#
# logs entire run of dhclient-script to /var/log/dhclient-script, 
# if DHCLIENT_DEBUG is set in sysconfig/network/dhcp
#
eval `grep "^DHCLIENT_DEBUG=" /etc/sysconfig/network/dhcp 2>/dev/null`
if [ "$DHCLIENT_DEBUG" = yes ]; then
  set -a # allexport
  (
    echo '****************' 
    echo "$0 $*"
    date
    echo '----------------'
    set
    echo '----------------'
  ) >> /var/log/dhclient-script.log
  exec 2>> /var/log/dhclient-script.log
  set +a
  set -x
fi

# netconfig makes all "additional" modifications as DNS or NTP
use_netconfig() {
  test -x /sbin/netconfig
}
netconfig_modify() {
  if test -x /sbin/netconfig -a -n "$interface" ; then
    {
      echo "INTERFACE='$interface'"
      for v in ${!new_*}; do
        case $v in
          (new_ip_address)            k='IPADDR'             ;;
          (new_subnet_mask)           k='NETMASK'            ;;
          (new_network_number)        k='NETWORK'            ;;
          (new_broadcast_address)     k='BROADCAST'          ;;
          (new_interface_mtu)         k='MTU'                ;;
        # (new_static_routes)         k='ROUTES'             ;;
          (new_routers)               k='GATEWAYS'           ;;
          (new_host_name)             k='HOSTNAME'           ;;
          (new_domain_name)           k='DNSDOMAIN'          ;;
          (new_domain_name_servers)   k='DNSSERVERS'         ;;
          (new_ntp_servers)           k='NTPSERVERS'         ;;
          (new_nis_domain)            k='NISDOMAIN'          ;;
          (new_nis_servers)           k='NISSERVERS'         ;;
          (new_root_path)             k='ROOTPATH'           ;;
          (new_dhcp_server_identifier)k='DHCPSID'            ;;
          (new_lpr_servers)           k='LPRSERVER'          ;;
          (new_log_servers)           k='LOGSERVER'          ;;
          (new_netbios_dd_server)     k='NETBIOSDDSERVER'    ;;
          (new_netbios_name_servers)  k='NETBIOSNAMESERVER'  ;;
          (new_netbios_node_type)     k='NETBIOSNODETYPE'    ;;
          (new_netbios_scope)         k='NETBIOSSCOPE'       ;;
          (*)                         k="dhclient_${v#new_}" ;;
        esac
        [ "k${k}" != k ] && echo "${k}='${!v}'"
      done
    } | /sbin/netconfig modify -s "dhclient3" -i "$interface"
  fi
}
netconfig_remove() {
  if test -x /sbin/netconfig -a -n "$interface" ; then
    /sbin/netconfig remove -s "dhclient3" -i "$interface" </dev/null
  fi
}

make_resolv_conf() {
  ###
  ### note: this function is never used
  ###       it creates /var/lib/dhcp/resolv.conf.dhclient.$interface only
  ###
  if [ x"$new_domain_name_servers" != x ]; then
    cat /dev/null > /var/lib/dhcp/resolv.conf.dhclient.$interface
    chmod 644 /var/lib/dhcp/resolv.conf.dhclient.$interface
    if [ x"$new_domain_search" != x ]; then
      echo search $new_domain_search >> /var/lib/dhcp/resolv.conf.dhclient.$interface
    elif [ x"$new_domain_name" != x ]; then
      # Note that the DHCP 'Domain Name Option' is really just a domain
      # name, and that this practice of using the domain name option as
      # a search path is both nonstandard and deprecated.
      echo search $new_domain_name >> /var/lib/dhcp/resolv.conf.dhclient.$interface
    fi
    for nameserver in $new_domain_name_servers; do
      echo nameserver $nameserver >> /var/lib/dhcp/resolv.conf.dhclient.$interface
    done
  fi
}
restore_resolv_conf()
{
  rm -f /var/lib/dhcp/resolv.conf.dhclient.$interface
}

# Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
exit_with_hooks() {
  exit_status=$1
  if [ -f /etc/dhclient-exit-hooks ]; then
    . /etc/dhclient-exit-hooks
  fi
# probably should do something with exit status of the local script
  exit $exit_status
}

# Invoke the local dhcp client enter hooks, if they exist.
if [ -f /etc/dhclient-enter-hooks ]; then
  exit_status=0
  . /etc/dhclient-enter-hooks
  # allow the local script to abort processing of this state
  # local script must set exit_status variable to nonzero.
  if [ $exit_status -ne 0 ]; then
    exit $exit_status
  fi
fi

release=`uname -r`
release=`expr $release : '\(.*\)\..*'`
relminor=`echo $release |sed -e 's/[0-9]*\.\([0-9][0-9]*\)\(\..*\)*$/\1/'`
relmajor=`echo $release |sed -e 's/\([0-9][0-9]*\)\..*$/\1/'`

if [ x$new_broadcast_address != x ]; then
  new_broadcast_arg="broadcast $new_broadcast_address"
fi
if [ x$old_broadcast_address != x ]; then
  old_broadcast_arg="broadcast $old_broadcast_address"
fi
if [ x$new_subnet_mask != x ]; then
  new_subnet_arg="netmask $new_subnet_mask"
fi
if [ x$old_subnet_mask != x ]; then
  old_subnet_arg="netmask $old_subnet_mask"
fi
if [ x$alias_subnet_mask != x ]; then
  alias_subnet_arg="netmask $alias_subnet_mask"
fi
if [ $(($new_interface_mtu)) -lt 576 ] ; then
  # 68 is the minimal legal value, but 576 the real life minimum
  unset new_interface_mtu
fi

if [ x$reason = xMEDIUM ]; then
  # Linux doesn't do mediums (ok, ok, media).
  exit_with_hooks 0
fi

if [ x$reason = xPREINIT ]; then
  if [ x$alias_ip_address != x ]; then
    # Bring down alias interface. Its routes will disappear too.
    ifconfig $interface:0- inet 0
  fi
  if [ $relmajor -lt 2 ] || ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] )
   then
    ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 \
                        broadcast 255.255.255.255 up
    # Add route to make broadcast work. Do not omit netmask.
    route add default dev $interface netmask 0.0.0.0
  else
    ifconfig $interface 0 up
  fi

  # We need to give the kernel some time to get the interface up.
  sleep 1

  exit_with_hooks 0
fi

if [ x$reason = xARPCHECK ] || [ x$reason = xARPSEND ]; then
  exit_with_hooks 0
fi
  
if [ x$reason = xBOUND ] || [ x$reason = xRENEW ] || \
   [ x$reason = xREBIND ] || [ x$reason = xREBOOT ]; then
  if [ x$old_ip_address != x ] && [ x$alias_ip_address != x ] && \
     [ x$alias_ip_address != x$old_ip_address ]; then
    # Possible new alias. Remove old alias.
    ifconfig $interface:0- inet 0
  fi
  if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]; then
    # IP address changed. Bringing down the interface will delete all routes,
    # and clear the ARP cache.
    ifconfig $interface inet 0

  fi
  if [ x$old_ip_address = x ] || [ x$old_ip_address != x$new_ip_address ] || \
     [ x$reason = xBOUND ] || [ x$reason = xREBOOT ]; then

    ifconfig $interface inet $new_ip_address $new_subnet_arg \
                             $new_broadcast_arg \
                             ${new_interface_mtu:+mtu "$new_interface_mtu"}
    # Add a network route to the computed network address.
    if [ $relmajor -lt 2 ] || \
       ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ); then
      route add -net $new_network_number $new_subnet_arg dev $interface
    fi

    if test -f /etc/sysconfig/network/ifcfg-${interface} -o \
            -f /dev/.sysconfig/network/if-${interface} ;
    then
      if grep -qs '^primary=yes' /dev/.sysconfig/network/if-${interface} 2>/dev/null ;
      then
        eval `grep --no-filename '^[[:space:]]*DHCLIENT_SET_DEFAULT_ROUTE=' \
              /etc/sysconfig/network/dhcp \
              /etc/sysconfig/network/ifcfg-${interface} 2>/dev/null`
      else
        eval `grep --no-filename '^[[:space:]]*DHCLIENT_SET_DEFAULT_ROUTE=' \
              /etc/sysconfig/network/ifcfg-${interface} 2>/dev/null`
      fi
    else
      eval `grep --no-filename '^[[:space:]]*DHCLIENT_SET_DEFAULT_ROUTE=' \
            /etc/sysconfig/network/dhcp 2>/dev/null`
    fi
    if [ "$DHCLIENT_SET_DEFAULT_ROUTE" = yes ] ; then
      for router in $new_routers; do
        err=`route add default gw $router  2>&1` && break || \
        case "$err" in
          SIOCADDRT:*)
            # The gateway seems to be not reachable via local network
            # route (implicitely created by ifconfig based on the IP
            # an netmask provided by dhcp).
            # Check this, set an explicit host route to the gateway
            # over the current interface and try again (bnc#266215).
            matches=$(test -x /sbin/ip && \
                  /sbin/ip -f inet -o route list match $router | \
                  grep -v ^default | grep -c -v "^$" 2>/dev/null)
            if test -n "$matches" -a $(($matches)) -eq 0 ; then
              route add -host $router dev $interface && \
              route add default gw $router && break
            fi
          ;;
        esac
      done
    fi
  fi

  if [ x$new_ip_address != x$alias_ip_address ] && [ x$alias_ip_address != x ];
   then
    ifconfig $interface:0- inet 0
    ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
    route add -host $alias_ip_address $interface:0
  fi

  if use_netconfig ; then
    netconfig_modify
  else
    make_resolv_conf
  fi

  if test -f /etc/sysconfig/network/ifcfg-${interface} -o \
          -f /dev/.sysconfig/network/if-${interface} ;
  then
    if grep -qs '^primary=yes' /dev/.sysconfig/network/if-${interface} 2>/dev/null ;
    then
      eval `grep --no-filename "^[[:space:]]*DHCLIENT_SET_HOSTNAME=" \
          /etc/sysconfig/network/dhcp \
          /etc/sysconfig/network/ifcfg-${interface} 2>/dev/null`
    else
      eval `grep --no-filename "^[[:space:]]*DHCLIENT_SET_HOSTNAME=" \
          /etc/sysconfig/network/ifcfg-${interface} 2>/dev/null`
    fi
  else
    eval `grep --no-filename "^[[:space:]]*DHCLIENT_SET_HOSTNAME=" \
        /etc/sysconfig/network/dhcp 2>/dev/null`
  fi
  if [ "$DHCLIENT_SET_HOSTNAME" = yes ] ; then
    rx_host='^[[:alnum:]][[:alnum:]_-]{0,62}$'

    new_host_name="${new_host_name%%.*}"
    [[ ${new_host_name} =~ ${rx_host} ]] || unset new_host_name

    current_hostname=`hostname`
    current_hostname="${current_hostname%%.*}"
    [[ ${current_hostname} =~ ${rx_host} ]] || unset current_hostname

    if [ "x${current_hostname}" = "x" ] || \
       [ "x${current_hostname}" = "xlocalhost" ] || \
       [ "x${current_hostname}" != "x${new_host_name}" ]; then

      if [ "x${new_host_name}" != "x" ]; then
        hostname "${new_host_name}"
      else
        if [ -x /usr/bin/host ] ; then
          if out=`host -W 2 "$new_ip_address" 2>/dev/null` ; then
            _hostname="`echo "$out" | sed 's:^.* ::; s:\..*::; s:.*[)]::'`"
            [[ ${_hostname} =~ ${rx_host} ]] || unset _hostname
            if [ "x${_hostname}" != "x" -a \
                 "x${_hostname}" != "x${current_hostname}" ]; then
              hostname "${_hostname}"
            fi
          fi
        fi
      fi

    fi
  fi

  exit_with_hooks 0
fi

if [ x$reason = xEXPIRE ] || [ x$reason = xFAIL ] || [ x$reason = xRELEASE ] \
   || [ x$reason = xSTOP ]; then
  if [ x$alias_ip_address != x ]; then
    # Turn off alias interface.
    ifconfig $interface:0- inet 0
  fi
  if [ x$old_ip_address != x ]; then
    # Shut down interface, which will delete routes and clear arp cache.
    ifconfig $interface inet 0
  fi
  if [ x$alias_ip_address != x ]; then
    ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
    route add -host $alias_ip_address $interface:0
  fi
  exit_with_hooks 0
fi

if [ x$reason = xTIMEOUT ]; then
  if [ x$alias_ip_address != x ]; then
    ifconfig $interface:0- inet 0
  fi
  ifconfig $interface inet $new_ip_address $new_subnet_arg \
                           $new_broadcast_arg
  set $new_routers
  ############## what is -w in ping?
  if ping -q -c 1 $1; then
    if [ x$new_ip_address != x$alias_ip_address ] && \
       [ x$alias_ip_address != x ]; then
      ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
      route add -host $alias_ip_address dev $interface:0
    fi
    if [ $relmajor -lt 2 ] || \
       ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ); then
      route add -net $new_network_number
    fi
    for router in $new_routers; do
      route add default gw $router
    done
    if use_netconfig ; then
      netconfig_modify
    else
      make_resolv_conf
    fi
    exit_with_hooks 0
  fi
  ifconfig $interface inet 0
  exit_with_hooks 1
fi

if use_netconfig ; then
  netconfig_remove
else
  restore_resolv_conf
fi

exit_with_hooks 0
