| 1 | /** |
| 2 | * @file oval_state.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_state { |
| 46 | struct oval_definition_model *model; |
| 47 | oval_subtype_t subtype; |
| 48 | char *comment; |
| 49 | char *id; |
| 50 | int deprecated; |
| 51 | int version; |
| 52 | oval_operator_t operator; |
| 53 | struct oval_collection *notes; |
| 54 | struct oval_collection *contents; |
| 55 | } oval_state_t; |
| 56 | |
| 57 | bool oval_state_iterator_has_more(struct oval_state_iterator *oc_state) |
| 58 | { |
| 59 | return oval_collection_iterator_has_more((struct oval_iterator *) |
| 60 | oc_state); |
| 61 | } |
| 62 | |
| 63 | struct oval_state *oval_state_iterator_next(struct oval_state_iterator |
| 64 | *oc_state) |
| 65 | { |
| 66 | return (struct oval_state *) |
| 67 | oval_collection_iterator_next((struct oval_iterator *)oc_state); |
| 68 | } |
| 69 | |
| 70 | void oval_state_iterator_free(struct oval_state_iterator |
| 71 | *oc_state) |
| 72 | { |
| 73 | oval_collection_iterator_free((struct oval_iterator *)oc_state); |
| 74 | } |
| 75 | |
| 76 | oval_family_t oval_state_get_family(struct oval_state *state) |
| 77 | { |
| 78 | return (oval_state_get_subtype(state) / 1000) * 1000; |
| 79 | } |
| 80 | |
| 81 | oval_subtype_t oval_state_get_subtype(struct oval_state * state) |
| 82 | { |
| 83 | __attribute__nonnull__(state); |
| 84 | |
| 85 | return ((struct oval_state *)state)->subtype; |
| 86 | } |
| 87 | |
| 88 | const char *oval_state_get_name(struct oval_state *state) |
| 89 | { |
| 90 | return oval_subtype_get_text(oval_state_get_subtype(state)); |
| 91 | } |
| 92 | |
| 93 | struct oval_string_iterator *oval_state_get_notes(struct oval_state *state) |
| 94 | { |
| 95 | __attribute__nonnull__(state); |
| 96 | |
| 97 | return (struct oval_string_iterator *)oval_collection_iterator(state->notes); |
| 98 | } |
| 99 | |
| 100 | struct oval_state_content_iterator *oval_state_get_contents(struct oval_state *state) |
| 101 | { |
| 102 | __attribute__nonnull__(state); |
| 103 | |
| 104 | return (struct oval_state_content_iterator *) |
| 105 | oval_collection_iterator(state->contents); |
| 106 | } |
| 107 | |
| 108 | char *oval_state_get_comment(struct oval_state *state) |
| 109 | { |
| 110 | __attribute__nonnull__(state); |
| 111 | |
| 112 | return ((struct oval_state *)state)->comment; |
| 113 | } |
| 114 | |
| 115 | char *oval_state_get_id(struct oval_state *state) |
| 116 | { |
| 117 | __attribute__nonnull__(state); |
| 118 | |
| 119 | return ((struct oval_state *)state)->id; |
| 120 | } |
| 121 | |
| 122 | bool oval_state_get_deprecated(struct oval_state *state) |
| 123 | { |
| 124 | __attribute__nonnull__(state); |
| 125 | |
| 126 | return ((struct oval_state *)state)->deprecated; |
| 127 | } |
| 128 | |
| 129 | int oval_state_get_version(struct oval_state *state) |
| 130 | { |
| 131 | __attribute__nonnull__(state); |
| 132 | |
| 133 | return state->version; |
| 134 | } |
| 135 | |
| 136 | int oval_state_get_operator(struct oval_state *state) |
| 137 | { |
| 138 | __attribute__nonnull__(state); |
| 139 | |
| 140 | return state->operator; |
| 141 | } |
| 142 | |
| 143 | struct oval_state *oval_state_new(struct oval_definition_model *model, const char *id) |
| 144 | { |
| 145 | __attribute__nonnull__(model); |
| 146 | oval_state_t *state; |
| 147 | |
| 148 | state = (oval_state_t *) malloc(sizeof(oval_state_t)); |
| 149 | if (state == NULL) |
| 150 | return NULL; |
| 151 | |
| 152 | state->deprecated = 0; |
| 153 | state->version = 0; |
| 154 | state->operator = OVAL_OPERATOR_UNKNOWN; |
| 155 | state->subtype = OVAL_SUBTYPE_UNKNOWN; |
| 156 | state->comment = NULL; |
| 157 | state->id = oscap_strdup(id); |
| 158 | state->notes = oval_collection_new(); |
| 159 | state->contents = oval_collection_new(); |
| 160 | state->model = model; |
| 161 | |
| 162 | oval_definition_model_add_state(model, state); |
| 163 | |
| 164 | return state; |
| 165 | } |
| 166 | |
| 167 | struct oval_state *oval_state_clone(struct oval_definition_model *new_model, struct oval_state *old_state) { |
| 168 | __attribute__nonnull__(old_state); |
| 169 | |
| 170 | oval_state_t *new_state = oval_definition_model_get_state(new_model, old_state->id); |
| 171 | if (new_state == NULL) { |
| 172 | new_state = oval_state_new(new_model, old_state->id); |
| 173 | oval_state_set_deprecated(new_state, old_state->deprecated); |
| 174 | oval_state_set_version(new_state, old_state->version); |
| 175 | oval_state_set_operator(new_state, old_state->operator); |
| 176 | oval_state_set_subtype(new_state, old_state->subtype); |
| 177 | oval_state_set_comment(new_state, old_state->comment); |
| 178 | |
| 179 | struct oval_string_iterator *notes = oval_state_get_notes(old_state); |
| 180 | while (oval_string_iterator_has_more(notes)) { |
| 181 | char *note = oval_string_iterator_next(notes); |
| 182 | oval_state_add_note(new_state, note); |
| 183 | } |
| 184 | oval_string_iterator_free(notes); |
| 185 | struct oval_state_content_iterator *contents = oval_state_get_contents(old_state); |
| 186 | while (oval_state_content_iterator_has_more(contents)) { |
| 187 | struct oval_state_content *content = oval_state_content_iterator_next(contents); |
| 188 | oval_state_add_content(new_state, oval_state_content_clone(new_model, content)); |
| 189 | } |
| 190 | oval_state_content_iterator_free(contents); |
| 191 | } |
| 192 | return new_state; |
| 193 | } |
| 194 | |
| 195 | void oval_state_free(struct oval_state *state) |
| 196 | { |
| 197 | __attribute__nonnull__(state); |
| 198 | |
| 199 | if (state->comment != NULL) |
| 200 | free(state->comment); |
| 201 | if (state->id != NULL) |
| 202 | free(state->id); |
| 203 | oval_collection_free_items(state->notes, &free); |
| 204 | oval_collection_free_items(state->contents, (oscap_destruct_func) oval_state_content_free); |
| 205 | |
| 206 | state->comment = NULL; |
| 207 | state->contents = NULL; |
| 208 | state->id = NULL; |
| 209 | state->notes = NULL; |
| 210 | free(state); |
| 211 | } |
| 212 | |
| 213 | void oval_state_set_subtype(struct oval_state *state, oval_subtype_t subtype) |
| 214 | { |
| 215 | __attribute__nonnull__(state); |
| 216 | state->subtype = subtype; |
| 217 | } |
| 218 | |
| 219 | void oval_state_add_note(struct oval_state *state, char *notes) |
| 220 | { |
| 221 | __attribute__nonnull__(state); |
| 222 | oval_collection_add(state->notes, (void *)oscap_strdup(notes)); |
| 223 | } |
| 224 | |
| 225 | void oval_state_set_comment(struct oval_state *state, char *comm) |
| 226 | { |
| 227 | __attribute__nonnull__(state); |
| 228 | free(state->comment); |
| 229 | state->comment = oscap_strdup(comm); |
| 230 | } |
| 231 | |
| 232 | void oval_state_set_deprecated(struct oval_state *state, bool deprecated) |
| 233 | { |
| 234 | __attribute__nonnull__(state); |
| 235 | state->deprecated = deprecated; |
| 236 | } |
| 237 | |
| 238 | void oval_state_set_version(struct oval_state *state, int version) |
| 239 | { |
| 240 | __attribute__nonnull__(state); |
| 241 | state->version = version; |
| 242 | } |
| 243 | |
| 244 | void oval_state_set_operator(struct oval_state *state, oval_operator_t operator) |
| 245 | { |
| 246 | __attribute__nonnull__(state); |
| 247 | state->operator = operator; |
| 248 | } |
| 249 | |
| 250 | void oval_state_add_content(struct oval_state *state, struct oval_state_content *content) |
| 251 | { |
| 252 | __attribute__nonnull__(state); |
| 253 | oval_collection_add(state->contents, content); |
| 254 | } |
| 255 | |
| 256 | oval_schema_version_t oval_state_get_platform_schema_version(const struct oval_state *state) |
| 257 | { |
| 258 | oval_family_t family = oval_state_get_family((struct oval_state *)state); |
| 259 | const char *platform = oval_family_get_text(family); |
| 260 | return oval_definition_model_get_platform_schema_version(state->model, platform); |
| 261 | } |
| 262 | |
| 263 | static void _oval_note_consumer(char *text, void *state) |
| 264 | { |
| 265 | oval_state_add_note(state, text); |
| 266 | } |
| 267 | |
| 268 | static int _oval_state_parse_notes(xmlTextReaderPtr reader, struct oval_parser_context *context, void *user) |
| 269 | { |
| 270 | struct oval_state *state = (struct oval_state *)user; |
| 271 | return oscap_parser_text_value(reader, _oval_note_consumer, state); |
| 272 | } |
| 273 | |
| 274 | static void _oval_state_content_consumer(struct oval_state_content *content, struct oval_state *state) { |
| 275 | oval_state_add_content(state, content); |
| 276 | } |
| 277 | |
| 278 | static int _oval_state_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *user) |
| 279 | { |
| 280 | __attribute__nonnull__(user); |
| 281 | |
| 282 | struct oval_state *state = (struct oval_state *)user; |
| 283 | char *tagname = (char *)xmlTextReaderLocalName(reader); |
| 284 | xmlChar *namespace = xmlTextReaderNamespaceUri(reader); |
| 285 | int return_code = 0; |
| 286 | if ((strcmp(tagname, "notes") == 0)) { |
| 287 | return_code = oval_parser_parse_tag(reader, context, &_oval_state_parse_notes, state); |
| 288 | } else { |
| 289 | return_code = oval_state_content_parse_tag |
| 290 | (reader, context, (oscap_consumer_func) _oval_state_content_consumer, state); |
| 291 | } |
| 292 | free(tagname); |
| 293 | free(namespace); |
| 294 | return return_code; |
| 295 | } |
| 296 | |
| 297 | int oval_state_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *usr) |
| 298 | { |
| 299 | struct oval_definition_model *model = context->definition_model; |
| 300 | char *id = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "id"); |
| 301 | struct oval_state *state = oval_definition_model_get_new_state(model, id); |
| 302 | oval_subtype_t subtype = oval_subtype_parse(reader); |
| 303 | if (subtype == OVAL_SUBTYPE_UNKNOWN) { |
| 304 | dI("Unknown state type %s, using independent/unknown", id); |
| 305 | subtype = OVAL_INDEPENDENT_UNKNOWN; |
| 306 | } |
| 307 | free(id); |
| 308 | oval_state_set_subtype(state, subtype); |
| 309 | char *comm = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "comment"); |
| 310 | if (comm != NULL) { |
| 311 | oval_state_set_comment(state, comm); |
| 312 | free(comm); |
| 313 | comm = NULL; |
| 314 | } |
| 315 | int deprecated = oval_parser_boolean_attribute(reader, "deprecated", 0); |
| 316 | oval_state_set_deprecated(state, deprecated); |
| 317 | char *version = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "version"); |
| 318 | oval_state_set_version(state, atoi(version)); |
| 319 | free(version); |
| 320 | oval_operator_t operator = oval_operator_parse(reader, "operator", OVAL_OPERATOR_AND); |
| 321 | oval_state_set_operator(state, operator); |
| 322 | |
| 323 | int return_code = oval_parser_parse_tag(reader, context, &_oval_state_parse_tag, state); |
| 324 | return return_code; |
| 325 | } |
| 326 | |
| 327 | xmlNode *oval_state_to_dom(struct oval_state *state, xmlDoc * doc, xmlNode * parent) |
| 328 | { |
| 329 | xmlNode *state_node = NULL; |
| 330 | |
| 331 | /* get state name */ |
| 332 | oval_subtype_t subtype = oval_state_get_subtype(state); |
| 333 | if (subtype == OVAL_SUBTYPE_UNKNOWN) { |
| 334 | dW("Unable to export unknown state %s, skipping", oval_state_get_id(state)); |
| 335 | return state_node; |
| 336 | } |
| 337 | |
| 338 | const char *subtype_text = oval_subtype_get_text(subtype); |
| 339 | char *state_name = malloc(strlen(subtype_text) + 7); |
| 340 | sprintf(state_name, "%s_state", subtype_text); |
| 341 | |
| 342 | oval_family_t family = oval_state_get_family(state); |
| 343 | |
| 344 | /* search namespace & create child */ |
| 345 | xmlNs *ns_family = oval_family_to_namespace(family, (const char *) OVAL_DEFINITIONS_NAMESPACE, doc, parent); |
| 346 | state_node = xmlNewTextChild(parent, ns_family, BAD_CAST state_name, NULL); |
| 347 | free(state_name); |
| 348 | |
| 349 | char *id = oval_state_get_id(state); |
| 350 | xmlNewProp(state_node, BAD_CAST "id", BAD_CAST id); |
| 351 | |
| 352 | char version[10]; |
| 353 | *version = '\0'; |
| 354 | snprintf(version, sizeof(version), "%d", oval_state_get_version(state)); |
| 355 | xmlNewProp(state_node, BAD_CAST "version", BAD_CAST version); |
| 356 | |
| 357 | oval_operator_t operator = oval_state_get_operator(state); |
| 358 | const char *operator_text = oval_operator_get_text(operator); |
| 359 | if (operator != OVAL_OPERATOR_AND) |
| 360 | xmlNewProp(state_node, BAD_CAST "operator", BAD_CAST operator_text); |
| 361 | |
| 362 | char *comm = oval_state_get_comment(state); |
| 363 | if (comm) |
| 364 | xmlNewProp(state_node, BAD_CAST "comment", BAD_CAST comm); |
| 365 | |
| 366 | bool deprecated = oval_state_get_deprecated(state); |
| 367 | if (deprecated) |
| 368 | xmlNewProp(state_node, BAD_CAST "deprecated", BAD_CAST "true"); |
| 369 | |
| 370 | struct oval_string_iterator *notes = oval_state_get_notes(state); |
| 371 | if (oval_string_iterator_has_more(notes)) { |
| 372 | xmlNs *ns_definitions = xmlSearchNsByHref(doc, parent, OVAL_DEFINITIONS_NAMESPACE); |
| 373 | xmlNode *notes_node = xmlNewTextChild(state_node, ns_definitions, BAD_CAST "notes", NULL); |
| 374 | while (oval_string_iterator_has_more(notes)) { |
| 375 | char *note = oval_string_iterator_next(notes); |
| 376 | xmlNewTextChild(notes_node, ns_definitions, BAD_CAST "note", BAD_CAST note); |
| 377 | } |
| 378 | } |
| 379 | oval_string_iterator_free(notes); |
| 380 | |
| 381 | struct oval_state_content_iterator *contents = oval_state_get_contents(state); |
| 382 | while (oval_state_content_iterator_has_more(contents)) { |
| 383 | struct oval_state_content *content = oval_state_content_iterator_next(contents); |
| 384 | oval_state_content_to_dom(content, doc, state_node); |
| 385 | } |
| 386 | oval_state_content_iterator_free(contents); |
| 387 | return state_node; |
| 388 | } |