OpenDNSSEC-enforcer  2.1.9
zonelist_export.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 .SE (The Internet Infrastructure Foundation).
3  * Copyright (c) 2014 OpenDNSSEC AB (svb)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 #include "config.h"
30 
31 #include "log.h"
32 #include "str.h"
33 #include "clientpipe.h"
34 #include "db/zone_db.h"
35 #include "utils/kc_helper.h"
36 
38 
39 #include <libxml/parser.h>
40 #include <libxml/tree.h>
41 #include <limits.h>
42 #include <unistd.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <errno.h>
46 
47 int zonelist_export(int sockfd, db_connection_t* connection, const char* filename, int comment) {
48  xmlDocPtr doc;
49  xmlNodePtr root = NULL;
50  xmlNodePtr node;
51  xmlNodePtr node2;
52  xmlNodePtr node3;
53  xmlNodePtr node4;
54  zone_list_db_t* zone_list;
55  const zone_db_t* zone;
56  policy_t* policy = NULL;
57  int cmp;
58  char path[PATH_MAX];
59  char* dirname, *dirlast;
60 
61  if (!connection) {
63  }
64  if (!filename) {
66  }
67 
68  if (access(filename, W_OK)) {
69  if (errno == ENOENT) {
70  if ((dirname = strdup(filename))) {
71  if ((dirlast = strrchr(dirname, '/'))) {
72  *dirlast = 0;
73  if (access(dirname, W_OK)) {
74  client_printf_err(sockfd, "Write access to directory denied: %s\n", strerror(errno));
75  free(dirname);
77  }
78  }
79  free(dirname);
80  }
81  }
82  else {
83  client_printf_err(sockfd, "Write access to file denied: %s\n", strerror(errno));
85  }
86  }
87 
88  if (!(doc = xmlNewDoc((xmlChar*)"1.0"))
89  || !(root = xmlNewNode(NULL, (xmlChar*)"ZoneList")))
90  {
91  client_printf_err(sockfd, "Unable to create XML elements, memory allocation error!\n");
92  if (doc) {
93  xmlFreeDoc(doc);
94  }
96  }
97 
98  if (comment) {
99  node = xmlNewComment((xmlChar*)
100  "\n\n"
101  "********* Important changes to zonelist.xml in 2.0 ***************\n"
102  "\n"
103  "In 2.0, the zonelist.xml file is no longer automatically updated when zones\n"
104  "are added or deleted via the command line by using the 'ods-enforcer zone add'\n"
105  "command. However, in 2.0 it is possible to force an update of the zonelist.xml\n"
106  "file by using the new 'xml' flag. This is in contrast to the behaviour in 1.4\n"
107  "where zonelist.xml was always updated, unless the 'no-xml' flag was used. \n"
108  "\n");
109  xmlNodeAddContent(node, (xmlChar*)
110  "As a result in 2.0 the contents of the enforcer database should be considered\n"
111  "the 'master' for the list of currently configured zones, not the zonelist.xml\n"
112  "file as the file can easily become out of sync with the database.\n"
113  "\n");
114  xmlNodeAddContent(node, (xmlChar*)
115  "The contents of the database can be listed using:\n"
116  " ods-enforcer zone list\n"
117  "and exported using the command\n"
118  " ods-enforcer zonelist export\n"
119  "The contents of the database can still be updated in bulk from the zonelist.xml\n"
120  "file by using the command:\n"
121  " ods-enforcer zonelist import (or ods-enforcer update zonelist)\n\n"
122  );
123  xmlAddChild(root, node);
124  }
125  xmlDocSetRootElement(doc, root);
126 
127  if (!(zone_list = zone_list_db_new(connection))
128  || zone_list_db_get(zone_list))
129  {
130  xmlFreeDoc(doc);
131  if (zone_list) {
132  zone_list_db_free(zone_list);
133  client_printf_err(sockfd, "Unable to get list of zones, database error!\n");
135  }
136  else {
137  client_printf_err(sockfd, "Unable to get list of zones, memory allocation error!\n");
139  }
140  }
141 
142  while ((zone = zone_list_db_next(zone_list))) {
143  if (policy) {
144  /*
145  * If we already have a policy object; If policy_id compare fails
146  * or if they are not the same free the policy object to we will
147  * later retrieve the correct policy
148  */
150  || cmp)
151  {
153  policy = NULL;
154  }
155  }
156  if (!policy) {
157  if (!(policy = zone_db_get_policy(zone))) {
158  client_printf_err(sockfd, "Unable to get policy, database error!\n");
159  zone_list_db_free(zone_list);
160  xmlFreeDoc(doc);
162  }
163  }
164 
165  if (!(node = xmlNewChild(root, NULL, (xmlChar*)"Zone", NULL))
166  || !xmlNewProp(node, (xmlChar*)"name", (xmlChar*)zone_db_name(zone))
167  || !xmlNewChild(node, NULL, (xmlChar*)"Policy", (xmlChar*)policy_name(policy))
168  || !xmlNewChild(node, NULL, (xmlChar*)"SignerConfiguration", (xmlChar*)zone_db_signconf_path(zone))
169  || !(node2 = xmlNewChild(node, NULL, (xmlChar*)"Adapters", NULL))
170  || !(node3 = xmlNewChild(node2, NULL, (xmlChar*)"Input", NULL))
171  || !(node4 = xmlNewChild(node3, NULL, (xmlChar*)"Adapter", (xmlChar*)zone_db_input_adapter_uri(zone)))
172  || !xmlNewProp(node4, (xmlChar*)"type", (xmlChar*)zone_db_input_adapter_type(zone))
173  || !(node3 = xmlNewChild(node2, NULL, (xmlChar*)"Output", NULL))
174  || !(node4 = xmlNewChild(node3, NULL, (xmlChar*)"Adapter", (xmlChar*)zone_db_output_adapter_uri(zone)))
175  || !xmlNewProp(node4, (xmlChar*)"type", (xmlChar*)zone_db_output_adapter_type(zone)))
176  {
177  client_printf_err(sockfd, "Unable to create XML elements for zone %s!\n", zone_db_name(zone));
178  zone_list_db_free(zone_list);
179  xmlFreeDoc(doc);
181  }
182  }
183  zone_list_db_free(zone_list);
185 
186  if (snprintf(path, sizeof(path), "%s.new", filename) >= (int)sizeof(path)) {
187  client_printf_err(sockfd, "Unable to write zonelist, memory allocation error!\n");
188  xmlFreeDoc(doc);
190  }
191  unlink(path);
192  if (xmlSaveFormatFileEnc(path, doc, "UTF-8", 1) == -1) {
193  client_printf_err(sockfd, "Unable to write zonelist, LibXML error!\n");
194  xmlFreeDoc(doc);
196  }
197  xmlFreeDoc(doc);
198 
199  if (check_zonelist(path, 0, NULL, 0)) {
200  client_printf_err(sockfd, "Unable to validate the exported zonelist XML!\n");
201  unlink(path);
203  }
204 
205  if (rename(path, filename)) {
206  client_printf_err(sockfd, "Unable to write zonelist, rename failed!\n");
207  unlink(path);
209  }
210 
211  return ZONELIST_EXPORT_OK;
212 }
int db_value_cmp(const db_value_t *value_a, const db_value_t *value_b, int *result)
Definition: db_value.c:102
int check_zonelist(const char *zonelist, int verbose, char **policy_names, int policy_count)
Definition: kc_helper.c:1664
const char * policy_name(const policy_t *policy)
Definition: policy.c:813
const db_value_t * policy_id(const policy_t *policy)
Definition: policy.c:805
void policy_free(policy_t *policy)
Definition: policy.c:518
Definition: policy.h:60
const char * zone_db_output_adapter_uri(const zone_db_t *zone)
Definition: zone_db.c:886
const char * zone_db_name(const zone_db_t *zone)
Definition: zone_db.c:782
const char * zone_db_signconf_path(const zone_db_t *zone)
Definition: zone_db.c:798
policy_t * zone_db_get_policy(const zone_db_t *zone)
Definition: zone_db.c:744
const zone_db_t * zone_list_db_next(zone_list_db_t *zone_list)
Definition: zone_db.c:2603
const char * zone_db_output_adapter_type(const zone_db_t *zone)
Definition: zone_db.c:878
int zone_list_db_get(zone_list_db_t *zone_list)
Definition: zone_db.c:2363
const char * zone_db_input_adapter_type(const zone_db_t *zone)
Definition: zone_db.c:862
zone_list_db_t * zone_list_db_new(const db_connection_t *connection)
Definition: zone_db.c:1946
const char * zone_db_input_adapter_uri(const zone_db_t *zone)
Definition: zone_db.c:870
void zone_list_db_free(zone_list_db_t *zone_list)
Definition: zone_db.c:1989
const db_value_t * zone_db_policy_id(const zone_db_t *zone)
Definition: zone_db.c:736
int zonelist_export(int sockfd, db_connection_t *connection, const char *filename, int comment)
#define ZONELIST_EXPORT_ERR_DATABASE
#define ZONELIST_EXPORT_OK
#define ZONELIST_EXPORT_ERR_ARGS
#define ZONELIST_EXPORT_ERR_XML
#define ZONELIST_EXPORT_ERR_MEMORY
#define ZONELIST_EXPORT_ERR_FILE