[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to modify dhclient-script to reload NAT rules?



* Marco Radzinschi <marco@radzinschi.com> [03/03/02 20:48]:
> 	I noticed dhclient-script on OpenBSD 3.0 doesn't have support for
> an exit-hooks script.  Could someone tell me how to modify it to reload
> the NAT rules when it gets a new IP address?
> 
> I am thinking I could modify every exit line to reload NAT, then exit, but
> this may be overdoing it, and I do not want to screw up dhclient-script.
> 
> Please CC me as I am not on the list.

Attached is my patch to dhclient-script that adds the HOOKS
functionality described in the manpage. I've submitted the patch via
gnats, but it hasn't been integrated yet.

-- 
Wes Griffin                                                     NAI Labs
wgriffin at tislabs.com                                     443.259.2388
--- dhclient-script.orig	Wed Oct 18 17:42:56 2000
+++ dhclient-script	Tue Dec 11 10:02:31 2001
@@ -3,6 +3,45 @@
 # $OpenBSD: dhclient-script,v 1.11 2000/10/18 23:42:56 todd Exp $
 #
 
+make_resolv_conf() {
+  if [ "$new_domain_name" != "" ]; then
+    if [ -n "$new_domain_name_servers" ]; then
+      echo "search $new_domain_name" >/etc/resolv.conf.std
+      for nameserver in $new_domain_name_servers; do
+        echo "nameserver $nameserver" >>/etc/resolv.conf.std
+      done
+      if [ -f /etc/resolv.conf.tail ]; then
+        cat /etc/resolv.conf.tail >>/etc/resolv.conf.std
+      fi
+      if [ -f /etc/resolv.conf ]; then
+        rm -f /etc/resolv.conf
+      fi
+      mv /etc/resolv.conf.std /etc/resolv.conf
+    fi
+  fi
+}
+
+# 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
+
 if [ "x$new_network_number" != "x" ]; then
    echo "New Network Number: $new_network_number"
 fi
@@ -15,7 +54,7 @@
   ifconfig $interface $medium
   ifconfig $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
   sleep 1
-  exit 0
+  exit_with_hooks 0
 fi
 
 if [ "x$reason" = "xPREINIT" ]; then
@@ -25,11 +64,11 @@
   fi
   ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 \
 		broadcast 255.255.255.255 up
-  exit 0
+  exit_with_hooks 0
 fi
 
 if [ "x$reason" = "xARPCHECK" ] || [ "x$reason" = "xARPSEND" ]; then
-  exit 0;
+  exit_with_hooks 0;
 fi
   
 if [ "x$reason" = "xBOUND" ] || [ "x$reason" = "xRENEW" ] || \
@@ -76,20 +115,8 @@
     ifconfig $interface inet alias $alias_ip_address netmask $alias_subnet_mask
     route add $alias_ip_address 127.0.0.1
   fi
-  if [ "x$new_domain_name" != "x" ];
-   then
-    if [ -n "$new_domain_name_servers" ];
-     then
-      echo "search $new_domain_name" >/etc/resolv.conf
-      for nameserver in $new_domain_name_servers; do
-        echo nameserver $nameserver >>/etc/resolv.conf
-      done
-      if [ -f /etc/resolv.conf.tail ]; then
-	cat /etc/resolv.conf.tail >>/etc/resolv.conf
-      fi
-      exit 0
-    fi
-  fi
+  make_resolv_conf
+  exit_with_hooks 0
 fi
 
 if [ "x$reason" = "xEXPIRE" ] || [ "x$reason" = "xFAIL" ]; then
@@ -117,7 +144,7 @@
     ifconfig $interface inet alias $alias_ip_address netmask $alias_subnet_mask
     route add $alias_ip_address 127.0.0.1
   fi
-  exit 0
+  exit_with_hooks 0
 fi
 
 if [ "x$reason" = "xTIMEOUT" ]; then
@@ -147,22 +174,8 @@
 	  shift; shift
         done
       fi
-      if [ "$new_domain_name" != "" ]; then
-        if [ -n "$new_domain_name_servers" ]; then
-          echo "search $new_domain_name" >/etc/resolv.conf.std
-          for nameserver in $new_domain_name_servers; do
-  	    echo "nameserver $nameserver" >>/etc/resolv.conf.std
-          done
-          if [ -f /etc/resolv.conf.tail ]; then
-	    cat /etc/resolv.conf.tail >>/etc/resolv.conf.std
-      	  fi
-          if [ -f /etc/resolv.conf ]; then
-	    rm -f /etc/resolv.conf
-          fi
-          mv /etc/resolv.conf.std /etc/resolv.conf
-          exit 0
-        fi
-      fi
+      make_resolv_conf
+      exit_with_hooks 0
     fi
   fi
   ifconfig $interface inet -alias $new_ip_address $medium
@@ -178,7 +191,7 @@
   fi
   arp -n -a | sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' \
 							|sh >/dev/null 2>&1
-  exit 1
+  exit_with_hooks 1
 fi
 
-exit 0
+exit_with_hooks 0