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