Merge commit 'crater/master'
[dragonfly.git] / sbin / dhclient / dhclient-script
bloba7a203fc8d4e833188d6ccb8cac2f2473d2d8d97
1 #!/bin/sh
3 # $OpenBSD: dhclient-script,v 1.12 2007/08/11 17:58:55 krw Exp $
4 # $DragonFly: src/sbin/dhclient/dhclient-script,v 1.1 2008/08/30 16:07:58 hasso Exp $
6 # Copyright (c) 2003 Kenneth R Westerback <krw@openbsd.org>
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.
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.
23 # Helper functions that implement common actions.
26 delete_old_address() {
27 if [ -n "$old_ip_address" ]; then
28 ifconfig $interface inet $old_ip_address delete $medium
29 route delete "$old_ip_address" 127.0.0.1 >/dev/null 2>&1
33 add_new_address() {
34 ifconfig $interface \
35 inet $new_ip_address \
36 netmask $new_subnet_mask \
37 broadcast $new_broadcast_address \
38 $medium
40 # XXX Original TIMEOUT code did not do this unless $new_routers was set?
41 route add $new_ip_address 127.0.0.1 >/dev/null 2>&1
44 delete_old_alias() {
45 if [ -n "$alias_ip_address" ]; then
46 ifconfig $interface inet $alias_ip_address delete > /dev/null 2>&1
47 route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
51 add_new_alias() {
52 if [ -n "$alias_ip_address" ]; then
53 ifconfig $interface inet $alias_ip_address alias netmask \
54 $alias_subnet_mask
55 route add $alias_ip_address 127.0.0.1
59 delete_old_routes() {
60 # Delete existing default route. We only allow one, so no need to
61 # process $old_routers list.
62 route delete default >/dev/null 2>&1
64 if [ -n "$old_static_routes" ]; then
65 set $old_static_routes
66 while [ $# -gt 1 ]; do
67 route delete "$1" "$2"
68 shift; shift
69 done
72 arp -dan
75 add_new_routes() {
76 route delete default >/dev/null 2>&1
77 for router in $new_routers; do
78 if [ "$new_ip_address" = "$router" ]; then
79 route add default -iface $router >/dev/null 2>&1
80 else
81 route add default $router >/dev/null 2>&1
83 # 2nd and subsequent default routers error out, so explicitly
84 # stop processing the list after the first one.
85 break
86 done
88 if [ -n "$new_static_routes" ]; then
89 set $new_static_routes
90 while [ $# -gt 1 ]; do
91 route add $1 $2
92 shift; shift
93 done
97 add_new_resolv_conf() {
98 # XXX Old code did not create/update resolv.conf unless both
99 # $new_domain_name and $new_domain_name_servers were provided. PR
100 # #3135 reported some ISPs only provide $new_domain_name_servers and
101 # thus broke the script. This code creates the resolv.conf if either
102 # are provided.
104 rm -f /etc/resolv.conf.std
106 if [ -n "$new_domain_name" ]; then
107 echo "search $new_domain_name" >>/etc/resolv.conf.std
110 if [ -n "$new_domain_name_servers" ]; then
111 for nameserver in $new_domain_name_servers; do
112 echo "nameserver $nameserver" >>/etc/resolv.conf.std
113 done
116 if [ -f /etc/resolv.conf.std ]; then
117 if [ -f /etc/resolv.conf.tail ]; then
118 cat /etc/resolv.conf.tail >>/etc/resolv.conf.std
121 # In case (e.g. during OpenBSD installs) /etc/resolv.conf
122 # is a symbolic link, take care to preserve the link and write
123 # the new data in the correct location.
125 if [ -f /etc/resolv.conf ]; then
126 cat /etc/resolv.conf > /etc/resolv.conf.save
128 cat /etc/resolv.conf.std > /etc/resolv.conf
129 rm -f /etc/resolv.conf.std
131 # Try to ensure correct ownership and permissions.
132 chown -RL root:wheel /etc/resolv.conf
133 chmod -RL 644 /etc/resolv.conf
135 return 0
138 return 1
142 # Start of active code.
145 case $reason in
146 MEDIUM)
147 ifconfig $interface $medium
148 sleep 1
151 PREINIT)
152 delete_old_alias
153 ifconfig $interface up
156 ARPCHECK|ARPSEND)
159 BOUND|RENEW|REBIND|REBOOT)
160 if [ -n "$old_ip_address" ]; then
161 if [ "$old_ip_address" != "$alias_ip_address" ]; then
162 delete_old_alias
164 if [ "$old_ip_address" != "$new_ip_address" ]; then
165 delete_old_address
166 delete_old_routes
169 if [ "$reason" = BOUND ] ||
170 [ "$reason" = REBOOT ] ||
171 [ -z "$old_ip_address" ] ||
172 [ "$old_ip_address" != "$new_ip_address" ]; then
173 add_new_address
174 add_new_routes
176 if [ "$new_ip_address" != "$alias_ip_address" ]; then
177 add_new_alias
179 add_new_resolv_conf
182 EXPIRE|FAIL)
183 delete_old_alias
184 if [ -n "$old_ip_address" ]; then
185 delete_old_address
186 delete_old_routes
188 # XXX Why add alias we just deleted above?
189 add_new_alias
190 if [ -f /etc/resolv.conf.save ]; then
191 cat /etc/resolv.conf.save > /etc/resolv.conf
195 TIMEOUT)
196 delete_old_alias
197 add_new_address
198 sleep 1
199 if [ -n "$new_routers" ]; then
200 set "$new_routers"
201 if ping -q -c 1 -w 1 "$1"; then
202 if [ "$new_ip_address" != "$alias_ip_address" ]; then
203 add_new_alias
205 add_new_routes
206 if add_new_resolv_conf; then
207 exit 0
211 ifconfig $interface inet $new_ip_address delete $medium
212 # XXX Why not a delete_old_address as before all other invocations of
213 # delete_old_routes?
214 delete_old_routes
215 exit 1
217 esac
219 exit 0