Coverage Report

Created: 2026-03-15 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/unbound/services/localzone.c
Line
Count
Source
1
/*
2
 * services/localzone.c - local zones authority service.
3
 *
4
 * Copyright (c) 2007, NLnet Labs. All rights reserved.
5
 *
6
 * This software is open source.
7
 * 
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 
12
 * Redistributions of source code must retain the above copyright notice,
13
 * this list of conditions and the following disclaimer.
14
 * 
15
 * Redistributions in binary form must reproduce the above copyright notice,
16
 * this list of conditions and the following disclaimer in the documentation
17
 * and/or other materials provided with the distribution.
18
 * 
19
 * Neither the name of the NLNET LABS nor the names of its contributors may
20
 * be used to endorse or promote products derived from this software without
21
 * specific prior written permission.
22
 * 
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
 */
35
36
/**
37
 * \file
38
 *
39
 * This file contains functions to enable local zone authority service.
40
 */
41
#include "config.h"
42
#include "services/localzone.h"
43
#include "sldns/str2wire.h"
44
#include "util/regional.h"
45
#include "util/config_file.h"
46
#include "util/data/dname.h"
47
#include "util/data/packed_rrset.h"
48
#include "util/data/msgencode.h"
49
#include "util/net_help.h"
50
#include "util/netevent.h"
51
#include "util/data/msgreply.h"
52
#include "util/data/msgparse.h"
53
#include "util/as112.h"
54
55
/* maximum RRs in an RRset, to cap possible 'endless' list RRs.
56
 * with 16 bytes for an A record, a 64K packet has about 4000 max */
57
0
#define LOCALZONE_RRSET_COUNT_MAX 4096
58
59
static const char* default_zones_reverse_array[] = {
60
  "127.in-addr.arpa.", /* reverse ip4 zone */
61
  "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.", /* reverse ip6 zone */
62
  0
63
};
64
const char** local_zones_default_reverse = default_zones_reverse_array;
65
66
static const char* default_zones_special_array[] = {
67
  "test.",    /* RFC 6761 */
68
  "invalid.",   /* RFC 6761 */
69
  "onion.",   /* RFC 7686 */
70
  "home.arpa.",   /* RFC 8375 */
71
  "resolver.arpa.", /* RFC 9462 */
72
  "service.arpa.",  /* RFC 9665 */
73
  0
74
};
75
const char** local_zones_default_special = default_zones_special_array;
76
77
/** print all RRsets in local zone */
78
static void
79
local_zone_out(struct local_zone* z)
80
0
{
81
0
  struct local_data* d;
82
0
  struct local_rrset* p;
83
0
  RBTREE_FOR(d, struct local_data*, &z->data) {
84
0
    for(p = d->rrsets; p; p = p->next) {
85
0
      log_nametypeclass(NO_VERBOSE, "rrset", d->name,
86
0
        ntohs(p->rrset->rk.type),
87
0
        ntohs(p->rrset->rk.rrset_class));
88
0
    }
89
0
  }
90
0
}
91
92
static void
93
local_zone_print(struct local_zone* z)
94
0
{
95
0
  char buf[64];
96
0
  lock_rw_rdlock(&z->lock);
97
0
  snprintf(buf, sizeof(buf), "%s zone",
98
0
    local_zone_type2str(z->type));
99
0
  log_nametypeclass(NO_VERBOSE, buf, z->name, 0, z->dclass);
100
0
  local_zone_out(z);
101
0
  lock_rw_unlock(&z->lock);
102
0
}
103
104
void local_zones_print(struct local_zones* zones)
105
0
{
106
0
  struct local_zone* z;
107
0
  lock_rw_rdlock(&zones->lock);
108
0
  log_info("number of auth zones %u", (unsigned)zones->ztree.count);
109
0
  RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
110
0
    local_zone_print(z);
111
0
  }
112
0
  lock_rw_unlock(&zones->lock);
113
0
}
114
115
struct local_zones* 
116
local_zones_create(void)
117
0
{
118
0
  struct local_zones* zones = (struct local_zones*)calloc(1, 
119
0
    sizeof(*zones));
120
0
  if(!zones)
121
0
    return NULL;
122
0
  rbtree_init(&zones->ztree, &local_zone_cmp);
123
0
  lock_rw_init(&zones->lock);
124
0
  lock_protect(&zones->lock, &zones->ztree, sizeof(zones->ztree));
125
  /* also lock protects the rbnode's in struct local_zone */
126
0
  return zones;
127
0
}
128
129
/** helper traverse to delete zones */
130
static void 
131
lzdel(rbnode_type* n, void* ATTR_UNUSED(arg))
132
0
{
133
0
  struct local_zone* z = (struct local_zone*)n->key;
134
0
  local_zone_delete(z);
135
0
}
136
137
void 
138
local_zones_delete(struct local_zones* zones)
139
0
{
140
0
  if(!zones)
141
0
    return;
142
0
  lock_rw_destroy(&zones->lock);
143
  /* walk through zones and delete them all */
144
0
  traverse_postorder(&zones->ztree, lzdel, NULL);
145
0
  free(zones);
146
0
}
147
148
void 
149
local_zone_delete(struct local_zone* z)
150
0
{
151
0
  if(!z)
152
0
    return;
153
0
  lock_rw_destroy(&z->lock);
154
0
  regional_destroy(z->region);
155
0
  free(z->name);
156
0
  free(z->taglist);
157
0
  free(z);
158
0
}
159
160
int 
161
local_zone_cmp(const void* z1, const void* z2)
162
0
{
163
  /* first sort on class, so that hierarchy can be maintained within
164
   * a class */
165
0
  struct local_zone* a = (struct local_zone*)z1;
166
0
  struct local_zone* b = (struct local_zone*)z2;
167
0
  int m;
168
0
  if(a->dclass != b->dclass) {
169
0
    if(a->dclass < b->dclass)
170
0
      return -1;
171
0
    return 1;
172
0
  }
173
0
  return dname_lab_cmp(a->name, a->namelabs, b->name, b->namelabs, &m);
174
0
}
175
176
int 
177
local_data_cmp(const void* d1, const void* d2)
178
0
{
179
0
  struct local_data* a = (struct local_data*)d1;
180
0
  struct local_data* b = (struct local_data*)d2;
181
0
  int m;
182
0
  return dname_canon_lab_cmp(a->name, a->namelabs, b->name, 
183
0
    b->namelabs, &m);
184
0
}
185
186
/* form wireformat from text format domain name */
187
int
188
parse_dname(const char* str, uint8_t** res, size_t* len, int* labs)
189
0
{
190
0
  *res = sldns_str2wire_dname(str, len);
191
0
  *labs = 0;
192
0
  if(!*res) {
193
0
    log_err("cannot parse name %s", str);
194
0
    return 0;
195
0
  }
196
0
  *labs = dname_count_size_labels(*res, len);
197
0
  return 1;
198
0
}
199
200
/** create a new localzone */
201
static struct local_zone*
202
local_zone_create(uint8_t* nm, size_t len, int labs, 
203
  enum localzone_type t, uint16_t dclass)
204
0
{
205
0
  struct local_zone* z = (struct local_zone*)calloc(1, sizeof(*z));
206
0
  if(!z) {
207
0
    return NULL;
208
0
  }
209
0
  z->node.key = z;
210
0
  z->dclass = dclass;
211
0
  z->type = t;
212
0
  z->name = nm;
213
0
  z->namelen = len;
214
0
  z->namelabs = labs;
215
0
  lock_rw_init(&z->lock);
216
0
  z->region = regional_create_nochunk(sizeof(struct regional));
217
0
  if(!z->region) {
218
0
    free(z);
219
0
    return NULL;
220
0
  }
221
0
  rbtree_init(&z->data, &local_data_cmp);
222
0
  lock_protect(&z->lock, &z->parent, sizeof(*z)-sizeof(rbnode_type));
223
  /* also the zones->lock protects node, parent, name*, class */
224
0
  return z;
225
0
}
226
227
/** enter a new zone with allocated dname returns with WRlock */
228
static struct local_zone*
229
lz_enter_zone_dname(struct local_zones* zones, uint8_t* nm, size_t len, 
230
  int labs, enum localzone_type t, uint16_t c)
231
0
{
232
0
  struct local_zone* z = local_zone_create(nm, len, labs, t, c);
233
0
  if(!z) {
234
0
    free(nm);
235
0
    log_err("out of memory");
236
0
    return NULL;
237
0
  }
238
239
  /* add to rbtree */
240
0
  lock_rw_wrlock(&zones->lock);
241
0
  lock_rw_wrlock(&z->lock);
242
0
  if(!rbtree_insert(&zones->ztree, &z->node)) {
243
0
    struct local_zone* oldz;
244
0
    char str[LDNS_MAX_DOMAINLEN];
245
0
    dname_str(nm, str);
246
0
    log_warn("duplicate local-zone %s", str);
247
0
    lock_rw_unlock(&z->lock);
248
    /* save zone name locally before deallocation,
249
     * otherwise, nm is gone if we zone_delete now. */
250
0
    oldz = z;
251
    /* find the correct zone, so not an error for duplicate */
252
0
    z = local_zones_find(zones, nm, len, labs, c);
253
0
    lock_rw_wrlock(&z->lock);
254
0
    lock_rw_unlock(&zones->lock);
255
0
    local_zone_delete(oldz);
256
0
    return z;
257
0
  }
258
0
  lock_rw_unlock(&zones->lock);
259
0
  return z;
260
0
}
261
262
/** enter a new zone */
263
struct local_zone*
264
lz_enter_zone(struct local_zones* zones, const char* name, const char* type,
265
  uint16_t dclass)
266
0
{
267
0
  struct local_zone* z;
268
0
  enum localzone_type t;
269
0
  uint8_t* nm;
270
0
  size_t len;
271
0
  int labs;
272
0
  if(!parse_dname(name, &nm, &len, &labs)) {
273
0
    log_err("bad zone name %s %s", name, type);
274
0
    return NULL;
275
0
  }
276
0
  if(!local_zone_str2type(type, &t)) {
277
0
    log_err("bad lz_enter_zone type %s %s", name, type);
278
0
    free(nm);
279
0
    return NULL;
280
0
  }
281
0
  if(!(z=lz_enter_zone_dname(zones, nm, len, labs, t, dclass))) {
282
0
    log_err("could not enter zone %s %s", name, type);
283
0
    return NULL;
284
0
  }
285
0
  return z;
286
0
}
287
288
int
289
rrstr_get_rr_content(const char* str, uint8_t** nm, uint16_t* type,
290
  uint16_t* dclass, time_t* ttl, uint8_t* rr, size_t len,
291
  uint8_t** rdata, size_t* rdata_len)
292
0
{
293
0
  size_t dname_len = 0;
294
0
  int e = sldns_str2wire_rr_buf(str, rr, &len, &dname_len, 3600,
295
0
    NULL, 0, NULL, 0);
296
0
  if(e) {
297
0
    log_err("error parsing local-data at %d: '%s': %s",
298
0
      LDNS_WIREPARSE_OFFSET(e), str,
299
0
      sldns_get_errorstr_parse(e));
300
0
    return 0;
301
0
  }
302
0
  *nm = memdup(rr, dname_len);
303
0
  if(!*nm) {
304
0
    log_err("out of memory");
305
0
    return 0;
306
0
  }
307
0
  *dclass = sldns_wirerr_get_class(rr, len, dname_len);
308
0
  *type = sldns_wirerr_get_type(rr, len, dname_len);
309
0
  *ttl = (time_t)sldns_wirerr_get_ttl(rr, len, dname_len);
310
0
  *rdata = sldns_wirerr_get_rdatawl(rr, len, dname_len);
311
0
  *rdata_len = sldns_wirerr_get_rdatalen(rr, len, dname_len)+2;
312
0
  return 1;
313
0
}
314
315
/** return name and class of rr; parses string */
316
static int
317
get_rr_nameclass(const char* str, uint8_t** nm, uint16_t* dclass,
318
  uint16_t* dtype)
319
0
{
320
0
  uint8_t rr[LDNS_RR_BUF_SIZE];
321
0
  size_t len = sizeof(rr), dname_len = 0;
322
0
  int s = sldns_str2wire_rr_buf(str, rr, &len, &dname_len, 3600,
323
0
    NULL, 0, NULL, 0);
324
0
  if(s != 0) {
325
0
    log_err("error parsing local-data at %d '%s': %s",
326
0
      LDNS_WIREPARSE_OFFSET(s), str,
327
0
      sldns_get_errorstr_parse(s));
328
0
    return 0;
329
0
  }
330
0
  *nm = memdup(rr, dname_len);
331
0
  *dclass = sldns_wirerr_get_class(rr, len, dname_len);
332
0
  *dtype = sldns_wirerr_get_type(rr, len, dname_len);
333
0
  if(!*nm) {
334
0
    log_err("out of memory");
335
0
    return 0;
336
0
  }
337
0
  return 1;
338
0
}
339
340
/**
341
 * Find an rrset in local data structure.
342
 * @param data: local data domain name structure.
343
 * @param type: type to look for (host order).
344
 * @param alias_ok: 1 if matching a non-exact, alias type such as CNAME is
345
 * allowed.  otherwise 0.
346
 * @return rrset pointer or NULL if not found.
347
 */
