Project

General

Profile

Download (8.33 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2
# $Id$
3
# $OpenBSD: dhclient-script,v 1.6 2004/05/06 18:22:41 claudio Exp $
4
# $FreeBSD: src/sbin/dhclient/dhclient-script,v 1.4 2005/06/10 03:41:18 brooks Exp $
5
#
6
# Copyright (c) 2003 Kenneth R Westerback <krw@openbsd.org>
7
#
8
# Permission to use, copy, modify, and distribute this software for any
9
# purpose with or without fee is hereby granted, provided that the above
10
# copyright notice and this permission notice appear in all copies.
11
#
12
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
#
20

    
21
NETSTAT=/usr/bin/netstat
22
GREP=/usr/bin/grep
23
AWK=/usr/bin/awk
24
HOSTNAME=/bin/hostname
25
GREP=/usr/bin/grep
26
ROUTE=/sbin/route
27
SED=/usr/bin/sed
28
ARP=/usr/sbin/arp
29
IFCONFIG=/sbin/ifconfig
30

    
31
LOCALHOST=127.0.0.1
32

    
33
# Ensure check_reload_status is running at all times.
34
PS_COUNT=`ps awux | grep -v grep | grep check_reload_status | wc -l`
35
if [ $PS_COUNT -lt 1 ]; then
36
        echo "check_reload_status not running, restarting."  | logger -p daemon.info -i -t dhclient-script
37
        /usr/bin/nice -n20 /usr/local/sbin/check_reload_status 2>/dev/null
38
fi
39

    
40
if [ -x /usr/bin/logger ]; then
41
	LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
42
else
43
	LOGGER=echo
44
fi
45

    
46
#
47
# Helper functions that implement common actions.
48
#
49

    
50
check_hostname() {
51
	current_hostname=`$HOSTNAME`
52
	if [ -z "$current_hostname" ]; then
53
		$LOGGER "New Hostname ($interface): $new_host_name"
54
		$HOSTNAME $new_host_name
55
	elif [ "$current_hostname" = "$old_host_name" -a \
56
	       "$new_host_name" != "$old_host_name" ]; then
57
		$LOGGER "New Hostname ($interface): $new_host_name"
58
		$HOSTNAME $new_host_name
59
	fi
60
}
61

    
62
arp_flush() {
63
	$ARP -an -i $interface | \
64
		$SED -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \
65
		/bin/sh >/dev/null 2>&1
66
}
67

    
68
delete_old_address() {
69
	$IFCONFIG $interface inet -alias $old_ip_address $medium
70
}
71

    
72
add_new_address() {
73

    
74
	# Kill off old pftpx process.
75
	kill `ps awux | grep "pftpx -b" | grep -v grep | grep $old_ip_address | cut -d" " -f5`
76

    
77
	$IFCONFIG $interface \
78
		inet $new_ip_address \
79
		netmask $new_subnet_mask \
80
		broadcast $new_broadcast_address \
81
		$medium
82

    
83
	$LOGGER "New IP Address ($interface): $new_ip_address"
84
	$LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
85
	$LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
86
	$LOGGER "New Routers ($interface): $new_routers"
87

    
88
        echo $new_routers > /tmp/${interface}_router
89
	echo $new_ip_address > /var/db/${interface}_ip
90
}
91

    
92
delete_old_alias() {
93
	if [ -n "$alias_ip_address" ]; then
94
		$IFCONFIG $interface inet -alias $alias_ip_address > /dev/null 2>&1
95
		$ROUTE delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
96
	fi
97
}
98

    
99
add_new_alias() {
100
	if [ -n "$alias_ip_address" ]; then
101
		$IFCONFIG $interface inet alias $alias_ip_address netmask \
102
		    $alias_subnet_mask
103
		$ROUTE add $alias_ip_address $LOCALHOST
104
	fi
105
}
106

    
107
delete_old_routes() {
108
	$ROUTE delete "$old_ip_address" $LOCALHOST >/dev/null 2>&1
109
	# Only allow the default route to be overridden if it's on our own interface
110
	DEFAULTROUTE_IFACE=`route get default | grep interface | awk '{print $2};'`
111
	if [ -z "${DEFAULTROUTE_IFACE}" -o "{$interface}" = "${DEFAULTROUTE_IFACE}" ]; then
112
		for router in $old_routers; do
113
			if [ $if_defaultroute = x -o $if_defaultroute = $interface ]; then
114
				$ROUTE delete default $route >/dev/null 2>&1
115
                        	/bin/rm -f /tmp/${interface}_router
116
			fi
117
		done
118
	fi
119

    
120
	if [ -n "$old_static_routes" ]; then
121
		set $old_static_routes
122
		while [ $# -gt 1 ]; do
123
			$ROUTE delete "$1" "$2"
124
			shift; shift
125
                        /bin/rm -f /tmp/${interface}_router
126
		done
127
	fi
128

    
129
	arp_flush
130
}
131

    
132
add_new_routes() {
133
	$ROUTE add $new_ip_address $LOCALHOST >/dev/null 2>&1
134

    
135
	# Only allow the default route to be overridden if it's on our own interface
136
	DEFAULTROUTE_IFACE=`route get default | grep interface | awk '{print $2};'`
137
	if [ -z "${DEFAULTROUTE_IFACE}" -o "{$interface}" = "${DEFAULTROUTE_IFACE}" ]; then
138
		for router in $new_routers; do
139
			if [ "$new_ip_address" = "$router" ]; then
140
				$ROUTE add default -iface $router
141
				#>/dev/null 2>&1
142
				echo $ROUTE add default -iface $router | $LOGGER
143
				echo $new_routers > /tmp/${interface}_router
144
			else
145
				$ROUTE add default $router
146
				echo $ROUTE add default $router | $LOGGER
147
				#>/dev/null 2>&1
148
                       		echo $new_routers > /tmp/${interface}_router
149
			fi
150
			# 2nd and subsequent default routers error out, so explicitly
151
			# stop processing the list after the first one.
152
			break
153
		done
154
	fi
155

    
156
	if [ -n "$new_static_routes" ]; then
157
		$LOGGER "New Static Routes ($interface): $new_static_routes"
158
		set $new_static_routes
159
		while [ $# -gt 1 ]; do
160
			$ROUTE add $1 $2
161
			shift; shift
162
                        echo $new_routers > /tmp/${interface}_router
163
		done
164
	fi
165

    
166
	# last ditch effort if no route exists.
167
	DEFAULTROUTE=`$NETSTAT -rn | $GREP default`
168
	if [ -z "${DEFAULTROUTE}" ]; then
169
	    $ROUTE add default -iface $interface
170
	fi
171

    
172
}
173

    
174
add_new_resolv_conf() {
175
	/bin/rm -f /var/etc/nameservers.conf
176
	if [ ! -f "/var/etc/nameserver_$interface" ]; then
177
		# Make sure file exists to avoid errors
178
		touch /var/etc/nameserver_$interface
179
	fi
180
	if [ -n "$new_domain_name_servers" ]; then
181
		# Remove old entries
182
		for nameserver in `cat /var/etc/nameserver_$interface`; do
183
			route delete $nameserver
184
		done
185
		rm -f /var/etc/nameserver_$interface
186
		for nameserver in $new_domain_name_servers; do
187
			echo $nameserver >>/var/etc/nameservers.conf
188
			# Add a route to the nameserver out the correct interface
189
			# so that mulitple wans work correctly with multiple dns
190
			# also backup the nameserver for later route removal
191
			echo $nameserver >>/var/etc/nameserver_$interface
192
			route add $nameserver -if $interface
193
		done
194
		echo $new_domain_name >/var/etc/defaultdomain.conf
195
	fi
196

    
197
	return 0
198
}
199

    
200
# Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
201
exit_with_hooks() {
202
	echo $interface > /tmp/rc.newwanip
203
	echo $old_ip_address > /tmp/rc.newwanip_oldip
204
	# probably should do something with exit status of the local script
205
	exit 0
206
}
207

    
208
#
209
# Start of active code.
210
#
211

    
212
# Invoke the local dhcp client enter hooks, if they exist.
213
if [ -f /etc/dhclient-enter-hooks ]; then
214
	exit_status=0
215
	. /etc/dhclient-enter-hooks
216
	# allow the local script to abort processing of this state
217
	# local script must set exit_status variable to nonzero.
218
	if [ $exit_status -ne 0 ]; then
219
		exit $exit_status
220
	fi
221
fi
222

    
223
if [ -x $NETSTAT ]; then
224
	if_defaulroute=`$NETSTAT -rn | $GREP "^default" | $AWK '{print $6}'`
225
else
226
	if_defaultroute="x"
227
fi
228

    
229
case $reason in
230
MEDIUM)
231
	$IFCONFIG $interface $medium
232
	$IFCONFIG $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
233
	/bin/sleep 1
234
	;;
235

    
236
PREINIT)
237
	delete_old_alias
238
	$IFCONFIG $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
239
        /bin/rm -f /tmp/${interface}_router
240
	;;
241

    
242
ARPCHECK|ARPSEND)
243
	;;
