config.go 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159
  1. // Copyright (C) 2019-2023 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. // Package config manages the configuration
  15. package config
  16. import (
  17. "errors"
  18. "fmt"
  19. "os"
  20. "path/filepath"
  21. "strconv"
  22. "strings"
  23. "github.com/spf13/viper"
  24. "github.com/subosito/gotenv"
  25. "github.com/drakkan/sftpgo/v2/internal/acme"
  26. "github.com/drakkan/sftpgo/v2/internal/command"
  27. "github.com/drakkan/sftpgo/v2/internal/common"
  28. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  29. "github.com/drakkan/sftpgo/v2/internal/ftpd"
  30. "github.com/drakkan/sftpgo/v2/internal/httpclient"
  31. "github.com/drakkan/sftpgo/v2/internal/httpd"
  32. "github.com/drakkan/sftpgo/v2/internal/kms"
  33. "github.com/drakkan/sftpgo/v2/internal/logger"
  34. "github.com/drakkan/sftpgo/v2/internal/mfa"
  35. "github.com/drakkan/sftpgo/v2/internal/plugin"
  36. "github.com/drakkan/sftpgo/v2/internal/sftpd"
  37. "github.com/drakkan/sftpgo/v2/internal/smtp"
  38. "github.com/drakkan/sftpgo/v2/internal/telemetry"
  39. "github.com/drakkan/sftpgo/v2/internal/util"
  40. "github.com/drakkan/sftpgo/v2/internal/version"
  41. "github.com/drakkan/sftpgo/v2/internal/webdavd"
  42. )
  43. const (
  44. logSender = "config"
  45. // configName defines the name for config file.
  46. // This name does not include the extension, viper will search for files
  47. // with supported extensions such as "sftpgo.json", "sftpgo.yaml" and so on
  48. configName = "sftpgo"
  49. // ConfigEnvPrefix defines a prefix that environment variables will use
  50. configEnvPrefix = "sftpgo"
  51. )
  52. var (
  53. globalConf globalConfig
  54. defaultSFTPDBanner = fmt.Sprintf("SFTPGo_%v", version.Get().Version)
  55. defaultFTPDBanner = fmt.Sprintf("SFTPGo %v ready", version.Get().Version)
  56. defaultInstallCodeHint = "Installation code"
  57. defaultSFTPDBinding = sftpd.Binding{
  58. Address: "",
  59. Port: 2022,
  60. ApplyProxyConfig: true,
  61. }
  62. defaultFTPDBinding = ftpd.Binding{
  63. Address: "",
  64. Port: 0,
  65. ApplyProxyConfig: true,
  66. TLSMode: 0,
  67. CertificateFile: "",
  68. CertificateKeyFile: "",
  69. MinTLSVersion: 12,
  70. ForcePassiveIP: "",
  71. PassiveIPOverrides: nil,
  72. ClientAuthType: 0,
  73. TLSCipherSuites: nil,
  74. PassiveConnectionsSecurity: 0,
  75. ActiveConnectionsSecurity: 0,
  76. Debug: false,
  77. }
  78. defaultWebDAVDBinding = webdavd.Binding{
  79. Address: "",
  80. Port: 0,
  81. EnableHTTPS: false,
  82. CertificateFile: "",
  83. CertificateKeyFile: "",
  84. MinTLSVersion: 12,
  85. ClientAuthType: 0,
  86. TLSCipherSuites: nil,
  87. Prefix: "",
  88. ProxyAllowed: nil,
  89. ClientIPProxyHeader: "",
  90. ClientIPHeaderDepth: 0,
  91. DisableWWWAuthHeader: false,
  92. }
  93. defaultHTTPDBinding = httpd.Binding{
  94. Address: "",
  95. Port: 8080,
  96. EnableWebAdmin: true,
  97. EnableWebClient: true,
  98. EnableRESTAPI: true,
  99. EnabledLoginMethods: 0,
  100. EnableHTTPS: false,
  101. CertificateFile: "",
  102. CertificateKeyFile: "",
  103. MinTLSVersion: 12,
  104. ClientAuthType: 0,
  105. TLSCipherSuites: nil,
  106. ProxyAllowed: nil,
  107. ClientIPProxyHeader: "",
  108. ClientIPHeaderDepth: 0,
  109. HideLoginURL: 0,
  110. RenderOpenAPI: true,
  111. WebClientIntegrations: nil,
  112. OIDC: httpd.OIDC{
  113. ClientID: "",
  114. ClientSecret: "",
  115. ConfigURL: "",
  116. RedirectBaseURL: "",
  117. UsernameField: "",
  118. RoleField: "",
  119. ImplicitRoles: false,
  120. Scopes: []string{"openid", "profile", "email"},
  121. CustomFields: []string{},
  122. InsecureSkipSignatureCheck: false,
  123. Debug: false,
  124. },
  125. Security: httpd.SecurityConf{
  126. Enabled: false,
  127. AllowedHosts: nil,
  128. AllowedHostsAreRegex: false,
  129. HostsProxyHeaders: nil,
  130. HTTPSRedirect: false,
  131. HTTPSHost: "",
  132. HTTPSProxyHeaders: nil,
  133. STSSeconds: 0,
  134. STSIncludeSubdomains: false,
  135. STSPreload: false,
  136. ContentTypeNosniff: false,
  137. ContentSecurityPolicy: "",
  138. PermissionsPolicy: "",
  139. CrossOriginOpenerPolicy: "",
  140. ExpectCTHeader: "",
  141. },
  142. Branding: httpd.Branding{},
  143. }
  144. defaultRateLimiter = common.RateLimiterConfig{
  145. Average: 0,
  146. Period: 1000,
  147. Burst: 1,
  148. Type: 2,
  149. Protocols: []string{common.ProtocolSSH, common.ProtocolFTP, common.ProtocolWebDAV, common.ProtocolHTTP},
  150. GenerateDefenderEvents: false,
  151. EntriesSoftLimit: 100,
  152. EntriesHardLimit: 150,
  153. }
  154. defaultTOTP = mfa.TOTPConfig{
  155. Name: "Default",
  156. Issuer: "SFTPGo",
  157. Algo: mfa.TOTPAlgoSHA1,
  158. }
  159. )
  160. type globalConfig struct {
  161. Common common.Configuration `json:"common" mapstructure:"common"`
  162. ACME acme.Configuration `json:"acme" mapstructure:"acme"`
  163. SFTPD sftpd.Configuration `json:"sftpd" mapstructure:"sftpd"`
  164. FTPD ftpd.Configuration `json:"ftpd" mapstructure:"ftpd"`
  165. WebDAVD webdavd.Configuration `json:"webdavd" mapstructure:"webdavd"`
  166. ProviderConf dataprovider.Config `json:"data_provider" mapstructure:"data_provider"`
  167. HTTPDConfig httpd.Conf `json:"httpd" mapstructure:"httpd"`
  168. HTTPConfig httpclient.Config `json:"http" mapstructure:"http"`
  169. CommandConfig command.Config `json:"command" mapstructure:"command"`
  170. KMSConfig kms.Configuration `json:"kms" mapstructure:"kms"`
  171. MFAConfig mfa.Config `json:"mfa" mapstructure:"mfa"`
  172. TelemetryConfig telemetry.Conf `json:"telemetry" mapstructure:"telemetry"`
  173. PluginsConfig []plugin.Config `json:"plugins" mapstructure:"plugins"`
  174. SMTPConfig smtp.Config `json:"smtp" mapstructure:"smtp"`
  175. }
  176. func init() {
  177. Init()
  178. }
  179. // Init initializes the global configuration.
  180. // It is not supposed to be called outside of this package.
  181. // It is exported to minimize refactoring efforts. Will eventually disappear.
  182. func Init() {
  183. // create a default configuration to use if no config file is provided
  184. globalConf = globalConfig{
  185. Common: common.Configuration{
  186. IdleTimeout: 15,
  187. UploadMode: 0,
  188. Actions: common.ProtocolActions{
  189. ExecuteOn: []string{},
  190. ExecuteSync: []string{},
  191. Hook: "",
  192. },
  193. SetstatMode: 0,
  194. RenameMode: 0,
  195. TempPath: "",
  196. ProxyProtocol: 0,
  197. ProxyAllowed: []string{},
  198. PostConnectHook: "",
  199. PostDisconnectHook: "",
  200. DataRetentionHook: "",
  201. MaxTotalConnections: 0,
  202. MaxPerHostConnections: 20,
  203. AllowListStatus: 0,
  204. AllowSelfConnections: 0,
  205. DefenderConfig: common.DefenderConfig{
  206. Enabled: false,
  207. Driver: common.DefenderDriverMemory,
  208. BanTime: 30,
  209. BanTimeIncrement: 50,
  210. Threshold: 15,
  211. ScoreInvalid: 2,
  212. ScoreValid: 1,
  213. ScoreLimitExceeded: 3,
  214. ScoreNoAuth: 0,
  215. ObservationTime: 30,
  216. EntriesSoftLimit: 100,
  217. EntriesHardLimit: 150,
  218. },
  219. RateLimitersConfig: []common.RateLimiterConfig{defaultRateLimiter},
  220. },
  221. ACME: acme.Configuration{
  222. Email: "",
  223. KeyType: "4096",
  224. CertsPath: "certs",
  225. CAEndpoint: "https://acme-v02.api.letsencrypt.org/directory",
  226. Domains: []string{},
  227. RenewDays: 30,
  228. HTTP01Challenge: acme.HTTP01Challenge{
  229. Port: 80,
  230. WebRoot: "",
  231. ProxyHeader: "",
  232. },
  233. TLSALPN01Challenge: acme.TLSALPN01Challenge{
  234. Port: 0,
  235. },
  236. },
  237. SFTPD: sftpd.Configuration{
  238. Bindings: []sftpd.Binding{defaultSFTPDBinding},
  239. MaxAuthTries: 0,
  240. Banner: defaultSFTPDBanner,
  241. HostKeys: []string{},
  242. HostCertificates: []string{},
  243. HostKeyAlgorithms: []string{},
  244. Moduli: []string{},
  245. KexAlgorithms: []string{},
  246. Ciphers: []string{},
  247. MACs: []string{},
  248. TrustedUserCAKeys: []string{},
  249. RevokedUserCertsFile: "",
  250. LoginBannerFile: "",
  251. EnabledSSHCommands: []string{},
  252. KeyboardInteractiveAuthentication: false,
  253. KeyboardInteractiveHook: "",
  254. PasswordAuthentication: true,
  255. FolderPrefix: "",
  256. },
  257. FTPD: ftpd.Configuration{
  258. Bindings: []ftpd.Binding{defaultFTPDBinding},
  259. Banner: defaultFTPDBanner,
  260. BannerFile: "",
  261. ActiveTransfersPortNon20: true,
  262. PassivePortRange: ftpd.PortRange{
  263. Start: 50000,
  264. End: 50100,
  265. },
  266. DisableActiveMode: false,
  267. EnableSite: false,
  268. HASHSupport: 0,
  269. CombineSupport: 0,
  270. CertificateFile: "",
  271. CertificateKeyFile: "",
  272. CACertificates: []string{},
  273. CARevocationLists: []string{},
  274. },
  275. WebDAVD: webdavd.Configuration{
  276. Bindings: []webdavd.Binding{defaultWebDAVDBinding},
  277. CertificateFile: "",
  278. CertificateKeyFile: "",
  279. CACertificates: []string{},
  280. CARevocationLists: []string{},
  281. Cors: webdavd.CorsConfig{
  282. Enabled: false,
  283. AllowedOrigins: []string{},
  284. AllowedMethods: []string{},
  285. AllowedHeaders: []string{},
  286. ExposedHeaders: []string{},
  287. AllowCredentials: false,
  288. MaxAge: 0,
  289. OptionsPassthrough: false,
  290. OptionsSuccessStatus: 0,
  291. AllowPrivateNetwork: false,
  292. },
  293. Cache: webdavd.Cache{
  294. Users: webdavd.UsersCacheConfig{
  295. ExpirationTime: 0,
  296. MaxSize: 50,
  297. },
  298. MimeTypes: webdavd.MimeCacheConfig{
  299. Enabled: true,
  300. MaxSize: 1000,
  301. CustomMappings: nil,
  302. },
  303. },
  304. },
  305. ProviderConf: dataprovider.Config{
  306. Driver: "sqlite",
  307. Name: "sftpgo.db",
  308. Host: "",
  309. Port: 0,
  310. Username: "",
  311. Password: "",
  312. ConnectionString: "",
  313. SQLTablesPrefix: "",
  314. SSLMode: 0,
  315. DisableSNI: false,
  316. TargetSessionAttrs: "",
  317. RootCert: "",
  318. ClientCert: "",
  319. ClientKey: "",
  320. TrackQuota: 2,
  321. PoolSize: 0,
  322. UsersBaseDir: "",
  323. Actions: dataprovider.ObjectsActions{
  324. ExecuteOn: []string{},
  325. ExecuteFor: []string{},
  326. Hook: "",
  327. },
  328. ExternalAuthHook: "",
  329. ExternalAuthScope: 0,
  330. PreLoginHook: "",
  331. PostLoginHook: "",
  332. PostLoginScope: 0,
  333. CheckPasswordHook: "",
  334. CheckPasswordScope: 0,
  335. PasswordHashing: dataprovider.PasswordHashing{
  336. Argon2Options: dataprovider.Argon2Options{
  337. Memory: 65536,
  338. Iterations: 1,
  339. Parallelism: 2,
  340. },
  341. BcryptOptions: dataprovider.BcryptOptions{
  342. Cost: 10,
  343. },
  344. Algo: dataprovider.HashingAlgoBcrypt,
  345. },
  346. PasswordValidation: dataprovider.PasswordValidation{
  347. Admins: dataprovider.PasswordValidationRules{
  348. MinEntropy: 0,
  349. },
  350. Users: dataprovider.PasswordValidationRules{
  351. MinEntropy: 0,
  352. },
  353. },
  354. PasswordCaching: true,
  355. UpdateMode: 0,
  356. DelayedQuotaUpdate: 0,
  357. CreateDefaultAdmin: false,
  358. NamingRules: 1,
  359. IsShared: 0,
  360. Node: dataprovider.NodeConfig{
  361. Host: "",
  362. Port: 0,
  363. Proto: "http",
  364. },
  365. BackupsPath: "backups",
  366. },
  367. HTTPDConfig: httpd.Conf{
  368. Bindings: []httpd.Binding{defaultHTTPDBinding},
  369. TemplatesPath: "templates",
  370. StaticFilesPath: "static",
  371. OpenAPIPath: "openapi",
  372. WebRoot: "",
  373. CertificateFile: "",
  374. CertificateKeyFile: "",
  375. CACertificates: nil,
  376. CARevocationLists: nil,
  377. SigningPassphrase: "",
  378. TokenValidation: 0,
  379. MaxUploadFileSize: 1048576000,
  380. Cors: httpd.CorsConfig{
  381. Enabled: false,
  382. AllowedOrigins: []string{},
  383. AllowedMethods: []string{},
  384. AllowedHeaders: []string{},
  385. ExposedHeaders: []string{},
  386. AllowCredentials: false,
  387. MaxAge: 0,
  388. OptionsPassthrough: false,
  389. OptionsSuccessStatus: 0,
  390. AllowPrivateNetwork: false,
  391. },
  392. Setup: httpd.SetupConfig{
  393. InstallationCode: "",
  394. InstallationCodeHint: defaultInstallCodeHint,
  395. },
  396. HideSupportLink: false,
  397. },
  398. HTTPConfig: httpclient.Config{
  399. Timeout: 20,
  400. RetryWaitMin: 2,
  401. RetryWaitMax: 30,
  402. RetryMax: 3,
  403. CACertificates: nil,
  404. Certificates: nil,
  405. SkipTLSVerify: false,
  406. Headers: nil,
  407. },
  408. CommandConfig: command.Config{
  409. Timeout: 30,
  410. Env: nil,
  411. Commands: nil,
  412. },
  413. KMSConfig: kms.Configuration{
  414. Secrets: kms.Secrets{
  415. URL: "",
  416. MasterKeyString: "",
  417. MasterKeyPath: "",
  418. },
  419. },
  420. MFAConfig: mfa.Config{
  421. TOTP: []mfa.TOTPConfig{defaultTOTP},
  422. },
  423. TelemetryConfig: telemetry.Conf{
  424. BindPort: 0,
  425. BindAddress: "127.0.0.1",
  426. EnableProfiler: false,
  427. AuthUserFile: "",
  428. CertificateFile: "",
  429. CertificateKeyFile: "",
  430. MinTLSVersion: 12,
  431. TLSCipherSuites: nil,
  432. },
  433. SMTPConfig: smtp.Config{
  434. Host: "",
  435. Port: 25,
  436. From: "",
  437. User: "",
  438. Password: "",
  439. AuthType: 0,
  440. Encryption: 0,
  441. Domain: "",
  442. TemplatesPath: "templates",
  443. },
  444. PluginsConfig: nil,
  445. }
  446. viper.SetEnvPrefix(configEnvPrefix)
  447. replacer := strings.NewReplacer(".", "__")
  448. viper.SetEnvKeyReplacer(replacer)
  449. viper.SetConfigName(configName)
  450. setViperDefaults()
  451. viper.AutomaticEnv()
  452. viper.AllowEmptyEnv(true)
  453. }
  454. // GetCommonConfig returns the common protocols configuration
  455. func GetCommonConfig() common.Configuration {
  456. return globalConf.Common
  457. }
  458. // SetCommonConfig sets the common protocols configuration
  459. func SetCommonConfig(config common.Configuration) {
  460. globalConf.Common = config
  461. }
  462. // GetSFTPDConfig returns the configuration for the SFTP server
  463. func GetSFTPDConfig() sftpd.Configuration {
  464. return globalConf.SFTPD
  465. }
  466. // SetSFTPDConfig sets the configuration for the SFTP server
  467. func SetSFTPDConfig(config sftpd.Configuration) {
  468. globalConf.SFTPD = config
  469. }
  470. // GetFTPDConfig returns the configuration for the FTP server
  471. func GetFTPDConfig() ftpd.Configuration {
  472. return globalConf.FTPD
  473. }
  474. // SetFTPDConfig sets the configuration for the FTP server
  475. func SetFTPDConfig(config ftpd.Configuration) {
  476. globalConf.FTPD = config
  477. }
  478. // GetWebDAVDConfig returns the configuration for the WebDAV server
  479. func GetWebDAVDConfig() webdavd.Configuration {
  480. return globalConf.WebDAVD
  481. }
  482. // SetWebDAVDConfig sets the configuration for the WebDAV server
  483. func SetWebDAVDConfig(config webdavd.Configuration) {
  484. globalConf.WebDAVD = config
  485. }
  486. // GetHTTPDConfig returns the configuration for the HTTP server
  487. func GetHTTPDConfig() httpd.Conf {
  488. return globalConf.HTTPDConfig
  489. }
  490. // SetHTTPDConfig sets the configuration for the HTTP server
  491. func SetHTTPDConfig(config httpd.Conf) {
  492. globalConf.HTTPDConfig = config
  493. }
  494. // GetProviderConf returns the configuration for the data provider
  495. func GetProviderConf() dataprovider.Config {
  496. return globalConf.ProviderConf
  497. }
  498. // SetProviderConf sets the configuration for the data provider
  499. func SetProviderConf(config dataprovider.Config) {
  500. globalConf.ProviderConf = config
  501. }
  502. // GetHTTPConfig returns the configuration for HTTP clients
  503. func GetHTTPConfig() httpclient.Config {
  504. return globalConf.HTTPConfig
  505. }
  506. // GetCommandConfig returns the configuration for external commands
  507. func GetCommandConfig() command.Config {
  508. return globalConf.CommandConfig
  509. }
  510. // GetKMSConfig returns the KMS configuration
  511. func GetKMSConfig() kms.Configuration {
  512. return globalConf.KMSConfig
  513. }
  514. // SetKMSConfig sets the kms configuration
  515. func SetKMSConfig(config kms.Configuration) {
  516. globalConf.KMSConfig = config
  517. }
  518. // GetTelemetryConfig returns the telemetry configuration
  519. func GetTelemetryConfig() telemetry.Conf {
  520. return globalConf.TelemetryConfig
  521. }
  522. // SetTelemetryConfig sets the telemetry configuration
  523. func SetTelemetryConfig(config telemetry.Conf) {
  524. globalConf.TelemetryConfig = config
  525. }
  526. // GetPluginsConfig returns the plugins configuration
  527. func GetPluginsConfig() []plugin.Config {
  528. return globalConf.PluginsConfig
  529. }
  530. // SetPluginsConfig sets the plugin configuration
  531. func SetPluginsConfig(config []plugin.Config) {
  532. globalConf.PluginsConfig = config
  533. }
  534. // GetMFAConfig returns multi-factor authentication config
  535. func GetMFAConfig() mfa.Config {
  536. return globalConf.MFAConfig
  537. }
  538. // GetSMTPConfig returns the SMTP configuration
  539. func GetSMTPConfig() smtp.Config {
  540. return globalConf.SMTPConfig
  541. }
  542. // GetACMEConfig returns the ACME configuration
  543. func GetACMEConfig() acme.Configuration {
  544. return globalConf.ACME
  545. }
  546. // HasServicesToStart returns true if the config defines at least a service to start.
  547. // Supported services are SFTP, FTP and WebDAV
  548. func HasServicesToStart() bool {
  549. if globalConf.SFTPD.ShouldBind() {
  550. return true
  551. }
  552. if globalConf.FTPD.ShouldBind() {
  553. return true
  554. }
  555. if globalConf.WebDAVD.ShouldBind() {
  556. return true
  557. }
  558. if globalConf.HTTPDConfig.ShouldBind() {
  559. return true
  560. }
  561. return false
  562. }
  563. func getRedactedPassword(value string) string {
  564. if value == "" {
  565. return value
  566. }
  567. return "[redacted]"
  568. }
  569. func getRedactedGlobalConf() globalConfig {
  570. conf := globalConf
  571. conf.Common.Actions.Hook = util.GetRedactedURL(conf.Common.Actions.Hook)
  572. conf.Common.StartupHook = util.GetRedactedURL(conf.Common.StartupHook)
  573. conf.Common.PostConnectHook = util.GetRedactedURL(conf.Common.PostConnectHook)
  574. conf.Common.PostDisconnectHook = util.GetRedactedURL(conf.Common.PostDisconnectHook)
  575. conf.Common.DataRetentionHook = util.GetRedactedURL(conf.Common.DataRetentionHook)
  576. conf.SFTPD.KeyboardInteractiveHook = util.GetRedactedURL(conf.SFTPD.KeyboardInteractiveHook)
  577. conf.HTTPDConfig.SigningPassphrase = getRedactedPassword(conf.HTTPDConfig.SigningPassphrase)
  578. conf.HTTPDConfig.Setup.InstallationCode = getRedactedPassword(conf.HTTPDConfig.Setup.InstallationCode)
  579. conf.ProviderConf.Password = getRedactedPassword(conf.ProviderConf.Password)
  580. conf.ProviderConf.Actions.Hook = util.GetRedactedURL(conf.ProviderConf.Actions.Hook)
  581. conf.ProviderConf.ExternalAuthHook = util.GetRedactedURL(conf.ProviderConf.ExternalAuthHook)
  582. conf.ProviderConf.PreLoginHook = util.GetRedactedURL(conf.ProviderConf.PreLoginHook)
  583. conf.ProviderConf.PostLoginHook = util.GetRedactedURL(conf.ProviderConf.PostLoginHook)
  584. conf.ProviderConf.CheckPasswordHook = util.GetRedactedURL(conf.ProviderConf.CheckPasswordHook)
  585. conf.SMTPConfig.Password = getRedactedPassword(conf.SMTPConfig.Password)
  586. conf.HTTPDConfig.Bindings = nil
  587. for _, binding := range globalConf.HTTPDConfig.Bindings {
  588. binding.OIDC.ClientID = getRedactedPassword(binding.OIDC.ClientID)
  589. binding.OIDC.ClientSecret = getRedactedPassword(binding.OIDC.ClientSecret)
  590. conf.HTTPDConfig.Bindings = append(conf.HTTPDConfig.Bindings, binding)
  591. }
  592. return conf
  593. }
  594. func setConfigFile(configDir, configFile string) {
  595. if configFile == "" {
  596. return
  597. }
  598. if !filepath.IsAbs(configFile) && util.IsFileInputValid(configFile) {
  599. configFile = filepath.Join(configDir, configFile)
  600. }
  601. viper.SetConfigFile(configFile)
  602. }
  603. // readEnvFiles reads files inside the "env.d" directory relative to configDir
  604. // and then export the valid variables into environment variables if they do
  605. // not exist
  606. func readEnvFiles(configDir string) {
  607. envd := filepath.Join(configDir, "env.d")
  608. entries, err := os.ReadDir(envd)
  609. if err != nil {
  610. logger.Info(logSender, "", "unable to read env files from %q: %v", envd, err)
  611. return
  612. }
  613. for _, entry := range entries {
  614. info, err := entry.Info()
  615. if err == nil && info.Mode().IsRegular() {
  616. envFile := filepath.Join(envd, entry.Name())
  617. err = gotenv.Load(envFile)
  618. if err != nil {
  619. logger.Error(logSender, "", "unable to load env vars from file %q, err: %v", envFile, err)
  620. } else {
  621. logger.Info(logSender, "", "set env vars from file %q", envFile)
  622. }
  623. }
  624. }
  625. }
  626. func checkOverrideDefaultSettings() {
  627. // for slices we need to set the defaults to nil if the key is set in the config file,
  628. // otherwise the values are merged and not replaced as expected
  629. rateLimiters := viper.Get("common.rate_limiters")
  630. if val, ok := rateLimiters.([]any); ok {
  631. if len(val) > 0 {
  632. if rl, ok := val[0].(map[string]any); ok {
  633. if _, ok := rl["protocols"]; ok {
  634. globalConf.Common.RateLimitersConfig[0].Protocols = nil
  635. }
  636. }
  637. }
  638. }
  639. httpdBindings := viper.Get("httpd.bindings")
  640. if val, ok := httpdBindings.([]any); ok {
  641. if len(val) > 0 {
  642. if binding, ok := val[0].(map[string]any); ok {
  643. if val, ok := binding["oidc"]; ok {
  644. if oidc, ok := val.(map[string]any); ok {
  645. if _, ok := oidc["scopes"]; ok {
  646. globalConf.HTTPDConfig.Bindings[0].OIDC.Scopes = nil
  647. }
  648. }
  649. }
  650. }
  651. }
  652. }
  653. if util.Contains(viper.AllKeys(), "mfa.totp") {
  654. globalConf.MFAConfig.TOTP = nil
  655. }
  656. }
  657. // LoadConfig loads the configuration
  658. // configDir will be added to the configuration search paths.
  659. // The search path contains by default the current directory and on linux it contains
  660. // $HOME/.config/sftpgo and /etc/sftpgo too.
  661. // configFile is an absolute or relative path (to the config dir) to the configuration file.
  662. func LoadConfig(configDir, configFile string) error {
  663. var err error
  664. readEnvFiles(configDir)
  665. viper.AddConfigPath(configDir)
  666. setViperAdditionalConfigPaths()
  667. viper.AddConfigPath(".")
  668. setConfigFile(configDir, configFile)
  669. if err = viper.ReadInConfig(); err != nil {
  670. // if the user specify a configuration file we get os.ErrNotExist.
  671. // viper.ConfigFileNotFoundError is returned if viper is unable
  672. // to find sftpgo.{json,yaml, etc..} in any of the search paths
  673. if errors.As(err, &viper.ConfigFileNotFoundError{}) {
  674. logger.Debug(logSender, "", "no configuration file found")
  675. } else {
  676. // should we return the error and not start here?
  677. logger.Warn(logSender, "", "error loading configuration file: %v", err)
  678. logger.WarnToConsole("error loading configuration file: %v", err)
  679. }
  680. }
  681. checkOverrideDefaultSettings()
  682. err = viper.Unmarshal(&globalConf)
  683. if err != nil {
  684. logger.Warn(logSender, "", "error parsing configuration file: %v", err)
  685. logger.WarnToConsole("error parsing configuration file: %v", err)
  686. return err
  687. }
  688. // viper only supports slice of strings from env vars, so we use our custom method
  689. loadBindingsFromEnv()
  690. loadWebDAVCacheMappingsFromEnv()
  691. resetInvalidConfigs()
  692. logger.Debug(logSender, "", "config file used: '%#v', config loaded: %+v", viper.ConfigFileUsed(), getRedactedGlobalConf())
  693. return nil
  694. }
  695. func isUploadModeValid() bool {
  696. return globalConf.Common.UploadMode >= 0 && globalConf.Common.UploadMode <= 2
  697. }
  698. func isProxyProtocolValid() bool {
  699. return globalConf.Common.ProxyProtocol >= 0 && globalConf.Common.ProxyProtocol <= 2
  700. }
  701. func isExternalAuthScopeValid() bool {
  702. return globalConf.ProviderConf.ExternalAuthScope >= 0 && globalConf.ProviderConf.ExternalAuthScope <= 15
  703. }
  704. func resetInvalidConfigs() {
  705. if strings.TrimSpace(globalConf.SFTPD.Banner) == "" {
  706. globalConf.SFTPD.Banner = defaultSFTPDBanner
  707. }
  708. if strings.TrimSpace(globalConf.FTPD.Banner) == "" {
  709. globalConf.FTPD.Banner = defaultFTPDBanner
  710. }
  711. if strings.TrimSpace(globalConf.HTTPDConfig.Setup.InstallationCodeHint) == "" {
  712. globalConf.HTTPDConfig.Setup.InstallationCodeHint = defaultInstallCodeHint
  713. }
  714. if globalConf.ProviderConf.UsersBaseDir != "" && !util.IsFileInputValid(globalConf.ProviderConf.UsersBaseDir) {
  715. warn := fmt.Sprintf("invalid users base dir %#v will be ignored", globalConf.ProviderConf.UsersBaseDir)
  716. globalConf.ProviderConf.UsersBaseDir = ""
  717. logger.Warn(logSender, "", "Non-fatal configuration error: %v", warn)
  718. logger.WarnToConsole("Non-fatal configuration error: %v", warn)
  719. }
  720. if !isUploadModeValid() {
  721. warn := fmt.Sprintf("invalid upload_mode 0, 1 and 2 are supported, configured: %v reset upload_mode to 0",
  722. globalConf.Common.UploadMode)
  723. globalConf.Common.UploadMode = 0
  724. logger.Warn(logSender, "", "Non-fatal configuration error: %v", warn)
  725. logger.WarnToConsole("Non-fatal configuration error: %v", warn)
  726. }
  727. if !isProxyProtocolValid() {
  728. warn := fmt.Sprintf("invalid proxy_protocol 0, 1 and 2 are supported, configured: %v reset proxy_protocol to 0",
  729. globalConf.Common.ProxyProtocol)
  730. globalConf.Common.ProxyProtocol = 0
  731. logger.Warn(logSender, "", "Non-fatal configuration error: %v", warn)
  732. logger.WarnToConsole("Non-fatal configuration error: %v", warn)
  733. }
  734. if !isExternalAuthScopeValid() {
  735. warn := fmt.Sprintf("invalid external_auth_scope: %v reset to 0", globalConf.ProviderConf.ExternalAuthScope)
  736. globalConf.ProviderConf.ExternalAuthScope = 0
  737. logger.Warn(logSender, "", "Non-fatal configuration error: %v", warn)
  738. logger.WarnToConsole("Non-fatal configuration error: %v", warn)
  739. }
  740. if globalConf.Common.DefenderConfig.Enabled && globalConf.Common.DefenderConfig.Driver == common.DefenderDriverProvider {
  741. if !globalConf.ProviderConf.IsDefenderSupported() {
  742. warn := fmt.Sprintf("provider based defender is not supported with data provider %#v, "+
  743. "the memory defender implementation will be used. If you want to use the provider defender "+
  744. "implementation please switch to a shared/distributed data provider",
  745. globalConf.ProviderConf.Driver)
  746. globalConf.Common.DefenderConfig.Driver = common.DefenderDriverMemory
  747. logger.Warn(logSender, "", "Non-fatal configuration error: %v", warn)
  748. logger.WarnToConsole("Non-fatal configuration error: %v", warn)
  749. }
  750. }
  751. }
  752. func loadBindingsFromEnv() {
  753. for idx := 0; idx < 10; idx++ {
  754. getTOTPFromEnv(idx)
  755. getRateLimitersFromEnv(idx)
  756. getPluginsFromEnv(idx)
  757. getSFTPDBindindFromEnv(idx)
  758. getFTPDBindingFromEnv(idx)
  759. getWebDAVDBindingFromEnv(idx)
  760. getHTTPDBindingFromEnv(idx)
  761. getHTTPClientCertificatesFromEnv(idx)
  762. getHTTPClientHeadersFromEnv(idx)
  763. getCommandConfigsFromEnv(idx)
  764. }
  765. }
  766. func getTOTPFromEnv(idx int) {
  767. totpConfig := defaultTOTP
  768. if len(globalConf.MFAConfig.TOTP) > idx {
  769. totpConfig = globalConf.MFAConfig.TOTP[idx]
  770. }
  771. isSet := false
  772. name, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_MFA__TOTP__%v__NAME", idx))
  773. if ok {
  774. totpConfig.Name = name
  775. isSet = true
  776. }
  777. issuer, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_MFA__TOTP__%v__ISSUER", idx))
  778. if ok {
  779. totpConfig.Issuer = issuer
  780. isSet = true
  781. }
  782. algo, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_MFA__TOTP__%v__ALGO", idx))
  783. if ok {
  784. totpConfig.Algo = algo
  785. isSet = true
  786. }
  787. if isSet {
  788. if len(globalConf.MFAConfig.TOTP) > idx {
  789. globalConf.MFAConfig.TOTP[idx] = totpConfig
  790. } else {
  791. globalConf.MFAConfig.TOTP = append(globalConf.MFAConfig.TOTP, totpConfig)
  792. }
  793. }
  794. }
  795. func getRateLimitersFromEnv(idx int) {
  796. rtlConfig := defaultRateLimiter
  797. if len(globalConf.Common.RateLimitersConfig) > idx {
  798. rtlConfig = globalConf.Common.RateLimitersConfig[idx]
  799. }
  800. isSet := false
  801. average, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__AVERAGE", idx))
  802. if ok {
  803. rtlConfig.Average = average
  804. isSet = true
  805. }
  806. period, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__PERIOD", idx))
  807. if ok {
  808. rtlConfig.Period = period
  809. isSet = true
  810. }
  811. burst, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__BURST", idx))
  812. if ok {
  813. rtlConfig.Burst = int(burst)
  814. isSet = true
  815. }
  816. rtlType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__TYPE", idx))
  817. if ok {
  818. rtlConfig.Type = int(rtlType)
  819. isSet = true
  820. }
  821. protocols, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__PROTOCOLS", idx))
  822. if ok {
  823. rtlConfig.Protocols = protocols
  824. isSet = true
  825. }
  826. generateEvents, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__GENERATE_DEFENDER_EVENTS", idx))
  827. if ok {
  828. rtlConfig.GenerateDefenderEvents = generateEvents
  829. isSet = true
  830. }
  831. softLimit, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__ENTRIES_SOFT_LIMIT", idx))
  832. if ok {
  833. rtlConfig.EntriesSoftLimit = int(softLimit)
  834. isSet = true
  835. }
  836. hardLimit, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__ENTRIES_HARD_LIMIT", idx))
  837. if ok {
  838. rtlConfig.EntriesHardLimit = int(hardLimit)
  839. isSet = true
  840. }
  841. if isSet {
  842. if len(globalConf.Common.RateLimitersConfig) > idx {
  843. globalConf.Common.RateLimitersConfig[idx] = rtlConfig
  844. } else {
  845. globalConf.Common.RateLimitersConfig = append(globalConf.Common.RateLimitersConfig, rtlConfig)
  846. }
  847. }
  848. }
  849. func getKMSPluginFromEnv(idx int, pluginConfig *plugin.Config) bool {
  850. isSet := false
  851. kmsScheme, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__KMS_OPTIONS__SCHEME", idx))
  852. if ok {
  853. pluginConfig.KMSOptions.Scheme = kmsScheme
  854. isSet = true
  855. }
  856. kmsEncStatus, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__KMS_OPTIONS__ENCRYPTED_STATUS", idx))
  857. if ok {
  858. pluginConfig.KMSOptions.EncryptedStatus = kmsEncStatus
  859. isSet = true
  860. }
  861. return isSet
  862. }
  863. func getAuthPluginFromEnv(idx int, pluginConfig *plugin.Config) bool {
  864. isSet := false
  865. authScope, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__AUTH_OPTIONS__SCOPE", idx))
  866. if ok {
  867. pluginConfig.AuthOptions.Scope = int(authScope)
  868. isSet = true
  869. }
  870. return isSet
  871. }
  872. func getNotifierPluginFromEnv(idx int, pluginConfig *plugin.Config) bool {
  873. isSet := false
  874. notifierFsEvents, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__FS_EVENTS", idx))
  875. if ok {
  876. pluginConfig.NotifierOptions.FsEvents = notifierFsEvents
  877. isSet = true
  878. }
  879. notifierProviderEvents, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__PROVIDER_EVENTS", idx))
  880. if ok {
  881. pluginConfig.NotifierOptions.ProviderEvents = notifierProviderEvents
  882. isSet = true
  883. }
  884. notifierProviderObjects, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__PROVIDER_OBJECTS", idx))
  885. if ok {
  886. pluginConfig.NotifierOptions.ProviderObjects = notifierProviderObjects
  887. isSet = true
  888. }
  889. notifierRetryMaxTime, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__RETRY_MAX_TIME", idx))
  890. if ok {
  891. pluginConfig.NotifierOptions.RetryMaxTime = int(notifierRetryMaxTime)
  892. isSet = true
  893. }
  894. notifierRetryQueueMaxSize, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__RETRY_QUEUE_MAX_SIZE", idx))
  895. if ok {
  896. pluginConfig.NotifierOptions.RetryQueueMaxSize = int(notifierRetryQueueMaxSize)
  897. isSet = true
  898. }
  899. return isSet
  900. }
  901. func getPluginsFromEnv(idx int) {
  902. pluginConfig := plugin.Config{}
  903. if len(globalConf.PluginsConfig) > idx {
  904. pluginConfig = globalConf.PluginsConfig[idx]
  905. }
  906. isSet := false
  907. pluginType, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__TYPE", idx))
  908. if ok {
  909. pluginConfig.Type = pluginType
  910. isSet = true
  911. }
  912. if getNotifierPluginFromEnv(idx, &pluginConfig) {
  913. isSet = true
  914. }
  915. if getKMSPluginFromEnv(idx, &pluginConfig) {
  916. isSet = true
  917. }
  918. if getAuthPluginFromEnv(idx, &pluginConfig) {
  919. isSet = true
  920. }
  921. cmd, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__CMD", idx))
  922. if ok {
  923. pluginConfig.Cmd = cmd
  924. isSet = true
  925. }
  926. cmdArgs, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__ARGS", idx))
  927. if ok {
  928. pluginConfig.Args = cmdArgs
  929. isSet = true
  930. }
  931. pluginHash, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__SHA256SUM", idx))
  932. if ok {
  933. pluginConfig.SHA256Sum = pluginHash
  934. isSet = true
  935. }
  936. autoMTLS, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__AUTO_MTLS", idx))
  937. if ok {
  938. pluginConfig.AutoMTLS = autoMTLS
  939. isSet = true
  940. }
  941. if isSet {
  942. if len(globalConf.PluginsConfig) > idx {
  943. globalConf.PluginsConfig[idx] = pluginConfig
  944. } else {
  945. globalConf.PluginsConfig = append(globalConf.PluginsConfig, pluginConfig)
  946. }
  947. }
  948. }
  949. func getSFTPDBindindFromEnv(idx int) {
  950. binding := defaultSFTPDBinding
  951. if len(globalConf.SFTPD.Bindings) > idx {
  952. binding = globalConf.SFTPD.Bindings[idx]
  953. }
  954. isSet := false
  955. port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_SFTPD__BINDINGS__%v__PORT", idx))
  956. if ok {
  957. binding.Port = int(port)
  958. isSet = true
  959. }
  960. address, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_SFTPD__BINDINGS__%v__ADDRESS", idx))
  961. if ok {
  962. binding.Address = address
  963. isSet = true
  964. }
  965. applyProxyConfig, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_SFTPD__BINDINGS__%v__APPLY_PROXY_CONFIG", idx))
  966. if ok {
  967. binding.ApplyProxyConfig = applyProxyConfig
  968. isSet = true
  969. }
  970. if isSet {
  971. if len(globalConf.SFTPD.Bindings) > idx {
  972. globalConf.SFTPD.Bindings[idx] = binding
  973. } else {
  974. globalConf.SFTPD.Bindings = append(globalConf.SFTPD.Bindings, binding)
  975. }
  976. }
  977. }
  978. func getFTPDPassiveIPOverridesFromEnv(idx int) []ftpd.PassiveIPOverride {
  979. var overrides []ftpd.PassiveIPOverride
  980. if len(globalConf.FTPD.Bindings) > idx {
  981. overrides = globalConf.FTPD.Bindings[idx].PassiveIPOverrides
  982. }
  983. for subIdx := 0; subIdx < 10; subIdx++ {
  984. var override ftpd.PassiveIPOverride
  985. var replace bool
  986. if len(globalConf.FTPD.Bindings) > idx && len(globalConf.FTPD.Bindings[idx].PassiveIPOverrides) > subIdx {
  987. override = globalConf.FTPD.Bindings[idx].PassiveIPOverrides[subIdx]
  988. replace = true
  989. }
  990. ip, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__PASSIVE_IP_OVERRIDES__%v__IP", idx, subIdx))
  991. if ok {
  992. override.IP = ip
  993. }
  994. networks, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__PASSIVE_IP_OVERRIDES__%v__NETWORKS",
  995. idx, subIdx))
  996. if ok {
  997. override.Networks = networks
  998. }
  999. if len(override.Networks) > 0 {
  1000. if replace {
  1001. overrides[subIdx] = override
  1002. } else {
  1003. overrides = append(overrides, override)
  1004. }
  1005. }
  1006. }
  1007. return overrides
  1008. }
  1009. func getDefaultFTPDBinding(idx int) ftpd.Binding {
  1010. binding := defaultFTPDBinding
  1011. if len(globalConf.FTPD.Bindings) > idx {
  1012. binding = globalConf.FTPD.Bindings[idx]
  1013. }
  1014. return binding
  1015. }
  1016. func getFTPDBindingFromEnv(idx int) {
  1017. binding := getDefaultFTPDBinding(idx)
  1018. isSet := false
  1019. port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__PORT", idx))
  1020. if ok {
  1021. binding.Port = int(port)
  1022. isSet = true
  1023. }
  1024. address, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__ADDRESS", idx))
  1025. if ok {
  1026. binding.Address = address
  1027. isSet = true
  1028. }
  1029. applyProxyConfig, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__APPLY_PROXY_CONFIG", idx))
  1030. if ok {
  1031. binding.ApplyProxyConfig = applyProxyConfig
  1032. isSet = true
  1033. }
  1034. certificateFile, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__CERTIFICATE_FILE", idx))
  1035. if ok {
  1036. binding.CertificateFile = certificateFile
  1037. isSet = true
  1038. }
  1039. certificateKeyFile, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__CERTIFICATE_KEY_FILE", idx))
  1040. if ok {
  1041. binding.CertificateKeyFile = certificateKeyFile
  1042. isSet = true
  1043. }
  1044. tlsMode, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__TLS_MODE", idx))
  1045. if ok {
  1046. binding.TLSMode = int(tlsMode)
  1047. isSet = true
  1048. }
  1049. tlsVer, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__MIN_TLS_VERSION", idx))
  1050. if ok {
  1051. binding.MinTLSVersion = int(tlsVer)
  1052. isSet = true
  1053. }
  1054. passiveIP, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__FORCE_PASSIVE_IP", idx))
  1055. if ok {
  1056. binding.ForcePassiveIP = passiveIP
  1057. isSet = true
  1058. }
  1059. passiveIPOverrides := getFTPDPassiveIPOverridesFromEnv(idx)
  1060. if len(passiveIPOverrides) > 0 {
  1061. binding.PassiveIPOverrides = passiveIPOverrides
  1062. isSet = true
  1063. }
  1064. clientAuthType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__CLIENT_AUTH_TYPE", idx))
  1065. if ok {
  1066. binding.ClientAuthType = int(clientAuthType)
  1067. isSet = true
  1068. }
  1069. tlsCiphers, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__TLS_CIPHER_SUITES", idx))
  1070. if ok {
  1071. binding.TLSCipherSuites = tlsCiphers
  1072. isSet = true
  1073. }
  1074. pasvSecurity, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__PASSIVE_CONNECTIONS_SECURITY", idx))
  1075. if ok {
  1076. binding.PassiveConnectionsSecurity = int(pasvSecurity)
  1077. isSet = true
  1078. }
  1079. activeSecurity, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__ACTIVE_CONNECTIONS_SECURITY", idx))
  1080. if ok {
  1081. binding.ActiveConnectionsSecurity = int(activeSecurity)
  1082. isSet = true
  1083. }
  1084. debug, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__DEBUG", idx))
  1085. if ok {
  1086. binding.Debug = debug
  1087. isSet = true
  1088. }
  1089. applyFTPDBindingFromEnv(idx, isSet, binding)
  1090. }
  1091. func applyFTPDBindingFromEnv(idx int, isSet bool, binding ftpd.Binding) {
  1092. if isSet {
  1093. if len(globalConf.FTPD.Bindings) > idx {
  1094. globalConf.FTPD.Bindings[idx] = binding
  1095. } else {
  1096. globalConf.FTPD.Bindings = append(globalConf.FTPD.Bindings, binding)
  1097. }
  1098. }
  1099. }
  1100. func getWebDAVDBindingProxyConfigsFromEnv(idx int, binding *webdavd.Binding) bool {
  1101. isSet := false
  1102. proxyAllowed, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__PROXY_ALLOWED", idx))
  1103. if ok {
  1104. binding.ProxyAllowed = proxyAllowed
  1105. isSet = true
  1106. }
  1107. clientIPProxyHeader, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__CLIENT_IP_PROXY_HEADER", idx))
  1108. if ok {
  1109. binding.ClientIPProxyHeader = clientIPProxyHeader
  1110. isSet = true
  1111. }
  1112. clientIPHeaderDepth, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__CLIENT_IP_HEADER_DEPTH", idx))
  1113. if ok {
  1114. binding.ClientIPHeaderDepth = int(clientIPHeaderDepth)
  1115. isSet = true
  1116. }
  1117. return isSet
  1118. }
  1119. func loadWebDAVCacheMappingsFromEnv() []webdavd.CustomMimeMapping {
  1120. for idx := 0; idx < 30; idx++ {
  1121. ext, extOK := os.LookupEnv(fmt.Sprintf("SFTPGO_WEBDAVD__CACHE__MIME_TYPES__CUSTOM_MAPPINGS__%d__EXT", idx))
  1122. mime, mimeOK := os.LookupEnv(fmt.Sprintf("SFTPGO_WEBDAVD__CACHE__MIME_TYPES__CUSTOM_MAPPINGS__%d__MIME", idx))
  1123. if extOK && mimeOK {
  1124. if len(globalConf.WebDAVD.Cache.MimeTypes.CustomMappings) > idx {
  1125. globalConf.WebDAVD.Cache.MimeTypes.CustomMappings[idx].Ext = ext
  1126. globalConf.WebDAVD.Cache.MimeTypes.CustomMappings[idx].Mime = mime
  1127. } else {
  1128. globalConf.WebDAVD.Cache.MimeTypes.CustomMappings = append(globalConf.WebDAVD.Cache.MimeTypes.CustomMappings,
  1129. webdavd.CustomMimeMapping{
  1130. Ext: ext,
  1131. Mime: mime,
  1132. })
  1133. }
  1134. }
  1135. }
  1136. return globalConf.WebDAVD.Cache.MimeTypes.CustomMappings
  1137. }
  1138. func getWebDAVDBindingFromEnv(idx int) {
  1139. binding := defaultWebDAVDBinding
  1140. if len(globalConf.WebDAVD.Bindings) > idx {
  1141. binding = globalConf.WebDAVD.Bindings[idx]
  1142. }
  1143. isSet := false
  1144. port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__PORT", idx))
  1145. if ok {
  1146. binding.Port = int(port)
  1147. isSet = true
  1148. }
  1149. address, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__ADDRESS", idx))
  1150. if ok {
  1151. binding.Address = address
  1152. isSet = true
  1153. }
  1154. enableHTTPS, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__ENABLE_HTTPS", idx))
  1155. if ok {
  1156. binding.EnableHTTPS = enableHTTPS
  1157. isSet = true
  1158. }
  1159. certificateFile, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__CERTIFICATE_FILE", idx))
  1160. if ok {
  1161. binding.CertificateFile = certificateFile
  1162. isSet = true
  1163. }
  1164. certificateKeyFile, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__CERTIFICATE_KEY_FILE", idx))
  1165. if ok {
  1166. binding.CertificateKeyFile = certificateKeyFile
  1167. isSet = true
  1168. }
  1169. tlsVer, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__MIN_TLS_VERSION", idx))
  1170. if ok {
  1171. binding.MinTLSVersion = int(tlsVer)
  1172. isSet = true
  1173. }
  1174. clientAuthType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__CLIENT_AUTH_TYPE", idx))
  1175. if ok {
  1176. binding.ClientAuthType = int(clientAuthType)
  1177. isSet = true
  1178. }
  1179. tlsCiphers, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__TLS_CIPHER_SUITES", idx))
  1180. if ok {
  1181. binding.TLSCipherSuites = tlsCiphers
  1182. isSet = true
  1183. }
  1184. prefix, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__PREFIX", idx))
  1185. if ok {
  1186. binding.Prefix = prefix
  1187. isSet = true
  1188. }
  1189. if getWebDAVDBindingProxyConfigsFromEnv(idx, &binding) {
  1190. isSet = true
  1191. }
  1192. disableWWWAuth, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__DISABLE_WWW_AUTH_HEADER", idx))
  1193. if ok {
  1194. binding.DisableWWWAuthHeader = disableWWWAuth
  1195. isSet = true
  1196. }
  1197. if isSet {
  1198. if len(globalConf.WebDAVD.Bindings) > idx {
  1199. globalConf.WebDAVD.Bindings[idx] = binding
  1200. } else {
  1201. globalConf.WebDAVD.Bindings = append(globalConf.WebDAVD.Bindings, binding)
  1202. }
  1203. }
  1204. }
  1205. func getHTTPDSecurityProxyHeadersFromEnv(idx int) []httpd.HTTPSProxyHeader {
  1206. var httpsProxyHeaders []httpd.HTTPSProxyHeader
  1207. if len(globalConf.HTTPDConfig.Bindings) > idx {
  1208. httpsProxyHeaders = globalConf.HTTPDConfig.Bindings[idx].Security.HTTPSProxyHeaders
  1209. }
  1210. for subIdx := 0; subIdx < 10; subIdx++ {
  1211. var httpsProxyHeader httpd.HTTPSProxyHeader
  1212. var replace bool
  1213. if len(globalConf.HTTPDConfig.Bindings) > idx &&
  1214. len(globalConf.HTTPDConfig.Bindings[idx].Security.HTTPSProxyHeaders) > subIdx {
  1215. httpsProxyHeader = httpsProxyHeaders[subIdx]
  1216. replace = true
  1217. }
  1218. proxyKey, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__HTTPS_PROXY_HEADERS__%v__KEY",
  1219. idx, subIdx))
  1220. if ok {
  1221. httpsProxyHeader.Key = proxyKey
  1222. }
  1223. proxyVal, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__HTTPS_PROXY_HEADERS__%v__VALUE",
  1224. idx, subIdx))
  1225. if ok {
  1226. httpsProxyHeader.Value = proxyVal
  1227. }
  1228. if httpsProxyHeader.Key != "" && httpsProxyHeader.Value != "" {
  1229. if replace {
  1230. httpsProxyHeaders[subIdx] = httpsProxyHeader
  1231. } else {
  1232. httpsProxyHeaders = append(httpsProxyHeaders, httpsProxyHeader)
  1233. }
  1234. }
  1235. }
  1236. return httpsProxyHeaders
  1237. }
  1238. func getHTTPDSecurityConfFromEnv(idx int) (httpd.SecurityConf, bool) { //nolint:gocyclo
  1239. result := defaultHTTPDBinding.Security
  1240. if len(globalConf.HTTPDConfig.Bindings) > idx {
  1241. result = globalConf.HTTPDConfig.Bindings[idx].Security
  1242. }
  1243. isSet := false
  1244. enabled, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__ENABLED", idx))
  1245. if ok {
  1246. result.Enabled = enabled
  1247. isSet = true
  1248. }
  1249. allowedHosts, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__ALLOWED_HOSTS", idx))
  1250. if ok {
  1251. result.AllowedHosts = allowedHosts
  1252. isSet = true
  1253. }
  1254. allowedHostsAreRegex, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__ALLOWED_HOSTS_ARE_REGEX", idx))
  1255. if ok {
  1256. result.AllowedHostsAreRegex = allowedHostsAreRegex
  1257. isSet = true
  1258. }
  1259. hostsProxyHeaders, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__HOSTS_PROXY_HEADERS", idx))
  1260. if ok {
  1261. result.HostsProxyHeaders = hostsProxyHeaders
  1262. isSet = true
  1263. }
  1264. httpsRedirect, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__HTTPS_REDIRECT", idx))
  1265. if ok {
  1266. result.HTTPSRedirect = httpsRedirect
  1267. isSet = true
  1268. }
  1269. httpsHost, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__HTTPS_HOST", idx))
  1270. if ok {
  1271. result.HTTPSHost = httpsHost
  1272. isSet = true
  1273. }
  1274. httpsProxyHeaders := getHTTPDSecurityProxyHeadersFromEnv(idx)
  1275. if len(httpsProxyHeaders) > 0 {
  1276. result.HTTPSProxyHeaders = httpsProxyHeaders
  1277. isSet = true
  1278. }
  1279. stsSeconds, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__STS_SECONDS", idx))
  1280. if ok {
  1281. result.STSSeconds = stsSeconds
  1282. isSet = true
  1283. }
  1284. stsIncludeSubDomains, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__STS_INCLUDE_SUBDOMAINS", idx))
  1285. if ok {
  1286. result.STSIncludeSubdomains = stsIncludeSubDomains
  1287. isSet = true
  1288. }
  1289. stsPreload, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__STS_PRELOAD", idx))
  1290. if ok {
  1291. result.STSPreload = stsPreload
  1292. isSet = true
  1293. }
  1294. contentTypeNosniff, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__CONTENT_TYPE_NOSNIFF", idx))
  1295. if ok {
  1296. result.ContentTypeNosniff = contentTypeNosniff
  1297. isSet = true
  1298. }
  1299. contentSecurityPolicy, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__CONTENT_SECURITY_POLICY", idx))
  1300. if ok {
  1301. result.ContentSecurityPolicy = contentSecurityPolicy
  1302. isSet = true
  1303. }
  1304. permissionsPolicy, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__PERMISSIONS_POLICY", idx))
  1305. if ok {
  1306. result.PermissionsPolicy = permissionsPolicy
  1307. isSet = true
  1308. }
  1309. crossOriginOpenedPolicy, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__CROSS_ORIGIN_OPENER_POLICY", idx))
  1310. if ok {
  1311. result.CrossOriginOpenerPolicy = crossOriginOpenedPolicy
  1312. isSet = true
  1313. }
  1314. expectCTHeader, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__EXPECT_CT_HEADER", idx))
  1315. if ok {
  1316. result.ExpectCTHeader = expectCTHeader
  1317. isSet = true
  1318. }
  1319. return result, isSet
  1320. }
  1321. func getHTTPDOIDCFromEnv(idx int) (httpd.OIDC, bool) {
  1322. result := defaultHTTPDBinding.OIDC
  1323. if len(globalConf.HTTPDConfig.Bindings) > idx {
  1324. result = globalConf.HTTPDConfig.Bindings[idx].OIDC
  1325. }
  1326. isSet := false
  1327. clientID, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__CLIENT_ID", idx))
  1328. if ok {
  1329. result.ClientID = clientID
  1330. isSet = true
  1331. }
  1332. clientSecret, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__CLIENT_SECRET", idx))
  1333. if ok {
  1334. result.ClientSecret = clientSecret
  1335. isSet = true
  1336. }
  1337. configURL, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__CONFIG_URL", idx))
  1338. if ok {
  1339. result.ConfigURL = configURL
  1340. isSet = true
  1341. }
  1342. redirectBaseURL, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__REDIRECT_BASE_URL", idx))
  1343. if ok {
  1344. result.RedirectBaseURL = redirectBaseURL
  1345. isSet = true
  1346. }
  1347. usernameField, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__USERNAME_FIELD", idx))
  1348. if ok {
  1349. result.UsernameField = usernameField
  1350. isSet = true
  1351. }
  1352. scopes, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__SCOPES", idx))
  1353. if ok {
  1354. result.Scopes = scopes
  1355. isSet = true
  1356. }
  1357. roleField, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__ROLE_FIELD", idx))
  1358. if ok {
  1359. result.RoleField = roleField
  1360. isSet = true
  1361. }
  1362. implicitRoles, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__IMPLICIT_ROLES", idx))
  1363. if ok {
  1364. result.ImplicitRoles = implicitRoles
  1365. isSet = true
  1366. }
  1367. customFields, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__CUSTOM_FIELDS", idx))
  1368. if ok {
  1369. result.CustomFields = customFields
  1370. isSet = true
  1371. }
  1372. skipSignatureCheck, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__INSECURE_SKIP_SIGNATURE_CHECK", idx))
  1373. if ok {
  1374. result.InsecureSkipSignatureCheck = skipSignatureCheck
  1375. isSet = true
  1376. }
  1377. debug, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__DEBUG", idx))
  1378. if ok {
  1379. result.Debug = debug
  1380. isSet = true
  1381. }
  1382. return result, isSet
  1383. }
  1384. func getHTTPDUIBrandingFromEnv(prefix string, branding httpd.UIBranding) (httpd.UIBranding, bool) {
  1385. isSet := false
  1386. name, ok := os.LookupEnv(fmt.Sprintf("%s__NAME", prefix))
  1387. if ok {
  1388. branding.Name = name
  1389. isSet = true
  1390. }
  1391. shortName, ok := os.LookupEnv(fmt.Sprintf("%s__SHORT_NAME", prefix))
  1392. if ok {
  1393. branding.ShortName = shortName
  1394. isSet = true
  1395. }
  1396. faviconPath, ok := os.LookupEnv(fmt.Sprintf("%s__FAVICON_PATH", prefix))
  1397. if ok {
  1398. branding.FaviconPath = faviconPath
  1399. isSet = true
  1400. }
  1401. logoPath, ok := os.LookupEnv(fmt.Sprintf("%s__LOGO_PATH", prefix))
  1402. if ok {
  1403. branding.LogoPath = logoPath
  1404. isSet = true
  1405. }
  1406. loginImagePath, ok := os.LookupEnv(fmt.Sprintf("%s__LOGIN_IMAGE_PATH", prefix))
  1407. if ok {
  1408. branding.LoginImagePath = loginImagePath
  1409. isSet = true
  1410. }
  1411. disclaimerName, ok := os.LookupEnv(fmt.Sprintf("%s__DISCLAIMER_NAME", prefix))
  1412. if ok {
  1413. branding.DisclaimerName = disclaimerName
  1414. isSet = true
  1415. }
  1416. disclaimerPath, ok := os.LookupEnv(fmt.Sprintf("%s__DISCLAIMER_PATH", prefix))
  1417. if ok {
  1418. branding.DisclaimerPath = disclaimerPath
  1419. isSet = true
  1420. }
  1421. defaultCSSPath, ok := os.LookupEnv(fmt.Sprintf("%s__DEFAULT_CSS", prefix))
  1422. if ok {
  1423. branding.DefaultCSS = defaultCSSPath
  1424. isSet = true
  1425. }
  1426. extraCSS, ok := lookupStringListFromEnv(fmt.Sprintf("%s__EXTRA_CSS", prefix))
  1427. if ok {
  1428. branding.ExtraCSS = extraCSS
  1429. isSet = true
  1430. }
  1431. return branding, isSet
  1432. }
  1433. func getHTTPDBrandingFromEnv(idx int) (httpd.Branding, bool) {
  1434. result := defaultHTTPDBinding.Branding
  1435. if len(globalConf.HTTPDConfig.Bindings) > idx {
  1436. result = globalConf.HTTPDConfig.Bindings[idx].Branding
  1437. }
  1438. isSet := false
  1439. webAdmin, ok := getHTTPDUIBrandingFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__BRANDING__WEB_ADMIN", idx),
  1440. result.WebAdmin)
  1441. if ok {
  1442. result.WebAdmin = webAdmin
  1443. isSet = true
  1444. }
  1445. webClient, ok := getHTTPDUIBrandingFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__BRANDING__WEB_CLIENT", idx),
  1446. result.WebClient)
  1447. if ok {
  1448. result.WebClient = webClient
  1449. isSet = true
  1450. }
  1451. return result, isSet
  1452. }
  1453. func getHTTPDWebClientIntegrationsFromEnv(idx int) []httpd.WebClientIntegration {
  1454. var integrations []httpd.WebClientIntegration
  1455. if len(globalConf.HTTPDConfig.Bindings) > idx {
  1456. integrations = globalConf.HTTPDConfig.Bindings[idx].WebClientIntegrations
  1457. }
  1458. for subIdx := 0; subIdx < 10; subIdx++ {
  1459. var integration httpd.WebClientIntegration
  1460. var replace bool
  1461. if len(globalConf.HTTPDConfig.Bindings) > idx &&
  1462. len(globalConf.HTTPDConfig.Bindings[idx].WebClientIntegrations) > subIdx {
  1463. integration = integrations[subIdx]
  1464. replace = true
  1465. }
  1466. url, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__WEB_CLIENT_INTEGRATIONS__%v__URL", idx, subIdx))
  1467. if ok {
  1468. integration.URL = url
  1469. }
  1470. extensions, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__WEB_CLIENT_INTEGRATIONS__%v__FILE_EXTENSIONS",
  1471. idx, subIdx))
  1472. if ok {
  1473. integration.FileExtensions = extensions
  1474. }
  1475. if integration.URL != "" && len(integration.FileExtensions) > 0 {
  1476. if replace {
  1477. integrations[subIdx] = integration
  1478. } else {
  1479. integrations = append(integrations, integration)
  1480. }
  1481. }
  1482. }
  1483. return integrations
  1484. }
  1485. func getDefaultHTTPBinding(idx int) httpd.Binding {
  1486. binding := defaultHTTPDBinding
  1487. if len(globalConf.HTTPDConfig.Bindings) > idx {
  1488. binding = globalConf.HTTPDConfig.Bindings[idx]
  1489. }
  1490. return binding
  1491. }
  1492. func getHTTPDNestedObjectsFromEnv(idx int, binding *httpd.Binding) bool {
  1493. isSet := false
  1494. webClientIntegrations := getHTTPDWebClientIntegrationsFromEnv(idx)
  1495. if len(webClientIntegrations) > 0 {
  1496. binding.WebClientIntegrations = webClientIntegrations
  1497. isSet = true
  1498. }
  1499. oidc, ok := getHTTPDOIDCFromEnv(idx)
  1500. if ok {
  1501. binding.OIDC = oidc
  1502. isSet = true
  1503. }
  1504. securityConf, ok := getHTTPDSecurityConfFromEnv(idx)
  1505. if ok {
  1506. binding.Security = securityConf
  1507. isSet = true
  1508. }
  1509. brandingConf, ok := getHTTPDBrandingFromEnv(idx)
  1510. if ok {
  1511. binding.Branding = brandingConf
  1512. isSet = true
  1513. }
  1514. return isSet
  1515. }
  1516. func getHTTPDBindingProxyConfigsFromEnv(idx int, binding *httpd.Binding) bool {
  1517. isSet := false
  1518. proxyAllowed, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__PROXY_ALLOWED", idx))
  1519. if ok {
  1520. binding.ProxyAllowed = proxyAllowed
  1521. isSet = true
  1522. }
  1523. clientIPProxyHeader, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__CLIENT_IP_PROXY_HEADER", idx))
  1524. if ok {
  1525. binding.ClientIPProxyHeader = clientIPProxyHeader
  1526. isSet = true
  1527. }
  1528. clientIPHeaderDepth, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__CLIENT_IP_HEADER_DEPTH", idx))
  1529. if ok {
  1530. binding.ClientIPHeaderDepth = int(clientIPHeaderDepth)
  1531. isSet = true
  1532. }
  1533. return isSet
  1534. }
  1535. func getHTTPDBindingFromEnv(idx int) { //nolint:gocyclo
  1536. binding := getDefaultHTTPBinding(idx)
  1537. isSet := false
  1538. port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__PORT", idx))
  1539. if ok {
  1540. binding.Port = int(port)
  1541. isSet = true
  1542. }
  1543. address, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ADDRESS", idx))
  1544. if ok {
  1545. binding.Address = address
  1546. isSet = true
  1547. }
  1548. certificateFile, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__CERTIFICATE_FILE", idx))
  1549. if ok {
  1550. binding.CertificateFile = certificateFile
  1551. isSet = true
  1552. }
  1553. certificateKeyFile, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__CERTIFICATE_KEY_FILE", idx))
  1554. if ok {
  1555. binding.CertificateKeyFile = certificateKeyFile
  1556. isSet = true
  1557. }
  1558. enableWebAdmin, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ENABLE_WEB_ADMIN", idx))
  1559. if ok {
  1560. binding.EnableWebAdmin = enableWebAdmin
  1561. isSet = true
  1562. }
  1563. enableWebClient, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ENABLE_WEB_CLIENT", idx))
  1564. if ok {
  1565. binding.EnableWebClient = enableWebClient
  1566. isSet = true
  1567. }
  1568. enableRESTAPI, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ENABLE_REST_API", idx))
  1569. if ok {
  1570. binding.EnableRESTAPI = enableRESTAPI
  1571. isSet = true
  1572. }
  1573. enabledLoginMethods, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ENABLED_LOGIN_METHODS", idx))
  1574. if ok {
  1575. binding.EnabledLoginMethods = int(enabledLoginMethods)
  1576. isSet = true
  1577. }
  1578. renderOpenAPI, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__RENDER_OPENAPI", idx))
  1579. if ok {
  1580. binding.RenderOpenAPI = renderOpenAPI
  1581. isSet = true
  1582. }
  1583. enableHTTPS, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ENABLE_HTTPS", idx))
  1584. if ok {
  1585. binding.EnableHTTPS = enableHTTPS
  1586. isSet = true
  1587. }
  1588. tlsVer, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__MIN_TLS_VERSION", idx))
  1589. if ok {
  1590. binding.MinTLSVersion = int(tlsVer)
  1591. isSet = true
  1592. }
  1593. clientAuthType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__CLIENT_AUTH_TYPE", idx))
  1594. if ok {
  1595. binding.ClientAuthType = int(clientAuthType)
  1596. isSet = true
  1597. }
  1598. tlsCiphers, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__TLS_CIPHER_SUITES", idx))
  1599. if ok {
  1600. binding.TLSCipherSuites = tlsCiphers
  1601. isSet = true
  1602. }
  1603. if getHTTPDBindingProxyConfigsFromEnv(idx, &binding) {
  1604. isSet = true
  1605. }
  1606. hideLoginURL, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__HIDE_LOGIN_URL", idx))
  1607. if ok {
  1608. binding.HideLoginURL = int(hideLoginURL)
  1609. isSet = true
  1610. }
  1611. if getHTTPDNestedObjectsFromEnv(idx, &binding) {
  1612. isSet = true
  1613. }
  1614. setHTTPDBinding(isSet, binding, idx)
  1615. }
  1616. func setHTTPDBinding(isSet bool, binding httpd.Binding, idx int) {
  1617. if isSet {
  1618. if len(globalConf.HTTPDConfig.Bindings) > idx {
  1619. globalConf.HTTPDConfig.Bindings[idx] = binding
  1620. } else {
  1621. globalConf.HTTPDConfig.Bindings = append(globalConf.HTTPDConfig.Bindings, binding)
  1622. }
  1623. }
  1624. }
  1625. func getHTTPClientCertificatesFromEnv(idx int) {
  1626. tlsCert := httpclient.TLSKeyPair{}
  1627. if len(globalConf.HTTPConfig.Certificates) > idx {
  1628. tlsCert = globalConf.HTTPConfig.Certificates[idx]
  1629. }
  1630. cert, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__CERTIFICATES__%v__CERT", idx))
  1631. if ok {
  1632. tlsCert.Cert = cert
  1633. }
  1634. key, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__CERTIFICATES__%v__KEY", idx))
  1635. if ok {
  1636. tlsCert.Key = key
  1637. }
  1638. if tlsCert.Cert != "" && tlsCert.Key != "" {
  1639. if len(globalConf.HTTPConfig.Certificates) > idx {
  1640. globalConf.HTTPConfig.Certificates[idx] = tlsCert
  1641. } else {
  1642. globalConf.HTTPConfig.Certificates = append(globalConf.HTTPConfig.Certificates, tlsCert)
  1643. }
  1644. }
  1645. }
  1646. func getHTTPClientHeadersFromEnv(idx int) {
  1647. header := httpclient.Header{}
  1648. if len(globalConf.HTTPConfig.Headers) > idx {
  1649. header = globalConf.HTTPConfig.Headers[idx]
  1650. }
  1651. key, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__HEADERS__%v__KEY", idx))
  1652. if ok {
  1653. header.Key = key
  1654. }
  1655. value, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__HEADERS__%v__VALUE", idx))
  1656. if ok {
  1657. header.Value = value
  1658. }
  1659. url, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__HEADERS__%v__URL", idx))
  1660. if ok {
  1661. header.URL = url
  1662. }
  1663. if header.Key != "" && header.Value != "" {
  1664. if len(globalConf.HTTPConfig.Headers) > idx {
  1665. globalConf.HTTPConfig.Headers[idx] = header
  1666. } else {
  1667. globalConf.HTTPConfig.Headers = append(globalConf.HTTPConfig.Headers, header)
  1668. }
  1669. }
  1670. }
  1671. func getCommandConfigsFromEnv(idx int) {
  1672. cfg := command.Command{}
  1673. if len(globalConf.CommandConfig.Commands) > idx {
  1674. cfg = globalConf.CommandConfig.Commands[idx]
  1675. }
  1676. path, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_COMMAND__COMMANDS__%v__PATH", idx))
  1677. if ok {
  1678. cfg.Path = path
  1679. }
  1680. timeout, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMAND__COMMANDS__%v__TIMEOUT", idx))
  1681. if ok {
  1682. cfg.Timeout = int(timeout)
  1683. }
  1684. env, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_COMMAND__COMMANDS__%v__ENV", idx))
  1685. if ok {
  1686. cfg.Env = env
  1687. }
  1688. if cfg.Path != "" {
  1689. if len(globalConf.CommandConfig.Commands) > idx {
  1690. globalConf.CommandConfig.Commands[idx] = cfg
  1691. } else {
  1692. globalConf.CommandConfig.Commands = append(globalConf.CommandConfig.Commands, cfg)
  1693. }
  1694. }
  1695. }
  1696. func setViperDefaults() {
  1697. viper.SetDefault("common.idle_timeout", globalConf.Common.IdleTimeout)
  1698. viper.SetDefault("common.upload_mode", globalConf.Common.UploadMode)
  1699. viper.SetDefault("common.actions.execute_on", globalConf.Common.Actions.ExecuteOn)
  1700. viper.SetDefault("common.actions.execute_sync", globalConf.Common.Actions.ExecuteSync)
  1701. viper.SetDefault("common.actions.hook", globalConf.Common.Actions.Hook)
  1702. viper.SetDefault("common.setstat_mode", globalConf.Common.SetstatMode)
  1703. viper.SetDefault("common.rename_mode", globalConf.Common.RenameMode)
  1704. viper.SetDefault("common.temp_path", globalConf.Common.TempPath)
  1705. viper.SetDefault("common.proxy_protocol", globalConf.Common.ProxyProtocol)
  1706. viper.SetDefault("common.proxy_allowed", globalConf.Common.ProxyAllowed)
  1707. viper.SetDefault("common.post_connect_hook", globalConf.Common.PostConnectHook)
  1708. viper.SetDefault("common.post_disconnect_hook", globalConf.Common.PostDisconnectHook)
  1709. viper.SetDefault("common.data_retention_hook", globalConf.Common.DataRetentionHook)
  1710. viper.SetDefault("common.max_total_connections", globalConf.Common.MaxTotalConnections)
  1711. viper.SetDefault("common.max_per_host_connections", globalConf.Common.MaxPerHostConnections)
  1712. viper.SetDefault("common.allowlist_status", globalConf.Common.AllowListStatus)
  1713. viper.SetDefault("common.allow_self_connections", globalConf.Common.AllowSelfConnections)
  1714. viper.SetDefault("common.defender.enabled", globalConf.Common.DefenderConfig.Enabled)
  1715. viper.SetDefault("common.defender.driver", globalConf.Common.DefenderConfig.Driver)
  1716. viper.SetDefault("common.defender.ban_time", globalConf.Common.DefenderConfig.BanTime)
  1717. viper.SetDefault("common.defender.ban_time_increment", globalConf.Common.DefenderConfig.BanTimeIncrement)
  1718. viper.SetDefault("common.defender.threshold", globalConf.Common.DefenderConfig.Threshold)
  1719. viper.SetDefault("common.defender.score_invalid", globalConf.Common.DefenderConfig.ScoreInvalid)
  1720. viper.SetDefault("common.defender.score_valid", globalConf.Common.DefenderConfig.ScoreValid)
  1721. viper.SetDefault("common.defender.score_limit_exceeded", globalConf.Common.DefenderConfig.ScoreLimitExceeded)
  1722. viper.SetDefault("common.defender.score_no_auth", globalConf.Common.DefenderConfig.ScoreNoAuth)
  1723. viper.SetDefault("common.defender.observation_time", globalConf.Common.DefenderConfig.ObservationTime)
  1724. viper.SetDefault("common.defender.entries_soft_limit", globalConf.Common.DefenderConfig.EntriesSoftLimit)
  1725. viper.SetDefault("common.defender.entries_hard_limit", globalConf.Common.DefenderConfig.EntriesHardLimit)
  1726. viper.SetDefault("acme.email", globalConf.ACME.Email)
  1727. viper.SetDefault("acme.key_type", globalConf.ACME.KeyType)
  1728. viper.SetDefault("acme.certs_path", globalConf.ACME.CertsPath)
  1729. viper.SetDefault("acme.ca_endpoint", globalConf.ACME.CAEndpoint)
  1730. viper.SetDefault("acme.domains", globalConf.ACME.Domains)
  1731. viper.SetDefault("acme.renew_days", globalConf.ACME.RenewDays)
  1732. viper.SetDefault("acme.http01_challenge.port", globalConf.ACME.HTTP01Challenge.Port)
  1733. viper.SetDefault("acme.http01_challenge.webroot", globalConf.ACME.HTTP01Challenge.WebRoot)
  1734. viper.SetDefault("acme.http01_challenge.proxy_header", globalConf.ACME.HTTP01Challenge.ProxyHeader)
  1735. viper.SetDefault("acme.tls_alpn01_challenge.port", globalConf.ACME.TLSALPN01Challenge.Port)
  1736. viper.SetDefault("sftpd.max_auth_tries", globalConf.SFTPD.MaxAuthTries)
  1737. viper.SetDefault("sftpd.banner", globalConf.SFTPD.Banner)
  1738. viper.SetDefault("sftpd.host_keys", globalConf.SFTPD.HostKeys)
  1739. viper.SetDefault("sftpd.host_certificates", globalConf.SFTPD.HostCertificates)
  1740. viper.SetDefault("sftpd.host_key_algorithms", globalConf.SFTPD.HostKeyAlgorithms)
  1741. viper.SetDefault("sftpd.moduli", globalConf.SFTPD.Moduli)
  1742. viper.SetDefault("sftpd.kex_algorithms", globalConf.SFTPD.KexAlgorithms)
  1743. viper.SetDefault("sftpd.ciphers", globalConf.SFTPD.Ciphers)
  1744. viper.SetDefault("sftpd.macs", globalConf.SFTPD.MACs)
  1745. viper.SetDefault("sftpd.trusted_user_ca_keys", globalConf.SFTPD.TrustedUserCAKeys)
  1746. viper.SetDefault("sftpd.revoked_user_certs_file", globalConf.SFTPD.RevokedUserCertsFile)
  1747. viper.SetDefault("sftpd.login_banner_file", globalConf.SFTPD.LoginBannerFile)
  1748. viper.SetDefault("sftpd.enabled_ssh_commands", sftpd.GetDefaultSSHCommands())
  1749. viper.SetDefault("sftpd.keyboard_interactive_authentication", globalConf.SFTPD.KeyboardInteractiveAuthentication)
  1750. viper.SetDefault("sftpd.keyboard_interactive_auth_hook", globalConf.SFTPD.KeyboardInteractiveHook)
  1751. viper.SetDefault("sftpd.password_authentication", globalConf.SFTPD.PasswordAuthentication)
  1752. viper.SetDefault("sftpd.folder_prefix", globalConf.SFTPD.FolderPrefix)
  1753. viper.SetDefault("ftpd.banner", globalConf.FTPD.Banner)
  1754. viper.SetDefault("ftpd.banner_file", globalConf.FTPD.BannerFile)
  1755. viper.SetDefault("ftpd.active_transfers_port_non_20", globalConf.FTPD.ActiveTransfersPortNon20)
  1756. viper.SetDefault("ftpd.passive_port_range.start", globalConf.FTPD.PassivePortRange.Start)
  1757. viper.SetDefault("ftpd.passive_port_range.end", globalConf.FTPD.PassivePortRange.End)
  1758. viper.SetDefault("ftpd.disable_active_mode", globalConf.FTPD.DisableActiveMode)
  1759. viper.SetDefault("ftpd.enable_site", globalConf.FTPD.EnableSite)
  1760. viper.SetDefault("ftpd.hash_support", globalConf.FTPD.HASHSupport)
  1761. viper.SetDefault("ftpd.combine_support", globalConf.FTPD.CombineSupport)
  1762. viper.SetDefault("ftpd.certificate_file", globalConf.FTPD.CertificateFile)
  1763. viper.SetDefault("ftpd.certificate_key_file", globalConf.FTPD.CertificateKeyFile)
  1764. viper.SetDefault("ftpd.ca_certificates", globalConf.FTPD.CACertificates)
  1765. viper.SetDefault("ftpd.ca_revocation_lists", globalConf.FTPD.CARevocationLists)
  1766. viper.SetDefault("webdavd.certificate_file", globalConf.WebDAVD.CertificateFile)
  1767. viper.SetDefault("webdavd.certificate_key_file", globalConf.WebDAVD.CertificateKeyFile)
  1768. viper.SetDefault("webdavd.ca_certificates", globalConf.WebDAVD.CACertificates)
  1769. viper.SetDefault("webdavd.ca_revocation_lists", globalConf.WebDAVD.CARevocationLists)
  1770. viper.SetDefault("webdavd.cors.enabled", globalConf.WebDAVD.Cors.Enabled)
  1771. viper.SetDefault("webdavd.cors.allowed_origins", globalConf.WebDAVD.Cors.AllowedOrigins)
  1772. viper.SetDefault("webdavd.cors.allowed_methods", globalConf.WebDAVD.Cors.AllowedMethods)
  1773. viper.SetDefault("webdavd.cors.allowed_headers", globalConf.WebDAVD.Cors.AllowedHeaders)
  1774. viper.SetDefault("webdavd.cors.exposed_headers", globalConf.WebDAVD.Cors.ExposedHeaders)
  1775. viper.SetDefault("webdavd.cors.allow_credentials", globalConf.WebDAVD.Cors.AllowCredentials)
  1776. viper.SetDefault("webdavd.cors.options_passthrough", globalConf.WebDAVD.Cors.OptionsPassthrough)
  1777. viper.SetDefault("webdavd.cors.options_success_status", globalConf.WebDAVD.Cors.OptionsSuccessStatus)
  1778. viper.SetDefault("webdavd.cors.allow_private_network", globalConf.WebDAVD.Cors.AllowPrivateNetwork)
  1779. viper.SetDefault("webdavd.cors.max_age", globalConf.WebDAVD.Cors.MaxAge)
  1780. viper.SetDefault("webdavd.cache.users.expiration_time", globalConf.WebDAVD.Cache.Users.ExpirationTime)
  1781. viper.SetDefault("webdavd.cache.users.max_size", globalConf.WebDAVD.Cache.Users.MaxSize)
  1782. viper.SetDefault("webdavd.cache.mime_types.enabled", globalConf.WebDAVD.Cache.MimeTypes.Enabled)
  1783. viper.SetDefault("webdavd.cache.mime_types.max_size", globalConf.WebDAVD.Cache.MimeTypes.MaxSize)
  1784. viper.SetDefault("webdavd.cache.mime_types.custom_mappings", globalConf.WebDAVD.Cache.MimeTypes.CustomMappings)
  1785. viper.SetDefault("data_provider.driver", globalConf.ProviderConf.Driver)
  1786. viper.SetDefault("data_provider.name", globalConf.ProviderConf.Name)
  1787. viper.SetDefault("data_provider.host", globalConf.ProviderConf.Host)
  1788. viper.SetDefault("data_provider.port", globalConf.ProviderConf.Port)
  1789. viper.SetDefault("data_provider.username", globalConf.ProviderConf.Username)
  1790. viper.SetDefault("data_provider.password", globalConf.ProviderConf.Password)
  1791. viper.SetDefault("data_provider.sslmode", globalConf.ProviderConf.SSLMode)
  1792. viper.SetDefault("data_provider.disable_sni", globalConf.ProviderConf.DisableSNI)
  1793. viper.SetDefault("data_provider.target_session_attrs", globalConf.ProviderConf.TargetSessionAttrs)
  1794. viper.SetDefault("data_provider.root_cert", globalConf.ProviderConf.RootCert)
  1795. viper.SetDefault("data_provider.client_cert", globalConf.ProviderConf.ClientCert)
  1796. viper.SetDefault("data_provider.client_key", globalConf.ProviderConf.ClientKey)
  1797. viper.SetDefault("data_provider.connection_string", globalConf.ProviderConf.ConnectionString)
  1798. viper.SetDefault("data_provider.sql_tables_prefix", globalConf.ProviderConf.SQLTablesPrefix)
  1799. viper.SetDefault("data_provider.track_quota", globalConf.ProviderConf.TrackQuota)
  1800. viper.SetDefault("data_provider.pool_size", globalConf.ProviderConf.PoolSize)
  1801. viper.SetDefault("data_provider.users_base_dir", globalConf.ProviderConf.UsersBaseDir)
  1802. viper.SetDefault("data_provider.actions.execute_on", globalConf.ProviderConf.Actions.ExecuteOn)
  1803. viper.SetDefault("data_provider.actions.execute_for", globalConf.ProviderConf.Actions.ExecuteFor)
  1804. viper.SetDefault("data_provider.actions.hook", globalConf.ProviderConf.Actions.Hook)
  1805. viper.SetDefault("data_provider.external_auth_hook", globalConf.ProviderConf.ExternalAuthHook)
  1806. viper.SetDefault("data_provider.external_auth_scope", globalConf.ProviderConf.ExternalAuthScope)
  1807. viper.SetDefault("data_provider.pre_login_hook", globalConf.ProviderConf.PreLoginHook)
  1808. viper.SetDefault("data_provider.post_login_hook", globalConf.ProviderConf.PostLoginHook)
  1809. viper.SetDefault("data_provider.post_login_scope", globalConf.ProviderConf.PostLoginScope)
  1810. viper.SetDefault("data_provider.check_password_hook", globalConf.ProviderConf.CheckPasswordHook)
  1811. viper.SetDefault("data_provider.check_password_scope", globalConf.ProviderConf.CheckPasswordScope)
  1812. viper.SetDefault("data_provider.password_hashing.bcrypt_options.cost", globalConf.ProviderConf.PasswordHashing.BcryptOptions.Cost)
  1813. viper.SetDefault("data_provider.password_hashing.argon2_options.memory", globalConf.ProviderConf.PasswordHashing.Argon2Options.Memory)
  1814. viper.SetDefault("data_provider.password_hashing.argon2_options.iterations", globalConf.ProviderConf.PasswordHashing.Argon2Options.Iterations)
  1815. viper.SetDefault("data_provider.password_hashing.argon2_options.parallelism", globalConf.ProviderConf.PasswordHashing.Argon2Options.Parallelism)
  1816. viper.SetDefault("data_provider.password_hashing.algo", globalConf.ProviderConf.PasswordHashing.Algo)
  1817. viper.SetDefault("data_provider.password_validation.admins.min_entropy", globalConf.ProviderConf.PasswordValidation.Admins.MinEntropy)
  1818. viper.SetDefault("data_provider.password_validation.users.min_entropy", globalConf.ProviderConf.PasswordValidation.Users.MinEntropy)
  1819. viper.SetDefault("data_provider.password_caching", globalConf.ProviderConf.PasswordCaching)
  1820. viper.SetDefault("data_provider.update_mode", globalConf.ProviderConf.UpdateMode)
  1821. viper.SetDefault("data_provider.delayed_quota_update", globalConf.ProviderConf.DelayedQuotaUpdate)
  1822. viper.SetDefault("data_provider.create_default_admin", globalConf.ProviderConf.CreateDefaultAdmin)
  1823. viper.SetDefault("data_provider.naming_rules", globalConf.ProviderConf.NamingRules)
  1824. viper.SetDefault("data_provider.is_shared", globalConf.ProviderConf.IsShared)
  1825. viper.SetDefault("data_provider.node.host", globalConf.ProviderConf.Node.Host)
  1826. viper.SetDefault("data_provider.node.port", globalConf.ProviderConf.Node.Port)
  1827. viper.SetDefault("data_provider.node.proto", globalConf.ProviderConf.Node.Proto)
  1828. viper.SetDefault("data_provider.backups_path", globalConf.ProviderConf.BackupsPath)
  1829. viper.SetDefault("httpd.templates_path", globalConf.HTTPDConfig.TemplatesPath)
  1830. viper.SetDefault("httpd.static_files_path", globalConf.HTTPDConfig.StaticFilesPath)
  1831. viper.SetDefault("httpd.openapi_path", globalConf.HTTPDConfig.OpenAPIPath)
  1832. viper.SetDefault("httpd.web_root", globalConf.HTTPDConfig.WebRoot)
  1833. viper.SetDefault("httpd.certificate_file", globalConf.HTTPDConfig.CertificateFile)
  1834. viper.SetDefault("httpd.certificate_key_file", globalConf.HTTPDConfig.CertificateKeyFile)
  1835. viper.SetDefault("httpd.ca_certificates", globalConf.HTTPDConfig.CACertificates)
  1836. viper.SetDefault("httpd.ca_revocation_lists", globalConf.HTTPDConfig.CARevocationLists)
  1837. viper.SetDefault("httpd.signing_passphrase", globalConf.HTTPDConfig.SigningPassphrase)
  1838. viper.SetDefault("httpd.token_validation", globalConf.HTTPDConfig.TokenValidation)
  1839. viper.SetDefault("httpd.max_upload_file_size", globalConf.HTTPDConfig.MaxUploadFileSize)
  1840. viper.SetDefault("httpd.cors.enabled", globalConf.HTTPDConfig.Cors.Enabled)
  1841. viper.SetDefault("httpd.cors.allowed_origins", globalConf.HTTPDConfig.Cors.AllowedOrigins)
  1842. viper.SetDefault("httpd.cors.allowed_methods", globalConf.HTTPDConfig.Cors.AllowedMethods)
  1843. viper.SetDefault("httpd.cors.allowed_headers", globalConf.HTTPDConfig.Cors.AllowedHeaders)
  1844. viper.SetDefault("httpd.cors.exposed_headers", globalConf.HTTPDConfig.Cors.ExposedHeaders)
  1845. viper.SetDefault("httpd.cors.allow_credentials", globalConf.HTTPDConfig.Cors.AllowCredentials)
  1846. viper.SetDefault("httpd.cors.max_age", globalConf.HTTPDConfig.Cors.MaxAge)
  1847. viper.SetDefault("httpd.cors.options_passthrough", globalConf.HTTPDConfig.Cors.OptionsPassthrough)
  1848. viper.SetDefault("httpd.cors.options_success_status", globalConf.HTTPDConfig.Cors.OptionsSuccessStatus)
  1849. viper.SetDefault("httpd.cors.allow_private_network", globalConf.HTTPDConfig.Cors.AllowPrivateNetwork)
  1850. viper.SetDefault("httpd.setup.installation_code", globalConf.HTTPDConfig.Setup.InstallationCode)
  1851. viper.SetDefault("httpd.setup.installation_code_hint", globalConf.HTTPDConfig.Setup.InstallationCodeHint)
  1852. viper.SetDefault("httpd.hide_support_link", globalConf.HTTPDConfig.HideSupportLink)
  1853. viper.SetDefault("http.timeout", globalConf.HTTPConfig.Timeout)
  1854. viper.SetDefault("http.retry_wait_min", globalConf.HTTPConfig.RetryWaitMin)
  1855. viper.SetDefault("http.retry_wait_max", globalConf.HTTPConfig.RetryWaitMax)
  1856. viper.SetDefault("http.retry_max", globalConf.HTTPConfig.RetryMax)
  1857. viper.SetDefault("http.ca_certificates", globalConf.HTTPConfig.CACertificates)
  1858. viper.SetDefault("http.skip_tls_verify", globalConf.HTTPConfig.SkipTLSVerify)
  1859. viper.SetDefault("command.timeout", globalConf.CommandConfig.Timeout)
  1860. viper.SetDefault("command.env", globalConf.CommandConfig.Env)
  1861. viper.SetDefault("kms.secrets.url", globalConf.KMSConfig.Secrets.URL)
  1862. viper.SetDefault("kms.secrets.master_key", globalConf.KMSConfig.Secrets.MasterKeyString)
  1863. viper.SetDefault("kms.secrets.master_key_path", globalConf.KMSConfig.Secrets.MasterKeyPath)
  1864. viper.SetDefault("telemetry.bind_port", globalConf.TelemetryConfig.BindPort)
  1865. viper.SetDefault("telemetry.bind_address", globalConf.TelemetryConfig.BindAddress)
  1866. viper.SetDefault("telemetry.enable_profiler", globalConf.TelemetryConfig.EnableProfiler)
  1867. viper.SetDefault("telemetry.auth_user_file", globalConf.TelemetryConfig.AuthUserFile)
  1868. viper.SetDefault("telemetry.certificate_file", globalConf.TelemetryConfig.CertificateFile)
  1869. viper.SetDefault("telemetry.certificate_key_file", globalConf.TelemetryConfig.CertificateKeyFile)
  1870. viper.SetDefault("telemetry.min_tls_version", globalConf.TelemetryConfig.MinTLSVersion)
  1871. viper.SetDefault("telemetry.tls_cipher_suites", globalConf.TelemetryConfig.TLSCipherSuites)
  1872. viper.SetDefault("smtp.host", globalConf.SMTPConfig.Host)
  1873. viper.SetDefault("smtp.port", globalConf.SMTPConfig.Port)
  1874. viper.SetDefault("smtp.from", globalConf.SMTPConfig.From)
  1875. viper.SetDefault("smtp.user", globalConf.SMTPConfig.User)
  1876. viper.SetDefault("smtp.password", globalConf.SMTPConfig.Password)
  1877. viper.SetDefault("smtp.auth_type", globalConf.SMTPConfig.AuthType)
  1878. viper.SetDefault("smtp.encryption", globalConf.SMTPConfig.Encryption)
  1879. viper.SetDefault("smtp.domain", globalConf.SMTPConfig.Domain)
  1880. viper.SetDefault("smtp.templates_path", globalConf.SMTPConfig.TemplatesPath)
  1881. }
  1882. func lookupBoolFromEnv(envName string) (bool, bool) {
  1883. value, ok := os.LookupEnv(envName)
  1884. if ok {
  1885. converted, err := strconv.ParseBool(strings.TrimSpace(value))
  1886. if err == nil {
  1887. return converted, ok
  1888. }
  1889. }
  1890. return false, false
  1891. }
  1892. func lookupIntFromEnv(envName string) (int64, bool) {
  1893. value, ok := os.LookupEnv(envName)
  1894. if ok {
  1895. converted, err := strconv.ParseInt(strings.TrimSpace(value), 10, 64)
  1896. if err == nil {
  1897. return converted, ok
  1898. }
  1899. }
  1900. return 0, false
  1901. }
  1902. func lookupStringListFromEnv(envName string) ([]string, bool) {
  1903. value, ok := os.LookupEnv(envName)
  1904. if ok {
  1905. var result []string
  1906. for _, v := range strings.Split(value, ",") {
  1907. val := strings.TrimSpace(v)
  1908. if val != "" {
  1909. result = append(result, val)
  1910. }
  1911. }
  1912. return result, true
  1913. }
  1914. return nil, false
  1915. }