ftpd.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Package ftpd implements the FTP protocol
  2. package ftpd
  3. import (
  4. "fmt"
  5. "path/filepath"
  6. ftpserver "github.com/fclairamb/ftpserverlib"
  7. "github.com/drakkan/sftpgo/common"
  8. "github.com/drakkan/sftpgo/logger"
  9. "github.com/drakkan/sftpgo/utils"
  10. )
  11. const (
  12. logSender = "ftpd"
  13. )
  14. var (
  15. certMgr *common.CertManager
  16. serviceStatus ServiceStatus
  17. )
  18. // Binding defines the configuration for a network listener
  19. type Binding struct {
  20. // The address to listen on. A blank value means listen on all available network interfaces.
  21. Address string `json:"address" mapstructure:"address"`
  22. // The port used for serving requests
  23. Port int `json:"port" mapstructure:"port"`
  24. // apply the proxy configuration, if any, for this binding
  25. ApplyProxyConfig bool `json:"apply_proxy_config" mapstructure:"apply_proxy_config"`
  26. // set to 1 to require TLS for both data and control connection
  27. TLSMode int `json:"tls_mode" mapstructure:"tls_mode"`
  28. // External IP address to expose for passive connections.
  29. ForcePassiveIP string `json:"force_passive_ip" mapstructure:"force_passive_ip"`
  30. }
  31. // GetAddress returns the binding address
  32. func (b *Binding) GetAddress() string {
  33. return fmt.Sprintf("%s:%d", b.Address, b.Port)
  34. }
  35. // IsValid returns true if the binding port is > 0
  36. func (b *Binding) IsValid() bool {
  37. return b.Port > 0
  38. }
  39. // HasProxy returns true if the proxy protocol is active for this binding
  40. func (b *Binding) HasProxy() bool {
  41. return b.ApplyProxyConfig && common.Config.ProxyProtocol > 0
  42. }
  43. // GetTLSDescription returns the TLS mode as string
  44. func (b *Binding) GetTLSDescription() string {
  45. if certMgr == nil {
  46. return "Disabled"
  47. }
  48. switch b.TLSMode {
  49. case 1:
  50. return "Explicit required"
  51. case 2:
  52. return "Implicit"
  53. }
  54. return "Plain and explicit"
  55. }
  56. // PortRange defines a port range
  57. type PortRange struct {
  58. // Range start
  59. Start int `json:"start" mapstructure:"start"`
  60. // Range end
  61. End int `json:"end" mapstructure:"end"`
  62. }
  63. // ServiceStatus defines the service status
  64. type ServiceStatus struct {
  65. IsActive bool `json:"is_active"`
  66. Bindings []Binding `json:"bindings"`
  67. PassivePortRange PortRange `json:"passive_port_range"`
  68. }
  69. // Configuration defines the configuration for the ftp server
  70. type Configuration struct {
  71. // Addresses and ports to bind to
  72. Bindings []Binding `json:"bindings" mapstructure:"bindings"`
  73. // Deprecated: please use Bindings
  74. BindPort int `json:"bind_port" mapstructure:"bind_port"`
  75. // Deprecated: please use Bindings
  76. BindAddress string `json:"bind_address" mapstructure:"bind_address"`
  77. // Deprecated: please use Bindings
  78. ForcePassiveIP string `json:"force_passive_ip" mapstructure:"force_passive_ip"`
  79. // Greeting banner displayed when a connection first comes in
  80. Banner string `json:"banner" mapstructure:"banner"`
  81. // the contents of the specified file, if any, are diplayed when someone connects to the server.
  82. // If set, it overrides the banner string provided by the banner option
  83. BannerFile string `json:"banner_file" mapstructure:"banner_file"`
  84. // If files containing a certificate and matching private key for the server are provided the server will accept
  85. // both plain FTP an explicit FTP over TLS.
  86. // Certificate and key files can be reloaded on demand sending a "SIGHUP" signal on Unix based systems and a
  87. // "paramchange" request to the running service on Windows.
  88. CertificateFile string `json:"certificate_file" mapstructure:"certificate_file"`
  89. CertificateKeyFile string `json:"certificate_key_file" mapstructure:"certificate_key_file"`
  90. // Do not impose the port 20 for active data transfer. Enabling this option allows to run SFTPGo with less privilege
  91. ActiveTransfersPortNon20 bool `json:"active_transfers_port_non_20" mapstructure:"active_transfers_port_non_20"`
  92. // Port Range for data connections. Random if not specified
  93. PassivePortRange PortRange `json:"passive_port_range" mapstructure:"passive_port_range"`
  94. // Deprecated: please use Bindings
  95. TLSMode int `json:"tls_mode" mapstructure:"tls_mode"`
  96. }
  97. // ShouldBind returns true if there is at least a valid binding
  98. func (c *Configuration) ShouldBind() bool {
  99. for _, binding := range c.Bindings {
  100. if binding.IsValid() {
  101. return true
  102. }
  103. }
  104. return false
  105. }
  106. // Initialize configures and starts the FTP server
  107. func (c *Configuration) Initialize(configDir string) error {
  108. logger.Debug(logSender, "", "initializing FTP server with config %+v", *c)
  109. if !c.ShouldBind() {
  110. return common.ErrNoBinding
  111. }
  112. certificateFile := getConfigPath(c.CertificateFile, configDir)
  113. certificateKeyFile := getConfigPath(c.CertificateKeyFile, configDir)
  114. if certificateFile != "" && certificateKeyFile != "" {
  115. mgr, err := common.NewCertManager(certificateFile, certificateKeyFile, logSender)
  116. if err != nil {
  117. return err
  118. }
  119. certMgr = mgr
  120. }
  121. serviceStatus = ServiceStatus{
  122. Bindings: nil,
  123. PassivePortRange: c.PassivePortRange,
  124. }
  125. exitChannel := make(chan error)
  126. for idx, binding := range c.Bindings {
  127. if !binding.IsValid() {
  128. continue
  129. }
  130. server := NewServer(c, configDir, binding, idx)
  131. go func(s *Server) {
  132. ftpServer := ftpserver.NewFtpServer(s)
  133. exitChannel <- ftpServer.ListenAndServe()
  134. }(server)
  135. serviceStatus.Bindings = append(serviceStatus.Bindings, binding)
  136. }
  137. serviceStatus.IsActive = true
  138. return <-exitChannel
  139. }
  140. // ReloadTLSCertificate reloads the TLS certificate and key from the configured paths
  141. func ReloadTLSCertificate() error {
  142. if certMgr != nil {
  143. return certMgr.LoadCertificate(logSender)
  144. }
  145. return nil
  146. }
  147. // GetStatus returns the server status
  148. func GetStatus() ServiceStatus {
  149. return serviceStatus
  150. }
  151. func getConfigPath(name, configDir string) string {
  152. if !utils.IsFileInputValid(name) {
  153. return ""
  154. }
  155. if name != "" && !filepath.IsAbs(name) {
  156. return filepath.Join(configDir, name)
  157. }
  158. return name
  159. }