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