| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | package reverse
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"net"
 | 
					
						
							| 
									
										
										
										
											2017-02-10 12:48:51 +00:00
										 |  |  | 	"regexp"
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 	"sort"
 | 
					
						
							|  |  |  | 	"strconv"
 | 
					
						
							| 
									
										
										
										
											2017-02-10 12:48:51 +00:00
										 |  |  | 	"strings"
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/middleware"
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() {
 | 
					
						
							|  |  |  | 	caddy.RegisterPlugin("reverse", caddy.Plugin{
 | 
					
						
							|  |  |  | 		ServerType: "dns",
 | 
					
						
							|  |  |  | 		Action:     setupReverse,
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func setupReverse(c *caddy.Controller) error {
 | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 	networks, fallThrough, err := reverseParse(c)
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return middleware.Error("reverse", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dnsserver.GetConfig(c).AddMiddleware(func(next middleware.Handler) middleware.Handler {
 | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 		return Reverse{Next: next, Networks: networks, Fallthrough: fallThrough}
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | func reverseParse(c *caddy.Controller) (nets networks, fall bool, err error) {
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 	zones := make([]string, len(c.ServerBlockKeys))
 | 
					
						
							| 
									
										
										
										
											2017-07-11 00:49:24 -04:00
										 |  |  | 	wildcard := false
 | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 	// We copy from the serverblock, these contains Hosts.
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 	for i, str := range c.ServerBlockKeys {
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 		zones[i] = middleware.Host(str).Normalize()
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for c.Next() {
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 		var cidrs []*net.IPNet
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 		// parse all networks
 | 
					
						
							|  |  |  | 		for _, cidr := range c.RemainingArgs() {
 | 
					
						
							|  |  |  | 			if cidr == "{" {
 | 
					
						
							|  |  |  | 				break
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 			_, ipnet, err := net.ParseCIDR(cidr)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				return nil, false, c.Errf("network needs to be CIDR formatted: %q\n", cidr)
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 			cidrs = append(cidrs, ipnet)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if len(cidrs) == 0 {
 | 
					
						
							|  |  |  | 			return nil, false, c.ArgErr()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 		// set defaults
 | 
					
						
							|  |  |  | 		var (
 | 
					
						
							|  |  |  | 			template = "ip-" + templateNameIP + ".{zone[1]}"
 | 
					
						
							|  |  |  | 			ttl      = 60
 | 
					
						
							|  |  |  | 		)
 | 
					
						
							|  |  |  | 		for c.NextBlock() {
 | 
					
						
							|  |  |  | 			switch c.Val() {
 | 
					
						
							|  |  |  | 			case "hostname":
 | 
					
						
							|  |  |  | 				if !c.NextArg() {
 | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 					return nil, false, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 				template = c.Val()
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 			case "ttl":
 | 
					
						
							|  |  |  | 				if !c.NextArg() {
 | 
					
						
							|  |  |  | 					return nil, false, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2017-07-11 00:49:24 -04:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 				ttl, err = strconv.Atoi(c.Val())
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 				if err != nil {
 | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 					return nil, false, err
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 				}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 			case "wildcard":
 | 
					
						
							|  |  |  | 				wildcard = true
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			case "fallthrough":
 | 
					
						
							|  |  |  | 				fall = true
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			default:
 | 
					
						
							|  |  |  | 				return nil, false, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// prepare template
 | 
					
						
							|  |  |  | 		// replace {zone[index]} by the listen zone/domain of this config block
 | 
					
						
							|  |  |  | 		for i, zone := range zones {
 | 
					
						
							|  |  |  | 			// TODO: we should be smarter about actually replacing this. This works, but silently allows "zone[-1]"
 | 
					
						
							|  |  |  | 			// for instance.
 | 
					
						
							|  |  |  | 			template = strings.Replace(template, "{zone["+strconv.Itoa(i+1)+"]}", zone, 1)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if !strings.HasSuffix(template, ".") {
 | 
					
						
							|  |  |  | 			template += "."
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// extract zone from template
 | 
					
						
							|  |  |  | 		templateZone := strings.SplitAfterN(template, ".", 2)
 | 
					
						
							|  |  |  | 		if len(templateZone) != 2 || templateZone[1] == "" {
 | 
					
						
							|  |  |  | 			return nil, false, c.Errf("cannot find domain in template '%v'", template)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Create for each configured network in this stanza
 | 
					
						
							|  |  |  | 		for _, ipnet := range cidrs {
 | 
					
						
							|  |  |  | 			// precompile regex for hostname to ip matching
 | 
					
						
							|  |  |  | 			regexIP := regexMatchV4
 | 
					
						
							|  |  |  | 			if ipnet.IP.To4() == nil {
 | 
					
						
							|  |  |  | 				regexIP = regexMatchV6
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			prefix := "^"
 | 
					
						
							|  |  |  | 			if wildcard {
 | 
					
						
							|  |  |  | 				prefix += ".*"
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			regex, err := regexp.Compile(
 | 
					
						
							|  |  |  | 				prefix + strings.Replace( // inject ip regex into template
 | 
					
						
							|  |  |  | 					regexp.QuoteMeta(template), // escape dots
 | 
					
						
							|  |  |  | 					regexp.QuoteMeta(templateNameIP),
 | 
					
						
							|  |  |  | 					regexIP,
 | 
					
						
							|  |  |  | 					1) + "$")
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				return nil, false, err
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			nets = append(nets, network{
 | 
					
						
							|  |  |  | 				IPnet:        ipnet,
 | 
					
						
							|  |  |  | 				Zone:         templateZone[1],
 | 
					
						
							|  |  |  | 				Template:     template,
 | 
					
						
							|  |  |  | 				RegexMatchIP: regex,
 | 
					
						
							|  |  |  | 				TTL:          uint32(ttl),
 | 
					
						
							|  |  |  | 			})
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// sort by cidr
 | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 	sort.Sort(nets)
 | 
					
						
							|  |  |  | 	return nets, fall, nil
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | }
 |