| 1 | /** |
| 2 | * @file oval_resultSystem.c |
| 3 | * \brief Open Vulnerability and Assessment Language |
| 4 | * |
| 5 | * See more details at http://oval.mitre.org/ |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Copyright 2009--2013 Red Hat Inc., Durham, North Carolina. |
| 10 | * All Rights Reserved. |
| 11 | * |
| 12 | * This library is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU Lesser General Public |
| 14 | * License as published by the Free Software Foundation; either |
| 15 | * version 2.1 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This library is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * Lesser General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public |
| 23 | * License along with this library; if not, write to the Free Software |
| 24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 | * |
| 26 | * Authors: |
| 27 | * "David Niemoller" <[email protected]> |
| 28 | * "Peter Vrabec" <[email protected]> |
| 29 | * Šimon Lukašík |
| 30 | */ |
| 31 | |
| 32 | #ifdef HAVE_CONFIG_H |
| 33 | #include <config.h> |
| 34 | #endif |
| 35 | |
| 36 | #include <stdlib.h> |
| 37 | #include <stdio.h> |
| 38 | #include <string.h> |
| 39 | |
| 40 | #include "oval_definitions.h" |
| 41 | #include "oval_agent_api.h" |
| 42 | #include "oval_agent_api_impl.h" |
| 43 | #include "results/oval_results_impl.h" |
| 44 | #include "adt/oval_collection_impl.h" |
| 45 | #include "adt/oval_smc_impl.h" |
| 46 | #include "adt/oval_smc_iterator_impl.h" |
| 47 | #include "adt/oval_string_map_impl.h" |
| 48 | #include "oval_parser_impl.h" |
| 49 | |
| 50 | #include "common/debug_priv.h" |
| 51 | #include "common/_error.h" |
| 52 | #include "common/util.h" |
| 53 | #include "common/list.h" |
| 54 | |
| 55 | typedef struct oval_result_system { |
| 56 | struct oval_results_model *model; |
| 57 | struct oval_smc *definitions; ///< Map contains lists of oval_result_definition |
| 58 | struct oval_smc *tests; ///< Map contains lists of oval_result_test |
| 59 | struct oval_syschar_model *syschar_model; |
| 60 | } oval_result_system_t; |
| 61 | |
| 62 | |
| 63 | static void _oval_result_system_scan_criteria_for_references(struct oval_result_criteria_node *node, |
| 64 | struct oval_smc *testmap); |
| 65 | |
| 66 | static void _oval_result_system_scan_entity_for_references(struct oval_syschar_model *syschar_model, |
| 67 | struct oval_entity *entity, |
| 68 | struct oval_string_map *objmap, |
| 69 | struct oval_string_map *sttmap, |
| 70 | struct oval_string_map *varmap, |
| 71 | struct oval_string_map *sysmap); |
| 72 | |
| 73 | static void _oval_result_system_scan_set_for_references(struct oval_syschar_model *syschar_model, |
| 74 | struct oval_setobject *set, |
| 75 | struct oval_string_map *objmap, |
| 76 | struct oval_string_map *sttmap, |
| 77 | struct oval_string_map *varmap, |
| 78 | struct oval_string_map *sysmap); |
| 79 | |
| 80 | static void _oval_result_system_scan_object_for_references(struct oval_syschar_model *syschar_model, |
| 81 | struct oval_object *object, |
| 82 | struct oval_string_map *objmap, |
| 83 | struct oval_string_map *sttmap, |
| 84 | struct oval_string_map *varmap, |
| 85 | struct oval_string_map *sysmap); |
| 86 | static void _oval_result_system_scan_state_for_references(struct oval_syschar_model *syschar_model, |
| 87 | struct oval_state *state, |
| 88 | struct oval_string_map *objmap, |
| 89 | struct oval_string_map *sttmap, |
| 90 | struct oval_string_map *varmap, |
| 91 | struct oval_string_map *sysmap); |
| 92 | |
| 93 | static void _oval_result_system_scan_component_for_references(struct oval_syschar_model *syschar_model, |
| 94 | struct oval_component *component, |
| 95 | struct oval_string_map *objmap, |
| 96 | struct oval_string_map *sttmap, |
| 97 | struct oval_string_map *varmap, |
| 98 | struct oval_string_map *sysmap); |
| 99 | |
| 100 | static bool _oval_result_system_resolve_syschar(struct oval_syschar *syschar, struct oval_string_map *sysmap); |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | struct oval_result_system *oval_result_system_new(struct oval_results_model *model, |
| 107 | struct oval_syschar_model *syschar_model) |
| 108 | { |
| 109 | __attribute__nonnull__(model); |
| 110 | oval_result_system_t *sys; |
| 111 | |
| 112 | sys = (oval_result_system_t *) malloc(sizeof(oval_result_system_t)); |
| 113 | if (sys == NULL) |
| 114 | return NULL; |
| 115 | |
| 116 | sys->definitions = oval_smc_new(); |
| 117 | sys->tests = oval_smc_new(); |
| 118 | sys->syschar_model = syschar_model; |
| 119 | sys->model = model; |
| 120 | |
| 121 | oval_results_model_add_system(model, sys); |
| 122 | |
| 123 | return sys; |
| 124 | } |
| 125 | |
| 126 | struct oval_result_system *oval_result_system_clone(struct oval_results_model *new_model, |
| 127 | struct oval_result_system *old_system) |
| 128 | { |
| 129 | __attribute__nonnull__(old_system); |
| 130 | |
| 131 | struct oval_result_system *new_system = |
| 132 | oval_result_system_new(new_model, oval_result_system_get_syschar_model(old_system)); |
| 133 | |
| 134 | oval_smc_free0(new_system->definitions); |
| 135 | oval_smc_free0(new_system->tests); |
| 136 | new_system->definitions = oval_smc_clone_user(old_system->definitions, (oval_smc_user_clone_func) oval_result_definition_clone, new_system); |
| 137 | new_system->tests = oval_smc_clone_user(old_system->tests, (oval_smc_user_clone_func) oval_result_test_clone, new_system); |
| 138 | |
| 139 | return new_system; |
| 140 | } |
| 141 | |
| 142 | void oval_result_system_free(struct oval_result_system *sys) |
| 143 | { |
| 144 | __attribute__nonnull__(sys); |
| 145 | |
| 146 | oval_smc_free(sys->definitions, (oscap_destruct_func) oval_result_definition_free); |
| 147 | oval_smc_free(sys->tests, (oscap_destruct_func) oval_result_test_free); |
| 148 | |
| 149 | sys->definitions = NULL; |
| 150 | sys->syschar_model = NULL; |
| 151 | sys->tests = NULL; |
| 152 | |
| 153 | free(sys); |
| 154 | } |
| 155 | |
| 156 | bool oval_result_system_iterator_has_more(struct oval_result_system_iterator *sys) { |
| 157 | return oval_collection_iterator_has_more((struct oval_iterator *)sys); |
| 158 | } |
| 159 | |
| 160 | struct oval_result_system *oval_result_system_iterator_next(struct oval_result_system_iterator *sys) { |
| 161 | return (struct oval_result_system *) |
| 162 | oval_collection_iterator_next((struct oval_iterator *)sys); |
| 163 | } |
| 164 | |
| 165 | void oval_result_system_iterator_free(struct oval_result_system_iterator *sys) { |
| 166 | oval_collection_iterator_free((struct oval_iterator *)sys); |
| 167 | } |
| 168 | |
| 169 | struct oval_result_definition_iterator *oval_result_system_get_definitions(struct oval_result_system *sys) { |
| 170 | __attribute__nonnull__(sys); |
| 171 | |
| 172 | return oval_result_definition_iterator_new(sys->definitions); |
| 173 | } |
| 174 | |
| 175 | struct oval_result_test_iterator *oval_result_system_get_tests(struct oval_result_system *sys) { |
| 176 | __attribute__nonnull__(sys); |
| 177 | |
| 178 | return oval_result_test_iterator_new(sys->tests); |
| 179 | } |
| 180 | |
| 181 | struct oval_result_definition *oval_result_system_get_definition(struct oval_result_system *sys, const char *id) { |
| 182 | __attribute__nonnull__(sys); |
| 183 | |
| 184 | // Previously, this structure used to hold only one result_definition per given ID. |
| 185 | // Now we need to return the very last one from a list. |
| 186 | return oval_smc_get_last(sys->definitions, id); |
| 187 | } |
| 188 | |
| 189 | struct oval_result_test *oval_result_system_get_test(struct oval_result_system *sys, char *id) { |
| 190 | __attribute__nonnull__(sys); |
| 191 | |
| 192 | // Previously, this structure used to hold only one result_test per given ID. |
| 193 | // Now we need to return the very last one from a list. |
| 194 | return oval_smc_get_last(sys->tests, id); |
| 195 | } |
| 196 | |
| 197 | struct oval_result_definition *oval_result_system_get_new_definition |
| 198 | (struct oval_result_system *sys, struct oval_definition *oval_definition, int variable_instance) { |
| 199 | |
| 200 | return oval_result_system_get_new_definition_with_check(sys, oval_definition, NULL, variable_instance); |
| 201 | |
| 202 | } |
| 203 | |
| 204 | struct oval_result_definition *oval_result_system_get_new_definition_with_check |
| 205 | (struct oval_result_system *sys, struct oval_definition *oval_definition, struct oscap_list *visited_definitions, int variable_instance) |
| 206 | { |
| 207 | // This function is used from multiple different places which might not be sustainable. |
| 208 | // variable_instance=0 means that caller has no special preference for variable_instance |
| 209 | // and the very last definition shall be returned. Additionally, we should create |
| 210 | // new result_definition when the variable_instance_hint does not match. |
| 211 | // variable_instance!=0 means that we should find result_definition with given instance |
| 212 | struct oval_result_definition *rslt_definition = NULL; |
| 213 | if (oval_definition) { |
| 214 | char *id = oval_definition_get_id(oval_definition); |
| 215 | rslt_definition = oval_result_system_get_definition(sys, id); |
| 216 | if (rslt_definition == NULL) { |
| 217 | rslt_definition = make_result_definition_from_oval_definition(sys, oval_definition, visited_definitions, |
| 218 | variable_instance ? variable_instance : 1); |
| 219 | if (rslt_definition == NULL) |
| 220 | return NULL; |
| 221 | oval_result_system_add_definition(sys, rslt_definition); |
| 222 | } |
| 223 | else if (oval_result_definition_get_variable_instance_hint(rslt_definition) != oval_result_definition_get_instance(rslt_definition)) { |
| 224 | int hint = oval_result_definition_get_variable_instance_hint(rslt_definition); |
| 225 | dI("Creating another result-definition for id=%s based on variable_instance: %d", id, hint); |
| 226 | rslt_definition = make_result_definition_from_oval_definition(sys, oval_definition, visited_definitions, hint); |
| 227 | if (rslt_definition == NULL) |
| 228 | return NULL; |
| 229 | oval_result_system_add_definition(sys, rslt_definition); |
| 230 | } |
| 231 | } |
| 232 | return rslt_definition; |
| 233 | } |
| 234 | |
| 235 | struct oval_result_test *oval_result_system_get_new_test(struct oval_result_system *sys, struct oval_test *oval_test, int variable_instance) { |
| 236 | char *id = oval_test_get_id(oval_test); |
| 237 | struct oval_result_test *rslt_testtest = oval_result_system_get_test(sys, id); |
| 238 | if (rslt_testtest == NULL) { |
| 239 | //test = oval_result_test_new(sys, id); |
| 240 | rslt_testtest = make_result_test_from_oval_test(sys, oval_test, variable_instance); |
| 241 | oval_result_system_add_test(sys, rslt_testtest); |
| 242 | } |
| 243 | else if (variable_instance != 0 && oval_result_test_get_instance(rslt_testtest) != variable_instance) { |
| 244 | dI("Creating another result-test for id=%s based on variable_instance: %d", id, variable_instance); |
| 245 | rslt_testtest = make_result_test_from_oval_test(sys, oval_test, variable_instance); |
| 246 | oval_result_system_add_test(sys, rslt_testtest); |
| 247 | } |
| 248 | return rslt_testtest; |
| 249 | } |
| 250 | |
| 251 | struct oval_results_model *oval_result_system_get_results_model(struct oval_result_system *sys) { |
| 252 | __attribute__nonnull__(sys); |
| 253 | |
| 254 | return sys->model; |
| 255 | } |
| 256 | |
| 257 | struct oval_syschar_model *oval_result_system_get_syschar_model(struct oval_result_system *sys) { |
| 258 | __attribute__nonnull__(sys); |
| 259 | |
| 260 | return sys->syschar_model; |
| 261 | } |
| 262 | |
| 263 | struct oval_sysinfo *oval_result_system_get_sysinfo(struct oval_result_system *sys) { |
| 264 | struct oval_syschar_model *syschar_model = oval_result_system_get_syschar_model(sys); |
| 265 | return (syschar_model) |
| 266 | ? oval_syschar_model_get_sysinfo(syschar_model) : NULL; |
| 267 | } |
| 268 | |
| 269 | void oval_result_system_add_definition(struct oval_result_system *sys, struct oval_result_definition *definition) |
| 270 | { |
| 271 | __attribute__nonnull__(sys); |
| 272 | if (definition) { |
| 273 | const char *id = oval_result_definition_get_id(definition); |
| 274 | oval_smc_put_last(sys->definitions, id, definition); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void oval_result_system_add_test(struct oval_result_system *sys, struct oval_result_test *test) |
| 279 | { |
| 280 | __attribute__nonnull__(sys); |
| 281 | if (test) { |
| 282 | const char *id = oval_result_test_get_id(test); |
| 283 | oval_smc_put_last(sys->tests, id, test); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | static int oval_result_system_parse(xmlTextReaderPtr reader, struct oval_parser_context *context, void *usr) { |
| 288 | __attribute__nonnull__(usr); |
| 289 | |
| 290 | struct oval_result_system *sys = (struct oval_result_system *) usr; |
| 291 | xmlChar *localName = xmlTextReaderLocalName(reader); |
| 292 | |
| 293 | int return_code = 0; |
| 294 | if (strcmp((const char *)localName, "definitions") == 0) { |
| 295 | return_code = oval_parser_parse_tag(reader, context, oval_result_definition_parse_tag, sys); |
| 296 | } else if (strcmp((const char *)localName, "tests") == 0) { |
| 297 | return_code = oval_parser_parse_tag(reader, context, oval_result_test_parse_tag, sys); |
| 298 | } else if (strcmp((const char *)localName, OVAL_ROOT_ELM_SYSCHARS) == 0) { |
| 299 | return_code = oval_syschar_model_parse(reader, context); |
| 300 | } else { |
| 301 | dW("Skipping tag: %s", localName); |
| 302 | oval_parser_skip_tag(reader, context); |
| 303 | } |
| 304 | |
| 305 | if (return_code != 0) { |
| 306 | dW("Parsing of <%s> terminated by an error at line %d.", localName, xmlTextReaderGetParserLineNumber(reader)); |
| 307 | } |
| 308 | |
| 309 | free(localName); |
| 310 | return return_code; |
| 311 | } |
| 312 | |
| 313 | int oval_result_system_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *usr) { |
| 314 | __attribute__nonnull__(context); |
| 315 | |
| 316 | char *tagname = (char *)xmlTextReaderLocalName(reader); |
| 317 | char *namespace = (char *)xmlTextReaderNamespaceUri(reader); |
| 318 | int return_code = 0; |
| 319 | |
| 320 | int is_ovalres = 0; |
| 321 | if (namespace != NULL) { |
| 322 | is_ovalres = strcmp((const char *)OVAL_RESULTS_NAMESPACE, namespace) == 0; |
| 323 | } |
| 324 | if (is_ovalres && (strcmp(tagname, "system") == 0)) { |
| 325 | /* create new empty syschar for this model */ |
| 326 | struct oval_syschar_model *syschar_model = oval_syschar_model_new(context->definition_model); |
| 327 | context->syschar_model = syschar_model; |
| 328 | /* create new results system instance */ |
| 329 | struct oval_result_system *sys = oval_result_system_new(context->results_model, syschar_model); |
| 330 | /* populate results system */ |
| 331 | return_code = oval_parser_parse_tag(reader, context, oval_result_system_parse, sys); |
| 332 | } else { |
| 333 | dW("Skipping tag: %s", tagname); |
| 334 | oval_parser_skip_tag(reader, context); |
| 335 | } |
| 336 | |
| 337 | if (return_code != 0) { |
| 338 | dW("Parsing of <%s> terminated by an error at line %d.", tagname, xmlTextReaderGetParserLineNumber(reader)); |
| 339 | } |
| 340 | |
| 341 | free(tagname); |
| 342 | free(namespace); |
| 343 | |
| 344 | return return_code; |
| 345 | } |
| 346 | |
| 347 | int oval_result_system_eval(struct oval_result_system *sys) |
| 348 | { |
| 349 | struct oval_results_model *res_model; |
| 350 | struct oval_definition_model *definition_model; |
| 351 | struct oval_definition_iterator *definitions_itr; |
| 352 | |
| 353 | res_model = oval_result_system_get_results_model(sys); |
| 354 | definition_model = oval_results_model_get_definition_model(res_model); |
| 355 | definitions_itr = oval_definition_model_get_definitions(definition_model); |
| 356 | |
| 357 | while (oval_definition_iterator_has_more(definitions_itr)) { |
| 358 | struct oval_definition *definition; |
| 359 | struct oval_result_definition *rslt_definition; |
| 360 | |
| 361 | definition = oval_definition_iterator_next(definitions_itr); |
| 362 | rslt_definition = oval_result_system_get_new_definition(sys, definition, 0); |
| 363 | oval_result_definition_eval(rslt_definition); |
| 364 | } |
| 365 | |
| 366 | oval_definition_iterator_free(definitions_itr); |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | int oval_result_system_eval_definition(struct oval_result_system *sys, const char *id) |
| 371 | { |
| 372 | struct oval_result_definition *rslt_definition = oval_result_system_prepare_definition(sys, id); |
| 373 | if (rslt_definition == NULL) { |
| 374 | return -1; |
| 375 | } |
| 376 | oval_result_definition_eval(rslt_definition); |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | struct oval_result_definition *oval_result_system_prepare_definition(struct oval_result_system *sys, const char *id) |
| 381 | { |
| 382 | struct oval_results_model *res_model; |
| 383 | struct oval_definition_model *definition_model; |
| 384 | struct oval_definition *oval_definition; |
| 385 | struct oval_result_definition *rslt_definition; |
| 386 | |
| 387 | res_model = oval_result_system_get_results_model(sys); |
| 388 | definition_model = oval_results_model_get_definition_model(res_model); |
| 389 | oval_definition = oval_definition_model_get_definition(definition_model, id); |
| 390 | if (oval_definition == NULL) { |
| 391 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "No definition with ID: %s in definition model.", id); |
| 392 | return NULL; |
| 393 | } |
| 394 | |
| 395 | struct oscap_list *visited_definitions = oscap_list_new(); |
| 396 | rslt_definition = oval_result_system_get_definition(sys, id); |
| 397 | if (rslt_definition == NULL) { |
| 398 | rslt_definition = make_result_definition_from_oval_definition(sys, oval_definition, visited_definitions, 1); |
| 399 | if (rslt_definition == NULL) |
| 400 | goto cleanup; |
| 401 | oval_result_system_add_definition(sys, rslt_definition); |
| 402 | } |
| 403 | else if (oval_result_definition_get_variable_instance_hint(rslt_definition) != oval_result_definition_get_instance(rslt_definition)) { |
| 404 | int hint = oval_result_definition_get_variable_instance_hint(rslt_definition); |
| 405 | dI("Creating another result-definition for id=%s based on variable_instance: %d", id, hint); |
| 406 | rslt_definition = make_result_definition_from_oval_definition(sys, oval_definition, visited_definitions, hint); |
| 407 | if (rslt_definition == NULL) |
| 408 | goto cleanup; |
| 409 | oval_result_system_add_definition(sys, rslt_definition); |
| 410 | } |
| 411 | cleanup: |
| 412 | oscap_list_free(visited_definitions, free); |
| 413 | return rslt_definition; |
| 414 | } |
| 415 | |
| 416 | static void _oval_result_definition_to_dom_based_on_directives(struct oval_result_definition *rslt_definition, |
| 417 | struct oval_result_directives * directives, |
| 418 | xmlDocPtr doc, |
| 419 | xmlNode *definitions_node, |
| 420 | struct oval_smc *tstmap) |
| 421 | { |
| 422 | oval_result_t result = oval_result_definition_get_result(rslt_definition); |
| 423 | if (oval_result_directives_get_reported(directives, result)) { |
| 424 | oval_result_directive_content_t content = oval_result_directives_get_content(directives, result); |
| 425 | /* report definition according to directives settings */ |
| 426 | oval_result_definition_to_dom(rslt_definition, content, doc, definitions_node); |
| 427 | if (content == OVAL_DIRECTIVE_CONTENT_FULL) { |
| 428 | struct oval_result_criteria_node *criteria = oval_result_definition_get_criteria(rslt_definition); |
| 429 | /* collect the tests that are referenced from reported definitions */ |
| 430 | if (criteria) |
| 431 | _oval_result_system_scan_criteria_for_references(criteria, tstmap); |
| 432 | } else if (content == OVAL_DIRECTIVE_CONTENT_THIN) { |
| 433 | // NOOP |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | xmlNode *oval_result_system_to_dom(struct oval_result_system * sys, |
| 439 | struct oval_results_model * results_model, |
| 440 | struct oval_directives_model * directives_model, |
| 441 | xmlDocPtr doc, xmlNode * parent) { |
| 442 | |
| 443 | struct oval_result_directives * directives; |
| 444 | struct oval_result_directives * class_dirs; |
| 445 | struct oval_result_directives * def_dirs = oval_directives_model_get_defdirs(directives_model); |
| 446 | |
| 447 | xmlNs *ns_results = xmlSearchNsByHref(doc, parent, OVAL_RESULTS_NAMESPACE); |
| 448 | xmlNode *system_node = xmlNewTextChild(parent, ns_results, BAD_CAST "system", NULL); |
| 449 | |
| 450 | struct oval_smc *tstmap = oval_smc_new(); |
| 451 | |
| 452 | struct oval_definition_model *definition_model = oval_results_model_get_definition_model(results_model); |
| 453 | struct oval_definition_iterator *oval_definitions = oval_definition_model_get_definitions(definition_model); |
| 454 | if(oval_definition_iterator_has_more(oval_definitions)) { |
| 455 | xmlNode *definitions_node = xmlNewTextChild(system_node, ns_results, BAD_CAST "definitions", NULL); |
| 456 | while(oval_definition_iterator_has_more(oval_definitions)) { |
| 457 | struct oval_definition *oval_definition = oval_definition_iterator_next(oval_definitions); |
| 458 | |
| 459 | oval_definition_class_t def_class = oval_definition_get_class(oval_definition); |
| 460 | class_dirs = oval_directives_model_get_classdir(directives_model, def_class); |
| 461 | directives = class_dirs ? class_dirs : def_dirs; |
| 462 | |
| 463 | bool exported = false; |
| 464 | struct oval_iterator *rslt_definitions_it = oval_smc_get_all_it(sys->definitions, oval_definition_get_id(oval_definition)); |
| 465 | if (rslt_definitions_it != NULL) { |
| 466 | while (oval_collection_iterator_has_more(rslt_definitions_it)) { |
| 467 | struct oval_result_definition *rslt_definition = oval_collection_iterator_next(rslt_definitions_it); |
| 468 | _oval_result_definition_to_dom_based_on_directives(rslt_definition, directives, doc, definitions_node, tstmap); |
| 469 | exported = true; |
| 470 | } |
| 471 | oval_collection_iterator_free(rslt_definitions_it); |
| 472 | } |
| 473 | if (!exported) { |
| 474 | struct oval_result_definition *rslt_definition = oval_result_system_get_new_definition(sys, oval_definition, 1); |
| 475 | if (rslt_definition) { |
| 476 | _oval_result_definition_to_dom_based_on_directives(rslt_definition, directives, doc, definitions_node, tstmap); |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | oval_definition_iterator_free(oval_definitions); |
| 482 | |
| 483 | struct oval_syschar_model *syschar_model = oval_result_system_get_syschar_model(sys); |
| 484 | struct oval_string_map *sysmap = oval_string_map_new(); |
| 485 | struct oval_string_map *objmap = oval_string_map_new(); |
| 486 | struct oval_string_map *sttmap = oval_string_map_new(); |
| 487 | struct oval_string_map *varmap = oval_string_map_new(); |
| 488 | |
| 489 | struct oval_smc_iterator *result_tests = oval_smc_iterator_new(tstmap); |
| 490 | if (oval_smc_iterator_has_more(result_tests)) { |
| 491 | xmlNode *tests_node = xmlNewTextChild(system_node, ns_results, BAD_CAST "tests", NULL); |
| 492 | while (oval_smc_iterator_has_more(result_tests)) { |
| 493 | struct oval_state_iterator *ste_itr; |
| 494 | struct oval_result_test *result_test = oval_smc_iterator_next(result_tests); |
| 495 | /* report the test */ |
| 496 | oval_result_test_to_dom(result_test, doc, tests_node); |
| 497 | struct oval_test *oval_test = oval_result_test_get_test(result_test); |
| 498 | /* collect the objects that are referenced from reported test */ |
| 499 | /* look for objects in path: test->object ... */ |
| 500 | struct oval_object *object = oval_test_get_object(oval_test); |
| 501 | if (object) { |
| 502 | char *objid = oval_object_get_id(object); |
| 503 | /* is object already "collected" */ |
| 504 | void *value = oval_string_map_get_value(objmap, objid); |
| 505 | if (value == NULL) { |
| 506 | /* is there a system characteristic for the object */ |
| 507 | struct oval_syschar *syschar = |
| 508 | oval_syschar_model_get_syschar(syschar_model, objid); |
| 509 | if (syschar) { |
| 510 | /* put object into map */ |
| 511 | oval_string_map_put(objmap, objid, object); |
| 512 | _oval_result_system_scan_object_for_references |
| 513 | (syschar_model, object, objmap, sttmap, varmap, sysmap); |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | /* look for objects in test->state->... */ |
| 518 | ste_itr = oval_test_get_states(oval_test); |
| 519 | while (oval_state_iterator_has_more(ste_itr)) { |
| 520 | struct oval_state *state = oval_state_iterator_next(ste_itr); |
| 521 | char *sttid = oval_state_get_id(state); |
| 522 | void *value = oval_string_map_get_value(sttmap, sttid); |
| 523 | if (value == NULL) { |
| 524 | oval_string_map_put(sttmap, sttid, state); |
| 525 | _oval_result_system_scan_state_for_references(syschar_model, state, objmap, sttmap, |
| 526 | varmap, sysmap); |
| 527 | } |
| 528 | } |
| 529 | oval_state_iterator_free(ste_itr); |
| 530 | } |
| 531 | } |
| 532 | oval_smc_iterator_free(result_tests); |
| 533 | |
| 534 | bool export_sys_char = oval_results_model_get_export_system_characteristics(results_model); |
| 535 | oval_syschar_model_to_dom(syschar_model, doc, system_node, |
| 536 | (oval_syschar_resolver *) _oval_result_system_resolve_syschar, sysmap, export_sys_char); |
| 537 | |
| 538 | oval_string_map_free(sysmap, NULL); |
| 539 | oval_string_map_free(objmap, NULL); |
| 540 | oval_string_map_free(sttmap, NULL); |
| 541 | oval_string_map_free(varmap, NULL); |
| 542 | oval_smc_free0(tstmap); |
| 543 | |
| 544 | return system_node; |
| 545 | } |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | static void _oval_result_system_scan_criteria_for_references |
| 552 | (struct oval_result_criteria_node *node, struct oval_smc *testmap) { |
| 553 | struct oval_result_criteria_node_iterator *subnodes = oval_result_criteria_node_get_subnodes(node); |
| 554 | while (subnodes && oval_result_criteria_node_iterator_has_more(subnodes)) { |
| 555 | struct oval_result_criteria_node *subnode = oval_result_criteria_node_iterator_next(subnodes); |
| 556 | _oval_result_system_scan_criteria_for_references(subnode, testmap); |
| 557 | } |
| 558 | oval_result_criteria_node_iterator_free(subnodes); |
| 559 | struct oval_result_test *result_test = oval_result_criteria_node_get_test(node); |
| 560 | if (result_test) { |
| 561 | const char *testid = oval_result_test_get_id(result_test); |
| 562 | oval_smc_put_last_if_not_exists(testmap, testid, result_test); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | static void _oval_result_system_scan_object_for_references |
| 567 | (struct oval_syschar_model *syschar_model, struct oval_object *object, |
| 568 | struct oval_string_map *objmap, |
| 569 | struct oval_string_map *sttmap, |
| 570 | struct oval_string_map *varmap, |
| 571 | struct oval_string_map *sysmap) { |
| 572 | char *objid = oval_object_get_id(object); |
| 573 | struct oval_syschar *syschar = oval_syschar_model_get_syschar(syschar_model, objid); |
| 574 | if (syschar) /* put syschar of the object into map */ |
| 575 | oval_string_map_put(sysmap, objid, syschar); |
| 576 | struct oval_object_content_iterator *contents = oval_object_get_object_contents(object); |
| 577 | /* see if there are more objects referenced from this object's content */ |
| 578 | while (oval_object_content_iterator_has_more(contents)) { |
| 579 | struct oval_object_content *content = oval_object_content_iterator_next(contents); |
| 580 | struct oval_entity *entity = oval_object_content_get_entity(content); |
| 581 | if (entity) |
| 582 | _oval_result_system_scan_entity_for_references(syschar_model, entity, objmap, sttmap, varmap, |
| 583 | sysmap); |
| 584 | struct oval_setobject *set = oval_object_content_get_setobject(content); |
| 585 | if (set) |
| 586 | _oval_result_system_scan_set_for_references(syschar_model, set, objmap, sttmap, varmap, sysmap); |
| 587 | } |
| 588 | oval_object_content_iterator_free(contents); |
| 589 | } |
| 590 | |
| 591 | static void _oval_result_system_scan_state_for_references |
| 592 | (struct oval_syschar_model *syschar_model, struct oval_state *state, |
| 593 | struct oval_string_map *objmap, |
| 594 | struct oval_string_map *sttmap, struct oval_string_map *varmap, struct oval_string_map *sysmap) { |
| 595 | struct oval_state_content_iterator *contents = oval_state_get_contents(state); |
| 596 | while (oval_state_content_iterator_has_more(contents)) { |
| 597 | struct oval_state_content *content = oval_state_content_iterator_next(contents); |
| 598 | struct oval_entity *entity = oval_state_content_get_entity(content); |
| 599 | if (entity) |
| 600 | _oval_result_system_scan_entity_for_references(syschar_model, entity, objmap, sttmap, varmap, |
| 601 | sysmap); |
| 602 | } |
| 603 | oval_state_content_iterator_free(contents); |
| 604 | } |
| 605 | |
| 606 | static void _oval_result_system_scan_component_for_references |
| 607 | (struct oval_syschar_model *syschar_model, struct oval_component *component, |
| 608 | struct oval_string_map *objmap, |
| 609 | struct oval_string_map *sttmap, struct oval_string_map *varmap, struct oval_string_map *sysmap) { |
| 610 | struct oval_variable *variable = oval_component_get_variable(component); |
| 611 | if (variable) { |
| 612 | char *varid = oval_variable_get_id(variable); |
| 613 | void *value = oval_string_map_get_value(varmap, varid); |
| 614 | if (value == NULL) { |
| 615 | oval_string_map_put(varmap, varid, variable); |
| 616 | struct oval_component *component2 = oval_variable_get_component(variable); |
| 617 | if (component2) { |
| 618 | _oval_result_system_scan_component_for_references(syschar_model, component2, objmap, |
| 619 | sttmap, varmap, sysmap); |
| 620 | } |
| 621 | } |
| 622 | } else { |
| 623 | struct oval_component_iterator *fcomponents = oval_component_get_function_components(component); |
| 624 | if (fcomponents) |
| 625 | while (oval_component_iterator_has_more(fcomponents)) { |
| 626 | struct oval_component *fcomponent = oval_component_iterator_next(fcomponents); |
| 627 | _oval_result_system_scan_component_for_references(syschar_model, fcomponent, objmap, |
| 628 | sttmap, varmap, sysmap); |
| 629 | } |
| 630 | oval_component_iterator_free(fcomponents); |
| 631 | |
| 632 | struct oval_object *object = oval_component_get_object(component); |
| 633 | if (object) { |
| 634 | char *objid = oval_object_get_id(object); |
| 635 | void *value = oval_string_map_get_value(objmap, objid); |
| 636 | if (value == NULL) { |
| 637 | oval_string_map_put(objmap, objid, object); |
| 638 | _oval_result_system_scan_object_for_references(syschar_model, object, objmap, sttmap, |
| 639 | varmap, sysmap); |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | static void _oval_result_system_scan_entity_for_references |
| 646 | (struct oval_syschar_model *syschar_model, struct oval_entity *entity, |
| 647 | struct oval_string_map *objmap, |
| 648 | struct oval_string_map *sttmap, struct oval_string_map *varmap, struct oval_string_map *sysmap) { |
| 649 | struct oval_variable *variable = oval_entity_get_variable(entity); |
| 650 | if (variable) { |
| 651 | char *varid = oval_variable_get_id(variable); |
| 652 | void *value = oval_string_map_get_value(varmap, varid); |
| 653 | if (value == NULL) { |
| 654 | oval_string_map_put(varmap, varid, variable); |
| 655 | struct oval_component *component = oval_variable_get_component(variable); |
| 656 | if (component) { |
| 657 | _oval_result_system_scan_component_for_references(syschar_model, component, objmap, |
| 658 | sttmap, varmap, sysmap); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | static void _oval_result_system_scan_set_for_references |
| 665 | (struct oval_syschar_model *syschar_model, struct oval_setobject *set, |
| 666 | struct oval_string_map *objmap, |
| 667 | struct oval_string_map *sttmap, struct oval_string_map *varmap, struct oval_string_map *sysmap) { |
| 668 | struct oval_object_iterator *objects = oval_setobject_get_objects(set); |
| 669 | if (objects) |
| 670 | while (oval_object_iterator_has_more(objects)) { |
| 671 | struct oval_object *object = oval_object_iterator_next(objects); |
| 672 | char *objid = oval_object_get_id(object); |
| 673 | void *value = oval_string_map_get_value(objmap, objid); |
| 674 | if (value == NULL) { |
| 675 | oval_string_map_put(objmap, objid, object); |
| 676 | _oval_result_system_scan_object_for_references(syschar_model, object, objmap, sttmap, |
| 677 | varmap, sysmap); |
| 678 | } |
| 679 | } |
| 680 | oval_object_iterator_free(objects); |
| 681 | struct oval_filter_iterator *filters = oval_setobject_get_filters(set); |
| 682 | if (filters) |
| 683 | while (oval_filter_iterator_has_more(filters)) { |
| 684 | struct oval_filter *filter; |
| 685 | struct oval_state *state; |
| 686 | |
| 687 | filter = oval_filter_iterator_next(filters); |
| 688 | state = oval_filter_get_state(filter); |
| 689 | char *sttid = oval_state_get_id(state); |
| 690 | void *value = oval_string_map_get_value(sttmap, sttid); |
| 691 | if (value == NULL) { |
| 692 | oval_string_map_put(sttmap, sttid, state); |
| 693 | _oval_result_system_scan_state_for_references(syschar_model, state, objmap, sttmap, |
| 694 | varmap, sysmap); |
| 695 | } |
| 696 | } |
| 697 | oval_filter_iterator_free(filters); |
| 698 | struct oval_setobject_iterator *subsets = oval_setobject_get_subsets(set); |
| 699 | if (subsets) |
| 700 | while (oval_setobject_iterator_has_more(subsets)) { |
| 701 | struct oval_setobject *subset = oval_setobject_iterator_next(subsets); |
| 702 | _oval_result_system_scan_set_for_references(syschar_model, subset, objmap, sttmap, varmap, |
| 703 | sysmap); |
| 704 | } |
| 705 | oval_setobject_iterator_free(subsets); |
| 706 | } |
| 707 | |
| 708 | static bool _oval_result_system_resolve_syschar(struct oval_syschar *syschar, struct oval_string_map *sysmap) { |
| 709 | const char *objid = oval_syschar_get_id(syschar); |
| 710 | return oval_string_map_get_value(sysmap, objid) != NULL; |
| 711 | } |
| 712 |