config.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package config
  26. import (
  27. "log"
  28. "strings"
  29. "sync"
  30. "time"
  31. utils "git.semlanik.org/semlanik/gostfix/utils"
  32. ini "gopkg.in/go-ini/ini.v1"
  33. )
  34. const configPath = "data/main.ini"
  35. const (
  36. KeyWebPort = "web_port"
  37. KeySASLPort = "sasl_port"
  38. KeyPostfixConfig = "postfix_config"
  39. KeyMongoAddress = "mongo_address"
  40. KeyMongoUser = "mongo_user"
  41. KeyMongoPassword = "mongo_password"
  42. KeyAttachmentsPath = "attachments_path"
  43. KeyAttachmentsUser = "attachments_user"
  44. KeyAttachmentsPassword = "attachments_password"
  45. KeyRegistrationEnabled = "registration_enabled"
  46. )
  47. const (
  48. SetupSection = "intial_setup"
  49. SetupKeyEnabled = "enabled"
  50. SetupKeyPassword = "password"
  51. )
  52. const (
  53. WebSection = "web"
  54. WebKeySessionExpireTime = "session_expire_time"
  55. )
  56. const (
  57. PostfixKeyMyDomain = "mydomain"
  58. PostfixKeyVirtualMailboxMaps = "virtual_mailbox_maps"
  59. PostfixKeyVirtualMailboxBase = "virtual_mailbox_base"
  60. PostfixKeyVirtualMailboxDomains = "virtual_mailbox_domains"
  61. )
  62. type GostfixConfig gostfixConfig
  63. var (
  64. once sync.Once
  65. instance *gostfixConfig
  66. )
  67. func ConfigInstance() *GostfixConfig {
  68. once.Do(func() {
  69. instance, _ = newConfig()
  70. })
  71. return (*GostfixConfig)(instance)
  72. }
  73. type gostfixConfig struct {
  74. WebPort string
  75. SASLPort string
  76. MyDomain string
  77. VMailboxMaps string
  78. VMailboxBase string
  79. VMailboxDomains []string
  80. MongoUser string
  81. MongoPassword string
  82. MongoAddress string
  83. AttachmentsPath string
  84. RegistrationEnabled bool
  85. WebSessionExpireTime time.Duration
  86. SetupEnabled bool
  87. SetupPassword string
  88. }
  89. func newConfig() (config *gostfixConfig, err error) {
  90. cfg, err := ini.Load(configPath)
  91. if err != nil {
  92. log.Fatalf("Unable to load %s\n", configPath)
  93. return
  94. }
  95. webPort := cfg.Section("").Key(KeyWebPort).String()
  96. if webPort == "" {
  97. log.Printf("Web server port is not specified in configuration file, use default 65200")
  98. webPort = "65200"
  99. }
  100. initialSetup, _ := cfg.Section(SetupSection).Key(SetupKeyEnabled).Bool()
  101. initialPassword := cfg.Section(SetupSection).Key(SetupKeyPassword).String()
  102. if initialSetup {
  103. if len(initialPassword) < 8 {
  104. log.Fatalf("Initial setup requires a temporary master password in the configuration file. The minimum lenght is 8 symbols.")
  105. return
  106. }
  107. }
  108. postfixConfigPath := cfg.Section("").Key(KeyPostfixConfig).String()
  109. if !utils.FileExists(postfixConfigPath) {
  110. log.Fatalf("Unable to find postfix config %s\n", postfixConfigPath)
  111. return
  112. }
  113. postfixCfg, err := ini.Load(postfixConfigPath)
  114. if err != nil {
  115. log.Fatalf("Unable to load %s: %s\n", postfixConfigPath, err)
  116. return
  117. }
  118. baseDir := postfixCfg.Section("").Key(PostfixKeyVirtualMailboxBase).String()
  119. if !utils.DirectoryExists(baseDir) {
  120. log.Fatalf("Base dir %s doesn't exist, postfix is not configured proper way, check %s in %s\n", baseDir, PostfixKeyVirtualMailboxBase, postfixConfigPath)
  121. return
  122. }
  123. maps := postfixCfg.Section("").Key(PostfixKeyVirtualMailboxMaps).String()
  124. mapsList := strings.Split(maps, ":")
  125. if len(mapsList) != 2 || mapsList[0] != "hash" {
  126. log.Fatalf("%s is not set proper way in %s. Should be hash:<path/to/virtualmailbox/map>, but %s provided\n", PostfixKeyVirtualMailboxMaps, postfixConfigPath, maps)
  127. return
  128. }
  129. if !utils.FileExists(mapsList[1] + ".db") {
  130. log.Fatalf("Virtual mailbox map %s doesn't exist, postfix is not configured proper way, check %s in %s\n", mapsList[1], PostfixKeyVirtualMailboxMaps, postfixConfigPath)
  131. return
  132. }
  133. domains := postfixCfg.Section("").Key(PostfixKeyVirtualMailboxDomains).String()
  134. domainsList := strings.Split(domains, " ")
  135. var validDomains []string
  136. for _, domain := range domainsList {
  137. if utils.RegExpUtilsInstance().DomainChecker.MatchString(domain) {
  138. validDomains = append(validDomains, domain)
  139. }
  140. }
  141. if len(validDomains) <= 0 {
  142. log.Fatalf("Virtual mailbox domains %s are not configured proper way, check %s in %s\n", domains, PostfixKeyVirtualMailboxDomains, postfixConfigPath)
  143. return
  144. }
  145. myDomain := postfixCfg.Section("").Key(PostfixKeyMyDomain).String()
  146. if len(myDomain) <= 0 {
  147. myDomain = "localhost"
  148. }
  149. mongoUser := cfg.Section("").Key(KeyMongoUser).String()
  150. mongoPassword := cfg.Section("").Key(KeyMongoPassword).String()
  151. mongoAddress := cfg.Section("").Key(KeyMongoAddress).String()
  152. if mongoAddress == "" {
  153. mongoAddress = "localhost:27017"
  154. }
  155. attachmentsPath := cfg.Section("").Key(KeyAttachmentsPath).String()
  156. if attachmentsPath == "" {
  157. attachmentsPath = "attachments"
  158. }
  159. registrationEnabled := cfg.Section("").Key(KeyRegistrationEnabled).String()
  160. saslPort := cfg.Section("").Key(KeySASLPort).String()
  161. if saslPort == "" {
  162. log.Printf("SASL server port is not specified in configuration file, use default 65201")
  163. saslPort = "65201"
  164. }
  165. webSessionExpireTime, err := time.ParseDuration(cfg.Section(WebSection).Key(WebKeySessionExpireTime).String())
  166. if err != nil {
  167. webSessionExpireTime = time.Hour * 24
  168. log.Printf("Unable to read web session expire time. 24h by default.");
  169. }
  170. config = &gostfixConfig{
  171. WebPort: webPort,
  172. SASLPort: saslPort,
  173. MyDomain: myDomain,
  174. VMailboxBase: baseDir,
  175. VMailboxMaps: mapsList[1] + ".db",
  176. VMailboxDomains: validDomains,
  177. MongoUser: mongoUser,
  178. MongoPassword: mongoPassword,
  179. MongoAddress: mongoAddress,
  180. AttachmentsPath: attachmentsPath,
  181. RegistrationEnabled: registrationEnabled == "true",
  182. WebSessionExpireTime: webSessionExpireTime * 1000,
  183. SetupEnabled: initialSetup,
  184. SetupPassword: initialPassword,
  185. }
  186. return
  187. }