]> git.ipfire.org Git - thirdparty/dhcp.git/blob - client/scripts/macos
a896b1e9a288e4145379c3b60449e45fc6c2def9
[thirdparty/dhcp.git] / client / scripts / macos
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
8 if [ -x /usr/bin/logger ]; then
9 LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
10 else
11 LOGGER=echo
12 fi
13
14 to_commit="yes"
15
16 make_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).
59 commit_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.
112 exit_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.
122 if [ -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
130 fi
131
132 if [ 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
137 fi
138
139 ###
140 ### DHCPv6 Handlers
141 ###
142
143 if [ x$reason = xPREINIT6 ]; then
144 # Ensure interface is up.
145 ifconfig ${interface} up
146
147 # We need to give the kernel some time to active interface
148 interface_up_wait_time=5
149 for i in $(seq 0 ${interface_up_wait_time})
150 do
151 ifconfig ${interface} | grep inactive &> /dev/null
152 if [ $? -ne 0 ]; then
153 break;
154 fi
155 sleep 1
156 done
157
158 # XXX: Remove any stale addresses from aborted clients.
159
160 # Wait for duplicate address detection for this interface if the
161 # --dad-wait-time parameter has been specified and is greater than
162 # zero.
163 if [ ${dad_wait_time} -gt 0 ]; then
164 # Check if any IPv6 address on this interface is marked as
165 # tentative.
166 ifconfig ${interface} | grep inet6 | grep tentative \
167 &> /dev/null
168 if [ $? -eq 0 ]; then
169 # Wait for duplicate address detection to complete or for
170 # the timeout specified as --dad-wait-time.
171 for i in $(seq 0 $dad_wait_time)
172 do
173 # We're going to poll for the tentative flag every second.
174 sleep 1
175 ifconfig ${interface} | grep inet6 | grep tentative \
176 &> /dev/null
177 if [ $? -ne 0 ]; then
178 break;
179 fi
180 done
181 fi
182 fi
183
184 exit_with_hooks 0
185 fi
186
187 if [ x${old_ip6_prefix} != x ] || [ x${new_ip6_prefix} != x ]; then
188 echo Prefix $reason old=${old_ip6_prefix} new=${new_ip6_prefix}
189
190 exit_with_hooks 0
191 fi
192
193 if [ x$reason = xBOUND6 ]; then
194 if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ]; then
195 exit_with_hooks 2;
196 fi
197
198 ifconfig ${interface} inet6 ${new_ip6_address}/${new_ip6_prefixlen} alias
199
200 # Check for nameserver options.
201 make_resolv_conf
202
203 exit_with_hooks 0
204 fi
205
206 if [ x$reason = xRENEW6 ] || [ x$reason = xREBIND6 ]; then
207 # Make sure nothing has moved around on us.
208
209 # Nameservers/domains/etc.
210 if [ "x${new_dhcp6_name_servers}" != "x${old_dhcp6_name_servers}" ] ||
211 [ "x${new_dhcp6_domain_search}" != "x${old_dhcp6_domain_search}" ]; then
212 make_resolv_conf
213 fi
214
215 exit_with_hooks 0
216 fi
217
218 if [ x$reason = xDEPREF6 ]; then
219 if [ x${new_ip6_address} = x ]; then
220 exit_with_hooks 2;
221 fi
222
223 ifconfig ${interface} inet6 ${new_ip6_address} deprecated
224
225 exit_with_hooks 0
226 fi
227
228 if [ x$reason = xEXPIRE6 -o x$reason = xRELEASE6 -o x$reason = xSTOP6 ]; then
229 if [ x${old_ip6_address} = x ] || [ x${old_ip6_prefixlen} = x ]; then
230 exit_with_hooks 2;
231 fi
232
233 ifconfig ${interface} inet6 ${old_ip6_address}/${old_ip6_prefixlen} -alias
234
235 exit_with_hooks 0
236 fi
237
238 if [ $to_commit = yes ]; then
239 commit_resolv_conf
240 fi
241
242 exit_with_hooks 0