Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / isc-dhcp / client / scripts / freebsd
1 #!/bin/sh
2 #
3 # $Id: freebsd,v 1.13.2.4 2002/06/09 22:37:55 murray Exp $
4 #
5 # $FreeBSD: src/contrib/isc-dhcp/client/scripts/freebsd,v 1.9.2.6 2003/03/02 16:42:38 murray Exp $
6
7 if [ -x /usr/bin/logger ]; then
8         LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
9 else
10         LOGGER=echo
11 fi
12
13 make_resolv_conf() {
14   if [ x"$new_domain_name_servers" != x ]; then
15     if [ "x$new_domain_name" != x ]; then
16       echo search $new_domain_name >/etc/resolv.conf
17     else
18       rm /etc/resolv.conf
19     fi
20     for nameserver in $new_domain_name_servers; do
21       echo nameserver $nameserver >>/etc/resolv.conf
22     done
23   fi
24 }
25
26 # Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
27 exit_with_hooks() {
28   exit_status=$1
29   if [ -f /etc/dhclient-exit-hooks ]; then
30     . /etc/dhclient-exit-hooks
31   fi
32 # probably should do something with exit status of the local script
33   exit $exit_status
34 }
35
36 # Invoke the local dhcp client enter hooks, if they exist.
37 if [ -f /etc/dhclient-enter-hooks ]; then
38   exit_status=0
39   . /etc/dhclient-enter-hooks
40   # allow the local script to abort processing of this state
41   # local script must set exit_status variable to nonzero.
42   if [ $exit_status -ne 0 ]; then
43     exit $exit_status
44   fi
45 fi
46
47 if [ x$new_network_number != x ]; then
48    $LOGGER New Network Number: $new_network_number
49 fi
50
51 if [ x$new_broadcast_address != x ]; then
52  $LOGGER New Broadcast Address: $new_broadcast_address
53   new_broadcast_arg="broadcast $new_broadcast_address"
54 fi
55 if [ x$old_broadcast_address != x ]; then
56   old_broadcast_arg="broadcast $old_broadcast_address"
57 fi
58 if [ x$new_subnet_mask != x ]; then
59   new_netmask_arg="netmask $new_subnet_mask"
60 fi
61 if [ x$old_subnet_mask != x ]; then
62   old_netmask_arg="netmask $old_subnet_mask"
63 fi
64 if [ x$alias_subnet_mask != x ]; then
65   alias_subnet_arg="netmask $alias_subnet_mask"
66 fi
67
68 if [ x$reason = xMEDIUM ]; then
69   eval "ifconfig $interface $medium"
70   eval "ifconfig $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
71   sleep 1
72   exit_with_hooks 0
73 fi
74
75 if [ x$reason = xPREINIT ]; then
76   if [ x$alias_ip_address != x ]; then
77     ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
78     route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
79   fi
80   ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 \
81                 broadcast 255.255.255.255 up
82   exit_with_hooks 0
83 fi
84
85 if [ x$reason = xARPCHECK ] || [ x$reason = xARPSEND ]; then
86   exit_with_hooks 0;
87 fi
88   
89 if [ x$reason = xBOUND ] || [ x$reason = xRENEW ] || \
90    [ x$reason = xREBIND ] || [ x$reason = xREBOOT ]; then
91   current_hostname=`/bin/hostname`
92   if [ x$current_hostname = x ] || \
93      [ x$current_hostname = x$old_host_name ]; then
94     if [ x$current_hostname = x ] || \
95        [ x$new_host_name != x$old_host_name ]; then
96       $LOGGER "New Hostname: $new_host_name"
97       hostname $new_host_name
98     fi
99   fi
100   if [ x$old_ip_address != x ] && [ x$alias_ip_address != x ] && \
101                 [ x$alias_ip_address != x$old_ip_address ]; then
102     ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
103     route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
104   fi
105   if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]
106    then
107     eval "ifconfig $interface inet -alias $old_ip_address $medium"
108     route delete $old_ip_address 127.1 >/dev/null 2>&1
109     for router in $old_routers; do
110       route delete default $router >/dev/null 2>&1
111     done
112     if [ -n "$old_static_routes" ]; then
113       set -- $old_static_routes
114       while [ $# -gt 1 ]; do
115         route delete $1 $2
116         shift; shift
117       done
118     fi
119     arp -n -a | sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' |sh
120   fi
121   if [ x$old_ip_address = x ] || [ x$old_ip_address != x$new_ip_address ] || \
122      [ x$reason = xBOUND ] || [ x$reason = xREBOOT ]; then
123     eval "ifconfig $interface inet $new_ip_address $new_netmask_arg \
124                                         $new_broadcast_arg $medium"
125     $LOGGER "New IP Address ($interface): $new_ip_address"
126     $LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
127     $LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
128     if [ -n "$new_routers" ]; then
129       $LOGGER "New Routers: $new_routers"
130     fi
131     route add $new_ip_address 127.1 >/dev/null 2>&1
132     for router in $new_routers; do
133       route add default $router >/dev/null 2>&1
134     done
135     if [ -n "$new_static_routes" ]; then
136       $LOGGER "New Static Routes: $new_static_routes"
137       set -- $new_static_routes
138       while [ $# -gt 1 ]; do
139         route add $1 $2
140         shift; shift
141       done
142     fi
143   fi
144   if [ x$new_ip_address != x$alias_ip_address ] && [ x$alias_ip_address != x ];
145    then
146     ifconfig $interface inet alias $alias_ip_address $alias_subnet_arg
147     route add $alias_ip_address 127.0.0.1
148   fi
149   make_resolv_conf
150   exit_with_hooks 0
151 fi
152
153 if [ x$reason = xEXPIRE ] || [ x$reason = xFAIL ] || [ x$reason = xRELEASE ] \
154    || [ x$reason = xSTOP ]; then
155   if [ x$alias_ip_address != x ]; then
156     ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
157     route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
158   fi
159   if [ x$old_ip_address != x ]; then
160     eval "ifconfig $interface inet -alias $old_ip_address $medium"
161     route delete $old_ip_address 127.1 >/dev/null 2>&1
162     for router in $old_routers; do
163       route delete default $router >/dev/null 2>&1
164     done
165     if [ -n "$old_static_routes" ]; then
166       set -- $old_static_routes
167       while [ $# -gt 1 ]; do
168         route delete $1 $2
169         shift; shift
170       done
171     fi
172     arp -n -a | sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' \
173                                                 |sh >/dev/null 2>&1
174   fi
175   if [ x$alias_ip_address != x ]; then
176     ifconfig $interface inet alias $alias_ip_address $alias_subnet_arg
177     route add $alias_ip_address 127.0.0.1
178   fi
179   exit_with_hooks 0
180 fi
181
182 if [ x$reason = xTIMEOUT ]; then
183   if [ x$alias_ip_address != x ]; then
184     ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
185     route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
186   fi
187   eval "ifconfig $interface inet $new_ip_address $new_netmask_arg \
188                                         $new_broadcast_arg $medium"
189   $LOGGER "New IP Address ($interface): $new_ip_address"
190   $LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
191   $LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
192   sleep 1
193   if [ -n "$new_routers" ]; then
194     $LOGGER "New Routers: $new_routers"
195     set -- $new_routers
196     if ping -q -c 1 $1; then
197       if [ x$new_ip_address != x$alias_ip_address ] && \
198                         [ x$alias_ip_address != x ]; then
199         ifconfig $interface inet alias $alias_ip_address $alias_subnet_arg
200         route add $alias_ip_address 127.0.0.1
201       fi
202       route add $new_ip_address 127.1 >/dev/null 2>&1
203       for router in $new_routers; do
204         route add default $router >/dev/null 2>&1
205       done
206       set -- $new_static_routes
207       while [ $# -gt 1 ]; do
208         route add $1 $2
209         shift; shift
210       done
211       make_resolv_conf
212       exit_with_hooks 0
213     fi
214   fi
215   eval "ifconfig $interface inet -alias $new_ip_address $medium"
216   for router in $old_routers; do
217     route delete default $router >/dev/null 2>&1
218   done
219   if [ -n "$old_static_routes" ]; then
220     set -- $old_static_routes
221     while [ $# -gt 1 ]; do
222       route delete $1 $2
223       shift; shift
224     done
225   fi
226   arp -n -a | sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' \
227                                                         |sh >/dev/null 2>&1
228   exit_with_hooks 1
229 fi
230
231 exit_with_hooks 0