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