2023-01-03 09:18:30 +00:00
|
|
|
// Copyright (C) 2019-2023 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
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"
|
|
|
|
"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"
|
|
|
|
|
2023-02-23 18:25:20 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/acme"
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/config"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/httpd"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/plugin"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/version"
|
2019-09-16 06:52:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
logSender = "service"
|
|
|
|
)
|
|
|
|
|
2019-10-24 16:50:35 +00:00
|
|
|
var (
|
2022-10-22 09:56:41 +00:00
|
|
|
chars = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
|
|
|
|
graceTime int
|
2019-10-24 16:50:35 +00:00
|
|
|
)
|
|
|
|
|
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
|
2022-07-13 08:40:24 +00:00
|
|
|
LogLevel string
|
2021-11-06 14:18:16 +00:00
|
|
|
LogUTCTime bool
|
2020-10-20 16:42:37 +00:00
|
|
|
LoadDataClean bool
|
|
|
|
LoadDataFrom string
|
|
|
|
LoadDataMode int
|
|
|
|
LoadDataQuotaScan int
|
|
|
|
Shutdown chan bool
|
|
|
|
Error error
|
2019-09-16 06:52:58 +00:00
|
|
|
}
|
|
|
|
|
2021-02-13 13:41:37 +00:00
|
|
|
func (s *Service) initLogger() {
|
2022-07-13 08:40:24 +00:00
|
|
|
var logLevel zerolog.Level
|
|
|
|
switch s.LogLevel {
|
|
|
|
case "info":
|
2019-09-16 06:52:58 +00:00
|
|
|
logLevel = zerolog.InfoLevel
|
2022-07-13 08:40:24 +00:00
|
|
|
case "warn":
|
|
|
|
logLevel = zerolog.WarnLevel
|
|
|
|
case "error":
|
|
|
|
logLevel = zerolog.ErrorLevel
|
|
|
|
default:
|
|
|
|
logLevel = zerolog.DebugLevel
|
2019-09-16 06:52:58 +00:00
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
if !filepath.IsAbs(s.LogFilePath) && util.IsFileInputValid(s.LogFilePath) {
|
2020-03-02 23:34:06 +00:00
|
|
|
s.LogFilePath = filepath.Join(s.ConfigDir, s.LogFilePath)
|
|
|
|
}
|
2021-11-06 14:18:16 +00:00
|
|
|
logger.InitLogger(s.LogFilePath, s.LogMaxSize, s.LogMaxBackups, s.LogMaxAge, s.LogCompress, s.LogUTCTime, 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()
|
|
|
|
}
|
|
|
|
}
|
2021-02-13 13:41:37 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 05:39:55 +00:00
|
|
|
// Start initializes and starts the service
|
2022-05-07 10:50:49 +00:00
|
|
|
func (s *Service) Start(disableAWSInstallationCode bool) error {
|
2021-02-13 13:41:37 +00:00
|
|
|
s.initLogger()
|
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 "+
|
2023-02-27 18:02:43 +00:00
|
|
|
"log max age: %v log level: %v, log compress: %v, log utc time: %v, load data from: %q, grace time: %d secs",
|
2022-10-22 09:56:41 +00:00
|
|
|
version.GetAsString(), s.ConfigDir, s.ConfigFile, s.LogMaxSize, s.LogMaxBackups, s.LogMaxAge, s.LogLevel,
|
|
|
|
s.LogCompress, s.LogUTCTime, s.LoadDataFrom, graceTime)
|
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() {
|
2021-03-25 18:31:21 +00:00
|
|
|
infoString := "no service configured, nothing to do"
|
2020-11-24 12:44:57 +00:00
|
|
|
logger.Info(logSender, "", infoString)
|
|
|
|
logger.InfoToConsole(infoString)
|
|
|
|
return errors.New(infoString)
|
|
|
|
}
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2022-05-27 05:39:55 +00:00
|
|
|
if err := s.initializeServices(disableAWSInstallationCode); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.startServices()
|
|
|
|
go common.Config.ExecuteStartupHook() //nolint:errcheck
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) initializeServices(disableAWSInstallationCode bool) error {
|
2022-01-30 10:42:36 +00:00
|
|
|
providerConf := config.GetProviderConf()
|
2020-12-06 09:36:48 +00:00
|
|
|
kmsConfig := config.GetKMSConfig()
|
2023-02-09 08:33:33 +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)
|
2022-05-27 05:39:55 +00:00
|
|
|
return err
|
2020-12-06 09:36:48 +00:00
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
mfaConfig := config.GetMFAConfig()
|
|
|
|
err = mfaConfig.Initialize()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, "", "unable to initialize MFA: %v", err)
|
|
|
|
logger.ErrorToConsole("unable to initialize MFA: %v", err)
|
2022-05-27 05:39:55 +00:00
|
|
|
return err
|
2021-09-04 10:11:04 +00:00
|
|
|
}
|
2023-02-19 18:03:45 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, s.ConfigDir, s.PortableMode == 0)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, "", "error initializing data provider: %v", err)
|
|
|
|
logger.ErrorToConsole("error initializing data provider: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2022-07-13 08:40:24 +00:00
|
|
|
if err := plugin.Initialize(config.GetPluginsConfig(), s.LogLevel); err != nil {
|
2021-07-31 17:09:09 +00:00
|
|
|
logger.Error(logSender, "", "unable to initialize plugin system: %v", err)
|
|
|
|
logger.ErrorToConsole("unable to initialize plugin system: %v", err)
|
2022-05-27 05:39:55 +00:00
|
|
|
return err
|
2021-07-11 13:26:51 +00:00
|
|
|
}
|
2021-09-26 18:25:37 +00:00
|
|
|
smtpConfig := config.GetSMTPConfig()
|
2023-03-01 17:31:02 +00:00
|
|
|
err = smtpConfig.Initialize(s.ConfigDir, s.PortableMode != 1)
|
2021-09-26 18:25:37 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, "", "unable to initialize SMTP configuration: %v", err)
|
|
|
|
logger.ErrorToConsole("unable to initialize SMTP configuration: %v", err)
|
2022-05-27 05:39:55 +00:00
|
|
|
return err
|
2021-09-26 18:25:37 +00:00
|
|
|
}
|
2023-02-09 08:33:33 +00:00
|
|
|
err = common.Initialize(config.GetCommonConfig(), providerConf.GetShared())
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, "", "%v", err)
|
|
|
|
logger.ErrorToConsole("%v", err)
|
|
|
|
return err
|
|
|
|
}
|
2019-09-16 06:52:58 +00:00
|
|
|
|
2019-10-24 16:50:35 +00:00
|
|
|
if s.PortableMode == 1 {
|
|
|
|
// create the user for portable mode
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.AddUser(&s.PortableUser, dataprovider.ActionExecutorSystem, "", "")
|
2019-10-24 16:50:35 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("error adding portable user: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2022-05-27 05:39:55 +00:00
|
|
|
} else {
|
|
|
|
acmeConfig := config.GetACMEConfig()
|
2023-02-23 18:25:20 +00:00
|
|
|
err = acme.Initialize(acmeConfig, s.ConfigDir, true)
|
2022-05-27 05:39:55 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, "", "error initializing ACME configuration: %v", err)
|
|
|
|
logger.ErrorToConsole("error initializing ACME configuration: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2019-10-24 16:50:35 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 10:50:49 +00:00
|
|
|
if err := registerAWSContainer(disableAWSInstallationCode); err != nil {
|
2022-04-03 06:52:36 +00:00
|
|
|
logger.Error(logSender, "", "error registering AWS container: %v", err)
|
|
|
|
logger.ErrorToConsole("error registering AWS container: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
httpConfig := config.GetHTTPConfig()
|
2021-02-13 13:41:37 +00:00
|
|
|
err = httpConfig.Initialize(s.ConfigDir)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, "", "error initializing http client: %v", err)
|
|
|
|
logger.ErrorToConsole("error initializing http client: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2022-05-20 17:30:54 +00:00
|
|
|
commandConfig := config.GetCommandConfig()
|
|
|
|
if err := commandConfig.Initialize(); err != nil {
|
|
|
|
logger.Error(logSender, "", "error initializing commands configuration: %v", err)
|
|
|
|
logger.ErrorToConsole("error initializing commands configuration: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) startServices() {
|
2022-05-20 17:30:54 +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
|
|
|
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() {
|
2021-05-12 20:44:17 +00:00
|
|
|
redactedConf := sftpdConf
|
2021-07-11 13:26:51 +00:00
|
|
|
redactedConf.KeyboardInteractiveHook = util.GetRedactedURL(sftpdConf.KeyboardInteractiveHook)
|
2022-01-05 12:22:49 +00:00
|
|
|
logger.Info(logSender, "", "initializing SFTP server with config %+v", redactedConf)
|
2020-11-24 12:44:57 +00:00
|
|
|
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 {
|
2022-01-05 12:22:49 +00:00
|
|
|
logger.Info(logSender, "", "SFTP server not started, disabled in config file")
|
2020-11-24 12:44:57 +00:00
|
|
|
}
|
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() {
|
2022-05-19 17:49:51 +00:00
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
if err := httpdConf.Initialize(s.ConfigDir, providerConf.GetShared()); 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 {
|
2022-01-05 12:22:49 +00:00
|
|
|
logger.Info(logSender, "", "HTTP server not started, disabled in config file")
|
2019-10-24 16:50:35 +00:00
|
|
|
if s.PortableMode != 1 {
|
2022-01-05 12:22:49 +00:00
|
|
|
logger.InfoToConsole("HTTP server not started, disabled in config file")
|
2019-10-24 16:50:35 +00:00
|
|
|
}
|
|
|
|
}
|
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 {
|
2022-01-05 12:22:49 +00:00
|
|
|
logger.Info(logSender, "", "FTP server not started, disabled in config file")
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
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 {
|
2022-01-05 12:22:49 +00:00
|
|
|
logger.Info(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 {
|
2022-01-05 12:22:49 +00:00
|
|
|
logger.Info(logSender, "", "telemetry server not started, disabled in config file")
|
2020-12-18 08:01:19 +00:00
|
|
|
if s.PortableMode != 1 {
|
2022-01-05 12:22:49 +00:00
|
|
|
logger.InfoToConsole("telemetry server not started, disabled in config file")
|
2020-12-18 08:01:19 +00:00
|
|
|
}
|
|
|
|
}
|
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 {
|
2021-03-25 18:31:21 +00:00
|
|
|
registerSignals()
|
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
|
|
|
|
2022-02-28 16:05:18 +00:00
|
|
|
// LoadInitialData if a data file is set
|
|
|
|
func (s *Service) LoadInitialData() error {
|
2020-10-20 16:42:37 +00:00
|
|
|
if s.LoadDataFrom == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !filepath.IsAbs(s.LoadDataFrom) {
|
2023-02-27 18:02:43 +00:00
|
|
|
return fmt.Errorf("invalid input_file %q, it must be an absolute path", s.LoadDataFrom)
|
2020-10-20 16:42:37 +00:00
|
|
|
}
|
|
|
|
if s.LoadDataMode < 0 || s.LoadDataMode > 1 {
|
2021-03-25 18:31:21 +00:00
|
|
|
return fmt.Errorf("invalid loaddata-mode %v", s.LoadDataMode)
|
2020-10-20 16:42:37 +00:00
|
|
|
}
|
|
|
|
if s.LoadDataQuotaScan < 0 || s.LoadDataQuotaScan > 2 {
|
2021-03-25 18:31:21 +00:00
|
|
|
return fmt.Errorf("invalid loaddata-scan %v", s.LoadDataQuotaScan)
|
2020-10-20 16:42:37 +00:00
|
|
|
}
|
|
|
|
info, err := os.Stat(s.LoadDataFrom)
|
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
return fmt.Errorf("unable to stat file %q: %w", s.LoadDataFrom, err)
|
2020-10-20 16:42:37 +00:00
|
|
|
}
|
|
|
|
if info.Size() > httpd.MaxRestoreSize {
|
2023-02-09 08:33:33 +00:00
|
|
|
return fmt.Errorf("unable to restore input file %q size too big: %d/%d bytes",
|
2020-10-20 16:42:37 +00:00
|
|
|
s.LoadDataFrom, info.Size(), httpd.MaxRestoreSize)
|
|
|
|
}
|
2021-02-25 20:53:04 +00:00
|
|
|
content, err := os.ReadFile(s.LoadDataFrom)
|
2020-10-20 16:42:37 +00:00
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
return fmt.Errorf("unable to read input file %q: %w", s.LoadDataFrom, err)
|
2020-10-20 16:42:37 +00:00
|
|
|
}
|
2020-11-22 20:53:04 +00:00
|
|
|
dump, err := dataprovider.ParseDumpData(content)
|
2020-10-20 16:42:37 +00:00
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
return fmt.Errorf("unable to parse file to restore %q: %w", s.LoadDataFrom, err)
|
2020-10-20 16:42:37 +00:00
|
|
|
}
|
2021-02-24 18:40:29 +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
|
|
|
}
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Info(logSender, "", "data loaded from file %q mode: %v", s.LoadDataFrom, s.LoadDataMode)
|
|
|
|
logger.InfoToConsole("data loaded from file %q mode: %v", s.LoadDataFrom, s.LoadDataMode)
|
2020-10-20 16:42:37 +00:00
|
|
|
if s.LoadDataClean {
|
|
|
|
err = os.Remove(s.LoadDataFrom)
|
|
|
|
if err == nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Info(logSender, "", "file %q deleted after successful load", s.LoadDataFrom)
|
|
|
|
logger.InfoToConsole("file %q deleted after successful load", s.LoadDataFrom)
|
2020-10-20 16:42:37 +00:00
|
|
|
} else {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Warn(logSender, "", "unable to delete file %q after successful load: %v", s.LoadDataFrom, err)
|
|
|
|
logger.WarnToConsole("unable to delete file %q after successful load: %v", s.LoadDataFrom, err)
|
2020-10-20 16:42:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-11 07:33:32 +00:00
|
|
|
|
2021-02-24 18:40:29 +00:00
|
|
|
func (s *Service) restoreDump(dump *dataprovider.BackupData) error {
|
2023-02-19 18:03:45 +00:00
|
|
|
err := httpd.RestoreConfigs(dump.Configs, s.LoadDataFrom, s.LoadDataMode, dataprovider.ActionExecutorSystem, "", "")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to restore configs from file %q: %v", s.LoadDataFrom, err)
|
|
|
|
}
|
|
|
|
err = httpd.RestoreIPListEntries(dump.IPLists, s.LoadDataFrom, s.LoadDataMode, dataprovider.ActionExecutorSystem, "", "")
|
2023-02-09 08:33:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to restore IP list entries from file %q: %v", s.LoadDataFrom, err)
|
|
|
|
}
|
|
|
|
err = httpd.RestoreRoles(dump.Roles, s.LoadDataFrom, s.LoadDataMode, dataprovider.ActionExecutorSystem, "", "")
|
2022-11-16 18:04:50 +00:00
|
|
|
if err != nil {
|
2023-02-09 08:33:33 +00:00
|
|
|
return fmt.Errorf("unable to restore roles from file %q: %v", s.LoadDataFrom, err)
|
2022-11-16 18:04:50 +00:00
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = httpd.RestoreFolders(dump.Folders, s.LoadDataFrom, s.LoadDataMode, s.LoadDataQuotaScan, dataprovider.ActionExecutorSystem, "", "")
|
2021-08-17 16:08:32 +00:00
|
|
|
if err != nil {
|
2023-02-09 08:33:33 +00:00
|
|
|
return fmt.Errorf("unable to restore folders from file %q: %v", s.LoadDataFrom, err)
|
2021-08-17 16:08:32 +00:00
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = httpd.RestoreGroups(dump.Groups, s.LoadDataFrom, s.LoadDataMode, dataprovider.ActionExecutorSystem, "", "")
|
2022-04-25 13:49:11 +00:00
|
|
|
if err != nil {
|
2023-02-09 08:33:33 +00:00
|
|
|
return fmt.Errorf("unable to restore groups from file %q: %v", s.LoadDataFrom, err)
|
2022-04-25 13:49:11 +00:00
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = httpd.RestoreUsers(dump.Users, s.LoadDataFrom, s.LoadDataMode, s.LoadDataQuotaScan, dataprovider.ActionExecutorSystem, "", "")
|
2021-02-11 07:33:32 +00:00
|
|
|
if err != nil {
|
2023-02-09 08:33:33 +00:00
|
|
|
return fmt.Errorf("unable to restore users from file %q: %v", s.LoadDataFrom, err)
|
2021-02-11 07:33:32 +00:00
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = httpd.RestoreAdmins(dump.Admins, s.LoadDataFrom, s.LoadDataMode, dataprovider.ActionExecutorSystem, "", "")
|
2021-02-11 07:33:32 +00:00
|
|
|
if err != nil {
|
2023-02-09 08:33:33 +00:00
|
|
|
return fmt.Errorf("unable to restore admins from file %q: %v", s.LoadDataFrom, err)
|
2021-02-11 07:33:32 +00:00
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = httpd.RestoreAPIKeys(dump.APIKeys, s.LoadDataFrom, s.LoadDataMode, dataprovider.ActionExecutorSystem, "", "")
|
2021-02-11 07:33:32 +00:00
|
|
|
if err != nil {
|
2023-02-09 08:33:33 +00:00
|
|
|
return fmt.Errorf("unable to restore API keys from file %q: %v", s.LoadDataFrom, err)
|
2021-02-11 07:33:32 +00:00
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = httpd.RestoreShares(dump.Shares, s.LoadDataFrom, s.LoadDataMode, dataprovider.ActionExecutorSystem, "", "")
|
2021-11-06 13:13:20 +00:00
|
|
|
if err != nil {
|
2023-02-09 08:33:33 +00:00
|
|
|
return fmt.Errorf("unable to restore API keys from file %q: %v", s.LoadDataFrom, err)
|
2021-11-06 13:13:20 +00:00
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = httpd.RestoreEventActions(dump.EventActions, s.LoadDataFrom, s.LoadDataMode, dataprovider.ActionExecutorSystem, "", "")
|
2022-07-11 06:17:36 +00:00
|
|
|
if err != nil {
|
2023-02-09 08:33:33 +00:00
|
|
|
return fmt.Errorf("unable to restore event actions from file %q: %v", s.LoadDataFrom, err)
|
2022-07-11 06:17:36 +00:00
|
|
|
}
|
2023-01-19 17:33:04 +00:00
|
|
|
err = httpd.RestoreEventRules(dump.EventRules, s.LoadDataFrom, s.LoadDataMode, dataprovider.ActionExecutorSystem,
|
|
|
|
"", "", dump.Version)
|
2022-07-11 06:17:36 +00:00
|
|
|
if err != nil {
|
2023-02-09 08:33:33 +00:00
|
|
|
return fmt.Errorf("unable to restore event rules from file %q: %v", s.LoadDataFrom, err)
|
2022-07-11 06:17:36 +00:00
|
|
|
}
|
2021-02-11 07:33:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-10-22 09:56:41 +00:00
|
|
|
|
|
|
|
// SetGraceTime sets the grace time
|
|
|
|
func SetGraceTime(val int) {
|
|
|
|
graceTime = val
|
|
|
|
}
|