Skip to content

Commit 190ca9d

Browse files
committed
Detect source datastream version
Instead of hard-coding SCAP 1.2, we will use data-stream-collection@schematron-version attribute to detect the SCAP version of datastream and we will choose the right XML schema accordingly. So far, only 1.2 and 1.3 datastreams are supported.
1 parent b967d10 commit 190ca9d

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

src/DS/sds.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,3 +1265,29 @@ int ds_sds_compose_from_xccdf(const char *xccdf_file, const char *target_datastr
12651265
xmlFreeDoc(doc);
12661266
return 0;
12671267
}
1268+
1269+
char *ds_sds_detect_version(xmlTextReader *reader)
1270+
{
1271+
/* find root element */
1272+
while (xmlTextReaderRead(reader) == 1 && xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT)
1273+
;
1274+
1275+
char *element_name = (char *) xmlTextReaderConstLocalName(reader);
1276+
if (!element_name) {
1277+
oscap_setxmlerr(xmlGetLastError());
1278+
return NULL;
1279+
}
1280+
if (strcmp(element_name, "data-stream-collection")) {
1281+
oscap_seterr(OSCAP_EFAMILY_OSCAP,
1282+
"Expected root element name for SCAP source datastream is" \
1283+
"'data-stream-collection' but actual root element name is '%s'.",
1284+
element_name);
1285+
return NULL;
1286+
}
1287+
char *schematron_version = (char *) xmlTextReaderGetAttribute(reader, BAD_CAST "schematron-version");
1288+
if (!schematron_version) {
1289+
oscap_setxmlerr(xmlGetLastError());
1290+
return NULL;
1291+
}
1292+
return schematron_version;
1293+
}

src/DS/sds_priv.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#endif
2727

2828
#include <libxml/tree.h>
29+
#include <libxml/xmlreader.h>
2930
#include "common/public/oscap.h"
3031
#include "common/util.h"
3132
#include "ds_sds_session.h"
@@ -50,5 +51,8 @@ xmlNodePtr ds_sds_find_component_ref(xmlNodePtr datastream, const char *id);
5051

5152
char *ds_sds_mangle_filepath(const char *filepath);
5253

54+
char *ds_sds_detect_version(xmlTextReader *reader);
55+
5356
OSCAP_HIDDEN_END;
57+
5458
#endif

src/source/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ liboscapsource_la_CPPFLAGS = \
1919
@xml2_CFLAGS@ @xslt_CFLAGS@ @exslt_CFLAGS@ \
2020
-I$(srcdir)/public \
2121
-I$(top_srcdir)/src \
22+
-I$(top_srcdir)/src/DS/public \
2223
-I$(top_srcdir)/src/CPE/public \
2324
-I$(top_srcdir)/src/OVAL/probes/SEAP/public \
2425
-I$(top_srcdir)/src/common/public

src/source/oscap_source.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "source/validate_priv.h"
5252
#include "XCCDF/elements.h"
5353
#include "XCCDF/public/xccdf_benchmark.h"
54+
#include "DS/sds_priv.h"
5455

5556
typedef enum oscap_source_type {
5657
OSCAP_SRC_FROM_USER_XML_FILE = 1, ///< The source originated from XML file supplied by user
@@ -360,7 +361,7 @@ const char *oscap_source_get_schema_version(struct oscap_source *source)
360361
}
361362
switch (oscap_source_get_scap_type(source)) {
362363
case OSCAP_DOCUMENT_SDS:
363-
source->origin.version = oscap_strdup("1.2");
364+
source->origin.version = ds_sds_detect_version(reader);
364365
break;
365366
case OSCAP_DOCUMENT_ARF:
366367
source->origin.version = oscap_strdup("1.1");

src/source/validate.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ struct oscap_schema_table_entry OSCAP_SCHEMAS_TABLE[] = {
220220
{OSCAP_DOCUMENT_XCCDF_TAILORING, "1.2", "xccdf/1.2/xccdf_1.2.xsd"},
221221
{OSCAP_DOCUMENT_XCCDF_TAILORING, "1.1", "xccdf/1.1-tailoring/xccdf-1.1-tailoring.xsd"}, // unofficial openscap extension!
222222
{OSCAP_DOCUMENT_SDS, "1.2", "sds/1.2/scap-source-data-stream_1.2.xsd"},
223+
{OSCAP_DOCUMENT_SDS, "1.3", "sds/1.3/scap-source-data-stream_1.3.xsd"},
223224
{OSCAP_DOCUMENT_ARF, "1.1", "arf/1.1/asset-reporting-format_1.1.0.xsd"},
224225
{OSCAP_DOCUMENT_CPE_DICTIONARY, "2.0", "cpe/2.0/cpe-dictionary_2.0.xsd"},
225226
{OSCAP_DOCUMENT_CPE_DICTIONARY, "2.1", "cpe/2.1/cpe-dictionary_2.1.xsd"},

0 commit comments

Comments
 (0)