7 | 7 |
"context"
|
8 | 8 |
"crypto/tls"
|
9 | 9 |
"errors"
|
|
10 |
"fmt"
|
10 | 11 |
"io"
|
11 | 12 |
"net"
|
12 | 13 |
"net/textproto"
|
|
37 | 38 |
features map[string]string
|
38 | 39 |
skipEPSV bool
|
39 | 40 |
mlstSupported bool
|
|
41 |
usePRET bool
|
40 | 42 |
}
|
41 | 43 |
|
42 | 44 |
// DialOption represents an option to start a new connection with Dial
|
|
53 | 55 |
conn net.Conn
|
54 | 56 |
disableEPSV bool
|
55 | 57 |
disableUTF8 bool
|
|
58 |
disableMLSD bool
|
56 | 59 |
location *time.Location
|
57 | 60 |
debugOutput io.Writer
|
58 | 61 |
dialFunc func(network, address string) (net.Conn, error)
|
|
121 | 124 |
|
122 | 125 |
_, _, err := c.conn.ReadResponse(StatusReady)
|
123 | 126 |
if err != nil {
|
124 | |
c.Quit()
|
|
127 |
_ = c.Quit()
|
125 | 128 |
return nil, err
|
126 | 129 |
}
|
127 | 130 |
|
|
134 | 137 |
c.conn = textproto.NewConn(do.wrapConn(tconn))
|
135 | 138 |
}
|
136 | 139 |
|
137 | |
err = c.feat()
|
138 | |
if err != nil {
|
139 | |
c.Quit()
|
140 | |
return nil, err
|
141 | |
}
|
142 | |
|
143 | |
if _, mlstSupported := c.features["MLST"]; mlstSupported {
|
144 | |
c.mlstSupported = true
|
145 | |
}
|
146 | |
|
147 | 140 |
return c, nil
|
148 | 141 |
}
|
149 | 142 |
|
|
180 | 173 |
func DialWithDisabledUTF8(disabled bool) DialOption {
|
181 | 174 |
return DialOption{func(do *dialOptions) {
|
182 | 175 |
do.disableUTF8 = disabled
|
|
176 |
}}
|
|
177 |
}
|
|
178 |
|
|
179 |
// DialWithDisabledMLSD returns a DialOption that configures the ServerConn with MLSD option disabled
|
|
180 |
//
|
|
181 |
// This is useful for servers which advertise MLSD (eg some versions
|
|
182 |
// of Serv-U) but don't support it properly.
|
|
183 |
func DialWithDisabledMLSD(disabled bool) DialOption {
|
|
184 |
return DialOption{func(do *dialOptions) {
|
|
185 |
do.disableMLSD = disabled
|
183 | 186 |
}}
|
184 | 187 |
}
|
185 | 188 |
|
|
281 | 284 |
return errors.New(message)
|
282 | 285 |
}
|
283 | 286 |
|
|
287 |
// Probe features
|
|
288 |
err = c.feat()
|
|
289 |
if err != nil {
|
|
290 |
return err
|
|
291 |
}
|
|
292 |
if _, mlstSupported := c.features["MLST"]; mlstSupported && !c.options.disableMLSD {
|
|
293 |
c.mlstSupported = true
|
|
294 |
}
|
|
295 |
if _, usePRET := c.features["PRET"]; usePRET {
|
|
296 |
c.usePRET = true
|
|
297 |
}
|
|
298 |
|
284 | 299 |
// Switch to binary mode
|
285 | 300 |
if _, _, err = c.cmd(StatusCommandOK, "TYPE I"); err != nil {
|
286 | 301 |
return err
|
|
293 | 308 |
|
294 | 309 |
// If using implicit TLS, make data connections also use TLS
|
295 | 310 |
if c.options.tlsConfig != nil {
|
296 | |
c.cmd(StatusCommandOK, "PBSZ 0")
|
297 | |
c.cmd(StatusCommandOK, "PROT P")
|
|
311 |
if _, _, err = c.cmd(StatusCommandOK, "PBSZ 0"); err != nil {
|
|
312 |
return err
|
|
313 |
}
|
|
314 |
if _, _, err = c.cmd(StatusCommandOK, "PROT P"); err != nil {
|
|
315 |
return err
|
|
316 |
}
|
298 | 317 |
}
|
299 | 318 |
|
300 | 319 |
return err
|
|
377 | 396 |
func (c *ServerConn) epsv() (port int, err error) {
|
378 | 397 |
_, line, err := c.cmd(StatusExtendedPassiveMode, "EPSV")
|
379 | 398 |
if err != nil {
|
380 | |
return
|
|
399 |
return 0, err
|
381 | 400 |
}
|
382 | 401 |
|
383 | 402 |
start := strings.Index(line, "|||")
|
384 | 403 |
end := strings.LastIndex(line, "|")
|
385 | 404 |
if start == -1 || end == -1 {
|
386 | |
err = errors.New("invalid EPSV response format")
|
387 | |
return
|
|
405 |
return 0, errors.New("invalid EPSV response format")
|
388 | 406 |
}
|
389 | 407 |
port, err = strconv.Atoi(line[start+3 : end])
|
390 | |
return
|
|
408 |
return port, err
|
391 | 409 |
}
|
392 | 410 |
|
393 | 411 |
// pasv issues a "PASV" command to get a port number for a data connection.
|
394 | 412 |
func (c *ServerConn) pasv() (host string, port int, err error) {
|
395 | 413 |
_, line, err := c.cmd(StatusPassiveMode, "PASV")
|
396 | 414 |
if err != nil {
|
397 | |
return
|
|
415 |
return "", 0, err
|
398 | 416 |
}
|
399 | 417 |
|
400 | 418 |
// PASV response format : 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2).
|
401 | 419 |
start := strings.Index(line, "(")
|
402 | 420 |
end := strings.LastIndex(line, ")")
|
403 | 421 |
if start == -1 || end == -1 {
|
404 | |
err = errors.New("invalid PASV response format")
|
405 | |
return
|
|
422 |
return "", 0, errors.New("invalid PASV response format")
|
406 | 423 |
}
|
407 | 424 |
|
408 | 425 |
// We have to split the response string
|
409 | 426 |
pasvData := strings.Split(line[start+1:end], ",")
|
410 | 427 |
|
411 | 428 |
if len(pasvData) < 6 {
|
412 | |
err = errors.New("invalid PASV response format")
|
413 | |
return
|
|
429 |
return "", 0, errors.New("invalid PASV response format")
|
414 | 430 |
}
|
415 | 431 |
|
416 | 432 |
// Let's compute the port number
|
417 | |
portPart1, err1 := strconv.Atoi(pasvData[4])
|
418 | |
if err1 != nil {
|
419 | |
err = err1
|
420 | |
return
|
421 | |
}
|
422 | |
|
423 | |
portPart2, err2 := strconv.Atoi(pasvData[5])
|
424 | |
if err2 != nil {
|
425 | |
err = err2
|
426 | |
return
|
|
433 |
portPart1, err := strconv.Atoi(pasvData[4])
|
|
434 |
if err != nil {
|
|
435 |
return "", 0, err
|
|
436 |
}
|
|
437 |
|
|
438 |
portPart2, err := strconv.Atoi(pasvData[5])
|
|
439 |
if err != nil {
|
|
440 |
return "", 0, err
|
427 | 441 |
}
|
428 | 442 |
|
429 | 443 |
// Recompose port
|
|
431 | 445 |
|
432 | 446 |
// Make the IP address to connect to
|
433 | 447 |
host = strings.Join(pasvData[0:4], ".")
|
434 | |
return
|
|
448 |
return host, port, nil
|
435 | 449 |
}
|
436 | 450 |
|
437 | 451 |
// getDataConnPort returns a host, port for a new data connection
|
|
486 | 500 |
// cmdDataConnFrom executes a command which require a FTP data connection.
|
487 | 501 |
// Issues a REST FTP command to specify the number of bytes to skip for the transfer.
|
488 | 502 |
func (c *ServerConn) cmdDataConnFrom(offset uint64, format string, args ...interface{}) (net.Conn, error) {
|
|
503 |
// If server requires PRET send the PRET command to warm it up
|
|
504 |
// See: https://tools.ietf.org/html/draft-dd-pret-00
|
|
505 |
if c.usePRET {
|
|
506 |
_, _, err := c.cmd(-1, "PRET "+format, args...)
|
|
507 |
if err != nil {
|
|
508 |
return nil, err
|
|
509 |
}
|
|
510 |
}
|
|
511 |
|
489 | 512 |
conn, err := c.openDataConn()
|
490 | 513 |
if err != nil {
|
491 | 514 |
return nil, err
|
492 | 515 |
}
|
493 | 516 |
|
494 | 517 |
if offset != 0 {
|
495 | |
_, _, err := c.cmd(StatusRequestFilePending, "REST %d", offset)
|
|
518 |
_, _, err = c.cmd(StatusRequestFilePending, "REST %d", offset)
|
496 | 519 |
if err != nil {
|
497 | |
conn.Close()
|
|
520 |
_ = conn.Close()
|
498 | 521 |
return nil, err
|
499 | 522 |
}
|
500 | 523 |
}
|
501 | 524 |
|
502 | 525 |
_, err = c.conn.Cmd(format, args...)
|
503 | 526 |
if err != nil {
|
504 | |
conn.Close()
|
|
527 |
_ = conn.Close()
|
505 | 528 |
return nil, err
|
506 | 529 |
}
|
507 | 530 |
|
508 | 531 |
code, msg, err := c.conn.ReadResponse(-1)
|
509 | 532 |
if err != nil {
|
510 | |
conn.Close()
|
|
533 |
_ = conn.Close()
|
511 | 534 |
return nil, err
|
512 | 535 |
}
|
513 | 536 |
if code != StatusAlreadyOpen && code != StatusAboutToSend {
|
514 | |
conn.Close()
|
|
537 |
_ = conn.Close()
|
515 | 538 |
return nil, &textproto.Error{Code: code, Msg: msg}
|
516 | 539 |
}
|
517 | 540 |
|
|
520 | 543 |
|
521 | 544 |
// NameList issues an NLST FTP command.
|
522 | 545 |
func (c *ServerConn) NameList(path string) (entries []string, err error) {
|
523 | |
conn, err := c.cmdDataConnFrom(0, "NLST %s", path)
|
524 | |
if err != nil {
|
525 | |
return
|
|
546 |
space := " "
|
|
547 |
if path == "" {
|
|
548 |
space = ""
|
|
549 |
}
|
|
550 |
conn, err := c.cmdDataConnFrom(0, "NLST%s%s", space, path)
|
|
551 |
if err != nil {
|
|
552 |
return nil, err
|
526 | 553 |
}
|
527 | 554 |
|
528 | 555 |
r := &Response{conn: conn, c: c}
|
529 | |
defer r.Close()
|
|
556 |
defer func() {
|
|
557 |
errClose := r.Close()
|
|
558 |
if err == nil {
|
|
559 |
err = errClose
|
|
560 |
}
|
|
561 |
}()
|
530 | 562 |
|
531 | 563 |
scanner := bufio.NewScanner(r)
|
532 | 564 |
for scanner.Scan() {
|
533 | 565 |
entries = append(entries, scanner.Text())
|
534 | 566 |
}
|
535 | |
if err = scanner.Err(); err != nil {
|
536 | |
return entries, err
|
537 | |
}
|
538 | |
return
|
|
567 |
|
|
568 |
err = scanner.Err()
|
|
569 |
return entries, err
|
539 | 570 |
}
|
540 | 571 |
|
541 | 572 |
// List issues a LIST FTP command.
|
|
551 | 582 |
parser = parseListLine
|
552 | 583 |
}
|
553 | 584 |
|
554 | |
conn, err := c.cmdDataConnFrom(0, "%s %s", cmd, path)
|
555 | |
if err != nil {
|
556 | |
return
|
|
585 |
space := " "
|
|
586 |
if path == "" {
|
|
587 |
space = ""
|
|
588 |
}
|
|
589 |
conn, err := c.cmdDataConnFrom(0, "%s%s%s", cmd, space, path)
|
|
590 |
if err != nil {
|
|
591 |
return nil, err
|
557 | 592 |
}
|
558 | 593 |
|
559 | 594 |
r := &Response{conn: conn, c: c}
|
560 | |
defer r.Close()
|
|
595 |
defer func() {
|
|
596 |
errClose := r.Close()
|
|
597 |
if err == nil {
|
|
598 |
err = errClose
|
|
599 |
}
|
|
600 |
}()
|
561 | 601 |
|
562 | 602 |
scanner := bufio.NewScanner(r)
|
563 | 603 |
now := time.Now()
|
564 | 604 |
for scanner.Scan() {
|
565 | |
entry, err := parser(scanner.Text(), now, c.options.location)
|
566 | |
if err == nil {
|
|
605 |
entry, errParse := parser(scanner.Text(), now, c.options.location)
|
|
606 |
if errParse == nil {
|
567 | 607 |
entries = append(entries, entry)
|
568 | 608 |
}
|
569 | 609 |
}
|
570 | |
if err := scanner.Err(); err != nil {
|
571 | |
return nil, err
|
572 | |
}
|
573 | |
return
|
|
610 |
|
|
611 |
err = scanner.Err()
|
|
612 |
return entries, err
|
574 | 613 |
}
|
575 | 614 |
|
576 | 615 |
// ChangeDir issues a CWD FTP command, which changes the current directory to
|
|
662 | 701 |
// the response and we cannot use the connection to send other commands.
|
663 | 702 |
// So we don't check io.Copy error and we return the error from
|
664 | 703 |
// ReadResponse so the user can see the real error
|
665 | |
io.Copy(conn, r)
|
666 | |
conn.Close()
|
667 | |
|
668 | |
_, _, err = c.conn.ReadResponse(StatusClosingDataConnection)
|
|
704 |
var n int64
|
|
705 |
n, err = io.Copy(conn, r)
|
|
706 |
|
|
707 |
// If we wrote no bytes but got no error, make sure we call
|
|
708 |
// tls.Handshake on the connection as it won't get called
|
|
709 |
// unless Write() is called.
|
|
710 |
//
|
|
711 |
// ProFTP doesn't like this and returns "Unable to build data
|
|
712 |
// connection: Operation not permitted" when trying to upload
|
|
713 |
// an empty file without this.
|
|
714 |
if n == 0 && err == nil {
|
|
715 |
if do, ok := conn.(interface{ Handshake() error }); ok {
|
|
716 |
err = do.Handshake()
|
|
717 |
}
|
|
718 |
}
|
|
719 |
|
|
720 |
// Use io.Copy or Handshake error in preference to this one
|
|
721 |
closeErr := conn.Close()
|
|
722 |
if err == nil {
|
|
723 |
err = closeErr
|
|
724 |
}
|
|
725 |
|
|
726 |
// Read the response and use this error in preference to
|
|
727 |
// previous errors
|
|
728 |
_, _, respErr := c.conn.ReadResponse(StatusClosingDataConnection)
|
|
729 |
if respErr != nil {
|
|
730 |
err = respErr
|
|
731 |
}
|
669 | 732 |
return err
|
670 | 733 |
}
|
671 | 734 |
|
|
681 | 744 |
}
|
682 | 745 |
|
683 | 746 |
// see the comment for StorFrom above
|
684 | |
io.Copy(conn, r)
|
685 | |
conn.Close()
|
686 | |
|
687 | |
_, _, err = c.conn.ReadResponse(StatusClosingDataConnection)
|
|
747 |
_, err = io.Copy(conn, r)
|
|
748 |
errClose := conn.Close()
|
|
749 |
|
|
750 |
_, _, respErr := c.conn.ReadResponse(StatusClosingDataConnection)
|
|
751 |
if respErr != nil {
|
|
752 |
err = respErr
|
|
753 |
}
|
|
754 |
|
|
755 |
if err == nil {
|
|
756 |
err = errClose
|
|
757 |
}
|
|
758 |
|
688 | 759 |
return err
|
689 | 760 |
}
|
690 | 761 |
|
|
792 | 863 |
// Quit issues a QUIT FTP command to properly close the connection from the
|
793 | 864 |
// remote FTP server.
|
794 | 865 |
func (c *ServerConn) Quit() error {
|
795 | |
c.conn.Cmd("QUIT")
|
796 | |
return c.conn.Close()
|
|
866 |
_, errQuit := c.conn.Cmd("QUIT")
|
|
867 |
err := c.conn.Close()
|
|
868 |
|
|
869 |
if errQuit != nil {
|
|
870 |
if err != nil {
|
|
871 |
return fmt.Errorf("error while quitting: %s: %w", errQuit, err)
|
|
872 |
}
|
|
873 |
return errQuit
|
|
874 |
}
|
|
875 |
|
|
876 |
return err
|
797 | 877 |
}
|
798 | 878 |
|
799 | 879 |
// Read implements the io.Reader interface on a FTP data connection.
|