348
static struct local_rrset*
349
local_data_find_type(struct local_data* data, uint16_t type, int alias_ok)
350
0
{
351
0
  struct local_rrset* p, *cname = NULL;
352
0
  type = htons(type);
353
0
  for(p = data->rrsets; p; p = p->next) {
354
0
    if(p->rrset->rk.type == type)
355
0
      return p;
356
0
    if(alias_ok && p->rrset->rk.type == htons(LDNS_RR_TYPE_CNAME))
357
0
      cname = p;
358
0
  }
359
0
  if(alias_ok)
360
0
    return cname;
361
0
  return NULL;
362
0
}
363
364
/** check for RR duplicates */
365
static int
366
rr_is_duplicate(struct packed_rrset_data* pd, uint8_t* rdata, size_t rdata_len)
367
0
{
368
0
  size_t i;
369
0
  for(i=0; i<pd->count; i++) {
370
0
    if(pd->rr_len[i] == rdata_len &&
371
0
      memcmp(pd->rr_data[i], rdata, rdata_len) == 0)
372
0
      return 1;
373
0
  }
374
0
  return 0;
375
0
}
376
377
/** new local_rrset */
378
static struct local_rrset*
379
new_local_rrset(struct regional* region, struct local_data* node,
380
  uint16_t rrtype, uint16_t rrclass)
381
0
{
382
0
  struct packed_rrset_data* pd;
383
0
  struct local_rrset* rrset = (struct local_rrset*)
384
0
    regional_alloc_zero(region, sizeof(*rrset));
385
0
  if(!rrset) {
386
0
    log_err("out of memory");
387
0
    return NULL;
388
0
  }
389
0
  rrset->next = node->rrsets;
390
0
  node->rrsets = rrset;
391
0
  rrset->rrset = (struct ub_packed_rrset_key*)
392
0
    regional_alloc_zero(region, sizeof(*rrset->rrset));
393
0
  if(!rrset->rrset) {
394
0
    log_err("out of memory");
395
0
    return NULL;
396
0
  }
397
0
  rrset->rrset->entry.key = rrset->rrset;
398
0
  pd = (struct packed_rrset_data*)regional_alloc_zero(region,
399
0
    sizeof(*pd));
400
0
  if(!pd) {
401
0
    log_err("out of memory");
402
0
    return NULL;
403
0
  }
404
0
  pd->trust = rrset_trust_prim_noglue;
405
0
  pd->security = sec_status_insecure;
406
0
  rrset->rrset->entry.data = pd;
407
0
  rrset->rrset->rk.dname = node->name;
408
0
  rrset->rrset->rk.dname_len = node->namelen;
409
0
  rrset->rrset->rk.type = htons(rrtype);
410
0
  rrset->rrset->rk.rrset_class = htons(rrclass);
411
0
  return rrset;
412
0
}
413
414
/** insert RR into RRset data structure; Wastes a couple of bytes */
415
int
416
rrset_insert_rr(struct regional* region, struct packed_rrset_data* pd,
417
  uint8_t* rdata, size_t rdata_len, time_t ttl, const char* rrstr)
418
0
{
419
0
  size_t* oldlen = pd->rr_len;
420
0
  time_t* oldttl = pd->rr_ttl;
421
0
  uint8_t** olddata = pd->rr_data;
422
423
  /* add RR to rrset */
424
0
  if(pd->count > LOCALZONE_RRSET_COUNT_MAX) {
425
0
    log_warn("RRset '%s' has more than %d records, record ignored",
426
0
      rrstr, LOCALZONE_RRSET_COUNT_MAX);
427
0
    return 1;
428
0
  }
429
0
  pd->count++;
430
0
  pd->rr_len = regional_alloc(region, sizeof(*pd->rr_len)*pd->count);
431
0
  pd->rr_ttl = regional_alloc(region, sizeof(*pd->rr_ttl)*pd->count);
432
0
  pd->rr_data = regional_alloc(region, sizeof(*pd->rr_data)*pd->count);
433
0
  if(!pd->rr_len || !pd->rr_ttl || !pd->rr_data) {
434
0
    log_err("out of memory");
435
0
    return 0;
436
0
  }
437
0
  if(pd->count > 1) {
438
0
    memcpy(pd->rr_len+1, oldlen, 
439
0
      sizeof(*pd->rr_len)*(pd->count-1));
440
0
    memcpy(pd->rr_ttl+1, oldttl, 
441
0
      sizeof(*pd->rr_ttl)*(pd->count-1));
442
0
    memcpy(pd->rr_data+1, olddata, 
443
0
      sizeof(*pd->rr_data)*(pd->count-1));
444
0
  }
445
0
  pd->rr_len[0] = rdata_len;
446
0
  pd->rr_ttl[0] = ttl;
447
0
  pd->rr_data[0] = regional_alloc_init(region, rdata, rdata_len);
448
0
  if(!pd->rr_data[0]) {
449
0
    log_err("out of memory");
450
0
    return 0;
451
0
  }
452
0
  return 1;
453
0
}
454
455
/** Delete RR from local-zone RRset, wastes memory as the deleted RRs cannot be
456
 * free'd (regionally alloc'd) */
457
int
458
local_rrset_remove_rr(struct packed_rrset_data* pd, size_t index)
459
0
{
460
0
  log_assert(pd->count > 0);
461
0
  if(index >= pd->count) {
462
0
    log_warn("Trying to remove RR with out of bound index");
463
0
    return 0;
464
0
  }
465
0
  if(index + 1 < pd->count) {
466
    /* not removing last element */
467
0
    size_t nexti = index + 1;
468
0
    size_t num = pd->count - nexti;
469
0
    memmove(pd->rr_len+index, pd->rr_len+nexti, sizeof(*pd->rr_len)*num);
470
0
    memmove(pd->rr_ttl+index, pd->rr_ttl+nexti, sizeof(*pd->rr_ttl)*num);
471
0
    memmove(pd->rr_data+index, pd->rr_data+nexti, sizeof(*pd->rr_data)*num);
472
0
  }
473
0
  pd->count--;
474
0
  return 1;
475
0
}
476
477
struct local_data* 
478
local_zone_find_data(struct local_zone* z, uint8_t* nm, size_t nmlen, int nmlabs)
479
0
{
480
0
  struct local_data key;
481
0
  key.node.key = &key;
482
0
  key.name = nm;
483
0
  key.namelen = nmlen;
484
0
  key.namelabs = nmlabs;
485
0
  return (struct local_data*)rbtree_search(&z->data, &key.node);
486
0
}
487
488
/** find a node, create it if not and all its empty nonterminal parents */
489
static int
490
lz_find_create_node(struct local_zone* z, uint8_t* nm, size_t nmlen, 
491
  int nmlabs, struct local_data** res)
492
0
{
493
0
  struct local_data* ld = local_zone_find_data(z, nm, nmlen, nmlabs);
494
0
  if(!ld) {
495
    /* create a domain name to store rr. */
496
0
    ld = (struct local_data*)regional_alloc_zero(z->region,
497
0
      sizeof(*ld));
498
0
    if(!ld) {
499
0
      log_err("out of memory adding local data");
500
0
      return 0;
501
0
    }
502
0
    ld->node.key = ld;
503
0
    ld->name = regional_alloc_init(z->region, nm, nmlen);
504
0
    if(!ld->name) {
505
0
      log_err("out of memory");
506
0
      return 0;
507
0
    }
508
0
    ld->namelen = nmlen;
509
0
    ld->namelabs = nmlabs;
510
0
    if(!rbtree_insert(&z->data, &ld->node)) {
511
0
      log_assert(0); /* duplicate name */
512
0
    }
513
    /* see if empty nonterminals need to be created */
514
0
    if(nmlabs > z->namelabs) {
515
0
      dname_remove_label(&nm, &nmlen);
516
0
      if(!lz_find_create_node(z, nm, nmlen, nmlabs-1, res))
517
0
        return 0;
518
0
    }
519
0
  }
520
0
  *res = ld;
521
0
  return 1;
522
0
}
523
524
/* Mark the SOA record for the zone. This only marks the SOA rrset; the data
525
 * for the RR is entered later on local_zone_enter_rr() as with the other
526
 * records. An artificial soa_negative record with a modified TTL (minimum of
527
 * the TTL and the SOA.MINIMUM) is also created and marked for usage with
528
 * negative answers and to avoid allocations during those answers. */
529
static int
530
lz_mark_soa_for_zone(struct local_zone* z, struct ub_packed_rrset_key* soa_rrset,
531
  uint8_t* rdata, size_t rdata_len, time_t ttl, const char* rrstr)
532
0
{
533
0
  struct packed_rrset_data* pd = (struct packed_rrset_data*)
534
0
    regional_alloc_zero(z->region, sizeof(*pd));
535
0
  struct ub_packed_rrset_key* rrset_negative = (struct ub_packed_rrset_key*)
536
0
    regional_alloc_zero(z->region, sizeof(*rrset_negative));
537
0
  time_t minimum;
538
0
  if(!rrset_negative||!pd) {
539
0
    log_err("out of memory");
540
0
    return 0;
541
0
  }
542
  /* Mark the original SOA record and then continue with the negative one. */
543
0
  z->soa = soa_rrset;
544
0
  rrset_negative->entry.key = rrset_negative;
545
0
  pd->trust = rrset_trust_prim_noglue;
546
0
  pd->security = sec_status_insecure;
547
0
  rrset_negative->entry.data = pd;
548
0
  rrset_negative->rk.dname = soa_rrset->rk.dname;
549
0
  rrset_negative->rk.dname_len = soa_rrset->rk.dname_len;
550
0
  rrset_negative->rk.type = soa_rrset->rk.type;
551
0
  rrset_negative->rk.rrset_class = soa_rrset->rk.rrset_class;
552
0
  if(!rrset_insert_rr(z->region, pd, rdata, rdata_len, ttl, rrstr))
553
0
    return 0;
554
  /* last 4 bytes are minimum ttl in network format */
555
0
  if(pd->count == 0 || pd->rr_len[0] < 2+4)
556
0
    return 0;
557
0
  minimum = (time_t)sldns_read_uint32(pd->rr_data[0]+(pd->rr_len[0]-4));
558
0
  minimum = ttl<minimum?ttl:minimum;
559
0
  pd->ttl = minimum;
560
0
  pd->rr_ttl[0] = minimum;
561
562
0
  z->soa_negative = rrset_negative;
563
0
  return 1;
564
0
}
565
566
int
567
local_zone_enter_rr(struct local_zone* z, uint8_t* nm, size_t nmlen,
568
  int nmlabs, uint16_t rrtype, uint16_t rrclass, time_t ttl,
569
  uint8_t* rdata, size_t rdata_len, const char* rrstr)
