| 1 | /* |
| 2 | * Copyright 2010--2014 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 | * Authors: |
| 20 | * Maros Barabas <[email protected]> |
| 21 | * Šimon Lukašík |
| 22 | */ |
| 23 | |
| 24 | #ifdef HAVE_CONFIG_H |
| 25 | #include <config.h> |
| 26 | #endif |
| 27 | |
| 28 | /* OVAL & OSCAP common */ |
| 29 | #include "oscap_source.h" |
| 30 | #if defined(OVAL_PROBES_ENABLED) |
| 31 | # include <oval_probe.h> |
| 32 | # include "probe-table.h" |
| 33 | #endif |
| 34 | #include <oval_agent_api.h> |
| 35 | #include <oval_session.h> |
| 36 | #include <oval_results.h> |
| 37 | #include <oval_variables.h> |
| 38 | #include <ds_sds_session.h> |
| 39 | #include <assert.h> |
| 40 | #include <limits.h> |
| 41 | #ifdef HAVE_GETOPT_H |
| 42 | #include <getopt.h> |
| 43 | #endif |
| 44 | |
| 45 | #include "oscap-tool.h" |
| 46 | #include "scap_ds.h" |
| 47 | #include <oscap_debug.h> |
| 48 | |
| 49 | #if defined(OVAL_PROBES_ENABLED) |
| 50 | static int app_collect_oval(const struct oscap_action *action); |
| 51 | static int app_evaluate_oval(const struct oscap_action *action); |
| 52 | #endif |
| 53 | static int app_oval_validate(const struct oscap_action *action); |
| 54 | static int app_oval_xslt(const struct oscap_action *action); |
| 55 | static int app_analyse_oval(const struct oscap_action *action); |
| 56 | |
| 57 | #if defined(OVAL_PROBES_ENABLED) |
| 58 | static bool getopt_oval_eval(int argc, char **argv, struct oscap_action *action); |
| 59 | static bool getopt_oval_collect(int argc, char **argv, struct oscap_action *action); |
| 60 | #endif |
| 61 | static bool getopt_oval_analyse(int argc, char **argv, struct oscap_action *action); |
| 62 | static bool getopt_oval_validate(int argc, char **argv, struct oscap_action *action); |
| 63 | static bool getopt_oval_report(int argc, char **argv, struct oscap_action *action); |
| 64 | |
| 65 | |
| 66 | static bool valid_inputs(const struct oscap_action *action); |
| 67 | |
| 68 | #define OVAL_SUBMODULES_NUM 7 |
| 69 | #define OVAL_GEN_SUBMODULES_NUM 2 /* See actual OVAL_GEN_SUBMODULES and |
| 70 | OVAL_SUBMODULES arrays initialization below. */ |
| 71 | static struct oscap_module* OVAL_SUBMODULES[OVAL_SUBMODULES_NUM]; |
| 72 | static struct oscap_module* OVAL_GEN_SUBMODULES[OVAL_GEN_SUBMODULES_NUM]; |
| 73 | |
| 74 | struct oscap_module OSCAP_OVAL_MODULE = { |
| 75 | .name = "oval", |
| 76 | .parent = &OSCAP_ROOT_MODULE, |
| 77 | .summary = "Open Vulnerability and Assessment Language", |
| 78 | .submodules = OVAL_SUBMODULES |
| 79 | }; |
| 80 | |
| 81 | static struct oscap_module OVAL_VALIDATE = { |
| 82 | .name = "validate", |
| 83 | .parent = &OSCAP_OVAL_MODULE, |
| 84 | .summary = "Validate OVAL XML content", |
| 85 | .usage = "[options] oval-file.xml", |
| 86 | .help = |
| 87 | "Options:\n" |
| 88 | " --definitions - Validate OVAL Definitions\n" |
| 89 | " --variables - Validate external OVAL Variables\n" |
| 90 | " --syschar - Validate OVAL System Characteristics\n" |
| 91 | " --results - Validate OVAL Results\n" |
| 92 | " --skip-schematron - Do not use schematron-based validation in addition to XML Schema\n" |
| 93 | " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n" |
| 94 | " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n" |
| 95 | " --verbose-log-file <file> - Write verbose information into file.\n", |
| 96 | .opt_parser = getopt_oval_validate, |
| 97 | .func = app_oval_validate |
| 98 | }; |
| 99 | |
| 100 | #if defined(OVAL_PROBES_ENABLED) |
| 101 | static struct oscap_module OVAL_EVAL = { |
| 102 | .name = "eval", |
| 103 | .parent = &OSCAP_OVAL_MODULE, |
| 104 | .summary = "Probe the system and evaluate definitions from OVAL Definition file", |
| 105 | .usage = "[options] oval-definitions.xml", |
| 106 | .help = |
| 107 | "Options:\n" |
| 108 | " --id <definition-id> - ID of the definition we want to evaluate.\n" |
| 109 | " --variables <file> - Provide external variables expected by OVAL Definitions.\n" |
| 110 | " --directives <file> - Use OVAL Directives content to specify desired results content.\n" |
| 111 | " --without-syschar - Don't provide system characteristic in result file.\n" |
| 112 | " --results <file> - Write OVAL Results into file.\n" |
| 113 | " --report <file> - Create human readable (HTML) report from OVAL Results.\n" |
| 114 | " --skip-validation - Skip validation.\n" |
| 115 | " --datastream-id <id> - ID of the data stream in the collection to use.\n" |
| 116 | " (only applicable for source data streams)\n" |
| 117 | " --oval-id <id> - ID of the OVAL component ref in the data stream to use.\n" |
| 118 | " (only applicable for source data streams)\n" |
| 119 | " --fetch-remote-resources - Download remote content referenced by OVAL Definitions.\n" |
| 120 | " (only applicable for source data streams)\n" |
| 121 | " --local-files <dir> - Use locally downloaded copies of remote resources stored in the given directory.\n" |
| 122 | " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n" |
| 123 | " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n" |
| 124 | " --verbose-log-file <file> - Write verbose information into file.\n", |
| 125 | .opt_parser = getopt_oval_eval, |
| 126 | .func = app_evaluate_oval |
| 127 | }; |
| 128 | |
| 129 | static struct oscap_module OVAL_COLLECT = { |
| 130 | .name = "collect", |
| 131 | .parent = &OSCAP_OVAL_MODULE, |
| 132 | .summary = "Probe the system and create system characteristics", |
| 133 | .usage = "[options] oval-definitions.xml", |
| 134 | .help = |
| 135 | "Options:\n" |
| 136 | " --id <object> - Collect system characteristics ONLY for specified OVAL Object.\n" |
| 137 | " --syschar <file> - Write OVAL System Characteristic into file.\n" |
| 138 | " --variables <file> - Provide external variables expected by OVAL Definitions.\n" |
| 139 | " --skip-validation - Skip validation.\n" |
| 140 | " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n" |
| 141 | " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n" |
| 142 | " --verbose-log-file <file> - Write verbose information into file.\n", |
| 143 | .opt_parser = getopt_oval_collect, |
| 144 | .func = app_collect_oval |
| 145 | }; |
| 146 | #endif /* OVAL_PROBES_ENABLED */ |
| 147 | |
| 148 | static struct oscap_module OVAL_ANALYSE = { |
| 149 | .name = "analyse", |
| 150 | .parent = &OSCAP_OVAL_MODULE, |
| 151 | .summary = "Evaluate provided system characteristics file", |
| 152 | .usage = "[options] --results FILE oval-definitions.xml system-characteristics.xml" , |
| 153 | .help = |
| 154 | "Options:\n" |
| 155 | " --variables <file> - Provide external variables expected by OVAL Definitions.\n" |
| 156 | " --directives <file> - Use OVAL Directives content to specify desired results content.\n" |
| 157 | " --skip-validation - Skip validation.\n" |
| 158 | " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n" |
| 159 | " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n" |
| 160 | " --verbose-log-file <file> - Write verbose information into file.\n", |
| 161 | .opt_parser = getopt_oval_analyse, |
| 162 | .func = app_analyse_oval |
| 163 | }; |
| 164 | |
| 165 | static struct oscap_module OVAL_GENERATE = { |
| 166 | .name = "generate", |
| 167 | .parent = &OSCAP_OVAL_MODULE, |
| 168 | .summary = "Convert an OVAL file to other formats", |
| 169 | .usage_extra = "<subcommand> [sub-options] oval-file.xml", |
| 170 | .submodules = OVAL_GEN_SUBMODULES |
| 171 | }; |
| 172 | |
| 173 | static struct oscap_module OVAL_REPORT = { |
| 174 | .name = "report", |
| 175 | .parent = &OVAL_GENERATE, |
| 176 | .summary = "Generate a HTML report from OVAL results file", |
| 177 | .usage = "[options] oval-file.xml", |
| 178 | .help = |
| 179 | "Options:\n" |
| 180 | " --output <file> - Write the HTML into file.\n" |
| 181 | " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n" |
| 182 | " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n" |
| 183 | " --verbose-log-file <file> - Write verbose information into file.\n", |
| 184 | .opt_parser = getopt_oval_report, |
| 185 | .user = "oval-results-report.xsl", |
| 186 | .func = app_oval_xslt |
| 187 | }; |
| 188 | |
| 189 | static struct oscap_module* OVAL_GEN_SUBMODULES[OVAL_GEN_SUBMODULES_NUM] = { |
| 190 | &OVAL_REPORT, |
| 191 | NULL |
| 192 | }; |
| 193 | |
| 194 | static struct oscap_module* OVAL_SUBMODULES[OVAL_SUBMODULES_NUM] = { |
| 195 | #if defined(OVAL_PROBES_ENABLED) |
| 196 | &OVAL_COLLECT, |
| 197 | &OVAL_EVAL, |
| 198 | #endif |
| 199 | &OVAL_ANALYSE, |
| 200 | &OVAL_VALIDATE, |
| 201 | &OVAL_GENERATE, |
| 202 | NULL |
| 203 | }; |
| 204 | |
| 205 | static int app_oval_callback(const struct oval_result_definition * res_def, void *arg) |
| 206 | { |
| 207 | oval_result_t result = oval_result_definition_get_result(res_def); |
| 208 | |
| 209 | printf("Definition %s: %s\n", oval_result_definition_get_id(res_def), oval_result_get_text(result)); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | #if defined(OVAL_PROBES_ENABLED) |
| 215 | int app_collect_oval(const struct oscap_action *action) |
| 216 | { |
| 217 | struct oval_definition_model *def_model = NULL; |
| 218 | struct oval_variable_model *var_model = NULL; |
| 219 | struct oval_syschar_model *sys_model = NULL; |
| 220 | struct oval_sysinfo *sysinfo = NULL; |
| 221 | struct oval_probe_session *pb_sess = NULL; |
| 222 | struct oval_generator *generator = NULL; |
| 223 | int ret = OSCAP_ERROR; |
| 224 | |
| 225 | /* validate inputs */ |
| 226 | if (action->validate) { |
| 227 | if (!valid_inputs(action)) { |
| 228 | goto cleanup; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /* import definitions */ |
| 233 | struct oscap_source *source = oscap_source_new_from_file(action->f_oval); |
| 234 | def_model = oval_definition_model_import_source(source); |
| 235 | oscap_source_free(source); |
| 236 | if (def_model == NULL) { |
| 237 | fprintf(stderr, "Failed to import the OVAL Definitions from '%s'.\n", action->f_oval); |
| 238 | goto cleanup; |
| 239 | } |
| 240 | |
| 241 | /* bind external variables */ |
| 242 | if(action->f_variables) { |
| 243 | struct oscap_source *var_source = oscap_source_new_from_file(action->f_variables); |
| 244 | var_model = oval_variable_model_import_source(var_source); |
| 245 | oscap_source_free(var_source); |
| 246 | if (var_model == NULL) { |
| 247 | fprintf(stderr, "Failed to import the OVAL Variables from '%s'.\n", action->f_variables); |
| 248 | goto cleanup; |
| 249 | } |
| 250 | |
| 251 | if (oval_definition_model_bind_variable_model(def_model, var_model)) { |
| 252 | fprintf(stderr, "Failed to bind Variables to Definitions\n"); |
| 253 | goto cleanup; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /* create empty syschar model */ |
| 258 | sys_model = oval_syschar_model_new(def_model); |
| 259 | |
| 260 | /* set product name */ |
| 261 | generator = oval_syschar_model_get_generator(sys_model); |
| 262 | oval_generator_set_product_name(generator, OSCAP_PRODUCTNAME); |
| 263 | |
| 264 | /* create probe session */ |
| 265 | pb_sess = oval_probe_session_new(sys_model); |
| 266 | |
| 267 | /* query sysinfo */ |
| 268 | ret = oval_probe_query_sysinfo(pb_sess, &sysinfo); |
| 269 | if (ret != 0) { |
| 270 | fprintf(stderr, "Failed to query sysinfo\n"); |
| 271 | goto cleanup; |
| 272 | } |
| 273 | oval_syschar_model_set_sysinfo(sys_model, sysinfo); |
| 274 | |
| 275 | /* query objects */ |
| 276 | struct oval_object *object; |
| 277 | struct oval_syschar *syschar; |
| 278 | oval_syschar_collection_flag_t sc_flg; |
| 279 | if (action->id) { |
| 280 | object = oval_definition_model_get_object(def_model, action->id); |
| 281 | if (!object) { |
| 282 | fprintf(stderr, "Object ID(%s) does not exist in '%s'.\n", action->id, action->f_oval); |
| 283 | goto cleanup; |
| 284 | } |
| 285 | printf("Collected: \"%s\" : ", oval_object_get_id(object)); |
| 286 | oval_probe_query_object(pb_sess, object, 0, &syschar); |
| 287 | sc_flg = oval_syschar_get_flag(syschar); |
| 288 | printf("%s\n", oval_syschar_collection_flag_get_text(sc_flg)); |
| 289 | } |
| 290 | else { |
| 291 | struct oval_object_iterator *objects = oval_definition_model_get_objects(def_model); |
| 292 | while (oval_object_iterator_has_more(objects)) { |
| 293 | object = oval_object_iterator_next(objects); |
| 294 | printf("Collected: \"%s\" : ", oval_object_get_id(object)); |
| 295 | oval_probe_query_object(pb_sess, object, 0, &syschar); |
| 296 | sc_flg = oval_syschar_get_flag(syschar); |
| 297 | printf("%s\n", oval_syschar_collection_flag_get_text(sc_flg)); |
| 298 | } |
| 299 | oval_object_iterator_free(objects); |
| 300 | } |
| 301 | |
| 302 | const char* full_validation = getenv("OSCAP_FULL_VALIDATION"); |
| 303 | |
| 304 | /* output */ |
| 305 | if (action->f_syschar != NULL) { |
| 306 | /* export OVAL System Characteristics */ |
| 307 | oval_syschar_model_export(sys_model, action->f_syschar); |
| 308 | |
| 309 | /* validate OVAL System Characteristics */ |
| 310 | if (action->validate && full_validation) { |
| 311 | struct oscap_source *syschar_source = oscap_source_new_from_file(action->f_syschar); |
| 312 | if (oscap_source_validate(syschar_source, reporter, (void *)action)) { |
| 313 | oscap_source_free(syschar_source); |
| 314 | goto cleanup; |
| 315 | } |
| 316 | fprintf(stdout, "OVAL System Characteristics are exported correctly.\n"); |
| 317 | oscap_source_free(syschar_source); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | ret = OSCAP_OK; |
| 322 | |
| 323 | cleanup: |
| 324 | if(oscap_err()) |
| 325 | fprintf(stderr, "%s %s\n", OSCAP_ERR_MSG, oscap_err_desc()); |
| 326 | |
| 327 | if (sysinfo) oval_sysinfo_free(sysinfo); |
| 328 | if (pb_sess) oval_probe_session_destroy(pb_sess); |
| 329 | if (sys_model) oval_syschar_model_free(sys_model); |
| 330 | if (def_model) oval_definition_model_free(def_model); |
| 331 | |
| 332 | return ret; |
| 333 | } |
| 334 | |
| 335 | int app_evaluate_oval(const struct oscap_action *action) |
| 336 | { |
| 337 | struct oval_session *session = NULL; |
| 338 | oval_result_t eval_result; |
| 339 | int ret = OSCAP_ERROR; |
| 340 | |
| 341 | /* create a new OVAL session */ |
| 342 | if ((session = oval_session_new(action->f_oval)) == NULL) { |
| 343 | oscap_print_error(); |
| 344 | return ret; |
| 345 | } |
| 346 | |
| 347 | /* set validation level */ |
| 348 | oval_session_set_validation(session, action->validate, getenv("OSCAP_FULL_VALIDATION") != NULL); |
| 349 | |
| 350 | /* set source DS related IDs */ |
| 351 | oval_session_set_datastream_id(session, action->f_datastream_id); |
| 352 | oval_session_set_component_id(session, action->f_oval_id); |
| 353 | |
| 354 | /* set reporter function for XML validation of inputs and outputs */ |
| 355 | oval_session_set_xml_reporter(session, reporter); |
| 356 | |
| 357 | /* set OVAL Variables */ |
| 358 | oval_session_set_variables(session, action->f_variables); |
| 359 | |
| 360 | oval_session_configure_remote_resources(session, action->remote_resources, action->local_files, download_reporting_callback); |
| 361 | /* load all necesary OVAL Definitions and bind OVAL Variables if provided */ |
| 362 | if ((oval_session_load(session)) != 0) |
| 363 | goto cleanup; |
| 364 | |
| 365 | /* evaluation */ |
| 366 | if (action->id) { |
| 367 | if ((oval_session_evaluate_id(session, action->id, &eval_result)) != 0) |
| 368 | goto cleanup; |
| 369 | printf("Definition %s: %s\n", action->id, oval_result_get_text(eval_result)); |
| 370 | } |
| 371 | else { |
| 372 | if ((oval_session_evaluate(session, app_oval_callback, NULL)) != 0) |
| 373 | goto cleanup; |
| 374 | } |
| 375 | |
| 376 | printf("Evaluation done.\n"); |
| 377 | |
| 378 | oval_session_set_directives(session, action->f_directives); |
| 379 | oval_session_set_results_export(session, action->f_results); |
| 380 | oval_session_set_report_export(session, action->f_report); |
| 381 | oval_session_set_export_system_characteristics(session, !action->without_sys_chars); |
| 382 | if (oval_session_export(session) != 0) |
| 383 | goto cleanup; |
| 384 | |
| 385 | ret = OSCAP_OK; |
| 386 | |
| 387 | cleanup: |
| 388 | oscap_print_error(); |
| 389 | oval_session_free(session); |
| 390 | return ret; |
| 391 | } |
| 392 | #endif /* OVAL_PROBES_ENABLED */ |
| 393 | |
| 394 | static int app_analyse_oval(const struct oscap_action *action) { |
| 395 | struct oval_definition_model *def_model = NULL; |
| 396 | struct oval_syschar_model *sys_model = NULL; |
| 397 | struct oval_results_model *res_model = NULL; |
| 398 | struct oval_variable_model *var_model = NULL; |
| 399 | struct oval_directives_model *dir_model = NULL; |
| 400 | struct oval_syschar_model *sys_models[2]; |
| 401 | struct oval_generator *generator = NULL; |
| 402 | int ret = OSCAP_ERROR; |
| 403 | |
| 404 | /* validate inputs */ |
| 405 | if (action->validate) { |
| 406 | if (!valid_inputs(action)) { |
| 407 | goto cleanup; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | /* load defnitions */ |
| 412 | struct oscap_source *source = oscap_source_new_from_file(action->f_oval); |
| 413 | def_model = oval_definition_model_import_source(source); |
| 414 | oscap_source_free(source); |
| 415 | if (def_model == NULL) { |
| 416 | fprintf(stderr, "Failed to import the OVAL Definitions from '%s'.\n", action->f_oval); |
| 417 | goto cleanup; |
| 418 | } |
| 419 | |
| 420 | /* bind external variables */ |
| 421 | if(action->f_variables) { |
| 422 | struct oscap_source *var_source = oscap_source_new_from_file(action->f_variables); |
| 423 | var_model = oval_variable_model_import_source(var_source); |
| 424 | oscap_source_free(var_source); |
| 425 | if (var_model == NULL) { |
| 426 | fprintf(stderr, "Failed to import the OVAL Variables from '%s'.\n", action->f_variables); |
| 427 | goto cleanup; |
| 428 | } |
| 429 | |
| 430 | if (oval_definition_model_bind_variable_model(def_model, var_model)) { |
| 431 | fprintf(stderr, "Failed to bind Variables to Definitions\n"); |
| 432 | goto cleanup; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /* load system characteristics */ |
| 437 | sys_model = oval_syschar_model_new(def_model); |
| 438 | source = oscap_source_new_from_file(action->f_syschar); |
| 439 | if (oval_syschar_model_import_source(sys_model, source) == -1 ) { |
| 440 | fprintf(stderr, "Failed to import the System Characteristics from '%s'.\n", action->f_syschar); |
| 441 | oscap_source_free(source); |
| 442 | goto cleanup; |
| 443 | } |
| 444 | oscap_source_free(source); |
| 445 | |
| 446 | /* evaluate */ |
| 447 | sys_models[0] = sys_model; |
| 448 | sys_models[1] = NULL; |
| 449 | res_model = oval_results_model_new(def_model, sys_models); |
| 450 | |
| 451 | /* set product name */ |
| 452 | generator = oval_results_model_get_generator(res_model); |
| 453 | oval_generator_set_product_name(generator, OSCAP_PRODUCTNAME); |
| 454 | oval_generator_set_product_version(generator, oscap_get_version()); |
| 455 | |
| 456 | oval_results_model_eval(res_model); |
| 457 | |
| 458 | /* export results */ |
| 459 | if (action->f_results != NULL) { |
| 460 | /* import directives */ |
| 461 | if (action->f_directives != NULL) { |
| 462 | dir_model = oval_directives_model_new(); |
| 463 | struct oscap_source *dir_source = oscap_source_new_from_file(action->f_directives); |
| 464 | oval_directives_model_import_source(dir_model, dir_source); |
| 465 | oscap_source_free(dir_source); |
| 466 | } |
| 467 | |
| 468 | /* export result model to XML */ |
| 469 | oval_results_model_export(res_model, dir_model, action->f_results); |
| 470 | |
| 471 | const char* full_validation = getenv("OSCAP_FULL_VALIDATION"); |
| 472 | |
| 473 | /* validate OVAL Results */ |
| 474 | if (action->validate && full_validation) { |
| 475 | struct oscap_source *result_source = oscap_source_new_from_file(action->f_results); |
| 476 | if (oscap_source_validate(result_source, reporter, (void *) action)) { |
| 477 | oscap_source_free(result_source); |
| 478 | goto cleanup; |
| 479 | } |
| 480 | fprintf(stdout, "OVAL Results are exported correctly.\n"); |
| 481 | oscap_source_free(result_source); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | ret = OSCAP_OK; |
| 486 | |
| 487 | /* clean up */ |
| 488 | cleanup: |
| 489 | if(oscap_err()) |
| 490 | fprintf(stderr, "%s %s\n", OSCAP_ERR_MSG, oscap_err_desc()); |
| 491 | |
| 492 | if(res_model) oval_results_model_free(res_model); |
| 493 | if(sys_model) oval_syschar_model_free(sys_model); |
| 494 | if(def_model) oval_definition_model_free(def_model); |
| 495 | if(dir_model) oval_directives_model_free(dir_model); |
| 496 | |
| 497 | return ret; |
| 498 | } |
| 499 | |
| 500 | static int app_oval_xslt(const struct oscap_action *action) |
| 501 | { |
| 502 | assert(action->module->user); |
| 503 | return app_xslt(action->f_oval, action->module->user, action->f_results, NULL); |
| 504 | } |
| 505 | |
| 506 | enum oval_opt { |
| 507 | OVAL_OPT_RESULT_FILE = 1, |
| 508 | OVAL_OPT_REPORT_FILE, |
| 509 | OVAL_OPT_ID, |
| 510 | OVAL_OPT_VARIABLES, |
| 511 | OVAL_OPT_SYSCHAR, |
| 512 | OVAL_OPT_DIRECTIVES, |
| 513 | OVAL_OPT_DATASTREAM_ID, |
| 514 | OVAL_OPT_OVAL_ID, |
| 515 | OVAL_OPT_OUTPUT = 'o', |
| 516 | OVAL_OPT_LOCAL_FILES, |
| 517 | OVAL_OPT_VERBOSE, |
| 518 | OVAL_OPT_VERBOSE_LOG_FILE, |
| 519 | }; |
| 520 | |
| 521 | #if defined(OVAL_PROBES_ENABLED) |
| 522 | bool getopt_oval_eval(int argc, char **argv, struct oscap_action *action) |
| 523 | { |
| 524 | action->doctype = OSCAP_DOCUMENT_OVAL_DEFINITIONS; |
| 525 | |
| 526 | /* Command-options */ |
| 527 | struct option long_options[] = { |
| 528 | { "results", required_argument, NULL, OVAL_OPT_RESULT_FILE }, |
| 529 | { "report", required_argument, NULL, OVAL_OPT_REPORT_FILE }, |
| 530 | { "id", required_argument, NULL, OVAL_OPT_ID }, |
| 531 | { "variables", required_argument, NULL, OVAL_OPT_VARIABLES }, |
| 532 | { "directives", required_argument, NULL, OVAL_OPT_DIRECTIVES }, |
| 533 | { "without-syschar", no_argument, &action->without_sys_chars, 1}, |
| 534 | { "datastream-id",required_argument, NULL, OVAL_OPT_DATASTREAM_ID}, |
| 535 | { "oval-id", required_argument, NULL, OVAL_OPT_OVAL_ID}, |
| 536 | { "skip-validation", no_argument, &action->validate, 0 }, |
| 537 | { "fetch-remote-resources", no_argument, &action->remote_resources, 1}, |
| 538 | { "local-files", required_argument, NULL, OVAL_OPT_LOCAL_FILES}, |
| 539 | { "verbose", required_argument, NULL, OVAL_OPT_VERBOSE}, |
| 540 | { "verbose-log-file", required_argument, NULL, OVAL_OPT_VERBOSE_LOG_FILE}, |
| 541 | { 0, 0, 0, 0 } |
| 542 | }; |
| 543 | |
| 544 | int c; |
| 545 | while ((c = getopt_long(argc, argv, "o:", long_options, NULL)) != -1) { |
| 546 | switch (c) { |
| 547 | case OVAL_OPT_RESULT_FILE: action->f_results = optarg; break; |
| 548 | case OVAL_OPT_REPORT_FILE: action->f_report = optarg; break; |
| 549 | case OVAL_OPT_ID: action->id = optarg; break; |
| 550 | case OVAL_OPT_VARIABLES: action->f_variables = optarg; break; |
| 551 | case OVAL_OPT_DIRECTIVES: action->f_directives = optarg; break; |
| 552 | case OVAL_OPT_DATASTREAM_ID: action->f_datastream_id = optarg; break; |
| 553 | case OVAL_OPT_OVAL_ID: action->f_oval_id = optarg; break; |
| 554 | case OVAL_OPT_LOCAL_FILES: |
| 555 | action->local_files = optarg; |
| 556 | break; |
| 557 | case OVAL_OPT_VERBOSE: |
| 558 | action->verbosity_level = optarg; |
| 559 | break; |
| 560 | case OVAL_OPT_VERBOSE_LOG_FILE: |
| 561 | action->f_verbose_log = optarg; |
| 562 | break; |
| 563 | case 0: break; |
| 564 | default: return oscap_module_usage(action->module, stderr, NULL); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /* We should have Definitions file here */ |
| 569 | if (optind >= argc) |
| 570 | return oscap_module_usage(action->module, stderr, "Definitions file is not specified!"); |
| 571 | action->f_oval = argv[optind]; |
| 572 | |
| 573 | return true; |
| 574 | } |
| 575 | #endif /* OVAL_PROBES_ENABLED */ |
| 576 | |
| 577 | #if defined(OVAL_PROBES_ENABLED) |
| 578 | bool getopt_oval_collect(int argc, char **argv, struct oscap_action *action) |
| 579 | { |
| 580 | action->doctype = OSCAP_DOCUMENT_OVAL_DEFINITIONS; |
| 581 | |
| 582 | /* Command-options */ |
| 583 | struct option long_options[] = { |
| 584 | { "id", required_argument, NULL, OVAL_OPT_ID }, |
| 585 | { "variables", required_argument, NULL, OVAL_OPT_VARIABLES }, |
| 586 | { "syschar", required_argument, NULL, OVAL_OPT_SYSCHAR }, |
| 587 | { "skip-validation", no_argument, &action->validate, 0 }, |
| 588 | { "verbose", required_argument, NULL, OVAL_OPT_VERBOSE}, |
| 589 | { "verbose-log-file", required_argument, NULL, OVAL_OPT_VERBOSE_LOG_FILE}, |
| 590 | { 0, 0, 0, 0 } |
| 591 | }; |
| 592 | |
| 593 | int c; |
| 594 | while ((c = getopt_long(argc, argv, "o:", long_options, NULL)) != -1) { |
| 595 | switch (c) { |
| 596 | case OVAL_OPT_ID: action->id = optarg; break; |
| 597 | case OVAL_OPT_VARIABLES: action->f_variables = optarg; break; |
| 598 | case OVAL_OPT_SYSCHAR: action->f_syschar = optarg; break; |
| 599 | case OVAL_OPT_VERBOSE: |
| 600 | action->verbosity_level = optarg; |
| 601 | break; |
| 602 | case OVAL_OPT_VERBOSE_LOG_FILE: |
| 603 | action->f_verbose_log = optarg; |
| 604 | break; |
| 605 | case 0: break; |
| 606 | default: return oscap_module_usage(action->module, stderr, NULL); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | /* We should have Definitions file here */ |
| 611 | if (optind >= argc) |
| 612 | return oscap_module_usage(action->module, stderr, "Definitions file is not specified!"); |
| 613 | action->f_oval = argv[optind]; |
| 614 | |
| 615 | return true; |
| 616 | } |
| 617 | #endif /* OVAL_PROBES_ENABLED */ |
| 618 | |
| 619 | bool getopt_oval_analyse(int argc, char **argv, struct oscap_action *action) |
| 620 | { |
| 621 | action->doctype = OSCAP_DOCUMENT_OVAL_DEFINITIONS; |
| 622 | |
| 623 | /* Command-options */ |
| 624 | struct option long_options[] = { |
| 625 | { "results", required_argument, NULL, OVAL_OPT_RESULT_FILE }, |
| 626 | { "variables", required_argument, NULL, OVAL_OPT_VARIABLES }, |
| 627 | { "directives", required_argument, NULL, OVAL_OPT_DIRECTIVES }, |
| 628 | { "skip-validation", no_argument, &action->validate, 0 }, |
| 629 | { "verbose", required_argument, NULL, OVAL_OPT_VERBOSE}, |
| 630 | { "verbose-log-file", required_argument, NULL, OVAL_OPT_VERBOSE_LOG_FILE}, |
| 631 | { 0, 0, 0, 0 } |
| 632 | }; |
| 633 | |
| 634 | int c; |
| 635 | while ((c = getopt_long(argc, argv, "o:", long_options, NULL)) != -1) { |
| 636 | switch (c) { |
| 637 | case OVAL_OPT_RESULT_FILE: action->f_results = optarg; break; |
| 638 | case OVAL_OPT_VARIABLES: action->f_variables = optarg; break; |
| 639 | case OVAL_OPT_DIRECTIVES: action->f_directives = optarg; break; |
| 640 | case OVAL_OPT_VERBOSE: |
| 641 | action->verbosity_level = optarg; |
| 642 | break; |
| 643 | case OVAL_OPT_VERBOSE_LOG_FILE: |
| 644 | action->f_verbose_log = optarg; |
| 645 | break; |
| 646 | case 0: break; |
| 647 | default: return oscap_module_usage(action->module, stderr, NULL); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /* We should have Definitions file here */ |
| 652 | if (optind >= argc) |
| 653 | return oscap_module_usage(action->module, stderr, "Definitions file is not specified!"); |
| 654 | action->f_oval = argv[optind]; |
| 655 | |
| 656 | if (action->module == &OVAL_ANALYSE) { |
| 657 | /* We should have System Characteristics file here */ |
| 658 | if ((optind+1) > argc) |
| 659 | return oscap_module_usage(action->module, stderr, "System characteristics file is not specified"); |
| 660 | action->f_syschar = argv[optind + 1]; |
| 661 | |
| 662 | if (action->f_results == NULL) { |
| 663 | return oscap_module_usage(action->module, stderr, "OVAL Results file is not specified(--results parameter)"); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | return true; |
| 668 | } |
| 669 | |
| 670 | bool getopt_oval_report(int argc, char **argv, struct oscap_action *action) |
| 671 | { |
| 672 | action->doctype = OSCAP_DOCUMENT_OVAL_DEFINITIONS; |
| 673 | |
| 674 | /* Command-options */ |
| 675 | struct option long_options[] = { |
| 676 | { "output", required_argument, NULL, OVAL_OPT_OUTPUT }, |
| 677 | { "verbose", required_argument, NULL, OVAL_OPT_VERBOSE}, |
| 678 | { "verbose-log-file", required_argument, NULL, OVAL_OPT_VERBOSE_LOG_FILE}, |
| 679 | { 0, 0, 0, 0 } |
| 680 | }; |
| 681 | |
| 682 | /* |
| 683 | * it is not nice that we use action->f_results for output and |
| 684 | * action->f_oval as input here. |
| 685 | */ |
| 686 | int c; |
| 687 | while ((c = getopt_long(argc, argv, "o:", long_options, NULL)) != -1) { |
| 688 | switch (c) { |
| 689 | case OVAL_OPT_OUTPUT: action->f_results = optarg; break; |
| 690 | case OVAL_OPT_VERBOSE: |
| 691 | action->verbosity_level = optarg; |
| 692 | break; |
| 693 | case OVAL_OPT_VERBOSE_LOG_FILE: |
| 694 | action->f_verbose_log = optarg; |
| 695 | break; |
| 696 | case 0: break; |
| 697 | default: return oscap_module_usage(action->module, stderr, NULL); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | /* We should have oval results file here */ |
| 702 | if (optind >= argc) |
| 703 | return oscap_module_usage(action->module, stderr, "Definitions file is not specified!"); |
| 704 | action->f_oval = argv[optind]; |
| 705 | |
| 706 | return true; |
| 707 | } |
| 708 | |
| 709 | bool getopt_oval_validate(int argc, char **argv, struct oscap_action *action) |
| 710 | { |
| 711 | /* we assume 0 is unknown */ |
| 712 | action->doctype = 0; |
| 713 | |
| 714 | /* Command-options */ |
| 715 | struct option long_options[] = { |
| 716 | // flags |
| 717 | { "definitions", no_argument, &action->doctype, OSCAP_DOCUMENT_OVAL_DEFINITIONS }, |
| 718 | { "variables", no_argument, &action->doctype, OSCAP_DOCUMENT_OVAL_VARIABLES }, |
| 719 | { "syschar", no_argument, &action->doctype, OSCAP_DOCUMENT_OVAL_SYSCHAR }, |
| 720 | { "results", no_argument, &action->doctype, OSCAP_DOCUMENT_OVAL_RESULTS }, |
| 721 | { "directives", no_argument, &action->doctype, OSCAP_DOCUMENT_OVAL_DIRECTIVES }, |
| 722 | { "skip-schematron",no_argument, &action->schematron, 0 }, |
| 723 | { "verbose", required_argument, NULL, OVAL_OPT_VERBOSE}, |
| 724 | { "verbose-log-file", required_argument, NULL, OVAL_OPT_VERBOSE_LOG_FILE}, |
| 725 | // end |
| 726 | { 0, 0, 0, 0 } |
| 727 | }; |
| 728 | |
| 729 | int c; |
| 730 | while ((c = getopt_long(argc, argv, "", long_options, NULL)) != -1) { |
| 731 | switch (c) { |
| 732 | case OVAL_OPT_VERBOSE: |
| 733 | action->verbosity_level = optarg; |
| 734 | break; |
| 735 | case OVAL_OPT_VERBOSE_LOG_FILE: |
| 736 | action->f_verbose_log = optarg; |
| 737 | break; |
| 738 | case 0: break; |
| 739 | default: return oscap_module_usage(action->module, stderr, NULL); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | /* we should have OVAL content here */ |
| 744 | if (optind >= argc) |
| 745 | return oscap_module_usage(action->module, stderr, "OVAL file needs to be specified!"); |
| 746 | action->f_oval = argv[optind]; |
| 747 | |
| 748 | return true; |
| 749 | } |
| 750 | |
| 751 | static bool valid_inputs(const struct oscap_action *action) { |
| 752 | bool result = true; |
| 753 | |
| 754 | /* validate SDS or OVAL Definitions & Variables & Syschars, |
| 755 | depending on the data */ |
| 756 | struct oscap_source *definitions_source = oscap_source_new_from_file(action->f_oval); |
| 757 | if (oscap_source_get_scap_type(definitions_source) != OSCAP_DOCUMENT_OVAL_DEFINITIONS && |
| 758 | oscap_source_get_scap_type(definitions_source) != OSCAP_DOCUMENT_SDS) { |
| 759 | fprintf(stderr, "Type mismatch: %s. Expecting OVAL Definition or source data stream, but found %s.\n", |
| 760 | action->f_oval, oscap_document_type_to_string(oscap_source_get_scap_type(definitions_source))); |
| 761 | result = false; |
| 762 | } |
| 763 | if (oscap_source_validate(definitions_source, reporter, (void *) action)) { |
| 764 | result = false; |
| 765 | } |
| 766 | oscap_source_free(definitions_source); |
| 767 | |
| 768 | if (action->f_variables) { |
| 769 | struct oscap_source *variables_source = oscap_source_new_from_file(action->f_variables); |
| 770 | if (oscap_source_get_scap_type(variables_source) != OSCAP_DOCUMENT_OVAL_VARIABLES) { |
| 771 | fprintf(stderr, "Type mismatch: %s. Expecting OVAL Variables, but found %s.\n", |
| 772 | action->f_variables, oscap_document_type_to_string(oscap_source_get_scap_type(variables_source))); |
| 773 | result = false; |
| 774 | } |
| 775 | if (oscap_source_validate(variables_source, reporter, (void *) action)) { |
| 776 | result = false; |
| 777 | } |
| 778 | oscap_source_free(variables_source); |
| 779 | } |
| 780 | |
| 781 | if (action->f_directives) { |
| 782 | struct oscap_source *directives_source = oscap_source_new_from_file(action->f_directives); |
| 783 | if (oscap_source_get_scap_type(directives_source) != OSCAP_DOCUMENT_OVAL_DIRECTIVES) { |
| 784 | fprintf(stderr, "Type mismatch: %s. Expecting OVAL Directives, but found %s.\n", |
| 785 | action->f_directives, oscap_document_type_to_string(oscap_source_get_scap_type(directives_source))); |
| 786 | result = false; |
| 787 | } |
| 788 | if (oscap_source_validate(directives_source, reporter, (void *) action)) { |
| 789 | result = false; |
| 790 | } |
| 791 | oscap_source_free(directives_source); |
| 792 | } |
| 793 | |
| 794 | if (action->module == &OVAL_ANALYSE && action->f_syschar) { |
| 795 | struct oscap_source *syschar_source = oscap_source_new_from_file(action->f_syschar); |
| 796 | if (oscap_source_get_scap_type(syschar_source) != OSCAP_DOCUMENT_OVAL_SYSCHAR) { |
| 797 | fprintf(stderr, "Type mismatch: %s. Expecting OVAL System Characteristic, but found %s.\n", |
| 798 | action->f_syschar, oscap_document_type_to_string(oscap_source_get_scap_type(syschar_source))); |
| 799 | result = false; |
| 800 | } |
| 801 | if (oscap_source_validate(syschar_source, reporter, (void *) action)) { |
| 802 | result = false; |
| 803 | } |
| 804 | oscap_source_free(syschar_source); |
| 805 | } |
| 806 | |
| 807 | return result; |
| 808 | } |
| 809 | |
| 810 | static int app_oval_validate(const struct oscap_action *action) { |
| 811 | int ret; |
| 812 | int result = OSCAP_OK; |
| 813 | |
| 814 | struct oscap_source *source = oscap_source_new_from_file(action->f_oval); |
| 815 | ret = oscap_source_validate(source, reporter, (void *) action); |
| 816 | |
| 817 | if (ret < 0) { |
| 818 | result = OSCAP_ERROR; |
| 819 | } else if (ret > 0) { |
| 820 | result = OSCAP_FAIL; |
| 821 | } else { |
| 822 | // We can only do schematron validation if the file isn't a source datastream |
| 823 | if (action->schematron && oscap_source_get_scap_type(source) != OSCAP_DOCUMENT_SDS) { |
| 824 | ret = oscap_source_validate_schematron(source); |
| 825 | if (ret < 0) { |
| 826 | result = OSCAP_ERROR; |
| 827 | } else if (ret > 0) { |
| 828 | result = OSCAP_FAIL; |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | oscap_source_free(source); |
| 834 | oscap_print_error(); |
| 835 | |
| 836 | return result; |
| 837 | } |
| 838 |