Project

General

Profile

Download (7.53 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
	# XXX Old code did not create/update resolv.conf unless both
140
	# $new_domain_name and $new_domain_name_servers were provided.  PR
141
	# #3135 reported some ISP's only provide $new_domain_name_servers and
142
	# thus broke the script. This code creates the resolv.conf if either
143
	# are provided.
144

    
145
	rm -f /var/etc/resolv.conf.std
146

    
147
	if [ -n "$new_domain_name" ]; then
148
		echo "search $new_domain_name" >>/var/etc/resolv.conf.std
149
	fi
150

    
151
	rm -f /var/etc/nameservers.conf
152

    
153
	if [ -n "$new_domain_name_servers" ]; then
154
		for nameserver in $new_domain_name_servers; do
155
			echo "nameserver $nameserver" >>/var/etc/resolv.conf.std
156
			echo $nameserver >>/var/etc/nameservers.conf
157
			echo $new_domain_name >/var/etc/defaultdomain.conf
158
		done
159
	fi
160

    
161
	if [ -f /var/etc/resolv.conf.std ]; then
162
		if [ -f /var/etc/resolv.conf.tail ]; then
163
			cat /var/etc/resolv.conf.tail >>/var/etc/resolv.conf.std
164
		fi
165

    
166
		# In case (e.g. during OpenBSD installs) /var/etc/resolv.conf
167
		# is a symbolic link, take care to preserve the link and write
168
		# the new data in the correct location.
169

    
170
		if [ -f /var/etc/resolv.conf ]; then
171
			cat /var/etc/resolv.conf > /var/etc/resolv.conf.save
172
		fi
173
		cat /var/etc/resolv.conf.std > /var/etc/nameservers.conf
174
		cat /var/etc/resolv.conf.std > /var/etc/resolv.conf
175
		rm -f /var/etc/resolv.conf.std
176

    
177
		# Try to ensure correct ownership and permissions.
178
		chown -RL root:wheel /var/etc/resolv.conf
179
		chmod -RL 644 /var/etc/resolv.conf
180

    
181
		return 0
182
	fi
183

    
184
	return 1
185
}
186

    
187
# Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
188
exit_with_hooks() {
189
	exit_status=$1
190
	if [ -f /etc/dhclient-exit-hooks ]; then
191
		. /etc/dhclient-exit-hooks
192
	fi
193
	# probably should do something with exit status of the local script
194
	exit $exit_status
195
}
196

    
197
#
198
# Start of active code.
199
#
200

    
201
# Invoke the local dhcp client enter hooks, if they exist.
202
if [ -f /etc/dhclient-enter-hooks ]; then
203
	exit_status=0
204
	. /etc/dhclient-enter-hooks
205
	# allow the local script to abort processing of this state
206
	# local script must set exit_status variable to nonzero.
207
	if [ $exit_status -ne 0 ]; then
208
		exit $exit_status
209
	fi
210
fi
211

    
212
if [ -x $NETSTAT ]; then
213
	if_defaulroute=`$NETSTAT -rn | $GREP "^default" | $AWK '{print $6}'`
214
else
215
	if_defaultroute="x"
216
fi
217

    
218
case $reason in
219
MEDIUM)
220
	ifconfig $interface $medium
221
	ifconfig $interface inet -alias 0.0.0.0 $medium >/dev/null 2>&1
222
	sleep 1
223
	;;
224

    
225
PREINIT)
226
	delete_old_alias
227
	ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 broadcast 255.255.255.255 up
228
        rm -f /tmp/${interface}_router
229
	;;
230

    
231
ARPCHECK|ARPSEND)
232
	;;
233

    
234
BOUND|RENEW|REBIND|REBOOT)
235
	check_hostname
236
	if [ -n "$old_ip_address" ]; then
237
		if [ "$old_ip_address" != "$alias_ip_address" ]; then
238
			delete_old_alias
239
		fi
240
		if [ "$old_ip_address" != "$new_ip_address" ]; then
241
			delete_old_address
242
			delete_old_routes
243
		fi
244
	fi
245
	if [ "$reason" = BOUND ] || \
246
	   [ "$reason" = REBOOT ] || \
247
	   [ -z "$old_ip_address" ] || \
248
	   [ "$old_ip_address" != "$new_ip_address" ]; then
249
		add_new_address
250
		add_new_routes
251
	fi
252
	if [ "$new_ip_address" != "$alias_ip_address" ]; then
253
		add_new_alias
254
	fi
255
	add_new_resolv_conf
256
	;;
257

    
258
EXPIRE|FAIL)
259
	delete_old_alias
260
	if [ -n "$old_ip_address" ]; then
261
		delete_old_address
262
		delete_old_routes
263
	fi
264
	# XXX Why add alias we just deleted above?
265
	add_new_alias
266
	if [ -f /var/etc/resolv.conf.save ]; then
267
		cat /var/etc/resolv.conf.save > /var/etc/resolv.conf
268
	fi
269
	;;
270

    
271
TIMEOUT)
272
	delete_old_alias
273
	add_new_address
274
	sleep 1
275
	if [ -n "$new_routers" ]; then
276
		$LOGGER "New Routers ($interface): $new_routers"
277
		set "$new_routers"
278
		if ping -q -c 1 -w 1 "$1"; then
279
			if [ "$new_ip_address" != "$alias_ip_address" ]; then
280
				add_new_alias
281
			fi
282
			add_new_routes
283
			if add_new_resolv_conf; then
284
				exit_with_hooks 0
285
			fi
286
		fi
287
	fi
288
	ifconfig $interface inet -alias $new_ip_address $medium
289
	delete_old_routes
290
	exit_with_hooks 1
291
	;;
292
esac
293

    
294
exit_with_hooks 0
    (1-1/1)