| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package file
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	"errors"
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	"io"
 | 
					
						
							|  |  |  | 	"log"
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							| 
									
										
										
										
											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 (
 | 
					
						
							|  |  |  | 	File struct {
 | 
					
						
							|  |  |  | 		Next  middleware.Handler
 | 
					
						
							|  |  |  | 		Zones Zones
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Zones struct {
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 		Z     map[string]*Zone
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		Names []string
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | func (f File) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							|  |  |  | 	state := middleware.State{W: w, Req: r}
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-04 15:45:17 +01:00
										 |  |  | 	if state.QClass() != dns.ClassINET {
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 		return dns.RcodeServerFailure, errors.New("can only deal with ClassINET")
 | 
					
						
							| 
									
										
										
										
											2016-04-04 15:45:17 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 	qname := state.Name()
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	zone := middleware.Zones(f.Zones.Names).Matches(qname)
 | 
					
						
							|  |  |  | 	if zone == "" {
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 		if f.Next != nil {
 | 
					
						
							|  |  |  | 			return f.Next.ServeDNS(ctx, w, r)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return dns.RcodeServerFailure, errors.New("no next middleware found")
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	z, ok := f.Zones.Z[zone]
 | 
					
						
							|  |  |  | 	if !ok {
 | 
					
						
							|  |  |  | 		return f.Next.ServeDNS(ctx, w, r)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 	if z == nil {
 | 
					
						
							|  |  |  | 		return dns.RcodeServerFailure, nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											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-03-29 13:22:17 +00:00
										 |  |  | 	answer, ns, extra, result := z.Lookup(qname, state.QType(), state.Do())
 | 
					
						
							| 
									
										
										
										
											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-03-27 07:37:23 +01:00
										 |  |  | // Parse parses the zone in filename and returns a new Zone or an error.
 | 
					
						
							|  |  |  | func Parse(f io.Reader, origin, fileName string) (*Zone, error) {
 | 
					
						
							|  |  |  | 	tokens := dns.ParseZone(f, dns.Fqdn(origin), fileName)
 | 
					
						
							| 
									
										
										
										
											2016-04-15 14:26:27 +01:00
										 |  |  | 	z := NewZone(origin, fileName)
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	for x := range tokens {
 | 
					
						
							|  |  |  | 		if x.Error != nil {
 | 
					
						
							| 
									
										
										
										
											2016-04-14 07:33:03 +01:00
										 |  |  | 			log.Printf("[ERROR] Failed to parse `%s': %v", origin, x.Error)
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 			return nil, x.Error
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00: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
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	return z, nil
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 |