service_portable.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Copyright (C) 2019 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. //go:build !noportable
  15. // +build !noportable
  16. package service
  17. import (
  18. "fmt"
  19. "math/rand"
  20. "slices"
  21. "strings"
  22. "github.com/sftpgo/sdk"
  23. "github.com/drakkan/sftpgo/v2/internal/config"
  24. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  25. "github.com/drakkan/sftpgo/v2/internal/ftpd"
  26. "github.com/drakkan/sftpgo/v2/internal/httpd"
  27. "github.com/drakkan/sftpgo/v2/internal/kms"
  28. "github.com/drakkan/sftpgo/v2/internal/logger"
  29. "github.com/drakkan/sftpgo/v2/internal/sftpd"
  30. "github.com/drakkan/sftpgo/v2/internal/util"
  31. "github.com/drakkan/sftpgo/v2/internal/webdavd"
  32. )
  33. // StartPortableMode starts the service in portable mode
  34. func (s *Service) StartPortableMode(sftpdPort, ftpPort, webdavPort, httpPort int, enabledSSHCommands []string,
  35. ftpsCert, ftpsKey, webDavCert, webDavKey, httpsCert, httpsKey string) error {
  36. if s.PortableMode != 1 {
  37. return fmt.Errorf("service is not configured for portable mode")
  38. }
  39. err := config.LoadConfig(s.ConfigDir, s.ConfigFile)
  40. if err != nil {
  41. fmt.Printf("error loading configuration file: %v using defaults\n", err)
  42. }
  43. kmsConfig := config.GetKMSConfig()
  44. err = kmsConfig.Initialize()
  45. if err != nil {
  46. return err
  47. }
  48. printablePassword := s.configurePortableUser()
  49. dataProviderConf := config.GetProviderConf()
  50. dataProviderConf.Driver = dataprovider.MemoryDataProviderName
  51. dataProviderConf.Name = ""
  52. config.SetProviderConf(dataProviderConf)
  53. httpdConf := config.GetHTTPDConfig()
  54. for idx := range httpdConf.Bindings {
  55. httpdConf.Bindings[idx].Port = 0
  56. }
  57. config.SetHTTPDConfig(httpdConf)
  58. telemetryConf := config.GetTelemetryConfig()
  59. telemetryConf.BindPort = 0
  60. config.SetTelemetryConfig(telemetryConf)
  61. configurePortableSFTPService(sftpdPort, enabledSSHCommands)
  62. configurePortableFTPService(ftpPort, ftpsCert, ftpsKey)
  63. configurePortableWebDAVService(webdavPort, webDavCert, webDavKey)
  64. configurePortableHTTPService(httpPort, httpsCert, httpsKey)
  65. err = s.Start(true)
  66. if err != nil {
  67. return err
  68. }
  69. if httpPort >= 0 {
  70. admin := &dataprovider.Admin{
  71. Username: util.GenerateUniqueID(),
  72. Password: util.GenerateUniqueID(),
  73. Status: 0,
  74. Permissions: []string{dataprovider.PermAdminAny},
  75. }
  76. if err := dataprovider.AddAdmin(admin, dataprovider.ActionExecutorSystem, "", ""); err != nil {
  77. return err
  78. }
  79. }
  80. logger.InfoToConsole("Portable mode ready, user: %q, password: %q, public keys: %v, directory: %q, "+
  81. "permissions: %+v, file patterns filters: %+v %v", s.PortableUser.Username,
  82. printablePassword, s.PortableUser.PublicKeys, s.getPortableDirToServe(), s.PortableUser.Permissions,
  83. s.PortableUser.Filters.FilePatterns, s.getServiceOptionalInfoString())
  84. return nil
  85. }
  86. func (s *Service) getServiceOptionalInfoString() string {
  87. var info strings.Builder
  88. if config.GetSFTPDConfig().Bindings[0].IsValid() {
  89. info.WriteString(fmt.Sprintf("SFTP port: %v ", config.GetSFTPDConfig().Bindings[0].Port))
  90. }
  91. if config.GetFTPDConfig().Bindings[0].IsValid() {
  92. info.WriteString(fmt.Sprintf("FTP port: %v ", config.GetFTPDConfig().Bindings[0].Port))
  93. }
  94. if config.GetWebDAVDConfig().Bindings[0].IsValid() {
  95. scheme := "http"
  96. if config.GetWebDAVDConfig().CertificateFile != "" && config.GetWebDAVDConfig().CertificateKeyFile != "" {
  97. scheme = "https"
  98. }
  99. info.WriteString(fmt.Sprintf("WebDAV URL: %v://<your IP>:%v/ ", scheme, config.GetWebDAVDConfig().Bindings[0].Port))
  100. }
  101. if config.GetHTTPDConfig().Bindings[0].IsValid() {
  102. scheme := "http"
  103. if config.GetHTTPDConfig().CertificateFile != "" && config.GetHTTPDConfig().CertificateKeyFile != "" {
  104. scheme = "https"
  105. }
  106. info.WriteString(fmt.Sprintf("WebClient URL: %v://<your IP>:%v/ ", scheme, config.GetHTTPDConfig().Bindings[0].Port))
  107. }
  108. return info.String()
  109. }
  110. func (s *Service) getPortableDirToServe() string {
  111. switch s.PortableUser.FsConfig.Provider {
  112. case sdk.S3FilesystemProvider:
  113. return s.PortableUser.FsConfig.S3Config.KeyPrefix
  114. case sdk.GCSFilesystemProvider:
  115. return s.PortableUser.FsConfig.GCSConfig.KeyPrefix
  116. case sdk.AzureBlobFilesystemProvider:
  117. return s.PortableUser.FsConfig.AzBlobConfig.KeyPrefix
  118. case sdk.SFTPFilesystemProvider:
  119. return s.PortableUser.FsConfig.SFTPConfig.Prefix
  120. case sdk.HTTPFilesystemProvider:
  121. return "/"
  122. default:
  123. return s.PortableUser.HomeDir
  124. }
  125. }
  126. // configures the portable user and return the printable password if any
  127. func (s *Service) configurePortableUser() string {
  128. if s.PortableUser.Username == "" {
  129. s.PortableUser.Username = "user"
  130. }
  131. printablePassword := ""
  132. if s.PortableUser.Password != "" {
  133. printablePassword = "[redacted]"
  134. }
  135. if len(s.PortableUser.PublicKeys) == 0 && s.PortableUser.Password == "" {
  136. var b strings.Builder
  137. for i := 0; i < 16; i++ {
  138. b.WriteRune(chars[rand.Intn(len(chars))])
  139. }
  140. s.PortableUser.Password = b.String()
  141. printablePassword = s.PortableUser.Password
  142. }
  143. s.PortableUser.Filters.WebClient = []string{sdk.WebClientSharesDisabled, sdk.WebClientInfoChangeDisabled,
  144. sdk.WebClientPubKeyChangeDisabled, sdk.WebClientPasswordChangeDisabled, sdk.WebClientAPIKeyAuthChangeDisabled,
  145. sdk.WebClientMFADisabled,
  146. }
  147. s.configurePortableSecrets()
  148. return printablePassword
  149. }
  150. func (s *Service) configurePortableSecrets() {
  151. // we created the user before to initialize the KMS so we need to create the secret here
  152. switch s.PortableUser.FsConfig.Provider {
  153. case sdk.S3FilesystemProvider:
  154. payload := s.PortableUser.FsConfig.S3Config.AccessSecret.GetPayload()
  155. s.PortableUser.FsConfig.S3Config.AccessSecret = getSecretFromString(payload)
  156. case sdk.GCSFilesystemProvider:
  157. payload := s.PortableUser.FsConfig.GCSConfig.Credentials.GetPayload()
  158. s.PortableUser.FsConfig.GCSConfig.Credentials = getSecretFromString(payload)
  159. case sdk.AzureBlobFilesystemProvider:
  160. payload := s.PortableUser.FsConfig.AzBlobConfig.AccountKey.GetPayload()
  161. s.PortableUser.FsConfig.AzBlobConfig.AccountKey = getSecretFromString(payload)
  162. payload = s.PortableUser.FsConfig.AzBlobConfig.SASURL.GetPayload()
  163. s.PortableUser.FsConfig.AzBlobConfig.SASURL = getSecretFromString(payload)
  164. case sdk.CryptedFilesystemProvider:
  165. payload := s.PortableUser.FsConfig.CryptConfig.Passphrase.GetPayload()
  166. s.PortableUser.FsConfig.CryptConfig.Passphrase = getSecretFromString(payload)
  167. case sdk.SFTPFilesystemProvider:
  168. payload := s.PortableUser.FsConfig.SFTPConfig.Password.GetPayload()
  169. s.PortableUser.FsConfig.SFTPConfig.Password = getSecretFromString(payload)
  170. payload = s.PortableUser.FsConfig.SFTPConfig.PrivateKey.GetPayload()
  171. s.PortableUser.FsConfig.SFTPConfig.PrivateKey = getSecretFromString(payload)
  172. payload = s.PortableUser.FsConfig.SFTPConfig.KeyPassphrase.GetPayload()
  173. s.PortableUser.FsConfig.SFTPConfig.KeyPassphrase = getSecretFromString(payload)
  174. case sdk.HTTPFilesystemProvider:
  175. payload := s.PortableUser.FsConfig.HTTPConfig.Password.GetPayload()
  176. s.PortableUser.FsConfig.HTTPConfig.Password = getSecretFromString(payload)
  177. payload = s.PortableUser.FsConfig.HTTPConfig.APIKey.GetPayload()
  178. s.PortableUser.FsConfig.HTTPConfig.APIKey = getSecretFromString(payload)
  179. }
  180. }
  181. func getSecretFromString(payload string) *kms.Secret {
  182. if payload != "" {
  183. return kms.NewPlainSecret(payload)
  184. }
  185. return kms.NewEmptySecret()
  186. }
  187. func configurePortableSFTPService(port int, enabledSSHCommands []string) {
  188. sftpdConf := config.GetSFTPDConfig()
  189. if len(sftpdConf.Bindings) == 0 {
  190. sftpdConf.Bindings = append(sftpdConf.Bindings, sftpd.Binding{})
  191. }
  192. if port > 0 {
  193. sftpdConf.Bindings[0].Port = port
  194. } else if port == 0 {
  195. // dynamic ports starts from 49152
  196. sftpdConf.Bindings[0].Port = 49152 + rand.Intn(15000)
  197. } else {
  198. sftpdConf.Bindings[0].Port = 0
  199. }
  200. if slices.Contains(enabledSSHCommands, "*") {
  201. sftpdConf.EnabledSSHCommands = sftpd.GetSupportedSSHCommands()
  202. } else {
  203. sftpdConf.EnabledSSHCommands = enabledSSHCommands
  204. }
  205. config.SetSFTPDConfig(sftpdConf)
  206. }
  207. func configurePortableFTPService(port int, cert, key string) {
  208. ftpConf := config.GetFTPDConfig()
  209. if len(ftpConf.Bindings) == 0 {
  210. ftpConf.Bindings = append(ftpConf.Bindings, ftpd.Binding{})
  211. }
  212. if port > 0 {
  213. ftpConf.Bindings[0].Port = port
  214. } else if port == 0 {
  215. ftpConf.Bindings[0].Port = 49152 + rand.Intn(15000)
  216. } else {
  217. ftpConf.Bindings[0].Port = 0
  218. }
  219. ftpConf.Bindings[0].CertificateFile = cert
  220. ftpConf.Bindings[0].CertificateKeyFile = key
  221. config.SetFTPDConfig(ftpConf)
  222. }
  223. func configurePortableWebDAVService(port int, cert, key string) {
  224. webDavConf := config.GetWebDAVDConfig()
  225. if len(webDavConf.Bindings) == 0 {
  226. webDavConf.Bindings = append(webDavConf.Bindings, webdavd.Binding{})
  227. }
  228. if port > 0 {
  229. webDavConf.Bindings[0].Port = port
  230. } else if port == 0 {
  231. webDavConf.Bindings[0].Port = 49152 + rand.Intn(15000)
  232. } else {
  233. webDavConf.Bindings[0].Port = 0
  234. }
  235. webDavConf.Bindings[0].CertificateFile = cert
  236. webDavConf.Bindings[0].CertificateKeyFile = key
  237. if cert != "" && key != "" {
  238. webDavConf.Bindings[0].EnableHTTPS = true
  239. }
  240. config.SetWebDAVDConfig(webDavConf)
  241. }
  242. func configurePortableHTTPService(port int, cert, key string) {
  243. httpdConf := config.GetHTTPDConfig()
  244. if len(httpdConf.Bindings) == 0 {
  245. httpdConf.Bindings = append(httpdConf.Bindings, httpd.Binding{})
  246. }
  247. if port > 0 {
  248. httpdConf.Bindings[0].Port = port
  249. } else if port == 0 {
  250. httpdConf.Bindings[0].Port = 49152 + rand.Intn(15000)
  251. } else {
  252. httpdConf.Bindings[0].Port = 0
  253. }
  254. httpdConf.Bindings[0].CertificateFile = cert
  255. httpdConf.Bindings[0].CertificateKeyFile = key
  256. if cert != "" && key != "" {
  257. httpdConf.Bindings[0].EnableHTTPS = true
  258. }
  259. httpdConf.Bindings[0].EnableWebAdmin = false
  260. httpdConf.Bindings[0].EnableWebClient = true
  261. httpdConf.Bindings[0].EnableRESTAPI = false
  262. httpdConf.Bindings[0].RenderOpenAPI = false
  263. config.SetHTTPDConfig(httpdConf)
  264. }