Debian Codegraph @

src/DS/sds_index.c

raw symbols SCIP
1 /*
2 * Copyright 2012--2014 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Martin Preisler <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "public/scap_ds.h"
28 #include "common/list.h"
29 #include "common/_error.h"
30 #include "common/elements.h"
31 #include "sds_index_priv.h"
32
33
34 #include <libxml/xmlreader.h>
35 #include <string.h>
36
37 struct ds_stream_index
38 {
39 char* id;
40 char* timestamp;
41 char* version;
42
43 struct oscap_stringlist* check_components;
44 struct oscap_stringlist* checklist_components;
45 struct oscap_stringlist* dictionary_components;
46 struct oscap_stringlist* extended_components;
47
48 struct oscap_htable *component_id_to_component_ref_id;
49 };
50
51 struct ds_stream_index* ds_stream_index_new(void)
52 {
53 struct ds_stream_index* ret = malloc(sizeof(struct ds_stream_index));
54
55 ret->id = NULL;
56 ret->timestamp = NULL;
57 ret->version = NULL;
58
59 ret->check_components = oscap_stringlist_new();
60 ret->checklist_components = oscap_stringlist_new();
61 ret->dictionary_components = oscap_stringlist_new();
62 ret->extended_components = oscap_stringlist_new();
63
64 ret->component_id_to_component_ref_id = oscap_htable_new();
65
66 return ret;
67 }
68
69 void ds_stream_index_free(struct ds_stream_index* s)
70 {
71 free(s->id);
72 free(s->timestamp);
73 free(s->version);
74
75 oscap_stringlist_free(s->check_components);
76 oscap_stringlist_free(s->checklist_components);
77 oscap_stringlist_free(s->dictionary_components);
78 oscap_stringlist_free(s->extended_components);
79
80 oscap_htable_free(s->component_id_to_component_ref_id, (oscap_destruct_func)free);
81
82 free(s);
83 }
84
85 const char* ds_stream_index_get_id(struct ds_stream_index* s)
86 {
87 return s->id;
88 }
89
90 const char* ds_stream_index_get_timestamp(struct ds_stream_index* s)
91 {
92 return s->timestamp;
93 }
94
95 const char* ds_stream_index_get_version(struct ds_stream_index* s)
96 {
97 return s->version;
98 }
99
100 struct oscap_string_iterator* ds_stream_index_get_checks(struct ds_stream_index* s)
101 {
102 return oscap_iterator_new((struct oscap_list*)s->check_components);
103 }
104
105 struct oscap_string_iterator* ds_stream_index_get_checklists(struct ds_stream_index* s)
106 {
107 return oscap_iterator_new((struct oscap_list*)s->checklist_components);
108 }
109
110 struct oscap_string_iterator* ds_stream_index_get_dictionaries(struct ds_stream_index* s)
111 {
112 return oscap_iterator_new((struct oscap_list*)s->dictionary_components);
113 }
114
115 static struct ds_stream_index* ds_stream_index_parse(xmlTextReaderPtr reader)
116 {
117 // sanity check
118 if (xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT ||
119 strcmp((const char*)xmlTextReaderConstLocalName(reader), "data-stream") != 0)
120 {
121 oscap_seterr(OSCAP_EFAMILY_XML,
122 "Expected to have xmlTextReader at start of <ds:data-stream>, "
123 "the current event is '%i' at '%s' instead. I refuse to parse!",
124 xmlTextReaderNodeType(reader), (const char*)xmlTextReaderConstLocalName(reader));
125
126 return NULL;
127 }
128
129 struct ds_stream_index* ret = ds_stream_index_new();
130
131 ret->id = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "id");
132 ret->timestamp = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "timestamp");
133 ret->version = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "scap-version");
134
135 // We assume well-formedness and validity of the datastream.
136 // The parser can be broken with invalid content such as:
137 // .. <checklists><checks/><component-ref ../></checklists> ..
138
139 struct oscap_stringlist* cref_target = NULL;
140 while (xmlTextReaderRead(reader) == 1)
141 {
142 int node_type = xmlTextReaderNodeType(reader);
143 const char* local_name = (const char*)xmlTextReaderConstLocalName(reader);
144
145 if (node_type == XML_READER_TYPE_END_ELEMENT && // 15 == end element
146 strcmp(local_name, "data-stream") == 0)
147 {
148 // we are done reading
149 break;
150 }
151 // the following code switches where we push component refs
152 else if (strcmp(local_name, "checklists") == 0)
153 {
154 cref_target = node_type == XML_READER_TYPE_ELEMENT ? ret->checklist_components : NULL;
155 }
156 else if (strcmp(local_name, "checks") == 0)
157 {
158 cref_target = node_type == XML_READER_TYPE_ELEMENT ? ret->check_components : NULL;
159 }
160 else if (strcmp(local_name, "dictionaries") == 0)
161 {
162 cref_target = node_type == XML_READER_TYPE_ELEMENT ? ret->dictionary_components : NULL;
163 }
164 else if (strcmp(local_name, "extended-components") == 0)
165 {
166 cref_target = node_type == XML_READER_TYPE_ELEMENT ? ret->extended_components : NULL;
167 }
168 // reading of the component refs, we only care about their ID
169 else if (strcmp(local_name, "component-ref") == 0 &&
170 node_type == XML_READER_TYPE_ELEMENT)
171 {
172 // sanity check
173 if (cref_target == NULL) {
174 oscap_seterr(OSCAP_EFAMILY_XML,
175 "Encountered <ds:component-ref> but it is either not inside "
176 "any container element or container elements interleave. "
177 "Please make sure the datastream is valid!");
178 }
179 else {
180 xmlChar *id_attr = xmlTextReaderGetAttribute(reader, BAD_CAST "id");
181 xmlChar *href_attr = xmlTextReaderGetAttributeNs(reader, BAD_CAST "href", BAD_CAST "http://www.w3.org/1999/xlink");
182
183 // this copies the id_attr string
184 oscap_stringlist_add_string(cref_target, (const char*)id_attr);
185
186 // because of the leading '#' in the href preceding the component id
187 const char *component_id = (const char*)(href_attr && href_attr[0] ? (href_attr + 1 * sizeof(char)) : NULL);
188 if (!component_id || !oscap_htable_add(ret->component_id_to_component_ref_id, component_id, (char*)id_attr)) {
189 if (component_id) {
190 oscap_seterr(OSCAP_EFAMILY_XML,
191 "There is already a mapping from component id '%s' to component-ref id '%s'. "
192 "Having multiple mappings is legal in a datastream but it may prove problematic "
193 "when selecting component-refs using XCCDF Benchmark IDs.",
194 component_id, id_attr);
195 }
196
197 xmlFree(id_attr);
198 }
199
200 xmlFree(href_attr);
201 }
202 }
203 }
204
205 return ret;
206 }
207
208 struct ds_sds_index
209 {
210 struct oscap_list *streams;
211
212 struct oscap_htable *benchmark_id_to_component_id;
213 };
214
215 struct ds_sds_index* ds_sds_index_new(void)
216 {
217 struct ds_sds_index* ret = malloc(sizeof(struct ds_sds_index));
218 ret->streams = oscap_list_new();
219
220 ret->benchmark_id_to_component_id = oscap_htable_new();
221
222 return ret;
223 }
224
225 void ds_sds_index_free(struct ds_sds_index* s)
226 {
227 if (s != NULL) {
228 oscap_list_free(s->streams, (oscap_destruct_func)ds_stream_index_free);
229
230 oscap_htable_free(s->benchmark_id_to_component_id, (oscap_destruct_func)free);
231
232 free(s);
233 }
234 }
235
236 static void ds_sds_index_add_stream(struct ds_sds_index* s, struct ds_stream_index* stream)
237 {
238 oscap_list_add(s->streams, stream);
239 }
240
241 struct ds_stream_index* ds_sds_index_get_stream(struct ds_sds_index* sds, const char* stream_id)
242 {
243 struct ds_stream_index* ret = NULL;
244
245 struct ds_stream_index_iterator* streams = ds_sds_index_get_streams(sds);
246 while (ds_stream_index_iterator_has_more(streams))
247 {
248 struct ds_stream_index* stream = ds_stream_index_iterator_next(streams);
249 if (strcmp(ds_stream_index_get_id(stream), stream_id) == 0)
250 {
251 ret = stream;
252 break;
253 }
254
255 }
256 ds_stream_index_iterator_free(streams);
257
258 return ret;
259 }
260
261 struct ds_stream_index_iterator* ds_sds_index_get_streams(struct ds_sds_index* s)
262 {
263 return (struct ds_stream_index_iterator*)oscap_iterator_new(s->streams);
264 }
265
266 static char *ds_sds_component_dig_benchmark_id(xmlTextReaderPtr reader)
267 {
268 // sanity check
269 if (xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT ||
270 strcmp((const char*)xmlTextReaderConstLocalName(reader), "component") != 0)
271 {
272 oscap_seterr(OSCAP_EFAMILY_XML,
273 "Expected to have xmlTextReader at start of <ds:component>, "
274 "the current event is '%i' at '%s' instead. I refuse to parse!",
275 xmlTextReaderNodeType(reader), (const char*)xmlTextReaderConstLocalName(reader));
276
277 return NULL;
278 }
279
280 char *ret = NULL;
281 while (xmlTextReaderRead(reader) == 1)
282 {
283 int node_type = xmlTextReaderNodeType(reader);
284 const char* local_name = (const char*)xmlTextReaderConstLocalName(reader);
285
286 if (node_type == XML_READER_TYPE_END_ELEMENT &&
287 strcmp(local_name, "component") == 0) {
288 // we are done reading
289 break;
290 }
291 else if (node_type == XML_READER_TYPE_ELEMENT &&
292 strcmp(local_name, "Benchmark") == 0) {
293 if (ret) {
294 oscap_seterr(OSCAP_EFAMILY_XML,
295 "Found 2 Benchmark elements inside a single sds:component element! "
296 "Please make sure your datastream is valid. Skipping the second Benchmark.");
297 }
298 else {
299 ret = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "id");
300 }
301 }
302 else {
303 // ignore
304 }
305 }
306
307 return ret;
308 }
309
310 struct ds_sds_index* ds_sds_index_parse(xmlTextReaderPtr reader)
311 {
312 if (!oscap_to_start_element(reader, 0)) {
313 oscap_seterr(OSCAP_EFAMILY_XML,
314 "Expected to to have start of <ds:data-stream-collection> at document root, "
315 "no elements were found! I refuse to parse!");
316
317 return NULL;
318 }
319
320 if (xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT ||
321 strcmp((const char*)xmlTextReaderConstLocalName(reader), "data-stream-collection") != 0)
322 {
323 oscap_seterr(OSCAP_EFAMILY_XML,
324 "Expected to to have start of <ds:data-stream-collection> at document root, "
325 "the current event is '%i' at '%s' instead. I refuse to parse!",
326 xmlTextReaderNodeType(reader), (const char*)xmlTextReaderConstLocalName(reader));
327
328 return NULL;
329 }
330
331 if (xmlTextReaderRead(reader) != 1)
332 {
333 oscap_setxmlerr(xmlGetLastError());
334 return NULL;
335 }
336
337 struct ds_sds_index* ret = ds_sds_index_new();
338
339 while (oscap_to_start_element(reader, 1))
340 {
341 const char* name = (const char *)xmlTextReaderConstLocalName(reader);
342 /*if (strcmp(name, "component") == 0)
343 {
344 // XSD of source datastream dictates that no ds:data-stream elements
345 // follow after ds:component element. We can safely stop reading here.
346 break;
347 }*/
348
349 if (strcmp(name, "data-stream") == 0) {
350 struct ds_stream_index* s = ds_stream_index_parse(reader);
351 // NULL means error happened, the ds_stream_index_parse already set the error
352 // in that case
353 if (s != NULL)
354 ds_sds_index_add_stream(ret, s);
355 }
356 else if (strcmp(name, "component") == 0) {
357 char *component_id = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "id");
358 char *benchmark_id = ds_sds_component_dig_benchmark_id(reader);
359
360 if (benchmark_id == NULL) {
361 free(component_id);
362 }
363 else {
364 if (!oscap_htable_add(ret->benchmark_id_to_component_id, benchmark_id, component_id)) {
365 // This benchmark ID was already in the map, therefore there must be 2 components
366 // with Benchmarks in them that have the same IDs. In this case, all bets are off
367 // when it comes to selecting a component-ref using Benchmark ID.
368 //
369 // To establish a well defined behavior, we don't "overwrite" it with the newly
370 // found benchmark but instead use the first benchmark with such ID there.
371
372 // TODO: Warning?
373 /*oscap_seterr(OSCAP_EFAMILY_XML, "There are at least two components containing "
374 "an XCCDF Benchmark with exactly the same ID ('%s'). Selecting a component-ref "
375 "using Benchmark ID will use the first component encountered and ignore "
376 "the subsequent ones with the same ID.", benchmark_id);*/
377
378 free(component_id);
379 }
380
381 free(benchmark_id);
382 }
383
384 // we are going to free the component_id string later (in ds_sds_index_free)
385 }
386 else if (strcmp(name, "extended-component") == 0) {
387 // ignore, extended-component can't be an XCCDF, therefore we are sure
388 // not to be able to dig any Benchmark @id from it
389 }
390 else if (strcmp(name, "Signature") == 0) {
391 // ignore, Signatures are to be checked externally, we don't load them in
392 }
393 else {
394 oscap_seterr(OSCAP_EFAMILY_XML, "Unknown element '%s' encountered while parsing Source DataStream to ds_sds_index, skipping...", name);
395 }
396
397 xmlTextReaderRead(reader);
398 }
399
400 return ret;
401 }
402
403 int ds_sds_index_select_checklist(struct ds_sds_index* s,
404 const char** datastream_id, const char** component_id)
405 {
406 // The following convoluted and complex code looks for a pair of
407 // datastream and checklist inside it that satisfies conditions
408 // (if any) of having specific IDs.
409 //
410 // It is possible to just pass xccdf-id in which case we have to
411 // search through all the datastreams.
412 //
413 // It is also possible to just pass datastream-id in which case we
414 // take the first checklist in given datastream.
415 //
416 // In case datastream-id is not passed AND xccdf-id is also not passed,
417 // we look for the first datastream (topdown in XML) that has any
418 // checklist. Taking the first checklist in such a datastream in this case.
419
420 int ret = 1;
421
422 struct ds_stream_index_iterator* streams_it = ds_sds_index_get_streams(s);
423 while (ds_stream_index_iterator_has_more(streams_it))
424 {
425 struct ds_stream_index* stream_idx = ds_stream_index_iterator_next(streams_it);
426 const char* stream_id = ds_stream_index_get_id(stream_idx);
427
428 if (!*datastream_id || strcmp(stream_id, *datastream_id) == 0)
429 {
430 struct oscap_string_iterator* checklists_it = ds_stream_index_get_checklists(stream_idx);
431 while (oscap_string_iterator_has_more(checklists_it))
432 {
433 const char* checklist_id = oscap_string_iterator_next(checklists_it);
434
435 if (!*component_id || strcmp(checklist_id, *component_id) == 0)
436 {
437 *component_id = checklist_id;
438 *datastream_id = ds_stream_index_get_id(stream_idx);
439 ret = 0;
440 break;
441 }
442 }
443 oscap_string_iterator_free(checklists_it);
444 }
445 }
446 ds_stream_index_iterator_free(streams_it);
447
448 return ret;
449 }
450
451 int ds_sds_index_select_checklist_by_benchmark_id(struct ds_sds_index* s,
452 const char *benchmark_id, const char **datastream_id, const char **component_ref_id)
453 {
454 const char *mapped_component_id = (const char*)oscap_htable_get(s->benchmark_id_to_component_id, benchmark_id);
455 if (!mapped_component_id) {
456 oscap_seterr(OSCAP_EFAMILY_XML, "Can't map benchmark ID '%s' to any component ID.", benchmark_id);
457 return 1;
458 }
459
460 int ret = 1;
461
462 struct ds_stream_index_iterator* streams_it = ds_sds_index_get_streams(s);
463 while (ds_stream_index_iterator_has_more(streams_it))
464 {
465 struct ds_stream_index *stream_idx = ds_stream_index_iterator_next(streams_it);
466 const char *stream_id = ds_stream_index_get_id(stream_idx);
467
468 if (!*datastream_id || strcmp(stream_id, *datastream_id) == 0)
469 {
470 const char *candidate_component_ref_id = (const char*)oscap_htable_get(stream_idx->component_id_to_component_ref_id, mapped_component_id);
471 if (candidate_component_ref_id) {
472 *datastream_id = stream_id;
473 *component_ref_id = candidate_component_ref_id;
474 ret = 0;
475 break;
476 }
477 }
478 }
479 ds_stream_index_iterator_free(streams_it);
480
481 return ret;
482 }
483
484 struct ds_stream_index *ds_stream_index_iterator_next(struct ds_stream_index_iterator *it)
485 {
486 return (struct ds_stream_index*)(oscap_iterator_next((struct oscap_iterator*)it));
487 }
488
489 bool ds_stream_index_iterator_has_more(struct ds_stream_index_iterator *it)
490 {
491 return oscap_iterator_has_more((struct oscap_iterator*)it);
492 }
493
494 void ds_stream_index_iterator_free(struct ds_stream_index_iterator *it)
495 {
496 oscap_iterator_free((struct oscap_iterator*)it);
497 }