]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - regress/forwarding.sh
Vendor import of OpenSSH 7.8p1.
[FreeBSD/FreeBSD.git] / regress / forwarding.sh
1 #       $OpenBSD: forwarding.sh,v 1.20 2017/04/30 23:34:55 djm Exp $
2 #       Placed in the Public Domain.
3
4 tid="local and remote forwarding"
5
6 DATA=/bin/ls${EXEEXT}
7
8 start_sshd
9
10 base=33
11 last=$PORT
12 fwd=""
13 make_tmpdir
14 CTL=${SSH_REGRESS_TMP}/ctl-sock
15
16 for 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
25 done
26
27 trace "start forwarding, fork to background"
28 rm -f $CTL
29 ${SSH} -S $CTL -M -F $OBJ/ssh_config -f $fwd somehost sleep 10
30
31 trace "transfer over forwarded channels and check result"
32 ${SSH} -F $OBJ/ssh_config -p$last -o 'ConnectionAttempts=4' \
33         somehost cat ${DATA} > ${COPY}
34 test -s ${COPY}         || fail "failed copy of ${DATA}"
35 cmp ${DATA} ${COPY}     || fail "corrupted copy of ${DATA}"
36
37 ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost
38
39 for d in L R; do
40         trace "exit on -$d forward failure"
41
42         # this one should succeed
43         ${SSH}  -F $OBJ/ssh_config \
44             -$d ${base}01:127.0.0.1:$PORT \
45             -$d ${base}02:127.0.0.1:$PORT \
46             -$d ${base}03:127.0.0.1:$PORT \
47             -$d ${base}04:127.0.0.1:$PORT \
48             -oExitOnForwardFailure=yes somehost true
49         if [ $? != 0 ]; then
50                 fatal "connection failed, should not"
51         else
52                 # this one should fail
53                 ${SSH} -q -F $OBJ/ssh_config \
54                     -$d ${base}01:127.0.0.1:$PORT \
55                     -$d ${base}02:127.0.0.1:$PORT \
56                     -$d ${base}03:127.0.0.1:$PORT \
57                     -$d ${base}01:localhost:$PORT \
58                     -$d ${base}04:127.0.0.1:$PORT \
59                     -oExitOnForwardFailure=yes somehost true
60                 r=$?
61                 if [ $r != 255 ]; then
62                         fail "connection not termintated, but should ($r)"
63                 fi
64         fi
65 done
66
67 trace "simple clear forwarding"
68 ${SSH} -F $OBJ/ssh_config -oClearAllForwardings=yes somehost true
69
70 trace "clear local forward"
71 rm -f $CTL
72 ${SSH} -S $CTL -M -f -F $OBJ/ssh_config -L ${base}01:127.0.0.1:$PORT \
73     -oClearAllForwardings=yes somehost sleep 10
74 if [ $? != 0 ]; then
75         fail "connection failed with cleared local forwarding"
76 else
77         # this one should fail
78         ${SSH} -F $OBJ/ssh_config -p ${base}01 somehost true \
79              >>$TEST_REGRESS_LOGFILE 2>&1 && \
80                 fail "local forwarding not cleared"
81 fi
82 ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost
83
84 trace "clear remote forward"
85 rm -f $CTL
86 ${SSH} -S $CTL -M -f -F $OBJ/ssh_config -R ${base}01:127.0.0.1:$PORT \
87     -oClearAllForwardings=yes somehost sleep 10
88 if [ $? != 0 ]; then
89         fail "connection failed with cleared remote forwarding"
90 else
91         # this one should fail
92         ${SSH} -F $OBJ/ssh_config -p ${base}01 somehost true \
93              >>$TEST_REGRESS_LOGFILE 2>&1 && \
94                 fail "remote forwarding not cleared"
95 fi
96 ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost
97
98 trace "stdio forwarding"
99 cmd="${SSH} -F $OBJ/ssh_config"
100 $cmd -o "ProxyCommand $cmd -q -W localhost:$PORT somehost" somehost true
101 if [ $? != 0 ]; then
102         fail "stdio forwarding"
103 fi
104
105 echo "LocalForward ${base}01 127.0.0.1:$PORT" >> $OBJ/ssh_config
106 echo "RemoteForward ${base}02 127.0.0.1:${base}01" >> $OBJ/ssh_config
107
108 trace "config file: start forwarding, fork to background"
109 rm -f $CTL
110 ${SSH} -S $CTL -M -F $OBJ/ssh_config -f somehost sleep 10
111
112 trace "config file: transfer over forwarded channels and check result"
113 ${SSH} -F $OBJ/ssh_config -p${base}02 -o 'ConnectionAttempts=4' \
114         somehost cat ${DATA} > ${COPY}
115 test -s ${COPY}         || fail "failed copy of ${DATA}"
116 cmp ${DATA} ${COPY}     || fail "corrupted copy of ${DATA}"
117
118 ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost
119
120 trace "transfer over chained unix domain socket forwards and check result"
121 rm -f $OBJ/unix-[123].fwd
122 rm -f $CTL $CTL.[123]
123 ${SSH} -S $CTL -M -f -F $OBJ/ssh_config -R${base}01:[$OBJ/unix-1.fwd] somehost sleep 10
124 ${SSH} -S $CTL.1 -M -f -F $OBJ/ssh_config -L[$OBJ/unix-1.fwd]:[$OBJ/unix-2.fwd] somehost sleep 10
125 ${SSH} -S $CTL.2 -M -f -F $OBJ/ssh_config -R[$OBJ/unix-2.fwd]:[$OBJ/unix-3.fwd] somehost sleep 10
126 ${SSH} -S $CTL.3 -M -f -F $OBJ/ssh_config -L[$OBJ/unix-3.fwd]:127.0.0.1:$PORT somehost sleep 10
127 ${SSH} -F $OBJ/ssh_config -p${base}01 -o 'ConnectionAttempts=4' \
128         somehost cat ${DATA} > ${COPY}
129 test -s ${COPY}                 || fail "failed copy ${DATA}"
130 cmp ${DATA} ${COPY}             || fail "corrupted copy of ${DATA}"
131
132 ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost
133 ${SSH} -F $OBJ/ssh_config -S $CTL.1 -O exit somehost
134 ${SSH} -F $OBJ/ssh_config -S $CTL.2 -O exit somehost
135 ${SSH} -F $OBJ/ssh_config -S $CTL.3 -O exit somehost
136