Coverage Report

Created: 2023-03-26 06:07

/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
47
int 
48
view_cmp(const void* v1, const void* v2)
49
0
{
50
0
  struct view* a = (struct view*)v1;
51
0
  struct view* b = (struct view*)v2;
52
  
53
0
  return strcmp(a->name, b->name);
54
0
}
55
56
struct views* 
57
views_create(void)
58
0
{
59
0
  struct views* v = (struct views*)calloc(1, 
60
0
    sizeof(*v));
61
0
  if(!v)
62
0
    return NULL;
63
0
  rbtree_init(&v->vtree, &view_cmp);
64
0
  lock_rw_init(&v->lock);
65
0
  lock_protect(&v->lock, &v->vtree, sizeof(v->vtree));
66
0
  return v;
67
0
}
68
69
/* \noop (ignore this comment for doxygen)
70
 * This prototype is defined in in respip.h, but we want to avoid
71
 * unnecessary dependencies */
72
void respip_set_delete(struct respip_set *set);
73
74
void 
75
view_delete(struct view* v)
76
0
{
77
0
  if(!v)
78
0
    return;
79
0
  lock_rw_destroy(&v->lock);
80
0
  local_zones_delete(v->local_zones);
81
0
  respip_set_delete(v->respip_set);
82
0
  free(v->name);
83
0
  free(v);
84
0
}
85
86
static void
87
delviewnode(rbnode_type* n, void* ATTR_UNUSED(arg))
88
0
{
89
0
  struct view* v = (struct view*)n;
90
0
  view_delete(v);
91
0
}
92
93
void 
94
views_delete(struct views* v)
95
0
{
96
0
  if(!v)
97
0
    return;
98
0
  lock_rw_destroy(&v->lock);
99
0
  traverse_postorder(&v->vtree, delviewnode, NULL);
100
0
  free(v);
101
0
}
102
103
/** create a new view */
104
static struct view*
105
view_create(char* name)
106
0
{
107
0
  struct view* v = (struct view*)calloc(1, sizeof(*v));
108
0
  if(!v)
109
0
    return NULL;
110
0
  v->node.key = v;
111
0
  if(!(v->name = strdup(name))) {
112
0
    free(v);
113
0
    return NULL;
114
0
  }
115
0
  lock_rw_init(&v->lock);
116
0
  lock_protect(&v->lock, &v->name, sizeof(*v)-sizeof(rbnode_type));
117
0
  return v;
118
0
}
119
120
/** enter a new view returns with WRlock */
121
static struct view*
122
views_enter_view_name(struct views* vs, char* name)
123
0
{
124
0
  struct view* v = view_create(name);
125
0
  if(!v) {
126
0
    log_err("out of memory");
127
0
    return NULL;
128
0
  }
129
130
  /* add to rbtree */
131
0
  lock_rw_wrlock(&vs->lock);
132
0
  lock_rw_wrlock(&v->lock);
133
0
  if(!rbtree_insert(&vs->vtree, &v->node)) {
134
0
    log_warn("duplicate view: %s", name);
135
0
    lock_rw_unlock(&v->lock);
136
0
    view_delete(v);
137
0
    lock_rw_unlock(&vs->lock);
138
0
    return NULL;
139
0
  }
140
0
  lock_rw_unlock(&vs->lock);
141
0
  return v;
142
0
}
143
144
int 
145
views_apply_cfg(struct views* vs, struct config_file* cfg)
146
0
{
147
0
  struct config_view* cv;
148
0
  struct view* v;
149
0
  struct config_file lz_cfg;
150
  /* Check existence of name in first view (last in config). Rest of
151
   * views are already checked when parsing config. */
152
0
  if(cfg->views && !cfg->views->name) {
153
0
    log_err("view without a name");
154
0
    return 0;
155
0
  }
156
0
  for(cv = cfg->views; cv; cv = cv->next) {
157
    /* create and enter view */
158
0
    if(!(v = views_enter_view_name(vs, cv->name)))
159
0
      return 0;
160
0
    v->isfirst = cv->isfirst;
161
0
    if(cv->local_zones || cv->local_data) {
162
0
      if(!(v->local_zones = local_zones_create())){
163
0
        lock_rw_unlock(&v->lock);
164
0
        return 0;
165
0
      }
166
0
      memset(&lz_cfg, 0, sizeof(lz_cfg));
167
0
      lz_cfg.local_zones = cv->local_zones;
168
0
      lz_cfg.local_data = cv->local_data;
169
0
      lz_cfg.local_zones_nodefault =
170
0
        cv->local_zones_nodefault;
171
0
      if(v->isfirst) {
172
        /* Do not add defaults to view-specific
173
         * local-zone when global local zone will be
174
         * used. */
175
0
        struct config_strlist* nd;
176
0
        lz_cfg.local_zones_disable_default = 1;
177
        /* Add nodefault zones to list of zones to add,
178
         * so they will be used as if they are
179
         * configured as type transparent */
180
0
        for(nd = cv->local_zones_nodefault; nd;
181
0
          nd = nd->next) {
182
0
          char* nd_str, *nd_type;
183
0
          nd_str = strdup(nd->str);
184
0
          if(!nd_str) {
185
0
            log_err("out of memory");
186
0
            lock_rw_unlock(&v->lock);
187
0
            return 0;
188
0
          }
189
0
          nd_type = strdup("nodefault");
190
0
          if(!nd_type) {
191
0
            log_err("out of memory");
192
0
            free(nd_str);
193
0
            lock_rw_unlock(&v->lock);
194
0
            return 0;
195
0
          }
196
0
          if(!cfg_str2list_insert(
197
0
            &lz_cfg.local_zones, nd_str,
198
0
            nd_type)) {
199
0
            log_err("failed to insert "
200
0
              "default zones into "
201
0
              "local-zone list");
202
0
            lock_rw_unlock(&v->lock);
203
0
            return 0;
204
0
          }
205
0
        }
206
0
      }
207
0
      if(!local_zones_apply_cfg(v->local_zones, &lz_cfg)){
208
0
        lock_rw_unlock(&v->lock);
209
0
        return 0;
210
0
      }
211
      /* local_zones, local_zones_nodefault and local_data 
212
       * are free'd from config_view by local_zones_apply_cfg.
213
       * Set pointers to NULL. */
214
0
      cv->local_zones = NULL;
215
0
      cv->local_data = NULL;
216
0
      cv->local_zones_nodefault = NULL;
217
0
    }
218
0
    lock_rw_unlock(&v->lock);
219
0
  }
220
0
  return 1;
221
0
}
222
223
/** find a view by name */
224
struct view*
225
views_find_view(struct views* vs, const char* name, int write)
226
0
{
227
0
  struct view* v;
228
0
  struct view key;
229
0
  key.node.key = &v;
230
0
  key.name = (char *)name;
231
0
  lock_rw_rdlock(&vs->lock);
232
0
  if(!(v = (struct view*)rbtree_search(&vs->vtree, &key.node))) {
233
0
    lock_rw_unlock(&vs->lock);
234
0
    return 0;
235
0
  }
236
0
  if(write) {
237
0
    lock_rw_wrlock(&v->lock);
238
0
  } else {
239
0
    lock_rw_rdlock(&v->lock);
240
0
  }
241
0
  lock_rw_unlock(&vs->lock);
242
0
  return v;
243
0
}
244
245
void views_print(struct views* v)
246
0
{
247
  /* TODO implement print */
248
0
  (void)v;
249
0
}