config.go 43 KB

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