dhclient-script revision 177730
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 177730 2008-03-30 02:42:39Z brooks $
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
23147086SbrooksHOSTNAME=/bin/hostname
24171187SthompsaIFCONFIG='/sbin/ifconfig -n'
25147086Sbrooks
26147086SbrooksLOCALHOST=127.0.0.1
27147086Sbrooks
28147086Sbrooksif [ -x /usr/bin/logger ]; then
29147086Sbrooks	LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
30147086Sbrookselse
31147086Sbrooks	LOGGER=echo
32147086Sbrooksfi
33147086Sbrooks
34147072Sbrooks#
35147072Sbrooks# Helper functions that implement common actions.
36147072Sbrooks#
37147072Sbrooks
38147086Sbrookscheck_hostname() {
39147086Sbrooks	current_hostname=`$HOSTNAME`
40147086Sbrooks	if [ -z "$current_hostname" ]; then
41147086Sbrooks		$LOGGER "New Hostname ($interface): $new_host_name"
42147086Sbrooks		$HOSTNAME $new_host_name
43147086Sbrooks	elif [ "$current_hostname" = "$old_host_name" -a \
44147086Sbrooks	       "$new_host_name" != "$old_host_name" ]; then
45147086Sbrooks		$LOGGER "New Hostname ($interface): $new_host_name"
46147086Sbrooks		$HOSTNAME $new_host_name
47147072Sbrooks	fi
48147072Sbrooks}
49147072Sbrooks
50147086Sbrooksarp_flush() {
51147086Sbrooks	arp -an -i $interface | \
52147086Sbrooks		sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \
53147086Sbrooks		sh >/dev/null 2>&1
54147086Sbrooks}
55147086Sbrooks
56147086Sbrooksdelete_old_address() {
57171187Sthompsa	eval "$IFCONFIG $interface inet -alias $old_ip_address $medium"
58147086Sbrooks}
59147086Sbrooks
60147072Sbrooksadd_new_address() {
61171187Sthompsa	eval "$IFCONFIG $interface \
62147072Sbrooks		inet $new_ip_address \
63147072Sbrooks		netmask $new_subnet_mask \
64147072Sbrooks		broadcast $new_broadcast_address \
65149519Sbrooks		$medium"
66147072Sbrooks
67147086Sbrooks	$LOGGER "New IP Address ($interface): $new_ip_address"
68147086Sbrooks	$LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
69147086Sbrooks	$LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
70147086Sbrooks	$LOGGER "New Routers ($interface): $new_routers"
71147072Sbrooks}
72147072Sbrooks
73147072Sbrooksdelete_old_alias() {
74147072Sbrooks	if [ -n "$alias_ip_address" ]; then
75171187Sthompsa		$IFCONFIG $interface inet -alias $alias_ip_address > /dev/null 2>&1
76149479Sbrooks		#route delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
77147072Sbrooks	fi
78147072Sbrooks}
79147072Sbrooks
80147072Sbrooksadd_new_alias() {
81147072Sbrooks	if [ -n "$alias_ip_address" ]; then
82171187Sthompsa		$IFCONFIG $interface inet alias $alias_ip_address netmask \
83147072Sbrooks		    $alias_subnet_mask
84149479Sbrooks		#route add $alias_ip_address $LOCALHOST
85147072Sbrooks	fi
86147072Sbrooks}
87147072Sbrooks
88166602Semastefill_classless_routes() {
89166602Semaste	set $1
90168689Semaste	while [ $# -ge 5 ]; do
91166602Semaste		if [ $1 -eq 0 ]; then
92166602Semaste			route="default"
93166602Semaste		elif [ $1 -le 8 ]; then
94166602Semaste			route="$2.0.0.0/$1"
95166602Semaste			shift
96166602Semaste		elif [ $1 -le 16 ]; then
97166602Semaste			route="$2.$3.0.0/$1"
98166602Semaste			shift; shift
99166602Semaste		elif [ $1 -le 24 ]; then
100166602Semaste			route="$2.$3.$4.0/$1"
101166602Semaste			shift; shift; shift
102166602Semaste		else
103166602Semaste			route="$2.$3.$4.$5/$1"
104166602Semaste			shift; shift; shift; shift
105166602Semaste		fi
106166602Semaste		shift
107166602Semaste		router="$1.$2.$3.$4"
108166602Semaste		classless_routes="$classless_routes $route $router"
109166602Semaste		shift; shift; shift; shift
110166602Semaste	done
111166602Semaste}
112166602Semaste
113147072Sbrooksdelete_old_routes() {
114149479Sbrooks	#route delete "$old_ip_address" $LOCALHOST >/dev/null 2>&1
115166602Semaste	if [ -n "$old_classless_routes" ]; then
116166602Semaste		fill_classless_routes "$old_classless_routes"
117166602Semaste		set $classless_routes
118166602Semaste		while [ $# -gt 1 ]; do
119166602Semaste			route delete "$1" "$2"
120166602Semaste			shift; shift
121166602Semaste		done
122166602Semaste		return 0;
123166602Semaste	fi
124166602Semaste
125177730Sbrooks	# If we supported multiple default routes, we'd be removing each
126177730Sbrooks	# one here.  We don't so just delete the default route if it's
127177730Sbrooks	# through our interface.
128177730Sbrooks	if is_default_interface; then
129177730Sbrooks		route delete default >/dev/null 2>&1
130177730Sbrooks	fi
131147072Sbrooks
132147072Sbrooks	if [ -n "$old_static_routes" ]; then
133147072Sbrooks		set $old_static_routes
134147072Sbrooks		while [ $# -gt 1 ]; do
135147072Sbrooks			route delete "$1" "$2"
136147072Sbrooks			shift; shift
137147072Sbrooks		done
138147072Sbrooks	fi
139147072Sbrooks
140147086Sbrooks	arp_flush
141147072Sbrooks}
142147072Sbrooks
143147072Sbrooksadd_new_routes() {
144149479Sbrooks	#route add $new_ip_address $LOCALHOST >/dev/null 2>&1
145166602Semaste
146166602Semaste	# RFC 3442: If the DHCP server returns both a Classless Static
147166602Semaste	# Routes option and a Router option, the DHCP client MUST ignore
148166602Semaste	# the Router option.
149166602Semaste	#
150166602Semaste	# DHCP clients that support this option (Classless Static Routes)
151166602Semaste	# MUST NOT install the routes specified in the Static Routes
152166602Semaste	# option (option code 33) if both a Static Routes option and the
153166602Semaste	# Classless Static Routes option are provided.
154166602Semaste
155166602Semaste	if [ -n "$new_classless_routes" ]; then
156166602Semaste		fill_classless_routes "$new_classless_routes"
157166602Semaste		$LOGGER "New Classless Static Routes ($interface): $classless_routes"
158166602Semaste		set $classless_routes
159166602Semaste		while [ $# -gt 1 ]; do
160166602Semaste			if [ "0.0.0.0" = "$2" ]; then
161166602Semaste				route add "$1" -iface "$interface"
162166602Semaste			else
163166602Semaste				route add "$1" "$2"
164166602Semaste			fi
165166602Semaste			shift; shift
166166602Semaste		done
167166602Semaste		return
168166602Semaste	fi
169166602Semaste
170147072Sbrooks	for router in $new_routers; do
171177730Sbrooks		if is_default_interface; then
172177730Sbrooks
173177730Sbrooks			if [ "$new_ip_address" = "$router" ]; then
174177730Sbrooks				route add default -iface $router >/dev/null 2>&1
175177730Sbrooks			else
176177730Sbrooks				route add default $router >/dev/null 2>&1
177177730Sbrooks			fi
178147072Sbrooks		fi
179147072Sbrooks		# 2nd and subsequent default routers error out, so explicitly
180147072Sbrooks		# stop processing the list after the first one.
181147072Sbrooks		break
182147072Sbrooks	done
183147072Sbrooks
184147072Sbrooks	if [ -n "$new_static_routes" ]; then
185147086Sbrooks		$LOGGER "New Static Routes ($interface): $new_static_routes"
186147072Sbrooks		set $new_static_routes
187147072Sbrooks		while [ $# -gt 1 ]; do
188147072Sbrooks			route add $1 $2
189147072Sbrooks			shift; shift
190147072Sbrooks		done
191147072Sbrooks	fi
192147072Sbrooks}
193147072Sbrooks
194147072Sbrooksadd_new_resolv_conf() {
195147072Sbrooks	# XXX Old code did not create/update resolv.conf unless both
196147072Sbrooks	# $new_domain_name and $new_domain_name_servers were provided.  PR
197147072Sbrooks	# #3135 reported some ISP's only provide $new_domain_name_servers and
198147072Sbrooks	# thus broke the script. This code creates the resolv.conf if either
199147072Sbrooks	# are provided.
200147072Sbrooks
201154869Sbrooks	local tmpres=/var/run/resolv.conf.${interface}
202154702Swes	rm -f $tmpres
203147072Sbrooks
204147072Sbrooks	if [ -n "$new_domain_name" ]; then
205154702Swes		echo "search $new_domain_name" >>$tmpres
206147072Sbrooks	fi
207147072Sbrooks
208147072Sbrooks	if [ -n "$new_domain_name_servers" ]; then
209147072Sbrooks		for nameserver in $new_domain_name_servers; do
210154702Swes			echo "nameserver $nameserver" >>$tmpres
211147072Sbrooks		done
212147072Sbrooks	fi
213147072Sbrooks
214154702Swes	if [ -f $tmpres ]; then
215147072Sbrooks		if [ -f /etc/resolv.conf.tail ]; then
216154702Swes			cat /etc/resolv.conf.tail >>$tmpres
217147072Sbrooks		fi
218147072Sbrooks
219149898Sbrooks		# When resolv.conf is not changed actually, we don't
220149898Sbrooks		# need to update it.
221149898Sbrooks		# If /usr is not mounted yet, we cannot use cmp, then
222149898Sbrooks		# the following test fails.  In such case, we simply
223149898Sbrooks		# ignore an error and do update resolv.conf.
224154702Swes		if cmp -s $tmpres /etc/resolv.conf; then
225154702Swes			rm -f $tmpres
226149898Sbrooks			return 0
227149898Sbrooks		fi 2>/dev/null
228149898Sbrooks
229147072Sbrooks		# In case (e.g. during OpenBSD installs) /etc/resolv.conf
230147072Sbrooks		# is a symbolic link, take care to preserve the link and write
231147072Sbrooks		# the new data in the correct location.
232147072Sbrooks
233147072Sbrooks		if [ -f /etc/resolv.conf ]; then
234147072Sbrooks			cat /etc/resolv.conf > /etc/resolv.conf.save
235147072Sbrooks		fi
236154702Swes		cat $tmpres > /etc/resolv.conf
237154702Swes		rm -f $tmpres
238147072Sbrooks
239147072Sbrooks		# Try to ensure correct ownership and permissions.
240147072Sbrooks		chown -RL root:wheel /etc/resolv.conf
241147072Sbrooks		chmod -RL 644 /etc/resolv.conf
242147072Sbrooks
243147072Sbrooks		return 0
244147072Sbrooks	fi
245147072Sbrooks
246147072Sbrooks	return 1
247147072Sbrooks}
248147072Sbrooks
249147138Sbrooks# Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
250147138Sbrooksexit_with_hooks() {
251147138Sbrooks	exit_status=$1
252147138Sbrooks	if [ -f /etc/dhclient-exit-hooks ]; then
253147138Sbrooks		. /etc/dhclient-exit-hooks
254147138Sbrooks	fi
255147138Sbrooks	# probably should do something with exit status of the local script
256147138Sbrooks	exit $exit_status
257147138Sbrooks}
258147138Sbrooks
259177730Sbrooks# Get the interface with the current ipv4 default route on it using only
260177730Sbrooks# commands that are available prior to /usr being mounted.
261177730Sbrooksis_default_interface()
262177730Sbrooks{
263177730Sbrooks	routeget="`route get -inet default`"
264177730Sbrooks	oldifs="$IFS"
265177730Sbrooks	IFS="
266177730Sbrooks"
267177730Sbrooks	defif=
268177730Sbrooks	for line in $routeget ; do
269177730Sbrooks		case $line in
270177730Sbrooks		*interface:*)
271177730Sbrooks			defif=${line##*: }
272177730Sbrooks			;;
273177730Sbrooks		esac
274177730Sbrooks	done
275177730Sbrooks	IFS=${oldifs}
276177730Sbrooks
277177730Sbrooks	if [ -z "$defif" -o "$defif" = "$interface" ]; then
278177730Sbrooks		return 0
279177730Sbrooks	else
280177730Sbrooks		return 1
281177730Sbrooks	fi
282177730Sbrooks}
283177730Sbrooks
284147072Sbrooks#
285147072Sbrooks# Start of active code.
286147072Sbrooks#
287147072Sbrooks
288147218Sbrooks# Invoke the local dhcp client enter hooks, if they exist.
289147218Sbrooksif [ -f /etc/dhclient-enter-hooks ]; then
290147218Sbrooks	exit_status=0
291147218Sbrooks	. /etc/dhclient-enter-hooks
292147218Sbrooks	# allow the local script to abort processing of this state
293147218Sbrooks	# local script must set exit_status variable to nonzero.
294147218Sbrooks	if [ $exit_status -ne 0 ]; then
295147218Sbrooks		exit $exit_status
296147218Sbrooks	fi
297147218Sbrooksfi
298147218Sbrooks
299147072Sbrookscase $reason in
300147072SbrooksMEDIUM)
301171187Sthompsa	eval "$IFCONFIG $interface $medium"
302171187Sthompsa	eval "$IFCONFIG $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
303147072Sbrooks	sleep 1
304147072Sbrooks	;;
305147072Sbrooks
306147072SbrooksPREINIT)
307147072Sbrooks	delete_old_alias
308175554Sbrooks	$IFCONFIG $interface inet alias 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
309147072Sbrooks	;;
310147072Sbrooks
311147072SbrooksARPCHECK|ARPSEND)
312147072Sbrooks	;;
313147072Sbrooks
314147072SbrooksBOUND|RENEW|REBIND|REBOOT)
315147086Sbrooks	check_hostname
316147072Sbrooks	if [ -n "$old_ip_address" ]; then
317147072Sbrooks		if [ "$old_ip_address" != "$alias_ip_address" ]; then
318147072Sbrooks			delete_old_alias
319147072Sbrooks		fi
320147072Sbrooks		if [ "$old_ip_address" != "$new_ip_address" ]; then
321147072Sbrooks			delete_old_address
322147072Sbrooks			delete_old_routes
323147072Sbrooks		fi
324147072Sbrooks	fi
325147072Sbrooks	if [ "$reason" = BOUND ] || \
326147072Sbrooks	   [ "$reason" = REBOOT ] || \
327147072Sbrooks	   [ -z "$old_ip_address" ] || \
328147072Sbrooks	   [ "$old_ip_address" != "$new_ip_address" ]; then
329147072Sbrooks		add_new_address
330147072Sbrooks		add_new_routes
331147072Sbrooks	fi
332147072Sbrooks	if [ "$new_ip_address" != "$alias_ip_address" ]; then
333147072Sbrooks		add_new_alias
334147072Sbrooks	fi
335177730Sbrooks	if is_default_interface; then
336177730Sbrooks		add_new_resolv_conf
337177730Sbrooks	fi
338147072Sbrooks	;;
339147072Sbrooks
340147072SbrooksEXPIRE|FAIL)
341147072Sbrooks	delete_old_alias
342147072Sbrooks	if [ -n "$old_ip_address" ]; then
343147072Sbrooks		delete_old_address
344147072Sbrooks		delete_old_routes
345147072Sbrooks	fi
346154164Sbrooks	if [ -x $ARP ]; then
347154164Sbrooks		$ARP -d -a -i $interface
348154164Sbrooks	fi
349147072Sbrooks	# XXX Why add alias we just deleted above?
350147072Sbrooks	add_new_alias
351177730Sbrooks	if is_default_interface; then
352177730Sbrooks		if [ -f /etc/resolv.conf.save ]; then
353177730Sbrooks			cat /etc/resolv.conf.save > /etc/resolv.conf
354177730Sbrooks		fi
355147072Sbrooks	fi
356147072Sbrooks	;;
357147072Sbrooks
358147072SbrooksTIMEOUT)
359147072Sbrooks	delete_old_alias
360147072Sbrooks	add_new_address
361147072Sbrooks	sleep 1
362147072Sbrooks	if [ -n "$new_routers" ]; then
363147086Sbrooks		$LOGGER "New Routers ($interface): $new_routers"
364147072Sbrooks		set "$new_routers"
365154760Sbrooks		if ping -q -c 1 -t 1 "$1"; then
366147072Sbrooks			if [ "$new_ip_address" != "$alias_ip_address" ]; then
367147072Sbrooks				add_new_alias
368147072Sbrooks			fi
369147072Sbrooks			add_new_routes
370177730Sbrooks			if ! is_default_interface; then
371177730Sbrooks				exit_with_hooks 0
372177730Sbrooks			fi
373147072Sbrooks			if add_new_resolv_conf; then
374147138Sbrooks				exit_with_hooks 0
375147072Sbrooks			fi
376147072Sbrooks		fi
377147072Sbrooks	fi
378171187Sthompsa	eval "$IFCONFIG $interface inet -alias $new_ip_address $medium"
379147072Sbrooks	delete_old_routes
380147138Sbrooks	exit_with_hooks 1
381147072Sbrooks	;;
382147072Sbrooksesac
383147072Sbrooks
384147138Sbrooksexit_with_hooks 0
385