dhclient-script revision 147072
1#!/bin/sh 2# 3# $OpenBSD: dhclient-script,v 1.6 2004/05/06 18:22:41 claudio Exp $ 4# 5# Copyright (c) 2003 Kenneth R Westerback <krw@openbsd.org> 6# 7# Permission to use, copy, modify, and distribute this software for any 8# purpose with or without fee is hereby granted, provided that the above 9# copyright notice and this permission notice appear in all copies. 10# 11# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18# 19# 20 21# 22# Helper functions that implement common actions. 23# 24 25delete_old_address() { 26 if [ -n "$old_ip_address" ]; then 27 ifconfig $interface inet -alias $old_ip_address $medium 28 route delete "$old_ip_address" 127.0.0.1 >/dev/null 2>&1 29 fi 30} 31 32add_new_address() { 33 ifconfig $interface \ 34 inet $new_ip_address \ 35 netmask $new_subnet_mask \ 36 broadcast $new_broadcast_address \ 37 $medium 38 39 # XXX Original TIMEOUT code did not do this unless $new_routers was set? 40 route add $new_ip_address 127.0.0.1 >/dev/null 2>&1 41} 42 43delete_old_alias() { 44 if [ -n "$alias_ip_address" ]; then 45 ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1 46 route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1 47 fi 48} 49 50add_new_alias() { 51 if [ -n "$alias_ip_address" ]; then 52 ifconfig $interface inet alias $alias_ip_address netmask \ 53 $alias_subnet_mask 54 route add $alias_ip_address 127.0.0.1 55 fi 56} 57 58delete_old_routes() { 59 # Delete existing default route. We only allow one, so no need to 60 # process $old_routers list. 61 route delete default >/dev/null 2>&1 62 63 if [ -n "$old_static_routes" ]; then 64 set $old_static_routes 65 while [ $# -gt 1 ]; do 66 route delete "$1" "$2" 67 shift; shift 68 done 69 fi 70 71 arp -dan 72} 73 74add_new_routes() { 75 route delete default >/dev/null 2>&1 76 for router in $new_routers; do 77 if [ "$new_ip_address" = "$router" ]; then 78 route add default -iface $router >/dev/null 2>&1 79 else 80 route add default $router >/dev/null 2>&1 81 fi 82 # 2nd and subsequent default routers error out, so explicitly 83 # stop processing the list after the first one. 84 break 85 done 86 87 if [ -n "$new_static_routes" ]; then 88 set $new_static_routes 89 while [ $# -gt 1 ]; do 90 route add $1 $2 91 shift; shift 92 done 93 fi 94} 95 96add_new_resolv_conf() { 97 # XXX Old code did not create/update resolv.conf unless both 98 # $new_domain_name and $new_domain_name_servers were provided. PR 99 # #3135 reported some ISP's only provide $new_domain_name_servers and 100 # thus broke the script. This code creates the resolv.conf if either 101 # are provided. 102 103 rm -f /etc/resolv.conf.std 104 105 if [ -n "$new_domain_name" ]; then 106 echo "search $new_domain_name" >>/etc/resolv.conf.std 107 fi 108 109 if [ -n "$new_domain_name_servers" ]; then 110 for nameserver in $new_domain_name_servers; do 111 echo "nameserver $nameserver" >>/etc/resolv.conf.std 112 done 113 fi 114 115 if [ -f /etc/resolv.conf.std ]; then 116 if [ -f /etc/resolv.conf.tail ]; then 117 cat /etc/resolv.conf.tail >>/etc/resolv.conf.std 118 fi 119 120 # In case (e.g. during OpenBSD installs) /etc/resolv.conf 121 # is a symbolic link, take care to preserve the link and write 122 # the new data in the correct location. 123 124 if [ -f /etc/resolv.conf ]; then 125 cat /etc/resolv.conf > /etc/resolv.conf.save 126 fi 127 cat /etc/resolv.conf.std > /etc/resolv.conf 128 rm -f /etc/resolv.conf.std 129 130 # Try to ensure correct ownership and permissions. 131 chown -RL root:wheel /etc/resolv.conf 132 chmod -RL 644 /etc/resolv.conf 133 134 return 0 135 fi 136 137 return 1 138} 139 140# 141# Start of active code. 142# 143 144if [ -n "$new_network_number" ]; then 145 echo "New Network Number: $new_network_number" 146fi 147 148if [ -n "$new_broadcast_address" ]; then 149 echo "New Broadcast Address: $new_broadcast_address" 150fi 151 152case $reason in 153MEDIUM) 154 ifconfig $interface $medium 155 ifconfig $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1 156 sleep 1 157 ;; 158 159PREINIT) 160 delete_old_alias 161 ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up 162 ;; 163 164ARPCHECK|ARPSEND) 165 ;; 166 167BOUND|RENEW|REBIND|REBOOT) 168 if [ -n "$old_ip_address" ]; then 169 if [ "$old_ip_address" != "$alias_ip_address" ]; then 170 delete_old_alias 171 fi 172 if [ "$old_ip_address" != "$new_ip_address" ]; then 173 delete_old_address 174 delete_old_routes 175 fi 176 fi 177 if [ "$reason" = BOUND ] || \ 178 [ "$reason" = REBOOT ] || \ 179 [ -z "$old_ip_address" ] || \ 180 [ "$old_ip_address" != "$new_ip_address" ]; then 181 add_new_address 182 add_new_routes 183 fi 184 if [ "$new_ip_address" != "$alias_ip_address" ]; then 185 add_new_alias 186 fi 187 add_new_resolv_conf 188 ;; 189 190EXPIRE|FAIL) 191 delete_old_alias 192 if [ -n "$old_ip_address" ]; then 193 delete_old_address 194 delete_old_routes 195 fi 196 # XXX Why add alias we just deleted above? 197 add_new_alias 198 if [ -f /etc/resolv.conf.save ]; then 199 cat /etc/resolv.conf.save > /etc/resolv.conf 200 fi 201 ;; 202 203TIMEOUT) 204 delete_old_alias 205 add_new_address 206 sleep 1 207 if [ -n "$new_routers" ]; then 208 set "$new_routers" 209 if ping -q -c 1 -w 1 "$1"; then 210 if [ "$new_ip_address" != "$alias_ip_address" ]; then 211 add_new_alias 212 fi 213 add_new_routes 214 if add_new_resolv_conf; then 215 exit 0 216 fi 217 fi 218 fi 219 ifconfig $interface inet -alias $new_ip_address $medium 220 # XXX Why not a delete_old_address as before all other invocations of 221 # delete_old_routes? 222 delete_old_routes 223 exit 1 224 ;; 225esac 226 227exit 0 228