home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / sbin / dhclient-script < prev    next >
Encoding:
Text File  |  2007-04-02  |  8.0 KB  |  267 lines

  1. #!/bin/bash
  2.  
  3. # dhclient-script for Linux. Dan Halbert, March, 1997.
  4. # Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
  5. # Modified for Debian.  Matt Zimmerman and Eloy Paris, December 2003
  6. # Modified to remove useless tests for antiquated kernel versions that
  7. # this doesn't even work with anyway, and introduces a dependency on /usr
  8. # being mounted, which causes cosmetic errors on hosts that NFS mount /usr
  9. # Andrew Pollock, February 2005
  10. # Modified to work on point-to-point links. Andrew Pollock, June 2005
  11. # Modified to support passing the parameters called with to the hooks. Andrew Pollock, November 2005
  12.  
  13. # The alias handling in here probably still sucks. -mdz
  14.  
  15. make_resolv_conf() {
  16.     if [ -n "$new_domain_name" -o -n "$new_domain_name_servers" ]; then
  17.         # Find out whether we are going to mount / rw
  18.         exec 9>&0 </etc/fstab
  19.         rootmode=rw
  20.         while read dev mnt type opts dump pass junk; do
  21.             [ "$mnt" != / ] && continue
  22.             case "$opts" in
  23.                 ro|ro,*|*,ro|*,ro,*)
  24.                    rootmode=ro
  25.                    ;;
  26.                  esac
  27.          done
  28.          exec 0>&9 9>&-
  29.  
  30.         # Wait for /etc/resolv.conf to become writable
  31.         if [ "$rootmode" = "rw" ]; then
  32.             while [ ! -w /etc ]; do
  33.                 sleep 0.1
  34.             done
  35.         fi
  36.  
  37.         local new_resolv_conf=/etc/resolv.conf.dhclient-new
  38.         rm -f $new_resolv_conf
  39.         if [ -n "$new_domain_name" ]; then
  40.             echo search $new_domain_name >>$new_resolv_conf
  41.         else # keep 'old' search/domain scope
  42.             egrep -i '^ *[:space:]*(search|domain)' /etc/resolv.conf >> $new_resolv_conf
  43.         fi
  44.         if [ -n "$new_domain_name_servers" ]; then
  45.                    for nameserver in $new_domain_name_servers; do
  46.                        echo nameserver $nameserver >>$new_resolv_conf
  47.             done
  48.         else # keep 'old' nameservers
  49.             sed -n /^\w*[Nn][Aa][Mm][Ee][Ss][Ee][Rr][Vv][Ee][Rr]/p /etc/resolv.conf >>$new_resolv_conf
  50.         fi
  51.         chown --reference=/etc/resolv.conf $new_resolv_conf
  52.         chmod --reference=/etc/resolv.conf $new_resolv_conf
  53.         mv -f $new_resolv_conf /etc/resolv.conf
  54.     fi
  55. }
  56.  
  57. run_hook() {
  58.     local script="$1"
  59.     local exit_status
  60.     shift    # discard the first argument, then the rest are the script's
  61.  
  62.     if [ -f $script ]; then
  63.         . $script "$@"
  64.     fi
  65.  
  66.  
  67.     if [ -n "$exit_status" ] && [ "$exit_status" -ne 0 ]; then
  68.         logger -p daemon.err "$script returned non-zero exit status $exit_status"
  69.         save_exit_status=$exit_status
  70.     fi
  71.  
  72.     return $exit_status
  73. }
  74.  
  75. run_hookdir() {
  76.     local dir="$1"
  77.     local exit_status
  78.     shift    # See run_hook
  79.  
  80.     if [ -d "$dir" ]; then
  81.         for script in $(run-parts --list $dir); do
  82.             run_hook $script "$@" || true
  83.             exit_status=$?
  84.         done
  85.     fi
  86.  
  87.     return $exit_status
  88. }
  89.  
  90. # Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
  91. exit_with_hooks() {
  92.     exit_status=$1
  93.  
  94.     # Source the documented exit-hook script, if it exists
  95.     if ! run_hook /etc/dhcp3/dhclient-exit-hooks "$@"; then
  96.         exit_status=$?
  97.     fi
  98.  
  99.     # Now run scripts in the Debian-specific directory.
  100.     if ! run_hookdir /etc/dhcp3/dhclient-exit-hooks.d "$@"; then
  101.         exit_status=$?
  102.     fi
  103.  
  104.     exit $exit_status
  105. }
  106.  
  107. set_hostname() {
  108.     local current_hostname=$(hostname)
  109.     if [ -z "$current_hostname" -o "$current_hostname" = "(none)" ]; then
  110.         hostname "$new_host_name"
  111.     fi
  112. }
  113.  
  114. if [ -n "$new_broadcast_address" ]; then
  115.     new_broadcast_arg="broadcast $new_broadcast_address"
  116. fi
  117. if [ -n "$old_broadcast_address" ]; then
  118.     old_broadcast_arg="broadcast $old_broadcast_address"
  119. fi
  120. if [ -n "$new_subnet_mask" ]; then
  121.     new_subnet_arg="netmask $new_subnet_mask"
  122. fi
  123. if [ -n "$old_subnet_mask" ]; then
  124.     old_subnet_arg="netmask $old_subnet_mask"
  125. fi
  126. if [ -n "$alias_subnet_mask" ]; then
  127.     alias_subnet_arg="netmask $alias_subnet_mask"
  128. fi
  129. if [ -n "$new_interface_mtu" ] && [ $new_interface_mtu -ge 575 ]; then
  130.     mtu_arg="mtu $new_interface_mtu"
  131. fi
  132. if [ -n "$IF_METRIC" ]; then
  133.     metric_arg="metric $IF_METRIC"    # interfaces(5), "metric" option
  134. fi
  135.  
  136.  
  137. # The action starts here
  138.  
  139. # Invoke the local dhcp client enter hooks, if they exist.
  140. run_hook /etc/dhcp3/dhclient-enter-hooks
  141. run_hookdir /etc/dhcp3/dhclient-enter-hooks.d
  142.  
  143. # Execute the operation
  144. case "$reason" in
  145.     MEDIUM|ARPCHECK|ARPSEND)
  146.         # Do nothing
  147.         ;;
  148.     PREINIT)
  149.         # The DHCP client is requesting that an interface be
  150.         # configured as required in order to send packets prior to
  151.         # receiving an actual address. - dhclient-script(8)
  152.  
  153.         if [ -n "$alias_ip_address" ]; then
  154.             # Bring down alias interface. Its routes will disappear too.
  155.             ifconfig $interface:0- inet 0
  156.         fi
  157.         ifconfig $interface inet 0 up
  158.  
  159.         # We need to give the kernel some time to get the interface up.
  160.         sleep 1
  161.         ;;
  162.     BOUND|RENEW|REBIND|REBOOT)
  163.  
  164.         set_hostname
  165.         
  166.         if [ -n "$old_ip_address" -a -n "$alias_ip_address" -a \
  167.              "$alias_ip_address" != "$old_ip_address" ]; then
  168.             # Possible new alias. Remove old alias.
  169.             ifconfig $interface:0- inet 0
  170.         fi
  171.  
  172.         if [ -n "$old_ip_address" -a \
  173.              "$old_ip_address" != "$new_ip_address" ]; then
  174.             # IP address changed. Bringing down the interface will delete all routes,
  175.             # and clear the ARP cache.
  176.             ifconfig $interface inet 0
  177.  
  178.         fi
  179.  
  180.         if [ -z "$old_ip_address" -o "$old_ip_address" != "$new_ip_address" -o \
  181.             "$reason" = "BOUND" -o "$reason" = "REBOOT" ]; then
  182.  
  183.             ifconfig $interface inet $new_ip_address $new_subnet_arg \
  184.                 $new_broadcast_arg $mtu_arg
  185.  
  186.         # point to point
  187.         if [ "$new_subnet_mask" == "255.255.255.255" ]; then
  188.             for router in $new_routers; do
  189.                 route add -host $router dev $interface
  190.             done
  191.         fi
  192.  
  193.             for router in $new_routers; do
  194.                 route add default dev $interface gw $router $metric_arg
  195.             done
  196.         fi
  197.  
  198.         if [ "$new_ip_address" != "$alias_ip_address" -a -n "$alias_ip_address" ];
  199.             then
  200.             ifconfig $interface:0- inet 0
  201.             ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  202.             route add -host $alias_ip_address $interface:0
  203.         fi
  204.  
  205.         make_resolv_conf
  206.  
  207.         ;;
  208.  
  209.     EXPIRE|FAIL|RELEASE|STOP)
  210.         if [ -n "$alias_ip_address" ]; then
  211.             # Turn off alias interface.
  212.             ifconfig $interface:0- inet 0
  213.         fi
  214.  
  215.         if [ -n "$old_ip_address" ]; then
  216.             # Shut down interface, which will delete routes and clear arp cache.
  217.             ifconfig $interface inet 0
  218.         fi
  219.  
  220.         if [ -n "$alias_ip_address" ]; then
  221.             ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  222.             route add -host $alias_ip_address $interface:0
  223.         fi
  224.  
  225.         ;;
  226.  
  227.     TIMEOUT)
  228.         if [ -n "$alias_ip_address" ]; then
  229.             ifconfig $interface:0- inet 0
  230.         fi
  231.  
  232.         ifconfig $interface inet $new_ip_address $new_subnet_arg \
  233.             $new_broadcast_arg $mtu_arg
  234.  
  235.         set -- $new_routers
  236.         first_router="$1"
  237.  
  238.         if ping -q -c 1 $first_router; then
  239.             if [ "$new_ip_address" != "$alias_ip_address" -a \
  240.                 -n "$alias_ip_address" ]; then
  241.                 ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  242.                 route add -host $alias_ip_address dev $interface:0
  243.             fi
  244.         
  245.         # point to point
  246.         if [ "$new_subnet_mask" == "255.255.255.255" ]; then
  247.             for router in $new_routers; do
  248.                 route add -host $router dev $interface
  249.             done
  250.         fi
  251.  
  252.             for router in $new_routers; do
  253.                 route add default dev $interface gw $router $metric_arg
  254.             done
  255.  
  256.             make_resolv_conf
  257.         else
  258.             # Changed from 'ifconfig $interface inet 0 down' - see Debian bug #144666
  259.             ifconfig $interface inet 0
  260.             exit_with_hooks 2 "$@"
  261.         fi
  262.  
  263.         ;;
  264. esac
  265.  
  266. exit_with_hooks 0
  267.