| 1 | /* |
| 2 | * Copyright 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 | * Author: |
| 20 | * Šimon Lukašík |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include "common/debug_priv.h" |
| 28 | #include "common/oscap_acquire.h" |
| 29 | #include "common/_error.h" |
| 30 | #include "common/list.h" |
| 31 | #include "common/oscapxml.h" |
| 32 | #include "common/public/oscap.h" |
| 33 | #include "common/util.h" |
| 34 | #include "ds_common.h" |
| 35 | #include "ds_sds_session.h" |
| 36 | #include "ds_sds_session_priv.h" |
| 37 | #include "sds_index_priv.h" |
| 38 | #include "sds_priv.h" |
| 39 | #include "source/oscap_source_priv.h" |
| 40 | #include "source/public/oscap_source.h" |
| 41 | #include "source/xslt_priv.h" |
| 42 | #include <libxml/tree.h> |
| 43 | #include "oscap_helpers.h" |
| 44 | |
| 45 | struct ds_sds_session { |
| 46 | struct oscap_source *source; ///< Source DataStream raw representation |
| 47 | struct ds_sds_index *index; ///< Source DataStream index |
| 48 | char *temp_dir; ///< Temp directory managed by the session |
| 49 | const char *target_dir; ///< Target directory for current split |
| 50 | const char *datastream_id; ///< ID of selected datastream |
| 51 | const char *checklist_id; ///< ID of selected checklist |
| 52 | const char *checklist_uri; ///< URI of selected checklist |
| 53 | struct oscap_htable *component_sources; ///< oscap_source for parsed components |
| 54 | struct oscap_htable *component_uris; ///< maps component refs to component URIs |
| 55 | bool fetch_remote_resources; ///< Allows loading of external components; |
| 56 | download_progress_calllback_t progress; ///< Callback to report progress of download. |
| 57 | const char *local_files; ///< Path to the directory where local copies of remote components are located |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * "null object" for download callback |
| 62 | */ |
| 63 | void download_progress_empty_calllback (bool warning, const char * format, ...) { |
| 64 | ; // no action |
| 65 | } |
| 66 | |
| 67 | struct ds_sds_session *ds_sds_session_new_from_source(struct oscap_source *source) |
| 68 | { |
| 69 | if (oscap_source_get_scap_type(source) != OSCAP_DOCUMENT_SDS) { |
| 70 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not create Source DataStream " |
| 71 | "session: File is not Source DataStream."); |
| 72 | return NULL; |
| 73 | } |
| 74 | struct ds_sds_session *sds_session = (struct ds_sds_session *) calloc(1, sizeof(struct ds_sds_session)); |
| 75 | sds_session->source = source; |
| 76 | sds_session->component_sources = oscap_htable_new(); |
| 77 | sds_session->component_uris = oscap_htable_new(); |
| 78 | sds_session->progress = download_progress_empty_calllback; |
| 79 | return sds_session; |
| 80 | } |
| 81 | |
| 82 | void ds_sds_session_free(struct ds_sds_session *sds_session) |
| 83 | { |
| 84 | if (sds_session != NULL) { |
| 85 | ds_sds_index_free(sds_session->index); |
| 86 | if (sds_session->temp_dir != NULL) { |
| 87 | oscap_acquire_cleanup_dir(&(sds_session->temp_dir)); |
| 88 | } |
| 89 | oscap_htable_free(sds_session->component_sources, (oscap_destruct_func) oscap_source_free); |
| 90 | oscap_htable_free(sds_session->component_uris, (oscap_destruct_func) free); |
| 91 | free(sds_session); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void ds_sds_session_reset(struct ds_sds_session *session) |
| 96 | { |
| 97 | session->checklist_id = NULL; |
| 98 | session->checklist_uri = NULL; |
| 99 | session->datastream_id = NULL; |
| 100 | session->target_dir = NULL; |
| 101 | oscap_htable_free(session->component_sources, (oscap_destruct_func) oscap_source_free); |
| 102 | session->component_sources = oscap_htable_new(); |
| 103 | oscap_htable_free(session->component_uris, (oscap_destruct_func) free); |
| 104 | session->component_uris = oscap_htable_new(); |
| 105 | } |
| 106 | |
| 107 | struct ds_sds_index *ds_sds_session_get_sds_idx(struct ds_sds_session *session) |
| 108 | { |
| 109 | if (session->index == NULL) { |
| 110 | xmlTextReader *reader = oscap_source_get_xmlTextReader(session->source); |
| 111 | if (reader == NULL) { |
| 112 | return NULL; |
| 113 | } |
| 114 | session->index = ds_sds_index_parse(reader); |
| 115 | xmlFreeTextReader(reader); |
| 116 | } |
| 117 | return session->index; |
| 118 | } |
| 119 | |
| 120 | static const char *ds_sds_session_get_temp_dir(struct ds_sds_session *session) |
| 121 | { |
| 122 | if (session->temp_dir == NULL) { |
| 123 | session->temp_dir = oscap_acquire_temp_dir(); |
| 124 | dD("SDS session created temporary directory '%s'.", session->temp_dir); |
| 125 | } |
| 126 | return session->temp_dir; |
| 127 | } |
| 128 | |
| 129 | const char *ds_sds_session_get_target_dir(struct ds_sds_session *session) |
| 130 | { |
| 131 | if (session->target_dir == NULL) { |
| 132 | session->target_dir = ds_sds_session_get_temp_dir(session); |
| 133 | } |
| 134 | return session->target_dir; |
| 135 | } |
| 136 | |
| 137 | int ds_sds_session_set_target_dir(struct ds_sds_session *session, const char *target_dir) |
| 138 | { |
| 139 | if (session->target_dir == NULL) { |
| 140 | session->target_dir = target_dir; |
| 141 | } |
| 142 | if (oscap_streq(session->target_dir, target_dir)) { |
| 143 | return 0; |
| 144 | } else { |
| 145 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Internal Error: Not implemented: Could not reset DataStream target_session in session."); |
| 146 | return 1; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | int ds_sds_session_set_datastream_id(struct ds_sds_session *session, const char *datastream_id) |
| 151 | { |
| 152 | if (session->datastream_id == NULL) { |
| 153 | session->datastream_id = datastream_id; |
| 154 | } |
| 155 | if (oscap_streq(session->datastream_id, datastream_id)) { |
| 156 | return 0; |
| 157 | } else { |
| 158 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Internal Error: Not implemented: Could not rewrite datastream_id in the session."); |
| 159 | return 1; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | const char *ds_sds_session_get_datastream_id(const struct ds_sds_session *session) |
| 164 | { |
| 165 | return session->datastream_id; |
| 166 | } |
| 167 | |
| 168 | const char *ds_sds_session_get_checklist_id(const struct ds_sds_session *session) |
| 169 | { |
| 170 | return session->checklist_id; |
| 171 | } |
| 172 | |
| 173 | const char *ds_sds_session_get_checklist_uri(const struct ds_sds_session *session) |
| 174 | { |
| 175 | return session->checklist_uri; |
| 176 | } |
| 177 | |
| 178 | struct oscap_htable *ds_sds_session_get_component_sources(struct ds_sds_session *session) |
| 179 | { |
| 180 | return session->component_sources; |
| 181 | } |
| 182 | |
| 183 | struct oscap_htable *ds_sds_session_get_component_uris(struct ds_sds_session *session) |
| 184 | { |
| 185 | return session->component_uris; |
| 186 | } |
| 187 | |
| 188 | const char *ds_sds_session_get_readable_origin(const struct ds_sds_session *session) |
| 189 | { |
| 190 | if (session->source == NULL) |
| 191 | return NULL; |
| 192 | |
| 193 | return oscap_source_readable_origin(session->source); |
| 194 | } |
| 195 | |
| 196 | struct oscap_source *ds_sds_session_select_checklist(struct ds_sds_session *session, const char *datastream_id, const char *component_id, const char *benchmark_id) |
| 197 | { |
| 198 | session->datastream_id = datastream_id; |
| 199 | session->checklist_id = component_id; |
| 200 | |
| 201 | // We only use benchmark ID if datastream ID and/or component ID were NOT supplied. |
| 202 | if (!datastream_id && !component_id && benchmark_id) { |
| 203 | if (ds_sds_index_select_checklist_by_benchmark_id(ds_sds_session_get_sds_idx(session), benchmark_id, |
| 204 | (const char **) &(session->datastream_id), (const char **) &(session->checklist_id)) != 0) { |
| 205 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Failed to locate a datastream with component-ref " |
| 206 | "that points to a component containing Benchmark with ID '%s'.", benchmark_id); |
| 207 | return NULL; |
| 208 | } |
| 209 | } |
| 210 | else { |
| 211 | if (benchmark_id) { |
| 212 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Not using benchmark ID ('%s') for component-ref lookup, " |
| 213 | "datastream ID ('%s') and/or component-ref ID ('%s') were supplied, using them instead.", |
| 214 | benchmark_id, datastream_id, component_id); |
| 215 | } |
| 216 | |
| 217 | if (ds_sds_index_select_checklist(ds_sds_session_get_sds_idx(session), (const char **) &(session->datastream_id), |
| 218 | (const char **) &(session->checklist_id)) != 0) { |
| 219 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Failed to locate a datastream with ID matching " |
| 220 | "'%s' ID and checklist inside matching '%s' ID.", |
| 221 | datastream_id == NULL ? "<any>" : datastream_id, |
| 222 | component_id == NULL ? "<any>" : component_id); |
| 223 | return NULL; |
| 224 | } |
| 225 | } |
| 226 | if (ds_sds_session_register_component_with_dependencies(session, "checklists", session->checklist_id, session->checklist_id) != 0) { |
| 227 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not extract %s with all dependencies from datastream.", session->checklist_id); |
| 228 | return NULL; |
| 229 | } |
| 230 | session->checklist_uri = oscap_htable_get(session->component_uris, session->checklist_id); |
| 231 | struct oscap_source *xccdf = oscap_htable_get(session->component_sources, session->checklist_id); |
| 232 | if (xccdf == NULL) { |
| 233 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Internal error: Could not acquire handle to '%s' source.", session->checklist_id); |
| 234 | } |
| 235 | return xccdf; |
| 236 | } |
| 237 | |
| 238 | struct oscap_source *ds_sds_session_select_tailoring(struct ds_sds_session *session, const char *component_id) |
| 239 | { |
| 240 | if (ds_sds_session_register_component_with_dependencies(session, "checklists", component_id, "tailoring.xml") != 0) { |
| 241 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not extract %s with all dependencies from datastream.", component_id); |
| 242 | return NULL; |
| 243 | } |
| 244 | struct oscap_source *tailoring = oscap_htable_get(session->component_sources, "tailoring.xml"); |
| 245 | if (tailoring == NULL) { |
| 246 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Internal error: Could not acquire handle to tailoring.xml source."); |
| 247 | } |
| 248 | return tailoring; |
| 249 | } |
| 250 | |
| 251 | xmlNode *ds_sds_session_get_selected_datastream(struct ds_sds_session *session) |
| 252 | { |
| 253 | xmlDoc *doc = oscap_source_get_xmlDoc(session->source); |
| 254 | xmlNode *datastream = ds_sds_lookup_datastream_in_collection(doc, session->datastream_id); |
| 255 | if (datastream == NULL) { |
| 256 | char *error = session->datastream_id ? |
| 257 | oscap_sprintf("Could not find any datastream of id '%s'", session->datastream_id) : |
| 258 | oscap_sprintf("Could not find any datastream inside the file"); |
| 259 | oscap_seterr(OSCAP_EFAMILY_XML, "%s", error); |
| 260 | free(error); |
| 261 | } |
| 262 | return datastream; |
| 263 | } |
| 264 | |
| 265 | xmlDoc *ds_sds_session_get_xmlDoc(struct ds_sds_session *session) |
| 266 | { |
| 267 | return oscap_source_get_xmlDoc(session->source); |
| 268 | } |
| 269 | |
| 270 | int ds_sds_session_register_component_source(struct ds_sds_session *session, const char *relative_filepath, struct oscap_source *component) |
| 271 | { |
| 272 | if (!oscap_htable_add(session->component_sources, relative_filepath, component)) { |
| 273 | dI("File %s has already been registered in Source DataStream session: %s", |
| 274 | relative_filepath, oscap_source_readable_origin(session->source)); |
| 275 | return -1; |
| 276 | } |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | struct oscap_source *ds_sds_session_get_component_by_href(struct ds_sds_session *session, const char *href) |
| 281 | { |
| 282 | struct oscap_source *component = oscap_htable_get(session->component_sources, href); |
| 283 | return component; |
| 284 | } |
| 285 | |
| 286 | bool ds_sds_session_can_register_component(struct ds_sds_session *session, const char *container_name, const char *component_id) |
| 287 | { |
| 288 | xmlNode *datastream = ds_sds_session_get_selected_datastream(session); |
| 289 | if (!datastream) |
| 290 | return false; |
| 291 | |
| 292 | xmlNodePtr container = node_get_child_element(datastream, container_name); |
| 293 | if (!container) |
| 294 | return false; |
| 295 | |
| 296 | // it's fine if component_id is NULL, it will return any component in that case |
| 297 | xmlNode *component_ref = containter_get_component_ref_by_id(container, component_id); |
| 298 | if (component_ref == NULL) |
| 299 | return false; |
| 300 | |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | int ds_sds_session_register_component_with_dependencies(struct ds_sds_session *session, const char *container_name, const char *component_id, const char *target_filename) |
| 305 | { |
| 306 | xmlNode *datastream = ds_sds_session_get_selected_datastream(session); |
| 307 | if (!datastream) { |
| 308 | return -1; |
| 309 | } |
| 310 | |
| 311 | xmlNodePtr container = node_get_child_element(datastream, container_name); |
| 312 | if (!container) { |
| 313 | if (ds_sds_session_get_datastream_id(session) == NULL) |
| 314 | oscap_seterr(OSCAP_EFAMILY_XML, "No '%s' container element found in file '%s' in the first datastream.", |
| 315 | container_name, oscap_source_readable_origin(session->source)); |
| 316 | else |
| 317 | oscap_seterr(OSCAP_EFAMILY_XML, "No '%s' container element found in file '%s' in datastream of id '%s'.", |
| 318 | container_name, oscap_source_readable_origin(session->source), ds_sds_session_get_datastream_id(session)); |
| 319 | return -1; |
| 320 | } |
| 321 | |
| 322 | int res = -1; |
| 323 | xmlNode *component_ref = containter_get_component_ref_by_id(container, component_id); |
| 324 | if (component_ref != NULL) { |
| 325 | if (target_filename == NULL) { |
| 326 | res = ds_sds_dump_component_ref(component_ref, session); |
| 327 | } else { |
| 328 | res = ds_sds_dump_component_ref_as(component_ref, session, "." , target_filename); |
| 329 | } |
| 330 | } |
| 331 | else { |
| 332 | oscap_seterr(OSCAP_EFAMILY_XML, "No '%s' component ref found in file '%s' in datastream of id '%s'.", |
| 333 | component_id, oscap_source_readable_origin(session->source), ds_sds_session_get_datastream_id(session)); |
| 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | return res; |
| 338 | } |
| 339 | |
| 340 | void ds_sds_session_configure_remote_resources(struct ds_sds_session *session, bool allowed, const char *local_files, download_progress_calllback_t callback) |
| 341 | { |
| 342 | session->fetch_remote_resources = allowed; |
| 343 | session->local_files = local_files; |
| 344 | session->progress = (callback != NULL) ? callback : download_progress_empty_calllback; |
| 345 | if (local_files != NULL) { |
| 346 | struct ds_sds_index *idx = ds_sds_session_get_sds_idx(session); |
| 347 | struct ds_stream_index_iterator *streams = ds_sds_index_get_streams(idx); |
| 348 | while (ds_stream_index_iterator_has_more(streams)) { |
| 349 | struct ds_stream_index *stream = ds_stream_index_iterator_next(streams); |
| 350 | const char *version = ds_stream_index_get_version(stream); |
| 351 | if (strcmp(version, "1.3")) { |
| 352 | ds_sds_session_remote_resources_progress(session)( |
| 353 | true, |
| 354 | "WARNING: The '--local-files' option can be used only with " |
| 355 | "SCAP 1.3 source data streams, but the provided data stream " |
| 356 | "is version '%s'. No local files will be used.\n", |
| 357 | version); |
| 358 | break; |
| 359 | } |
| 360 | |
| 361 | } |
| 362 | ds_stream_index_iterator_free(streams); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | const char *ds_sds_session_local_files(struct ds_sds_session *session) |
| 367 | { |
| 368 | return session->local_files; |
| 369 | } |
| 370 | |
| 371 | int ds_sds_session_dump_component_files(struct ds_sds_session *session) |
| 372 | { |
| 373 | return ds_dump_component_sources(session->component_sources, ds_sds_session_get_target_dir(session)); |
| 374 | } |
| 375 | |
| 376 | bool ds_sds_session_fetch_remote_resources(struct ds_sds_session *session) |
| 377 | { |
| 378 | return session->fetch_remote_resources; |
| 379 | } |
| 380 | |
| 381 | download_progress_calllback_t ds_sds_session_remote_resources_progress(struct ds_sds_session *session) |
| 382 | { |
| 383 | return session->progress; |
| 384 | } |
| 385 |