struct xccdf_benchmark *xccdf_benchmark_new(void) { struct xccdf_item *bench = xccdf_item_new(XCCDF_BENCHMARK, NULL); bench->sub.benchmark.schema_version = NULL; // lists bench->sub.benchmark.rear_matter = oscap_list_new(); bench->sub.benchmark.front_matter = oscap_list_new(); bench->sub.benchmark.notices = oscap_list_new(); bench->sub.benchmark.models = oscap_list_new(); bench->sub.benchmark.content = oscap_list_new(); bench->sub.benchmark.values = oscap_list_new(); bench->sub.benchmark.plain_texts = oscap_list_new(); bench->sub.benchmark.cpe_list = NULL; bench->sub.benchmark.cpe_lang_model = NULL; bench->sub.benchmark.profiles = oscap_list_new(); bench->sub.benchmark.results = oscap_list_new(); // hash tables bench->sub.benchmark.items_dict = oscap_htable_new(); bench->sub.benchmark.profiles_dict = oscap_htable_new(); bench->sub.benchmark.results_dict = oscap_htable_new(); bench->sub.benchmark.clusters_dict = oscap_htable_new(); // add the implied default scoring model struct xccdf_model *default_model = xccdf_model_new(); xccdf_model_set_system(default_model, "urn:xccdf:scoring:default"); assume_ex(xccdf_benchmark_add_model(XBENCHMARK(bench), default_model), XBENCHMARK(bench)); return XBENCHMARK(bench); }
void ds_sds_session_reset(struct ds_sds_session *session) { session->checklist_id = NULL; session->datastream_id = NULL; session->target_dir = NULL; oscap_htable_free(session->component_sources, (oscap_destruct_func) oscap_source_free); session->component_sources = oscap_htable_new(); }
struct ds_rds_session *ds_rds_session_new_from_source(struct oscap_source *source) { if (oscap_source_get_scap_type(source) != OSCAP_DOCUMENT_ARF) { oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not create Result DataStream " "session: File is not Result DataStream."); return NULL; } struct ds_rds_session *rds_session = (struct ds_rds_session *) oscap_calloc(1, sizeof(struct ds_rds_session)); rds_session->source = source; rds_session->component_sources = oscap_htable_new(); return rds_session; }
struct ds_sds_session *ds_sds_session_new_from_source(struct oscap_source *source) { if (oscap_source_get_scap_type(source) != OSCAP_DOCUMENT_SDS) { oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not create Source DataStream " "session: File is not Source DataStream."); return NULL; } struct ds_sds_session *sds_session = (struct ds_sds_session *) oscap_calloc(1, sizeof(struct ds_sds_session)); sds_session->source = source; sds_session->component_sources = oscap_htable_new(); sds_session->progress = download_progress_empty_calllback; return sds_session; }
struct cpe_lang_model *cpe_lang_model_new() { struct cpe_lang_model *ret; ret = oscap_alloc(sizeof(struct cpe_lang_model)); if (ret == NULL) return NULL; ret->platforms = oscap_list_new(); ret->item = oscap_htable_new(); ret->origin_file = NULL; return ret; }
int ds_rds_create(const char* sds_file, const char* xccdf_result_file, const char** oval_result_files, const char* target_file) { struct oscap_source *sds_source = oscap_source_new_from_file(sds_file); struct oscap_source *xccdf_result_source = oscap_source_new_from_file(xccdf_result_file); struct oscap_htable *oval_result_sources = oscap_htable_new(); int result = 0; // this check is there to allow passing NULL instead of having to allocate // an empty array if (oval_result_files != NULL) { while (*oval_result_files != NULL) { struct oscap_source *oval_source = oscap_source_new_from_file(*oval_result_files); if (oscap_source_get_xmlDoc(oval_source) == NULL) { result = -1; oscap_source_free(oval_source); } else { oscap_htable_add(oval_result_sources, *oval_result_files, oval_source); } oval_result_files++; } } if (result == 0) { struct oscap_source *target_rds = ds_rds_create_source(sds_source, xccdf_result_source, oval_result_sources, target_file); result = target_rds == NULL; if (result == 0) { result = oscap_source_save_as(target_rds, NULL); } oscap_source_free(target_rds); } oscap_htable_free(oval_result_sources, (oscap_destruct_func) oscap_source_free); oscap_source_free(sds_source); oscap_source_free(xccdf_result_source); return result; }
static int ds_sds_compose_add_component_dependencies(xmlDocPtr doc, xmlNodePtr datastream, struct oscap_source *component_source, xmlNodePtr catalog, int component_type) { xmlDocPtr component_doc = oscap_source_get_xmlDoc(component_source); if (component_doc == NULL) { return -1; } xmlXPathContextPtr xpathCtx = xmlXPathNewContext(component_doc); if (xpathCtx == NULL) { oscap_seterr(OSCAP_EFAMILY_XML, "Error: unable to create new XPath context."); return -1; } xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression( // we want robustness and support for future versions, this expression // retrieves check-content-refs from any namespace BAD_CAST _get_dep_xpath_for_type(component_type), xpathCtx); if (xpathObj == NULL) { oscap_seterr(OSCAP_EFAMILY_XML, "Error: Unable to evalute XPath expression."); xmlXPathFreeContext(xpathCtx); return -1; } xmlNsPtr cat_ns = xmlSearchNsByHref(doc, datastream, BAD_CAST cat_ns_uri); xmlNodeSetPtr nodeset = xpathObj->nodesetval; if (nodeset != NULL) { struct oscap_htable *exported = oscap_htable_new(); char* filepath_cpy = oscap_strdup(oscap_source_readable_origin(component_source)); const char* dir = dirname(filepath_cpy); for (int i = 0; i < nodeset->nodeNr; i++) { xmlNodePtr node = nodeset->nodeTab[i]; if (node->type != XML_ELEMENT_NODE) continue; if (xmlHasProp(node, BAD_CAST "href")) { char* href = (char*)xmlGetProp(node, BAD_CAST "href"); if (oscap_htable_get(exported, href) != NULL) { // This path has been already exported. Do not export duplicate. xmlFree(href); continue; } oscap_htable_add(exported, href, ""); if (oscap_acquire_url_is_supported(href)) { /* If the referenced component is remote one, do not include * it within the DataStream. Such component shall only be * downloaded once the scan is run. */ xmlFree(href); continue; } char* real_path = (strcmp(dir, "") == 0 || strcmp(dir, ".") == 0) ? oscap_strdup(href) : oscap_sprintf("%s/%s", dir, href); char* mangled_path = ds_sds_mangle_filepath(real_path); char* cref_id = oscap_sprintf("scap_org.open-scap_cref_%s", mangled_path); int counter = 0; while (ds_sds_find_component_ref(datastream, cref_id) != NULL) { // While the given component ID already exists in the document. oscap_free(cref_id); cref_id = oscap_sprintf("scap_org.open-scap_cref_%s%03d", mangled_path, counter++); } oscap_free(mangled_path); char* uri = oscap_sprintf("#%s", cref_id); // we don't want duplicated uri elements in the catalog if (ds_sds_compose_catalog_has_uri(doc, catalog, uri) == 0) { oscap_free(uri); oscap_free(cref_id); oscap_free(real_path); xmlFree(href); continue; } int ret = ds_sds_compose_add_component_with_ref(doc, datastream, real_path, cref_id); if (ret == 0) { xmlNodePtr catalog_uri = xmlNewNode(cat_ns, BAD_CAST "uri"); xmlSetProp(catalog_uri, BAD_CAST "name", BAD_CAST href); xmlSetProp(catalog_uri, BAD_CAST "uri", BAD_CAST uri); xmlAddChild(catalog, catalog_uri); } oscap_free(cref_id); oscap_free(uri); oscap_free(real_path); xmlFree(href); if (ret < 0) { // oscap_seterr has already been called oscap_htable_free0(exported); return -1; } } } oscap_htable_free0(exported); oscap_free(filepath_cpy); } xmlXPathFreeObject(xpathObj); xmlXPathFreeContext(xpathCtx); return 0; }