| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | 	// normalize zones, validation is almost done by dnsserver
 | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 	// TODO(miek): need sane helpers for these.
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 	zones := make([]string, len(c.ServerBlockKeys))
 | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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() {
 | 
					
						
							|  |  |  | 		if c.Val() == "reverse" {
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var cidrs []*net.IPNet
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// parse all networks
 | 
					
						
							| 
									
										
										
										
											2017-02-10 12:48:51 +00:00
										 |  |  | 			for _, cidr := range c.RemainingArgs() {
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 				if cidr == "{" {
 | 
					
						
							|  |  |  | 					break
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				_, ipnet, err := net.ParseCIDR(cidr)
 | 
					
						
							|  |  |  | 				if err != nil {
 | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 					return nil, false, c.Errf("network needs to be CIDR formatted: %q\n", cidr)
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 				}
 | 
					
						
							|  |  |  | 				cidrs = append(cidrs, ipnet)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			if len(cidrs) == 0 {
 | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 				return nil, false, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// set defaults
 | 
					
						
							|  |  |  | 			var (
 | 
					
						
							| 
									
										
										
										
											2017-02-10 12:48:51 +00:00
										 |  |  | 				template = "ip-" + templateNameIP + ".{zone[1]}"
 | 
					
						
							|  |  |  | 				ttl      = 60
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 			)
 | 
					
						
							|  |  |  | 			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
										 |  |  | 					}
 | 
					
						
							|  |  |  | 					template = c.Val()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				case "ttl":
 | 
					
						
							|  |  |  | 					if !c.NextArg() {
 | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 						return nil, false, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 					}
 | 
					
						
							|  |  |  | 					ttl, err = strconv.Atoi(c.Val())
 | 
					
						
							|  |  |  | 					if err != nil {
 | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 						return nil, false, err
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 					}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				case "fallthrough":
 | 
					
						
							|  |  |  | 					fall = true
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				default:
 | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 					return nil, false, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 				}
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// prepare template
 | 
					
						
							|  |  |  | 			// replace {zone[index]} by the listen zone/domain of this config block
 | 
					
						
							|  |  |  | 			for i, zone := range zones {
 | 
					
						
							| 
									
										
										
										
											2017-02-10 12:48:51 +00:00
										 |  |  | 				// 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)
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 			if !strings.HasSuffix(template, ".") {
 | 
					
						
							|  |  |  | 				template += "."
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// extract zone from template
 | 
					
						
							|  |  |  | 			templateZone := strings.SplitAfterN(template, ".", 2)
 | 
					
						
							|  |  |  | 			if len(templateZone) != 2 || templateZone[1] == "" {
 | 
					
						
							| 
									
										
										
										
											2017-02-20 21:00:00 +00:00
										 |  |  | 				return nil, false, c.Errf("cannot find domain in template '%v'", template)
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// 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
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				regex, err := regexp.Compile(
 | 
					
						
							|  |  |  | 					"^" + strings.Replace( // inject ip regex into template
 | 
					
						
							|  |  |  | 						regexp.QuoteMeta(template), // escape dots
 | 
					
						
							|  |  |  | 						regexp.QuoteMeta(templateNameIP),
 | 
					
						
							|  |  |  | 						regexIP,
 | 
					
						
							| 
									
										
										
										
											2017-02-10 12:48:51 +00:00
										 |  |  | 						1) + "$")
 | 
					
						
							| 
									
										
										
										
											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-02-20 21:00:00 +00:00
										 |  |  | 				nets = append(nets, network{
 | 
					
						
							| 
									
										
										
										
											2017-02-10 12:48:51 +00:00
										 |  |  | 					IPnet:        ipnet,
 | 
					
						
							|  |  |  | 					Zone:         templateZone[1],
 | 
					
						
							|  |  |  | 					Template:     template,
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 					RegexMatchIP: regex,
 | 
					
						
							| 
									
										
										
										
											2017-02-10 12:48:51 +00:00
										 |  |  | 					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
										 |  |  | }
 |