1#!/bin/sh
2#
3# $NetBSD: dhclient-script,v 1.2 2015/02/26 09:59:55 roy Exp $
4
5ENTERHOOKS=/etc/dhclient-enter-hooks
6EXITHOOKS=/etc/dhclient-exit-hooks
7RESOLV=/etc/resolv.conf
8SIGNATURE="# Created by dhclient from"
9INTSIG="$SIGNATURE $interface"
10
11echo_resolv_conf() {
12	echo "$INTSIG"
13	echo search $new_domain_name
14	for nameserver in $new_domain_name_servers; do
15		echo nameserver $nameserver
16	done
17}
18
19make_resolv_conf() {
20	if [ ! -z "$new_domain_name_servers" ]; then
21		if [ -x /sbin/resolvconf ]; then
22			echo_resolv_conf | /sbin/resolvconf -a $interface
23		else
24			if [ -f $RESOLV ]; then
25				while read line; do
26					case $line in
27					"$SIGNATURE"*)
28						;;
29					*)
30						mv $RESOLV $RESOLV.save;;
31					esac
32					break
33				done < $RESOLV
34			fi
35			echo_resolv_conf > $RESOLV
36		fi
37	fi
38}
39
40restore_resolv_conf() {
41	if [ -x /sbin/resolvconf ]; then
42		/sbin/resolvconf -d $interface
43	elif [ -f $RESOLV.save -a -f $RESOLV ]; then
44		while read line; do
45			case $line in
46			"$INTSIG"*)
47				mv $RESOLV.save $RESOLV;;
48			esac
49			break
50		done < $RESOLV
51	fi
52}
53
54# Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
55exit_with_hooks() {
56	exit_status=$1
57	if [ -f "$EXITHOOKS" ]; then
58		. "$EXITHOOKS"
59	fi
60	# probably should do something with exit status of the local script
61	exit $exit_status
62}
63
64
65add_new_routes() {
66	for router in $new_routers; do
67		route add default $router
68	done >/dev/null 2>&1
69
70	set -- $new_static_routes
71	while [ $# -gt 1 ]; do
72		route add $1 $2
73		shift; shift
74	done
75}
76
77delete_old_routes() {
78	for router in $old_routers; do
79		route delete default $router
80	done >/dev/null 2>&1
81
82	set -- $old_static_routes
83	while [ $# -gt 1 ]; do
84		route delete $1 $2
85		shift; shift
86	done
87
88	route -n flush -inet -llinfo -host
89}
90
91# Invoke the local dhcp client enter hooks, if they exist.
92if [ -f $ENTERHOOKS ]; then
93	exit_status=0
94	. $ENTERHOOKS
95	# allow the local script to abort processing of this state
96	# local script must set exit_status variable to nonzero.
97	if [ $exit_status -ne 0 ]; then
98		exit $exit_status
99	fi
100fi
101
102#if [ ! -z "$new_host_name" ]; then
103#	echo New Host Name: $new_host_name
104#fi
105#
106#if [ ! -z "$new_nis_domain" ]; then
107#	echo New NIS Domain: $new_nis_domain
108#fi
109#
110#if [ ! -z "$new_network_number" ]; then
111#	echo New Network Number: $new_network_number
112#fi
113
114if [ ! -z "$new_broadcast_address" ]; then
115#	echo New Broadcast Address: $new_broadcast_address
116	new_broadcast_arg="broadcast $new_broadcast_address"
117fi
118
119if [ ! -z "$old_broadcast_address" ]; then
120	old_broadcast_arg="broadcast $old_broadcast_address"
121fi
122
123if [ ! -z "$new_subnet_mask" ]; then
124	new_netmask_arg="netmask $new_subnet_mask"
125fi
126
127if [ ! -z "$old_subnet_mask" ]; then
128	old_netmask_arg="netmask $old_subnet_mask"
129fi
130
131if [ ! -z "$alias_subnet_mask" ]; then
132	alias_subnet_arg="netmask $alias_subnet_mask"
133fi
134
135case "$reason" in
136MEDIUM)
137	test -z "$medium" && exit_with_hooks 0
138	eval "ifconfig $interface $medium"
139	eval "ifconfig $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
140
141	sleep 1
142
143	exit_with_hooks 0
144	;;
145
146PREINIT)
147	if [ ! -z "$alias_ip_address" ]; then
148		ifconfig $interface inet \
149		    -alias $alias_ip_address >/dev/null 2>&1
150	fi
151
152	ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 \
153	    broadcast 255.255.255.255 up
154
155	exit_with_hooks 0
156	;;
157
158ARPCHECK|ARPSEND)
159	exit_with_hooks 0
160	;;
161
162BOUND|RENEW|REBIND|REBOOT)
163	if [ ! -z "$new_nis_domain" ]; then
164		if type domainname > /dev/null 2>&1; then
165			domainname $new_nis_domain
166		fi
167	fi
168
169	if type hostname > /dev/null 2>&1; then
170		if [ \( -z "$current_hostname" \) -o \
171		    \( "x$new_host_name" = "x$old_hostname" \) ]; then
172			current_hostname=`hostname`
173		fi
174		if [ \( -z "$current_hostname" \) -o \
175		    \( "x$new_host_name" != "x$old_hostname" \) ]; then
176			hostname $new_host_name
177		fi
178	fi
179
180	if [ \( ! -z "$old_ip_address" \) -a \( ! -z "$alias_ip_address" \) -a \
181	    \( "x$alias_ip_address" != "x$old_ip_address" \) ]; then
182		ifconfig $interface inet \
183		    -alias $alias_ip_address > /dev/null 2>&1
184	fi
185
186	if [ \( ! -z "$old_ip_address" \) -a \
187	    \( "x$old_ip_address" != "x$new_ip_address" \) ]; then
188		eval "ifconfig $interface inet -alias $old_ip_address $medium"
189		delete_old_routes
190	fi
191
192	if [ \( -z "$old_ip_address" \) -o \
193	    \( "x$old_ip_address" != "x$new_ip_address" \) -o \
194	    \( "x$reason" = "xBOUND" \) -o \( "x$reason" = "xREBOOT" \) ]; then
195		eval "ifconfig $interface inet $new_ip_address \
196		    $new_netmask_arg $new_broadcast_arg $medium"
197		add_new_routes
198	fi
199
200	if [ \( ! -z "$alias_ip_address" \) -a \
201	    \( "x$new_ip_address" != "x$alias_ip_address" \) ]; then
202		ifconfig $interface inet alias $alias_ip_address \
203		    $alias_subnet_arg
204	fi
205	make_resolv_conf
206	exit_with_hooks 0
207	;;
208
209EXPIRE|FAIL|RELEASE|STOP)
210	if [ ! -z "$old_nis_domain" ]; then
211		if type domainname > /dev/null 2>&1; then
212			# delete the old nis domain name
213			domainname ""
214		fi
215	fi
216
217	if [ ! -z "$alias_ip_address" ]; then
218		ifconfig $interface inet -alias $alias_ip_address
219	fi > /dev/null 2>&1
220
221	if [ ! -z "$old_ip_address" ]; then
222		eval "ifconfig $interface inet -alias $old_ip_address $medium"
223		delete_old_routes
224
225	fi
226
227	if [ ! -z "$alias_ip_address" ]; then
228		ifconfig $interface inet alias $alias_ip_address \
229		    $alias_subnet_arg
230	fi
231
232	restore_resolv_conf
233	exit_with_hooks 0
234	;;
235
236TIMEOUT)
237	if [ ! -z "$alias_ip_address" ]; then
238		ifconfig $interface inet -alias $alias_ip_address
239	fi > /dev/null 2>&1
240
241	if [ ! -z "$new_host_name" ]; then
242		if type hostname > /dev/null 2>&1; then
243			hostname $new_host_name
244		fi
245	fi
246
247	if [ ! -z "$new_nis_domain" ]; then
248		if type domainname > /dev/null 2>&1; then
249			domainname $new_nis_domain
250		fi
251	fi
252
253	eval "ifconfig $interface inet $new_ip_address $new_netmask_arg \
254	    $new_broadcast_arg $medium"
255	sleep 1
256
257	if [ ! -z "$new_routers" ]; then
258		set -- $new_routers
259		if ping -n -q -c 1 -w 1 $1; then
260			if [ \( ! -z "$alias_ip_address" \) -a \
261			    \( "x$new_ip_address" != "x$alias_ip_address" \) ]
262			then
263				ifconfig $interface inet alias \
264				    $alias_ip_address $alias_subnet_arg
265			fi
266
267			add_new_routes
268			make_resolv_conf
269			exit_with_hooks 0
270		fi
271	fi
272
273	eval "ifconfig $interface inet -alias $new_ip_address $medium"
274
275	delete_old_routes
276
277	exit_with_hooks 1
278	;;
279*)
280	exit_with_hooks 0
281	;;
282esac
283