xfrin.c revision 1.11 1 /* $NetBSD: xfrin.c,v 1.11 2022/09/23 12:15:30 christos Exp $ */
2
3 /*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 *
12 * See the COPYRIGHT file distributed with this work for additional
13 * information regarding copyright ownership.
14 */
15
16 /*! \file */
17
18 #include <inttypes.h>
19 #include <stdbool.h>
20
21 #include <isc/mem.h>
22 #include <isc/print.h>
23 #include <isc/random.h>
24 #include <isc/string.h> /* Required for HP/UX (and others?) */
25 #include <isc/task.h>
26 #include <isc/timer.h>
27 #include <isc/util.h>
28
29 #include <dns/callbacks.h>
30 #include <dns/catz.h>
31 #include <dns/db.h>
32 #include <dns/diff.h>
33 #include <dns/events.h>
34 #include <dns/journal.h>
35 #include <dns/log.h>
36 #include <dns/message.h>
37 #include <dns/rdataclass.h>
38 #include <dns/rdatalist.h>
39 #include <dns/rdataset.h>
40 #include <dns/result.h>
41 #include <dns/soa.h>
42 #include <dns/tcpmsg.h>
43 #include <dns/timer.h>
44 #include <dns/tsig.h>
45 #include <dns/view.h>
46 #include <dns/xfrin.h>
47 #include <dns/zone.h>
48
49 #include <dst/dst.h>
50
51 /*
52 * Incoming AXFR and IXFR.
53 */
54
55 /*%
56 * It would be non-sensical (or at least obtuse) to use FAIL() with an
57 * ISC_R_SUCCESS code, but the test is there to keep the Solaris compiler
58 * from complaining about "end-of-loop code not reached".
59 */
60 #define FAIL(code) \
61 do { \
62 result = (code); \
63 if (result != ISC_R_SUCCESS) \
64 goto failure; \
65 } while (0)
66
67 #define CHECK(op) \
68 do { \
69 result = (op); \
70 if (result != ISC_R_SUCCESS) \
71 goto failure; \
72 } while (0)
73
74 /*%
75 * The states of the *XFR state machine. We handle both IXFR and AXFR
76 * with a single integrated state machine because they cannot be distinguished
77 * immediately - an AXFR response to an IXFR request can only be detected
78 * when the first two (2) response RRs have already been received.
79 */
80 typedef enum {
81 XFRST_SOAQUERY,
82 XFRST_GOTSOA,
83 XFRST_INITIALSOA,
84 XFRST_FIRSTDATA,
85 XFRST_IXFR_DELSOA,
86 XFRST_IXFR_DEL,
87 XFRST_IXFR_ADDSOA,
88 XFRST_IXFR_ADD,
89 XFRST_IXFR_END,
90 XFRST_AXFR,
91 XFRST_AXFR_END
92 } xfrin_state_t;
93
94 /*%
95 * Incoming zone transfer context.
96 */
97
98 struct dns_xfrin_ctx {
99 unsigned int magic;
100 isc_mem_t *mctx;
101 dns_zone_t *zone;
102
103 int refcount;
104
105 isc_task_t *task;
106 isc_timer_t *timer;
107 isc_socketmgr_t *socketmgr;
108
109 int connects; /*%< Connect in progress */
110 int sends; /*%< Send in progress */
111 int recvs; /*%< Receive in progress */
112 bool shuttingdown;
113 isc_result_t shutdown_result;
114
115 dns_name_t name; /*%< Name of zone to transfer */
116 dns_rdataclass_t rdclass;
117
118 bool checkid, logit;
119 dns_messageid_t id;
120
121 /*%
122 * Requested transfer type (dns_rdatatype_axfr or
123 * dns_rdatatype_ixfr). The actual transfer type
124 * may differ due to IXFR->AXFR fallback.
125 */
126 dns_rdatatype_t reqtype;
127 isc_dscp_t dscp;
128
129 isc_sockaddr_t masteraddr;
130 isc_sockaddr_t sourceaddr;
131 isc_socket_t *socket;
132
133 /*% Buffer for IXFR/AXFR request message */
134 isc_buffer_t qbuffer;
135 unsigned char qbuffer_data[512];
136
137 /*% Incoming reply TCP message */
138 dns_tcpmsg_t tcpmsg;
139 bool tcpmsg_valid;
140
141 /*%
142 * Whether the zone originally had a database attached at the time this
143 * transfer context was created. Used by maybe_free() when making
144 * logging decisions.
145 */
146 bool zone_had_db;
147
148 dns_db_t *db;
149 dns_dbversion_t *ver;
150 dns_diff_t diff; /*%< Pending database changes */
151 int difflen; /*%< Number of pending tuples */
152
153 xfrin_state_t state;
154 uint32_t end_serial;
155 bool is_ixfr;
156
157 unsigned int nmsg; /*%< Number of messages recvd */
158 unsigned int nrecs; /*%< Number of records recvd */
159 uint64_t nbytes; /*%< Number of bytes received */
160
161 unsigned int maxrecords; /*%< The maximum number of
162 * records set for the zone */
163
164 isc_time_t start; /*%< Start time of the transfer */
165 isc_time_t end; /*%< End time of the transfer */
166
167 dns_tsigkey_t *tsigkey; /*%< Key used to create TSIG */
168 isc_buffer_t *lasttsig; /*%< The last TSIG */
169 dst_context_t *tsigctx; /*%< TSIG verification context */
170 unsigned int sincetsig; /*%< recvd since the last TSIG */
171 dns_xfrindone_t done;
172
173 /*%
174 * AXFR- and IXFR-specific data. Only one is used at a time
175 * according to the is_ixfr flag, so this could be a union,
176 * but keeping them separate makes it a bit simpler to clean
177 * things up when destroying the context.
178 */
179 dns_rdatacallbacks_t axfr;
180
181 struct {
182 uint32_t request_serial;
183 uint32_t current_serial;
184 dns_journal_t *journal;
185 } ixfr;
186
187 dns_rdata_t firstsoa;
188 unsigned char *firstsoa_data;
189 };
190
191 #define XFRIN_MAGIC ISC_MAGIC('X', 'f', 'r', 'I')
192 #define VALID_XFRIN(x) ISC_MAGIC_VALID(x, XFRIN_MAGIC)
193
194 /**************************************************************************/
195 /*
196 * Forward declarations.
197 */
198
199 static isc_result_t
200 xfrin_create(isc_mem_t *mctx, dns_zone_t *zone, dns_db_t *db, isc_task_t *task,
201 isc_timermgr_t *timermgr, isc_socketmgr_t *socketmgr,
202 dns_name_t *zonename, dns_rdataclass_t rdclass,
203 dns_rdatatype_t reqtype, const isc_sockaddr_t *masteraddr,
204 const isc_sockaddr_t *sourceaddr, isc_dscp_t dscp,
205 dns_tsigkey_t *tsigkey, dns_xfrin_ctx_t **xfrp);
206
207 static isc_result_t
208 axfr_init(dns_xfrin_ctx_t *xfr);
209 static isc_result_t
210 axfr_makedb(dns_xfrin_ctx_t *xfr, dns_db_t **dbp);
211 static isc_result_t
212 axfr_putdata(dns_xfrin_ctx_t *xfr, dns_diffop_t op, dns_name_t *name,
213 dns_ttl_t ttl, dns_rdata_t *rdata);
214 static isc_result_t
215 axfr_apply(dns_xfrin_ctx_t *xfr);
216 static isc_result_t
217 axfr_commit(dns_xfrin_ctx_t *xfr);
218 static isc_result_t
219 axfr_finalize(dns_xfrin_ctx_t *xfr);
220
221 static isc_result_t
222 ixfr_init(dns_xfrin_ctx_t *xfr);
223 static isc_result_t
224 ixfr_apply(dns_xfrin_ctx_t *xfr);
225 static isc_result_t
226 ixfr_putdata(dns_xfrin_ctx_t *xfr, dns_diffop_t op, dns_name_t *name,
227 dns_ttl_t ttl, dns_rdata_t *rdata);
228 static isc_result_t
229 ixfr_commit(dns_xfrin_ctx_t *xfr);
230
231 static isc_result_t
232 xfr_rr(dns_xfrin_ctx_t *xfr, dns_name_t *name, uint32_t ttl,
233 dns_rdata_t *rdata);
234
235 static isc_result_t
236 xfrin_start(dns_xfrin_ctx_t *xfr);
237
238 static void
239 xfrin_connect_done(isc_task_t *task, isc_event_t *event);
240 static isc_result_t
241 xfrin_send_request(dns_xfrin_ctx_t *xfr);
242 static void
243 xfrin_send_done(isc_task_t *task, isc_event_t *event);
244 static void
245 xfrin_recv_done(isc_task_t *task, isc_event_t *event);
246 static void
247 xfrin_timeout(isc_task_t *task, isc_event_t *event);
248
249 static void
250 maybe_free(dns_xfrin_ctx_t *xfr);
251
252 static void
253 xfrin_fail(dns_xfrin_ctx_t *xfr, isc_result_t result, const char *msg);
254 static isc_result_t
255 render(dns_message_t *msg, isc_mem_t *mctx, isc_buffer_t *buf);
256
257 static void
258 xfrin_logv(int level, const char *zonetext, const isc_sockaddr_t *masteraddr,
259 const char *fmt, va_list ap) ISC_FORMAT_PRINTF(4, 0);
260
261 static void
262 xfrin_log1(int level, const char *zonetext, const isc_sockaddr_t *masteraddr,
263 const char *fmt, ...) ISC_FORMAT_PRINTF(4, 5);
264
265 static void
266 xfrin_log(dns_xfrin_ctx_t *xfr, int level, const char *fmt, ...)
267 ISC_FORMAT_PRINTF(3, 4);
268
269 /**************************************************************************/
270 /*
271 * AXFR handling
272 */
273
274 static isc_result_t
275 axfr_init(dns_xfrin_ctx_t *xfr) {
276 isc_result_t result;
277
278 xfr->is_ixfr = false;
279
280 if (xfr->db != NULL) {
281 dns_db_detach(&xfr->db);
282 }
283
284 CHECK(axfr_makedb(xfr, &xfr->db));
285 dns_rdatacallbacks_init(&xfr->axfr);
286 CHECK(dns_db_beginload(xfr->db, &xfr->axfr));
287 result = ISC_R_SUCCESS;
288 failure:
289 return (result);
290 }
291
292 static isc_result_t
293 axfr_makedb(dns_xfrin_ctx_t *xfr, dns_db_t **dbp) {
294 isc_result_t result;
295
296 result = dns_db_create(xfr->mctx, /* XXX */
297 "rbt", /* XXX guess */
298 &xfr->name, dns_dbtype_zone, xfr->rdclass, 0,
299 NULL, /* XXX guess */
300 dbp);
301 if (result == ISC_R_SUCCESS) {
302 dns_zone_rpz_enable_db(xfr->zone, *dbp);
303 dns_zone_catz_enable_db(xfr->zone, *dbp);
304 }
305 return (result);
306 }
307
308 static isc_result_t
309 axfr_putdata(dns_xfrin_ctx_t *xfr, dns_diffop_t op, dns_name_t *name,
310 dns_ttl_t ttl, dns_rdata_t *rdata) {
311 isc_result_t result;
312
313 dns_difftuple_t *tuple = NULL;
314
315 if (rdata->rdclass != xfr->rdclass) {
316 return (DNS_R_BADCLASS);
317 }
318
319 CHECK(dns_zone_checknames(xfr->zone, name, rdata));
320 CHECK(dns_difftuple_create(xfr->diff.mctx, op, name, ttl, rdata,
321 &tuple));
322 dns_diff_append(&xfr->diff, &tuple);
323 if (++xfr->difflen > 100) {
324 CHECK(axfr_apply(xfr));
325 }
326 result = ISC_R_SUCCESS;
327 failure:
328 return (result);
329 }
330
331 /*
332 * Store a set of AXFR RRs in the database.
333 */
334 static isc_result_t
335 axfr_apply(dns_xfrin_ctx_t *xfr) {
336 isc_result_t result;
337 uint64_t records;
338
339 CHECK(dns_diff_load(&xfr->diff, xfr->axfr.add, xfr->axfr.add_private));
340 xfr->difflen = 0;
341 dns_diff_clear(&xfr->diff);
342 if (xfr->maxrecords != 0U) {
343 result = dns_db_getsize(xfr->db, xfr->ver, &records, NULL);
344 if (result == ISC_R_SUCCESS && records > xfr->maxrecords) {
345 result = DNS_R_TOOMANYRECORDS;
346 goto failure;
347 }
348 }
349 result = ISC_R_SUCCESS;
350 failure:
351 return (result);
352 }
353
354 static isc_result_t
355 axfr_commit(dns_xfrin_ctx_t *xfr) {
356 isc_result_t result;
357
358 CHECK(axfr_apply(xfr));
359 CHECK(dns_db_endload(xfr->db, &xfr->axfr));
360 CHECK(dns_zone_verifydb(xfr->zone, xfr->db, NULL));
361
362 result = ISC_R_SUCCESS;
363 failure:
364 return (result);
365 }
366
367 static isc_result_t
368 axfr_finalize(dns_xfrin_ctx_t *xfr) {
369 isc_result_t result;
370
371 CHECK(dns_zone_replacedb(xfr->zone, xfr->db, true));
372
373 result = ISC_R_SUCCESS;
374 failure:
375 return (result);
376 }
377
378 /**************************************************************************/
379 /*
380 * IXFR handling
381 */
382
383 static isc_result_t
384 ixfr_init(dns_xfrin_ctx_t *xfr) {
385 isc_result_t result;
386 char *journalfile;
387
388 if (xfr->reqtype != dns_rdatatype_ixfr) {
389 xfrin_log(xfr, ISC_LOG_ERROR,
390 "got incremental response to AXFR request");
391 return (DNS_R_FORMERR);
392 }
393
394 xfr->is_ixfr = true;
395 INSIST(xfr->db != NULL);
396 xfr->difflen = 0;
397
398 journalfile = dns_zone_getjournal(xfr->zone);
399 if (journalfile != NULL) {
400 CHECK(dns_journal_open(xfr->mctx, journalfile,
401 DNS_JOURNAL_CREATE, &xfr->ixfr.journal));
402 }
403
404 result = ISC_R_SUCCESS;
405 failure:
406 return (result);
407 }
408
409 static isc_result_t
410 ixfr_putdata(dns_xfrin_ctx_t *xfr, dns_diffop_t op, dns_name_t *name,
411 dns_ttl_t ttl, dns_rdata_t *rdata) {
412 isc_result_t result;
413 dns_difftuple_t *tuple = NULL;
414
415 if (rdata->rdclass != xfr->rdclass) {
416 return (DNS_R_BADCLASS);
417 }
418
419 if (op == DNS_DIFFOP_ADD) {
420 CHECK(dns_zone_checknames(xfr->zone, name, rdata));
421 }
422 CHECK(dns_difftuple_create(xfr->diff.mctx, op, name, ttl, rdata,
423 &tuple));
424 dns_diff_append(&xfr->diff, &tuple);
425 if (++xfr->difflen > 100) {
426 CHECK(ixfr_apply(xfr));
427 }
428 result = ISC_R_SUCCESS;
429 failure:
430 return (result);
431 }
432
433 /*
434 * Apply a set of IXFR changes to the database.
435 */
436 static isc_result_t
437 ixfr_apply(dns_xfrin_ctx_t *xfr) {
438 isc_result_t result;
439 uint64_t records;
440
441 if (xfr->ver == NULL) {
442 CHECK(dns_db_newversion(xfr->db, &xfr->ver));
443 if (xfr->ixfr.journal != NULL) {
444 CHECK(dns_journal_begin_transaction(xfr->ixfr.journal));
445 }
446 }
447 CHECK(dns_diff_apply(&xfr->diff, xfr->db, xfr->ver));
448 if (xfr->maxrecords != 0U) {
449 result = dns_db_getsize(xfr->db, xfr->ver, &records, NULL);
450 if (result == ISC_R_SUCCESS && records > xfr->maxrecords) {
451 result = DNS_R_TOOMANYRECORDS;
452 goto failure;
453 }
454 }
455 if (xfr->ixfr.journal != NULL) {
456 result = dns_journal_writediff(xfr->ixfr.journal, &xfr->diff);
457 if (result != ISC_R_SUCCESS) {
458 goto failure;
459 }
460 }
461 dns_diff_clear(&xfr->diff);
462 xfr->difflen = 0;
463 result = ISC_R_SUCCESS;
464 failure:
465 return (result);
466 }
467
468 static isc_result_t
469 ixfr_commit(dns_xfrin_ctx_t *xfr) {
470 isc_result_t result;
471
472 CHECK(ixfr_apply(xfr));
473 if (xfr->ver != NULL) {
474 CHECK(dns_zone_verifydb(xfr->zone, xfr->db, xfr->ver));
475 /* XXX enter ready-to-commit state here */
476 if (xfr->ixfr.journal != NULL) {
477 CHECK(dns_journal_commit(xfr->ixfr.journal));
478 }
479 dns_db_closeversion(xfr->db, &xfr->ver, true);
480 dns_zone_markdirty(xfr->zone);
481 }
482 result = ISC_R_SUCCESS;
483 failure:
484 return (result);
485 }
486
487 /**************************************************************************/
488 /*
489 * Common AXFR/IXFR protocol code
490 */
491
492 /*
493 * Handle a single incoming resource record according to the current
494 * state.
495 */
496 static isc_result_t
497 xfr_rr(dns_xfrin_ctx_t *xfr, dns_name_t *name, uint32_t ttl,
498 dns_rdata_t *rdata) {
499 isc_result_t result;
500
501 xfr->nrecs++;
502
503 if (rdata->type == dns_rdatatype_none ||
504 dns_rdatatype_ismeta(rdata->type)) {
505 FAIL(DNS_R_FORMERR);
506 }
507
508 /*
509 * Immediately reject the entire transfer if the RR that is currently
510 * being processed is an SOA record that is not placed at the zone
511 * apex.
512 */
513 if (rdata->type == dns_rdatatype_soa &&
514 !dns_name_equal(&xfr->name, name)) {
515 char namebuf[DNS_NAME_FORMATSIZE];
516 dns_name_format(name, namebuf, sizeof(namebuf));
517 xfrin_log(xfr, ISC_LOG_DEBUG(3), "SOA name mismatch: '%s'",
518 namebuf);
519 FAIL(DNS_R_NOTZONETOP);
520 }
521
522 redo:
523 switch (xfr->state) {
524 case XFRST_SOAQUERY:
525 if (rdata->type != dns_rdatatype_soa) {
526 xfrin_log(xfr, ISC_LOG_ERROR,
527 "non-SOA response to SOA query");
528 FAIL(DNS_R_FORMERR);
529 }
530 xfr->end_serial = dns_soa_getserial(rdata);
531 if (!DNS_SERIAL_GT(xfr->end_serial, xfr->ixfr.request_serial) &&
532 !dns_zone_isforced(xfr->zone))
533 {
534 xfrin_log(xfr, ISC_LOG_DEBUG(3),
535 "requested serial %u, "
536 "master has %u, not updating",
537 xfr->ixfr.request_serial, xfr->end_serial);
538 FAIL(DNS_R_UPTODATE);
539 }
540 xfr->state = XFRST_GOTSOA;
541 break;
542
543 case XFRST_GOTSOA:
544 /*
545 * Skip other records in the answer section.
546 */
547 break;
548
549 case XFRST_INITIALSOA:
550 if (rdata->type != dns_rdatatype_soa) {
551 xfrin_log(xfr, ISC_LOG_ERROR,
552 "first RR in zone transfer must be SOA");
553 FAIL(DNS_R_FORMERR);
554 }
555 /*
556 * Remember the serial number in the initial SOA.
557 * We need it to recognize the end of an IXFR.
558 */
559 xfr->end_serial = dns_soa_getserial(rdata);
560 if (xfr->reqtype == dns_rdatatype_ixfr &&
561 !DNS_SERIAL_GT(xfr->end_serial, xfr->ixfr.request_serial) &&
562 !dns_zone_isforced(xfr->zone))
563 {
564 /*
565 * This must be the single SOA record that is
566 * sent when the current version on the master
567 * is not newer than the version in the request.
568 */
569 xfrin_log(xfr, ISC_LOG_DEBUG(3),
570 "requested serial %u, "
571 "master has %u, not updating",
572 xfr->ixfr.request_serial, xfr->end_serial);
573 FAIL(DNS_R_UPTODATE);
574 }
575 if (xfr->reqtype == dns_rdatatype_axfr) {
576 xfr->checkid = false;
577 }
578 xfr->firstsoa = *rdata;
579 if (xfr->firstsoa_data != NULL) {
580 isc_mem_free(xfr->mctx, xfr->firstsoa_data);
581 }
582 xfr->firstsoa_data = isc_mem_allocate(xfr->mctx, rdata->length);
583 memcpy(xfr->firstsoa_data, rdata->data, rdata->length);
584 xfr->firstsoa.data = xfr->firstsoa_data;
585 xfr->state = XFRST_FIRSTDATA;
586 break;
587
588 case XFRST_FIRSTDATA:
589 /*
590 * If the transfer begins with one SOA record, it is an AXFR,
591 * if it begins with two SOAs, it is an IXFR.
592 */
593 if (xfr->reqtype == dns_rdatatype_ixfr &&
594 rdata->type == dns_rdatatype_soa &&
595 xfr->ixfr.request_serial == dns_soa_getserial(rdata))
596 {
597 xfrin_log(xfr, ISC_LOG_DEBUG(3),
598 "got incremental response");
599 CHECK(ixfr_init(xfr));
600 xfr->state = XFRST_IXFR_DELSOA;
601 } else {
602 xfrin_log(xfr, ISC_LOG_DEBUG(3),
603 "got nonincremental response");
604 CHECK(axfr_init(xfr));
605 xfr->state = XFRST_AXFR;
606 }
607 goto redo;
608
609 case XFRST_IXFR_DELSOA:
610 INSIST(rdata->type == dns_rdatatype_soa);
611 CHECK(ixfr_putdata(xfr, DNS_DIFFOP_DEL, name, ttl, rdata));
612 xfr->state = XFRST_IXFR_DEL;
613 break;
614
615 case XFRST_IXFR_DEL:
616 if (rdata->type == dns_rdatatype_soa) {
617 uint32_t soa_serial = dns_soa_getserial(rdata);
618 xfr->state = XFRST_IXFR_ADDSOA;
619 xfr->ixfr.current_serial = soa_serial;
620 goto redo;
621 }
622 CHECK(ixfr_putdata(xfr, DNS_DIFFOP_DEL, name, ttl, rdata));
623 break;
624
625 case XFRST_IXFR_ADDSOA:
626 INSIST(rdata->type == dns_rdatatype_soa);
627 CHECK(ixfr_putdata(xfr, DNS_DIFFOP_ADD, name, ttl, rdata));
628 xfr->state = XFRST_IXFR_ADD;
629 break;
630
631 case XFRST_IXFR_ADD:
632 if (rdata->type == dns_rdatatype_soa) {
633 uint32_t soa_serial = dns_soa_getserial(rdata);
634 if (soa_serial == xfr->end_serial) {
635 CHECK(ixfr_commit(xfr));
636 xfr->state = XFRST_IXFR_END;
637 break;
638 } else if (soa_serial != xfr->ixfr.current_serial) {
639 xfrin_log(xfr, ISC_LOG_ERROR,
640 "IXFR out of sync: "
641 "expected serial %u, got %u",
642 xfr->ixfr.current_serial, soa_serial);
643 FAIL(DNS_R_FORMERR);
644 } else {
645 CHECK(ixfr_commit(xfr));
646 xfr->state = XFRST_IXFR_DELSOA;
647 goto redo;
648 }
649 }
650 if (rdata->type == dns_rdatatype_ns &&
651 dns_name_iswildcard(name)) {
652 FAIL(DNS_R_INVALIDNS);
653 }
654 CHECK(ixfr_putdata(xfr, DNS_DIFFOP_ADD, name, ttl, rdata));
655 break;
656
657 case XFRST_AXFR:
658 /*
659 * Old BINDs sent cross class A records for non IN classes.
660 */
661 if (rdata->type == dns_rdatatype_a &&
662 rdata->rdclass != xfr->rdclass &&
663 xfr->rdclass != dns_rdataclass_in)
664 {
665 break;
666 }
667 CHECK(axfr_putdata(xfr, DNS_DIFFOP_ADD, name, ttl, rdata));
668 if (rdata->type == dns_rdatatype_soa) {
669 /*
670 * Use dns_rdata_compare instead of memcmp to
671 * allow for case differences.
672 */
673 if (dns_rdata_compare(rdata, &xfr->firstsoa) != 0) {
674 xfrin_log(xfr, ISC_LOG_ERROR,
675 "start and ending SOA records "
676 "mismatch");
677 FAIL(DNS_R_FORMERR);
678 }
679 CHECK(axfr_commit(xfr));
680 xfr->state = XFRST_AXFR_END;
681 break;
682 }
683 break;
684 case XFRST_AXFR_END:
685 case XFRST_IXFR_END:
686 FAIL(DNS_R_EXTRADATA);
687 FALLTHROUGH;
688 default:
689 UNREACHABLE();
690 }
691 result = ISC_R_SUCCESS;
692 failure:
693 return (result);
694 }
695
696 isc_result_t
697 dns_xfrin_create(dns_zone_t *zone, dns_rdatatype_t xfrtype,
698 const isc_sockaddr_t *masteraddr,
699 const isc_sockaddr_t *sourceaddr, isc_dscp_t dscp,
700 dns_tsigkey_t *tsigkey, isc_mem_t *mctx,
701 isc_timermgr_t *timermgr, isc_socketmgr_t *socketmgr,
702 isc_task_t *task, dns_xfrindone_t done,
703 dns_xfrin_ctx_t **xfrp) {
704 dns_name_t *zonename = dns_zone_getorigin(zone);
705 dns_xfrin_ctx_t *xfr = NULL;
706 isc_result_t result;
707 dns_db_t *db = NULL;
708
709 REQUIRE(xfrp != NULL && *xfrp == NULL);
710
711 (void)dns_zone_getdb(zone, &db);
712
713 if (xfrtype == dns_rdatatype_soa || xfrtype == dns_rdatatype_ixfr) {
714 REQUIRE(db != NULL);
715 }
716
717 CHECK(xfrin_create(mctx, zone, db, task, timermgr, socketmgr, zonename,
718 dns_zone_getclass(zone), xfrtype, masteraddr,
719 sourceaddr, dscp, tsigkey, &xfr));
720
721 if (db != NULL) {
722 xfr->zone_had_db = true;
723 }
724
725 CHECK(xfrin_start(xfr));
726
727 xfr->done = done;
728 if (xfr->done != NULL) {
729 xfr->refcount++;
730 }
731 *xfrp = xfr;
732
733 failure:
734 if (db != NULL) {
735 dns_db_detach(&db);
736 }
737 if (result != ISC_R_SUCCESS) {
738 char zonetext[DNS_NAME_MAXTEXT + 32];
739 dns_zone_name(zone, zonetext, sizeof(zonetext));
740 xfrin_log1(ISC_LOG_ERROR, zonetext, masteraddr,
741 "zone transfer setup failed");
742 }
743 return (result);
744 }
745
746 void
747 dns_xfrin_shutdown(dns_xfrin_ctx_t *xfr) {
748 if (!xfr->shuttingdown) {
749 xfrin_fail(xfr, ISC_R_CANCELED, "shut down");
750 }
751 }
752
753 void
754 dns_xfrin_attach(dns_xfrin_ctx_t *source, dns_xfrin_ctx_t **target) {
755 REQUIRE(target != NULL && *target == NULL);
756 source->refcount++;
757 *target = source;
758 }
759
760 void
761 dns_xfrin_detach(dns_xfrin_ctx_t **xfrp) {
762 dns_xfrin_ctx_t *xfr = *xfrp;
763 *xfrp = NULL;
764 INSIST(xfr->refcount > 0);
765 xfr->refcount--;
766 maybe_free(xfr);
767 }
768
769 static void
770 xfrin_cancelio(dns_xfrin_ctx_t *xfr) {
771 if (xfr->connects > 0) {
772 isc_socket_cancel(xfr->socket, xfr->task,
773 ISC_SOCKCANCEL_CONNECT);
774 } else if (xfr->recvs > 0) {
775 dns_tcpmsg_cancelread(&xfr->tcpmsg);
776 } else if (xfr->sends > 0) {
777 isc_socket_cancel(xfr->socket, xfr->task, ISC_SOCKCANCEL_SEND);
778 }
779 }
780
781 static void
782 xfrin_reset(dns_xfrin_ctx_t *xfr) {
783 REQUIRE(VALID_XFRIN(xfr));
784
785 xfrin_log(xfr, ISC_LOG_INFO, "resetting");
786
787 xfrin_cancelio(xfr);
788
789 if (xfr->socket != NULL) {
790 isc_socket_detach(&xfr->socket);
791 }
792
793 if (xfr->lasttsig != NULL) {
794 isc_buffer_free(&xfr->lasttsig);
795 }
796
797 dns_diff_clear(&xfr->diff);
798 xfr->difflen = 0;
799
800 if (xfr->ixfr.journal != NULL) {
801 dns_journal_destroy(&xfr->ixfr.journal);
802 }
803
804 if (xfr->axfr.add_private != NULL) {
805 (void)dns_db_endload(xfr->db, &xfr->axfr);
806 }
807
808 if (xfr->tcpmsg_valid) {
809 dns_tcpmsg_invalidate(&xfr->tcpmsg);
810 xfr->tcpmsg_valid = false;
811 }
812
813 if (xfr->ver != NULL) {
814 dns_db_closeversion(xfr->db, &xfr->ver, false);
815 }
816 }
817
818 static void
819 xfrin_fail(dns_xfrin_ctx_t *xfr, isc_result_t result, const char *msg) {
820 if (result != DNS_R_UPTODATE && result != DNS_R_TOOMANYRECORDS) {
821 xfrin_log(xfr, ISC_LOG_ERROR, "%s: %s", msg,
822 isc_result_totext(result));
823 if (xfr->is_ixfr) {
824 /* Pass special result code to force AXFR retry */
825 result = DNS_R_BADIXFR;
826 }
827 }
828 xfrin_cancelio(xfr);
829 /*
830 * Close the journal.
831 */
832 if (xfr->ixfr.journal != NULL) {
833 dns_journal_destroy(&xfr->ixfr.journal);
834 }
835 if (xfr->done != NULL) {
836 (xfr->done)(xfr->zone, result);
837 xfr->done = NULL;
838 }
839 xfr->shuttingdown = true;
840 xfr->shutdown_result = result;
841 maybe_free(xfr);
842 }
843
844 static isc_result_t
845 xfrin_create(isc_mem_t *mctx, dns_zone_t *zone, dns_db_t *db, isc_task_t *task,
846 isc_timermgr_t *timermgr, isc_socketmgr_t *socketmgr,
847 dns_name_t *zonename, dns_rdataclass_t rdclass,
848 dns_rdatatype_t reqtype, const isc_sockaddr_t *masteraddr,
849 const isc_sockaddr_t *sourceaddr, isc_dscp_t dscp,
850 dns_tsigkey_t *tsigkey, dns_xfrin_ctx_t **xfrp) {
851 dns_xfrin_ctx_t *xfr = NULL;
852 isc_result_t result;
853
854 xfr = isc_mem_get(mctx, sizeof(*xfr));
855 xfr->mctx = NULL;
856 isc_mem_attach(mctx, &xfr->mctx);
857 xfr->refcount = 0;
858 xfr->zone = NULL;
859 dns_zone_iattach(zone, &xfr->zone);
860 xfr->task = NULL;
861 isc_task_attach(task, &xfr->task);
862 xfr->timer = NULL;
863 xfr->socketmgr = socketmgr;
864 xfr->done = NULL;
865
866 xfr->connects = 0;
867 xfr->sends = 0;
868 xfr->recvs = 0;
869 xfr->shuttingdown = false;
870 xfr->shutdown_result = ISC_R_UNSET;
871
872 dns_name_init(&xfr->name, NULL);
873 xfr->rdclass = rdclass;
874 xfr->checkid = true;
875 xfr->logit = true;
876 xfr->id = (dns_messageid_t)isc_random16();
877 xfr->reqtype = reqtype;
878 xfr->dscp = dscp;
879
880 /* sockaddr */
881 xfr->socket = NULL;
882 /* qbuffer */
883 /* qbuffer_data */
884 /* tcpmsg */
885 xfr->tcpmsg_valid = false;
886
887 xfr->zone_had_db = false;
888 xfr->db = NULL;
889 if (db != NULL) {
890 dns_db_attach(db, &xfr->db);
891 }
892 xfr->ver = NULL;
893 dns_diff_init(xfr->mctx, &xfr->diff);
894 xfr->difflen = 0;
895
896 if (reqtype == dns_rdatatype_soa) {
897 xfr->state = XFRST_SOAQUERY;
898 } else {
899 xfr->state = XFRST_INITIALSOA;
900 }
901 /* end_serial */
902
903 xfr->nmsg = 0;
904 xfr->nrecs = 0;
905 xfr->nbytes = 0;
906 xfr->maxrecords = dns_zone_getmaxrecords(zone);
907 isc_time_now(&xfr->start);
908
909 xfr->tsigkey = NULL;
910 if (tsigkey != NULL) {
911 dns_tsigkey_attach(tsigkey, &xfr->tsigkey);
912 }
913 xfr->lasttsig = NULL;
914 xfr->tsigctx = NULL;
915 xfr->sincetsig = 0;
916 xfr->is_ixfr = false;
917
918 /* ixfr.request_serial */
919 /* ixfr.current_serial */
920 xfr->ixfr.journal = NULL;
921
922 xfr->axfr.add = NULL;
923 xfr->axfr.add_private = NULL;
924 dns_rdata_init(&xfr->firstsoa);
925 xfr->firstsoa_data = NULL;
926
927 dns_name_dup(zonename, mctx, &xfr->name);
928
929 CHECK(isc_timer_create(timermgr, isc_timertype_inactive, NULL, NULL,
930 task, xfrin_timeout, xfr, &xfr->timer));
931 CHECK(dns_timer_setidle(xfr->timer, dns_zone_getmaxxfrin(xfr->zone),
932 dns_zone_getidlein(xfr->zone), false));
933
934 xfr->masteraddr = *masteraddr;
935
936 INSIST(isc_sockaddr_pf(masteraddr) == isc_sockaddr_pf(sourceaddr));
937 xfr->sourceaddr = *sourceaddr;
938 isc_sockaddr_setport(&xfr->sourceaddr, 0);
939
940 /*
941 * Reserve 2 bytes for TCP length at the beginning of the buffer.
942 */
943 isc_buffer_init(&xfr->qbuffer, &xfr->qbuffer_data[2],
944 sizeof(xfr->qbuffer_data) - 2);
945
946 xfr->magic = XFRIN_MAGIC;
947 *xfrp = xfr;
948 return (ISC_R_SUCCESS);
949
950 failure:
951 if (xfr->timer != NULL) {
952 isc_timer_detach(&xfr->timer);
953 }
954 if (dns_name_dynamic(&xfr->name)) {
955 dns_name_free(&xfr->name, xfr->mctx);
956 }
957 if (xfr->tsigkey != NULL) {
958 dns_tsigkey_detach(&xfr->tsigkey);
959 }
960 if (xfr->db != NULL) {
961 dns_db_detach(&xfr->db);
962 }
963 isc_task_detach(&xfr->task);
964 dns_zone_idetach(&xfr->zone);
965 isc_mem_putanddetach(&xfr->mctx, xfr, sizeof(*xfr));
966
967 return (result);
968 }
969
970 static isc_result_t
971 xfrin_start(dns_xfrin_ctx_t *xfr) {
972 isc_result_t result;
973 CHECK(isc_socket_create(xfr->socketmgr,
974 isc_sockaddr_pf(&xfr->sourceaddr),
975 isc_sockettype_tcp, &xfr->socket));
976 isc_socket_setname(xfr->socket, "xfrin", NULL);
977 #ifndef BROKEN_TCP_BIND_BEFORE_CONNECT
978 CHECK(isc_socket_bind(xfr->socket, &xfr->sourceaddr,
979 ISC_SOCKET_REUSEADDRESS));
980 #endif /* ifndef BROKEN_TCP_BIND_BEFORE_CONNECT */
981 isc_socket_dscp(xfr->socket, xfr->dscp);
982 CHECK(isc_socket_connect(xfr->socket, &xfr->masteraddr, xfr->task,
983 xfrin_connect_done, xfr));
984 xfr->connects++;
985 return (ISC_R_SUCCESS);
986 failure:
987 xfrin_fail(xfr, result, "failed setting up socket");
988 return (result);
989 }
990
991 /* XXX the resolver could use this, too */
992
993 static isc_result_t
994 render(dns_message_t *msg, isc_mem_t *mctx, isc_buffer_t *buf) {
995 dns_compress_t cctx;
996 bool cleanup_cctx = false;
997 isc_result_t result;
998
999 CHECK(dns_compress_init(&cctx, -1, mctx));
1000 cleanup_cctx = true;
1001 CHECK(dns_message_renderbegin(msg, &cctx, buf));
1002 CHECK(dns_message_rendersection(msg, DNS_SECTION_QUESTION, 0));
1003 CHECK(dns_message_rendersection(msg, DNS_SECTION_ANSWER, 0));
1004 CHECK(dns_message_rendersection(msg, DNS_SECTION_AUTHORITY, 0));
1005 CHECK(dns_message_rendersection(msg, DNS_SECTION_ADDITIONAL, 0));
1006 CHECK(dns_message_renderend(msg));
1007 result = ISC_R_SUCCESS;
1008 failure:
1009 if (cleanup_cctx) {
1010 dns_compress_invalidate(&cctx);
1011 }
1012 return (result);
1013 }
1014
1015 /*
1016 * A connection has been established.
1017 */
1018 static void
1019 xfrin_connect_done(isc_task_t *task, isc_event_t *event) {
1020 isc_socket_connev_t *cev = (isc_socket_connev_t *)event;
1021 dns_xfrin_ctx_t *xfr = (dns_xfrin_ctx_t *)event->ev_arg;
1022 isc_result_t result = cev->result;
1023 char sourcetext[ISC_SOCKADDR_FORMATSIZE];
1024 char signerbuf[DNS_NAME_FORMATSIZE];
1025 const char *signer = "", *sep = "";
1026 isc_sockaddr_t sockaddr;
1027 dns_zonemgr_t *zmgr;
1028 isc_time_t now;
1029
1030 REQUIRE(VALID_XFRIN(xfr));
1031
1032 UNUSED(task);
1033
1034 INSIST(event->ev_type == ISC_SOCKEVENT_CONNECT);
1035 isc_event_free(&event);
1036
1037 xfr->connects--;
1038 if (xfr->shuttingdown) {
1039 maybe_free(xfr);
1040 return;
1041 }
1042
1043 zmgr = dns_zone_getmgr(xfr->zone);
1044 if (zmgr != NULL) {
1045 if (result != ISC_R_SUCCESS) {
1046 TIME_NOW(&now);
1047 dns_zonemgr_unreachableadd(zmgr, &xfr->masteraddr,
1048 &xfr->sourceaddr, &now);
1049 goto failure;
1050 } else {
1051 dns_zonemgr_unreachabledel(zmgr, &xfr->masteraddr,
1052 &xfr->sourceaddr);
1053 }
1054 }
1055
1056 result = isc_socket_getsockname(xfr->socket, &sockaddr);
1057 if (result == ISC_R_SUCCESS) {
1058 isc_sockaddr_format(&sockaddr, sourcetext, sizeof(sourcetext));
1059 } else {
1060 strlcpy(sourcetext, "<UNKNOWN>", sizeof(sourcetext));
1061 }
1062
1063 if (xfr->tsigkey != NULL && xfr->tsigkey->key != NULL) {
1064 dns_name_format(dst_key_name(xfr->tsigkey->key), signerbuf,
1065 sizeof(signerbuf));
1066 sep = " TSIG ";
1067 signer = signerbuf;
1068 }
1069
1070 xfrin_log(xfr, ISC_LOG_INFO, "connected using %s%s%s", sourcetext, sep,
1071 signer);
1072
1073 dns_tcpmsg_init(xfr->mctx, xfr->socket, &xfr->tcpmsg);
1074 xfr->tcpmsg_valid = true;
1075
1076 CHECK(xfrin_send_request(xfr));
1077 failure:
1078 if (result != ISC_R_SUCCESS) {
1079 xfrin_fail(xfr, result, "failed to connect");
1080 }
1081 }
1082
1083 /*
1084 * Convert a tuple into a dns_name_t suitable for inserting
1085 * into the given dns_message_t.
1086 */
1087 static isc_result_t
1088 tuple2msgname(dns_difftuple_t *tuple, dns_message_t *msg, dns_name_t **target) {
1089 isc_result_t result;
1090 dns_rdata_t *rdata = NULL;
1091 dns_rdatalist_t *rdl = NULL;
1092 dns_rdataset_t *rds = NULL;
1093 dns_name_t *name = NULL;
1094
1095 REQUIRE(target != NULL && *target == NULL);
1096
1097 CHECK(dns_message_gettemprdata(msg, &rdata));
1098 dns_rdata_init(rdata);
1099 dns_rdata_clone(&tuple->rdata, rdata);
1100
1101 CHECK(dns_message_gettemprdatalist(msg, &rdl));
1102 dns_rdatalist_init(rdl);
1103 rdl->type = tuple->rdata.type;
1104 rdl->rdclass = tuple->rdata.rdclass;
1105 rdl->ttl = tuple->ttl;
1106 ISC_LIST_APPEND(rdl->rdata, rdata, link);
1107
1108 CHECK(dns_message_gettemprdataset(msg, &rds));
1109 CHECK(dns_rdatalist_tordataset(rdl, rds));
1110
1111 CHECK(dns_message_gettempname(msg, &name));
1112 dns_name_clone(&tuple->name, name);
1113 ISC_LIST_APPEND(name->list, rds, link);
1114
1115 *target = name;
1116 return (ISC_R_SUCCESS);
1117
1118 failure:
1119
1120 if (rds != NULL) {
1121 dns_rdataset_disassociate(rds);
1122 dns_message_puttemprdataset(msg, &rds);
1123 }
1124 if (rdl != NULL) {
1125 ISC_LIST_UNLINK(rdl->rdata, rdata, link);
1126 dns_message_puttemprdatalist(msg, &rdl);
1127 }
1128 if (rdata != NULL) {
1129 dns_message_puttemprdata(msg, &rdata);
1130 }
1131
1132 return (result);
1133 }
1134
1135 /*
1136 * Build an *XFR request and send its length prefix.
1137 */
1138 static isc_result_t
1139 xfrin_send_request(dns_xfrin_ctx_t *xfr) {
1140 isc_result_t result;
1141 isc_region_t region;
1142 dns_rdataset_t *qrdataset = NULL;
1143 dns_message_t *msg = NULL;
1144 dns_difftuple_t *soatuple = NULL;
1145 dns_name_t *qname = NULL;
1146 dns_dbversion_t *ver = NULL;
1147 dns_name_t *msgsoaname = NULL;
1148
1149 /* Create the request message */
1150 dns_message_create(xfr->mctx, DNS_MESSAGE_INTENTRENDER, &msg);
1151 CHECK(dns_message_settsigkey(msg, xfr->tsigkey));
1152
1153 /* Create a name for the question section. */
1154 CHECK(dns_message_gettempname(msg, &qname));
1155 dns_name_clone(&xfr->name, qname);
1156
1157 /* Formulate the question and attach it to the question name. */
1158 CHECK(dns_message_gettemprdataset(msg, &qrdataset));
1159 dns_rdataset_makequestion(qrdataset, xfr->rdclass, xfr->reqtype);
1160 ISC_LIST_APPEND(qname->list, qrdataset, link);
1161 qrdataset = NULL;
1162
1163 dns_message_addname(msg, qname, DNS_SECTION_QUESTION);
1164 qname = NULL;
1165
1166 if (xfr->reqtype == dns_rdatatype_ixfr) {
1167 /* Get the SOA and add it to the authority section. */
1168 /* XXX is using the current version the right thing? */
1169 dns_db_currentversion(xfr->db, &ver);
1170 CHECK(dns_db_createsoatuple(xfr->db, ver, xfr->mctx,
1171 DNS_DIFFOP_EXISTS, &soatuple));
1172 xfr->ixfr.request_serial = dns_soa_getserial(&soatuple->rdata);
1173 xfr->ixfr.current_serial = xfr->ixfr.request_serial;
1174 xfrin_log(xfr, ISC_LOG_DEBUG(3),
1175 "requesting IXFR for serial %u",
1176 xfr->ixfr.request_serial);
1177
1178 CHECK(tuple2msgname(soatuple, msg, &msgsoaname));
1179 dns_message_addname(msg, msgsoaname, DNS_SECTION_AUTHORITY);
1180 } else if (xfr->reqtype == dns_rdatatype_soa) {
1181 CHECK(dns_db_getsoaserial(xfr->db, NULL,
1182 &xfr->ixfr.request_serial));
1183 }
1184
1185 xfr->checkid = true;
1186 xfr->logit = true;
1187 xfr->id++;
1188 xfr->nmsg = 0;
1189 xfr->nrecs = 0;
1190 xfr->nbytes = 0;
1191 isc_time_now(&xfr->start);
1192 msg->id = xfr->id;
1193 if (xfr->tsigctx != NULL) {
1194 dst_context_destroy(&xfr->tsigctx);
1195 }
1196
1197 CHECK(render(msg, xfr->mctx, &xfr->qbuffer));
1198
1199 /*
1200 * Free the last tsig, if there is one.
1201 */
1202 if (xfr->lasttsig != NULL) {
1203 isc_buffer_free(&xfr->lasttsig);
1204 }
1205
1206 /*
1207 * Save the query TSIG and don't let message_destroy free it.
1208 */
1209 CHECK(dns_message_getquerytsig(msg, xfr->mctx, &xfr->lasttsig));
1210
1211 isc_buffer_usedregion(&xfr->qbuffer, ®ion);
1212 INSIST(region.length <= 65535);
1213
1214 /*
1215 * Record message length and adjust region to include TCP
1216 * length field.
1217 */
1218 xfr->qbuffer_data[0] = (region.length >> 8) & 0xff;
1219 xfr->qbuffer_data[1] = region.length & 0xff;
1220 region.base -= 2;
1221 region.length += 2;
1222 CHECK(isc_socket_send(xfr->socket, ®ion, xfr->task, xfrin_send_done,
1223 xfr));
1224 xfr->sends++;
1225
1226 failure:
1227 if (qname != NULL) {
1228 dns_message_puttempname(msg, &qname);
1229 }
1230 if (qrdataset != NULL) {
1231 dns_message_puttemprdataset(msg, &qrdataset);
1232 }
1233 if (msg != NULL) {
1234 dns_message_detach(&msg);
1235 }
1236 if (soatuple != NULL) {
1237 dns_difftuple_free(&soatuple);
1238 }
1239 if (ver != NULL) {
1240 dns_db_closeversion(xfr->db, &ver, false);
1241 }
1242 return (result);
1243 }
1244
1245 static void
1246 xfrin_send_done(isc_task_t *task, isc_event_t *event) {
1247 isc_socketevent_t *sev = (isc_socketevent_t *)event;
1248 dns_xfrin_ctx_t *xfr = (dns_xfrin_ctx_t *)event->ev_arg;
1249 isc_result_t result;
1250
1251 REQUIRE(VALID_XFRIN(xfr));
1252
1253 UNUSED(task);
1254
1255 INSIST(event->ev_type == ISC_SOCKEVENT_SENDDONE);
1256
1257 xfr->sends--;
1258 xfrin_log(xfr, ISC_LOG_DEBUG(3), "sent request data");
1259 CHECK(sev->result);
1260
1261 CHECK(dns_tcpmsg_readmessage(&xfr->tcpmsg, xfr->task, xfrin_recv_done,
1262 xfr));
1263 xfr->recvs++;
1264 failure:
1265 isc_event_free(&event);
1266 if (result != ISC_R_SUCCESS) {
1267 xfrin_fail(xfr, result, "failed sending request data");
1268 }
1269 }
1270
1271 static void
1272 xfrin_recv_done(isc_task_t *task, isc_event_t *ev) {
1273 dns_xfrin_ctx_t *xfr = (dns_xfrin_ctx_t *)ev->ev_arg;
1274 isc_result_t result;
1275 dns_message_t *msg = NULL;
1276 dns_name_t *name;
1277 dns_tcpmsg_t *tcpmsg;
1278 const dns_name_t *tsigowner = NULL;
1279
1280 REQUIRE(VALID_XFRIN(xfr));
1281
1282 UNUSED(task);
1283
1284 INSIST(ev->ev_type == DNS_EVENT_TCPMSG);
1285 tcpmsg = ev->ev_sender;
1286 isc_event_free(&ev);
1287
1288 xfr->recvs--;
1289 if (xfr->shuttingdown) {
1290 maybe_free(xfr);
1291 return;
1292 }
1293
1294 CHECK(tcpmsg->result);
1295
1296 xfrin_log(xfr, ISC_LOG_DEBUG(7), "received %u bytes",
1297 tcpmsg->buffer.used);
1298
1299 CHECK(isc_timer_touch(xfr->timer));
1300
1301 dns_message_create(xfr->mctx, DNS_MESSAGE_INTENTPARSE, &msg);
1302
1303 CHECK(dns_message_settsigkey(msg, xfr->tsigkey));
1304 CHECK(dns_message_setquerytsig(msg, xfr->lasttsig));
1305
1306 msg->tsigctx = xfr->tsigctx;
1307 xfr->tsigctx = NULL;
1308
1309 dns_message_setclass(msg, xfr->rdclass);
1310
1311 if (xfr->nmsg > 0) {
1312 msg->tcp_continuation = 1;
1313 }
1314
1315 result = dns_message_parse(msg, &tcpmsg->buffer,
1316 DNS_MESSAGEPARSE_PRESERVEORDER);
1317
1318 if (result == ISC_R_SUCCESS) {
1319 dns_message_logpacket(msg, "received message from",
1320 &tcpmsg->address, DNS_LOGCATEGORY_XFER_IN,
1321 DNS_LOGMODULE_XFER_IN, ISC_LOG_DEBUG(10),
1322 xfr->mctx);
1323 } else {
1324 xfrin_log(xfr, ISC_LOG_DEBUG(10), "dns_message_parse: %s",
1325 dns_result_totext(result));
1326 }
1327
1328 if (result != ISC_R_SUCCESS || msg->rcode != dns_rcode_noerror ||
1329 msg->opcode != dns_opcode_query || msg->rdclass != xfr->rdclass ||
1330 (xfr->checkid && msg->id != xfr->id))
1331 {
1332 if (result == ISC_R_SUCCESS && msg->rcode != dns_rcode_noerror)
1333 {
1334 result = ISC_RESULTCLASS_DNSRCODE + msg->rcode; /*XXX*/
1335 } else if (result == ISC_R_SUCCESS &&
1336 msg->opcode != dns_opcode_query) {
1337 result = DNS_R_UNEXPECTEDOPCODE;
1338 } else if (result == ISC_R_SUCCESS &&
1339 msg->rdclass != xfr->rdclass) {
1340 result = DNS_R_BADCLASS;
1341 } else if (result == ISC_R_SUCCESS || result == DNS_R_NOERROR) {
1342 result = DNS_R_UNEXPECTEDID;
1343 }
1344 if (xfr->reqtype == dns_rdatatype_axfr ||
1345 xfr->reqtype == dns_rdatatype_soa) {
1346 goto failure;
1347 }
1348 xfrin_log(xfr, ISC_LOG_DEBUG(3), "got %s, retrying with AXFR",
1349 isc_result_totext(result));
1350 try_axfr:
1351 dns_message_detach(&msg);
1352 xfrin_reset(xfr);
1353 xfr->reqtype = dns_rdatatype_soa;
1354 xfr->state = XFRST_SOAQUERY;
1355 (void)xfrin_start(xfr);
1356 return;
1357 } else if (!xfr->checkid && msg->id != xfr->id && xfr->logit) {
1358 xfrin_log(xfr, ISC_LOG_WARNING,
1359 "detected message ID mismatch on incoming AXFR "
1360 "stream, transfer will fail in BIND 9.17.2 and "
1361 "later if AXFR source is not fixed");
1362 xfr->logit = false;
1363 }
1364
1365 /*
1366 * Does the server know about IXFR? If it doesn't we will get
1367 * a message with a empty answer section or a potentially a CNAME /
1368 * DNAME, the later is handled by xfr_rr() which will return FORMERR
1369 * if the first RR in the answer section is not a SOA record.
1370 */
1371 if (xfr->reqtype == dns_rdatatype_ixfr &&
1372 xfr->state == XFRST_INITIALSOA &&
1373 msg->counts[DNS_SECTION_ANSWER] == 0)
1374 {
1375 xfrin_log(xfr, ISC_LOG_DEBUG(3),
1376 "empty answer section, retrying with AXFR");
1377 goto try_axfr;
1378 }
1379
1380 if (xfr->reqtype == dns_rdatatype_soa &&
1381 (msg->flags & DNS_MESSAGEFLAG_AA) == 0) {
1382 FAIL(DNS_R_NOTAUTHORITATIVE);
1383 }
1384
1385 result = dns_message_checksig(msg, dns_zone_getview(xfr->zone));
1386 if (result != ISC_R_SUCCESS) {
1387 xfrin_log(xfr, ISC_LOG_DEBUG(3), "TSIG check failed: %s",
1388 isc_result_totext(result));
1389 goto failure;
1390 }
1391
1392 for (result = dns_message_firstname(msg, DNS_SECTION_ANSWER);
1393 result == ISC_R_SUCCESS;
1394 result = dns_message_nextname(msg, DNS_SECTION_ANSWER))
1395 {
1396 dns_rdataset_t *rds;
1397
1398 name = NULL;
1399 dns_message_currentname(msg, DNS_SECTION_ANSWER, &name);
1400 for (rds = ISC_LIST_HEAD(name->list); rds != NULL;
1401 rds = ISC_LIST_NEXT(rds, link))
1402 {
1403 for (result = dns_rdataset_first(rds);
1404 result == ISC_R_SUCCESS;
1405 result = dns_rdataset_next(rds))
1406 {
1407 dns_rdata_t rdata = DNS_RDATA_INIT;
1408 dns_rdataset_current(rds, &rdata);
1409 CHECK(xfr_rr(xfr, name, rds->ttl, &rdata));
1410 }
1411 }
1412 }
1413 if (result != ISC_R_NOMORE) {
1414 goto failure;
1415 }
1416
1417 if (dns_message_gettsig(msg, &tsigowner) != NULL) {
1418 /*
1419 * Reset the counter.
1420 */
1421 xfr->sincetsig = 0;
1422
1423 /*
1424 * Free the last tsig, if there is one.
1425 */
1426 if (xfr->lasttsig != NULL) {
1427 isc_buffer_free(&xfr->lasttsig);
1428 }
1429
1430 /*
1431 * Update the last tsig pointer.
1432 */
1433 CHECK(dns_message_getquerytsig(msg, xfr->mctx, &xfr->lasttsig));
1434 } else if (dns_message_gettsigkey(msg) != NULL) {
1435 xfr->sincetsig++;
1436 if (xfr->sincetsig > 100 || xfr->nmsg == 0 ||
1437 xfr->state == XFRST_AXFR_END ||
1438 xfr->state == XFRST_IXFR_END)
1439 {
1440 result = DNS_R_EXPECTEDTSIG;
1441 goto failure;
1442 }
1443 }
1444
1445 /*
1446 * Update the number of messages received.
1447 */
1448 xfr->nmsg++;
1449
1450 /*
1451 * Update the number of bytes received.
1452 */
1453 xfr->nbytes += tcpmsg->buffer.used;
1454
1455 /*
1456 * Take the context back.
1457 */
1458 INSIST(xfr->tsigctx == NULL);
1459 xfr->tsigctx = msg->tsigctx;
1460 msg->tsigctx = NULL;
1461
1462 dns_message_detach(&msg);
1463
1464 switch (xfr->state) {
1465 case XFRST_GOTSOA:
1466 xfr->reqtype = dns_rdatatype_axfr;
1467 xfr->state = XFRST_INITIALSOA;
1468 CHECK(xfrin_send_request(xfr));
1469 break;
1470 case XFRST_AXFR_END:
1471 CHECK(axfr_finalize(xfr));
1472 FALLTHROUGH;
1473 case XFRST_IXFR_END:
1474 /*
1475 * Close the journal.
1476 */
1477 if (xfr->ixfr.journal != NULL) {
1478 dns_journal_destroy(&xfr->ixfr.journal);
1479 }
1480
1481 /*
1482 * Inform the caller we succeeded.
1483 */
1484 if (xfr->done != NULL) {
1485 (xfr->done)(xfr->zone, ISC_R_SUCCESS);
1486 xfr->done = NULL;
1487 }
1488 /*
1489 * We should have no outstanding events at this
1490 * point, thus maybe_free() should succeed.
1491 */
1492 xfr->shuttingdown = true;
1493 xfr->shutdown_result = ISC_R_SUCCESS;
1494 maybe_free(xfr);
1495 break;
1496 default:
1497 /*
1498 * Read the next message.
1499 */
1500 CHECK(dns_tcpmsg_readmessage(&xfr->tcpmsg, xfr->task,
1501 xfrin_recv_done, xfr));
1502 xfr->recvs++;
1503 }
1504 return;
1505
1506 failure:
1507 if (msg != NULL) {
1508 dns_message_detach(&msg);
1509 }
1510 if (result != ISC_R_SUCCESS) {
1511 xfrin_fail(xfr, result, "failed while receiving responses");
1512 }
1513 }
1514
1515 static void
1516 xfrin_timeout(isc_task_t *task, isc_event_t *event) {
1517 dns_xfrin_ctx_t *xfr = (dns_xfrin_ctx_t *)event->ev_arg;
1518
1519 REQUIRE(VALID_XFRIN(xfr));
1520
1521 UNUSED(task);
1522
1523 isc_event_free(&event);
1524 /*
1525 * This will log "giving up: timeout".
1526 */
1527 xfrin_fail(xfr, ISC_R_TIMEDOUT, "giving up");
1528 }
1529
1530 static void
1531 maybe_free(dns_xfrin_ctx_t *xfr) {
1532 uint64_t msecs;
1533 uint64_t persec;
1534 const char *result_str;
1535
1536 REQUIRE(VALID_XFRIN(xfr));
1537
1538 if (!xfr->shuttingdown || xfr->refcount != 0 || xfr->connects != 0 ||
1539 xfr->sends != 0 || xfr->recvs != 0)
1540 {
1541 return;
1542 }
1543
1544 INSIST(!xfr->shuttingdown || xfr->shutdown_result != ISC_R_UNSET);
1545
1546 /* If we're called through dns_xfrin_detach() and are not
1547 * shutting down, we can't know what the transfer status is as
1548 * we are only called when the last reference is lost.
1549 */
1550 result_str = (xfr->shuttingdown
1551 ? isc_result_totext(xfr->shutdown_result)
1552 : "unknown");
1553 xfrin_log(xfr, ISC_LOG_INFO, "Transfer status: %s", result_str);
1554
1555 /*
1556 * Calculate the length of time the transfer took,
1557 * and print a log message with the bytes and rate.
1558 */
1559 isc_time_now(&xfr->end);
1560 msecs = isc_time_microdiff(&xfr->end, &xfr->start) / 1000;
1561 if (msecs == 0) {
1562 msecs = 1;
1563 }
1564 persec = (xfr->nbytes * 1000) / msecs;
1565 xfrin_log(xfr, ISC_LOG_INFO,
1566 "Transfer completed: %d messages, %d records, "
1567 "%" PRIu64 " bytes, "
1568 "%u.%03u secs (%u bytes/sec) (serial %u)",
1569 xfr->nmsg, xfr->nrecs, xfr->nbytes,
1570 (unsigned int)(msecs / 1000), (unsigned int)(msecs % 1000),
1571 (unsigned int)persec, xfr->end_serial);
1572
1573 if (xfr->socket != NULL) {
1574 isc_socket_detach(&xfr->socket);
1575 }
1576
1577 if (xfr->timer != NULL) {
1578 isc_timer_detach(&xfr->timer);
1579 }
1580
1581 if (xfr->task != NULL) {
1582 isc_task_detach(&xfr->task);
1583 }
1584
1585 if (xfr->tsigkey != NULL) {
1586 dns_tsigkey_detach(&xfr->tsigkey);
1587 }
1588
1589 if (xfr->lasttsig != NULL) {
1590 isc_buffer_free(&xfr->lasttsig);
1591 }
1592
1593 dns_diff_clear(&xfr->diff);
1594
1595 if (xfr->ixfr.journal != NULL) {
1596 dns_journal_destroy(&xfr->ixfr.journal);
1597 }
1598
1599 if (xfr->axfr.add_private != NULL) {
1600 (void)dns_db_endload(xfr->db, &xfr->axfr);
1601 }
1602
1603 if (xfr->tcpmsg_valid) {
1604 dns_tcpmsg_invalidate(&xfr->tcpmsg);
1605 }
1606
1607 if (xfr->tsigctx != NULL) {
1608 dst_context_destroy(&xfr->tsigctx);
1609 }
1610
1611 if ((xfr->name.attributes & DNS_NAMEATTR_DYNAMIC) != 0) {
1612 dns_name_free(&xfr->name, xfr->mctx);
1613 }
1614
1615 if (xfr->ver != NULL) {
1616 dns_db_closeversion(xfr->db, &xfr->ver, false);
1617 }
1618
1619 if (xfr->db != NULL) {
1620 dns_db_detach(&xfr->db);
1621 }
1622
1623 if (xfr->zone != NULL) {
1624 if (!xfr->zone_had_db && xfr->shuttingdown &&
1625 xfr->shutdown_result == ISC_R_SUCCESS &&
1626 dns_zone_gettype(xfr->zone) == dns_zone_mirror)
1627 {
1628 dns_zone_log(xfr->zone, ISC_LOG_INFO,
1629 "mirror zone is now in use");
1630 }
1631 xfrin_log(xfr, ISC_LOG_DEBUG(99), "freeing transfer context");
1632 /*
1633 * xfr->zone must not be detached before xfrin_log() is called.
1634 */
1635 dns_zone_idetach(&xfr->zone);
1636 }
1637
1638 if (xfr->firstsoa_data != NULL) {
1639 isc_mem_free(xfr->mctx, xfr->firstsoa_data);
1640 }
1641
1642 isc_mem_putanddetach(&xfr->mctx, xfr, sizeof(*xfr));
1643 }
1644
1645 /*
1646 * Log incoming zone transfer messages in a format like
1647 * transfer of <zone> from <address>: <message>
1648 */
1649 static void
1650 xfrin_logv(int level, const char *zonetext, const isc_sockaddr_t *masteraddr,
1651 const char *fmt, va_list ap) {
1652 char mastertext[ISC_SOCKADDR_FORMATSIZE];
1653 char msgtext[2048];
1654
1655 isc_sockaddr_format(masteraddr, mastertext, sizeof(mastertext));
1656 vsnprintf(msgtext, sizeof(msgtext), fmt, ap);
1657
1658 isc_log_write(dns_lctx, DNS_LOGCATEGORY_XFER_IN, DNS_LOGMODULE_XFER_IN,
1659 level, "transfer of '%s' from %s: %s", zonetext,
1660 mastertext, msgtext);
1661 }
1662
1663 /*
1664 * Logging function for use when a xfrin_ctx_t has not yet been created.
1665 */
1666
1667 static void
1668 xfrin_log1(int level, const char *zonetext, const isc_sockaddr_t *masteraddr,
1669 const char *fmt, ...) {
1670 va_list ap;
1671
1672 if (!isc_log_wouldlog(dns_lctx, level)) {
1673 return;
1674 }
1675
1676 va_start(ap, fmt);
1677 xfrin_logv(level, zonetext, masteraddr, fmt, ap);
1678 va_end(ap);
1679 }
1680
1681 /*
1682 * Logging function for use when there is a xfrin_ctx_t.
1683 */
1684
1685 static void
1686 xfrin_log(dns_xfrin_ctx_t *xfr, int level, const char *fmt, ...) {
1687 va_list ap;
1688 char zonetext[DNS_NAME_MAXTEXT + 32];
1689
1690 if (!isc_log_wouldlog(dns_lctx, level)) {
1691 return;
1692 }
1693
1694 dns_zone_name(xfr->zone, zonetext, sizeof(zonetext));
1695
1696 va_start(ap, fmt);
1697 xfrin_logv(level, zonetext, &xfr->masteraddr, fmt, ap);
1698 va_end(ap);
1699 }
1700