xref: /freebsd/crypto/openssh/regress/forwarding.sh (revision 076ad2f836d5f49dc1375f1677335a48fe0d4b82)
1#	$OpenBSD: forwarding.sh,v 1.16 2016/04/14 23:57:17 djm 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=""
13CTL=$OBJ/ctl-sock
14rm -f $CTL
15
16for j in 0 1 2; do
17	for i in 0 1 2; do
18		a=$base$j$i
19		b=`expr $a + 50`
20		c=$last
21		# fwd chain: $a -> $b -> $c
22		fwd="$fwd -L$a:127.0.0.1:$b -R$b:127.0.0.1:$c"
23		last=$a
24	done
25done
26for p in ${SSH_PROTOCOLS}; do
27	q=`expr 3 - $p`
28	if ! ssh_version $q; then
29		q=$p
30	fi
31	trace "start forwarding, fork to background"
32	${SSH} -$p -F $OBJ/ssh_config -f $fwd somehost sleep 10
33
34	trace "transfer over forwarded channels and check result"
35	${SSH} -$q -F $OBJ/ssh_config -p$last -o 'ConnectionAttempts=4' \
36		somehost cat ${DATA} > ${COPY}
37	test -s ${COPY}		|| fail "failed copy of ${DATA}"
38	cmp ${DATA} ${COPY}	|| fail "corrupted copy of ${DATA}"
39
40	sleep 10
41done
42
43for p in ${SSH_PROTOCOLS}; do
44for d in L R; do
45	trace "exit on -$d forward failure, proto $p"
46
47	# this one should succeed
48	${SSH} -$p -F $OBJ/ssh_config \
49	    -$d ${base}01:127.0.0.1:$PORT \
50	    -$d ${base}02:127.0.0.1:$PORT \
51	    -$d ${base}03:127.0.0.1:$PORT \
52	    -$d ${base}04:127.0.0.1:$PORT \
53	    -oExitOnForwardFailure=yes somehost true
54	if [ $? != 0 ]; then
55		fail "connection failed, should not"
56	else
57		# this one should fail
58		${SSH} -q -$p -F $OBJ/ssh_config \
59		    -$d ${base}01:127.0.0.1:$PORT \
60		    -$d ${base}02:127.0.0.1:$PORT \
61		    -$d ${base}03:127.0.0.1:$PORT \
62		    -$d ${base}01:localhost:$PORT \
63		    -$d ${base}04:127.0.0.1:$PORT \
64		    -oExitOnForwardFailure=yes somehost true
65		r=$?
66		if [ $r != 255 ]; then
67			fail "connection not termintated, but should ($r)"
68		fi
69	fi
70done
71done
72
73for p in ${SSH_PROTOCOLS}; do
74	trace "simple clear forwarding proto $p"
75	${SSH} -$p -F $OBJ/ssh_config -oClearAllForwardings=yes somehost true
76
77	trace "clear local forward proto $p"
78	${SSH} -$p -f -F $OBJ/ssh_config -L ${base}01:127.0.0.1:$PORT \
79	    -oClearAllForwardings=yes somehost sleep 10
80	if [ $? != 0 ]; then
81		fail "connection failed with cleared local forwarding"
82	else
83		# this one should fail
84		${SSH} -$p -F $OBJ/ssh_config -p ${base}01 true \
85		     >>$TEST_REGRESS_LOGFILE 2>&1 && \
86			fail "local forwarding not cleared"
87	fi
88	sleep 10
89
90	trace "clear remote forward proto $p"
91	${SSH} -$p -f -F $OBJ/ssh_config -R ${base}01:127.0.0.1:$PORT \
92	    -oClearAllForwardings=yes somehost sleep 10
93	if [ $? != 0 ]; then
94		fail "connection failed with cleared remote forwarding"
95	else
96		# this one should fail
97		${SSH} -$p -F $OBJ/ssh_config -p ${base}01 true \
98		     >>$TEST_REGRESS_LOGFILE 2>&1 && \
99			fail "remote forwarding not cleared"
100	fi
101	sleep 10
102done
103
104for p in 2; do
105	trace "stdio forwarding proto $p"
106	cmd="${SSH} -$p -F $OBJ/ssh_config"
107	$cmd -o "ProxyCommand $cmd -q -W localhost:$PORT somehost" \
108		somehost true
109	if [ $? != 0 ]; then
110		fail "stdio forwarding proto $p"
111	fi
112done
113
114echo "LocalForward ${base}01 127.0.0.1:$PORT" >> $OBJ/ssh_config
115echo "RemoteForward ${base}02 127.0.0.1:${base}01" >> $OBJ/ssh_config
116for p in ${SSH_PROTOCOLS}; do
117	trace "config file: start forwarding, fork to background"
118	${SSH} -S $CTL -M -$p -F $OBJ/ssh_config -f somehost sleep 10
119
120	trace "config file: transfer over forwarded channels and check result"
121	${SSH} -F $OBJ/ssh_config -p${base}02 -o 'ConnectionAttempts=4' \
122		somehost cat ${DATA} > ${COPY}
123	test -s ${COPY}		|| fail "failed copy of ${DATA}"
124	cmp ${DATA} ${COPY}	|| fail "corrupted copy of ${DATA}"
125
126	${SSH} -S $CTL -O exit somehost
127done
128
129for p in 2; do
130	trace "transfer over chained unix domain socket forwards and check result"
131	rm -f $OBJ/unix-[123].fwd
132	${SSH} -f -F $OBJ/ssh_config -R${base}01:[$OBJ/unix-1.fwd] somehost sleep 10
133	${SSH} -f -F $OBJ/ssh_config -L[$OBJ/unix-1.fwd]:[$OBJ/unix-2.fwd] somehost sleep 10
134	${SSH} -f -F $OBJ/ssh_config -R[$OBJ/unix-2.fwd]:[$OBJ/unix-3.fwd] somehost sleep 10
135	${SSH} -f -F $OBJ/ssh_config -L[$OBJ/unix-3.fwd]:127.0.0.1:$PORT somehost sleep 10
136	${SSH} -F $OBJ/ssh_config -p${base}01 -o 'ConnectionAttempts=4' \
137		somehost cat ${DATA} > ${COPY}
138	test -s ${COPY}			|| fail "failed copy ${DATA}"
139	cmp ${DATA} ${COPY}		|| fail "corrupted copy of ${DATA}"
140
141	#wait
142	sleep 10
143done
144