2020-07-29 19:56:56 +00:00
|
|
|
package ftpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2021-01-03 16:03:04 +00:00
|
|
|
"crypto/x509"
|
2020-07-29 19:56:56 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-02-25 20:53:04 +00:00
|
|
|
"os"
|
2020-07-29 19:56:56 +00:00
|
|
|
"path/filepath"
|
2021-02-28 11:10:40 +00:00
|
|
|
"sync"
|
2020-07-29 19:56:56 +00:00
|
|
|
|
|
|
|
ftpserver "github.com/fclairamb/ftpserverlib"
|
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/logger"
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/metric"
|
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/version"
|
2020-07-29 19:56:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Server implements the ftpserverlib MainDriver interface
|
|
|
|
type Server struct {
|
2021-02-28 11:10:40 +00:00
|
|
|
ID int
|
|
|
|
config *Configuration
|
|
|
|
initialMsg string
|
|
|
|
statusBanner string
|
|
|
|
binding Binding
|
2021-04-16 07:00:40 +00:00
|
|
|
tlsConfig *tls.Config
|
2021-02-28 11:10:40 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
verifiedTLSConns map[uint32]bool
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer returns a new FTP server driver
|
2020-12-23 15:12:30 +00:00
|
|
|
func NewServer(config *Configuration, configDir string, binding Binding, id int) *Server {
|
2021-02-28 11:10:40 +00:00
|
|
|
binding.setCiphers()
|
2020-07-29 19:56:56 +00:00
|
|
|
server := &Server{
|
2021-02-28 11:10:40 +00:00
|
|
|
config: config,
|
|
|
|
initialMsg: config.Banner,
|
|
|
|
statusBanner: fmt.Sprintf("SFTPGo %v FTP Server", version.Get().Version),
|
|
|
|
binding: binding,
|
|
|
|
ID: id,
|
|
|
|
verifiedTLSConns: make(map[uint32]bool),
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2020-12-23 15:12:30 +00:00
|
|
|
if config.BannerFile != "" {
|
2020-07-29 19:56:56 +00:00
|
|
|
bannerFilePath := config.BannerFile
|
|
|
|
if !filepath.IsAbs(bannerFilePath) {
|
|
|
|
bannerFilePath = filepath.Join(configDir, bannerFilePath)
|
|
|
|
}
|
2021-02-25 20:53:04 +00:00
|
|
|
bannerContent, err := os.ReadFile(bannerFilePath)
|
2020-07-29 19:56:56 +00:00
|
|
|
if err == nil {
|
|
|
|
server.initialMsg = string(bannerContent)
|
|
|
|
} else {
|
|
|
|
logger.WarnToConsole("unable to read FTPD banner file: %v", err)
|
|
|
|
logger.Warn(logSender, "", "unable to read banner file: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2021-04-16 07:00:40 +00:00
|
|
|
server.buildTLSConfig()
|
2020-12-23 15:12:30 +00:00
|
|
|
return server
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 11:10:40 +00:00
|
|
|
func (s *Server) isTLSConnVerified(id uint32) bool {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
|
|
|
|
return s.verifiedTLSConns[id]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) setTLSConnVerified(id uint32, value bool) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
s.verifiedTLSConns[id] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) cleanTLSConnVerification(id uint32) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
delete(s.verifiedTLSConns, id)
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
// GetSettings returns FTP server settings
|
|
|
|
func (s *Server) GetSettings() (*ftpserver.Settings, error) {
|
2021-04-16 13:08:10 +00:00
|
|
|
if err := s.binding.checkPassiveIP(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-31 17:24:57 +00:00
|
|
|
var portRange *ftpserver.PortRange
|
2020-07-29 19:56:56 +00:00
|
|
|
if s.config.PassivePortRange.Start > 0 && s.config.PassivePortRange.End > s.config.PassivePortRange.Start {
|
|
|
|
portRange = &ftpserver.PortRange{
|
|
|
|
Start: s.config.PassivePortRange.Start,
|
|
|
|
End: s.config.PassivePortRange.End,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var ftpListener net.Listener
|
2020-12-23 15:12:30 +00:00
|
|
|
if common.Config.ProxyProtocol > 0 && s.binding.ApplyProxyConfig {
|
|
|
|
listener, err := net.Listen("tcp", s.binding.GetAddress())
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
2020-12-23 15:12:30 +00:00
|
|
|
logger.Warn(logSender, "", "error starting listener on address %v: %v", s.binding.GetAddress(), err)
|
2020-07-29 19:56:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ftpListener, err = common.Config.GetProxyListener(listener)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "error enabling proxy listener: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 15:12:30 +00:00
|
|
|
if s.binding.TLSMode < 0 || s.binding.TLSMode > 2 {
|
|
|
|
return nil, errors.New("unsupported TLS mode")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.binding.TLSMode > 0 && certMgr == nil {
|
|
|
|
return nil, errors.New("to enable TLS you need to provide a certificate")
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
return &ftpserver.Settings{
|
|
|
|
Listener: ftpListener,
|
2020-12-23 15:12:30 +00:00
|
|
|
ListenAddr: s.binding.GetAddress(),
|
|
|
|
PublicHost: s.binding.ForcePassiveIP,
|
2020-07-29 19:56:56 +00:00
|
|
|
PassiveTransferPortRange: portRange,
|
|
|
|
ActiveTransferPortNon20: s.config.ActiveTransfersPortNon20,
|
|
|
|
IdleTimeout: -1,
|
2020-08-03 16:03:09 +00:00
|
|
|
ConnectionTimeout: 20,
|
|
|
|
Banner: s.statusBanner,
|
2020-12-23 15:12:30 +00:00
|
|
|
TLSRequired: ftpserver.TLSRequirement(s.binding.TLSMode),
|
2020-12-24 17:48:06 +00:00
|
|
|
DisableSite: !s.config.EnableSite,
|
|
|
|
DisableActiveMode: s.config.DisableActiveMode,
|
|
|
|
EnableHASH: s.config.HASHSupport > 0,
|
|
|
|
EnableCOMB: s.config.CombineSupport > 0,
|
2020-12-27 08:48:56 +00:00
|
|
|
DefaultTransferType: ftpserver.TransferTypeBinary,
|
2020-07-29 19:56:56 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClientConnected is called to send the very first welcome message
|
|
|
|
func (s *Server) ClientConnected(cc ftpserver.ClientContext) (string, error) {
|
2021-07-11 13:26:51 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(cc.RemoteAddr().String())
|
2021-05-08 17:45:21 +00:00
|
|
|
common.Connections.AddClientConnection(ipAddr)
|
2021-01-02 13:05:09 +00:00
|
|
|
if common.IsBanned(ipAddr) {
|
|
|
|
logger.Log(logger.LevelDebug, common.ProtocolFTP, "", "connection refused, ip %#v is banned", ipAddr)
|
2021-04-18 10:31:06 +00:00
|
|
|
return "Access denied: banned client IP", common.ErrConnectionDenied
|
2021-01-02 13:05:09 +00:00
|
|
|
}
|
2021-05-08 17:45:21 +00:00
|
|
|
if !common.Connections.IsNewConnectionAllowed(ipAddr) {
|
2020-12-15 18:29:30 +00:00
|
|
|
logger.Log(logger.LevelDebug, common.ProtocolFTP, "", "connection refused, configured limit reached")
|
2021-04-18 10:31:06 +00:00
|
|
|
return "Access denied: max allowed connection exceeded", common.ErrConnectionDenied
|
|
|
|
}
|
2021-04-19 06:14:04 +00:00
|
|
|
_, err := common.LimitRate(common.ProtocolFTP, ipAddr)
|
|
|
|
if err != nil {
|
2021-04-18 10:31:06 +00:00
|
|
|
return fmt.Sprintf("Access denied: %v", err.Error()), err
|
2020-12-15 18:29:30 +00:00
|
|
|
}
|
2021-01-02 13:05:09 +00:00
|
|
|
if err := common.Config.ExecutePostConnectHook(ipAddr, common.ProtocolFTP); err != nil {
|
2021-04-18 10:31:06 +00:00
|
|
|
return "Access denied by post connect hook", err
|
2020-07-30 20:33:49 +00:00
|
|
|
}
|
2020-12-23 15:12:30 +00:00
|
|
|
connID := fmt.Sprintf("%v_%v", s.ID, cc.ID())
|
2020-07-29 19:56:56 +00:00
|
|
|
user := dataprovider.User{}
|
|
|
|
connection := &Connection{
|
2021-06-01 20:28:43 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(connID, common.ProtocolFTP, cc.RemoteAddr().String(), user),
|
2020-07-29 19:56:56 +00:00
|
|
|
clientContext: cc,
|
|
|
|
}
|
|
|
|
common.Connections.Add(connection)
|
|
|
|
return s.initialMsg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClientDisconnected is called when the user disconnects, even if he never authenticated
|
|
|
|
func (s *Server) ClientDisconnected(cc ftpserver.ClientContext) {
|
2021-02-28 11:10:40 +00:00
|
|
|
s.cleanTLSConnVerification(cc.ID())
|
2020-12-23 15:12:30 +00:00
|
|
|
connID := fmt.Sprintf("%v_%v_%v", common.ProtocolFTP, s.ID, cc.ID())
|
2020-07-29 19:56:56 +00:00
|
|
|
common.Connections.Remove(connID)
|
2021-07-11 13:26:51 +00:00
|
|
|
common.Connections.RemoveClientConnection(util.GetIPFromRemoteAddress(cc.RemoteAddr().String()))
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AuthUser authenticates the user and selects an handling driver
|
|
|
|
func (s *Server) AuthUser(cc ftpserver.ClientContext, username, password string) (ftpserver.ClientDriver, error) {
|
2021-02-28 11:10:40 +00:00
|
|
|
loginMethod := dataprovider.LoginMethodPassword
|
|
|
|
if s.isTLSConnVerified(cc.ID()) {
|
|
|
|
loginMethod = dataprovider.LoginMethodTLSCertificateAndPwd
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(cc.RemoteAddr().String())
|
2021-01-02 13:05:09 +00:00
|
|
|
user, err := dataprovider.CheckUserAndPass(username, password, ipAddr, common.ProtocolFTP)
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
2021-01-26 17:05:44 +00:00
|
|
|
user.Username = username
|
2021-02-28 11:10:40 +00:00
|
|
|
updateLoginMetrics(&user, ipAddr, loginMethod, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
return nil, dataprovider.ErrInvalidCredentials
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 11:10:40 +00:00
|
|
|
connection, err := s.validateUser(user, cc, loginMethod)
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2021-02-28 11:10:40 +00:00
|
|
|
defer updateLoginMetrics(&user, ipAddr, loginMethod, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
connection.Log(logger.LevelInfo, "User id: %d, logged in with FTP, username: %#v, home_dir: %#v remote addr: %#v",
|
2021-01-03 16:03:04 +00:00
|
|
|
user.ID, user.Username, user.HomeDir, ipAddr)
|
2021-02-16 18:11:36 +00:00
|
|
|
dataprovider.UpdateLastLogin(&user) //nolint:errcheck
|
2020-07-29 19:56:56 +00:00
|
|
|
return connection, nil
|
|
|
|
}
|
|
|
|
|
2021-02-28 11:10:40 +00:00
|
|
|
// VerifyConnection checks whether a user should be authenticated using a client certificate without prompting for a password
|
|
|
|
func (s *Server) VerifyConnection(cc ftpserver.ClientContext, user string, tlsConn *tls.Conn) (ftpserver.ClientDriver, error) {
|
|
|
|
if !s.binding.isMutualTLSEnabled() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
s.setTLSConnVerified(cc.ID(), false)
|
|
|
|
if tlsConn != nil {
|
|
|
|
state := tlsConn.ConnectionState()
|
|
|
|
if len(state.PeerCertificates) > 0 {
|
2021-07-11 13:26:51 +00:00
|
|
|
ipAddr := util.GetIPFromRemoteAddress(cc.RemoteAddr().String())
|
2021-02-28 11:10:40 +00:00
|
|
|
dbUser, err := dataprovider.CheckUserBeforeTLSAuth(user, ipAddr, common.ProtocolFTP, state.PeerCertificates[0])
|
|
|
|
if err != nil {
|
|
|
|
dbUser.Username = user
|
|
|
|
updateLoginMetrics(&dbUser, ipAddr, dataprovider.LoginMethodTLSCertificate, err)
|
2021-05-06 19:35:43 +00:00
|
|
|
return nil, dataprovider.ErrInvalidCredentials
|
2021-02-28 11:10:40 +00:00
|
|
|
}
|
|
|
|
if dbUser.IsTLSUsernameVerificationEnabled() {
|
|
|
|
dbUser, err = dataprovider.CheckUserAndTLSCert(user, ipAddr, common.ProtocolFTP, state.PeerCertificates[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.setTLSConnVerified(cc.ID(), true)
|
|
|
|
|
|
|
|
if dbUser.IsLoginMethodAllowed(dataprovider.LoginMethodTLSCertificate, nil) {
|
|
|
|
connection, err := s.validateUser(dbUser, cc, dataprovider.LoginMethodTLSCertificate)
|
|
|
|
|
|
|
|
defer updateLoginMetrics(&dbUser, ipAddr, dataprovider.LoginMethodTLSCertificate, err)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
connection.Log(logger.LevelInfo, "User id: %d, logged in with FTP using a TLS certificate, username: %#v, home_dir: %#v remote addr: %#v",
|
|
|
|
dbUser.ID, dbUser.Username, dbUser.HomeDir, ipAddr)
|
|
|
|
dataprovider.UpdateLastLogin(&dbUser) //nolint:errcheck
|
|
|
|
return connection, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-04-16 07:00:40 +00:00
|
|
|
func (s *Server) buildTLSConfig() {
|
2020-12-23 15:12:30 +00:00
|
|
|
if certMgr != nil {
|
2021-04-16 07:00:40 +00:00
|
|
|
s.tlsConfig = &tls.Config{
|
2021-02-28 11:10:40 +00:00
|
|
|
GetCertificate: certMgr.GetCertificateFunc(),
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
CipherSuites: s.binding.ciphers,
|
|
|
|
PreferServerCipherSuites: true,
|
2020-12-29 08:20:09 +00:00
|
|
|
}
|
2021-02-28 11:10:40 +00:00
|
|
|
if s.binding.isMutualTLSEnabled() {
|
2021-04-16 07:00:40 +00:00
|
|
|
s.tlsConfig.ClientCAs = certMgr.GetRootCAs()
|
|
|
|
s.tlsConfig.VerifyConnection = s.verifyTLSConnection
|
2021-02-28 11:10:40 +00:00
|
|
|
switch s.binding.ClientAuthType {
|
|
|
|
case 1:
|
2021-04-16 07:00:40 +00:00
|
|
|
s.tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
|
2021-02-28 11:10:40 +00:00
|
|
|
case 2:
|
2021-04-16 07:00:40 +00:00
|
|
|
s.tlsConfig.ClientAuth = tls.VerifyClientCertIfGiven
|
2021-02-28 11:10:40 +00:00
|
|
|
}
|
2020-12-29 08:20:09 +00:00
|
|
|
}
|
2021-04-16 07:00:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTLSConfig returns the TLS configuration for this server
|
|
|
|
func (s *Server) GetTLSConfig() (*tls.Config, error) {
|
|
|
|
if s.tlsConfig != nil {
|
|
|
|
return s.tlsConfig, nil
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
return nil, errors.New("no TLS certificate configured")
|
|
|
|
}
|
|
|
|
|
2021-01-03 16:03:04 +00:00
|
|
|
func (s *Server) verifyTLSConnection(state tls.ConnectionState) error {
|
|
|
|
if certMgr != nil {
|
|
|
|
var clientCrt *x509.Certificate
|
|
|
|
var clientCrtName string
|
|
|
|
if len(state.PeerCertificates) > 0 {
|
|
|
|
clientCrt = state.PeerCertificates[0]
|
|
|
|
clientCrtName = clientCrt.Subject.String()
|
|
|
|
}
|
|
|
|
if len(state.VerifiedChains) == 0 {
|
2021-02-28 11:10:40 +00:00
|
|
|
if s.binding.ClientAuthType == 2 {
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-03 16:03:04 +00:00
|
|
|
logger.Warn(logSender, "", "TLS connection cannot be verified: unable to get verification chain")
|
|
|
|
return errors.New("TLS connection cannot be verified: unable to get verification chain")
|
|
|
|
}
|
|
|
|
for _, verifiedChain := range state.VerifiedChains {
|
|
|
|
var caCrt *x509.Certificate
|
|
|
|
if len(verifiedChain) > 0 {
|
|
|
|
caCrt = verifiedChain[len(verifiedChain)-1]
|
|
|
|
}
|
|
|
|
if certMgr.IsRevoked(clientCrt, caCrt) {
|
|
|
|
logger.Debug(logSender, "", "tls handshake error, client certificate %#v has beed revoked", clientCrtName)
|
|
|
|
return common.ErrCrtRevoked
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-28 11:10:40 +00:00
|
|
|
func (s *Server) validateUser(user dataprovider.User, cc ftpserver.ClientContext, loginMethod string) (*Connection, error) {
|
2020-12-24 17:48:06 +00:00
|
|
|
connectionID := fmt.Sprintf("%v_%v_%v", common.ProtocolFTP, s.ID, cc.ID())
|
2020-07-29 19:56:56 +00:00
|
|
|
if !filepath.IsAbs(user.HomeDir) {
|
|
|
|
logger.Warn(logSender, connectionID, "user %#v has an invalid home dir: %#v. Home dir must be an absolute path, login not allowed",
|
|
|
|
user.Username, user.HomeDir)
|
|
|
|
return nil, fmt.Errorf("cannot login user with invalid home dir: %#v", user.HomeDir)
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
if util.IsStringInSlice(common.ProtocolFTP, user.Filters.DeniedProtocols) {
|
2020-08-17 10:49:20 +00:00
|
|
|
logger.Debug(logSender, connectionID, "cannot login user %#v, protocol FTP is not allowed", user.Username)
|
2021-03-21 18:15:47 +00:00
|
|
|
return nil, fmt.Errorf("protocol FTP is not allowed for user %#v", user.Username)
|
2020-08-17 10:49:20 +00:00
|
|
|
}
|
2021-02-28 11:10:40 +00:00
|
|
|
if !user.IsLoginMethodAllowed(loginMethod, nil) {
|
|
|
|
logger.Debug(logSender, connectionID, "cannot login user %#v, %v login method is not allowed", user.Username, loginMethod)
|
2021-03-21 18:15:47 +00:00
|
|
|
return nil, fmt.Errorf("login method %v is not allowed for user %#v", loginMethod, user.Username)
|
2020-08-19 17:36:12 +00:00
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
if user.MaxSessions > 0 {
|
|
|
|
activeSessions := common.Connections.GetActiveSessions(user.Username)
|
|
|
|
if activeSessions >= user.MaxSessions {
|
|
|
|
logger.Debug(logSender, connectionID, "authentication refused for user: %#v, too many open sessions: %v/%v", user.Username,
|
|
|
|
activeSessions, user.MaxSessions)
|
|
|
|
return nil, fmt.Errorf("too many open sessions: %v", activeSessions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
remoteAddr := cc.RemoteAddr().String()
|
|
|
|
if !user.IsLoginFromAddrAllowed(remoteAddr) {
|
|
|
|
logger.Debug(logSender, connectionID, "cannot login user %#v, remote address is not allowed: %v", user.Username, remoteAddr)
|
2021-03-21 18:15:47 +00:00
|
|
|
return nil, fmt.Errorf("login for user %#v is not allowed from this address: %v", user.Username, remoteAddr)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
err := user.CheckFsRoot(connectionID)
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
errClose := user.CloseFs()
|
|
|
|
logger.Warn(logSender, connectionID, "unable to check fs root: %v close fs error: %v", err, errClose)
|
2021-06-05 14:07:09 +00:00
|
|
|
return nil, common.ErrInternalFailure
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
connection := &Connection{
|
2021-06-01 20:28:43 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(fmt.Sprintf("%v_%v", s.ID, cc.ID()), common.ProtocolFTP, remoteAddr, user),
|
2020-07-29 19:56:56 +00:00
|
|
|
clientContext: cc,
|
|
|
|
}
|
|
|
|
err = common.Connections.Swap(connection)
|
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
err = user.CloseFs()
|
|
|
|
logger.Warn(logSender, connectionID, "unable to swap connection, close fs error: %v", err)
|
2021-06-05 14:07:09 +00:00
|
|
|
return nil, common.ErrInternalFailure
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
return connection, nil
|
|
|
|
}
|
|
|
|
|
2021-02-28 11:10:40 +00:00
|
|
|
func updateLoginMetrics(user *dataprovider.User, ip, loginMethod string, err error) {
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.AddLoginAttempt(loginMethod)
|
2021-06-05 14:07:09 +00:00
|
|
|
if err != nil && err != common.ErrInternalFailure {
|
2021-02-28 11:10:40 +00:00
|
|
|
logger.ConnectionFailedLog(user.Username, ip, loginMethod,
|
2020-08-12 14:15:12 +00:00
|
|
|
common.ProtocolFTP, err.Error())
|
2021-01-02 13:05:09 +00:00
|
|
|
event := common.HostEventLoginFailed
|
2021-07-11 13:26:51 +00:00
|
|
|
if _, ok := err.(*util.RecordNotFoundError); ok {
|
2021-01-02 13:05:09 +00:00
|
|
|
event = common.HostEventUserNotFound
|
|
|
|
}
|
|
|
|
common.AddDefenderEvent(ip, event)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.AddLoginResult(loginMethod, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
dataprovider.ExecutePostLoginHook(user, loginMethod, ip, common.ProtocolFTP, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|