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