config.go 73 KB

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