OpenDNSSEC-enforcer 2.1.13
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"
36#include "longgetopt.h"
38#include "db/policy.h"
39#include "duration.h"
40
42
43static const char *module_str = "key_generate_cmd";
44
45static void
46usage(int sockfd)
47{
48 client_printf(sockfd,
49 "key generate\n"
50 " --duration <duration> aka -d\n"
51 " --policy <policy> aka -p \n"
52 " --all aka -a\n"
53 );
54}
55
56static void
57help(int sockfd)
58{
59 client_printf(sockfd,
60 "Pre-generate keys for all or a given policy, the duration to pre-generate for\n"
61 "can be specified or otherwise its taken from the conf.xml.\n"
62 "\nOptions:\n"
63 "duration duration to generate keys for\n"
64 "policy|all generate keys for a specified policy or for all of them \n\n");
65}
66
67static int
68run(cmdhandler_ctx_type* context, int argc, char* argv[])
69{
70 int sockfd = context->sockfd;
71 struct longgetopt optctx;
72 int 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] key generate command", module_str);
90
91 for(opt = longgetopt(argc, argv, "p:ad:", long_options, &long_index, &optctx); opt != -1;
92 opt = longgetopt(argc, argv, NULL, long_options, &long_index, &optctx)) {
93 switch (opt) {
94 case 'd':
95 duration_text = optctx.optarg;
96 break;
97 case 'p':
98 policy_name = optctx.optarg;
99 break;
100 case 'a':
101 all = 1;
102 break;
103 default:
104 client_printf_err(sockfd, "unknown arguments\n");
105 ods_log_error("[%s] unknown arguments for key generate command", module_str);
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 return 1;
116 }
117 duration_cleanup(duration);
118 }
119
120 if (all) {
121 hsm_key_factory_schedule_generate_all(engine, duration_time);
122 }
123 else if (policy_name) {
124 if (!(policy = policy_new_get_by_name(dbconn, policy_name))) {
125 client_printf_err(sockfd, "Unable to find policy %s!\n", policy_name);
126 return 1;
127 }
128 hsm_key_factory_schedule_generate_policy(engine, policy, duration_time);
130 }
131 else {
132 client_printf_err(sockfd, "Either --all or --policy needs to be given!\n");
133 return 1;
134 }
135
136 client_printf(sockfd, "Key generation task scheduled.\n");
137 return 0;
138}
139
140struct cmd_func_block key_generate_funcblock = {
141 "key generate", &usage, &help, NULL, NULL, &run, NULL
142};
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
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