| 1 | /** |
| 2 | * @file oval_defModel.c |
| 3 | * \brief Open Vulnerability and Assessment Language |
| 4 | * |
| 5 | * See more details at http://oval.mitre.org/ |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Copyright 2009--2014 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 | * Šimon Lukašík |
| 29 | */ |
| 30 | |
| 31 | #ifdef HAVE_CONFIG_H |
| 32 | #include <config.h> |
| 33 | #endif |
| 34 | |
| 35 | #include <string.h> |
| 36 | #include <time.h> |
| 37 | |
| 38 | #include "oval_definitions_impl.h" |
| 39 | #include "oval_agent_api_impl.h" |
| 40 | #include "oval_parser_impl.h" |
| 41 | #include "adt/oval_string_map_impl.h" |
| 42 | #include "oval_system_characteristics_impl.h" |
| 43 | #if defined(OVAL_PROBES_ENABLED) |
| 44 | # include "oval_probe_impl.h" |
| 45 | #endif |
| 46 | #include "common/util.h" |
| 47 | #include "common/debug_priv.h" |
| 48 | #include "common/_error.h" |
| 49 | #include "common/elements.h" |
| 50 | #include "oscap_source.h" |
| 51 | #include "source/oscap_source_priv.h" |
| 52 | |
| 53 | typedef struct oval_definition_model { |
| 54 | struct oval_generator *generator; |
| 55 | struct oval_string_map *definition_map; |
| 56 | struct oval_string_map *test_map; |
| 57 | struct oval_string_map *object_map; |
| 58 | struct oval_string_map *state_map; |
| 59 | struct oval_string_map *variable_map; |
| 60 | struct oval_collection *bound_variable_models; |
| 61 | char *schema; |
| 62 | struct oval_string_map *vardef_map; ///< look-up table for efficient @variable_instance processing |
| 63 | } oval_definition_model_t; |
| 64 | |
| 65 | /* failed - NULL |
| 66 | * success - oval_definition_model |
| 67 | * */ |
| 68 | struct oval_definition_model *oval_definition_model_new() |
| 69 | { |
| 70 | oval_definition_model_t *newmodel = (oval_definition_model_t *) malloc(sizeof(oval_definition_model_t)); |
| 71 | if (newmodel == NULL) |
| 72 | return NULL; |
| 73 | |
| 74 | newmodel->generator = oval_generator_new(); |
| 75 | newmodel->definition_map = oval_string_map_new(); |
| 76 | newmodel->object_map = oval_string_map_new(); |
| 77 | newmodel->state_map = oval_string_map_new(); |
| 78 | newmodel->test_map = oval_string_map_new(); |
| 79 | newmodel->variable_map = oval_string_map_new(); |
| 80 | newmodel->bound_variable_models = NULL; |
| 81 | newmodel->schema = oscap_strdup(OVAL_DEF_SCHEMA_LOCATION); |
| 82 | newmodel->vardef_map = NULL; |
| 83 | |
| 84 | return newmodel; |
| 85 | } |
| 86 | |
| 87 | typedef void *(*_oval_clone_func) (void *, struct oval_definition_model *); |
| 88 | |
| 89 | static void _oval_definition_model_clone(struct oval_string_map *oldmap, |
| 90 | struct oval_definition_model *newmodel, |
| 91 | _oval_clone_func cloner) |
| 92 | { |
| 93 | struct oval_string_iterator *keys = (struct oval_string_iterator *)oval_string_map_keys(oldmap); |
| 94 | while (oval_string_iterator_has_more(keys)) { |
| 95 | char *key = oval_string_iterator_next(keys); |
| 96 | void *olditem = oval_string_map_get_value(oldmap, key); |
| 97 | (*cloner) (newmodel, olditem); |
| 98 | } |
| 99 | oval_string_iterator_free(keys); |
| 100 | } |
| 101 | |
| 102 | /* failed - NULL |
| 103 | * success - oval_definition_model |
| 104 | * */ |
| 105 | struct oval_definition_model *oval_definition_model_clone(struct oval_definition_model *oldmodel) |
| 106 | { |
| 107 | __attribute__nonnull__(oldmodel); |
| 108 | |
| 109 | struct oval_definition_model *newmodel = oval_definition_model_new(); |
| 110 | if (newmodel == NULL) |
| 111 | return NULL; |
| 112 | |
| 113 | _oval_definition_model_clone |
| 114 | (oldmodel->definition_map, newmodel, (_oval_clone_func) oval_definition_clone); |
| 115 | _oval_definition_model_clone |
| 116 | (oldmodel->object_map, newmodel, (_oval_clone_func) oval_object_clone); |
| 117 | _oval_definition_model_clone(oldmodel->state_map, newmodel, (_oval_clone_func) oval_state_clone); |
| 118 | _oval_definition_model_clone(oldmodel->test_map, newmodel, (_oval_clone_func) oval_test_clone); |
| 119 | _oval_definition_model_clone |
| 120 | (oldmodel->variable_map, newmodel, (_oval_clone_func) oval_variable_clone); |
| 121 | newmodel->schema = oscap_strdup(oldmodel->schema); |
| 122 | newmodel->vardef_map = NULL; |
| 123 | return newmodel; |
| 124 | } |
| 125 | |
| 126 | void oval_definition_model_free(struct oval_definition_model *model) |
| 127 | { |
| 128 | if (model != NULL) { |
| 129 | oval_string_map_free(model->definition_map, (oscap_destruct_func) oval_definition_free); |
| 130 | oval_string_map_free(model->object_map, (oscap_destruct_func) oval_object_free); |
| 131 | oval_string_map_free(model->state_map, (oscap_destruct_func) oval_state_free); |
| 132 | oval_string_map_free(model->test_map, (oscap_destruct_func) oval_test_free); |
| 133 | oval_string_map_free(model->variable_map, (oscap_destruct_func) oval_variable_free); |
| 134 | if (model->vardef_map != NULL) |
| 135 | oval_string_map_free(model->vardef_map, (oscap_destruct_func) oval_string_map_free0); |
| 136 | if (model->bound_variable_models) |
| 137 | oval_collection_free_items(model->bound_variable_models, |
| 138 | (oscap_destruct_func) oval_variable_model_free); |
| 139 | |
| 140 | if (model->schema != NULL) |
| 141 | free(model->schema); |
| 142 | |
| 143 | oval_generator_free(model->generator); |
| 144 | free(model); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | struct oval_generator *oval_definition_model_get_generator(struct oval_definition_model *model) |
| 149 | { |
| 150 | return model->generator; |
| 151 | } |
| 152 | |
| 153 | void oval_definition_model_set_generator(struct oval_definition_model *model, struct oval_generator *generator) |
| 154 | { |
| 155 | oval_generator_free(model->generator); |
| 156 | model->generator = generator; |
| 157 | } |
| 158 | |
| 159 | const char * oval_definition_model_get_schema(struct oval_definition_model * model) |
| 160 | { |
| 161 | __attribute__nonnull__(model); |
| 162 | |
| 163 | return model->schema; |
| 164 | } |
| 165 | |
| 166 | oval_schema_version_t oval_definition_model_get_core_schema_version(struct oval_definition_model *model) |
| 167 | { |
| 168 | if (model == NULL || model->generator == NULL) { |
| 169 | return OVAL_SCHEMA_VERSION_INVALID; |
| 170 | } |
| 171 | const char *version = oval_generator_get_core_schema_version(model->generator); |
| 172 | return oval_schema_version_from_cstr(version); |
| 173 | } |
| 174 | |
| 175 | oval_schema_version_t oval_definition_model_get_platform_schema_version(struct oval_definition_model *model, const char *platform) |
| 176 | { |
| 177 | if (model == NULL || model->generator == NULL) { |
| 178 | return OVAL_SCHEMA_VERSION_INVALID; |
| 179 | } |
| 180 | const char *version = oval_generator_get_platform_schema_version(model->generator, platform); |
| 181 | return oval_schema_version_from_cstr(version); |
| 182 | } |
| 183 | |
| 184 | void oval_definition_model_add_definition(struct oval_definition_model *model, struct oval_definition *definition) |
| 185 | { |
| 186 | __attribute__nonnull__(model); |
| 187 | char *key = oval_definition_get_id(definition); |
| 188 | oval_string_map_put(model->definition_map, key, (void *)definition); |
| 189 | } |
| 190 | |
| 191 | void oval_definition_model_set_schema(struct oval_definition_model *model, const char *version) |
| 192 | { |
| 193 | __attribute__nonnull__(model); |
| 194 | model->schema = oscap_strdup(version); |
| 195 | } |
| 196 | |
| 197 | void oval_definition_model_add_test(struct oval_definition_model *model, struct oval_test *test) |
| 198 | { |
| 199 | __attribute__nonnull__(model); |
| 200 | char *key = oval_test_get_id(test); |
| 201 | oval_string_map_put(model->test_map, key, (void *)test); |
| 202 | } |
| 203 | |
| 204 | void oval_definition_model_add_object(struct oval_definition_model *model, struct oval_object *object) |
| 205 | { |
| 206 | __attribute__nonnull__(model); |
| 207 | char *key = oval_object_get_id(object); |
| 208 | oval_string_map_put(model->object_map, key, (void *)object); |
| 209 | } |
| 210 | |
| 211 | void oval_definition_model_add_state(struct oval_definition_model *model, struct oval_state *state) |
| 212 | { |
| 213 | __attribute__nonnull__(model); |
| 214 | char *key = oval_state_get_id(state); |
| 215 | oval_string_map_put(model->state_map, key, (void *)state); |
| 216 | } |
| 217 | |
| 218 | void oval_definition_model_add_variable(struct oval_definition_model *model, struct oval_variable *variable) |
| 219 | { |
| 220 | __attribute__nonnull__(model); |
| 221 | char *key = oval_variable_get_id(variable); |
| 222 | oval_string_map_put(model->variable_map, key, (void *)variable); |
| 223 | } |
| 224 | |
| 225 | static inline int _oval_definition_model_merge_source(struct oval_definition_model *model, struct oscap_source *source) |
| 226 | { |
| 227 | /* setup context */ |
| 228 | struct oval_parser_context context; |
| 229 | context.reader = oscap_source_get_xmlTextReader(source); |
| 230 | if (context.reader == NULL) { |
| 231 | return -1; |
| 232 | } |
| 233 | context.definition_model = model; |
| 234 | context.user_data = NULL; |
| 235 | /* jump into oval_definitions */ |
| 236 | while (xmlTextReaderRead(context.reader) == 1 |
| 237 | && xmlTextReaderNodeType(context.reader) != XML_READER_TYPE_ELEMENT) ; |
| 238 | /* start parsing */ |
| 239 | int ret = oval_definition_model_parse(context.reader, &context); |
| 240 | xmlFreeTextReader(context.reader); |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | struct oval_definition_model *oval_definition_model_import_source(struct oscap_source *source) |
| 245 | { |
| 246 | struct oval_definition_model *model = oval_definition_model_new(); |
| 247 | int ret = _oval_definition_model_merge_source(model, source); |
| 248 | if (ret == -1 ) { |
| 249 | oval_definition_model_free(model); |
| 250 | model = NULL; |
| 251 | } |
| 252 | return model; |
| 253 | } |
| 254 | |
| 255 | struct oval_definition *oval_definition_model_get_definition(struct oval_definition_model *model, const char *key) |
| 256 | { |
| 257 | __attribute__nonnull__(model); |
| 258 | |
| 259 | return (struct oval_definition *)oval_string_map_get_value(model->definition_map, key); |
| 260 | } |
| 261 | |
| 262 | struct oval_test *oval_definition_model_get_test(struct oval_definition_model *model, const char *key) |
| 263 | { |
| 264 | __attribute__nonnull__(model); |
| 265 | |
| 266 | return (struct oval_test *)oval_string_map_get_value(model->test_map, key); |
| 267 | } |
| 268 | |
| 269 | struct oval_object *oval_definition_model_get_object(struct oval_definition_model *model, const char *key) |
| 270 | { |
| 271 | __attribute__nonnull__(model); |
| 272 | |
| 273 | return (struct oval_object *)oval_string_map_get_value(model->object_map, key); |
| 274 | } |
| 275 | |
| 276 | struct oval_state *oval_definition_model_get_state(struct oval_definition_model *model, const char *key) |
| 277 | { |
| 278 | __attribute__nonnull__(model); |
| 279 | |
| 280 | return (struct oval_state *)oval_string_map_get_value(model->state_map, key); |
| 281 | } |
| 282 | |
| 283 | struct oval_variable *oval_definition_model_get_variable(struct oval_definition_model *model, const char *key) |
| 284 | { |
| 285 | __attribute__nonnull__(model); |
| 286 | |
| 287 | return (struct oval_variable *)oval_string_map_get_value(model->variable_map, key); |
| 288 | } |
| 289 | |
| 290 | int oval_definition_model_bind_variable_model(struct oval_definition_model *defmodel, |
| 291 | struct oval_variable_model *varmodel) |
| 292 | { |
| 293 | struct oval_string_iterator *evar_id_itr; |
| 294 | |
| 295 | if (!defmodel->bound_variable_models) |
| 296 | defmodel->bound_variable_models = oval_collection_new(); |
| 297 | |
| 298 | oval_collection_add(defmodel->bound_variable_models, varmodel); |
| 299 | |
| 300 | /* todo: keep reference count for each variable model if it can be bound to multiple definition models */ |
| 301 | |
| 302 | evar_id_itr = oval_variable_model_get_variable_ids(varmodel); |
| 303 | while (oval_string_iterator_has_more(evar_id_itr)) { |
| 304 | char *evar_id; |
| 305 | struct oval_variable *var; |
| 306 | |
| 307 | evar_id = oval_string_iterator_next(evar_id_itr); |
| 308 | var = oval_definition_model_get_variable(defmodel, evar_id); |
| 309 | if (!var) |
| 310 | continue; |
| 311 | |
| 312 | oval_variable_bind_ext_var(var, varmodel, evar_id); |
| 313 | } |
| 314 | oval_string_iterator_free(evar_id_itr); |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | struct oval_variable_model_iterator *oval_definition_model_get_variable_models(struct oval_definition_model *model) |
| 320 | { |
| 321 | if (model->bound_variable_models) |
| 322 | return (struct oval_variable_model_iterator *) oval_collection_iterator(model->bound_variable_models); |
| 323 | else |
| 324 | return (struct oval_variable_model_iterator *) oval_collection_iterator_new(); |
| 325 | } |
| 326 | |
| 327 | void oval_definition_model_clear_external_variables(struct oval_definition_model *model) |
| 328 | { |
| 329 | struct oval_variable_iterator *vars_itr; |
| 330 | |
| 331 | vars_itr = oval_definition_model_get_variables(model); |
| 332 | while (oval_variable_iterator_has_more(vars_itr)) { |
| 333 | struct oval_variable *var; |
| 334 | |
| 335 | var = oval_variable_iterator_next(vars_itr); |
| 336 | if (oval_variable_get_type(var) != OVAL_VARIABLE_EXTERNAL) |
| 337 | continue; |
| 338 | |
| 339 | oval_variable_clear_values(var); |
| 340 | } |
| 341 | oval_variable_iterator_free(vars_itr); |
| 342 | } |
| 343 | |
| 344 | struct oval_definition_iterator *oval_definition_model_get_definitions(struct oval_definition_model |
| 345 | *model) |
| 346 | { |
| 347 | __attribute__nonnull__(model); |
| 348 | |
| 349 | struct oval_definition_iterator *iterator = |
| 350 | (struct oval_definition_iterator *)oval_string_map_values(model->definition_map); |
| 351 | |
| 352 | return iterator; |
| 353 | } |
| 354 | |
| 355 | struct oval_string_iterator *oval_definition_model_get_definitions_dependent_on_variable(struct oval_definition_model *model, struct oval_variable *variable) |
| 356 | { |
| 357 | __attribute__nonnull__(model); |
| 358 | __attribute__nonnull__(variable); |
| 359 | |
| 360 | if (model->vardef_map == NULL) |
| 361 | model->vardef_map = oval_definition_model_build_vardef_mapping(model); |
| 362 | |
| 363 | struct oval_string_map *def_list = (struct oval_string_map *) oval_string_map_get_value(model->vardef_map, oval_variable_get_id(variable)); |
| 364 | return (struct oval_string_iterator *) (def_list != NULL ? |
| 365 | oval_string_map_keys(def_list) : oval_collection_iterator_new()); |
| 366 | } |
| 367 | |
| 368 | struct oval_test_iterator *oval_definition_model_get_tests(struct oval_definition_model *model) |
| 369 | { |
| 370 | __attribute__nonnull__(model); |
| 371 | |
| 372 | struct oval_test_iterator *iterator = (struct oval_test_iterator *)oval_string_map_values(model->test_map); |
| 373 | |
| 374 | return iterator; |
| 375 | } |
| 376 | |
| 377 | struct oval_object_iterator *oval_definition_model_get_objects(struct oval_definition_model *model) |
| 378 | { |
| 379 | __attribute__nonnull__(model); |
| 380 | |
| 381 | struct oval_object_iterator *iterator = |
| 382 | (struct oval_object_iterator *)oval_string_map_values(model->object_map); |
| 383 | |
| 384 | return iterator; |
| 385 | } |
| 386 | |
| 387 | struct oval_state_iterator *oval_definition_model_get_states(struct oval_definition_model *model) |
| 388 | { |
| 389 | __attribute__nonnull__(model); |
| 390 | |
| 391 | struct oval_state_iterator *iterator = (struct oval_state_iterator *)oval_string_map_values(model->state_map); |
| 392 | |
| 393 | return iterator; |
| 394 | } |
| 395 | |
| 396 | struct oval_variable_iterator *oval_definition_model_get_variables(struct oval_definition_model |
| 397 | *model) |
| 398 | { |
| 399 | __attribute__nonnull__(model); |
| 400 | |
| 401 | struct oval_variable_iterator *iterator = |
| 402 | (struct oval_variable_iterator *)oval_string_map_values(model->variable_map); |
| 403 | |
| 404 | return iterator; |
| 405 | } |
| 406 | |
| 407 | struct oval_definition *oval_definition_model_get_new_definition(struct oval_definition_model *model, const char *id) |
| 408 | { |
| 409 | struct oval_definition *definition = oval_definition_model_get_definition(model, id); |
| 410 | if (definition == NULL) { |
| 411 | definition = oval_definition_new(model, id); |
| 412 | } |
| 413 | return definition; |
| 414 | } |
| 415 | |
| 416 | struct oval_variable *oval_definition_model_get_new_variable(struct oval_definition_model *model, const char *id, oval_variable_type_t type) |
| 417 | { |
| 418 | struct oval_variable *variable = oval_definition_model_get_variable(model, id); |
| 419 | if (variable == NULL) { |
| 420 | variable = oval_variable_new(model, id, type); |
| 421 | } else if (type != OVAL_VARIABLE_UNKNOWN) { |
| 422 | oval_variable_set_type(variable, type); |
| 423 | } |
| 424 | return variable; |
| 425 | } |
| 426 | |
| 427 | struct oval_state *oval_definition_model_get_new_state(struct oval_definition_model *model, const char *id) |
| 428 | { |
| 429 | struct oval_state *state = oval_definition_model_get_state(model, id); |
| 430 | if (state == NULL) { |
| 431 | state = oval_state_new(model, id); |
| 432 | } |
| 433 | return state; |
| 434 | } |
| 435 | |
| 436 | struct oval_object *oval_definition_model_get_new_object(struct oval_definition_model *model, const char *id) |
| 437 | { |
| 438 | struct oval_object *object = oval_definition_model_get_object(model, id); |
| 439 | if (object == NULL) { |
| 440 | object = oval_object_new(model, id); |
| 441 | } |
| 442 | return object; |
| 443 | } |
| 444 | |
| 445 | struct oval_test *oval_definition_model_get_new_test(struct oval_definition_model *model, const char *id) |
| 446 | { |
| 447 | struct oval_test *test = oval_definition_model_get_test(model, id); |
| 448 | if (test == NULL) { |
| 449 | test = oval_test_new(model, id); |
| 450 | } |
| 451 | return test; |
| 452 | } |
| 453 | |
| 454 | xmlNode *oval_definition_model_to_dom(struct oval_definition_model *definition_model, xmlDocPtr doc, xmlNode * parent) |
| 455 | { |
| 456 | |
| 457 | xmlNodePtr root_node = NULL; |
| 458 | |
| 459 | if (parent) { /* result file */ |
| 460 | root_node = xmlNewTextChild(parent, NULL, BAD_CAST OVAL_ROOT_ELM_DEFINITIONS, NULL); |
| 461 | } else { /* definitions file, we are the root */ |
| 462 | root_node = xmlNewNode(NULL, BAD_CAST OVAL_ROOT_ELM_DEFINITIONS); |
| 463 | xmlDocSetRootElement(doc, root_node); |
| 464 | } |
| 465 | xmlNewNsProp(root_node, lookup_xsi_ns(doc), BAD_CAST "schemaLocation", BAD_CAST definition_model->schema); |
| 466 | |
| 467 | xmlNs *ns_common = xmlNewNs(root_node, OVAL_COMMON_NAMESPACE, BAD_CAST "oval"); |
| 468 | xmlNs *ns_unix = xmlNewNs(root_node, OVAL_DEFINITIONS_UNIX_NS, BAD_CAST "unix-def"); |
| 469 | xmlNs *ns_ind = xmlNewNs(root_node, OVAL_DEFINITIONS_IND_NS, BAD_CAST "ind-def"); |
| 470 | xmlNs *ns_lin = xmlNewNs(root_node, OVAL_DEFINITIONS_LIN_NS, BAD_CAST "lin-def"); |
| 471 | xmlNs *ns_win = xmlNewNs(root_node, OVAL_DEFINITIONS_WIN_NS, BAD_CAST "win-def"); |
| 472 | xmlNs *ns_mac = xmlNewNs(root_node, OVAL_DEFINITIONS_MAC_NS, BAD_CAST "mac-def"); |
| 473 | xmlNs *ns_defntns = xmlNewNs(root_node, OVAL_DEFINITIONS_NAMESPACE, NULL); |
| 474 | |
| 475 | xmlSetNs(root_node, ns_common); |
| 476 | xmlSetNs(root_node, ns_unix); |
| 477 | xmlSetNs(root_node, ns_ind); |
| 478 | xmlSetNs(root_node, ns_lin); |
| 479 | xmlSetNs(root_node, ns_win); |
| 480 | xmlSetNs(root_node, ns_mac); |
| 481 | xmlSetNs(root_node, ns_defntns); |
| 482 | |
| 483 | /* Always report the generator */ |
| 484 | oval_generator_to_dom(definition_model->generator, doc, root_node); |
| 485 | |
| 486 | /* Report definitions */ |
| 487 | struct oval_definition_iterator *definitions = oval_definition_model_get_definitions(definition_model); |
| 488 | if (oval_definition_iterator_has_more(definitions)) { |
| 489 | xmlNode *definitions_node = NULL; |
| 490 | while(oval_definition_iterator_has_more(definitions)) { |
| 491 | struct oval_definition *definition = oval_definition_iterator_next(definitions); |
| 492 | if (definitions_node == NULL) { |
| 493 | definitions_node = xmlNewTextChild(root_node, ns_defntns, BAD_CAST "definitions", NULL); |
| 494 | } |
| 495 | oval_definition_to_dom(definition, doc, definitions_node); |
| 496 | } |
| 497 | } |
| 498 | oval_definition_iterator_free(definitions); |
| 499 | |
| 500 | /* Report tests */ |
| 501 | struct oval_test_iterator *tests = oval_definition_model_get_tests(definition_model); |
| 502 | if (oval_test_iterator_has_more(tests)) { |
| 503 | xmlNode *tests_node = xmlNewTextChild(root_node, ns_defntns, BAD_CAST "tests", NULL); |
| 504 | while (oval_test_iterator_has_more(tests)) { |
| 505 | struct oval_test *test = oval_test_iterator_next(tests); |
| 506 | oval_test_to_dom(test, doc, tests_node); |
| 507 | } |
| 508 | } |
| 509 | oval_test_iterator_free(tests); |
| 510 | |
| 511 | /* Report objects */ |
| 512 | struct oval_object_iterator *objects = oval_definition_model_get_objects(definition_model); |
| 513 | if (oval_object_iterator_has_more(objects)) { |
| 514 | xmlNode *objects_node = xmlNewTextChild(root_node, ns_defntns, BAD_CAST "objects", NULL); |
| 515 | while(oval_object_iterator_has_more(objects)) { |
| 516 | struct oval_object *object = oval_object_iterator_next(objects); |
| 517 | if (oval_object_get_base_obj(object)) |
| 518 | /* Skip internal objects */ |
| 519 | continue; |
| 520 | oval_object_to_dom(object, doc, objects_node); |
| 521 | } |
| 522 | } |
| 523 | oval_object_iterator_free(objects); |
| 524 | |
| 525 | /* Report states */ |
| 526 | struct oval_state_iterator *states = oval_definition_model_get_states(definition_model); |
| 527 | if (oval_state_iterator_has_more(states)) { |
| 528 | xmlNode *states_node = xmlNewTextChild(root_node, ns_defntns, BAD_CAST "states", NULL); |
| 529 | while (oval_state_iterator_has_more(states)) { |
| 530 | struct oval_state *state = oval_state_iterator_next(states); |
| 531 | oval_state_to_dom(state, doc, states_node); |
| 532 | } |
| 533 | } |
| 534 | oval_state_iterator_free(states); |
| 535 | |
| 536 | /* Report variables */ |
| 537 | struct oval_variable_iterator *variables = oval_definition_model_get_variables(definition_model); |
| 538 | if (oval_variable_iterator_has_more(variables)) { |
| 539 | xmlNode *variables_node = xmlNewTextChild(root_node, ns_defntns, BAD_CAST "variables", NULL); |
| 540 | while (oval_variable_iterator_has_more(variables)) { |
| 541 | struct oval_variable *variable = oval_variable_iterator_next(variables); |
| 542 | oval_variable_to_dom(variable, doc, variables_node); |
| 543 | } |
| 544 | } |
| 545 | oval_variable_iterator_free(variables); |
| 546 | |
| 547 | return root_node; |
| 548 | } |
| 549 | |
| 550 | int oval_definition_model_export(struct oval_definition_model *model, const char *file) |
| 551 | { |
| 552 | |
| 553 | __attribute__nonnull__(model); |
| 554 | |
| 555 | LIBXML_TEST_VERSION; |
| 556 | |
| 557 | xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0"); |
| 558 | if (doc == NULL) { |
| 559 | oscap_setxmlerr(xmlGetLastError()); |
| 560 | return -1; |
| 561 | } |
| 562 | |
| 563 | oval_definition_model_to_dom(model, doc, NULL); |
| 564 | return oscap_xml_save_filename_free(file, doc); |
| 565 | } |
| 566 | |
| 567 | static void _fp_set_recurse(struct oval_definition_model *model, struct oval_setobject *set, char *set_id) |
| 568 | { |
| 569 | struct oval_setobject_iterator *subset_itr; |
| 570 | |
| 571 | subset_itr = oval_setobject_get_subsets(set); |
| 572 | while (oval_setobject_iterator_has_more(subset_itr)) { |
| 573 | struct oval_setobject *subset; |
| 574 | |
| 575 | subset = oval_setobject_iterator_next(subset_itr); |
| 576 | if (oval_setobject_get_type(subset) == OVAL_SET_COLLECTIVE) |
| 577 | oval_set_propagate_filters(model, subset, set_id); |
| 578 | else |
| 579 | _fp_set_recurse(model, subset, set_id); |
| 580 | } |
| 581 | oval_setobject_iterator_free(subset_itr); |
| 582 | } |
| 583 | |
| 584 | void oval_definition_model_optimize_by_filter_propagation(struct oval_definition_model *model) |
| 585 | { |
| 586 | struct oval_object_iterator *obj_itr; |
| 587 | struct oval_string_map *processed_obj_map; |
| 588 | |
| 589 | processed_obj_map = oval_string_map_new(); |
| 590 | |
| 591 | obj_itr = oval_definition_model_get_objects(model); |
| 592 | while (oval_object_iterator_has_more(obj_itr)) { |
| 593 | struct oval_object *obj; |
| 594 | char *obj_id; |
| 595 | struct oval_object_content_iterator *cont_itr; |
| 596 | struct oval_object_content *cont; |
| 597 | struct oval_setobject *set; |
| 598 | struct oval_filter_iterator *filter_itr; |
| 599 | |
| 600 | obj = oval_object_iterator_next(obj_itr); |
| 601 | obj_id = oval_object_get_id(obj); |
| 602 | if (oval_string_map_get_value(processed_obj_map, obj_id) != NULL) |
| 603 | continue; |
| 604 | |
| 605 | oval_string_map_put(processed_obj_map, obj_id, obj); |
| 606 | |
| 607 | cont_itr = oval_object_get_object_contents(obj); |
| 608 | if (!oval_object_content_iterator_has_more(cont_itr)) { |
| 609 | oval_object_content_iterator_free(cont_itr); |
| 610 | continue; |
| 611 | } |
| 612 | |
| 613 | cont = oval_object_content_iterator_next(cont_itr); |
| 614 | oval_object_content_iterator_free(cont_itr); |
| 615 | if (oval_object_content_get_type(cont) != OVAL_OBJECTCONTENT_SET) |
| 616 | continue; |
| 617 | |
| 618 | set = oval_object_content_get_setobject(cont); |
| 619 | if (oval_setobject_get_type(set) == OVAL_SET_AGGREGATE) { |
| 620 | _fp_set_recurse(model, set, obj_id); |
| 621 | continue; |
| 622 | } |
| 623 | |
| 624 | filter_itr = oval_setobject_get_filters(set); |
| 625 | if (!oval_filter_iterator_has_more(filter_itr)) { |
| 626 | oval_filter_iterator_free(filter_itr); |
| 627 | continue; |
| 628 | } |
| 629 | oval_filter_iterator_free(filter_itr); |
| 630 | |
| 631 | oval_set_propagate_filters(model, set, obj_id); |
| 632 | } |
| 633 | oval_object_iterator_free(obj_itr); |
| 634 | |
| 635 | oval_string_map_free(processed_obj_map, NULL); |
| 636 | } |