Sfoglia il codice sorgente

Don't crash if a zone doesn't have any apex records

Ask Bjørn Hansen 12 anni fa
parent
commit
c3a1cbd6e7
4 ha cambiato i file con 35 aggiunte e 12 eliminazioni
  1. 9 11
      config.go
  2. 2 1
      config_test.go
  3. 9 0
      dns/test.example.org.json
  4. 15 0
      types.go

+ 9 - 11
config.go

@@ -160,12 +160,7 @@ func setupZoneData(data map[string]interface{}, Zone *Zone) {
 
 		//log.Printf("K %s V %s TYPE-V %T\n", dk, dv, dv)
 
-		dk = strings.ToLower(dk)
-		Zone.Labels[dk] = new(Label)
-		label := Zone.Labels[dk]
-		label.Label = dk
-		label.Ttl = Zone.Options.Ttl
-		label.MaxHosts = Zone.Options.MaxHosts
+		label := Zone.AddLabel(dk)
 
 		if ttl, ok := dv["ttl"]; ok {
 			label.Ttl = valueToInt(ttl)
@@ -210,11 +205,6 @@ func setupZoneData(data map[string]interface{}, Zone *Zone) {
 
 			//log.Printf("RECORDS %s TYPE-REC %T\n", Records, Records)
 
-			if label.Records == nil {
-				label.Records = make(map[uint16]Records)
-				label.Weight = make(map[uint16]int)
-			}
-
 			label.Records[dnsType] = make(Records, len(records[rType]))
 
 			for i := 0; i < len(records[rType]); i++ {
@@ -347,6 +337,14 @@ func setupSOA(Zone *Zone) {
 
 	primaryNs := "ns"
 
+	// log.Println("LABEL", label)
+
+	if label == nil {
+		log.Println(Zone.Origin, "doesn't have any 'root' records,",
+			"you should probably add some NS records")
+		label = Zone.AddLabel("")
+	}
+
 	if record, ok := label.Records[dns.TypeNS]; ok {
 		primaryNs = record[0].RR.(*dns.NS).Ns
 	}

+ 2 - 1
config_test.go

@@ -19,8 +19,9 @@ func (s *ConfigSuite) TestReadConfigs(c *C) {
 	s.zones = make(Zones)
 	configReadDir("dns", s.zones)
 
-	// Just check that example.com loaded, too.
+	// Just check that example.com and test.example.org loaded, too.
 	c.Check(s.zones["example.com"].Origin, Equals, "example.com")
+	c.Check(s.zones["test.example.org"].Origin, Equals, "test.example.org")
 
 	tz := s.zones["test.example.com"]
 

+ 9 - 0
dns/test.example.org.json

@@ -0,0 +1,9 @@
+{
+    "serial": 1,
+    "data" : {
+        "bad-example-there-really-should-be-an-ns-record-at-the-apex-here": {},
+        "bar": { 
+            "a": [ [ "192.168.1.2" ] ]
+        }
+    }
+}

+ 15 - 0
types.go

@@ -3,6 +3,7 @@ package main
 import (
 	"github.com/abh/geodns/countries"
 	"github.com/miekg/dns"
+	"strings"
 )
 
 type Options struct {
@@ -49,6 +50,20 @@ func (l *Label) firstRR(dnsType uint16) dns.RR {
 	return l.Records[dnsType][0].RR
 }
 
+func (z *Zone) AddLabel(k string) *Label {
+	k = strings.ToLower(k)
+	z.Labels[k] = new(Label)
+	label := z.Labels[k]
+	label.Label = k
+	label.Ttl = z.Options.Ttl
+	label.MaxHosts = z.Options.MaxHosts
+
+	label.Records = make(map[uint16]Records)
+	label.Weight = make(map[uint16]int)
+
+	return label
+}
+
 func (z *Zone) SoaRR() dns.RR {
 	return z.Labels[""].firstRR(dns.TypeSOA)
 }