ftp.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. -----------------------------------------------------------------------------
  2. -- FTP support for the Lua language
  3. -- LuaSocket toolkit.
  4. -- Author: Diego Nehab
  5. -----------------------------------------------------------------------------
  6. -----------------------------------------------------------------------------
  7. -- Declare module and import dependencies
  8. -----------------------------------------------------------------------------
  9. local base = _G
  10. local table = require("table")
  11. local string = require("string")
  12. local math = require("math")
  13. local socket = require("socket")
  14. local url = require("socket.url")
  15. local tp = require("socket.tp")
  16. local ltn12 = require("ltn12")
  17. socket.ftp = {}
  18. local _M = socket.ftp
  19. -----------------------------------------------------------------------------
  20. -- Program constants
  21. -----------------------------------------------------------------------------
  22. -- timeout in seconds before the program gives up on a connection
  23. _M.TIMEOUT = 60
  24. -- default port for ftp service
  25. local PORT = 21
  26. -- this is the default anonymous password. used when no password is
  27. -- provided in url. should be changed to your e-mail.
  28. _M.USER = "ftp"
  29. _M.PASSWORD = "anonymous@anonymous.org"
  30. -----------------------------------------------------------------------------
  31. -- Low level FTP API
  32. -----------------------------------------------------------------------------
  33. local metat = { __index = {} }
  34. function _M.open(server, port, create)
  35. local tp = socket.try(tp.connect(server, port or PORT, _M.TIMEOUT, create))
  36. local f = base.setmetatable({ tp = tp }, metat)
  37. -- make sure everything gets closed in an exception
  38. f.try = socket.newtry(function() f:close() end)
  39. return f
  40. end
  41. function metat.__index:portconnect()
  42. self.try(self.server:settimeout(_M.TIMEOUT))
  43. self.data = self.try(self.server:accept())
  44. self.try(self.data:settimeout(_M.TIMEOUT))
  45. end
  46. function metat.__index:pasvconnect()
  47. self.data = self.try(socket.tcp())
  48. self.try(self.data:settimeout(_M.TIMEOUT))
  49. self.try(self.data:connect(self.pasvt.address, self.pasvt.port))
  50. end
  51. function metat.__index:login(user, password)
  52. self.try(self.tp:command("user", user or _M.USER))
  53. local code, reply = self.try(self.tp:check{"2..", 331})
  54. if code == 331 then
  55. self.try(self.tp:command("pass", password or _M.PASSWORD))
  56. self.try(self.tp:check("2.."))
  57. end
  58. return 1
  59. end
  60. function metat.__index:pasv()
  61. self.try(self.tp:command("pasv"))
  62. local code, reply = self.try(self.tp:check("2.."))
  63. local pattern = "(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)"
  64. local a, b, c, d, p1, p2 = socket.skip(2, string.find(reply, pattern))
  65. self.try(a and b and c and d and p1 and p2, reply)
  66. self.pasvt = {
  67. address = string.format("%d.%d.%d.%d", a, b, c, d),
  68. port = p1*256 + p2
  69. }
  70. if self.server then
  71. self.server:close()
  72. self.server = nil
  73. end
  74. return self.pasvt.address, self.pasvt.port
  75. end
  76. function metat.__index:epsv()
  77. self.try(self.tp:command("epsv"))
  78. local code, reply = self.try(self.tp:check("229"))
  79. local pattern = "%((.)(.-)%1(.-)%1(.-)%1%)"
  80. local d, prt, address, port = string.match(reply, pattern)
  81. self.try(port, "invalid epsv response")
  82. self.pasvt = {
  83. address = self.tp:getpeername(),
  84. port = port
  85. }
  86. if self.server then
  87. self.server:close()
  88. self.server = nil
  89. end
  90. return self.pasvt.address, self.pasvt.port
  91. end
  92. function metat.__index:port(address, port)
  93. self.pasvt = nil
  94. if not address then
  95. address, port = self.try(self.tp:getsockname())
  96. self.server = self.try(socket.bind(address, 0))
  97. address, port = self.try(self.server:getsockname())
  98. self.try(self.server:settimeout(_M.TIMEOUT))
  99. end
  100. local pl = math.mod(port, 256)
  101. local ph = (port - pl)/256
  102. local arg = string.gsub(string.format("%s,%d,%d", address, ph, pl), "%.", ",")
  103. self.try(self.tp:command("port", arg))
  104. self.try(self.tp:check("2.."))
  105. return 1
  106. end
  107. function metat.__index:eprt(family, address, port)
  108. self.pasvt = nil
  109. if not address then
  110. address, port = self.try(self.tp:getsockname())
  111. self.server = self.try(socket.bind(address, 0))
  112. address, port = self.try(self.server:getsockname())
  113. self.try(self.server:settimeout(_M.TIMEOUT))
  114. end
  115. local arg = string.format("|%s|%s|%d|", family, address, port)
  116. self.try(self.tp:command("eprt", arg))
  117. self.try(self.tp:check("2.."))
  118. return 1
  119. end
  120. function metat.__index:send(sendt)
  121. self.try(self.pasvt or self.server, "need port or pasv first")
  122. -- if there is a pasvt table, we already sent a PASV command
  123. -- we just get the data connection into self.data
  124. if self.pasvt then self:pasvconnect() end
  125. -- get the transfer argument and command
  126. local argument = sendt.argument or
  127. url.unescape(string.gsub(sendt.path or "", "^[/\\]", ""))
  128. if argument == "" then argument = nil end
  129. local command = sendt.command or "stor"
  130. -- send the transfer command and check the reply
  131. self.try(self.tp:command(command, argument))
  132. local code, reply = self.try(self.tp:check{"2..", "1.."})
  133. -- if there is not a pasvt table, then there is a server
  134. -- and we already sent a PORT command
  135. if not self.pasvt then self:portconnect() end
  136. -- get the sink, source and step for the transfer
  137. local step = sendt.step or ltn12.pump.step
  138. local readt = { self.tp }
  139. local checkstep = function(src, snk)
  140. -- check status in control connection while downloading
  141. local readyt = socket.select(readt, nil, 0)
  142. if readyt[tp] then code = self.try(self.tp:check("2..")) end
  143. return step(src, snk)
  144. end
  145. local sink = socket.sink("close-when-done", self.data)
  146. -- transfer all data and check error
  147. self.try(ltn12.pump.all(sendt.source, sink, checkstep))
  148. if string.find(code, "1..") then self.try(self.tp:check("2..")) end
  149. -- done with data connection
  150. self.data:close()
  151. -- find out how many bytes were sent
  152. local sent = socket.skip(1, self.data:getstats())
  153. self.data = nil
  154. return sent
  155. end
  156. function metat.__index:receive(recvt)
  157. self.try(self.pasvt or self.server, "need port or pasv first")
  158. if self.pasvt then self:pasvconnect() end
  159. local argument = recvt.argument or
  160. url.unescape(string.gsub(recvt.path or "", "^[/\\]", ""))
  161. if argument == "" then argument = nil end
  162. local command = recvt.command or "retr"
  163. self.try(self.tp:command(command, argument))
  164. local code,reply = self.try(self.tp:check{"1..", "2.."})
  165. if (code >= 200) and (code <= 299) then
  166. recvt.sink(reply)
  167. return 1
  168. end
  169. if not self.pasvt then self:portconnect() end
  170. local source = socket.source("until-closed", self.data)
  171. local step = recvt.step or ltn12.pump.step
  172. self.try(ltn12.pump.all(source, recvt.sink, step))
  173. if string.find(code, "1..") then self.try(self.tp:check("2..")) end
  174. self.data:close()
  175. self.data = nil
  176. return 1
  177. end
  178. function metat.__index:cwd(dir)
  179. self.try(self.tp:command("cwd", dir))
  180. self.try(self.tp:check(250))
  181. return 1
  182. end
  183. function metat.__index:type(type)
  184. self.try(self.tp:command("type", type))
  185. self.try(self.tp:check(200))
  186. return 1
  187. end
  188. function metat.__index:greet()
  189. local code = self.try(self.tp:check{"1..", "2.."})
  190. if string.find(code, "1..") then self.try(self.tp:check("2..")) end
  191. return 1
  192. end
  193. function metat.__index:quit()
  194. self.try(self.tp:command("quit"))
  195. self.try(self.tp:check("2.."))
  196. return 1
  197. end
  198. function metat.__index:close()
  199. if self.data then self.data:close() end
  200. if self.server then self.server:close() end
  201. return self.tp:close()
  202. end
  203. -----------------------------------------------------------------------------
  204. -- High level FTP API
  205. -----------------------------------------------------------------------------
  206. local function override(t)
  207. if t.url then
  208. local u = url.parse(t.url)
  209. for i,v in base.pairs(t) do
  210. u[i] = v
  211. end
  212. return u
  213. else return t end
  214. end
  215. local function tput(putt)
  216. putt = override(putt)
  217. socket.try(putt.host, "missing hostname")
  218. local f = _M.open(putt.host, putt.port, putt.create)
  219. f:greet()
  220. f:login(putt.user, putt.password)
  221. if putt.type then f:type(putt.type) end
  222. f:epsv()
  223. local sent = f:send(putt)
  224. f:quit()
  225. f:close()
  226. return sent
  227. end
  228. local default = {
  229. path = "/",
  230. scheme = "ftp"
  231. }
  232. local function genericform(u)
  233. local t = socket.try(url.parse(u, default))
  234. socket.try(t.scheme == "ftp", "wrong scheme '" .. t.scheme .. "'")
  235. socket.try(t.host, "missing hostname")
  236. local pat = "^type=(.)$"
  237. if t.params then
  238. t.type = socket.skip(2, string.find(t.params, pat))
  239. socket.try(t.type == "a" or t.type == "i",
  240. "invalid type '" .. t.type .. "'")
  241. end
  242. return t
  243. end
  244. _M.genericform = genericform
  245. local function sput(u, body)
  246. local putt = genericform(u)
  247. putt.source = ltn12.source.string(body)
  248. return tput(putt)
  249. end
  250. _M.put = socket.protect(function(putt, body)
  251. if base.type(putt) == "string" then return sput(putt, body)
  252. else return tput(putt) end
  253. end)
  254. local function tget(gett)
  255. gett = override(gett)
  256. socket.try(gett.host, "missing hostname")
  257. local f = _M.open(gett.host, gett.port, gett.create)
  258. f:greet()
  259. f:login(gett.user, gett.password)
  260. if gett.type then f:type(gett.type) end
  261. f:epsv()
  262. f:receive(gett)
  263. f:quit()
  264. return f:close()
  265. end
  266. local function sget(u)
  267. local gett = genericform(u)
  268. local t = {}
  269. gett.sink = ltn12.sink.table(t)
  270. tget(gett)
  271. return table.concat(t)
  272. end
  273. _M.command = socket.protect(function(cmdt)
  274. cmdt = override(cmdt)
  275. socket.try(cmdt.host, "missing hostname")
  276. socket.try(cmdt.command, "missing command")
  277. local f = _M.open(cmdt.host, cmdt.port, cmdt.create)
  278. f:greet()
  279. f:login(cmdt.user, cmdt.password)
  280. if type(cmdt.command) == "table" then
  281. local argument = cmdt.argument or {}
  282. local check = cmdt.check or {}
  283. for i,cmd in ipairs(cmdt.command) do
  284. f.try(f.tp:command(cmd, argument[i]))
  285. if check[i] then f.try(f.tp:check(check[i])) end
  286. end
  287. else
  288. f.try(f.tp:command(cmdt.command, cmdt.argument))
  289. if cmdt.check then f.try(f.tp:check(cmdt.check)) end
  290. end
  291. f:quit()
  292. return f:close()
  293. end)
  294. _M.get = socket.protect(function(gett)
  295. if base.type(gett) == "string" then return sget(gett)
  296. else return tget(gett) end
  297. end)
  298. return _M