OpenDNSSEC-enforcer  2.1.12
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 "log.h"
32 #include "str.h"
33 #include <libxml/xpath.h>
34 #include <libxml/xmlreader.h>
35 #include "file.h"
36 
37 static const char *module_str = "repositorylist_cmd";
38 
39 static int
40 perform_repositorylist(int sockfd)
41 {
42  const char* cfgfile = ODS_SE_CFGFILE;
43  xmlDocPtr doc = NULL;
44  xmlNode *curNode;
45  xmlXPathContextPtr xpathCtx = NULL;
46  xmlXPathObjectPtr xpathObj = NULL;
47 
48  const char *fmt = "%-31s %-13s %-13s\n";
49  char *capacity = NULL;
50  int backup;
51  char *repository = NULL;
52  int i;
53 
54 
55  xmlChar *xexpr = (unsigned char *)"//Configuration/RepositoryList/Repository";
56  doc = xmlParseFile(cfgfile);
57  if (doc == NULL) {
58  ods_log_error("[%s] unable to read cfgfile %s", module_str, cfgfile);
59  return -1;
60  }
61 
62  xpathCtx = xmlXPathNewContext(doc);
63  if (xpathCtx == NULL) {
64  ods_log_error("[%s] unable to create new XPath context for cfgfile"
65  "%s expr %s", module_str, cfgfile, xexpr);
66  xmlFreeDoc(doc);
67  return -1;
68  }
69 
70  xpathObj = xmlXPathEvalExpression(xexpr, xpathCtx);
71  if(xpathObj == NULL) {
72  ods_log_error("[%s] unable to evaluate required element %s in "
73  "cfgfile %s", module_str, xexpr, cfgfile);
74  xmlXPathFreeContext(xpathCtx);
75  xmlFreeDoc(doc);
76  return -1;
77  }
78 
79  client_printf(sockfd, "Repositories:\n");
80  client_printf(sockfd, fmt, "Name:", "Capacity:", "RequireBackup:");
81 
82  if (xpathObj->nodesetval){
83  for (i = 0; i < xpathObj->nodesetval->nodeNr; i++) {
84  curNode = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
85  repository = (char*)xmlGetProp(xpathObj->nodesetval->nodeTab[i], (const xmlChar *)"name");
86 
87  backup = 0;
88  while (curNode) {
89  if (xmlStrEqual(curNode->name, (const xmlChar *)"Capacity"))
90  capacity = (char*) xmlNodeGetContent(curNode);
91  if (xmlStrEqual(curNode->name, (const xmlChar *)"RequireBackup"))
92  backup = 1;
93  curNode = curNode->next;
94  }
95  client_printf(sockfd, fmt, repository, capacity?capacity:"-", backup?"Yes":"No");
96  free(repository);
97  repository = NULL;
98  free(capacity);
99  capacity = NULL;
100  }
101  }
102 
103  xmlXPathFreeObject(xpathObj);
104  xmlXPathFreeContext(xpathCtx);
105  xmlFreeDoc(doc);
106 
107 
108  return 0;
109 }
110 
111 static void
112 usage(int sockfd)
113 {
114  client_printf(sockfd,
115  "repository list\n");
116 }
117 
118 static void
119 help(int sockfd)
120 {
121  client_printf(sockfd, "List repositories.\n\n");
122 }
123 
124 static int
125 run(int sockfd, cmdhandler_ctx_type* context, const char *cmd)
126 {
127  (void)cmd;
128  ods_log_debug("[%s] %s command", module_str,
129  repositorylist_funcblock.cmdname);
130 
131  if (perform_repositorylist(sockfd)) {
132  ods_log_error_and_printf(sockfd, module_str,
133  "unable to list repositories ");
134  return 1;
135  }
136  return 0;
137 }
138 
139 struct cmd_func_block repositorylist_funcblock = {
140  "repository list", &usage, &help, NULL, &run
141 };
struct cmd_func_block repositorylist_funcblock