mirror of
https://github.com/miekg/dns.git
synced 2025-10-11 18:01:02 +02:00
Fix error handling
This commit is contained in:
parent
e47ebb2e4c
commit
72c6ff37eb
11
dns.go
11
dns.go
@ -112,6 +112,9 @@ func (d *Conn) Read(p []byte) (n int, err os.Error) {
|
|||||||
if d.Tsig != nil {
|
if d.Tsig != nil {
|
||||||
// Check the TSIG that we should be read
|
// Check the TSIG that we should be read
|
||||||
_, err = d.Tsig.Verify(p)
|
_, err = d.Tsig.Verify(p)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -132,11 +135,9 @@ func (d *Conn) Write(p []byte) (n int, err os.Error) {
|
|||||||
|
|
||||||
if d.Tsig != nil {
|
if d.Tsig != nil {
|
||||||
// Create a new buffer with the TSIG added.
|
// Create a new buffer with the TSIG added.
|
||||||
var ok bool
|
q, err = d.Tsig.Generate(p)
|
||||||
q, ok = d.Tsig.Generate(p)
|
if err != nil {
|
||||||
if !ok {
|
return 0, err
|
||||||
// dikke shit
|
|
||||||
// Generate should return os.Error
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
q = p
|
q = p
|
||||||
|
42
tsig.go
42
tsig.go
@ -69,10 +69,10 @@ type timerWireFmt struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// In a message and out a new message with the tsig added
|
// In a message and out a new message with the tsig added
|
||||||
func (t *Tsig) Generate(msg []byte) ([]byte, bool) {
|
func (t *Tsig) Generate(msg []byte) ([]byte, os.Error) {
|
||||||
rawsecret, err := packBase64([]byte(t.Secret))
|
rawsecret, err := packBase64([]byte(t.Secret))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false
|
return nil, err
|
||||||
}
|
}
|
||||||
if t.Fudge == 0 {
|
if t.Fudge == 0 {
|
||||||
t.Fudge = 300
|
t.Fudge = 300
|
||||||
@ -81,21 +81,19 @@ func (t *Tsig) Generate(msg []byte) ([]byte, bool) {
|
|||||||
t.TimeSigned = uint64(time.Seconds())
|
t.TimeSigned = uint64(time.Seconds())
|
||||||
}
|
}
|
||||||
|
|
||||||
buf, ok := t.Buffer(msg)
|
buf, err := t.Buffer(msg)
|
||||||
if !ok {
|
if err != nil {
|
||||||
return nil, false
|
return nil, err
|
||||||
}
|
}
|
||||||
h := hmac.NewMD5([]byte(rawsecret))
|
h := hmac.NewMD5([]byte(rawsecret))
|
||||||
io.WriteString(h, string(buf))
|
io.WriteString(h, string(buf))
|
||||||
|
|
||||||
t.MAC = hex.EncodeToString(h.Sum()) // Size is half!
|
t.MAC = hex.EncodeToString(h.Sum()) // Size is half!
|
||||||
if !ok {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create TSIG and add it to the message.
|
// Create TSIG and add it to the message.
|
||||||
q := new(Msg)
|
q := new(Msg)
|
||||||
q.Unpack(msg) // TODO(mg): error handling
|
if !q.Unpack(msg) {
|
||||||
|
return nil, &Error{Error: "Failed to unpack"}
|
||||||
|
}
|
||||||
|
|
||||||
rr := new(RR_TSIG)
|
rr := new(RR_TSIG)
|
||||||
rr.Hdr = RR_Header{Name: t.Name, Rrtype: TypeTSIG, Class: ClassANY, Ttl: 0}
|
rr.Hdr = RR_Header{Name: t.Name, Rrtype: TypeTSIG, Class: ClassANY, Ttl: 0}
|
||||||
@ -108,7 +106,10 @@ func (t *Tsig) Generate(msg []byte) ([]byte, bool) {
|
|||||||
|
|
||||||
q.Extra = append(q.Extra, rr)
|
q.Extra = append(q.Extra, rr)
|
||||||
send, ok := q.Pack()
|
send, ok := q.Pack()
|
||||||
return send, ok
|
if !ok {
|
||||||
|
return send, &Error{Error: "Failed to pack"}
|
||||||
|
}
|
||||||
|
return send, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify a TSIG on a message. All relevant data should
|
// Verify a TSIG on a message. All relevant data should
|
||||||
@ -124,18 +125,21 @@ func (t *Tsig) Verify(msg []byte) (bool, os.Error) {
|
|||||||
return false, &Error{Error: "Failed to strip tsig"}
|
return false, &Error{Error: "Failed to strip tsig"}
|
||||||
}
|
}
|
||||||
|
|
||||||
buf, ok := t.Buffer(stripped)
|
buf,err := t.Buffer(stripped)
|
||||||
if !ok {
|
if err != nil {
|
||||||
return false, &Error{Error: "Failed to convert to raw buffer"}
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Time needs to be checked */
|
||||||
|
// Generic time error
|
||||||
|
|
||||||
h := hmac.NewMD5([]byte(rawsecret))
|
h := hmac.NewMD5([]byte(rawsecret))
|
||||||
io.WriteString(h, string(buf))
|
io.WriteString(h, string(buf))
|
||||||
return strings.ToUpper(hex.EncodeToString(h.Sum())) == strings.ToUpper(t.MAC), nil
|
return strings.ToUpper(hex.EncodeToString(h.Sum())) == strings.ToUpper(t.MAC), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a wiredata buffer for the MAC calculation
|
// Create a wiredata buffer for the MAC calculation
|
||||||
func (t *Tsig) Buffer(msg []byte) ([]byte, bool) {
|
func (t *Tsig) Buffer(msg []byte) ([]byte, os.Error) {
|
||||||
var (
|
var (
|
||||||
macbuf []byte
|
macbuf []byte
|
||||||
buf []byte
|
buf []byte
|
||||||
@ -148,7 +152,7 @@ func (t *Tsig) Buffer(msg []byte) ([]byte, bool) {
|
|||||||
macbuf = make([]byte, len(t.RequestMAC)) // reqmac should be twice as long
|
macbuf = make([]byte, len(t.RequestMAC)) // reqmac should be twice as long
|
||||||
n, ok := packStruct(m, macbuf, 0)
|
n, ok := packStruct(m, macbuf, 0)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, false
|
return nil, &Error{Error: "Failed to pack request mac"}
|
||||||
}
|
}
|
||||||
macbuf = macbuf[:n]
|
macbuf = macbuf[:n]
|
||||||
}
|
}
|
||||||
@ -160,7 +164,7 @@ func (t *Tsig) Buffer(msg []byte) ([]byte, bool) {
|
|||||||
tsig.Fudge = t.Fudge
|
tsig.Fudge = t.Fudge
|
||||||
n, ok1 := packStruct(tsig, tsigvar, 0)
|
n, ok1 := packStruct(tsig, tsigvar, 0)
|
||||||
if !ok1 {
|
if !ok1 {
|
||||||
return nil, false
|
return nil, &Error{Error: "Failed to pack timers"}
|
||||||
}
|
}
|
||||||
tsigvar = tsigvar[:n]
|
tsigvar = tsigvar[:n]
|
||||||
} else {
|
} else {
|
||||||
@ -176,7 +180,7 @@ func (t *Tsig) Buffer(msg []byte) ([]byte, bool) {
|
|||||||
tsig.OtherData = ""
|
tsig.OtherData = ""
|
||||||
n, ok1 := packStruct(tsig, tsigvar, 0)
|
n, ok1 := packStruct(tsig, tsigvar, 0)
|
||||||
if !ok1 {
|
if !ok1 {
|
||||||
return nil, false
|
return nil, &Error{Error: "Failed to pack tsig variables"}
|
||||||
}
|
}
|
||||||
tsigvar = tsigvar[:n]
|
tsigvar = tsigvar[:n]
|
||||||
}
|
}
|
||||||
@ -186,7 +190,7 @@ func (t *Tsig) Buffer(msg []byte) ([]byte, bool) {
|
|||||||
} else {
|
} else {
|
||||||
buf = append(msg, tsigvar...)
|
buf = append(msg, tsigvar...)
|
||||||
}
|
}
|
||||||
return buf, true
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Strip the TSIG from the pkt.
|
// Strip the TSIG from the pkt.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user