| 1 | /* |
| 2 | * Copyright 2012--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 | * Martin Preisler <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | /* Standard header files */ |
| 24 | #include <config.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #ifdef HAVE_GETOPT_H |
| 29 | #include <getopt.h> |
| 30 | #endif |
| 31 | #ifdef OS_WINDOWS |
| 32 | #include <io.h> |
| 33 | #else |
| 34 | #include <unistd.h> |
| 35 | #endif |
| 36 | |
| 37 | #ifndef PATH_MAX |
| 38 | #define PATH_MAX 4096 |
| 39 | #endif |
| 40 | |
| 41 | /* DS */ |
| 42 | #include <scap_ds.h> |
| 43 | #include <oscap_source.h> |
| 44 | #include <ds_rds_session.h> |
| 45 | #include <ds_sds_session.h> |
| 46 | #include <xccdf_session.h> |
| 47 | |
| 48 | #include "oscap-tool.h" |
| 49 | #include <oscap_debug.h> |
| 50 | #include "oscap_helpers.h" |
| 51 | |
| 52 | #define DS_SUBMODULES_NUM 8 /* See actual DS_SUBMODULES array |
| 53 | initialization below. */ |
| 54 | static struct oscap_module* DS_SUBMODULES[DS_SUBMODULES_NUM]; |
| 55 | bool getopt_ds(int argc, char **argv, struct oscap_action *action); |
| 56 | int app_ds_sds_split(const struct oscap_action *action); |
| 57 | int app_ds_sds_compose(const struct oscap_action *action); |
| 58 | int app_ds_sds_add(const struct oscap_action *action); |
| 59 | int app_ds_sds_validate(const struct oscap_action *action); |
| 60 | int app_ds_rds_split(const struct oscap_action *action); |
| 61 | int app_ds_rds_create(const struct oscap_action *action); |
| 62 | int app_ds_rds_validate(const struct oscap_action *action); |
| 63 | |
| 64 | struct oscap_module OSCAP_DS_MODULE = { |
| 65 | .name = "ds", |
| 66 | .parent = &OSCAP_ROOT_MODULE, |
| 67 | .summary = "Data stream utilities", |
| 68 | .submodules = DS_SUBMODULES |
| 69 | }; |
| 70 | |
| 71 | static struct oscap_module DS_SDS_VALIDATE_MODULE = { |
| 72 | .name = "sds-validate", |
| 73 | .parent = &OSCAP_DS_MODULE, |
| 74 | .summary = "Validate given source data stream", |
| 75 | .usage = "source_datastream.xml", |
| 76 | .help = "Options:\n" |
| 77 | " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n" |
| 78 | " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n" |
| 79 | " --verbose-log-file <file> - Write verbose information into file.\n", |
| 80 | .opt_parser = getopt_ds, |
| 81 | .func = app_ds_sds_validate |
| 82 | }; |
| 83 | |
| 84 | static struct oscap_module DS_RDS_VALIDATE_MODULE = { |
| 85 | .name = "rds-validate", |
| 86 | .parent = &OSCAP_DS_MODULE, |
| 87 | .summary = "Validate given result data stream", |
| 88 | .usage = "[options] result_datastream.xml", |
| 89 | .help = "Options:\n" |
| 90 | " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n" |
| 91 | " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n" |
| 92 | " --verbose-log-file <file> - Write verbose information into file.\n", |
| 93 | .opt_parser = getopt_ds, |
| 94 | .func = app_ds_rds_validate |
| 95 | }; |
| 96 | |
| 97 | static struct oscap_module* DS_SUBMODULES[DS_SUBMODULES_NUM] = { |
| 98 | &DS_SDS_VALIDATE_MODULE, |
| 99 | &DS_RDS_VALIDATE_MODULE, |
| 100 | NULL |
| 101 | }; |
| 102 | |
| 103 | enum ds_opt { |
| 104 | DS_OPT_DATASTREAM_ID = 1, |
| 105 | DS_OPT_XCCDF_ID, |
| 106 | DS_OPT_REPORT_ID, |
| 107 | DS_OPT_LOCAL_FILES, |
| 108 | DS_OPT_VERBOSE, |
| 109 | DS_OPT_VERBOSE_LOG_FILE, |
| 110 | }; |
| 111 | |
| 112 | bool getopt_ds(int argc, char **argv, struct oscap_action *action) { |
| 113 | action->doctype = OSCAP_DOCUMENT_SDS; |
| 114 | |
| 115 | /* Command-options */ |
| 116 | const struct option long_options[] = { |
| 117 | // options |
| 118 | {"skip-validation", no_argument, &action->validate, 0}, |
| 119 | {"datastream-id", required_argument, NULL, DS_OPT_DATASTREAM_ID}, |
| 120 | {"xccdf-id", required_argument, NULL, DS_OPT_XCCDF_ID}, |
| 121 | {"report-id", required_argument, NULL, DS_OPT_REPORT_ID}, |
| 122 | {"fetch-remote-resources", no_argument, &action->remote_resources, 1}, |
| 123 | {"local-files", required_argument, NULL, DS_OPT_LOCAL_FILES}, |
| 124 | {"verbose", required_argument, NULL, DS_OPT_VERBOSE}, |
| 125 | {"verbose-log-file", required_argument, NULL, DS_OPT_VERBOSE_LOG_FILE}, |
| 126 | // end |
| 127 | {0, 0, 0, 0} |
| 128 | }; |
| 129 | |
| 130 | int c; |
| 131 | while ((c = getopt_long(argc, argv, "o:i:", long_options, NULL)) != -1) { |
| 132 | |
| 133 | switch (c) { |
| 134 | case DS_OPT_DATASTREAM_ID: action->f_datastream_id = optarg; break; |
| 135 | case DS_OPT_XCCDF_ID: action->f_xccdf_id = optarg; break; |
| 136 | case DS_OPT_REPORT_ID: action->f_report_id = optarg; break; |
| 137 | case DS_OPT_LOCAL_FILES: |
| 138 | action->local_files = optarg; |
| 139 | break; |
| 140 | case DS_OPT_VERBOSE: |
| 141 | action->verbosity_level = optarg; |
| 142 | break; |
| 143 | case DS_OPT_VERBOSE_LOG_FILE: |
| 144 | action->f_verbose_log = optarg; |
| 145 | break; |
| 146 | case 0: break; |
| 147 | default: return oscap_module_usage(action->module, stderr, NULL); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if (action->module == &DS_SDS_VALIDATE_MODULE) { |
| 152 | if( argc != 4 ) { |
| 153 | oscap_module_usage(action->module, stderr, "Wrong number of parameters.\n"); |
| 154 | return false; |
| 155 | } |
| 156 | action->ds_action = malloc(sizeof(struct ds_action)); |
| 157 | action->ds_action->file = argv[3]; |
| 158 | } |
| 159 | else if (action->module == &DS_RDS_VALIDATE_MODULE) { |
| 160 | if(optind >= argc) { |
| 161 | oscap_module_usage(action->module, stderr, "Result data stream file need to be specified!\n"); |
| 162 | return false; |
| 163 | } |
| 164 | action->ds_action = malloc(sizeof(struct ds_action)); |
| 165 | action->ds_action->file = argv[optind]; |
| 166 | } |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | static inline char *_gcwd(void) |
| 171 | { |
| 172 | char *cwd = malloc(sizeof(char) * (PATH_MAX + 1)); |
| 173 | if (getcwd(cwd, PATH_MAX) == NULL) { |
| 174 | perror("Can't find out current working directory.\n"); |
| 175 | free(cwd); |
| 176 | cwd = NULL; |
| 177 | } |
| 178 | return cwd; |
| 179 | } |
| 180 | |
| 181 | int app_ds_sds_split(const struct oscap_action *action) { |
| 182 | int ret = OSCAP_ERROR; |
| 183 | const char* f_datastream_id = action->f_datastream_id; |
| 184 | const char* f_component_id = action->f_xccdf_id; |
| 185 | struct ds_sds_session *session = NULL; |
| 186 | |
| 187 | struct oscap_source *source = oscap_source_new_from_file(action->ds_action->file); |
| 188 | /* Validate */ |
| 189 | if (action->validate) |
| 190 | { |
| 191 | if (oscap_source_validate(source, reporter, (void *) action) != 0) { |
| 192 | goto cleanup; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | session = ds_sds_session_new_from_source(source); |
| 197 | if (session == NULL) { |
| 198 | goto cleanup; |
| 199 | } |
| 200 | if (ds_sds_index_select_checklist(ds_sds_session_get_sds_idx(session), &f_datastream_id, &f_component_id) != 0) { |
| 201 | fprintf(stdout, "Failed to locate a data stream with ID matching '%s' ID " |
| 202 | "and checklist inside matching '%s' ID.\n", |
| 203 | action->f_datastream_id == NULL ? "<any>" : action->f_datastream_id, |
| 204 | action->f_xccdf_id == NULL ? "<any>" : action->f_xccdf_id); |
| 205 | goto cleanup; |
| 206 | } |
| 207 | ds_sds_session_set_datastream_id(session, f_datastream_id); |
| 208 | |
| 209 | ds_sds_session_configure_remote_resources(session, action->remote_resources, action->local_files, download_reporting_callback); |
| 210 | ds_sds_session_set_target_dir(session, action->ds_action->target); |
| 211 | if (ds_sds_session_register_component_with_dependencies(session, "checklists", f_component_id, NULL) != 0) { |
| 212 | goto cleanup; |
| 213 | } |
| 214 | // CPE dictionaries aren't required in datastreams, just silently continue |
| 215 | // if we can't register them |
| 216 | if (ds_sds_session_can_register_component(session, "dictionaries", NULL)) { |
| 217 | if (ds_sds_session_register_component_with_dependencies(session, "dictionaries", NULL, NULL) != 0) { |
| 218 | goto cleanup; |
| 219 | } |
| 220 | } |
| 221 | if (ds_sds_session_dump_component_files(session) != 0) { |
| 222 | goto cleanup; |
| 223 | } |
| 224 | |
| 225 | ret = OSCAP_OK; |
| 226 | |
| 227 | cleanup: |
| 228 | oscap_print_error(); |
| 229 | |
| 230 | ds_sds_session_free(session); |
| 231 | oscap_source_free(source); |
| 232 | free(action->ds_action); |
| 233 | |
| 234 | return ret; |
| 235 | } |
| 236 | |
| 237 | int app_ds_sds_compose(const struct oscap_action *action) { |
| 238 | int ret = OSCAP_ERROR; |
| 239 | |
| 240 | // The API will correctly deal with the file parameter having directory |
| 241 | // references in it. However this will create a hard to navigate mangled |
| 242 | // component IDs in the resulting datastream. |
| 243 | // |
| 244 | // To fix this we will chdir to parent dir of the given XCCDF and chdir |
| 245 | // back after we are done. |
| 246 | |
| 247 | char *previous_cwd = _gcwd(); |
| 248 | if (previous_cwd == NULL) { |
| 249 | goto cleanup; |
| 250 | } |
| 251 | |
| 252 | char target_abs_path[PATH_MAX + 1]; |
| 253 | |
| 254 | // if the path is already absolute we just use it as it is |
| 255 | if (*action->ds_action->target == '/') |
| 256 | snprintf(target_abs_path, PATH_MAX, "%s", action->ds_action->target); |
| 257 | else |
| 258 | snprintf(target_abs_path, PATH_MAX, "%s/%s", previous_cwd, action->ds_action->target); |
| 259 | |
| 260 | char* temp_cwd = strdup(action->ds_action->file); |
| 261 | char *temp_cwd_dirname = oscap_dirname(temp_cwd); |
| 262 | if (chdir(temp_cwd_dirname) < 0) { |
| 263 | free(temp_cwd_dirname); |
| 264 | free(temp_cwd); |
| 265 | goto cleanup; |
| 266 | } |
| 267 | free(temp_cwd_dirname); |
| 268 | free(temp_cwd); |
| 269 | |
| 270 | char* source_xccdf = strdup(action->ds_action->file); |
| 271 | char *base_name = oscap_basename(source_xccdf); |
| 272 | ds_sds_compose_from_xccdf(base_name, target_abs_path); |
| 273 | free(base_name); |
| 274 | free(source_xccdf); |
| 275 | |
| 276 | if (chdir(previous_cwd) < 0) { |
| 277 | goto cleanup; |
| 278 | } |
| 279 | |
| 280 | if (action->validate) |
| 281 | { |
| 282 | struct oscap_source *source = oscap_source_new_from_file(target_abs_path); |
| 283 | if (oscap_source_validate(source, reporter, (void *) action) != 0) { |
| 284 | oscap_source_free(source); |
| 285 | goto cleanup; |
| 286 | } |
| 287 | oscap_source_free(source); |
| 288 | } |
| 289 | |
| 290 | ret = OSCAP_OK; |
| 291 | |
| 292 | cleanup: |
| 293 | oscap_print_error(); |
| 294 | |
| 295 | free(previous_cwd); |
| 296 | free(action->ds_action); |
| 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | int app_ds_sds_add(const struct oscap_action *action) |
| 301 | { |
| 302 | int ret = OSCAP_ERROR; |
| 303 | // TODO: chdir to the directory of the component (same as when composing new sds) |
| 304 | ret = ds_sds_compose_add_component(action->ds_action->target, action->f_datastream_id, action->ds_action->file, false); |
| 305 | if (action->validate) { |
| 306 | struct oscap_source *source = oscap_source_new_from_file(action->ds_action->file); |
| 307 | if (oscap_source_validate(source, reporter, (void *) action) != 0) { |
| 308 | ret = OSCAP_ERROR; |
| 309 | } |
| 310 | oscap_source_free(source); |
| 311 | } |
| 312 | oscap_print_error(); |
| 313 | |
| 314 | free(action->ds_action); |
| 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | int app_ds_sds_validate(const struct oscap_action *action) { |
| 319 | int ret = OSCAP_ERROR; |
| 320 | struct oscap_source *source = oscap_source_new_from_file(action->ds_action->file); |
| 321 | if (oscap_source_validate(source, reporter, (void*) action) != 0) { |
| 322 | goto cleanup; |
| 323 | } |
| 324 | |
| 325 | ret = OSCAP_OK; |
| 326 | |
| 327 | cleanup: |
| 328 | oscap_print_error(); |
| 329 | |
| 330 | free(action->ds_action); |
| 331 | oscap_source_free(source); |
| 332 | return ret; |
| 333 | } |
| 334 | |
| 335 | int app_ds_rds_split(const struct oscap_action *action) { |
| 336 | int ret = OSCAP_ERROR; |
| 337 | struct ds_rds_session *session = NULL; |
| 338 | |
| 339 | struct oscap_source *source = oscap_source_new_from_file(action->ds_action->file); |
| 340 | if (action->validate) |
| 341 | { |
| 342 | if (oscap_source_validate(source, reporter, (void *) action) != 0) { |
| 343 | goto cleanup; |
| 344 | } |
| 345 | } |
| 346 | session = ds_rds_session_new_from_source(source); |
| 347 | if (session == NULL |
| 348 | || ds_rds_session_set_target_dir(session, action->ds_action->target) != 0 |
| 349 | || ds_rds_session_select_report(session, action->f_report_id) == NULL |
| 350 | || ds_rds_session_select_report_request(session, NULL) == NULL |
| 351 | || ds_rds_session_dump_component_files(session) != 0) { |
| 352 | fprintf(stdout, "Failed to split given result data stream '%s'.\n", action->ds_action->file); |
| 353 | goto cleanup; |
| 354 | } |
| 355 | |
| 356 | ret = OSCAP_OK; |
| 357 | |
| 358 | cleanup: |
| 359 | oscap_print_error(); |
| 360 | |
| 361 | ds_rds_session_free(session); |
| 362 | free(action->ds_action); |
| 363 | oscap_source_free(source); |
| 364 | |
| 365 | return ret; |
| 366 | } |
| 367 | |
| 368 | int app_ds_rds_create(const struct oscap_action *action) { |
| 369 | int ret = OSCAP_ERROR; |
| 370 | |
| 371 | if (action->validate) |
| 372 | { |
| 373 | struct oscap_source *sds = oscap_source_new_from_file(action->ds_action->file); |
| 374 | if (oscap_source_validate(sds, reporter, (void *) action) != 0) { |
| 375 | oscap_source_free(sds); |
| 376 | goto cleanup; |
| 377 | } |
| 378 | oscap_source_free(sds); |
| 379 | |
| 380 | struct oscap_source *result = oscap_source_new_from_file(action->ds_action->xccdf_result); |
| 381 | if (oscap_source_validate(result, reporter, (void *) action) != 0) { |
| 382 | ret = OSCAP_ERROR; |
| 383 | oscap_source_free(result); |
| 384 | goto cleanup; |
| 385 | } |
| 386 | oscap_source_free(result); |
| 387 | } |
| 388 | |
| 389 | char** oval_result_files = malloc(sizeof(char*) * (action->ds_action->oval_result_count + 1)); |
| 390 | |
| 391 | size_t i; |
| 392 | for (i = 0; i < action->ds_action->oval_result_count; ++i) |
| 393 | { |
| 394 | oval_result_files[i] = action->ds_action->oval_results[i]; |
| 395 | |
| 396 | if (action->validate) |
| 397 | { |
| 398 | struct oscap_source *source = oscap_source_new_from_file(oval_result_files[i]); |
| 399 | if (oscap_source_validate(source, reporter, (void *) action) != 0) { |
| 400 | ret = OSCAP_ERROR; |
| 401 | oscap_source_free(source); |
| 402 | free(oval_result_files); |
| 403 | goto cleanup; |
| 404 | } |
| 405 | oscap_source_free(source); |
| 406 | } |
| 407 | } |
| 408 | oval_result_files[i] = NULL; |
| 409 | |
| 410 | ret = ds_rds_create(action->ds_action->file, action->ds_action->xccdf_result, |
| 411 | (const char**)oval_result_files, action->ds_action->target); |
| 412 | |
| 413 | free(oval_result_files); |
| 414 | |
| 415 | if (ret != 0) |
| 416 | { |
| 417 | fprintf(stdout, "Failed to create result data stream in ARF."); |
| 418 | ret = OSCAP_ERROR; |
| 419 | goto cleanup; |
| 420 | } |
| 421 | |
| 422 | const char* full_validation = getenv("OSCAP_FULL_VALIDATION"); |
| 423 | |
| 424 | if (action->validate && full_validation) |
| 425 | { |
| 426 | struct oscap_source *rds = oscap_source_new_from_file(action->ds_action->target); |
| 427 | if (oscap_source_validate(rds, reporter, (void *) action) != 0) { |
| 428 | oscap_source_free(rds); |
| 429 | goto cleanup; |
| 430 | } |
| 431 | oscap_source_free(rds); |
| 432 | } |
| 433 | |
| 434 | ret = OSCAP_OK; |
| 435 | |
| 436 | cleanup: |
| 437 | oscap_print_error(); |
| 438 | |
| 439 | free(action->ds_action); |
| 440 | return ret; |
| 441 | } |
| 442 | |
| 443 | int app_ds_rds_validate(const struct oscap_action *action) { |
| 444 | int ret = OSCAP_ERROR; |
| 445 | |
| 446 | struct oscap_source *rds = oscap_source_new_from_file(action->ds_action->file); |
| 447 | if (oscap_source_validate(rds, reporter, (void *) action) != 0) { |
| 448 | oscap_source_free(rds); |
| 449 | goto cleanup; |
| 450 | } |
| 451 | oscap_source_free(rds); |
| 452 | |
| 453 | ret = OSCAP_OK; |
| 454 | |
| 455 | cleanup: |
| 456 | oscap_print_error(); |
| 457 | |
| 458 | free(action->ds_action); |
| 459 | return ret; |
| 460 | } |