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