OpenDNSSEC-enforcer 2.1.13
policy_purge_cmd.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 NLNet Labs. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
27#include "daemon/engine.h"
28#include "cmdhandler.h"
30#include "log.h"
31#include "str.h"
32#include "clientpipe.h"
33#include "longgetopt.h"
35#include "db/policy.h"
36
38
39static const char *module_str = "policy_purge_cmd";
40
41static void
42usage(int sockfd)
43{
44 client_printf(sockfd,
45 "policy purge\n"
46 );
47}
48
49static void
50help(int sockfd)
51{
52 client_printf(sockfd,
53 "This command will remove any policies from the database which have no\n"
54 "associated zones. Use with caution.\n\n"
55 );
56}
57
58static int
59run(cmdhandler_ctx_type* context, int argc, char* argv[])
60{
61 int sockfd = context->sockfd;
62 db_connection_t* dbconn = getconnectioncontext(context);
65 zone_list_db_t* zonelist;
66 const char* name;
67 size_t listsize;
68 int result = 0;
69
70 client_printf(sockfd, "Purging policies\n");
71
73 if (!policy_list) return 1;
74
76 name = policy_name(policy);
77 /*fetch zonelist from db, owned by policy*/
79 result = 1;
80 client_printf(sockfd, "Error fetching zones\n");
81 break;
82 }
83 zonelist = policy_zone_list(policy);
84 listsize = zone_list_db_size(zonelist);
85 if (listsize == 0) {
86 ods_log_info("[%s] No zones on policy %s; purging...", module_str, name);
87 client_printf(sockfd, "No zones on policy %s; purging...\n", name);
88 if (policy_delete(policy)) {
89 ods_log_crit("[%s] Error while purging policy from database", module_str);
90 client_printf(sockfd, "Error while updating database\n");
91 result++;
92 }
93 }
95 }
97 return result;
98}
99
100struct cmd_func_block policy_purge_funcblock = {
101 "policy purge", &usage, &help, NULL, NULL, &run, NULL
102};
db_connection_t * getconnectioncontext(cmdhandler_ctx_type *context)
zone_list_db_t * policy_zone_list(policy_t *policy)
Definition: policy.c:1093
void policy_list_free(policy_list_t *policy_list)
Definition: policy.c:2664
policy_list_t * policy_list_new_get(const db_connection_t *connection)
Definition: policy.c:3079
const char * policy_name(const policy_t *policy)
Definition: policy.c:813
int policy_retrieve_zone_list(policy_t *policy)
Definition: policy.c:1111
policy_t * policy_list_get_next(policy_list_t *policy_list)
Definition: policy.c:3280
int policy_delete(policy_t *policy)
Definition: policy.c:2571
void policy_free(policy_t *policy)
Definition: policy.c:518
struct cmd_func_block policy_purge_funcblock
Definition: policy.h:60
size_t zone_list_db_size(zone_list_db_t *zone_list)
Definition: zone_db.c:2705