| 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 | * Peter Vrabec <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | /* Standard header files */ |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #ifdef HAVE_GETOPT_H |
| 32 | #include <getopt.h> |
| 33 | #endif |
| 34 | #include <assert.h> |
| 35 | #include <math.h> |
| 36 | #include <time.h> |
| 37 | #include <sys/types.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <limits.h> |
| 40 | |
| 41 | #include <oscap.h> |
| 42 | #include <oscap_helpers.h> |
| 43 | #include "oscap_source.h" |
| 44 | #include <xccdf_policy.h> |
| 45 | #include <oval_results.h> |
| 46 | #include <oval_variables.h> |
| 47 | #include <oval_system_characteristics.h> |
| 48 | #include <oval_directives.h> |
| 49 | #include <scap_ds.h> |
| 50 | #include <ds_rds_session.h> |
| 51 | #include <ds_sds_session.h> |
| 52 | #include <xccdf_session.h> |
| 53 | |
| 54 | #include "oscap-tool.h" |
| 55 | |
| 56 | |
| 57 | static bool getopt_info(int argc, char **argv, struct oscap_action *action); |
| 58 | static int app_info(const struct oscap_action *action); |
| 59 | |
| 60 | struct oscap_module OSCAP_INFO_MODULE = { |
| 61 | .name = "info", |
| 62 | .parent = &OSCAP_ROOT_MODULE, |
| 63 | .summary = "Print information about a SCAP file.", |
| 64 | .usage = "some-file.xml", |
| 65 | .help = "Options:\n" |
| 66 | " --fetch-remote-resources - Download remote content referenced by data stream.\n" |
| 67 | " --list-rules - Print selected rule IDs for the given profile (requires --profile).\n" |
| 68 | " --list-vars - Print XCCDF Value IDs and their resolved values for the given profile (requires --profile).\n" |
| 69 | " --local-files <dir> - Use locally downloaded copies of remote resources stored in the given directory.\n" |
| 70 | " --profile <id> - Show info of the profile with the given ID.\n" |
| 71 | " --profiles - Show profiles from the input file in the <id>:<title> format, one line per profile.\n" |
| 72 | " --references - Show references available in the input benchmark\n" |
| 73 | " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n" |
| 74 | " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n" |
| 75 | " --verbose-log-file <file> - Write verbose information into file.\n", |
| 76 | .opt_parser = getopt_info, |
| 77 | .func = app_info |
| 78 | }; |
| 79 | |
| 80 | static void print_time(const char *file) { |
| 81 | struct stat buffer; |
| 82 | struct tm result; |
| 83 | char timeStr[ 100 ] = ""; |
| 84 | |
| 85 | if(!stat(file, &buffer)) { |
| 86 | strftime(timeStr, 100, "%Y-%m-%dT%H:%M:%S", localtime_r(&buffer.st_mtime, &result)); |
| 87 | printf("Imported: %s\n", timeStr); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | static inline void _print_xccdf_status(struct xccdf_status *status, const char *prefix) |
| 92 | { |
| 93 | if (status) { |
| 94 | printf("%sStatus: %s\n", prefix, xccdf_status_type_to_text(xccdf_status_get_status(status))); |
| 95 | const time_t date_time = xccdf_status_get_date(status); |
| 96 | if (date_time != 0) { |
| 97 | struct tm result; |
| 98 | struct tm *date = localtime_r(&date_time, &result); |
| 99 | char date_str[] = "YYYY-DD-MM"; |
| 100 | int ret = snprintf(date_str, sizeof(date_str), "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday); |
| 101 | if (ret < 0) { |
| 102 | return; |
| 103 | } |
| 104 | printf("%sGenerated: %s\n", prefix, date_str); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static void _remove_occurence_of_character_from_string(char *string, char c) |
| 110 | { |
| 111 | if (string == NULL) |
| 112 | return; |
| 113 | |
| 114 | char *reading_ptr = string, *writing_ptr = string; |
| 115 | while (*reading_ptr) { |
| 116 | *writing_ptr = *reading_ptr; |
| 117 | if (*reading_ptr != c) |
| 118 | writing_ptr++; |
| 119 | reading_ptr++; |
| 120 | } |
| 121 | *writing_ptr = '\0'; |
| 122 | } |
| 123 | |
| 124 | static void _translate_character_in_string(char *string, char to_replace, char replace_with) |
| 125 | { |
| 126 | if (string == NULL) |
| 127 | return; |
| 128 | |
| 129 | char *string_iterator = string; |
| 130 | while (*string_iterator) { |
| 131 | if (*string_iterator == to_replace) |
| 132 | *string_iterator = replace_with; |
| 133 | string_iterator++; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static void _print_xccdf_profile_default(const struct xccdf_profile *prof, const char *prefix) |
| 138 | { |
| 139 | struct oscap_text_iterator *title_it = xccdf_profile_get_title(prof); |
| 140 | char *profile_title = oscap_textlist_get_preferred_plaintext(title_it, NULL); |
| 141 | oscap_text_iterator_free(title_it); |
| 142 | _remove_occurence_of_character_from_string(profile_title, '\n'); |
| 143 | printf("%s\tTitle: %s\n", prefix, profile_title); |
| 144 | free(profile_title); |
| 145 | printf("%s\t\tId: %s%s\n", prefix, |
| 146 | xccdf_profile_get_id(prof), |
| 147 | xccdf_profile_get_abstract(prof) ? " (abstract)" : ""); |
| 148 | } |
| 149 | |
| 150 | static void _print_xccdf_profile_verbose(const struct xccdf_profile *prof, const char *prefix) |
| 151 | { |
| 152 | struct oscap_text_iterator *text_it; |
| 153 | |
| 154 | printf("%sProfile\n", prefix); |
| 155 | |
| 156 | text_it = xccdf_profile_get_title(prof); |
| 157 | char *profile_title = oscap_textlist_get_preferred_plaintext(text_it, NULL); |
| 158 | oscap_text_iterator_free(text_it); |
| 159 | _remove_occurence_of_character_from_string(profile_title, '\n'); |
| 160 | printf("%s\tTitle: %s\n", prefix, profile_title); |
| 161 | free(profile_title); |
| 162 | |
| 163 | printf("%s\tId: %s%s\n\n", prefix, |
| 164 | xccdf_profile_get_id(prof), |
| 165 | xccdf_profile_get_abstract(prof) ? " (abstract)" : ""); |
| 166 | |
| 167 | text_it = xccdf_profile_get_description(prof); |
| 168 | char *profile_description = oscap_textlist_get_preferred_plaintext(text_it, NULL); |
| 169 | oscap_text_iterator_free(text_it); |
| 170 | _translate_character_in_string(profile_description, '\n', ' '); |
| 171 | printf("%s\tDescription: %s\n", prefix, profile_description); |
| 172 | free(profile_description); |
| 173 | } |
| 174 | |
| 175 | static void _print_xccdf_profile_terse(const struct xccdf_profile *prof, const char *prefix) |
| 176 | { |
| 177 | struct oscap_text_iterator *title_it = xccdf_profile_get_title(prof); |
| 178 | char *profile_title = oscap_textlist_get_preferred_plaintext(title_it, NULL); |
| 179 | _remove_occurence_of_character_from_string(profile_title, '\n'); |
| 180 | oscap_text_iterator_free(title_it); |
| 181 | const char *profile_id = xccdf_profile_get_id(prof); |
| 182 | char cleaned_profile_id[1024]; |
| 183 | strncpy(cleaned_profile_id, profile_id, sizeof(cleaned_profile_id) - 1); |
| 184 | _remove_occurence_of_character_from_string(cleaned_profile_id, '\n'); |
| 185 | printf("%s:%s\n", |
| 186 | cleaned_profile_id, |
| 187 | profile_title); |
| 188 | free(profile_title); |
| 189 | } |
| 190 | |
| 191 | static void _print_xccdf_profile_with_id(struct xccdf_profile *profile, const char *prefix) |
| 192 | { |
| 193 | if (profile == NULL) |
| 194 | return; |
| 195 | |
| 196 | _print_xccdf_profile_verbose(profile, prefix); |
| 197 | } |
| 198 | |
| 199 | static inline void _print_xccdf_profiles(struct xccdf_profile_iterator *prof_it, const char *prefix, void (*print_one_profile)(const struct xccdf_profile *, const char *)) |
| 200 | { |
| 201 | if (prefix) |
| 202 | printf("%sProfiles:\n", prefix); |
| 203 | |
| 204 | if (print_one_profile == NULL) |
| 205 | print_one_profile = &_print_xccdf_profile_default; |
| 206 | |
| 207 | while (xccdf_profile_iterator_has_more(prof_it)) { |
| 208 | struct xccdf_profile *prof = xccdf_profile_iterator_next(prof_it); |
| 209 | print_one_profile(prof, prefix); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | static inline void _print_xccdf_referenced_files(struct xccdf_policy_model *policy_model, const char *prefix) |
| 214 | { |
| 215 | struct oscap_file_entry_list *referenced_files = xccdf_policy_model_get_systems_and_files(policy_model); |
| 216 | struct oscap_file_entry_iterator *files_it = oscap_file_entry_list_get_files(referenced_files); |
| 217 | printf("%sReferenced check files:\n", prefix); |
| 218 | while (oscap_file_entry_iterator_has_more(files_it)) { |
| 219 | struct oscap_file_entry *file_entry = (struct oscap_file_entry *) oscap_file_entry_iterator_next(files_it); |
| 220 | const char *system = oscap_file_entry_get_system(file_entry); |
| 221 | if (strcmp(system, "http://open-scap.org/page/SCE")) { |
| 222 | printf("%s\t%s\n", prefix, oscap_file_entry_get_file(file_entry)); |
| 223 | printf("%s\t\tsystem: %s\n", prefix, oscap_file_entry_get_system(file_entry)); |
| 224 | } |
| 225 | } |
| 226 | oscap_file_entry_iterator_free(files_it); |
| 227 | oscap_file_entry_list_free(referenced_files); |
| 228 | } |
| 229 | |
| 230 | static inline void _print_xccdf_result(struct xccdf_result *xccdf_result, const char *prefix) |
| 231 | { |
| 232 | const char *result_id = xccdf_result_get_id(xccdf_result); |
| 233 | const char *benchmark_uri = xccdf_result_get_benchmark_uri(xccdf_result); |
| 234 | const char *xccdf_profile = xccdf_result_get_profile(xccdf_result); |
| 235 | if (xccdf_profile == NULL) { |
| 236 | xccdf_profile = "(default)"; |
| 237 | } |
| 238 | const char *start_time = xccdf_result_get_start_time(xccdf_result); |
| 239 | const char *end_time = xccdf_result_get_end_time(xccdf_result); |
| 240 | printf("%s\tResult ID: %s\n", prefix, result_id); |
| 241 | printf("%s\tSource benchmark: %s\n", prefix, benchmark_uri); |
| 242 | printf("%s\tSource profile: %s\n", prefix, xccdf_profile); |
| 243 | printf("%s\tEvaluation started: %s\n", prefix, start_time); |
| 244 | printf("%s\tEvaluation finished: %s\n", prefix, end_time); |
| 245 | printf("%s\tPlatform CPEs:\n", prefix); |
| 246 | struct oscap_string_iterator *platforms_it = xccdf_result_get_platforms(xccdf_result); |
| 247 | if (!oscap_string_iterator_has_more(platforms_it)) { |
| 248 | printf("%s\t\t(none)\n", prefix); |
| 249 | } |
| 250 | while (oscap_string_iterator_has_more(platforms_it)) { |
| 251 | const char *platform = oscap_string_iterator_next(platforms_it); |
| 252 | printf("%s\t\t%s\n", prefix, platform); |
| 253 | } |
| 254 | oscap_string_iterator_free(platforms_it); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | static inline void _print_xccdf_testresults(struct xccdf_benchmark *bench, const char *prefix) |
| 259 | { |
| 260 | struct xccdf_result_iterator *res_it = xccdf_benchmark_get_results(bench); |
| 261 | if (xccdf_result_iterator_has_more(res_it)) |
| 262 | printf("%sTest Results:\n", prefix); |
| 263 | while (xccdf_result_iterator_has_more(res_it)) { |
| 264 | struct xccdf_result *test_result = xccdf_result_iterator_next(res_it); |
| 265 | _print_xccdf_result(test_result, prefix); |
| 266 | } |
| 267 | xccdf_result_iterator_free(res_it); |
| 268 | } |
| 269 | |
| 270 | static void _print_references(struct xccdf_benchmark *bench, const char *prefix) |
| 271 | { |
| 272 | struct oscap_reference_iterator *it = xccdf_item_get_references((struct xccdf_item *)bench); |
| 273 | printf("%sReferences:\n", prefix); |
| 274 | if (!oscap_reference_iterator_has_more(it)) { |
| 275 | printf("%s\tNone\n", prefix); |
| 276 | } |
| 277 | while (oscap_reference_iterator_has_more(it)) { |
| 278 | struct oscap_reference *ref = oscap_reference_iterator_next(it); |
| 279 | const char *title = oscap_reference_get_title(ref); |
| 280 | const char *href = oscap_reference_get_href(ref); |
| 281 | printf("%s\t%s: %s\n", prefix, title, href); |
| 282 | } |
| 283 | oscap_reference_iterator_free(it); |
| 284 | } |
| 285 | |
| 286 | static inline void _print_xccdf_benchmark(struct xccdf_benchmark *bench, const char *prefix, bool print_references) |
| 287 | { |
| 288 | _print_xccdf_status(xccdf_benchmark_get_status_current(bench), prefix); |
| 289 | printf("%sResolved: %s\n", prefix, xccdf_benchmark_get_resolved(bench) ? "true" : "false"); |
| 290 | |
| 291 | struct xccdf_profile_iterator *prof_it = xccdf_benchmark_get_profiles(bench); |
| 292 | _print_xccdf_profiles(prof_it, prefix, NULL); |
| 293 | xccdf_profile_iterator_free(prof_it); |
| 294 | if (print_references) { |
| 295 | _print_references(bench, prefix); |
| 296 | } |
| 297 | |
| 298 | struct xccdf_policy_model *policy_model = xccdf_policy_model_new(bench); |
| 299 | _print_xccdf_referenced_files(policy_model, prefix); |
| 300 | _print_xccdf_testresults(bench, prefix); |
| 301 | |
| 302 | xccdf_policy_model_free(policy_model); |
| 303 | // xccdf_benchmark_free not needed, it si already freed by the policy! |
| 304 | } |
| 305 | |
| 306 | static inline void _print_xccdf_tailoring_header(struct xccdf_tailoring *tailoring, const char *prefix) |
| 307 | { |
| 308 | if (tailoring == NULL) { |
| 309 | return; |
| 310 | } |
| 311 | printf("%sBenchmark Hint: %s\n", prefix ? prefix : "", xccdf_tailoring_get_benchmark_ref(tailoring)); |
| 312 | } |
| 313 | |
| 314 | static inline void _print_xccdf_tailoring(struct oscap_source *source, const char *prefix, void (*print_one_profile)(const struct xccdf_profile *, const char *)) |
| 315 | { |
| 316 | struct xccdf_tailoring *tailoring = xccdf_tailoring_import_source(source, NULL); |
| 317 | _print_xccdf_tailoring_header(tailoring, prefix); |
| 318 | |
| 319 | struct xccdf_profile_iterator *prof_it = xccdf_tailoring_get_profiles(tailoring); |
| 320 | _print_xccdf_profiles(prof_it, prefix, print_one_profile); |
| 321 | xccdf_profile_iterator_free(prof_it); |
| 322 | |
| 323 | xccdf_tailoring_free(tailoring); |
| 324 | } |
| 325 | |
| 326 | static void _print_single_benchmark_profiles_only(struct xccdf_benchmark *bench) |
| 327 | { |
| 328 | struct xccdf_profile_iterator *prof_it = xccdf_benchmark_get_profiles(bench); |
| 329 | _print_xccdf_profiles(prof_it, 0, _print_xccdf_profile_terse); |
| 330 | xccdf_profile_iterator_free(prof_it); |
| 331 | } |
| 332 | |
| 333 | static void _print_single_benchmark_one_profile(struct xccdf_benchmark *bench, const char *profile_id) |
| 334 | { |
| 335 | struct xccdf_profile *profile = xccdf_benchmark_get_profile_by_id(bench, profile_id); |
| 336 | if (profile != NULL) |
| 337 | _print_xccdf_profile_with_id(profile, ""); |
| 338 | } |
| 339 | |
| 340 | static void _print_single_benchmark_all(struct xccdf_benchmark *bench, const char *prefix) |
| 341 | { |
| 342 | _print_xccdf_benchmark(bench, prefix, false); |
| 343 | // bench is freed as a side-effect of the function above |
| 344 | } |
| 345 | |
| 346 | static int app_info_single_ds_profiles_only(struct ds_stream_index_iterator* sds_it, struct ds_sds_session *session, const struct oscap_action *action) |
| 347 | { |
| 348 | struct ds_stream_index * stream = ds_stream_index_iterator_next(sds_it); |
| 349 | struct oscap_string_iterator* checklist_it = ds_stream_index_get_checklists(stream); |
| 350 | |
| 351 | while (oscap_string_iterator_has_more(checklist_it)) { |
| 352 | const char * id = oscap_string_iterator_next(checklist_it); |
| 353 | /* decompose */ |
| 354 | struct oscap_source *xccdf_source = ds_sds_session_select_checklist(session, ds_stream_index_get_id(stream), id, NULL); |
| 355 | if (xccdf_source == NULL) { |
| 356 | oscap_string_iterator_free(checklist_it); |
| 357 | ds_stream_index_iterator_free(sds_it); |
| 358 | ds_sds_session_free(session); |
| 359 | return OSCAP_ERROR; |
| 360 | } |
| 361 | |
| 362 | if (oscap_source_get_scap_type(xccdf_source) == OSCAP_DOCUMENT_XCCDF) { |
| 363 | struct xccdf_benchmark* bench = xccdf_benchmark_import_source(xccdf_source); |
| 364 | if(!bench) { |
| 365 | oscap_string_iterator_free(checklist_it); |
| 366 | ds_stream_index_iterator_free(sds_it); |
| 367 | ds_sds_session_free(session); |
| 368 | return OSCAP_ERROR; |
| 369 | } |
| 370 | _print_single_benchmark_profiles_only(bench); |
| 371 | xccdf_benchmark_free(bench); |
| 372 | |
| 373 | } else if (oscap_source_get_scap_type(xccdf_source) == OSCAP_DOCUMENT_XCCDF_TAILORING) { |
| 374 | _print_xccdf_tailoring(xccdf_source, 0, _print_xccdf_profile_terse); |
| 375 | } |
| 376 | ds_sds_session_reset(session); |
| 377 | } |
| 378 | oscap_string_iterator_free(checklist_it); |
| 379 | return OSCAP_OK; |
| 380 | } |
| 381 | |
| 382 | static const char *tailoring_get_profile_or_report_multiple_ids(struct xccdf_tailoring *tailoring, const char *profile_suffix, const char *source_file) |
| 383 | { |
| 384 | int match_status; |
| 385 | const char *result = xccdf_tailoring_match_profile_id(tailoring, profile_suffix, &match_status); |
| 386 | evaluate_suffix_match_result_with_custom_reports(match_status, profile_suffix, source_file, NULL, &report_multiple_profile_matches); |
| 387 | return result; |
| 388 | } |
| 389 | |
| 390 | static const char *benchmark_get_profile_or_report_multiple_ids(struct xccdf_benchmark *bench, const char *profile_suffix, const char *source_file) |
| 391 | { |
| 392 | int match_status; |
| 393 | const char *result = xccdf_benchmark_match_profile_id(bench, profile_suffix, &match_status); |
| 394 | evaluate_suffix_match_result_with_custom_reports(match_status, profile_suffix, source_file, NULL, &report_multiple_profile_matches); |
| 395 | return result; |
| 396 | } |
| 397 | |
| 398 | static const char *benchmark_get_profile_or_report_id_issues(struct xccdf_benchmark *bench, const char *profile_suffix, const char *source_file) |
| 399 | { |
| 400 | int match_status; |
| 401 | const char *result = xccdf_benchmark_match_profile_id(bench, profile_suffix, &match_status); |
| 402 | evaluate_suffix_match_result(match_status, profile_suffix, source_file); |
| 403 | return result; |
| 404 | } |
| 405 | |
| 406 | static void _print_rules_for_profile(struct xccdf_policy_model *policy_model, const char *profile_id) |
| 407 | { |
| 408 | struct xccdf_policy *policy = xccdf_policy_model_get_policy_by_id(policy_model, profile_id); |
| 409 | if (policy == NULL) { |
| 410 | return; |
| 411 | } |
| 412 | struct xccdf_select_iterator *sel_it = xccdf_policy_get_selected_rules(policy); |
| 413 | while (xccdf_select_iterator_has_more(sel_it)) { |
| 414 | const struct xccdf_select *sel = xccdf_select_iterator_next(sel_it); |
| 415 | printf("%s\n", xccdf_select_get_item(sel)); |
| 416 | } |
| 417 | xccdf_select_iterator_free(sel_it); |
| 418 | } |
| 419 | |
| 420 | static void _print_vars_for_profile(struct xccdf_policy_model *policy_model, const char *profile_id) |
| 421 | { |
| 422 | struct xccdf_policy *policy = xccdf_policy_model_get_policy_by_id(policy_model, profile_id); |
| 423 | if (policy == NULL) { |
| 424 | return; |
| 425 | } |
| 426 | const struct xccdf_profile *profile = xccdf_policy_get_profile(policy); |
| 427 | if (profile == NULL) { |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | const struct xccdf_benchmark *bench = xccdf_policy_model_get_benchmark(policy_model); |
| 432 | |
| 433 | struct xccdf_setvalue_iterator *sv_it = xccdf_profile_get_setvalues(profile); |
| 434 | while (xccdf_setvalue_iterator_has_more(sv_it)) { |
| 435 | const struct xccdf_setvalue *sv = xccdf_setvalue_iterator_next(sv_it); |
| 436 | const char *value_id = xccdf_setvalue_get_item(sv); |
| 437 | struct xccdf_item *item = xccdf_benchmark_get_item(bench, value_id); |
| 438 | if (item != NULL) { |
| 439 | const char *resolved = xccdf_policy_get_value_of_item(policy, item); |
| 440 | if (resolved != NULL) |
| 441 | printf("%s\t%s\n", value_id, resolved); |
| 442 | } |
| 443 | } |
| 444 | xccdf_setvalue_iterator_free(sv_it); |
| 445 | |
| 446 | struct xccdf_refine_value_iterator *rv_it = xccdf_profile_get_refine_values(profile); |
| 447 | while (xccdf_refine_value_iterator_has_more(rv_it)) { |
| 448 | const struct xccdf_refine_value *rv = xccdf_refine_value_iterator_next(rv_it); |
| 449 | const char *value_id = xccdf_refine_value_get_item(rv); |
| 450 | struct xccdf_item *item = xccdf_benchmark_get_item(bench, value_id); |
| 451 | if (item != NULL) { |
| 452 | const char *resolved = xccdf_policy_get_value_of_item(policy, item); |
| 453 | if (resolved != NULL) |
| 454 | printf("%s\t%s\n", value_id, resolved); |
| 455 | } |
| 456 | } |
| 457 | xccdf_refine_value_iterator_free(rv_it); |
| 458 | } |
| 459 | |
| 460 | static struct xccdf_benchmark *_find_benchmark_in_stream( |
| 461 | struct ds_sds_session *session, |
| 462 | struct ds_stream_index *stream) |
| 463 | { |
| 464 | const char *stream_id = ds_stream_index_get_id(stream); |
| 465 | struct oscap_string_iterator *bench_it = ds_stream_index_get_checklists(stream); |
| 466 | struct xccdf_benchmark *bench = NULL; |
| 467 | while (oscap_string_iterator_has_more(bench_it)) { |
| 468 | const char *cl_id = oscap_string_iterator_next(bench_it); |
| 469 | struct oscap_source *src = ds_sds_session_select_checklist(session, stream_id, cl_id, NULL); |
| 470 | if (src != NULL && oscap_source_get_scap_type(src) == OSCAP_DOCUMENT_XCCDF) { |
| 471 | bench = xccdf_benchmark_import_source(src); |
| 472 | break; |
| 473 | } |
| 474 | ds_sds_session_reset(session); |
| 475 | } |
| 476 | oscap_string_iterator_free(bench_it); |
| 477 | return bench; |
| 478 | } |
| 479 | |
| 480 | static struct xccdf_benchmark *_resolve_benchmark_for_tailoring( |
| 481 | struct oscap_source *tailoring_source, |
| 482 | const char *tailoring_filepath, |
| 483 | struct oscap_source **out_bench_source) |
| 484 | { |
| 485 | *out_bench_source = NULL; |
| 486 | |
| 487 | struct xccdf_tailoring *tailoring = xccdf_tailoring_import_source(tailoring_source, NULL); |
| 488 | if (tailoring == NULL) { |
| 489 | return NULL; |
| 490 | } |
| 491 | |
| 492 | const char *ref = xccdf_tailoring_get_benchmark_ref(tailoring); |
| 493 | char *benchmark_ref = ref ? strdup(ref) : NULL; |
| 494 | xccdf_tailoring_free(tailoring); |
| 495 | |
| 496 | if (benchmark_ref == NULL) { |
| 497 | fprintf(stderr, "The tailoring file doesn't contain a benchmark reference.\n"); |
| 498 | return NULL; |
| 499 | } |
| 500 | |
| 501 | // Extract #component_ref fragment from benchmark_ref, if present |
| 502 | const char *component_ref = NULL; |
| 503 | char *sep = strchr(benchmark_ref, '#'); |
| 504 | if (sep != NULL) { |
| 505 | component_ref = sep + 1; |
| 506 | *sep = '\0'; |
| 507 | } |
| 508 | |
| 509 | // Strip file:// prefix |
| 510 | const char *path_start = benchmark_ref; |
| 511 | if (strncmp(benchmark_ref, "file://", 7) == 0) { |
| 512 | path_start = benchmark_ref + 7; |
| 513 | } |
| 514 | |
| 515 | char *filepath_cpy = strdup(tailoring_filepath); |
| 516 | char *dir = oscap_dirname(filepath_cpy); |
| 517 | char *benchmark_path = path_start[0] == '/' ? |
| 518 | strdup(path_start) : oscap_sprintf("%s/%s", dir, path_start); |
| 519 | free(dir); |
| 520 | free(filepath_cpy); |
| 521 | |
| 522 | struct oscap_source *bench_source = oscap_source_new_from_file(benchmark_path); |
| 523 | free(benchmark_path); |
| 524 | if (bench_source == NULL) { |
| 525 | free(benchmark_ref); |
| 526 | return NULL; |
| 527 | } |
| 528 | |
| 529 | struct xccdf_benchmark *bench = NULL; |
| 530 | oscap_document_type_t doc_type = oscap_source_get_scap_type(bench_source); |
| 531 | if (doc_type == OSCAP_DOCUMENT_SDS) { |
| 532 | struct ds_sds_session *sds_session = ds_sds_session_new_from_source(bench_source); |
| 533 | if (sds_session == NULL) { |
| 534 | oscap_source_free(bench_source); |
| 535 | free(benchmark_ref); |
| 536 | return NULL; |
| 537 | } |
| 538 | if (component_ref != NULL) { |
| 539 | struct oscap_source *xccdf_source = ds_sds_session_select_checklist(sds_session, NULL, component_ref, NULL); |
| 540 | if (xccdf_source != NULL) { |
| 541 | bench = xccdf_benchmark_import_source(xccdf_source); |
| 542 | } |
| 543 | } else { |
| 544 | struct ds_sds_index *sds_index = ds_sds_session_get_sds_idx(sds_session); |
| 545 | struct ds_stream_index_iterator *streams = ds_sds_index_get_streams(sds_index); |
| 546 | if (ds_stream_index_iterator_has_more(streams)) { |
| 547 | struct ds_stream_index *stream = ds_stream_index_iterator_next(streams); |
| 548 | bench = _find_benchmark_in_stream(sds_session, stream); |
| 549 | } |
| 550 | ds_stream_index_iterator_free(streams); |
| 551 | } |
| 552 | ds_sds_session_free(sds_session); |
| 553 | } else { |
| 554 | bench = xccdf_benchmark_import_source(bench_source); |
| 555 | } |
| 556 | |
| 557 | free(benchmark_ref); |
| 558 | |
| 559 | if (bench == NULL) { |
| 560 | oscap_source_free(bench_source); |
| 561 | return NULL; |
| 562 | } |
| 563 | |
| 564 | *out_bench_source = bench_source; |
| 565 | return bench; |
| 566 | } |
| 567 | |
| 568 | static int _handle_xccdf_benchmark_profile( |
| 569 | struct xccdf_benchmark *bench, |
| 570 | const struct oscap_action *action, |
| 571 | const char *profile_suffix, |
| 572 | const char *filename, |
| 573 | bool *profile_found) |
| 574 | { |
| 575 | const char *profile_id = benchmark_get_profile_or_report_multiple_ids(bench, profile_suffix, filename); |
| 576 | if (profile_id == NULL) { |
| 577 | xccdf_benchmark_free(bench); |
| 578 | return OSCAP_OK; |
| 579 | } |
| 580 | if (action->list_rules || action->list_vars) { |
| 581 | struct xccdf_policy_model *policy_model = xccdf_policy_model_new(bench); |
| 582 | if (action->list_rules) { |
| 583 | _print_rules_for_profile(policy_model, profile_id); |
| 584 | } else { |
| 585 | _print_vars_for_profile(policy_model, profile_id); |
| 586 | } |
| 587 | xccdf_policy_model_free(policy_model); |
| 588 | } else { |
| 589 | _print_single_benchmark_one_profile(bench, profile_id); |
| 590 | xccdf_benchmark_free(bench); |
| 591 | } |
| 592 | *profile_found = true; |
| 593 | return OSCAP_OK; |
| 594 | } |
| 595 | |
| 596 | static int _handle_xccdf_tailoring_profile( |
| 597 | struct xccdf_tailoring *tailoring, |
| 598 | const struct oscap_action *action, |
| 599 | const char *profile_suffix, |
| 600 | const char *filename, |
| 601 | struct ds_sds_session *session, |
| 602 | struct ds_stream_index *stream, |
| 603 | const char *checklist_id, |
| 604 | bool *profile_found) |
| 605 | { |
| 606 | const char *profile_id = tailoring_get_profile_or_report_multiple_ids(tailoring, profile_suffix, filename); |
| 607 | if (profile_id == NULL) { |
| 608 | xccdf_tailoring_free(tailoring); |
| 609 | return OSCAP_OK; |
| 610 | } |
| 611 | if (!(action->list_rules || action->list_vars)) { |
| 612 | struct xccdf_profile *profile = xccdf_tailoring_get_profile_by_id(tailoring, profile_id); |
| 613 | _print_xccdf_profile_with_id(profile, ""); |
| 614 | xccdf_tailoring_free(tailoring); |
| 615 | *profile_found = true; |
| 616 | return OSCAP_OK; |
| 617 | } |
| 618 | char *profile_id_dup = strdup(profile_id); |
| 619 | xccdf_tailoring_free(tailoring); |
| 620 | |
| 621 | /* Find the XCCDF benchmark component in the stream */ |
| 622 | ds_sds_session_reset(session); |
| 623 | struct xccdf_benchmark *bench = _find_benchmark_in_stream(session, stream); |
| 624 | if (bench == NULL) { |
| 625 | fprintf(stderr, "Could not find a benchmark in the datastream.\n"); |
| 626 | free(profile_id_dup); |
| 627 | return OSCAP_ERROR; |
| 628 | } |
| 629 | |
| 630 | /* Re-select the tailoring component and import with benchmark */ |
| 631 | const char *stream_id = ds_stream_index_get_id(stream); |
| 632 | ds_sds_session_reset(session); |
| 633 | struct oscap_source *tail_source = ds_sds_session_select_checklist(session, stream_id, checklist_id, NULL); |
| 634 | struct xccdf_tailoring *resolved_tailoring = xccdf_tailoring_import_source(tail_source, bench); |
| 635 | |
| 636 | struct xccdf_policy_model *policy_model = xccdf_policy_model_new(bench); |
| 637 | xccdf_policy_model_set_tailoring(policy_model, resolved_tailoring); |
| 638 | |
| 639 | if (action->list_rules) { |
| 640 | _print_rules_for_profile(policy_model, profile_id_dup); |
| 641 | } else { |
| 642 | _print_vars_for_profile(policy_model, profile_id_dup); |
| 643 | } |
| 644 | xccdf_policy_model_free(policy_model); |
| 645 | free(profile_id_dup); |
| 646 | *profile_found = true; |
| 647 | return OSCAP_OK; |
| 648 | } |
| 649 | |
| 650 | static int app_info_single_ds_one_profile(struct ds_stream_index_iterator* sds_it, struct ds_sds_session *session, const struct oscap_action *action) |
| 651 | { |
| 652 | int ret = OSCAP_OK; |
| 653 | const char *profile_suffix = action->profile; |
| 654 | const char *filename = action->file; |
| 655 | struct ds_stream_index *stream = ds_stream_index_iterator_next(sds_it); |
| 656 | struct oscap_string_iterator *checklist_it = ds_stream_index_get_checklists(stream); |
| 657 | |
| 658 | if (!action->list_rules && !action->list_vars) { |
| 659 | printf("\nStream: %s\n", ds_stream_index_get_id(stream)); |
| 660 | printf("Generated: %s\n", ds_stream_index_get_timestamp(stream)); |
| 661 | printf("Version: %s\n", ds_stream_index_get_version(stream)); |
| 662 | } |
| 663 | bool profile_found = false; |
| 664 | |
| 665 | while (oscap_string_iterator_has_more(checklist_it) && !profile_found) { |
| 666 | const char *id = oscap_string_iterator_next(checklist_it); |
| 667 | |
| 668 | /* decompose */ |
| 669 | struct oscap_source *xccdf_source = ds_sds_session_select_checklist(session, ds_stream_index_get_id(stream), id, NULL); |
| 670 | if (xccdf_source == NULL) { |
| 671 | ret = OSCAP_ERROR; |
| 672 | goto cleanup; |
| 673 | } |
| 674 | |
| 675 | if (oscap_source_get_scap_type(xccdf_source) == OSCAP_DOCUMENT_XCCDF) { |
| 676 | struct xccdf_benchmark *bench = xccdf_benchmark_import_source(xccdf_source); |
| 677 | if (!bench) { |
| 678 | ret = OSCAP_ERROR; |
| 679 | goto cleanup; |
| 680 | } |
| 681 | ret = _handle_xccdf_benchmark_profile(bench, action, profile_suffix, filename, &profile_found); |
| 682 | } else if (oscap_source_get_scap_type(xccdf_source) == OSCAP_DOCUMENT_XCCDF_TAILORING) { |
| 683 | struct xccdf_tailoring *tailoring = xccdf_tailoring_import_source(xccdf_source, NULL); |
| 684 | if (!tailoring) { |
| 685 | ret = OSCAP_ERROR; |
| 686 | goto cleanup; |
| 687 | } |
| 688 | ret = _handle_xccdf_tailoring_profile(tailoring, action, profile_suffix, filename, session, stream, id, &profile_found); |
| 689 | } |
| 690 | if (ret != OSCAP_OK) { |
| 691 | goto cleanup; |
| 692 | } |
| 693 | ds_sds_session_reset(session); |
| 694 | } |
| 695 | if (!profile_found) { |
| 696 | report_missing_profile(profile_suffix, filename); |
| 697 | } |
| 698 | |
| 699 | cleanup: |
| 700 | oscap_string_iterator_free(checklist_it); |
| 701 | if (ret != OSCAP_OK) { |
| 702 | ds_stream_index_iterator_free(sds_it); |
| 703 | ds_sds_session_free(session); |
| 704 | } |
| 705 | return ret; |
| 706 | } |
| 707 | |
| 708 | static int app_info_single_ds_all(struct ds_stream_index_iterator* sds_it, struct ds_sds_session *session, const struct oscap_action *action) |
| 709 | { |
| 710 | const char *prefix = "\t\t"; |
| 711 | struct ds_stream_index * stream = ds_stream_index_iterator_next(sds_it); |
| 712 | struct oscap_string_iterator* checklist_it = ds_stream_index_get_checklists(stream); |
| 713 | |
| 714 | printf("\nStream: %s\n", ds_stream_index_get_id(stream)); |
| 715 | printf("Generated: %s\n", ds_stream_index_get_timestamp(stream)); |
| 716 | printf("Version: %s\n", ds_stream_index_get_version(stream)); |
| 717 | |
| 718 | printf("Checklists:\n"); |
| 719 | while (oscap_string_iterator_has_more(checklist_it)) { |
| 720 | const char * id = oscap_string_iterator_next(checklist_it); |
| 721 | printf("\tRef-Id: %s\n", id); |
| 722 | |
| 723 | /* decompose */ |
| 724 | struct oscap_source *xccdf_source = ds_sds_session_select_checklist(session, ds_stream_index_get_id(stream), id, NULL); |
| 725 | if (xccdf_source == NULL) { |
| 726 | oscap_string_iterator_free(checklist_it); |
| 727 | ds_stream_index_iterator_free(sds_it); |
| 728 | ds_sds_session_free(session); |
| 729 | return OSCAP_ERROR; |
| 730 | } |
| 731 | |
| 732 | if (oscap_source_get_scap_type(xccdf_source) == OSCAP_DOCUMENT_XCCDF) { |
| 733 | struct xccdf_benchmark* bench = xccdf_benchmark_import_source(xccdf_source); |
| 734 | if(!bench) { |
| 735 | oscap_string_iterator_free(checklist_it); |
| 736 | ds_stream_index_iterator_free(sds_it); |
| 737 | ds_sds_session_free(session); |
| 738 | return OSCAP_ERROR; |
| 739 | } |
| 740 | _print_xccdf_benchmark(bench, prefix, action->references); |
| 741 | // bench is freed as a side-effect of the function above |
| 742 | } else if (oscap_source_get_scap_type(xccdf_source) == OSCAP_DOCUMENT_XCCDF_TAILORING) { |
| 743 | _print_xccdf_tailoring(xccdf_source, prefix, 0); |
| 744 | } |
| 745 | ds_sds_session_reset(session); |
| 746 | } |
| 747 | oscap_string_iterator_free(checklist_it); |
| 748 | return OSCAP_OK; |
| 749 | } |
| 750 | |
| 751 | static void app_info_single_benchmark(struct xccdf_benchmark *bench, const struct oscap_action *action, struct oscap_source *source) |
| 752 | { |
| 753 | if (action->show_profiles_only) { |
| 754 | _print_single_benchmark_profiles_only(bench); |
| 755 | xccdf_benchmark_free(bench); |
| 756 | } else if (action->profile) { |
| 757 | const char *profile_id = benchmark_get_profile_or_report_id_issues(bench, action->profile, action->file); |
| 758 | if (profile_id != NULL) { |
| 759 | if (action->list_rules) { |
| 760 | struct xccdf_policy_model *policy_model = xccdf_policy_model_new(bench); |
| 761 | _print_rules_for_profile(policy_model, profile_id); |
| 762 | xccdf_policy_model_free(policy_model); |
| 763 | } else if (action->list_vars) { |
| 764 | struct xccdf_policy_model *policy_model = xccdf_policy_model_new(bench); |
| 765 | _print_vars_for_profile(policy_model, profile_id); |
| 766 | xccdf_policy_model_free(policy_model); |
| 767 | } else { |
| 768 | _print_single_benchmark_one_profile(bench, profile_id); |
| 769 | xccdf_benchmark_free(bench); |
| 770 | } |
| 771 | } else { |
| 772 | xccdf_benchmark_free(bench); |
| 773 | } |
| 774 | } else { |
| 775 | printf("Checklist version: %s\n", oscap_source_get_schema_version(source)); |
| 776 | print_time(action->file); |
| 777 | |
| 778 | _print_single_benchmark_all(bench, ""); |
| 779 | // bench is freed as a side-effect of the function above |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | static int app_info_single_ds(struct ds_stream_index_iterator* sds_it, struct ds_sds_session *session, const struct oscap_action *action) |
| 784 | { |
| 785 | int return_value; |
| 786 | if (action->show_profiles_only) { |
| 787 | return_value = app_info_single_ds_profiles_only(sds_it, session, action); |
| 788 | } else if (action->profile) { |
| 789 | return_value = app_info_single_ds_one_profile(sds_it, session, action); |
| 790 | } else { |
| 791 | return_value = app_info_single_ds_all(sds_it, session, action); |
| 792 | } |
| 793 | return return_value; |
| 794 | } |
| 795 | |
| 796 | static int app_info_sds(struct oscap_source *source, const struct oscap_action *action) |
| 797 | { |
| 798 | if (! action->provide_machine_readable_output) { |
| 799 | printf("Document type: Source Data Stream\n"); |
| 800 | print_time(action->file); |
| 801 | } |
| 802 | |
| 803 | struct ds_sds_session *session = ds_sds_session_new_from_source(source); |
| 804 | if (session == NULL) { |
| 805 | return OSCAP_ERROR; |
| 806 | } |
| 807 | |
| 808 | ds_sds_session_configure_remote_resources(session, action->remote_resources, action->local_files, download_reporting_callback); |
| 809 | |
| 810 | /* get collection */ |
| 811 | struct ds_sds_index *sds = ds_sds_session_get_sds_idx(session); |
| 812 | if (!sds) { |
| 813 | ds_sds_session_free(session); |
| 814 | return OSCAP_ERROR; |
| 815 | } |
| 816 | /* iterate over streams */ |
| 817 | struct ds_stream_index_iterator* sds_it = ds_sds_index_get_streams(sds); |
| 818 | while (ds_stream_index_iterator_has_more(sds_it)) { |
| 819 | if (app_info_single_ds(sds_it, session, action) == OSCAP_ERROR) |
| 820 | return OSCAP_ERROR; |
| 821 | } |
| 822 | ds_stream_index_iterator_free(sds_it); |
| 823 | ds_sds_session_free(session); |
| 824 | return OSCAP_OK; |
| 825 | } |
| 826 | |
| 827 | static int app_info(const struct oscap_action *action) |
| 828 | { |
| 829 | int result = OSCAP_ERROR; |
| 830 | |
| 831 | struct oscap_source *source = oscap_source_new_from_file(action->file); |
| 832 | |
| 833 | switch (oscap_source_get_scap_type(source)) { |
| 834 | case OSCAP_DOCUMENT_OVAL_DEFINITIONS: { |
| 835 | printf("Document type: OVAL Definitions\n"); |
| 836 | struct oval_definition_model *def_model = oval_definition_model_import_source(source); |
| 837 | if(!def_model) |
| 838 | goto cleanup; |
| 839 | struct oval_generator *gen = oval_definition_model_get_generator(def_model); |
| 840 | printf("OVAL version: %s\n", oval_generator_get_core_schema_version(gen)); |
| 841 | printf("Generated: %s\n", oval_generator_get_timestamp(gen)); |
| 842 | print_time(action->file); |
| 843 | oval_definition_model_free(def_model); |
| 844 | } |
| 845 | break; |
| 846 | case OSCAP_DOCUMENT_OVAL_VARIABLES: { |
| 847 | printf("Document type: OVAL Variables\n"); |
| 848 | struct oval_variable_model *var_model = oval_variable_model_import_source(source); |
| 849 | if(!var_model) |
| 850 | goto cleanup; |
| 851 | struct oval_generator *gen = oval_variable_model_get_generator(var_model); |
| 852 | printf("OVAL version: %s\n", oval_generator_get_core_schema_version(gen)); |
| 853 | printf("Generated: %s\n", oval_generator_get_timestamp(gen)); |
| 854 | print_time(action->file); |
| 855 | oval_variable_model_free(var_model); |
| 856 | } |
| 857 | break; |
| 858 | case OSCAP_DOCUMENT_OVAL_DIRECTIVES: { |
| 859 | printf("Document type: OVAL Directives\n"); |
| 860 | struct oval_directives_model *dir_model = oval_directives_model_new(); |
| 861 | int ret = oval_directives_model_import_source(dir_model, source); |
| 862 | if(ret) |
| 863 | goto cleanup; |
| 864 | struct oval_generator *gen = oval_directives_model_get_generator(dir_model); |
| 865 | printf("OVAL version: %s\n", oval_generator_get_core_schema_version(gen)); |
| 866 | printf("Generated: %s\n", oval_generator_get_timestamp(gen)); |
| 867 | print_time(action->file); |
| 868 | oval_directives_model_free(dir_model); |
| 869 | } |
| 870 | break; |
| 871 | case OSCAP_DOCUMENT_OVAL_SYSCHAR: { |
| 872 | printf("Document type: OVAL System Characteristics\n"); |
| 873 | struct oval_definition_model * def_model = oval_definition_model_new(); |
| 874 | struct oval_syschar_model * sys_model = oval_syschar_model_new(def_model); |
| 875 | int ret = oval_syschar_model_import_source(sys_model, source); |
| 876 | if(ret) |
| 877 | goto cleanup; |
| 878 | struct oval_generator *gen = oval_syschar_model_get_generator(sys_model); |
| 879 | printf("OVAL version: %s\n", oval_generator_get_core_schema_version(gen)); |
| 880 | printf("Generated: %s\n", oval_generator_get_timestamp(gen)); |
| 881 | print_time(action->file); |
| 882 | oval_syschar_model_free(sys_model); |
| 883 | oval_definition_model_free(def_model); |
| 884 | } |
| 885 | break; |
| 886 | case OSCAP_DOCUMENT_OVAL_RESULTS: { |
| 887 | printf("Document type: OVAL Results\n"); |
| 888 | struct oval_definition_model * def_model=oval_definition_model_new(); |
| 889 | struct oval_results_model * res_model = oval_results_model_new(def_model,NULL); |
| 890 | int ret = oval_results_model_import_source(res_model, source); |
| 891 | if(ret) |
| 892 | goto cleanup; |
| 893 | struct oval_generator *gen = oval_results_model_get_generator(res_model); |
| 894 | printf("OVAL version: %s\n", oval_generator_get_core_schema_version(gen)); |
| 895 | printf("Generated: %s\n", oval_generator_get_timestamp(gen)); |
| 896 | print_time(action->file); |
| 897 | oval_results_model_free(res_model); |
| 898 | oval_definition_model_free(def_model); |
| 899 | } |
| 900 | break; |
| 901 | case OSCAP_DOCUMENT_XCCDF: { |
| 902 | if (!action->provide_machine_readable_output) |
| 903 | printf("Document type: XCCDF Checklist\n"); |
| 904 | struct xccdf_benchmark* bench = xccdf_benchmark_import_source(source); |
| 905 | if(!bench) |
| 906 | goto cleanup; |
| 907 | app_info_single_benchmark(bench, action, source); |
| 908 | // bench is freed as a side-effect of the function above |
| 909 | } |
| 910 | break; |
| 911 | case OSCAP_DOCUMENT_CPE_LANGUAGE: { |
| 912 | printf("Document type: CPE Language\n"); |
| 913 | print_time(action->file); |
| 914 | } |
| 915 | break; |
| 916 | case OSCAP_DOCUMENT_CPE_DICTIONARY: { |
| 917 | printf("Document type: CPE Dictionary\n"); |
| 918 | struct cpe_dict_model *dict_model = cpe_dict_model_import_source(source); |
| 919 | if (!dict_model) |
| 920 | goto cleanup; |
| 921 | struct cpe_generator *gen = cpe_dict_model_get_generator(dict_model); |
| 922 | if (gen != NULL) { |
| 923 | printf("CPE version: %s\n", cpe_generator_get_schema_version(gen)); |
| 924 | printf("Generated: %s\n", cpe_generator_get_timestamp(gen)); |
| 925 | } |
| 926 | print_time(action->file); |
| 927 | cpe_dict_model_free(dict_model); |
| 928 | } |
| 929 | break; |
| 930 | case OSCAP_DOCUMENT_SDS: { |
| 931 | if (app_info_sds(source, action) == OSCAP_ERROR) |
| 932 | goto cleanup; |
| 933 | } |
| 934 | break; |
| 935 | case OSCAP_DOCUMENT_ARF: { |
| 936 | printf("Document type: Result Data Stream\n"); |
| 937 | print_time(action->file); |
| 938 | struct ds_rds_session *session = ds_rds_session_new_from_source(source); |
| 939 | if (session == NULL) { |
| 940 | goto cleanup; |
| 941 | } |
| 942 | struct rds_index *rds = ds_rds_session_get_rds_idx(session); |
| 943 | if (!rds) { |
| 944 | ds_rds_session_free(session); |
| 945 | goto cleanup; |
| 946 | } |
| 947 | |
| 948 | struct rds_asset_index_iterator* asset_it = rds_index_get_assets(rds); |
| 949 | while (rds_asset_index_iterator_has_more(asset_it)) { |
| 950 | struct rds_asset_index* asset = rds_asset_index_iterator_next(asset_it); |
| 951 | printf("\nAsset: %s\n", rds_asset_index_get_id(asset)); |
| 952 | |
| 953 | struct rds_report_index_iterator* report_it = rds_asset_index_get_reports(asset); |
| 954 | while (rds_report_index_iterator_has_more(report_it)) { |
| 955 | struct rds_report_index* report = rds_report_index_iterator_next(report_it); |
| 956 | struct rds_report_request_index* request = rds_report_index_get_request(report); |
| 957 | const char *report_request_id = rds_report_request_index_get_id(request); |
| 958 | const char *report_id = rds_report_index_get_id(report); |
| 959 | |
| 960 | struct oscap_source *report_source = ds_rds_session_select_report(session, report_id); |
| 961 | if (report_source == NULL) { |
| 962 | rds_report_index_iterator_free(report_it); |
| 963 | rds_asset_index_iterator_free(asset_it); |
| 964 | ds_rds_session_free(session); |
| 965 | goto cleanup; |
| 966 | } |
| 967 | oscap_document_type_t report_source_type = oscap_source_get_scap_type(report_source); |
| 968 | if (report_source_type != OSCAP_DOCUMENT_XCCDF) { |
| 969 | rds_report_index_iterator_free(report_it); |
| 970 | rds_asset_index_iterator_free(asset_it); |
| 971 | ds_rds_session_free(session); |
| 972 | oscap_source_free(report_source); |
| 973 | goto cleanup; |
| 974 | } |
| 975 | struct xccdf_result *xccdf_result = xccdf_result_import_source(report_source); |
| 976 | if (xccdf_result == NULL) { |
| 977 | rds_report_index_iterator_free(report_it); |
| 978 | rds_asset_index_iterator_free(asset_it); |
| 979 | ds_rds_session_free(session); |
| 980 | oscap_source_free(report_source); |
| 981 | goto cleanup; |
| 982 | } |
| 983 | printf("\tARF report: %s\n", report_id); |
| 984 | printf("\t\tReport request: %s\n", report_request_id); |
| 985 | _print_xccdf_result(xccdf_result, "\t"); |
| 986 | xccdf_result_free(xccdf_result); |
| 987 | // oscap_source_free is not needed, it is already freed by xccdf_result_free |
| 988 | |
| 989 | } |
| 990 | rds_report_index_iterator_free(report_it); |
| 991 | } |
| 992 | rds_asset_index_iterator_free(asset_it); |
| 993 | ds_rds_session_free(session); |
| 994 | } |
| 995 | break; |
| 996 | case OSCAP_DOCUMENT_XCCDF_TAILORING: |
| 997 | if (action->profile && (action->list_rules || action->list_vars)) { |
| 998 | struct oscap_source *bench_source = NULL; |
| 999 | struct xccdf_benchmark *bench = _resolve_benchmark_for_tailoring(source, action->file, &bench_source); |
| 1000 | if (bench == NULL) { |
| 1001 | goto cleanup; |
| 1002 | } |
| 1003 | struct xccdf_policy_model *policy_model = xccdf_policy_model_new(bench); |
| 1004 | /* Re-import tailoring with benchmark for proper profile resolution */ |
| 1005 | struct xccdf_tailoring *tailoring = xccdf_tailoring_import_source(source, bench); |
| 1006 | if (tailoring == NULL) { |
| 1007 | xccdf_policy_model_free(policy_model); |
| 1008 | oscap_source_free(bench_source); |
| 1009 | goto cleanup; |
| 1010 | } |
| 1011 | xccdf_policy_model_set_tailoring(policy_model, tailoring); |
| 1012 | const char *profile_id = tailoring_get_profile_or_report_multiple_ids(tailoring, action->profile, action->file); |
| 1013 | if (profile_id == NULL) { |
| 1014 | fprintf(stderr, |
| 1015 | "Profile '%s' not found in the tailoring file. Provide the customized profile ID. " |
| 1016 | "Get available profiles using:\n$ oscap info %s\n", action->profile, action->file); |
| 1017 | } else { |
| 1018 | if (action->list_rules) { |
| 1019 | _print_rules_for_profile(policy_model, profile_id); |
| 1020 | } else { |
| 1021 | _print_vars_for_profile(policy_model, profile_id); |
| 1022 | } |
| 1023 | } |
| 1024 | xccdf_policy_model_free(policy_model); |
| 1025 | oscap_source_free(bench_source); |
| 1026 | } else { |
| 1027 | printf("Document type: XCCDF Tailoring\n"); |
| 1028 | print_time(action->file); |
| 1029 | _print_xccdf_tailoring(source, "", 0); |
| 1030 | } |
| 1031 | break; |
| 1032 | case OSCAP_DOCUMENT_SCE_RESULT: |
| 1033 | printf("Document type: SCE Result File\n"); |
| 1034 | // Currently, we do not have any SCE result file parsing capabilities. |
| 1035 | break; |
| 1036 | case OSCAP_DOCUMENT_OCIL: |
| 1037 | printf("Document type: OCIL Definitions file\n"); |
| 1038 | // we don't support OCIL yet |
| 1039 | break; |
| 1040 | default: |
| 1041 | printf("Could not determine document type\n"); |
| 1042 | goto cleanup; |
| 1043 | break; |
| 1044 | } |
| 1045 | |
| 1046 | result = OSCAP_OK; |
| 1047 | |
| 1048 | cleanup: |
| 1049 | oscap_source_free(source); |
| 1050 | oscap_print_error(); |
| 1051 | |
| 1052 | return result; |
| 1053 | } |
| 1054 | |
| 1055 | bool getopt_info(int argc, char **argv, struct oscap_action *action) |
| 1056 | { |
| 1057 | assert(action != NULL); |
| 1058 | |
| 1059 | enum oscap_info_opts { |
| 1060 | OSCAP_INFO_OPT_REMOTE_RESOURCES, |
| 1061 | OSCAP_INFO_OPT_LIST_RULES, |
| 1062 | OSCAP_INFO_OPT_LIST_VARS, |
| 1063 | OSCAP_INFO_OPT_LOCAL_FILES, |
| 1064 | OSCAP_INFO_OPT_PROFILE, |
| 1065 | OSCAP_INFO_OPT_PROFILES, |
| 1066 | OSCAP_INFO_OPT_REFERENCES, |
| 1067 | OSCAP_INFO_OPT_VERBOSE, |
| 1068 | OSCAP_INFO_OPT_VERBOSE_LOG_FILE, |
| 1069 | }; |
| 1070 | /* Command-options */ |
| 1071 | const struct option long_options[] = { |
| 1072 | {"fetch-remote-resources", no_argument, &action->remote_resources, 1}, |
| 1073 | {"list-rules", no_argument, 0, OSCAP_INFO_OPT_LIST_RULES}, |
| 1074 | {"list-vars", no_argument, 0, OSCAP_INFO_OPT_LIST_VARS}, |
| 1075 | {"local-files", required_argument, NULL, OSCAP_INFO_OPT_LOCAL_FILES}, |
| 1076 | {"profile", required_argument, 0, OSCAP_INFO_OPT_PROFILE}, |
| 1077 | {"profiles", no_argument, 0, OSCAP_INFO_OPT_PROFILES}, |
| 1078 | {"references", no_argument, 0, OSCAP_INFO_OPT_REFERENCES}, |
| 1079 | {"verbose", required_argument, NULL, OSCAP_INFO_OPT_VERBOSE}, |
| 1080 | {"verbose-log-file", required_argument, NULL, OSCAP_INFO_OPT_VERBOSE_LOG_FILE}, |
| 1081 | // end |
| 1082 | {0, 0, 0, 0} |
| 1083 | }; |
| 1084 | |
| 1085 | int c; |
| 1086 | while ((c = getopt_long(argc, argv, "", long_options, NULL)) != -1) { |
| 1087 | switch(c) { |
| 1088 | case 0: break; |
| 1089 | case OSCAP_INFO_OPT_LIST_RULES: |
| 1090 | action->list_rules = 1; |
| 1091 | action->provide_machine_readable_output = 1; |
| 1092 | break; |
| 1093 | case OSCAP_INFO_OPT_LIST_VARS: |
| 1094 | action->list_vars = 1; |
| 1095 | action->provide_machine_readable_output = 1; |
| 1096 | break; |
| 1097 | case OSCAP_INFO_OPT_PROFILE: |
| 1098 | action->profile = optarg; |
| 1099 | break; |
| 1100 | case OSCAP_INFO_OPT_PROFILES: |
| 1101 | action->show_profiles_only = 1; |
| 1102 | action->provide_machine_readable_output = 1; |
| 1103 | break; |
| 1104 | case OSCAP_INFO_OPT_LOCAL_FILES: |
| 1105 | action->local_files = optarg; |
| 1106 | break; |
| 1107 | case OSCAP_INFO_OPT_REFERENCES: |
| 1108 | action->references = 1; |
| 1109 | break; |
| 1110 | case OSCAP_INFO_OPT_VERBOSE: |
| 1111 | action->verbosity_level = optarg; |
| 1112 | break; |
| 1113 | case OSCAP_INFO_OPT_VERBOSE_LOG_FILE: |
| 1114 | action->f_verbose_log = optarg; |
| 1115 | break; |
| 1116 | default: return oscap_module_usage(action->module, stderr, NULL); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | if (action->list_rules && action->profile == NULL) { |
| 1121 | oscap_module_usage(action->module, stderr, "The --list-rules option requires --profile.\n"); |
| 1122 | return false; |
| 1123 | } |
| 1124 | |
| 1125 | if (action->list_vars && action->profile == NULL) { |
| 1126 | oscap_module_usage(action->module, stderr, "The --list-vars option requires --profile.\n"); |
| 1127 | return false; |
| 1128 | } |
| 1129 | |
| 1130 | if (action->list_rules && action->list_vars) { |
| 1131 | oscap_module_usage(action->module, stderr, "The --list-rules and --list-vars options can't be used at the same time.\n"); |
| 1132 | return false; |
| 1133 | } |
| 1134 | |
| 1135 | if (optind >= argc) { |
| 1136 | oscap_module_usage(action->module, stderr, "SCAP file needs to be specified!\n"); |
| 1137 | return false; |
| 1138 | } |
| 1139 | |
| 1140 | action->file = argv[optind]; |
| 1141 | |
| 1142 | return true; |
| 1143 | } |