1#!/bin/sh 2# 3# $OpenBSD: dhclient-script,v 1.6 2004/05/06 18:22:41 claudio Exp $ 4# $FreeBSD$ 5# 6# Copyright (c) 2003 Kenneth R Westerback <krw@openbsd.org> 7# 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. 11# 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. 19# 20# 21 22NETSTAT=/usr/bin/netstat 23AWK=/usr/bin/awk 24HOSTNAME=/bin/hostname 25 26LOCALHOST=127.0.0.1 27 28if [ -x /usr/bin/logger ]; then 29 LOGGER="/usr/bin/logger -s -p user.notice -t dhclient" 30else 31 LOGGER=echo 32fi 33 34# 35# Helper functions that implement common actions. 36# 37 38check_hostname() { 39 current_hostname=`$HOSTNAME` 40 if [ -z "$current_hostname" ]; then 41 $LOGGER "New Hostname ($interface): $new_host_name" 42 $HOSTNAME $new_host_name 43 elif [ "$current_hostname" = "$old_host_name" -a \ 44 "$new_host_name" != "$old_host_name" ]; then 45 $LOGGER "New Hostname ($interface): $new_host_name" 46 $HOSTNAME $new_host_name 47 fi 48} 49 50arp_flush() { 51 arp -an -i $interface | \ 52 sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \ 53 sh >/dev/null 2>&1 54} 55 56delete_old_address() { 57 eval "ifconfig $interface inet -alias $old_ip_address $medium" 58} 59 60add_new_address() { 61 eval "ifconfig $interface \ 62 inet $new_ip_address \ 63 netmask $new_subnet_mask \ 64 broadcast $new_broadcast_address \ 65 $medium" 66 67 $LOGGER "New IP Address ($interface): $new_ip_address" 68 $LOGGER "New Subnet Mask ($interface): $new_subnet_mask" 69 $LOGGER "New Broadcast Address ($interface): $new_broadcast_address" 70 $LOGGER "New Routers ($interface): $new_routers" 71} 72 73delete_old_alias() { 74 if [ -n "$alias_ip_address" ]; then 75 ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1 76 #route delete $alias_ip_address $LOCALHOST > /dev/null 2>&1 77 fi 78} 79 80add_new_alias() { 81 if [ -n "$alias_ip_address" ]; then 82 ifconfig $interface inet alias $alias_ip_address netmask \ 83 $alias_subnet_mask 84 #route add $alias_ip_address $LOCALHOST 85 fi 86} 87 88delete_old_routes() { 89 #route delete "$old_ip_address" $LOCALHOST >/dev/null 2>&1 90 for router in $old_routers; do 91 if [ $if_defaultroute = x -o $if_defaultroute = $interface ]; then 92 route delete default $route >/dev/null 2>&1 93 fi 94 done 95 96 if [ -n "$old_static_routes" ]; then 97 set $old_static_routes 98 while [ $# -gt 1 ]; do 99 route delete "$1" "$2" 100 shift; shift 101 done 102 fi 103 104 arp_flush 105} 106 107add_new_routes() { 108 #route add $new_ip_address $LOCALHOST >/dev/null 2>&1 109 for router in $new_routers; do 110 if [ "$new_ip_address" = "$router" ]; then 111 route add default -iface $router >/dev/null 2>&1 112 else 113 route add default $router >/dev/null 2>&1 114 fi 115 # 2nd and subsequent default routers error out, so explicitly 116 # stop processing the list after the first one. 117 break 118 done 119 120 if [ -n "$new_static_routes" ]; then 121 $LOGGER "New Static Routes ($interface): $new_static_routes" 122 set $new_static_routes 123 while [ $# -gt 1 ]; do 124 route add $1 $2 125 shift; shift 126 done 127 fi 128} 129 130add_new_resolv_conf() { 131 # XXX Old code did not create/update resolv.conf unless both 132 # $new_domain_name and $new_domain_name_servers were provided. PR 133 # #3135 reported some ISP's only provide $new_domain_name_servers and 134 # thus broke the script. This code creates the resolv.conf if either 135 # are provided. 136 137 rm -f /etc/resolv.conf.std 138 139 if [ -n "$new_domain_name" ]; then 140 echo "search $new_domain_name" >>/etc/resolv.conf.std 141 fi 142 143 if [ -n "$new_domain_name_servers" ]; then 144 for nameserver in $new_domain_name_servers; do 145 echo "nameserver $nameserver" >>/etc/resolv.conf.std 146 done 147 fi 148 149 if [ -f /etc/resolv.conf.std ]; then 150 if [ -f /etc/resolv.conf.tail ]; then 151 cat /etc/resolv.conf.tail >>/etc/resolv.conf.std 152 fi 153 154 # When resolv.conf is not changed actually, we don't 155 # need to update it. 156 # If /usr is not mounted yet, we cannot use cmp, then 157 # the following test fails. In such case, we simply 158 # ignore an error and do update resolv.conf. 159 if cmp -s /etc/resolv.conf.std /etc/resolv.conf; then 160 rm -f /etc/resolv.conf.std 161 return 0 162 fi 2>/dev/null 163 164 # In case (e.g. during OpenBSD installs) /etc/resolv.conf 165 # is a symbolic link, take care to preserve the link and write 166 # the new data in the correct location. 167 168 if [ -f /etc/resolv.conf ]; then 169 cat /etc/resolv.conf > /etc/resolv.conf.save 170 fi 171 cat /etc/resolv.conf.std > /etc/resolv.conf 172 rm -f /etc/resolv.conf.std 173 174 # Try to ensure correct ownership and permissions. 175 chown -RL root:wheel /etc/resolv.conf 176 chmod -RL 644 /etc/resolv.conf 177 178 return 0 179 fi 180 181 return 1 182} 183 184# Must be used on exit. Invokes the local dhcp client exit hooks, if any. 185exit_with_hooks() { 186 exit_status=$1 187 if [ -f /etc/dhclient-exit-hooks ]; then 188 . /etc/dhclient-exit-hooks 189 fi 190 # probably should do something with exit status of the local script 191 exit $exit_status 192} 193 194# 195# Start of active code. 196# 197 198# Invoke the local dhcp client enter hooks, if they exist. 199if [ -f /etc/dhclient-enter-hooks ]; then 200 exit_status=0 201 . /etc/dhclient-enter-hooks 202 # allow the local script to abort processing of this state 203 # local script must set exit_status variable to nonzero. 204 if [ $exit_status -ne 0 ]; then 205 exit $exit_status 206 fi 207fi 208 209if [ -x $NETSTAT ]; then 210 if_defaultroute=`$NETSTAT -rnf inet | $AWK '{if ($1=="default") printf $6}'` 211else 212 if_defaultroute="x" 213fi 214 215case $reason in 216MEDIUM) 217 eval "ifconfig $interface $medium" 218 eval "ifconfig $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1 219 sleep 1 220 ;; 221 222PREINIT) 223 delete_old_alias 224 ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up 225 ;; 226 227ARPCHECK|ARPSEND) 228 ;; 229 230BOUND|RENEW|REBIND|REBOOT) 231 check_hostname 232 if [ -n "$old_ip_address" ]; then 233 if [ "$old_ip_address" != "$alias_ip_address" ]; then 234 delete_old_alias 235 fi 236 if [ "$old_ip_address" != "$new_ip_address" ]; then 237 delete_old_address 238 delete_old_routes 239 fi 240 fi 241 if [ "$reason" = BOUND ] || \ 242 [ "$reason" = REBOOT ] || \ 243 [ -z "$old_ip_address" ] || \ 244 [ "$old_ip_address" != "$new_ip_address" ]; then 245 add_new_address 246 add_new_routes 247 fi 248 if [ "$new_ip_address" != "$alias_ip_address" ]; then 249 add_new_alias 250 fi 251 add_new_resolv_conf 252 ;; 253 254EXPIRE|FAIL) 255 delete_old_alias 256 if [ -n "$old_ip_address" ]; then 257 delete_old_address 258 delete_old_routes 259 fi 260 ifconfig $interface delete 261 # XXX Why add alias we just deleted above? 262 add_new_alias 263 if [ -f /etc/resolv.conf.save ]; then 264 cat /etc/resolv.conf.save > /etc/resolv.conf 265 fi 266 ;; 267 268TIMEOUT) 269 delete_old_alias 270 add_new_address 271 sleep 1 272 if [ -n "$new_routers" ]; then 273 $LOGGER "New Routers ($interface): $new_routers" 274 set "$new_routers" 275 if ping -q -c 1 -w 1 "$1"; then 276 if [ "$new_ip_address" != "$alias_ip_address" ]; then 277 add_new_alias 278 fi 279 add_new_routes 280 if add_new_resolv_conf; then 281 exit_with_hooks 0 282 fi 283 fi 284 fi 285 eval "ifconfig $interface inet -alias $new_ip_address $medium" 286 delete_old_routes 287 exit_with_hooks 1 288 ;; 289esac 290 291exit_with_hooks 0 292