| 1 |
package dnszone |
| 2 |
|
| 3 |
import ( |
| 4 |
"codeberg.org/miekg/dns" |
| 5 |
"codeberg.org/miekg/dns/dnsutil" |
| 6 |
) |
| 7 |
|
| 8 |
// Hints give a hint to the functions here on what type of answer we got. This could be (mostly?) be done in |
| 9 |
// retrieve, but requires redoing work already done, easier to just notify what we have. |
| 10 |
type Hint int |
| 11 |
|
| 12 |
const ( |
| 13 |
hintAnswer Hint = iota |
| 14 |
hintDelegation |
| 15 |
hintWildcard |
| 16 |
) |
| 17 |
|
| 18 |
// Synthesize handles all wildcard responses, we are only called when we hit a wildcard and didn't find any |
| 19 |
// more specific. I.e. original qname did not exist. Now we need to assemble the answer plus adding the NSECs |
| 20 |
// that validate the answer. If sosynthesis.Name != encloser.Name, those two NSECs need to be added. |
| 21 |
func Synthesize(z Interface, r *dns.Msg, sosynthesis, encloser *Node, re *Restart) *dns.Msg { |
| 22 |
// Synthesis, can still lead to no data if the qtype doesn't match. |
| 23 |
if len(sosynthesis.RRs) > 0 { |
| 24 |
qtype := dns.RRToType(r.Question[0]) |
| 25 |
for _, rr := range sosynthesis.RRs { |
| 26 |
if rrtype := dns.RRToType(rr); rrtype == qtype || rrtype == dns.TypeCNAME { |
| 27 |
rr1 := rr.Clone() |
| 28 |
rr1.Header().Name = r.Question[0].Header().Name // replace owner names with the qname |
| 29 |
r.Answer = append(r.Answer, rr1) |
| 30 |
} |
| 31 |
if r.Security { |
| 32 |
if _, ok := rr.(*dns.NSEC); ok { |
| 33 |
r.Ns = append(r.Ns, rr) // SoS' NSEC |
| 34 |
} |
| 35 |
if s, ok := rr.(*dns.RRSIG); ok { |
| 36 |
if s.TypeCovered == qtype || s.TypeCovered == dns.TypeCNAME { |
| 37 |
rr1 := rr.Clone() |
| 38 |
rr1.Header().Name = r.Question[0].Header().Name |
| 39 |
r.Answer = append(r.Answer, rr1) |
| 40 |
} |
| 41 |
if s.TypeCovered == dns.TypeNSEC { |
| 42 |
r.Ns = append(r.Ns, rr) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
} |
| 47 |
} |
| 48 |
if len(r.Answer) > 0 { |
| 49 |
return r |
| 50 |
} |
| 51 |
// NODATA, as the type isn't there, only need SOA + RRSIG. |
| 52 |
for _, rr := range z.Apex().RRs { |
| 53 |
if _, ok := rr.(*dns.SOA); ok { |
| 54 |
r.Ns = append(r.Ns, rr) |
| 55 |
continue |
| 56 |
} |
| 57 |
if r.Security { |
| 58 |
if s, ok := rr.(*dns.RRSIG); ok && s.TypeCovered == dns.TypeSOA { |
| 59 |
r.Ns = append(r.Ns, rr) |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
return r |
| 64 |
} |
| 65 |
|
| 66 |
// NODATA response, when there are no RRs or when we fall through from above. |
| 67 |
if len(sosynthesis.RRs) == 0 { |
| 68 |
for _, rr := range z.Apex().RRs { |
| 69 |
if _, ok := rr.(*dns.SOA); ok { |
| 70 |
r.Ns = append(r.Ns, rr) |
| 71 |
continue |
| 72 |
} |
| 73 |
if r.Security { |
| 74 |
if _, ok := rr.(*dns.NSEC); ok { |
| 75 |
r.Ns = append(r.Ns, rr) |
| 76 |
} |
| 77 |
if s, ok := rr.(*dns.RRSIG); ok && (s.TypeCovered == dns.TypeSOA || s.TypeCovered == dns.TypeNSEC) { |
| 78 |
r.Ns = append(r.Ns, rr) |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
if r.Security { // proving there is no wildcard |
| 83 |
prev := z.Previous(r.Question[0].Header().Name) |
| 84 |
if !dns.EqualName(prev.Name, z.Origin()) { // we already have the SOA records, don't repeat. |
| 85 |
for _, rr := range prev.RRs { |
| 86 |
if _, ok := rr.(*dns.NSEC); ok { |
| 87 |
r.Ns = append(r.Ns, rr) |
| 88 |
continue |
| 89 |
} |
| 90 |
if s, ok := rr.(*dns.RRSIG); ok && s.TypeCovered == dns.TypeNSEC { |
| 91 |
r.Ns = append(r.Ns, rr) |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
return r |
| 97 |
} |
| 98 |
|
| 99 |
return r |
| 100 |
} |
| 101 |
|
| 102 |
func MsgFound(z Interface, r *dns.Msg, encloser *Node, hint Hint, re *Restart) *dns.Msg { |
| 103 |
section := &r.Answer |
| 104 |
qtype := dns.RRToType(r.Question[0]) |
| 105 |
if hint == hintDelegation { |
| 106 |
r.Authoritative = false |
| 107 |
section = &r.Ns |
| 108 |
qtype = dns.TypeNS |
| 109 |
} |
| 110 |
|
| 111 |
// NXDOOMAIN response. |
| 112 |
if hint != hintDelegation && !dns.EqualName(encloser.Name, r.Question[0].Header().Name) { |
| 113 |
for _, rr := range z.Apex().RRs { |
| 114 |
if _, ok := rr.(*dns.SOA); ok { |
| 115 |
r.Ns = append(r.Ns, rr) |
| 116 |
continue |
| 117 |
} |
| 118 |
if r.Security { |
| 119 |
if _, ok := rr.(*dns.NSEC); ok { |
| 120 |
r.Ns = append(r.Ns, rr) |
| 121 |
} |
| 122 |
if s, ok := rr.(*dns.RRSIG); ok && (s.TypeCovered == dns.TypeSOA || s.TypeCovered == dns.TypeNSEC) { |
| 123 |
r.Ns = append(r.Ns, rr) |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
if r.Security { |
| 128 |
prev := z.Previous(r.Question[0].Header().Name) |
| 129 |
if !dns.EqualName(prev.Name, z.Origin()) { |
| 130 |
for _, rr := range prev.RRs { |
| 131 |
if _, ok := rr.(*dns.NSEC); ok { |
| 132 |
r.Ns = append(r.Ns, rr) |
| 133 |
continue |
| 134 |
} |
| 135 |
if s, ok := rr.(*dns.RRSIG); ok && s.TypeCovered == dns.TypeNSEC { |
| 136 |
r.Ns = append(r.Ns, rr) |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
if re != nil { |
| 142 |
r.Question[0].Header().Name = re.Name |
| 143 |
} |
| 144 |
r.Rcode = dns.RcodeNameError |
| 145 |
return r |
| 146 |
} |
| 147 |
|
| 148 |
// If this is a CNAME we need to chase it within the zone for (up to 8?) CNAME chains. |
| 149 |
for _, rr := range encloser.RRs { |
| 150 |
if dns.RRToType(rr) == dns.TypeCNAME && qtype != dns.TypeCNAME { |
| 151 |
return Canonical(z, r, encloser, re) |
| 152 |
} |
| 153 |
} |
| 154 |
if re != nil { |
| 155 |
// First answer in the chain must have the original qname. |
| 156 |
// But this is only true if we have a full chain. Use the saved re.Name |
| 157 |
r.Question[0].Header().Name = re.Name |
| 158 |
r.Answer = append(r.Answer, re.Answer...) |
| 159 |
} |
| 160 |
|
| 161 |
ds := false // if no DS is added we need an NSEC proofing it is not there. |
| 162 |
for _, rr := range encloser.RRs { |
| 163 |
if dns.RRToType(rr) == qtype { |
| 164 |
*section = append(*section, rr) |
| 165 |
} |
| 166 |
if hint == hintDelegation { |
| 167 |
if n, ok := rr.(*dns.NS); ok { |
| 168 |
// if the owner name is a child of the target we need to find the glue |
| 169 |
if dnsutil.IsBelow(rr.Header().Name, n.Ns) { |
| 170 |
if glue, ok := z.Get(n.Ns); ok { |
| 171 |
for _, rr := range glue.RRs { |
| 172 |
if _, ok := rr.(*dns.A); ok { |
| 173 |
r.Extra = append(r.Extra, rr) |
| 174 |
continue |
| 175 |
} |
| 176 |
if _, ok := rr.(*dns.AAAA); ok { |
| 177 |
r.Extra = append(r.Extra, rr) |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
if _, ok := rr.(*dns.DS); ok && r.Security { |
| 184 |
*section = append(*section, rr) |
| 185 |
ds = true |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
if r.Security { |
| 190 |
if hint == hintDelegation && !ds { |
| 191 |
for _, rr := range encloser.RRs { |
| 192 |
if dns.RRToType(rr) == dns.TypeNSEC { |
| 193 |
*section = append(*section, rr) |
| 194 |
} |
| 195 |
if s, ok := rr.(*dns.RRSIG); ok && s.TypeCovered == dns.TypeNSEC { |
| 196 |
*section = append(*section, rr) |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
for _, rr := range encloser.RRs { |
| 202 |
if s, ok := rr.(*dns.RRSIG); ok { |
| 203 |
if s.TypeCovered == qtype { |
| 204 |
*section = append(*section, rr) |
| 205 |
} |
| 206 |
if hint == hintDelegation { |
| 207 |
if s.TypeCovered == dns.TypeDS { |
| 208 |
*section = append(*section, rr) |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
if len(*section) > 0 { |
| 216 |
return r |
| 217 |
} |
| 218 |
|
| 219 |
// NODATA response. |
| 220 |
for _, rr := range z.Apex().RRs { |
| 221 |
if _, ok := rr.(*dns.SOA); ok { |
| 222 |
r.Ns = append(r.Ns, rr) |
| 223 |
continue |
| 224 |
} |
| 225 |
if r.Security { |
| 226 |
if _, ok := rr.(*dns.NSEC); ok { |
| 227 |
r.Ns = append(r.Ns, rr) |
| 228 |
} |
| 229 |
if s, ok := rr.(*dns.RRSIG); ok && (s.TypeCovered == dns.TypeSOA || s.TypeCovered == dns.TypeNSEC) { |
| 230 |
r.Ns = append(r.Ns, rr) |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
return r |
| 236 |
} |
| 237 |
|
| 238 |
// Canonical follows the cname chain. |
| 239 |
func Canonical(z Interface, r *dns.Msg, encloser *Node, re *Restart) *dns.Msg { |
| 240 |
if re == nil { |
| 241 |
re = &Restart{Name: r.Question[0].Header().Name} |
| 242 |
} |
| 243 |
|
| 244 |
for _, rr := range encloser.RRs { |
| 245 |
if c, ok := rr.(*dns.CNAME); ok { |
| 246 |
r.Question[0].Header().Name = c.Target |
| 247 |
re.Answer = append(re.Answer, rr) |
| 248 |
continue |
| 249 |
} |
| 250 |
if s, ok := rr.(*dns.RRSIG); ok && r.Security && s.TypeCovered == dns.TypeCNAME { |
| 251 |
re.Answer = append(re.Answer, rr) |
| 252 |
} |
| 253 |
} |
| 254 |
re.I++ |
| 255 |
if re.I > 7 { |
| 256 |
r.Question[0].Header().Name = re.Name |
| 257 |
return r |
| 258 |
} |
| 259 |
return Retrieve(z, r, re) |
| 260 |
} |