config.go 73 KB

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