| 1 | /** |
| 2 | * @file oval_set.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 | */ |
| 29 | |
| 30 | #ifdef HAVE_CONFIG_H |
| 31 | #include <config.h> |
| 32 | #endif |
| 33 | |
| 34 | #include <stdlib.h> |
| 35 | #include <stdio.h> |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include "oval_definitions_impl.h" |
| 39 | #include "adt/oval_collection_impl.h" |
| 40 | #include "oval_agent_api_impl.h" |
| 41 | #include "common/util.h" |
| 42 | #include "common/debug_priv.h" |
| 43 | #include "common/elements.h" |
| 44 | |
| 45 | typedef struct oval_setobject { |
| 46 | struct oval_definition_model *model; |
| 47 | oval_setobject_type_t type; |
| 48 | oval_setobject_operation_t operation; |
| 49 | void *extension; |
| 50 | } oval_set_t; |
| 51 | |
| 52 | typedef struct oval_set_AGGREGATE { |
| 53 | struct oval_collection *subsets; /*type==OVAL_SET_AGGREGATE; */ |
| 54 | } oval_set_AGGREGATE_t; |
| 55 | |
| 56 | typedef struct oval_set_COLLECTIVE { |
| 57 | struct oval_collection *objects; //type==OVAL_SET_COLLECTIVE; |
| 58 | struct oval_collection *filters; //type==OVAL_SET_COLLECTIVE; |
| 59 | } oval_set_COLLECTIVE_t; |
| 60 | |
| 61 | bool oval_setobject_iterator_has_more(struct oval_setobject_iterator *oc_set) |
| 62 | { |
| 63 | return oval_collection_iterator_has_more((struct oval_iterator *) |
| 64 | oc_set); |
| 65 | } |
| 66 | |
| 67 | struct oval_setobject *oval_setobject_iterator_next(struct oval_setobject_iterator *oc_set) |
| 68 | { |
| 69 | return (struct oval_setobject *) |
| 70 | oval_collection_iterator_next((struct oval_iterator *)oc_set); |
| 71 | } |
| 72 | |
| 73 | void oval_setobject_iterator_free(struct oval_setobject_iterator *oc_set) |
| 74 | { |
| 75 | oval_collection_iterator_free((struct oval_iterator *)oc_set); |
| 76 | } |
| 77 | |
| 78 | oval_setobject_type_t oval_setobject_get_type(struct oval_setobject *set) |
| 79 | { |
| 80 | __attribute__nonnull__(set); |
| 81 | |
| 82 | return (set)->type; |
| 83 | } |
| 84 | |
| 85 | oval_setobject_operation_t oval_setobject_get_operation(struct oval_setobject * set) |
| 86 | { |
| 87 | __attribute__nonnull__(set); |
| 88 | |
| 89 | return ((struct oval_setobject *)set)->operation; |
| 90 | } |
| 91 | |
| 92 | struct oval_setobject_iterator *oval_setobject_get_subsets(struct oval_setobject *set) |
| 93 | { |
| 94 | __attribute__nonnull__(set); |
| 95 | |
| 96 | struct oval_setobject_iterator *subsets = NULL; |
| 97 | if (set->type == OVAL_SET_AGGREGATE) { |
| 98 | struct oval_set_AGGREGATE *aggregate = (struct oval_set_AGGREGATE *)set->extension; |
| 99 | subsets = (struct oval_setobject_iterator *) |
| 100 | oval_collection_iterator(aggregate->subsets); |
| 101 | } |
| 102 | return subsets; |
| 103 | } |
| 104 | |
| 105 | struct oval_object_iterator *oval_setobject_get_objects(struct oval_setobject *set) |
| 106 | { |
| 107 | __attribute__nonnull__(set); |
| 108 | |
| 109 | /* type == OVAL_SET_COLLECTIVE; */ |
| 110 | struct oval_object_iterator *objects = NULL; |
| 111 | if (set->type == OVAL_SET_COLLECTIVE) { |
| 112 | struct oval_set_COLLECTIVE *collective = (struct oval_set_COLLECTIVE *)set->extension; |
| 113 | objects = (struct oval_object_iterator *) |
| 114 | oval_collection_iterator(collective->objects); |
| 115 | } |
| 116 | return objects; |
| 117 | } |
| 118 | |
| 119 | struct oval_filter_iterator *oval_setobject_get_filters(struct oval_setobject *set) |
| 120 | { |
| 121 | __attribute__nonnull__(set); |
| 122 | |
| 123 | /* type == OVAL_SET_COLLECTIVE; */ |
| 124 | struct oval_filter_iterator *filters = NULL; |
| 125 | if (set->type == OVAL_SET_COLLECTIVE) { |
| 126 | struct oval_set_COLLECTIVE *collective = (struct oval_set_COLLECTIVE *)set->extension; |
| 127 | filters = (struct oval_filter_iterator *) |
| 128 | oval_collection_iterator(collective->filters); |
| 129 | } |
| 130 | return filters; |
| 131 | } |
| 132 | |
| 133 | struct oval_setobject *oval_setobject_new(struct oval_definition_model *model) |
| 134 | { |
| 135 | oval_set_t *set = (oval_set_t *) malloc(sizeof(oval_set_t)); |
| 136 | if (set == NULL) |
| 137 | return NULL; |
| 138 | |
| 139 | set->operation = OVAL_SET_OPERATION_UNKNOWN; |
| 140 | set->type = OVAL_SET_UNKNOWN; |
| 141 | set->extension = NULL; |
| 142 | set->model = model; |
| 143 | return set; |
| 144 | } |
| 145 | |
| 146 | struct oval_setobject *oval_setobject_clone |
| 147 | (struct oval_definition_model *new_model, struct oval_setobject *old_setobject) { |
| 148 | struct oval_setobject *new_setobject = oval_setobject_new(new_model); |
| 149 | oval_setobject_type_t type = oval_setobject_get_type(old_setobject); |
| 150 | oval_setobject_set_type(new_setobject, type); |
| 151 | oval_setobject_operation_t operation = oval_setobject_get_operation(old_setobject); |
| 152 | oval_setobject_set_operation(new_setobject, operation); |
| 153 | switch (type) { |
| 154 | case OVAL_SET_COLLECTIVE:{ |
| 155 | struct oval_filter_iterator *filters = oval_setobject_get_filters(old_setobject); |
| 156 | while (oval_filter_iterator_has_more(filters)) { |
| 157 | struct oval_filter *filter = oval_filter_iterator_next(filters); |
| 158 | oval_setobject_add_filter(new_setobject, oval_filter_clone(new_model, filter)); |
| 159 | } |
| 160 | oval_filter_iterator_free(filters); |
| 161 | struct oval_object_iterator *objects = oval_setobject_get_objects(old_setobject); |
| 162 | while (oval_object_iterator_has_more(objects)) { |
| 163 | struct oval_object *object = oval_object_iterator_next(objects); |
| 164 | oval_setobject_add_object(new_setobject, oval_object_clone(new_model, object)); |
| 165 | } |
| 166 | oval_object_iterator_free(objects); |
| 167 | |
| 168 | } break; |
| 169 | case OVAL_SET_AGGREGATE:{ |
| 170 | struct oval_setobject_iterator *subsets = oval_setobject_get_subsets(old_setobject); |
| 171 | while (oval_setobject_iterator_has_more(subsets)) { |
| 172 | struct oval_setobject *subset = oval_setobject_iterator_next(subsets); |
| 173 | oval_setobject_add_subset(new_setobject, oval_setobject_clone(new_model, subset)); |
| 174 | } |
| 175 | oval_setobject_iterator_free(subsets); |
| 176 | } break; |
| 177 | default: |
| 178 | /*NOOP*/; |
| 179 | } |
| 180 | return new_setobject; |
| 181 | } |
| 182 | |
| 183 | void oval_setobject_free(struct oval_setobject *set) |
| 184 | { |
| 185 | __attribute__nonnull__(set); |
| 186 | |
| 187 | switch (set->type) { |
| 188 | case OVAL_SET_AGGREGATE:{ |
| 189 | oval_set_AGGREGATE_t *aggregate = (oval_set_AGGREGATE_t *) set->extension; |
| 190 | oval_collection_free_items(aggregate->subsets, (oscap_destruct_func) oval_setobject_free); |
| 191 | aggregate->subsets = NULL; |
| 192 | free(set->extension); |
| 193 | set->extension = NULL; |
| 194 | } |
| 195 | break; |
| 196 | case OVAL_SET_COLLECTIVE:{ |
| 197 | oval_set_COLLECTIVE_t *collective = (oval_set_COLLECTIVE_t *) set->extension; |
| 198 | oval_collection_free_items(collective->filters, (oscap_destruct_func) oval_filter_free); |
| 199 | //Objects (and states in filters) are shared and should not be deleted here. |
| 200 | oval_collection_free_items(collective->objects, NULL); |
| 201 | collective->filters = NULL; |
| 202 | collective->objects = NULL; |
| 203 | free(set->extension); |
| 204 | set->extension = NULL; |
| 205 | } |
| 206 | break; |
| 207 | case OVAL_SET_UNKNOWN: |
| 208 | break; |
| 209 | } |
| 210 | free(set); |
| 211 | } |
| 212 | |
| 213 | void oval_setobject_set_type(struct oval_setobject *set, oval_setobject_type_t type) |
| 214 | { |
| 215 | __attribute__nonnull__(set); |
| 216 | |
| 217 | set->type = type; |
| 218 | switch (type) { |
| 219 | case OVAL_SET_AGGREGATE:{ |
| 220 | oval_set_AGGREGATE_t *aggregate = |
| 221 | (oval_set_AGGREGATE_t *) (set->extension = |
| 222 | malloc(sizeof(oval_set_AGGREGATE_t))); |
| 223 | aggregate->subsets = oval_collection_new(); |
| 224 | } |
| 225 | break; |
| 226 | case OVAL_SET_COLLECTIVE:{ |
| 227 | oval_set_COLLECTIVE_t *collective = |
| 228 | (oval_set_COLLECTIVE_t *) (set->extension = |
| 229 | malloc(sizeof(oval_set_COLLECTIVE_t))); |
| 230 | collective->filters = oval_collection_new(); |
| 231 | collective->objects = oval_collection_new(); |
| 232 | } |
| 233 | break; |
| 234 | case OVAL_SET_UNKNOWN: |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | void oval_setobject_set_operation(struct oval_setobject *set, oval_setobject_operation_t operation) |
| 240 | { |
| 241 | __attribute__nonnull__(set); |
| 242 | set->operation = operation; |
| 243 | } |
| 244 | |
| 245 | void oval_setobject_add_subset(struct oval_setobject *set, struct oval_setobject *subset) |
| 246 | { |
| 247 | __attribute__nonnull__(set); |
| 248 | oval_set_AGGREGATE_t *aggregate = (oval_set_AGGREGATE_t *) set->extension; |
| 249 | assert(aggregate != NULL); |
| 250 | oval_collection_add(aggregate->subsets, (void *)subset); |
| 251 | } |
| 252 | |
| 253 | void oval_setobject_add_object(struct oval_setobject *set, struct oval_object *object) |
| 254 | { |
| 255 | __attribute__nonnull__(set); |
| 256 | oval_set_COLLECTIVE_t *collective = (oval_set_COLLECTIVE_t *) set->extension; |
| 257 | assert(collective != NULL); |
| 258 | oval_collection_add(collective->objects, (void *)object); |
| 259 | } |
| 260 | |
| 261 | void oval_setobject_add_filter(struct oval_setobject *set, struct oval_filter *filter) |
| 262 | { |
| 263 | __attribute__nonnull__(set); |
| 264 | oval_set_COLLECTIVE_t *collective = (oval_set_COLLECTIVE_t *) set->extension; |
| 265 | assert(collective != NULL); |
| 266 | oval_collection_add(collective->filters, (void *)filter); |
| 267 | } |
| 268 | |
| 269 | //typedef int (*oval_xml_tag_parser)(xmlTextReaderPtr, struct oval_parser_context*, void*); |
| 270 | static void oval_set_consume(struct oval_setobject *subset, void *set) |
| 271 | { |
| 272 | oval_setobject_add_subset(set, subset); |
| 273 | } |
| 274 | |
| 275 | struct oval_set_context { |
| 276 | struct oval_parser_context *context; |
| 277 | struct oval_setobject *set; |
| 278 | }; |
| 279 | static void oval_consume_object_ref(char *objref, void *user) |
| 280 | { |
| 281 | |
| 282 | __attribute__nonnull__(user); |
| 283 | |
| 284 | struct oval_set_context *ctx = user; |
| 285 | struct oval_definition_model *model = ctx->context->definition_model; |
| 286 | struct oval_object *object = oval_definition_model_get_new_object(model, objref); |
| 287 | oval_setobject_add_object(ctx->set, object); |
| 288 | } |
| 289 | |
| 290 | static void oval_set_consume_filter(struct oval_filter *filter, void *set) |
| 291 | { |
| 292 | oval_setobject_add_filter(set, filter); |
| 293 | } |
| 294 | |
| 295 | static int _oval_set_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *user) |
| 296 | { |
| 297 | __attribute__nonnull__(user); |
| 298 | |
| 299 | struct oval_setobject *set = (struct oval_setobject *)user; |
| 300 | char *tagname = (char *)xmlTextReaderLocalName(reader); |
| 301 | xmlChar *namespace = xmlTextReaderNamespaceUri(reader); |
| 302 | struct oval_set_context ctx = {.context = context,.set = set }; |
| 303 | |
| 304 | int return_code = 0; |
| 305 | |
| 306 | if (strcmp(tagname, "set") == 0) { |
| 307 | if (set->type == OVAL_SET_UNKNOWN) { |
| 308 | oval_setobject_set_type(set, OVAL_SET_AGGREGATE); |
| 309 | } |
| 310 | return_code = oval_set_parse_tag(reader, context, &oval_set_consume, set); |
| 311 | } else { |
| 312 | if (set->type == OVAL_SET_UNKNOWN) { |
| 313 | oval_setobject_set_type(set, OVAL_SET_COLLECTIVE); |
| 314 | } |
| 315 | if (strcmp(tagname, "object_reference") == 0) { |
| 316 | return_code = oscap_parser_text_value(reader, &oval_consume_object_ref, &ctx); |
| 317 | } else if (strcmp(tagname, "filter") == 0) { |
| 318 | return_code = oval_filter_parse_tag(reader, context, &oval_set_consume_filter, set); |
| 319 | } else { |
| 320 | dW("Unknown tag: <%s>, line: %d.", tagname, |
| 321 | xmlTextReaderGetParserLineNumber(reader)); |
| 322 | return_code = oval_parser_skip_tag(reader, context); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | if (return_code != 0) { |
| 327 | dW("Parsing of <%s> terminated by an error at line %d.", tagname, xmlTextReaderGetParserLineNumber(reader)); |
| 328 | } |
| 329 | |
| 330 | free(tagname); |
| 331 | free(namespace); |
| 332 | return return_code; |
| 333 | } |
| 334 | |
| 335 | //typedef void (*oval_set_consumer)(struct oval_set*,void*); |
| 336 | int oval_set_parse_tag(xmlTextReaderPtr reader, |
| 337 | struct oval_parser_context *context, oval_set_consumer consumer, void *user) |
| 338 | { |
| 339 | __attribute__nonnull__(context); |
| 340 | |
| 341 | xmlChar *tagname = xmlTextReaderLocalName(reader); |
| 342 | xmlChar *namespace = xmlTextReaderNamespaceUri(reader); |
| 343 | struct oval_setobject *set = oval_setobject_new(context->definition_model); |
| 344 | |
| 345 | oval_setobject_operation_t operation = oval_set_operation_parse(reader, "set_operator", |
| 346 | OVAL_SET_OPERATION_UNION); |
| 347 | oval_setobject_set_operation(set, operation); |
| 348 | |
| 349 | (*consumer) (set, user); |
| 350 | |
| 351 | int return_code = oval_parser_parse_tag(reader, context, &_oval_set_parse_tag, set); |
| 352 | |
| 353 | free(tagname); |
| 354 | free(namespace); |
| 355 | return return_code; |
| 356 | } |
| 357 | |
| 358 | xmlNode *oval_set_to_dom(struct oval_setobject *set, xmlDoc * doc, xmlNode * parent) { |
| 359 | xmlNs *ns_definitions = xmlSearchNsByHref(doc, parent, OVAL_DEFINITIONS_NAMESPACE); |
| 360 | xmlNode *set_node = xmlNewTextChild(parent, ns_definitions, BAD_CAST "set", NULL); |
| 361 | |
| 362 | oval_setobject_operation_t operation = oval_setobject_get_operation(set); |
| 363 | if (operation != OVAL_SET_OPERATION_UNION) |
| 364 | xmlNewProp(set_node, BAD_CAST "set_operator", BAD_CAST oval_set_operation_get_text(operation)); |
| 365 | |
| 366 | switch (oval_setobject_get_type(set)) { |
| 367 | case OVAL_SET_AGGREGATE:{ |
| 368 | struct oval_setobject_iterator *subsets = oval_setobject_get_subsets(set); |
| 369 | while (oval_setobject_iterator_has_more(subsets)) { |
| 370 | struct oval_setobject *subset = oval_setobject_iterator_next(subsets); |
| 371 | oval_set_to_dom(subset, doc, set_node); |
| 372 | } |
| 373 | oval_setobject_iterator_free(subsets); |
| 374 | } break; |
| 375 | case OVAL_SET_COLLECTIVE:{ |
| 376 | struct oval_object_iterator *objects = oval_setobject_get_objects(set); |
| 377 | while (oval_object_iterator_has_more(objects)) { |
| 378 | struct oval_object *object = oval_object_iterator_next(objects); |
| 379 | if (oval_object_get_base_obj(object)) |
| 380 | /* Skip internal objects */ |
| 381 | object = oval_object_get_base_obj(object); |
| 382 | char *id = oval_object_get_id(object); |
| 383 | xmlNewTextChild(set_node, ns_definitions, BAD_CAST "object_reference", BAD_CAST id); |
| 384 | } |
| 385 | oval_object_iterator_free(objects); |
| 386 | |
| 387 | struct oval_filter_iterator *filters = oval_setobject_get_filters(set); |
| 388 | while (oval_filter_iterator_has_more(filters)) { |
| 389 | struct oval_filter *filter; |
| 390 | |
| 391 | filter = oval_filter_iterator_next(filters); |
| 392 | oval_filter_to_dom(filter, doc, set_node); |
| 393 | } |
| 394 | oval_filter_iterator_free(filters); |
| 395 | } break; |
| 396 | default: |
| 397 | break; |
| 398 | } |
| 399 | |
| 400 | return set_node; |
| 401 | } |
| 402 | |
| 403 | void oval_set_propagate_filters(struct oval_definition_model *model, struct oval_setobject *set, char *set_id) |
| 404 | { |
| 405 | struct oval_object_iterator *obj_itr; |
| 406 | struct oval_collection *new_objects; |
| 407 | struct oval_set_COLLECTIVE *ext_col; |
| 408 | |
| 409 | new_objects = oval_collection_new(); |
| 410 | |
| 411 | obj_itr = oval_setobject_get_objects(set); |
| 412 | while (oval_object_iterator_has_more(obj_itr)) { |
| 413 | struct oval_object *obj, *new_obj; |
| 414 | struct oval_object_content_iterator *cont_itr; |
| 415 | struct oval_object_content *cont; |
| 416 | struct oval_filter_iterator *filter_itr; |
| 417 | |
| 418 | obj = oval_object_iterator_next(obj_itr); |
| 419 | cont_itr = oval_object_get_object_contents(obj); |
| 420 | if (!oval_object_content_iterator_has_more(cont_itr)) { |
| 421 | oval_object_content_iterator_free(cont_itr); |
| 422 | oval_collection_add(new_objects, obj); |
| 423 | continue; |
| 424 | } |
| 425 | |
| 426 | cont = oval_object_content_iterator_next(cont_itr); |
| 427 | oval_object_content_iterator_free(cont_itr); |
| 428 | if (oval_object_content_get_type(cont) == OVAL_OBJECTCONTENT_SET) { |
| 429 | oval_collection_add(new_objects, obj); |
| 430 | continue; |
| 431 | } |
| 432 | |
| 433 | new_obj = oval_object_create_internal(obj, set_id); |
| 434 | |
| 435 | filter_itr = oval_setobject_get_filters(set); |
| 436 | while (oval_filter_iterator_has_more(filter_itr)) { |
| 437 | struct oval_filter *filter, *new_filter; |
| 438 | struct oval_state *ste; |
| 439 | oval_filter_action_t act; |
| 440 | struct oval_object_content *content; |
| 441 | |
| 442 | filter = oval_filter_iterator_next(filter_itr); |
| 443 | ste = oval_filter_get_state(filter); |
| 444 | act = oval_filter_get_filter_action(filter); |
| 445 | new_filter = oval_filter_new(model); |
| 446 | oval_filter_set_state(new_filter, ste); |
| 447 | oval_filter_set_filter_action(new_filter, act); |
| 448 | content = oval_object_content_new(model, OVAL_OBJECTCONTENT_FILTER); |
| 449 | oval_object_content_set_filter(content, new_filter); |
| 450 | oval_object_add_object_content(new_obj, content); |
| 451 | } |
| 452 | oval_filter_iterator_free(filter_itr); |
| 453 | |
| 454 | oval_collection_add(new_objects, new_obj); |
| 455 | } |
| 456 | oval_object_iterator_free(obj_itr); |
| 457 | |
| 458 | ext_col = (struct oval_set_COLLECTIVE *) set->extension; |
| 459 | oval_collection_free_items(ext_col->objects, NULL); |
| 460 | ext_col->objects = new_objects; |
| 461 | } |