| 1 | /** |
| 2 | * @file oval_syschar.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 | * Šimon Lukašík |
| 29 | */ |
| 30 | |
| 31 | #ifdef HAVE_CONFIG_H |
| 32 | #include <config.h> |
| 33 | #endif |
| 34 | |
| 35 | #include <stdlib.h> |
| 36 | #include <stdio.h> |
| 37 | #include <string.h> |
| 38 | |
| 39 | #include "oval_system_characteristics_impl.h" |
| 40 | #include "adt/oval_collection_impl.h" |
| 41 | #include "oval_agent_api_impl.h" |
| 42 | #include "oval_definitions_impl.h" |
| 43 | |
| 44 | #include "common/util.h" |
| 45 | #include "common/debug_priv.h" |
| 46 | |
| 47 | typedef struct oval_syschar { |
| 48 | struct oval_syschar_model *model; |
| 49 | oval_syschar_collection_flag_t flag; |
| 50 | struct oval_collection *messages; |
| 51 | struct oval_object *object; |
| 52 | struct oval_collection *variable_bindings; ///< Represents <variable_value> elements |
| 53 | struct oval_collection *sysitem; ///< Represents <reference> elements |
| 54 | int variable_instance; ///< Represents variable_instance attribute |
| 55 | int variable_instance_hint; ///< Internal hint of the next possible variable_instance attribute |
| 56 | } oval_syschar_t; ///< Represents a single collected <object> element |
| 57 | |
| 58 | oval_syschar_collection_flag_t oval_syschar_get_flag(struct oval_syschar |
| 59 | *syschar) |
| 60 | { |
| 61 | __attribute__nonnull__(syschar); |
| 62 | |
| 63 | return ((struct oval_syschar *)syschar)->flag; |
| 64 | } |
| 65 | |
| 66 | void oval_syschar_set_flag(struct oval_syschar *syschar, oval_syschar_collection_flag_t flag) { |
| 67 | __attribute__nonnull__(syschar); |
| 68 | syschar->flag = flag; |
| 69 | } |
| 70 | |
| 71 | void oval_syschar_set_object(struct oval_syschar *syschar, struct oval_object *object) |
| 72 | { |
| 73 | __attribute__nonnull__(syschar); |
| 74 | syschar->object = object; |
| 75 | } |
| 76 | |
| 77 | struct oval_message_iterator *oval_syschar_get_messages(struct oval_syschar *syschar) |
| 78 | { |
| 79 | return (struct oval_message_iterator *)oval_collection_iterator(syschar->messages); |
| 80 | } |
| 81 | |
| 82 | void oval_syschar_add_message(struct oval_syschar *syschar, struct oval_message *message) |
| 83 | { |
| 84 | __attribute__nonnull__(syschar); |
| 85 | oval_collection_add(syschar->messages, message); |
| 86 | } |
| 87 | |
| 88 | void oval_syschar_add_new_message(struct oval_syschar *syschar, char *text, oval_message_level_t level) |
| 89 | { |
| 90 | __attribute__nonnull__(syschar); |
| 91 | struct oval_message *msg; |
| 92 | |
| 93 | msg = oval_message_new(); |
| 94 | oval_message_set_text(msg, text); |
| 95 | oval_message_set_level(msg, level); |
| 96 | oval_collection_add(syschar->messages, msg); |
| 97 | } |
| 98 | |
| 99 | struct oval_object *oval_syschar_get_object(struct oval_syschar *syschar) |
| 100 | { |
| 101 | __attribute__nonnull__(syschar); |
| 102 | |
| 103 | return ((struct oval_syschar *)syschar)->object; |
| 104 | } |
| 105 | |
| 106 | struct oval_syschar_model *oval_syschar_get_model(struct oval_syschar *syschar) |
| 107 | { |
| 108 | __attribute__nonnull__(syschar); |
| 109 | |
| 110 | return ((struct oval_syschar *)syschar)->model; |
| 111 | } |
| 112 | |
| 113 | struct oval_variable_binding_iterator *oval_syschar_get_variable_bindings(struct |
| 114 | oval_syschar |
| 115 | *syschar) |
| 116 | { |
| 117 | __attribute__nonnull__(syschar); |
| 118 | |
| 119 | return (struct oval_variable_binding_iterator *) |
| 120 | oval_collection_iterator(syschar->variable_bindings); |
| 121 | } |
| 122 | |
| 123 | struct oval_sysitem_iterator *oval_syschar_get_sysitem(struct oval_syschar *syschar) |
| 124 | { |
| 125 | __attribute__nonnull__(syschar); |
| 126 | |
| 127 | return (struct oval_sysitem_iterator *) |
| 128 | oval_collection_iterator(syschar->sysitem); |
| 129 | } |
| 130 | |
| 131 | void oval_syschar_add_sysitem(struct oval_syschar *syschar, struct oval_sysitem *sysitem) { |
| 132 | __attribute__nonnull__(syschar); |
| 133 | |
| 134 | oval_collection_add(syschar->sysitem, sysitem); |
| 135 | } |
| 136 | |
| 137 | void oval_syschar_add_variable_binding(struct oval_syschar *syschar, struct oval_variable_binding *binding) { |
| 138 | __attribute__nonnull__(syschar); |
| 139 | |
| 140 | oval_collection_add(syschar->variable_bindings, binding); |
| 141 | } |
| 142 | |
| 143 | struct oval_syschar *oval_syschar_new(struct oval_syschar_model *model, struct oval_object *object) |
| 144 | { |
| 145 | __attribute__nonnull__(model); |
| 146 | oval_syschar_t *syschar; |
| 147 | |
| 148 | syschar = (oval_syschar_t *) malloc(sizeof(oval_syschar_t)); |
| 149 | if (syschar == NULL) |
| 150 | return NULL; |
| 151 | |
| 152 | syschar->flag = SYSCHAR_FLAG_UNKNOWN; |
| 153 | syschar->variable_instance = 1; |
| 154 | syschar->variable_instance_hint = 1; |
| 155 | syschar->object = object; |
| 156 | syschar->messages = oval_collection_new(); |
| 157 | syschar->sysitem = oval_collection_new(); |
| 158 | syschar->variable_bindings = oval_collection_new(); |
| 159 | syschar->model = model; |
| 160 | |
| 161 | oval_syschar_model_add_syschar(model, syschar); |
| 162 | |
| 163 | return syschar; |
| 164 | } |
| 165 | |
| 166 | struct oval_syschar *oval_syschar_clone(struct oval_syschar_model *new_model, struct oval_syschar *old_syschar) |
| 167 | { |
| 168 | struct oval_definition_model *def_model = oval_syschar_model_get_definition_model(new_model); |
| 169 | struct oval_object *old_object = oval_syschar_get_object(old_syschar); |
| 170 | struct oval_object *new_object = oval_definition_model_get_object(def_model, oval_object_get_id(old_object)); |
| 171 | if (new_object == NULL) { |
| 172 | new_object = oval_object_clone(def_model, old_object); |
| 173 | } |
| 174 | |
| 175 | struct oval_syschar *new_syschar = oval_syschar_new(new_model, new_object); |
| 176 | |
| 177 | oval_syschar_collection_flag_t flag = oval_syschar_get_flag(old_syschar); |
| 178 | oval_syschar_set_flag(new_syschar, flag); |
| 179 | int variable_instance = oval_syschar_get_variable_instance(old_syschar); |
| 180 | oval_syschar_set_variable_instance(new_syschar, variable_instance); |
| 181 | oval_syschar_set_variable_instance_hint(new_syschar, variable_instance); |
| 182 | |
| 183 | struct oval_message_iterator *old_messages = oval_syschar_get_messages(old_syschar); |
| 184 | while (oval_message_iterator_has_more(old_messages)) { |
| 185 | struct oval_message *old_message = oval_message_iterator_next(old_messages); |
| 186 | struct oval_message *new_message = oval_message_clone(old_message); |
| 187 | oval_syschar_add_message(new_syschar, new_message); |
| 188 | } |
| 189 | oval_message_iterator_free(old_messages); |
| 190 | |
| 191 | struct oval_sysitem_iterator *old_sysitems = oval_syschar_get_sysitem(old_syschar); |
| 192 | while (oval_sysitem_iterator_has_more(old_sysitems)) { |
| 193 | struct oval_sysitem *old_sysitem = oval_sysitem_iterator_next(old_sysitems); |
| 194 | struct oval_sysitem *new_sysitem = |
| 195 | oval_syschar_model_get_sysitem(new_model, oval_sysitem_get_id(old_sysitem)); |
| 196 | if (new_sysitem == NULL) |
| 197 | new_sysitem = oval_sysitem_clone(new_model, old_sysitem); |
| 198 | oval_syschar_add_sysitem(new_syschar, new_sysitem); |
| 199 | } |
| 200 | oval_sysitem_iterator_free(old_sysitems); |
| 201 | |
| 202 | struct oval_variable_binding_iterator *old_bindings = oval_syschar_get_variable_bindings(old_syschar); |
| 203 | while (oval_variable_binding_iterator_has_more(old_bindings)) { |
| 204 | struct oval_variable_binding *old_binding = oval_variable_binding_iterator_next(old_bindings); |
| 205 | struct oval_variable_binding *new_binding = oval_variable_binding_clone(old_binding, def_model); |
| 206 | oval_syschar_add_variable_binding(new_syschar, new_binding); |
| 207 | } |
| 208 | oval_variable_binding_iterator_free(old_bindings); |
| 209 | |
| 210 | return new_syschar; |
| 211 | } |
| 212 | |
| 213 | void oval_syschar_free(struct oval_syschar *syschar) |
| 214 | { |
| 215 | if (syschar == NULL) |
| 216 | return; |
| 217 | |
| 218 | oval_collection_free_items(syschar->messages, (oscap_destruct_func) oval_message_free); |
| 219 | oval_collection_free_items(syschar->sysitem, NULL); //sysitems are shared with syschar_model |
| 220 | oval_collection_free_items(syschar->variable_bindings, (oscap_destruct_func) oval_variable_binding_free); |
| 221 | |
| 222 | syschar->messages = NULL; |
| 223 | syschar->object = NULL; |
| 224 | syschar->sysitem = NULL; |
| 225 | syschar->variable_bindings = NULL; |
| 226 | free(syschar); |
| 227 | } |
| 228 | |
| 229 | static void add_oval_syschar_message(struct oval_syschar *syschar, struct oval_message *message) { |
| 230 | __attribute__nonnull__(syschar); |
| 231 | |
| 232 | oval_collection_add(syschar->messages, message); |
| 233 | } |
| 234 | |
| 235 | static void _oval_syschar_parse_subtag_consume_message(struct oval_message *message, void *syschar) |
| 236 | { |
| 237 | add_oval_syschar_message((struct oval_syschar *)syschar, message); |
| 238 | } |
| 239 | |
| 240 | struct oval_syschar_parse_subtag_varval_context { |
| 241 | struct oval_syschar_model *model; |
| 242 | struct oval_syschar *syschar; |
| 243 | }; |
| 244 | static void _oval_syschar_parse_subtag_consume_variable_binding(struct oval_variable_binding *binding, void *user) |
| 245 | { |
| 246 | struct oval_syschar_parse_subtag_varval_context *ctx = user; |
| 247 | |
| 248 | __attribute__nonnull__(ctx); |
| 249 | |
| 250 | oval_syschar_add_variable_binding(ctx->syschar, binding); |
| 251 | } |
| 252 | |
| 253 | static int _oval_syschar_parse_subtag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *client) |
| 254 | { |
| 255 | |
| 256 | __attribute__nonnull__(context); |
| 257 | |
| 258 | struct oval_syschar *syschar = client; |
| 259 | char *tagname = (char *)xmlTextReaderLocalName(reader); |
| 260 | char *namespace = (char *)xmlTextReaderNamespaceUri(reader); |
| 261 | int return_code = 0; |
| 262 | |
| 263 | if (strcmp("message", tagname) == 0) { |
| 264 | return_code = oval_message_parse_tag |
| 265 | (reader, context, (oscap_consumer_func) _oval_syschar_parse_subtag_consume_message, syschar); |
| 266 | } else if (strcmp("variable_value", tagname) == 0) { |
| 267 | struct oval_syschar_parse_subtag_varval_context ctx = { context->syschar_model, syschar }; |
| 268 | return_code = oval_variable_binding_parse_tag |
| 269 | (reader, context, &_oval_syschar_parse_subtag_consume_variable_binding, &ctx); |
| 270 | } else if (strcmp("reference", tagname) == 0) { |
| 271 | char *itemid = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "item_ref"); |
| 272 | struct oval_sysitem *sysitem = oval_syschar_model_get_new_sysitem(context->syschar_model, itemid); |
| 273 | free(itemid); |
| 274 | oval_syschar_add_sysitem(syschar, sysitem); |
| 275 | } |
| 276 | |
| 277 | if (return_code != 0) { |
| 278 | dW("Parsing of <%s> terminated by an error at line %d.", tagname, xmlTextReaderGetParserLineNumber(reader)); |
| 279 | } |
| 280 | |
| 281 | free(tagname); |
| 282 | free(namespace); |
| 283 | |
| 284 | return return_code; |
| 285 | } |
| 286 | |
| 287 | int oval_syschar_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *usr) |
| 288 | { |
| 289 | __attribute__nonnull__(context); |
| 290 | |
| 291 | char *tagname = (char *)xmlTextReaderLocalName(reader); |
| 292 | char *namespace = (char *)xmlTextReaderNamespaceUri(reader); |
| 293 | int return_code = 0; |
| 294 | |
| 295 | int is_ovalsys = 0; |
| 296 | if (namespace != NULL) { |
| 297 | is_ovalsys = strcmp((const char *)OVAL_SYSCHAR_NAMESPACE, namespace) == 0; |
| 298 | } |
| 299 | if (is_ovalsys && (strcmp(tagname, "object") == 0)) { |
| 300 | char *object_id = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "id"); |
| 301 | struct oval_object *object = oval_definition_model_get_new_object(context->definition_model, object_id); |
| 302 | free(object_id); |
| 303 | |
| 304 | oval_syschar_t *syschar = oval_syschar_model_get_new_syschar(context->syschar_model, object); |
| 305 | char *flag = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "flag"); |
| 306 | oval_syschar_collection_flag_t flag_enum = oval_syschar_flag_parse(reader, "flag", SYSCHAR_FLAG_UNKNOWN); |
| 307 | free(flag); |
| 308 | oval_syschar_set_flag(syschar, flag_enum); |
| 309 | |
| 310 | return_code = oval_parser_parse_tag(reader, context, &_oval_syschar_parse_subtag, syschar); |
| 311 | } else { |
| 312 | dW("Skipping tag: %s", tagname); |
| 313 | oval_parser_skip_tag(reader, context); |
| 314 | } |
| 315 | |
| 316 | if (return_code != 0) { |
| 317 | dW("Parsing of <%s> terminated by an error at line %d.", tagname, xmlTextReaderGetParserLineNumber(reader)); |
| 318 | } |
| 319 | |
| 320 | free(tagname); |
| 321 | free(namespace); |
| 322 | |
| 323 | return return_code; |
| 324 | } |
| 325 | |
| 326 | void oval_syschar_to_dom(struct oval_syschar *syschar, xmlDoc * doc, xmlNode * tag_parent) |
| 327 | { |
| 328 | |
| 329 | if (syschar) { |
| 330 | xmlNs *ns_syschar = xmlSearchNsByHref(doc, tag_parent, OVAL_SYSCHAR_NAMESPACE); |
| 331 | xmlNode *tag_syschar = xmlNewTextChild(tag_parent, ns_syschar, BAD_CAST "object", NULL); |
| 332 | |
| 333 | { /*attributes */ |
| 334 | struct oval_object *object = oval_syschar_get_object(syschar); |
| 335 | xmlNewProp(tag_syschar, BAD_CAST "id", BAD_CAST oval_object_get_id(object)); |
| 336 | char version[17]; |
| 337 | snprintf(version, sizeof(version), "%d", oval_object_get_version(object)); |
| 338 | xmlNewProp(tag_syschar, BAD_CAST "version", BAD_CAST version); |
| 339 | oval_syschar_collection_flag_t flag = oval_syschar_get_flag(syschar); |
| 340 | xmlNewProp(tag_syschar, BAD_CAST "flag", BAD_CAST oval_syschar_collection_flag_get_text(flag)); |
| 341 | |
| 342 | int instance = oval_syschar_get_variable_instance(syschar); |
| 343 | if (instance != 1 || |
| 344 | oval_syschar_get_variable_instance_hint(syschar) != instance) { |
| 345 | char instance_att[10] = ""; |
| 346 | snprintf(instance_att, sizeof(instance_att), "%d", instance); |
| 347 | xmlNewProp(tag_syschar, BAD_CAST "variable_instance", BAD_CAST instance_att); |
| 348 | } |
| 349 | } |
| 350 | { /*messages */ |
| 351 | struct oval_message_iterator *messages = oval_syschar_get_messages(syschar); |
| 352 | while (oval_message_iterator_has_more(messages)) { |
| 353 | struct oval_message *message = oval_message_iterator_next(messages); |
| 354 | oval_message_to_dom(message, doc, tag_syschar); |
| 355 | } |
| 356 | oval_message_iterator_free(messages); |
| 357 | } |
| 358 | { /*variable values */ |
| 359 | struct oval_variable_binding_iterator *bindings = oval_syschar_get_variable_bindings(syschar); |
| 360 | while (oval_variable_binding_iterator_has_more(bindings)) { |
| 361 | struct oval_variable_binding *binding = oval_variable_binding_iterator_next(bindings); |
| 362 | oval_variable_binding_to_dom(binding, doc, tag_syschar); |
| 363 | } |
| 364 | oval_variable_binding_iterator_free(bindings); |
| 365 | } |
| 366 | { /*references */ |
| 367 | struct oval_sysitem_iterator *sysitems = oval_syschar_get_sysitem(syschar); |
| 368 | while (oval_sysitem_iterator_has_more(sysitems)) { |
| 369 | struct oval_sysitem *sysitem = oval_sysitem_iterator_next(sysitems); |
| 370 | xmlNode *tag_reference = xmlNewTextChild |
| 371 | (tag_syschar, ns_syschar, BAD_CAST "reference", NULL); |
| 372 | xmlNewProp(tag_reference, BAD_CAST "item_ref", BAD_CAST oval_sysitem_get_id(sysitem)); |
| 373 | } |
| 374 | oval_sysitem_iterator_free(sysitems); |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | int oval_syschar_get_variable_instance(const struct oval_syschar *syschar) |
| 380 | { |
| 381 | __attribute__nonnull__(syschar); |
| 382 | return syschar->variable_instance; |
| 383 | } |
| 384 | |
| 385 | void oval_syschar_set_variable_instance(struct oval_syschar *syschar, int variable_instance_in) |
| 386 | { |
| 387 | __attribute__nonnull__(syschar); |
| 388 | syschar->variable_instance = variable_instance_in; |
| 389 | } |
| 390 | |
| 391 | int oval_syschar_get_variable_instance_hint(const struct oval_syschar *syschar) |
| 392 | { |
| 393 | __attribute__nonnull__(syschar); |
| 394 | return syschar->variable_instance_hint; |
| 395 | } |
| 396 | |
| 397 | void oval_syschar_set_variable_instance_hint(struct oval_syschar *syschar, int variable_instance_hint_in) |
| 398 | { |
| 399 | __attribute__nonnull__(syschar); |
| 400 | syschar->variable_instance_hint = variable_instance_hint_in; |
| 401 | } |
| 402 | |
| 403 | const char *oval_syschar_get_id(const struct oval_syschar *syschar) |
| 404 | { |
| 405 | __attribute__nonnull__(syschar); |
| 406 | struct oval_object *obj = oval_syschar_get_object((struct oval_syschar *) syschar); |
| 407 | return (obj == NULL) ? NULL : oval_object_get_id(obj); |
| 408 | } |