]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/openssh/regress/multiplex.sh
ssh: Update to OpenSSH 9.3p1
[FreeBSD/FreeBSD.git] / crypto / openssh / regress / multiplex.sh
1 #       $OpenBSD: multiplex.sh,v 1.36 2023/03/01 09:29:32 dtucker Exp $
2 #       Placed in the Public Domain.
3
4 make_tmpdir
5 CTL=${SSH_REGRESS_TMP}/ctl-sock
6
7 tid="connection multiplexing"
8
9 trace "will use ProxyCommand $proxycmd"
10 if config_defined DISABLE_FD_PASSING ; then
11         echo "skipped (not supported on this platform)"
12         exit 0
13 fi
14
15 P=3301  # test port
16
17 wait_for_mux_master_ready()
18 {
19         for i in 1 2 3 4 5 6 7 8 9; do
20                 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost \
21                     >/dev/null 2>&1 && return 0
22                 sleep $i
23         done
24         fatal "mux master never becomes ready"
25 }
26
27 maybe_add_scp_path_to_sshd
28 start_sshd
29
30 start_mux_master()
31 {
32         trace "start master, fork to background"
33         ${SSH} -Nn2 -MS$CTL -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" somehost \
34             -E $TEST_REGRESS_LOGFILE 2>&1 &
35         # NB. $SSH_PID will be killed by test-exec.sh:cleanup on fatal errors.
36         SSH_PID=$!
37         wait_for_mux_master_ready
38 }
39
40 start_mux_master
41
42 verbose "test $tid: setenv"
43 trace "setenv over multiplexed connection"
44 _XXX_TEST=blah ${SSH} -F $OBJ/ssh_config -oSendEnv="_XXX_TEST" -S$CTL otherhost sh << 'EOF'
45         test X"$_XXX_TEST" = X"blah"
46 EOF
47 if [ $? -ne 0 ]; then
48         fail "environment not found"
49 fi
50
51 verbose "test $tid: envpass"
52 trace "env passing over multiplexed connection"
53 ${SSH} -F $OBJ/ssh_config -oSetEnv="_XXX_TEST=foo" -S$CTL otherhost sh << 'EOF'
54         test X"$_XXX_TEST" = X"foo"
55 EOF
56 if [ $? -ne 0 ]; then
57         fail "environment not found"
58 fi
59
60
61 verbose "test $tid: transfer"
62 rm -f ${COPY}
63 trace "ssh transfer over multiplexed connection and check result"
64 ${SSH} -F $OBJ/ssh_config -S$CTL otherhost cat ${DATA} > ${COPY}
65 test -f ${COPY}                 || fail "ssh -Sctl: failed copy ${DATA}" 
66 cmp ${DATA} ${COPY}             || fail "ssh -Sctl: corrupted copy of ${DATA}"
67
68 rm -f ${COPY}
69 trace "ssh transfer over multiplexed connection and check result"
70 ${SSH} -F $OBJ/ssh_config -S $CTL otherhost cat ${DATA} > ${COPY}
71 test -f ${COPY}                 || fail "ssh -S ctl: failed copy ${DATA}" 
72 cmp ${DATA} ${COPY}             || fail "ssh -S ctl: corrupted copy of ${DATA}"
73
74 rm -f ${COPY}
75 trace "sftp transfer over multiplexed connection and check result"
76 echo "get ${DATA} ${COPY}" | \
77         ${SFTP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost >>$TEST_REGRESS_LOGFILE 2>&1
78 test -f ${COPY}                 || fail "sftp: failed copy ${DATA}" 
79 cmp ${DATA} ${COPY}             || fail "sftp: corrupted copy of ${DATA}"
80
81 rm -f ${COPY}
82 trace "scp transfer over multiplexed connection and check result"
83 ${SCP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost:${DATA} ${COPY} >>$TEST_REGRESS_LOGFILE 2>&1
84 test -f ${COPY}                 || fail "scp: failed copy ${DATA}" 
85 cmp ${DATA} ${COPY}             || fail "scp: corrupted copy of ${DATA}"
86
87 rm -f ${COPY}
88 verbose "test $tid: forward"
89 trace "forward over TCP/IP and check result"
90 $NC -N -l 127.0.0.1 $((${PORT} + 1)) < ${DATA} >`ssh_logfile nc` &
91 netcat_pid=$!
92 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L127.0.0.1:$((${PORT} + 2)):127.0.0.1:$((${PORT} + 1)) otherhost >>$TEST_SSH_LOGFILE 2>&1
93 sleep 1  # XXX remove once race fixed
94 $NC 127.0.0.1 $((${PORT} + 2)) < /dev/null > ${COPY}
95 cmp ${DATA} ${COPY}             || fail "ssh: corrupted copy of ${DATA}"
96 kill $netcat_pid 2>/dev/null
97 rm -f ${COPY} $OBJ/unix-[123].fwd
98
99 trace "forward over UNIX and check result"
100 $NC -N -Ul $OBJ/unix-1.fwd < ${DATA} > /dev/null &
101 netcat_pid=$!
102 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L$OBJ/unix-2.fwd:$OBJ/unix-1.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1
103 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R$OBJ/unix-3.fwd:$OBJ/unix-2.fwd otherhost >>$TEST_SSH_LOGFILE 2>&1
104 sleep 1  # XXX remove once race fixed
105 $NC -U $OBJ/unix-3.fwd < /dev/null > ${COPY}
106 cmp ${DATA} ${COPY}             || fail "ssh: corrupted copy of ${DATA}"
107 kill $netcat_pid 2>/dev/null
108 rm -f ${COPY} $OBJ/unix-[123].fwd
109
110 for s in 0 1 4 5 44; do
111    for mode in "" "-Oproxy"; do
112         trace "exit status $s over multiplexed connection ($mode)"
113         verbose "test $tid: status $s ($mode)"
114         ${SSH} -F $OBJ/ssh_config -S $CTL $mode otherhost exit $s
115         r=$?
116         if [ $r -ne $s ]; then
117                 fail "exit code mismatch: $r != $s"
118         fi
119
120         # same with early close of stdout/err
121         trace "exit status $s with early close over multiplexed connection ($mode)"
122         ${SSH} -F $OBJ/ssh_config -S $CTL -n $mode otherhost \
123                 exec sh -c \'"sleep 2; exec > /dev/null 2>&1; sleep 3; exit $s"\'
124         r=$?
125         if [ $r -ne $s ]; then
126                 fail "exit code (with sleep) mismatch: $r != $s"
127         fi
128    done
129 done
130
131 verbose "test $tid: cmd check"
132 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
133     || fail "check command failed" 
134
135 verbose "test $tid: cmd forward local (TCP)"
136 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $P:localhost:$PORT otherhost \
137      || fail "request local forward failed"
138 sleep 1  # XXX remove once race fixed
139 ${SSH} -F $OBJ/ssh_config -p$P otherhost true \
140      || fail "connect to local forward port failed"
141 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $P:localhost:$PORT otherhost \
142      || fail "cancel local forward failed"
143 ${SSH} -F $OBJ/ssh_config -p$P otherhost true \
144      && fail "local forward port still listening"
145
146 verbose "test $tid: cmd forward remote (TCP)"
147 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $P:localhost:$PORT otherhost \
148      || fail "request remote forward failed"
149 sleep 1  # XXX remove once race fixed
150 ${SSH} -F $OBJ/ssh_config -p$P otherhost true \
151      || fail "connect to remote forwarded port failed"
152 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $P:localhost:$PORT otherhost \
153      || fail "cancel remote forward failed"
154 ${SSH} -F $OBJ/ssh_config -p$P otherhost true \
155      && fail "remote forward port still listening"
156
157 verbose "test $tid: cmd forward local (UNIX)"
158 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \
159      || fail "request local forward failed"
160 sleep 1  # XXX remove once race fixed
161 echo "" | $NC -U $OBJ/unix-1.fwd | \
162     grep "Invalid SSH identification string" >/dev/null 2>&1 \
163      || fail "connect to local forward path failed"
164 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \
165      || fail "cancel local forward failed"
166 N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
167 test ${N} -eq 0 || fail "local forward path still listening"
168 rm -f $OBJ/unix-1.fwd
169
170 verbose "test $tid: cmd forward remote (UNIX)"
171 ${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \
172      || fail "request remote forward failed"
173 sleep 1  # XXX remove once race fixed
174 echo "" | $NC -U $OBJ/unix-1.fwd | \
175     grep "Invalid SSH identification string" >/dev/null 2>&1 \
176      || fail "connect to remote forwarded path failed"
177 ${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \
178      || fail "cancel remote forward failed"
179 N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
180 test ${N} -eq 0 || fail "remote forward path still listening"
181 rm -f $OBJ/unix-1.fwd
182
183 verbose "test $tid: cmd exit"
184 ${SSH} -F $OBJ/ssh_config -S $CTL -Oexit otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
185     || fail "send exit command failed" 
186
187 # Wait for master to exit
188 wait $SSH_PID
189 kill -0 $SSH_PID >/dev/null 2>&1 && fail "exit command failed"
190
191 # Restart master and test -O stop command with master using -N
192 verbose "test $tid: cmd stop"
193 trace "restart master, fork to background"
194 start_mux_master
195
196 # start a long-running command then immediately request a stop
197 ${SSH} -F $OBJ/ssh_config -S $CTL otherhost "sleep 10; exit 0" \
198      >>$TEST_REGRESS_LOGFILE 2>&1 &
199 SLEEP_PID=$!
200 ${SSH} -F $OBJ/ssh_config -S $CTL -Ostop otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
201     || fail "send stop command failed"
202
203 # wait until both long-running command and master have exited.
204 wait $SLEEP_PID
205 [ $! != 0 ] || fail "waiting for concurrent command"
206 wait $SSH_PID
207 [ $! != 0 ] || fail "waiting for master stop"
208 kill -0 $SSH_PID >/dev/null 2>&1 && fatal "stop command failed"
209 SSH_PID="" # Already gone, so don't kill in cleanup
210