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