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