ftp.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. // Package ftp implements a FTP client as described in RFC 959.
  2. package ftp
  3. import (
  4. "bufio"
  5. "errors"
  6. "io"
  7. "net"
  8. "net/textproto"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // EntryType describes the different types of an Entry.
  14. type EntryType int
  15. const (
  16. EntryTypeFile EntryType = iota
  17. EntryTypeFolder
  18. EntryTypeLink
  19. )
  20. // ServerConn represents the connection to a remote FTP server.
  21. type ServerConn struct {
  22. conn *textproto.Conn
  23. host string
  24. features map[string]string
  25. }
  26. // Entry describes a file and is returned by List().
  27. type Entry struct {
  28. Name string
  29. Type EntryType
  30. Size uint64
  31. Time time.Time
  32. }
  33. // response represent a data-connection
  34. type response struct {
  35. conn net.Conn
  36. c *ServerConn
  37. }
  38. // Connect initializes the connection to the specified ftp server address.
  39. //
  40. // It is generally followed by a call to Login() as most FTP commands require
  41. // an authenticated user.
  42. func Connect(addr string) (*ServerConn, error) {
  43. conn, err := textproto.Dial("tcp", addr)
  44. if err != nil {
  45. return nil, err
  46. }
  47. host, _, err := net.SplitHostPort(addr)
  48. if err != nil {
  49. conn.Close()
  50. return nil, err
  51. }
  52. c := &ServerConn{
  53. conn: conn,
  54. host: host,
  55. features: make(map[string]string),
  56. }
  57. _, _, err = c.conn.ReadResponse(StatusReady)
  58. if err != nil {
  59. c.Quit()
  60. return nil, err
  61. }
  62. err = c.feat()
  63. if err != nil {
  64. c.Quit()
  65. return nil, err
  66. }
  67. return c, nil
  68. }
  69. // Login authenticates the client with specified user and password.
  70. //
  71. // "anonymous"/"anonymous" is a common user/password scheme for FTP servers
  72. // that allows anonymous read-only accounts.
  73. func (c *ServerConn) Login(user, password string) error {
  74. _, _, err := c.cmd(StatusUserOK, "USER %s", user)
  75. if err != nil {
  76. return err
  77. }
  78. _, _, err = c.cmd(StatusLoggedIn, "PASS %s", password)
  79. if err != nil {
  80. return err
  81. }
  82. // Switch to binary mode
  83. _, _, err = c.cmd(StatusCommandOK, "TYPE I")
  84. if err != nil {
  85. return err
  86. }
  87. return nil
  88. }
  89. // feat issues a FEAT FTP command to list the additional commands supported by
  90. // the remote FTP server.
  91. // FEAT is described in RFC 2389
  92. func (c *ServerConn) feat() error {
  93. code, message, err := c.cmd(-1, "FEAT")
  94. if err != nil {
  95. return err
  96. }
  97. if code != StatusSystem {
  98. // The server does not support the FEAT command. This is not an
  99. // error: we consider that there is no additional feature.
  100. return nil
  101. }
  102. lines := strings.Split(message, "\n")
  103. for _, line := range lines {
  104. if !strings.HasPrefix(line, " ") {
  105. continue
  106. }
  107. line = strings.TrimSpace(line)
  108. featureElements := strings.SplitN(line, " ", 2)
  109. command := featureElements[0]
  110. var commandDesc string
  111. if len(featureElements) == 2 {
  112. commandDesc = featureElements[1]
  113. }
  114. c.features[command] = commandDesc
  115. }
  116. return nil
  117. }
  118. // epsv issues an "EPSV" command to get a port number for a data connection.
  119. func (c *ServerConn) epsv() (port int, err error) {
  120. _, line, err := c.cmd(StatusExtendedPassiveMode, "EPSV")
  121. if err != nil {
  122. return
  123. }
  124. start := strings.Index(line, "|||")
  125. end := strings.LastIndex(line, "|")
  126. if start == -1 || end == -1 {
  127. err = errors.New("Invalid EPSV response format")
  128. return
  129. }
  130. port, err = strconv.Atoi(line[start+3 : end])
  131. return
  132. }
  133. // pasv issues a "PASV" command to get a port number for a data connection.
  134. func (c *ServerConn) pasv() (port int, err error) {
  135. _, line, err := c.cmd(StatusPassiveMode, "PASV")
  136. if err != nil {
  137. return
  138. }
  139. // PASV response format : 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2).
  140. start := strings.Index(line, "(")
  141. end := strings.LastIndex(line, ")")
  142. if start == -1 || end == -1 {
  143. err = errors.New("Invalid EPSV response format")
  144. return
  145. }
  146. // We have to split the response string
  147. pasvData := strings.Split(line[start+1:end], ",")
  148. // Let's compute the port number
  149. portPart1, err1 := strconv.Atoi(pasvData[4])
  150. if err1 != nil {
  151. err = err1
  152. return
  153. }
  154. portPart2, err2 := strconv.Atoi(pasvData[5])
  155. if err2 != nil {
  156. err = err2
  157. return
  158. }
  159. // Recompose port
  160. port = portPart1*256 + portPart2
  161. return
  162. }
  163. // openDataConn creates a new FTP data connection.
  164. func (c *ServerConn) openDataConn() (net.Conn, error) {
  165. var port int
  166. var err error
  167. // If features contains nat6 or EPSV => EPSV
  168. // else -> PASV
  169. _, nat6Supported := c.features["nat6"]
  170. _, epsvSupported := c.features["EPSV"]
  171. // If host is IPv6 => EPSV
  172. if strings.ContainsAny(c.host, ":%") {
  173. epsvSupported = true
  174. }
  175. if nat6Supported || epsvSupported {
  176. port, err = c.epsv()
  177. if err != nil {
  178. return nil, err
  179. }
  180. } else {
  181. port, err = c.pasv()
  182. if err != nil {
  183. return nil, err
  184. }
  185. }
  186. // Build the new net address string
  187. addr := net.JoinHostPort(c.host, strconv.Itoa(port))
  188. conn, err := net.Dial("tcp", addr)
  189. if err != nil {
  190. return nil, err
  191. }
  192. return conn, nil
  193. }
  194. // cmd is a helper function to execute a command and check for the expected FTP
  195. // return code
  196. func (c *ServerConn) cmd(expected int, format string, args ...interface{}) (int, string, error) {
  197. _, err := c.conn.Cmd(format, args...)
  198. if err != nil {
  199. return 0, "", err
  200. }
  201. code, line, err := c.conn.ReadResponse(expected)
  202. return code, line, err
  203. }
  204. // cmdDataConn executes a command which require a FTP data connection.
  205. func (c *ServerConn) cmdDataConn(format string, args ...interface{}) (net.Conn, error) {
  206. conn, err := c.openDataConn()
  207. if err != nil {
  208. return nil, err
  209. }
  210. _, err = c.conn.Cmd(format, args...)
  211. if err != nil {
  212. conn.Close()
  213. return nil, err
  214. }
  215. code, msg, err := c.conn.ReadCodeLine(-1)
  216. if err != nil {
  217. conn.Close()
  218. return nil, err
  219. }
  220. if code != StatusAlreadyOpen && code != StatusAboutToSend {
  221. conn.Close()
  222. return nil, &textproto.Error{code, msg}
  223. }
  224. return conn, nil
  225. }
  226. // parseListLine parses the various non-standard format returned by the LIST
  227. // FTP command.
  228. func parseListLine(line string) (*Entry, error) {
  229. fields := strings.Fields(line)
  230. if len(fields) < 9 {
  231. return nil, errors.New("Unsupported LIST line")
  232. }
  233. e := &Entry{}
  234. switch fields[0][0] {
  235. case '-':
  236. e.Type = EntryTypeFile
  237. case 'd':
  238. e.Type = EntryTypeFolder
  239. case 'l':
  240. e.Type = EntryTypeLink
  241. default:
  242. return nil, errors.New("Unknown entry type")
  243. }
  244. if e.Type == EntryTypeFile {
  245. size, err := strconv.ParseUint(fields[4], 10, 0)
  246. if err != nil {
  247. return nil, err
  248. }
  249. e.Size = size
  250. }
  251. var timeStr string
  252. if strings.Contains(fields[7], ":") { // this year
  253. thisYear, _, _ := time.Now().Date()
  254. timeStr = fields[6] + " " + fields[5] + " " + strconv.Itoa(thisYear)[2:4] + " " + fields[7] + " GMT"
  255. } else { // not this year
  256. timeStr = fields[6] + " " + fields[5] + " " + fields[7][2:4] + " " + "00:00" + " GMT"
  257. }
  258. t, err := time.Parse("_2 Jan 06 15:04 MST", timeStr)
  259. if err != nil {
  260. return nil, err
  261. }
  262. e.Time = t
  263. e.Name = strings.Join(fields[8:], " ")
  264. return e, nil
  265. }
  266. // NameList issues an NLST FTP command.
  267. func (c *ServerConn) NameList(path string) (entries []string, err error) {
  268. conn, err := c.cmdDataConn("NLST %s", path)
  269. if err != nil {
  270. return
  271. }
  272. r := &response{conn, c}
  273. defer r.Close()
  274. scanner := bufio.NewScanner(r)
  275. for scanner.Scan() {
  276. entries = append(entries, scanner.Text())
  277. }
  278. if err = scanner.Err(); err != nil {
  279. return entries, err
  280. }
  281. return
  282. }
  283. // List issues a LIST FTP command.
  284. func (c *ServerConn) List(path string) (entries []*Entry, err error) {
  285. conn, err := c.cmdDataConn("LIST %s", path)
  286. if err != nil {
  287. return
  288. }
  289. r := &response{conn, c}
  290. defer r.Close()
  291. bio := bufio.NewReader(r)
  292. for {
  293. line, e := bio.ReadString('\n')
  294. if e == io.EOF {
  295. break
  296. } else if e != nil {
  297. return nil, e
  298. }
  299. entry, err := parseListLine(line)
  300. if err == nil {
  301. entries = append(entries, entry)
  302. }
  303. }
  304. return
  305. }
  306. // ChangeDir issues a CWD FTP command, which changes the current directory to
  307. // the specified path.
  308. func (c *ServerConn) ChangeDir(path string) error {
  309. _, _, err := c.cmd(StatusRequestedFileActionOK, "CWD %s", path)
  310. return err
  311. }
  312. // ChangeDirToParent issues a CDUP FTP command, which changes the current
  313. // directory to the parent directory. This is similar to a call to ChangeDir
  314. // with a path set to "..".
  315. func (c *ServerConn) ChangeDirToParent() error {
  316. _, _, err := c.cmd(StatusRequestedFileActionOK, "CDUP")
  317. return err
  318. }
  319. // CurrentDir issues a PWD FTP command, which Returns the path of the current
  320. // directory.
  321. func (c *ServerConn) CurrentDir() (string, error) {
  322. _, msg, err := c.cmd(StatusPathCreated, "PWD")
  323. if err != nil {
  324. return "", err
  325. }
  326. start := strings.Index(msg, "\"")
  327. end := strings.LastIndex(msg, "\"")
  328. if start == -1 || end == -1 {
  329. return "", errors.New("Unsuported PWD response format")
  330. }
  331. return msg[start+1 : end], nil
  332. }
  333. // Retr issues a RETR FTP command to fetch the specified file from the remote
  334. // FTP server.
  335. //
  336. // The returned ReadCloser must be closed to cleanup the FTP data connection.
  337. func (c *ServerConn) Retr(path string) (io.ReadCloser, error) {
  338. conn, err := c.cmdDataConn("RETR %s", path)
  339. if err != nil {
  340. return nil, err
  341. }
  342. r := &response{conn, c}
  343. return r, nil
  344. }
  345. // Stor issues a STOR FTP command to store a file to the remote FTP server.
  346. // Stor creates the specified file with the content of the io.Reader.
  347. //
  348. // Hint: io.Pipe() can be used if an io.Writer is required.
  349. func (c *ServerConn) Stor(path string, r io.Reader) error {
  350. conn, err := c.cmdDataConn("STOR %s", path)
  351. if err != nil {
  352. return err
  353. }
  354. _, err = io.Copy(conn, r)
  355. conn.Close()
  356. if err != nil {
  357. return err
  358. }
  359. _, _, err = c.conn.ReadCodeLine(StatusClosingDataConnection)
  360. return err
  361. }
  362. // Rename renames a file on the remote FTP server.
  363. func (c *ServerConn) Rename(from, to string) error {
  364. _, _, err := c.cmd(StatusRequestFilePending, "RNFR %s", from)
  365. if err != nil {
  366. return err
  367. }
  368. _, _, err = c.cmd(StatusRequestedFileActionOK, "RNTO %s", to)
  369. return err
  370. }
  371. // Delete issues a DELE FTP command to delete the specified file from the
  372. // remote FTP server.
  373. func (c *ServerConn) Delete(path string) error {
  374. _, _, err := c.cmd(StatusRequestedFileActionOK, "DELE %s", path)
  375. return err
  376. }
  377. // MakeDir issues a MKD FTP command to create the specified directory on the
  378. // remote FTP server.
  379. func (c *ServerConn) MakeDir(path string) error {
  380. _, _, err := c.cmd(StatusPathCreated, "MKD %s", path)
  381. return err
  382. }
  383. // RemoveDir issues a RMD FTP command to remove the specified directory from
  384. // the remote FTP server.
  385. func (c *ServerConn) RemoveDir(path string) error {
  386. _, _, err := c.cmd(StatusRequestedFileActionOK, "RMD %s", path)
  387. return err
  388. }
  389. // NoOp issues a NOOP FTP command.
  390. // NOOP has no effects and is usually used to prevent the remote FTP server to
  391. // close the otherwise idle connection.
  392. func (c *ServerConn) NoOp() error {
  393. _, _, err := c.cmd(StatusCommandOK, "NOOP")
  394. return err
  395. }
  396. // Logout issues a REIN FTP command to logout the current user.
  397. func (c *ServerConn) Logout() error {
  398. _, _, err := c.cmd(StatusLoggedIn, "REIN")
  399. return err
  400. }
  401. // Quit issues a QUIT FTP command to properly close the connection from the
  402. // remote FTP server.
  403. func (c *ServerConn) Quit() error {
  404. c.conn.Cmd("QUIT")
  405. return c.conn.Close()
  406. }
  407. // Read implements the io.Reader interface on a FTP data connection.
  408. func (r *response) Read(buf []byte) (int, error) {
  409. n, err := r.conn.Read(buf)
  410. return n, err
  411. }
  412. // Close implements the io.Closer interface on a FTP data connection.
  413. func (r *response) Close() error {
  414. err := r.conn.Close()
  415. _, _, err2 := r.c.conn.ReadCodeLine(StatusClosingDataConnection)
  416. if err2 != nil {
  417. err = err2
  418. }
  419. return err
  420. }