| 1 | /** |
| 2 | * @file oval_agent.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 | * "Peter Vrabec" <[email protected]> |
| 28 | * Šimon Lukašík |
| 29 | */ |
| 30 | |
| 31 | #ifdef HAVE_CONFIG_H |
| 32 | #include <config.h> |
| 33 | #endif |
| 34 | |
| 35 | #include <string.h> |
| 36 | #include <time.h> |
| 37 | |
| 38 | #include "oval_agent_api.h" |
| 39 | #include "oval_definitions_impl.h" |
| 40 | #include "oval_agent_api_impl.h" |
| 41 | #include "oval_parser_impl.h" |
| 42 | #include "adt/oval_string_map_impl.h" |
| 43 | #include "oval_system_characteristics_impl.h" |
| 44 | #include "results/oval_results_impl.h" |
| 45 | #if defined(OVAL_PROBES_ENABLED) |
| 46 | # include "oval_probe_impl.h" |
| 47 | #endif |
| 48 | #include "common/list.h" |
| 49 | #include "common/util.h" |
| 50 | #include "common/debug_priv.h" |
| 51 | #include "common/_error.h" |
| 52 | #include "oval_agent_xccdf_api.h" |
| 53 | |
| 54 | struct oval_agent_session { |
| 55 | char *filename; |
| 56 | char *product_name; |
| 57 | |
| 58 | struct oval_definition_model * def_model; |
| 59 | struct oval_variable_model *cur_var_model; |
| 60 | struct oval_syschar_model * sys_model; |
| 61 | struct oval_syschar_model * sys_models[2]; |
| 62 | #if defined(OVAL_PROBES_ENABLED) |
| 63 | struct oval_results_model * res_model; |
| 64 | oval_probe_session_t * psess; |
| 65 | #endif |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * Specification of structure for transformation of OVAL Result type |
| 71 | * to XCCDF result type. |
| 72 | */ |
| 73 | struct oval_result_to_xccdf_spec { |
| 74 | oval_result_t oval; |
| 75 | xccdf_test_result_type_t xccdf; |
| 76 | xccdf_test_result_type_t reversed_xccdf; |
| 77 | }; |
| 78 | |
| 79 | /** |
| 80 | * Array of transformation rules from OVAL Result type to XCCDF result type |
| 81 | */ |
| 82 | static const struct oval_result_to_xccdf_spec XCCDF_OVAL_RESULTS_MAP[] = { |
| 83 | {OVAL_RESULT_TRUE, XCCDF_RESULT_PASS, XCCDF_RESULT_FAIL}, |
| 84 | {OVAL_RESULT_FALSE, XCCDF_RESULT_FAIL, XCCDF_RESULT_PASS}, |
| 85 | {OVAL_RESULT_UNKNOWN, XCCDF_RESULT_UNKNOWN, XCCDF_RESULT_UNKNOWN}, |
| 86 | {OVAL_RESULT_ERROR, XCCDF_RESULT_ERROR, XCCDF_RESULT_ERROR}, |
| 87 | {OVAL_RESULT_NOT_EVALUATED, XCCDF_RESULT_NOT_CHECKED, XCCDF_RESULT_NOT_CHECKED}, |
| 88 | {OVAL_RESULT_NOT_APPLICABLE, XCCDF_RESULT_NOT_APPLICABLE, XCCDF_RESULT_NOT_APPLICABLE}, |
| 89 | {0, 0, 0} |
| 90 | }; |
| 91 | |
| 92 | oval_agent_session_t * oval_agent_new_session(struct oval_definition_model *model, const char * name) { |
| 93 | struct oval_sysinfo *sysinfo; |
| 94 | struct oval_generator *generator; |
| 95 | int ret; |
| 96 | |
| 97 | dI("Started new OVAL agent %s.", name); |
| 98 | |
| 99 | /* Optimalization */ |
| 100 | oval_definition_model_optimize_by_filter_propagation(model); |
| 101 | |
| 102 | oval_agent_session_t *ag_sess = malloc(sizeof(oval_agent_session_t)); |
| 103 | ag_sess->filename = oscap_strdup(name); |
| 104 | ag_sess->def_model = model; |
| 105 | ag_sess->cur_var_model = NULL; |
| 106 | ag_sess->sys_model = oval_syschar_model_new(model); |
| 107 | #if defined(OVAL_PROBES_ENABLED) |
| 108 | ag_sess->psess = oval_probe_session_new(ag_sess->sys_model); |
| 109 | #endif |
| 110 | |
| 111 | #if defined(OVAL_PROBES_ENABLED) |
| 112 | /* probe sysinfo */ |
| 113 | ret = oval_probe_query_sysinfo(ag_sess->psess, &sysinfo); |
| 114 | if (ret != 0) { |
| 115 | free(ag_sess->filename); |
| 116 | oval_probe_session_destroy(ag_sess->psess); |
| 117 | oval_syschar_model_free(ag_sess->sys_model); |
| 118 | free(ag_sess); |
| 119 | return NULL; |
| 120 | } |
| 121 | #else |
| 122 | /* TODO */ |
| 123 | sysinfo = oval_sysinfo_new(ag_sess->sys_model); |
| 124 | #endif /* OVAL_PROBES_ENABLED */ |
| 125 | oval_syschar_model_set_sysinfo(ag_sess->sys_model, sysinfo); |
| 126 | oval_sysinfo_free(sysinfo); |
| 127 | |
| 128 | /* one system only */ |
| 129 | ag_sess->sys_models[0] = ag_sess->sys_model; |
| 130 | ag_sess->sys_models[1] = NULL; |
| 131 | #if defined(OVAL_PROBES_ENABLED) |
| 132 | ag_sess->res_model = oval_results_model_new_with_probe_session( |
| 133 | model, ag_sess->sys_models, ag_sess->psess); |
| 134 | generator = oval_results_model_get_generator(ag_sess->res_model); |
| 135 | oval_generator_set_product_version(generator, oscap_get_version()); |
| 136 | #endif |
| 137 | |
| 138 | ag_sess->product_name = NULL; |
| 139 | |
| 140 | return ag_sess; |
| 141 | } |
| 142 | |
| 143 | struct oval_definition_model* oval_agent_get_definition_model(oval_agent_session_t* ag_sess) |
| 144 | { |
| 145 | return ag_sess->def_model; |
| 146 | } |
| 147 | |
| 148 | void oval_agent_set_product_name(oval_agent_session_t *ag_sess, char * product_name) |
| 149 | { |
| 150 | struct oval_generator *generator; |
| 151 | ag_sess->product_name = oscap_strdup(product_name); |
| 152 | |
| 153 | generator = oval_syschar_model_get_generator(ag_sess->sys_models[0]); |
| 154 | oval_generator_set_product_name(generator, product_name); |
| 155 | |
| 156 | #if defined(OVAL_PROBES_ENABLED) |
| 157 | generator = oval_results_model_get_generator(ag_sess->res_model); |
| 158 | oval_generator_set_product_name(generator, product_name); |
| 159 | #endif |
| 160 | } |
| 161 | |
| 162 | static struct oval_result_system *_oval_agent_get_first_result_system(oval_agent_session_t *ag_sess) |
| 163 | { |
| 164 | struct oval_results_model *rmodel = oval_agent_get_results_model(ag_sess); |
| 165 | struct oval_result_system_iterator *rsystem_it = oval_results_model_get_systems(rmodel); |
| 166 | struct oval_result_system *rsystem = NULL; |
| 167 | if (oval_result_system_iterator_has_more(rsystem_it)) { |
| 168 | rsystem = oval_result_system_iterator_next(rsystem_it); |
| 169 | } |
| 170 | oval_result_system_iterator_free(rsystem_it); |
| 171 | return rsystem; |
| 172 | } |
| 173 | |
| 174 | int oval_agent_eval_definition(oval_agent_session_t *ag_sess, const char *id) |
| 175 | { |
| 176 | #if defined(OVAL_PROBES_ENABLED) |
| 177 | int ret; |
| 178 | struct oval_result_system *rsystem; |
| 179 | |
| 180 | rsystem = _oval_agent_get_first_result_system(ag_sess); |
| 181 | /* eval */ |
| 182 | ret = oval_result_system_eval_definition(rsystem, id); |
| 183 | return ret; |
| 184 | #else |
| 185 | /* TODO */ |
| 186 | return -1; |
| 187 | #endif |
| 188 | } |
| 189 | |
| 190 | int oval_agent_get_definition_result(oval_agent_session_t *ag_sess, const char *id, oval_result_t * result) |
| 191 | { |
| 192 | struct oval_result_system *rsystem; |
| 193 | struct oval_result_definition *rdef; |
| 194 | |
| 195 | rsystem = _oval_agent_get_first_result_system(ag_sess); |
| 196 | if (rsystem == NULL) { |
| 197 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "No results system in agent session."); |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | rdef = oval_result_system_get_definition(rsystem, id); |
| 202 | if (rdef == NULL) { |
| 203 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "No definition with ID: %s in result model.", id); |
| 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | *result = oval_result_definition_get_result(rdef); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | struct oval_result_definition * oval_agent_get_result_definition(oval_agent_session_t *ag_sess, const char *id) { |
| 213 | struct oval_result_system *rsystem; |
| 214 | struct oval_result_definition *rdef; |
| 215 | |
| 216 | rsystem = _oval_agent_get_first_result_system(ag_sess); |
| 217 | if (rsystem == NULL) |
| 218 | return NULL; |
| 219 | |
| 220 | rdef = oval_result_system_get_definition(rsystem, id); |
| 221 | |
| 222 | return rdef; |
| 223 | } |
| 224 | |
| 225 | int oval_agent_reset_session(oval_agent_session_t * ag_sess) { |
| 226 | ag_sess->cur_var_model = NULL; |
| 227 | oval_definition_model_clear_external_variables(ag_sess->def_model); |
| 228 | |
| 229 | /* We intentionally do not flush out the results model which should |
| 230 | * be able to encompass results from multiple evaluations */ |
| 231 | |
| 232 | /* Here we temporarily do not flush out the system characteristics model |
| 233 | * because there might be tests in the results model which refer to this |
| 234 | * sys char (via @tested_item attribute). Later we will need a mechanism |
| 235 | * to selectivelly invalidate some of the system characteristics cached |
| 236 | * objects (similar to the concept of variable_instance_hint from the |
| 237 | * results model. Hooray corner cases! */ |
| 238 | //oval_syschar_model_reset(ag_sess->sys_model); |
| 239 | |
| 240 | #if defined(OVAL_PROBES_ENABLED) |
| 241 | /* Apply product name to new results_model */ |
| 242 | if (ag_sess->product_name) { |
| 243 | struct oval_generator *generator; |
| 244 | |
| 245 | generator = oval_results_model_get_generator(ag_sess->res_model); |
| 246 | oval_generator_set_product_name(generator, ag_sess->product_name); |
| 247 | } |
| 248 | |
| 249 | /* We have to reset probe_session inplace, because |
| 250 | * ag_sess->res_model points to old probe_session |
| 251 | * and we are not able to update the reference clearly */ |
| 252 | oval_probe_session_reinit(ag_sess->psess, ag_sess->sys_model); |
| 253 | #endif |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | void oval_agent_reset_syschar(oval_agent_session_t * ag_sess) { |
| 259 | oval_syschar_model_reset(ag_sess->sys_model); |
| 260 | } |
| 261 | |
| 262 | void oval_agent_reset_results(oval_agent_session_t * ag_sess) { |
| 263 | #if defined(OVAL_PROBES_ENABLED) |
| 264 | if (ag_sess != NULL) { |
| 265 | oval_results_model_free(ag_sess->res_model); |
| 266 | ag_sess->res_model = oval_results_model_new_with_probe_session( |
| 267 | ag_sess->def_model, ag_sess->sys_models, ag_sess->psess); |
| 268 | oval_probe_session_reinit(ag_sess->psess, ag_sess->sys_model); |
| 269 | } |
| 270 | #endif |
| 271 | } |
| 272 | |
| 273 | int oval_agent_abort_session(oval_agent_session_t *ag_sess) |
| 274 | { |
| 275 | if (ag_sess == NULL) { |
| 276 | return -1; |
| 277 | } |
| 278 | #if defined(OVAL_PROBES_ENABLED) |
| 279 | if (ag_sess->psess == NULL) { |
| 280 | return -1; |
| 281 | } |
| 282 | return oval_probe_session_abort(ag_sess->psess); |
| 283 | #else |
| 284 | /* TODO */ |
| 285 | return 0; |
| 286 | #endif |
| 287 | } |
| 288 | |
| 289 | int oval_agent_eval_system(oval_agent_session_t * ag_sess, agent_reporter cb, void *arg) { |
| 290 | struct oval_definition *oval_def; |
| 291 | struct oval_definition_iterator *oval_def_it; |
| 292 | char *id; |
| 293 | int ret = 0; |
| 294 | |
| 295 | dI("OVAL agent started to evaluate OVAL definitions on your system."); |
| 296 | oval_def_it = oval_definition_model_get_definitions(ag_sess->def_model); |
| 297 | while (oval_definition_iterator_has_more(oval_def_it)) { |
| 298 | oval_def = oval_definition_iterator_next(oval_def_it); |
| 299 | id = oval_definition_get_id(oval_def); |
| 300 | |
| 301 | /* probe and eval */ |
| 302 | ret = oval_agent_eval_definition(ag_sess, id); |
| 303 | if (ret==-1) { |
| 304 | goto cleanup; |
| 305 | } |
| 306 | |
| 307 | /* callback */ |
| 308 | if (cb != NULL) { |
| 309 | struct oval_result_definition * res_def = oval_agent_get_result_definition(ag_sess, id); |
| 310 | ret = cb(res_def,arg); |
| 311 | /* stop? */ |
| 312 | if (ret!=0) |
| 313 | goto cleanup; |
| 314 | } |
| 315 | |
| 316 | /* probe evaluation terminated by signal */ |
| 317 | if (ret == -2) { |
| 318 | ret = 1; |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | cleanup: |
| 324 | oval_definition_iterator_free(oval_def_it); |
| 325 | dI("OVAL agent finished evaluation."); |
| 326 | return ret; |
| 327 | } |
| 328 | |
| 329 | struct oval_results_model * oval_agent_get_results_model(oval_agent_session_t * ag_sess) { |
| 330 | __attribute__nonnull__(ag_sess); |
| 331 | |
| 332 | #if defined(OVAL_PROBES_ENABLED) |
| 333 | return ag_sess->res_model; |
| 334 | #else |
| 335 | return NULL; |
| 336 | #endif |
| 337 | } |
| 338 | |
| 339 | const char * oval_agent_get_filename(oval_agent_session_t * ag_sess) { |
| 340 | __attribute__nonnull__(ag_sess); |
| 341 | |
| 342 | return ag_sess->filename; |
| 343 | } |
| 344 | |
| 345 | void oval_agent_destroy_session(oval_agent_session_t * ag_sess) { |
| 346 | if (ag_sess != NULL) { |
| 347 | free(ag_sess->product_name); |
| 348 | #if defined(OVAL_PROBES_ENABLED) |
| 349 | oval_probe_session_destroy(ag_sess->psess); |
| 350 | oval_results_model_free(ag_sess->res_model); |
| 351 | #endif |
| 352 | oval_syschar_model_free(ag_sess->sys_model); |
| 353 | free(ag_sess->filename); |
| 354 | free(ag_sess); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | |
| 359 | /** |
| 360 | * Function for OVAL Result type -> XCCDF result type transformation |
| 361 | * @param id OVAL_RESULT_* type |
| 362 | * @return xccdf_test_result_type_t |
| 363 | */ |
| 364 | static xccdf_test_result_type_t xccdf_get_result_from_oval(oval_definition_class_t class, oval_result_t id) |
| 365 | { |
| 366 | |
| 367 | const struct oval_result_to_xccdf_spec *mapptr; |
| 368 | |
| 369 | for (mapptr = XCCDF_OVAL_RESULTS_MAP; mapptr->oval != 0; ++mapptr) { |
| 370 | if (id == mapptr->oval) |
| 371 | // SP800-126r2: Deriving XCCDF Check Results from OVAL Definition Results |
| 372 | return (class == OVAL_CLASS_VULNERABILITY || class == OVAL_CLASS_PATCH) ? mapptr->reversed_xccdf : mapptr->xccdf; |
| 373 | } |
| 374 | |
| 375 | return XCCDF_RESULT_UNKNOWN; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Transform the value_bindings to intermediary mapping. |
| 380 | * @param it XCCDF value binding iterator |
| 381 | * @return dict structure which for each OVAL variable name contains a list of binded values |
| 382 | */ |
| 383 | static struct oscap_htable *_binding_iterator_to_dict(struct xccdf_value_binding_iterator *it) |
| 384 | { |
| 385 | struct oscap_htable *dict = oscap_htable_new(); |
| 386 | while (xccdf_value_binding_iterator_has_more(it)) { |
| 387 | struct xccdf_value_binding *binding = xccdf_value_binding_iterator_next(it); |
| 388 | const char *var_name = xccdf_value_binding_get_name(binding); |
| 389 | const char *var_val = xccdf_value_binding_get_setvalue(binding); |
| 390 | if (var_val == NULL) { |
| 391 | var_val = xccdf_value_binding_get_value(binding); |
| 392 | if (var_val == NULL) { |
| 393 | var_val = ""; |
| 394 | } |
| 395 | } |
| 396 | struct oscap_stringlist *list = (struct oscap_stringlist *) oscap_htable_get(dict, var_name); |
| 397 | if (list == NULL) { |
| 398 | list = oscap_stringlist_new(); |
| 399 | oscap_htable_add(dict, var_name, list); |
| 400 | } |
| 401 | |
| 402 | if (!oscap_list_contains((struct oscap_list *) list, (char *) var_val, (oscap_cmp_func) oscap_streq)) { |
| 403 | oscap_stringlist_add_string(list, var_val); |
| 404 | } |
| 405 | } |
| 406 | xccdf_value_binding_iterator_reset(it); |
| 407 | return dict; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Ensure that the newly binded values do not pose conflict with existing value set. |
| 412 | * The lists should be equal or the existing must be empty. |
| 413 | */ |
| 414 | static bool _stringlist_conflicts_with_value_it(struct oscap_stringlist *slist, struct oval_value_iterator *val_it) |
| 415 | { |
| 416 | int multival_count = 0; |
| 417 | if (!oval_value_iterator_has_more(val_it)) |
| 418 | return false; |
| 419 | while (oval_value_iterator_has_more(val_it)) { |
| 420 | struct oval_value *o_value = oval_value_iterator_next(val_it); |
| 421 | char *o_value_text = oval_value_get_text(o_value); |
| 422 | |
| 423 | if (!oscap_list_contains((struct oscap_list *) slist, o_value_text, (oscap_cmp_func) oscap_streq)) |
| 424 | return true; |
| 425 | multival_count++; |
| 426 | } |
| 427 | // It is conflict if the numbers do not match |
| 428 | return oscap_list_get_itemcount((struct oscap_list *) slist) != multival_count; |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Finds out, if the new batch of variable bindings compel new variable model |
| 433 | * (so-called multiset). Creates new variable model if needed. |
| 434 | */ |
| 435 | static void _oval_agent_resolve_variables_conflict(struct oval_agent_session *session, struct xccdf_value_binding_iterator *it) |
| 436 | { |
| 437 | const char *var_name = NULL; |
| 438 | struct oscap_stringlist *value_list = NULL; |
| 439 | bool conflict = false; |
| 440 | struct oscap_htable *dict = _binding_iterator_to_dict(it); |
| 441 | struct oscap_htable_iterator *hit = oscap_htable_iterator_new(dict); |
| 442 | struct oval_definition_model *def_model = |
| 443 | oval_results_model_get_definition_model(oval_agent_get_results_model(session)); |
| 444 | while (!conflict && oscap_htable_iterator_has_more(hit)) { |
| 445 | oscap_htable_iterator_next_kv(hit, &var_name, (void*) &value_list); |
| 446 | struct oval_variable *variable = oval_definition_model_get_variable(def_model, var_name); |
| 447 | if (variable != NULL) { |
| 448 | struct oval_value_iterator *value_it = oval_variable_get_values(variable); |
| 449 | if (_stringlist_conflicts_with_value_it(value_list, value_it)) { |
| 450 | // You don't want to touch this code. There are also other means to waste your |
| 451 | // life. Now please proceed by reading the previous line again. |
| 452 | // |
| 453 | // Now, we have found that the variable we are trying to bind into the session |
| 454 | // is already there with different values. This is the rise of concept which |
| 455 | // is often referred as variable_instance or simply multiset. |
| 456 | // |
| 457 | // As per OVAL 5.10.1, the Variable Schema does not allow multisets. Therefore, |
| 458 | // we will later create new variable model and export multiple variables docs. |
| 459 | conflict = true; |
| 460 | // Next, in the results model, there might be already some definitions, tests |
| 461 | // states, or objects. These might be dependent on the previous value of the |
| 462 | // given variable. |
| 463 | // |
| 464 | // The 'latest' result-definition for each such definition (whose result depends |
| 465 | // on the value) needs to be marked by 'variable_instance_hint'. The hint has |
| 466 | // meaning that any possible future evaluation of the given definition needs |
| 467 | // to create new result-definition and not re-use the old one. |
| 468 | // |
| 469 | // Both (or all) such result-definitions are then distinguished by different |
| 470 | // @variable_instance attribute. And each result-definition refers to different |
| 471 | // set of tests. These tests might have same @id but differ in @variable_instance |
| 472 | // attribute. Further, some of these tests will differ in tested_variable element. |
| 473 | struct oval_result_system *r_system = _oval_agent_get_first_result_system(session); |
| 474 | if (r_system == NULL) { |
| 475 | oval_value_iterator_free(value_it); |
| 476 | continue; |
| 477 | } |
| 478 | |
| 479 | struct oval_string_iterator *def_it = |
| 480 | oval_definition_model_get_definitions_dependent_on_variable(def_model, variable); |
| 481 | while (oval_string_iterator_has_more(def_it)) { |
| 482 | char *definition_id = oval_string_iterator_next(def_it); |
| 483 | |
| 484 | struct oval_result_definition *r_definition = oval_result_system_get_definition(r_system, definition_id); |
| 485 | if (r_definition != NULL) { |
| 486 | // Here we simply increase the variable_instance_hint, however |
| 487 | // in future we might want to do better and have a single session wide |
| 488 | // counter and set the variable_instance_hints to this given counter. |
| 489 | // That would allow the one-to-one mapping of variable_instance attributes |
| 490 | // to the oval_variable files. |
| 491 | int instance = oval_result_definition_get_instance(r_definition); |
| 492 | oval_result_definition_set_variable_instance_hint(r_definition, instance + 1); |
| 493 | struct oval_definition *definition = oval_result_definition_get_definition(r_definition); |
| 494 | #if defined(OVAL_PROBES_ENABLED) |
| 495 | oval_probe_hint_definition(session->psess, definition, instance + 1); |
| 496 | #endif |
| 497 | } |
| 498 | else { |
| 499 | // TODO: We really need oval_agent_session wide variable_instance attribute |
| 500 | // to be able to correctly handle syschars even when there is no result-definition. |
| 501 | } |
| 502 | } |
| 503 | oval_string_iterator_free(def_it); |
| 504 | } |
| 505 | oval_value_iterator_free(value_it); |
| 506 | } |
| 507 | } |
| 508 | oscap_htable_iterator_free(hit); |
| 509 | oscap_htable_free(dict, (oscap_destruct_func) oscap_stringlist_free); |
| 510 | |
| 511 | if (conflict) { |
| 512 | /* We have a conflict, clear session and external variables */ |
| 513 | oval_agent_reset_session(session); |
| 514 | } |
| 515 | |
| 516 | if (!session->cur_var_model) { |
| 517 | session->cur_var_model = oval_variable_model_new(); |
| 518 | oval_definition_model_bind_variable_model(def_model, session->cur_var_model); |
| 519 | /* Apply generator template to new model */ |
| 520 | if (session->product_name) { |
| 521 | struct oval_generator *generator; |
| 522 | |
| 523 | generator = oval_variable_model_get_generator(session->cur_var_model); |
| 524 | oval_generator_set_product_name(generator, session->product_name); |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | int oval_agent_resolve_variables(struct oval_agent_session * session, struct xccdf_value_binding_iterator *it) |
| 530 | { |
| 531 | int retval = 0; |
| 532 | |
| 533 | if (!xccdf_value_binding_iterator_has_more(it)) |
| 534 | return 0; |
| 535 | |
| 536 | _oval_agent_resolve_variables_conflict(session, it); |
| 537 | |
| 538 | /* Get the definition model from OVAL agent session */ |
| 539 | struct oval_definition_model *def_model = |
| 540 | oval_results_model_get_definition_model(oval_agent_get_results_model(session)); |
| 541 | |
| 542 | /* Iterate through variable bindings and add variables into the variable model */ |
| 543 | while (xccdf_value_binding_iterator_has_more(it)) { |
| 544 | struct xccdf_value_binding *binding = xccdf_value_binding_iterator_next(it); |
| 545 | char *name = xccdf_value_binding_get_name(binding); |
| 546 | char * value = xccdf_value_binding_get_setvalue(binding); |
| 547 | if (value == NULL) { |
| 548 | value = xccdf_value_binding_get_value(binding); |
| 549 | if (value == NULL) { |
| 550 | value = ""; |
| 551 | } |
| 552 | } |
| 553 | struct oval_variable *variable = oval_definition_model_get_variable(def_model, name); |
| 554 | if (variable != NULL) { |
| 555 | oval_datatype_t o_type = oval_variable_get_datatype(variable); |
| 556 | if (!oval_variable_contains_value(variable, value)) { |
| 557 | /* Add variable to variable model */ |
| 558 | oval_variable_model_add(session->cur_var_model, name, "Unknown", o_type, value); |
| 559 | oval_variable_bind_ext_var(variable, session->cur_var_model, name); |
| 560 | dI("Adding external variable %s.", name); |
| 561 | } else { |
| 562 | /* Skip this variable (we assume it has same values otherwise conflict was detected) */ |
| 563 | dI("Skipping external variable %s.", name); |
| 564 | } |
| 565 | } else { |
| 566 | dW("Variable %s does not exist, skipping.", name); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | return retval; |
| 571 | } |
| 572 | |
| 573 | static int |
| 574 | oval_agent_eval_multi_check(oval_agent_session_t *sess) |
| 575 | { |
| 576 | // This is special handling of evaluation when the process is driven by XCCDF |
| 577 | // and xccdf:check/@multi-check=false. We need extra function for this to apply |
| 578 | // the XCCDF Truth Table for AND. |
| 579 | struct oval_definition *oval_def; |
| 580 | struct oval_definition_iterator *oval_def_it; |
| 581 | const char *id; |
| 582 | oval_result_t oval_result; |
| 583 | xccdf_test_result_type_t xccdf_result; |
| 584 | xccdf_test_result_type_t final_result = 0; |
| 585 | |
| 586 | oval_def_it = oval_definition_model_get_definitions(sess->def_model); |
| 587 | if (!oval_definition_iterator_has_more(oval_def_it)) { |
| 588 | // We are evaluating oval, which has no definitions. We are in state |
| 589 | // which is not explicitly covered in SCAP 1.2 Specification, we are |
| 590 | // better to report error. |
| 591 | final_result = XCCDF_RESULT_ERROR; |
| 592 | } |
| 593 | while (oval_definition_iterator_has_more(oval_def_it)) { |
| 594 | oval_def = oval_definition_iterator_next(oval_def_it); |
| 595 | id = oval_definition_get_id(oval_def); |
| 596 | |
| 597 | // Evaluate definition. |
| 598 | if (oval_agent_eval_definition(sess, id) == -1) { |
| 599 | return -1; |
| 600 | } |
| 601 | if (oval_agent_get_definition_result(sess, id, &oval_result) == -1) { |
| 602 | return -1; |
| 603 | } |
| 604 | // Get XCCDF equivalent of the oval result. |
| 605 | xccdf_result = xccdf_get_result_from_oval(oval_definition_get_class(oval_def), oval_result); |
| 606 | // AND as described in (NISTIR-7275r4): Table 12: Truth Table for AND |
| 607 | final_result = (final_result == 0) ? xccdf_result : |
| 608 | xccdf_test_result_resolve_and_operation(final_result, xccdf_result); |
| 609 | } |
| 610 | oval_definition_iterator_free(oval_def_it); |
| 611 | return final_result; |
| 612 | } |
| 613 | |
| 614 | xccdf_test_result_type_t oval_agent_eval_rule(struct xccdf_policy *policy, const char *rule_id, const char *id, |
| 615 | const char * href, struct xccdf_value_binding_iterator *it, |
| 616 | struct xccdf_check_import_iterator * check_import_it, |
| 617 | void *usr) |
| 618 | { |
| 619 | __attribute__nonnull__(usr); |
| 620 | |
| 621 | oval_result_t result; |
| 622 | int retval = 0; |
| 623 | struct oval_agent_session * sess = (struct oval_agent_session *) usr; |
| 624 | if (strcmp(sess->filename, href)) |
| 625 | return XCCDF_RESULT_NOT_CHECKED; |
| 626 | |
| 627 | /* Resolve variables */ |
| 628 | retval = oval_agent_resolve_variables(sess, it); |
| 629 | if (retval != 0) return XCCDF_RESULT_UNKNOWN; |
| 630 | |
| 631 | if (id != NULL) { |
| 632 | struct oval_definition *definition = oval_definition_model_get_definition(oval_results_model_get_definition_model(oval_agent_get_results_model(sess)), id); |
| 633 | /* If there is no such OVAL definition, return XCCDF_RESUL_NOT_CHECKED. XDCCDF should look for alternative definition in this case. */ |
| 634 | if (definition == NULL) |
| 635 | return XCCDF_RESULT_NOT_CHECKED; |
| 636 | /* Evaluate OVAL definition */ |
| 637 | oval_agent_eval_definition(sess, id); |
| 638 | oval_agent_get_definition_result(sess, id, &result); |
| 639 | return xccdf_get_result_from_oval(oval_definition_get_class(definition), result); |
| 640 | } else { |
| 641 | return oval_agent_eval_multi_check(sess); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | static void * |
| 646 | _oval_agent_list_definitions(void *usr, xccdf_policy_engine_query_t query_type, void *query_data) |
| 647 | { |
| 648 | __attribute__nonnull__(usr); |
| 649 | struct oval_agent_session *sess = (struct oval_agent_session *) usr; |
| 650 | if (query_data != NULL && strcmp(sess->filename, (const char *) query_data)) { |
| 651 | return NULL; |
| 652 | } |
| 653 | if (query_type == POLICY_ENGINE_QUERY_NAMES_FOR_HREF) { |
| 654 | struct oval_definition_iterator *iterator = oval_definition_model_get_definitions(sess->def_model); |
| 655 | struct oscap_stringlist *result = oscap_stringlist_new(); |
| 656 | |
| 657 | while (oval_definition_iterator_has_more(iterator)) { |
| 658 | struct oval_definition *oval_def = oval_definition_iterator_next(iterator); |
| 659 | oscap_stringlist_add_string(result, oval_definition_get_id(oval_def)); |
| 660 | } |
| 661 | |
| 662 | oval_definition_iterator_free(iterator); |
| 663 | return result; |
| 664 | } else if (query_type == POLICY_ENGINE_QUERY_OVAL_DEFS_FOR_HREF) { |
| 665 | struct oval_definition_iterator *iterator = oval_definition_model_get_definitions(sess->def_model); |
| 666 | struct oscap_list *result = oscap_list_new(); |
| 667 | |
| 668 | while (oval_definition_iterator_has_more(iterator)) { |
| 669 | struct oval_definition *oval_def = oval_definition_iterator_next(iterator); |
| 670 | oscap_list_add(result, oval_def); |
| 671 | } |
| 672 | |
| 673 | oval_definition_iterator_free(iterator); |
| 674 | return result; |
| 675 | } else { |
| 676 | return NULL; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | bool xccdf_policy_model_register_engine_oval(struct xccdf_policy_model * model, struct oval_agent_session * usr) |
| 681 | { |
| 682 | |
| 683 | return xccdf_policy_model_register_engine_and_query_callback(model, "http://oval.mitre.org/XMLSchema/oval-definitions-5", |
| 684 | oval_agent_eval_rule, (void *) usr, _oval_agent_list_definitions); |
| 685 | } |
| 686 |