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 (
|
2019-10-24 16:50:35 +00:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
2019-10-26 16:25:53 +00:00
|
|
|
"os/signal"
|
2020-01-31 18:04:00 +00:00
|
|
|
"path/filepath"
|
2019-10-24 16:50:35 +00:00
|
|
|
"strings"
|
2019-10-26 16:25:53 +00:00
|
|
|
"syscall"
|
2019-10-24 16:50:35 +00:00
|
|
|
"time"
|
|
|
|
|
2019-09-16 06:52:58 +00:00
|
|
|
"github.com/drakkan/sftpgo/config"
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
2019-10-07 16:19:01 +00:00
|
|
|
"github.com/drakkan/sftpgo/httpd"
|
2019-09-16 06:52:58 +00:00
|
|
|
"github.com/drakkan/sftpgo/logger"
|
|
|
|
"github.com/drakkan/sftpgo/sftpd"
|
2019-10-21 19:10:07 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2019-10-26 16:25:53 +00:00
|
|
|
"github.com/grandcat/zeroconf"
|
2019-09-16 06:52:58 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
ConfigDir string
|
|
|
|
ConfigFile string
|
|
|
|
LogFilePath string
|
|
|
|
LogMaxSize int
|
|
|
|
LogMaxBackups int
|
|
|
|
LogMaxAge int
|
|
|
|
LogCompress bool
|
|
|
|
LogVerbose bool
|
2019-10-24 16:50:35 +00:00
|
|
|
PortableMode int
|
|
|
|
PortableUser dataprovider.User
|
2019-09-16 06:52:58 +00:00
|
|
|
Shutdown chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start initializes the service
|
|
|
|
func (s *Service) Start() error {
|
|
|
|
logLevel := zerolog.DebugLevel
|
|
|
|
if !s.LogVerbose {
|
|
|
|
logLevel = zerolog.InfoLevel
|
|
|
|
}
|
2020-03-02 23:34:06 +00:00
|
|
|
if !filepath.IsAbs(s.LogFilePath) && len(s.LogFilePath) > 0 && s.LogFilePath != "." {
|
|
|
|
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)
|
|
|
|
if len(s.LogFilePath) == 0 {
|
|
|
|
logger.DisableLogger()
|
|
|
|
}
|
|
|
|
}
|
2019-10-21 19:18:45 +00:00
|
|
|
version := utils.GetAppVersion()
|
|
|
|
logger.Info(logSender, "", "starting SFTPGo %v, config dir: %v, config file: %v, log max size: %v log max backups: %v "+
|
|
|
|
"log max age: %v log verbose: %v, log compress: %v", version.GetVersionAsString(), s.ConfigDir, s.ConfigFile, s.LogMaxSize,
|
|
|
|
s.LogMaxBackups, s.LogMaxAge, s.LogVerbose, s.LogCompress)
|
2019-10-24 16:50:35 +00:00
|
|
|
// in portable mode we don't read configuration from file
|
|
|
|
if s.PortableMode != 1 {
|
|
|
|
config.LoadConfig(s.ConfigDir, s.ConfigFile)
|
|
|
|
}
|
2019-09-16 06:52:58 +00:00
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
|
|
|
|
err := dataprovider.Initialize(providerConf, s.ConfigDir)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(logSender, "", "error initializing data provider: %v", err)
|
|
|
|
logger.ErrorToConsole("error initializing data provider: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dataProvider := dataprovider.GetProvider()
|
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
httpdConf := config.GetHTTPDConfig()
|
|
|
|
|
2019-10-24 16:50:35 +00:00
|
|
|
if s.PortableMode == 1 {
|
|
|
|
// create the user for portable mode
|
|
|
|
err = dataprovider.AddUser(dataProvider, s.PortableUser)
|
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("error adding portable user: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 06:52:58 +00:00
|
|
|
sftpd.SetDataProvider(dataProvider)
|
|
|
|
|
|
|
|
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.Shutdown <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
if httpdConf.BindPort > 0 {
|
2019-10-07 16:19:01 +00:00
|
|
|
httpd.SetDataProvider(dataProvider)
|
2019-09-16 06:52:58 +00:00
|
|
|
|
|
|
|
go func() {
|
2019-10-07 16:19:01 +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)
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 06:52:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait blocks until the service exits
|
|
|
|
func (s *Service) Wait() {
|
2020-02-04 22:21:33 +00:00
|
|
|
if s.PortableMode != 1 {
|
|
|
|
registerSigHup()
|
|
|
|
}
|
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")
|
|
|
|
}
|
2019-10-24 16:50:35 +00:00
|
|
|
|
|
|
|
// StartPortableMode starts the service in portable mode
|
2019-11-18 22:30:37 +00:00
|
|
|
func (s *Service) StartPortableMode(sftpdPort int, enabledSSHCommands []string, advertiseService, advertiseCredentials bool) error {
|
2019-10-24 16:50:35 +00:00
|
|
|
if s.PortableMode != 1 {
|
|
|
|
return fmt.Errorf("service is not configured for portable mode")
|
|
|
|
}
|
2019-10-26 16:25:53 +00:00
|
|
|
var err error
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
2019-10-24 16:50:35 +00:00
|
|
|
if len(s.PortableUser.Username) == 0 {
|
|
|
|
s.PortableUser.Username = "user"
|
|
|
|
}
|
|
|
|
if len(s.PortableUser.PublicKeys) == 0 && len(s.PortableUser.Password) == 0 {
|
|
|
|
var b strings.Builder
|
|
|
|
for i := 0; i < 8; i++ {
|
|
|
|
b.WriteRune(chars[rand.Intn(len(chars))])
|
|
|
|
}
|
|
|
|
s.PortableUser.Password = b.String()
|
|
|
|
}
|
|
|
|
dataProviderConf := config.GetProviderConf()
|
2019-10-25 16:37:12 +00:00
|
|
|
dataProviderConf.Driver = dataprovider.MemoryDataProviderName
|
2020-02-14 13:08:42 +00:00
|
|
|
dataProviderConf.Name = ""
|
2020-01-31 18:04:00 +00:00
|
|
|
dataProviderConf.CredentialsPath = filepath.Join(os.TempDir(), "credentials")
|
2019-10-24 16:50:35 +00:00
|
|
|
config.SetProviderConf(dataProviderConf)
|
|
|
|
httpdConf := config.GetHTTPDConfig()
|
|
|
|
httpdConf.BindPort = 0
|
|
|
|
config.SetHTTPDConfig(httpdConf)
|
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
sftpdConf.MaxAuthTries = 12
|
|
|
|
if sftpdPort > 0 {
|
|
|
|
sftpdConf.BindPort = sftpdPort
|
|
|
|
} else {
|
|
|
|
// dynamic ports starts from 49152
|
|
|
|
sftpdConf.BindPort = 49152 + rand.Intn(15000)
|
|
|
|
}
|
2019-11-18 22:30:37 +00:00
|
|
|
if utils.IsStringInSlice("*", enabledSSHCommands) {
|
|
|
|
sftpdConf.EnabledSSHCommands = sftpd.GetSupportedSSHCommands()
|
|
|
|
} else {
|
|
|
|
sftpdConf.EnabledSSHCommands = enabledSSHCommands
|
|
|
|
}
|
2019-10-24 16:50:35 +00:00
|
|
|
config.SetSFTPDConfig(sftpdConf)
|
|
|
|
|
2019-10-26 16:25:53 +00:00
|
|
|
err = s.Start()
|
2020-02-19 08:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var mDNSService *zeroconf.Server
|
|
|
|
if advertiseService {
|
|
|
|
version := utils.GetAppVersion()
|
|
|
|
meta := []string{
|
|
|
|
fmt.Sprintf("version=%v", version.GetVersionAsString()),
|
|
|
|
}
|
|
|
|
if advertiseCredentials {
|
|
|
|
logger.InfoToConsole("Advertising credentials via multicast DNS")
|
|
|
|
meta = append(meta, fmt.Sprintf("user=%v", s.PortableUser.Username))
|
|
|
|
if len(s.PortableUser.Password) > 0 {
|
|
|
|
meta = append(meta, fmt.Sprintf("password=%v", s.PortableUser.Password))
|
2019-10-26 16:25:53 +00:00
|
|
|
} else {
|
2020-02-19 08:41:15 +00:00
|
|
|
logger.InfoToConsole("Unable to advertise key based credentials via multicast DNS, we don't have the private key")
|
2019-10-26 16:25:53 +00:00
|
|
|
}
|
2020-02-19 08:41:15 +00:00
|
|
|
}
|
|
|
|
mDNSService, err = zeroconf.Register(
|
|
|
|
fmt.Sprintf("SFTPGo portable %v", sftpdConf.BindPort), // service instance name
|
|
|
|
"_sftp-ssh._tcp", // service type and protocol
|
|
|
|
"local.", // service domain
|
|
|
|
sftpdConf.BindPort, // service port
|
|
|
|
meta, // service metadata
|
|
|
|
nil, // register on all network interfaces
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
mDNSService = nil
|
|
|
|
logger.WarnToConsole("Unable to advertise SFTP service via multicast DNS: %v", err)
|
|
|
|
} else {
|
|
|
|
logger.InfoToConsole("SFTP service advertised via multicast DNS")
|
|
|
|
}
|
2019-10-26 16:25:53 +00:00
|
|
|
|
2020-02-19 08:41:15 +00:00
|
|
|
}
|
|
|
|
sig := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
<-sig
|
|
|
|
if mDNSService != nil {
|
|
|
|
logger.InfoToConsole("unregistering multicast DNS service")
|
|
|
|
mDNSService.Shutdown()
|
2019-10-26 16:25:53 +00:00
|
|
|
}
|
2020-02-19 08:41:15 +00:00
|
|
|
s.Stop()
|
|
|
|
}()
|
2020-02-20 14:53:26 +00:00
|
|
|
|
|
|
|
logger.InfoToConsole("Portable mode ready, SFTP port: %v, user: %#v, password: %#v, public keys: %v, directory: %#v, "+
|
|
|
|
"permissions: %v, enabled ssh commands: %v", sftpdConf.BindPort, s.PortableUser.Username, s.PortableUser.Password,
|
|
|
|
s.PortableUser.PublicKeys, s.getPortableDirToServe(), s.PortableUser.Permissions, sftpdConf.EnabledSSHCommands)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) getPortableDirToServe() string {
|
2020-02-19 08:41:15 +00:00
|
|
|
var dirToServe string
|
|
|
|
if s.PortableUser.FsConfig.Provider == 1 {
|
|
|
|
dirToServe = s.PortableUser.FsConfig.S3Config.KeyPrefix
|
|
|
|
} else if s.PortableUser.FsConfig.Provider == 2 {
|
|
|
|
dirToServe = s.PortableUser.FsConfig.GCSConfig.KeyPrefix
|
|
|
|
} else {
|
|
|
|
dirToServe = s.PortableUser.HomeDir
|
2019-10-26 16:25:53 +00:00
|
|
|
}
|
2020-02-20 14:53:26 +00:00
|
|
|
return dirToServe
|
2019-10-24 16:50:35 +00:00
|
|
|
}
|