Skip to content

Commit 4fc819d

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 a73e37a commit 4fc819d

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

src/DS/sds.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,3 +1233,29 @@ int ds_sds_compose_from_xccdf(const char *xccdf_file, const char *target_datastr
12331233
xmlFreeDoc(doc);
12341234
return 0;
12351235
}
1236+
1237+
char *ds_sds_detect_version(xmlTextReader *reader)
1238+
{
1239+
/* find root element */
1240+
while (xmlTextReaderRead(reader) == 1 && xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT)
1241+
;
1242+
1243+
char *element_name = (char *) xmlTextReaderConstLocalName(reader);
1244+
if (!element_name) {
1245+
oscap_setxmlerr(xmlGetLastError());
1246+
return NULL;
1247+
}
1248+
if (strcmp(element_name, "data-stream-collection")) {
1249+
oscap_seterr(OSCAP_EFAMILY_OSCAP,
1250+
"Expected root element name for SCAP source datastream is" \
1251+
"'data-stream-collection' but actual root element name is '%s'.",
1252+
element_name);
1253+
return NULL;
1254+
}
1255+
char *schematron_version = (char *) xmlTextReaderGetAttribute(reader, BAD_CAST "schematron-version");
1256+
if (!schematron_version) {
1257+
oscap_setxmlerr(xmlGetLastError());
1258+
return NULL;
1259+
}
1260+
return schematron_version;
1261+
}

src/DS/sds_priv.h

Lines changed: 3 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"
@@ -49,4 +50,6 @@ xmlNodePtr ds_sds_find_component_ref(xmlNodePtr datastream, const char *id);
4950

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

53+
char *ds_sds_detect_version(xmlTextReader *reader);
54+
5255
#endif

src/source/oscap_source.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ const char *oscap_source_get_schema_version(struct oscap_source *source)
363363
}
364364
switch (oscap_source_get_scap_type(source)) {
365365
case OSCAP_DOCUMENT_SDS:
366-
source->origin.version = oscap_strdup("1.2");
366+
source->origin.version = ds_sds_detect_version(reader);
367367
break;
368368
case OSCAP_DOCUMENT_ARF:
369369
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
@@ -225,6 +225,7 @@ struct oscap_schema_table_entry OSCAP_SCHEMAS_TABLE[] = {
225225
{OSCAP_DOCUMENT_XCCDF_TAILORING, "1.2", "xccdf/1.2/xccdf_1.2.xsd"},
226226
{OSCAP_DOCUMENT_XCCDF_TAILORING, "1.1", "xccdf/1.1-tailoring/xccdf-1.1-tailoring.xsd"}, // unofficial openscap extension!
227227
{OSCAP_DOCUMENT_SDS, "1.2", "sds/1.2/scap-source-data-stream_1.2.xsd"},
228+
{OSCAP_DOCUMENT_SDS, "1.3", "sds/1.3/scap-source-data-stream_1.3.xsd"},
228229
{OSCAP_DOCUMENT_ARF, "1.1", "arf/1.1/asset-reporting-format_1.1.0.xsd"},
229230
{OSCAP_DOCUMENT_CPE_DICTIONARY, "2.0", "cpe/2.0/cpe-dictionary_2.0.xsd"},
230231
{OSCAP_DOCUMENT_CPE_DICTIONARY, "2.1", "cpe/2.1/cpe-dictionary_2.1.xsd"},

0 commit comments

Comments
 (0)