dhclient-script revision 171187
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 171187 2007-07-03 17:49:32Z thompsa $
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
25171187SthompsaIFCONFIG='/sbin/ifconfig -n'
26154164SbrooksNETSTAT=/usr/bin/netstat
27147086Sbrooks
28147086SbrooksLOCALHOST=127.0.0.1
29147086Sbrooks
30147086Sbrooksif [ -x /usr/bin/logger ]; then
31147086Sbrooks	LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
32147086Sbrookselse
33147086Sbrooks	LOGGER=echo
34147086Sbrooksfi
35147086Sbrooks
36147072Sbrooks#
37147072Sbrooks# Helper functions that implement common actions.
38147072Sbrooks#
39147072Sbrooks
40147086Sbrookscheck_hostname() {
41147086Sbrooks	current_hostname=`$HOSTNAME`
42147086Sbrooks	if [ -z "$current_hostname" ]; then
43147086Sbrooks		$LOGGER "New Hostname ($interface): $new_host_name"
44147086Sbrooks		$HOSTNAME $new_host_name
45147086Sbrooks	elif [ "$current_hostname" = "$old_host_name" -a \
46147086Sbrooks	       "$new_host_name" != "$old_host_name" ]; then
47147086Sbrooks		$LOGGER "New Hostname ($interface): $new_host_name"
48147086Sbrooks		$HOSTNAME $new_host_name
49147072Sbrooks	fi
50147072Sbrooks}
51147072Sbrooks
52147086Sbrooksarp_flush() {
53147086Sbrooks	arp -an -i $interface | \
54147086Sbrooks		sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \
55147086Sbrooks		sh >/dev/null 2>&1
56147086Sbrooks}
57147086Sbrooks
58147086Sbrooksdelete_old_address() {
59171187Sthompsa	eval "$IFCONFIG $interface inet -alias $old_ip_address $medium"
60147086Sbrooks}
61147086Sbrooks
62147072Sbrooksadd_new_address() {
63171187Sthompsa	eval "$IFCONFIG $interface \
64147072Sbrooks		inet $new_ip_address \
65147072Sbrooks		netmask $new_subnet_mask \
66147072Sbrooks		broadcast $new_broadcast_address \
67149519Sbrooks		$medium"
68147072Sbrooks
69147086Sbrooks	$LOGGER "New IP Address ($interface): $new_ip_address"
70147086Sbrooks	$LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
71147086Sbrooks	$LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
72147086Sbrooks	$LOGGER "New Routers ($interface): $new_routers"
73147072Sbrooks}
74147072Sbrooks
75147072Sbrooksdelete_old_alias() {
76147072Sbrooks	if [ -n "$alias_ip_address" ]; then
77171187Sthompsa		$IFCONFIG $interface inet -alias $alias_ip_address > /dev/null 2>&1
78149479Sbrooks		#route delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
79147072Sbrooks	fi
80147072Sbrooks}
81147072Sbrooks
82147072Sbrooksadd_new_alias() {
83147072Sbrooks	if [ -n "$alias_ip_address" ]; then
84171187Sthompsa		$IFCONFIG $interface inet alias $alias_ip_address netmask \
85147072Sbrooks		    $alias_subnet_mask
86149479Sbrooks		#route add $alias_ip_address $LOCALHOST
87147072Sbrooks	fi
88147072Sbrooks}
89147072Sbrooks
90166602Semastefill_classless_routes() {
91166602Semaste	set $1
92168689Semaste	while [ $# -ge 5 ]; do
93166602Semaste		if [ $1 -eq 0 ]; then
94166602Semaste			route="default"
95166602Semaste		elif [ $1 -le 8 ]; then
96166602Semaste			route="$2.0.0.0/$1"
97166602Semaste			shift
98166602Semaste		elif [ $1 -le 16 ]; then
99166602Semaste			route="$2.$3.0.0/$1"
100166602Semaste			shift; shift
101166602Semaste		elif [ $1 -le 24 ]; then
102166602Semaste			route="$2.$3.$4.0/$1"
103166602Semaste			shift; shift; shift
104166602Semaste		else
105166602Semaste			route="$2.$3.$4.$5/$1"
106166602Semaste			shift; shift; shift; shift
107166602Semaste		fi
108166602Semaste		shift
109166602Semaste		router="$1.$2.$3.$4"
110166602Semaste		classless_routes="$classless_routes $route $router"
111166602Semaste		shift; shift; shift; shift
112166602Semaste	done
113166602Semaste}
114166602Semaste
115147072Sbrooksdelete_old_routes() {
116149479Sbrooks	#route delete "$old_ip_address" $LOCALHOST >/dev/null 2>&1
117166602Semaste	if [ -n "$old_classless_routes" ]; then
118166602Semaste		fill_classless_routes "$old_classless_routes"
119166602Semaste		set $classless_routes
120166602Semaste		while [ $# -gt 1 ]; do
121166602Semaste			route delete "$1" "$2"
122166602Semaste			shift; shift
123166602Semaste		done
124166602Semaste		return 0;
125166602Semaste	fi
126166602Semaste
127147086Sbrooks	for router in $old_routers; do
128147086Sbrooks		if [ $if_defaultroute = x -o $if_defaultroute = $interface ]; then
129147086Sbrooks			route delete default $route >/dev/null 2>&1
130147086Sbrooks		fi
131147086Sbrooks	done
132147072Sbrooks
133147072Sbrooks	if [ -n "$old_static_routes" ]; then
134147072Sbrooks		set $old_static_routes
135147072Sbrooks		while [ $# -gt 1 ]; do
136147072Sbrooks			route delete "$1" "$2"
137147072Sbrooks			shift; shift
138147072Sbrooks		done
139147072Sbrooks	fi
140147072Sbrooks
141147086Sbrooks	arp_flush
142147072Sbrooks}
143147072Sbrooks
144147072Sbrooksadd_new_routes() {
145149479Sbrooks	#route add $new_ip_address $LOCALHOST >/dev/null 2>&1
146166602Semaste
147166602Semaste	# RFC 3442: If the DHCP server returns both a Classless Static
148166602Semaste	# Routes option and a Router option, the DHCP client MUST ignore
149166602Semaste	# the Router option.
150166602Semaste	#
151166602Semaste	# DHCP clients that support this option (Classless Static Routes)
152166602Semaste	# MUST NOT install the routes specified in the Static Routes
153166602Semaste	# option (option code 33) if both a Static Routes option and the
154166602Semaste	# Classless Static Routes option are provided.
155166602Semaste
156166602Semaste	if [ -n "$new_classless_routes" ]; then
157166602Semaste		fill_classless_routes "$new_classless_routes"
158166602Semaste		$LOGGER "New Classless Static Routes ($interface): $classless_routes"
159166602Semaste		set $classless_routes
160166602Semaste		while [ $# -gt 1 ]; do
161166602Semaste			if [ "0.0.0.0" = "$2" ]; then
162166602Semaste				route add "$1" -iface "$interface"
163166602Semaste			else
164166602Semaste				route add "$1" "$2"
165166602Semaste			fi
166166602Semaste			shift; shift
167166602Semaste		done
168166602Semaste		return
169166602Semaste	fi
170166602Semaste
171147072Sbrooks	for router in $new_routers; do
172147072Sbrooks		if [ "$new_ip_address" = "$router" ]; then
173147072Sbrooks			route add default -iface $router >/dev/null 2>&1
174147072Sbrooks		else
175147072Sbrooks			route add default $router >/dev/null 2>&1
176147072Sbrooks		fi
177147072Sbrooks		# 2nd and subsequent default routers error out, so explicitly
178147072Sbrooks		# stop processing the list after the first one.
179147072Sbrooks		break
180147072Sbrooks	done
181147072Sbrooks
182147072Sbrooks	if [ -n "$new_static_routes" ]; then
183147086Sbrooks		$LOGGER "New Static Routes ($interface): $new_static_routes"
184147072Sbrooks		set $new_static_routes
185147072Sbrooks		while [ $# -gt 1 ]; do
186147072Sbrooks			route add $1 $2
187147072Sbrooks			shift; shift
188147072Sbrooks		done
189147072Sbrooks	fi
190147072Sbrooks}
191147072Sbrooks
192147072Sbrooksadd_new_resolv_conf() {
193147072Sbrooks	# XXX Old code did not create/update resolv.conf unless both
194147072Sbrooks	# $new_domain_name and $new_domain_name_servers were provided.  PR
195147072Sbrooks	# #3135 reported some ISP's only provide $new_domain_name_servers and
196147072Sbrooks	# thus broke the script. This code creates the resolv.conf if either
197147072Sbrooks	# are provided.
198147072Sbrooks
199154869Sbrooks	local tmpres=/var/run/resolv.conf.${interface}
200154702Swes	rm -f $tmpres
201147072Sbrooks
202147072Sbrooks	if [ -n "$new_domain_name" ]; then
203154702Swes		echo "search $new_domain_name" >>$tmpres
204147072Sbrooks	fi
205147072Sbrooks
206147072Sbrooks	if [ -n "$new_domain_name_servers" ]; then
207147072Sbrooks		for nameserver in $new_domain_name_servers; do
208154702Swes			echo "nameserver $nameserver" >>$tmpres
209147072Sbrooks		done
210147072Sbrooks	fi
211147072Sbrooks
212154702Swes	if [ -f $tmpres ]; then
213147072Sbrooks		if [ -f /etc/resolv.conf.tail ]; then
214154702Swes			cat /etc/resolv.conf.tail >>$tmpres
215147072Sbrooks		fi
216147072Sbrooks
217149898Sbrooks		# When resolv.conf is not changed actually, we don't
218149898Sbrooks		# need to update it.
219149898Sbrooks		# If /usr is not mounted yet, we cannot use cmp, then
220149898Sbrooks		# the following test fails.  In such case, we simply
221149898Sbrooks		# ignore an error and do update resolv.conf.
222154702Swes		if cmp -s $tmpres /etc/resolv.conf; then
223154702Swes			rm -f $tmpres
224149898Sbrooks			return 0
225149898Sbrooks		fi 2>/dev/null
226149898Sbrooks
227147072Sbrooks		# In case (e.g. during OpenBSD installs) /etc/resolv.conf
228147072Sbrooks		# is a symbolic link, take care to preserve the link and write
229147072Sbrooks		# the new data in the correct location.
230147072Sbrooks
231147072Sbrooks		if [ -f /etc/resolv.conf ]; then
232147072Sbrooks			cat /etc/resolv.conf > /etc/resolv.conf.save
233147072Sbrooks		fi
234154702Swes		cat $tmpres > /etc/resolv.conf
235154702Swes		rm -f $tmpres
236147072Sbrooks
237147072Sbrooks		# Try to ensure correct ownership and permissions.
238147072Sbrooks		chown -RL root:wheel /etc/resolv.conf
239147072Sbrooks		chmod -RL 644 /etc/resolv.conf
240147072Sbrooks
241147072Sbrooks		return 0
242147072Sbrooks	fi
243147072Sbrooks
244147072Sbrooks	return 1
245147072Sbrooks}
246147072Sbrooks
247147138Sbrooks# Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
248147138Sbrooksexit_with_hooks() {
249147138Sbrooks	exit_status=$1
250147138Sbrooks	if [ -f /etc/dhclient-exit-hooks ]; then
251147138Sbrooks		. /etc/dhclient-exit-hooks
252147138Sbrooks	fi
253147138Sbrooks	# probably should do something with exit status of the local script
254147138Sbrooks	exit $exit_status
255147138Sbrooks}
256147138Sbrooks
257147072Sbrooks#
258147072Sbrooks# Start of active code.
259147072Sbrooks#
260147072Sbrooks
261147218Sbrooks# Invoke the local dhcp client enter hooks, if they exist.
262147218Sbrooksif [ -f /etc/dhclient-enter-hooks ]; then
263147218Sbrooks	exit_status=0
264147218Sbrooks	. /etc/dhclient-enter-hooks
265147218Sbrooks	# allow the local script to abort processing of this state
266147218Sbrooks	# local script must set exit_status variable to nonzero.
267147218Sbrooks	if [ $exit_status -ne 0 ]; then
268147218Sbrooks		exit $exit_status
269147218Sbrooks	fi
270147218Sbrooksfi
271147218Sbrooks
272147086Sbrooksif [ -x $NETSTAT ]; then
273149480Sbrooks	if_defaultroute=`$NETSTAT -rnf inet | $AWK '{if ($1=="default") printf $6}'`
274147086Sbrookselse
275147086Sbrooks	if_defaultroute="x"
276147072Sbrooksfi
277147072Sbrooks
278147072Sbrookscase $reason in
279147072SbrooksMEDIUM)
280171187Sthompsa	eval "$IFCONFIG $interface $medium"
281171187Sthompsa	eval "$IFCONFIG $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
282147072Sbrooks	sleep 1
283147072Sbrooks	;;
284147072Sbrooks
285147072SbrooksPREINIT)
286147072Sbrooks	delete_old_alias
287171187Sthompsa	$IFCONFIG $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
288147072Sbrooks	;;
289147072Sbrooks
290147072SbrooksARPCHECK|ARPSEND)
291147072Sbrooks	;;
292147072Sbrooks
293147072SbrooksBOUND|RENEW|REBIND|REBOOT)
294147086Sbrooks	check_hostname
295147072Sbrooks	if [ -n "$old_ip_address" ]; then
296147072Sbrooks		if [ "$old_ip_address" != "$alias_ip_address" ]; then
297147072Sbrooks			delete_old_alias
298147072Sbrooks		fi
299147072Sbrooks		if [ "$old_ip_address" != "$new_ip_address" ]; then
300147072Sbrooks			delete_old_address
301147072Sbrooks			delete_old_routes
302147072Sbrooks		fi
303147072Sbrooks	fi
304147072Sbrooks	if [ "$reason" = BOUND ] || \
305147072Sbrooks	   [ "$reason" = REBOOT ] || \
306147072Sbrooks	   [ -z "$old_ip_address" ] || \
307147072Sbrooks	   [ "$old_ip_address" != "$new_ip_address" ]; then
308147072Sbrooks		add_new_address
309147072Sbrooks		add_new_routes
310147072Sbrooks	fi
311147072Sbrooks	if [ "$new_ip_address" != "$alias_ip_address" ]; then
312147072Sbrooks		add_new_alias
313147072Sbrooks	fi
314147072Sbrooks	add_new_resolv_conf
315147072Sbrooks	;;
316147072Sbrooks
317147072SbrooksEXPIRE|FAIL)
318147072Sbrooks	delete_old_alias
319147072Sbrooks	if [ -n "$old_ip_address" ]; then
320147072Sbrooks		delete_old_address
321147072Sbrooks		delete_old_routes
322147072Sbrooks	fi
323154164Sbrooks	if [ -x $ARP ]; then
324154164Sbrooks		$ARP -d -a -i $interface
325154164Sbrooks	fi
326147072Sbrooks	# XXX Why add alias we just deleted above?
327147072Sbrooks	add_new_alias
328147072Sbrooks	if [ -f /etc/resolv.conf.save ]; then
329147072Sbrooks		cat /etc/resolv.conf.save > /etc/resolv.conf
330147072Sbrooks	fi
331147072Sbrooks	;;
332147072Sbrooks
333147072SbrooksTIMEOUT)
334147072Sbrooks	delete_old_alias
335147072Sbrooks	add_new_address
336147072Sbrooks	sleep 1
337147072Sbrooks	if [ -n "$new_routers" ]; then
338147086Sbrooks		$LOGGER "New Routers ($interface): $new_routers"
339147072Sbrooks		set "$new_routers"
340154760Sbrooks		if ping -q -c 1 -t 1 "$1"; then
341147072Sbrooks			if [ "$new_ip_address" != "$alias_ip_address" ]; then
342147072Sbrooks				add_new_alias
343147072Sbrooks			fi
344147072Sbrooks			add_new_routes
345147072Sbrooks			if add_new_resolv_conf; then
346147138Sbrooks				exit_with_hooks 0
347147072Sbrooks			fi
348147072Sbrooks		fi
349147072Sbrooks	fi
350171187Sthompsa	eval "$IFCONFIG $interface inet -alias $new_ip_address $medium"
351147072Sbrooks	delete_old_routes
352147138Sbrooks	exit_with_hooks 1
353147072Sbrooks	;;
354147072Sbrooksesac
355147072Sbrooks
356147138Sbrooksexit_with_hooks 0
357