config.go 74 KB

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