-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.go
588 lines (568 loc) · 16.5 KB
/
server.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/* Main program for the GRONG authoritative name server
Stephane Bortzmeyer <[email protected]>
*/
package main
import (
"bytes"
"encoding/binary"
"./myflag"
"fmt"
"net"
"os"
"strings"
"reflect"
"log"
"syslog"
"./responder"
"./types"
)
const defaultTTL = 3600
const loggerOptions = log.Ldate | log.Ltime | log.Lshortfile
var (
/* Configuration, indexed by keywords, for instance "debug" or
"servername" */
globalConfig map[string]interface{}
debug int // Not mandatory but it is simpler to use than
// globalConfig["debug"], which has type interface{}. Same thing for the
// others:
daemon bool
debuglogger, infologger, crisislogger *log.Logger
zone string
)
func fatal(msg string) {
crisislogger.Logf("%s\n", msg)
os.Exit(1)
}
// TODO: it would be nice to have a "fatal bool" parameter to indicate
// if we should stop but Go's functions do not have parameters with
// default values :-(
func checkError(msg string, error os.Error) {
if error != nil {
fatal(fmt.Sprintf("%s: %s", msg, error))
}
}
func serialize(packet types.DNSpacket) []byte {
// TODO: rewrite result as an io.Writer so we can just use Write? See
// the example in "Effective Go", section "Pointers vs Values"
result := make([]byte, packet.EdnsBufferSize)
// ID
binary.BigEndian.PutUint16(result[0:2], packet.Id)
// Misc flags...
result[2] = 0x80 // QR 1 (response), everything else 0
result[3] = byte(packet.Rcode)
binary.BigEndian.PutUint16(result[4:6], packet.Qdcount)
// Ancount
binary.BigEndian.PutUint16(result[6:8], packet.Ancount)
// Nscount
result[8] = 0
result[9] = 0
// Arcount
result[10] = 0
if packet.Edns {
result[11] = 1
} else {
result[11] = 0
}
if len(packet.Qsection) != 1 {
fatal(fmt.Sprintf("Qsection's length is not 1: %d\n", len(packet.Qsection)))
}
encoded_qname := types.Encode(packet.Qsection[0].Qname)
n := copy(result[12:], encoded_qname)
if n != len(encoded_qname) {
fatal(fmt.Sprintf("Copying %d bytes from a name of %d bytes\n",
n, len(encoded_qname)))
}
last := 12 + len(encoded_qname)
binary.BigEndian.PutUint16(result[last:], packet.Qsection[0].Qtype)
binary.BigEndian.PutUint16(result[last+2:], packet.Qsection[0].Qclass)
last = last + 4
for rrnum, rr := range packet.Ansection {
encoded_qname := types.Encode(packet.Ansection[rrnum].Name)
n = copy(result[last:], encoded_qname)
if n != len(encoded_qname) {
fatal(fmt.Sprintf("Copying %d bytes from a name of %d bytes\n",
n, len(encoded_qname)))
}
last = last + len(encoded_qname)
binary.BigEndian.PutUint16(result[last:last+2], rr.Type)
binary.BigEndian.PutUint16(result[last+2:last+4], rr.Class)
binary.BigEndian.PutUint32(result[last+4:last+8], rr.TTL)
binary.BigEndian.PutUint16(result[last+8:last+10], uint16(len(rr.Data)))
last = last + 10
n = copy(result[last:], packet.Ansection[rrnum].Data)
if n != len(packet.Ansection[rrnum].Data) {
fatal(fmt.Sprintf("Copying %d bytes from data of %d bytes\n",
n, len(packet.Ansection[rrnum].Data)))
}
last = last + len(packet.Ansection[rrnum].Data)
}
if packet.Edns {
result[last] = 0 // EDNS0's Name
binary.BigEndian.PutUint16(result[last+1:last+3], types.OPT)
binary.BigEndian.PutUint16(result[last+3:last+5], packet.EdnsBufferSize)
binary.BigEndian.PutUint32(result[last+5:last+9], 0)
servernamei, nameexists := globalConfig["servername"]
if nameexists {
servername := reflect.NewValue(servernamei).(*reflect.StringValue).Get()
if packet.Nsid {
binary.BigEndian.PutUint16(result[last+9:last+11],
uint16(4+len(servername)))
binary.BigEndian.PutUint16(result[last+11:last+13], types.NSID)
binary.BigEndian.PutUint16(result[last+13:last+15], uint16(len(servername)))
last += 15
n = copy(result[last:], []byte(servername))
if n != len(servername) {
fatal(fmt.Sprintf("Cannot copy servername (length %d bytes), %d bytes actually copied\n", len(servername), n))
}
last += int(len(servername))
} else {
// Zero EDNS options
binary.BigEndian.PutUint16(result[last+9:last+11], 0)
last += 11
}
}
}
return result[0:last]
}
func readShortInteger(buf *bytes.Buffer) (uint16, bool) {
slice := make([]byte, 2)
n, error := buf.Read(slice[0:2])
if error != nil || n != 2 {
if debug > 2 {
debuglogger.Logf("Error in Read of an int16: %s (%d bytes read)\n", error, n)
}
return 0, false
}
return binary.BigEndian.Uint16(slice[0:2]), true
}
func readInteger(buf *bytes.Buffer) (uint32, bool) {
slice := make([]byte, 4)
n, error := buf.Read(slice[0:4])
if error != nil || n != 4 {
if debug > 2 {
debuglogger.Logf("Error in Read of an int32: %s (%d bytes read)\n", error, n)
}
return 0, false
}
return binary.BigEndian.Uint32(slice[0:4]), true
}
func parse(buf *bytes.Buffer) (types.DNSpacket, bool) {
var (
packet types.DNSpacket
ok bool
)
// Initialize with sensible values
packet.Edns = false
packet.EdnsBufferSize = 512
packet.Nsid = false
packet.Id, ok = readShortInteger(buf)
if !ok {
return packet, false
}
dnsmisc, ok := readShortInteger(buf)
if !ok {
return packet, false
}
qr := (dnsmisc & 0x8000) >> 15
packet.Query = false
if qr == 0 {
packet.Query = true
}
packet.Opcode = uint((dnsmisc >> 11) & 0x000F)
rd := (dnsmisc & 0x0100) >> 8
packet.Recursion = false
if rd == 1 {
packet.Recursion = true
}
packet.Rcode = uint(dnsmisc & 0x000F)
packet.Qdcount, ok = readShortInteger(buf)
if !ok {
return packet, false
}
if packet.Qdcount != 1 {
// This may be legal but we would not know what to do with it
return packet, false
}
packet.Ancount, ok = readShortInteger(buf)
if !ok {
return packet, false
}
// TODO: reject packets with non-empty answer or authority sections
packet.Nscount, ok = readShortInteger(buf)
if !ok {
return packet, false
}
packet.Arcount, ok = readShortInteger(buf)
if !ok {
return packet, false
}
over := false
labels_max := make([]string, 63)
labels := labels_max[0:0]
// Parse the Question section
nlabels := 0
for !over {
labelsize, error := buf.ReadByte()
if error != nil {
if error == os.EOF {
return packet, false
} else {
if debug > 2 {
debuglogger.Logf("Error in ReadByte: %s\n", error)
}
return packet, false
}
}
if labelsize == 0 {
over = true
break
}
label := make([]byte, labelsize)
n, error := buf.Read(label)
if error != nil || n != int(labelsize) {
if error == nil {
// Client left after leaving only a few bytes
return packet, false
} else {
if debug > 2 {
debuglogger.Logf("Error in Read %d bytes: %s\n", n, error)
}
return packet, false
}
}
nlabels += 1
labels = labels[0:nlabels]
labels[nlabels-1] = string(label)
}
packet.Qsection = make([]types.Qentry, packet.Qdcount)
if len(labels) == 0 { // A special case, the root (see issue #4)
packet.Qsection[0].Qname = "."
} else {
packet.Qsection[0].Qname = strings.Join(labels, ".")
}
packet.Qsection[0].Qtype, ok = readShortInteger(buf)
if !ok {
return packet, false
}
packet.Qsection[0].Qclass, ok = readShortInteger(buf)
if !ok {
return packet, false
}
if packet.Arcount > 0 {
labelsize, error := buf.ReadByte()
if error != nil {
if error == os.EOF {
return packet, false
} else {
if debug > 2 {
debuglogger.Logf("Error in ReadByte: %s\n", error)
}
return packet, false
}
}
if labelsize != 0 {
if debug > 2 {
debuglogger.Logf("Additional section with non-empty name\n")
}
return packet, false
}
artype, ok := readShortInteger(buf)
if !ok {
return packet, false
}
if artype == types.OPT {
packet.Edns = true
packet.EdnsBufferSize, ok = readShortInteger(buf)
if !ok {
return packet, false
}
extrcode, ok := readInteger(buf)
if !ok {
return packet, false
}
ednslength, ok := readShortInteger(buf)
if !ok {
return packet, false
}
options := make([]byte, ednslength)
if ednslength > 0 {
n, error := buf.Read(options)
if error != nil || n != int(ednslength) {
if error == nil {
// Client left after leaving only a few bytes
return packet, false
} else {
if debug > 2 {
debuglogger.Logf("Error in Read %d bytes: %s\n", n, error)
}
return packet, false
}
}
over = false
counter := 0
for !over {
optcode := binary.BigEndian.Uint16(options[counter : counter+2])
if optcode == types.NSID {
packet.Nsid = true
}
optlen := int(binary.BigEndian.Uint16(options[counter+2 : counter+4]))
if optlen > 0 {
if counter+4+optlen > len(options) {
return packet, false
}
_ = options[counter+4 : counter+4+optlen] // Yes, useless, I know
}
counter += (4 + optlen)
if counter >= len(options) {
over = true
}
if debug > 3 {
debuglogger.Logf("EDNS option code %d\n", optcode)
}
}
}
if debug > 2 {
debuglogger.Logf("EDNS0 found, buffer size is %d, extended rcode is %d, ", packet.EdnsBufferSize, extrcode)
if ednslength > 0 {
debuglogger.Logf("length of options is %d\n", ednslength)
} else {
debuglogger.Logf("no options\n")
}
}
} else {
if debug > 2 {
debuglogger.Logf("Ignore additional section if not EDNS")
}
}
}
return packet, true
}
func generichandle(buf *bytes.Buffer, remaddr net.Addr) (response types.DNSpacket, noresponse bool) {
var (
query types.DNSquery
desiredresponse types.DNSresponse
)
noresponse = true
packet, valid := parse(buf)
if !valid { // Invalid packet or client too impatient
if debug > 3 {
debuglogger.Logf("Invalid packet received\n")
}
return
}
if debug > 2 {
debuglogger.Logf("%s\n", packet)
}
if packet.Query && packet.Opcode == types.STDQUERY {
if debug > 2 {
debuglogger.Logf("Replying with ID %d...\n", packet.Id)
}
noresponse = false
response.Id = packet.Id
response.Query = false
response.Opcode = packet.Opcode
response.Qdcount = 1 // Or packet.Qdcount ?
response.Qsection = make([]types.Qentry, response.Qdcount)
response.Qsection[0].Qname = packet.Qsection[0].Qname
response.Qsection[0].Qclass = packet.Qsection[0].Qclass
response.Qsection[0].Qtype = packet.Qsection[0].Qtype
response.Edns = packet.Edns
response.Nsid = packet.Nsid
query.Client = remaddr
query.Qname = strings.ToLower(packet.Qsection[0].Qname)
query.Qclass = packet.Qsection[0].Qclass
query.Qtype = packet.Qsection[0].Qtype
if packet.Edns {
query.BufferSize = packet.EdnsBufferSize
response.EdnsBufferSize = packet.EdnsBufferSize
} else {
query.BufferSize = 512 // Traditional value
response.EdnsBufferSize = 512
}
servernamei, nameexists := globalConfig["servername"]
if query.Qclass == types.CH && query.Qtype == types.TXT &&
(query.Qname == "hostname.bind" ||
query.Qname == "id.server") && nameexists {
servername := reflect.NewValue(servernamei).(*reflect.StringValue).Get()
desiredresponse.Responsecode = types.NOERROR
desiredresponse.Ansection = make([]types.RR, 1)
desiredresponse.Ansection[0] = types.RR{
Name: query.Qname,
TTL: defaultTTL,
Type: types.TXT,
Class: types.IN,
Data: types.ToTXT(servername)}
} else {
desiredresponse = responder.Respond(query, globalConfig)
}
response.Rcode = desiredresponse.Responsecode
response.Ancount = uint16(len(desiredresponse.Ansection))
if response.Ancount > 0 {
response.Ansection = desiredresponse.Ansection
}
return
}
// Else, ignore the incoming query. May be we should reply REFUSED instead?
noresponse = true
return
}
func udphandle(conn *net.UDPConn, remaddr net.Addr, buf *bytes.Buffer) {
var response types.DNSpacket
if debug > 1 {
debuglogger.Logf("%d bytes packet from %s\n", buf.Len(), remaddr)
}
response, noresponse := generichandle(buf, remaddr)
if !noresponse {
binaryresponse := serialize(response)
_, error := conn.WriteTo(binaryresponse, remaddr)
if error != nil {
if debug > 2 {
debuglogger.Logf("Error in Write: %s\n", error)
return
}
}
}
// Else, ignore the incoming packet. May be we should reply REFUSED instead?
}
func tcphandle(connection net.Conn) {
if debug > 1 {
debuglogger.Logf("TCP connection accepted from %s\n", connection.RemoteAddr())
}
smallbuf := make([]byte, 2)
n, error := connection.Read(smallbuf)
if error != nil {
if debug > 2 {
debuglogger.Logf("Cannot read message length from TCP connection: %s\n", error)
return
}
}
msglength := binary.BigEndian.Uint16(smallbuf) // RFC 1035, section 4.2.2 "TCP usage"
message := make([]byte, msglength)
n, error = connection.Read(message)
if error != nil {
if debug > 2 {
debuglogger.Logf("Cannot read message from TCP connection with %s: %s\n", connection.RemoteAddr(), error)
return
}
}
if debug > 1 {
debuglogger.Logf("%d bytes read from %s\n", n, connection.RemoteAddr())
}
response, noresponse := generichandle(bytes.NewBuffer(message), connection.RemoteAddr())
if !noresponse {
binaryresponse := serialize(response)
shortbuf := make([]byte, 2)
binary.BigEndian.PutUint16(shortbuf, uint16(len(binaryresponse)))
n, error := connection.Write(shortbuf)
if n != 2 || error != nil {
if debug > 2 {
debuglogger.Logf("Error in TCP message length Write: %s\n", error)
return
}
}
n, error = connection.Write(binaryresponse)
if error != nil {
if debug > 2 {
debuglogger.Logf("Error in TCP message Write: %s\n", error)
return
}
}
}
connection.Close() // In theory, we may have other requests. We clearly violate the RFC by not waiting for them. TODO
}
func tcpListener(address *net.TCPAddr, comm chan bool) {
listener, error := net.ListenTCP("udp", address)
checkError("Cannot listen", error)
for {
connection, error := listener.Accept()
if error != nil {
if debug > 1 {
debuglogger.Logf("Cannot accept TCP connection: %s\n", error)
continue
}
}
go tcphandle(connection)
}
listener.Close()
comm <- true
}
func udpListener(address *net.UDPAddr, comm chan bool) {
listener, error := net.ListenUDP("udp", address)
checkError("Cannot listen", error)
for {
message := make([]byte, 512) // 512 is a reasonable upper limit
// for *incoming* queries
n, remaddr, error := listener.ReadFrom(message)
if error != nil {
if debug > 1 {
debuglogger.Logf("Cannot read UDP from %s: %s\n", remaddr.String(), error)
continue
}
}
buf := bytes.NewBuffer(message[0:n])
go udphandle(listener, remaddr, buf)
}
listener.Close()
comm <- true
}
func main() {
debugptr := flag.Int("debug", 0, "Set the debug level, the higher, the more verbose")
nodaemonptr := flag.Bool("nodaemon", false, "Run in the foreground and not as a daemon")
listen := flag.String("address", ":8053", "Set the port (+optional address) to listen at")
nameptr := flag.String("servername", "",
"Set the server name (and send it to clients)")
helpptr := flag.Bool("help", false, "Displays usage instructions")
zoneptr := flag.String("domain", "", "Set the name of the zone we are authoritative for")
flag.Parse()
help := *helpptr
if help {
fmt.Printf("Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
os.Exit(0)
}
globalConfig = make(map[string]interface{})
namemsg := ""
if *nameptr != "" {
globalConfig["servername"] = *nameptr
namemsg = fmt.Sprintf(" %s", *nameptr)
}
zonemsg := ""
if *zoneptr != "" {
zone = strings.ToLower(*zoneptr)
globalConfig["zonename"] = zone
zonemsg = fmt.Sprintf(" on zone %s", zone)
}
debug = *debugptr
globalConfig["debug"] = *debugptr
globalConfig["daemon"] = !*nodaemonptr
daemon = !reflect.NewValue(*nodaemonptr).(*reflect.BoolValue).Get()
udpaddr, error := net.ResolveUDPAddr(*listen)
checkError(fmt.Sprintf("Cannot parse \"%s\": %s\n", *listen), error)
tcpaddr, error := net.ResolveTCPAddr(*listen)
checkError(fmt.Sprintf("Cannot parse \"%s\": %s\n", *listen), error)
if daemon {
debuglogger = syslog.NewLogger(syslog.LOG_DEBUG,
loggerOptions)
infologger = syslog.NewLogger(syslog.LOG_NOTICE,
loggerOptions)
crisislogger = syslog.NewLogger(syslog.LOG_CRIT,
loggerOptions)
} else {
debuglogger = log.New(os.Stderr, nil, "[DEBUG] ",
loggerOptions)
infologger = log.New(os.Stderr, nil, "[INFO] ",
loggerOptions)
crisislogger = log.New(os.Stderr, nil, "[FATAL] ",
loggerOptions)
}
responder.Init(flag.LastOption())
infologger.Logf("%s", fmt.Sprintf("Starting%s%s...", namemsg, zonemsg))
udpchan := make(chan bool)
go udpListener(udpaddr, udpchan)
tcpchan := make(chan bool)
go tcpListener(tcpaddr, tcpchan)
<-udpchan // Just to wait the listener, otherwise, the Go runtime ends
// even if there are live goroutines
<-tcpchan
infologger.Logf("%s", "Terminating...")
}