1#	$OpenBSD: forwarding.sh,v 1.7 2010/01/11 02:53:44 dtucker Exp $
2#	Placed in the Public Domain.
3
4tid="local and remote forwarding"
5DATA=/bin/ls${EXEEXT}
6
7start_sshd
8
9base=33
10last=$PORT
11fwd=""
12for j in 0 1 2; do
13	for i in 0 1 2; do
14		a=$base$j$i
15		b=`expr $a + 50`
16		c=$last
17		# fwd chain: $a -> $b -> $c
18		fwd="$fwd -L$a:127.0.0.1:$b -R$b:127.0.0.1:$c"
19		last=$a
20	done
21done
22for p in 1 2; do
23	q=`expr 3 - $p`
24	trace "start forwarding, fork to background"
25	${SSH} -$p -F $OBJ/ssh_config -f $fwd somehost sleep 10
26
27	trace "transfer over forwarded channels and check result"
28	${SSH} -$q -F $OBJ/ssh_config -p$last -o 'ConnectionAttempts=4' \
29		somehost cat $DATA > $OBJ/ls.copy
30	test -f $OBJ/ls.copy			|| fail "failed copy $DATA"
31	cmp $DATA $OBJ/ls.copy			|| fail "corrupted copy of $DATA"
32
33	sleep 10
34done
35
36for p in 1 2; do
37for d in L R; do
38	trace "exit on -$d forward failure, proto $p"
39
40	# this one should succeed
41	${SSH} -$p -F $OBJ/ssh_config \
42	    -$d ${base}01:127.0.0.1:$PORT \
43	    -$d ${base}02:127.0.0.1:$PORT \
44	    -$d ${base}03:127.0.0.1:$PORT \
45	    -$d ${base}04:127.0.0.1:$PORT \
46	    -oExitOnForwardFailure=yes somehost true
47	if [ $? != 0 ]; then
48		fail "connection failed, should not"
49	else
50		# this one should fail
51		${SSH} -q -$p -F $OBJ/ssh_config \
52		    -$d ${base}01:127.0.0.1:$PORT \
53		    -$d ${base}02:127.0.0.1:$PORT \
54		    -$d ${base}03:127.0.0.1:$PORT \
55		    -$d ${base}01:127.0.0.1:$PORT \
56		    -$d ${base}04:127.0.0.1:$PORT \
57		    -oExitOnForwardFailure=yes somehost true
58		r=$?
59		if [ $r != 255 ]; then
60			fail "connection not termintated, but should ($r)"
61		fi
62	fi
63done
64done
65
66for p in 1 2; do
67	trace "simple clear forwarding proto $p"
68	${SSH} -$p -F $OBJ/ssh_config -oClearAllForwardings=yes somehost true
69
70	trace "clear local forward proto $p"
71	${SSH} -$p -f -F $OBJ/ssh_config -L ${base}01:127.0.0.1:$PORT \
72	    -oClearAllForwardings=yes somehost sleep 10
73	if [ $? != 0 ]; then
74		fail "connection failed with cleared local forwarding"
75	else
76		# this one should fail
77		${SSH} -$p -F $OBJ/ssh_config -p ${base}01 true \
78		     2>${TEST_SSH_LOGFILE} && \
79			fail "local forwarding not cleared"
80	fi
81	sleep 10
82	
83	trace "clear remote forward proto $p"
84	${SSH} -$p -f -F $OBJ/ssh_config -R ${base}01:127.0.0.1:$PORT \
85	    -oClearAllForwardings=yes somehost sleep 10
86	if [ $? != 0 ]; then
87		fail "connection failed with cleared remote forwarding"
88	else
89		# this one should fail
90		${SSH} -$p -F $OBJ/ssh_config -p ${base}01 true \
91		     2>${TEST_SSH_LOGFILE} && \
92			fail "remote forwarding not cleared"
93	fi
94	sleep 10
95done
96
97for p in 2; do
98	trace "stdio forwarding proto $p"
99	cmd="${SSH} -$p -F $OBJ/ssh_config"
100	$cmd -o "ProxyCommand $cmd -q -W localhost:$PORT somehost" \
101		somehost true
102	if [ $? != 0 ]; then
103		fail "stdio forwarding proto $p"
104	fi
105done
106