dhclient-script revision 168689
1147072Sbrooks#!/bin/sh 2147072Sbrooks# 3147072Sbrooks# $OpenBSD: dhclient-script,v 1.6 2004/05/06 18:22:41 claudio Exp $ 4147086Sbrooks# $FreeBSD: head/sbin/dhclient/dhclient-script 168689 2007-04-13 15:07:10Z emaste $ 5147072Sbrooks# 6147072Sbrooks# Copyright (c) 2003 Kenneth R Westerback <krw@openbsd.org> 7147072Sbrooks# 8147072Sbrooks# Permission to use, copy, modify, and distribute this software for any 9147072Sbrooks# purpose with or without fee is hereby granted, provided that the above 10147072Sbrooks# copyright notice and this permission notice appear in all copies. 11147072Sbrooks# 12147072Sbrooks# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13147072Sbrooks# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14147072Sbrooks# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15147072Sbrooks# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16147072Sbrooks# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17147072Sbrooks# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18147072Sbrooks# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19147072Sbrooks# 20147072Sbrooks# 21147072Sbrooks 22154164SbrooksARP=/usr/sbin/arp 23147086SbrooksAWK=/usr/bin/awk 24147086SbrooksHOSTNAME=/bin/hostname 25154164SbrooksNETSTAT=/usr/bin/netstat 26147086Sbrooks 27147086SbrooksLOCALHOST=127.0.0.1 28147086Sbrooks 29147086Sbrooksif [ -x /usr/bin/logger ]; then 30147086Sbrooks LOGGER="/usr/bin/logger -s -p user.notice -t dhclient" 31147086Sbrookselse 32147086Sbrooks LOGGER=echo 33147086Sbrooksfi 34147086Sbrooks 35147072Sbrooks# 36147072Sbrooks# Helper functions that implement common actions. 37147072Sbrooks# 38147072Sbrooks 39147086Sbrookscheck_hostname() { 40147086Sbrooks current_hostname=`$HOSTNAME` 41147086Sbrooks if [ -z "$current_hostname" ]; then 42147086Sbrooks $LOGGER "New Hostname ($interface): $new_host_name" 43147086Sbrooks $HOSTNAME $new_host_name 44147086Sbrooks elif [ "$current_hostname" = "$old_host_name" -a \ 45147086Sbrooks "$new_host_name" != "$old_host_name" ]; then 46147086Sbrooks $LOGGER "New Hostname ($interface): $new_host_name" 47147086Sbrooks $HOSTNAME $new_host_name 48147072Sbrooks fi 49147072Sbrooks} 50147072Sbrooks 51147086Sbrooksarp_flush() { 52147086Sbrooks arp -an -i $interface | \ 53147086Sbrooks sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \ 54147086Sbrooks sh >/dev/null 2>&1 55147086Sbrooks} 56147086Sbrooks 57147086Sbrooksdelete_old_address() { 58149519Sbrooks eval "ifconfig $interface inet -alias $old_ip_address $medium" 59147086Sbrooks} 60147086Sbrooks 61147072Sbrooksadd_new_address() { 62149519Sbrooks eval "ifconfig $interface \ 63147072Sbrooks inet $new_ip_address \ 64147072Sbrooks netmask $new_subnet_mask \ 65147072Sbrooks broadcast $new_broadcast_address \ 66149519Sbrooks $medium" 67147072Sbrooks 68147086Sbrooks $LOGGER "New IP Address ($interface): $new_ip_address" 69147086Sbrooks $LOGGER "New Subnet Mask ($interface): $new_subnet_mask" 70147086Sbrooks $LOGGER "New Broadcast Address ($interface): $new_broadcast_address" 71147086Sbrooks $LOGGER "New Routers ($interface): $new_routers" 72147072Sbrooks} 73147072Sbrooks 74147072Sbrooksdelete_old_alias() { 75147072Sbrooks if [ -n "$alias_ip_address" ]; then 76147072Sbrooks ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1 77149479Sbrooks #route delete $alias_ip_address $LOCALHOST > /dev/null 2>&1 78147072Sbrooks fi 79147072Sbrooks} 80147072Sbrooks 81147072Sbrooksadd_new_alias() { 82147072Sbrooks if [ -n "$alias_ip_address" ]; then 83147072Sbrooks ifconfig $interface inet alias $alias_ip_address netmask \ 84147072Sbrooks $alias_subnet_mask 85149479Sbrooks #route add $alias_ip_address $LOCALHOST 86147072Sbrooks fi 87147072Sbrooks} 88147072Sbrooks 89166602Semastefill_classless_routes() { 90166602Semaste set $1 91168689Semaste while [ $# -ge 5 ]; do 92166602Semaste if [ $1 -eq 0 ]; then 93166602Semaste route="default" 94166602Semaste elif [ $1 -le 8 ]; then 95166602Semaste route="$2.0.0.0/$1" 96166602Semaste shift 97166602Semaste elif [ $1 -le 16 ]; then 98166602Semaste route="$2.$3.0.0/$1" 99166602Semaste shift; shift 100166602Semaste elif [ $1 -le 24 ]; then 101166602Semaste route="$2.$3.$4.0/$1" 102166602Semaste shift; shift; shift 103166602Semaste else 104166602Semaste route="$2.$3.$4.$5/$1" 105166602Semaste shift; shift; shift; shift 106166602Semaste fi 107166602Semaste shift 108166602Semaste router="$1.$2.$3.$4" 109166602Semaste classless_routes="$classless_routes $route $router" 110166602Semaste shift; shift; shift; shift 111166602Semaste done 112166602Semaste} 113166602Semaste 114147072Sbrooksdelete_old_routes() { 115149479Sbrooks #route delete "$old_ip_address" $LOCALHOST >/dev/null 2>&1 116166602Semaste if [ -n "$old_classless_routes" ]; then 117166602Semaste fill_classless_routes "$old_classless_routes" 118166602Semaste set $classless_routes 119166602Semaste while [ $# -gt 1 ]; do 120166602Semaste route delete "$1" "$2" 121166602Semaste shift; shift 122166602Semaste done 123166602Semaste return 0; 124166602Semaste fi 125166602Semaste 126147086Sbrooks for router in $old_routers; do 127147086Sbrooks if [ $if_defaultroute = x -o $if_defaultroute = $interface ]; then 128147086Sbrooks route delete default $route >/dev/null 2>&1 129147086Sbrooks fi 130147086Sbrooks done 131147072Sbrooks 132147072Sbrooks if [ -n "$old_static_routes" ]; then 133147072Sbrooks set $old_static_routes 134147072Sbrooks while [ $# -gt 1 ]; do 135147072Sbrooks route delete "$1" "$2" 136147072Sbrooks shift; shift 137147072Sbrooks done 138147072Sbrooks fi 139147072Sbrooks 140147086Sbrooks arp_flush 141147072Sbrooks} 142147072Sbrooks 143147072Sbrooksadd_new_routes() { 144149479Sbrooks #route add $new_ip_address $LOCALHOST >/dev/null 2>&1 145166602Semaste 146166602Semaste # RFC 3442: If the DHCP server returns both a Classless Static 147166602Semaste # Routes option and a Router option, the DHCP client MUST ignore 148166602Semaste # the Router option. 149166602Semaste # 150166602Semaste # DHCP clients that support this option (Classless Static Routes) 151166602Semaste # MUST NOT install the routes specified in the Static Routes 152166602Semaste # option (option code 33) if both a Static Routes option and the 153166602Semaste # Classless Static Routes option are provided. 154166602Semaste 155166602Semaste if [ -n "$new_classless_routes" ]; then 156166602Semaste fill_classless_routes "$new_classless_routes" 157166602Semaste $LOGGER "New Classless Static Routes ($interface): $classless_routes" 158166602Semaste set $classless_routes 159166602Semaste while [ $# -gt 1 ]; do 160166602Semaste if [ "0.0.0.0" = "$2" ]; then 161166602Semaste route add "$1" -iface "$interface" 162166602Semaste else 163166602Semaste route add "$1" "$2" 164166602Semaste fi 165166602Semaste shift; shift 166166602Semaste done 167166602Semaste return 168166602Semaste fi 169166602Semaste 170147072Sbrooks for router in $new_routers; do 171147072Sbrooks if [ "$new_ip_address" = "$router" ]; then 172147072Sbrooks route add default -iface $router >/dev/null 2>&1 173147072Sbrooks else 174147072Sbrooks route add default $router >/dev/null 2>&1 175147072Sbrooks fi 176147072Sbrooks # 2nd and subsequent default routers error out, so explicitly 177147072Sbrooks # stop processing the list after the first one. 178147072Sbrooks break 179147072Sbrooks done 180147072Sbrooks 181147072Sbrooks if [ -n "$new_static_routes" ]; then 182147086Sbrooks $LOGGER "New Static Routes ($interface): $new_static_routes" 183147072Sbrooks set $new_static_routes 184147072Sbrooks while [ $# -gt 1 ]; do 185147072Sbrooks route add $1 $2 186147072Sbrooks shift; shift 187147072Sbrooks done 188147072Sbrooks fi 189147072Sbrooks} 190147072Sbrooks 191147072Sbrooksadd_new_resolv_conf() { 192147072Sbrooks # XXX Old code did not create/update resolv.conf unless both 193147072Sbrooks # $new_domain_name and $new_domain_name_servers were provided. PR 194147072Sbrooks # #3135 reported some ISP's only provide $new_domain_name_servers and 195147072Sbrooks # thus broke the script. This code creates the resolv.conf if either 196147072Sbrooks # are provided. 197147072Sbrooks 198154869Sbrooks local tmpres=/var/run/resolv.conf.${interface} 199154702Swes rm -f $tmpres 200147072Sbrooks 201147072Sbrooks if [ -n "$new_domain_name" ]; then 202154702Swes echo "search $new_domain_name" >>$tmpres 203147072Sbrooks fi 204147072Sbrooks 205147072Sbrooks if [ -n "$new_domain_name_servers" ]; then 206147072Sbrooks for nameserver in $new_domain_name_servers; do 207154702Swes echo "nameserver $nameserver" >>$tmpres 208147072Sbrooks done 209147072Sbrooks fi 210147072Sbrooks 211154702Swes if [ -f $tmpres ]; then 212147072Sbrooks if [ -f /etc/resolv.conf.tail ]; then 213154702Swes cat /etc/resolv.conf.tail >>$tmpres 214147072Sbrooks fi 215147072Sbrooks 216149898Sbrooks # When resolv.conf is not changed actually, we don't 217149898Sbrooks # need to update it. 218149898Sbrooks # If /usr is not mounted yet, we cannot use cmp, then 219149898Sbrooks # the following test fails. In such case, we simply 220149898Sbrooks # ignore an error and do update resolv.conf. 221154702Swes if cmp -s $tmpres /etc/resolv.conf; then 222154702Swes rm -f $tmpres 223149898Sbrooks return 0 224149898Sbrooks fi 2>/dev/null 225149898Sbrooks 226147072Sbrooks # In case (e.g. during OpenBSD installs) /etc/resolv.conf 227147072Sbrooks # is a symbolic link, take care to preserve the link and write 228147072Sbrooks # the new data in the correct location. 229147072Sbrooks 230147072Sbrooks if [ -f /etc/resolv.conf ]; then 231147072Sbrooks cat /etc/resolv.conf > /etc/resolv.conf.save 232147072Sbrooks fi 233154702Swes cat $tmpres > /etc/resolv.conf 234154702Swes rm -f $tmpres 235147072Sbrooks 236147072Sbrooks # Try to ensure correct ownership and permissions. 237147072Sbrooks chown -RL root:wheel /etc/resolv.conf 238147072Sbrooks chmod -RL 644 /etc/resolv.conf 239147072Sbrooks 240147072Sbrooks return 0 241147072Sbrooks fi 242147072Sbrooks 243147072Sbrooks return 1 244147072Sbrooks} 245147072Sbrooks 246147138Sbrooks# Must be used on exit. Invokes the local dhcp client exit hooks, if any. 247147138Sbrooksexit_with_hooks() { 248147138Sbrooks exit_status=$1 249147138Sbrooks if [ -f /etc/dhclient-exit-hooks ]; then 250147138Sbrooks . /etc/dhclient-exit-hooks 251147138Sbrooks fi 252147138Sbrooks # probably should do something with exit status of the local script 253147138Sbrooks exit $exit_status 254147138Sbrooks} 255147138Sbrooks 256147072Sbrooks# 257147072Sbrooks# Start of active code. 258147072Sbrooks# 259147072Sbrooks 260147218Sbrooks# Invoke the local dhcp client enter hooks, if they exist. 261147218Sbrooksif [ -f /etc/dhclient-enter-hooks ]; then 262147218Sbrooks exit_status=0 263147218Sbrooks . /etc/dhclient-enter-hooks 264147218Sbrooks # allow the local script to abort processing of this state 265147218Sbrooks # local script must set exit_status variable to nonzero. 266147218Sbrooks if [ $exit_status -ne 0 ]; then 267147218Sbrooks exit $exit_status 268147218Sbrooks fi 269147218Sbrooksfi 270147218Sbrooks 271147086Sbrooksif [ -x $NETSTAT ]; then 272149480Sbrooks if_defaultroute=`$NETSTAT -rnf inet | $AWK '{if ($1=="default") printf $6}'` 273147086Sbrookselse 274147086Sbrooks if_defaultroute="x" 275147072Sbrooksfi 276147072Sbrooks 277147072Sbrookscase $reason in 278147072SbrooksMEDIUM) 279149519Sbrooks eval "ifconfig $interface $medium" 280149519Sbrooks eval "ifconfig $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1 281147072Sbrooks sleep 1 282147072Sbrooks ;; 283147072Sbrooks 284147072SbrooksPREINIT) 285147072Sbrooks delete_old_alias 286147072Sbrooks ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up 287147072Sbrooks ;; 288147072Sbrooks 289147072SbrooksARPCHECK|ARPSEND) 290147072Sbrooks ;; 291147072Sbrooks 292147072SbrooksBOUND|RENEW|REBIND|REBOOT) 293147086Sbrooks check_hostname 294147072Sbrooks if [ -n "$old_ip_address" ]; then 295147072Sbrooks if [ "$old_ip_address" != "$alias_ip_address" ]; then 296147072Sbrooks delete_old_alias 297147072Sbrooks fi 298147072Sbrooks if [ "$old_ip_address" != "$new_ip_address" ]; then 299147072Sbrooks delete_old_address 300147072Sbrooks delete_old_routes 301147072Sbrooks fi 302147072Sbrooks fi 303147072Sbrooks if [ "$reason" = BOUND ] || \ 304147072Sbrooks [ "$reason" = REBOOT ] || \ 305147072Sbrooks [ -z "$old_ip_address" ] || \ 306147072Sbrooks [ "$old_ip_address" != "$new_ip_address" ]; then 307147072Sbrooks add_new_address 308147072Sbrooks add_new_routes 309147072Sbrooks fi 310147072Sbrooks if [ "$new_ip_address" != "$alias_ip_address" ]; then 311147072Sbrooks add_new_alias 312147072Sbrooks fi 313147072Sbrooks add_new_resolv_conf 314147072Sbrooks ;; 315147072Sbrooks 316147072SbrooksEXPIRE|FAIL) 317147072Sbrooks delete_old_alias 318147072Sbrooks if [ -n "$old_ip_address" ]; then 319147072Sbrooks delete_old_address 320147072Sbrooks delete_old_routes 321147072Sbrooks fi 322154164Sbrooks if [ -x $ARP ]; then 323154164Sbrooks $ARP -d -a -i $interface 324154164Sbrooks fi 325147072Sbrooks # XXX Why add alias we just deleted above? 326147072Sbrooks add_new_alias 327147072Sbrooks if [ -f /etc/resolv.conf.save ]; then 328147072Sbrooks cat /etc/resolv.conf.save > /etc/resolv.conf 329147072Sbrooks fi 330147072Sbrooks ;; 331147072Sbrooks 332147072SbrooksTIMEOUT) 333147072Sbrooks delete_old_alias 334147072Sbrooks add_new_address 335147072Sbrooks sleep 1 336147072Sbrooks if [ -n "$new_routers" ]; then 337147086Sbrooks $LOGGER "New Routers ($interface): $new_routers" 338147072Sbrooks set "$new_routers" 339154760Sbrooks if ping -q -c 1 -t 1 "$1"; then 340147072Sbrooks if [ "$new_ip_address" != "$alias_ip_address" ]; then 341147072Sbrooks add_new_alias 342147072Sbrooks fi 343147072Sbrooks add_new_routes 344147072Sbrooks if add_new_resolv_conf; then 345147138Sbrooks exit_with_hooks 0 346147072Sbrooks fi 347147072Sbrooks fi 348147072Sbrooks fi 349149519Sbrooks eval "ifconfig $interface inet -alias $new_ip_address $medium" 350147072Sbrooks delete_old_routes 351147138Sbrooks exit_with_hooks 1 352147072Sbrooks ;; 353147072Sbrooksesac 354147072Sbrooks 355147138Sbrooksexit_with_hooks 0 356