OpenDNSSEC-enforcer  2.0.4
key_purge_cmd.c
Go to the documentation of this file.
1 #include "daemon/engine.h"
2 #include "daemon/cmdhandler.h"
3 #include "log.h"
4 #include "str.h"
5 #include "clientpipe.h"
7 #include "db/key_data.h"
8 #include "keystate/key_purge.h"
9 
10 #include "keystate/key_purge_cmd.h"
11 
12 #define MAX_ARGS 16
13 
14 static const char *module_str = "key_purge_cmd";
15 
16 static void
17 usage(int sockfd)
18 {
19  client_printf(sockfd,
20  "key purge\n"
21  " --policy <policy> | --zone <zone> aka -p | -z\n");
22 }
23 
24 static void
25 help(int sockfd)
26 {
27  client_printf(sockfd,
28  "This command will remove keys from the database and HSM that "
29  "are dead. Use with caution.\n"
30  "\nOptions:\n"
31  "policy limit the purge to the given policy\n"
32  "zone limit the purge to the given zone\n\n"
33  );
34 }
35 
36 static int
37 handles(const char *cmd, ssize_t n)
38 {
39  return ods_check_command(cmd, n, key_purge_funcblock()->cmdname) ? 1 : 0;
40 }
41 
42 
50 static int
51 run(int sockfd, engine_type* engine, const char *cmd, ssize_t n,
52  db_connection_t *dbconn)
53 {
54  zone_t *zone;
56  const char *zone_name = NULL;
57  const char *policy_name = NULL;
58  char *buf;
59  int argc;
60  const char *argv[4];
61  int error = 0;
62  (void)engine;
63 
64  if (!dbconn) return 1;
65 
66  ods_log_debug("[%s] %s command", module_str, key_purge_funcblock()->cmdname);
67  cmd = ods_check_command(cmd, n, key_purge_funcblock()->cmdname);
68 
69  if (!(buf = strdup(cmd))) {
70  client_printf_err(sockfd, "memory error\n");
71  return -1;
72  }
73 
74  argc = ods_str_explode(buf, MAX_ARGS, argv);
75 
76  ods_find_arg_and_param(&argc, argv, "zone", "z", &zone_name);
77  ods_find_arg_and_param(&argc, argv, "policy", "p", &policy_name);
78 
79 
80  if ((!zone_name && !policy_name) || (zone_name && policy_name)) {
81  ods_log_error("[%s] expected either --zone or --policy", module_str);
82  client_printf_err(sockfd, "expected either --zone or --policy \n");
83  free(buf);
84  return -1;
85  }
86 
87  if (argc) {
88  client_printf_err(sockfd, "unknown arguments\n");
89  free(buf);
90  return -1;
91  }
92 
93  if (zone_name) {
94  zone = zone_new(dbconn);
95  if (zone_get_by_name(zone, zone_name)) {
96  client_printf_err(sockfd, "unknown zone %s\n", zone_name);
97  zone_free(zone);
98  zone = NULL;
99  free(buf);
100  return -1;
101  }
102  error = removeDeadKeysNow(sockfd, dbconn, NULL, zone);
103  zone_free(zone);
104  zone = NULL;
105  free(buf);
106  return error;
107  }
108 
109  /* have policy_name since it is mutualy exlusive with zone_name */
110  policy = policy_new(dbconn);
111  if (policy_get_by_name(policy, policy_name)){
112  policy_free(policy);
113  policy = NULL;
114  free(buf);
115  client_printf_err(sockfd, "unknown policy %s\n", policy_name);
116  return -1;
117  }
118  error = removeDeadKeysNow(sockfd, dbconn, policy, NULL);
119  policy_free(policy);
120  policy = NULL;
121  free(buf);
122  return error;
123 }
124 
125 static struct cmd_func_block funcblock = {
126  "key purge", &usage, &help, &handles, &run
127 };
128 
129 struct cmd_func_block*
131 {
132  return &funcblock;
133 }
void(* help)(int sockfd)
Definition: cmdhandler.h:64
void ods_log_debug(const char *format,...)
Definition: log.c:41
const char * policy_name(const policy_t *policy)
Definition: policy.c:813
int(* run)(int sockfd, struct engine_struct *engine, const char *cmd, ssize_t n, db_connection_t *dbconn)
Definition: cmdhandler.h:79
const char * cmdname
Definition: cmdhandler.h:59
int zone_get_by_name(zone_t *zone, const char *name)
Definition: zone.c:1519
void ods_log_error(const char *format,...)
Definition: log.c:69
void zone_free(zone_t *zone)
Definition: zone.c:325
#define MAX_ARGS
Definition: key_purge_cmd.c:12
void(* usage)(int sockfd)
Definition: cmdhandler.h:61
void policy_free(policy_t *policy)
Definition: policy.c:518
zone_t * zone_new(const db_connection_t *connection)
Definition: zone.c:287
struct cmd_func_block * key_purge_funcblock(void)
policy_t * policy_new(const db_connection_t *connection)
Definition: policy.c:479
const char * zone_name(const zone_t *zone)
Definition: zone.c:782
int policy_get_by_name(policy_t *policy, const char *name)
Definition: policy.c:2040
Definition: policy.h:60
Definition: zone.h:46
int(* handles)(const char *cmd, ssize_t n)
Definition: cmdhandler.h:67
int removeDeadKeysNow(int sockfd, db_connection_t *dbconn, policy_t *policy, zone_t *rzone)
Definition: key_purge.c:40