2 * Copyright (C) 2004-2009, 2011-2013 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
24 #include <isc/event.h>
26 #include <isc/magic.h>
28 #include <isc/print.h>
29 #include <isc/serial.h>
30 #include <isc/stdio.h>
31 #include <isc/stdtime.h>
32 #include <isc/string.h>
36 #include <dns/callbacks.h>
37 #include <dns/events.h>
38 #include <dns/fixedname.h>
39 #include <dns/master.h>
41 #include <dns/rdata.h>
42 #include <dns/rdataclass.h>
43 #include <dns/rdatalist.h>
44 #include <dns/rdataset.h>
45 #include <dns/rdatastruct.h>
46 #include <dns/rdatatype.h>
47 #include <dns/result.h>
53 * Grow the number of dns_rdatalist_t (#RDLSZ) and dns_rdata_t (#RDSZ) structures
54 * by these sizes when we need to.
57 /*% RDLSZ reflects the number of different types with the same name expected. */
60 * RDSZ reflects the number of rdata expected at a give name that can fit into
69 * Target buffer size and minimum target size.
70 * MINTSIZ must be big enough to hold the largest rdata record.
74 #define TSIZ (128*1024)
76 * max message size - header - root - type - class - ttl - rdlen
78 #define MINTSIZ DNS_RDATA_MAXLENGTH
80 * Size for tokens in the presentation format,
81 * The largest tokens are the base64 blocks in KEY and CERT records,
82 * Largest key allowed is about 1372 bytes but
83 * there is no fixed upper bound on CERT records.
84 * 2K is too small for some X.509s, 8K is overkill.
86 #define TOKENSIZ (8*1024)
89 * Buffers sizes for $GENERATE.
91 #define DNS_MASTER_LHS 2048
92 #define DNS_MASTER_RHS MINTSIZ
94 typedef ISC_LIST(dns_rdatalist_t) rdatalist_head_t;
96 typedef struct dns_incctx dns_incctx_t;
99 * Master file load state.
105 dns_masterformat_t format;
107 dns_rdatacallbacks_t *callbacks;
109 dns_loaddonefunc_t done;
113 isc_result_t (*openfile)(dns_loadctx_t *lctx,
114 const char *filename);
115 isc_result_t (*load)(dns_loadctx_t *lctx);
117 /* Members specific to the text format: */
119 isc_boolean_t keep_lex;
120 unsigned int options;
121 isc_boolean_t ttl_known;
122 isc_boolean_t default_ttl_known;
123 isc_boolean_t warn_1035;
124 isc_boolean_t warn_tcr;
125 isc_boolean_t warn_sigexpired;
126 isc_boolean_t seen_include;
128 isc_uint32_t default_ttl;
129 dns_rdataclass_t zclass;
130 dns_fixedname_t fixed_top;
131 dns_name_t *top; /*%< top of zone */
133 /* Members specific to the raw format: */
136 dns_masterrawheader_t header;
138 /* Which fixed buffers we are using? */
139 unsigned int loop_cnt; /*% records per quantum,
141 isc_boolean_t canceled;
145 isc_uint32_t references;
151 dns_incctx_t *parent;
155 dns_fixedname_t fixed[NBUFS]; /* working buffers */
156 unsigned int in_use[NBUFS]; /* covert to bitmap? */
160 isc_boolean_t origin_changed;
162 unsigned int glue_line;
163 unsigned int current_line;
166 #define DNS_LCTX_MAGIC ISC_MAGIC('L','c','t','x')
167 #define DNS_LCTX_VALID(lctx) ISC_MAGIC_VALID(lctx, DNS_LCTX_MAGIC)
169 #define DNS_AS_STR(t) ((t).value.as_textregion.base)
172 openfile_text(dns_loadctx_t *lctx, const char *master_file);
175 openfile_raw(dns_loadctx_t *lctx, const char *master_file);
178 load_text(dns_loadctx_t *lctx);
181 load_raw(dns_loadctx_t *lctx);
184 pushfile(const char *master_file, dns_name_t *origin, dns_loadctx_t *lctx);
187 commit(dns_rdatacallbacks_t *, dns_loadctx_t *, rdatalist_head_t *,
188 dns_name_t *, const char *, unsigned int);
191 is_glue(rdatalist_head_t *, dns_name_t *);
193 static dns_rdatalist_t *
194 grow_rdatalist(int, dns_rdatalist_t *, int, rdatalist_head_t *,
195 rdatalist_head_t *, isc_mem_t *mctx);
198 grow_rdata(int, dns_rdata_t *, int, rdatalist_head_t *, rdatalist_head_t *,
202 load_quantum(isc_task_t *task, isc_event_t *event);
205 task_send(dns_loadctx_t *lctx);
208 loadctx_destroy(dns_loadctx_t *lctx);
210 #define GETTOKEN(lexer, options, token, eol) \
212 result = gettoken(lexer, options, token, eol, callbacks); \
214 case ISC_R_SUCCESS: \
216 case ISC_R_UNEXPECTED: \
217 goto insist_and_cleanup; \
219 if (MANYERRS(lctx, result)) { \
220 SETRESULT(lctx, result); \
222 read_till_eol = ISC_TRUE; \
225 goto log_and_cleanup; \
227 if ((token)->type == isc_tokentype_special) { \
228 result = DNS_R_SYNTAX; \
229 if (MANYERRS(lctx, result)) { \
230 SETRESULT(lctx, result); \
232 read_till_eol = ISC_TRUE; \
235 goto log_and_cleanup; \
241 result = commit(callbacks, lctx, ¤t_list, \
242 ictx->current, source, ictx->current_line); \
243 if (MANYERRS(lctx, result)) { \
244 SETRESULT(lctx, result); \
245 } else if (result != ISC_R_SUCCESS) \
246 goto insist_and_cleanup; \
247 result = commit(callbacks, lctx, &glue_list, \
248 ictx->glue, source, ictx->glue_line); \
249 if (MANYERRS(lctx, result)) { \
250 SETRESULT(lctx, result); \
251 } else if (result != ISC_R_SUCCESS) \
252 goto insist_and_cleanup; \
255 isc_buffer_init(&target, target_mem, target_size); \
256 rdcount_save = rdcount; \
257 rdlcount_save = rdlcount; \
260 #define WARNUNEXPECTEDEOF(lexer) \
262 if (isc_lex_isfile(lexer)) \
263 (*callbacks->warn)(callbacks, \
264 "%s: file does not end with newline", \
270 GETTOKEN(lctx->lex, 0, &token, ISC_TRUE); \
271 if (token.type != isc_tokentype_eol) { \
272 isc_lex_ungettoken(lctx->lex, &token); \
273 result = DNS_R_EXTRATOKEN; \
274 if (MANYERRS(lctx, result)) { \
275 SETRESULT(lctx, result); \
277 read_till_eol = ISC_TRUE; \
279 } else if (result != ISC_R_SUCCESS) \
280 goto log_and_cleanup; \
284 #define MANYERRS(lctx, result) \
285 ((result != ISC_R_SUCCESS) && \
286 (result != ISC_R_IOERROR) && \
287 ((lctx)->options & DNS_MASTER_MANYERRORS) != 0)
289 #define SETRESULT(lctx, r) \
291 if ((lctx)->result == ISC_R_SUCCESS) \
292 (lctx)->result = r; \
295 #define LOGITFILE(result, filename) \
296 if (result == ISC_R_INVALIDFILE || result == ISC_R_FILENOTFOUND || \
297 result == ISC_R_IOERROR || result == ISC_R_TOOMANYOPENFILES || \
298 result == ISC_R_NOPERM) \
299 (*callbacks->error)(callbacks, "%s: %s:%lu: %s: %s", \
300 "dns_master_load", source, line, \
301 filename, dns_result_totext(result)); \
304 #define LOGIT(result) \
305 if (result == ISC_R_NOMEMORY) \
306 (*callbacks->error)(callbacks, "dns_master_load: %s", \
307 dns_result_totext(result)); \
309 (*callbacks->error)(callbacks, "%s: %s:%lu: %s", \
311 source, line, dns_result_totext(result))
314 static unsigned char in_addr_arpa_data[] = "\007IN-ADDR\004ARPA";
315 static unsigned char in_addr_arpa_offsets[] = { 0, 8, 13 };
316 static const dns_name_t in_addr_arpa =
319 in_addr_arpa_data, 14, 3,
320 DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE,
321 in_addr_arpa_offsets, NULL,
322 {(void *)-1, (void *)-1},
326 static unsigned char ip6_int_data[] = "\003IP6\003INT";
327 static unsigned char ip6_int_offsets[] = { 0, 4, 8 };
328 static const dns_name_t ip6_int =
332 DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE,
333 ip6_int_offsets, NULL,
334 {(void *)-1, (void *)-1},
338 static unsigned char ip6_arpa_data[] = "\003IP6\004ARPA";
339 static unsigned char ip6_arpa_offsets[] = { 0, 4, 9 };
340 static const dns_name_t ip6_arpa =
343 ip6_arpa_data, 10, 3,
344 DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE,
345 ip6_arpa_offsets, NULL,
346 {(void *)-1, (void *)-1},
351 static inline isc_result_t
352 gettoken(isc_lex_t *lex, unsigned int options, isc_token_t *token,
353 isc_boolean_t eol, dns_rdatacallbacks_t *callbacks)
357 options |= ISC_LEXOPT_EOL | ISC_LEXOPT_EOF | ISC_LEXOPT_DNSMULTILINE |
359 result = isc_lex_gettoken(lex, options, token);
360 if (result != ISC_R_SUCCESS) {
363 return (ISC_R_NOMEMORY);
365 (*callbacks->error)(callbacks,
366 "dns_master_load: %s:%lu:"
367 " isc_lex_gettoken() failed: %s",
368 isc_lex_getsourcename(lex),
369 isc_lex_getsourceline(lex),
370 isc_result_totext(result));
376 if (token->type == isc_tokentype_eol ||
377 token->type == isc_tokentype_eof) {
378 (*callbacks->error)(callbacks,
379 "dns_master_load: %s:%lu: unexpected end of %s",
380 isc_lex_getsourcename(lex),
381 isc_lex_getsourceline(lex),
385 return (ISC_R_UNEXPECTEDEND);
387 return (ISC_R_SUCCESS);
392 dns_loadctx_attach(dns_loadctx_t *source, dns_loadctx_t **target) {
394 REQUIRE(target != NULL && *target == NULL);
395 REQUIRE(DNS_LCTX_VALID(source));
398 INSIST(source->references > 0);
399 source->references++;
400 INSIST(source->references != 0); /* Overflow? */
401 UNLOCK(&source->lock);
407 dns_loadctx_detach(dns_loadctx_t **lctxp) {
409 isc_boolean_t need_destroy = ISC_FALSE;
411 REQUIRE(lctxp != NULL);
413 REQUIRE(DNS_LCTX_VALID(lctx));
416 INSIST(lctx->references > 0);
418 if (lctx->references == 0)
419 need_destroy = ISC_TRUE;
423 loadctx_destroy(lctx);
428 incctx_destroy(isc_mem_t *mctx, dns_incctx_t *ictx) {
429 dns_incctx_t *parent;
432 parent = ictx->parent;
435 isc_mem_put(mctx, ictx, sizeof(*ictx));
437 if (parent != NULL) {
444 loadctx_destroy(dns_loadctx_t *lctx) {
448 REQUIRE(DNS_LCTX_VALID(lctx));
451 if (lctx->inc != NULL)
452 incctx_destroy(lctx->mctx, lctx->inc);
454 if (lctx->f != NULL) {
455 result = isc_stdio_close(lctx->f);
456 if (result != ISC_R_SUCCESS) {
457 UNEXPECTED_ERROR(__FILE__, __LINE__,
458 "isc_stdio_close() failed: %s",
459 isc_result_totext(result));
463 /* isc_lex_destroy() will close all open streams */
464 if (lctx->lex != NULL && !lctx->keep_lex)
465 isc_lex_destroy(&lctx->lex);
467 if (lctx->task != NULL)
468 isc_task_detach(&lctx->task);
469 DESTROYLOCK(&lctx->lock);
471 isc_mem_attach(lctx->mctx, &mctx);
472 isc_mem_detach(&lctx->mctx);
473 isc_mem_put(mctx, lctx, sizeof(*lctx));
474 isc_mem_detach(&mctx);
478 incctx_create(isc_mem_t *mctx, dns_name_t *origin, dns_incctx_t **ictxp) {
483 ictx = isc_mem_get(mctx, sizeof(*ictx));
485 return (ISC_R_NOMEMORY);
487 for (i = 0; i < NBUFS; i++) {
488 dns_fixedname_init(&ictx->fixed[i]);
489 ictx->in_use[i] = ISC_FALSE;
492 ictx->origin_in_use = 0;
493 ictx->origin = dns_fixedname_name(&ictx->fixed[ictx->origin_in_use]);
494 ictx->in_use[ictx->origin_in_use] = ISC_TRUE;
495 dns_name_toregion(origin, &r);
496 dns_name_fromregion(ictx->origin, &r);
499 ictx->current = NULL;
500 ictx->glue_in_use = -1;
501 ictx->current_in_use = -1;
503 ictx->drop = ISC_FALSE;
505 ictx->current_line = 0;
508 return (ISC_R_SUCCESS);
512 loadctx_create(dns_masterformat_t format, isc_mem_t *mctx,
513 unsigned int options, isc_uint32_t resign, dns_name_t *top,
514 dns_rdataclass_t zclass, dns_name_t *origin,
515 dns_rdatacallbacks_t *callbacks, isc_task_t *task,
516 dns_loaddonefunc_t done, void *done_arg, isc_lex_t *lex,
517 dns_loadctx_t **lctxp)
522 isc_lexspecials_t specials;
524 REQUIRE(lctxp != NULL && *lctxp == NULL);
525 REQUIRE(callbacks != NULL);
526 REQUIRE(callbacks->add != NULL);
527 REQUIRE(callbacks->error != NULL);
528 REQUIRE(callbacks->warn != NULL);
529 REQUIRE(mctx != NULL);
530 REQUIRE(dns_name_isabsolute(top));
531 REQUIRE(dns_name_isabsolute(origin));
532 REQUIRE((task == NULL && done == NULL) ||
533 (task != NULL && done != NULL));
535 lctx = isc_mem_get(mctx, sizeof(*lctx));
537 return (ISC_R_NOMEMORY);
538 result = isc_mutex_init(&lctx->lock);
539 if (result != ISC_R_SUCCESS) {
540 isc_mem_put(mctx, lctx, sizeof(*lctx));
545 result = incctx_create(mctx, origin, &lctx->inc);
546 if (result != ISC_R_SUCCESS)
549 lctx->format = format;
553 case dns_masterformat_text:
554 lctx->openfile = openfile_text;
555 lctx->load = load_text;
557 case dns_masterformat_raw:
558 lctx->openfile = openfile_raw;
559 lctx->load = load_raw;
565 lctx->keep_lex = ISC_TRUE;
568 result = isc_lex_create(mctx, TOKENSIZ, &lctx->lex);
569 if (result != ISC_R_SUCCESS)
571 lctx->keep_lex = ISC_FALSE;
572 memset(specials, 0, sizeof(specials));
577 isc_lex_setspecials(lctx->lex, specials);
578 isc_lex_setcomments(lctx->lex, ISC_LEXCOMMENT_DNSMASTERFILE);
581 lctx->ttl_known = ISC_TF((options & DNS_MASTER_NOTTL) != 0);
583 lctx->default_ttl_known = lctx->ttl_known;
584 lctx->default_ttl = 0;
585 lctx->warn_1035 = ISC_TRUE; /* XXX Argument? */
586 lctx->warn_tcr = ISC_TRUE; /* XXX Argument? */
587 lctx->warn_sigexpired = ISC_TRUE; /* XXX Argument? */
588 lctx->options = options;
589 lctx->seen_include = ISC_FALSE;
590 lctx->zclass = zclass;
591 lctx->resign = resign;
592 lctx->result = ISC_R_SUCCESS;
594 dns_fixedname_init(&lctx->fixed_top);
595 lctx->top = dns_fixedname_name(&lctx->fixed_top);
596 dns_name_toregion(top, &r);
597 dns_name_fromregion(lctx->top, &r);
600 lctx->first = ISC_TRUE;
601 dns_master_initrawheader(&lctx->header);
603 lctx->loop_cnt = (done != NULL) ? 100 : 0;
604 lctx->callbacks = callbacks;
607 isc_task_attach(task, &lctx->task);
609 lctx->done_arg = done_arg;
610 lctx->canceled = ISC_FALSE;
612 isc_mem_attach(mctx, &lctx->mctx);
613 lctx->references = 1; /* Implicit attach. */
614 lctx->magic = DNS_LCTX_MAGIC;
616 return (ISC_R_SUCCESS);
619 incctx_destroy(mctx, lctx->inc);
621 isc_mem_put(mctx, lctx, sizeof(*lctx));
625 static const char *hex = "0123456789abcdef0123456789ABCDEF";
628 * Convert value into a nibble sequence from least significant to most
629 * significant nibble. Zero fill upper most significant nibbles if
630 * required to make the width.
632 * Returns the number of characters that should have been written without
633 * counting the terminating NUL.
636 nibbles(char *numbuf, size_t length, unsigned int width, char mode, int value) {
637 unsigned int count = 0;
640 * This reserve space for the NUL string terminator.
647 char val = hex[(value & 0x0f) + ((mode == 'n') ? 0 : 16)];
658 * If width is non zero then we need to add a label seperator.
659 * If value is non zero then we need to add another label and
660 * that requires a label seperator.
662 if (width > 0 || value != 0) {
672 } while (value != 0 || width > 0);
677 genname(char *name, int it, char *buffer, size_t length) {
678 char fmt[sizeof("%04000000000d")];
686 isc_boolean_t nibblemode;
689 r.length = (unsigned int)length;
691 while (*name != '\0') {
696 return (ISC_R_NOSPACE);
698 isc_textregion_consume(&r, 1);
701 nibblemode = ISC_FALSE;
703 /* Get format specifier. */
705 n = sscanf(name, "{%d,%u,%1[doxXnN]}",
706 &delta, &width, mode);
711 n = snprintf(fmt, sizeof(fmt),
715 if (mode[0] == 'n' || mode[0] == 'N')
716 nibblemode = ISC_TRUE;
717 n = snprintf(fmt, sizeof(fmt),
718 "%%0%u%c", width, mode[0]);
721 return (DNS_R_SYNTAX);
723 if (n >= sizeof(fmt))
724 return (ISC_R_NOSPACE);
725 /* Skip past closing brace. */
726 while (*name != '\0' && *name++ != '}')
730 n = nibbles(numbuf, sizeof(numbuf), width,
731 mode[0], it + delta);
733 n = snprintf(numbuf, sizeof(numbuf), fmt,
735 if (n >= sizeof(numbuf))
736 return (ISC_R_NOSPACE);
738 while (*cp != '\0') {
740 return (ISC_R_NOSPACE);
742 isc_textregion_consume(&r, 1);
744 } else if (*name == '\\') {
746 return (ISC_R_NOSPACE);
748 isc_textregion_consume(&r, 1);
752 return (ISC_R_NOSPACE);
754 isc_textregion_consume(&r, 1);
757 return (ISC_R_NOSPACE);
759 isc_textregion_consume(&r, 1);
763 return (ISC_R_NOSPACE);
765 return (ISC_R_SUCCESS);
769 openfile_text(dns_loadctx_t *lctx, const char *master_file) {
770 return (isc_lex_openfile(lctx->lex, master_file));
774 openfile_raw(dns_loadctx_t *lctx, const char *master_file) {
777 result = isc_stdio_open(master_file, "rb", &lctx->f);
778 if (result != ISC_R_SUCCESS && result != ISC_R_FILENOTFOUND) {
779 UNEXPECTED_ERROR(__FILE__, __LINE__,
780 "isc_stdio_open() failed: %s",
781 isc_result_totext(result));
788 generate(dns_loadctx_t *lctx, char *range, char *lhs, char *gtype, char *rhs,
789 const char *source, unsigned int line)
791 char *target_mem = NULL;
794 dns_fixedname_t ownerfixed;
796 dns_rdata_t rdata = DNS_RDATA_INIT;
797 dns_rdatacallbacks_t *callbacks;
798 dns_rdatalist_t rdatalist;
799 dns_rdatatype_t type;
800 rdatalist_head_t head;
802 int target_size = MINTSIZ; /* only one rdata at a time */
807 unsigned int start, stop, step, i;
811 callbacks = lctx->callbacks;
812 dns_fixedname_init(&ownerfixed);
813 owner = dns_fixedname_name(&ownerfixed);
816 target_mem = isc_mem_get(lctx->mctx, target_size);
817 rhsbuf = isc_mem_get(lctx->mctx, DNS_MASTER_RHS);
818 lhsbuf = isc_mem_get(lctx->mctx, DNS_MASTER_LHS);
819 if (target_mem == NULL || rhsbuf == NULL || lhsbuf == NULL) {
820 result = ISC_R_NOMEMORY;
823 isc_buffer_init(&target, target_mem, target_size);
825 n = sscanf(range, "%u-%u/%u", &start, &stop, &step);
826 if (n < 2 || stop < start) {
827 (*callbacks->error)(callbacks,
828 "%s: %s:%lu: invalid range '%s'",
829 "$GENERATE", source, line, range);
830 result = DNS_R_SYNTAX;
840 r.length = strlen(gtype);
841 result = dns_rdatatype_fromtext(&type, &r);
842 if (result != ISC_R_SUCCESS) {
843 (*callbacks->error)(callbacks,
844 "%s: %s:%lu: unknown RR type '%s'",
845 "$GENERATE", source, line, gtype);
849 ISC_LIST_INIT(rdatalist.rdata);
850 ISC_LINK_INIT(&rdatalist, link);
851 for (i = start; i <= stop; i += step) {
852 result = genname(lhs, i, lhsbuf, DNS_MASTER_LHS);
853 if (result != ISC_R_SUCCESS)
855 result = genname(rhs, i, rhsbuf, DNS_MASTER_RHS);
856 if (result != ISC_R_SUCCESS)
859 isc_buffer_init(&buffer, lhsbuf, strlen(lhsbuf));
860 isc_buffer_add(&buffer, strlen(lhsbuf));
861 isc_buffer_setactive(&buffer, strlen(lhsbuf));
862 result = dns_name_fromtext(owner, &buffer, ictx->origin,
864 if (result != ISC_R_SUCCESS)
867 if ((lctx->options & DNS_MASTER_ZONE) != 0 &&
868 (lctx->options & DNS_MASTER_SLAVE) == 0 &&
869 (lctx->options & DNS_MASTER_KEY) == 0 &&
870 !dns_name_issubdomain(owner, lctx->top))
872 char namebuf[DNS_NAME_FORMATSIZE];
873 dns_name_format(owner, namebuf, sizeof(namebuf));
875 * Ignore out-of-zone data.
877 (*callbacks->warn)(callbacks,
879 "ignoring out-of-zone data (%s)",
880 source, line, namebuf);
884 isc_buffer_init(&buffer, rhsbuf, strlen(rhsbuf));
885 isc_buffer_add(&buffer, strlen(rhsbuf));
886 isc_buffer_setactive(&buffer, strlen(rhsbuf));
888 result = isc_lex_openbuffer(lctx->lex, &buffer);
889 if (result != ISC_R_SUCCESS)
892 isc_buffer_init(&target, target_mem, target_size);
893 result = dns_rdata_fromtext(&rdata, lctx->zclass, type,
894 lctx->lex, ictx->origin, 0,
895 lctx->mctx, &target, callbacks);
896 RUNTIME_CHECK(isc_lex_close(lctx->lex) == ISC_R_SUCCESS);
897 if (result != ISC_R_SUCCESS)
900 rdatalist.type = type;
901 rdatalist.covers = 0;
902 rdatalist.rdclass = lctx->zclass;
903 rdatalist.ttl = lctx->ttl;
904 ISC_LIST_PREPEND(head, &rdatalist, link);
905 ISC_LIST_APPEND(rdatalist.rdata, &rdata, link);
906 result = commit(callbacks, lctx, &head, owner, source, line);
907 ISC_LIST_UNLINK(rdatalist.rdata, &rdata, link);
908 if (result != ISC_R_SUCCESS)
910 dns_rdata_reset(&rdata);
912 result = ISC_R_SUCCESS;
916 if (result == ISC_R_NOMEMORY)
917 (*callbacks->error)(callbacks, "$GENERATE: %s",
918 dns_result_totext(result));
920 (*callbacks->error)(callbacks, "$GENERATE: %s:%lu: %s",
921 source, line, dns_result_totext(result));
924 INSIST(result != ISC_R_SUCCESS);
927 if (target_mem != NULL)
928 isc_mem_put(lctx->mctx, target_mem, target_size);
930 isc_mem_put(lctx->mctx, lhsbuf, DNS_MASTER_LHS);
932 isc_mem_put(lctx->mctx, rhsbuf, DNS_MASTER_RHS);
937 limit_ttl(dns_rdatacallbacks_t *callbacks, const char *source, unsigned int line,
940 if (*ttlp > 0x7fffffffUL) {
941 (callbacks->warn)(callbacks,
943 "$TTL %lu > MAXTTL, "
953 check_ns(dns_loadctx_t *lctx, isc_token_t *token, const char *source,
957 isc_result_t result = ISC_R_SUCCESS;
958 void (*callback)(struct dns_rdatacallbacks *, const char *, ...);
960 if ((lctx->options & DNS_MASTER_FATALNS) != 0)
961 callback = lctx->callbacks->error;
963 callback = lctx->callbacks->warn;
965 if (token->type == isc_tokentype_string) {
967 struct in6_addr addr6;
969 tmp = isc_mem_strdup(lctx->mctx, DNS_AS_STR(*token));
971 return (ISC_R_NOMEMORY);
973 * Catch both "1.2.3.4" and "1.2.3.4."
975 if (tmp[strlen(tmp) - 1] == '.')
976 tmp[strlen(tmp) - 1] = '\0';
977 if (inet_aton(tmp, &addr) == 1 ||
978 inet_pton(AF_INET6, tmp, &addr6) == 1)
979 result = DNS_R_NSISADDRESS;
981 if (result != ISC_R_SUCCESS)
982 (*callback)(lctx->callbacks, "%s:%lu: NS record '%s' "
983 "appears to be an address",
984 source, line, DNS_AS_STR(*token));
986 isc_mem_free(lctx->mctx, tmp);
991 check_wildcard(dns_incctx_t *ictx, const char *source, unsigned long line,
992 dns_rdatacallbacks_t *callbacks)
996 name = (ictx->glue != NULL) ? ictx->glue : ictx->current;
997 if (dns_name_internalwildcard(name)) {
998 char namebuf[DNS_NAME_FORMATSIZE];
1000 dns_name_format(name, namebuf, sizeof(namebuf));
1001 (*callbacks->warn)(callbacks, "%s:%lu: warning: ownername "
1002 "'%s' contains an non-terminal wildcard",
1003 source, line, namebuf);
1008 load_text(dns_loadctx_t *lctx) {
1009 dns_rdataclass_t rdclass;
1010 dns_rdatatype_t type, covers;
1011 isc_uint32_t ttl_offset = 0;
1012 dns_name_t *new_name;
1013 isc_boolean_t current_has_delegation = ISC_FALSE;
1014 isc_boolean_t done = ISC_FALSE;
1015 isc_boolean_t finish_origin = ISC_FALSE;
1016 isc_boolean_t finish_include = ISC_FALSE;
1017 isc_boolean_t read_till_eol = ISC_FALSE;
1018 isc_boolean_t initialws;
1019 char *include_file = NULL;
1021 isc_result_t result = ISC_R_UNEXPECTED;
1022 rdatalist_head_t glue_list;
1023 rdatalist_head_t current_list;
1024 dns_rdatalist_t *this;
1025 dns_rdatalist_t *rdatalist = NULL;
1026 dns_rdatalist_t *new_rdatalist;
1028 int rdlcount_save = 0;
1029 int rdatalist_size = 0;
1030 isc_buffer_t buffer;
1031 isc_buffer_t target;
1032 isc_buffer_t target_ft;
1033 isc_buffer_t target_save;
1034 dns_rdata_t *rdata = NULL;
1035 dns_rdata_t *new_rdata;
1037 int rdcount_save = 0;
1039 unsigned char *target_mem = NULL;
1040 int target_size = TSIZ;
1042 unsigned int loop_cnt = 0;
1044 dns_rdatacallbacks_t *callbacks;
1050 const char *source = "";
1051 unsigned long line = 0;
1052 isc_boolean_t explicit_ttl;
1054 char classname1[DNS_RDATACLASS_FORMATSIZE];
1055 char classname2[DNS_RDATACLASS_FORMATSIZE];
1056 unsigned int options = 0;
1058 REQUIRE(DNS_LCTX_VALID(lctx));
1059 callbacks = lctx->callbacks;
1063 ISC_LIST_INIT(glue_list);
1064 ISC_LIST_INIT(current_list);
1066 isc_stdtime_get(&now);
1069 * Allocate target_size of buffer space. This is greater than twice
1070 * the maximum individual RR data size.
1072 target_mem = isc_mem_get(mctx, target_size);
1073 if (target_mem == NULL) {
1074 result = ISC_R_NOMEMORY;
1075 goto log_and_cleanup;
1077 isc_buffer_init(&target, target_mem, target_size);
1078 target_save = target;
1080 if ((lctx->options & DNS_MASTER_CHECKNAMES) != 0)
1081 options |= DNS_RDATA_CHECKNAMES;
1082 if ((lctx->options & DNS_MASTER_CHECKNAMESFAIL) != 0)
1083 options |= DNS_RDATA_CHECKNAMESFAIL;
1084 if ((lctx->options & DNS_MASTER_CHECKMX) != 0)
1085 options |= DNS_RDATA_CHECKMX;
1086 if ((lctx->options & DNS_MASTER_CHECKMXFAIL) != 0)
1087 options |= DNS_RDATA_CHECKMXFAIL;
1088 source = isc_lex_getsourcename(lctx->lex);
1090 initialws = ISC_FALSE;
1091 line = isc_lex_getsourceline(lctx->lex);
1092 GETTOKEN(lctx->lex, ISC_LEXOPT_INITIALWS | ISC_LEXOPT_QSTRING,
1094 line = isc_lex_getsourceline(lctx->lex);
1096 if (token.type == isc_tokentype_eof) {
1098 WARNUNEXPECTEDEOF(lctx->lex);
1099 /* Pop the include stack? */
1100 if (ictx->parent != NULL) {
1102 lctx->inc = ictx->parent;
1103 ictx->parent = NULL;
1104 incctx_destroy(lctx->mctx, ictx);
1105 RUNTIME_CHECK(isc_lex_close(lctx->lex) == ISC_R_SUCCESS);
1106 line = isc_lex_getsourceline(lctx->lex);
1107 source = isc_lex_getsourcename(lctx->lex);
1116 if (token.type == isc_tokentype_eol) {
1117 read_till_eol = ISC_FALSE;
1118 continue; /* blank line */
1124 if (token.type == isc_tokentype_initialws) {
1126 * Still working on the same name.
1128 initialws = ISC_TRUE;
1129 } else if (token.type == isc_tokentype_string ||
1130 token.type == isc_tokentype_qstring) {
1135 * "$ORIGIN" and "$INCLUDE" can both take domain names.
1136 * The processing of "$ORIGIN" and "$INCLUDE" extends
1137 * across the normal domain name processing.
1140 if (strcasecmp(DNS_AS_STR(token), "$ORIGIN") == 0) {
1141 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1142 finish_origin = ISC_TRUE;
1143 } else if (strcasecmp(DNS_AS_STR(token),
1145 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1147 dns_ttl_fromtext(&token.value.as_textregion,
1149 if (MANYERRS(lctx, result)) {
1150 SETRESULT(lctx, result);
1152 } else if (result != ISC_R_SUCCESS)
1153 goto insist_and_cleanup;
1154 limit_ttl(callbacks, source, line, &lctx->ttl);
1155 lctx->default_ttl = lctx->ttl;
1156 lctx->default_ttl_known = ISC_TRUE;
1159 } else if (strcasecmp(DNS_AS_STR(token),
1162 if ((lctx->options & DNS_MASTER_NOINCLUDE)
1165 (callbacks->error)(callbacks,
1166 "%s: %s:%lu: $INCLUDE not allowed",
1169 result = DNS_R_REFUSED;
1170 goto insist_and_cleanup;
1172 if (ttl_offset != 0) {
1173 (callbacks->error)(callbacks,
1174 "%s: %s:%lu: $INCLUDE "
1175 "may not be used with $DATE",
1178 result = DNS_R_SYNTAX;
1179 goto insist_and_cleanup;
1181 GETTOKEN(lctx->lex, ISC_LEXOPT_QSTRING, &token,
1183 if (include_file != NULL)
1184 isc_mem_free(mctx, include_file);
1185 include_file = isc_mem_strdup(mctx,
1187 if (include_file == NULL) {
1188 result = ISC_R_NOMEMORY;
1189 goto log_and_cleanup;
1191 GETTOKEN(lctx->lex, 0, &token, ISC_TRUE);
1193 if (token.type == isc_tokentype_eol ||
1194 token.type == isc_tokentype_eof) {
1195 if (token.type == isc_tokentype_eof)
1196 WARNUNEXPECTEDEOF(lctx->lex);
1197 isc_lex_ungettoken(lctx->lex, &token);
1201 result = pushfile(include_file,
1202 ictx->origin, lctx);
1203 if (MANYERRS(lctx, result)) {
1204 SETRESULT(lctx, result);
1205 LOGITFILE(result, include_file);
1207 } else if (result != ISC_R_SUCCESS) {
1208 LOGITFILE(result, include_file);
1209 goto insist_and_cleanup;
1213 isc_lex_getsourcename(lctx->lex);
1214 line = isc_lex_getsourceline(lctx->lex);
1219 * There is an origin field. Fall through
1220 * to domain name processing code and do
1221 * the actual inclusion later.
1223 finish_include = ISC_TRUE;
1224 } else if (strcasecmp(DNS_AS_STR(token),
1226 isc_int64_t dump_time64;
1227 isc_stdtime_t dump_time, current_time;
1228 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1229 isc_stdtime_get(¤t_time);
1230 result = dns_time64_fromtext(DNS_AS_STR(token),
1232 if (MANYERRS(lctx, result)) {
1233 SETRESULT(lctx, result);
1236 } else if (result != ISC_R_SUCCESS)
1237 goto log_and_cleanup;
1238 dump_time = (isc_stdtime_t)dump_time64;
1239 if (dump_time != dump_time64) {
1240 UNEXPECTED_ERROR(__FILE__, __LINE__,
1241 "%s: %s:%lu: $DATE outside epoch",
1242 "dns_master_load", source, line);
1243 result = ISC_R_UNEXPECTED;
1244 goto insist_and_cleanup;
1246 if (dump_time > current_time) {
1247 UNEXPECTED_ERROR(__FILE__, __LINE__,
1249 "$DATE in future, using current date",
1250 "dns_master_load", source, line);
1251 dump_time = current_time;
1253 ttl_offset = current_time - dump_time;
1256 } else if (strcasecmp(DNS_AS_STR(token),
1257 "$GENERATE") == 0) {
1262 isc_mem_free(mctx, range);
1264 isc_mem_free(mctx, lhs);
1266 isc_mem_free(mctx, gtype);
1268 isc_mem_free(mctx, rhs);
1269 range = lhs = gtype = rhs = NULL;
1271 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1272 range = isc_mem_strdup(mctx,
1274 if (range == NULL) {
1275 result = ISC_R_NOMEMORY;
1276 goto log_and_cleanup;
1279 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1280 lhs = isc_mem_strdup(mctx, DNS_AS_STR(token));
1282 result = ISC_R_NOMEMORY;
1283 goto log_and_cleanup;
1286 explicit_ttl = ISC_FALSE;
1288 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1289 if (dns_rdataclass_fromtext(&rdclass,
1290 &token.value.as_textregion)
1292 GETTOKEN(lctx->lex, 0, &token,
1296 if (dns_ttl_fromtext(&token.value.as_textregion,
1299 limit_ttl(callbacks, source, line,
1301 lctx->ttl_known = ISC_TRUE;
1302 explicit_ttl = ISC_TRUE;
1303 GETTOKEN(lctx->lex, 0, &token,
1308 dns_rdataclass_fromtext(&rdclass,
1309 &token.value.as_textregion)
1311 GETTOKEN(lctx->lex, 0, &token,
1314 gtype = isc_mem_strdup(mctx,
1316 if (gtype == NULL) {
1317 result = ISC_R_NOMEMORY;
1318 goto log_and_cleanup;
1321 GETTOKEN(lctx->lex, ISC_LEXOPT_QSTRING,
1323 rhs = isc_mem_strdup(mctx, DNS_AS_STR(token));
1325 result = ISC_R_NOMEMORY;
1326 goto log_and_cleanup;
1328 if (!lctx->ttl_known &&
1329 !lctx->default_ttl_known) {
1330 (*callbacks->error)(callbacks,
1331 "%s: %s:%lu: no TTL specified",
1332 "dns_master_load", source, line);
1333 result = DNS_R_NOTTL;
1334 if (MANYERRS(lctx, result)) {
1335 SETRESULT(lctx, result);
1337 } else if (result != ISC_R_SUCCESS)
1338 goto insist_and_cleanup;
1339 } else if (!explicit_ttl &&
1340 lctx->default_ttl_known) {
1341 lctx->ttl = lctx->default_ttl;
1344 * If the class specified does not match the
1345 * zone's class print out a error message and
1348 if (rdclass != 0 && rdclass != lctx->zclass) {
1351 result = generate(lctx, range, lhs, gtype, rhs,
1353 if (MANYERRS(lctx, result)) {
1354 SETRESULT(lctx, result);
1355 } else if (result != ISC_R_SUCCESS)
1356 goto insist_and_cleanup;
1359 } else if (strncasecmp(DNS_AS_STR(token),
1361 (callbacks->error)(callbacks,
1363 "unknown $ directive '%s'",
1364 "dns_master_load", source, line,
1366 result = DNS_R_SYNTAX;
1367 if (MANYERRS(lctx, result)) {
1368 SETRESULT(lctx, result);
1369 } else if (result != ISC_R_SUCCESS)
1370 goto insist_and_cleanup;
1374 * Normal processing resumes.
1376 * Find a free name buffer.
1378 for (new_in_use = 0; new_in_use < NBUFS; new_in_use++)
1379 if (!ictx->in_use[new_in_use])
1381 INSIST(new_in_use < NBUFS);
1382 dns_fixedname_init(&ictx->fixed[new_in_use]);
1383 new_name = dns_fixedname_name(&ictx->fixed[new_in_use]);
1384 isc_buffer_init(&buffer, token.value.as_region.base,
1385 token.value.as_region.length);
1386 isc_buffer_add(&buffer, token.value.as_region.length);
1387 isc_buffer_setactive(&buffer,
1388 token.value.as_region.length);
1389 result = dns_name_fromtext(new_name, &buffer,
1390 ictx->origin, 0, NULL);
1391 if (MANYERRS(lctx, result)) {
1392 SETRESULT(lctx, result);
1394 read_till_eol = ISC_TRUE;
1396 } else if (result != ISC_R_SUCCESS)
1397 goto log_and_cleanup;
1400 * Finish $ORIGIN / $INCLUDE processing if required.
1402 if (finish_origin) {
1403 if (ictx->origin_in_use != -1)
1404 ictx->in_use[ictx->origin_in_use] =
1406 ictx->origin_in_use = new_in_use;
1407 ictx->in_use[ictx->origin_in_use] = ISC_TRUE;
1408 ictx->origin = new_name;
1409 ictx->origin_changed = ISC_TRUE;
1410 finish_origin = ISC_FALSE;
1414 if (finish_include) {
1415 finish_include = ISC_FALSE;
1416 result = pushfile(include_file, new_name, lctx);
1417 if (MANYERRS(lctx, result)) {
1418 SETRESULT(lctx, result);
1419 LOGITFILE(result, include_file);
1421 } else if (result != ISC_R_SUCCESS) {
1422 LOGITFILE(result, include_file);
1423 goto insist_and_cleanup;
1426 source = isc_lex_getsourcename(lctx->lex);
1427 line = isc_lex_getsourceline(lctx->lex);
1433 * "$" Processing Finished
1437 * If we are processing glue and the new name does
1438 * not match the current glue name, commit the glue
1439 * and pop stacks leaving us in 'normal' processing
1440 * state. Linked lists are undone by commit().
1442 if (ictx->glue != NULL &&
1443 dns_name_compare(ictx->glue, new_name) != 0) {
1444 result = commit(callbacks, lctx, &glue_list,
1447 if (MANYERRS(lctx, result)) {
1448 SETRESULT(lctx, result);
1449 } else if (result != ISC_R_SUCCESS)
1450 goto insist_and_cleanup;
1451 if (ictx->glue_in_use != -1)
1452 ictx->in_use[ictx->glue_in_use] =
1454 ictx->glue_in_use = -1;
1456 rdcount = rdcount_save;
1457 rdlcount = rdlcount_save;
1458 target = target_save;
1462 * If we are in 'normal' processing state and the new
1463 * name does not match the current name, see if the
1464 * new name is for glue and treat it as such,
1465 * otherwise we have a new name so commit what we
1468 if ((ictx->glue == NULL) && (ictx->current == NULL ||
1469 dns_name_compare(ictx->current, new_name) != 0)) {
1470 if (current_has_delegation &&
1471 is_glue(¤t_list, new_name)) {
1472 rdcount_save = rdcount;
1473 rdlcount_save = rdlcount;
1474 target_save = target;
1475 ictx->glue = new_name;
1476 ictx->glue_in_use = new_in_use;
1477 ictx->in_use[ictx->glue_in_use] =
1480 result = commit(callbacks, lctx,
1484 ictx->current_line);
1485 if (MANYERRS(lctx, result)) {
1486 SETRESULT(lctx, result);
1487 } else if (result != ISC_R_SUCCESS)
1488 goto insist_and_cleanup;
1491 if (ictx->current_in_use != -1)
1492 ictx->in_use[ictx->current_in_use] =
1494 ictx->current_in_use = new_in_use;
1495 ictx->in_use[ictx->current_in_use] =
1497 ictx->current = new_name;
1498 current_has_delegation = ISC_FALSE;
1499 isc_buffer_init(&target, target_mem,
1503 * Check for internal wildcards.
1505 if ((lctx->options & DNS_MASTER_CHECKWILDCARD)
1507 check_wildcard(ictx, source, line,
1511 if ((lctx->options & DNS_MASTER_ZONE) != 0 &&
1512 (lctx->options & DNS_MASTER_SLAVE) == 0 &&
1513 (lctx->options & DNS_MASTER_KEY) == 0 &&
1514 !dns_name_issubdomain(new_name, lctx->top))
1516 char namebuf[DNS_NAME_FORMATSIZE];
1517 dns_name_format(new_name, namebuf,
1520 * Ignore out-of-zone data.
1522 (*callbacks->warn)(callbacks,
1524 "ignoring out-of-zone data (%s)",
1525 source, line, namebuf);
1526 ictx->drop = ISC_TRUE;
1528 ictx->drop = ISC_FALSE;
1530 UNEXPECTED_ERROR(__FILE__, __LINE__,
1531 "%s:%lu: isc_lex_gettoken() returned "
1532 "unexpected token type (%d)",
1533 source, line, token.type);
1534 result = ISC_R_UNEXPECTED;
1535 if (MANYERRS(lctx, result)) {
1536 SETRESULT(lctx, result);
1539 } else if (result != ISC_R_SUCCESS)
1540 goto insist_and_cleanup;
1544 * Find TTL, class and type. Both TTL and class are optional
1545 * and may occur in any order if they exist. TTL and class
1546 * come before type which must exist.
1548 * [<TTL>] [<class>] <type> <RDATA>
1549 * [<class>] [<TTL>] <type> <RDATA>
1555 GETTOKEN(lctx->lex, 0, &token, initialws);
1558 if (token.type == isc_tokentype_eol) {
1559 read_till_eol = ISC_FALSE;
1560 continue; /* blank line */
1563 if (token.type == isc_tokentype_eof) {
1564 WARNUNEXPECTEDEOF(lctx->lex);
1565 read_till_eol = ISC_FALSE;
1566 isc_lex_ungettoken(lctx->lex, &token);
1570 if (ictx->current == NULL) {
1571 (*callbacks->error)(callbacks,
1572 "%s:%lu: no current owner name",
1574 result = DNS_R_NOOWNER;
1575 if (MANYERRS(lctx, result)) {
1576 SETRESULT(lctx, result);
1577 read_till_eol = ISC_TRUE;
1579 } else if (result != ISC_R_SUCCESS)
1580 goto insist_and_cleanup;
1583 if (ictx->origin_changed) {
1584 char cbuf[DNS_NAME_FORMATSIZE];
1585 char obuf[DNS_NAME_FORMATSIZE];
1586 dns_name_format(ictx->current, cbuf,
1588 dns_name_format(ictx->origin, obuf,
1590 (*callbacks->warn)(callbacks,
1591 "%s:%lu: record with inherited "
1592 "owner (%s) immediately after "
1593 "$ORIGIN (%s)", source, line,
1598 ictx->origin_changed = ISC_FALSE;
1600 if (dns_rdataclass_fromtext(&rdclass,
1601 &token.value.as_textregion)
1603 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1605 explicit_ttl = ISC_FALSE;
1606 if (dns_ttl_fromtext(&token.value.as_textregion, &lctx->ttl)
1608 limit_ttl(callbacks, source, line, &lctx->ttl);
1609 explicit_ttl = ISC_TRUE;
1610 lctx->ttl_known = ISC_TRUE;
1611 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1614 if (token.type != isc_tokentype_string) {
1615 UNEXPECTED_ERROR(__FILE__, __LINE__,
1616 "isc_lex_gettoken() returned unexpected token type");
1617 result = ISC_R_UNEXPECTED;
1618 if (MANYERRS(lctx, result)) {
1619 SETRESULT(lctx, result);
1620 read_till_eol = ISC_TRUE;
1622 } else if (result != ISC_R_SUCCESS)
1623 goto insist_and_cleanup;
1627 dns_rdataclass_fromtext(&rdclass,
1628 &token.value.as_textregion)
1630 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1632 if (token.type != isc_tokentype_string) {
1633 UNEXPECTED_ERROR(__FILE__, __LINE__,
1634 "isc_lex_gettoken() returned unexpected token type");
1635 result = ISC_R_UNEXPECTED;
1636 if (MANYERRS(lctx, result)) {
1637 SETRESULT(lctx, result);
1638 read_till_eol = ISC_TRUE;
1640 } else if (result != ISC_R_SUCCESS)
1641 goto insist_and_cleanup;
1644 result = dns_rdatatype_fromtext(&type,
1645 &token.value.as_textregion);
1646 if (result != ISC_R_SUCCESS) {
1647 (*callbacks->warn)(callbacks,
1648 "%s:%lu: unknown RR type '%.*s'",
1650 token.value.as_textregion.length,
1651 token.value.as_textregion.base);
1652 if (MANYERRS(lctx, result)) {
1653 SETRESULT(lctx, result);
1654 read_till_eol = ISC_TRUE;
1656 } else if (result != ISC_R_SUCCESS)
1657 goto insist_and_cleanup;
1661 * If the class specified does not match the zone's class
1662 * print out a error message and exit.
1664 if (rdclass != 0 && rdclass != lctx->zclass) {
1667 dns_rdataclass_format(rdclass, classname1,
1668 sizeof(classname1));
1669 dns_rdataclass_format(lctx->zclass, classname2,
1670 sizeof(classname2));
1671 (*callbacks->error)(callbacks,
1672 "%s:%lu: class '%s' != "
1675 classname1, classname2);
1676 result = DNS_R_BADCLASS;
1677 if (MANYERRS(lctx, result)) {
1678 SETRESULT(lctx, result);
1679 read_till_eol = ISC_TRUE;
1681 } else if (result != ISC_R_SUCCESS)
1682 goto insist_and_cleanup;
1685 if (type == dns_rdatatype_ns && ictx->glue == NULL)
1686 current_has_delegation = ISC_TRUE;
1689 * RFC1123: MD and MF are not allowed to be loaded from
1692 if ((lctx->options & DNS_MASTER_ZONE) != 0 &&
1693 (lctx->options & DNS_MASTER_SLAVE) == 0 &&
1694 (type == dns_rdatatype_md || type == dns_rdatatype_mf)) {
1695 char typename[DNS_RDATATYPE_FORMATSIZE];
1697 result = DNS_R_OBSOLETE;
1699 dns_rdatatype_format(type, typename, sizeof(typename));
1700 (*callbacks->error)(callbacks,
1701 "%s:%lu: %s '%s': %s",
1704 dns_result_totext(result));
1705 if (MANYERRS(lctx, result)) {
1706 SETRESULT(lctx, result);
1708 goto insist_and_cleanup;
1712 * Find a rdata structure.
1714 if (rdcount == rdata_size) {
1715 new_rdata = grow_rdata(rdata_size + RDSZ, rdata,
1716 rdata_size, ¤t_list,
1718 if (new_rdata == NULL) {
1719 result = ISC_R_NOMEMORY;
1720 goto log_and_cleanup;
1727 * Peek at the NS record.
1729 if (type == dns_rdatatype_ns &&
1730 lctx->zclass == dns_rdataclass_in &&
1731 (lctx->options & DNS_MASTER_CHECKNS) != 0) {
1733 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1734 result = check_ns(lctx, &token, source, line);
1735 isc_lex_ungettoken(lctx->lex, &token);
1736 if ((lctx->options & DNS_MASTER_FATALNS) != 0) {
1737 if (MANYERRS(lctx, result)) {
1738 SETRESULT(lctx, result);
1739 } else if (result != ISC_R_SUCCESS)
1740 goto insist_and_cleanup;
1747 options &= ~DNS_RDATA_CHECKREVERSE;
1748 if ((lctx->options & DNS_MASTER_CHECKNAMES) != 0) {
1752 name = (ictx->glue != NULL) ? ictx->glue :
1754 ok = dns_rdata_checkowner(name, lctx->zclass, type,
1757 char namebuf[DNS_NAME_FORMATSIZE];
1759 dns_name_format(name, namebuf, sizeof(namebuf));
1760 result = DNS_R_BADOWNERNAME;
1761 desc = dns_result_totext(result);
1762 if ((lctx->options & DNS_MASTER_CHECKNAMESFAIL) != 0) {
1763 (*callbacks->error)(callbacks,
1767 if (MANYERRS(lctx, result)) {
1768 SETRESULT(lctx, result);
1769 } else if (result != ISC_R_SUCCESS)
1772 (*callbacks->warn)(callbacks,
1778 if (type == dns_rdatatype_ptr &&
1779 (dns_name_issubdomain(name, &in_addr_arpa) ||
1780 dns_name_issubdomain(name, &ip6_arpa) ||
1781 dns_name_issubdomain(name, &ip6_int)))
1782 options |= DNS_RDATA_CHECKREVERSE;
1786 * Read rdata contents.
1788 dns_rdata_init(&rdata[rdcount]);
1790 result = dns_rdata_fromtext(&rdata[rdcount], lctx->zclass,
1791 type, lctx->lex, ictx->origin,
1792 options, lctx->mctx, &target,
1794 if (MANYERRS(lctx, result)) {
1795 SETRESULT(lctx, result);
1797 } else if (result != ISC_R_SUCCESS)
1798 goto insist_and_cleanup;
1805 if (type == dns_rdatatype_soa &&
1806 (lctx->options & DNS_MASTER_ZONE) != 0 &&
1807 dns_name_compare(ictx->current, lctx->top) != 0) {
1808 char namebuf[DNS_NAME_FORMATSIZE];
1809 dns_name_format(ictx->current, namebuf,
1811 (*callbacks->error)(callbacks, "%s:%lu: SOA "
1812 "record not at top of zone (%s)",
1813 source, line, namebuf);
1814 result = DNS_R_NOTZONETOP;
1815 if (MANYERRS(lctx, result)) {
1816 SETRESULT(lctx, result);
1817 read_till_eol = ISC_TRUE;
1820 } else if (result != ISC_R_SUCCESS)
1821 goto insist_and_cleanup;
1825 if (type == dns_rdatatype_rrsig ||
1826 type == dns_rdatatype_sig)
1827 covers = dns_rdata_covers(&rdata[rdcount]);
1831 if (!lctx->ttl_known && !lctx->default_ttl_known) {
1832 if (type == dns_rdatatype_soa) {
1833 (*callbacks->warn)(callbacks,
1834 "%s:%lu: no TTL specified; "
1835 "using SOA MINTTL instead",
1837 lctx->ttl = dns_soa_getminimum(&rdata[rdcount]);
1838 limit_ttl(callbacks, source, line, &lctx->ttl);
1839 lctx->default_ttl = lctx->ttl;
1840 lctx->default_ttl_known = ISC_TRUE;
1841 } else if ((lctx->options & DNS_MASTER_HINT) != 0) {
1843 * Zero TTL's are fine for hints.
1846 lctx->default_ttl = lctx->ttl;
1847 lctx->default_ttl_known = ISC_TRUE;
1849 (*callbacks->warn)(callbacks,
1850 "%s:%lu: no TTL specified; "
1853 result = DNS_R_NOTTL;
1854 if (MANYERRS(lctx, result)) {
1855 SETRESULT(lctx, result);
1858 goto insist_and_cleanup;
1861 } else if (!explicit_ttl && lctx->default_ttl_known) {
1862 lctx->ttl = lctx->default_ttl;
1863 } else if (!explicit_ttl && lctx->warn_1035) {
1864 (*callbacks->warn)(callbacks,
1866 "using RFC1035 TTL semantics",
1868 lctx->warn_1035 = ISC_FALSE;
1871 if (type == dns_rdatatype_rrsig && lctx->warn_sigexpired) {
1872 dns_rdata_rrsig_t sig;
1873 result = dns_rdata_tostruct(&rdata[rdcount], &sig,
1875 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1876 if (isc_serial_lt(sig.timeexpire, now)) {
1877 (*callbacks->warn)(callbacks,
1879 "signature has expired",
1881 lctx->warn_sigexpired = ISC_FALSE;
1885 if ((type == dns_rdatatype_sig || type == dns_rdatatype_nxt) &&
1886 lctx->warn_tcr && (lctx->options & DNS_MASTER_ZONE) != 0 &&
1887 (lctx->options & DNS_MASTER_SLAVE) == 0) {
1888 (*callbacks->warn)(callbacks, "%s:%lu: old style DNSSEC "
1889 " zone detected", source, line);
1890 lctx->warn_tcr = ISC_FALSE;
1893 if ((lctx->options & DNS_MASTER_AGETTL) != 0) {
1895 * Adjust the TTL for $DATE. If the RR has already
1896 * expired, ignore it.
1898 if (lctx->ttl < ttl_offset)
1900 lctx->ttl -= ttl_offset;
1904 * Find type in rdatalist.
1905 * If it does not exist create new one and prepend to list
1906 * as this will minimise list traversal.
1908 if (ictx->glue != NULL)
1909 this = ISC_LIST_HEAD(glue_list);
1911 this = ISC_LIST_HEAD(current_list);
1913 while (this != NULL) {
1914 if (this->type == type && this->covers == covers)
1916 this = ISC_LIST_NEXT(this, link);
1920 if (rdlcount == rdatalist_size) {
1922 grow_rdatalist(rdatalist_size + RDLSZ,
1928 if (new_rdatalist == NULL) {
1929 result = ISC_R_NOMEMORY;
1930 goto log_and_cleanup;
1932 rdatalist = new_rdatalist;
1933 rdatalist_size += RDLSZ;
1935 this = &rdatalist[rdlcount++];
1937 this->covers = covers;
1938 this->rdclass = lctx->zclass;
1939 this->ttl = lctx->ttl;
1940 ISC_LIST_INIT(this->rdata);
1941 if (ictx->glue != NULL)
1942 ISC_LIST_INITANDPREPEND(glue_list, this, link);
1944 ISC_LIST_INITANDPREPEND(current_list, this,
1946 } else if (this->ttl != lctx->ttl) {
1947 (*callbacks->warn)(callbacks,
1949 "TTL set to prior TTL (%lu)",
1950 source, line, this->ttl);
1951 lctx->ttl = this->ttl;
1954 ISC_LIST_APPEND(this->rdata, &rdata[rdcount], link);
1955 if (ictx->glue != NULL)
1956 ictx->glue_line = line;
1958 ictx->current_line = line;
1962 * We must have at least 64k as rdlen is 16 bits.
1963 * If we don't commit everything we have so far.
1965 if ((target.length - target.used) < MINTSIZ)
1969 } while (!done && (lctx->loop_cnt == 0 || loop_cnt++ < lctx->loop_cnt));
1972 * Commit what has not yet been committed.
1974 result = commit(callbacks, lctx, ¤t_list, ictx->current,
1975 source, ictx->current_line);
1976 if (MANYERRS(lctx, result)) {
1977 SETRESULT(lctx, result);
1978 } else if (result != ISC_R_SUCCESS)
1979 goto insist_and_cleanup;
1980 result = commit(callbacks, lctx, &glue_list, ictx->glue,
1981 source, ictx->glue_line);
1982 if (MANYERRS(lctx, result)) {
1983 SETRESULT(lctx, result);
1984 } else if (result != ISC_R_SUCCESS)
1985 goto insist_and_cleanup;
1988 INSIST(lctx->done != NULL && lctx->task != NULL);
1989 result = DNS_R_CONTINUE;
1990 } else if (result == ISC_R_SUCCESS && lctx->result != ISC_R_SUCCESS) {
1991 result = lctx->result;
1992 } else if (result == ISC_R_SUCCESS && lctx->seen_include)
1993 result = DNS_R_SEENINCLUDE;
2000 INSIST(result != ISC_R_SUCCESS);
2003 while ((this = ISC_LIST_HEAD(current_list)) != NULL)
2004 ISC_LIST_UNLINK(current_list, this, link);
2005 while ((this = ISC_LIST_HEAD(glue_list)) != NULL)
2006 ISC_LIST_UNLINK(glue_list, this, link);
2007 if (rdatalist != NULL)
2008 isc_mem_put(mctx, rdatalist,
2009 rdatalist_size * sizeof(*rdatalist));
2011 isc_mem_put(mctx, rdata, rdata_size * sizeof(*rdata));
2012 if (target_mem != NULL)
2013 isc_mem_put(mctx, target_mem, target_size);
2014 if (include_file != NULL)
2015 isc_mem_free(mctx, include_file);
2017 isc_mem_free(mctx, range);
2019 isc_mem_free(mctx, lhs);
2021 isc_mem_free(mctx, gtype);
2023 isc_mem_free(mctx, rhs);
2028 pushfile(const char *master_file, dns_name_t *origin, dns_loadctx_t *lctx) {
2029 isc_result_t result;
2031 dns_incctx_t *new = NULL;
2035 REQUIRE(master_file != NULL);
2036 REQUIRE(DNS_LCTX_VALID(lctx));
2039 lctx->seen_include = ISC_TRUE;
2041 result = incctx_create(lctx->mctx, origin, &new);
2042 if (result != ISC_R_SUCCESS)
2045 /* Set current domain. */
2046 if (ictx->glue != NULL || ictx->current != NULL) {
2047 for (new_in_use = 0; new_in_use < NBUFS; new_in_use++)
2048 if (!new->in_use[new_in_use])
2050 INSIST(new_in_use < NBUFS);
2051 new->current_in_use = new_in_use;
2053 dns_fixedname_name(&new->fixed[new->current_in_use]);
2054 new->in_use[new->current_in_use] = ISC_TRUE;
2055 dns_name_toregion((ictx->glue != NULL) ?
2056 ictx->glue : ictx->current, &r);
2057 dns_name_fromregion(new->current, &r);
2058 new->drop = ictx->drop;
2061 result = (lctx->openfile)(lctx, master_file);
2062 if (result != ISC_R_SUCCESS)
2066 return (ISC_R_SUCCESS);
2070 incctx_destroy(lctx->mctx, new);
2074 static inline isc_result_t
2075 read_and_check(isc_boolean_t do_read, isc_buffer_t *buffer,
2076 size_t len, FILE *f)
2078 isc_result_t result;
2081 INSIST(isc_buffer_availablelength(buffer) >= len);
2082 result = isc_stdio_read(isc_buffer_used(buffer), 1, len,
2084 if (result != ISC_R_SUCCESS)
2086 isc_buffer_add(buffer, (unsigned int)len);
2087 } else if (isc_buffer_remaininglength(buffer) < len)
2088 return (ISC_R_RANGE);
2090 return (ISC_R_SUCCESS);
2094 load_raw(dns_loadctx_t *lctx) {
2095 isc_result_t result = ISC_R_SUCCESS;
2096 isc_boolean_t done = ISC_FALSE;
2097 unsigned int loop_cnt = 0;
2098 dns_rdatacallbacks_t *callbacks;
2099 unsigned char namebuf[DNS_NAME_MAXWIRE];
2100 dns_fixedname_t fixed;
2102 rdatalist_head_t head, dummy;
2103 dns_rdatalist_t rdatalist;
2104 isc_mem_t *mctx = lctx->mctx;
2105 dns_rdata_t *rdata = NULL;
2106 unsigned int rdata_size = 0;
2107 int target_size = TSIZ;
2108 isc_buffer_t target, buf;
2109 unsigned char *target_mem = NULL;
2110 dns_masterrawheader_t header;
2111 dns_decompress_t dctx;
2113 REQUIRE(DNS_LCTX_VALID(lctx));
2114 callbacks = lctx->callbacks;
2115 dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_NONE);
2117 dns_master_initrawheader(&header);
2120 unsigned char data[sizeof(header)];
2122 sizeof(header.format) + sizeof(header.version);
2125 INSIST(commonlen <= sizeof(header));
2126 isc_buffer_init(&target, data, sizeof(data));
2128 result = isc_stdio_read(data, 1, commonlen, lctx->f, NULL);
2129 if (result != ISC_R_SUCCESS) {
2130 UNEXPECTED_ERROR(__FILE__, __LINE__,
2131 "isc_stdio_read failed: %s",
2132 isc_result_totext(result));
2135 isc_buffer_add(&target, commonlen);
2136 header.format = isc_buffer_getuint32(&target);
2137 if (header.format != dns_masterformat_raw) {
2138 (*callbacks->error)(callbacks,
2140 "file format mismatch");
2141 return (ISC_R_NOTIMPLEMENTED);
2144 header.version = isc_buffer_getuint32(&target);
2145 switch (header.version) {
2147 remainder = sizeof(header.dumptime);
2149 case DNS_RAWFORMAT_VERSION:
2150 remainder = sizeof(header) - commonlen;
2153 (*callbacks->error)(callbacks,
2155 "unsupported file format version");
2156 return (ISC_R_NOTIMPLEMENTED);
2159 result = isc_stdio_read(data + commonlen, 1, remainder,
2161 if (result != ISC_R_SUCCESS) {
2162 UNEXPECTED_ERROR(__FILE__, __LINE__,
2163 "isc_stdio_read failed: %s",
2164 isc_result_totext(result));
2168 isc_buffer_add(&target, remainder);
2169 header.dumptime = isc_buffer_getuint32(&target);
2170 if (header.version == DNS_RAWFORMAT_VERSION) {
2171 header.flags = isc_buffer_getuint32(&target);
2172 header.sourceserial = isc_buffer_getuint32(&target);
2173 header.lastxfrin = isc_buffer_getuint32(&target);
2176 lctx->first = ISC_FALSE;
2177 lctx->header = header;
2180 ISC_LIST_INIT(head);
2181 ISC_LIST_INIT(dummy);
2182 dns_rdatalist_init(&rdatalist);
2185 * Allocate target_size of buffer space. This is greater than twice
2186 * the maximum individual RR data size.
2188 target_mem = isc_mem_get(mctx, target_size);
2189 if (target_mem == NULL) {
2190 result = ISC_R_NOMEMORY;
2193 isc_buffer_init(&target, target_mem, target_size);
2195 dns_fixedname_init(&fixed);
2196 name = dns_fixedname_name(&fixed);
2199 * In the following loop, we regard any error fatal regardless of
2200 * whether "MANYERRORS" is set in the context option. This is because
2201 * normal errors should already have been checked at creation time.
2202 * Besides, it is very unlikely that we can recover from an error
2203 * in this format, and so trying to continue parsing erroneous data
2204 * does not really make sense.
2207 (lctx->loop_cnt == 0 || loop_cnt < lctx->loop_cnt);
2209 unsigned int i, rdcount;
2210 isc_uint16_t namelen;
2211 isc_uint32_t totallen;
2212 size_t minlen, readlen;
2213 isc_boolean_t sequential_read = ISC_FALSE;
2215 /* Read the data length */
2216 isc_buffer_clear(&target);
2217 INSIST(isc_buffer_availablelength(&target) >=
2219 result = isc_stdio_read(target.base, 1, sizeof(totallen),
2221 if (result == ISC_R_EOF) {
2222 result = ISC_R_SUCCESS;
2226 if (result != ISC_R_SUCCESS)
2228 isc_buffer_add(&target, sizeof(totallen));
2229 totallen = isc_buffer_getuint32(&target);
2231 * Validation: the input data must at least contain the common
2234 minlen = sizeof(totallen) + sizeof(isc_uint16_t) +
2235 sizeof(isc_uint16_t) + sizeof(isc_uint16_t) +
2236 sizeof(isc_uint32_t) + sizeof(isc_uint32_t);
2237 if (totallen < minlen) {
2238 result = ISC_R_RANGE;
2241 totallen -= sizeof(totallen);
2243 isc_buffer_clear(&target);
2244 if (totallen > isc_buffer_availablelength(&target)) {
2246 * The default buffer size should typically be large
2247 * enough to store the entire RRset. We could try to
2248 * allocate enough space if this is not the case, but
2249 * it might cause a hazardous result when "totallen"
2250 * is forged. Thus, we'd rather take an inefficient
2251 * but robust approach in this atypical case: read
2252 * data step by step, and commit partial data when
2253 * necessary. Note that the buffer must be large
2254 * enough to store the "header part", owner name, and
2255 * at least one rdata (however large it is).
2257 sequential_read = ISC_TRUE;
2258 readlen = minlen - sizeof(totallen);
2261 * Typical case. We can read the whole RRset at once
2262 * with the default buffer.
2266 result = isc_stdio_read(target.base, 1, readlen,
2268 if (result != ISC_R_SUCCESS)
2270 isc_buffer_add(&target, (unsigned int)readlen);
2272 /* Construct RRset headers */
2273 rdatalist.rdclass = isc_buffer_getuint16(&target);
2274 rdatalist.type = isc_buffer_getuint16(&target);
2275 rdatalist.covers = isc_buffer_getuint16(&target);
2276 rdatalist.ttl = isc_buffer_getuint32(&target);
2277 rdcount = isc_buffer_getuint32(&target);
2279 result = ISC_R_RANGE;
2282 INSIST(isc_buffer_consumedlength(&target) <= readlen);
2284 /* Owner name: length followed by name */
2285 result = read_and_check(sequential_read, &target,
2286 sizeof(namelen), lctx->f);
2287 if (result != ISC_R_SUCCESS)
2289 namelen = isc_buffer_getuint16(&target);
2290 if (namelen > sizeof(namebuf)) {
2291 result = ISC_R_RANGE;
2295 result = read_and_check(sequential_read, &target, namelen,
2297 if (result != ISC_R_SUCCESS)
2300 isc_buffer_setactive(&target, (unsigned int)namelen);
2301 result = dns_name_fromwire(name, &target, &dctx, 0, NULL);
2302 if (result != ISC_R_SUCCESS)
2305 /* Rdata contents. */
2306 if (rdcount > rdata_size) {
2307 dns_rdata_t *new_rdata = NULL;
2309 new_rdata = grow_rdata(rdcount + RDSZ, rdata,
2312 if (new_rdata == NULL) {
2313 result = ISC_R_NOMEMORY;
2316 rdata_size = rdcount + RDSZ;
2321 for (i = 0; i < rdcount; i++) {
2324 dns_rdata_init(&rdata[i]);
2326 if (sequential_read &&
2327 isc_buffer_availablelength(&target) < MINTSIZ) {
2330 INSIST(i > 0); /* detect an infinite loop */
2332 /* Partial Commit. */
2333 ISC_LIST_APPEND(head, &rdatalist, link);
2334 result = commit(callbacks, lctx, &head, name,
2336 for (j = 0; j < i; j++) {
2337 ISC_LIST_UNLINK(rdatalist.rdata,
2339 dns_rdata_reset(&rdata[j]);
2341 if (result != ISC_R_SUCCESS)
2344 /* Rewind the buffer and continue */
2345 isc_buffer_clear(&target);
2353 result = read_and_check(sequential_read, &target,
2354 sizeof(rdlen), lctx->f);
2355 if (result != ISC_R_SUCCESS)
2357 rdlen = isc_buffer_getuint16(&target);
2360 result = read_and_check(sequential_read, &target,
2362 if (result != ISC_R_SUCCESS)
2364 isc_buffer_setactive(&target, (unsigned int)rdlen);
2366 * It is safe to have the source active region and
2367 * the target available region be the same if
2368 * decompression is disabled (see dctx above) and we
2369 * are not downcasing names (options == 0).
2371 isc_buffer_init(&buf, isc_buffer_current(&target),
2372 (unsigned int)rdlen);
2373 result = dns_rdata_fromwire(&rdata[i],
2375 rdatalist.type, &target,
2377 if (result != ISC_R_SUCCESS)
2379 ISC_LIST_APPEND(rdatalist.rdata, &rdata[i], link);
2383 * Sanity check. Still having remaining space is not
2384 * necessarily critical, but it very likely indicates broken
2385 * or malformed data.
2387 if (isc_buffer_remaininglength(&target) != 0) {
2388 result = ISC_R_RANGE;
2392 ISC_LIST_APPEND(head, &rdatalist, link);
2394 /* Commit this RRset. rdatalist will be unlinked. */
2395 result = commit(callbacks, lctx, &head, name, NULL, 0);
2397 for (i = 0; i < rdcount; i++) {
2398 ISC_LIST_UNLINK(rdatalist.rdata, &rdata[i], link);
2399 dns_rdata_reset(&rdata[i]);
2402 if (result != ISC_R_SUCCESS)
2407 INSIST(lctx->done != NULL && lctx->task != NULL);
2408 result = DNS_R_CONTINUE;
2409 } else if (result == ISC_R_SUCCESS && lctx->result != ISC_R_SUCCESS)
2410 result = lctx->result;
2412 if (result == ISC_R_SUCCESS && callbacks->rawdata != NULL)
2413 (*callbacks->rawdata)(callbacks->zone, &header);
2417 isc_mem_put(mctx, rdata, rdata_size * sizeof(*rdata));
2418 if (target_mem != NULL)
2419 isc_mem_put(mctx, target_mem, target_size);
2420 if (result != ISC_R_SUCCESS && result != DNS_R_CONTINUE) {
2421 (*callbacks->error)(callbacks, "dns_master_load: %s",
2422 dns_result_totext(result));
2429 dns_master_loadfile(const char *master_file, dns_name_t *top,
2431 dns_rdataclass_t zclass, unsigned int options,
2432 dns_rdatacallbacks_t *callbacks, isc_mem_t *mctx)
2434 return (dns_master_loadfile3(master_file, top, origin, zclass, options,
2435 0, callbacks, mctx, dns_masterformat_text));
2439 dns_master_loadfile2(const char *master_file, dns_name_t *top,
2441 dns_rdataclass_t zclass, unsigned int options,
2442 dns_rdatacallbacks_t *callbacks, isc_mem_t *mctx,
2443 dns_masterformat_t format)
2445 return (dns_master_loadfile3(master_file, top, origin, zclass, options,
2446 0, callbacks, mctx, format));
2450 dns_master_loadfile3(const char *master_file, dns_name_t *top,
2451 dns_name_t *origin, dns_rdataclass_t zclass,
2452 unsigned int options, isc_uint32_t resign,
2453 dns_rdatacallbacks_t *callbacks, isc_mem_t *mctx,
2454 dns_masterformat_t format)
2456 dns_loadctx_t *lctx = NULL;
2457 isc_result_t result;
2459 result = loadctx_create(format, mctx, options, resign, top, zclass,
2460 origin, callbacks, NULL, NULL, NULL, NULL,
2462 if (result != ISC_R_SUCCESS)
2465 result = (lctx->openfile)(lctx, master_file);
2466 if (result != ISC_R_SUCCESS)
2469 result = (lctx->load)(lctx);
2470 INSIST(result != DNS_R_CONTINUE);
2473 dns_loadctx_detach(&lctx);
2478 dns_master_loadfileinc(const char *master_file, dns_name_t *top,
2479 dns_name_t *origin, dns_rdataclass_t zclass,
2480 unsigned int options, dns_rdatacallbacks_t *callbacks,
2481 isc_task_t *task, dns_loaddonefunc_t done,
2482 void *done_arg, dns_loadctx_t **lctxp, isc_mem_t *mctx)
2484 return (dns_master_loadfileinc3(master_file, top, origin, zclass,
2485 options, 0, callbacks, task, done,
2486 done_arg, lctxp, mctx,
2487 dns_masterformat_text));
2491 dns_master_loadfileinc2(const char *master_file, dns_name_t *top,
2492 dns_name_t *origin, dns_rdataclass_t zclass,
2493 unsigned int options, dns_rdatacallbacks_t *callbacks,
2494 isc_task_t *task, dns_loaddonefunc_t done,
2495 void *done_arg, dns_loadctx_t **lctxp, isc_mem_t *mctx,
2496 dns_masterformat_t format)
2498 return (dns_master_loadfileinc3(master_file, top, origin, zclass,
2499 options, 0, callbacks, task, done,
2500 done_arg, lctxp, mctx, format));
2504 dns_master_loadfileinc3(const char *master_file, dns_name_t *top,
2505 dns_name_t *origin, dns_rdataclass_t zclass,
2506 unsigned int options, isc_uint32_t resign,
2507 dns_rdatacallbacks_t *callbacks, isc_task_t *task,
2508 dns_loaddonefunc_t done, void *done_arg,
2509 dns_loadctx_t **lctxp, isc_mem_t *mctx,
2510 dns_masterformat_t format)
2512 dns_loadctx_t *lctx = NULL;
2513 isc_result_t result;
2515 REQUIRE(task != NULL);
2516 REQUIRE(done != NULL);
2518 result = loadctx_create(format, mctx, options, resign, top, zclass,
2519 origin, callbacks, task, done, done_arg, NULL,
2521 if (result != ISC_R_SUCCESS)
2524 result = (lctx->openfile)(lctx, master_file);
2525 if (result != ISC_R_SUCCESS)
2528 result = task_send(lctx);
2529 if (result == ISC_R_SUCCESS) {
2530 dns_loadctx_attach(lctx, lctxp);
2531 return (DNS_R_CONTINUE);
2535 dns_loadctx_detach(&lctx);
2540 dns_master_loadstream(FILE *stream, dns_name_t *top, dns_name_t *origin,
2541 dns_rdataclass_t zclass, unsigned int options,
2542 dns_rdatacallbacks_t *callbacks, isc_mem_t *mctx)
2544 isc_result_t result;
2545 dns_loadctx_t *lctx = NULL;
2547 REQUIRE(stream != NULL);
2549 result = loadctx_create(dns_masterformat_text, mctx, options, 0, top,
2550 zclass, origin, callbacks, NULL, NULL, NULL,
2552 if (result != ISC_R_SUCCESS)
2555 result = isc_lex_openstream(lctx->lex, stream);
2556 if (result != ISC_R_SUCCESS)
2559 result = (lctx->load)(lctx);
2560 INSIST(result != DNS_R_CONTINUE);
2564 dns_loadctx_detach(&lctx);
2569 dns_master_loadstreaminc(FILE *stream, dns_name_t *top, dns_name_t *origin,
2570 dns_rdataclass_t zclass, unsigned int options,
2571 dns_rdatacallbacks_t *callbacks, isc_task_t *task,
2572 dns_loaddonefunc_t done, void *done_arg,
2573 dns_loadctx_t **lctxp, isc_mem_t *mctx)
2575 isc_result_t result;
2576 dns_loadctx_t *lctx = NULL;
2578 REQUIRE(stream != NULL);
2579 REQUIRE(task != NULL);
2580 REQUIRE(done != NULL);
2582 result = loadctx_create(dns_masterformat_text, mctx, options, 0, top,
2583 zclass, origin, callbacks, task, done,
2584 done_arg, NULL, &lctx);
2585 if (result != ISC_R_SUCCESS)
2588 result = isc_lex_openstream(lctx->lex, stream);
2589 if (result != ISC_R_SUCCESS)
2592 result = task_send(lctx);
2593 if (result == ISC_R_SUCCESS) {
2594 dns_loadctx_attach(lctx, lctxp);
2595 return (DNS_R_CONTINUE);
2600 dns_loadctx_detach(&lctx);
2605 dns_master_loadbuffer(isc_buffer_t *buffer, dns_name_t *top,
2606 dns_name_t *origin, dns_rdataclass_t zclass,
2607 unsigned int options,
2608 dns_rdatacallbacks_t *callbacks, isc_mem_t *mctx)
2610 isc_result_t result;
2611 dns_loadctx_t *lctx = NULL;
2613 REQUIRE(buffer != NULL);
2615 result = loadctx_create(dns_masterformat_text, mctx, options, 0, top,
2616 zclass, origin, callbacks, NULL, NULL, NULL,
2618 if (result != ISC_R_SUCCESS)
2621 result = isc_lex_openbuffer(lctx->lex, buffer);
2622 if (result != ISC_R_SUCCESS)
2625 result = (lctx->load)(lctx);
2626 INSIST(result != DNS_R_CONTINUE);
2629 dns_loadctx_detach(&lctx);
2634 dns_master_loadbufferinc(isc_buffer_t *buffer, dns_name_t *top,
2635 dns_name_t *origin, dns_rdataclass_t zclass,
2636 unsigned int options,
2637 dns_rdatacallbacks_t *callbacks, isc_task_t *task,
2638 dns_loaddonefunc_t done, void *done_arg,
2639 dns_loadctx_t **lctxp, isc_mem_t *mctx)
2641 isc_result_t result;
2642 dns_loadctx_t *lctx = NULL;
2644 REQUIRE(buffer != NULL);
2645 REQUIRE(task != NULL);
2646 REQUIRE(done != NULL);
2648 result = loadctx_create(dns_masterformat_text, mctx, options, 0, top,
2649 zclass, origin, callbacks, task, done,
2650 done_arg, NULL, &lctx);
2651 if (result != ISC_R_SUCCESS)
2654 result = isc_lex_openbuffer(lctx->lex, buffer);
2655 if (result != ISC_R_SUCCESS)
2658 result = task_send(lctx);
2659 if (result == ISC_R_SUCCESS) {
2660 dns_loadctx_attach(lctx, lctxp);
2661 return (DNS_R_CONTINUE);
2665 dns_loadctx_detach(&lctx);
2670 dns_master_loadlexer(isc_lex_t *lex, dns_name_t *top,
2671 dns_name_t *origin, dns_rdataclass_t zclass,
2672 unsigned int options,
2673 dns_rdatacallbacks_t *callbacks, isc_mem_t *mctx)
2675 isc_result_t result;
2676 dns_loadctx_t *lctx = NULL;
2678 REQUIRE(lex != NULL);
2680 result = loadctx_create(dns_masterformat_text, mctx, options, 0, top,
2681 zclass, origin, callbacks, NULL, NULL, NULL,
2683 if (result != ISC_R_SUCCESS)
2686 result = (lctx->load)(lctx);
2687 INSIST(result != DNS_R_CONTINUE);
2689 dns_loadctx_detach(&lctx);
2694 dns_master_loadlexerinc(isc_lex_t *lex, dns_name_t *top,
2695 dns_name_t *origin, dns_rdataclass_t zclass,
2696 unsigned int options,
2697 dns_rdatacallbacks_t *callbacks, isc_task_t *task,
2698 dns_loaddonefunc_t done, void *done_arg,
2699 dns_loadctx_t **lctxp, isc_mem_t *mctx)
2701 isc_result_t result;
2702 dns_loadctx_t *lctx = NULL;
2704 REQUIRE(lex != NULL);
2705 REQUIRE(task != NULL);
2706 REQUIRE(done != NULL);
2708 result = loadctx_create(dns_masterformat_text, mctx, options, 0, top,
2709 zclass, origin, callbacks, task, done,
2710 done_arg, lex, &lctx);
2711 if (result != ISC_R_SUCCESS)
2714 result = task_send(lctx);
2715 if (result == ISC_R_SUCCESS) {
2716 dns_loadctx_attach(lctx, lctxp);
2717 return (DNS_R_CONTINUE);
2720 dns_loadctx_detach(&lctx);
2725 * Grow the slab of dns_rdatalist_t structures.
2726 * Re-link glue and current list.
2728 static dns_rdatalist_t *
2729 grow_rdatalist(int new_len, dns_rdatalist_t *old, int old_len,
2730 rdatalist_head_t *current, rdatalist_head_t *glue,
2733 dns_rdatalist_t *new;
2735 ISC_LIST(dns_rdatalist_t) save;
2736 dns_rdatalist_t *this;
2738 new = isc_mem_get(mctx, new_len * sizeof(*new));
2742 ISC_LIST_INIT(save);
2743 while ((this = ISC_LIST_HEAD(*current)) != NULL) {
2744 ISC_LIST_UNLINK(*current, this, link);
2745 ISC_LIST_APPEND(save, this, link);
2747 while ((this = ISC_LIST_HEAD(save)) != NULL) {
2748 ISC_LIST_UNLINK(save, this, link);
2749 INSIST(rdlcount < new_len);
2750 new[rdlcount] = *this;
2751 ISC_LIST_APPEND(*current, &new[rdlcount], link);
2755 ISC_LIST_INIT(save);
2756 while ((this = ISC_LIST_HEAD(*glue)) != NULL) {
2757 ISC_LIST_UNLINK(*glue, this, link);
2758 ISC_LIST_APPEND(save, this, link);
2760 while ((this = ISC_LIST_HEAD(save)) != NULL) {
2761 ISC_LIST_UNLINK(save, this, link);
2762 INSIST(rdlcount < new_len);
2763 new[rdlcount] = *this;
2764 ISC_LIST_APPEND(*glue, &new[rdlcount], link);
2768 INSIST(rdlcount == old_len);
2770 isc_mem_put(mctx, old, old_len * sizeof(*old));
2775 * Grow the slab of rdata structs.
2776 * Re-link the current and glue chains.
2778 static dns_rdata_t *
2779 grow_rdata(int new_len, dns_rdata_t *old, int old_len,
2780 rdatalist_head_t *current, rdatalist_head_t *glue,
2785 ISC_LIST(dns_rdata_t) save;
2786 dns_rdatalist_t *this;
2789 new = isc_mem_get(mctx, new_len * sizeof(*new));
2792 memset(new, 0, new_len * sizeof(*new));
2795 * Copy current relinking.
2797 this = ISC_LIST_HEAD(*current);
2798 while (this != NULL) {
2799 ISC_LIST_INIT(save);
2800 while ((rdata = ISC_LIST_HEAD(this->rdata)) != NULL) {
2801 ISC_LIST_UNLINK(this->rdata, rdata, link);
2802 ISC_LIST_APPEND(save, rdata, link);
2804 while ((rdata = ISC_LIST_HEAD(save)) != NULL) {
2805 ISC_LIST_UNLINK(save, rdata, link);
2806 INSIST(rdcount < new_len);
2807 new[rdcount] = *rdata;
2808 ISC_LIST_APPEND(this->rdata, &new[rdcount], link);
2811 this = ISC_LIST_NEXT(this, link);
2815 * Copy glue relinking.
2817 this = ISC_LIST_HEAD(*glue);
2818 while (this != NULL) {
2819 ISC_LIST_INIT(save);
2820 while ((rdata = ISC_LIST_HEAD(this->rdata)) != NULL) {
2821 ISC_LIST_UNLINK(this->rdata, rdata, link);
2822 ISC_LIST_APPEND(save, rdata, link);
2824 while ((rdata = ISC_LIST_HEAD(save)) != NULL) {
2825 ISC_LIST_UNLINK(save, rdata, link);
2826 INSIST(rdcount < new_len);
2827 new[rdcount] = *rdata;
2828 ISC_LIST_APPEND(this->rdata, &new[rdcount], link);
2831 this = ISC_LIST_NEXT(this, link);
2833 INSIST(rdcount == old_len || rdcount == 0);
2835 isc_mem_put(mctx, old, old_len * sizeof(*old));
2840 resign_fromlist(dns_rdatalist_t *this, isc_uint32_t resign) {
2842 dns_rdata_rrsig_t sig;
2845 rdata = ISC_LIST_HEAD(this->rdata);
2846 INSIST(rdata != NULL);
2847 (void)dns_rdata_tostruct(rdata, &sig, NULL);
2848 when = sig.timeexpire - resign;
2850 rdata = ISC_LIST_NEXT(rdata, link);
2851 while (rdata != NULL) {
2852 (void)dns_rdata_tostruct(rdata, &sig, NULL);
2853 if (sig.timeexpire - resign < when)
2854 when = sig.timeexpire - resign;
2855 rdata = ISC_LIST_NEXT(rdata, link);
2861 * Convert each element from a rdatalist_t to rdataset then call commit.
2862 * Unlink each element as we go.
2866 commit(dns_rdatacallbacks_t *callbacks, dns_loadctx_t *lctx,
2867 rdatalist_head_t *head, dns_name_t *owner,
2868 const char *source, unsigned int line)
2870 dns_rdatalist_t *this;
2871 dns_rdataset_t dataset;
2872 isc_result_t result;
2873 char namebuf[DNS_NAME_FORMATSIZE];
2874 void (*error)(struct dns_rdatacallbacks *, const char *, ...);
2876 this = ISC_LIST_HEAD(*head);
2877 error = callbacks->error;
2880 return (ISC_R_SUCCESS);
2882 dns_rdataset_init(&dataset);
2883 RUNTIME_CHECK(dns_rdatalist_tordataset(this, &dataset)
2885 dataset.trust = dns_trust_ultimate;
2887 * If this is a secure dynamic zone set the re-signing time.
2889 if (dataset.type == dns_rdatatype_rrsig &&
2890 (lctx->options & DNS_MASTER_RESIGN) != 0) {
2891 dataset.attributes |= DNS_RDATASETATTR_RESIGN;
2892 dns_name_format(owner, namebuf, sizeof(namebuf));
2893 dataset.resign = resign_fromlist(this, lctx->resign);
2895 result = ((*callbacks->add)(callbacks->add_private, owner,
2897 if (result == ISC_R_NOMEMORY) {
2898 (*error)(callbacks, "dns_master_load: %s",
2899 dns_result_totext(result));
2900 } else if (result != ISC_R_SUCCESS) {
2901 dns_name_format(owner, namebuf, sizeof(namebuf));
2902 if (source != NULL) {
2903 (*error)(callbacks, "%s: %s:%lu: %s: %s",
2904 "dns_master_load", source, line,
2905 namebuf, dns_result_totext(result));
2907 (*error)(callbacks, "%s: %s: %s",
2908 "dns_master_load", namebuf,
2909 dns_result_totext(result));
2912 if (MANYERRS(lctx, result))
2913 SETRESULT(lctx, result);
2914 else if (result != ISC_R_SUCCESS)
2916 ISC_LIST_UNLINK(*head, this, link);
2917 this = ISC_LIST_HEAD(*head);
2918 } while (this != NULL);
2919 return (ISC_R_SUCCESS);
2923 * Returns ISC_TRUE if one of the NS rdata's contains 'owner'.
2926 static isc_boolean_t
2927 is_glue(rdatalist_head_t *head, dns_name_t *owner) {
2928 dns_rdatalist_t *this;
2930 isc_region_t region;
2936 this = ISC_LIST_HEAD(*head);
2937 while (this != NULL) {
2938 if (this->type == dns_rdatatype_ns)
2940 this = ISC_LIST_NEXT(this, link);
2945 rdata = ISC_LIST_HEAD(this->rdata);
2946 while (rdata != NULL) {
2947 dns_name_init(&name, NULL);
2948 dns_rdata_toregion(rdata, ®ion);
2949 dns_name_fromregion(&name, ®ion);
2950 if (dns_name_compare(&name, owner) == 0)
2952 rdata = ISC_LIST_NEXT(rdata, link);
2958 load_quantum(isc_task_t *task, isc_event_t *event) {
2959 isc_result_t result;
2960 dns_loadctx_t *lctx;
2962 REQUIRE(event != NULL);
2963 lctx = event->ev_arg;
2964 REQUIRE(DNS_LCTX_VALID(lctx));
2967 result = ISC_R_CANCELED;
2969 result = (lctx->load)(lctx);
2970 if (result == DNS_R_CONTINUE) {
2971 event->ev_arg = lctx;
2972 isc_task_send(task, &event);
2974 (lctx->done)(lctx->done_arg, result);
2975 isc_event_free(&event);
2976 dns_loadctx_detach(&lctx);
2981 task_send(dns_loadctx_t *lctx) {
2984 event = isc_event_allocate(lctx->mctx, NULL,
2985 DNS_EVENT_MASTERQUANTUM,
2986 load_quantum, lctx, sizeof(*event));
2988 return (ISC_R_NOMEMORY);
2989 isc_task_send(lctx->task, &event);
2990 return (ISC_R_SUCCESS);
2994 dns_loadctx_cancel(dns_loadctx_t *lctx) {
2995 REQUIRE(DNS_LCTX_VALID(lctx));
2998 lctx->canceled = ISC_TRUE;
2999 UNLOCK(&lctx->lock);
3003 dns_master_initrawheader(dns_masterrawheader_t *header) {
3004 memset(header, 0, sizeof(dns_masterrawheader_t));