ftp.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. // Package ftp implements a FTP client as described in RFC 959.
  2. //
  3. // A textproto.Error is returned for errors at the protocol level.
  4. package ftp
  5. import (
  6. "bufio"
  7. "errors"
  8. "io"
  9. "net"
  10. "net/textproto"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // EntryType describes the different types of an Entry.
  16. type EntryType int
  17. // The differents types of an Entry
  18. const (
  19. EntryTypeFile EntryType = iota
  20. EntryTypeFolder
  21. EntryTypeLink
  22. )
  23. // ServerConn represents the connection to a remote FTP server.
  24. // It should be protected from concurrent accesses.
  25. type ServerConn struct {
  26. // Do not use EPSV mode
  27. DisableEPSV bool
  28. // Timezone that the server is in
  29. Location *time.Location
  30. conn *textproto.Conn
  31. host string
  32. timeout time.Duration
  33. features map[string]string
  34. mlstSupported bool
  35. }
  36. // Entry describes a file and is returned by List().
  37. type Entry struct {
  38. Name string
  39. Type EntryType
  40. Size uint64
  41. Time time.Time
  42. }
  43. // Response represents a data-connection
  44. type Response struct {
  45. conn net.Conn
  46. c *ServerConn
  47. closed bool
  48. }
  49. // Connect is an alias to Dial, for backward compatibility
  50. func Connect(addr string) (*ServerConn, error) {
  51. return Dial(addr)
  52. }
  53. // Dial is like DialTimeout with no timeout
  54. func Dial(addr string) (*ServerConn, error) {
  55. return DialTimeout(addr, 0)
  56. }
  57. // DialTimeout initializes the connection to the specified ftp server address.
  58. //
  59. // It is generally followed by a call to Login() as most FTP commands require
  60. // an authenticated user.
  61. func DialTimeout(addr string, timeout time.Duration) (*ServerConn, error) {
  62. tconn, err := net.DialTimeout("tcp", addr, timeout)
  63. if err != nil {
  64. return nil, err
  65. }
  66. // Use the resolved IP address in case addr contains a domain name
  67. // If we use the domain name, we might not resolve to the same IP.
  68. remoteAddr := tconn.RemoteAddr().(*net.TCPAddr)
  69. conn := textproto.NewConn(tconn)
  70. c := &ServerConn{
  71. conn: conn,
  72. host: remoteAddr.IP.String(),
  73. timeout: timeout,
  74. features: make(map[string]string),
  75. Location: time.UTC,
  76. }
  77. _, _, err = c.conn.ReadResponse(StatusReady)
  78. if err != nil {
  79. c.Quit()
  80. return nil, err
  81. }
  82. err = c.feat()
  83. if err != nil {
  84. c.Quit()
  85. return nil, err
  86. }
  87. if _, mlstSupported := c.features["MLST"]; mlstSupported {
  88. c.mlstSupported = true
  89. }
  90. return c, nil
  91. }
  92. // Login authenticates the client with specified user and password.
  93. //
  94. // "anonymous"/"anonymous" is a common user/password scheme for FTP servers
  95. // that allows anonymous read-only accounts.
  96. func (c *ServerConn) Login(user, password string) error {
  97. code, message, err := c.cmd(-1, "USER %s", user)
  98. if err != nil {
  99. return err
  100. }
  101. switch code {
  102. case StatusLoggedIn:
  103. case StatusUserOK:
  104. _, _, err = c.cmd(StatusLoggedIn, "PASS %s", password)
  105. if err != nil {
  106. return err
  107. }
  108. default:
  109. return errors.New(message)
  110. }
  111. // Switch to binary mode
  112. if _, _, err = c.cmd(StatusCommandOK, "TYPE I"); err != nil {
  113. return err
  114. }
  115. // Switch to UTF-8
  116. err = c.setUTF8()
  117. return err
  118. }
  119. // feat issues a FEAT FTP command to list the additional commands supported by
  120. // the remote FTP server.
  121. // FEAT is described in RFC 2389
  122. func (c *ServerConn) feat() error {
  123. code, message, err := c.cmd(-1, "FEAT")
  124. if err != nil {
  125. return err
  126. }
  127. if code != StatusSystem {
  128. // The server does not support the FEAT command. This is not an
  129. // error: we consider that there is no additional feature.
  130. return nil
  131. }
  132. lines := strings.Split(message, "\n")
  133. for _, line := range lines {
  134. if !strings.HasPrefix(line, " ") {
  135. continue
  136. }
  137. line = strings.TrimSpace(line)
  138. featureElements := strings.SplitN(line, " ", 2)
  139. command := featureElements[0]
  140. var commandDesc string
  141. if len(featureElements) == 2 {
  142. commandDesc = featureElements[1]
  143. }
  144. c.features[command] = commandDesc
  145. }
  146. return nil
  147. }
  148. // setUTF8 issues an "OPTS UTF8 ON" command.
  149. func (c *ServerConn) setUTF8() error {
  150. if _, ok := c.features["UTF8"]; !ok {
  151. return nil
  152. }
  153. code, message, err := c.cmd(-1, "OPTS UTF8 ON")
  154. if err != nil {
  155. return err
  156. }
  157. // The ftpd "filezilla-server" has FEAT support for UTF8, but always returns
  158. // "202 UTF8 mode is always enabled. No need to send this command." when
  159. // trying to use it. That's OK
  160. if code == StatusCommandNotImplemented {
  161. return nil
  162. }
  163. if code != StatusCommandOK {
  164. return errors.New(message)
  165. }
  166. return nil
  167. }
  168. // epsv issues an "EPSV" command to get a port number for a data connection.
  169. func (c *ServerConn) epsv() (port int, err error) {
  170. _, line, err := c.cmd(StatusExtendedPassiveMode, "EPSV")
  171. if err != nil {
  172. return
  173. }
  174. start := strings.Index(line, "|||")
  175. end := strings.LastIndex(line, "|")
  176. if start == -1 || end == -1 {
  177. err = errors.New("Invalid EPSV response format")
  178. return
  179. }
  180. port, err = strconv.Atoi(line[start+3 : end])
  181. return
  182. }
  183. // pasv issues a "PASV" command to get a port number for a data connection.
  184. func (c *ServerConn) pasv() (host string, port int, err error) {
  185. _, line, err := c.cmd(StatusPassiveMode, "PASV")
  186. if err != nil {
  187. return
  188. }
  189. // PASV response format : 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2).
  190. start := strings.Index(line, "(")
  191. end := strings.LastIndex(line, ")")
  192. if start == -1 || end == -1 {
  193. err = errors.New("Invalid PASV response format")
  194. return
  195. }
  196. // We have to split the response string
  197. pasvData := strings.Split(line[start+1:end], ",")
  198. if len(pasvData) < 6 {
  199. err = errors.New("Invalid PASV response format")
  200. return
  201. }
  202. // Let's compute the port number
  203. portPart1, err1 := strconv.Atoi(pasvData[4])
  204. if err1 != nil {
  205. err = err1
  206. return
  207. }
  208. portPart2, err2 := strconv.Atoi(pasvData[5])
  209. if err2 != nil {
  210. err = err2
  211. return
  212. }
  213. // Recompose port
  214. port = portPart1*256 + portPart2
  215. // Make the IP address to connect to
  216. host = strings.Join(pasvData[0:4], ".")
  217. return
  218. }
  219. // getDataConnPort returns a host, port for a new data connection
  220. // it uses the best available method to do so
  221. func (c *ServerConn) getDataConnPort() (string, int, error) {
  222. if !c.DisableEPSV {
  223. if port, err := c.epsv(); err == nil {
  224. return c.host, port, nil
  225. }
  226. // if there is an error, disable EPSV for the next attempts
  227. c.DisableEPSV = true
  228. }
  229. return c.pasv()
  230. }
  231. // openDataConn creates a new FTP data connection.
  232. func (c *ServerConn) openDataConn() (net.Conn, error) {
  233. host, port, err := c.getDataConnPort()
  234. if err != nil {
  235. return nil, err
  236. }
  237. return net.DialTimeout("tcp", net.JoinHostPort(host, strconv.Itoa(port)), c.timeout)
  238. }
  239. // cmd is a helper function to execute a command and check for the expected FTP
  240. // return code
  241. func (c *ServerConn) cmd(expected int, format string, args ...interface{}) (int, string, error) {
  242. _, err := c.conn.Cmd(format, args...)
  243. if err != nil {
  244. return 0, "", err
  245. }
  246. return c.conn.ReadResponse(expected)
  247. }
  248. // cmdDataConnFrom executes a command which require a FTP data connection.
  249. // Issues a REST FTP command to specify the number of bytes to skip for the transfer.
  250. func (c *ServerConn) cmdDataConnFrom(offset uint64, format string, args ...interface{}) (net.Conn, error) {
  251. conn, err := c.openDataConn()
  252. if err != nil {
  253. return nil, err
  254. }
  255. if offset != 0 {
  256. _, _, err = c.cmd(StatusRequestFilePending, "REST %d", offset)
  257. if err != nil {
  258. conn.Close()
  259. return nil, err
  260. }
  261. }
  262. _, err = c.conn.Cmd(format, args...)
  263. if err != nil {
  264. conn.Close()
  265. return nil, err
  266. }
  267. code, msg, err := c.conn.ReadResponse(-1)
  268. if err != nil {
  269. conn.Close()
  270. return nil, err
  271. }
  272. if code != StatusAlreadyOpen && code != StatusAboutToSend {
  273. conn.Close()
  274. return nil, &textproto.Error{Code: code, Msg: msg}
  275. }
  276. return conn, nil
  277. }
  278. // NameList issues an NLST FTP command.
  279. func (c *ServerConn) NameList(path string) (entries []string, err error) {
  280. conn, err := c.cmdDataConnFrom(0, "NLST %s", path)
  281. if err != nil {
  282. return
  283. }
  284. r := &Response{conn: conn, c: c}
  285. defer r.Close()
  286. scanner := bufio.NewScanner(r)
  287. for scanner.Scan() {
  288. entries = append(entries, scanner.Text())
  289. }
  290. if err = scanner.Err(); err != nil {
  291. return entries, err
  292. }
  293. return
  294. }
  295. // List issues a LIST FTP command.
  296. func (c *ServerConn) List(path string) (entries []*Entry, err error) {
  297. var cmd string
  298. var parser parseFunc
  299. if c.mlstSupported {
  300. cmd = "MLSD"
  301. parser = parseRFC3659ListLine
  302. } else {
  303. cmd = "LIST"
  304. parser = parseListLine
  305. }
  306. conn, err := c.cmdDataConnFrom(0, "%s %s", cmd, path)
  307. if err != nil {
  308. return
  309. }
  310. r := &Response{conn: conn, c: c}
  311. defer r.Close()
  312. scanner := bufio.NewScanner(r)
  313. now := time.Now()
  314. for scanner.Scan() {
  315. entry, err := parser(scanner.Text(), now, c.Location)
  316. if err == nil {
  317. entries = append(entries, entry)
  318. }
  319. }
  320. if err := scanner.Err(); err != nil {
  321. return nil, err
  322. }
  323. return
  324. }
  325. // ChangeDir issues a CWD FTP command, which changes the current directory to
  326. // the specified path.
  327. func (c *ServerConn) ChangeDir(path string) error {
  328. _, _, err := c.cmd(StatusRequestedFileActionOK, "CWD %s", path)
  329. return err
  330. }
  331. // ChangeDirToParent issues a CDUP FTP command, which changes the current
  332. // directory to the parent directory. This is similar to a call to ChangeDir
  333. // with a path set to "..".
  334. func (c *ServerConn) ChangeDirToParent() error {
  335. _, _, err := c.cmd(StatusRequestedFileActionOK, "CDUP")
  336. return err
  337. }
  338. // CurrentDir issues a PWD FTP command, which Returns the path of the current
  339. // directory.
  340. func (c *ServerConn) CurrentDir() (string, error) {
  341. _, msg, err := c.cmd(StatusPathCreated, "PWD")
  342. if err != nil {
  343. return "", err
  344. }
  345. start := strings.Index(msg, "\"")
  346. end := strings.LastIndex(msg, "\"")
  347. if start == -1 || end == -1 {
  348. return "", errors.New("Unsuported PWD response format")
  349. }
  350. return msg[start+1 : end], nil
  351. }
  352. // FileSize issues a SIZE FTP command, which Returns the size of the file
  353. func (c *ServerConn) FileSize(path string) (int64, error) {
  354. _, msg, err := c.cmd(StatusFile, "SIZE %s", path)
  355. if err != nil {
  356. return 0, err
  357. }
  358. return strconv.ParseInt(msg, 10, 64)
  359. }
  360. // Retr issues a RETR FTP command to fetch the specified file from the remote
  361. // FTP server.
  362. //
  363. // The returned ReadCloser must be closed to cleanup the FTP data connection.
  364. func (c *ServerConn) Retr(path string) (*Response, error) {
  365. return c.RetrFrom(path, 0)
  366. }
  367. // RetrFrom issues a RETR FTP command to fetch the specified file from the remote
  368. // FTP server, the server will not send the offset first bytes of the file.
  369. //
  370. // The returned ReadCloser must be closed to cleanup the FTP data connection.
  371. func (c *ServerConn) RetrFrom(path string, offset uint64) (*Response, error) {
  372. conn, err := c.cmdDataConnFrom(offset, "RETR %s", path)
  373. if err != nil {
  374. return nil, err
  375. }
  376. return &Response{conn: conn, c: c}, nil
  377. }
  378. // Stor issues a STOR FTP command to store a file to the remote FTP server.
  379. // Stor creates the specified file with the content of the io.Reader.
  380. //
  381. // Hint: io.Pipe() can be used if an io.Writer is required.
  382. func (c *ServerConn) Stor(path string, r io.Reader) error {
  383. return c.StorFrom(path, r, 0)
  384. }
  385. // StorFrom issues a STOR FTP command to store a file to the remote FTP server.
  386. // Stor creates the specified file with the content of the io.Reader, writing
  387. // on the server will start at the given file offset.
  388. //
  389. // Hint: io.Pipe() can be used if an io.Writer is required.
  390. func (c *ServerConn) StorFrom(path string, r io.Reader, offset uint64) error {
  391. conn, err := c.cmdDataConnFrom(offset, "STOR %s", path)
  392. if err != nil {
  393. return err
  394. }
  395. _, err = io.Copy(conn, r)
  396. conn.Close()
  397. if err != nil {
  398. return err
  399. }
  400. _, _, err = c.conn.ReadResponse(StatusClosingDataConnection)
  401. return err
  402. }
  403. // Rename renames a file on the remote FTP server.
  404. func (c *ServerConn) Rename(from, to string) error {
  405. _, _, err := c.cmd(StatusRequestFilePending, "RNFR %s", from)
  406. if err != nil {
  407. return err
  408. }
  409. _, _, err = c.cmd(StatusRequestedFileActionOK, "RNTO %s", to)
  410. return err
  411. }
  412. // Delete issues a DELE FTP command to delete the specified file from the
  413. // remote FTP server.
  414. func (c *ServerConn) Delete(path string) error {
  415. _, _, err := c.cmd(StatusRequestedFileActionOK, "DELE %s", path)
  416. return err
  417. }
  418. // RemoveDirRecur deletes a non-empty folder recursively using
  419. // RemoveDir and Delete
  420. func (c *ServerConn) RemoveDirRecur(path string) error {
  421. var (
  422. err error
  423. currentDir string
  424. entries []*Entry
  425. )
  426. err = c.ChangeDir(path)
  427. if err != nil {
  428. return err
  429. }
  430. currentDir, err = c.CurrentDir()
  431. if err != nil {
  432. return err
  433. }
  434. entries, err = c.List(currentDir)
  435. if err != nil {
  436. return err
  437. }
  438. for _, entry := range entries {
  439. if entry.Name != ".." && entry.Name != "." {
  440. if entry.Type == EntryTypeFolder {
  441. err = c.RemoveDirRecur(currentDir + "/" + entry.Name)
  442. if err != nil {
  443. return err
  444. }
  445. } else {
  446. err = c.Delete(entry.Name)
  447. if err != nil {
  448. return err
  449. }
  450. }
  451. }
  452. }
  453. err = c.ChangeDirToParent()
  454. if err != nil {
  455. return err
  456. }
  457. err = c.RemoveDir(currentDir)
  458. return err
  459. }
  460. // MakeDir issues a MKD FTP command to create the specified directory on the
  461. // remote FTP server.
  462. func (c *ServerConn) MakeDir(path string) error {
  463. _, _, err := c.cmd(StatusPathCreated, "MKD %s", path)
  464. return err
  465. }
  466. // RemoveDir issues a RMD FTP command to remove the specified directory from
  467. // the remote FTP server.
  468. func (c *ServerConn) RemoveDir(path string) error {
  469. _, _, err := c.cmd(StatusRequestedFileActionOK, "RMD %s", path)
  470. return err
  471. }
  472. // NoOp issues a NOOP FTP command.
  473. // NOOP has no effects and is usually used to prevent the remote FTP server to
  474. // close the otherwise idle connection.
  475. func (c *ServerConn) NoOp() error {
  476. _, _, err := c.cmd(StatusCommandOK, "NOOP")
  477. return err
  478. }
  479. // Logout issues a REIN FTP command to logout the current user.
  480. func (c *ServerConn) Logout() error {
  481. _, _, err := c.cmd(StatusReady, "REIN")
  482. return err
  483. }
  484. // Quit issues a QUIT FTP command to properly close the connection from the
  485. // remote FTP server.
  486. func (c *ServerConn) Quit() error {
  487. c.conn.Cmd("QUIT")
  488. return c.conn.Close()
  489. }
  490. // Read implements the io.Reader interface on a FTP data connection.
  491. func (r *Response) Read(buf []byte) (int, error) {
  492. return r.conn.Read(buf)
  493. }
  494. // Close implements the io.Closer interface on a FTP data connection.
  495. // After the first call, Close will do nothing and return nil.
  496. func (r *Response) Close() error {
  497. if r.closed {
  498. return nil
  499. }
  500. err := r.conn.Close()
  501. _, _, err2 := r.c.conn.ReadResponse(StatusClosingDataConnection)
  502. if err2 != nil {
  503. err = err2
  504. }
  505. r.closed = true
  506. return err
  507. }
  508. // SetDeadline sets the deadlines associated with the connection.
  509. func (r *Response) SetDeadline(t time.Time) error {
  510. return r.conn.SetDeadline(t)
  511. }