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