home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Linux / Ubuntu_64-bit / ubuntu-11.04-desktop-amd64.iso / casper / filesystem.squashfs / sbin / dhclient-script < prev    next >
Text File  |  2011-04-19  |  9KB  |  275 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 [ "$new_domain_name" -o "$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.     fi
  37.  
  38.     if [ "$new_domain_name" -o "$new_domain_name_servers" ]; then
  39.         local new_resolv_conf=/etc/resolv.conf.dhclient-new
  40.         rm -f $new_resolv_conf
  41.         if [ "$new_domain_name" ]; then
  42.             echo domain ${new_domain_name%% *} >>$new_resolv_conf
  43.         fi
  44.     if [ "$new_domain_search" ]; then
  45.         #new_domain_search="${new_domain_search% }"
  46.         if [ "$new_domain_name" ]; then
  47.         domain_in_search_list=""
  48.         for domain in "$new_domain_search"; do
  49.             if [ "$domain" = "$new_domain_name" ]; then
  50.                 domain_in_search="Yes"
  51.             fi
  52.         done
  53.         if [ ! "$domain_in_search" ]; then
  54.                 new_domain_search="$new_domain_name $new_domain_search"
  55.         fi
  56.         fi
  57.         echo "search ${new_domain_search}" >> $new_resolv_conf
  58.     elif [ "$new_domain_name" ]; then
  59.         echo "search ${new_domain_name}" >> $new_resolv_conf
  60.     fi
  61.         if [ "$new_domain_name_servers" ]; then
  62.                    for nameserver in $new_domain_name_servers; do
  63.                        echo nameserver $nameserver >>$new_resolv_conf
  64.             done
  65.         else # keep 'old' nameservers
  66.             sed -n /^\w*[Nn][Aa][Mm][Ee][Ss][Ee][Rr][Vv][Ee][Rr]/p /etc/resolv.conf >>$new_resolv_conf
  67.         fi
  68.         chown --reference=/etc/resolv.conf $new_resolv_conf
  69.         chmod --reference=/etc/resolv.conf $new_resolv_conf
  70.         mv -f $new_resolv_conf /etc/resolv.conf
  71.     fi
  72. }
  73.  
  74. run_hook() {
  75.     local script="$1"
  76.     local exit_status
  77.     shift    # discard the first argument, then the rest are the script's
  78.  
  79.     if [ -f $script ]; then
  80.         . $script "$@"
  81.     fi
  82.  
  83.  
  84.     if [ -n "$exit_status" ] && [ "$exit_status" -ne 0 ]; then
  85.         logger -p daemon.err "$script returned non-zero exit status $exit_status"
  86.         save_exit_status=$exit_status
  87.     fi
  88.  
  89.     return $exit_status
  90. }
  91.  
  92. run_hookdir() {
  93.     local dir="$1"
  94.     local exit_status
  95.     shift    # See run_hook
  96.  
  97.     if [ -d "$dir" ]; then
  98.         for script in $(run-parts --list $dir); do
  99.             run_hook $script "$@" || true
  100.             exit_status=$?
  101.         done
  102.     fi
  103.  
  104.     return $exit_status
  105. }
  106.  
  107. # Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
  108. exit_with_hooks() {
  109.     exit_status=$1
  110.  
  111.     # Source the documented exit-hook script, if it exists
  112.     if ! run_hook /etc/dhcp/dhclient-exit-hooks "$@"; then
  113.         exit_status=$?
  114.     fi
  115.  
  116.     # Now run scripts in the Debian-specific directory.
  117.     if ! run_hookdir /etc/dhcp/dhclient-exit-hooks.d "$@"; then
  118.         exit_status=$?
  119.     fi
  120.  
  121.     exit $exit_status
  122. }
  123.  
  124. if [ -n "$new_broadcast_address" ]; then
  125.     new_broadcast_arg="broadcast $new_broadcast_address"
  126. fi
  127. if [ -n "$old_broadcast_address" ]; then
  128.     old_broadcast_arg="broadcast $old_broadcast_address"
  129. fi
  130. if [ -n "$new_subnet_mask" ]; then
  131.     new_subnet_arg="netmask $new_subnet_mask"
  132. fi
  133. if [ -n "$old_subnet_mask" ]; then
  134.     old_subnet_arg="netmask $old_subnet_mask"
  135. fi
  136. if [ -n "$alias_subnet_mask" ]; then
  137.     alias_subnet_arg="netmask $alias_subnet_mask"
  138. fi
  139. # The 576 MTU is only used for X.25 and dialup connections
  140. # where the admin wants low latency.  Such a low MTU can cause
  141. # problems with UDP traffic, among other things.  As such,
  142. # disallow MTUs from 576 and below by default, so that broken
  143. # MTUs are ignored, but higher stuff is allowed (1492, 1500, etc).
  144. if [ -n "$new_interface_mtu" ] && [ $new_interface_mtu -gt 576 ]; then
  145.     mtu_arg="mtu $new_interface_mtu"
  146. fi
  147. if [ -n "$IF_METRIC" ]; then
  148.     metric_arg="metric $IF_METRIC"    # interfaces(5), "metric" option
  149. fi
  150.  
  151.  
  152. # The action starts here
  153.  
  154. # Invoke the local dhcp client enter hooks, if they exist.
  155. run_hook /etc/dhcp/dhclient-enter-hooks
  156. run_hookdir /etc/dhcp/dhclient-enter-hooks.d
  157.  
  158. # Execute the operation
  159. case "$reason" in
  160.     MEDIUM|ARPCHECK|ARPSEND)
  161.         # Do nothing
  162.         ;;
  163.     PREINIT)
  164.         # The DHCP client is requesting that an interface be
  165.         # configured as required in order to send packets prior to
  166.         # receiving an actual address. - dhclient-script(8)
  167.  
  168.         if [ -n "$alias_ip_address" ]; then
  169.             # Bring down alias interface. Its routes will disappear too.
  170.             ifconfig $interface:0- inet 0
  171.         fi
  172.         ifconfig $interface inet 0 up
  173.  
  174.         ;;
  175.     BOUND|RENEW|REBIND|REBOOT)
  176.  
  177.     if [ -n "$old_host_name" -a ! -s /etc/hostname ]; then
  178.             hostname "$new_host_name"
  179.     fi
  180.  
  181.         if [ -n "$old_ip_address" -a -n "$alias_ip_address" -a \
  182.              "$alias_ip_address" != "$old_ip_address" ]; then
  183.             # Possible new alias. Remove old alias.
  184.             ifconfig $interface:0- inet 0
  185.         fi
  186.  
  187.         if [ -n "$old_ip_address" -a \
  188.              "$old_ip_address" != "$new_ip_address" ]; then
  189.             # IP address changed. Bringing down the interface will delete all routes,
  190.             # and clear the ARP cache.
  191.             ifconfig $interface inet 0
  192.  
  193.         fi
  194.  
  195.         if [ -z "$old_ip_address" -o "$old_ip_address" != "$new_ip_address" -o \
  196.             "$reason" = "BOUND" -o "$reason" = "REBOOT" ]; then
  197.  
  198.             ifconfig $interface inet $new_ip_address $new_subnet_arg \
  199.                 $new_broadcast_arg $mtu_arg
  200.  
  201.         # point to point
  202.         if [ "$new_subnet_mask" == "255.255.255.255" ]; then
  203.             for router in $new_routers; do
  204.                 route add -host $router dev $interface
  205.             done
  206.         fi
  207.  
  208.             for router in $new_routers; do
  209.                 route add default dev $interface gw $router $metric_arg
  210.             done
  211.         fi
  212.  
  213.         if [ "$new_ip_address" != "$alias_ip_address" -a -n "$alias_ip_address" ];
  214.             then
  215.             ifconfig $interface:0- inet 0
  216.             ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  217.             route add -host $alias_ip_address $interface:0
  218.         fi
  219.  
  220.         make_resolv_conf
  221.  
  222.         ;;
  223.  
  224.     EXPIRE|FAIL|RELEASE|STOP)
  225.         if [ -n "$alias_ip_address" ]; then
  226.             # Turn off alias interface.
  227.             ifconfig $interface:0- inet 0
  228.         fi
  229.  
  230.         if [ -n "$old_ip_address" ]; then
  231.             # Shut down interface, which will delete routes and clear arp cache.
  232.             ifconfig $interface inet 0
  233.         fi
  234.  
  235.         if [ -n "$alias_ip_address" ]; then
  236.             ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  237.             route add -host $alias_ip_address $interface:0
  238.         fi
  239.  
  240.         ;;
  241.  
  242.     TIMEOUT)
  243.         if [ -n "$alias_ip_address" ]; then
  244.             ifconfig $interface:0- inet 0
  245.         fi
  246.  
  247.         ifconfig $interface inet $new_ip_address $new_subnet_arg \
  248.             $new_broadcast_arg $mtu_arg
  249.  
  250.         set -- $new_routers
  251.         first_router="$1"
  252.  
  253.         if [ -z "$first_router" ] || ping -q -c 1 $first_router; then
  254.             if [ "$new_ip_address" != "$alias_ip_address" -a \
  255.                 -n "$alias_ip_address" ]; then
  256.                 ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  257.                 route add -host $alias_ip_address dev $interface:0
  258.             fi
  259.         
  260.             for router in $new_routers; do
  261.                 route add default dev $interface gw $router $metric_arg
  262.             done
  263.  
  264.             make_resolv_conf
  265.         else
  266.             # Changed from 'ifconfig $interface inet 0 down' - see Debian bug #144666
  267.             ifconfig $interface inet 0
  268.             exit_with_hooks 2 "$@"
  269.         fi
  270.  
  271.         ;;
  272. esac
  273.  
  274. exit_with_hooks 0
  275.