#!/bin/sh
#
# This file is sourced using (.) by /sbin/dhclient-script after the eth0 
# interface is brought up.
#
# The code here creates a file named /var/named/forwarders.conf containing
# the nameservers listed in the DHCP response.
# This allows the local named to properly respond to queries for both local
# and remote zones.

# The environment is inherited from the context in the dhclient-script at the
# time when this file is sourced.

# The file operations here require the dhcpnamedforward SELinux module to
# succeed.  If the file write fails, check that the policy is loaded.
# 

FORWARD_CONF=${FORWARD_CONF:="/var/named/forwarders.conf"}

if [ -n "$new_domain_name_servers" ]
then
  # remove the localhost reference if it's provided
  NAME_SERVER_LIST=`echo ${new_domain_name_servers} | sed -e 's/127.0.0.1 *//g'`
  logmessage "NAME_SERVER_LIST ${NAME_SERVER_LIST}"
  FORWARDERS=""
  for i in $NAME_SERVER_LIST; do
    FORWARDERS="${FORWARDERS} $i ; "
  done
  FORWARDERS="${FORWARDERS} 8.8.8.8 ; 8.8.4.4 ;"
  logmessage "set forwarders: ${FORWARDERS}"
  cat > ${FORWARD_CONF} <<EOF
// created by /etc/dhcp/dhclient-up-hooks
// set named forwarders from the DHCP supplied name server list
forwarders { ${FORWARDERS} } ;
EOF

  # reload the named configuration if needed
  if service named status 2>&1 >/dev/null
  then
    service named reload
  fi
else
  logmessage "no new name servers provided by DHCP"
fi
