dhclient-script revision 149898
1189251Ssam#!/bin/sh
2189251Ssam#
3189251Ssam# $OpenBSD: dhclient-script,v 1.6 2004/05/06 18:22:41 claudio Exp $
4189251Ssam# $FreeBSD: head/sbin/dhclient/dhclient-script 149898 2005-09-08 22:49:17Z brooks $
5189251Ssam#
6189251Ssam# Copyright (c) 2003 Kenneth R Westerback <krw@openbsd.org>
7189251Ssam#
8189251Ssam# Permission to use, copy, modify, and distribute this software for any
9189251Ssam# purpose with or without fee is hereby granted, provided that the above
10189251Ssam# copyright notice and this permission notice appear in all copies.
11189251Ssam#
12189251Ssam# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13189251Ssam# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14189251Ssam# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15189251Ssam# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16189251Ssam# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17189251Ssam# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18189251Ssam# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19189251Ssam#
20189251Ssam#
21189251Ssam
22189251SsamNETSTAT=/usr/bin/netstat
23189251SsamAWK=/usr/bin/awk
24189251SsamHOSTNAME=/bin/hostname
25189251Ssam
26189251SsamLOCALHOST=127.0.0.1
27189251Ssam
28189251Ssamif [ -x /usr/bin/logger ]; then
29189251Ssam	LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
30189251Ssamelse
31189251Ssam	LOGGER=echo
32189251Ssamfi
33189251Ssam
34189251Ssam#
35189251Ssam# Helper functions that implement common actions.
36189251Ssam#
37189251Ssam
38189251Ssamcheck_hostname() {
39189251Ssam	current_hostname=`$HOSTNAME`
40189251Ssam	if [ -z "$current_hostname" ]; then
41189251Ssam		$LOGGER "New Hostname ($interface): $new_host_name"
42189251Ssam		$HOSTNAME $new_host_name
43189251Ssam	elif [ "$current_hostname" = "$old_host_name" -a \
44189251Ssam	       "$new_host_name" != "$old_host_name" ]; then
45189251Ssam		$LOGGER "New Hostname ($interface): $new_host_name"
46189251Ssam		$HOSTNAME $new_host_name
47189251Ssam	fi
48189251Ssam}
49189251Ssam
50189251Ssamarp_flush() {
51189251Ssam	arp -an -i $interface | \
52189251Ssam		sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \
53189251Ssam		sh >/dev/null 2>&1
54189251Ssam}
55189251Ssam
56189251Ssamdelete_old_address() {
57189251Ssam	eval "ifconfig $interface inet -alias $old_ip_address $medium"
58189251Ssam}
59189251Ssam
60189251Ssamadd_new_address() {
61189251Ssam	eval "ifconfig $interface \
62189251Ssam		inet $new_ip_address \
63189251Ssam		netmask $new_subnet_mask \
64189251Ssam		broadcast $new_broadcast_address \
65189251Ssam		$medium"
66189251Ssam
67189251Ssam	$LOGGER "New IP Address ($interface): $new_ip_address"
68189251Ssam	$LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
69189251Ssam	$LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
70189251Ssam	$LOGGER "New Routers ($interface): $new_routers"
71189251Ssam}
72189251Ssam
73189251Ssamdelete_old_alias() {
74189251Ssam	if [ -n "$alias_ip_address" ]; then
75189251Ssam		ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
76189251Ssam		#route delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
77189251Ssam	fi
78189251Ssam}
79214734Srpaulo
80189251Ssamadd_new_alias() {
81189251Ssam	if [ -n "$alias_ip_address" ]; then
82189251Ssam		ifconfig $interface inet alias $alias_ip_address netmask \
83189251Ssam		    $alias_subnet_mask
84189251Ssam		#route add $alias_ip_address $LOCALHOST
85189251Ssam	fi
86189251Ssam}
87189251Ssam
88189251Ssamdelete_old_routes() {
89189251Ssam	#route delete "$old_ip_address" $LOCALHOST >/dev/null 2>&1
90189251Ssam	for router in $old_routers; do
91189251Ssam		if [ $if_defaultroute = x -o $if_defaultroute = $interface ]; then
92189251Ssam			route delete default $route >/dev/null 2>&1
93189251Ssam		fi
94189251Ssam	done
95189251Ssam
96189251Ssam	if [ -n "$old_static_routes" ]; then
97189251Ssam		set $old_static_routes
98189251Ssam		while [ $# -gt 1 ]; do
99189251Ssam			route delete "$1" "$2"
100189251Ssam			shift; shift
101189251Ssam		done
102189251Ssam	fi
103189251Ssam
104189251Ssam	arp_flush
105189251Ssam}
106189251Ssam
107189251Ssamadd_new_routes() {
108189251Ssam	#route add $new_ip_address $LOCALHOST >/dev/null 2>&1
109189251Ssam	for router in $new_routers; do
110189251Ssam		if [ "$new_ip_address" = "$router" ]; then
111189251Ssam			route add default -iface $router >/dev/null 2>&1
112189251Ssam		else
113189251Ssam			route add default $router >/dev/null 2>&1
114189251Ssam		fi
115189251Ssam		# 2nd and subsequent default routers error out, so explicitly
116189251Ssam		# stop processing the list after the first one.
117189251Ssam		break
118189251Ssam	done
119189251Ssam
120189251Ssam	if [ -n "$new_static_routes" ]; then
121		$LOGGER "New Static Routes ($interface): $new_static_routes"
122		set $new_static_routes
123		while [ $# -gt 1 ]; do
124			route add $1 $2
125			shift; shift
126		done
127	fi
128}
129
130add_new_resolv_conf() {
131	# XXX Old code did not create/update resolv.conf unless both
132	# $new_domain_name and $new_domain_name_servers were provided.  PR
133	# #3135 reported some ISP's only provide $new_domain_name_servers and
134	# thus broke the script. This code creates the resolv.conf if either
135	# are provided.
136
137	rm -f /etc/resolv.conf.std
138
139	if [ -n "$new_domain_name" ]; then
140		echo "search $new_domain_name" >>/etc/resolv.conf.std
141	fi
142
143	if [ -n "$new_domain_name_servers" ]; then
144		for nameserver in $new_domain_name_servers; do
145			echo "nameserver $nameserver" >>/etc/resolv.conf.std
146		done
147	fi
148
149	if [ -f /etc/resolv.conf.std ]; then
150		if [ -f /etc/resolv.conf.tail ]; then
151			cat /etc/resolv.conf.tail >>/etc/resolv.conf.std
152		fi
153
154		# When resolv.conf is not changed actually, we don't
155		# need to update it.
156		# If /usr is not mounted yet, we cannot use cmp, then
157		# the following test fails.  In such case, we simply
158		# ignore an error and do update resolv.conf.
159		if cmp -s /etc/resolv.conf.std /etc/resolv.conf; then
160			rm -f /etc/resolv.conf.std
161			return 0
162		fi 2>/dev/null
163
164		# In case (e.g. during OpenBSD installs) /etc/resolv.conf
165		# is a symbolic link, take care to preserve the link and write
166		# the new data in the correct location.
167
168		if [ -f /etc/resolv.conf ]; then
169			cat /etc/resolv.conf > /etc/resolv.conf.save
170		fi
171		cat /etc/resolv.conf.std > /etc/resolv.conf
172		rm -f /etc/resolv.conf.std
173
174		# Try to ensure correct ownership and permissions.
175		chown -RL root:wheel /etc/resolv.conf
176		chmod -RL 644 /etc/resolv.conf
177
178		return 0
179	fi
180
181	return 1
182}
183
184# Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
185exit_with_hooks() {
186	exit_status=$1
187	if [ -f /etc/dhclient-exit-hooks ]; then
188		. /etc/dhclient-exit-hooks
189	fi
190	# probably should do something with exit status of the local script
191	exit $exit_status
192}
193
194#
195# Start of active code.
196#
197
198# Invoke the local dhcp client enter hooks, if they exist.
199if [ -f /etc/dhclient-enter-hooks ]; then
200	exit_status=0
201	. /etc/dhclient-enter-hooks
202	# allow the local script to abort processing of this state
203	# local script must set exit_status variable to nonzero.
204	if [ $exit_status -ne 0 ]; then
205		exit $exit_status
206	fi
207fi
208
209if [ -x $NETSTAT ]; then
210	if_defaultroute=`$NETSTAT -rnf inet | $AWK '{if ($1=="default") printf $6}'`
211else
212	if_defaultroute="x"
213fi
214
215case $reason in
216MEDIUM)
217	eval "ifconfig $interface $medium"
218	eval "ifconfig $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
219	sleep 1
220	;;
221
222PREINIT)
223	delete_old_alias
224	ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
225	;;
226
227ARPCHECK|ARPSEND)
228	;;
229
230BOUND|RENEW|REBIND|REBOOT)
231	check_hostname
232	if [ -n "$old_ip_address" ]; then
233		if [ "$old_ip_address" != "$alias_ip_address" ]; then
234			delete_old_alias
235		fi
236		if [ "$old_ip_address" != "$new_ip_address" ]; then
237			delete_old_address
238			delete_old_routes
239		fi
240	fi
241	if [ "$reason" = BOUND ] || \
242	   [ "$reason" = REBOOT ] || \
243	   [ -z "$old_ip_address" ] || \
244	   [ "$old_ip_address" != "$new_ip_address" ]; then
245		add_new_address
246		add_new_routes
247	fi
248	if [ "$new_ip_address" != "$alias_ip_address" ]; then
249		add_new_alias
250	fi
251	add_new_resolv_conf
252	;;
253
254EXPIRE|FAIL)
255	delete_old_alias
256	if [ -n "$old_ip_address" ]; then
257		delete_old_address
258		delete_old_routes
259	fi
260	ifconfig $interface delete
261	# XXX Why add alias we just deleted above?
262	add_new_alias
263	if [ -f /etc/resolv.conf.save ]; then
264		cat /etc/resolv.conf.save > /etc/resolv.conf
265	fi
266	;;
267
268TIMEOUT)
269	delete_old_alias
270	add_new_address
271	sleep 1
272	if [ -n "$new_routers" ]; then
273		$LOGGER "New Routers ($interface): $new_routers"
274		set "$new_routers"
275		if ping -q -c 1 -w 1 "$1"; then
276			if [ "$new_ip_address" != "$alias_ip_address" ]; then
277				add_new_alias
278			fi
279			add_new_routes
280			if add_new_resolv_conf; then
281				exit_with_hooks 0
282			fi
283		fi
284	fi
285	eval "ifconfig $interface inet -alias $new_ip_address $medium"
286	delete_old_routes
287	exit_with_hooks 1
288	;;
289esac
290
291exit_with_hooks 0
292