rev |
line source |
pascal@7145
|
1 #!/bin/ash
|
pascal@7145
|
2
|
pascal@7145
|
3 # dhclient-script for Linux. Dan Halbert, March, 1997.
|
pascal@7145
|
4 # Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
|
pascal@7145
|
5 # Modified for Debian. Matt Zimmerman and Eloy Paris, December 2003
|
pascal@7145
|
6 # Modified to remove useless tests for antiquated kernel versions that
|
pascal@7145
|
7 # this doesn't even work with anyway, and introduces a dependency on /usr
|
pascal@7145
|
8 # being mounted, which causes cosmetic errors on hosts that NFS mount /usr
|
pascal@7145
|
9 # Andrew Pollock, February 2005
|
pascal@7145
|
10 # Modified to work on point-to-point links. Andrew Pollock, June 2005
|
pascal@7145
|
11 # Modified to support passing the parameters called with to the hooks. Andrew Pollock, November 2005
|
pascal@7145
|
12
|
pascal@7145
|
13 # The alias handling in here probably still sucks. -mdz
|
pascal@7145
|
14
|
pascal@7145
|
15 make_resolv_conf() {
|
pascal@7145
|
16 if [ -n "$new_domain_name" -o -n "$new_domain_name_servers" ]; then
|
pascal@7145
|
17 # Find out whether we are going to mount / rw
|
pascal@7145
|
18 exec 9>&0 </etc/fstab
|
pascal@7145
|
19 rootmode=rw
|
pascal@7145
|
20 while read dev mnt type opts dump pass junk; do
|
pascal@7145
|
21 [ "$mnt" != / ] && continue
|
pascal@7145
|
22 case "$opts" in
|
pascal@7145
|
23 ro|ro,*|*,ro|*,ro,*)
|
pascal@7145
|
24 rootmode=ro
|
pascal@7145
|
25 ;;
|
pascal@7145
|
26 esac
|
pascal@7145
|
27 done
|
pascal@7145
|
28 exec 0>&9 9>&-
|
pascal@7145
|
29
|
pascal@7145
|
30 # Wait for /etc/resolv.conf to become writable
|
pascal@7145
|
31 if [ "$rootmode" = "rw" ]; then
|
pascal@7145
|
32 while [ ! -w /etc ]; do
|
pascal@7145
|
33 sleep 1
|
pascal@7145
|
34 done
|
pascal@7145
|
35 fi
|
pascal@7145
|
36
|
pascal@7145
|
37 local new_resolv_conf=/etc/resolv.conf.dhclient-new
|
pascal@7145
|
38 rm -f $new_resolv_conf
|
pascal@7145
|
39 if [ -n "$new_domain_name" ]; then
|
pascal@7145
|
40 echo search $new_domain_name >>$new_resolv_conf
|
pascal@7145
|
41 else # keep 'old' search/domain scope
|
pascal@7145
|
42 egrep -i '^ *[:space:]*(search|domain)' /etc/resolv.conf >> $new_resolv_conf
|
pascal@7145
|
43 fi
|
pascal@7145
|
44 if [ -n "$new_domain_name_servers" ]; then
|
pascal@7145
|
45 for nameserver in $new_domain_name_servers; do
|
pascal@7145
|
46 echo nameserver $nameserver >>$new_resolv_conf
|
pascal@7145
|
47 done
|
pascal@7145
|
48 else # keep 'old' nameservers
|
pascal@7145
|
49 sed -n /^\w*[Nn][Aa][Mm][Ee][Ss][Ee][Rr][Vv][Ee][Rr]/p /etc/resolv.conf >>$new_resolv_conf
|
pascal@7145
|
50 fi
|
pascal@7145
|
51 chown root.root $new_resolv_conf
|
pascal@7145
|
52 chmod 644 $new_resolv_conf
|
pascal@7145
|
53 mv -f $new_resolv_conf /etc/resolv.conf
|
pascal@7145
|
54 fi
|
pascal@7145
|
55 }
|
pascal@7145
|
56
|
pascal@7145
|
57 run_hook() {
|
pascal@7145
|
58 local script="$1"
|
pascal@7145
|
59 local exit_status
|
pascal@7145
|
60 shift # discard the first argument, then the rest are the script's
|
pascal@7145
|
61
|
pascal@7145
|
62 if [ -f $script ]; then
|
pascal@7145
|
63 . $script "$@"
|
pascal@7145
|
64 fi
|
pascal@7145
|
65
|
pascal@7145
|
66
|
pascal@7145
|
67 if [ -n "$exit_status" ] && [ "$exit_status" -ne 0 ]; then
|
pascal@7145
|
68 logger -p daemon.err "$script returned non-zero exit status $exit_status"
|
pascal@7145
|
69 save_exit_status=$exit_status
|
pascal@7145
|
70 fi
|
pascal@7145
|
71
|
pascal@7145
|
72 return $exit_status
|
pascal@7145
|
73 }
|
pascal@7145
|
74
|
pascal@7145
|
75 run_hookdir() {
|
pascal@7145
|
76 local dir="$1"
|
pascal@7145
|
77 local exit_status
|
pascal@7145
|
78 shift # See run_hook
|
pascal@7145
|
79
|
pascal@7145
|
80 if [ -d "$dir" ]; then
|
pascal@7145
|
81 for script in $(run-parts --list $dir); do
|
pascal@7145
|
82 run_hook $script "$@" || true
|
pascal@7145
|
83 exit_status=$?
|
pascal@7145
|
84 done
|
pascal@7145
|
85 fi
|
pascal@7145
|
86
|
pascal@7145
|
87 return $exit_status
|
pascal@7145
|
88 }
|
pascal@7145
|
89
|
pascal@7145
|
90 # Must be used on exit. Invokes the local dhcp client exit hooks, if any.
|
pascal@7145
|
91 exit_with_hooks() {
|
pascal@7145
|
92 exit_status=$1
|
pascal@7145
|
93
|
pascal@7145
|
94 # Source the documented exit-hook script, if it exists
|
pascal@7145
|
95 if ! run_hook /etc/dhcp3/dhclient-exit-hooks "$@"; then
|
pascal@7145
|
96 exit_status=$?
|
pascal@7145
|
97 fi
|
pascal@7145
|
98
|
pascal@7145
|
99 # Now run scripts in the Debian-specific directory.
|
pascal@7145
|
100 if ! run_hookdir /etc/dhcp3/dhclient-exit-hooks.d "$@"; then
|
pascal@7145
|
101 exit_status=$?
|
pascal@7145
|
102 fi
|
pascal@7145
|
103
|
pascal@7145
|
104 exit $exit_status
|
pascal@7145
|
105 }
|
pascal@7145
|
106
|
pascal@7145
|
107 set_hostname() {
|
pascal@7145
|
108 local current_hostname=$(hostname)
|
pascal@7145
|
109 if [ -z "$current_hostname" -o "$current_hostname" = "(none)" ]; then
|
pascal@7145
|
110 hostname "$new_host_name"
|
pascal@7145
|
111 fi
|
pascal@7145
|
112 }
|
pascal@7145
|
113
|
pascal@7145
|
114 if [ -n "$new_broadcast_address" ]; then
|
pascal@7145
|
115 new_broadcast_arg="broadcast $new_broadcast_address"
|
pascal@7145
|
116 fi
|
pascal@7145
|
117 if [ -n "$old_broadcast_address" ]; then
|
pascal@7145
|
118 old_broadcast_arg="broadcast $old_broadcast_address"
|
pascal@7145
|
119 fi
|
pascal@7145
|
120 if [ -n "$new_subnet_mask" ]; then
|
pascal@7145
|
121 new_subnet_arg="netmask $new_subnet_mask"
|
pascal@7145
|
122 fi
|
pascal@7145
|
123 if [ -n "$old_subnet_mask" ]; then
|
pascal@7145
|
124 old_subnet_arg="netmask $old_subnet_mask"
|
pascal@7145
|
125 fi
|
pascal@7145
|
126 if [ -n "$alias_subnet_mask" ]; then
|
pascal@7145
|
127 alias_subnet_arg="netmask $alias_subnet_mask"
|
pascal@7145
|
128 fi
|
pascal@7145
|
129 if [ -n "$new_interface_mtu" ] && [ $new_interface_mtu -ge 575 ]; then
|
pascal@7145
|
130 mtu_arg="mtu $new_interface_mtu"
|
pascal@7145
|
131 fi
|
pascal@7145
|
132 if [ -n "$IF_METRIC" ]; then
|
pascal@7145
|
133 metric_arg="metric $IF_METRIC" # interfaces(5), "metric" option
|
pascal@7145
|
134 fi
|
pascal@7145
|
135
|
pascal@7145
|
136
|
pascal@7145
|
137 # The action starts here
|
pascal@7145
|
138
|
pascal@7145
|
139 # Invoke the local dhcp client enter hooks, if they exist.
|
pascal@7145
|
140 run_hook /etc/dhcp3/dhclient-enter-hooks
|
pascal@7145
|
141 run_hookdir /etc/dhcp3/dhclient-enter-hooks.d
|
pascal@7145
|
142
|
pascal@7145
|
143 # Execute the operation
|
pascal@7145
|
144 case "$reason" in
|
pascal@7145
|
145 MEDIUM|ARPCHECK|ARPSEND)
|
pascal@7145
|
146 # Do nothing
|
pascal@7145
|
147 ;;
|
pascal@7145
|
148 PREINIT)
|
pascal@7145
|
149 # The DHCP client is requesting that an interface be
|
pascal@7145
|
150 # configured as required in order to send packets prior to
|
pascal@7145
|
151 # receiving an actual address. - dhclient-script(8)
|
pascal@7145
|
152
|
pascal@7145
|
153 if [ -n "$alias_ip_address" ]; then
|
pascal@7145
|
154 # Bring down alias interface. Its routes will disappear too.
|
pascal@7145
|
155 ifconfig $interface:0- inet 0
|
pascal@7145
|
156 fi
|
pascal@7145
|
157 ifconfig $interface inet 0 up
|
pascal@7145
|
158
|
pascal@7145
|
159 # We need to give the kernel some time to get the interface up.
|
pascal@7145
|
160 sleep 1
|
pascal@7145
|
161 ;;
|
pascal@7145
|
162 BOUND|RENEW|REBIND|REBOOT)
|
pascal@7145
|
163
|
pascal@7145
|
164 set_hostname
|
pascal@7145
|
165
|
pascal@7145
|
166 if [ -n "$old_ip_address" -a -n "$alias_ip_address" -a \
|
pascal@7145
|
167 "$alias_ip_address" != "$old_ip_address" ]; then
|
pascal@7145
|
168 # Possible new alias. Remove old alias.
|
pascal@7145
|
169 ifconfig $interface:0- inet 0
|
pascal@7145
|
170 fi
|
pascal@7145
|
171
|
pascal@7145
|
172 if [ -n "$old_ip_address" -a \
|
pascal@7145
|
173 "$old_ip_address" != "$new_ip_address" ]; then
|
pascal@7145
|
174 # IP address changed. Bringing down the interface will delete all routes,
|
pascal@7145
|
175 # and clear the ARP cache.
|
pascal@7145
|
176 ifconfig $interface inet 0
|
pascal@7145
|
177
|
pascal@7145
|
178 fi
|
pascal@7145
|
179
|
pascal@7145
|
180 if [ -z "$old_ip_address" -o "$old_ip_address" != "$new_ip_address" -o \
|
pascal@7145
|
181 "$reason" = "BOUND" -o "$reason" = "REBOOT" ]; then
|
pascal@7145
|
182
|
pascal@7145
|
183 ifconfig $interface inet $new_ip_address $new_subnet_arg \
|
pascal@7145
|
184 $new_broadcast_arg $mtu_arg
|
pascal@7145
|
185
|
pascal@7145
|
186 # point to point
|
pascal@7145
|
187 if [ "$new_subnet_mask" == "255.255.255.255" ]; then
|
pascal@7145
|
188 for router in $new_routers; do
|
pascal@7145
|
189 route add -host $router dev $interface
|
pascal@7145
|
190 done
|
pascal@7145
|
191 fi
|
pascal@7145
|
192
|
pascal@7145
|
193 for router in $new_routers; do
|
pascal@7145
|
194 route add default dev $interface gw $router $metric_arg
|
pascal@7145
|
195 done
|
pascal@7145
|
196 fi
|
pascal@7145
|
197
|
pascal@7145
|
198 if [ "$new_ip_address" != "$alias_ip_address" -a -n "$alias_ip_address" ];
|
pascal@7145
|
199 then
|
pascal@7145
|
200 ifconfig $interface:0- inet 0
|
pascal@7145
|
201 ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
|
pascal@7145
|
202 route add -host $alias_ip_address $interface:0
|
pascal@7145
|
203 fi
|
pascal@7145
|
204
|
pascal@7145
|
205 make_resolv_conf
|
pascal@7145
|
206
|
pascal@7145
|
207 ;;
|
pascal@7145
|
208
|
pascal@7145
|
209 EXPIRE|FAIL|RELEASE|STOP)
|
pascal@7145
|
210 if [ -n "$alias_ip_address" ]; then
|
pascal@7145
|
211 # Turn off alias interface.
|
pascal@7145
|
212 ifconfig $interface:0- inet 0
|
pascal@7145
|
213 fi
|
pascal@7145
|
214
|
pascal@7145
|
215 if [ -n "$old_ip_address" ]; then
|
pascal@7145
|
216 # Shut down interface, which will delete routes and clear arp cache.
|
pascal@7145
|
217 ifconfig $interface inet 0
|
pascal@7145
|
218 fi
|
pascal@7145
|
219
|
pascal@7145
|
220 if [ -n "$alias_ip_address" ]; then
|
pascal@7145
|
221 ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
|
pascal@7145
|
222 route add -host $alias_ip_address $interface:0
|
pascal@7145
|
223 fi
|
pascal@7145
|
224
|
pascal@7145
|
225 ;;
|
pascal@7145
|
226
|
pascal@7145
|
227 TIMEOUT)
|
pascal@7145
|
228 if [ -n "$alias_ip_address" ]; then
|
pascal@7145
|
229 ifconfig $interface:0- inet 0
|
pascal@7145
|
230 fi
|
pascal@7145
|
231
|
pascal@7145
|
232 ifconfig $interface inet $new_ip_address $new_subnet_arg \
|
pascal@7145
|
233 $new_broadcast_arg $mtu_arg
|
pascal@7145
|
234
|
pascal@7145
|
235 set -- $new_routers
|
pascal@7145
|
236 first_router="$1"
|
pascal@7145
|
237
|
pascal@7145
|
238 if ping -q -c 1 $first_router; then
|
pascal@7145
|
239 if [ "$new_ip_address" != "$alias_ip_address" -a \
|
pascal@7145
|
240 -n "$alias_ip_address" ]; then
|
pascal@7145
|
241 ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
|
pascal@7145
|
242 route add -host $alias_ip_address dev $interface:0
|
pascal@7145
|
243 fi
|
pascal@7145
|
244
|
pascal@7145
|
245 # point to point
|
pascal@7145
|
246 if [ "$new_subnet_mask" == "255.255.255.255" ]; then
|
pascal@7145
|
247 for router in $new_routers; do
|
pascal@7145
|
248 route add -host $router dev $interface
|
pascal@7145
|
249 done
|
pascal@7145
|
250 fi
|
pascal@7145
|
251
|
pascal@7145
|
252 for router in $new_routers; do
|
pascal@7145
|
253 route add default dev $interface gw $router $metric_arg
|
pascal@7145
|
254 done
|
pascal@7145
|
255
|
pascal@7145
|
256 make_resolv_conf
|
pascal@7145
|
257 else
|
pascal@7145
|
258 # Changed from 'ifconfig $interface inet 0 down' - see Debian bug #144666
|
pascal@7145
|
259 ifconfig $interface inet 0
|
pascal@7145
|
260 exit_with_hooks 2 "$@"
|
pascal@7145
|
261 fi
|
pascal@7145
|
262
|
pascal@7145
|
263 ;;
|
pascal@7145
|
264 esac
|
pascal@7145
|
265
|
pascal@7145
|
266 exit_with_hooks 0
|