Project

General

Profile

Download (6.74 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
if [ -x /usr/bin/logger ]; then
34
	LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
35
else
36
	LOGGER=echo
37
fi
38

    
39
#
40
# Helper functions that implement common actions.
41
#
42

    
43
check_hostname() {
44
	current_hostname=`$HOSTNAME`
45
	if [ -z "$current_hostname" ]; then
46
		$LOGGER "New Hostname ($interface): $new_host_name"
47
		$HOSTNAME $new_host_name
48
	elif [ "$current_hostname" = "$old_host_name" -a \
49
	       "$new_host_name" != "$old_host_name" ]; then
50
		$LOGGER "New Hostname ($interface): $new_host_name"
51
		$HOSTNAME $new_host_name
52
	fi
53
}
54

    
55
arp_flush() {
56
	$ARP -an -i $interface | \
57
		$SED -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' | \
58
		/bin/sh >/dev/null 2>&1
59
}
60

    
61
delete_old_address() {
62
	$IFCONFIG $interface inet -alias $old_ip_address $medium
63
}
64

    
65
add_new_address() {
66
	$IFCONFIG $interface \
67
		inet $new_ip_address \
68
		netmask $new_subnet_mask \
69
		broadcast $new_broadcast_address \
70
		$medium
71

    
72
	$LOGGER "New IP Address ($interface): $new_ip_address"
73
	$LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
74
	$LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
75
	$LOGGER "New Routers ($interface): $new_routers"
76
        
77
        echo $new_routers > /tmp/${interface}_router
78
        
79
}
80

    
81
delete_old_alias() {
82
	if [ -n "$alias_ip_address" ]; then
83
		$IFCONFIG $interface inet -alias $alias_ip_address > /dev/null 2>&1
84
		$ROUTE delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
85
	fi
86
}
87

    
88
add_new_alias() {
89
	if [ -n "$alias_ip_address" ]; then
90
		$IFCONFIG $interface inet alias $alias_ip_address netmask \
91
		    $alias_subnet_mask
92
		$ROUTE add $alias_ip_address $LOCALHOST
93
	fi
94
}
95

    
96
delete_old_routes() {
97
	$ROUTE delete "$old_ip_address" $LOCALHOST >/dev/null 2>&1
98
	for router in $old_routers; do
99
		if [ $if_defaultroute = x -o $if_defaultroute = $interface ]; then
100
			$ROUTE delete default $route >/dev/null 2>&1
101
                        /bin/rm -f /tmp/${interface}_router
102
		fi
103
	done
104

    
105
	if [ -n "$old_static_routes" ]; then
106
		set $old_static_routes
107
		while [ $# -gt 1 ]; do
108
			$ROUTE delete "$1" "$2"
109
			shift; shift
110
                        /bin/rm -f /tmp/${interface}_router
111
		done
112
	fi
113

    
114
	arp_flush
115
}
116

    
117
add_new_routes() {
118
	$ROUTE add $new_ip_address $LOCALHOST >/dev/null 2>&1
119
	for router in $new_routers; do
120
		if [ "$new_ip_address" = "$router" ]; then
121
			$ROUTE add default -iface $router
122
			#>/dev/null 2>&1
123
			echo $ROUTE add default -iface $router | $LOGGER
124
			echo $new_routers > /tmp/${interface}_router
125
		else
126
			$ROUTE add default $router
127
			echo $ROUTE add default $router | $LOGGER
128
			#>/dev/null 2>&1
129
                        echo $new_routers > /tmp/${interface}_router
130
		fi
131
		# 2nd and subsequent default routers error out, so explicitly
132
		# stop processing the list after the first one.
133
		break
134
	done
135

    
136
	if [ -n "$new_static_routes" ]; then
137
		$LOGGER "New Static Routes ($interface): $new_static_routes"
138
		set $new_static_routes
139
		while [ $# -gt 1 ]; do
140
			$ROUTE add $1 $2
141
			shift; shift
142
                        echo $new_routers > /tmp/${interface}_router
143
		done
144
	fi
145
	
146
	# last ditch effort if no route exists.
147
	DEFAULTROUTE=`$NETSTAT -rn | $GREP default`
148
	if [ -z "${DEFAULTROUTE}" ]; then
149
	    $ROUTE add default -iface $interface
150
	fi
151
	
152
}
153

    
154
add_new_resolv_conf() {
155

    
156
	/bin/rm -f /var/etc/nameservers.conf
157

    
158
	if [ -n "$new_domain_name_servers" ]; then
159
		for nameserver in $new_domain_name_servers; do
160
			echo $nameserver >>/var/etc/nameservers.conf
161
		done
162
		echo $new_domain_name >/var/etc/defaultdomain.conf
163
	fi
164
	
165
	return 0
166
}
167

    
168
# Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
169
exit_with_hooks() {
170
	/etc/rc.newwanip
171
	# probably should do something with exit status of the local script
172
	exit 0
173
}
174

    
175
#
176
# Start of active code.
177
#
178

    
179
# Invoke the local dhcp client enter hooks, if they exist.
180
if [ -f /etc/dhclient-enter-hooks ]; then
181
	exit_status=0
182
	. /etc/dhclient-enter-hooks
183
	# allow the local script to abort processing of this state
184
	# local script must set exit_status variable to nonzero.
185
	if [ $exit_status -ne 0 ]; then
186
		exit $exit_status
187
	fi
188
fi
189

    
190
if [ -x $NETSTAT ]; then
191
	if_defaulroute=`$NETSTAT -rn | $GREP "^default" | $AWK '{print $6}'`
192
else
193
	if_defaultroute="x"
194
fi
195

    
196
case $reason in
197
MEDIUM)
198
	$IFCONFIG $interface $medium
199
	$IFCONFIG $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
200
	/bin/sleep 1
201
	;;
202

    
203
PREINIT)
204
	delete_old_alias
205
	$IFCONFIG $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
206
        /bin/rm -f /tmp/${interface}_router
207
	;;
208

    
209
ARPCHECK|ARPSEND)
210
	;;
