service_portable.go 10 KB

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