2019-10-08 08:29:16 +00:00
|
|
|
// Package service allows to start and stop the SFTPGo service
|
2019-09-16 06:52:58 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2020-11-24 12:44:57 +00:00
|
|
|
"errors"
|
2020-10-20 16:42:37 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-01-31 18:04:00 +00:00
|
|
|
"path/filepath"
|
2019-10-24 16:50:35 +00:00
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
"github.com/drakkan/sftpgo/common"
|
2019-09-16 06:52:58 +00:00
|
|
|
"github.com/drakkan/sftpgo/config"
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
2020-10-20 16:42:37 +00:00
|
|
|
"github.com/drakkan/sftpgo/httpd"
|
2019-09-16 06:52:58 +00:00
|
|
|
"github.com/drakkan/sftpgo/logger"
|
2019-10-21 19:10:07 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2020-06-19 15:08:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/version"
|
2019-09-16 06:52:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
logSender = "service"
|
|
|
|
)
|
|
|
|
|
2019-10-24 16:50:35 +00:00
|
|
|
var (
|
|
|
|
chars = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
|
|
|
|
)
|
|
|
|
|
2019-09-16 06:52:58 +00:00
|
|
|
// Service defines the SFTPGo service
|
|
|
|
type Service struct {
|
2020-10-20 16:42:37 +00:00
|
|
|
ConfigDir string
|
|
|
|
ConfigFile string
|
|
|
|
LogFilePath string
|
|
|
|
LogMaxSize int
|
|
|
|
LogMaxBackups int
|
|
|
|
LogMaxAge int
|
|
|
|
PortableMode int
|
|
|
|
PortableUser dataprovider.User
|
|
|
|
LogCompress bool
|
|
|
|
LogVerbose bool
|
|
|
|
LoadDataClean bool
|
|
|
|
LoadDataFrom string
|
|
|
|
LoadDataMode int
|
|
|
|
LoadDataQuotaScan int
|
|
|
|
Shutdown chan bool
|
|
|
|
Error error
|
2019-09-16 06:52:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start initializes the service
|
|
|
|
func (s *Service) Start() error {
|
|
|
|
logLevel := zerolog.DebugLevel
|
|
|
|
if !s.LogVerbose {
|
|
|
|
logLevel = zerolog.InfoLevel
|
|
|
|
}
|
2020-03-03 08:09:58 +00:00
|
|
|
if !filepath.IsAbs(s.LogFilePath) && utils.IsFileInputValid(s.LogFilePath) {
|
2020-03-02 23:34:06 +00:00
|
|
|
s.LogFilePath = filepath.Join(s.ConfigDir, s.LogFilePath)
|
|
|
|
}
|
2019-09-16 06:52:58 +00:00
|
|
|
logger.InitLogger(s.LogFilePath, s.LogMaxSize, s.LogMaxBackups, s.LogMaxAge, s.LogCompress, logLevel)
|
2019-10-26 16:25:53 +00:00
|
|
|
if s.PortableMode == 1 {
|
|
|
|
logger.EnableConsoleLogger(logLevel)
|
2020-10-25 20:42:43 +00:00
|
|
|
if s.LogFilePath == "" {
|
2019-10-26 16:25:53 +00:00
|
|
|
logger.DisableLogger()
|
|
|
|
}
|
|
|
|
}
|
2019-10-21 19:18:45 +00:00
|
|
|
logger.Info(logSender, "", "starting SFTPGo %v, config dir: %v, config file: %v, log max size: %v log max backups: %v "+
|
2020-12-18 15:04:42 +00:00
|
|
|
"log max age: %v log verbose: %v, log compress: %v, load data from: %#v", version.GetAsString(), s.ConfigDir, s.ConfigFile,
|
|
|
|
s.LogMaxSize, s.LogMaxBackups, s.LogMaxAge, s.LogVerbose, s.LogCompress, s.LoadDataFrom)
|
2019-10-24 16:50:35 +00:00
|
|
|
// in portable mode we don't read configuration from file
|
|
|
|
if s.PortableMode != 1 {
|
2020-04-30 12:23:55 +00:00
|
|
|
err := config.LoadConfig(s.ConfigDir, s.ConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, "", "error loading configuration: %v", err)
|
2020-12-03 16:16:35 +00:00
|
|
|
return err
|
2020-04-30 12:23:55 +00:00
|
|
|
}
|
2019-10-24 16:50:35 +00:00
|
|
|
}
|
2020-11-24 12:44:57 +00:00
|
|
|
if !config.HasServicesToStart() {
|
|
|
|
infoString := "No service configured, nothing to do"
|
|
|
|
logger.Info(logSender, "", infoString)
|
|
|
|
logger.InfoToConsole(infoString)
|
|
|
|
return errors.New(infoString)
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2021-01-02 13:05:09 +00:00
|
|
|
err := common.Initialize(config.GetCommonConfig())
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, "", "%v", err)
|
|
|
|
logger.ErrorToConsole("%v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-12-06 09:36:48 +00:00
|
|
|
kmsConfig := config.GetKMSConfig()
|
2021-01-02 13:05:09 +00:00
|
|
|
err = kmsConfig.Initialize()
|
2020-12-06 09:36:48 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, "", "unable to initialize KMS: %v", err)
|
|
|
|
logger.ErrorToConsole("unable to initialize KMS: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2019-09-16 06:52:58 +00:00
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, s.ConfigDir, s.PortableMode == 0)
|
2019-09-16 06:52:58 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, "", "error initializing data provider: %v", err)
|
|
|
|
logger.ErrorToConsole("error initializing data provider: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-24 16:50:35 +00:00
|
|
|
if s.PortableMode == 1 {
|
|
|
|
// create the user for portable mode
|
2021-01-05 08:50:22 +00:00
|
|
|
err = dataprovider.AddUser(&s.PortableUser)
|
2019-10-24 16:50:35 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("error adding portable user: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 16:42:37 +00:00
|
|
|
err = s.loadInitialData()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, "", "unable to load initial data: %v", err)
|
|
|
|
logger.ErrorToConsole("unable to load initial data: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
httpConfig := config.GetHTTPConfig()
|
|
|
|
httpConfig.Initialize(s.ConfigDir)
|
|
|
|
|
|
|
|
s.startServices()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) startServices() {
|
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
ftpdConf := config.GetFTPDConfig()
|
|
|
|
httpdConf := config.GetHTTPDConfig()
|
2020-08-11 21:56:10 +00:00
|
|
|
webDavDConf := config.GetWebDAVDConfig()
|
2020-12-18 08:01:19 +00:00
|
|
|
telemetryConf := config.GetTelemetryConfig()
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2020-12-23 15:12:30 +00:00
|
|
|
if sftpdConf.ShouldBind() {
|
2020-11-24 12:44:57 +00:00
|
|
|
go func() {
|
|
|
|
logger.Debug(logSender, "", "initializing SFTP server with config %+v", sftpdConf)
|
|
|
|
if err := sftpdConf.Initialize(s.ConfigDir); err != nil {
|
|
|
|
logger.Error(logSender, "", "could not start SFTP server: %v", err)
|
|
|
|
logger.ErrorToConsole("could not start SFTP server: %v", err)
|
|
|
|
s.Error = err
|
|
|
|
}
|
|
|
|
s.Shutdown <- true
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
logger.Debug(logSender, "", "SFTP server not started, disabled in config file")
|
|
|
|
}
|
2019-09-16 06:52:58 +00:00
|
|
|
|
2020-12-29 18:02:56 +00:00
|
|
|
if httpdConf.ShouldBind() {
|
2019-09-16 06:52:58 +00:00
|
|
|
go func() {
|
2020-12-18 08:47:22 +00:00
|
|
|
if err := httpdConf.Initialize(s.ConfigDir); err != nil {
|
2019-09-16 06:52:58 +00:00
|
|
|
logger.Error(logSender, "", "could not start HTTP server: %v", err)
|
|
|
|
logger.ErrorToConsole("could not start HTTP server: %v", err)
|
2020-07-09 17:16:52 +00:00
|
|
|
s.Error = err
|
2019-09-16 06:52:58 +00:00
|
|
|
}
|
|
|
|
s.Shutdown <- true
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
logger.Debug(logSender, "", "HTTP server not started, disabled in config file")
|
2019-10-24 16:50:35 +00:00
|
|
|
if s.PortableMode != 1 {
|
|
|
|
logger.DebugToConsole("HTTP server not started, disabled in config file")
|
|
|
|
}
|
|
|
|
}
|
2020-12-23 15:12:30 +00:00
|
|
|
if ftpdConf.ShouldBind() {
|
2020-07-29 19:56:56 +00:00
|
|
|
go func() {
|
|
|
|
if err := ftpdConf.Initialize(s.ConfigDir); err != nil {
|
|
|
|
logger.Error(logSender, "", "could not start FTP server: %v", err)
|
|
|
|
logger.ErrorToConsole("could not start FTP server: %v", err)
|
|
|
|
s.Error = err
|
|
|
|
}
|
|
|
|
s.Shutdown <- true
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
logger.Debug(logSender, "", "FTP server not started, disabled in config file")
|
|
|
|
}
|
2020-12-23 15:12:30 +00:00
|
|
|
if webDavDConf.ShouldBind() {
|
2020-08-11 21:56:10 +00:00
|
|
|
go func() {
|
|
|
|
if err := webDavDConf.Initialize(s.ConfigDir); err != nil {
|
|
|
|
logger.Error(logSender, "", "could not start WebDAV server: %v", err)
|
|
|
|
logger.ErrorToConsole("could not start WebDAV server: %v", err)
|
|
|
|
s.Error = err
|
|
|
|
}
|
|
|
|
s.Shutdown <- true
|
|
|
|
}()
|
|
|
|
} else {
|
2020-11-24 12:44:57 +00:00
|
|
|
logger.Debug(logSender, "", "WebDAV server not started, disabled in config file")
|
2020-08-11 21:56:10 +00:00
|
|
|
}
|
2020-12-29 18:02:56 +00:00
|
|
|
if telemetryConf.ShouldBind() {
|
2020-12-18 08:01:19 +00:00
|
|
|
go func() {
|
2020-12-18 15:04:42 +00:00
|
|
|
if err := telemetryConf.Initialize(s.ConfigDir); err != nil {
|
2020-12-18 08:01:19 +00:00
|
|
|
logger.Error(logSender, "", "could not start telemetry server: %v", err)
|
|
|
|
logger.ErrorToConsole("could not start telemetry server: %v", err)
|
|
|
|
s.Error = err
|
|
|
|
}
|
|
|
|
s.Shutdown <- true
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
logger.Debug(logSender, "", "telemetry server not started, disabled in config file")
|
|
|
|
if s.PortableMode != 1 {
|
|
|
|
logger.DebugToConsole("telemetry server not started, disabled in config file")
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 06:52:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait blocks until the service exits
|
|
|
|
func (s *Service) Wait() {
|
2020-02-04 22:21:33 +00:00
|
|
|
if s.PortableMode != 1 {
|
|
|
|
registerSigHup()
|
2020-06-22 17:11:53 +00:00
|
|
|
registerSigUSR1()
|
2020-02-04 22:21:33 +00:00
|
|
|
}
|
2019-09-16 06:52:58 +00:00
|
|
|
<-s.Shutdown
|
|
|
|
}
|
|
|
|
|
2019-10-08 08:29:16 +00:00
|
|
|
// Stop terminates the service unblocking the Wait method
|
2019-09-16 06:52:58 +00:00
|
|
|
func (s *Service) Stop() {
|
|
|
|
close(s.Shutdown)
|
|
|
|
logger.Debug(logSender, "", "Service stopped")
|
|
|
|
}
|
2020-10-20 16:42:37 +00:00
|
|
|
|
|
|
|
func (s *Service) loadInitialData() error {
|
|
|
|
if s.LoadDataFrom == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !filepath.IsAbs(s.LoadDataFrom) {
|
|
|
|
return fmt.Errorf("invalid input_file %#v, it must be an absolute path", s.LoadDataFrom)
|
|
|
|
}
|
|
|
|
if s.LoadDataMode < 0 || s.LoadDataMode > 1 {
|
|
|
|
return fmt.Errorf("Invalid loaddata-mode %v", s.LoadDataMode)
|
|
|
|
}
|
|
|
|
if s.LoadDataQuotaScan < 0 || s.LoadDataQuotaScan > 2 {
|
|
|
|
return fmt.Errorf("Invalid loaddata-scan %v", s.LoadDataQuotaScan)
|
|
|
|
}
|
|
|
|
info, err := os.Stat(s.LoadDataFrom)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.Size() > httpd.MaxRestoreSize {
|
|
|
|
return fmt.Errorf("unable to restore input file %#v size too big: %v/%v bytes",
|
|
|
|
s.LoadDataFrom, info.Size(), httpd.MaxRestoreSize)
|
|
|
|
}
|
|
|
|
content, err := ioutil.ReadFile(s.LoadDataFrom)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to read input file %#v: %v", s.LoadDataFrom, err)
|
|
|
|
}
|
2020-11-22 20:53:04 +00:00
|
|
|
dump, err := dataprovider.ParseDumpData(content)
|
2020-10-20 16:42:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to parse file to restore %#v: %v", s.LoadDataFrom, err)
|
|
|
|
}
|
2021-02-11 07:33:32 +00:00
|
|
|
err = s.restoreDump(dump)
|
2020-10-20 16:42:37 +00:00
|
|
|
if err != nil {
|
2021-02-11 07:33:32 +00:00
|
|
|
return err
|
2020-10-20 16:42:37 +00:00
|
|
|
}
|
2020-11-04 18:11:40 +00:00
|
|
|
logger.Info(logSender, "", "data loaded from file %#v mode: %v, quota scan %v", s.LoadDataFrom,
|
|
|
|
s.LoadDataMode, s.LoadDataQuotaScan)
|
|
|
|
logger.InfoToConsole("data loaded from file %#v mode: %v, quota scan %v", s.LoadDataFrom,
|
|
|
|
s.LoadDataMode, s.LoadDataQuotaScan)
|
2020-10-20 16:42:37 +00:00
|
|
|
if s.LoadDataClean {
|
|
|
|
err = os.Remove(s.LoadDataFrom)
|
|
|
|
if err == nil {
|
|
|
|
logger.Info(logSender, "", "file %#v deleted after successful load", s.LoadDataFrom)
|
|
|
|
logger.InfoToConsole("file %#v deleted after successful load", s.LoadDataFrom)
|
|
|
|
} else {
|
|
|
|
logger.Warn(logSender, "", "unable to delete file %#v after successful load: %v", s.LoadDataFrom, err)
|
|
|
|
logger.WarnToConsole("unable to delete file %#v after successful load: %v", s.LoadDataFrom, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-11 07:33:32 +00:00
|
|
|
|
|
|
|
func (s *Service) restoreDump(dump dataprovider.BackupData) error {
|
|
|
|
err := httpd.RestoreAdmins(dump.Admins, s.LoadDataFrom, s.LoadDataMode)
|
|
|
|
if err != nil {
|
2021-02-11 18:45:52 +00:00
|
|
|
return fmt.Errorf("unable to restore admins from file %#v: %v", s.LoadDataFrom, err)
|
2021-02-11 07:33:32 +00:00
|
|
|
}
|
|
|
|
err = httpd.RestoreFolders(dump.Folders, s.LoadDataFrom, s.LoadDataMode, s.LoadDataQuotaScan)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to restore folders from file %#v: %v", s.LoadDataFrom, err)
|
|
|
|
}
|
|
|
|
err = httpd.RestoreUsers(dump.Users, s.LoadDataFrom, s.LoadDataMode, s.LoadDataQuotaScan)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to restore users from file %#v: %v", s.LoadDataFrom, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|