From c0a41f867faae02ad917bc7421fc3c72d3abb313 Mon Sep 17 00:00:00 2001 From: Tianon Gravi Date: Fri, 20 Jan 2017 13:09:38 -0800 Subject: [PATCH] Fix CNAME handling a bit If there's a CNAME in the list of RRs to return, _always_ put it in Answer (not Extra), regardless of the Question type. Additionally, when we perform recursive queries, our recursive query is what needs to be in the "name" field for the reply. Before: ```console $ dig ha.pool.sks-keyservers.net ; <<>> DiG 9.11.0-P2 <<>> ha.pool.sks-keyservers.net ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 6632 ;; flags: qr rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; WARNING: recursion requested but not available ;; QUESTION SECTION: ;ha.pool.sks-keyservers.net. IN A ;; ANSWER SECTION: ha.pool.sks-keyservers.net. 0 IN A 172.18.0.7 ;; ADDITIONAL SECTION: ha.pool.sks-keyservers.net. 0 IN CNAME haproxy-sks.docker. ;; Query time: 1 msec ;; SERVER: 172.18.42.1#53(172.18.42.1) ;; WHEN: Fri Jan 20 12:56:19 PST 2017 ;; MSG SIZE rcvd: 144 ``` After: ```console $ dig ha.pool.sks-keyservers.net ; <<>> DiG 9.11.0-P2 <<>> ha.pool.sks-keyservers.net ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 53265 ;; flags: qr rd; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0 ;; WARNING: recursion requested but not available ;; QUESTION SECTION: ;ha.pool.sks-keyservers.net. IN A ;; ANSWER SECTION: ha.pool.sks-keyservers.net. 0 IN CNAME haproxy-sks.docker. haproxy-sks.docker. 0 IN A 172.18.0.7 ;; Query time: 1 msec ;; SERVER: 172.18.42.1#53(172.18.42.1) ;; WHEN: Fri Jan 20 13:07:19 PST 2017 ;; MSG SIZE rcvd: 136 ``` --- src/cmd/rawdns/main.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/cmd/rawdns/main.go b/src/cmd/rawdns/main.go index 4dad48c..0e88d71 100644 --- a/src/cmd/rawdns/main.go +++ b/src/cmd/rawdns/main.go @@ -171,7 +171,7 @@ func dnsAppend(q dns.Question, m *dns.Msg, rr dns.RR) { return } - if q.Qtype == dns.TypeANY || q.Qtype == rr.Header().Rrtype { + if q.Qtype == dns.TypeANY || q.Qtype == rr.Header().Rrtype || rr.Header().Rrtype == dns.TypeCNAME { m.Answer = append(m.Answer, rr) } else { m.Extra = append(m.Extra, rr) @@ -240,20 +240,23 @@ func handleStaticRequest(config DomainConfig, w dns.ResponseWriter, r *dns.Msg) dnsAppend(q, m, &dns.CNAME{Target: cname}) if r.RecursionDesired && len(config.Nameservers) > 0 { + recQ := dns.Question{ + Name: cname, + Qtype: q.Qtype, + Qclass: q.Qclass, + } recR := &dns.Msg{ MsgHdr: dns.MsgHdr{ Id: dns.Id(), }, - Question: []dns.Question{ - {Name: cname, Qtype: q.Qtype, Qclass: q.Qclass}, - }, + Question: []dns.Question{recQ}, } recM := handleForwardingRaw(config.Nameservers, recR, w.RemoteAddr()) for _, rr := range recM.Answer { - dnsAppend(q, m, rr) + dnsAppend(recQ, m, rr) } for _, rr := range recM.Extra { - dnsAppend(q, m, rr) + dnsAppend(recQ, m, rr) } } }