From d594c79d193a15d38d58a18d13684c84df96ea78 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Thu, 13 Dec 2012 10:08:35 +0100 Subject: [PATCH 01/10] A optional new origin can be used after $INCLUDE Add code to check for a new origin. --- zscan.go | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/zscan.go b/zscan.go index 76714fea..fc4e1d09 100644 --- a/zscan.go +++ b/zscan.go @@ -246,8 +246,28 @@ func parseZone(r io.Reader, origin, f string, t chan Token, include int) { t <- Token{Error: &ParseError{f, "expecting $INCLUDE value, not this...", l}} return } - if e := slurpRemainder(c, f); e != nil { - t <- Token{Error: e} + neworigin := origin // There may be optionally a new origin set after the filename, if not use current one + l := <-c + switch l.value { + case _BLANK: + l := <-c + if l.value == _STRING { + // a new origin is specified. + if !IsFqdn(l.token) { + if origin != "." { // Prevent .. endings + neworigin = l.token + "." + origin + } else { + neworigin = l.token + origin + } + } else { + neworigin = l.token + } + } + case _NEWLINE, _EOF: + // Ok + default: + t <- Token{Error: &ParseError{f, "garbage after $INCLUDE", l}} + return } // Start with the new file r1, e1 := os.Open(l.token) @@ -259,7 +279,7 @@ func parseZone(r io.Reader, origin, f string, t chan Token, include int) { t <- Token{Error: &ParseError{f, "too deeply nested $INCLUDE", l}} return } - parseZone(r1, l.token, origin, t, include+1) + parseZone(r1, l.token, neworigin, t, include+1) st = _EXPECT_OWNER_DIR case _EXPECT_DIRTTL_BL: if l.value != _BLANK { From 623de14ebb299b08d20d14f9115c14522ddd8999 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Thu, 13 Dec 2012 10:14:22 +0100 Subject: [PATCH 02/10] Add missing checks if $ORIGIN's name is valid Call IsDomainName() as $ORIGIN is rel. infrequent this does not impact the parsing speed. --- zscan.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/zscan.go b/zscan.go index fc4e1d09..903b806b 100644 --- a/zscan.go +++ b/zscan.go @@ -252,6 +252,10 @@ func parseZone(r io.Reader, origin, f string, t chan Token, include int) { case _BLANK: l := <-c if l.value == _STRING { + if _, _, ok := IsDomainName(l.token); !ok { + t <- Token{Error: &ParseError{f, "bad origin name", l}} + return + } // a new origin is specified. if !IsFqdn(l.token) { if origin != "." { // Prevent .. endings @@ -317,6 +321,10 @@ func parseZone(r io.Reader, origin, f string, t chan Token, include int) { if e := slurpRemainder(c, f); e != nil { t <- Token{Error: e} } + if _, _, ok := IsDomainName(l.token); !ok { + t <- Token{Error: &ParseError{f, "bad origin name", l}} + return + } if !IsFqdn(l.token) { if origin != "." { // Prevent .. endings origin = l.token + "." + origin From 0c7712073a1117494d39a5675f5694de772fd313 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Thu, 13 Dec 2012 10:47:33 +0100 Subject: [PATCH 03/10] Return the correct values --- ex/q/q.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ex/q/q.go b/ex/q/q.go index 2794386d..0247e84e 100644 --- a/ex/q/q.go +++ b/ex/q/q.go @@ -269,7 +269,7 @@ func tsigKeyParse(s string) (algo, name, secret string, ok bool) { case 3: switch s1[0] { case "hmac-md5": - return "hmac-md5.sig-alg.reg.int.", s1[0], s1[1], true + return "hmac-md5.sig-alg.reg.int.", s1[1], s1[2], true case "hmac-sha1": return "hmac-sha1.", s1[1], s1[2], true case "hmac-sha256": From 375917b7a081764fbeae688dfc56ced4c5d7225d Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Thu, 13 Dec 2012 13:10:34 +0100 Subject: [PATCH 04/10] update the todo --- TODO.markdown | 1 + tsig.go | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO.markdown b/TODO.markdown index f0c1fc6e..9b3b8c3a 100644 --- a/TODO.markdown +++ b/TODO.markdown @@ -6,6 +6,7 @@ from/to wireformat * NSEC3 support propper in the zone structure(s) * Test all rdata packing with zero rdata -- allowed for dynamic updates +* TSIG is not added in q when the query is for . ## Nice to have diff --git a/tsig.go b/tsig.go index 73c10dd5..fc1c69df 100644 --- a/tsig.go +++ b/tsig.go @@ -220,7 +220,6 @@ func TsigVerify(msg []byte, secret, requestMAC string, timersOnly bool) error { } buf := tsigBuffer(stripped, tsig, requestMAC, timersOnly) - ti := uint64(time.Now().Unix()) - tsig.TimeSigned if uint64(tsig.Fudge) < ti { return ErrTime From 99817d7c33812a4cf3eb3c3dfb1021c11def4624 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Thu, 13 Dec 2012 13:44:27 +0100 Subject: [PATCH 05/10] Fix TSIG If you even add a tag to a struct member, be sure to add that tag to the other important structs too. --- tsig.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tsig.go b/tsig.go index fc1c69df..644829d3 100644 --- a/tsig.go +++ b/tsig.go @@ -125,7 +125,7 @@ type tsigWireFmt struct { Ttl uint32 // Rdata of the TSIG Algorithm string `dns:"domain-name"` - TimeSigned uint64 + TimeSigned uint64 `dns:"uint48"` Fudge uint16 // MACSize, MAC and OrigId excluded Error uint16 @@ -142,7 +142,7 @@ type macWireFmt struct { // 3.3. Time values used in TSIG calculations type timerWireFmt struct { - TimeSigned uint64 + TimeSigned uint64 `dns:"uint48"` Fudge uint16 } From 025362a8206c383d1c0fd2fe622cbba09191e2c0 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Thu, 13 Dec 2012 13:55:58 +0100 Subject: [PATCH 06/10] Print the query packet after adding the tsig --- ex/q/q.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ex/q/q.go b/ex/q/q.go index 0247e84e..44a958cd 100644 --- a/ex/q/q.go +++ b/ex/q/q.go @@ -192,10 +192,6 @@ Flags: m.Question[0] = dns.Question{dns.Fqdn(v), qtype, qclass} m.Id = dns.Id() - if *query { - fmt.Printf("%s", m.String()) - fmt.Printf("\n;; size: %d bytes\n\n", m.Len()) - } // Add tsig if *tsig != "" { if algo, name, secret, ok := tsigKeyParse(*tsig); ok { @@ -206,6 +202,10 @@ Flags: return } } + if *query { + fmt.Printf("%s", m.String()) + fmt.Printf("\n;; size: %d bytes\n\n", m.Len()) + } if qtype == dns.TypeAXFR { c.Net = "tcp" doXfr(c, m, nameserver) From 9bb9dc990beb1e3cef83a375038c135b1afde9db Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Thu, 13 Dec 2012 13:56:23 +0100 Subject: [PATCH 07/10] remove todo item --- TODO.markdown | 1 - 1 file changed, 1 deletion(-) diff --git a/TODO.markdown b/TODO.markdown index 9b3b8c3a..f0c1fc6e 100644 --- a/TODO.markdown +++ b/TODO.markdown @@ -6,7 +6,6 @@ from/to wireformat * NSEC3 support propper in the zone structure(s) * Test all rdata packing with zero rdata -- allowed for dynamic updates -* TSIG is not added in q when the query is for . ## Nice to have From 7cdb2a8bc8b2530523b7ebedf864c14f96d2af03 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Thu, 13 Dec 2012 14:57:19 +0100 Subject: [PATCH 08/10] update the example tests --- parse_test.go | 76 ++++++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/parse_test.go b/parse_test.go index deb4dffe..c2a98eac 100644 --- a/parse_test.go +++ b/parse_test.go @@ -414,27 +414,6 @@ func TestZoneParsing(t *testing.T) { t.Logf("%d RRs parsed in %.2f s (%.2f RR/s)", i, float32(delta)/1e9, float32(i)/(float32(delta)/1e9)) } -// name. 3600 IN SOA a6.nstld.com. hostmaster.nic.name. 203362132 300 300 1209600 300 -// name. 10800 IN NS name. -// name. 10800 IN NS g6.nstld.com. -// name. 7200 IN NS h6.nstld.com. -// name. 3600 IN NS j6.nstld.com. -// name. 3600 IN NS k6.nstld.com. -// name. 10800 IN NS l6.nstld.com. -// name. 10800 IN NS a6.nstld.com. -// name. 10800 IN NS c6.nstld.com. -// name. 10800 IN NS d6.nstld.com. -// name. 10800 IN NS f6.nstld.com. -// name. 10800 IN NS m6.nstld.com. -// 0-0onlus.name. 10800 IN NS ns7.ehiweb.it. -// 0-0onlus.name. 10800 IN NS ns8.ehiweb.it. -// 0-g.name. 10800 IN MX 10 mx01.nic.name. -// 0-g.name. 10800 IN MX 10 mx02.nic.name. -// 0-g.name. 10800 IN MX 10 mx03.nic.name. -// 0-g.name. 10800 IN MX 10 mx04.nic.name. -// 0-g.name. 10800 IN TXT "10 mx\"04.nic" -// moutamassey.0-g.name.name. 10800 IN NS ns01.yahoodomains.jp. -// moutamassey.0-g.name.name. 10800 IN NS ns02.yahoodomains.jp. func ExampleZone() { zone := `$ORIGIN . $TTL 3600 ; 1 hour @@ -446,7 +425,7 @@ name IN SOA a6.nstld.com. hostmaster.nic.name. ( 300 ; minimum (5 minutes) ) $TTL 10800 ; 3 hours -@ 10800 IN NS @ +name. 10800 IN NS name. IN NS g6.nstld.com. 7200 NS h6.nstld.com. 3600 IN NS j6.nstld.com. @@ -457,6 +436,9 @@ $TTL 10800 ; 3 hours NS d6.nstld.com. NS f6.nstld.com. NS m6.nstld.com. +( + NS m7.nstld.com. +) $ORIGIN name. 0-0onlus NS ns7.ehiweb.it. NS ns8.ehiweb.it. @@ -473,9 +455,31 @@ moutamassey NS ns01.yahoodomains.jp. for x := range to { fmt.Printf("%s\n", x.RR) } + // Output: + // name. 3600 IN SOA a6.nstld.com. hostmaster.nic.name. 203362132 300 300 1209600 300 + // name. 10800 IN NS name. + // name. 10800 IN NS g6.nstld.com. + // name. 7200 IN NS h6.nstld.com. + // name. 3600 IN NS j6.nstld.com. + // name. 3600 IN NS k6.nstld.com. + // name. 10800 IN NS l6.nstld.com. + // name. 10800 IN NS a6.nstld.com. + // name. 10800 IN NS c6.nstld.com. + // name. 10800 IN NS d6.nstld.com. + // name. 10800 IN NS f6.nstld.com. + // name. 10800 IN NS m6.nstld.com. + // name. 10800 IN NS m7.nstld.com. + // 0-0onlus.name. 10800 IN NS ns7.ehiweb.it. + // 0-0onlus.name. 10800 IN NS ns8.ehiweb.it. + // 0-g.name. 10800 IN MX 10 mx01.nic.name. + // 0-g.name. 10800 IN MX 10 mx02.nic.name. + // 0-g.name. 10800 IN MX 10 mx03.nic.name. + // 0-g.name. 10800 IN MX 10 mx04.nic.name. + // 0-g.name. 10800 IN TXT "10 mx\\\"04.nic" + // moutamassey.0-g.name.name. 10800 IN NS ns01.yahoodomains.jp. + // moutamassey.0-g.name.name. 10800 IN NS ns02.yahoodomains.jp. } -// www.example.com. 3600 IN HIP 2 200100107B1A74DF365639CC39F1D578 AwEAAbdxyhNuSutc5EMzxTs9LBPCIkOFH8cIvM4p9+LrV4e19WzK00+CI6zBCQTdtWsuxKbWIy87UOoJTwkUs7lBu+Upr1gsNrut79ryra+bSRGQb1slImA8YVJyuIDsj7kwzG7jnERNqnWxZ48AWkskmdHaVDP4BcelrTI3rMXdXF5D rvs.example.com. func ExampleHIP() { h := `www.example.com IN HIP ( 2 200100107B1A74DF365639CC39F1D578 AwEAAbdxyhNuSutc5EMzxTs9LBPCIkOFH8cIvM4p @@ -485,14 +489,17 @@ b1slImA8YVJyuIDsj7kwzG7jnERNqnWxZ48AWkskmdHaVDP4BcelrTI3rMXdXF5D if hip, err := NewRR(h); err == nil { fmt.Printf("%s\n", hip.String()) } + // Output: + // www.example.com. 3600 IN HIP 2 200100107B1A74DF365639CC39F1D578 AwEAAbdxyhNuSutc5EMzxTs9LBPCIkOFH8cIvM4p9+LrV4e19WzK00+CI6zBCQTdtWsuxKbWIy87UOoJTwkUs7lBu+Upr1gsNrut79ryra+bSRGQb1slImA8YVJyuIDsj7kwzG7jnERNqnWxZ48AWkskmdHaVDP4BcelrTI3rMXdXF5D rvs.example.com. } -// example.com. 1000 IN SOA master.example.com. admin.example.com. 1 4294967294 4294967293 4294967295 100 func ExampleSOA() { s := "example.com. 1000 SOA master.example.com. admin.example.com. 1 4294967294 4294967293 4294967295 100" if soa, err := NewRR(s); err == nil { fmt.Printf("%s\n", soa.String()) } + // Output: + // example.com. 1000 IN SOA master.example.com. admin.example.com. 1 4294967294 4294967293 4294967295 100 } func TestLineNumberError(t *testing.T) { @@ -582,16 +589,6 @@ func TestEmpty(t *testing.T) { } } -// 0.0.0.192.IN-ADDR.ARPA. 3600 IN NS SERVER1.EXAMPLE. -// 0.0.0.192.IN-ADDR.ARPA. 3600 IN NS SERVER2.EXAMPLE. -// 1.0.0.192.IN-ADDR.ARPA. 3600 IN CNAME 1.0.0.0.192.IN-ADDR.ARPA. -// 2.0.0.192.IN-ADDR.ARPA. 3600 IN CNAME 2.0.0.0.192.IN-ADDR.ARPA. -// 3.0.0.192.IN-ADDR.ARPA. 3600 IN CNAME 3.0.0.0.192.IN-ADDR.ARPA. -// 4.0.0.192.IN-ADDR.ARPA. 3600 IN CNAME 4.0.0.0.192.IN-ADDR.ARPA. -// 5.0.0.192.IN-ADDR.ARPA. 3600 IN CNAME 5.0.0.0.192.IN-ADDR.ARPA. -// 6.0.0.192.IN-ADDR.ARPA. 3600 IN CNAME 6.0.0.0.192.IN-ADDR.ARPA. -// 7.0.0.192.IN-ADDR.ARPA. 3600 IN CNAME 7.0.0.0.192.IN-ADDR.ARPA. -// 8.0.0.192.IN-ADDR.ARPA. 3600 IN CNAME 8.0.0.0.192.IN-ADDR.ARPA. func ExampleGenerate() { // From the manual: http://www.bind9.net/manual/bind/9.3.2/Bv9ARM.ch06.html#id2566761 zone := "$GENERATE 1-2 0 NS SERVER$.EXAMPLE.\n$GENERATE 1-8 $ CNAME $.0" @@ -601,6 +598,17 @@ func ExampleGenerate() { fmt.Printf("%s\n", x.RR.String()) } } + // Output: + // 0.0.0.192.IN-ADDR.ARPA. 3600 IN NS SERVER1.EXAMPLE. + // 0.0.0.192.IN-ADDR.ARPA. 3600 IN NS SERVER2.EXAMPLE. + // 1.0.0.192.IN-ADDR.ARPA. 3600 IN CNAME 1.0.0.0.192.IN-ADDR.ARPA. + // 2.0.0.192.IN-ADDR.ARPA. 3600 IN CNAME 2.0.0.0.192.IN-ADDR.ARPA. + // 3.0.0.192.IN-ADDR.ARPA. 3600 IN CNAME 3.0.0.0.192.IN-ADDR.ARPA. + // 4.0.0.192.IN-ADDR.ARPA. 3600 IN CNAME 4.0.0.0.192.IN-ADDR.ARPA. + // 5.0.0.192.IN-ADDR.ARPA. 3600 IN CNAME 5.0.0.0.192.IN-ADDR.ARPA. + // 6.0.0.192.IN-ADDR.ARPA. 3600 IN CNAME 6.0.0.0.192.IN-ADDR.ARPA. + // 7.0.0.192.IN-ADDR.ARPA. 3600 IN CNAME 7.0.0.0.192.IN-ADDR.ARPA. + // 8.0.0.192.IN-ADDR.ARPA. 3600 IN CNAME 8.0.0.0.192.IN-ADDR.ARPA. } func TestSRVPacking(t *testing.T) { From 464a7bacf4ff02d50021b9847bebb5c7623f5da1 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Thu, 13 Dec 2012 15:11:22 +0100 Subject: [PATCH 09/10] For ommited ttls fall back to $TTL or 3600 As per RFC 2308 --- zscan.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/zscan.go b/zscan.go index 903b806b..3f942b5a 100644 --- a/zscan.go +++ b/zscan.go @@ -227,7 +227,8 @@ func parseZone(r io.Reader, origin, f string, t chan Token, include int) { return } else { h.Ttl = ttl - defttl = ttl + // Don't about the defttl, we should take the $TTL value + // defttl = ttl } st = _EXPECT_ANY_NOTTL_BL @@ -371,7 +372,7 @@ func parseZone(r io.Reader, origin, f string, t chan Token, include int) { return } else { h.Ttl = ttl - defttl = ttl + // defttl = ttl // don't set the defttl here } st = _EXPECT_ANY_NOTTL_BL default: @@ -410,7 +411,7 @@ func parseZone(r io.Reader, origin, f string, t chan Token, include int) { return } else { h.Ttl = ttl - defttl = ttl + // defttl = ttl // don't set the def ttl anymore } st = _EXPECT_RRTYPE_BL case _RRTYPE: From 3233964b4d9143ca08fc55c600630e7044121d6d Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Thu, 13 Dec 2012 15:23:44 +0100 Subject: [PATCH 10/10] Fix parse tests --- parse_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/parse_test.go b/parse_test.go index c2a98eac..24fe506b 100644 --- a/parse_test.go +++ b/parse_test.go @@ -446,7 +446,6 @@ $ORIGIN name. MX 10 mx02.nic MX 10 mx03.nic MX 10 mx04.nic - TXT "10 mx\"04.nic" $ORIGIN 0-g.name moutamassey NS ns01.yahoodomains.jp. NS ns02.yahoodomains.jp. @@ -475,7 +474,6 @@ moutamassey NS ns01.yahoodomains.jp. // 0-g.name. 10800 IN MX 10 mx02.nic.name. // 0-g.name. 10800 IN MX 10 mx03.nic.name. // 0-g.name. 10800 IN MX 10 mx04.nic.name. - // 0-g.name. 10800 IN TXT "10 mx\\\"04.nic" // moutamassey.0-g.name.name. 10800 IN NS ns01.yahoodomains.jp. // moutamassey.0-g.name.name. 10800 IN NS ns02.yahoodomains.jp. }