-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.go
More file actions
447 lines (364 loc) · 11.5 KB
/
Copy pathsession.go
File metadata and controls
447 lines (364 loc) · 11.5 KB
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
// 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 ftp
import (
"bufio"
"context"
"crypto/rand"
"crypto/tls"
"encoding/hex"
"fmt"
"io"
"log"
"math/big"
"net"
"path/filepath"
"runtime"
"runtime/debug"
"strconv"
"strings"
"time"
)
const (
defaultTimeout = 60 * time.Second
defaultWelcomeMessage = "Welcome to the Go FTP Server"
)
type (
// Context represents a context the driver may want to know.
Context struct {
CMD string // Request command on this request.
Data map[string]any // Share data between middleware.
Param string // Request param on this request.
Sess *Session
}
// Session represents a session between an FTP client and the server.
Session struct {
ctx context.Context
dataConn DataSocket
Conn net.Conn
controlReader *bufio.Reader
controlWriter *bufio.Writer
server *Server
Data map[string]any // Shared data between different commands.
id string
curDir string
reqUser string
user string
renameFrom string
preCommand string
clientSoft string
lastFilePos int64
closed bool
tls bool
}
)
func newContext(sess *Session, cmd string, param string, data map[string]any) *Context {
if data == nil {
data = make(map[string]any)
}
return &Context{
CMD: cmd,
Data: data,
Param: param,
Sess: sess,
}
}
func (sess *Session) Context() context.Context {
if sess.ctx != nil {
return sess.ctx
}
return context.Background()
}
// LocalAddr returns the local FTP server's address.
func (sess *Session) LocalAddr() net.Addr {
return sess.Conn.LocalAddr()
}
// RemoteAddr returns the remote FTP client's address.
func (sess *Session) RemoteAddr() net.Addr {
return sess.Conn.RemoteAddr()
}
// LoginUser returns the logged in user's name.
func (sess *Session) LoginUser() string {
return sess.user
}
// IsLogin returns if the user has logged in.
func (sess *Session) IsLogin() bool {
return len(sess.user) > 0
}
// PublicIP returns the public ip of the server.
func (sess *Session) PublicIP() string {
return sess.server.PublicIP
}
// Options returns the server options.
func (sess *Session) Options() *Options {
return sess.server.Options
}
// Server returns the server of the session.
func (sess *Session) Server() *Server {
return sess.server
}
// DataConn returns the data connection.
func (sess *Session) DataConn() DataSocket {
return sess.dataConn
}
func (sess *Session) passiveListenIP() string {
var listenIP string
tcpAddr, ok := sess.Conn.LocalAddr().(*net.TCPAddr)
if ok {
listenIP = tcpAddr.IP.String()
}
if len(sess.PublicIP()) > 0 {
listenIP = sess.PublicIP()
}
if listenIP == "::1" {
return listenIP
}
lastIdx := strings.LastIndex(listenIP, ":")
if lastIdx <= 0 {
return listenIP
}
return listenIP[:lastIdx]
}
// PassivePort returns the port which could be used by passive mode.
func (sess *Session) PassivePort() int {
if len(sess.server.PassivePorts) == 0 {
// Let the system automatically choose one port.
return 0
}
// Port range format: "min-max".
const expectedRangeParts = 2
portRange := strings.Split(sess.server.PassivePorts, "-")
if len(portRange) != expectedRangeParts {
log.Println("invalid port range format: expected min-max")
return 0
}
// TODO: Handle errors.
minPort, _ := strconv.Atoi(strings.TrimSpace(portRange[0]))
maxPort, _ := strconv.Atoi(strings.TrimSpace(portRange[1]))
rangeSize := maxPort - minPort
n, _ := rand.Int(rand.Reader, big.NewInt(int64(rangeSize)))
return minPort + int(n.Int64())
}
// newSessionID returns a random 20-character hexadecimal string that can be used as a unique session ID.
func newSessionID() (string, error) {
const hexCharsPerByte = 2 // Each byte encodes to 2 hex characters.
const sessionIDLength = 20
bytesNeeded := sessionIDLength / hexCharsPerByte
randomBytes := make([]byte, bytesNeeded)
if _, err := rand.Read(randomBytes); err != nil {
return "", fmt.Errorf("generate session id: %w", err)
}
return hex.EncodeToString(randomBytes), nil
}
// Serve starts an endless loop that reads FTP commands from the client and
// responds appropriately. terminated is a channel that will receive a true
// message when the connection closes. This loop will be running inside a
// goroutine, so use this channel to be notified when the connection can be
// cleaned up.
func (sess *Session) Serve() {
defer sess.Close()
// Leave a slight delay to close the context (needed to allow the connection to gracefully close).
defer func() {
if recovery := recover(); recovery != nil {
sess.log(fmt.Sprintf("recovered from handle panic; recovered=%v; stack=%v", recovery, string(debug.Stack())))
}
}()
sess.log("Connection Established")
sess.writeMessage(CodeServiceReady, sess.server.WelcomeMessage)
// Read commands.
for {
line, err := sess.controlReader.ReadString('\n')
if err != nil {
if err != io.EOF {
sess.log(fmt.Sprint("Read error:", err))
}
break
}
sess.server.notifiers.BeforeCommand(&Context{
Sess: sess,
}, strings.Trim(line, "\r\n"))
sess.receiveLine(line)
// QUIT command closes connection, break to avoid error on reading from a closed socket.
if sess.closed {
break
}
}
sess.log("Connection Terminated")
}
// Close will manually close this connection, even if the client isn't ready.
func (sess *Session) Close() error {
sess.closed = true
sess.reqUser = ""
sess.user = ""
if err := sess.Conn.Close(); err != nil {
return fmt.Errorf("close connection: %w", err)
}
if sess.dataConn != nil {
sess.dataConn = nil
if err := sess.dataConn.Close(); err != nil {
return fmt.Errorf("close data connection: %w", err)
}
}
return nil
}
func (sess *Session) upgradeToTLS() error {
sess.log("Upgrading connection to TLS")
tlsConn := tls.Server(sess.Conn, sess.server.TLSConfig)
if err := tlsConn.HandshakeContext(sess.Context()); err != nil {
return fmt.Errorf("tls handshake: %w", err)
}
sess.Conn = tlsConn
sess.controlReader = bufio.NewReader(tlsConn)
sess.controlWriter = bufio.NewWriter(tlsConn)
sess.tls = true
return nil
}
// receiveLine accepts a single line FTP command and co-ordinates an appropriate response.
func (sess *Session) receiveLine(line string) {
defer func() {
if err := recover(); err != nil {
// 64KB buffer for stack trace.
const stackBufferSize = 64 * 1024
buf := make([]byte, stackBufferSize)
buf = buf[:runtime.Stack(buf, false)]
sess.logf("handler crashed with error:%v\n%s", err, buf)
}
}()
command, param := sess.parseLine(line)
cmdGiven := strings.ToUpper(command)
sess.server.Logger.PrintCommand(sess.id, command, param)
sess.server.CommandsMu.RLock()
defer sess.server.CommandsMu.RUnlock()
cmdObj, ok := sess.server.Commands[cmdGiven]
sess.server.notifiers.AfterCommand(&Context{Sess: sess}, strings.Trim(line, "\r\n"), ok)
if !ok {
sess.writeMessage(CodeSyntaxError, "Command not found")
return
}
if cmdObj.RequireParam() && param == "" {
sess.writeMessage(CodeFileNameNotAllowed, "action aborted, required param missing")
return
}
if sess.server.Options.ForceTLS && !sess.tls && (cmdObj != sess.server.Commands["AUTH"] || param != "TLS") {
sess.writeMessage(CodeRequestDeniedPolicy, "Request denied for policy reasons. AUTH TLS required.")
return
}
if cmdObj.RequireAuth() && sess.user == "" {
sess.writeMessage(CodeNotLoggedIn, "not logged in")
return
}
cmdObj.Execute(sess, param)
sess.preCommand = cmdGiven
}
// parseLine splits a command line into command and parameters.
// It separates the first word (command) from the rest of the line (parameters).
func (sess *Session) parseLine(line string) (string, string) {
// Split into command and parameters.
const maxParts = 2
parts := strings.SplitN(strings.Trim(line, "\r\n"), " ", maxParts)
if len(parts) == 0 {
return "", ""
}
command := parts[0]
parameters := ""
if len(parts) == maxParts {
parameters = parts[1]
}
return command, parameters
}
func (sess *Session) WriteMessage(code int, message string) {
sess.writeMessage(code, message)
}
// writeMessage will send a standard FTP response back to the client.
func (sess *Session) writeMessage(code int, message string) {
sess.server.Logger.PrintResponse(sess.id, code, message)
line := fmt.Sprintf("%d %s\r\n", code, message)
_, _ = sess.controlWriter.WriteString(line)
sess.controlWriter.Flush()
}
// writeMessage will send a standard FTP response back to the client.
func (sess *Session) writeMessageMultiline(code int, message string) {
sess.server.Logger.PrintResponse(sess.id, code, message)
line := fmt.Sprintf("%d-%s\r\n%d END\r\n", code, message, code)
_, _ = sess.controlWriter.WriteString(line)
sess.controlWriter.Flush()
}
func (sess *Session) BuildPath(filename string) string {
return sess.buildPath(filename)
}
// buildPath takes a client supplied path or filename and generates a safe
// absolute path within their account sandbox.
//
// buildpath("/")
// => "/"
// buildpath("one.txt")
// => "/one.txt"
// buildpath("/files/two.txt")
// => "/files/two.txt"
// buildpath("files/two.txt")
// => "/files/two.txt"
// buildpath("/../../../../etc/passwd")
// => "/etc/passwd"
//
// The driver implementation is responsible for deciding how to treat this path. They must not read the path off disk.
// They probably want to prefix the path with something to scope the users access to a sandbox.
func (sess *Session) buildPath(filename string) string {
fullPath := filepath.Clean(sess.curDir)
if len(filename) > 0 && filename[0:1] == "/" {
fullPath = filepath.Clean(filename)
} else if len(filename) > 0 && filename != "-a" {
fullPath = filepath.Clean(sess.curDir + "/" + filename)
}
fullPath = strings.ReplaceAll(fullPath, "//", "/")
fullPath = strings.ReplaceAll(fullPath, string(filepath.Separator), "/")
return fullPath
}
// sendOutofbandData will send a string to the client via the currently open
// data socket. Assumes the socket is open and ready to be used.
func (sess *Session) sendOutofbandData(data []byte) {
bytes := len(data)
if sess.dataConn != nil {
_, _ = sess.dataConn.Write(data)
sess.dataConn.Close()
sess.dataConn = nil
}
message := "Closing data connection, sent " + strconv.Itoa(bytes) + " bytes"
sess.writeMessage(CodeClosingDataConnection, message)
}
// sendOutOfBandDataWriter sends data through the out-of-band data connection.
func (sess *Session) sendOutOfBandDataWriter(data io.ReadCloser) (err error) {
defer func() {
if sess.dataConn != nil {
if closeErr := sess.dataConn.Close(); closeErr != nil && err == nil {
err = fmt.Errorf("close data connection: %w", closeErr)
}
sess.dataConn = nil
}
}()
bytesWritten, writeErr := io.Copy(sess.dataConn, data)
if writeErr != nil {
return fmt.Errorf("write out-of-band data: %w", writeErr)
}
message := fmt.Sprintf("Closing data connection, sent %d bytes", bytesWritten)
sess.writeMessage(CodeClosingDataConnection, message)
return nil
}
// changeCurDir changes the current directory after validating it exists.
func (sess *Session) changeCurDir(path string) error {
fInfo, err := sess.server.Driver.Stat(&Context{Sess: sess}, path)
if err != nil {
return fmt.Errorf("stat directory: %w", err)
}
if !fInfo.IsDir() {
return fmt.Errorf("path is not a directory: %s", path)
}
sess.curDir = path
return nil
}
func (sess *Session) log(message any) {
sess.server.logger.Print(sess.id, message)
}
func (sess *Session) logf(format string, v ...any) {
sess.server.logger.Printf(sess.id, format, v...)
}