2019-06-30 17:07:58 -05:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-04-25 14:54:48 -05:00
|
|
|
package caddytls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2021-04-30 11:14:52 -05:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2022-08-17 17:10:57 -05:00
|
|
|
"net/netip"
|
2021-04-30 11:14:52 -05:00
|
|
|
"strings"
|
2019-04-25 14:54:48 -05:00
|
|
|
|
2020-03-26 15:01:38 -05:00
|
|
|
"github.com/caddyserver/certmagic"
|
2021-04-30 11:14:52 -05:00
|
|
|
"go.uber.org/zap"
|
2023-08-14 10:41:15 -05:00
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
2024-07-24 10:26:09 -05:00
|
|
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
2024-08-07 12:02:23 -05:00
|
|
|
"github.com/caddyserver/caddy/v2/internal"
|
2019-04-25 14:54:48 -05:00
|
|
|
)
|
|
|
|
|
2019-08-21 11:46:35 -05:00
|
|
|
func init() {
|
|
|
|
caddy.RegisterModule(MatchServerName{})
|
2021-04-30 11:14:52 -05:00
|
|
|
caddy.RegisterModule(MatchRemoteIP{})
|
2024-04-15 13:13:24 -05:00
|
|
|
caddy.RegisterModule(MatchLocalIP{})
|
2019-08-21 11:46:35 -05:00
|
|
|
}
|
|
|
|
|
2020-03-20 16:51:37 -05:00
|
|
|
// MatchServerName matches based on SNI. Names in
|
|
|
|
// this list may use left-most-label wildcards,
|
|
|
|
// similar to wildcard certificates.
|
2019-05-07 12:58:58 -05:00
|
|
|
type MatchServerName []string
|
2019-04-25 14:54:48 -05:00
|
|
|
|
2019-08-21 11:46:35 -05:00
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (MatchServerName) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
2019-12-10 15:36:46 -05:00
|
|
|
ID: "tls.handshake_match.sni",
|
|
|
|
New: func() caddy.Module { return new(MatchServerName) },
|
2019-08-21 11:46:35 -05:00
|
|
|
}
|
2019-04-25 14:54:48 -05:00
|
|
|
}
|
|
|
|
|
2019-04-26 13:35:39 -05:00
|
|
|
// Match matches hello based on SNI.
|
2019-04-25 14:54:48 -05:00
|
|
|
func (m MatchServerName) Match(hello *tls.ClientHelloInfo) bool {
|
2024-08-12 02:01:09 -05:00
|
|
|
repl := caddy.NewReplacer()
|
2024-08-07 12:02:23 -05:00
|
|
|
// caddytls.TestServerNameMatcher calls this function without any context
|
|
|
|
if ctx := hello.Context(); ctx != nil {
|
2024-08-12 02:01:09 -05:00
|
|
|
// In some situations the existing context may have no replacer
|
|
|
|
if replAny := ctx.Value(caddy.ReplacerCtxKey); replAny != nil {
|
|
|
|
repl = replAny.(*caddy.Replacer)
|
|
|
|
}
|
2024-08-07 12:02:23 -05:00
|
|
|
}
|
|
|
|
|
2019-04-25 14:54:48 -05:00
|
|
|
for _, name := range m {
|
2024-08-07 12:02:23 -05:00
|
|
|
rs := repl.ReplaceAll(name, "")
|
|
|
|
if certmagic.MatchWildcard(hello.ServerName, rs) {
|
2019-04-25 14:54:48 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-07-24 10:26:09 -05:00
|
|
|
// UnmarshalCaddyfile sets up the MatchServerName from Caddyfile tokens. Syntax:
|
|
|
|
//
|
|
|
|
// sni <domains...>
|
|
|
|
func (m *MatchServerName) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
|
|
|
for d.Next() {
|
|
|
|
wrapper := d.Val()
|
|
|
|
|
|
|
|
// At least one same-line option must be provided
|
|
|
|
if d.CountRemainingArgs() == 0 {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
*m = append(*m, d.RemainingArgs()...)
|
|
|
|
|
|
|
|
// No blocks are supported
|
|
|
|
if d.NextBlock(d.Nesting()) {
|
|
|
|
return d.Errf("malformed TLS handshake matcher '%s': blocks are not supported", wrapper)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-30 11:14:52 -05:00
|
|
|
// MatchRemoteIP matches based on the remote IP of the
|
|
|
|
// connection. Specific IPs or CIDR ranges can be specified.
|
|
|
|
//
|
|
|
|
// Note that IPs can sometimes be spoofed, so do not rely
|
|
|
|
// on this as a replacement for actual authentication.
|
|
|
|
type MatchRemoteIP struct {
|
|
|
|
// The IPs or CIDR ranges to match.
|
|
|
|
Ranges []string `json:"ranges,omitempty"`
|
|
|
|
|
|
|
|
// The IPs or CIDR ranges to *NOT* match.
|
|
|
|
NotRanges []string `json:"not_ranges,omitempty"`
|
|
|
|
|
2022-08-17 17:10:57 -05:00
|
|
|
cidrs []netip.Prefix
|
|
|
|
notCidrs []netip.Prefix
|
2021-04-30 11:14:52 -05:00
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (MatchRemoteIP) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
|
|
|
ID: "tls.handshake_match.remote_ip",
|
|
|
|
New: func() caddy.Module { return new(MatchRemoteIP) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provision parses m's IP ranges, either from IP or CIDR expressions.
|
|
|
|
func (m *MatchRemoteIP) Provision(ctx caddy.Context) error {
|
2024-08-07 12:02:23 -05:00
|
|
|
repl := caddy.NewReplacer()
|
2022-09-16 17:55:30 -05:00
|
|
|
m.logger = ctx.Logger()
|
2021-04-30 11:14:52 -05:00
|
|
|
for _, str := range m.Ranges {
|
2024-08-07 12:02:23 -05:00
|
|
|
rs := repl.ReplaceAll(str, "")
|
|
|
|
cidrs, err := m.parseIPRange(rs)
|
2021-04-30 11:14:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-13 11:56:18 -05:00
|
|
|
m.cidrs = append(m.cidrs, cidrs...)
|
2021-04-30 11:14:52 -05:00
|
|
|
}
|
|
|
|
for _, str := range m.NotRanges {
|
2024-08-07 12:02:23 -05:00
|
|
|
rs := repl.ReplaceAll(str, "")
|
|
|
|
cidrs, err := m.parseIPRange(rs)
|
2021-04-30 11:14:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-13 11:56:18 -05:00
|
|
|
m.notCidrs = append(m.notCidrs, cidrs...)
|
2021-04-30 11:14:52 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match matches hello based on the connection's remote IP.
|
|
|
|
func (m MatchRemoteIP) Match(hello *tls.ClientHelloInfo) bool {
|
|
|
|
remoteAddr := hello.Conn.RemoteAddr().String()
|
|
|
|
ipStr, _, err := net.SplitHostPort(remoteAddr)
|
|
|
|
if err != nil {
|
|
|
|
ipStr = remoteAddr // weird; maybe no port?
|
|
|
|
}
|
2022-08-17 17:10:57 -05:00
|
|
|
ipAddr, err := netip.ParseAddr(ipStr)
|
|
|
|
if err != nil {
|
2024-05-10 09:08:54 -05:00
|
|
|
m.logger.Error("invalid client IP address", zap.String("ip", ipStr))
|
2021-04-30 11:14:52 -05:00
|
|
|
return false
|
|
|
|
}
|
2022-08-17 17:10:57 -05:00
|
|
|
return (len(m.cidrs) == 0 || m.matches(ipAddr, m.cidrs)) &&
|
|
|
|
(len(m.notCidrs) == 0 || !m.matches(ipAddr, m.notCidrs))
|
2021-04-30 11:14:52 -05:00
|
|
|
}
|
|
|
|
|
2022-08-17 17:10:57 -05:00
|
|
|
func (MatchRemoteIP) parseIPRange(str string) ([]netip.Prefix, error) {
|
|
|
|
var cidrs []netip.Prefix
|
2021-04-30 11:14:52 -05:00
|
|
|
if strings.Contains(str, "/") {
|
2022-08-17 17:10:57 -05:00
|
|
|
ipNet, err := netip.ParsePrefix(str)
|
2021-04-30 11:14:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parsing CIDR expression: %v", err)
|
|
|
|
}
|
|
|
|
cidrs = append(cidrs, ipNet)
|
|
|
|
} else {
|
2022-08-17 17:10:57 -05:00
|
|
|
ipAddr, err := netip.ParseAddr(str)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid IP address: '%s': %v", str, err)
|
2021-04-30 11:14:52 -05:00
|
|
|
}
|
2022-08-17 17:10:57 -05:00
|
|
|
ip := netip.PrefixFrom(ipAddr, ipAddr.BitLen())
|
|
|
|
cidrs = append(cidrs, ip)
|
2021-04-30 11:14:52 -05:00
|
|
|
}
|
|
|
|
return cidrs, nil
|
|
|
|
}
|
|
|
|
|
2022-08-17 17:10:57 -05:00
|
|
|
func (MatchRemoteIP) matches(ip netip.Addr, ranges []netip.Prefix) bool {
|
2021-04-30 11:14:52 -05:00
|
|
|
for _, ipRange := range ranges {
|
|
|
|
if ipRange.Contains(ip) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-07-24 10:26:09 -05:00
|
|
|
// UnmarshalCaddyfile sets up the MatchRemoteIP from Caddyfile tokens. Syntax:
|
|
|
|
//
|
|
|
|
// remote_ip <ranges...>
|
|
|
|
//
|
|
|
|
// Note: IPs and CIDRs prefixed with ! symbol are treated as not_ranges
|
|
|
|
func (m *MatchRemoteIP) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
|
|
|
for d.Next() {
|
|
|
|
wrapper := d.Val()
|
|
|
|
|
|
|
|
// At least one same-line option must be provided
|
|
|
|
if d.CountRemainingArgs() == 0 {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
for d.NextArg() {
|
|
|
|
val := d.Val()
|
2024-08-07 12:02:23 -05:00
|
|
|
var exclamation bool
|
2024-07-24 10:26:09 -05:00
|
|
|
if len(val) > 1 && val[0] == '!' {
|
2024-08-07 12:02:23 -05:00
|
|
|
exclamation, val = true, val[1:]
|
|
|
|
}
|
|
|
|
ranges := []string{val}
|
|
|
|
if val == "private_ranges" {
|
|
|
|
ranges = internal.PrivateRangesCIDR()
|
|
|
|
}
|
|
|
|
if exclamation {
|
|
|
|
m.NotRanges = append(m.NotRanges, ranges...)
|
2024-07-24 10:26:09 -05:00
|
|
|
} else {
|
2024-08-07 12:02:23 -05:00
|
|
|
m.Ranges = append(m.Ranges, ranges...)
|
2024-07-24 10:26:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No blocks are supported
|
|
|
|
if d.NextBlock(d.Nesting()) {
|
|
|
|
return d.Errf("malformed TLS handshake matcher '%s': blocks are not supported", wrapper)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-15 13:13:24 -05:00
|
|
|
// MatchLocalIP matches based on the IP address of the interface
|
|
|
|
// receiving the connection. Specific IPs or CIDR ranges can be specified.
|
|
|
|
type MatchLocalIP struct {
|
|
|
|
// The IPs or CIDR ranges to match.
|
|
|
|
Ranges []string `json:"ranges,omitempty"`
|
|
|
|
|
|
|
|
cidrs []netip.Prefix
|
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (MatchLocalIP) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
|
|
|
ID: "tls.handshake_match.local_ip",
|
|
|
|
New: func() caddy.Module { return new(MatchLocalIP) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provision parses m's IP ranges, either from IP or CIDR expressions.
|
|
|
|
func (m *MatchLocalIP) Provision(ctx caddy.Context) error {
|
2024-08-07 12:02:23 -05:00
|
|
|
repl := caddy.NewReplacer()
|
2024-04-15 13:13:24 -05:00
|
|
|
m.logger = ctx.Logger()
|
|
|
|
for _, str := range m.Ranges {
|
2024-08-07 12:02:23 -05:00
|
|
|
rs := repl.ReplaceAll(str, "")
|
|
|
|
cidrs, err := m.parseIPRange(rs)
|
2024-04-15 13:13:24 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.cidrs = append(m.cidrs, cidrs...)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match matches hello based on the connection's remote IP.
|
|
|
|
func (m MatchLocalIP) Match(hello *tls.ClientHelloInfo) bool {
|
|
|
|
localAddr := hello.Conn.LocalAddr().String()
|
|
|
|
ipStr, _, err := net.SplitHostPort(localAddr)
|
|
|
|
if err != nil {
|
|
|
|
ipStr = localAddr // weird; maybe no port?
|
|
|
|
}
|
|
|
|
ipAddr, err := netip.ParseAddr(ipStr)
|
|
|
|
if err != nil {
|
2024-05-10 09:08:54 -05:00
|
|
|
m.logger.Error("invalid local IP address", zap.String("ip", ipStr))
|
2024-04-15 13:13:24 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return (len(m.cidrs) == 0 || m.matches(ipAddr, m.cidrs))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (MatchLocalIP) parseIPRange(str string) ([]netip.Prefix, error) {
|
|
|
|
var cidrs []netip.Prefix
|
|
|
|
if strings.Contains(str, "/") {
|
|
|
|
ipNet, err := netip.ParsePrefix(str)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parsing CIDR expression: %v", err)
|
|
|
|
}
|
|
|
|
cidrs = append(cidrs, ipNet)
|
|
|
|
} else {
|
|
|
|
ipAddr, err := netip.ParseAddr(str)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid IP address: '%s': %v", str, err)
|
|
|
|
}
|
|
|
|
ip := netip.PrefixFrom(ipAddr, ipAddr.BitLen())
|
|
|
|
cidrs = append(cidrs, ip)
|
|
|
|
}
|
|
|
|
return cidrs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (MatchLocalIP) matches(ip netip.Addr, ranges []netip.Prefix) bool {
|
|
|
|
for _, ipRange := range ranges {
|
|
|
|
if ipRange.Contains(ip) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-07-24 10:26:09 -05:00
|
|
|
// UnmarshalCaddyfile sets up the MatchLocalIP from Caddyfile tokens. Syntax:
|
|
|
|
//
|
|
|
|
// local_ip <ranges...>
|
|
|
|
func (m *MatchLocalIP) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
|
|
|
for d.Next() {
|
|
|
|
wrapper := d.Val()
|
|
|
|
|
|
|
|
// At least one same-line option must be provided
|
|
|
|
if d.CountRemainingArgs() == 0 {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
for d.NextArg() {
|
2024-08-07 12:02:23 -05:00
|
|
|
val := d.Val()
|
|
|
|
if val == "private_ranges" {
|
|
|
|
m.Ranges = append(m.Ranges, internal.PrivateRangesCIDR()...)
|
|
|
|
continue
|
2024-07-24 10:26:09 -05:00
|
|
|
}
|
2024-08-07 12:02:23 -05:00
|
|
|
m.Ranges = append(m.Ranges, val)
|
2024-07-24 10:26:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// No blocks are supported
|
|
|
|
if d.NextBlock(d.Nesting()) {
|
|
|
|
return d.Errf("malformed TLS handshake matcher '%s': blocks are not supported", wrapper)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-30 11:14:52 -05:00
|
|
|
// Interface guards
|
|
|
|
var (
|
|
|
|
_ ConnectionMatcher = (*MatchServerName)(nil)
|
|
|
|
_ ConnectionMatcher = (*MatchRemoteIP)(nil)
|
2024-04-15 13:13:24 -05:00
|
|
|
|
|
|
|
_ caddy.Provisioner = (*MatchLocalIP)(nil)
|
|
|
|
_ ConnectionMatcher = (*MatchLocalIP)(nil)
|
2024-07-24 10:26:09 -05:00
|
|
|
|
|
|
|
_ caddyfile.Unmarshaler = (*MatchLocalIP)(nil)
|
|
|
|
_ caddyfile.Unmarshaler = (*MatchRemoteIP)(nil)
|
|
|
|
_ caddyfile.Unmarshaler = (*MatchServerName)(nil)
|
2021-04-30 11:14:52 -05:00
|
|
|
)
|