dhclient-script revision 154869
1198389Smav#!/bin/sh 2198389Smav# 3198389Smav# $OpenBSD: dhclient-script,v 1.6 2004/05/06 18:22:41 claudio Exp $ 4198389Smav# $FreeBSD: head/sbin/dhclient/dhclient-script 154869 2006-01-26 21:05:39Z brooks $ 5198389Smav# 6198389Smav# Copyright (c) 2003 Kenneth R Westerback <krw@openbsd.org> 7198389Smav# 8198389Smav# Permission to use, copy, modify, and distribute this software for any 9198389Smav# purpose with or without fee is hereby granted, provided that the above 10198389Smav# copyright notice and this permission notice appear in all copies. 11198389Smav# 12198389Smav# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13198389Smav# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14198389Smav# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15198389Smav# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16198389Smav# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17198389Smav# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18198389Smav# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19198389Smav# 20198389Smav# 21198389Smav 22198389SmavARP=/usr/sbin/arp 23198389SmavAWK=/usr/bin/awk 24198389SmavHOSTNAME=/bin/hostname 25198389SmavNETSTAT=/usr/bin/netstat 26198389Smav 27198389SmavLOCALHOST=127.0.0.1 28198389Smav 29198389Smavif [ -x /usr/bin/logger ]; then 30198389Smav LOGGER="/usr/bin/logger -s -p user.notice -t dhclient" 31198389Smavelse 32198389Smav LOGGER=echo 33198389Smavfi 34198389Smav 35198389Smav# 36198389Smav# Helper functions that implement common actions. 37198389Smav# 38198389Smav 39198389Smavcheck_hostname() { 40198389Smav current_hostname=`$HOSTNAME` 41198389Smav if [ -z "$current_hostname" ]; then 42198389Smav $LOGGER "New Hostname ($interface): $new_host_name" 43198389Smav $HOSTNAME $new_host_name 44198389Smav elif [ "$current_hostname" = "$old_host_name" -a \ 45198389Smav "$new_host_name" != "$old_host_name" ]; then 46198389Smav $LOGGER "New Hostname ($interface): $new_host_name" 47198389Smav $HOSTNAME $new_host_name 48198389Smav fi 49198389Smav} 50198389Smav 51198389Smavarp_flush() { 52198389Smav arp -an -i $interface | \ 53198389Smav sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \ 54198389Smav sh >/dev/null 2>&1 55198389Smav} 56198389Smav 57200218Smavdelete_old_address() { 58198389Smav eval "ifconfig $interface inet -alias $old_ip_address $medium" 59198389Smav} 60198389Smav 61198389Smavadd_new_address() { 62198389Smav eval "ifconfig $interface \ 63198389Smav inet $new_ip_address \ 64198389Smav netmask $new_subnet_mask \ 65198389Smav broadcast $new_broadcast_address \ 66198389Smav $medium" 67251096Smav 68251096Smav $LOGGER "New IP Address ($interface): $new_ip_address" 69251096Smav $LOGGER "New Subnet Mask ($interface): $new_subnet_mask" 70199321Smav $LOGGER "New Broadcast Address ($interface): $new_broadcast_address" 71198389Smav $LOGGER "New Routers ($interface): $new_routers" 72198389Smav} 73198389Smav 74198389Smavdelete_old_alias() { 75199321Smav if [ -n "$alias_ip_address" ]; then 76198389Smav ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1 77198389Smav #route delete $alias_ip_address $LOCALHOST > /dev/null 2>&1 78198389Smav fi 79198389Smav} 80198389Smav 81198389Smavadd_new_alias() { 82198389Smav if [ -n "$alias_ip_address" ]; then 83198389Smav ifconfig $interface inet alias $alias_ip_address netmask \ 84198389Smav $alias_subnet_mask 85198389Smav #route add $alias_ip_address $LOCALHOST 86198389Smav fi 87198389Smav} 88198389Smav 89198389Smavdelete_old_routes() { 90198389Smav #route delete "$old_ip_address" $LOCALHOST >/dev/null 2>&1 91198389Smav for router in $old_routers; do 92198389Smav if [ $if_defaultroute = x -o $if_defaultroute = $interface ]; then 93198389Smav route delete default $route >/dev/null 2>&1 94198389Smav fi 95198389Smav done 96198389Smav 97198389Smav if [ -n "$old_static_routes" ]; then 98198389Smav set $old_static_routes 99198389Smav while [ $# -gt 1 ]; do 100198389Smav route delete "$1" "$2" 101198708Smav shift; shift 102198389Smav done 103198708Smav fi 104203108Smav 105203108Smav arp_flush 106203108Smav} 107207499Smav 108198389Smavadd_new_routes() { 109198389Smav #route add $new_ip_address $LOCALHOST >/dev/null 2>&1 110198389Smav for router in $new_routers; do 111198389Smav if [ "$new_ip_address" = "$router" ]; then 112198389Smav route add default -iface $router >/dev/null 2>&1 113198389Smav else 114198389Smav route add default $router >/dev/null 2>&1 115198389Smav fi 116198389Smav # 2nd and subsequent default routers error out, so explicitly 117198389Smav # stop processing the list after the first one. 118198389Smav break 119198389Smav done 120198389Smav 121198389Smav if [ -n "$new_static_routes" ]; then 122198389Smav $LOGGER "New Static Routes ($interface): $new_static_routes" 123198389Smav set $new_static_routes 124198389Smav while [ $# -gt 1 ]; do 125198389Smav route add $1 $2 126198389Smav shift; shift 127198389Smav done 128198389Smav fi 129198389Smav} 130198389Smav 131198389Smavadd_new_resolv_conf() { 132236765Smav # XXX Old code did not create/update resolv.conf unless both 133236765Smav # $new_domain_name and $new_domain_name_servers were provided. PR 134236765Smav # #3135 reported some ISP's only provide $new_domain_name_servers and 135236765Smav # thus broke the script. This code creates the resolv.conf if either 136198389Smav # are provided. 137198389Smav 138236765Smav local tmpres=/var/run/resolv.conf.${interface} 139198389Smav rm -f $tmpres 140248085Smarius 141198389Smav if [ -n "$new_domain_name" ]; then 142198389Smav echo "search $new_domain_name" >>$tmpres 143198389Smav fi 144198389Smav 145198389Smav if [ -n "$new_domain_name_servers" ]; then 146198389Smav for nameserver in $new_domain_name_servers; do 147198389Smav echo "nameserver $nameserver" >>$tmpres 148236765Smav done 149236765Smav fi 150236765Smav 151198389Smav if [ -f $tmpres ]; then 152198389Smav if [ -f /etc/resolv.conf.tail ]; then 153198389Smav cat /etc/resolv.conf.tail >>$tmpres 154198389Smav fi 155198708Smav 156198708Smav # When resolv.conf is not changed actually, we don't 157198389Smav # need to update it. 158198389Smav # If /usr is not mounted yet, we cannot use cmp, then 159198389Smav # the following test fails. In such case, we simply 160198389Smav # ignore an error and do update resolv.conf. 161249132Smav if cmp -s $tmpres /etc/resolv.conf; then 162198389Smav rm -f $tmpres 163198389Smav return 0 164198389Smav fi 2>/dev/null 165198389Smav 166198389Smav # In case (e.g. during OpenBSD installs) /etc/resolv.conf 167198389Smav # is a symbolic link, take care to preserve the link and write 168198389Smav # the new data in the correct location. 169198389Smav 170198389Smav if [ -f /etc/resolv.conf ]; then 171198389Smav cat /etc/resolv.conf > /etc/resolv.conf.save 172198389Smav fi 173198389Smav cat $tmpres > /etc/resolv.conf 174198389Smav rm -f $tmpres 175198389Smav 176198389Smav # Try to ensure correct ownership and permissions. 177198389Smav chown -RL root:wheel /etc/resolv.conf 178198389Smav chmod -RL 644 /etc/resolv.conf 179198389Smav 180198389Smav return 0 181198389Smav fi 182198389Smav 183198389Smav return 1 184198389Smav} 185198389Smav 186198389Smav# Must be used on exit. Invokes the local dhcp client exit hooks, if any. 187198389Smavexit_with_hooks() { 188198389Smav exit_status=$1 189198389Smav if [ -f /etc/dhclient-exit-hooks ]; then 190198389Smav . /etc/dhclient-exit-hooks 191198389Smav fi 192198389Smav # probably should do something with exit status of the local script 193198389Smav exit $exit_status 194198389Smav} 195200218Smav 196256215Smav# 197198389Smav# Start of active code. 198198389Smav# 199198389Smav 200198389Smav# Invoke the local dhcp client enter hooks, if they exist. 201198389Smavif [ -f /etc/dhclient-enter-hooks ]; then 202198389Smav exit_status=0 203198389Smav . /etc/dhclient-enter-hooks 204198389Smav # allow the local script to abort processing of this state 205198389Smav # local script must set exit_status variable to nonzero. 206198389Smav if [ $exit_status -ne 0 ]; then 207198389Smav exit $exit_status 208198389Smav fi 209198389Smavfi 210198389Smav 211198389Smavif [ -x $NETSTAT ]; then 212198389Smav if_defaultroute=`$NETSTAT -rnf inet | $AWK '{if ($1=="default") printf $6}'` 213198389Smavelse 214198389Smav if_defaultroute="x" 215198389Smavfi 216198389Smav 217256215Smavcase $reason in 218200218SmavMEDIUM) 219198389Smav eval "ifconfig $interface $medium" 220198389Smav eval "ifconfig $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1 221198389Smav sleep 1 222198389Smav ;; 223198389Smav 224198389SmavPREINIT) 225198389Smav delete_old_alias 226198389Smav ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up 227198389Smav ;; 228198389Smav 229198389SmavARPCHECK|ARPSEND) 230198389Smav ;; 231198389Smav 232198389SmavBOUND|RENEW|REBIND|REBOOT) 233198389Smav check_hostname 234198389Smav if [ -n "$old_ip_address" ]; then 235198389Smav if [ "$old_ip_address" != "$alias_ip_address" ]; then 236198389Smav delete_old_alias 237198389Smav fi 238198389Smav if [ "$old_ip_address" != "$new_ip_address" ]; then 239198389Smav delete_old_address 240198389Smav delete_old_routes 241198389Smav fi 242198389Smav fi 243200218Smav if [ "$reason" = BOUND ] || \ 244198389Smav [ "$reason" = REBOOT ] || \ 245198389Smav [ -z "$old_ip_address" ] || \ 246198389Smav [ "$old_ip_address" != "$new_ip_address" ]; then 247198389Smav add_new_address 248198389Smav add_new_routes 249198389Smav fi 250198389Smav if [ "$new_ip_address" != "$alias_ip_address" ]; then 251198389Smav add_new_alias 252198389Smav fi 253198389Smav add_new_resolv_conf 254198389Smav ;; 255198389Smav 256198389SmavEXPIRE|FAIL) 257198389Smav delete_old_alias 258198389Smav if [ -n "$old_ip_address" ]; then 259198389Smav delete_old_address 260198389Smav delete_old_routes 261198389Smav fi 262198389Smav if [ -x $ARP ]; then 263198389Smav $ARP -d -a -i $interface 264198389Smav fi 265198389Smav # XXX Why add alias we just deleted above? 266198389Smav add_new_alias 267198389Smav if [ -f /etc/resolv.conf.save ]; then 268198389Smav cat /etc/resolv.conf.save > /etc/resolv.conf 269198389Smav fi 270198389Smav ;; 271198389Smav 272198389SmavTIMEOUT) 273198389Smav delete_old_alias 274198389Smav add_new_address 275198389Smav sleep 1 276198389Smav if [ -n "$new_routers" ]; then 277198389Smav $LOGGER "New Routers ($interface): $new_routers" 278198389Smav set "$new_routers" 279198389Smav if ping -q -c 1 -t 1 "$1"; then 280198389Smav if [ "$new_ip_address" != "$alias_ip_address" ]; then 281198389Smav add_new_alias 282198389Smav fi 283198389Smav add_new_routes 284198389Smav if add_new_resolv_conf; then 285198389Smav exit_with_hooks 0 286198389Smav fi 287198389Smav fi 288198389Smav fi 289198389Smav eval "ifconfig $interface inet -alias $new_ip_address $medium" 290198389Smav delete_old_routes 291198389Smav exit_with_hooks 1 292198389Smav ;; 293198389Smavesac 294198389Smav 295198389Smavexit_with_hooks 0 296198389Smav