Coverage Report

Created: 2026-08-13 07:06

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