Project

General

Profile

Download (6.35 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2
#
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

    
22
NETSTAT=/usr/bin/netstat
23
GREP=/usr/bin/grep
24
AWK=/usr/bin/awk
25
HOSTNAME=/bin/hostname
26

    
27
LOCALHOST=127.0.0.1
28

    
29
if [ -x /usr/bin/logger ]; then
30
	LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
31
else
32
	LOGGER=echo
33
fi
34

    
35
#
36
# Helper functions that implement common actions.
37
#
38

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

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

    
57
delete_old_address() {
58
	ifconfig $interface inet -alias $old_ip_address $medium
59
}
60

    
61
add_new_address() {
62
	ifconfig $interface \
63
		inet $new_ip_address \
64
		netmask $new_subnet_mask \
65
		broadcast $new_broadcast_address \
66
		$medium
67

    
68
	$LOGGER "New IP Address ($interface): $new_ip_address"
69
	$LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
70
	$LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
71
	$LOGGER "New Routers ($interface): $new_routers"
72
        
73
        echo $new_routers > /tmp/${interface}_router
74
        
75
}
76

    
77
delete_old_alias() {
78
	if [ -n "$alias_ip_address" ]; then
79
		ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
80
		route delete $alias_ip_address $LOCALHOST > /dev/null 2>&1
81
	fi
82
}
83

    
84
add_new_alias() {
85
	if [ -n "$alias_ip_address" ]; then
86
		ifconfig $interface inet alias $alias_ip_address netmask \
87
		    $alias_subnet_mask
88
		route add $alias_ip_address $LOCALHOST
89
	fi
90
}
91

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

    
101
	if [ -n "$old_static_routes" ]; then
102
		set $old_static_routes
103
		while [ $# -gt 1 ]; do
104
			route delete "$1" "$2"
105
			shift; shift
106
                        rm -f /tmp/${interface}_router
107
		done
108
	fi
109

    
110
	arp_flush
111
}
112

    
113
add_new_routes() {
114
	route add $new_ip_address $LOCALHOST >/dev/null 2>&1
115
	for router in $new_routers; do
116
		if [ "$new_ip_address" = "$router" ]; then
117
			route add default -iface $router >/dev/null 2>&1
118
		else
119
			route add default $router >/dev/null 2>&1
120
                        echo $new_routers > /tmp/${interface}_router
121
		fi
122
		# 2nd and subsequent default routers error out, so explicitly
123
		# stop processing the list after the first one.
124
		break
125
	done
126

    
127
	if [ -n "$new_static_routes" ]; then
128
		$LOGGER "New Static Routes ($interface): $new_static_routes"
129
		set $new_static_routes
130
		while [ $# -gt 1 ]; do
131
			route add $1 $2
132
			shift; shift
133
                        echo $new_routers > /tmp/${interface}_router
134
		done
135
	fi
136
}
137

    
138
add_new_resolv_conf() {
139

    
140
	rm -f /var/etc/nameservers.conf
141

    
142
	if [ -n "$new_domain_name_servers" ]; then
143
		for nameserver in $new_domain_name_servers; do
144
			echo $nameserver >>/var/etc/nameservers.conf
145
		done
146
		echo $new_domain_name >/var/etc/defaultdomain.conf
147
	fi
148
	
149
	return 0
150
}
151

    
152
# Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
153
exit_with_hooks() {
154
	exit_status=$1
155
	if [ -f /etc/dhclient-exit-hooks ]; then
156
		. /etc/dhclient-exit-hooks
157
	fi
158
	# probably should do something with exit status of the local script
159
	exit $exit_status
160
}
161

    
162
#
163
# Start of active code.
164
#
165

    
166
# Invoke the local dhcp client enter hooks, if they exist.
167
if [ -f /etc/dhclient-enter-hooks ]; then
168
	exit_status=0
169
	. /etc/dhclient-enter-hooks
170
	# allow the local script to abort processing of this state
171
	# local script must set exit_status variable to nonzero.
172
	if [ $exit_status -ne 0 ]; then
173
		exit $exit_status
174
	fi
175
fi
176

    
177
if [ -x $NETSTAT ]; then
178
	if_defaulroute=`$NETSTAT -rn | $GREP "^default" | $AWK '{print $6}'`
179
else
180
	if_defaultroute="x"
181
fi
182

    
183
case $reason in
184
MEDIUM)
185
	ifconfig $interface $medium
186
	ifconfig $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
187
	sleep 1
188
	;;
189

    
190
PREINIT)
191
	delete_old_alias
192
	ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
193
        rm -f /tmp/${interface}_router
194
	;;
195

    
196
ARPCHECK|ARPSEND)
197
	;;
198

    
199
BOUND|RENEW|REBIND|REBOOT)
200
	check_hostname
201
	if [ -n "$old_ip_address" ]; then
202
		if [ "$old_ip_address" != "$alias_ip_address" ]; then
203
			delete_old_alias
204
		fi
205
		if [ "$old_ip_address" != "$new_ip_address" ]; then
206
			delete_old_address
207
			delete_old_routes
208
		fi
209
	fi
210
	if [ "$reason" = BOUND ] || \
211
	   [ "$reason" = REBOOT ] || \
212
	   [ -z "$old_ip_address" ] || \
213
	   [ "$old_ip_address" != "$new_ip_address" ]; then
214
		add_new_address
215
		add_new_routes
216
	fi
217
	if [ "$new_ip_address" != "$alias_ip_address" ]; then
218
		add_new_alias
219
	fi
220
	add_new_resolv_conf
221
	;;
222

    
223
EXPIRE|FAIL)
224
	delete_old_alias
225
	if [ -n "$old_ip_address" ]; then
226
		delete_old_address
227
		delete_old_routes
228
	fi
229
	# XXX Why add alias we just deleted above?
230
	add_new_alias
231
	if [ -f /var/etc/resolv.conf.save ]; then
232
		#cat /var/etc/resolv.conf.save > /var/etc/resolv.conf
233
	fi
234
	;;
235

    
236
TIMEOUT)
237
	delete_old_alias
238
	add_new_address
239
	sleep 1
240
	if [ -n "$new_routers" ]; then
241
		$LOGGER "New Routers ($interface): $new_routers"
242
		set "$new_routers"
243
		if ping -q -c 1 -w 1 "$1"; then
244
			if [ "$new_ip_address" != "$alias_ip_address" ]; then
245
				add_new_alias
246
			fi
247
			add_new_routes
248
			if add_new_resolv_conf; then
249
				exit_with_hooks 0
250
			fi
251
		fi
252
	fi
253
	ifconfig $interface inet -alias $new_ip_address $medium
254
	delete_old_routes
255
	exit_with_hooks 1
256
	;;
257
esac
258

    
259
exit_with_hooks 0
(2-2/2)