570
0
{
571
0
  struct local_data* node;
572
0
  struct local_rrset* rrset;
573
0
  struct packed_rrset_data* pd;
574
575
0
  if(!lz_find_create_node(z, nm, nmlen, nmlabs, &node)) {
576
0
    return 0;
577
0
  }
578
0
  log_assert(node);
579
580
  /* Reject it if we would end up having CNAME and other data (including
581
   * another CNAME) for a redirect zone. */
582
0
  if((z->type == local_zone_redirect ||
583
0
    z->type == local_zone_inform_redirect) && node->rrsets) {
584
0
    const char* othertype = NULL;
585
0
    if (rrtype == LDNS_RR_TYPE_CNAME)
586
0
      othertype = "other";
587
0
    else if (node->rrsets->rrset->rk.type ==
588
0
       htons(LDNS_RR_TYPE_CNAME)) {
589
0
      othertype = "CNAME";
590
0
    }
591
0
    if(othertype) {
592
0
      log_err("local-data '%s' in redirect zone must not "
593
0
        "coexist with %s local-data", rrstr, othertype);
594
0
      return 0;
595
0
    }
596
0
  }
597
0
  rrset = local_data_find_type(node, rrtype, 0);
598
0
  if(!rrset) {
599
0
    rrset = new_local_rrset(z->region, node, rrtype, rrclass);
600
0
    if(!rrset)
601
0
      return 0;
602
0
    if(query_dname_compare(node->name, z->name) == 0) {
603
0
      if(rrtype == LDNS_RR_TYPE_NSEC)
604
0
        rrset->rrset->rk.flags = PACKED_RRSET_NSEC_AT_APEX;
605
0
      if(rrtype == LDNS_RR_TYPE_SOA &&
606
0
        !lz_mark_soa_for_zone(z, rrset->rrset, rdata, rdata_len, ttl,
607
0
          rrstr))
608
0
        return 0;
609
0
    }
610
0
  } 
611
0
  pd = (struct packed_rrset_data*)rrset->rrset->entry.data;
612
0
  log_assert(rrset && pd);
613
614
  /* check for duplicate RR */
615
0
  if(rr_is_duplicate(pd, rdata, rdata_len)) {
616
0
    verbose(VERB_ALGO, "ignoring duplicate RR: %s", rrstr);
617
0
    return 1;
618
0
  } 
619
0
  return rrset_insert_rr(z->region, pd, rdata, rdata_len, ttl, rrstr);
620
0
}
621
622
/** enter data RR into auth zone */
623
static int
624
lz_enter_rr_into_zone(struct local_zone* z, const char* rrstr)
625
0
{
626
0
  uint8_t* nm;
627
0
  size_t nmlen;
628
0
  int nmlabs, ret;
629
0
  uint16_t rrtype = 0, rrclass = 0;
630
0
  time_t ttl = 0;
631
0
  uint8_t rr[LDNS_RR_BUF_SIZE];
632
0
  uint8_t* rdata;
633
0
  size_t rdata_len;
634
0
  if(!rrstr_get_rr_content(rrstr, &nm, &rrtype, &rrclass, &ttl, rr,
635
0
    sizeof(rr), &rdata, &rdata_len)) {
636
0
    log_err("bad local-data: %s", rrstr);
637
0
    return 0;
638
0
  }
639
0
  log_assert(z->dclass == rrclass);
640
0
  if((z->type == local_zone_redirect ||
641
0
    z->type == local_zone_inform_redirect) &&
642
0
    query_dname_compare(z->name, nm) != 0) {
643
0
    log_err("local-data in redirect zone must reside at top of zone"
644
0
      ", not at %s", rrstr);
645
0
    free(nm);
646
0
    return 0;
647
0
  }
648
0
  nmlabs = dname_count_size_labels(nm, &nmlen);
649
0
  ret = local_zone_enter_rr(z, nm, nmlen, nmlabs, rrtype, rrclass, ttl,
650
0
    rdata, rdata_len, rrstr);
651
0
  free(nm);
652
0
  return ret;
653
0
}
654
655
/** enter a data RR into auth data; a zone for it must exist */
656
static int
657
lz_enter_rr_str(struct local_zones* zones, const char* rr)
658
0
{
659
0
  uint8_t* rr_name;
660
0
  uint16_t rr_class, rr_type;
661
0
  size_t len;
662
0
  int labs;
663
0
  struct local_zone* z;
664
0
  int r;
665
0
  if(!get_rr_nameclass(rr, &rr_name, &rr_class, &rr_type)) {
666
0
    log_err("bad rr %s", rr);
667
0
    return 0;
668
0
  }
669
0
  labs = dname_count_size_labels(rr_name, &len);
670
0
  lock_rw_rdlock(&zones->lock);
671
0
  z = local_zones_lookup(zones, rr_name, len, labs, rr_class, rr_type, 1);
672
0
  if(!z) {
673
0
    lock_rw_unlock(&zones->lock);
674
0
    fatal_exit("internal error: no zone for rr %s", rr);
675
0
  }
676
0
  lock_rw_wrlock(&z->lock);
677
0
  lock_rw_unlock(&zones->lock);
678
0
  free(rr_name);
679
0
  r = lz_enter_rr_into_zone(z, rr);
680
0
  lock_rw_unlock(&z->lock);
681
0
  return r;
682
0
}
683
684
/** enter tagstring into zone */
685
static int
686
lz_enter_zone_tag(struct local_zones* zones, char* zname, uint8_t* list,
687
  size_t len, uint16_t rr_class)
688
0
{
689
0
  uint8_t dname[LDNS_MAX_DOMAINLEN+1];
690
0
  size_t dname_len = sizeof(dname);
691
0
  int dname_labs, r = 0;
692
0
  struct local_zone* z;
693
694
0
  if(sldns_str2wire_dname_buf(zname, dname, &dname_len) != 0) {
695
0
    log_err("cannot parse zone name in local-zone-tag: %s", zname);
696
0
    return 0;
697
0
  }
698
0
  dname_labs = dname_count_labels(dname);
699
  
700
0
  lock_rw_rdlock(&zones->lock);
701
0
  z = local_zones_find(zones, dname, dname_len, dname_labs, rr_class);
702
0
  if(!z) {
703
0
    lock_rw_unlock(&zones->lock);
704
0
    log_err("no local-zone for tag %s", zname);
705
0
    return 0;
706
0
  }
707
0
  lock_rw_wrlock(&z->lock);
708
0
  lock_rw_unlock(&zones->lock);
709
0
  free(z->taglist);
710
0
  z->taglist = memdup(list, len);
711
0
  z->taglen = len;
712
0
  if(z->taglist)
713
0
    r = 1;
714
0
  lock_rw_unlock(&z->lock);
715
0
  return r;
716
0
}
717
718
/** enter override into zone */
719
static int
720
lz_enter_override(struct local_zones* zones, char* zname, char* netblock,
721
  char* type, uint16_t rr_class)
722
0
{
723
0
  uint8_t dname[LDNS_MAX_DOMAINLEN+1];
724
0
  size_t dname_len = sizeof(dname);
725
0
  int dname_labs;
726
0
  struct sockaddr_storage addr;
727
0
  int net;
728
0
  socklen_t addrlen;
729
0
  struct local_zone* z;
730
0
  enum localzone_type t;
731
732
  /* parse zone name */
733
0
  if(sldns_str2wire_dname_buf(zname, dname, &dname_len) != 0) {
734
0
    log_err("cannot parse zone name in local-zone-override: %s %s",
735
0
      zname, netblock);
736
0
    return 0;
737
0
  }
738
0
  dname_labs = dname_count_labels(dname);
739
740
  /* parse netblock */
741
0
  if(!netblockstrtoaddr(netblock, UNBOUND_DNS_PORT, &addr, &addrlen,
742
0
    &net)) {
743
0
    log_err("cannot parse netblock in local-zone-override: %s %s",
744
0
      zname, netblock);
745
0
    return 0;
746
0
  }
747
748
  /* parse zone type */
749
0
  if(!local_zone_str2type(type, &t)) {
750
0
    log_err("cannot parse type in local-zone-override: %s %s %s",
751
0
      zname, netblock, type);
752
0
    return 0;
753
0
  }
754
755
  /* find localzone entry */
756
0
  lock_rw_rdlock(&zones->lock);
757
0
  z = local_zones_find(zones, dname, dname_len, dname_labs, rr_class);
758
0
  if(!z) {
759
0
    lock_rw_unlock(&zones->lock);
760
0
    log_err("no local-zone for local-zone-override %s", zname);
761
0
    return 0;
762
0
  }
763
0
  lock_rw_wrlock(&z->lock);
764
0
  lock_rw_unlock(&zones->lock);
765
766
  /* create netblock addr_tree if not present yet */
767
0
  if(!z->override_tree) {
768
0
    z->override_tree = (struct rbtree_type*)regional_alloc_zero(
769
0
      z->region, sizeof(*z->override_tree));
770
0
    if(!z->override_tree) {
771
0
      lock_rw_unlock(&z->lock);
772
0
      log_err("out of memory");
773
0
      return 0;
774
0
    }
775
0
    addr_tree_init(z->override_tree);
776
0
  }
777
  /* add new elem to tree */
778
0
  if(z->override_tree) {
779
0
    struct local_zone_override* n;
780
0
    n = (struct local_zone_override*)regional_alloc_zero(
781
0
      z->region, sizeof(*n));
782
0
    if(!n) {
783
0
      lock_rw_unlock(&z->lock);
784
0
      log_err("out of memory");
785
0
      return 0;
786
0
    }
787
0
    n->type = t;
788
0
    if(!addr_tree_insert(z->override_tree,
789
0
      (struct addr_tree_node*)n, &addr, addrlen, net)) {
790
0
      lock_rw_unlock(&z->lock);
791
0
      log_err("duplicate local-zone-override %s %s",
792
0
        zname, netblock);
793
0
      return 1;
794
0
    }
795
0
  }
796
797
0
  lock_rw_unlock(&z->lock);
798
0
  return 1;
799
0
}
800
801
/** parse local-zone: statements */
802
static int
803
lz_enter_zones(struct local_zones* zones, struct config_file* cfg)
804
0
{
805
0
  struct config_str2list* p;
806
0
#ifndef THREADS_DISABLED
807
0
  struct local_zone* z;
808
0
#endif
809
0
  for(p = cfg->local_zones; p; p = p->next) {
810
0
    if(!(
811
0
#ifndef THREADS_DISABLED
812
0
      z=
813
0
#endif
814
0
      lz_enter_zone(zones, p->str, p->str2,
815
0
      LDNS_RR_CLASS_IN)))
816
0
      return 0;
817
0
    lock_rw_unlock(&z->lock);
818
0
  }
819
0
  return 1;
820
0
}
821
822
/** lookup a zone in rbtree; exact match only; SLOW due to parse */
823
static int
824
lz_exists(struct local_zones* zones, const char* name)
825
0
{
826
0
  struct local_zone z;
827
0
  z.node.key = &z;
828
0
  z.dclass = LDNS_RR_CLASS_IN;
829
0
  if(!parse_dname(name, &z.name, &z.namelen, &z.namelabs)) {
830
0
    log_err("bad name %s", name);
831
0
    return 0;
832
0
  }
833
0
  lock_rw_rdlock(&zones->lock);
834
0
  if(rbtree_search(&zones->ztree, &z.node)) {
835
0
    lock_rw_unlock(&zones->lock);
836
0
    free(z.name);
837
0
    return 1;
838
0
  }
839
0
  lock_rw_unlock(&zones->lock);
840
0
  free(z.name);
841
0
  return 0;
842
0
}
843
844
/** lookup a zone in cfg->nodefault list */
845
static int
846
lz_nodefault(struct config_file* cfg, const char* name)
847
0
{
848
0
  struct config_strlist* p;
849
0
  size_t len = strlen(name);
850
0
  if(len == 0) return 0;
851
0
  if(name[len-1] == '.') len--;
852
853
0
  for(p = cfg->local_zones_nodefault; p; p = p->next) {
854
    /* compare zone name, lowercase, compare without ending . */
855
0
    if(strncasecmp(p->str, name, len) == 0 &&
856
0
      (strlen(p->str) == len || (strlen(p->str)==len+1 &&
857
0
      p->str[len] == '.')))
858
0
      return 1;
859
0
  }
860
0
  return 0;
861
0
}
862
863
/** enter reverse default zone */
864
static int
865
add_reverse_default(struct local_zones* zones, struct config_file* cfg,
866
        const char* name)
867
0
{
868
0
  struct local_zone* z;
869
0
  char str[1024]; /* known long enough */
870
0
  if(lz_exists(zones, name) || lz_nodefault(cfg, name))
871
0
    return 1; /* do not enter default content */
872
0
  if(!(z=lz_enter_zone(zones, name, "static", LDNS_RR_CLASS_IN)))
873
0
    return 0;
874
0
  snprintf(str, sizeof(str), "%s 10800 IN SOA localhost. "
875
0
    "nobody.invalid. 1 3600 1200 604800 10800", name);
876
0
  if(!lz_enter_rr_into_zone(z, str)) {
877
0
    lock_rw_unlock(&z->lock);
878
0
    return 0;
879
0
  }
880
0
  snprintf(str, sizeof(str), "%s 10800 IN NS localhost. ", name);
881
0
  if(!lz_enter_rr_into_zone(z, str)) {
882
0
    lock_rw_unlock(&z->lock);
883
0
    return 0;
884
0
  }
885
0
  if(strncasecmp("127.in-addr.arpa.", name, 17) ==  0) {
886
0
    if(!lz_enter_rr_into_zone(z,
887
0
      "1.0.0.127.in-addr.arpa. 10800 IN PTR localhost.")) {
888
0
      lock_rw_unlock(&z->lock);
889
0
      return 0;
890
0
    }
891
0
  } else if(strncasecmp("1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.", name, 73) ==  0) {
892
0
    snprintf(str, sizeof(str), "%s 10800 IN PTR localhost.", name);
893
0
    if(!lz_enter_rr_into_zone(z, str)) {
894
0
      lock_rw_unlock(&z->lock);
895
0
      return 0;
896
0
    }
897
0
  }
898
0
  lock_rw_unlock(&z->lock);
899
0
  return 1;
900
0
}
901
902
/** enter (AS112) empty default zone */
903
static int
904
add_empty_default(struct local_zones* zones, struct config_file* cfg,
905
        const char* name)
