Debian Codegraph @

src/XCCDF/benchmark.c

raw symbols SCIP
1 /*
2 * Copyright 2009 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 * Lukas Kuklinek <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28
29 #include "oscap_text.h"
30 #include "item.h"
31 #include "helpers.h"
32 #include "xccdf_impl.h"
33 #include "common/_error.h"
34 #include "common/debug_priv.h"
35 #include "common/elements.h"
36 #include "source/public/oscap_source.h"
37 #include "source/oscap_source_priv.h"
38
39 #include "CPE/cpedict_priv.h"
40 #include "CPE/cpelang_priv.h"
41 #include "CPE/cpe_ctx_priv.h"
42
43 #define XCCDF_SUPPORTED "1.2"
44
45 static struct oscap_htable *xccdf_benchmark_find_target_htable(const struct xccdf_benchmark *, xccdf_type_t);
46 static xmlNode *xccdf_plain_text_to_dom(const struct xccdf_plain_text *ptext, xmlDoc *doc, xmlNode *parent, const struct xccdf_version_info* version_info);
47
48 struct xccdf_backref {
49 struct xccdf_item **ptr; // pointer to a pointer that is supposed to be pointing to an item with id 'id'
50 xccdf_type_t type; // expected item type
51 char *id; // id
52 };
53
54 struct xccdf_benchmark *xccdf_benchmark_import_source(struct oscap_source *source)
55 {
56 xmlTextReader *reader = oscap_source_get_xmlTextReader(source);
57
58 while (xmlTextReaderRead(reader) == 1 && xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT) ;
59 struct xccdf_benchmark *benchmark = xccdf_benchmark_new();
60 const bool parse_result = xccdf_benchmark_parse(XITEM(benchmark), reader);
61 xmlFreeTextReader(reader);
62
63 if (!parse_result) { // parsing fatal error
64 oscap_seterr(OSCAP_EFAMILY_XML, "Failed to import XCCDF content from '%s'.", oscap_source_readable_origin(source));
65 xccdf_benchmark_free(benchmark);
66 return NULL;
67 }
68
69 // This is sadly the only place where we can pass origin file information
70 // to the CPE1 embedded dictionary (if any). It is necessary to figure out
71 // proper paths to OVAL files referenced from CPE1 dictionaries.
72
73 // FIXME: Refactor and move this somewhere else
74 struct cpe_dict_model* embedded_dict = xccdf_benchmark_get_cpe_list(benchmark);
75 if (embedded_dict != NULL) {
76 cpe_dict_model_set_origin_file(embedded_dict, oscap_source_readable_origin(source));
77 }
78
79 // same situation with embedded CPE2 lang model
80 // FIXME: Refactor and move this somewhere else
81 struct cpe_lang_model* embedded_lang_model = xccdf_benchmark_get_cpe_lang_model(benchmark);
82 if (embedded_lang_model != NULL) {
83 cpe_lang_model_set_origin_file(embedded_lang_model, oscap_source_readable_origin(source));
84 }
85 return benchmark;
86 }
87
88 struct xccdf_benchmark *xccdf_benchmark_new(void)
89 {
90 struct xccdf_item *bench = xccdf_item_new(XCCDF_BENCHMARK, NULL);
91 bench->sub.benchmark.schema_version = NULL;
92 // lists
93 bench->sub.benchmark.rear_matter = oscap_list_new();
94 bench->sub.benchmark.front_matter = oscap_list_new();
95 bench->sub.benchmark.notices = oscap_list_new();
96 bench->sub.benchmark.models = oscap_list_new();
97 bench->sub.benchmark.content = oscap_list_new();
98 bench->sub.benchmark.values = oscap_list_new();
99 bench->sub.benchmark.plain_texts = oscap_list_new();
100 bench->sub.benchmark.cpe_list = NULL;
101 bench->sub.benchmark.cpe_lang_model = NULL;
102 bench->sub.benchmark.profiles = oscap_list_new();
103 bench->sub.benchmark.results = oscap_list_new();
104 // hash tables
105 bench->sub.benchmark.items_dict = oscap_htable_new();
106 bench->sub.benchmark.profiles_dict = oscap_htable_new();
107 bench->sub.benchmark.results_dict = oscap_htable_new();
108 bench->sub.benchmark.clusters_dict = oscap_htable_new();
109
110 // add the implied default scoring model
111 struct xccdf_model *default_model = xccdf_model_new();
112 xccdf_model_set_system(default_model, "urn:xccdf:scoring:default");
113 xccdf_benchmark_add_model(XBENCHMARK(bench), default_model);
114
115 return XBENCHMARK(bench);
116 }
117
118 struct xccdf_benchmark *xccdf_benchmark_clone(const struct xccdf_benchmark *old_benchmark)
119 {
120 struct xccdf_item *new_benchmark = calloc(1, sizeof(struct xccdf_item) + sizeof(struct xccdf_benchmark_item));
121 struct xccdf_item *old = XITEM(old_benchmark);
122 xccdf_item_base_clone(&new_benchmark->item, &old->item);
123 new_benchmark->type = old->type;
124 //second argument is a pointer to the benchmark being created which will be the parent of all of its sub elements.
125 xccdf_benchmark_item_clone(new_benchmark, old_benchmark);
126 return XBENCHMARK(new_benchmark);
127 }
128
129 bool xccdf_benchmark_parse(struct xccdf_item * benchmark, xmlTextReaderPtr reader)
130 {
131 XCCDF_ASSERT_ELEMENT(reader, XCCDFE_BENCHMARK);
132 assert(benchmark != NULL);
133 if (benchmark->type != XCCDF_BENCHMARK)
134 return false;
135
136 xccdf_benchmark_set_schema_version(XBENCHMARK(benchmark), xccdf_detect_version_parser(reader));
137
138 if (!xccdf_item_process_attributes(benchmark, reader)) {
139 return false;
140 }
141 benchmark->sub.benchmark.style = xccdf_attribute_copy(reader, XCCDFA_STYLE);
142 benchmark->sub.benchmark.style_href = xccdf_attribute_copy(reader, XCCDFA_STYLE_HREF);
143 benchmark->sub.benchmark.lang = (char *) xmlTextReaderXmlLang(reader);
144 if (xccdf_attribute_has(reader, XCCDFA_RESOLVED))
145 benchmark->item.flags.resolved = xccdf_attribute_get_bool(reader, XCCDFA_RESOLVED);
146
147 int depth = oscap_element_depth(reader) + 1;
148
149 while (oscap_to_start_element(reader, depth)) {
150 struct xccdf_model *parsed_model;
151
152 switch (xccdf_element_get(reader)) {
153 case XCCDFE_NOTICE:
154 oscap_list_add(benchmark->sub.benchmark.notices, xccdf_notice_new_parse(reader));
155 break;
156 case XCCDFE_FRONT_MATTER:
157 oscap_list_add(benchmark->sub.benchmark.front_matter, oscap_text_new_parse(XCCDF_TEXT_HTMLSUB, reader));
158 break;
159 case XCCDFE_REAR_MATTER:
160 oscap_list_add(benchmark->sub.benchmark.rear_matter, oscap_text_new_parse(XCCDF_TEXT_HTMLSUB, reader));
161 break;
162 case XCCDFE_PLATFORM:
163 xccdf_item_add_applicable_platform(benchmark, reader);
164 break;
165 case XCCDFE_MODEL:
166 parsed_model = xccdf_model_new_xml(reader);
167
168 // we won't add the implied default scoring model, it is already in the benchmark
169 if (oscap_strcmp(xccdf_model_get_system(parsed_model), "urn:xccdf:scoring:default") != 0)
170 xccdf_benchmark_add_model(XBENCHMARK(benchmark), parsed_model);
171 else
172 xccdf_model_free(parsed_model);
173
174 break;
175 case XCCDFE_PLAIN_TEXT:{
176 const char *id = xccdf_attribute_get(reader, XCCDFA_ID);
177 char *data = (char *)xmlTextReaderReadInnerXml(reader);
178 if (id)
179 oscap_list_add(benchmark->sub.benchmark.plain_texts,
180 xccdf_plain_text_new_fill(id,
181 data == NULL ? "" : data));
182 xmlFree(data);
183 break;
184 }
185 case XCCDFE_CPE_LIST:{
186 struct cpe_parser_ctx *ctx = cpe_parser_ctx_from_reader(reader);
187 xccdf_benchmark_set_cpe_list(XBENCHMARK(benchmark), cpe_dict_model_parse(ctx));
188 cpe_parser_ctx_free(ctx);
189 break;
190 }
191 case XCCDFE_CPE2_PLATFORMSPEC:
192 xccdf_benchmark_set_cpe_lang_model(XBENCHMARK(benchmark), cpe_lang_model_parse(reader));
193 break;
194 case XCCDFE_PROFILE:
195 oscap_list_add(benchmark->sub.benchmark.profiles, xccdf_profile_parse(reader, benchmark));
196 break;
197 case XCCDFE_GROUP:
198 case XCCDFE_RULE:
199 xccdf_content_parse(reader, benchmark);
200 break;
201 case XCCDFE_VALUE:
202 oscap_list_add(benchmark->sub.benchmark.values, xccdf_value_parse(reader, benchmark));
203 break;
204 case XCCDFE_TESTRESULT:
205 xccdf_benchmark_add_result(XBENCHMARK(benchmark), xccdf_result_new_parse(reader));
206 break;
207 default:
208 if (!xccdf_item_process_element(benchmark, reader))
209 dW("Encountered an unknown element '%s' while parsing XCCDF benchmark.",
210 xmlTextReaderConstLocalName(reader));
211 }
212 xmlTextReaderRead(reader);
213 }
214
215 return true;
216 }
217
218 struct oscap_source *xccdf_benchmark_export_source(struct xccdf_benchmark *benchmark, const char *filename)
219 {
220 xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
221 if (doc == NULL) {
222 oscap_setxmlerr(xmlGetLastError());
223 return NULL;
224 }
225
226 xccdf_benchmark_to_dom(benchmark, doc, NULL, NULL);
227 return oscap_source_new_from_xmlDoc(doc, filename);
228 }
229
230 int xccdf_benchmark_export(struct xccdf_benchmark *benchmark, const char *file)
231 {
232 __attribute__nonnull__(file);
233
234 LIBXML_TEST_VERSION;
235
236 struct oscap_source *source = xccdf_benchmark_export_source(benchmark, file);
237 if (source == NULL) {
238 return -1;
239 }
240 int ret = oscap_source_save_as(source, NULL);
241 oscap_source_free(source);;
242 return ret;
243 }
244
245 #define OSCAP_XML_XSI BAD_CAST "http://www.w3.org/XML/1998/namespace"
246 xmlNode *xccdf_benchmark_to_dom(struct xccdf_benchmark *benchmark, xmlDocPtr doc,
247 xmlNode *parent, void *user_args)
248 {
249 const struct xccdf_version_info *version_info = xccdf_benchmark_get_schema_version(benchmark);
250
251 xmlNodePtr root_node = xccdf_item_to_dom(XITEM(benchmark), doc, parent, version_info);
252 if (parent == NULL) {
253 xmlDocSetRootElement(doc, root_node);
254 }
255
256 // FIXME!
257 //xmlNewProp(root_node, BAD_CAST "xsi:schemaLocation", BAD_CAST XCCDF_SCHEMA_LOCATION);
258
259 lookup_xsi_ns(doc);
260
261 /* Handle attributes */
262 if (xccdf_benchmark_get_resolved(benchmark))
263 xmlNewProp(root_node, BAD_CAST "resolved", BAD_CAST "1");
264 else
265 xmlNewProp(root_node, BAD_CAST "resolved", BAD_CAST "0");
266
267 const char *xmllang = xccdf_benchmark_get_lang(benchmark);
268 if (xmllang) {
269 xmlNs *ns_xml = xmlSearchNsByHref(doc, root_node, OSCAP_XML_XSI);
270 if (ns_xml == NULL) {
271 ns_xml = xmlNewNs(root_node, OSCAP_XML_XSI, BAD_CAST "xml");
272 }
273 xmlNewNsProp(root_node, ns_xml, BAD_CAST "lang", BAD_CAST xmllang);
274 }
275
276 const char *style = xccdf_benchmark_get_style(benchmark);
277 if (style)
278 xmlNewProp(root_node, BAD_CAST "style", BAD_CAST style);
279
280 const char *style_href = xccdf_benchmark_get_style_href(benchmark);
281 if (style_href)
282 xmlNewProp(root_node, BAD_CAST "style-href", BAD_CAST style_href);
283
284 // Export plain-text elements
285 struct xccdf_plain_text_iterator *plain_text_it = xccdf_benchmark_get_plain_texts(benchmark);
286 while (xccdf_plain_text_iterator_has_more(plain_text_it)) {
287 struct xccdf_plain_text *plain_text = xccdf_plain_text_iterator_next(plain_text_it);
288 xccdf_plain_text_to_dom(plain_text, doc, root_node, version_info);
289 }
290 xccdf_plain_text_iterator_free(plain_text_it);
291
292 /* Handle children */
293 if (xccdf_benchmark_get_cpe_list(benchmark)) {
294 // CPE API can only export via xmlTextWriter, we export via DOM
295 // this is used to bridge both methods
296 xmlTextWriterPtr writer = xmlNewTextWriterTree(doc, root_node, 0);
297 cpe_dict_export(xccdf_benchmark_get_cpe_list(benchmark), writer);
298 xmlFreeTextWriter(writer);
299 }
300 if (xccdf_benchmark_get_cpe_lang_model(benchmark)) {
301 // CPE API can only export via xmlTextWriter, we export via DOM
302 // this is used to bridge both methods
303 xmlTextWriterPtr writer = xmlNewTextWriterTree(doc, root_node, 0);
304 cpe_lang_export(xccdf_benchmark_get_cpe_lang_model(benchmark), writer);
305 xmlFreeTextWriter(writer);
306 }
307
308 xmlNs *ns_xccdf = lookup_xccdf_ns(doc, root_node, version_info);
309
310 struct oscap_string_iterator *platforms = xccdf_benchmark_get_platforms(benchmark);
311 while (oscap_string_iterator_has_more(platforms)) {
312 xmlNode *platform_node = xmlNewTextChild(root_node, ns_xccdf, BAD_CAST "platform", NULL);
313
314 const char *idref = oscap_string_iterator_next(platforms);
315 if (idref)
316 xmlNewProp(platform_node, BAD_CAST "idref", BAD_CAST idref);
317 }
318 oscap_string_iterator_free(platforms);
319
320 const char *version = xccdf_benchmark_get_version(benchmark);
321 if (version) {
322 xmlNodePtr version_node = xmlNewTextChild(root_node, ns_xccdf, BAD_CAST "version", BAD_CAST version);
323
324 const char *version_time = xccdf_benchmark_get_version_time(benchmark);
325 if (version_time)
326 xmlNewProp(version_node, BAD_CAST "time", BAD_CAST version_time);
327
328 const char *version_update = xccdf_benchmark_get_version_update(benchmark);
329 if (version_update)
330 xmlNewProp(version_node, BAD_CAST "update", BAD_CAST version_update);
331 }
332
333 struct oscap_string_iterator* metadata = xccdf_item_get_metadata(XITEM(benchmark));
334 while (oscap_string_iterator_has_more(metadata))
335 {
336 const char* meta = oscap_string_iterator_next(metadata);
337 xmlNode *m = oscap_xmlstr_to_dom(root_node, "metadata", meta);
338 xmlSetNs(m, ns_xccdf);
339 }
340 oscap_string_iterator_free(metadata);
341
342 OSCAP_FOR(xccdf_model, model, xccdf_benchmark_get_models(benchmark)) {
343 xmlNode *model_node = xmlNewTextChild(root_node, ns_xccdf, BAD_CAST "model", NULL);
344 xmlNewProp(model_node, BAD_CAST "system", BAD_CAST xccdf_model_get_system(model));
345 }
346
347 struct xccdf_profile_iterator *profiles = xccdf_benchmark_get_profiles(benchmark);
348 while (xccdf_profile_iterator_has_more(profiles)) {
349 struct xccdf_profile *profile = xccdf_profile_iterator_next(profiles);
350 xccdf_item_to_dom(XITEM(profile), doc, root_node, version_info);
351 }
352 xccdf_profile_iterator_free(profiles);
353
354 struct xccdf_value_iterator *values = xccdf_benchmark_get_values(benchmark);
355 while (xccdf_value_iterator_has_more(values)) {
356 struct xccdf_value *value = xccdf_value_iterator_next(values);
357 xccdf_item_to_dom(XITEM(value), doc, root_node, version_info);
358 }
359 xccdf_value_iterator_free(values);
360
361 struct xccdf_item_iterator *items = xccdf_benchmark_get_content(benchmark);
362 while (xccdf_item_iterator_has_more(items)) {
363 struct xccdf_item *item = xccdf_item_iterator_next(items);
364 if (XBENCHMARK(xccdf_item_get_parent(item)) == benchmark)
365 xccdf_item_to_dom(item, doc, root_node, version_info);
366 }
367 xccdf_item_iterator_free(items);
368
369 struct xccdf_result_iterator *results = xccdf_benchmark_get_results(benchmark);
370 while (xccdf_result_iterator_has_more(results)) {
371 struct xccdf_result *result = xccdf_result_iterator_next(results);
372 xccdf_item_to_dom(XITEM(result), doc, root_node, version_info);
373 }
374 xccdf_result_iterator_free(results);
375
376 return root_node;
377 }
378
379 void xccdf_benchmark_dump(struct xccdf_benchmark *benchmark)
380 {
381 struct xccdf_item *bench = XITEM(benchmark);
382 printf("Benchmark : %s\n", (bench ? bench->item.id : "(NULL)"));
383 if (bench) {
384 xccdf_item_print(bench, 1);
385 printf(" front m.");
386 xccdf_print_textlist(xccdf_benchmark_get_front_matter(benchmark), 2, 80, "...");
387 printf(" rear m.");
388 xccdf_print_textlist(xccdf_benchmark_get_rear_matter(benchmark), 2, 80, "...");
389 printf(" profiles");
390 oscap_list_dump(bench->sub.benchmark.profiles, xccdf_profile_dump, 2);
391 printf(" values");
392 oscap_list_dump(bench->sub.benchmark.values, xccdf_value_dump, 2);
393 printf(" content");
394 oscap_list_dump(bench->sub.benchmark.content, xccdf_item_dump, 2);
395 printf(" results");
396 oscap_list_dump(bench->sub.benchmark.results, (oscap_dump_func) xccdf_result_dump, 2);
397 }
398 }
399
400 void xccdf_benchmark_free(struct xccdf_benchmark *benchmark)
401 {
402 if (benchmark) {
403 struct xccdf_item *bench = XITEM(benchmark);
404 free(bench->sub.benchmark.style);
405 free(bench->sub.benchmark.style_href);
406 free(bench->sub.benchmark.lang);
407 oscap_list_free(bench->sub.benchmark.front_matter, (oscap_destruct_func) oscap_text_free);
408 oscap_list_free(bench->sub.benchmark.rear_matter, (oscap_destruct_func) oscap_text_free);
409 oscap_list_free(bench->sub.benchmark.notices, (oscap_destruct_func) xccdf_notice_free);
410 oscap_list_free(bench->sub.benchmark.models, (oscap_destruct_func) xccdf_model_free);
411 oscap_list_free(bench->sub.benchmark.content, (oscap_destruct_func) xccdf_item_free);
412 oscap_list_free(bench->sub.benchmark.values, (oscap_destruct_func) xccdf_value_free);
413 oscap_list_free(bench->sub.benchmark.results, (oscap_destruct_func) xccdf_result_free);
414 oscap_list_free(bench->sub.benchmark.plain_texts, (oscap_destruct_func) xccdf_plain_text_free);
415 cpe_dict_model_free(bench->sub.benchmark.cpe_list);
416 cpe_lang_model_free(bench->sub.benchmark.cpe_lang_model);
417 oscap_list_free(bench->sub.benchmark.profiles, (oscap_destruct_func) xccdf_profile_free);
418 oscap_htable_free0(bench->sub.benchmark.items_dict);
419 oscap_htable_free0(bench->sub.benchmark.profiles_dict);
420 oscap_htable_free0(bench->sub.benchmark.results_dict);
421 oscap_htable_free(bench->sub.benchmark.clusters_dict, (oscap_destruct_func) oscap_htable_free0);
422 xccdf_item_release(bench);
423 }
424 }
425
426 XCCDF_ACCESSOR_SIMPLE(benchmark, const struct xccdf_version_info*, schema_version);
427 XCCDF_ACCESSOR_STRING(benchmark, style)
428 XCCDF_ACCESSOR_STRING(benchmark, style_href)
429 XCCDF_ACCESSOR_STRING(benchmark, lang)
430 XCCDF_LISTMANIP_TEXT(benchmark, front_matter, front_matter)
431 XCCDF_LISTMANIP_TEXT(benchmark, rear_matter, rear_matter)
432 XCCDF_LISTMANIP(benchmark, notice, notices)
433 XCCDF_LISTMANIP(benchmark, model, models)
434 XCCDF_BENCHMARK_IGETTER(item, content)
435 XCCDF_BENCHMARK_IGETTER(result, results)
436 XCCDF_BENCHMARK_IGETTER(value, values)
437 XCCDF_BENCHMARK_IGETTER(profile, profiles)
438 XCCDF_ITERATOR_GEN_S(notice)
439 XCCDF_ITERATOR_GEN_S(model)
440 XCCDF_ITERATOR_GEN_S(profile)
441 XCCDF_LISTMANIP(benchmark, plain_text, plain_texts)
442 XCCDF_HTABLE_GETTER(struct xccdf_item *, benchmark, item, sub.benchmark.items_dict)
443 XCCDF_STATUS_CURRENT(benchmark)
444 OSCAP_ITERATOR_GEN(xccdf_plain_text)
445 OSCAP_ITERATOR_REMOVE_F(xccdf_plain_text)
446
447 XCCDF_ITEM_ADDER_REG(benchmark, rule, content)
448 XCCDF_ITEM_ADDER_REG(benchmark, group, content)
449 XCCDF_ITEM_ADDER_REG(benchmark, value, values)
450 XCCDF_ITEM_ADDER_REG(benchmark, profile, profiles)
451
452 bool xccdf_benchmark_add_result(struct xccdf_benchmark *benchmark, struct xccdf_result *item)
453 {
454 const char *id = xccdf_result_get_id(item);
455 if (id != NULL) {
456 // Resolve possible conflicts of the IDs in the list of TestResults.
457 struct xccdf_item *found = xccdf_benchmark_get_member(benchmark, XCCDF_RESULT, id);
458 if (found != NULL) {
459 if (found == XITEM(item))
460 return true;
461 // Here is conflict. Generate a new id.
462 char *new_id = xccdf_benchmark_gen_id(benchmark, XCCDF_RESULT, id);
463 xccdf_result_set_id(item, new_id);
464 free(new_id);
465 }
466 }
467 return xccdf_add_item(
468 ((struct xccdf_item*)benchmark)->sub.benchmark.results,
469 ((struct xccdf_item*)benchmark),
470 ((struct xccdf_item*)item),
471 "result" "-");
472 }
473
474 struct xccdf_profile *
475 xccdf_benchmark_get_profile_by_id(struct xccdf_benchmark *benchmark, const char *profile_id)
476 {
477 struct xccdf_profile_iterator *profit = xccdf_benchmark_get_profiles(benchmark);
478 while (xccdf_profile_iterator_has_more(profit)) {
479 struct xccdf_profile *profile = xccdf_profile_iterator_next(profit);
480 if (profile == NULL) {
481 assert(profile != NULL);
482 continue;
483 }
484 if (oscap_streq(xccdf_profile_get_id(profile), profile_id)) {
485 xccdf_profile_iterator_free(profit);
486 return profile;
487 }
488 }
489 xccdf_profile_iterator_free(profit);
490 return NULL;
491 }
492
493 struct xccdf_result *xccdf_benchmark_get_result_by_id(struct xccdf_benchmark *benchmark, const char *testresult_id)
494 {
495 struct xccdf_result *result = NULL;
496 if (testresult_id == NULL) {
497 /* Take the latest TestResult by default. It may turn out to be
498 * a good idea to not change that, since the SCAP-Workbench project
499 * is assuming thissemantics. */
500 struct xccdf_result_iterator * results_it = xccdf_benchmark_get_results(benchmark);
501 while (xccdf_result_iterator_has_more(results_it))
502 result = xccdf_result_iterator_next(results_it);
503 xccdf_result_iterator_free(results_it);
504 } else {
505 result = XRESULT(xccdf_benchmark_get_member(benchmark, XCCDF_RESULT, testresult_id));
506 }
507 return result;
508 }
509
510 struct xccdf_result *xccdf_benchmark_get_result_by_id_suffix(struct xccdf_benchmark *benchmark, const char *testresult_suffix)
511 {
512 struct xccdf_result *result = xccdf_benchmark_get_result_by_id(benchmark, testresult_suffix);
513 if (result != NULL)
514 return result;
515
516 struct xccdf_result_iterator *result_iterator = xccdf_benchmark_get_results(benchmark);
517
518 while (xccdf_result_iterator_has_more(result_iterator)) {
519 struct xccdf_result *temp_result = xccdf_result_iterator_next(result_iterator);
520 const char *result_full_id = xccdf_result_get_id(temp_result);
521
522 if (oscap_str_endswith(result_full_id, testresult_suffix)) {
523 if (result != NULL) {
524 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Multiple matches found:\n%s\n%s\n",
525 xccdf_result_get_id(result), result_full_id);
526 break;
527 } else {
528 result = temp_result;
529 }
530 }
531 }
532 xccdf_result_iterator_free(result_iterator);
533 return result;
534 }
535
536 bool xccdf_benchmark_add_content(struct xccdf_benchmark *bench, struct xccdf_item *item)
537 {
538 if (item == NULL) return false;
539 switch (xccdf_item_get_type(item)) {
540 case XCCDF_RULE: return xccdf_benchmark_add_rule(bench, XRULE(item));
541 case XCCDF_GROUP: return xccdf_benchmark_add_group(bench, XGROUP(item));
542 case XCCDF_VALUE: return xccdf_benchmark_add_value(bench, XVALUE(item));
543 default: return false;
544 }
545 }
546
547
548 const char *xccdf_benchmark_get_plain_text(const struct xccdf_benchmark *bench, const char *id)
549 {
550 assert(bench != NULL);
551
552 OSCAP_FOR(xccdf_plain_text, cur, xccdf_benchmark_get_plain_texts(bench)) {
553 if (oscap_streq(cur->id, id)) {
554 xccdf_plain_text_iterator_free(cur_iter);
555 return cur->text;
556 }
557 }
558 return NULL;
559 }
560
561 bool xccdf_benchmark_set_cpe_list(struct xccdf_benchmark *benchmark, struct cpe_dict_model* cpe_list)
562 {
563 struct xccdf_item *bench = XITEM(benchmark);
564
565 assert(bench != NULL);
566
567 if (bench->sub.benchmark.cpe_list)
568 cpe_dict_model_free(bench->sub.benchmark.cpe_list);
569
570 bench->sub.benchmark.cpe_list = cpe_list;
571
572 return true;
573 }
574
575 struct cpe_dict_model *xccdf_benchmark_get_cpe_list(const struct xccdf_benchmark *benchmark)
576 {
577 const struct xccdf_item *bench = XITEM(benchmark);
578
579 assert(bench != NULL);
580
581 return bench->sub.benchmark.cpe_list;
582 }
583
584 bool xccdf_benchmark_set_cpe_lang_model(struct xccdf_benchmark *benchmark, struct cpe_lang_model* cpe_lang_model)
585 {
586 struct xccdf_item *bench = XITEM(benchmark);
587
588 assert(bench != NULL);
589
590 if (bench->sub.benchmark.cpe_lang_model)
591 cpe_lang_model_free(bench->sub.benchmark.cpe_lang_model);
592
593 bench->sub.benchmark.cpe_lang_model = cpe_lang_model;
594
595 return true;
596 }
597
598 struct cpe_lang_model *xccdf_benchmark_get_cpe_lang_model(const struct xccdf_benchmark *benchmark)
599 {
600 const struct xccdf_item *bench = XITEM(benchmark);
601
602 assert(bench != NULL);
603
604 return bench->sub.benchmark.cpe_lang_model;
605 }
606
607 struct xccdf_notice *xccdf_notice_new(void)
608 {
609 struct xccdf_notice *notice = calloc(1, sizeof(struct xccdf_notice));
610 notice->text = oscap_text_new_full(XCCDF_TEXT_NOTICE, NULL, NULL);
611 return notice;
612 }
613
614 struct xccdf_notice *xccdf_notice_clone(const struct xccdf_notice * notice)
615 {
616 struct xccdf_notice *new_notice = calloc(1, sizeof(struct xccdf_notice));
617 new_notice->id = oscap_strdup(notice->id);
618 new_notice->text = oscap_text_clone(notice->text);
619 return new_notice;
620 }
621
622 struct xccdf_notice *xccdf_notice_new_parse(xmlTextReaderPtr reader)
623 {
624 struct xccdf_notice *notice = calloc(1, sizeof(struct xccdf_notice));
625 notice->id = xccdf_attribute_copy(reader, XCCDFA_ID);
626 notice->text = oscap_text_new_parse(XCCDF_TEXT_NOTICE, reader);
627 return notice;
628 }
629
630 void xccdf_notice_dump(struct xccdf_notice *notice, int depth)
631 {
632 xccdf_print_depth(depth);
633 printf("%.20s: ", xccdf_notice_get_id(notice));
634 xccdf_print_max_text(xccdf_notice_get_text(notice), 50, "...");
635 printf("\n");
636 }
637
638 void xccdf_notice_free(struct xccdf_notice *notice)
639 {
640 if (notice) {
641 free(notice->id);
642 oscap_text_free(notice->text);
643 free(notice);
644 }
645 }
646
647 OSCAP_ACCESSOR_STRING(xccdf_notice, id)
648 OSCAP_ACCESSOR_TEXT(xccdf_notice, text)
649
650 const char * xccdf_benchmark_supported(void)
651 {
652 return XCCDF_SUPPORTED;
653 }
654
655 const struct xccdf_version_info *xccdf_benchmark_supported_schema_version(void)
656 {
657 return xccdf_version_info_find(xccdf_benchmark_supported());
658 }
659
660 struct xccdf_group *xccdf_benchmark_append_new_group(struct xccdf_benchmark *benchmark, const char *id)
661 {
662 if (benchmark == NULL) return NULL;
663 struct xccdf_group *group = xccdf_group_new();
664 xccdf_group_set_id(group, id);
665 xccdf_benchmark_add_group(benchmark, group);
666 return group;
667 }
668 struct xccdf_value *xccdf_benchmark_append_new_value(struct xccdf_benchmark *benchmark, const char *id, xccdf_value_type_t type)
669 {
670 if (benchmark == NULL) return NULL;
671 struct xccdf_value *value = xccdf_value_new(type);
672 xccdf_value_set_id(value, id);
673 xccdf_benchmark_add_value(benchmark, value);
674 return value;
675 }
676 struct xccdf_rule *xccdf_benchmark_append_new_rule(struct xccdf_benchmark *benchmark, const char *id)
677 {
678 if (benchmark == NULL) return NULL;
679 struct xccdf_rule *rule = xccdf_rule_new();
680 xccdf_rule_set_id(rule, id);
681 xccdf_benchmark_add_rule(benchmark, rule);
682 return rule;
683 }
684
685 char *xccdf_benchmark_gen_id(struct xccdf_benchmark *benchmark, xccdf_type_t type, const char *prefix)
686 {
687 assert(prefix != NULL);
688
689 const char *fmt = "%s%03d";
690 int length = snprintf(NULL, 0, fmt, prefix, 0);
691 if (length < 0)
692 return NULL;
693 length++;
694 char *buff = (char *) calloc(length, sizeof(char));
695 int ret;
696 int i = 0;
697
698 do {
699 ret = snprintf(buff, length, fmt, prefix, ++i);
700 if (ret < 0 || ret > length) {
701 free(buff);
702 return NULL;
703 }
704 } while (xccdf_benchmark_get_member(benchmark, type, buff) != NULL);
705
706 return buff;
707 }
708
709 bool xccdf_add_item(struct oscap_list *list, struct xccdf_item *parent, struct xccdf_item *item, const char *prefix)
710 {
711 assert(list != NULL);
712 assert(item != NULL);
713
714 if (parent == NULL)
715 return false;
716
717 struct xccdf_benchmark *bench = xccdf_item_get_benchmark(parent);
718
719 if (bench != NULL) {
720 if (xccdf_item_get_id(item) == NULL)
721 item->item.id = xccdf_benchmark_gen_id(bench, xccdf_item_get_type(item), prefix);
722
723 if (xccdf_benchmark_register_item(bench, item)) {
724 item->item.parent = parent;
725 return oscap_list_add(list, item);
726 }
727 else
728 assert(false);
729 }
730 else return true;
731
732 return false;
733 }
734
735 struct xccdf_item *
736 xccdf_benchmark_get_member(const struct xccdf_benchmark *benchmark, xccdf_type_t type, const char *key)
737 {
738 return (struct xccdf_item *)oscap_htable_get(xccdf_benchmark_find_target_htable(benchmark, type), key);
739 }
740
741 static inline bool
742 _register_item_to_cluster(struct xccdf_benchmark *benchmark, struct xccdf_item *item)
743 {
744 if (!oscap_streq(xccdf_item_get_cluster_id(item), "")) {
745 struct oscap_htable *cluster = oscap_htable_get(XITEM(benchmark)->sub.benchmark.clusters_dict, xccdf_item_get_cluster_id(item));
746 if (cluster == NULL) {
747 cluster = oscap_htable_new();
748 if (cluster == NULL || !oscap_htable_add(XITEM(benchmark)->sub.benchmark.clusters_dict, xccdf_item_get_cluster_id(item), cluster)) {
749 oscap_htable_free0(cluster);
750 return false;
751 }
752 }
753 return oscap_htable_add(cluster, xccdf_item_get_id(item), item);
754 }
755 return true;
756 }
757
758 static inline bool
759 _unregister_item_from_cluster(struct xccdf_benchmark *benchmark, struct xccdf_item *item)
760 {
761 if (!oscap_streq(xccdf_item_get_cluster_id(item), "")) {
762 struct oscap_htable *cluster = oscap_htable_get(XITEM(benchmark)->sub.benchmark.clusters_dict, xccdf_item_get_cluster_id(item));
763 return cluster != NULL && oscap_htable_detach(cluster, xccdf_item_get_cluster_id(item)) != NULL;
764 }
765 return true;
766 }
767
768 struct oscap_htable_iterator *
769 xccdf_benchmark_get_cluster_items(struct xccdf_benchmark *benchmark, const char *cluster_id)
770 {
771 /* Get iterator through all items with given cluster-id */
772 struct oscap_htable *cluster = oscap_htable_get(XITEM(benchmark)->sub.benchmark.clusters_dict, cluster_id);
773 return (cluster == NULL) ? NULL : oscap_htable_iterator_new(cluster);
774 }
775
776 bool xccdf_benchmark_register_item(struct xccdf_benchmark *benchmark, struct xccdf_item *item)
777 {
778 if (benchmark == NULL || item == NULL || xccdf_item_get_id(item) == NULL)
779 return false;
780
781 if (xccdf_item_get_type(item) == XCCDF_PROFILE) {
782 struct xccdf_profile *profile = XPROFILE(item);
783
784 // If the profile is a tailoring profile (== comes from Tailoring element),
785 // we cannot register it to the Benchmark!
786 if (xccdf_profile_get_tailoring(profile))
787 return false;
788 }
789
790 const char *id = xccdf_item_get_id(item);
791 struct xccdf_item *found = xccdf_benchmark_get_member(benchmark, xccdf_item_get_type(item), id);
792 if (found != NULL) return found == item; // already registered
793
794 if (item->type == XCCDF_GROUP) {
795 OSCAP_FOR(xccdf_item, cnt, xccdf_group_get_content(XGROUP(item)))
796 xccdf_benchmark_register_item(benchmark, cnt);
797 OSCAP_FOR(xccdf_value, val, xccdf_group_get_values(XGROUP(item)))
798 xccdf_benchmark_register_item(benchmark, XITEM(val));
799 }
800
801 return oscap_htable_add(xccdf_benchmark_find_target_htable(benchmark, xccdf_item_get_type(item)), xccdf_item_get_id(item), item) &&
802 _register_item_to_cluster(benchmark, item);
803 }
804
805 bool xccdf_benchmark_unregister_item(struct xccdf_item *item)
806 {
807 if (item == NULL) return false;
808
809 struct xccdf_benchmark *bench = xccdf_item_get_benchmark(item);
810 if (bench == NULL) return false;
811
812 if (xccdf_item_get_type(item) == XCCDF_PROFILE) {
813 struct xccdf_profile *profile = XPROFILE(item);
814
815 // If the profile is a tailoring profile (== comes from Tailoring element),
816 // we cannot unregister it from the Benchmark as it was never there!
817 if (xccdf_profile_get_tailoring(profile))
818 return false;
819 }
820
821 assert(xccdf_benchmark_get_member(bench, xccdf_item_get_type(item), xccdf_item_get_id(item)) == item);
822
823 return oscap_htable_detach(xccdf_benchmark_find_target_htable(bench, xccdf_item_get_type(item)), xccdf_item_get_id(item)) != NULL &&
824 _unregister_item_from_cluster(bench, item);
825 }
826
827 bool xccdf_benchmark_rename_item(struct xccdf_item *item, const char *newid)
828 {
829 if (item == NULL)
830 return false;
831
832 struct xccdf_item *bench = XITEM(xccdf_item_get_benchmark(item));
833
834 if (bench != NULL) {
835 if (newid != NULL && xccdf_benchmark_get_member(XBENCHMARK(bench), xccdf_item_get_type(item), newid) != NULL)
836 return false; // ID already assigned
837
838 if (xccdf_item_get_id(item) != NULL)
839 xccdf_benchmark_unregister_item(item);
840
841 if (newid != NULL) {
842 oscap_htable_add(xccdf_benchmark_find_target_htable(xccdf_item_get_benchmark(item), xccdf_item_get_type(item)), newid, item);
843 _register_item_to_cluster(xccdf_item_get_benchmark(item), item);
844 }
845 }
846
847 free(item->item.id);
848 item->item.id = oscap_strdup(newid);
849
850 return true;
851 }
852
853 /**
854 * Find appropriate hashtable based on the type (xccdf type) of the item.
855 * Note that IDs of xccdf:Profile and xccdf:Item are not guaranteed to not
856 * overlap -> thus the need for separate hashtables.
857 */
858 struct oscap_htable *
859 xccdf_benchmark_find_target_htable(const struct xccdf_benchmark *benchmark, xccdf_type_t type)
860 {
861 assert(type & XCCDF_OBJECT);
862 if (type == XCCDF_PROFILE)
863 return XITEM(benchmark)->sub.benchmark.profiles_dict;
864 if (type == XCCDF_RESULT)
865 return XITEM(benchmark)->sub.benchmark.results_dict;
866 return XITEM(benchmark)->sub.benchmark.items_dict;
867 }
868
869 int xccdf_benchmark_include_tailored_profiles(struct xccdf_benchmark *benchmark)
870 {
871 struct oscap_list *profiles = XITEM(benchmark)->sub.benchmark.profiles;
872 struct oscap_htable_iterator *it = oscap_htable_iterator_new(XITEM(benchmark)->sub.benchmark.profiles_dict);
873 while(oscap_htable_iterator_has_more(it)) {
874 struct xccdf_profile *profile = oscap_htable_iterator_next_value(it);
875 if (xccdf_profile_get_tailoring(profile)) {
876 oscap_list_add(profiles, xccdf_profile_clone(profile));
877 }
878 }
879 oscap_htable_iterator_free(it);
880 return 0;
881 }
882
883 struct xccdf_plain_text *xccdf_plain_text_new(void)
884 {
885 return calloc(1, sizeof(struct xccdf_plain_text));
886 }
887
888 struct xccdf_plain_text *xccdf_plain_text_new_fill(const char *id, const char *text)
889 {
890 struct xccdf_plain_text *plain = xccdf_plain_text_new();
891 plain->id = oscap_strdup(id);
892 plain->text = oscap_strdup(text);
893 return plain;
894 }
895
896 static xmlNode *xccdf_plain_text_to_dom(const struct xccdf_plain_text *ptext, xmlDoc *doc, xmlNode *parent, const struct xccdf_version_info* version_info)
897 {
898 xmlNs *ns_xccdf = lookup_xccdf_ns(doc, parent, version_info);
899 xmlNode *ptext_node = xmlNewTextChild(parent, ns_xccdf, BAD_CAST "plain-text",
900 BAD_CAST (ptext->text == NULL ? "" : ptext->text));
901 if (ptext->id != NULL)
902 xmlNewProp(ptext_node, BAD_CAST "id", BAD_CAST ptext->id);
903 return ptext_node;
904 }
905
906 struct xccdf_plain_text * xccdf_plain_text_clone(const struct xccdf_plain_text * pt)
907 {
908 struct xccdf_plain_text *plain = calloc(1, sizeof(struct xccdf_plain_text));
909 plain->id = oscap_strdup(pt->id);
910 plain->text = oscap_strdup(pt->text);
911 return plain;
912 }
913
914 void xccdf_plain_text_free(struct xccdf_plain_text *plain)
915 {
916 if (plain != NULL) {
917 free(plain->id);
918 free(plain->text);
919 free(plain);
920 }
921 }
922
923 OSCAP_ACCESSOR_STRING(xccdf_plain_text, id)
924 OSCAP_ACCESSOR_STRING(xccdf_plain_text, text)