forked from fclairamb/ftpserverlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_dirs.go
340 lines (267 loc) · 7.73 KB
/
handle_dirs.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
package ftpserver // nolint
import (
"fmt"
"io"
"os"
"path"
"strings"
"time"
"github.com/spf13/afero"
)
// the order matter, put parameters with more characters first
var supportedlistArgs = []string{"-al", "-la", "-a", "-l"}
func (c *clientHandler) absPath(p string) string {
if strings.HasPrefix(p, "/") {
return path.Clean(p)
}
return path.Clean(c.Path() + "/" + p)
}
func (c *clientHandler) handleCWD(param string) error {
p := c.absPath(param)
if _, err := c.driver.Stat(p); err == nil {
c.SetPath(p)
c.writeMessage(StatusFileOK, fmt.Sprintf("CD worked on %s", p))
} else {
c.writeMessage(StatusActionNotTaken, fmt.Sprintf("CD issue: %v", err))
}
return nil
}
func (c *clientHandler) handleMKD(param string) error {
p := c.absPath(param)
if err := c.driver.Mkdir(p, 0755); err == nil {
// handleMKD confirms to "qoute-doubling"
// https://tools.ietf.org/html/rfc959 , page 63
c.writeMessage(StatusPathCreated, fmt.Sprintf(`Created dir "%s"`, quoteDoubling(p)))
} else {
c.writeMessage(StatusActionNotTaken, fmt.Sprintf(`Could not create "%s" : %v`, quoteDoubling(p), err))
}
return nil
}
func (c *clientHandler) handleMKDIR(params string) {
if params == "" {
c.writeMessage(StatusSyntaxErrorNotRecognised, "Missing path")
return
}
p := c.absPath(params)
if err := c.driver.MkdirAll(p, 0755); err == nil {
c.writeMessage(StatusFileOK, fmt.Sprintf("Created dir %s", p))
} else {
c.writeMessage(StatusActionNotTaken, fmt.Sprintf("Couldn't create dir %s: %v", p, err))
}
}
func (c *clientHandler) handleRMD(param string) error {
var err error
p := c.absPath(param)
if rmd, ok := c.driver.(ClientDriverExtensionRemoveDir); ok {
err = rmd.RemoveDir(p)
} else {
err = c.driver.Remove(p)
}
if err == nil {
c.writeMessage(StatusFileOK, fmt.Sprintf("Deleted dir %s", p))
} else {
c.writeMessage(StatusActionNotTaken, fmt.Sprintf("Could not delete dir %s: %v", p, err))
}
return nil
}
func (c *clientHandler) handleRMDIR(params string) {
if params == "" {
c.writeMessage(StatusSyntaxErrorNotRecognised, "Missing path")
return
}
p := c.absPath(params)
if err := c.driver.RemoveAll(p); err == nil {
c.writeMessage(StatusFileOK, fmt.Sprintf("Removed dir %s", p))
} else {
c.writeMessage(StatusActionNotTaken, fmt.Sprintf("Couldn't remove dir %s: %v", p, err))
}
}
func (c *clientHandler) handleCDUP(param string) error {
parent, _ := path.Split(c.Path())
if parent != "/" && strings.HasSuffix(parent, "/") {
parent = parent[0 : len(parent)-1]
}
if _, err := c.driver.Stat(parent); err == nil {
c.SetPath(parent)
c.writeMessage(StatusFileOK, fmt.Sprintf("CDUP worked on %s", parent))
} else {
c.writeMessage(StatusActionNotTaken, fmt.Sprintf("CDUP issue: %v", err))
}
return nil
}
func (c *clientHandler) handlePWD(param string) error {
c.writeMessage(StatusPathCreated, "\""+c.Path()+"\" is the current directory")
return nil
}
func (c *clientHandler) checkLISTArgs(args string) string {
result := args
param := strings.ToLower(args)
for _, arg := range supportedlistArgs {
if strings.HasPrefix(param, arg) {
// a check for a non-existent directory error is more appropriate here
// but we cannot assume that the driver implementation will return an
// os.IsNotExist error.
if _, err := c.driver.Stat(args); err != nil {
params := strings.SplitN(args, " ", 2)
if len(params) == 1 {
result = ""
} else {
result = params[1]
}
}
}
}
return result
}
func (c *clientHandler) handleLIST(param string) error {
info := fmt.Sprintf("LIST %v", param)
if files, err := c.getFileList(param); err == nil || err == io.EOF {
if tr, errTr := c.TransferOpen(info); errTr == nil {
err = c.dirTransferLIST(tr, files)
c.TransferClose(err)
return nil
}
} else {
if !c.isCommandAborted() {
c.writeMessage(StatusFileActionNotTaken, fmt.Sprintf("Could not list: %v", err))
}
}
return nil
}
func (c *clientHandler) handleNLST(param string) error {
info := fmt.Sprintf("NLST %v", param)
if files, err := c.getFileList(param); err == nil || err == io.EOF {
if tr, errTrOpen := c.TransferOpen(info); errTrOpen == nil {
err = c.dirTransferNLST(tr, files)
c.TransferClose(err)
return nil
}
} else {
if !c.isCommandAborted() {
c.writeMessage(StatusFileActionNotTaken, fmt.Sprintf("Could not list: %v", err))
}
}
return nil
}
func (c *clientHandler) dirTransferNLST(w io.Writer, files []os.FileInfo) error {
if len(files) == 0 {
_, err := w.Write([]byte(""))
return err
}
for _, file := range files {
if _, err := fmt.Fprintf(w, "%s\r\n", file.Name()); err != nil {
return err
}
}
return nil
}
func (c *clientHandler) handleMLSD(param string) error {
if c.server.settings.DisableMLSD && !c.isCommandAborted() {
c.writeMessage(StatusSyntaxErrorNotRecognised, "MLSD has been disabled")
return nil
}
info := fmt.Sprintf("MLSD %v", param)
if files, err := c.getFileList(param); err == nil || err == io.EOF {
if tr, errTr := c.TransferOpen(info); errTr == nil {
err = c.dirTransferMLSD(tr, files)
c.TransferClose(err)
return nil
}
} else {
if !c.isCommandAborted() {
c.writeMessage(StatusActionNotTaken, fmt.Sprintf("Could not list: %v", err))
}
}
return nil
}
const (
dateFormatStatTime = "Jan _2 15:04" // LIST date formatting with hour and minute
dateFormatStatYear = "Jan _2 2006" // LIST date formatting with year
dateFormatStatOldSwitch = time.Hour * 24 * 30 * 6 // 6 months ago
dateFormatMLSD = "20060102150405" // MLSD date formatting
)
func (c *clientHandler) fileStat(file os.FileInfo) string {
modTime := file.ModTime()
var dateFormat string
if c.connectedAt.Sub(modTime) > dateFormatStatOldSwitch {
dateFormat = dateFormatStatYear
} else {
dateFormat = dateFormatStatTime
}
return fmt.Sprintf(
"%s 1 ftp ftp %12d %s %s",
file.Mode(),
file.Size(),
file.ModTime().Format(dateFormat),
file.Name(),
)
}
// fclairamb (2018-02-13): #64: Removed extra empty line
func (c *clientHandler) dirTransferLIST(w io.Writer, files []os.FileInfo) error {
if len(files) == 0 {
_, err := w.Write([]byte(""))
return err
}
for _, file := range files {
if _, err := fmt.Fprintf(w, "%s\r\n", c.fileStat(file)); err != nil {
return err
}
}
return nil
}
// fclairamb (2018-02-13): #64: Removed extra empty line
func (c *clientHandler) dirTransferMLSD(w io.Writer, files []os.FileInfo) error {
if len(files) == 0 {
_, err := w.Write([]byte(""))
return err
}
for _, file := range files {
if err := c.writeMLSxOutput(w, file); err != nil {
return err
}
}
return nil
}
func (c *clientHandler) writeMLSxOutput(w io.Writer, file os.FileInfo) error {
var listType string
if file.IsDir() {
listType = "dir"
} else {
listType = "file"
}
_, err := fmt.Fprintf(
w,
"Type=%s;Size=%d;Modify=%s; %s\r\n",
listType,
file.Size(),
file.ModTime().Format(dateFormatMLSD),
file.Name(),
)
return err
}
func (c *clientHandler) getFileList(param string) ([]os.FileInfo, error) {
if !c.server.settings.DisableLISTArgs {
param = c.checkLISTArgs(param)
}
directoryPath := c.absPath(param)
if fileList, ok := c.driver.(ClientDriverExtensionFileList); ok {
return fileList.ReadDir(directoryPath)
}
directory, errOpenFile := c.driver.Open(directoryPath)
if errOpenFile != nil {
return nil, errOpenFile
}
defer c.closeDirectory(directoryPath, directory)
return directory.Readdir(-1)
}
func (c *clientHandler) closeDirectory(directoryPath string, directory afero.File) {
if errClose := directory.Close(); errClose != nil {
c.logger.Error("Couldn't close directory", "err", errClose, "directory", directoryPath)
}
}
func quoteDoubling(s string) string {
if !strings.Contains(s, "\"") {
return s
}
return strings.ReplaceAll(s, "\"", `""`)
}