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"
|
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/config"
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
2020-11-30 20:46:34 +00:00
|
|
|
"github.com/drakkan/sftpgo/kms"
|
2020-05-23 09:58:05 +00:00
|
|
|
"github.com/drakkan/sftpgo/logger"
|
|
|
|
"github.com/drakkan/sftpgo/sftpd"
|
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2020-06-19 15:08:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/version"
|
2020-05-23 09:58:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// StartPortableMode starts the service in portable mode
|
2020-08-17 12:08:08 +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()
|
|
|
|
httpdConf.BindPort = 0
|
|
|
|
config.SetHTTPDConfig(httpdConf)
|
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
sftpdConf.MaxAuthTries = 12
|
2020-11-24 12:44:57 +00:00
|
|
|
sftpdConf.BindPort = sftpdPort
|
|
|
|
if sftpdPort >= 0 {
|
|
|
|
if sftpdPort > 0 {
|
|
|
|
sftpdConf.BindPort = sftpdPort
|
|
|
|
} else {
|
|
|
|
// dynamic ports starts from 49152
|
|
|
|
sftpdConf.BindPort = 49152 + rand.Intn(15000)
|
|
|
|
}
|
|
|
|
if utils.IsStringInSlice("*", enabledSSHCommands) {
|
|
|
|
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()
|
|
|
|
if ftpPort > 0 {
|
|
|
|
ftpConf.BindPort = ftpPort
|
|
|
|
} else {
|
|
|
|
ftpConf.BindPort = 49152 + rand.Intn(15000)
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
if webdavPort > 0 {
|
|
|
|
webDavConf.BindPort = webdavPort
|
|
|
|
} else {
|
|
|
|
webDavConf.BindPort = 49152 + rand.Intn(15000)
|
|
|
|
}
|
|
|
|
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-11-24 12:44:57 +00:00
|
|
|
if config.GetSFTPDConfig().BindPort > 0 {
|
|
|
|
info.WriteString(fmt.Sprintf("SFTP port: %v ", config.GetSFTPDConfig().BindPort))
|
|
|
|
}
|
2020-08-17 12:08:08 +00:00
|
|
|
if config.GetFTPDConfig().BindPort > 0 {
|
|
|
|
info.WriteString(fmt.Sprintf("FTP port: %v ", config.GetFTPDConfig().BindPort))
|
|
|
|
}
|
|
|
|
if config.GetWebDAVDConfig().BindPort > 0 {
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
info.WriteString(fmt.Sprintf("WebDAV URL: %v://<your IP>:%v/%v",
|
|
|
|
scheme, config.GetWebDAVDConfig().BindPort, s.PortableUser.Username))
|
|
|
|
}
|
|
|
|
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-09-21 14:08:32 +00:00
|
|
|
if sftpdConf.BindPort > 0 {
|
|
|
|
mDNSServiceSFTP, 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 {
|
|
|
|
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-09-21 14:08:32 +00:00
|
|
|
if ftpdConf.BindPort > 0 {
|
|
|
|
mDNSServiceFTP, err = zeroconf.Register(
|
|
|
|
fmt.Sprintf("SFTPGo portable %v", ftpdConf.BindPort),
|
|
|
|
"_ftp._tcp",
|
|
|
|
"local.",
|
|
|
|
ftpdConf.BindPort,
|
|
|
|
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()
|
|
|
|
if webdavConf.BindPort > 0 {
|
|
|
|
mDNSServiceDAV, err = zeroconf.Register(
|
|
|
|
fmt.Sprintf("SFTPGo portable %v", webdavConf.BindPort),
|
|
|
|
"_http._tcp",
|
|
|
|
"local.",
|
|
|
|
webdavConf.BindPort,
|
|
|
|
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
|
2020-10-05 18:58:41 +00:00
|
|
|
if s.PortableUser.FsConfig.Provider == dataprovider.S3FilesystemProvider {
|
2020-05-23 09:58:05 +00:00
|
|
|
dirToServe = s.PortableUser.FsConfig.S3Config.KeyPrefix
|
2020-10-05 18:58:41 +00:00
|
|
|
} else if s.PortableUser.FsConfig.Provider == dataprovider.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]"
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
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 {
|
|
|
|
case dataprovider.S3FilesystemProvider:
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
case dataprovider.GCSFilesystemProvider:
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
case dataprovider.AzureBlobFilesystemProvider:
|
|
|
|
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)
|
|
|
|
}
|
2020-12-05 12:48:13 +00:00
|
|
|
case dataprovider.CryptedFilesystemProvider:
|
|
|
|
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)
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
case dataprovider.SFTPFilesystemProvider:
|
|
|
|
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
|
|
|
}
|