906
0
{
907
0
  struct local_zone* z;
908
0
  char str[1024]; /* known long enough */
909
0
  if(lz_exists(zones, name) || lz_nodefault(cfg, name))
910
0
    return 1; /* do not enter default content */
911
0
  if(!(z=lz_enter_zone(zones, name, "static", LDNS_RR_CLASS_IN)))
912
0
    return 0;
913
0
  snprintf(str, sizeof(str), "%s 10800 IN SOA localhost. "
914
0
    "nobody.invalid. 1 3600 1200 604800 10800", name);
915
0
  if(!lz_enter_rr_into_zone(z, str)) {
916
0
    lock_rw_unlock(&z->lock);
917
0
    return 0;
918
0
  }
919
0
  snprintf(str, sizeof(str), "%s 10800 IN NS localhost. ", name);
920
0
  if(!lz_enter_rr_into_zone(z, str)) {
921
0
    lock_rw_unlock(&z->lock);
922
0
    return 0;
923
0
  }
924
0
  lock_rw_unlock(&z->lock);
925
0
  return 1;
926
0
}
927
928
/** enter default zones */
929
int local_zone_enter_defaults(struct local_zones* zones, struct config_file* cfg)
930
0
{
931
0
  struct local_zone* z;
932
0
  const char** zstr;
933
934
  /* Do not add any default */
935
0
  if(cfg->local_zones_disable_default)
936
0
    return 1;
937
938
  /* this list of zones is from RFC 6303 and RFC 7686 */
939
940
  /* block localhost level zones first, then onion and later the LAN zones */
941
942
  /* localhost. zone */
943
0
  if(!lz_exists(zones, "localhost.") &&
944
0
    !lz_nodefault(cfg, "localhost.")) {
945
0
    if(!(z=lz_enter_zone(zones, "localhost.", "redirect", 
946
0
      LDNS_RR_CLASS_IN)) ||
947
0
       !lz_enter_rr_into_zone(z,
948
0
      "localhost. 10800 IN NS localhost.") ||
949
0
       !lz_enter_rr_into_zone(z,
950
0
      "localhost. 10800 IN SOA localhost. nobody.invalid. "
951
0
      "1 3600 1200 604800 10800") ||
952
0
       !lz_enter_rr_into_zone(z,
953
0
      "localhost. 10800 IN A 127.0.0.1") ||
954
0
       !lz_enter_rr_into_zone(z,
955
0
      "localhost. 10800 IN AAAA ::1")) {
956
0
      log_err("out of memory adding default zone");
957
0
      if(z) { lock_rw_unlock(&z->lock); }
958
0
      return 0;
959
0
    }
960
0
    lock_rw_unlock(&z->lock);
961
0
  }
962
963
  /* ip4 and ip6 reverse */
964
0
  for(zstr = local_zones_default_reverse; *zstr; zstr++) {
965
0
    if(!add_reverse_default(zones, cfg, *zstr)) {
966
0
      log_err("out of memory adding default zone");
967
0
      return 0;
968
0
    }
969
0
  }
970
971
  /* special-use zones */
972
0
  for(zstr = local_zones_default_special; *zstr; zstr++) {
973
0
    if(!add_empty_default(zones, cfg, *zstr)) {
974
0
      log_err("out of memory adding default zone");
975
0
      return 0;
976
0
    }
977
0
  }
978
979
  /* block AS112 zones, unless asked not to */
980
0
  if(!cfg->unblock_lan_zones) {
981
0
    for(zstr = as112_zones; *zstr; zstr++) {
982
0
      if(!add_empty_default(zones, cfg, *zstr)) {
983
0
        log_err("out of memory adding default zone");
984
0
        return 0;
985
0
      }
986
0
    }
987
0
  }
988
0
  return 1;
989
0
}
990
991
/** parse local-zone-override: statements */
992
static int
993
lz_enter_overrides(struct local_zones* zones, struct config_file* cfg)
994
0
{
995
0
  struct config_str3list* p;
996
0
  for(p = cfg->local_zone_overrides; p; p = p->next) {
997
0
    if(!lz_enter_override(zones, p->str, p->str2, p->str3,
998
0
      LDNS_RR_CLASS_IN))
999
0
      return 0;
1000
0
  }
1001
0
  return 1;
1002
0
}
1003
1004
/* return closest parent in the tree, NULL if none */
1005
static struct local_zone* find_closest_parent(struct local_zone* curr,
1006
  struct local_zone* prev)
1007
0
{
1008
0
  struct local_zone* p;
1009
0
  int m;
1010
0
  if(!prev || prev->dclass != curr->dclass) return NULL;
1011
0
  (void)dname_lab_cmp(prev->name, prev->namelabs, curr->name,
1012
0
    curr->namelabs, &m); /* we know prev is smaller */
1013
  /* sort order like: . com. bla.com. zwb.com. net. */
1014
  /* find the previous, or parent-parent-parent */
1015
0
  for(p = prev; p; p = p->parent) {
1016
    /* looking for name with few labels, a parent */
1017
0
    if(p->namelabs <= m) {
1018
      /* ==: since prev matched m, this is closest*/
1019
      /* <: prev matches more, but is not a parent,
1020
          * this one is a (grand)parent */
1021
0
      return p;
1022
0
    }
1023
0
  }
1024
0
  return NULL;
1025
0
}
1026
1027
/** setup parent pointers, so that a lookup can be done for closest match */
1028
void
1029
lz_init_parents(struct local_zones* zones)
1030
0
{
1031
0
  struct local_zone* node, *prev = NULL;
1032
0
  lock_rw_wrlock(&zones->lock);
1033
0
  RBTREE_FOR(node, struct local_zone*, &zones->ztree) {
1034
0
    lock_rw_wrlock(&node->lock);
1035
0
    node->parent = find_closest_parent(node, prev);
1036
0
    prev = node;
1037
0
    if(node->override_tree)
1038
0
      addr_tree_init_parents(node->override_tree);
1039
0
    lock_rw_unlock(&node->lock);
1040
0
  }
1041
0
  lock_rw_unlock(&zones->lock);
1042
0
}
1043
1044
/** enter implicit transparent zone for local-data: without local-zone: */
1045
static int
1046
lz_setup_implicit(struct local_zones* zones, struct config_file* cfg)
1047
0
{
1048
  /* walk over all items that have no parent zone and find
1049
   * the name that covers them all (could be the root) and
1050
   * add that as a transparent zone */
1051
0
  struct config_strlist* p;
1052
0
  int have_name = 0;
1053
0
  int have_other_classes = 0;
1054
0
  uint16_t dclass = 0;
1055
0
  uint8_t* nm = 0;
1056
0
  size_t nmlen = 0;
1057
0
  int nmlabs = 0;
1058
0
  int match = 0; /* number of labels match count */
1059
1060
0
  lz_init_parents(zones); /* to enable local_zones_lookup() */
1061
0
  for(p = cfg->local_data; p; p = p->next) {
1062
0
    uint8_t* rr_name;
1063
0
    uint16_t rr_class, rr_type;
1064
0
    size_t len;
1065
0
    int labs;
1066
0
    if(!get_rr_nameclass(p->str, &rr_name, &rr_class, &rr_type)) {
1067
0
      log_err("Bad local-data RR %s", p->str);
1068
0
      return 0;
1069
0
    }
1070
0
    labs = dname_count_size_labels(rr_name, &len);
1071
0
    lock_rw_rdlock(&zones->lock);
1072
0
    if(!local_zones_lookup(zones, rr_name, len, labs, rr_class,
1073
0
      rr_type, 1)) {
1074
      /* Check if there is a zone that this could go
1075
       * under but for different class; created zones are
1076
       * always for LDNS_RR_CLASS_IN. Create the zone with
1077
       * a different class but the same configured
1078
       * local_zone_type. */
1079
0
      struct local_zone* z = local_zones_lookup(zones,
1080
0
        rr_name, len, labs, LDNS_RR_CLASS_IN, rr_type,
1081
0
        1);
1082
0
      if(z) {
1083
0
        uint8_t* name = memdup(z->name, z->namelen);
1084
0
        size_t znamelen = z->namelen;
1085
0
        int znamelabs = z->namelabs;
1086
0
        enum localzone_type ztype = z->type;
1087
0
        lock_rw_unlock(&zones->lock);
1088
0
        if(!name) {
1089
0
          log_err("out of memory");
1090
0
          free(rr_name);
1091
0
          return 0;
1092
0
        }
1093
0
        if(!(
1094
0
#ifndef THREADS_DISABLED
1095
0
          z =
1096
0
#endif
1097
0
          lz_enter_zone_dname(zones, name,
1098
0
            znamelen, znamelabs,
1099
0
            ztype, rr_class))) {
1100
0
          free(rr_name);
1101
0
          return 0;
1102
0
        }
1103
0
        lock_rw_unlock(&z->lock);
1104
0
        free(rr_name);
1105
0
        continue;
1106
0
      }
1107
0
      if(!have_name) {
1108
0
        dclass = rr_class;
1109
0
        nm = rr_name;
1110
0
        nmlen = len;
1111
0
        nmlabs = labs;
1112
0
        match = labs;
1113
0
        have_name = 1;
1114
0
      } else {
1115
0
        int m;
1116
0
        if(rr_class != dclass) {
1117
          /* process other classes later */
1118
0
          free(rr_name);
1119
0
          have_other_classes = 1;
1120
0
          lock_rw_unlock(&zones->lock);
1121
0
          continue;
1122
0
        }
1123
        /* find smallest shared topdomain */
1124
0
        (void)dname_lab_cmp(nm, nmlabs, 
1125
0
          rr_name, labs, &m);
1126
0
        free(rr_name);
1127
0
        if(m < match)
1128
0
          match = m;
1129
0
      }
1130
0
    } else free(rr_name);
1131
0
    lock_rw_unlock(&zones->lock);
1132
0
  }
1133
0
  if(have_name) {
1134
0
    uint8_t* n2;
1135
0
#ifndef THREADS_DISABLED
1136
0
    struct local_zone* z;
1137
0
#endif
1138
    /* allocate zone of smallest shared topdomain to contain em */
1139
0
    n2 = nm;
1140
0
    dname_remove_labels(&n2, &nmlen, nmlabs - match);
1141
0
    n2 = memdup(n2, nmlen);
1142
0
    free(nm);
1143
0
    if(!n2) {
1144
0
      log_err("out of memory");
1145
0
      return 0;
1146
0
    }
1147
0
    log_nametypeclass(VERB_ALGO, "implicit transparent local-zone", 
1148
0
      n2, 0, dclass);
1149
0
    if(!(
1150
0
#ifndef THREADS_DISABLED
1151
0
      z=
1152
0
#endif
1153
0
      lz_enter_zone_dname(zones, n2, nmlen, match,
1154
0
      local_zone_transparent, dclass))) {
1155
0
      return 0;
1156
0
    }
1157
0
    lock_rw_unlock(&z->lock);
1158
0
  }
1159
0
  if(have_other_classes) { 
1160
    /* restart to setup other class */
1161
0
    return lz_setup_implicit(zones, cfg);
1162
0
  }
1163
0
  return 1;
1164
0
}
1165
1166
/** enter local-zone-tag info */
1167
static int
1168
lz_enter_zone_tags(struct local_zones* zones, struct config_file* cfg)
1169
0
{
1170
0
  struct config_strbytelist* p;
1171
0
  int c = 0;
1172
0
  for(p = cfg->local_zone_tags; p; p = p->next) {
1173
0
    if(!lz_enter_zone_tag(zones, p->str, p->str2, p->str2len,
1174
0
      LDNS_RR_CLASS_IN))
1175
0
      return 0;
1176
0
    c++;
1177
0
  }
1178
0
  if(c) verbose(VERB_ALGO, "applied tags to %d local zones", c);
1179
0
  return 1;
1180
0
}
1181
  
1182
/** enter auth data */
1183
static int
1184
lz_enter_data(struct local_zones* zones, struct config_file* cfg)
1185
0
{
1186
0
  struct config_strlist* p;
1187
0
  for(p = cfg->local_data; p; p = p->next) {
1188
0
    if(!lz_enter_rr_str(zones, p->str))
1189
0
      return 0;
1190
0
  }
1191
0
  return 1;
1192
0
}
1193
1194
/** free memory from config */
1195
static void
1196
lz_freeup_cfg(struct config_file* cfg)
1197
0
{
1198
0
  config_deldblstrlist(cfg->local_zones);
1199
0
  cfg->local_zones = NULL;
1200
0
  config_delstrlist(cfg->local_zones_nodefault);
1201
0
  cfg->local_zones_nodefault = NULL;
1202
0
  config_delstrlist(cfg->local_data);
1203
0
  cfg->local_data = NULL;
1204
0
}
1205
1206
int 
1207
local_zones_apply_cfg(struct local_zones* zones, struct config_file* cfg)
1208
0
{
1209
  /* create zones from zone statements. */
1210
0
  if(!lz_enter_zones(zones, cfg)) {
1211
0
    return 0;
1212
0
  }
1213
  /* apply default zones+content (unless disabled, or overridden) */
1214
0
  if(!local_zone_enter_defaults(zones, cfg)) {
1215
0
    return 0;
1216
0
  }
1217
  /* enter local zone overrides */
1218
0
  if(!lz_enter_overrides(zones, cfg)) {
1219
0
    return 0;
1220
0
  }
1221
  /* create implicit transparent zone from data. */
1222
0
  if(!lz_setup_implicit(zones, cfg)) {
1223
0
    return 0;
1224
0
  }
1225
1226
  /* setup parent ptrs for lookup during data entry */
1227
0
  lz_init_parents(zones);
1228
  /* insert local zone tags */
1229
0
  if(!lz_enter_zone_tags(zones, cfg)) {
1230
0
    return 0;
1231
0
  }
1232
  /* insert local data */
1233
0
  if(!lz_enter_data(zones, cfg)) {
1234
0
    return 0;
1235
0
  }
1236
  /* freeup memory from cfg struct. */
1237
0
  lz_freeup_cfg(cfg);
1238
0
  return 1;
1239
0
}
1240
1241
struct local_zone* 
1242
local_zones_lookup(struct local_zones* zones,
1243
        uint8_t* name, size_t len, int labs, uint16_t dclass, uint16_t dtype,
1244
  int foradd)
