| 1 | /** |
| 2 | * @file oval_entity.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 | */ |
| 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 <libxml/tree.h> |
| 39 | |
| 40 | #include "oval_definitions_impl.h" |
| 41 | #include "adt/oval_collection_impl.h" |
| 42 | #include "oval_agent_api_impl.h" |
| 43 | #include "oval_parser_impl.h" |
| 44 | |
| 45 | #include "common/util.h" |
| 46 | #include "common/debug_priv.h" |
| 47 | #include "common/elements.h" |
| 48 | #include "common/_error.h" |
| 49 | |
| 50 | /***************************************************************************/ |
| 51 | /* Variable definitions |
| 52 | * */ |
| 53 | |
| 54 | struct oval_entity { |
| 55 | struct oval_definition_model *model; |
| 56 | oval_entity_type_t type; |
| 57 | oval_datatype_t datatype; |
| 58 | oval_operation_t operation; |
| 59 | int mask; |
| 60 | oval_entity_varref_type_t varref_type; |
| 61 | char *name; |
| 62 | struct oval_variable *variable; |
| 63 | struct oval_value *value; |
| 64 | bool xsi_nil; ///< @xsi:nil boolean attribute |
| 65 | }; |
| 66 | |
| 67 | struct oval_consume_varref_context { |
| 68 | struct oval_definition_model *model; |
| 69 | struct oval_variable **variable; |
| 70 | struct oval_value **value; |
| 71 | }; |
| 72 | |
| 73 | /* End of variable definitions |
| 74 | * */ |
| 75 | /***************************************************************************/ |
| 76 | static inline bool oval_entity_get_xsi_nil(const struct oval_entity *entity); |
| 77 | static inline void oval_entity_set_xsi_nil(struct oval_entity *entity, bool xsi_nil); |
| 78 | |
| 79 | |
| 80 | bool oval_entity_iterator_has_more(struct oval_entity_iterator *oc_entity) |
| 81 | { |
| 82 | return oval_collection_iterator_has_more((struct oval_iterator *) |
| 83 | oc_entity); |
| 84 | } |
| 85 | |
| 86 | struct oval_entity *oval_entity_iterator_next(struct oval_entity_iterator |
| 87 | *oc_entity) |
| 88 | { |
| 89 | return (struct oval_entity *) |
| 90 | oval_collection_iterator_next((struct oval_iterator *)oc_entity); |
| 91 | } |
| 92 | |
| 93 | void oval_entity_iterator_free(struct oval_entity_iterator |
| 94 | *oc_entity) |
| 95 | { |
| 96 | oval_collection_iterator_free((struct oval_iterator *)oc_entity); |
| 97 | } |
| 98 | |
| 99 | char *oval_entity_get_name(struct oval_entity *entity) |
| 100 | { |
| 101 | __attribute__nonnull__(entity); |
| 102 | |
| 103 | return entity->name; |
| 104 | } |
| 105 | |
| 106 | oval_entity_type_t oval_entity_get_type(struct oval_entity * entity) |
| 107 | { |
| 108 | __attribute__nonnull__(entity); |
| 109 | |
| 110 | return entity->type; |
| 111 | } |
| 112 | |
| 113 | oval_datatype_t oval_entity_get_datatype(struct oval_entity * entity) |
| 114 | { |
| 115 | __attribute__nonnull__(entity); |
| 116 | |
| 117 | return entity->datatype; |
| 118 | } |
| 119 | |
| 120 | oval_operation_t oval_entity_get_operation(struct oval_entity * entity) |
| 121 | { |
| 122 | __attribute__nonnull__(entity); |
| 123 | |
| 124 | return entity->operation; |
| 125 | } |
| 126 | |
| 127 | int oval_entity_get_mask(struct oval_entity *entity) |
| 128 | { |
| 129 | __attribute__nonnull__(entity); |
| 130 | |
| 131 | return entity->mask; |
| 132 | } |
| 133 | |
| 134 | static inline bool oval_entity_get_xsi_nil(const struct oval_entity *entity) |
| 135 | { |
| 136 | __attribute__nonnull__(entity); |
| 137 | return entity->xsi_nil; |
| 138 | } |
| 139 | |
| 140 | oval_entity_varref_type_t oval_entity_get_varref_type(struct oval_entity * entity) |
| 141 | { |
| 142 | __attribute__nonnull__(entity); |
| 143 | |
| 144 | return entity->varref_type; |
| 145 | } |
| 146 | |
| 147 | struct oval_variable *oval_entity_get_variable(struct oval_entity *entity) |
| 148 | { |
| 149 | __attribute__nonnull__(entity); |
| 150 | |
| 151 | return entity->variable; |
| 152 | } |
| 153 | |
| 154 | struct oval_value *oval_entity_get_value(struct oval_entity *entity) |
| 155 | { |
| 156 | __attribute__nonnull__(entity); |
| 157 | |
| 158 | return entity->value; |
| 159 | } |
| 160 | |
| 161 | struct oval_entity *oval_entity_new(struct oval_definition_model *model) |
| 162 | { |
| 163 | struct oval_entity *entity = (struct oval_entity *)malloc(sizeof(struct oval_entity)); |
| 164 | if (entity == NULL) |
| 165 | return NULL; |
| 166 | |
| 167 | entity->datatype = OVAL_DATATYPE_UNKNOWN; |
| 168 | entity->mask = 0; |
| 169 | entity->xsi_nil = 0; |
| 170 | entity->operation = OVAL_OPERATION_UNKNOWN; |
| 171 | entity->type = OVAL_ENTITY_TYPE_UNKNOWN; |
| 172 | entity->name = NULL; |
| 173 | entity->value = NULL; |
| 174 | entity->variable = NULL; |
| 175 | entity->model = model; |
| 176 | return entity; |
| 177 | } |
| 178 | |
| 179 | struct oval_entity *oval_entity_clone(struct oval_definition_model *new_model, struct oval_entity *old_entity) { |
| 180 | struct oval_entity *new_entity = oval_entity_new(new_model); |
| 181 | oval_datatype_t datatype = oval_entity_get_datatype(old_entity); |
| 182 | oval_entity_set_datatype(new_entity, datatype); |
| 183 | int mask = oval_entity_get_mask(old_entity); |
| 184 | oval_entity_set_mask(new_entity, mask); |
| 185 | bool xsi_nil = oval_entity_get_xsi_nil(old_entity); |
| 186 | oval_entity_set_xsi_nil(new_entity, xsi_nil); |
| 187 | char *name = oval_entity_get_name(old_entity); |
| 188 | oval_entity_set_name(new_entity, name); |
| 189 | oval_operation_t operation = oval_entity_get_operation(old_entity); |
| 190 | oval_entity_set_operation(new_entity, operation); |
| 191 | oval_entity_type_t type = oval_entity_get_type(old_entity); |
| 192 | oval_entity_set_type(new_entity, type); |
| 193 | struct oval_value *value = oval_entity_get_value(old_entity); |
| 194 | if (value) { |
| 195 | oval_entity_set_value(new_entity, oval_value_clone(value)); |
| 196 | } |
| 197 | struct oval_variable *old_variable = oval_entity_get_variable(old_entity); |
| 198 | if (old_variable) { |
| 199 | oval_entity_set_variable(new_entity, oval_variable_clone(new_model, old_variable)); |
| 200 | } |
| 201 | oval_entity_varref_type_t reftype = oval_entity_get_varref_type(old_entity); |
| 202 | oval_entity_set_varref_type(new_entity, reftype); |
| 203 | return new_entity; |
| 204 | } |
| 205 | |
| 206 | void oval_entity_free(struct oval_entity *entity) |
| 207 | { |
| 208 | __attribute__nonnull__(entity); |
| 209 | |
| 210 | if (entity->value != NULL) |
| 211 | oval_value_free(entity->value); |
| 212 | if (entity->name != NULL) |
| 213 | free(entity->name); |
| 214 | |
| 215 | entity->name = NULL; |
| 216 | entity->value = NULL; |
| 217 | entity->variable = NULL; |
| 218 | free(entity); |
| 219 | } |
| 220 | |
| 221 | void oval_entity_set_type(struct oval_entity *entity, oval_entity_type_t type) |
| 222 | { |
| 223 | __attribute__nonnull__(entity); |
| 224 | entity->type = type; |
| 225 | } |
| 226 | |
| 227 | void oval_entity_set_datatype(struct oval_entity *entity, oval_datatype_t datatype) |
| 228 | { |
| 229 | __attribute__nonnull__(entity); |
| 230 | entity->datatype = datatype; |
| 231 | } |
| 232 | |
| 233 | void oval_entity_set_operation(struct oval_entity *entity, oval_operation_t operation) |
| 234 | { |
| 235 | __attribute__nonnull__(entity); |
| 236 | entity->operation = operation; |
| 237 | } |
| 238 | |
| 239 | void oval_entity_set_mask(struct oval_entity *entity, int mask) |
| 240 | { |
| 241 | __attribute__nonnull__(entity); |
| 242 | entity->mask = mask; |
| 243 | } |
| 244 | |
| 245 | static inline void oval_entity_set_xsi_nil(struct oval_entity *entity, bool xsi_nil) |
| 246 | { |
| 247 | __attribute__nonnull__(entity); |
| 248 | entity->xsi_nil = xsi_nil; |
| 249 | } |
| 250 | |
| 251 | void oval_entity_set_varref_type(struct oval_entity *entity, oval_entity_varref_type_t type) |
| 252 | { |
| 253 | __attribute__nonnull__(entity); |
| 254 | entity->varref_type = type; |
| 255 | } |
| 256 | |
| 257 | void oval_entity_set_variable(struct oval_entity *entity, struct oval_variable *variable) |
| 258 | { |
| 259 | __attribute__nonnull__(entity); |
| 260 | entity->variable = variable; |
| 261 | } |
| 262 | |
| 263 | void oval_entity_set_value(struct oval_entity *entity, struct oval_value *value) |
| 264 | { |
| 265 | __attribute__nonnull__(entity); |
| 266 | entity->value = value; |
| 267 | } |
| 268 | |
| 269 | void oval_entity_set_name(struct oval_entity *entity, char *name) |
| 270 | { |
| 271 | __attribute__nonnull__(entity); |
| 272 | if (entity->name != NULL) |
| 273 | free(entity->name); |
| 274 | entity->name = (name == NULL) ? NULL : oscap_strdup(name); |
| 275 | } |
| 276 | |
| 277 | static void oval_consume_varref(char *varref, void *user) |
| 278 | { |
| 279 | |
| 280 | __attribute__nonnull__(user); |
| 281 | |
| 282 | struct oval_consume_varref_context *ctx = user; |
| 283 | *(ctx->variable) = oval_definition_model_get_new_variable((struct oval_definition_model *)ctx->model, varref, OVAL_VARIABLE_UNKNOWN); |
| 284 | *(ctx->value) = oval_value_new(OVAL_DATATYPE_STRING, varref); |
| 285 | } |
| 286 | |
| 287 | static void oval_consume_value(struct oval_value *use_value, void *value) |
| 288 | { |
| 289 | |
| 290 | *(struct oval_value **)value = use_value; |
| 291 | } |
| 292 | |
| 293 | //typedef void (*oval_entity_consumer)(struct oval_entity_node*, void*); |
| 294 | int oval_entity_parse_tag(xmlTextReaderPtr reader, |
| 295 | struct oval_parser_context *context, oscap_consumer_func consumer, void *user) |
| 296 | { |
| 297 | __attribute__nonnull__(context); |
| 298 | |
| 299 | struct oval_entity *entity = oval_entity_new(context->definition_model); |
| 300 | int return_code = 0; |
| 301 | oval_datatype_t datatype = oval_datatype_parse(reader, "datatype", OVAL_DATATYPE_STRING); |
| 302 | oval_operation_t operation = oval_operation_parse(reader, "operation", OVAL_OPERATION_EQUALS); |
| 303 | int mask = oval_parser_boolean_attribute(reader, "mask", 0); |
| 304 | |
| 305 | char *nil_attr = (char *) xmlTextReaderGetAttributeNs(reader, BAD_CAST "nil", OSCAP_XMLNS_XSI); |
| 306 | int xsi_nil = oscap_streq(nil_attr, "true") != 0; |
| 307 | xmlFree(nil_attr); |
| 308 | |
| 309 | oval_entity_type_t type = OVAL_ENTITY_TYPE_UNKNOWN; |
| 310 | //The value of the type field vs. the complexity of extracting type is arguable |
| 311 | char *varref = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "var_ref"); |
| 312 | struct oval_value *value = NULL; |
| 313 | struct oval_variable *variable = NULL; |
| 314 | char *name = (char *)xmlTextReaderLocalName(reader); |
| 315 | oval_entity_varref_type_t varref_type; |
| 316 | if (strcmp(name, "var_ref") == 0) { //special case for <var_ref> |
| 317 | if (varref == NULL) { |
| 318 | struct oval_definition_model *model = context->definition_model; |
| 319 | varref_type = OVAL_ENTITY_VARREF_ELEMENT; |
| 320 | struct oval_consume_varref_context ctx = {.model = model, .variable = &variable, .value = &value}; |
| 321 | return_code = oscap_parser_text_value(reader, &oval_consume_varref, &ctx); |
| 322 | } else { |
| 323 | varref_type = OVAL_ENTITY_VARREF_ATTRIBUTE; |
| 324 | struct oval_definition_model *model = context->definition_model; |
| 325 | oval_schema_version_t version = oval_definition_model_get_core_schema_version(model); |
| 326 | if (oval_schema_version_cmp(version, OVAL_SCHEMA_VERSION(5.6)) > 0) { |
| 327 | oscap_seterr(OSCAP_EFAMILY_OVAL, "The var_ref attribute for the var_ref entity " |
| 328 | "of a variable_object is prohibited since OVAL 5.6. Use plain " |
| 329 | "var_ref instead."); |
| 330 | } |
| 331 | variable = oval_definition_model_get_variable(model, varref); |
| 332 | if (variable == NULL) { |
| 333 | oscap_seterr(OSCAP_EFAMILY_OVAL, |
| 334 | "Could not found variable '%s' referenced by var_ref element.", varref); |
| 335 | return_code = -1; |
| 336 | } else { |
| 337 | free(varref); |
| 338 | varref = NULL; |
| 339 | value = NULL; |
| 340 | } |
| 341 | } |
| 342 | } else if (varref == NULL) { |
| 343 | variable = NULL; |
| 344 | varref_type = OVAL_ENTITY_VARREF_NONE; |
| 345 | if (datatype == OVAL_DATATYPE_RECORD) { |
| 346 | value = NULL; |
| 347 | } else { |
| 348 | if (xsi_nil) |
| 349 | value = NULL; |
| 350 | else |
| 351 | return_code = oval_value_parse_tag(reader, context, &oval_consume_value, &value); |
| 352 | } |
| 353 | } else { |
| 354 | struct oval_definition_model *model = context->definition_model; |
| 355 | variable = oval_definition_model_get_new_variable(model, varref, OVAL_VARIABLE_UNKNOWN); |
| 356 | varref_type = OVAL_ENTITY_VARREF_ATTRIBUTE; |
| 357 | value = NULL; |
| 358 | free(varref); |
| 359 | varref = NULL; |
| 360 | } |
| 361 | oval_entity_set_name(entity, name); |
| 362 | oval_entity_set_type(entity, type); |
| 363 | oval_entity_set_datatype(entity, datatype); |
| 364 | oval_entity_set_operation(entity, operation); |
| 365 | oval_entity_set_mask(entity, mask); |
| 366 | oval_entity_set_varref_type(entity, varref_type); |
| 367 | oval_entity_set_variable(entity, variable); |
| 368 | oval_entity_set_value(entity, value); |
| 369 | oval_entity_set_xsi_nil(entity, xsi_nil); |
| 370 | (*consumer) (entity, user); |
| 371 | |
| 372 | if (return_code != 0) { |
| 373 | dW("Parsing of <%s> terminated by an error at line %d.", name, xmlTextReaderGetParserLineNumber(reader)); |
| 374 | } |
| 375 | |
| 376 | free(name); |
| 377 | return return_code; |
| 378 | } |
| 379 | |
| 380 | xmlNode *oval_entity_to_dom(struct oval_entity *entity, xmlDoc * doc, xmlNode * parent) { |
| 381 | |
| 382 | xmlNsPtr ent_ns = parent->ns; |
| 383 | xmlNodePtr root_node = xmlDocGetRootElement(doc); |
| 384 | |
| 385 | xmlNode *entity_node = NULL; |
| 386 | char *content = NULL; |
| 387 | |
| 388 | struct oval_variable *variable = oval_entity_get_variable(entity); |
| 389 | oval_entity_varref_type_t vtype = oval_entity_get_varref_type(entity); |
| 390 | struct oval_value *value = oval_entity_get_value(entity); |
| 391 | |
| 392 | if (vtype == OVAL_ENTITY_VARREF_ELEMENT) { |
| 393 | content = oval_variable_get_id(variable); |
| 394 | } else if (value) { |
| 395 | content = oval_value_get_text(value); |
| 396 | } |
| 397 | |
| 398 | char *tagname = oval_entity_get_name(entity); |
| 399 | bool mask = oval_entity_get_mask(entity); |
| 400 | |
| 401 | /* omit the value and operation used for testing in oval_results if mask=true */ |
| 402 | /* Omit it only for older versions of OVAL than 5.10 */ |
| 403 | oval_schema_version_t oval_version = oval_definition_model_get_core_schema_version(entity->model); |
| 404 | if (oval_schema_version_cmp(oval_version, OVAL_SCHEMA_VERSION(5.10)) < 0 && |
| 405 | mask && !xmlStrcmp(root_node->name, BAD_CAST OVAL_ROOT_ELM_RESULTS)) { |
| 406 | entity_node = xmlNewTextChild(parent, ent_ns, BAD_CAST tagname, BAD_CAST ""); |
| 407 | } else { |
| 408 | entity_node = xmlNewTextChild(parent, ent_ns, BAD_CAST tagname, BAD_CAST content); |
| 409 | oval_operation_t operation = oval_entity_get_operation(entity); |
| 410 | if (operation != OVAL_OPERATION_EQUALS) |
| 411 | xmlNewProp(entity_node, BAD_CAST "operation", BAD_CAST oval_operation_get_text(operation)); |
| 412 | if (oscap_streq(content, "") && oval_entity_get_xsi_nil(entity)) { |
| 413 | // Export @xsi:nil="true" only if it was imported and the content is still empty |
| 414 | xmlNewNsProp(entity_node, lookup_xsi_ns(doc), BAD_CAST "nil", BAD_CAST "true"); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | oval_datatype_t datatype = oval_entity_get_datatype(entity); |
| 419 | if (datatype != OVAL_DATATYPE_STRING) |
| 420 | xmlNewProp(entity_node, BAD_CAST "datatype", BAD_CAST oval_datatype_get_text(datatype)); |
| 421 | if (mask) |
| 422 | xmlNewProp(entity_node, BAD_CAST "mask", BAD_CAST "true"); |
| 423 | if (vtype == OVAL_ENTITY_VARREF_ATTRIBUTE) |
| 424 | xmlNewProp(entity_node, BAD_CAST "var_ref", BAD_CAST oval_variable_get_id(variable)); |
| 425 | |
| 426 | return entity_node; |
| 427 | } |