ftpd.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Package ftpd implements the FTP protocol
  2. package ftpd
  3. import (
  4. "path/filepath"
  5. ftpserver "github.com/fclairamb/ftpserverlib"
  6. "github.com/drakkan/sftpgo/logger"
  7. "github.com/drakkan/sftpgo/utils"
  8. )
  9. const (
  10. logSender = "ftpd"
  11. )
  12. var (
  13. server *Server
  14. )
  15. // PortRange defines a port range
  16. type PortRange struct {
  17. // Range start
  18. Start int `json:"start" mapstructure:"start"`
  19. // Range end
  20. End int `json:"end" mapstructure:"end"`
  21. }
  22. // Configuration defines the configuration for the ftp server
  23. type Configuration struct {
  24. // The port used for serving FTP requests
  25. BindPort int `json:"bind_port" mapstructure:"bind_port"`
  26. // The address to listen on. A blank value means listen on all available network interfaces.
  27. BindAddress string `json:"bind_address" mapstructure:"bind_address"`
  28. // External IP address to expose for passive connections.
  29. ForcePassiveIP string `json:"force_passive_ip" mapstructure:"force_passive_ip"`
  30. // Greeting banner displayed when a connection first comes in
  31. Banner string `json:"banner" mapstructure:"banner"`
  32. // the contents of the specified file, if any, are diplayed when someone connects to the server.
  33. // If set, it overrides the banner string provided by the banner option
  34. BannerFile string `json:"banner_file" mapstructure:"banner_file"`
  35. // If files containing a certificate and matching private key for the server are provided the server will accept
  36. // both plain FTP an explicit FTP over TLS.
  37. // Certificate and key files can be reloaded on demand sending a "SIGHUP" signal on Unix based systems and a
  38. // "paramchange" request to the running service on Windows.
  39. CertificateFile string `json:"certificate_file" mapstructure:"certificate_file"`
  40. CertificateKeyFile string `json:"certificate_key_file" mapstructure:"certificate_key_file"`
  41. // Do not impose the port 20 for active data transfer. Enabling this option allows to run SFTPGo with less privilege
  42. ActiveTransfersPortNon20 bool `json:"active_transfers_port_non_20" mapstructure:"active_transfers_port_non_20"`
  43. // Port Range for data connections. Random if not specified
  44. PassivePortRange PortRange `json:"passive_port_range" mapstructure:"passive_port_range"`
  45. // set to 1 to require TLS for both data and control connection
  46. TLSMode int `json:"tls_mode" mapstructure:"tls_mode"`
  47. }
  48. // Initialize configures and starts the FTP server
  49. func (c *Configuration) Initialize(configDir string) error {
  50. var err error
  51. logger.Debug(logSender, "", "initializing FTP server with config %+v", *c)
  52. server, err = NewServer(c, configDir)
  53. if err != nil {
  54. return err
  55. }
  56. ftpServer := ftpserver.NewFtpServer(server)
  57. return ftpServer.ListenAndServe()
  58. }
  59. // ReloadTLSCertificate reloads the TLS certificate and key from the configured paths
  60. func ReloadTLSCertificate() error {
  61. if server != nil && server.certMgr != nil {
  62. return server.certMgr.LoadCertificate(logSender)
  63. }
  64. return nil
  65. }
  66. func getConfigPath(name, configDir string) string {
  67. if !utils.IsFileInputValid(name) {
  68. return ""
  69. }
  70. if len(name) > 0 && !filepath.IsAbs(name) {
  71. return filepath.Join(configDir, name)
  72. }
  73. return name
  74. }