1# $OpenBSD: connection-timeout.sh,v 1.2 2023/01/17 10:15:10 djm Exp $ 2# Placed in the Public Domain. 3 4tid="unused connection timeout" 5 6CTL=$OBJ/ctl-sock 7cp $OBJ/sshd_proxy $OBJ/sshd_proxy.orig 8 9check_ssh() { 10 test -S $CTL || return 1 11 if ! ${REAL_SSH} -qF$OBJ/ssh_proxy -O check \ 12 -oControlPath=$CTL somehost >/dev/null 2>&1 ; then 13 return 1 14 fi 15 return 0 16} 17 18start_ssh() { 19 trace "start ssh" 20 ${SSH} -nNfF $OBJ/ssh_proxy "$@" -oExitOnForwardFailure=yes \ 21 -oControlMaster=yes -oControlPath=$CTL somehost 22 r=$? 23 test $r -eq 0 || fatal "failed to start ssh $r" 24 check_ssh || fatal "ssh process unresponsive" 25} 26 27stop_ssh() { 28 test -S $CTL || return 29 check_ssh || fatal "ssh process is unresponsive: cannot close" 30 if ! ${REAL_SSH} -qF$OBJ/ssh_proxy -O exit \ 31 -oControlPath=$CTL >/dev/null somehost >/dev/null ; then 32 fatal "ssh process did not respond to close" 33 fi 34 n=0 35 while [ "$n" -lt 20 ] ; do 36 test -S $CTL || break 37 sleep 1 38 n=`expr $n + 1` 39 done 40 if test -S $CTL ; then 41 fatal "ssh process did not exit" 42 fi 43} 44 45trap "stop_ssh" EXIT 46 47verbose "no timeout" 48start_ssh 49sleep 5 50check_ssh || fatal "ssh unexpectedly missing" 51stop_ssh 52 53(cat $OBJ/sshd_proxy.orig ; echo "UnusedConnectionTimeout 2") > $OBJ/sshd_proxy 54 55verbose "timeout" 56start_ssh 57sleep 8 58check_ssh && fail "ssh unexpectedly present" 59stop_ssh 60 61verbose "session inhibits timeout" 62rm -f $OBJ/copy.1 63start_ssh 64${REAL_SSH} -qoControlPath=$CTL -oControlMaster=no -Fnone somehost \ 65 "sleep 8; touch $OBJ/copy.1" & 66check_ssh || fail "ssh unexpectedly missing" 67wait 68test -f $OBJ/copy.1 || fail "missing result file" 69 70verbose "timeout after session" 71# Session should still be running from previous 72sleep 8 73check_ssh && fail "ssh unexpectedly present" 74stop_ssh 75 76LPORT=`expr $PORT + 1` 77RPORT=`expr $LPORT + 1` 78DPORT=`expr $RPORT + 1` 79RDPORT=`expr $DPORT + 1` 80verbose "timeout with listeners" 81start_ssh -L$LPORT:127.0.0.1:$PORT -R$RPORT:127.0.0.1:$PORT -D$DPORT -R$RDPORT 82sleep 8 83check_ssh && fail "ssh unexpectedly present" 84stop_ssh 85