| 1 | /** |
| 2 | * @file oval_varModel.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 <string.h> |
| 35 | #include <time.h> |
| 36 | |
| 37 | #include "oval_definitions_impl.h" |
| 38 | #include "oval_agent_api_impl.h" |
| 39 | #include "oval_parser_impl.h" |
| 40 | #include "adt/oval_string_map_impl.h" |
| 41 | #include "oval_system_characteristics_impl.h" |
| 42 | #include "common/util.h" |
| 43 | #include "common/debug_priv.h" |
| 44 | #include "common/_error.h" |
| 45 | #include "common/elements.h" |
| 46 | #include "oscap_source.h" |
| 47 | #include "source/oscap_source_priv.h" |
| 48 | |
| 49 | typedef struct _oval_variable_model_frame { |
| 50 | char *id; |
| 51 | char *comment; |
| 52 | struct oval_collection *values; |
| 53 | oval_datatype_t datatype; |
| 54 | } _oval_variable_model_frame_t; |
| 55 | |
| 56 | typedef struct oval_variable_model { |
| 57 | struct oval_generator *generator; |
| 58 | struct oval_string_map *varmap; |
| 59 | } oval_variable_model_t; |
| 60 | |
| 61 | static _oval_variable_model_frame_t *_oval_variable_model_frame_new(char *id, const char *comm, oval_datatype_t datatype); |
| 62 | static int _oval_variable_model_parse_tag(xmlTextReader * reader, struct oval_parser_context *context, |
| 63 | struct oval_variable_model *model); |
| 64 | static void _oval_variable_model_frame_free(_oval_variable_model_frame_t * frame); |
| 65 | static int _oval_variable_model_parse_variable_values(xmlTextReader * reader, struct oval_parser_context *context, |
| 66 | _oval_variable_model_frame_t * frame); |
| 67 | static int _oval_variable_model_parse_variable(xmlTextReader * reader, struct oval_parser_context *context, |
| 68 | struct oval_variable_model *model); |
| 69 | static int _oval_variable_model_parse_variables(xmlTextReader * reader, struct oval_parser_context *context, |
| 70 | struct oval_variable_model *model); |
| 71 | static int _oval_variable_model_parse(struct oval_variable_model *model, xmlTextReader * reader, void *user_param); |
| 72 | |
| 73 | static _oval_variable_model_frame_t *_oval_variable_model_frame_new(char *id, const char *comm, oval_datatype_t datatype) |
| 74 | { |
| 75 | _oval_variable_model_frame_t *frame = (_oval_variable_model_frame_t *) malloc(sizeof(_oval_variable_model_frame_t)); |
| 76 | if (frame == NULL) |
| 77 | return NULL; |
| 78 | frame->id = oscap_strdup(id); |
| 79 | frame->comment = oscap_strdup(comm); |
| 80 | frame->datatype = datatype; |
| 81 | frame->values = oval_collection_new(); |
| 82 | return frame; |
| 83 | } |
| 84 | |
| 85 | bool oval_variable_model_iterator_has_more(struct oval_variable_model_iterator *itr) |
| 86 | { |
| 87 | return oval_collection_iterator_has_more((struct oval_iterator *) itr); |
| 88 | } |
| 89 | |
| 90 | struct oval_variable_model *oval_variable_model_iterator_next(struct oval_variable_model_iterator *itr) |
| 91 | { |
| 92 | return (struct oval_variable_model *) oval_collection_iterator_next((struct oval_iterator *) itr); |
| 93 | } |
| 94 | |
| 95 | void oval_variable_model_iterator_free(struct oval_variable_model_iterator *itr) |
| 96 | { |
| 97 | oval_collection_iterator_free((struct oval_iterator *) itr); |
| 98 | } |
| 99 | |
| 100 | static void _oval_variable_model_frame_free(_oval_variable_model_frame_t * frame) |
| 101 | { |
| 102 | if (frame) { |
| 103 | if (frame->id) |
| 104 | free(frame->id); |
| 105 | if (frame->comment) |
| 106 | free(frame->comment); |
| 107 | oval_collection_free_items(frame->values, (oscap_destruct_func) oval_value_free); |
| 108 | frame->id = NULL; |
| 109 | frame->comment = NULL; |
| 110 | frame->values = NULL; |
| 111 | frame->datatype = 0; |
| 112 | free(frame); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | struct oval_variable_model *oval_variable_model_new() |
| 117 | { |
| 118 | oval_variable_model_t *model = (oval_variable_model_t *) malloc(sizeof(oval_variable_model_t)); |
| 119 | if (model == NULL) |
| 120 | return NULL; |
| 121 | model->generator = oval_generator_new(); |
| 122 | model->varmap = oval_string_map_new(); |
| 123 | return model; |
| 124 | } |
| 125 | |
| 126 | struct oval_variable_model *oval_variable_model_clone(struct oval_variable_model *old_model) |
| 127 | { |
| 128 | struct oval_variable_model *new_model = oval_variable_model_new(); |
| 129 | struct oval_string_iterator *old_varids = oval_variable_model_get_variable_ids(old_model); |
| 130 | while (oval_string_iterator_has_more(old_varids)) { |
| 131 | char *varid = oval_string_iterator_next(old_varids); |
| 132 | oval_datatype_t datatype = oval_variable_model_get_datatype(old_model, varid); |
| 133 | struct oval_value_iterator *values = oval_variable_model_get_values(old_model, varid); |
| 134 | const char *comm = oval_variable_model_get_comment(old_model, varid); |
| 135 | while (oval_value_iterator_has_more(values)) { |
| 136 | struct oval_value *ov; |
| 137 | char *text; |
| 138 | |
| 139 | ov = oval_value_iterator_next(values); |
| 140 | text = oval_value_get_text(ov); |
| 141 | oval_variable_model_add(new_model, varid, comm, datatype, text); |
| 142 | } |
| 143 | oval_value_iterator_free(values); |
| 144 | } oval_string_iterator_free(old_varids); |
| 145 | return new_model; |
| 146 | } |
| 147 | |
| 148 | void oval_variable_model_free(struct oval_variable_model *model) |
| 149 | { |
| 150 | if (model) { |
| 151 | oval_string_map_free(model->varmap, (oscap_destruct_func) _oval_variable_model_frame_free); |
| 152 | model->varmap = NULL; |
| 153 | oval_generator_free(model->generator); |
| 154 | free(model); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | struct oval_generator *oval_variable_model_get_generator(struct oval_variable_model *model) |
| 159 | { |
| 160 | return model->generator; |
| 161 | } |
| 162 | |
| 163 | void oval_variable_model_set_generator(struct oval_variable_model *model, struct oval_generator *generator) |
| 164 | { |
| 165 | oval_generator_free(model->generator); |
| 166 | model->generator = generator; |
| 167 | } |
| 168 | |
| 169 | void oval_variable_model_add(struct oval_variable_model *model, char *varid, const char *comm, |
| 170 | oval_datatype_t datatype, char *value) |
| 171 | { |
| 172 | struct oval_value *ov; |
| 173 | |
| 174 | struct _oval_variable_model_frame *frame = |
| 175 | (struct _oval_variable_model_frame *)oval_string_map_get_value(model->varmap, varid); |
| 176 | if (frame == NULL) { |
| 177 | frame = _oval_variable_model_frame_new(varid, comm, datatype); |
| 178 | oval_string_map_put(model->varmap, varid, frame); |
| 179 | } |
| 180 | ov = oval_value_new(datatype, value); |
| 181 | oval_collection_add(frame->values, ov); |
| 182 | } |
| 183 | |
| 184 | #define NAMESPACE_VARIABLES "http://oval.mitre.org/XMLSchema/oval-variables-5" |
| 185 | |
| 186 | static int _oval_variable_model_parse_variable_values |
| 187 | (xmlTextReader * reader, struct oval_parser_context *context, _oval_variable_model_frame_t * frame) { |
| 188 | char *tagname = (char *)xmlTextReaderLocalName(reader); |
| 189 | char *namespace = (char *)xmlTextReaderNamespaceUri(reader); |
| 190 | int return_code; |
| 191 | bool is_variable_ns = oscap_strcmp(NAMESPACE_VARIABLES, namespace) == 0; |
| 192 | if (is_variable_ns && oscap_strcmp("value", tagname) == 0) { |
| 193 | struct oval_value *ov; |
| 194 | |
| 195 | return_code = xmlTextReaderRead(reader); |
| 196 | char *value = (char *)xmlTextReaderValue(reader); |
| 197 | ov = oval_value_new(frame->datatype, value); |
| 198 | oval_collection_add(frame->values, ov); |
| 199 | free(value); |
| 200 | } else { |
| 201 | dW("Unprocessed tag: <%s:%s>.", namespace, tagname); |
| 202 | oval_parser_skip_tag(reader, context); |
| 203 | return_code = 0; |
| 204 | } |
| 205 | free(tagname); |
| 206 | free(namespace); |
| 207 | return return_code; |
| 208 | } |
| 209 | |
| 210 | static int _oval_variable_model_parse_variable |
| 211 | (xmlTextReader * reader, struct oval_parser_context *context, struct oval_variable_model *model) { |
| 212 | char *id = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "id"); |
| 213 | char *comm = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "comment"); |
| 214 | oval_datatype_t datatype = oval_datatype_parse(reader, "datatype", OVAL_DATATYPE_STRING); |
| 215 | _oval_variable_model_frame_t *frame = oval_string_map_get_value(model->varmap, id); |
| 216 | int return_code = 1; |
| 217 | if (frame) { |
| 218 | if (frame->datatype != datatype) { |
| 219 | dW("Unmatched variable datatypes: %s:%s.", |
| 220 | oval_datatype_get_text(frame->datatype), oval_datatype_get_text(datatype)); |
| 221 | oval_parser_skip_tag(reader, context); |
| 222 | return_code = 0; |
| 223 | } |
| 224 | frame = NULL; |
| 225 | } else { |
| 226 | frame = _oval_variable_model_frame_new(id, comm, datatype); |
| 227 | oval_string_map_put(model->varmap, id, frame); |
| 228 | } |
| 229 | return_code = |
| 230 | oval_parser_parse_tag(reader, context, (oval_xml_tag_parser) _oval_variable_model_parse_variable_values, frame); |
| 231 | free(id); |
| 232 | free(comm); |
| 233 | return return_code; |
| 234 | } |
| 235 | |
| 236 | static int _oval_variable_model_parse_variables |
| 237 | (xmlTextReader * reader, struct oval_parser_context *context, struct oval_variable_model *model) { |
| 238 | char *tagname = (char *)xmlTextReaderLocalName(reader); |
| 239 | char *namespace = (char *)xmlTextReaderNamespaceUri(reader); |
| 240 | int return_code; |
| 241 | bool is_variable_ns = oscap_strcmp(NAMESPACE_VARIABLES, namespace) == 0; |
| 242 | if (is_variable_ns && oscap_strcmp("variable", tagname) == 0) { |
| 243 | return_code = _oval_variable_model_parse_variable(reader, context, model); |
| 244 | } else { |
| 245 | dW("Unprocessed tag: <%s:%s>.", namespace, tagname); |
| 246 | oval_parser_skip_tag(reader, context); |
| 247 | |
| 248 | /*oscap_seterr */ |
| 249 | return_code = 0; |
| 250 | } |
| 251 | free(tagname); |
| 252 | free(namespace); |
| 253 | return return_code; |
| 254 | } |
| 255 | |
| 256 | static int _oval_variable_model_parse_tag |
| 257 | (xmlTextReader * reader, struct oval_parser_context *context, struct oval_variable_model *model) { |
| 258 | char *tagname = (char *)xmlTextReaderLocalName(reader); |
| 259 | char *namespace = (char *)xmlTextReaderNamespaceUri(reader); |
| 260 | int return_code; |
| 261 | bool is_variable_ns = oscap_strcmp(NAMESPACE_VARIABLES, namespace) == 0; |
| 262 | if (is_variable_ns && oscap_strcmp("generator", tagname) == 0) { |
| 263 | struct oval_generator *gen; |
| 264 | |
| 265 | gen = oval_variable_model_get_generator(context->variable_model); |
| 266 | return_code = oval_generator_parse_tag(reader, context, gen); |
| 267 | } else if (is_variable_ns && oscap_strcmp("variables", tagname) == 0) { |
| 268 | return_code = |
| 269 | oval_parser_parse_tag(reader, context, (oval_xml_tag_parser) _oval_variable_model_parse_variables, model); |
| 270 | } else { |
| 271 | dW("Unprocessed tag: <%s:%s>.", namespace, tagname); |
| 272 | oval_parser_skip_tag(reader, context); |
| 273 | return_code = 0; |
| 274 | } |
| 275 | free(tagname); |
| 276 | free(namespace); |
| 277 | return return_code; |
| 278 | } |
| 279 | |
| 280 | static int _oval_variable_model_parse(struct oval_variable_model *model, xmlTextReader * reader, void *user_param) |
| 281 | { |
| 282 | int return_code = 0; |
| 283 | struct oval_parser_context context; |
| 284 | context.variable_model = model; |
| 285 | context.reader = reader; |
| 286 | context.user_data = user_param; |
| 287 | char *tagname = (char *)xmlTextReaderLocalName(reader); |
| 288 | char *namespace = (char *)xmlTextReaderNamespaceUri(reader); |
| 289 | bool is_variables = (oscap_strcmp(NAMESPACE_VARIABLES, namespace) == 0) && (oscap_strcmp(OVAL_ROOT_ELM_VARIABLES, tagname) == 0); |
| 290 | if (is_variables) { |
| 291 | return_code = |
| 292 | oval_parser_parse_tag(reader, &context, (oval_xml_tag_parser) _oval_variable_model_parse_tag, model); |
| 293 | } else { |
| 294 | dW("Unprocessed tag: <%s:%s>.", namespace, tagname); |
| 295 | return_code = oval_parser_skip_tag(reader, &context); |
| 296 | } |
| 297 | free(tagname); |
| 298 | free(namespace); |
| 299 | return return_code; |
| 300 | } |
| 301 | |
| 302 | struct oval_variable_model *oval_variable_model_import_source(struct oscap_source *source) |
| 303 | { |
| 304 | int ret; |
| 305 | xmlTextReader *reader = oscap_source_get_xmlTextReader(source); |
| 306 | if (reader == NULL) { |
| 307 | return NULL; |
| 308 | } |
| 309 | |
| 310 | xmlTextReaderRead(reader); |
| 311 | struct oval_variable_model *model = oval_variable_model_new(); |
| 312 | ret = _oval_variable_model_parse(model, reader, NULL); |
| 313 | if (ret != 1) { |
| 314 | oval_variable_model_free(model); |
| 315 | model = NULL; |
| 316 | } |
| 317 | xmlFreeTextReader(reader); |
| 318 | return model; |
| 319 | |
| 320 | } |
| 321 | |
| 322 | static xmlNode *oval_variable_model_to_dom(struct oval_variable_model * variable_model, |
| 323 | xmlDocPtr doc, xmlNode * parent, void *user_arg) |
| 324 | { |
| 325 | |
| 326 | xmlNodePtr root_node; |
| 327 | |
| 328 | if (parent) { |
| 329 | root_node = xmlNewTextChild(parent, NULL, BAD_CAST OVAL_ROOT_ELM_VARIABLES, NULL); |
| 330 | } else { |
| 331 | root_node = xmlNewNode(NULL, BAD_CAST OVAL_ROOT_ELM_VARIABLES); |
| 332 | xmlDocSetRootElement(doc, root_node); |
| 333 | } |
| 334 | |
| 335 | xmlNewNsProp(root_node, lookup_xsi_ns(doc), BAD_CAST "schemaLocation", BAD_CAST OVAL_VAR_SCHEMA_LOCATION); |
| 336 | |
| 337 | xmlNs *ns_common = xmlNewNs(root_node, OVAL_COMMON_NAMESPACE, BAD_CAST "oval"); |
| 338 | xmlNs *ns_variables = xmlNewNs(root_node, OVAL_VARIABLES_NAMESPACE, NULL); |
| 339 | |
| 340 | xmlSetNs(root_node, ns_common); |
| 341 | xmlSetNs(root_node, ns_variables); |
| 342 | |
| 343 | oval_generator_to_dom(variable_model->generator, doc, root_node); |
| 344 | |
| 345 | xmlNode *variables = xmlNewTextChild(root_node, ns_variables, BAD_CAST "variables", NULL); |
| 346 | |
| 347 | |
| 348 | struct oval_string_iterator *varids = oval_variable_model_get_variable_ids(variable_model); |
| 349 | while (oval_string_iterator_has_more(varids)) { |
| 350 | char *varid = oval_string_iterator_next(varids); |
| 351 | oval_datatype_t datatype = oval_variable_model_get_datatype(variable_model, varid); |
| 352 | const char *comm = oval_variable_model_get_comment(variable_model, varid); |
| 353 | |
| 354 | xmlNode *variable = xmlNewTextChild(variables, ns_variables, BAD_CAST "variable", NULL); |
| 355 | xmlNewProp(variable, BAD_CAST "id", BAD_CAST varid); |
| 356 | xmlNewProp(variable, BAD_CAST "datatype", BAD_CAST oval_datatype_get_text(datatype)); |
| 357 | xmlNewProp(variable, BAD_CAST "comment", BAD_CAST comm); |
| 358 | |
| 359 | struct oval_value_iterator *value_itr = oval_variable_model_get_values(variable_model, varid); |
| 360 | while (oval_value_iterator_has_more(value_itr)) { |
| 361 | struct oval_value *value; |
| 362 | char *text; |
| 363 | |
| 364 | value = oval_value_iterator_next(value_itr); |
| 365 | text = oval_value_get_text(value); |
| 366 | xmlNewTextChild(variable, ns_variables, BAD_CAST "value", BAD_CAST text); |
| 367 | } |
| 368 | oval_value_iterator_free(value_itr); |
| 369 | } |
| 370 | oval_string_iterator_free(varids); |
| 371 | |
| 372 | return root_node; |
| 373 | } |
| 374 | |
| 375 | int oval_variable_model_export(struct oval_variable_model *model, const char *file) |
| 376 | { |
| 377 | |
| 378 | __attribute__nonnull__(model); |
| 379 | |
| 380 | LIBXML_TEST_VERSION; |
| 381 | |
| 382 | xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0"); |
| 383 | if (doc == NULL) { |
| 384 | oscap_setxmlerr(xmlGetLastError()); |
| 385 | return -1; |
| 386 | } |
| 387 | |
| 388 | oval_variable_model_to_dom(model, doc, NULL, NULL); |
| 389 | return oscap_xml_save_filename_free(file, doc); |
| 390 | } |
| 391 | |
| 392 | bool oval_variable_model_has_variable(struct oval_variable_model *model, const char * id) |
| 393 | { |
| 394 | __attribute__nonnull__(model); |
| 395 | struct oval_string_iterator * str_it = (struct oval_string_iterator *)oval_string_map_keys(model->varmap); |
| 396 | while (oval_string_iterator_has_more(str_it)) { |
| 397 | if (!oscap_strcmp(oval_string_iterator_next(str_it), id)) { |
| 398 | oval_string_iterator_free(str_it); |
| 399 | return true; |
| 400 | } |
| 401 | } |
| 402 | oval_string_iterator_free(str_it); |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | struct oval_string_iterator *oval_variable_model_get_variable_ids(struct oval_variable_model *model) |
| 407 | { |
| 408 | __attribute__nonnull__(model); |
| 409 | return (struct oval_string_iterator *)oval_string_map_keys(model->varmap); |
| 410 | } |
| 411 | oval_datatype_t oval_variable_model_get_datatype(struct oval_variable_model *model, char *varid) |
| 412 | { |
| 413 | |
| 414 | __attribute__nonnull__(model); |
| 415 | _oval_variable_model_frame_t *frame = oval_string_map_get_value(model->varmap, varid); |
| 416 | return (frame) ? frame->datatype : OVAL_DATATYPE_UNKNOWN; |
| 417 | } |
| 418 | |
| 419 | const char *oval_variable_model_get_comment(struct oval_variable_model *model, char *varid) |
| 420 | { |
| 421 | __attribute__nonnull__(model); |
| 422 | _oval_variable_model_frame_t *frame = oval_string_map_get_value(model->varmap, varid); |
| 423 | return (frame) ? frame->comment : NULL; |
| 424 | } |
| 425 | |
| 426 | struct oval_value_iterator *oval_variable_model_get_values(struct oval_variable_model *model, char *varid) |
| 427 | { |
| 428 | __attribute__nonnull__(model); |
| 429 | _oval_variable_model_frame_t *frame = oval_string_map_get_value(model->varmap, varid); |
| 430 | return (frame) ? (struct oval_value_iterator *) oval_collection_iterator(frame->values) : NULL; |
| 431 | } |
| 432 | |
| 433 | struct oval_collection *oval_variable_model_get_values_ref(struct oval_variable_model *model, char *varid) |
| 434 | { |
| 435 | _oval_variable_model_frame_t *frame; |
| 436 | |
| 437 | frame = oval_string_map_get_value(model->varmap, varid); |
| 438 | return (frame) ? frame->values : NULL; |
| 439 | } |