forwarding.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # $OpenBSD: forwarding.sh,v 1.23 2019/07/20 09:50:58 dtucker Exp $
  2. # Placed in the Public Domain.
  3. tid="local and remote forwarding"
  4. DATA=/bin/ls${EXEEXT}
  5. start_sshd
  6. base=33
  7. last=$PORT
  8. fwd=""
  9. make_tmpdir
  10. CTL=${SSH_REGRESS_TMP}/ctl-sock
  11. for j in 0 1 2; do
  12. for i in 0 1 2; do
  13. a=$base$j$i
  14. b=$(expr $a + 50)
  15. c=$last
  16. # fwd chain: $a -> $b -> $c
  17. fwd="$fwd -L$a:127.0.0.1:$b -R$b:127.0.0.1:$c"
  18. last=$a
  19. done
  20. done
  21. trace "start forwarding, fork to background"
  22. rm -f $CTL
  23. ${SSH} -S $CTL -N -M -F $OBJ/ssh_config -f $fwd somehost
  24. trace "transfer over forwarded channels and check result"
  25. ${SSH} -F $OBJ/ssh_config -p$last -o 'ConnectionAttempts=4' \
  26. somehost cat ${DATA} > ${COPY}
  27. test -s ${COPY} || fail "failed copy of ${DATA}"
  28. cmp ${DATA} ${COPY} || fail "corrupted copy of ${DATA}"
  29. ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost 2> /dev/null
  30. for d in L R; do
  31. trace "exit on -$d forward failure"
  32. # this one should succeed
  33. ${SSH} -F $OBJ/ssh_config \
  34. -$d ${base}01:127.0.0.1:$PORT \
  35. -$d ${base}02:127.0.0.1:$PORT \
  36. -$d ${base}03:127.0.0.1:$PORT \
  37. -$d ${base}04:127.0.0.1:$PORT \
  38. -oExitOnForwardFailure=yes somehost true
  39. if [ $? != 0 ]; then
  40. fatal "connection failed, should not"
  41. else
  42. # this one should fail
  43. ${SSH} -q -F $OBJ/ssh_config \
  44. -$d ${base}01:127.0.0.1:$PORT \
  45. -$d ${base}02:127.0.0.1:$PORT \
  46. -$d ${base}03:127.0.0.1:$PORT \
  47. -$d ${base}01:localhost:$PORT \
  48. -$d ${base}04:127.0.0.1:$PORT \
  49. -oExitOnForwardFailure=yes somehost true
  50. r=$?
  51. if [ $r != 255 ]; then
  52. fail "connection not termintated, but should ($r)"
  53. fi
  54. fi
  55. done
  56. trace "simple clear forwarding"
  57. ${SSH} -F $OBJ/ssh_config -oClearAllForwardings=yes somehost true
  58. trace "clear local forward"
  59. rm -f $CTL
  60. ${SSH} -S $CTL -N -M -f -F $OBJ/ssh_config -L ${base}01:127.0.0.1:$PORT \
  61. -oClearAllForwardings=yes somehost
  62. if [ $? != 0 ]; then
  63. fail "connection failed with cleared local forwarding"
  64. else
  65. # this one should fail
  66. ${SSH} -F $OBJ/ssh_config -p ${base}01 somehost true \
  67. >> $TEST_REGRESS_LOGFILE 2>&1 &&
  68. fail "local forwarding not cleared"
  69. fi
  70. ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost 2> /dev/null
  71. trace "clear remote forward"
  72. rm -f $CTL
  73. ${SSH} -S $CTL -N -M -f -F $OBJ/ssh_config -R ${base}01:127.0.0.1:$PORT \
  74. -oClearAllForwardings=yes somehost
  75. if [ $? != 0 ]; then
  76. fail "connection failed with cleared remote forwarding"
  77. else
  78. # this one should fail
  79. ${SSH} -F $OBJ/ssh_config -p ${base}01 somehost true \
  80. >> $TEST_REGRESS_LOGFILE 2>&1 &&
  81. fail "remote forwarding not cleared"
  82. fi
  83. ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost 2> /dev/null
  84. trace "stdio forwarding"
  85. cmd="${SSH} -F $OBJ/ssh_config"
  86. $cmd -o "ProxyCommand $cmd -q -W localhost:$PORT somehost" somehost true
  87. if [ $? != 0 ]; then
  88. fail "stdio forwarding"
  89. fi
  90. echo "LocalForward ${base}01 127.0.0.1:$PORT" >> $OBJ/ssh_config
  91. echo "RemoteForward ${base}02 127.0.0.1:${base}01" >> $OBJ/ssh_config
  92. trace "config file: start forwarding, fork to background"
  93. rm -f $CTL
  94. ${SSH} -S $CTL -N -M -F $OBJ/ssh_config -f somehost
  95. trace "config file: transfer over forwarded channels and check result"
  96. ${SSH} -F $OBJ/ssh_config -p${base}02 -o 'ConnectionAttempts=4' \
  97. somehost cat ${DATA} > ${COPY}
  98. test -s ${COPY} || fail "failed copy of ${DATA}"
  99. cmp ${DATA} ${COPY} || fail "corrupted copy of ${DATA}"
  100. ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost 2> /dev/null
  101. trace "transfer over chained unix domain socket forwards and check result"
  102. rm -f $OBJ/unix-[123].fwd
  103. rm -f $CTL $CTL.[123]
  104. ${SSH} -S $CTL -N -M -f -F $OBJ/ssh_config -R${base}01:[$OBJ/unix-1.fwd] somehost
  105. ${SSH} -S $CTL.1 -N -M -f -F $OBJ/ssh_config -L[$OBJ/unix-1.fwd]:[$OBJ/unix-2.fwd] somehost
  106. ${SSH} -S $CTL.2 -N -M -f -F $OBJ/ssh_config -R[$OBJ/unix-2.fwd]:[$OBJ/unix-3.fwd] somehost
  107. ${SSH} -S $CTL.3 -N -M -f -F $OBJ/ssh_config -L[$OBJ/unix-3.fwd]:127.0.0.1:$PORT somehost
  108. ${SSH} -F $OBJ/ssh_config -p${base}01 -o 'ConnectionAttempts=4' \
  109. somehost cat ${DATA} > ${COPY}
  110. test -s ${COPY} || fail "failed copy ${DATA}"
  111. cmp ${DATA} ${COPY} || fail "corrupted copy of ${DATA}"
  112. ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost 2> /dev/null
  113. ${SSH} -F $OBJ/ssh_config -S $CTL.1 -O exit somehost 2> /dev/null
  114. ${SSH} -F $OBJ/ssh_config -S $CTL.2 -O exit somehost 2> /dev/null
  115. ${SSH} -F $OBJ/ssh_config -S $CTL.3 -O exit somehost 2> /dev/null