zonelist_export.c
Go to the documentation of this file.
/*
* Copyright (c) 2014 .SE (The Internet Infrastructure Foundation).
* Copyright (c) 2014 OpenDNSSEC AB (svb)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "config.h"
#include "log.h"
#include "str.h"
#include "clientpipe.h"
#include "db/zone_db.h"
#include "utils/kc_helper.h"
#include "keystate/zonelist_export.h"
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <limits.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
int zonelist_export(int sockfd, db_connection_t* connection, const char* filename, int comment) {
xmlDocPtr doc;
xmlNodePtr root = NULL;
xmlNodePtr node;
xmlNodePtr node2;
xmlNodePtr node3;
xmlNodePtr node4;
zone_list_db_t* zone_list;
const zone_db_t* zone;
policy_t* policy = NULL;
int cmp;
char path[PATH_MAX];
char* dirname, *dirlast;
if (!connection) {
return ZONELIST_EXPORT_ERR_ARGS;
}
if (!filename) {
return ZONELIST_EXPORT_ERR_ARGS;
}
if (access(filename, W_OK)) {
if (errno == ENOENT) {
if ((dirname = strdup(filename))) {
if ((dirlast = strrchr(dirname, '/'))) {
*dirlast = 0;
if (access(dirname, W_OK)) {
client_printf_err(sockfd, "Write access to directory denied: %s\n", strerror(errno));
free(dirname);
return ZONELIST_EXPORT_ERR_FILE;
}
}
free(dirname);
}
}
else {
client_printf_err(sockfd, "Write access to file denied: %s\n", strerror(errno));
return ZONELIST_EXPORT_ERR_FILE;
}
}
if (!(doc = xmlNewDoc((xmlChar*)"1.0"))
|| !(root = xmlNewNode(NULL, (xmlChar*)"ZoneList")))
{
client_printf_err(sockfd, "Unable to create XML elements, memory allocation error!\n");
if (doc) {
xmlFreeDoc(doc);
}
return ZONELIST_EXPORT_ERR_MEMORY;
}
if (comment) {
node = xmlNewComment((xmlChar*)
"\n\n"
"********* Important changes to zonelist.xml in 2.0 ***************\n"
"\n"
"In 2.0, the zonelist.xml file is no longer automatically updated when zones\n"
"are added or deleted via the command line by using the 'ods-enforcer zone add'\n"
"command. However, in 2.0 it is possible to force an update of the zonelist.xml\n"
"file by using the new 'xml' flag. This is in contrast to the behaviour in 1.4\n"
"where zonelist.xml was always updated, unless the 'no-xml' flag was used. \n"
"\n");
xmlNodeAddContent(node, (xmlChar*)
"As a result in 2.0 the contents of the enforcer database should be considered\n"
"the 'master' for the list of currently configured zones, not the zonelist.xml\n"
"file as the file can easily become out of sync with the database.\n"
"\n");
xmlNodeAddContent(node, (xmlChar*)
"The contents of the database can be listed using:\n"
" ods-enforcer zone list\n"
"and exported using the command\n"
" ods-enforcer zonelist export\n"
"The contents of the database can still be updated in bulk from the zonelist.xml\n"
"file by using the command:\n"
" ods-enforcer zonelist import (or ods-enforcer update zonelist)\n\n"
);
xmlAddChild(root, node);
}
xmlDocSetRootElement(doc, root);
if (!(zone_list = zone_list_db_new(connection))
|| zone_list_db_get(zone_list))
{
xmlFreeDoc(doc);
if (zone_list) {
zone_list_db_free(zone_list);
client_printf_err(sockfd, "Unable to get list of zones, database error!\n");
return ZONELIST_EXPORT_ERR_DATABASE;
}
else {
client_printf_err(sockfd, "Unable to get list of zones, memory allocation error!\n");
return ZONELIST_EXPORT_ERR_MEMORY;
}
}
while ((zone = zone_list_db_next(zone_list))) {
if (policy) {
/*
* If we already have a policy object; If policy_id compare fails
* or if they are not the same free the policy object to we will
* later retrieve the correct policy
*/
if (db_value_cmp(policy_id(policy), zone_db_policy_id(zone), &cmp)
|| cmp)
{
policy_free(policy);
policy = NULL;
}
}
if (!policy) {
if (!(policy = zone_db_get_policy(zone))) {
client_printf_err(sockfd, "Unable to get policy, database error!\n");
zone_list_db_free(zone_list);
xmlFreeDoc(doc);
return ZONELIST_EXPORT_ERR_DATABASE;
}
}
if (!(node = xmlNewChild(root, NULL, (xmlChar*)"Zone", NULL))
|| !xmlNewProp(node, (xmlChar*)"name", (xmlChar*)zone_db_name(zone))
|| !xmlNewChild(node, NULL, (xmlChar*)"Policy", (xmlChar*)policy_name(policy))
|| !xmlNewChild(node, NULL, (xmlChar*)"SignerConfiguration", (xmlChar*)zone_db_signconf_path(zone))
|| !(node2 = xmlNewChild(node, NULL, (xmlChar*)"Adapters", NULL))
|| !(node3 = xmlNewChild(node2, NULL, (xmlChar*)"Input", NULL))
|| !(node4 = xmlNewChild(node3, NULL, (xmlChar*)"Adapter", (xmlChar*)zone_db_input_adapter_uri(zone)))
|| !xmlNewProp(node4, (xmlChar*)"type", (xmlChar*)zone_db_input_adapter_type(zone))
|| !(node3 = xmlNewChild(node2, NULL, (xmlChar*)"Output", NULL))
|| !(node4 = xmlNewChild(node3, NULL, (xmlChar*)"Adapter", (xmlChar*)zone_db_output_adapter_uri(zone)))
|| !xmlNewProp(node4, (xmlChar*)"type", (xmlChar*)zone_db_output_adapter_type(zone)))
{
client_printf_err(sockfd, "Unable to create XML elements for zone %s!\n", zone_db_name(zone));
zone_list_db_free(zone_list);
xmlFreeDoc(doc);
return ZONELIST_EXPORT_ERR_XML;
}
}
zone_list_db_free(zone_list);
policy_free(policy);
if (snprintf(path, sizeof(path), "%s.new", filename) >= (int)sizeof(path)) {
client_printf_err(sockfd, "Unable to write zonelist, memory allocation error!\n");
xmlFreeDoc(doc);
return ZONELIST_EXPORT_ERR_MEMORY;
}
unlink(path);
if (xmlSaveFormatFileEnc(path, doc, "UTF-8", 1) == -1) {
client_printf_err(sockfd, "Unable to write zonelist, LibXML error!\n");
xmlFreeDoc(doc);
return ZONELIST_EXPORT_ERR_FILE;
}
xmlFreeDoc(doc);
if (check_zonelist(path, 0, NULL, 0)) {
client_printf_err(sockfd, "Unable to validate the exported zonelist XML!\n");
unlink(path);
return ZONELIST_EXPORT_ERR_XML;
}
if (rename(path, filename)) {
client_printf_err(sockfd, "Unable to write zonelist, rename failed!\n");
unlink(path);
return ZONELIST_EXPORT_ERR_FILE;
}
return ZONELIST_EXPORT_OK;
}
zone_db.h
policy_name
const char * policy_name(const policy_t *policy)
Definition: policy.c:813
zone_list_db_new
zone_list_db_t * zone_list_db_new(const db_connection_t *connection)
Definition: zone_db.c:1946
ZONELIST_EXPORT_ERR_ARGS
#define ZONELIST_EXPORT_ERR_ARGS
Definition: zonelist_export.h:41
zone_db_policy_id
const db_value_t * zone_db_policy_id(const zone_db_t *zone)
Definition: zone_db.c:736
ZONELIST_EXPORT_ERR_XML
#define ZONELIST_EXPORT_ERR_XML
Definition: zonelist_export.h:45
zonelist_export.h
check_zonelist
int check_zonelist(const char *zonelist, int verbose, char **policy_names, int policy_count)
Definition: kc_helper.c:1664
ZONELIST_EXPORT_ERR_DATABASE
#define ZONELIST_EXPORT_ERR_DATABASE
Definition: zonelist_export.h:49
zone_db_signconf_path
const char * zone_db_signconf_path(const zone_db_t *zone)
Definition: zone_db.c:798
ZONELIST_EXPORT_ERR_FILE
#define ZONELIST_EXPORT_ERR_FILE
Definition: zonelist_export.h:57
zone_db_output_adapter_type
const char * zone_db_output_adapter_type(const zone_db_t *zone)
Definition: zone_db.c:878
ZONELIST_EXPORT_OK
#define ZONELIST_EXPORT_OK
Definition: zonelist_export.h:37
kc_helper.h
zone_list_db_free
void zone_list_db_free(zone_list_db_t *zone_list)
Definition: zone_db.c:1989
zone_list_db_get
int zone_list_db_get(zone_list_db_t *zone_list)
Definition: zone_db.c:2363
db_value_cmp
int db_value_cmp(const db_value_t *value_a, const db_value_t *value_b, int *result)
Definition: db_value.c:102
zone_list_db_next
const zone_db_t * zone_list_db_next(zone_list_db_t *zone_list)
Definition: zone_db.c:2603
zonelist_export
int zonelist_export(int sockfd, db_connection_t *connection, const char *filename, int comment)
Definition: zonelist_export.c:47
policy_free
void policy_free(policy_t *policy)
Definition: policy.c:518
zone_db
Definition: zone_db.h:46
zone_db_input_adapter_uri
const char * zone_db_input_adapter_uri(const zone_db_t *zone)
Definition: zone_db.c:870
zone_db_name
const char * zone_db_name(const zone_db_t *zone)
Definition: zone_db.c:782
zone_list_db
Definition: zone_db.h:466
zone_db_get_policy
policy_t * zone_db_get_policy(const zone_db_t *zone)
Definition: zone_db.c:744
policy
Definition: policy.h:60
db_connection
Definition: db_connection.h:46
zone_db_output_adapter_uri
const char * zone_db_output_adapter_uri(const zone_db_t *zone)
Definition: zone_db.c:886
policy_id
const db_value_t * policy_id(const policy_t *policy)
Definition: policy.c:805
ZONELIST_EXPORT_ERR_MEMORY
#define ZONELIST_EXPORT_ERR_MEMORY
Definition: zonelist_export.h:53
zone_db_input_adapter_type
const char * zone_db_input_adapter_type(const zone_db_t *zone)
Definition: zone_db.c:862