211

    
212
BOUND|RENEW|REBIND|REBOOT)
213
	check_hostname
214
	if [ -n "$old_ip_address" ]; then
215
		if [ "$old_ip_address" != "$alias_ip_address" ]; then
216
			delete_old_alias
217
		fi
218
		if [ "$old_ip_address" != "$new_ip_address" ]; then
219
			delete_old_address
220
			delete_old_routes
221
		fi
222
	fi
223
	if [ "$reason" = BOUND ] || \
224
	   [ "$reason" = REBOOT ] || \
225
	   [ -z "$old_ip_address" ] || \
226
	   [ "$old_ip_address" != "$new_ip_address" ]; then
227
		add_new_address
228
		add_new_routes
229
	fi
230
	if [ "$new_ip_address" != "$alias_ip_address" ]; then
231
		add_new_alias
232
	fi
233
	add_new_resolv_conf
234
	;;
235

    
236
EXPIRE|FAIL)
237
	delete_old_alias
238
	if [ -n "$old_ip_address" ]; then
239
		delete_old_address
240
		delete_old_routes
241
	fi
242
	# XXX Why add alias we just deleted above?
243
	add_new_alias
244
	if [ -f /var/etc/resolv.conf.save ]; then
245
		#cat /var/etc/resolv.conf.save > /var/etc/resolv.conf
246
	fi
247
	;;
248

    
249
TIMEOUT)
250
	delete_old_alias
251
	add_new_address
252
	/bin/sleep 1
253
	if [ -n "$new_routers" ]; then
254
		$LOGGER "New Routers ($interface): $new_routers"
255
		set "$new_routers"
256
		if /sbin/ping -q -c 1 -w 1 "$1"; then
257
			if [ "$new_ip_address" != "$alias_ip_address" ]; then
258
				add_new_alias
259
			fi
260
			add_new_routes
261
			if add_new_resolv_conf; then
262
				exit_with_hooks
263
			fi
264
		fi
265
	fi
266
	$IFCONFIG $interface inet -alias $new_ip_address $medium
267
	delete_old_routes
268
	exit_with_hooks
269
	;;
270
esac
271

    
272
exit_with_hooks 0
(2-2/2)