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