2020-05-23 09:58:05 +00:00
|
|
|
// +build !noportable
|
|
|
|
|
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grandcat/zeroconf"
|
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/config"
|
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
"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")
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
if len(s.PortableUser.Username) == 0 {
|
|
|
|
s.PortableUser.Username = "user"
|
|
|
|
}
|
2020-06-26 21:38:29 +00:00
|
|
|
printablePassword := ""
|
|
|
|
if len(s.PortableUser.Password) > 0 {
|
|
|
|
printablePassword = "[redacted]"
|
|
|
|
}
|
2020-05-23 09:58:05 +00:00
|
|
|
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()
|
2020-06-24 11:37:38 +00:00
|
|
|
printablePassword = s.PortableUser.Password
|
2020-05-23 09:58:05 +00:00
|
|
|
}
|
|
|
|
dataProviderConf := config.GetProviderConf()
|
|
|
|
dataProviderConf.Driver = dataprovider.MemoryDataProviderName
|
|
|
|
dataProviderConf.Name = ""
|
|
|
|
dataProviderConf.CredentialsPath = filepath.Join(os.TempDir(), "credentials")
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if utils.IsStringInSlice("*", enabledSSHCommands) {
|
|
|
|
sftpdConf.EnabledSSHCommands = sftpd.GetSupportedSSHCommands()
|
|
|
|
} else {
|
|
|
|
sftpdConf.EnabledSSHCommands = enabledSSHCommands
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
|
|
|
|
logger.InfoToConsole("Portable mode ready, SFTP port: %v, user: %#v, password: %#v, public keys: %v, directory: %#v, "+
|
|
|
|
"permissions: %+v, enabled ssh commands: %v file extensions filters: %+v %v", sftpdConf.BindPort, s.PortableUser.Username,
|
|
|
|
printablePassword, s.PortableUser.PublicKeys, s.getPortableDirToServe(), s.PortableUser.Permissions,
|
2020-08-17 12:08:08 +00:00
|
|
|
sftpdConf.EnabledSSHCommands, s.PortableUser.Filters.FileExtensions, 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
|
|
|
|
if config.GetFTPDConfig().BindPort > 0 {
|
|
|
|
info.WriteString(fmt.Sprintf("FTP port: %v ", config.GetFTPDConfig().BindPort))
|
|
|
|
}
|
|
|
|
if config.GetWebDAVDConfig().BindPort > 0 {
|
|
|
|
if info.Len() == 0 {
|
|
|
|
info.WriteString(" ")
|
|
|
|
}
|
|
|
|
scheme := "http"
|
|
|
|
if len(config.GetWebDAVDConfig().CertificateFile) > 0 && len(config.GetWebDAVDConfig().CertificateKeyFile) > 0 {
|
|
|
|
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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return dirToServe
|
|
|
|
}
|