Debian Codegraph @

src/DS/rds.c

raw symbols SCIP
1 /*
2 * Copyright 2012--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 * Authors:
20 * Martin Preisler <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "public/scap_ds.h"
28 #include "public/oscap_text.h"
29
30 #include "common/_error.h"
31 #include "common/util.h"
32 #include "common/list.h"
33 #include "common/debug_priv.h"
34
35 #include "ds_common.h"
36 #include "ds_rds_session.h"
37 #include "ds_rds_session_priv.h"
38 #include "rds_priv.h"
39 #include "sds_priv.h"
40 #include "source/public/oscap_source.h"
41 #include "source/oscap_source_priv.h"
42
43 #include <sys/stat.h>
44 #include <time.h>
45 #include <string.h>
46
47 #include <libxml/parser.h>
48 #include <libxml/tree.h>
49 #include <libxml/xpath.h>
50 #include <libxml/xpathInternals.h>
51 #include "oscap_helpers.h"
52
53 static const char* arf_ns_uri = "http://scap.nist.gov/schema/asset-reporting-format/1.1";
54 static const char* core_ns_uri = "http://scap.nist.gov/schema/reporting-core/1.1";
55 static const char* arfvocab_ns_uri = "http://scap.nist.gov/specifications/arf/vocabulary/relationships/1.0#";
56 static const char* ai_ns_uri = "http://scap.nist.gov/schema/asset-identification/1.1";
57 static const char* xlink_ns_uri = "http://www.w3.org/1999/xlink";
58
59
60 xmlNode *ds_rds_lookup_container(xmlDocPtr doc, const char *container_name)
61 {
62 xmlNodePtr root = xmlDocGetRootElement(doc);
63 xmlNodePtr ret = NULL;
64
65 if (root == NULL)
66 return NULL;
67
68 xmlNodePtr candidate = root->children;
69
70 for (; candidate != NULL; candidate = candidate->next)
71 {
72 if (candidate->type != XML_ELEMENT_NODE)
73 continue;
74
75 if (strcmp((const char*)(candidate->name), container_name) == 0) {
76 ret = candidate;
77 break;
78 }
79 }
80
81 return ret;
82 }
83
84 xmlNode *ds_rds_lookup_component(xmlDocPtr doc, const char *container_name, const char *component_name, const char *id)
85 {
86 xmlNodePtr container = ds_rds_lookup_container(doc, container_name);
87 xmlNodePtr component = NULL;
88
89 if (container == NULL)
90 return NULL;
91
92 for (xmlNode *candidate = container->children; candidate != NULL; candidate = candidate->next) {
93 if (candidate->type != XML_ELEMENT_NODE)
94 continue;
95
96 if (!oscap_streq((const char*)(candidate->name), component_name))
97 continue;
98
99 char* candidate_id = (char*)xmlGetProp(candidate, BAD_CAST "id");
100 if (oscap_streq(candidate_id, id)) {
101 component = candidate;
102 xmlFree(candidate_id);
103 break;
104 }
105 xmlFree(candidate_id);
106 }
107 return component;
108 }
109
110 static xmlNodePtr ds_rds_get_inner_content(xmlDocPtr doc, xmlNodePtr parent_node)
111 {
112 xmlNodePtr candidate = parent_node->children;
113 xmlNodePtr content_node = NULL;
114
115 for (; candidate != NULL; candidate = candidate->next)
116 {
117 if (candidate->type != XML_ELEMENT_NODE)
118 continue;
119
120 if (strcmp((const char*)(candidate->name), "content") == 0) {
121 content_node = candidate;
122 break;
123 }
124 }
125
126 if (!content_node) {
127 oscap_seterr(OSCAP_EFAMILY_XML, "Given ARF node has no 'arf:content' node inside!");
128 return NULL;
129 }
130
131 return content_node;
132 }
133
134 int ds_rds_dump_arf_content(struct ds_rds_session *session, const char *container_name, const char *component_name, const char *content_id)
135 {
136 xmlDoc *doc = ds_rds_session_get_xmlDoc(session);
137 xmlNodePtr parent_node = ds_rds_lookup_component(doc, container_name, component_name, content_id);
138 if (!parent_node) {
139 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not find any %s of id '%s'", component_name, content_id);
140 return -1;
141 }
142
143 xmlNodePtr content_node = ds_rds_get_inner_content(NULL, parent_node);
144
145 if (!content_node)
146 return -1;
147
148 xmlNodePtr candidate = content_node->children;
149 xmlNodePtr inner_root = NULL;
150
151 for (; candidate != NULL; candidate = candidate->next)
152 {
153 if (candidate->type != XML_ELEMENT_NODE)
154 continue;
155
156 if (inner_root) {
157 oscap_seterr(OSCAP_EFAMILY_XML, "There are multiple nodes inside an 'arf:content' node. "
158 "Only the last one will be used!");
159 }
160
161 inner_root = candidate;
162 }
163 if (inner_root == NULL) {
164 oscap_seterr(OSCAP_EFAMILY_XML, "Could not found any child inside 'arf:content' node when looking for %s.",
165 content_id);
166 return -1;
167 }
168
169 // We assume that arf:content is XML. This is reasonable because both
170 // reports and report requests are XML documents.
171 xmlDoc *new_doc = ds_doc_from_foreign_node(inner_root, ds_rds_session_get_xmlDoc(session));
172 char *target_file = oscap_sprintf("%s/%s.xml", ds_rds_session_get_target_dir(session), component_name);
173 struct oscap_source *source = oscap_source_new_from_xmlDoc(new_doc, target_file);
174 free(target_file);
175 return ds_rds_session_register_component_source(session, content_id, source);
176 }
177
178 xmlNodePtr ds_rds_create_report(xmlDocPtr target_doc, xmlNodePtr reports_node, xmlDocPtr source_doc, const char* report_id)
179 {
180 xmlNsPtr arf_ns = xmlSearchNsByHref(target_doc, xmlDocGetRootElement(target_doc), BAD_CAST arf_ns_uri);
181
182 xmlNodePtr report = xmlNewNode(arf_ns, BAD_CAST "report");
183 xmlSetProp(report, BAD_CAST "id", BAD_CAST report_id);
184
185 xmlNodePtr report_content = xmlNewNode(arf_ns, BAD_CAST "content");
186 xmlAddChild(report, report_content);
187
188 xmlDOMWrapCtxtPtr wrap_ctxt = xmlDOMWrapNewCtxt();
189 xmlNodePtr res_node = NULL;
190 xmlDOMWrapCloneNode(wrap_ctxt, source_doc, xmlDocGetRootElement(source_doc),
191 &res_node, target_doc, NULL, 1, 0);
192 xmlAddChild(report_content, res_node);
193 xmlDOMWrapReconcileNamespaces(wrap_ctxt, res_node, 0);
194 xmlDOMWrapFreeCtxt(wrap_ctxt);
195
196 xmlAddChild(reports_node, report);
197
198 return report;
199 }
200
201 static void ds_rds_add_relationship(xmlDocPtr doc, xmlNodePtr relationships,
202 const char* type, const char* subject, const char* ref)
203 {
204 xmlNsPtr core_ns = xmlSearchNsByHref(doc, xmlDocGetRootElement(doc), BAD_CAST core_ns_uri);
205
206 // create relationship between given request and the report
207 xmlNodePtr relationship = xmlNewNode(core_ns, BAD_CAST "relationship");
208 xmlSetProp(relationship, BAD_CAST "type", BAD_CAST type);
209 xmlSetProp(relationship, BAD_CAST "subject", BAD_CAST subject);
210
211 xmlNodePtr ref_node = xmlNewNode(core_ns, BAD_CAST "ref");
212 xmlNodeSetContent(ref_node, BAD_CAST ref);
213 xmlAddChild(relationship, ref_node);
214
215 xmlAddChild(relationships, relationship);
216 }
217
218 static xmlNodePtr ds_rds_add_ai_from_xccdf_results(xmlDocPtr doc, xmlNodePtr assets,
219 xmlDocPtr xccdf_result_doc)
220 {
221 xmlNsPtr arf_ns = xmlSearchNsByHref(doc, xmlDocGetRootElement(doc), BAD_CAST arf_ns_uri);
222 xmlNsPtr ai_ns = xmlSearchNsByHref(doc, xmlDocGetRootElement(doc), BAD_CAST ai_ns_uri);
223
224 xmlNodePtr asset = xmlNewNode(arf_ns, BAD_CAST "asset");
225
226 // Lets figure out a unique asset identification
227 // The format is: "asset%i" where %i is a increasing integer suffix
228 //
229 // We use a very simple optimization, we know that assets will be "ordered"
230 // by their @id because we are adding them there in that order.
231 // Whenever we get a collision we can simply bump the suffix and continue,
232 // no need to go back and check the previous assets.
233
234 xmlNodePtr child_asset = assets->children;
235
236 unsigned int suffix = 0;
237 for (; child_asset != NULL; child_asset = child_asset->next)
238 {
239 if (child_asset->type != XML_ELEMENT_NODE)
240 continue;
241
242 if (strcmp((const char*)(child_asset->name), "asset") != 0)
243 continue;
244
245 char* id_candidate = oscap_sprintf("asset%i", suffix);
246 xmlChar* id = xmlGetProp(child_asset, BAD_CAST "id");
247
248 if (strcmp(id_candidate, (const char*)id) == 0)
249 {
250 suffix++;
251 }
252 xmlFree(id);
253 free(id_candidate);
254 }
255
256 char* id = oscap_sprintf("asset%i", suffix);
257 xmlSetProp(asset, BAD_CAST "id", BAD_CAST id);
258 free(id);
259
260 xmlAddChild(assets, asset);
261
262 xmlNodePtr computing_device = xmlNewNode(ai_ns, BAD_CAST "computing-device");
263 xmlAddChild(asset, computing_device);
264
265 xmlNodePtr connections = xmlNewNode(ai_ns, BAD_CAST "connections");
266 xmlAddChild(computing_device, connections);
267
268 xmlNodePtr test_result = xmlDocGetRootElement(xccdf_result_doc);
269
270 xmlNodePtr test_result_child = test_result->children;
271
272 xmlNodePtr last_fqdn = NULL;
273 xmlNodePtr last_hostname = NULL;
274 for (; test_result_child != NULL; test_result_child = test_result_child->next)
275 {
276 if (test_result_child->type != XML_ELEMENT_NODE)
277 continue;
278
279 if (strcmp((const char*)(test_result_child->name), "target-address") == 0)
280 {
281 xmlNodePtr connection = xmlNewNode(ai_ns, BAD_CAST "connection");
282 xmlAddChild(connections, connection);
283 xmlNodePtr ip_address = xmlNewNode(ai_ns, BAD_CAST "ip-address");
284 xmlAddChild(connection, ip_address);
285
286 xmlChar* content = xmlNodeGetContent(test_result_child);
287
288 // we need to figure out whether the address is IPv4 or IPv6
289 if (strchr((char*)content, '.') != NULL) // IPv4 has to have 4 dots
290 {
291 xmlNewTextChild(ip_address, ai_ns, BAD_CAST "ip-v4", content);
292 }
293 else // IPv6 has semicolons instead of dots
294 {
295 // lets expand the IPv6 to conform to the AI XSD and specification
296 char *expanded_ipv6 = oscap_expand_ipv6((const char*)content);
297 xmlNewTextChild(ip_address, ai_ns, BAD_CAST "ip-v6", BAD_CAST expanded_ipv6);
298 free(expanded_ipv6);
299 }
300 xmlFree(content);
301 }
302 else if (strcmp((const char*)(test_result_child->name), "target-facts") == 0)
303 {
304 xmlNodePtr target_fact_child = test_result_child->children;
305
306 for (; target_fact_child != NULL; target_fact_child = target_fact_child->next)
307 {
308 if (target_fact_child->type != XML_ELEMENT_NODE)
309 continue;
310
311 if (strcmp((const char*)(target_fact_child->name), "fact") != 0)
312 continue;
313
314 xmlChar *name = xmlGetProp(target_fact_child, BAD_CAST "name");
315 if (name) {
316 if (!strcmp((const char*)name, "urn:xccdf:fact:asset:identifier:mac")) {
317 xmlChar *content = xmlNodeGetContent(target_fact_child);
318 xmlNodePtr connection = xmlNewNode(ai_ns, BAD_CAST "connection");
319 xmlAddChild(connections, connection);
320 xmlNewTextChild(connection, ai_ns, BAD_CAST "mac-address", content);
321 xmlFree(content);
322 }
323
324 // Order for the output to be valid: fqdn then hostname, just one of each kind
325
326 if (!strcmp((const char*)name, "urn:xccdf:fact:asset:identifier:fqdn")) {
327 xmlChar *content = xmlNodeGetContent(target_fact_child);
328 xmlNodePtr fqdn = xmlNewNode(ai_ns, BAD_CAST "fqdn");
329 xmlNodeSetContent(fqdn, BAD_CAST content);
330 if (!last_fqdn)
331 last_fqdn = last_hostname ? xmlAddPrevSibling(last_hostname, fqdn) : xmlAddChild(computing_device, fqdn);
332 xmlFree(content);
333 }
334
335 if (!strcmp((const char*)name, "urn:xccdf:fact:asset:identifier:host_name")) {
336 xmlChar *content = xmlNodeGetContent(target_fact_child);
337 xmlNodePtr hostname = xmlNewNode(ai_ns, BAD_CAST "hostname");
338 xmlNodeSetContent(hostname, BAD_CAST content);
339 if (!last_hostname)
340 last_hostname = last_fqdn ? xmlAddNextSibling(last_fqdn, hostname) : xmlAddChild(computing_device, hostname);
341 xmlFree(content);
342 }
343 }
344 xmlFree(name);
345 }
346 }
347 }
348
349 if (xmlGetLastChild(connections) == NULL) {
350 xmlUnlinkNode(connections);
351 xmlFreeNode(connections);
352 }
353
354 return asset;
355 }
356
357 static int ds_rds_report_inject_ai_target_id_ref(xmlDocPtr doc, xmlNodePtr test_result_node, const char *asset_id)
358 {
359 // Now we need to find the right place to inject the target-id-ref element.
360 // It has to come after target, target-address and target-facts elements.
361 // However target-address and target-fact are both optional.
362
363 xmlNodePtr prev_sibling = NULL;
364 xmlNodePtr prev_sibling_candidate = test_result_node->children;
365
366 while (prev_sibling_candidate) {
367 if (prev_sibling_candidate->type == XML_ELEMENT_NODE) {
368 if (strcmp((const char*)prev_sibling_candidate->name, "target") == 0 ||
369 strcmp((const char*)prev_sibling_candidate->name, "target-address") == 0 ||
370 strcmp((const char*)prev_sibling_candidate->name, "target-facts") == 0) {
371
372 prev_sibling = prev_sibling_candidate;
373 }
374 }
375
376 prev_sibling_candidate = prev_sibling_candidate->next;
377 }
378
379 if (!prev_sibling) {
380 oscap_seterr(OSCAP_EFAMILY_XML, "No target element was found in TestResult. "
381 "The most likely reason is that the content is not valid! "
382 "(XCCDF spec states 'target' element as required)");
383 return -1;
384 }
385
386 // We have to make sure we are not injecting a target-id-ref that is there
387 // already. if there is any duplicate, it has to come right after prev_sibling.
388 xmlNodePtr duplicate_candidate = prev_sibling->next;
389 while (duplicate_candidate) {
390 if (duplicate_candidate->type == XML_ELEMENT_NODE) {
391 if (strcmp((const char*)duplicate_candidate->name, "target-id-ref") == 0) {
392 xmlChar* system_attr = xmlGetProp(duplicate_candidate, BAD_CAST "system");
393 xmlChar* name_attr = xmlGetProp(duplicate_candidate, BAD_CAST "name");
394
395 if (strcmp((const char*)system_attr, ai_ns_uri) == 0 &&
396 strcmp((const char*)name_attr, asset_id) == 0) {
397
398 xmlFree(system_attr);
399 xmlFree(name_attr);
400 return 0;
401 }
402
403 xmlFree(system_attr);
404 xmlFree(name_attr);
405 }
406 else {
407 break;
408 }
409 }
410 duplicate_candidate = duplicate_candidate->next;
411 }
412
413 xmlNodePtr target_id_ref = xmlNewNode(prev_sibling->ns, BAD_CAST "target-id-ref");
414 xmlNewProp(target_id_ref, BAD_CAST "system", BAD_CAST ai_ns_uri);
415 xmlNewProp(target_id_ref, BAD_CAST "name", BAD_CAST asset_id);
416 // @href is a required attribute by the XSD! The spec advocates filling it
417 // blank when it's not needed.
418 xmlNewProp(target_id_ref, BAD_CAST "href", BAD_CAST "");
419
420 xmlAddNextSibling(prev_sibling, target_id_ref);
421
422 return 0;
423 }
424
425 static void ds_rds_report_inject_check_content_ref(xmlNodePtr check_content_ref, struct oscap_htable *arf_report_mapping)
426 {
427 if (check_content_ref->type == XML_ELEMENT_NODE) {
428 if (strcmp((const char*)check_content_ref->name, "check-content-ref") == 0) {
429 char *oval_filename = (char *) xmlGetProp(check_content_ref,
430 BAD_CAST "href");
431 if (oval_filename == NULL) {
432 return;
433 }
434 char *report_id = oscap_htable_get(arf_report_mapping, oval_filename);
435 if (report_id == NULL) {
436 free(oval_filename);
437 return;
438 }
439 char *desired_href = oscap_sprintf("#%s", report_id);
440 xmlSetProp(check_content_ref, BAD_CAST "href", BAD_CAST desired_href);
441 free(desired_href);
442 free(oval_filename);
443 }
444 }
445 }
446
447 static void ds_rds_report_inject_rule_result_check_refs(xmlDocPtr doc, xmlNodePtr rule_result, struct oscap_htable *arf_report_mapping)
448 {
449 xmlNodePtr child = rule_result->children;
450
451 while (child) {
452 if (child->type == XML_ELEMENT_NODE) {
453 if (strcmp((const char*)child->name, "complex-check") == 0) {
454 ds_rds_report_inject_rule_result_check_refs(doc, child, arf_report_mapping);
455 } else if (strcmp((const char*)child->name, "check") == 0) {
456 xmlNodePtr check_content_ref = child->children;
457
458 while (check_content_ref) {
459 ds_rds_report_inject_check_content_ref(check_content_ref, arf_report_mapping);
460 check_content_ref = check_content_ref->next;
461 }
462 }
463 }
464
465 child = child->next;
466 }
467 }
468
469 static void ds_rds_report_inject_rule_result_refs(xmlDocPtr doc, xmlNodePtr test_result_node, struct oscap_htable *arf_report_mapping)
470 {
471 xmlNodePtr child = test_result_node->children;
472 while (child) {
473 if (child->type == XML_ELEMENT_NODE) {
474 if (strcmp((const char*)child->name, "rule-result") == 0) {
475 ds_rds_report_inject_rule_result_check_refs(doc, child, arf_report_mapping);
476 }
477 }
478
479 child = child->next;
480 }
481 }
482
483 static int ds_rds_report_inject_refs(xmlDocPtr doc, xmlNodePtr report, const char *asset_id, struct oscap_htable* arf_report_mapping)
484 {
485 xmlNodePtr content_node = ds_rds_get_inner_content(doc, report);
486
487 if (!content_node)
488 return -1;
489
490 if (!content_node->children) {
491 oscap_seterr(OSCAP_EFAMILY_XML, "Given report doesn't contain any data, "
492 "can't inject AI asset target id ref");
493 return -1;
494 }
495
496 xmlNodePtr test_result_node = NULL;
497 xmlNodePtr test_result_candidate = content_node->children;
498 xmlNodePtr inner_element_node = NULL;
499
500 while (test_result_candidate) {
501 if (test_result_candidate->type == XML_ELEMENT_NODE) {
502 inner_element_node = test_result_candidate;
503
504 if (strcmp((const char*)test_result_candidate->name, "TestResult") == 0) {
505 test_result_node = test_result_candidate;
506 break;
507 }
508 }
509
510 test_result_candidate = test_result_candidate->next;
511 }
512
513 if (!inner_element_node) {
514 oscap_seterr(OSCAP_EFAMILY_XML, "Given report doesn't contain any XML element! "
515 "Can't inject AI asset target id ref");
516 return -1;
517 }
518
519 if (!test_result_node) {
520 // TestResult may not be the top level element in the report.
521 // While that is very unusual it is legitimate, lets check child elements.
522
523 // As a rule, we only inject target-id-ref to the last test result
524 // (XML, top-down).
525
526 if (strcmp((const char*)inner_element_node->name, "Benchmark")) {
527 oscap_seterr(OSCAP_EFAMILY_XML, "Top level element of the report isn't TestResult "
528 "or Benchmark, the report is likely invalid!");
529 return -1;
530 }
531
532 if (!inner_element_node->children) {
533 oscap_seterr(OSCAP_EFAMILY_XML, "Top level element of the report isn't TestResult "
534 "and does not contain any children! No TestResult to inject to has been found.");
535 return -1;
536 }
537
538 test_result_candidate = inner_element_node->children;
539 while (test_result_candidate) {
540 if (test_result_candidate->type == XML_ELEMENT_NODE) {
541 if (strcmp((const char*)test_result_candidate->name, "TestResult") == 0) {
542 test_result_node = test_result_candidate;
543 // we intentionally do not break here, we are looking for the
544 // last (top-down) TestResult in the report.
545 //break;
546 }
547 }
548
549 test_result_candidate = test_result_candidate->next;
550 }
551 }
552
553 if (!test_result_node) {
554 oscap_seterr(OSCAP_EFAMILY_XML, "TestResult node to inject to has not been found"
555 "(checked root element and all children of it).");
556 return -1;
557 }
558
559 int ret = ds_rds_report_inject_ai_target_id_ref(doc, test_result_node, asset_id);
560
561 ds_rds_report_inject_rule_result_refs(doc, test_result_node, arf_report_mapping);
562
563 return ret;
564 }
565
566 static void ds_rds_add_xccdf_test_results(xmlDocPtr doc, xmlNodePtr reports,
567 xmlDocPtr xccdf_result_file_doc, xmlNodePtr relationships, xmlNodePtr assets,
568 const char* report_request_id, struct oscap_htable *arf_report_mapping)
569 {
570 xmlNodePtr root_element = xmlDocGetRootElement(xccdf_result_file_doc);
571
572 if (root_element->ns && root_element->ns->href &&
573 oscap_str_endswith((const char*)root_element->ns->href, "xccdf/1.1")) {
574 dW("Exporting ARF from XCCDF 1.1 is not allowed by SCAP specification. "
575 "The resulting ARF will not validate. Convert the input to XCCDF 1.2 "
576 "to get valid ARF results. The xccdf_1.1_to_1.2.xsl transformation."
577 "that ships with OpenSCAP can do that automatically.");
578 }
579
580 // There are 2 possible scenarios here:
581
582 // 1) root element of given xccdf result file doc is a TestResult element
583 // This is the easier scenario, we will just use ds_rds_create_report and
584 // be done with it.
585 if (strcmp((const char*)root_element->name, "TestResult") == 0)
586 {
587 xmlNodePtr report = ds_rds_create_report(doc, reports, xccdf_result_file_doc, "xccdf1");
588 ds_rds_add_relationship(doc, relationships, "arfvocab:createdFor",
589 "xccdf1", report_request_id);
590
591 xmlNodePtr asset = ds_rds_add_ai_from_xccdf_results(doc, assets, xccdf_result_file_doc);
592 char* asset_id = (char*)xmlGetProp(asset, BAD_CAST "id");
593 ds_rds_add_relationship(doc, relationships, "arfvocab:isAbout",
594 "xccdf1", asset_id);
595
596 // We deliberately don't act on errors in inject refs as
597 // these aren't fatal errors.
598 ds_rds_report_inject_refs(doc, report, asset_id, arf_report_mapping);
599
600 xmlFree(asset_id);
601 }
602
603 // 2) the root element is a Benchmark, TestResults are embedded within
604 // We will have to walk through all elements, wrap each TestResult
605 // in a xmlDoc and add them separately
606 else if (strcmp((const char*)root_element->name, "Benchmark") == 0)
607 {
608 unsigned int report_suffix = 1;
609
610 xmlNodePtr candidate_result = root_element->children;
611
612 for (; candidate_result != NULL; candidate_result = candidate_result->next)
613 {
614 if (candidate_result->type != XML_ELEMENT_NODE)
615 continue;
616
617 if (strcmp((const char*)(candidate_result->name), "TestResult") != 0)
618 continue;
619
620 xmlDocPtr wrap_doc = xmlNewDoc(BAD_CAST "1.0");
621
622 xmlDOMWrapCtxtPtr wrap_ctxt = xmlDOMWrapNewCtxt();
623 xmlNodePtr res_node = NULL;
624 xmlDOMWrapCloneNode(wrap_ctxt, xccdf_result_file_doc, candidate_result,
625 &res_node, wrap_doc, NULL, 1, 0);
626 xmlDocSetRootElement(wrap_doc, res_node);
627 xmlDOMWrapReconcileNamespaces(wrap_ctxt, res_node, 0);
628 xmlDOMWrapFreeCtxt(wrap_ctxt);
629
630 char* report_id = oscap_sprintf("xccdf%i", report_suffix++);
631 xmlNodePtr report = ds_rds_create_report(doc, reports, wrap_doc, report_id);
632 ds_rds_add_relationship(doc, relationships, "arfvocab:createdFor",
633 report_id, report_request_id);
634
635 xmlNodePtr asset = ds_rds_add_ai_from_xccdf_results(doc, assets, wrap_doc);
636 char* asset_id = (char*)xmlGetProp(asset, BAD_CAST "id");
637 ds_rds_add_relationship(doc, relationships, "arfvocab:isAbout",
638 report_id, asset_id);
639
640 // We deliberately don't act on errors in inject ref as
641 // these aren't fatal errors.
642 ds_rds_report_inject_refs(doc, report, asset_id, arf_report_mapping);
643
644 xmlFree(asset_id);
645
646 free(report_id);
647
648 xmlFreeDoc(wrap_doc);
649 }
650 }
651
652 else
653 {
654 char* error = oscap_sprintf(
655 "Unknown root element '%s' in given XCCDF result document, expected TestResult or Benchmark.",
656 (const char*)root_element->name);
657
658 oscap_seterr(OSCAP_EFAMILY_XML, "%s", error);
659 free(error);
660 }
661 }
662
663 static int _ds_rds_create_from_dom(xmlDocPtr *ret, xmlDocPtr sds_doc,
664 xmlDocPtr tailoring_doc, const char *tailoring_filepath,
665 char *tailoring_doc_timestamp, xmlDocPtr xccdf_result_file_doc,
666 struct oscap_htable *oval_result_sources,
667 struct oscap_htable *oval_result_mapping,
668 struct oscap_htable *arf_report_mapping,
669 bool clone)
670 {
671 *ret = NULL;
672
673 xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
674 xmlNodePtr root = xmlNewNode(NULL, BAD_CAST "asset-report-collection");
675 xmlDocSetRootElement(doc, root);
676
677 xmlNsPtr arf_ns = xmlNewNs(root, BAD_CAST arf_ns_uri, BAD_CAST "arf");
678 xmlSetNs(root, arf_ns);
679
680 xmlNsPtr core_ns = xmlNewNs(root, BAD_CAST core_ns_uri, BAD_CAST "core");
681 xmlNewNs(root, BAD_CAST ai_ns_uri, BAD_CAST "ai");
682
683 xmlNodePtr relationships = xmlNewNode(core_ns, BAD_CAST "relationships");
684 xmlNewNs(relationships, BAD_CAST arfvocab_ns_uri, BAD_CAST "arfvocab");
685 xmlAddChild(root, relationships);
686
687 xmlNodePtr report_requests = xmlNewNode(arf_ns, BAD_CAST "report-requests");
688 xmlAddChild(root, report_requests);
689
690 xmlNodePtr assets = xmlNewNode(arf_ns, BAD_CAST "assets");
691 xmlAddChild(root, assets);
692
693 xmlNodePtr report_request = xmlNewNode(arf_ns, BAD_CAST "report-request");
694 xmlSetProp(report_request, BAD_CAST "id", BAD_CAST "collection1");
695
696 xmlNodePtr arf_content = xmlNewNode(arf_ns, BAD_CAST "content");
697
698 xmlDOMWrapCtxtPtr sds_wrap_ctxt = xmlDOMWrapNewCtxt();
699 xmlNodePtr sds_res_node = NULL;
700 if (clone) {
701 xmlDOMWrapCloneNode(sds_wrap_ctxt, sds_doc, xmlDocGetRootElement(sds_doc),
702 &sds_res_node, doc, NULL, 1, 0);
703 } else {
704 sds_res_node = xmlDocGetRootElement(sds_doc);
705 xmlDOMWrapAdoptNode(sds_wrap_ctxt, sds_doc, sds_res_node, doc, NULL, 0);
706 }
707 xmlAddChild(arf_content, sds_res_node);
708 xmlDOMWrapReconcileNamespaces(sds_wrap_ctxt, sds_res_node, 0);
709 xmlDOMWrapFreeCtxt(sds_wrap_ctxt);
710
711 if (tailoring_doc && strcmp(tailoring_filepath, "NONEXISTENT")) {
712 char *mangled_tailoring_filepath = ds_sds_mangle_filepath(tailoring_filepath);
713 char *tailoring_component_id = oscap_sprintf("scap_org.open-scap_comp_%s_tailoring", mangled_tailoring_filepath);
714 char *tailoring_component_ref_id = oscap_sprintf("scap_org.open-scap_cref_%s_tailoring", mangled_tailoring_filepath);
715
716 // Need unique id (ref_id) - if generated already exists, then create new one
717 int counter = 0;
718 while (lookup_component_in_collection(sds_res_node, tailoring_component_id) != NULL) {
719 free(tailoring_component_id);
720 tailoring_component_id = oscap_sprintf("scap_org.open-scap_comp_%s_tailoring%03d", mangled_tailoring_filepath, counter++);
721 }
722
723 counter = 0;
724 while (ds_sds_find_component_ref(xmlDocGetRootElement((xmlDocPtr) sds_res_node)->children, tailoring_component_ref_id) != NULL) {
725 free(tailoring_component_ref_id);
726 tailoring_component_ref_id = oscap_sprintf("scap_org.open-scap_cref_%s_tailoring%03d", mangled_tailoring_filepath, counter++);
727 }
728
729 free(mangled_tailoring_filepath);
730
731 xmlDOMWrapCtxtPtr tailoring_wrap_ctxt = xmlDOMWrapNewCtxt();
732 xmlNodePtr tailoring_res_node = NULL;
733 xmlDOMWrapCloneNode(tailoring_wrap_ctxt, tailoring_doc, xmlDocGetRootElement(tailoring_doc),
734 &tailoring_res_node, doc, NULL, 1, 0);
735 xmlNsPtr sds_ns = sds_res_node->ns;
736 xmlNodePtr tailoring_component = xmlNewNode(sds_ns, BAD_CAST "component");
737 xmlSetProp(tailoring_component, BAD_CAST "id", BAD_CAST tailoring_component_id);
738 xmlSetProp(tailoring_component, BAD_CAST "timestamp", BAD_CAST tailoring_doc_timestamp);
739 xmlAddChild(tailoring_component, tailoring_res_node);
740 xmlAddChild(sds_res_node, tailoring_component);
741
742 xmlNodePtr checklists_element = NULL;
743 xmlNodePtr datastream_element = node_get_child_element(sds_res_node, "data-stream");
744 if (datastream_element == NULL) {
745 datastream_element = xmlNewNode(sds_ns, BAD_CAST "data-stream");
746 xmlAddChild(sds_res_node, datastream_element);
747 checklists_element = xmlNewNode(sds_ns, BAD_CAST "checklists");
748 xmlAddChild(datastream_element, checklists_element);
749 }
750 else {
751 checklists_element = node_get_child_element(datastream_element, "checklists");
752 }
753
754 xmlNodePtr tailoring_component_ref = xmlNewNode(sds_ns, BAD_CAST "component-ref");
755 xmlSetProp(tailoring_component_ref, BAD_CAST "id", BAD_CAST tailoring_component_ref_id);
756 free(tailoring_component_ref_id);
757 xmlNsPtr xlink_ns = xmlSearchNsByHref(doc, sds_res_node, BAD_CAST xlink_ns_uri);
758 if (!xlink_ns) {
759 xlink_ns = xmlNewNs(tailoring_component_ref, BAD_CAST xlink_ns_uri, BAD_CAST "xlink");
760 }
761 char *tailoring_cref_href = oscap_sprintf("#%s", tailoring_component_id);
762 free(tailoring_component_id);
763 xmlSetNsProp(tailoring_component_ref, xlink_ns, BAD_CAST "href", BAD_CAST tailoring_cref_href);
764 free(tailoring_cref_href);
765 xmlAddChild(checklists_element, tailoring_component_ref);
766
767 xmlDOMWrapReconcileNamespaces(tailoring_wrap_ctxt, tailoring_res_node, 0);
768 xmlDOMWrapFreeCtxt(tailoring_wrap_ctxt);
769 }
770
771 xmlAddChild(report_request, arf_content);
772
773 xmlAddChild(report_requests, report_request);
774
775 xmlNodePtr reports = xmlNewNode(arf_ns, BAD_CAST "reports");
776
777 ds_rds_add_xccdf_test_results(doc, reports, xccdf_result_file_doc,
778 relationships, assets, "collection1", arf_report_mapping);
779
780 struct oscap_htable_iterator *hit = oscap_htable_iterator_new(arf_report_mapping);
781 while (oscap_htable_iterator_has_more(hit)) {
782 const struct oscap_htable_item *report_mapping_item = oscap_htable_iterator_next(hit);
783 const char *oval_filename = report_mapping_item->key;
784 const char *report_id = report_mapping_item->value;
785 const char *report_file = oscap_htable_get(oval_result_mapping, oval_filename);
786 struct oscap_source *oval_source = oscap_htable_get(oval_result_sources, report_file);
787 xmlDoc *oval_result_doc = oscap_source_get_xmlDoc(oval_source);
788
789 ds_rds_create_report(doc, reports, oval_result_doc, report_id);
790 }
791 oscap_htable_iterator_free(hit);
792
793 xmlAddChild(root, reports);
794
795 *ret = doc;
796 return 0;
797 }
798
799 int ds_rds_create_from_dom(xmlDocPtr *ret, xmlDocPtr sds_doc,
800 xmlDocPtr tailoring_doc, const char *tailoring_filepath,
801 char *tailoring_doc_timestamp, xmlDocPtr xccdf_result_file_doc,
802 struct oscap_htable *oval_result_sources,
803 struct oscap_htable *oval_result_mapping,
804 struct oscap_htable *arf_report_mapping)
805 {
806 return _ds_rds_create_from_dom(ret, sds_doc, tailoring_doc,
807 tailoring_filepath, tailoring_doc_timestamp,
808 xccdf_result_file_doc, oval_result_sources, oval_result_mapping,
809 arf_report_mapping, false);
810 }
811
812 static int ds_rds_create_from_dom_clone(xmlDocPtr *ret, xmlDocPtr sds_doc,
813 xmlDocPtr tailoring_doc, const char *tailoring_filepath,
814 char *tailoring_doc_timestamp, xmlDocPtr xccdf_result_file_doc,
815 struct oscap_htable *oval_result_sources,
816 struct oscap_htable *oval_result_mapping,
817 struct oscap_htable *arf_report_mapping)
818 {
819 return _ds_rds_create_from_dom(ret, sds_doc, tailoring_doc,
820 tailoring_filepath, tailoring_doc_timestamp,
821 xccdf_result_file_doc, oval_result_sources, oval_result_mapping,
822 arf_report_mapping, true);
823 }
824
825 struct oscap_source *ds_rds_create_source(struct oscap_source *sds_source, struct oscap_source *tailoring_source, struct oscap_source *xccdf_result_source, struct oscap_htable *oval_result_sources, struct oscap_htable *oval_result_mapping, struct oscap_htable *arf_report_mapping, const char *target_file)
826 {
827 xmlDoc *sds_doc = oscap_source_get_xmlDoc(sds_source);
828 if (sds_doc == NULL) {
829 return NULL;
830 }
831
832 xmlDoc *result_file_doc = oscap_source_get_xmlDoc(xccdf_result_source);
833 if (result_file_doc == NULL) {
834 return NULL;
835 }
836
837 xmlDoc *tailoring_doc = NULL;
838 char *tailoring_doc_timestamp = NULL;
839 const char *tailoring_filepath = NULL;
840 if (tailoring_source) {
841 tailoring_doc = oscap_source_get_xmlDoc(tailoring_source);
842 if (tailoring_doc == NULL) {
843 return NULL;
844 }
845 tailoring_filepath = oscap_source_get_filepath(tailoring_source);
846 struct stat file_stat;
847 if (stat(tailoring_filepath, &file_stat) == 0) {
848 const size_t max_timestamp_len = 32;
849 struct tm result;
850 tailoring_doc_timestamp = malloc(max_timestamp_len);
851 strftime(tailoring_doc_timestamp, max_timestamp_len, "%Y-%m-%dT%H:%M:%S", localtime_r(&file_stat.st_mtime, &result));
852 }
853 }
854
855 xmlDocPtr rds_doc = NULL;
856
857 if (ds_rds_create_from_dom_clone(&rds_doc, sds_doc, tailoring_doc, tailoring_filepath, tailoring_doc_timestamp, result_file_doc,
858 oval_result_sources, oval_result_mapping, arf_report_mapping) != 0) {
859 free(tailoring_doc_timestamp);
860 return NULL;
861 }
862 free(tailoring_doc_timestamp);
863 return oscap_source_new_from_xmlDoc(rds_doc, target_file);
864 }
865
866 int ds_rds_create(const char* sds_file, const char* xccdf_result_file, const char** oval_result_files, const char* target_file)
867 {
868 struct oscap_source *sds_source = oscap_source_new_from_file(sds_file);
869 struct oscap_source *xccdf_result_source = oscap_source_new_from_file(xccdf_result_file);
870 struct oscap_htable *oval_result_sources = oscap_htable_new();
871 struct oscap_htable *oval_result_mapping = oscap_htable_new();
872 struct oscap_htable *arf_report_mapping = oscap_htable_new();
873
874 int result = 0;
875 // this check is there to allow passing NULL instead of having to allocate
876 // an empty array
877 if (oval_result_files != NULL)
878 {
879 while (*oval_result_files != NULL)
880 {
881 struct oscap_source *oval_source = oscap_source_new_from_file(*oval_result_files);
882 if (oscap_source_get_xmlDoc(oval_source) == NULL) {
883 result = -1;
884 oscap_source_free(oval_source);
885 } else {
886 if (!oscap_htable_add(oval_result_sources, *oval_result_files, oval_source)) {
887 result = -1;
888 oscap_source_free(oval_source);
889 }
890 }
891 oval_result_files++;
892 }
893 }
894 if (result == 0) {
895 struct oscap_source *target_rds = ds_rds_create_source(sds_source, NULL, xccdf_result_source, oval_result_sources, oval_result_mapping, arf_report_mapping, target_file);
896 result = target_rds == NULL;
897 if (result == 0) {
898 result = oscap_source_save_as(target_rds, NULL);
899 }
900 oscap_source_free(target_rds);
901 }
902 oscap_htable_free(oval_result_sources, (oscap_destruct_func) oscap_source_free);
903 oscap_htable_free(oval_result_mapping, (oscap_destruct_func) free);
904 oscap_htable_free(arf_report_mapping, (oscap_destruct_func) free);
905 oscap_source_free(sds_source);
906 oscap_source_free(xccdf_result_source);
907
908 return result;
909 }