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