1147072Sbrooks#!/bin/sh
2147072Sbrooks#
3147072Sbrooks# $OpenBSD: dhclient-script,v 1.6 2004/05/06 18:22:41 claudio Exp $
4147086Sbrooks# $FreeBSD$
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
204230597Sdumbbell	if [ -n "$new_domain_search" ]; then
205230597Sdumbbell		echo "search $new_domain_search" >>$tmpres
206230597Sdumbbell	elif [ -n "$new_domain_name" ]; then
207154702Swes		echo "search $new_domain_name" >>$tmpres
208147072Sbrooks	fi
209147072Sbrooks
210147072Sbrooks	if [ -n "$new_domain_name_servers" ]; then
211147072Sbrooks		for nameserver in $new_domain_name_servers; do
212154702Swes			echo "nameserver $nameserver" >>$tmpres
213147072Sbrooks		done
214147072Sbrooks	fi
215147072Sbrooks
216154702Swes	if [ -f $tmpres ]; then
217147072Sbrooks		if [ -f /etc/resolv.conf.tail ]; then
218154702Swes			cat /etc/resolv.conf.tail >>$tmpres
219147072Sbrooks		fi
220147072Sbrooks
221219739Sume		case $resolvconf_enable in
222219739Sume		# "no", "false", "off", or "0"
223219739Sume		[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
224219739Sume			# When resolv.conf is not changed actually, we don't
225219739Sume			# need to update it.
226219739Sume			# If /usr is not mounted yet, we cannot use cmp, then
227219739Sume			# the following test fails.  In such case, we simply
228219739Sume			# ignore an error and do update resolv.conf.
229219739Sume			if cmp -s $tmpres /etc/resolv.conf; then
230219739Sume				rm -f $tmpres
231219739Sume				return 0
232219739Sume			fi 2>/dev/null
233149898Sbrooks
234219739Sume			# In case (e.g. during OpenBSD installs)
235219739Sume			# /etc/resolv.conf is a symbolic link, take
236219739Sume			# care to preserve the link and write the new
237219739Sume			# data in the correct location.
238147072Sbrooks
239219739Sume			if [ -f /etc/resolv.conf ]; then
240219739Sume				cat /etc/resolv.conf > /etc/resolv.conf.save
241219739Sume			fi
242219739Sume			cat $tmpres > /etc/resolv.conf
243219739Sume
244219739Sume			# Try to ensure correct ownership and permissions.
245219739Sume			chown -RL root:wheel /etc/resolv.conf
246219739Sume			chmod -RL 644 /etc/resolv.conf
247219739Sume			;;
248219739Sume
249219739Sume		*)
250219739Sume			/sbin/resolvconf -a ${interface} < $tmpres
251219739Sume			;;
252219739Sume		esac
253219739Sume
254154702Swes		rm -f $tmpres
255147072Sbrooks
256147072Sbrooks		return 0
257147072Sbrooks	fi
258147072Sbrooks
259147072Sbrooks	return 1
260147072Sbrooks}
261147072Sbrooks
262147138Sbrooks# Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
263147138Sbrooksexit_with_hooks() {
264147138Sbrooks	exit_status=$1
265147138Sbrooks	if [ -f /etc/dhclient-exit-hooks ]; then
266147138Sbrooks		. /etc/dhclient-exit-hooks
267147138Sbrooks	fi
268147138Sbrooks	# probably should do something with exit status of the local script
269147138Sbrooks	exit $exit_status
270147138Sbrooks}
271147138Sbrooks
272177730Sbrooks# Get the interface with the current ipv4 default route on it using only
273177730Sbrooks# commands that are available prior to /usr being mounted.
274177730Sbrooksis_default_interface()
275177730Sbrooks{
276179689Sbrooks	routeget="`route -n get -inet default`"
277177730Sbrooks	oldifs="$IFS"
278177730Sbrooks	IFS="
279177730Sbrooks"
280177730Sbrooks	defif=
281177730Sbrooks	for line in $routeget ; do
282177730Sbrooks		case $line in
283177730Sbrooks		*interface:*)
284177730Sbrooks			defif=${line##*: }
285177730Sbrooks			;;
286177730Sbrooks		esac
287177730Sbrooks	done
288177730Sbrooks	IFS=${oldifs}
289177730Sbrooks
290177730Sbrooks	if [ -z "$defif" -o "$defif" = "$interface" ]; then
291177730Sbrooks		return 0
292177730Sbrooks	else
293177730Sbrooks		return 1
294177730Sbrooks	fi
295177730Sbrooks}
296177730Sbrooks
297147072Sbrooks#
298147072Sbrooks# Start of active code.
299147072Sbrooks#
300147072Sbrooks
301147218Sbrooks# Invoke the local dhcp client enter hooks, if they exist.
302147218Sbrooksif [ -f /etc/dhclient-enter-hooks ]; then
303147218Sbrooks	exit_status=0
304147218Sbrooks	. /etc/dhclient-enter-hooks
305147218Sbrooks	# allow the local script to abort processing of this state
306147218Sbrooks	# local script must set exit_status variable to nonzero.
307147218Sbrooks	if [ $exit_status -ne 0 ]; then
308147218Sbrooks		exit $exit_status
309147218Sbrooks	fi
310147218Sbrooksfi
311147218Sbrooks
312219739Sume: ${resolvconf_enable="YES"}
313219739Sume
314147072Sbrookscase $reason in
315147072SbrooksMEDIUM)
316171187Sthompsa	eval "$IFCONFIG $interface $medium"
317171187Sthompsa	eval "$IFCONFIG $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
318147072Sbrooks	sleep 1
319147072Sbrooks	;;
320147072Sbrooks
321147072SbrooksPREINIT)
322147072Sbrooks	delete_old_alias
323229583Sglebius	$IFCONFIG $interface inet alias 0.0.0.0 netmask 255.0.0.0 broadcast 255.255.255.255 up
324147072Sbrooks	;;
325147072Sbrooks
326147072SbrooksARPCHECK|ARPSEND)
327147072Sbrooks	;;
328147072Sbrooks
329147072SbrooksBOUND|RENEW|REBIND|REBOOT)
330147086Sbrooks	check_hostname
331147072Sbrooks	if [ -n "$old_ip_address" ]; then
332147072Sbrooks		if [ "$old_ip_address" != "$alias_ip_address" ]; then
333147072Sbrooks			delete_old_alias
334147072Sbrooks		fi
335147072Sbrooks		if [ "$old_ip_address" != "$new_ip_address" ]; then
336147072Sbrooks			delete_old_address
337147072Sbrooks			delete_old_routes
338147072Sbrooks		fi
339147072Sbrooks	fi
340147072Sbrooks	if [ "$reason" = BOUND ] || \
341147072Sbrooks	   [ "$reason" = REBOOT ] || \
342147072Sbrooks	   [ -z "$old_ip_address" ] || \
343147072Sbrooks	   [ "$old_ip_address" != "$new_ip_address" ]; then
344147072Sbrooks		add_new_address
345147072Sbrooks		add_new_routes
346147072Sbrooks	fi
347147072Sbrooks	if [ "$new_ip_address" != "$alias_ip_address" ]; then
348147072Sbrooks		add_new_alias
349147072Sbrooks	fi
350177730Sbrooks	if is_default_interface; then
351177730Sbrooks		add_new_resolv_conf
352177730Sbrooks	fi
353147072Sbrooks	;;
354147072Sbrooks
355147072SbrooksEXPIRE|FAIL)
356147072Sbrooks	delete_old_alias
357147072Sbrooks	if [ -n "$old_ip_address" ]; then
358147072Sbrooks		delete_old_address
359147072Sbrooks		delete_old_routes
360147072Sbrooks	fi
361154164Sbrooks	if [ -x $ARP ]; then
362154164Sbrooks		$ARP -d -a -i $interface
363154164Sbrooks	fi
364147072Sbrooks	# XXX Why add alias we just deleted above?
365147072Sbrooks	add_new_alias
366177730Sbrooks	if is_default_interface; then
367219739Sume		case $resolvconf_enable in
368219739Sume		# "no", "false", "off", or "0"
369219739Sume		[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
370219739Sume			if [ -f /etc/resolv.conf.save ]; then
371219739Sume				cat /etc/resolv.conf.save > /etc/resolv.conf
372219739Sume			fi
373219739Sume			;;
374219739Sume		*)
375219739Sume			/sbin/resolvconf -d ${interface}
376219739Sume			;;
377219739Sume		esac
378147072Sbrooks	fi
379147072Sbrooks	;;
380147072Sbrooks
381147072SbrooksTIMEOUT)
382147072Sbrooks	delete_old_alias
383147072Sbrooks	add_new_address
384147072Sbrooks	sleep 1
385147072Sbrooks	if [ -n "$new_routers" ]; then
386147086Sbrooks		$LOGGER "New Routers ($interface): $new_routers"
387147072Sbrooks		set "$new_routers"
388154760Sbrooks		if ping -q -c 1 -t 1 "$1"; then
389147072Sbrooks			if [ "$new_ip_address" != "$alias_ip_address" ]; then
390147072Sbrooks				add_new_alias
391147072Sbrooks			fi
392147072Sbrooks			add_new_routes
393177730Sbrooks			if ! is_default_interface; then
394177730Sbrooks				exit_with_hooks 0
395177730Sbrooks			fi
396147072Sbrooks			if add_new_resolv_conf; then
397147138Sbrooks				exit_with_hooks 0
398147072Sbrooks			fi
399147072Sbrooks		fi
400147072Sbrooks	fi
401171187Sthompsa	eval "$IFCONFIG $interface inet -alias $new_ip_address $medium"
402147072Sbrooks	delete_old_routes
403147138Sbrooks	exit_with_hooks 1
404147072Sbrooks	;;
405147072Sbrooksesac
406147072Sbrooks
407147138Sbrooksexit_with_hooks 0
408