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