2017-10-15 08:18:14 +02: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 (
|
2018-09-03 06:19:10 +02:00
|
|
|
"errors"
|
2017-10-15 08:18:14 +02:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2018-09-03 06:19:10 +02:00
|
|
|
"strings"
|
2019-11-20 23:14:42 +01:00
|
|
|
"time"
|
2017-10-15 08:18:14 +02:00
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
"github.com/oragono/oragono/irc/modes"
|
2017-10-15 08:18:14 +02:00
|
|
|
"github.com/oragono/oragono/irc/utils"
|
|
|
|
)
|
|
|
|
|
2018-09-03 06:19:10 +02:00
|
|
|
var (
|
|
|
|
errBadGatewayAddress = errors.New("PROXY/WEBIRC commands are not accepted from this IP address")
|
|
|
|
errBadProxyLine = errors.New("Invalid PROXY/WEBIRC command")
|
|
|
|
)
|
|
|
|
|
2019-11-20 23:14:42 +01: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 08:18:14 +02:00
|
|
|
type webircConfig struct {
|
2017-10-15 10:15:18 +02:00
|
|
|
PasswordString string `yaml:"password"`
|
|
|
|
Password []byte `yaml:"password-bytes"`
|
2017-10-16 00:47:49 +02:00
|
|
|
Fingerprint string
|
2017-10-15 10:15:18 +02:00
|
|
|
Hosts []string
|
2019-02-05 06:19:03 +01:00
|
|
|
allowedNets []net.IPNet
|
2017-10-15 08:18:14 +02:00
|
|
|
}
|
|
|
|
|
2017-10-16 00:47:49 +02:00
|
|
|
// Populate fills out our password or fingerprint.
|
|
|
|
func (wc *webircConfig) Populate() (err error) {
|
|
|
|
if wc.Fingerprint == "" && wc.PasswordString == "" {
|
2019-12-29 17:59:49 +01:00
|
|
|
err = ErrNoFingerprintOrPassword
|
2017-10-16 00:47:49 +02:00
|
|
|
}
|
|
|
|
|
2019-12-29 17:59:49 +01:00
|
|
|
if err == nil && wc.PasswordString != "" {
|
2018-08-06 04:51:39 +02:00
|
|
|
wc.Password, err = decodeLegacyPasswordHash(wc.PasswordString)
|
2017-10-16 00:47:49 +02:00
|
|
|
}
|
2018-02-01 21:53:49 +01:00
|
|
|
|
2019-12-29 17:59:49 +01:00
|
|
|
if err == nil && wc.Fingerprint != "" {
|
|
|
|
wc.Fingerprint, err = utils.NormalizeCertfp(wc.Fingerprint)
|
|
|
|
}
|
|
|
|
|
2019-02-05 06:19:03 +01:00
|
|
|
if err == nil {
|
|
|
|
wc.allowedNets, err = utils.ParseNetList(wc.Hosts)
|
2018-02-01 21:53:49 +01:00
|
|
|
}
|
|
|
|
|
2019-02-05 06:19:03 +01:00
|
|
|
return err
|
2018-02-01 21:53:49 +01:00
|
|
|
}
|
|
|
|
|
2017-10-15 08:18:14 +02:00
|
|
|
// ApplyProxiedIP applies the given IP to the client.
|
2019-05-13 07:54:50 +02:00
|
|
|
func (client *Client) ApplyProxiedIP(session *Session, proxiedIP string, tls bool) (err error, quitMsg string) {
|
2019-02-26 03:50:43 +01:00
|
|
|
// PROXY and WEBIRC are never accepted from a Tor listener, even if the address itself
|
|
|
|
// is whitelisted:
|
2020-02-19 01:38:42 +01:00
|
|
|
if session.isTor {
|
2019-05-13 07:54:50 +02:00
|
|
|
return errBadProxyLine, ""
|
2019-02-26 03:50:43 +01:00
|
|
|
}
|
|
|
|
|
2017-10-15 08:18:14 +02:00
|
|
|
// ensure IP is sane
|
2019-02-05 06:19:03 +01:00
|
|
|
parsedProxiedIP := net.ParseIP(proxiedIP).To16()
|
2017-10-15 08:18:14 +02:00
|
|
|
if parsedProxiedIP == nil {
|
2019-05-13 07:54:50 +02:00
|
|
|
return errBadProxyLine, fmt.Sprintf(client.t("Proxied IP address is not valid: [%s]"), proxiedIP)
|
2018-09-03 06:19:10 +02:00
|
|
|
}
|
|
|
|
|
2017-10-15 08:18:14 +02:00
|
|
|
isBanned, banMsg := client.server.checkBans(parsedProxiedIP)
|
|
|
|
if isBanned {
|
2019-05-13 07:54:50 +02:00
|
|
|
return errBanned, banMsg
|
2017-10-15 08:18:14 +02:00
|
|
|
}
|
2019-09-01 08:36:56 +02: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 08:18:14 +02:00
|
|
|
|
|
|
|
// given IP is sane! override the client's current IP
|
2020-04-12 19:58:35 +02:00
|
|
|
client.server.logger.Info("connect-ip", "Accepted proxy IP for client", parsedProxiedIP.String())
|
2019-02-05 19:44:33 +01:00
|
|
|
|
2018-04-23 08:38:35 +02:00
|
|
|
client.stateMutex.Lock()
|
2019-02-05 19:44:33 +01:00
|
|
|
defer client.stateMutex.Unlock()
|
2018-02-01 21:53:49 +01:00
|
|
|
client.proxiedIP = parsedProxiedIP
|
2019-11-20 23:14:42 +01:00
|
|
|
session.proxiedIP = parsedProxiedIP
|
2018-04-23 08:38:35 +02:00
|
|
|
// nickmask will be updated when the client completes registration
|
2017-10-16 00:47:49 +02:00
|
|
|
// set tls info
|
2020-02-19 03:42:27 +01:00
|
|
|
session.certfp = ""
|
2018-04-23 00:47:10 +02:00
|
|
|
client.SetMode(modes.TLS, tls)
|
2017-10-16 00:47:49 +02:00
|
|
|
|
2019-05-13 07:54:50 +02:00
|
|
|
return nil, ""
|
2018-09-03 06:19:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2019-04-12 06:08:46 +02:00
|
|
|
func handleProxyCommand(server *Server, client *Client, session *Session, line string) (err error) {
|
2019-05-13 07:54:50 +02:00
|
|
|
var quitMsg string
|
2018-09-03 06:19:10 +02:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2019-05-13 07:54:50 +02:00
|
|
|
if quitMsg == "" {
|
|
|
|
quitMsg = client.t("Bad or unauthorized PROXY command")
|
|
|
|
}
|
|
|
|
client.Quit(quitMsg, session)
|
2018-09-03 06:19:10 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
params := strings.Fields(line)
|
|
|
|
if len(params) != 6 {
|
|
|
|
return errBadProxyLine
|
|
|
|
}
|
|
|
|
|
2019-02-05 06:19:03 +01:00
|
|
|
if utils.IPInNets(client.realIP, server.Config().Server.proxyAllowedFromNets) {
|
|
|
|
// assume PROXY connections are always secure
|
2019-05-13 07:54:50 +02:00
|
|
|
err, quitMsg = client.ApplyProxiedIP(session, params[2], true)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
// real source IP is not authorized to issue PROXY:
|
|
|
|
return errBadGatewayAddress
|
2018-09-03 06:19:10 +02:00
|
|
|
}
|
2017-10-15 08:18:14 +02:00
|
|
|
}
|
2019-11-20 23:14:42 +01:00
|
|
|
|
|
|
|
// read a PROXY line one byte at a time, to ensure we don't read anything beyond
|
|
|
|
// that into a buffer, which would break the TLS handshake
|
|
|
|
func readRawProxyLine(conn net.Conn) (result string) {
|
|
|
|
// normally this is covered by ping timeouts, but we're doing this outside
|
|
|
|
// of the normal client goroutine:
|
|
|
|
conn.SetDeadline(time.Now().Add(time.Minute))
|
|
|
|
defer conn.SetDeadline(time.Time{})
|
|
|
|
|
|
|
|
var buf [maxProxyLineLen]byte
|
|
|
|
oneByte := make([]byte, 1)
|
|
|
|
i := 0
|
|
|
|
for i < maxProxyLineLen {
|
|
|
|
n, err := conn.Read(oneByte)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
} else if n == 1 {
|
|
|
|
buf[i] = oneByte[0]
|
|
|
|
if buf[i] == '\n' {
|
|
|
|
candidate := string(buf[0 : i+1])
|
|
|
|
if strings.HasPrefix(candidate, "PROXY") {
|
|
|
|
return candidate
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no \r\n, fail out
|
|
|
|
return
|
|
|
|
}
|