-
Notifications
You must be signed in to change notification settings - Fork 430
Expand file tree
/
Copy pathxccdf_session.c
More file actions
2086 lines (1832 loc) · 71.6 KB
/
xccdf_session.c
File metadata and controls
2086 lines (1832 loc) · 71.6 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
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2013--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
*
*/
#include <string.h>
#include <limits.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/stat.h>
#ifdef OS_WINDOWS
#include <io.h>
#else
#include <unistd.h>
#endif
#include <oscap.h>
#include "oscap_source.h"
#include "source/oscap_source_priv.h"
#include <cpe_lang.h>
#include <OVAL/public/oval_agent_api.h>
#include <OVAL/public/oval_agent_xccdf_api.h>
#include "common/oscap_acquire.h"
#include "common/util.h"
#include "common/list.h"
#include "common/oscapxml.h"
#include "common/_error.h"
#include "common/debug_priv.h"
#include "CPE/cpe_session_priv.h"
#include "DS/public/scap_ds.h"
#include "DS/public/ds_sds_session.h"
#include "DS/ds_sds_session_priv.h"
#include "DS/rds_priv.h"
#include "DS/sds_priv.h"
#include "OVAL/results/oval_results_impl.h"
#include "source/xslt_priv.h"
#include "source/signature_priv.h"
#include "XCCDF/xccdf_impl.h"
#include "XCCDF_POLICY/public/xccdf_policy.h"
#include "XCCDF_POLICY/xccdf_policy_priv.h"
#include "XCCDF_POLICY/xccdf_policy_model_priv.h"
#include "item.h"
#include "public/xccdf_session.h"
#include "XCCDF_POLICY/public/check_engine_plugin.h"
#include "oscap_helpers.h"
struct oval_content_resource {
char *href; ///< Coresponds with xccdf:check-content-ref/\@href.
struct oscap_source *source; ///< The oscap_source representing the href resource
bool source_owned; ///< Indicates whether we need to dispose source property
};
struct xccdf_session {
const char *filename; ///< File name of SCAP (SDS or XCCDF) file for this session.
struct oscap_list *rules;
struct oscap_list *skip_rules;
const char *reference_parameter;
struct oscap_source *source; ///< Main source assigned with the main file (SDS or XCCDF)
char *temp_dir; ///< Temp directory used for decomposed component files.
struct {
struct oscap_source *source; ///< oscap_source representing the XCCDF file
struct xccdf_policy_model *policy_model;///< Active policy model.
char *profile_id; ///< Last selected profile.
struct xccdf_result *result; ///< XCCDF Result model.
float base_score; ///< Basec score of the latest evaluation.
struct oscap_source *result_source; ///< oscap_source for the exported XCCDF result
} xccdf;
struct {
struct ds_sds_session *session; ///< SDS Registry abstract structure
char *user_datastream_id; ///< Datastream id requested by user (only applicable for sds).
char *user_component_id; ///< Component id requested by user (only applicable for sds).
char *user_benchmark_id; ///< Benchmark id requested by user (only applicable for sds).
} ds;
struct {
bool fetch_remote_resources; ///< Allows download of remote resources (not applicable when user sets custom oval files)
const char *local_files; ///< Path to the directory where local copies of remote components are located
download_progress_calllback_t progress; ///< Callback to report progress of download.
struct oval_content_resource **custom_resources;///< OVAL files required by user
struct oval_content_resource **resources;///< OVAL files referenced from XCCDF
struct oval_agent_session **agents; ///< OVAL Agent Session
xccdf_policy_engine_eval_fn user_eval_fn;///< Custom OVAL engine callback
char *product_cpe; ///< CPE of scanner product.
struct oscap_source* arf_report; ///< ARF report
struct oscap_htable *result_sources; ///< mapping 'filepath' to oscap_source for OVAL results
struct oscap_htable *results_mapping; ///< mapping OVAL filename to filepath for OVAL results
struct oscap_htable *arf_report_mapping; ///< mapping OVAL filename to ARF report ID for OVAL results
} oval;
struct {
char *arf_file; ///< Path to ARF file to export
char *xccdf_file; ///< Path to XCCDF file to export
char *xccdf_stig_viewer_file; ///< Path to STIG Viewer XCCDF file to export
char *report_file; ///< Path to HTML file to export
bool oval_results; ///< Shall be the OVAL results files exported?
bool oval_variables; ///< Shall be the OVAL variable files exported?
bool check_engine_plugins_results; ///< Shall the check engine plugins results be exported?
bool without_sys_chars; ///< Shall system characteristics be exported?
bool thin_results; ///< Shall OVAL/ARF results be exported as THIN? Default is FULL
} export; ///< Settings of Session export
char *user_cpe; ///< Path to CPE dictionary required by user
struct {
struct oscap_source *user_file; ///< Tailoring file requested by the user
char *user_component_id; ///< Component ID of the Tailoring requested by the user
} tailoring;
bool validate; ///< False value indicates to skip any XSD validation.
bool full_validation; ///< True value indicates that every possible step will be validated by XSD.
bool validate_signature; ///< False value indicates to skip XML signature validation.
bool enforce_signature; ///< True value forces session to treat all XMLs without signature as invalid.
bool show_rule_details; ///< True value indicates that rule details will be shown.
struct oscap_signature_ctx *signature_ctx; ///< Paths to public keys, certificates, signature related info
struct oscap_list *check_engine_plugins; ///< Extra non-OVAL check engines that may or may not have been loaded
xccdf_session_loading_flags_t loading_flags; ///< Load referenced files while loading XCCDF
};
static int _xccdf_session_autonegotiate_tailoring_file(struct xccdf_session *session, const char *original_path);
static void _oval_content_resources_free(struct oval_content_resource **resources);
static void _xccdf_session_free_oval_agents(struct xccdf_session *session);
static void _xccdf_session_free_oval_result_sources(struct xccdf_session *session);
static const char *oscap_productname = "cpe:/a:open-scap:oscap";
static const char *oval_sysname = "http://oval.mitre.org/XMLSchema/oval-definitions-5";
struct xccdf_session *xccdf_session_new_from_source(struct oscap_source *source)
{
if (source == NULL) {
return NULL;
}
const char *filename = oscap_source_get_filepath(source);
struct xccdf_session *session = (struct xccdf_session *) calloc(1, sizeof(struct xccdf_session));
session->source = source;
oscap_document_type_t document_type = oscap_source_get_scap_type(session->source);
if (document_type == OSCAP_DOCUMENT_UNKNOWN) {
xccdf_session_free(session);
return NULL;
}
if (document_type != OSCAP_DOCUMENT_XCCDF
&& document_type != OSCAP_DOCUMENT_SDS
&& document_type != OSCAP_DOCUMENT_XCCDF_TAILORING) {
oscap_seterr(OSCAP_EFAMILY_OSCAP,
"Session input file was determined but it isn't an XCCDF file, "
"a source datastream or an XCCDF tailoring file.");
xccdf_session_free(session);
return NULL;
}
session->validate = true;
session->validate_signature = true;
session->enforce_signature = false;
session->show_rule_details = false;
session->signature_ctx = oscap_signature_ctx_new();
session->xccdf.base_score = 0;
session->oval.progress = download_progress_empty_calllback;
session->check_engine_plugins = oscap_list_new();
session->loading_flags = XCCDF_SESSION_LOAD_ALL;
session->rules = oscap_list_new();
session->skip_rules = oscap_list_new();
// We now have to switch up the oscap_sources in case we were given XCCDF tailoring
if (document_type == OSCAP_DOCUMENT_XCCDF_TAILORING) {
if (_xccdf_session_autonegotiate_tailoring_file(session, filename) != 0) {
xccdf_session_free(session);
return NULL;
}
}
dI("Created a new XCCDF session from a %s '%s'.",
oscap_document_type_to_string(document_type), filename);
return session;
}
struct xccdf_session *xccdf_session_new(const char *filename)
{
struct oscap_source *source = oscap_source_new_from_file(filename);
struct xccdf_session *session = xccdf_session_new_from_source(source);
return session;
}
static int _xccdf_session_autonegotiate_tailoring_file(struct xccdf_session *session, const char *original_path)
{
struct xccdf_tailoring* tailoring = xccdf_tailoring_import_source(session->source, NULL);
if (tailoring == NULL) {
return -1;
}
char *source_path = oscap_strdup(xccdf_tailoring_get_benchmark_ref(tailoring));
xccdf_tailoring_free(tailoring);
if (source_path == NULL) {
oscap_seterr(OSCAP_EFAMILY_OSCAP,
"Session input file was determined to be XCCDF tailoring file, "
"but it contained no benchmark reference!");
return -1;
}
char *original_path_cpy = oscap_strdup(original_path);
char *base_dir = oscap_dirname(original_path_cpy);
char *real_source_path = source_path[0] == '/' ?
oscap_strdup(source_path) : oscap_sprintf("%s/%s", base_dir, source_path);
free(base_dir);
free(original_path_cpy);
free(source_path);
struct oscap_source *real_source = oscap_source_new_from_file(real_source_path);
free(real_source_path);
if (real_source == NULL) {
oscap_seterr(OSCAP_EFAMILY_OSCAP,
"Session input file was determined to be XCCDF tailoring file, "
"but the real source file could not be loaded.");
return -1;
}
session->tailoring.user_file = session->source;
session->source = real_source;
return 0;
}
static void xccdf_session_unload_check_engine_plugins(struct xccdf_session *session);
static struct oscap_source* xccdf_session_create_arf_source(struct xccdf_session *session)
{
if (session->oval.arf_report != NULL) {
return session->oval.arf_report;
}
struct oscap_source *sds_source = NULL;
if (xccdf_session_is_sds(session)) {
sds_source = session->source;
} else {
xmlDocPtr sds_doc = ds_sds_compose_xmlDoc_from_xccdf_source(session->source);
sds_source = oscap_source_new_from_xmlDoc(sds_doc, NULL);
}
session->oval.arf_report = ds_rds_create_source(sds_source, session->tailoring.user_file, session->xccdf.result_source, session->oval.result_sources, session->oval.results_mapping, session->oval.arf_report_mapping, session->export.arf_file);
if (!xccdf_session_is_sds(session)) {
oscap_source_free(sds_source);
}
return session->oval.arf_report;
}
static struct oscap_source *xccdf_session_extract_arf_source(struct xccdf_session *session)
{
struct oscap_source *rds_source = NULL;
char *tailoring_doc_timestamp = NULL;
xmlDoc *sds_doc = NULL;
if (xccdf_session_is_sds(session)) {
sds_doc = oscap_source_pop_xmlDoc(session->source);
} else {
sds_doc = ds_sds_compose_xmlDoc_from_xccdf_source(session->source);
}
oscap_source_free(session->source);
session->source = NULL;
if (sds_doc == NULL) {
goto cleanup;
}
xmlDoc *result_file_doc = oscap_source_get_xmlDoc(session->xccdf.result_source);
if (result_file_doc == NULL) {
goto cleanup;
}
xmlDoc *tailoring_doc = NULL;
const char *tailoring_filepath = NULL;
if (session->tailoring.user_file) {
tailoring_doc = oscap_source_get_xmlDoc(session->tailoring.user_file);
if (tailoring_doc == NULL) {
goto cleanup;
}
tailoring_filepath = oscap_source_get_filepath(session->tailoring.user_file);
struct stat file_stat;
if (stat(tailoring_filepath, &file_stat) == 0) {
const size_t max_timestamp_len = 32;
tailoring_doc_timestamp = malloc(max_timestamp_len);
if (tailoring_doc_timestamp == NULL) {
oscap_seterr(OSCAP_EFAMILY_GLIBC, "Failed to allocate %zu bytes for tailoring_doc_timestamp: %s", max_timestamp_len, strerror(errno));
goto cleanup;
}
struct tm *tm_mtime = malloc(sizeof(struct tm));
#ifdef OS_WINDOWS
localtime_s(tm_mtime, &file_stat.st_mtime);
#else
localtime_r(&file_stat.st_mtime, tm_mtime);
#endif
strftime(tailoring_doc_timestamp, max_timestamp_len,
"%Y-%m-%dT%H:%M:%S", tm_mtime);
free(tm_mtime);
}
}
xmlDocPtr rds_doc = NULL;
if (ds_rds_create_from_dom(&rds_doc, sds_doc, tailoring_doc,
tailoring_filepath, tailoring_doc_timestamp, result_file_doc,
session->oval.result_sources, session->oval.results_mapping,
session->oval.arf_report_mapping) != 0) {
goto cleanup;
}
rds_source = oscap_source_new_from_xmlDoc(rds_doc, session->export.arf_file);
cleanup:
free(tailoring_doc_timestamp);
xmlFreeDoc(sds_doc);
return rds_source;
}
void xccdf_session_free(struct xccdf_session *session)
{
if (session == NULL)
return;
free(session->xccdf.profile_id);
free(session->export.xccdf_file);
free(session->export.xccdf_stig_viewer_file);
free(session->export.report_file);
free(session->export.arf_file);
_xccdf_session_free_oval_result_sources(session);
xccdf_session_unload_check_engine_plugins(session);
oscap_list_free0(session->check_engine_plugins);
free(session->user_cpe);
free(session->oval.product_cpe);
_xccdf_session_free_oval_agents(session);
_oval_content_resources_free(session->oval.custom_resources);
_oval_content_resources_free(session->oval.resources);
oscap_source_free(session->oval.arf_report);
oscap_source_free(session->xccdf.result_source);
if (session->xccdf.policy_model != NULL)
xccdf_policy_model_free(session->xccdf.policy_model);
free(session->ds.user_datastream_id);
free(session->ds.user_component_id);
free(session->ds.user_benchmark_id);
ds_sds_session_free(session->ds.session);
if (session->temp_dir != NULL)
oscap_acquire_cleanup_dir((char **) &(session->temp_dir));
oscap_source_free(session->source);
oscap_source_free(session->tailoring.user_file);
free(session->tailoring.user_component_id);
oscap_htable_free(session->oval.results_mapping, (oscap_destruct_func) free);
oscap_htable_free(session->oval.arf_report_mapping, (oscap_destruct_func) free);
oscap_signature_ctx_free(session->signature_ctx);
oscap_list_free(session->rules, (oscap_destruct_func) free);
oscap_list_free(session->skip_rules, (oscap_destruct_func) free);
free(session);
}
static void _xccdf_session_reset_oval_agents_syschar(struct xccdf_session *session)
{
if (session->oval.agents != NULL) {
for (int i=0; session->oval.agents[i]; i++) {
oval_agent_reset_syschar(session->oval.agents[i]);
}
}
}
static void _xccdf_session_reset_oval_agents_results(struct xccdf_session *session)
{
if (session->oval.agents != NULL) {
for (int i=0; session->oval.agents[i]; i++) {
oval_agent_reset_results(session->oval.agents[i]);
}
}
}
void xccdf_session_result_reset(struct xccdf_session *session)
{
if (session->xccdf.policy_model != NULL) {
oscap_list_free(session->xccdf.policy_model->policies, (oscap_destruct_func) xccdf_policy_free);
session->xccdf.policy_model->policies = oscap_list_new();
}
oscap_list_free(session->rules, (oscap_destruct_func) free);
session->rules = oscap_list_new();
oscap_list_free(session->skip_rules, (oscap_destruct_func) free);
session->skip_rules = oscap_list_new();
_xccdf_session_reset_oval_agents_syschar(session);
_xccdf_session_reset_oval_agents_results(session);
}
const char *xccdf_session_get_filename(const struct xccdf_session *session)
{
return oscap_source_readable_origin(session->source);
}
bool xccdf_session_is_sds(const struct xccdf_session *session)
{
return oscap_source_get_scap_type(session->source) == OSCAP_DOCUMENT_SDS;
}
void xccdf_session_set_rule(struct xccdf_session *session, const char *rule)
{
while (oscap_list_pop(session->rules, free))
;
oscap_list_add(session->rules, strdup(rule));
}
void xccdf_session_add_rule(struct xccdf_session *session, const char *rule)
{
oscap_list_add(session->rules, strdup(rule));
}
void xccdf_session_skip_rule(struct xccdf_session *session, const char *rule)
{
oscap_list_add(session->skip_rules, strdup(rule));
}
void xccdf_session_set_validation(struct xccdf_session *session, bool validate, bool full_validation)
{
session->validate = validate;
session->full_validation = full_validation;
}
void xccdf_session_set_signature_validation(struct xccdf_session *session, bool validate)
{
session->validate_signature = validate;
}
void xccdf_session_set_signature_enforcement(struct xccdf_session *session, bool enforce)
{
session->enforce_signature = enforce;
}
void xccdf_session_set_thin_results(struct xccdf_session *session, bool thin_results)
{
session->export.thin_results = thin_results;
}
void xccdf_session_set_datastream_id(struct xccdf_session *session, const char *datastream_id)
{
free(session->ds.user_datastream_id);
session->ds.user_datastream_id = oscap_strdup(datastream_id);
}
const char *xccdf_session_get_datastream_id(struct xccdf_session *session)
{
if (session->ds.session != NULL) {
return ds_sds_session_get_datastream_id(session->ds.session);
}
return session->ds.user_datastream_id;
}
void xccdf_session_set_component_id(struct xccdf_session *session, const char *component_id)
{
free(session->ds.user_component_id);
session->ds.user_component_id = oscap_strdup(component_id);
}
const char *xccdf_session_get_component_id(struct xccdf_session *session)
{
if (session->ds.session != NULL) {
return ds_sds_session_get_checklist_id(session->ds.session);
}
return session->ds.user_component_id;
}
void xccdf_session_set_benchmark_id(struct xccdf_session *session, const char *benchmark_id)
{
free(session->ds.user_benchmark_id);
session->ds.user_benchmark_id = oscap_strdup(benchmark_id);
}
const char *xccdf_session_get_benchmark_id(struct xccdf_session *session)
{
return session->ds.user_benchmark_id;
}
const char *xccdf_session_get_result_id(struct xccdf_session *session)
{
return xccdf_result_get_id(session->xccdf.result);
}
void xccdf_session_set_user_cpe(struct xccdf_session *session, const char *user_cpe)
{
free(session->user_cpe);
session->user_cpe = oscap_strdup(user_cpe);
}
void xccdf_session_set_user_tailoring_file(struct xccdf_session *session, const char *user_tailoring_file)
{
oscap_source_free(session->tailoring.user_file);
session->tailoring.user_file = user_tailoring_file != NULL ?
oscap_source_new_from_file(user_tailoring_file) : NULL;
}
struct oscap_source *xccdf_session_get_user_tailoring_file(struct xccdf_session *session)
{
return session->tailoring.user_file;
}
void xccdf_session_set_user_tailoring_cid(struct xccdf_session *session, const char *user_tailoring_cid)
{
free(session->tailoring.user_component_id);
session->tailoring.user_component_id = oscap_strdup(user_tailoring_cid);
}
void xccdf_session_set_custom_oval_eval_fn(struct xccdf_session *session, xccdf_policy_engine_eval_fn eval_fn)
{
session->oval.user_eval_fn = eval_fn;
}
bool xccdf_session_set_product_cpe(struct xccdf_session *session, const char *product_cpe)
{
free(session->oval.product_cpe);
session->oval.product_cpe = oscap_strdup(product_cpe);
return true;
}
void xccdf_session_set_without_sys_chars_export(struct xccdf_session *session, bool without_sys_chars)
{
session->export.without_sys_chars = without_sys_chars;
}
void xccdf_session_set_oval_results_export(struct xccdf_session *session, bool to_export_oval_results)
{
session->export.oval_results = to_export_oval_results;
}
void xccdf_session_set_oval_variables_export(struct xccdf_session *session, bool to_export_oval_variables)
{
session->export.oval_variables = to_export_oval_variables;
}
void xccdf_session_set_check_engine_plugins_results_export(struct xccdf_session *session, bool to_export_results)
{
session->export.check_engine_plugins_results = to_export_results;
}
bool xccdf_session_set_arf_export(struct xccdf_session *session, const char *arf_file)
{
free(session->export.arf_file);
session->export.arf_file = oscap_strdup(arf_file);
return true;
}
bool xccdf_session_set_xccdf_export(struct xccdf_session *session, const char *xccdf_file)
{
free(session->export.xccdf_file);
session->export.xccdf_file = oscap_strdup(xccdf_file);
return true;
}
bool xccdf_session_set_xccdf_stig_viewer_export(struct xccdf_session *session, const char *xccdf_stig_viewer_file)
{
free(session->export.xccdf_stig_viewer_file);
session->export.xccdf_stig_viewer_file = oscap_strdup(xccdf_stig_viewer_file);
return true;
}
bool xccdf_session_set_report_export(struct xccdf_session *session, const char *report_file)
{
free(session->export.report_file);
session->export.report_file = oscap_strdup(report_file);
return true;
}
bool xccdf_session_set_profile_id(struct xccdf_session *session, const char *profile_id)
{
if (xccdf_policy_model_get_policy_by_id(session->xccdf.policy_model, profile_id) == NULL)
return false;
free(session->xccdf.profile_id);
session->xccdf.profile_id = oscap_strdup(profile_id);
return true;
}
static const char *xccdf_profiles_match_profile_id(struct xccdf_profile_iterator *profile_it, const char *profile_suffix, int *match_status)
{
const char *full_profile_id = NULL;
bool multiple = false;
while (xccdf_profile_iterator_has_more(profile_it)) {
struct xccdf_profile *profile = xccdf_profile_iterator_next(profile_it);
const char *profile_id = xccdf_profile_get_id(profile);
if(oscap_str_endswith(profile_id, profile_suffix)) {
if (full_profile_id != NULL) {
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Multiple matches found:\n%s\n%s\n",
full_profile_id, profile_id);
multiple = true;
break;
} else {
full_profile_id = profile_id;
}
}
}
xccdf_profile_iterator_free(profile_it);
if (match_status != NULL) {
if (multiple) {
*match_status = OSCAP_PROFILE_MULTIPLE_MATCHES;
full_profile_id = NULL;
} else if (full_profile_id == NULL) {
*match_status = OSCAP_PROFILE_NO_MATCH;
} else {
*match_status = OSCAP_PROFILE_MATCH_OK;
}
}
return full_profile_id;
}
const char *xccdf_tailoring_match_profile_id(struct xccdf_tailoring *tailoring, const char *profile_suffix, int *match_status)
{
struct xccdf_profile_iterator *profile_it = xccdf_tailoring_get_profiles(tailoring);
return xccdf_profiles_match_profile_id(profile_it, profile_suffix, match_status);
}
const char *xccdf_benchmark_match_profile_id(struct xccdf_benchmark *bench, const char *profile_suffix, int *match_status)
{
struct xccdf_profile_iterator *profile_it = xccdf_benchmark_get_profiles(bench);
return xccdf_profiles_match_profile_id(profile_it, profile_suffix, match_status);
}
int xccdf_session_set_profile_id_by_suffix(struct xccdf_session *session, const char *profile_suffix)
{
const char *full_profile_id = NULL;
struct xccdf_benchmark *bench = xccdf_policy_model_get_benchmark(session->xccdf.policy_model);
// Tailoring Profiles
struct xccdf_tailoring *tailoring = xccdf_policy_model_get_tailoring(session->xccdf.policy_model);
int return_code = OSCAP_PROFILE_NO_MATCH;
if (tailoring != NULL) {
struct xccdf_profile_iterator *profit_tailoring = xccdf_tailoring_get_profiles(tailoring);
while (xccdf_profile_iterator_has_more(profit_tailoring)) {
struct xccdf_profile *tailoring_profile = xccdf_profile_iterator_next(profit_tailoring);
const char *tailoring_profile_id = xccdf_profile_get_id(tailoring_profile);
if (oscap_str_endswith(tailoring_profile_id, profile_suffix)) {
if (full_profile_id != NULL) {
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Multiple matches found:\n%s\n%s\n",
full_profile_id, tailoring_profile_id);
return_code = OSCAP_PROFILE_MULTIPLE_MATCHES;
break;
} else {
full_profile_id = tailoring_profile_id;
return_code = OSCAP_PROFILE_MATCH_OK;
}
}
}
xccdf_profile_iterator_free(profit_tailoring);
}
// Benchmark Profiles
if (return_code == OSCAP_PROFILE_NO_MATCH) {
full_profile_id = xccdf_benchmark_match_profile_id(bench, profile_suffix, &return_code);
}
if (return_code == OSCAP_PROFILE_MATCH_OK) {
if (!xccdf_session_set_profile_id(session, full_profile_id)) {
return_code = OSCAP_PROFILE_NO_MATCH;
}
}
return return_code;
}
const char *xccdf_session_get_profile_id(struct xccdf_session *session)
{
return session->xccdf.profile_id;
}
static struct ds_sds_session *xccdf_session_get_ds_sds_session(struct xccdf_session *session)
{
if (!xccdf_session_is_sds(session))
return NULL;
if (session->ds.session == NULL) {
session->ds.session = ds_sds_session_new_from_source(session->source);
}
return session->ds.session;
}
void xccdf_session_configure_remote_resources(struct xccdf_session *session, bool allowed, const char *local_files, download_progress_calllback_t callback)
{
if (callback == NULL) {
// With empty cb we don't have to check for NULL
// when we want to use it
callback = download_progress_empty_calllback;
}
session->oval.fetch_remote_resources = allowed;
session->oval.local_files = local_files;
session->oval.progress = callback;
if (xccdf_session_is_sds(session)) {
// We have to propagate this option to allow loading
// of external datastream components
ds_sds_session_configure_remote_resources(xccdf_session_get_ds_sds_session(session), allowed, local_files, callback);
}
}
void xccdf_session_set_remote_resources(struct xccdf_session *session, bool allowed, download_progress_calllback_t callback)
{
xccdf_session_configure_remote_resources(session, allowed, NULL, callback);
}
void xccdf_session_set_loading_flags(struct xccdf_session *session, xccdf_session_loading_flags_t flags)
{
session->loading_flags = flags;
}
/**
* Get Source DataStream index of the session.
* @memberof xccdf_session
* @warning This is applicable only on sessions which are SDS.
* @return sds index
*/
struct ds_sds_index *xccdf_session_get_sds_idx(struct xccdf_session *session)
{
if (!xccdf_session_is_sds(session))
return NULL;
return ds_sds_session_get_sds_idx(xccdf_session_get_ds_sds_session(session));
}
int xccdf_session_load(struct xccdf_session *session)
{
int ret = 0;
if (session->ds.session) {
ds_sds_session_reset(session->ds.session);
}
const xccdf_session_loading_flags_t flags = session->loading_flags;
if (flags & XCCDF_SESSION_LOAD_XCCDF) {
if ((ret = xccdf_session_load_xccdf(session)) != 0)
return ret;
}
if (flags & XCCDF_SESSION_LOAD_CPE) {
if ((ret = xccdf_session_load_cpe(session)) != 0) {
return ret;
}
}
if (flags & XCCDF_SESSION_LOAD_OVAL) {
if ((ret = xccdf_session_load_oval(session)) != 0) {
return ret;
}
}
if (flags & XCCDF_SESSION_LOAD_CHECK_ENGINE_PLUGINS) {
if ((ret = xccdf_session_load_check_engine_plugins(session)) != 0) {
return ret;
}
}
ret = xccdf_session_load_tailoring(session);
oscap_source_free_xmlDoc(session->source);
return ret;
}
static int _reporter(const char *file, int line, const char *msg, void *arg)
{
oscap_seterr(OSCAP_EFAMILY_OSCAP, "File '%s' line %d: %s", file, line, msg);
return 0;
}
static inline int _xccdf_session_load_xccdf_benchmark(struct xccdf_session *session)
{
if (session->xccdf.policy_model != NULL) {
xccdf_policy_model_free(session->xccdf.policy_model);
session->xccdf.policy_model = NULL;
}
/* Validate documents */
if (session->validate && (!xccdf_session_is_sds(session) || session->full_validation)) {
if (oscap_source_validate(session->xccdf.source, _reporter, NULL)) {
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Invalid %s (%s) content in %s",
oscap_document_type_to_string(oscap_source_get_scap_type(session->source)),
oscap_source_get_schema_version(session->source),
oscap_source_readable_origin(session->source));
return 1;
}
}
/* Load XCCDF model and XCCDF Policy model */
struct xccdf_benchmark *benchmark = xccdf_benchmark_import_source(session->xccdf.source);
if (benchmark == NULL) {
return 1;
}
oscap_source_free_xmlDoc(session->xccdf.source);
/* create the policy model */
session->xccdf.policy_model = xccdf_policy_model_new(benchmark);
if (session->xccdf.policy_model == NULL) {
xccdf_benchmark_free(benchmark);
return 1;
}
xccdf_policy_model_set_show_rule_details(session->xccdf.policy_model, session->show_rule_details);
return 0;
}
static int _acquire_xccdf_checklist_from_tailoring(struct xccdf_session* session)
{
struct ds_sds_session *ds_sds_session = xccdf_session_get_ds_sds_session(session);
if (ds_sds_session == NULL) {
return 1;
}
xmlDoc *tailoring_xmlDoc = xmlCopyDoc(oscap_source_get_xmlDoc(session->xccdf.source), true);
struct oscap_source *tailoring_source = oscap_source_new_from_xmlDoc(tailoring_xmlDoc, NULL);
struct xccdf_tailoring* tailoring = xccdf_tailoring_import_source(tailoring_source, NULL);
if (tailoring == NULL) {
/* Freeing tailoring_source also frees the tailoring_xmlDoc used to create the source */
oscap_source_free(tailoring_source);
return 1;
}
char *benchmark_ref = oscap_strdup(xccdf_tailoring_get_benchmark_ref(tailoring));
xccdf_tailoring_free(tailoring);
if (benchmark_ref == NULL) {
oscap_source_free(tailoring_source);
return 1;
}
struct oscap_source *xccdf_source = NULL;
if (oscap_str_startswith(benchmark_ref, "file:")) {
char* sep = strchr(benchmark_ref, '#');
const char *filename = benchmark_ref + strlen("file:");
const char *component_ref = NULL;
if (sep != NULL) {
component_ref = sep + 1;
*sep = '\0';
}
struct oscap_source *external_file = oscap_source_new_from_file(filename);
if (oscap_source_get_scap_type(external_file) == OSCAP_DOCUMENT_SDS) {
if (component_ref == NULL) {
oscap_seterr(OSCAP_EFAMILY_OSCAP,
"The referenced component is a datastream, but no datastream component was specified.");
oscap_source_free(external_file);
oscap_source_free(tailoring_source);
free(benchmark_ref);
return 1;
}
ds_sds_session_free(ds_sds_session);
ds_sds_session = ds_sds_session_new_from_source(external_file);
session->ds.session = ds_sds_session;
xccdf_source = ds_sds_session_select_checklist(ds_sds_session, NULL, component_ref, NULL);
oscap_source_free(session->source);
session->source = external_file;
} else {
xccdf_source = external_file;
}
} else {
xccdf_source = ds_sds_session_select_checklist(ds_sds_session,
NULL, benchmark_ref + 1, NULL);
}
if (xccdf_source == NULL) {
oscap_seterr(OSCAP_EFAMILY_OSCAP,
"Could not find benchmark referenced from tailoring as '%s'.", benchmark_ref);
oscap_source_free(tailoring_source);
free(benchmark_ref);
return 1;
}
session->xccdf.source = xccdf_source;
session->tailoring.user_file = tailoring_source;
free(benchmark_ref);
return 0;
}
int xccdf_session_load_xccdf(struct xccdf_session *session)
{
session->xccdf.source = NULL;
if (xccdf_session_is_sds(session)) {
if (session->validate) {
if (oscap_source_validate(session->source, _reporter, NULL)) {
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Invalid %s (%s) content in %s",
oscap_document_type_to_string(oscap_source_get_scap_type(session->source)),
oscap_source_get_schema_version(session->source),
oscap_source_readable_origin(session->source));
return 1;
}
}
if (session->validate_signature || session->enforce_signature) {
if (oscap_signature_validate(session->source, session->signature_ctx, session->enforce_signature)) {
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Invalid signature in %s (%s) content in %s",
oscap_document_type_to_string(oscap_source_get_scap_type(session->source)),
oscap_source_get_schema_version(session->source),
oscap_source_readable_origin(session->source));
return 1;
}
}
session->xccdf.source = ds_sds_session_select_checklist(xccdf_session_get_ds_sds_session(session), session->ds.user_datastream_id,
session->ds.user_component_id, session->ds.user_benchmark_id);
if (session->xccdf.source == NULL) {
return 1;
}
if (oscap_source_get_scap_type(session->xccdf.source) == OSCAP_DOCUMENT_XCCDF_TAILORING) {
if (_acquire_xccdf_checklist_from_tailoring(session)) {
oscap_seterr(OSCAP_EFAMILY_OSCAP,
"Could not find appropriate checklist to tailor.");
return 1;
}
}
if (oscap_source_get_scap_type(session->xccdf.source) != OSCAP_DOCUMENT_XCCDF) {
oscap_seterr(OSCAP_EFAMILY_OSCAP, "The selected checklist document is not '%s', but '%s'.",
oscap_document_type_to_string(OSCAP_DOCUMENT_XCCDF),
oscap_document_type_to_string(oscap_source_get_scap_type(session->xccdf.source)));
return 1;
}
}
else {
session->xccdf.source = session->source;
}
return _xccdf_session_load_xccdf_benchmark(session);
}
static inline void _connect_cpe_session_with_sds(struct xccdf_session *session)
{
struct cpe_session *cpe_session = xccdf_policy_model_get_cpe_session(session->xccdf.policy_model);
struct oscap_htable *sources_cache = ds_sds_session_get_component_sources(xccdf_session_get_ds_sds_session(session));
cpe_session_set_cache(cpe_session, sources_cache);
}
int xccdf_session_load_cpe(struct xccdf_session *session)
{
if (session == NULL || session->xccdf.policy_model == NULL)
return 1;
// The CPE session will load OVAL files for any CPE dicts that require it.
// These OVAL files are outside of scope of XCCDF session but we still want
// to apply the thin results settings to them.
struct cpe_session *cpe_session = xccdf_policy_model_get_cpe_session(session->xccdf.policy_model);
cpe_session_set_thin_results(cpe_session, session->export.thin_results);
/* Use custom CPE dict if given */
if (session->user_cpe != NULL) {
struct oscap_source *source = oscap_source_new_from_file(session->user_cpe);
if (oscap_source_validate(source, _reporter, NULL) != 0) {
oscap_source_free(source);
return 1;
}
if (!xccdf_policy_model_add_cpe_autodetect_source(session->xccdf.policy_model, source)) {
oscap_source_free(source);
return 1;
}
oscap_source_free(source);
}
if (xccdf_session_is_sds(session)) {
_connect_cpe_session_with_sds(session);
struct ds_sds_index *sds_idx = xccdf_session_get_sds_idx(session);
if (sds_idx == NULL) {
return -1;
}
struct ds_stream_index* stream_idx = ds_sds_index_get_stream(sds_idx, xccdf_session_get_datastream_id(session));
struct oscap_string_iterator* cpe_it = ds_stream_index_get_dictionaries(stream_idx);
// This potentially allows us to skip yet another decompose if we are sure
// there are no CPE dictionaries or language models inside the datastream.
if (oscap_string_iterator_has_more(cpe_it)) {
if (ds_sds_session_register_component_with_dependencies(xccdf_session_get_ds_sds_session(session),
"dictionaries", NULL, NULL) != 0) {
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Can't decompose CPE dictionaries from datastream '%s' "
"from file '%s'!\n", xccdf_session_get_datastream_id(session),
oscap_source_readable_origin(session->source));
oscap_string_iterator_free(cpe_it);
return 1;
}
while (oscap_string_iterator_has_more(cpe_it)) {
const char* cpe_filename = oscap_string_iterator_next(cpe_it);
struct oscap_source *source = ds_sds_session_get_component_by_href(xccdf_session_get_ds_sds_session(session), cpe_filename);
if (session->full_validation) {
if (oscap_source_validate(source, _reporter, NULL) != 0) {
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Invalid %s (%s) content in %s",
oscap_document_type_to_string(oscap_source_get_scap_type(source)),
oscap_source_get_schema_version(source),
oscap_source_readable_origin(source));
oscap_string_iterator_free(cpe_it);
return 1;
}
}
if (!xccdf_policy_model_add_cpe_autodetect_source(session->xccdf.policy_model, source)) {
oscap_string_iterator_free(cpe_it);
return 1;
}
}
}
oscap_string_iterator_free(cpe_it);
}
return 0;