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