djm@openbsd.org | dd36932 | 2017-04-30 23:34:55 +0000 | [diff] [blame] | 1 | # $OpenBSD: forwarding.sh,v 1.20 2017/04/30 23:34:55 djm Exp $ |
Damien Miller | 38cd435 | 2002-05-01 13:17:33 +1000 | [diff] [blame] | 2 | # Placed in the Public Domain. |
| 3 | |
| 4 | tid="local and remote forwarding" |
| 5 | |
Darren Tucker | 2a22873 | 2013-06-06 01:59:13 +1000 | [diff] [blame] | 6 | DATA=/bin/ls${EXEEXT} |
| 7 | |
Damien Miller | 38cd435 | 2002-05-01 13:17:33 +1000 | [diff] [blame] | 8 | start_sshd |
| 9 | |
| 10 | base=33 |
| 11 | last=$PORT |
| 12 | fwd="" |
Damien Miller | 9165abf | 2017-03-20 09:58:34 +1100 | [diff] [blame] | 13 | CTL=/tmp/openssh.regress.ctl-sock.$$ |
djm@openbsd.org | b3c1915 | 2015-02-23 20:32:15 +0000 | [diff] [blame] | 14 | |
Damien Miller | 38cd435 | 2002-05-01 13:17:33 +1000 | [diff] [blame] | 15 | for j in 0 1 2; do |
| 16 | for i in 0 1 2; do |
| 17 | a=$base$j$i |
| 18 | b=`expr $a + 50` |
| 19 | c=$last |
| 20 | # fwd chain: $a -> $b -> $c |
| 21 | fwd="$fwd -L$a:127.0.0.1:$b -R$b:127.0.0.1:$c" |
| 22 | last=$a |
| 23 | done |
| 24 | done |
Damien Miller | 38cd435 | 2002-05-01 13:17:33 +1000 | [diff] [blame] | 25 | |
djm@openbsd.org | dd36932 | 2017-04-30 23:34:55 +0000 | [diff] [blame] | 26 | trace "start forwarding, fork to background" |
| 27 | rm -f $CTL |
| 28 | ${SSH} -S $CTL -M -F $OBJ/ssh_config -f $fwd somehost sleep 10 |
Damien Miller | 38cd435 | 2002-05-01 13:17:33 +1000 | [diff] [blame] | 29 | |
djm@openbsd.org | dd36932 | 2017-04-30 23:34:55 +0000 | [diff] [blame] | 30 | trace "transfer over forwarded channels and check result" |
| 31 | ${SSH} -F $OBJ/ssh_config -p$last -o 'ConnectionAttempts=4' \ |
| 32 | somehost cat ${DATA} > ${COPY} |
| 33 | test -s ${COPY} || fail "failed copy of ${DATA}" |
| 34 | cmp ${DATA} ${COPY} || fail "corrupted copy of ${DATA}" |
Damien Miller | edaeff5 | 2006-01-31 21:56:24 +1100 | [diff] [blame] | 35 | |
djm@openbsd.org | dd36932 | 2017-04-30 23:34:55 +0000 | [diff] [blame] | 36 | ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost |
| 37 | |
Damien Miller | 7b1877c | 2006-07-24 15:31:41 +1000 | [diff] [blame] | 38 | for d in L R; do |
djm@openbsd.org | dd36932 | 2017-04-30 23:34:55 +0000 | [diff] [blame] | 39 | trace "exit on -$d forward failure" |
Damien Miller | 7b1877c | 2006-07-24 15:31:41 +1000 | [diff] [blame] | 40 | |
| 41 | # this one should succeed |
djm@openbsd.org | dd36932 | 2017-04-30 23:34:55 +0000 | [diff] [blame] | 42 | ${SSH} -F $OBJ/ssh_config \ |
Damien Miller | 7b1877c | 2006-07-24 15:31:41 +1000 | [diff] [blame] | 43 | -$d ${base}01:127.0.0.1:$PORT \ |
| 44 | -$d ${base}02:127.0.0.1:$PORT \ |
| 45 | -$d ${base}03:127.0.0.1:$PORT \ |
| 46 | -$d ${base}04:127.0.0.1:$PORT \ |
| 47 | -oExitOnForwardFailure=yes somehost true |
| 48 | if [ $? != 0 ]; then |
djm@openbsd.org | f25ee13 | 2017-01-30 05:22:14 +0000 | [diff] [blame] | 49 | fatal "connection failed, should not" |
Damien Miller | 7b1877c | 2006-07-24 15:31:41 +1000 | [diff] [blame] | 50 | else |
| 51 | # this one should fail |
djm@openbsd.org | dd36932 | 2017-04-30 23:34:55 +0000 | [diff] [blame] | 52 | ${SSH} -q -F $OBJ/ssh_config \ |
Damien Miller | 7b1877c | 2006-07-24 15:31:41 +1000 | [diff] [blame] | 53 | -$d ${base}01:127.0.0.1:$PORT \ |
| 54 | -$d ${base}02:127.0.0.1:$PORT \ |
| 55 | -$d ${base}03:127.0.0.1:$PORT \ |
djm@openbsd.org | 6b8a1a8 | 2016-04-14 23:57:17 +0000 | [diff] [blame] | 56 | -$d ${base}01:localhost:$PORT \ |
Damien Miller | 7b1877c | 2006-07-24 15:31:41 +1000 | [diff] [blame] | 57 | -$d ${base}04:127.0.0.1:$PORT \ |
| 58 | -oExitOnForwardFailure=yes somehost true |
| 59 | r=$? |
| 60 | if [ $r != 255 ]; then |
| 61 | fail "connection not termintated, but should ($r)" |
| 62 | fi |
| 63 | fi |
| 64 | done |
Damien Miller | 7b1877c | 2006-07-24 15:31:41 +1000 | [diff] [blame] | 65 | |
djm@openbsd.org | dd36932 | 2017-04-30 23:34:55 +0000 | [diff] [blame] | 66 | trace "simple clear forwarding" |
| 67 | ${SSH} -F $OBJ/ssh_config -oClearAllForwardings=yes somehost true |
Damien Miller | edaeff5 | 2006-01-31 21:56:24 +1100 | [diff] [blame] | 68 | |
djm@openbsd.org | dd36932 | 2017-04-30 23:34:55 +0000 | [diff] [blame] | 69 | trace "clear local forward" |
| 70 | rm -f $CTL |
| 71 | ${SSH} -S $CTL -M -f -F $OBJ/ssh_config -L ${base}01:127.0.0.1:$PORT \ |
| 72 | -oClearAllForwardings=yes somehost sleep 10 |
| 73 | if [ $? != 0 ]; then |
| 74 | fail "connection failed with cleared local forwarding" |
| 75 | else |
| 76 | # this one should fail |
| 77 | ${SSH} -F $OBJ/ssh_config -p ${base}01 somehost true \ |
| 78 | >>$TEST_REGRESS_LOGFILE 2>&1 && \ |
| 79 | fail "local forwarding not cleared" |
| 80 | fi |
| 81 | ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost |
Damien Miller | 0dff9c7 | 2010-02-24 17:25:58 +1100 | [diff] [blame] | 82 | |
djm@openbsd.org | dd36932 | 2017-04-30 23:34:55 +0000 | [diff] [blame] | 83 | trace "clear remote forward" |
| 84 | rm -f $CTL |
| 85 | ${SSH} -S $CTL -M -f -F $OBJ/ssh_config -R ${base}01:127.0.0.1:$PORT \ |
| 86 | -oClearAllForwardings=yes somehost sleep 10 |
| 87 | if [ $? != 0 ]; then |
| 88 | fail "connection failed with cleared remote forwarding" |
| 89 | else |
| 90 | # this one should fail |
| 91 | ${SSH} -F $OBJ/ssh_config -p ${base}01 somehost true \ |
| 92 | >>$TEST_REGRESS_LOGFILE 2>&1 && \ |
| 93 | fail "remote forwarding not cleared" |
| 94 | fi |
| 95 | ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost |
| 96 | |
| 97 | trace "stdio forwarding" |
| 98 | cmd="${SSH} -F $OBJ/ssh_config" |
| 99 | $cmd -o "ProxyCommand $cmd -q -W localhost:$PORT somehost" somehost true |
| 100 | if [ $? != 0 ]; then |
| 101 | fail "stdio forwarding" |
| 102 | fi |
Damien Miller | 78d47b7 | 2013-07-25 12:08:46 +1000 | [diff] [blame] | 103 | |
Tim Rice | 0553ad7 | 2013-07-25 16:03:16 -0700 | [diff] [blame] | 104 | echo "LocalForward ${base}01 127.0.0.1:$PORT" >> $OBJ/ssh_config |
| 105 | echo "RemoteForward ${base}02 127.0.0.1:${base}01" >> $OBJ/ssh_config |
Damien Miller | 78d47b7 | 2013-07-25 12:08:46 +1000 | [diff] [blame] | 106 | |
djm@openbsd.org | dd36932 | 2017-04-30 23:34:55 +0000 | [diff] [blame] | 107 | trace "config file: start forwarding, fork to background" |
| 108 | rm -f $CTL |
| 109 | ${SSH} -S $CTL -M -F $OBJ/ssh_config -f somehost sleep 10 |
Damien Miller | 78d47b7 | 2013-07-25 12:08:46 +1000 | [diff] [blame] | 110 | |
djm@openbsd.org | dd36932 | 2017-04-30 23:34:55 +0000 | [diff] [blame] | 111 | trace "config file: transfer over forwarded channels and check result" |
| 112 | ${SSH} -F $OBJ/ssh_config -p${base}02 -o 'ConnectionAttempts=4' \ |
| 113 | somehost cat ${DATA} > ${COPY} |
| 114 | test -s ${COPY} || fail "failed copy of ${DATA}" |
| 115 | cmp ${DATA} ${COPY} || fail "corrupted copy of ${DATA}" |
Damien Miller | 0e4e955 | 2014-07-21 09:52:54 +1000 | [diff] [blame] | 116 | |
djm@openbsd.org | dd36932 | 2017-04-30 23:34:55 +0000 | [diff] [blame] | 117 | ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost |
Damien Miller | 0e4e955 | 2014-07-21 09:52:54 +1000 | [diff] [blame] | 118 | |
djm@openbsd.org | dd36932 | 2017-04-30 23:34:55 +0000 | [diff] [blame] | 119 | trace "transfer over chained unix domain socket forwards and check result" |
| 120 | rm -f $OBJ/unix-[123].fwd |
| 121 | rm -f $CTL $CTL.[123] |
| 122 | ${SSH} -S $CTL -M -f -F $OBJ/ssh_config -R${base}01:[$OBJ/unix-1.fwd] somehost sleep 10 |
| 123 | ${SSH} -S $CTL.1 -M -f -F $OBJ/ssh_config -L[$OBJ/unix-1.fwd]:[$OBJ/unix-2.fwd] somehost sleep 10 |
| 124 | ${SSH} -S $CTL.2 -M -f -F $OBJ/ssh_config -R[$OBJ/unix-2.fwd]:[$OBJ/unix-3.fwd] somehost sleep 10 |
| 125 | ${SSH} -S $CTL.3 -M -f -F $OBJ/ssh_config -L[$OBJ/unix-3.fwd]:127.0.0.1:$PORT somehost sleep 10 |
| 126 | ${SSH} -F $OBJ/ssh_config -p${base}01 -o 'ConnectionAttempts=4' \ |
| 127 | somehost cat ${DATA} > ${COPY} |
| 128 | test -s ${COPY} || fail "failed copy ${DATA}" |
| 129 | cmp ${DATA} ${COPY} || fail "corrupted copy of ${DATA}" |
| 130 | |
| 131 | ${SSH} -F $OBJ/ssh_config -S $CTL -O exit somehost |
| 132 | ${SSH} -F $OBJ/ssh_config -S $CTL.1 -O exit somehost |
| 133 | ${SSH} -F $OBJ/ssh_config -S $CTL.2 -O exit somehost |
| 134 | ${SSH} -F $OBJ/ssh_config -S $CTL.3 -O exit somehost |
| 135 | |