ergo/irc/gateways.go

115 lines
3.2 KiB
Go
Raw Normal View History

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 (
"errors"
2017-10-15 08:18:14 +02:00
"fmt"
"net"
"strings"
2017-10-15 08:18:14 +02:00
"github.com/oragono/oragono/irc/modes"
2017-10-15 08:18:14 +02: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")
)
2017-10-15 08:18:14 +02:00
type webircConfig struct {
PasswordString string `yaml:"password"`
Password []byte `yaml:"password-bytes"`
Fingerprint string
Hosts []string
2019-02-05 06:19:03 +01:00
allowedNets []net.IPNet
2017-10-15 08:18:14 +02:00
}
// Populate fills out our password or fingerprint.
func (wc *webircConfig) Populate() (err error) {
if wc.Fingerprint == "" && wc.PasswordString == "" {
2018-02-03 13:03:36 +01:00
return ErrNoFingerprintOrPassword
}
if wc.PasswordString != "" {
wc.Password, err = decodeLegacyPasswordHash(wc.PasswordString)
}
2018-02-01 21:53:49 +01:00
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.
func (client *Client) ApplyProxiedIP(session *Session, proxiedIP string, tls bool) (success bool) {
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:
if client.isTor {
return false
}
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 {
client.Quit(fmt.Sprintf(client.t("Proxied IP address is not valid: [%s]"), proxiedIP), session)
return false
}
2017-10-15 08:18:14 +02:00
isBanned, banMsg := client.server.checkBans(parsedProxiedIP)
if isBanned {
client.Quit(banMsg, session)
return false
2017-10-15 08:18:14 +02:00
}
// given IP is sane! override the client's current IP
2019-02-05 19:44:33 +01:00
ipstring := parsedProxiedIP.String()
client.server.logger.Info("localconnect-ip", "Accepted proxy IP for client", ipstring)
rawHostname := utils.LookupHostname(ipstring)
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
2018-04-23 08:38:35 +02:00
client.rawHostname = rawHostname
// nickmask will be updated when the client completes registration
// set tls info
client.certfp = ""
2018-04-23 00:47:10 +02:00
client.SetMode(modes.TLS, tls)
return true
}
// 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) {
defer func() {
if err != nil {
client.Quit(client.t("Bad or unauthorized PROXY command"), session)
}
}()
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
if client.ApplyProxiedIP(session, params[2], true) {
2019-02-05 06:19:03 +01:00
return nil
} else {
return errBadProxyLine
}
}
// real source IP is not authorized to issue PROXY:
return errBadGatewayAddress
2017-10-15 08:18:14 +02:00
}