OpenDNSSEC-libhsm  2.0.2
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 
44 static const char* parser_str = "parser";
45 
51 parse_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  int require_backup;
66  hsm_repository_t* rlist = NULL;
67  hsm_repository_t* repo = NULL;
68 
69  /* Load XML document */
70  doc = xmlParseFile(cfgfile);
71  if (doc == NULL) {
72  ods_log_error("[%s] could not parse <RepositoryList>: "
73  "xmlParseFile() failed", parser_str);
74  return NULL;
75  }
76  /* Create xpath evaluation context */
77  xpathCtx = xmlXPathNewContext(doc);
78  if(xpathCtx == NULL) {
79  xmlFreeDoc(doc);
80  ods_log_error("[%s] could not parse <RepositoryList>: "
81  "xmlXPathNewContext() failed", parser_str);
82  return NULL;
83  }
84  /* Evaluate xpath expression */
85  xexpr = (xmlChar*) "//Configuration/RepositoryList/Repository";
86  xpathObj = xmlXPathEvalExpression(xexpr, xpathCtx);
87  if(xpathObj == NULL) {
88  xmlXPathFreeContext(xpathCtx);
89  xmlFreeDoc(doc);
90  ods_log_error("[%s] could not parse <RepositoryList>: "
91  "xmlXPathEvalExpression failed", parser_str);
92  return NULL;
93  }
94  /* Parse repositories */
95  if (xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0) {
96  for (i = 0; i < xpathObj->nodesetval->nodeNr; i++) {
97  repo = NULL;
98  name = NULL;
99  module = NULL;
100  tokenlabel = NULL;
101  pin = NULL;
102  use_pubkey = 1;
103  require_backup = 0;
104 
105  curNode = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode;
106  name = (char *) xmlGetProp(xpathObj->nodesetval->nodeTab[i],
107  (const xmlChar *)"name");
108  while (curNode) {
109  if (xmlStrEqual(curNode->name, (const xmlChar *)"RequireBackup"))
110  require_backup = 1;
111  if (xmlStrEqual(curNode->name, (const xmlChar *)"Module"))
112  module = (char *) xmlNodeGetContent(curNode);
113  if (xmlStrEqual(curNode->name, (const xmlChar *)"TokenLabel"))
114  tokenlabel = (char *) xmlNodeGetContent(curNode);
115  if (xmlStrEqual(curNode->name, (const xmlChar *)"PIN"))
116  pin = (char *) xmlNodeGetContent(curNode);
117  if (xmlStrEqual(curNode->name, (const xmlChar *)"SkipPublicKey"))
118  use_pubkey = 0;
119 
120  curNode = curNode->next;
121  }
122  if (name && module && tokenlabel) {
123  repo = hsm_repository_new(name, module, tokenlabel, pin,
124  use_pubkey, require_backup);
125  }
126  if (!repo) {
127  ods_log_error("[%s] unable to add %s repository: "
128  "hsm_repository_new() failed", parser_str, name?name:"-");
129  } else {
130  repo->next = rlist;
131  rlist = repo;
132  ods_log_debug("[%s] added %s repository to repositorylist",
133  parser_str, name);
134  }
135  free((void*)name);
136  free((void*)module);
137  free((void*)tokenlabel);
138  }
139  }
140 
141  xmlXPathFreeObject(xpathObj);
142  xmlXPathFreeContext(xpathCtx);
143  if (doc) {
144  xmlFreeDoc(doc);
145  }
146  return rlist;
147 }
hsm_repository_t * next
Definition: libhsm.h:118
hsm_repository_t * hsm_repository_new(char *name, char *module, char *tokenlabel, char *pin, uint8_t use_pubkey, uint8_t require_backup)
Definition: libhsm.c:368
hsm_repository_t * parse_conf_repositories(const char *cfgfile)
Definition: confparser.c:51