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