~ubuntu-branches/ubuntu/oneiric/isc-dhcp/oneiric-security

« back to all changes in this revision

Viewing changes to client/scripts/freebsd

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Pollock
  • Date: 2009-09-02 22:34:25 UTC
  • Revision ID: james.westby@ubuntu.com-20090902223425-nypo7bkftxffq41m
Tags: upstream-4.1.0
ImportĀ upstreamĀ versionĀ 4.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
#
 
3
# $Id: freebsd,v 1.22 2008/05/23 13:56:07 fdupont Exp $
 
4
#
 
5
# $FreeBSD$
 
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
    ( cat /dev/null > /etc/resolv.conf.dhclient )
 
16
    exit_status=$?
 
17
    if [ $exit_status -ne 0 ]; then
 
18
      $LOGGER "Unable to create /etc/resolv.conf.dhclient: Error $exit_status"
 
19
    else
 
20
      if [ "x$new_domain_search" != x ]; then
 
21
        ( echo search $new_domain_search >> /etc/resolv.conf.dhclient )
 
22
        exit_status=$?
 
23
      elif [ "x$new_domain_name" != x ]; then
 
24
        # Note that the DHCP 'Domain Name Option' is really just a domain
 
25
        # name, and that this practice of using the domain name option as
 
26
        # a search path is both nonstandard and deprecated.
 
27
        ( echo search $new_domain_name >> /etc/resolv.conf.dhclient )
 
28
        exit_status=$?
 
29
      fi
 
30
      for nameserver in $new_domain_name_servers; do
 
31
        if [ $exit_status -ne 0 ]; then
 
32
          break
 
33
        fi
 
34
        ( echo nameserver $nameserver >>/etc/resolv.conf.dhclient )
 
35
        exit_status=$?
 
36
      done
 
37
 
 
38
      # If there were no errors, attempt to mv the new file into place.
 
39
      if [ $exit_status -eq 0 ]; then
 
40
        ( mv /etc/resolv.conf.dhclient /etc/resolv.conf )
 
41
        exit_status=$?
 
42
      fi
 
43
 
 
44
      if [ $exit_status -ne 0 ]; then
 
45
        $LOGGER "Error while writing new /etc/resolv.conf."
 
46
      fi
 
47
    fi
 
48
  elif [ "x${new_dhcp6_name_servers}" != x ] ; then
 
49
    ( cat /dev/null > /etc/resolv.conf.dhclient6 )
 
50
    exit_status=$?
 
51
    if [ $exit_status -ne 0 ] ; then
 
52
      $LOGGER "Unable to create /etc/resolv.conf.dhclient6: Error $exit_status"
 
53
    else
 
54
      if [ "x${new_dhcp6_domain_search}" != x ] ; then
 
55
        ( echo search ${new_dhcp6_domain_search} >> /etc/resolv.conf.dhclient6 )
 
56
        exit_status=$?
 
57
      fi
 
58
      for nameserver in ${new_dhcp6_name_servers} ; do
 
59
        if [ $exit_status -ne 0 ] ; then
 
60
          break
 
61
        fi
 
62
        ( echo nameserver ${nameserver} >> /etc/resolv.conf.dhclient6 )
 
63
        exit_status=$?
 
64
      done
 
65
 
 
66
      if [ $exit_status -eq 0 ] ; then
 
67
        ( mv /etc/resolv.conf.dhclient6 /etc/resolv.conf )
 
68
        exit_status=$?
 
69
      fi
 
70
 
 
71
      if [ $exit_status -ne 0 ] ; then
 
72
        $LOGGER "Error while writing new /etc/resolv.conf."
 
73
      fi
 
74
    fi
 
75
  fi
 
76
}
 
77
 
 
78
# Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
 
79
exit_with_hooks() {
 
80
  exit_status=$1
 
81
  if [ -f /etc/dhclient-exit-hooks ]; then
 
82
    . /etc/dhclient-exit-hooks
 
83
  fi
 
84
# probably should do something with exit status of the local script
 
85
  exit $exit_status
 
86
}
 
87
 
 
88
# Invoke the local dhcp client enter hooks, if they exist.
 
