xref: /freebsd/crypto/openssh/regress/dynamic-forward.sh (revision a91a246563dffa876a52f53a98de4af9fa364c52)
1*a91a2465SEd Maste#	$OpenBSD: dynamic-forward.sh,v 1.17 2024/03/08 11:34:10 dtucker Exp $
2ce3adf43SDag-Erling Smørgrav#	Placed in the Public Domain.
3ce3adf43SDag-Erling Smørgrav
4ce3adf43SDag-Erling Smørgravtid="dynamic forwarding"
5ce3adf43SDag-Erling Smørgrav
6f374ba41SEd Maste# This is a reasonable proxy for IPv6 support.
7f374ba41SEd Masteif ! config_defined HAVE_STRUCT_IN6_ADDR ; then
8f374ba41SEd Maste	SKIP_IPV6=yes
9ce3adf43SDag-Erling Smørgravfi
10f374ba41SEd Maste
11f374ba41SEd MasteFWDPORT=`expr $PORT + 1`
12f374ba41SEd Mastemake_tmpdir
13f374ba41SEd MasteCTL=${SSH_REGRESS_TMP}/ctl-sock
14f374ba41SEd Mastecp $OBJ/ssh_config $OBJ/ssh_config.orig
15f374ba41SEd Masteproxycmd="$OBJ/netcat -x 127.0.0.1:$FWDPORT -X"
16ce3adf43SDag-Erling Smørgravtrace "will use ProxyCommand $proxycmd"
17ce3adf43SDag-Erling Smørgrav
18f374ba41SEd Mastestart_ssh() {
19f374ba41SEd Maste	direction="$1"
20f374ba41SEd Maste	arg="$2"
21ce3adf43SDag-Erling Smørgrav	n=0
22ce3adf43SDag-Erling Smørgrav	error="1"
23*a91a2465SEd Maste	# Use a multiplexed ssh so we can control its lifecycle.
24f374ba41SEd Maste	trace "start dynamic -$direction forwarding, fork to background"
25f374ba41SEd Maste	(cat $OBJ/ssh_config.orig ; echo "$arg") > $OBJ/ssh_config
26f374ba41SEd Maste	${REAL_SSH} -vvvnNfF $OBJ/ssh_config -E$TEST_SSH_LOGFILE \
27f374ba41SEd Maste	    -$direction $FWDPORT -oExitOnForwardFailure=yes \
28f374ba41SEd Maste	    -oControlMaster=yes -oControlPath=$CTL somehost
29f374ba41SEd Maste	r=$?
30f374ba41SEd Maste	test $r -eq 0 || fatal "failed to start dynamic forwarding $r"
31f374ba41SEd Maste	if ! ${REAL_SSH} -qF$OBJ/ssh_config -O check \
32f374ba41SEd Maste	     -oControlPath=$CTL somehost >/dev/null 2>&1 ; then
33f374ba41SEd Maste		fatal "forwarding ssh process unresponsive"
34f374ba41SEd Maste	fi
35f374ba41SEd Maste}
364f52dfbbSDag-Erling Smørgrav
37f374ba41SEd Mastestop_ssh() {
38f374ba41SEd Maste	test -S $CTL || return
39f374ba41SEd Maste	if ! ${REAL_SSH} -qF$OBJ/ssh_config -O exit \
40f374ba41SEd Maste	     -oControlPath=$CTL >/dev/null somehost >/dev/null ; then
41f374ba41SEd Maste		fatal "forwarding ssh process did not respond to close"
42f374ba41SEd Maste	fi
43f374ba41SEd Maste	n=0
44f374ba41SEd Maste	while [ "$n" -lt 20 ] ; do
45f374ba41SEd Maste		test -S $CTL || break
46f374ba41SEd Maste		sleep 1
47ce3adf43SDag-Erling Smørgrav		n=`expr $n + 1`
48ce3adf43SDag-Erling Smørgrav	done
49f374ba41SEd Maste	if test -S $CTL ; then
50f374ba41SEd Maste		fatal "forwarding ssh process did not exit"
51ce3adf43SDag-Erling Smørgrav	fi
52f374ba41SEd Maste}
53ce3adf43SDag-Erling Smørgrav
54f374ba41SEd Mastecheck_socks() {
55f374ba41SEd Maste	direction=$1
56f374ba41SEd Maste	expect_success=$2
57ce3adf43SDag-Erling Smørgrav	for s in 4 5; do
58ce3adf43SDag-Erling Smørgrav	    for h in 127.0.0.1 localhost; do
59f374ba41SEd Maste		trace "testing ssh socks version $s host $h (-$direction)"
60*a91a2465SEd Maste		${REAL_SSH} -q -F $OBJ/ssh_config -o \
61*a91a2465SEd Maste		   "ProxyCommand ${TEST_SHELL} -c '${proxycmd}${s} $h $PORT 2>/dev/null'" \
624f52dfbbSDag-Erling Smørgrav		   somehost cat ${DATA} > ${COPY}
63f374ba41SEd Maste		r=$?
64f374ba41SEd Maste		if [ "x$expect_success" = "xY" ] ; then
65f374ba41SEd Maste			if [ $r -ne 0 ] ; then
66f374ba41SEd Maste				fail "ssh failed with exit status $r"
67f374ba41SEd Maste			fi
684f52dfbbSDag-Erling Smørgrav			test -f ${COPY}	 || fail "failed copy ${DATA}"
694f52dfbbSDag-Erling Smørgrav			cmp ${DATA} ${COPY} || fail "corrupted copy of ${DATA}"
70f374ba41SEd Maste		elif [ $r -eq 0 ] ; then
71f374ba41SEd Maste			fail "ssh unexpectedly succeeded"
72f374ba41SEd Maste		fi
73ce3adf43SDag-Erling Smørgrav	    done
74ce3adf43SDag-Erling Smørgrav	done
75f374ba41SEd Maste}
76ce3adf43SDag-Erling Smørgrav
77f374ba41SEd Mastestart_sshd
78f374ba41SEd Mastetrap "stop_ssh" EXIT
794f52dfbbSDag-Erling Smørgrav
80f374ba41SEd Mastefor d in D R; do
81f374ba41SEd Maste	verbose "test -$d forwarding"
82f374ba41SEd Maste	start_ssh $d
83f374ba41SEd Maste	check_socks $d Y
84f374ba41SEd Maste	stop_ssh
85f374ba41SEd Maste	test "x$d" = "xR" || continue
86f374ba41SEd Maste
87f374ba41SEd Maste	# Test PermitRemoteOpen
88f374ba41SEd Maste	verbose "PermitRemoteOpen=any"
89f374ba41SEd Maste	start_ssh $d PermitRemoteOpen=any
90f374ba41SEd Maste	check_socks $d Y
91f374ba41SEd Maste	stop_ssh
92f374ba41SEd Maste
93f374ba41SEd Maste	verbose "PermitRemoteOpen=none"
94f374ba41SEd Maste	start_ssh $d PermitRemoteOpen=none
95f374ba41SEd Maste	check_socks $d N
96f374ba41SEd Maste	stop_ssh
97f374ba41SEd Maste
98f374ba41SEd Maste	verbose "PermitRemoteOpen=explicit"
99f374ba41SEd Maste	permit="127.0.0.1:$PORT [::1]:$PORT localhost:$PORT"
100f374ba41SEd Maste	test -z "$SKIP_IPV6" || permit="127.0.0.1:$PORT localhost:$PORT"
101f374ba41SEd Maste	start_ssh $d PermitRemoteOpen="$permit"
102f374ba41SEd Maste	check_socks $d Y
103f374ba41SEd Maste	stop_ssh
104f374ba41SEd Maste
105f374ba41SEd Maste	verbose "PermitRemoteOpen=disallowed"
106f374ba41SEd Maste	permit="127.0.0.1:1 [::1]:1 localhost:1"
107f374ba41SEd Maste	test -z "$SKIP_IPV6" || permit="127.0.0.1:1 localhost:1"
108f374ba41SEd Maste	start_ssh $d PermitRemoteOpen="$permit"
109f374ba41SEd Maste	check_socks $d N
110f374ba41SEd Maste	stop_ssh
111ce3adf43SDag-Erling Smørgravdone
112