xref: /freebsd/crypto/openssh/regress/channel-timeout.sh (revision a91a246563dffa876a52f53a98de4af9fa364c52)
1*a91a2465SEd Maste#	$OpenBSD: channel-timeout.sh,v 1.2 2024/01/09 22:19:36 djm Exp $
2f374ba41SEd Maste#	Placed in the Public Domain.
3f374ba41SEd Maste
4f374ba41SEd Mastetid="channel timeout"
5f374ba41SEd Maste
6f374ba41SEd Maste# XXX not comprehensive. Still need -R -L agent X11 forwarding + interactive
7f374ba41SEd Maste
8*a91a2465SEd Masterm -f $OBJ/finished.* $OBJ/mux.*
9*a91a2465SEd Maste
10*a91a2465SEd MasteMUXPATH=$OBJ/mux.$$
11*a91a2465SEd Masteopen_mux() {
12*a91a2465SEd Maste	${SSH} -nNfM -oControlPath=$MUXPATH -F $OBJ/ssh_proxy "$@" somehost ||
13*a91a2465SEd Maste	    fatal "open mux failed"
14*a91a2465SEd Maste	test -e $MUXPATH || fatal "mux socket $MUXPATH not established"
15*a91a2465SEd Maste}
16*a91a2465SEd Maste
17*a91a2465SEd Masteclose_mux() {
18*a91a2465SEd Maste	test -e $MUXPATH || fatal "mux socket $MUXPATH missing"
19*a91a2465SEd Maste	${SSH} -qF $OBJ/ssh_proxy -oControlPath=$MUXPATH -O exit somehost ||
20*a91a2465SEd Maste	    fatal "could not terminate mux process"
21*a91a2465SEd Maste	for x in 1 2 3 4 5 6 7 8 9 10 ; do
22*a91a2465SEd Maste		test -e $OBJ/mux && break
23*a91a2465SEd Maste		sleep 1
24*a91a2465SEd Maste	done
25*a91a2465SEd Maste	test -e $MUXPATH && fatal "mux did not clean up"
26*a91a2465SEd Maste}
27*a91a2465SEd Mastemux_client() {
28*a91a2465SEd Maste	${SSH} -F $OBJ/ssh_proxy -oControlPath=$MUXPATH somehost "$@"
29*a91a2465SEd Maste}
30*a91a2465SEd Maste
31f374ba41SEd Masterm -f $OBJ/sshd_proxy.orig
32f374ba41SEd Mastecp $OBJ/sshd_proxy $OBJ/sshd_proxy.orig
33f374ba41SEd Maste
34f374ba41SEd Masteverbose "no timeout"
35f374ba41SEd Maste${SSH} -F $OBJ/ssh_proxy somehost "sleep 5 ; exit 23"
36f374ba41SEd Master=$?
37f374ba41SEd Masteif [ $r -ne 23 ]; then
38f374ba41SEd Maste	fail "ssh failed"
39f374ba41SEd Mastefi
40f374ba41SEd Maste
41f374ba41SEd Masteverbose "command timeout"
42f374ba41SEd Maste(cat $OBJ/sshd_proxy.orig ; echo "ChannelTimeout session:command=1") \
43f374ba41SEd Maste	> $OBJ/sshd_proxy
44f374ba41SEd Maste${SSH} -F $OBJ/ssh_proxy somehost "sleep 5 ; exit 23"
45f374ba41SEd Master=$?
46f374ba41SEd Masteif [ $r -ne 255 ]; then
47f374ba41SEd Maste	fail "ssh returned unexpected error code $r"
48f374ba41SEd Mastefi
49f374ba41SEd Maste
50*a91a2465SEd Masteverbose "command long timeout"
51*a91a2465SEd Maste(cat $OBJ/sshd_proxy.orig ; echo "ChannelTimeout session:command=60") \
52*a91a2465SEd Maste	> $OBJ/sshd_proxy
53*a91a2465SEd Maste${SSH} -F $OBJ/ssh_proxy somehost "exit 23"
54*a91a2465SEd Master=$?
55*a91a2465SEd Masteif [ $r -ne 23 ]; then
56*a91a2465SEd Maste	fail "ssh returned unexpected error code $r"
57*a91a2465SEd Mastefi
58*a91a2465SEd Maste
59f374ba41SEd Masteverbose "command wildcard timeout"
60f374ba41SEd Maste(cat $OBJ/sshd_proxy.orig ; echo "ChannelTimeout session:*=1") \
61f374ba41SEd Maste	> $OBJ/sshd_proxy
62f374ba41SEd Maste${SSH} -F $OBJ/ssh_proxy somehost "sleep 5 ; exit 23"
63f374ba41SEd Master=$?
64f374ba41SEd Masteif [ $r -ne 255 ]; then
65f374ba41SEd Maste	fail "ssh returned unexpected error code $r"
66f374ba41SEd Mastefi
67f374ba41SEd Maste
68f374ba41SEd Masteverbose "command irrelevant timeout"
69f374ba41SEd Maste(cat $OBJ/sshd_proxy.orig ; echo "ChannelTimeout session:shell=1") \
70f374ba41SEd Maste	> $OBJ/sshd_proxy
71f374ba41SEd Maste${SSH} -F $OBJ/ssh_proxy somehost "sleep 5 ; exit 23"
72f374ba41SEd Master=$?
73f374ba41SEd Masteif [ $r -ne 23 ]; then
74f374ba41SEd Maste	fail "ssh failed"
75f374ba41SEd Mastefi
76f374ba41SEd Maste
77*a91a2465SEd Masteif config_defined DISABLE_FD_PASSING ; then
78*a91a2465SEd Maste	verbose "skipping multiplexing tests"
79*a91a2465SEd Masteelse
80*a91a2465SEd Maste	verbose "multiplexed command timeout"
81*a91a2465SEd Maste	(cat $OBJ/sshd_proxy.orig ; echo "ChannelTimeout session:command=1") \
82*a91a2465SEd Maste		> $OBJ/sshd_proxy
83*a91a2465SEd Maste	open_mux
84*a91a2465SEd Maste	mux_client "sleep 5 ; exit 23"
85*a91a2465SEd Maste	r=$?
86*a91a2465SEd Maste	if [ $r -ne 255 ]; then
87*a91a2465SEd Maste		fail "ssh returned unexpected error code $r"
88*a91a2465SEd Maste	fi
89*a91a2465SEd Maste	close_mux
90*a91a2465SEd Maste
91*a91a2465SEd Maste	verbose "irrelevant multiplexed command timeout"
92*a91a2465SEd Maste	(cat $OBJ/sshd_proxy.orig ; echo "ChannelTimeout session:shell=1") \
93*a91a2465SEd Maste		> $OBJ/sshd_proxy
94*a91a2465SEd Maste	open_mux
95*a91a2465SEd Maste	mux_client "sleep 5 ; exit 23"
96*a91a2465SEd Maste	r=$?
97*a91a2465SEd Maste	if [ $r -ne 23 ]; then
98*a91a2465SEd Maste		fail "ssh returned unexpected error code $r"
99*a91a2465SEd Maste	fi
100*a91a2465SEd Maste	close_mux
101*a91a2465SEd Maste
102*a91a2465SEd Maste	verbose "global command timeout"
103*a91a2465SEd Maste	(cat $OBJ/sshd_proxy.orig ; echo "ChannelTimeout global=10") \
104*a91a2465SEd Maste		> $OBJ/sshd_proxy
105*a91a2465SEd Maste	open_mux
106*a91a2465SEd Maste	mux_client "sleep 1 ; echo ok ; sleep 1; echo ok; sleep 60; touch $OBJ/finished.1" >/dev/null &
107*a91a2465SEd Maste	mux_client "sleep 60 ; touch $OBJ/finished.2" >/dev/null &
108*a91a2465SEd Maste	mux_client "sleep 2 ; touch $OBJ/finished.3" >/dev/null &
109*a91a2465SEd Maste	wait
110*a91a2465SEd Maste	test -f $OBJ/finished.1 && fail "first mux process completed"
111*a91a2465SEd Maste	test -f $OBJ/finished.2 && fail "second mux process completed"
112*a91a2465SEd Maste	test -f $OBJ/finished.3 || fail "third mux process did not complete"
113*a91a2465SEd Maste	close_mux
114*a91a2465SEd Mastefi
115*a91a2465SEd Maste
116f374ba41SEd Maste# Set up a "slow sftp server" that sleeps before executing the real one.
117f374ba41SEd Mastecat > $OBJ/slow-sftp-server.sh << _EOF
118f374ba41SEd Maste#!/bin/sh
119f374ba41SEd Maste
120f374ba41SEd Mastesleep 5
121f374ba41SEd Maste$SFTPSERVER
122f374ba41SEd Maste_EOF
123f374ba41SEd Mastechmod a+x $OBJ/slow-sftp-server.sh
124f374ba41SEd Maste
125f374ba41SEd Masteverbose "sftp no timeout"
126f374ba41SEd Maste(grep -vi subsystem.*sftp $OBJ/sshd_proxy.orig;
127f374ba41SEd Maste echo "Subsystem sftp $OBJ/slow-sftp-server.sh" ) > $OBJ/sshd_proxy
128f374ba41SEd Maste
129f374ba41SEd Masterm -f ${COPY}
130f374ba41SEd Maste$SFTP -qS $SSH -F $OBJ/ssh_proxy somehost:$DATA $COPY
131f374ba41SEd Master=$?
132f374ba41SEd Masteif [ $r -ne 0 ]; then
133f374ba41SEd Maste	fail "sftp failed"
134f374ba41SEd Mastefi
135f374ba41SEd Mastecmp $DATA $COPY || fail "corrupted copy"
136f374ba41SEd Maste
137f374ba41SEd Masteverbose "sftp timeout"
138f374ba41SEd Maste(grep -vi subsystem.*sftp $OBJ/sshd_proxy.orig;
139f374ba41SEd Maste echo "ChannelTimeout session:subsystem:sftp=1" ;
140f374ba41SEd Maste echo "Subsystem sftp $OBJ/slow-sftp-server.sh" ) > $OBJ/sshd_proxy
141f374ba41SEd Maste
142f374ba41SEd Masterm -f ${COPY}
143f374ba41SEd Maste$SFTP -qS $SSH -F $OBJ/ssh_proxy somehost:$DATA $COPY
144f374ba41SEd Master=$?
145f374ba41SEd Masteif [ $r -eq 0 ]; then
146f374ba41SEd Maste	fail "sftp succeeded unexpectedly"
147f374ba41SEd Mastefi
148f374ba41SEd Mastetest -f $COPY && cmp $DATA $COPY && fail "intact copy"
149f374ba41SEd Maste
150f374ba41SEd Masteverbose "sftp irrelevant timeout"
151f374ba41SEd Maste(grep -vi subsystem.*sftp $OBJ/sshd_proxy.orig;
152f374ba41SEd Maste echo "ChannelTimeout session:subsystem:command=1" ;
153f374ba41SEd Maste echo "Subsystem sftp $OBJ/slow-sftp-server.sh" ) > $OBJ/sshd_proxy
154f374ba41SEd Maste
155f374ba41SEd Masterm -f ${COPY}
156f374ba41SEd Maste$SFTP -qS $SSH -F $OBJ/ssh_proxy somehost:$DATA $COPY
157f374ba41SEd Master=$?
158f374ba41SEd Masteif [ $r -ne 0 ]; then
159f374ba41SEd Maste	fail "sftp failed"
160f374ba41SEd Mastefi
161f374ba41SEd Mastecmp $DATA $COPY || fail "corrupted copy"
162