diff options
Diffstat (limited to 'worker/xml_helper.c')
-rw-r--r-- | worker/xml_helper.c | 63 |
1 files changed, 31 insertions, 32 deletions
diff --git a/worker/xml_helper.c b/worker/xml_helper.c index 35932e8..8eff726 100644 --- a/worker/xml_helper.c +++ b/worker/xml_helper.c @@ -46,13 +46,14 @@ xmlChar *default_namespace_prefix = (xmlChar *) "def"; int validate_policy(const char *policy_file_name, char **ipa_policy_type, char **xslt_file_name) { xmlDocPtr doc; char *rng_file_name; + xmlRelaxNGParserCtxtPtr rng_parser_context; + xmlRelaxNGPtr rng_schema; xmlRelaxNGValidCtxtPtr rng_context; xmlChar xpath_expr[XMLCHARLEN]; doc = xmlParseFile(policy_file_name); CHECK(doc, NULL, ("Cannot parse document %s!\n", policy_file_name), exit(1)); - xmlStrPrintf(xpath_expr, XMLCHARLEN, (xmlChar *) "//%s:ipa/*[2]", default_namespace_prefix); *ipa_policy_type = find_by_xpath(doc, xpath_expr, FIND_NAME); @@ -73,11 +74,12 @@ int validate_policy(const char *policy_file_name, char **ipa_policy_type, char * DEBUG(3, ("Found name of RELAX NG schema file: %s\n", rng_file_name)); - /* validate the document */ - rng_context = - xmlRelaxNGNewValidCtxt(xmlRelaxNGParse - (xmlRelaxNGNewParserCtxt(rng_file_name))); + rng_parser_context = xmlRelaxNGNewParserCtxt(rng_file_name); + CHECK(rng_parser_context, NULL, ("Failed to parse RNG file\n"), exit(1)); + rng_schema = xmlRelaxNGParse(rng_parser_context); + CHECK(rng_schema, NULL, ("Failed to create RNG schema\n"), exit(1)); + rng_context = xmlRelaxNGNewValidCtxt(rng_schema); CHECK(rng_context, NULL, ("Failed to create RNG context\n"), exit(1)); if (xmlRelaxNGValidateDoc(rng_context, doc) == 0) { DEBUG(3, ("The document is valid.\n")); @@ -87,6 +89,8 @@ int validate_policy(const char *policy_file_name, char **ipa_policy_type, char * } xmlRelaxNGFreeValidCtxt(rng_context); + xmlRelaxNGFree(rng_schema); + xmlRelaxNGFreeParserCtxt(rng_parser_context); free(rng_file_name); @@ -99,6 +103,7 @@ int validate_policy(const char *policy_file_name, char **ipa_policy_type, char * } xmlFreeDoc(doc); + xmlCleanupParser(); return 0; } @@ -140,7 +145,7 @@ xmlChar *get_default_namespace(xmlDocPtr doc) { } CHECK(root_node->ns->href, NULL, ("Root node of the current document must define a namespace!\n"), return NULL); - default_namespace = xmlStrndup(root_node->ns->href, XMLCHARLEN); + default_namespace = xmlStrdup(root_node->ns->href); CHECK(default_namespace, NULL, ("Cannot copy namespace!\n"), return NULL); DEBUG(3, ("Default namespace is %s\n", default_namespace)); @@ -164,47 +169,36 @@ xmlChar *get_default_namespace(xmlDocPtr doc) { */ char *find_by_xpath(const xmlDocPtr doc, const xmlChar * xpath_expr, const int type) { - - xmlXPathContextPtr xpath_context; - xmlXPathObjectPtr xpath_obj; + int ret; + xmlXPathContextPtr xpath_context=NULL; + xmlXPathObjectPtr xpath_obj=NULL; char *result = NULL; - xmlChar *namespace; + xmlChar *namespace=NULL; namespace = get_default_namespace(doc); CHECK(namespace, NULL, ("No default namespace found.\n"), return NULL); /* Create xpath evaluation context */ xpath_context = xmlXPathNewContext(doc); - CHECK_NULL_FATAL(xpath_context, - ("Error: unable to create new XPath context\n")); + CHECK(xpath_context, NULL, + ("Error: unable to create new XPath context\n"), goto failed); /* Register a namespace */ - if (xmlXPathRegisterNs(xpath_context, default_namespace_prefix, namespace) != 0) { - DEBUG(0, - ("Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", - default_namespace_prefix , namespace)); - xmlXPathFreeContext(xpath_context); - return NULL; - } + ret=xmlXPathRegisterNs(xpath_context, default_namespace_prefix, namespace); + CHECK(ret, -1, + ("Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", + default_namespace_prefix , namespace), goto failed); /* Evaluate xpath expression */ xpath_obj = xmlXPathEvalExpression(xpath_expr, xpath_context); - if (xpath_obj == NULL) { - DEBUG(0, - ("Error: unable to evaluate xpath expression \"%s\"\n", - xpath_expr)); - xmlXPathFreeContext(xpath_context); - return NULL; - } + CHECK(xpath_obj, NULL, + ("Error: unable to evaluate xpath expression \"%s\"\n", xpath_expr), + goto failed); if (xmlXPathNodeSetIsEmpty(xpath_obj->nodesetval)) { DEBUG(0, ("Nothing found for %s\n", xpath_expr)); - xmlXPathFreeObject(xpath_obj); - xmlXPathFreeContext(xpath_context); - return NULL; + goto failed; } else if (xmlXPathNodeSetGetLength(xpath_obj->nodesetval) != 1) { DEBUG(0, ("More than one node found for %s!", xpath_expr)); - xmlXPathFreeObject(xpath_obj); - xmlXPathFreeContext(xpath_context); - return NULL; + goto failed; } else { switch (type) { case FIND_NAME: @@ -226,4 +220,9 @@ char *find_by_xpath(const xmlDocPtr doc, const xmlChar * xpath_expr, const int t xmlXPathFreeContext(xpath_context); return result; +failed: + xmlFree(namespace); + xmlXPathFreeObject(xpath_obj); + xmlXPathFreeContext(xpath_context); + return NULL; } |