xref: /freebsd/sbin/dhclient/dhclient-script (revision 60932bc9ae77f0c2b82e2968f66afc7dbbf3d39c)
147c08596SBrooks Davis#!/bin/sh
247c08596SBrooks Davis#
347c08596SBrooks Davis# $OpenBSD: dhclient-script,v 1.6 2004/05/06 18:22:41 claudio Exp $
48750adafSBrooks Davis# $FreeBSD$
547c08596SBrooks Davis#
647c08596SBrooks Davis# Copyright (c) 2003 Kenneth R Westerback <krw@openbsd.org>
747c08596SBrooks Davis#
847c08596SBrooks Davis# Permission to use, copy, modify, and distribute this software for any
947c08596SBrooks Davis# purpose with or without fee is hereby granted, provided that the above
1047c08596SBrooks Davis# copyright notice and this permission notice appear in all copies.
1147c08596SBrooks Davis#
1247c08596SBrooks Davis# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1347c08596SBrooks Davis# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1447c08596SBrooks Davis# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1547c08596SBrooks Davis# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1647c08596SBrooks Davis# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1747c08596SBrooks Davis# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1847c08596SBrooks Davis# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1947c08596SBrooks Davis#
2047c08596SBrooks Davis#
2147c08596SBrooks Davis
22b1f35e43SBrooks DavisARP=/usr/sbin/arp
238750adafSBrooks DavisAWK=/usr/bin/awk
248750adafSBrooks DavisHOSTNAME=/bin/hostname
2560932bc9SAndrew ThompsonIFCONFIG='/sbin/ifconfig -n'
26b1f35e43SBrooks DavisNETSTAT=/usr/bin/netstat
278750adafSBrooks Davis
288750adafSBrooks DavisLOCALHOST=127.0.0.1
298750adafSBrooks Davis
308750adafSBrooks Davisif [ -x /usr/bin/logger ]; then
318750adafSBrooks Davis	LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
328750adafSBrooks Daviselse
338750adafSBrooks Davis	LOGGER=echo
348750adafSBrooks Davisfi
358750adafSBrooks Davis
3647c08596SBrooks Davis#
3747c08596SBrooks Davis# Helper functions that implement common actions.
3847c08596SBrooks Davis#
3947c08596SBrooks Davis
408750adafSBrooks Davischeck_hostname() {
418750adafSBrooks Davis	current_hostname=`$HOSTNAME`
428750adafSBrooks Davis	if [ -z "$current_hostname" ]; then
438750adafSBrooks Davis		$LOGGER "New Hostname ($interface): $new_host_name"
448750adafSBrooks Davis		$HOSTNAME $new_host_name
458750adafSBrooks Davis	elif [ "$current_hostname" = "$old_host_name" -a \
468750adafSBrooks Davis	       "$new_host_name" != "$old_host_name" ]; then
478750adafSBrooks Davis		$LOGGER "New Hostname ($interface): $new_host_name"
488750adafSBrooks Davis		$HOSTNAME $new_host_name
4947c08596SBrooks Davis	fi
5047c08596SBrooks Davis}
5147c08596SBrooks Davis
528750adafSBrooks Davisarp_flush() {
538750adafSBrooks Davis	arp -an -i $interface | \
548750adafSBrooks Davis		sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \
558750adafSBrooks Davis		sh >/dev/null 2>&1
568750adafSBrooks Davis}
578750adafSBrooks Davis
588750adafSBrooks Davisdelete_old_address() {
5960932bc9SAndrew Thompson	eval "$IFCONFIG $interface inet -alias $old_ip_address $medium"
608750adafSBrooks Davis}
618750adafSBrooks Davis
6247c08596SBrooks Davisadd_new_address() {
6360932bc9SAndrew Thompson	eval "$IFCONFIG $interface \
6447c08596SBrooks Davis		inet $new_ip_address \
6547c08596SBrooks Davis		netmask $new_subnet_mask \
6647c08596SBrooks Davis		broadcast $new_broadcast_address \
67001f040aSBrooks Davis		$medium"
6847c08596SBrooks Davis
698750adafSBrooks Davis	$LOGGER "New IP Address ($interface): $new_ip_address"
708750adafSBrooks Davis	$LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
718750adafSBrooks Davis	$LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
728750adafSBrooks Davis	$LOGGER "New Routers ($interface): $new_routers"
7347c08596SBrooks Davis}
7447c08596SBrooks Davis
7547c08596SBrooks Davisdelete_old_alias() {
7647c08596SBrooks Davis	if [ -n "$alias_ip_address" ]; then
7760932bc9SAndrew Thompson		$IFCONFIG $interface inet -alias $alias_ip_address > /dev/null 2>&1
787e82455eSBrooks Davis		#route delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
7947c08596SBrooks Davis	fi
8047c08596SBrooks Davis}
8147c08596SBrooks Davis
8247c08596SBrooks Davisadd_new_alias() {
8347c08596SBrooks Davis	if [ -n "$alias_ip_address" ]; then
8460932bc9SAndrew Thompson		$IFCONFIG $interface inet alias $alias_ip_address netmask \
8547c08596SBrooks Davis		    $alias_subnet_mask
867e82455eSBrooks Davis		#route add $alias_ip_address $LOCALHOST
8747c08596SBrooks Davis	fi
8847c08596SBrooks Davis}
8947c08596SBrooks Davis
902fcc7370SEd Mastefill_classless_routes() {
912fcc7370SEd Maste	set $1
929f0d81baSEd Maste	while [ $# -ge 5 ]; do
932fcc7370SEd Maste		if [ $1 -eq 0 ]; then
942fcc7370SEd Maste			route="default"
952fcc7370SEd Maste		elif [ $1 -le 8 ]; then
962fcc7370SEd Maste			route="$2.0.0.0/$1"
972fcc7370SEd Maste			shift
982fcc7370SEd Maste		elif [ $1 -le 16 ]; then
992fcc7370SEd Maste			route="$2.$3.0.0/$1"
1002fcc7370SEd Maste			shift; shift
1012fcc7370SEd Maste		elif [ $1 -le 24 ]; then
1022fcc7370SEd Maste			route="$2.$3.$4.0/$1"
1032fcc7370SEd Maste			shift; shift; shift
1042fcc7370SEd Maste		else
1052fcc7370SEd Maste			route="$2.$3.$4.$5/$1"
1062fcc7370SEd Maste			shift; shift; shift; shift
1072fcc7370SEd Maste		fi
1082fcc7370SEd Maste		shift
1092fcc7370SEd Maste		router="$1.$2.$3.$4"
1102fcc7370SEd Maste		classless_routes="$classless_routes $route $router"
1112fcc7370SEd Maste		shift; shift; shift; shift
1122fcc7370SEd Maste	done
1132fcc7370SEd Maste}
1142fcc7370SEd Maste
11547c08596SBrooks Davisdelete_old_routes() {
1167e82455eSBrooks Davis	#route delete "$old_ip_address" $LOCALHOST >/dev/null 2>&1
1172fcc7370SEd Maste	if [ -n "$old_classless_routes" ]; then
1182fcc7370SEd Maste		fill_classless_routes "$old_classless_routes"
1192fcc7370SEd Maste		set $classless_routes
1202fcc7370SEd Maste		while [ $# -gt 1 ]; do
1212fcc7370SEd Maste			route delete "$1" "$2"
1222fcc7370SEd Maste			shift; shift
1232fcc7370SEd Maste		done
1242fcc7370SEd Maste		return 0;
1252fcc7370SEd Maste	fi
1262fcc7370SEd Maste
1278750adafSBrooks Davis	for router in $old_routers; do
1288750adafSBrooks Davis		if [ $if_defaultroute = x -o $if_defaultroute = $interface ]; then
1298750adafSBrooks Davis			route delete default $route >/dev/null 2>&1
1308750adafSBrooks Davis		fi
1318750adafSBrooks Davis	done
13247c08596SBrooks Davis
13347c08596SBrooks Davis	if [ -n "$old_static_routes" ]; then
13447c08596SBrooks Davis		set $old_static_routes
13547c08596SBrooks Davis		while [ $# -gt 1 ]; do
13647c08596SBrooks Davis			route delete "$1" "$2"
13747c08596SBrooks Davis			shift; shift
13847c08596SBrooks Davis		done
13947c08596SBrooks Davis	fi
14047c08596SBrooks Davis
1418750adafSBrooks Davis	arp_flush
14247c08596SBrooks Davis}
14347c08596SBrooks Davis
14447c08596SBrooks Davisadd_new_routes() {
1457e82455eSBrooks Davis	#route add $new_ip_address $LOCALHOST >/dev/null 2>&1
1462fcc7370SEd Maste
1472fcc7370SEd Maste	# RFC 3442: If the DHCP server returns both a Classless Static
1482fcc7370SEd Maste	# Routes option and a Router option, the DHCP client MUST ignore
1492fcc7370SEd Maste	# the Router option.
1502fcc7370SEd Maste	#
1512fcc7370SEd Maste	# DHCP clients that support this option (Classless Static Routes)
1522fcc7370SEd Maste	# MUST NOT install the routes specified in the Static Routes
1532fcc7370SEd Maste	# option (option code 33) if both a Static Routes option and the
1542fcc7370SEd Maste	# Classless Static Routes option are provided.
1552fcc7370SEd Maste
1562fcc7370SEd Maste	if [ -n "$new_classless_routes" ]; then
1572fcc7370SEd Maste		fill_classless_routes "$new_classless_routes"
1582fcc7370SEd Maste		$LOGGER "New Classless Static Routes ($interface): $classless_routes"
1592fcc7370SEd Maste		set $classless_routes
1602fcc7370SEd Maste		while [ $# -gt 1 ]; do
1612fcc7370SEd Maste			if [ "0.0.0.0" = "$2" ]; then
1622fcc7370SEd Maste				route add "$1" -iface "$interface"
1632fcc7370SEd Maste			else
1642fcc7370SEd Maste				route add "$1" "$2"
1652fcc7370SEd Maste			fi
1662fcc7370SEd Maste			shift; shift
1672fcc7370SEd Maste		done
1682fcc7370SEd Maste		return
1692fcc7370SEd Maste	fi
1702fcc7370SEd Maste
17147c08596SBrooks Davis	for router in $new_routers; do
17247c08596SBrooks Davis		if [ "$new_ip_address" = "$router" ]; then
17347c08596SBrooks Davis			route add default -iface $router >/dev/null 2>&1
17447c08596SBrooks Davis		else
17547c08596SBrooks Davis			route add default $router >/dev/null 2>&1
17647c08596SBrooks Davis		fi
17747c08596SBrooks Davis		# 2nd and subsequent default routers error out, so explicitly
17847c08596SBrooks Davis		# stop processing the list after the first one.
17947c08596SBrooks Davis		break
18047c08596SBrooks Davis	done
18147c08596SBrooks Davis
18247c08596SBrooks Davis	if [ -n "$new_static_routes" ]; then
1838750adafSBrooks Davis		$LOGGER "New Static Routes ($interface): $new_static_routes"
18447c08596SBrooks Davis		set $new_static_routes
18547c08596SBrooks Davis		while [ $# -gt 1 ]; do
18647c08596SBrooks Davis			route add $1 $2
18747c08596SBrooks Davis			shift; shift
18847c08596SBrooks Davis		done
18947c08596SBrooks Davis	fi
19047c08596SBrooks Davis}
19147c08596SBrooks Davis
19247c08596SBrooks Davisadd_new_resolv_conf() {
19347c08596SBrooks Davis	# XXX Old code did not create/update resolv.conf unless both
19447c08596SBrooks Davis	# $new_domain_name and $new_domain_name_servers were provided.  PR
19547c08596SBrooks Davis	# #3135 reported some ISP's only provide $new_domain_name_servers and
19647c08596SBrooks Davis	# thus broke the script. This code creates the resolv.conf if either
19747c08596SBrooks Davis	# are provided.
19847c08596SBrooks Davis
199f1bacaa5SBrooks Davis	local tmpres=/var/run/resolv.conf.${interface}
20090158aeeSWes Peters	rm -f $tmpres
20147c08596SBrooks Davis
20247c08596SBrooks Davis	if [ -n "$new_domain_name" ]; then
20390158aeeSWes Peters		echo "search $new_domain_name" >>$tmpres
20447c08596SBrooks Davis	fi
20547c08596SBrooks Davis
20647c08596SBrooks Davis	if [ -n "$new_domain_name_servers" ]; then
20747c08596SBrooks Davis		for nameserver in $new_domain_name_servers; do
20890158aeeSWes Peters			echo "nameserver $nameserver" >>$tmpres
20947c08596SBrooks Davis		done
21047c08596SBrooks Davis	fi
21147c08596SBrooks Davis
21290158aeeSWes Peters	if [ -f $tmpres ]; then
21347c08596SBrooks Davis		if [ -f /etc/resolv.conf.tail ]; then
21490158aeeSWes Peters			cat /etc/resolv.conf.tail >>$tmpres
21547c08596SBrooks Davis		fi
21647c08596SBrooks Davis
217dd415a50SBrooks Davis		# When resolv.conf is not changed actually, we don't
218dd415a50SBrooks Davis		# need to update it.
219dd415a50SBrooks Davis		# If /usr is not mounted yet, we cannot use cmp, then
220dd415a50SBrooks Davis		# the following test fails.  In such case, we simply
221dd415a50SBrooks Davis		# ignore an error and do update resolv.conf.
22290158aeeSWes Peters		if cmp -s $tmpres /etc/resolv.conf; then
22390158aeeSWes Peters			rm -f $tmpres
224dd415a50SBrooks Davis			return 0
225dd415a50SBrooks Davis		fi 2>/dev/null
226dd415a50SBrooks Davis
22747c08596SBrooks Davis		# In case (e.g. during OpenBSD installs) /etc/resolv.conf
22847c08596SBrooks Davis		# is a symbolic link, take care to preserve the link and write
22947c08596SBrooks Davis		# the new data in the correct location.
23047c08596SBrooks Davis
23147c08596SBrooks Davis		if [ -f /etc/resolv.conf ]; then
23247c08596SBrooks Davis			cat /etc/resolv.conf > /etc/resolv.conf.save
23347c08596SBrooks Davis		fi
23490158aeeSWes Peters		cat $tmpres > /etc/resolv.conf
23590158aeeSWes Peters		rm -f $tmpres
23647c08596SBrooks Davis
23747c08596SBrooks Davis		# Try to ensure correct ownership and permissions.
23847c08596SBrooks Davis		chown -RL root:wheel /etc/resolv.conf
23947c08596SBrooks Davis		chmod -RL 644 /etc/resolv.conf
24047c08596SBrooks Davis
24147c08596SBrooks Davis		return 0
24247c08596SBrooks Davis	fi
24347c08596SBrooks Davis
24447c08596SBrooks Davis	return 1
24547c08596SBrooks Davis}
24647c08596SBrooks Davis
247d6790d5aSBrooks Davis# Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
248d6790d5aSBrooks Davisexit_with_hooks() {
249d6790d5aSBrooks Davis	exit_status=$1
250d6790d5aSBrooks Davis	if [ -f /etc/dhclient-exit-hooks ]; then
251d6790d5aSBrooks Davis		. /etc/dhclient-exit-hooks
252d6790d5aSBrooks Davis	fi
253d6790d5aSBrooks Davis	# probably should do something with exit status of the local script
254d6790d5aSBrooks Davis	exit $exit_status
255d6790d5aSBrooks Davis}
256d6790d5aSBrooks Davis
25747c08596SBrooks Davis#
25847c08596SBrooks Davis# Start of active code.
25947c08596SBrooks Davis#
26047c08596SBrooks Davis
261d5fedb6eSBrooks Davis# Invoke the local dhcp client enter hooks, if they exist.
262d5fedb6eSBrooks Davisif [ -f /etc/dhclient-enter-hooks ]; then
263d5fedb6eSBrooks Davis	exit_status=0
264d5fedb6eSBrooks Davis	. /etc/dhclient-enter-hooks
265d5fedb6eSBrooks Davis	# allow the local script to abort processing of this state
266d5fedb6eSBrooks Davis	# local script must set exit_status variable to nonzero.
267d5fedb6eSBrooks Davis	if [ $exit_status -ne 0 ]; then
268d5fedb6eSBrooks Davis		exit $exit_status
269d5fedb6eSBrooks Davis	fi
270d5fedb6eSBrooks Davisfi
271d5fedb6eSBrooks Davis
2728750adafSBrooks Davisif [ -x $NETSTAT ]; then
2736ae27cb6SBrooks Davis	if_defaultroute=`$NETSTAT -rnf inet | $AWK '{if ($1=="default") printf $6}'`
2748750adafSBrooks Daviselse
2758750adafSBrooks Davis	if_defaultroute="x"
27647c08596SBrooks Davisfi
27747c08596SBrooks Davis
27847c08596SBrooks Daviscase $reason in
27947c08596SBrooks DavisMEDIUM)
28060932bc9SAndrew Thompson	eval "$IFCONFIG $interface $medium"
28160932bc9SAndrew Thompson	eval "$IFCONFIG $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
28247c08596SBrooks Davis	sleep 1
28347c08596SBrooks Davis	;;
28447c08596SBrooks Davis
28547c08596SBrooks DavisPREINIT)
28647c08596SBrooks Davis	delete_old_alias
28760932bc9SAndrew Thompson	$IFCONFIG $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
28847c08596SBrooks Davis	;;
28947c08596SBrooks Davis
29047c08596SBrooks DavisARPCHECK|ARPSEND)
29147c08596SBrooks Davis	;;
29247c08596SBrooks Davis
29347c08596SBrooks DavisBOUND|RENEW|REBIND|REBOOT)
2948750adafSBrooks Davis	check_hostname
29547c08596SBrooks Davis	if [ -n "$old_ip_address" ]; then
29647c08596SBrooks Davis		if [ "$old_ip_address" != "$alias_ip_address" ]; then
29747c08596SBrooks Davis			delete_old_alias
29847c08596SBrooks Davis		fi
29947c08596SBrooks Davis		if [ "$old_ip_address" != "$new_ip_address" ]; then
30047c08596SBrooks Davis			delete_old_address
30147c08596SBrooks Davis			delete_old_routes
30247c08596SBrooks Davis		fi
30347c08596SBrooks Davis	fi
30447c08596SBrooks Davis	if [ "$reason" = BOUND ] || \
30547c08596SBrooks Davis	   [ "$reason" = REBOOT ] || \
30647c08596SBrooks Davis	   [ -z "$old_ip_address" ] || \
30747c08596SBrooks Davis	   [ "$old_ip_address" != "$new_ip_address" ]; then
30847c08596SBrooks Davis		add_new_address
30947c08596SBrooks Davis		add_new_routes
31047c08596SBrooks Davis	fi
31147c08596SBrooks Davis	if [ "$new_ip_address" != "$alias_ip_address" ]; then
31247c08596SBrooks Davis		add_new_alias
31347c08596SBrooks Davis	fi
31447c08596SBrooks Davis	add_new_resolv_conf
31547c08596SBrooks Davis	;;
31647c08596SBrooks Davis
31747c08596SBrooks DavisEXPIRE|FAIL)
31847c08596SBrooks Davis	delete_old_alias
31947c08596SBrooks Davis	if [ -n "$old_ip_address" ]; then
32047c08596SBrooks Davis		delete_old_address
32147c08596SBrooks Davis		delete_old_routes
32247c08596SBrooks Davis	fi
323b1f35e43SBrooks Davis	if [ -x $ARP ]; then
324b1f35e43SBrooks Davis		$ARP -d -a -i $interface
325b1f35e43SBrooks Davis	fi
32647c08596SBrooks Davis	# XXX Why add alias we just deleted above?
32747c08596SBrooks Davis	add_new_alias
32847c08596SBrooks Davis	if [ -f /etc/resolv.conf.save ]; then
32947c08596SBrooks Davis		cat /etc/resolv.conf.save > /etc/resolv.conf
33047c08596SBrooks Davis	fi
33147c08596SBrooks Davis	;;
33247c08596SBrooks Davis
33347c08596SBrooks DavisTIMEOUT)
33447c08596SBrooks Davis	delete_old_alias
33547c08596SBrooks Davis	add_new_address
33647c08596SBrooks Davis	sleep 1
33747c08596SBrooks Davis	if [ -n "$new_routers" ]; then
3388750adafSBrooks Davis		$LOGGER "New Routers ($interface): $new_routers"
33947c08596SBrooks Davis		set "$new_routers"
340b0864f3aSBrooks Davis		if ping -q -c 1 -t 1 "$1"; then
34147c08596SBrooks Davis			if [ "$new_ip_address" != "$alias_ip_address" ]; then
34247c08596SBrooks Davis				add_new_alias
34347c08596SBrooks Davis			fi
34447c08596SBrooks Davis			add_new_routes
34547c08596SBrooks Davis			if add_new_resolv_conf; then
346d6790d5aSBrooks Davis				exit_with_hooks 0
34747c08596SBrooks Davis			fi
34847c08596SBrooks Davis		fi
34947c08596SBrooks Davis	fi
35060932bc9SAndrew Thompson	eval "$IFCONFIG $interface inet -alias $new_ip_address $medium"
35147c08596SBrooks Davis	delete_old_routes
352d6790d5aSBrooks Davis	exit_with_hooks 1
35347c08596SBrooks Davis	;;
35447c08596SBrooks Davisesac
35547c08596SBrooks Davis
356d6790d5aSBrooks Davisexit_with_hooks 0
357