| 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/_error.h" |
| 28 | #include "common/list.h" |
| 29 | #include "common/oscapxml.h" |
| 30 | #include "common/public/oscap.h" |
| 31 | #include "common/util.h" |
| 32 | #include "ds_common.h" |
| 33 | #include "ds_rds_session.h" |
| 34 | #include "ds_rds_session_priv.h" |
| 35 | #include "rds_index_priv.h" |
| 36 | #include "rds_priv.h" |
| 37 | #include "source/oscap_source_priv.h" |
| 38 | #include "source/public/oscap_source.h" |
| 39 | #include "source/xslt_priv.h" |
| 40 | |
| 41 | struct ds_rds_session { |
| 42 | struct oscap_source *source; ///< Result DataStream raw representation |
| 43 | struct rds_index *index; ///< Result DataStream index |
| 44 | const char *target_dir; ///< Target directory for current split |
| 45 | const char *report_id; ///< Last selected report ID |
| 46 | struct oscap_htable *component_sources; ///< oscap_sources for parsed contents (arf:content) |
| 47 | }; |
| 48 | |
| 49 | struct ds_rds_session *ds_rds_session_new_from_source(struct oscap_source *source) |
| 50 | { |
| 51 | if (oscap_source_get_scap_type(source) != OSCAP_DOCUMENT_ARF) { |
| 52 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not create Result DataStream " |
| 53 | "session: File is not Result DataStream."); |
| 54 | return NULL; |
| 55 | } |
| 56 | struct ds_rds_session *rds_session = (struct ds_rds_session *) calloc(1, sizeof(struct ds_rds_session)); |
| 57 | rds_session->source = source; |
| 58 | rds_session->component_sources = oscap_htable_new(); |
| 59 | return rds_session; |
| 60 | } |
| 61 | |
| 62 | void ds_rds_session_free(struct ds_rds_session *rds_session) |
| 63 | { |
| 64 | if (rds_session != NULL) { |
| 65 | rds_index_free(rds_session->index); |
| 66 | oscap_htable_free(rds_session->component_sources, (oscap_destruct_func) oscap_source_free); |
| 67 | free(rds_session); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | struct rds_index *ds_rds_session_get_rds_idx(struct ds_rds_session *session) |
| 72 | { |
| 73 | if (session->index == NULL) { |
| 74 | xmlTextReader *reader = oscap_source_get_xmlTextReader(session->source); |
| 75 | if (reader == NULL) { |
| 76 | return NULL; |
| 77 | } |
| 78 | session->index = rds_index_parse(reader); |
| 79 | xmlFreeTextReader(reader); |
| 80 | } |
| 81 | return session->index; |
| 82 | } |
| 83 | |
| 84 | xmlDoc *ds_rds_session_get_xmlDoc(struct ds_rds_session *session) |
| 85 | { |
| 86 | return oscap_source_get_xmlDoc(session->source); |
| 87 | } |
| 88 | |
| 89 | const char *ds_rds_session_get_target_dir(struct ds_rds_session *session) |
| 90 | { |
| 91 | return session->target_dir; |
| 92 | } |
| 93 | |
| 94 | int ds_rds_session_set_target_dir(struct ds_rds_session *session, const char *target_dir) |
| 95 | { |
| 96 | if (session->target_dir == NULL) { |
| 97 | session->target_dir = target_dir; |
| 98 | } |
| 99 | if (oscap_streq(session->target_dir, target_dir)) { |
| 100 | return 0; |
| 101 | } else { |
| 102 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Internal Error: Not implemented: Could not reset DataStream target_session in session."); |
| 103 | return 1; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | int ds_rds_session_register_component_source(struct ds_rds_session *session, const char *content_id, struct oscap_source *component) |
| 108 | { |
| 109 | if (!oscap_htable_add(session->component_sources, content_id, component)) { |
| 110 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Content '%s' has already been register with Result DataStream session: %s", |
| 111 | content_id, oscap_source_readable_origin(session->source)); |
| 112 | return -1; |
| 113 | } |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | int ds_rds_session_dump_component_files(struct ds_rds_session *session) |
| 118 | { |
| 119 | return ds_dump_component_sources(session->component_sources, NULL); |
| 120 | } |
| 121 | |
| 122 | struct oscap_source *ds_rds_session_select_report(struct ds_rds_session *session, const char *report_id) |
| 123 | { |
| 124 | if (report_id == NULL || !oscap_streq(session->report_id, report_id)) { |
| 125 | session->report_id = report_id; |
| 126 | if (rds_index_select_report(ds_rds_session_get_rds_idx(session), &(session->report_id)) != 0) { |
| 127 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Failed to locate a report with ID matching '%s' ID.", |
| 128 | session->report_id == NULL ? "<any>" : session->report_id); |
| 129 | return NULL; |
| 130 | } |
| 131 | if (ds_rds_dump_arf_content(session, "reports", "report", session->report_id) != 0) { |
| 132 | return NULL; |
| 133 | } |
| 134 | } |
| 135 | return oscap_htable_get(session->component_sources, session->report_id); |
| 136 | } |
| 137 | |
| 138 | struct oscap_source *ds_rds_session_select_report_request(struct ds_rds_session *session, const char *report_request_id) |
| 139 | { |
| 140 | if (report_request_id == NULL) { |
| 141 | if (session->report_id == NULL) { |
| 142 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Internal Error: Not implemented: " |
| 143 | "Could not select report-request: '<any>': No report selected."); |
| 144 | return NULL; |
| 145 | } |
| 146 | struct rds_report_index *report = rds_index_get_report(ds_rds_session_get_rds_idx(session), session->report_id); |
| 147 | struct rds_report_request_index *request = rds_report_index_get_request(report); |
| 148 | if (request == NULL) { |
| 149 | return NULL; |
| 150 | } |
| 151 | report_request_id = rds_report_request_index_get_id(request); |
| 152 | } |
| 153 | if (ds_rds_dump_arf_content(session, "report-requests", "report-request", report_request_id) != 0) { |
| 154 | return NULL; |
| 155 | } |
| 156 | return oscap_htable_get(session->component_sources, report_request_id); |
| 157 | } |
| 158 | |
| 159 | int ds_rds_session_replace_report_with_source(struct ds_rds_session *session, struct oscap_source *source) |
| 160 | { |
| 161 | xmlDoc *doc = oscap_source_get_xmlDoc(session->source); |
| 162 | xmlNode *reports_node = ds_rds_lookup_container(doc, "reports"); |
| 163 | xmlNode *report_node = ds_rds_lookup_component(doc, "reports", "report", session->report_id); |
| 164 | xmlDOMWrapCtxtPtr wrap_ctxt = xmlDOMWrapNewCtxt(); |
| 165 | if (xmlDOMWrapRemoveNode(wrap_ctxt, doc, report_node, 0) != 0) { |
| 166 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not remove arf:report[@id='%s'] from result DataStream", session->report_id); |
| 167 | return 1; |
| 168 | } |
| 169 | struct oscap_source *prev_source = oscap_htable_detach(session->component_sources, session->report_id); |
| 170 | oscap_source_free(prev_source); |
| 171 | if (ds_rds_session_register_component_source(session, session->report_id, source) != 0) { |
| 172 | return 1; |
| 173 | } |
| 174 | return ds_rds_create_report(doc, reports_node, oscap_source_get_xmlDoc(source), session->report_id) == NULL; |
| 175 | } |
| 176 |