xref: /dflybsd-src/sbin/dhclient/dhclient-script (revision b976f5669e4108cfebb0d7c7f2c675c1adf9a90a)
1#!/bin/sh
2#
3# $OpenBSD: src/sbin/dhclient/dhclient-script,v 1.13 2009/01/24 13:59:43 krw Exp $
4#
5# Copyright (c) 2003 Kenneth R Westerback <krw@openbsd.org>
6#
7# Permission to use, copy, modify, and distribute this software for any
8# purpose with or without fee is hereby granted, provided that the above
9# copyright notice and this permission notice appear in all copies.
10#
11# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18#
19#
20
21#
22# Helper functions that implement common actions.
23#
24
25delete_old_address() {
26	if [ -n "$old_ip_address" ]; then
27		ifconfig $interface inet $old_ip_address delete $medium
28		#route delete "$old_ip_address" 127.0.0.1 >/dev/null 2>&1
29	fi
30}
31
32add_new_address() {
33	ifconfig $interface \
34		inet $new_ip_address \
35		netmask $new_subnet_mask \
36		broadcast $new_broadcast_address \
37		$medium
38
39	# XXX Original TIMEOUT code did not do this unless $new_routers was set?
40	#route add $new_ip_address 127.0.0.1 >/dev/null 2>&1
41}
42
43delete_old_alias() {
44	if [ -n "$alias_ip_address" ]; then
45		ifconfig $interface inet $alias_ip_address delete > /dev/null 2>&1
46#		#route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
47	fi
48}
49
50add_new_alias() {
51	if [ -n "$alias_ip_address" ]; then
52		ifconfig $interface inet $alias_ip_address alias netmask \
53		    $alias_subnet_mask
54		#route add $alias_ip_address 127.0.0.1
55	fi
56}
57
58delete_old_routes() {
59	if [ -n "$old_static_routes" ]; then
60		set $old_static_routes
61		while [ $# -gt 1 ]; do
62			route delete "$1" "$2"
63			shift; shift
64		done
65	fi
66
67	arp -dan
68}
69
70add_new_routes() {
71	for router in $new_routers; do
72		route -q delete default
73		if [ "$new_ip_address" = "$router" ]; then
74			route -q add default -iface $router
75		else
76			route -q add default $router
77		fi
78		# 2nd and subsequent default routers error out, so explicitly
79		# stop processing the list after the first one.
80		break
81	done
82
83	if [ -n "$new_static_routes" ]; then
84		set $new_static_routes
85		while [ $# -gt 1 ]; do
86			route add $1 $2
87			shift; shift
88		done
89	fi
90}
91
92add_new_resolv_conf() {
93	# Create resolv.conf when either $new_domain_name_servers or
94	# $new_domain_name are provided. As reported in PR#3135, some ISPs
95	# provide only $new_domain_name_servers.
96
97	rm -f /etc/resolv.conf.std
98
99	if [ -n "$new_domain_name" ]; then
100		echo "search $new_domain_name" >>/etc/resolv.conf.std
101	fi
102
103	if [ -n "$new_domain_name_servers" ]; then
104		for nameserver in $new_domain_name_servers; do
105			echo "nameserver $nameserver" >>/etc/resolv.conf.std
106		done
107	fi
108
109	if [ -f /etc/resolv.conf.std ]; then
110		if [ -f /etc/resolv.conf.tail ]; then
111			cat /etc/resolv.conf.tail >>/etc/resolv.conf.std
112		fi
113
114		# In case (e.g. during OpenBSD installs) /etc/resolv.conf
115		# is a symbolic link, take care to preserve the link and write
116		# the new data in the correct location.
117
118		if [ -f /etc/resolv.conf ]; then
119			cat /etc/resolv.conf > /etc/resolv.conf.save
120		fi
121		cat /etc/resolv.conf.std > /etc/resolv.conf
122		rm -f /etc/resolv.conf.std
123
124		# Try to ensure correct ownership and permissions.
125		chown -RL root:wheel /etc/resolv.conf
126		chmod -RL 644 /etc/resolv.conf
127
128		return 0
129	fi
130
131	return 1
132}
133
134#
135# Start of active code.
136#
137
138case $reason in
139MEDIUM)
140	ifconfig $interface $medium
141	sleep 1
142	;;
143
144PREINIT)
145	delete_old_alias
146	ifconfig $interface up
147	;;
148
149ARPCHECK|ARPSEND)
150	;;
151
152BOUND|RENEW|REBIND|REBOOT)
153	if [ -n "$old_ip_address" ]; then
154		if [ "$old_ip_address" != "$alias_ip_address" ]; then
155			delete_old_alias
156		fi
157		if [ "$old_ip_address" != "$new_ip_address" ]; then
158			delete_old_address
159			delete_old_routes
160		fi
161	fi
162	if [ "$reason" = BOUND ] ||
163	   [ "$reason" = REBOOT ] ||
164	   [ -z "$old_ip_address" ] ||
165	   [ "$old_ip_address" != "$new_ip_address" ]; then
166		add_new_address
167		add_new_routes
168	fi
169	if [ "$new_ip_address" != "$alias_ip_address" ]; then
170		add_new_alias
171	fi
172	add_new_resolv_conf
173	;;
174
175EXPIRE|FAIL)
176	delete_old_alias
177	if [ -n "$old_ip_address" ]; then
178		delete_old_address
179		delete_old_routes
180	fi
181	# XXX Why add alias we just deleted above?
182	add_new_alias
183	if [ -f /etc/resolv.conf.save ]; then
184		cat /etc/resolv.conf.save > /etc/resolv.conf
185	fi
186	;;
187
188TIMEOUT)
189	delete_old_alias
190	add_new_address
191	sleep 1
192	if [ -n "$new_routers" ]; then
193		set "$new_routers"
194		if ping -q -c 1 -w 1 "$1"; then
195			if [ "$new_ip_address" != "$alias_ip_address" ]; then
196				add_new_alias
197			fi
198			add_new_routes
199			if add_new_resolv_conf; then
200				exit 0
201			fi
202		fi
203	fi
204	ifconfig $interface inet $new_ip_address delete $medium
205	# XXX Why not a delete_old_address as before all other invocations of
206	#     delete_old_routes?
207	delete_old_routes
208	exit 1
209	;;
210esac
211
212exit 0
213