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