Coverage Report

Created: 2026-06-15 06:56

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