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