config.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // TODO: Trigger initial setup cycle instead of throwing an error
  104. // if !utils.FileExists(mapsList[1] + ".db") {
  105. // log.Fatalf("Virtual mailbox map %s doesn't exist, postfix is not configured proper way, check %s in %s\n", mapsList[1], PostfixKeyVirtualMailboxMaps, postfixConfigPath)
  106. // return
  107. // }
  108. domains := postfixCfg.Section("").Key(PostfixKeyVirtualMailboxDomains).String()
  109. domainsList := strings.Split(domains, " ")
  110. var validDomains []string
  111. for _, domain := range domainsList {
  112. if utils.RegExpUtilsInstance().DomainChecker.MatchString(domain) {
  113. validDomains = append(validDomains, domain)
  114. }
  115. }
  116. if len(validDomains) <= 0 {
  117. log.Fatalf("Virtual mailbox domains %s are not configured proper way, check %s in %s\n", domains, PostfixKeyVirtualMailboxDomains, postfixConfigPath)
  118. return
  119. }
  120. myDomain := postfixCfg.Section("").Key(PostfixKeyMyDomain).String()
  121. if len(myDomain) <= 0 {
  122. myDomain = "localhost"
  123. }
  124. mongoUser := cfg.Section("").Key(KeyMongoUser).String()
  125. mongoPassword := cfg.Section("").Key(KeyMongoPassword).String()
  126. mongoAddress := cfg.Section("").Key(KeyMongoAddress).String()
  127. if mongoAddress == "" {
  128. mongoAddress = "localhost:27017"
  129. }
  130. attachmentsPath := cfg.Section("").Key(KeyAttachmentsPath).String()
  131. if attachmentsPath == "" {
  132. attachmentsPath = "attachments"
  133. }
  134. registrationEnabled := cfg.Section("").Key(KeyRegistrationEnabled).String()
  135. webPort := cfg.Section("").Key(KeyWebPort).String()
  136. if webPort == "" {
  137. log.Printf("Web server port is not specified in configuration file, use default 65200")
  138. webPort = "65200"
  139. }
  140. saslPort := cfg.Section("").Key(KeySASLPort).String()
  141. if saslPort == "" {
  142. log.Printf("SASL server port is not specified in configuration file, use default 65201")
  143. saslPort = "65201"
  144. }
  145. config = &gostfixConfig{
  146. WebPort: webPort,
  147. SASLPort: saslPort,
  148. MyDomain: myDomain,
  149. VMailboxBase: baseDir,
  150. VMailboxMaps: mapsList[1] + ".db",
  151. VMailboxDomains: validDomains,
  152. MongoUser: mongoUser,
  153. MongoPassword: mongoPassword,
  154. MongoAddress: mongoAddress,
  155. AttachmentsPath: attachmentsPath,
  156. RegistrationEnabled: registrationEnabled == "true",
  157. }
  158. return
  159. }