-
Notifications
You must be signed in to change notification settings - Fork 2
/
dns.go
executable file
·313 lines (288 loc) · 11 KB
/
dns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package yesdns
// Dns Message format: http://www.zytrax.com/books/dns/ch15/
// Depends on:
// db.go/Database
import (
"github.com/miekg/dns"
"log"
"net"
"time"
"errors"
"fmt"
)
func appendRR(dnsMsgSection *[]dns.RR, rrSection *DnsRR) error {
switch rrSection.Type {
case dns.TypeA:
appendA(dnsMsgSection, rrSection)
case dns.TypeAAAA:
appendAAAA(dnsMsgSection, rrSection)
case dns.TypeCNAME:
appendCNAME(dnsMsgSection, rrSection)
case dns.TypeMX:
appendMX(dnsMsgSection, rrSection)
case dns.TypeNS:
appendNS(dnsMsgSection, rrSection)
case dns.TypePTR:
appendPTR(dnsMsgSection, rrSection)
case dns.TypeSOA:
appendSOA(dnsMsgSection, rrSection)
case dns.TypeSRV:
appendSRV(dnsMsgSection, rrSection)
case dns.TypeTXT:
appendTXT(dnsMsgSection, rrSection)
default:
return errors.New(fmt.Sprintf("Don't know how to build RR for type %s", rrSection.Type))
}
return nil
}
func appendA(dnsMsgSection *[]dns.RR, rrSection *DnsRR) {
*dnsMsgSection = append(*dnsMsgSection,
&dns.A{
Hdr: dns.RR_Header{Name: rrSection.Name, Rrtype: rrSection.Type, Class: rrSection.Class, Ttl: rrSection.Ttl},
A: net.ParseIP(rrSection.Rdata.(string)),
},
)
}
func appendAAAA(dnsMsgSection *[]dns.RR, rrSection *DnsRR) {
*dnsMsgSection = append(*dnsMsgSection,
&dns.AAAA{
Hdr: dns.RR_Header{Name: rrSection.Name, Rrtype: rrSection.Type, Class: rrSection.Class, Ttl: rrSection.Ttl},
AAAA: net.ParseIP(rrSection.Rdata.(string)),
},
)
}
func appendCNAME(dnsMsgSection *[]dns.RR, rrSection *DnsRR) {
*dnsMsgSection = append(*dnsMsgSection,
&dns.CNAME{
Hdr: dns.RR_Header{Name: rrSection.Name, Rrtype: rrSection.Type, Class: rrSection.Class, Ttl: rrSection.Ttl},
Target: rrSection.Rdata.(string),
},
)
}
func appendMX(dnsMsgSection *[]dns.RR, rrSection *DnsRR) {
rdataMap := rrSection.Rdata.(map[string]interface{})
*dnsMsgSection = append(*dnsMsgSection,
&dns.MX{
Hdr: dns.RR_Header{Name: rrSection.Name, Rrtype: rrSection.Type, Class: rrSection.Class, Ttl: rrSection.Ttl},
Preference: uint16(rdataMap["preference"].(float64)),
Mx: rdataMap["mx"].(string),
},
)
}
func appendNS(dnsMsgSection *[]dns.RR, rrSection *DnsRR) {
*dnsMsgSection = append(*dnsMsgSection,
&dns.NS{
Hdr: dns.RR_Header{Name: rrSection.Name, Rrtype: rrSection.Type, Class: rrSection.Class, Ttl: rrSection.Ttl},
Ns: rrSection.Rdata.(string),
},
)
}
func appendPTR(dnsMsgSection *[]dns.RR, rrSection *DnsRR) {
*dnsMsgSection = append(*dnsMsgSection,
&dns.PTR{
Hdr: dns.RR_Header{Name: rrSection.Name, Rrtype: rrSection.Type, Class: rrSection.Class, Ttl: rrSection.Ttl},
Ptr: rrSection.Rdata.(string),
},
)
}
func appendSOA(dnsMsgSection *[]dns.RR, rrSection *DnsRR) {
rdataMap := rrSection.Rdata.(map[string]interface{})
*dnsMsgSection = append(*dnsMsgSection,
&dns.SOA{
Hdr: dns.RR_Header{Name: rrSection.Name, Rrtype: rrSection.Type, Class: rrSection.Class, Ttl: rrSection.Ttl},
Ns: rdataMap["ns"].(string),
Mbox: rdataMap["mbox"].(string),
Serial: uint32(rdataMap["serial"].(float64)),
Refresh: uint32(rdataMap["refresh"].(float64)),
Retry: uint32(rdataMap["retry"].(float64)),
Expire: uint32(rdataMap["expire"].(float64)),
Minttl: uint32(rdataMap["minttl"].(float64)),
},
)
}
func appendSRV(dnsMsgSection *[]dns.RR, rrSection *DnsRR) {
rdataMap := rrSection.Rdata.(map[string]interface{})
*dnsMsgSection = append(*dnsMsgSection,
&dns.SRV{
Hdr: dns.RR_Header{Name: rrSection.Name, Rrtype: rrSection.Type, Class: rrSection.Class, Ttl: rrSection.Ttl},
Priority: uint16(rdataMap["priority"].(float64)),
Weight: uint16(rdataMap["weight"].(float64)),
Port: uint16(rdataMap["port"].(float64)),
Target: rdataMap["target"].(string),
},
)
}
func appendTXT(dnsMsgSection *[]dns.RR, rrSection *DnsRR) {
// Convert rdata to slice of string
txtLines := make([]string, len(rrSection.Rdata.([]interface{})))
for _, line := range rrSection.Rdata.([]interface{}) {
txtLines = append(txtLines, line.(string))
}
*dnsMsgSection = append(*dnsMsgSection,
&dns.TXT{
Hdr: dns.RR_Header{Name: rrSection.Name, Rrtype: rrSection.Type, Class: rrSection.Class, Ttl: rrSection.Ttl},
Txt: txtLines,
},
)
}
// Handles DNS Query operation (OpCode 0)
// https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-5
func queryOperation(database *Database, dnsResponseWriter dns.ResponseWriter, requestDnsMsg *dns.Msg, resolver *Resolver) *dns.Msg {
queryDomain := requestDnsMsg.Question[0].Name
qtype := requestDnsMsg.Question[0].Qtype
// Try to resolve query and set status
err, resolvedDnsMessage := resolver.Resolve(qtype, queryDomain)
if err != nil {
// Lookup failed
return &dns.Msg{
Compress: false,
MsgHdr: dns.MsgHdr{
Id: requestDnsMsg.Id,
Opcode: requestDnsMsg.Opcode,
RecursionDesired: requestDnsMsg.RecursionDesired,
Rcode: dns.RcodeServerFailure,
RecursionAvailable: false,
Response: true,
Authoritative: false,
},
}
} else if resolvedDnsMessage == nil {
// Lookup did not error, but nothing found
return &dns.Msg{
Compress: false,
MsgHdr: dns.MsgHdr{
Id: requestDnsMsg.Id,
Opcode: requestDnsMsg.Opcode,
RecursionDesired: requestDnsMsg.RecursionDesired,
Rcode: dns.RcodeNameError, // aka NXDomain
RecursionAvailable: false,
Response: true,
Authoritative: false,
},
}
} // else: lookup did not error and answer found
returnDnsMsg := &dns.Msg{
Compress: false,
MsgHdr: dns.MsgHdr{
Id: requestDnsMsg.Id,
RecursionDesired: requestDnsMsg.RecursionDesired,
Opcode: requestDnsMsg.Opcode,
Response: true, // QR, Query/Response. 1 bit. 0=Query, 1=Response
// go's 'zero value' for int is 0, which == rcode 0 (NoError)
Rcode: resolvedDnsMessage.MsgHdr.Rcode,
// go's 'zero value' for boolean is false, so these all default to false if not supplied in json
RecursionAvailable: resolvedDnsMessage.MsgHdr.RecursionAvailable,
Authoritative: resolvedDnsMessage.MsgHdr.Authoritative, // AA, Authoritative Answer. 1 bit. 0=Not authoritative, 1=Is authoritative
Truncated: resolvedDnsMessage.MsgHdr.Truncated, // TC, Truncated. 1 bit. 0=Not truncated, 1=Message truncated
Zero: resolvedDnsMessage.MsgHdr.Zero,
AuthenticatedData: resolvedDnsMessage.MsgHdr.AuthenticatedData, // AD, Authenticated data. 1 bit. All data in the response has been cryptographically verified or otherwise meets the server's local security policy.
CheckingDisabled: resolvedDnsMessage.MsgHdr.CheckingDisabled, // CD, Checking Disabled. 1 bit.
},
}
// Build response Question section
for _, questionSection := range resolvedDnsMessage.Question {
dnsQuestion := dns.Question{Name: questionSection.Qname, Qtype: questionSection.Qtype, Qclass: questionSection.Qclass}
returnDnsMsg.Question = append(returnDnsMsg.Question, dnsQuestion)
}
// Build response Answer section
for _, rrSection := range resolvedDnsMessage.Answer {
if err := appendRR(&returnDnsMsg.Answer, &rrSection); err != nil {
log.Printf("WARN Cant build Answer section for type: %s.\n", rrSection.Type)
}
}
// Build response Authority section
for _, rrSection := range resolvedDnsMessage.Ns {
if err := appendRR(&returnDnsMsg.Ns, &rrSection); err != nil {
log.Printf("WARN Cant build Authority section for type: %s.\n", rrSection.Type)
}
}
// Build response Extra section
for _, rrSection := range resolvedDnsMessage.Extra {
if err := appendRR(&returnDnsMsg.Extra, &rrSection); err != nil {
log.Printf("WARN Cant build Extra section for type: %s.\n", rrSection.Type)
}
}
if requestDnsMsg.IsTsig() != nil {
if dnsResponseWriter.TsigStatus() == nil {
returnDnsMsg.SetTsig(requestDnsMsg.Extra[len(requestDnsMsg.Extra)-1].(*dns.TSIG).Hdr.Name, dns.HmacMD5, 300, time.Now().Unix())
} else {
log.Println("DEBUG Status", dnsResponseWriter.TsigStatus().Error())
}
}
return returnDnsMsg
}
// DNS query handler. Dispatches to operation handlers based on query OpCode.
// We use a closure to maintain a reference to the database.
func handleDnsQuery(database *Database, resolver *Resolver) func (dnsResponseWriter dns.ResponseWriter, requestDnsMsg *dns.Msg) {
return func (dnsResponseWriter dns.ResponseWriter, requestDnsMsg *dns.Msg) {
log.Printf("DEBUG Received query for resolver '%s' on local addr %s network %s. Message is: \n%s\n",
resolver.Id, dnsResponseWriter.LocalAddr(), dnsResponseWriter.LocalAddr().Network(), requestDnsMsg)
switch requestDnsMsg.Opcode {
case dns.OpcodeQuery:
// Try to find answer in our internal db
log.Printf("DEBUG Trying internal resolution with resolver '%s'\n", resolver.Id)
dnsMsg := queryOperation(database, dnsResponseWriter, requestDnsMsg, resolver)
log.Printf("DEBUG Internal resolution Rcode is %s\n", dnsMsg.Rcode)
if dnsMsg.Rcode == dns.RcodeSuccess {
log.Printf("DEBUG Internal resolution succeeded. Responding with message \n%s\n", dnsMsg)
dnsResponseWriter.WriteMsg(dnsMsg)
return
}
// We did not succeed in internal lookup, so try forwarders
log.Printf("DEBUG Trying resolver '%s' forwarders: %s\n", resolver.Id, resolver.Forwarders)
err, forwardDnsMsg := resolver.Forward(requestDnsMsg)
if err == nil && forwardDnsMsg != nil {
// Return successful forward resolution
log.Printf("DEBUG Forward resolution succeeded with Rcode %s. Responding with message: \n%s\n", forwardDnsMsg.Rcode, forwardDnsMsg)
dnsResponseWriter.WriteMsg(forwardDnsMsg)
} else {
// TODO separate log message for nil and not nil forwardDnsMsg
// TODO is this correct behavior?
// Default to our (failed) internal lookup
log.Printf("DEBUG Forward resolution failed. Returning (failed) internal lookup: \n%s\n", dnsMsg)
dnsResponseWriter.WriteMsg(dnsMsg)
}
default:
log.Printf("WARN Opcode %s not supported\n", requestDnsMsg.Opcode)
// Return a failure message
dnsMsg := new(dns.Msg)
dnsMsg.Rcode = dns.RcodeNotImplemented
dnsMsg.Id = requestDnsMsg.Id
dnsMsg.RecursionDesired = requestDnsMsg.RecursionDesired // Copy rd bit
dnsMsg.Response = true
dnsMsg.Opcode = requestDnsMsg.Opcode
dnsMsg.RecursionAvailable = false
dnsResponseWriter.WriteMsg(dnsMsg)
}
}
}
// Runs DNS server forever.
//
// if we read something from shutdownChannel, call server.Shutdown()
//
// net: (string) "tcp" or "udp"
// listenAddr: (string) ip addr and port to listen on
// name: (string) DNSSEC name.
// secret: (string) DNSSEC TSIG.
func serveDns(net, listenAddr, name, secret string, handler dns.Handler, shutdownChannel chan int) {
log.Printf("DEBUG Starting DNS listener on %s %s\n", net, listenAddr)
var server *dns.Server
// Support for TSIG
switch name {
case "":
server = &dns.Server{Addr: listenAddr, Net: net, TsigSecret: nil, Handler: handler}
default:
server = &dns.Server{Addr: listenAddr, Net: net, TsigSecret: map[string]string{name: secret}, Handler: handler}
}
// Start this up in an anonymous goroutine because server.ListenAndServe() blocks
go func() {
if err := server.ListenAndServe(); err != nil {
log.Printf("DEBUG Closed " + net + " server: %s\n", err.Error())
}
}()
// Wait (possibly forever) for shutdown signal
select {
case <-shutdownChannel:
server.Shutdown()
}
}