]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/unbound/services/view.c
Upgrade Unbound to 1.6.0. More to follow.
[FreeBSD/FreeBSD.git] / contrib / unbound / services / view.c
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 {
50         struct view* a = (struct view*)v1;
51         struct view* b = (struct view*)v2;
52         
53         return strcmp(a->name, b->name);
54 }
55
56 struct views* 
57 views_create(void)
58 {
59         struct views* v = (struct views*)calloc(1, 
60                 sizeof(*v));
61         if(!v)
62                 return NULL;
63         rbtree_init(&v->vtree, &view_cmp);
64         lock_rw_init(&v->lock);
65         lock_protect(&v->lock, &v->vtree, sizeof(v->vtree));
66         return v;
67 }
68
69 void 
70 view_delete(struct view* v)
71 {
72         if(!v)
73                 return;
74         lock_rw_destroy(&v->lock);
75         local_zones_delete(v->local_zones);
76         free(v->name);
77         free(v);
78 }
79
80 static void
81 delviewnode(rbnode_t* n, void* ATTR_UNUSED(arg))
82 {
83         struct view* v = (struct view*)n;
84         view_delete(v);
85 }
86
87 void 
88 views_delete(struct views* v)
89 {
90         if(!v)
91                 return;
92         lock_rw_destroy(&v->lock);
93         traverse_postorder(&v->vtree, delviewnode, NULL);
94         free(v);
95 }
96
97 /** create a new view */
98 static struct view*
99 view_create(char* name)
100 {
101         struct view* v = (struct view*)calloc(1, sizeof(*v));
102         if(!v)
103                 return NULL;
104         v->node.key = v;
105         if(!(v->name = strdup(name))) {
106                 free(v);
107                 return NULL;
108         }
109         lock_rw_init(&v->lock);
110         lock_protect(&v->lock, &v->name, sizeof(*v)-sizeof(rbnode_t));
111         return v;
112 }
113
114 /** enter a new view returns with WRlock */
115 static struct view*
116 views_enter_view_name(struct views* vs, char* name)
117 {
118         struct view* v = view_create(name);
119         if(!v) {
120                 log_err("out of memory");
121                 return NULL;
122         }
123
124         /* add to rbtree */
125         lock_rw_wrlock(&vs->lock);
126         lock_rw_wrlock(&v->lock);
127         if(!rbtree_insert(&vs->vtree, &v->node)) {
128                 log_warn("duplicate view: %s", name);
129                 lock_rw_unlock(&v->lock);
130                 view_delete(v);
131                 lock_rw_unlock(&vs->lock);
132                 return NULL;
133         }
134         lock_rw_unlock(&vs->lock);
135         return v;
136 }
137
138 int 
139 views_apply_cfg(struct views* vs, struct config_file* cfg)
140 {
141         struct config_view* cv;
142         struct view* v;
143         struct config_file lz_cfg;
144         /* Check existence of name in first view (last in config). Rest of
145          * views are already checked when parsing config. */
146         if(cfg->views && !cfg->views->name) {
147                 log_err("view without a name");
148                 return 0;
149         }
150         for(cv = cfg->views; cv; cv = cv->next) {
151                 /* create and enter view */
152                 if(!(v = views_enter_view_name(vs, cv->name)))
153                         return 0;
154                 v->isfirst = cv->isfirst;
155                 if(cv->local_zones || cv->local_data) {
156                         if(!(v->local_zones = local_zones_create())){
157                                 lock_rw_unlock(&v->lock);
158                                 return 0;
159                         }
160                         memset(&lz_cfg, 0, sizeof(lz_cfg));
161                         lz_cfg.local_zones = cv->local_zones;
162                         lz_cfg.local_data = cv->local_data;
163                         lz_cfg.local_zones_nodefault =
164                                 cv->local_zones_nodefault;
165                         if(!local_zones_apply_cfg(v->local_zones, &lz_cfg)){
166                                 lock_rw_unlock(&v->lock);
167                                 return 0;
168                         }
169                         /* local_zones, local_zones_nodefault and local_data 
170                          * are free'd from config_view by local_zones_apply_cfg.
171                          * Set pointers to NULL. */
172                         cv->local_zones = NULL;
173                         cv->local_data = NULL;
174                         cv->local_zones_nodefault = NULL;
175                 }
176                 lock_rw_unlock(&v->lock);
177         }
178         return 1;
179 }
180
181 /** find a view by name */
182 struct view*
183 views_find_view(struct views* vs, const char* name, int write)
184 {
185         struct view* v;
186         struct view key;
187         key.node.key = &v;
188         key.name = (char *)name;
189         lock_rw_rdlock(&vs->lock);
190         if(!(v = (struct view*)rbtree_search(&vs->vtree, &key.node))) {
191                 lock_rw_unlock(&vs->lock);
192                 return 0;
193         }
194         if(write) {
195                 lock_rw_wrlock(&v->lock);
196         } else {
197                 lock_rw_rdlock(&v->lock);
198         }
199         lock_rw_unlock(&vs->lock);
200         return v;
201 }
202
203 void views_print(struct views* v)
204 {
205         /* TODO implement print */
206         (void)v;
207 }