xref: /freebsd/crypto/openssh/regress/multiplex.sh (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1#	$OpenBSD: multiplex.sh,v 1.36 2023/03/01 09:29:32 dtucker Exp $
2#	Placed in the Public Domain.
3
4make_tmpdir
5CTL=${SSH_REGRESS_TMP}/ctl-sock
6
7tid="connection multiplexing"
8
9trace "will use ProxyCommand $proxycmd"
10if config_defined DISABLE_FD_PASSING ; then
11	echo "skipped (not supported on this platform)"
12	exit 0
13fi
14
15P=3301  # test port
16
17wait_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
27maybe_add_scp_path_to_sshd
28start_sshd
29
30start_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
40start_mux_master
41
42verbose "test $tid: setenv"
43trace "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"
46EOF
47if [ $? -ne 0 ]; then
48	fail "environment not found"
49fi
50
51verbose "test $tid: envpass"
52trace "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"
55EOF
56if [ $? -ne 0 ]; then
57	fail "environment not found"
58fi
59
60
61verbose "test $tid: transfer"
62rm -f ${COPY}
63trace "ssh transfer over multiplexed connection and check result"
64${SSH} -F $OBJ/ssh_config -S$CTL otherhost cat ${DATA} > ${COPY}
65test -f ${COPY}			|| fail "ssh -Sctl: failed copy ${DATA}"
66cmp ${DATA} ${COPY}		|| fail "ssh -Sctl: corrupted copy of ${DATA}"
67
68rm -f ${COPY}
69trace "ssh transfer over multiplexed connection and check result"
70${SSH} -F $OBJ/ssh_config -S $CTL otherhost cat ${DATA} > ${COPY}
71test -f ${COPY}			|| fail "ssh -S ctl: failed copy ${DATA}"
72cmp ${DATA} ${COPY}		|| fail "ssh -S ctl: corrupted copy of ${DATA}"
73
74rm -f ${COPY}
75trace "sftp transfer over multiplexed connection and check result"
76echo "get ${DATA} ${COPY}" | \
77	${SFTP} -S ${SSH} -F $OBJ/ssh_config -oControlPath=$CTL otherhost >>$TEST_REGRESS_LOGFILE 2>&1
78test -f ${COPY}			|| fail "sftp: failed copy ${DATA}"
79cmp ${DATA} ${COPY}		|| fail "sftp: corrupted copy of ${DATA}"
80
81rm -f ${COPY}
82trace "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
84test -f ${COPY}			|| fail "scp: failed copy ${DATA}"
85cmp ${DATA} ${COPY}		|| fail "scp: corrupted copy of ${DATA}"
86
87rm -f ${COPY}
88verbose "test $tid: forward"
89trace "forward over TCP/IP and check result"
90$NC -N -l 127.0.0.1 $((${PORT} + 1)) < ${DATA} >`ssh_logfile nc` &
91netcat_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
93sleep 1  # XXX remove once race fixed
94$NC 127.0.0.1 $((${PORT} + 2)) < /dev/null > ${COPY}
95cmp ${DATA} ${COPY}		|| fail "ssh: corrupted copy of ${DATA}"
96kill $netcat_pid 2>/dev/null
97rm -f ${COPY} $OBJ/unix-[123].fwd
98
99trace "forward over UNIX and check result"
100$NC -N -Ul $OBJ/unix-1.fwd < ${DATA} > /dev/null &
101netcat_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
104sleep 1  # XXX remove once race fixed
105$NC -U $OBJ/unix-3.fwd < /dev/null > ${COPY}
106cmp ${DATA} ${COPY}		|| fail "ssh: corrupted copy of ${DATA}"
107kill $netcat_pid 2>/dev/null
108rm -f ${COPY} $OBJ/unix-[123].fwd
109
110for 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
129done
130
131verbose "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
135verbose "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"
138sleep 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
146verbose "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"
149sleep 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
157verbose "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"
160sleep 1  # XXX remove once race fixed
161echo "" | $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"
166N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
167test ${N} -eq 0 || fail "local forward path still listening"
168rm -f $OBJ/unix-1.fwd
169
170verbose "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"
173sleep 1  # XXX remove once race fixed
174echo "" | $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"
179N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
180test ${N} -eq 0 || fail "remote forward path still listening"
181rm -f $OBJ/unix-1.fwd
182
183verbose "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
188wait $SSH_PID
189kill -0 $SSH_PID >/dev/null 2>&1 && fail "exit command failed"
190
191# Restart master and test -O stop command with master using -N
192verbose "test $tid: cmd stop"
193trace "restart master, fork to background"
194start_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 &
199SLEEP_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.
204wait $SLEEP_PID
205[ $! != 0 ] || fail "waiting for concurrent command"
206wait $SSH_PID
207[ $! != 0 ] || fail "waiting for master stop"
208kill -0 $SSH_PID >/dev/null 2>&1 && fatal "stop command failed"
209SSH_PID="" # Already gone, so don't kill in cleanup
210
211