89
if [ -f /etc/dhclient-enter-hooks ]; then
 
90
  exit_status=0
 
91
  . /etc/dhclient-enter-hooks
 
92
  # allow the local script to abort processing of this state
 
93
  # local script must set exit_status variable to nonzero.
 
94
  if [ $exit_status -ne 0 ]; then
 
95
    exit $exit_status
 
96
  fi
 
97
fi
 
98
 
 
99
if [ x$new_network_number != x ]; then
 
100
   $LOGGER New Network Number: $new_network_number
 
101
fi
 
102
 
 
103
if [ x$new_broadcast_address != x ]; then
 
104
 $LOGGER New Broadcast Address: $new_broadcast_address
 
105
  new_broadcast_arg="broadcast $new_broadcast_address"
 
106
fi
 
107
if [ x$old_broadcast_address != x ]; then
 
108
  old_broadcast_arg="broadcast $old_broadcast_address"
 
109
fi
 
110
if [ x$new_subnet_mask != x ]; then
 
111
  new_netmask_arg="netmask $new_subnet_mask"
 
112
fi
 
113
if [ x$old_subnet_mask != x ]; then
 
114
  old_netmask_arg="netmask $old_subnet_mask"
 
115
fi
 
116
if [ x$alias_subnet_mask != x ]; then
 
117
  alias_subnet_arg="netmask $alias_subnet_mask"
 
118
fi
 
119
 
 
120
if [ x$reason = xMEDIUM ]; then
 
121
  eval "ifconfig $interface $medium"
 
122
  eval "ifconfig $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
 
123
  sleep 1
 
124
  exit_with_hooks 0
 
125
fi
 
126
 
 
127
###
 
128
### DHCPv4 Handlers
 
129
###
 
130
 
 
131
if [ x$reason = xPREINIT ]; then
 
132
  if [ x$alias_ip_address != x ]; then
 
133
    ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
 
134
    route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
 
135
  fi
 
136
  ifconfig $interface inet 0.0.0.0 netmask 0.0.0.0 \
 
137
                broadcast 255.255.255.255 up
 
138
  exit_with_hooks 0
 
139
fi
 
140
 
 
141
if [ x$reason = xARPCHECK ] || [ x$reason = xARPSEND ]; then
 
142
  exit_with_hooks 0;
 
143
fi
 
144
  
 
145
if [ x$reason = xBOUND ] || [ x$reason = xRENEW ] || \
 
146
   [ x$reason = xREBIND ] || [ x$reason = xREBOOT ]; then
 
147
  current_hostname=`/bin/hostname`
 
148
  if [ x$current_hostname = x ] || \
 
149
     [ x$current_hostname = x$old_host_name ]; then
 
150
    if [ x$current_hostname = x ] || \
 
151
       [ x$new_host_name != x$old_host_name ]; then
 
152
      $LOGGER "New Hostname: $new_host_name"
 
153
      hostname $new_host_name
 
154
    fi
 
155
  fi
 
156
  if [ x$old_ip_address != x ] && [ x$alias_ip_address != x ] && \
 
157
                [ x$alias_ip_address != x$old_ip_address ]; then
 
158
    ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
 
159
    route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
 
160
  fi
 
161
  if [ x$old_ip_address != x ] && [ x$old_ip_address != x$new_ip_address ]
 
162
   then
 
163
    eval "ifconfig $interface inet -alias $old_ip_address $medium"
 
164
    route delete $old_ip_address 127.1 >/dev/null 2>&1
 
165
    for router in $old_routers; do
 
166
      route delete default $router >/dev/null 2>&1
 
167
    done
 
168
    if [ -n "$old_static_routes" ]; then
 
169
      set -- $old_static_routes
 
