| 1 | /* |
| 2 | * Copyright 2013--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 | */ |
| 20 | #include <string.h> |
| 21 | #include <limits.h> |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <sys/stat.h> |
| 28 | #ifdef OS_WINDOWS |
| 29 | #include <io.h> |
| 30 | #else |
| 31 | #include <unistd.h> |
| 32 | #endif |
| 33 | |
| 34 | #include <oscap.h> |
| 35 | #include "oscap_source.h" |
| 36 | #include <cpe_lang.h> |
| 37 | #include <OVAL/public/oval_agent_api.h> |
| 38 | #include <OVAL/public/oval_agent_xccdf_api.h> |
| 39 | #include "common/oscap_acquire.h" |
| 40 | #include "common/util.h" |
| 41 | #include "common/list.h" |
| 42 | #include "common/oscapxml.h" |
| 43 | #include "common/_error.h" |
| 44 | #include "common/debug_priv.h" |
| 45 | #include "CPE/cpe_session_priv.h" |
| 46 | #include "DS/public/scap_ds.h" |
| 47 | #include "DS/public/ds_sds_session.h" |
| 48 | #include "DS/ds_sds_session_priv.h" |
| 49 | #include "DS/rds_priv.h" |
| 50 | #include "DS/sds_priv.h" |
| 51 | #include "OVAL/results/oval_results_impl.h" |
| 52 | #include "source/xslt_priv.h" |
| 53 | #include "source/signature_priv.h" |
| 54 | #include "XCCDF/xccdf_impl.h" |
| 55 | #include "XCCDF_POLICY/public/xccdf_policy.h" |
| 56 | #include "XCCDF_POLICY/xccdf_policy_priv.h" |
| 57 | #include "XCCDF_POLICY/xccdf_policy_model_priv.h" |
| 58 | #include "item.h" |
| 59 | #include "public/xccdf_session.h" |
| 60 | #include "XCCDF_POLICY/public/check_engine_plugin.h" |
| 61 | #include "oscap_helpers.h" |
| 62 | |
| 63 | struct oval_content_resource { |
| 64 | char *href; ///< Coresponds with xccdf:check-content-ref/\@href. |
| 65 | struct oscap_source *source; ///< The oscap_source representing the href resource |
| 66 | bool source_owned; ///< Indicates whether we need to dispose source property |
| 67 | }; |
| 68 | |
| 69 | struct xccdf_session { |
| 70 | const char *filename; ///< File name of SCAP (SDS or XCCDF) file for this session. |
| 71 | struct oscap_list *rules; |
| 72 | struct oscap_list *skip_rules; |
| 73 | const char *reference_parameter; |
| 74 | struct oscap_source *source; ///< Main source assigned with the main file (SDS or XCCDF) |
| 75 | char *temp_dir; ///< Temp directory used for decomposed component files. |
| 76 | struct { |
| 77 | struct oscap_source *source; ///< oscap_source representing the XCCDF file |
| 78 | struct xccdf_policy_model *policy_model;///< Active policy model. |
| 79 | char *profile_id; ///< Last selected profile. |
| 80 | struct xccdf_result *result; ///< XCCDF Result model. |
| 81 | float base_score; ///< Basec score of the latest evaluation. |
| 82 | struct oscap_source *result_source; ///< oscap_source for the exported XCCDF result |
| 83 | } xccdf; |
| 84 | struct { |
| 85 | struct ds_sds_session *session; ///< SDS Registry abstract structure |
| 86 | char *user_datastream_id; ///< Datastream id requested by user (only applicable for sds). |
| 87 | char *user_component_id; ///< Component id requested by user (only applicable for sds). |
| 88 | char *user_benchmark_id; ///< Benchmark id requested by user (only applicable for sds). |
| 89 | } ds; |
| 90 | struct { |
| 91 | bool fetch_remote_resources; ///< Allows download of remote resources (not applicable when user sets custom oval files) |
| 92 | const char *local_files; ///< Path to the directory where local copies of remote components are located |
| 93 | download_progress_calllback_t progress; ///< Callback to report progress of download. |
| 94 | struct oval_content_resource **custom_resources;///< OVAL files required by user |
| 95 | struct oval_content_resource **resources;///< OVAL files referenced from XCCDF |
| 96 | struct oval_agent_session **agents; ///< OVAL Agent Session |
| 97 | xccdf_policy_engine_eval_fn user_eval_fn;///< Custom OVAL engine callback |
| 98 | char *product_cpe; ///< CPE of scanner product. |
| 99 | struct oscap_source* arf_report; ///< ARF report |
| 100 | struct oscap_htable *result_sources; ///< mapping 'filepath' to oscap_source for OVAL results |
| 101 | struct oscap_htable *results_mapping; ///< mapping OVAL filename to filepath for OVAL results |
| 102 | struct oscap_htable *arf_report_mapping; ///< mapping OVAL filename to ARF report ID for OVAL results |
| 103 | } oval; |
| 104 | struct { |
| 105 | char *arf_file; ///< Path to ARF file to export |
| 106 | char *xccdf_file; ///< Path to XCCDF file to export |
| 107 | char *xccdf_stig_viewer_file; ///< Path to STIG Viewer XCCDF file to export |
| 108 | char *report_file; ///< Path to HTML file to export |
| 109 | bool oval_results; ///< Shall be the OVAL results files exported? |
| 110 | bool oval_variables; ///< Shall be the OVAL variable files exported? |
| 111 | bool check_engine_plugins_results; ///< Shall the check engine plugins results be exported? |
| 112 | bool without_sys_chars; ///< Shall system characteristics be exported? |
| 113 | bool thin_results; ///< Shall OVAL/ARF results be exported as THIN? Default is FULL |
| 114 | } export; ///< Settings of Session export |
| 115 | char *user_cpe; ///< Path to CPE dictionary required by user |
| 116 | struct { |
| 117 | struct oscap_source *user_file; ///< Tailoring file requested by the user |
| 118 | char *user_component_id; ///< Component ID of the Tailoring requested by the user |
| 119 | } tailoring; |
| 120 | bool validate; ///< False value indicates to skip any XSD validation. |
| 121 | bool full_validation; ///< True value indicates that every possible step will be validated by XSD. |
| 122 | bool validate_signature; ///< False value indicates to skip XML signature validation. |
| 123 | bool enforce_signature; ///< True value forces session to treat all XMLs without signature as invalid. |
| 124 | bool show_rule_details; ///< True value indicates that rule details will be shown. |
| 125 | struct oscap_signature_ctx *signature_ctx; ///< Paths to public keys, certificates, signature related info |
| 126 | |
| 127 | struct oscap_list *check_engine_plugins; ///< Extra non-OVAL check engines that may or may not have been loaded |
| 128 | xccdf_session_loading_flags_t loading_flags; ///< Load referenced files while loading XCCDF |
| 129 | }; |
| 130 | |
| 131 | static int _xccdf_session_autonegotiate_tailoring_file(struct xccdf_session *session, const char *original_path); |
| 132 | static void _oval_content_resources_free(struct oval_content_resource **resources); |
| 133 | static void _xccdf_session_free_oval_agents(struct xccdf_session *session); |
| 134 | static void _xccdf_session_free_oval_result_sources(struct xccdf_session *session); |
| 135 | |
| 136 | static const char *oscap_productname = "cpe:/a:open-scap:oscap"; |
| 137 | static const char *oval_sysname = "http://oval.mitre.org/XMLSchema/oval-definitions-5"; |
| 138 | |
| 139 | struct xccdf_session *xccdf_session_new_from_source(struct oscap_source *source) |
| 140 | { |
| 141 | if (source == NULL) { |
| 142 | return NULL; |
| 143 | } |
| 144 | const char *filename = oscap_source_get_filepath(source); |
| 145 | struct xccdf_session *session = (struct xccdf_session *) calloc(1, sizeof(struct xccdf_session)); |
| 146 | session->source = source; |
| 147 | oscap_document_type_t document_type = oscap_source_get_scap_type(session->source); |
| 148 | if (document_type == OSCAP_DOCUMENT_UNKNOWN) { |
| 149 | xccdf_session_free(session); |
| 150 | return NULL; |
| 151 | } |
| 152 | if (document_type != OSCAP_DOCUMENT_XCCDF |
| 153 | && document_type != OSCAP_DOCUMENT_SDS |
| 154 | && document_type != OSCAP_DOCUMENT_XCCDF_TAILORING) { |
| 155 | oscap_seterr(OSCAP_EFAMILY_OSCAP, |
| 156 | "Session input file was determined but it isn't an XCCDF file, " |
| 157 | "a source datastream or an XCCDF tailoring file."); |
| 158 | xccdf_session_free(session); |
| 159 | return NULL; |
| 160 | } |
| 161 | session->validate = true; |
| 162 | session->validate_signature = true; |
| 163 | session->enforce_signature = false; |
| 164 | session->show_rule_details = false; |
| 165 | session->signature_ctx = oscap_signature_ctx_new(); |
| 166 | session->xccdf.base_score = 0; |
| 167 | session->oval.progress = download_progress_empty_calllback; |
| 168 | session->check_engine_plugins = oscap_list_new(); |
| 169 | session->loading_flags = XCCDF_SESSION_LOAD_ALL; |
| 170 | session->rules = oscap_list_new(); |
| 171 | session->skip_rules = oscap_list_new(); |
| 172 | |
| 173 | // We now have to switch up the oscap_sources in case we were given XCCDF tailoring |
| 174 | |
| 175 | if (document_type == OSCAP_DOCUMENT_XCCDF_TAILORING) { |
| 176 | if (_xccdf_session_autonegotiate_tailoring_file(session, filename) != 0) { |
| 177 | xccdf_session_free(session); |
| 178 | return NULL; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | dI("Created a new XCCDF session from a %s '%s'.", |
| 183 | oscap_document_type_to_string(document_type), filename); |
| 184 | return session; |
| 185 | } |
| 186 | |
| 187 | struct xccdf_session *xccdf_session_new(const char *filename) |
| 188 | { |
| 189 | struct oscap_source *source = oscap_source_new_from_file(filename); |
| 190 | struct xccdf_session *session = xccdf_session_new_from_source(source); |
| 191 | return session; |
| 192 | } |
| 193 | |
| 194 | static int _xccdf_session_autonegotiate_tailoring_file(struct xccdf_session *session, const char *original_path) |
| 195 | { |
| 196 | struct xccdf_tailoring* tailoring = xccdf_tailoring_import_source(session->source, NULL); |
| 197 | |
| 198 | if (tailoring == NULL) { |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | char *source_path = oscap_strdup(xccdf_tailoring_get_benchmark_ref(tailoring)); |
| 203 | xccdf_tailoring_free(tailoring); |
| 204 | |
| 205 | if (source_path == NULL) { |
| 206 | oscap_seterr(OSCAP_EFAMILY_OSCAP, |
| 207 | "Session input file was determined to be XCCDF tailoring file, " |
| 208 | "but it contained no benchmark reference!"); |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | char *original_path_cpy = oscap_strdup(original_path); |
| 213 | char *base_dir = oscap_dirname(original_path_cpy); |
| 214 | |
| 215 | char *real_source_path = source_path[0] == '/' ? |
| 216 | oscap_strdup(source_path) : oscap_sprintf("%s/%s", base_dir, source_path); |
| 217 | free(base_dir); |
| 218 | |
| 219 | free(original_path_cpy); |
| 220 | free(source_path); |
| 221 | |
| 222 | struct oscap_source *real_source = oscap_source_new_from_file(real_source_path); |
| 223 | free(real_source_path); |
| 224 | |
| 225 | if (real_source == NULL) { |
| 226 | oscap_seterr(OSCAP_EFAMILY_OSCAP, |
| 227 | "Session input file was determined to be XCCDF tailoring file, " |
| 228 | "but the real source file could not be loaded."); |
| 229 | return -1; |
| 230 | } |
| 231 | |
| 232 | session->tailoring.user_file = session->source; |
| 233 | session->source = real_source; |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static void xccdf_session_unload_check_engine_plugins(struct xccdf_session *session); |
| 239 | |
| 240 | static struct oscap_source* xccdf_session_create_arf_source(struct xccdf_session *session) |
| 241 | { |
| 242 | if (session->oval.arf_report != NULL) { |
| 243 | return session->oval.arf_report; |
| 244 | } |
| 245 | |
| 246 | struct oscap_source *sds_source = NULL; |
| 247 | |
| 248 | if (xccdf_session_is_sds(session)) { |
| 249 | sds_source = session->source; |
| 250 | } else { |
| 251 | xmlDocPtr sds_doc = ds_sds_compose_xmlDoc_from_xccdf_source(session->source); |
| 252 | sds_source = oscap_source_new_from_xmlDoc(sds_doc, NULL); |
| 253 | } |
| 254 | |
| 255 | session->oval.arf_report = ds_rds_create_source(sds_source, session->tailoring.user_file, session->xccdf.result_source, session->oval.result_sources, session->oval.results_mapping, session->oval.arf_report_mapping, session->export.arf_file); |
| 256 | if (!xccdf_session_is_sds(session)) { |
| 257 | oscap_source_free(sds_source); |
| 258 | } |
| 259 | return session->oval.arf_report; |
| 260 | } |
| 261 | |
| 262 | static struct oscap_source *xccdf_session_extract_arf_source(struct xccdf_session *session) |
| 263 | { |
| 264 | struct oscap_source *rds_source = NULL; |
| 265 | char *tailoring_doc_timestamp = NULL; |
| 266 | xmlDoc *sds_doc = NULL; |
| 267 | |
| 268 | if (xccdf_session_is_sds(session)) { |
| 269 | sds_doc = oscap_source_pop_xmlDoc(session->source); |
| 270 | } else { |
| 271 | sds_doc = ds_sds_compose_xmlDoc_from_xccdf_source(session->source); |
| 272 | } |
| 273 | oscap_source_free(session->source); |
| 274 | session->source = NULL; |
| 275 | |
| 276 | if (sds_doc == NULL) { |
| 277 | goto cleanup; |
| 278 | } |
| 279 | |
| 280 | xmlDoc *result_file_doc = oscap_source_get_xmlDoc(session->xccdf.result_source); |
| 281 | if (result_file_doc == NULL) { |
| 282 | goto cleanup; |
| 283 | } |
| 284 | |
| 285 | xmlDoc *tailoring_doc = NULL; |
| 286 | const char *tailoring_filepath = NULL; |
| 287 | if (session->tailoring.user_file) { |
| 288 | tailoring_doc = oscap_source_get_xmlDoc(session->tailoring.user_file); |
| 289 | if (tailoring_doc == NULL) { |
| 290 | goto cleanup; |
| 291 | } |
| 292 | tailoring_filepath = oscap_source_get_filepath(session->tailoring.user_file); |
| 293 | struct stat file_stat; |
| 294 | if (stat(tailoring_filepath, &file_stat) == 0) { |
| 295 | const size_t max_timestamp_len = 32; |
| 296 | tailoring_doc_timestamp = malloc(max_timestamp_len); |
| 297 | if (tailoring_doc_timestamp == NULL) { |
| 298 | oscap_seterr(OSCAP_EFAMILY_GLIBC, "Failed to allocate %zu bytes for tailoring_doc_timestamp: %s", max_timestamp_len, strerror(errno)); |
| 299 | goto cleanup; |
| 300 | } |
| 301 | struct tm *tm_mtime = malloc(sizeof(struct tm)); |
| 302 | #ifdef OS_WINDOWS |
| 303 | localtime_s(tm_mtime, &file_stat.st_mtime); |
| 304 | #else |
| 305 | localtime_r(&file_stat.st_mtime, tm_mtime); |
| 306 | #endif |
| 307 | strftime(tailoring_doc_timestamp, max_timestamp_len, |
| 308 | "%Y-%m-%dT%H:%M:%S", tm_mtime); |
| 309 | free(tm_mtime); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | xmlDocPtr rds_doc = NULL; |
| 314 | |
| 315 | if (ds_rds_create_from_dom(&rds_doc, sds_doc, tailoring_doc, |
| 316 | tailoring_filepath, tailoring_doc_timestamp, result_file_doc, |
| 317 | session->oval.result_sources, session->oval.results_mapping, |
| 318 | session->oval.arf_report_mapping) != 0) { |
| 319 | goto cleanup; |
| 320 | } |
| 321 | |
| 322 | rds_source = oscap_source_new_from_xmlDoc(rds_doc, session->export.arf_file); |
| 323 | |
| 324 | cleanup: |
| 325 | free(tailoring_doc_timestamp); |
| 326 | xmlFreeDoc(sds_doc); |
| 327 | return rds_source; |
| 328 | } |
| 329 | |
| 330 | void xccdf_session_free(struct xccdf_session *session) |
| 331 | { |
| 332 | if (session == NULL) |
| 333 | return; |
| 334 | free(session->xccdf.profile_id); |
| 335 | free(session->export.xccdf_file); |
| 336 | free(session->export.xccdf_stig_viewer_file); |
| 337 | free(session->export.report_file); |
| 338 | free(session->export.arf_file); |
| 339 | _xccdf_session_free_oval_result_sources(session); |
| 340 | xccdf_session_unload_check_engine_plugins(session); |
| 341 | oscap_list_free0(session->check_engine_plugins); |
| 342 | free(session->user_cpe); |
| 343 | free(session->oval.product_cpe); |
| 344 | _xccdf_session_free_oval_agents(session); |
| 345 | _oval_content_resources_free(session->oval.custom_resources); |
| 346 | _oval_content_resources_free(session->oval.resources); |
| 347 | oscap_source_free(session->oval.arf_report); |
| 348 | oscap_source_free(session->xccdf.result_source); |
| 349 | if (session->xccdf.policy_model != NULL) |
| 350 | xccdf_policy_model_free(session->xccdf.policy_model); |
| 351 | free(session->ds.user_datastream_id); |
| 352 | free(session->ds.user_component_id); |
| 353 | free(session->ds.user_benchmark_id); |
| 354 | ds_sds_session_free(session->ds.session); |
| 355 | if (session->temp_dir != NULL) |
| 356 | oscap_acquire_cleanup_dir((char **) &(session->temp_dir)); |
| 357 | oscap_source_free(session->source); |
| 358 | oscap_source_free(session->tailoring.user_file); |
| 359 | free(session->tailoring.user_component_id); |
| 360 | oscap_htable_free(session->oval.results_mapping, (oscap_destruct_func) free); |
| 361 | oscap_htable_free(session->oval.arf_report_mapping, (oscap_destruct_func) free); |
| 362 | oscap_signature_ctx_free(session->signature_ctx); |
| 363 | oscap_list_free(session->rules, (oscap_destruct_func) free); |
| 364 | oscap_list_free(session->skip_rules, (oscap_destruct_func) free); |
| 365 | free(session); |
| 366 | } |
| 367 | |
| 368 | static void _xccdf_session_reset_oval_agents_syschar(struct xccdf_session *session) |
| 369 | { |
| 370 | if (session->oval.agents != NULL) { |
| 371 | for (int i=0; session->oval.agents[i]; i++) { |
| 372 | oval_agent_reset_syschar(session->oval.agents[i]); |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | static void _xccdf_session_reset_oval_agents_results(struct xccdf_session *session) |
| 378 | { |
| 379 | if (session->oval.agents != NULL) { |
| 380 | for (int i=0; session->oval.agents[i]; i++) { |
| 381 | oval_agent_reset_results(session->oval.agents[i]); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | void xccdf_session_result_reset(struct xccdf_session *session) |
| 387 | { |
| 388 | if (session->xccdf.policy_model != NULL) { |
| 389 | oscap_list_free(session->xccdf.policy_model->policies, (oscap_destruct_func) xccdf_policy_free); |
| 390 | session->xccdf.policy_model->policies = oscap_list_new(); |
| 391 | } |
| 392 | |
| 393 | oscap_list_free(session->rules, (oscap_destruct_func) free); |
| 394 | session->rules = oscap_list_new(); |
| 395 | oscap_list_free(session->skip_rules, (oscap_destruct_func) free); |
| 396 | session->skip_rules = oscap_list_new(); |
| 397 | |
| 398 | _xccdf_session_reset_oval_agents_syschar(session); |
| 399 | _xccdf_session_reset_oval_agents_results(session); |
| 400 | } |
| 401 | |
| 402 | const char *xccdf_session_get_filename(const struct xccdf_session *session) |
| 403 | { |
| 404 | return oscap_source_readable_origin(session->source); |
| 405 | } |
| 406 | |
| 407 | bool xccdf_session_is_sds(const struct xccdf_session *session) |
| 408 | { |
| 409 | return oscap_source_get_scap_type(session->source) == OSCAP_DOCUMENT_SDS; |
| 410 | } |
| 411 | |
| 412 | void xccdf_session_set_rule(struct xccdf_session *session, const char *rule) |
| 413 | { |
| 414 | while (oscap_list_pop(session->rules, free)) |
| 415 | ; |
| 416 | oscap_list_add(session->rules, strdup(rule)); |
| 417 | } |
| 418 | |
| 419 | void xccdf_session_add_rule(struct xccdf_session *session, const char *rule) |
| 420 | { |
| 421 | oscap_list_add(session->rules, strdup(rule)); |
| 422 | } |
| 423 | |
| 424 | void xccdf_session_skip_rule(struct xccdf_session *session, const char *rule) |
| 425 | { |
| 426 | oscap_list_add(session->skip_rules, strdup(rule)); |
| 427 | } |
| 428 | |
| 429 | void xccdf_session_set_validation(struct xccdf_session *session, bool validate, bool full_validation) |
| 430 | { |
| 431 | session->validate = validate; |
| 432 | session->full_validation = full_validation; |
| 433 | } |
| 434 | |
| 435 | void xccdf_session_set_signature_validation(struct xccdf_session *session, bool validate) |
| 436 | { |
| 437 | session->validate_signature = validate; |
| 438 | } |
| 439 | |
| 440 | void xccdf_session_set_signature_enforcement(struct xccdf_session *session, bool enforce) |
| 441 | { |
| 442 | session->enforce_signature = enforce; |
| 443 | } |
| 444 | |
| 445 | void xccdf_session_set_thin_results(struct xccdf_session *session, bool thin_results) |
| 446 | { |
| 447 | session->export.thin_results = thin_results; |
| 448 | } |
| 449 | |
| 450 | void xccdf_session_set_datastream_id(struct xccdf_session *session, const char *datastream_id) |
| 451 | { |
| 452 | free(session->ds.user_datastream_id); |
| 453 | session->ds.user_datastream_id = oscap_strdup(datastream_id); |
| 454 | } |
| 455 | |
| 456 | const char *xccdf_session_get_datastream_id(struct xccdf_session *session) |
| 457 | { |
| 458 | if (session->ds.session != NULL) { |
| 459 | return ds_sds_session_get_datastream_id(session->ds.session); |
| 460 | } |
| 461 | return session->ds.user_datastream_id; |
| 462 | } |
| 463 | |
| 464 | void xccdf_session_set_component_id(struct xccdf_session *session, const char *component_id) |
| 465 | { |
| 466 | free(session->ds.user_component_id); |
| 467 | session->ds.user_component_id = oscap_strdup(component_id); |
| 468 | } |
| 469 | |
| 470 | const char *xccdf_session_get_component_id(struct xccdf_session *session) |
| 471 | { |
| 472 | if (session->ds.session != NULL) { |
| 473 | return ds_sds_session_get_checklist_id(session->ds.session); |
| 474 | } |
| 475 | return session->ds.user_component_id; |
| 476 | } |
| 477 | |
| 478 | void xccdf_session_set_benchmark_id(struct xccdf_session *session, const char *benchmark_id) |
| 479 | { |
| 480 | free(session->ds.user_benchmark_id); |
| 481 | session->ds.user_benchmark_id = oscap_strdup(benchmark_id); |
| 482 | } |
| 483 | |
| 484 | const char *xccdf_session_get_benchmark_id(struct xccdf_session *session) |
| 485 | { |
| 486 | return session->ds.user_benchmark_id; |
| 487 | } |
| 488 | |
| 489 | const char *xccdf_session_get_result_id(struct xccdf_session *session) |
| 490 | { |
| 491 | return xccdf_result_get_id(session->xccdf.result); |
| 492 | } |
| 493 | |
| 494 | void xccdf_session_set_user_cpe(struct xccdf_session *session, const char *user_cpe) |
| 495 | { |
| 496 | free(session->user_cpe); |
| 497 | session->user_cpe = oscap_strdup(user_cpe); |
| 498 | } |
| 499 | |
| 500 | void xccdf_session_set_user_tailoring_file(struct xccdf_session *session, const char *user_tailoring_file) |
| 501 | { |
| 502 | oscap_source_free(session->tailoring.user_file); |
| 503 | session->tailoring.user_file = user_tailoring_file != NULL ? |
| 504 | oscap_source_new_from_file(user_tailoring_file) : NULL; |
| 505 | } |
| 506 | |
| 507 | struct oscap_source *xccdf_session_get_user_tailoring_file(struct xccdf_session *session) |
| 508 | { |
| 509 | return session->tailoring.user_file; |
| 510 | } |
| 511 | |
| 512 | void xccdf_session_set_user_tailoring_cid(struct xccdf_session *session, const char *user_tailoring_cid) |
| 513 | { |
| 514 | free(session->tailoring.user_component_id); |
| 515 | session->tailoring.user_component_id = oscap_strdup(user_tailoring_cid); |
| 516 | } |
| 517 | |
| 518 | void xccdf_session_set_custom_oval_eval_fn(struct xccdf_session *session, xccdf_policy_engine_eval_fn eval_fn) |
| 519 | { |
| 520 | session->oval.user_eval_fn = eval_fn; |
| 521 | } |
| 522 | |
| 523 | bool xccdf_session_set_product_cpe(struct xccdf_session *session, const char *product_cpe) |
| 524 | { |
| 525 | free(session->oval.product_cpe); |
| 526 | session->oval.product_cpe = oscap_strdup(product_cpe); |
| 527 | return true; |
| 528 | } |
| 529 | |
| 530 | void xccdf_session_set_without_sys_chars_export(struct xccdf_session *session, bool without_sys_chars) |
| 531 | { |
| 532 | session->export.without_sys_chars = without_sys_chars; |
| 533 | } |
| 534 | |
| 535 | void xccdf_session_set_oval_results_export(struct xccdf_session *session, bool to_export_oval_results) |
| 536 | { |
| 537 | session->export.oval_results = to_export_oval_results; |
| 538 | } |
| 539 | |
| 540 | void xccdf_session_set_oval_variables_export(struct xccdf_session *session, bool to_export_oval_variables) |
| 541 | { |
| 542 | session->export.oval_variables = to_export_oval_variables; |
| 543 | } |
| 544 | |
| 545 | void xccdf_session_set_check_engine_plugins_results_export(struct xccdf_session *session, bool to_export_results) |
| 546 | { |
| 547 | session->export.check_engine_plugins_results = to_export_results; |
| 548 | } |
| 549 | |
| 550 | bool xccdf_session_set_arf_export(struct xccdf_session *session, const char *arf_file) |
| 551 | { |
| 552 | free(session->export.arf_file); |
| 553 | session->export.arf_file = oscap_strdup(arf_file); |
| 554 | return true; |
| 555 | } |
| 556 | |
| 557 | bool xccdf_session_set_xccdf_export(struct xccdf_session *session, const char *xccdf_file) |
| 558 | { |
| 559 | free(session->export.xccdf_file); |
| 560 | session->export.xccdf_file = oscap_strdup(xccdf_file); |
| 561 | return true; |
| 562 | } |
| 563 | |
| 564 | bool xccdf_session_set_xccdf_stig_viewer_export(struct xccdf_session *session, const char *xccdf_stig_viewer_file) |
| 565 | { |
| 566 | free(session->export.xccdf_stig_viewer_file); |
| 567 | session->export.xccdf_stig_viewer_file = oscap_strdup(xccdf_stig_viewer_file); |
| 568 | return true; |
| 569 | } |
| 570 | |
| 571 | bool xccdf_session_set_report_export(struct xccdf_session *session, const char *report_file) |
| 572 | { |
| 573 | free(session->export.report_file); |
| 574 | session->export.report_file = oscap_strdup(report_file); |
| 575 | return true; |
| 576 | } |
| 577 | |
| 578 | bool xccdf_session_set_profile_id(struct xccdf_session *session, const char *profile_id) |
| 579 | { |
| 580 | if (xccdf_policy_model_get_policy_by_id(session->xccdf.policy_model, profile_id) == NULL) |
| 581 | return false; |
| 582 | free(session->xccdf.profile_id); |
| 583 | session->xccdf.profile_id = oscap_strdup(profile_id); |
| 584 | return true; |
| 585 | } |
| 586 | |
| 587 | static const char *xccdf_profiles_match_profile_id(struct xccdf_profile_iterator *profile_it, const char *profile_suffix, int *match_status) |
| 588 | { |
| 589 | const char *full_profile_id = NULL; |
| 590 | bool multiple = false; |
| 591 | while (xccdf_profile_iterator_has_more(profile_it)) { |
| 592 | struct xccdf_profile *profile = xccdf_profile_iterator_next(profile_it); |
| 593 | const char *profile_id = xccdf_profile_get_id(profile); |
| 594 | |
| 595 | if(oscap_str_endswith(profile_id, profile_suffix)) { |
| 596 | if (full_profile_id != NULL) { |
| 597 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Multiple matches found:\n%s\n%s\n", |
| 598 | full_profile_id, profile_id); |
| 599 | multiple = true; |
| 600 | break; |
| 601 | } else { |
| 602 | full_profile_id = profile_id; |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | xccdf_profile_iterator_free(profile_it); |
| 607 | if (match_status != NULL) { |
| 608 | if (multiple) { |
| 609 | *match_status = OSCAP_PROFILE_MULTIPLE_MATCHES; |
| 610 | full_profile_id = NULL; |
| 611 | } else if (full_profile_id == NULL) { |
| 612 | *match_status = OSCAP_PROFILE_NO_MATCH; |
| 613 | } else { |
| 614 | *match_status = OSCAP_PROFILE_MATCH_OK; |
| 615 | } |
| 616 | } |
| 617 | return full_profile_id; |
| 618 | } |
| 619 | |
| 620 | const char *xccdf_tailoring_match_profile_id(struct xccdf_tailoring *tailoring, const char *profile_suffix, int *match_status) |
| 621 | { |
| 622 | struct xccdf_profile_iterator *profile_it = xccdf_tailoring_get_profiles(tailoring); |
| 623 | return xccdf_profiles_match_profile_id(profile_it, profile_suffix, match_status); |
| 624 | } |
| 625 | |
| 626 | const char *xccdf_benchmark_match_profile_id(struct xccdf_benchmark *bench, const char *profile_suffix, int *match_status) |
| 627 | { |
| 628 | struct xccdf_profile_iterator *profile_it = xccdf_benchmark_get_profiles(bench); |
| 629 | return xccdf_profiles_match_profile_id(profile_it, profile_suffix, match_status); |
| 630 | } |
| 631 | |
| 632 | int xccdf_session_set_profile_id_by_suffix(struct xccdf_session *session, const char *profile_suffix) |
| 633 | { |
| 634 | const char *full_profile_id = NULL; |
| 635 | struct xccdf_benchmark *bench = xccdf_policy_model_get_benchmark(session->xccdf.policy_model); |
| 636 | |
| 637 | // Tailoring Profiles |
| 638 | struct xccdf_tailoring *tailoring = xccdf_policy_model_get_tailoring(session->xccdf.policy_model); |
| 639 | int return_code = OSCAP_PROFILE_NO_MATCH; |
| 640 | if (tailoring != NULL) { |
| 641 | struct xccdf_profile_iterator *profit_tailoring = xccdf_tailoring_get_profiles(tailoring); |
| 642 | while (xccdf_profile_iterator_has_more(profit_tailoring)) { |
| 643 | struct xccdf_profile *tailoring_profile = xccdf_profile_iterator_next(profit_tailoring); |
| 644 | const char *tailoring_profile_id = xccdf_profile_get_id(tailoring_profile); |
| 645 | |
| 646 | if (oscap_str_endswith(tailoring_profile_id, profile_suffix)) { |
| 647 | if (full_profile_id != NULL) { |
| 648 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Multiple matches found:\n%s\n%s\n", |
| 649 | full_profile_id, tailoring_profile_id); |
| 650 | return_code = OSCAP_PROFILE_MULTIPLE_MATCHES; |
| 651 | break; |
| 652 | } else { |
| 653 | full_profile_id = tailoring_profile_id; |
| 654 | return_code = OSCAP_PROFILE_MATCH_OK; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | xccdf_profile_iterator_free(profit_tailoring); |
| 659 | } |
| 660 | |
| 661 | // Benchmark Profiles |
| 662 | if (return_code == OSCAP_PROFILE_NO_MATCH) { |
| 663 | full_profile_id = xccdf_benchmark_match_profile_id(bench, profile_suffix, &return_code); |
| 664 | } |
| 665 | |
| 666 | if (return_code == OSCAP_PROFILE_MATCH_OK) { |
| 667 | if (!xccdf_session_set_profile_id(session, full_profile_id)) { |
| 668 | return_code = OSCAP_PROFILE_NO_MATCH; |
| 669 | } |
| 670 | } |
| 671 | return return_code; |
| 672 | } |
| 673 | |
| 674 | const char *xccdf_session_get_profile_id(struct xccdf_session *session) |
| 675 | { |
| 676 | return session->xccdf.profile_id; |
| 677 | } |
| 678 | |
| 679 | static struct ds_sds_session *xccdf_session_get_ds_sds_session(struct xccdf_session *session) |
| 680 | { |
| 681 | if (!xccdf_session_is_sds(session)) |
| 682 | return NULL; |
| 683 | if (session->ds.session == NULL) { |
| 684 | session->ds.session = ds_sds_session_new_from_source(session->source); |
| 685 | } |
| 686 | return session->ds.session; |
| 687 | } |
| 688 | |
| 689 | |
| 690 | void xccdf_session_configure_remote_resources(struct xccdf_session *session, bool allowed, const char *local_files, download_progress_calllback_t callback) |
| 691 | { |
| 692 | if (callback == NULL) { |
| 693 | // With empty cb we don't have to check for NULL |
| 694 | // when we want to use it |
| 695 | callback = download_progress_empty_calllback; |
| 696 | } |
| 697 | |
| 698 | session->oval.fetch_remote_resources = allowed; |
| 699 | session->oval.local_files = local_files; |
| 700 | session->oval.progress = callback; |
| 701 | |
| 702 | if (xccdf_session_is_sds(session)) { |
| 703 | // We have to propagate this option to allow loading |
| 704 | // of external datastream components |
| 705 | ds_sds_session_configure_remote_resources(xccdf_session_get_ds_sds_session(session), allowed, local_files, callback); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | void xccdf_session_set_remote_resources(struct xccdf_session *session, bool allowed, download_progress_calllback_t callback) |
| 710 | { |
| 711 | xccdf_session_configure_remote_resources(session, allowed, NULL, callback); |
| 712 | } |
| 713 | |
| 714 | void xccdf_session_set_loading_flags(struct xccdf_session *session, xccdf_session_loading_flags_t flags) |
| 715 | { |
| 716 | session->loading_flags = flags; |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * Get Source DataStream index of the session. |
| 721 | * @memberof xccdf_session |
| 722 | * @warning This is applicable only on sessions which are SDS. |
| 723 | * @return sds index |
| 724 | */ |
| 725 | struct ds_sds_index *xccdf_session_get_sds_idx(struct xccdf_session *session) |
| 726 | { |
| 727 | if (!xccdf_session_is_sds(session)) |
| 728 | return NULL; |
| 729 | |
| 730 | return ds_sds_session_get_sds_idx(xccdf_session_get_ds_sds_session(session)); |
| 731 | } |
| 732 | |
| 733 | int xccdf_session_load(struct xccdf_session *session) |
| 734 | { |
| 735 | int ret = 0; |
| 736 | |
| 737 | if (session->ds.session) { |
| 738 | ds_sds_session_reset(session->ds.session); |
| 739 | } |
| 740 | |
| 741 | const xccdf_session_loading_flags_t flags = session->loading_flags; |
| 742 | if (flags & XCCDF_SESSION_LOAD_XCCDF) { |
| 743 | if ((ret = xccdf_session_load_xccdf(session)) != 0) |
| 744 | return ret; |
| 745 | } |
| 746 | if (flags & XCCDF_SESSION_LOAD_CPE) { |
| 747 | if ((ret = xccdf_session_load_cpe(session)) != 0) { |
| 748 | return ret; |
| 749 | } |
| 750 | } |
| 751 | if (flags & XCCDF_SESSION_LOAD_OVAL) { |
| 752 | if ((ret = xccdf_session_load_oval(session)) != 0) { |
| 753 | return ret; |
| 754 | } |
| 755 | } |
| 756 | if (flags & XCCDF_SESSION_LOAD_CHECK_ENGINE_PLUGINS) { |
| 757 | if ((ret = xccdf_session_load_check_engine_plugins(session)) != 0) { |
| 758 | return ret; |
| 759 | } |
| 760 | } |
| 761 | ret = xccdf_session_load_tailoring(session); |
| 762 | oscap_source_free_xmlDoc(session->source); |
| 763 | return ret; |
| 764 | } |
| 765 | |
| 766 | static int _reporter(const char *file, int line, const char *msg, void *arg) |
| 767 | { |
| 768 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "File '%s' line %d: %s", file, line, msg); |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | static inline int _xccdf_session_load_xccdf_benchmark(struct xccdf_session *session) |
| 773 | { |
| 774 | if (session->xccdf.policy_model != NULL) { |
| 775 | xccdf_policy_model_free(session->xccdf.policy_model); |
| 776 | session->xccdf.policy_model = NULL; |
| 777 | } |
| 778 | |
| 779 | /* Validate documents */ |
| 780 | if (session->validate && (!xccdf_session_is_sds(session) || session->full_validation)) { |
| 781 | if (oscap_source_validate(session->xccdf.source, _reporter, NULL)) { |
| 782 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Invalid %s (%s) content in %s", |
| 783 | oscap_document_type_to_string(oscap_source_get_scap_type(session->source)), |
| 784 | oscap_source_get_schema_version(session->source), |
| 785 | oscap_source_readable_origin(session->source)); |
| 786 | return 1; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | /* Load XCCDF model and XCCDF Policy model */ |
| 791 | struct xccdf_benchmark *benchmark = xccdf_benchmark_import_source(session->xccdf.source); |
| 792 | if (benchmark == NULL) { |
| 793 | return 1; |
| 794 | } |
| 795 | |
| 796 | /* create the policy model */ |
| 797 | session->xccdf.policy_model = xccdf_policy_model_new(benchmark); |
| 798 | if (session->xccdf.policy_model == NULL) { |
| 799 | xccdf_benchmark_free(benchmark); |
| 800 | return 1; |
| 801 | } |
| 802 | xccdf_policy_model_set_show_rule_details(session->xccdf.policy_model, session->show_rule_details); |
| 803 | return 0; |
| 804 | } |
| 805 | |
| 806 | static int _acquire_xccdf_checklist_from_tailoring(struct xccdf_session* session) |
| 807 | { |
| 808 | struct ds_sds_session *ds_sds_session = xccdf_session_get_ds_sds_session(session); |
| 809 | if (ds_sds_session == NULL) { |
| 810 | return 1; |
| 811 | } |
| 812 | |
| 813 | xmlDoc *tailoring_xmlDoc = xmlCopyDoc(oscap_source_get_xmlDoc(session->xccdf.source), true); |
| 814 | struct oscap_source *tailoring_source = oscap_source_new_from_xmlDoc(tailoring_xmlDoc, NULL); |
| 815 | struct xccdf_tailoring* tailoring = xccdf_tailoring_import_source(tailoring_source, NULL); |
| 816 | if (tailoring == NULL) { |
| 817 | /* Freeing tailoring_source also frees the tailoring_xmlDoc used to create the source */ |
| 818 | oscap_source_free(tailoring_source); |
| 819 | return 1; |
| 820 | } |
| 821 | char *benchmark_ref = oscap_strdup(xccdf_tailoring_get_benchmark_ref(tailoring)); |
| 822 | xccdf_tailoring_free(tailoring); |
| 823 | if (benchmark_ref == NULL) { |
| 824 | oscap_source_free(tailoring_source); |
| 825 | return 1; |
| 826 | } |
| 827 | |
| 828 | struct oscap_source *xccdf_source = NULL; |
| 829 | if (oscap_str_startswith(benchmark_ref, "file:")) { |
| 830 | char* sep = strchr(benchmark_ref, '#'); |
| 831 | const char *filename = benchmark_ref + strlen("file:"); |
| 832 | const char *component_ref = NULL; |
| 833 | if (sep != NULL) { |
| 834 | component_ref = sep + 1; |
| 835 | *sep = '\0'; |
| 836 | } |
| 837 | |
| 838 | struct oscap_source *external_file = oscap_source_new_from_file(filename); |
| 839 | if (oscap_source_get_scap_type(external_file) == OSCAP_DOCUMENT_SDS) { |
| 840 | if (component_ref == NULL) { |
| 841 | oscap_seterr(OSCAP_EFAMILY_OSCAP, |
| 842 | "The referenced component is a datastream, but no datastream component was specified."); |
| 843 | oscap_source_free(external_file); |
| 844 | oscap_source_free(tailoring_source); |
| 845 | free(benchmark_ref); |
| 846 | return 1; |
| 847 | } |
| 848 | ds_sds_session_free(ds_sds_session); |
| 849 | ds_sds_session = ds_sds_session_new_from_source(external_file); |
| 850 | session->ds.session = ds_sds_session; |
| 851 | xccdf_source = ds_sds_session_select_checklist(ds_sds_session, NULL, component_ref, NULL); |
| 852 | oscap_source_free(session->source); |
| 853 | session->source = external_file; |
| 854 | } else { |
| 855 | xccdf_source = external_file; |
| 856 | } |
| 857 | } else { |
| 858 | xccdf_source = ds_sds_session_select_checklist(ds_sds_session, |
| 859 | NULL, benchmark_ref + 1, NULL); |
| 860 | } |
| 861 | if (xccdf_source == NULL) { |
| 862 | oscap_seterr(OSCAP_EFAMILY_OSCAP, |
| 863 | "Could not find benchmark referenced from tailoring as '%s'.", benchmark_ref); |
| 864 | oscap_source_free(tailoring_source); |
| 865 | free(benchmark_ref); |
| 866 | return 1; |
| 867 | } |
| 868 | |
| 869 | session->xccdf.source = xccdf_source; |
| 870 | session->tailoring.user_file = tailoring_source; |
| 871 | free(benchmark_ref); |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | int xccdf_session_load_xccdf(struct xccdf_session *session) |
| 876 | { |
| 877 | session->xccdf.source = NULL; |
| 878 | |
| 879 | if (xccdf_session_is_sds(session)) { |
| 880 | if (session->validate) { |
| 881 | if (oscap_source_validate(session->source, _reporter, NULL)) { |
| 882 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Invalid %s (%s) content in %s", |
| 883 | oscap_document_type_to_string(oscap_source_get_scap_type(session->source)), |
| 884 | oscap_source_get_schema_version(session->source), |
| 885 | oscap_source_readable_origin(session->source)); |
| 886 | return 1; |
| 887 | } |
| 888 | } |
| 889 | if (session->validate_signature || session->enforce_signature) { |
| 890 | if (oscap_signature_validate(session->source, session->signature_ctx, session->enforce_signature)) { |
| 891 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Invalid signature in %s (%s) content in %s", |
| 892 | oscap_document_type_to_string(oscap_source_get_scap_type(session->source)), |
| 893 | oscap_source_get_schema_version(session->source), |
| 894 | oscap_source_readable_origin(session->source)); |
| 895 | return 1; |
| 896 | } |
| 897 | } |
| 898 | session->xccdf.source = ds_sds_session_select_checklist(xccdf_session_get_ds_sds_session(session), session->ds.user_datastream_id, |
| 899 | session->ds.user_component_id, session->ds.user_benchmark_id); |
| 900 | if (session->xccdf.source == NULL) { |
| 901 | return 1; |
| 902 | } |
| 903 | if (oscap_source_get_scap_type(session->xccdf.source) == OSCAP_DOCUMENT_XCCDF_TAILORING) { |
| 904 | if (_acquire_xccdf_checklist_from_tailoring(session)) { |
| 905 | oscap_seterr(OSCAP_EFAMILY_OSCAP, |
| 906 | "Could not find appropriate checklist to tailor."); |
| 907 | return 1; |
| 908 | } |
| 909 | } |
| 910 | if (oscap_source_get_scap_type(session->xccdf.source) != OSCAP_DOCUMENT_XCCDF) { |
| 911 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "The selected checklist document is not '%s', but '%s'.", |
| 912 | oscap_document_type_to_string(OSCAP_DOCUMENT_XCCDF), |
| 913 | oscap_document_type_to_string(oscap_source_get_scap_type(session->xccdf.source))); |
| 914 | return 1; |
| 915 | } |
| 916 | } |
| 917 | else { |
| 918 | session->xccdf.source = session->source; |
| 919 | } |
| 920 | |
| 921 | return _xccdf_session_load_xccdf_benchmark(session); |
| 922 | } |
| 923 | static inline void _connect_cpe_session_with_sds(struct xccdf_session *session) |
| 924 | { |
| 925 | struct cpe_session *cpe_session = xccdf_policy_model_get_cpe_session(session->xccdf.policy_model); |
| 926 | struct oscap_htable *sources_cache = ds_sds_session_get_component_sources(xccdf_session_get_ds_sds_session(session)); |
| 927 | cpe_session_set_cache(cpe_session, sources_cache); |
| 928 | } |
| 929 | |
| 930 | int xccdf_session_load_cpe(struct xccdf_session *session) |
| 931 | { |
| 932 | if (session == NULL || session->xccdf.policy_model == NULL) |
| 933 | return 1; |
| 934 | |
| 935 | // The CPE session will load OVAL files for any CPE dicts that require it. |
| 936 | // These OVAL files are outside of scope of XCCDF session but we still want |
| 937 | // to apply the thin results settings to them. |
| 938 | struct cpe_session *cpe_session = xccdf_policy_model_get_cpe_session(session->xccdf.policy_model); |
| 939 | cpe_session_set_thin_results(cpe_session, session->export.thin_results); |
| 940 | |
| 941 | /* Use custom CPE dict if given */ |
| 942 | if (session->user_cpe != NULL) { |
| 943 | struct oscap_source *source = oscap_source_new_from_file(session->user_cpe); |
| 944 | if (oscap_source_validate(source, _reporter, NULL) != 0) { |
| 945 | oscap_source_free(source); |
| 946 | return 1; |
| 947 | } |
| 948 | if (!xccdf_policy_model_add_cpe_autodetect_source(session->xccdf.policy_model, source)) { |
| 949 | oscap_source_free(source); |
| 950 | return 1; |
| 951 | } |
| 952 | oscap_source_free(source); |
| 953 | } |
| 954 | |
| 955 | if (xccdf_session_is_sds(session)) { |
| 956 | _connect_cpe_session_with_sds(session); |
| 957 | struct ds_sds_index *sds_idx = xccdf_session_get_sds_idx(session); |
| 958 | if (sds_idx == NULL) { |
| 959 | return -1; |
| 960 | } |
| 961 | struct ds_stream_index* stream_idx = ds_sds_index_get_stream(sds_idx, xccdf_session_get_datastream_id(session)); |
| 962 | struct oscap_string_iterator* cpe_it = ds_stream_index_get_dictionaries(stream_idx); |
| 963 | |
| 964 | // This potentially allows us to skip yet another decompose if we are sure |
| 965 | // there are no CPE dictionaries or language models inside the datastream. |
| 966 | if (oscap_string_iterator_has_more(cpe_it)) { |
| 967 | if (ds_sds_session_register_component_with_dependencies(xccdf_session_get_ds_sds_session(session), |
| 968 | "dictionaries", NULL, NULL) != 0) { |
| 969 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Can't decompose CPE dictionaries from datastream '%s' " |
| 970 | "from file '%s'!\n", xccdf_session_get_datastream_id(session), |
| 971 | oscap_source_readable_origin(session->source)); |
| 972 | oscap_string_iterator_free(cpe_it); |
| 973 | return 1; |
| 974 | } |
| 975 | while (oscap_string_iterator_has_more(cpe_it)) { |
| 976 | const char* cpe_filename = oscap_string_iterator_next(cpe_it); |
| 977 | |
| 978 | struct oscap_source *source = ds_sds_session_get_component_by_href(xccdf_session_get_ds_sds_session(session), cpe_filename); |
| 979 | |
| 980 | if (session->full_validation) { |
| 981 | if (oscap_source_validate(source, _reporter, NULL) != 0) { |
| 982 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Invalid %s (%s) content in %s", |
| 983 | oscap_document_type_to_string(oscap_source_get_scap_type(source)), |
| 984 | oscap_source_get_schema_version(source), |
| 985 | oscap_source_readable_origin(source)); |
| 986 | oscap_string_iterator_free(cpe_it); |
| 987 | return 1; |
| 988 | } |
| 989 | } |
| 990 | if (!xccdf_policy_model_add_cpe_autodetect_source(session->xccdf.policy_model, source)) { |
| 991 | oscap_string_iterator_free(cpe_it); |
| 992 | return 1; |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | oscap_string_iterator_free(cpe_it); |
| 997 | } |
| 998 | return 0; |
| 999 | } |
| 1000 | |
| 1001 | static void _oval_content_resources_free(struct oval_content_resource **resources) |
| 1002 | { |
| 1003 | if (resources) { |
| 1004 | for (int i=0; resources[i]; i++) { |
| 1005 | free(resources[i]->href); |
| 1006 | if (resources[i]->source_owned) { |
| 1007 | oscap_source_free(resources[i]->source); |
| 1008 | } |
| 1009 | free(resources[i]); |
| 1010 | } |
| 1011 | free(resources); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | void xccdf_session_set_custom_oval_files(struct xccdf_session *session, char **oval_filenames) |
| 1016 | { |
| 1017 | _oval_content_resources_free(session->oval.custom_resources); |
| 1018 | session->oval.custom_resources = NULL; |
| 1019 | if (oval_filenames == NULL) |
| 1020 | return; |
| 1021 | |
| 1022 | size_t count; |
| 1023 | for (count = 0; oval_filenames[count];) |
| 1024 | count++; |
| 1025 | struct oval_content_resource **resources = malloc((count + 1) * sizeof(struct oval_content_resource *)); |
| 1026 | if (resources == NULL) |
| 1027 | return; |
| 1028 | |
| 1029 | for (int i = 0; oval_filenames[i];) { |
| 1030 | resources[i] = malloc(sizeof(struct oval_content_resource)); |
| 1031 | resources[i]->href = oscap_basename(oval_filenames[i]); |
| 1032 | resources[i]->source = oscap_source_new_from_file(oval_filenames[i]); |
| 1033 | resources[i]->source_owned = true; |
| 1034 | i++; |
| 1035 | resources[i] = NULL; |
| 1036 | } |
| 1037 | session->oval.custom_resources = resources; |
| 1038 | } |
| 1039 | |
| 1040 | static int _xccdf_session_get_oval_from_model(struct xccdf_session *session) |
| 1041 | { |
| 1042 | struct oval_content_resource **resources = NULL; |
| 1043 | struct oscap_file_entry_list *files = NULL; |
| 1044 | struct oscap_file_entry_iterator *files_it = NULL; |
| 1045 | int idx = 0; |
| 1046 | char *tmp_path; |
| 1047 | char *printable_path; |
| 1048 | bool fetch_option_suggested = false; |
| 1049 | char *xccdf_path_cpy = NULL; |
| 1050 | char *dir_path = NULL; |
| 1051 | int res = 0; |
| 1052 | |
| 1053 | _oval_content_resources_free(session->oval.resources); |
| 1054 | |
| 1055 | xccdf_path_cpy = strdup(oscap_source_readable_origin(session->xccdf.source)); |
| 1056 | dir_path = oscap_dirname(xccdf_path_cpy); |
| 1057 | |
| 1058 | resources = malloc(sizeof(struct oval_content_resource *)); |
| 1059 | resources[idx] = NULL; |
| 1060 | |
| 1061 | files = xccdf_policy_model_get_systems_and_files(session->xccdf.policy_model); |
| 1062 | files_it = oscap_file_entry_list_get_files(files); |
| 1063 | while (oscap_file_entry_iterator_has_more(files_it)) { |
| 1064 | struct oscap_file_entry *file_entry; |
| 1065 | struct stat sb; |
| 1066 | bool source_owned = false; |
| 1067 | |
| 1068 | file_entry = (struct oscap_file_entry *) oscap_file_entry_iterator_next(files_it); |
| 1069 | |
| 1070 | // we only care about OVAL referenced files |
| 1071 | if (strcmp(oscap_file_entry_get_system(file_entry), oval_sysname)) |
| 1072 | continue; |
| 1073 | |
| 1074 | const char *file_path = oscap_file_entry_get_file(file_entry); |
| 1075 | struct oscap_source *source = NULL; |
| 1076 | |
| 1077 | tmp_path = malloc(PATH_MAX * sizeof(char)); |
| 1078 | if (file_path[0] == '/') { // it's a simple absolute path |
| 1079 | snprintf(tmp_path, PATH_MAX, "%s", file_path); |
| 1080 | } |
| 1081 | else if (oscap_str_startswith(file_path, "file://")) { // it's an absolute path prefixed with file:// |
| 1082 | snprintf(tmp_path, PATH_MAX, "%s", file_path + 7); |
| 1083 | } |
| 1084 | else { // assume it's a relative path |
| 1085 | snprintf(tmp_path, PATH_MAX, "%s/%s", dir_path, file_path); |
| 1086 | } |
| 1087 | |
| 1088 | if (xccdf_session_get_ds_sds_session(session) != NULL) { |
| 1089 | source = ds_sds_session_get_component_by_href(xccdf_session_get_ds_sds_session(session), file_path); |
| 1090 | source_owned = false; |
| 1091 | } else { |
| 1092 | if (stat(tmp_path, &sb) == 0) { |
| 1093 | source = oscap_source_new_from_file(tmp_path); |
| 1094 | source_owned = true; |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | if (source != NULL) { |
| 1099 | resources[idx] = malloc(sizeof(struct oval_content_resource)); |
| 1100 | resources[idx]->href = oscap_strdup(oscap_file_entry_get_file(file_entry)); |
| 1101 | resources[idx]->source_owned = source_owned; |
| 1102 | resources[idx]->source = source; |
| 1103 | idx++; |
| 1104 | void *new_resources = realloc(resources, (idx + 1) * sizeof(struct oval_content_resource *)); |
| 1105 | if (new_resources == NULL) { |
| 1106 | _oval_content_resources_free(resources); |
| 1107 | free(tmp_path); |
| 1108 | res = -1; |
| 1109 | goto cleanup; |
| 1110 | } |
| 1111 | resources = new_resources; |
| 1112 | resources[idx] = NULL; |
| 1113 | } |
| 1114 | else { |
| 1115 | if (oscap_acquire_url_is_supported(oscap_file_entry_get_file(file_entry))) { |
| 1116 | // Strip out the 'path' for printing the url. |
| 1117 | printable_path = (char *) oscap_file_entry_get_file(file_entry); |
| 1118 | |
| 1119 | if (session->oval.fetch_remote_resources) { |
| 1120 | session->oval.progress(false, "Downloading: %s ... ", printable_path); |
| 1121 | |
| 1122 | size_t data_size; |
| 1123 | char *data = oscap_acquire_url_download(printable_path, &data_size); |
| 1124 | if (data == NULL) { |
| 1125 | session->oval.progress(false, "error\n"); |
| 1126 | } else { |
| 1127 | session->oval.progress(false, "ok\n"); |
| 1128 | |
| 1129 | resources[idx] = malloc(sizeof(struct oval_content_resource)); |
| 1130 | resources[idx]->href = oscap_strdup(printable_path); |
| 1131 | resources[idx]->source = oscap_source_new_take_memory(data, data_size, printable_path); |
| 1132 | resources[idx]->source_owned = true; |
| 1133 | idx++; |
| 1134 | void * new_resources = realloc(resources, (idx + 1) * sizeof(struct oval_content_resource *)); |
| 1135 | if (new_resources == NULL) { |
| 1136 | _oval_content_resources_free(resources); |
| 1137 | free(tmp_path); |
| 1138 | res = -1; |
| 1139 | goto cleanup; |
| 1140 | } |
| 1141 | resources = new_resources; |
| 1142 | resources[idx] = NULL; |
| 1143 | free(tmp_path); |
| 1144 | continue; |
| 1145 | } |
| 1146 | } |
| 1147 | else if (!fetch_option_suggested) { |
| 1148 | session->oval.progress(true, "WARNING: This content points out to the remote resources. " |
| 1149 | "Use `--fetch-remote-resources' option to download them.\n"); |
| 1150 | fetch_option_suggested = true; |
| 1151 | } |
| 1152 | } |
| 1153 | else |
| 1154 | printable_path = tmp_path; |
| 1155 | session->oval.progress(true, "WARNING: Skipping %s file which is referenced from XCCDF content\n", printable_path); |
| 1156 | } |
| 1157 | free(tmp_path); |
| 1158 | } |
| 1159 | session->oval.resources = resources; |
| 1160 | cleanup: |
| 1161 | free(dir_path); |
| 1162 | oscap_file_entry_iterator_free(files_it); |
| 1163 | oscap_file_entry_list_free(files); |
| 1164 | free(xccdf_path_cpy); |
| 1165 | return res; |
| 1166 | } |
| 1167 | |
| 1168 | static void _xccdf_session_free_oval_agents(struct xccdf_session *session) |
| 1169 | { |
| 1170 | if (session->oval.agents != NULL) { |
| 1171 | for (int i=0; session->oval.agents[i]; i++) { |
| 1172 | struct oval_definition_model *def_model = oval_agent_get_definition_model(session->oval.agents[i]); |
| 1173 | oval_definition_model_free(def_model); |
| 1174 | oval_agent_destroy_session(session->oval.agents[i]); |
| 1175 | } |
| 1176 | free(session->oval.agents); |
| 1177 | session->oval.agents = NULL; |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | int xccdf_session_load_oval(struct xccdf_session *session) |
| 1182 | { |
| 1183 | struct oval_content_resource **contents = NULL; |
| 1184 | |
| 1185 | _xccdf_session_free_oval_agents(session); |
| 1186 | |
| 1187 | /* Locate all OVAL files */ |
| 1188 | if (session->oval.custom_resources == NULL) { |
| 1189 | /* Use OVAL files from policy model */ |
| 1190 | if (_xccdf_session_get_oval_from_model(session) != 0) |
| 1191 | return 1; |
| 1192 | } |
| 1193 | |
| 1194 | contents = session->oval.custom_resources != NULL ? session->oval.custom_resources : session->oval.resources; |
| 1195 | |
| 1196 | /* Validate OVAL files. Only validate if the file doesn't come from a datastream |
| 1197 | * or if full validation was explicitly requested. |
| 1198 | */ |
| 1199 | if (session->validate && (!xccdf_session_is_sds(session) || session->full_validation)) { |
| 1200 | for (int idx=0; contents[idx]; idx++) { |
| 1201 | if (oscap_source_validate(contents[idx]->source, _reporter, NULL) != 0) { |
| 1202 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Invalid %s (%s) content in %s", |
| 1203 | oscap_document_type_to_string(oscap_source_get_scap_type(session->source)), |
| 1204 | oscap_source_get_schema_version(session->source), |
| 1205 | contents[idx]->href); |
| 1206 | return 1; |
| 1207 | } |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | for (int idx=0; contents[idx]; idx++) { |
| 1212 | /* file -> def_model */ |
| 1213 | struct oval_definition_model *tmp_def_model = oval_definition_model_import_source(contents[idx]->source); |
| 1214 | if (tmp_def_model == NULL) { |
| 1215 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Failed to create OVAL definition model from: '%s'.", |
| 1216 | oscap_source_readable_origin(contents[idx]->source)); |
| 1217 | return 1; |
| 1218 | } |
| 1219 | |
| 1220 | /* def_model -> session */ |
| 1221 | struct oval_agent_session *tmp_sess = oval_agent_new_session(tmp_def_model, contents[idx]->href); |
| 1222 | if (tmp_sess == NULL) { |
| 1223 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Failed to create new OVAL agent session for: '%s'.", contents[idx]->href); |
| 1224 | oval_definition_model_free(tmp_def_model); |
| 1225 | return 2; |
| 1226 | } |
| 1227 | |
| 1228 | if (session->export.thin_results) { |
| 1229 | struct oval_results_model *res_model = oval_agent_get_results_model(tmp_sess); |
| 1230 | struct oval_directives_model *dir_model = oval_results_model_get_directives_model(res_model); |
| 1231 | // This is the worst function name in existence, despite its name, |
| 1232 | // it's getting the oval_result_directives of the oval_directives_model. |
| 1233 | // You would expect oval_directives_model_getresdirs at least, but no.. |
| 1234 | struct oval_result_directives *dir = oval_directives_model_get_defdirs(dir_model); |
| 1235 | oval_result_directives_set_content(dir, OVAL_RESULT_TRUE | OVAL_RESULT_FALSE | |
| 1236 | OVAL_RESULT_UNKNOWN | OVAL_RESULT_NOT_EVALUATED | |
| 1237 | OVAL_RESULT_NOT_APPLICABLE | OVAL_RESULT_ERROR, |
| 1238 | OVAL_DIRECTIVE_CONTENT_THIN); |
| 1239 | } |
| 1240 | |
| 1241 | /* store our name in the generated documents */ |
| 1242 | oval_agent_set_product_name(tmp_sess, session->oval.product_cpe != NULL ? |
| 1243 | session->oval.product_cpe : (char *) oscap_productname); |
| 1244 | |
| 1245 | /* remember sessions */ |
| 1246 | void *new_oval_agents = realloc(session->oval.agents, (idx + 2) * sizeof(struct oval_agent_session *)); |
| 1247 | if (new_oval_agents == NULL) { |
| 1248 | oval_agent_destroy_session(tmp_sess); |
| 1249 | return -1; |
| 1250 | } |
| 1251 | session->oval.agents = new_oval_agents; |
| 1252 | session->oval.agents[idx] = tmp_sess; |
| 1253 | session->oval.agents[idx+1] = NULL; |
| 1254 | |
| 1255 | /* register session */ |
| 1256 | if (session->oval.user_eval_fn != NULL) |
| 1257 | xccdf_policy_model_register_engine_and_query_callback( |
| 1258 | session->xccdf.policy_model, (char *) oval_sysname, |
| 1259 | session->oval.user_eval_fn, tmp_sess, NULL); |
| 1260 | else |
| 1261 | xccdf_policy_model_register_engine_oval(session->xccdf.policy_model, tmp_sess); |
| 1262 | } |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
| 1266 | int xccdf_session_load_check_engine_plugin2(struct xccdf_session *session, const char *plugin_name, bool quiet) |
| 1267 | { |
| 1268 | struct check_engine_plugin_def *plugin = check_engine_plugin_load2(plugin_name, quiet); |
| 1269 | |
| 1270 | if (!plugin) |
| 1271 | return -1; // error already set |
| 1272 | |
| 1273 | oscap_list_add(session->check_engine_plugins, plugin); |
| 1274 | |
| 1275 | if (xccdf_session_get_datastream_id(session) != NULL){ |
| 1276 | return check_engine_plugin_register(plugin, session->xccdf.policy_model, ds_sds_session_get_target_dir(session->ds.session)); |
| 1277 | } else { |
| 1278 | char* xccdf_filename = oscap_strdup(oscap_source_readable_origin(session->xccdf.source)); |
| 1279 | char *xccdf_dirname = oscap_dirname(xccdf_filename); |
| 1280 | int res = check_engine_plugin_register(plugin, session->xccdf.policy_model, xccdf_dirname); |
| 1281 | free(xccdf_dirname); |
| 1282 | free(xccdf_filename); |
| 1283 | return res; |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | int xccdf_session_load_check_engine_plugin(struct xccdf_session *session, const char *plugin_name) |
| 1288 | { |
| 1289 | return xccdf_session_load_check_engine_plugin2(session, plugin_name, false); |
| 1290 | } |
| 1291 | |
| 1292 | int xccdf_session_load_check_engine_plugins(struct xccdf_session *session) |
| 1293 | { |
| 1294 | xccdf_session_unload_check_engine_plugins(session); |
| 1295 | |
| 1296 | const char * const *known_plugins = check_engine_plugin_get_known_plugins(); |
| 1297 | |
| 1298 | while (*known_plugins) { |
| 1299 | // We do not report failure when a known plugin doesn't load properly, that's because they |
| 1300 | // are optional and we don't know if it's not there or if it just failed to load. |
| 1301 | if (xccdf_session_load_check_engine_plugin2(session, *known_plugins, true) != 0) |
| 1302 | oscap_clearerr(); |
| 1303 | |
| 1304 | known_plugins++; |
| 1305 | } |
| 1306 | |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
| 1310 | static void xccdf_session_unload_check_engine_plugins(struct xccdf_session *session) |
| 1311 | { |
| 1312 | struct oscap_iterator *it = oscap_iterator_new(session->check_engine_plugins); |
| 1313 | |
| 1314 | while (oscap_iterator_has_more(it)) { |
| 1315 | struct check_engine_plugin_def *plugin = (struct check_engine_plugin_def *)oscap_iterator_next(it); |
| 1316 | |
| 1317 | check_engine_plugin_cleanup(plugin, session->xccdf.policy_model); |
| 1318 | check_engine_plugin_unload(plugin); |
| 1319 | } |
| 1320 | |
| 1321 | oscap_iterator_free(it); |
| 1322 | |
| 1323 | oscap_list_free0(session->check_engine_plugins); |
| 1324 | session->check_engine_plugins = oscap_list_new(); |
| 1325 | } |
| 1326 | |
| 1327 | int xccdf_session_load_tailoring(struct xccdf_session *session) |
| 1328 | { |
| 1329 | bool from_sds = false; |
| 1330 | struct oscap_source *tailoring_source = NULL; |
| 1331 | |
| 1332 | if (session->tailoring.user_file != NULL) { |
| 1333 | tailoring_source = session->tailoring.user_file; |
| 1334 | } |
| 1335 | else if (session->tailoring.user_component_id != NULL) { |
| 1336 | if (!xccdf_session_is_sds(session)) { |
| 1337 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Can't use given tailoring component ID because file isn't a source datastream."); |
| 1338 | return 1; |
| 1339 | } |
| 1340 | |
| 1341 | tailoring_source = ds_sds_session_select_tailoring(xccdf_session_get_ds_sds_session(session), session->tailoring.user_component_id); |
| 1342 | if (tailoring_source == NULL) { |
| 1343 | return 1; |
| 1344 | } |
| 1345 | |
| 1346 | from_sds = true; |
| 1347 | } |
| 1348 | |
| 1349 | // TODO: We should warn if has a XCCDF tailoring-file hint and user isn't |
| 1350 | // using that particular tailoring. |
| 1351 | |
| 1352 | if (tailoring_source == NULL) |
| 1353 | return 0; // nothing to do |
| 1354 | |
| 1355 | if (session->validate && (!from_sds || session->full_validation)) { |
| 1356 | if (oscap_source_validate(tailoring_source, _reporter, NULL) != 0) { |
| 1357 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Invalid %s (%s) content in %s", |
| 1358 | oscap_document_type_to_string(oscap_source_get_scap_type(tailoring_source)), |
| 1359 | oscap_source_get_schema_version(tailoring_source), |
| 1360 | oscap_source_readable_origin(tailoring_source)); |
| 1361 | oscap_source_free(tailoring_source); |
| 1362 | session->tailoring.user_file = NULL; |
| 1363 | return 1; |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | struct xccdf_benchmark *benchmark = xccdf_policy_model_get_benchmark(session->xccdf.policy_model); |
| 1368 | struct xccdf_tailoring *tailoring = xccdf_tailoring_import_source(tailoring_source, benchmark); |
| 1369 | |
| 1370 | if (tailoring == NULL) |
| 1371 | return 1; |
| 1372 | |
| 1373 | return xccdf_policy_model_set_tailoring(session->xccdf.policy_model, tailoring) ? 0 : 1; |
| 1374 | } |
| 1375 | |
| 1376 | int xccdf_session_evaluate(struct xccdf_session *session) |
| 1377 | { |
| 1378 | struct xccdf_policy *policy = xccdf_session_get_xccdf_policy(session); |
| 1379 | if (policy == NULL) { |
| 1380 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Cannot build xccdf_policy."); |
| 1381 | return 1; |
| 1382 | } |
| 1383 | struct oscap_iterator *it = oscap_iterator_new(session->rules); |
| 1384 | while (oscap_iterator_has_more(it)) { |
| 1385 | const char *rule_id = oscap_iterator_next(it); |
| 1386 | oscap_htable_add(policy->rules, rule_id, (void *)true); |
| 1387 | } |
| 1388 | oscap_iterator_free(it); |
| 1389 | struct oscap_iterator *sit = oscap_iterator_new(session->skip_rules); |
| 1390 | while (oscap_iterator_has_more(sit)) { |
| 1391 | const char *rule_id = oscap_iterator_next(sit); |
| 1392 | oscap_htable_add(policy->skip_rules, rule_id, (void *)true); |
| 1393 | } |
| 1394 | oscap_iterator_free(sit); |
| 1395 | |
| 1396 | if (session->reference_parameter) { |
| 1397 | xccdf_policy_set_reference_filter(policy, session->reference_parameter); |
| 1398 | } |
| 1399 | session->xccdf.result = xccdf_policy_evaluate(policy); |
| 1400 | if (session->xccdf.result == NULL) |
| 1401 | return 1; |
| 1402 | |
| 1403 | /* Write results into XCCDF Test Result model */ |
| 1404 | xccdf_result_set_benchmark_uri(session->xccdf.result, oscap_source_readable_origin(session->source)); |
| 1405 | struct oscap_text *title = oscap_text_new(); |
| 1406 | oscap_text_set_text(title, "OSCAP Scan Result"); |
| 1407 | xccdf_result_add_title(session->xccdf.result, title); |
| 1408 | struct xccdf_benchmark *benchmark = xccdf_policy_get_benchmark(policy); |
| 1409 | xccdf_result_set_version(session->xccdf.result, |
| 1410 | benchmark != NULL ? xccdf_benchmark_get_version(benchmark) : NULL); |
| 1411 | |
| 1412 | xccdf_result_fill_sysinfo(session->xccdf.result); |
| 1413 | |
| 1414 | struct xccdf_model_iterator *model_it = xccdf_benchmark_get_models(xccdf_policy_model_get_benchmark(session->xccdf.policy_model)); |
| 1415 | while (xccdf_model_iterator_has_more(model_it)) { |
| 1416 | struct xccdf_model *model = xccdf_model_iterator_next(model_it); |
| 1417 | const char *score_model = xccdf_model_get_system(model); |
| 1418 | struct xccdf_score *score = xccdf_policy_get_score(policy, session->xccdf.result, score_model); |
| 1419 | xccdf_result_add_score(session->xccdf.result, score); |
| 1420 | |
| 1421 | /* record default base score for later use */ |
| 1422 | if (!strcmp(score_model, "urn:xccdf:scoring:default")) |
| 1423 | session->xccdf.base_score = xccdf_score_get_score(score); |
| 1424 | } |
| 1425 | xccdf_model_iterator_free(model_it); |
| 1426 | return 0; |
| 1427 | } |
| 1428 | |
| 1429 | static size_t _paramlist_size(const char **p) { size_t s = 0; if (!p) return s; while (p[s]) s += 2; return s; } |
| 1430 | |
| 1431 | static size_t _paramlist_cpy(const char **to, const char **p) { |
| 1432 | size_t s = 0; |
| 1433 | if (!to || !p) |
| 1434 | return s; |
| 1435 | for (;p && p[s]; s += 2) to[s] = p[s], to[s+1] = p[s+1]; |
| 1436 | to[s] = p[s]; |
| 1437 | return s; |
| 1438 | } |
| 1439 | |
| 1440 | static int _app_xslt(struct oscap_source *infile, const char *xsltfile, const char *outfile, const char **params) |
| 1441 | { |
| 1442 | char pwd[PATH_MAX]; |
| 1443 | |
| 1444 | if (getcwd(pwd, sizeof(pwd)) == NULL) { |
| 1445 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Getcwd() failed: %s", strerror(errno)); |
| 1446 | return 1; |
| 1447 | } |
| 1448 | |
| 1449 | /* add params oscap-version & pwd */ |
| 1450 | const char *stdparams[] = { "oscap-version", oscap_get_version(), "pwd", pwd, NULL }; |
| 1451 | const size_t par_cnt = _paramlist_size(params) + _paramlist_size(stdparams) + 1; |
| 1452 | const char **par = malloc(par_cnt * sizeof(const char *)); |
| 1453 | size_t s = _paramlist_cpy(par, params); |
| 1454 | s += _paramlist_cpy(par + s, stdparams); |
| 1455 | |
| 1456 | int ret = oscap_source_apply_xslt_path(infile, xsltfile, outfile, par, oscap_path_to_xslt()); |
| 1457 | free(par); |
| 1458 | return ret == -1; |
| 1459 | } |
| 1460 | |
| 1461 | static inline int _xccdf_gen_report(struct oscap_source *infile, const char *id, const char *outfile, const char *show, const char* sce_template, const char* profile) |
| 1462 | { |
| 1463 | const char *params[] = { |
| 1464 | "result-id", id, |
| 1465 | "show", show, |
| 1466 | "profile", profile, |
| 1467 | "oval-template", "", |
| 1468 | "sce-template", sce_template, |
| 1469 | "verbosity", "", |
| 1470 | "hide-profile-info", NULL, |
| 1471 | NULL}; |
| 1472 | |
| 1473 | return _app_xslt(infile, "xccdf-report.xsl", outfile, params); |
| 1474 | } |
| 1475 | |
| 1476 | static int _build_xccdf_result_source(struct xccdf_session *session) |
| 1477 | { |
| 1478 | if (session->xccdf.result_source != NULL) { |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
| 1482 | /* Build oscap_source of XCCDF TestResult only when needed */ |
| 1483 | if (session->export.xccdf_file != NULL || session->export.report_file != NULL || session->export.arf_file != NULL || session->export.xccdf_stig_viewer_file != NULL) { |
| 1484 | struct xccdf_benchmark *benchmark = xccdf_policy_model_get_benchmark(session->xccdf.policy_model); |
| 1485 | |
| 1486 | if (session->xccdf.result == NULL) { |
| 1487 | // Attempt to export session before evaluation |
| 1488 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "No XCCDF results to export."); |
| 1489 | return 1; |
| 1490 | } |
| 1491 | |
| 1492 | if (session->export.xccdf_file != NULL) { |
| 1493 | struct xccdf_benchmark *cloned_benchmark = xccdf_benchmark_clone(benchmark); |
| 1494 | struct xccdf_result *cloned_result = xccdf_result_clone(session->xccdf.result); |
| 1495 | xccdf_benchmark_add_result(cloned_benchmark, cloned_result); |
| 1496 | struct oscap_source *xccdf_result_source = xccdf_benchmark_export_source(cloned_benchmark, session->export.xccdf_file); |
| 1497 | // cloned_result is freed during xccdf_benchmark_free |
| 1498 | xccdf_benchmark_free(cloned_benchmark); |
| 1499 | // Export XCCDF result file only when explicitly requested |
| 1500 | if (oscap_source_save_as(xccdf_result_source, NULL) != 0) { |
| 1501 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not save file: %s", |
| 1502 | oscap_source_readable_origin(xccdf_result_source)); |
| 1503 | oscap_source_free(xccdf_result_source); |
| 1504 | return -1; |
| 1505 | } |
| 1506 | oscap_source_free(xccdf_result_source); |
| 1507 | } |
| 1508 | |
| 1509 | if (session->export.xccdf_stig_viewer_file != NULL) { |
| 1510 | struct xccdf_benchmark *cloned_benchmark = xccdf_benchmark_clone(benchmark); |
| 1511 | struct xccdf_result *cloned_result = xccdf_result_clone(session->xccdf.result); |
| 1512 | xccdf_benchmark_add_result(cloned_benchmark, cloned_result); |
| 1513 | struct oscap_source * stig_result = xccdf_result_stig_viewer_export_source(cloned_result, session->export.xccdf_stig_viewer_file); |
| 1514 | // cloned_result is freed during xccdf_benchmark_free |
| 1515 | xccdf_benchmark_free(cloned_benchmark); |
| 1516 | if (oscap_source_save_as(stig_result, NULL) != 0) { |
| 1517 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not save file: %s", |
| 1518 | oscap_source_readable_origin(stig_result)); |
| 1519 | oscap_source_free(stig_result); |
| 1520 | return -1; |
| 1521 | } |
| 1522 | oscap_source_free(stig_result); |
| 1523 | } |
| 1524 | |
| 1525 | struct xccdf_result *cloned_result = xccdf_result_clone(session->xccdf.result); |
| 1526 | if (xccdf_session_is_sds(session)) { |
| 1527 | struct ds_sds_session *sds_session = xccdf_session_get_ds_sds_session(session); |
| 1528 | const char *benchmark_uri = ds_sds_session_get_checklist_uri(sds_session); |
| 1529 | xccdf_result_set_benchmark_uri(cloned_result, benchmark_uri); |
| 1530 | } |
| 1531 | xccdf_benchmark_add_result(benchmark, cloned_result); |
| 1532 | session->xccdf.result_source = xccdf_benchmark_export_source(benchmark, session->export.xccdf_file); |
| 1533 | /* validate XCCDF Results */ |
| 1534 | if (session->validate && session->full_validation) { |
| 1535 | if (oscap_source_validate(session->xccdf.result_source, _reporter, NULL)) { |
| 1536 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not export XCCDF Results correctly to %s", |
| 1537 | oscap_source_readable_origin(session->xccdf.result_source)); |
| 1538 | return 1; |
| 1539 | } |
| 1540 | } |
| 1541 | } |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
| 1545 | int xccdf_session_export_xccdf(struct xccdf_session *session) |
| 1546 | { |
| 1547 | if (_build_xccdf_result_source(session)) { |
| 1548 | return 1; |
| 1549 | } |
| 1550 | |
| 1551 | if (session->export.report_file == NULL) |
| 1552 | return 0; |
| 1553 | |
| 1554 | struct oscap_source* arf = xccdf_session_create_arf_source(session); |
| 1555 | if (arf == NULL) { |
| 1556 | return 1; |
| 1557 | } |
| 1558 | |
| 1559 | /* generate report */ |
| 1560 | _xccdf_gen_report(arf, |
| 1561 | xccdf_result_get_id(session->xccdf.result), |
| 1562 | session->export.report_file, |
| 1563 | "", |
| 1564 | (session->export.check_engine_plugins_results ? "%.result.xml" : ""), |
| 1565 | session->xccdf.profile_id == NULL ? "" : session->xccdf.profile_id |
| 1566 | ); |
| 1567 | |
| 1568 | return 0; |
| 1569 | } |
| 1570 | |
| 1571 | static void _xccdf_session_free_oval_result_sources(struct xccdf_session *session) |
| 1572 | { |
| 1573 | if (session->oval.result_sources != NULL) { |
| 1574 | oscap_htable_free(session->oval.result_sources, (oscap_destruct_func) oscap_source_free); |
| 1575 | session->oval.result_sources = NULL; |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | static char *_xccdf_session_get_unique_oval_result_filename(struct xccdf_session *session, struct oval_agent_session *oval_session, const char *oval_results_directory) |
| 1580 | { |
| 1581 | char *escaped_url = NULL; |
| 1582 | const char *filename = oval_agent_get_filename(oval_session); |
| 1583 | if (!filename) { |
| 1584 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Given oval_session doesn't have a valid filename. " |
| 1585 | "Can't deduce destination where we should be exporting it!"); |
| 1586 | return NULL; |
| 1587 | } |
| 1588 | |
| 1589 | if (oscap_acquire_url_is_supported(filename) || strchr(filename, '/')) { |
| 1590 | // We need escaping if: |
| 1591 | // - filename is a URL |
| 1592 | // - filename is an absolute path |
| 1593 | |
| 1594 | escaped_url = oscap_acquire_url_to_filename(filename); |
| 1595 | if (escaped_url == NULL) |
| 1596 | return NULL; |
| 1597 | } |
| 1598 | else if (!session->export.oval_results && strchr(filename, '/')) { |
| 1599 | // We need recursive mkdir_p if: |
| 1600 | // - filename is not a basename and we are exporting to a temp dir (ARF) |
| 1601 | // (we are assuming that the dir structure exists if we are exporting to '.') |
| 1602 | char *filename_cpy = oscap_sprintf("%s/%s", oval_results_directory, filename); |
| 1603 | if (oscap_acquire_ensure_parent_dir(filename_cpy) != 0) { |
| 1604 | free(filename_cpy); |
| 1605 | return NULL; |
| 1606 | } |
| 1607 | free(filename_cpy); |
| 1608 | } |
| 1609 | |
| 1610 | char *name = NULL; |
| 1611 | unsigned int suffix = 1; |
| 1612 | while (suffix < UINT_MAX) |
| 1613 | { |
| 1614 | name = malloc(PATH_MAX * sizeof(char)); |
| 1615 | if (suffix == 1) |
| 1616 | snprintf(name, PATH_MAX, "%s/%s.result.xml", oval_results_directory, escaped_url != NULL ? escaped_url : filename); |
| 1617 | else |
| 1618 | snprintf(name, PATH_MAX, "%s/%s.result%i.xml", oval_results_directory, escaped_url != NULL ? escaped_url : filename, suffix); |
| 1619 | |
| 1620 | // Try to guess how the real path will look like. This should avoid us rewriting |
| 1621 | // the results files if the OVAL happens to have the same name. We allow users |
| 1622 | // to shoot themselves to the foot, but it is not easy. They need to set-up |
| 1623 | // file->file symlink before scanning. |
| 1624 | char *final_name = oscap_acquire_guess_realpath(name); |
| 1625 | free(name); |
| 1626 | name = final_name; |
| 1627 | if (name == NULL) { |
| 1628 | free(escaped_url); |
| 1629 | return NULL; |
| 1630 | } |
| 1631 | if (oscap_htable_get(session->oval.result_sources, name) == NULL) { |
| 1632 | // Check if this export name conflicts with any other exported OVAL result. |
| 1633 | // |
| 1634 | // One example where a conflict can easily happen is if we have the |
| 1635 | // same OVAL file used for CPE platform evaluation and check evaluation. |
| 1636 | // |
| 1637 | // openscap will create 2 OVAL sessions for the same file and will try |
| 1638 | // to export 2 different OVAL result files to the same path. |
| 1639 | break; |
| 1640 | } |
| 1641 | |
| 1642 | free(name); |
| 1643 | name = NULL; |
| 1644 | ++suffix; |
| 1645 | } |
| 1646 | |
| 1647 | if (escaped_url != NULL) |
| 1648 | free(escaped_url); |
| 1649 | |
| 1650 | return name; |
| 1651 | } |
| 1652 | |
| 1653 | static char *_xccdf_session_export_oval_result_file(struct xccdf_session *session, struct oval_agent_session *oval_session) |
| 1654 | { |
| 1655 | /* get result model and session name */ |
| 1656 | struct oval_results_model *res_model = oval_agent_get_results_model(oval_session); |
| 1657 | /* Import XCCDF session without_sys_chars flag to res_model */ |
| 1658 | oval_results_model_set_export_system_characteristics(res_model, !session->export.without_sys_chars); |
| 1659 | const char* oval_results_directory = NULL; |
| 1660 | |
| 1661 | if (session->export.oval_results == true) { |
| 1662 | oval_results_directory = "."; |
| 1663 | } |
| 1664 | else { |
| 1665 | if (!session->temp_dir) |
| 1666 | session->temp_dir = oscap_acquire_temp_dir(); |
| 1667 | if (session->temp_dir == NULL) |
| 1668 | return NULL; |
| 1669 | |
| 1670 | oval_results_directory = session->temp_dir; |
| 1671 | } |
| 1672 | |
| 1673 | char *name = _xccdf_session_get_unique_oval_result_filename(session, oval_session, oval_results_directory); |
| 1674 | |
| 1675 | if (name == NULL) { |
| 1676 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Can't figure out the right filename for OVAL result file. Can't export that file!"); |
| 1677 | return NULL; |
| 1678 | } |
| 1679 | |
| 1680 | struct oscap_source *source = oval_results_model_export_source(res_model, NULL, name); |
| 1681 | if (source == NULL) { |
| 1682 | free(name); |
| 1683 | return NULL; |
| 1684 | } |
| 1685 | if (oscap_htable_add(session->oval.result_sources, name, source) == false) { |
| 1686 | // The source is already there, but it shouldn't be |
| 1687 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Internal error: attempted to export file %s twice", name); |
| 1688 | oscap_source_free(source); |
| 1689 | free(name); |
| 1690 | abort(); // Let's make this visible in debug mode |
| 1691 | return NULL; |
| 1692 | } |
| 1693 | |
| 1694 | static int counter = 0; |
| 1695 | char *report_id = oscap_sprintf("oval%d", counter++); |
| 1696 | const char *original_name = oval_agent_get_filename(oval_session); |
| 1697 | char *results_file_name = oscap_strdup(name); |
| 1698 | if (!oscap_htable_add(session->oval.results_mapping, original_name, results_file_name)){ |
| 1699 | free(results_file_name); |
| 1700 | } |
| 1701 | if (!oscap_htable_add(session->oval.arf_report_mapping, original_name, report_id)) { |
| 1702 | free(report_id); |
| 1703 | }; |
| 1704 | |
| 1705 | /* validate OVAL Results */ |
| 1706 | if (session->validate && session->full_validation) { |
| 1707 | if (oscap_source_validate(source, _reporter, NULL)) { |
| 1708 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not export OVAL Results correctly to %s", |
| 1709 | oscap_source_readable_origin(source)); |
| 1710 | free(name); |
| 1711 | return NULL; |
| 1712 | } |
| 1713 | } |
| 1714 | return name; |
| 1715 | } |
| 1716 | |
| 1717 | static int _build_oval_result_sources(struct xccdf_session *session) |
| 1718 | { |
| 1719 | if (session->oval.result_sources != NULL) { |
| 1720 | return 0; |
| 1721 | } |
| 1722 | |
| 1723 | /* Export OVAL results */ |
| 1724 | session->oval.result_sources = oscap_htable_new(); |
| 1725 | session->oval.results_mapping = oscap_htable_new(); |
| 1726 | session->oval.arf_report_mapping = oscap_htable_new(); |
| 1727 | if (session->oval.agents) { |
| 1728 | for (int i = 0; session->oval.agents[i]; i++) { |
| 1729 | char *filename = _xccdf_session_export_oval_result_file(session, session->oval.agents[i]); |
| 1730 | if (filename == NULL) { |
| 1731 | _xccdf_session_free_oval_result_sources(session); |
| 1732 | return 1; |
| 1733 | } |
| 1734 | free(filename); |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | struct oscap_htable_iterator *cpe_it = xccdf_policy_model_get_cpe_oval_sessions(session->xccdf.policy_model); |
| 1739 | while (oscap_htable_iterator_has_more(cpe_it)) { |
| 1740 | const char *key = NULL; |
| 1741 | struct oval_agent_session *value = NULL; |
| 1742 | oscap_htable_iterator_next_kv(cpe_it, &key, (void*)&value); |
| 1743 | |
| 1744 | char *filename = _xccdf_session_export_oval_result_file(session, value); |
| 1745 | if (filename == NULL) { |
| 1746 | _xccdf_session_free_oval_result_sources(session); |
| 1747 | oscap_htable_iterator_free(cpe_it); |
| 1748 | return 1; |
| 1749 | } |
| 1750 | free(filename); |
| 1751 | } |
| 1752 | oscap_htable_iterator_free(cpe_it); |
| 1753 | return 0; |
| 1754 | } |
| 1755 | |
| 1756 | int xccdf_session_export_oval(struct xccdf_session *session) |
| 1757 | { |
| 1758 | if (_build_oval_result_sources(session) != 0) { |
| 1759 | return 1; |
| 1760 | } |
| 1761 | struct oscap_htable_iterator *hit = oscap_htable_iterator_new(session->oval.result_sources); |
| 1762 | while (oscap_htable_iterator_has_more(hit)) { |
| 1763 | struct oscap_source *source = oscap_htable_iterator_next_value(hit); |
| 1764 | if (oscap_source_save_as(source, NULL) != 0) { |
| 1765 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not save file: %s", oscap_source_readable_origin(source)); |
| 1766 | oscap_htable_iterator_free(hit); |
| 1767 | return 1; |
| 1768 | } |
| 1769 | } |
| 1770 | oscap_htable_iterator_free(hit); |
| 1771 | |
| 1772 | /* Export variables */ |
| 1773 | if (session->export.oval_variables && session->oval.agents != NULL) { |
| 1774 | for (int i = 0; session->oval.agents[i]; ++i) { |
| 1775 | int j; |
| 1776 | char *sname; |
| 1777 | struct oval_definition_model *defmod; |
| 1778 | struct oval_variable_model_iterator *varmod_itr; |
| 1779 | char *escaped_url = NULL; |
| 1780 | |
| 1781 | sname = (char *) oval_agent_get_filename(session->oval.agents[i]); |
| 1782 | if (oscap_acquire_url_is_supported(sname)) { |
| 1783 | escaped_url = oscap_acquire_url_to_filename(sname); |
| 1784 | if (escaped_url == NULL) |
| 1785 | return 1; |
| 1786 | sname = escaped_url; |
| 1787 | } |
| 1788 | defmod = oval_agent_get_definition_model(session->oval.agents[i]); |
| 1789 | j = 0; |
| 1790 | varmod_itr = oval_definition_model_get_variable_models(defmod); |
| 1791 | while (oval_variable_model_iterator_has_more(varmod_itr)) { |
| 1792 | const size_t fname_len = strlen(sname) + 32; |
| 1793 | char *fname = malloc(fname_len); |
| 1794 | struct oval_variable_model *varmod; |
| 1795 | |
| 1796 | varmod = oval_variable_model_iterator_next(varmod_itr); |
| 1797 | snprintf(fname, fname_len, "%s-%d.variables-%d.xml", sname, i, j++); |
| 1798 | oval_variable_model_export(varmod, fname); |
| 1799 | free(fname); |
| 1800 | } |
| 1801 | if (escaped_url != NULL) |
| 1802 | free(escaped_url); |
| 1803 | oval_variable_model_iterator_free(varmod_itr); |
| 1804 | } |
| 1805 | } |
| 1806 | return 0; |
| 1807 | } |
| 1808 | |
| 1809 | int xccdf_session_export_check_engine_plugins(struct xccdf_session *session) |
| 1810 | { |
| 1811 | if (!session->export.check_engine_plugins_results) |
| 1812 | return 0; |
| 1813 | |
| 1814 | struct oscap_iterator *it = oscap_iterator_new(session->check_engine_plugins); |
| 1815 | |
| 1816 | int ret = 0; |
| 1817 | while (oscap_iterator_has_more(it)) { |
| 1818 | struct check_engine_plugin_def *plugin = (struct check_engine_plugin_def *)oscap_iterator_next(it); |
| 1819 | |
| 1820 | if (check_engine_plugin_export_results( |
| 1821 | plugin, |
| 1822 | session->xccdf.policy_model, |
| 1823 | session->validate && session->full_validation, |
| 1824 | oscap_source_readable_origin(session->xccdf.source)) != 0) |
| 1825 | ret = 1; |
| 1826 | } |
| 1827 | |
| 1828 | oscap_iterator_free(it); |
| 1829 | |
| 1830 | return ret; |
| 1831 | } |
| 1832 | |
| 1833 | int xccdf_session_export_arf(struct xccdf_session *session) |
| 1834 | { |
| 1835 | if (session->export.arf_file != NULL) { |
| 1836 | struct oscap_source* arf_source = xccdf_session_create_arf_source(session); |
| 1837 | if (arf_source == NULL) { |
| 1838 | return 1; |
| 1839 | } |
| 1840 | |
| 1841 | if (oscap_source_save_as(arf_source, NULL) != 0) { |
| 1842 | oscap_source_free(arf_source); |
| 1843 | session->oval.arf_report = NULL; |
| 1844 | return 1; |
| 1845 | } |
| 1846 | if (session->full_validation) { |
| 1847 | if (oscap_source_validate(arf_source, _reporter, NULL) != 0) { |
| 1848 | oscap_source_free(arf_source); |
| 1849 | return 1; |
| 1850 | } |
| 1851 | } |
| 1852 | } |
| 1853 | return 0; |
| 1854 | } |
| 1855 | |
| 1856 | OSCAP_GENERIC_GETTER(struct xccdf_policy_model *, xccdf_session, policy_model, xccdf.policy_model) |
| 1857 | OSCAP_GENERIC_GETTER(float, xccdf_session, base_score, xccdf.base_score); |
| 1858 | |
| 1859 | struct xccdf_policy *xccdf_session_get_xccdf_policy(const struct xccdf_session *session) |
| 1860 | { |
| 1861 | if (session->xccdf.policy_model == NULL) { |
| 1862 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Cannot build xccdf_policy."); |
| 1863 | return NULL; |
| 1864 | } |
| 1865 | return xccdf_policy_model_get_policy_by_id(session->xccdf.policy_model, session->xccdf.profile_id); |
| 1866 | } |
| 1867 | |
| 1868 | unsigned int xccdf_session_get_oval_agents_count(const struct xccdf_session *session) |
| 1869 | { |
| 1870 | unsigned int i = 0; |
| 1871 | if (session->oval.agents != NULL) |
| 1872 | while (session->oval.agents[i]) |
| 1873 | i++; |
| 1874 | return i; |
| 1875 | } |
| 1876 | |
| 1877 | unsigned int xccdf_session_get_cpe_oval_agents_count(const struct xccdf_session *session) |
| 1878 | { |
| 1879 | if (session->xccdf.policy_model == NULL) { |
| 1880 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Cannot build xccdf_policy."); |
| 1881 | return 0; |
| 1882 | } |
| 1883 | |
| 1884 | unsigned int i = 0; |
| 1885 | struct oscap_htable_iterator *it = xccdf_policy_model_get_cpe_oval_sessions(session->xccdf.policy_model); |
| 1886 | while (oscap_htable_iterator_has_more(it)) { |
| 1887 | oscap_htable_iterator_next(it); |
| 1888 | i++; |
| 1889 | } |
| 1890 | oscap_htable_iterator_free(it); |
| 1891 | |
| 1892 | return i; |
| 1893 | } |
| 1894 | |
| 1895 | bool xccdf_session_contains_fail_result(const struct xccdf_session *session) |
| 1896 | { |
| 1897 | struct xccdf_rule_result_iterator *res_it = xccdf_result_get_rule_results(session->xccdf.result); |
| 1898 | while (xccdf_rule_result_iterator_has_more(res_it)) { |
| 1899 | struct xccdf_rule_result *res = xccdf_rule_result_iterator_next(res_it); |
| 1900 | xccdf_test_result_type_t rule_result = xccdf_rule_result_get_result(res); |
| 1901 | if (rule_result == XCCDF_RESULT_FAIL || |
| 1902 | rule_result == XCCDF_RESULT_UNKNOWN || |
| 1903 | rule_result == XCCDF_RESULT_ERROR) { |
| 1904 | xccdf_rule_result_iterator_free(res_it); |
| 1905 | return true; |
| 1906 | } |
| 1907 | } |
| 1908 | xccdf_rule_result_iterator_free(res_it); |
| 1909 | return false; |
| 1910 | } |
| 1911 | |
| 1912 | struct xccdf_rule_result_iterator *xccdf_session_get_rule_results(const struct xccdf_session *session) |
| 1913 | { |
| 1914 | return xccdf_result_get_rule_results(session->xccdf.result); |
| 1915 | } |
| 1916 | |
| 1917 | int xccdf_session_remediate(struct xccdf_session *session) |
| 1918 | { |
| 1919 | int res = 0; |
| 1920 | if (session == NULL || session->xccdf.policy_model == NULL || |
| 1921 | xccdf_session_get_xccdf_policy(session) == NULL || |
| 1922 | session->xccdf.result == NULL) |
| 1923 | return 1; |
| 1924 | if(getenv("OSCAP_PROBE_ROOT") != NULL) { |
| 1925 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Can't perform remediation in offline mode: not implemented"); |
| 1926 | return 1; |
| 1927 | } |
| 1928 | xccdf_policy_model_unregister_engines(session->xccdf.policy_model, oval_sysname); |
| 1929 | if ((res = xccdf_session_load_oval(session)) != 0) |
| 1930 | return res; |
| 1931 | struct xccdf_benchmark *benchmark = xccdf_policy_get_benchmark(xccdf_session_get_xccdf_policy(session)); |
| 1932 | xccdf_result_set_version(session->xccdf.result, |
| 1933 | benchmark != NULL ? xccdf_benchmark_get_version(benchmark) : NULL); |
| 1934 | xccdf_result_fill_sysinfo(session->xccdf.result); |
| 1935 | |
| 1936 | if ((res = xccdf_policy_remediate(xccdf_session_get_xccdf_policy(session), session->xccdf.result)) != 0) |
| 1937 | return res; |
| 1938 | |
| 1939 | xccdf_result_set_start_time_current(session->xccdf.result); |
| 1940 | return xccdf_policy_recalculate_score(xccdf_session_get_xccdf_policy(session), session->xccdf.result); |
| 1941 | } |
| 1942 | |
| 1943 | int xccdf_session_build_policy_from_testresult(struct xccdf_session *session, const char *testresult_id) |
| 1944 | { |
| 1945 | if (session->xccdf.result_source == NULL) { |
| 1946 | session->xccdf.result = NULL; |
| 1947 | struct xccdf_benchmark *benchmark = xccdf_policy_model_get_benchmark(session->xccdf.policy_model); |
| 1948 | struct xccdf_result *result = xccdf_benchmark_get_result_by_id_suffix(benchmark, testresult_id); |
| 1949 | if (result == NULL) { |
| 1950 | if (testresult_id == NULL) |
| 1951 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not find latest TestResult element."); |
| 1952 | else |
| 1953 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not find TestResult/@id=\"%s\"", testresult_id); |
| 1954 | return 1; |
| 1955 | } |
| 1956 | session->xccdf.result = xccdf_result_clone(result); |
| 1957 | } |
| 1958 | |
| 1959 | const char *profile_id = xccdf_result_get_profile(session->xccdf.result); |
| 1960 | if (xccdf_session_set_profile_id(session, profile_id) == false) { |
| 1961 | oscap_seterr(OSCAP_EFAMILY_OSCAP, |
| 1962 | "Could not find Profile/@id=\"%s\" to build policy from TestResult/@id=\"%s\"", |
| 1963 | profile_id, testresult_id); |
| 1964 | return 1; |
| 1965 | } |
| 1966 | struct xccdf_policy *xccdf_policy = xccdf_session_get_xccdf_policy(session); |
| 1967 | if (xccdf_policy == NULL) |
| 1968 | return 1; |
| 1969 | xccdf_policy_add_result(xccdf_policy, session->xccdf.result); |
| 1970 | return 0; |
| 1971 | } |
| 1972 | |
| 1973 | int xccdf_session_add_report_from_source(struct xccdf_session *session, struct oscap_source *report_source) |
| 1974 | { |
| 1975 | struct xccdf_result *result = xccdf_result_import_source(report_source); |
| 1976 | if (result == NULL) { |
| 1977 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not find TestResult element."); |
| 1978 | return 1; |
| 1979 | } |
| 1980 | session->xccdf.result_source = report_source; |
| 1981 | session->xccdf.result = result; |
| 1982 | return 0; |
| 1983 | } |
| 1984 | |
| 1985 | int xccdf_session_generate_guide(struct xccdf_session *session, const char *outfile) |
| 1986 | { |
| 1987 | struct xccdf_policy *policy = xccdf_session_get_xccdf_policy(session); |
| 1988 | if (policy == NULL) { |
| 1989 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not get XCCDF policy."); |
| 1990 | return 1; |
| 1991 | } |
| 1992 | struct xccdf_policy_model *model = xccdf_policy_get_model(policy); |
| 1993 | if (model == NULL) { |
| 1994 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not get XCCDF policy model."); |
| 1995 | return 1; |
| 1996 | } |
| 1997 | struct xccdf_benchmark *benchmark = xccdf_policy_model_get_benchmark(model); |
| 1998 | if (benchmark == NULL) { |
| 1999 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not get XCCDF benchmark."); |
| 2000 | return 1; |
| 2001 | } |
| 2002 | xccdf_benchmark_include_tailored_profiles(benchmark); |
| 2003 | const char *params[] = { |
| 2004 | "oscap-version", oscap_get_version(), |
| 2005 | "benchmark_id", xccdf_benchmark_get_id(benchmark), |
| 2006 | "profile_id", xccdf_session_get_profile_id(session), |
| 2007 | NULL |
| 2008 | }; |
| 2009 | struct oscap_source *benchmark_source = xccdf_benchmark_export_source(benchmark, NULL); |
| 2010 | if (benchmark_source == NULL) { |
| 2011 | oscap_seterr(OSCAP_EFAMILY_OSCAP, |
| 2012 | "Failed to export source from XCCDF benchmark %s.", |
| 2013 | xccdf_benchmark_get_id(benchmark)); |
| 2014 | return 1; |
| 2015 | } |
| 2016 | int ret = oscap_source_apply_xslt_path(benchmark_source, |
| 2017 | "xccdf-guide.xsl", outfile, params, oscap_path_to_xslt()); |
| 2018 | oscap_source_free(benchmark_source); |
| 2019 | if (ret < 0) { |
| 2020 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not apply XSLT."); |
| 2021 | return 1; |
| 2022 | } |
| 2023 | return 0; |
| 2024 | } |
| 2025 | |
| 2026 | int xccdf_session_export_all(struct xccdf_session *session) |
| 2027 | { |
| 2028 | int ret = 0; |
| 2029 | struct oscap_source *arf_source = NULL; |
| 2030 | |
| 2031 | if (_build_xccdf_result_source(session)) { |
| 2032 | ret = 1; |
| 2033 | goto cleanup; |
| 2034 | } |
| 2035 | |
| 2036 | if (session->export.report_file == NULL && session->export.arf_file == NULL) { |
| 2037 | goto cleanup; |
| 2038 | } |
| 2039 | |
| 2040 | arf_source = xccdf_session_extract_arf_source(session); |
| 2041 | if (arf_source == NULL) { |
| 2042 | ret = 1; |
| 2043 | goto cleanup; |
| 2044 | } |
| 2045 | |
| 2046 | if (session->export.report_file != NULL) { |
| 2047 | /* generate report */ |
| 2048 | _xccdf_gen_report(arf_source, |
| 2049 | xccdf_result_get_id(session->xccdf.result), |
| 2050 | session->export.report_file, |
| 2051 | "", |
| 2052 | (session->export.check_engine_plugins_results ? "%.result.xml" : ""), |
| 2053 | session->xccdf.profile_id == NULL ? "" : session->xccdf.profile_id |
| 2054 | ); |
| 2055 | } |
| 2056 | |
| 2057 | if (session->export.arf_file != NULL) { |
| 2058 | if (oscap_source_save_as(arf_source, NULL) != 0) { |
| 2059 | ret = 1; |
| 2060 | goto cleanup; |
| 2061 | } |
| 2062 | if (session->full_validation) { |
| 2063 | if (oscap_source_validate(arf_source, _reporter, NULL) != 0) { |
| 2064 | ret = 1; |
| 2065 | goto cleanup; |
| 2066 | } |
| 2067 | } |
| 2068 | } |
| 2069 | |
| 2070 | cleanup: |
| 2071 | oscap_source_free(arf_source); |
| 2072 | return ret; |
| 2073 | } |
| 2074 | |
| 2075 | void xccdf_session_set_reference_filter(struct xccdf_session *session, const char *reference_filter) |
| 2076 | { |
| 2077 | session->reference_parameter = reference_filter; |
| 2078 | } |
| 2079 | |
| 2080 | void xccdf_session_set_show_rule_details(struct xccdf_session *session, bool show_rule_details) |
| 2081 | { |
| 2082 | session->show_rule_details = show_rule_details; |
| 2083 | } |