-
Notifications
You must be signed in to change notification settings - Fork 359
/
ftp.go
1160 lines (987 loc) · 31.3 KB
/
ftp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package ftp implements a FTP client as described in RFC 959.
//
// A textproto.Error is returned for errors at the protocol level.
package ftp
import (
"bufio"
"context"
"crypto/tls"
"errors"
"io"
"net"
"net/textproto"
"strconv"
"strings"
"time"
"github.com/hashicorp/go-multierror"
)
const (
// 30 seconds was chosen as it's the
// same duration as http.DefaultTransport's timeout.
DefaultDialTimeout = 30 * time.Second
)
// EntryType describes the different types of an Entry.
type EntryType int
// The differents types of an Entry
const (
EntryTypeFile EntryType = iota
EntryTypeFolder
EntryTypeLink
)
// TransferType denotes the formats for transferring Entries.
type TransferType string
// The different transfer types
const (
TransferTypeBinary = TransferType("I")
TransferTypeASCII = TransferType("A")
)
// Time format used by the MDTM and MFMT commands
const timeFormat = "20060102150405"
// ServerConn represents the connection to a remote FTP server.
// A single connection only supports one in-flight data connection.
// It is not safe to be called concurrently.
type ServerConn struct {
options *dialOptions
conn *textproto.Conn // connection wrapper for text protocol
netConn net.Conn // underlying network connection
host string
// Server capabilities discovered at runtime
features map[string]string
skipEPSV bool
mlstSupported bool
mfmtSupported bool
mdtmSupported bool
mdtmCanWrite bool
usePRET bool
}
// DialOption represents an option to start a new connection with Dial
type DialOption struct {
setup func(do *dialOptions)
}
// dialOptions contains all the options set by DialOption.setup
type dialOptions struct {
context context.Context
dialer net.Dialer
tlsConfig *tls.Config
explicitTLS bool
disableEPSV bool
disableUTF8 bool
disableMLSD bool
writingMDTM bool
forceListHidden bool
location *time.Location
debugOutput io.Writer
dialFunc func(network, address string) (net.Conn, error)
shutTimeout time.Duration // time to wait for data connection closing status
}
// Entry describes a file and is returned by List().
type Entry struct {
Name string
Target string // target of symbolic link
Type EntryType
Size uint64
Time time.Time
}
// Response represents a data-connection
type Response struct {
conn net.Conn
c *ServerConn
closed bool
}
// Dial connects to the specified address with optional options
func Dial(addr string, options ...DialOption) (*ServerConn, error) {
do := &dialOptions{}
for _, option := range options {
option.setup(do)
}
if do.location == nil {
do.location = time.UTC
}
dialFunc := do.dialFunc
if dialFunc == nil {
ctx := do.context
if ctx == nil {
ctx = context.Background()
}
if _, ok := ctx.Deadline(); !ok {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, DefaultDialTimeout)
defer cancel()
}
if do.tlsConfig != nil && !do.explicitTLS {
dialFunc = func(network, address string) (net.Conn, error) {
tlsDialer := &tls.Dialer{
NetDialer: &do.dialer,
Config: do.tlsConfig,
}
return tlsDialer.DialContext(ctx, network, addr)
}
} else {
dialFunc = func(network, address string) (net.Conn, error) {
return do.dialer.DialContext(ctx, network, addr)
}
}
}
tconn, err := dialFunc("tcp", addr)
if err != nil {
return nil, err
}
// Use the resolved IP address in case addr contains a domain name
// If we use the domain name, we might not resolve to the same IP.
remoteAddr := tconn.RemoteAddr().(*net.TCPAddr)
c := &ServerConn{
options: do,
features: make(map[string]string),
conn: textproto.NewConn(do.wrapConn(tconn)),
netConn: tconn,
host: remoteAddr.IP.String(),
}
_, _, err = c.conn.ReadResponse(StatusReady)
if err != nil {
_ = c.Quit()
return nil, err
}
if do.explicitTLS {
if err := c.authTLS(); err != nil {
_ = c.Quit()
return nil, err
}
tconn = tls.Client(tconn, do.tlsConfig)
c.conn = textproto.NewConn(do.wrapConn(tconn))
}
return c, nil
}
// DialWithTimeout returns a DialOption that configures the ServerConn with specified timeout
func DialWithTimeout(timeout time.Duration) DialOption {
return DialOption{func(do *dialOptions) {
do.dialer.Timeout = timeout
}}
}
// DialWithShutTimeout returns a DialOption that configures the ServerConn with
// maximum time to wait for the data closing status on control connection
// and nudging the control connection deadline before reading status.
func DialWithShutTimeout(shutTimeout time.Duration) DialOption {
return DialOption{func(do *dialOptions) {
do.shutTimeout = shutTimeout
}}
}
// DialWithDialer returns a DialOption that configures the ServerConn with specified net.Dialer
func DialWithDialer(dialer net.Dialer) DialOption {
return DialOption{func(do *dialOptions) {
do.dialer = dialer
}}
}
// DialWithNetConn returns a DialOption that configures the ServerConn with the underlying net.Conn
//
// Deprecated: Use [DialWithDialFunc] instead
func DialWithNetConn(conn net.Conn) DialOption {
return DialWithDialFunc(func(network, address string) (net.Conn, error) {
return conn, nil
})
}
// DialWithDisabledEPSV returns a DialOption that configures the ServerConn with EPSV disabled
// Note that EPSV is only used when advertised in the server features.
func DialWithDisabledEPSV(disabled bool) DialOption {
return DialOption{func(do *dialOptions) {
do.disableEPSV = disabled
}}
}
// DialWithDisabledUTF8 returns a DialOption that configures the ServerConn with UTF8 option disabled
func DialWithDisabledUTF8(disabled bool) DialOption {
return DialOption{func(do *dialOptions) {
do.disableUTF8 = disabled
}}
}
// DialWithDisabledMLSD returns a DialOption that configures the ServerConn with MLSD option disabled
//
// This is useful for servers which advertise MLSD (eg some versions
// of Serv-U) but don't support it properly.
func DialWithDisabledMLSD(disabled bool) DialOption {
return DialOption{func(do *dialOptions) {
do.disableMLSD = disabled
}}
}
// DialWithWritingMDTM returns a DialOption making ServerConn use MDTM to set file time
//
// This option addresses a quirk in the VsFtpd server which doesn't support
// the MFMT command for setting file time like other servers but by default
// uses the MDTM command with non-standard arguments for that.
// See "mdtm_write" in https://security.appspot.com/vsftpd/vsftpd_conf.html
func DialWithWritingMDTM(enabled bool) DialOption {
return DialOption{func(do *dialOptions) {
do.writingMDTM = enabled
}}
}
// DialWithForceListHidden returns a DialOption making ServerConn use LIST -a to include hidden files and folders in directory listings
//
// This is useful for servers that do not do this by default, but it forces the use of the LIST command
// even if the server supports MLST.
func DialWithForceListHidden(enabled bool) DialOption {
return DialOption{func(do *dialOptions) {
do.forceListHidden = enabled
}}
}
// DialWithLocation returns a DialOption that configures the ServerConn with specified time.Location
// The location is used to parse the dates sent by the server which are in server's timezone
func DialWithLocation(location *time.Location) DialOption {
return DialOption{func(do *dialOptions) {
do.location = location
}}
}
// DialWithContext returns a DialOption that configures the ServerConn with specified context
// The context will be used for the initial connection setup
func DialWithContext(ctx context.Context) DialOption {
return DialOption{func(do *dialOptions) {
do.context = ctx
}}
}
// DialWithTLS returns a DialOption that configures the ServerConn with specified TLS config
//
// If called together with the DialWithDialFunc option, the DialWithDialFunc function
// will be used when dialing new connections but regardless of the function,
// the connection will be treated as a TLS connection.
func DialWithTLS(tlsConfig *tls.Config) DialOption {
return DialOption{func(do *dialOptions) {
do.tlsConfig = tlsConfig
}}
}
// DialWithExplicitTLS returns a DialOption that configures the ServerConn to be upgraded to TLS
// See DialWithTLS for general TLS documentation
func DialWithExplicitTLS(tlsConfig *tls.Config) DialOption {
return DialOption{func(do *dialOptions) {
do.explicitTLS = true
do.tlsConfig = tlsConfig
}}
}
// DialWithDebugOutput returns a DialOption that configures the ServerConn to write to the Writer
// everything it reads from the server
func DialWithDebugOutput(w io.Writer) DialOption {
return DialOption{func(do *dialOptions) {
do.debugOutput = w
}}
}
// DialWithDialFunc returns a DialOption that configures the ServerConn to use the
// specified function to establish both control and data connections
//
// If used together with the DialWithNetConn option, the DialWithNetConn
// takes precedence for the control connection, while data connections will
// be established using function specified with the DialWithDialFunc option
func DialWithDialFunc(f func(network, address string) (net.Conn, error)) DialOption {
return DialOption{func(do *dialOptions) {
do.dialFunc = f
}}
}
func (o *dialOptions) wrapConn(netConn net.Conn) io.ReadWriteCloser {
if o.debugOutput == nil {
return netConn
}
return newDebugWrapper(netConn, o.debugOutput)
}
func (o *dialOptions) wrapStream(rd io.ReadCloser) io.ReadCloser {
if o.debugOutput == nil {
return rd
}
return newStreamDebugWrapper(rd, o.debugOutput)
}
// Connect is an alias to Dial, for backward compatibility
//
// Deprecated: Use [Dial] instead
func Connect(addr string) (*ServerConn, error) {
return Dial(addr)
}
// DialTimeout initializes the connection to the specified ftp server address.
//
// Deprecated: Use [Dial] with [DialWithTimeout] option instead
func DialTimeout(addr string, timeout time.Duration) (*ServerConn, error) {
return Dial(addr, DialWithTimeout(timeout))
}
// Login authenticates the client with specified user and password.
//
// "anonymous"/"anonymous" is a common user/password scheme for FTP servers
// that allows anonymous read-only accounts.
func (c *ServerConn) Login(user, password string) error {
code, message, err := c.cmd(-1, "USER %s", user)
if err != nil {
return err
}
switch code {
case StatusLoggedIn:
case StatusUserOK:
_, _, err = c.cmd(StatusLoggedIn, "PASS %s", password)
if err != nil {
return err
}
default:
return errors.New(message)
}
// Probe features
err = c.feat()
if err != nil {
return err
}
if _, mlstSupported := c.features["MLST"]; mlstSupported && !c.options.disableMLSD {
c.mlstSupported = true
}
_, c.usePRET = c.features["PRET"]
_, c.mfmtSupported = c.features["MFMT"]
_, c.mdtmSupported = c.features["MDTM"]
c.mdtmCanWrite = c.mdtmSupported && c.options.writingMDTM
// Switch to binary mode
if err = c.Type(TransferTypeBinary); err != nil {
return err
}
// Switch to UTF-8
if !c.options.disableUTF8 {
err = c.setUTF8()
}
// If using implicit TLS, make data connections also use TLS
if c.options.tlsConfig != nil {
if _, _, err = c.cmd(StatusCommandOK, "PBSZ 0"); err != nil {
return err
}
if _, _, err = c.cmd(StatusCommandOK, "PROT P"); err != nil {
return err
}
}
return err
}
// authTLS upgrades the connection to use TLS
func (c *ServerConn) authTLS() error {
_, _, err := c.cmd(StatusAuthOK, "AUTH TLS")
return err
}
// feat issues a FEAT FTP command to list the additional commands supported by
// the remote FTP server.
// FEAT is described in RFC 2389
func (c *ServerConn) feat() error {
code, message, err := c.cmd(-1, "FEAT")
if err != nil {
return err
}
if code != StatusSystem {
// The server does not support the FEAT command. This is not an
// error: we consider that there is no additional feature.
return nil
}
lines := strings.Split(message, "\n")
for _, line := range lines {
if !strings.HasPrefix(line, " ") {
continue
}
line = strings.TrimSpace(line)
featureElements := strings.SplitN(line, " ", 2)
command := featureElements[0]
var commandDesc string
if len(featureElements) == 2 {
commandDesc = featureElements[1]
}
c.features[command] = commandDesc
}
return nil
}
// setUTF8 issues an "OPTS UTF8 ON" command.
func (c *ServerConn) setUTF8() error {
if _, ok := c.features["UTF8"]; !ok {
return nil
}
code, message, err := c.cmd(-1, "OPTS UTF8 ON")
if err != nil {
return err
}
// Workaround for FTP servers, that does not support this option.
if code == StatusBadArguments || code == StatusNotImplementedParameter {
return nil
}
// The ftpd "filezilla-server" has FEAT support for UTF8, but always returns
// "202 UTF8 mode is always enabled. No need to send this command." when
// trying to use it. That's OK
if code == StatusCommandNotImplemented {
return nil
}
if code != StatusCommandOK {
return errors.New(message)
}
return nil
}
// epsv issues an "EPSV" command to get a port number for a data connection.
func (c *ServerConn) epsv() (port int, err error) {
_, line, err := c.cmd(StatusExtendedPassiveMode, "EPSV")
if err != nil {
return 0, err
}
start := strings.Index(line, "|||")
end := strings.LastIndex(line, "|")
if start == -1 || end == -1 {
return 0, errors.New("invalid EPSV response format")
}
port, err = strconv.Atoi(line[start+3 : end])
return port, err
}
// pasv issues a "PASV" command to get a port number for a data connection.
func (c *ServerConn) pasv() (host string, port int, err error) {
_, line, err := c.cmd(StatusPassiveMode, "PASV")
if err != nil {
return "", 0, err
}
// PASV response format : 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2).
start := strings.Index(line, "(")
end := strings.LastIndex(line, ")")
if start == -1 || end == -1 {
return "", 0, errors.New("invalid PASV response format")
}
// We have to split the response string
pasvData := strings.Split(line[start+1:end], ",")
if len(pasvData) < 6 {
return "", 0, errors.New("invalid PASV response format")
}
// Let's compute the port number
portPart1, err := strconv.Atoi(pasvData[4])
if err != nil {
return "", 0, err
}
portPart2, err := strconv.Atoi(pasvData[5])
if err != nil {
return "", 0, err
}
// Recompose port
port = portPart1*256 + portPart2
// Make the IP address to connect to
host = strings.Join(pasvData[0:4], ".")
if c.host != host {
if cmdIP := net.ParseIP(c.host); cmdIP != nil {
if dataIP := net.ParseIP(host); dataIP != nil {
if isBogusDataIP(cmdIP, dataIP) {
return c.host, port, nil
}
}
}
}
return host, port, nil
}
func isBogusDataIP(cmdIP, dataIP net.IP) bool {
// Logic stolen from lftp (https://github.com/lavv17/lftp/blob/d67fc14d085849a6b0418bb3e912fea2e94c18d1/src/ftpclass.cc#L769)
return dataIP.IsMulticast() ||
cmdIP.IsPrivate() != dataIP.IsPrivate() ||
cmdIP.IsLoopback() != dataIP.IsLoopback()
}
// getDataConnPort returns a host, port for a new data connection
// it uses the best available method to do so
func (c *ServerConn) getDataConnPort() (string, int, error) {
if !c.options.disableEPSV && !c.skipEPSV {
if port, err := c.epsv(); err == nil {
return c.host, port, nil
}
// if there is an error, skip EPSV for the next attempts
c.skipEPSV = true
}
return c.pasv()
}
// openDataConn creates a new FTP data connection.
func (c *ServerConn) openDataConn() (net.Conn, error) {
host, port, err := c.getDataConnPort()
if err != nil {
return nil, err
}
addr := net.JoinHostPort(host, strconv.Itoa(port))
if c.options.dialFunc != nil {
return c.options.dialFunc("tcp", addr)
}
if c.options.tlsConfig != nil {
// We don't use tls.DialWithDialer here (which does Dial, create
// the Client and then do the Handshake) because it seems to
// hang with some FTP servers, namely proftpd and pureftpd.
//
// Instead we do Dial, create the Client and wait for the first
// Read or Write to trigger the Handshake.
//
// This means that if we are uploading a zero sized file, we
// need to make sure we do the Handshake explicitly as Write
// won't have been called. This is done in StorFrom().
//
// See: https://github.com/jlaffaye/ftp/issues/282
conn, err := c.options.dialer.Dial("tcp", addr)
if err != nil {
return nil, err
}
tlsConn := tls.Client(conn, c.options.tlsConfig)
return tlsConn, nil
}
return c.options.dialer.Dial("tcp", addr)
}
// cmd is a helper function to execute a command and check for the expected FTP
// return code
func (c *ServerConn) cmd(expected int, format string, args ...interface{}) (int, string, error) {
_, err := c.conn.Cmd(format, args...)
if err != nil {
return 0, "", err
}
return c.conn.ReadResponse(expected)
}
// cmdDataConnFrom executes a command which require a FTP data connection.
// Issues a REST FTP command to specify the number of bytes to skip for the transfer.
func (c *ServerConn) cmdDataConnFrom(offset uint64, format string, args ...interface{}) (net.Conn, error) {
// If server requires PRET send the PRET command to warm it up
// See: https://tools.ietf.org/html/draft-dd-pret-00
if c.usePRET {
_, _, err := c.cmd(-1, "PRET "+format, args...)
if err != nil {
return nil, err
}
}
conn, err := c.openDataConn()
if err != nil {
return nil, err
}
if offset != 0 {
_, _, err = c.cmd(StatusRequestFilePending, "REST %d", offset)
if err != nil {
_ = conn.Close()
return nil, err
}
}
_, err = c.conn.Cmd(format, args...)
if err != nil {
_ = conn.Close()
return nil, err
}
code, msg, err := c.conn.ReadResponse(-1)
if err != nil {
_ = conn.Close()
return nil, err
}
if code != StatusAlreadyOpen && code != StatusAboutToSend {
_ = conn.Close()
return nil, &textproto.Error{Code: code, Msg: msg}
}
return conn, nil
}
// Type switches the transfer mode for the connection.
func (c *ServerConn) Type(transferType TransferType) (err error) {
_, _, err = c.cmd(StatusCommandOK, "TYPE "+string(transferType))
return err
}
// NameList issues an NLST FTP command.
func (c *ServerConn) NameList(path string) (entries []string, err error) {
space := " "
if path == "" {
space = ""
}
conn, err := c.cmdDataConnFrom(0, "NLST%s%s", space, path)
if err != nil {
return nil, err
}
var errs *multierror.Error
r := &Response{conn: conn, c: c}
scanner := bufio.NewScanner(c.options.wrapStream(r))
for scanner.Scan() {
entries = append(entries, scanner.Text())
}
if err := scanner.Err(); err != nil {
errs = multierror.Append(errs, err)
}
if err := r.Close(); err != nil {
errs = multierror.Append(errs, err)
}
return entries, errs.ErrorOrNil()
}
// List issues a LIST FTP command.
func (c *ServerConn) List(path string) (entries []*Entry, err error) {
var cmd string
var parser parseFunc
if c.mlstSupported && !c.options.forceListHidden {
cmd = "MLSD"
parser = parseRFC3659ListLine
} else {
cmd = "LIST"
if c.options.forceListHidden {
cmd += " -a"
}
parser = parseListLine
}
space := " "
if path == "" {
space = ""
}
conn, err := c.cmdDataConnFrom(0, "%s%s%s", cmd, space, path)
if err != nil {
return nil, err
}
var errs *multierror.Error
r := &Response{conn: conn, c: c}
scanner := bufio.NewScanner(c.options.wrapStream(r))
now := time.Now()
for scanner.Scan() {
entry, errParse := parser(scanner.Text(), now, c.options.location)
if errParse == nil {
entries = append(entries, entry)
}
}
if err := scanner.Err(); err != nil {
errs = multierror.Append(errs, err)
}
if err := r.Close(); err != nil {
errs = multierror.Append(errs, err)
}
return entries, errs.ErrorOrNil()
}
// GetEntry issues a MLST FTP command which retrieves one single Entry using the
// control connection. The returnedEntry will describe the current directory
// when no path is given.
func (c *ServerConn) GetEntry(path string) (entry *Entry, err error) {
if !c.mlstSupported {
return nil, &textproto.Error{Code: StatusNotImplemented, Msg: StatusText(StatusNotImplemented)}
}
space := " "
if path == "" {
space = ""
}
_, msg, err := c.cmd(StatusRequestedFileActionOK, "%s%s%s", "MLST", space, path)
if err != nil {
return nil, err
}
// The expected reply will look something like:
//
// 250-File details
// Type=file;Size=1024;Modify=20220813133357; path
// 250 End
//
// Multiple lines are allowed though, so it can also be in the form:
//
// 250-File details
// Type=file;Size=1024; path
// Modify=20220813133357; path
// 250 End
lines := strings.Split(msg, "\n")
lc := len(lines)
// lines must be a multi-line message with a length of 3 or more, and we
// don't care about the first and last line
if lc < 3 {
return nil, errors.New("invalid response")
}
e := &Entry{}
for _, l := range lines[1 : lc-1] {
// According to RFC 3659, the entry lines must start with a space when passed over the
// control connection. Some servers don't seem to add that space though. Both forms are
// accepted here.
if len(l) > 0 && l[0] == ' ' {
l = l[1:]
}
// Some severs seem to send a blank line at the end which we ignore
if l == "" {
continue
}
if e, err = parseNextRFC3659ListLine(l, c.options.location, e); err != nil {
return nil, err
}
}
return e, nil
}
// IsTimePreciseInList returns true if client and server support the MLSD
// command so List can return time with 1-second precision for all files.
func (c *ServerConn) IsTimePreciseInList() bool {
return c.mlstSupported
}
// ChangeDir issues a CWD FTP command, which changes the current directory to
// the specified path.
func (c *ServerConn) ChangeDir(path string) error {
_, _, err := c.cmd(StatusRequestedFileActionOK, "CWD %s", path)
return err
}
// ChangeDirToParent issues a CDUP FTP command, which changes the current
// directory to the parent directory. This is similar to a call to ChangeDir
// with a path set to "..".
func (c *ServerConn) ChangeDirToParent() error {
_, _, err := c.cmd(StatusRequestedFileActionOK, "CDUP")
return err
}
// CurrentDir issues a PWD FTP command, which Returns the path of the current
// directory.
func (c *ServerConn) CurrentDir() (string, error) {
_, msg, err := c.cmd(StatusPathCreated, "PWD")
if err != nil {
return "", err
}
start := strings.Index(msg, "\"")
end := strings.LastIndex(msg, "\"")
if start == -1 || end == -1 {
return "", errors.New("unsuported PWD response format")
}
return msg[start+1 : end], nil
}
// FileSize issues a SIZE FTP command, which Returns the size of the file
func (c *ServerConn) FileSize(path string) (int64, error) {
_, msg, err := c.cmd(StatusFile, "SIZE %s", path)
if err != nil {
return 0, err
}
return strconv.ParseInt(msg, 10, 64)
}
// GetTime issues the MDTM FTP command to obtain the file modification time.
// It returns a UTC time.
func (c *ServerConn) GetTime(path string) (time.Time, error) {
var t time.Time
if !c.mdtmSupported {
return t, errors.New("GetTime is not supported")
}
_, msg, err := c.cmd(StatusFile, "MDTM %s", path)
if err != nil {
return t, err
}
return time.ParseInLocation(timeFormat, msg, time.UTC)
}
// IsGetTimeSupported allows library callers to check in advance that they
// can use GetTime to get file time.
func (c *ServerConn) IsGetTimeSupported() bool {
return c.mdtmSupported
}
// SetTime issues the MFMT FTP command to set the file modification time.
// Also it can use a non-standard form of the MDTM command supported by
// the VsFtpd server instead of MFMT for the same purpose.
// See "mdtm_write" in https://security.appspot.com/vsftpd/vsftpd_conf.html
func (c *ServerConn) SetTime(path string, t time.Time) (err error) {
utime := t.In(time.UTC).Format(timeFormat)
switch {
case c.mfmtSupported:
_, _, err = c.cmd(StatusFile, "MFMT %s %s", utime, path)
case c.mdtmCanWrite:
_, _, err = c.cmd(StatusFile, "MDTM %s %s", utime, path)
default:
err = errors.New("SetTime is not supported")
}
return
}
// IsSetTimeSupported allows library callers to check in advance that they
// can use SetTime to set file time.
func (c *ServerConn) IsSetTimeSupported() bool {
return c.mfmtSupported || c.mdtmCanWrite
}
// Retr issues a RETR FTP command to fetch the specified file from the remote
// FTP server.
//
// The returned ReadCloser must be closed to cleanup the FTP data connection.
func (c *ServerConn) Retr(path string) (*Response, error) {
return c.RetrFrom(path, 0)
}
// RetrFrom issues a RETR FTP command to fetch the specified file from the remote
// FTP server, the server will not send the offset first bytes of the file.
//
// The returned ReadCloser must be closed to cleanup the FTP data connection.
func (c *ServerConn) RetrFrom(path string, offset uint64) (*Response, error) {
conn, err := c.cmdDataConnFrom(offset, "RETR %s", path)
if err != nil {
return nil, err
}
return &Response{conn: conn, c: c}, nil
}
// Stor issues a STOR FTP command to store a file to the remote FTP server.
// Stor creates the specified file with the content of the io.Reader.
//
// Hint: io.Pipe() can be used if an io.Writer is required.
func (c *ServerConn) Stor(path string, r io.Reader) error {
return c.StorFrom(path, r, 0)
}
// checkDataShut reads the "closing data connection" status from the
// control connection. It is called after transferring a piece of data
// on the data connection during which the control connection was idle.
// This may result in the idle timeout triggering on the control connection
// right when we try to read the response.
// The ShutTimeout dial option will rescue here. It will nudge the control
// connection deadline right before checking the data closing status.
func (c *ServerConn) checkDataShut() error {
if c.options.shutTimeout != 0 {
shutDeadline := time.Now().Add(c.options.shutTimeout)
if err := c.netConn.SetDeadline(shutDeadline); err != nil {
return err
}
}
_, _, err := c.conn.ReadResponse(StatusClosingDataConnection)
return err
}
// StorFrom issues a STOR FTP command to store a file to the remote FTP server.
// Stor creates the specified file with the content of the io.Reader, writing
// on the server will start at the given file offset.
//
// Hint: io.Pipe() can be used if an io.Writer is required.
func (c *ServerConn) StorFrom(path string, r io.Reader, offset uint64) error {
conn, err := c.cmdDataConnFrom(offset, "STOR %s", path)
if err != nil {
return err
}
var errs *multierror.Error
// if the upload fails we still need to try to read the server
// response otherwise if the failure is not due to a connection problem,
// for example the server denied the upload for quota limits, we miss
// the response and we cannot use the connection to send other commands.
if n, err := io.Copy(conn, r); err != nil {
errs = multierror.Append(errs, err)
} else if n == 0 {
// If we wrote no bytes and got no error, make sure we call
// tls.Handshake on the connection as it won't get called
// unless Write() is called. (See comment in openDataConn()).
//
// ProFTP doesn't like this and returns "Unable to build data
// connection: Operation not permitted" when trying to upload
// an empty file without this.
if do, ok := conn.(interface{ Handshake() error }); ok {
if err := do.Handshake(); err != nil {
errs = multierror.Append(errs, err)
}
}
}
if err := conn.Close(); err != nil {
errs = multierror.Append(errs, err)
}
if err := c.checkDataShut(); err != nil {
errs = multierror.Append(errs, err)
}
return errs.ErrorOrNil()
}
// Append issues a APPE FTP command to store a file to the remote FTP server.
// If a file already exists with the given path, then the content of the
// io.Reader is appended. Otherwise, a new file is created with that content.
//
// Hint: io.Pipe() can be used if an io.Writer is required.
func (c *ServerConn) Append(path string, r io.Reader) error {
conn, err := c.cmdDataConnFrom(0, "APPE %s", path)
if err != nil {
return err
}
var errs *multierror.Error
if _, err := io.Copy(conn, r); err != nil {
errs = multierror.Append(errs, err)
}
if err := conn.Close(); err != nil {
errs = multierror.Append(errs, err)
}