Coverage Report

Created: 2025-07-11 06:08

/src/unbound/services/view.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * services/view.c - named views containing local zones authority service.
3
 *
4
 * Copyright (c) 2016, 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 named views that can hold local zone
40
 * authority service.
41
 */
42
#include "config.h"
43
#include "services/view.h"
44
#include "services/localzone.h"
45
#include "util/config_file.h"
46
#include "respip/respip.h"
47
48
int 
49
view_cmp(const void* v1, const void* v2)
50
0
{
51
0
  struct view* a = (struct view*)v1;
52
0
  struct view* b = (struct view*)v2;
53
  
54
0
  return strcmp(a->name, b->name);
55
0
}
56
57
struct views* 
58
views_create(void)
59
0
{
60
0
  struct views* v = (struct views*)calloc(1, 
61
0
    sizeof(*v));
62
0
  if(!v)
63
0
    return NULL;
64
0
  rbtree_init(&v->vtree, &view_cmp);
65
0
  lock_rw_init(&v->lock);
66
0
  lock_protect(&v->lock, &v->vtree, sizeof(v->vtree));
67
0
  return v;
68
0
}
69
70
void 
71
view_delete(struct view* v)
72
0
{
73
0
  if(!v)
74
0
    return;
75
0
  lock_rw_destroy(&v->lock);
76
0
  local_zones_delete(v->local_zones);
77
0
  respip_set_delete(v->respip_set);
78
0
  free(v->name);
79
0
  free(v);
80
0
}
81
82
static void
83
delviewnode(rbnode_type* n, void* ATTR_UNUSED(arg))
84
0
{
85
0
  struct view* v = (struct view*)n;
86
0
  view_delete(v);
87
0
}
88
89
void 
90
views_delete(struct views* v)
91
0
{
92
0
  if(!v)
93
0
    return;
94
0
  lock_rw_destroy(&v->lock);
95
0
  traverse_postorder(&v->vtree, delviewnode, NULL);
96
0
  free(v);
97
0
}
98
99
/** create a new view */
100
static struct view*
101
view_create(char* name)
102
0
{
103
0
  struct view* v = (struct view*)calloc(1, sizeof(*v));
104
0
  if(!v)
105
0
    return NULL;
106
0
  v->node.key = v;
107
0
  if(!(v->name = strdup(name))) {
108
0
    free(v);
109
0
    return NULL;
110
0
  }
111
0
  lock_rw_init(&v->lock);
112
0
  lock_protect(&v->lock, &v->name, sizeof(*v)-sizeof(rbnode_type));
113
0
  return v;
114
0
}
115
116
/** enter a new view returns with WRlock */
117
static struct view*
118
views_enter_view_name(struct views* vs, char* name)
119
0
{
120
0
  struct view* v = view_create(name);
121
0
  if(!v) {
122
0
    log_err("out of memory");
123
0
    return NULL;
124
0
  }
125
126
  /* add to rbtree */
127
0
  lock_rw_wrlock(&vs->lock);
128
0
  lock_rw_wrlock(&v->lock);
129
0
  if(!rbtree_insert(&vs->vtree, &v->node)) {
130
0
    log_warn("duplicate view: %s", name);
131
0
    lock_rw_unlock(&v->lock);
132
0
    view_delete(v);
133
0
    lock_rw_unlock(&vs->lock);
134
0
    return NULL;
135
0
  }
136
0
  lock_rw_unlock(&vs->lock);
137
0
  return v;
138
0
}
139
140
int 
141
views_apply_cfg(struct views* vs, struct config_file* cfg)
142
0
{
143
0
  struct config_view* cv;
144
0
  struct view* v;
145
0
  struct config_file lz_cfg;
146
  /* Check existence of name in first view (last in config). Rest of
147
   * views are already checked when parsing config. */
148
0
  if(cfg->views && !cfg->views->name) {
149
0
    log_err("view without a name");
150
0
    return 0;
151
0
  }
152
0
  for(cv = cfg->views; cv; cv = cv->next) {
153
    /* create and enter view */
154
0
    if(!(v = views_enter_view_name(vs, cv->name)))
155
0
      return 0;
156
0
    v->isfirst = cv->isfirst;
157
0
    if(cv->local_zones || cv->local_data) {
158
0
      if(!(v->local_zones = local_zones_create())){
159
0
        lock_rw_unlock(&v->lock);
160
0
        return 0;
161
0
      }
162
0
      memset(&lz_cfg, 0, sizeof(lz_cfg));
163
0
      lz_cfg.local_zones = cv->local_zones;
164
0
      lz_cfg.local_data = cv->local_data;
165
0
      lz_cfg.local_zones_nodefault =
166
0
        cv->local_zones_nodefault;
167
0
      if(v->isfirst) {
168
        /* Do not add defaults to view-specific
169
         * local-zone when global local zone will be
170
         * used. */
171
0
        struct config_strlist* nd;
172
0
        lz_cfg.local_zones_disable_default = 1;
173
        /* Add nodefault zones to list of zones to add,
174
         * so they will be used as if they are
175
         * configured as type transparent */
176
0
        for(nd = cv->local_zones_nodefault; nd;
177
0
          nd = nd->next) {
178
0
          char* nd_str, *nd_type;
179
0
          nd_str = strdup(nd->str);
180
0
          if(!nd_str) {
181
0
            log_err("out of memory");
182
0
            lock_rw_unlock(&v->lock);
183
0
            return 0;
184
0
          }
185
0
          nd_type = strdup("nodefault");
186
0
          if(!nd_type) {
187
0
            log_err("out of memory");
188
0
            free(nd_str);
189
0
            lock_rw_unlock(&v->lock);
190
0
            return 0;
191
0
          }
192
0
          if(!cfg_str2list_insert(
193
0
            &lz_cfg.local_zones, nd_str,
194
0
            nd_type)) {
195
0
            log_err("failed to insert "
196
0
              "default zones into "
197
0
              "local-zone list");
198
0
            lock_rw_unlock(&v->lock);
199
0
            return 0;
200
0
          }
201
0
        }
202
0
      }
203
0
      if(!local_zones_apply_cfg(v->local_zones, &lz_cfg)){
204
0
        lock_rw_unlock(&v->lock);
205
0
        return 0;
206
0
      }
207
      /* local_zones, local_zones_nodefault and local_data 
208
       * are free'd from config_view by local_zones_apply_cfg.
209
       * Set pointers to NULL. */
210
0
      cv->local_zones = NULL;
211
0
      cv->local_data = NULL;
212
0
      cv->local_zones_nodefault = NULL;
213
0
    }
214
0
    lock_rw_unlock(&v->lock);
215
0
  }
216
0
  return 1;
217
0
}
218
219
/** find a view by name */
220
struct view*
221
views_find_view(struct views* vs, const char* name, int write)
222
0
{
223
0
  struct view* v;
224
0
  struct view key;
225
0
  key.node.key = &v;
226
0
  key.name = (char *)name;
227
0
  lock_rw_rdlock(&vs->lock);
228
0
  if(!(v = (struct view*)rbtree_search(&vs->vtree, &key.node))) {
229
0
    lock_rw_unlock(&vs->lock);
230
0
    return 0;
231
0
  }
232
0
  if(write) {
233
0
    lock_rw_wrlock(&v->lock);
234
0
  } else {
235
0
    lock_rw_rdlock(&v->lock);
236
0
  }
237
0
  lock_rw_unlock(&vs->lock);
238
0
  return v;
239
0
}
240
241
void views_print(struct views* v)
242
0
{
243
  /* TODO implement print */
244
0
  (void)v;
245
0
}
246
247
size_t views_get_mem(struct views* vs)
248
0
{
249
0
  struct view* v;
250
0
  size_t m;
251
0
  if(!vs) return 0;
252
0
  m = sizeof(struct views);
253
0
  lock_rw_rdlock(&vs->lock);
254
0
  RBTREE_FOR(v, struct view*, &vs->vtree) {
255
0
    m += view_get_mem(v);
256
0
  }
257
0
  lock_rw_unlock(&vs->lock);
258
0
  return m;
259
0
}
260
261
size_t view_get_mem(struct view* v)
262
0
{
263
0
  size_t m = sizeof(*v);
264
0
  lock_rw_rdlock(&v->lock);
265
0
  m += getmem_str(v->name);
266
0
  m += local_zones_get_mem(v->local_zones);
267
0
  m += respip_set_get_mem(v->respip_set);
268
0
  lock_rw_unlock(&v->lock);
269
0
  return m;
270
0
}
271
272
void views_swap_tree(struct views* vs, struct views* data)
273
0
{
274
0
  rbnode_type* oldroot = vs->vtree.root;
275
0
  size_t oldcount = vs->vtree.count;
276
0
  vs->vtree.root = data->vtree.root;
277
0
  vs->vtree.count = data->vtree.count;
278
0
  data->vtree.root = oldroot;
279
0
  data->vtree.count = oldcount;
280
0
}