| 1 |
// Package implement a DNS zone, held in a binary tree. Each RR(set) that gets inserted will need to create |
| 2 |
// any empty non-terminals (ENT) it possesses. I.e. inserting www.example.org into example.org is easy, but when |
| 3 |
// www.a.b.c.example.org inserts we need to make sure that 'c.example.org', 'b.c.example.org' and |
| 4 |
// 'a.b.c.example.org' also exist and are ENTs (have no actual RRs). For deleted the opposite must happen. As |
| 5 |
// an example from RFC 4592, the record: sub.*.example. TXT "this is not a wildcard" is a fun one. As this |
| 6 |
// means the '*.example' ENT exists meaning that bogus.example. gets a NODATA response instead of NXDOMAIN. |
| 7 |
// |
| 8 |
// Doing this on insert sucks a bit, but makes the lookup code much more simple (and correct), which is more |
| 9 |
// important for a DNS server. |
| 10 |
// |
| 11 |
// CNAME, DNSSEC, wildcards, etc. are all supported. Not supported is: DNAME (RFC 6672), the server will just |
| 12 |
// return the DNAME without any of the (in the RFC required) post-processing. |
| 13 |
package zone |
| 14 |
|
| 15 |
import ( |
| 16 |
"fmt" |
| 17 |
"os" |
| 18 |
"path/filepath" |
| 19 |
"sync" |
| 20 |
|
| 21 |
"codeberg.org/miekg/dns" |
| 22 |
"codeberg.org/miekg/dns/cmd/atomdns/internal/dnszone" |
| 23 |
"codeberg.org/miekg/dns/dnsutil" |
| 24 |
"github.com/tidwall/btree" |
| 25 |
) |
| 26 |
|
| 27 |
// Zone holds the main zone and some meta data of the DNS zone we are serving. |
| 28 |
// There is no locking, because after creation this structure is basically read-only. |
| 29 |
// Tree will be used to write, but that has its own locking. |
| 30 |
type Zone struct { |
| 31 |
origin string |
| 32 |
labels int |
| 33 |
Path string |
| 34 |
Tree *btree.BTreeG[*dnszone.Node] |
| 35 |
|
| 36 |
sync.RWMutex // protects apex |
| 37 |
apex *dnszone.Node // apex node, filled after a Load. |
| 38 |
} |
| 39 |
|
| 40 |
var _ dnszone.Interface = &Zone{} |
| 41 |
|
| 42 |
func New(origin, path string) *Zone { |
| 43 |
z := &Zone{ |
| 44 |
origin: dnsutil.Canonical(origin), |
| 45 |
labels: dnsutil.Labels(dnsutil.Canonical(origin)), |
| 46 |
Path: func() string { a, _ := filepath.Abs(path); return a }(), |
| 47 |
Tree: btree.NewBTreeG(dnszone.Less), |
| 48 |
} |
| 49 |
return z |
| 50 |
} |
| 51 |
|
| 52 |
func (z *Zone) Origin() string { return z.origin } |
| 53 |
func (z *Zone) Labels() int { return z.labels } |
| 54 |
|
| 55 |
// Load loads a new zone with origin from path from z. Load also sets the apex, so the z.Apex can return that. |
| 56 |
func (z *Zone) Load() error { |
| 57 |
f, err := os.Open(z.Path) |
| 58 |
if err != nil { |
| 59 |
return fmt.Errorf("failed to load zone %s with origin %s: %s", z.Path, z.origin, err) |
| 60 |
} |
| 61 |
zp := dns.NewZoneParser(f, z.origin, z.Path) |
| 62 |
soa := 0 |
| 63 |
// TODO(miek): various optimizations: gather names until we have a different one, then insert. |
| 64 |
// Downside: RR's are pointers so we need to empty out the structure and then refill it next time. |
| 65 |
// something Set also does. |
| 66 |
for rr, ok := zp.Next(); ok; rr, ok = zp.Next() { |
| 67 |
if _, ok := rr.(*dns.SOA); ok { |
| 68 |
soa++ |
| 69 |
} |
| 70 |
z.Set(&dnszone.Node{Name: rr.Header().Name, RRs: []dns.RR{rr}}) |
| 71 |
} |
| 72 |
if zp.Err() != nil { |
| 73 |
return fmt.Errorf("failed to parse zone %s with origin %s: %s ", z.Path, z.origin, zp.Err()) |
| 74 |
} |
| 75 |
if soa != 1 { |
| 76 |
return fmt.Errorf("zone %s with origin %s has no SOA or not a single SOA record", z.Path, z.origin) |
| 77 |
} |
| 78 |
z.Lock() |
| 79 |
z.apex, _ = z.Tree.Get(&dnszone.Node{Name: z.origin}) |
| 80 |
z.Unlock() |
| 81 |
return nil |
| 82 |
} |
| 83 |
|
| 84 |
func (z *Zone) Apex() *dnszone.Node { |
| 85 |
z.RLock() |
| 86 |
defer z.RUnlock() |
| 87 |
a := z.apex |
| 88 |
if a != nil { |
| 89 |
return a |
| 90 |
} |
| 91 |
return &dnszone.Node{} |
| 92 |
} |
| 93 |
|
| 94 |
// Set sets the RRs in the zone. It needs to create any empty non-terminals it has. Meaning for each label |
| 95 |
// a lookup is done if there already is an empty non-terminal, if not an empty set is inserted. |
| 96 |
// We should never be called to insert ENT (or names without RRs attached to them). The node's name is |
| 97 |
// returned. |
| 98 |
func (z *Zone) Set(node *dnszone.Node) string { |
| 99 |
// If the name already exist, we can just add our stuff to the node and we are done. |
| 100 |
n, ok := z.Tree.Get(node) |
| 101 |
if ok { |
| 102 |
n.RRs = append(n.RRs, node.RRs...) |
| 103 |
z.Tree.Set(n) |
| 104 |
return node.Name |
| 105 |
} |
| 106 |
// The name didn't exist before, we need to insert it. |
| 107 |
z.Tree.Set(node) |
| 108 |
// Now we need to create (potential) ENT up to the apex. So when just insert www.a.b.example.org. We need |
| 109 |
// make a.b.example.org, b.example.org. So we need N+2 labels, if this zone has N labels. If we only have |
| 110 |
// 1 label more, we just created the correct node. |
| 111 |
labels := dnsutil.Labels(node.Name) |
| 112 |
if labels == z.Labels()+1 { |
| 113 |
return node.Name |
| 114 |
} |
| 115 |
|
| 116 |
// Else we create (or check if they exist) the intermediate nodes. |
| 117 |
off := 0 |
| 118 |
name := node.Name |
| 119 |
for i := 1; i < labels-z.Labels(); i++ { |
| 120 |
off, _ = dnsutil.Next(name, off) |
| 121 |
|
| 122 |
node := &dnszone.Node{Name: name[off:]} |
| 123 |
if _, ok := z.Tree.Get(node); ok { |
| 124 |
continue // already exist, nothing to add |
| 125 |
} |
| 126 |
z.Tree.Set(node) // set an empty node |
| 127 |
} |
| 128 |
return node.Name |
| 129 |
} |
| 130 |
|
| 131 |
// Get gets the node under name from z. |
| 132 |
func (z *Zone) Get(name string) (*dnszone.Node, bool) { |
| 133 |
n, ok := z.Tree.Get(&dnszone.Node{Name: name}) |
| 134 |
if ok { |
| 135 |
return n, true |
| 136 |
} |
| 137 |
return nil, false |
| 138 |
} |
| 139 |
|
| 140 |
// Previous returns the logical previous name from name. |
| 141 |
func (z *Zone) Previous(name string) *dnszone.Node { |
| 142 |
node := &dnszone.Node{} |
| 143 |
z.Tree.Descend(&dnszone.Node{Name: name}, func(n *dnszone.Node) bool { |
| 144 |
node = n |
| 145 |
return false |
| 146 |
}) |
| 147 |
return node |
| 148 |
} |