Darren Tucker | c0dc1c9 | 2004-12-06 22:58:11 +1100 | [diff] [blame] | 1 | # $OpenBSD: dynamic-forward.sh,v 1.4 2004/06/22 22:55:56 dtucker Exp $ |
Darren Tucker | b611c12 | 2003-06-28 12:42:09 +1000 | [diff] [blame] | 2 | # Placed in the Public Domain. |
| 3 | |
| 4 | tid="dynamic forwarding" |
| 5 | |
Darren Tucker | 3b9c0ad | 2004-06-23 09:28:20 +1000 | [diff] [blame] | 6 | FWDPORT=`expr $PORT + 1` |
| 7 | |
Darren Tucker | d199b6d | 2003-09-07 09:28:03 +1000 | [diff] [blame] | 8 | DATA=/bin/ls${EXEEXT} |
Darren Tucker | b611c12 | 2003-06-28 12:42:09 +1000 | [diff] [blame] | 9 | |
Darren Tucker | 68b184c | 2004-02-29 20:37:06 +1100 | [diff] [blame] | 10 | if have_prog nc && nc -h 2>&1 | grep "proxy address" >/dev/null; then |
Darren Tucker | b611c12 | 2003-06-28 12:42:09 +1000 | [diff] [blame] | 11 | proxycmd="nc -x 127.0.0.1:$FWDPORT -X" |
Tim Rice | 23ee359 | 2003-09-11 22:32:17 -0700 | [diff] [blame] | 12 | elif have_prog connect; then |
Darren Tucker | b611c12 | 2003-06-28 12:42:09 +1000 | [diff] [blame] | 13 | proxycmd="connect -S 127.0.0.1:$FWDPORT -" |
| 14 | else |
| 15 | echo "skipped (no suitable ProxyCommand found)" |
| 16 | exit 0 |
| 17 | fi |
| 18 | trace "will use ProxyCommand $proxycmd" |
| 19 | |
| 20 | start_sshd |
| 21 | |
| 22 | for p in 1 2; do |
Darren Tucker | b611c12 | 2003-06-28 12:42:09 +1000 | [diff] [blame] | 23 | trace "start dynamic forwarding, fork to background" |
Darren Tucker | 7a24662 | 2003-07-03 20:26:04 +1000 | [diff] [blame] | 24 | ${SSH} -$p -F $OBJ/ssh_config -f -D $FWDPORT -q somehost \ |
| 25 | exec sh -c \'"echo \$\$ > $OBJ/remote_pid; exec sleep 444"\' |
Darren Tucker | b611c12 | 2003-06-28 12:42:09 +1000 | [diff] [blame] | 26 | |
Darren Tucker | 7a24662 | 2003-07-03 20:26:04 +1000 | [diff] [blame] | 27 | for s in 4 5; do |
| 28 | for h in 127.0.0.1 localhost; do |
| 29 | trace "testing ssh protocol $p socks version $s host $h" |
| 30 | ${SSH} -F $OBJ/ssh_config \ |
| 31 | -o "ProxyCommand ${proxycmd}${s} $h $PORT" \ |
Darren Tucker | d199b6d | 2003-09-07 09:28:03 +1000 | [diff] [blame] | 32 | somehost cat $DATA > $OBJ/ls.copy |
| 33 | test -f $OBJ/ls.copy || fail "failed copy $DATA" |
| 34 | cmp $DATA $OBJ/ls.copy || fail "corrupted copy of $DATA" |
Darren Tucker | 7a24662 | 2003-07-03 20:26:04 +1000 | [diff] [blame] | 35 | done |
| 36 | done |
Darren Tucker | b611c12 | 2003-06-28 12:42:09 +1000 | [diff] [blame] | 37 | |
Darren Tucker | 7a24662 | 2003-07-03 20:26:04 +1000 | [diff] [blame] | 38 | if [ -f $OBJ/remote_pid ]; then |
| 39 | remote=`cat $OBJ/remote_pid` |
| 40 | trace "terminate remote shell, pid $remote" |
| 41 | if [ $remote -gt 1 ]; then |
| 42 | kill -HUP $remote |
| 43 | fi |
| 44 | else |
| 45 | fail "no pid file: $OBJ/remote_pid" |
| 46 | fi |
Darren Tucker | 07d30e4 | 2004-08-29 17:14:31 +1000 | [diff] [blame] | 47 | |
| 48 | # Must allow time for connection tear-down |
| 49 | sleep 2 |
Darren Tucker | b611c12 | 2003-06-28 12:42:09 +1000 | [diff] [blame] | 50 | done |