Every plugin needs to deal with EDNS0 and should call Scrub to make a message fit the client's buffer. Move this functionality into the server and wrapping the ResponseWriter into a ScrubWriter that handles these bits for us. Result: Less code and faster, because multiple chained plugins could all be calling scrub and SizeAndDo - now there is just one place. Most tests in file/* and dnssec/* needed adjusting because in those unit tests you don't see OPT RRs anymore. The DNSSEC signer was also looking at the returned OPT RR to see if it needed to sign - as those are now added by the server (and thus later), this needed to change slightly. Scrub itself still exist (for backward compat reasons), but has been made a noop. Scrub has been renamed to scrub as it should not be used by external plugins. Fixes: #2010 Signed-off-by: Miek Gieben <miek@miek.nl>
149 lines
3.6 KiB
Go
149 lines
3.6 KiB
Go
// Package file implements a file backend.
|
|
package file
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/coredns/coredns/plugin"
|
|
clog "github.com/coredns/coredns/plugin/pkg/log"
|
|
"github.com/coredns/coredns/request"
|
|
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
var log = clog.NewWithPlugin("file")
|
|
|
|
type (
|
|
// File is the plugin that reads zone data from disk.
|
|
File struct {
|
|
Next plugin.Handler
|
|
Zones Zones
|
|
}
|
|
|
|
// Zones maps zone names to a *Zone.
|
|
Zones struct {
|
|
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.
|
|
}
|
|
)
|
|
|
|
// ServeDNS implements the plugin.Handle interface.
|
|
func (f File) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
|
state := request.Request{W: w, Req: r, Context: ctx}
|
|
|
|
qname := state.Name()
|
|
// TODO(miek): match the qname better in the map
|
|
zone := plugin.Zones(f.Zones.Names).Matches(qname)
|
|
if zone == "" {
|
|
return plugin.NextOrFailure(f.Name(), f.Next, ctx, w, r)
|
|
}
|
|
|
|
z, ok := f.Zones.Z[zone]
|
|
if !ok || z == nil {
|
|
return dns.RcodeServerFailure, nil
|
|
}
|
|
|
|
// This is only for when we are a secondary zones.
|
|
if r.Opcode == dns.OpcodeNotify {
|
|
if z.isNotify(state) {
|
|
m := new(dns.Msg)
|
|
m.SetReply(r)
|
|
m.Authoritative, m.RecursionAvailable = true, true
|
|
state.SizeAndDo(m)
|
|
w.WriteMsg(m)
|
|
|
|
log.Infof("Notify from %s for %s: checking transfer", state.IP(), zone)
|
|
ok, err := z.shouldTransfer()
|
|
if ok {
|
|
z.TransferIn()
|
|
} else {
|
|
log.Infof("Notify from %s for %s: no serial increase seen", state.IP(), zone)
|
|
}
|
|
if err != nil {
|
|
log.Warningf("Notify from %s for %s: failed primary check: %s", state.IP(), zone, err)
|
|
}
|
|
return dns.RcodeSuccess, nil
|
|
}
|
|
log.Infof("Dropping notify from %s for %s", state.IP(), zone)
|
|
return dns.RcodeSuccess, nil
|
|
}
|
|
|
|
if z.Expired != nil && *z.Expired {
|
|
log.Errorf("Zone %s is expired", zone)
|
|
return dns.RcodeServerFailure, nil
|
|
}
|
|
|
|
if state.QType() == dns.TypeAXFR || state.QType() == dns.TypeIXFR {
|
|
xfr := Xfr{z}
|
|
return xfr.ServeDNS(ctx, w, r)
|
|
}
|
|
|
|
answer, ns, extra, result := z.Lookup(state, qname)
|
|
|
|
m := new(dns.Msg)
|
|
m.SetReply(r)
|
|
m.Authoritative, m.RecursionAvailable = true, true
|
|
m.Answer, m.Ns, m.Extra = answer, ns, extra
|
|
|
|
switch result {
|
|
case Success:
|
|
case NoData:
|
|
case NameError:
|
|
m.Rcode = dns.RcodeNameError
|
|
case Delegation:
|
|
m.Authoritative = false
|
|
case ServerFailure:
|
|
return dns.RcodeServerFailure, nil
|
|
}
|
|
|
|
w.WriteMsg(m)
|
|
return dns.RcodeSuccess, nil
|
|
}
|
|
|
|
// Name implements the Handler interface.
|
|
func (f File) Name() string { return "file" }
|
|
|
|
type serialErr struct {
|
|
err string
|
|
zone string
|
|
origin string
|
|
serial int64
|
|
}
|
|
|
|
func (s *serialErr) Error() string {
|
|
return fmt.Sprintf("%s for origin %s in file %s, with serial %d", s.err, s.origin, s.zone, s.serial)
|
|
}
|
|
|
|
// Parse parses the zone in filename and returns a new Zone or an error.
|
|
// 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) {
|
|
tokens := dns.ParseZone(f, dns.Fqdn(origin), fileName)
|
|
z := NewZone(origin, fileName)
|
|
seenSOA := false
|
|
for x := range tokens {
|
|
if x.Error != nil {
|
|
return nil, x.Error
|
|
}
|
|
|
|
if !seenSOA && serial >= 0 {
|
|
if s, ok := x.RR.(*dns.SOA); ok {
|
|
if s.Serial == uint32(serial) { // same serial
|
|
return nil, &serialErr{err: "no change in SOA serial", origin: origin, zone: fileName, serial: serial}
|
|
}
|
|
seenSOA = true
|
|
}
|
|
}
|
|
|
|
if err := z.Insert(x.RR); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if !seenSOA {
|
|
return nil, fmt.Errorf("file %q has no SOA record", fileName)
|
|
}
|
|
|
|
return z, nil
|
|
}
|