1245
0
{
1246
0
  return local_zones_tags_lookup(zones, name, len, labs,
1247
0
    dclass, dtype, NULL, 0, 1, foradd);
1248
0
}
1249
1250
struct local_zone* 
1251
local_zones_tags_lookup(struct local_zones* zones,
1252
        uint8_t* name, size_t len, int labs, uint16_t dclass, uint16_t dtype,
1253
  uint8_t* taglist, size_t taglen, int ignoretags, int foradd)
1254
0
{
1255
0
  rbnode_type* res = NULL;
1256
0
  struct local_zone *result;
1257
0
  struct local_zone key;
1258
0
  int m;
1259
0
  key.node.key = &key;
1260
0
  key.dclass = dclass;
1261
  /* for type DS use a zone higher when on a zonecut */
1262
0
  if(dtype == LDNS_RR_TYPE_DS && !dname_is_root(name)) {
1263
    /* If this is at a zone cut, of a local-zone, and it is
1264
     * of type always_refuse. Then also refuse the type DS
1265
     * for it. That could make it DNSSEC bogus, but it is
1266
     * REFUSED anyway. It stops CNAME type answers in the
1267
     * type DS lookup. */
1268
0
    key.name = name;
1269
0
    key.namelen = len;
1270
0
    key.namelabs = labs;
1271
    /* For additions and removals, use the ordinary rule,
1272
     * to remove a label for type DS to locate the parent zone.
1273
     * That is where the DS RR needs to be put. */
1274
0
    if(!foradd &&
1275
0
      (result=(struct local_zone*)rbtree_search(
1276
0
      &zones->ztree, &key)) != NULL &&
1277
0
      result->type == local_zone_always_refuse) {
1278
      /* The type DS does not go up one label. */
1279
0
      return result;
1280
0
    } else {
1281
0
      dname_remove_label(&name, &len);
1282
0
      labs--;
1283
0
    }
1284
0
  }
1285
0
  key.name = name;
1286
0
  key.namelen = len;
1287
0
  key.namelabs = labs;
1288
0
  rbtree_find_less_equal(&zones->ztree, &key, &res);
1289
0
  result = (struct local_zone*)res;
1290
  /* exact or smaller element (or no element) */
1291
0
  if(!result || result->dclass != dclass)
1292
0
    return NULL;
1293
  /* count number of labels matched */
1294
0
  (void)dname_lab_cmp(result->name, result->namelabs, key.name,
1295
0
    key.namelabs, &m);
1296
0
  while(result) { /* go up until qname is zone or subdomain of zone */
1297
0
    if(result->namelabs <= m)
1298
0
      if(ignoretags || !result->taglist ||
1299
0
        taglist_intersect(result->taglist, 
1300
0
        result->taglen, taglist, taglen))
1301
0
        break;
1302
0
    result = result->parent;
1303
0
  }
1304
0
  return result;
1305
0
}
1306
1307
struct local_zone* 
1308
local_zones_find(struct local_zones* zones,
1309
        uint8_t* name, size_t len, int labs, uint16_t dclass)
1310
0
{
1311
0
  struct local_zone key;
1312
0
  key.node.key = &key;
1313
0
  key.dclass = dclass;
1314
0
  key.name = name;
1315
0
  key.namelen = len;
1316
0
  key.namelabs = labs;
1317
  /* exact */
1318
0
  return (struct local_zone*)rbtree_search(&zones->ztree, &key);
1319
0
}
1320
1321
struct local_zone*
1322
local_zones_find_le(struct local_zones* zones,
1323
        uint8_t* name, size_t len, int labs, uint16_t dclass,
1324
  int* exact)
1325
0
{
1326
0
  struct local_zone key;
1327
0
  rbnode_type *node;
1328
0
  key.node.key = &key;
1329
0
  key.dclass = dclass;
1330
0
  key.name = name;
1331
0
  key.namelen = len;
1332
0
  key.namelabs = labs;
1333
0
  *exact = rbtree_find_less_equal(&zones->ztree, &key, &node);
1334
0
  return (struct local_zone*)node;
1335
0
}
1336
1337
/** encode answer consisting of 1 rrset */
1338
static int
1339
local_encode(struct query_info* qinfo, struct module_env* env,
1340
  struct edns_data* edns, struct comm_reply* repinfo, sldns_buffer* buf,
1341
  struct regional* temp, struct ub_packed_rrset_key* rrset, int ansec,
1342
  int rcode)
1343
0
{
1344
0
  struct reply_info rep;
1345
0
  uint16_t udpsize;
1346
  /* make answer with time=0 for fixed TTL values */
1347
0
  memset(&rep, 0, sizeof(rep));
1348
0
  rep.flags = (uint16_t)((BIT_QR | BIT_AA | BIT_RA) | rcode);
1349
0
  rep.qdcount = 1;
1350
0
  if(ansec)
1351
0
    rep.an_numrrsets = 1;
1352
0
  else  rep.ns_numrrsets = 1;
1353
0
  rep.rrset_count = 1;
1354
0
  rep.rrsets = &rrset;
1355
0
  rep.reason_bogus = LDNS_EDE_NONE;
1356
0
  udpsize = edns->udp_size;
1357
0
  edns->edns_version = EDNS_ADVERTISED_VERSION;
1358
0
  edns->udp_size = EDNS_ADVERTISED_SIZE;
1359
0
  edns->ext_rcode = 0;
1360
0
  edns->bits &= EDNS_DO;
1361
0
  if(!inplace_cb_reply_local_call(env, qinfo, NULL, &rep, rcode, edns,
1362
0
    repinfo, temp, env->now_tv) || !reply_info_answer_encode(qinfo, &rep,
1363
0
    *(uint16_t*)sldns_buffer_begin(buf), sldns_buffer_read_u16_at(buf, 2),
1364
0
    buf, 0, 0, temp, udpsize, edns, (int)(edns->bits&EDNS_DO), 0)) {
1365
0
    error_encode(buf, (LDNS_RCODE_SERVFAIL|BIT_AA), qinfo,
1366
0
      *(uint16_t*)sldns_buffer_begin(buf),
1367
0
      sldns_buffer_read_u16_at(buf, 2), edns);
1368
0
  }
1369
0
  return 1;
1370
0
}
1371
1372
/** encode local error answer */
1373
static void
1374
local_error_encode(struct query_info* qinfo, struct module_env* env,
1375
  struct edns_data* edns, struct comm_reply* repinfo, sldns_buffer* buf,
1376
  struct regional* temp, int rcode, int r, int ede_code,
1377
  const char* ede_txt)
1378
0
{
1379
0
  edns->edns_version = EDNS_ADVERTISED_VERSION;
1380
0
  edns->udp_size = EDNS_ADVERTISED_SIZE;
1381
0
  edns->ext_rcode = 0;
1382
0
  edns->bits &= EDNS_DO;
1383
1384
0
  if(!inplace_cb_reply_local_call(env, qinfo, NULL, NULL,
1385
0
    rcode, edns, repinfo, temp, env->now_tv))
1386
0
    edns->opt_list_inplace_cb_out = NULL;
1387
1388
0
  if(ede_code != LDNS_EDE_NONE && env->cfg->ede) {
1389
0
    edns_opt_list_append_ede(&edns->opt_list_out, temp,
1390
0
      ede_code, ede_txt);
1391
0
  }
1392
1393
0
  error_encode(buf, r, qinfo, *(uint16_t*)sldns_buffer_begin(buf),
1394
0
    sldns_buffer_read_u16_at(buf, 2), edns);
1395
0
}
1396
1397
/** find local data tag string match for the given type in the list */
1398
int
1399
local_data_find_tag_datas(const struct query_info* qinfo,
1400
  struct config_strlist* list, struct ub_packed_rrset_key* r,
1401
  struct regional* temp)
1402
0
{
1403
0
  struct config_strlist* p;
1404
0
  char buf[65536];
1405
0
  uint8_t rr[LDNS_RR_BUF_SIZE];
1406
0
  size_t len;
1407
0
  int res;
1408
0
  struct packed_rrset_data* d;
1409
0
  for(p=list; p; p=p->next) {
1410
0
    uint16_t rdr_type;
1411
1412
0
    len = sizeof(rr);
1413
    /* does this element match the type? */
1414
0
    snprintf(buf, sizeof(buf), ". %s", p->str);
1415
0
    res = sldns_str2wire_rr_buf(buf, rr, &len, NULL, 3600,
1416
0
      NULL, 0, NULL, 0);
1417
0
    if(res != 0)
1418
      /* parse errors are already checked before, in
1419
       * acllist check_data, skip this for robustness */
1420
0
      continue;
1421
0
    if(len < 1 /* . */ + 8 /* typeclassttl*/ + 2 /*rdatalen*/)
1422
0
      continue;
1423
0
    rdr_type = sldns_wirerr_get_type(rr, len, 1);
1424
0
    if(rdr_type != qinfo->qtype && rdr_type != LDNS_RR_TYPE_CNAME)
1425
0
      continue;
1426
    
1427
    /* do we have entries already? if not setup key */
1428
0
    if(r->rk.dname == NULL) {
1429
0
      r->entry.key = r;
1430
0
      r->rk.dname = qinfo->qname;
1431
0
      r->rk.dname_len = qinfo->qname_len;
1432
0
      r->rk.type = htons(rdr_type);
1433
0
      r->rk.rrset_class = htons(qinfo->qclass);
1434
0
      r->rk.flags = 0;
1435
0
      d = (struct packed_rrset_data*)regional_alloc_zero(
1436
0
        temp, sizeof(struct packed_rrset_data)
1437
0
        + sizeof(size_t) + sizeof(uint8_t*) +
1438
0
        sizeof(time_t));
1439
0
      if(!d) return 0; /* out of memory */
1440
0
      r->entry.data = d;
1441
0
      d->ttl = sldns_wirerr_get_ttl(rr, len, 1);
1442
0
      d->rr_len = (size_t*)((uint8_t*)d +
1443
0
        sizeof(struct packed_rrset_data));
1444
0
      d->rr_data = (uint8_t**)&(d->rr_len[1]);
1445
0
      d->rr_ttl = (time_t*)&(d->rr_data[1]);
1446
0
    }
1447
0
    d = (struct packed_rrset_data*)r->entry.data;
1448
    /* add entry to the data */
1449
0
    if(d->count != 0) {
1450
0
      size_t* oldlen = d->rr_len;
1451
0
      uint8_t** olddata = d->rr_data;
1452
0
      time_t* oldttl = d->rr_ttl;
1453
      /* increase arrays for lookup */
1454
      /* this is of course slow for very many records,
1455
       * but most redirects are expected with few records */
1456
0
      d->rr_len = (size_t*)regional_alloc_zero(temp,
1457
0
        (d->count+1)*sizeof(size_t));
1458
0
      d->rr_data = (uint8_t**)regional_alloc_zero(temp,
1459
0
        (d->count+1)*sizeof(uint8_t*));
1460
0
      d->rr_ttl = (time_t*)regional_alloc_zero(temp,
1461
0
        (d->count+1)*sizeof(time_t));
1462
0
      if(!d->rr_len || !d->rr_data || !d->rr_ttl)
1463
0
        return 0; /* out of memory */
1464
      /* first one was allocated after struct d, but new
1465
       * ones get their own array increment alloc, so
1466
       * copy old content */
1467
0
      memmove(d->rr_len, oldlen, d->count*sizeof(size_t));
1468
0
      memmove(d->rr_data, olddata, d->count*sizeof(uint8_t*));
1469
0
      memmove(d->rr_ttl, oldttl, d->count*sizeof(time_t));
1470
0
    }
1471
1472
0
    d->rr_len[d->count] = sldns_wirerr_get_rdatalen(rr, len, 1)+2;
1473
0
    d->rr_ttl[d->count] = sldns_wirerr_get_ttl(rr, len, 1);
1474
0
    d->rr_data[d->count] = regional_alloc_init(temp,
1475
0
      sldns_wirerr_get_rdatawl(rr, len, 1),
1476
0
      d->rr_len[d->count]);
1477
0
    if(!d->rr_data[d->count])
1478
0
      return 0; /* out of memory */
1479
0
    d->count++;
1480
0
  }
1481
0
  if(r->rk.dname)
1482
0
    return 1;
1483
0
  return 0;
1484
0
}
1485
1486
static int
1487
find_tag_datas(struct query_info* qinfo, struct config_strlist* list,
1488
  struct ub_packed_rrset_key* r, struct regional* temp)
