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