244

    
245
BOUND|RENEW|REBIND|REBOOT)
246
	check_hostname
247
	if [ -n "$old_ip_address" ]; then
248
		if [ "$old_ip_address" != "$alias_ip_address" ]; then
249
			delete_old_alias
250
		fi
251
		if [ "$old_ip_address" != "$new_ip_address" ]; then
252
			delete_old_address
253
			delete_old_routes
254
		fi
255
	fi
256
	if [ "$reason" = BOUND ] || \
257
	   [ "$reason" = REBOOT ] || \
258
	   [ -z "$old_ip_address" ] || \
259
	   [ "$old_ip_address" != "$new_ip_address" ]; then
260
		add_new_address
261
		add_new_routes
262
	fi
263
	if [ "$new_ip_address" != "$alias_ip_address" ]; then
264
		add_new_alias
265
	fi
266
	add_new_resolv_conf
267
	;;
268

    
269
EXPIRE|FAIL)
270
	delete_old_alias
271
	if [ -n "$old_ip_address" ]; then
272
		delete_old_address
273
		delete_old_routes
274
	fi
275
	# XXX Why add alias we just deleted above?
276
	add_new_alias
277
	if [ -f /var/etc/resolv.conf.save ]; then
278
		#cat /var/etc/resolv.conf.save > /var/etc/resolv.conf
279
	fi
280
	;;
281

    
282
TIMEOUT)
283
	delete_old_alias
284
	add_new_address
285
	/bin/sleep 1
286
	if [ -n "$new_routers" ]; then
287
		$LOGGER "New Routers ($interface): $new_routers"
288
		set "$new_routers"
289
		if /sbin/ping -q -c 1 -w 1 "$1"; then
290
			if [ "$new_ip_address" != "$alias_ip_address" ]; then
291
				add_new_alias
292
			fi
293
			add_new_routes
294
			if add_new_resolv_conf; then
295
				exit_with_hooks
296
			fi
297
		fi
298
	fi
299
	$IFCONFIG $interface inet -alias $new_ip_address $medium
300
	delete_old_routes
301
	exit_with_hooks
302
	;;
303
esac
304

    
305
exit_with_hooks 0
(2-2/2)