1489
0
{
1490
0
  int result = local_data_find_tag_datas(qinfo, list, r, temp);
1491
1492
  /* If we've found a non-exact alias type of local data, make a shallow
1493
   * copy of the RRset and remember it in qinfo to complete the alias
1494
   * chain later. */
1495
0
  if(result && qinfo->qtype != LDNS_RR_TYPE_CNAME &&
1496
0
    r->rk.type == htons(LDNS_RR_TYPE_CNAME)) {
1497
0
    qinfo->local_alias =
1498
0
      regional_alloc_zero(temp, sizeof(struct local_rrset));
1499
0
    if(!qinfo->local_alias)
1500
0
      return 0; /* out of memory */
1501
0
    qinfo->local_alias->rrset =
1502
0
      regional_alloc_init(temp, r, sizeof(*r));
1503
0
    if(!qinfo->local_alias->rrset)
1504
0
      return 0; /* out of memory */
1505
0
  }
1506
0
  return result;
1507
0
}
1508
1509
int
1510
local_data_answer(struct local_zone* z, struct module_env* env,
1511
  struct query_info* qinfo, struct edns_data* edns,
1512
  struct comm_reply* repinfo, sldns_buffer* buf,
1513
  struct regional* temp, int labs, struct local_data** ldp,
1514
  enum localzone_type lz_type, int tag, struct config_strlist** tag_datas,
1515
  size_t tag_datas_size, char** tagname, int num_tags)
1516
0
{
1517
0
  struct local_data key;
1518
0
  struct local_data* ld;
1519
0
  struct local_rrset* lr;
1520
0
  key.node.key = &key;
1521
0
  key.name = qinfo->qname;
1522
0
  key.namelen = qinfo->qname_len;
1523
0
  key.namelabs = labs;
1524
0
  if(lz_type == local_zone_redirect ||
1525
0
    lz_type == local_zone_inform_redirect) {
1526
0
    key.name = z->name;
1527
0
    key.namelen = z->namelen;
1528
0
    key.namelabs = z->namelabs;
1529
0
    if(tag != -1 && (size_t)tag<tag_datas_size && tag_datas[tag]) {
1530
0
      struct ub_packed_rrset_key r;
1531
0
      memset(&r, 0, sizeof(r));
1532
0
      if(find_tag_datas(qinfo, tag_datas[tag], &r, temp)) {
1533
0
        verbose(VERB_ALGO, "redirect with tag data [%d] %s",
1534
0
          tag, (tag<num_tags?tagname[tag]:"null"));
1535
1536
        /* If we found a matching alias, we should
1537
         * use it as part of the answer, but we can't
1538
         * encode it until we complete the alias
1539
         * chain. */
1540
0
        if(qinfo->local_alias)
1541
0
          return 1;
1542
0
        return local_encode(qinfo, env, edns, repinfo, buf, temp,
1543
0
          &r, 1, LDNS_RCODE_NOERROR);
1544
0
      }
1545
0
    }
1546
0
  }
1547
0
  ld = (struct local_data*)rbtree_search(&z->data, &key.node);
1548
0
  *ldp = ld;
1549
0
  if(!ld) {
1550
0
    return 0;
1551
0
  }
1552
0
  lr = local_data_find_type(ld, qinfo->qtype, 1);
1553
0
  if(!lr)
1554
0
    return 0;
1555
1556
  /* Special case for alias matching.  See local_data_answer(). */
1557
0
  if((lz_type == local_zone_redirect ||
1558
0
    lz_type == local_zone_inform_redirect) &&
1559
0
    qinfo->qtype != LDNS_RR_TYPE_CNAME &&
1560
0
    lr->rrset->rk.type == htons(LDNS_RR_TYPE_CNAME)) {
1561
0
    uint8_t* ctarget;
1562
0
    size_t ctargetlen = 0;
1563
1564
0
    qinfo->local_alias =
1565
0
      regional_alloc_zero(temp, sizeof(struct local_rrset));
1566
0
    if(!qinfo->local_alias)
1567
0
      return 0; /* out of memory */
1568
0
    qinfo->local_alias->rrset = regional_alloc_init(
1569
0
      temp, lr->rrset, sizeof(*lr->rrset));
1570
0
    if(!qinfo->local_alias->rrset)
1571
0
      return 0; /* out of memory */
1572
0
    qinfo->local_alias->rrset->rk.dname = qinfo->qname;
1573
0
    qinfo->local_alias->rrset->rk.dname_len = qinfo->qname_len;
1574
0
    get_cname_target(lr->rrset, &ctarget, &ctargetlen);
1575
0
    if(!ctargetlen)
1576
0
      return 0; /* invalid cname */
1577
0
    if(dname_is_wild(ctarget)) {
1578
      /* synthesize cname target */
1579
0
      struct packed_rrset_data* d, *lr_d;
1580
      /* -3 for wildcard label and root label from qname */
1581
0
      size_t newtargetlen = qinfo->qname_len + ctargetlen - 3;
1582
1583
0
      log_assert(ctargetlen >= 3);
1584
0
      log_assert(qinfo->qname_len >= 1);
1585
1586
0
      if(newtargetlen > LDNS_MAX_DOMAINLEN) {
1587
0
        qinfo->local_alias = NULL;
1588
0
        local_error_encode(qinfo, env, edns, repinfo,
1589
0
          buf, temp, LDNS_RCODE_YXDOMAIN,
1590
0
          (LDNS_RCODE_YXDOMAIN|BIT_AA),
1591
0
          LDNS_EDE_OTHER,
1592
0
          "DNAME expansion became too large");
1593
0
        return 1;
1594
0
      }
1595
0
      memset(&qinfo->local_alias->rrset->entry, 0,
1596
0
        sizeof(qinfo->local_alias->rrset->entry));
1597
0
      qinfo->local_alias->rrset->entry.key =
1598
0
        qinfo->local_alias->rrset;
1599
0
      qinfo->local_alias->rrset->entry.hash =
1600
0
        rrset_key_hash(&qinfo->local_alias->rrset->rk);
1601
0
      d = (struct packed_rrset_data*)regional_alloc_zero(temp,
1602
0
        sizeof(struct packed_rrset_data) + sizeof(size_t) +
1603
0
        sizeof(uint8_t*) + sizeof(time_t) + sizeof(uint16_t)
1604
0
        + newtargetlen);
1605
0
      if(!d)
1606
0
        return 0; /* out of memory */
1607
0
      lr_d = (struct packed_rrset_data*)lr->rrset->entry.data;
1608
0
      qinfo->local_alias->rrset->entry.data = d;
1609
0
      d->ttl = lr_d->rr_ttl[0]; /* RFC6672-like behavior:
1610
              synth CNAME TTL uses original TTL*/
1611
0
      d->count = 1;
1612
0
      d->rrsig_count = 0;
1613
0
      d->trust = rrset_trust_ans_noAA;
1614
0
      d->rr_len = (size_t*)((uint8_t*)d +
1615
0
        sizeof(struct packed_rrset_data));
1616
0
      d->rr_len[0] = newtargetlen + sizeof(uint16_t);
1617
0
      packed_rrset_ptr_fixup(d);
1618
0
      d->rr_ttl[0] = d->ttl;
1619
0
      sldns_write_uint16(d->rr_data[0], newtargetlen);
1620
      /* write qname */
1621
0
      memmove(d->rr_data[0] + sizeof(uint16_t), qinfo->qname,
1622
0
        qinfo->qname_len - 1);
1623
      /* write cname target wildcard label */
1624
0
      memmove(d->rr_data[0] + sizeof(uint16_t) +
1625
0
        qinfo->qname_len - 1, ctarget + 2,
1626
0
        ctargetlen - 2);
1627
0
    }
1628
0
    return 1;
1629
0
  }
1630
0
  if(lz_type == local_zone_redirect ||
1631
0
    lz_type == local_zone_inform_redirect) {
1632
    /* convert rrset name to query name; like a wildcard */
1633
0
    struct ub_packed_rrset_key r = *lr->rrset;
1634
0
    r.rk.dname = qinfo->qname;
1635
0
    r.rk.dname_len = qinfo->qname_len;
1636
0
    return local_encode(qinfo, env, edns, repinfo, buf, temp, &r, 1,
1637
0
      LDNS_RCODE_NOERROR);
1638
0
  }
1639
0
  return local_encode(qinfo, env, edns, repinfo, buf, temp, lr->rrset, 1,
1640
0
    LDNS_RCODE_NOERROR);
1641
0
}
1642
1643
/**
1644
 * See if the local zone does not cover the name, eg. the name is not
1645
 * in the zone and the zone is transparent */
1646
static int
1647
local_zone_does_not_cover(struct local_zone* z, struct query_info* qinfo,
1648
  int labs)
1649
0
{
1650
0
  struct local_data key;
1651
0
  struct local_data* ld = NULL;
1652
0
  struct local_rrset* lr = NULL;
1653
0
  if(z->type == local_zone_always_transparent || z->type == local_zone_block_a)
1654
0
    return 1;
1655
0
  if(z->type != local_zone_transparent
1656
0
    && z->type != local_zone_typetransparent
1657
0
    && z->type != local_zone_inform)
1658
0
    return 0;
1659
0
  key.node.key = &key;
1660
0
  key.name = qinfo->qname;
1661
0
  key.namelen = qinfo->qname_len;
1662
0
  key.namelabs = labs;
1663
0
  ld = (struct local_data*)rbtree_search(&z->data, &key.node);
1664
0
  if(z->type == local_zone_transparent || z->type == local_zone_inform)
1665
0
    return (ld == NULL);
1666
0
  if(ld)
1667
0
    lr = local_data_find_type(ld, qinfo->qtype, 1);
1668
  /* local_zone_typetransparent */
1669
0
  return (lr == NULL);
1670
0
}
1671
1672
static inline int
1673
0
local_zone_is_udp_query(struct comm_reply* repinfo) {
1674
0
  return repinfo != NULL
1675
0
      ? (repinfo->c != NULL
1676
0
        ? repinfo->c->type == comm_udp
1677
0
        : 0)
1678
0
      : 0;
1679
0
}
1680
1681
int
1682
local_zones_zone_answer(struct local_zone* z, struct module_env* env,
1683
  struct query_info* qinfo, struct edns_data* edns,
1684
  struct comm_reply* repinfo, sldns_buffer* buf, struct regional* temp,
1685
  struct local_data* ld, enum localzone_type lz_type)
