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