forked from fclairamb/ftpserverlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_auth.go
48 lines (39 loc) · 1.24 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
package ftpserver // nolint
import (
"fmt"
)
// Handle the "USER" command
func (c *clientHandler) handleUSER(param string) error {
if c.server.settings.TLSRequired == MandatoryEncryption && !c.HasTLSForControl() {
c.writeMessage(StatusServiceNotAvailable, "TLS is required")
return nil
}
if c.HasTLSForControl() && c.server.settings.SkipPasswordIfClientCertMatchesUser {
clientCertificates := c.controlTLSConn.ConnectionState().PeerCertificates
// TODO: Add check for subjectAltName as well
if len(clientCertificates) > 0 && clientCertificates[0].Subject.CommonName == param {
c.user = param
c.writeMessage(StatusUserLoggedIn, "User logged in, no password required.")
return nil
}
}
c.user = param
c.writeMessage(StatusUserOK, "OK")
return nil
}
// Handle the "PASS" command
func (c *clientHandler) handlePASS(param string) error {
var err error
c.driver, err = c.server.driver.AuthUser(c, c.user, param)
switch {
case err == nil:
c.writeMessage(StatusUserLoggedIn, "Password ok, continue")
case err != nil:
c.writeMessage(StatusNotLoggedIn, fmt.Sprintf("Authentication problem: %v", err))
c.disconnect()
default:
c.writeMessage(StatusNotLoggedIn, "I can't deal with you (nil driver)")
c.disconnect()
}
return nil
}