1686
0
{
1687
0
  if(lz_type == local_zone_deny ||
1688
0
    lz_type == local_zone_always_deny ||
1689
0
    lz_type == local_zone_inform_deny) {
1690
    /** no reply at all, signal caller by clearing buffer. */
1691
0
    sldns_buffer_clear(buf);
1692
0
    sldns_buffer_flip(buf);
1693
0
    return 1;
1694
0
  } else if(lz_type == local_zone_refuse
1695
0
    || lz_type == local_zone_always_refuse) {
1696
0
    local_error_encode(qinfo, env, edns, repinfo, buf, temp,
1697
0
      LDNS_RCODE_REFUSED, (LDNS_RCODE_REFUSED|BIT_AA),
1698
0
      LDNS_EDE_NONE, NULL);
1699
0
    return 1;
1700
0
  } else if(lz_type == local_zone_static ||
1701
0
    lz_type == local_zone_redirect ||
1702
0
    lz_type == local_zone_inform_redirect ||
1703
0
    lz_type == local_zone_always_nxdomain ||
1704
0
    lz_type == local_zone_always_nodata ||
1705
0
    (lz_type == local_zone_truncate
1706
0
      && local_zone_is_udp_query(repinfo))) {
1707
    /* for static, reply nodata or nxdomain
1708
     * for redirect, reply nodata */
1709
    /* no additional section processing,
1710
     * cname, dname or wildcard processing,
1711
     * or using closest match for NSEC.
1712
     * or using closest match for returning delegation downwards
1713
     */
1714
0
    int rcode = (ld || lz_type == local_zone_redirect ||
1715
0
      lz_type == local_zone_inform_redirect ||
1716
0
      lz_type == local_zone_always_nodata ||
1717
0
      lz_type == local_zone_truncate)?
1718
0
      LDNS_RCODE_NOERROR:LDNS_RCODE_NXDOMAIN;
1719
0
    rcode = (lz_type == local_zone_truncate ? (rcode|BIT_TC) : rcode);
1720
0
    if(z != NULL && z->soa && z->soa_negative)
1721
0
      return local_encode(qinfo, env, edns, repinfo, buf, temp,
1722
0
        z->soa_negative, 0, rcode);
1723
0
    local_error_encode(qinfo, env, edns, repinfo, buf, temp,
1724
0
      rcode, (rcode|BIT_AA), LDNS_EDE_NONE, NULL);
1725
0
    return 1;
1726
0
  } else if(lz_type == local_zone_typetransparent
1727
0
    || lz_type == local_zone_always_transparent) {
1728
    /* no NODATA or NXDOMAINS for this zone type */
1729
0
    return 0;
1730
0
  } else if(lz_type == local_zone_block_a) {
1731
    /* Return NODATA for all A queries */
1732
0
    if(qinfo->qtype == LDNS_RR_TYPE_A) {
1733
0
      local_error_encode(qinfo, env, edns, repinfo, buf, temp,
1734
0
        LDNS_RCODE_NOERROR, (LDNS_RCODE_NOERROR|BIT_AA),
1735
0
        LDNS_EDE_NONE, NULL);
1736
0
        return 1;
1737
0
    }
1738
1739
0
    return 0;
1740
0
  } else if(lz_type == local_zone_always_null) {
1741
    /* 0.0.0.0 or ::0 or noerror/nodata for this zone type,
1742
     * used for blocklists. */
1743
0
    if(qinfo->qtype == LDNS_RR_TYPE_A ||
1744
0
      qinfo->qtype == LDNS_RR_TYPE_AAAA) {
1745
0
      struct ub_packed_rrset_key lrr;
1746
0
      struct packed_rrset_data d;
1747
0
      time_t rr_ttl = 3600;
1748
0
      size_t rr_len = 0;
1749
0
      uint8_t rr_data[2+16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
1750
0
      uint8_t* rr_datas = rr_data;
1751
0
      memset(&lrr, 0, sizeof(lrr));
1752
0
      memset(&d, 0, sizeof(d));
1753
0
      lrr.entry.data = &d;
1754
0
      lrr.rk.dname = qinfo->qname;
1755
0
      lrr.rk.dname_len = qinfo->qname_len;
1756
0
      lrr.rk.type = htons(qinfo->qtype);
1757
0
      lrr.rk.rrset_class = htons(qinfo->qclass);
1758
0
      if(qinfo->qtype == LDNS_RR_TYPE_A) {
1759
0
        rr_len = 4;
1760
0
        sldns_write_uint16(rr_data, rr_len);
1761
0
        rr_len += 2;
1762
0
      } else {
1763
0
        rr_len = 16;
1764
0
        sldns_write_uint16(rr_data, rr_len);
1765
0
        rr_len += 2;
1766
0
      }
1767
0
      d.ttl = rr_ttl;
1768
0
      d.count = 1;
1769
0
      d.rr_len = &rr_len;
1770
0
      d.rr_data = &rr_datas;
1771
0
      d.rr_ttl = &rr_ttl;
1772
0
      return local_encode(qinfo, env, edns, repinfo, buf, temp,
1773
0
        &lrr, 1, LDNS_RCODE_NOERROR);
1774
0
    } else {
1775
      /* NODATA: No EDE needed */
1776
0
      local_error_encode(qinfo, env, edns, repinfo, buf,
1777
0
        temp, LDNS_RCODE_NOERROR,
1778
0
        (LDNS_RCODE_NOERROR|BIT_AA), -1, NULL);
1779
0
    }
1780
0
    return 1;
1781
0
  }
1782
  /* else lz_type == local_zone_transparent */
1783
1784
  /* if the zone is transparent and the name exists, but the type
1785
   * does not, then we should make this noerror/nodata */
1786
0
  if(ld && ld->rrsets) {
1787
0
    int rcode = LDNS_RCODE_NOERROR;
1788
0
    if(z != NULL && z->soa && z->soa_negative)
1789
0
      return local_encode(qinfo, env, edns, repinfo, buf, temp,
1790
0
        z->soa_negative, 0, rcode);
1791
    /* NODATA: No EDE needed */
1792
0
    local_error_encode(qinfo, env, edns, repinfo, buf, temp, rcode,
1793
0
      (rcode|BIT_AA), LDNS_EDE_NONE, NULL);
1794
0
    return 1;
1795
0
  }
1796
1797
  /* stop here, and resolve further on */
1798
0
  return 0;
1799
0
}
1800
1801
/** print log information for an inform zone query */
1802
static void
1803
lz_inform_print(struct local_zone* z, struct query_info* qinfo,
1804
  struct sockaddr_storage* addr, socklen_t addrlen)
1805
0
{
1806
0
  char ip[128], txt[512];
1807
0
  char zname[LDNS_MAX_DOMAINLEN];
1808
0
  uint16_t port = ntohs(((struct sockaddr_in*)addr)->sin_port);
1809
0
  dname_str(z->name, zname);
1810
0
  addr_to_str(addr, addrlen, ip, sizeof(ip));
1811
0
  snprintf(txt, sizeof(txt), "%s %s %s@%u", zname, local_zone_type2str(z->type), ip,
1812
0
    (unsigned)port);
1813
0
  log_nametypeclass(NO_VERBOSE, txt, qinfo->qname, qinfo->qtype, qinfo->qclass);
1814
0
}
1815
1816
static enum localzone_type
1817
lz_type(uint8_t *taglist, size_t taglen, uint8_t *taglist2, size_t taglen2,
1818
  uint8_t *tagactions, size_t tagactionssize, enum localzone_type lzt,
1819
  struct comm_reply* repinfo, struct rbtree_type* override_tree,
1820
  int* tag, char** tagname, int num_tags)
1821
0
{
1822
0
  struct local_zone_override* lzo;  
1823
0
  if(repinfo && override_tree) {
1824
0
    lzo = (struct local_zone_override*)addr_tree_lookup(
1825
0
      override_tree, &repinfo->client_addr,
1826
0
      repinfo->client_addrlen);
1827
0
    if(lzo && lzo->type) {
1828
0
      verbose(VERB_ALGO, "local zone override to type %s",
1829
0
        local_zone_type2str(lzo->type));
1830
0
      return lzo->type;
1831
0
    }
1832
0
  }
1833
0
  if(!taglist || !taglist2)
1834
0
    return lzt;
1835
0
  return local_data_find_tag_action(taglist, taglen, taglist2, taglen2,
1836
0
    tagactions, tagactionssize, lzt, tag, tagname, num_tags);
1837
0
}
1838
1839
enum localzone_type
1840
local_data_find_tag_action(const uint8_t* taglist, size_t taglen,
1841
  const uint8_t* taglist2, size_t taglen2, const uint8_t* tagactions,
1842
  size_t tagactionssize, enum localzone_type lzt, int* tag,
1843
  char* const* tagname, int num_tags)
1844
0
{
1845
0
  size_t i, j;
1846
0
  uint8_t tagmatch;
1847
1848
0
  for(i=0; i<taglen && i<taglen2; i++) {
1849
0
    tagmatch = (taglist[i] & taglist2[i]);
1850
0
    for(j=0; j<8 && tagmatch>0; j++) {
1851
0
      if((tagmatch & 0x1)) {
1852
0
        *tag = (int)(i*8+j);
1853
0
        verbose(VERB_ALGO, "matched tag [%d] %s",
1854
0
          *tag, (*tag<num_tags?tagname[*tag]:"null"));
1855
        /* does this tag have a tag action? */
1856
0
        if(i*8+j < tagactionssize && tagactions
1857
0
           && tagactions[i*8+j] != 0) {
1858
0
          verbose(VERB_ALGO, "tag action [%d] %s to type %s",
1859
0
          *tag, (*tag<num_tags?tagname[*tag]:"null"),
1860
0
            local_zone_type2str(
1861
0
          (enum localzone_type)
1862
0
          tagactions[i*8+j]));
1863
0
          return (enum localzone_type)tagactions[i*8+j];
1864
0
        }
1865
0
        return lzt;
1866
0
      }
1867
0
      tagmatch >>= 1; 
1868
0
    }
1869
0
  }
1870
0
  return lzt;
1871
0
}
1872
1873
int 
1874
local_zones_answer(struct local_zones* zones, struct module_env* env,
1875
  struct query_info* qinfo, struct edns_data* edns, sldns_buffer* buf,
1876
  struct regional* temp, struct comm_reply* repinfo, uint8_t* taglist,
1877
  size_t taglen, uint8_t* tagactions, size_t tagactionssize,
1878
  struct config_strlist** tag_datas, size_t tag_datas_size,
1879
  char** tagname, int num_tags, struct view* view)
1880
0
{
1881
  /* see if query is covered by a zone,
1882
   *  if so:  - try to match (exact) local data 
1883
   *    - look at zone type for negative response. */
1884
0
  int labs = dname_count_labels(qinfo->qname);
1885
0
  struct local_data* ld = NULL;
1886
0
  struct local_zone* z = NULL;
1887
0
  enum localzone_type lzt = local_zone_transparent;
1888
0
  int r, tag = -1;
1889
1890
0
  if(view) {
1891
0
    lock_rw_rdlock(&view->lock);
1892
0
    if(view->local_zones &&
1893
0
      (z = local_zones_lookup(view->local_zones,
1894
0
      qinfo->qname, qinfo->qname_len, labs,
1895
0
      qinfo->qclass, qinfo->qtype, 0))) {
1896
0
      lock_rw_rdlock(&z->lock);
1897
0
      lzt = z->type;
1898
0
    }
1899
0
    if(lzt == local_zone_noview) {
1900
0
      lock_rw_unlock(&z->lock);
1901
0
      z = NULL;
1902
0
    }
1903
0
    if(z && (lzt == local_zone_transparent ||
1904
0
      lzt == local_zone_typetransparent ||
1905
0
      lzt == local_zone_inform ||
1906
0
      lzt == local_zone_always_transparent ||
1907
0
      lzt == local_zone_block_a) &&
1908
0
      local_zone_does_not_cover(z, qinfo, labs)) {
1909
0
      lock_rw_unlock(&z->lock);
1910
0
      z = NULL;
1911
0
    }
1912
0
    if(view->local_zones && !z && !view->isfirst){
1913
0
      lock_rw_unlock(&view->lock);
1914
0
      return 0;
1915
0
    }
1916
0
    if(z && verbosity >= VERB_ALGO) {
1917
0
      char zname[LDNS_MAX_DOMAINLEN];
1918
0
      dname_str(z->name, zname);
1919
0
      verbose(VERB_ALGO, "using localzone %s %s from view %s", 
1920
0
        zname, local_zone_type2str(lzt), view->name);
1921
0
    }
1922
0
    lock_rw_unlock(&view->lock);
1923
0
  }
1924
0
  if(!z) {
1925
    /* try global local_zones tree */
1926
0
    lock_rw_rdlock(&zones->lock);
1927
0
    if(!(z = local_zones_tags_lookup(zones, qinfo->qname,
1928
0
      qinfo->qname_len, labs, qinfo->qclass, qinfo->qtype,
1929
0
      taglist, taglen, 0, 0))) {
1930
0
      lock_rw_unlock(&zones->lock);
1931
0
      return 0;
1932
0
    }
1933
0
    lock_rw_rdlock(&z->lock);
1934
0
    lzt = lz_type(taglist, taglen, z->taglist, z->taglen,
1935
0
      tagactions, tagactionssize, z->type, repinfo,
1936
0
      z->override_tree, &tag, tagname, num_tags);
1937
0
    lock_rw_unlock(&zones->lock);
1938
0
    if(z && verbosity >= VERB_ALGO) {
1939
0
      char zname[LDNS_MAX_DOMAINLEN];
1940
0
      dname_str(z->name, zname);
1941
0
      verbose(VERB_ALGO, "using localzone %s %s", zname,
1942
0
        local_zone_type2str(lzt));
1943
0
    }
1944
0
  }
1945
0
  if((env->cfg->log_local_actions ||
1946
0
      lzt == local_zone_inform ||
1947
0
      lzt == local_zone_inform_deny ||
1948
0
      lzt == local_zone_inform_redirect)
1949
0
      && repinfo)
1950
0
    lz_inform_print(z, qinfo, &repinfo->client_addr,
1951
0
      repinfo->client_addrlen);
1952
1953
0
  if(lzt != local_zone_always_refuse
1954
0
    && lzt != local_zone_always_transparent
1955
0
    && lzt != local_zone_block_a
1956
0
    && lzt != local_zone_always_nxdomain
1957
0
    && lzt != local_zone_always_nodata
1958
0
    && lzt != local_zone_always_deny
1959
0
    && local_data_answer(z, env, qinfo, edns, repinfo, buf, temp, labs,
1960
0
      &ld, lzt, tag, tag_datas, tag_datas_size, tagname, num_tags)) {
1961
0
    lock_rw_unlock(&z->lock);
1962
    /* We should tell the caller that encode is deferred if we found
1963
     * a local alias. */
1964
0
    return !qinfo->local_alias;
1965
0
  }
1966
0
  r = local_zones_zone_answer(z, env, qinfo, edns, repinfo, buf, temp, ld, lzt);
1967
0
  lock_rw_unlock(&z->lock);
1968
0
  return r && !qinfo->local_alias; /* see above */
1969
0
}
1970
1971
const char* local_zone_type2str(enum localzone_type t)
1972
0
{
1973
0
  switch(t) {
1974
0
    case local_zone_unset: return "unset";
1975
0
    case local_zone_deny: return "deny";
1976
0
    case local_zone_refuse: return "refuse";
1977
0
    case local_zone_redirect: return "redirect";
1978
0
    case local_zone_transparent: return "transparent";
1979
0
    case local_zone_typetransparent: return "typetransparent";
1980
0
    case local_zone_static: return "static";
1981
0
    case local_zone_nodefault: return "nodefault";
1982
0
    case local_zone_inform: return "inform";
1983
0
    case local_zone_inform_deny: return "inform_deny";
1984
0
    case local_zone_inform_redirect: return "inform_redirect";
1985
0
    case local_zone_always_transparent: return "always_transparent";
1986
0
    case local_zone_block_a: return "block_a";
1987
0
    case local_zone_always_refuse: return "always_refuse";
1988
0
    case local_zone_always_nxdomain: return "always_nxdomain";
1989
0
    case local_zone_always_nodata: return "always_nodata";
1990
0
    case local_zone_always_deny: return "always_deny";
1991
0
    case local_zone_always_null: return "always_null";
1992
0
    case local_zone_noview: return "noview";
1993
0
    case local_zone_truncate: return "truncate";
1994
0
    case local_zone_invalid: return "invalid";
1995
0
  }
1996
0
  return "badtyped"; 
1997
0
}
1998
1999
int local_zone_str2type(const char* type, enum localzone_type* t)
2000
0
{
2001
0
  if(strcmp(type, "deny") == 0)
2002
0
    *t = local_zone_deny;
2003
0
  else if(strcmp(type, "refuse") == 0)
2004
0
    *t = local_zone_refuse;
2005
0
  else if(strcmp(type, "static") == 0)
2006
0
    *t = local_zone_static;
2007
0
  else if(strcmp(type, "transparent") == 0)
2008
0
    *t = local_zone_transparent;
2009
0
  else if(strcmp(type, "typetransparent") == 0)
2010
0
    *t = local_zone_typetransparent;
2011
0
  else if(strcmp(type, "redirect") == 0)
2012
0
    *t = local_zone_redirect;
2013
0
  else if(strcmp(type, "inform") == 0)
2014
0
    *t = local_zone_inform;
2015
0
  else if(strcmp(type, "inform_deny") == 0)
2016
0
    *t = local_zone_inform_deny;
2017
0
  else if(strcmp(type, "inform_redirect") == 0)
2018
0
    *t = local_zone_inform_redirect;
2019
0
  else if(strcmp(type, "always_transparent") == 0)
2020
0
    *t = local_zone_always_transparent;
2021
0
  else if(strcmp(type, "block_a") == 0)
2022
0
    *t = local_zone_block_a;
2023
0
  else if(strcmp(type, "always_refuse") == 0)
2024
0
    *t = local_zone_always_refuse;
2025
0
  else if(strcmp(type, "always_nxdomain") == 0)
2026
0
    *t = local_zone_always_nxdomain;
2027
0
  else if(strcmp(type, "always_nodata") == 0)
2028
0
    *t = local_zone_always_nodata;
2029
0
  else if(strcmp(type, "always_deny") == 0)
2030
0
    *t = local_zone_always_deny;
2031
0
  else if(strcmp(type, "always_null") == 0)
2032
0
    *t = local_zone_always_null;
2033
0
  else if(strcmp(type, "noview") == 0)
2034
0
    *t = local_zone_noview;
2035
0
  else if(strcmp(type, "truncate") == 0)
2036
0
    *t = local_zone_truncate;
2037
0
  else if(strcmp(type, "nodefault") == 0)
2038
0
    *t = local_zone_nodefault;
2039
0
  else return 0;
2040
0
  return 1;
2041
0
}
2042
2043
/** iterate over the kiddies of the given name and set their parent ptr */
2044
static void
2045
set_kiddo_parents(struct local_zone* z, struct local_zone* match, 
2046
  struct local_zone* newp)
2047
0
{
2048
  /* both zones and z are locked already */
2049
  /* in the sorted rbtree, the kiddies of z are located after z */
2050
  /* z must be present in the tree */
2051
0
  struct local_zone* p = z;
2052
0
  p = (struct local_zone*)rbtree_next(&p->node);
2053
0
  while(p!=(struct local_zone*)RBTREE_NULL &&
2054
0
    p->dclass == z->dclass && dname_strict_subdomain(p->name,
2055
0
    p->namelabs, z->name, z->namelabs)) {
2056
    /* update parent ptr */
2057
    /* only when matches with existing parent pointer, so that
2058
     * deeper child structures are not touched, i.e.
2059
     * update of x, and a.x, b.x, f.b.x, g.b.x, c.x, y
2060
     * gets to update a.x, b.x and c.x */
2061
0
    lock_rw_wrlock(&p->lock);
2062
0
    if(p->parent == match)
2063
0
      p->parent = newp;
2064
0
    lock_rw_unlock(&p->lock);
2065
0
    p = (struct local_zone*)rbtree_next(&p->node);
2066
0
  }
2067
0
}
2068
2069
struct local_zone* local_zones_add_zone(struct local_zones* zones,
2070
  uint8_t* name, size_t len, int labs, uint16_t dclass,
2071
  enum localzone_type tp)
2072
0
{
2073
0
  int exact;
2074
  /* create */
2075
0
  struct local_zone *prev;
2076
0
  struct local_zone* z = local_zone_create(name, len, labs, tp, dclass);
2077
0
  if(!z) {
2078
0
    free(name);
2079
0
    return NULL;
2080
0
  }
2081
0
  lock_rw_wrlock(&z->lock);
2082
2083
  /* find the closest parent */
2084
0
  prev = local_zones_find_le(zones, name, len, labs, dclass, &exact);
2085
0
  if(!exact)
2086
0
    z->parent = find_closest_parent(z, prev);
2087
2088
  /* insert into the tree */
2089
0
  if(exact||!rbtree_insert(&zones->ztree, &z->node)) {
2090
    /* duplicate entry! */
2091
0
    lock_rw_unlock(&z->lock);
2092
0
    local_zone_delete(z);
2093
0
    log_err("internal: duplicate entry in local_zones_add_zone");
2094
0
    return NULL;
2095
0
  }
2096
2097
  /* set parent pointers right */
2098
0
  set_kiddo_parents(z, z->parent, z);
2099
2100
0
  lock_rw_unlock(&z->lock);
2101
0
  return z;
2102
0
}
2103
2104
void local_zones_del_zone(struct local_zones* zones, struct local_zone* z)
2105
0
{
2106
  /* fix up parents in tree */
2107
0
  lock_rw_wrlock(&z->lock);
2108
0
  set_kiddo_parents(z, z, z->parent);
2109
2110
  /* remove from tree */
2111
0
  (void)rbtree_delete(&zones->ztree, z);
2112
2113
  /* delete the zone */
2114
0
  lock_rw_unlock(&z->lock);
2115
0
  local_zone_delete(z);
2116
0
}
2117
2118
int
2119
local_zones_add_RR(struct local_zones* zones, const char* rr)
2120
0
{
2121
0
  uint8_t* rr_name;
2122
0
  uint16_t rr_class, rr_type;
2123
0
  size_t len;
2124
0
  int labs;
2125
0
  struct local_zone* z;
2126
0
  int r;
2127
0
  if(!get_rr_nameclass(rr, &rr_name, &rr_class, &rr_type)) {
2128
0
    return 0;
2129
0
  }
2130
0
  labs = dname_count_size_labels(rr_name, &len);
2131
  /* could first try readlock then get writelock if zone does not exist,
2132
   * but we do not add enough RRs (from multiple threads) to optimize */
2133
0
  lock_rw_wrlock(&zones->lock);
2134
0
  z = local_zones_lookup(zones, rr_name, len, labs, rr_class, rr_type,
2135
0
    1);
2136
0
  if(!z) {
2137
0
    z = local_zones_add_zone(zones, rr_name, len, labs, rr_class,
2138
0
      local_zone_transparent);
2139
0
    if(!z) {
2140
0
      lock_rw_unlock(&zones->lock);
2141
0
      return 0;
2142
0
    }
2143
0
  } else {
2144
0
    free(rr_name);
2145
0
  }
2146
0
  lock_rw_wrlock(&z->lock);
2147
0
  lock_rw_unlock(&zones->lock);
2148
0
  r = lz_enter_rr_into_zone(z, rr);
2149
0
  lock_rw_unlock(&z->lock);
2150
0
  return r;
2151
0
}
2152
2153
/** returns true if the node is terminal so no deeper domain names exist */
2154
static int
2155
is_terminal(struct local_data* d)
2156
0
{
2157
  /* for empty nonterminals, the deeper domain names are sorted
2158
   * right after them, so simply check the next name in the tree 
2159
   */
2160
0
  struct local_data* n = (struct local_data*)rbtree_next(&d->node);
2161
0
  if(n == (struct local_data*)RBTREE_NULL)
2162
0
    return 1; /* last in tree, no deeper node */
2163
0
  if(dname_strict_subdomain(n->name, n->namelabs, d->name, d->namelabs))
2164
0
    return 0; /* there is a deeper node */
2165
0
  return 1;
2166
0
}
2167
2168
/** delete empty terminals from tree when final data is deleted */
2169
static void 
2170
del_empty_term(struct local_zone* z, struct local_data* d, 
2171
  uint8_t* name, size_t len, int labs)
2172
0
{
2173
0
  while(d && d->rrsets == NULL && is_terminal(d)) {
2174
    /* is this empty nonterminal? delete */
2175
    /* note, no memory recycling in zone region */
2176
0
    (void)rbtree_delete(&z->data, d);
2177
2178
    /* go up and to the next label */
2179
0
    if(dname_is_root(name))
2180
0
      return;
2181
0
    dname_remove_label(&name, &len);
2182
0
    labs--;
2183
0
    d = local_zone_find_data(z, name, len, labs);
2184
0
  }
2185
0
}
2186
2187
/** find and remove type from list in domain struct */
2188
static void
2189
del_local_rrset(struct local_data* d, uint16_t dtype)
2190
0
{
2191
0
  struct local_rrset* prev=NULL, *p=d->rrsets;
2192
0
  while(p && ntohs(p->rrset->rk.type) != dtype) {
2193
0
    prev = p;
2194
0
    p = p->next;
2195
0
  }
2196
0
  if(!p) 
2197
0
    return; /* rrset type not found */
2198
  /* unlink it */
2199
0
  if(prev) prev->next = p->next;
2200
0
  else d->rrsets = p->next;
2201
  /* no memory recycling for zone deletions ... */
2202
0
}
2203
2204
void local_zones_del_data(struct local_zones* zones, 
2205
  uint8_t* name, size_t len, int labs, uint16_t dclass)
2206
0
{
2207
  /* find zone */
2208
0
  struct local_zone* z;
2209
0
  struct local_data* d;
2210
2211
  /* remove DS */
2212
0
  lock_rw_rdlock(&zones->lock);
2213
0
  z = local_zones_lookup(zones, name, len, labs, dclass, LDNS_RR_TYPE_DS,
2214
0
    1);
2215
0
  if(z) {
2216
0
    lock_rw_wrlock(&z->lock);
2217
0
    d = local_zone_find_data(z, name, len, labs);
2218
0
    if(d) {
2219
0
      del_local_rrset(d, LDNS_RR_TYPE_DS);
2220
0
      del_empty_term(z, d, name, len, labs);
2221
0
    }
2222
0
    lock_rw_unlock(&z->lock);
2223
0
  }
2224
0
  lock_rw_unlock(&zones->lock);
2225
2226
  /* remove other types */
2227
0
  lock_rw_rdlock(&zones->lock);
2228
0
  z = local_zones_lookup(zones, name, len, labs, dclass, 0, 1);
2229
0
  if(!z) {
2230
    /* no such zone, we're done */
2231
0
    lock_rw_unlock(&zones->lock);
2232
0
    return;
2233
0
  }
2234
0
  lock_rw_wrlock(&z->lock);
2235
0
  lock_rw_unlock(&zones->lock);
2236
2237
  /* find the domain */
2238
0
  d = local_zone_find_data(z, name, len, labs);
2239
0
  if(d) {
2240
    /* no memory recycling for zone deletions ... */
2241
0
    d->rrsets = NULL;
2242
    /* did we delete the soa record ? */
2243
0
    if(query_dname_compare(d->name, z->name) == 0) {
2244
0
      z->soa = NULL;
2245
0
      z->soa_negative = NULL;
2246
0
    }
2247
2248
    /* cleanup the empty nonterminals for this name */
2249
0
    del_empty_term(z, d, name, len, labs);
2250
0
  }
2251
2252
0
  lock_rw_unlock(&z->lock);
2253
0
}
2254
2255
/** Get memory usage for local_zone */
2256
static size_t
2257
local_zone_get_mem(struct local_zone* z)
2258
0
{
2259
0
  size_t m = sizeof(*z);
2260
0
  lock_rw_rdlock(&z->lock);
2261
0
  m += z->namelen + z->taglen + regional_get_mem(z->region);
2262
0
  lock_rw_unlock(&z->lock);
2263
0
  return m;
2264
0
}
2265
2266
size_t local_zones_get_mem(struct local_zones* zones)
2267
0
{
2268
0
  struct local_zone* z;
2269
0
  size_t m;
2270
0
  if(!zones) return 0;
2271
0
  m = sizeof(*zones);
2272
0
  lock_rw_rdlock(&zones->lock);
2273
0
  RBTREE_FOR(z, struct local_zone*, &zones->ztree) {
2274
0
    m += local_zone_get_mem(z);
2275
0
  }
2276
0
  lock_rw_unlock(&zones->lock);
2277
0
  return m;
2278
0
}
2279
2280
void local_zones_swap_tree(struct local_zones* zones, struct local_zones* data)
2281
0
{
2282
0
  rbtree_type oldtree = zones->ztree;
2283
0
  zones->ztree = data->ztree;
2284
0
  data->ztree = oldtree;
2285
0
}