3 #############################################################################
5 # Copyright (c) 1999, MindStep Corporation
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
11 # 1. Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
17 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #############################################################################
32 # This script was written by Patrick Bihan-Faou, patrick@mindstep.com,
33 # Please contact us for bug reports, etc.
35 #############################################################################
36 # $MindStep_Id: dhclient-script.sh,v 1.8 1999/12/07 22:11:08 patrick Exp $
37 # $MindStep_Tag: CONTRIB_19991207 $
38 # $FreeBSD: src/sbin/dhclient/dhclient-script.sh,v 1.2.2.1 2002/04/11 10:21:20 murray Exp $
39 #############################################################################
42 #############################################################################
43 # hook functions prototypes
45 # The "pre_state_XXX_hook" functions are called before the main
46 # work is done for the state XXX
48 # The "post_state_XXX_hook" functions are called after the main
49 # work is done for the state XXX
51 # These functions are meant to be overridden by the user's
52 # dhclient-enter-hooks file
53 #############################################################################
55 pre_state_MEDIUM_hook () { }
56 pre_state_PREINIT_hook () { }
57 pre_state_ARPCHECK_hook () { }
58 pre_state_ARPSEND_hook () { }
59 pre_state_RENEW_hook () { }
60 pre_state_REBIND_hook () { }
61 pre_state_BOUND_hook () { }
62 pre_state_REBOOT_hook () { }
63 pre_state_EXPIRE_hook () { }
64 pre_state_FAIL_hook () { }
65 pre_state_TIMEOUT_hook () { }
66 post_state_MEDIUM_hook () { }
67 post_state_PREINIT_hook () { }
68 post_state_ARPCHECK_hook () { }
69 post_state_ARPSEND_hook () { }
70 post_state_RENEW_hook () { }
71 post_state_REBIND_hook () { }
72 post_state_BOUND_hook () { }
73 post_state_REBOOT_hook () { }
74 post_state_EXPIRE_hook () { }
75 post_state_FAIL_hook () { }
76 post_state_TIMEOUT_hook () { }
78 #############################################################################
81 # This function is called to update the information related to the
82 # DNS configuration (the resolver part)
83 #############################################################################
86 if [ "x$new_domain_name" != x ] && [ "x$new_domain_name_servers" != x ]; then
87 echo search $new_domain_name >/etc/resolv.conf
88 for nameserver in $new_domain_name_servers; do
89 echo nameserver $nameserver >>/etc/resolv.conf
94 # Must be used on exit. Invokes the local dhcp client exit hooks, if any.
97 if [ -x /etc/dhclient-exit-hooks ]; then
98 . /etc/dhclient-exit-hooks
100 # probably should do something with exit status of the local script
104 #############################################################################
108 # These function each deal with one particular setting.
109 # They are OS dependent and may be overridden in the
110 # dhclient-enter-hooks file if needed.
112 # These functions are called with either "new" or "old" to indicate which
113 # set of variables to use (new_ip_address or old_ip_address...)
115 #############################################################################
119 local current_hostname=`/bin/hostname`
120 if [ "$current_hostname" = "" ] || \
121 [ "$current_hostname" = "$old_host_name" ]
123 if [ "$new_host_name" != "$old_host_name" ]
125 $LOGGER "New Hostname: $new_host_name"
126 hostname $new_host_name
142 eval ip="\$${1}_ip_address"
143 eval mask="\$${1}_subnet_mask"
144 eval bcast="\$${1}_broadcast_address"
148 ifconfig $interface inet $ip netmask $mask broadcast $bcast $medium
149 # route add $ip 127.0.0.1 > /dev/null 2>&1
162 eval ip="\$${1}_ip_address"
166 ifconfig $interface inet -alias $ip $medium
167 # route delete $ip 127.0.0.1 > /dev/null 2>&1
173 if [ "$alias_ip_address" != "" ]
175 ifconfig $interface inet alias $alias_ip_address netmask $alias_subnet_mask
176 # route add $alias_ip_address 127.0.0.1
182 if [ "$alias_ip_address" != "" ]
184 ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
185 # route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
198 eval router_list="\$${1}_routers"
200 for router in $router_list
202 route add default $router >/dev/null 2>&1
215 eval router_list="\$${1}_routers"
217 for router in $router_list
219 route delete default $router >/dev/null 2>&1
232 eval static_routes="\$${1}_static_routes"
238 $LOGGER "New Static Route: $1 -> $2"
244 unset_static_routes ()
253 eval static_routes="\$${1}_static_routes"
264 #############################################################################
266 # utility functions grouping what needs to be done in logical units.
268 #############################################################################
274 set_static_routes new
276 if [ "$new_ip_address" != "$alias_ip_address" ]
295 if [ "$alias_ip_address" != "$old_ip_address" ]
300 if [ "$old_ip_address" != "" ]
304 unset_static_routes old
331 #############################################################################
332 # Main State functions.
334 # There is a state function for each state of the DHCP client
335 # These functions are OS specific and should be be tampered with.
336 #############################################################################
340 ifconfig $interface $medium
341 ifconfig $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
350 ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 \
351 broadcast 255.255.255.255 up
367 if [ "$old_ip_address" != "$new_ip_address" ]
410 $LOGGER "No good lease information in TIMEOUT state"
416 #############################################################################
419 # dhclient_script_init() parses the optional "enter_hooks" script which can
420 # override any of the state functions
422 # This function also parses the variables and notifies the detected changes.
423 #############################################################################
424 dhclient_script_init ()
426 if [ -x /usr/bin/logger ]; then
427 LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
432 # Invoke the local dhcp client enter hooks, if they exist.
433 if [ -x /etc/dhclient-enter-hooks ]
436 . /etc/dhclient-enter-hooks
437 # allow the local script to abort processing of this state
438 # local script must set exit_status variable to nonzero.
439 if [ $exit_status -ne 0 ]
445 if [ "$new_network_number" != "" ]
447 $LOGGER "New Network Number: $new_network_number"
450 if [ "$new_ip_address" != "" ]
452 $LOGGER "New IP Address: $new_ip_address"
455 if [ "$new_broadcast_address" != "" ]
457 $LOGGER "New Broadcast Address: $new_broadcast_address"
460 if [ "$new_subnet_mask" != "" ]
462 $LOGGER "New Subnet Mask for $interface: $new_subnet_mask"
465 if [ "$alias_subnet_mask" != "" ]
470 #############################################################################
471 # dhclient_main() does the appropriate work depending on the state of
473 #############################################################################
474 dhclient_script_main ()
491 pre_state_${reason}_hook
493 post_state_${reason}_hook
496 $LOGGER "dhclient-script called with invalid reason $reason"
501 exit_with_hooks $exit_status
504 #############################################################################
505 # Let's do the work...
506 #############################################################################
512 #############################################################################
514 #############################################################################