| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2017-08-07 13:24:09 -07:00
										 |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 08:16:04 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/parse" | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-21 17:01:19 +01:00
										 |  |  | // Zones respresents a lists of zone names. | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | type Zones []string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Matches checks is qname is a subdomain of any of the zones in z.  The match | 
					
						
							|  |  |  | // will return the most specific zones that matches other. The empty string | 
					
						
							|  |  |  | // 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. | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func (h Host) Normalize() string { | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | 	// The error can be ignore here, because this function is called after the corefile has already been vetted. | 
					
						
							| 
									
										
										
										
											2017-10-24 10:16:03 +01:00
										 |  |  | 	host, _, _, _ := SplitHostPort(s) | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	return Name(host).Normalize() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 13:24:09 -07:00
										 |  |  | // SplitHostPort splits s up in a host and port portion, taking reverse address notation into account. | 
					
						
							| 
									
										
										
										
											2017-10-24 10:16:03 +01:00
										 |  |  | // String the string s should *not* be prefixed with any protocols, i.e. dns://. The returned ipnet is the | 
					
						
							|  |  |  | // *net.IPNet that is used when the zone is a reverse and a netmask is given. | 
					
						
							|  |  |  | func SplitHostPort(s string) (host, port string, ipnet *net.IPNet, 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. | 
					
						
							|  |  |  | 	// So from the back, find first colon, and then check if its a number. | 
					
						
							|  |  |  | 	host = s | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	colon := strings.LastIndex(s, ":") | 
					
						
							|  |  |  | 	if colon == len(s)-1 { | 
					
						
							| 
									
										
										
										
											2017-10-24 10:16:03 +01: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) | 
					
						
							|  |  |  | 			host = s[:colon] | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// TODO(miek): this should take escaping into account. | 
					
						
							|  |  |  | 	if len(host) > 255 { | 
					
						
							| 
									
										
										
										
											2017-10-24 10:16:03 +01:00
										 |  |  | 		return "", "", nil, fmt.Errorf("specified zone is too long: %d > 255", len(host)) | 
					
						
							| 
									
										
										
										
											2017-08-07 13:24:09 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_, d := dns.IsDomainName(host) | 
					
						
							|  |  |  | 	if !d { | 
					
						
							| 
									
										
										
										
											2017-10-24 10:16:03 +01:00
										 |  |  | 		return "", "", nil, fmt.Errorf("zone is not a valid domain name: %s", host) | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							|  |  |  | 	ip, n, err := net.ParseCIDR(host) | 
					
						
							|  |  |  | 	ones, bits := 0, 0 | 
					
						
							| 
									
										
										
										
											2017-08-07 13:24:09 -07:00
										 |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		if rev, e := dns.ReverseAddr(ip.String()); e == nil { | 
					
						
							| 
									
										
										
										
											2017-10-24 10:16:03 +01:00
										 |  |  | 			ones, bits = n.Mask.Size() | 
					
						
							| 
									
										
										
										
											2018-01-23 10:58:36 -05:00
										 |  |  | 			// get the size, in bits, of each portion of hostname defined in the reverse address. (8 for IPv4, 4 for IPv6) | 
					
						
							|  |  |  | 			sizeDigit := 8 | 
					
						
							|  |  |  | 			if len(n.IP) == net.IPv6len { | 
					
						
							|  |  |  | 				sizeDigit = 4 | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-10-24 10:16:03 +01:00
										 |  |  | 			// Get the first lower octet boundary to see what encompassing zone we should be authoritative for. | 
					
						
							| 
									
										
										
										
											2018-01-23 10:58:36 -05:00
										 |  |  | 			mod := (bits - ones) % sizeDigit | 
					
						
							| 
									
										
										
										
											2017-10-24 10:16:03 +01:00
										 |  |  | 			nearest := (bits - ones) + mod | 
					
						
							| 
									
										
										
										
											2018-07-28 17:32:13 +08:00
										 |  |  | 			offset := 0 | 
					
						
							|  |  |  | 			var end bool | 
					
						
							| 
									
										
										
										
											2018-01-23 10:58:36 -05:00
										 |  |  | 			for i := 0; i < nearest/sizeDigit; i++ { | 
					
						
							| 
									
										
										
										
											2017-10-24 10:16:03 +01:00
										 |  |  | 				offset, end = dns.NextLabel(rev, offset) | 
					
						
							|  |  |  | 				if end { | 
					
						
							|  |  |  | 					break | 
					
						
							| 
									
										
										
										
											2017-08-07 13:24:09 -07:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-10-24 10:16:03 +01:00
										 |  |  | 			host = rev[offset:] | 
					
						
							| 
									
										
										
										
											2017-08-07 13:24:09 -07:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-24 10:16:03 +01:00
										 |  |  | 	return host, port, n, nil | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | } |