OpenDNSSEC-enforcer  2.1.10
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 
43 
44 static const char *module_str = "rollover_list_cmd";
45 
52 static char*
53 map_keytime(const zone_db_t *zone, const key_data_t *key)
54 {
55  time_t t = 0;
56  char ct[26];
57  struct tm srtm;
58 
59  switch(key_data_ds_at_parent(key)) {
61  return strdup("waiting for ds-submit");
63  return strdup("waiting for ds-seen");
65  return strdup("waiting for ds-retract");
67  return strdup("waiting for ds-gone");
68  default: break;
69  }
70 
71  switch (key_data_role(key)) {
72  case KEY_DATA_ROLE_KSK: t = (time_t)zone_db_next_ksk_roll(zone); break;
73  case KEY_DATA_ROLE_ZSK: t = (time_t)zone_db_next_zsk_roll(zone); break;
74  case KEY_DATA_ROLE_CSK: t = (time_t)zone_db_next_csk_roll(zone); break;
75  default: break;
76  }
77  if (!t) return strdup("No roll scheduled");
78 
79  localtime_r(&t, &srtm);
80  strftime(ct, 26, "%Y-%m-%d %H:%M:%S", &srtm);
81  return strdup(ct);
82 }
83 
84 static void
85 print_zone(int sockfd, const char* fmt, const zone_db_t* zone)
86 {
87  key_data_list_t *keylist;
88  const key_data_t *key;
89 
90  keylist = zone_db_get_keys(zone);
91  while ((key = key_data_list_next(keylist))) {
92  char *tchange = map_keytime(zone, key);
93  client_printf(sockfd, fmt, zone_db_name(zone),
94  key_data_role_text(key), tchange);
95  free(tchange);
96  }
97  key_data_list_free(keylist);
98 }
99 
108 static int
109 perform_rollover_list(int sockfd, const char *listed_zone,
110  db_connection_t *dbconn)
111 {
112  zone_list_db_t *zonelist = NULL;
113  zone_db_t *zone = NULL;
114  const zone_db_t *zone_walk = NULL;
115  const char* fmt = "%-31s %-8s %-30s\n";
116 
117  if (listed_zone) {
118  zone = zone_db_new_get_by_name(dbconn, listed_zone);
119  } else {
120  zonelist = zone_list_db_new_get(dbconn);
121  }
122 
123  if (listed_zone && !zone) {
124  ods_log_error("[%s] zone '%s' not found", module_str, listed_zone);
125  client_printf(sockfd, "zone '%s' not found\n", listed_zone);
126  return 1;
127  }
128 
129  if (!zone && !zonelist) {
130  ods_log_error("[%s] error enumerating zones", module_str);
131  client_printf(sockfd, "error enumerating zones\n");
132  return 1;
133  }
134 
135  client_printf(sockfd, "Keys:\n");
136  client_printf(sockfd, fmt, "Zone:", "Keytype:", "Rollover expected:");
137 
138  if (zone) {
139  print_zone(sockfd, fmt, zone);
140  zone_db_free(zone);
141  return 0;
142  }
143 
144  while ((zone_walk = zone_list_db_next(zonelist))) {
145  print_zone(sockfd, fmt, zone_walk);
146  }
147  zone_list_db_free(zonelist);
148  return 0;
149 }
150 
151 static void
152 usage(int sockfd)
153 {
154  client_printf(sockfd,
155  "rollover list\n"
156  " [--zone <zone>] aka -z\n"
157  );
158 }
159 
160 static void
161 help(int sockfd)
162 {
163  client_printf(sockfd,
164  "List the expected dates and times of upcoming rollovers. This can be used to get an idea of upcoming works.\n"
165  "\nOptions:\n"
166  "zone name of the zone\n\n");
167 }
168 
169 static int
170 run(int sockfd, cmdhandler_ctx_type* context, const char *cmd)
171 {
172  #define NARGV 4
173  char buf[ODS_SE_MAXLINE];
174  const char *argv[NARGV];
175  int argc = 0, 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  ods_log_debug("[%s] %s command", module_str, rollover_list_funcblock.cmdname);
185 
186  /* Use buf as an intermediate buffer for the command.*/
187  strncpy(buf, cmd,sizeof(buf));
188  buf[sizeof(buf)-1] = '\0';
189 
190  /* separate the arguments*/
191  argc = ods_str_explode(buf, NARGV, argv);
192  if (argc == -1) {
193  client_printf_err(sockfd, "too many arguments\n");
194  ods_log_error("[%s] too many arguments for %s command",
195  module_str, rollover_list_funcblock.cmdname);
196  return -1;
197  }
198 
199  optind = 0;
200  while ((opt = getopt_long(argc, (char* const*)argv, "z:", long_options, &long_index)) != -1) {
201  switch (opt) {
202  case 'z':
203  zone = optarg;
204  break;
205  default:
206  client_printf_err(sockfd, "unknown arguments\n");
207  ods_log_error("[%s] unknown arguments for %s command",
208  module_str, rollover_list_funcblock.cmdname);
209  return -1;
210  }
211  }
212  return perform_rollover_list(sockfd, zone, dbconn);
213 }
214 
215 struct cmd_func_block rollover_list_funcblock = {
216  "rollover list", &usage, &help, NULL, &run
217 };
db_connection_t * getconnectioncontext(cmdhandler_ctx_type *context)
const key_data_t * key_data_list_next(key_data_list_t *key_data_list)
Definition: key_data.c:2359
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
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
#define NARGV
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
const zone_db_t * zone_list_db_next(zone_list_db_t *zone_list)
Definition: zone_db.c:2603
unsigned int zone_db_next_zsk_roll(const zone_db_t *zone)
Definition: zone_db.c:902
void zone_list_db_free(zone_list_db_t *zone_list)
Definition: zone_db.c:1989
zone_db_t * zone_db_new_get_by_name(const db_connection_t *connection, const char *name)
Definition: zone_db.c:1569
key_data_list_t * zone_db_get_keys(const zone_db_t *zone)
Definition: zone_db_ext.c:56