This repository has been archived by the owner on Jul 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 138
/
cmd.go
1206 lines (978 loc) · 26.8 KB
/
cmd.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
// Copyright 2018 The goftp Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package server
import (
"encoding/binary"
"fmt"
"log"
"strconv"
"strings"
)
type Command interface {
IsExtend() bool
RequireParam() bool
RequireAuth() bool
Execute(*Conn, string)
}
type commandMap map[string]Command
var (
commands = commandMap{
"ADAT": commandAdat{},
"ALLO": commandAllo{},
"APPE": commandAppe{},
"AUTH": commandAuth{},
"CDUP": commandCdup{},
"CWD": commandCwd{},
"CCC": commandCcc{},
"CONF": commandConf{},
"DELE": commandDele{},
"ENC": commandEnc{},
"EPRT": commandEprt{},
"EPSV": commandEpsv{},
"FEAT": commandFeat{},
"LIST": commandList{},
"LPRT": commandLprt{},
"NLST": commandNlst{},
"MDTM": commandMdtm{},
"MIC": commandMic{},
"MKD": commandMkd{},
"MODE": commandMode{},
"NOOP": commandNoop{},
"OPTS": commandOpts{},
"PASS": commandPass{},
"PASV": commandPasv{},
"PBSZ": commandPbsz{},
"PORT": commandPort{},
"PROT": commandProt{},
"PWD": commandPwd{},
"QUIT": commandQuit{},
"RETR": commandRetr{},
"REST": commandRest{},
"RNFR": commandRnfr{},
"RNTO": commandRnto{},
"RMD": commandRmd{},
"SIZE": commandSize{},
"STOR": commandStor{},
"STRU": commandStru{},
"SYST": commandSyst{},
"TYPE": commandType{},
"USER": commandUser{},
"XCUP": commandCdup{},
"XCWD": commandCwd{},
"XMKD": commandMkd{},
"XPWD": commandPwd{},
"XRMD": commandRmd{},
}
)
// commandAllo responds to the ALLO FTP command.
//
// This is essentially a ping from the client so we just respond with an
// basic OK message.
type commandAllo struct{}
func (cmd commandAllo) IsExtend() bool {
return false
}
func (cmd commandAllo) RequireParam() bool {
return false
}
func (cmd commandAllo) RequireAuth() bool {
return false
}
func (cmd commandAllo) Execute(conn *Conn, param string) {
conn.writeMessage(202, "Obsolete")
}
// commandAppe responds to the APPE FTP command. It allows the user to upload a
// new file but always append if file exists otherwise create one.
type commandAppe struct{}
func (cmd commandAppe) IsExtend() bool {
return false
}
func (cmd commandAppe) RequireParam() bool {
return true
}
func (cmd commandAppe) RequireAuth() bool {
return true
}
func (cmd commandAppe) Execute(conn *Conn, param string) {
targetPath := conn.buildPath(param)
conn.writeMessage(150, "Data transfer starting")
bytes, err := conn.driver.PutFile(targetPath, conn.dataConn, true)
if err == nil {
msg := "OK, received " + strconv.Itoa(int(bytes)) + " bytes"
conn.writeMessage(226, msg)
} else {
conn.writeMessage(450, fmt.Sprint("error during transfer: ", err))
}
}
type commandOpts struct{}
func (cmd commandOpts) IsExtend() bool {
return false
}
func (cmd commandOpts) RequireParam() bool {
return false
}
func (cmd commandOpts) RequireAuth() bool {
return false
}
func (cmd commandOpts) Execute(conn *Conn, param string) {
parts := strings.Fields(param)
if len(parts) != 2 {
conn.writeMessage(550, "Unknow params")
return
}
if strings.ToUpper(parts[0]) != "UTF8" {
conn.writeMessage(550, "Unknow params")
return
}
if strings.ToUpper(parts[1]) == "ON" {
conn.writeMessage(200, "UTF8 mode enabled")
} else {
conn.writeMessage(550, "Unsupported non-utf8 mode")
}
}
type commandFeat struct{}
func (cmd commandFeat) IsExtend() bool {
return false
}
func (cmd commandFeat) RequireParam() bool {
return false
}
func (cmd commandFeat) RequireAuth() bool {
return false
}
var (
feats = "Extensions supported:\n%s"
featCmds = " UTF8\n"
)
func init() {
for k, v := range commands {
if v.IsExtend() {
featCmds = featCmds + " " + k + "\n"
}
}
}
func (cmd commandFeat) Execute(conn *Conn, param string) {
conn.writeMessageMultiline(211, conn.server.feats)
}
// cmdCdup responds to the CDUP FTP command.
//
// Allows the client change their current directory to the parent.
type commandCdup struct{}
func (cmd commandCdup) IsExtend() bool {
return false
}
func (cmd commandCdup) RequireParam() bool {
return false
}
func (cmd commandCdup) RequireAuth() bool {
return true
}
func (cmd commandCdup) Execute(conn *Conn, param string) {
otherCmd := &commandCwd{}
otherCmd.Execute(conn, "..")
}
// commandCwd responds to the CWD FTP command. It allows the client to change the
// current working directory.
type commandCwd struct{}
func (cmd commandCwd) IsExtend() bool {
return false
}
func (cmd commandCwd) RequireParam() bool {
return true
}
func (cmd commandCwd) RequireAuth() bool {
return true
}
func (cmd commandCwd) Execute(conn *Conn, param string) {
path := conn.buildPath(param)
err := conn.driver.ChangeDir(path)
if err == nil {
conn.namePrefix = path
conn.writeMessage(250, "Directory changed to "+path)
} else {
conn.writeMessage(550, fmt.Sprint("Directory change to ", path, " failed: ", err))
}
}
// commandDele responds to the DELE FTP command. It allows the client to delete
// a file
type commandDele struct{}
func (cmd commandDele) IsExtend() bool {
return false
}
func (cmd commandDele) RequireParam() bool {
return true
}
func (cmd commandDele) RequireAuth() bool {
return true
}
func (cmd commandDele) Execute(conn *Conn, param string) {
path := conn.buildPath(param)
err := conn.driver.DeleteFile(path)
if err == nil {
conn.writeMessage(250, "File deleted")
} else {
conn.writeMessage(550, fmt.Sprint("File delete failed: ", err))
}
}
// commandEprt responds to the EPRT FTP command. It allows the client to
// request an active data socket with more options than the original PORT
// command. It mainly adds ipv6 support.
type commandEprt struct{}
func (cmd commandEprt) IsExtend() bool {
return true
}
func (cmd commandEprt) RequireParam() bool {
return true
}
func (cmd commandEprt) RequireAuth() bool {
return true
}
func (cmd commandEprt) Execute(conn *Conn, param string) {
delim := string(param[0:1])
parts := strings.Split(param, delim)
addressFamily, err := strconv.Atoi(parts[1])
host := parts[2]
port, err := strconv.Atoi(parts[3])
if addressFamily != 1 && addressFamily != 2 {
conn.writeMessage(522, "Network protocol not supported, use (1,2)")
return
}
socket, err := newActiveSocket(host, port, conn.logger, conn.sessionID)
if err != nil {
conn.writeMessage(425, "Data connection failed")
return
}
conn.dataConn = socket
conn.writeMessage(200, "Connection established ("+strconv.Itoa(port)+")")
}
// commandLprt responds to the LPRT FTP command. It allows the client to
// request an active data socket with more options than the original PORT
// command. FTP Operation Over Big Address Records.
type commandLprt struct{}
func (cmd commandLprt) IsExtend() bool {
return true
}
func (cmd commandLprt) RequireParam() bool {
return true
}
func (cmd commandLprt) RequireAuth() bool {
return true
}
func (cmd commandLprt) Execute(conn *Conn, param string) {
// No tests for this code yet
parts := strings.Split(param, ",")
addressFamily, err := strconv.Atoi(parts[0])
if addressFamily != 4 {
conn.writeMessage(522, "Network protocol not supported, use 4")
return
}
addressLength, err := strconv.Atoi(parts[1])
if addressLength != 4 {
conn.writeMessage(522, "Network IP length not supported, use 4")
return
}
host := strings.Join(parts[2:2+addressLength], ".")
portLength, err := strconv.Atoi(parts[2+addressLength])
portAddress := parts[3+addressLength : 3+addressLength+portLength]
// Convert string[] to byte[]
portBytes := make([]byte, portLength)
for i := range portAddress {
p, _ := strconv.Atoi(portAddress[i])
portBytes[i] = byte(p)
}
// convert the bytes to an int
port := int(binary.BigEndian.Uint16(portBytes))
// if the existing connection is on the same host/port don't reconnect
if conn.dataConn.Host() == host && conn.dataConn.Port() == port {
return
}
socket, err := newActiveSocket(host, port, conn.logger, conn.sessionID)
if err != nil {
conn.writeMessage(425, "Data connection failed")
return
}
conn.dataConn = socket
conn.writeMessage(200, "Connection established ("+strconv.Itoa(port)+")")
}
// commandEpsv responds to the EPSV FTP command. It allows the client to
// request a passive data socket with more options than the original PASV
// command. It mainly adds ipv6 support, although we don't support that yet.
type commandEpsv struct{}
func (cmd commandEpsv) IsExtend() bool {
return true
}
func (cmd commandEpsv) RequireParam() bool {
return false
}
func (cmd commandEpsv) RequireAuth() bool {
return true
}
func (cmd commandEpsv) Execute(conn *Conn, param string) {
addr := conn.passiveListenIP()
socket, err := newPassiveSocket(addr, conn.PassivePort, conn.logger, conn.sessionID, conn.tlsConfig)
if err != nil {
log.Println(err)
conn.writeMessage(425, "Data connection failed")
return
}
conn.dataConn = socket
msg := fmt.Sprintf("Entering Extended Passive Mode (|||%d|)", socket.Port())
conn.writeMessage(229, msg)
}
// commandList responds to the LIST FTP command. It allows the client to retreive
// a detailed listing of the contents of a directory.
type commandList struct{}
func (cmd commandList) IsExtend() bool {
return false
}
func (cmd commandList) RequireParam() bool {
return false
}
func (cmd commandList) RequireAuth() bool {
return true
}
func (cmd commandList) Execute(conn *Conn, param string) {
path := conn.buildPath(parseListParam(param))
info, err := conn.driver.Stat(path)
if err != nil {
conn.writeMessage(550, err.Error())
return
}
if info == nil {
conn.logger.Printf(conn.sessionID, "%s: no such file or directory.\n", path)
return
}
var files []FileInfo
if info.IsDir() {
err = conn.driver.ListDir(path, func(f FileInfo) error {
files = append(files, f)
return nil
})
if err != nil {
conn.writeMessage(550, err.Error())
return
}
} else {
files = append(files, info)
}
conn.writeMessage(150, "Opening ASCII mode data connection for file list")
conn.sendOutofbandData(listFormatter(files).Detailed())
}
func parseListParam(param string) (path string) {
if len(param) == 0 {
path = param
} else {
fields := strings.Fields(param)
i := 0
for _, field := range fields {
if !strings.HasPrefix(field, "-") {
break
}
i = strings.LastIndex(param, " "+field) + len(field) + 1
}
path = strings.TrimLeft(param[i:], " ") //Get all the path even with space inside
}
return path
}
// commandNlst responds to the NLST FTP command. It allows the client to
// retreive a list of filenames in the current directory.
type commandNlst struct{}
func (cmd commandNlst) IsExtend() bool {
return false
}
func (cmd commandNlst) RequireParam() bool {
return false
}
func (cmd commandNlst) RequireAuth() bool {
return true
}
func (cmd commandNlst) Execute(conn *Conn, param string) {
path := conn.buildPath(parseListParam(param))
info, err := conn.driver.Stat(path)
if err != nil {
conn.writeMessage(550, err.Error())
return
}
if !info.IsDir() {
conn.writeMessage(550, param+" is not a directory")
return
}
var files []FileInfo
err = conn.driver.ListDir(path, func(f FileInfo) error {
files = append(files, f)
return nil
})
if err != nil {
conn.writeMessage(550, err.Error())
return
}
conn.writeMessage(150, "Opening ASCII mode data connection for file list")
conn.sendOutofbandData(listFormatter(files).Short())
}
// commandMdtm responds to the MDTM FTP command. It allows the client to
// retreive the last modified time of a file.
type commandMdtm struct{}
func (cmd commandMdtm) IsExtend() bool {
return false
}
func (cmd commandMdtm) RequireParam() bool {
return true
}
func (cmd commandMdtm) RequireAuth() bool {
return true
}
func (cmd commandMdtm) Execute(conn *Conn, param string) {
path := conn.buildPath(param)
stat, err := conn.driver.Stat(path)
if err == nil {
conn.writeMessage(213, stat.ModTime().Format("20060102150405"))
} else {
conn.writeMessage(450, "File not available")
}
}
// commandMkd responds to the MKD FTP command. It allows the client to create
// a new directory
type commandMkd struct{}
func (cmd commandMkd) IsExtend() bool {
return false
}
func (cmd commandMkd) RequireParam() bool {
return true
}
func (cmd commandMkd) RequireAuth() bool {
return true
}
func (cmd commandMkd) Execute(conn *Conn, param string) {
path := conn.buildPath(param)
err := conn.driver.MakeDir(path)
if err == nil {
conn.writeMessage(257, "Directory created")
} else {
conn.writeMessage(550, fmt.Sprint("Action not taken: ", err))
}
}
// cmdMode responds to the MODE FTP command.
//
// the original FTP spec had various options for hosts to negotiate how data
// would be sent over the data socket, In reality these days (S)tream mode
// is all that is used for the mode - data is just streamed down the data
// socket unchanged.
type commandMode struct{}
func (cmd commandMode) IsExtend() bool {
return false
}
func (cmd commandMode) RequireParam() bool {
return true
}
func (cmd commandMode) RequireAuth() bool {
return true
}
func (cmd commandMode) Execute(conn *Conn, param string) {
if strings.ToUpper(param) == "S" {
conn.writeMessage(200, "OK")
} else {
conn.writeMessage(504, "MODE is an obsolete command")
}
}
// cmdNoop responds to the NOOP FTP command.
//
// This is essentially a ping from the client so we just respond with an
// basic 200 message.
type commandNoop struct{}
func (cmd commandNoop) IsExtend() bool {
return false
}
func (cmd commandNoop) RequireParam() bool {
return false
}
func (cmd commandNoop) RequireAuth() bool {
return false
}
func (cmd commandNoop) Execute(conn *Conn, param string) {
conn.writeMessage(200, "OK")
}
// commandPass respond to the PASS FTP command by asking the driver if the
// supplied username and password are valid
type commandPass struct{}
func (cmd commandPass) IsExtend() bool {
return false
}
func (cmd commandPass) RequireParam() bool {
return true
}
func (cmd commandPass) RequireAuth() bool {
return false
}
func (cmd commandPass) Execute(conn *Conn, param string) {
ok, err := conn.server.Auth.CheckPasswd(conn.reqUser, param)
if err != nil {
conn.writeMessage(550, "Checking password error")
return
}
if ok {
conn.user = conn.reqUser
conn.reqUser = ""
conn.writeMessage(230, "Password ok, continue")
} else {
conn.writeMessage(530, "Incorrect password, not logged in")
}
}
// commandPasv responds to the PASV FTP command.
//
// The client is requesting us to open a new TCP listing socket and wait for them
// to connect to it.
type commandPasv struct{}
func (cmd commandPasv) IsExtend() bool {
return false
}
func (cmd commandPasv) RequireParam() bool {
return false
}
func (cmd commandPasv) RequireAuth() bool {
return true
}
func (cmd commandPasv) Execute(conn *Conn, param string) {
listenIP := conn.passiveListenIP()
socket, err := newPassiveSocket(listenIP, conn.PassivePort, conn.logger, conn.sessionID, conn.tlsConfig)
if err != nil {
conn.writeMessage(425, "Data connection failed")
return
}
conn.dataConn = socket
p1 := socket.Port() / 256
p2 := socket.Port() - (p1 * 256)
quads := strings.Split(listenIP, ".")
target := fmt.Sprintf("(%s,%s,%s,%s,%d,%d)", quads[0], quads[1], quads[2], quads[3], p1, p2)
msg := "Entering Passive Mode " + target
conn.writeMessage(227, msg)
}
// commandPort responds to the PORT FTP command.
//
// The client has opened a listening socket for sending out of band data and
// is requesting that we connect to it
type commandPort struct{}
func (cmd commandPort) IsExtend() bool {
return false
}
func (cmd commandPort) RequireParam() bool {
return true
}
func (cmd commandPort) RequireAuth() bool {
return true
}
func (cmd commandPort) Execute(conn *Conn, param string) {
nums := strings.Split(param, ",")
portOne, _ := strconv.Atoi(nums[4])
portTwo, _ := strconv.Atoi(nums[5])
port := (portOne * 256) + portTwo
host := nums[0] + "." + nums[1] + "." + nums[2] + "." + nums[3]
socket, err := newActiveSocket(host, port, conn.logger, conn.sessionID)
if err != nil {
conn.writeMessage(425, "Data connection failed")
return
}
conn.dataConn = socket
conn.writeMessage(200, "Connection established ("+strconv.Itoa(port)+")")
}
// commandPwd responds to the PWD FTP command.
//
// Tells the client what the current working directory is.
type commandPwd struct{}
func (cmd commandPwd) IsExtend() bool {
return false
}
func (cmd commandPwd) RequireParam() bool {
return false
}
func (cmd commandPwd) RequireAuth() bool {
return true
}
func (cmd commandPwd) Execute(conn *Conn, param string) {
conn.writeMessage(257, "\""+conn.namePrefix+"\" is the current directory")
}
// CommandQuit responds to the QUIT FTP command. The client has requested the
// connection be closed.
type commandQuit struct{}
func (cmd commandQuit) IsExtend() bool {
return false
}
func (cmd commandQuit) RequireParam() bool {
return false
}
func (cmd commandQuit) RequireAuth() bool {
return false
}
func (cmd commandQuit) Execute(conn *Conn, param string) {
conn.writeMessage(221, "Goodbye")
conn.Close()
}
// commandRetr responds to the RETR FTP command. It allows the client to
// download a file.
type commandRetr struct{}
func (cmd commandRetr) IsExtend() bool {
return false
}
func (cmd commandRetr) RequireParam() bool {
return true
}
func (cmd commandRetr) RequireAuth() bool {
return true
}
func (cmd commandRetr) Execute(conn *Conn, param string) {
path := conn.buildPath(param)
defer func() {
conn.lastFilePos = 0
conn.appendData = false
}()
bytes, data, err := conn.driver.GetFile(path, conn.lastFilePos)
if err == nil {
defer data.Close()
conn.writeMessage(150, fmt.Sprintf("Data transfer starting %v bytes", bytes))
err = conn.sendOutofBandDataWriter(data)
if err != nil {
conn.writeMessage(551, "Error reading file")
}
} else {
conn.writeMessage(551, "File not available")
}
}
type commandRest struct{}
func (cmd commandRest) IsExtend() bool {
return false
}
func (cmd commandRest) RequireParam() bool {
return true
}
func (cmd commandRest) RequireAuth() bool {
return true
}
func (cmd commandRest) Execute(conn *Conn, param string) {
var err error
conn.lastFilePos, err = strconv.ParseInt(param, 10, 64)
if err != nil {
conn.writeMessage(551, "File not available")
return
}
conn.appendData = true
conn.writeMessage(350, fmt.Sprint("Start transfer from ", conn.lastFilePos))
}
// commandRnfr responds to the RNFR FTP command. It's the first of two commands
// required for a client to rename a file.
type commandRnfr struct{}
func (cmd commandRnfr) IsExtend() bool {
return false
}
func (cmd commandRnfr) RequireParam() bool {
return true
}
func (cmd commandRnfr) RequireAuth() bool {
return true
}
func (cmd commandRnfr) Execute(conn *Conn, param string) {
conn.renameFrom = conn.buildPath(param)
conn.writeMessage(350, "Requested file action pending further information.")
}
// cmdRnto responds to the RNTO FTP command. It's the second of two commands
// required for a client to rename a file.
type commandRnto struct{}
func (cmd commandRnto) IsExtend() bool {
return false
}
func (cmd commandRnto) RequireParam() bool {
return true
}
func (cmd commandRnto) RequireAuth() bool {
return true
}
func (cmd commandRnto) Execute(conn *Conn, param string) {
toPath := conn.buildPath(param)
err := conn.driver.Rename(conn.renameFrom, toPath)
defer func() {
conn.renameFrom = ""
}()
if err == nil {
conn.writeMessage(250, "File renamed")
} else {
conn.writeMessage(550, fmt.Sprint("Action not taken: ", err))
}
}
// cmdRmd responds to the RMD FTP command. It allows the client to delete a
// directory.
type commandRmd struct{}
func (cmd commandRmd) IsExtend() bool {
return false
}
func (cmd commandRmd) RequireParam() bool {
return true
}
func (cmd commandRmd) RequireAuth() bool {
return true
}
func (cmd commandRmd) Execute(conn *Conn, param string) {
path := conn.buildPath(param)
err := conn.driver.DeleteDir(path)
if err == nil {
conn.writeMessage(250, "Directory deleted")
} else {
conn.writeMessage(550, fmt.Sprint("Directory delete failed: ", err))
}
}
type commandAdat struct{}
func (cmd commandAdat) IsExtend() bool {
return false
}
func (cmd commandAdat) RequireParam() bool {
return true
}
func (cmd commandAdat) RequireAuth() bool {
return true
}
func (cmd commandAdat) Execute(conn *Conn, param string) {
conn.writeMessage(550, "Action not taken")
}
type commandAuth struct{}
func (cmd commandAuth) IsExtend() bool {
return false
}
func (cmd commandAuth) RequireParam() bool {
return true
}
func (cmd commandAuth) RequireAuth() bool {
return false
}
func (cmd commandAuth) Execute(conn *Conn, param string) {
if param == "TLS" && conn.tlsConfig != nil {
conn.writeMessage(234, "AUTH command OK")
err := conn.upgradeToTLS()
if err != nil {
conn.logger.Printf("Error upgrading connection to TLS %v", err.Error())
}
} else {
conn.writeMessage(550, "Action not taken")
}
}
type commandCcc struct{}
func (cmd commandCcc) IsExtend() bool {
return false
}
func (cmd commandCcc) RequireParam() bool {
return true
}
func (cmd commandCcc) RequireAuth() bool {
return true
}
func (cmd commandCcc) Execute(conn *Conn, param string) {
conn.writeMessage(550, "Action not taken")
}
type commandEnc struct{}
func (cmd commandEnc) IsExtend() bool {
return false
}
func (cmd commandEnc) RequireParam() bool {
return true
}
func (cmd commandEnc) RequireAuth() bool {
return true
}
func (cmd commandEnc) Execute(conn *Conn, param string) {
conn.writeMessage(550, "Action not taken")
}
type commandMic struct{}
func (cmd commandMic) IsExtend() bool {
return false
}
func (cmd commandMic) RequireParam() bool {
return true
}
func (cmd commandMic) RequireAuth() bool {
return true
}
func (cmd commandMic) Execute(conn *Conn, param string) {
conn.writeMessage(550, "Action not taken")
}
type commandPbsz struct{}
func (cmd commandPbsz) IsExtend() bool {
return false
}
func (cmd commandPbsz) RequireParam() bool {
return true
}
func (cmd commandPbsz) RequireAuth() bool {
return false
}
func (cmd commandPbsz) Execute(conn *Conn, param string) {
if conn.tls && param == "0" {
conn.writeMessage(200, "OK")
} else {
conn.writeMessage(550, "Action not taken")
}
}
type commandProt struct{}
func (cmd commandProt) IsExtend() bool {