Coverage Report

Created: 2026-08-13 07:07

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