1#	$OpenBSD: multiplex.sh,v 1.32 2020/01/25 02:57:53 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
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	trace "exit status $s over multiplexed connection"
101	verbose "test $tid: status $s"
102	${SSH} -F $OBJ/ssh_config -S $CTL otherhost exit $s
103	r=$?
104	if [ $r -ne $s ]; then
105		fail "exit code mismatch: $r != $s"
106	fi
107
108	# same with early close of stdout/err
109	trace "exit status $s with early close over multiplexed connection"
110	${SSH} -F $OBJ/ssh_config -S $CTL -n otherhost \
111                exec sh -c \'"sleep 2; exec > /dev/null 2>&1; sleep 3; exit $s"\'
112	r=$?
113	if [ $r -ne $s ]; then
114		fail "exit code (with sleep) mismatch: $r != $s"
115	fi
116done
117
118verbose "test $tid: cmd check"
119${SSH} -F $OBJ/ssh_config -S $CTL -Ocheck otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
120    || fail "check command failed"
121
122verbose "test $tid: cmd forward local (TCP)"
123${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $P:localhost:$PORT otherhost \
124     || fail "request local forward failed"
125sleep 1  # XXX remove once race fixed
126${SSH} -F $OBJ/ssh_config -p$P otherhost true \
127     || fail "connect to local forward port failed"
128${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $P:localhost:$PORT otherhost \
129     || fail "cancel local forward failed"
130${SSH} -F $OBJ/ssh_config -p$P otherhost true \
131     && fail "local forward port still listening"
132
133verbose "test $tid: cmd forward remote (TCP)"
134${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $P:localhost:$PORT otherhost \
135     || fail "request remote forward failed"
136sleep 1  # XXX remove once race fixed
137${SSH} -F $OBJ/ssh_config -p$P otherhost true \
138     || fail "connect to remote forwarded port failed"
139${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $P:localhost:$PORT otherhost \
140     || fail "cancel remote forward failed"
141${SSH} -F $OBJ/ssh_config -p$P otherhost true \
142     && fail "remote forward port still listening"
143
144verbose "test $tid: cmd forward local (UNIX)"
145${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \
146     || fail "request local forward failed"
147sleep 1  # XXX remove once race fixed
148echo "" | $NC -U $OBJ/unix-1.fwd | \
149    grep "Invalid SSH identification string" >/dev/null 2>&1 \
150     || fail "connect to local forward path failed"
151${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -L $OBJ/unix-1.fwd:localhost:$PORT otherhost \
152     || fail "cancel local forward failed"
153N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
154test ${N} -eq 0 || fail "local forward path still listening"
155rm -f $OBJ/unix-1.fwd
156
157verbose "test $tid: cmd forward remote (UNIX)"
158${SSH} -F $OBJ/ssh_config -S $CTL -Oforward -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \
159     || fail "request remote 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 remote forwarded path failed"
164${SSH} -F $OBJ/ssh_config -S $CTL -Ocancel -R $OBJ/unix-1.fwd:localhost:$PORT otherhost \
165     || fail "cancel remote forward failed"
166N=$(echo "xyzzy" | $NC -U $OBJ/unix-1.fwd 2>&1 | grep "xyzzy" | wc -l)
167test ${N} -eq 0 || fail "remote forward path still listening"
168rm -f $OBJ/unix-1.fwd
169
170verbose "test $tid: cmd exit"
171${SSH} -F $OBJ/ssh_config -S $CTL -Oexit otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
172    || fail "send exit command failed"
173
174# Wait for master to exit
175wait $SSH_PID
176kill -0 $SSH_PID >/dev/null 2>&1 && fail "exit command failed"
177
178# Restart master and test -O stop command with master using -N
179verbose "test $tid: cmd stop"
180trace "restart master, fork to background"
181start_mux_master
182
183# start a long-running command then immediately request a stop
184${SSH} -F $OBJ/ssh_config -S $CTL otherhost "sleep 10; exit 0" \
185     >>$TEST_REGRESS_LOGFILE 2>&1 &
186SLEEP_PID=$!
187${SSH} -F $OBJ/ssh_config -S $CTL -Ostop otherhost >>$TEST_REGRESS_LOGFILE 2>&1 \
188    || fail "send stop command failed"
189
190# wait until both long-running command and master have exited.
191wait $SLEEP_PID
192[ $! != 0 ] || fail "waiting for concurrent command"
193wait $SSH_PID
194[ $! != 0 ] || fail "waiting for master stop"
195kill -0 $SSH_PID >/dev/null 2>&1 && fatal "stop command failed"
196SSH_PID="" # Already gone, so don't kill in cleanup
197
198