2020-07-29 19:56:56 +00:00
|
|
|
package ftpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
ftpserver "github.com/fclairamb/ftpserverlib"
|
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/common"
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/logger"
|
|
|
|
"github.com/drakkan/sftpgo/metrics"
|
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2020-08-03 16:03:09 +00:00
|
|
|
"github.com/drakkan/sftpgo/version"
|
2020-07-29 19:56:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Server implements the ftpserverlib MainDriver interface
|
|
|
|
type Server struct {
|
2020-12-23 15:12:30 +00:00
|
|
|
ID int
|
2020-08-03 16:03:09 +00:00
|
|
|
config *Configuration
|
|
|
|
initialMsg string
|
|
|
|
statusBanner string
|
2020-12-23 15:12:30 +00:00
|
|
|
binding Binding
|
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 {
|
2020-07-29 19:56:56 +00:00
|
|
|
server := &Server{
|
2020-08-03 16:03:09 +00:00
|
|
|
config: config,
|
|
|
|
initialMsg: config.Banner,
|
|
|
|
statusBanner: fmt.Sprintf("SFTPGo %v FTP Server", version.Get().Version),
|
2020-12-23 15:12:30 +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)
|
|
|
|
}
|
|
|
|
bannerContent, err := ioutil.ReadFile(bannerFilePath)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
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) {
|
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-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) {
|
2020-12-15 18:29:30 +00:00
|
|
|
if !common.Connections.IsNewConnectionAllowed() {
|
|
|
|
logger.Log(logger.LevelDebug, common.ProtocolFTP, "", "connection refused, configured limit reached")
|
|
|
|
return "", common.ErrConnectionDenied
|
|
|
|
}
|
2020-08-11 21:56:10 +00:00
|
|
|
if err := common.Config.ExecutePostConnectHook(cc.RemoteAddr().String(), common.ProtocolFTP); err != nil {
|
2020-12-15 18:29:30 +00:00
|
|
|
return "", 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{
|
|
|
|
BaseConnection: common.NewBaseConnection(connID, common.ProtocolFTP, user, nil),
|
|
|
|
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) {
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthUser authenticates the user and selects an handling driver
|
|
|
|
func (s *Server) AuthUser(cc ftpserver.ClientContext, username, password string) (ftpserver.ClientDriver, error) {
|
|
|
|
remoteAddr := cc.RemoteAddr().String()
|
2020-08-12 14:15:12 +00:00
|
|
|
user, err := dataprovider.CheckUserAndPass(username, password, utils.GetIPFromRemoteAddress(remoteAddr), common.ProtocolFTP)
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
2020-08-11 21:56:10 +00:00
|
|
|
updateLoginMetrics(username, remoteAddr, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
connection, err := s.validateUser(user, cc)
|
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
defer updateLoginMetrics(username, remoteAddr, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
connection.Fs.CheckRootPath(connection.GetUsername(), user.GetUID(), user.GetGID())
|
|
|
|
connection.Log(logger.LevelInfo, "User id: %d, logged in with FTP, username: %#v, home_dir: %#v remote addr: %#v",
|
|
|
|
user.ID, user.Username, user.HomeDir, remoteAddr)
|
|
|
|
dataprovider.UpdateLastLogin(user) //nolint:errcheck
|
|
|
|
return connection, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTLSConfig returns a TLS Certificate to use
|
|
|
|
func (s *Server) GetTLSConfig() (*tls.Config, error) {
|
2020-12-23 15:12:30 +00:00
|
|
|
if certMgr != nil {
|
2020-07-29 19:56:56 +00:00
|
|
|
return &tls.Config{
|
2020-12-23 15:12:30 +00:00
|
|
|
GetCertificate: certMgr.GetCertificateFunc(),
|
2020-08-15 11:02:25 +00:00
|
|
|
MinVersion: tls.VersionTLS12,
|
2020-07-29 19:56:56 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("no TLS certificate configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) validateUser(user dataprovider.User, cc ftpserver.ClientContext) (*Connection, error) {
|
|
|
|
connectionID := fmt.Sprintf("%v_%v", common.ProtocolFTP, cc.ID())
|
|
|
|
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)
|
|
|
|
}
|
2020-08-17 10:49:20 +00:00
|
|
|
if utils.IsStringInSlice(common.ProtocolFTP, user.Filters.DeniedProtocols) {
|
|
|
|
logger.Debug(logSender, connectionID, "cannot login user %#v, protocol FTP is not allowed", user.Username)
|
|
|
|
return nil, fmt.Errorf("Protocol FTP is not allowed for user %#v", user.Username)
|
|
|
|
}
|
2020-08-19 17:36:12 +00:00
|
|
|
if !user.IsLoginMethodAllowed(dataprovider.LoginMethodPassword, nil) {
|
|
|
|
logger.Debug(logSender, connectionID, "cannot login user %#v, password login method is not allowed", user.Username)
|
|
|
|
return nil, fmt.Errorf("Password login method is not allowed for user %#v", user.Username)
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if dataprovider.GetQuotaTracking() > 0 && user.HasOverlappedMappedPaths() {
|
|
|
|
logger.Debug(logSender, connectionID, "cannot login user %#v, overlapping mapped folders are allowed only with quota tracking disabled",
|
|
|
|
user.Username)
|
|
|
|
return nil, errors.New("overlapping mapped folders are allowed only with quota tracking disabled")
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
return nil, fmt.Errorf("Login for user %#v is not allowed from this address: %v", user.Username, remoteAddr)
|
|
|
|
}
|
|
|
|
fs, err := user.GetFilesystem(connectionID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
connection := &Connection{
|
2020-12-23 15:12:30 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(fmt.Sprintf("%v_%v", s.ID, cc.ID()), common.ProtocolFTP, user, fs),
|
2020-07-29 19:56:56 +00:00
|
|
|
clientContext: cc,
|
|
|
|
}
|
|
|
|
err = common.Connections.Swap(connection)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("Internal authentication error")
|
|
|
|
}
|
|
|
|
return connection, nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
func updateLoginMetrics(username, remoteAddress string, err error) {
|
2020-08-12 14:15:12 +00:00
|
|
|
metrics.AddLoginAttempt(dataprovider.LoginMethodPassword)
|
|
|
|
ip := utils.GetIPFromRemoteAddress(remoteAddress)
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
2020-08-12 14:15:12 +00:00
|
|
|
logger.ConnectionFailedLog(username, ip, dataprovider.LoginMethodPassword,
|
|
|
|
common.ProtocolFTP, err.Error())
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2020-08-12 14:15:12 +00:00
|
|
|
metrics.AddLoginResult(dataprovider.LoginMethodPassword, err)
|
|
|
|
dataprovider.ExecutePostLoginHook(username, dataprovider.LoginMethodPassword, ip, common.ProtocolFTP, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|