Home | History | Annotate | Line # | Download | only in dns
master.c revision 1.1.1.3
      1      1.1  christos /*	$NetBSD: master.c,v 1.1.1.3 2019/09/05 19:27:39 christos Exp $	*/
      2      1.1  christos 
      3      1.1  christos /*
      4      1.1  christos  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5      1.1  christos  *
      6      1.1  christos  * This Source Code Form is subject to the terms of the Mozilla Public
      7      1.1  christos  * License, v. 2.0. If a copy of the MPL was not distributed with this
      8      1.1  christos  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9      1.1  christos  *
     10      1.1  christos  * See the COPYRIGHT file distributed with this work for additional
     11      1.1  christos  * information regarding copyright ownership.
     12      1.1  christos  */
     13      1.1  christos 
     14      1.1  christos /*! \file */
     15      1.1  christos 
     16      1.1  christos #include <config.h>
     17      1.1  christos 
     18  1.1.1.2  christos #include <inttypes.h>
     19  1.1.1.2  christos #include <stdbool.h>
     20  1.1.1.2  christos 
     21      1.1  christos #include <isc/event.h>
     22      1.1  christos #include <isc/lex.h>
     23      1.1  christos #include <isc/magic.h>
     24      1.1  christos #include <isc/mem.h>
     25      1.1  christos #include <isc/print.h>
     26      1.1  christos #include <isc/serial.h>
     27      1.1  christos #include <isc/stdio.h>
     28      1.1  christos #include <isc/stdtime.h>
     29      1.1  christos #include <isc/string.h>
     30      1.1  christos #include <isc/task.h>
     31      1.1  christos #include <isc/util.h>
     32      1.1  christos 
     33      1.1  christos #include <dns/callbacks.h>
     34      1.1  christos #include <dns/events.h>
     35      1.1  christos #include <dns/fixedname.h>
     36      1.1  christos #include <dns/master.h>
     37      1.1  christos #include <dns/name.h>
     38      1.1  christos #include <dns/rdata.h>
     39      1.1  christos #include <dns/rdataclass.h>
     40      1.1  christos #include <dns/rdatalist.h>
     41      1.1  christos #include <dns/rdataset.h>
     42      1.1  christos #include <dns/rdatastruct.h>
     43      1.1  christos #include <dns/rdatatype.h>
     44      1.1  christos #include <dns/result.h>
     45      1.1  christos #include <dns/soa.h>
     46      1.1  christos #include <dns/time.h>
     47      1.1  christos #include <dns/ttl.h>
     48      1.1  christos 
     49      1.1  christos /*!
     50      1.1  christos  * Grow the number of dns_rdatalist_t (#RDLSZ) and dns_rdata_t (#RDSZ) structures
     51      1.1  christos  * by these sizes when we need to.
     52      1.1  christos  *
     53      1.1  christos  */
     54      1.1  christos /*% RDLSZ reflects the number of different types with the same name expected. */
     55      1.1  christos #define RDLSZ 32
     56      1.1  christos /*%
     57      1.1  christos  * RDSZ reflects the number of rdata expected at a give name that can fit into
     58      1.1  christos  * 64k.
     59      1.1  christos  */
     60      1.1  christos #define RDSZ 512
     61      1.1  christos 
     62      1.1  christos #define NBUFS 4
     63      1.1  christos #define MAXWIRESZ 255
     64      1.1  christos 
     65      1.1  christos /*%
     66      1.1  christos  * Target buffer size and minimum target size.
     67      1.1  christos  * MINTSIZ must be big enough to hold the largest rdata record.
     68      1.1  christos  * \brief
     69      1.1  christos  * TSIZ >= MINTSIZ
     70      1.1  christos  */
     71      1.1  christos #define TSIZ (128*1024)
     72      1.1  christos /*%
     73      1.1  christos  * max message size - header - root - type - class - ttl - rdlen
     74      1.1  christos  */
     75      1.1  christos #define MINTSIZ DNS_RDATA_MAXLENGTH
     76      1.1  christos /*%
     77      1.1  christos  * Size for tokens in the presentation format,
     78      1.1  christos  * The largest tokens are the base64 blocks in KEY and CERT records,
     79      1.1  christos  * Largest key allowed is about 1372 bytes but
     80      1.1  christos  * there is no fixed upper bound on CERT records.
     81      1.1  christos  * 2K is too small for some X.509s, 8K is overkill.
     82      1.1  christos  */
     83      1.1  christos #define TOKENSIZ (8*1024)
     84      1.1  christos 
     85      1.1  christos /*%
     86      1.1  christos  * Buffers sizes for $GENERATE.
     87      1.1  christos  */
     88      1.1  christos #define DNS_MASTER_LHS 2048
     89      1.1  christos #define DNS_MASTER_RHS MINTSIZ
     90      1.1  christos 
     91      1.1  christos #define CHECKNAMESFAIL(x) (((x) & DNS_MASTER_CHECKNAMESFAIL) != 0)
     92      1.1  christos 
     93      1.1  christos typedef ISC_LIST(dns_rdatalist_t) rdatalist_head_t;
     94      1.1  christos 
     95      1.1  christos typedef struct dns_incctx dns_incctx_t;
     96      1.1  christos 
     97      1.1  christos /*%
     98      1.1  christos  * Master file load state.
     99      1.1  christos  */
    100      1.1  christos 
    101      1.1  christos struct dns_loadctx {
    102      1.1  christos 	unsigned int		magic;
    103      1.1  christos 	isc_mem_t		*mctx;
    104      1.1  christos 	dns_masterformat_t	format;
    105      1.1  christos 
    106      1.1  christos 	dns_rdatacallbacks_t	*callbacks;
    107      1.1  christos 	isc_task_t		*task;
    108      1.1  christos 	dns_loaddonefunc_t	done;
    109      1.1  christos 	void			*done_arg;
    110      1.1  christos 
    111      1.1  christos 	/* Common methods */
    112      1.1  christos 	isc_result_t		(*openfile)(dns_loadctx_t *lctx,
    113      1.1  christos 					    const char *filename);
    114      1.1  christos 	isc_result_t		(*load)(dns_loadctx_t *lctx);
    115      1.1  christos 
    116      1.1  christos 	/* Members used by all formats */
    117  1.1.1.2  christos 	uint32_t		maxttl;
    118      1.1  christos 
    119      1.1  christos 	/* Members specific to the text format: */
    120      1.1  christos 	isc_lex_t		*lex;
    121  1.1.1.2  christos 	bool		keep_lex;
    122      1.1  christos 	unsigned int		options;
    123  1.1.1.2  christos 	bool		ttl_known;
    124  1.1.1.2  christos 	bool		default_ttl_known;
    125  1.1.1.2  christos 	bool		warn_1035;
    126  1.1.1.2  christos 	bool		warn_tcr;
    127  1.1.1.2  christos 	bool		warn_sigexpired;
    128  1.1.1.2  christos 	bool		seen_include;
    129  1.1.1.2  christos 	uint32_t		ttl;
    130  1.1.1.2  christos 	uint32_t		default_ttl;
    131      1.1  christos 	dns_rdataclass_t	zclass;
    132      1.1  christos 	dns_fixedname_t		fixed_top;
    133      1.1  christos 	dns_name_t		*top;			/*%< top of zone */
    134      1.1  christos 
    135      1.1  christos 	/* Members specific to the raw format: */
    136      1.1  christos 	FILE			*f;
    137  1.1.1.2  christos 	bool		first;
    138      1.1  christos 	dns_masterrawheader_t	header;
    139      1.1  christos 
    140      1.1  christos 	/* Which fixed buffers we are using? */
    141      1.1  christos 	unsigned int		loop_cnt;		/*% records per quantum,
    142      1.1  christos 							 * 0 => all. */
    143  1.1.1.2  christos 	bool		canceled;
    144      1.1  christos 	isc_mutex_t		lock;
    145      1.1  christos 	isc_result_t		result;
    146      1.1  christos 	/* locked by lock */
    147  1.1.1.2  christos 	uint32_t		references;
    148      1.1  christos 	dns_incctx_t		*inc;
    149  1.1.1.2  christos 	uint32_t		resign;
    150      1.1  christos 	isc_stdtime_t		now;
    151      1.1  christos 
    152      1.1  christos 	dns_masterincludecb_t	include_cb;
    153      1.1  christos 	void			*include_arg;
    154      1.1  christos };
    155      1.1  christos 
    156      1.1  christos struct dns_incctx {
    157      1.1  christos 	dns_incctx_t		*parent;
    158      1.1  christos 	dns_name_t		*origin;
    159      1.1  christos 	dns_name_t		*current;
    160      1.1  christos 	dns_name_t		*glue;
    161      1.1  christos 	dns_fixedname_t		fixed[NBUFS];		/* working buffers */
    162      1.1  christos 	unsigned int		in_use[NBUFS];		/* covert to bitmap? */
    163      1.1  christos 	int			glue_in_use;
    164      1.1  christos 	int			current_in_use;
    165      1.1  christos 	int			origin_in_use;
    166  1.1.1.2  christos 	bool		origin_changed;
    167  1.1.1.2  christos 	bool		drop;
    168      1.1  christos 	unsigned int		glue_line;
    169      1.1  christos 	unsigned int		current_line;
    170      1.1  christos };
    171      1.1  christos 
    172      1.1  christos #define DNS_LCTX_MAGIC ISC_MAGIC('L','c','t','x')
    173      1.1  christos #define DNS_LCTX_VALID(lctx) ISC_MAGIC_VALID(lctx, DNS_LCTX_MAGIC)
    174      1.1  christos 
    175      1.1  christos #define DNS_AS_STR(t) ((t).value.as_textregion.base)
    176      1.1  christos 
    177      1.1  christos static isc_result_t
    178      1.1  christos openfile_text(dns_loadctx_t *lctx, const char *master_file);
    179      1.1  christos 
    180      1.1  christos static isc_result_t
    181      1.1  christos load_text(dns_loadctx_t *lctx);
    182      1.1  christos 
    183      1.1  christos static isc_result_t
    184      1.1  christos openfile_raw(dns_loadctx_t *lctx, const char *master_file);
    185      1.1  christos 
    186      1.1  christos static isc_result_t
    187      1.1  christos load_raw(dns_loadctx_t *lctx);
    188      1.1  christos 
    189      1.1  christos static isc_result_t
    190      1.1  christos openfile_map(dns_loadctx_t *lctx, const char *master_file);
    191      1.1  christos 
    192      1.1  christos static isc_result_t
    193      1.1  christos load_map(dns_loadctx_t *lctx);
    194      1.1  christos 
    195      1.1  christos static isc_result_t
    196      1.1  christos pushfile(const char *master_file, dns_name_t *origin, dns_loadctx_t *lctx);
    197      1.1  christos 
    198      1.1  christos static isc_result_t
    199      1.1  christos commit(dns_rdatacallbacks_t *, dns_loadctx_t *, rdatalist_head_t *,
    200      1.1  christos        dns_name_t *, const char *, unsigned int);
    201      1.1  christos 
    202  1.1.1.2  christos static bool
    203      1.1  christos is_glue(rdatalist_head_t *, dns_name_t *);
    204      1.1  christos 
    205      1.1  christos static dns_rdatalist_t *
    206      1.1  christos grow_rdatalist(int, dns_rdatalist_t *, int, rdatalist_head_t *,
    207      1.1  christos 		rdatalist_head_t *, isc_mem_t *mctx);
    208      1.1  christos 
    209      1.1  christos static dns_rdata_t *
    210      1.1  christos grow_rdata(int, dns_rdata_t *, int, rdatalist_head_t *, rdatalist_head_t *,
    211      1.1  christos 	   isc_mem_t *);
    212      1.1  christos 
    213      1.1  christos static void
    214      1.1  christos load_quantum(isc_task_t *task, isc_event_t *event);
    215      1.1  christos 
    216      1.1  christos static isc_result_t
    217      1.1  christos task_send(dns_loadctx_t *lctx);
    218      1.1  christos 
    219      1.1  christos static void
    220      1.1  christos loadctx_destroy(dns_loadctx_t *lctx);
    221      1.1  christos 
    222      1.1  christos #define GETTOKENERR(lexer, options, token, eol, err) \
    223      1.1  christos 	do { \
    224      1.1  christos 		result = gettoken(lexer, options, token, eol, callbacks); \
    225      1.1  christos 		switch (result) { \
    226      1.1  christos 		case ISC_R_SUCCESS: \
    227      1.1  christos 			break; \
    228      1.1  christos 		case ISC_R_UNEXPECTED: \
    229      1.1  christos 			goto insist_and_cleanup; \
    230      1.1  christos 		default: \
    231      1.1  christos 			if (MANYERRS(lctx, result)) { \
    232      1.1  christos 				SETRESULT(lctx, result); \
    233      1.1  christos 				LOGIT(result); \
    234  1.1.1.2  christos 				read_till_eol = true; \
    235      1.1  christos 				err \
    236      1.1  christos 				goto next_line; \
    237      1.1  christos 			} else \
    238      1.1  christos 				goto log_and_cleanup; \
    239      1.1  christos 		} \
    240      1.1  christos 		if ((token)->type == isc_tokentype_special) { \
    241      1.1  christos 			result = DNS_R_SYNTAX; \
    242      1.1  christos 			if (MANYERRS(lctx, result)) { \
    243      1.1  christos 				SETRESULT(lctx, result); \
    244      1.1  christos 				LOGIT(result); \
    245  1.1.1.2  christos 				read_till_eol = true; \
    246      1.1  christos 				goto next_line; \
    247      1.1  christos 			} else \
    248      1.1  christos 				goto log_and_cleanup; \
    249      1.1  christos 		} \
    250      1.1  christos 	} while (0)
    251      1.1  christos #define GETTOKEN(lexer, options, token, eol) \
    252      1.1  christos 	GETTOKENERR(lexer, options, token, eol, {} )
    253      1.1  christos 
    254      1.1  christos #define COMMITALL \
    255      1.1  christos 	do { \
    256      1.1  christos 		result = commit(callbacks, lctx, &current_list, \
    257      1.1  christos 				ictx->current, source, ictx->current_line); \
    258      1.1  christos 		if (MANYERRS(lctx, result)) { \
    259      1.1  christos 			SETRESULT(lctx, result); \
    260      1.1  christos 		} else if (result != ISC_R_SUCCESS) \
    261      1.1  christos 			goto insist_and_cleanup; \
    262      1.1  christos 		result = commit(callbacks, lctx, &glue_list, \
    263      1.1  christos 				ictx->glue, source, ictx->glue_line); \
    264      1.1  christos 		if (MANYERRS(lctx, result)) { \
    265      1.1  christos 			SETRESULT(lctx, result); \
    266      1.1  christos 		} else if (result != ISC_R_SUCCESS) \
    267      1.1  christos 			goto insist_and_cleanup; \
    268      1.1  christos 		rdcount = 0; \
    269      1.1  christos 		rdlcount = 0; \
    270      1.1  christos 		isc_buffer_init(&target, target_mem, target_size); \
    271      1.1  christos 		rdcount_save = rdcount; \
    272      1.1  christos 		rdlcount_save = rdlcount; \
    273      1.1  christos 	} while (0)
    274      1.1  christos 
    275      1.1  christos #define WARNUNEXPECTEDEOF(lexer) \
    276      1.1  christos 	do { \
    277      1.1  christos 		if (isc_lex_isfile(lexer)) \
    278      1.1  christos 			(*callbacks->warn)(callbacks, \
    279      1.1  christos 				"%s: file does not end with newline", \
    280      1.1  christos 				source); \
    281      1.1  christos 	} while (0)
    282      1.1  christos 
    283      1.1  christos #define EXPECTEOL \
    284      1.1  christos 	do { \
    285  1.1.1.2  christos 		GETTOKEN(lctx->lex, 0, &token, true); \
    286      1.1  christos 		if (token.type != isc_tokentype_eol) { \
    287      1.1  christos 			isc_lex_ungettoken(lctx->lex, &token); \
    288      1.1  christos 			result = DNS_R_EXTRATOKEN; \
    289      1.1  christos 			if (MANYERRS(lctx, result)) { \
    290      1.1  christos 				SETRESULT(lctx, result); \
    291      1.1  christos 				LOGIT(result); \
    292  1.1.1.2  christos 				read_till_eol = true; \
    293      1.1  christos 				break; \
    294      1.1  christos 			} else if (result != ISC_R_SUCCESS) \
    295      1.1  christos 				goto log_and_cleanup; \
    296      1.1  christos 		} \
    297      1.1  christos 	} while (0)
    298      1.1  christos 
    299      1.1  christos #define MANYERRS(lctx, result) \
    300      1.1  christos 		((result != ISC_R_SUCCESS) && \
    301      1.1  christos 		 (result != ISC_R_IOERROR) && \
    302      1.1  christos 		 ((lctx)->options & DNS_MASTER_MANYERRORS) != 0)
    303      1.1  christos 
    304      1.1  christos #define SETRESULT(lctx, r) \
    305      1.1  christos 		do { \
    306      1.1  christos 			if ((lctx)->result == ISC_R_SUCCESS) \
    307      1.1  christos 				(lctx)->result = r; \
    308      1.1  christos 		} while (0)
    309      1.1  christos 
    310      1.1  christos #define LOGITFILE(result, filename) \
    311      1.1  christos 	if (result == ISC_R_INVALIDFILE || result == ISC_R_FILENOTFOUND || \
    312      1.1  christos 	    result == ISC_R_IOERROR || result == ISC_R_TOOMANYOPENFILES || \
    313      1.1  christos 	    result == ISC_R_NOPERM) \
    314      1.1  christos 		(*callbacks->error)(callbacks, "%s: %s:%lu: %s: %s", \
    315      1.1  christos 				    "dns_master_load", source, line, \
    316      1.1  christos 				    filename, dns_result_totext(result)); \
    317      1.1  christos 	else LOGIT(result)
    318      1.1  christos 
    319      1.1  christos #define LOGIT(result) \
    320      1.1  christos 	if (result == ISC_R_NOMEMORY) \
    321      1.1  christos 		(*callbacks->error)(callbacks, "dns_master_load: %s", \
    322      1.1  christos 				    dns_result_totext(result)); \
    323      1.1  christos 	else \
    324      1.1  christos 		(*callbacks->error)(callbacks, "%s: %s:%lu: %s", \
    325      1.1  christos 				    "dns_master_load", \
    326      1.1  christos 				    source, line, dns_result_totext(result))
    327      1.1  christos 
    328      1.1  christos 
    329      1.1  christos static unsigned char in_addr_arpa_data[]  = "\007IN-ADDR\004ARPA";
    330      1.1  christos static unsigned char in_addr_arpa_offsets[] = { 0, 8, 13 };
    331      1.1  christos static dns_name_t const in_addr_arpa =
    332      1.1  christos 	DNS_NAME_INITABSOLUTE(in_addr_arpa_data, in_addr_arpa_offsets);
    333      1.1  christos 
    334      1.1  christos static unsigned char ip6_int_data[]  = "\003IP6\003INT";
    335      1.1  christos static unsigned char ip6_int_offsets[] = { 0, 4, 8 };
    336      1.1  christos static dns_name_t const ip6_int =
    337      1.1  christos 	DNS_NAME_INITABSOLUTE(ip6_int_data, ip6_int_offsets);
    338      1.1  christos 
    339      1.1  christos static unsigned char ip6_arpa_data[]  = "\003IP6\004ARPA";
    340      1.1  christos static unsigned char ip6_arpa_offsets[] = { 0, 4, 9 };
    341      1.1  christos static dns_name_t const ip6_arpa =
    342      1.1  christos 	DNS_NAME_INITABSOLUTE(ip6_arpa_data, ip6_arpa_offsets);
    343      1.1  christos 
    344      1.1  christos static inline isc_result_t
    345      1.1  christos gettoken(isc_lex_t *lex, unsigned int options, isc_token_t *token,
    346  1.1.1.2  christos 	 bool eol, dns_rdatacallbacks_t *callbacks)
    347      1.1  christos {
    348      1.1  christos 	isc_result_t result;
    349      1.1  christos 
    350      1.1  christos 	options |= ISC_LEXOPT_EOL | ISC_LEXOPT_EOF | ISC_LEXOPT_DNSMULTILINE |
    351      1.1  christos 		ISC_LEXOPT_ESCAPE;
    352      1.1  christos 	result = isc_lex_gettoken(lex, options, token);
    353      1.1  christos 	if (result != ISC_R_SUCCESS) {
    354      1.1  christos 		switch (result) {
    355      1.1  christos 		case ISC_R_NOMEMORY:
    356      1.1  christos 			return (ISC_R_NOMEMORY);
    357      1.1  christos 		default:
    358      1.1  christos 			(*callbacks->error)(callbacks,
    359      1.1  christos 					    "dns_master_load: %s:%lu:"
    360      1.1  christos 					    " isc_lex_gettoken() failed: %s",
    361      1.1  christos 					    isc_lex_getsourcename(lex),
    362      1.1  christos 					    isc_lex_getsourceline(lex),
    363      1.1  christos 					    isc_result_totext(result));
    364      1.1  christos 			return (result);
    365      1.1  christos 		}
    366      1.1  christos 		/*NOTREACHED*/
    367      1.1  christos 	}
    368  1.1.1.2  christos 	if (eol != true)
    369      1.1  christos 		if (token->type == isc_tokentype_eol ||
    370      1.1  christos 		    token->type == isc_tokentype_eof) {
    371      1.1  christos 			unsigned long int line;
    372      1.1  christos 			const char *what;
    373      1.1  christos 			const char *file;
    374      1.1  christos 			file = isc_lex_getsourcename(lex);
    375      1.1  christos 			line = isc_lex_getsourceline(lex);
    376      1.1  christos 			if (token->type == isc_tokentype_eol) {
    377      1.1  christos 				line--;
    378      1.1  christos 				what = "line";
    379      1.1  christos 			} else
    380      1.1  christos 				what = "file";
    381      1.1  christos 			(*callbacks->error)(callbacks,
    382      1.1  christos 			    "dns_master_load: %s:%lu: unexpected end of %s",
    383      1.1  christos 					    file, line, what);
    384      1.1  christos 			return (ISC_R_UNEXPECTEDEND);
    385      1.1  christos 		}
    386      1.1  christos 	return (ISC_R_SUCCESS);
    387      1.1  christos }
    388      1.1  christos 
    389      1.1  christos 
    390      1.1  christos void
    391      1.1  christos dns_loadctx_attach(dns_loadctx_t *source, dns_loadctx_t **target) {
    392      1.1  christos 
    393      1.1  christos 	REQUIRE(target != NULL && *target == NULL);
    394      1.1  christos 	REQUIRE(DNS_LCTX_VALID(source));
    395      1.1  christos 
    396      1.1  christos 	LOCK(&source->lock);
    397      1.1  christos 	INSIST(source->references > 0);
    398      1.1  christos 	source->references++;
    399      1.1  christos 	INSIST(source->references != 0);	/* Overflow? */
    400      1.1  christos 	UNLOCK(&source->lock);
    401      1.1  christos 
    402      1.1  christos 	*target = source;
    403      1.1  christos }
    404      1.1  christos 
    405      1.1  christos void
    406      1.1  christos dns_loadctx_detach(dns_loadctx_t **lctxp) {
    407      1.1  christos 	dns_loadctx_t *lctx;
    408  1.1.1.2  christos 	bool need_destroy = false;
    409      1.1  christos 
    410      1.1  christos 	REQUIRE(lctxp != NULL);
    411      1.1  christos 	lctx = *lctxp;
    412      1.1  christos 	REQUIRE(DNS_LCTX_VALID(lctx));
    413      1.1  christos 
    414      1.1  christos 	LOCK(&lctx->lock);
    415      1.1  christos 	INSIST(lctx->references > 0);
    416      1.1  christos 	lctx->references--;
    417      1.1  christos 	if (lctx->references == 0)
    418  1.1.1.2  christos 		need_destroy = true;
    419      1.1  christos 	UNLOCK(&lctx->lock);
    420      1.1  christos 
    421      1.1  christos 	if (need_destroy)
    422      1.1  christos 		loadctx_destroy(lctx);
    423      1.1  christos 	*lctxp = NULL;
    424      1.1  christos }
    425      1.1  christos 
    426      1.1  christos static void
    427      1.1  christos incctx_destroy(isc_mem_t *mctx, dns_incctx_t *ictx) {
    428      1.1  christos 	dns_incctx_t *parent;
    429      1.1  christos 
    430      1.1  christos  again:
    431      1.1  christos 	parent = ictx->parent;
    432      1.1  christos 	ictx->parent = NULL;
    433      1.1  christos 
    434      1.1  christos 	isc_mem_put(mctx, ictx, sizeof(*ictx));
    435      1.1  christos 
    436      1.1  christos 	if (parent != NULL) {
    437      1.1  christos 		ictx = parent;
    438      1.1  christos 		goto again;
    439      1.1  christos 	}
    440      1.1  christos }
    441      1.1  christos 
    442      1.1  christos static void
    443      1.1  christos loadctx_destroy(dns_loadctx_t *lctx) {
    444      1.1  christos 	isc_mem_t *mctx;
    445      1.1  christos 	isc_result_t result;
    446      1.1  christos 
    447      1.1  christos 	REQUIRE(DNS_LCTX_VALID(lctx));
    448      1.1  christos 
    449      1.1  christos 	lctx->magic = 0;
    450      1.1  christos 	if (lctx->inc != NULL)
    451      1.1  christos 		incctx_destroy(lctx->mctx, lctx->inc);
    452      1.1  christos 
    453      1.1  christos 	if (lctx->f != NULL) {
    454      1.1  christos 		result = isc_stdio_close(lctx->f);
    455      1.1  christos 		if (result != ISC_R_SUCCESS) {
    456      1.1  christos 			UNEXPECTED_ERROR(__FILE__, __LINE__,
    457      1.1  christos 					 "isc_stdio_close() failed: %s",
    458      1.1  christos 					 isc_result_totext(result));
    459      1.1  christos 		}
    460      1.1  christos 	}
    461      1.1  christos 
    462      1.1  christos 	/* isc_lex_destroy() will close all open streams */
    463      1.1  christos 	if (lctx->lex != NULL && !lctx->keep_lex)
    464      1.1  christos 		isc_lex_destroy(&lctx->lex);
    465      1.1  christos 
    466      1.1  christos 	if (lctx->task != NULL)
    467      1.1  christos 		isc_task_detach(&lctx->task);
    468  1.1.1.2  christos 	isc_mutex_destroy(&lctx->lock);
    469      1.1  christos 	mctx = NULL;
    470      1.1  christos 	isc_mem_attach(lctx->mctx, &mctx);
    471      1.1  christos 	isc_mem_detach(&lctx->mctx);
    472      1.1  christos 	isc_mem_put(mctx, lctx, sizeof(*lctx));
    473      1.1  christos 	isc_mem_detach(&mctx);
    474      1.1  christos }
    475      1.1  christos 
    476      1.1  christos static isc_result_t
    477      1.1  christos incctx_create(isc_mem_t *mctx, dns_name_t *origin, dns_incctx_t **ictxp) {
    478      1.1  christos 	dns_incctx_t *ictx;
    479      1.1  christos 	isc_region_t r;
    480      1.1  christos 	int i;
    481      1.1  christos 
    482      1.1  christos 	ictx = isc_mem_get(mctx, sizeof(*ictx));
    483      1.1  christos 	if (ictx == NULL)
    484      1.1  christos 		return (ISC_R_NOMEMORY);
    485      1.1  christos 
    486      1.1  christos 	for (i = 0; i < NBUFS; i++) {
    487      1.1  christos 		dns_fixedname_init(&ictx->fixed[i]);
    488  1.1.1.2  christos 		ictx->in_use[i] = false;
    489      1.1  christos 	}
    490      1.1  christos 
    491      1.1  christos 	ictx->origin_in_use = 0;
    492      1.1  christos 	ictx->origin = dns_fixedname_name(&ictx->fixed[ictx->origin_in_use]);
    493  1.1.1.2  christos 	ictx->in_use[ictx->origin_in_use] = true;
    494      1.1  christos 	dns_name_toregion(origin, &r);
    495      1.1  christos 	dns_name_fromregion(ictx->origin, &r);
    496      1.1  christos 
    497      1.1  christos 	ictx->glue = NULL;
    498      1.1  christos 	ictx->current = NULL;
    499      1.1  christos 	ictx->glue_in_use = -1;
    500      1.1  christos 	ictx->current_in_use = -1;
    501      1.1  christos 	ictx->parent = NULL;
    502  1.1.1.2  christos 	ictx->drop = false;
    503      1.1  christos 	ictx->glue_line = 0;
    504      1.1  christos 	ictx->current_line = 0;
    505  1.1.1.2  christos 	ictx->origin_changed = true;
    506      1.1  christos 
    507      1.1  christos 	*ictxp = ictx;
    508      1.1  christos 	return (ISC_R_SUCCESS);
    509      1.1  christos }
    510      1.1  christos 
    511      1.1  christos static isc_result_t
    512      1.1  christos loadctx_create(dns_masterformat_t format, isc_mem_t *mctx,
    513  1.1.1.2  christos 	       unsigned int options, uint32_t resign, dns_name_t *top,
    514      1.1  christos 	       dns_rdataclass_t zclass, dns_name_t *origin,
    515      1.1  christos 	       dns_rdatacallbacks_t *callbacks, isc_task_t *task,
    516      1.1  christos 	       dns_loaddonefunc_t done, void *done_arg,
    517      1.1  christos 	       dns_masterincludecb_t include_cb, void *include_arg,
    518      1.1  christos 	       isc_lex_t *lex, dns_loadctx_t **lctxp)
    519      1.1  christos {
    520      1.1  christos 	dns_loadctx_t *lctx;
    521      1.1  christos 	isc_result_t result;
    522      1.1  christos 	isc_region_t r;
    523      1.1  christos 	isc_lexspecials_t specials;
    524      1.1  christos 
    525      1.1  christos 	REQUIRE(lctxp != NULL && *lctxp == NULL);
    526      1.1  christos 	REQUIRE(callbacks != NULL);
    527      1.1  christos 	REQUIRE(callbacks->add != NULL);
    528      1.1  christos 	REQUIRE(callbacks->error != NULL);
    529      1.1  christos 	REQUIRE(callbacks->warn != NULL);
    530      1.1  christos 	REQUIRE(mctx != NULL);
    531      1.1  christos 	REQUIRE(dns_name_isabsolute(top));
    532      1.1  christos 	REQUIRE(dns_name_isabsolute(origin));
    533      1.1  christos 	REQUIRE((task == NULL && done == NULL) ||
    534      1.1  christos 		(task != NULL && done != NULL));
    535      1.1  christos 
    536      1.1  christos 	lctx = isc_mem_get(mctx, sizeof(*lctx));
    537      1.1  christos 	if (lctx == NULL)
    538      1.1  christos 		return (ISC_R_NOMEMORY);
    539  1.1.1.2  christos 	isc_mutex_init(&lctx->lock);
    540      1.1  christos 
    541      1.1  christos 	lctx->inc = NULL;
    542      1.1  christos 	result = incctx_create(mctx, origin, &lctx->inc);
    543      1.1  christos 	if (result != ISC_R_SUCCESS)
    544      1.1  christos 		goto cleanup_ctx;
    545      1.1  christos 
    546      1.1  christos 	lctx->maxttl = 0;
    547      1.1  christos 
    548      1.1  christos 	lctx->format = format;
    549      1.1  christos 	switch (format) {
    550      1.1  christos 	case dns_masterformat_text:
    551      1.1  christos 		lctx->openfile = openfile_text;
    552      1.1  christos 		lctx->load = load_text;
    553      1.1  christos 		break;
    554      1.1  christos 	case dns_masterformat_raw:
    555      1.1  christos 		lctx->openfile = openfile_raw;
    556      1.1  christos 		lctx->load = load_raw;
    557      1.1  christos 		break;
    558      1.1  christos 	case dns_masterformat_map:
    559      1.1  christos 		lctx->openfile = openfile_map;
    560      1.1  christos 		lctx->load = load_map;
    561      1.1  christos 		break;
    562  1.1.1.2  christos 	default:
    563  1.1.1.2  christos 		INSIST(0);
    564  1.1.1.2  christos 		ISC_UNREACHABLE();
    565      1.1  christos 	}
    566      1.1  christos 
    567      1.1  christos 	if (lex != NULL) {
    568      1.1  christos 		lctx->lex = lex;
    569  1.1.1.2  christos 		lctx->keep_lex = true;
    570      1.1  christos 	} else {
    571      1.1  christos 		lctx->lex = NULL;
    572      1.1  christos 		result = isc_lex_create(mctx, TOKENSIZ, &lctx->lex);
    573      1.1  christos 		if (result != ISC_R_SUCCESS)
    574      1.1  christos 			goto cleanup_inc;
    575  1.1.1.2  christos 		lctx->keep_lex = false;
    576  1.1.1.3  christos 		/*
    577  1.1.1.3  christos 		 * If specials change update dns_test_rdatafromstring()
    578  1.1.1.3  christos 		 * in lib/dns/tests/dnstest.c.
    579  1.1.1.3  christos 		 */
    580      1.1  christos 		memset(specials, 0, sizeof(specials));
    581      1.1  christos 		specials[0] = 1;
    582      1.1  christos 		specials['('] = 1;
    583      1.1  christos 		specials[')'] = 1;
    584      1.1  christos 		specials['"'] = 1;
    585      1.1  christos 		isc_lex_setspecials(lctx->lex, specials);
    586      1.1  christos 		isc_lex_setcomments(lctx->lex, ISC_LEXCOMMENT_DNSMASTERFILE);
    587      1.1  christos 	}
    588      1.1  christos 
    589  1.1.1.2  christos 	lctx->ttl_known = ((options & DNS_MASTER_NOTTL) != 0);
    590      1.1  christos 	lctx->ttl = 0;
    591      1.1  christos 	lctx->default_ttl_known = lctx->ttl_known;
    592      1.1  christos 	lctx->default_ttl = 0;
    593  1.1.1.2  christos 	lctx->warn_1035 = true;	/* XXX Argument? */
    594  1.1.1.2  christos 	lctx->warn_tcr = true;	/* XXX Argument? */
    595  1.1.1.2  christos 	lctx->warn_sigexpired = true;	/* XXX Argument? */
    596      1.1  christos 	lctx->options = options;
    597  1.1.1.2  christos 	lctx->seen_include = false;
    598      1.1  christos 	lctx->zclass = zclass;
    599      1.1  christos 	lctx->resign = resign;
    600      1.1  christos 	lctx->result = ISC_R_SUCCESS;
    601      1.1  christos 	lctx->include_cb = include_cb;
    602      1.1  christos 	lctx->include_arg = include_arg;
    603      1.1  christos 	isc_stdtime_get(&lctx->now);
    604      1.1  christos 
    605      1.1  christos 	lctx->top = dns_fixedname_initname(&lctx->fixed_top);
    606      1.1  christos 	dns_name_toregion(top, &r);
    607      1.1  christos 	dns_name_fromregion(lctx->top, &r);
    608      1.1  christos 
    609      1.1  christos 	lctx->f = NULL;
    610  1.1.1.2  christos 	lctx->first = true;
    611      1.1  christos 	dns_master_initrawheader(&lctx->header);
    612      1.1  christos 
    613      1.1  christos 	lctx->loop_cnt = (done != NULL) ? 100 : 0;
    614      1.1  christos 	lctx->callbacks = callbacks;
    615      1.1  christos 	lctx->task = NULL;
    616      1.1  christos 	if (task != NULL)
    617      1.1  christos 		isc_task_attach(task, &lctx->task);
    618      1.1  christos 	lctx->done = done;
    619      1.1  christos 	lctx->done_arg = done_arg;
    620  1.1.1.2  christos 	lctx->canceled = false;
    621      1.1  christos 	lctx->mctx = NULL;
    622      1.1  christos 	isc_mem_attach(mctx, &lctx->mctx);
    623      1.1  christos 	lctx->references = 1;			/* Implicit attach. */
    624      1.1  christos 	lctx->magic = DNS_LCTX_MAGIC;
    625      1.1  christos 	*lctxp = lctx;
    626      1.1  christos 	return (ISC_R_SUCCESS);
    627      1.1  christos 
    628      1.1  christos  cleanup_inc:
    629      1.1  christos 	incctx_destroy(mctx, lctx->inc);
    630      1.1  christos  cleanup_ctx:
    631      1.1  christos 	isc_mem_put(mctx, lctx, sizeof(*lctx));
    632      1.1  christos 	return (result);
    633      1.1  christos }
    634      1.1  christos 
    635      1.1  christos static const char *hex = "0123456789abcdef0123456789ABCDEF";
    636      1.1  christos 
    637      1.1  christos /*%
    638      1.1  christos  * Convert value into a nibble sequence from least significant to most
    639      1.1  christos  * significant nibble.  Zero fill upper most significant nibbles if
    640      1.1  christos  * required to make the width.
    641      1.1  christos  *
    642      1.1  christos  * Returns the number of characters that should have been written without
    643      1.1  christos  * counting the terminating NUL.
    644      1.1  christos  */
    645      1.1  christos static unsigned int
    646      1.1  christos nibbles(char *numbuf, size_t length, unsigned int width, char mode, int value) {
    647      1.1  christos 	unsigned int count = 0;
    648      1.1  christos 
    649      1.1  christos 	/*
    650      1.1  christos 	 * This reserve space for the NUL string terminator.
    651      1.1  christos 	 */
    652      1.1  christos 	if (length > 0U) {
    653      1.1  christos 		*numbuf = '\0';
    654      1.1  christos 		length--;
    655      1.1  christos 	}
    656      1.1  christos 	do {
    657      1.1  christos 		char val = hex[(value & 0x0f) + ((mode == 'n') ? 0 : 16)];
    658      1.1  christos 		value >>= 4;
    659      1.1  christos 		if (length > 0U) {
    660      1.1  christos 			*numbuf++ = val;
    661      1.1  christos 			*numbuf = '\0';
    662      1.1  christos 			length--;
    663      1.1  christos 		}
    664      1.1  christos 		if (width > 0)
    665      1.1  christos 			width--;
    666      1.1  christos 		count++;
    667      1.1  christos 		/*
    668      1.1  christos 		 * If width is non zero then we need to add a label seperator.
    669      1.1  christos 		 * If value is non zero then we need to add another label and
    670      1.1  christos 		 * that requires a label seperator.
    671      1.1  christos 		 */
    672      1.1  christos 		if (width > 0 || value != 0) {
    673      1.1  christos 			if (length > 0U) {
    674      1.1  christos 				*numbuf++ = '.';
    675      1.1  christos 				*numbuf = '\0';
    676      1.1  christos 				length--;
    677      1.1  christos 			}
    678      1.1  christos 			if (width > 0)
    679      1.1  christos 				width--;
    680      1.1  christos 			count++;
    681      1.1  christos 		}
    682      1.1  christos 	} while (value != 0 || width > 0);
    683      1.1  christos 	return (count);
    684      1.1  christos }
    685      1.1  christos 
    686      1.1  christos static isc_result_t
    687      1.1  christos genname(char *name, int it, char *buffer, size_t length) {
    688      1.1  christos 	char fmt[sizeof("%04000000000d")];
    689      1.1  christos 	char numbuf[128];
    690      1.1  christos 	char *cp;
    691      1.1  christos 	char mode[2];
    692      1.1  christos 	int delta = 0;
    693      1.1  christos 	isc_textregion_t r;
    694      1.1  christos 	unsigned int n;
    695      1.1  christos 	unsigned int width;
    696  1.1.1.2  christos 	bool nibblemode;
    697      1.1  christos 
    698      1.1  christos 	r.base = buffer;
    699      1.1  christos 	r.length = (unsigned int)length;
    700      1.1  christos 
    701      1.1  christos 	while (*name != '\0') {
    702      1.1  christos 		if (*name == '$') {
    703      1.1  christos 			name++;
    704      1.1  christos 			if (*name == '$') {
    705      1.1  christos 				if (r.length == 0)
    706      1.1  christos 					return (ISC_R_NOSPACE);
    707      1.1  christos 				r.base[0] = *name++;
    708      1.1  christos 				isc_textregion_consume(&r, 1);
    709      1.1  christos 				continue;
    710      1.1  christos 			}
    711  1.1.1.2  christos 			nibblemode = false;
    712      1.1  christos 			strlcpy(fmt, "%d", sizeof(fmt));
    713      1.1  christos 			/* Get format specifier. */
    714      1.1  christos 			if (*name == '{' ) {
    715      1.1  christos 				n = sscanf(name, "{%d,%u,%1[doxXnN]}",
    716      1.1  christos 					   &delta, &width, mode);
    717      1.1  christos 				switch (n) {
    718      1.1  christos 				case 1:
    719      1.1  christos 					break;
    720      1.1  christos 				case 2:
    721      1.1  christos 					n = snprintf(fmt, sizeof(fmt),
    722      1.1  christos 						     "%%0%ud", width);
    723      1.1  christos 					break;
    724      1.1  christos 				case 3:
    725      1.1  christos 					if (mode[0] == 'n' || mode[0] == 'N')
    726  1.1.1.2  christos 						nibblemode = true;
    727      1.1  christos 					n = snprintf(fmt, sizeof(fmt),
    728      1.1  christos 						     "%%0%u%c", width, mode[0]);
    729      1.1  christos 					break;
    730      1.1  christos 				default:
    731      1.1  christos 					return (DNS_R_SYNTAX);
    732      1.1  christos 				}
    733      1.1  christos 				if (n >= sizeof(fmt))
    734      1.1  christos 					return (ISC_R_NOSPACE);
    735      1.1  christos 				/* Skip past closing brace. */
    736      1.1  christos 				while (*name != '\0' && *name++ != '}')
    737      1.1  christos 					continue;
    738      1.1  christos 			}
    739      1.1  christos 			if (nibblemode)
    740      1.1  christos 				n = nibbles(numbuf, sizeof(numbuf), width,
    741      1.1  christos 					    mode[0], it + delta);
    742      1.1  christos 			else
    743      1.1  christos 				n = snprintf(numbuf, sizeof(numbuf), fmt,
    744      1.1  christos 					     it + delta);
    745      1.1  christos 			if (n >= sizeof(numbuf))
    746      1.1  christos 				return (ISC_R_NOSPACE);
    747      1.1  christos 			cp = numbuf;
    748      1.1  christos 			while (*cp != '\0') {
    749      1.1  christos 				if (r.length == 0)
    750      1.1  christos 					return (ISC_R_NOSPACE);
    751      1.1  christos 				r.base[0] = *cp++;
    752      1.1  christos 				isc_textregion_consume(&r, 1);
    753      1.1  christos 			}
    754      1.1  christos 		} else if (*name == '\\') {
    755      1.1  christos 			if (r.length == 0)
    756      1.1  christos 				return (ISC_R_NOSPACE);
    757      1.1  christos 			r.base[0] = *name++;
    758      1.1  christos 			isc_textregion_consume(&r, 1);
    759      1.1  christos 			if (*name == '\0')
    760      1.1  christos 				continue;
    761      1.1  christos 			if (r.length == 0)
    762      1.1  christos 				return (ISC_R_NOSPACE);
    763      1.1  christos 			r.base[0] = *name++;
    764      1.1  christos 			isc_textregion_consume(&r, 1);
    765      1.1  christos 		} else {
    766      1.1  christos 			if (r.length == 0)
    767      1.1  christos 				return (ISC_R_NOSPACE);
    768      1.1  christos 			r.base[0] = *name++;
    769      1.1  christos 			isc_textregion_consume(&r, 1);
    770      1.1  christos 		}
    771      1.1  christos 	}
    772      1.1  christos 	if (r.length == 0)
    773      1.1  christos 		return (ISC_R_NOSPACE);
    774      1.1  christos 	r.base[0] = '\0';
    775      1.1  christos 	return (ISC_R_SUCCESS);
    776      1.1  christos }
    777      1.1  christos 
    778      1.1  christos static isc_result_t
    779      1.1  christos generate(dns_loadctx_t *lctx, char *range, char *lhs, char *gtype, char *rhs,
    780      1.1  christos 	 const char *source, unsigned int line)
    781      1.1  christos {
    782      1.1  christos 	char *target_mem = NULL;
    783      1.1  christos 	char *lhsbuf = NULL;
    784      1.1  christos 	char *rhsbuf = NULL;
    785      1.1  christos 	dns_fixedname_t ownerfixed;
    786      1.1  christos 	dns_name_t *owner;
    787      1.1  christos 	dns_rdata_t rdata = DNS_RDATA_INIT;
    788      1.1  christos 	dns_rdatacallbacks_t *callbacks;
    789      1.1  christos 	dns_rdatalist_t rdatalist;
    790      1.1  christos 	dns_rdatatype_t type;
    791      1.1  christos 	rdatalist_head_t head;
    792      1.1  christos 	int target_size = MINTSIZ;	/* only one rdata at a time */
    793      1.1  christos 	isc_buffer_t buffer;
    794      1.1  christos 	isc_buffer_t target;
    795      1.1  christos 	isc_result_t result;
    796      1.1  christos 	isc_textregion_t r;
    797      1.1  christos 	int i, n, start, stop, step = 0;
    798      1.1  christos 	dns_incctx_t *ictx;
    799      1.1  christos 	char dummy[2];
    800      1.1  christos 
    801      1.1  christos 	ictx = lctx->inc;
    802      1.1  christos 	callbacks = lctx->callbacks;
    803      1.1  christos 	owner = dns_fixedname_initname(&ownerfixed);
    804      1.1  christos 	ISC_LIST_INIT(head);
    805      1.1  christos 
    806      1.1  christos 	target_mem = isc_mem_get(lctx->mctx, target_size);
    807      1.1  christos 	rhsbuf = isc_mem_get(lctx->mctx, DNS_MASTER_RHS);
    808      1.1  christos 	lhsbuf = isc_mem_get(lctx->mctx, DNS_MASTER_LHS);
    809      1.1  christos 	if (target_mem == NULL || rhsbuf == NULL || lhsbuf == NULL) {
    810      1.1  christos 		result = ISC_R_NOMEMORY;
    811      1.1  christos 		goto error_cleanup;
    812      1.1  christos 	}
    813      1.1  christos 	isc_buffer_init(&target, target_mem, target_size);
    814      1.1  christos 
    815      1.1  christos 	n = sscanf(range, "%d-%d%1[/]%d", &start, &stop, dummy, &step);
    816      1.1  christos 	if ((n != 2 && n != 4) || (start < 0) || (stop < 0) ||
    817      1.1  christos 	     (n == 4 && step < 1) || (stop < start))
    818      1.1  christos 	{
    819      1.1  christos 	       (*callbacks->error)(callbacks,
    820      1.1  christos 				  "%s: %s:%lu: invalid range '%s'",
    821      1.1  christos 				  "$GENERATE", source, line, range);
    822      1.1  christos 		result = DNS_R_SYNTAX;
    823      1.1  christos 		goto insist_cleanup;
    824      1.1  christos 	}
    825      1.1  christos 	if (n == 2)
    826      1.1  christos 		step = 1;
    827      1.1  christos 
    828      1.1  christos 	/*
    829      1.1  christos 	 * Get type.
    830      1.1  christos 	 */
    831      1.1  christos 	r.base = gtype;
    832      1.1  christos 	r.length = strlen(gtype);
    833      1.1  christos 	result = dns_rdatatype_fromtext(&type, &r);
    834      1.1  christos 	if (result != ISC_R_SUCCESS) {
    835      1.1  christos 		(*callbacks->error)(callbacks,
    836      1.1  christos 				   "%s: %s:%lu: unknown RR type '%s'",
    837      1.1  christos 				   "$GENERATE", source, line, gtype);
    838      1.1  christos 		goto insist_cleanup;
    839      1.1  christos 	}
    840      1.1  christos 
    841      1.1  christos 	/*
    842      1.1  christos 	 * RFC2930: TKEY and TSIG are not allowed to be loaded
    843      1.1  christos 	 * from master files.
    844      1.1  christos 	 */
    845      1.1  christos 	if ((lctx->options & DNS_MASTER_ZONE) != 0 &&
    846      1.1  christos 	    (lctx->options & DNS_MASTER_SLAVE) == 0 &&
    847      1.1  christos 	    dns_rdatatype_ismeta(type))
    848      1.1  christos 	{
    849      1.1  christos 		(*callbacks->error)(callbacks,
    850      1.1  christos 				   "%s: %s:%lu: meta RR type '%s'",
    851      1.1  christos 				   "$GENERATE",
    852      1.1  christos 				   source, line, gtype);
    853      1.1  christos 		result = DNS_R_METATYPE;
    854      1.1  christos 		goto insist_cleanup;
    855      1.1  christos 	}
    856      1.1  christos 
    857      1.1  christos 	for (i = start; i <= stop; i += step) {
    858      1.1  christos 		result = genname(lhs, i, lhsbuf, DNS_MASTER_LHS);
    859      1.1  christos 		if (result != ISC_R_SUCCESS)
    860      1.1  christos 			goto error_cleanup;
    861      1.1  christos 		result = genname(rhs, i, rhsbuf, DNS_MASTER_RHS);
    862      1.1  christos 		if (result != ISC_R_SUCCESS)
    863      1.1  christos 			goto error_cleanup;
    864      1.1  christos 
    865      1.1  christos 		isc_buffer_init(&buffer, lhsbuf, strlen(lhsbuf));
    866      1.1  christos 		isc_buffer_add(&buffer, strlen(lhsbuf));
    867      1.1  christos 		isc_buffer_setactive(&buffer, strlen(lhsbuf));
    868      1.1  christos 		result = dns_name_fromtext(owner, &buffer, ictx->origin,
    869      1.1  christos 					   0, NULL);
    870      1.1  christos 		if (result != ISC_R_SUCCESS)
    871      1.1  christos 			goto error_cleanup;
    872      1.1  christos 
    873      1.1  christos 		if ((lctx->options & DNS_MASTER_ZONE) != 0 &&
    874      1.1  christos 		    (lctx->options & DNS_MASTER_SLAVE) == 0 &&
    875      1.1  christos 		    (lctx->options & DNS_MASTER_KEY) == 0 &&
    876      1.1  christos 		    !dns_name_issubdomain(owner, lctx->top))
    877      1.1  christos 		{
    878      1.1  christos 			char namebuf[DNS_NAME_FORMATSIZE];
    879      1.1  christos 			dns_name_format(owner, namebuf, sizeof(namebuf));
    880      1.1  christos 			/*
    881      1.1  christos 			 * Ignore out-of-zone data.
    882      1.1  christos 			 */
    883      1.1  christos 			(*callbacks->warn)(callbacks,
    884      1.1  christos 					   "%s:%lu: "
    885      1.1  christos 					   "ignoring out-of-zone data (%s)",
    886      1.1  christos 					   source, line, namebuf);
    887      1.1  christos 			continue;
    888      1.1  christos 		}
    889      1.1  christos 
    890      1.1  christos 		isc_buffer_init(&buffer, rhsbuf, strlen(rhsbuf));
    891      1.1  christos 		isc_buffer_add(&buffer, strlen(rhsbuf));
    892      1.1  christos 		isc_buffer_setactive(&buffer, strlen(rhsbuf));
    893      1.1  christos 
    894      1.1  christos 		result = isc_lex_openbuffer(lctx->lex, &buffer);
    895      1.1  christos 		if (result != ISC_R_SUCCESS)
    896      1.1  christos 			goto error_cleanup;
    897      1.1  christos 
    898      1.1  christos 		isc_buffer_init(&target, target_mem, target_size);
    899      1.1  christos 		result = dns_rdata_fromtext(&rdata, lctx->zclass, type,
    900      1.1  christos 					    lctx->lex, ictx->origin, 0,
    901      1.1  christos 					    lctx->mctx, &target, callbacks);
    902      1.1  christos 		RUNTIME_CHECK(isc_lex_close(lctx->lex) == ISC_R_SUCCESS);
    903      1.1  christos 		if (result != ISC_R_SUCCESS)
    904      1.1  christos 			goto error_cleanup;
    905      1.1  christos 
    906      1.1  christos 		dns_rdatalist_init(&rdatalist);
    907      1.1  christos 		rdatalist.type = type;
    908      1.1  christos 		rdatalist.rdclass = lctx->zclass;
    909      1.1  christos 		rdatalist.ttl = lctx->ttl;
    910      1.1  christos 		ISC_LIST_PREPEND(head, &rdatalist, link);
    911      1.1  christos 		ISC_LIST_APPEND(rdatalist.rdata, &rdata, link);
    912      1.1  christos 		result = commit(callbacks, lctx, &head, owner, source, line);
    913      1.1  christos 		ISC_LIST_UNLINK(rdatalist.rdata, &rdata, link);
    914      1.1  christos 		if (result != ISC_R_SUCCESS)
    915      1.1  christos 			goto error_cleanup;
    916      1.1  christos 		dns_rdata_reset(&rdata);
    917      1.1  christos 	}
    918      1.1  christos 	result = ISC_R_SUCCESS;
    919      1.1  christos 	goto cleanup;
    920      1.1  christos 
    921      1.1  christos  error_cleanup:
    922      1.1  christos 	if (result == ISC_R_NOMEMORY)
    923      1.1  christos 		(*callbacks->error)(callbacks, "$GENERATE: %s",
    924      1.1  christos 				    dns_result_totext(result));
    925      1.1  christos 	else
    926      1.1  christos 		(*callbacks->error)(callbacks, "$GENERATE: %s:%lu: %s",
    927      1.1  christos 				    source, line, dns_result_totext(result));
    928      1.1  christos 
    929      1.1  christos  insist_cleanup:
    930      1.1  christos 	INSIST(result != ISC_R_SUCCESS);
    931      1.1  christos 
    932      1.1  christos  cleanup:
    933      1.1  christos 	if (target_mem != NULL)
    934      1.1  christos 		isc_mem_put(lctx->mctx, target_mem, target_size);
    935      1.1  christos 	if (lhsbuf != NULL)
    936      1.1  christos 		isc_mem_put(lctx->mctx, lhsbuf, DNS_MASTER_LHS);
    937      1.1  christos 	if (rhsbuf != NULL)
    938      1.1  christos 		isc_mem_put(lctx->mctx, rhsbuf, DNS_MASTER_RHS);
    939      1.1  christos 	return (result);
    940      1.1  christos }
    941      1.1  christos 
    942      1.1  christos static void
    943      1.1  christos limit_ttl(dns_rdatacallbacks_t *callbacks, const char *source,
    944  1.1.1.2  christos 	  unsigned int line, uint32_t *ttlp)
    945      1.1  christos {
    946      1.1  christos 	if (*ttlp > 0x7fffffffUL) {
    947      1.1  christos 		(callbacks->warn)(callbacks,
    948      1.1  christos 				  "%s: %s:%lu: "
    949      1.1  christos 				  "$TTL %lu > MAXTTL, "
    950      1.1  christos 				  "setting $TTL to 0",
    951      1.1  christos 				  "dns_master_load",
    952      1.1  christos 				  source, line,
    953      1.1  christos 				  *ttlp);
    954      1.1  christos 		*ttlp = 0;
    955      1.1  christos 	}
    956      1.1  christos }
    957      1.1  christos 
    958      1.1  christos static isc_result_t
    959      1.1  christos check_ns(dns_loadctx_t *lctx, isc_token_t *token, const char *source,
    960      1.1  christos 	 unsigned long line)
    961      1.1  christos {
    962      1.1  christos 	char *tmp = NULL;
    963      1.1  christos 	isc_result_t result = ISC_R_SUCCESS;
    964      1.1  christos 	void (*callback)(struct dns_rdatacallbacks *, const char *, ...);
    965      1.1  christos 
    966      1.1  christos 	if ((lctx->options & DNS_MASTER_FATALNS) != 0)
    967      1.1  christos 		callback = lctx->callbacks->error;
    968      1.1  christos 	else
    969      1.1  christos 		callback = lctx->callbacks->warn;
    970      1.1  christos 
    971      1.1  christos 	if (token->type == isc_tokentype_string) {
    972      1.1  christos 		struct in_addr addr;
    973      1.1  christos 		struct in6_addr addr6;
    974      1.1  christos 
    975      1.1  christos 		tmp = isc_mem_strdup(lctx->mctx, DNS_AS_STR(*token));
    976      1.1  christos 		if (tmp == NULL)
    977      1.1  christos 			return (ISC_R_NOMEMORY);
    978      1.1  christos 		/*
    979      1.1  christos 		 * Catch both "1.2.3.4" and "1.2.3.4."
    980      1.1  christos 		 */
    981      1.1  christos 		if (tmp[strlen(tmp) - 1] == '.')
    982      1.1  christos 			tmp[strlen(tmp) - 1] = '\0';
    983  1.1.1.2  christos 		if (inet_pton(AF_INET, tmp, &addr) == 1 ||
    984      1.1  christos 		    inet_pton(AF_INET6, tmp, &addr6) == 1)
    985      1.1  christos 			result = DNS_R_NSISADDRESS;
    986      1.1  christos 	}
    987      1.1  christos 	if (result != ISC_R_SUCCESS)
    988      1.1  christos 		(*callback)(lctx->callbacks, "%s:%lu: NS record '%s' "
    989      1.1  christos 			    "appears to be an address",
    990      1.1  christos 			    source, line, DNS_AS_STR(*token));
    991      1.1  christos 	if (tmp != NULL)
    992      1.1  christos 		isc_mem_free(lctx->mctx, tmp);
    993      1.1  christos 	return (result);
    994      1.1  christos }
    995      1.1  christos 
    996      1.1  christos static void
    997      1.1  christos check_wildcard(dns_incctx_t *ictx, const char *source, unsigned long line,
    998      1.1  christos 	       dns_rdatacallbacks_t *callbacks)
    999      1.1  christos {
   1000      1.1  christos 	dns_name_t *name;
   1001      1.1  christos 
   1002      1.1  christos 	name = (ictx->glue != NULL) ? ictx->glue : ictx->current;
   1003      1.1  christos 	if (dns_name_internalwildcard(name)) {
   1004      1.1  christos 		char namebuf[DNS_NAME_FORMATSIZE];
   1005      1.1  christos 
   1006      1.1  christos 		dns_name_format(name, namebuf, sizeof(namebuf));
   1007      1.1  christos 		(*callbacks->warn)(callbacks, "%s:%lu: warning: ownername "
   1008      1.1  christos 				   "'%s' contains an non-terminal wildcard",
   1009      1.1  christos 				   source, line, namebuf);
   1010      1.1  christos 	}
   1011      1.1  christos }
   1012      1.1  christos 
   1013      1.1  christos static isc_result_t
   1014      1.1  christos openfile_text(dns_loadctx_t *lctx, const char *master_file) {
   1015      1.1  christos 	return (isc_lex_openfile(lctx->lex, master_file));
   1016      1.1  christos }
   1017      1.1  christos 
   1018      1.1  christos static int
   1019      1.1  christos find_free_name(dns_incctx_t *incctx) {
   1020      1.1  christos 	int i;
   1021      1.1  christos 
   1022      1.1  christos 	for (i = 0; i < (NBUFS - 1); i++) {
   1023      1.1  christos 		if (!incctx->in_use[i]) {
   1024      1.1  christos 			break;
   1025      1.1  christos 		}
   1026      1.1  christos 	}
   1027      1.1  christos 	INSIST(!incctx->in_use[i]);
   1028      1.1  christos 	return (i);
   1029      1.1  christos }
   1030      1.1  christos 
   1031      1.1  christos static isc_result_t
   1032      1.1  christos load_text(dns_loadctx_t *lctx) {
   1033      1.1  christos 	dns_rdataclass_t rdclass;
   1034      1.1  christos 	dns_rdatatype_t type, covers;
   1035  1.1.1.2  christos 	uint32_t ttl_offset = 0;
   1036      1.1  christos 	dns_name_t *new_name;
   1037  1.1.1.2  christos 	bool current_has_delegation = false;
   1038  1.1.1.2  christos 	bool done = false;
   1039  1.1.1.2  christos 	bool finish_origin = false;
   1040  1.1.1.2  christos 	bool finish_include = false;
   1041  1.1.1.2  christos 	bool read_till_eol = false;
   1042  1.1.1.2  christos 	bool initialws;
   1043      1.1  christos 	char *include_file = NULL;
   1044      1.1  christos 	isc_token_t token;
   1045      1.1  christos 	isc_result_t result = ISC_R_UNEXPECTED;
   1046      1.1  christos 	rdatalist_head_t glue_list;
   1047      1.1  christos 	rdatalist_head_t current_list;
   1048      1.1  christos 	dns_rdatalist_t *this;
   1049      1.1  christos 	dns_rdatalist_t *rdatalist = NULL;
   1050      1.1  christos 	dns_rdatalist_t *new_rdatalist;
   1051      1.1  christos 	int rdlcount = 0;
   1052      1.1  christos 	int rdlcount_save = 0;
   1053      1.1  christos 	int rdatalist_size = 0;
   1054      1.1  christos 	isc_buffer_t buffer;
   1055      1.1  christos 	isc_buffer_t target;
   1056      1.1  christos 	isc_buffer_t target_ft;
   1057      1.1  christos 	isc_buffer_t target_save;
   1058      1.1  christos 	dns_rdata_t *rdata = NULL;
   1059      1.1  christos 	dns_rdata_t *new_rdata;
   1060      1.1  christos 	int rdcount = 0;
   1061      1.1  christos 	int rdcount_save = 0;
   1062      1.1  christos 	int rdata_size = 0;
   1063      1.1  christos 	unsigned char *target_mem = NULL;
   1064      1.1  christos 	int target_size = TSIZ;
   1065      1.1  christos 	int new_in_use;
   1066      1.1  christos 	unsigned int loop_cnt = 0;
   1067      1.1  christos 	isc_mem_t *mctx;
   1068      1.1  christos 	dns_rdatacallbacks_t *callbacks;
   1069      1.1  christos 	dns_incctx_t *ictx;
   1070      1.1  christos 	char *range = NULL;
   1071      1.1  christos 	char *lhs = NULL;
   1072      1.1  christos 	char *gtype = NULL;
   1073      1.1  christos 	char *rhs = NULL;
   1074      1.1  christos 	const char *source = "";
   1075      1.1  christos 	unsigned long line = 0;
   1076  1.1.1.2  christos 	bool explicit_ttl;
   1077      1.1  christos 	char classname1[DNS_RDATACLASS_FORMATSIZE];
   1078      1.1  christos 	char classname2[DNS_RDATACLASS_FORMATSIZE];
   1079      1.1  christos 	unsigned int options = 0;
   1080      1.1  christos 
   1081      1.1  christos 	REQUIRE(DNS_LCTX_VALID(lctx));
   1082      1.1  christos 	callbacks = lctx->callbacks;
   1083      1.1  christos 	mctx = lctx->mctx;
   1084      1.1  christos 	ictx = lctx->inc;
   1085      1.1  christos 
   1086      1.1  christos 	ISC_LIST_INIT(glue_list);
   1087      1.1  christos 	ISC_LIST_INIT(current_list);
   1088      1.1  christos 
   1089      1.1  christos 
   1090      1.1  christos 	/*
   1091      1.1  christos 	 * Allocate target_size of buffer space.  This is greater than twice
   1092      1.1  christos 	 * the maximum individual RR data size.
   1093      1.1  christos 	 */
   1094      1.1  christos 	target_mem = isc_mem_get(mctx, target_size);
   1095      1.1  christos 	if (target_mem == NULL) {
   1096      1.1  christos 		result = ISC_R_NOMEMORY;
   1097      1.1  christos 		goto log_and_cleanup;
   1098      1.1  christos 	}
   1099      1.1  christos 	isc_buffer_init(&target, target_mem, target_size);
   1100      1.1  christos 	target_save = target;
   1101      1.1  christos 
   1102      1.1  christos 	if ((lctx->options & DNS_MASTER_CHECKNAMES) != 0)
   1103      1.1  christos 		options |= DNS_RDATA_CHECKNAMES;
   1104      1.1  christos 	if ((lctx->options & DNS_MASTER_CHECKNAMESFAIL) != 0)
   1105      1.1  christos 		options |= DNS_RDATA_CHECKNAMESFAIL;
   1106      1.1  christos 	if ((lctx->options & DNS_MASTER_CHECKMX) != 0)
   1107      1.1  christos 		options |= DNS_RDATA_CHECKMX;
   1108      1.1  christos 	if ((lctx->options & DNS_MASTER_CHECKMXFAIL) != 0)
   1109      1.1  christos 		options |= DNS_RDATA_CHECKMXFAIL;
   1110      1.1  christos 	source = isc_lex_getsourcename(lctx->lex);
   1111      1.1  christos 	do {
   1112  1.1.1.2  christos 		initialws = false;
   1113      1.1  christos 		line = isc_lex_getsourceline(lctx->lex);
   1114      1.1  christos 		GETTOKEN(lctx->lex, ISC_LEXOPT_INITIALWS | ISC_LEXOPT_QSTRING,
   1115  1.1.1.2  christos 			 &token, true);
   1116      1.1  christos 		line = isc_lex_getsourceline(lctx->lex);
   1117      1.1  christos 
   1118      1.1  christos 		if (token.type == isc_tokentype_eof) {
   1119      1.1  christos 			if (read_till_eol)
   1120      1.1  christos 				WARNUNEXPECTEDEOF(lctx->lex);
   1121      1.1  christos 			/* Pop the include stack? */
   1122      1.1  christos 			if (ictx->parent != NULL) {
   1123      1.1  christos 				COMMITALL;
   1124      1.1  christos 				lctx->inc = ictx->parent;
   1125      1.1  christos 				ictx->parent = NULL;
   1126      1.1  christos 				incctx_destroy(lctx->mctx, ictx);
   1127      1.1  christos 				RUNTIME_CHECK(isc_lex_close(lctx->lex) == ISC_R_SUCCESS);
   1128      1.1  christos 				line = isc_lex_getsourceline(lctx->lex);
   1129      1.1  christos 				POST(line);
   1130      1.1  christos 				source = isc_lex_getsourcename(lctx->lex);
   1131      1.1  christos 				ictx = lctx->inc;
   1132      1.1  christos 				continue;
   1133      1.1  christos 			}
   1134  1.1.1.2  christos 			done = true;
   1135      1.1  christos 			continue;
   1136      1.1  christos 		}
   1137      1.1  christos 
   1138      1.1  christos 		if (token.type == isc_tokentype_eol) {
   1139  1.1.1.2  christos 			read_till_eol = false;
   1140      1.1  christos 			continue;		/* blank line */
   1141      1.1  christos 		}
   1142      1.1  christos 
   1143      1.1  christos 		if (read_till_eol)
   1144      1.1  christos 			continue;
   1145      1.1  christos 
   1146      1.1  christos 		if (token.type == isc_tokentype_initialws) {
   1147      1.1  christos 			/*
   1148      1.1  christos 			 * Still working on the same name.
   1149      1.1  christos 			 */
   1150  1.1.1.2  christos 			initialws = true;
   1151      1.1  christos 		} else if (token.type == isc_tokentype_string ||
   1152      1.1  christos 			   token.type == isc_tokentype_qstring) {
   1153      1.1  christos 
   1154      1.1  christos 			/*
   1155      1.1  christos 			 * "$" Support.
   1156      1.1  christos 			 *
   1157      1.1  christos 			 * "$ORIGIN" and "$INCLUDE" can both take domain names.
   1158      1.1  christos 			 * The processing of "$ORIGIN" and "$INCLUDE" extends
   1159      1.1  christos 			 * across the normal domain name processing.
   1160      1.1  christos 			 */
   1161      1.1  christos 
   1162      1.1  christos 			if (strcasecmp(DNS_AS_STR(token), "$ORIGIN") == 0) {
   1163  1.1.1.2  christos 				GETTOKEN(lctx->lex, 0, &token, false);
   1164  1.1.1.2  christos 				finish_origin = true;
   1165      1.1  christos 			} else if (strcasecmp(DNS_AS_STR(token),
   1166      1.1  christos 					      "$TTL") == 0) {
   1167  1.1.1.2  christos 				GETTOKENERR(lctx->lex, 0, &token, false,
   1168      1.1  christos 					    lctx->ttl = 0;
   1169  1.1.1.2  christos 					    lctx->default_ttl_known = true;);
   1170      1.1  christos 				result =
   1171      1.1  christos 				   dns_ttl_fromtext(&token.value.as_textregion,
   1172      1.1  christos 						    &lctx->ttl);
   1173      1.1  christos 				if (MANYERRS(lctx, result)) {
   1174      1.1  christos 					SETRESULT(lctx, result);
   1175      1.1  christos 					lctx->ttl = 0;
   1176      1.1  christos 				} else if (result != ISC_R_SUCCESS)
   1177      1.1  christos 					goto insist_and_cleanup;
   1178      1.1  christos 				limit_ttl(callbacks, source, line, &lctx->ttl);
   1179      1.1  christos 				lctx->default_ttl = lctx->ttl;
   1180  1.1.1.2  christos 				lctx->default_ttl_known = true;
   1181      1.1  christos 				EXPECTEOL;
   1182      1.1  christos 				continue;
   1183      1.1  christos 			} else if (strcasecmp(DNS_AS_STR(token),
   1184      1.1  christos 					      "$INCLUDE") == 0) {
   1185      1.1  christos 				COMMITALL;
   1186      1.1  christos 				if ((lctx->options & DNS_MASTER_NOINCLUDE)
   1187      1.1  christos 				    != 0)
   1188      1.1  christos 				{
   1189      1.1  christos 					(callbacks->error)(callbacks,
   1190      1.1  christos 					   "%s: %s:%lu: $INCLUDE not allowed",
   1191      1.1  christos 					   "dns_master_load",
   1192      1.1  christos 					   source, line);
   1193      1.1  christos 					result = DNS_R_REFUSED;
   1194      1.1  christos 					goto insist_and_cleanup;
   1195      1.1  christos 				}
   1196      1.1  christos 				if (ttl_offset != 0) {
   1197      1.1  christos 					(callbacks->error)(callbacks,
   1198      1.1  christos 					   "%s: %s:%lu: $INCLUDE "
   1199      1.1  christos 					   "may not be used with $DATE",
   1200      1.1  christos 					   "dns_master_load",
   1201      1.1  christos 					   source, line);
   1202      1.1  christos 					result = DNS_R_SYNTAX;
   1203      1.1  christos 					goto insist_and_cleanup;
   1204      1.1  christos 				}
   1205      1.1  christos 				GETTOKEN(lctx->lex, ISC_LEXOPT_QSTRING, &token,
   1206  1.1.1.2  christos 					 false);
   1207      1.1  christos 				if (include_file != NULL)
   1208      1.1  christos 					isc_mem_free(mctx, include_file);
   1209      1.1  christos 				include_file = isc_mem_strdup(mctx,
   1210      1.1  christos 							   DNS_AS_STR(token));
   1211      1.1  christos 				if (include_file == NULL) {
   1212      1.1  christos 					result = ISC_R_NOMEMORY;
   1213      1.1  christos 					goto log_and_cleanup;
   1214      1.1  christos 				}
   1215  1.1.1.2  christos 				GETTOKEN(lctx->lex, 0, &token, true);
   1216      1.1  christos 
   1217      1.1  christos 				if (token.type == isc_tokentype_eol ||
   1218      1.1  christos 				    token.type == isc_tokentype_eof) {
   1219      1.1  christos 					if (token.type == isc_tokentype_eof)
   1220      1.1  christos 						WARNUNEXPECTEDEOF(lctx->lex);
   1221      1.1  christos 					/*
   1222      1.1  christos 					 * No origin field.
   1223      1.1  christos 					 */
   1224      1.1  christos 					result = pushfile(include_file,
   1225      1.1  christos 							  ictx->origin, lctx);
   1226      1.1  christos 					if (MANYERRS(lctx, result)) {
   1227      1.1  christos 						SETRESULT(lctx, result);
   1228      1.1  christos 						LOGITFILE(result, include_file);
   1229      1.1  christos 						continue;
   1230      1.1  christos 					} else if (result != ISC_R_SUCCESS) {
   1231      1.1  christos 						LOGITFILE(result, include_file);
   1232      1.1  christos 						goto insist_and_cleanup;
   1233      1.1  christos 					}
   1234      1.1  christos 					ictx = lctx->inc;
   1235      1.1  christos 					source =
   1236      1.1  christos 					       isc_lex_getsourcename(lctx->lex);
   1237      1.1  christos 					line = isc_lex_getsourceline(lctx->lex);
   1238      1.1  christos 					POST(line);
   1239      1.1  christos 					continue;
   1240      1.1  christos 				}
   1241      1.1  christos 				/*
   1242      1.1  christos 				 * There is an origin field.  Fall through
   1243      1.1  christos 				 * to domain name processing code and do
   1244      1.1  christos 				 * the actual inclusion later.
   1245      1.1  christos 				 */
   1246  1.1.1.2  christos 				finish_include = true;
   1247      1.1  christos 			} else if (strcasecmp(DNS_AS_STR(token),
   1248      1.1  christos 					      "$DATE") == 0) {
   1249  1.1.1.2  christos 				int64_t dump_time64;
   1250      1.1  christos 				isc_stdtime_t dump_time, current_time;
   1251  1.1.1.2  christos 				GETTOKEN(lctx->lex, 0, &token, false);
   1252      1.1  christos 				isc_stdtime_get(&current_time);
   1253      1.1  christos 				result = dns_time64_fromtext(DNS_AS_STR(token),
   1254      1.1  christos 							     &dump_time64);
   1255      1.1  christos 				if (MANYERRS(lctx, result)) {
   1256      1.1  christos 					SETRESULT(lctx, result);
   1257      1.1  christos 					LOGIT(result);
   1258      1.1  christos 					dump_time64 = 0;
   1259      1.1  christos 				} else if (result != ISC_R_SUCCESS)
   1260      1.1  christos 					goto log_and_cleanup;
   1261      1.1  christos 				dump_time = (isc_stdtime_t)dump_time64;
   1262      1.1  christos 				if (dump_time != dump_time64) {
   1263      1.1  christos 					UNEXPECTED_ERROR(__FILE__, __LINE__,
   1264      1.1  christos 					 "%s: %s:%lu: $DATE outside epoch",
   1265      1.1  christos 					 "dns_master_load", source, line);
   1266      1.1  christos 					result = ISC_R_UNEXPECTED;
   1267      1.1  christos 					goto insist_and_cleanup;
   1268      1.1  christos 				}
   1269      1.1  christos 				if (dump_time > current_time) {
   1270      1.1  christos 					UNEXPECTED_ERROR(__FILE__, __LINE__,
   1271      1.1  christos 					"%s: %s:%lu: "
   1272      1.1  christos 					"$DATE in future, using current date",
   1273      1.1  christos 					"dns_master_load", source, line);
   1274      1.1  christos 					dump_time = current_time;
   1275      1.1  christos 				}
   1276      1.1  christos 				ttl_offset = current_time - dump_time;
   1277      1.1  christos 				EXPECTEOL;
   1278      1.1  christos 				continue;
   1279      1.1  christos 			} else if (strcasecmp(DNS_AS_STR(token),
   1280      1.1  christos 					      "$GENERATE") == 0) {
   1281      1.1  christos 				/*
   1282      1.1  christos 				 * Lazy cleanup.
   1283      1.1  christos 				 */
   1284      1.1  christos 				if (range != NULL)
   1285      1.1  christos 					isc_mem_free(mctx, range);
   1286      1.1  christos 				if (lhs != NULL)
   1287      1.1  christos 					isc_mem_free(mctx, lhs);
   1288      1.1  christos 				if (gtype != NULL)
   1289      1.1  christos 					isc_mem_free(mctx, gtype);
   1290      1.1  christos 				if (rhs != NULL)
   1291      1.1  christos 					isc_mem_free(mctx, rhs);
   1292      1.1  christos 				range = lhs = gtype = rhs = NULL;
   1293      1.1  christos 				/* RANGE */
   1294  1.1.1.2  christos 				GETTOKEN(lctx->lex, 0, &token, false);
   1295      1.1  christos 				range = isc_mem_strdup(mctx,
   1296      1.1  christos 						     DNS_AS_STR(token));
   1297      1.1  christos 				if (range == NULL) {
   1298      1.1  christos 					result = ISC_R_NOMEMORY;
   1299      1.1  christos 					goto log_and_cleanup;
   1300      1.1  christos 				}
   1301      1.1  christos 				/* LHS */
   1302  1.1.1.2  christos 				GETTOKEN(lctx->lex, 0, &token, false);
   1303      1.1  christos 				lhs = isc_mem_strdup(mctx, DNS_AS_STR(token));
   1304      1.1  christos 				if (lhs == NULL) {
   1305      1.1  christos 					result = ISC_R_NOMEMORY;
   1306      1.1  christos 					goto log_and_cleanup;
   1307      1.1  christos 				}
   1308      1.1  christos 				rdclass = 0;
   1309  1.1.1.2  christos 				explicit_ttl = false;
   1310      1.1  christos 				/* CLASS? */
   1311  1.1.1.2  christos 				GETTOKEN(lctx->lex, 0, &token, false);
   1312      1.1  christos 				if (dns_rdataclass_fromtext(&rdclass,
   1313      1.1  christos 					    &token.value.as_textregion)
   1314      1.1  christos 						== ISC_R_SUCCESS) {
   1315      1.1  christos 					GETTOKEN(lctx->lex, 0, &token,
   1316  1.1.1.2  christos 						 false);
   1317      1.1  christos 				}
   1318      1.1  christos 				/* TTL? */
   1319      1.1  christos 				if (dns_ttl_fromtext(&token.value.as_textregion,
   1320      1.1  christos 						     &lctx->ttl)
   1321      1.1  christos 						== ISC_R_SUCCESS) {
   1322      1.1  christos 					limit_ttl(callbacks, source, line,
   1323      1.1  christos 						  &lctx->ttl);
   1324  1.1.1.2  christos 					lctx->ttl_known = true;
   1325  1.1.1.2  christos 					explicit_ttl = true;
   1326      1.1  christos 					GETTOKEN(lctx->lex, 0, &token,
   1327  1.1.1.2  christos 						 false);
   1328      1.1  christos 				}
   1329      1.1  christos 				/* CLASS? */
   1330      1.1  christos 				if (rdclass == 0 &&
   1331      1.1  christos 				    dns_rdataclass_fromtext(&rdclass,
   1332      1.1  christos 						    &token.value.as_textregion)
   1333      1.1  christos 						== ISC_R_SUCCESS)
   1334      1.1  christos 					GETTOKEN(lctx->lex, 0, &token,
   1335  1.1.1.2  christos 						 false);
   1336      1.1  christos 				/* TYPE */
   1337      1.1  christos 				gtype = isc_mem_strdup(mctx,
   1338      1.1  christos 						       DNS_AS_STR(token));
   1339      1.1  christos 				if (gtype == NULL) {
   1340      1.1  christos 					result = ISC_R_NOMEMORY;
   1341      1.1  christos 					goto log_and_cleanup;
   1342      1.1  christos 				}
   1343      1.1  christos 				/* RHS */
   1344      1.1  christos 				GETTOKEN(lctx->lex, ISC_LEXOPT_QSTRING,
   1345  1.1.1.2  christos 					 &token, false);
   1346      1.1  christos 				rhs = isc_mem_strdup(mctx, DNS_AS_STR(token));
   1347      1.1  christos 				if (rhs == NULL) {
   1348      1.1  christos 					result = ISC_R_NOMEMORY;
   1349      1.1  christos 					goto log_and_cleanup;
   1350      1.1  christos 				}
   1351      1.1  christos 				if (!lctx->ttl_known &&
   1352      1.1  christos 				    !lctx->default_ttl_known) {
   1353      1.1  christos 					(*callbacks->error)(callbacks,
   1354      1.1  christos 					    "%s: %s:%lu: no TTL specified",
   1355      1.1  christos 					    "dns_master_load", source, line);
   1356      1.1  christos 					result = DNS_R_NOTTL;
   1357      1.1  christos 					if (MANYERRS(lctx, result)) {
   1358      1.1  christos 						SETRESULT(lctx, result);
   1359      1.1  christos 						lctx->ttl = 0;
   1360      1.1  christos 					} else {
   1361      1.1  christos 						goto insist_and_cleanup;
   1362      1.1  christos 					}
   1363      1.1  christos 				} else if (!explicit_ttl &&
   1364      1.1  christos 					   lctx->default_ttl_known) {
   1365      1.1  christos 					lctx->ttl = lctx->default_ttl;
   1366      1.1  christos 				}
   1367      1.1  christos 				/*
   1368      1.1  christos 				 * If the class specified does not match the
   1369      1.1  christos 				 * zone's class print out a error message and
   1370      1.1  christos 				 * exit.
   1371      1.1  christos 				 */
   1372      1.1  christos 				if (rdclass != 0 && rdclass != lctx->zclass) {
   1373      1.1  christos 					goto bad_class;
   1374      1.1  christos 				}
   1375      1.1  christos 				result = generate(lctx, range, lhs, gtype, rhs,
   1376      1.1  christos 						  source, line);
   1377      1.1  christos 				if (MANYERRS(lctx, result)) {
   1378      1.1  christos 					SETRESULT(lctx, result);
   1379      1.1  christos 				} else if (result != ISC_R_SUCCESS)
   1380      1.1  christos 					goto insist_and_cleanup;
   1381      1.1  christos 				EXPECTEOL;
   1382      1.1  christos 				continue;
   1383      1.1  christos 			} else if (strncasecmp(DNS_AS_STR(token),
   1384      1.1  christos 					       "$", 1) == 0) {
   1385      1.1  christos 				(callbacks->error)(callbacks,
   1386      1.1  christos 					   "%s: %s:%lu: "
   1387      1.1  christos 					   "unknown $ directive '%s'",
   1388      1.1  christos 					   "dns_master_load", source, line,
   1389      1.1  christos 					   DNS_AS_STR(token));
   1390      1.1  christos 				result = DNS_R_SYNTAX;
   1391      1.1  christos 				if (MANYERRS(lctx, result)) {
   1392      1.1  christos 					SETRESULT(lctx, result);
   1393      1.1  christos 				} else {
   1394      1.1  christos 					goto insist_and_cleanup;
   1395      1.1  christos 				}
   1396      1.1  christos 			}
   1397      1.1  christos 
   1398      1.1  christos 			/*
   1399      1.1  christos 			 * Normal processing resumes.
   1400      1.1  christos 			 */
   1401      1.1  christos 			new_in_use = find_free_name(ictx);
   1402      1.1  christos 			new_name =
   1403      1.1  christos 			      dns_fixedname_initname(&ictx->fixed[new_in_use]);
   1404      1.1  christos 			isc_buffer_init(&buffer, token.value.as_region.base,
   1405      1.1  christos 					token.value.as_region.length);
   1406      1.1  christos 			isc_buffer_add(&buffer, token.value.as_region.length);
   1407      1.1  christos 			isc_buffer_setactive(&buffer,
   1408      1.1  christos 					     token.value.as_region.length);
   1409      1.1  christos 			result = dns_name_fromtext(new_name, &buffer,
   1410      1.1  christos 					  ictx->origin, 0, NULL);
   1411      1.1  christos 			if (MANYERRS(lctx, result)) {
   1412      1.1  christos 				SETRESULT(lctx, result);
   1413      1.1  christos 				LOGIT(result);
   1414  1.1.1.2  christos 				read_till_eol = true;
   1415      1.1  christos 				continue;
   1416      1.1  christos 			} else if (result != ISC_R_SUCCESS)
   1417      1.1  christos 				goto log_and_cleanup;
   1418      1.1  christos 
   1419      1.1  christos 			/*
   1420      1.1  christos 			 * Finish $ORIGIN / $INCLUDE processing if required.
   1421      1.1  christos 			 */
   1422      1.1  christos 			if (finish_origin) {
   1423      1.1  christos 				if (ictx->origin_in_use != -1)
   1424      1.1  christos 					ictx->in_use[ictx->origin_in_use] =
   1425  1.1.1.2  christos 						false;
   1426      1.1  christos 				ictx->origin_in_use = new_in_use;
   1427  1.1.1.2  christos 				ictx->in_use[ictx->origin_in_use] = true;
   1428      1.1  christos 				ictx->origin = new_name;
   1429  1.1.1.2  christos 				ictx->origin_changed = true;
   1430  1.1.1.2  christos 				finish_origin = false;
   1431      1.1  christos 				EXPECTEOL;
   1432      1.1  christos 				continue;
   1433      1.1  christos 			}
   1434      1.1  christos 			if (finish_include) {
   1435  1.1.1.2  christos 				finish_include = false;
   1436      1.1  christos 				EXPECTEOL;
   1437      1.1  christos 				result = pushfile(include_file, new_name, lctx);
   1438      1.1  christos 				if (MANYERRS(lctx, result)) {
   1439      1.1  christos 					SETRESULT(lctx, result);
   1440      1.1  christos 					LOGITFILE(result, include_file);
   1441      1.1  christos 					continue;
   1442      1.1  christos 				} else if (result != ISC_R_SUCCESS) {
   1443      1.1  christos 					LOGITFILE(result, include_file);
   1444      1.1  christos 					goto insist_and_cleanup;
   1445      1.1  christos 				}
   1446      1.1  christos 				ictx = lctx->inc;
   1447  1.1.1.2  christos 				ictx->origin_changed = true;
   1448      1.1  christos 				source = isc_lex_getsourcename(lctx->lex);
   1449      1.1  christos 				line = isc_lex_getsourceline(lctx->lex);
   1450      1.1  christos 				POST(line);
   1451      1.1  christos 				continue;
   1452      1.1  christos 			}
   1453      1.1  christos 
   1454      1.1  christos 			/*
   1455      1.1  christos 			 * "$" Processing Finished
   1456      1.1  christos 			 */
   1457      1.1  christos 
   1458      1.1  christos 			/*
   1459      1.1  christos 			 * If we are processing glue and the new name does
   1460      1.1  christos 			 * not match the current glue name, commit the glue
   1461      1.1  christos 			 * and pop stacks leaving us in 'normal' processing
   1462      1.1  christos 			 * state.  Linked lists are undone by commit().
   1463      1.1  christos 			 */
   1464      1.1  christos 			if (ictx->glue != NULL &&
   1465  1.1.1.2  christos 			    !dns_name_caseequal(ictx->glue, new_name)) {
   1466      1.1  christos 				result = commit(callbacks, lctx, &glue_list,
   1467      1.1  christos 						ictx->glue, source,
   1468      1.1  christos 						ictx->glue_line);
   1469      1.1  christos 				if (MANYERRS(lctx, result)) {
   1470      1.1  christos 					SETRESULT(lctx, result);
   1471      1.1  christos 				} else if (result != ISC_R_SUCCESS)
   1472      1.1  christos 					goto insist_and_cleanup;
   1473      1.1  christos 				if (ictx->glue_in_use != -1)
   1474      1.1  christos 					ictx->in_use[ictx->glue_in_use] =
   1475  1.1.1.2  christos 						false;
   1476      1.1  christos 				ictx->glue_in_use = -1;
   1477      1.1  christos 				ictx->glue = NULL;
   1478      1.1  christos 				rdcount = rdcount_save;
   1479      1.1  christos 				rdlcount = rdlcount_save;
   1480      1.1  christos 				target = target_save;
   1481      1.1  christos 			}
   1482      1.1  christos 
   1483      1.1  christos 			/*
   1484      1.1  christos 			 * If we are in 'normal' processing state and the new
   1485      1.1  christos 			 * name does not match the current name, see if the
   1486      1.1  christos 			 * new name is for glue and treat it as such,
   1487      1.1  christos 			 * otherwise we have a new name so commit what we
   1488      1.1  christos 			 * have.
   1489      1.1  christos 			 */
   1490      1.1  christos 			if ((ictx->glue == NULL) && (ictx->current == NULL ||
   1491  1.1.1.2  christos 			    !dns_name_caseequal(ictx->current, new_name))) {
   1492      1.1  christos 				if (current_has_delegation &&
   1493      1.1  christos 					is_glue(&current_list, new_name)) {
   1494      1.1  christos 					rdcount_save = rdcount;
   1495      1.1  christos 					rdlcount_save = rdlcount;
   1496      1.1  christos 					target_save = target;
   1497      1.1  christos 					ictx->glue = new_name;
   1498      1.1  christos 					ictx->glue_in_use = new_in_use;
   1499      1.1  christos 					ictx->in_use[ictx->glue_in_use] =
   1500  1.1.1.2  christos 						true;
   1501      1.1  christos 				} else {
   1502      1.1  christos 					result = commit(callbacks, lctx,
   1503      1.1  christos 							&current_list,
   1504      1.1  christos 							ictx->current,
   1505      1.1  christos 							source,
   1506      1.1  christos 							ictx->current_line);
   1507      1.1  christos 					if (MANYERRS(lctx, result)) {
   1508      1.1  christos 						SETRESULT(lctx, result);
   1509      1.1  christos 					} else if (result != ISC_R_SUCCESS)
   1510      1.1  christos 						goto insist_and_cleanup;
   1511      1.1  christos 					rdcount = 0;
   1512      1.1  christos 					rdlcount = 0;
   1513      1.1  christos 					if (ictx->current_in_use != -1)
   1514      1.1  christos 					    ictx->in_use[ictx->current_in_use] =
   1515  1.1.1.2  christos 						false;
   1516      1.1  christos 					ictx->current_in_use = new_in_use;
   1517      1.1  christos 					ictx->in_use[ictx->current_in_use] =
   1518  1.1.1.2  christos 						true;
   1519      1.1  christos 					ictx->current = new_name;
   1520  1.1.1.2  christos 					current_has_delegation = false;
   1521      1.1  christos 					isc_buffer_init(&target, target_mem,
   1522      1.1  christos 							target_size);
   1523      1.1  christos 				}
   1524      1.1  christos 				/*
   1525      1.1  christos 				 * Check for internal wildcards.
   1526      1.1  christos 				 */
   1527      1.1  christos 				if ((lctx->options & DNS_MASTER_CHECKWILDCARD)
   1528      1.1  christos 						 != 0)
   1529      1.1  christos 					check_wildcard(ictx, source, line,
   1530      1.1  christos 						       callbacks);
   1531      1.1  christos 
   1532      1.1  christos 			}
   1533      1.1  christos 			if ((lctx->options & DNS_MASTER_ZONE) != 0 &&
   1534      1.1  christos 			    (lctx->options & DNS_MASTER_SLAVE) == 0 &&
   1535      1.1  christos 			    (lctx->options & DNS_MASTER_KEY) == 0 &&
   1536      1.1  christos 			    !dns_name_issubdomain(new_name, lctx->top))
   1537      1.1  christos 			{
   1538      1.1  christos 				char namebuf[DNS_NAME_FORMATSIZE];
   1539      1.1  christos 				dns_name_format(new_name, namebuf,
   1540      1.1  christos 						sizeof(namebuf));
   1541      1.1  christos 				/*
   1542      1.1  christos 				 * Ignore out-of-zone data.
   1543      1.1  christos 				 */
   1544      1.1  christos 				(*callbacks->warn)(callbacks,
   1545      1.1  christos 				       "%s:%lu: "
   1546      1.1  christos 				       "ignoring out-of-zone data (%s)",
   1547      1.1  christos 				       source, line, namebuf);
   1548  1.1.1.2  christos 				ictx->drop = true;
   1549      1.1  christos 			} else
   1550  1.1.1.2  christos 				ictx->drop = false;
   1551      1.1  christos 		} else {
   1552      1.1  christos 			UNEXPECTED_ERROR(__FILE__, __LINE__,
   1553      1.1  christos 					 "%s:%lu: isc_lex_gettoken() returned "
   1554      1.1  christos 					 "unexpected token type (%d)",
   1555      1.1  christos 					 source, line, token.type);
   1556      1.1  christos 			result = ISC_R_UNEXPECTED;
   1557      1.1  christos 			if (MANYERRS(lctx, result)) {
   1558      1.1  christos 				SETRESULT(lctx, result);
   1559      1.1  christos 				LOGIT(result);
   1560      1.1  christos 				continue;
   1561      1.1  christos 			} else {
   1562      1.1  christos 				goto insist_and_cleanup;
   1563      1.1  christos 			}
   1564      1.1  christos 		}
   1565      1.1  christos 
   1566      1.1  christos 		/*
   1567      1.1  christos 		 * Find TTL, class and type.  Both TTL and class are optional
   1568      1.1  christos 		 * and may occur in any order if they exist. TTL and class
   1569      1.1  christos 		 * come before type which must exist.
   1570      1.1  christos 		 *
   1571      1.1  christos 		 * [<TTL>] [<class>] <type> <RDATA>
   1572      1.1  christos 		 * [<class>] [<TTL>] <type> <RDATA>
   1573      1.1  christos 		 */
   1574      1.1  christos 
   1575      1.1  christos 		type = 0;
   1576      1.1  christos 		rdclass = 0;
   1577      1.1  christos 
   1578      1.1  christos 		GETTOKEN(lctx->lex, 0, &token, initialws);
   1579      1.1  christos 
   1580      1.1  christos 		if (initialws) {
   1581      1.1  christos 			if (token.type == isc_tokentype_eol) {
   1582  1.1.1.2  christos 				read_till_eol = false;
   1583      1.1  christos 				continue;		/* blank line */
   1584      1.1  christos 			}
   1585      1.1  christos 
   1586      1.1  christos 			if (token.type == isc_tokentype_eof) {
   1587      1.1  christos 				WARNUNEXPECTEDEOF(lctx->lex);
   1588  1.1.1.2  christos 				read_till_eol = false;
   1589      1.1  christos 				isc_lex_ungettoken(lctx->lex, &token);
   1590      1.1  christos 				continue;
   1591      1.1  christos 			}
   1592      1.1  christos 
   1593      1.1  christos 			if (ictx->current == NULL) {
   1594      1.1  christos 				(*callbacks->error)(callbacks,
   1595      1.1  christos 					"%s:%lu: no current owner name",
   1596      1.1  christos 					source, line);
   1597      1.1  christos 				result = DNS_R_NOOWNER;
   1598      1.1  christos 				if (MANYERRS(lctx, result)) {
   1599      1.1  christos 					SETRESULT(lctx, result);
   1600  1.1.1.2  christos 					read_till_eol = true;
   1601      1.1  christos 					continue;
   1602      1.1  christos 				} else {
   1603      1.1  christos 					goto insist_and_cleanup;
   1604      1.1  christos 				}
   1605      1.1  christos 			}
   1606      1.1  christos 
   1607      1.1  christos 			if (ictx->origin_changed) {
   1608      1.1  christos 				char cbuf[DNS_NAME_FORMATSIZE];
   1609      1.1  christos 				char obuf[DNS_NAME_FORMATSIZE];
   1610      1.1  christos 				dns_name_format(ictx->current, cbuf,
   1611      1.1  christos 						sizeof(cbuf));
   1612      1.1  christos 				dns_name_format(ictx->origin, obuf,
   1613      1.1  christos 						sizeof(obuf));
   1614      1.1  christos 				(*callbacks->warn)(callbacks,
   1615      1.1  christos 					"%s:%lu: record with inherited "
   1616      1.1  christos 					"owner (%s) immediately after "
   1617      1.1  christos 					"$ORIGIN (%s)", source, line,
   1618      1.1  christos 					cbuf, obuf);
   1619      1.1  christos 			}
   1620      1.1  christos 		}
   1621      1.1  christos 
   1622  1.1.1.2  christos 		ictx->origin_changed = false;
   1623      1.1  christos 
   1624      1.1  christos 		if (dns_rdataclass_fromtext(&rdclass,
   1625      1.1  christos 					    &token.value.as_textregion)
   1626      1.1  christos 				== ISC_R_SUCCESS)
   1627  1.1.1.2  christos 			GETTOKEN(lctx->lex, 0, &token, false);
   1628      1.1  christos 
   1629  1.1.1.2  christos 		explicit_ttl = false;
   1630      1.1  christos 		result = dns_ttl_fromtext(&token.value.as_textregion,
   1631      1.1  christos 					  &lctx->ttl);
   1632      1.1  christos 		if (result == ISC_R_SUCCESS) {
   1633      1.1  christos 			limit_ttl(callbacks, source, line, &lctx->ttl);
   1634  1.1.1.2  christos 			explicit_ttl = true;
   1635  1.1.1.2  christos 			lctx->ttl_known = true;
   1636  1.1.1.2  christos 			GETTOKEN(lctx->lex, 0, &token, false);
   1637      1.1  christos 		}
   1638      1.1  christos 
   1639      1.1  christos 		if (token.type != isc_tokentype_string) {
   1640      1.1  christos 			UNEXPECTED_ERROR(__FILE__, __LINE__,
   1641      1.1  christos 			"isc_lex_gettoken() returned unexpected token type");
   1642      1.1  christos 			result = ISC_R_UNEXPECTED;
   1643      1.1  christos 			if (MANYERRS(lctx, result)) {
   1644      1.1  christos 				SETRESULT(lctx, result);
   1645  1.1.1.2  christos 				read_till_eol = true;
   1646      1.1  christos 				continue;
   1647      1.1  christos 			} else {
   1648      1.1  christos 				goto insist_and_cleanup;
   1649      1.1  christos 			}
   1650      1.1  christos 		}
   1651      1.1  christos 
   1652      1.1  christos 		if (rdclass == 0 &&
   1653      1.1  christos 		    dns_rdataclass_fromtext(&rdclass,
   1654      1.1  christos 					    &token.value.as_textregion)
   1655      1.1  christos 				== ISC_R_SUCCESS)
   1656  1.1.1.2  christos 			GETTOKEN(lctx->lex, 0, &token, false);
   1657      1.1  christos 
   1658      1.1  christos 		if (token.type != isc_tokentype_string) {
   1659      1.1  christos 			UNEXPECTED_ERROR(__FILE__, __LINE__,
   1660      1.1  christos 			"isc_lex_gettoken() returned unexpected token type");
   1661      1.1  christos 			result = ISC_R_UNEXPECTED;
   1662      1.1  christos 			if (MANYERRS(lctx, result)) {
   1663      1.1  christos 				SETRESULT(lctx, result);
   1664  1.1.1.2  christos 				read_till_eol = true;
   1665      1.1  christos 				continue;
   1666      1.1  christos 			} else {
   1667      1.1  christos 				goto insist_and_cleanup;
   1668      1.1  christos 			}
   1669      1.1  christos 		}
   1670      1.1  christos 
   1671      1.1  christos 		result = dns_rdatatype_fromtext(&type,
   1672      1.1  christos 						&token.value.as_textregion);
   1673      1.1  christos 		if (result != ISC_R_SUCCESS) {
   1674      1.1  christos 			(*callbacks->warn)(callbacks,
   1675      1.1  christos 				   "%s:%lu: unknown RR type '%.*s'",
   1676      1.1  christos 				   source, line,
   1677      1.1  christos 				   token.value.as_textregion.length,
   1678      1.1  christos 				   token.value.as_textregion.base);
   1679      1.1  christos 			if (MANYERRS(lctx, result)) {
   1680      1.1  christos 				SETRESULT(lctx, result);
   1681  1.1.1.2  christos 				read_till_eol = true;
   1682      1.1  christos 				continue;
   1683      1.1  christos 			} else if (result != ISC_R_SUCCESS)
   1684      1.1  christos 				goto insist_and_cleanup;
   1685      1.1  christos 		}
   1686      1.1  christos 
   1687      1.1  christos 		/*
   1688      1.1  christos 		 * If the class specified does not match the zone's class
   1689      1.1  christos 		 * print out a error message and exit.
   1690      1.1  christos 		 */
   1691      1.1  christos 		if (rdclass != 0 && rdclass != lctx->zclass) {
   1692      1.1  christos   bad_class:
   1693      1.1  christos 
   1694      1.1  christos 			dns_rdataclass_format(rdclass, classname1,
   1695      1.1  christos 					      sizeof(classname1));
   1696      1.1  christos 			dns_rdataclass_format(lctx->zclass, classname2,
   1697      1.1  christos 					      sizeof(classname2));
   1698      1.1  christos 			(*callbacks->error)(callbacks,
   1699      1.1  christos 					    "%s:%lu: class '%s' != "
   1700      1.1  christos 					    "zone class '%s'",
   1701      1.1  christos 					    source, line,
   1702      1.1  christos 					    classname1, classname2);
   1703      1.1  christos 			result = DNS_R_BADCLASS;
   1704      1.1  christos 			if (MANYERRS(lctx, result)) {
   1705      1.1  christos 				SETRESULT(lctx, result);
   1706  1.1.1.2  christos 				read_till_eol = true;
   1707      1.1  christos 				continue;
   1708      1.1  christos 			} else {
   1709      1.1  christos 				goto insist_and_cleanup;
   1710      1.1  christos 			}
   1711      1.1  christos 		}
   1712      1.1  christos 
   1713      1.1  christos 		if (type == dns_rdatatype_ns && ictx->glue == NULL)
   1714  1.1.1.2  christos 			current_has_delegation = true;
   1715      1.1  christos 
   1716      1.1  christos 		/*
   1717      1.1  christos 		 * RFC1123: MD and MF are not allowed to be loaded from
   1718      1.1  christos 		 * master files.
   1719      1.1  christos 		 */
   1720      1.1  christos 		if ((lctx->options & DNS_MASTER_ZONE) != 0 &&
   1721      1.1  christos 		    (lctx->options & DNS_MASTER_SLAVE) == 0 &&
   1722      1.1  christos 		    (type == dns_rdatatype_md || type == dns_rdatatype_mf)) {
   1723  1.1.1.2  christos 			char typebuf[DNS_RDATATYPE_FORMATSIZE];
   1724      1.1  christos 
   1725      1.1  christos 			result = DNS_R_OBSOLETE;
   1726      1.1  christos 
   1727  1.1.1.2  christos 			dns_rdatatype_format(type, typebuf, sizeof(typebuf));
   1728      1.1  christos 			(*callbacks->error)(callbacks,
   1729      1.1  christos 					    "%s:%lu: %s '%s': %s",
   1730      1.1  christos 					    source, line,
   1731  1.1.1.2  christos 					    "type", typebuf,
   1732      1.1  christos 					    dns_result_totext(result));
   1733      1.1  christos 			if (MANYERRS(lctx, result)) {
   1734      1.1  christos 				SETRESULT(lctx, result);
   1735      1.1  christos 			} else
   1736      1.1  christos 				goto insist_and_cleanup;
   1737      1.1  christos 		}
   1738      1.1  christos 
   1739      1.1  christos 		/*
   1740      1.1  christos 		 * RFC2930: TKEY and TSIG are not allowed to be loaded
   1741      1.1  christos 		 * from master files.
   1742      1.1  christos 		 */
   1743      1.1  christos 		if ((lctx->options & DNS_MASTER_ZONE) != 0 &&
   1744      1.1  christos 		    (lctx->options & DNS_MASTER_SLAVE) == 0 &&
   1745      1.1  christos 		    dns_rdatatype_ismeta(type))
   1746      1.1  christos 		{
   1747  1.1.1.2  christos 			char typebuf[DNS_RDATATYPE_FORMATSIZE];
   1748      1.1  christos 
   1749      1.1  christos 			result = DNS_R_METATYPE;
   1750      1.1  christos 
   1751  1.1.1.2  christos 			dns_rdatatype_format(type, typebuf, sizeof(typebuf));
   1752      1.1  christos 			(*callbacks->error)(callbacks,
   1753      1.1  christos 					    "%s:%lu: %s '%s': %s",
   1754      1.1  christos 					    source, line,
   1755  1.1.1.2  christos 					    "type", typebuf,
   1756      1.1  christos 					    dns_result_totext(result));
   1757      1.1  christos 			if (MANYERRS(lctx, result)) {
   1758      1.1  christos 				SETRESULT(lctx, result);
   1759      1.1  christos 			} else
   1760      1.1  christos 				goto insist_and_cleanup;
   1761      1.1  christos 		}
   1762      1.1  christos 
   1763      1.1  christos 		/*
   1764      1.1  christos 		 * Find a rdata structure.
   1765      1.1  christos 		 */
   1766      1.1  christos 		if (rdcount == rdata_size) {
   1767      1.1  christos 			new_rdata = grow_rdata(rdata_size + RDSZ, rdata,
   1768      1.1  christos 					       rdata_size, &current_list,
   1769      1.1  christos 					       &glue_list, mctx);
   1770      1.1  christos 			if (new_rdata == NULL) {
   1771      1.1  christos 				result = ISC_R_NOMEMORY;
   1772      1.1  christos 				goto log_and_cleanup;
   1773      1.1  christos 			}
   1774      1.1  christos 			rdata_size += RDSZ;
   1775      1.1  christos 			rdata = new_rdata;
   1776      1.1  christos 		}
   1777      1.1  christos 
   1778      1.1  christos 		/*
   1779      1.1  christos 		 * Peek at the NS record.
   1780      1.1  christos 		 */
   1781      1.1  christos 		if (type == dns_rdatatype_ns &&
   1782      1.1  christos 		    lctx->zclass == dns_rdataclass_in &&
   1783      1.1  christos 		    (lctx->options & DNS_MASTER_CHECKNS) != 0) {
   1784      1.1  christos 
   1785  1.1.1.2  christos 			GETTOKEN(lctx->lex, 0, &token, false);
   1786      1.1  christos 			result = check_ns(lctx, &token, source, line);
   1787      1.1  christos 			isc_lex_ungettoken(lctx->lex, &token);
   1788      1.1  christos 			if ((lctx->options & DNS_MASTER_FATALNS) != 0) {
   1789      1.1  christos 				if (MANYERRS(lctx, result)) {
   1790      1.1  christos 					SETRESULT(lctx, result);
   1791      1.1  christos 				} else if (result != ISC_R_SUCCESS)
   1792      1.1  christos 					goto insist_and_cleanup;
   1793      1.1  christos 			}
   1794      1.1  christos 		}
   1795      1.1  christos 
   1796      1.1  christos 		/*
   1797      1.1  christos 		 * Check owner name.
   1798      1.1  christos 		 */
   1799      1.1  christos 		options &= ~DNS_RDATA_CHECKREVERSE;
   1800      1.1  christos 		if ((lctx->options & DNS_MASTER_CHECKNAMES) != 0) {
   1801  1.1.1.2  christos 			bool ok;
   1802      1.1  christos 			dns_name_t *name;
   1803      1.1  christos 
   1804      1.1  christos 			name = (ictx->glue != NULL) ? ictx->glue :
   1805      1.1  christos 						      ictx->current;
   1806      1.1  christos 			ok = dns_rdata_checkowner(name, lctx->zclass, type,
   1807  1.1.1.2  christos 						  true);
   1808      1.1  christos 			if (!ok) {
   1809      1.1  christos 				char namebuf[DNS_NAME_FORMATSIZE];
   1810      1.1  christos 				const char *desc;
   1811      1.1  christos 				dns_name_format(name, namebuf, sizeof(namebuf));
   1812      1.1  christos 				result = DNS_R_BADOWNERNAME;
   1813      1.1  christos 				desc = dns_result_totext(result);
   1814      1.1  christos 				if (CHECKNAMESFAIL(lctx->options) ||
   1815      1.1  christos 				    type == dns_rdatatype_nsec3) {
   1816      1.1  christos 					(*callbacks->error)(callbacks,
   1817      1.1  christos 							    "%s:%lu: %s: %s",
   1818      1.1  christos 							    source, line,
   1819      1.1  christos 							    namebuf, desc);
   1820      1.1  christos 					if (MANYERRS(lctx, result)) {
   1821      1.1  christos 						SETRESULT(lctx, result);
   1822      1.1  christos 					} else {
   1823      1.1  christos 						goto cleanup;
   1824      1.1  christos 					}
   1825      1.1  christos 				} else {
   1826      1.1  christos 					(*callbacks->warn)(callbacks,
   1827      1.1  christos 							   "%s:%lu: %s: %s",
   1828      1.1  christos 							   source, line,
   1829      1.1  christos 							   namebuf, desc);
   1830      1.1  christos 				}
   1831      1.1  christos 			}
   1832      1.1  christos 			if (type == dns_rdatatype_ptr &&
   1833      1.1  christos 			    !dns_name_isdnssd(name) &&
   1834      1.1  christos 			    (dns_name_issubdomain(name, &in_addr_arpa) ||
   1835      1.1  christos 			     dns_name_issubdomain(name, &ip6_arpa) ||
   1836      1.1  christos 			     dns_name_issubdomain(name, &ip6_int)))
   1837      1.1  christos 				options |= DNS_RDATA_CHECKREVERSE;
   1838      1.1  christos 		}
   1839      1.1  christos 
   1840      1.1  christos 		/*
   1841      1.1  christos 		 * Read rdata contents.
   1842      1.1  christos 		 */
   1843      1.1  christos 		dns_rdata_init(&rdata[rdcount]);
   1844      1.1  christos 		target_ft = target;
   1845      1.1  christos 		result = dns_rdata_fromtext(&rdata[rdcount], lctx->zclass,
   1846      1.1  christos 					    type, lctx->lex, ictx->origin,
   1847      1.1  christos 					    options, lctx->mctx, &target,
   1848      1.1  christos 					    callbacks);
   1849      1.1  christos 		if (MANYERRS(lctx, result)) {
   1850      1.1  christos 			SETRESULT(lctx, result);
   1851      1.1  christos 			continue;
   1852      1.1  christos 		} else if (result != ISC_R_SUCCESS)
   1853      1.1  christos 			goto insist_and_cleanup;
   1854      1.1  christos 
   1855      1.1  christos 		if (ictx->drop) {
   1856      1.1  christos 			target = target_ft;
   1857      1.1  christos 			continue;
   1858      1.1  christos 		}
   1859      1.1  christos 
   1860      1.1  christos 		if (type == dns_rdatatype_soa &&
   1861      1.1  christos 		    (lctx->options & DNS_MASTER_ZONE) != 0 &&
   1862  1.1.1.2  christos 		    !dns_name_equal(ictx->current, lctx->top)) {
   1863      1.1  christos 			char namebuf[DNS_NAME_FORMATSIZE];
   1864      1.1  christos 			dns_name_format(ictx->current, namebuf,
   1865      1.1  christos 					sizeof(namebuf));
   1866      1.1  christos 			(*callbacks->error)(callbacks, "%s:%lu: SOA "
   1867      1.1  christos 					    "record not at top of zone (%s)",
   1868      1.1  christos 					    source, line, namebuf);
   1869      1.1  christos 			result = DNS_R_NOTZONETOP;
   1870      1.1  christos 			if (MANYERRS(lctx, result)) {
   1871      1.1  christos 				SETRESULT(lctx, result);
   1872  1.1.1.2  christos 				read_till_eol = true;
   1873      1.1  christos 				target = target_ft;
   1874      1.1  christos 				continue;
   1875      1.1  christos 			} else {
   1876      1.1  christos 				goto insist_and_cleanup;
   1877      1.1  christos 			}
   1878      1.1  christos 		}
   1879      1.1  christos 
   1880      1.1  christos 		if (type == dns_rdatatype_rrsig ||
   1881      1.1  christos 		    type == dns_rdatatype_sig)
   1882      1.1  christos 			covers = dns_rdata_covers(&rdata[rdcount]);
   1883      1.1  christos 		else
   1884      1.1  christos 			covers = 0;
   1885      1.1  christos 
   1886      1.1  christos 		if (!lctx->ttl_known && !lctx->default_ttl_known) {
   1887      1.1  christos 			if (type == dns_rdatatype_soa) {
   1888      1.1  christos 				(*callbacks->warn)(callbacks,
   1889      1.1  christos 						   "%s:%lu: no TTL specified; "
   1890      1.1  christos 						   "using SOA MINTTL instead",
   1891      1.1  christos 						   source, line);
   1892      1.1  christos 				lctx->ttl = dns_soa_getminimum(&rdata[rdcount]);
   1893      1.1  christos 				limit_ttl(callbacks, source, line, &lctx->ttl);
   1894      1.1  christos 				lctx->default_ttl = lctx->ttl;
   1895  1.1.1.2  christos 				lctx->default_ttl_known = true;
   1896      1.1  christos 			} else if ((lctx->options & DNS_MASTER_HINT) != 0) {
   1897      1.1  christos 				/*
   1898      1.1  christos 				 * Zero TTL's are fine for hints.
   1899      1.1  christos 				 */
   1900      1.1  christos 				lctx->ttl = 0;
   1901      1.1  christos 				lctx->default_ttl = lctx->ttl;
   1902  1.1.1.2  christos 				lctx->default_ttl_known = true;
   1903      1.1  christos 			} else {
   1904      1.1  christos 				(*callbacks->warn)(callbacks,
   1905      1.1  christos 						   "%s:%lu: no TTL specified; "
   1906      1.1  christos 						   "zone rejected",
   1907      1.1  christos 						   source, line);
   1908      1.1  christos 				result = DNS_R_NOTTL;
   1909      1.1  christos 				if (MANYERRS(lctx, result)) {
   1910      1.1  christos 					SETRESULT(lctx, result);
   1911      1.1  christos 					lctx->ttl = 0;
   1912      1.1  christos 				} else {
   1913      1.1  christos 					goto insist_and_cleanup;
   1914      1.1  christos 				}
   1915      1.1  christos 			}
   1916      1.1  christos 		} else if (!explicit_ttl && lctx->default_ttl_known) {
   1917      1.1  christos 			lctx->ttl = lctx->default_ttl;
   1918      1.1  christos 		} else if (!explicit_ttl && lctx->warn_1035) {
   1919      1.1  christos 			(*callbacks->warn)(callbacks,
   1920      1.1  christos 					   "%s:%lu: "
   1921      1.1  christos 					   "using RFC1035 TTL semantics",
   1922      1.1  christos 					   source, line);
   1923  1.1.1.2  christos 			lctx->warn_1035 = false;
   1924      1.1  christos 		}
   1925      1.1  christos 
   1926      1.1  christos 		if (type == dns_rdatatype_rrsig && lctx->warn_sigexpired) {
   1927      1.1  christos 			dns_rdata_rrsig_t sig;
   1928      1.1  christos 			result = dns_rdata_tostruct(&rdata[rdcount], &sig,
   1929      1.1  christos 						    NULL);
   1930      1.1  christos 			RUNTIME_CHECK(result == ISC_R_SUCCESS);
   1931      1.1  christos 			if (isc_serial_lt(sig.timeexpire, lctx->now)) {
   1932      1.1  christos 				(*callbacks->warn)(callbacks,
   1933      1.1  christos 						   "%s:%lu: "
   1934      1.1  christos 						   "signature has expired",
   1935      1.1  christos 						   source, line);
   1936  1.1.1.2  christos 				lctx->warn_sigexpired = false;
   1937      1.1  christos 			}
   1938      1.1  christos 		}
   1939      1.1  christos 
   1940      1.1  christos 		if ((type == dns_rdatatype_sig || type == dns_rdatatype_nxt) &&
   1941      1.1  christos 		    lctx->warn_tcr && (lctx->options & DNS_MASTER_ZONE) != 0 &&
   1942      1.1  christos 		    (lctx->options & DNS_MASTER_SLAVE) == 0) {
   1943      1.1  christos 			(*callbacks->warn)(callbacks, "%s:%lu: old style DNSSEC "
   1944      1.1  christos 					   " zone detected", source, line);
   1945  1.1.1.2  christos 			lctx->warn_tcr = false;
   1946      1.1  christos 		}
   1947      1.1  christos 
   1948      1.1  christos 		if ((lctx->options & DNS_MASTER_AGETTL) != 0) {
   1949      1.1  christos 			/*
   1950      1.1  christos 			 * Adjust the TTL for $DATE. If the RR has
   1951      1.1  christos 			 * already expired, set its TTL to 0. This
   1952      1.1  christos 			 * should be okay even if the TTL stretching
   1953      1.1  christos 			 * feature is not in effect, because it will
   1954      1.1  christos 			 * just be quickly expired by the cache, and the
   1955      1.1  christos 			 * way this was written before the patch it
   1956      1.1  christos 			 * could potentially add 0 TTLs anyway.
   1957      1.1  christos 			 */
   1958      1.1  christos 			if (lctx->ttl < ttl_offset)
   1959      1.1  christos 				lctx->ttl = 0;
   1960      1.1  christos 			else
   1961      1.1  christos 				lctx->ttl -= ttl_offset;
   1962      1.1  christos 		}
   1963      1.1  christos 
   1964      1.1  christos 		/*
   1965      1.1  christos 		 * Find type in rdatalist.
   1966      1.1  christos 		 * If it does not exist create new one and prepend to list
   1967      1.1  christos 		 * as this will minimise list traversal.
   1968      1.1  christos 		 */
   1969      1.1  christos 		if (ictx->glue != NULL)
   1970      1.1  christos 			this = ISC_LIST_HEAD(glue_list);
   1971      1.1  christos 		else
   1972      1.1  christos 			this = ISC_LIST_HEAD(current_list);
   1973      1.1  christos 
   1974      1.1  christos 		while (this != NULL) {
   1975      1.1  christos 			if (this->type == type && this->covers == covers)
   1976      1.1  christos 				break;
   1977      1.1  christos 			this = ISC_LIST_NEXT(this, link);
   1978      1.1  christos 		}
   1979      1.1  christos 
   1980      1.1  christos 		if (this == NULL) {
   1981      1.1  christos 			if (rdlcount == rdatalist_size) {
   1982      1.1  christos 				new_rdatalist =
   1983      1.1  christos 					grow_rdatalist(rdatalist_size + RDLSZ,
   1984      1.1  christos 						       rdatalist,
   1985      1.1  christos 						       rdatalist_size,
   1986      1.1  christos 						       &current_list,
   1987      1.1  christos 						       &glue_list,
   1988      1.1  christos 						       mctx);
   1989      1.1  christos 				if (new_rdatalist == NULL) {
   1990      1.1  christos 					result = ISC_R_NOMEMORY;
   1991      1.1  christos 					goto log_and_cleanup;
   1992      1.1  christos 				}
   1993      1.1  christos 				rdatalist = new_rdatalist;
   1994      1.1  christos 				rdatalist_size += RDLSZ;
   1995      1.1  christos 			}
   1996      1.1  christos 			this = &rdatalist[rdlcount++];
   1997      1.1  christos 			dns_rdatalist_init(this);
   1998      1.1  christos 			this->type = type;
   1999      1.1  christos 			this->covers = covers;
   2000      1.1  christos 			this->rdclass = lctx->zclass;
   2001      1.1  christos 			this->ttl = lctx->ttl;
   2002      1.1  christos 			if (ictx->glue != NULL)
   2003      1.1  christos 				ISC_LIST_INITANDPREPEND(glue_list, this, link);
   2004      1.1  christos 			else
   2005      1.1  christos 				ISC_LIST_INITANDPREPEND(current_list, this,
   2006      1.1  christos 							link);
   2007      1.1  christos 		} else if (this->ttl != lctx->ttl) {
   2008      1.1  christos 			(*callbacks->warn)(callbacks,
   2009      1.1  christos 					   "%s:%lu: "
   2010      1.1  christos 					   "TTL set to prior TTL (%lu)",
   2011      1.1  christos 					   source, line, this->ttl);
   2012      1.1  christos 			lctx->ttl = this->ttl;
   2013      1.1  christos 		}
   2014      1.1  christos 
   2015      1.1  christos 		if ((lctx->options & DNS_MASTER_CHECKTTL) != 0 &&
   2016      1.1  christos 		    lctx->ttl > lctx->maxttl)
   2017      1.1  christos 		{
   2018      1.1  christos 			(callbacks->error)(callbacks,
   2019      1.1  christos 				   "dns_master_load: %s:%lu: "
   2020      1.1  christos 				   "TTL %d exceeds configured max-zone-ttl %d",
   2021      1.1  christos 				   source, line, lctx->ttl, lctx->maxttl);
   2022      1.1  christos 			result = ISC_R_RANGE;
   2023      1.1  christos 			goto log_and_cleanup;
   2024      1.1  christos 		}
   2025      1.1  christos 
   2026      1.1  christos 		ISC_LIST_APPEND(this->rdata, &rdata[rdcount], link);
   2027      1.1  christos 		if (ictx->glue != NULL)
   2028      1.1  christos 			ictx->glue_line = line;
   2029      1.1  christos 		else
   2030      1.1  christos 			ictx->current_line = line;
   2031      1.1  christos 		rdcount++;
   2032      1.1  christos 
   2033      1.1  christos 		/*
   2034      1.1  christos 		 * We must have at least 64k as rdlen is 16 bits.
   2035      1.1  christos 		 * If we don't commit everything we have so far.
   2036      1.1  christos 		 */
   2037      1.1  christos 		if ((target.length - target.used) < MINTSIZ)
   2038      1.1  christos 			COMMITALL;
   2039      1.1  christos  next_line:
   2040      1.1  christos 		;
   2041      1.1  christos 	} while (!done && (lctx->loop_cnt == 0 || loop_cnt++ < lctx->loop_cnt));
   2042      1.1  christos 
   2043      1.1  christos 	/*
   2044      1.1  christos 	 * Commit what has not yet been committed.
   2045      1.1  christos 	 */
   2046      1.1  christos 	result = commit(callbacks, lctx, &current_list, ictx->current,
   2047      1.1  christos 			source, ictx->current_line);
   2048      1.1  christos 	if (MANYERRS(lctx, result)) {
   2049      1.1  christos 		SETRESULT(lctx, result);
   2050      1.1  christos 	} else if (result != ISC_R_SUCCESS)
   2051      1.1  christos 		goto insist_and_cleanup;
   2052      1.1  christos 	result = commit(callbacks, lctx, &glue_list, ictx->glue,
   2053      1.1  christos 			source, ictx->glue_line);
   2054      1.1  christos 	if (MANYERRS(lctx, result)) {
   2055      1.1  christos 		SETRESULT(lctx, result);
   2056      1.1  christos 	} else if (result != ISC_R_SUCCESS)
   2057      1.1  christos 		goto insist_and_cleanup;
   2058      1.1  christos 
   2059      1.1  christos 	if (!done) {
   2060      1.1  christos 		INSIST(lctx->done != NULL && lctx->task != NULL);
   2061      1.1  christos 		result = DNS_R_CONTINUE;
   2062      1.1  christos 	} else if (result == ISC_R_SUCCESS && lctx->result != ISC_R_SUCCESS) {
   2063      1.1  christos 		result = lctx->result;
   2064      1.1  christos 	} else if (result == ISC_R_SUCCESS && lctx->seen_include)
   2065      1.1  christos 		result = DNS_R_SEENINCLUDE;
   2066      1.1  christos 	goto cleanup;
   2067      1.1  christos 
   2068      1.1  christos  log_and_cleanup:
   2069      1.1  christos 	LOGIT(result);
   2070      1.1  christos 
   2071      1.1  christos  insist_and_cleanup:
   2072      1.1  christos 	INSIST(result != ISC_R_SUCCESS);
   2073      1.1  christos 
   2074      1.1  christos  cleanup:
   2075      1.1  christos 	while ((this = ISC_LIST_HEAD(current_list)) != NULL)
   2076      1.1  christos 		ISC_LIST_UNLINK(current_list, this, link);
   2077      1.1  christos 	while ((this = ISC_LIST_HEAD(glue_list)) != NULL)
   2078      1.1  christos 		ISC_LIST_UNLINK(glue_list, this, link);
   2079      1.1  christos 	if (rdatalist != NULL)
   2080      1.1  christos 		isc_mem_put(mctx, rdatalist,
   2081      1.1  christos 			    rdatalist_size * sizeof(*rdatalist));
   2082      1.1  christos 	if (rdata != NULL)
   2083      1.1  christos 		isc_mem_put(mctx, rdata, rdata_size * sizeof(*rdata));
   2084      1.1  christos 	if (target_mem != NULL)
   2085      1.1  christos 		isc_mem_put(mctx, target_mem, target_size);
   2086      1.1  christos 	if (include_file != NULL)
   2087      1.1  christos 		isc_mem_free(mctx, include_file);
   2088      1.1  christos 	if (range != NULL)
   2089      1.1  christos 		isc_mem_free(mctx, range);
   2090      1.1  christos 	if (lhs != NULL)
   2091      1.1  christos 		isc_mem_free(mctx, lhs);
   2092      1.1  christos 	if (gtype != NULL)
   2093      1.1  christos 		isc_mem_free(mctx, gtype);
   2094      1.1  christos 	if (rhs != NULL)
   2095      1.1  christos 		isc_mem_free(mctx, rhs);
   2096      1.1  christos 	return (result);
   2097      1.1  christos }
   2098      1.1  christos 
   2099      1.1  christos static isc_result_t
   2100      1.1  christos pushfile(const char *master_file, dns_name_t *origin, dns_loadctx_t *lctx) {
   2101      1.1  christos 	isc_result_t result;
   2102      1.1  christos 	dns_incctx_t *ictx;
   2103      1.1  christos 	dns_incctx_t *newctx = NULL;
   2104      1.1  christos 	isc_region_t r;
   2105      1.1  christos 
   2106      1.1  christos 	REQUIRE(master_file != NULL);
   2107      1.1  christos 	REQUIRE(DNS_LCTX_VALID(lctx));
   2108      1.1  christos 
   2109      1.1  christos 	ictx = lctx->inc;
   2110  1.1.1.2  christos 	lctx->seen_include = true;
   2111      1.1  christos 
   2112      1.1  christos 	result = incctx_create(lctx->mctx, origin, &newctx);
   2113      1.1  christos 	if (result != ISC_R_SUCCESS)
   2114      1.1  christos 		return (result);
   2115      1.1  christos 
   2116      1.1  christos 	/*
   2117      1.1  christos 	 * Push origin_changed.
   2118      1.1  christos 	 */
   2119      1.1  christos 	newctx->origin_changed = ictx->origin_changed;
   2120      1.1  christos 
   2121      1.1  christos 	/* Set current domain. */
   2122      1.1  christos 	if (ictx->glue != NULL || ictx->current != NULL) {
   2123      1.1  christos 		newctx->current_in_use = find_free_name(newctx);
   2124      1.1  christos 		newctx->current =
   2125      1.1  christos 			dns_fixedname_name(&newctx->fixed[newctx->current_in_use]);
   2126  1.1.1.2  christos 		newctx->in_use[newctx->current_in_use] = true;
   2127      1.1  christos 		dns_name_toregion((ictx->glue != NULL) ?
   2128      1.1  christos 				   ictx->glue : ictx->current, &r);
   2129      1.1  christos 		dns_name_fromregion(newctx->current, &r);
   2130      1.1  christos 		newctx->drop = ictx->drop;
   2131      1.1  christos 	}
   2132      1.1  christos 
   2133      1.1  christos 	result = (lctx->openfile)(lctx, master_file);
   2134      1.1  christos 	if (result != ISC_R_SUCCESS)
   2135      1.1  christos 		goto cleanup;
   2136      1.1  christos 	newctx->parent = ictx;
   2137      1.1  christos 	lctx->inc = newctx;
   2138      1.1  christos 
   2139      1.1  christos 	if (lctx->include_cb != NULL)
   2140      1.1  christos 		lctx->include_cb(master_file, lctx->include_arg);
   2141      1.1  christos 	return (ISC_R_SUCCESS);
   2142      1.1  christos 
   2143      1.1  christos  cleanup:
   2144      1.1  christos 	incctx_destroy(lctx->mctx, newctx);
   2145      1.1  christos 	return (result);
   2146      1.1  christos }
   2147      1.1  christos 
   2148      1.1  christos /*
   2149      1.1  christos  * Fill/check exists buffer with 'len' bytes.  Track remaining bytes to be
   2150      1.1  christos  * read when incrementally filling the buffer.
   2151      1.1  christos  */
   2152      1.1  christos static inline isc_result_t
   2153  1.1.1.2  christos read_and_check(bool do_read, isc_buffer_t *buffer,
   2154  1.1.1.2  christos 	       size_t len, FILE *f, uint32_t *totallen)
   2155      1.1  christos {
   2156      1.1  christos 	isc_result_t result;
   2157      1.1  christos 
   2158      1.1  christos 	REQUIRE(totallen != NULL);
   2159      1.1  christos 
   2160      1.1  christos 	if (do_read) {
   2161      1.1  christos 		INSIST(isc_buffer_availablelength(buffer) >= len);
   2162      1.1  christos 		result = isc_stdio_read(isc_buffer_used(buffer), 1, len,
   2163      1.1  christos 					f, NULL);
   2164      1.1  christos 		if (result != ISC_R_SUCCESS)
   2165      1.1  christos 			return (result);
   2166      1.1  christos 		isc_buffer_add(buffer, (unsigned int)len);
   2167      1.1  christos 		if (*totallen < len)
   2168      1.1  christos 			return (ISC_R_RANGE);
   2169  1.1.1.2  christos 		*totallen -= (uint32_t)len;
   2170      1.1  christos 	} else if (isc_buffer_remaininglength(buffer) < len)
   2171      1.1  christos 		return (ISC_R_RANGE);
   2172      1.1  christos 
   2173      1.1  christos 	return (ISC_R_SUCCESS);
   2174      1.1  christos }
   2175      1.1  christos 
   2176      1.1  christos static isc_result_t
   2177      1.1  christos load_header(dns_loadctx_t *lctx) {
   2178      1.1  christos 	isc_result_t result = ISC_R_SUCCESS;
   2179      1.1  christos 	dns_masterrawheader_t header;
   2180      1.1  christos 	dns_rdatacallbacks_t *callbacks;
   2181      1.1  christos 	size_t commonlen = sizeof(header.format) + sizeof(header.version);
   2182      1.1  christos 	size_t remainder;
   2183      1.1  christos 	unsigned char data[sizeof(header)];
   2184      1.1  christos 	isc_buffer_t target;
   2185      1.1  christos 
   2186      1.1  christos 	REQUIRE(DNS_LCTX_VALID(lctx));
   2187      1.1  christos 
   2188      1.1  christos 	if (lctx->format != dns_masterformat_raw &&
   2189      1.1  christos 	    lctx->format != dns_masterformat_map)
   2190      1.1  christos 		return (ISC_R_NOTIMPLEMENTED);
   2191      1.1  christos 
   2192      1.1  christos 	callbacks = lctx->callbacks;
   2193      1.1  christos 	dns_master_initrawheader(&header);
   2194      1.1  christos 
   2195      1.1  christos 	INSIST(commonlen <= sizeof(header));
   2196      1.1  christos 	isc_buffer_init(&target, data, sizeof(data));
   2197      1.1  christos 
   2198      1.1  christos 	result = isc_stdio_read(data, 1, commonlen, lctx->f, NULL);
   2199      1.1  christos 	if (result != ISC_R_SUCCESS) {
   2200      1.1  christos 		UNEXPECTED_ERROR(__FILE__, __LINE__,
   2201      1.1  christos 				 "isc_stdio_read failed: %s",
   2202      1.1  christos 				 isc_result_totext(result));
   2203      1.1  christos 		return (result);
   2204      1.1  christos 	}
   2205      1.1  christos 
   2206      1.1  christos 	isc_buffer_add(&target, (unsigned int)commonlen);
   2207      1.1  christos 	header.format = isc_buffer_getuint32(&target);
   2208      1.1  christos 	if (header.format != lctx->format) {
   2209      1.1  christos 		(*callbacks->error)(callbacks, "dns_master_load: "
   2210      1.1  christos 				    "file format mismatch (not %s)",
   2211      1.1  christos 				    lctx->format == dns_masterformat_map
   2212      1.1  christos 					    ? "map"
   2213      1.1  christos 					    : "raw");
   2214      1.1  christos 		return (ISC_R_NOTIMPLEMENTED);
   2215      1.1  christos 	}
   2216      1.1  christos 
   2217      1.1  christos 	header.version = isc_buffer_getuint32(&target);
   2218      1.1  christos 
   2219      1.1  christos 	switch (header.version) {
   2220      1.1  christos 	case 0:
   2221      1.1  christos 		remainder = sizeof(header.dumptime);
   2222      1.1  christos 		break;
   2223      1.1  christos 	case DNS_RAWFORMAT_VERSION:
   2224      1.1  christos 		remainder = sizeof(header) - commonlen;
   2225      1.1  christos 		break;
   2226      1.1  christos 	default:
   2227      1.1  christos 		(*callbacks->error)(callbacks,
   2228      1.1  christos 				    "dns_master_load: "
   2229      1.1  christos 				    "unsupported file format version");
   2230      1.1  christos 		return (ISC_R_NOTIMPLEMENTED);
   2231      1.1  christos 	}
   2232      1.1  christos 
   2233      1.1  christos 	result = isc_stdio_read(data + commonlen, 1, remainder, lctx->f, NULL);
   2234      1.1  christos 	if (result != ISC_R_SUCCESS) {
   2235      1.1  christos 		UNEXPECTED_ERROR(__FILE__, __LINE__,
   2236      1.1  christos 				 "isc_stdio_read failed: %s",
   2237      1.1  christos 				 isc_result_totext(result));
   2238      1.1  christos 		return (result);
   2239      1.1  christos 	}
   2240      1.1  christos 
   2241      1.1  christos 	isc_buffer_add(&target, (unsigned int)remainder);
   2242      1.1  christos 	header.dumptime = isc_buffer_getuint32(&target);
   2243      1.1  christos 	if (header.version == DNS_RAWFORMAT_VERSION) {
   2244      1.1  christos 		header.flags = isc_buffer_getuint32(&target);
   2245      1.1  christos 		header.sourceserial = isc_buffer_getuint32(&target);
   2246      1.1  christos 		header.lastxfrin = isc_buffer_getuint32(&target);
   2247      1.1  christos 	}
   2248      1.1  christos 
   2249  1.1.1.2  christos 	lctx->first = false;
   2250      1.1  christos 	lctx->header = header;
   2251      1.1  christos 
   2252      1.1  christos 	return (ISC_R_SUCCESS);
   2253      1.1  christos }
   2254      1.1  christos 
   2255      1.1  christos static isc_result_t
   2256      1.1  christos openfile_map(dns_loadctx_t *lctx, const char *master_file) {
   2257      1.1  christos 	isc_result_t result;
   2258      1.1  christos 
   2259      1.1  christos 	result = isc_stdio_open(master_file, "rb", &lctx->f);
   2260      1.1  christos 	if (result != ISC_R_SUCCESS && result != ISC_R_FILENOTFOUND) {
   2261      1.1  christos 		UNEXPECTED_ERROR(__FILE__, __LINE__,
   2262      1.1  christos 				 "isc_stdio_open() failed: %s",
   2263      1.1  christos 				 isc_result_totext(result));
   2264      1.1  christos 	}
   2265      1.1  christos 
   2266      1.1  christos 	return (result);
   2267      1.1  christos }
   2268      1.1  christos 
   2269      1.1  christos /*
   2270      1.1  christos  * Load a map format file, using mmap() to access RBT trees directly
   2271      1.1  christos  */
   2272      1.1  christos static isc_result_t
   2273      1.1  christos load_map(dns_loadctx_t *lctx) {
   2274      1.1  christos 	isc_result_t result = ISC_R_SUCCESS;
   2275      1.1  christos 	dns_rdatacallbacks_t *callbacks;
   2276      1.1  christos 
   2277      1.1  christos 	REQUIRE(DNS_LCTX_VALID(lctx));
   2278      1.1  christos 
   2279      1.1  christos 	callbacks = lctx->callbacks;
   2280      1.1  christos 
   2281      1.1  christos 	if (lctx->first) {
   2282      1.1  christos 		result = load_header(lctx);
   2283      1.1  christos 		if (result != ISC_R_SUCCESS)
   2284      1.1  christos 			return (result);
   2285      1.1  christos 
   2286      1.1  christos 		result = (*callbacks->deserialize)
   2287      1.1  christos 			  (callbacks->deserialize_private,
   2288      1.1  christos 			   lctx->f, sizeof(dns_masterrawheader_t));
   2289      1.1  christos 	}
   2290      1.1  christos 
   2291      1.1  christos 	return (result);
   2292      1.1  christos }
   2293      1.1  christos 
   2294      1.1  christos static isc_result_t
   2295      1.1  christos openfile_raw(dns_loadctx_t *lctx, const char *master_file) {
   2296      1.1  christos 	isc_result_t result;
   2297      1.1  christos 
   2298      1.1  christos 	result = isc_stdio_open(master_file, "rb", &lctx->f);
   2299      1.1  christos 	if (result != ISC_R_SUCCESS && result != ISC_R_FILENOTFOUND) {
   2300      1.1  christos 		UNEXPECTED_ERROR(__FILE__, __LINE__,
   2301      1.1  christos 				 "isc_stdio_open() failed: %s",
   2302      1.1  christos 				 isc_result_totext(result));
   2303      1.1  christos 	}
   2304      1.1  christos 
   2305      1.1  christos 	return (result);
   2306      1.1  christos }
   2307      1.1  christos 
   2308      1.1  christos static isc_result_t
   2309      1.1  christos load_raw(dns_loadctx_t *lctx) {
   2310      1.1  christos 	isc_result_t result = ISC_R_SUCCESS;
   2311  1.1.1.2  christos 	bool done = false;
   2312      1.1  christos 	unsigned int loop_cnt = 0;
   2313      1.1  christos 	dns_rdatacallbacks_t *callbacks;
   2314      1.1  christos 	unsigned char namebuf[DNS_NAME_MAXWIRE];
   2315      1.1  christos 	dns_fixedname_t fixed;
   2316      1.1  christos 	dns_name_t *name;
   2317      1.1  christos 	rdatalist_head_t head, dummy;
   2318      1.1  christos 	dns_rdatalist_t rdatalist;
   2319      1.1  christos 	isc_mem_t *mctx = lctx->mctx;
   2320      1.1  christos 	dns_rdata_t *rdata = NULL;
   2321      1.1  christos 	unsigned int rdata_size = 0;
   2322      1.1  christos 	int target_size = TSIZ;
   2323      1.1  christos 	isc_buffer_t target, buf;
   2324      1.1  christos 	unsigned char *target_mem = NULL;
   2325      1.1  christos 	dns_decompress_t dctx;
   2326      1.1  christos 
   2327      1.1  christos 	callbacks = lctx->callbacks;
   2328      1.1  christos 	dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_NONE);
   2329      1.1  christos 
   2330      1.1  christos 	if (lctx->first) {
   2331      1.1  christos 		result = load_header(lctx);
   2332      1.1  christos 		if (result != ISC_R_SUCCESS)
   2333      1.1  christos 			return (result);
   2334      1.1  christos 	}
   2335      1.1  christos 
   2336      1.1  christos 	ISC_LIST_INIT(head);
   2337      1.1  christos 	ISC_LIST_INIT(dummy);
   2338      1.1  christos 
   2339      1.1  christos 	/*
   2340      1.1  christos 	 * Allocate target_size of buffer space.  This is greater than twice
   2341      1.1  christos 	 * the maximum individual RR data size.
   2342      1.1  christos 	 */
   2343      1.1  christos 	target_mem = isc_mem_get(mctx, target_size);
   2344      1.1  christos 	if (target_mem == NULL) {
   2345      1.1  christos 		result = ISC_R_NOMEMORY;
   2346      1.1  christos 		goto cleanup;
   2347      1.1  christos 	}
   2348      1.1  christos 	isc_buffer_init(&target, target_mem, target_size);
   2349      1.1  christos 
   2350      1.1  christos 	name = dns_fixedname_initname(&fixed);
   2351      1.1  christos 
   2352      1.1  christos 	/*
   2353      1.1  christos 	 * In the following loop, we regard any error fatal regardless of
   2354      1.1  christos 	 * whether "MANYERRORS" is set in the context option.  This is because
   2355      1.1  christos 	 * normal errors should already have been checked at creation time.
   2356      1.1  christos 	 * Besides, it is very unlikely that we can recover from an error
   2357      1.1  christos 	 * in this format, and so trying to continue parsing erroneous data
   2358      1.1  christos 	 * does not really make sense.
   2359      1.1  christos 	 */
   2360      1.1  christos 	for (loop_cnt = 0;
   2361      1.1  christos 	     (lctx->loop_cnt == 0 || loop_cnt < lctx->loop_cnt);
   2362      1.1  christos 	     loop_cnt++) {
   2363      1.1  christos 		unsigned int i, rdcount;
   2364  1.1.1.2  christos 		uint16_t namelen;
   2365  1.1.1.2  christos 		uint32_t totallen;
   2366      1.1  christos 		size_t minlen, readlen;
   2367  1.1.1.2  christos 		bool sequential_read = false;
   2368      1.1  christos 
   2369      1.1  christos 		/* Read the data length */
   2370      1.1  christos 		isc_buffer_clear(&target);
   2371      1.1  christos 		INSIST(isc_buffer_availablelength(&target) >=
   2372      1.1  christos 		       sizeof(totallen));
   2373      1.1  christos 		result = isc_stdio_read(target.base, 1, sizeof(totallen),
   2374      1.1  christos 					lctx->f, NULL);
   2375      1.1  christos 		if (result == ISC_R_EOF) {
   2376      1.1  christos 			result = ISC_R_SUCCESS;
   2377  1.1.1.2  christos 			done = true;
   2378      1.1  christos 			break;
   2379      1.1  christos 		}
   2380      1.1  christos 		if (result != ISC_R_SUCCESS)
   2381      1.1  christos 			goto cleanup;
   2382      1.1  christos 		isc_buffer_add(&target, sizeof(totallen));
   2383      1.1  christos 		totallen = isc_buffer_getuint32(&target);
   2384      1.1  christos 
   2385      1.1  christos 		/*
   2386      1.1  christos 		 * Validation: the input data must at least contain the common
   2387      1.1  christos 		 * header.
   2388      1.1  christos 		 */
   2389  1.1.1.2  christos 		minlen = sizeof(totallen) + sizeof(uint16_t) +
   2390  1.1.1.2  christos 			sizeof(uint16_t) + sizeof(uint16_t) +
   2391  1.1.1.2  christos 			sizeof(uint32_t) + sizeof(uint32_t);
   2392      1.1  christos 		if (totallen < minlen) {
   2393      1.1  christos 			result = ISC_R_RANGE;
   2394      1.1  christos 			goto cleanup;
   2395      1.1  christos 		}
   2396      1.1  christos 		totallen -= sizeof(totallen);
   2397      1.1  christos 
   2398      1.1  christos 		isc_buffer_clear(&target);
   2399      1.1  christos 		if (totallen > isc_buffer_availablelength(&target)) {
   2400      1.1  christos 			/*
   2401      1.1  christos 			 * The default buffer size should typically be large
   2402      1.1  christos 			 * enough to store the entire RRset.  We could try to
   2403      1.1  christos 			 * allocate enough space if this is not the case, but
   2404      1.1  christos 			 * it might cause a hazardous result when "totallen"
   2405      1.1  christos 			 * is forged.  Thus, we'd rather take an inefficient
   2406      1.1  christos 			 * but robust approach in this atypical case: read
   2407      1.1  christos 			 * data step by step, and commit partial data when
   2408      1.1  christos 			 * necessary.  Note that the buffer must be large
   2409      1.1  christos 			 * enough to store the "header part", owner name, and
   2410      1.1  christos 			 * at least one rdata (however large it is).
   2411      1.1  christos 			 */
   2412  1.1.1.2  christos 			sequential_read = true;
   2413      1.1  christos 			readlen = minlen - sizeof(totallen);
   2414      1.1  christos 		} else {
   2415      1.1  christos 			/*
   2416      1.1  christos 			 * Typical case.  We can read the whole RRset at once
   2417      1.1  christos 			 * with the default buffer.
   2418      1.1  christos 			 */
   2419      1.1  christos 			readlen = totallen;
   2420      1.1  christos 		}
   2421      1.1  christos 		result = isc_stdio_read(target.base, 1, readlen,
   2422      1.1  christos 					lctx->f, NULL);
   2423      1.1  christos 		if (result != ISC_R_SUCCESS)
   2424      1.1  christos 			goto cleanup;
   2425      1.1  christos 		isc_buffer_add(&target, (unsigned int)readlen);
   2426  1.1.1.2  christos 		totallen -= (uint32_t)readlen;
   2427      1.1  christos 
   2428      1.1  christos 		/* Construct RRset headers */
   2429      1.1  christos 		dns_rdatalist_init(&rdatalist);
   2430      1.1  christos 		rdatalist.rdclass = isc_buffer_getuint16(&target);
   2431      1.1  christos 		if (lctx->zclass != rdatalist.rdclass) {
   2432      1.1  christos 			result = DNS_R_BADCLASS;
   2433      1.1  christos 			goto cleanup;
   2434      1.1  christos 		}
   2435      1.1  christos 		rdatalist.type = isc_buffer_getuint16(&target);
   2436      1.1  christos 		rdatalist.covers = isc_buffer_getuint16(&target);
   2437      1.1  christos 		rdatalist.ttl =  isc_buffer_getuint32(&target);
   2438      1.1  christos 		rdcount = isc_buffer_getuint32(&target);
   2439      1.1  christos 		if (rdcount == 0 || rdcount > 0xffff) {
   2440      1.1  christos 			result = ISC_R_RANGE;
   2441      1.1  christos 			goto cleanup;
   2442      1.1  christos 		}
   2443      1.1  christos 		INSIST(isc_buffer_consumedlength(&target) <= readlen);
   2444      1.1  christos 
   2445      1.1  christos 		/* Owner name: length followed by name */
   2446      1.1  christos 		result = read_and_check(sequential_read, &target,
   2447      1.1  christos 					sizeof(namelen), lctx->f, &totallen);
   2448      1.1  christos 		if (result != ISC_R_SUCCESS)
   2449      1.1  christos 			goto cleanup;
   2450      1.1  christos 		namelen = isc_buffer_getuint16(&target);
   2451      1.1  christos 		if (namelen > sizeof(namebuf)) {
   2452      1.1  christos 			result = ISC_R_RANGE;
   2453      1.1  christos 			goto cleanup;
   2454      1.1  christos 		}
   2455      1.1  christos 
   2456      1.1  christos 		result = read_and_check(sequential_read, &target, namelen,
   2457      1.1  christos 					lctx->f, &totallen);
   2458      1.1  christos 		if (result != ISC_R_SUCCESS)
   2459      1.1  christos 			goto cleanup;
   2460      1.1  christos 
   2461      1.1  christos 		isc_buffer_setactive(&target, (unsigned int)namelen);
   2462      1.1  christos 		result = dns_name_fromwire(name, &target, &dctx, 0, NULL);
   2463      1.1  christos 		if (result != ISC_R_SUCCESS)
   2464      1.1  christos 			goto cleanup;
   2465      1.1  christos 
   2466      1.1  christos 		if ((lctx->options & DNS_MASTER_CHECKTTL) != 0 &&
   2467      1.1  christos 		    rdatalist.ttl > lctx->maxttl)
   2468      1.1  christos 		{
   2469      1.1  christos 			(callbacks->error)(callbacks,
   2470      1.1  christos 					   "dns_master_load: "
   2471      1.1  christos 					   "TTL %d exceeds configured "
   2472      1.1  christos 					   "max-zone-ttl %d",
   2473      1.1  christos 					   rdatalist.ttl, lctx->maxttl);
   2474      1.1  christos 			result = ISC_R_RANGE;
   2475      1.1  christos 			goto cleanup;
   2476      1.1  christos 		}
   2477      1.1  christos 
   2478      1.1  christos 		/* Rdata contents. */
   2479      1.1  christos 		if (rdcount > rdata_size) {
   2480      1.1  christos 			dns_rdata_t *new_rdata = NULL;
   2481      1.1  christos 
   2482      1.1  christos 			new_rdata = grow_rdata(rdcount + RDSZ, rdata,
   2483      1.1  christos 					       rdata_size, &head,
   2484      1.1  christos 					       &dummy, mctx);
   2485      1.1  christos 			if (new_rdata == NULL) {
   2486      1.1  christos 				result = ISC_R_NOMEMORY;
   2487      1.1  christos 				goto cleanup;
   2488      1.1  christos 			}
   2489      1.1  christos 			rdata_size = rdcount + RDSZ;
   2490      1.1  christos 			rdata = new_rdata;
   2491      1.1  christos 		}
   2492      1.1  christos 
   2493      1.1  christos 	continue_read:
   2494      1.1  christos 		for (i = 0; i < rdcount; i++) {
   2495  1.1.1.2  christos 			uint16_t rdlen;
   2496      1.1  christos 
   2497      1.1  christos 			dns_rdata_init(&rdata[i]);
   2498      1.1  christos 
   2499      1.1  christos 			if (sequential_read &&
   2500      1.1  christos 			    isc_buffer_availablelength(&target) < MINTSIZ) {
   2501      1.1  christos 				unsigned int j;
   2502      1.1  christos 
   2503      1.1  christos 				INSIST(i > 0); /* detect an infinite loop */
   2504      1.1  christos 
   2505      1.1  christos 				/* Partial Commit. */
   2506      1.1  christos 				ISC_LIST_APPEND(head, &rdatalist, link);
   2507      1.1  christos 				result = commit(callbacks, lctx, &head, name,
   2508      1.1  christos 						NULL, 0);
   2509      1.1  christos 				for (j = 0; j < i; j++) {
   2510      1.1  christos 					ISC_LIST_UNLINK(rdatalist.rdata,
   2511      1.1  christos 							&rdata[j], link);
   2512      1.1  christos 					dns_rdata_reset(&rdata[j]);
   2513      1.1  christos 				}
   2514      1.1  christos 				if (result != ISC_R_SUCCESS)
   2515      1.1  christos 					goto cleanup;
   2516      1.1  christos 
   2517      1.1  christos 				/* Rewind the buffer and continue */
   2518      1.1  christos 				isc_buffer_clear(&target);
   2519      1.1  christos 
   2520      1.1  christos 				rdcount -= i;
   2521      1.1  christos 
   2522      1.1  christos 				goto continue_read;
   2523      1.1  christos 			}
   2524      1.1  christos 
   2525      1.1  christos 			/* rdata length */
   2526      1.1  christos 			result = read_and_check(sequential_read, &target,
   2527      1.1  christos 						sizeof(rdlen), lctx->f,
   2528      1.1  christos 						&totallen);
   2529      1.1  christos 			if (result != ISC_R_SUCCESS)
   2530      1.1  christos 				goto cleanup;
   2531      1.1  christos 			rdlen = isc_buffer_getuint16(&target);
   2532      1.1  christos 
   2533      1.1  christos 			/* rdata */
   2534      1.1  christos 			result = read_and_check(sequential_read, &target,
   2535      1.1  christos 						rdlen, lctx->f, &totallen);
   2536      1.1  christos 			if (result != ISC_R_SUCCESS)
   2537      1.1  christos 				goto cleanup;
   2538      1.1  christos 			isc_buffer_setactive(&target, (unsigned int)rdlen);
   2539      1.1  christos 			/*
   2540      1.1  christos 			 * It is safe to have the source active region and
   2541      1.1  christos 			 * the target available region be the same if
   2542      1.1  christos 			 * decompression is disabled (see dctx above) and we
   2543      1.1  christos 			 * are not downcasing names (options == 0).
   2544      1.1  christos 			 */
   2545      1.1  christos 			isc_buffer_init(&buf, isc_buffer_current(&target),
   2546      1.1  christos 					(unsigned int)rdlen);
   2547      1.1  christos 			result = dns_rdata_fromwire(&rdata[i],
   2548      1.1  christos 						    rdatalist.rdclass,
   2549      1.1  christos 						    rdatalist.type, &target,
   2550      1.1  christos 						    &dctx, 0, &buf);
   2551      1.1  christos 			if (result != ISC_R_SUCCESS)
   2552      1.1  christos 				goto cleanup;
   2553      1.1  christos 			ISC_LIST_APPEND(rdatalist.rdata, &rdata[i], link);
   2554      1.1  christos 		}
   2555      1.1  christos 
   2556      1.1  christos 		/*
   2557      1.1  christos 		 * Sanity check.  Still having remaining space is not
   2558      1.1  christos 		 * necessarily critical, but it very likely indicates broken
   2559      1.1  christos 		 * or malformed data.
   2560      1.1  christos 		 */
   2561      1.1  christos 		if (isc_buffer_remaininglength(&target) != 0 || totallen != 0) {
   2562      1.1  christos 			result = ISC_R_RANGE;
   2563      1.1  christos 			goto cleanup;
   2564      1.1  christos 		}
   2565      1.1  christos 
   2566      1.1  christos 		ISC_LIST_APPEND(head, &rdatalist, link);
   2567      1.1  christos 
   2568      1.1  christos 		/* Commit this RRset.  rdatalist will be unlinked. */
   2569      1.1  christos 		result = commit(callbacks, lctx, &head, name, NULL, 0);
   2570      1.1  christos 
   2571      1.1  christos 		for (i = 0; i < rdcount; i++) {
   2572      1.1  christos 			ISC_LIST_UNLINK(rdatalist.rdata, &rdata[i], link);
   2573      1.1  christos 			dns_rdata_reset(&rdata[i]);
   2574      1.1  christos 		}
   2575      1.1  christos 
   2576      1.1  christos 		if (result != ISC_R_SUCCESS)
   2577      1.1  christos 			goto cleanup;
   2578      1.1  christos 	}
   2579      1.1  christos 
   2580      1.1  christos 	if (!done) {
   2581      1.1  christos 		INSIST(lctx->done != NULL && lctx->task != NULL);
   2582      1.1  christos 		result = DNS_R_CONTINUE;
   2583      1.1  christos 	} else if (result == ISC_R_SUCCESS && lctx->result != ISC_R_SUCCESS)
   2584      1.1  christos 		result = lctx->result;
   2585      1.1  christos 
   2586      1.1  christos 	if (result == ISC_R_SUCCESS && callbacks->rawdata != NULL)
   2587      1.1  christos 		(*callbacks->rawdata)(callbacks->zone, &lctx->header);
   2588      1.1  christos 
   2589      1.1  christos  cleanup:
   2590      1.1  christos 	if (rdata != NULL)
   2591      1.1  christos 		isc_mem_put(mctx, rdata, rdata_size * sizeof(*rdata));
   2592      1.1  christos 	if (target_mem != NULL)
   2593      1.1  christos 		isc_mem_put(mctx, target_mem, target_size);
   2594      1.1  christos 	if (result != ISC_R_SUCCESS && result != DNS_R_CONTINUE) {
   2595      1.1  christos 		(*callbacks->error)(callbacks, "dns_master_load: %s",
   2596      1.1  christos 				    dns_result_totext(result));
   2597      1.1  christos 	}
   2598      1.1  christos 
   2599      1.1  christos 	return (result);
   2600      1.1  christos }
   2601      1.1  christos 
   2602      1.1  christos isc_result_t
   2603      1.1  christos dns_master_loadfile(const char *master_file, dns_name_t *top,
   2604  1.1.1.2  christos 		    dns_name_t *origin, dns_rdataclass_t zclass,
   2605  1.1.1.2  christos 		    unsigned int options, uint32_t resign,
   2606  1.1.1.2  christos 		    dns_rdatacallbacks_t *callbacks,
   2607  1.1.1.2  christos 		    dns_masterincludecb_t include_cb, void *include_arg,
   2608  1.1.1.2  christos 		    isc_mem_t *mctx, dns_masterformat_t format,
   2609  1.1.1.2  christos 		    dns_ttl_t maxttl)
   2610      1.1  christos {
   2611      1.1  christos 	dns_loadctx_t *lctx = NULL;
   2612      1.1  christos 	isc_result_t result;
   2613      1.1  christos 
   2614      1.1  christos 	result = loadctx_create(format, mctx, options, resign, top, zclass,
   2615      1.1  christos 				origin, callbacks, NULL, NULL, NULL,
   2616      1.1  christos 				include_cb, include_arg, NULL, &lctx);
   2617      1.1  christos 	if (result != ISC_R_SUCCESS)
   2618      1.1  christos 		return (result);
   2619      1.1  christos 
   2620      1.1  christos 	lctx->maxttl = maxttl;
   2621      1.1  christos 
   2622      1.1  christos 	result = (lctx->openfile)(lctx, master_file);
   2623      1.1  christos 	if (result != ISC_R_SUCCESS)
   2624      1.1  christos 		goto cleanup;
   2625      1.1  christos 
   2626      1.1  christos 	result = (lctx->load)(lctx);
   2627      1.1  christos 	INSIST(result != DNS_R_CONTINUE);
   2628      1.1  christos 
   2629      1.1  christos  cleanup:
   2630      1.1  christos 	dns_loadctx_detach(&lctx);
   2631      1.1  christos 	return (result);
   2632      1.1  christos }
   2633      1.1  christos 
   2634      1.1  christos isc_result_t
   2635      1.1  christos dns_master_loadfileinc(const char *master_file, dns_name_t *top,
   2636      1.1  christos 		       dns_name_t *origin, dns_rdataclass_t zclass,
   2637  1.1.1.2  christos 		       unsigned int options, uint32_t resign,
   2638  1.1.1.2  christos 		       dns_rdatacallbacks_t *callbacks,
   2639      1.1  christos 		       isc_task_t *task, dns_loaddonefunc_t done,
   2640  1.1.1.2  christos 		       void *done_arg, dns_loadctx_t **lctxp,
   2641  1.1.1.2  christos 		       dns_masterincludecb_t include_cb, void *include_arg,
   2642  1.1.1.2  christos 		       isc_mem_t *mctx, dns_masterformat_t format,
   2643  1.1.1.2  christos 		       uint32_t maxttl)
   2644      1.1  christos {
   2645      1.1  christos 	dns_loadctx_t *lctx = NULL;
   2646      1.1  christos 	isc_result_t result;
   2647      1.1  christos 
   2648      1.1  christos 	REQUIRE(task != NULL);
   2649      1.1  christos 	REQUIRE(done != NULL);
   2650      1.1  christos 
   2651      1.1  christos 	result = loadctx_create(format, mctx, options, resign, top, zclass,
   2652      1.1  christos 				origin, callbacks, task, done, done_arg,
   2653      1.1  christos 				include_cb, include_arg, NULL, &lctx);
   2654      1.1  christos 	if (result != ISC_R_SUCCESS)
   2655      1.1  christos 		return (result);
   2656      1.1  christos 
   2657      1.1  christos 	lctx->maxttl = maxttl;
   2658      1.1  christos 
   2659      1.1  christos 	result = (lctx->openfile)(lctx, master_file);
   2660      1.1  christos 	if (result != ISC_R_SUCCESS)
   2661      1.1  christos 		goto cleanup;
   2662      1.1  christos 
   2663      1.1  christos 	result = task_send(lctx);
   2664      1.1  christos 	if (result == ISC_R_SUCCESS) {
   2665      1.1  christos 		dns_loadctx_attach(lctx, lctxp);
   2666      1.1  christos 		return (DNS_R_CONTINUE);
   2667      1.1  christos 	}
   2668      1.1  christos 
   2669      1.1  christos  cleanup:
   2670      1.1  christos 	dns_loadctx_detach(&lctx);
   2671      1.1  christos 	return (result);
   2672      1.1  christos }
   2673      1.1  christos 
   2674      1.1  christos isc_result_t
   2675      1.1  christos dns_master_loadstream(FILE *stream, dns_name_t *top, dns_name_t *origin,
   2676      1.1  christos 		      dns_rdataclass_t zclass, unsigned int options,
   2677      1.1  christos 		      dns_rdatacallbacks_t *callbacks, isc_mem_t *mctx)
   2678      1.1  christos {
   2679      1.1  christos 	isc_result_t result;
   2680      1.1  christos 	dns_loadctx_t *lctx = NULL;
   2681      1.1  christos 
   2682      1.1  christos 	REQUIRE(stream != NULL);
   2683      1.1  christos 
   2684      1.1  christos 	result = loadctx_create(dns_masterformat_text, mctx, options, 0, top,
   2685      1.1  christos 				zclass, origin, callbacks, NULL, NULL, NULL,
   2686      1.1  christos 				NULL, NULL, NULL, &lctx);
   2687      1.1  christos 	if (result != ISC_R_SUCCESS)
   2688      1.1  christos 		goto cleanup;
   2689      1.1  christos 
   2690      1.1  christos 	result = isc_lex_openstream(lctx->lex, stream);
   2691      1.1  christos 	if (result != ISC_R_SUCCESS)
   2692      1.1  christos 		goto cleanup;
   2693      1.1  christos 
   2694      1.1  christos 	result = (lctx->load)(lctx);
   2695      1.1  christos 	INSIST(result != DNS_R_CONTINUE);
   2696      1.1  christos 
   2697      1.1  christos  cleanup:
   2698      1.1  christos 	if (lctx != NULL)
   2699      1.1  christos 		dns_loadctx_detach(&lctx);
   2700      1.1  christos 	return (result);
   2701      1.1  christos }
   2702      1.1  christos 
   2703      1.1  christos isc_result_t
   2704      1.1  christos dns_master_loadstreaminc(FILE *stream, dns_name_t *top, dns_name_t *origin,
   2705      1.1  christos 			 dns_rdataclass_t zclass, unsigned int options,
   2706      1.1  christos 			 dns_rdatacallbacks_t *callbacks, isc_task_t *task,
   2707      1.1  christos 			 dns_loaddonefunc_t done, void *done_arg,
   2708      1.1  christos 			 dns_loadctx_t **lctxp, isc_mem_t *mctx)
   2709      1.1  christos {
   2710      1.1  christos 	isc_result_t result;
   2711      1.1  christos 	dns_loadctx_t *lctx = NULL;
   2712      1.1  christos 
   2713      1.1  christos 	REQUIRE(stream != NULL);
   2714      1.1  christos 	REQUIRE(task != NULL);
   2715      1.1  christos 	REQUIRE(done != NULL);
   2716      1.1  christos 
   2717      1.1  christos 	result = loadctx_create(dns_masterformat_text, mctx, options, 0, top,
   2718      1.1  christos 				zclass, origin, callbacks, task, done,
   2719      1.1  christos 				done_arg, NULL, NULL, NULL, &lctx);
   2720      1.1  christos 	if (result != ISC_R_SUCCESS)
   2721      1.1  christos 		goto cleanup;
   2722      1.1  christos 
   2723      1.1  christos 	result = isc_lex_openstream(lctx->lex, stream);
   2724      1.1  christos 	if (result != ISC_R_SUCCESS)
   2725      1.1  christos 		goto cleanup;
   2726      1.1  christos 
   2727      1.1  christos 	result = task_send(lctx);
   2728      1.1  christos 	if (result == ISC_R_SUCCESS) {
   2729      1.1  christos 		dns_loadctx_attach(lctx, lctxp);
   2730      1.1  christos 		return (DNS_R_CONTINUE);
   2731      1.1  christos 	}
   2732      1.1  christos 
   2733      1.1  christos  cleanup:
   2734      1.1  christos 	if (lctx != NULL)
   2735      1.1  christos 		dns_loadctx_detach(&lctx);
   2736      1.1  christos 	return (result);
   2737      1.1  christos }
   2738      1.1  christos 
   2739      1.1  christos isc_result_t
   2740      1.1  christos dns_master_loadbuffer(isc_buffer_t *buffer, dns_name_t *top,
   2741      1.1  christos 		      dns_name_t *origin, dns_rdataclass_t zclass,
   2742      1.1  christos 		      unsigned int options,
   2743      1.1  christos 		      dns_rdatacallbacks_t *callbacks, isc_mem_t *mctx)
   2744      1.1  christos {
   2745      1.1  christos 	isc_result_t result;
   2746      1.1  christos 	dns_loadctx_t *lctx = NULL;
   2747      1.1  christos 
   2748      1.1  christos 	REQUIRE(buffer != NULL);
   2749      1.1  christos 
   2750      1.1  christos 	result = loadctx_create(dns_masterformat_text, mctx, options, 0, top,
   2751      1.1  christos 				zclass, origin, callbacks, NULL, NULL, NULL,
   2752      1.1  christos 				NULL, NULL, NULL, &lctx);
   2753      1.1  christos 	if (result != ISC_R_SUCCESS)
   2754      1.1  christos 		return (result);
   2755      1.1  christos 
   2756      1.1  christos 	result = isc_lex_openbuffer(lctx->lex, buffer);
   2757      1.1  christos 	if (result != ISC_R_SUCCESS)
   2758      1.1  christos 		goto cleanup;
   2759      1.1  christos 
   2760      1.1  christos 	result = (lctx->load)(lctx);
   2761      1.1  christos 	INSIST(result != DNS_R_CONTINUE);
   2762      1.1  christos 
   2763      1.1  christos  cleanup:
   2764      1.1  christos 	dns_loadctx_detach(&lctx);
   2765      1.1  christos 	return (result);
   2766      1.1  christos }
   2767      1.1  christos 
   2768      1.1  christos isc_result_t
   2769      1.1  christos dns_master_loadbufferinc(isc_buffer_t *buffer, dns_name_t *top,
   2770      1.1  christos 			 dns_name_t *origin, dns_rdataclass_t zclass,
   2771      1.1  christos 			 unsigned int options,
   2772      1.1  christos 			 dns_rdatacallbacks_t *callbacks, isc_task_t *task,
   2773      1.1  christos 			 dns_loaddonefunc_t done, void *done_arg,
   2774      1.1  christos 			 dns_loadctx_t **lctxp, isc_mem_t *mctx)
   2775      1.1  christos {
   2776      1.1  christos 	isc_result_t result;
   2777      1.1  christos 	dns_loadctx_t *lctx = NULL;
   2778      1.1  christos 
   2779      1.1  christos 	REQUIRE(buffer != NULL);
   2780      1.1  christos 	REQUIRE(task != NULL);
   2781      1.1  christos 	REQUIRE(done != NULL);
   2782      1.1  christos 
   2783      1.1  christos 	result = loadctx_create(dns_masterformat_text, mctx, options, 0, top,
   2784      1.1  christos 				zclass, origin, callbacks, task, done,
   2785      1.1  christos 				done_arg, NULL, NULL, NULL, &lctx);
   2786      1.1  christos 	if (result != ISC_R_SUCCESS)
   2787      1.1  christos 		return (result);
   2788      1.1  christos 
   2789      1.1  christos 	result = isc_lex_openbuffer(lctx->lex, buffer);
   2790      1.1  christos 	if (result != ISC_R_SUCCESS)
   2791      1.1  christos 		goto cleanup;
   2792      1.1  christos 
   2793      1.1  christos 	result = task_send(lctx);
   2794      1.1  christos 	if (result == ISC_R_SUCCESS) {
   2795      1.1  christos 		dns_loadctx_attach(lctx, lctxp);
   2796      1.1  christos 		return (DNS_R_CONTINUE);
   2797      1.1  christos 	}
   2798      1.1  christos 
   2799      1.1  christos  cleanup:
   2800      1.1  christos 	dns_loadctx_detach(&lctx);
   2801      1.1  christos 	return (result);
   2802      1.1  christos }
   2803      1.1  christos 
   2804      1.1  christos isc_result_t
   2805      1.1  christos dns_master_loadlexer(isc_lex_t *lex, dns_name_t *top,
   2806      1.1  christos 		     dns_name_t *origin, dns_rdataclass_t zclass,
   2807      1.1  christos 		     unsigned int options,
   2808      1.1  christos 		     dns_rdatacallbacks_t *callbacks, isc_mem_t *mctx)
   2809      1.1  christos {
   2810      1.1  christos 	isc_result_t result;
   2811      1.1  christos 	dns_loadctx_t *lctx = NULL;
   2812      1.1  christos 
   2813      1.1  christos 	REQUIRE(lex != NULL);
   2814      1.1  christos 
   2815      1.1  christos 	result = loadctx_create(dns_masterformat_text, mctx, options, 0, top,
   2816      1.1  christos 				zclass, origin, callbacks, NULL, NULL, NULL,
   2817      1.1  christos 				NULL, NULL, lex, &lctx);
   2818      1.1  christos 	if (result != ISC_R_SUCCESS)
   2819      1.1  christos 		return (result);
   2820      1.1  christos 
   2821      1.1  christos 	result = (lctx->load)(lctx);
   2822      1.1  christos 	INSIST(result != DNS_R_CONTINUE);
   2823      1.1  christos 
   2824      1.1  christos 	dns_loadctx_detach(&lctx);
   2825      1.1  christos 	return (result);
   2826      1.1  christos }
   2827      1.1  christos 
   2828      1.1  christos isc_result_t
   2829      1.1  christos dns_master_loadlexerinc(isc_lex_t *lex, dns_name_t *top,
   2830      1.1  christos 			dns_name_t *origin, dns_rdataclass_t zclass,
   2831      1.1  christos 			unsigned int options,
   2832      1.1  christos 			dns_rdatacallbacks_t *callbacks, isc_task_t *task,
   2833      1.1  christos 			dns_loaddonefunc_t done, void *done_arg,
   2834      1.1  christos 			dns_loadctx_t **lctxp, isc_mem_t *mctx)
   2835      1.1  christos {
   2836      1.1  christos 	isc_result_t result;
   2837      1.1  christos 	dns_loadctx_t *lctx = NULL;
   2838      1.1  christos 
   2839      1.1  christos 	REQUIRE(lex != NULL);
   2840      1.1  christos 	REQUIRE(task != NULL);
   2841      1.1  christos 	REQUIRE(done != NULL);
   2842      1.1  christos 
   2843      1.1  christos 	result = loadctx_create(dns_masterformat_text, mctx, options, 0, top,
   2844      1.1  christos 				zclass, origin, callbacks, task, done,
   2845      1.1  christos 				done_arg, NULL, NULL, lex, &lctx);
   2846      1.1  christos 	if (result != ISC_R_SUCCESS)
   2847      1.1  christos 		return (result);
   2848      1.1  christos 
   2849      1.1  christos 	result = task_send(lctx);
   2850      1.1  christos 	if (result == ISC_R_SUCCESS) {
   2851      1.1  christos 		dns_loadctx_attach(lctx, lctxp);
   2852      1.1  christos 		return (DNS_R_CONTINUE);
   2853      1.1  christos 	}
   2854      1.1  christos 
   2855      1.1  christos 	dns_loadctx_detach(&lctx);
   2856      1.1  christos 	return (result);
   2857      1.1  christos }
   2858      1.1  christos 
   2859      1.1  christos /*
   2860      1.1  christos  * Grow the slab of dns_rdatalist_t structures.
   2861      1.1  christos  * Re-link glue and current list.
   2862      1.1  christos  */
   2863      1.1  christos static dns_rdatalist_t *
   2864      1.1  christos grow_rdatalist(int new_len, dns_rdatalist_t *oldlist, int old_len,
   2865      1.1  christos 	       rdatalist_head_t *current, rdatalist_head_t *glue,
   2866      1.1  christos 	       isc_mem_t *mctx)
   2867      1.1  christos {
   2868      1.1  christos 	dns_rdatalist_t *newlist;
   2869      1.1  christos 	int rdlcount = 0;
   2870      1.1  christos 	ISC_LIST(dns_rdatalist_t) save;
   2871      1.1  christos 	dns_rdatalist_t *this;
   2872      1.1  christos 
   2873      1.1  christos 	newlist = isc_mem_get(mctx, new_len * sizeof(*newlist));
   2874      1.1  christos 	if (newlist == NULL)
   2875      1.1  christos 		return (NULL);
   2876      1.1  christos 
   2877      1.1  christos 	ISC_LIST_INIT(save);
   2878      1.1  christos 	while ((this = ISC_LIST_HEAD(*current)) != NULL) {
   2879      1.1  christos 		ISC_LIST_UNLINK(*current, this, link);
   2880      1.1  christos 		ISC_LIST_APPEND(save, this, link);
   2881      1.1  christos 	}
   2882      1.1  christos 	while ((this = ISC_LIST_HEAD(save)) != NULL) {
   2883      1.1  christos 		ISC_LIST_UNLINK(save, this, link);
   2884      1.1  christos 		INSIST(rdlcount < new_len);
   2885      1.1  christos 		newlist[rdlcount] = *this;
   2886      1.1  christos 		ISC_LIST_APPEND(*current, &newlist[rdlcount], link);
   2887      1.1  christos 		rdlcount++;
   2888      1.1  christos 	}
   2889      1.1  christos 
   2890      1.1  christos 	ISC_LIST_INIT(save);
   2891      1.1  christos 	while ((this = ISC_LIST_HEAD(*glue)) != NULL) {
   2892      1.1  christos 		ISC_LIST_UNLINK(*glue, this, link);
   2893      1.1  christos 		ISC_LIST_APPEND(save, this, link);
   2894      1.1  christos 	}
   2895      1.1  christos 	while ((this = ISC_LIST_HEAD(save)) != NULL) {
   2896      1.1  christos 		ISC_LIST_UNLINK(save, this, link);
   2897      1.1  christos 		INSIST(rdlcount < new_len);
   2898      1.1  christos 		newlist[rdlcount] = *this;
   2899      1.1  christos 		ISC_LIST_APPEND(*glue, &newlist[rdlcount], link);
   2900      1.1  christos 		rdlcount++;
   2901      1.1  christos 	}
   2902      1.1  christos 
   2903      1.1  christos 	INSIST(rdlcount == old_len);
   2904      1.1  christos 	if (oldlist != NULL)
   2905      1.1  christos 		isc_mem_put(mctx, oldlist, old_len * sizeof(*oldlist));
   2906      1.1  christos 	return (newlist);
   2907      1.1  christos }
   2908      1.1  christos 
   2909      1.1  christos /*
   2910      1.1  christos  * Grow the slab of rdata structs.
   2911      1.1  christos  * Re-link the current and glue chains.
   2912      1.1  christos  */
   2913      1.1  christos static dns_rdata_t *
   2914      1.1  christos grow_rdata(int new_len, dns_rdata_t *oldlist, int old_len,
   2915      1.1  christos 	   rdatalist_head_t *current, rdatalist_head_t *glue,
   2916      1.1  christos 	   isc_mem_t *mctx)
   2917      1.1  christos {
   2918      1.1  christos 	dns_rdata_t *newlist;
   2919      1.1  christos 	int rdcount = 0;
   2920      1.1  christos 	ISC_LIST(dns_rdata_t) save;
   2921      1.1  christos 	dns_rdatalist_t *this;
   2922      1.1  christos 	dns_rdata_t *rdata;
   2923      1.1  christos 
   2924      1.1  christos 	newlist = isc_mem_get(mctx, new_len * sizeof(*newlist));
   2925      1.1  christos 	if (newlist == NULL)
   2926      1.1  christos 		return (NULL);
   2927      1.1  christos 	memset(newlist, 0, new_len * sizeof(*newlist));
   2928      1.1  christos 
   2929      1.1  christos 	/*
   2930      1.1  christos 	 * Copy current relinking.
   2931      1.1  christos 	 */
   2932      1.1  christos 	this = ISC_LIST_HEAD(*current);
   2933      1.1  christos 	while (this != NULL) {
   2934      1.1  christos 		ISC_LIST_INIT(save);
   2935      1.1  christos 		while ((rdata = ISC_LIST_HEAD(this->rdata)) != NULL) {
   2936      1.1  christos 			ISC_LIST_UNLINK(this->rdata, rdata, link);
   2937      1.1  christos 			ISC_LIST_APPEND(save, rdata, link);
   2938      1.1  christos 		}
   2939      1.1  christos 		while ((rdata = ISC_LIST_HEAD(save)) != NULL) {
   2940      1.1  christos 			ISC_LIST_UNLINK(save, rdata, link);
   2941      1.1  christos 			INSIST(rdcount < new_len);
   2942      1.1  christos 			newlist[rdcount] = *rdata;
   2943      1.1  christos 			ISC_LIST_APPEND(this->rdata, &newlist[rdcount], link);
   2944      1.1  christos 			rdcount++;
   2945      1.1  christos 		}
   2946      1.1  christos 		this = ISC_LIST_NEXT(this, link);
   2947      1.1  christos 	}
   2948      1.1  christos 
   2949      1.1  christos 	/*
   2950      1.1  christos 	 * Copy glue relinking.
   2951      1.1  christos 	 */
   2952      1.1  christos 	this = ISC_LIST_HEAD(*glue);
   2953      1.1  christos 	while (this != NULL) {
   2954      1.1  christos 		ISC_LIST_INIT(save);
   2955      1.1  christos 		while ((rdata = ISC_LIST_HEAD(this->rdata)) != NULL) {
   2956      1.1  christos 			ISC_LIST_UNLINK(this->rdata, rdata, link);
   2957      1.1  christos 			ISC_LIST_APPEND(save, rdata, link);
   2958      1.1  christos 		}
   2959      1.1  christos 		while ((rdata = ISC_LIST_HEAD(save)) != NULL) {
   2960      1.1  christos 			ISC_LIST_UNLINK(save, rdata, link);
   2961      1.1  christos 			INSIST(rdcount < new_len);
   2962      1.1  christos 			newlist[rdcount] = *rdata;
   2963      1.1  christos 			ISC_LIST_APPEND(this->rdata, &newlist[rdcount], link);
   2964      1.1  christos 			rdcount++;
   2965      1.1  christos 		}
   2966      1.1  christos 		this = ISC_LIST_NEXT(this, link);
   2967      1.1  christos 	}
   2968      1.1  christos 	INSIST(rdcount == old_len || rdcount == 0);
   2969      1.1  christos 	if (oldlist != NULL)
   2970      1.1  christos 		isc_mem_put(mctx, oldlist, old_len * sizeof(*oldlist));
   2971      1.1  christos 	return (newlist);
   2972      1.1  christos }
   2973      1.1  christos 
   2974  1.1.1.2  christos static uint32_t
   2975      1.1  christos resign_fromlist(dns_rdatalist_t *this, dns_loadctx_t *lctx) {
   2976      1.1  christos 	dns_rdata_t *rdata;
   2977      1.1  christos 	dns_rdata_rrsig_t sig;
   2978  1.1.1.2  christos 	uint32_t when;
   2979      1.1  christos 
   2980      1.1  christos 	rdata = ISC_LIST_HEAD(this->rdata);
   2981      1.1  christos 	INSIST(rdata != NULL);
   2982      1.1  christos 	(void)dns_rdata_tostruct(rdata, &sig, NULL);
   2983      1.1  christos 	if (isc_serial_gt(sig.timesigned, lctx->now))
   2984      1.1  christos 		when = lctx->now;
   2985      1.1  christos 	else
   2986      1.1  christos 		when = sig.timeexpire - lctx->resign;
   2987      1.1  christos 
   2988      1.1  christos 	rdata = ISC_LIST_NEXT(rdata, link);
   2989      1.1  christos 	while (rdata != NULL) {
   2990      1.1  christos 		(void)dns_rdata_tostruct(rdata, &sig, NULL);
   2991      1.1  christos 		if (isc_serial_gt(sig.timesigned, lctx->now))
   2992      1.1  christos 			when = lctx->now;
   2993      1.1  christos 		else if (sig.timeexpire - lctx->resign < when)
   2994      1.1  christos 			when = sig.timeexpire - lctx->resign;
   2995      1.1  christos 		rdata = ISC_LIST_NEXT(rdata, link);
   2996      1.1  christos 	}
   2997      1.1  christos 	return (when);
   2998      1.1  christos }
   2999      1.1  christos 
   3000      1.1  christos /*
   3001      1.1  christos  * Convert each element from a rdatalist_t to rdataset then call commit.
   3002      1.1  christos  * Unlink each element as we go.
   3003      1.1  christos  */
   3004      1.1  christos 
   3005      1.1  christos static isc_result_t
   3006      1.1  christos commit(dns_rdatacallbacks_t *callbacks, dns_loadctx_t *lctx,
   3007      1.1  christos        rdatalist_head_t *head, dns_name_t *owner,
   3008      1.1  christos        const char *source, unsigned int line)
   3009      1.1  christos {
   3010      1.1  christos 	dns_rdatalist_t *this;
   3011      1.1  christos 	dns_rdataset_t dataset;
   3012      1.1  christos 	isc_result_t result;
   3013      1.1  christos 	char namebuf[DNS_NAME_FORMATSIZE];
   3014      1.1  christos 	void    (*error)(struct dns_rdatacallbacks *, const char *, ...);
   3015      1.1  christos 
   3016      1.1  christos 	this = ISC_LIST_HEAD(*head);
   3017      1.1  christos 	error = callbacks->error;
   3018      1.1  christos 
   3019      1.1  christos 	if (this == NULL)
   3020      1.1  christos 		return (ISC_R_SUCCESS);
   3021      1.1  christos 	do {
   3022      1.1  christos 		dns_rdataset_init(&dataset);
   3023      1.1  christos 		RUNTIME_CHECK(dns_rdatalist_tordataset(this, &dataset)
   3024      1.1  christos 			      == ISC_R_SUCCESS);
   3025      1.1  christos 		dataset.trust = dns_trust_ultimate;
   3026      1.1  christos 		/*
   3027      1.1  christos 		 * If this is a secure dynamic zone set the re-signing time.
   3028      1.1  christos 		 */
   3029      1.1  christos 		if (dataset.type == dns_rdatatype_rrsig &&
   3030      1.1  christos 		    (lctx->options & DNS_MASTER_RESIGN) != 0) {
   3031      1.1  christos 			dataset.attributes |= DNS_RDATASETATTR_RESIGN;
   3032      1.1  christos 			dataset.resign = resign_fromlist(this, lctx);
   3033      1.1  christos 		}
   3034      1.1  christos 		result = ((*callbacks->add)(callbacks->add_private, owner,
   3035      1.1  christos 					    &dataset));
   3036      1.1  christos 		if (result == ISC_R_NOMEMORY) {
   3037      1.1  christos 			(*error)(callbacks, "dns_master_load: %s",
   3038      1.1  christos 				 dns_result_totext(result));
   3039      1.1  christos 		} else if (result != ISC_R_SUCCESS) {
   3040      1.1  christos 			dns_name_format(owner, namebuf, sizeof(namebuf));
   3041      1.1  christos 			if (source != NULL) {
   3042      1.1  christos 				(*error)(callbacks, "%s: %s:%lu: %s: %s",
   3043      1.1  christos 					 "dns_master_load", source, line,
   3044      1.1  christos 					 namebuf, dns_result_totext(result));
   3045      1.1  christos 			} else {
   3046      1.1  christos 				(*error)(callbacks, "%s: %s: %s",
   3047      1.1  christos 					 "dns_master_load", namebuf,
   3048      1.1  christos 					 dns_result_totext(result));
   3049      1.1  christos 			}
   3050      1.1  christos 		}
   3051      1.1  christos 		if (MANYERRS(lctx, result))
   3052      1.1  christos 			SETRESULT(lctx, result);
   3053      1.1  christos 		else if (result != ISC_R_SUCCESS)
   3054      1.1  christos 			return (result);
   3055      1.1  christos 		ISC_LIST_UNLINK(*head, this, link);
   3056      1.1  christos 		this = ISC_LIST_HEAD(*head);
   3057      1.1  christos 	} while (this != NULL);
   3058      1.1  christos 	return (ISC_R_SUCCESS);
   3059      1.1  christos }
   3060      1.1  christos 
   3061      1.1  christos /*
   3062  1.1.1.2  christos  * Returns true if one of the NS rdata's contains 'owner'.
   3063      1.1  christos  */
   3064      1.1  christos 
   3065  1.1.1.2  christos static bool
   3066      1.1  christos is_glue(rdatalist_head_t *head, dns_name_t *owner) {
   3067      1.1  christos 	dns_rdatalist_t *this;
   3068      1.1  christos 	dns_rdata_t *rdata;
   3069      1.1  christos 	isc_region_t region;
   3070      1.1  christos 	dns_name_t name;
   3071      1.1  christos 
   3072      1.1  christos 	/*
   3073      1.1  christos 	 * Find NS rrset.
   3074      1.1  christos 	 */
   3075      1.1  christos 	this = ISC_LIST_HEAD(*head);
   3076      1.1  christos 	while (this != NULL) {
   3077      1.1  christos 		if (this->type == dns_rdatatype_ns)
   3078      1.1  christos 			break;
   3079      1.1  christos 		this = ISC_LIST_NEXT(this, link);
   3080      1.1  christos 	}
   3081      1.1  christos 	if (this == NULL)
   3082  1.1.1.2  christos 		return (false);
   3083      1.1  christos 
   3084      1.1  christos 	rdata = ISC_LIST_HEAD(this->rdata);
   3085      1.1  christos 	while (rdata != NULL) {
   3086      1.1  christos 		dns_name_init(&name, NULL);
   3087      1.1  christos 		dns_rdata_toregion(rdata, &region);
   3088      1.1  christos 		dns_name_fromregion(&name, &region);
   3089  1.1.1.2  christos 		if (dns_name_equal(&name, owner)) {
   3090  1.1.1.2  christos 			return (true);
   3091  1.1.1.2  christos 		}
   3092      1.1  christos 		rdata = ISC_LIST_NEXT(rdata, link);
   3093      1.1  christos 	}
   3094  1.1.1.2  christos 	return (false);
   3095      1.1  christos }
   3096      1.1  christos 
   3097      1.1  christos static void
   3098      1.1  christos load_quantum(isc_task_t *task, isc_event_t *event) {
   3099      1.1  christos 	isc_result_t result;
   3100      1.1  christos 	dns_loadctx_t *lctx;
   3101      1.1  christos 
   3102      1.1  christos 	REQUIRE(event != NULL);
   3103      1.1  christos 	lctx = event->ev_arg;
   3104      1.1  christos 	REQUIRE(DNS_LCTX_VALID(lctx));
   3105      1.1  christos 
   3106      1.1  christos 	if (lctx->canceled)
   3107      1.1  christos 		result = ISC_R_CANCELED;
   3108      1.1  christos 	else
   3109      1.1  christos 		result = (lctx->load)(lctx);
   3110      1.1  christos 	if (result == DNS_R_CONTINUE) {
   3111      1.1  christos 		event->ev_arg = lctx;
   3112      1.1  christos 		isc_task_send(task, &event);
   3113      1.1  christos 	} else {
   3114      1.1  christos 		(lctx->done)(lctx->done_arg, result);
   3115      1.1  christos 		isc_event_free(&event);
   3116      1.1  christos 		dns_loadctx_detach(&lctx);
   3117      1.1  christos 	}
   3118      1.1  christos }
   3119      1.1  christos 
   3120      1.1  christos static isc_result_t
   3121      1.1  christos task_send(dns_loadctx_t *lctx) {
   3122      1.1  christos 	isc_event_t *event;
   3123      1.1  christos 
   3124      1.1  christos 	event = isc_event_allocate(lctx->mctx, NULL,
   3125      1.1  christos 				   DNS_EVENT_MASTERQUANTUM,
   3126      1.1  christos 				   load_quantum, lctx, sizeof(*event));
   3127      1.1  christos 	if (event == NULL)
   3128      1.1  christos 		return (ISC_R_NOMEMORY);
   3129      1.1  christos 	isc_task_send(lctx->task, &event);
   3130      1.1  christos 	return (ISC_R_SUCCESS);
   3131      1.1  christos }
   3132      1.1  christos 
   3133      1.1  christos void
   3134      1.1  christos dns_loadctx_cancel(dns_loadctx_t *lctx) {
   3135      1.1  christos 	REQUIRE(DNS_LCTX_VALID(lctx));
   3136      1.1  christos 
   3137      1.1  christos 	LOCK(&lctx->lock);
   3138  1.1.1.2  christos 	lctx->canceled = true;
   3139      1.1  christos 	UNLOCK(&lctx->lock);
   3140      1.1  christos }
   3141      1.1  christos 
   3142      1.1  christos void
   3143      1.1  christos dns_master_initrawheader(dns_masterrawheader_t *header) {
   3144      1.1  christos 	memset(header, 0, sizeof(dns_masterrawheader_t));
   3145      1.1  christos }
   3146