OpenDNSSEC-enforcer  2.1.3
key_purge_cmd.c
Go to the documentation of this file.
1 #include "daemon/engine.h"
2 #include "cmdhandler.h"
4 #include "log.h"
5 #include "str.h"
6 #include "clientpipe.h"
8 #include "db/key_data.h"
9 #include "keystate/key_purge.h"
10 
11 #include "keystate/key_purge_cmd.h"
12 
13 #include <getopt.h>
14 
15 #define MAX_ARGS 4
16 
17 static const char *module_str = "key_purge_cmd";
18 
19 static void
20 usage(int sockfd)
21 {
22  client_printf(sockfd,
23  "key purge\n"
24  " --policy <policy> | --zone <zone> aka -p | -z\n");
25 }
26 
27 static void
28 help(int sockfd)
29 {
30  client_printf(sockfd,
31  "This command will remove keys from the database and HSM that "
32  "are dead. Use with caution.\n"
33  "\nOptions:\n"
34  "policy limit the purge to the given policy\n"
35  "zone limit the purge to the given zone\n\n"
36  );
37 }
38 
39 
47 static int
48 run(int sockfd, cmdhandler_ctx_type* context, const char *cmd)
49 {
50  zone_db_t *zone;
52  const char *zone_name = NULL;
53  const char *policy_name = NULL;
54  char *buf;
55  int argc = 0;
56  const char *argv[MAX_ARGS];
57  int long_index = 0, opt = 0;
58  int error = 0;
59  db_connection_t* dbconn = getconnectioncontext(context);
60 
61  static struct option long_options[] = {
62  {"zone", required_argument, 0, 'z'},
63  {"policy", required_argument, 0, 'p'},
64  {0, 0, 0, 0}
65  };
66 
67  if (!dbconn) return 1;
68 
69  ods_log_debug("[%s] %s command", module_str, key_purge_funcblock.cmdname);
70 
71  if (!(buf = strdup(cmd))) {
72  client_printf_err(sockfd, "memory error\n");
73  return -1;
74  }
75 
76  argc = ods_str_explode(buf, MAX_ARGS, argv);
77  if (argc == -1) {
78  client_printf_err(sockfd, "too many arguments\n");
79  ods_log_error("[%s] too many arguments for %s command",
80  module_str, key_purge_funcblock.cmdname);
81  free(buf);
82  return -1;
83  }
84 
85  optind = 0;
86  while ((opt = getopt_long(argc, (char* const*)argv, "z:p:", long_options, &long_index)) != -1) {
87  switch (opt) {
88  case 'z':
89  zone_name = optarg;
90  break;
91  case 'p':
92  policy_name = optarg;
93  break;
94  default:
95  client_printf_err(sockfd, "unknown arguments\n");
96  ods_log_error("[%s] unknown arguments for %s command",
97  module_str, key_purge_funcblock.cmdname);
98  free(buf);
99  return -1;
100  }
101  }
102 
103  if ((!zone_name && !policy_name) || (zone_name && policy_name)) {
104  ods_log_error("[%s] expected either --zone or --policy", module_str);
105  client_printf_err(sockfd, "expected either --zone or --policy \n");
106  free(buf);
107  return -1;
108  }
109 
110  if (zone_name) {
111  zone = zone_db_new(dbconn);
112  if (zone_db_get_by_name(zone, zone_name)) {
113  client_printf_err(sockfd, "unknown zone %s\n", zone_name);
114  zone_db_free(zone);
115  zone = NULL;
116  free(buf);
117  return -1;
118  }
119  error = removeDeadKeysNow(sockfd, dbconn, NULL, zone);
120  zone_db_free(zone);
121  zone = NULL;
122  free(buf);
123  return error;
124  }
125 
126  /* have policy_name since it is mutualy exlusive with zone_name */
127  policy = policy_new(dbconn);
128  if (policy_get_by_name(policy, policy_name)){
129  policy_free(policy);
130  policy = NULL;
131  free(buf);
132  client_printf_err(sockfd, "unknown policy %s\n", policy_name);
133  return -1;
134  }
135  error = removeDeadKeysNow(sockfd, dbconn, policy, NULL);
136  policy_free(policy);
137  policy = NULL;
138  free(buf);
139  return error;
140 }
141 
142 struct cmd_func_block key_purge_funcblock = {
143  "key purge", &usage, &help, NULL, &run
144 };
void zone_db_free(zone_db_t *zone)
Definition: zone_db.c:325
struct cmd_func_block key_purge_funcblock
const char * policy_name(const policy_t *policy)
Definition: policy.c:813
db_connection_t * getconnectioncontext(cmdhandler_ctx_type *context)
#define MAX_ARGS
Definition: key_purge_cmd.c:15
void policy_free(policy_t *policy)
Definition: policy.c:518
policy_t * policy_new(const db_connection_t *connection)
Definition: policy.c:479
int removeDeadKeysNow(int sockfd, db_connection_t *dbconn, policy_t *policy, zone_db_t *rzone)
Definition: key_purge.c:40
int policy_get_by_name(policy_t *policy, const char *name)
Definition: policy.c:2040
int zone_db_get_by_name(zone_db_t *zone, const char *name)
Definition: zone_db.c:1519
Definition: policy.h:60
zone_db_t * zone_db_new(const db_connection_t *connection)
Definition: zone_db.c:287