xref: /minix3/external/bsd/dhcp/dist/client/scripts/macos (revision 83ee113ee0d94f3844d44065af2311604e9a30ad)
1#!/bin/sh
2#
3# Id: macos,v 1.4 2011/09/20 16:59:54 sar Exp
4#
5# automous run of this script will commit the DNS setting
6#
7
8if [ -x /usr/bin/logger ]; then
9	LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
10else
11	LOGGER=echo
12fi
13
14to_commit="yes"
15
16make_resolv_conf() {
17  to_commit="no"
18  if [ "x${new_dhcp6_name_servers}" != x ]; then
19    ( cat /dev/null > /var/run/resolv.conf.dhclient6 )
20    exit_status=$?
21    if [ $exit_status -ne 0 ]; then
22      $LOGGER "Unable to create /var/run/resolv.conf.dhclient6: Error $exit_status"
23    else
24      if [ "x${new_dhcp6_domain_search}" != x ]; then
25	( echo search ${new_dhcp6_domain_search} >> /var/run/resolv.conf.dhclient6 )
26	exit_status=$?
27      fi
28      for nameserver in ${new_dhcp6_name_servers} ; do
29	if [ $exit_status -ne 0 ]; then
30	  break
31	fi
32	# If the nameserver has a link-local address
33	# add a <zone_id> (interface name) to it.
34	case $nameserver in
35	    fe80:*) zone_id="%$interface";;
36	    FE80:*) zone_id="%$interface";;
37	   *)      zone_id="";;
38	esac
39        ( echo nameserver ${nameserver}$zone_id >> /etc/resolv.conf.dhclient6 )
40	exit_status=$?
41      done
42
43      if [ $exit_status -eq 0 ]; then
44	to_commit="force"
45	commit_resolv_conf
46      fi
47    fi
48  fi
49}
50
51# Try to commit /var/run/resolv.conf.dhclient6 contents to
52# System Configuration framework's Dynamic Store.
53# Note this will be cleared by the next location change
54# or preempted by IPv4.
55#
56# The System Configuration agent "IPMonitor" gets the DNS configuration
57# from the IPv4 or IPv6 primary service in the Dynamic Store
58# (managed by configd).
59commit_resolv_conf() {
60  if [ -f /var/run/resolv.conf.dhclient6 ]; then
61    if [ -x /usr/sbin/scutil ]; then
62      serviceID=`echo show State:/Network/Global/IPv6 | \
63          /usr/sbin/scutil | \
64          awk '/PrimaryService/ { print $3 }'`
65      echo $serviceID
66      if [ x$serviceID = x ]; then
67        $LOGGER "Can't find the primary IPv6 service"
68      else
69        tmp=`mktemp SC_dhclient6.XXXXXXXXXX`
70        echo list | /usr/sbin/scutil > /tmp/$tmp
71        grep -q State:/Network/Service/$serviceID/DNS /tmp/$tmp
72        grep_status=$?
73        if [ $grep_status -eq 0 ]; then
74          $LOGGER "DNS service already set in primary IPv6 service"
75          rm /tmp/$tmp
76        else
77          res=/var/run/resolv.conf.dhclient6
78          cp /dev/null /tmp/$tmp
79          grep -q '^nameserver' $res
80          grep_status=$?
81          if [ $grep_status -eq 0 ]; then
82            echo d.add ServerAddresses '*' \
83                 `awk 'BEGIN { n="" } \
84                       /^nameserver/ { n=n " " $2 } \
85                       END { print n}' < $res` >> /tmp/$tmp
86          fi
87          grep -q '^search' $res
88          grep_status=$?
89          if [ $grep_status -eq 0 ]; then
90            echo d.add SearchDomains '*' \
91                 `sed 's/^search//' < $res` >> /tmp/$tmp
92          fi
93          echo set State:/Network/Service/$serviceID/DNS >> /tmp/$tmp
94          echo quit >> /tmp/$tmp
95          cat /tmp/$tmp
96          /usr/sbin/scutil < /tmp/$tmp
97          rm /tmp/$tmp
98        fi
99      fi
100    else
101      $LOGGER "Can't find SystemConfiguration tools."
102    fi
103  else
104    if [ $to_commit = force ]; then
105      $LOGGER "Can't find /var/run/resolv.conf.dhclient6"
106    fi
107  fi
108  to_commit="done"
109}
110
111# Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
112exit_with_hooks() {
113  exit_status=$1
114  if [ -f /etc/dhclient-exit-hooks ]; then
115    . /etc/dhclient-exit-hooks
116  fi
117# probably should do something with exit status of the local script
118  exit $exit_status
119}
120
121# Invoke the local dhcp client enter hooks, if they exist.
122if [ -f /etc/dhclient-enter-hooks ]; then
123  exit_status=0
124  . /etc/dhclient-enter-hooks
125  # allow the local script to abort processing of this state
126  # local script must set exit_status variable to nonzero.
127  if [ $exit_status -ne 0 ]; then
128    exit $exit_status
129  fi
130fi
131
132if [ x$reason = xMEDIUM ]; then
133  eval "ifconfig $interface $medium"
134  eval "ifconfig $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1
135  sleep 1
136  exit_with_hooks 0
137fi
138
139###
140### DHCPv6 Handlers
141###
142
143if [ x$reason = xPREINIT6 ]; then
144  # Ensure interface is up.
145  ifconfig ${interface} up
146
147  # XXX: Remove any stale addresses from aborted clients.
148
149  exit_with_hooks 0
150fi
151
152if [ x${old_ip6_prefix} != x ] || [ x${new_ip6_prefix} != x ]; then
153    echo Prefix $reason old=${old_ip6_prefix} new=${new_ip6_prefix}
154
155    exit_with_hooks 0
156fi
157
158if [ x$reason = xBOUND6 ]; then
159  if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ]; then
160    exit_with_hooks 2;
161  fi
162
163  ifconfig ${interface} inet6 ${new_ip6_address}/${new_ip6_prefixlen} alias
164
165  # Check for nameserver options.
166  make_resolv_conf
167
168  exit_with_hooks 0
169fi
170
171if [ x$reason = xRENEW6 ] || [ x$reason = xREBIND6 ]; then
172  # Make sure nothing has moved around on us.
173
174  # Nameservers/domains/etc.
175  if [ "x${new_dhcp6_name_servers}" != "x${old_dhcp6_name_servers}" ] ||
176     [ "x${new_dhcp6_domain_search}" != "x${old_dhcp6_domain_search}" ]; then
177    make_resolv_conf
178  fi
179
180  exit_with_hooks 0
181fi
182
183if [ x$reason = xDEPREF6 ]; then
184  if [ x${new_ip6_address} = x ]; then
185    exit_with_hooks 2;
186  fi
187
188  ifconfig ${interface} inet6 ${new_ip6_address} deprecated
189
190  exit_with_hooks 0
191fi
192
193if [ x$reason = xEXPIRE6 -o x$reason = xRELEASE6 -o x$reason = xSTOP6 ]; then
194  if [ x${old_ip6_address} = x ] || [ x${old_ip6_prefixlen} = x ]; then
195    exit_with_hooks 2;
196  fi
197
198  ifconfig ${interface} inet6 ${old_ip6_address}/${old_ip6_prefixlen} -alias
199
200  exit_with_hooks 0
201fi
202
203if [ $to_commit = yes ]; then
204  commit_resolv_conf
205fi
206
207exit_with_hooks 0
208