2 * Copyright (C) 2004-2008 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.
18 /* $Id: master.c,v 1.148.18.21 2008/01/17 23:45:58 tbox Exp $ */
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 (65535 - 12 - 1 - 2 - 2 - 4 - 2)
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)
88 #define DNS_MASTER_BUFSZ 2048
90 typedef ISC_LIST(dns_rdatalist_t) rdatalist_head_t;
92 typedef struct dns_incctx dns_incctx_t;
95 * Master file load state.
101 dns_masterformat_t format;
103 dns_rdatacallbacks_t *callbacks;
105 dns_loaddonefunc_t done;
109 isc_result_t (*openfile)(dns_loadctx_t *lctx,
110 const char *filename);
111 isc_result_t (*load)(dns_loadctx_t *lctx);
113 /* Members specific to the text format: */
115 isc_boolean_t keep_lex;
116 unsigned int options;
117 isc_boolean_t ttl_known;
118 isc_boolean_t default_ttl_known;
119 isc_boolean_t warn_1035;
120 isc_boolean_t warn_tcr;
121 isc_boolean_t warn_sigexpired;
122 isc_boolean_t seen_include;
124 isc_uint32_t default_ttl;
125 dns_rdataclass_t zclass;
126 dns_fixedname_t fixed_top;
127 dns_name_t *top; /*%< top of zone */
129 /* Members specific to the raw format: */
133 /* Which fixed buffers we are using? */
134 unsigned int loop_cnt; /*% records per quantum,
136 isc_boolean_t canceled;
140 isc_uint32_t references;
145 dns_incctx_t *parent;
149 dns_fixedname_t fixed[NBUFS]; /* working buffers */
150 unsigned int in_use[NBUFS]; /* covert to bitmap? */
155 unsigned int glue_line;
156 unsigned int current_line;
159 #define DNS_LCTX_MAGIC ISC_MAGIC('L','c','t','x')
160 #define DNS_LCTX_VALID(lctx) ISC_MAGIC_VALID(lctx, DNS_LCTX_MAGIC)
162 #define DNS_AS_STR(t) ((t).value.as_textregion.base)
165 openfile_text(dns_loadctx_t *lctx, const char *master_file);
168 openfile_raw(dns_loadctx_t *lctx, const char *master_file);
171 load_text(dns_loadctx_t *lctx);
174 load_raw(dns_loadctx_t *lctx);
177 pushfile(const char *master_file, dns_name_t *origin, dns_loadctx_t *lctx);
180 commit(dns_rdatacallbacks_t *, dns_loadctx_t *, rdatalist_head_t *,
181 dns_name_t *, const char *, unsigned int);
184 is_glue(rdatalist_head_t *, dns_name_t *);
186 static dns_rdatalist_t *
187 grow_rdatalist(int, dns_rdatalist_t *, int, rdatalist_head_t *,
188 rdatalist_head_t *, isc_mem_t *mctx);
191 grow_rdata(int, dns_rdata_t *, int, rdatalist_head_t *, rdatalist_head_t *,
195 load_quantum(isc_task_t *task, isc_event_t *event);
198 task_send(dns_loadctx_t *lctx);
201 loadctx_destroy(dns_loadctx_t *lctx);
203 #define GETTOKEN(lexer, options, token, eol) \
205 result = gettoken(lexer, options, token, eol, callbacks); \
207 case ISC_R_SUCCESS: \
209 case ISC_R_UNEXPECTED: \
210 goto insist_and_cleanup; \
212 if (MANYERRS(lctx, result)) { \
213 SETRESULT(lctx, result); \
215 read_till_eol = ISC_TRUE; \
218 goto log_and_cleanup; \
220 if ((token)->type == isc_tokentype_special) { \
221 result = DNS_R_SYNTAX; \
222 if (MANYERRS(lctx, result)) { \
223 SETRESULT(lctx, result); \
225 read_till_eol = ISC_TRUE; \
228 goto log_and_cleanup; \
234 result = commit(callbacks, lctx, ¤t_list, \
235 ictx->current, source, ictx->current_line); \
236 if (MANYERRS(lctx, result)) { \
237 SETRESULT(lctx, result); \
238 } else if (result != ISC_R_SUCCESS) \
239 goto insist_and_cleanup; \
240 result = commit(callbacks, lctx, &glue_list, \
241 ictx->glue, source, ictx->glue_line); \
242 if (MANYERRS(lctx, result)) { \
243 SETRESULT(lctx, result); \
244 } else if (result != ISC_R_SUCCESS) \
245 goto insist_and_cleanup; \
248 isc_buffer_init(&target, target_mem, target_size); \
249 rdcount_save = rdcount; \
250 rdlcount_save = rdlcount; \
253 #define WARNUNEXPECTEDEOF(lexer) \
255 if (isc_lex_isfile(lexer)) \
256 (*callbacks->warn)(callbacks, \
257 "%s: file does not end with newline", \
263 GETTOKEN(lctx->lex, 0, &token, ISC_TRUE); \
264 if (token.type != isc_tokentype_eol) { \
265 isc_lex_ungettoken(lctx->lex, &token); \
266 result = DNS_R_EXTRATOKEN; \
267 if (MANYERRS(lctx, result)) { \
268 SETRESULT(lctx, result); \
270 read_till_eol = ISC_TRUE; \
272 } else if (result != ISC_R_SUCCESS) \
273 goto log_and_cleanup; \
277 #define MANYERRS(lctx, result) \
278 ((result != ISC_R_SUCCESS) && \
279 (result != ISC_R_IOERROR) && \
280 ((lctx)->options & DNS_MASTER_MANYERRORS) != 0)
282 #define SETRESULT(lctx, r) \
284 if ((lctx)->result == ISC_R_SUCCESS) \
285 (lctx)->result = r; \
288 #define LOGITFILE(result, filename) \
289 if (result == ISC_R_INVALIDFILE || result == ISC_R_FILENOTFOUND || \
290 result == ISC_R_IOERROR || result == ISC_R_TOOMANYOPENFILES || \
291 result == ISC_R_NOPERM) \
292 (*callbacks->error)(callbacks, "%s: %s:%lu: %s: %s", \
293 "dns_master_load", source, line, \
294 filename, dns_result_totext(result)); \
297 #define LOGIT(result) \
298 if (result == ISC_R_NOMEMORY) \
299 (*callbacks->error)(callbacks, "dns_master_load: %s", \
300 dns_result_totext(result)); \
302 (*callbacks->error)(callbacks, "%s: %s:%lu: %s", \
304 source, line, dns_result_totext(result))
307 static unsigned char in_addr_arpa_data[] = "\007IN-ADDR\004ARPA";
308 static unsigned char in_addr_arpa_offsets[] = { 0, 8, 13 };
309 static const dns_name_t in_addr_arpa =
312 in_addr_arpa_data, 14, 3,
313 DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE,
314 in_addr_arpa_offsets, NULL,
315 {(void *)-1, (void *)-1},
319 static unsigned char ip6_int_data[] = "\003IP6\003INT";
320 static unsigned char ip6_int_offsets[] = { 0, 4, 8 };
321 static const dns_name_t ip6_int =
325 DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE,
326 ip6_int_offsets, NULL,
327 {(void *)-1, (void *)-1},
331 static unsigned char ip6_arpa_data[] = "\003IP6\004ARPA";
332 static unsigned char ip6_arpa_offsets[] = { 0, 4, 9 };
333 static const dns_name_t ip6_arpa =
336 ip6_arpa_data, 10, 3,
337 DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE,
338 ip6_arpa_offsets, NULL,
339 {(void *)-1, (void *)-1},
344 static inline isc_result_t
345 gettoken(isc_lex_t *lex, unsigned int options, isc_token_t *token,
346 isc_boolean_t eol, dns_rdatacallbacks_t *callbacks)
350 options |= ISC_LEXOPT_EOL | ISC_LEXOPT_EOF | ISC_LEXOPT_DNSMULTILINE |
352 result = isc_lex_gettoken(lex, options, token);
353 if (result != ISC_R_SUCCESS) {
356 return (ISC_R_NOMEMORY);
358 (*callbacks->error)(callbacks,
359 "dns_master_load: %s:%lu:"
360 " isc_lex_gettoken() failed: %s",
361 isc_lex_getsourcename(lex),
362 isc_lex_getsourceline(lex),
363 isc_result_totext(result));
369 if (token->type == isc_tokentype_eol ||
370 token->type == isc_tokentype_eof) {
371 (*callbacks->error)(callbacks,
372 "dns_master_load: %s:%lu: unexpected end of %s",
373 isc_lex_getsourcename(lex),
374 isc_lex_getsourceline(lex),
378 return (ISC_R_UNEXPECTEDEND);
380 return (ISC_R_SUCCESS);
385 dns_loadctx_attach(dns_loadctx_t *source, dns_loadctx_t **target) {
387 REQUIRE(target != NULL && *target == NULL);
388 REQUIRE(DNS_LCTX_VALID(source));
391 INSIST(source->references > 0);
392 source->references++;
393 INSIST(source->references != 0); /* Overflow? */
394 UNLOCK(&source->lock);
400 dns_loadctx_detach(dns_loadctx_t **lctxp) {
402 isc_boolean_t need_destroy = ISC_FALSE;
404 REQUIRE(lctxp != NULL);
406 REQUIRE(DNS_LCTX_VALID(lctx));
409 INSIST(lctx->references > 0);
411 if (lctx->references == 0)
412 need_destroy = ISC_TRUE;
416 loadctx_destroy(lctx);
421 incctx_destroy(isc_mem_t *mctx, dns_incctx_t *ictx) {
422 dns_incctx_t *parent;
425 parent = ictx->parent;
428 isc_mem_put(mctx, ictx, sizeof(*ictx));
430 if (parent != NULL) {
437 loadctx_destroy(dns_loadctx_t *lctx) {
441 REQUIRE(DNS_LCTX_VALID(lctx));
444 if (lctx->inc != NULL)
445 incctx_destroy(lctx->mctx, lctx->inc);
447 if (lctx->f != NULL) {
448 result = isc_stdio_close(lctx->f);
449 if (result != ISC_R_SUCCESS) {
450 UNEXPECTED_ERROR(__FILE__, __LINE__,
451 "isc_stdio_close() failed: %s",
452 isc_result_totext(result));
456 /* isc_lex_destroy() will close all open streams */
457 if (lctx->lex != NULL && !lctx->keep_lex)
458 isc_lex_destroy(&lctx->lex);
460 if (lctx->task != NULL)
461 isc_task_detach(&lctx->task);
462 DESTROYLOCK(&lctx->lock);
464 isc_mem_attach(lctx->mctx, &mctx);
465 isc_mem_detach(&lctx->mctx);
466 isc_mem_put(mctx, lctx, sizeof(*lctx));
467 isc_mem_detach(&mctx);
471 incctx_create(isc_mem_t *mctx, dns_name_t *origin, dns_incctx_t **ictxp) {
476 ictx = isc_mem_get(mctx, sizeof(*ictx));
478 return (ISC_R_NOMEMORY);
480 for (i = 0; i < NBUFS; i++) {
481 dns_fixedname_init(&ictx->fixed[i]);
482 ictx->in_use[i] = ISC_FALSE;
485 ictx->origin_in_use = 0;
486 ictx->origin = dns_fixedname_name(&ictx->fixed[ictx->origin_in_use]);
487 ictx->in_use[ictx->origin_in_use] = ISC_TRUE;
488 dns_name_toregion(origin, &r);
489 dns_name_fromregion(ictx->origin, &r);
492 ictx->current = NULL;
493 ictx->glue_in_use = -1;
494 ictx->current_in_use = -1;
496 ictx->drop = ISC_FALSE;
498 ictx->current_line = 0;
501 return (ISC_R_SUCCESS);
505 loadctx_create(dns_masterformat_t format, isc_mem_t *mctx,
506 unsigned int options, dns_name_t *top,
507 dns_rdataclass_t zclass, dns_name_t *origin,
508 dns_rdatacallbacks_t *callbacks, isc_task_t *task,
509 dns_loaddonefunc_t done, void *done_arg, isc_lex_t *lex,
510 dns_loadctx_t **lctxp)
515 isc_lexspecials_t specials;
517 REQUIRE(lctxp != NULL && *lctxp == NULL);
518 REQUIRE(callbacks != NULL);
519 REQUIRE(callbacks->add != NULL);
520 REQUIRE(callbacks->error != NULL);
521 REQUIRE(callbacks->warn != NULL);
522 REQUIRE(mctx != NULL);
523 REQUIRE(dns_name_isabsolute(top));
524 REQUIRE(dns_name_isabsolute(origin));
525 REQUIRE((task == NULL && done == NULL) ||
526 (task != NULL && done != NULL));
528 lctx = isc_mem_get(mctx, sizeof(*lctx));
530 return (ISC_R_NOMEMORY);
531 result = isc_mutex_init(&lctx->lock);
532 if (result != ISC_R_SUCCESS) {
533 isc_mem_put(mctx, lctx, sizeof(*lctx));
538 result = incctx_create(mctx, origin, &lctx->inc);
539 if (result != ISC_R_SUCCESS)
542 lctx->format = format;
546 case dns_masterformat_text:
547 lctx->openfile = openfile_text;
548 lctx->load = load_text;
550 case dns_masterformat_raw:
551 lctx->openfile = openfile_raw;
552 lctx->load = load_raw;
558 lctx->keep_lex = ISC_TRUE;
561 result = isc_lex_create(mctx, TOKENSIZ, &lctx->lex);
562 if (result != ISC_R_SUCCESS)
564 lctx->keep_lex = ISC_FALSE;
565 memset(specials, 0, sizeof(specials));
569 isc_lex_setspecials(lctx->lex, specials);
570 isc_lex_setcomments(lctx->lex, ISC_LEXCOMMENT_DNSMASTERFILE);
573 lctx->ttl_known = ISC_FALSE;
575 lctx->default_ttl_known = ISC_FALSE;
576 lctx->default_ttl = 0;
577 lctx->warn_1035 = ISC_TRUE; /* XXX Argument? */
578 lctx->warn_tcr = ISC_TRUE; /* XXX Argument? */
579 lctx->warn_sigexpired = ISC_TRUE; /* XXX Argument? */
580 lctx->options = options;
581 lctx->seen_include = ISC_FALSE;
582 lctx->zclass = zclass;
583 lctx->result = ISC_R_SUCCESS;
585 dns_fixedname_init(&lctx->fixed_top);
586 lctx->top = dns_fixedname_name(&lctx->fixed_top);
587 dns_name_toregion(top, &r);
588 dns_name_fromregion(lctx->top, &r);
591 lctx->first = ISC_TRUE;
593 lctx->loop_cnt = (done != NULL) ? 100 : 0;
594 lctx->callbacks = callbacks;
597 isc_task_attach(task, &lctx->task);
599 lctx->done_arg = done_arg;
600 lctx->canceled = ISC_FALSE;
602 isc_mem_attach(mctx, &lctx->mctx);
603 lctx->references = 1; /* Implicit attach. */
604 lctx->magic = DNS_LCTX_MAGIC;
606 return (ISC_R_SUCCESS);
609 incctx_destroy(mctx, lctx->inc);
611 isc_mem_put(mctx, lctx, sizeof(*lctx));
616 genname(char *name, int it, char *buffer, size_t length) {
617 char fmt[sizeof("%04000000000d")];
629 while (*name != '\0') {
634 return (ISC_R_NOSPACE);
636 isc_textregion_consume(&r, 1);
640 /* Get format specifier. */
642 n = sscanf(name, "{%d,%u,%1[doxX]}",
643 &delta, &width, mode);
648 n = snprintf(fmt, sizeof(fmt),
652 n = snprintf(fmt, sizeof(fmt),
653 "%%0%u%c", width, mode[0]);
656 return (DNS_R_SYNTAX);
658 if (n >= sizeof(fmt))
659 return (ISC_R_NOSPACE);
660 /* Skip past closing brace. */
661 while (*name != '\0' && *name++ != '}')
664 n = snprintf(numbuf, sizeof(numbuf), fmt, it + delta);
665 if (n >= sizeof(numbuf))
666 return (ISC_R_NOSPACE);
668 while (*cp != '\0') {
670 return (ISC_R_NOSPACE);
672 isc_textregion_consume(&r, 1);
674 } else if (*name == '\\') {
676 return (ISC_R_NOSPACE);
678 isc_textregion_consume(&r, 1);
682 return (ISC_R_NOSPACE);
684 isc_textregion_consume(&r, 1);
687 return (ISC_R_NOSPACE);
689 isc_textregion_consume(&r, 1);
693 return (ISC_R_NOSPACE);
695 return (ISC_R_SUCCESS);
699 openfile_text(dns_loadctx_t *lctx, const char *master_file) {
700 return (isc_lex_openfile(lctx->lex, master_file));
704 openfile_raw(dns_loadctx_t *lctx, const char *master_file) {
707 result = isc_stdio_open(master_file, "r", &lctx->f);
708 if (result != ISC_R_SUCCESS && result != ISC_R_FILENOTFOUND) {
709 UNEXPECTED_ERROR(__FILE__, __LINE__,
710 "isc_stdio_open() failed: %s",
711 isc_result_totext(result));
718 generate(dns_loadctx_t *lctx, char *range, char *lhs, char *gtype, char *rhs,
719 const char *source, unsigned int line)
721 char *target_mem = NULL;
724 dns_fixedname_t ownerfixed;
726 dns_rdata_t rdata = DNS_RDATA_INIT;
727 dns_rdatacallbacks_t *callbacks;
728 dns_rdatalist_t rdatalist;
729 dns_rdatatype_t type;
730 rdatalist_head_t head;
732 int target_size = MINTSIZ; /* only one rdata at a time */
737 unsigned int start, stop, step, i;
741 callbacks = lctx->callbacks;
742 dns_fixedname_init(&ownerfixed);
743 owner = dns_fixedname_name(&ownerfixed);
746 target_mem = isc_mem_get(lctx->mctx, target_size);
747 rhsbuf = isc_mem_get(lctx->mctx, DNS_MASTER_BUFSZ);
748 lhsbuf = isc_mem_get(lctx->mctx, DNS_MASTER_BUFSZ);
749 if (target_mem == NULL || rhsbuf == NULL || lhsbuf == NULL) {
750 result = ISC_R_NOMEMORY;
753 isc_buffer_init(&target, target_mem, target_size);
755 n = sscanf(range, "%u-%u/%u", &start, &stop, &step);
756 if (n < 2 || stop < start) {
757 (*callbacks->error)(callbacks,
758 "%s: %s:%lu: invalid range '%s'",
759 "$GENERATE", source, line, range);
760 result = DNS_R_SYNTAX;
770 r.length = strlen(gtype);
771 result = dns_rdatatype_fromtext(&type, &r);
772 if (result != ISC_R_SUCCESS) {
773 (*callbacks->error)(callbacks,
774 "%s: %s:%lu: unknown RR type '%s'",
775 "$GENERATE", source, line, gtype);
780 case dns_rdatatype_ns:
781 case dns_rdatatype_ptr:
782 case dns_rdatatype_cname:
783 case dns_rdatatype_dname:
786 case dns_rdatatype_a:
787 case dns_rdatatype_aaaa:
788 if (lctx->zclass == dns_rdataclass_in ||
789 lctx->zclass == dns_rdataclass_ch ||
790 lctx->zclass == dns_rdataclass_hs)
794 (*callbacks->error)(callbacks,
795 "%s: %s:%lu: unsupported type '%s'",
796 "$GENERATE", source, line, gtype);
797 result = ISC_R_NOTIMPLEMENTED;
801 ISC_LIST_INIT(rdatalist.rdata);
802 ISC_LINK_INIT(&rdatalist, link);
803 for (i = start; i <= stop; i += step) {
804 result = genname(lhs, i, lhsbuf, DNS_MASTER_BUFSZ);
805 if (result != ISC_R_SUCCESS)
807 result = genname(rhs, i, rhsbuf, DNS_MASTER_BUFSZ);
808 if (result != ISC_R_SUCCESS)
811 isc_buffer_init(&buffer, lhsbuf, strlen(lhsbuf));
812 isc_buffer_add(&buffer, strlen(lhsbuf));
813 isc_buffer_setactive(&buffer, strlen(lhsbuf));
814 result = dns_name_fromtext(owner, &buffer, ictx->origin,
816 if (result != ISC_R_SUCCESS)
819 if ((lctx->options & DNS_MASTER_ZONE) != 0 &&
820 (lctx->options & DNS_MASTER_SLAVE) == 0 &&
821 !dns_name_issubdomain(owner, lctx->top))
823 char namebuf[DNS_NAME_FORMATSIZE];
824 dns_name_format(owner, namebuf, sizeof(namebuf));
826 * Ignore out-of-zone data.
828 (*callbacks->warn)(callbacks,
830 "ignoring out-of-zone data (%s)",
831 source, line, namebuf);
835 isc_buffer_init(&buffer, rhsbuf, strlen(rhsbuf));
836 isc_buffer_add(&buffer, strlen(rhsbuf));
837 isc_buffer_setactive(&buffer, strlen(rhsbuf));
839 result = isc_lex_openbuffer(lctx->lex, &buffer);
840 if (result != ISC_R_SUCCESS)
843 isc_buffer_init(&target, target_mem, target_size);
844 result = dns_rdata_fromtext(&rdata, lctx->zclass, type,
845 lctx->lex, ictx->origin, 0,
846 lctx->mctx, &target, callbacks);
847 RUNTIME_CHECK(isc_lex_close(lctx->lex) == ISC_R_SUCCESS);
848 if (result != ISC_R_SUCCESS)
851 rdatalist.type = type;
852 rdatalist.covers = 0;
853 rdatalist.rdclass = lctx->zclass;
854 rdatalist.ttl = lctx->ttl;
855 ISC_LIST_PREPEND(head, &rdatalist, link);
856 ISC_LIST_APPEND(rdatalist.rdata, &rdata, link);
857 result = commit(callbacks, lctx, &head, owner, source, line);
858 ISC_LIST_UNLINK(rdatalist.rdata, &rdata, link);
859 if (result != ISC_R_SUCCESS)
861 dns_rdata_reset(&rdata);
863 result = ISC_R_SUCCESS;
867 if (result == ISC_R_NOMEMORY)
868 (*callbacks->error)(callbacks, "$GENERATE: %s",
869 dns_result_totext(result));
871 (*callbacks->error)(callbacks, "$GENERATE: %s:%lu: %s",
872 source, line, dns_result_totext(result));
875 INSIST(result != ISC_R_SUCCESS);
878 if (target_mem != NULL)
879 isc_mem_put(lctx->mctx, target_mem, target_size);
881 isc_mem_put(lctx->mctx, lhsbuf, DNS_MASTER_BUFSZ);
883 isc_mem_put(lctx->mctx, rhsbuf, DNS_MASTER_BUFSZ);
888 limit_ttl(dns_rdatacallbacks_t *callbacks, const char *source, unsigned int line,
891 if (*ttlp > 0x7fffffffUL) {
892 (callbacks->warn)(callbacks,
894 "$TTL %lu > MAXTTL, "
904 check_ns(dns_loadctx_t *lctx, isc_token_t *token, const char *source,
908 isc_result_t result = ISC_R_SUCCESS;
909 void (*callback)(struct dns_rdatacallbacks *, const char *, ...);
911 if ((lctx->options & DNS_MASTER_FATALNS) != 0)
912 callback = lctx->callbacks->error;
914 callback = lctx->callbacks->warn;
916 if (token->type == isc_tokentype_string) {
918 struct in6_addr addr6;
920 tmp = isc_mem_strdup(lctx->mctx, DNS_AS_STR(*token));
922 return (ISC_R_NOMEMORY);
924 * Catch both "1.2.3.4" and "1.2.3.4."
926 if (tmp[strlen(tmp) - 1] == '.')
927 tmp[strlen(tmp) - 1] = '\0';
928 if (inet_aton(tmp, &addr) == 1 ||
929 inet_pton(AF_INET6, tmp, &addr6) == 1)
930 result = DNS_R_NSISADDRESS;
932 if (result != ISC_R_SUCCESS)
933 (*callback)(lctx->callbacks, "%s:%lu: NS record '%s' "
934 "appears to be an address",
935 source, line, DNS_AS_STR(*token));
937 isc_mem_free(lctx->mctx, tmp);
942 check_wildcard(dns_incctx_t *ictx, const char *source, unsigned long line,
943 dns_rdatacallbacks_t *callbacks)
947 name = (ictx->glue != NULL) ? ictx->glue : ictx->current;
948 if (dns_name_internalwildcard(name)) {
949 char namebuf[DNS_NAME_FORMATSIZE];
951 dns_name_format(name, namebuf, sizeof(namebuf));
952 (*callbacks->warn)(callbacks, "%s:%lu: warning: ownername "
953 "'%s' contains an non-terminal wildcard",
954 source, line, namebuf);
959 load_text(dns_loadctx_t *lctx) {
960 dns_rdataclass_t rdclass;
961 dns_rdatatype_t type, covers;
962 isc_uint32_t ttl_offset = 0;
963 dns_name_t *new_name;
964 isc_boolean_t current_has_delegation = ISC_FALSE;
965 isc_boolean_t done = ISC_FALSE;
966 isc_boolean_t finish_origin = ISC_FALSE;
967 isc_boolean_t finish_include = ISC_FALSE;
968 isc_boolean_t read_till_eol = ISC_FALSE;
969 isc_boolean_t initialws;
970 char *include_file = NULL;
972 isc_result_t result = ISC_R_UNEXPECTED;
973 rdatalist_head_t glue_list;
974 rdatalist_head_t current_list;
975 dns_rdatalist_t *this;
976 dns_rdatalist_t *rdatalist = NULL;
977 dns_rdatalist_t *new_rdatalist;
979 int rdlcount_save = 0;
980 int rdatalist_size = 0;
983 isc_buffer_t target_ft;
984 isc_buffer_t target_save;
985 dns_rdata_t *rdata = NULL;
986 dns_rdata_t *new_rdata;
988 int rdcount_save = 0;
990 unsigned char *target_mem = NULL;
991 int target_size = TSIZ;
993 unsigned int loop_cnt = 0;
995 dns_rdatacallbacks_t *callbacks;
1001 const char *source = "";
1002 unsigned long line = 0;
1003 isc_boolean_t explicit_ttl;
1005 char classname1[DNS_RDATACLASS_FORMATSIZE];
1006 char classname2[DNS_RDATACLASS_FORMATSIZE];
1007 unsigned int options = 0;
1009 REQUIRE(DNS_LCTX_VALID(lctx));
1010 callbacks = lctx->callbacks;
1014 ISC_LIST_INIT(glue_list);
1015 ISC_LIST_INIT(current_list);
1017 isc_stdtime_get(&now);
1020 * Allocate target_size of buffer space. This is greater than twice
1021 * the maximum individual RR data size.
1023 target_mem = isc_mem_get(mctx, target_size);
1024 if (target_mem == NULL) {
1025 result = ISC_R_NOMEMORY;
1026 goto log_and_cleanup;
1028 isc_buffer_init(&target, target_mem, target_size);
1029 target_save = target;
1031 if ((lctx->options & DNS_MASTER_CHECKNAMES) != 0)
1032 options |= DNS_RDATA_CHECKNAMES;
1033 if ((lctx->options & DNS_MASTER_CHECKNAMESFAIL) != 0)
1034 options |= DNS_RDATA_CHECKNAMESFAIL;
1035 if ((lctx->options & DNS_MASTER_CHECKMX) != 0)
1036 options |= DNS_RDATA_CHECKMX;
1037 if ((lctx->options & DNS_MASTER_CHECKMXFAIL) != 0)
1038 options |= DNS_RDATA_CHECKMXFAIL;
1039 source = isc_lex_getsourcename(lctx->lex);
1041 initialws = ISC_FALSE;
1042 line = isc_lex_getsourceline(lctx->lex);
1043 GETTOKEN(lctx->lex, ISC_LEXOPT_INITIALWS | ISC_LEXOPT_QSTRING,
1045 line = isc_lex_getsourceline(lctx->lex);
1047 if (token.type == isc_tokentype_eof) {
1049 WARNUNEXPECTEDEOF(lctx->lex);
1050 /* Pop the include stack? */
1051 if (ictx->parent != NULL) {
1053 lctx->inc = ictx->parent;
1054 ictx->parent = NULL;
1055 incctx_destroy(lctx->mctx, ictx);
1056 RUNTIME_CHECK(isc_lex_close(lctx->lex) == ISC_R_SUCCESS);
1057 line = isc_lex_getsourceline(lctx->lex);
1058 source = isc_lex_getsourcename(lctx->lex);
1067 if (token.type == isc_tokentype_eol) {
1068 read_till_eol = ISC_FALSE;
1069 continue; /* blank line */
1075 if (token.type == isc_tokentype_initialws) {
1077 * Still working on the same name.
1079 initialws = ISC_TRUE;
1080 } else if (token.type == isc_tokentype_string ||
1081 token.type == isc_tokentype_qstring) {
1086 * "$ORIGIN" and "$INCLUDE" can both take domain names.
1087 * The processing of "$ORIGIN" and "$INCLUDE" extends
1088 * across the normal domain name processing.
1091 if (strcasecmp(DNS_AS_STR(token), "$ORIGIN") == 0) {
1092 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1093 finish_origin = ISC_TRUE;
1094 } else if (strcasecmp(DNS_AS_STR(token),
1096 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1098 dns_ttl_fromtext(&token.value.as_textregion,
1100 if (MANYERRS(lctx, result)) {
1101 SETRESULT(lctx, result);
1103 } else if (result != ISC_R_SUCCESS)
1104 goto insist_and_cleanup;
1105 limit_ttl(callbacks, source, line, &lctx->ttl);
1106 lctx->default_ttl = lctx->ttl;
1107 lctx->default_ttl_known = ISC_TRUE;
1110 } else if (strcasecmp(DNS_AS_STR(token),
1113 if ((lctx->options & DNS_MASTER_NOINCLUDE)
1116 (callbacks->error)(callbacks,
1117 "%s: %s:%lu: $INCLUDE not allowed",
1120 result = DNS_R_REFUSED;
1121 goto insist_and_cleanup;
1123 if (ttl_offset != 0) {
1124 (callbacks->error)(callbacks,
1125 "%s: %s:%lu: $INCLUDE "
1126 "may not be used with $DATE",
1129 result = DNS_R_SYNTAX;
1130 goto insist_and_cleanup;
1132 GETTOKEN(lctx->lex, ISC_LEXOPT_QSTRING, &token,
1134 if (include_file != NULL)
1135 isc_mem_free(mctx, include_file);
1136 include_file = isc_mem_strdup(mctx,
1138 if (include_file == NULL) {
1139 result = ISC_R_NOMEMORY;
1140 goto log_and_cleanup;
1142 GETTOKEN(lctx->lex, 0, &token, ISC_TRUE);
1144 if (token.type == isc_tokentype_eol ||
1145 token.type == isc_tokentype_eof) {
1146 if (token.type == isc_tokentype_eof)
1147 WARNUNEXPECTEDEOF(lctx->lex);
1148 isc_lex_ungettoken(lctx->lex, &token);
1152 result = pushfile(include_file,
1153 ictx->origin, lctx);
1154 if (MANYERRS(lctx, result)) {
1155 SETRESULT(lctx, result);
1156 LOGITFILE(result, include_file);
1158 } else if (result != ISC_R_SUCCESS) {
1159 LOGITFILE(result, include_file);
1160 goto insist_and_cleanup;
1163 line = isc_lex_getsourceline(lctx->lex);
1165 isc_lex_getsourcename(lctx->lex);
1169 * There is an origin field. Fall through
1170 * to domain name processing code and do
1171 * the actual inclusion later.
1173 finish_include = ISC_TRUE;
1174 } else if (strcasecmp(DNS_AS_STR(token),
1176 isc_int64_t dump_time64;
1177 isc_stdtime_t dump_time, current_time;
1178 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1179 isc_stdtime_get(¤t_time);
1180 result = dns_time64_fromtext(DNS_AS_STR(token),
1182 if (MANYERRS(lctx, result)) {
1183 SETRESULT(lctx, result);
1186 } else if (result != ISC_R_SUCCESS)
1187 goto log_and_cleanup;
1188 dump_time = (isc_stdtime_t)dump_time64;
1189 if (dump_time != dump_time64) {
1190 UNEXPECTED_ERROR(__FILE__, __LINE__,
1191 "%s: %s:%lu: $DATE outside epoch",
1192 "dns_master_load", source, line);
1193 result = ISC_R_UNEXPECTED;
1194 goto insist_and_cleanup;
1196 if (dump_time > current_time) {
1197 UNEXPECTED_ERROR(__FILE__, __LINE__,
1199 "$DATE in future, using current date",
1200 "dns_master_load", source, line);
1201 dump_time = current_time;
1203 ttl_offset = current_time - dump_time;
1206 } else if (strcasecmp(DNS_AS_STR(token),
1207 "$GENERATE") == 0) {
1212 isc_mem_free(mctx, range);
1214 isc_mem_free(mctx, lhs);
1216 isc_mem_free(mctx, gtype);
1218 isc_mem_free(mctx, rhs);
1219 range = lhs = gtype = rhs = NULL;
1221 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1222 range = isc_mem_strdup(mctx,
1224 if (range == NULL) {
1225 result = ISC_R_NOMEMORY;
1226 goto log_and_cleanup;
1229 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1230 lhs = isc_mem_strdup(mctx, DNS_AS_STR(token));
1232 result = ISC_R_NOMEMORY;
1233 goto log_and_cleanup;
1236 explicit_ttl = ISC_FALSE;
1238 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1239 if (dns_rdataclass_fromtext(&rdclass,
1240 &token.value.as_textregion)
1242 GETTOKEN(lctx->lex, 0, &token,
1246 if (dns_ttl_fromtext(&token.value.as_textregion,
1249 limit_ttl(callbacks, source, line,
1251 lctx->ttl_known = ISC_TRUE;
1252 explicit_ttl = ISC_TRUE;
1253 GETTOKEN(lctx->lex, 0, &token,
1258 dns_rdataclass_fromtext(&rdclass,
1259 &token.value.as_textregion)
1261 GETTOKEN(lctx->lex, 0, &token,
1264 gtype = isc_mem_strdup(mctx,
1266 if (gtype == NULL) {
1267 result = ISC_R_NOMEMORY;
1268 goto log_and_cleanup;
1271 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1272 rhs = isc_mem_strdup(mctx, DNS_AS_STR(token));
1274 result = ISC_R_NOMEMORY;
1275 goto log_and_cleanup;
1277 if (!lctx->ttl_known &&
1278 !lctx->default_ttl_known) {
1279 (*callbacks->error)(callbacks,
1280 "%s: %s:%lu: no TTL specified",
1281 "dns_master_load", source, line);
1282 result = DNS_R_NOTTL;
1283 if (MANYERRS(lctx, result)) {
1284 SETRESULT(lctx, result);
1286 } else if (result != ISC_R_SUCCESS)
1287 goto insist_and_cleanup;
1288 } else if (!explicit_ttl &&
1289 lctx->default_ttl_known) {
1290 lctx->ttl = lctx->default_ttl;
1293 * If the class specified does not match the
1294 * zone's class print out a error message and
1297 if (rdclass != 0 && rdclass != lctx->zclass) {
1300 result = generate(lctx, range, lhs, gtype, rhs,
1302 if (MANYERRS(lctx, result)) {
1303 SETRESULT(lctx, result);
1304 } else if (result != ISC_R_SUCCESS)
1305 goto insist_and_cleanup;
1308 } else if (strncasecmp(DNS_AS_STR(token),
1310 (callbacks->error)(callbacks,
1312 "unknown $ directive '%s'",
1313 "dns_master_load", source, line,
1315 result = DNS_R_SYNTAX;
1316 if (MANYERRS(lctx, result)) {
1317 SETRESULT(lctx, result);
1318 } else if (result != ISC_R_SUCCESS)
1319 goto insist_and_cleanup;
1323 * Normal processing resumes.
1325 * Find a free name buffer.
1327 for (new_in_use = 0; new_in_use < NBUFS; new_in_use++)
1328 if (!ictx->in_use[new_in_use])
1330 INSIST(new_in_use < NBUFS);
1331 dns_fixedname_init(&ictx->fixed[new_in_use]);
1332 new_name = dns_fixedname_name(&ictx->fixed[new_in_use]);
1333 isc_buffer_init(&buffer, token.value.as_region.base,
1334 token.value.as_region.length);
1335 isc_buffer_add(&buffer, token.value.as_region.length);
1336 isc_buffer_setactive(&buffer,
1337 token.value.as_region.length);
1338 result = dns_name_fromtext(new_name, &buffer,
1339 ictx->origin, ISC_FALSE, NULL);
1340 if (MANYERRS(lctx, result)) {
1341 SETRESULT(lctx, result);
1343 read_till_eol = ISC_TRUE;
1345 } else if (result != ISC_R_SUCCESS)
1346 goto log_and_cleanup;
1349 * Finish $ORIGIN / $INCLUDE processing if required.
1351 if (finish_origin) {
1352 if (ictx->origin_in_use != -1)
1353 ictx->in_use[ictx->origin_in_use] =
1355 ictx->origin_in_use = new_in_use;
1356 ictx->in_use[ictx->origin_in_use] = ISC_TRUE;
1357 ictx->origin = new_name;
1358 finish_origin = ISC_FALSE;
1362 if (finish_include) {
1363 finish_include = ISC_FALSE;
1364 result = pushfile(include_file, new_name, lctx);
1365 if (MANYERRS(lctx, result)) {
1366 SETRESULT(lctx, result);
1367 LOGITFILE(result, include_file);
1369 } else if (result != ISC_R_SUCCESS) {
1370 LOGITFILE(result, include_file);
1371 goto insist_and_cleanup;
1374 line = isc_lex_getsourceline(lctx->lex);
1375 source = isc_lex_getsourcename(lctx->lex);
1380 * "$" Processing Finished
1384 * If we are processing glue and the new name does
1385 * not match the current glue name, commit the glue
1386 * and pop stacks leaving us in 'normal' processing
1387 * state. Linked lists are undone by commit().
1389 if (ictx->glue != NULL &&
1390 dns_name_compare(ictx->glue, new_name) != 0) {
1391 result = commit(callbacks, lctx, &glue_list,
1394 if (MANYERRS(lctx, result)) {
1395 SETRESULT(lctx, result);
1396 } else if (result != ISC_R_SUCCESS)
1397 goto insist_and_cleanup;
1398 if (ictx->glue_in_use != -1)
1399 ictx->in_use[ictx->glue_in_use] =
1401 ictx->glue_in_use = -1;
1403 rdcount = rdcount_save;
1404 rdlcount = rdlcount_save;
1405 target = target_save;
1409 * If we are in 'normal' processing state and the new
1410 * name does not match the current name, see if the
1411 * new name is for glue and treat it as such,
1412 * otherwise we have a new name so commit what we
1415 if ((ictx->glue == NULL) && (ictx->current == NULL ||
1416 dns_name_compare(ictx->current, new_name) != 0)) {
1417 if (current_has_delegation &&
1418 is_glue(¤t_list, new_name)) {
1419 rdcount_save = rdcount;
1420 rdlcount_save = rdlcount;
1421 target_save = target;
1422 ictx->glue = new_name;
1423 ictx->glue_in_use = new_in_use;
1424 ictx->in_use[ictx->glue_in_use] =
1427 result = commit(callbacks, lctx,
1431 ictx->current_line);
1432 if (MANYERRS(lctx, result)) {
1433 SETRESULT(lctx, result);
1434 } else if (result != ISC_R_SUCCESS)
1435 goto insist_and_cleanup;
1438 if (ictx->current_in_use != -1)
1439 ictx->in_use[ictx->current_in_use] =
1441 ictx->current_in_use = new_in_use;
1442 ictx->in_use[ictx->current_in_use] =
1444 ictx->current = new_name;
1445 current_has_delegation = ISC_FALSE;
1446 isc_buffer_init(&target, target_mem,
1450 * Check for internal wildcards.
1452 if ((lctx->options & DNS_MASTER_CHECKWILDCARD)
1454 check_wildcard(ictx, source, line,
1458 if ((lctx->options & DNS_MASTER_ZONE) != 0 &&
1459 (lctx->options & DNS_MASTER_SLAVE) == 0 &&
1460 !dns_name_issubdomain(new_name, lctx->top))
1462 char namebuf[DNS_NAME_FORMATSIZE];
1463 dns_name_format(new_name, namebuf,
1466 * Ignore out-of-zone data.
1468 (*callbacks->warn)(callbacks,
1470 "ignoring out-of-zone data (%s)",
1471 source, line, namebuf);
1472 ictx->drop = ISC_TRUE;
1474 ictx->drop = ISC_FALSE;
1476 UNEXPECTED_ERROR(__FILE__, __LINE__,
1477 "%s:%lu: isc_lex_gettoken() returned "
1478 "unexpected token type (%d)",
1479 source, line, token.type);
1480 result = ISC_R_UNEXPECTED;
1481 if (MANYERRS(lctx, result)) {
1482 SETRESULT(lctx, result);
1485 } else if (result != ISC_R_SUCCESS)
1486 goto insist_and_cleanup;
1490 * Find TTL, class and type. Both TTL and class are optional
1491 * and may occur in any order if they exist. TTL and class
1492 * come before type which must exist.
1494 * [<TTL>] [<class>] <type> <RDATA>
1495 * [<class>] [<TTL>] <type> <RDATA>
1501 GETTOKEN(lctx->lex, 0, &token, initialws);
1504 if (token.type == isc_tokentype_eol) {
1505 read_till_eol = ISC_FALSE;
1506 continue; /* blank line */
1509 if (token.type == isc_tokentype_eof) {
1510 WARNUNEXPECTEDEOF(lctx->lex);
1511 read_till_eol = ISC_FALSE;
1512 isc_lex_ungettoken(lctx->lex, &token);
1516 if (ictx->current == NULL) {
1517 (*callbacks->error)(callbacks,
1518 "%s:%lu: no current owner name",
1520 result = DNS_R_NOOWNER;
1521 if (MANYERRS(lctx, result)) {
1522 SETRESULT(lctx, result);
1523 read_till_eol = ISC_TRUE;
1525 } else if (result != ISC_R_SUCCESS)
1526 goto insist_and_cleanup;
1530 if (dns_rdataclass_fromtext(&rdclass,
1531 &token.value.as_textregion)
1533 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1535 explicit_ttl = ISC_FALSE;
1536 if (dns_ttl_fromtext(&token.value.as_textregion, &lctx->ttl)
1538 limit_ttl(callbacks, source, line, &lctx->ttl);
1539 explicit_ttl = ISC_TRUE;
1540 lctx->ttl_known = ISC_TRUE;
1541 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1544 if (token.type != isc_tokentype_string) {
1545 UNEXPECTED_ERROR(__FILE__, __LINE__,
1546 "isc_lex_gettoken() returned unexpected token type");
1547 result = ISC_R_UNEXPECTED;
1548 if (MANYERRS(lctx, result)) {
1549 SETRESULT(lctx, result);
1550 read_till_eol = ISC_TRUE;
1552 } else if (result != ISC_R_SUCCESS)
1553 goto insist_and_cleanup;
1557 dns_rdataclass_fromtext(&rdclass,
1558 &token.value.as_textregion)
1560 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1562 if (token.type != isc_tokentype_string) {
1563 UNEXPECTED_ERROR(__FILE__, __LINE__,
1564 "isc_lex_gettoken() returned unexpected token type");
1565 result = ISC_R_UNEXPECTED;
1566 if (MANYERRS(lctx, result)) {
1567 SETRESULT(lctx, result);
1568 read_till_eol = ISC_TRUE;
1570 } else if (result != ISC_R_SUCCESS)
1571 goto insist_and_cleanup;
1574 result = dns_rdatatype_fromtext(&type,
1575 &token.value.as_textregion);
1576 if (result != ISC_R_SUCCESS) {
1577 (*callbacks->warn)(callbacks,
1578 "%s:%lu: unknown RR type '%.*s'",
1580 token.value.as_textregion.length,
1581 token.value.as_textregion.base);
1582 if (MANYERRS(lctx, result)) {
1583 SETRESULT(lctx, result);
1584 read_till_eol = ISC_TRUE;
1586 } else if (result != ISC_R_SUCCESS)
1587 goto insist_and_cleanup;
1591 * If the class specified does not match the zone's class
1592 * print out a error message and exit.
1594 if (rdclass != 0 && rdclass != lctx->zclass) {
1597 dns_rdataclass_format(rdclass, classname1,
1598 sizeof(classname1));
1599 dns_rdataclass_format(lctx->zclass, classname2,
1600 sizeof(classname2));
1601 (*callbacks->error)(callbacks,
1602 "%s:%lu: class '%s' != "
1605 classname1, classname2);
1606 result = DNS_R_BADCLASS;
1607 if (MANYERRS(lctx, result)) {
1608 SETRESULT(lctx, result);
1609 read_till_eol = ISC_TRUE;
1611 } else if (result != ISC_R_SUCCESS)
1612 goto insist_and_cleanup;
1615 if (type == dns_rdatatype_ns && ictx->glue == NULL)
1616 current_has_delegation = ISC_TRUE;
1619 * RFC1123: MD and MF are not allowed to be loaded from
1622 if ((lctx->options & DNS_MASTER_ZONE) != 0 &&
1623 (lctx->options & DNS_MASTER_SLAVE) == 0 &&
1624 (type == dns_rdatatype_md || type == dns_rdatatype_mf)) {
1625 char typename[DNS_RDATATYPE_FORMATSIZE];
1627 result = DNS_R_OBSOLETE;
1629 dns_rdatatype_format(type, typename, sizeof(typename));
1630 (*callbacks->error)(callbacks,
1631 "%s:%lu: %s '%s': %s",
1634 dns_result_totext(result));
1635 if (MANYERRS(lctx, result)) {
1636 SETRESULT(lctx, result);
1638 goto insist_and_cleanup;
1642 * Find a rdata structure.
1644 if (rdcount == rdata_size) {
1645 new_rdata = grow_rdata(rdata_size + RDSZ, rdata,
1646 rdata_size, ¤t_list,
1648 if (new_rdata == NULL) {
1649 result = ISC_R_NOMEMORY;
1650 goto log_and_cleanup;
1657 * Peek at the NS record.
1659 if (type == dns_rdatatype_ns &&
1660 lctx->zclass == dns_rdataclass_in &&
1661 (lctx->options & DNS_MASTER_CHECKNS) != 0) {
1663 GETTOKEN(lctx->lex, 0, &token, ISC_FALSE);
1664 result = check_ns(lctx, &token, source, line);
1665 isc_lex_ungettoken(lctx->lex, &token);
1666 if ((lctx->options & DNS_MASTER_FATALNS) != 0) {
1667 if (MANYERRS(lctx, result)) {
1668 SETRESULT(lctx, result);
1669 } else if (result != ISC_R_SUCCESS)
1670 goto insist_and_cleanup;
1677 options &= ~DNS_RDATA_CHECKREVERSE;
1678 if ((lctx->options & DNS_MASTER_CHECKNAMES) != 0) {
1682 name = (ictx->glue != NULL) ? ictx->glue :
1684 ok = dns_rdata_checkowner(name, lctx->zclass, type,
1687 char namebuf[DNS_NAME_FORMATSIZE];
1689 dns_name_format(name, namebuf, sizeof(namebuf));
1690 result = DNS_R_BADOWNERNAME;
1691 desc = dns_result_totext(result);
1692 if ((lctx->options & DNS_MASTER_CHECKNAMESFAIL) != 0) {
1693 (*callbacks->error)(callbacks,
1697 if (MANYERRS(lctx, result)) {
1698 SETRESULT(lctx, result);
1699 } else if (result != ISC_R_SUCCESS)
1702 (*callbacks->warn)(callbacks,
1708 if (type == dns_rdatatype_ptr &&
1709 (dns_name_issubdomain(name, &in_addr_arpa) ||
1710 dns_name_issubdomain(name, &ip6_arpa) ||
1711 dns_name_issubdomain(name, &ip6_int)))
1712 options |= DNS_RDATA_CHECKREVERSE;
1716 * Read rdata contents.
1718 dns_rdata_init(&rdata[rdcount]);
1720 result = dns_rdata_fromtext(&rdata[rdcount], lctx->zclass,
1721 type, lctx->lex, ictx->origin,
1722 options, lctx->mctx, &target,
1724 if (MANYERRS(lctx, result)) {
1725 SETRESULT(lctx, result);
1727 } else if (result != ISC_R_SUCCESS)
1728 goto insist_and_cleanup;
1735 if (type == dns_rdatatype_soa &&
1736 (lctx->options & DNS_MASTER_ZONE) != 0 &&
1737 dns_name_compare(ictx->current, lctx->top) != 0) {
1738 char namebuf[DNS_NAME_FORMATSIZE];
1739 dns_name_format(ictx->current, namebuf,
1741 (*callbacks->error)(callbacks,
1743 "record not at top of zone (%s)",
1744 source, line, namebuf);
1745 result = DNS_R_NOTZONETOP;
1746 if (MANYERRS(lctx, result)) {
1747 SETRESULT(lctx, result);
1748 read_till_eol = ISC_TRUE;
1751 } else if (result != ISC_R_SUCCESS)
1752 goto insist_and_cleanup;
1756 if (type == dns_rdatatype_rrsig ||
1757 type == dns_rdatatype_sig)
1758 covers = dns_rdata_covers(&rdata[rdcount]);
1762 if (!lctx->ttl_known && !lctx->default_ttl_known) {
1763 if (type == dns_rdatatype_soa) {
1764 (*callbacks->warn)(callbacks,
1765 "%s:%lu: no TTL specified; "
1766 "using SOA MINTTL instead",
1768 lctx->ttl = dns_soa_getminimum(&rdata[rdcount]);
1769 limit_ttl(callbacks, source, line, &lctx->ttl);
1770 lctx->default_ttl = lctx->ttl;
1771 lctx->default_ttl_known = ISC_TRUE;
1772 } else if ((lctx->options & DNS_MASTER_HINT) != 0) {
1774 * Zero TTL's are fine for hints.
1777 lctx->default_ttl = lctx->ttl;
1778 lctx->default_ttl_known = ISC_TRUE;
1780 (*callbacks->warn)(callbacks,
1781 "%s:%lu: no TTL specified; "
1784 result = DNS_R_NOTTL;
1785 if (MANYERRS(lctx, result)) {
1786 SETRESULT(lctx, result);
1789 goto insist_and_cleanup;
1792 } else if (!explicit_ttl && lctx->default_ttl_known) {
1793 lctx->ttl = lctx->default_ttl;
1794 } else if (!explicit_ttl && lctx->warn_1035) {
1795 (*callbacks->warn)(callbacks,
1797 "using RFC1035 TTL semantics",
1799 lctx->warn_1035 = ISC_FALSE;
1802 if (type == dns_rdatatype_rrsig && lctx->warn_sigexpired) {
1803 dns_rdata_rrsig_t sig;
1804 result = dns_rdata_tostruct(&rdata[rdcount], &sig,
1806 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1807 if (isc_serial_lt(sig.timeexpire, now)) {
1808 (*callbacks->warn)(callbacks,
1810 "signature has expired",
1812 lctx->warn_sigexpired = ISC_FALSE;
1816 if ((type == dns_rdatatype_sig || type == dns_rdatatype_nxt) &&
1817 lctx->warn_tcr && (lctx->options & DNS_MASTER_ZONE) != 0 &&
1818 (lctx->options & DNS_MASTER_SLAVE) == 0) {
1819 (*callbacks->warn)(callbacks, "%s:%lu: old style DNSSEC "
1820 " zone detected", source, line);
1821 lctx->warn_tcr = ISC_FALSE;
1824 if ((lctx->options & DNS_MASTER_AGETTL) != 0) {
1826 * Adjust the TTL for $DATE. If the RR has already
1827 * expired, ignore it.
1829 if (lctx->ttl < ttl_offset)
1831 lctx->ttl -= ttl_offset;
1835 * Find type in rdatalist.
1836 * If it does not exist create new one and prepend to list
1837 * as this will mimimise list traversal.
1839 if (ictx->glue != NULL)
1840 this = ISC_LIST_HEAD(glue_list);
1842 this = ISC_LIST_HEAD(current_list);
1844 while (this != NULL) {
1845 if (this->type == type && this->covers == covers)
1847 this = ISC_LIST_NEXT(this, link);
1851 if (rdlcount == rdatalist_size) {
1853 grow_rdatalist(rdatalist_size + RDLSZ,
1859 if (new_rdatalist == NULL) {
1860 result = ISC_R_NOMEMORY;
1861 goto log_and_cleanup;
1863 rdatalist = new_rdatalist;
1864 rdatalist_size += RDLSZ;
1866 this = &rdatalist[rdlcount++];
1868 this->covers = covers;
1869 this->rdclass = lctx->zclass;
1870 this->ttl = lctx->ttl;
1871 ISC_LIST_INIT(this->rdata);
1872 if (ictx->glue != NULL)
1873 ISC_LIST_INITANDPREPEND(glue_list, this, link);
1875 ISC_LIST_INITANDPREPEND(current_list, this,
1877 } else if (this->ttl != lctx->ttl) {
1878 (*callbacks->warn)(callbacks,
1880 "TTL set to prior TTL (%lu)",
1881 source, line, this->ttl);
1882 lctx->ttl = this->ttl;
1885 ISC_LIST_APPEND(this->rdata, &rdata[rdcount], link);
1886 if (ictx->glue != NULL)
1887 ictx->glue_line = line;
1889 ictx->current_line = line;
1893 * We must have at least 64k as rdlen is 16 bits.
1894 * If we don't commit everything we have so far.
1896 if ((target.length - target.used) < MINTSIZ)
1900 } while (!done && (lctx->loop_cnt == 0 || loop_cnt++ < lctx->loop_cnt));
1903 * Commit what has not yet been committed.
1905 result = commit(callbacks, lctx, ¤t_list, ictx->current,
1906 source, ictx->current_line);
1907 if (MANYERRS(lctx, result)) {
1908 SETRESULT(lctx, result);
1909 } else if (result != ISC_R_SUCCESS)
1910 goto insist_and_cleanup;
1911 result = commit(callbacks, lctx, &glue_list, ictx->glue,
1912 source, ictx->glue_line);
1913 if (MANYERRS(lctx, result)) {
1914 SETRESULT(lctx, result);
1915 } else if (result != ISC_R_SUCCESS)
1916 goto insist_and_cleanup;
1919 INSIST(lctx->done != NULL && lctx->task != NULL);
1920 result = DNS_R_CONTINUE;
1921 } else if (result == ISC_R_SUCCESS && lctx->result != ISC_R_SUCCESS) {
1922 result = lctx->result;
1923 } else if (result == ISC_R_SUCCESS && lctx->seen_include)
1924 result = DNS_R_SEENINCLUDE;
1931 INSIST(result != ISC_R_SUCCESS);
1934 while ((this = ISC_LIST_HEAD(current_list)) != NULL)
1935 ISC_LIST_UNLINK(current_list, this, link);
1936 while ((this = ISC_LIST_HEAD(glue_list)) != NULL)
1937 ISC_LIST_UNLINK(glue_list, this, link);
1938 if (rdatalist != NULL)
1939 isc_mem_put(mctx, rdatalist,
1940 rdatalist_size * sizeof(*rdatalist));
1942 isc_mem_put(mctx, rdata, rdata_size * sizeof(*rdata));
1943 if (target_mem != NULL)
1944 isc_mem_put(mctx, target_mem, target_size);
1945 if (include_file != NULL)
1946 isc_mem_free(mctx, include_file);
1948 isc_mem_free(mctx, range);
1950 isc_mem_free(mctx, lhs);
1952 isc_mem_free(mctx, gtype);
1954 isc_mem_free(mctx, rhs);
1959 pushfile(const char *master_file, dns_name_t *origin, dns_loadctx_t *lctx) {
1960 isc_result_t result;
1962 dns_incctx_t *new = NULL;
1966 REQUIRE(master_file != NULL);
1967 REQUIRE(DNS_LCTX_VALID(lctx));
1970 lctx->seen_include = ISC_TRUE;
1972 result = incctx_create(lctx->mctx, origin, &new);
1973 if (result != ISC_R_SUCCESS)
1976 /* Set current domain. */
1977 if (ictx->glue != NULL || ictx->current != NULL) {
1978 for (new_in_use = 0; new_in_use < NBUFS; new_in_use++)
1979 if (!new->in_use[new_in_use])
1981 INSIST(new_in_use < NBUFS);
1982 new->current_in_use = new_in_use;
1984 dns_fixedname_name(&new->fixed[new->current_in_use]);
1985 new->in_use[new->current_in_use] = ISC_TRUE;
1986 dns_name_toregion((ictx->glue != NULL) ?
1987 ictx->glue : ictx->current, &r);
1988 dns_name_fromregion(new->current, &r);
1989 new->drop = ictx->drop;
1992 result = (lctx->openfile)(lctx, master_file);
1993 if (result != ISC_R_SUCCESS)
1997 return (ISC_R_SUCCESS);
2001 incctx_destroy(lctx->mctx, new);
2005 static inline isc_result_t
2006 read_and_check(isc_boolean_t do_read, isc_buffer_t *buffer,
2007 size_t len, FILE *f)
2009 isc_result_t result;
2012 INSIST(isc_buffer_availablelength(buffer) >= len);
2013 result = isc_stdio_read(isc_buffer_used(buffer), 1, len,
2015 if (result != ISC_R_SUCCESS)
2017 isc_buffer_add(buffer, len);
2018 } else if (isc_buffer_remaininglength(buffer) < len)
2019 return (ISC_R_RANGE);
2021 return (ISC_R_SUCCESS);
2025 load_raw(dns_loadctx_t *lctx) {
2026 isc_result_t result = ISC_R_SUCCESS;
2027 isc_boolean_t done = ISC_FALSE;
2028 unsigned int loop_cnt = 0;
2029 dns_rdatacallbacks_t *callbacks;
2030 unsigned char namebuf[DNS_NAME_MAXWIRE];
2033 rdatalist_head_t head, dummy;
2034 dns_rdatalist_t rdatalist;
2035 isc_mem_t *mctx = lctx->mctx;
2036 dns_rdata_t *rdata = NULL;
2037 unsigned int rdata_size = 0;
2038 int target_size = TSIZ;
2039 isc_buffer_t target;
2040 unsigned char *target_mem = NULL;
2042 REQUIRE(DNS_LCTX_VALID(lctx));
2043 callbacks = lctx->callbacks;
2046 dns_masterrawheader_t header;
2047 isc_uint32_t format, version, dumptime;
2048 size_t hdrlen = sizeof(format) + sizeof(version) +
2051 INSIST(hdrlen <= sizeof(header));
2052 isc_buffer_init(&target, &header, sizeof(header));
2054 result = isc_stdio_read(&header, 1, hdrlen, lctx->f, NULL);
2055 if (result != ISC_R_SUCCESS) {
2056 UNEXPECTED_ERROR(__FILE__, __LINE__,
2057 "isc_stdio_read failed: %s",
2058 isc_result_totext(result));
2061 isc_buffer_add(&target, hdrlen);
2062 format = isc_buffer_getuint32(&target);
2063 if (format != dns_masterformat_raw) {
2064 (*callbacks->error)(callbacks,
2066 "file format mismatch");
2067 return (ISC_R_NOTIMPLEMENTED);
2070 version = isc_buffer_getuint32(&target);
2071 if (version > DNS_RAWFORMAT_VERSION) {
2072 (*callbacks->error)(callbacks,
2074 "unsupported file format version");
2075 return (ISC_R_NOTIMPLEMENTED);
2078 /* Empty read: currently, we do not use dumptime */
2079 dumptime = isc_buffer_getuint32(&target);
2081 lctx->first = ISC_FALSE;
2084 ISC_LIST_INIT(head);
2085 ISC_LIST_INIT(dummy);
2086 dns_rdatalist_init(&rdatalist);
2089 * Allocate target_size of buffer space. This is greater than twice
2090 * the maximum individual RR data size.
2092 target_mem = isc_mem_get(mctx, target_size);
2093 if (target_mem == NULL) {
2094 result = ISC_R_NOMEMORY;
2097 isc_buffer_init(&target, target_mem, target_size);
2100 * In the following loop, we regard any error fatal regardless of
2101 * whether "MANYERRORS" is set in the context option. This is because
2102 * normal errors should already have been checked at creation time.
2103 * Besides, it is very unlikely that we can recover from an error
2104 * in this format, and so trying to continue parsing erroneous data
2105 * does not really make sense.
2108 (lctx->loop_cnt == 0 || loop_cnt < lctx->loop_cnt);
2110 unsigned int i, rdcount, consumed_name;
2111 isc_uint16_t namelen;
2112 isc_uint32_t totallen;
2113 size_t minlen, readlen;
2114 isc_boolean_t sequential_read = ISC_FALSE;
2116 /* Read the data length */
2117 isc_buffer_clear(&target);
2118 INSIST(isc_buffer_availablelength(&target) >=
2120 result = isc_stdio_read(target.base, 1, sizeof(totallen),
2122 if (result == ISC_R_EOF) {
2123 result = ISC_R_SUCCESS;
2127 if (result != ISC_R_SUCCESS)
2129 isc_buffer_add(&target, sizeof(totallen));
2130 totallen = isc_buffer_getuint32(&target);
2132 * Validation: the input data must at least contain the common
2135 minlen = sizeof(totallen) + sizeof(isc_uint16_t) +
2136 sizeof(isc_uint16_t) + sizeof(isc_uint16_t) +
2137 sizeof(isc_uint32_t) + sizeof(isc_uint32_t);
2138 if (totallen < minlen) {
2139 result = ISC_R_RANGE;
2142 totallen -= sizeof(totallen);
2144 isc_buffer_clear(&target);
2145 if (totallen > isc_buffer_availablelength(&target)) {
2147 * The default buffer size should typically be large
2148 * enough to store the entire RRset. We could try to
2149 * allocate enough space if this is not the case, but
2150 * it might cause a hazardous result when "totallen"
2151 * is forged. Thus, we'd rather take an inefficient
2152 * but robust approach in this atypical case: read
2153 * data step by step, and commit partial data when
2154 * necessary. Note that the buffer must be large
2155 * enough to store the "header part", owner name, and
2156 * at least one rdata (however large it is).
2158 sequential_read = ISC_TRUE;
2159 readlen = minlen - sizeof(totallen);
2162 * Typical case. We can read the whole RRset at once
2163 * with the default buffer.
2167 result = isc_stdio_read(target.base, 1, readlen,
2169 if (result != ISC_R_SUCCESS)
2171 isc_buffer_add(&target, readlen);
2173 /* Construct RRset headers */
2174 rdatalist.rdclass = isc_buffer_getuint16(&target);
2175 rdatalist.type = isc_buffer_getuint16(&target);
2176 rdatalist.covers = isc_buffer_getuint16(&target);
2177 rdatalist.ttl = isc_buffer_getuint32(&target);
2178 rdcount = isc_buffer_getuint32(&target);
2180 result = ISC_R_RANGE;
2183 INSIST(isc_buffer_consumedlength(&target) <= readlen);
2185 /* Owner name: length followed by name */
2186 result = read_and_check(sequential_read, &target,
2187 sizeof(namelen), lctx->f);
2188 if (result != ISC_R_SUCCESS)
2190 namelen = isc_buffer_getuint16(&target);
2191 if (namelen > sizeof(namebuf)) {
2192 result = ISC_R_RANGE;
2196 result = read_and_check(sequential_read, &target, namelen,
2198 if (result != ISC_R_SUCCESS)
2200 isc_buffer_setactive(&target, (unsigned int)namelen);
2201 isc_buffer_activeregion(&target, &r);
2202 dns_name_init(&name, NULL);
2203 dns_name_fromregion(&name, &r);
2204 isc_buffer_forward(&target, (unsigned int)namelen);
2205 consumed_name = isc_buffer_consumedlength(&target);
2207 /* Rdata contents. */
2208 if (rdcount > rdata_size) {
2209 dns_rdata_t *new_rdata = NULL;
2211 new_rdata = grow_rdata(rdata_size + RDSZ, rdata,
2214 if (new_rdata == NULL) {
2215 result = ISC_R_NOMEMORY;
2223 for (i = 0; i < rdcount; i++) {
2226 dns_rdata_init(&rdata[i]);
2228 if (sequential_read &&
2229 isc_buffer_availablelength(&target) < MINTSIZ) {
2232 INSIST(i > 0); /* detect an infinite loop */
2234 /* Partial Commit. */
2235 ISC_LIST_APPEND(head, &rdatalist, link);
2236 result = commit(callbacks, lctx, &head, &name,
2238 for (j = 0; j < i; j++) {
2239 ISC_LIST_UNLINK(rdatalist.rdata,
2241 dns_rdata_reset(&rdata[j]);
2243 if (result != ISC_R_SUCCESS)
2246 /* Rewind the buffer and continue */
2247 isc_buffer_clear(&target);
2248 isc_buffer_add(&target, consumed_name);
2249 isc_buffer_forward(&target, consumed_name);
2258 result = read_and_check(sequential_read, &target,
2259 sizeof(rdlen), lctx->f);
2260 if (result != ISC_R_SUCCESS)
2262 rdlen = isc_buffer_getuint16(&target);
2265 result = read_and_check(sequential_read, &target,
2267 if (result != ISC_R_SUCCESS)
2269 isc_buffer_setactive(&target, (unsigned int)rdlen);
2270 isc_buffer_activeregion(&target, &r);
2271 isc_buffer_forward(&target, (unsigned int)rdlen);
2272 dns_rdata_fromregion(&rdata[i], rdatalist.rdclass,
2273 rdatalist.type, &r);
2275 ISC_LIST_APPEND(rdatalist.rdata, &rdata[i], link);
2279 * Sanity check. Still having remaining space is not
2280 * necessarily critical, but it very likely indicates broken
2281 * or malformed data.
2283 if (isc_buffer_remaininglength(&target) != 0) {
2284 result = ISC_R_RANGE;
2288 ISC_LIST_APPEND(head, &rdatalist, link);
2290 /* Commit this RRset. rdatalist will be unlinked. */
2291 result = commit(callbacks, lctx, &head, &name, NULL, 0);
2293 for (i = 0; i < rdcount; i++) {
2294 ISC_LIST_UNLINK(rdatalist.rdata, &rdata[i], link);
2295 dns_rdata_reset(&rdata[i]);
2298 if (result != ISC_R_SUCCESS)
2303 INSIST(lctx->done != NULL && lctx->task != NULL);
2304 result = DNS_R_CONTINUE;
2305 } else if (result == ISC_R_SUCCESS && lctx->result != ISC_R_SUCCESS)
2306 result = lctx->result;
2310 isc_mem_put(mctx, rdata, rdata_size * sizeof(*rdata));
2311 if (target_mem != NULL)
2312 isc_mem_put(mctx, target_mem, target_size);
2313 if (result != ISC_R_SUCCESS && result != DNS_R_CONTINUE) {
2314 (*callbacks->error)(callbacks, "dns_master_load: %s",
2315 dns_result_totext(result));
2322 dns_master_loadfile(const char *master_file, dns_name_t *top,
2324 dns_rdataclass_t zclass, unsigned int options,
2325 dns_rdatacallbacks_t *callbacks, isc_mem_t *mctx)
2327 return (dns_master_loadfile2(master_file, top, origin, zclass, options,
2328 callbacks, mctx, dns_masterformat_text));
2332 dns_master_loadfile2(const char *master_file, dns_name_t *top,
2334 dns_rdataclass_t zclass, unsigned int options,
2335 dns_rdatacallbacks_t *callbacks, isc_mem_t *mctx,
2336 dns_masterformat_t format)
2338 dns_loadctx_t *lctx = NULL;
2339 isc_result_t result;
2341 result = loadctx_create(format, mctx, options, top, zclass, origin,
2342 callbacks, NULL, NULL, NULL, NULL, &lctx);
2343 if (result != ISC_R_SUCCESS)
2346 result = (lctx->openfile)(lctx, master_file);
2347 if (result != ISC_R_SUCCESS)
2350 result = (lctx->load)(lctx);
2351 INSIST(result != DNS_R_CONTINUE);
2354 dns_loadctx_detach(&lctx);
2359 dns_master_loadfileinc(const char *master_file, dns_name_t *top,
2360 dns_name_t *origin, dns_rdataclass_t zclass,
2361 unsigned int options, dns_rdatacallbacks_t *callbacks,
2362 isc_task_t *task, dns_loaddonefunc_t done,
2363 void *done_arg, dns_loadctx_t **lctxp, isc_mem_t *mctx)
2365 return (dns_master_loadfileinc2(master_file, top, origin, zclass,
2366 options, callbacks, task, done,
2367 done_arg, lctxp, mctx,
2368 dns_masterformat_text));
2372 dns_master_loadfileinc2(const char *master_file, dns_name_t *top,
2373 dns_name_t *origin, dns_rdataclass_t zclass,
2374 unsigned int options, dns_rdatacallbacks_t *callbacks,
2375 isc_task_t *task, dns_loaddonefunc_t done,
2376 void *done_arg, dns_loadctx_t **lctxp, isc_mem_t *mctx,
2377 dns_masterformat_t format)
2379 dns_loadctx_t *lctx = NULL;
2380 isc_result_t result;
2382 REQUIRE(task != NULL);
2383 REQUIRE(done != NULL);
2385 result = loadctx_create(format, mctx, options, top, zclass, origin,
2386 callbacks, task, done, done_arg, NULL, &lctx);
2387 if (result != ISC_R_SUCCESS)
2390 result = (lctx->openfile)(lctx, master_file);
2391 if (result != ISC_R_SUCCESS)
2394 result = task_send(lctx);
2395 if (result == ISC_R_SUCCESS) {
2396 dns_loadctx_attach(lctx, lctxp);
2397 return (DNS_R_CONTINUE);
2401 dns_loadctx_detach(&lctx);
2406 dns_master_loadstream(FILE *stream, dns_name_t *top, dns_name_t *origin,
2407 dns_rdataclass_t zclass, unsigned int options,
2408 dns_rdatacallbacks_t *callbacks, isc_mem_t *mctx)
2410 isc_result_t result;
2411 dns_loadctx_t *lctx = NULL;
2413 REQUIRE(stream != NULL);
2415 result = loadctx_create(dns_masterformat_text, mctx, options, top,
2416 zclass, origin, callbacks, NULL, NULL, NULL,
2418 if (result != ISC_R_SUCCESS)
2421 result = isc_lex_openstream(lctx->lex, stream);
2422 if (result != ISC_R_SUCCESS)
2425 result = (lctx->load)(lctx);
2426 INSIST(result != DNS_R_CONTINUE);
2430 dns_loadctx_detach(&lctx);
2435 dns_master_loadstreaminc(FILE *stream, dns_name_t *top, dns_name_t *origin,
2436 dns_rdataclass_t zclass, unsigned int options,
2437 dns_rdatacallbacks_t *callbacks, isc_task_t *task,
2438 dns_loaddonefunc_t done, void *done_arg,
2439 dns_loadctx_t **lctxp, isc_mem_t *mctx)
2441 isc_result_t result;
2442 dns_loadctx_t *lctx = NULL;
2444 REQUIRE(stream != NULL);
2445 REQUIRE(task != NULL);
2446 REQUIRE(done != NULL);
2448 result = loadctx_create(dns_masterformat_text, mctx, options, top,
2449 zclass, origin, callbacks, task, done,
2450 done_arg, NULL, &lctx);
2451 if (result != ISC_R_SUCCESS)
2454 result = isc_lex_openstream(lctx->lex, stream);
2455 if (result != ISC_R_SUCCESS)
2458 result = task_send(lctx);
2459 if (result == ISC_R_SUCCESS) {
2460 dns_loadctx_attach(lctx, lctxp);
2461 return (DNS_R_CONTINUE);
2466 dns_loadctx_detach(&lctx);
2471 dns_master_loadbuffer(isc_buffer_t *buffer, dns_name_t *top,
2472 dns_name_t *origin, dns_rdataclass_t zclass,
2473 unsigned int options,
2474 dns_rdatacallbacks_t *callbacks, isc_mem_t *mctx)
2476 isc_result_t result;
2477 dns_loadctx_t *lctx = NULL;
2479 REQUIRE(buffer != NULL);
2481 result = loadctx_create(dns_masterformat_text, mctx, options, top,
2482 zclass, origin, callbacks, NULL, NULL, NULL,
2484 if (result != ISC_R_SUCCESS)
2487 result = isc_lex_openbuffer(lctx->lex, buffer);
2488 if (result != ISC_R_SUCCESS)
2491 result = (lctx->load)(lctx);
2492 INSIST(result != DNS_R_CONTINUE);
2495 dns_loadctx_detach(&lctx);
2500 dns_master_loadbufferinc(isc_buffer_t *buffer, dns_name_t *top,
2501 dns_name_t *origin, dns_rdataclass_t zclass,
2502 unsigned int options,
2503 dns_rdatacallbacks_t *callbacks, isc_task_t *task,
2504 dns_loaddonefunc_t done, void *done_arg,
2505 dns_loadctx_t **lctxp, isc_mem_t *mctx)
2507 isc_result_t result;
2508 dns_loadctx_t *lctx = NULL;
2510 REQUIRE(buffer != NULL);
2511 REQUIRE(task != NULL);
2512 REQUIRE(done != NULL);
2514 result = loadctx_create(dns_masterformat_text, mctx, options, top,
2515 zclass, origin, callbacks, task, done,
2516 done_arg, NULL, &lctx);
2517 if (result != ISC_R_SUCCESS)
2520 result = isc_lex_openbuffer(lctx->lex, buffer);
2521 if (result != ISC_R_SUCCESS)
2524 result = task_send(lctx);
2525 if (result == ISC_R_SUCCESS) {
2526 dns_loadctx_attach(lctx, lctxp);
2527 return (DNS_R_CONTINUE);
2531 dns_loadctx_detach(&lctx);
2536 dns_master_loadlexer(isc_lex_t *lex, dns_name_t *top,
2537 dns_name_t *origin, dns_rdataclass_t zclass,
2538 unsigned int options,
2539 dns_rdatacallbacks_t *callbacks, isc_mem_t *mctx)
2541 isc_result_t result;
2542 dns_loadctx_t *lctx = NULL;
2544 REQUIRE(lex != NULL);
2546 result = loadctx_create(dns_masterformat_text, mctx, options, top,
2547 zclass, origin, callbacks, NULL, NULL, NULL,
2549 if (result != ISC_R_SUCCESS)
2552 result = (lctx->load)(lctx);
2553 INSIST(result != DNS_R_CONTINUE);
2555 dns_loadctx_detach(&lctx);
2560 dns_master_loadlexerinc(isc_lex_t *lex, dns_name_t *top,
2561 dns_name_t *origin, dns_rdataclass_t zclass,
2562 unsigned int options,
2563 dns_rdatacallbacks_t *callbacks, isc_task_t *task,
2564 dns_loaddonefunc_t done, void *done_arg,
2565 dns_loadctx_t **lctxp, isc_mem_t *mctx)
2567 isc_result_t result;
2568 dns_loadctx_t *lctx = NULL;
2570 REQUIRE(lex != NULL);
2571 REQUIRE(task != NULL);
2572 REQUIRE(done != NULL);
2574 result = loadctx_create(dns_masterformat_text, mctx, options, top,
2575 zclass, origin, callbacks, task, done,
2576 done_arg, lex, &lctx);
2577 if (result != ISC_R_SUCCESS)
2580 result = task_send(lctx);
2581 if (result == ISC_R_SUCCESS) {
2582 dns_loadctx_attach(lctx, lctxp);
2583 return (DNS_R_CONTINUE);
2586 dns_loadctx_detach(&lctx);
2591 * Grow the slab of dns_rdatalist_t structures.
2592 * Re-link glue and current list.
2594 static dns_rdatalist_t *
2595 grow_rdatalist(int new_len, dns_rdatalist_t *old, int old_len,
2596 rdatalist_head_t *current, rdatalist_head_t *glue,
2599 dns_rdatalist_t *new;
2601 ISC_LIST(dns_rdatalist_t) save;
2602 dns_rdatalist_t *this;
2604 new = isc_mem_get(mctx, new_len * sizeof(*new));
2608 ISC_LIST_INIT(save);
2609 this = ISC_LIST_HEAD(*current);
2610 while ((this = ISC_LIST_HEAD(*current)) != NULL) {
2611 ISC_LIST_UNLINK(*current, this, link);
2612 ISC_LIST_APPEND(save, this, link);
2614 while ((this = ISC_LIST_HEAD(save)) != NULL) {
2615 ISC_LIST_UNLINK(save, this, link);
2616 new[rdlcount] = *this;
2617 ISC_LIST_APPEND(*current, &new[rdlcount], link);
2621 ISC_LIST_INIT(save);
2622 this = ISC_LIST_HEAD(*glue);
2623 while ((this = ISC_LIST_HEAD(*glue)) != NULL) {
2624 ISC_LIST_UNLINK(*glue, this, link);
2625 ISC_LIST_APPEND(save, this, link);
2627 while ((this = ISC_LIST_HEAD(save)) != NULL) {
2628 ISC_LIST_UNLINK(save, this, link);
2629 new[rdlcount] = *this;
2630 ISC_LIST_APPEND(*glue, &new[rdlcount], link);
2634 INSIST(rdlcount == old_len);
2636 isc_mem_put(mctx, old, old_len * sizeof(*old));
2641 * Grow the slab of rdata structs.
2642 * Re-link the current and glue chains.
2644 static dns_rdata_t *
2645 grow_rdata(int new_len, dns_rdata_t *old, int old_len,
2646 rdatalist_head_t *current, rdatalist_head_t *glue,
2651 ISC_LIST(dns_rdata_t) save;
2652 dns_rdatalist_t *this;
2655 new = isc_mem_get(mctx, new_len * sizeof(*new));
2658 memset(new, 0, new_len * sizeof(*new));
2661 * Copy current relinking.
2663 this = ISC_LIST_HEAD(*current);
2664 while (this != NULL) {
2665 ISC_LIST_INIT(save);
2666 while ((rdata = ISC_LIST_HEAD(this->rdata)) != NULL) {
2667 ISC_LIST_UNLINK(this->rdata, rdata, link);
2668 ISC_LIST_APPEND(save, rdata, link);
2670 while ((rdata = ISC_LIST_HEAD(save)) != NULL) {
2671 ISC_LIST_UNLINK(save, rdata, link);
2672 new[rdcount] = *rdata;
2673 ISC_LIST_APPEND(this->rdata, &new[rdcount], link);
2676 this = ISC_LIST_NEXT(this, link);
2680 * Copy glue relinking.
2682 this = ISC_LIST_HEAD(*glue);
2683 while (this != NULL) {
2684 ISC_LIST_INIT(save);
2685 while ((rdata = ISC_LIST_HEAD(this->rdata)) != NULL) {
2686 ISC_LIST_UNLINK(this->rdata, rdata, link);
2687 ISC_LIST_APPEND(save, rdata, link);
2689 while ((rdata = ISC_LIST_HEAD(save)) != NULL) {
2690 ISC_LIST_UNLINK(save, rdata, link);
2691 new[rdcount] = *rdata;
2692 ISC_LIST_APPEND(this->rdata, &new[rdcount], link);
2695 this = ISC_LIST_NEXT(this, link);
2697 INSIST(rdcount == old_len);
2699 isc_mem_put(mctx, old, old_len * sizeof(*old));
2704 * Convert each element from a rdatalist_t to rdataset then call commit.
2705 * Unlink each element as we go.
2709 commit(dns_rdatacallbacks_t *callbacks, dns_loadctx_t *lctx,
2710 rdatalist_head_t *head, dns_name_t *owner,
2711 const char *source, unsigned int line)
2713 dns_rdatalist_t *this;
2714 dns_rdataset_t dataset;
2715 isc_result_t result;
2716 char namebuf[DNS_NAME_FORMATSIZE];
2717 void (*error)(struct dns_rdatacallbacks *, const char *, ...);
2719 this = ISC_LIST_HEAD(*head);
2720 error = callbacks->error;
2723 return (ISC_R_SUCCESS);
2725 dns_rdataset_init(&dataset);
2726 RUNTIME_CHECK(dns_rdatalist_tordataset(this, &dataset)
2728 dataset.trust = dns_trust_ultimate;
2729 result = ((*callbacks->add)(callbacks->add_private, owner,
2731 if (result == ISC_R_NOMEMORY) {
2732 (*error)(callbacks, "dns_master_load: %s",
2733 dns_result_totext(result));
2734 } else if (result != ISC_R_SUCCESS) {
2735 dns_name_format(owner, namebuf,
2737 if (source != NULL) {
2738 (*error)(callbacks, "%s: %s:%lu: %s: %s",
2739 "dns_master_load", source, line,
2740 namebuf, dns_result_totext(result));
2742 (*error)(callbacks, "%s: %s: %s",
2743 "dns_master_load", namebuf,
2744 dns_result_totext(result));
2747 if (MANYERRS(lctx, result))
2748 SETRESULT(lctx, result);
2749 else if (result != ISC_R_SUCCESS)
2751 ISC_LIST_UNLINK(*head, this, link);
2752 this = ISC_LIST_HEAD(*head);
2753 } while (this != NULL);
2754 return (ISC_R_SUCCESS);
2758 * Returns ISC_TRUE if one of the NS rdata's contains 'owner'.
2761 static isc_boolean_t
2762 is_glue(rdatalist_head_t *head, dns_name_t *owner) {
2763 dns_rdatalist_t *this;
2765 isc_region_t region;
2771 this = ISC_LIST_HEAD(*head);
2772 while (this != NULL) {
2773 if (this->type == dns_rdatatype_ns)
2775 this = ISC_LIST_NEXT(this, link);
2780 rdata = ISC_LIST_HEAD(this->rdata);
2781 while (rdata != NULL) {
2782 dns_name_init(&name, NULL);
2783 dns_rdata_toregion(rdata, ®ion);
2784 dns_name_fromregion(&name, ®ion);
2785 if (dns_name_compare(&name, owner) == 0)
2787 rdata = ISC_LIST_NEXT(rdata, link);
2793 load_quantum(isc_task_t *task, isc_event_t *event) {
2794 isc_result_t result;
2795 dns_loadctx_t *lctx;
2797 REQUIRE(event != NULL);
2798 lctx = event->ev_arg;
2799 REQUIRE(DNS_LCTX_VALID(lctx));
2802 result = ISC_R_CANCELED;
2804 result = (lctx->load)(lctx);
2805 if (result == DNS_R_CONTINUE) {
2806 event->ev_arg = lctx;
2807 isc_task_send(task, &event);
2809 (lctx->done)(lctx->done_arg, result);
2810 isc_event_free(&event);
2811 dns_loadctx_detach(&lctx);
2816 task_send(dns_loadctx_t *lctx) {
2819 event = isc_event_allocate(lctx->mctx, NULL,
2820 DNS_EVENT_MASTERQUANTUM,
2821 load_quantum, lctx, sizeof(*event));
2823 return (ISC_R_NOMEMORY);
2824 isc_task_send(lctx->task, &event);
2825 return (ISC_R_SUCCESS);
2829 dns_loadctx_cancel(dns_loadctx_t *lctx) {
2830 REQUIRE(DNS_LCTX_VALID(lctx));
2833 lctx->canceled = ISC_TRUE;
2834 UNLOCK(&lctx->lock);