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