blob: 39fccba7385a67fd6293088786e75d3068cd7dd4 [file] [log] [blame]
djm@openbsd.orgdd369322017-04-30 23:34:55 +00001# $OpenBSD: forwarding.sh,v 1.20 2017/04/30 23:34:55 djm Exp $
Damien Miller38cd4352002-05-01 13:17:33 +10002# Placed in the Public Domain.
3
4tid="local and remote forwarding"
5
Darren Tucker2a228732013-06-06 01:59:13 +10006DATA=/bin/ls${EXEEXT}
7
Damien Miller38cd4352002-05-01 13:17:33 +10008start_sshd
9
10base=33
11last=$PORT
12fwd=""
Damien Miller9165abf2017-03-20 09:58:34 +110013CTL=/tmp/openssh.regress.ctl-sock.$$
djm@openbsd.orgb3c19152015-02-23 20:32:15 +000014
Damien Miller38cd4352002-05-01 13:17:33 +100015for 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
Damien Miller38cd4352002-05-01 13:17:33 +100025
djm@openbsd.orgdd369322017-04-30 23:34:55 +000026trace "start forwarding, fork to background"
27rm -f $CTL
28${SSH} -S $CTL -M -F $OBJ/ssh_config -f $fwd somehost sleep 10
Damien Miller38cd4352002-05-01 13:17:33 +100029
djm@openbsd.orgdd369322017-04-30 23:34:55 +000030trace "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}"
Damien Milleredaeff52006-01-31 21:56:24 +110035
djm@openbsd.orgdd369322017-04-30 23:34:55 +000036${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost
37
Damien Miller7b1877c2006-07-24 15:31:41 +100038for d in L R; do
djm@openbsd.orgdd369322017-04-30 23:34:55 +000039 trace "exit on -$d forward failure"
Damien Miller7b1877c2006-07-24 15:31:41 +100040
41 # this one should succeed
djm@openbsd.orgdd369322017-04-30 23:34:55 +000042 ${SSH} -F $OBJ/ssh_config \
Damien Miller7b1877c2006-07-24 15:31:41 +100043 -$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
djm@openbsd.orgf25ee132017-01-30 05:22:14 +000049 fatal "connection failed, should not"
Damien Miller7b1877c2006-07-24 15:31:41 +100050 else
51 # this one should fail
djm@openbsd.orgdd369322017-04-30 23:34:55 +000052 ${SSH} -q -F $OBJ/ssh_config \
Damien Miller7b1877c2006-07-24 15:31:41 +100053 -$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 \
djm@openbsd.org6b8a1a82016-04-14 23:57:17 +000056 -$d ${base}01:localhost:$PORT \
Damien Miller7b1877c2006-07-24 15:31:41 +100057 -$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
Damien Miller7b1877c2006-07-24 15:31:41 +100065
djm@openbsd.orgdd369322017-04-30 23:34:55 +000066trace "simple clear forwarding"
67${SSH} -F $OBJ/ssh_config -oClearAllForwardings=yes somehost true
Damien Milleredaeff52006-01-31 21:56:24 +110068
djm@openbsd.orgdd369322017-04-30 23:34:55 +000069trace "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
Damien Miller0dff9c72010-02-24 17:25:58 +110082
djm@openbsd.orgdd369322017-04-30 23:34:55 +000083trace "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
Damien Miller78d47b72013-07-25 12:08:46 +1000103
Tim Rice0553ad72013-07-25 16:03:16 -0700104echo "LocalForward ${base}01 127.0.0.1:$PORT" >> $OBJ/ssh_config
105echo "RemoteForward ${base}02 127.0.0.1:${base}01" >> $OBJ/ssh_config
Damien Miller78d47b72013-07-25 12:08:46 +1000106
djm@openbsd.orgdd369322017-04-30 23:34:55 +0000107trace "config file: start forwarding, fork to background"
108rm -f $CTL
109${SSH} -S $CTL -M -F $OBJ/ssh_config -f somehost sleep 10
Damien Miller78d47b72013-07-25 12:08:46 +1000110
djm@openbsd.orgdd369322017-04-30 23:34:55 +0000111trace "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}"
Damien Miller0e4e9552014-07-21 09:52:54 +1000116
djm@openbsd.orgdd369322017-04-30 23:34:55 +0000117${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost
Damien Miller0e4e9552014-07-21 09:52:54 +1000118
djm@openbsd.orgdd369322017-04-30 23:34:55 +0000119trace "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