Debian Codegraph @

src/DS/sds.c

raw symbols SCIP
1 /*
2 * Copyright 2012 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
29 #include "public/oscap.h"
30 #include "public/oscap_text.h"
31
32 #include "ds_common.h"
33 #include "ds_sds_session_priv.h"
34 #include "sds_priv.h"
35
36 #include "common/debug_priv.h"
37 #include "common/_error.h"
38 #include "common/util.h"
39 #include "common/list.h"
40 #include "common/oscap_acquire.h"
41 #include "source/oscap_source_priv.h"
42 #include "source/public/oscap_source.h"
43 #include "oscap_helpers.h"
44
45 #include <sys/stat.h>
46 #include <time.h>
47
48 #include <libxml/xmlreader.h>
49 #include <libxml/parser.h>
50 #include <libxml/tree.h>
51 #include <libxml/xpath.h>
52 #include <libxml/xpathInternals.h>
53
54 #include <string.h>
55 #include <fcntl.h>
56 #ifdef OS_WINDOWS
57 #include <io.h>
58 #else
59 #include <unistd.h>
60 #endif
61
62 #ifndef MAXPATHLEN
63 # define MAXPATHLEN 1024
64 #endif
65
66 #ifndef O_NOFOLLOW
67 #define O_NOFOLLOW 0
68 #endif
69
70 static const char* datastream_ns_uri = "http://scap.nist.gov/schema/scap/source/1.2";
71 static const char* xlink_ns_uri = "http://www.w3.org/1999/xlink";
72 static const char* cat_ns_uri = "urn:oasis:names:tc:entity:xmlns:xml:catalog";
73 static const char* sce_xccdf_ns_uri = "http://open-scap.org/page/SCE_xccdf_stream";
74
75 xmlNodePtr node_get_child_element(xmlNodePtr parent, const char* name)
76 {
77 xmlNodePtr candidate = parent->children;
78
79 for (; candidate != NULL; candidate = candidate->next)
80 {
81 if (candidate->type != XML_ELEMENT_NODE)
82 continue;
83
84 if (name != NULL && strcmp((const char*)(candidate->name), name) != 0)
85 continue;
86
87 return candidate;
88 }
89
90 return NULL;
91 }
92
93 xmlNode *containter_get_component_ref_by_id(xmlNode *container, const char *component_id)
94 {
95 for (xmlNode *component_ref = container->children; component_ref != NULL; component_ref = component_ref->next)
96 {
97 if (component_ref->type != XML_ELEMENT_NODE)
98 continue;
99
100 if (strcmp((const char*)(component_ref->name), "component-ref") != 0)
101 continue;
102
103 xmlChar* cref_id = xmlGetProp(component_ref, BAD_CAST "id");
104 // if cref_id is zero we have encountered a fatal error that will be handled
105 // in ds_sds_dump_component_ref
106 if (component_id && cref_id && strcmp(component_id, (char*)cref_id) != 0)
107 {
108 xmlFree(cref_id);
109 continue;
110 }
111 xmlFree(cref_id);
112 return component_ref;
113 }
114 return NULL;
115 }
116
117 xmlNodePtr ds_sds_find_component_ref(xmlNodePtr datastream, const char* id)
118 {
119 /* This searches for a ds:component-ref (XLink) element with a given id.
120 * It returns a first such element in a given ds:data-stream.
121 */
122 xmlNodePtr cref_parent = datastream->children;
123
124 for (; cref_parent != NULL; cref_parent = cref_parent->next)
125 {
126 if (cref_parent->type != XML_ELEMENT_NODE)
127 continue;
128 xmlNode *component_ref = containter_get_component_ref_by_id(cref_parent, id);
129 if (component_ref != NULL) {
130 return component_ref;
131 }
132 }
133
134 return NULL;
135 }
136
137 xmlNodePtr lookup_component_in_collection(xmlNodePtr root, const char *component_id)
138 {
139 xmlNodePtr component = NULL;
140 xmlNodePtr candidate = root->children;
141
142 for (; candidate != NULL; candidate = candidate->next)
143 {
144 if (candidate->type != XML_ELEMENT_NODE)
145 continue;
146
147 if ((strcmp((const char*)(candidate->name), "component") != 0) &&
148 (strcmp((const char*)(candidate->name), "extended-component") != 0))
149 continue;
150
151 char* candidate_id = (char*)xmlGetProp(candidate, BAD_CAST "id");
152 if (strcmp(candidate_id, component_id) == 0)
153 {
154 component = candidate;
155 xmlFree(candidate_id);
156 break;
157 }
158 xmlFree(candidate_id);
159 }
160 return component;
161 }
162
163 static int ds_sds_dump_component_sce(const xmlNode *script_node, const char *component_id, const char *filename)
164 {
165 if (script_node) {
166 if (oscap_acquire_ensure_parent_dir(filename) < 0) {
167 oscap_seterr(OSCAP_EFAMILY_XML, "Error while creating script parent directory for file '%s' of (id='%s')", filename, component_id);
168 return -1;
169 }
170 // TODO: should we check whether the component is extended?
171 int fd;
172 xmlChar* text_contents = xmlNodeGetContent(script_node);
173 if ((fd = open(filename, O_CREAT | O_TRUNC | O_NOFOLLOW | O_WRONLY, 0700)) < 0) {
174 oscap_seterr(OSCAP_EFAMILY_XML, "Error while creating script component (id='%s') to file '%s'.", component_id, filename);
175 xmlFree(text_contents);
176 return -1;
177 }
178 FILE* output_file = fdopen(fd, "w");
179 if (output_file == NULL) {
180 oscap_seterr(OSCAP_EFAMILY_XML, "Error while dumping script component (id='%s') to file '%s'.", component_id, filename);
181 xmlFree(text_contents);
182 close(fd);
183 return -1;
184 }
185 // TODO: error checking, fprintf should return strlen((const char*)text_contents)
186 fprintf(output_file, "%s", text_contents ? (char*)text_contents : "");
187 #ifndef OS_WINDOWS
188 // NB: This code is for SCE scripts
189 if (fchmod(fd, 0700) != 0) {
190 oscap_seterr(OSCAP_EFAMILY_XML, "Failed to set executable permission on script (id='%s') that was split to '%s'.", component_id, filename);
191 }
192 #endif
193
194 dD("Successfully dumped script component (id='%s') to file '%s'.", component_id, filename);
195 fclose(output_file);
196 xmlFree(text_contents);
197 return 0;
198 }
199 else {
200 oscap_seterr(OSCAP_EFAMILY_XML, "Error while dumping script component (id='%s') to file '%s'. "
201 "The script element was empty!", component_id, filename);
202 return -1;
203 }
204 }
205
206 /**
207 * Load oscap source from file
208 * Filename is relatively to datastream file
209 */
210 static struct oscap_source *load_referenced_source(const struct ds_sds_session *session, const char *filename)
211 {
212 const char* readable_origin = ds_sds_session_get_readable_origin(session);
213 assert(readable_origin != NULL);
214 char* readable_origin_cp = oscap_strdup(readable_origin);
215
216 char *dir_name = oscap_dirname(readable_origin_cp);
217 char* full_path = oscap_sprintf("%s/%s", dir_name, filename);
218 free(dir_name);
219
220 struct oscap_source *source_file = oscap_source_new_from_file(full_path);
221
222 free(full_path);
223 free(readable_origin_cp);
224
225 return source_file;
226 }
227
228 static int ds_sds_register_sce(struct ds_sds_session *session, xmlNodePtr component_inner_root, const char* component_id, const char* target_filename_dirname, const char* relative_filepath)
229 {
230 // the cast is safe to do because we are using the GNU basename, it doesn't
231 // modify the string
232 char *file_basename = oscap_basename((char*)relative_filepath);
233 char *sce_filename = oscap_sprintf("%s/%s/%s",ds_sds_session_get_target_dir(session), target_filename_dirname, file_basename);
234 free(file_basename);
235 const int ret = ds_sds_dump_component_sce(component_inner_root->children, component_id, sce_filename);
236 free(sce_filename);
237 return ret;
238 }
239
240 static int ds_sds_register_xmlDoc(struct ds_sds_session *session, xmlDoc* doc, xmlNodePtr component_inner_root, const char *relative_filepath)
241 {
242 xmlDoc *new_doc = ds_doc_from_foreign_node(component_inner_root, doc);
243 if (new_doc == NULL) {
244 return -1;
245 }
246
247 struct oscap_source *component_source = oscap_source_new_from_xmlDoc(new_doc, relative_filepath);
248
249 if (ds_sds_session_register_component_source(session, relative_filepath, component_source) != 0) {
250 oscap_source_free(component_source);
251 }
252 return 0; // TODO: Return value of ds_sds_session_register_component_source(). (commit message)
253 }
254
255 static int ds_sds_register_component(struct ds_sds_session *session, xmlDoc* doc, xmlNodePtr component_inner_root, const char* component_id, const char* target_filename_dirname, const char* relative_filepath)
256 {
257 if (component_inner_root == NULL)
258 {
259 oscap_seterr(OSCAP_EFAMILY_XML, "Found component (id='%s') but it has no element contents, nothing to dump, skipping...", component_id);
260 return -1;
261 }
262
263 // If the inner root is script, we have to treat it in a special way
264 if (strcmp((const char*)component_inner_root->name, "script") == 0) {
265 return ds_sds_register_sce(session, component_inner_root, component_id, target_filename_dirname, relative_filepath);
266 } else {
267 // Otherwise we create a new XML doc we will dump the contents to.
268 // We can't just dump node "innerXML" because namespaces have to be
269 // handled.
270 return ds_sds_register_xmlDoc(session, doc, component_inner_root, relative_filepath);
271 }
272 }
273
274 static xmlNodePtr ds_sds_get_component_root_by_id(xmlDoc *doc, const char* component_id)
275 {
276 xmlNodePtr component;
277 if (component_id == NULL) {
278 component = (xmlNodePtr)doc;
279 } else {
280 xmlNodePtr root = xmlDocGetRootElement(doc);
281 component = lookup_component_in_collection(root, component_id);
282 if (component == NULL)
283 {
284 oscap_seterr(OSCAP_EFAMILY_XML, "Component of given id '%s' was not found in the document.", component_id);
285 return NULL;
286 }
287 }
288
289 return node_get_child_element(component, NULL);
290 }
291
292 static int ds_sds_dump_local_component(const char* component_id, struct ds_sds_session *session, const char *target_filename_dirname, const char *relative_filepath)
293 {
294 xmlDoc *doc = ds_sds_session_get_xmlDoc(session);
295
296 xmlNodePtr inner_root = ds_sds_get_component_root_by_id(doc, component_id);
297
298 return ds_sds_register_component(session, doc, inner_root, component_id, target_filename_dirname, relative_filepath);
299 }
300
301 static int ds_sds_dump_file_component(const char* external_file, const char* component_id, struct ds_sds_session *session, const char *target_filename_dirname, const char *relative_filepath)
302 {
303 int ret = 0;
304
305 struct oscap_source *source_file = load_referenced_source(session, external_file);
306 xmlDoc *doc = oscap_source_get_xmlDoc(source_file);
307
308 if (doc == NULL) {
309 ret = -1;
310 goto cleanup;
311 }
312
313 xmlNodePtr inner_root = ds_sds_get_component_root_by_id(doc, component_id);
314
315 if (ds_sds_register_component(session, doc, inner_root, component_id, target_filename_dirname, relative_filepath) != 0) {
316 ret = -1;
317 goto cleanup;
318 }
319
320 cleanup:
321 oscap_source_free(source_file);
322 return ret;
323 }
324
325 static int ds_dsd_dump_remote_component(const char* url, const char* component_id, struct ds_sds_session *session, const char *target_filename_dirname, const char *relative_filepath)
326 {
327 int ret = 0;
328 size_t memory_size = 0;
329
330 ds_sds_session_remote_resources_progress(session)(false, "Downloading: %s ... ", url);
331
332 char* mem = oscap_acquire_url_download(url, &memory_size);
333 if (mem == NULL) {
334 ds_sds_session_remote_resources_progress(session)(false, "error\n", url);
335 return -1;
336 }
337
338 ds_sds_session_remote_resources_progress(session)(false, "ok\n", url);
339
340 struct oscap_source *source_file = oscap_source_new_take_memory(mem, memory_size, url);
341 xmlDoc *doc = oscap_source_get_xmlDoc(source_file);
342
343 if (doc == NULL) {
344 ret = -1;
345 goto cleanup;
346 }
347
348 xmlNodePtr inner_root = ds_sds_get_component_root_by_id(doc, component_id);
349
350 if (ds_sds_register_component(session, doc, inner_root, component_id, target_filename_dirname, relative_filepath) != 0) {
351 ret = -1;
352 goto cleanup;
353 }
354
355 cleanup:
356 oscap_source_free(source_file);
357 return ret;
358 }
359
360 static char *compose_target_filename_dirname(const char *relative_filepath, const char* sub_dir)
361 {
362 char* filename_cpy = oscap_sprintf("./%s", relative_filepath);
363 char* file_reldir = oscap_dirname(filename_cpy);
364
365 char* target_filename_dirname = oscap_sprintf("%s/%s",sub_dir, file_reldir);
366 free(file_reldir);
367 free(filename_cpy);
368
369 return target_filename_dirname;
370 }
371
372 static int _handle_disabled_downloads(struct ds_sds_session *session, const char *relative_filepath, const char *xlink_href, const char *component_id, const char *target_filename_dirname, const char *cref_id, const char *url)
373 {
374 /*
375 * If fetching remote resources isn't allowed by the user let's take a look
376 * whether there exists a file whose file name is equal to @name attribute
377 * of the uri element within the catalog of the previously processed
378 * component-ref which pointed us to the currently processed component-ref.
379 * Note that the @name attribute value has been passed as relative_filepath
380 * in the recursive call of ds_sds_dump_component_ref_as. If such file
381 * exists, we will assume that it's a local copy of the remote component
382 * located at the URL defined in @xlink:href. This way people can provide
383 * the previously downloaded component which might be useful on systems with
384 * limited internet access. This behavior is allowed only when --local-files
385 * is used on the command line.
386 * See: https://bugzilla.redhat.com/show_bug.cgi?id=1970527
387 * See: https://access.redhat.com/solutions/5185891
388 */
389 const char *local_files = ds_sds_session_local_files(session);
390 if (local_files == NULL) {
391 static bool fetch_remote_resources_suggested = false;
392 if (!fetch_remote_resources_suggested) {
393 fetch_remote_resources_suggested = true;
394 ds_sds_session_remote_resources_progress(session)(true,
395 "WARNING: Datastream component '%s' points out to the remote '%s'. Use '--fetch-remote-resources' option to download it.\n",
396 cref_id, url);
397 }
398
399 ds_sds_session_remote_resources_progress(session)(true,
400 "WARNING: Skipping '%s' file which is referenced from datastream\n",
401 url);
402 // -2 means that remote resources were not downloaded
403 return -2;
404 }
405 char *local_filepath = oscap_path_join(local_files, relative_filepath);
406 struct stat sb;
407 if (stat(local_filepath, &sb) == 0) {
408 ds_sds_session_remote_resources_progress(session)(true,
409 "WARNING: Using local file '%s' instead of '%s'",
410 local_filepath, xlink_href);
411 struct oscap_source *source_file = oscap_source_new_from_file(local_filepath);
412 xmlDoc *doc = oscap_source_pop_xmlDoc(source_file);
413 if (doc == NULL) {
414 free(local_filepath);
415 oscap_source_free(source_file);
416 return -1;
417 }
418 xmlNodePtr inner_root = ds_sds_get_component_root_by_id(doc, component_id);
419
420 if (ds_sds_register_component(session, doc, inner_root, component_id, target_filename_dirname, relative_filepath) != 0) {
421 oscap_source_free(source_file);
422 free(local_filepath);
423 return -1;
424 }
425 free(local_filepath);
426 oscap_source_free(source_file);
427 return 0;
428 }
429 ds_sds_session_remote_resources_progress(session)(true,
430 "WARNING: Data stream component '%s' points out to the remote '%s'. " \
431 "The option --local-files '%s' has been provided, but the file '%s' can't be used locally: %s.\n",
432 cref_id, url, local_files, local_filepath, strerror(errno));
433 free(local_filepath);
434 return -2;
435 }
436
437 static int ds_sds_dump_component_by_href(struct ds_sds_session *session, char* xlink_href, char *target_filename_dirname, const char* relative_filepath, char* cref_id, char **component_id)
438 {
439 if (!xlink_href || strlen(xlink_href) < 2)
440 {
441 oscap_seterr(OSCAP_EFAMILY_XML, "No or invalid xlink:href attribute on given component-ref.");
442 return -1;
443 }
444
445 if (xlink_href[0] == '#')
446 {
447 *component_id = xlink_href + 1;
448
449 return ds_sds_dump_local_component(*component_id, session, target_filename_dirname, relative_filepath);
450
451 } else if (oscap_str_startswith(xlink_href, "file:")){
452
453 char* sep = strchr(xlink_href, '#');
454
455 const char *filename = xlink_href + strlen("file:");
456
457 if (sep == NULL) {
458 *component_id = NULL;
459 } else {
460 *sep = '\0';
461 *component_id = sep + 1;
462 }
463
464 return ds_sds_dump_file_component(filename, *component_id, session, target_filename_dirname, relative_filepath);
465
466 } else if (oscap_acquire_url_is_supported(xlink_href)){
467 char *sep = strchr(xlink_href, '#');
468
469 char* url = xlink_href;
470
471 if (sep == NULL) {
472 *component_id = NULL;
473 } else {
474 *sep = '\0';
475 *component_id = sep + 1;
476 }
477
478 if (!ds_sds_session_fetch_remote_resources(session)) {
479 return _handle_disabled_downloads(
480 session, relative_filepath, xlink_href, *component_id,
481 target_filename_dirname, cref_id, url);
482 }
483
484 return ds_dsd_dump_remote_component(url, *component_id, session, target_filename_dirname, relative_filepath);
485 } else {
486 oscap_seterr(OSCAP_EFAMILY_XML, "Unsupported type of xlink:href attribute on given component-ref - '%s'.", xlink_href);
487 return -1;
488 }
489 return 0;
490 }
491
492 int ds_sds_dump_component_ref_as(const xmlNodePtr component_ref, struct ds_sds_session *session, const char* sub_dir, const char* relative_filepath)
493 {
494 char* cref_id = (char*)xmlGetProp(component_ref, BAD_CAST "id");
495 if (!cref_id)
496 {
497 oscap_seterr(OSCAP_EFAMILY_XML, "No or invalid id attribute on given component-ref.");
498 xmlFree(cref_id);
499 return -1;
500 }
501
502 char* xlink_href = (char*)xmlGetNsProp(component_ref, BAD_CAST "href", BAD_CAST xlink_ns_uri);
503
504 char* target_filename_dirname = compose_target_filename_dirname(relative_filepath, sub_dir);
505
506 char* component_id = NULL;
507
508 // make a copy of xlink_href because ds_sds_dump_component_by_href modifies its second argument
509 char *xlink_href_copy = oscap_strdup(xlink_href);
510 int ret = ds_sds_dump_component_by_href(session, xlink_href, target_filename_dirname, relative_filepath, cref_id, &component_id);
511 if (!oscap_htable_add(ds_sds_session_get_component_uris(session), cref_id, xlink_href_copy)) {
512 free(xlink_href_copy);
513 }
514
515 xmlFree(xlink_href);
516 xmlFree(cref_id);
517
518 if (ret == -2) {
519 // A remote component was not dumped
520 // It should be ok to continue without it
521 free(target_filename_dirname);
522 return 0;
523 } else if (ret != 0) {
524 free(target_filename_dirname);
525 return -1;
526 }
527
528 xmlNodePtr catalog = node_get_child_element(component_ref, "catalog");
529 if (catalog)
530 {
531 xmlNodePtr uri = catalog->children;
532
533 for (; uri != NULL; uri = uri->next)
534 {
535 if (uri->type != XML_ELEMENT_NODE)
536 continue;
537
538 if (strcmp((const char*)(uri->name), "uri") != 0)
539 continue;
540
541 char* name = (char*)xmlGetProp(uri, BAD_CAST "name");
542
543 if (!name)
544 {
545 oscap_seterr(OSCAP_EFAMILY_XML, "No 'name' attribute for a component referenced in the catalog of component '%s'.", component_id);
546 free(target_filename_dirname);
547 return -1;
548 }
549
550 char* str_uri = (char*)xmlGetProp(uri, BAD_CAST "uri");
551
552 if (!str_uri || strlen(str_uri) < 2)
553 {
554 oscap_seterr(OSCAP_EFAMILY_XML, "No or invalid 'uri' attribute for a component referenced in the catalog of component '%s'.", component_id);
555 xmlFree(str_uri);
556 xmlFree(name);
557 free(target_filename_dirname);
558 return -1;
559 }
560
561 // the pointer arithmetics simply skips the first character which is '#'
562 assert(str_uri[0] == '#');
563 xmlNodePtr cat_component_ref = ds_sds_find_component_ref(ds_sds_session_get_selected_datastream(session), str_uri + 1 * sizeof(char));
564
565 if (!cat_component_ref)
566 {
567 oscap_seterr(OSCAP_EFAMILY_XML, "component-ref with given id '%s' wasn't found in the document! We are looking for it because it's in the catalog of component '%s'.", str_uri + 1 * sizeof(char), component_id);
568 xmlFree(str_uri);
569 xmlFree(name);
570 free(target_filename_dirname);
571 return -1;
572 }
573 xmlFree(str_uri);
574
575 if (ds_sds_dump_component_ref_as(cat_component_ref, session, target_filename_dirname, name) != 0)
576 {
577 xmlFree(name);
578 free(target_filename_dirname);
579 return -1; // no need to call oscap_seterr here, it's already set
580 }
581
582 xmlFree(name);
583 }
584 }
585
586 free(target_filename_dirname);
587
588
589 return 0;
590 }
591
592 int ds_sds_dump_component_ref(const xmlNodePtr component_ref, struct ds_sds_session *session)
593 {
594 char* cref_id = (char*)xmlGetProp(component_ref, BAD_CAST "id");
595 if (!cref_id)
596 {
597 oscap_seterr(OSCAP_EFAMILY_XML, "No or invalid id attribute on given component-ref.");
598 xmlFree(cref_id);
599 return -1;
600 }
601
602 int result = ds_sds_dump_component_ref_as(component_ref, session, ".", cref_id);
603 xmlFree(cref_id);
604
605 // if result is -1, oscap_seterr was already called, no need to call it again
606 return result;
607 }
608
609 xmlNodePtr ds_sds_lookup_datastream_in_collection(xmlDocPtr doc, const char *datastream_id)
610 {
611 xmlNodePtr root = xmlDocGetRootElement(doc);
612
613 xmlNodePtr datastream = NULL;
614 xmlNodePtr candidate_datastream = root->children;
615
616 for (; candidate_datastream != NULL; candidate_datastream = candidate_datastream->next)
617 {
618 if (candidate_datastream->type != XML_ELEMENT_NODE)
619 continue;
620 if (strcmp((const char*)(candidate_datastream->name), "data-stream") != 0)
621 continue;
622
623 // at this point it is sure to be a <data-stream> element
624
625 char* candidate_id = (char*)xmlGetProp(candidate_datastream, BAD_CAST "id");
626 if (datastream_id == NULL || oscap_streq(datastream_id, candidate_id)) {
627 datastream = candidate_datastream;
628 xmlFree(candidate_id);
629 break;
630 }
631 xmlFree(candidate_id);
632 }
633
634 return datastream;
635 }
636
637 static inline int ds_sds_compose_component_add_script_content(xmlNode *component, const char *filepath)
638 {
639 FILE* f = fopen(filepath, "r");
640 if (!f) {
641 oscap_seterr(OSCAP_EFAMILY_GLIBC, "Can't read plain text from file '%s'.", filepath);
642 return -1;
643 }
644
645 fseek(f, 0, SEEK_END);
646 long int length = ftell(f);
647 fseek(f, 0, SEEK_SET);
648 if (length >= 0) {
649 char* buffer = malloc((length + 1) * sizeof(char));
650 if (fread(buffer, length, 1, f) != 1) {
651 oscap_seterr(OSCAP_EFAMILY_GLIBC, "Error while reading from file '%s'.", filepath);
652 fclose(f);
653 free(buffer);
654 return -1;
655 }
656 fclose(f);
657 buffer[length] = '\0';
658 xmlNsPtr local_ns = xmlNewNs(component, BAD_CAST sce_xccdf_ns_uri, BAD_CAST "oscap-sce-xccdf-stream");
659 xmlNewTextChild(component, local_ns, BAD_CAST "script", BAD_CAST buffer);
660 free(buffer);
661 return 0;
662 } else {
663 oscap_seterr(OSCAP_EFAMILY_GLIBC, "No data read from file '%s'.", filepath);
664 fclose(f);
665 return -1;
666 }
667 }
668
669 static int ds_sds_compose_add_component_internal(xmlDocPtr doc, xmlNodePtr datastream, struct oscap_source *component_source, const char* comp_id, bool extended)
670 {
671 xmlNsPtr ds_ns = xmlSearchNsByHref(doc, datastream, BAD_CAST datastream_ns_uri);
672 if (!ds_ns)
673 {
674 oscap_seterr(OSCAP_EFAMILY_GLIBC,
675 "Unable to find namespace '%s' in the XML DOM tree when create "
676 "source datastream. This is most likely an internal error!",
677 datastream_ns_uri);
678 return -1;
679 }
680
681 char file_timestamp[32];
682 strncpy(file_timestamp, "0000-00-00T00:00:00", sizeof(file_timestamp));
683
684 const char *filepath = oscap_source_get_filepath(component_source);
685 struct stat file_stat;
686 if (stat(filepath, &file_stat) == 0) {
687 time_t mtime;
688 struct tm result;
689 char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
690 if (source_date_epoch == NULL ||
691 (mtime = (time_t)strtoll(source_date_epoch, NULL, 10)) <= 0 ||
692 mtime > file_stat.st_mtime)
693 mtime = file_stat.st_mtime;
694 strftime(file_timestamp, 32, "%Y-%m-%dT%H:%M:%S", localtime_r(&mtime, &result));
695 } else {
696 oscap_seterr(OSCAP_EFAMILY_GLIBC, "Could not find file %s: %s.", filepath, strerror(errno));
697 // Return positive number, indicating less severe problem.
698 // Rationale: When an OVAL file is missing during a scan it it not considered
699 // to be deal breaker (it shall have 'notchecked' result), thus we shall allow
700 // DataStreams with missing OVAL.
701 return 1;
702 }
703
704 xmlNodePtr component = xmlNewNode(ds_ns, BAD_CAST (extended ? "extended-component" : "component"));
705 xmlSetProp(component, BAD_CAST "id", BAD_CAST comp_id);
706 xmlSetProp(component, BAD_CAST "timestamp", BAD_CAST file_timestamp);
707
708 xmlNodePtr doc_root = xmlDocGetRootElement(doc);
709
710 if (extended) {
711 if (ds_sds_compose_component_add_script_content(component, filepath) == -1) {
712 xmlFreeNode(component);
713 return -1;
714 }
715 // extended components always go at the end
716 xmlAddChild(doc_root, component);
717 } else {
718 xmlDoc *component_doc = oscap_source_get_xmlDoc(component_source);
719 if (!component_doc) {
720 oscap_seterr(OSCAP_EFAMILY_XML, "Could not read/parse XML of given input file at path '%s'.", filepath);
721 xmlFreeNode(component);
722 return -1;
723 }
724
725 xmlNodePtr component_root = xmlDocGetRootElement(component_doc);
726
727 xmlDOMWrapCtxtPtr wrap_ctxt = xmlDOMWrapNewCtxt();
728
729 xmlNodePtr res_component_root = NULL;
730 if (xmlDOMWrapCloneNode(wrap_ctxt, component_doc, component_root, &res_component_root, doc, NULL, 1, 0) != 0)
731 {
732 oscap_seterr(OSCAP_EFAMILY_XML,
733 "Cannot clone node when adding component from file '%s' with id '%s' while "
734 "creating source datastream.", filepath, comp_id);
735
736 xmlDOMWrapFreeCtxt(wrap_ctxt);
737 xmlFreeNode(component);
738
739 return -1;
740 }
741 if (xmlDOMWrapReconcileNamespaces(wrap_ctxt, res_component_root, 0) != 0)
742 {
743 oscap_seterr(OSCAP_EFAMILY_XML,
744 "Cannot reconcile namespaces when adding component from file '%s' with id '%s' while "
745 "creating source datastream.", filepath, comp_id);
746
747 xmlDOMWrapFreeCtxt(wrap_ctxt);
748 xmlFreeNode(component);
749
750 return -1;
751 }
752
753 xmlAddChild(component, res_component_root);
754
755 xmlDOMWrapFreeCtxt(wrap_ctxt);
756
757 // this component is not extended, we have to figure out if there
758 // already is an extended-component and if so, add it right before
759 // that component
760
761 xmlNodePtr first_extended_component = node_get_child_element(doc_root, "extended-component");
762 if (first_extended_component == NULL)
763 {
764 // no extended component yet, add to the end
765 xmlAddChild(doc_root, component);
766 }
767 else
768 {
769 xmlAddPrevSibling(first_extended_component, component);
770 }
771 }
772
773 return 0;
774 }
775
776 static int ds_sds_compose_catalog_has_uri(xmlDocPtr doc, xmlNodePtr catalog, const char* uri)
777 {
778 xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
779 if (xpathCtx == NULL)
780 {
781 oscap_seterr(OSCAP_EFAMILY_XML, "Error: unable to create a new XPath context.");
782 return -1;
783 }
784
785 xmlXPathRegisterNs(xpathCtx, BAD_CAST "cat", BAD_CAST cat_ns_uri);
786 xmlXPathRegisterNs(xpathCtx, BAD_CAST "xlink", BAD_CAST xlink_ns_uri);
787
788 // limit xpath execution to just the catalog node
789 // this is done for performance reasons
790 xpathCtx->node = catalog;
791
792 char *expression = oscap_sprintf("cat:uri[@uri = '%s']", uri);
793 if (expression == NULL)
794 {
795 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Error: Unable to create XPath expression.");
796 xmlXPathFreeContext(xpathCtx);
797
798 return -1;
799 }
800
801 xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(
802 BAD_CAST expression,
803 xpathCtx);
804
805 free(expression);
806
807 if (xpathObj == NULL)
808 {
809 oscap_seterr(OSCAP_EFAMILY_XML, "Error: Unable to evaluate XPath expression.");
810 xmlXPathFreeContext(xpathCtx);
811
812 return -1;
813 }
814
815 int result = 0;
816
817 xmlNodeSetPtr nodeset = xpathObj->nodesetval;
818 if (nodeset != NULL)
819 result = nodeset->nodeNr > 0 ? 0 : 1;
820
821 xmlXPathFreeObject(xpathObj);
822 xmlXPathFreeContext(xpathCtx);
823
824 return result;
825 }
826
827 // takes given relative filepath and mangles it so that it's acceptable
828 // as a component id
829 char* ds_sds_mangle_filepath(const char* filepath)
830 {
831 if (filepath == NULL)
832 return NULL;
833
834 // the string will grow 2x the size in the worst case (every char is /)
835 // TODO: We can do better than this by counting the slashes
836 char* ret = malloc(strlen(filepath) * sizeof(char) * 2);
837
838 const char* src_it = filepath;
839 char* dst_it = ret;
840
841 while (*src_it)
842 {
843 if (*src_it == '/')
844 {
845 *dst_it++ = '-';
846 *dst_it++ = '-';
847 }
848 else if (*src_it == '@') {
849 *dst_it++ = '-';
850 *dst_it++ = '-';
851 }
852 else
853 {
854 *dst_it++ = *src_it;
855 }
856
857 src_it++;
858 }
859
860 *dst_it = '\0';
861
862 return ret;
863 }
864
865 static int ds_sds_compose_add_component_with_ref(xmlDocPtr doc, xmlNodePtr datastream, const char* filepath, const char* cref_id);
866
867 static inline const char *_get_dep_xpath_for_type(int document_type)
868 {
869 static const char *xccdf_xpath = "//*[local-name() = 'check-content-ref']";
870 static const char *cpe_xpath = "//*[local-name() = 'check']";
871 if (document_type == OSCAP_DOCUMENT_CPE_DICTIONARY)
872 return cpe_xpath;
873 return xccdf_xpath;
874 }
875
876 static int ds_sds_compose_add_component_dependencies(xmlDocPtr doc, xmlNodePtr datastream, struct oscap_source *component_source, xmlNodePtr catalog, int component_type)
877 {
878 xmlDocPtr component_doc = oscap_source_get_xmlDoc(component_source);
879 if (component_doc == NULL)
880 {
881 return -1;
882 }
883
884 xmlXPathContextPtr xpathCtx = xmlXPathNewContext(component_doc);
885 if (xpathCtx == NULL)
886 {
887 oscap_seterr(OSCAP_EFAMILY_XML, "Error: unable to create new XPath context.");
888 return -1;
889 }
890
891 xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(
892 // we want robustness and support for future versions, this expression
893 // retrieves check-content-refs from any namespace
894 BAD_CAST _get_dep_xpath_for_type(component_type),
895 xpathCtx);
896 if (xpathObj == NULL)
897 {
898 oscap_seterr(OSCAP_EFAMILY_XML, "Error: Unable to evaluate XPath expression.");
899 xmlXPathFreeContext(xpathCtx);
900
901 return -1;
902 }
903
904 xmlNsPtr cat_ns = xmlSearchNsByHref(doc, datastream, BAD_CAST cat_ns_uri);
905
906 xmlNodeSetPtr nodeset = xpathObj->nodesetval;
907 if (nodeset != NULL)
908 {
909 struct oscap_htable *exported = oscap_htable_new();
910 char* filepath_cpy = oscap_strdup(oscap_source_readable_origin(component_source));
911 char *dir = oscap_dirname(filepath_cpy);
912
913 for (int i = 0; i < nodeset->nodeNr; i++)
914 {
915 xmlNodePtr node = nodeset->nodeTab[i];
916
917 if (node->type != XML_ELEMENT_NODE)
918 continue;
919
920 if (xmlHasProp(node, BAD_CAST "href"))
921 {
922 char* href = (char*)xmlGetProp(node, BAD_CAST "href");
923 if (oscap_htable_get(exported, href) != NULL) {
924 // This path has been already exported. Do not export duplicate.
925 xmlFree(href);
926 continue;
927 }
928 oscap_htable_add(exported, href, "");
929
930 if (oscap_acquire_url_is_supported(href)) {
931 /* If the referenced component is remote one, do not include
932 * it within the DataStream. Such component shall only be
933 * downloaded once the scan is run. */
934 xmlFree(href);
935 continue;
936 }
937
938 // skip over file:// if it's used in the file href
939 const char *altered_href = oscap_str_startswith(href, "file://") ? href + 7 : href;
940
941 char* real_path = (strcmp(dir, "") == 0 || strcmp(dir, ".") == 0 || altered_href[0] == '/') ?
942 oscap_strdup(altered_href) : oscap_sprintf("%s/%s", dir, altered_href);
943
944 char* mangled_path = ds_sds_mangle_filepath(real_path);
945 char* cref_id = oscap_sprintf("scap_org.open-scap_cref_%s", mangled_path);
946
947 int counter = 0;
948 while (ds_sds_find_component_ref(datastream, cref_id) != NULL) {
949 // While the given component ID already exists in the document.
950 free(cref_id);
951 cref_id = oscap_sprintf("scap_org.open-scap_cref_%s%03d", mangled_path, counter++);
952 }
953 free(mangled_path);
954
955 char* uri = oscap_sprintf("#%s", cref_id);
956
957 // we don't want duplicated uri elements in the catalog
958 if (ds_sds_compose_catalog_has_uri(doc, catalog, uri) == 0)
959 {
960 free(uri);
961 free(cref_id);
962 free(real_path);
963 xmlFree(href);
964 continue;
965 }
966
967 int ret = ds_sds_compose_add_component_with_ref(doc, datastream, real_path, cref_id);
968 if (ret == 0) {
969 xmlNodePtr catalog_uri = xmlNewNode(cat_ns, BAD_CAST "uri");
970 xmlSetProp(catalog_uri, BAD_CAST "name", BAD_CAST href);
971 xmlSetProp(catalog_uri, BAD_CAST "uri", BAD_CAST uri);
972 xmlAddChild(catalog, catalog_uri);
973 }
974
975 free(cref_id);
976 free(uri);
977 free(real_path);
978 xmlFree(href);
979
980 if (ret < 0) {
981 // oscap_seterr has already been called
982 oscap_htable_free0(exported);
983 xmlXPathFreeObject(xpathObj);
984 xmlXPathFreeContext(xpathCtx);
985 free(filepath_cpy);
986 free(dir);
987 return -1;
988 }
989
990 }
991 }
992
993 free(dir);
994 oscap_htable_free0(exported);
995 free(filepath_cpy);
996 }
997
998 xmlXPathFreeObject(xpathObj);
999 xmlXPathFreeContext(xpathCtx);
1000
1001 return 0;
1002 }
1003
1004 static int ds_sds_compose_has_component_ref(xmlDocPtr doc, xmlNodePtr datastream, const char* filepath, const char* cref_id)
1005 {
1006 xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
1007 if (xpathCtx == NULL)
1008 {
1009 oscap_seterr(OSCAP_EFAMILY_XML, "Error: unable to create new XPath context.");
1010 return -1;
1011 }
1012
1013 xmlXPathRegisterNs(xpathCtx, BAD_CAST "ds", BAD_CAST datastream_ns_uri);
1014 xmlXPathRegisterNs(xpathCtx, BAD_CAST "xlink", BAD_CAST xlink_ns_uri);
1015
1016 // limit xpath execution to just the datastream node
1017 // this is done for performance reasons
1018 xpathCtx->node = datastream;
1019
1020 char *expression = oscap_sprintf("*/ds:component-ref[@xlink:href = '#%s' and @id = '%s']", filepath, cref_id);
1021 if (expression == NULL)
1022 {
1023 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Error: Unable to create XPath expression.");
1024 xmlXPathFreeContext(xpathCtx);
1025
1026 return -1;
1027 }
1028
1029 xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(
1030 BAD_CAST expression,
1031 xpathCtx);
1032
1033 free(expression);
1034
1035 if (xpathObj == NULL)
1036 {
1037 oscap_seterr(OSCAP_EFAMILY_XML, "Error: Unable to evaluate XPath expression.");
1038 xmlXPathFreeContext(xpathCtx);
1039
1040 return -1;
1041 }
1042
1043 int result = 1;
1044
1045 xmlNodeSetPtr nodeset = xpathObj->nodesetval;
1046 if (nodeset != NULL)
1047 result = nodeset->nodeNr > 0 ? 0 : 1;
1048
1049 xmlXPathFreeObject(xpathObj);
1050 xmlXPathFreeContext(xpathCtx);
1051
1052 return result;
1053 }
1054
1055 static int ds_sds_compose_add_component_source_with_ref(xmlDocPtr doc, xmlNodePtr datastream, struct oscap_source *component_source, const char* cref_id)
1056 {
1057 xmlNsPtr ds_ns = xmlSearchNsByHref(doc, datastream, BAD_CAST datastream_ns_uri);
1058 xmlNsPtr xlink_ns = xmlSearchNsByHref(doc, datastream, BAD_CAST xlink_ns_uri);
1059 xmlNsPtr cat_ns = xmlSearchNsByHref(doc, datastream, BAD_CAST cat_ns_uri);
1060 const char *filepath = oscap_source_get_filepath(component_source);
1061
1062 // In case we already have this component we just return, no need to add
1063 // it twice. We will typically have many references to OVAL files, adding
1064 // component for each reference would create unnecessarily huge datastreams
1065 int result = ds_sds_compose_has_component_ref(doc, datastream, filepath, cref_id);
1066 if (result == 0)
1067 {
1068 return 0;
1069 }
1070
1071 if (result == -1)
1072 {
1073 // no need to free anything
1074 // oscap_seterr has already been called
1075 return -1;
1076 }
1077
1078 xmlNodePtr cref_catalog = xmlNewNode(cat_ns, BAD_CAST "catalog");
1079 xmlNodePtr cref_parent;
1080
1081 bool extended_component = false;
1082
1083 oscap_document_type_t doc_type = oscap_source_get_scap_type(component_source);
1084 if (doc_type == OSCAP_DOCUMENT_XCCDF)
1085 {
1086 cref_parent = node_get_child_element(datastream, "checklists");
1087 if (ds_sds_compose_add_component_dependencies(doc, datastream, component_source, cref_catalog, doc_type) != 0)
1088 {
1089 // oscap_seterr has already been called
1090 return -1;
1091 }
1092 }
1093 else if (doc_type == OSCAP_DOCUMENT_CPE_DICTIONARY || doc_type == OSCAP_DOCUMENT_CPE_LANGUAGE)
1094 {
1095 cref_parent = node_get_child_element(datastream, "dictionaries");
1096 if (cref_parent == NULL) {
1097 cref_parent = xmlNewNode(ds_ns, BAD_CAST "dictionaries");
1098 // The <ds:dictionaries element must as the first child of the datastream
1099 xmlNodePtr first_child = datastream->xmlChildrenNode;
1100 xmlNodePtr new_node = (first_child == NULL) ?
1101 xmlAddChild(datastream, cref_parent) : xmlAddPrevSibling(first_child, cref_parent);
1102 if (new_node == NULL) {
1103 oscap_seterr(OSCAP_EFAMILY_XML, "Failed to add dictionaries element to the DataStream.");
1104 xmlFreeNode(cref_parent);
1105 cref_parent = NULL;
1106 }
1107 }
1108 if (ds_sds_compose_add_component_dependencies(doc, datastream, component_source, cref_catalog, doc_type) != 0) {
1109 return -1;
1110 }
1111 }
1112 else if (doc_type == OSCAP_DOCUMENT_OVAL_DEFINITIONS || doc_type == OSCAP_DOCUMENT_OCIL)
1113 {
1114 cref_parent = node_get_child_element(datastream, "checks");
1115 }
1116 else
1117 {
1118 // not an XCCDF file, not an OVAL file, not a dict/lang, assume it goes into extended components
1119 extended_component = true;
1120 cref_parent = node_get_child_element(datastream, "extended-components");
1121 }
1122
1123 char* mangled_filepath = ds_sds_mangle_filepath(filepath);
1124 // extended components (sadly :-/) use a different ID scheme and have
1125 // a different element name than "normal" components
1126 char* comp_id = oscap_sprintf("scap_org.open-scap_%scomp_%s",
1127 extended_component ? "e" : "", mangled_filepath);
1128
1129 int counter = 0;
1130 xmlNodePtr root = xmlDocGetRootElement(doc);
1131 while (lookup_component_in_collection(root, comp_id) != NULL) {
1132 // While a component of the given ID already exists, generate a new one
1133 free(comp_id);
1134 comp_id = oscap_sprintf("scap_org.open-scap_%scomp_%s%03d",
1135 extended_component ? "e" : "", mangled_filepath, counter++);
1136 }
1137
1138 free(mangled_filepath);
1139
1140 result = ds_sds_compose_add_component_internal(doc, datastream, component_source, comp_id, extended_component);
1141 if (result == 0) {
1142 xmlNodePtr cref = xmlNewNode(ds_ns, BAD_CAST "component-ref");
1143 xmlAddChild(cref, cref_catalog);
1144 xmlSetProp(cref, BAD_CAST "id", BAD_CAST cref_id);
1145
1146 char *xlink_href = oscap_sprintf("#%s", comp_id);
1147 xmlSetNsProp(cref, xlink_ns, BAD_CAST "href", BAD_CAST xlink_href);
1148 free(xlink_href);
1149
1150 if (xmlAddChild(cref_parent, cref) == NULL) {
1151 oscap_seterr(OSCAP_EFAMILY_XML, "Failed to add component-ref/@id='%s' to the DataStream.", cref_id);
1152 result = 1;
1153 }
1154 }
1155
1156 free(comp_id);
1157 // the source data stream XSD requires either no catalog or a non-empty one
1158 if (cref_catalog->children == NULL)
1159 {
1160 xmlUnlinkNode(cref_catalog);
1161 xmlFreeNode(cref_catalog);
1162 }
1163
1164 return result;
1165 }
1166
1167 int ds_sds_compose_add_component_with_ref(xmlDocPtr doc, xmlNodePtr datastream, const char* filepath, const char* cref_id)
1168 {
1169 struct oscap_source *component_source = oscap_source_new_from_file(filepath);
1170 int ret = ds_sds_compose_add_component_source_with_ref(doc, datastream, component_source, cref_id);
1171 oscap_source_free(component_source);
1172 return ret;
1173 }
1174
1175 int ds_sds_compose_add_component(const char *target_datastream, const char *datastream_id, const char *new_component, bool extended)
1176 {
1177 struct oscap_source *sds_source = oscap_source_new_from_file(target_datastream);
1178 xmlDoc *doc = oscap_source_get_xmlDoc(sds_source);
1179 if (doc == NULL) {
1180 oscap_source_free(sds_source);
1181 return 1;
1182 }
1183 xmlNodePtr datastream = ds_sds_lookup_datastream_in_collection(doc, datastream_id);
1184 if (datastream == NULL) {
1185 char *error = datastream_id ?
1186 oscap_sprintf("Could not find any datastream of id '%s'", datastream_id) :
1187 oscap_sprintf("Could not find any datastream inside the file");
1188
1189 oscap_seterr(OSCAP_EFAMILY_XML, "%s", error);
1190 free(error);
1191 oscap_source_free(sds_source);
1192 return 1;
1193 }
1194
1195 char* mangled_path = ds_sds_mangle_filepath(new_component);
1196
1197 char* cref_id = oscap_sprintf("scap_org.open-scap_cref_%s", mangled_path);
1198 free(mangled_path);
1199 if (ds_sds_compose_add_component_with_ref(doc, datastream, new_component, cref_id) != 0) {
1200 free(cref_id);
1201 oscap_source_free(sds_source);
1202 return 1;
1203 }
1204 free(cref_id);
1205
1206 if (oscap_source_save_as(sds_source, NULL) != 0) {
1207 oscap_seterr(OSCAP_EFAMILY_GLIBC, "Error saving source datastream to '%s'.", target_datastream);
1208 oscap_source_free(sds_source);
1209 return 1;
1210 }
1211 oscap_source_free(sds_source);
1212 return 0;
1213 }
1214
1215 xmlDocPtr ds_sds_compose_xmlDoc_from_xccdf_source(struct oscap_source *xccdf_source)
1216 {
1217 xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
1218 xmlNodePtr root = xmlNewNode(NULL, BAD_CAST "data-stream-collection");
1219 xmlDocSetRootElement(doc, root);
1220
1221 xmlNsPtr ds_ns = xmlNewNs(root, BAD_CAST datastream_ns_uri, BAD_CAST "ds");
1222 xmlSetNs(root, ds_ns);
1223
1224 // we will need this namespace later when creating xlink:href attr in
1225 // component-ref
1226 xmlNewNs(root, BAD_CAST xlink_ns_uri, BAD_CAST "xlink");
1227
1228 const char *xccdf_file = oscap_source_get_filepath(xccdf_source);
1229 char* mangled_xccdf_file = ds_sds_mangle_filepath(xccdf_file);
1230 char* collection_id = oscap_sprintf("scap_org.open-scap_collection_from_xccdf_%s", mangled_xccdf_file);
1231 xmlSetProp(root, BAD_CAST "id", BAD_CAST collection_id);
1232 free(collection_id);
1233
1234 xmlSetProp(root, BAD_CAST "schematron-version", BAD_CAST "1.2");
1235
1236 // we will need this namespace later when creating component-ref
1237 // dependency catalog
1238 xmlNewNs(root, BAD_CAST cat_ns_uri, BAD_CAST "cat");
1239
1240 xmlNodePtr datastream = xmlNewNode(ds_ns, BAD_CAST "data-stream");
1241 xmlAddChild(root, datastream);
1242
1243 char* datastream_id = oscap_sprintf("scap_org.open-scap_datastream_from_xccdf_%s", mangled_xccdf_file);
1244 xmlSetProp(datastream, BAD_CAST "id", BAD_CAST datastream_id);
1245 free(datastream_id);
1246
1247 xmlSetProp(datastream, BAD_CAST "scap-version", BAD_CAST "1.2");
1248
1249 xmlSetProp(datastream, BAD_CAST "use-case", BAD_CAST "OTHER");
1250
1251 xmlNodePtr dictionaries = xmlNewNode(ds_ns, BAD_CAST "dictionaries");
1252 xmlAddChild(datastream, dictionaries);
1253
1254 xmlNodePtr checklists = xmlNewNode(ds_ns, BAD_CAST "checklists");
1255 xmlAddChild(datastream, checklists);
1256
1257 xmlNodePtr checks = xmlNewNode(ds_ns, BAD_CAST "checks");
1258 xmlAddChild(datastream, checks);
1259
1260 xmlNodePtr extended_components = xmlNewNode(ds_ns, BAD_CAST "extended-components");
1261 xmlAddChild(datastream, extended_components);
1262
1263 char* cref_id = oscap_sprintf("scap_org.open-scap_cref_%s", mangled_xccdf_file);
1264 if (ds_sds_compose_add_component_source_with_ref(doc, datastream, xccdf_source, cref_id) != 0)
1265 {
1266 // oscap_seterr already called
1267 free(cref_id);
1268 free(mangled_xccdf_file);
1269 xmlFreeDoc(doc);
1270 return NULL;
1271 }
1272 free(cref_id);
1273
1274 // the XSD of source data stream enforces that the collection elements are
1275 // not empty if they are there, we will therefore now removes collections
1276 // where nothing has been added
1277
1278 if (dictionaries->children == NULL)
1279 {
1280 xmlUnlinkNode(dictionaries);
1281 xmlFreeNode(dictionaries);
1282 }
1283 if (checklists->children == NULL)
1284 {
1285 xmlUnlinkNode(checklists);
1286 xmlFreeNode(checklists);
1287 }
1288 if (checks->children == NULL)
1289 {
1290 xmlUnlinkNode(checks);
1291 xmlFreeNode(checks);
1292 }
1293 if (extended_components->children == NULL)
1294 {
1295 xmlUnlinkNode(extended_components);
1296 xmlFreeNode(extended_components);
1297 }
1298
1299 free(mangled_xccdf_file);
1300
1301 return doc;
1302 }
1303
1304 xmlDocPtr ds_sds_compose_xmlDoc_from_xccdf(const char *xccdf_file)
1305 {
1306 struct oscap_source *xccdf_source = oscap_source_new_from_file(xccdf_file);
1307 xmlDocPtr doc = ds_sds_compose_xmlDoc_from_xccdf_source(xccdf_source);
1308 oscap_source_free(xccdf_source);
1309 return doc;
1310 }
1311
1312 int ds_sds_compose_from_xccdf(const char *xccdf_file, const char *target_datastream)
1313 {
1314 xmlDocPtr doc = ds_sds_compose_xmlDoc_from_xccdf(xccdf_file);
1315 if (doc == NULL) {
1316 return -1;
1317 }
1318 if (xmlSaveFileEnc(target_datastream, doc, "utf-8") == -1)
1319 {
1320 oscap_seterr(OSCAP_EFAMILY_GLIBC, "Error saving source datastream to '%s'.", target_datastream);
1321 xmlFreeDoc(doc);
1322 return -1;
1323 }
1324
1325 xmlFreeDoc(doc);
1326 return 0;
1327 }
1328
1329 char *ds_sds_detect_version(xmlTextReader *reader)
1330 {
1331 /* find root element */
1332 while (xmlTextReaderRead(reader) == 1 && xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT)
1333 ;
1334
1335 char *element_name = (char *) xmlTextReaderConstLocalName(reader);
1336 if (!element_name) {
1337 oscap_setxmlerr(xmlGetLastError());
1338 return NULL;
1339 }
1340 if (strcmp(element_name, "data-stream-collection")) {
1341 oscap_seterr(OSCAP_EFAMILY_OSCAP,
1342 "Expected root element name for SCAP source datastream is" \
1343 "'data-stream-collection' but actual root element name is '%s'.",
1344 element_name);
1345 return NULL;
1346 }
1347 char *schematron_version = (char *) xmlTextReaderGetAttribute(reader, BAD_CAST "schematron-version");
1348 if (!schematron_version) {
1349 oscap_setxmlerr(xmlGetLastError());
1350 return NULL;
1351 }
1352 return schematron_version;
1353 }