Merge branch 'vendor/TCPDUMP' (early part)
[dragonfly.git] / sbin / dhclient / dhclient-script
1 #!/bin/sh
2 #
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 $
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 #
23 # Helper functions that implement common actions.
24 #
25
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
30         fi
31 }
32
33 add_new_address() {
34         ifconfig $interface \
35                 inet $new_ip_address \
36                 netmask $new_subnet_mask \
37                 broadcast $new_broadcast_address \
38                 $medium
39
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
42 }
43
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
48         fi
49 }
50
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
56         fi
57 }
58
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
63
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
70         fi
71
72         arp -dan
73 }
74
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
82                 fi
83                 # 2nd and subsequent default routers error out, so explicitly
84                 # stop processing the list after the first one.
85                 break
86         done
87
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
94         fi
95 }
96
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.
103
104         rm -f /etc/resolv.conf.std
105
106         if [ -n "$new_domain_name" ]; then
107                 echo "search $new_domain_name" >>/etc/resolv.conf.std
108         fi
109
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
114         fi
115
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
119                 fi
120
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.
124
125                 if [ -f /etc/resolv.conf ]; then
126                         cat /etc/resolv.conf > /etc/resolv.conf.save
127                 fi
128                 cat /etc/resolv.conf.std > /etc/resolv.conf
129                 rm -f /etc/resolv.conf.std
130
131                 # Try to ensure correct ownership and permissions.
132                 chown -RL root:wheel /etc/resolv.conf
133                 chmod -RL 644 /etc/resolv.conf
134
135                 return 0
136         fi
137
138         return 1
139 }
140
141 #
142 # Start of active code.
143 #
144
145 case $reason in
146 MEDIUM)
147         ifconfig $interface $medium
148         sleep 1
149         ;;
150
151 PREINIT)
152         delete_old_alias
153         ifconfig $interface up
154         ;;
155
156 ARPCHECK|ARPSEND)
157         ;;
158
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
163                 fi
164                 if [ "$old_ip_address" != "$new_ip_address" ]; then
165                         delete_old_address
166                         delete_old_routes
167                 fi
168         fi
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
175         fi
176         if [ "$new_ip_address" != "$alias_ip_address" ]; then
177                 add_new_alias
178         fi
179         add_new_resolv_conf
180         ;;
181
182 EXPIRE|FAIL)
183         delete_old_alias
184         if [ -n "$old_ip_address" ]; then
185                 delete_old_address
186                 delete_old_routes
187         fi
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
192         fi
193         ;;
194
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
204                         fi
205                         add_new_routes
206                         if add_new_resolv_conf; then
207                                 exit 0
208                         fi
209                 fi
210         fi
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
216         ;;
217 esac
218
219 exit 0