OpenDNSSEC-enforcer 2.1.13
repositorylist_cmd.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Stichting NLnet Labs
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
27
29#include "daemon/engine.h"
30#include "clientpipe.h"
31#include "longgetopt.h"
32#include "log.h"
33#include "str.h"
34#include <libxml/xpath.h>
35#include <libxml/xmlreader.h>
36#include "file.h"
37
38static const char *module_str = "repositorylist_cmd";
39
40static int
41perform_repositorylist(int sockfd)
42{
43 const char* cfgfile = ODS_SE_CFGFILE;
44 xmlDocPtr doc = NULL;
45 xmlNode *curNode;
46 xmlXPathContextPtr xpathCtx = NULL;
47 xmlXPathObjectPtr xpathObj = NULL;
48
49 const char *fmt = "%-31s %-13s %-13s\n";
50 char *capacity = NULL;
51 int backup;
52 char *repository = NULL;
53 int i;
54
55
56 xmlChar *xexpr = (unsigned char *)"//Configuration/RepositoryList/Repository";
57 doc = xmlParseFile(cfgfile);
58 if (doc == NULL) {
59 ods_log_error("[%s] unable to read cfgfile %s", module_str, cfgfile);
60 return -1;
61 }
62
63 xpathCtx = xmlXPathNewContext(doc);
64 if (xpathCtx == NULL) {
65 ods_log_error("[%s] unable to create new XPath context for cfgfile"
66 "%s expr %s", module_str, cfgfile, xexpr);
67 xmlFreeDoc(doc);
68 return -1;
69 }
70
71 xpathObj = xmlXPathEvalExpression(xexpr, xpathCtx);
72 if(xpathObj == NULL) {
73 ods_log_error("[%s] unable to evaluate required element %s in "
74 "cfgfile %s", module_str, xexpr, cfgfile);
75 xmlXPathFreeContext(xpathCtx);
76 xmlFreeDoc(doc);
77 return -1;
78 }
79
80 client_printf(sockfd, "Repositories:\n");
81 client_printf(sockfd, fmt, "Name:", "Capacity:", "RequireBackup:");
82
83 if (xpathObj->nodesetval){
84 for (i = 0; i < xpathObj->nodesetval->nodeNr; i++) {
85 curNode = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
86 repository = (char*)xmlGetProp(xpathObj->nodesetval->nodeTab[i], (const xmlChar *)"name");
87
88 backup = 0;
89 while (curNode) {
90 if (xmlStrEqual(curNode->name, (const xmlChar *)"Capacity"))
91 capacity = (char*) xmlNodeGetContent(curNode);
92 if (xmlStrEqual(curNode->name, (const xmlChar *)"RequireBackup"))
93 backup = 1;
94 curNode = curNode->next;
95 }
96 client_printf(sockfd, fmt, repository, capacity?capacity:"-", backup?"Yes":"No");
97 free(repository);
98 repository = NULL;
99 free(capacity);
100 capacity = NULL;
101 }
102 }
103
104 xmlXPathFreeObject(xpathObj);
105 xmlXPathFreeContext(xpathCtx);
106 xmlFreeDoc(doc);
107
108
109 return 0;
110}
111
112static void
113usage(int sockfd)
114{
115 client_printf(sockfd,
116 "repository list\n");
117}
118
119static void
120help(int sockfd)
121{
122 client_printf(sockfd, "List repositories.\n\n");
123}
124
125static int
126run(cmdhandler_ctx_type* context, int argc, char* argv[])
127{
128 int sockfd = context->sockfd;
129 if (perform_repositorylist(sockfd)) {
130 ods_log_error_and_printf(sockfd, module_str,
131 "unable to list repositories ");
132 return 1;
133 }
134 return 0;
135}
136
137struct cmd_func_block repositorylist_funcblock = {
138 "repository list", &usage, &help, NULL, NULL, &run, NULL
139};
struct cmd_func_block repositorylist_funcblock