You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #! /bin/sh
  2. # source: ftp.sh
  3. # Copyright Gerhard Rieger and contributors (see file CHANGES)
  4. # Published under the GNU General Public License V.2, see file COPYING
  5. # example how to write a shell script that communicates with stdio on the front
  6. # end and with a socat address on the back end
  7. # usage:
  8. # ftp.sh [opts] server directory/ # show directory contents on stdout
  9. # ftp.sh [opts] server file # print file contents to stdout
  10. # opts:
  11. # -socks socksserver # use given socks server, port 1080
  12. # -proxy proxyserver # use given proxy server, port 8080
  13. # # must be http proxy that accepts CONNECT
  14. # # method to ports 21 and >=1024
  15. # -user username # default: "ftp"
  16. # -passwd password # default: "anonymous@domain.org"
  17. # -t # shell script trace+debug
  18. # -d # debug on control connection (use up to 4 times)
  19. # -D # debug on data connection (use up to 4 times)
  20. # -b # block size for data connection
  21. # -v # verbose
  22. # -l* # socat logging options
  23. # example:
  24. # ftp.sh -v -d -d -D -D -D -b 65536 -proxy proxy ftp.ftp.org /README >README
  25. user="ftp"
  26. passwd="anonymous@domain.org"
  27. #method="socks4:socks" # socks4 is address spec, socks is socks server name
  28. method=tcp
  29. addropts=
  30. # socat options
  31. SO1=
  32. SO2=
  33. while :; do
  34. case "$1" in
  35. -socks|-socks4) shift;
  36. case "$1" in
  37. *:*) method="socks4:${1%%:*}"; addropts="socksport=${1#*:}" ;;
  38. *) method="socks4:$1" ;;
  39. esac ;;
  40. -socks4a) shift;
  41. case "$1" in
  42. *:*) method="socks4a:${1%%:*}"; addropts="socksport=${1#*:}" ;;
  43. *) method="socks4a:$1" ;;
  44. esac ;;
  45. -proxy) shift;
  46. case "$1" in
  47. *:*) method="proxy:${1%%:*}"; addropts="proxyport=${1#*:}" ;;
  48. *) method="proxy:$1" ;;
  49. esac ;;
  50. -user) shift; user="$1" ;;
  51. -passwd) shift; passwd="$1" ;;
  52. -t) set -vx ;;
  53. -d) SO1="$SO1 -d" ;;
  54. -D) SO2="$SO2 -d" ;;
  55. -b) SO2="$SO2 -b $2"; shift ;;
  56. -v) SO1="$SO1 -v" ;;
  57. -l*) SO1="$SO1 $1"; SO2="$SO2 $1" ;;
  58. -*) echo "unknown option \"$1\"" >&2; exit 1;;
  59. *) break ;;
  60. esac
  61. shift
  62. done
  63. export SO2
  64. server="$1"
  65. dir="$2"
  66. echo "addr=$method:$server:21,$addropts"; exit
  67. ### this is the central part to establish communication with socat ###
  68. ### copy these lines to make new communication shell scripts
  69. TMPDIR=$(if [ -x /bin/mktemp ]; then
  70. /bin/mktemp -d /tmp/$USER/FTPSH.XXXXXX
  71. else
  72. (umask 077; d=/tmp/$USER/FTPSH.$$; mkdir $d; echo $d)
  73. fi)
  74. TO="$TMPDIR/to"; FROM="$TMPDIR/from"
  75. socat $SO1 fifo:$TO,nonblock,ignoreeof!!fifo:$FROM $method:$server:21,$addropts &
  76. S1=$!
  77. while ! [ -p "$TO" -a -p "$FROM" ]; do sleep 1; done
  78. exec 4>$TMPDIR/to 3<$TMPDIR/from
  79. trap "S1=" 17
  80. #trap "echo cleaning up...>&2; rm -r $TMPDIR; [ -n "$S1" ] && kill $S1" 0 3
  81. trap "rm -r $TMPDIR" 0 3
  82. ### here the central part ends
  83. # this function waits for a complete server message, checks if its status
  84. # is in the permitted range (terminates session if not), and returns.
  85. ftp_chat () {
  86. local cmd="$1"
  87. local errlevel="$2"; [ -z "$errlevel" ] && errlevel=300
  88. if [ -n "$cmd" ]; then echo "$cmd" >&4; fi
  89. while read status message <&3;
  90. ( case "$status" in [0-9][0-9][0-9]-*) exit 0;; [0-9][0-9][0-9]*) exit 1;; *) exit 1;; esac )
  91. do :; done
  92. #echo "got \"$status $message\"" >&2
  93. if [ -z "$status" ]; then echo ftp data connection failed >&2; exit; fi
  94. if [ "$status" -ge "$errlevel" ]; then
  95. echo $message >&2
  96. echo "QUIT" >&4; exit 1
  97. fi
  98. set +vx
  99. }
  100. # wait for server greeting
  101. ftp_chat
  102. ftp_chat "USER $user" 400
  103. ftp_chat "PASS $passwd"
  104. #ftp_chat "CWD $dir"
  105. case "$dir" in
  106. */) ftp_chat "TYPE A" ;;
  107. *) ftp_chat "TYPE I" ;;
  108. esac
  109. echo "PASV" >&4; read status message <&3
  110. info=$(expr "$message" : '.*[^0-9]\([0-9]*,[0-9]*,[0-9]*,[0-9]*,[0-9]*,[0-9]*\).*')
  111. echo $info |tr ',' ' ' |(read i1 i2 i3 i4 p1 p2
  112. addr=$i1.$i2.$i3.$i4
  113. port=$(echo "256*$p1+$p2" |bc)
  114. #echo $addr:$port
  115. trap : 20
  116. # open data connection and transfer data
  117. socat -u $SO2 $method:$server:$port,$addropts -
  118. ) &
  119. S2=$!
  120. case "$dir" in
  121. */) ftp_chat "NLST $dir" ;;
  122. #*/) ftp_chat "LIST $dir" ;;
  123. *) ftp_chat "RETR $dir" ;;
  124. esac
  125. case "$status" in
  126. [45]*) kill $S2;;
  127. esac
  128. #echo "waiting for process $S2 to terminate" >&2
  129. wait $S2
  130. ftp_chat
  131. ftp_chat "QUIT"
  132. #echo "waiting for process $S1 to terminate" >&2
  133. wait $S1
  134. exit