#!/usr/local/bin/bash # restore original resolv.conf # update DNS # rewrite ipsec config and resart ipsec # public at wernig.net abort() { echo $1 exit 0 } . /etc/rc.conf # This script is called everytime dhclient has something to do. # We do not want do rewrite the system configuration every time, # e.g. when we do not yet have an IP address. IP=`ifconfig ep0 | grep "inet\ " | awk '{print $2}'` # logger -t dhclient-script -p notice "IP is: $IP" if [ $IP = "0.0.0.0" ]; then # Interface not yet configured. Just exit exit 0 fi if [ $RESOLVE_LOCALLY = 'yes' -a -f /etc/resolv.conf.local ]; then # obviously, RESOLVE_LOCALLY = 'yes' must be set in rc.conf # and a suitable resolv.conf must be prepared at /etc/resolv.conf.local. # The variable $reason is set by dhclient (see dhclient-script(8)). if ! diff /etc/resolv.conf /etc/resolv.conf.local >/dev/null 2>&1; then cp /etc/resolv.conf /etc/resolv.conf.dhcp cp /etc/resolv.conf.local /etc/resolv.conf logger -t dhclient-script -p notice "$reason: Switched name resolver to use local DNS" fi fi # Update DNS entry # This works because I also have control of the DNS and have configured # it to accept updates for the zone which have been signed by the key # that this script uses. # Restart firewall # Rewrite IPSec config and restart IPsec # Continue only if IP has changed if [ -f /tmp/ip.dhcp.old ]; then OLDIP=`cat /tmp/ip.dhcp.old` if [ $IP = $OLDIP ]; then # logger -t dhclient-script -p notice "IP Address hasn't changed. Exiting." exit 0 fi fi echo "$IP" > /tmp/ip.dhcp.old # restart firewall # logger -t dhclient-script -p notice "Restarting firewall router" /etc/rc.d/ipfw restart # update DNS HOSTNAME="my.host.name" FILE="/etc/namedb/tsig:my-key-auth." # the actual file name is Kmy-key-auth.+157+00000.key # it was created by dnskeygen # This version of nsupdate adds the leading K and the rest automatically, # others don't and take a different syntax echo "`date`: Updating DNS with $HOSTNAME = $IP" cat < /etc/ipsec/ipsec.conf.tmp if [ $? -eq 0 ]; then mv /etc/ipsec/ipsec.conf.tmp /etc/ipsec/ipsec.conf fi /etc/rc.d/ipsec restart fi logger -t dhclient-script -p notice "IP Address: $IP; DNS update $SUC, firewall and ipsec restarted. Reason: $reason" exit $RES