dhclient-script revision 229583
1147072Sbrooks#!/bin/sh
2147072Sbrooks#
3147072Sbrooks# $OpenBSD: dhclient-script,v 1.6 2004/05/06 18:22:41 claudio Exp $
4147086Sbrooks# $FreeBSD: stable/9/sbin/dhclient/dhclient-script 229583 2012-01-05 11:14:28Z glebius $
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
219219739Sume		case $resolvconf_enable in
220219739Sume		# "no", "false", "off", or "0"
221219739Sume		[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
222219739Sume			# When resolv.conf is not changed actually, we don't
223219739Sume			# need to update it.
224219739Sume			# If /usr is not mounted yet, we cannot use cmp, then
225219739Sume			# the following test fails.  In such case, we simply
226219739Sume			# ignore an error and do update resolv.conf.
227219739Sume			if cmp -s $tmpres /etc/resolv.conf; then
228219739Sume				rm -f $tmpres
229219739Sume				return 0
230219739Sume			fi 2>/dev/null
231149898Sbrooks
232219739Sume			# In case (e.g. during OpenBSD installs)
233219739Sume			# /etc/resolv.conf is a symbolic link, take
234219739Sume			# care to preserve the link and write the new
235219739Sume			# data in the correct location.
236147072Sbrooks
237219739Sume			if [ -f /etc/resolv.conf ]; then
238219739Sume				cat /etc/resolv.conf > /etc/resolv.conf.save
239219739Sume			fi
240219739Sume			cat $tmpres > /etc/resolv.conf
241219739Sume
242219739Sume			# Try to ensure correct ownership and permissions.
243219739Sume			chown -RL root:wheel /etc/resolv.conf
244219739Sume			chmod -RL 644 /etc/resolv.conf
245219739Sume			;;
246219739Sume
247219739Sume		*)
248219739Sume			/sbin/resolvconf -a ${interface} < $tmpres
249219739Sume			;;
250219739Sume		esac
251219739Sume
252154702Swes		rm -f $tmpres
253147072Sbrooks
254147072Sbrooks		return 0
255147072Sbrooks	fi
256147072Sbrooks
257147072Sbrooks	return 1
258147072Sbrooks}
259147072Sbrooks
260147138Sbrooks# Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
261147138Sbrooksexit_with_hooks() {
262147138Sbrooks	exit_status=$1
263147138Sbrooks	if [ -f /etc/dhclient-exit-hooks ]; then
264147138Sbrooks		. /etc/dhclient-exit-hooks
265147138Sbrooks	fi
266147138Sbrooks	# probably should do something with exit status of the local script
267147138Sbrooks	exit $exit_status
268147138Sbrooks}
269147138Sbrooks
270177730Sbrooks# Get the interface with the current ipv4 default route on it using only
271177730Sbrooks# commands that are available prior to /usr being mounted.
272177730Sbrooksis_default_interface()
273177730Sbrooks{
274179689Sbrooks	routeget="`route -n get -inet default`"
275177730Sbrooks	oldifs="$IFS"
276177730Sbrooks	IFS="
277177730Sbrooks"
278177730Sbrooks	defif=
279177730Sbrooks	for line in $routeget ; do
280177730Sbrooks		case $line in
281177730Sbrooks		*interface:*)
282177730Sbrooks			defif=${line##*: }
283177730Sbrooks			;;
284177730Sbrooks		esac
285177730Sbrooks	done
286177730Sbrooks	IFS=${oldifs}
287177730Sbrooks
288177730Sbrooks	if [ -z "$defif" -o "$defif" = "$interface" ]; then
289177730Sbrooks		return 0
290177730Sbrooks	else
291177730Sbrooks		return 1
292177730Sbrooks	fi
293177730Sbrooks}
294177730Sbrooks
295147072Sbrooks#
296147072Sbrooks# Start of active code.
297147072Sbrooks#
298147072Sbrooks
299147218Sbrooks# Invoke the local dhcp client enter hooks, if they exist.
300147218Sbrooksif [ -f /etc/dhclient-enter-hooks ]; then
301147218Sbrooks	exit_status=0
302147218Sbrooks	. /etc/dhclient-enter-hooks
303147218Sbrooks	# allow the local script to abort processing of this state
304147218Sbrooks	# local script must set exit_status variable to nonzero.
305147218Sbrooks	if [ $exit_status -ne 0 ]; then
306147218Sbrooks		exit $exit_status
307147218Sbrooks	fi
308147218Sbrooksfi
309147218Sbrooks
310219739Sume: ${resolvconf_enable="YES"}
311219739Sume
312147072Sbrookscase $reason in
313147072SbrooksMEDIUM)
314171187Sthompsa	eval "$IFCONFIG $interface $medium"
315171187Sthompsa	eval "$IFCONFIG $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
316147072Sbrooks	sleep 1
317147072Sbrooks	;;
318147072Sbrooks
319147072SbrooksPREINIT)
320147072Sbrooks	delete_old_alias
321229583Sglebius	$IFCONFIG $interface inet alias 0.0.0.0 netmask 255.0.0.0 broadcast 255.255.255.255 up
322147072Sbrooks	;;
323147072Sbrooks
324147072SbrooksARPCHECK|ARPSEND)
325147072Sbrooks	;;
326147072Sbrooks
327147072SbrooksBOUND|RENEW|REBIND|REBOOT)
328147086Sbrooks	check_hostname
329147072Sbrooks	if [ -n "$old_ip_address" ]; then
330147072Sbrooks		if [ "$old_ip_address" != "$alias_ip_address" ]; then
331147072Sbrooks			delete_old_alias
332147072Sbrooks		fi
333147072Sbrooks		if [ "$old_ip_address" != "$new_ip_address" ]; then
334147072Sbrooks			delete_old_address
335147072Sbrooks			delete_old_routes
336147072Sbrooks		fi
337147072Sbrooks	fi
338147072Sbrooks	if [ "$reason" = BOUND ] || \
339147072Sbrooks	   [ "$reason" = REBOOT ] || \
340147072Sbrooks	   [ -z "$old_ip_address" ] || \
341147072Sbrooks	   [ "$old_ip_address" != "$new_ip_address" ]; then
342147072Sbrooks		add_new_address
343147072Sbrooks		add_new_routes
344147072Sbrooks	fi
345147072Sbrooks	if [ "$new_ip_address" != "$alias_ip_address" ]; then
346147072Sbrooks		add_new_alias
347147072Sbrooks	fi
348177730Sbrooks	if is_default_interface; then
349177730Sbrooks		add_new_resolv_conf
350177730Sbrooks	fi
351147072Sbrooks	;;
352147072Sbrooks
353147072SbrooksEXPIRE|FAIL)
354147072Sbrooks	delete_old_alias
355147072Sbrooks	if [ -n "$old_ip_address" ]; then
356147072Sbrooks		delete_old_address
357147072Sbrooks		delete_old_routes
358147072Sbrooks	fi
359154164Sbrooks	if [ -x $ARP ]; then
360154164Sbrooks		$ARP -d -a -i $interface
361154164Sbrooks	fi
362147072Sbrooks	# XXX Why add alias we just deleted above?
363147072Sbrooks	add_new_alias
364177730Sbrooks	if is_default_interface; then
365219739Sume		case $resolvconf_enable in
366219739Sume		# "no", "false", "off", or "0"
367219739Sume		[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
368219739Sume			if [ -f /etc/resolv.conf.save ]; then
369219739Sume				cat /etc/resolv.conf.save > /etc/resolv.conf
370219739Sume			fi
371219739Sume			;;
372219739Sume		*)
373219739Sume			/sbin/resolvconf -d ${interface}
374219739Sume			;;
375219739Sume		esac
376147072Sbrooks	fi
377147072Sbrooks	;;
378147072Sbrooks
379147072SbrooksTIMEOUT)
380147072Sbrooks	delete_old_alias
381147072Sbrooks	add_new_address
382147072Sbrooks	sleep 1
383147072Sbrooks	if [ -n "$new_routers" ]; then
384147086Sbrooks		$LOGGER "New Routers ($interface): $new_routers"
385147072Sbrooks		set "$new_routers"
386154760Sbrooks		if ping -q -c 1 -t 1 "$1"; then
387147072Sbrooks			if [ "$new_ip_address" != "$alias_ip_address" ]; then
388147072Sbrooks				add_new_alias
389147072Sbrooks			fi
390147072Sbrooks			add_new_routes
391177730Sbrooks			if ! is_default_interface; then
392177730Sbrooks				exit_with_hooks 0
393177730Sbrooks			fi
394147072Sbrooks			if add_new_resolv_conf; then
395147138Sbrooks				exit_with_hooks 0
396147072Sbrooks			fi
397147072Sbrooks		fi
398147072Sbrooks	fi
399171187Sthompsa	eval "$IFCONFIG $interface inet -alias $new_ip_address $medium"
400147072Sbrooks	delete_old_routes
401147138Sbrooks	exit_with_hooks 1
402147072Sbrooks	;;
403147072Sbrooksesac
404147072Sbrooks
405147138Sbrooksexit_with_hooks 0
406