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
|
|
|
|
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"
|
|
|
|
"strings"
|
|
|
|
|
2022-01-06 10:54:43 +00:00
|
|
|
"github.com/sftpgo/sdk"
|
2020-05-23 09:58:05 +00:00
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/config"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/ftpd"
|
2023-08-10 17:02:55 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/httpd"
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/kms"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/sftpd"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/util"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/version"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/webdavd"
|
2020-05-23 09:58:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// StartPortableMode starts the service in portable mode
|
2023-08-10 17:02:55 +00:00
|
|
|
func (s *Service) StartPortableMode(sftpdPort, ftpPort, webdavPort, httpPort int, enabledSSHCommands []string,
|
|
|
|
ftpsCert, ftpsKey, webDavCert, webDavKey, httpsCert, httpsKey string) error {
|
2020-05-23 09:58:05 +00:00
|
|
|
if s.PortableMode != 1 {
|
|
|
|
return fmt.Errorf("service is not configured for portable mode")
|
|
|
|
}
|
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 = ""
|
|
|
|
config.SetProviderConf(dataProviderConf)
|
|
|
|
httpdConf := config.GetHTTPDConfig()
|
2023-08-10 17:02:55 +00:00
|
|
|
for idx := range httpdConf.Bindings {
|
|
|
|
httpdConf.Bindings[idx].Port = 0
|
|
|
|
}
|
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)
|
2023-08-10 17:02:55 +00:00
|
|
|
|
2023-12-17 08:09:18 +00:00
|
|
|
configurePortableSFTPService(sftpdPort, enabledSSHCommands)
|
|
|
|
configurePortableFTPService(ftpPort, ftpsCert, ftpsKey)
|
|
|
|
configurePortableWebDAVService(webdavPort, webDavCert, webDavKey)
|
|
|
|
configurePortableHTTPService(httpPort, httpsCert, httpsKey)
|
2020-08-17 12:08:08 +00:00
|
|
|
|
2022-05-07 10:50:49 +00:00
|
|
|
err = s.Start(true)
|
2020-05-23 09:58:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-10 17:02:55 +00:00
|
|
|
if httpPort >= 0 {
|
|
|
|
admin := &dataprovider.Admin{
|
|
|
|
Username: util.GenerateUniqueID(),
|
|
|
|
Password: util.GenerateUniqueID(),
|
|
|
|
Status: 0,
|
|
|
|
Permissions: []string{dataprovider.PermAdminAny},
|
|
|
|
}
|
|
|
|
if err := dataprovider.AddAdmin(admin, dataprovider.ActionExecutorSystem, "", ""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.InfoToConsole("Portable mode ready, user: %q, password: %q, public keys: %v, directory: %q, "+
|
2023-08-10 17:02:55 +00:00
|
|
|
"permissions: %+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,
|
2023-08-10 17:02:55 +00:00
|
|
|
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"
|
|
|
|
}
|
2023-08-10 17:02:55 +00:00
|
|
|
info.WriteString(fmt.Sprintf("WebDAV URL: %v://<your IP>:%v/ ", scheme, config.GetWebDAVDConfig().Bindings[0].Port))
|
|
|
|
}
|
|
|
|
if config.GetHTTPDConfig().Bindings[0].IsValid() {
|
|
|
|
scheme := "http"
|
|
|
|
if config.GetHTTPDConfig().CertificateFile != "" && config.GetHTTPDConfig().CertificateKeyFile != "" {
|
|
|
|
scheme = "https"
|
|
|
|
}
|
|
|
|
info.WriteString(fmt.Sprintf("WebClient URL: %v://<your IP>:%v/ ", scheme, config.GetHTTPDConfig().Bindings[0].Port))
|
2020-08-17 12:08:08 +00:00
|
|
|
}
|
|
|
|
return info.String()
|
|
|
|
}
|
|
|
|
|
2020-05-23 09:58:05 +00:00
|
|
|
func (s *Service) getPortableDirToServe() string {
|
2022-06-11 08:41:34 +00:00
|
|
|
switch s.PortableUser.FsConfig.Provider {
|
|
|
|
case sdk.S3FilesystemProvider:
|
|
|
|
return s.PortableUser.FsConfig.S3Config.KeyPrefix
|
|
|
|
case sdk.GCSFilesystemProvider:
|
|
|
|
return s.PortableUser.FsConfig.GCSConfig.KeyPrefix
|
|
|
|
case sdk.AzureBlobFilesystemProvider:
|
|
|
|
return s.PortableUser.FsConfig.AzBlobConfig.KeyPrefix
|
|
|
|
case sdk.SFTPFilesystemProvider:
|
|
|
|
return s.PortableUser.FsConfig.SFTPConfig.Prefix
|
|
|
|
case sdk.HTTPFilesystemProvider:
|
|
|
|
return "/"
|
|
|
|
default:
|
|
|
|
return s.PortableUser.HomeDir
|
2020-05-23 09:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
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 := ""
|
2022-02-27 12:08:47 +00:00
|
|
|
if s.PortableUser.Password != "" {
|
2020-11-24 12:44:57 +00:00
|
|
|
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
|
2023-08-10 17:02:55 +00:00
|
|
|
for i := 0; i < 16; i++ {
|
2020-11-24 12:44:57 +00:00
|
|
|
b.WriteRune(chars[rand.Intn(len(chars))])
|
|
|
|
}
|
|
|
|
s.PortableUser.Password = b.String()
|
|
|
|
printablePassword = s.PortableUser.Password
|
|
|
|
}
|
2023-08-10 17:02:55 +00:00
|
|
|
s.PortableUser.Filters.WebClient = []string{sdk.WebClientSharesDisabled, sdk.WebClientInfoChangeDisabled,
|
|
|
|
sdk.WebClientPubKeyChangeDisabled, sdk.WebClientPasswordChangeDisabled, sdk.WebClientAPIKeyAuthChangeDisabled,
|
|
|
|
sdk.WebClientMFADisabled,
|
|
|
|
}
|
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()
|
2022-06-11 08:41:34 +00:00
|
|
|
s.PortableUser.FsConfig.S3Config.AccessSecret = getSecretFromString(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()
|
2022-06-11 08:41:34 +00:00
|
|
|
s.PortableUser.FsConfig.GCSConfig.Credentials = getSecretFromString(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()
|
2022-06-11 08:41:34 +00:00
|
|
|
s.PortableUser.FsConfig.AzBlobConfig.AccountKey = getSecretFromString(payload)
|
2021-06-11 20:27:36 +00:00
|
|
|
payload = s.PortableUser.FsConfig.AzBlobConfig.SASURL.GetPayload()
|
2022-06-11 08:41:34 +00:00
|
|
|
s.PortableUser.FsConfig.AzBlobConfig.SASURL = getSecretFromString(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()
|
2022-06-11 08:41:34 +00:00
|
|
|
s.PortableUser.FsConfig.CryptConfig.Passphrase = getSecretFromString(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()
|
2022-06-11 08:41:34 +00:00
|
|
|
s.PortableUser.FsConfig.SFTPConfig.Password = getSecretFromString(payload)
|
2020-12-12 09:31:09 +00:00
|
|
|
payload = s.PortableUser.FsConfig.SFTPConfig.PrivateKey.GetPayload()
|
2022-06-11 08:41:34 +00:00
|
|
|
s.PortableUser.FsConfig.SFTPConfig.PrivateKey = getSecretFromString(payload)
|
|
|
|
payload = s.PortableUser.FsConfig.SFTPConfig.KeyPassphrase.GetPayload()
|
|
|
|
s.PortableUser.FsConfig.SFTPConfig.KeyPassphrase = getSecretFromString(payload)
|
|
|
|
case sdk.HTTPFilesystemProvider:
|
|
|
|
payload := s.PortableUser.FsConfig.HTTPConfig.Password.GetPayload()
|
|
|
|
s.PortableUser.FsConfig.HTTPConfig.Password = getSecretFromString(payload)
|
|
|
|
payload = s.PortableUser.FsConfig.HTTPConfig.APIKey.GetPayload()
|
|
|
|
s.PortableUser.FsConfig.HTTPConfig.APIKey = getSecretFromString(payload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSecretFromString(payload string) *kms.Secret {
|
|
|
|
if payload != "" {
|
|
|
|
return kms.NewPlainSecret(payload)
|
2020-11-30 20:46:34 +00:00
|
|
|
}
|
2022-06-11 08:41:34 +00:00
|
|
|
return kms.NewEmptySecret()
|
2020-11-24 12:44:57 +00:00
|
|
|
}
|
2023-08-10 17:02:55 +00:00
|
|
|
|
|
|
|
func configurePortableSFTPService(port int, enabledSSHCommands []string) {
|
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
|
|
|
if len(sftpdConf.Bindings) == 0 {
|
|
|
|
sftpdConf.Bindings = append(sftpdConf.Bindings, sftpd.Binding{})
|
|
|
|
}
|
|
|
|
if port > 0 {
|
|
|
|
sftpdConf.Bindings[0].Port = port
|
2023-12-17 08:09:18 +00:00
|
|
|
} else if port == 0 {
|
2023-08-10 17:02:55 +00:00
|
|
|
// dynamic ports starts from 49152
|
|
|
|
sftpdConf.Bindings[0].Port = 49152 + rand.Intn(15000)
|
2023-12-17 08:09:18 +00:00
|
|
|
} else {
|
|
|
|
sftpdConf.Bindings[0].Port = 0
|
2023-08-10 17:02:55 +00:00
|
|
|
}
|
|
|
|
if util.Contains(enabledSSHCommands, "*") {
|
|
|
|
sftpdConf.EnabledSSHCommands = sftpd.GetSupportedSSHCommands()
|
|
|
|
} else {
|
|
|
|
sftpdConf.EnabledSSHCommands = enabledSSHCommands
|
|
|
|
}
|
|
|
|
config.SetSFTPDConfig(sftpdConf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func configurePortableFTPService(port int, cert, key string) {
|
|
|
|
ftpConf := config.GetFTPDConfig()
|
|
|
|
if len(ftpConf.Bindings) == 0 {
|
|
|
|
ftpConf.Bindings = append(ftpConf.Bindings, ftpd.Binding{})
|
|
|
|
}
|
|
|
|
if port > 0 {
|
|
|
|
ftpConf.Bindings[0].Port = port
|
2023-12-17 08:09:18 +00:00
|
|
|
} else if port == 0 {
|
2023-08-10 17:02:55 +00:00
|
|
|
ftpConf.Bindings[0].Port = 49152 + rand.Intn(15000)
|
2023-12-17 08:09:18 +00:00
|
|
|
} else {
|
|
|
|
ftpConf.Bindings[0].Port = 0
|
2023-08-10 17:02:55 +00:00
|
|
|
}
|
|
|
|
if ftpConf.Banner == "" {
|
|
|
|
ftpConf.Banner = fmt.Sprintf("SFTPGo portable %v ready", version.Get().Version)
|
|
|
|
}
|
|
|
|
ftpConf.Bindings[0].CertificateFile = cert
|
|
|
|
ftpConf.Bindings[0].CertificateKeyFile = key
|
|
|
|
config.SetFTPDConfig(ftpConf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func configurePortableWebDAVService(port int, cert, key string) {
|
|
|
|
webDavConf := config.GetWebDAVDConfig()
|
|
|
|
if len(webDavConf.Bindings) == 0 {
|
|
|
|
webDavConf.Bindings = append(webDavConf.Bindings, webdavd.Binding{})
|
|
|
|
}
|
|
|
|
if port > 0 {
|
|
|
|
webDavConf.Bindings[0].Port = port
|
2023-12-17 08:09:18 +00:00
|
|
|
} else if port == 0 {
|
2023-08-10 17:02:55 +00:00
|
|
|
webDavConf.Bindings[0].Port = 49152 + rand.Intn(15000)
|
2023-12-17 08:09:18 +00:00
|
|
|
} else {
|
|
|
|
webDavConf.Bindings[0].Port = 0
|
2023-08-10 17:02:55 +00:00
|
|
|
}
|
|
|
|
webDavConf.Bindings[0].CertificateFile = cert
|
|
|
|
webDavConf.Bindings[0].CertificateKeyFile = key
|
2023-12-17 08:09:18 +00:00
|
|
|
if cert != "" && key != "" {
|
|
|
|
webDavConf.Bindings[0].EnableHTTPS = true
|
|
|
|
}
|
2023-08-10 17:02:55 +00:00
|
|
|
config.SetWebDAVDConfig(webDavConf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func configurePortableHTTPService(port int, cert, key string) {
|
|
|
|
httpdConf := config.GetHTTPDConfig()
|
|
|
|
if len(httpdConf.Bindings) == 0 {
|
|
|
|
httpdConf.Bindings = append(httpdConf.Bindings, httpd.Binding{})
|
|
|
|
}
|
|
|
|
if port > 0 {
|
|
|
|
httpdConf.Bindings[0].Port = port
|
2023-12-17 08:09:18 +00:00
|
|
|
} else if port == 0 {
|
2023-08-10 17:02:55 +00:00
|
|
|
httpdConf.Bindings[0].Port = 49152 + rand.Intn(15000)
|
2023-12-17 08:09:18 +00:00
|
|
|
} else {
|
|
|
|
httpdConf.Bindings[0].Port = 0
|
2023-08-10 17:02:55 +00:00
|
|
|
}
|
|
|
|
httpdConf.Bindings[0].CertificateFile = cert
|
|
|
|
httpdConf.Bindings[0].CertificateKeyFile = key
|
2023-12-17 08:09:18 +00:00
|
|
|
if cert != "" && key != "" {
|
|
|
|
httpdConf.Bindings[0].EnableHTTPS = true
|
|
|
|
}
|
2023-08-10 17:02:55 +00:00
|
|
|
httpdConf.Bindings[0].EnableWebAdmin = false
|
|
|
|
httpdConf.Bindings[0].EnableWebClient = true
|
|
|
|
httpdConf.Bindings[0].EnableRESTAPI = false
|
|
|
|
httpdConf.Bindings[0].RenderOpenAPI = false
|
|
|
|
config.SetHTTPDConfig(httpdConf)
|
|
|
|
}
|