ircd/irc/gateways.go

140 lines
4.0 KiB
Go
Raw Normal View History

2017-10-15 06:18:14 +00:00
// Copyright (c) 2012-2014 Jeremy Latt
// Copyright (c) 2014-2015 Edmund Huber
// Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package irc
import (
"errors"
2017-10-15 06:18:14 +00:00
"net"
"github.com/oragono/oragono/irc/modes"
2017-10-15 06:18:14 +00:00
"github.com/oragono/oragono/irc/utils"
)
var (
errBadGatewayAddress = errors.New("PROXY/WEBIRC commands are not accepted from this IP address")
errBadProxyLine = errors.New("Invalid PROXY/WEBIRC command")
)
2019-11-20 22:14:42 +00:00
const (
// https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
// "a 108-byte buffer is always enough to store all the line and a trailing zero
// for string processing."
maxProxyLineLen = 107
)
2017-10-15 06:18:14 +00:00
type webircConfig struct {
2020-06-21 19:46:08 +00:00
PasswordString string `yaml:"password"`
Password []byte `yaml:"password-bytes"`
Fingerprint *string // legacy name for certfp, #1050
Certfp string
Hosts []string
2019-02-05 05:19:03 +00:00
allowedNets []net.IPNet
2017-10-15 06:18:14 +00:00
}
// Populate fills out our password or fingerprint.
func (wc *webircConfig) Populate() (err error) {
2020-06-21 19:46:08 +00:00
if wc.PasswordString != "" {
wc.Password, err = decodeLegacyPasswordHash(wc.PasswordString)
2020-06-21 19:46:08 +00:00
if err != nil {
return
}
}
2018-02-01 20:53:49 +00:00
2020-06-21 19:46:08 +00:00
certfp := wc.Certfp
if certfp == "" && wc.Fingerprint != nil {
certfp = *wc.Fingerprint
}
if certfp != "" {
wc.Certfp, err = utils.NormalizeCertfp(certfp)
}
if err != nil {
return
2019-12-29 16:59:49 +00:00
}
2020-06-21 19:46:08 +00:00
if wc.Certfp == "" && wc.PasswordString == "" {
return errors.New("webirc block has no certfp or password specified")
2018-02-01 20:53:49 +00:00
}
2020-06-21 19:46:08 +00:00
wc.allowedNets, err = utils.ParseNetList(wc.Hosts)
2019-02-05 05:19:03 +00:00
return err
2018-02-01 20:53:49 +00:00
}
2017-10-15 06:18:14 +00:00
// ApplyProxiedIP applies the given IP to the client.
2020-05-05 02:29:10 +00:00
func (client *Client) ApplyProxiedIP(session *Session, proxiedIP net.IP, tls bool) (err error, quitMsg string) {
2019-02-26 02:50:43 +00:00
// PROXY and WEBIRC are never accepted from a Tor listener, even if the address itself
// is whitelisted. Furthermore, don't accept PROXY or WEBIRC if we already accepted
// a proxied IP from any source (PROXY, WEBIRC, or X-Forwarded-For):
if session.isTor || session.proxiedIP != nil {
return errBadProxyLine, ""
2019-02-26 02:50:43 +00:00
}
2017-10-15 06:18:14 +00:00
// ensure IP is sane
2020-05-05 02:29:10 +00:00
if proxiedIP == nil {
return errBadProxyLine, "proxied IP is not valid"
}
2020-05-05 02:29:10 +00:00
proxiedIP = proxiedIP.To16()
isBanned, requireSASL, banMsg := client.server.checkBans(client.server.Config(), proxiedIP, true)
2017-10-15 06:18:14 +00:00
if isBanned {
return errBanned, banMsg
2017-10-15 06:18:14 +00:00
}
client.requireSASL = requireSASL
if requireSASL {
client.requireSASLMessage = banMsg
}
2019-09-01 06:36:56 +00:00
// successfully added a limiter entry for the proxied IP;
// remove the entry for the real IP if applicable (#197)
client.server.connectionLimiter.RemoveClient(session.realIP)
2017-10-15 06:18:14 +00:00
// given IP is sane! override the client's current IP
2020-05-05 02:29:10 +00:00
client.server.logger.Info("connect-ip", "Accepted proxy IP for client", proxiedIP.String())
2019-02-05 18:44:33 +00:00
2018-04-23 06:38:35 +00:00
client.stateMutex.Lock()
2019-02-05 18:44:33 +00:00
defer client.stateMutex.Unlock()
2020-05-05 02:29:10 +00:00
client.proxiedIP = proxiedIP
session.proxiedIP = proxiedIP
2018-04-23 06:38:35 +00:00
// nickmask will be updated when the client completes registration
// set tls info
session.certfp = ""
2020-09-23 06:23:35 +00:00
session.peerCerts = nil
2018-04-22 22:47:10 +00:00
client.SetMode(modes.TLS, tls)
return nil, ""
}
// handle the PROXY command: http://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
// PROXY must be sent as the first message in the session and has the syntax:
// PROXY TCP[46] SOURCEIP DESTIP SOURCEPORT DESTPORT\r\n
// unfortunately, an ipv6 SOURCEIP can start with a double colon; in this case,
// the message is invalid IRC and can't be parsed normally, hence the special handling.
func handleProxyCommand(server *Server, client *Client, session *Session, line string) (err error) {
var quitMsg string
defer func() {
if err != nil {
if quitMsg == "" {
quitMsg = client.t("Bad or unauthorized PROXY command")
}
client.Quit(quitMsg, session)
}
}()
ip, err := utils.ParseProxyLineV1(line)
2020-05-05 02:29:10 +00:00
if err != nil {
return err
} else if ip == nil {
return nil
}
2019-02-05 05:19:03 +00:00
if utils.IPInNets(client.realIP, server.Config().Server.proxyAllowedFromNets) {
// assume PROXY connections are always secure
2020-05-05 02:29:10 +00:00
err, quitMsg = client.ApplyProxiedIP(session, ip, true)
return
} else {
// real source IP is not authorized to issue PROXY:
return errBadGatewayAddress
}
2017-10-15 06:18:14 +00:00
}