| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | package plugin
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2017-08-07 13:24:09 -07:00
										 |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	"net"
 | 
					
						
							| 
									
										
										
										
											2021-05-27 07:26:14 -04:00
										 |  |  | 	"runtime"
 | 
					
						
							| 
									
										
										
										
											2017-08-07 13:24:09 -07:00
										 |  |  | 	"strconv"
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	"strings"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/cidr"
 | 
					
						
							| 
									
										
										
										
											2021-05-27 07:26:14 -04:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/log"
 | 
					
						
							| 
									
										
										
										
											2018-09-19 08:16:04 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/parse"
 | 
					
						
							| 
									
										
										
										
											2021-01-24 18:28:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | // See core/dnsserver/address.go - we should unify these two impls.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-21 16:08:55 -04:00
										 |  |  | // Zones represents a lists of zone names.
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | type Zones []string
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 08:30:03 -05:00
										 |  |  | // Matches checks if qname is a subdomain of any of the zones in z.  The match
 | 
					
						
							|  |  |  | // will return the most specific zones that matches. The empty string
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | // signals a not found condition.
 | 
					
						
							|  |  |  | func (z Zones) Matches(qname string) string {
 | 
					
						
							|  |  |  | 	zone := ""
 | 
					
						
							|  |  |  | 	for _, zname := range z {
 | 
					
						
							|  |  |  | 		if dns.IsSubDomain(zname, qname) {
 | 
					
						
							| 
									
										
										
										
											2016-09-07 12:41:40 +00:00
										 |  |  | 			// We want the *longest* matching zone, otherwise we may end up in a parent
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 			if len(zname) > len(zone) {
 | 
					
						
							|  |  |  | 				zone = zname
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return zone
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | // Normalize fully qualifies all zones in z. The zones in Z must be domain names, without
 | 
					
						
							|  |  |  | // a port or protocol prefix.
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func (z Zones) Normalize() {
 | 
					
						
							| 
									
										
										
										
											2016-09-21 17:01:19 +01:00
										 |  |  | 	for i := range z {
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 		z[i] = Name(z[i]).Normalize()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Name represents a domain name.
 | 
					
						
							|  |  |  | type Name string
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Matches checks to see if other is a subdomain (or the same domain) of n.
 | 
					
						
							|  |  |  | // This method assures that names can be easily and consistently matched.
 | 
					
						
							|  |  |  | func (n Name) Matches(child string) bool {
 | 
					
						
							|  |  |  | 	if dns.Name(n) == dns.Name(child) {
 | 
					
						
							|  |  |  | 		return true
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return dns.IsSubDomain(string(n), child)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Normalize lowercases and makes n fully qualified.
 | 
					
						
							|  |  |  | func (n Name) Normalize() string { return strings.ToLower(dns.Fqdn(string(n))) }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type (
 | 
					
						
							| 
									
										
										
										
											2016-09-21 17:01:19 +01:00
										 |  |  | 	// Host represents a host from the Corefile, may contain port.
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 	Host string
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Normalize will return the host portion of host, stripping
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | // of any port or transport. The host will also be fully qualified and lowercased.
 | 
					
						
							| 
									
										
										
										
											2021-05-27 07:26:14 -04:00
										 |  |  | // An empty string is returned on failure
 | 
					
						
							|  |  |  | // Deprecated: use OriginsFromArgsOrServerBlock or NormalizeExact
 | 
					
						
							|  |  |  | func (h Host) Normalize() string {
 | 
					
						
							|  |  |  | 	var caller string
 | 
					
						
							|  |  |  | 	if _, file, line, ok := runtime.Caller(1); ok {
 | 
					
						
							|  |  |  | 		caller = fmt.Sprintf("(%v line %d) ", file, line)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	log.Warning("An external plugin " + caller + "is using the deprecated function Normalize. " +
 | 
					
						
							|  |  |  | 		"This will be removed in a future versions of CoreDNS. The plugin should be updated to use " +
 | 
					
						
							|  |  |  | 		"OriginsFromArgsOrServerBlock or NormalizeExact instead.")
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	s := string(h)
 | 
					
						
							|  |  |  | 	_, s = parse.Transport(s)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// The error can be ignored here, because this function is called after the corefile has already been vetted.
 | 
					
						
							|  |  |  | 	hosts, _, err := SplitHostPort(s)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return ""
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return Name(hosts[0]).Normalize()
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // MustNormalize will return the host portion of host, stripping
 | 
					
						
							|  |  |  | // of any port or transport. The host will also be fully qualified and lowercased.
 | 
					
						
							|  |  |  | // An error is returned on error
 | 
					
						
							|  |  |  | // Deprecated: use OriginsFromArgsOrServerBlock or NormalizeExact
 | 
					
						
							|  |  |  | func (h Host) MustNormalize() (string, error) {
 | 
					
						
							|  |  |  | 	var caller string
 | 
					
						
							|  |  |  | 	if _, file, line, ok := runtime.Caller(1); ok {
 | 
					
						
							|  |  |  | 		caller = fmt.Sprintf("(%v line %d) ", file, line)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	log.Warning("An external plugin " + caller + "is using the deprecated function MustNormalize. " +
 | 
					
						
							|  |  |  | 		"This will be removed in a future versions of CoreDNS. The plugin should be updated to use " +
 | 
					
						
							|  |  |  | 		"OriginsFromArgsOrServerBlock or NormalizeExact instead.")
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	s := string(h)
 | 
					
						
							|  |  |  | 	_, s = parse.Transport(s)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// The error can be ignored here, because this function is called after the corefile has already been vetted.
 | 
					
						
							|  |  |  | 	hosts, _, err := SplitHostPort(s)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return "", err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return Name(hosts[0]).Normalize(), nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NormalizeExact will return the host portion of host, stripping
 | 
					
						
							|  |  |  | // of any port or transport. The host will also be fully qualified and lowercased.
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | // An empty slice is returned on failure
 | 
					
						
							| 
									
										
										
										
											2021-05-27 07:26:14 -04:00
										 |  |  | func (h Host) NormalizeExact() []string {
 | 
					
						
							| 
									
										
										
										
											2019-10-19 03:08:14 -04:00
										 |  |  | 	// The error can be ignored here, because this function should only be called after the corefile has already been vetted.
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	s := string(h)
 | 
					
						
							| 
									
										
										
										
											2018-09-19 08:16:04 +01:00
										 |  |  | 	_, s = parse.Transport(s)
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | 	hosts, _, err := SplitHostPort(s)
 | 
					
						
							| 
									
										
										
										
											2019-10-19 03:08:14 -04:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | 		return nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	for i := range hosts {
 | 
					
						
							|  |  |  | 		hosts[i] = Name(hosts[i]).Normalize()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-19 03:08:14 -04:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | 	return hosts
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | // SplitHostPort splits s up in a host(s) and port portion, taking reverse address notation into account.
 | 
					
						
							|  |  |  | // String the string s should *not* be prefixed with any protocols, i.e. dns://. SplitHostPort can return
 | 
					
						
							|  |  |  | // multiple hosts when a reverse notation on a non-octet boundary is given.
 | 
					
						
							|  |  |  | func SplitHostPort(s string) (hosts []string, port string, err error) {
 | 
					
						
							| 
									
										
										
										
											2017-08-07 13:24:09 -07:00
										 |  |  | 	// If there is: :[0-9]+ on the end we assume this is the port. This works for (ascii) domain
 | 
					
						
							|  |  |  | 	// names and our reverse syntax, which always needs a /mask *before* the port.
 | 
					
						
							| 
									
										
										
										
											2019-08-21 16:08:55 -04:00
										 |  |  | 	// So from the back, find first colon, and then check if it's a number.
 | 
					
						
							| 
									
										
										
										
											2017-08-07 13:24:09 -07:00
										 |  |  | 	colon := strings.LastIndex(s, ":")
 | 
					
						
							|  |  |  | 	if colon == len(s)-1 {
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | 		return nil, "", fmt.Errorf("expecting data after last colon: %q", s)
 | 
					
						
							| 
									
										
										
										
											2017-08-07 13:24:09 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	if colon != -1 {
 | 
					
						
							|  |  |  | 		if p, err := strconv.Atoi(s[colon+1:]); err == nil {
 | 
					
						
							|  |  |  | 			port = strconv.Itoa(p)
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | 			s = s[:colon]
 | 
					
						
							| 
									
										
										
										
											2017-08-07 13:24:09 -07:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// TODO(miek): this should take escaping into account.
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | 	if len(s) > 255 {
 | 
					
						
							|  |  |  | 		return nil, "", fmt.Errorf("specified zone is too long: %d > 255", len(s))
 | 
					
						
							| 
									
										
										
										
											2017-08-07 13:24:09 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | 	if _, ok := dns.IsDomainName(s); !ok {
 | 
					
						
							|  |  |  | 		return nil, "", fmt.Errorf("zone is not a valid domain name: %s", s)
 | 
					
						
							| 
									
										
										
										
											2017-08-07 13:24:09 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-24 10:16:03 +01:00
										 |  |  | 	// Check if it parses as a reverse zone, if so we use that. Must be fully specified IP and mask.
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | 	_, n, err := net.ParseCIDR(s)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return []string{s}, port, nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-04 04:17:17 -04:00
										 |  |  | 	if s[0] == ':' || (s[0] == '0' && strings.Contains(s, ":")) {
 | 
					
						
							|  |  |  | 		return nil, "", fmt.Errorf("invalid CIDR %s", s)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | 	// now check if multiple hosts must be returned.
 | 
					
						
							| 
									
										
										
										
											2021-06-04 04:17:17 -04:00
										 |  |  | 	nets := cidr.Split(n)
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | 	hosts = cidr.Reverse(nets)
 | 
					
						
							|  |  |  | 	return hosts, port, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // OriginsFromArgsOrServerBlock returns the normalized args if that slice
 | 
					
						
							|  |  |  | // is not empty, otherwise the serverblock slice is returned (in a newly copied slice).
 | 
					
						
							|  |  |  | func OriginsFromArgsOrServerBlock(args, serverblock []string) []string {
 | 
					
						
							|  |  |  | 	if len(args) == 0 {
 | 
					
						
							|  |  |  | 		s := make([]string, len(serverblock))
 | 
					
						
							|  |  |  | 		copy(s, serverblock)
 | 
					
						
							|  |  |  | 		for i := range s {
 | 
					
						
							| 
									
										
										
										
											2021-05-27 07:26:14 -04:00
										 |  |  | 			s[i] = Host(s[i]).NormalizeExact()[0] // expansion of these already happened in dnsserver/register.go
 | 
					
						
							| 
									
										
										
										
											2017-08-07 13:24:09 -07:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | 		return s
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	s := []string{}
 | 
					
						
							|  |  |  | 	for i := range args {
 | 
					
						
							| 
									
										
										
										
											2021-05-27 07:26:14 -04:00
										 |  |  | 		sx := Host(args[i]).NormalizeExact()
 | 
					
						
							| 
									
										
										
										
											2021-05-19 19:38:37 +02:00
										 |  |  | 		if len(sx) == 0 {
 | 
					
						
							|  |  |  | 			continue // silently ignores errors.
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		s = append(s, sx...)
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return s
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | }
 |