| 1 | #!/bin/sh |
| 2 | # |
| 3 | # $OpenBSD: src/sbin/dhclient/dhclient-script,v 1.20 2011/04/04 11:29:51 krw Exp $ |
| 4 | # |
| 5 | # Copyright (c) 2003 Kenneth R Westerback <krw@openbsd.org> |
| 6 | # |
| 7 | # Permission to use, copy, modify, and distribute this software for any |
| 8 | # purpose with or without fee is hereby granted, provided that the above |
| 9 | # copyright notice and this permission notice appear in all copies. |
| 10 | # |
| 11 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 12 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 13 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 14 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 15 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 16 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 17 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 18 | # |
| 19 | # |
| 20 | |
| 21 | # |
| 22 | # Helper functions that implement common actions. |
| 23 | # |
| 24 | |
| 25 | delete_old_address() { |
| 26 | if [ -n "$old_ip_address" ]; then |
| 27 | ifconfig $interface inet $old_ip_address delete |
| 28 | #route delete "$old_ip_address" 127.0.0.1 >/dev/null 2>&1 |
| 29 | fi |
| 30 | } |
| 31 | |
| 32 | add_new_address() { |
| 33 | ifconfig $interface \ |
| 34 | inet $new_ip_address \ |
| 35 | netmask $new_subnet_mask \ |
| 36 | broadcast $new_broadcast_address |
| 37 | |
| 38 | # XXX Original TIMEOUT code did not do this unless $new_routers was set? |
| 39 | #route add $new_ip_address 127.0.0.1 >/dev/null 2>&1 |
| 40 | } |
| 41 | |
| 42 | delete_old_routes() { |
| 43 | if [ -n "$old_static_routes" ]; then |
| 44 | set $old_static_routes |
| 45 | while [ $# -gt 1 ]; do |
| 46 | route delete "$1" "$2" |
| 47 | shift; shift |
| 48 | done |
| 49 | fi |
| 50 | |
| 51 | arp -dan |
| 52 | } |
| 53 | |
| 54 | add_new_routes() { |
| 55 | for router in $new_routers; do |
| 56 | route -q delete default |
| 57 | if [ "$new_ip_address" = "$router" ]; then |
| 58 | route -q add default -iface $router |
| 59 | else |
| 60 | route -q add default $router |
| 61 | fi |
| 62 | # 2nd and subsequent default routers error out, so explicitly |
| 63 | # stop processing the list after the first one. |
| 64 | break |
| 65 | done |
| 66 | |
| 67 | if [ -n "$new_static_routes" ]; then |
| 68 | set $new_static_routes |
| 69 | while [ $# -gt 1 ]; do |
| 70 | route add $1 $2 |
| 71 | shift; shift |
| 72 | done |
| 73 | fi |
| 74 | } |
| 75 | |
| 76 | add_new_resolv_conf() { |
| 77 | # Create resolv.conf when either $new_domain_name_servers or |
| 78 | # $new_domain_name are provided. As reported in PR#3135, some ISPs |
| 79 | # provide only $new_domain_name_servers. |
| 80 | |
| 81 | rm -f /etc/resolv.conf.std |
| 82 | |
| 83 | if [ -n "$new_domain_name" ]; then |
| 84 | echo "search $new_domain_name" >>/etc/resolv.conf.std |
| 85 | fi |
| 86 | |
| 87 | if [ -n "$new_domain_name_servers" ]; then |
| 88 | for nameserver in $new_domain_name_servers; do |
| 89 | echo "nameserver $nameserver" >>/etc/resolv.conf.std |
| 90 | done |
| 91 | fi |
| 92 | |
| 93 | if [ -f /etc/resolv.conf.std ]; then |
| 94 | if [ -f /etc/resolv.conf.tail ]; then |
| 95 | cat /etc/resolv.conf.tail >>/etc/resolv.conf.std |
| 96 | fi |
| 97 | |
| 98 | # In case (e.g. during OpenBSD installs) /etc/resolv.conf |
| 99 | # is a symbolic link, take care to preserve the link and write |
| 100 | # the new data in the correct location. |
| 101 | |
| 102 | if [ -f /etc/resolv.conf ]; then |
| 103 | cat /etc/resolv.conf > /etc/resolv.conf.save |
| 104 | fi |
| 105 | cat /etc/resolv.conf.std > /etc/resolv.conf |
| 106 | rm -f /etc/resolv.conf.std |
| 107 | |
| 108 | # Try to ensure correct ownership and permissions. |
| 109 | chown -RL root:wheel /etc/resolv.conf |
| 110 | chmod -RL 644 /etc/resolv.conf |
| 111 | |
| 112 | return 0 |
| 113 | fi |
| 114 | |
| 115 | return 1 |
| 116 | } |
| 117 | |
| 118 | # |
| 119 | # Start of active code. |
| 120 | # |
| 121 | |
| 122 | case $reason in |
| 123 | MEDIUM) |
| 124 | # Not called by OpenBSD dhclient(8). |
| 125 | ;; |
| 126 | |
| 127 | PREINIT) |
| 128 | # Not called by OpenBSD dhclient(8). |
| 129 | ;; |
| 130 | |
| 131 | ARPSEND) |
| 132 | # Not called by OpenBSD dhclient(8). |
| 133 | exit 1 |
| 134 | ;; |
| 135 | |
| 136 | ARPCHECK) |
| 137 | # Not called by OpenBSD dhclient(8). |
| 138 | # Always succeed. i.e. accept lease. |
| 139 | ;; |
| 140 | |
| 141 | BOUND|RENEW|REBIND|REBOOT) |
| 142 | if [ -n "$old_ip_address" ]; then |
| 143 | if [ "$old_ip_address" != "$new_ip_address" ]; then |
| 144 | delete_old_address |
| 145 | delete_old_routes |
| 146 | fi |
| 147 | fi |
| 148 | if [ "$reason" = BOUND ] || |
| 149 | [ "$reason" = REBOOT ] || |
| 150 | [ -z "$old_ip_address" ] || |
| 151 | [ "$old_ip_address" != "$new_ip_address" ]; then |
| 152 | add_new_address |
| 153 | add_new_routes |
| 154 | fi |
| 155 | add_new_resolv_conf |
| 156 | ;; |
| 157 | |
| 158 | EXPIRE|FAIL) |
| 159 | if [ -n "$old_ip_address" ]; then |
| 160 | delete_old_address |
| 161 | delete_old_routes |
| 162 | fi |
| 163 | if [ -f /etc/resolv.conf.save ]; then |
| 164 | cat /etc/resolv.conf.save > /etc/resolv.conf |
| 165 | fi |
| 166 | ;; |
| 167 | |
| 168 | TIMEOUT) |
| 169 | add_new_address |
| 170 | sleep 1 |
| 171 | if [ -n "$new_routers" ]; then |
| 172 | set "$new_routers" |
| 173 | if ping -q -c 1 -w 1 "$1"; then |
| 174 | add_new_routes |
| 175 | if add_new_resolv_conf; then |
| 176 | exit 0 |
| 177 | fi |
| 178 | fi |
| 179 | fi |
| 180 | ifconfig $interface inet $new_ip_address delete |
| 181 | # XXX Why not a delete_old_address as before all other invocations of |
| 182 | # delete_old_routes? |
| 183 | delete_old_routes |
| 184 | exit 1 |
| 185 | ;; |
| 186 | esac |
| 187 | |
| 188 | exit 0 |