Projet

Général

Profil

Télécharger (10,8 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / sbin / dhclient-script @ ff3da5db

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
PFCTL=/sbin/pfctl
31

    
32
LOCALHOST=127.0.0.1
33

    
34
if [ -x /usr/bin/logger ]; then
35
	LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
36
else
37
	LOGGER="echo"
38
fi
39

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

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

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

    
59
delete_old_states() {
60
	$LOGGER "Starting delete_old_states()"
61
	_FLUSHED=0
62
	# If the IP changed, remove states from the old one
63
	if [ -f /var/db/${interface}_ip ]; then
64
		OLD_IP=`cat /var/db/${interface}_ip`
65
		$LOGGER "Comparing IPs: Old: ${OLD_IP} New: ${new_ip_address}"
66
		if [ -n "${OLD_IP}" ] && [ "${OLD_IP}" != "${new_ip_address}" ]; then
67
			$LOGGER "Removing states from old IP '${OLD_IP}' (new IP '${new_ip_address}')"
68
			${PFCTL} -i $interface -Fs
69
			${PFCTL} -K ${OLD_IP}/32
70
			_FLUSHED=1
71
		fi
72
	fi
73
	# Delete states through old gateway if it's not the same
74
	OLD_ROUTER=""
75
	if [ -n "${old_routers}" ]; then
76
		OLD_ROUTER=$old_routers
77
	elif [ -f /tmp/${interface}_router ]; then
78
		OLD_ROUTER=`cat /tmp/${interface}_router`
79
	fi
80
	if [ ${_FLUSHED} -eq 0 -a -n "${OLD_ROUTER}" ]; then
81
		$LOGGER "Comparing Routers: Old: ${OLD_ROUTER} New: ${new_routers}"
82
		if [ "${OLD_ROUTER}" != "${new_routers}" ]; then
83
			$LOGGER "Removing states through old gateway '${OLD_ROUTER}' (new gateway '${new_routers}')"
84
			${PFCTL} -i $interface -Fs
85
		fi
86
	fi
87
}
88

    
89
delete_old_address() {
90
	/bin/rm -f /var/db/${interface}_ip
91
	$IFCONFIG $interface inet -alias $old_ip_address $medium
92
}
93

    
94
add_new_address() {
95

    
96
	$LOGGER "Starting add_new_address()"
97

    
98
	$LOGGER "ifconfig $interface inet $new_ip_address netmask $new_subnet_mask broadcast $new_broadcast_address $medium"
99

    
100
	$IFCONFIG $interface \
101
		inet $new_ip_address \
102
		netmask $new_subnet_mask \
103
		broadcast $new_broadcast_address \
104
		$medium
105
	$IFCONFIG $interface setfirst $new_ip_address
106

    
107
	$LOGGER "New IP Address ($interface): $new_ip_address"
108
	$LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
109
	$LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
110
	$LOGGER "New Routers ($interface): $new_routers"
111

    
112

    
113
	# This is necessary otherwise apinger will try to ping all 1s address
114
	if [ -n "$new_routers" ] && [ "$new_routers" != "255.255.255.255" ]; then
115
		echo $new_routers > /tmp/${interface}_router
116
	fi
117
	echo $new_ip_address > /var/db/${interface}_ip
118
}
119

    
120
delete_old_alias() {
121
	if [ -n "$alias_ip_address" ]; then
122
		$IFCONFIG $interface inet -alias $alias_ip_address > /dev/null 2>&1
123
		$ROUTE delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
124
	fi
125
}
126

    
127
add_new_alias() {
128
	if [ -n "$alias_ip_address" ]; then
129
		$IFCONFIG $interface inet alias $alias_ip_address netmask \
130
		    $alias_subnet_mask
131
		$ROUTE add $alias_ip_address $LOCALHOST
132
	fi
133
}
134

    
135
fill_classless_routes() {
136
	set $1
137
	while [ $# -ge 5 ]; do
138
		if [ $1 -eq 0 ]; then
139
			route="default"
140
		elif [ $1 -le 8 ]; then
141
			route="$2.0.0.0/$1"
142
			shift
143
		elif [ $1 -le 16 ]; then
144
			route="$2.$3.0.0/$1"
145
			shift; shift
146
		elif [ $1 -le 24 ]; then
147
			route="$2.$3.$4.0/$1"
148
			shift; shift; shift
149
		else
150
			route="$2.$3.$4.$5/$1"
151
			shift; shift; shift; shift
152
		fi
153
		shift
154
		router="$1.$2.$3.$4"
155
		classless_routes="$classless_routes $route $router"
156
		shift; shift; shift; shift
157
	done
158
}
159

    
160
delete_old_routes() {
161
	$LOGGER "Deleting old routes"
162

    
163
	if [ -n "$old_classless_routes" ]; then
164
		fill_classless_routes "$old_classless_routes"
165
		set $classless_routes
166
		while [ $# -gt 1 ]; do
167
			route delete "$1" "$2"
168
			shift; shift
169
		done
170
		return 0;
171
	fi
172

    
173
	# Only allow the default route to be overridden if it's on our own interface
174
	if [ -f "/tmp/${interface}_defaultgw" ]; then
175
		for router in $old_routers; do
176
			$ROUTE delete default $router >/dev/null 2>&1
177
			/bin/rm -f /tmp/${interface}_router
178
		done
179
	fi
180

    
181
	if [ -n "$old_static_routes" ]; then
182
		set $old_static_routes
183
		while [ $# -gt 1 ]; do
184
			$ROUTE delete "$1" "$2"
185
			shift; shift
186
                        /bin/rm -f /tmp/${interface}_router
187
		done
188
	fi
189

    
190
	arp_flush
191
}
192

    
193
add_new_routes() {
194
	$LOGGER "Adding new routes to interface: $interface"
195

    
196
	# RFC 3442: If the DHCP server returns both a Classless Static
197
	# Routes option and a Router option, the DHCP client MUST ignore
198
	# the Router option.
199
	#
200
	# DHCP clients that support this option (Classless Static Routes)
201
	# MUST NOT install the routes specified in the Static Routes
202
	# option (option code 33) if both a Static Routes option and the
203
	# Classless Static Routes option are provided.
204
	if [ -n "$new_classless_routes" ]; then
205
		fill_classless_routes "$new_classless_routes"
206
		$LOGGER "New Classless Static Routes ($interface): $classless_routes"
207
		set $classless_routes
208
		while [ $# -gt 1 ]; do
209
			if [ "0.0.0.0" = "$2" ]; then
210
				route add "$1" -iface "$interface"
211
			else
212
				route add "$1" "$2"
213
			fi
214
			shift; shift
215
		done
216
		return
217
	fi
218

    
219
	ADDED_ROUTE=no
220
	EXISTSGW=`/bin/ls -l /tmp/*_defaultgw | /usr/bin/wc -l`
221
	# Only allow the default route to be overridden if it's on our own interface
222
	if [ -f "/tmp/${interface}_defaultgw" -o $EXISTSGW -eq 0 ]; then
223
		$ROUTE delete default
224
		for router in $new_routers; do
225
			if [ "$new_ip_address" = "$router" -o "$router" = "255.255.255.255" ]; then
226
				$ROUTE add default -iface $interface
227
				echo $ROUTE add default -iface $interface | $LOGGER
228
				# NOTE: Do not activate this for all ones address since pf(4) will try to forward packets to it.
229
				if [ "$new_ip_address" = "$router" ]; then
230
					echo $router > /tmp/${interface}_router
231
				fi
232
			else
233
				$ROUTE add default $router
234
				echo $ROUTE add default $router | $LOGGER
235
                       		echo $router > /tmp/${interface}_router
236
			fi
237
			ADDED_ROUTE=yes
238
			# 2nd and subsequent default routers error out, so explicitly
239
			# stop processing the list after the first one.
240
			break
241
		done
242
	fi
243

    
244
	if [ -n "$new_static_routes" ]; then
245
		$LOGGER "New Static Routes ($interface): $new_static_routes"
246
		set $new_static_routes
247
		while [ $# -gt 1 ]; do
248
			$ROUTE add $1 $2
249
			if [ "$ADDED_ROUTE" = "no" ]; then
250
                        	echo $2 > /tmp/${interface}_router
251
			fi
252
			shift; shift
253
		done
254
	fi
255
}
256

    
257
add_new_resolv_conf() {
258
	$LOGGER "Creating resolv.conf"
259
	if [ -f "/var/etc/nameserver_$interface" ]; then
260
		# Remove old entries
261
		for nameserver in `cat /var/etc/nameserver_$interface`; do
262
			$ROUTE delete $nameserver >/dev/null 2>&1
263
		done
264
	fi
265
	if [ -n "$new_domain_name_servers" ]; then 
266
		/bin/rm -f /var/etc/nameserver_$interface
267
		ALLOWOVERRIDE=`/usr/bin/grep dnsallowoverride /conf/config.xml | /usr/bin/wc -l`
268
		for nameserver in $new_domain_name_servers; do
269
			# Add a route to the nameserver out the correct interface
270
			# so that mulitple wans work correctly with multiple dns
271
			# also backup the nameserver for later route removal
272
			if [ $ALLOWOVERRIDE -gt 0 ]; then
273
				echo $nameserver >>/var/etc/nameserver_$interface
274
				$ROUTE add $nameserver -iface $interface
275
			fi
276
		done
277
		echo $new_domain_name >/var/etc/searchdomain_$interface
278
	fi
279

    
280
	return 0
281
}
282

    
283
# Notify rc.newwanip of changes to an interface
284
notify_rc_newwanip() {
285
	/usr/local/sbin/pfSctl -c "interface newip $interface"
286
}
287

    
288
#
289
# Start of active code.
290
#
291

    
292
# Invoke the local dhcp client enter hooks, if they exist.
293
if [ -f /etc/dhclient-enter-hooks ]; then
294
	$LOGGER "dhclient-enter-hooks"
295
	exit_status=0
296
	. /etc/dhclient-enter-hooks
297
	# allow the local script to abort processing of this state
298
	# local script must set exit_status variable to nonzero.
299
	if [ $exit_status -ne 0 ]; then
300
		exit $exit_status
301
	fi
302
fi
303

    
304
#if [ -x $ROUTE ]; then
305
#	if_defaultroute=`$ROUTE -n get -inet default | $GREP interface | $AWK '{print $2}'`
306
#else
307
#	$LOGGER "if_defaultroute"
308
#	if_defaultroute="x"
309
#fi
310

    
311
$LOGGER $reason
312
case $reason in
313
MEDIUM)
314
	$IFCONFIG $interface $medium
315
	$IFCONFIG $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
316
	/bin/sleep 1
317
	;;
318

    
319
PREINIT)
320
	delete_old_alias
321
	$IFCONFIG $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
322
	delete_old_states
323
	/bin/rm -f /tmp/${interface}_router
324
	;;
325

    
326
ARPCHECK|ARPSEND)
327
	;;
328

    
329
BOUND|RENEW|REBIND|REBOOT)
330
	check_hostname
331
	changes="no"
332
	if [ "$old_ip_address" != "$new_ip_address" ]; then
333
		delete_old_states
334
	fi
335
	if [ -n "$old_ip_address" ]; then
336
		if [ -n "$alias_ip_address" ] && \
337
		   [ "$old_ip_address" != "$alias_ip_address" ]; then
338
			delete_old_alias
339
			changes="yes"
340
		fi
341
		if [ "$old_ip_address" != "$new_ip_address" ]; then
342
			delete_old_address
343
			delete_old_routes
344
			changes="yes"
345
		fi
346
	fi
347
	if [ "$reason" = BOUND ] || \
348
	   [ "$reason" = REBOOT ] || \
349
	   [ -z "$old_ip_address" ] || \
350
	   [ "$old_ip_address" != "$new_ip_address" ]; then
351
		add_new_address
352
		add_new_routes
353
		changes="yes"
354
	fi
355
	if [ -n "$alias_ip_address" ] && \
356
       [ "$new_ip_address" != "$alias_ip_address" ]; then
357
		add_new_alias
358
		changes="yes"
359
	fi
360
	add_new_resolv_conf
361
	if [ "$changes" = "yes" ] ; then
362
		notify_rc_newwanip
363
	fi
364
	;;
365

    
366
EXPIRE|FAIL)
367
	delete_old_alias
368
	delete_old_states
369
	if [ -n "$old_ip_address" ]; then
370
		delete_old_address
371
		delete_old_routes
372
	fi
373
	;;
374

    
375
TIMEOUT)
376
	delete_old_alias
377
	add_new_address
378
	/bin/sleep 1
379
	if [ -n "$new_routers" ]; then
380
		$LOGGER "New Routers ($interface): $new_routers"
381
		set "$new_routers"
382
		if /sbin/ping -q -c 1 -t 1 "$1"; then
383
			if [ "$new_ip_address" != "$alias_ip_address" ]; then
384
				add_new_alias
385
			fi
386
			add_new_routes
387
			if add_new_resolv_conf; then
388
				notify_rc_newwanip
389
			fi
390
		fi
391
	fi
392
	$IFCONFIG $interface inet -alias $new_ip_address $medium
393
	delete_old_states
394
	delete_old_routes
395
	;;
396
esac
397

    
398
# Invoke the local dhcp client exit hooks, if they exist.
399
if [ -f /etc/dhclient-exit-hooks ]; then
400
	$LOGGER "dhclient-exit-hooks"
401
	exit_status=0
402
	. /etc/dhclient-exit-hooks
403
	# allow the local script to abort processing of this state
404
	# local script must set exit_status variable to nonzero.
405
	exit $exit_status
406
fi
(2-2/2)