1 1.1 christos #!/bin/sh 2 1.1 christos # 3 1.1 christos # Id: macos,v 1.4 2011/09/20 16:59:54 sar Exp 4 1.1 christos # 5 1.1 christos # automous run of this script will commit the DNS setting 6 1.1 christos # 7 1.1 christos 8 1.1 christos if [ -x /usr/bin/logger ]; then 9 1.1 christos LOGGER="/usr/bin/logger -s -p user.notice -t dhclient" 10 1.1 christos else 11 1.1 christos LOGGER=echo 12 1.1 christos fi 13 1.1 christos 14 1.1 christos to_commit="yes" 15 1.1 christos 16 1.1 christos make_resolv_conf() { 17 1.1 christos to_commit="no" 18 1.1 christos if [ "x${new_dhcp6_name_servers}" != x ]; then 19 1.1 christos ( cat /dev/null > /var/run/resolv.conf.dhclient6 ) 20 1.1 christos exit_status=$? 21 1.1 christos if [ $exit_status -ne 0 ]; then 22 1.1 christos $LOGGER "Unable to create /var/run/resolv.conf.dhclient6: Error $exit_status" 23 1.1 christos else 24 1.1 christos if [ "x${new_dhcp6_domain_search}" != x ]; then 25 1.1 christos ( echo search ${new_dhcp6_domain_search} >> /var/run/resolv.conf.dhclient6 ) 26 1.1 christos exit_status=$? 27 1.1 christos fi 28 1.1 christos for nameserver in ${new_dhcp6_name_servers} ; do 29 1.1 christos if [ $exit_status -ne 0 ]; then 30 1.1 christos break 31 1.1 christos fi 32 1.1 christos # If the nameserver has a link-local address 33 1.1 christos # add a <zone_id> (interface name) to it. 34 1.1 christos case $nameserver in 35 1.1 christos fe80:*) zone_id="%$interface";; 36 1.1 christos FE80:*) zone_id="%$interface";; 37 1.1 christos *) zone_id="";; 38 1.1 christos esac 39 1.1 christos ( echo nameserver ${nameserver}$zone_id >> /etc/resolv.conf.dhclient6 ) 40 1.1 christos exit_status=$? 41 1.1 christos done 42 1.1 christos 43 1.1 christos if [ $exit_status -eq 0 ]; then 44 1.1 christos to_commit="force" 45 1.1 christos commit_resolv_conf 46 1.1 christos fi 47 1.1 christos fi 48 1.1 christos fi 49 1.1 christos } 50 1.1 christos 51 1.1 christos # Try to commit /var/run/resolv.conf.dhclient6 contents to 52 1.1 christos # System Configuration framework's Dynamic Store. 53 1.1 christos # Note this will be cleared by the next location change 54 1.1 christos # or preempted by IPv4. 55 1.1 christos # 56 1.1 christos # The System Configuration agent "IPMonitor" gets the DNS configuration 57 1.1 christos # from the IPv4 or IPv6 primary service in the Dynamic Store 58 1.1 christos # (managed by configd). 59 1.1 christos commit_resolv_conf() { 60 1.1 christos if [ -f /var/run/resolv.conf.dhclient6 ]; then 61 1.1 christos if [ -x /usr/sbin/scutil ]; then 62 1.1 christos serviceID=`echo show State:/Network/Global/IPv6 | \ 63 1.1 christos /usr/sbin/scutil | \ 64 1.1 christos awk '/PrimaryService/ { print $3 }'` 65 1.1 christos echo $serviceID 66 1.1 christos if [ x$serviceID = x ]; then 67 1.1 christos $LOGGER "Can't find the primary IPv6 service" 68 1.1 christos else 69 1.1 christos tmp=`mktemp SC_dhclient6.XXXXXXXXXX` 70 1.1 christos echo list | /usr/sbin/scutil > /tmp/$tmp 71 1.1 christos grep -q State:/Network/Service/$serviceID/DNS /tmp/$tmp 72 1.1 christos grep_status=$? 73 1.1 christos if [ $grep_status -eq 0 ]; then 74 1.1 christos $LOGGER "DNS service already set in primary IPv6 service" 75 1.1 christos rm /tmp/$tmp 76 1.1 christos else 77 1.1 christos res=/var/run/resolv.conf.dhclient6 78 1.1 christos cp /dev/null /tmp/$tmp 79 1.1 christos grep -q '^nameserver' $res 80 1.1 christos grep_status=$? 81 1.1 christos if [ $grep_status -eq 0 ]; then 82 1.1 christos echo d.add ServerAddresses '*' \ 83 1.1 christos `awk 'BEGIN { n="" } \ 84 1.1 christos /^nameserver/ { n=n " " $2 } \ 85 1.1 christos END { print n}' < $res` >> /tmp/$tmp 86 1.1 christos fi 87 1.1 christos grep -q '^search' $res 88 1.1 christos grep_status=$? 89 1.1 christos if [ $grep_status -eq 0 ]; then 90 1.1 christos echo d.add SearchDomains '*' \ 91 1.1 christos `sed 's/^search//' < $res` >> /tmp/$tmp 92 1.1 christos fi 93 1.1 christos echo set State:/Network/Service/$serviceID/DNS >> /tmp/$tmp 94 1.1 christos echo quit >> /tmp/$tmp 95 1.1 christos cat /tmp/$tmp 96 1.1 christos /usr/sbin/scutil < /tmp/$tmp 97 1.1 christos rm /tmp/$tmp 98 1.1 christos fi 99 1.1 christos fi 100 1.1 christos else 101 1.1 christos $LOGGER "Can't find SystemConfiguration tools." 102 1.1 christos fi 103 1.1 christos else 104 1.1 christos if [ $to_commit = force ]; then 105 1.1 christos $LOGGER "Can't find /var/run/resolv.conf.dhclient6" 106 1.1 christos fi 107 1.1 christos fi 108 1.1 christos to_commit="done" 109 1.1 christos } 110 1.1 christos 111 1.1 christos # This function was largely borrowed from dhclient-script that 112 1.1 christos # ships with Centos, authored by Jiri Popelka and David Cantrell 113 1.1 christos # of Redhat. Thanks guys. 114 1.1 christos add_ipv6_addr_with_DAD() { 115 1.1 christos ifconfig ${interface} inet6 ${new_ip6_address}/${new_ip6_prefixlen} alias 116 1.1 christos 117 1.1 christos if [ ${dad_wait_time} -le 0 ] 118 1.1 christos then 119 1.1 christos # if we're not waiting for DAD, assume we're good 120 1.1 christos return 0 121 1.1 christos fi 122 1.1 christos 123 1.1 christos # Repeatedly test whether newly added address passed 124 1.1 christos # duplicate address detection (DAD) 125 1.1 christos for i in $(seq 1 ${dad_wait_time}); do 126 1.1 christos sleep 1 # give the DAD some time 127 1.1 christos 128 1.1 christos addr=$(ifconfig ${interface} \ 129 1.1 christos | grep "${new_ip6_address} prefixlen ${new_ip6_prefixlen}") 130 1.1 christos 131 1.1 christos # tentative flag == DAD is still not complete 132 1.1 christos tentative=$(echo "${addr}" | grep tentative) 133 1.1 christos # dadfailed flag == address is already in use somewhere else 134 1.1 christos dadfailed=$(echo "${addr}" | grep duplicated) 135 1.1 christos 136 1.1 christos if [ -n "${dadfailed}" ] ; then 137 1.1 christos # dad failed, remove the address 138 1.1 christos ifconfig ${interface} inet6 ${new_ip6_address}/${new_ip6_prefixlen} -alias 139 1.1 christos exit_with_hooks 3 140 1.1 christos fi 141 1.1 christos 142 1.1 christos if [ -z "${tentative}" ] ; then 143 1.1 christos if [ -n "${addr}" ]; then 144 1.1 christos # DAD is over 145 1.1 christos return 0 146 1.1 christos else 147 1.1 christos # address was auto-removed (or not added at all) 148 1.1 christos exit_with_hooks 3 149 1.1 christos fi 150 1.1 christos fi 151 1.1 christos done 152 1.1 christos 153 1.1 christos return 0 154 1.1 christos } 155 1.1 christos 156 1.1 christos # Must be used on exit. Invokes the local dhcp client exit hooks, if any. 157 1.1 christos exit_with_hooks() { 158 1.1 christos exit_status=$1 159 1.1 christos if [ -f /etc/dhclient-exit-hooks ]; then 160 1.1 christos . /etc/dhclient-exit-hooks 161 1.1 christos fi 162 1.1 christos # probably should do something with exit status of the local script 163 1.1 christos exit $exit_status 164 1.1 christos } 165 1.1 christos 166 1.1 christos # Invoke the local dhcp client enter hooks, if they exist. 167 1.1 christos if [ -f /etc/dhclient-enter-hooks ]; then 168 1.1 christos exit_status=0 169 1.1 christos . /etc/dhclient-enter-hooks 170 1.1 christos # allow the local script to abort processing of this state 171 1.1 christos # local script must set exit_status variable to nonzero. 172 1.1 christos if [ $exit_status -ne 0 ]; then 173 1.1 christos exit $exit_status 174 1.1 christos fi 175 1.1 christos fi 176 1.1 christos 177 1.1 christos if [ x$reason = xMEDIUM ]; then 178 1.1 christos eval "ifconfig $interface $medium" 179 1.1 christos eval "ifconfig $interface inet -alias 0.0.0.0 $medium" >/dev/null 2>&1 180 1.1 christos sleep 1 181 1.1 christos exit_with_hooks 0 182 1.1 christos fi 183 1.1 christos 184 1.1 christos ### 185 1.1 christos ### DHCPv6 Handlers 186 1.1 christos ### 187 1.1 christos 188 1.1 christos if [ x$reason = xPREINIT6 ]; then 189 1.1 christos # Ensure interface is up. 190 1.1 christos ifconfig ${interface} up 191 1.1 christos 192 1.1 christos # We need to give the kernel some time to active interface 193 1.1 christos interface_up_wait_time=5 194 1.1 christos for i in $(seq 0 ${interface_up_wait_time}) 195 1.1 christos do 196 1.1 christos ifconfig ${interface} | grep inactive &> /dev/null 197 1.1 christos if [ $? -ne 0 ]; then 198 1.1 christos break; 199 1.1 christos fi 200 1.1 christos sleep 1 201 1.1 christos done 202 1.1 christos 203 1.1 christos # XXX: Remove any stale addresses from aborted clients. 204 1.1 christos 205 1.1 christos # Wait for duplicate address detection for this interface if the 206 1.1 christos # --dad-wait-time parameter has been specified and is greater than 207 1.1 christos # zero. 208 1.1 christos if [ ${dad_wait_time} -gt 0 ]; then 209 1.1 christos # Check if any IPv6 address on this interface is marked as 210 1.1 christos # tentative. 211 1.1 christos ifconfig ${interface} | grep inet6 | grep tentative \ 212 1.1 christos &> /dev/null 213 1.1 christos if [ $? -eq 0 ]; then 214 1.1 christos # Wait for duplicate address detection to complete or for 215 1.1 christos # the timeout specified as --dad-wait-time. 216 1.1 christos for i in $(seq 0 $dad_wait_time) 217 1.1 christos do 218 1.1 christos # We're going to poll for the tentative flag every second. 219 1.1 christos sleep 1 220 1.1 christos ifconfig ${interface} | grep inet6 | grep tentative \ 221 1.1 christos &> /dev/null 222 1.1 christos if [ $? -ne 0 ]; then 223 1.1 christos break; 224 1.1 christos fi 225 1.1 christos done 226 1.1 christos fi 227 1.1 christos fi 228 1.1 christos 229 1.1 christos exit_with_hooks 0 230 1.1 christos fi 231 1.1 christos 232 1.1 christos if [ x${old_ip6_prefix} != x ] || [ x${new_ip6_prefix} != x ]; then 233 1.1 christos echo Prefix $reason old=${old_ip6_prefix} new=${new_ip6_prefix} 234 1.1 christos 235 1.1 christos exit_with_hooks 0 236 1.1 christos fi 237 1.1 christos 238 1.1 christos if [ x$reason = xBOUND6 ]; then 239 1.1 christos if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ]; then 240 1.1 christos exit_with_hooks 2; 241 1.1 christos fi 242 1.1 christos 243 1.1 christos # Add address to interface, check for DAD if dad_wait_time > 0 244 1.1 christos add_ipv6_addr_with_DAD 245 1.1 christos 246 1.1 christos # Check for nameserver options. 247 1.1 christos make_resolv_conf 248 1.1 christos 249 1.1 christos exit_with_hooks 0 250 1.1 christos fi 251 1.1 christos 252 1.1 christos if [ x$reason = xRENEW6 ] || [ x$reason = xREBIND6 ]; then 253 1.1 christos # Make sure nothing has moved around on us. 254 1.1 christos 255 1.1 christos # Nameservers/domains/etc. 256 1.1 christos if [ "x${new_dhcp6_name_servers}" != "x${old_dhcp6_name_servers}" ] || 257 1.1 christos [ "x${new_dhcp6_domain_search}" != "x${old_dhcp6_domain_search}" ]; then 258 1.1 christos make_resolv_conf 259 1.1 christos fi 260 1.1 christos 261 1.1 christos exit_with_hooks 0 262 1.1 christos fi 263 1.1 christos 264 1.1 christos if [ x$reason = xDEPREF6 ]; then 265 1.1 christos if [ x${new_ip6_address} = x ]; then 266 1.1 christos exit_with_hooks 2; 267 1.1 christos fi 268 1.1 christos 269 1.1 christos ifconfig ${interface} inet6 ${new_ip6_address} deprecated 270 1.1 christos 271 1.1 christos exit_with_hooks 0 272 1.1 christos fi 273 1.1 christos 274 1.1 christos if [ x$reason = xEXPIRE6 -o x$reason = xRELEASE6 -o x$reason = xSTOP6 ]; then 275 1.1 christos if [ x${old_ip6_address} = x ] || [ x${old_ip6_prefixlen} = x ]; then 276 1.1 christos exit_with_hooks 2; 277 1.1 christos fi 278 1.1 christos 279 1.1 christos ifconfig ${interface} inet6 ${old_ip6_address}/${old_ip6_prefixlen} -alias 280 1.1 christos 281 1.1 christos exit_with_hooks 0 282 1.1 christos fi 283 1.1 christos 284 1.1 christos if [ $to_commit = yes ]; then 285 1.1 christos commit_resolv_conf 286 1.1 christos fi 287 1.1 christos 288 1.1 christos exit_with_hooks 0 289