3 # $OpenBSD: dhclient-script,v 1.6 2004/05/06 18:22:41 claudio Exp $
6 # Copyright (c) 2003 Kenneth R Westerback <krw@openbsd.org>
8 # Permission to use, copy, modify, and distribute this software for any
9 # purpose with or without fee is hereby granted, provided that the above
10 # copyright notice and this permission notice appear in all copies.
12 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 NETSTAT=/usr/bin/netstat
25 HOSTNAME=/bin/hostname
29 if [ -x /usr/bin/logger ]; then
30 LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
36 # Helper functions that implement common actions.
40 current_hostname=`$HOSTNAME`
41 if [ -z "$current_hostname" ]; then
42 $LOGGER "New Hostname ($interface): $new_host_name"
43 $HOSTNAME $new_host_name
44 elif [ "$current_hostname" = "$old_host_name" -a \
45 "$new_host_name" != "$old_host_name" ]; then
46 $LOGGER "New Hostname ($interface): $new_host_name"
47 $HOSTNAME $new_host_name
52 arp -an -i $interface | \
53 sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \
57 delete_old_address() {
58 ifconfig $interface inet -alias $old_ip_address $medium
63 inet $new_ip_address \
64 netmask $new_subnet_mask \
65 broadcast $new_broadcast_address \
68 $LOGGER "New IP Address ($interface): $new_ip_address"
69 $LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
70 $LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
71 $LOGGER "New Routers ($interface): $new_routers"
75 if [ -n "$alias_ip_address" ]; then
76 ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
77 route delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
82 if [ -n "$alias_ip_address" ]; then
83 ifconfig $interface inet alias $alias_ip_address netmask \
85 route add $alias_ip_address $LOCALHOST
90 route delete "$old_ip_address" $LOCALHOST >/dev/null 2>&1
91 for router in $old_routers; do
92 if [ $if_defaultroute = x -o $if_defaultroute = $interface ]; then
93 route delete default $route >/dev/null 2>&1
97 if [ -n "$old_static_routes" ]; then
98 set $old_static_routes
99 while [ $# -gt 1 ]; do
100 route delete "$1" "$2"
109 route add $new_ip_address $LOCALHOST >/dev/null 2>&1
110 for router in $new_routers; do
111 if [ "$new_ip_address" = "$router" ]; then
112 route add default -iface $router >/dev/null 2>&1
114 route add default $router >/dev/null 2>&1
116 # 2nd and subsequent default routers error out, so explicitly
117 # stop processing the list after the first one.
121 if [ -n "$new_static_routes" ]; then
122 $LOGGER "New Static Routes ($interface): $new_static_routes"
123 set $new_static_routes
124 while [ $# -gt 1 ]; do
131 add_new_resolv_conf() {
132 # XXX Old code did not create/update resolv.conf unless both
133 # $new_domain_name and $new_domain_name_servers were provided. PR
134 # #3135 reported some ISP's only provide $new_domain_name_servers and
135 # thus broke the script. This code creates the resolv.conf if either
138 rm -f /etc/resolv.conf.std
140 if [ -n "$new_domain_name" ]; then
141 echo "search $new_domain_name" >>/etc/resolv.conf.std
144 if [ -n "$new_domain_name_servers" ]; then
145 for nameserver in $new_domain_name_servers; do
146 echo "nameserver $nameserver" >>/etc/resolv.conf.std
150 if [ -f /etc/resolv.conf.std ]; then
151 if [ -f /etc/resolv.conf.tail ]; then
152 cat /etc/resolv.conf.tail >>/etc/resolv.conf.std
155 # In case (e.g. during OpenBSD installs) /etc/resolv.conf
156 # is a symbolic link, take care to preserve the link and write
157 # the new data in the correct location.
159 if [ -f /etc/resolv.conf ]; then
160 cat /etc/resolv.conf > /etc/resolv.conf.save
162 cat /etc/resolv.conf.std > /etc/resolv.conf
163 rm -f /etc/resolv.conf.std
165 # Try to ensure correct ownership and permissions.
166 chown -RL root:wheel /etc/resolv.conf
167 chmod -RL 644 /etc/resolv.conf
175 # Must be used on exit. Invokes the local dhcp client exit hooks, if any.
178 if [ -f /etc/dhclient-exit-hooks ]; then
179 . /etc/dhclient-exit-hooks
181 # probably should do something with exit status of the local script
186 # Start of active code.
189 # Invoke the local dhcp client enter hooks, if they exist.
190 if [ -f /etc/dhclient-enter-hooks ]; then
192 . /etc/dhclient-enter-hooks
193 # allow the local script to abort processing of this state
194 # local script must set exit_status variable to nonzero.
195 if [ $exit_status -ne 0 ]; then
200 if [ -x $NETSTAT ]; then
201 if_defaulroute=`$NETSTAT -rn | $GREP "^default" | $AWK '{print $6}'`
208 ifconfig $interface $medium
209 ifconfig $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
215 ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
221 BOUND|RENEW|REBIND|REBOOT)
223 if [ -n "$old_ip_address" ]; then
224 if [ "$old_ip_address" != "$alias_ip_address" ]; then
227 if [ "$old_ip_address" != "$new_ip_address" ]; then
232 if [ "$reason" = BOUND ] || \
233 [ "$reason" = REBOOT ] || \
234 [ -z "$old_ip_address" ] || \
235 [ "$old_ip_address" != "$new_ip_address" ]; then
239 if [ "$new_ip_address" != "$alias_ip_address" ]; then
247 if [ -n "$old_ip_address" ]; then
251 # XXX Why add alias we just deleted above?
253 if [ -f /etc/resolv.conf.save ]; then
254 cat /etc/resolv.conf.save > /etc/resolv.conf
262 if [ -n "$new_routers" ]; then
263 $LOGGER "New Routers ($interface): $new_routers"
265 if ping -q -c 1 -w 1 "$1"; then
266 if [ "$new_ip_address" != "$alias_ip_address" ]; then
270 if add_new_resolv_conf; then
275 ifconfig $interface inet -alias $new_ip_address $medium