2021-08-19 13:51:43 +00:00
|
|
|
//go:build !noportable
|
2020-05-23 09:58:05 +00:00
|
|
|
// +build !noportable
|
|
|
|
|
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grandcat/zeroconf"
|
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/config"
|
|
|
|
"github.com/drakkan/sftpgo/v2/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/ftpd"
|
|
|
|
"github.com/drakkan/sftpgo/v2/kms"
|
|
|
|
"github.com/drakkan/sftpgo/v2/logger"
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/sdk"
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/sftpd"
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/version"
|
|
|
|
"github.com/drakkan/sftpgo/v2/webdavd"
|
2020-05-23 09:58:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// StartPortableMode starts the service in portable mode
|
2021-03-06 14:41:40 +00:00
|
|
|
func (s *Service) StartPortableMode(sftpdPort, ftpPort, webdavPort int, enabledSSHCommands []string, advertiseService,
|
|
|
|
advertiseCredentials bool, ftpsCert, ftpsKey, webDavCert, webDavKey string) error {
|
2020-05-23 09:58:05 +00:00
|
|
|
if s.PortableMode != 1 {
|
|
|
|
return fmt.Errorf("service is not configured for portable mode")
|
|
|
|
}
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
2020-10-25 20:42:43 +00:00
|
|
|
err := config.LoadConfig(s.ConfigDir, s.ConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error loading configuration file: %v using defaults\n", err)
|
|
|
|
}
|
2020-11-30 20:46:34 +00:00
|
|
|
kmsConfig := config.GetKMSConfig()
|
|
|
|
err = kmsConfig.Initialize()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-24 12:44:57 +00:00
|
|
|
printablePassword := s.configurePortableUser()
|
2020-05-23 09:58:05 +00:00
|
|
|
dataProviderConf := config.GetProviderConf()
|
|
|
|
dataProviderConf.Driver = dataprovider.MemoryDataProviderName
|
|
|
|
dataProviderConf.Name = ""
|
2020-10-22 08:42:40 +00:00
|
|
|
dataProviderConf.PreferDatabaseCredentials = true
|
2020-05-23 09:58:05 +00:00
|
|
|
config.SetProviderConf(dataProviderConf)
|
|
|
|
httpdConf := config.GetHTTPDConfig()
|
2021-01-19 17:59:41 +00:00
|
|
|
httpdConf.Bindings = nil
|
2020-05-23 09:58:05 +00:00
|
|
|
config.SetHTTPDConfig(httpdConf)
|
2021-03-06 14:41:40 +00:00
|
|
|
telemetryConf := config.GetTelemetryConfig()
|
|
|
|
telemetryConf.BindPort = 0
|
|
|
|
config.SetTelemetryConfig(telemetryConf)
|
2020-05-23 09:58:05 +00:00
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
sftpdConf.MaxAuthTries = 12
|
2020-12-23 15:12:30 +00:00
|
|
|
sftpdConf.Bindings = []sftpd.Binding{
|
|
|
|
{
|
|
|
|
Port: sftpdPort,
|
|
|
|
},
|
|
|
|
}
|
2020-11-24 12:44:57 +00:00
|
|
|
if sftpdPort >= 0 {
|
|
|
|
if sftpdPort > 0 {
|
2020-12-23 15:12:30 +00:00
|
|
|
sftpdConf.Bindings[0].Port = sftpdPort
|
2020-11-24 12:44:57 +00:00
|
|
|
} else {
|
|
|
|
// dynamic ports starts from 49152
|
2020-12-23 15:12:30 +00:00
|
|
|
sftpdConf.Bindings[0].Port = 49152 + rand.Intn(15000)
|
2020-11-24 12:44:57 +00:00
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
if util.IsStringInSlice("*", enabledSSHCommands) {
|
2020-11-24 12:44:57 +00:00
|
|
|
sftpdConf.EnabledSSHCommands = sftpd.GetSupportedSSHCommands()
|
|
|
|
} else {
|
|
|
|
sftpdConf.EnabledSSHCommands = enabledSSHCommands
|
|
|
|
}
|
2020-05-23 09:58:05 +00:00
|
|
|
}
|
|
|
|
config.SetSFTPDConfig(sftpdConf)
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
if ftpPort >= 0 {
|
|
|
|
ftpConf := config.GetFTPDConfig()
|
2020-12-23 15:12:30 +00:00
|
|
|
binding := ftpd.Binding{}
|
2020-07-29 19:56:56 +00:00
|
|
|
if ftpPort > 0 {
|
2020-12-23 15:12:30 +00:00
|
|
|
binding.Port = ftpPort
|
2020-07-29 19:56:56 +00:00
|
|
|
} else {
|
2020-12-23 15:12:30 +00:00
|
|
|
binding.Port = 49152 + rand.Intn(15000)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2020-12-23 15:12:30 +00:00
|
|
|
ftpConf.Bindings = []ftpd.Binding{binding}
|
2020-07-29 19:56:56 +00:00
|
|
|
ftpConf.Banner = fmt.Sprintf("SFTPGo portable %v ready", version.Get().Version)
|
|
|
|
ftpConf.CertificateFile = ftpsCert
|
|
|
|
ftpConf.CertificateKeyFile = ftpsKey
|
|
|
|
config.SetFTPDConfig(ftpConf)
|
|
|
|
}
|
|
|
|
|
2020-09-21 14:08:32 +00:00
|
|
|
if webdavPort >= 0 {
|
2020-08-17 12:08:08 +00:00
|
|
|
webDavConf := config.GetWebDAVDConfig()
|
2020-12-23 15:12:30 +00:00
|
|
|
binding := webdavd.Binding{}
|
2020-08-17 12:08:08 +00:00
|
|
|
if webdavPort > 0 {
|
2020-12-23 15:12:30 +00:00
|
|
|
binding.Port = webdavPort
|
2020-08-17 12:08:08 +00:00
|
|
|
} else {
|
2020-12-23 15:12:30 +00:00
|
|
|
binding.Port = 49152 + rand.Intn(15000)
|
2020-08-17 12:08:08 +00:00
|
|
|
}
|
2021-03-05 07:41:24 +00:00
|
|
|
webDavConf.Bindings = []webdavd.Binding{binding}
|
2020-08-17 12:08:08 +00:00
|
|
|
webDavConf.CertificateFile = webDavCert
|
|
|
|
webDavConf.CertificateKeyFile = webDavKey
|
|
|
|
config.SetWebDAVDConfig(webDavConf)
|
|
|
|
}
|
|
|
|
|
2020-05-23 09:58:05 +00:00
|
|
|
err = s.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
|
|
|
|
s.advertiseServices(advertiseService, advertiseCredentials)
|
|
|
|
|
2020-11-24 12:44:57 +00:00
|
|
|
logger.InfoToConsole("Portable mode ready, user: %#v, password: %#v, public keys: %v, directory: %#v, "+
|
|
|
|
"permissions: %+v, enabled ssh commands: %v file patterns filters: %+v %v", s.PortableUser.Username,
|
2020-07-29 19:56:56 +00:00
|
|
|
printablePassword, s.PortableUser.PublicKeys, s.getPortableDirToServe(), s.PortableUser.Permissions,
|
2020-11-15 21:04:48 +00:00
|
|
|
sftpdConf.EnabledSSHCommands, s.PortableUser.Filters.FilePatterns, s.getServiceOptionalInfoString())
|
2020-07-29 19:56:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-17 12:08:08 +00:00
|
|
|
func (s *Service) getServiceOptionalInfoString() string {
|
|
|
|
var info strings.Builder
|
2020-12-23 15:12:30 +00:00
|
|
|
if config.GetSFTPDConfig().Bindings[0].IsValid() {
|
|
|
|
info.WriteString(fmt.Sprintf("SFTP port: %v ", config.GetSFTPDConfig().Bindings[0].Port))
|
2020-11-24 12:44:57 +00:00
|
|
|
}
|
2020-12-23 15:12:30 +00:00
|
|
|
if config.GetFTPDConfig().Bindings[0].IsValid() {
|
|
|
|
info.WriteString(fmt.Sprintf("FTP port: %v ", config.GetFTPDConfig().Bindings[0].Port))
|
2020-08-17 12:08:08 +00:00
|
|
|
}
|
2020-12-23 15:12:30 +00:00
|
|
|
if config.GetWebDAVDConfig().Bindings[0].IsValid() {
|
2020-08-17 12:08:08 +00:00
|
|
|
scheme := "http"
|
2020-11-24 12:44:57 +00:00
|
|
|
if config.GetWebDAVDConfig().CertificateFile != "" && config.GetWebDAVDConfig().CertificateKeyFile != "" {
|
2020-08-17 12:08:08 +00:00
|
|
|
scheme = "https"
|
|
|
|
}
|
2021-03-05 07:41:24 +00:00
|
|
|
info.WriteString(fmt.Sprintf("WebDAV URL: %v://<your IP>:%v/", scheme, config.GetWebDAVDConfig().Bindings[0].Port))
|
2020-08-17 12:08:08 +00:00
|
|
|
}
|
|
|
|
return info.String()
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
func (s *Service) advertiseServices(advertiseService, advertiseCredentials bool) {
|
|
|
|
var mDNSServiceSFTP *zeroconf.Server
|
|
|
|
var mDNSServiceFTP *zeroconf.Server
|
2020-09-21 14:08:32 +00:00
|
|
|
var mDNSServiceDAV *zeroconf.Server
|
2020-07-29 19:56:56 +00:00
|
|
|
var err error
|
2020-09-21 14:08:32 +00:00
|
|
|
|
2020-05-23 09:58:05 +00:00
|
|
|
if advertiseService {
|
|
|
|
meta := []string{
|
2020-07-29 19:56:56 +00:00
|
|
|
fmt.Sprintf("version=%v", version.Get().Version),
|
2020-05-23 09:58:05 +00:00
|
|
|
}
|
|
|
|
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))
|
|
|
|
} else {
|
|
|
|
logger.InfoToConsole("Unable to advertise key based credentials via multicast DNS, we don't have the private key")
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
2020-12-23 15:12:30 +00:00
|
|
|
if sftpdConf.Bindings[0].IsValid() {
|
2020-09-21 14:08:32 +00:00
|
|
|
mDNSServiceSFTP, err = zeroconf.Register(
|
2020-12-23 15:12:30 +00:00
|
|
|
fmt.Sprintf("SFTPGo portable %v", sftpdConf.Bindings[0].Port), // service instance name
|
|
|
|
"_sftp-ssh._tcp", // service type and protocol
|
|
|
|
"local.", // service domain
|
|
|
|
sftpdConf.Bindings[0].Port, // service port
|
|
|
|
meta, // service metadata
|
|
|
|
nil, // register on all network interfaces
|
2020-09-21 14:08:32 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
mDNSServiceSFTP = nil
|
|
|
|
logger.WarnToConsole("Unable to advertise SFTP service via multicast DNS: %v", err)
|
|
|
|
} else {
|
|
|
|
logger.InfoToConsole("SFTP service advertised via multicast DNS")
|
|
|
|
}
|
2020-05-23 09:58:05 +00:00
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
ftpdConf := config.GetFTPDConfig()
|
2020-12-23 15:12:30 +00:00
|
|
|
if ftpdConf.Bindings[0].IsValid() {
|
|
|
|
port := ftpdConf.Bindings[0].Port
|
2020-09-21 14:08:32 +00:00
|
|
|
mDNSServiceFTP, err = zeroconf.Register(
|
2020-12-23 15:12:30 +00:00
|
|
|
fmt.Sprintf("SFTPGo portable %v", port),
|
2020-09-21 14:08:32 +00:00
|
|
|
"_ftp._tcp",
|
|
|
|
"local.",
|
2020-12-23 15:12:30 +00:00
|
|
|
port,
|
2020-09-21 14:08:32 +00:00
|
|
|
meta,
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
mDNSServiceFTP = nil
|
|
|
|
logger.WarnToConsole("Unable to advertise FTP service via multicast DNS: %v", err)
|
|
|
|
} else {
|
|
|
|
logger.InfoToConsole("FTP service advertised via multicast DNS")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
webdavConf := config.GetWebDAVDConfig()
|
2020-12-23 15:12:30 +00:00
|
|
|
if webdavConf.Bindings[0].IsValid() {
|
2020-09-21 14:08:32 +00:00
|
|
|
mDNSServiceDAV, err = zeroconf.Register(
|
2020-12-23 15:12:30 +00:00
|
|
|
fmt.Sprintf("SFTPGo portable %v", webdavConf.Bindings[0].Port),
|
2020-09-21 14:08:32 +00:00
|
|
|
"_http._tcp",
|
|
|
|
"local.",
|
2020-12-23 15:12:30 +00:00
|
|
|
webdavConf.Bindings[0].Port,
|
2020-09-21 14:08:32 +00:00
|
|
|
meta,
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
mDNSServiceDAV = nil
|
|
|
|
logger.WarnToConsole("Unable to advertise WebDAV service via multicast DNS: %v", err)
|
|
|
|
} else {
|
|
|
|
logger.InfoToConsole("WebDAV service advertised via multicast DNS")
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2020-05-23 09:58:05 +00:00
|
|
|
}
|
|
|
|
sig := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
<-sig
|
2020-07-29 19:56:56 +00:00
|
|
|
if mDNSServiceSFTP != nil {
|
|
|
|
logger.InfoToConsole("unregistering multicast DNS SFTP service")
|
|
|
|
mDNSServiceSFTP.Shutdown()
|
|
|
|
}
|
|
|
|
if mDNSServiceFTP != nil {
|
|
|
|
logger.InfoToConsole("unregistering multicast DNS FTP service")
|
|
|
|
mDNSServiceFTP.Shutdown()
|
2020-05-23 09:58:05 +00:00
|
|
|
}
|
2020-09-21 14:08:32 +00:00
|
|
|
if mDNSServiceDAV != nil {
|
|
|
|
logger.InfoToConsole("unregistering multicast DNS WebDAV service")
|
|
|
|
mDNSServiceDAV.Shutdown()
|
|
|
|
}
|
2020-05-23 09:58:05 +00:00
|
|
|
s.Stop()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) getPortableDirToServe() string {
|
|
|
|
var dirToServe string
|
2021-07-11 13:26:51 +00:00
|
|
|
if s.PortableUser.FsConfig.Provider == sdk.S3FilesystemProvider {
|
2020-05-23 09:58:05 +00:00
|
|
|
dirToServe = s.PortableUser.FsConfig.S3Config.KeyPrefix
|
2021-07-11 13:26:51 +00:00
|
|
|
} else if s.PortableUser.FsConfig.Provider == sdk.GCSFilesystemProvider {
|
2020-05-23 09:58:05 +00:00
|
|
|
dirToServe = s.PortableUser.FsConfig.GCSConfig.KeyPrefix
|
|
|
|
} else {
|
|
|
|
dirToServe = s.PortableUser.HomeDir
|
|
|
|
}
|
|
|
|
return dirToServe
|
|
|
|
}
|
2020-11-24 12:44:57 +00:00
|
|
|
|
|
|
|
// configures the portable user and return the printable password if any
|
|
|
|
func (s *Service) configurePortableUser() string {
|
|
|
|
if s.PortableUser.Username == "" {
|
|
|
|
s.PortableUser.Username = "user"
|
|
|
|
}
|
|
|
|
printablePassword := ""
|
|
|
|
if len(s.PortableUser.Password) > 0 {
|
|
|
|
printablePassword = "[redacted]"
|
|
|
|
}
|
2021-01-05 08:50:22 +00:00
|
|
|
if len(s.PortableUser.PublicKeys) == 0 && s.PortableUser.Password == "" {
|
2020-11-24 12:44:57 +00:00
|
|
|
var b strings.Builder
|
|
|
|
for i := 0; i < 8; i++ {
|
|
|
|
b.WriteRune(chars[rand.Intn(len(chars))])
|
|
|
|
}
|
|
|
|
s.PortableUser.Password = b.String()
|
|
|
|
printablePassword = s.PortableUser.Password
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
s.configurePortableSecrets()
|
|
|
|
return printablePassword
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) configurePortableSecrets() {
|
2020-11-30 20:46:34 +00:00
|
|
|
// we created the user before to initialize the KMS so we need to create the secret here
|
|
|
|
switch s.PortableUser.FsConfig.Provider {
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.S3FilesystemProvider:
|
2020-11-30 20:46:34 +00:00
|
|
|
payload := s.PortableUser.FsConfig.S3Config.AccessSecret.GetPayload()
|
2020-12-12 09:31:09 +00:00
|
|
|
s.PortableUser.FsConfig.S3Config.AccessSecret = kms.NewEmptySecret()
|
2020-11-30 20:46:34 +00:00
|
|
|
if payload != "" {
|
|
|
|
s.PortableUser.FsConfig.S3Config.AccessSecret = kms.NewPlainSecret(payload)
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.GCSFilesystemProvider:
|
2020-11-30 20:46:34 +00:00
|
|
|
payload := s.PortableUser.FsConfig.GCSConfig.Credentials.GetPayload()
|
2020-12-12 09:31:09 +00:00
|
|
|
s.PortableUser.FsConfig.GCSConfig.Credentials = kms.NewEmptySecret()
|
2020-11-30 20:46:34 +00:00
|
|
|
if payload != "" {
|
|
|
|
s.PortableUser.FsConfig.GCSConfig.Credentials = kms.NewPlainSecret(payload)
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.AzureBlobFilesystemProvider:
|
2020-11-30 20:46:34 +00:00
|
|
|
payload := s.PortableUser.FsConfig.AzBlobConfig.AccountKey.GetPayload()
|
2020-12-12 09:31:09 +00:00
|
|
|
s.PortableUser.FsConfig.AzBlobConfig.AccountKey = kms.NewEmptySecret()
|
2020-11-30 20:46:34 +00:00
|
|
|
if payload != "" {
|
|
|
|
s.PortableUser.FsConfig.AzBlobConfig.AccountKey = kms.NewPlainSecret(payload)
|
|
|
|
}
|
2021-06-11 20:27:36 +00:00
|
|
|
payload = s.PortableUser.FsConfig.AzBlobConfig.SASURL.GetPayload()
|
|
|
|
s.PortableUser.FsConfig.AzBlobConfig.SASURL = kms.NewEmptySecret()
|
|
|
|
if payload != "" {
|
|
|
|
s.PortableUser.FsConfig.AzBlobConfig.SASURL = kms.NewPlainSecret(payload)
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.CryptedFilesystemProvider:
|
2020-12-05 12:48:13 +00:00
|
|
|
payload := s.PortableUser.FsConfig.CryptConfig.Passphrase.GetPayload()
|
2020-12-12 09:31:09 +00:00
|
|
|
s.PortableUser.FsConfig.CryptConfig.Passphrase = kms.NewEmptySecret()
|
2020-12-05 12:48:13 +00:00
|
|
|
if payload != "" {
|
|
|
|
s.PortableUser.FsConfig.CryptConfig.Passphrase = kms.NewPlainSecret(payload)
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.SFTPFilesystemProvider:
|
2020-12-12 09:31:09 +00:00
|
|
|
payload := s.PortableUser.FsConfig.SFTPConfig.Password.GetPayload()
|
|
|
|
s.PortableUser.FsConfig.SFTPConfig.Password = kms.NewEmptySecret()
|
|
|
|
if payload != "" {
|
|
|
|
s.PortableUser.FsConfig.SFTPConfig.Password = kms.NewPlainSecret(payload)
|
|
|
|
}
|
|
|
|
payload = s.PortableUser.FsConfig.SFTPConfig.PrivateKey.GetPayload()
|
|
|
|
s.PortableUser.FsConfig.SFTPConfig.PrivateKey = kms.NewEmptySecret()
|
|
|
|
if payload != "" {
|
|
|
|
s.PortableUser.FsConfig.SFTPConfig.PrivateKey = kms.NewPlainSecret(payload)
|
|
|
|
}
|
2020-11-30 20:46:34 +00:00
|
|
|
}
|
2020-11-24 12:44:57 +00:00
|
|
|
}
|