You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright (c) 2012-2014 Jeremy Latt
  2. // Copyright (c) 2014-2015 Edmund Huber
  3. // Copyright (c) 2016- Daniel Oaks <daniel@danieloaks.net>
  4. // released under the MIT license
  5. package irc
  6. import (
  7. "crypto/tls"
  8. "errors"
  9. "io/ioutil"
  10. "log"
  11. "gopkg.in/yaml.v2"
  12. )
  13. type PassConfig struct {
  14. Password string
  15. }
  16. // TLSListenConfig defines configuration options for listening on TLS
  17. type TLSListenConfig struct {
  18. Cert string
  19. Key string
  20. }
  21. // Certificate returns the TLS certificate assicated with this TLSListenConfig
  22. func (conf *TLSListenConfig) Config() (*tls.Config, error) {
  23. cert, err := tls.LoadX509KeyPair(conf.Cert, conf.Key)
  24. if err != nil {
  25. return nil, errors.New("tls cert+key: invalid pair")
  26. }
  27. return &tls.Config{
  28. Certificates: []tls.Certificate{cert},
  29. }, err
  30. }
  31. func (conf *PassConfig) PasswordBytes() []byte {
  32. bytes, err := DecodePasswordHash(conf.Password)
  33. if err != nil {
  34. log.Fatal("decode password error: ", err)
  35. }
  36. return bytes
  37. }
  38. type AccountRegistrationConfig struct {
  39. Enabled bool
  40. EnabledCallbacks []string `yaml:"enabled-callbacks"`
  41. Callbacks struct {
  42. Mailto struct {
  43. Server string
  44. Port int
  45. TLS struct {
  46. Enabled bool
  47. InsecureSkipVerify bool `yaml:"insecure_skip_verify"`
  48. ServerName string `yaml:"servername"`
  49. }
  50. Username string
  51. Password string
  52. Sender string
  53. VerifyMessageSubject string `yaml:"verify-message-subject"`
  54. VerifyMessage string `yaml:"verify-message"`
  55. }
  56. }
  57. }
  58. type Config struct {
  59. Network struct {
  60. Name string
  61. }
  62. Server struct {
  63. PassConfig
  64. Password string
  65. Name string
  66. Listen []string
  67. Wslisten string `yaml:"ws-listen"`
  68. TLSListeners map[string]*TLSListenConfig `yaml:"tls-listeners"`
  69. CheckIdent bool `yaml:"check-ident"`
  70. Log string
  71. MOTD string
  72. ProxyAllowedFrom []string `yaml:"proxy-allowed-from"`
  73. }
  74. Datastore struct {
  75. Path string
  76. }
  77. Registration struct {
  78. Accounts AccountRegistrationConfig
  79. }
  80. Operator map[string]*PassConfig
  81. Limits struct {
  82. NickLen int `yaml:"nicklen"`
  83. ChannelLen int `yaml:"channellen"`
  84. AwayLen int `yaml:"awaylen"`
  85. KickLen int `yaml:"kicklen"`
  86. TopicLen int `yaml:"topiclen"`
  87. WhowasEntries uint `yaml:"whowas-entries"`
  88. }
  89. }
  90. func (conf *Config) Operators() map[string][]byte {
  91. operators := make(map[string][]byte)
  92. for name, opConf := range conf.Operator {
  93. name, err := CasefoldName(name)
  94. if err == nil {
  95. operators[name] = opConf.PasswordBytes()
  96. } else {
  97. log.Println("Could not casefold oper name:", err.Error())
  98. }
  99. }
  100. return operators
  101. }
  102. func (conf *Config) TLSListeners() map[string]*tls.Config {
  103. tlsListeners := make(map[string]*tls.Config)
  104. for s, tlsListenersConf := range conf.Server.TLSListeners {
  105. config, err := tlsListenersConf.Config()
  106. if err != nil {
  107. log.Fatal(err)
  108. }
  109. name, err := CasefoldName(s)
  110. if err == nil {
  111. tlsListeners[name] = config
  112. } else {
  113. log.Println("Could not casefold TLS listener:", err.Error())
  114. }
  115. }
  116. return tlsListeners
  117. }
  118. func LoadConfig(filename string) (config *Config, err error) {
  119. data, err := ioutil.ReadFile(filename)
  120. if err != nil {
  121. return nil, err
  122. }
  123. err = yaml.Unmarshal(data, &config)
  124. if err != nil {
  125. return nil, err
  126. }
  127. // we need this so PasswordBytes returns the correct info
  128. if config.Server.Password != "" {
  129. config.Server.PassConfig.Password = config.Server.Password
  130. }
  131. if config.Network.Name == "" {
  132. return nil, errors.New("Network name missing")
  133. }
  134. if config.Server.Name == "" {
  135. return nil, errors.New("Server name missing")
  136. }
  137. if !IsHostname(config.Server.Name) {
  138. return nil, errors.New("Server name must match the format of a hostname")
  139. }
  140. if config.Datastore.Path == "" {
  141. return nil, errors.New("Datastore path missing")
  142. }
  143. if len(config.Server.Listen) == 0 {
  144. return nil, errors.New("Server listening addresses missing")
  145. }
  146. if config.Limits.NickLen < 1 || config.Limits.ChannelLen < 2 || config.Limits.AwayLen < 1 || config.Limits.TopicLen < 1 || config.Limits.TopicLen < 1 {
  147. return nil, errors.New("Limits aren't setup properly, check them and make them sane")
  148. }
  149. return config, nil
  150. }