xfr.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package dns
  2. import (
  3. "time"
  4. )
  5. // Envelope is used when doing a zone transfer with a remote server.
  6. type Envelope struct {
  7. RR []RR // The set of RRs in the answer section of the xfr reply message.
  8. Error error // If something went wrong, this contains the error.
  9. }
  10. // A Transfer defines parameters that are used during a zone transfer.
  11. type Transfer struct {
  12. *Conn
  13. DialTimeout time.Duration // net.DialTimeout, defaults to 2 seconds
  14. ReadTimeout time.Duration // net.Conn.SetReadTimeout value for connections, defaults to 2 seconds
  15. WriteTimeout time.Duration // net.Conn.SetWriteTimeout value for connections, defaults to 2 seconds
  16. TsigSecret map[string]string // Secret(s) for Tsig map[<zonename>]<base64 secret>, zonename must be fully qualified
  17. tsigTimersOnly bool
  18. }
  19. // Think we need to away to stop the transfer
  20. // In performs an incoming transfer with the server in a.
  21. // If you would like to set the source IP, or some other attribute
  22. // of a Dialer for a Transfer, you can do so by specifying the attributes
  23. // in the Transfer.Conn:
  24. //
  25. // d := net.Dialer{LocalAddr: transfer_source}
  26. // con, err := d.Dial("tcp", master)
  27. // dnscon := &dns.Conn{Conn:con}
  28. // transfer = &dns.Transfer{Conn: dnscon}
  29. // channel, err := transfer.In(message, master)
  30. //
  31. func (t *Transfer) In(q *Msg, a string) (env chan *Envelope, err error) {
  32. timeout := dnsTimeout
  33. if t.DialTimeout != 0 {
  34. timeout = t.DialTimeout
  35. }
  36. if t.Conn == nil {
  37. t.Conn, err = DialTimeout("tcp", a, timeout)
  38. if err != nil {
  39. return nil, err
  40. }
  41. }
  42. if err := t.WriteMsg(q); err != nil {
  43. return nil, err
  44. }
  45. env = make(chan *Envelope)
  46. go func() {
  47. if q.Question[0].Qtype == TypeAXFR {
  48. go t.inAxfr(q.Id, env)
  49. return
  50. }
  51. if q.Question[0].Qtype == TypeIXFR {
  52. go t.inIxfr(q.Id, env)
  53. return
  54. }
  55. }()
  56. return env, nil
  57. }
  58. func (t *Transfer) inAxfr(id uint16, c chan *Envelope) {
  59. first := true
  60. defer t.Close()
  61. defer close(c)
  62. timeout := dnsTimeout
  63. if t.ReadTimeout != 0 {
  64. timeout = t.ReadTimeout
  65. }
  66. for {
  67. t.Conn.SetReadDeadline(time.Now().Add(timeout))
  68. in, err := t.ReadMsg()
  69. if err != nil {
  70. c <- &Envelope{nil, err}
  71. return
  72. }
  73. if id != in.Id {
  74. c <- &Envelope{in.Answer, ErrId}
  75. return
  76. }
  77. if first {
  78. if !isSOAFirst(in) {
  79. c <- &Envelope{in.Answer, ErrSoa}
  80. return
  81. }
  82. first = !first
  83. // only one answer that is SOA, receive more
  84. if len(in.Answer) == 1 {
  85. t.tsigTimersOnly = true
  86. c <- &Envelope{in.Answer, nil}
  87. continue
  88. }
  89. }
  90. if !first {
  91. t.tsigTimersOnly = true // Subsequent envelopes use this.
  92. if isSOALast(in) {
  93. c <- &Envelope{in.Answer, nil}
  94. return
  95. }
  96. c <- &Envelope{in.Answer, nil}
  97. }
  98. }
  99. }
  100. func (t *Transfer) inIxfr(id uint16, c chan *Envelope) {
  101. serial := uint32(0) // The first serial seen is the current server serial
  102. first := true
  103. defer t.Close()
  104. defer close(c)
  105. timeout := dnsTimeout
  106. if t.ReadTimeout != 0 {
  107. timeout = t.ReadTimeout
  108. }
  109. for {
  110. t.SetReadDeadline(time.Now().Add(timeout))
  111. in, err := t.ReadMsg()
  112. if err != nil {
  113. c <- &Envelope{nil, err}
  114. return
  115. }
  116. if id != in.Id {
  117. c <- &Envelope{in.Answer, ErrId}
  118. return
  119. }
  120. if first {
  121. // A single SOA RR signals "no changes"
  122. if len(in.Answer) == 1 && isSOAFirst(in) {
  123. c <- &Envelope{in.Answer, nil}
  124. return
  125. }
  126. // Check if the returned answer is ok
  127. if !isSOAFirst(in) {
  128. c <- &Envelope{in.Answer, ErrSoa}
  129. return
  130. }
  131. // This serial is important
  132. serial = in.Answer[0].(*SOA).Serial
  133. first = !first
  134. }
  135. // Now we need to check each message for SOA records, to see what we need to do
  136. if !first {
  137. t.tsigTimersOnly = true
  138. // If the last record in the IXFR contains the servers' SOA, we should quit
  139. if v, ok := in.Answer[len(in.Answer)-1].(*SOA); ok {
  140. if v.Serial == serial {
  141. c <- &Envelope{in.Answer, nil}
  142. return
  143. }
  144. }
  145. c <- &Envelope{in.Answer, nil}
  146. }
  147. }
  148. }
  149. // Out performs an outgoing transfer with the client connecting in w.
  150. // Basic use pattern:
  151. //
  152. // ch := make(chan *dns.Envelope)
  153. // tr := new(dns.Transfer)
  154. // tr.Out(w, r, ch)
  155. // c <- &dns.Envelope{RR: []dns.RR{soa, rr1, rr2, rr3, soa}}
  156. // close(ch)
  157. // w.Hijack()
  158. // // w.Close() // Client closes connection
  159. //
  160. // The server is responsible for sending the correct sequence of RRs through the
  161. // channel ch.
  162. func (t *Transfer) Out(w ResponseWriter, q *Msg, ch chan *Envelope) error {
  163. for x := range ch {
  164. r := new(Msg)
  165. // Compress?
  166. r.SetReply(q)
  167. r.Authoritative = true
  168. // assume it fits TODO(miek): fix
  169. r.Answer = append(r.Answer, x.RR...)
  170. if err := w.WriteMsg(r); err != nil {
  171. return err
  172. }
  173. }
  174. w.TsigTimersOnly(true)
  175. return nil
  176. }
  177. // ReadMsg reads a message from the transfer connection t.
  178. func (t *Transfer) ReadMsg() (*Msg, error) {
  179. m := new(Msg)
  180. p := make([]byte, MaxMsgSize)
  181. n, err := t.Read(p)
  182. if err != nil && n == 0 {
  183. return nil, err
  184. }
  185. p = p[:n]
  186. if err := m.Unpack(p); err != nil {
  187. return nil, err
  188. }
  189. if ts := m.IsTsig(); ts != nil && t.TsigSecret != nil {
  190. if _, ok := t.TsigSecret[ts.Hdr.Name]; !ok {
  191. return m, ErrSecret
  192. }
  193. // Need to work on the original message p, as that was used to calculate the tsig.
  194. err = TsigVerify(p, t.TsigSecret[ts.Hdr.Name], t.tsigRequestMAC, t.tsigTimersOnly)
  195. t.tsigRequestMAC = ts.MAC
  196. }
  197. return m, err
  198. }
  199. // WriteMsg writes a message through the transfer connection t.
  200. func (t *Transfer) WriteMsg(m *Msg) (err error) {
  201. var out []byte
  202. if ts := m.IsTsig(); ts != nil && t.TsigSecret != nil {
  203. if _, ok := t.TsigSecret[ts.Hdr.Name]; !ok {
  204. return ErrSecret
  205. }
  206. out, t.tsigRequestMAC, err = TsigGenerate(m, t.TsigSecret[ts.Hdr.Name], t.tsigRequestMAC, t.tsigTimersOnly)
  207. } else {
  208. out, err = m.Pack()
  209. }
  210. if err != nil {
  211. return err
  212. }
  213. if _, err = t.Write(out); err != nil {
  214. return err
  215. }
  216. return nil
  217. }
  218. func isSOAFirst(in *Msg) bool {
  219. if len(in.Answer) > 0 {
  220. return in.Answer[0].Header().Rrtype == TypeSOA
  221. }
  222. return false
  223. }
  224. func isSOALast(in *Msg) bool {
  225. if len(in.Answer) > 0 {
  226. return in.Answer[len(in.Answer)-1].Header().Rrtype == TypeSOA
  227. }
  228. return false
  229. }