| 1 | /* |
| 2 | * Copyright 2015 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Author: |
| 20 | * Michal Šrubař <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #ifdef OS_WINDOWS |
| 28 | #include <io.h> |
| 29 | #else |
| 30 | #include <unistd.h> |
| 31 | #endif |
| 32 | #include <string.h> |
| 33 | #if defined(OS_LINUX) |
| 34 | #include <linux/limits.h> |
| 35 | #endif |
| 36 | #include <limits.h> |
| 37 | |
| 38 | #include "common/debug_priv.h" |
| 39 | #include "common/util.h" |
| 40 | #include "common/_error.h" |
| 41 | #include "common/oscapxml.h" |
| 42 | #include "source/xslt_priv.h" |
| 43 | #include "public/oval_agent_api.h" |
| 44 | #include "public/oval_session.h" |
| 45 | #include "../DS/public/ds_sds_session.h" |
| 46 | #include "oscap_source.h" |
| 47 | #include "oscap_helpers.h" |
| 48 | |
| 49 | |
| 50 | static const char *oscap_productname = "cpe:/a:open-scap:oscap"; |
| 51 | static const char *oval_results_report = "oval-results-report.xsl"; |
| 52 | |
| 53 | struct oval_session { |
| 54 | /* Main source assigned with the main file (SDS or OVAL) */ |
| 55 | struct oscap_source *source; |
| 56 | struct oval_definition_model *def_model; |
| 57 | struct oval_variable_model *var_model; |
| 58 | struct oval_results_model *res_model; |
| 59 | |
| 60 | oval_agent_session_t *sess; |
| 61 | struct ds_sds_session *sds_session; |
| 62 | |
| 63 | struct { |
| 64 | struct oscap_source *definitions; |
| 65 | struct oscap_source *variables; |
| 66 | struct oscap_source *directives; |
| 67 | } oval; |
| 68 | |
| 69 | char *datastream_id; |
| 70 | /* particular OVAL component if there are two OVALs in one datastream */ |
| 71 | char *component_id; |
| 72 | |
| 73 | struct { |
| 74 | char *results; |
| 75 | char *report; |
| 76 | } export; |
| 77 | |
| 78 | struct { |
| 79 | /* it's called when there is something invalid in input/output files */ |
| 80 | xml_reporter xml_fn; |
| 81 | } reporter; |
| 82 | |
| 83 | bool validation; |
| 84 | bool export_sys_chars; |
| 85 | bool full_validation; |
| 86 | bool fetch_remote_resources; |
| 87 | download_progress_calllback_t progress; |
| 88 | const char *local_files; |
| 89 | }; |
| 90 | |
| 91 | struct oval_session *oval_session_new(const char *filename) |
| 92 | { |
| 93 | oscap_document_type_t scap_type; |
| 94 | struct oval_session *session; |
| 95 | |
| 96 | session = (struct oval_session *) calloc(1, sizeof(struct oval_session)); |
| 97 | |
| 98 | session->source = oscap_source_new_from_file(filename); |
| 99 | if ((scap_type = oscap_source_get_scap_type(session->source)) == OSCAP_DOCUMENT_UNKNOWN) { |
| 100 | oval_session_free(session); |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | if (scap_type != OSCAP_DOCUMENT_OVAL_DEFINITIONS && scap_type != OSCAP_DOCUMENT_SDS) { |
| 105 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Session input file was determined but it" |
| 106 | " isn't an OVAL file nor a source datastream file."); |
| 107 | oval_session_free(session); |
| 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | session->export_sys_chars = true; |
| 112 | |
| 113 | dI("Created a new OVAL session from input file '%s'.", filename); |
| 114 | return session; |
| 115 | } |
| 116 | |
| 117 | void oval_session_set_variables(struct oval_session *session, const char *filename) |
| 118 | { |
| 119 | __attribute__nonnull__(session); |
| 120 | |
| 121 | oscap_source_free(session->oval.variables); |
| 122 | |
| 123 | if (filename != NULL) |
| 124 | session->oval.variables = oscap_source_new_from_file(filename); |
| 125 | else |
| 126 | session->oval.variables = NULL; /* reset */ |
| 127 | } |
| 128 | |
| 129 | void oval_session_set_directives(struct oval_session *session, const char *filename) |
| 130 | { |
| 131 | __attribute__nonnull__(session); |
| 132 | |
| 133 | oscap_source_free(session->oval.directives); |
| 134 | |
| 135 | if (filename != NULL) |
| 136 | session->oval.directives = oscap_source_new_from_file(filename); |
| 137 | else |
| 138 | session->oval.directives = NULL; |
| 139 | } |
| 140 | |
| 141 | void oval_session_set_validation(struct oval_session *session, bool validate, bool full_validation) |
| 142 | { |
| 143 | __attribute__nonnull__(session); |
| 144 | |
| 145 | session->validation = validate; |
| 146 | session->full_validation = full_validation; |
| 147 | } |
| 148 | |
| 149 | void oval_session_set_datastream_id(struct oval_session *session, const char *id) |
| 150 | { |
| 151 | __attribute__nonnull__(session); |
| 152 | |
| 153 | free(session->datastream_id); |
| 154 | session->datastream_id = oscap_strdup(id); |
| 155 | } |
| 156 | |
| 157 | void oval_session_set_component_id(struct oval_session *session, const char *id) |
| 158 | { |
| 159 | __attribute__nonnull__(session); |
| 160 | |
| 161 | free(session->component_id); |
| 162 | session->component_id = oscap_strdup(id); |
| 163 | } |
| 164 | |
| 165 | void oval_session_set_results_export(struct oval_session *session, const char *filename) |
| 166 | { |
| 167 | __attribute__nonnull__(session); |
| 168 | |
| 169 | free(session->export.results); |
| 170 | session->export.results = oscap_strdup(filename); |
| 171 | } |
| 172 | |
| 173 | void oval_session_set_report_export(struct oval_session *session, const char *filename) |
| 174 | { |
| 175 | __attribute__nonnull__(session); |
| 176 | |
| 177 | free(session->export.report); |
| 178 | session->export.report = oscap_strdup(filename); |
| 179 | } |
| 180 | |
| 181 | void oval_session_set_xml_reporter(struct oval_session *session, xml_reporter fn) |
| 182 | { |
| 183 | __attribute__nonnull__(session); |
| 184 | |
| 185 | session->reporter.xml_fn = fn; |
| 186 | } |
| 187 | |
| 188 | static bool oval_session_validate(struct oval_session *session, struct oscap_source *source, oscap_document_type_t type) |
| 189 | { |
| 190 | if (oscap_source_get_scap_type(source) == type) { |
| 191 | if (oscap_source_validate(source, session->reporter.xml_fn, NULL)) |
| 192 | return false; |
| 193 | } |
| 194 | else { |
| 195 | oscap_seterr(OSCAP_EFAMILY_OVAL, "Type mismatch: %s. Expecting %s " |
| 196 | "but found %s.", oscap_source_readable_origin(source), |
| 197 | oscap_document_type_to_string(type), |
| 198 | oscap_document_type_to_string(oscap_source_get_scap_type(source))); |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | static int oval_session_load_definitions(struct oval_session *session) |
| 206 | { |
| 207 | __attribute__nonnull__(session); |
| 208 | __attribute__nonnull__(session->source); |
| 209 | |
| 210 | oscap_document_type_t type = oscap_source_get_scap_type(session->source); |
| 211 | if (type != OSCAP_DOCUMENT_OVAL_DEFINITIONS && type != OSCAP_DOCUMENT_SDS) { |
| 212 | oscap_seterr(OSCAP_EFAMILY_OVAL, "Type mismatch: %s. Expecting %s " |
| 213 | "or %s but found %s.", oscap_source_readable_origin(session->source), |
| 214 | oscap_document_type_to_string(OSCAP_DOCUMENT_OVAL_DEFINITIONS), |
| 215 | oscap_document_type_to_string(OSCAP_DOCUMENT_SDS), |
| 216 | oscap_document_type_to_string(type)); |
| 217 | return 1; |
| 218 | } |
| 219 | else { |
| 220 | if (session->validation && !oval_session_validate(session, session->source, type)) |
| 221 | return 1; |
| 222 | } |
| 223 | |
| 224 | if (oscap_source_get_scap_type(session->source) == OSCAP_DOCUMENT_SDS) { |
| 225 | if ((session->sds_session = ds_sds_session_new_from_source(session->source)) == NULL) { |
| 226 | return 1; |
| 227 | } |
| 228 | ds_sds_session_configure_remote_resources(session->sds_session, session->fetch_remote_resources, session->local_files, session->progress); |
| 229 | ds_sds_session_set_datastream_id(session->sds_session, session->datastream_id); |
| 230 | if (ds_sds_session_register_component_with_dependencies(session->sds_session, |
| 231 | "checks", session->component_id, "oval.xml") != 0) { |
| 232 | return 1; |
| 233 | } |
| 234 | |
| 235 | session->oval.definitions = ds_sds_session_get_component_by_href(session->sds_session, "oval.xml"); |
| 236 | if (session->oval.definitions == NULL) { |
| 237 | oscap_seterr(OSCAP_EFAMILY_OVAL, "Internal error: OVAL file was not found in " |
| 238 | "Source DataStream session cache!"); |
| 239 | return 1; |
| 240 | } |
| 241 | } |
| 242 | else { |
| 243 | session->oval.definitions = session->source; |
| 244 | } |
| 245 | |
| 246 | /* import OVAL Definitions */ |
| 247 | if (session->def_model) oval_definition_model_free(session->def_model); |
| 248 | session->def_model = oval_definition_model_import_source(session->oval.definitions); |
| 249 | if (session->def_model == NULL) { |
| 250 | oscap_seterr(OSCAP_EFAMILY_OVAL, "Failed to import the OVAL Definitions from '%s'.", |
| 251 | oscap_source_readable_origin(session->oval.definitions)); |
| 252 | return 1; |
| 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static int oval_session_load_variables(struct oval_session *session) |
| 259 | { |
| 260 | __attribute__nonnull__(session); |
| 261 | |
| 262 | if (session->oval.variables != NULL) { |
| 263 | |
| 264 | if (session->def_model == NULL) { |
| 265 | oscap_seterr(OSCAP_EFAMILY_OVAL, "No OVAL Definitions to bind the variables to."); |
| 266 | return 1; |
| 267 | } |
| 268 | if (session->validation && !oval_session_validate(session, session->oval.variables, OSCAP_DOCUMENT_OVAL_VARIABLES)) { |
| 269 | return 1; |
| 270 | } |
| 271 | |
| 272 | /* bind external variables */ |
| 273 | if (session->oval.variables) { |
| 274 | session->var_model = oval_variable_model_import_source(session->oval.variables); |
| 275 | |
| 276 | if (session->var_model == NULL) { |
| 277 | oscap_seterr(OSCAP_EFAMILY_OVAL, "Failed to import the OVAL Variables " |
| 278 | "from '%s'.", oscap_source_get_filepath(session->oval.variables)); |
| 279 | return 1; |
| 280 | } |
| 281 | |
| 282 | if (oval_definition_model_bind_variable_model(session->def_model, session->var_model)) { |
| 283 | oscap_seterr(OSCAP_EFAMILY_OVAL, "Failed to bind Variables to Definitions."); |
| 284 | return 1; |
| 285 | } |
| 286 | } |
| 287 | dI("Loaded OVAL variables."); |
| 288 | } else { |
| 289 | dI("No external OVAL variables provided."); |
| 290 | } |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | int oval_session_load(struct oval_session *session) |
| 296 | { |
| 297 | __attribute__nonnull__(session); |
| 298 | |
| 299 | int ret = 0; |
| 300 | |
| 301 | if ((ret = oval_session_load_definitions(session)) != 0) { |
| 302 | return ret; |
| 303 | } |
| 304 | if ((ret = oval_session_load_variables(session)) != 0) { |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | static int oval_session_setup_agent(struct oval_session *session) |
| 312 | { |
| 313 | __attribute__nonnull__(session); |
| 314 | |
| 315 | char *path_clone; |
| 316 | |
| 317 | path_clone = oscap_strdup(oscap_source_readable_origin(session->oval.definitions)); |
| 318 | if (path_clone == NULL) { |
| 319 | return 1; |
| 320 | } |
| 321 | |
| 322 | /* free the previous Agent session if this function was already called */ |
| 323 | if (session->sess) |
| 324 | oval_agent_destroy_session(session->sess); |
| 325 | |
| 326 | char *base_name = oscap_basename(path_clone); |
| 327 | session->sess = oval_agent_new_session(session->def_model, base_name); |
| 328 | free(base_name); |
| 329 | if (session->sess == NULL) { |
| 330 | oscap_seterr(OSCAP_EFAMILY_OVAL, "Failed to create a new agent session."); |
| 331 | free(path_clone); |
| 332 | return 1; |
| 333 | } |
| 334 | free(path_clone); |
| 335 | |
| 336 | oval_agent_set_product_name(session->sess, (char *)oscap_productname); |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | int oval_session_evaluate_id(struct oval_session *session, const char *id, oval_result_t *result) |
| 341 | { |
| 342 | __attribute__nonnull__(session); |
| 343 | |
| 344 | if (id == NULL) { |
| 345 | oscap_seterr(OSCAP_EFAMILY_OVAL, "No OVAL Definion id set."); |
| 346 | return 1; |
| 347 | } |
| 348 | |
| 349 | if (oval_session_setup_agent(session) != 0) { |
| 350 | return 1; |
| 351 | } |
| 352 | |
| 353 | oval_agent_eval_definition(session->sess, id); |
| 354 | *result = OVAL_RESULT_NOT_EVALUATED; |
| 355 | oval_agent_get_definition_result(session->sess, id, result); |
| 356 | if (oscap_err()) { |
| 357 | return 1; |
| 358 | } |
| 359 | |
| 360 | session->res_model = oval_agent_get_results_model(session->sess); |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | int oval_session_evaluate(struct oval_session *session, agent_reporter fn, void *arg) |
| 366 | { |
| 367 | __attribute__nonnull__(session); |
| 368 | |
| 369 | if (oval_session_setup_agent(session) != 0) { |
| 370 | return 1; |
| 371 | } |
| 372 | |
| 373 | oval_agent_eval_system(session->sess, fn, arg); |
| 374 | if (oscap_err()) { |
| 375 | return 1; |
| 376 | } |
| 377 | |
| 378 | session->res_model = oval_agent_get_results_model(session->sess); |
| 379 | |
| 380 | dI("OVAL evaluation successfully finished."); |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | int oval_session_export(struct oval_session *session) |
| 385 | { |
| 386 | __attribute__nonnull__(session); |
| 387 | |
| 388 | struct oval_directives_model *dir_model = NULL; |
| 389 | struct oscap_source *result = NULL; /* OVAL Results */ |
| 390 | const char *filename = NULL; |
| 391 | int ret = 0; |
| 392 | |
| 393 | /* Import OVAL Directives if any */ |
| 394 | if (session->oval.directives && session->res_model) { |
| 395 | /* OVAL Directives can be used only if there are OVAL Resutls and these are |
| 396 | * only available if there is a Results model which mean that either |
| 397 | * evaluation or analyse was performed. */ |
| 398 | dir_model = oval_directives_model_new(); |
| 399 | if (oval_directives_model_import_source(dir_model, session->oval.directives) != 0) |
| 400 | goto cleanup; |
| 401 | } |
| 402 | |
| 403 | /* Get OVAL Results if evaluation or analyse has been done and apply |
| 404 | * directives to them */ |
| 405 | if (session->res_model && (session->export.results || session->export.report)) { |
| 406 | oval_results_model_set_export_system_characteristics(session->res_model, session->export_sys_chars); |
| 407 | result = oval_results_model_export_source(session->res_model, dir_model, NULL); |
| 408 | filename = session->export.results; |
| 409 | } |
| 410 | |
| 411 | /* Validate OVAL Results. The 'result' in condition will make sure that there is |
| 412 | * something to validate */ |
| 413 | if (result && session->validation && session->full_validation) { |
| 414 | if (!oval_session_validate(session, result, OSCAP_DOCUMENT_OVAL_RESULTS)) |
| 415 | goto cleanup; |
| 416 | } |
| 417 | |
| 418 | if (session->export.results && result) { /* export to XML */ |
| 419 | if (oscap_source_save_as(result, filename) != 0) |
| 420 | goto cleanup; |
| 421 | } |
| 422 | |
| 423 | if (session->export.report && result) { /* export to HTML */ |
| 424 | char pwd[PATH_MAX]; |
| 425 | |
| 426 | if (getcwd(pwd, sizeof(pwd)) == NULL) { |
| 427 | oscap_seterr(OSCAP_EFAMILY_GLIBC, "ERROR: %s", strerror(errno)); |
| 428 | goto cleanup; |
| 429 | } |
| 430 | |
| 431 | /* add params oscap-version & pwd */ |
| 432 | const char *stdparams[] = { "oscap-version", oscap_get_version(), "pwd", pwd, NULL }; |
| 433 | |
| 434 | /* TODO: let the user set the xsl by oval_session_set_report_xsl? */ |
| 435 | if (oscap_source_apply_xslt_path(result, oval_results_report, |
| 436 | session->export.report, stdparams, oscap_path_to_xslt()) == -1) { |
| 437 | goto cleanup; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | ret = 0; /* successfull export */ |
| 442 | |
| 443 | cleanup: |
| 444 | if (result) |
| 445 | oscap_source_free(result); |
| 446 | if (dir_model) |
| 447 | oval_directives_model_free(dir_model); |
| 448 | return ret; |
| 449 | } |
| 450 | |
| 451 | void oval_session_set_export_system_characteristics(struct oval_session *session, bool export) |
| 452 | { |
| 453 | session->export_sys_chars = export; |
| 454 | } |
| 455 | |
| 456 | void oval_session_configure_remote_resources(struct oval_session *session, bool allowed, const char *local_files, download_progress_calllback_t callback) |
| 457 | { |
| 458 | session->fetch_remote_resources = allowed; |
| 459 | session->local_files = local_files; |
| 460 | session->progress = callback; |
| 461 | } |
| 462 | |
| 463 | void oval_session_set_remote_resources(struct oval_session *session, bool allowed, download_progress_calllback_t callback) |
| 464 | { |
| 465 | oval_session_configure_remote_resources(session, allowed, NULL, callback); |
| 466 | } |
| 467 | |
| 468 | void oval_session_free(struct oval_session *session) |
| 469 | { |
| 470 | if (session == NULL) |
| 471 | return; |
| 472 | |
| 473 | oscap_source_free(session->oval.directives); |
| 474 | oscap_source_free(session->oval.variables); |
| 475 | oscap_source_free(session->source); |
| 476 | free(session->datastream_id); |
| 477 | free(session->component_id); |
| 478 | free(session->export.results); |
| 479 | free(session->export.report); |
| 480 | if (session->sess) |
| 481 | oval_agent_destroy_session(session->sess); |
| 482 | if (session->def_model) |
| 483 | oval_definition_model_free(session->def_model); |
| 484 | ds_sds_session_free(session->sds_session); |
| 485 | free(session); |
| 486 | } |