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"
|
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")
|
|
|
|
)
|
|
|
|
|
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
|
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 == "" {
|
2018-02-03 13:03:36 +01:00
|
|
|
return ErrNoFingerprintOrPassword
|
2017-10-16 00:47:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if wc.PasswordString != "" {
|
2018-08-06 04:51:39 +02:00
|
|
|
wc.Password, err = decodeLegacyPasswordHash(wc.PasswordString)
|
2017-10-16 00:47:49 +02:00
|
|
|
}
|
|
|
|
return err
|
2017-10-15 08:18:14 +02:00
|
|
|
}
|
|
|
|
|
2018-02-01 21:53:49 +01:00
|
|
|
func isGatewayAllowed(addr net.Addr, gatewaySpec string) bool {
|
|
|
|
// "localhost" includes any loopback IP or unix domain socket
|
|
|
|
if gatewaySpec == "localhost" {
|
|
|
|
return utils.AddrIsLocal(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := utils.AddrToIP(addr)
|
|
|
|
if ip == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// exact IP match
|
|
|
|
if ip.String() == gatewaySpec {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// CIDR match
|
|
|
|
_, gatewayNet, err := net.ParseCIDR(gatewaySpec)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return gatewayNet.Contains(ip)
|
|
|
|
}
|
|
|
|
|
2017-10-15 08:18:14 +02:00
|
|
|
// ApplyProxiedIP applies the given IP to the client.
|
2018-09-03 06:19:10 +02:00
|
|
|
func (client *Client) ApplyProxiedIP(proxiedIP string, tls bool) (success bool) {
|
2017-10-15 08:18:14 +02:00
|
|
|
// ensure IP is sane
|
|
|
|
parsedProxiedIP := net.ParseIP(proxiedIP)
|
|
|
|
if parsedProxiedIP == nil {
|
2018-01-22 12:26:01 +01:00
|
|
|
client.Quit(fmt.Sprintf(client.t("Proxied IP address is not valid: [%s]"), proxiedIP))
|
2018-09-03 06:19:10 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// undo any mapping of v4 addresses into the v6 space: https://stackoverflow.com/a/1618259
|
|
|
|
// this is how a typical stunnel4 deployment on Linux will handle dual-stack
|
|
|
|
unmappedIP := parsedProxiedIP.To4()
|
|
|
|
if unmappedIP != nil {
|
|
|
|
parsedProxiedIP = unmappedIP
|
2017-10-15 08:18:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isBanned, banMsg := client.server.checkBans(parsedProxiedIP)
|
|
|
|
if isBanned {
|
|
|
|
client.Quit(banMsg)
|
2018-09-03 06:19:10 +02:00
|
|
|
return false
|
2017-10-15 08:18:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// given IP is sane! override the client's current IP
|
2018-09-03 06:19:10 +02:00
|
|
|
rawHostname := utils.LookupHostname(parsedProxiedIP.String())
|
2018-04-23 08:38:35 +02:00
|
|
|
client.stateMutex.Lock()
|
2018-02-01 21:53:49 +01:00
|
|
|
client.proxiedIP = parsedProxiedIP
|
2018-04-23 08:38:35 +02:00
|
|
|
client.rawHostname = rawHostname
|
|
|
|
client.stateMutex.Unlock()
|
|
|
|
// nickmask will be updated when the client completes registration
|
2017-10-16 00:47:49 +02:00
|
|
|
|
|
|
|
// set tls info
|
|
|
|
client.certfp = ""
|
2018-04-23 00:47:10 +02:00
|
|
|
client.SetMode(modes.TLS, tls)
|
2017-10-16 00:47:49 +02:00
|
|
|
|
2018-09-03 06:19:10 +02:00
|
|
|
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, line string) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
client.Quit(client.t("Bad or unauthorized PROXY command"))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
params := strings.Fields(line)
|
|
|
|
if len(params) != 6 {
|
|
|
|
return errBadProxyLine
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, gateway := range server.ProxyAllowedFrom() {
|
|
|
|
if isGatewayAllowed(client.socket.conn.RemoteAddr(), gateway) {
|
|
|
|
// assume PROXY connections are always secure
|
|
|
|
if client.ApplyProxiedIP(params[2], true) {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return errBadProxyLine
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// real source IP is not authorized to issue PROXY:
|
|
|
|
return errBadGatewayAddress
|
2017-10-15 08:18:14 +02:00
|
|
|
}
|