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 (
|
2018-04-22 08:34:35 +01:00
|
|
|
"context"
|
2017-06-08 18:43:11 +01:00
|
|
|
"fmt"
|
2016-03-27 07:37:23 +01:00
|
|
|
"io"
|
2016-03-19 07:18:57 +00:00
|
|
|
|
2017-09-14 09:36:06 +01:00
|
|
|
"github.com/coredns/coredns/plugin"
|
2018-04-22 21:40:33 +01:00
|
|
|
clog "github.com/coredns/coredns/plugin/pkg/log"
|
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"
|
|
|
|
|
)
|
|
|
|
|
|
2018-04-22 21:40:33 +01:00
|
|
|
var log = clog.NewWithPlugin("file")
|
|
|
|
|
|
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 {
|
2019-06-29 22:22:34 +01:00
|
|
|
Next plugin.Handler
|
|
|
|
|
Zones
|
2016-03-18 20:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
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) {
|
2019-03-26 14:37:30 +00: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)
|
2018-12-30 17:05:08 +01:00
|
|
|
m.Authoritative = true
|
2016-04-05 10:53:23 +01:00
|
|
|
w.WriteMsg(m)
|
|
|
|
|
|
2018-04-19 07:41:56 +01:00
|
|
|
log.Infof("Notify from %s for %s: checking transfer", state.IP(), zone)
|
2016-04-06 22:29:33 +01:00
|
|
|
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 {
|
2018-04-19 07:41:56 +01:00
|
|
|
log.Infof("Notify from %s for %s: no serial increase seen", state.IP(), zone)
|
2016-04-06 22:29:33 +01:00
|
|
|
}
|
|
|
|
|
if err != nil {
|
2018-04-19 07:41:56 +01:00
|
|
|
log.Warningf("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
|
|
|
|
|
}
|
2018-04-19 07:41:56 +01:00
|
|
|
log.Infof("Dropping notify from %s for %s", state.IP(), zone)
|
2016-04-05 10:53:23 +01:00
|
|
|
return dns.RcodeSuccess, nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 15:06:50 +00:00
|
|
|
z.RLock()
|
|
|
|
|
exp := z.Expired
|
|
|
|
|
z.RUnlock()
|
|
|
|
|
if exp != nil && *exp {
|
2018-04-19 07:41:56 +01:00
|
|
|
log.Errorf("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)
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-26 14:37:30 +00:00
|
|
|
answer, ns, extra, result := z.Lookup(ctx, state, qname)
|
2016-03-27 07:37:23 +01:00
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
|
m.SetReply(r)
|
2018-12-30 17:05:08 +01:00
|
|
|
m.Authoritative = 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
|
|
|
|
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" }
|
|
|
|
|
|
2017-09-20 17:28:23 +01:00
|
|
|
type serialErr struct {
|
|
|
|
|
err string
|
|
|
|
|
zone string
|
|
|
|
|
origin string
|
|
|
|
|
serial int64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *serialErr) Error() string {
|
2017-09-22 16:13:04 -07:00
|
|
|
return fmt.Sprintf("%s for origin %s in file %s, with serial %d", s.err, s.origin, s.zone, s.serial)
|
2017-09-20 17:28:23 +01:00
|
|
|
}
|
|
|
|
|
|
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) {
|
2018-10-23 20:19:36 +01:00
|
|
|
zp := dns.NewZoneParser(f, dns.Fqdn(origin), fileName)
|
|
|
|
|
zp.SetIncludeAllowed(true)
|
2016-04-15 14:26:27 +01:00
|
|
|
z := NewZone(origin, fileName)
|
2017-06-08 18:43:11 +01:00
|
|
|
seenSOA := false
|
2018-10-23 20:19:36 +01:00
|
|
|
for rr, ok := zp.Next(); ok; rr, ok = zp.Next() {
|
|
|
|
|
if err := zp.Err(); err != nil {
|
|
|
|
|
return nil, err
|
2016-03-18 20:57:35 +00:00
|
|
|
}
|
2017-06-08 18:43:11 +01:00
|
|
|
|
2019-07-04 06:56:37 +01:00
|
|
|
if !seenSOA {
|
2018-10-23 20:19:36 +01:00
|
|
|
if s, ok := rr.(*dns.SOA); ok {
|
2019-07-04 06:56:37 +01:00
|
|
|
seenSOA = true
|
|
|
|
|
|
|
|
|
|
// -1 is valid serial is we failed to load the file on startup.
|
|
|
|
|
|
|
|
|
|
if serial >= 0 && s.Serial == uint32(serial) { // same serial
|
2017-09-20 17:28:23 +01:00
|
|
|
return nil, &serialErr{err: "no change in SOA serial", origin: origin, zone: fileName, serial: serial}
|
2017-06-08 18:43:11 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-23 20:19:36 +01:00
|
|
|
if err := z.Insert(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 {
|
2019-07-27 11:47:55 +00:00
|
|
|
return nil, fmt.Errorf("file %q has no SOA record for origin %s", fileName, origin)
|
2017-06-13 12:51:52 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-27 07:37:23 +01:00
|
|
|
return z, nil
|
2016-03-18 20:57:35 +00:00
|
|
|
}
|