1# $OpenBSD: dynamic-forward.sh,v 1.9 2011/06/03 00:29:52 dtucker Exp $ 2# Placed in the Public Domain. 3 4tid="dynamic forwarding" 5 6FWDPORT=`expr $PORT + 1` 7 8DATA=/bin/ls${EXEEXT} 9 10if have_prog nc && nc -h 2>&1 | grep "proxy address" >/dev/null; then 11 proxycmd="nc -x 127.0.0.1:$FWDPORT -X" 12elif have_prog connect; then 13 proxycmd="connect -S 127.0.0.1:$FWDPORT -" 14else 15 echo "skipped (no suitable ProxyCommand found)" 16 exit 0 17fi 18trace "will use ProxyCommand $proxycmd" 19 20start_sshd 21 22for p in 1 2; do 23 n=0 24 error="1" 25 trace "start dynamic forwarding, fork to background" 26 while [ "$error" -ne 0 -a "$n" -lt 3 ]; do 27 n=`expr $n + 1` 28 ${SSH} -$p -F $OBJ/ssh_config -f -D $FWDPORT -q \ 29 -oExitOnForwardFailure=yes somehost exec sh -c \ 30 \'"echo \$\$ > $OBJ/remote_pid; exec sleep 444"\' 31 error=$? 32 if [ "$error" -ne 0 ]; then 33 trace "forward failed proto $p attempt $n err $error" 34 sleep $n 35 fi 36 done 37 if [ "$error" -ne 0 ]; then 38 fatal "failed to start dynamic forwarding proto $p" 39 fi 40 41 for s in 4 5; do 42 for h in 127.0.0.1 localhost; do 43 trace "testing ssh protocol $p socks version $s host $h" 44 ${SSH} -F $OBJ/ssh_config \ 45 -o "ProxyCommand ${proxycmd}${s} $h $PORT" \ 46 somehost cat $DATA > $OBJ/ls.copy 47 test -f $OBJ/ls.copy || fail "failed copy $DATA" 48 cmp $DATA $OBJ/ls.copy || fail "corrupted copy of $DATA" 49 done 50 done 51 52 if [ -f $OBJ/remote_pid ]; then 53 remote=`cat $OBJ/remote_pid` 54 trace "terminate remote shell, pid $remote" 55 if [ $remote -gt 1 ]; then 56 kill -HUP $remote 57 fi 58 else 59 fail "no pid file: $OBJ/remote_pid" 60 fi 61done 62