Debian Codegraph @

cmd/atomdns/handlers/dbsqlite/zone.go

raw
1 package dbsqlite
2
3 import (
4 "fmt"
5 "strconv"
6 "time"
7
8 "codeberg.org/miekg/dns"
9 "codeberg.org/miekg/dns/cmd/atomdns/internal/dnszone"
10 "codeberg.org/miekg/dns/dnsutil"
11 "codeberg.org/miekg/dns/pkg/pool"
12 "github.com/jmoiron/sqlx"
13 )
14
15 type Zone struct {
16 db *sqlx.DB
17 origin string
18 labels int
19 }
20
21 var _ dnszone.Interface = &Zone{}
22
23 // RR is the data we stored in the rrs table.
24 type RR struct {
25 Name string
26 Type string
27 Data string
28 TTL int
29 Date time.Time
30 }
31
32 func (z *Zone) Load() error { return nil }
33 func (z *Zone) Set(*dnszone.Node) string { return "" }
34 func (z *Zone) Origin() string { return z.origin }
35 func (z *Zone) Labels() int { return z.labels }
36
37 func (z *Zone) Walk(fn func(*dnszone.Node) bool) {
38 // For some reason this give no names, but:
39 // err := z.db.Select(&names, `SELECT DISTINCT name FROM rrs WHERE name LIKE '%.?' COLLATE canonical`, z.origin)
40 // this does:
41 names := []string{}
42 err := z.db.Select(&names, fmt.Sprintf(`SELECT DISTINCT name FROM rrs WHERE name LIKE '%%.%[1]s' COLLATE canonical OR name = '%[1]s' ORDER BY name COLLATE canonical`, z.origin))
43 if err != nil {
44 return
45 }
46 for _, name := range names {
47 n, ok := z.Get(name)
48 if !ok {
49 continue
50 }
51 if !fn(n) {
52 return
53 }
54 }
55 }
56
57 func (z *Zone) AuthoritativeWalk(fn func(*dnszone.Node, bool) bool) {
58 // See comment in Walk, we keep track of delegations, also see dbfile/zone.Walk.
59 names := []string{}
60 err := z.db.Select(&names, fmt.Sprintf(`SELECT DISTINCT name FROM rrs WHERE name LIKE '%%.%[1]s' COLLATE canonical OR name = '%[1]s' ORDER BY name COLLATE canonical`, z.origin))
61 if err != nil {
62 return
63 }
64
65 delegated := map[string]struct{}{}
66
67 z.Walk(func(n *dnszone.Node) bool {
68 if len(n.Name) > len(z.Origin()) { // apex also has NSes, if we add those the entire zone would be delegated
69 for _, rr := range n.RRs {
70 if _, ok := rr.(*dns.NS); ok {
71 delegated[n.Name] = struct{}{}
72 break
73 }
74 }
75 }
76 auth, end := true, false
77 i, j := 0, 0
78 for ; !end; j, end = dnsutil.Next(n.Name, i) {
79 if len(n.Name[j:]) < len(z.Origin()) {
80 break
81 }
82 if _, ok := delegated[n.Name[j:]]; ok {
83 auth = false
84 break
85 }
86 i++
87 }
88
89 return fn(n, auth)
90 })
91 }
92
93 func (z *Zone) Previous(name string) *dnszone.Node {
94 prevs := []string{}
95 err := z.db.Select(&prevs, "SELECT name FROM rrs WHERE name < ? COLLATE canonical ORDER BY name COLLATE canonical DESC LIMIT 1", name)
96 if err != nil || len(prevs) == 0 {
97 return nil
98 }
99 node, _ := z.Get(prevs[0])
100 return node
101 }
102
103 func (z *Zone) Get(name string) (*dnszone.Node, bool) {
104 // Get will get name, if that doesn't return anything we do LIKE '%.<name>' this is to shake out empty
105 // non-terminals. If we have something returned we know that <name> is an ENT. Wildcards are handled by
106 // retrieve.
107
108 rrs := []RR{}
109 err := z.db.Select(&rrs, "SELECT * FROM rrs WHERE name = ?", name)
110 if err != nil {
111 return nil, false
112 }
113 sb := builderPool.Get()
114
115 if len(rrs) > 0 {
116 node := &dnszone.Node{Name: name, RRs: make([]dns.RR, 0, len(rrs))}
117 for _, rr := range rrs {
118 sb.WriteString(rr.Name)
119 sb.WriteByte(' ')
120 sb.WriteString(strconv.Itoa(rr.TTL))
121 sb.WriteByte(' ')
122 sb.WriteString(rr.Type)
123 sb.WriteByte(' ')
124 sb.WriteString(rr.Data)
125 sb.WriteByte('\n')
126 rr1, err := dns.New(sb.String())
127 if err != nil {
128 log().Debug("Failed to convert to dns.RR", "rr", sb, Err(err))
129 sb.Reset()
130 continue
131 }
132 node.RRs = append(node.RRs, rr1)
133 sb.Reset()
134 }
135 sb.Reset()
136 builderPool.Put(sb)
137 return node, true
138 }
139
140 // nothing found, check for empty non-terminals, if this returns a wildcard? Should we exclude wildcards? TODO(miek).
141 names := []string{}
142 err = z.db.Select(&names, fmt.Sprintf(`SELECT DISTINCT name FROM rrs WHERE name LIKE '%%.%[1]s' COLLATE canonical`, name))
143 if err != nil {
144 return nil, false
145 }
146 if len(names) > 0 { // we have found longer names than name, we have an empty non-terminal at name
147 return &dnszone.Node{Name: name}, true
148 }
149
150 return nil, false
151 }
152
153 func (z *Zone) Apex() *dnszone.Node {
154 node, _ := z.Get(z.Origin())
155 if node != nil {
156 return node
157 }
158 return &dnszone.Node{}
159 }
160
161 var builderPool = pool.NewBuilder()