2020-07-29 19:56:56 +00:00
|
|
|
// Package ftpd implements the FTP protocol
|
|
|
|
package ftpd
|
|
|
|
|
|
|
|
import (
|
2020-12-08 10:18:34 +00:00
|
|
|
"fmt"
|
2020-07-29 19:56:56 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
ftpserver "github.com/fclairamb/ftpserverlib"
|
|
|
|
|
2020-12-23 15:12:30 +00:00
|
|
|
"github.com/drakkan/sftpgo/common"
|
2020-07-29 19:56:56 +00:00
|
|
|
"github.com/drakkan/sftpgo/logger"
|
|
|
|
"github.com/drakkan/sftpgo/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
logSender = "ftpd"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-12-23 15:12:30 +00:00
|
|
|
certMgr *common.CertManager
|
|
|
|
serviceStatus ServiceStatus
|
2020-07-29 19:56:56 +00:00
|
|
|
)
|
|
|
|
|
2020-12-23 15:12:30 +00:00
|
|
|
// Binding defines the configuration for a network listener
|
|
|
|
type Binding struct {
|
|
|
|
// The address to listen on. A blank value means listen on all available network interfaces.
|
|
|
|
Address string `json:"address" mapstructure:"address"`
|
|
|
|
// The port used for serving requests
|
|
|
|
Port int `json:"port" mapstructure:"port"`
|
2020-12-24 17:48:06 +00:00
|
|
|
// Apply the proxy configuration, if any, for this binding
|
2020-12-23 15:12:30 +00:00
|
|
|
ApplyProxyConfig bool `json:"apply_proxy_config" mapstructure:"apply_proxy_config"`
|
2020-12-24 17:48:06 +00:00
|
|
|
// Set to 1 to require TLS for both data and control connection.
|
|
|
|
// Set to 2 to enable implicit TLS
|
2020-12-23 15:12:30 +00:00
|
|
|
TLSMode int `json:"tls_mode" mapstructure:"tls_mode"`
|
|
|
|
// External IP address to expose for passive connections.
|
|
|
|
ForcePassiveIP string `json:"force_passive_ip" mapstructure:"force_passive_ip"`
|
2020-12-29 08:20:09 +00:00
|
|
|
// set to 1 to require client certificate authentication in addition to FTP auth.
|
|
|
|
// You need to define at least a certificate authority for this to work
|
|
|
|
ClientAuthType int `json:"client_auth_type" mapstructure:"client_auth_type"`
|
2020-12-23 15:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAddress returns the binding address
|
|
|
|
func (b *Binding) GetAddress() string {
|
|
|
|
return fmt.Sprintf("%s:%d", b.Address, b.Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsValid returns true if the binding port is > 0
|
|
|
|
func (b *Binding) IsValid() bool {
|
|
|
|
return b.Port > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasProxy returns true if the proxy protocol is active for this binding
|
|
|
|
func (b *Binding) HasProxy() bool {
|
|
|
|
return b.ApplyProxyConfig && common.Config.ProxyProtocol > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTLSDescription returns the TLS mode as string
|
|
|
|
func (b *Binding) GetTLSDescription() string {
|
|
|
|
if certMgr == nil {
|
|
|
|
return "Disabled"
|
|
|
|
}
|
|
|
|
switch b.TLSMode {
|
|
|
|
case 1:
|
|
|
|
return "Explicit required"
|
|
|
|
case 2:
|
|
|
|
return "Implicit"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Plain and explicit"
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
// PortRange defines a port range
|
|
|
|
type PortRange struct {
|
|
|
|
// Range start
|
|
|
|
Start int `json:"start" mapstructure:"start"`
|
|
|
|
// Range end
|
|
|
|
End int `json:"end" mapstructure:"end"`
|
|
|
|
}
|
|
|
|
|
2020-12-08 10:18:34 +00:00
|
|
|
// ServiceStatus defines the service status
|
|
|
|
type ServiceStatus struct {
|
|
|
|
IsActive bool `json:"is_active"`
|
2020-12-23 15:12:30 +00:00
|
|
|
Bindings []Binding `json:"bindings"`
|
2020-12-08 10:18:34 +00:00
|
|
|
PassivePortRange PortRange `json:"passive_port_range"`
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
// Configuration defines the configuration for the ftp server
|
|
|
|
type Configuration struct {
|
2020-12-23 15:12:30 +00:00
|
|
|
// Addresses and ports to bind to
|
|
|
|
Bindings []Binding `json:"bindings" mapstructure:"bindings"`
|
|
|
|
// Deprecated: please use Bindings
|
2020-07-29 19:56:56 +00:00
|
|
|
BindPort int `json:"bind_port" mapstructure:"bind_port"`
|
2020-12-23 15:12:30 +00:00
|
|
|
// Deprecated: please use Bindings
|
2020-07-29 19:56:56 +00:00
|
|
|
BindAddress string `json:"bind_address" mapstructure:"bind_address"`
|
2020-12-23 15:12:30 +00:00
|
|
|
// Deprecated: please use Bindings
|
2020-07-29 19:56:56 +00:00
|
|
|
ForcePassiveIP string `json:"force_passive_ip" mapstructure:"force_passive_ip"`
|
|
|
|
// Greeting banner displayed when a connection first comes in
|
|
|
|
Banner string `json:"banner" mapstructure:"banner"`
|
|
|
|
// the contents of the specified file, if any, are diplayed when someone connects to the server.
|
|
|
|
// If set, it overrides the banner string provided by the banner option
|
|
|
|
BannerFile string `json:"banner_file" mapstructure:"banner_file"`
|
|
|
|
// If files containing a certificate and matching private key for the server are provided the server will accept
|
|
|
|
// both plain FTP an explicit FTP over TLS.
|
|
|
|
// Certificate and key files can be reloaded on demand sending a "SIGHUP" signal on Unix based systems and a
|
|
|
|
// "paramchange" request to the running service on Windows.
|
|
|
|
CertificateFile string `json:"certificate_file" mapstructure:"certificate_file"`
|
|
|
|
CertificateKeyFile string `json:"certificate_key_file" mapstructure:"certificate_key_file"`
|
2020-12-29 08:20:09 +00:00
|
|
|
// CACertificates defines the set of root certificate authorities to use to verify client certificates.
|
|
|
|
CACertificates []string `json:"ca_certificates" mapstructure:"ca_certificates"`
|
2020-07-29 19:56:56 +00:00
|
|
|
// Do not impose the port 20 for active data transfer. Enabling this option allows to run SFTPGo with less privilege
|
|
|
|
ActiveTransfersPortNon20 bool `json:"active_transfers_port_non_20" mapstructure:"active_transfers_port_non_20"`
|
2020-12-24 17:48:06 +00:00
|
|
|
// Set to true to disable active FTP
|
|
|
|
DisableActiveMode bool `json:"disable_active_mode" mapstructure:"disable_active_mode"`
|
|
|
|
// Set to true to enable the FTP SITE command.
|
|
|
|
// We support chmod and symlink if SITE support is enabled
|
|
|
|
EnableSite bool `json:"enable_site" mapstructure:"enable_site"`
|
|
|
|
// Set to 1 to enable FTP commands that allow to calculate the hash value of files.
|
|
|
|
// These FTP commands will be enabled: HASH, XCRC, MD5/XMD5, XSHA/XSHA1, XSHA256, XSHA512.
|
|
|
|
// Please keep in mind that to calculate the hash we need to read the whole file, for
|
|
|
|
// remote backends this means downloading the file, for the encrypted backend this means
|
|
|
|
// decrypting the file
|
|
|
|
HASHSupport int `json:"hash_support" mapstructure:"hash_support"`
|
|
|
|
// Set to 1 to enable support for the non standard "COMB" FTP command.
|
|
|
|
// Combine is only supported for local filesystem, for cloud backends it has
|
|
|
|
// no advantage as it will download the partial files and will upload the
|
|
|
|
// combined one. Cloud backends natively support multipart uploads.
|
|
|
|
CombineSupport int `json:"combine_support" mapstructure:"combine_support"`
|
2020-12-23 15:12:30 +00:00
|
|
|
// Deprecated: please use Bindings
|
2020-08-15 11:02:25 +00:00
|
|
|
TLSMode int `json:"tls_mode" mapstructure:"tls_mode"`
|
2020-12-24 17:48:06 +00:00
|
|
|
// Port Range for data connections. Random if not specified
|
|
|
|
PassivePortRange PortRange `json:"passive_port_range" mapstructure:"passive_port_range"`
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 15:12:30 +00:00
|
|
|
// ShouldBind returns true if there is at least a valid binding
|
|
|
|
func (c *Configuration) ShouldBind() bool {
|
|
|
|
for _, binding := range c.Bindings {
|
|
|
|
if binding.IsValid() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
// Initialize configures and starts the FTP server
|
|
|
|
func (c *Configuration) Initialize(configDir string) error {
|
2020-08-11 21:56:10 +00:00
|
|
|
logger.Debug(logSender, "", "initializing FTP server with config %+v", *c)
|
2020-12-23 15:12:30 +00:00
|
|
|
if !c.ShouldBind() {
|
|
|
|
return common.ErrNoBinding
|
|
|
|
}
|
|
|
|
|
|
|
|
certificateFile := getConfigPath(c.CertificateFile, configDir)
|
|
|
|
certificateKeyFile := getConfigPath(c.CertificateKeyFile, configDir)
|
|
|
|
if certificateFile != "" && certificateKeyFile != "" {
|
|
|
|
mgr, err := common.NewCertManager(certificateFile, certificateKeyFile, logSender)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-29 08:20:09 +00:00
|
|
|
if err := mgr.LoadRootCAs(c.CACertificates, configDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-23 15:12:30 +00:00
|
|
|
certMgr = mgr
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2020-12-23 15:12:30 +00:00
|
|
|
serviceStatus = ServiceStatus{
|
|
|
|
Bindings: nil,
|
2020-12-08 10:18:34 +00:00
|
|
|
PassivePortRange: c.PassivePortRange,
|
|
|
|
}
|
2020-12-23 15:12:30 +00:00
|
|
|
|
2020-12-23 18:53:07 +00:00
|
|
|
exitChannel := make(chan error, 1)
|
2020-12-23 15:12:30 +00:00
|
|
|
|
|
|
|
for idx, binding := range c.Bindings {
|
|
|
|
if !binding.IsValid() {
|
|
|
|
continue
|
2020-12-08 10:18:34 +00:00
|
|
|
}
|
2020-12-23 15:12:30 +00:00
|
|
|
|
|
|
|
server := NewServer(c, configDir, binding, idx)
|
|
|
|
|
|
|
|
go func(s *Server) {
|
|
|
|
ftpServer := ftpserver.NewFtpServer(s)
|
2021-01-03 08:51:54 +00:00
|
|
|
logger.Info(logSender, "", "starting FTP serving, binding: %v", s.binding.GetAddress())
|
2020-12-23 15:12:30 +00:00
|
|
|
exitChannel <- ftpServer.ListenAndServe()
|
|
|
|
}(server)
|
|
|
|
|
|
|
|
serviceStatus.Bindings = append(serviceStatus.Bindings, binding)
|
2020-12-08 10:18:34 +00:00
|
|
|
}
|
2020-12-23 15:12:30 +00:00
|
|
|
|
|
|
|
serviceStatus.IsActive = true
|
|
|
|
|
|
|
|
return <-exitChannel
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReloadTLSCertificate reloads the TLS certificate and key from the configured paths
|
|
|
|
func ReloadTLSCertificate() error {
|
2020-12-23 15:12:30 +00:00
|
|
|
if certMgr != nil {
|
|
|
|
return certMgr.LoadCertificate(logSender)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-08 10:18:34 +00:00
|
|
|
// GetStatus returns the server status
|
|
|
|
func GetStatus() ServiceStatus {
|
2020-12-23 15:12:30 +00:00
|
|
|
return serviceStatus
|
2020-12-08 10:18:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
func getConfigPath(name, configDir string) string {
|
|
|
|
if !utils.IsFileInputValid(name) {
|
|
|
|
return ""
|
|
|
|
}
|
2020-12-23 15:12:30 +00:00
|
|
|
if name != "" && !filepath.IsAbs(name) {
|
2020-07-29 19:56:56 +00:00
|
|
|
return filepath.Join(configDir, name)
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|