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 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. "fmt"
  10. "io/ioutil"
  11. "log"
  12. "strings"
  13. "time"
  14. "gopkg.in/yaml.v2"
  15. )
  16. type PassConfig struct {
  17. Password string
  18. }
  19. // TLSListenConfig defines configuration options for listening on TLS
  20. type TLSListenConfig struct {
  21. Cert string
  22. Key string
  23. }
  24. // Certificate returns the TLS certificate assicated with this TLSListenConfig
  25. func (conf *TLSListenConfig) Config() (*tls.Config, error) {
  26. cert, err := tls.LoadX509KeyPair(conf.Cert, conf.Key)
  27. if err != nil {
  28. return nil, errors.New("tls cert+key: invalid pair")
  29. }
  30. return &tls.Config{
  31. Certificates: []tls.Certificate{cert},
  32. }, err
  33. }
  34. func (conf *PassConfig) PasswordBytes() []byte {
  35. bytes, err := DecodePasswordHash(conf.Password)
  36. if err != nil {
  37. log.Fatal("decode password error: ", err)
  38. }
  39. return bytes
  40. }
  41. type AccountRegistrationConfig struct {
  42. Enabled bool
  43. EnabledCallbacks []string `yaml:"enabled-callbacks"`
  44. Callbacks struct {
  45. Mailto struct {
  46. Server string
  47. Port int
  48. TLS struct {
  49. Enabled bool
  50. InsecureSkipVerify bool `yaml:"insecure_skip_verify"`
  51. ServerName string `yaml:"servername"`
  52. }
  53. Username string
  54. Password string
  55. Sender string
  56. VerifyMessageSubject string `yaml:"verify-message-subject"`
  57. VerifyMessage string `yaml:"verify-message"`
  58. }
  59. }
  60. }
  61. type OperClassConfig struct {
  62. Title string
  63. WhoisLine string
  64. Extends string
  65. Capabilities []string
  66. }
  67. type OperConfig struct {
  68. Class string
  69. Vhost string
  70. WhoisLine string `yaml:"whois-line"`
  71. Password string
  72. }
  73. func (conf *OperConfig) PasswordBytes() []byte {
  74. bytes, err := DecodePasswordHash(conf.Password)
  75. if err != nil {
  76. log.Fatal("decode password error: ", err)
  77. }
  78. return bytes
  79. }
  80. type RestAPIConfig struct {
  81. Enabled bool
  82. Listen string
  83. }
  84. type ConnectionLimitsConfig struct {
  85. Enabled bool
  86. CidrLenIPv4 int `yaml:"cidr-len-ipv4"`
  87. CidrLenIPv6 int `yaml:"cidr-len-ipv6"`
  88. IPsPerCidr int `yaml:"ips-per-subnet"`
  89. Exempted []string
  90. }
  91. type ConnectionThrottleConfig struct {
  92. Enabled bool
  93. CidrLenIPv4 int `yaml:"cidr-len-ipv4"`
  94. CidrLenIPv6 int `yaml:"cidr-len-ipv6"`
  95. ConnectionsPerCidr int `yaml:"max-connections"`
  96. DurationString string `yaml:"duration"`
  97. Duration time.Duration `yaml:"duration-time"`
  98. BanDurationString string `yaml:"ban-duration"`
  99. BanDuration time.Duration
  100. BanMessage string `yaml:"ban-message"`
  101. Exempted []string
  102. }
  103. type Config struct {
  104. Network struct {
  105. Name string
  106. }
  107. Server struct {
  108. PassConfig
  109. Password string
  110. Name string
  111. Listen []string
  112. Wslisten string `yaml:"ws-listen"`
  113. TLSListeners map[string]*TLSListenConfig `yaml:"tls-listeners"`
  114. RestAPI RestAPIConfig `yaml:"rest-api"`
  115. CheckIdent bool `yaml:"check-ident"`
  116. Log string
  117. MOTD string
  118. ConnectionLimits ConnectionLimitsConfig `yaml:"connection-limits"`
  119. ConnectionThrottle ConnectionThrottleConfig `yaml:"connection-throttling"`
  120. }
  121. Datastore struct {
  122. Path string
  123. }
  124. AuthenticationEnabled bool `yaml:"authentication-enabled"`
  125. Registration struct {
  126. Accounts AccountRegistrationConfig
  127. }
  128. OperClasses map[string]*OperClassConfig `yaml:"oper-classes"`
  129. Opers map[string]*OperConfig
  130. Limits struct {
  131. NickLen uint `yaml:"nicklen"`
  132. ChannelLen uint `yaml:"channellen"`
  133. AwayLen uint `yaml:"awaylen"`
  134. KickLen uint `yaml:"kicklen"`
  135. TopicLen uint `yaml:"topiclen"`
  136. WhowasEntries uint `yaml:"whowas-entries"`
  137. MonitorEntries uint `yaml:"monitor-entries"`
  138. ChanListModes uint `yaml:"chan-list-modes"`
  139. }
  140. }
  141. type OperClass struct {
  142. Title string
  143. WhoisLine string `yaml:"whois-line"`
  144. Capabilities map[string]bool // map to make lookups much easier
  145. }
  146. func (conf *Config) OperatorClasses() (*map[string]OperClass, error) {
  147. ocs := make(map[string]OperClass)
  148. // loop from no extends to most extended, breaking if we can't add any more
  149. lenOfLastOcs := -1
  150. for {
  151. if lenOfLastOcs == len(ocs) {
  152. return nil, errors.New("OperClasses contains a looping dependency, or a class extends from a class that doesn't exist")
  153. }
  154. lenOfLastOcs = len(ocs)
  155. var anyMissing bool
  156. for name, info := range conf.OperClasses {
  157. _, exists := ocs[name]
  158. _, extendsExists := ocs[info.Extends]
  159. if exists {
  160. // class already exists
  161. continue
  162. } else if len(info.Extends) > 0 && !extendsExists {
  163. // class we extend on doesn't exist
  164. _, exists := conf.OperClasses[info.Extends]
  165. if !exists {
  166. return nil, fmt.Errorf("Operclass [%s] extends [%s], which doesn't exist", name, info.Extends)
  167. }
  168. anyMissing = true
  169. continue
  170. }
  171. // create new operclass
  172. var oc OperClass
  173. oc.Capabilities = make(map[string]bool)
  174. // get inhereted info from other operclasses
  175. if len(info.Extends) > 0 {
  176. einfo, _ := ocs[info.Extends]
  177. for capab := range einfo.Capabilities {
  178. oc.Capabilities[capab] = true
  179. }
  180. }
  181. // add our own info
  182. oc.Title = info.Title
  183. for _, capab := range info.Capabilities {
  184. oc.Capabilities[capab] = true
  185. }
  186. if len(info.WhoisLine) > 0 {
  187. oc.WhoisLine = info.WhoisLine
  188. } else {
  189. oc.WhoisLine = "is a"
  190. if strings.Contains(strings.ToLower(string(oc.Title[0])), "aeiou") {
  191. oc.WhoisLine += "n"
  192. }
  193. oc.WhoisLine += " "
  194. oc.WhoisLine += oc.Title
  195. }
  196. ocs[name] = oc
  197. }
  198. if !anyMissing {
  199. // we've got every operclass!
  200. break
  201. }
  202. }
  203. return &ocs, nil
  204. }
  205. type Oper struct {
  206. Class *OperClass
  207. WhoisLine string
  208. Vhost string
  209. Pass []byte
  210. }
  211. func (conf *Config) Operators(oc *map[string]OperClass) (map[string]Oper, error) {
  212. operators := make(map[string]Oper)
  213. for name, opConf := range conf.Opers {
  214. var oper Oper
  215. // oper name
  216. name, err := CasefoldName(name)
  217. if err != nil {
  218. return nil, fmt.Errorf("Could not casefold oper name: %s", err.Error())
  219. }
  220. oper.Pass = opConf.PasswordBytes()
  221. oper.Vhost = opConf.Vhost
  222. class, exists := (*oc)[opConf.Class]
  223. if !exists {
  224. return nil, fmt.Errorf("Could not load operator [%s] - they use operclass [%s] which does not exist", name, opConf.Class)
  225. }
  226. oper.Class = &class
  227. if len(opConf.WhoisLine) > 0 {
  228. oper.WhoisLine = opConf.WhoisLine
  229. } else {
  230. oper.WhoisLine = class.WhoisLine
  231. }
  232. // successful, attach to list of opers
  233. operators[name] = oper
  234. }
  235. return operators, nil
  236. }
  237. func (conf *Config) TLSListeners() map[string]*tls.Config {
  238. tlsListeners := make(map[string]*tls.Config)
  239. for s, tlsListenersConf := range conf.Server.TLSListeners {
  240. config, err := tlsListenersConf.Config()
  241. if err != nil {
  242. log.Fatal(err)
  243. }
  244. name, err := CasefoldName(s)
  245. if err == nil {
  246. tlsListeners[name] = config
  247. } else {
  248. log.Println("Could not casefold TLS listener:", err.Error())
  249. }
  250. }
  251. return tlsListeners
  252. }
  253. func LoadConfig(filename string) (config *Config, err error) {
  254. data, err := ioutil.ReadFile(filename)
  255. if err != nil {
  256. return nil, err
  257. }
  258. err = yaml.Unmarshal(data, &config)
  259. if err != nil {
  260. return nil, err
  261. }
  262. // we need this so PasswordBytes returns the correct info
  263. if config.Server.Password != "" {
  264. config.Server.PassConfig.Password = config.Server.Password
  265. }
  266. if config.Network.Name == "" {
  267. return nil, errors.New("Network name missing")
  268. }
  269. if config.Server.Name == "" {
  270. return nil, errors.New("Server name missing")
  271. }
  272. if !IsHostname(config.Server.Name) {
  273. return nil, errors.New("Server name must match the format of a hostname")
  274. }
  275. if config.Datastore.Path == "" {
  276. return nil, errors.New("Datastore path missing")
  277. }
  278. if len(config.Server.Listen) == 0 {
  279. return nil, errors.New("Server listening addresses missing")
  280. }
  281. if config.Limits.NickLen < 1 || config.Limits.ChannelLen < 2 || config.Limits.AwayLen < 1 || config.Limits.KickLen < 1 || config.Limits.TopicLen < 1 {
  282. return nil, errors.New("Limits aren't setup properly, check them and make them sane")
  283. }
  284. if config.Server.ConnectionThrottle.Enabled {
  285. config.Server.ConnectionThrottle.Duration, err = time.ParseDuration(config.Server.ConnectionThrottle.DurationString)
  286. if err != nil {
  287. return nil, fmt.Errorf("Could not parse connection-throttle duration: %s", err.Error())
  288. }
  289. config.Server.ConnectionThrottle.BanDuration, err = time.ParseDuration(config.Server.ConnectionThrottle.BanDurationString)
  290. if err != nil {
  291. return nil, fmt.Errorf("Could not parse connection-throttle ban-duration: %s", err.Error())
  292. }
  293. }
  294. return config, nil
  295. }