OpenDNSSEC-libhsm 2.1.13
confparser.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 NLNet Labs. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
32#include "config.h"
33#include "compat.h"
34#include "log.h"
35#include "status.h"
36
37#include <libxml/xpath.h>
38#include <libxml/relaxng.h>
39#include <libxml/xmlreader.h>
40#include <string.h>
41#include <stdlib.h>
42#include "libhsm.h"
43
44static const char* parser_str = "parser";
45
51parse_conf_repositories(const char* cfgfile)
52{
53 xmlDocPtr doc = NULL;
54 xmlXPathContextPtr xpathCtx = NULL;
55 xmlXPathObjectPtr xpathObj = NULL;
56 xmlNode* curNode = NULL;
57 xmlChar* xexpr = NULL;
58
59 int i;
60 char* name;
61 char* module;
62 char* tokenlabel;
63 char* pin;
64 uint8_t use_pubkey;
65 uint8_t allowextract;
66 int require_backup;
67 hsm_repository_t* rlist = NULL;
68 hsm_repository_t* repo = NULL;
69
70 /* Load XML document */
71 doc = xmlParseFile(cfgfile);
72 if (doc == NULL) {
73 ods_log_error("[%s] could not parse <RepositoryList>: "
74 "xmlParseFile() failed", parser_str);
75 return NULL;
76 }
77 /* Create xpath evaluation context */
78 xpathCtx = xmlXPathNewContext(doc);
79 if(xpathCtx == NULL) {
80 xmlFreeDoc(doc);
81 ods_log_error("[%s] could not parse <RepositoryList>: "
82 "xmlXPathNewContext() failed", parser_str);
83 return NULL;
84 }
85 /* Evaluate xpath expression */
86 xexpr = (xmlChar*) "//Configuration/RepositoryList/Repository";
87 xpathObj = xmlXPathEvalExpression(xexpr, xpathCtx);
88 if(xpathObj == NULL) {
89 xmlXPathFreeContext(xpathCtx);
90 xmlFreeDoc(doc);
91 ods_log_error("[%s] could not parse <RepositoryList>: "
92 "xmlXPathEvalExpression failed", parser_str);
93 return NULL;
94 }
95 /* Parse repositories */
96 if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
97 for (i = 0; i < xpathObj->nodesetval->nodeNr; i++) {
98 repo = NULL;
99 name = NULL;
100 module = NULL;
101 tokenlabel = NULL;
102 pin = NULL;
103 use_pubkey = 1;
104 allowextract = 0;
105 require_backup = 0;
106
107 curNode = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
108 name = (char *) xmlGetProp(xpathObj->nodesetval->nodeTab[i],
109 (const xmlChar *)"name");
110 while (curNode) {
111 if (xmlStrEqual(curNode->name, (const xmlChar *)"RequireBackup"))
112 require_backup = 1;
113 if (xmlStrEqual(curNode->name, (const xmlChar *)"Module"))
114 module = (char *) xmlNodeGetContent(curNode);
115 if (xmlStrEqual(curNode->name, (const xmlChar *)"TokenLabel"))
116 tokenlabel = (char *) xmlNodeGetContent(curNode);
117 if (xmlStrEqual(curNode->name, (const xmlChar *)"PIN"))
118 pin = (char *) xmlNodeGetContent(curNode);
119 if (xmlStrEqual(curNode->name, (const xmlChar *)"SkipPublicKey"))
120 use_pubkey = 0;
121 if (xmlStrEqual(curNode->name, (const xmlChar *)"AllowExtraction"))
122 allowextract = 1;
123
124 curNode = curNode->next;
125 }
126 if (name && module && tokenlabel) {
127 repo = hsm_repository_new(name, module, tokenlabel, pin,
128 use_pubkey, allowextract, require_backup);
129 }
130 if (!repo) {
131 ods_log_error("[%s] unable to add %s repository: "
132 "hsm_repository_new() failed", parser_str, name?name:"-");
133 } else {
134 repo->next = rlist;
135 rlist = repo;
136 ods_log_debug("[%s] added %s repository to repositorylist",
137 parser_str, name);
138 }
139 free((void*)name);
140 free((void*)module);
141 free((void*)tokenlabel);
142 }
143 }
144
145 xmlXPathFreeObject(xpathObj);
146 xmlXPathFreeContext(xpathCtx);
147 if (doc) {
148 xmlFreeDoc(doc);
149 }
150 return rlist;
151}
hsm_repository_t * parse_conf_repositories(const char *cfgfile)
Definition: confparser.c:51
hsm_repository_t * hsm_repository_new(char *name, char *module, char *tokenlabel, char *pin, uint8_t use_pubkey, uint8_t allowextract, uint8_t require_backup)
Definition: libhsm.c:374
hsm_repository_t * next
Definition: libhsm.h:119