| 1 |
package dns |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"net" |
| 8 |
"time" |
| 9 |
) |
| 10 |
|
| 11 |
// Envelope is used when doing a zone transfer with a remote server. |
| 12 |
type Envelope struct { |
| 13 |
Answer []RR // The RRs as returned by the remote server, or the ones to be send to the remote. |
| 14 |
Error error // If something went wrong, this contains the error. |
| 15 |
} |
| 16 |
|
| 17 |
// TransferIn performs a zone transfer with address over TCP, the message m is used to ask for the transfer and |
| 18 |
// should have an [AXFR] or [IXFR] RR in the question section. For doing an IXFR a SOA record needs to be |
| 19 |
// present in the [Ns] section of the [Msg], see RFC 1995. |
| 20 |
// |
| 21 |
// If the pseudo section contains a (stub) TSIG recrods, TSIG signing is performed, see [NewTSIG] on how create such RRs. For |
| 22 |
// this [TransferOption] needs a [TSIGSigner]. In the future SIG0 may be implemented as well. |
| 23 |
// |
| 24 |
// On the returned channel the received RRs are returned (and a non-nil error in case of an error). These RRs |
| 25 |
// are as they were found, i.e. including the starting and ending SOA RRs. |
| 26 |
// |
| 27 |
// If m's buffer is empty TransferIn will call m.Pack(). If the clients's transport is nil [NewDefaultTransport] will |
| 28 |
// be set and used. |
| 29 |
// |
| 30 |
// Setting up a transfer is done as follows: |
| 31 |
// |
| 32 |
// c := dns.NewClient() |
| 33 |
// m := dns.NewMsg("example.org.", dns.TypeAXFR) |
| 34 |
// options := &dns.TransferOption{TSIGSigner: dns.HmacTSIG{[]byte("secret")}} // optionally set up a TSIG with hmac |
| 35 |
// env, err := c.TransferIn(context.TODO(), m, addr, options) |
| 36 |
// if err != nil { |
| 37 |
// return fmt.Errorf("failed to setup zone transfer in", err) |
| 38 |
// } |
| 39 |
// |
| 40 |
// for e := range env { |
| 41 |
// if e.Error != nil { |
| 42 |
// // ... |
| 43 |
// } |
| 44 |
// // do things with e.Answer |
| 45 |
// } |
| 46 |
func (c *Client) TransferIn(ctx context.Context, m *Msg, address string, options *TransferOption) (<-chan *Envelope, error) { |
| 47 |
if c.Transport == nil { |
| 48 |
c.Transport = NewTransport() |
| 49 |
} |
| 50 |
conn, err := c.dial(ctx, "tcp", address) |
| 51 |
if err != nil { |
| 52 |
return nil, err |
| 53 |
} |
| 54 |
return c.TransferInWithConn(ctx, m, conn, options) |
| 55 |
} |
| 56 |
|
| 57 |
// TransferInWithConn behaves like [client.TransferIn], but with a supplied (TCP) connection. |
| 58 |
func (c *Client) TransferInWithConn(ctx context.Context, m *Msg, conn net.Conn, options *TransferOption) (<-chan *Envelope, error) { |
| 59 |
if _, ok := conn.(*net.UDPConn); ok { |
| 60 |
return nil, &Error{err: "bad network"} |
| 61 |
} |
| 62 |
_, axfr := m.Question[0].(*AXFR) |
| 63 |
_, ixfr := m.Question[0].(*IXFR) |
| 64 |
if !axfr && !ixfr { |
| 65 |
return nil, &Error{err: "unsupported transfer type"} |
| 66 |
} |
| 67 |
if ixfr { |
| 68 |
if len(m.Ns) == 0 { |
| 69 |
return nil, fmt.Errorf("%w: %s", ErrSOA, "empty Ns") |
| 70 |
} |
| 71 |
if _, ok := m.Ns[0].(*SOA); !ok { |
| 72 |
return nil, fmt.Errorf("%w: %s", ErrSOA, "bad Ns") |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if len(m.Data) == 0 { |
| 77 |
if err := m.Pack(); err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if options != nil && options.TSIGSigner != nil && hasTSIG(m) != nil { |
| 83 |
if err := TSIGSign(m, options.TSIGSigner, &TSIGOption{}); err != nil { |
| 84 |
return nil, err |
| 85 |
} |
| 86 |
} |
| 87 |
// if options.SIG0Signer != nil {} // TODO(miek): implement the whole SIG0 dance |
| 88 |
|
| 89 |
remote := &response{conn: conn} // for Session() call in msg.go#L926 |
| 90 |
if _, err := io.Copy(remote, m); err != nil { |
| 91 |
return nil, err |
| 92 |
} |
| 93 |
|
| 94 |
ch := make(chan *Envelope) |
| 95 |
if axfr { |
| 96 |
go c.transferInAXFR(ctx, m, ch, conn, options) |
| 97 |
} |
| 98 |
if ixfr { |
| 99 |
go c.transferInIXFR(ctx, m, ch, conn, options) |
| 100 |
} |
| 101 |
return ch, nil |
| 102 |
} |
| 103 |
|
| 104 |
func (c *Client) transferInAXFR(ctx context.Context, m *Msg, ch chan<- *Envelope, conn net.Conn, options *TransferOption) { |
| 105 |
defer func() { |
| 106 |
// First close the connection, then the channel. This allows functions blocked on the channel to |
| 107 |
// assume that the connection is closed and no further operations are pending when they resume. |
| 108 |
conn.Close() |
| 109 |
close(ch) |
| 110 |
}() |
| 111 |
|
| 112 |
tsigOptions := TSIGOption{} |
| 113 |
t := hasTSIG(m) |
| 114 |
if t != nil { |
| 115 |
tsigOptions.RequestMAC = t.MAC |
| 116 |
} |
| 117 |
|
| 118 |
r := &Msg{} |
| 119 |
for { |
| 120 |
r.Options = MsgOptionUnpackHeader |
| 121 |
conn.SetReadDeadline(time.Now().Add(c.ReadTimeout)) |
| 122 |
if _, err := io.Copy(r, conn); err != nil { |
| 123 |
// the response writer or actual conn is closed, just return, or some other error, we may |
| 124 |
// not be sure someone is still listening on this channel. |
| 125 |
return |
| 126 |
} |
| 127 |
if err := ctx.Err(); err != nil { |
| 128 |
ch <- &Envelope{Error: err} |
| 129 |
return |
| 130 |
} |
| 131 |
|
| 132 |
if err := r.Unpack(); err != nil { |
| 133 |
ch <- &Envelope{Error: err} |
| 134 |
return |
| 135 |
} |
| 136 |
|
| 137 |
if m.ID != r.ID { |
| 138 |
ch <- &Envelope{Error: fmt.Errorf("%w: %d != %d", ErrID, m.ID, r.ID)} |
| 139 |
return |
| 140 |
} |
| 141 |
|
| 142 |
if r.Rcode != RcodeSuccess { |
| 143 |
ch <- &Envelope{Error: fmt.Errorf("%w: %s", ErrRcode, rcodeToString(r.Rcode))} |
| 144 |
return |
| 145 |
} |
| 146 |
|
| 147 |
r.Options = MsgOptionUnpack |
| 148 |
err := r.Unpack() |
| 149 |
if err != nil { |
| 150 |
ch <- &Envelope{Answer: r.Answer, Error: err} |
| 151 |
return |
| 152 |
} |
| 153 |
|
| 154 |
// On first loop first be need to see a SOA RR. |
| 155 |
if !tsigOptions.TimersOnly { |
| 156 |
if len(r.Answer) == 0 { |
| 157 |
ch <- &Envelope{Error: fmt.Errorf("%w: %s", ErrSOA, "empty answer")} |
| 158 |
return |
| 159 |
} |
| 160 |
if _, ok := r.Answer[0].(*SOA); !ok { |
| 161 |
ch <- &Envelope{Error: ErrSOA} |
| 162 |
return |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
if options != nil && options.TSIGSigner != nil && t != nil { // Original request had tsig, so we need to check that. |
| 167 |
if err := TSIGVerify(r, options.TSIGSigner, &tsigOptions); err != nil { |
| 168 |
ch <- &Envelope{Answer: r.Answer, Error: err} |
| 169 |
return |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
ch <- &Envelope{Answer: r.Answer} |
| 174 |
|
| 175 |
// If there is a SOA RR as the last we're done |
| 176 |
if tsigOptions.TimersOnly { |
| 177 |
if len(r.Answer) > 0 { |
| 178 |
if _, ok := r.Answer[len(r.Answer)-1].(*SOA); ok { |
| 179 |
return |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
tsigOptions.TimersOnly = true |
| 185 |
if t != nil { |
| 186 |
// r must have tsig, otherwise errored out above |
| 187 |
tsigOptions.RequestMAC = hasTSIG(r).MAC |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
// ixfr is similar, but different enough to warrant its own function. Doing this in the axfr-loop is possible, |
| 193 |
// but make that more brittle. Although ifxr also needs to support axfr... |
| 194 |
func (c *Client) transferInIXFR(ctx context.Context, m *Msg, ch chan<- *Envelope, conn net.Conn, options *TransferOption) { |
| 195 |
defer func() { |
| 196 |
conn.Close() |
| 197 |
close(ch) |
| 198 |
}() |
| 199 |
|
| 200 |
tsigOptions := TSIGOption{} |
| 201 |
t := hasTSIG(m) |
| 202 |
if t != nil { |
| 203 |
tsigOptions.RequestMAC = t.MAC |
| 204 |
} |
| 205 |
// serial is the serial of the first SOA, it used to determine when we seen all the RRs. |
| 206 |
serial := uint32(0) |
| 207 |
// assume incremental transfer, which implies seeing the 2n SOA with serial 'serial', |
| 208 |
expectSOA := 1 // we ignore the first one and assume we get an axfr instead of ixfr, if the first msg indicates we do ifxr with +1 this. |
| 209 |
|
| 210 |
r := &Msg{} |
| 211 |
for { |
| 212 |
r.Options = MsgOptionUnpackHeader |
| 213 |
conn.SetReadDeadline(time.Now().Add(c.ReadTimeout)) |
| 214 |
if _, err := io.Copy(r, conn); err != nil { |
| 215 |
// the response writer or actual conn is closed, just return, or some other error, we may |
| 216 |
// not be sure someone is still listening on this channel. |
| 217 |
return |
| 218 |
} |
| 219 |
if err := ctx.Err(); err != nil { |
| 220 |
ch <- &Envelope{Error: err} |
| 221 |
return |
| 222 |
} |
| 223 |
|
| 224 |
if err := r.Unpack(); err != nil { |
| 225 |
ch <- &Envelope{Error: err} |
| 226 |
return |
| 227 |
} |
| 228 |
|
| 229 |
if m.ID != r.ID { |
| 230 |
ch <- &Envelope{Error: fmt.Errorf("%w: %d != %d", ErrID, m.ID, r.ID)} |
| 231 |
return |
| 232 |
} |
| 233 |
|
| 234 |
if r.Rcode != RcodeSuccess { |
| 235 |
ch <- &Envelope{Error: fmt.Errorf("%w: %s", ErrRcode, rcodeToString(r.Rcode))} |
| 236 |
return |
| 237 |
} |
| 238 |
|
| 239 |
r.Options = MsgOptionUnpack |
| 240 |
err := r.Unpack() |
| 241 |
if err != nil { |
| 242 |
ch <- &Envelope{Answer: r.Answer, Error: err} |
| 243 |
return |
| 244 |
} |
| 245 |
|
| 246 |
// On first loop first be need to see a SOA RR and check that with the request serial. |
| 247 |
if !tsigOptions.TimersOnly { |
| 248 |
if len(r.Answer) == 0 { |
| 249 |
ch <- &Envelope{Error: fmt.Errorf("%w: %s", ErrSOA, "empty answer")} |
| 250 |
return |
| 251 |
} |
| 252 |
if _, ok := r.Answer[0].(*SOA); !ok { |
| 253 |
ch <- &Envelope{Error: ErrSOA} |
| 254 |
return |
| 255 |
} |
| 256 |
serial = r.Answer[0].(*SOA).Serial |
| 257 |
// If client serial >= server serial per RFC 1982, we are already up to date. |
| 258 |
if CompareSerial(m.Ns[0].(*SOA).Serial, serial) >= 0 { |
| 259 |
ch <- &Envelope{Answer: []RR{r.Answer[0]}} |
| 260 |
return |
| 261 |
} |
| 262 |
if len(r.Answer) > 2 { |
| 263 |
if _, ok := r.Answer[1].(*SOA); ok { |
| 264 |
expectSOA++ |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
if options != nil && options.TSIGSigner != nil && t != nil { // Original request had tsig, so we need to check that. |
| 270 |
if err := TSIGVerify(r, options.TSIGSigner, &tsigOptions); err != nil { |
| 271 |
ch <- &Envelope{Answer: r.Answer, Error: err} |
| 272 |
return |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
ch <- &Envelope{Answer: r.Answer} |
| 277 |
|
| 278 |
// On the first message, skip index 0 (the opening SOA) — it is not the |
| 279 |
// terminal SOA. On subsequent messages start from 0 so nothing is missed. |
| 280 |
start := 0 |
| 281 |
if !tsigOptions.TimersOnly { |
| 282 |
start = 1 |
| 283 |
} |
| 284 |
|
| 285 |
// If we see the first SOA's serial expectSOA times we need to stop. |
| 286 |
for i := start; i < len(r.Answer); i++ { |
| 287 |
if s, ok := r.Answer[i].(*SOA); ok && s.Serial == serial { |
| 288 |
expectSOA-- |
| 289 |
if expectSOA == 0 { |
| 290 |
return |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
tsigOptions.TimersOnly = true |
| 296 |
if t != nil { |
| 297 |
// r must have tsig, otherwise errored out above |
| 298 |
tsigOptions.RequestMAC = hasTSIG(r).MAC |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
// TransferOut performs an outgoing transfer with the client connecting in w, r is the request |
| 304 |
// that initiates the transfer and is used for TSIG/SIG0. |
| 305 |
// |
| 306 |
// Example setup from within a dns.HandleFunc: |
| 307 |
// |
| 308 |
// r.Unpack() |
| 309 |
// w.Hijack() // hijack the connection as we should close when done |
| 310 |
// env := make(chan *dns.Envelope) |
| 311 |
// c := dns.NewClient() |
| 312 |
// var wg sync.WaitGroup |
| 313 |
// wg.Go(func() { |
| 314 |
// c.TransferOut(w, r, env) |
| 315 |
// w.Close() |
| 316 |
// }) |
| 317 |
// env <- &dns.Envelope{Answer: []dns.RR{...}} |
| 318 |
// close(env) |
| 319 |
// |
| 320 |
// The server is responsible for sending the correct sequence of RRs through the channel env. |
| 321 |
// If the clients's transport is nil [NewDefaultTransport] will be set and used. |
| 322 |
func (c *Client) TransferOut(w ResponseWriter, r *Msg, env <-chan *Envelope, options *TransferOption) (err error) { |
| 323 |
if _, ok := w.RemoteAddr().(*net.UDPAddr); ok { |
| 324 |
return &Error{err: "bad network"} |
| 325 |
} |
| 326 |
if c.Transport == nil { |
| 327 |
c.Transport = NewTransport() |
| 328 |
} |
| 329 |
defer func() { |
| 330 |
// drain channel reads |
| 331 |
for range env { |
| 332 |
} |
| 333 |
}() |
| 334 |
|
| 335 |
tsigOptions := TSIGOption{} |
| 336 |
t := hasTSIG(r) |
| 337 |
if t != nil { |
| 338 |
tsigOptions.RequestMAC = t.MAC |
| 339 |
} |
| 340 |
m := new(Msg) |
| 341 |
m.Authoritative = true |
| 342 |
// dnsutil.SetReply as used here, but led to all kinds of cyclic imports, just use that very static code here. |
| 343 |
m.ID, m.Rcode = r.ID, RcodeSuccess |
| 344 |
m.Response, m.Opcode = true, r.Opcode |
| 345 |
m.RecursionDesired = r.RecursionDesired |
| 346 |
m.CheckingDisabled = r.CheckingDisabled |
| 347 |
m.Security = r.Security |
| 348 |
m.Question = r.Question |
| 349 |
|
| 350 |
for e := range env { |
| 351 |
m.Reset() |
| 352 |
m.Answer = e.Answer |
| 353 |
if t != nil { |
| 354 |
m.Pseudo = []RR{t} // will overwrite the bits that matter |
| 355 |
} |
| 356 |
if err = m.Pack(); err != nil { |
| 357 |
return err |
| 358 |
} |
| 359 |
if options != nil && options.TSIGSigner != nil && t != nil { |
| 360 |
if err = TSIGSign(m, options.TSIGSigner, &tsigOptions); err != nil { |
| 361 |
return err |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
if _, err = io.Copy(w, m); err != nil { |
| 366 |
return err |
| 367 |
} |
| 368 |
|
| 369 |
tsigOptions.TimersOnly = true |
| 370 |
if t != nil { |
| 371 |
// m must have tsig, otherwise errored out above |
| 372 |
tsigOptions.RequestMAC = hasTSIG(m).MAC |
| 373 |
} |
| 374 |
} |
| 375 |
return nil |
| 376 |
} |