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