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