struct xccdf_benchmark *xccdf_benchmark_import_source(struct oscap_source *source) { xmlTextReader *reader = oscap_source_get_xmlTextReader(source); while (xmlTextReaderRead(reader) == 1 && xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT) ; struct xccdf_benchmark *benchmark = xccdf_benchmark_new(); const bool parse_result = xccdf_benchmark_parse(XITEM(benchmark), reader); xmlFreeTextReader(reader); if (!parse_result) { // parsing fatal error oscap_seterr(OSCAP_EFAMILY_XML, "Failed to import XCCDF content from '%s'.", oscap_source_readable_origin(source)); xccdf_benchmark_free(benchmark); return NULL; } // This is sadly the only place where we can pass origin file information // to the CPE1 embedded dictionary (if any). It is necessary to figure out // proper paths to OVAL files referenced from CPE1 dictionaries. // FIXME: Refactor and move this somewhere else struct cpe_dict_model* embedded_dict = xccdf_benchmark_get_cpe_list(benchmark); if (embedded_dict != NULL) { cpe_dict_model_set_origin_file(embedded_dict, oscap_source_readable_origin(source)); } // same situation with embedded CPE2 lang model // FIXME: Refactor and move this somewhere else struct cpe_lang_model* embedded_lang_model = xccdf_benchmark_get_cpe_lang_model(benchmark); if (embedded_lang_model != NULL) { cpe_lang_model_set_origin_file(embedded_lang_model, oscap_source_readable_origin(source)); } return benchmark; }
struct xccdf_benchmark *xccdf_benchmark_import(const char *file) { xmlTextReaderPtr reader = xmlReaderForFile(file, NULL, 0); if (!reader) { oscap_seterr(OSCAP_EFAMILY_GLIBC, "Unable to open file: '%s'", file); return NULL; } xmlTextReaderSetErrorHandler(reader, &libxml_error_handler, NULL); while (xmlTextReaderRead(reader) == 1 && xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT) ; struct xccdf_benchmark *benchmark = xccdf_benchmark_new(); const bool parse_result = xccdf_benchmark_parse(XITEM(benchmark), reader); xmlFreeTextReader(reader); if (!parse_result) { // parsing fatal error oscap_seterr(OSCAP_EFAMILY_XML, "Failed to parse '%s'.", file); xccdf_benchmark_free(benchmark); return NULL; } // This is sadly the only place where we can pass origin file information // to the CPE1 embedded dictionary (if any). It is necessary to figure out // proper paths to OVAL files referenced from CPE1 dictionaries. // FIXME: Refactor and move this somewhere else struct cpe_dict_model* embedded_dict = xccdf_benchmark_get_cpe_list(benchmark); if (embedded_dict != NULL) { cpe_dict_model_set_origin_file(embedded_dict, file); } // same situation with embedded CPE2 lang model // FIXME: Refactor and move this somewhere else struct cpe_lang_model* embedded_lang_model = xccdf_benchmark_get_cpe_lang_model(benchmark); if (embedded_lang_model != NULL) { cpe_lang_model_set_origin_file(embedded_lang_model, file); } return benchmark; }
xmlNode *xccdf_benchmark_to_dom(struct xccdf_benchmark *benchmark, xmlDocPtr doc, xmlNode *parent, void *user_args) { xmlNodePtr root_node = NULL; if (parent) { root_node = xccdf_item_to_dom(XITEM(benchmark), doc, parent); } else { root_node = xccdf_item_to_dom(XITEM(benchmark), doc, parent); xmlDocSetRootElement(doc, root_node); } // FIXME! //xmlNewProp(root_node, BAD_CAST "xsi:schemaLocation", BAD_CAST XCCDF_SCHEMA_LOCATION); xmlNs *ns_xccdf = xmlNewNs(root_node, (const xmlChar*)xccdf_version_info_get_namespace_uri(xccdf_benchmark_get_schema_version(benchmark)), NULL); xmlNs *ns_xsi = xmlNewNs(root_node, XCCDF_XSI_NAMESPACE, BAD_CAST "xsi"); xmlSetNs(root_node, ns_xsi); xmlSetNs(root_node, ns_xccdf); /* Handle attributes */ if (xccdf_benchmark_get_resolved(benchmark)) xmlNewProp(root_node, BAD_CAST "resolved", BAD_CAST "1"); else xmlNewProp(root_node, BAD_CAST "resolved", BAD_CAST "0"); const char *xmllang = xccdf_benchmark_get_lang(benchmark); if (xmllang) xmlNewProp(root_node, BAD_CAST "xml:lang", BAD_CAST xmllang); const char *style = xccdf_benchmark_get_style(benchmark); if (style) xmlNewProp(root_node, BAD_CAST "style", BAD_CAST style); const char *style_href = xccdf_benchmark_get_style_href(benchmark); if (style_href) xmlNewProp(root_node, BAD_CAST "style-href", BAD_CAST style_href); // Export plain-text elements struct xccdf_plain_text_iterator *plain_text_it = xccdf_benchmark_get_plain_texts(benchmark); while (xccdf_plain_text_iterator_has_more(plain_text_it)) { struct xccdf_plain_text *plain_text = xccdf_plain_text_iterator_next(plain_text_it); xccdf_plain_text_to_dom(plain_text, doc, root_node, xccdf_benchmark_get_schema_version(benchmark)); } xccdf_plain_text_iterator_free(plain_text_it); /* Handle children */ if (xccdf_benchmark_get_cpe_list(benchmark)) { // CPE API can only export via xmlTextWriter, we export via DOM // this is used to bridge both methods xmlTextWriterPtr writer = xmlNewTextWriterTree(doc, root_node, 0); cpe_dict_export(xccdf_benchmark_get_cpe_list(benchmark), writer); xmlFreeTextWriter(writer); } if (xccdf_benchmark_get_cpe_lang_model(benchmark)) { // CPE API can only export via xmlTextWriter, we export via DOM // this is used to bridge both methods xmlTextWriterPtr writer = xmlNewTextWriterTree(doc, root_node, 0); cpe_lang_export(xccdf_benchmark_get_cpe_lang_model(benchmark), writer); xmlFreeTextWriter(writer); } struct oscap_string_iterator *platforms = xccdf_benchmark_get_platforms(benchmark); while (oscap_string_iterator_has_more(platforms)) { xmlNode *platform_node = xmlNewTextChild(root_node, ns_xccdf, BAD_CAST "platform", NULL); const char *idref = oscap_string_iterator_next(platforms); if (idref) xmlNewProp(platform_node, BAD_CAST "idref", BAD_CAST idref); } oscap_string_iterator_free(platforms); const char *version = xccdf_benchmark_get_version(benchmark); if (version) xmlNewTextChild(root_node, ns_xccdf, BAD_CAST "version", BAD_CAST version); struct oscap_string_iterator* metadata = xccdf_item_get_metadata(XITEM(benchmark)); while (oscap_string_iterator_has_more(metadata)) { const char* meta = oscap_string_iterator_next(metadata); oscap_xmlstr_to_dom(root_node, "metadata", meta); } oscap_string_iterator_free(metadata); OSCAP_FOR(xccdf_model, model, xccdf_benchmark_get_models(benchmark)) { xmlNode *model_node = xmlNewTextChild(root_node, ns_xccdf, BAD_CAST "model", NULL); xmlNewProp(model_node, BAD_CAST "system", BAD_CAST xccdf_model_get_system(model)); }