-
Notifications
You must be signed in to change notification settings - Fork 100
/
handle_misc.go
382 lines (293 loc) · 8.28 KB
/
handle_misc.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
package ftpserver
import (
"bufio"
"crypto/tls"
"errors"
"fmt"
"strconv"
"strings"
"time"
)
var errUnknowHash = errors.New("unknown hash algorithm")
func (c *clientHandler) handleAUTH(_ string) error {
if tlsConfig, err := c.server.driver.GetTLSConfig(); err == nil {
c.writeMessage(StatusAuthAccepted, "AUTH command ok. Expecting TLS Negotiation.")
c.conn = tls.Server(c.conn, tlsConfig)
c.reader = bufio.NewReaderSize(c.conn, maxCommandSize)
c.writer = bufio.NewWriter(c.conn)
c.setTLSForControl(true)
} else {
c.writeMessage(StatusActionNotTaken, fmt.Sprintf("Cannot get a TLS config: %v", err))
}
return nil
}
func (c *clientHandler) handlePROT(param string) error {
// P for Private, C for Clear
c.setTLSForTransfer(param == "P")
c.writeMessage(StatusOK, "OK")
return nil
}
func (c *clientHandler) handlePBSZ(_ string) error {
c.writeMessage(StatusOK, "Whatever")
return nil
}
func (c *clientHandler) handleSYST(_ string) error {
if c.server.settings.DisableSYST {
c.writeMessage(StatusCommandNotImplemented, "SYST is disabled")
return nil
}
c.writeMessage(StatusSystemType, "UNIX Type: L8")
return nil
}
func (c *clientHandler) handleSTAT(param string) error {
if param == "" { // Without a file, it's the server stat
return c.handleSTATServer()
}
// With a file/dir it's the file or the dir's files stat
return c.handleSTATFile(param)
}
func (c *clientHandler) handleSITE(param string) error {
if c.server.settings.DisableSite {
c.writeMessage(StatusSyntaxErrorNotRecognised, "SITE support is disabled")
return nil
}
spl := strings.SplitN(param, " ", 2)
cmd := strings.ToUpper(spl[0])
var params string
if len(spl) > 1 {
params = spl[1]
} else {
params = ""
}
switch cmd {
case "CHMOD":
c.handleCHMOD(params)
case "CHOWN":
c.handleCHOWN(params)
case "SYMLINK":
c.handleSYMLINK(params)
case "MKDIR":
c.handleMKDIR(params)
case "RMDIR":
c.handleRMDIR(params)
default:
c.writeMessage(StatusSyntaxErrorNotRecognised, "Unknown SITE subcommand: "+cmd)
}
return nil
}
func (c *clientHandler) handleSTATServer() error {
// we need to hold the transfer lock here:
// server STAT is a special action command so we need to ensure
// to write the whole STAT response before sending a transfer
// open/close message
c.transferMu.Lock()
defer c.transferMu.Unlock()
if c.server.settings.DisableSTAT {
c.writeMessage(StatusCommandNotImplemented, "STAT is disabled")
return nil
}
defer c.multilineAnswer(StatusSystemStatus, "Server status")()
duration := time.Now().UTC().Sub(c.connectedAt)
duration -= duration % time.Second
c.writeLine(fmt.Sprintf(
"Connected to %s from %s for %s",
c.server.settings.ListenAddr,
c.conn.RemoteAddr(),
duration,
))
if c.user != "" {
c.writeLine("Logged in as " + c.user)
} else {
c.writeLine("Not logged in yet")
}
if info := c.GetTranferInfo(); info != "" {
c.writeLine("Transfer connection open")
c.writeLine(info)
}
c.writeLine(c.server.settings.Banner)
return nil
}
func (c *clientHandler) handleOptsUtf8() error {
c.writeMessage(StatusOK, "I'm in UTF8 only anyway")
return nil
}
func (c *clientHandler) handleOptsHash(args []string) error {
hashMapping := getHashMapping()
if len(args) > 0 {
// try to change the current hash algorithm to the requested one
if value, ok := hashMapping[args[0]]; ok {
c.selectedHashAlgo = value
c.writeMessage(StatusOK, args[0])
} else {
c.writeMessage(StatusSyntaxErrorParameters, "Unknown algorithm, current selection not changed")
}
return nil
}
// return the current hash algorithm
var currentHash string
for k, v := range hashMapping {
if v == c.selectedHashAlgo {
currentHash = k
}
}
c.writeMessage(StatusOK, currentHash)
return nil
}
func (c *clientHandler) handleOPTS(param string) error {
args := strings.SplitN(param, " ", 2)
switch strings.ToUpper(args[0]) {
case "UTF8":
return c.handleOptsUtf8()
case "HASH":
if c.server.settings.EnableHASH {
return c.handleOptsHash(args[1:])
}
}
c.writeMessage(StatusSyntaxErrorNotRecognised, "Don't know this option")
return nil
}
func (c *clientHandler) handleNOOP(_ string) error {
c.writeMessage(StatusOK, "OK")
return nil
}
func (c *clientHandler) handleCLNT(param string) error {
c.setClientVersion(param)
c.writeMessage(StatusOK, "Good to know")
return nil
}
func (c *clientHandler) handleFEAT(_ string) error {
c.writeLine(fmt.Sprintf("%d- These are my features", StatusSystemStatus))
defer c.writeMessage(StatusSystemStatus, "end")
features := []string{
"CLNT",
"UTF8",
"SIZE",
"MDTM",
"REST STREAM",
"EPRT",
"EPSV",
}
if !c.server.settings.DisableMLSD {
features = append(features, "MLSD")
}
if !c.server.settings.DisableMLST {
features = append(features, "MLST")
}
if !c.server.settings.DisableMFMT {
features = append(features, "MFMT")
}
// This code made me think about adding this: https://github.com/stianstr/ftpserver/commit/387f2ba
if tlsConfig, err := c.server.driver.GetTLSConfig(); tlsConfig != nil && err == nil {
features = append(features, "AUTH TLS", "PBSZ", "PROT")
}
if c.server.settings.EnableHASH {
var hashLine strings.Builder
nonStandardHashImpl := []string{"XCRC", "MD5", "XMD5", "XSHA", "XSHA1", "XSHA256", "XSHA512"}
hashMapping := getHashMapping()
for k, v := range hashMapping {
hashLine.WriteString(k)
if v == c.selectedHashAlgo {
hashLine.WriteString("*")
}
hashLine.WriteString(";")
}
features = append(features, hashLine.String())
features = append(features, nonStandardHashImpl...)
}
if c.server.settings.EnableCOMB {
features = append(features, "COMB")
}
if _, ok := c.driver.(ClientDriverExtensionAvailableSpace); ok {
features = append(features, "AVBL")
}
for _, f := range features {
c.writeLine(" " + f)
}
return nil
}
func (c *clientHandler) handleTYPE(param string) error {
param = strings.ReplaceAll(strings.ToUpper(param), " ", "")
switch param {
case "I", "L8":
c.currentTransferType = TransferTypeBinary
c.writeMessage(StatusOK, "Type set to binary")
case "A", "AN", "L7":
c.currentTransferType = TransferTypeASCII
c.writeMessage(StatusOK, "Type set to ASCII")
default:
c.writeMessage(StatusNotImplementedParam, "Unsupported transfer type")
}
return nil
}
func (c *clientHandler) handleMODE(param string) error {
if param == "S" {
c.writeMessage(StatusOK, "Using stream mode")
} else {
c.writeMessage(StatusNotImplementedParam, "Unsupported mode")
}
return nil
}
func (c *clientHandler) handleQUIT(_ string) error {
c.transferWg.Wait()
var msg string
if quitter, ok := c.server.driver.(MainDriverExtensionQuitMessage); ok {
msg = quitter.QuitMessage()
} else {
msg = "Goodbye"
}
c.writeMessage(StatusClosingControlConn, msg)
c.disconnect()
c.reader = nil
return nil
}
func (c *clientHandler) handleABOR(param string) error {
c.transferMu.Lock()
defer c.transferMu.Unlock()
if c.transfer != nil {
isOpened := c.isTransferOpen
c.isTransferAborted = true
if err := c.closeTransfer(); err != nil {
c.logger.Warn(
"Problem aborting transfer for command", param,
"err", err,
)
}
if c.debug {
c.logger.Debug(
"Transfer aborted",
"command", param)
}
if isOpened {
c.writeMessage(StatusTransferAborted, "Connection closed; transfer aborted")
}
}
c.writeMessage(StatusClosingDataConn, "ABOR successful; closing transfer connection")
return nil
}
func (c *clientHandler) handleAVBL(param string) error {
if avbl, ok := c.driver.(ClientDriverExtensionAvailableSpace); ok {
path := c.absPath(param)
info, err := c.driver.Stat(path)
if err != nil {
c.writeMessage(StatusActionNotTaken, fmt.Sprintf("Couldn't access %s: %v", path, err))
return nil
}
if !info.IsDir() {
c.writeMessage(StatusActionNotTaken, path+": is not a directory")
return nil
}
available, err := avbl.GetAvailableSpace(path)
if err != nil {
c.writeMessage(StatusActionNotTaken, fmt.Sprintf("Couldn't get space for path %s: %v", path, err))
return nil
}
c.writeMessage(StatusFileStatus, strconv.FormatInt(available, 10))
} else {
c.writeMessage(StatusNotImplemented, "This extension hasn't been implemented !")
}
return nil
}
func (c *clientHandler) handleNotImplemented(_ string) error {
c.writeMessage(StatusCommandNotImplemented, "This command hasn't been implemented !")
return nil
}