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