dhclient-script revision 149480
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 149480 2005-08-26 01:07:51Z brooks $
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
22NETSTAT=/usr/bin/netstat
23AWK=/usr/bin/awk
24HOSTNAME=/bin/hostname
25
26LOCALHOST=127.0.0.1
27
28if [ -x /usr/bin/logger ]; then
29	LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
30else
31	LOGGER=echo
32fi
33
34#
35# Helper functions that implement common actions.
36#
37
38check_hostname() {
39	current_hostname=`$HOSTNAME`
40	if [ -z "$current_hostname" ]; then
41		$LOGGER "New Hostname ($interface): $new_host_name"
42		$HOSTNAME $new_host_name
43	elif [ "$current_hostname" = "$old_host_name" -a \
44	       "$new_host_name" != "$old_host_name" ]; then
45		$LOGGER "New Hostname ($interface): $new_host_name"
46		$HOSTNAME $new_host_name
47	fi
48}
49
50arp_flush() {
51	arp -an -i $interface | \
52		sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \
53		sh >/dev/null 2>&1
54}
55
56delete_old_address() {
57	ifconfig $interface inet -alias $old_ip_address $medium
58}
59
60add_new_address() {
61	ifconfig $interface \
62		inet $new_ip_address \
63		netmask $new_subnet_mask \
64		broadcast $new_broadcast_address \
65		$medium
66
67	$LOGGER "New IP Address ($interface): $new_ip_address"
68	$LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
69	$LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
70	$LOGGER "New Routers ($interface): $new_routers"
71}
72
73delete_old_alias() {
74	if [ -n "$alias_ip_address" ]; then
75		ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
76		#route delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
77	fi
78}
79
80add_new_alias() {
81	if [ -n "$alias_ip_address" ]; then
82		ifconfig $interface inet alias $alias_ip_address netmask \
83		    $alias_subnet_mask
84		#route add $alias_ip_address $LOCALHOST
85	fi
86}
87
88delete_old_routes() {
89	#route delete "$old_ip_address" $LOCALHOST >/dev/null 2>&1
90	for router in $old_routers; do
91		if [ $if_defaultroute = x -o $if_defaultroute = $interface ]; then
92			route delete default $route >/dev/null 2>&1
93		fi
94	done
95
96	if [ -n "$old_static_routes" ]; then
97		set $old_static_routes
98		while [ $# -gt 1 ]; do
99			route delete "$1" "$2"
100			shift; shift
101		done
102	fi
103
104	arp_flush
105}
106
107add_new_routes() {
108	#route add $new_ip_address $LOCALHOST >/dev/null 2>&1
109	for router in $new_routers; do
110		if [ "$new_ip_address" = "$router" ]; then
111			route add default -iface $router >/dev/null 2>&1
112		else
113			route add default $router >/dev/null 2>&1
114		fi
115		# 2nd and subsequent default routers error out, so explicitly
116		# stop processing the list after the first one.
117		break
118	done
119
120	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		# In case (e.g. during OpenBSD installs) /etc/resolv.conf
155		# is a symbolic link, take care to preserve the link and write
156		# the new data in the correct location.
157
158		if [ -f /etc/resolv.conf ]; then
159			cat /etc/resolv.conf > /etc/resolv.conf.save
160		fi
161		cat /etc/resolv.conf.std > /etc/resolv.conf
162		rm -f /etc/resolv.conf.std
163
164		# Try to ensure correct ownership and permissions.
165		chown -RL root:wheel /etc/resolv.conf
166		chmod -RL 644 /etc/resolv.conf
167
168		return 0
169	fi
170
171	return 1
172}
173
174# Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
175exit_with_hooks() {
176	exit_status=$1
177	if [ -f /etc/dhclient-exit-hooks ]; then
178		. /etc/dhclient-exit-hooks
179	fi
180	# probably should do something with exit status of the local script
181	exit $exit_status
182}
183
184#
185# Start of active code.
186#
187
188# Invoke the local dhcp client enter hooks, if they exist.
189if [ -f /etc/dhclient-enter-hooks ]; then
190	exit_status=0
191	. /etc/dhclient-enter-hooks
192	# allow the local script to abort processing of this state
193	# local script must set exit_status variable to nonzero.
194	if [ $exit_status -ne 0 ]; then
195		exit $exit_status
196	fi
197fi
198
199if [ -x $NETSTAT ]; then
200	if_defaultroute=`$NETSTAT -rnf inet | $AWK '{if ($1=="default") printf $6}'`
201else
202	if_defaultroute="x"
203fi
204
205case $reason in
206MEDIUM)
207	ifconfig $interface $medium
208	ifconfig $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
209	sleep 1
210	;;
211
212PREINIT)
213	delete_old_alias
214	ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
215	;;
216
217ARPCHECK|ARPSEND)
218	;;
219
220BOUND|RENEW|REBIND|REBOOT)
221	check_hostname
222	if [ -n "$old_ip_address" ]; then
223		if [ "$old_ip_address" != "$alias_ip_address" ]; then
224			delete_old_alias
225		fi
226		if [ "$old_ip_address" != "$new_ip_address" ]; then
227			delete_old_address
228			delete_old_routes
229		fi
230	fi
231	if [ "$reason" = BOUND ] || \
232	   [ "$reason" = REBOOT ] || \
233	   [ -z "$old_ip_address" ] || \
234	   [ "$old_ip_address" != "$new_ip_address" ]; then
235		add_new_address
236		add_new_routes
237	fi
238	if [ "$new_ip_address" != "$alias_ip_address" ]; then
239		add_new_alias
240	fi
241	add_new_resolv_conf
242	;;
243
244EXPIRE|FAIL)
245	delete_old_alias
246	if [ -n "$old_ip_address" ]; then
247		delete_old_address
248		delete_old_routes
249	fi
250	# XXX Why add alias we just deleted above?
251	add_new_alias
252	if [ -f /etc/resolv.conf.save ]; then
253		cat /etc/resolv.conf.save > /etc/resolv.conf
254	fi
255	;;
256
257TIMEOUT)
258	delete_old_alias
259	add_new_address
260	sleep 1
261	if [ -n "$new_routers" ]; then
262		$LOGGER "New Routers ($interface): $new_routers"
263		set "$new_routers"
264		if ping -q -c 1 -w 1 "$1"; then
265			if [ "$new_ip_address" != "$alias_ip_address" ]; then
266				add_new_alias
267			fi
268			add_new_routes
269			if add_new_resolv_conf; then
270				exit_with_hooks 0
271			fi
272		fi
273	fi
274	ifconfig $interface inet -alias $new_ip_address $medium
275	delete_old_routes
276	exit_with_hooks 1
277	;;
278esac
279
280exit_with_hooks 0
281