1 # $OpenBSD: forwarding.sh,v 1.19 2017/01/30 05:22:14 djm Exp $
2 # Placed in the Public Domain.
4 tid="local and remote forwarding"
13 CTL=/tmp/openssh.regress.ctl-sock.$$
20 # fwd chain: $a -> $b -> $c
21 fwd="$fwd -L$a:127.0.0.1:$b -R$b:127.0.0.1:$c"
25 for p in ${SSH_PROTOCOLS}; do
27 if ! ssh_version $q; then
30 trace "start forwarding, fork to background"
32 ${SSH} -S $CTL -M -$p -F $OBJ/ssh_config -f $fwd somehost sleep 10
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}"
40 ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost
43 for p in ${SSH_PROTOCOLS}; do
45 trace "exit on -$d forward failure, proto $p"
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
55 fatal "connection failed, should not"
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
66 if [ $r != 255 ]; then
67 fail "connection not termintated, but should ($r)"
73 for p in ${SSH_PROTOCOLS}; do
74 trace "simple clear forwarding proto $p"
75 ${SSH} -$p -F $OBJ/ssh_config -oClearAllForwardings=yes somehost true
77 trace "clear local forward proto $p"
79 ${SSH} -S $CTL -M -$p -f -F $OBJ/ssh_config -L ${base}01:127.0.0.1:$PORT \
80 -oClearAllForwardings=yes somehost sleep 10
82 fail "connection failed with cleared local forwarding"
84 # this one should fail
85 ${SSH} -$p -F $OBJ/ssh_config -p ${base}01 somehost true \
86 >>$TEST_REGRESS_LOGFILE 2>&1 && \
87 fail "local forwarding not cleared"
89 ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost
91 trace "clear remote forward proto $p"
93 ${SSH} -S $CTL -M -$p -f -F $OBJ/ssh_config -R ${base}01:127.0.0.1:$PORT \
94 -oClearAllForwardings=yes somehost sleep 10
96 fail "connection failed with cleared remote forwarding"
98 # this one should fail
99 ${SSH} -$p -F $OBJ/ssh_config -p ${base}01 somehost true \
100 >>$TEST_REGRESS_LOGFILE 2>&1 && \
101 fail "remote forwarding not cleared"
103 ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost
107 trace "stdio forwarding proto $p"
108 cmd="${SSH} -$p -F $OBJ/ssh_config"
109 $cmd -o "ProxyCommand $cmd -q -W localhost:$PORT somehost" \
112 fail "stdio forwarding proto $p"
116 echo "LocalForward ${base}01 127.0.0.1:$PORT" >> $OBJ/ssh_config
117 echo "RemoteForward ${base}02 127.0.0.1:${base}01" >> $OBJ/ssh_config
118 for p in ${SSH_PROTOCOLS}; do
119 trace "config file: start forwarding, fork to background"
121 ${SSH} -S $CTL -M -$p -F $OBJ/ssh_config -f somehost sleep 10
123 trace "config file: transfer over forwarded channels and check result"
124 ${SSH} -F $OBJ/ssh_config -p${base}02 -o 'ConnectionAttempts=4' \
125 somehost cat ${DATA} > ${COPY}
126 test -s ${COPY} || fail "failed copy of ${DATA}"
127 cmp ${DATA} ${COPY} || fail "corrupted copy of ${DATA}"
129 ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost
133 trace "transfer over chained unix domain socket forwards and check result"
134 rm -f $OBJ/unix-[123].fwd
135 rm -f $CTL $CTL.[123]
136 ${SSH} -S $CTL -M -f -F $OBJ/ssh_config -R${base}01:[$OBJ/unix-1.fwd] somehost sleep 10
137 ${SSH} -S $CTL.1 -M -f -F $OBJ/ssh_config -L[$OBJ/unix-1.fwd]:[$OBJ/unix-2.fwd] somehost sleep 10
138 ${SSH} -S $CTL.2 -M -f -F $OBJ/ssh_config -R[$OBJ/unix-2.fwd]:[$OBJ/unix-3.fwd] somehost sleep 10
139 ${SSH} -S $CTL.3 -M -f -F $OBJ/ssh_config -L[$OBJ/unix-3.fwd]:127.0.0.1:$PORT somehost sleep 10
140 ${SSH} -F $OBJ/ssh_config -p${base}01 -o 'ConnectionAttempts=4' \
141 somehost cat ${DATA} > ${COPY}
142 test -s ${COPY} || fail "failed copy ${DATA}"
143 cmp ${DATA} ${COPY} || fail "corrupted copy of ${DATA}"
145 ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost
146 ${SSH} -F $OBJ/ssh_config -S $CTL.1 -O exit somehost
147 ${SSH} -F $OBJ/ssh_config -S $CTL.2 -O exit somehost
148 ${SSH} -F $OBJ/ssh_config -S $CTL.3 -O exit somehost