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.

gateways.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (c) 2012-2014 Jeremy Latt
  2. // Copyright (c) 2014-2015 Edmund Huber
  3. // Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
  4. // released under the MIT license
  5. package irc
  6. import (
  7. "errors"
  8. "fmt"
  9. "net"
  10. "strings"
  11. "github.com/oragono/oragono/irc/passwd"
  12. "github.com/goshuirc/irc-go/ircmsg"
  13. "github.com/oragono/oragono/irc/utils"
  14. )
  15. type webircConfig struct {
  16. PasswordString string `yaml:"password"`
  17. Password []byte `yaml:"password-bytes"`
  18. Fingerprint string
  19. Hosts []string
  20. }
  21. // Populate fills out our password or fingerprint.
  22. func (wc *webircConfig) Populate() (err error) {
  23. if wc.Fingerprint == "" && wc.PasswordString == "" {
  24. return errors.New("Fingerprint or password needs to be specified")
  25. }
  26. if wc.PasswordString != "" {
  27. var password []byte
  28. password, err = passwd.DecodePasswordHash(wc.PasswordString)
  29. wc.Password = password
  30. }
  31. return err
  32. }
  33. // WEBIRC <password> <gateway> <hostname> <ip> [:flag1 flag2=x flag3]
  34. func webircHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
  35. // only allow unregistered clients to use this command
  36. if client.registered {
  37. return false
  38. }
  39. // process flags
  40. var secure bool
  41. if 4 < len(msg.Params) {
  42. for _, x := range strings.Split(msg.Params[4], " ") {
  43. // split into key=value
  44. var key string
  45. if strings.Contains(x, "=") {
  46. y := strings.SplitN(x, "=", 2)
  47. key, _ = y[0], y[1]
  48. } else {
  49. key = x
  50. }
  51. // only accept "tls" flag if the gateway's connection to us is secure as well
  52. if strings.ToLower(key) == "tls" && client.flags[TLS] {
  53. secure = true
  54. }
  55. }
  56. }
  57. clientAddress := utils.IPString(client.socket.conn.RemoteAddr())
  58. clientHostname := client.hostname
  59. for _, info := range server.WebIRCConfig() {
  60. for _, address := range info.Hosts {
  61. if clientHostname == address || clientAddress == address {
  62. // confirm password and/or fingerprint
  63. givenPassword := msg.Params[0]
  64. if 0 < len(info.Password) && passwd.ComparePasswordString(info.Password, givenPassword) != nil {
  65. continue
  66. }
  67. if 0 < len(info.Fingerprint) && client.certfp != info.Fingerprint {
  68. continue
  69. }
  70. proxiedIP := msg.Params[3]
  71. return client.ApplyProxiedIP(proxiedIP, secure)
  72. }
  73. }
  74. }
  75. client.Quit(client.t("WEBIRC command is not usable from your address or incorrect password given"))
  76. return true
  77. }
  78. // PROXY TCP4/6 SOURCEIP DESTIP SOURCEPORT DESTPORT
  79. // http://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
  80. func proxyHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
  81. // only allow unregistered clients to use this command
  82. if client.registered {
  83. return false
  84. }
  85. clientAddress := utils.IPString(client.socket.conn.RemoteAddr())
  86. clientHostname := client.hostname
  87. for _, address := range server.ProxyAllowedFrom() {
  88. if clientHostname == address || clientAddress == address {
  89. proxiedIP := msg.Params[1]
  90. // assume PROXY connections are always secure
  91. return client.ApplyProxiedIP(proxiedIP, true)
  92. }
  93. }
  94. client.Quit(client.t("PROXY command is not usable from your address"))
  95. return true
  96. }
  97. // ApplyProxiedIP applies the given IP to the client.
  98. func (client *Client) ApplyProxiedIP(proxiedIP string, tls bool) (exiting bool) {
  99. // ensure IP is sane
  100. parsedProxiedIP := net.ParseIP(proxiedIP)
  101. if parsedProxiedIP == nil {
  102. client.Quit(fmt.Sprintf(client.t("Proxied IP address is not valid: [%s]"), proxiedIP))
  103. return true
  104. }
  105. isBanned, banMsg := client.server.checkBans(parsedProxiedIP)
  106. if isBanned {
  107. client.Quit(banMsg)
  108. return true
  109. }
  110. // given IP is sane! override the client's current IP
  111. client.proxiedIP = proxiedIP
  112. client.rawHostname = utils.LookupHostname(proxiedIP)
  113. client.hostname = client.rawHostname
  114. // set tls info
  115. client.certfp = ""
  116. if tls {
  117. client.flags[TLS] = true
  118. } else {
  119. delete(client.flags, TLS)
  120. }
  121. return false
  122. }