OpenDNSSEC-enforcer  2.0.4
key_generate_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 "daemon/engine.h"
30 #include "daemon/cmdhandler.h"
31 #include "log.h"
32 #include "str.h"
33 #include "clientpipe.h"
34 #include "hsmkey/hsm_key_factory.h"
35 #include "db/policy.h"
36 #include "duration.h"
37 
39 
40 static const char *module_str = "key_generate_cmd";
41 
42 static void
43 usage(int sockfd)
44 {
45  client_printf(sockfd,
46  "key generate\n"
47  " --duration <duration> aka -d\n"
48  " --policy <policy> aka -p \n"
49  " --all aka -a\n"
50  );
51 }
52 
53 static void
54 help(int sockfd)
55 {
56  client_printf(sockfd,
57  "Pre-generate keys for all or a given policy, the duration to pre-generate for\n"
58  "can be specified or otherwise its taken from the conf.xml.\n"
59  "\nOptions:\n"
60  "duration duration to generate keys for\n"
61  "policy|all generate keys for a specified policy or for all of them \n\n");
62 }
63 
64 static int
65 handles(const char *cmd, ssize_t n)
66 {
67  return ods_check_command(cmd, n, key_generate_funcblock()->cmdname) ? 1 : 0;
68 }
69 
70 static int
71 run(int sockfd, engine_type* engine, const char *cmd, ssize_t n,
72  db_connection_t *dbconn)
73 {
74  char* buf;
75  const char* argv[6];
76  int argc;
77  const char* policy_name = NULL;
78  const char* duration_text = NULL;
79  time_t duration_time = 0;
80  duration_type* duration = NULL;
81  int all = 0;
83 
84  ods_log_debug("[%s] %s command", module_str, key_generate_funcblock()->cmdname);
85  cmd = ods_check_command(cmd, n, key_generate_funcblock()->cmdname);
86 
87  if (!(buf = strdup(cmd))) {
88  client_printf_err(sockfd, "memory error\n");
89  return -1;
90  }
91 
92  argc = ods_str_explode(buf, 6, argv);
93  if (argc > 6) {
94  client_printf_err(sockfd, "too many arguments\n");
95  free(buf);
96  return -1;
97  }
98 
99  ods_find_arg_and_param(&argc, argv, "duration", "d", &duration_text);
100  ods_find_arg_and_param(&argc, argv, "policy", "p", &policy_name);
101  all = ods_find_arg(&argc, argv, "all", "a") > -1 ? 1 : 0;
102 
103  if (argc) {
104  client_printf_err(sockfd, "unknown arguments\n");
105  free(buf);
106  return -1;
107  }
108 
109  if (duration_text) {
110  if (!(duration = duration_create_from_string(duration_text))
111  || !(duration_time = duration2time(duration)))
112  {
113  client_printf_err(sockfd, "Error parsing the specified duration!\n");
114  duration_cleanup(duration);
115  free(buf);
116  return 1;
117  }
118  duration_cleanup(duration);
119  }
120 
121  if (all) {
122  hsm_key_factory_schedule_generate_all(engine, duration_time);
123  }
124  else if (policy_name) {
125  if (!(policy = policy_new_get_by_name(dbconn, policy_name))) {
126  client_printf_err(sockfd, "Unable to find policy %s!\n", policy_name);
127  free(buf);
128  return 1;
129  }
130  hsm_key_factory_schedule_generate_policy(engine, policy, duration_time);
131  policy_free(policy);
132  }
133  else {
134  client_printf_err(sockfd, "Either --all or --policy needs to be given!\n");
135  free(buf);
136  return 1;
137  }
138 
139  client_printf(sockfd, "Key generation task scheduled.\n");
140  free(buf);
141  return 0;
142 }
143 
144 static struct cmd_func_block funcblock = {
145  "key generate", &usage, &help, &handles, &run
146 };
147 
148 struct cmd_func_block*
150 {
151  return &funcblock;
152 }
void(* help)(int sockfd)
Definition: cmdhandler.h:64
void ods_log_debug(const char *format,...)
Definition: log.c:41
policy_t * policy_new_get_by_name(const db_connection_t *connection, const char *name)
Definition: policy.c:2090
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
void(* usage)(int sockfd)
Definition: cmdhandler.h:61
struct cmd_func_block * key_generate_funcblock(void)
void policy_free(policy_t *policy)
Definition: policy.c:518
int hsm_key_factory_schedule_generate_all(engine_type *engine, time_t duration)
int hsm_key_factory_schedule_generate_policy(engine_type *engine, const policy_t *policy_orig, time_t duration)
Definition: policy.h:60
int(* handles)(const char *cmd, ssize_t n)
Definition: cmdhandler.h:67