util: add xml validation against schema in virXMLParseHelper()

We need this in order to validate XML against schema at one
place, rather than have the same code for validation in different
functions.
I will add '--validate' option to more virsh commands soon and
this makes it easier as virXMLParse() is called in every one I
plan to change.

Signed-off-by: Kristina Hanicova <khanicov@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Kristina Hanicova 2021-08-11 14:36:45 +02:00 committed by Ján Tomko
parent f43c27960e
commit 323a1318a3
1 changed files with 14 additions and 4 deletions

View File

@ -33,6 +33,7 @@
#include "virfile.h"
#include "virstring.h"
#include "virutil.h"
#include "configmake.h"
#define VIR_FROM_THIS VIR_FROM_XML
@ -1096,8 +1097,8 @@ catchXMLError(void *ctx, const char *msg G_GNUC_UNUSED, ...)
* @url: URL of XML document for string parser
* @rootelement: Optional name of the expected root element
* @ctxt: optional pointer to populate with new context pointer
* @schemafile: unused
* @validate: unused
* @schemafile: optional name of the file the parsed XML will be validated against
* @validate: if true, the XML will be validated against schema in @schemafile
*
* Parse XML document provided either as a file or a string. The function
* guarantees that the XML document contains a root element.
@ -1114,8 +1115,8 @@ virXMLParseHelper(int domcode,
const char *url,
const char *rootelement,
xmlXPathContextPtr *ctxt,
const char *schemafile G_GNUC_UNUSED,
bool validate G_GNUC_UNUSED)
const char *schemafile,
bool validate)
{
struct virParserData private;
g_autoptr(xmlParserCtxt) pctxt = NULL;
@ -1181,6 +1182,15 @@ virXMLParseHelper(int domcode,
(*ctxt)->node = rootnode;
}
if (validate && schemafile != NULL) {
g_autofree char *schema = virFileFindResource(schemafile,
abs_top_srcdir "/docs/schemas",
PKGDATADIR "/schemas");
if (!schema ||
(virXMLValidateAgainstSchema(schema, xml) < 0))
return NULL;
}
return g_steal_pointer(&xml);
}