OpenDNSSEC-enforcer  2.0.2
zone_list_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 
29 #include "config.h"
30 
31 #include "daemon/cmdhandler.h"
32 #include "daemon/engine.h"
33 #include "file.h"
34 #include "log.h"
35 #include "str.h"
36 #include "duration.h"
37 #include "clientpipe.h"
38 #include "db/zone.h"
39 #include "db/policy.h"
40 #include "db/db_value.h"
41 
42 #include "keystate/zone_list_cmd.h"
43 
44 static const char *module_str = "zone_list_cmd";
45 
46 static void
47 usage(int sockfd)
48 {
49  client_printf(sockfd,
50  "zone list\n");
51 }
52 
53 static void
54 help(int sockfd)
55 {
56  client_printf(sockfd,
57  "List all zones currently in the database.\n\n"
58  );
59 }
60 
61 static int
62 handles(const char *cmd, ssize_t n)
63 {
64  return ods_check_command(cmd, n, zone_list_funcblock()->cmdname)?1:0;
65 }
66 
67 static int
68 run(int sockfd, engine_type* engine, const char *cmd, ssize_t n,
69  db_connection_t *dbconn)
70 {
71  const char* fmt = "%-31s %-13s %-26s %-34s\n";
73  const zone_t* zone;
74  policy_t* policy = NULL;
75  const char* nctime;
76  char buf[32];
77  int cmp;
78  (void)cmd; (void)n;
79 
80  ods_log_debug("[%s] %s command", module_str, zone_list_funcblock()->cmdname);
81 
82  if (!(zone_list = zone_list_new_get(dbconn))) {
83  client_printf_err(sockfd, "Unable to get list of zones, memory allocation or database error!\n");
84  return 1;
85  }
86 
87  client_printf(sockfd, "Database set to: %s\n", engine->config->datastore);
88  if (!(zone = zone_list_next(zone_list))) {
89  client_printf(sockfd, "No zones in database.\n");
90  zone_list_free(zone_list);
91  return 0;
92  }
93 
94  client_printf(sockfd, "Zones:\n");
95  client_printf(sockfd, fmt, "Zone:", "Policy:", "Next change:",
96  "Signer Configuration:");
97  while (zone) {
98  if (zone_next_change(zone) >= time_now()) {
99  if (!ods_ctime_r(buf, sizeof(buf), zone_next_change(zone))) {
100  nctime = "invalid date/time";
101  }
102  else {
103  nctime = buf;
104  }
105  } else if (zone_next_change(zone) >= 0) {
106  nctime = "as soon as possible";
107  } else {
108  nctime = "no changes scheduled";
109  }
110 
111  if (policy) {
112  /*
113  * If we already have a policy object; If policy_id compare fails
114  * or if they are not the same free the policy object to we will
115  * later retrieve the correct policy
116  */
117  if (db_value_cmp(policy_id(policy), zone_policy_id(zone), &cmp)
118  || cmp)
119  {
120  policy_free(policy);
121  policy = NULL;
122  }
123  }
124  if (!policy) {
125  policy = zone_get_policy(zone);
126  }
127 
128  client_printf(sockfd, fmt,
129  zone_name(zone),
130  (policy ? policy_name(policy) : "NOT_FOUND"),
131  nctime,
132  zone_signconf_path(zone));
133 
134  zone = zone_list_next(zone_list);
135  }
136  policy_free(policy);
137  zone_list_free(zone_list);
138 
139  return 0;
140 }
141 
142 static struct cmd_func_block funcblock = {
143  "zone list", &usage, &help, &handles, &run
144 };
145 
146 struct cmd_func_block*
148 {
149  return &funcblock;
150 }
void(* help)(int sockfd)
Definition: cmdhandler.h:64
const char * zone_signconf_path(const zone_t *zone)
Definition: zone.c:798
const char * datastore
Definition: cfg.h:68
const db_value_t * zone_policy_id(const zone_t *zone)
Definition: zone.c:736
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
const char * policy_name(const policy_t *policy)
Definition: policy.c:813
policy_t * zone_get_policy(const zone_t *zone)
Definition: zone.c:744
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
int zone_next_change(const zone_t *zone)
Definition: zone.c:806
const char * cmdname
Definition: cmdhandler.h:59
void(* usage)(int sockfd)
Definition: cmdhandler.h:61
int db_value_cmp(const db_value_t *value_a, const db_value_t *value_b, int *result)
Definition: db_value.c:102
engineconfig_type * config
Definition: engine.h:53
void policy_free(policy_t *policy)
Definition: policy.c:518
const char * zone_name(const zone_t *zone)
Definition: zone.c:782
zone_list_t * zone_list_new_get(const db_connection_t *connection)
Definition: zone.c:2399
Definition: policy.h:60
Definition: zone.h:46
int(* handles)(const char *cmd, ssize_t n)
Definition: cmdhandler.h:67
struct cmd_func_block * zone_list_funcblock(void)
const db_value_t * policy_id(const policy_t *policy)
Definition: policy.c:805