config.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. utils "git.semlanik.org/semlanik/gostfix/utils"
  31. ini "gopkg.in/go-ini/ini.v1"
  32. )
  33. const configPath = "data/main.ini"
  34. const (
  35. KeyWebPort = "web_port"
  36. KeySASLPort = "sasl_port"
  37. KeyPostfixConfig = "postfix_config"
  38. KeyMongoAddress = "mongo_address"
  39. KeyMongoUser = "mongo_user"
  40. KeyMongoPassword = "mongo_password"
  41. KeyAttachmentsPath = "attachments_path"
  42. KeyAttachmentsUser = "attachments_user"
  43. KeyAttachmentsPassword = "attachments_password"
  44. KeyRegistrationEnabled = "registration_enabled"
  45. )
  46. const (
  47. PostfixKeyMyDomain = "mydomain"
  48. PostfixKeyVirtualMailboxMaps = "virtual_mailbox_maps"
  49. PostfixKeyVirtualMailboxBase = "virtual_mailbox_base"
  50. PostfixKeyVirtualMailboxDomains = "virtual_mailbox_domains"
  51. )
  52. type GostfixConfig gostfixConfig
  53. var (
  54. once sync.Once
  55. instance *gostfixConfig
  56. )
  57. func ConfigInstance() *GostfixConfig {
  58. once.Do(func() {
  59. instance, _ = newConfig()
  60. })
  61. return (*GostfixConfig)(instance)
  62. }
  63. type gostfixConfig struct {
  64. WebPort string
  65. SASLPort string
  66. MyDomain string
  67. VMailboxMaps string
  68. VMailboxBase string
  69. VMailboxDomains []string
  70. MongoUser string
  71. MongoPassword string
  72. MongoAddress string
  73. AttachmentsPath string
  74. RegistrationEnabled bool
  75. }
  76. func newConfig() (config *gostfixConfig, err error) {
  77. cfg, err := ini.Load(configPath)
  78. if err != nil {
  79. log.Fatalf("Unable to load %s\n", configPath)
  80. return
  81. }
  82. postfixConfigPath := cfg.Section("").Key(KeyPostfixConfig).String()
  83. if !utils.FileExists(postfixConfigPath) {
  84. log.Fatalf("Unable to find postfix config %s\n", postfixConfigPath)
  85. return
  86. }
  87. postfixCfg, err := ini.Load(postfixConfigPath)
  88. if err != nil {
  89. log.Fatalf("Unable to load %s: %s\n", postfixConfigPath, err)
  90. return
  91. }
  92. baseDir := postfixCfg.Section("").Key(PostfixKeyVirtualMailboxBase).String()
  93. if !utils.DirectoryExists(baseDir) {
  94. log.Fatalf("Base dir %s doesn't exist, postfix is not configured proper way, check %s in %s\n", baseDir, PostfixKeyVirtualMailboxBase, postfixConfigPath)
  95. return
  96. }
  97. maps := postfixCfg.Section("").Key(PostfixKeyVirtualMailboxMaps).String()
  98. mapsList := strings.Split(maps, ":")
  99. if len(mapsList) != 2 || mapsList[0] != "hash" {
  100. log.Fatalf("%s is not set proper way in %s. Should be hash:<path/to/virtualmailbox/map>, but %s provided\n", PostfixKeyVirtualMailboxMaps, postfixConfigPath, maps)
  101. return
  102. }
  103. if !utils.FileExists(mapsList[1] + ".db") {
  104. log.Fatalf("Virtual mailbox map %s doesn't exist, postfix is not configured proper way, check %s in %s\n", mapsList[1], PostfixKeyVirtualMailboxMaps, postfixConfigPath)
  105. return
  106. }
  107. domains := postfixCfg.Section("").Key(PostfixKeyVirtualMailboxDomains).String()
  108. domainsList := strings.Split(domains, " ")
  109. var validDomains []string
  110. for _, domain := range domainsList {
  111. if utils.RegExpUtilsInstance().DomainChecker.MatchString(domain) {
  112. validDomains = append(validDomains, domain)
  113. }
  114. }
  115. if len(validDomains) <= 0 {
  116. log.Fatalf("Virtual mailbox domains %s are not configured proper way, check %s in %s\n", domains, PostfixKeyVirtualMailboxDomains, postfixConfigPath)
  117. return
  118. }
  119. myDomain := postfixCfg.Section("").Key(PostfixKeyMyDomain).String()
  120. if len(myDomain) <= 0 {
  121. myDomain = "localhost"
  122. }
  123. mongoUser := cfg.Section("").Key(KeyMongoUser).String()
  124. mongoPassword := cfg.Section("").Key(KeyMongoPassword).String()
  125. mongoAddress := cfg.Section("").Key(KeyMongoAddress).String()
  126. if mongoAddress == "" {
  127. mongoAddress = "localhost:27017"
  128. }
  129. attachmentsPath := cfg.Section("").Key(KeyAttachmentsPath).String()
  130. if attachmentsPath == "" {
  131. attachmentsPath = "attachments"
  132. }
  133. registrationEnabled := cfg.Section("").Key(KeyRegistrationEnabled).String()
  134. webPort := cfg.Section("").Key(KeyWebPort).String()
  135. if webPort == "" {
  136. webPort = "65200"
  137. }
  138. saslPort := cfg.Section("").Key(KeySASLPort).String()
  139. if saslPort == "" {
  140. saslPort = "65201"
  141. }
  142. config = &gostfixConfig{
  143. WebPort: webPort,
  144. SASLPort: saslPort,
  145. MyDomain: myDomain,
  146. VMailboxBase: baseDir,
  147. VMailboxMaps: mapsList[1] + ".db",
  148. VMailboxDomains: validDomains,
  149. MongoUser: mongoUser,
  150. MongoPassword: mongoPassword,
  151. MongoAddress: mongoAddress,
  152. AttachmentsPath: attachmentsPath,
  153. RegistrationEnabled: registrationEnabled == "true",
  154. }
  155. return
  156. }