| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | /* | 
					
						
							| 
									
										
										
										
											2017-08-24 08:56:48 +01:00
										 |  |  |  | Package autopath implements autopathing. This is a hack; it shortcuts the | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  |  | client's search path resolution by performing these lookups on the server... | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | The server has a copy (via AutoPathFunc) of the client's search path and on | 
					
						
							|  |  |  |  | receiving a query it first establish if the suffix matches the FIRST configured | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  |  | element. If no match can be found the query will be forwarded up the plugin | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | chain without interference (iff 'fallthrough' has been set). | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | If the query is deemed to fall in the search path the server will perform the | 
					
						
							|  |  |  |  | queries with each element of the search path appended in sequence until a | 
					
						
							|  |  |  |  | non-NXDOMAIN answer has been found. That reply will then be returned to the | 
					
						
							|  |  |  |  | client - with some CNAME hackery to let the client accept the reply. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | If all queries return NXDOMAIN we return the original as-is and let the client | 
					
						
							|  |  |  |  | continue searching. The client will go to the next element in the search path, | 
					
						
							|  |  |  |  | but we won’t do any more autopathing. It means that in the failure case, you do | 
					
						
							|  |  |  |  | more work, since the server looks it up, then the client still needs to go | 
					
						
							|  |  |  |  | through the search path. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | It is assume the search path ordering is identical between server and client. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | Midldeware implementing autopath, must have a function called `AutoPath` of type | 
					
						
							| 
									
										
										
										
											2017-08-24 08:56:48 +01:00
										 |  |  |  | autopath.Func. Note the searchpath must be ending with the empty string. | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | I.e: | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-16 14:13:28 +01:00
										 |  |  |  | func (m Plugins ) AutoPath(state request.Request) []string { | 
					
						
							| 
									
										
										
										
											2017-08-10 19:26:31 +01:00
										 |  |  |  | 	return []string{"first", "second", "last", ""} | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-08-18 14:45:20 +01:00
										 |  |  |  | package autopath | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  |  | 	"github.com/coredns/coredns/plugin" | 
					
						
							|  |  |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnsutil" | 
					
						
							|  |  |  |  | 	"github.com/coredns/coredns/plugin/pkg/nonwriter" | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 	"github.com/coredns/coredns/request" | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	"github.com/miekg/dns" | 
					
						
							|  |  |  |  | 	"golang.org/x/net/context" | 
					
						
							|  |  |  |  | ) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  |  | // Func defines the function plugin should implement to return a search | 
					
						
							|  |  |  |  | // path to the autopath plugin. The last element of the slice must be the empty string. | 
					
						
							| 
									
										
										
										
											2017-08-24 08:56:48 +01:00
										 |  |  |  | // If Func returns a nil slice, no autopathing will be done. | 
					
						
							|  |  |  |  | type Func func(request.Request) []string | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:40:30 -05:00
										 |  |  |  | // AutoPather defines the interface that a plugin should implement in order to be | 
					
						
							|  |  |  |  | // used by AutoPath. | 
					
						
							|  |  |  |  | type AutoPather interface { | 
					
						
							|  |  |  |  | 	AutoPath(request.Request) []string | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-11 19:48:11 -07:00
										 |  |  |  | // AutoPath perform autopath: service side search path completion. | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | type AutoPath struct { | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  |  | 	Next  plugin.Handler | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 	Zones []string | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// Search always includes "" as the last element, so we try the base query with out any search paths added as well. | 
					
						
							|  |  |  |  | 	search     []string | 
					
						
							| 
									
										
										
										
											2017-08-24 08:56:48 +01:00
										 |  |  |  | 	searchFunc Func | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  |  | // ServeDNS implements the plugin.Handle interface. | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | func (a *AutoPath) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { | 
					
						
							|  |  |  |  | 	state := request.Request{W: w, Req: r} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  |  | 	zone := plugin.Zones(a.Zones).Matches(state.Name()) | 
					
						
							| 
									
										
										
										
											2017-08-10 19:27:54 +01:00
										 |  |  |  | 	if zone == "" { | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  |  | 		return plugin.NextOrFailure(a.Name(), a.Next, ctx, w, r) | 
					
						
							| 
									
										
										
										
											2017-08-10 19:27:54 +01:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  |  | 	// Check if autopath should be done, searchFunc takes precedence over the local configured search path. | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 	var err error | 
					
						
							|  |  |  |  | 	searchpath := a.search | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 	if a.searchFunc != nil { | 
					
						
							| 
									
										
										
										
											2017-08-10 19:26:31 +01:00
										 |  |  |  | 		searchpath = a.searchFunc(state) | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  |  | 	if len(searchpath) == 0 { | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  |  | 		return plugin.NextOrFailure(a.Name(), a.Next, ctx, w, r) | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if !firstInSearchPath(state.Name(), searchpath) { | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  |  | 		return plugin.NextOrFailure(a.Name(), a.Next, ctx, w, r) | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	origQName := state.QName() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// Establish base name of the query. I.e what was originally asked. | 
					
						
							| 
									
										
										
										
											2017-10-15 19:39:24 +02:00
										 |  |  |  | 	base, err := dnsutil.TrimZone(state.QName(), searchpath[0]) | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 	if err != nil { | 
					
						
							|  |  |  |  | 		return dns.RcodeServerFailure, err | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	firstReply := new(dns.Msg) | 
					
						
							|  |  |  |  | 	firstRcode := 0 | 
					
						
							|  |  |  |  | 	var firstErr error | 
					
						
							| 
									
										
										
										
											2017-08-10 19:27:54 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	ar := r.Copy() | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 	// Walk the search path and see if we can get a non-nxdomain - if they all fail we return the first | 
					
						
							|  |  |  |  | 	// query we've done and return that as-is. This means the client will do the search path walk again... | 
					
						
							|  |  |  |  | 	for i, s := range searchpath { | 
					
						
							|  |  |  |  | 		newQName := base + "." + s | 
					
						
							| 
									
										
										
										
											2017-08-10 19:27:54 +01:00
										 |  |  |  | 		ar.Question[0].Name = newQName | 
					
						
							| 
									
										
										
										
											2017-08-19 17:28:42 +01:00
										 |  |  |  | 		nw := nonwriter.New(w) | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  |  | 		rcode, err := plugin.NextOrFailure(a.Name(), a.Next, ctx, nw, ar) | 
					
						
							| 
									
										
										
										
											2017-08-10 19:27:54 +01:00
										 |  |  |  | 		if err != nil { | 
					
						
							|  |  |  |  | 			// Return now - not sure if this is the best. We should also check if the write has happened. | 
					
						
							|  |  |  |  | 			return rcode, err | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 		if i == 0 { | 
					
						
							|  |  |  |  | 			firstReply = nw.Msg | 
					
						
							|  |  |  |  | 			firstRcode = rcode | 
					
						
							|  |  |  |  | 			firstErr = err | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  |  | 		if !plugin.ClientWrite(rcode) { | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 			continue | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if nw.Msg.Rcode == dns.RcodeNameError { | 
					
						
							|  |  |  |  | 			continue | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		msg := nw.Msg | 
					
						
							|  |  |  |  | 		cnamer(msg, origQName) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		// Write whatever non-nxdomain answer we've found. | 
					
						
							|  |  |  |  | 		w.WriteMsg(msg) | 
					
						
							| 
									
										
										
										
											2017-10-15 19:39:24 +02:00
										 |  |  |  | 		AutoPathCount.WithLabelValues().Add(1) | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 		return rcode, err | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  |  | 	if plugin.ClientWrite(firstRcode) { | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 		w.WriteMsg(firstReply) | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	return firstRcode, firstErr | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  |  | // Name implements the Handler interface. | 
					
						
							|  |  |  |  | func (a *AutoPath) Name() string { return "autopath" } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // firstInSearchPath checks if name is equal to are a sibling of the first element in the search path. | 
					
						
							|  |  |  |  | func firstInSearchPath(name string, searchpath []string) bool { | 
					
						
							|  |  |  |  | 	if name == searchpath[0] { | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 		return true | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  |  | 	if dns.IsSubDomain(searchpath[0], name) { | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  |  | 		return true | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	return false | 
					
						
							|  |  |  |  | } |