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