| 
									
										
										
										
											2016-09-25 08:39:20 +01:00
										 |  |  | // Package file implements a file backend.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package file
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2017-06-08 18:43:11 +01:00
										 |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	"io"
 | 
					
						
							|  |  |  | 	"log"
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	"golang.org/x/net/context"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type (
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	// File is the plugin that reads zone data from disk.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	File struct {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		Next  plugin.Handler
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		Zones Zones
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 	// Zones maps zone names to a *Zone.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	Zones struct {
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		Z     map[string]*Zone // A map mapping zone (origin) to the Zone's data
 | 
					
						
							|  |  |  | 		Names []string         // All the keys from the map Z as a string slice.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // ServeDNS implements the plugin.Handle interface.
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | func (f File) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	state := request.Request{W: w, Req: r}
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 	qname := state.Name()
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	// TODO(miek): match the qname better in the map
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	zone := plugin.Zones(f.Zones.Names).Matches(qname)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	if zone == "" {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		return plugin.NextOrFailure(f.Name(), f.Next, ctx, w, r)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-11-07 11:12:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	z, ok := f.Zones.Z[zone]
 | 
					
						
							| 
									
										
										
										
											2016-11-07 11:12:20 +00:00
										 |  |  | 	if !ok || z == nil {
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 		return dns.RcodeServerFailure, nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// This is only for when we are a secondary zones.
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 	if r.Opcode == dns.OpcodeNotify {
 | 
					
						
							|  |  |  | 		if z.isNotify(state) {
 | 
					
						
							|  |  |  | 			m := new(dns.Msg)
 | 
					
						
							|  |  |  | 			m.SetReply(r)
 | 
					
						
							|  |  |  | 			m.Authoritative, m.RecursionAvailable, m.Compress = true, true, true
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | 			state.SizeAndDo(m)
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 			w.WriteMsg(m)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 			log.Printf("[INFO] Notify from %s for %s: checking transfer", state.IP(), zone)
 | 
					
						
							|  |  |  | 			ok, err := z.shouldTransfer()
 | 
					
						
							|  |  |  | 			if ok {
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 				z.TransferIn()
 | 
					
						
							| 
									
										
										
										
											2016-04-06 22:29:33 +01:00
										 |  |  | 			} else {
 | 
					
						
							|  |  |  | 				log.Printf("[INFO] Notify from %s for %s: no serial increase seen", state.IP(), zone)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				log.Printf("[WARNING] Notify from %s for %s: failed primary check: %s", state.IP(), zone, err)
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 			return dns.RcodeSuccess, nil
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		log.Printf("[INFO] Dropping notify from %s for %s", state.IP(), zone)
 | 
					
						
							|  |  |  | 		return dns.RcodeSuccess, nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 	if z.Expired != nil && *z.Expired {
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 		log.Printf("[ERROR] Zone %s is expired", zone)
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 		return dns.RcodeServerFailure, nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-05 15:54:06 +01:00
										 |  |  | 	if state.QType() == dns.TypeAXFR || state.QType() == dns.TypeIXFR {
 | 
					
						
							| 
									
										
										
										
											2016-03-28 12:08:05 +01:00
										 |  |  | 		xfr := Xfr{z}
 | 
					
						
							|  |  |  | 		return xfr.ServeDNS(ctx, w, r)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-10 07:48:47 +00:00
										 |  |  | 	answer, ns, extra, result := z.Lookup(state, qname)
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	m := new(dns.Msg)
 | 
					
						
							|  |  |  | 	m.SetReply(r)
 | 
					
						
							|  |  |  | 	m.Authoritative, m.RecursionAvailable, m.Compress = true, true, true
 | 
					
						
							| 
									
										
										
										
											2016-04-16 16:16:52 +01:00
										 |  |  | 	m.Answer, m.Ns, m.Extra = answer, ns, extra
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	switch result {
 | 
					
						
							|  |  |  | 	case Success:
 | 
					
						
							| 
									
										
										
										
											2016-04-16 16:16:52 +01:00
										 |  |  | 	case NoData:
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	case NameError:
 | 
					
						
							|  |  |  | 		m.Rcode = dns.RcodeNameError
 | 
					
						
							| 
									
										
										
										
											2016-04-16 16:16:52 +01:00
										 |  |  | 	case Delegation:
 | 
					
						
							|  |  |  | 		m.Authoritative = false
 | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | 	case ServerFailure:
 | 
					
						
							|  |  |  | 		return dns.RcodeServerFailure, nil
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	state.SizeAndDo(m)
 | 
					
						
							| 
									
										
										
										
											2016-03-28 11:29:50 +01:00
										 |  |  | 	m, _ = state.Scrub(m)
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	w.WriteMsg(m)
 | 
					
						
							|  |  |  | 	return dns.RcodeSuccess, nil
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 11:48:37 +00:00
										 |  |  | // Name implements the Handler interface.
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | func (f File) Name() string { return "file" }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | // Parse parses the zone in filename and returns a new Zone or an error.
 | 
					
						
							| 
									
										
										
										
											2017-06-08 18:43:11 +01:00
										 |  |  | // If serial >= 0 it will reload the zone, if the SOA hasn't changed
 | 
					
						
							|  |  |  | // it returns an error indicating nothing was read.
 | 
					
						
							|  |  |  | func Parse(f io.Reader, origin, fileName string, serial int64) (*Zone, error) {
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	tokens := dns.ParseZone(f, dns.Fqdn(origin), fileName)
 | 
					
						
							| 
									
										
										
										
											2016-04-15 14:26:27 +01:00
										 |  |  | 	z := NewZone(origin, fileName)
 | 
					
						
							| 
									
										
										
										
											2017-06-08 18:43:11 +01:00
										 |  |  | 	seenSOA := false
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	for x := range tokens {
 | 
					
						
							|  |  |  | 		if x.Error != nil {
 | 
					
						
							|  |  |  | 			return nil, x.Error
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-06-08 18:43:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if !seenSOA && serial >= 0 {
 | 
					
						
							|  |  |  | 			if s, ok := x.RR.(*dns.SOA); ok {
 | 
					
						
							|  |  |  | 				if s.Serial == uint32(serial) { // same zone
 | 
					
						
							|  |  |  | 					return nil, fmt.Errorf("no change in serial: %d", serial)
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:51:52 -07:00
										 |  |  | 				seenSOA = true
 | 
					
						
							| 
									
										
										
										
											2017-06-08 18:43:11 +01:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-15 14:26:27 +01:00
										 |  |  | 		if err := z.Insert(x.RR); err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-04-14 07:33:03 +01:00
										 |  |  | 			return nil, err
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:51:52 -07:00
										 |  |  | 	if !seenSOA {
 | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("file %q has no SOA record", fileName)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	return z, nil
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 |