| 1 |
#! /bin/bash |
| 2 |
|
| 3 |
### Script to set up an iodine tunnel route traffic through it |
| 4 |
### |
| 5 |
### Copyright 2008 Barak A. Pearlmutter <[email protected]> |
| 6 |
### |
| 7 |
### License: MIT |
| 8 |
### |
| 9 |
### Permission to use, copy, modify, and distribute this software for |
| 10 |
### any purpose with or without fee is hereby granted, provided that |
| 11 |
### the above copyright notice and this permission notice appear in |
| 12 |
### all copies. |
| 13 |
### |
| 14 |
### THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL |
| 15 |
### WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED |
| 16 |
### WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE |
| 17 |
### AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR |
| 18 |
### CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 19 |
### LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| 20 |
### NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 21 |
### CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 |
|
| 23 |
## Cause script to bail immediately on failed command |
| 24 |
set -e |
| 25 |
|
| 26 |
function usage |
| 27 |
{ |
| 28 |
cat <<EOF |
| 29 |
'iodine-client-start' starts an iodine IP-over-DNS tunnel. |
| 30 |
|
| 31 |
Usage: iodine-client-start [option] |
| 32 |
|
| 33 |
-h, --help Print help and exit |
| 34 |
-v, --version Print version info and exit |
| 35 |
|
| 36 |
Invoking the program without options attempts to set up and configure |
| 37 |
an iodine IP-over-DNS tunnel using the configuration in the file |
| 38 |
/etc/default/iodine-client or by querying the user. It tries to |
| 39 |
figure out the right way to set things up by observing the network, |
| 40 |
and if all else fails by guessing. |
| 41 |
|
| 42 |
QUICK CONFIGURATION |
| 43 |
|
| 44 |
Put two lines in the file /etc/default/iodine-client |
| 45 |
|
| 46 |
subdomain=your.tunnel.sub.domain |
| 47 |
|
| 48 |
passwd=password_for_that_tunnel |
| 49 |
|
| 50 |
|
| 51 |
or invoke the script with those environment variables set: |
| 52 |
|
| 53 |
env subdomain=xxx passwd=xxx iodine-client-start |
| 54 |
|
| 55 |
If these are not set, the script will query the user for them. |
| 56 |
|
| 57 |
DETAILS |
| 58 |
|
| 59 |
The configuration file consists of lines which are either comments |
| 60 |
starting with '#', or settings of the form VAR="val". Valid VARs are: |
| 61 |
|
| 62 |
subdomain |
| 63 |
Sample value: your.tunnel.sub.domain (no default, must be set) |
| 64 |
|
| 65 |
passwd |
| 66 |
Sample value: password_for_that_tunnel (no default, must be set) |
| 67 |
|
| 68 |
testhost |
| 69 |
Hostname to ping when testing if network is working (default: |
| 70 |
slashdot.org) |
| 71 |
|
| 72 |
bounce_localnet |
| 73 |
Take the local network down and then up again before starting |
| 74 |
tunnel (default: false) |
| 75 |
|
| 76 |
test_ping_localnet |
| 77 |
Test if the local network is working by pinging the gateway |
| 78 |
(default: true) |
| 79 |
|
| 80 |
test_ping_tunnel |
| 81 |
Test if the iodine tunnel is working after it has been set up by |
| 82 |
pinging the host at the other end (default: true) |
| 83 |
|
| 84 |
test_ping_final |
| 85 |
Test if the tunnel is working after everything is ostensibly set |
| 86 |
up by trying to ping an external host (default: true) |
| 87 |
|
| 88 |
default_router |
| 89 |
IP address of router on the local network---should be found |
| 90 |
automatically, set this if that fails and the program guesses wrong. |
| 91 |
|
| 92 |
interface |
| 93 |
Interface to use (e.g., eth1, eth0, etc) for connection to DNS |
| 94 |
server used for the iodine tunnel---should be found automatically, |
| 95 |
set this if that fails and the program guesses wrong. |
| 96 |
|
| 97 |
mtu |
| 98 |
Set if tunnel MTU needs to be manually changed (lowered). Should |
| 99 |
not be necessary anymore, as recent versions of iodine negotiate |
| 100 |
an appropriate MTU during tunnel setup. But if that negotiation |
| 101 |
does not happen, or if you are using an older version of iodine, |
| 102 |
the default tunnel MTU is 1024, and if the local DNS server |
| 103 |
restricts to 512 byte packets you might need to use an MTU of 220. |
| 104 |
|
| 105 |
skip_raw_udp_mode |
| 106 |
Set "-r" option in iodine command line. With this option, iodine |
| 107 |
does not try to establish a direct UDP socket to the iodine server |
| 108 |
on port 53. (default: true). |
| 109 |
|
| 110 |
continue_on_error |
| 111 |
Set if the script should continue even if a command fails. |
| 112 |
Use to test script when running as non-root. Defaults to false |
| 113 |
if running as root, true otherwise. |
| 114 |
EOF |
| 115 |
} |
| 116 |
|
| 117 |
function version |
| 118 |
{ |
| 119 |
echo iodine-client-start 1.0.6 |
| 120 |
} |
| 121 |
|
| 122 |
case $# in |
| 123 |
0) |
| 124 |
;; |
| 125 |
1) |
| 126 |
case "$1" in |
| 127 |
# start) |
| 128 |
# ;; |
| 129 |
# stop) |
| 130 |
# ;; |
| 131 |
# restart) |
| 132 |
# ;; |
| 133 |
--version|-v) |
| 134 |
version |
| 135 |
exit |
| 136 |
;; |
| 137 |
--help|-h) |
| 138 |
usage |
| 139 |
exit |
| 140 |
;; |
| 141 |
*) |
| 142 |
echo error: unknown option "$1" |
| 143 |
exit 1 |
| 144 |
esac |
| 145 |
exit |
| 146 |
;; |
| 147 |
*) |
| 148 |
echo error: too many arguments "$*" |
| 149 |
exit 1 |
| 150 |
;; |
| 151 |
esac |
| 152 |
|
| 153 |
echo "${iodine_client_rc:=/etc/default/iodine-client}" > /dev/null |
| 154 |
|
| 155 |
if [ -r ${iodine_client_rc} ]; then |
| 156 |
. ${iodine_client_rc} |
| 157 |
else |
| 158 |
echo WARNING: Cannot read ${iodine_client_rc} |
| 159 |
fi |
| 160 |
|
| 161 |
if [ -z ${subdomain} ]; then |
| 162 |
read -p "DNS tunnel DNS subdomain: " subdomain |
| 163 |
fi |
| 164 |
|
| 165 |
if [ -z ${subdomain} ]; then |
| 166 |
echo ERROR: Must set subdomain. |
| 167 |
exit 1 |
| 168 |
fi |
| 169 |
|
| 170 |
if [ -z ${passwd} ]; then |
| 171 |
read -p "Password for DNS tunnel over ${subdomain}: " passwd |
| 172 |
fi |
| 173 |
|
| 174 |
## This is a host name used for testing DNS and for pinging |
| 175 |
echo "${testhost:=slashdot.org}" > /dev/null |
| 176 |
|
| 177 |
## Set if local network should be taken down and then up |
| 178 |
echo "${bounce_localnet:=false}" > /dev/null |
| 179 |
|
| 180 |
## Set for testing network availability via ping at various points |
| 181 |
echo "${test_ping_localnet:=true}" > /dev/null |
| 182 |
echo "${test_ping_tunnel:=true}" > /dev/null |
| 183 |
echo "${test_ping_final:=true}" > /dev/null |
| 184 |
|
| 185 |
## Set if the script cannot find and then incorrectly guesses the |
| 186 |
## local network router |
| 187 |
echo "${default_router}" > /dev/null |
| 188 |
|
| 189 |
## Set if script uses the wrong hardware interface |
| 190 |
echo "${interface}" > /dev/null |
| 191 |
|
| 192 |
## Set if MTU needs to be manually altered (lowered) |
| 193 |
## - the default tunnel MTU is 1024. |
| 194 |
## - if local DNS server restricts to 512 byte packets then use MTU 220 |
| 195 |
echo "${mtu}" > /dev/null |
| 196 |
|
| 197 |
## Set it if you want try RAW udp mode |
| 198 |
echo "${skip_raw_udp_mode:=true}" > /dev/null |
| 199 |
|
| 200 |
## Set if the script should continue even if a command fails. |
| 201 |
## Used to test script when running as non-root. |
| 202 |
if [ $(whoami) = root ]; then |
| 203 |
echo "${continue_on_error:=false}" > /dev/null |
| 204 |
else |
| 205 |
echo "${continue_on_error:=true}" > /dev/null |
| 206 |
fi |
| 207 |
|
| 208 |
## DEBIAN PACKAGES TO INSTALL: these are needed to run this script |
| 209 |
## iodine (for /usr/sbin/iodine) |
| 210 |
## iproute (for /bin/ip) |
| 211 |
## ipcalc (for /usr/bin/ipcalc) |
| 212 |
## dnsutils (for /usr/bin/dig) |
| 213 |
## fping (for /usr/bin/fping) |
| 214 |
## or oping (for /usr/bin/oping) |
| 215 |
## gawk (for /usr/bin/gawk, to use gensub()) |
| 216 |
|
| 217 |
if type -P fping > /dev/null; then |
| 218 |
ping_cmd="fping -C1" |
| 219 |
elif type -P oping > /dev/null; then |
| 220 |
ping_cmd="oping -c1" |
| 221 |
else |
| 222 |
ping_cmd="echo would ping" |
| 223 |
fi |
| 224 |
|
| 225 |
## TO DO |
| 226 |
## - avoid double ping when DNS server and local router are the same |
| 227 |
## - option to not kill existing iodine DNS tunnels, in case there |
| 228 |
## are meant to be more than one |
| 229 |
## - sanify check whether default_router is on local network |
| 230 |
|
| 231 |
echo ==== Creating IP-over-DNS tunnel over local network connection... |
| 232 |
|
| 233 |
|
| 234 |
## Find a network interface |
| 235 |
|
| 236 |
if [ -z ${interface} ]; then |
| 237 |
interfaces=$(tail --lines=+3 /proc/net/wireless \ |
| 238 |
| tr -d : | awk '{print $1}') |
| 239 |
for dev in ${interfaces}; do |
| 240 |
if ip -oneline -4 addr show dev ${dev} | grep -q inet; then |
| 241 |
interface=${dev} |
| 242 |
fi |
| 243 |
done |
| 244 |
fi |
| 245 |
|
| 246 |
if [ -z ${interface} ]; then |
| 247 |
interface=$(ip -oneline link show | awk '$16=="link/ether" {print $2}' | tr -d : | head -1) |
| 248 |
fi |
| 249 |
|
| 250 |
if [ -z ${interface} ]; then |
| 251 |
echo ERROR: No network interface found. |
| 252 |
exit 1 |
| 253 |
fi |
| 254 |
|
| 255 |
echo ==== Local network interface: ${interface} |
| 256 |
|
| 257 |
## Down any existing DNS tunnel (wish there were "approved" way to do this) |
| 258 |
|
| 259 |
echo ==== Killing existing DNS tunnels... |
| 260 |
if killall --quiet --wait --verbose --signal HUP iodine; then |
| 261 |
sleep 2 |
| 262 |
fi |
| 263 |
|
| 264 |
## Stabilize local network |
| 265 |
|
| 266 |
if ${bounce_localnet}; then |
| 267 |
echo ==== Bouncing local network connection... |
| 268 |
ifdown --force ${interface} || true |
| 269 |
ifup ${interface} || ${continue_on_error} |
| 270 |
fi |
| 271 |
|
| 272 |
## Fetch some information about the local network |
| 273 |
|
| 274 |
addr=$(ip -oneline -4 addr show dev ${interface} scope global \ |
| 275 |
| awk '{print $4}' | head -1) |
| 276 |
prefix_len=$(echo ${addr} | sed 'sX^.*/XX') |
| 277 |
local_net=$(ipcalc --nobinary ${addr} | awk '$1=="Network:" {print $2}') |
| 278 |
|
| 279 |
echo ==== Local address: ${addr} |
| 280 |
echo ==== Local network: ${local_net} |
| 281 |
|
| 282 |
router=$(ip -oneline -4 route list dev ${interface} default \ |
| 283 |
| awk '{print $3}' | head -1) |
| 284 |
if [ -z ${router} ]; then |
| 285 |
## This can happen when the default local route is already deleted |
| 286 |
if [ -z ${default_router} ]; then |
| 287 |
echo WARNING: no default route, guessing local router IP address. |
| 288 |
## Minimum address on local net is usually right |
| 289 |
router=$(ipcalc --nobinary ${addr} | awk '$1=="HostMin:" {print $2}') |
| 290 |
else |
| 291 |
echo WARNING: no default route, using configured default router. |
| 292 |
## But sometimes need to hardwire... |
| 293 |
router=${default_router} |
| 294 |
fi |
| 295 |
fi |
| 296 |
|
| 297 |
echo ==== Local network router: ${router} |
| 298 |
|
| 299 |
## Test DNS service |
| 300 |
|
| 301 |
testhost_ip=$(dig +short -t A -q ${testhost}) |
| 302 |
if [ -z ${testhost_ip} ]; then |
| 303 |
echo WARNING: Failure on DNS lookup of ${testhost}. |
| 304 |
fi |
| 305 |
|
| 306 |
## fetch DNS servers |
| 307 |
|
| 308 |
nameservers=$(awk '$1=="nameserver" {print $2}' /etc/resolv.conf) |
| 309 |
if [ -n "${nameservers}" ]; then |
| 310 |
echo ==== DNS servers: ${nameservers} |
| 311 |
else |
| 312 |
echo ERROR: No DNS servers found. |
| 313 |
exit 1 |
| 314 |
fi |
| 315 |
|
| 316 |
## Test if local network is up |
| 317 |
|
| 318 |
if ${test_ping_localnet}; then |
| 319 |
echo ==== Ping test of local network router and DNS servers... |
| 320 |
${ping_cmd} ${router} ${nameservers} \ |
| 321 |
|| echo WARNING: Ping test failed. |
| 322 |
fi |
| 323 |
|
| 324 |
## Add point-to-point routes for any non-local DNS servers |
| 325 |
|
| 326 |
for n in ${nameservers}; do |
| 327 |
n_net=$(ipcalc --nobinary ${n}/${prefix_len} | awk '$1=="Network:" {print $2}') |
| 328 |
n_net8=$(ipcalc --nobinary ${n}/8 | awk '$1=="Network:" {print $2}') |
| 329 |
if [ "${n_net}" != "${local_net}" ]; then |
| 330 |
if [ "${n_net8}" != "127.0.0.0/8" ]; then |
| 331 |
echo ==== Adding point-to-point route for DNS server ${n} |
| 332 |
## remove point-to-point route first, in case it is already present |
| 333 |
ip -4 route del ${n}/32 || true |
| 334 |
ip -4 route add ${n}/32 via ${router} || ${continue_on_error} |
| 335 |
fi |
| 336 |
fi |
| 337 |
done |
| 338 |
|
| 339 |
## Bring up DNS tunnel |
| 340 |
|
| 341 |
echo ==== Creating IP-over-DNS tunnel... |
| 342 |
if ${skip_raw_udp_mode}; then |
| 343 |
iodine_opts="${iodine_opts} -r" |
| 344 |
fi |
| 345 |
|
| 346 |
iodine ${iodine_opts} -P "${passwd}" "${subdomain}" || ${continue_on_error} |
| 347 |
|
| 348 |
## Find DNS tunnel interface |
| 349 |
tunnel_interface=$(ip -oneline link show | gawk '$2 ~ /^dns/ {print $2}' | tr -d : | head -1) |
| 350 |
if [ -z "${tunnel_interface}" ]; then |
| 351 |
echo WARNING: Cannot find DNS tunnel interface, using default. |
| 352 |
tunnel_interface=dns0 |
| 353 |
fi |
| 354 |
echo ==== DNS tunnel interface: ${tunnel_interface} |
| 355 |
|
| 356 |
## Maybe try to change MTU |
| 357 |
|
| 358 |
if [ -n "${mtu}" ]; then |
| 359 |
echo ==== Setting MTU of ${tunnel_interface} to ${mtu} |
| 360 |
ip link set ${tunnel_interface} mtu ${mtu} |
| 361 |
fi |
| 362 |
|
| 363 |
## Figure out router at other end of tunnel, assuming router uses final octet .1 |
| 364 |
## (There should be some way to get this information out of iodine, since |
| 365 |
## it *prints* it as it sets up the tunnel, so it does know it.) |
| 366 |
|
| 367 |
tunnel_remote=$(ip -oneline -4 address show dev ${tunnel_interface} \ |
| 368 |
| gawk '$3=="inet" {print gensub("[.][0-9]*/.*", ".1", 1, $4)}' | head -1) |
| 369 |
|
| 370 |
if [ -z ${tunnel_remote} ]; then |
| 371 |
echo ERROR: Cannot find DNS tunnel remote endpoint. |
| 372 |
${continue_on_error} |
| 373 |
## set something random if debugging |
| 374 |
echo WARNING: Confabulating DNS tunnel remote endpoint. |
| 375 |
tunnel_remote=192.168.253.1 |
| 376 |
fi |
| 377 |
|
| 378 |
echo ==== DNS tunnel remote endpoint: ${tunnel_remote} |
| 379 |
|
| 380 |
if ${test_ping_tunnel}; then |
| 381 |
echo ==== Ping test of local router, nameserver, and DNS tunnel... |
| 382 |
${ping_cmd} ${router} ${nameservers} ${tunnel_remote} \ |
| 383 |
|| echo WARNING: Ping test failed. |
| 384 |
fi |
| 385 |
|
| 386 |
## Modify routing table to send trafic via DNS tunnel |
| 387 |
|
| 388 |
echo ==== Setting default route through DNS tunnel... |
| 389 |
|
| 390 |
## Remove default route via local router |
| 391 |
ip -4 route del default via ${router} || echo WARNING: No default route to delete |
| 392 |
## Add default via tunnel |
| 393 |
ip -4 route add default via ${tunnel_remote} || ${continue_on_error} |
| 394 |
|
| 395 |
## Test if all is well |
| 396 |
|
| 397 |
if ${test_ping_final}; then |
| 398 |
echo ==== Ping test of local router, nameserver, DNS tunnel, external test host... |
| 399 |
${ping_cmd} ${router} ${nameservers} ${tunnel_remote} ${testhost_ip:-${testhost}} \ |
| 400 |
|| echo WARNING: Ping test failed. |
| 401 |
fi |