OpenDNSSEC-enforcer 2.1.13
zonelist_import_cmd.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#include <limits.h>
31#include <getopt.h>
32
33#include "daemon/engine.h"
34#include "cmdhandler.h"
36#include "log.h"
37#include "str.h"
38#include "clientpipe.h"
39#include "longgetopt.h"
43
45
46static const char *module_str = "zonelist_import_cmd";
47
48static void
49usage(int sockfd)
50{
51 client_printf(sockfd,
52 "zonelist import\n"
53 " [--remove-missing-zones] aka -r\n"
54 /* We require the user to give an absolute path. The daemon
55 * and the client might not have the same working directory. */
56 " [--file <absolute path>] aka -f\n"
57 );
58}
59
60static void
61help(int sockfd)
62{
63 client_printf(sockfd,
64 "Import zones from zonelist.xml into enforcer database.\n"
65 "\nOptions:\n"
66 "remove-missing-zones Remove any zones from database not existed in zonelist file\n"
67 "file File to import, instead of zonelist file configured in conf.xml\n\n"
68 );
69}
70
71static int
72run(cmdhandler_ctx_type* context, int argc, char* argv[])
73{
74 int sockfd = context->sockfd;
75 struct longgetopt optctx;
76 char path[PATH_MAX];
77 int ret, remove_missing_zones = 0;
78 int long_index = 0, opt = 0;
79 const char* zonelist_path = NULL;
80 db_connection_t* dbconn = getconnectioncontext(context);
81 engine_type* engine = getglobalcontext(context);
82
83 static struct option long_options[] = {
84 {"remove-missing-zones", no_argument, 0, 'r'},
85 {"file", required_argument, 0, 'f'},
86 {0, 0, 0, 0}
87 };
88
89 if (!engine || !engine->config ||
90 !engine->config->zonelist_filename || !dbconn)
91 {
92 return 1;
93 }
94
95 for(opt = longgetopt(argc, argv, "rf:", long_options, &long_index, &optctx); opt != -1;
96 opt = longgetopt(argc, argv, NULL, long_options, &long_index, &optctx)) {
97 switch (opt) {
98 case 'r':
99 remove_missing_zones = 1;
100 break;
101 case 'f':
102 zonelist_path = optctx.optarg;
103 break;
104 default:
105 client_printf_err(sockfd, "unknown arguments\n");
106 ods_log_error("[%s] unknown arguments for zonelist import command", module_str);
107 return -1;
108 }
109 }
110
111 ret = zonelist_import(sockfd, engine, dbconn, remove_missing_zones, zonelist_path);
112 if (ret == ZONELIST_IMPORT_NO_CHANGE) {
113 return 0;
114 } else if (ret != ZONELIST_IMPORT_OK) {
115 return 1;
116 }
117
118 if (snprintf(path, sizeof(path), "%s/%s", engine->config->working_dir, OPENDNSSEC_ENFORCER_ZONELIST) >= (int)sizeof(path)
119 || zonelist_export(sockfd, dbconn, path, 0) != ZONELIST_EXPORT_OK)
120 {
121 ods_log_error("[%s] internal zonelist export failed", module_str);
122 client_printf_err(sockfd, "Unable to export the internal zonelist %s, updates will not reach the Signer!\n", path);
123 return 1;
124 } else {
125 ods_log_info("[%s] internal zonelist exported successfully", module_str);
126 }
127
128 /* YBS Only flush for zones with changed policy */
129 enforce_task_flush_all(engine, dbconn);
130
131 return 0;
132}
133
134struct cmd_func_block zonelist_import_funcblock = {
135 "zonelist import", &usage, &help, NULL, NULL, &run, NULL
136};
void enforce_task_flush_all(engine_type *engine, db_connection_t *dbconn)
db_connection_t * getconnectioncontext(cmdhandler_ctx_type *context)
engine_type * getglobalcontext(cmdhandler_ctx_type *context)
engineconfig_type * config
Definition engine.h:48
const char * working_dir
Definition cfg.h:64
const char * zonelist_filename
Definition cfg.h:57
int zonelist_export(int sockfd, db_connection_t *connection, const char *filename, int comment)
#define ZONELIST_EXPORT_OK
int zonelist_import(int sockfd, engine_type *engine, db_connection_t *dbconn, int do_delete, const char *zonelist_path)
#define ZONELIST_IMPORT_NO_CHANGE
#define ZONELIST_IMPORT_OK
struct cmd_func_block zonelist_import_funcblock