| 1 | /** |
| 2 | * @file oval_resultSystem.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_agent_api_impl.h" |
| 40 | #include "results/oval_results_impl.h" |
| 41 | #include "adt/oval_collection_impl.h" |
| 42 | #include "public/oval_agent_api.h" |
| 43 | #include "common/util.h" |
| 44 | #include "common/debug_priv.h" |
| 45 | |
| 46 | typedef struct oval_result_definition { |
| 47 | struct oval_definition *definition; |
| 48 | oval_result_t result; |
| 49 | struct oval_result_system *system; |
| 50 | struct oval_result_criteria_node *criteria; |
| 51 | struct oval_collection *messages; |
| 52 | int instance; |
| 53 | int variable_instance_hint; ///< A next possible variable_instance attribute |
| 54 | } oval_result_definition_t; |
| 55 | |
| 56 | struct oval_result_definition *oval_result_definition_new(struct oval_result_system *sys, char *definition_id) { |
| 57 | oval_result_definition_t *definition = (oval_result_definition_t *) |
| 58 | malloc(sizeof(oval_result_definition_t)); |
| 59 | if (definition == NULL) |
| 60 | return NULL; |
| 61 | |
| 62 | definition->system = sys; |
| 63 | struct oval_syschar_model *syschar_model = oval_result_system_get_syschar_model(sys); |
| 64 | struct oval_definition_model *definition_model = oval_syschar_model_get_definition_model(syschar_model); |
| 65 | definition->definition = oval_definition_model_get_new_definition(definition_model, definition_id); |
| 66 | definition->result = OVAL_RESULT_NOT_EVALUATED; |
| 67 | definition->criteria = NULL; |
| 68 | definition->messages = oval_collection_new(); |
| 69 | definition->variable_instance_hint = 1; |
| 70 | definition->instance = 1; |
| 71 | return definition; |
| 72 | } |
| 73 | |
| 74 | struct oval_result_definition *oval_result_definition_clone |
| 75 | (struct oval_result_system *new_system, struct oval_result_definition *old_definition) { |
| 76 | const char *id = oval_result_definition_get_id(old_definition); |
| 77 | struct oval_result_definition *new_definition = oval_result_definition_new(new_system, (char *) id); |
| 78 | |
| 79 | struct oval_result_criteria_node *old_crit = oval_result_definition_get_criteria(old_definition); |
| 80 | if (old_crit) { |
| 81 | struct oval_result_criteria_node *new_crit = oval_result_criteria_node_clone(new_system, old_crit); |
| 82 | oval_result_definition_set_criteria(new_definition, new_crit); |
| 83 | } |
| 84 | |
| 85 | struct oval_message_iterator *old_messages = oval_result_definition_get_messages(old_definition); |
| 86 | while (oval_message_iterator_has_more(old_messages)) { |
| 87 | struct oval_message *old_message = oval_message_iterator_next(old_messages); |
| 88 | struct oval_message *new_message = oval_message_clone(old_message); |
| 89 | oval_result_definition_add_message(new_definition, new_message); |
| 90 | } |
| 91 | oval_message_iterator_free(old_messages); |
| 92 | |
| 93 | oval_result_definition_set_result(new_definition, oval_result_definition_get_result(old_definition)); |
| 94 | oval_result_definition_set_instance(new_definition, oval_result_definition_get_instance(old_definition)); |
| 95 | oval_result_definition_set_variable_instance_hint(new_definition, oval_result_definition_get_variable_instance_hint(old_definition)); |
| 96 | return new_definition; |
| 97 | } |
| 98 | |
| 99 | void oval_result_definition_free(struct oval_result_definition *definition) |
| 100 | { |
| 101 | __attribute__nonnull__(definition); |
| 102 | |
| 103 | if (definition->criteria) |
| 104 | oval_result_criteria_node_free(definition->criteria); |
| 105 | oval_collection_free_items(definition->messages, (oscap_destruct_func) oval_message_free); |
| 106 | |
| 107 | definition->system = NULL; |
| 108 | definition->criteria = NULL; |
| 109 | definition->definition = NULL; |
| 110 | definition->messages = NULL; |
| 111 | definition->result = OVAL_RESULT_NOT_EVALUATED; |
| 112 | definition->instance = 1; |
| 113 | free(definition); |
| 114 | } |
| 115 | |
| 116 | struct oval_result_definition *make_result_definition_from_oval_definition(struct oval_result_system *sys, struct oval_definition *oval_definition, struct oscap_list *visited_definitions, int variable_instance) |
| 117 | { |
| 118 | char *defid = oval_definition_get_id(oval_definition); |
| 119 | if (visited_definitions != NULL) { |
| 120 | if (oscap_list_contains(visited_definitions, defid, (oscap_cmp_func) oscap_streq)) { |
| 121 | dE("Circular dependency in OVAL definition '%s'.", defid); |
| 122 | return NULL; |
| 123 | } |
| 124 | oscap_list_add(visited_definitions, strdup(defid)); |
| 125 | } |
| 126 | struct oval_result_definition *rslt_definition = oval_result_definition_new(sys, defid); |
| 127 | oval_result_definition_set_instance(rslt_definition, variable_instance); |
| 128 | struct oval_criteria_node *oval_criteria = oval_definition_get_criteria(oval_definition); |
| 129 | struct oval_result_criteria_node *rslt_criteria = |
| 130 | make_result_criteria_node_from_oval_criteria_node(sys, oval_criteria, visited_definitions, variable_instance); |
| 131 | if (rslt_criteria) |
| 132 | oval_result_definition_set_criteria(rslt_definition, rslt_criteria); |
| 133 | return rslt_definition; |
| 134 | } |
| 135 | |
| 136 | struct oval_definition *oval_result_definition_get_definition(const struct oval_result_definition *definition) { |
| 137 | __attribute__nonnull__(definition); |
| 138 | |
| 139 | return definition->definition; |
| 140 | } |
| 141 | |
| 142 | struct oval_result_system *oval_result_definition_get_system(const struct oval_result_definition *definition) { |
| 143 | __attribute__nonnull__(definition); |
| 144 | |
| 145 | return definition->system; |
| 146 | } |
| 147 | |
| 148 | int oval_result_definition_get_instance(const struct oval_result_definition *definition) { |
| 149 | __attribute__nonnull__(definition); |
| 150 | |
| 151 | return definition->instance; |
| 152 | } |
| 153 | |
| 154 | oval_result_t oval_result_definition_eval(struct oval_result_definition * definition) |
| 155 | { |
| 156 | __attribute__nonnull__(definition); |
| 157 | const char *id = oval_result_definition_get_id(definition); |
| 158 | const char *title = oval_definition_get_title(oval_result_definition_get_definition(definition)); |
| 159 | dI("Evaluating definition '%s': %s.", id, title); |
| 160 | |
| 161 | if (definition->result == OVAL_RESULT_NOT_EVALUATED) { |
| 162 | struct oval_result_criteria_node *criteria = oval_result_definition_get_criteria(definition); |
| 163 | if (criteria != NULL) { |
| 164 | dIndent(1); |
| 165 | definition->result = oval_result_criteria_node_eval(criteria); |
| 166 | dIndent(-1); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | dI("Definition '%s' evaluated as %s.", id, oval_result_get_text(definition->result)); |
| 171 | return definition->result; |
| 172 | } |
| 173 | |
| 174 | oval_result_t oval_result_definition_get_result(const struct oval_result_definition * definition) |
| 175 | { |
| 176 | __attribute__nonnull__(definition); |
| 177 | |
| 178 | return definition->result; |
| 179 | } |
| 180 | |
| 181 | struct oval_message_iterator *oval_result_definition_get_messages(const struct oval_result_definition *definition) |
| 182 | { |
| 183 | __attribute__nonnull__(definition); |
| 184 | |
| 185 | return (struct oval_message_iterator *) |
| 186 | oval_collection_iterator(definition->messages); |
| 187 | } |
| 188 | |
| 189 | struct oval_result_criteria_node *oval_result_definition_get_criteria(const struct oval_result_definition *definition) |
| 190 | { |
| 191 | __attribute__nonnull__(definition); |
| 192 | |
| 193 | return definition->criteria; |
| 194 | } |
| 195 | |
| 196 | void oval_result_definition_set_result(struct oval_result_definition *definition, oval_result_t result) |
| 197 | { |
| 198 | __attribute__nonnull__(definition); |
| 199 | definition->result = result; |
| 200 | } |
| 201 | |
| 202 | void oval_result_definition_set_instance(struct oval_result_definition *definition, int instance) |
| 203 | { |
| 204 | __attribute__nonnull__(definition); |
| 205 | definition->instance = instance; |
| 206 | // When a new variable_instance is set, we usually want to reset the hint |
| 207 | definition->variable_instance_hint = instance; |
| 208 | } |
| 209 | |
| 210 | void oval_result_definition_set_criteria(struct oval_result_definition *definition, struct oval_result_criteria_node *criteria) |
| 211 | { |
| 212 | __attribute__nonnull__(definition); |
| 213 | if (definition->criteria) { |
| 214 | if (oval_result_criteria_node_get_type(criteria) == OVAL_NODETYPE_CRITERIA) { |
| 215 | oval_result_criteria_node_free(definition->criteria); |
| 216 | } |
| 217 | } |
| 218 | definition->criteria = criteria; |
| 219 | } |
| 220 | |
| 221 | void oval_result_definition_add_message(struct oval_result_definition *definition, struct oval_message *message) { |
| 222 | __attribute__nonnull__(definition); |
| 223 | if (message) |
| 224 | oval_collection_add(definition->messages, message); |
| 225 | } |
| 226 | |
| 227 | static void _oval_result_definition_consume_criteria(struct oval_result_criteria_node *node, struct oval_result_definition *definition) { |
| 228 | oval_result_definition_set_criteria(definition, node); |
| 229 | } |
| 230 | |
| 231 | static void _oval_result_definition_consume_message(struct oval_message *message, struct oval_result_definition *definition) { |
| 232 | oval_result_definition_add_message(definition, message); |
| 233 | } |
| 234 | |
| 235 | static int oval_result_definition_parse(xmlTextReaderPtr reader, struct oval_parser_context *context, void *usr) { |
| 236 | int return_code = 0; |
| 237 | xmlChar *localName = xmlTextReaderLocalName(reader); |
| 238 | struct oval_result_system *sys = oval_result_definition_get_system((struct oval_result_definition *) usr); |
| 239 | |
| 240 | if (strcmp((const char *)localName, "criteria") == 0) { |
| 241 | return_code = oval_result_criteria_node_parse |
| 242 | (reader, context, sys, (oscap_consumer_func) _oval_result_definition_consume_criteria, usr); |
| 243 | } else if (strcmp((const char *)localName, "message") == 0) { |
| 244 | return_code = oval_message_parse_tag |
| 245 | (reader, context, (oscap_consumer_func) _oval_result_definition_consume_message, usr); |
| 246 | } |
| 247 | free(localName); |
| 248 | return return_code; |
| 249 | } |
| 250 | |
| 251 | int oval_result_definition_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *usr) { |
| 252 | |
| 253 | struct oval_result_system *sys = (struct oval_result_system *) usr; |
| 254 | int return_code = 0; |
| 255 | struct oval_definition_model *dmod; |
| 256 | struct oval_definition *ddef; |
| 257 | struct oval_result_definition *definition; |
| 258 | xmlChar *definition_id = xmlTextReaderGetAttribute(reader, BAD_CAST "definition_id"); |
| 259 | xmlChar *definition_version = xmlTextReaderGetAttribute(reader, BAD_CAST "version"); |
| 260 | int resvsn = atoi((char *)definition_version); |
| 261 | |
| 262 | oval_result_t result = oval_result_parse(reader, "result", OVAL_ENUMERATION_INVALID); |
| 263 | |
| 264 | int instance = oval_parser_int_attribute(reader, "variable_instance", 1); |
| 265 | |
| 266 | dmod = context->definition_model; |
| 267 | ddef = oval_definition_model_get_new_definition(dmod, (char *) definition_id); |
| 268 | definition = oval_result_system_get_new_definition(sys, ddef, instance); |
| 269 | if (definition == NULL) |
| 270 | return -1; |
| 271 | |
| 272 | int defvsn = oval_definition_get_version(definition->definition); |
| 273 | if (defvsn && resvsn != defvsn) { |
| 274 | dW("Definition versions don't match: definition id: %s, ovaldef vsn: %d, resdef vsn: %d.", definition_id, defvsn, resvsn); |
| 275 | } |
| 276 | oval_definition_set_version(definition->definition, resvsn); |
| 277 | // The following _set_instance() might be overabundant, since it should be already set |
| 278 | // by oval_result_system_get_new_definition() Let's see if the assert agrees over time: |
| 279 | assert(oval_result_definition_get_instance(definition) == instance); |
| 280 | oval_result_definition_set_instance(definition, instance); |
| 281 | |
| 282 | |
| 283 | if ((int)result != OVAL_ENUMERATION_INVALID) { |
| 284 | oval_result_definition_set_result(definition, result); |
| 285 | } else { |
| 286 | dW("Can't resolve result attribute, definition id: %s.", definition_id); |
| 287 | oval_result_definition_set_result(definition, OVAL_RESULT_UNKNOWN); |
| 288 | } |
| 289 | |
| 290 | return_code = oval_parser_parse_tag(reader, context, oval_result_definition_parse, definition); |
| 291 | |
| 292 | free(definition_id); |
| 293 | free(definition_version); |
| 294 | |
| 295 | return return_code; |
| 296 | } |
| 297 | |
| 298 | xmlNode *oval_result_definition_to_dom |
| 299 | (struct oval_result_definition * definition, oval_result_directive_content_t content, |
| 300 | xmlDocPtr doc, xmlNode * parent) { |
| 301 | xmlNs *ns_results = xmlSearchNsByHref(doc, parent, OVAL_RESULTS_NAMESPACE); |
| 302 | xmlNode *definition_node = xmlNewTextChild(parent, ns_results, BAD_CAST "definition", NULL); |
| 303 | |
| 304 | struct oval_definition *oval_definition = oval_result_definition_get_definition(definition); |
| 305 | char *definition_id = oval_definition_get_id(oval_definition); |
| 306 | xmlNewProp(definition_node, BAD_CAST "definition_id", BAD_CAST definition_id); |
| 307 | |
| 308 | oval_result_t result = oval_result_definition_get_result(definition); |
| 309 | const char *result_att = oval_result_get_text(result); |
| 310 | xmlNewProp(definition_node, BAD_CAST "result", BAD_CAST result_att); |
| 311 | |
| 312 | int version = oval_definition_get_version(oval_definition); |
| 313 | char version_att[10] = ""; |
| 314 | snprintf(version_att, sizeof(version_att), "%d", version); |
| 315 | xmlNewProp(definition_node, BAD_CAST "version", BAD_CAST version_att); |
| 316 | |
| 317 | int instance = oval_result_definition_get_instance(definition); |
| 318 | if (instance != 1 || |
| 319 | oval_result_definition_get_variable_instance_hint(definition) != instance) { |
| 320 | char instance_att[10] = ""; |
| 321 | snprintf(instance_att, sizeof(instance_att), "%d", instance); |
| 322 | xmlNewProp(definition_node, BAD_CAST "variable_instance", BAD_CAST instance_att); |
| 323 | } |
| 324 | |
| 325 | struct oval_message_iterator *messages = oval_result_definition_get_messages(definition); |
| 326 | while (oval_message_iterator_has_more(messages)) { |
| 327 | oval_message_to_dom(oval_message_iterator_next(messages), doc, definition_node); |
| 328 | } |
| 329 | oval_message_iterator_free(messages); |
| 330 | |
| 331 | if (content == OVAL_DIRECTIVE_CONTENT_FULL) { |
| 332 | struct oval_result_criteria_node *criteria = oval_result_definition_get_criteria(definition); |
| 333 | if (criteria) { |
| 334 | oval_result_criteria_node_to_dom(criteria, doc, definition_node); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | return definition_node; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Get the value of the variable_instance hint. |
| 343 | * @memberof oval_result_definition |
| 344 | */ |
| 345 | int oval_result_definition_get_variable_instance_hint(const struct oval_result_definition *definition) |
| 346 | { |
| 347 | return definition->variable_instance_hint; |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * This sets counter on next possible variable instance. The hint is later used |
| 352 | * to decide whether instanciate a new result_definition, and what variable_instance |
| 353 | * attribute assign to it. |
| 354 | * @memberof oval_result_definition |
| 355 | */ |
| 356 | void oval_result_definition_set_variable_instance_hint(struct oval_result_definition *definition, int new_hint_value) |
| 357 | { |
| 358 | definition->variable_instance_hint = new_hint_value; |
| 359 | } |
| 360 | |
| 361 | const char *oval_result_definition_get_id(const struct oval_result_definition *rslt_definition) |
| 362 | { |
| 363 | __attribute__nonnull__(rslt_definition); |
| 364 | struct oval_definition *def = oval_result_definition_get_definition(rslt_definition); |
| 365 | return (def == NULL) ? NULL : oval_definition_get_id(def); |
| 366 | } |