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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "net"
  9. "github.com/oragono/oragono/irc/modes"
  10. "github.com/oragono/oragono/irc/utils"
  11. )
  12. var (
  13. errBadGatewayAddress = errors.New("PROXY/WEBIRC commands are not accepted from this IP address")
  14. errBadProxyLine = errors.New("Invalid PROXY/WEBIRC command")
  15. )
  16. const (
  17. // https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
  18. // "a 108-byte buffer is always enough to store all the line and a trailing zero
  19. // for string processing."
  20. maxProxyLineLen = 107
  21. )
  22. type webircConfig struct {
  23. PasswordString string `yaml:"password"`
  24. Password []byte `yaml:"password-bytes"`
  25. Fingerprint string
  26. Hosts []string
  27. allowedNets []net.IPNet
  28. }
  29. // Populate fills out our password or fingerprint.
  30. func (wc *webircConfig) Populate() (err error) {
  31. if wc.Fingerprint == "" && wc.PasswordString == "" {
  32. err = ErrNoFingerprintOrPassword
  33. }
  34. if err == nil && wc.PasswordString != "" {
  35. wc.Password, err = decodeLegacyPasswordHash(wc.PasswordString)
  36. }
  37. if err == nil && wc.Fingerprint != "" {
  38. wc.Fingerprint, err = utils.NormalizeCertfp(wc.Fingerprint)
  39. }
  40. if err == nil {
  41. wc.allowedNets, err = utils.ParseNetList(wc.Hosts)
  42. }
  43. return err
  44. }
  45. // ApplyProxiedIP applies the given IP to the client.
  46. func (client *Client) ApplyProxiedIP(session *Session, proxiedIP net.IP, tls bool) (err error, quitMsg string) {
  47. // PROXY and WEBIRC are never accepted from a Tor listener, even if the address itself
  48. // is whitelisted:
  49. if session.isTor {
  50. return errBadProxyLine, ""
  51. }
  52. // ensure IP is sane
  53. if proxiedIP == nil {
  54. return errBadProxyLine, "proxied IP is not valid"
  55. }
  56. proxiedIP = proxiedIP.To16()
  57. isBanned, banMsg := client.server.checkBans(proxiedIP)
  58. if isBanned {
  59. return errBanned, banMsg
  60. }
  61. // successfully added a limiter entry for the proxied IP;
  62. // remove the entry for the real IP if applicable (#197)
  63. client.server.connectionLimiter.RemoveClient(session.realIP)
  64. // given IP is sane! override the client's current IP
  65. client.server.logger.Info("connect-ip", "Accepted proxy IP for client", proxiedIP.String())
  66. client.stateMutex.Lock()
  67. defer client.stateMutex.Unlock()
  68. client.proxiedIP = proxiedIP
  69. session.proxiedIP = proxiedIP
  70. // nickmask will be updated when the client completes registration
  71. // set tls info
  72. session.certfp = ""
  73. client.SetMode(modes.TLS, tls)
  74. return nil, ""
  75. }
  76. // handle the PROXY command: http://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
  77. // PROXY must be sent as the first message in the session and has the syntax:
  78. // PROXY TCP[46] SOURCEIP DESTIP SOURCEPORT DESTPORT\r\n
  79. // unfortunately, an ipv6 SOURCEIP can start with a double colon; in this case,
  80. // the message is invalid IRC and can't be parsed normally, hence the special handling.
  81. func handleProxyCommand(server *Server, client *Client, session *Session, line string) (err error) {
  82. var quitMsg string
  83. defer func() {
  84. if err != nil {
  85. if quitMsg == "" {
  86. quitMsg = client.t("Bad or unauthorized PROXY command")
  87. }
  88. client.Quit(quitMsg, session)
  89. }
  90. }()
  91. ip, err := utils.ParseProxyLine(line)
  92. if err != nil {
  93. return err
  94. }
  95. if utils.IPInNets(client.realIP, server.Config().Server.proxyAllowedFromNets) {
  96. // assume PROXY connections are always secure
  97. err, quitMsg = client.ApplyProxiedIP(session, ip, true)
  98. return
  99. } else {
  100. // real source IP is not authorized to issue PROXY:
  101. return errBadGatewayAddress
  102. }
  103. }