OpenDNSSEC-enforcer  2.0.4
zone_ext.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Jerry Lundström <lundstrom.jerry@gmail.com>
3  * Copyright (c) 2014 .SE (The Internet Infrastructure Foundation).
4  * Copyright (c) 2014 OpenDNSSEC AB (svb)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #include "zone.h"
31 
32 #include "db_error.h"
33 #include "log.h"
34 #include "policy.h"
35 
36 #include <string.h>
37 
39  if (!zone) {
40  return NULL;
41  }
42  if (!zone->dbo) {
43  return NULL;
44  }
45  if (db_value_not_empty(&(zone->id))) {
46  return NULL;
47  }
48 
50  &(zone->id));
51 
52  /*
53  * TODO: associated
54  return key_data_list_new_copy(zone_key_data_list(zone));
55  */
56 }
57 
59  if (!zone) {
60  return NULL;
61  }
62  if (!zone->dbo) {
63  return NULL;
64  }
65  if (db_value_not_empty(&(zone->id))) {
66  return NULL;
67  }
68 
70  &(zone->id));
71 
72  /*
73  * TODO: associated
74  return key_dependency_list_new_copy(zone_key_dependency_list(zone));
75  */
76 }
77 
78 static int __xmlNode2zone(zone_t* zone, xmlNodePtr zone_node, int* updated) {
79  xmlNodePtr node;
80  xmlNodePtr node2;
81  xmlNodePtr node3;
82  xmlChar* xml_text = NULL;
83  int check_if_updated = 0;
84  int update_this = 1;
85  policy_t* policy = NULL;
86  int ret;
87 
88  if (!zone) {
89  return DB_ERROR_UNKNOWN;
90  }
91  if (!zone_node) {
92  return DB_ERROR_UNKNOWN;
93  }
94 
95  /*
96  * If updated is set we will check if the content is changed and set the
97  * integer pointed by updated to non-zero.
98  */
99  if (updated) {
100  *updated = 0;
101  check_if_updated = 1;
102  }
103 
104  if (!(xml_text = xmlGetProp(zone_node, (xmlChar*)"name"))) {
105  return DB_ERROR_UNKNOWN;
106  }
107  ods_log_deeebug("[zone_*_from_xml] zone %s", (char*)xml_text);
108  if (check_if_updated) {
109  update_this = 0;
110  if (!zone_name(zone)) {
111  *updated = 1;
112  update_this = 1;
113  }
114  else if (strcmp(zone_name(zone), (char*)xml_text)) {
115  *updated = 1;
116  update_this = 1;
117  }
118  }
119  if (update_this) {
120  if (zone_set_name(zone, (char*)xml_text)) {
121  if (xml_text) {
122  xmlFree(xml_text);
123  }
124  return DB_ERROR_UNKNOWN;
125  }
126  }
127  if (xml_text) {
128  xmlFree(xml_text);
129  xml_text = NULL;
130  }
131 
132  for (node = zone_node->children; node; node = node->next) {
133  if (node->type != XML_ELEMENT_NODE) {
134  continue;
135  }
136 
137  if (!strcmp((char*)node->name, "Policy")) {
138  if (!(xml_text = xmlNodeGetContent(node))) {
139  policy_free(policy);
140  return DB_ERROR_UNKNOWN;
141  }
142  if (policy) {
143  if (strcmp(policy_name(policy), (char*)xml_text)
144  && policy_get_by_name(policy, (char*)xml_text))
145  {
146  policy_free(policy);
147  if (xml_text) {
148  xmlFree(xml_text);
149  }
150  return DB_ERROR_UNKNOWN;
151  }
152  }
153  else {
154  if (!(policy = policy_new(db_object_connection(zone->dbo)))
155  || policy_get_by_name(policy, (char*)xml_text))
156  {
157  policy_free(policy);
158  if (xml_text) {
159  xmlFree(xml_text);
160  }
161  return DB_ERROR_UNKNOWN;
162  }
163  }
164  ods_log_deeebug("[zone_*_from_xml] policy %s", (char*)xml_text);
165  if (check_if_updated) {
166  update_this = 0;
167  if (db_value_cmp(zone_policy_id(zone), policy_id(policy), &ret)) {
168  policy_free(policy);
169  if (xml_text) {
170  xmlFree(xml_text);
171  }
172  return DB_ERROR_UNKNOWN;
173  }
174  if (ret) {
175  *updated = 1;
176  update_this = 1;
177  }
178  }
179  if (update_this) {
180  if (zone_set_policy_id(zone, policy_id(policy))) {
181  policy_free(policy);
182  if (xml_text) {
183  xmlFree(xml_text);
184  }
185  return DB_ERROR_UNKNOWN;
186  }
187  }
188  if (xml_text) {
189  xmlFree(xml_text);
190  xml_text = NULL;
191  }
192  }
193  else if (!strcmp((char*)node->name, "SignerConfiguration")) {
194  if (!(xml_text = xmlNodeGetContent(node))) {
195  policy_free(policy);
196  return DB_ERROR_UNKNOWN;
197  }
198  ods_log_deeebug("[zone_*_from_xml] signconf path %s", (char*)xml_text);
199  if (check_if_updated) {
200  update_this = 0;
201  if (!zone_signconf_path(zone)) {
202  *updated = 1;
203  update_this = 1;
204  }
205  else if (strcmp(zone_signconf_path(zone), (char*)xml_text)) {
206  *updated = 1;
207  update_this = 1;
208  }
209  }
210  if (update_this) {
211  if (zone_set_signconf_path(zone, (char*)xml_text)) {
212  if (xml_text) {
213  xmlFree(xml_text);
214  }
215  policy_free(policy);
216  return DB_ERROR_UNKNOWN;
217  }
218  }
219  if (xml_text) {
220  xmlFree(xml_text);
221  xml_text = NULL;
222  }
223  }
224  else if (!strcmp((char*)node->name, "Adapters")) {
225  for (node2 = node->children; node2; node2 = node2->next) {
226  if (node2->type != XML_ELEMENT_NODE) {
227  continue;
228  }
229 
230  if (!strcmp((char*)node2->name, "Input")) {
231  for (node3 = node2->children; node3; node3 = node3->next) {
232  if (node3->type != XML_ELEMENT_NODE) {
233  continue;
234  }
235 
236  if (!strcmp((char*)node3->name, "File")) {
237  ods_log_deeebug("[zone_*_from_xml] input adapter type File");
238  if (check_if_updated) {
239  update_this = 0;
240  if (!zone_input_adapter_type(zone)) {
241  *updated = 1;
242  update_this = 1;
243  }
244  else if (strcmp(zone_input_adapter_type(zone), "File")) {
245  *updated = 1;
246  update_this = 1;
247  }
248  }
249  if (update_this) {
250  if (zone_set_input_adapter_type(zone, "File")) {
251  if (xml_text) {
252  xmlFree(xml_text);
253  }
254  policy_free(policy);
255  return DB_ERROR_UNKNOWN;
256  }
257  }
258  if (xml_text) {
259  xmlFree(xml_text);
260  xml_text = NULL;
261  }
262 
263  if (!(xml_text = xmlNodeGetContent(node3))) {
264  policy_free(policy);
265  return DB_ERROR_UNKNOWN;
266  }
267  ods_log_deeebug("[zone_*_from_xml] input adapter uri %s", (char*)xml_text);
268  if (check_if_updated) {
269  update_this = 0;
270  if (!zone_input_adapter_uri(zone)) {
271  *updated = 1;
272  update_this = 1;
273  }
274  else if (strcmp(zone_input_adapter_uri(zone), (char*)xml_text)) {
275  *updated = 1;
276  update_this = 1;
277  }
278  }
279  if (update_this) {
280  if (zone_set_input_adapter_uri(zone, (char*)xml_text)) {
281  if (xml_text) {
282  xmlFree(xml_text);
283  }
284  policy_free(policy);
285  return DB_ERROR_UNKNOWN;
286  }
287  }
288  if (xml_text) {
289  xmlFree(xml_text);
290  xml_text = NULL;
291  }
292  }
293  else if (!strcmp((char*)node3->name, "Adapter")) {
294  if (!(xml_text = xmlGetProp(node3, (xmlChar*)"type"))) {
295  policy_free(policy);
296  return DB_ERROR_UNKNOWN;
297  }
298  ods_log_deeebug("[zone_*_from_xml] input adapter type %s", (char*)xml_text);
299  if (check_if_updated) {
300  update_this = 0;
301  if (!zone_input_adapter_type(zone)) {
302  *updated = 1;
303  update_this = 1;
304  }
305  else if (strcmp(zone_input_adapter_type(zone), (char*)xml_text)) {
306  *updated = 1;
307  update_this = 1;
308  }
309  }
310  if (update_this) {
311  if (zone_set_input_adapter_type(zone, (char*)xml_text)) {
312  if (xml_text) {
313  xmlFree(xml_text);
314  }
315  policy_free(policy);
316  return DB_ERROR_UNKNOWN;
317  }
318  }
319  if (xml_text) {
320  xmlFree(xml_text);
321  xml_text = NULL;
322  }
323 
324  if (!(xml_text = xmlNodeGetContent(node3))) {
325  policy_free(policy);
326  return DB_ERROR_UNKNOWN;
327  }
328  ods_log_deeebug("[zone_*_from_xml] input adapter uri %s", (char*)xml_text);
329  if (check_if_updated) {
330  update_this = 0;
331  if (!zone_input_adapter_uri(zone)) {
332  *updated = 1;
333  update_this = 1;
334  }
335  else if (strcmp(zone_input_adapter_uri(zone), (char*)xml_text)) {
336  *updated = 1;
337  update_this = 1;
338  }
339  }
340  if (update_this) {
341  if (zone_set_input_adapter_uri(zone, (char*)xml_text)) {
342  if (xml_text) {
343  xmlFree(xml_text);
344  }
345  policy_free(policy);
346  return DB_ERROR_UNKNOWN;
347  }
348  }
349  if (xml_text) {
350  xmlFree(xml_text);
351  xml_text = NULL;
352  }
353  }
354  else {
355  ods_log_deeebug("[zone_*_from_xml] unknown %s", (char*)node3->name);
356  policy_free(policy);
357  return DB_ERROR_UNKNOWN;
358  }
359  }
360  }
361  else if (!strcmp((char*)node2->name, "Output")) {
362  for (node3 = node2->children; node3; node3 = node3->next) {
363  if (node3->type != XML_ELEMENT_NODE) {
364  continue;
365  }
366 
367  if (!strcmp((char*)node3->name, "File")) {
368  ods_log_deeebug("[zone_*_from_xml] output adapter type File");
369  if (check_if_updated) {
370  update_this = 0;
371  if (!zone_output_adapter_type(zone)) {
372  *updated = 1;
373  update_this = 1;
374  }
375  else if (strcmp(zone_output_adapter_type(zone), "File")) {
376  *updated = 1;
377  update_this = 1;
378  }
379  }
380  if (update_this) {
381  if (zone_set_output_adapter_type(zone, "File")) {
382  if (xml_text) {
383  xmlFree(xml_text);
384  }
385  policy_free(policy);
386  return DB_ERROR_UNKNOWN;
387  }
388  }
389  if (xml_text) {
390  xmlFree(xml_text);
391  xml_text = NULL;
392  }
393 
394  if (!(xml_text = xmlNodeGetContent(node3))) {
395  policy_free(policy);
396  return DB_ERROR_UNKNOWN;
397  }
398  ods_log_deeebug("[zone_*_from_xml] output adapter uri %s", (char*)xml_text);
399  if (check_if_updated) {
400  update_this = 0;
401  if (!zone_output_adapter_uri(zone)) {
402  *updated = 1;
403  update_this = 1;
404  }
405  else if (strcmp(zone_output_adapter_uri(zone), (char*)xml_text)) {
406  *updated = 1;
407  update_this = 1;
408  }
409  }
410  if (update_this) {
411  if (zone_set_output_adapter_uri(zone, (char*)xml_text)) {
412  if (xml_text) {
413  xmlFree(xml_text);
414  }
415  policy_free(policy);
416  return DB_ERROR_UNKNOWN;
417  }
418  }
419  if (xml_text) {
420  xmlFree(xml_text);
421  xml_text = NULL;
422  }
423  }
424  else if (!strcmp((char*)node3->name, "Adapter")) {
425  if (!(xml_text = xmlGetProp(node3, (xmlChar*)"type"))) {
426  policy_free(policy);
427  return DB_ERROR_UNKNOWN;
428  }
429  ods_log_deeebug("[zone_*_from_xml] output adapter type %s", (char*)xml_text);
430  if (check_if_updated) {
431  update_this = 0;
432  if (!zone_output_adapter_type(zone)) {
433  *updated = 1;
434  update_this = 1;
435  }
436  else if (strcmp(zone_output_adapter_type(zone), (char*)xml_text)) {
437  *updated = 1;
438  update_this = 1;
439  }
440  }
441  if (update_this) {
442  if (zone_set_output_adapter_type(zone, (char*)xml_text)) {
443  if (xml_text) {
444  xmlFree(xml_text);
445  }
446  policy_free(policy);
447  return DB_ERROR_UNKNOWN;
448  }
449  }
450  if (xml_text) {
451  xmlFree(xml_text);
452  xml_text = NULL;
453  }
454 
455  if (!(xml_text = xmlNodeGetContent(node3))) {
456  policy_free(policy);
457  return DB_ERROR_UNKNOWN;
458  }
459  ods_log_deeebug("[zone_*_from_xml] output adapter uri %s", (char*)xml_text);
460  if (check_if_updated) {
461  update_this = 0;
462  if (!zone_output_adapter_uri(zone)) {
463  *updated = 1;
464  update_this = 1;
465  }
466  else if (strcmp(zone_output_adapter_uri(zone), (char*)xml_text)) {
467  *updated = 1;
468  update_this = 1;
469  }
470  }
471  if (update_this) {
472  if (zone_set_output_adapter_uri(zone, (char*)xml_text)) {
473  if (xml_text) {
474  xmlFree(xml_text);
475  }
476  policy_free(policy);
477  return DB_ERROR_UNKNOWN;
478  }
479  }
480  if (xml_text) {
481  xmlFree(xml_text);
482  xml_text = NULL;
483  }
484  }
485  else {
486  ods_log_deeebug("[zone_*_from_xml] unknown %s", (char*)node3->name);
487  policy_free(policy);
488  return DB_ERROR_UNKNOWN;
489  }
490  }
491  }
492  else {
493  ods_log_deeebug("[zone_*_from_xml] unknown %s", (char*)node2->name);
494  policy_free(policy);
495  return DB_ERROR_UNKNOWN;
496  }
497  }
498  }
499  else {
500  ods_log_deeebug("[zone_*_from_xml] unknown %s", (char*)node->name);
501  policy_free(policy);
502  return DB_ERROR_UNKNOWN;
503  }
504  }
505 
506  if (xml_text) {
507  xmlFree(xml_text);
508  xml_text = NULL;
509  }
510  policy_free(policy);
511  return DB_OK;
512 }
513 
514 int zone_create_from_xml(zone_t* zone, xmlNodePtr zone_node) {
515  if (!zone) {
516  return DB_ERROR_UNKNOWN;
517  }
518  if (!zone_node) {
519  return DB_ERROR_UNKNOWN;
520  }
521 
522  return __xmlNode2zone(zone, zone_node, NULL);
523 }
524 
525 int zone_update_from_xml(zone_t* zone, xmlNodePtr zone_node, int* updated) {
526  if (!zone) {
527  return DB_ERROR_UNKNOWN;
528  }
529  if (!zone_node) {
530  return DB_ERROR_UNKNOWN;
531  }
532  if (!updated) {
533  return DB_ERROR_UNKNOWN;
534  }
535 
536  return __xmlNode2zone(zone, zone_node, updated);
537 }
const char * zone_input_adapter_type(const zone_t *zone)
Definition: zone.c:862
const char * zone_output_adapter_uri(const zone_t *zone)
Definition: zone.c:886
const char * zone_signconf_path(const zone_t *zone)
Definition: zone.c:798
const db_value_t * zone_policy_id(const zone_t *zone)
Definition: zone.c:736
#define DB_ERROR_UNKNOWN
Definition: db_error.h:40
const char * policy_name(const policy_t *policy)
Definition: policy.c:813
int zone_set_signconf_path(zone_t *zone, const char *signconf_path_text)
Definition: zone.c:969
int zone_set_output_adapter_uri(zone_t *zone, const char *output_adapter_uri_text)
Definition: zone.c:1127
int zone_set_policy_id(zone_t *zone, const db_value_t *policy_id)
Definition: zone.c:918
const char * zone_output_adapter_type(const zone_t *zone)
Definition: zone.c:878
key_data_list_t * zone_get_keys(const zone_t *zone)
Definition: zone_ext.c:38
int zone_set_input_adapter_uri(zone_t *zone, const char *input_adapter_uri_text)
Definition: zone.c:1083
int db_value_not_empty(const db_value_t *value)
Definition: db_value.c:347
key_dependency_list_t * zone_get_key_dependencies(const zone_t *zone)
Definition: zone_ext.c:58
const db_connection_t * db_object_connection(const db_object_t *object)
Definition: db_object.c:320
db_value_t id
Definition: zone.h:48
int db_value_cmp(const db_value_t *value_a, const db_value_t *value_b, int *result)
Definition: db_value.c:102
void policy_free(policy_t *policy)
Definition: policy.c:518
int zone_set_name(zone_t *zone, const char *name_text)
Definition: zone.c:937
policy_t * policy_new(const db_connection_t *connection)
Definition: policy.c:479
db_object_t * dbo
Definition: zone.h:47
const char * zone_name(const zone_t *zone)
Definition: zone.c:782
int zone_set_output_adapter_type(zone_t *zone, const char *output_adapter_type_text)
Definition: zone.c:1105
int policy_get_by_name(policy_t *policy, const char *name)
Definition: policy.c:2040
int zone_update_from_xml(zone_t *zone, xmlNodePtr zone_node, int *updated)
Definition: zone_ext.c:525
key_data_list_t * key_data_list_new_get_by_zone_id(const db_connection_t *connection, const db_value_t *zone_id)
Definition: key_data.c:2244
#define DB_OK
Definition: db_error.h:36
void ods_log_deeebug(const char *format,...)
Definition: log.c:34
int zone_create_from_xml(zone_t *zone, xmlNodePtr zone_node)
Definition: zone_ext.c:514
Definition: policy.h:60
Definition: zone.h:46
const char * zone_input_adapter_uri(const zone_t *zone)
Definition: zone.c:870
const db_value_t * policy_id(const policy_t *policy)
Definition: policy.c:805
key_dependency_list_t * key_dependency_list_new_get_by_zone_id(const db_connection_t *connection, const db_value_t *zone_id)
int zone_set_input_adapter_type(zone_t *zone, const char *input_adapter_type_text)
Definition: zone.c:1061