2024-01-01 10:31:45 +00:00
|
|
|
// Copyright (C) 2019 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
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"
|
2024-07-24 16:27:13 +00:00
|
|
|
"slices"
|
2020-07-29 19:56:56 +00:00
|
|
|
|
|
|
|
ftpserver "github.com/fclairamb/ftpserverlib"
|
2023-05-12 16:34:59 +00:00
|
|
|
"github.com/sftpgo/sdk/plugin/notifier"
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/metric"
|
2023-05-12 16:34:59 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/plugin"
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/version"
|
2020-07-29 19:56:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Server implements the ftpserverlib MainDriver interface
|
|
|
|
type Server struct {
|
2023-08-18 12:39:28 +00:00
|
|
|
ID int
|
|
|
|
config *Configuration
|
|
|
|
initialMsg string
|
|
|
|
statusBanner string
|
|
|
|
binding Binding
|
|
|
|
tlsConfig *tls.Config
|
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()
|
2024-05-01 17:42:09 +00:00
|
|
|
vers := version.GetServerVersion("_", false)
|
2020-07-29 19:56:56 +00:00
|
|
|
server := &Server{
|
2023-08-18 12:39:28 +00:00
|
|
|
config: config,
|
2024-05-01 17:42:09 +00:00
|
|
|
initialMsg: vers,
|
|
|
|
statusBanner: fmt.Sprintf("%s FTP Server", vers),
|
2023-08-18 12:39:28 +00:00
|
|
|
binding: binding,
|
|
|
|
ID: id,
|
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 {
|
2024-05-08 17:01:58 +00:00
|
|
|
server.initialMsg = util.BytesToString(bannerContent)
|
2020-07-29 19:56:56 +00:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2021-08-05 16:38:15 +00:00
|
|
|
if err := s.binding.checkSecuritySettings(); 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
|
2021-08-05 16:38:15 +00:00
|
|
|
if s.binding.HasProxy() {
|
2020-12-23 15:12:30 +00:00
|
|
|
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
|
|
|
|
}
|
2021-08-05 16:38:15 +00:00
|
|
|
if s.binding.TLSMode == 2 && s.tlsConfig != nil {
|
|
|
|
ftpListener = tls.NewListener(ftpListener, s.tlsConfig)
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-20 14:00:36 +00:00
|
|
|
if !s.binding.isTLSModeValid() {
|
|
|
|
return nil, fmt.Errorf("unsupported TLS mode: %d", s.binding.TLSMode)
|
2020-12-23 15:12:30 +00:00
|
|
|
}
|
|
|
|
|
2023-08-20 14:00:36 +00:00
|
|
|
if !s.binding.isTLSSessionReuseValid() {
|
|
|
|
return nil, fmt.Errorf("unsupported TLS reuse mode %d", s.binding.TLSSessionReuse)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.binding.TLSMode > 0 || s.binding.TLSSessionReuse > 0) && certMgr == nil {
|
2020-12-23 15:12:30 +00:00
|
|
|
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(),
|
2021-11-25 11:45:09 +00:00
|
|
|
PublicIPResolver: s.binding.passiveIPResolver,
|
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),
|
2023-08-20 14:00:36 +00:00
|
|
|
TLSSessionReuse: ftpserver.TLSSessionReuse(s.binding.TLSSessionReuse),
|
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,
|
2024-05-02 17:00:29 +00:00
|
|
|
IgnoreASCIITranferType: s.binding.IgnoreASCIITransferType == 1,
|
2021-08-05 16:38:15 +00:00
|
|
|
ActiveConnectionsCheck: ftpserver.DataConnectionRequirement(s.binding.ActiveConnectionsSecurity),
|
|
|
|
PasvConnectionsCheck: ftpserver.DataConnectionRequirement(s.binding.PassiveConnectionsSecurity),
|
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-20 15:22:08 +00:00
|
|
|
cc.SetDebug(s.binding.Debug)
|
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)
|
2023-02-09 08:33:33 +00:00
|
|
|
if common.IsBanned(ipAddr, common.ProtocolFTP) {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Log(logger.LevelDebug, common.ProtocolFTP, "", "connection refused, ip %q 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
|
|
|
}
|
2023-02-09 08:33:33 +00:00
|
|
|
if err := common.Connections.IsNewConnectionAllowed(ipAddr, common.ProtocolFTP); err != nil {
|
2022-10-22 09:56:41 +00:00
|
|
|
logger.Log(logger.LevelDebug, common.ProtocolFTP, "", "connection not allowed from ip %q: %v", ipAddr, err)
|
|
|
|
return "Access denied", err
|
2021-04-18 10:31:06 +00:00
|
|
|
}
|
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 {
|
2023-02-28 17:01:09 +00:00
|
|
|
return "Access denied", 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-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(connID, common.ProtocolFTP, cc.LocalAddr().String(),
|
|
|
|
cc.RemoteAddr().String(), user),
|
|
|
|
clientContext: cc,
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2022-04-14 17:07:41 +00:00
|
|
|
err = common.Connections.Add(connection)
|
|
|
|
return s.initialMsg, err
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClientDisconnected is called when the user disconnects, even if he never authenticated
|
|
|
|
func (s *Server) ClientDisconnected(cc ftpserver.ClientContext) {
|
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
|
2023-08-18 12:39:28 +00:00
|
|
|
if verified, ok := cc.Extra().(bool); ok && verified {
|
2021-02-28 11:10:40 +00:00
|
|
|
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
|
|
|
|
}
|
2022-03-03 11:44:56 +00:00
|
|
|
setStartDirectory(user.Filters.StartDirectory, cc)
|
2024-05-03 16:47:01 +00:00
|
|
|
connection.Log(logger.LevelInfo, "User %q logged in with %q from ip %q, TLS enabled? %t",
|
|
|
|
user.Username, loginMethod, ipAddr, cc.HasTLSForControl())
|
2021-08-19 13:51:43 +00:00
|
|
|
dataprovider.UpdateLastLogin(&user)
|
2020-07-29 19:56:56 +00:00
|
|
|
return connection, nil
|
|
|
|
}
|
|
|
|
|
2022-07-26 16:51:39 +00:00
|
|
|
// PreAuthUser implements the MainDriverExtensionUserVerifier interface
|
|
|
|
func (s *Server) PreAuthUser(cc ftpserver.ClientContext, username string) error {
|
|
|
|
if s.binding.TLSMode == 0 && s.tlsConfig != nil {
|
|
|
|
user, err := dataprovider.GetFTPPreAuthUser(username, util.GetIPFromRemoteAddress(cc.RemoteAddr().String()))
|
|
|
|
if err == nil {
|
|
|
|
if user.Filters.FTPSecurity == 1 {
|
|
|
|
return cc.SetTLSRequirement(ftpserver.MandatoryEncryption)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-25 17:49:03 +00:00
|
|
|
if !errors.Is(err, util.ErrNotFound) {
|
2022-07-27 16:54:25 +00:00
|
|
|
logger.Error(logSender, fmt.Sprintf("%v_%v_%v", common.ProtocolFTP, s.ID, cc.ID()),
|
|
|
|
"unable to get user on pre auth: %v", err)
|
2022-07-26 16:51:39 +00:00
|
|
|
return common.ErrInternalFailure
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-05 16:38:15 +00:00
|
|
|
// WrapPassiveListener implements the MainDriverExtensionPassiveWrapper interface
|
|
|
|
func (s *Server) WrapPassiveListener(listener net.Listener) (net.Listener, error) {
|
|
|
|
if s.binding.HasProxy() {
|
|
|
|
return common.Config.GetProxyListener(listener)
|
|
|
|
}
|
|
|
|
return listener, 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
|
|
|
|
}
|
2023-08-18 12:39:28 +00:00
|
|
|
cc.SetExtra(false)
|
2021-02-28 11:10:40 +00:00
|
|
|
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
|
|
|
}
|
2024-01-14 20:36:23 +00:00
|
|
|
if dbUser.IsTLSVerificationEnabled() {
|
2021-02-28 11:10:40 +00:00
|
|
|
dbUser, err = dataprovider.CheckUserAndTLSCert(user, ipAddr, common.ProtocolFTP, state.PeerCertificates[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-18 12:39:28 +00:00
|
|
|
cc.SetExtra(true)
|
2021-02-28 11:10:40 +00:00
|
|
|
|
2023-08-04 18:56:23 +00:00
|
|
|
if dbUser.IsLoginMethodAllowed(dataprovider.LoginMethodTLSCertificate, common.ProtocolFTP) {
|
2021-02-28 11:10:40 +00:00
|
|
|
connection, err := s.validateUser(dbUser, cc, dataprovider.LoginMethodTLSCertificate)
|
|
|
|
|
|
|
|
defer updateLoginMetrics(&dbUser, ipAddr, dataprovider.LoginMethodTLSCertificate, err)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-03 11:44:56 +00:00
|
|
|
setStartDirectory(dbUser.Filters.StartDirectory, cc)
|
2023-02-27 18:02:43 +00:00
|
|
|
connection.Log(logger.LevelInfo, "User id: %d, logged in with FTP using a TLS certificate, username: %q, home_dir: %q remote addr: %q",
|
2021-02-28 11:10:40 +00:00
|
|
|
dbUser.ID, dbUser.Username, dbUser.HomeDir, ipAddr)
|
2021-08-19 13:51:43 +00:00
|
|
|
dataprovider.UpdateLastLogin(&dbUser)
|
2021-02-28 11:10:40 +00:00
|
|
|
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 {
|
2022-05-21 14:34:47 +00:00
|
|
|
certID := common.DefaultTLSKeyPaidID
|
|
|
|
if getConfigPath(s.binding.CertificateFile, "") != "" && getConfigPath(s.binding.CertificateKeyFile, "") != "" {
|
|
|
|
certID = s.binding.GetAddress()
|
|
|
|
}
|
2023-08-21 13:48:29 +00:00
|
|
|
if !certMgr.HasCertificate(certID) {
|
|
|
|
return
|
|
|
|
}
|
2021-04-16 07:00:40 +00:00
|
|
|
s.tlsConfig = &tls.Config{
|
2023-08-07 17:11:48 +00:00
|
|
|
GetCertificate: certMgr.GetCertificateFunc(certID),
|
|
|
|
MinVersion: util.GetTLSVersion(s.binding.MinTLSVersion),
|
|
|
|
CipherSuites: s.binding.ciphers,
|
2020-12-29 08:20:09 +00:00
|
|
|
}
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Debug(logSender, "", "configured TLS cipher suites for binding %q: %v, certID: %v",
|
2022-05-21 14:34:47 +00:00
|
|
|
s.binding.GetAddress(), s.binding.ciphers, certID)
|
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()
|
2023-08-20 14:00:36 +00:00
|
|
|
if s.binding.TLSSessionReuse != int(ftpserver.TLSSessionReuseRequired) {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-08-20 14:00:36 +00:00
|
|
|
// VerifyTLSConnectionState implements the MainDriverExtensionTLSConnectionStateVerifier extension
|
|
|
|
func (s *Server) VerifyTLSConnectionState(_ ftpserver.ClientContext, cs tls.ConnectionState) error {
|
|
|
|
if !s.binding.isMutualTLSEnabled() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return s.verifyTLSConnection(cs)
|
|
|
|
}
|
|
|
|
|
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) {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Debug(logSender, "", "tls handshake error, client certificate %q has beed revoked", clientCrtName)
|
2021-01-03 16:03:04 +00:00
|
|
|
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) {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Warn(logSender, connectionID, "user %q has an invalid home dir: %q. Home dir must be an absolute path, login not allowed",
|
2020-07-29 19:56:56 +00:00
|
|
|
user.Username, user.HomeDir)
|
2023-02-27 18:02:43 +00:00
|
|
|
return nil, fmt.Errorf("cannot login user with invalid home dir: %q", user.HomeDir)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2024-07-24 16:27:13 +00:00
|
|
|
if slices.Contains(user.Filters.DeniedProtocols, common.ProtocolFTP) {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Info(logSender, connectionID, "cannot login user %q, protocol FTP is not allowed", user.Username)
|
|
|
|
return nil, fmt.Errorf("protocol FTP is not allowed for user %q", user.Username)
|
2020-08-17 10:49:20 +00:00
|
|
|
}
|
2023-08-04 18:56:23 +00:00
|
|
|
if !user.IsLoginMethodAllowed(loginMethod, common.ProtocolFTP) {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Info(logSender, connectionID, "cannot login user %q, %v login method is not allowed",
|
2021-12-02 18:36:42 +00:00
|
|
|
user.Username, loginMethod)
|
2023-02-27 18:02:43 +00:00
|
|
|
return nil, fmt.Errorf("login method %v is not allowed for user %q", loginMethod, user.Username)
|
2020-08-19 17:36:12 +00:00
|
|
|
}
|
2022-03-06 15:57:13 +00:00
|
|
|
if user.MustSetSecondFactorForProtocol(common.ProtocolFTP) {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Info(logSender, connectionID, "cannot login user %q, second factor authentication is not set",
|
2022-03-06 15:57:13 +00:00
|
|
|
user.Username)
|
2023-02-27 18:02:43 +00:00
|
|
|
return nil, fmt.Errorf("second factor authentication is not set for user %q", user.Username)
|
2022-03-06 15:57:13 +00:00
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
if user.MaxSessions > 0 {
|
|
|
|
activeSessions := common.Connections.GetActiveSessions(user.Username)
|
|
|
|
if activeSessions >= user.MaxSessions {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Info(logSender, connectionID, "authentication refused for user: %q, too many open sessions: %v/%v",
|
2021-12-02 18:36:42 +00:00
|
|
|
user.Username, activeSessions, user.MaxSessions)
|
2020-07-29 19:56:56 +00:00
|
|
|
return nil, fmt.Errorf("too many open sessions: %v", activeSessions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
remoteAddr := cc.RemoteAddr().String()
|
|
|
|
if !user.IsLoginFromAddrAllowed(remoteAddr) {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Info(logSender, connectionID, "cannot login user %q, remote address is not allowed: %v",
|
2021-12-02 18:36:42 +00:00
|
|
|
user.Username, remoteAddr)
|
2023-02-27 18:02:43 +00:00
|
|
|
return nil, fmt.Errorf("login for user %q 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-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(fmt.Sprintf("%v_%v", s.ID, cc.ID()), common.ProtocolFTP,
|
|
|
|
cc.LocalAddr().String(), remoteAddr, user),
|
|
|
|
clientContext: cc,
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
err = common.Connections.Swap(connection)
|
|
|
|
if err != nil {
|
2022-04-14 17:07:41 +00:00
|
|
|
errClose := user.CloseFs()
|
|
|
|
logger.Warn(logSender, connectionID, "unable to swap connection: %v, close fs error: %v", err, errClose)
|
|
|
|
return nil, err
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
return connection, nil
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:44:56 +00:00
|
|
|
func setStartDirectory(startDirectory string, cc ftpserver.ClientContext) {
|
|
|
|
if startDirectory == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cc.SetPath(startDirectory)
|
|
|
|
}
|
|
|
|
|
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)
|
2024-04-10 16:39:08 +00:00
|
|
|
if err == nil {
|
|
|
|
plugin.Handler.NotifyLogEvent(notifier.LogEventTypeLoginOK, common.ProtocolFTP, user.Username, ip, "", nil)
|
2024-05-17 18:48:18 +00:00
|
|
|
common.DelayLogin(nil)
|
2024-04-10 16:39:08 +00:00
|
|
|
} else if err != common.ErrInternalFailure {
|
2024-05-17 18:48:18 +00:00
|
|
|
logger.ConnectionFailedLog(user.Username, ip, loginMethod, common.ProtocolFTP, err.Error())
|
2021-01-02 13:05:09 +00:00
|
|
|
event := common.HostEventLoginFailed
|
2023-05-12 16:34:59 +00:00
|
|
|
logEv := notifier.LogEventTypeLoginFailed
|
2023-01-25 17:49:03 +00:00
|
|
|
if errors.Is(err, util.ErrNotFound) {
|
2021-01-02 13:05:09 +00:00
|
|
|
event = common.HostEventUserNotFound
|
2023-05-12 16:34:59 +00:00
|
|
|
logEv = notifier.LogEventTypeLoginNoUser
|
2021-01-02 13:05:09 +00:00
|
|
|
}
|
2023-02-09 08:33:33 +00:00
|
|
|
common.AddDefenderEvent(ip, common.ProtocolFTP, event)
|
2023-05-12 16:34:59 +00:00
|
|
|
plugin.Handler.NotifyLogEvent(logEv, common.ProtocolFTP, user.Username, ip, "", err)
|
2024-05-17 18:48:18 +00:00
|
|
|
if loginMethod != dataprovider.LoginMethodTLSCertificate {
|
|
|
|
common.DelayLogin(err)
|
|
|
|
}
|
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
|
|
|
}
|