blob: 2e2115f8403fbba10f66e30cc18bdd0909e2c4fa [file] [log] [blame]
djm@openbsd.orgdd369322017-04-30 23:34:55 +00001# $OpenBSD: dynamic-forward.sh,v 1.12 2017/04/30 23:34:55 djm Exp $
Darren Tuckerb611c122003-06-28 12:42:09 +10002# Placed in the Public Domain.
3
4tid="dynamic forwarding"
5
Darren Tucker3b9c0ad2004-06-23 09:28:20 +10006FWDPORT=`expr $PORT + 1`
7
Darren Tucker68b184c2004-02-29 20:37:06 +11008if have_prog nc && nc -h 2>&1 | grep "proxy address" >/dev/null; then
Darren Tuckerb611c122003-06-28 12:42:09 +10009 proxycmd="nc -x 127.0.0.1:$FWDPORT -X"
Tim Rice23ee3592003-09-11 22:32:17 -070010elif have_prog connect; then
Darren Tuckerb611c122003-06-28 12:42:09 +100011 proxycmd="connect -S 127.0.0.1:$FWDPORT -"
12else
13 echo "skipped (no suitable ProxyCommand found)"
14 exit 0
15fi
16trace "will use ProxyCommand $proxycmd"
17
18start_sshd
19
markus@openbsd.orgdad2b182015-03-03 22:35:19 +000020for p in ${SSH_PROTOCOLS}; do
Darren Tuckerbf4d05a2011-06-03 14:19:02 +100021 n=0
22 error="1"
Darren Tuckerb611c122003-06-28 12:42:09 +100023 trace "start dynamic forwarding, fork to background"
Darren Tuckerbf4d05a2011-06-03 14:19:02 +100024 while [ "$error" -ne 0 -a "$n" -lt 3 ]; do
25 n=`expr $n + 1`
26 ${SSH} -$p -F $OBJ/ssh_config -f -D $FWDPORT -q \
27 -oExitOnForwardFailure=yes somehost exec sh -c \
28 \'"echo \$\$ > $OBJ/remote_pid; exec sleep 444"\'
29 error=$?
30 if [ "$error" -ne 0 ]; then
31 trace "forward failed proto $p attempt $n err $error"
32 sleep $n
33 fi
34 done
35 if [ "$error" -ne 0 ]; then
36 fatal "failed to start dynamic forwarding proto $p"
37 fi
Darren Tuckerb611c122003-06-28 12:42:09 +100038
Darren Tucker7a246622003-07-03 20:26:04 +100039 for s in 4 5; do
40 for h in 127.0.0.1 localhost; do
41 trace "testing ssh protocol $p socks version $s host $h"
42 ${SSH} -F $OBJ/ssh_config \
43 -o "ProxyCommand ${proxycmd}${s} $h $PORT" \
Darren Tuckerd199b6d2003-09-07 09:28:03 +100044 somehost cat $DATA > $OBJ/ls.copy
45 test -f $OBJ/ls.copy || fail "failed copy $DATA"
46 cmp $DATA $OBJ/ls.copy || fail "corrupted copy of $DATA"
Darren Tucker7a246622003-07-03 20:26:04 +100047 done
48 done
Darren Tuckerb611c122003-06-28 12:42:09 +100049
Darren Tucker7a246622003-07-03 20:26:04 +100050 if [ -f $OBJ/remote_pid ]; then
51 remote=`cat $OBJ/remote_pid`
52 trace "terminate remote shell, pid $remote"
53 if [ $remote -gt 1 ]; then
54 kill -HUP $remote
55 fi
56 else
57 fail "no pid file: $OBJ/remote_pid"
58 fi
Darren Tuckerb611c122003-06-28 12:42:09 +100059done