Improve conformance with DNS behavior

This commit is contained in:
Dessa Simpson 2025-12-02 00:57:49 -07:00
parent b79df4bf1d
commit 07da2a1269
6 changed files with 96 additions and 50 deletions

View file

@ -76,7 +76,7 @@ func LoadZoneBytes(data []byte, filename string) (*Zone, error) {
return z, nil
}
func (z *Zone) Validate(name string, soa *Record, ns *[]Record) error {
func (z *Zone) Validate(name string, soa *NamedRecord, ns *[]Record) error {
nameservers, otherRecords := []Record{}, []Record{}
nameserversMap := map[string]bool{}
isZoneApex := false
@ -85,8 +85,7 @@ func (z *Zone) Validate(name string, soa *Record, ns *[]Record) error {
switch record.Type {
case "SOA":
isZoneApex = true
soa = &record
z.SOA = NamedRecord{Name: name, Record: record}
soa = &NamedRecord{Name: name, Record: record}
case "NS":
nameservers = append(nameservers, record)
nameserversMap[record.Value] = true
@ -97,12 +96,12 @@ func (z *Zone) Validate(name string, soa *Record, ns *[]Record) error {
}
}
z.IsDelegationPoint = !isZoneApex && len(nameservers) > 0
if cnameCount > 1 || (cnameCount > 0 && len(otherRecords) > 0) {
return fmt.Errorf("%s: extraneous records found next to CNAME", name)
}
z.IsDelegationPoint = !isZoneApex && len(nameservers) > 0
if soa == nil {
// Outside zone
if z.IsDelegationPoint {
@ -115,9 +114,10 @@ func (z *Zone) Validate(name string, soa *Record, ns *[]Record) error {
if len(nameservers) == 0 {
return fmt.Errorf("%s: zone apex missing NS records", name)
}
z.SOA = *soa
ns = &nameservers
} else if len(nameservers) > 0 {
// Delegation point (does not fall through to subzone validation)
// Delegation point (does not fall through)
if len(otherRecords) > 0 {
return fmt.Errorf("%s: non-glue, non-NS records found at delegation point: %v", name, otherRecords)
}
@ -129,9 +129,14 @@ func (z *Zone) Validate(name string, soa *Record, ns *[]Record) error {
}
}
return nil
} else {
// Normal node
// Cache SOA at each node in the zone tree
z.SOA = *soa
}
// Subzone validation
// Either we're outside a zone, at a zone apex, or at a non-delegated subzone
for subname, subzone := range z.Subzones {
if err := subzone.Validate(concatName(name, subname), soa, ns); err != nil {
return err
@ -275,6 +280,11 @@ func nameToPath(name string) []string {
}
func (z *Zone) Lookup(name string) (LookupResult, bool) {
res, ok := z.LookupType(name, "")
return res, ok
}
func (z *Zone) LookupType(name string, recordType string) (LookupResult, bool) {
res := LookupResult{}
path := nameToPath(name)
for _, label := range path {
@ -282,18 +292,6 @@ func (z *Zone) Lookup(name string) (LookupResult, bool) {
if label == "" {
continue
}
if z.IsDelegationPoint {
res.IsReferral = true
// Capture NS records
for _, record := range z.Records {
if record.Type == "NS" {
res.Ns = append(res.Ns, NamedRecord{Name: name, Record: record})
}
}
// Retrieve glue records from cache
res.Extra = append(res.Extra, z.GlueRecords...)
return res, true
}
if sz, ok := z.Subzones[label]; !ok {
// NXDOMAIN
res.Ns = []NamedRecord{z.SOA}
@ -301,10 +299,27 @@ func (z *Zone) Lookup(name string) (LookupResult, bool) {
} else {
z = sz
}
if z.IsDelegationPoint {
// Retrieve glue records from cache, whether referral or NS query
res.Extra = append(res.Extra, z.GlueRecords...)
if recordType != "NS" {
// Treat as referral unless client requested NS records
res.IsReferral = true
// Capture NS records
for _, record := range z.Records {
if record.Type == "NS" {
res.Ns = append(res.Ns, NamedRecord{Name: name, Record: record})
}
}
return res, true
}
}
}
res.Answer = []NamedRecord{}
for _, record := range z.Records {
res.Answer = append(res.Answer, NamedRecord{Name: name, Record: record})
if recordType == "" || record.Type == recordType {
res.Answer = append(res.Answer, NamedRecord{Name: name, Record: record})
}
}
if len(res.Answer) == 0 {
// NODATA
@ -314,29 +329,6 @@ func (z *Zone) Lookup(name string) (LookupResult, bool) {
return res, true
}
func (z *Zone) FilterRecords(res LookupResult, recordType string) LookupResult {
filtered := []NamedRecord{}
for _, nr := range res.Answer {
if nr.Record.Type == recordType {
filtered = append(filtered, nr)
}
}
res.Answer = filtered
if len(res.Answer) == 0 && !res.IsReferral {
// This ends up as NODATA even if it had data before filtering
res.Ns = []NamedRecord{z.SOA}
}
return res
}
func (z *Zone) LookupType(name string, recordType string) (LookupResult, bool) {
res, ok := z.Lookup(name)
if !ok {
return LookupResult{}, false
}
return z.FilterRecords(res, recordType), true
}
func concatName(name string, subname string) string {
if name == "." {
return subname + "."