Merge branch 'vendor/LESS' into less_update
[dragonfly.git] / contrib / bind-9.5.2 / bin / named / xfrout.c
1 /*
2  * Copyright (C) 2004-2009  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: xfrout.c,v 1.126.128.8 2009/01/29 22:41:44 jinmei Exp $ */
19
20 #include <config.h>
21
22 #include <isc/formatcheck.h>
23 #include <isc/mem.h>
24 #include <isc/timer.h>
25 #include <isc/print.h>
26 #include <isc/stats.h>
27 #include <isc/util.h>
28
29 #include <dns/db.h>
30 #include <dns/dbiterator.h>
31 #ifdef DLZ
32 #include <dns/dlz.h>
33 #endif
34 #include <dns/fixedname.h>
35 #include <dns/journal.h>
36 #include <dns/message.h>
37 #include <dns/peer.h>
38 #include <dns/rdataclass.h>
39 #include <dns/rdatalist.h>
40 #include <dns/rdataset.h>
41 #include <dns/rdatasetiter.h>
42 #include <dns/result.h>
43 #include <dns/soa.h>
44 #include <dns/stats.h>
45 #include <dns/timer.h>
46 #include <dns/tsig.h>
47 #include <dns/view.h>
48 #include <dns/zone.h>
49 #include <dns/zt.h>
50
51 #include <named/client.h>
52 #include <named/log.h>
53 #include <named/server.h>
54 #include <named/xfrout.h>
55
56 /*! \file
57  * \brief
58  * Outgoing AXFR and IXFR.
59  */
60
61 /*
62  * TODO:
63  *  - IXFR over UDP
64  */
65
66 #define XFROUT_COMMON_LOGARGS \
67         ns_g_lctx, DNS_LOGCATEGORY_XFER_OUT, NS_LOGMODULE_XFER_OUT
68
69 #define XFROUT_PROTOCOL_LOGARGS \
70         XFROUT_COMMON_LOGARGS, ISC_LOG_INFO
71
72 #define XFROUT_DEBUG_LOGARGS(n) \
73         XFROUT_COMMON_LOGARGS, ISC_LOG_DEBUG(n)
74
75 #define XFROUT_RR_LOGARGS \
76         XFROUT_COMMON_LOGARGS, XFROUT_RR_LOGLEVEL
77
78 #define XFROUT_RR_LOGLEVEL      ISC_LOG_DEBUG(8)
79
80 /*%
81  * Fail unconditionally and log as a client error.
82  * The test against ISC_R_SUCCESS is there to keep the Solaris compiler
83  * from complaining about "end-of-loop code not reached".
84  */
85 #define FAILC(code, msg) \
86         do {                                                    \
87                 result = (code);                                \
88                 ns_client_log(client, DNS_LOGCATEGORY_XFER_OUT, \
89                            NS_LOGMODULE_XFER_OUT, ISC_LOG_INFO, \
90                            "bad zone transfer request: %s (%s)", \
91                            msg, isc_result_totext(code));       \
92                 if (result != ISC_R_SUCCESS) goto failure;      \
93         } while (0)
94
95 #define FAILQ(code, msg, question, rdclass) \
96         do {                                                    \
97                 char _buf1[DNS_NAME_FORMATSIZE];                \
98                 char _buf2[DNS_RDATACLASS_FORMATSIZE];          \
99                 result = (code);                                \
100                 dns_name_format(question, _buf1, sizeof(_buf1));  \
101                 dns_rdataclass_format(rdclass, _buf2, sizeof(_buf2)); \
102                 ns_client_log(client, DNS_LOGCATEGORY_XFER_OUT, \
103                            NS_LOGMODULE_XFER_OUT, ISC_LOG_INFO, \
104                            "bad zone transfer request: '%s/%s': %s (%s)", \
105                            _buf1, _buf2, msg, isc_result_totext(code)); \
106                 if (result != ISC_R_SUCCESS) goto failure;      \
107         } while (0)
108
109 #define CHECK(op) \
110         do { result = (op);                                     \
111                 if (result != ISC_R_SUCCESS) goto failure;      \
112         } while (0)
113
114 /**************************************************************************/
115 /*%
116  * A db_rr_iterator_t is an iterator that iterates over an entire database,
117  * returning one RR at a time, in some arbitrary order.
118  */
119
120 typedef struct db_rr_iterator db_rr_iterator_t;
121
122 /*% db_rr_iterator structure */
123 struct db_rr_iterator {
124         isc_result_t            result;
125         dns_db_t                *db;
126         dns_dbiterator_t        *dbit;
127         dns_dbversion_t         *ver;
128         isc_stdtime_t           now;
129         dns_dbnode_t            *node;
130         dns_fixedname_t         fixedname;
131         dns_rdatasetiter_t      *rdatasetit;
132         dns_rdataset_t          rdataset;
133         dns_rdata_t             rdata;
134 };
135
136 static isc_result_t
137 db_rr_iterator_init(db_rr_iterator_t *it, dns_db_t *db, dns_dbversion_t *ver,
138                     isc_stdtime_t now);
139
140 static isc_result_t
141 db_rr_iterator_first(db_rr_iterator_t *it);
142
143 static isc_result_t
144 db_rr_iterator_next(db_rr_iterator_t *it);
145
146 static void
147 db_rr_iterator_current(db_rr_iterator_t *it, dns_name_t **name,
148                        isc_uint32_t *ttl, dns_rdata_t **rdata);
149
150 static void
151 db_rr_iterator_destroy(db_rr_iterator_t *it);
152
153 static inline void
154 inc_stats(dns_zone_t *zone, isc_statscounter_t counter) {
155         isc_stats_increment(ns_g_server->nsstats, counter);
156         if (zone != NULL) {
157                 isc_stats_t *zonestats = dns_zone_getrequeststats(zone);
158                 if (zonestats != NULL)
159                         isc_stats_increment(zonestats, counter);
160         }
161 }
162
163 static isc_result_t
164 db_rr_iterator_init(db_rr_iterator_t *it, dns_db_t *db, dns_dbversion_t *ver,
165                     isc_stdtime_t now)
166 {
167         isc_result_t result;
168         it->db = db;
169         it->dbit = NULL;
170         it->ver = ver;
171         it->now = now;
172         it->node = NULL;
173         result = dns_db_createiterator(it->db, ISC_FALSE, &it->dbit);
174         if (result != ISC_R_SUCCESS)
175                 return (result);
176         it->rdatasetit = NULL;
177         dns_rdata_init(&it->rdata);
178         dns_rdataset_init(&it->rdataset);
179         dns_fixedname_init(&it->fixedname);
180         INSIST(! dns_rdataset_isassociated(&it->rdataset));
181         it->result = ISC_R_SUCCESS;
182         return (it->result);
183 }
184
185 static isc_result_t
186 db_rr_iterator_first(db_rr_iterator_t *it) {
187         it->result = dns_dbiterator_first(it->dbit);
188         /*
189          * The top node may be empty when out of zone glue exists.
190          * Walk the tree to find the first node with data.
191          */
192         while (it->result == ISC_R_SUCCESS) {
193                 it->result = dns_dbiterator_current(it->dbit, &it->node,
194                                     dns_fixedname_name(&it->fixedname));
195                 if (it->result != ISC_R_SUCCESS)
196                         return (it->result);
197
198                 it->result = dns_db_allrdatasets(it->db, it->node,
199                                                  it->ver, it->now,
200                                                  &it->rdatasetit);
201                 if (it->result != ISC_R_SUCCESS)
202                         return (it->result);
203
204                 it->result = dns_rdatasetiter_first(it->rdatasetit);
205                 if (it->result != ISC_R_SUCCESS) {
206                         /*
207                          * This node is empty. Try next node.
208                          */
209                         dns_rdatasetiter_destroy(&it->rdatasetit);
210                         dns_db_detachnode(it->db, &it->node);
211                         it->result = dns_dbiterator_next(it->dbit);
212                         continue;
213                 }
214                 dns_rdatasetiter_current(it->rdatasetit, &it->rdataset);
215                 it->rdataset.attributes |= DNS_RDATASETATTR_LOADORDER;
216                 it->result = dns_rdataset_first(&it->rdataset);
217                 return (it->result);
218         }
219         return (it->result);
220 }
221
222
223 static isc_result_t
224 db_rr_iterator_next(db_rr_iterator_t *it) {
225         if (it->result != ISC_R_SUCCESS)
226                 return (it->result);
227
228         INSIST(it->dbit != NULL);
229         INSIST(it->node != NULL);
230         INSIST(it->rdatasetit != NULL);
231
232         it->result = dns_rdataset_next(&it->rdataset);
233         if (it->result == ISC_R_NOMORE) {
234                 dns_rdataset_disassociate(&it->rdataset);
235                 it->result = dns_rdatasetiter_next(it->rdatasetit);
236                 /*
237                  * The while loop body is executed more than once
238                  * only when an empty dbnode needs to be skipped.
239                  */
240                 while (it->result == ISC_R_NOMORE) {
241                         dns_rdatasetiter_destroy(&it->rdatasetit);
242                         dns_db_detachnode(it->db, &it->node);
243                         it->result = dns_dbiterator_next(it->dbit);
244                         if (it->result == ISC_R_NOMORE) {
245                                 /* We are at the end of the entire database. */
246                                 return (it->result);
247                         }
248                         if (it->result != ISC_R_SUCCESS)
249                                 return (it->result);
250                         it->result = dns_dbiterator_current(it->dbit,
251                                     &it->node,
252                                     dns_fixedname_name(&it->fixedname));
253                         if (it->result != ISC_R_SUCCESS)
254                                 return (it->result);
255                         it->result = dns_db_allrdatasets(it->db, it->node,
256                                          it->ver, it->now,
257                                          &it->rdatasetit);
258                         if (it->result != ISC_R_SUCCESS)
259                                 return (it->result);
260                         it->result = dns_rdatasetiter_first(it->rdatasetit);
261                 }
262                 if (it->result != ISC_R_SUCCESS)
263                         return (it->result);
264                 dns_rdatasetiter_current(it->rdatasetit, &it->rdataset);
265                 it->rdataset.attributes |= DNS_RDATASETATTR_LOADORDER;
266                 it->result = dns_rdataset_first(&it->rdataset);
267                 if (it->result != ISC_R_SUCCESS)
268                         return (it->result);
269         }
270         return (it->result);
271 }
272
273 static void
274 db_rr_iterator_pause(db_rr_iterator_t *it) {
275         RUNTIME_CHECK(dns_dbiterator_pause(it->dbit) == ISC_R_SUCCESS);
276 }
277
278 static void
279 db_rr_iterator_destroy(db_rr_iterator_t *it) {
280         if (dns_rdataset_isassociated(&it->rdataset))
281                 dns_rdataset_disassociate(&it->rdataset);
282         if (it->rdatasetit != NULL)
283                 dns_rdatasetiter_destroy(&it->rdatasetit);
284         if (it->node != NULL)
285                 dns_db_detachnode(it->db, &it->node);
286         dns_dbiterator_destroy(&it->dbit);
287 }
288
289 static void
290 db_rr_iterator_current(db_rr_iterator_t *it, dns_name_t **name,
291                       isc_uint32_t *ttl, dns_rdata_t **rdata)
292 {
293         REQUIRE(name != NULL && *name == NULL);
294         REQUIRE(it->result == ISC_R_SUCCESS);
295         *name = dns_fixedname_name(&it->fixedname);
296         *ttl = it->rdataset.ttl;
297         dns_rdata_reset(&it->rdata);
298         dns_rdataset_current(&it->rdataset, &it->rdata);
299         *rdata = &it->rdata;
300 }
301
302 /**************************************************************************/
303
304 /*% Log an RR (for debugging) */
305
306 static void
307 log_rr(dns_name_t *name, dns_rdata_t *rdata, isc_uint32_t ttl) {
308         isc_result_t result;
309         isc_buffer_t buf;
310         char mem[2000];
311         dns_rdatalist_t rdl;
312         dns_rdataset_t rds;
313         dns_rdata_t rd = DNS_RDATA_INIT;
314
315         rdl.type = rdata->type;
316         rdl.rdclass = rdata->rdclass;
317         rdl.ttl = ttl;
318         if (rdata->type == dns_rdatatype_sig ||
319             rdata->type == dns_rdatatype_rrsig)
320                 rdl.covers = dns_rdata_covers(rdata);
321         else
322                 rdl.covers = dns_rdatatype_none;
323         ISC_LIST_INIT(rdl.rdata);
324         ISC_LINK_INIT(&rdl, link);
325         dns_rdataset_init(&rds);
326         dns_rdata_init(&rd);
327         dns_rdata_clone(rdata, &rd);
328         ISC_LIST_APPEND(rdl.rdata, &rd, link);
329         RUNTIME_CHECK(dns_rdatalist_tordataset(&rdl, &rds) == ISC_R_SUCCESS);
330
331         isc_buffer_init(&buf, mem, sizeof(mem));
332         result = dns_rdataset_totext(&rds, name,
333                                      ISC_FALSE, ISC_FALSE, &buf);
334
335         /*
336          * We could use xfrout_log(), but that would produce
337          * very long lines with a repetitive prefix.
338          */
339         if (result == ISC_R_SUCCESS) {
340                 /*
341                  * Get rid of final newline.
342                  */
343                 INSIST(buf.used >= 1 &&
344                        ((char *) buf.base)[buf.used - 1] == '\n');
345                 buf.used--;
346
347                 isc_log_write(XFROUT_RR_LOGARGS, "%.*s",
348                               (int)isc_buffer_usedlength(&buf),
349                               (char *)isc_buffer_base(&buf));
350         } else {
351                 isc_log_write(XFROUT_RR_LOGARGS, "<RR too large to print>");
352         }
353 }
354
355 /**************************************************************************/
356 /*
357  * An 'rrstream_t' is a polymorphic iterator that returns
358  * a stream of resource records.  There are multiple implementations,
359  * e.g. for generating AXFR and IXFR records streams.
360  */
361
362 typedef struct rrstream_methods rrstream_methods_t;
363
364 typedef struct rrstream {
365         isc_mem_t               *mctx;
366         rrstream_methods_t      *methods;
367 } rrstream_t;
368
369 struct rrstream_methods {
370         isc_result_t            (*first)(rrstream_t *);
371         isc_result_t            (*next)(rrstream_t *);
372         void                    (*current)(rrstream_t *,
373                                            dns_name_t **,
374                                            isc_uint32_t *,
375                                            dns_rdata_t **);
376         void                    (*pause)(rrstream_t *);
377         void                    (*destroy)(rrstream_t **);
378 };
379
380 static void
381 rrstream_noop_pause(rrstream_t *rs) {
382         UNUSED(rs);
383 }
384
385 /**************************************************************************/
386 /*
387  * An 'ixfr_rrstream_t' is an 'rrstream_t' that returns
388  * an IXFR-like RR stream from a journal file.
389  *
390  * The SOA at the beginning of each sequence of additions
391  * or deletions are included in the stream, but the extra
392  * SOAs at the beginning and end of the entire transfer are
393  * not included.
394  */
395
396 typedef struct ixfr_rrstream {
397         rrstream_t              common;
398         dns_journal_t           *journal;
399 } ixfr_rrstream_t;
400
401 /* Forward declarations. */
402 static void
403 ixfr_rrstream_destroy(rrstream_t **sp);
404
405 static rrstream_methods_t ixfr_rrstream_methods;
406
407 /*
408  * Returns: anything dns_journal_open() or dns_journal_iter_init()
409  * may return.
410  */
411
412 static isc_result_t
413 ixfr_rrstream_create(isc_mem_t *mctx,
414                      const char *journal_filename,
415                      isc_uint32_t begin_serial,
416                      isc_uint32_t end_serial,
417                      rrstream_t **sp)
418 {
419         ixfr_rrstream_t *s;
420         isc_result_t result;
421
422         INSIST(sp != NULL && *sp == NULL);
423
424         s = isc_mem_get(mctx, sizeof(*s));
425         if (s == NULL)
426                 return (ISC_R_NOMEMORY);
427         s->common.mctx = mctx;
428         s->common.methods = &ixfr_rrstream_methods;
429         s->journal = NULL;
430
431         CHECK(dns_journal_open(mctx, journal_filename,
432                                ISC_FALSE, &s->journal));
433         CHECK(dns_journal_iter_init(s->journal, begin_serial, end_serial));
434
435         *sp = (rrstream_t *) s;
436         return (ISC_R_SUCCESS);
437
438  failure:
439         ixfr_rrstream_destroy((rrstream_t **) (void *)&s);
440         return (result);
441 }
442
443 static isc_result_t
444 ixfr_rrstream_first(rrstream_t *rs) {
445         ixfr_rrstream_t *s = (ixfr_rrstream_t *) rs;
446         return (dns_journal_first_rr(s->journal));
447 }
448
449 static isc_result_t
450 ixfr_rrstream_next(rrstream_t *rs) {
451         ixfr_rrstream_t *s = (ixfr_rrstream_t *) rs;
452         return (dns_journal_next_rr(s->journal));
453 }
454
455 static void
456 ixfr_rrstream_current(rrstream_t *rs,
457                        dns_name_t **name, isc_uint32_t *ttl,
458                        dns_rdata_t **rdata)
459 {
460         ixfr_rrstream_t *s = (ixfr_rrstream_t *) rs;
461         dns_journal_current_rr(s->journal, name, ttl, rdata);
462 }
463
464 static void
465 ixfr_rrstream_destroy(rrstream_t **rsp) {
466         ixfr_rrstream_t *s = (ixfr_rrstream_t *) *rsp;
467         if (s->journal != 0)
468                 dns_journal_destroy(&s->journal);
469         isc_mem_put(s->common.mctx, s, sizeof(*s));
470 }
471
472 static rrstream_methods_t ixfr_rrstream_methods = {
473         ixfr_rrstream_first,
474         ixfr_rrstream_next,
475         ixfr_rrstream_current,
476         rrstream_noop_pause,
477         ixfr_rrstream_destroy
478 };
479
480 /**************************************************************************/
481 /*
482  * An 'axfr_rrstream_t' is an 'rrstream_t' that returns
483  * an AXFR-like RR stream from a database.
484  *
485  * The SOAs at the beginning and end of the transfer are
486  * not included in the stream.
487  */
488
489 typedef struct axfr_rrstream {
490         rrstream_t              common;
491         db_rr_iterator_t        it;
492         isc_boolean_t           it_valid;
493 } axfr_rrstream_t;
494
495 /*
496  * Forward declarations.
497  */
498 static void
499 axfr_rrstream_destroy(rrstream_t **rsp);
500
501 static rrstream_methods_t axfr_rrstream_methods;
502
503 static isc_result_t
504 axfr_rrstream_create(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *ver,
505                      rrstream_t **sp)
506 {
507         axfr_rrstream_t *s;
508         isc_result_t result;
509
510         INSIST(sp != NULL && *sp == NULL);
511
512         s = isc_mem_get(mctx, sizeof(*s));
513         if (s == NULL)
514                 return (ISC_R_NOMEMORY);
515         s->common.mctx = mctx;
516         s->common.methods = &axfr_rrstream_methods;
517         s->it_valid = ISC_FALSE;
518
519         CHECK(db_rr_iterator_init(&s->it, db, ver, 0));
520         s->it_valid = ISC_TRUE;
521
522         *sp = (rrstream_t *) s;
523         return (ISC_R_SUCCESS);
524
525  failure:
526         axfr_rrstream_destroy((rrstream_t **) (void *)&s);
527         return (result);
528 }
529
530 static isc_result_t
531 axfr_rrstream_first(rrstream_t *rs) {
532         axfr_rrstream_t *s = (axfr_rrstream_t *) rs;
533         isc_result_t result;
534         result = db_rr_iterator_first(&s->it);
535         if (result != ISC_R_SUCCESS)
536                 return (result);
537         /* Skip SOA records. */
538         for (;;) {
539                 dns_name_t *name_dummy = NULL;
540                 isc_uint32_t ttl_dummy;
541                 dns_rdata_t *rdata = NULL;
542                 db_rr_iterator_current(&s->it, &name_dummy,
543                                       &ttl_dummy, &rdata);
544                 if (rdata->type != dns_rdatatype_soa)
545                         break;
546                 result = db_rr_iterator_next(&s->it);
547                 if (result != ISC_R_SUCCESS)
548                         break;
549         }
550         return (result);
551 }
552
553 static isc_result_t
554 axfr_rrstream_next(rrstream_t *rs) {
555         axfr_rrstream_t *s = (axfr_rrstream_t *) rs;
556         isc_result_t result;
557
558         /* Skip SOA records. */
559         for (;;) {
560                 dns_name_t *name_dummy = NULL;
561                 isc_uint32_t ttl_dummy;
562                 dns_rdata_t *rdata = NULL;
563                 result = db_rr_iterator_next(&s->it);
564                 if (result != ISC_R_SUCCESS)
565                         break;
566                 db_rr_iterator_current(&s->it, &name_dummy,
567                                       &ttl_dummy, &rdata);
568                 if (rdata->type != dns_rdatatype_soa)
569                         break;
570         }
571         return (result);
572 }
573
574 static void
575 axfr_rrstream_current(rrstream_t *rs, dns_name_t **name, isc_uint32_t *ttl,
576                       dns_rdata_t **rdata)
577 {
578         axfr_rrstream_t *s = (axfr_rrstream_t *) rs;
579         db_rr_iterator_current(&s->it, name, ttl, rdata);
580 }
581
582 static void
583 axfr_rrstream_pause(rrstream_t *rs) {
584         axfr_rrstream_t *s = (axfr_rrstream_t *) rs;
585         db_rr_iterator_pause(&s->it);
586 }
587
588 static void
589 axfr_rrstream_destroy(rrstream_t **rsp) {
590         axfr_rrstream_t *s = (axfr_rrstream_t *) *rsp;
591         if (s->it_valid)
592                 db_rr_iterator_destroy(&s->it);
593         isc_mem_put(s->common.mctx, s, sizeof(*s));
594 }
595
596 static rrstream_methods_t axfr_rrstream_methods = {
597         axfr_rrstream_first,
598         axfr_rrstream_next,
599         axfr_rrstream_current,
600         axfr_rrstream_pause,
601         axfr_rrstream_destroy
602 };
603
604 /**************************************************************************/
605 /*
606  * An 'soa_rrstream_t' is a degenerate 'rrstream_t' that returns
607  * a single SOA record.
608  */
609
610 typedef struct soa_rrstream {
611         rrstream_t              common;
612         dns_difftuple_t         *soa_tuple;
613 } soa_rrstream_t;
614
615 /*
616  * Forward declarations.
617  */
618 static void
619 soa_rrstream_destroy(rrstream_t **rsp);
620
621 static rrstream_methods_t soa_rrstream_methods;
622
623 static isc_result_t
624 soa_rrstream_create(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *ver,
625                     rrstream_t **sp)
626 {
627         soa_rrstream_t *s;
628         isc_result_t result;
629
630         INSIST(sp != NULL && *sp == NULL);
631
632         s = isc_mem_get(mctx, sizeof(*s));
633         if (s == NULL)
634                 return (ISC_R_NOMEMORY);
635         s->common.mctx = mctx;
636         s->common.methods = &soa_rrstream_methods;
637         s->soa_tuple = NULL;
638
639         CHECK(dns_db_createsoatuple(db, ver, mctx, DNS_DIFFOP_EXISTS,
640                                     &s->soa_tuple));
641
642         *sp = (rrstream_t *) s;
643         return (ISC_R_SUCCESS);
644
645  failure:
646         soa_rrstream_destroy((rrstream_t **) (void *)&s);
647         return (result);
648 }
649
650 static isc_result_t
651 soa_rrstream_first(rrstream_t *rs) {
652         UNUSED(rs);
653         return (ISC_R_SUCCESS);
654 }
655
656 static isc_result_t
657 soa_rrstream_next(rrstream_t *rs) {
658         UNUSED(rs);
659         return (ISC_R_NOMORE);
660 }
661
662 static void
663 soa_rrstream_current(rrstream_t *rs, dns_name_t **name, isc_uint32_t *ttl,
664                      dns_rdata_t **rdata)
665 {
666         soa_rrstream_t *s = (soa_rrstream_t *) rs;
667         *name = &s->soa_tuple->name;
668         *ttl = s->soa_tuple->ttl;
669         *rdata = &s->soa_tuple->rdata;
670 }
671
672 static void
673 soa_rrstream_destroy(rrstream_t **rsp) {
674         soa_rrstream_t *s = (soa_rrstream_t *) *rsp;
675         if (s->soa_tuple != NULL)
676                 dns_difftuple_free(&s->soa_tuple);
677         isc_mem_put(s->common.mctx, s, sizeof(*s));
678 }
679
680 static rrstream_methods_t soa_rrstream_methods = {
681         soa_rrstream_first,
682         soa_rrstream_next,
683         soa_rrstream_current,
684         rrstream_noop_pause,
685         soa_rrstream_destroy
686 };
687
688 /**************************************************************************/
689 /*
690  * A 'compound_rrstream_t' objects owns a soa_rrstream
691  * and another rrstream, the "data stream".  It returns
692  * a concatenated stream consisting of the soa_rrstream, then
693  * the data stream, then the soa_rrstream again.
694  *
695  * The component streams are owned by the compound_rrstream_t
696  * and are destroyed with it.
697  */
698
699 typedef struct compound_rrstream {
700         rrstream_t              common;
701         rrstream_t              *components[3];
702         int                     state;
703         isc_result_t            result;
704 } compound_rrstream_t;
705
706 /*
707  * Forward declarations.
708  */
709 static void
710 compound_rrstream_destroy(rrstream_t **rsp);
711
712 static isc_result_t
713 compound_rrstream_next(rrstream_t *rs);
714
715 static rrstream_methods_t compound_rrstream_methods;
716
717 /*
718  * Requires:
719  *      soa_stream != NULL && *soa_stream != NULL
720  *      data_stream != NULL && *data_stream != NULL
721  *      sp != NULL && *sp == NULL
722  *
723  * Ensures:
724  *      *soa_stream == NULL
725  *      *data_stream == NULL
726  *      *sp points to a valid compound_rrstream_t
727  *      The soa and data streams will be destroyed
728  *      when the compound_rrstream_t is destroyed.
729  */
730 static isc_result_t
731 compound_rrstream_create(isc_mem_t *mctx, rrstream_t **soa_stream,
732                          rrstream_t **data_stream, rrstream_t **sp)
733 {
734         compound_rrstream_t *s;
735
736         INSIST(sp != NULL && *sp == NULL);
737
738         s = isc_mem_get(mctx, sizeof(*s));
739         if (s == NULL)
740                 return (ISC_R_NOMEMORY);
741         s->common.mctx = mctx;
742         s->common.methods = &compound_rrstream_methods;
743         s->components[0] = *soa_stream;
744         s->components[1] = *data_stream;
745         s->components[2] = *soa_stream;
746         s->state = -1;
747         s->result = ISC_R_FAILURE;
748
749         *soa_stream = NULL;
750         *data_stream = NULL;
751         *sp = (rrstream_t *) s;
752         return (ISC_R_SUCCESS);
753 }
754
755 static isc_result_t
756 compound_rrstream_first(rrstream_t *rs) {
757         compound_rrstream_t *s = (compound_rrstream_t *) rs;
758         s->state = 0;
759         do {
760                 rrstream_t *curstream = s->components[s->state];
761                 s->result = curstream->methods->first(curstream);
762         } while (s->result == ISC_R_NOMORE && s->state < 2);
763         return (s->result);
764 }
765
766 static isc_result_t
767 compound_rrstream_next(rrstream_t *rs) {
768         compound_rrstream_t *s = (compound_rrstream_t *) rs;
769         rrstream_t *curstream = s->components[s->state];
770         s->result = curstream->methods->next(curstream);
771         while (s->result == ISC_R_NOMORE) {
772                 /*
773                  * Make sure locks held by the current stream
774                  * are released before we switch streams.
775                  */
776                 curstream->methods->pause(curstream);
777                 if (s->state == 2)
778                         return (ISC_R_NOMORE);
779                 s->state++;
780                 curstream = s->components[s->state];
781                 s->result = curstream->methods->first(curstream);
782         }
783         return (s->result);
784 }
785
786 static void
787 compound_rrstream_current(rrstream_t *rs, dns_name_t **name, isc_uint32_t *ttl,
788                           dns_rdata_t **rdata)
789 {
790         compound_rrstream_t *s = (compound_rrstream_t *) rs;
791         rrstream_t *curstream;
792         INSIST(0 <= s->state && s->state < 3);
793         INSIST(s->result == ISC_R_SUCCESS);
794         curstream = s->components[s->state];
795         curstream->methods->current(curstream, name, ttl, rdata);
796 }
797
798 static void
799 compound_rrstream_pause(rrstream_t *rs)
800 {
801         compound_rrstream_t *s = (compound_rrstream_t *) rs;
802         rrstream_t *curstream;
803         INSIST(0 <= s->state && s->state < 3);
804         curstream = s->components[s->state];
805         curstream->methods->pause(curstream);
806 }
807
808 static void
809 compound_rrstream_destroy(rrstream_t **rsp) {
810         compound_rrstream_t *s = (compound_rrstream_t *) *rsp;
811         s->components[0]->methods->destroy(&s->components[0]);
812         s->components[1]->methods->destroy(&s->components[1]);
813         s->components[2] = NULL; /* Copy of components[0]. */
814         isc_mem_put(s->common.mctx, s, sizeof(*s));
815 }
816
817 static rrstream_methods_t compound_rrstream_methods = {
818         compound_rrstream_first,
819         compound_rrstream_next,
820         compound_rrstream_current,
821         compound_rrstream_pause,
822         compound_rrstream_destroy
823 };
824
825 /**************************************************************************/
826 /*
827  * An 'xfrout_ctx_t' contains the state of an outgoing AXFR or IXFR
828  * in progress.
829  */
830
831 typedef struct {
832         isc_mem_t               *mctx;
833         ns_client_t             *client;
834         unsigned int            id;             /* ID of request */
835         dns_name_t              *qname;         /* Question name of request */
836         dns_rdatatype_t         qtype;          /* dns_rdatatype_{a,i}xfr */
837         dns_rdataclass_t        qclass;
838         dns_zone_t              *zone;          /* (necessary for stats) */
839         dns_db_t                *db;
840         dns_dbversion_t         *ver;
841         isc_quota_t             *quota;
842         rrstream_t              *stream;        /* The XFR RR stream */
843         isc_boolean_t           end_of_stream;  /* EOS has been reached */
844         isc_buffer_t            buf;            /* Buffer for message owner
845                                                    names and rdatas */
846         isc_buffer_t            txlenbuf;       /* Transmit length buffer */
847         isc_buffer_t            txbuf;          /* Transmit message buffer */
848         void                    *txmem;
849         unsigned int            txmemlen;
850         unsigned int            nmsg;           /* Number of messages sent */
851         dns_tsigkey_t           *tsigkey;       /* Key used to create TSIG */
852         isc_buffer_t            *lasttsig;      /* the last TSIG */
853         isc_boolean_t           many_answers;
854         int                     sends;          /* Send in progress */
855         isc_boolean_t           shuttingdown;
856         const char              *mnemonic;      /* Style of transfer */
857 } xfrout_ctx_t;
858
859 static isc_result_t
860 xfrout_ctx_create(isc_mem_t *mctx, ns_client_t *client,
861                   unsigned int id, dns_name_t *qname, dns_rdatatype_t qtype,
862                   dns_rdataclass_t qclass, dns_zone_t *zone,
863                   dns_db_t *db, dns_dbversion_t *ver, isc_quota_t *quota,
864                   rrstream_t *stream, dns_tsigkey_t *tsigkey,
865                   isc_buffer_t *lasttsig,
866                   unsigned int maxtime,
867                   unsigned int idletime,
868                   isc_boolean_t many_answers,
869                   xfrout_ctx_t **xfrp);
870
871 static void
872 sendstream(xfrout_ctx_t *xfr);
873
874 static void
875 xfrout_senddone(isc_task_t *task, isc_event_t *event);
876
877 static void
878 xfrout_fail(xfrout_ctx_t *xfr, isc_result_t result, const char *msg);
879
880 static void
881 xfrout_maybe_destroy(xfrout_ctx_t *xfr);
882
883 static void
884 xfrout_ctx_destroy(xfrout_ctx_t **xfrp);
885
886 static void
887 xfrout_client_shutdown(void *arg, isc_result_t result);
888
889 static void
890 xfrout_log1(ns_client_t *client, dns_name_t *zonename,
891             dns_rdataclass_t rdclass, int level,
892             const char *fmt, ...) ISC_FORMAT_PRINTF(5, 6);
893
894 static void
895 xfrout_log(xfrout_ctx_t *xfr, int level, const char *fmt, ...)
896            ISC_FORMAT_PRINTF(3, 4);
897
898 /**************************************************************************/
899
900 void
901 ns_xfr_start(ns_client_t *client, dns_rdatatype_t reqtype) {
902         isc_result_t result;
903         dns_name_t *question_name;
904         dns_rdataset_t *question_rdataset;
905         dns_zone_t *zone = NULL;
906         dns_db_t *db = NULL;
907         dns_dbversion_t *ver = NULL;
908         dns_rdataclass_t question_class;
909         rrstream_t *soa_stream = NULL;
910         rrstream_t *data_stream = NULL;
911         rrstream_t *stream = NULL;
912         dns_difftuple_t *current_soa_tuple = NULL;
913         dns_name_t *soa_name;
914         dns_rdataset_t *soa_rdataset;
915         dns_rdata_t soa_rdata = DNS_RDATA_INIT;
916         isc_boolean_t have_soa = ISC_FALSE;
917         const char *mnemonic = NULL;
918         isc_mem_t *mctx = client->mctx;
919         dns_message_t *request = client->message;
920         xfrout_ctx_t *xfr = NULL;
921         isc_quota_t *quota = NULL;
922         dns_transfer_format_t format = client->view->transfer_format;
923         isc_netaddr_t na;
924         dns_peer_t *peer = NULL;
925         isc_buffer_t *tsigbuf = NULL;
926         char *journalfile;
927         char msg[NS_CLIENT_ACLMSGSIZE("zone transfer")];
928         char keyname[DNS_NAME_FORMATSIZE];
929         isc_boolean_t is_poll = ISC_FALSE;
930 #ifdef DLZ
931         isc_boolean_t is_dlz = ISC_FALSE;
932 #endif
933
934         switch (reqtype) {
935         case dns_rdatatype_axfr:
936                 mnemonic = "AXFR";
937                 break;
938         case dns_rdatatype_ixfr:
939                 mnemonic = "IXFR";
940                 break;
941         default:
942                 INSIST(0);
943                 break;
944         }
945
946         ns_client_log(client,
947                       DNS_LOGCATEGORY_XFER_OUT, NS_LOGMODULE_XFER_OUT,
948                       ISC_LOG_DEBUG(6), "%s request", mnemonic);
949         /*
950          * Apply quota.
951          */
952         result = isc_quota_attach(&ns_g_server->xfroutquota, &quota);
953         if (result != ISC_R_SUCCESS) {
954                 isc_log_write(XFROUT_COMMON_LOGARGS, ISC_LOG_WARNING,
955                               "%s request denied: %s", mnemonic,
956                               isc_result_totext(result));
957                 goto failure;
958         }
959
960         /*
961          * Interpret the question section.
962          */
963         result = dns_message_firstname(request, DNS_SECTION_QUESTION);
964         INSIST(result == ISC_R_SUCCESS);
965
966         /*
967          * The question section must contain exactly one question, and
968          * it must be for AXFR/IXFR as appropriate.
969          */
970         question_name = NULL;
971         dns_message_currentname(request, DNS_SECTION_QUESTION, &question_name);
972         question_rdataset = ISC_LIST_HEAD(question_name->list);
973         question_class = question_rdataset->rdclass;
974         INSIST(question_rdataset->type == reqtype);
975         if (ISC_LIST_NEXT(question_rdataset, link) != NULL)
976                 FAILC(DNS_R_FORMERR, "multiple questions");
977         result = dns_message_nextname(request, DNS_SECTION_QUESTION);
978         if (result != ISC_R_NOMORE)
979                 FAILC(DNS_R_FORMERR, "multiple questions");
980
981         result = dns_zt_find(client->view->zonetable, question_name, 0, NULL,
982                              &zone);
983
984         if (result != ISC_R_SUCCESS)
985 #ifdef DLZ
986         {
987                 /*
988                  * Normal zone table does not have a match.  Try the DLZ database
989                  */
990                 if (client->view->dlzdatabase != NULL) {
991                         result = dns_dlzallowzonexfr(client->view,
992                                                      question_name, &client->peeraddr,
993                                                      &db);
994
995                         if (result == ISC_R_NOPERM) {
996                                 char _buf1[DNS_NAME_FORMATSIZE];
997                                 char _buf2[DNS_RDATACLASS_FORMATSIZE];
998
999                                 result = DNS_R_REFUSED;
1000                                 dns_name_format(question_name, _buf1,
1001                                                 sizeof(_buf1));
1002                                 dns_rdataclass_format(question_class,
1003                                                       _buf2, sizeof(_buf2));
1004                                 ns_client_log(client, DNS_LOGCATEGORY_SECURITY,
1005                                               NS_LOGMODULE_XFER_OUT,
1006                                               ISC_LOG_ERROR,
1007                                               "zone transfer '%s/%s' denied",
1008                                               _buf1, _buf2);
1009                                 goto failure;
1010                         }
1011                         if (result != ISC_R_SUCCESS)
1012 #endif
1013                         FAILQ(DNS_R_NOTAUTH, "non-authoritative zone",
1014                                   question_name, question_class);
1015 #ifdef DLZ
1016                         is_dlz = ISC_TRUE;
1017                         /*
1018                          * DLZ only support full zone transfer, not incremental
1019                          */
1020                         if (reqtype != dns_rdatatype_axfr) {
1021                                 mnemonic = "AXFR-style IXFR";
1022                                 reqtype = dns_rdatatype_axfr;
1023                         }
1024
1025                 } else {
1026                         /*
1027                          * not DLZ and not in normal zone table, we are
1028                          * not authoritative
1029                          */
1030                         FAILQ(DNS_R_NOTAUTH, "non-authoritative zone",
1031                               question_name, question_class);
1032                 }
1033         } else {
1034                 /* zone table has a match */
1035 #endif
1036                 switch(dns_zone_gettype(zone)) {
1037                         case dns_zone_master:
1038                         case dns_zone_slave:
1039                                 break;  /* Master and slave zones are OK for transfer. */
1040                         default:
1041                                 FAILQ(DNS_R_NOTAUTH, "non-authoritative zone", question_name, question_class);
1042                         }
1043                 CHECK(dns_zone_getdb(zone, &db));
1044                 dns_db_currentversion(db, &ver);
1045 #ifdef DLZ
1046         }
1047 #endif
1048
1049         xfrout_log1(client, question_name, question_class, ISC_LOG_DEBUG(6),
1050                     "%s question section OK", mnemonic);
1051
1052         /*
1053          * Check the authority section.  Look for a SOA record with
1054          * the same name and class as the question.
1055          */
1056         for (result = dns_message_firstname(request, DNS_SECTION_AUTHORITY);
1057              result == ISC_R_SUCCESS;
1058              result = dns_message_nextname(request, DNS_SECTION_AUTHORITY))
1059         {
1060                 soa_name = NULL;
1061                 dns_message_currentname(request, DNS_SECTION_AUTHORITY,
1062                                         &soa_name);
1063
1064                 /*
1065                  * Ignore data whose owner name is not the zone apex.
1066                  */
1067                 if (! dns_name_equal(soa_name, question_name))
1068                         continue;
1069
1070                 for (soa_rdataset = ISC_LIST_HEAD(soa_name->list);
1071                      soa_rdataset != NULL;
1072                      soa_rdataset = ISC_LIST_NEXT(soa_rdataset, link))
1073                 {
1074                         /*
1075                          * Ignore non-SOA data.
1076                          */
1077                         if (soa_rdataset->type != dns_rdatatype_soa)
1078                                 continue;
1079                         if (soa_rdataset->rdclass != question_class)
1080                                 continue;
1081
1082                         CHECK(dns_rdataset_first(soa_rdataset));
1083                         dns_rdataset_current(soa_rdataset, &soa_rdata);
1084                         result = dns_rdataset_next(soa_rdataset);
1085                         if (result == ISC_R_SUCCESS)
1086                                 FAILC(DNS_R_FORMERR,
1087                                       "IXFR authority section "
1088                                       "has multiple SOAs");
1089                         have_soa = ISC_TRUE;
1090                         goto got_soa;
1091                 }
1092         }
1093  got_soa:
1094         if (result != ISC_R_NOMORE)
1095                 CHECK(result);
1096
1097         xfrout_log1(client, question_name, question_class, ISC_LOG_DEBUG(6),
1098                     "%s authority section OK", mnemonic);
1099
1100         /*
1101          * Decide whether to allow this transfer.
1102          */
1103 #ifdef DLZ
1104         /*
1105          * if not a DLZ zone decide whether to allow this transfer.
1106          */
1107         if (!is_dlz) {
1108 #endif
1109                 ns_client_aclmsg("zone transfer", question_name, reqtype,
1110                                  client->view->rdclass, msg, sizeof(msg));
1111                 CHECK(ns_client_checkacl(client, NULL, msg,
1112                                          dns_zone_getxfracl(zone),
1113                                          ISC_TRUE, ISC_LOG_ERROR));
1114 #ifdef DLZ
1115         }
1116 #endif
1117
1118         /*
1119          * AXFR over UDP is not possible.
1120          */
1121         if (reqtype == dns_rdatatype_axfr &&
1122             (client->attributes & NS_CLIENTATTR_TCP) == 0)
1123                 FAILC(DNS_R_FORMERR, "attempted AXFR over UDP");
1124
1125         /*
1126          * Look up the requesting server in the peer table.
1127          */
1128         isc_netaddr_fromsockaddr(&na, &client->peeraddr);
1129         (void)dns_peerlist_peerbyaddr(client->view->peers, &na, &peer);
1130
1131         /*
1132          * Decide on the transfer format (one-answer or many-answers).
1133          */
1134         if (peer != NULL)
1135                 (void)dns_peer_gettransferformat(peer, &format);
1136
1137         /*
1138          * Get a dynamically allocated copy of the current SOA.
1139          */
1140 #ifdef DLZ
1141         if (is_dlz)
1142                 dns_db_currentversion(db, &ver);
1143 #endif
1144         CHECK(dns_db_createsoatuple(db, ver, mctx, DNS_DIFFOP_EXISTS,
1145                                     &current_soa_tuple));
1146
1147         if (reqtype == dns_rdatatype_ixfr) {
1148                 isc_uint32_t begin_serial, current_serial;
1149                 isc_boolean_t provide_ixfr;
1150
1151                 /*
1152                  * Outgoing IXFR may have been disabled for this peer
1153                  * or globally.
1154                  */
1155                 provide_ixfr = client->view->provideixfr;
1156                 if (peer != NULL)
1157                         (void) dns_peer_getprovideixfr(peer, &provide_ixfr);
1158                 if (provide_ixfr == ISC_FALSE)
1159                         goto axfr_fallback;
1160
1161                 if (! have_soa)
1162                         FAILC(DNS_R_FORMERR,
1163                               "IXFR request missing SOA");
1164
1165                 begin_serial = dns_soa_getserial(&soa_rdata);
1166                 current_serial = dns_soa_getserial(&current_soa_tuple->rdata);
1167
1168                 /*
1169                  * RFC1995 says "If an IXFR query with the same or
1170                  * newer version number than that of the server
1171                  * is received, it is replied to with a single SOA
1172                  * record of the server's current version, just as
1173                  * in AXFR".  The claim about AXFR is incorrect,
1174                  * but other than that, we do as the RFC says.
1175                  *
1176                  * Sending a single SOA record is also how we refuse
1177                  * IXFR over UDP (currently, we always do).
1178                  */
1179                 if (DNS_SERIAL_GE(begin_serial, current_serial) ||
1180                     (client->attributes & NS_CLIENTATTR_TCP) == 0)
1181                 {
1182                         CHECK(soa_rrstream_create(mctx, db, ver, &stream));
1183                         is_poll = ISC_TRUE;
1184                         goto have_stream;
1185                 }
1186                 journalfile = dns_zone_getjournal(zone);
1187                 if (journalfile != NULL)
1188                         result = ixfr_rrstream_create(mctx,
1189                                                       journalfile,
1190                                                       begin_serial,
1191                                                       current_serial,
1192                                                       &data_stream);
1193                 else
1194                         result = ISC_R_NOTFOUND;
1195                 if (result == ISC_R_NOTFOUND ||
1196                     result == ISC_R_RANGE) {
1197                         xfrout_log1(client, question_name, question_class,
1198                                     ISC_LOG_DEBUG(4),
1199                                     "IXFR version not in journal, "
1200                                     "falling back to AXFR");
1201                         mnemonic = "AXFR-style IXFR";
1202                         goto axfr_fallback;
1203                 }
1204                 CHECK(result);
1205         } else {
1206         axfr_fallback:
1207                 CHECK(axfr_rrstream_create(mctx, db, ver,
1208                                            &data_stream));
1209         }
1210
1211         /*
1212          * Bracket the data stream with SOAs.
1213          */
1214         CHECK(soa_rrstream_create(mctx, db, ver, &soa_stream));
1215         CHECK(compound_rrstream_create(mctx, &soa_stream, &data_stream,
1216                                        &stream));
1217         soa_stream = NULL;
1218         data_stream = NULL;
1219
1220  have_stream:
1221         CHECK(dns_message_getquerytsig(request, mctx, &tsigbuf));
1222         /*
1223          * Create the xfrout context object.  This transfers the ownership
1224          * of "stream", "db", "ver", and "quota" to the xfrout context object.
1225          */
1226
1227
1228
1229 #ifdef DLZ
1230         if (is_dlz)
1231                 CHECK(xfrout_ctx_create(mctx, client, request->id, question_name,
1232                                         reqtype, question_class, zone, db, ver,
1233                                         quota, stream,
1234                                         dns_message_gettsigkey(request),
1235                                         tsigbuf,
1236                                         3600,
1237                                         3600,
1238                                         (format == dns_many_answers) ?
1239                                         ISC_TRUE : ISC_FALSE,
1240                                         &xfr));
1241         else
1242 #endif
1243                 CHECK(xfrout_ctx_create(mctx, client, request->id, question_name,
1244                                         reqtype, question_class, zone, db, ver,
1245                                         quota, stream,
1246                                         dns_message_gettsigkey(request),
1247                                         tsigbuf,
1248                                         dns_zone_getmaxxfrout(zone),
1249                                         dns_zone_getidleout(zone),
1250                                         (format == dns_many_answers) ?
1251                                         ISC_TRUE : ISC_FALSE,
1252                                         &xfr));
1253
1254         xfr->mnemonic = mnemonic;
1255         stream = NULL;
1256         quota = NULL;
1257
1258         CHECK(xfr->stream->methods->first(xfr->stream));
1259
1260         if (xfr->tsigkey != NULL) {
1261                 dns_name_format(&xfr->tsigkey->name, keyname, sizeof(keyname));
1262         } else
1263                 keyname[0] = '\0';
1264         if (is_poll)
1265                 xfrout_log1(client, question_name, question_class,
1266                             ISC_LOG_DEBUG(1), "IXFR poll up to date%s%s",
1267                             (xfr->tsigkey != NULL) ? ": TSIG " : "", keyname);
1268         else
1269                 xfrout_log1(client, question_name, question_class,
1270                             ISC_LOG_INFO, "%s started%s%s", mnemonic,
1271                             (xfr->tsigkey != NULL) ? ": TSIG " : "", keyname);
1272
1273         /*
1274          * Hand the context over to sendstream().  Set xfr to NULL;
1275          * sendstream() is responsible for either passing the
1276          * context on to a later event handler or destroying it.
1277          */
1278         sendstream(xfr);
1279         xfr = NULL;
1280
1281         result = ISC_R_SUCCESS;
1282
1283  failure:
1284         if (result == DNS_R_REFUSED)
1285                 inc_stats(zone, dns_nsstatscounter_xfrrej);
1286         if (quota != NULL)
1287                 isc_quota_detach(&quota);
1288         if (current_soa_tuple != NULL)
1289                 dns_difftuple_free(&current_soa_tuple);
1290         if (stream != NULL)
1291                 stream->methods->destroy(&stream);
1292         if (soa_stream != NULL)
1293                 soa_stream->methods->destroy(&soa_stream);
1294         if (data_stream != NULL)
1295                 data_stream->methods->destroy(&data_stream);
1296         if (ver != NULL)
1297                 dns_db_closeversion(db, &ver, ISC_FALSE);
1298         if (db != NULL)
1299                 dns_db_detach(&db);
1300         if (zone != NULL)
1301                 dns_zone_detach(&zone);
1302         /* XXX kludge */
1303         if (xfr != NULL) {
1304                 xfrout_fail(xfr, result, "setting up zone transfer");
1305         } else if (result != ISC_R_SUCCESS) {
1306                 ns_client_log(client, DNS_LOGCATEGORY_XFER_OUT,
1307                               NS_LOGMODULE_XFER_OUT,
1308                               ISC_LOG_DEBUG(3), "zone transfer setup failed");
1309                 ns_client_error(client, result);
1310         }
1311 }
1312
1313 static isc_result_t
1314 xfrout_ctx_create(isc_mem_t *mctx, ns_client_t *client, unsigned int id,
1315                   dns_name_t *qname, dns_rdatatype_t qtype,
1316                   dns_rdataclass_t qclass, dns_zone_t *zone,
1317                   dns_db_t *db, dns_dbversion_t *ver, isc_quota_t *quota,
1318                   rrstream_t *stream, dns_tsigkey_t *tsigkey,
1319                   isc_buffer_t *lasttsig, unsigned int maxtime,
1320                   unsigned int idletime, isc_boolean_t many_answers,
1321                   xfrout_ctx_t **xfrp)
1322 {
1323         xfrout_ctx_t *xfr;
1324         isc_result_t result;
1325         unsigned int len;
1326         void *mem;
1327
1328         INSIST(xfrp != NULL && *xfrp == NULL);
1329         xfr = isc_mem_get(mctx, sizeof(*xfr));
1330         if (xfr == NULL)
1331                 return (ISC_R_NOMEMORY);
1332         xfr->mctx = mctx;
1333         xfr->client = NULL;
1334         ns_client_attach(client, &xfr->client);
1335         xfr->id = id;
1336         xfr->qname = qname;
1337         xfr->qtype = qtype;
1338         xfr->qclass = qclass;
1339         xfr->zone = NULL;
1340         xfr->db = NULL;
1341         xfr->ver = NULL;
1342         if (zone != NULL)       /* zone will be NULL if it's DLZ */
1343                 dns_zone_attach(zone, &xfr->zone);
1344         dns_db_attach(db, &xfr->db);
1345         dns_db_attachversion(db, ver, &xfr->ver);
1346         xfr->end_of_stream = ISC_FALSE;
1347         xfr->tsigkey = tsigkey;
1348         xfr->lasttsig = lasttsig;
1349         xfr->txmem = NULL;
1350         xfr->txmemlen = 0;
1351         xfr->nmsg = 0;
1352         xfr->many_answers = many_answers,
1353         xfr->sends = 0;
1354         xfr->shuttingdown = ISC_FALSE;
1355         xfr->mnemonic = NULL;
1356         xfr->buf.base = NULL;
1357         xfr->buf.length = 0;
1358         xfr->txmem = NULL;
1359         xfr->txmemlen = 0;
1360         xfr->stream = NULL;
1361         xfr->quota = NULL;
1362
1363         /*
1364          * Allocate a temporary buffer for the uncompressed response
1365          * message data.  The size should be no more than 65535 bytes
1366          * so that the compressed data will fit in a TCP message,
1367          * and no less than 65535 bytes so that an almost maximum-sized
1368          * RR will fit.  Note that although 65535-byte RRs are allowed
1369          * in principle, they cannot be zone-transferred (at least not
1370          * if uncompressible), because the message and RR headers would
1371          * push the size of the TCP message over the 65536 byte limit.
1372          */
1373         len = 65535;
1374         mem = isc_mem_get(mctx, len);
1375         if (mem == NULL) {
1376                 result = ISC_R_NOMEMORY;
1377                 goto failure;
1378         }
1379         isc_buffer_init(&xfr->buf, mem, len);
1380
1381         /*
1382          * Allocate another temporary buffer for the compressed
1383          * response message and its TCP length prefix.
1384          */
1385         len = 2 + 65535;
1386         mem = isc_mem_get(mctx, len);
1387         if (mem == NULL) {
1388                 result = ISC_R_NOMEMORY;
1389                 goto failure;
1390         }
1391         isc_buffer_init(&xfr->txlenbuf, mem, 2);
1392         isc_buffer_init(&xfr->txbuf, (char *) mem + 2, len - 2);
1393         xfr->txmem = mem;
1394         xfr->txmemlen = len;
1395
1396         CHECK(dns_timer_setidle(xfr->client->timer,
1397                                 maxtime, idletime, ISC_FALSE));
1398
1399         /*
1400          * Register a shutdown callback with the client, so that we
1401          * can stop the transfer immediately when the client task
1402          * gets a shutdown event.
1403          */
1404         xfr->client->shutdown = xfrout_client_shutdown;
1405         xfr->client->shutdown_arg = xfr;
1406         /*
1407          * These MUST be after the last "goto failure;" / CHECK to
1408          * prevent a double free by the caller.
1409          */
1410         xfr->quota = quota;
1411         xfr->stream = stream;
1412
1413         *xfrp = xfr;
1414         return (ISC_R_SUCCESS);
1415
1416 failure:
1417         xfrout_ctx_destroy(&xfr);
1418         return (result);
1419 }
1420
1421
1422 /*
1423  * Arrange to send as much as we can of "stream" without blocking.
1424  *
1425  * Requires:
1426  *      The stream iterator is initialized and points at an RR,
1427  *      or possibly at the end of the stream (that is, the
1428  *      _first method of the iterator has been called).
1429  */
1430 static void
1431 sendstream(xfrout_ctx_t *xfr) {
1432         dns_message_t *tcpmsg = NULL;
1433         dns_message_t *msg = NULL; /* Client message if UDP, tcpmsg if TCP */
1434         isc_result_t result;
1435         isc_region_t used;
1436         isc_region_t region;
1437         dns_rdataset_t *qrdataset;
1438         dns_name_t *msgname = NULL;
1439         dns_rdata_t *msgrdata = NULL;
1440         dns_rdatalist_t *msgrdl = NULL;
1441         dns_rdataset_t *msgrds = NULL;
1442         dns_compress_t cctx;
1443         isc_boolean_t cleanup_cctx = ISC_FALSE;
1444
1445         int n_rrs;
1446
1447         isc_buffer_clear(&xfr->buf);
1448         isc_buffer_clear(&xfr->txlenbuf);
1449         isc_buffer_clear(&xfr->txbuf);
1450
1451         if ((xfr->client->attributes & NS_CLIENTATTR_TCP) == 0) {
1452                 /*
1453                  * In the UDP case, we put the response data directly into
1454                  * the client message.
1455                  */
1456                 msg = xfr->client->message;
1457                 CHECK(dns_message_reply(msg, ISC_TRUE));
1458         } else {
1459                 /*
1460                  * TCP. Build a response dns_message_t, temporarily storing
1461                  * the raw, uncompressed owner names and RR data contiguously
1462                  * in xfr->buf.  We know that if the uncompressed data fits
1463                  * in xfr->buf, the compressed data will surely fit in a TCP
1464                  * message.
1465                  */
1466
1467                 CHECK(dns_message_create(xfr->mctx,
1468                                          DNS_MESSAGE_INTENTRENDER, &tcpmsg));
1469                 msg = tcpmsg;
1470
1471                 msg->id = xfr->id;
1472                 msg->rcode = dns_rcode_noerror;
1473                 msg->flags = DNS_MESSAGEFLAG_QR | DNS_MESSAGEFLAG_AA;
1474                 if ((xfr->client->attributes & NS_CLIENTATTR_RA) != 0)
1475                         msg->flags |= DNS_MESSAGEFLAG_RA;
1476                 CHECK(dns_message_settsigkey(msg, xfr->tsigkey));
1477                 CHECK(dns_message_setquerytsig(msg, xfr->lasttsig));
1478                 if (xfr->lasttsig != NULL)
1479                         isc_buffer_free(&xfr->lasttsig);
1480
1481                 /*
1482                  * Include a question section in the first message only.
1483                  * BIND 8.2.1 will not recognize an IXFR if it does not
1484                  * have a question section.
1485                  */
1486                 if (xfr->nmsg == 0) {
1487                         dns_name_t *qname = NULL;
1488                         isc_region_t r;
1489
1490                         /*
1491                          * Reserve space for the 12-byte message header
1492                          * and 4 bytes of question.
1493                          */
1494                         isc_buffer_add(&xfr->buf, 12 + 4);
1495
1496                         qrdataset = NULL;
1497                         result = dns_message_gettemprdataset(msg, &qrdataset);
1498                         if (result != ISC_R_SUCCESS)
1499                                 goto failure;
1500                         dns_rdataset_init(qrdataset);
1501                         dns_rdataset_makequestion(qrdataset,
1502                                         xfr->client->message->rdclass,
1503                                         xfr->qtype);
1504
1505                         result = dns_message_gettempname(msg, &qname);
1506                         if (result != ISC_R_SUCCESS)
1507                                 goto failure;
1508                         dns_name_init(qname, NULL);
1509                         isc_buffer_availableregion(&xfr->buf, &r);
1510                         INSIST(r.length >= xfr->qname->length);
1511                         r.length = xfr->qname->length;
1512                         isc_buffer_putmem(&xfr->buf, xfr->qname->ndata,
1513                                           xfr->qname->length);
1514                         dns_name_fromregion(qname, &r);
1515                         ISC_LIST_INIT(qname->list);
1516                         ISC_LIST_APPEND(qname->list, qrdataset, link);
1517
1518                         dns_message_addname(msg, qname, DNS_SECTION_QUESTION);
1519                 }
1520                 else
1521                         msg->tcp_continuation = 1;
1522         }
1523
1524         /*
1525          * Try to fit in as many RRs as possible, unless "one-answer"
1526          * format has been requested.
1527          */
1528         for (n_rrs = 0; ; n_rrs++) {
1529                 dns_name_t *name = NULL;
1530                 isc_uint32_t ttl;
1531                 dns_rdata_t *rdata = NULL;
1532
1533                 unsigned int size;
1534                 isc_region_t r;
1535
1536                 msgname = NULL;
1537                 msgrdata = NULL;
1538                 msgrdl = NULL;
1539                 msgrds = NULL;
1540
1541                 xfr->stream->methods->current(xfr->stream,
1542                                               &name, &ttl, &rdata);
1543                 size = name->length + 10 + rdata->length;
1544                 isc_buffer_availableregion(&xfr->buf, &r);
1545                 if (size >= r.length) {
1546                         /*
1547                          * RR would not fit.  If there are other RRs in the
1548                          * buffer, send them now and leave this RR to the
1549                          * next message.  If this RR overflows the buffer
1550                          * all by itself, fail.
1551                          *
1552                          * In theory some RRs might fit in a TCP message
1553                          * when compressed even if they do not fit when
1554                          * uncompressed, but surely we don't want
1555                          * to send such monstrosities to an unsuspecting
1556                          * slave.
1557                          */
1558                         if (n_rrs == 0) {
1559                                 xfrout_log(xfr, ISC_LOG_WARNING,
1560                                            "RR too large for zone transfer "
1561                                            "(%d bytes)", size);
1562                                 /* XXX DNS_R_RRTOOLARGE? */
1563                                 result = ISC_R_NOSPACE;
1564                                 goto failure;
1565                         }
1566                         break;
1567                 }
1568
1569                 if (isc_log_wouldlog(ns_g_lctx, XFROUT_RR_LOGLEVEL))
1570                         log_rr(name, rdata, ttl); /* XXX */
1571
1572                 result = dns_message_gettempname(msg, &msgname);
1573                 if (result != ISC_R_SUCCESS)
1574                         goto failure;
1575                 dns_name_init(msgname, NULL);
1576                 isc_buffer_availableregion(&xfr->buf, &r);
1577                 INSIST(r.length >= name->length);
1578                 r.length = name->length;
1579                 isc_buffer_putmem(&xfr->buf, name->ndata, name->length);
1580                 dns_name_fromregion(msgname, &r);
1581
1582                 /* Reserve space for RR header. */
1583                 isc_buffer_add(&xfr->buf, 10);
1584
1585                 result = dns_message_gettemprdata(msg, &msgrdata);
1586                 if (result != ISC_R_SUCCESS)
1587                         goto failure;
1588                 isc_buffer_availableregion(&xfr->buf, &r);
1589                 r.length = rdata->length;
1590                 isc_buffer_putmem(&xfr->buf, rdata->data, rdata->length);
1591                 dns_rdata_init(msgrdata);
1592                 dns_rdata_fromregion(msgrdata,
1593                                      rdata->rdclass, rdata->type, &r);
1594
1595                 result = dns_message_gettemprdatalist(msg, &msgrdl);
1596                 if (result != ISC_R_SUCCESS)
1597                         goto failure;
1598                 msgrdl->type = rdata->type;
1599                 msgrdl->rdclass = rdata->rdclass;
1600                 msgrdl->ttl = ttl;
1601                 if (rdata->type == dns_rdatatype_sig ||
1602                     rdata->type == dns_rdatatype_rrsig)
1603                         msgrdl->covers = dns_rdata_covers(rdata);
1604                 else
1605                         msgrdl->covers = dns_rdatatype_none;
1606                 ISC_LINK_INIT(msgrdl, link);
1607                 ISC_LIST_INIT(msgrdl->rdata);
1608                 ISC_LIST_APPEND(msgrdl->rdata, msgrdata, link);
1609
1610                 result = dns_message_gettemprdataset(msg, &msgrds);
1611                 if (result != ISC_R_SUCCESS)
1612                         goto failure;
1613                 dns_rdataset_init(msgrds);
1614                 result = dns_rdatalist_tordataset(msgrdl, msgrds);
1615                 INSIST(result == ISC_R_SUCCESS);
1616
1617                 ISC_LIST_APPEND(msgname->list, msgrds, link);
1618
1619                 dns_message_addname(msg, msgname, DNS_SECTION_ANSWER);
1620                 msgname = NULL;
1621
1622                 result = xfr->stream->methods->next(xfr->stream);
1623                 if (result == ISC_R_NOMORE) {
1624                         xfr->end_of_stream = ISC_TRUE;
1625                         break;
1626                 }
1627                 CHECK(result);
1628
1629                 if (! xfr->many_answers)
1630                         break;
1631         }
1632
1633         if ((xfr->client->attributes & NS_CLIENTATTR_TCP) != 0) {
1634                 CHECK(dns_compress_init(&cctx, -1, xfr->mctx));
1635                 dns_compress_setsensitive(&cctx, ISC_TRUE);
1636                 cleanup_cctx = ISC_TRUE;
1637                 CHECK(dns_message_renderbegin(msg, &cctx, &xfr->txbuf));
1638                 CHECK(dns_message_rendersection(msg, DNS_SECTION_QUESTION, 0));
1639                 CHECK(dns_message_rendersection(msg, DNS_SECTION_ANSWER, 0));
1640                 CHECK(dns_message_renderend(msg));
1641                 dns_compress_invalidate(&cctx);
1642                 cleanup_cctx = ISC_FALSE;
1643
1644                 isc_buffer_usedregion(&xfr->txbuf, &used);
1645                 isc_buffer_putuint16(&xfr->txlenbuf,
1646                                      (isc_uint16_t)used.length);
1647                 region.base = xfr->txlenbuf.base;
1648                 region.length = 2 + used.length;
1649                 xfrout_log(xfr, ISC_LOG_DEBUG(8),
1650                            "sending TCP message of %d bytes",
1651                            used.length);
1652                 CHECK(isc_socket_send(xfr->client->tcpsocket, /* XXX */
1653                                       &region, xfr->client->task,
1654                                       xfrout_senddone,
1655                                       xfr));
1656                 xfr->sends++;
1657         } else {
1658                 xfrout_log(xfr, ISC_LOG_DEBUG(8), "sending IXFR UDP response");
1659                 ns_client_send(xfr->client);
1660                 xfr->stream->methods->pause(xfr->stream);
1661                 xfrout_ctx_destroy(&xfr);
1662                 return;
1663         }
1664
1665         /* Advance lasttsig to be the last TSIG generated */
1666         CHECK(dns_message_getquerytsig(msg, xfr->mctx, &xfr->lasttsig));
1667
1668         xfr->nmsg++;
1669
1670  failure:
1671         if (msgname != NULL) {
1672                 if (msgrds != NULL) {
1673                         if (dns_rdataset_isassociated(msgrds))
1674                                 dns_rdataset_disassociate(msgrds);
1675                         dns_message_puttemprdataset(msg, &msgrds);
1676                 }
1677                 if (msgrdl != NULL) {
1678                         ISC_LIST_UNLINK(msgrdl->rdata, msgrdata, link);
1679                         dns_message_puttemprdatalist(msg, &msgrdl);
1680                 }
1681                 if (msgrdata != NULL)
1682                         dns_message_puttemprdata(msg, &msgrdata);
1683                 dns_message_puttempname(msg, &msgname);
1684         }
1685
1686         if (tcpmsg != NULL)
1687                 dns_message_destroy(&tcpmsg);
1688
1689         if (cleanup_cctx)
1690                 dns_compress_invalidate(&cctx);
1691         /*
1692          * Make sure to release any locks held by database
1693          * iterators before returning from the event handler.
1694          */
1695         xfr->stream->methods->pause(xfr->stream);
1696
1697         if (result == ISC_R_SUCCESS)
1698                 return;
1699
1700         xfrout_fail(xfr, result, "sending zone data");
1701 }
1702
1703 static void
1704 xfrout_ctx_destroy(xfrout_ctx_t **xfrp) {
1705         xfrout_ctx_t *xfr = *xfrp;
1706
1707         INSIST(xfr->sends == 0);
1708
1709         xfr->client->shutdown = NULL;
1710         xfr->client->shutdown_arg = NULL;
1711
1712         if (xfr->stream != NULL)
1713                 xfr->stream->methods->destroy(&xfr->stream);
1714         if (xfr->buf.base != NULL)
1715                 isc_mem_put(xfr->mctx, xfr->buf.base, xfr->buf.length);
1716         if (xfr->txmem != NULL)
1717                 isc_mem_put(xfr->mctx, xfr->txmem, xfr->txmemlen);
1718         if (xfr->lasttsig != NULL)
1719                 isc_buffer_free(&xfr->lasttsig);
1720         if (xfr->quota != NULL)
1721                 isc_quota_detach(&xfr->quota);
1722         if (xfr->ver != NULL)
1723                 dns_db_closeversion(xfr->db, &xfr->ver, ISC_FALSE);
1724         if (xfr->zone != NULL)
1725                 dns_zone_detach(&xfr->zone);
1726         if (xfr->db != NULL)
1727                 dns_db_detach(&xfr->db);
1728
1729         ns_client_detach(&xfr->client);
1730
1731         isc_mem_put(xfr->mctx, xfr, sizeof(*xfr));
1732
1733         *xfrp = NULL;
1734 }
1735
1736 static void
1737 xfrout_senddone(isc_task_t *task, isc_event_t *event) {
1738         isc_socketevent_t *sev = (isc_socketevent_t *)event;
1739         xfrout_ctx_t *xfr = (xfrout_ctx_t *)event->ev_arg;
1740         isc_result_t evresult = sev->result;
1741
1742         UNUSED(task);
1743
1744         INSIST(event->ev_type == ISC_SOCKEVENT_SENDDONE);
1745
1746         isc_event_free(&event);
1747         xfr->sends--;
1748         INSIST(xfr->sends == 0);
1749
1750         (void)isc_timer_touch(xfr->client->timer);
1751         if (xfr->shuttingdown == ISC_TRUE) {
1752                 xfrout_maybe_destroy(xfr);
1753         } else if (evresult != ISC_R_SUCCESS) {
1754                 xfrout_fail(xfr, evresult, "send");
1755         } else if (xfr->end_of_stream == ISC_FALSE) {
1756                 sendstream(xfr);
1757         } else {
1758                 /* End of zone transfer stream. */
1759                 inc_stats(xfr->zone, dns_nsstatscounter_xfrdone);
1760                 xfrout_log(xfr, ISC_LOG_INFO, "%s ended", xfr->mnemonic);
1761                 ns_client_next(xfr->client, ISC_R_SUCCESS);
1762                 xfrout_ctx_destroy(&xfr);
1763         }
1764 }
1765
1766 static void
1767 xfrout_fail(xfrout_ctx_t *xfr, isc_result_t result, const char *msg) {
1768         xfr->shuttingdown = ISC_TRUE;
1769         xfrout_log(xfr, ISC_LOG_ERROR, "%s: %s",
1770                    msg, isc_result_totext(result));
1771         xfrout_maybe_destroy(xfr);
1772 }
1773
1774 static void
1775 xfrout_maybe_destroy(xfrout_ctx_t *xfr) {
1776         INSIST(xfr->shuttingdown == ISC_TRUE);
1777         if (xfr->sends > 0) {
1778                 /*
1779                  * If we are currently sending, cancel it and wait for
1780                  * cancel event before destroying the context.
1781                  */
1782                 isc_socket_cancel(xfr->client->tcpsocket, xfr->client->task,
1783                                   ISC_SOCKCANCEL_SEND);
1784         } else {
1785                 ns_client_next(xfr->client, ISC_R_CANCELED);
1786                 xfrout_ctx_destroy(&xfr);
1787         }
1788 }
1789
1790 static void
1791 xfrout_client_shutdown(void *arg, isc_result_t result) {
1792         xfrout_ctx_t *xfr = (xfrout_ctx_t *) arg;
1793         xfrout_fail(xfr, result, "aborted");
1794 }
1795
1796 /*
1797  * Log outgoing zone transfer messages in a format like
1798  * <client>: transfer of <zone>: <message>
1799  */
1800
1801 static void
1802 xfrout_logv(ns_client_t *client, dns_name_t *zonename,
1803             dns_rdataclass_t rdclass, int level, const char *fmt, va_list ap)
1804      ISC_FORMAT_PRINTF(5, 0);
1805
1806 static void
1807 xfrout_logv(ns_client_t *client, dns_name_t *zonename,
1808             dns_rdataclass_t rdclass, int level, const char *fmt, va_list ap)
1809 {
1810         char msgbuf[2048];
1811         char namebuf[DNS_NAME_FORMATSIZE];
1812         char classbuf[DNS_RDATACLASS_FORMATSIZE];
1813
1814         dns_name_format(zonename, namebuf, sizeof(namebuf));
1815         dns_rdataclass_format(rdclass, classbuf, sizeof(classbuf));
1816         vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
1817         ns_client_log(client, DNS_LOGCATEGORY_XFER_OUT,
1818                       NS_LOGMODULE_XFER_OUT, level,
1819                       "transfer of '%s/%s': %s", namebuf, classbuf, msgbuf);
1820 }
1821
1822 /*
1823  * Logging function for use when a xfrout_ctx_t has not yet been created.
1824  */
1825 static void
1826 xfrout_log1(ns_client_t *client, dns_name_t *zonename,
1827             dns_rdataclass_t rdclass, int level, const char *fmt, ...) {
1828         va_list ap;
1829         va_start(ap, fmt);
1830         xfrout_logv(client, zonename, rdclass, level, fmt, ap);
1831         va_end(ap);
1832 }
1833
1834 /*
1835  * Logging function for use when there is a xfrout_ctx_t.
1836  */
1837 static void
1838 xfrout_log(xfrout_ctx_t *xfr, int level, const char *fmt, ...) {
1839         va_list ap;
1840         va_start(ap, fmt);
1841         xfrout_logv(xfr->client, xfr->qname, xfr->qclass, level, fmt, ap);
1842         va_end(ap);
1843 }