170
      while [ $# -gt 1 ]; do
 
171
        route delete $1 $2
 
172
        shift; shift
 
173
      done
 
174
    fi
 
175
    arp -n -a | sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' |sh
 
176
  fi
 
177
  if [ x$old_ip_address = x ] || [ x$old_ip_address != x$new_ip_address ] || \
 
178
     [ x$reason = xBOUND ] || [ x$reason = xREBOOT ]; then
 
179
    eval "ifconfig $interface inet $new_ip_address $new_netmask_arg \
 
180
                                        $new_broadcast_arg $medium"
 
181
    $LOGGER "New IP Address ($interface): $new_ip_address"
 
182
    $LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
 
183
    $LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
 
184
    if [ -n "$new_routers" ]; then
 
185
      $LOGGER "New Routers: $new_routers"
 
186
    fi
 
187
    route add $new_ip_address 127.1 >/dev/null 2>&1
 
188
    for router in $new_routers; do
 
189
      # If the subnet is captive, eg the netmask is /32 but the default
 
190
      # gateway is (obviously) outside of this, then we need to produce a
 
191
      # host route to reach the gateway.
 
192
      if [ "x$new_subnet_mask" = "x255.255.255.255" ] ; then
 
193
        route add -host $router -interface $interface
 
194
      fi
 
195
      route add default $router >/dev/null 2>&1
 
196
    done
 
197
    if [ -n "$new_static_routes" ]; then
 
198
      $LOGGER "New Static Routes: $new_static_routes"
 
199
      set -- $new_static_routes
 
200
      while [ $# -gt 1 ]; do
 
201
        route add $1 $2
 
202
        shift; shift
 
203
      done
 
204
    fi
 
205
  fi
 
206
  if [ x$new_ip_address != x$alias_ip_address ] && [ x$alias_ip_address != x ];
 
207
   then
 
208
    ifconfig $interface inet alias $alias_ip_address $alias_subnet_arg
 
209
    route add $alias_ip_address 127.0.0.1
 
210
  fi
 
211
  make_resolv_conf
 
212
  exit_with_hooks 0
 
213
fi
 
214
 
 
215
if [ x$reason = xEXPIRE ] || [ x$reason = xFAIL ] || [ x$reason = xRELEASE ] \
 
216
   || [ x$reason = xSTOP ]; then
 
217
  if [ x$alias_ip_address != x ]; then
 
218
    ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
 
219
    route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
 
220
  fi
 
221
  if [ x$old_ip_address != x ]; then
 
222
    eval "ifconfig $interface inet -alias $old_ip_address $medium"
 
223
    route delete $old_ip_address 127.1 >/dev/null 2>&1
 
224
    for router in $old_routers; do
 
225
      route delete default $router >/dev/null 2>&1
 
226
    done
 
227
    if [ -n "$old_static_routes" ]; then
 
228
      set -- $old_static_routes
 
229
      while [ $# -gt 1 ]; do
 
230
        route delete $1 $2
 
231
        shift; shift
 
232
      done
 
233
    fi
 
234
    arp -n -a | sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' \
 
235
                                                |sh >/dev/null 2>&1
 
236
  fi
 
237
  if [ x$alias_ip_address != x ]; then
 
238
    ifconfig $interface inet alias $alias_ip_address $alias_subnet_arg
 
239
    route add $alias_ip_address 127.0.0.1
 
240
  fi
 
241
  exit_with_hooks 0
 
242
fi
 
243
 
 
244
if [ x$reason = xTIMEOUT ]; then
 
245
  if [ x$alias_ip_address != x ]; then
 
246
    ifconfig $interface inet -alias $alias_ip_address > /dev/null 2>&1
 
247
    route delete $alias_ip_address 127.0.0.1 > /dev/null 2>&1
 
248
  fi
 
249
  eval "ifconfig $interface inet $new_ip_address $new_netmask_arg \
 
250
                                        $new_broadcast_arg $medium"
 
251
  $LOGGER "New IP Address ($interface): $new_ip_address"
 
252
  $LOGGER "New Subnet Mask ($interface): $new_subnet_mask"
 
253
  $LOGGER "New Broadcast Address ($interface): $new_broadcast_address"
 
254
  sleep 1
 
255
  if [ -n "$new_routers" ]; then
 
256
    $LOGGER "New Routers: $new_routers"
 
257
    set -- $new_routers
 
258
    if ping -q -c 1 $1; then
 
259
      if [ x$new_ip_address != x$alias_ip_address ] && \
 
260
                        [ x$alias_ip_address != x ]; then
 
261
        ifconfig $interface inet alias $alias_ip_address $alias_subnet_arg
 
262
        route add $alias_ip_address 127.0.0.1
 
263
      fi
 
264
      route add $new_ip_address 127.1 >/dev/null 2>&1
 
265
      for router in $new_routers; do
 
266
        if [ "x$new_subnet_mask" = "x255.255.255.255" ] ; then
 
267
          route add -host $router -interface $interface
 
268
        fi
 
269
        route add default $router >/dev/null 2>&1
 
270
      done
 
271
      set -- $new_static_routes
 
272
      while [ $# -gt 1 ]; do
 
273
        route add $1 $2
 
274
        shift; shift
 
275
      done
 
276
      make_resolv_conf
 
277
      exit_with_hooks 0
 
278
    fi
 
279
  fi
 
280
  eval "ifconfig $interface inet -alias $new_ip_address $medium"
 
281
  for router in $old_routers; do
 
282
    route delete default $router >/dev/null 2>&1
 
283
  done
 
284
  if [ -n "$old_static_routes" ]; then
 
285
    set -- $old_static_routes
 
286
    while [ $# -gt 1 ]; do
 
287
      route delete $1 $2
 
288
      shift; shift
 
289
    done
 
290
  fi
 
291
  arp -n -a | sed -n -e 's/^.*(\(.*\)) at .*$/arp -d \1/p' \
 
292
                                                        |sh >/dev/null 2>&1
 
293
  exit_with_hooks 1
 
294
fi
 
295
 
 
296
###
 
297
### DHCPv6 Handlers
 
298
###
 
299
 
 
300
if [ ${reason} = PREINIT6 ] ; then
 
301
  # Ensure interface is up.
 
302
  ifconfig ${interface} up
 
303
 
 
304
  # XXX: Remove any stale addresses from aborted clients.
 
305
 
 
306
  exit_with_hooks 0
 
307
fi
 
308
 
 
309
if [ x${old_ip6_prefix} != x ] || [ x${new_ip6_prefix} != x ] ; then
 
310
    echo Prefix ${reason} old=${old_ip6_prefix} new=${new_ip6_prefix}
 
311
 
 
312
    exit_with_hooks 0
 
313
fi
 
314
 
 
315
if [ ${reason} = BOUND6 ] ; then
 
316
  if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ] ; then
 
317
    exit_with_hooks 2;
 
318
  fi
 
319
 
 
320
  ifconfig ${interface} inet6 ${new_ip6_address}/${new_ip6_prefixlen} alias
 
321
 
 
322
  # Check for nameserver options.
 
323
  make_resolv_conf
 
324
 
 
325
  exit_with_hooks 0
 
326
fi
 
327
 
 
328
if [ ${reason} = RENEW6 ] || [ ${reason} = REBIND6 ] ; then
 
329
  # Make sure nothing has moved around on us.
 
330
 
 
331
  # Nameservers/domains/etc.
 
332
  if [ "x${new_dhcp6_name_servers}" != "x${old_dhcp6_name_servers}" ] ||
 
333
     [ "x${new_dhcp6_domain_search}" != "x${old_dhcp6_domain_search}" ] ; then
 
334
    make_resolv_conf
 
335
  fi
 
336
 
 
337
  exit_with_hooks 0
 
338
fi
 
339
 
 
340
if [ ${reason} = DEPREF6 ] ; then
 
341
  if [ x${new_ip6_address} = x ] ; then
 
342
    exit_with_hooks 2;
 
343
  fi
 
344
 
 
345
  ifconfig ${interface} inet6 ${new_ip6_address} deprecated
 
346
 
 
347
  exit_with_hooks 0
 
348
fi
 
349
 
 
350
if [ ${reason} = EXPIRE6 -o ${reason} = RELEASE6 -o ${reason} = STOP6 ] ; then
 
351
  if [ x${old_ip6_address} = x ] || [ x${old_ip6_prefixlen} = x ] ; then
 
352
    exit_with_hooks 2;
 
353
  fi
 
354
 
 
355
  ifconfig ${interface} inet6 ${old_ip6_address}/${old_ip6_prefixlen} -alias
 
356
 
 
357
  exit_with_hooks 0
 
358
fi
 
359
 
 
360
exit_with_hooks 0