1 # $OpenBSD: forwarding.sh,v 1.20 2017/04/30 23:34:55 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"
26 trace "start forwarding, fork to background"
28 ${SSH} -S $CTL -M -F $OBJ/ssh_config -f $fwd somehost sleep 10
30 trace "transfer over forwarded channels and check result"
31 ${SSH} -F $OBJ/ssh_config -p$last -o 'ConnectionAttempts=4' \
32 somehost cat ${DATA} > ${COPY}
33 test -s ${COPY} || fail "failed copy of ${DATA}"
34 cmp ${DATA} ${COPY} || fail "corrupted copy of ${DATA}"
36 ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost
39 trace "exit on -$d forward failure"
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
49 fatal "connection failed, should not"
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
60 if [ $r != 255 ]; then
61 fail "connection not termintated, but should ($r)"
66 trace "simple clear forwarding"
67 ${SSH} -F $OBJ/ssh_config -oClearAllForwardings=yes somehost true
69 trace "clear local forward"
71 ${SSH} -S $CTL -M -f -F $OBJ/ssh_config -L ${base}01:127.0.0.1:$PORT \
72 -oClearAllForwardings=yes somehost sleep 10
74 fail "connection failed with cleared local forwarding"
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"
81 ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost
83 trace "clear remote forward"
85 ${SSH} -S $CTL -M -f -F $OBJ/ssh_config -R ${base}01:127.0.0.1:$PORT \
86 -oClearAllForwardings=yes somehost sleep 10
88 fail "connection failed with cleared remote forwarding"
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"
95 ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost
97 trace "stdio forwarding"
98 cmd="${SSH} -F $OBJ/ssh_config"
99 $cmd -o "ProxyCommand $cmd -q -W localhost:$PORT somehost" somehost true
101 fail "stdio forwarding"
104 echo "LocalForward ${base}01 127.0.0.1:$PORT" >> $OBJ/ssh_config
105 echo "RemoteForward ${base}02 127.0.0.1:${base}01" >> $OBJ/ssh_config
107 trace "config file: start forwarding, fork to background"
109 ${SSH} -S $CTL -M -F $OBJ/ssh_config -f somehost sleep 10
111 trace "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}
114 test -s ${COPY} || fail "failed copy of ${DATA}"
115 cmp ${DATA} ${COPY} || fail "corrupted copy of ${DATA}"
117 ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost
119 trace "transfer over chained unix domain socket forwards and check result"
120 rm -f $OBJ/unix-[123].fwd
121 rm -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}
128 test -s ${COPY} || fail "failed copy ${DATA}"
129 cmp ${DATA} ${COPY} || fail "corrupted copy of ${DATA}"
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