forked from OpenSCAP/openscap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathds_rds_session.c
More file actions
181 lines (166 loc) · 6.5 KB
/
Copy pathds_rds_session.c
File metadata and controls
181 lines (166 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Copyright 2014 Red Hat Inc., Durham, North Carolina.
* All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Šimon Lukašík
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "common/_error.h"
#include "common/list.h"
#include "common/oscapxml.h"
#include "common/public/oscap.h"
#include "common/util.h"
#include "ds_common.h"
#include "ds_rds_session.h"
#include "ds_rds_session_priv.h"
#include "rds_index_priv.h"
#include "rds_priv.h"
#include "source/oscap_source_priv.h"
#include "source/public/oscap_source.h"
#include "source/xslt_priv.h"
struct ds_rds_session {
struct oscap_source *source; ///< Result DataStream raw representation
struct rds_index *index; ///< Result DataStream index
const char *target_dir; ///< Target directory for current split
const char *report_id; ///< Last selected report ID
struct oscap_htable *component_sources; ///< oscap_sources for parsed contents (arf:content)
};
struct ds_rds_session *ds_rds_session_new_from_source(struct oscap_source *source)
{
if (oscap_source_get_scap_type(source) != OSCAP_DOCUMENT_ARF) {
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not create Result DataStream "
"session: File is not Result DataStream.");
return NULL;
}
struct ds_rds_session *rds_session = (struct ds_rds_session *) calloc(1, sizeof(struct ds_rds_session));
rds_session->source = source;
rds_session->component_sources = oscap_htable_new();
return rds_session;
}
void ds_rds_session_free(struct ds_rds_session *rds_session)
{
if (rds_session != NULL) {
rds_index_free(rds_session->index);
oscap_htable_free(rds_session->component_sources, (oscap_destruct_func) oscap_source_free);
free(rds_session);
}
}
struct rds_index *ds_rds_session_get_rds_idx(struct ds_rds_session *session)
{
if (session->index == NULL) {
xmlTextReader *reader = oscap_source_get_xmlTextReader(session->source);
if (reader == NULL) {
return NULL;
}
session->index = rds_index_parse(reader);
xmlFreeTextReader(reader);
}
return session->index;
}
xmlDoc *ds_rds_session_get_xmlDoc(struct ds_rds_session *session)
{
return oscap_source_get_xmlDoc(session->source);
}
const char *ds_rds_session_get_target_dir(struct ds_rds_session *session)
{
return session->target_dir;
}
int ds_rds_session_set_target_dir(struct ds_rds_session *session, const char *target_dir)
{
if (session->target_dir == NULL) {
session->target_dir = target_dir;
}
if (oscap_streq(session->target_dir, target_dir)) {
return 0;
} else {
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Internal Error: Not implemented: Could not reset DataStream target_session in session.");
return 1;
}
}
int ds_rds_session_register_component_source(struct ds_rds_session *session, const char *content_id, struct oscap_source *component)
{
if (content_id == NULL) {
// A report/asset with no id cannot be used as a hash-table key.
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Cannot register a Result DataStream component without an id.");
return -1;
}
if (!oscap_htable_add(session->component_sources, content_id, component)) {
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Content '%s' has already been register with Result DataStream session: %s",
content_id, oscap_source_readable_origin(session->source));
return -1;
}
return 0;
}
int ds_rds_session_dump_component_files(struct ds_rds_session *session)
{
return ds_dump_component_sources(session->component_sources, NULL);
}
struct oscap_source *ds_rds_session_select_report(struct ds_rds_session *session, const char *report_id)
{
if (report_id == NULL || !oscap_streq(session->report_id, report_id)) {
session->report_id = report_id;
if (rds_index_select_report(ds_rds_session_get_rds_idx(session), &(session->report_id)) != 0) {
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Failed to locate a report with ID matching '%s' ID.",
session->report_id == NULL ? "<any>" : session->report_id);
return NULL;
}
if (ds_rds_dump_arf_content(session, "reports", "report", session->report_id) != 0) {
return NULL;
}
}
return oscap_htable_get(session->component_sources, session->report_id);
}
struct oscap_source *ds_rds_session_select_report_request(struct ds_rds_session *session, const char *report_request_id)
{
if (report_request_id == NULL) {
if (session->report_id == NULL) {
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Internal Error: Not implemented: "
"Could not select report-request: '<any>': No report selected.");
return NULL;
}
struct rds_report_index *report = rds_index_get_report(ds_rds_session_get_rds_idx(session), session->report_id);
struct rds_report_request_index *request = rds_report_index_get_request(report);
if (request == NULL) {
return NULL;
}
report_request_id = rds_report_request_index_get_id(request);
}
if (ds_rds_dump_arf_content(session, "report-requests", "report-request", report_request_id) != 0) {
return NULL;
}
return oscap_htable_get(session->component_sources, report_request_id);
}
int ds_rds_session_replace_report_with_source(struct ds_rds_session *session, struct oscap_source *source)
{
xmlDoc *doc = oscap_source_get_xmlDoc(session->source);
xmlNode *reports_node = ds_rds_lookup_container(doc, "reports");
xmlNode *report_node = ds_rds_lookup_component(doc, "reports", "report", session->report_id);
xmlDOMWrapCtxtPtr wrap_ctxt = xmlDOMWrapNewCtxt();
if (xmlDOMWrapRemoveNode(wrap_ctxt, doc, report_node, 0) != 0) {
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not remove arf:report[@id='%s'] from result DataStream", session->report_id);
return 1;
}
struct oscap_source *prev_source = oscap_htable_detach(session->component_sources, session->report_id);
oscap_source_free(prev_source);
if (ds_rds_session_register_component_source(session, session->report_id, source) != 0) {
return 1;
}
return ds_rds_create_report(doc, reports_node, oscap_source_get_xmlDoc(source), session->report_id) == NULL;
}