433deeefd369f9cc7914b5c4bb5a0a1c563741e3
[dragonfly.git] / sbin / dhclient / dhclient-script
1 #!/bin/sh
2 #
3 # $OpenBSD: src/sbin/dhclient/dhclient-script,v 1.22 2011/04/09 19:53:00 krw Exp $
4 #
5 # Copyright (c) 2003 Kenneth R Westerback <krw@openbsd.org>
6 #
7 # Permission to use, copy, modify, and distribute this software for any
8 # purpose with or without fee is hereby granted, provided that the above
9 # copyright notice and this permission notice appear in all copies.
10 #
11 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #
19 #
20
21 #
22 # Helper functions that implement common actions.
23 #
24
25 delete_old_address() {
26         if [ -n "$old_ip_address" ]; then
27                 ifconfig $interface inet $old_ip_address delete
28                 #route delete "$old_ip_address" 127.0.0.1 >/dev/null 2>&1
29         fi
30 }
31
32 add_new_address() {
33         ifconfig $interface \
34                 inet $new_ip_address \
35                 netmask $new_subnet_mask \
36                 broadcast $new_broadcast_address
37
38         # XXX Original TIMEOUT code did not do this unless $new_routers was set?
39         #route add $new_ip_address 127.0.0.1 >/dev/null 2>&1
40 }
41
42 delete_old_routes() {
43         arp -dan
44 }
45
46 add_new_routes() {
47         for router in $new_routers; do
48                 route -q delete default
49                 if [ "$new_ip_address" = "$router" ]; then
50                         route -q add default -iface $router
51                 else
52                         route -q add default $router
53                 fi
54                 # 2nd and subsequent default routers error out, so explicitly
55                 # stop processing the list after the first one.
56                 break
57         done
58 }
59
60 add_new_resolv_conf() {
61         # Create resolv.conf when either $new_domain_name_servers or
62         # $new_domain_name are provided. As reported in PR#3135, some ISPs
63         # provide only $new_domain_name_servers.
64
65         rm -f /etc/resolv.conf.std
66
67         if [ -n "$new_domain_name" ]; then
68                 echo "search $new_domain_name" >>/etc/resolv.conf.std
69         fi
70
71         if [ -n "$new_domain_name_servers" ]; then
72                 for nameserver in $new_domain_name_servers; do
73                         echo "nameserver $nameserver" >>/etc/resolv.conf.std
74                 done
75         fi
76
77         if [ -f /etc/resolv.conf.std ]; then
78                 if [ -f /etc/resolv.conf.tail ]; then
79                         cat /etc/resolv.conf.tail >>/etc/resolv.conf.std
80                 fi
81
82                 # In case (e.g. during OpenBSD installs) /etc/resolv.conf
83                 # is a symbolic link, take care to preserve the link and write
84                 # the new data in the correct location.
85
86                 if [ -f /etc/resolv.conf ]; then
87                         cat /etc/resolv.conf > /etc/resolv.conf.save
88                 fi
89                 cat /etc/resolv.conf.std > /etc/resolv.conf
90                 rm -f /etc/resolv.conf.std
91
92                 # Try to ensure correct ownership and permissions.
93                 chown -RL root:wheel /etc/resolv.conf
94                 chmod -RL 644 /etc/resolv.conf
95
96                 return 0
97         fi
98
99         return 1
100 }
101
102 #
103 # Start of active code.
104 #
105
106 case $reason in
107 MEDIUM)
108         # Not called by OpenBSD dhclient(8).
109         ;;
110
111 PREINIT)
112         # Not called by OpenBSD dhclient(8).
113         ;;
114
115 ARPSEND)
116         # Not called by OpenBSD dhclient(8).
117         exit 1
118         ;;
119
120 ARPCHECK)
121         # Not called by OpenBSD dhclient(8).
122         # Always succeed. i.e. accept lease.
123         ;;
124
125 BOUND|RENEW|REBIND|REBOOT)
126         if [ -n "$old_ip_address" ]; then
127                 if [ "$old_ip_address" != "$new_ip_address" ]; then
128                         delete_old_address
129                         delete_old_routes
130                 fi
131         fi
132         if [ "$reason" = BOUND ] ||
133            [ "$reason" = REBOOT ] ||
134            [ -z "$old_ip_address" ] ||
135            [ "$old_ip_address" != "$new_ip_address" ]; then
136                 add_new_address
137                 add_new_routes
138         fi
139         add_new_resolv_conf
140         ;;
141
142 EXPIRE|FAIL)
143         if [ -n "$old_ip_address" ]; then
144                 delete_old_address
145                 delete_old_routes
146         fi
147         if [ -f /etc/resolv.conf.save ]; then
148                 cat /etc/resolv.conf.save > /etc/resolv.conf
149         fi
150         ;;
151
152 TIMEOUT)
153         add_new_address
154         sleep 1
155         if [ -n "$new_routers" ]; then
156                 set "$new_routers"
157                 if ping -q -c 1 -w 1 "$1"; then
158                         add_new_routes
159                         if add_new_resolv_conf; then
160                                 exit 0
161                         fi
162                 fi
163         fi
164         ifconfig $interface inet $new_ip_address delete
165         # XXX Why not a delete_old_address as before all other invocations of
166         #     delete_old_routes?
167         delete_old_routes
168         exit 1
169         ;;
170 esac
171
172 exit 0