config.go 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. // Package config manages the configuration
  2. package config
  3. import (
  4. "errors"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "github.com/spf13/viper"
  11. "github.com/drakkan/sftpgo/v2/common"
  12. "github.com/drakkan/sftpgo/v2/dataprovider"
  13. "github.com/drakkan/sftpgo/v2/ftpd"
  14. "github.com/drakkan/sftpgo/v2/httpclient"
  15. "github.com/drakkan/sftpgo/v2/httpd"
  16. "github.com/drakkan/sftpgo/v2/kms"
  17. "github.com/drakkan/sftpgo/v2/logger"
  18. "github.com/drakkan/sftpgo/v2/mfa"
  19. "github.com/drakkan/sftpgo/v2/plugin"
  20. "github.com/drakkan/sftpgo/v2/sftpd"
  21. "github.com/drakkan/sftpgo/v2/smtp"
  22. "github.com/drakkan/sftpgo/v2/telemetry"
  23. "github.com/drakkan/sftpgo/v2/util"
  24. "github.com/drakkan/sftpgo/v2/version"
  25. "github.com/drakkan/sftpgo/v2/webdavd"
  26. )
  27. const (
  28. logSender = "config"
  29. // configName defines the name for config file.
  30. // This name does not include the extension, viper will search for files
  31. // with supported extensions such as "sftpgo.json", "sftpgo.yaml" and so on
  32. configName = "sftpgo"
  33. // ConfigEnvPrefix defines a prefix that environment variables will use
  34. configEnvPrefix = "sftpgo"
  35. )
  36. var (
  37. globalConf globalConfig
  38. defaultSFTPDBanner = fmt.Sprintf("SFTPGo_%v", version.Get().Version)
  39. defaultFTPDBanner = fmt.Sprintf("SFTPGo %v ready", version.Get().Version)
  40. defaultSFTPDBinding = sftpd.Binding{
  41. Address: "",
  42. Port: 2022,
  43. ApplyProxyConfig: true,
  44. }
  45. defaultFTPDBinding = ftpd.Binding{
  46. Address: "",
  47. Port: 0,
  48. ApplyProxyConfig: true,
  49. TLSMode: 0,
  50. ForcePassiveIP: "",
  51. PassiveIPOverrides: nil,
  52. ClientAuthType: 0,
  53. TLSCipherSuites: nil,
  54. PassiveConnectionsSecurity: 0,
  55. ActiveConnectionsSecurity: 0,
  56. Debug: false,
  57. }
  58. defaultWebDAVDBinding = webdavd.Binding{
  59. Address: "",
  60. Port: 0,
  61. EnableHTTPS: false,
  62. ClientAuthType: 0,
  63. TLSCipherSuites: nil,
  64. Prefix: "",
  65. ProxyAllowed: nil,
  66. }
  67. defaultHTTPDBinding = httpd.Binding{
  68. Address: "127.0.0.1",
  69. Port: 8080,
  70. EnableWebAdmin: true,
  71. EnableWebClient: true,
  72. EnableHTTPS: false,
  73. ClientAuthType: 0,
  74. TLSCipherSuites: nil,
  75. ProxyAllowed: nil,
  76. HideLoginURL: 0,
  77. RenderOpenAPI: true,
  78. WebClientIntegrations: nil,
  79. }
  80. defaultRateLimiter = common.RateLimiterConfig{
  81. Average: 0,
  82. Period: 1000,
  83. Burst: 1,
  84. Type: 2,
  85. Protocols: []string{common.ProtocolSSH, common.ProtocolFTP, common.ProtocolWebDAV, common.ProtocolHTTP},
  86. AllowList: []string{},
  87. GenerateDefenderEvents: false,
  88. EntriesSoftLimit: 100,
  89. EntriesHardLimit: 150,
  90. }
  91. defaultTOTP = mfa.TOTPConfig{
  92. Name: "Default",
  93. Issuer: "SFTPGo",
  94. Algo: mfa.TOTPAlgoSHA1,
  95. }
  96. )
  97. type globalConfig struct {
  98. Common common.Configuration `json:"common" mapstructure:"common"`
  99. SFTPD sftpd.Configuration `json:"sftpd" mapstructure:"sftpd"`
  100. FTPD ftpd.Configuration `json:"ftpd" mapstructure:"ftpd"`
  101. WebDAVD webdavd.Configuration `json:"webdavd" mapstructure:"webdavd"`
  102. ProviderConf dataprovider.Config `json:"data_provider" mapstructure:"data_provider"`
  103. HTTPDConfig httpd.Conf `json:"httpd" mapstructure:"httpd"`
  104. HTTPConfig httpclient.Config `json:"http" mapstructure:"http"`
  105. KMSConfig kms.Configuration `json:"kms" mapstructure:"kms"`
  106. MFAConfig mfa.Config `json:"mfa" mapstructure:"mfa"`
  107. TelemetryConfig telemetry.Conf `json:"telemetry" mapstructure:"telemetry"`
  108. PluginsConfig []plugin.Config `json:"plugins" mapstructure:"plugins"`
  109. SMTPConfig smtp.Config `json:"smtp" mapstructure:"smtp"`
  110. }
  111. func init() {
  112. Init()
  113. }
  114. // Init initializes the global configuration.
  115. // It is not supposed to be called outside of this package.
  116. // It is exported to minimize refactoring efforts. Will eventually disappear.
  117. func Init() {
  118. // create a default configuration to use if no config file is provided
  119. globalConf = globalConfig{
  120. Common: common.Configuration{
  121. IdleTimeout: 15,
  122. UploadMode: 0,
  123. Actions: common.ProtocolActions{
  124. ExecuteOn: []string{},
  125. ExecuteSync: []string{},
  126. Hook: "",
  127. },
  128. SetstatMode: 0,
  129. TempPath: "",
  130. ProxyProtocol: 0,
  131. ProxyAllowed: []string{},
  132. PostConnectHook: "",
  133. PostDisconnectHook: "",
  134. DataRetentionHook: "",
  135. MaxTotalConnections: 0,
  136. MaxPerHostConnections: 20,
  137. DefenderConfig: common.DefenderConfig{
  138. Enabled: false,
  139. Driver: common.DefenderDriverMemory,
  140. BanTime: 30,
  141. BanTimeIncrement: 50,
  142. Threshold: 15,
  143. ScoreInvalid: 2,
  144. ScoreValid: 1,
  145. ScoreLimitExceeded: 3,
  146. ObservationTime: 30,
  147. EntriesSoftLimit: 100,
  148. EntriesHardLimit: 150,
  149. SafeListFile: "",
  150. BlockListFile: "",
  151. },
  152. RateLimitersConfig: []common.RateLimiterConfig{defaultRateLimiter},
  153. },
  154. SFTPD: sftpd.Configuration{
  155. Banner: defaultSFTPDBanner,
  156. Bindings: []sftpd.Binding{defaultSFTPDBinding},
  157. MaxAuthTries: 0,
  158. HostKeys: []string{},
  159. KexAlgorithms: []string{},
  160. Ciphers: []string{},
  161. MACs: []string{},
  162. TrustedUserCAKeys: []string{},
  163. LoginBannerFile: "",
  164. EnabledSSHCommands: []string{},
  165. KeyboardInteractiveAuthentication: false,
  166. KeyboardInteractiveHook: "",
  167. PasswordAuthentication: true,
  168. FolderPrefix: "",
  169. },
  170. FTPD: ftpd.Configuration{
  171. Bindings: []ftpd.Binding{defaultFTPDBinding},
  172. Banner: defaultFTPDBanner,
  173. BannerFile: "",
  174. ActiveTransfersPortNon20: true,
  175. PassivePortRange: ftpd.PortRange{
  176. Start: 50000,
  177. End: 50100,
  178. },
  179. DisableActiveMode: false,
  180. EnableSite: false,
  181. HASHSupport: 0,
  182. CombineSupport: 0,
  183. CertificateFile: "",
  184. CertificateKeyFile: "",
  185. CACertificates: []string{},
  186. CARevocationLists: []string{},
  187. },
  188. WebDAVD: webdavd.Configuration{
  189. Bindings: []webdavd.Binding{defaultWebDAVDBinding},
  190. CertificateFile: "",
  191. CertificateKeyFile: "",
  192. CACertificates: []string{},
  193. CARevocationLists: []string{},
  194. Cors: webdavd.CorsConfig{
  195. Enabled: false,
  196. AllowedOrigins: []string{},
  197. AllowedMethods: []string{},
  198. AllowedHeaders: []string{},
  199. ExposedHeaders: []string{},
  200. AllowCredentials: false,
  201. MaxAge: 0,
  202. },
  203. Cache: webdavd.Cache{
  204. Users: webdavd.UsersCacheConfig{
  205. ExpirationTime: 0,
  206. MaxSize: 50,
  207. },
  208. MimeTypes: webdavd.MimeCacheConfig{
  209. Enabled: true,
  210. MaxSize: 1000,
  211. },
  212. },
  213. },
  214. ProviderConf: dataprovider.Config{
  215. Driver: "sqlite",
  216. Name: "sftpgo.db",
  217. Host: "",
  218. Port: 0,
  219. Username: "",
  220. Password: "",
  221. ConnectionString: "",
  222. SQLTablesPrefix: "",
  223. SSLMode: 0,
  224. RootCert: "",
  225. ClientCert: "",
  226. ClientKey: "",
  227. TrackQuota: 1,
  228. PoolSize: 0,
  229. UsersBaseDir: "",
  230. Actions: dataprovider.ObjectsActions{
  231. ExecuteOn: []string{},
  232. ExecuteFor: []string{},
  233. Hook: "",
  234. },
  235. ExternalAuthHook: "",
  236. ExternalAuthScope: 0,
  237. CredentialsPath: "credentials",
  238. PreLoginHook: "",
  239. PostLoginHook: "",
  240. PostLoginScope: 0,
  241. CheckPasswordHook: "",
  242. CheckPasswordScope: 0,
  243. PasswordHashing: dataprovider.PasswordHashing{
  244. Argon2Options: dataprovider.Argon2Options{
  245. Memory: 65536,
  246. Iterations: 1,
  247. Parallelism: 2,
  248. },
  249. BcryptOptions: dataprovider.BcryptOptions{
  250. Cost: 10,
  251. },
  252. Algo: dataprovider.HashingAlgoBcrypt,
  253. },
  254. PasswordValidation: dataprovider.PasswordValidation{
  255. Admins: dataprovider.PasswordValidationRules{
  256. MinEntropy: 0,
  257. },
  258. Users: dataprovider.PasswordValidationRules{
  259. MinEntropy: 0,
  260. },
  261. },
  262. PasswordCaching: true,
  263. UpdateMode: 0,
  264. PreferDatabaseCredentials: false,
  265. SkipNaturalKeysValidation: false,
  266. DelayedQuotaUpdate: 0,
  267. CreateDefaultAdmin: false,
  268. IsShared: 0,
  269. },
  270. HTTPDConfig: httpd.Conf{
  271. Bindings: []httpd.Binding{defaultHTTPDBinding},
  272. TemplatesPath: "templates",
  273. StaticFilesPath: "static",
  274. BackupsPath: "backups",
  275. OpenAPIPath: "openapi",
  276. WebRoot: "",
  277. CertificateFile: "",
  278. CertificateKeyFile: "",
  279. CACertificates: nil,
  280. CARevocationLists: nil,
  281. SigningPassphrase: "",
  282. MaxUploadFileSize: 1048576000,
  283. Cors: httpd.CorsConfig{
  284. Enabled: false,
  285. AllowedOrigins: []string{},
  286. AllowedMethods: []string{},
  287. AllowedHeaders: []string{},
  288. ExposedHeaders: []string{},
  289. AllowCredentials: false,
  290. MaxAge: 0,
  291. },
  292. },
  293. HTTPConfig: httpclient.Config{
  294. Timeout: 20,
  295. RetryWaitMin: 2,
  296. RetryWaitMax: 30,
  297. RetryMax: 3,
  298. CACertificates: nil,
  299. Certificates: nil,
  300. SkipTLSVerify: false,
  301. Headers: nil,
  302. },
  303. KMSConfig: kms.Configuration{
  304. Secrets: kms.Secrets{
  305. URL: "",
  306. MasterKeyString: "",
  307. MasterKeyPath: "",
  308. },
  309. },
  310. MFAConfig: mfa.Config{
  311. TOTP: nil,
  312. },
  313. TelemetryConfig: telemetry.Conf{
  314. BindPort: 10000,
  315. BindAddress: "127.0.0.1",
  316. EnableProfiler: false,
  317. AuthUserFile: "",
  318. CertificateFile: "",
  319. CertificateKeyFile: "",
  320. TLSCipherSuites: nil,
  321. },
  322. PluginsConfig: nil,
  323. SMTPConfig: smtp.Config{
  324. Host: "",
  325. Port: 25,
  326. From: "",
  327. User: "",
  328. Password: "",
  329. AuthType: 0,
  330. Encryption: 0,
  331. Domain: "",
  332. TemplatesPath: "templates",
  333. },
  334. }
  335. viper.SetEnvPrefix(configEnvPrefix)
  336. replacer := strings.NewReplacer(".", "__")
  337. viper.SetEnvKeyReplacer(replacer)
  338. viper.SetConfigName(configName)
  339. setViperDefaults()
  340. viper.AutomaticEnv()
  341. viper.AllowEmptyEnv(true)
  342. }
  343. // GetCommonConfig returns the common protocols configuration
  344. func GetCommonConfig() common.Configuration {
  345. return globalConf.Common
  346. }
  347. // SetCommonConfig sets the common protocols configuration
  348. func SetCommonConfig(config common.Configuration) {
  349. globalConf.Common = config
  350. }
  351. // GetSFTPDConfig returns the configuration for the SFTP server
  352. func GetSFTPDConfig() sftpd.Configuration {
  353. return globalConf.SFTPD
  354. }
  355. // SetSFTPDConfig sets the configuration for the SFTP server
  356. func SetSFTPDConfig(config sftpd.Configuration) {
  357. globalConf.SFTPD = config
  358. }
  359. // GetFTPDConfig returns the configuration for the FTP server
  360. func GetFTPDConfig() ftpd.Configuration {
  361. return globalConf.FTPD
  362. }
  363. // SetFTPDConfig sets the configuration for the FTP server
  364. func SetFTPDConfig(config ftpd.Configuration) {
  365. globalConf.FTPD = config
  366. }
  367. // GetWebDAVDConfig returns the configuration for the WebDAV server
  368. func GetWebDAVDConfig() webdavd.Configuration {
  369. return globalConf.WebDAVD
  370. }
  371. // SetWebDAVDConfig sets the configuration for the WebDAV server
  372. func SetWebDAVDConfig(config webdavd.Configuration) {
  373. globalConf.WebDAVD = config
  374. }
  375. // GetHTTPDConfig returns the configuration for the HTTP server
  376. func GetHTTPDConfig() httpd.Conf {
  377. return globalConf.HTTPDConfig
  378. }
  379. // SetHTTPDConfig sets the configuration for the HTTP server
  380. func SetHTTPDConfig(config httpd.Conf) {
  381. globalConf.HTTPDConfig = config
  382. }
  383. // GetProviderConf returns the configuration for the data provider
  384. func GetProviderConf() dataprovider.Config {
  385. return globalConf.ProviderConf
  386. }
  387. // SetProviderConf sets the configuration for the data provider
  388. func SetProviderConf(config dataprovider.Config) {
  389. globalConf.ProviderConf = config
  390. }
  391. // GetHTTPConfig returns the configuration for HTTP clients
  392. func GetHTTPConfig() httpclient.Config {
  393. return globalConf.HTTPConfig
  394. }
  395. // GetKMSConfig returns the KMS configuration
  396. func GetKMSConfig() kms.Configuration {
  397. return globalConf.KMSConfig
  398. }
  399. // SetKMSConfig sets the kms configuration
  400. func SetKMSConfig(config kms.Configuration) {
  401. globalConf.KMSConfig = config
  402. }
  403. // GetTelemetryConfig returns the telemetry configuration
  404. func GetTelemetryConfig() telemetry.Conf {
  405. return globalConf.TelemetryConfig
  406. }
  407. // SetTelemetryConfig sets the telemetry configuration
  408. func SetTelemetryConfig(config telemetry.Conf) {
  409. globalConf.TelemetryConfig = config
  410. }
  411. // GetPluginsConfig returns the plugins configuration
  412. func GetPluginsConfig() []plugin.Config {
  413. return globalConf.PluginsConfig
  414. }
  415. // SetPluginsConfig sets the plugin configuration
  416. func SetPluginsConfig(config []plugin.Config) {
  417. globalConf.PluginsConfig = config
  418. }
  419. // GetMFAConfig returns multi-factor authentication config
  420. func GetMFAConfig() mfa.Config {
  421. return globalConf.MFAConfig
  422. }
  423. // GetSMTPConfig returns the SMTP configuration
  424. func GetSMTPConfig() smtp.Config {
  425. return globalConf.SMTPConfig
  426. }
  427. // HasServicesToStart returns true if the config defines at least a service to start.
  428. // Supported services are SFTP, FTP and WebDAV
  429. func HasServicesToStart() bool {
  430. if globalConf.SFTPD.ShouldBind() {
  431. return true
  432. }
  433. if globalConf.FTPD.ShouldBind() {
  434. return true
  435. }
  436. if globalConf.WebDAVD.ShouldBind() {
  437. return true
  438. }
  439. return false
  440. }
  441. func getRedactedPassword() string {
  442. return "[redacted]"
  443. }
  444. func getRedactedGlobalConf() globalConfig {
  445. conf := globalConf
  446. conf.Common.Actions.Hook = util.GetRedactedURL(conf.Common.Actions.Hook)
  447. conf.Common.StartupHook = util.GetRedactedURL(conf.Common.StartupHook)
  448. conf.Common.PostConnectHook = util.GetRedactedURL(conf.Common.PostConnectHook)
  449. conf.Common.PostDisconnectHook = util.GetRedactedURL(conf.Common.PostDisconnectHook)
  450. conf.Common.DataRetentionHook = util.GetRedactedURL(conf.Common.DataRetentionHook)
  451. conf.SFTPD.KeyboardInteractiveHook = util.GetRedactedURL(conf.SFTPD.KeyboardInteractiveHook)
  452. conf.HTTPDConfig.SigningPassphrase = getRedactedPassword()
  453. conf.ProviderConf.Password = getRedactedPassword()
  454. conf.ProviderConf.Actions.Hook = util.GetRedactedURL(conf.ProviderConf.Actions.Hook)
  455. conf.ProviderConf.ExternalAuthHook = util.GetRedactedURL(conf.ProviderConf.ExternalAuthHook)
  456. conf.ProviderConf.PreLoginHook = util.GetRedactedURL(conf.ProviderConf.PreLoginHook)
  457. conf.ProviderConf.PostLoginHook = util.GetRedactedURL(conf.ProviderConf.PostLoginHook)
  458. conf.ProviderConf.CheckPasswordHook = util.GetRedactedURL(conf.ProviderConf.CheckPasswordHook)
  459. conf.SMTPConfig.Password = getRedactedPassword()
  460. return conf
  461. }
  462. func setConfigFile(configDir, configFile string) {
  463. if configFile == "" {
  464. return
  465. }
  466. if !filepath.IsAbs(configFile) && util.IsFileInputValid(configFile) {
  467. configFile = filepath.Join(configDir, configFile)
  468. }
  469. viper.SetConfigFile(configFile)
  470. }
  471. // LoadConfig loads the configuration
  472. // configDir will be added to the configuration search paths.
  473. // The search path contains by default the current directory and on linux it contains
  474. // $HOME/.config/sftpgo and /etc/sftpgo too.
  475. // configFile is an absolute or relative path (to the config dir) to the configuration file.
  476. func LoadConfig(configDir, configFile string) error {
  477. var err error
  478. viper.AddConfigPath(configDir)
  479. setViperAdditionalConfigPaths()
  480. viper.AddConfigPath(".")
  481. setConfigFile(configDir, configFile)
  482. if err = viper.ReadInConfig(); err != nil {
  483. // if the user specify a configuration file we get os.ErrNotExist.
  484. // viper.ConfigFileNotFoundError is returned if viper is unable
  485. // to find sftpgo.{json,yaml, etc..} in any of the search paths
  486. if errors.As(err, &viper.ConfigFileNotFoundError{}) {
  487. logger.Debug(logSender, "", "no configuration file found")
  488. } else {
  489. // should we return the error and not start here?
  490. logger.Warn(logSender, "", "error loading configuration file: %v", err)
  491. logger.WarnToConsole("error loading configuration file: %v", err)
  492. }
  493. }
  494. err = viper.Unmarshal(&globalConf)
  495. if err != nil {
  496. logger.Warn(logSender, "", "error parsing configuration file: %v", err)
  497. logger.WarnToConsole("error parsing configuration file: %v", err)
  498. return err
  499. }
  500. // viper only supports slice of strings from env vars, so we use our custom method
  501. loadBindingsFromEnv()
  502. resetInvalidConfigs()
  503. logger.Debug(logSender, "", "config file used: '%#v', config loaded: %+v", viper.ConfigFileUsed(), getRedactedGlobalConf())
  504. return nil
  505. }
  506. func resetInvalidConfigs() {
  507. if strings.TrimSpace(globalConf.SFTPD.Banner) == "" {
  508. globalConf.SFTPD.Banner = defaultSFTPDBanner
  509. }
  510. if strings.TrimSpace(globalConf.FTPD.Banner) == "" {
  511. globalConf.FTPD.Banner = defaultFTPDBanner
  512. }
  513. if globalConf.ProviderConf.UsersBaseDir != "" && !util.IsFileInputValid(globalConf.ProviderConf.UsersBaseDir) {
  514. warn := fmt.Sprintf("invalid users base dir %#v will be ignored", globalConf.ProviderConf.UsersBaseDir)
  515. globalConf.ProviderConf.UsersBaseDir = ""
  516. logger.Warn(logSender, "", "Non-fatal configuration error: %v", warn)
  517. logger.WarnToConsole("Non-fatal configuration error: %v", warn)
  518. }
  519. if globalConf.Common.UploadMode < 0 || globalConf.Common.UploadMode > 2 {
  520. warn := fmt.Sprintf("invalid upload_mode 0, 1 and 2 are supported, configured: %v reset upload_mode to 0",
  521. globalConf.Common.UploadMode)
  522. globalConf.Common.UploadMode = 0
  523. logger.Warn(logSender, "", "Non-fatal configuration error: %v", warn)
  524. logger.WarnToConsole("Non-fatal configuration error: %v", warn)
  525. }
  526. if globalConf.Common.ProxyProtocol < 0 || globalConf.Common.ProxyProtocol > 2 {
  527. warn := fmt.Sprintf("invalid proxy_protocol 0, 1 and 2 are supported, configured: %v reset proxy_protocol to 0",
  528. globalConf.Common.ProxyProtocol)
  529. globalConf.Common.ProxyProtocol = 0
  530. logger.Warn(logSender, "", "Non-fatal configuration error: %v", warn)
  531. logger.WarnToConsole("Non-fatal configuration error: %v", warn)
  532. }
  533. if globalConf.ProviderConf.ExternalAuthScope < 0 || globalConf.ProviderConf.ExternalAuthScope > 15 {
  534. warn := fmt.Sprintf("invalid external_auth_scope: %v reset to 0", globalConf.ProviderConf.ExternalAuthScope)
  535. globalConf.ProviderConf.ExternalAuthScope = 0
  536. logger.Warn(logSender, "", "Non-fatal configuration error: %v", warn)
  537. logger.WarnToConsole("Non-fatal configuration error: %v", warn)
  538. }
  539. if globalConf.ProviderConf.CredentialsPath == "" {
  540. warn := "invalid credentials path, reset to \"credentials\""
  541. globalConf.ProviderConf.CredentialsPath = "credentials"
  542. logger.Warn(logSender, "", "Non-fatal configuration error: %v", warn)
  543. logger.WarnToConsole("Non-fatal configuration error: %v", warn)
  544. }
  545. if globalConf.Common.DefenderConfig.Enabled && globalConf.Common.DefenderConfig.Driver == common.DefenderDriverProvider {
  546. if !globalConf.ProviderConf.IsDefenderSupported() {
  547. warn := fmt.Sprintf("provider based defender is not supported with data provider %#v, "+
  548. "the memory defender implementation will be used. If you want to use the provider defender "+
  549. "implementation please switch to a shared/distributed data provider",
  550. globalConf.ProviderConf.Driver)
  551. globalConf.Common.DefenderConfig.Driver = common.DefenderDriverMemory
  552. logger.Warn(logSender, "", "Non-fatal configuration error: %v", warn)
  553. logger.WarnToConsole("Non-fatal configuration error: %v", warn)
  554. }
  555. }
  556. }
  557. func loadBindingsFromEnv() {
  558. for idx := 0; idx < 10; idx++ {
  559. getTOTPFromEnv(idx)
  560. getRateLimitersFromEnv(idx)
  561. getPluginsFromEnv(idx)
  562. getSFTPDBindindFromEnv(idx)
  563. getFTPDBindingFromEnv(idx)
  564. getWebDAVDBindingFromEnv(idx)
  565. getHTTPDBindingFromEnv(idx)
  566. getHTTPClientCertificatesFromEnv(idx)
  567. getHTTPClientHeadersFromEnv(idx)
  568. }
  569. }
  570. func getTOTPFromEnv(idx int) {
  571. totpConfig := defaultTOTP
  572. if len(globalConf.MFAConfig.TOTP) > idx {
  573. totpConfig = globalConf.MFAConfig.TOTP[idx]
  574. }
  575. isSet := false
  576. name, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_MFA__TOTP__%v__NAME", idx))
  577. if ok {
  578. totpConfig.Name = name
  579. isSet = true
  580. }
  581. issuer, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_MFA__TOTP__%v__ISSUER", idx))
  582. if ok {
  583. totpConfig.Issuer = issuer
  584. isSet = true
  585. }
  586. algo, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_MFA__TOTP__%v__ALGO", idx))
  587. if ok {
  588. totpConfig.Algo = algo
  589. isSet = true
  590. }
  591. if isSet {
  592. if len(globalConf.MFAConfig.TOTP) > idx {
  593. globalConf.MFAConfig.TOTP[idx] = totpConfig
  594. } else {
  595. globalConf.MFAConfig.TOTP = append(globalConf.MFAConfig.TOTP, totpConfig)
  596. }
  597. }
  598. }
  599. func getRateLimitersFromEnv(idx int) {
  600. rtlConfig := defaultRateLimiter
  601. if len(globalConf.Common.RateLimitersConfig) > idx {
  602. rtlConfig = globalConf.Common.RateLimitersConfig[idx]
  603. }
  604. isSet := false
  605. average, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__AVERAGE", idx))
  606. if ok {
  607. rtlConfig.Average = average
  608. isSet = true
  609. }
  610. period, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__PERIOD", idx))
  611. if ok {
  612. rtlConfig.Period = period
  613. isSet = true
  614. }
  615. burst, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__BURST", idx))
  616. if ok {
  617. rtlConfig.Burst = int(burst)
  618. isSet = true
  619. }
  620. rtlType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__TYPE", idx))
  621. if ok {
  622. rtlConfig.Type = int(rtlType)
  623. isSet = true
  624. }
  625. protocols, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__PROTOCOLS", idx))
  626. if ok {
  627. rtlConfig.Protocols = protocols
  628. isSet = true
  629. }
  630. allowList, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__ALLOW_LIST", idx))
  631. if ok {
  632. rtlConfig.AllowList = allowList
  633. isSet = true
  634. }
  635. generateEvents, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__GENERATE_DEFENDER_EVENTS", idx))
  636. if ok {
  637. rtlConfig.GenerateDefenderEvents = generateEvents
  638. isSet = true
  639. }
  640. softLimit, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__ENTRIES_SOFT_LIMIT", idx))
  641. if ok {
  642. rtlConfig.EntriesSoftLimit = int(softLimit)
  643. isSet = true
  644. }
  645. hardLimit, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__ENTRIES_HARD_LIMIT", idx))
  646. if ok {
  647. rtlConfig.EntriesHardLimit = int(hardLimit)
  648. isSet = true
  649. }
  650. if isSet {
  651. if len(globalConf.Common.RateLimitersConfig) > idx {
  652. globalConf.Common.RateLimitersConfig[idx] = rtlConfig
  653. } else {
  654. globalConf.Common.RateLimitersConfig = append(globalConf.Common.RateLimitersConfig, rtlConfig)
  655. }
  656. }
  657. }
  658. func getKMSPluginFromEnv(idx int, pluginConfig *plugin.Config) bool {
  659. isSet := false
  660. kmsScheme, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__KMS_OPTIONS__SCHEME", idx))
  661. if ok {
  662. pluginConfig.KMSOptions.Scheme = kmsScheme
  663. isSet = true
  664. }
  665. kmsEncStatus, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__KMS_OPTIONS__ENCRYPTED_STATUS", idx))
  666. if ok {
  667. pluginConfig.KMSOptions.EncryptedStatus = kmsEncStatus
  668. isSet = true
  669. }
  670. return isSet
  671. }
  672. func getAuthPluginFromEnv(idx int, pluginConfig *plugin.Config) bool {
  673. isSet := false
  674. authScope, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__AUTH_OPTIONS__SCOPE", idx))
  675. if ok {
  676. pluginConfig.AuthOptions.Scope = int(authScope)
  677. isSet = true
  678. }
  679. return isSet
  680. }
  681. func getNotifierPluginFromEnv(idx int, pluginConfig *plugin.Config) bool {
  682. isSet := false
  683. notifierFsEvents, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__FS_EVENTS", idx))
  684. if ok {
  685. pluginConfig.NotifierOptions.FsEvents = notifierFsEvents
  686. isSet = true
  687. }
  688. notifierProviderEvents, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__PROVIDER_EVENTS", idx))
  689. if ok {
  690. pluginConfig.NotifierOptions.ProviderEvents = notifierProviderEvents
  691. isSet = true
  692. }
  693. notifierProviderObjects, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__PROVIDER_OBJECTS", idx))
  694. if ok {
  695. pluginConfig.NotifierOptions.ProviderObjects = notifierProviderObjects
  696. isSet = true
  697. }
  698. notifierRetryMaxTime, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__RETRY_MAX_TIME", idx))
  699. if ok {
  700. pluginConfig.NotifierOptions.RetryMaxTime = int(notifierRetryMaxTime)
  701. isSet = true
  702. }
  703. notifierRetryQueueMaxSize, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__RETRY_QUEUE_MAX_SIZE", idx))
  704. if ok {
  705. pluginConfig.NotifierOptions.RetryQueueMaxSize = int(notifierRetryQueueMaxSize)
  706. isSet = true
  707. }
  708. return isSet
  709. }
  710. func getPluginsFromEnv(idx int) {
  711. pluginConfig := plugin.Config{}
  712. if len(globalConf.PluginsConfig) > idx {
  713. pluginConfig = globalConf.PluginsConfig[idx]
  714. }
  715. isSet := false
  716. pluginType, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__TYPE", idx))
  717. if ok {
  718. pluginConfig.Type = pluginType
  719. isSet = true
  720. }
  721. if getNotifierPluginFromEnv(idx, &pluginConfig) {
  722. isSet = true
  723. }
  724. if getKMSPluginFromEnv(idx, &pluginConfig) {
  725. isSet = true
  726. }
  727. if getAuthPluginFromEnv(idx, &pluginConfig) {
  728. isSet = true
  729. }
  730. cmd, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__CMD", idx))
  731. if ok {
  732. pluginConfig.Cmd = cmd
  733. isSet = true
  734. }
  735. cmdArgs, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__ARGS", idx))
  736. if ok {
  737. pluginConfig.Args = cmdArgs
  738. isSet = true
  739. }
  740. pluginHash, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__SHA256SUM", idx))
  741. if ok {
  742. pluginConfig.SHA256Sum = pluginHash
  743. isSet = true
  744. }
  745. autoMTLS, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__AUTO_MTLS", idx))
  746. if ok {
  747. pluginConfig.AutoMTLS = autoMTLS
  748. isSet = true
  749. }
  750. if isSet {
  751. if len(globalConf.PluginsConfig) > idx {
  752. globalConf.PluginsConfig[idx] = pluginConfig
  753. } else {
  754. globalConf.PluginsConfig = append(globalConf.PluginsConfig, pluginConfig)
  755. }
  756. }
  757. }
  758. func getSFTPDBindindFromEnv(idx int) {
  759. binding := sftpd.Binding{
  760. ApplyProxyConfig: true,
  761. }
  762. if len(globalConf.SFTPD.Bindings) > idx {
  763. binding = globalConf.SFTPD.Bindings[idx]
  764. }
  765. isSet := false
  766. port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_SFTPD__BINDINGS__%v__PORT", idx))
  767. if ok {
  768. binding.Port = int(port)
  769. isSet = true
  770. }
  771. address, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_SFTPD__BINDINGS__%v__ADDRESS", idx))
  772. if ok {
  773. binding.Address = address
  774. isSet = true
  775. }
  776. applyProxyConfig, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_SFTPD__BINDINGS__%v__APPLY_PROXY_CONFIG", idx))
  777. if ok {
  778. binding.ApplyProxyConfig = applyProxyConfig
  779. isSet = true
  780. }
  781. if isSet {
  782. if len(globalConf.SFTPD.Bindings) > idx {
  783. globalConf.SFTPD.Bindings[idx] = binding
  784. } else {
  785. globalConf.SFTPD.Bindings = append(globalConf.SFTPD.Bindings, binding)
  786. }
  787. }
  788. }
  789. func getFTPDPassiveIPOverridesFromEnv(idx int) []ftpd.PassiveIPOverride {
  790. var overrides []ftpd.PassiveIPOverride
  791. for subIdx := 0; subIdx < 10; subIdx++ {
  792. var override ftpd.PassiveIPOverride
  793. ip, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__PASSIVE_IP_OVERRIDES__%v__IP", idx, subIdx))
  794. if ok {
  795. override.IP = ip
  796. }
  797. networks, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__PASSIVE_IP_OVERRIDES__%v__NETWORKS",
  798. idx, subIdx))
  799. if ok {
  800. override.Networks = networks
  801. }
  802. if len(override.Networks) > 0 {
  803. overrides = append(overrides, override)
  804. }
  805. }
  806. return overrides
  807. }
  808. func getFTPDBindingFromEnv(idx int) {
  809. binding := ftpd.Binding{
  810. ApplyProxyConfig: true,
  811. }
  812. if len(globalConf.FTPD.Bindings) > idx {
  813. binding = globalConf.FTPD.Bindings[idx]
  814. }
  815. isSet := false
  816. port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__PORT", idx))
  817. if ok {
  818. binding.Port = int(port)
  819. isSet = true
  820. }
  821. address, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__ADDRESS", idx))
  822. if ok {
  823. binding.Address = address
  824. isSet = true
  825. }
  826. applyProxyConfig, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__APPLY_PROXY_CONFIG", idx))
  827. if ok {
  828. binding.ApplyProxyConfig = applyProxyConfig
  829. isSet = true
  830. }
  831. tlsMode, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__TLS_MODE", idx))
  832. if ok {
  833. binding.TLSMode = int(tlsMode)
  834. isSet = true
  835. }
  836. passiveIP, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__FORCE_PASSIVE_IP", idx))
  837. if ok {
  838. binding.ForcePassiveIP = passiveIP
  839. isSet = true
  840. }
  841. passiveIPOverrides := getFTPDPassiveIPOverridesFromEnv(idx)
  842. if len(passiveIPOverrides) > 0 {
  843. binding.PassiveIPOverrides = passiveIPOverrides
  844. isSet = true
  845. }
  846. clientAuthType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__CLIENT_AUTH_TYPE", idx))
  847. if ok {
  848. binding.ClientAuthType = int(clientAuthType)
  849. isSet = true
  850. }
  851. tlsCiphers, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__TLS_CIPHER_SUITES", idx))
  852. if ok {
  853. binding.TLSCipherSuites = tlsCiphers
  854. isSet = true
  855. }
  856. pasvSecurity, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__PASSIVE_CONNECTIONS_SECURITY", idx))
  857. if ok {
  858. binding.PassiveConnectionsSecurity = int(pasvSecurity)
  859. isSet = true
  860. }
  861. activeSecurity, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__ACTIVE_CONNECTIONS_SECURITY", idx))
  862. if ok {
  863. binding.ActiveConnectionsSecurity = int(activeSecurity)
  864. isSet = true
  865. }
  866. debug, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__DEBUG", idx))
  867. if ok {
  868. binding.Debug = debug
  869. isSet = true
  870. }
  871. if isSet {
  872. if len(globalConf.FTPD.Bindings) > idx {
  873. globalConf.FTPD.Bindings[idx] = binding
  874. } else {
  875. globalConf.FTPD.Bindings = append(globalConf.FTPD.Bindings, binding)
  876. }
  877. }
  878. }
  879. func getWebDAVDBindingFromEnv(idx int) {
  880. binding := webdavd.Binding{}
  881. if len(globalConf.WebDAVD.Bindings) > idx {
  882. binding = globalConf.WebDAVD.Bindings[idx]
  883. }
  884. isSet := false
  885. port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__PORT", idx))
  886. if ok {
  887. binding.Port = int(port)
  888. isSet = true
  889. }
  890. address, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__ADDRESS", idx))
  891. if ok {
  892. binding.Address = address
  893. isSet = true
  894. }
  895. enableHTTPS, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__ENABLE_HTTPS", idx))
  896. if ok {
  897. binding.EnableHTTPS = enableHTTPS
  898. isSet = true
  899. }
  900. clientAuthType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__CLIENT_AUTH_TYPE", idx))
  901. if ok {
  902. binding.ClientAuthType = int(clientAuthType)
  903. isSet = true
  904. }
  905. tlsCiphers, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__TLS_CIPHER_SUITES", idx))
  906. if ok {
  907. binding.TLSCipherSuites = tlsCiphers
  908. isSet = true
  909. }
  910. proxyAllowed, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__PROXY_ALLOWED", idx))
  911. if ok {
  912. binding.ProxyAllowed = proxyAllowed
  913. isSet = true
  914. }
  915. prefix, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__PREFIX", idx))
  916. if ok {
  917. binding.Prefix = prefix
  918. isSet = true
  919. }
  920. if isSet {
  921. if len(globalConf.WebDAVD.Bindings) > idx {
  922. globalConf.WebDAVD.Bindings[idx] = binding
  923. } else {
  924. globalConf.WebDAVD.Bindings = append(globalConf.WebDAVD.Bindings, binding)
  925. }
  926. }
  927. }
  928. func getHTTPDWebClientIntegrationsFromEnv(idx int) []httpd.WebClientIntegration {
  929. var integrations []httpd.WebClientIntegration
  930. for subIdx := 0; subIdx < 10; subIdx++ {
  931. var integration httpd.WebClientIntegration
  932. url, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__WEB_CLIENT_INTEGRATIONS__%v__URL", idx, subIdx))
  933. if ok {
  934. integration.URL = url
  935. }
  936. extensions, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__WEB_CLIENT_INTEGRATIONS__%v__FILE_EXTENSIONS",
  937. idx, subIdx))
  938. if ok {
  939. integration.FileExtensions = extensions
  940. }
  941. if url != "" && len(extensions) > 0 {
  942. integrations = append(integrations, integration)
  943. }
  944. }
  945. return integrations
  946. }
  947. func getHTTPDBindingFromEnv(idx int) {
  948. binding := httpd.Binding{
  949. EnableWebAdmin: true,
  950. EnableWebClient: true,
  951. RenderOpenAPI: true,
  952. }
  953. if len(globalConf.HTTPDConfig.Bindings) > idx {
  954. binding = globalConf.HTTPDConfig.Bindings[idx]
  955. }
  956. isSet := false
  957. port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__PORT", idx))
  958. if ok {
  959. binding.Port = int(port)
  960. isSet = true
  961. }
  962. address, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ADDRESS", idx))
  963. if ok {
  964. binding.Address = address
  965. isSet = true
  966. }
  967. enableWebAdmin, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ENABLE_WEB_ADMIN", idx))
  968. if ok {
  969. binding.EnableWebAdmin = enableWebAdmin
  970. isSet = true
  971. }
  972. enableWebClient, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ENABLE_WEB_CLIENT", idx))
  973. if ok {
  974. binding.EnableWebClient = enableWebClient
  975. isSet = true
  976. }
  977. renderOpenAPI, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__RENDER_OPENAPI", idx))
  978. if ok {
  979. binding.RenderOpenAPI = renderOpenAPI
  980. isSet = true
  981. }
  982. webClientIntegrations := getHTTPDWebClientIntegrationsFromEnv(idx)
  983. if len(webClientIntegrations) > 0 {
  984. binding.WebClientIntegrations = webClientIntegrations
  985. isSet = true
  986. }
  987. enableHTTPS, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ENABLE_HTTPS", idx))
  988. if ok {
  989. binding.EnableHTTPS = enableHTTPS
  990. isSet = true
  991. }
  992. clientAuthType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__CLIENT_AUTH_TYPE", idx))
  993. if ok {
  994. binding.ClientAuthType = int(clientAuthType)
  995. isSet = true
  996. }
  997. tlsCiphers, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__TLS_CIPHER_SUITES", idx))
  998. if ok {
  999. binding.TLSCipherSuites = tlsCiphers
  1000. isSet = true
  1001. }
  1002. proxyAllowed, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__PROXY_ALLOWED", idx))
  1003. if ok {
  1004. binding.ProxyAllowed = proxyAllowed
  1005. isSet = true
  1006. }
  1007. hideLoginURL, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__HIDE_LOGIN_URL", idx))
  1008. if ok {
  1009. binding.HideLoginURL = int(hideLoginURL)
  1010. isSet = true
  1011. }
  1012. if isSet {
  1013. if len(globalConf.HTTPDConfig.Bindings) > idx {
  1014. globalConf.HTTPDConfig.Bindings[idx] = binding
  1015. } else {
  1016. globalConf.HTTPDConfig.Bindings = append(globalConf.HTTPDConfig.Bindings, binding)
  1017. }
  1018. }
  1019. }
  1020. func getHTTPClientCertificatesFromEnv(idx int) {
  1021. tlsCert := httpclient.TLSKeyPair{}
  1022. cert, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__CERTIFICATES__%v__CERT", idx))
  1023. if ok {
  1024. tlsCert.Cert = cert
  1025. }
  1026. key, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__CERTIFICATES__%v__KEY", idx))
  1027. if ok {
  1028. tlsCert.Key = key
  1029. }
  1030. if tlsCert.Cert != "" && tlsCert.Key != "" {
  1031. if len(globalConf.HTTPConfig.Certificates) > idx {
  1032. globalConf.HTTPConfig.Certificates[idx] = tlsCert
  1033. } else {
  1034. globalConf.HTTPConfig.Certificates = append(globalConf.HTTPConfig.Certificates, tlsCert)
  1035. }
  1036. }
  1037. }
  1038. func getHTTPClientHeadersFromEnv(idx int) {
  1039. header := httpclient.Header{}
  1040. key, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__HEADERS__%v__KEY", idx))
  1041. if ok {
  1042. header.Key = key
  1043. }
  1044. value, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__HEADERS__%v__VALUE", idx))
  1045. if ok {
  1046. header.Value = value
  1047. }
  1048. url, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__HEADERS__%v__URL", idx))
  1049. if ok {
  1050. header.URL = url
  1051. }
  1052. if header.Key != "" && header.Value != "" {
  1053. if len(globalConf.HTTPConfig.Headers) > idx {
  1054. globalConf.HTTPConfig.Headers[idx] = header
  1055. } else {
  1056. globalConf.HTTPConfig.Headers = append(globalConf.HTTPConfig.Headers, header)
  1057. }
  1058. }
  1059. }
  1060. func setViperDefaults() {
  1061. viper.SetDefault("common.idle_timeout", globalConf.Common.IdleTimeout)
  1062. viper.SetDefault("common.upload_mode", globalConf.Common.UploadMode)
  1063. viper.SetDefault("common.actions.execute_on", globalConf.Common.Actions.ExecuteOn)
  1064. viper.SetDefault("common.actions.execute_sync", globalConf.Common.Actions.ExecuteSync)
  1065. viper.SetDefault("common.actions.hook", globalConf.Common.Actions.Hook)
  1066. viper.SetDefault("common.setstat_mode", globalConf.Common.SetstatMode)
  1067. viper.SetDefault("common.temp_path", globalConf.Common.TempPath)
  1068. viper.SetDefault("common.proxy_protocol", globalConf.Common.ProxyProtocol)
  1069. viper.SetDefault("common.proxy_allowed", globalConf.Common.ProxyAllowed)
  1070. viper.SetDefault("common.post_connect_hook", globalConf.Common.PostConnectHook)
  1071. viper.SetDefault("common.post_disconnect_hook", globalConf.Common.PostDisconnectHook)
  1072. viper.SetDefault("common.data_retention_hook", globalConf.Common.DataRetentionHook)
  1073. viper.SetDefault("common.max_total_connections", globalConf.Common.MaxTotalConnections)
  1074. viper.SetDefault("common.max_per_host_connections", globalConf.Common.MaxPerHostConnections)
  1075. viper.SetDefault("common.defender.enabled", globalConf.Common.DefenderConfig.Enabled)
  1076. viper.SetDefault("common.defender.driver", globalConf.Common.DefenderConfig.Driver)
  1077. viper.SetDefault("common.defender.ban_time", globalConf.Common.DefenderConfig.BanTime)
  1078. viper.SetDefault("common.defender.ban_time_increment", globalConf.Common.DefenderConfig.BanTimeIncrement)
  1079. viper.SetDefault("common.defender.threshold", globalConf.Common.DefenderConfig.Threshold)
  1080. viper.SetDefault("common.defender.score_invalid", globalConf.Common.DefenderConfig.ScoreInvalid)
  1081. viper.SetDefault("common.defender.score_valid", globalConf.Common.DefenderConfig.ScoreValid)
  1082. viper.SetDefault("common.defender.score_limit_exceeded", globalConf.Common.DefenderConfig.ScoreLimitExceeded)
  1083. viper.SetDefault("common.defender.observation_time", globalConf.Common.DefenderConfig.ObservationTime)
  1084. viper.SetDefault("common.defender.entries_soft_limit", globalConf.Common.DefenderConfig.EntriesSoftLimit)
  1085. viper.SetDefault("common.defender.entries_hard_limit", globalConf.Common.DefenderConfig.EntriesHardLimit)
  1086. viper.SetDefault("common.defender.safelist_file", globalConf.Common.DefenderConfig.SafeListFile)
  1087. viper.SetDefault("common.defender.blocklist_file", globalConf.Common.DefenderConfig.BlockListFile)
  1088. viper.SetDefault("sftpd.max_auth_tries", globalConf.SFTPD.MaxAuthTries)
  1089. viper.SetDefault("sftpd.banner", globalConf.SFTPD.Banner)
  1090. viper.SetDefault("sftpd.host_keys", globalConf.SFTPD.HostKeys)
  1091. viper.SetDefault("sftpd.kex_algorithms", globalConf.SFTPD.KexAlgorithms)
  1092. viper.SetDefault("sftpd.ciphers", globalConf.SFTPD.Ciphers)
  1093. viper.SetDefault("sftpd.macs", globalConf.SFTPD.MACs)
  1094. viper.SetDefault("sftpd.trusted_user_ca_keys", globalConf.SFTPD.TrustedUserCAKeys)
  1095. viper.SetDefault("sftpd.login_banner_file", globalConf.SFTPD.LoginBannerFile)
  1096. viper.SetDefault("sftpd.enabled_ssh_commands", sftpd.GetDefaultSSHCommands())
  1097. viper.SetDefault("sftpd.keyboard_interactive_authentication", globalConf.SFTPD.KeyboardInteractiveAuthentication)
  1098. viper.SetDefault("sftpd.keyboard_interactive_auth_hook", globalConf.SFTPD.KeyboardInteractiveHook)
  1099. viper.SetDefault("sftpd.password_authentication", globalConf.SFTPD.PasswordAuthentication)
  1100. viper.SetDefault("sftpd.folder_prefix", globalConf.SFTPD.FolderPrefix)
  1101. viper.SetDefault("ftpd.banner", globalConf.FTPD.Banner)
  1102. viper.SetDefault("ftpd.banner_file", globalConf.FTPD.BannerFile)
  1103. viper.SetDefault("ftpd.active_transfers_port_non_20", globalConf.FTPD.ActiveTransfersPortNon20)
  1104. viper.SetDefault("ftpd.passive_port_range.start", globalConf.FTPD.PassivePortRange.Start)
  1105. viper.SetDefault("ftpd.passive_port_range.end", globalConf.FTPD.PassivePortRange.End)
  1106. viper.SetDefault("ftpd.disable_active_mode", globalConf.FTPD.DisableActiveMode)
  1107. viper.SetDefault("ftpd.enable_site", globalConf.FTPD.EnableSite)
  1108. viper.SetDefault("ftpd.hash_support", globalConf.FTPD.HASHSupport)
  1109. viper.SetDefault("ftpd.combine_support", globalConf.FTPD.CombineSupport)
  1110. viper.SetDefault("ftpd.certificate_file", globalConf.FTPD.CertificateFile)
  1111. viper.SetDefault("ftpd.certificate_key_file", globalConf.FTPD.CertificateKeyFile)
  1112. viper.SetDefault("ftpd.ca_certificates", globalConf.FTPD.CACertificates)
  1113. viper.SetDefault("ftpd.ca_revocation_lists", globalConf.FTPD.CARevocationLists)
  1114. viper.SetDefault("webdavd.certificate_file", globalConf.WebDAVD.CertificateFile)
  1115. viper.SetDefault("webdavd.certificate_key_file", globalConf.WebDAVD.CertificateKeyFile)
  1116. viper.SetDefault("webdavd.ca_certificates", globalConf.WebDAVD.CACertificates)
  1117. viper.SetDefault("webdavd.ca_revocation_lists", globalConf.WebDAVD.CARevocationLists)
  1118. viper.SetDefault("webdavd.cors.enabled", globalConf.WebDAVD.Cors.Enabled)
  1119. viper.SetDefault("webdavd.cors.allowed_origins", globalConf.WebDAVD.Cors.AllowedOrigins)
  1120. viper.SetDefault("webdavd.cors.allowed_methods", globalConf.WebDAVD.Cors.AllowedMethods)
  1121. viper.SetDefault("webdavd.cors.allowed_headers", globalConf.WebDAVD.Cors.AllowedHeaders)
  1122. viper.SetDefault("webdavd.cors.exposed_headers", globalConf.WebDAVD.Cors.ExposedHeaders)
  1123. viper.SetDefault("webdavd.cors.allow_credentials", globalConf.WebDAVD.Cors.AllowCredentials)
  1124. viper.SetDefault("webdavd.cors.max_age", globalConf.WebDAVD.Cors.MaxAge)
  1125. viper.SetDefault("webdavd.cache.users.expiration_time", globalConf.WebDAVD.Cache.Users.ExpirationTime)
  1126. viper.SetDefault("webdavd.cache.users.max_size", globalConf.WebDAVD.Cache.Users.MaxSize)
  1127. viper.SetDefault("webdavd.cache.mime_types.enabled", globalConf.WebDAVD.Cache.MimeTypes.Enabled)
  1128. viper.SetDefault("webdavd.cache.mime_types.max_size", globalConf.WebDAVD.Cache.MimeTypes.MaxSize)
  1129. viper.SetDefault("data_provider.driver", globalConf.ProviderConf.Driver)
  1130. viper.SetDefault("data_provider.name", globalConf.ProviderConf.Name)
  1131. viper.SetDefault("data_provider.host", globalConf.ProviderConf.Host)
  1132. viper.SetDefault("data_provider.port", globalConf.ProviderConf.Port)
  1133. viper.SetDefault("data_provider.username", globalConf.ProviderConf.Username)
  1134. viper.SetDefault("data_provider.password", globalConf.ProviderConf.Password)
  1135. viper.SetDefault("data_provider.sslmode", globalConf.ProviderConf.SSLMode)
  1136. viper.SetDefault("data_provider.root_cert", globalConf.ProviderConf.RootCert)
  1137. viper.SetDefault("data_provider.client_cert", globalConf.ProviderConf.ClientCert)
  1138. viper.SetDefault("data_provider.client_key", globalConf.ProviderConf.ClientKey)
  1139. viper.SetDefault("data_provider.connection_string", globalConf.ProviderConf.ConnectionString)
  1140. viper.SetDefault("data_provider.sql_tables_prefix", globalConf.ProviderConf.SQLTablesPrefix)
  1141. viper.SetDefault("data_provider.track_quota", globalConf.ProviderConf.TrackQuota)
  1142. viper.SetDefault("data_provider.pool_size", globalConf.ProviderConf.PoolSize)
  1143. viper.SetDefault("data_provider.users_base_dir", globalConf.ProviderConf.UsersBaseDir)
  1144. viper.SetDefault("data_provider.actions.execute_on", globalConf.ProviderConf.Actions.ExecuteOn)
  1145. viper.SetDefault("data_provider.actions.execute_for", globalConf.ProviderConf.Actions.ExecuteFor)
  1146. viper.SetDefault("data_provider.actions.hook", globalConf.ProviderConf.Actions.Hook)
  1147. viper.SetDefault("data_provider.external_auth_hook", globalConf.ProviderConf.ExternalAuthHook)
  1148. viper.SetDefault("data_provider.external_auth_scope", globalConf.ProviderConf.ExternalAuthScope)
  1149. viper.SetDefault("data_provider.credentials_path", globalConf.ProviderConf.CredentialsPath)
  1150. viper.SetDefault("data_provider.prefer_database_credentials", globalConf.ProviderConf.PreferDatabaseCredentials)
  1151. viper.SetDefault("data_provider.pre_login_hook", globalConf.ProviderConf.PreLoginHook)
  1152. viper.SetDefault("data_provider.post_login_hook", globalConf.ProviderConf.PostLoginHook)
  1153. viper.SetDefault("data_provider.post_login_scope", globalConf.ProviderConf.PostLoginScope)
  1154. viper.SetDefault("data_provider.check_password_hook", globalConf.ProviderConf.CheckPasswordHook)
  1155. viper.SetDefault("data_provider.check_password_scope", globalConf.ProviderConf.CheckPasswordScope)
  1156. viper.SetDefault("data_provider.password_hashing.bcrypt_options.cost", globalConf.ProviderConf.PasswordHashing.BcryptOptions.Cost)
  1157. viper.SetDefault("data_provider.password_hashing.argon2_options.memory", globalConf.ProviderConf.PasswordHashing.Argon2Options.Memory)
  1158. viper.SetDefault("data_provider.password_hashing.argon2_options.iterations", globalConf.ProviderConf.PasswordHashing.Argon2Options.Iterations)
  1159. viper.SetDefault("data_provider.password_hashing.argon2_options.parallelism", globalConf.ProviderConf.PasswordHashing.Argon2Options.Parallelism)
  1160. viper.SetDefault("data_provider.password_hashing.algo", globalConf.ProviderConf.PasswordHashing.Algo)
  1161. viper.SetDefault("data_provider.password_validation.admins.min_entropy", globalConf.ProviderConf.PasswordValidation.Admins.MinEntropy)
  1162. viper.SetDefault("data_provider.password_validation.users.min_entropy", globalConf.ProviderConf.PasswordValidation.Users.MinEntropy)
  1163. viper.SetDefault("data_provider.password_caching", globalConf.ProviderConf.PasswordCaching)
  1164. viper.SetDefault("data_provider.update_mode", globalConf.ProviderConf.UpdateMode)
  1165. viper.SetDefault("data_provider.skip_natural_keys_validation", globalConf.ProviderConf.SkipNaturalKeysValidation)
  1166. viper.SetDefault("data_provider.delayed_quota_update", globalConf.ProviderConf.DelayedQuotaUpdate)
  1167. viper.SetDefault("data_provider.create_default_admin", globalConf.ProviderConf.CreateDefaultAdmin)
  1168. viper.SetDefault("data_provider.is_shared", globalConf.ProviderConf.IsShared)
  1169. viper.SetDefault("httpd.templates_path", globalConf.HTTPDConfig.TemplatesPath)
  1170. viper.SetDefault("httpd.static_files_path", globalConf.HTTPDConfig.StaticFilesPath)
  1171. viper.SetDefault("httpd.backups_path", globalConf.HTTPDConfig.BackupsPath)
  1172. viper.SetDefault("httpd.openapi_path", globalConf.HTTPDConfig.OpenAPIPath)
  1173. viper.SetDefault("httpd.web_root", globalConf.HTTPDConfig.WebRoot)
  1174. viper.SetDefault("httpd.certificate_file", globalConf.HTTPDConfig.CertificateFile)
  1175. viper.SetDefault("httpd.certificate_key_file", globalConf.HTTPDConfig.CertificateKeyFile)
  1176. viper.SetDefault("httpd.ca_certificates", globalConf.HTTPDConfig.CACertificates)
  1177. viper.SetDefault("httpd.ca_revocation_lists", globalConf.HTTPDConfig.CARevocationLists)
  1178. viper.SetDefault("httpd.signing_passphrase", globalConf.HTTPDConfig.SigningPassphrase)
  1179. viper.SetDefault("httpd.max_upload_file_size", globalConf.HTTPDConfig.MaxUploadFileSize)
  1180. viper.SetDefault("httpd.cors.enabled", globalConf.HTTPDConfig.Cors.Enabled)
  1181. viper.SetDefault("httpd.cors.allowed_origins", globalConf.HTTPDConfig.Cors.AllowedOrigins)
  1182. viper.SetDefault("httpd.cors.allowed_methods", globalConf.HTTPDConfig.Cors.AllowedMethods)
  1183. viper.SetDefault("httpd.cors.allowed_headers", globalConf.HTTPDConfig.Cors.AllowedHeaders)
  1184. viper.SetDefault("httpd.cors.exposed_headers", globalConf.HTTPDConfig.Cors.ExposedHeaders)
  1185. viper.SetDefault("httpd.cors.allow_credentials", globalConf.HTTPDConfig.Cors.AllowCredentials)
  1186. viper.SetDefault("httpd.cors.max_age", globalConf.HTTPDConfig.Cors.MaxAge)
  1187. viper.SetDefault("http.timeout", globalConf.HTTPConfig.Timeout)
  1188. viper.SetDefault("http.retry_wait_min", globalConf.HTTPConfig.RetryWaitMin)
  1189. viper.SetDefault("http.retry_wait_max", globalConf.HTTPConfig.RetryWaitMax)
  1190. viper.SetDefault("http.retry_max", globalConf.HTTPConfig.RetryMax)
  1191. viper.SetDefault("http.ca_certificates", globalConf.HTTPConfig.CACertificates)
  1192. viper.SetDefault("http.skip_tls_verify", globalConf.HTTPConfig.SkipTLSVerify)
  1193. viper.SetDefault("kms.secrets.url", globalConf.KMSConfig.Secrets.URL)
  1194. viper.SetDefault("kms.secrets.master_key", globalConf.KMSConfig.Secrets.MasterKeyString)
  1195. viper.SetDefault("kms.secrets.master_key_path", globalConf.KMSConfig.Secrets.MasterKeyPath)
  1196. viper.SetDefault("telemetry.bind_port", globalConf.TelemetryConfig.BindPort)
  1197. viper.SetDefault("telemetry.bind_address", globalConf.TelemetryConfig.BindAddress)
  1198. viper.SetDefault("telemetry.enable_profiler", globalConf.TelemetryConfig.EnableProfiler)
  1199. viper.SetDefault("telemetry.auth_user_file", globalConf.TelemetryConfig.AuthUserFile)
  1200. viper.SetDefault("telemetry.certificate_file", globalConf.TelemetryConfig.CertificateFile)
  1201. viper.SetDefault("telemetry.certificate_key_file", globalConf.TelemetryConfig.CertificateKeyFile)
  1202. viper.SetDefault("telemetry.tls_cipher_suites", globalConf.TelemetryConfig.TLSCipherSuites)
  1203. viper.SetDefault("smtp.host", globalConf.SMTPConfig.Host)
  1204. viper.SetDefault("smtp.port", globalConf.SMTPConfig.Port)
  1205. viper.SetDefault("smtp.from", globalConf.SMTPConfig.From)
  1206. viper.SetDefault("smtp.user", globalConf.SMTPConfig.User)
  1207. viper.SetDefault("smtp.password", globalConf.SMTPConfig.Password)
  1208. viper.SetDefault("smtp.auth_type", globalConf.SMTPConfig.AuthType)
  1209. viper.SetDefault("smtp.encryption", globalConf.SMTPConfig.Encryption)
  1210. viper.SetDefault("smtp.domain", globalConf.SMTPConfig.Domain)
  1211. viper.SetDefault("smtp.templates_path", globalConf.SMTPConfig.TemplatesPath)
  1212. }
  1213. func lookupBoolFromEnv(envName string) (bool, bool) {
  1214. value, ok := os.LookupEnv(envName)
  1215. if ok {
  1216. converted, err := strconv.ParseBool(strings.TrimSpace(value))
  1217. if err == nil {
  1218. return converted, ok
  1219. }
  1220. }
  1221. return false, false
  1222. }
  1223. func lookupIntFromEnv(envName string) (int64, bool) {
  1224. value, ok := os.LookupEnv(envName)
  1225. if ok {
  1226. converted, err := strconv.ParseInt(strings.TrimSpace(value), 10, 64)
  1227. if err == nil {
  1228. return converted, ok
  1229. }
  1230. }
  1231. return 0, false
  1232. }
  1233. func lookupStringListFromEnv(envName string) ([]string, bool) {
  1234. value, ok := os.LookupEnv(envName)
  1235. if ok {
  1236. var result []string
  1237. for _, v := range strings.Split(value, ",") {
  1238. val := strings.TrimSpace(v)
  1239. if val != "" {
  1240. result = append(result, val)
  1241. }
  1242. }
  1243. return result, true
  1244. }
  1245. return nil, false
  1246. }