home *** CD-ROM | disk | FTP | other *** search
/ tusportal.tus.k12.pa.us / tusportal.tus.k12.pa.us.tar / tusportal.tus.k12.pa.us / Wyse / latest-image.raw / 0.img / sbin / dhclient-script < prev    next >
Text File  |  2010-05-12  |  15KB  |  441 lines

  1. #!/bin/bash
  2. # dhclient-script for Linux. Dan Halbert, March, 1997.
  3. # Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
  4. # No guarantees about this. I'm a novice at the details of Linux
  5. # networking.
  6.  
  7. # Notes:
  8.  
  9. # 0. This script is based on the netbsd script supplied with dhcp-970306.
  10.  
  11. # 1. ifconfig down apparently deletes all relevant routes and flushes
  12. # the arp cache, so this doesn't need to be done explicitly.
  13.  
  14. # 2. The alias address handling here has not been tested AT ALL.
  15. # I'm just going by the doc of modern Linux ip aliasing, which uses
  16. # notations like eth0:0, eth0:1, for each alias.
  17.  
  18. # 3. I have to calculate the network address, and calculate the broadcast
  19. # address if it is not supplied. This might be much more easily done
  20. # by the dhclient C code, and passed on.
  21.  
  22. # 4. TIMEOUT not tested. ping has a flag I don't know, and I'm suspicious
  23. # of the $1 in its args.
  24.  
  25.  
  26. # Debugging:
  27. #
  28. # logs entire run of dhclient-script to /var/log/dhclient-script, 
  29. # if DHCLIENT_DEBUG is set in sysconfig/network/dhcp
  30. #
  31. SYSCONFIG_CFG_DIR="/etc/sysconfig/network"
  32. . "$SYSCONFIG_CFG_DIR/scripts/functions"
  33. SYSCONFIG_RUN_DIR=${SYSCONFIG_RUN_DIR:-/dev/.sysconfig/network}
  34. eval `grep "^DHCLIENT_DEBUG=" "$SYSCONFIG_CFG_DIR/dhcp" 2>/dev/null`
  35. if [ "$DHCLIENT_DEBUG" = yes ]; then
  36.   set -a # allexport
  37.   (
  38.     echo '****************' 
  39.     echo "$0 $*"
  40.     date
  41.     echo '----------------'
  42.     env
  43.     echo '----------------'
  44.   ) >> /var/log/dhclient-script.log
  45.   exec 2>> /var/log/dhclient-script.log
  46.   set +a
  47.   set -x
  48. fi
  49.  
  50. is_ifup_controlled()
  51. {
  52.   test -f "$SYSCONFIG_RUN_DIR/if-${interface}"
  53. }
  54. # netconfig makes all "additional" modifications as DNS or NTP
  55. use_netconfig() {
  56.   test -x /sbin/netconfig
  57. }
  58. netconfig_modify() {
  59.   if test -x /sbin/netconfig -a -n "$interface" ; then
  60.     {
  61.       echo "INTERFACE='$interface'"
  62.       for v in ${!new_*}; do
  63.         case $v in
  64.           (new_ip_address)            k='IPADDR'             ;;
  65.           (new_subnet_mask)           k='NETMASK'            ;;
  66.           (new_network_number)        k='NETWORK'            ;;
  67.           (new_broadcast_address)     k='BROADCAST'          ;;
  68.           (new_interface_mtu)         k='MTU'                ;;
  69.         # (new_static_routes)         k='ROUTES'             ;;
  70.           (new_routers)               k='GATEWAYS'           ;;
  71.           (new_host_name)             k='HOSTNAME'           ;;
  72.           (new_domain_search)         k='DNSSEARCH'          ;;
  73.           (new_domain_name)           k='DNSDOMAIN'          ;;
  74.           (new_domain_name_servers)   k='DNSSERVERS'         ;;
  75.           (new_ntp_servers)           k='NTPSERVERS'         ;;
  76.           (new_nis_domain)            k='NISDOMAIN'          ;;
  77.           (new_nis_servers)           k='NISSERVERS'         ;;
  78.           (new_root_path)             k='ROOTPATH'           ;;
  79.           (new_dhcp_server_identifier)k='DHCPSID'            ;;
  80.           (new_lpr_servers)           k='LPRSERVER'          ;;
  81.           (new_log_servers)           k='LOGSERVER'          ;;
  82.           (new_netbios_dd_server)     k='NETBIOSDDSERVER'    ;;
  83.           (new_netbios_name_servers)  k='NETBIOSNAMESERVER'  ;;
  84.           (new_netbios_node_type)     k='NETBIOSNODETYPE'    ;;
  85.           (new_netbios_scope)         k='NETBIOSSCOPE'       ;;
  86.           (*)                         k="dhclient_${v#new_}" ;;
  87.         esac
  88.         [ "k${k}" != k ] && echo "${k}='${!v}'"
  89.       done
  90.     } | /sbin/netconfig modify -s "dhclient3" -i "$interface"
  91.   fi
  92. }
  93. netconfig_remove() {
  94.   if test -x /sbin/netconfig -a -n "$interface" ; then
  95.     /sbin/netconfig remove -s "dhclient3" -i "$interface" </dev/null
  96.   fi
  97. }
  98. set_default_gw_route()
  99. {
  100.   if is_ifup_controlled ; then
  101.     if grep -qs '^primary=yes' "$SYSCONFIG_RUN_DIR/if-${interface}" 2>/dev/null ;
  102.     then
  103.       eval `grep --no-filename '^[[:space:]]*DHCLIENT_SET_DEFAULT_ROUTE=' \
  104.             "$SYSCONFIG_CFG_DIR/dhcp"
  105.             "$SYSCONFIG_CFG_DIR/ifcfg-${interface}" 2>/dev/null`
  106.     else
  107.       eval `grep --no-filename '^[[:space:]]*DHCLIENT_SET_DEFAULT_ROUTE=' \
  108.             "$SYSCONFIG_CFG_DIR/ifcfg-${interface}" 2>/dev/null`
  109.     fi
  110.   else
  111.     eval `grep --no-filename '^[[:space:]]*DHCLIENT_SET_DEFAULT_ROUTE=' \
  112.           "$SYSCONFIG_CFG_DIR/dhcp" 2>/dev/null`
  113.   fi
  114.   if [ "$DHCLIENT_SET_DEFAULT_ROUTE" = yes ] ; then
  115.     for router in $new_routers; do
  116.       err=`route add default gw $router $metric_arg dev $interface  2>&1` && break || \
  117.       case "$err" in
  118.         SIOCADDRT:*)
  119.           # The gateway seems to be not reachable via local network
  120.           # route (implicitely created by ifconfig based on the IP
  121.           # an netmask provided by dhcp).
  122.           # Check this, set an explicit host route to the gateway
  123.           # over the current interface and try again (bnc#266215).
  124.           matches=$(test -x /sbin/ip && \
  125.                 /sbin/ip -f inet -o route list match $router | \
  126.                 grep -v ^default | grep -c -v "^$" 2>/dev/null)
  127.           if test -n "$matches" -a $(($matches)) -eq 0 ; then
  128.             route add -host $router dev $interface && \
  129.             route add default gw $router $metric_arg dev $interface && break
  130.           fi
  131.         ;;
  132.       esac
  133.     done
  134.   fi
  135. }
  136. set_hostname()
  137. {
  138.   if is_ifup_controlled ; then
  139.     if grep -qs '^primary=yes' "$SYSCONFIG_RUN_DIR/if-${interface}" 2>/dev/null ;
  140.     then
  141.       eval `grep --no-filename "^[[:space:]]*DHCLIENT_SET_HOSTNAME=" \
  142.             "$SYSCONFIG_CFG_DIR/dhcp"
  143.             "$SYSCONFIG_CFG_DIR/ifcfg-${interface}" 2>/dev/null`
  144.     else
  145.       eval `grep --no-filename "^[[:space:]]*DHCLIENT_SET_HOSTNAME=" \
  146.             "$SYSCONFIG_CFG_DIR/ifcfg-${interface}" 2>/dev/null`
  147.     fi
  148.   else
  149.     eval `grep --no-filename "^[[:space:]]*DHCLIENT_SET_HOSTNAME=" \
  150.           "$SYSCONFIG_CFG_DIR/dhcp" 2>/dev/null`
  151.   fi
  152.   if [ "$DHCLIENT_SET_HOSTNAME" = yes ] ; then
  153.     current_hostname=`hostname`
  154.     if [ "x${current_hostname%%.*}" = "x" ] || \
  155.        [ "x${current_hostname%%.*}" = "x(none)" ] || \
  156.        [ "x${current_hostname%%.*}" = "xlocalhost" ] || \
  157.        [ "x${current_hostname%%.*}" != "x${new_host_name%%.*}" ]; then
  158.  
  159.       if [ "x${new_host_name%%.*}" != "x" ]; then
  160.         hostname "${new_host_name%%.*}"
  161.       else
  162.         if [ -x /usr/bin/host ] ; then
  163.           if out=`host -W 2 "$new_ip_address" 2>/dev/null` ; then
  164.             _hostname="`echo "$out" | sed 's:^.* ::; s:\..*::'`"
  165.             if [ "x${_hostname}" != "x" -a \
  166.                  "x${_hostname}" != "x${current_hostname%%.*}" ]; then
  167.               hostname "${_hostname}"
  168.             fi
  169.           fi
  170.         fi
  171.       fi
  172.     fi
  173.   fi
  174.   if is_ifup_controlled ; then
  175.     # check regardless the DHCLIENT_SET_HOSTNAME setting
  176.     # and whether we've set it above or not, because when
  177.     # it changed, we've to handle it anyway...
  178.     local OLD_HOSTNAME=`read_cached_config_data hostname $interface`
  179.     local CUR_HOSTNAME=`hostname 2>/dev/null`
  180.     if test "x$OLD_HOSTNAME" != "x$CUR_HOSTNAME" ; then
  181.       write_cached_config_data hostname "$CUR_HOSTNAME" $interface
  182.       commit_cached_config_data                         $interface
  183.  
  184.       # reload syslog so it knows the new hostname
  185.       /etc/init.d/syslog reload
  186.     fi
  187.   fi
  188. }
  189.  
  190. resolv_conf_dhclient="/var/lib/dhcp/resolv.conf.dhclient.$interface"
  191. make_resolv_conf() {
  192.   ###
  193.   ### note: this function is never used by default
  194.   ###       and it has been modified to create the
  195.   ###         /var/lib/dhcp/resolv.conf.dhclient.$interface
  196.   ###       file instead /etc/resolv.conf.
  197.   ###
  198.   if [ x"$new_domain_name_servers" != x ]; then
  199.     cat /dev/null > "$resolv_conf_dhclient"
  200.     chmod 644 "$resolv_conf_dhclient"
  201.     if [ x"$new_domain_search" != x ]; then
  202.       echo search $new_domain_search >> "$resolv_conf_dhclient"
  203.     elif [ x"$new_domain_name" != x ]; then
  204.       # Note that the DHCP 'Domain Name Option' is really just a domain
  205.       # name, and that this practice of using the domain name option as
  206.       # a search path is both nonstandard and deprecated.
  207.       echo search $new_domain_name >> "$resolv_conf_dhclient"
  208.     fi
  209.     for nameserver in $new_domain_name_servers; do
  210.       echo nameserver $nameserver >> "$resolv_conf_dhclient"
  211.     done
  212.  
  213.     ### mv /etc/resolv.conf.dhclient /etc/resolv.conf
  214.   fi
  215. }
  216. restore_resolv_conf()
  217. {
  218.   rm -f "$resolv_conf_dhclient"
  219. }
  220.  
  221. # Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
  222. exit_with_hooks() {
  223.   exit_status=$1
  224.   if [ -f /etc/dhclient-exit-hooks ]; then
  225.     . /etc/dhclient-exit-hooks
  226.   fi
  227. # probably should do something with exit status of the local script
  228.   exit $exit_status
  229. }
  230.  
  231. # Invoke the local dhcp client enter hooks, if they exist.
  232. if [ -f /etc/dhclient-enter-hooks ]; then
  233.   exit_status=0
  234.   . /etc/dhclient-enter-hooks
  235.   # allow the local script to abort processing of this state
  236.   # local script must set exit_status variable to nonzero.
  237.   if [ $exit_status -ne 0 ]; then
  238.     exit $exit_status
  239.   fi
  240. fi
  241.  
  242. release=`uname -r`
  243. release=`expr $release : '\(.*\)\..*'`
  244. relminor=`echo $release |sed -e 's/[0-9]*\.\([0-9][0-9]*\)\(\..*\)*$/\1/'`
  245. relmajor=`echo $release |sed -e 's/\([0-9][0-9]*\)\..*$/\1/'`
  246.  
  247. if [ x$new_broadcast_address != x ]; then
  248.   new_broadcast_arg="broadcast $new_broadcast_address"
  249. fi
  250. if [ x$old_broadcast_address != x ]; then
  251.   old_broadcast_arg="broadcast $old_broadcast_address"
  252. fi
  253. if [ x$new_subnet_mask != x ]; then
  254.   new_subnet_arg="netmask $new_subnet_mask"
  255. fi
  256. if [ x$old_subnet_mask != x ]; then
  257.   old_subnet_arg="netmask $old_subnet_mask"
  258. fi
  259. if [ x$alias_subnet_mask != x ]; then
  260.   alias_subnet_arg="netmask $alias_subnet_mask"
  261. fi
  262. if [ x$new_interface_mtu != x ]; then
  263.   if [ $(( $new_interface_mtu )) -lt 576 ] ; then
  264.     # 68 is the minimal legal value, but 576 the real life minimum
  265.     unset new_interface_mtu
  266.   else
  267.     mtu_arg="mtu $new_interface_mtu"
  268.   fi
  269. fi
  270. if [ x$IF_METRIC != x ]; then
  271.   metric_arg="metric $IF_METRIC"
  272. fi
  273.  
  274. if [ x$reason = xMEDIUM ]; then
  275.   # Linux doesn't do mediums (ok, ok, media).
  276.   exit_with_hooks 0
  277. fi
  278.  
  279. if [ x$reason = xPREINIT ]; then
  280.   if [ x$alias_ip_address != x ]; then
  281.     # Bring down alias interface. Its routes will disappear too.
  282.     ifconfig $interface:0- inet 0
  283.   fi
  284.   if [ $relmajor -lt 2 ] || ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] )
  285.    then
  286.     ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 \
  287.         broadcast 255.255.255.255 up
  288.     # Add route to make broadcast work. Do not omit netmask.
  289.     route add default dev $interface netmask 0.0.0.0
  290.   else
  291.     ifconfig $interface 0 up
  292.   fi
  293.  
  294.   # We need to give the kernel some time to get the interface up.
  295.   sleep 1
  296.  
  297.   exit_with_hooks 0
  298. fi
  299.  
  300. if [ x$reason = xARPCHECK ] || [ x$reason = xARPSEND ]; then
  301.   exit_with_hooks 0
  302. fi
  303.   
  304. if [ x$reason = xBOUND ] || [ x$reason = xRENEW ] || \
  305.    [ x$reason = xREBIND ] || [ x$reason = xREBOOT ]; then
  306.     
  307.   if [ x$old_ip_address != x ] && [ x$alias_ip_address != x ] && \
  308.         [ x$alias_ip_address != x$old_ip_address ]; then
  309.     # Possible new alias. Remove old alias.
  310.     ifconfig $interface:0- inet 0
  311.   fi
  312.   if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]; then
  313.     # IP address changed. Bringing down the interface will delete all routes,
  314.     # and clear the ARP cache.
  315.     ifconfig $interface inet 0
  316.  
  317.     if is_ifup_controlled ; then
  318.       write_cached_config_data  dhcp4_state "new"      $interface
  319.       commit_cached_config_data                        $interface
  320.     fi
  321.   elif is_ifup_controlled ; then
  322.     write_cached_config_data  dhcp4_state "up"       $interface
  323.     commit_cached_config_data                        $interface
  324.   fi
  325.  
  326.   if [ x$old_ip_address = x ] || [ x$old_ip_address != x$new_ip_address ] || \
  327.      [ x$reason = xBOUND ] || [ x$reason = xREBOOT ]; then
  328.  
  329.     ifconfig $interface inet $new_ip_address $new_subnet_arg \
  330.                         $new_broadcast_arg $mtu_arg
  331.     # Add a network route to the computed network address.
  332.     if [ $relmajor -lt 2 ] || \
  333.         ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ); then
  334.       route add -net $new_network_number $new_subnet_arg dev $interface
  335.     fi
  336.  
  337.     set_default_gw_route
  338.   fi
  339.  
  340.   if [ x$new_ip_address != x$alias_ip_address ] && [ x$alias_ip_address != x ];
  341.    then
  342.     ifconfig $interface:0- inet 0
  343.     ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  344.     route add -host $alias_ip_address $interface:0
  345.   fi
  346.  
  347.   if use_netconfig ; then
  348.     netconfig_modify
  349.   else
  350.     make_resolv_conf
  351.   fi
  352.  
  353.   if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]; then
  354.     if is_ifup_controlled ; then
  355.       ifdown $interface -o dhcp
  356.     fi
  357.   fi
  358.  
  359.   set_hostname
  360.   if is_ifup_controlled ; then
  361.     # execute ifservice and if-up.d scripts
  362.     ifup $interface -o dhcp
  363.  
  364.     write_cached_config_data  dhcp4_state "complete" $interface
  365.     commit_cached_config_data                        $interface
  366.   fi
  367.  
  368.   exit_with_hooks 0
  369. fi
  370.  
  371. if [ x$reason = xEXPIRE ] || [ x$reason = xFAIL ] || [ x$reason = xRELEASE ] \
  372.    || [ x$reason = xSTOP ]; then
  373.   if [ x$alias_ip_address != x ]; then
  374.     # Turn off alias interface.
  375.     ifconfig $interface:0- inet 0
  376.   fi
  377.   if [ x$old_ip_address != x ]; then
  378.     # Shut down interface, which will delete routes and clear arp cache.
  379.     ifconfig $interface inet 0
  380.   fi
  381.   if [ x$alias_ip_address != x ]; then
  382.     ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  383.     route add -host $alias_ip_address $interface:0
  384.   fi
  385.   exit_with_hooks 0
  386. fi
  387.  
  388. if [ x$reason = xTIMEOUT ]; then
  389.   if [ x$alias_ip_address != x ]; then
  390.     ifconfig $interface:0- inet 0
  391.   fi
  392.   ifconfig $interface inet $new_ip_address $new_subnet_arg \
  393.                     $new_broadcast_arg $mtu_arg
  394.   set -- $new_routers
  395.   if ping -q -c 1 $1; then
  396.     if [ x$new_ip_address != x$alias_ip_address ] && \
  397.             [ x$alias_ip_address != x ]; then
  398.       ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  399.       route add -host $alias_ip_address dev $interface:0
  400.     fi
  401.     if [ $relmajor -lt 2 ] || \
  402.         ( [ $relmajor -eq 2 ] && [ $relminor -eq 0 ] ); then
  403.       route add -net $new_network_number
  404.     fi
  405.  
  406.     set_default_gw_route
  407.  
  408.     if use_netconfig ; then
  409.       netconfig_modify
  410.     else
  411.       make_resolv_conf
  412.     fi
  413.  
  414.     set_hostname
  415.     if is_ifup_controlled ; then
  416.       # execute ifservice and if-up.d scripts
  417.       ifup $interface -o dhcp
  418.  
  419.       write_cached_config_data  dhcp4_state "complete" $interface
  420.       commit_cached_config_data                        $interface
  421.     fi
  422.  
  423.     exit_with_hooks 0
  424.   fi
  425.   ifconfig $interface inet 0
  426.   exit_with_hooks 1
  427. fi
  428.  
  429. if is_ifup_controlled ; then
  430.   write_cached_config_data  dhcp4_state "down"     $interface
  431.   commit_cached_config_data                        $interface
  432. fi
  433.  
  434. if use_netconfig ; then
  435.   netconfig_remove
  436. else
  437.   restore_resolv_conf
  438. fi
  439.  
  440. exit_with_hooks 0
  441.