OpenDNSSEC-enforcer 2.1.13
rollover_list_cmd.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 Surfnet
3 * Copyright (c) 2011 .SE (The Internet Infrastructure Foundation).
4 * Copyright (c) 2011 OpenDNSSEC AB (svb)
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30#include "config.h"
31#include <getopt.h>
32
33#include "db/zone_db.h"
34#include "daemon/engine.h"
35#include "cmdhandler.h"
37#include "file.h"
38#include "log.h"
39#include "str.h"
40#include "clientpipe.h"
41#include "longgetopt.h"
42
44
45static const char *module_str = "rollover_list_cmd";
46
53static char*
54map_keytime(const zone_db_t *zone, const key_data_t *key)
55{
56 time_t t = 0;
57 char ct[26];
58 struct tm srtm;
59
60 switch(key_data_ds_at_parent(key)) {
62 return strdup("waiting for ds-submit");
64 return strdup("waiting for ds-seen");
66 return strdup("waiting for ds-retract");
68 return strdup("waiting for ds-gone");
69 default: break;
70 }
71
72 switch (key_data_role(key)) {
73 case KEY_DATA_ROLE_KSK: t = (time_t)zone_db_next_ksk_roll(zone); break;
74 case KEY_DATA_ROLE_ZSK: t = (time_t)zone_db_next_zsk_roll(zone); break;
75 case KEY_DATA_ROLE_CSK: t = (time_t)zone_db_next_csk_roll(zone); break;
76 default: break;
77 }
78 if (!t) return strdup("No roll scheduled");
79
80 localtime_r(&t, &srtm);
81 strftime(ct, 26, "%Y-%m-%d %H:%M:%S", &srtm);
82 return strdup(ct);
83}
84
85static void
86print_zone(int sockfd, const char* fmt, const zone_db_t* zone)
87{
88 key_data_list_t *keylist;
89 const key_data_t *key;
90
91 keylist = zone_db_get_keys(zone);
92 while ((key = key_data_list_next(keylist))) {
93 char *tchange = map_keytime(zone, key);
94 client_printf(sockfd, fmt, zone_db_name(zone),
95 key_data_role_text(key), tchange);
96 free(tchange);
97 }
98 key_data_list_free(keylist);
99}
100
109static int
110perform_rollover_list(int sockfd, const char *listed_zone,
111 db_connection_t *dbconn)
112{
113 zone_list_db_t *zonelist = NULL;
114 zone_db_t *zone = NULL;
115 const zone_db_t *zone_walk = NULL;
116 const char* fmt = "%-31s %-8s %-30s\n";
117
118 if (listed_zone) {
119 zone = zone_db_new_get_by_name(dbconn, listed_zone);
120 } else {
121 zonelist = zone_list_db_new_get(dbconn);
122 }
123
124 if (listed_zone && !zone) {
125 ods_log_error("[%s] zone '%s' not found", module_str, listed_zone);
126 client_printf(sockfd, "zone '%s' not found\n", listed_zone);
127 return 1;
128 }
129
130 if (!zone && !zonelist) {
131 ods_log_error("[%s] error enumerating zones", module_str);
132 client_printf(sockfd, "error enumerating zones\n");
133 return 1;
134 }
135
136 client_printf(sockfd, "Keys:\n");
137 client_printf(sockfd, fmt, "Zone:", "Keytype:", "Rollover expected:");
138
139 if (zone) {
140 print_zone(sockfd, fmt, zone);
141 zone_db_free(zone);
142 return 0;
143 }
144
145 while ((zone_walk = zone_list_db_next(zonelist))) {
146 print_zone(sockfd, fmt, zone_walk);
147 }
148 zone_list_db_free(zonelist);
149 return 0;
150}
151
152static void
153usage(int sockfd)
154{
155 client_printf(sockfd,
156 "rollover list\n"
157 " [--zone <zone>] aka -z\n"
158 );
159}
160
161static void
162help(int sockfd)
163{
164 client_printf(sockfd,
165 "List the expected dates and times of upcoming rollovers. This can be used to get an idea of upcoming works.\n"
166 "\nOptions:\n"
167 "zone name of the zone\n\n");
168}
169
170static int
171run(cmdhandler_ctx_type* context, int argc, char* argv[])
172{
173 int sockfd = context->sockfd;
174 struct longgetopt optctx;
175 int long_index = 0, opt = 0;
176 const char *zone = NULL;
177 db_connection_t* dbconn = getconnectioncontext(context);
178
179 static struct option long_options[] = {
180 {"zone", required_argument, 0, 'z'},
181 {0, 0, 0, 0}
182 };
183
184 for(opt = longgetopt(argc, argv, "z:", long_options, &long_index, &optctx); opt != -1;
185 opt = longgetopt(argc, argv, NULL, long_options, &long_index, &optctx)) {
186 switch (opt) {
187 case 'z':
188 zone = optctx.optarg;
189 break;
190 default:
191 client_printf_err(sockfd, "unknown arguments\n");
192 ods_log_error("[%s] unknown arguments for rollover list command", module_str);
193 return -1;
194 }
195 }
196 return perform_rollover_list(sockfd, zone, dbconn);
197}
198
199struct cmd_func_block rollover_list_funcblock = {
200 "rollover list", &usage, &help, NULL, NULL, &run, NULL
201};
db_connection_t * getconnectioncontext(cmdhandler_ctx_type *context)
const char * key_data_role_text(const key_data_t *key_data)
Definition: key_data.c:711
void key_data_list_free(key_data_list_t *key_data_list)
Definition: key_data.c:1694
const key_data_t * key_data_list_next(key_data_list_t *key_data_list)
Definition: key_data.c:2359
key_data_role
Definition: key_data.h:40
@ KEY_DATA_ROLE_ZSK
Definition: key_data.h:43
@ KEY_DATA_ROLE_KSK
Definition: key_data.h:42
@ KEY_DATA_ROLE_CSK
Definition: key_data.h:44
key_data_ds_at_parent
Definition: key_data.h:50
@ KEY_DATA_DS_AT_PARENT_SUBMITTED
Definition: key_data.h:54
@ KEY_DATA_DS_AT_PARENT_RETRACT
Definition: key_data.h:56
@ KEY_DATA_DS_AT_PARENT_SUBMIT
Definition: key_data.h:53
@ KEY_DATA_DS_AT_PARENT_RETRACTED
Definition: key_data.h:57
struct cmd_func_block rollover_list_funcblock
zone_list_db_t * zone_list_db_new_get(const db_connection_t *connection)
Definition: zone_db.c:2402
void zone_db_free(zone_db_t *zone)
Definition: zone_db.c:325
const char * zone_db_name(const zone_db_t *zone)
Definition: zone_db.c:782
unsigned int zone_db_next_ksk_roll(const zone_db_t *zone)
Definition: zone_db.c:894
unsigned int zone_db_next_csk_roll(const zone_db_t *zone)
Definition: zone_db.c:910
unsigned int zone_db_next_zsk_roll(const zone_db_t *zone)
Definition: zone_db.c:902
zone_db_t * zone_db_new_get_by_name(const db_connection_t *connection, const char *name)
Definition: zone_db.c:1569
const zone_db_t * zone_list_db_next(zone_list_db_t *zone_list)
Definition: zone_db.c:2603
void zone_list_db_free(zone_list_db_t *zone_list)
Definition: zone_db.c:1989
key_data_list_t * zone_db_get_keys(const zone_db_t *zone)
Definition: zone_db_ext.c:56