-
Notifications
You must be signed in to change notification settings - Fork 100
/
handle_auth.go
102 lines (79 loc) · 2.01 KB
/
handle_auth.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
package ftpserver
import (
"crypto/tls"
"fmt"
)
// Handle the "USER" command
func (c *clientHandler) handleUSER(user string) error {
if verifier, ok := c.server.driver.(MainDriverExtensionUserVerifier); ok {
err := verifier.PreAuthUser(c, user)
if err != nil {
c.writeMessage(StatusNotLoggedIn, fmt.Sprintf("User rejected: %v", err))
c.disconnect()
return nil
}
}
if c.isTLSRequired() && !c.HasTLSForControl() {
c.writeMessage(StatusServiceNotAvailable, "TLS is required")
c.disconnect()
return nil
}
if c.HasTLSForControl() {
if c.handleUserTLS(user) {
return nil
}
}
c.user = user
c.writeMessage(StatusUserOK, "OK")
return nil
}
func (c *clientHandler) handleUserTLS(user string) bool {
verifier, interfaceFound := c.server.driver.(MainDriverExtensionTLSVerifier)
if !interfaceFound {
return false
}
tlsConn, interfaceFound := c.conn.(*tls.Conn)
if !interfaceFound {
return false
}
driver, err := verifier.VerifyConnection(c, user, tlsConn)
if err != nil {
c.writeMessage(StatusNotLoggedIn, fmt.Sprintf("TLS verification failed: %v", err))
c.disconnect()
return true
}
if driver != nil {
c.user = user
c.driver = driver
c.writeMessage(StatusUserLoggedIn, "TLS certificate ok, continue")
return true
}
return false
}
// Handle the "PASS" command
func (c *clientHandler) handlePASS(param string) error {
var err error
var msg string
c.driver, err = c.server.driver.AuthUser(c, c.user, param)
dpa, ok := c.server.driver.(MainDriverExtensionPostAuthMessage)
if ok {
msg = dpa.PostAuthMessage(c, c.user, err)
}
switch {
case err == nil && c.driver == nil:
c.writeMessage(StatusNotLoggedIn, "Unexpected exception (driver is nil)")
c.disconnect()
case err != nil:
if msg == "" {
msg = fmt.Sprintf("Authentication error: %v", err)
}
c.writeMessage(StatusNotLoggedIn, msg)
c.disconnect()
default: // err == nil && c.driver != nil
if msg == "" {
msg = "Password ok, continue"
}
c.writeMessage(StatusUserLoggedIn, msg)
}
return nil
}