OpenDNSSEC-enforcer 2.1.13
zone_del_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
31#include "cmdhandler.h"
33#include "daemon/engine.h"
34#include "file.h"
35#include "log.h"
36#include "str.h"
37#include "clientpipe.h"
38#include "longgetopt.h"
39#include "db/zone_db.h"
43
45
46#include <limits.h>
47#include <getopt.h>
48
49static const char *module_str = "zone_del_cmd";
50
51static void
52usage(int sockfd)
53{
54 client_printf(sockfd,
55 "zone delete\n"
56 " --zone <zone> | --all aka -z | -a \n"
57 " [--xml] aka -u \n"
58 );
59}
60
61static void
62help(int sockfd)
63{
64 client_printf(sockfd,
65 "Delete one zone or all of them from the enforcer database.\n"
66 "\nOptions:\n"
67 "zone|all name of the zone or all zones\n"
68 "xml update zonelist.xml and remove the contents for the deleted zone\n\n"
69 );
70}
71
72static int delete_key_data(zone_db_t* zone, db_connection_t *dbconn, int sockfd) {
73 int successful;
78
79 /*
80 * Get key data for the zone and for each key data get the key state
81 * and try to delete all key state then the key data
82 */
84 client_printf_err(sockfd, "Unable to get key data for zone %s from database!\n", zone_db_name(zone));
85 return 0;
86 }
87 successful = 1;
90 client_printf_err(sockfd, "Unable to get key states for key data %s of zone %s from database!\n", key_data_role_text(key_data), zone_db_name(zone));
91 successful = 0;
92 continue;
93 }
94
97 client_printf_err(sockfd, "Unable to delete key state %s for key data %s of zone %s from database!\n", key_state_type_text(key_state), key_data_role_text(key_data), zone_db_name(zone));
98 successful = 0;
99 continue;
100 }
101 }
103
105 client_printf_err(sockfd, "Unable to delete key data %s of zone %s from database!\n", key_data_role_text(key_data), zone_db_name(zone));
106 successful = 0;
107 continue;
108 }
109
111 client_printf_err(sockfd, "Unable to release HSM key for key data %s of zone %s from database!\n", key_data_role_text(key_data), zone_db_name(zone));
112 successful = 0;
113 continue;
114 }
115 }
117
118 return successful;
119}
120
121static int
122run(cmdhandler_ctx_type* context, int argc, char* argv[])
123{
124 int sockfd = context->sockfd;
125 struct longgetopt optctx;
126 const char *zone_name2 = NULL;
127 int all = 0;
128 int write_xml = 0;
129 int long_index = 0, opt = 0;
130 zone_list_db_t* zone_list;
131 zone_db_t* zone;
132 int ret = 0;
133 char path[PATH_MAX];
134 char *signconf_del = NULL;
135 db_connection_t* dbconn = getconnectioncontext(context);;
136 engine_type* engine = getglobalcontext(context);
137
138 static struct option long_options[] = {
139 {"zone", required_argument, 0, 'z'},
140 {"all", no_argument, 0, 'a'},
141 {"xml", no_argument, 0, 'u'},
142 {0, 0, 0, 0}
143 };
144
145 for(opt = longgetopt(argc, argv, "z:au", long_options, &long_index, &optctx); opt != -1;
146 opt = longgetopt(argc, argv, NULL, long_options, &long_index, &optctx)) {
147 switch (opt) {
148 case 'z':
149 zone_name2 = optctx.optarg;
150 break;
151 case 'a':
152 all = 1;
153 break;
154 case 'u':
155 write_xml = 1;
156 break;
157 default:
158 client_printf_err(sockfd, "unknown arguments\n");
159 ods_log_error("[%s] unknown arguments for zone delete command", module_str);
160 return -1;
161 }
162 }
163
164 if (zone_name2 && !all) {
165 if (!(zone = zone_db_new_get_by_name(dbconn, zone_name2))) {
166 client_printf_err(sockfd, "Unable to delete zone, zone %s not found!\n", zone_name2);
167 return 1;
168 }
169
170 if (!delete_key_data(zone, dbconn, sockfd)) {
171 zone_db_free(zone);
172 return 1;
173 }
174 if (zone_db_delete(zone)) {
175 client_printf_err(sockfd, "Unable to delete zone %s from database!\n", zone_name2);
176 zone_db_free(zone);
177 return 1;
178 }
179 signconf_del = (char*) calloc(strlen(zone_db_signconf_path(zone)) +
180 strlen(".ZONE_DELETED") + 1, sizeof(char));
181 if (!signconf_del) {
182 ods_log_error("[%s] malloc failed", module_str);
183 zone_db_free(zone);
184 return 1;
185 }
186 strncpy(signconf_del, zone_db_signconf_path(zone), strlen(zone_db_signconf_path(zone)));
187 strncat(signconf_del, ".ZONE_DELETED", strlen(".ZONE_DELETED"));
188 rename(zone_db_signconf_path(zone), signconf_del);
189 free(signconf_del);
190 signconf_del = NULL;
191
192 /* Delete all 'zone' related tasks */
193 schedule_purge_owner(engine->taskq, TASK_CLASS_ENFORCER, zone_name2);
194
195 ods_log_info("[%s] zone %s deleted", module_str, zone_name2);
196 client_printf(sockfd, "Deleted zone %s successfully\n", zone_name2);
197 } else if (!zone_name2 && all) {
198 if (!(zone_list = zone_list_db_new_get(dbconn))) {
199 client_printf_err(sockfd, "Unable to get list of zones from database!\n");
200 return 1;
201 }
202 for (zone = zone_list_db_get_next(zone_list); zone; zone_db_free(zone), zone = zone_list_db_get_next(zone_list)) {
203 if (!delete_key_data(zone, dbconn, sockfd)) {
204 continue;
205 }
206 if (zone_db_delete(zone)) {
207 client_printf_err(sockfd, "Unable to delete zone %s from database!\n", zone_db_name(zone));
208 continue;
209 }
210
211 signconf_del = (char*) calloc(strlen(zone_db_signconf_path(zone)) +
212 strlen(".ZONE_DELETED") + 1, sizeof(char));
213 if (!signconf_del) {
214 ods_log_error("[%s] malloc failed", module_str);
215 zone_db_free(zone);
216 zone_list_db_free(zone_list);
217 return 1;
218 }
219 strncpy(signconf_del, zone_db_signconf_path(zone), strlen(zone_db_signconf_path(zone)));
220 strncat(signconf_del, ".ZONE_DELETED", strlen(".ZONE_DELETED"));
221 rename(zone_db_signconf_path(zone), signconf_del);
222 free(signconf_del);
223 signconf_del = NULL;
224
225 /* Delete all 'zone' related tasks */
226 schedule_purge_owner(engine->taskq, TASK_CLASS_ENFORCER, zone_db_name(zone));
227
228 ods_log_info("[%s] zone %s deleted", module_str, zone_db_name(zone));
229 client_printf(sockfd, "Deleted zone %s successfully\n", zone_db_name(zone));
230 }
231 zone_list_db_free(zone_list);
232 zone = NULL;
233 client_printf(sockfd, "All zones deleted successfully\n");
234 } else {
235 client_printf_err(sockfd, "expected either --zone <zone> or --all\n");
236 return -1;
237 }
238
239 if (write_xml) {
240 if (zone) {
241 if (zonelist_update_delete(sockfd, engine->config->zonelist_filename, zone, 1) != ZONELIST_UPDATE_OK) {
242 ods_log_error("[%s] zonelist %s updated failed", module_str, engine->config->zonelist_filename);
243 client_printf_err(sockfd, "Zonelist %s update failed!\n", engine->config->zonelist_filename);
244 ret = 1;
245 } else {
246 ods_log_info("[%s] zonelist %s updated successfully", module_str, engine->config->zonelist_filename);
247 client_printf(sockfd, "Zonelist %s updated successfully\n", engine->config->zonelist_filename);
248 }
249 } else {
250 if (zonelist_export(sockfd, dbconn, engine->config->zonelist_filename, 1) != ZONELIST_EXPORT_OK) {
251 ods_log_error("[%s] zonelist exported to %s failed", module_str, engine->config->zonelist_filename);
252 client_printf_err(sockfd, "Exported zonelist to %s failed!\n", engine->config->zonelist_filename);
253 ret = 1;
254 } else {
255 ods_log_info("[%s] zonelist exported to %s successfully", module_str, engine->config->zonelist_filename);
256 client_printf(sockfd, "Exported zonelist to %s successfully\n", engine->config->zonelist_filename);
257 }
258 }
259 }
260
261 if (zone) {
262 if (snprintf(path, sizeof(path), "%s/%s", engine->config->working_dir, OPENDNSSEC_ENFORCER_ZONELIST) >= (int)sizeof(path)
263 || zonelist_update_delete(sockfd, path, zone, 0) != ZONELIST_UPDATE_OK)
264 {
265 ods_log_error("[%s] internal zonelist update failed", module_str);
266 client_printf_err(sockfd, "Unable to update the internal zonelist %s, updates will not reach the Signer!\n", path);
267 ret = 1;
268 } else {
269 ods_log_info("[%s] internal zonelist updated successfully", module_str);
270 }
271 } else {
272 if (snprintf(path, sizeof(path), "%s/%s", engine->config->working_dir, OPENDNSSEC_ENFORCER_ZONELIST) >= (int)sizeof(path)
273 || zonelist_export(sockfd, dbconn, path, 0) != ZONELIST_EXPORT_OK)
274 {
275 ods_log_error("[%s] internal zonelist update failed", module_str);
276 client_printf_err(sockfd, "Unable to update the internal zonelist %s, updates will not reach the Signer!\n", path);
277 ret = 1;
278 } else {
279 ods_log_info("[%s] internal zonelist updated successfully", module_str);
280 }
281 }
282
283 zone_db_free(zone);
284 return ret;
285}
286
287struct cmd_func_block zone_del_funcblock = {
288 "zone delete", &usage, &help, NULL, NULL, &run, NULL
289};
db_connection_t * getconnectioncontext(cmdhandler_ctx_type *context)
engine_type * getglobalcontext(cmdhandler_ctx_type *context)
int hsm_key_factory_release_key_id(const db_value_t *hsm_key_id, const db_connection_t *connection)
const db_value_t * key_data_id(const key_data_t *key_data)
Definition: key_data.c:553
int key_data_delete(key_data_t *key_data)
Definition: key_data.c:1587
void key_data_free(key_data_t *key_data)
Definition: key_data.c:304
const char * key_data_role_text(const key_data_t *key_data)
Definition: key_data.c:711
void key_data_list_free(key_data_list_t *key_data_list)
Definition: key_data.c:1694
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
key_data_t * key_data_list_get_next(key_data_list_t *key_data_list)
Definition: key_data.c:2425
const db_value_t * key_data_hsm_key_id(const key_data_t *key_data)
Definition: key_data.c:607
key_state_t * key_state_list_get_next(key_state_list_t *key_state_list)
Definition: key_state.c:1398
int key_state_delete(const key_state_t *key_state)
Definition: key_state.c:831
void key_state_free(key_state_t *key_state)
Definition: key_state.c:214
key_state_list_t * key_state_list_new_get_by_key_data_id(const db_connection_t *connection, const db_value_t *key_data_id)
Definition: key_state.c:1217
void key_state_list_free(key_state_list_t *key_state_list)
Definition: key_state.c:924
const char * key_state_type_text(const key_state_t *key_state)
Definition: key_state.c:353
schedule_type * taskq
Definition: engine.h:60
engineconfig_type * config
Definition: engine.h:48
const char * working_dir
Definition: cfg.h:64
const char * zonelist_filename
Definition: cfg.h:57
zone_list_db_t * zone_list_db_new_get(const db_connection_t *connection)
Definition: zone_db.c:2402
void zone_db_free(zone_db_t *zone)
Definition: zone_db.c:325
int zone_db_delete(zone_db_t *zone)
Definition: zone_db.c:1884
const char * zone_db_signconf_path(const zone_db_t *zone)
Definition: zone_db.c:798
const char * zone_db_name(const zone_db_t *zone)
Definition: zone_db.c:782
zone_db_t * zone_list_db_get_next(zone_list_db_t *zone_list)
Definition: zone_db.c:2669
const db_value_t * zone_db_id(const zone_db_t *zone)
Definition: zone_db.c:728
zone_db_t * zone_db_new_get_by_name(const db_connection_t *connection, const char *name)
Definition: zone_db.c:1569
void zone_list_db_free(zone_list_db_t *zone_list)
Definition: zone_db.c:1989
struct cmd_func_block zone_del_funcblock
Definition: zone_del_cmd.c:287
int zonelist_export(int sockfd, db_connection_t *connection, const char *filename, int comment)
#define ZONELIST_EXPORT_OK
int zonelist_update_delete(int sockfd, const char *filename, const zone_db_t *zone, int comment)
#define ZONELIST_UPDATE_OK