OpenDNSSEC-enforcer  2.0.3
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 
32 #include "db/zone.h"
33 #include "daemon/engine.h"
34 #include "daemon/cmdhandler.h"
35 #include "file.h"
36 #include "log.h"
37 #include "str.h"
38 #include "clientpipe.h"
39 
41 
42 static const char *module_str = "rollover_list_cmd";
43 
50 static char*
51 map_keytime(const zone_t *zone, const key_data_t *key)
52 {
53  time_t t = 0;
54  char ct[26];
55  struct tm srtm;
56 
57  switch(key_data_ds_at_parent(key)) {
59  return strdup("waiting for ds-submit");
61  return strdup("waiting for ds-seen");
63  return strdup("waiting for ds-retract");
65  return strdup("waiting for ds-gone");
66  default: break;
67  }
68 
69  switch (key_data_role(key)) {
70  case KEY_DATA_ROLE_KSK: t = (time_t)zone_next_ksk_roll(zone); break;
71  case KEY_DATA_ROLE_ZSK: t = (time_t)zone_next_zsk_roll(zone); break;
72  case KEY_DATA_ROLE_CSK: t = (time_t)zone_next_csk_roll(zone); break;
73  default: break;
74  }
75  if (!t) return strdup("No roll scheduled");
76 
77  localtime_r(&t, &srtm);
78  strftime(ct, 26, "%Y-%m-%d %H:%M:%S", &srtm);
79  return strdup(ct);
80 }
81 
82 static void
83 print_zone(int sockfd, const char* fmt, const zone_t* zone)
84 {
85  key_data_list_t *keylist;
86  const key_data_t *key;
87 
88  keylist = zone_get_keys(zone);
89  while ((key = key_data_list_next(keylist))) {
90  char *tchange = map_keytime(zone, key);
91  client_printf(sockfd, fmt, zone_name(zone),
92  key_data_role_text(key), tchange);
93  free(tchange);
94  }
95  key_data_list_free(keylist);
96 }
97 
106 static int
107 perform_rollover_list(int sockfd, const char *listed_zone,
108  db_connection_t *dbconn)
109 {
110  zone_list_t *zonelist = NULL;
111  zone_t *zone = NULL;
112  const zone_t *zone_walk = NULL;
113  const char* fmt = "%-31s %-8s %-30s\n";
114 
115  if (listed_zone) {
116  zone = zone_new_get_by_name(dbconn, listed_zone);
117  } else {
118  zonelist = zone_list_new_get(dbconn);
119  }
120 
121  if (listed_zone && !zone) {
122  ods_log_error("[%s] zone '%s' not found", module_str, listed_zone);
123  client_printf(sockfd, "zone '%s' not found\n", listed_zone);
124  return 1;
125  }
126 
127  if (!zone && !zonelist) {
128  ods_log_error("[%s] error enumerating zones", module_str);
129  client_printf(sockfd, "error enumerating zones\n");
130  return 1;
131  }
132 
133  client_printf(sockfd, "Keys:\n");
134  client_printf(sockfd, fmt, "Zone:", "Keytype:", "Rollover expected:");
135 
136  if (zone) {
137  print_zone(sockfd, fmt, zone);
138  zone_free(zone);
139  return 0;
140  }
141 
142  while ((zone_walk = zone_list_next(zonelist))) {
143  print_zone(sockfd, fmt, zone_walk);
144  }
145  zone_list_free(zonelist);
146  return 0;
147 }
148 
149 static void
150 usage(int sockfd)
151 {
152  client_printf(sockfd,
153  "rollover list\n"
154  " [--zone <zone>] aka -z\n"
155  );
156 }
157 
158 static void
159 help(int sockfd)
160 {
161  client_printf(sockfd,
162  "List the expected dates and times of upcoming rollovers. This can be used to get an idea of upcoming works.\n"
163  "\nOptions:\n"
164  "zone name of the zone\n\n");
165 }
166 
167 static int
168 handles(const char *cmd, ssize_t n)
169 {
170  return ods_check_command(cmd, n, rollover_list_funcblock()->cmdname)?1:0;
171 }
172 
173 static int
174 run(int sockfd, engine_type* engine, const char *cmd, ssize_t n,
175  db_connection_t *dbconn)
176 {
177  #define NARGV 8
178  char buf[ODS_SE_MAXLINE];
179  const char *argv[NARGV];
180  int argc;
181  const char *zone = NULL;
182  (void)engine;
183 
184  ods_log_debug("[%s] %s command", module_str, rollover_list_funcblock()->cmdname);
185  cmd = ods_check_command(cmd, n, rollover_list_funcblock()->cmdname);
186 
187  /* Use buf as an intermediate buffer for the command.*/
188  strncpy(buf, cmd,sizeof(buf));
189  buf[sizeof(buf)-1] = '\0';
190 
191  /* separate the arguments*/
192  argc = ods_str_explode(buf, NARGV, argv);
193  if (argc > NARGV) {
194  ods_log_warning("[%s] too many arguments for %s command",
195  module_str, rollover_list_funcblock()->cmdname);
196  client_printf(sockfd,"too many arguments\n");
197  return -1;
198  }
199 
200  (void)ods_find_arg_and_param(&argc,argv,"zone","z",&zone);
201  if (argc) {
202  ods_log_warning("[%s] unknown arguments for %s command",
203  module_str, rollover_list_funcblock()->cmdname);
204  client_printf(sockfd,"unknown arguments\n");
205  return -1;
206  }
207  return perform_rollover_list(sockfd, zone, dbconn);
208 }
209 
210 static struct cmd_func_block funcblock = {
211  "rollover list", &usage, &help, &handles, &run
212 };
213 
214 struct cmd_func_block*
216 {
217  return &funcblock;
218 }
const char * key_data_role_text(const key_data_t *key_data)
Definition: key_data.c:711
key_data_role
Definition: key_data.h:40
void(* help)(int sockfd)
Definition: cmdhandler.h:64
const zone_t * zone_list_next(zone_list_t *zone_list)
Definition: zone.c:2600
void ods_log_debug(const char *format,...)
Definition: log.c:41
unsigned int zone_next_zsk_roll(const zone_t *zone)
Definition: zone.c:902
#define NARGV
void zone_list_free(zone_list_t *zone_list)
Definition: zone.c:1989
int(* run)(int sockfd, struct engine_struct *engine, const char *cmd, ssize_t n, db_connection_t *dbconn)
Definition: cmdhandler.h:79
key_data_list_t * zone_get_keys(const zone_t *zone)
Definition: zone_ext.c:38
void ods_log_error(const char *format,...)
Definition: log.c:69
void zone_free(zone_t *zone)
Definition: zone.c:325
void(* usage)(int sockfd)
Definition: cmdhandler.h:61
key_data_ds_at_parent
Definition: key_data.h:48
const key_data_t * key_data_list_next(key_data_list_t *key_data_list)
Definition: key_data.c:2359
zone_t * zone_new_get_by_name(const db_connection_t *connection, const char *name)
Definition: zone.c:1569
const char * zone_name(const zone_t *zone)
Definition: zone.c:782
void key_data_list_free(key_data_list_t *key_data_list)
Definition: key_data.c:1694
unsigned int zone_next_csk_roll(const zone_t *zone)
Definition: zone.c:910
unsigned int zone_next_ksk_roll(const zone_t *zone)
Definition: zone.c:894
zone_list_t * zone_list_new_get(const db_connection_t *connection)
Definition: zone.c:2399
Definition: zone.h:46
int(* handles)(const char *cmd, ssize_t n)
Definition: cmdhandler.h:67
void ods_log_warning(const char *format,...)
Definition: log.c:62
struct cmd_func_block * rollover_list_funcblock(void)