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