Debian Codegraph @

src/source/schematron.c

raw symbols SCIP
1 /*
2 * Copyright 2014 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <string.h>
26 #ifdef OS_WINDOWS
27 #include <io.h>
28 #else
29 #include <unistd.h>
30 #endif
31
32 #include <libxml/xpath.h>
33 #include <libxml/xpathInternals.h>
34 #include "common/_error.h"
35 #include "common/debug_priv.h"
36 #include "common/list.h"
37 #include "common/util.h"
38 #include "ds_sds_session.h"
39 #include "DS/ds_sds_session_priv.h"
40 #include "oscap.h"
41 #include "oscap_helpers.h"
42 #include "oscap_source.h"
43 #include "source/oscap_source_priv.h"
44 #include "source/schematron_priv.h"
45 #include "source/validate_priv.h"
46 #include "source/xslt_priv.h"
47
48 struct oscap_schema_table_entry OSCAP_SCHEMATRON_TABLE[] = {
49 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.3", "oval/5.3/oval-definitions-schematron.xsl"},
50 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.4", "oval/5.4/oval-definitions-schematron.xsl"},
51 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.5", "oval/5.5/oval-definitions-schematron.xsl"},
52 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.6", "oval/5.6/oval-definitions-schematron.xsl"},
53 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.7", "oval/5.7/oval-definitions-schematron.xsl"},
54 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.8", "oval/5.8/oval-definitions-schematron.xsl"},
55 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.9", "oval/5.9/oval-definitions-schematron.xsl"},
56 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.10", "oval/5.10/oval-definitions-schematron.xsl"},
57 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.10.1", "oval/5.10.1/oval-definitions-schematron.xsl"},
58 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.11", "oval/5.11/oval-definitions-schematron.xsl"},
59 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.11.1", "oval/5.11.1/oval-definitions-schematron.xsl"},
60 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.11.2", "oval/5.11.2/oval-definitions-schematron.xsl"},
61 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.11.3", "oval/5.11.3/oval-definitions-schematron.xsl"},
62 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.3", "oval/5.3/oval-system-characteristics-schematron.xsl"},
63 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.4", "oval/5.4/oval-system-characteristics-schematron.xsl"},
64 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.5", "oval/5.5/oval-system-characteristics-schematron.xsl"},
65 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.6", "oval/5.6/oval-system-characteristics-schematron.xsl"},
66 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.7", "oval/5.7/oval-system-characteristics-schematron.xsl"},
67 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.8", "oval/5.8/oval-system-characteristics-schematron.xsl"},
68 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.9", "oval/5.9/oval-system-characteristics-schematron.xsl"},
69 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.10", "oval/5.10/oval-system-characteristics-schematron.xsl"},
70 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.10.1", "oval/5.10.1/oval-system-characteristics-schematron.xsl"},
71 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.11", "oval/5.11/oval-system-characteristics-schematron.xsl"},
72 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.11.1", "oval/5.11.1/oval-system-characteristics-schematron.xsl"},
73 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.11.2", "oval/5.11.2/oval-system-characteristics-schematron.xsl"},
74 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.11.3", "oval/5.11.3/oval-system-characteristics-schematron.xsl"},
75 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.3", "oval/5.3/oval-results-schematron.xsl"},
76 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.4", "oval/5.4/oval-results-schematron.xsl"},
77 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.5", "oval/5.5/oval-results-schematron.xsl"},
78 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.6", "oval/5.6/oval-results-schematron.xsl"},
79 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.7", "oval/5.7/oval-results-schematron.xsl"},
80 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.8", "oval/5.8/oval-results-schematron.xsl"},
81 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.9", "oval/5.9/oval-results-schematron.xsl"},
82 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.10", "oval/5.10/oval-results-schematron.xsl"},
83 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.10.1", "oval/5.10.1/oval-results-schematron.xsl"},
84 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.11", "oval/5.11/oval-results-schematron.xsl"},
85 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.11.1", "oval/5.11.1/oval-results-schematron.xsl"},
86 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.11.2", "oval/5.11.2/oval-results-schematron.xsl"},
87 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.11.3", "oval/5.11.3/oval-results-schematron.xsl"},
88 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.3", "oval/5.3/oval-variables-schematron.xsl"},
89 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.4", "oval/5.4/oval-variables-schematron.xsl"},
90 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.5", "oval/5.5/oval-variables-schematron.xsl"},
91 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.6", "oval/5.6/oval-variables-schematron.xsl"},
92 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.7", "oval/5.7/oval-variables-schematron.xsl"},
93 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.8", "oval/5.8/oval-variables-schematron.xsl"},
94 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.9", "oval/5.9/oval-variables-schematron.xsl"},
95 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.10", "oval/5.10/oval-variables-schematron.xsl"},
96 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.10.1", "oval/5.10.1/oval-variables-schematron.xsl"},
97 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.11", "oval/5.11/oval-variables-schematron.xsl"},
98 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.11.1", "oval/5.11.1/oval-variables-schematron.xsl"},
99 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.11.2", "oval/5.11.2/oval-variables-schematron.xsl"},
100 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.11.3", "oval/5.11.3/oval-variables-schematron.xsl"},
101 {OSCAP_DOCUMENT_OVAL_DIRECTIVES, "5.8", "oval/5.8/oval-directives-schematron.xsl"},
102 {OSCAP_DOCUMENT_OVAL_DIRECTIVES, "5.9", "oval/5.9/oval-directives-schematron.xsl"},
103 {OSCAP_DOCUMENT_OVAL_DIRECTIVES, "5.10", "oval/5.10/oval-directives-schematron.xsl"},
104 {OSCAP_DOCUMENT_OVAL_DIRECTIVES, "5.10.1", "oval/5.10.1/oval-directives-schematron.xsl"},
105 {OSCAP_DOCUMENT_OVAL_DIRECTIVES, "5.11", "oval/5.11/oval-directives-schematron.xsl"},
106 {OSCAP_DOCUMENT_OVAL_DIRECTIVES, "5.11.1", "oval/5.11.1/oval-directives-schematron.xsl"},
107 {OSCAP_DOCUMENT_OVAL_DIRECTIVES, "5.11.2", "oval/5.11.2/oval-directives-schematron.xsl"},
108 {OSCAP_DOCUMENT_OVAL_DIRECTIVES, "5.11.3", "oval/5.11.3/oval-directives-schematron.xsl"},
109 {OSCAP_DOCUMENT_XCCDF, "1.2", "xccdf/1.2/xccdf_1.2-schematron.xsl"},
110 {OSCAP_DOCUMENT_SDS, "1.3", "sds/1.3/source-data-stream-1.3.xsl"}
111 };
112
113 static int _validate_sds_components(struct oscap_source *source)
114 {
115 int ret = 0;
116 struct ds_sds_session *session = ds_sds_session_new_from_source(source);
117 if (ds_sds_session_register_component_with_dependencies(session, "checklists", NULL, NULL) != 0) {
118 return -1;
119 }
120 if (ds_sds_session_register_component_with_dependencies(session, "checks", NULL, NULL) != 0) {
121 return -1;
122 }
123 struct oscap_htable *component_sources = ds_sds_session_get_component_sources(session);
124 struct oscap_htable_iterator *it = oscap_htable_iterator_new(component_sources);
125 while (oscap_htable_iterator_has_more(it)) {
126 struct oscap_source *cs = oscap_htable_iterator_next_value(it);
127 const char *type = oscap_document_type_to_string(oscap_source_get_scap_type(cs));
128 const char *origin = oscap_source_readable_origin(cs);
129 printf("Starting schematron validation of %s component '%s':\n", type, origin);
130 int component_result = oscap_source_validate_schematron_priv(cs,
131 oscap_source_get_scap_type(cs),
132 oscap_source_get_schema_version(cs));
133 printf("Schematron validation of %s component '%s': %s\n\n", type, origin, component_result == 0 ? "PASS" : "FAIL");
134 if (component_result != 0) {
135 ret = component_result;
136 }
137 }
138 oscap_htable_iterator_free(it);
139 ds_sds_session_free(session);
140 return ret;
141 }
142
143 static char *_xcf_resolve_in_catalog(xmlNodePtr resolver_node, const char *uri, xmlXPathContextPtr context)
144 {
145 if (*uri == '#') {
146 return oscap_strdup(uri);
147 }
148 if (resolver_node == NULL) {
149 return NULL;
150 }
151 if (strcmp((char *)resolver_node->name, "uri") == 0) {
152 char *name = (char *) xmlGetProp(resolver_node, BAD_CAST "name");
153 if (name != NULL && strcmp(name, uri) == 0) {
154 char *resolver_node_uri = (char *) xmlGetProp(resolver_node, BAD_CAST "uri");
155 free(name);
156 return resolver_node_uri;
157 } else {
158 free(name);
159 return _xcf_resolve_in_catalog(resolver_node->next, uri, context);
160 }
161 } else if (strcmp((char *)resolver_node->name, "rewriteURI") == 0) {
162 char *uri_start_string = (char *) xmlGetProp(resolver_node, BAD_CAST "uriStartString");
163 if (oscap_str_startswith(uri, uri_start_string)) {
164 char *rewrite_prefix = (char *) xmlGetProp(resolver_node, BAD_CAST "rewritePrefix");
165 char *concat = oscap_sprintf("%s%s", rewrite_prefix, uri + strlen(uri_start_string) + 1);
166 free(rewrite_prefix);
167 free(uri_start_string);
168 return concat;
169 } else {
170 free(uri_start_string);
171 return _xcf_resolve_in_catalog(resolver_node->next, uri, context);
172 }
173 }
174 return NULL;
175 }
176
177 static xmlNodePtr _xcf_get_component_ref(xmlNodePtr catalog, const char *uri, xmlXPathContextPtr context)
178 {
179 char *component_ref_uri = _xcf_resolve_in_catalog(catalog->children->next, uri, context);
180 if (component_ref_uri == NULL) {
181 dD("Can't get component_ref URI using check-content-ref href='%s'", uri);
182 return NULL;
183 }
184 char *cref = component_ref_uri + 1;
185 xmlNodePtr component_ref_node = NULL;
186 char *xpath = oscap_sprintf("ancestor::ds:data-stream//ds:component-ref[@id='%s']", cref);
187 xmlXPathObjectPtr component_refs = xmlXPathNodeEval(catalog, BAD_CAST xpath, context);
188 if (component_refs != NULL && component_refs->nodesetval != NULL && component_refs->nodesetval->nodeNr == 1) {
189 component_ref_node = component_refs->nodesetval->nodeTab[0];
190 }
191 if (component_ref_node == NULL) {
192 dD("component-ref '%s' not found", cref);
193 }
194 xmlXPathFreeObject(component_refs);
195 free(xpath);
196 free(component_ref_uri);
197
198 return component_ref_node;
199 }
200
201 static xmlNodePtr _xcf_get_component(xmlNodePtr component_ref_node, xmlXPathContextPtr context)
202 {
203 char *xlink_href = (char *) xmlGetNsProp(component_ref_node, BAD_CAST "href", BAD_CAST "http://www.w3.org/1999/xlink");
204 char *xpath = oscap_sprintf("ancestor::ds:data-stream-collection//ds:component[@id='%s']", xlink_href + 1);
205 free(xlink_href);
206 xmlXPathObjectPtr components = xmlXPathNodeEval(component_ref_node, BAD_CAST xpath, context);
207 xmlNodePtr component = NULL;
208 if (components != NULL && components->nodesetval != NULL && components->nodesetval->nodeNr == 1) {
209 component = components->nodesetval->nodeTab[0];
210 }
211 free(xpath);
212 xmlXPathFreeObject(components);
213 return component;
214 }
215
216 static xmlNodePtr _find_catalog(xmlNodePtr component_ref_node)
217 {
218 /* find $m/catalog */
219 xmlNodePtr catalog = NULL;
220 xmlNodePtr cur = component_ref_node->children;
221 while (cur != NULL) {
222 if (!xmlStrcmp(cur->name, BAD_CAST "catalog")) {
223 catalog = cur;
224 break;
225 }
226 cur = cur->next;
227 }
228 return catalog;
229 }
230
231 static bool _xcf_is_external_ref(xmlNodePtr catalog, const char *uri, xmlXPathContextPtr context)
232 {
233 xmlNodePtr comp_ref = _xcf_get_component_ref(catalog, uri, context);
234 bool res = false;
235 if (comp_ref != NULL) {
236 char *xlink_href = (char *) xmlGetNsProp(comp_ref, BAD_CAST "href", BAD_CAST "http://www.w3.org/1999/xlink");
237 if (!oscap_str_startswith(xlink_href, "#"))
238 res = true;
239 free(xlink_href);
240 }
241 return res;
242 }
243
244 static bool _req_src_236_2_sub6(xmlNodePtr component, xmlXPathContextPtr context, const char *name, const char *rule_id)
245 {
246 bool exists_ocil = false;
247 char *ocil_xpath = oscap_sprintf(".//ocil:questionnaire[@id='%s']", name);
248 xmlXPathObjectPtr ocil_questionnaires = xmlXPathNodeEval(component, BAD_CAST ocil_xpath, context);
249 if (ocil_questionnaires == NULL || ocil_questionnaires->nodesetval == NULL)
250 goto cleanup;
251 for (int m = 0; m < ocil_questionnaires->nodesetval->nodeNr; m++) {
252 xmlNodePtr ocil_questionnaire = ocil_questionnaires->nodesetval->nodeTab[m];
253 char *ocil_questionnaire_id = (char *) xmlGetProp(ocil_questionnaire, BAD_CAST "id");
254 dD("Found OCIL questionnaire id='%s' for rule id='%s'", ocil_questionnaire_id, rule_id);
255 free(ocil_questionnaire_id);
256 exists_ocil = true;
257 }
258 cleanup:
259 free(ocil_xpath);
260 xmlXPathFreeObject(ocil_questionnaires);
261 return exists_ocil;
262 }
263
264 static bool _req_src_236_2_sub5(xmlNodePtr component, xmlXPathContextPtr context, const char *name, const char *rule_id)
265 {
266 /* exists(xcf:get-component(xcf:get-component-ref($m/cat:catalog, $o/@href))//oval-def:definition[@id eq $o/@name and matches(@class, '^(compliance|patch)$')]) */
267 bool exists_oval = false;
268 char *oval_xpath = oscap_sprintf(".//oval-def:definition[@id='%s' and (@class='compliance' or @class='patch')]", name);
269 xmlXPathObjectPtr oval_definitions = xmlXPathNodeEval(component, BAD_CAST oval_xpath, context);
270 if (oval_definitions == NULL || oval_definitions->nodesetval == NULL)
271 goto cleanup;
272 for (int m = 0; m < oval_definitions->nodesetval->nodeNr; m++) {
273 xmlNodePtr oval_definition = oval_definitions->nodesetval->nodeTab[m];
274 char *oval_def_id = (char *) xmlGetProp(oval_definition, BAD_CAST "id");
275 dD("Found OVAL definition id='%s' for rule id='%s'", oval_def_id, rule_id);
276 free(oval_def_id);
277 exists_oval = true;
278 }
279 cleanup:
280 free(oval_xpath);
281 xmlXPathFreeObject(oval_definitions);
282 return exists_oval;
283 }
284
285 static bool _req_src_236_2_sub4(xmlNodePtr check_content_ref_node, xmlNodePtr catalog, xmlXPathContextPtr context, const char *rule_id)
286 {
287 char *href = (char *) xmlGetProp(check_content_ref_node, BAD_CAST "href");
288 /* xcf:get-component(xcf:get-component-ref($m/cat:catalog, $o/@href) */
289 xmlNodePtr component_ref = _xcf_get_component_ref(catalog, href, context);
290 free(href);
291 if (component_ref == NULL) {
292 dD("component_ref not found");
293 return false;
294 }
295 xmlNodePtr component = _xcf_get_component(component_ref, context);
296 if (component == NULL) {
297 dD("component not found\n");
298 return false;
299 }
300
301 char *name = (char *) xmlGetProp(check_content_ref_node, BAD_CAST "name");
302
303 /* exists(xcf:get-component(xcf:get-component-ref($m/cat:catalog, $o/@href))//oval-def:definition[@id eq $o/@name and matches(@class, '^(compliance|patch)$')]) or exists(xcf:get-component(xcf:get-component-ref($m/cat:catalog, $o/@href))//ocil:questionnaire[@id eq $o/@name])) */
304 bool res = (_req_src_236_2_sub5(component, context, name, rule_id) || _req_src_236_2_sub6(component, context, name, rule_id));
305
306 free(name);
307 return res;
308 }
309
310 static bool _req_src_236_2_sub3(xmlNodePtr rule_node, xmlNodePtr catalog, xmlXPathContextPtr context)
311 {
312 bool res = true;
313 char *rule_id = (char *) xmlGetProp(rule_node, BAD_CAST "id");
314
315 /* note: $n is an xccdf:Rule */
316 /* if(exists($n/xccdf:check[@system eq 'http://oval.mitre.org/XMLSchema/oval-definitions-5' or @system eq 'http://scap.nist.gov/schema/ocil/2']/xccdf:check-content-ref[exists(@name) and not(xcf:is-external-ref($m/cat:catalog, @href) cast as xsd:boolean)])) then ... else true() */
317 xmlXPathObjectPtr check_content_refs = xmlXPathNodeEval(rule_node, BAD_CAST "xccdf:check[@system='http://oval.mitre.org/XMLSchema/oval-definitions-5' or @system='http://scap.nist.gov/schema/ocil/2']/xccdf:check-content-ref[@name]", context);
318 /* TODO: external refs check */
319 if (check_content_refs == NULL || check_content_refs->nodesetval == NULL) {
320 dD("Rule '%s' has no suitable check-content-refs", rule_id);
321 free(rule_id);
322 xmlXPathFreeObject(check_content_refs);
323 return true;
324 }
325
326 /* ... then some $o in $n/xccdf:check[@system eq 'http://oval.mitre.org/XMLSchema/oval-definitions-5' or @system eq 'http://scap.nist.gov/schema/ocil/2']/xccdf:check-content-ref[exists(@name) and not(xcf:is-external-ref($m/cat:catalog, @href) cast as xsd:boolean)] satisfies ... */
327 bool found = false;
328 for (int l = 0; l < check_content_refs->nodesetval->nodeNr; l++) {
329 xmlNodePtr check_content_ref_node = check_content_refs->nodesetval->nodeTab[l];
330 char *href = (char *) xmlGetProp(check_content_ref_node, BAD_CAST "href");
331 if (_xcf_is_external_ref(catalog, href, context)) {
332 free(href);
333 continue;
334 }
335 free(href);
336 if (_req_src_236_2_sub4(check_content_ref_node, catalog, context, rule_id)) {
337 found = true;
338 break;
339 }
340 }
341 if (!found) {
342 res = false;
343 }
344 free(rule_id);
345 xmlXPathFreeObject(check_content_refs);
346 return res;
347 }
348
349 static bool _req_src_236_2_sub2(xmlNodePtr component_ref_node, xmlNodePtr catalog, xmlXPathContextPtr context)
350 {
351 bool res = true;
352 /* every $n in xcf:get-component($m)//xccdf:Rule satisfies ... */
353 xmlNodePtr component_node = _xcf_get_component(component_ref_node, context);
354 if (component_node == NULL) {
355 char *xlink_href = (char *) xmlGetNsProp(component_ref_node, BAD_CAST "href", BAD_CAST "http://www.w3.org/1999/xlink");
356 dD("Can't find component using component-ref '%s'", xlink_href);
357 free(xlink_href);
358 return false;
359 }
360 xmlXPathObjectPtr rules = xmlXPathNodeEval(component_node, BAD_CAST ".//xccdf:Rule", context);
361 if (rules == NULL || rules->nodesetval == NULL) {
362 xmlXPathFreeObject(rules);
363 return res;
364 }
365 for (int i = 0; i < rules->nodesetval->nodeNr; i++) {
366 xmlNodePtr rule_node = rules->nodesetval->nodeTab[i];
367 char *rule_id = (char *) xmlGetProp(rule_node, BAD_CAST "id");
368 dD("Checking xccdf:Rule id='%s'", rule_id);
369 free(rule_id);
370 if (!_req_src_236_2_sub3(rule_node, catalog, context)) {
371 res = false;
372 break;
373 }
374 }
375 xmlXPathFreeObject(rules);
376 return res;
377 }
378
379 static bool _req_src_236_2_sub1(xmlNodePtr data_stream_node, xmlXPathContextPtr context)
380 {
381 int res = true;
382 /* every $m in ds:checklists/ds:component-ref satisfies ... */
383 xmlXPathObjectPtr component_refs = xmlXPathNodeEval(data_stream_node, BAD_CAST "ds:checklists/ds:component-ref", context);
384 if (component_refs == NULL || component_refs->nodesetval == NULL) {
385 xmlXPathFreeObject(component_refs);
386 return res;
387 }
388 for (int i = 0; i < component_refs->nodesetval->nodeNr; i++) {
389 xmlNodePtr component_ref_node = component_refs->nodesetval->nodeTab[i];
390 char *component_ref_id = (char *) xmlGetProp(component_ref_node, BAD_CAST "id");
391 dD("Checking ds:component-ref id='%s'", component_ref_id);
392 free(component_ref_id);
393
394 /* find $m/catalog */
395 xmlNodePtr catalog = _find_catalog(component_ref_node);
396 if (catalog == NULL) {
397 res = false;
398 break;
399 }
400
401 if (!_req_src_236_2_sub2(component_ref_node, catalog, context)) {
402 res = false;
403 break;
404 }
405 }
406 xmlXPathFreeObject(component_refs);
407 return res;
408 }
409
410 static bool _req_src_236_2(xmlXPathContextPtr context)
411 {
412 /*
413 * This function implements the check for SCAPVAL requirement SRC-236-2
414 * which is implemented in XML Schematron file 'source-data-stream-1.3.sch'
415 * assert scap-use-case-conf-verification-rule-ref-oval.
416 *
417 * <sch:assert id="scap-use-case-conf-verification-rule-ref-oval" test="if( @use-case eq 'CONFIGURATION' ) then every $m in ds:checklists/ds:component-ref satisfies every $n in xcf:get-component($m)//xccdf:Rule satisfies (if(exists($n/ xccdf:check[@system eq 'http://oval.mitre.org/XMLSchema/oval-definitions-5' or @system eq 'http://scap.nist.gov/schema/ocil/2']/xccdf:check-content-ref[exists(@name) and not(xcf:is-external-ref($m/cat:catalog, @href) cast as xsd:boolean)])) then some $o in $n/xccdf:check[@system eq 'http://oval.mitre.org/XMLSchema/oval-definitions-5' or @system eq 'http://scap.nist.gov/schema/ocil/2']/xccdf:check-content-ref[exists(@name) and not(xcf:is-external-ref($m/cat:catalog, @href) cast as xsd:boolean)] satisfies (exists(xcf:get-component(xcf:get-component-ref($m/cat:catalog, $o/@href))//oval-def:definition[@id eq $o/@name and matches(@class,'^(compliance|patch)$')]) or exists(xcf:get-component(xcf: get-component-ref($m/cat:catalog, $o/@href))//ocil:questionnaire[@id eq $o/@name])) else true()) else true()">SRC-236-2|scap:data-stream <sch:value-of select="@id"/></sch:assert>
418 */
419
420 bool res = true;
421 /* The parent rule element in the schematron matches all scap:data-stream elements */
422 xmlXPathObjectPtr data_streams = xmlXPathEval(BAD_CAST "//scap:data-stream", context);
423 for (int i = 0; i < data_streams->nodesetval->nodeNr; i++) {
424 xmlNodePtr data_stream_node = data_streams->nodesetval->nodeTab[i];
425 /* The assert applies only to configuration use cases */
426 /* if(@use-case eq 'CONFIGURATION') then ... else true() */
427 char *use_case = (char *) xmlGetProp(data_stream_node, BAD_CAST "use-case");
428 if (use_case == NULL || strcmp(use_case, "CONFIGURATION") != 0) {
429 continue;
430 }
431 free(use_case);
432 if (!_req_src_236_2_sub1(data_stream_node, context)) {
433 char *data_stream_id = (char *) xmlGetProp(data_stream_node, BAD_CAST "id");
434 printf("Error: SRC-236-2|scap:data-stream %s\n", data_stream_id);
435 free(data_stream_id);
436 res = false;
437 break;
438 }
439 }
440 xmlXPathFreeObject(data_streams);
441 return res;
442 }
443
444
445 static bool _req_src_346_1_sub5(xmlNodePtr component, xmlXPathContextPtr context, const char *name)
446 {
447 /* .//oval-def:definition[@id eq $n/@name]) */
448 bool exists_oval = false;
449 char *oval_xpath = oscap_sprintf(".//oval-def:definition[@id='%s']", name);
450 xmlXPathObjectPtr oval_definitions = xmlXPathNodeEval(component, BAD_CAST oval_xpath, context);
451 if (oval_definitions == NULL || oval_definitions->nodesetval == NULL)
452 goto cleanup;
453 for (int m = 0; m < oval_definitions->nodesetval->nodeNr; m++) {
454 xmlNodePtr oval_definition = oval_definitions->nodesetval->nodeTab[m];
455 char *oval_def_id = (char *) xmlGetProp(oval_definition, BAD_CAST "id");
456 dD("Found OVAL definition id='%s'", oval_def_id);
457 free(oval_def_id);
458 exists_oval = true;
459 }
460 cleanup:
461 free(oval_xpath);
462 xmlXPathFreeObject(oval_definitions);
463 return exists_oval;
464 }
465
466 static bool _req_src_346_1_sub4(xmlNodePtr check_content_ref_node, xmlNodePtr catalog, xmlXPathContextPtr context)
467 {
468
469 char *href = (char *) xmlGetProp(check_content_ref_node, BAD_CAST "href");
470 /* xcf:get-component(xcf:get-component-ref($m/cat:catalog, $o/@href) */
471 xmlNodePtr component_ref = _xcf_get_component_ref(catalog, href, context);
472 free(href);
473 if (component_ref == NULL) {
474 dD("component_ref not found");
475 return false;
476 }
477 xmlNodePtr component = _xcf_get_component(component_ref, context);
478 if (component == NULL) {
479 dD("component not found\n");
480 return false;
481 }
482
483 /* not(exists($n/@name)) or exists(xcf:get-component(xcf:get-component-ref($m/cat:catalog, $n/@href))//oval-def:definition[@id eq $n/@name]) */
484 char *name = (char *) xmlGetProp(check_content_ref_node, BAD_CAST "name");
485 bool res = true;
486 if(name != NULL) {
487 res = _req_src_346_1_sub5(component, context, name);
488 }
489
490 free(name);
491 return res;
492 }
493
494 static bool _req_src_346_1_sub3(xmlNodePtr rule_node, xmlNodePtr catalog, xmlXPathContextPtr context)
495 {
496 bool res = true;
497
498 /* xccdf:check[@system eq 'http://oval.mitre.org/XMLSchema/oval-definitions-5']//xccdf:check-content-ref[not(xcf:is-external-ref($m/cat:catalog, @href) cast as xsd:boolean)] */
499 xmlXPathObjectPtr check_content_refs = xmlXPathNodeEval(rule_node, BAD_CAST ".//xccdf:Rule/xccdf:check[@system='http://oval.mitre.org/XMLSchema/oval-definitions-5']/xccdf:check-content-ref", context);
500
501 if (check_content_refs == NULL || check_content_refs->nodesetval == NULL) {
502 dD("There are no check-content-refs elements to be checked.");
503 xmlXPathFreeObject(check_content_refs);
504 return true;
505 }
506
507 /* ... (not(exists($n/@name)) or exists(xcf:get-component(xcf:get-component-ref($m/cat:catalog, $n/@href))//oval-def:definition[@id eq $n/@name])) */
508 bool found = true;
509 for (int l = 0; l < check_content_refs->nodesetval->nodeNr; l++) {
510 xmlNodePtr check_content_ref_node = check_content_refs->nodesetval->nodeTab[l];
511 char *href = (char *) xmlGetProp(check_content_ref_node, BAD_CAST "href");
512 if (_xcf_is_external_ref(catalog, href, context)) {
513 free(href);
514 continue;
515 }
516 if (!_req_src_346_1_sub4(check_content_ref_node, catalog, context)) {
517 found = false;
518 break;
519 }
520 }
521 if (!found) {
522 res = false;
523 }
524 xmlXPathFreeObject(check_content_refs);
525 return res;
526 }
527
528 static bool _req_src_346_1_sub2(xmlNodePtr component_ref_node, xmlNodePtr catalog, xmlXPathContextPtr context)
529 {
530 /* every $n in xcf:get-component($m)//xccdf:check satisfies ... */
531 xmlNodePtr component_node = _xcf_get_component(component_ref_node, context);
532 if (component_node == NULL) {
533 char *xlink_href = (char *) xmlGetNsProp(component_ref_node, BAD_CAST "href", BAD_CAST "http://www.w3.org/1999/xlink");
534 dD("Can't find component using component-ref '%s'", xlink_href);
535 free(xlink_href);
536 return false;
537 }
538 if (!_req_src_346_1_sub3(component_node, catalog, context)) {
539 return false;
540 }
541 return true;
542 }
543
544 static bool _req_src_346_1_sub1(xmlNodePtr data_stream_node, xmlXPathContextPtr context)
545 {
546 int res = true;
547 /* every $m in ds:checklists/ds:component-ref satisfies ... */
548 xmlXPathObjectPtr component_refs = xmlXPathNodeEval(data_stream_node, BAD_CAST "ds:checklists/ds:component-ref", context);
549 if (component_refs == NULL || component_refs->nodesetval == NULL) {
550 if (component_refs != NULL)
551 xmlXPathFreeObject(component_refs);
552 return res;
553 }
554 for (int i = 0; i < component_refs->nodesetval->nodeNr; i++) {
555 xmlNodePtr component_ref_node = component_refs->nodesetval->nodeTab[i];
556 char *component_ref_id = (char *) xmlGetProp(component_ref_node, BAD_CAST "id");
557 dD("Checking ds:component-ref id='%s'", component_ref_id);
558 free(component_ref_id);
559
560 /* find $m/catalog */
561 xmlNodePtr catalog = _find_catalog(component_ref_node);
562 if (catalog == NULL) {
563 res = false;
564 break;
565 }
566
567 if (!_req_src_346_1_sub2(component_ref_node, catalog, context)) {
568 res = false;
569 break;
570 }
571 }
572 xmlXPathFreeObject(component_refs);
573 return res;
574 }
575
576 static bool _req_src_346_1(xmlXPathContextPtr context)
577 {
578 /*
579 * This function implements the check for SCAPVAL requirement SRC-346-1
580 * which is implemented in XML Schematron file 'source-data-stream-1.3.sch'
581 * assert scap-general-scap-content-xccdf-check-content-ref-name-not-req.
582 *
583 * <sch:assert id="scap-general-scap-content-xccdf-check-content-ref-name-not-req" test="every $m in ds:checklists/ds:component-ref satisfies every $n in xcf:get-component($m)//xccdf:check[@system eq 'http://oval.mitre.org/XMLSchema/oval-definitions-5']//xccdf:check-content-ref[not(xcf:is-external-ref($m/cat:catalog, @href) cast as xsd:boolean)] satisfies (not(exists($n/@name)) or exists(xcf:get-component(xcf:get-component-ref($m/cat:catalog, $n/@href))//oval-def:definition[@id eq $n/@name]))">SRC-346-1|scap:data-stream <sch:value-of select="@id"/></sch:assert>
584 */
585
586 bool res = true;
587 /* The parent rule element in the schematron matches all scap:data-stream elements */
588 xmlXPathObjectPtr data_streams = xmlXPathEval(BAD_CAST "//scap:data-stream", context);
589 for (int i = 0; i < data_streams->nodesetval->nodeNr; i++) {
590 xmlNodePtr data_stream_node = data_streams->nodesetval->nodeTab[i];
591 if (!_req_src_346_1_sub1(data_stream_node, context)) {
592 char *data_stream_id = (char *) xmlGetProp(data_stream_node, BAD_CAST "id");
593 printf("Error: SRC-346-1|scap:data-stream %s\n", data_stream_id);
594 free(data_stream_id);
595 res = false;
596 break;
597 }
598 }
599 xmlXPathFreeObject(data_streams);
600 return res;
601 }
602
603 static int _additional_schematron_checks(struct oscap_source *source)
604 {
605 xmlDocPtr doc = oscap_source_get_xmlDoc(source);
606 xmlXPathContextPtr context = xmlXPathNewContext(doc);
607 if (context == NULL) {
608 oscap_seterr(OSCAP_EFAMILY_XML, "Error in xmlXPathNewContext");
609 return -1;
610 }
611 xmlXPathRegisterNs(context, BAD_CAST "xccdf", BAD_CAST "http://checklists.nist.gov/xccdf/1.2");
612 xmlXPathRegisterNs(context, BAD_CAST "scap", BAD_CAST "http://scap.nist.gov/schema/scap/source/1.2");
613 xmlXPathRegisterNs(context, BAD_CAST "ds", BAD_CAST "http://scap.nist.gov/schema/scap/source/1.2");
614 xmlXPathRegisterNs(context, BAD_CAST "xlink", BAD_CAST "http://www.w3.org/1999/xlink");
615 xmlXPathRegisterNs(context, BAD_CAST "oval-def", BAD_CAST "http://oval.mitre.org/XMLSchema/oval-definitions-5");
616 xmlXPathRegisterNs(context, BAD_CAST "ocil", BAD_CAST "http://scap.nist.gov/schema/ocil/2.0");
617
618 int res = 0;
619 /* Assert ID: scap-use-case-conf-verification-benchmark-one-rule-ref-oval-ocil */
620 if (!_req_src_236_2(context))
621 res = 1;
622 /* Assert ID: scap-general-scap-content-xccdf-check-content-ref-name-not-req */
623 if (!_req_src_346_1(context))
624 res = 1;
625
626 xmlXPathFreeContext(context);
627 return res;
628 }
629
630 static int _run_schematron_with_warnings_and_errors(struct oscap_source *source, const char *schema_path)
631 {
632 int validity = 0;
633 const char *params[] = { NULL };
634 char *xslt_output = oscap_source_apply_xslt_path_mem(source, schema_path, params, oscap_path_to_schemas());
635 if (xslt_output == NULL) {
636 return 1;
637 }
638 char *err_substr = strstr(xslt_output, "Error:");
639 if (err_substr != NULL) {
640 /* "Error:" found in schematron output */
641 validity = 1;
642 }
643 puts(xslt_output);
644 free(xslt_output);
645 return validity;
646 }
647
648 static int _sds_schematron_validation(struct oscap_source *source, const char *schema_path)
649 {
650 int validity = 0;
651 printf("Starting global schematron validation using the source data stream schematron\n");
652
653 if (_additional_schematron_checks(source) != 0) {
654 validity = 1;
655 }
656 if (_run_schematron_with_warnings_and_errors(source, schema_path) != 0) {
657 validity = 1;
658 }
659 printf("Global schematron validation using the source data stream schematron: %s\n\n", validity == 0 ? "PASS" : "FAIL");
660 return validity;
661 }
662
663 int oscap_source_validate_schematron_priv(struct oscap_source *source, oscap_document_type_t scap_type, const char *version)
664 {
665 const char *origin = oscap_source_readable_origin(source);
666 if (version == NULL) {
667 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not determine document version for %s",
668 origin);
669 return -1;
670 }
671
672 /* Skip document types that don't have a schematron available */
673 if (scap_type == OSCAP_DOCUMENT_CPE_LANGUAGE ||
674 scap_type == OSCAP_DOCUMENT_CPE_DICTIONARY ||
675 scap_type == OSCAP_DOCUMENT_SCE_RESULT ||
676 scap_type == OSCAP_DOCUMENT_OCIL) {
677 printf("Skipped\n");
678 return 0;
679 }
680 if (scap_type == OSCAP_DOCUMENT_XCCDF && !strcmp(version, "1.1")) {
681 printf("Skipped\n");
682 return 0;
683 }
684
685 /* find a right schematron file */
686 const char *schematron_path = NULL;
687 for (struct oscap_schema_table_entry *entry = OSCAP_SCHEMATRON_TABLE; entry->doc_type != 0; ++entry) {
688 if (entry->doc_type == scap_type && !strcmp(entry->schema_version, version)) {
689 schematron_path = entry->schema_path;
690 break;
691 }
692 }
693 if (schematron_path == NULL) {
694 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Schematron rules not found when trying to validate '%s'", origin);
695 return -1;
696 }
697
698 /* validate */
699 int ret = 0;
700 if (scap_type == OSCAP_DOCUMENT_SDS) {
701 int component_validity = _validate_sds_components(source);
702 int global_validity = _sds_schematron_validation(source, schematron_path);
703 if (component_validity != 0 || global_validity != 0) {
704 ret = 1;
705 }
706 printf("Complete result of schematron validation of '%s': %s\n", origin, ret == 0 ? "PASS" : "FAIL");
707 } else if (scap_type == OSCAP_DOCUMENT_XCCDF) {
708 ret = _run_schematron_with_warnings_and_errors(source, schematron_path);
709 } else {
710 const char *params[] = { NULL };
711 char *xslt_output = oscap_source_apply_xslt_path_mem(source, schematron_path, params, oscap_path_to_schemas());
712 if (xslt_output != NULL) {
713 ret = 1;
714 puts(xslt_output);
715 }
716 free(xslt_output);
717 }
718 return ret;
719 }