Debian Codegraph @

src/XCCDF_POLICY/xccdf_policy.c

raw symbols SCIP
1 /*
2 * xccdf_policy.c
3 *
4 * Copyright 2009--2014 Red Hat Inc., Durham, North Carolina.
5 * All Rights Reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 * Created on: Dec 16, 2009
22 * Author: David Niemoller
23 * Maros Barabas <[email protected]>
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #include "xccdf_policy_priv.h"
35 #include "xccdf_policy_model_priv.h"
36 #include "xccdf_policy_engine_priv.h"
37 #include "reporter_priv.h"
38 #include "public/xccdf_policy.h"
39 #include "public/xccdf_session.h"
40 #include "public/xccdf_benchmark.h"
41 #include "public/oscap_text.h"
42
43 #include "CPE/cpelang_priv.h"
44 #include "CPE/cpe_session_priv.h"
45 #include "CPE/cpedict_priv.h"
46
47 #include "oscap_source.h"
48 #include "oval_agent_api.h"
49
50 #include "item.h"
51 #include "helpers.h"
52 #include "common/list.h"
53 #include "common/_error.h"
54 #include "common/debug_priv.h"
55 #include "common/text_priv.h"
56 #include "XCCDF/result_scoring_priv.h"
57 #include "xccdf_policy_resolve.h"
58 #include "oscap_helpers.h"
59
60 /* Macros to generate iterators, getters and setters */
61 OSCAP_GETTER(struct xccdf_benchmark *, xccdf_policy_model, benchmark)
62 OSCAP_IGETINS_GEN(xccdf_policy, xccdf_policy_model, policies, policy)
63
64 /* Macros to generate iterators, getters and setters */
65 OSCAP_GETTER(struct xccdf_policy_model *, xccdf_policy, model)
66 OSCAP_GETTER(struct xccdf_profile *, xccdf_policy, profile)
67 OSCAP_IGETTER(xccdf_select, xccdf_policy, selects)
68 OSCAP_IGETINS_GEN(xccdf_value_binding, xccdf_policy, values, value)
69 OSCAP_IGETINS(xccdf_result, xccdf_policy, results, result)
70
71 /**
72 * XCCDF value binding structure is binding between Refine values, Set values,
73 * Value element and Check export element of benchmark. These structures are
74 * binded together for exporting values to checking engine.
75 *
76 * These bindings are set during the preprocessing of profile, when policies
77 * are beeing created.
78 */
79 struct xccdf_value_binding {
80
81 char * name; ///< The name of OVAL Variable
82 xccdf_value_type_t type; ///< Type of Variable
83 char * value; ///< Value of variable
84 char * setvalue; ///< Set value if defined or NULL
85 xccdf_operator_t operator; ///< Operator of Value
86 };
87 /* Macros to generate iterators, getters and setters */
88 OSCAP_GETTER(xccdf_value_type_t, xccdf_value_binding, type)
89 OSCAP_GETTER(char *, xccdf_value_binding, name)
90 OSCAP_GETTER(char *, xccdf_value_binding, value)
91 OSCAP_GETTER(char *, xccdf_value_binding, setvalue)
92 OSCAP_GETTER(xccdf_operator_t, xccdf_value_binding, operator)
93
94 /*==========================================================================*/
95 /* Declaration of static (private to this file) functions
96 * These function shoud not be called from outside. For exporting these
97 * elements has to call parent element's
98 */
99
100 /**
101 * Filter function returning true if the item is selected, false otherwise
102 */
103 static bool xccdf_policy_filter_selected(void *item, void *policy)
104 {
105 struct xccdf_benchmark * benchmark
106 = xccdf_policy_model_get_benchmark(xccdf_policy_get_model((struct xccdf_policy *) policy));
107
108 struct xccdf_item * titem = xccdf_benchmark_get_item(benchmark, xccdf_select_get_item((struct xccdf_select *) item));
109 if (titem == NULL) {
110 dE("Item \"%s\" does not exist. Remove it from Profile !", xccdf_select_get_item((struct xccdf_select *) item));
111 return false;
112 }
113 return ((xccdf_item_get_type(titem) == XCCDF_RULE) &&
114 (xccdf_select_get_selected((struct xccdf_select *) item)));
115 }
116
117 /**
118 * Get callbacks that match sysname. Call this function to get iterator over list of callbacks
119 * that have the same system name
120 */
121 static struct oscap_iterator *
122 _xccdf_policy_get_callbacks_by_sysname(struct xccdf_policy * policy, const char * sysname)
123 {
124 return oscap_iterator_new_filter( policy->model->callbacks, (oscap_filter_func) xccdf_policy_engine_filter, (void *) sysname);
125 }
126
127 /**
128 * Get the checking engines that match the sysname.
129 */
130 static struct oscap_iterator *_xccdf_policy_get_engines_by_sysname(struct xccdf_policy *policy, const char *sysname)
131 {
132 return oscap_iterator_new_filter(policy->model->engines, (oscap_filter_func) xccdf_policy_engine_filter, (void *) sysname);
133 }
134
135 static char *_xccdf_policy_get_readable_item_text(struct xccdf_policy *policy, struct xccdf_item *item, const char *preferred_lang, struct oscap_text_iterator *it)
136 {
137 struct oscap_text *unresolved_text = oscap_textlist_get_preferred_text(it, preferred_lang);
138 if (!unresolved_text)
139 return oscap_strdup("");
140 const char *unresolved = oscap_text_get_text(unresolved_text);
141 /* Resolve <xccdf:sub> elements */
142 char *resolved = xccdf_policy_substitute(unresolved, policy);
143 /* Get rid of xhtml elements */
144 char *plaintext = _xhtml_to_plaintext(resolved);
145 free(resolved);
146 char *filtered_plaintext = oscap_remove_excess_whitespace(plaintext);
147 free(plaintext);
148 return filtered_plaintext;
149 }
150
151 char *xccdf_policy_get_readable_item_title(struct xccdf_policy *policy, struct xccdf_item *item, const char *preferred_lang)
152 {
153 struct oscap_text_iterator *title_it = xccdf_item_get_title(item);
154 char *readable_text = _xccdf_policy_get_readable_item_text(policy, item, preferred_lang, title_it);
155 oscap_text_iterator_free(title_it);
156 return readable_text;
157 }
158
159 bool xccdf_policy_get_show_rule_details(struct xccdf_policy *policy)
160 {
161 if (policy == NULL)
162 return false;
163 return policy->show_rule_details;
164 }
165
166 char *xccdf_policy_get_readable_item_description(struct xccdf_policy *policy, struct xccdf_item *item, const char *preferred_lang)
167 {
168 /* Get description in preferred language */
169 struct oscap_text_iterator *description_it = xccdf_item_get_description(item);
170 char *readable_text = _xccdf_policy_get_readable_item_text(policy, item, preferred_lang, description_it);
171 oscap_text_iterator_free(description_it);
172 return readable_text;
173 }
174
175 char *xccdf_policy_get_readable_item_rationale(struct xccdf_policy *policy, struct xccdf_item *item, const char *preferred_lang)
176 {
177 /* Get rationale in preferred language */
178 struct oscap_text_iterator *rat_it = xccdf_item_get_rationale(item);
179 char *readable_text = _xccdf_policy_get_readable_item_text(policy, item, preferred_lang, rat_it);
180 oscap_text_iterator_free(rat_it);
181 return readable_text;
182 }
183
184 /**
185 * Get last setvalue from policy that match specified id
186 */
187 static struct xccdf_setvalue * xccdf_policy_get_setvalue(struct xccdf_policy * policy, const char * id)
188 {
189 /* return NULL if id or policy is NULL but don't use
190 * __attribute_not_null__ here, it will cause abort
191 * which is not desired
192 */
193 if (id == NULL) return NULL;
194 if (policy == NULL) return NULL;
195
196 struct xccdf_setvalue_iterator * s_value_it;
197 struct xccdf_setvalue * s_value;
198 struct xccdf_profile * profile = xccdf_policy_get_profile(policy);;
199
200 /* If profile is NULL we don't have setvalue's
201 * and we return NULL, otherwise we could cause SIGSEG
202 * with accessing NULL structure
203 */
204 if (profile == NULL) return NULL;
205
206 /* We need to return *LAST* setvalue in Profile.
207 * and iterate to the end of setvalue list
208 */
209 struct xccdf_setvalue * final = NULL;
210 s_value_it = xccdf_profile_get_setvalues(profile);
211 while (xccdf_setvalue_iterator_has_more(s_value_it)) {
212 s_value = xccdf_setvalue_iterator_next(s_value_it);
213 if (!strcmp(id, xccdf_setvalue_get_item(s_value))) {
214 final = s_value;
215 }
216 }
217 xccdf_setvalue_iterator_free(s_value_it);
218
219 return final;
220 }
221
222 static struct xccdf_refine_value * xccdf_policy_get_refine_value(struct xccdf_policy * policy, const char * id)
223 {
224 /* return NULL if id or policy is NULL but don't use
225 * __attribute_not_null__ here, it will cause abort
226 * which is not desired
227 */
228 if (id == NULL) return NULL;
229 if (policy == NULL) return NULL;
230
231 struct xccdf_refine_value_iterator * r_value_it;
232 struct xccdf_refine_value * r_value;
233 struct xccdf_profile * profile = xccdf_policy_get_profile(policy);;
234
235 /* If profile is NULL we don't have setvalue's
236 * and we return NULL, otherwise we could cause SIGSEG
237 * with accessing NULL structure
238 */
239 if (profile == NULL) return NULL;
240
241 /* We need to return *LAST* setvalue in Profile.
242 * and iterate to the end of setvalue list
243 */
244 struct xccdf_refine_value * final = NULL;
245 r_value_it = xccdf_profile_get_refine_values(profile);
246 while (xccdf_refine_value_iterator_has_more(r_value_it)) {
247 r_value = xccdf_refine_value_iterator_next(r_value_it);
248 if (!strcmp(id, xccdf_refine_value_get_item(r_value))) {
249 final = r_value;
250 }
251 }
252 xccdf_refine_value_iterator_free(r_value_it);
253
254 return final;
255 }
256
257 /**
258 * Function resolves two operations:
259 * P - PASS
260 * F - FAIL
261 * U - UNKNOWN
262 * E - ERROR
263 * N - NOT CHECKED, NOT SELECTED, NOT APPLICABLE
264 *
265 * ***************************************************************
266 * AND P F U E N OR P F U E N P F U E N *
267 * P P F U E P P P P P P P neg F P U E N *
268 * F F F F F F F P F U E F *
269 * U U F U U U U P U U U U *
270 * E E F U E E E P E U E E *
271 * N P F U E N N P F U E N *
272 * ***************************************************************
273 *
274 */
275
276 static xccdf_test_result_type_t _resolve_operation(int A, int B, xccdf_bool_operator_t oper)
277 {
278
279 xccdf_test_result_type_t value = 0;
280
281 static const xccdf_test_result_type_t RESULT_TABLE_AND[9][9] = {
282 /* P F E U N K S I */
283 {0, 0, 0, 0, 0, 0, 0, 0, 0},
284 {0, 1, 2, 3, 4, 1, 1, 1, 1}, /* P (pass)*/
285 {0, 2, 2, 2, 2, 2, 2, 2, 2}, /* F (fail) */
286 {0, 4, 2, 4, 4, 4, 4, 4, 4}, /* E (error) */
287 {0, 3, 2, 3, 4, 3, 3, 3, 3}, /* U (unknown) */
288 {0, 1, 2, 3, 4, 5, 5, 5, 5}, /* N (notapplicable) */
289 {0, 1, 2, 3, 4, 5, 6, 6, 6}, /* K (notchecked) */
290 {0, 1, 2, 3, 4, 5, 6, 7, 7}, /* S (notselected) */
291 {0, 1, 2, 3, 4, 5, 6, 7, 8}};/* I (informational) */
292
293 static const xccdf_test_result_type_t RESULT_TABLE_OR[9][9] = {
294 /* P F E U N K S I */
295 {0, 0, 0, 0, 0, 0, 0, 0, 0},
296 {0, 1, 1, 1, 1, 1, 1, 1, 1}, /* P (pass)*/
297 {0, 1, 2, 3, 4, 2, 2, 2, 2}, /* F (fail) */
298 {0, 1, 4, 4, 4, 4, 4, 4, 4}, /* E (error) */
299 {0, 1, 3, 3, 4, 3, 3, 3, 3}, /* U (unknown) */
300 {0, 1, 2, 3, 4, 5, 5, 5, 5}, /* N (notapplicable) */
301 {0, 1, 2, 3, 4, 5, 6, 6, 6}, /* K (notchecked) */
302 {0, 1, 2, 3, 4, 5, 6, 7, 7}, /* S (notselected) */
303 {0, 1, 2, 3, 4, 5, 6, 7, 8}};/* I (informational) */
304
305 /* No test result can end with 0
306 */
307 if ((A == 0) || (B == 0)
308 || A > XCCDF_RESULT_INFORMATIONAL || B > XCCDF_RESULT_INFORMATIONAL) {
309 dE("Bad test results %d, %d.", A, B);
310 return 0;
311 }
312
313 switch (oper) {
314 case XCCDF_OPERATOR_AND: /* AND */
315 value = (xccdf_test_result_type_t) RESULT_TABLE_AND[A][B];
316 break;
317
318 case XCCDF_OPERATOR_OR: /* OR */
319 value = (xccdf_test_result_type_t) RESULT_TABLE_OR[A][B];
320 break;
321 default:
322 dE("Operation not supported.");
323 return 0;
324 break;
325 }
326
327 return value;
328 }
329
330 xccdf_test_result_type_t xccdf_test_result_resolve_and_operation(xccdf_test_result_type_t A, xccdf_test_result_type_t B)
331 {
332 return _resolve_operation((int)A, (int)B, XCCDF_OPERATOR_AND);
333 }
334
335 /**
336 * Handle the negation="true" paramter of xccdf:complex-check.
337 * Shall be run only once per a complex-check.
338 */
339 static xccdf_test_result_type_t _resolve_negate(xccdf_test_result_type_t value, const struct xccdf_check *check)
340 {
341 if (xccdf_check_get_negate(check)) {
342 if (value == XCCDF_RESULT_PASS) return XCCDF_RESULT_FAIL;
343 else if (value == XCCDF_RESULT_FAIL) return XCCDF_RESULT_PASS;
344 }
345 return value;
346 }
347
348
349 static void _xccdf_policy_modify_selected_final(struct xccdf_policy *policy, const char *id, bool selected)
350 {
351 static bool TRUE0 = true;
352 static bool FALSE0 = false;
353 oscap_htable_detach(policy->selected_final, id);
354 oscap_htable_add(policy->selected_final, id, selected ? &TRUE0 : &FALSE0);
355 }
356
357 /**
358 * Resolve the xccdf item. Parameter selected indicates parents selection attribute
359 * It is used to decide the final selection attribute of item
360 *
361 * If parent's group of rule item has attribute selected = FALSE, It should not be processed, neither
362 * its children. This selected attribute is iterativly passed to each child of group to set their selected
363 * attribute to false no matter of profile setting.
364 */
365 static void xccdf_policy_resolve_item(struct xccdf_policy * policy, struct xccdf_item * item, bool selected)
366 {
367 __attribute__nonnull__(policy);
368 __attribute__nonnull__(item);
369
370 bool result;
371 const char *id = xccdf_item_get_id(item);
372 if (!selected)
373 result = false;
374 else {
375 struct xccdf_select *sel = xccdf_policy_get_select_by_id(policy, id);
376 result = (sel != NULL) ? xccdf_select_get_selected(sel) : xccdf_item_get_selected(item);
377 }
378
379 if (xccdf_item_get_type(item) == XCCDF_GROUP) {
380 struct xccdf_item_iterator *child_it = xccdf_group_get_content((const struct xccdf_group *)item);
381 while (xccdf_item_iterator_has_more(child_it))
382 xccdf_policy_resolve_item(policy, xccdf_item_iterator_next(child_it), selected);
383 xccdf_item_iterator_free(child_it);
384 }
385 _xccdf_policy_modify_selected_final(policy, id, result);
386 }
387
388 /**
389 * Evaluate the policy check with given checking system
390 */
391 static int
392 xccdf_policy_evaluate_cb(struct xccdf_policy * policy, const char * sysname, const char * content, const char * href,
393 struct oscap_list * bindings, struct xccdf_check_import_iterator * check_import_it)
394 {
395 xccdf_test_result_type_t retval = XCCDF_RESULT_NOT_CHECKED;
396 struct oscap_iterator * cb_it = _xccdf_policy_get_engines_by_sysname(policy, sysname);
397 while (oscap_iterator_has_more(cb_it)) {
398 struct xccdf_policy_engine *engine = (struct xccdf_policy_engine *) oscap_iterator_next(cb_it);
399 retval = xccdf_policy_engine_eval(engine, policy, content, href, bindings, check_import_it);
400 if (retval != XCCDF_RESULT_NOT_CHECKED) break;
401 }
402 oscap_iterator_free(cb_it);
403
404 return retval;
405 }
406
407 /**
408 * Find all possible names for given check-content-ref/@href, considering also the check/@system.
409 * This is useful for multi-check="true" feature.
410 * @return list of OVAL definitions if the given href found, NULL otherwise.
411 */
412 static struct oscap_list *_xccdf_policy_get_oval_definitions_for_href(struct xccdf_policy *policy, const char *sysname, const char *href)
413 {
414 struct oscap_iterator *cb_it = _xccdf_policy_get_engines_by_sysname(policy, sysname);
415 struct oscap_list *result = NULL;
416 while (oscap_iterator_has_more(cb_it) && result == NULL) {
417 struct xccdf_policy_engine *engine = (struct xccdf_policy_engine *) oscap_iterator_next(cb_it);
418 if (engine == NULL)
419 break;
420 result = xccdf_policy_engine_query(engine, POLICY_ENGINE_QUERY_OVAL_DEFS_FOR_HREF, (void *) href);
421 }
422 oscap_iterator_free(cb_it);
423 return result;
424 }
425
426 int xccdf_policy_report_cb(struct xccdf_policy *policy, const char *sysname, void *rule)
427 {
428 int retval = 0;
429 struct oscap_iterator * cb_it = _xccdf_policy_get_callbacks_by_sysname(policy, sysname);
430 while (oscap_iterator_has_more(cb_it)) {
431 struct reporter *cb = (struct reporter *) oscap_iterator_next(cb_it);
432
433 retval = reporter_send_simple(cb, rule);
434
435 /* We still want to stop evaluation if user cancel it
436 * TODO: We should have a way to stop evaluation of current item
437 */
438 if (retval != 0) break;
439 }
440 oscap_iterator_free(cb_it);
441
442 return retval;
443 }
444
445 static struct oscap_list * xccdf_policy_check_get_value_bindings(struct xccdf_policy * policy, struct xccdf_check_export_iterator * check_it)
446 {
447 __attribute__nonnull__(check_it);
448
449 struct xccdf_check_export * check;
450 struct xccdf_value * value;
451 struct xccdf_benchmark * benchmark;
452 struct xccdf_value_binding * binding;
453 struct xccdf_policy_model * model;
454 struct xccdf_refine_value * r_value;
455 struct xccdf_setvalue * s_value;
456 struct oscap_list * list = oscap_list_new();
457
458 model = xccdf_policy_get_model(policy);
459 benchmark = xccdf_policy_model_get_benchmark(model);
460
461 while (xccdf_check_export_iterator_has_more(check_it)) {
462 check = xccdf_check_export_iterator_next(check_it);
463
464 value = (struct xccdf_value *) xccdf_benchmark_get_item(benchmark, xccdf_check_export_get_value(check));
465 if (value == NULL) {
466 oscap_seterr(OSCAP_EFAMILY_XCCDF, "Value \"%s\" does not exist in benchmark", xccdf_check_export_get_value(check));
467 oscap_list_free(list, free);
468 return NULL;
469 }
470 binding = xccdf_value_binding_new();
471
472 /* Apply related setvalue from policy profile */
473 s_value = xccdf_policy_get_setvalue(policy, xccdf_value_get_id(value));
474 if (s_value != NULL) binding->setvalue = oscap_strdup((char *) xccdf_setvalue_get_value(s_value));
475
476 /* Apply related refine value from policy profile */
477 const char * selector = NULL;
478 r_value = xccdf_policy_get_refine_value(policy, xccdf_value_get_id(value));
479 if (r_value != NULL) {
480 selector = xccdf_refine_value_get_selector(r_value);
481 /* This refine value changes the value content */
482 if ((int)xccdf_refine_value_get_oper(r_value) > 0) {
483 binding->operator = xccdf_refine_value_get_oper(r_value);
484 } else binding->operator = xccdf_value_get_oper(value);
485
486 } else binding->operator = xccdf_value_get_oper(value);
487
488 const struct xccdf_value_instance * val = xccdf_value_get_instance_by_selector(value, selector);
489 if (val == NULL) {
490 oscap_seterr(OSCAP_EFAMILY_XCCDF, "Attempt to get non-existent selector \"%s\" from variable \"%s\"", selector, xccdf_value_get_id(value));
491 oscap_list_free(list, free);
492 xccdf_value_binding_free(binding);
493 return NULL;
494 }
495 binding->value = oscap_strdup(xccdf_value_instance_get_value(val));
496 binding->name = oscap_strdup((char *) xccdf_check_export_get_name(check));
497 binding->type = xccdf_value_get_type(value);
498 oscap_list_add(list, binding);
499 }
500 xccdf_check_export_iterator_free(check_it);
501
502 return list;
503
504 }
505
506 int xccdf_policy_check_evaluate(struct xccdf_policy * policy, struct xccdf_check * check)
507 {
508 struct xccdf_check_iterator * child_it;
509 struct xccdf_check * child;
510 struct xccdf_check_content_ref_iterator * content_it;
511 struct xccdf_check_content_ref * content;
512 const char * content_name;
513 const char * system_name;
514 const char * href;
515 struct oscap_list * bindings;
516 int ret = 0;
517 int ret2 = 0;
518
519 /* At least one of check-content or check-content-ref must
520 * appear in each check element. */
521 if (xccdf_check_get_complex(check)) { /* we have complex subtree */
522 child_it = xccdf_check_get_children(check);
523 while (xccdf_check_iterator_has_more(child_it)) {
524 child = xccdf_check_iterator_next(child_it);
525 ret2 = xccdf_policy_check_evaluate(policy, child);
526 if (ret2 == -1) {
527 xccdf_check_iterator_free(child_it);
528 return -1;
529 }
530 if (ret == 0) ret = ret2;
531 else {
532 ret = (int) _resolve_operation((xccdf_test_result_type_t) ret, (xccdf_test_result_type_t) ret2, xccdf_check_get_oper(check));
533 }
534 }
535 xccdf_check_iterator_free(child_it);
536
537 } else { /* This is <check> element */
538 /* It depends on what operation we process - we do only Compliance Check */
539 content_it = xccdf_check_get_content_refs(check);
540 system_name = xccdf_check_get_system(check);
541 bindings = xccdf_policy_check_get_value_bindings(policy, xccdf_check_get_exports(check));
542 if (bindings == NULL) {
543 xccdf_check_content_ref_iterator_free(content_it);
544 return XCCDF_RESULT_UNKNOWN;
545 }
546 while (xccdf_check_content_ref_iterator_has_more(content_it)) {
547 content = xccdf_check_content_ref_iterator_next(content_it);
548 content_name = xccdf_check_content_ref_get_name(content);
549 href = xccdf_check_content_ref_get_href(content);
550
551 struct xccdf_check_import_iterator * check_import_it = xccdf_check_get_imports(check);
552 ret = xccdf_policy_evaluate_cb(policy, system_name, content_name, href, bindings, check_import_it);
553 // the evaluation has filled check imports at this point, we can simply free the iterator
554 xccdf_check_import_iterator_free(check_import_it);
555
556 // the content references are basically alternatives according to the specification
557 // we should go through them in the order they are defined and we are done as soon
558 // as we process one of them successfully
559 if ((xccdf_test_result_type_t) ret != XCCDF_RESULT_NOT_CHECKED) {
560 xccdf_check_inject_content_ref(check, content, NULL);
561 break;
562 }
563 }
564 xccdf_check_content_ref_iterator_free(content_it);
565 oscap_list_free(bindings, (oscap_destruct_func) xccdf_value_binding_free);
566 }
567 /* Negate only once */
568 ret = _resolve_negate(ret, check);
569 return ret;
570 }
571
572 static inline bool
573 _xccdf_policy_is_engine_registered(struct xccdf_policy *policy, char *sysname)
574 {
575 return oscap_list_contains(policy->model->engines, (void *) sysname, (oscap_cmp_func) xccdf_policy_engine_filter);
576 }
577
578 static struct xccdf_check *
579 _xccdf_policy_rule_get_applicable_check(struct xccdf_policy *policy, struct xccdf_item *rule)
580 {
581 // Citations inline come from NISTIR-7275r4.
582 struct xccdf_check *result = NULL;
583 {
584 // If an <xccdf:Rule> contains an <xccdf:complex-check>, then the benchmark consumer MUST process
585 // it and MUST ignore any <xccdf:check> elements that are also contained by the <xccdf:Rule>.
586 struct xccdf_check_iterator *check_it = xccdf_rule_get_complex_checks(rule);
587 if (xccdf_check_iterator_has_more(check_it))
588 result = xccdf_check_iterator_next(check_it);
589 xccdf_check_iterator_free(check_it);
590 }
591 if (result == NULL) {
592 // Check Processing Algorithm -- Check.Initialize
593 // Check Processing Algorithm -- Check.Selector
594 struct xccdf_refine_rule_internal *r_rule = xccdf_policy_get_refine_rule_by_item(policy, rule);
595 char *selector = (r_rule == NULL) ? NULL : xccdf_refine_rule_internal_get_selector(r_rule);
596 struct xccdf_check_iterator *candidate_it = xccdf_rule_get_checks_filtered(rule, selector);
597 if (selector != NULL && !xccdf_check_iterator_has_more(candidate_it)) {
598 xccdf_check_iterator_free(candidate_it);
599 // If the refined selector does not match, checks without selector shall be used.
600 candidate_it = xccdf_rule_get_checks_filtered(rule, NULL);
601 }
602
603 bool print_general_warning = false;
604 bool print_oval_warning = false;
605 char *warning_check_system = NULL;
606 // Check Processing Algorithm -- Check.System
607 while (xccdf_check_iterator_has_more(candidate_it)) {
608 struct xccdf_check *check = xccdf_check_iterator_next(candidate_it);
609 if (_xccdf_policy_is_engine_registered(policy, (char *) xccdf_check_get_system(check))) {
610 result = check;
611 char *preferred_engine = getenv("OSCAP_PREFERRED_ENGINE");
612 if (preferred_engine) {
613 if (strcmp("SCE", preferred_engine) && strcmp("OVAL", preferred_engine)) {
614 dW("Unknown value of OSCAP_PREFFERED_ENGINE: '%s'. It will be ignored.", preferred_engine);
615 }
616 if ((!strcmp("SCE", preferred_engine) && !strcmp("http://open-scap.org/page/SCE", check->system)) ||
617 (!strcmp("OVAL", preferred_engine) && !strcmp("http://oval.mitre.org/XMLSchema/oval-definitions-5", check->system))) {
618 break;
619 }
620 }
621 } else if (strcmp("http://oval.mitre.org/XMLSchema/oval-definitions-5", check->system) == 0) {
622 print_oval_warning = true;
623 } else if (strcmp("http://scap.nist.gov/schema/ocil/2", check->system) == 0) {
624 dI("This rule requires an OCIL check. OCIL checks are not supported by OpenSCAP.");
625 } else if (strcmp("http://open-scap.org/page/SCE", check->system) == 0) {
626 dI("This rule requires a SCE check but the SCE plugin was disabled.");
627 } else {
628 print_general_warning = true;
629 warning_check_system = check->system;
630 }
631 }
632
633 // Only print a warning if we didn't select a check but could've otherwise.
634 if (print_oval_warning) {
635 dW("Skipping rule that uses OVAL but is possibly malformed; "
636 "an incorrect content reference prevents this check from being evaluated.\n");
637 } else if (print_general_warning && result == NULL) {
638 dW("Skipping rule that requires an unregistered check system "
639 "or incorrect content reference to evaluate. "
640 "Please consider providing a valid SCAP/OVAL instead of %s\n",
641 warning_check_system);
642 }
643 xccdf_check_iterator_free(candidate_it);
644 }
645 // A tool processing the Benchmark for compliance checking must pick at most one check or
646 // complex-check element to process for each Rule.
647 return result;
648 }
649
650 bool
651 xccdf_policy_is_item_selected(struct xccdf_policy *policy, const char *id)
652 {
653 const bool *tmp = (const bool*) oscap_htable_get(policy->selected_final, id);
654 if (tmp == NULL) {
655 /* This shall really never happen. All valid IDs of any
656 * xccdf:Item shall be stored in the dictionary. However,
657 * we shall not to segfault. */
658 assert(false);
659 return false;
660 }
661 return *tmp;
662 }
663
664 int xccdf_policy_get_selected_rules_count(struct xccdf_policy *policy)
665 {
666 int ret = 0;
667 struct xccdf_policy_model *model = xccdf_policy_get_model(policy);
668 struct xccdf_benchmark *benchmark = xccdf_policy_model_get_benchmark(model);
669 struct oscap_htable_iterator *it = oscap_htable_iterator_new(policy->selected_final);
670
671 while (oscap_htable_iterator_has_more(it)) {
672 const char *key = NULL;
673 void *value = NULL;
674
675 oscap_htable_iterator_next_kv(it, &key, &value);
676
677 if (!value || !*(bool*)value)
678 continue;
679
680 if (!key)
681 continue;
682
683 struct xccdf_item *item = xccdf_benchmark_get_member(benchmark, XCCDF_ITEM, key);
684
685 if (!item)
686 continue;
687
688 if (xccdf_item_get_type(item) == XCCDF_RULE)
689 ++ret;
690 }
691
692 oscap_htable_iterator_free(it);
693 return ret;
694 }
695
696 static struct xccdf_rule_result * _xccdf_rule_result_new_from_rule(const struct xccdf_policy *policy, const struct xccdf_rule *rule,
697 struct xccdf_check *check,
698 xccdf_test_result_type_t eval_result,
699 const char *message)
700 {
701 const char* rule_id = xccdf_rule_get_id(rule);
702 struct xccdf_rule_result *rule_ritem = xccdf_rule_result_new();
703 struct xccdf_refine_rule_internal* r_rule = oscap_htable_get(policy->refine_rules_internal, rule_id);
704
705 /* --Set rule-- */
706 xccdf_rule_result_set_result(rule_ritem, eval_result);
707 xccdf_rule_result_set_idref(rule_ritem, xccdf_rule_get_id(rule));
708 xccdf_rule_result_set_weight(rule_ritem, xccdf_get_final_weight(rule, r_rule));
709 xccdf_rule_result_set_version(rule_ritem, xccdf_rule_get_version(rule));
710 xccdf_rule_result_set_severity(rule_ritem, xccdf_get_final_severity(rule, r_rule));
711 xccdf_rule_result_set_role(rule_ritem, xccdf_get_final_role(rule, r_rule));
712
713 xccdf_rule_result_set_time_current(rule_ritem);
714
715 /* --Ident-- */
716 struct xccdf_ident_iterator * ident_it = xccdf_rule_get_idents(rule);
717 while (xccdf_ident_iterator_has_more(ident_it)){
718 struct xccdf_ident * ident = xccdf_ident_iterator_next(ident_it);
719 xccdf_rule_result_add_ident(rule_ritem, xccdf_ident_clone(ident));
720 }
721 xccdf_ident_iterator_free(ident_it);
722 if (check != NULL)
723 xccdf_rule_result_add_check(rule_ritem, check);
724
725 if (message != NULL) {
726 struct xccdf_message *msg = xccdf_message_new();
727 xccdf_message_set_content(msg, message);
728 xccdf_message_set_severity(msg, XCCDF_MSG_INFO);
729 xccdf_rule_result_add_message(rule_ritem, msg);
730 }
731 return rule_ritem;
732 }
733
734 static bool _user_specified_rule_mode(struct xccdf_policy *policy) {
735 return oscap_htable_itemcount(policy->rules) > 0;
736 }
737
738 static int _xccdf_policy_report_rule_result(struct xccdf_policy *policy,
739 struct xccdf_result *result,
740 const struct xccdf_rule *rule,
741 struct xccdf_check *check,
742 int res,
743 const char *message)
744 {
745 struct xccdf_rule_result * rule_result = NULL;
746 int ret=0;
747
748 if (res == -1)
749 return res;
750
751 if (result != NULL) {
752 /* Add result to policy */
753 /* TODO: instance */
754 rule_result = _xccdf_rule_result_new_from_rule(policy, rule, check, res, message);
755 xccdf_result_add_rule_result(result, rule_result);
756 } else
757 xccdf_check_free(check);
758
759 /* If at least one --rule option has been provided by the user on the
760 * command line skip reporting for the other rules - only the selected
761 * rule(s) will be reported. */
762 if (_user_specified_rule_mode(policy) > 0) {
763 const char* rule_id = xccdf_rule_get_id(rule);
764 if (oscap_htable_get(policy->rules, rule_id) == NULL)
765 return ret;
766 }
767 ret = xccdf_policy_report_cb(policy, XCCDF_POLICY_OUTCB_END, (void *) rule_result);
768 return ret;
769 }
770
771 struct cpe_check_cb_usr
772 {
773 struct xccdf_policy_model* model;
774 struct cpe_dict_model* dict;
775 struct cpe_lang_model* lang_model;
776 };
777
778 /**
779 * Return true if path starts with '.' or './'
780 */
781 static bool is_path_relative_subdir(const char* path) {
782 if ( path[0] == '.' ) {
783 if ( path[1] == '/' || path[1] == '\0' ) return true;
784 }
785 return false;
786 }
787
788 static char *_cpe_get_oval_href(struct cpe_dict_model *dict, struct cpe_lang_model *lang_model, const char *oval_relative_href)
789 {
790 char *oval_href = NULL;
791 if (dict != NULL || lang_model != NULL) {
792 char* origin_file = NULL;
793 const char* origin_file_c = NULL;
794
795 if (dict != NULL) {
796 // the href path is relative to the CPE dictionary, we need to figure out
797 // a "prefixed path" to deal with the case where CPE dict is not in CWD
798 origin_file_c = cpe_dict_model_get_origin_file(dict);
799 } else {
800 // the href path is relative to the CPE2 dictionary, we need to figure out
801 // a "prefixed path" to deal with the case where CPE2 dict is not in CWD
802 origin_file_c = cpe_lang_model_get_origin_file(lang_model);
803 }
804
805 // we need to strdup because dirname potentially alters the string
806 origin_file = oscap_strdup(origin_file_c ? origin_file_c : "");
807 char *prefix_dirname = oscap_dirname(origin_file);
808 if (is_path_relative_subdir(prefix_dirname)) {
809 // The path is relative. Do not overide it.
810 // Chances are that ds_sds_session expects the very same href
811 oval_href = oscap_strdup(oval_relative_href);
812 } else {
813 oval_href = oscap_sprintf("%s/%s", prefix_dirname, oval_relative_href);
814 }
815 free(prefix_dirname);
816 free(origin_file);
817 }
818 return oval_href;
819 }
820
821 static bool _xccdf_policy_cpe_check_cb(const char* sys, const char* href, const char* name, void* usr)
822 {
823 // FIXME: Check that sys is OVAL
824
825 struct cpe_check_cb_usr* cb_usr = (struct cpe_check_cb_usr*)usr;
826
827 struct xccdf_policy_model* model = cb_usr->model;
828
829 char* prefixed_href = _cpe_get_oval_href(cb_usr->dict, cb_usr->lang_model, href);
830 struct oval_agent_session *session = cpe_session_lookup_oval_session(model->cpe, prefixed_href);
831 free(prefixed_href);
832 if (session == NULL) {
833 return false;
834 }
835
836 oval_agent_eval_definition(session, name);
837 oval_result_t result = OVAL_RESULT_NOT_EVALUATED;
838 if (oval_agent_get_definition_result(session, name, &result) != 0)
839 {
840 // error message should already be set in the function
841 }
842
843 return result == OVAL_RESULT_TRUE;
844 }
845
846 static bool _xccdf_policy_cpe_dict_cb(struct cpe_name* name, void* usr)
847 {
848 struct cpe_check_cb_usr* cb_usr = (struct cpe_check_cb_usr*)usr;
849
850 // We have to check all CPE1 dicts in the model.
851 // cb_usr->dict has nothing to do with this callback! Do NOT touch it here.
852
853 struct xccdf_policy_model* model = cb_usr->model;
854 struct xccdf_benchmark* benchmark = xccdf_policy_model_get_benchmark(model);
855
856 bool ret = false;
857
858 struct cpe_dict_model* embedded_dict = xccdf_benchmark_get_cpe_list(benchmark);
859 if (embedded_dict != NULL) {
860 ret = cpe_name_applicable_dict(name, embedded_dict, (cpe_check_fn) _xccdf_policy_cpe_check_cb, usr);
861 }
862 if (ret)
863 return true;
864
865 struct oscap_iterator* dicts = oscap_iterator_new(model->cpe->dicts);
866 while (!ret && oscap_iterator_has_more(dicts)) {
867 struct cpe_dict_model *dict = (struct cpe_dict_model*)oscap_iterator_next(dicts);
868 ret = cpe_name_applicable_dict(name, dict, (cpe_check_fn) _xccdf_policy_cpe_check_cb, usr);
869 }
870 oscap_iterator_free(dicts);
871 return ret;
872 }
873
874 static bool xccdf_policy_model_platforms_are_applicable_dict(struct xccdf_policy_model *model, struct cpe_dict_model *dict, struct oscap_string_iterator *platforms)
875 {
876 // at this point we know that the item has 1 or more platforms specified
877 bool ret = false;
878
879 while (oscap_string_iterator_has_more(platforms))
880 {
881 const char* platform = oscap_string_iterator_next(platforms);
882 // Platform could be a reference to CPE2 platform, skip the ones
883 // that aren't valid CPE names.
884 if (!cpe_name_check(platform))
885 continue;
886
887 struct cpe_name* name = cpe_name_new(platform);
888
889 struct cpe_check_cb_usr* usr = malloc(sizeof(struct cpe_check_cb_usr));
890 usr->model = model;
891 usr->dict = dict;
892 usr->lang_model = NULL;
893 const bool applicable = cpe_name_applicable_dict(name, dict, (cpe_check_fn) _xccdf_policy_cpe_check_cb, usr);
894 free(usr);
895
896 cpe_name_free(name);
897
898 if (applicable)
899 {
900 ret = true;
901
902 if (oscap_htable_get(model->cpe->applicable_platforms, platform) == NULL) {
903 oscap_htable_add(model->cpe->applicable_platforms, platform, 0);
904 }
905 }
906 }
907 oscap_string_iterator_reset(platforms);
908
909 return ret;
910 }
911
912 static bool xccdf_policy_model_platforms_are_applicable_lang_model(struct xccdf_policy_model *model, struct cpe_lang_model *lang_model, struct oscap_string_iterator *platforms)
913 {
914 // at this point we know that the item has 1 or more platforms specified
915 bool ret = false;
916
917 while (oscap_string_iterator_has_more(platforms))
918 {
919 const char* platform = oscap_string_iterator_next(platforms);
920 // Specification says that platform should begin with "#" if it is
921 // a reference to a CPE2 platform. However content exists where this
922 // is not strictly followed so we support both with and without "#"
923 // references.
924
925 const char* platform_shifted = platform;
926 if (strlen(platform_shifted) >= 1 && *platform_shifted == '#')
927 {
928 // skip the "#" character
929 platform_shifted++;
930 }
931
932 struct cpe_check_cb_usr* usr = malloc(sizeof(struct cpe_check_cb_usr));
933 usr->model = model;
934 usr->dict = NULL;
935 usr->lang_model = lang_model;
936 const bool applicable = cpe_platform_applicable_lang_model(platform_shifted, lang_model, (cpe_check_fn)_xccdf_policy_cpe_check_cb, (cpe_dict_fn)_xccdf_policy_cpe_dict_cb, usr);
937 free(usr);
938
939 if (applicable)
940 {
941 ret = true;
942
943 if (oscap_htable_get(model->cpe->applicable_platforms, platform) == NULL) {
944 oscap_htable_add(model->cpe->applicable_platforms, platform, 0);
945 }
946 }
947 }
948 oscap_string_iterator_reset(platforms);
949
950 return ret;
951 }
952
953 bool xccdf_policy_model_platforms_are_applicable(struct xccdf_policy_model *model, struct oscap_string_iterator *platforms)
954 {
955 // we have to check whether the item has any platforms at all, if it has none
956 // it should be applicable to all platforms
957 if (!oscap_string_iterator_has_more(platforms))
958 return true;
959
960 bool ret = false;
961 // We do not check whether the platform entries are valid platform refs
962 // or CPE names. We let the policy_model methods do that instead.
963 // Therefore we check all 4 (!) places where a platform may match.
964 // CPE2 takes precedence over CPE1 in this implementation. This is not
965 // dictated by the specification, it's an arbitrary choice.
966 struct xccdf_benchmark* benchmark = xccdf_policy_model_get_benchmark(model);
967 struct cpe_lang_model *embedded_lang_model = xccdf_benchmark_get_cpe_lang_model(benchmark);
968 if (embedded_lang_model != NULL) {
969 if (xccdf_policy_model_platforms_are_applicable_lang_model(model, embedded_lang_model, platforms))
970 ret = true;
971 }
972
973 struct oscap_iterator *lang_models = oscap_iterator_new(model->cpe->lang_models);
974 while (oscap_iterator_has_more(lang_models)) {
975 struct cpe_lang_model *lang_model = (struct cpe_lang_model *) oscap_iterator_next(lang_models);
976 if (xccdf_policy_model_platforms_are_applicable_lang_model(model, lang_model, platforms))
977 ret = true;
978 }
979 oscap_iterator_free(lang_models);
980
981 struct cpe_dict_model *embedded_dict = xccdf_benchmark_get_cpe_list(benchmark);
982 if (embedded_dict != NULL) {
983 if (xccdf_policy_model_platforms_are_applicable_dict(model, embedded_dict, platforms))
984 ret = true;
985 }
986
987 struct oscap_iterator *dicts = oscap_iterator_new(model->cpe->dicts);
988 while (oscap_iterator_has_more(dicts)) {
989 struct cpe_dict_model *dict = (struct cpe_dict_model *) oscap_iterator_next(dicts);
990 if (xccdf_policy_model_platforms_are_applicable_dict(model, dict, platforms))
991 ret = true;
992 }
993 oscap_iterator_free(dicts);
994
995 return ret;
996 }
997
998 bool xccdf_policy_model_item_is_applicable(struct xccdf_policy_model *model, struct xccdf_item *item)
999 {
1000 struct xccdf_item* parent = xccdf_item_get_parent(item);
1001 if (!parent || xccdf_policy_model_item_is_applicable(model, parent))
1002 {
1003 struct oscap_string_iterator* platforms = xccdf_item_get_platforms(item);
1004 bool ret = xccdf_policy_model_platforms_are_applicable(model, platforms);
1005 oscap_string_iterator_free(platforms);
1006
1007 return ret;
1008 }
1009 else
1010 {
1011 // parent is not applicable
1012 return false;
1013 }
1014 }
1015
1016 static bool _xccdf_policy_item_is_in_conflict(struct xccdf_policy *policy, const struct xccdf_item *item) {
1017 bool in_conflict = false;
1018 struct oscap_string_iterator *item_conflicts_it = xccdf_item_get_conflicts(item);
1019 while (oscap_string_iterator_has_more(item_conflicts_it)) {
1020 const char *conflict_id = oscap_string_iterator_next(item_conflicts_it);
1021 if (xccdf_policy_is_item_selected(policy, conflict_id)) {
1022 in_conflict = true;
1023 break;
1024 }
1025 }
1026 oscap_string_iterator_free(item_conflicts_it);
1027 return in_conflict;
1028 }
1029
1030 static bool _xccdf_policy_item_has_all_requirements(struct xccdf_policy *policy, const struct xccdf_item *item) {
1031 bool has_all_requirements = true;
1032 struct oscap_stringlist_iterator *item_requires_it = xccdf_item_get_requires(item);
1033 while (oscap_stringlist_iterator_has_more(item_requires_it)) {
1034 struct oscap_stringlist *requires_ids_list = oscap_stringlist_iterator_next(item_requires_it);
1035 struct oscap_string_iterator *rule_requires_ids_it = oscap_stringlist_get_strings(requires_ids_list);
1036 bool has_requirements = false;
1037 while (oscap_string_iterator_has_more(rule_requires_ids_it)) {
1038 const char *requires_id = oscap_string_iterator_next(rule_requires_ids_it);
1039 has_requirements = has_requirements || xccdf_policy_is_item_selected(policy, requires_id);
1040 }
1041 has_all_requirements = has_all_requirements && has_requirements;
1042 oscap_string_iterator_free(rule_requires_ids_it);
1043 }
1044 oscap_stringlist_iterator_free(item_requires_it);
1045 return has_all_requirements;
1046 }
1047
1048 static void _warn_about_required_rules(const struct xccdf_policy *policy, const struct xccdf_rule *rule)
1049 {
1050 struct oscap_stringlist_iterator *requires_it = xccdf_item_get_requires((struct xccdf_item *) rule);
1051 while (oscap_stringlist_iterator_has_more(requires_it)) {
1052 struct oscap_stringlist *requires_ids_list = oscap_stringlist_iterator_next(requires_it);
1053 struct oscap_string_iterator *rule_requires_ids_it = oscap_stringlist_get_strings(requires_ids_list);
1054 while (oscap_string_iterator_has_more(rule_requires_ids_it)) {
1055 const char *requires_id = oscap_string_iterator_next(rule_requires_ids_it);
1056 if (oscap_htable_get(policy->rules, requires_id) == NULL) {
1057 dW("Rule '%s' requires rule '%s', but it hasn't been specified using the '--rule' option.", xccdf_rule_get_id(rule), requires_id);
1058 }
1059 }
1060 oscap_string_iterator_free(rule_requires_ids_it);
1061 }
1062 oscap_stringlist_iterator_free(requires_it);
1063 }
1064
1065 static bool _matches_references(struct xccdf_policy *policy, const struct xccdf_rule *rule)
1066 {
1067 if (!policy->reference_filter.active) {
1068 return true;
1069 }
1070 bool matched = false;
1071 struct oscap_reference_iterator *references = xccdf_item_get_references((struct xccdf_item *)rule);
1072 while (oscap_reference_iterator_has_more(references) && !matched) {
1073 struct oscap_reference *ref = oscap_reference_iterator_next(references);
1074 const char *href = oscap_reference_get_href(ref);
1075 const char *title = oscap_reference_get_title(ref);
1076 if (!strcmp(href, policy->reference_filter.href) && !strcmp(title, policy->reference_filter.title)) {
1077 matched = true;
1078 }
1079 }
1080 oscap_reference_iterator_free(references);
1081 return matched;
1082 }
1083
1084 /**
1085 * Evaluate given check which is immediate child of the rule.
1086 * A possibe child checks will be evaluated by xccdf_policy_check_evaluate.
1087 * This duplication is needed to handle @multi-check correctly,
1088 * which is (in general) not predictable in any way.
1089 */
1090 static inline int
1091 _xccdf_policy_rule_evaluate(struct xccdf_policy * policy, const struct xccdf_rule *rule, struct xccdf_result *result, bool parent_selected)
1092 {
1093 const char* rule_id = xccdf_rule_get_id(rule);
1094 const bool is_selected = xccdf_policy_is_item_selected(policy, rule_id);
1095 const char *message = NULL;
1096 int report = 0;
1097
1098 /* If the rule is requested to be skipped by the user using --skip-rule on
1099 * the command line we will skip the evaluation of this rule. */
1100 if (oscap_htable_get(policy->skip_rules, rule_id) != NULL) {
1101 return _xccdf_policy_report_rule_result(policy, result, rule, NULL, XCCDF_RESULT_NOT_SELECTED, NULL);
1102 }
1103
1104 /* If user wants to evaluate only specific rules and the rule currently
1105 * being evaluated is not among these rules, do not evaluate it and mark it
1106 * as notselected. */
1107 if (_user_specified_rule_mode(policy) > 0) {
1108 if (oscap_htable_get(policy->rules, rule_id) == NULL) {
1109 return _xccdf_policy_report_rule_result(policy, result, rule, NULL, XCCDF_RESULT_NOT_SELECTED, NULL);
1110 }
1111 oscap_htable_add(policy->rules_found, rule_id, (void *)true);
1112 _xccdf_policy_modify_selected_final(policy, rule_id, true);
1113 _warn_about_required_rules(policy, rule);
1114
1115 } else {
1116 /* solve selects only when in --rule mode */
1117 if (!is_selected || !parent_selected)
1118 return _xccdf_policy_report_rule_result(policy, result, rule, NULL, XCCDF_RESULT_NOT_SELECTED, NULL);
1119
1120 // See section 7.2.3.3.2 (<xccdf:requires> and <xccdf:conflicts> Elements) of the XCCDF specification.
1121 if (_xccdf_policy_item_is_in_conflict(policy, XITEM(rule)) || !_xccdf_policy_item_has_all_requirements(policy, XITEM(rule))) {
1122 xccdf_policy_resolve_item(policy, XITEM(rule), false);
1123 return _xccdf_policy_report_rule_result(policy, result, rule, NULL, XCCDF_RESULT_NOT_SELECTED, NULL);
1124 }
1125 }
1126
1127 if (!_matches_references(policy, rule)) {
1128 return _xccdf_policy_report_rule_result(policy, result, rule, NULL, XCCDF_RESULT_NOT_SELECTED, NULL);
1129 }
1130
1131 /* Otherwise start reporting */
1132 report = xccdf_policy_report_cb(policy, XCCDF_POLICY_OUTCB_START, (void *) rule);
1133 if (report)
1134 return report;
1135
1136 struct xccdf_refine_rule_internal* r_rule = oscap_htable_get(policy->refine_rules_internal, rule_id);
1137 xccdf_role_t role = xccdf_get_final_role(rule, r_rule);
1138
1139 dI("Evaluating XCCDF rule '%s'.", rule_id);
1140
1141 if (role == XCCDF_ROLE_UNCHECKED)
1142 return _xccdf_policy_report_rule_result(policy, result, rule, NULL, XCCDF_RESULT_NOT_CHECKED, NULL);
1143
1144 const bool is_applicable = xccdf_policy_model_item_is_applicable(policy->model, (struct xccdf_item*)rule);
1145 if (!is_applicable) {
1146 dI("Rule '%s' is not applicable.", rule_id);
1147 return _xccdf_policy_report_rule_result(policy, result, rule, NULL, XCCDF_RESULT_NOT_APPLICABLE, NULL);
1148 }
1149
1150 const struct xccdf_check *orig_check = _xccdf_policy_rule_get_applicable_check(policy, (struct xccdf_item *) rule);
1151 if (orig_check == NULL)
1152 // No candidate or applicable check found.
1153 return _xccdf_policy_report_rule_result(policy, result, rule, NULL, XCCDF_RESULT_NOT_CHECKED, "No candidate or applicable check found.");
1154
1155 // we need to clone the check to avoid changing the original content
1156 struct xccdf_check *check = xccdf_check_clone(orig_check);
1157 if (xccdf_check_get_complex(check))
1158 return _xccdf_policy_report_rule_result(policy, result, rule, check, xccdf_policy_check_evaluate(policy, check), NULL);
1159
1160 // Now we are evaluating single simple xccdf:check within xccdf:rule.
1161 // Since the fact that a check will yield multi-check is not predictable in general
1162 // we will evaluate the check here. (A link between rule and its only check is
1163 // somewhat tigher that the one between checks in complex-check tree).
1164 //
1165 // Important: if touching this code, please revisit also xccdf_policy_check_evaluate.
1166 const char *system_name = xccdf_check_get_system(check);
1167 struct oscap_list *bindings = xccdf_policy_check_get_value_bindings(policy, xccdf_check_get_exports(check));
1168 if (bindings == NULL)
1169 return _xccdf_policy_report_rule_result(policy, result, rule, check, XCCDF_RESULT_UNKNOWN, "Value bindings not found.");
1170
1171
1172 struct xccdf_check_content_ref_iterator *content_it = xccdf_check_get_content_refs(check);
1173 struct xccdf_check_content_ref *content;
1174 const char *content_name;
1175 const char *href;
1176 int ret = XCCDF_RESULT_NOT_CHECKED; // initialized for the case of no check-content-refs present
1177 while (xccdf_check_content_ref_iterator_has_more(content_it)) {
1178 message = NULL;
1179 content = xccdf_check_content_ref_iterator_next(content_it);
1180 content_name = xccdf_check_content_ref_get_name(content);
1181 href = xccdf_check_content_ref_get_href(content);
1182
1183 if (content_name == NULL && xccdf_check_get_multicheck(check)) {
1184 // parent element is Rule, @multi-check is required
1185 struct oscap_list *oval_definition_list = _xccdf_policy_get_oval_definitions_for_href(policy, system_name, href);
1186 if (oval_definition_list != NULL) {
1187 // multi-check is supported by checking-engine
1188 struct oscap_iterator *oval_definition_iterator = oscap_iterator_new(oval_definition_list);
1189 if (!oscap_iterator_has_more(oval_definition_iterator)) {
1190 // Super special case when oval file contains no definitions
1191 // thus multi-check shall yield zero rule-results.
1192 report = _xccdf_policy_report_rule_result(policy, result, rule, check, XCCDF_RESULT_UNKNOWN, "No definitions found for @multi-check.");
1193 oscap_iterator_free(oval_definition_iterator);
1194 oscap_list_free(oval_definition_list, NULL);
1195 xccdf_check_content_ref_iterator_free(content_it);
1196 oscap_list_free(bindings, (oscap_destruct_func) xccdf_value_binding_free);
1197 return report;
1198 }
1199 while (oscap_iterator_has_more(oval_definition_iterator)) {
1200 struct oval_definition *oval_definition = oscap_iterator_next(oval_definition_iterator);
1201 if ((report = xccdf_policy_report_cb(policy, XCCDF_POLICY_OUTCB_MULTICHECK, (void *) oval_definition)) != 0) {
1202 break;
1203 }
1204 struct xccdf_check *cloned_check = xccdf_check_clone(check);
1205 xccdf_check_inject_content_ref(cloned_check, content, oval_definition_get_id(oval_definition));
1206 int inner_ret = xccdf_policy_check_evaluate(policy, cloned_check);
1207 if (inner_ret == -1) {
1208 xccdf_check_free(cloned_check);
1209 report = inner_ret;
1210 break;
1211 }
1212 if ((report = _xccdf_policy_report_rule_result(policy, result, rule, cloned_check, inner_ret, NULL)) != 0)
1213 break;
1214 if (oscap_iterator_has_more(oval_definition_iterator)) {
1215 if ((report = xccdf_policy_report_cb(policy, XCCDF_POLICY_OUTCB_START, (void *) rule)) != 0)
1216 break;
1217 }
1218 }
1219 oscap_iterator_free(oval_definition_iterator);
1220 oscap_list_free(oval_definition_list, NULL);
1221 xccdf_check_content_ref_iterator_free(content_it);
1222 oscap_list_free(bindings, (oscap_destruct_func) xccdf_value_binding_free);
1223 xccdf_check_free(check);
1224 return report;
1225 }
1226 else
1227 message = "Checking engine does not support multi-check; falling back to multi-check='false'";
1228 }
1229
1230 struct xccdf_check_import_iterator *check_import_it = xccdf_check_get_imports(check);
1231 ret = xccdf_policy_evaluate_cb(policy, system_name, content_name, href, bindings, check_import_it);
1232 // the evaluation has filled check imports at this point, we can simply free the iterator
1233 xccdf_check_import_iterator_free(check_import_it);
1234
1235 // the content references are basically alternatives according to the specification
1236 // we should go through them in the order they are defined and we are done as soon
1237 // as we process one of them successfully
1238 if ((xccdf_test_result_type_t) ret != XCCDF_RESULT_NOT_CHECKED) {
1239 xccdf_check_inject_content_ref(check, content, NULL);
1240 break;
1241 }
1242 }
1243 if ((xccdf_test_result_type_t) ret == XCCDF_RESULT_NOT_CHECKED)
1244 message = "None of the check-content-ref elements was resolvable.";
1245
1246 if (role == XCCDF_ROLE_UNSCORED)
1247 ret = XCCDF_RESULT_INFORMATIONAL;
1248
1249 xccdf_check_content_ref_iterator_free(content_it);
1250 oscap_list_free(bindings, (oscap_destruct_func) xccdf_value_binding_free);
1251 /* Negate only once */
1252 ret = _resolve_negate(ret, check);
1253 return _xccdf_policy_report_rule_result(policy, result, rule, check, ret, message);
1254 }
1255
1256 /**
1257 * Evaluate the XCCDF item. If it is group, start recursive cycle, otherwise get XCCDF check
1258 * and evaluate it.
1259 * Name collision with xccdf_item -> changed to xccdf_policy_item
1260 */
1261 static int xccdf_policy_item_evaluate(struct xccdf_policy * policy, struct xccdf_item * item, struct xccdf_result * result, bool parent_selected)
1262 {
1263 struct xccdf_item_iterator * child_it;
1264 struct xccdf_item * child;
1265 int ret = 0;
1266
1267 xccdf_type_t itype = xccdf_item_get_type(item);
1268
1269 switch (itype) {
1270 case XCCDF_RULE:{
1271 return _xccdf_policy_rule_evaluate(policy, (struct xccdf_rule *) item, result, parent_selected);
1272 } break;
1273
1274 case XCCDF_GROUP:{
1275 child_it = xccdf_group_get_content((const struct xccdf_group *)item);
1276 while (xccdf_item_iterator_has_more(child_it)) {
1277 child = xccdf_item_iterator_next(child_it);
1278 bool is_selected = xccdf_policy_is_item_selected(policy, xccdf_item_get_id(item));
1279 if (is_selected) {
1280 // See section 7.2.3.3.2 (<xccdf:requires> and <xccdf:conflicts> Elements) of the XCCDF specification.
1281 if (_xccdf_policy_item_is_in_conflict(policy, item) || !_xccdf_policy_item_has_all_requirements(policy, item)) {
1282 xccdf_policy_resolve_item(policy, item, false);
1283 is_selected = false;
1284 }
1285 }
1286 ret = xccdf_policy_item_evaluate(policy, child, result, parent_selected && is_selected);
1287 if (ret != 0)
1288 break;
1289 }
1290 xccdf_item_iterator_free(child_it);
1291 } break;
1292
1293 default:
1294 /* TODO: set warning bad argument and return ? */
1295 assert(false);
1296 return -1;
1297 break;
1298 }
1299
1300 return ret;
1301 }
1302
1303 struct oscap_file_entry {
1304 char* system_name;
1305 char* file;
1306 };
1307
1308 struct oscap_file_entry *oscap_file_entry_new(void)
1309 {
1310 struct oscap_file_entry *ret = calloc(1, sizeof(struct oscap_file_entry));
1311 return ret;
1312 }
1313
1314 struct oscap_file_entry *oscap_file_entry_dup(struct oscap_file_entry * file_entry)
1315 {
1316 struct oscap_file_entry *source = (struct oscap_file_entry *) file_entry;
1317
1318 struct oscap_file_entry *ret = oscap_file_entry_new();
1319 ret->system_name = oscap_strdup(source->system_name);
1320 ret->file = oscap_strdup(source->file);
1321
1322 return ret;
1323 }
1324
1325 void oscap_file_entry_free(struct oscap_file_entry * entry)
1326 {
1327 free(entry->system_name);
1328 free(entry->file);
1329 free(entry);
1330 }
1331
1332 const char* oscap_file_entry_get_system(struct oscap_file_entry* entry)
1333 {
1334 return entry->system_name;
1335 }
1336
1337 const char* oscap_file_entry_get_file(struct oscap_file_entry* entry)
1338 {
1339 return entry->file;
1340 }
1341
1342 const struct oscap_file_entry *oscap_file_entry_iterator_next(struct oscap_file_entry_iterator *it)
1343 {
1344 return oscap_iterator_next((struct oscap_iterator *)it);
1345 }
1346
1347 bool oscap_file_entry_iterator_has_more(struct oscap_file_entry_iterator *it)
1348 {
1349 return oscap_iterator_has_more((struct oscap_iterator *)it);
1350 }
1351
1352 void oscap_file_entry_iterator_free(struct oscap_file_entry_iterator *it)
1353 {
1354 oscap_iterator_free((struct oscap_iterator *)it);
1355 }
1356
1357 void oscap_file_entry_iterator_reset(struct oscap_file_entry_iterator *it)
1358 {
1359 oscap_iterator_reset((struct oscap_iterator *)it);
1360 }
1361
1362 struct oscap_file_entry_list* oscap_file_entry_list_new(void)
1363 {
1364 return (struct oscap_file_entry_list *) oscap_list_new();
1365 }
1366
1367 static void oscap_file_entry_list_item_destructor(void* item)
1368 {
1369 oscap_file_entry_free(item);
1370 }
1371
1372 void oscap_file_entry_list_free(struct oscap_file_entry_list* list)
1373 {
1374 oscap_list_free((struct oscap_list *) list, oscap_file_entry_list_item_destructor);
1375 }
1376
1377 struct oscap_file_entry_iterator* oscap_file_entry_list_get_files(struct oscap_file_entry_list* list)
1378 {
1379 return (struct oscap_file_entry_iterator *) oscap_iterator_new((struct oscap_list *) list);
1380 }
1381
1382 static bool xccdf_file_entry_cmp_func(void *e1, void *e2)
1383 {
1384 struct oscap_file_entry *entry1 = (struct oscap_file_entry *) e1;
1385 struct oscap_file_entry *entry2 = (struct oscap_file_entry *) e2;
1386
1387 if (oscap_strcmp(entry1->system_name, entry2->system_name))
1388 return false;
1389
1390 if (oscap_strcmp(entry1->file, entry2->file))
1391 return false;
1392
1393 return true;
1394 }
1395
1396 static struct oscap_file_entry_list * xccdf_check_get_systems_and_files(struct xccdf_check * check)
1397 {
1398 struct xccdf_check_iterator * child_it;
1399 struct xccdf_check * child;
1400 struct xccdf_check_content_ref_iterator * content_it;
1401 struct xccdf_check_content_ref * content;
1402 struct oscap_file_entry * file_entry;
1403 char * href;
1404 char * system_name;
1405 struct oscap_file_entry_list * files;
1406 struct oscap_file_entry_list * sub_files;
1407
1408 system_name = (char *) xccdf_check_get_system(check);
1409
1410 files = oscap_file_entry_list_new();
1411 if (xccdf_check_get_complex(check)) {
1412 child_it = xccdf_check_get_children(check);
1413 while (xccdf_check_iterator_has_more(child_it)) {
1414 child = xccdf_check_iterator_next(child_it);
1415 sub_files = xccdf_check_get_systems_and_files(child);
1416
1417 struct oscap_file_entry_iterator *file_it = oscap_file_entry_list_get_files(sub_files);
1418 while (oscap_file_entry_iterator_has_more(file_it)) {
1419 file_entry = (struct oscap_file_entry *) oscap_file_entry_iterator_next(file_it);
1420
1421 if (!oscap_list_contains((struct oscap_list *)files, (void *) file_entry, (oscap_cmp_func) xccdf_file_entry_cmp_func))
1422 oscap_list_add((struct oscap_list *)files, oscap_file_entry_dup(file_entry));
1423 }
1424 oscap_file_entry_iterator_free(file_it);
1425 oscap_file_entry_list_free(sub_files);
1426 }
1427 xccdf_check_iterator_free(child_it);
1428 } else {
1429 content_it = xccdf_check_get_content_refs(check);
1430 while (xccdf_check_content_ref_iterator_has_more(content_it)) {
1431 content = xccdf_check_content_ref_iterator_next(content_it);
1432 href = (char *) xccdf_check_content_ref_get_href(content);
1433
1434 file_entry = (struct oscap_file_entry *) oscap_file_entry_new();
1435 file_entry->system_name = oscap_strdup(system_name);
1436 file_entry->file = oscap_strdup(href);
1437
1438 if (!oscap_list_contains((struct oscap_list *) files, file_entry, (oscap_cmp_func) xccdf_file_entry_cmp_func))
1439 oscap_list_add((struct oscap_list *) files, (void *) file_entry);
1440 else
1441 oscap_file_entry_free((void *) file_entry);
1442 }
1443 xccdf_check_content_ref_iterator_free(content_it);
1444 }
1445
1446 return files;
1447 }
1448
1449 struct oscap_file_entry_list * xccdf_item_get_systems_and_files(struct xccdf_item * item)
1450 {
1451
1452 struct xccdf_item_iterator * child_it;
1453 struct xccdf_item * child;
1454 struct xccdf_check_iterator * check_it;
1455 struct xccdf_check * check;
1456 struct oscap_file_entry_list * files;
1457 struct oscap_file_entry_list * sub_files;
1458 struct oscap_file_entry * file_entry;
1459
1460 xccdf_type_t itype = xccdf_item_get_type(item);
1461 files = oscap_file_entry_list_new();
1462
1463 switch (itype) {
1464 case XCCDF_RULE:
1465 check_it = xccdf_rule_get_checks((struct xccdf_rule *) item);
1466 while(xccdf_check_iterator_has_more(check_it)) {
1467 check = xccdf_check_iterator_next(check_it);
1468 sub_files = xccdf_check_get_systems_and_files(check);
1469
1470 struct oscap_file_entry_iterator *file_it = oscap_file_entry_list_get_files(sub_files);
1471 while (oscap_file_entry_iterator_has_more(file_it)) {
1472 file_entry = (struct oscap_file_entry *) oscap_file_entry_iterator_next(file_it);
1473 if (!oscap_list_contains((struct oscap_list *) files, file_entry, (oscap_cmp_func) xccdf_file_entry_cmp_func))
1474 oscap_list_add((struct oscap_list *) files, oscap_file_entry_dup(file_entry));
1475 }
1476 oscap_file_entry_iterator_free(file_it);
1477 oscap_file_entry_list_free(sub_files);
1478 }
1479 xccdf_check_iterator_free(check_it);
1480 break;
1481
1482 case XCCDF_BENCHMARK:
1483 case XCCDF_GROUP:
1484 if (itype == XCCDF_GROUP) child_it = xccdf_group_get_content((const struct xccdf_group *)item);
1485 else child_it = xccdf_benchmark_get_content((const struct xccdf_benchmark *)item);
1486 while (xccdf_item_iterator_has_more(child_it)) {
1487 child = xccdf_item_iterator_next(child_it);
1488 sub_files = xccdf_item_get_systems_and_files(child);
1489
1490 struct oscap_file_entry_iterator *file_it = oscap_file_entry_list_get_files(sub_files);
1491 while (oscap_file_entry_iterator_has_more(file_it)) {
1492 file_entry = (struct oscap_file_entry *) oscap_file_entry_iterator_next(file_it);
1493
1494 if (!oscap_list_contains((struct oscap_list *) files, file_entry, (oscap_cmp_func) xccdf_file_entry_cmp_func))
1495 oscap_list_add((struct oscap_list *) files, oscap_file_entry_dup(file_entry));
1496 }
1497 oscap_file_entry_iterator_free(file_it);
1498 oscap_file_entry_list_free(sub_files);
1499 }
1500 xccdf_item_iterator_free(child_it);
1501 break;
1502
1503 default:
1504 oscap_file_entry_list_free(files);
1505 files = NULL;
1506 break;
1507 }
1508
1509 return files;
1510 }
1511
1512 static bool xccdf_cmp_func(const char *s1, const char *s2)
1513 {
1514 return !oscap_strcmp(s1, s2);
1515 }
1516
1517 static struct oscap_stringlist * xccdf_check_get_files(struct xccdf_check * check)
1518 {
1519 struct xccdf_check_iterator * child_it;
1520 struct xccdf_check * child;
1521 struct xccdf_check_content_ref_iterator * content_it;
1522 struct xccdf_check_content_ref * content;
1523 char * href;
1524 struct oscap_stringlist * names;
1525 struct oscap_stringlist * sub_names;
1526
1527 names = oscap_stringlist_new();
1528 if (xccdf_check_get_complex(check)) {
1529 child_it = xccdf_check_get_children(check);
1530 while (xccdf_check_iterator_has_more(child_it)) {
1531 child = xccdf_check_iterator_next(child_it);
1532 sub_names = xccdf_check_get_files(child);
1533
1534 struct oscap_string_iterator *name_it = oscap_stringlist_get_strings(sub_names);
1535 while (oscap_string_iterator_has_more(name_it)) {
1536 href = (char *) oscap_string_iterator_next(name_it);
1537 if (!oscap_list_contains((struct oscap_list *) names, (void *) href, (oscap_cmp_func) xccdf_cmp_func))
1538 oscap_stringlist_add_string(names, href);
1539 }
1540 oscap_string_iterator_free(name_it);
1541 oscap_stringlist_free(sub_names);
1542 }
1543 xccdf_check_iterator_free(child_it);
1544 } else {
1545 content_it = xccdf_check_get_content_refs(check);
1546 while (xccdf_check_content_ref_iterator_has_more(content_it)) {
1547 content = xccdf_check_content_ref_iterator_next(content_it);
1548 href = (char *) xccdf_check_content_ref_get_href(content);
1549 if (!oscap_list_contains((struct oscap_list *) names, href, (oscap_cmp_func) xccdf_cmp_func))
1550 oscap_stringlist_add_string(names, href);
1551 }
1552 xccdf_check_content_ref_iterator_free(content_it);
1553 }
1554
1555 return names;
1556 }
1557
1558 struct oscap_stringlist * xccdf_item_get_files(struct xccdf_item * item)
1559 {
1560
1561 struct xccdf_item_iterator * child_it;
1562 struct xccdf_item * child;
1563 struct xccdf_check_iterator * check_it;
1564 struct xccdf_check * check;
1565 struct oscap_stringlist * names;
1566 struct oscap_stringlist * sub_names;
1567 char * href;
1568
1569 xccdf_type_t itype = xccdf_item_get_type(item);
1570 names = oscap_stringlist_new();
1571
1572 switch (itype) {
1573 case XCCDF_RULE:
1574 check_it = xccdf_rule_get_checks((struct xccdf_rule *) item);
1575 while(xccdf_check_iterator_has_more(check_it)) {
1576 check = xccdf_check_iterator_next(check_it);
1577 sub_names = xccdf_check_get_files(check);
1578
1579 struct oscap_string_iterator *name_it = oscap_stringlist_get_strings(sub_names);
1580 while (oscap_string_iterator_has_more(name_it)) {
1581 href = (char *) oscap_string_iterator_next(name_it);
1582 if (!oscap_list_contains((struct oscap_list *)names, href, (oscap_cmp_func) xccdf_cmp_func))
1583 oscap_stringlist_add_string(names, href);
1584 }
1585 oscap_string_iterator_free(name_it);
1586 oscap_stringlist_free(sub_names);
1587 }
1588 xccdf_check_iterator_free(check_it);
1589 break;
1590
1591 case XCCDF_BENCHMARK:
1592 case XCCDF_GROUP:
1593 if (itype == XCCDF_GROUP) child_it = xccdf_group_get_content((const struct xccdf_group *)item);
1594 else child_it = xccdf_benchmark_get_content((const struct xccdf_benchmark *)item);
1595 while (xccdf_item_iterator_has_more(child_it)) {
1596 child = xccdf_item_iterator_next(child_it);
1597 sub_names = xccdf_item_get_files(child);
1598
1599 struct oscap_string_iterator *name_it = oscap_stringlist_get_strings(sub_names);
1600 while (oscap_string_iterator_has_more(name_it)) {
1601 href = (char *) oscap_string_iterator_next(name_it);
1602 if (!oscap_list_contains((struct oscap_list *)names, href, (oscap_cmp_func) xccdf_cmp_func))
1603 oscap_stringlist_add_string(names, href);
1604 }
1605 oscap_string_iterator_free(name_it);
1606 oscap_stringlist_free(sub_names);
1607 }
1608 xccdf_item_iterator_free(child_it);
1609 break;
1610
1611 default:
1612 oscap_stringlist_free(names);
1613 names = NULL;
1614 break;
1615 }
1616
1617 return names;
1618 }
1619
1620 /***************************************************************************/
1621 /* Public functions.
1622 */
1623
1624 bool xccdf_policy_model_set_tailoring(struct xccdf_policy_model *model, struct xccdf_tailoring *tailoring)
1625 {
1626 // Clear cached policies, because we (might) have to resolve differently
1627 // with Tailoring element in place.
1628 oscap_list_free(model->policies, (oscap_destruct_func) xccdf_policy_free);
1629 model->policies = oscap_list_new();
1630
1631 xccdf_tailoring_free(model->tailoring);
1632 model->tailoring = tailoring;
1633
1634 if (tailoring)
1635 xccdf_tailoring_resolve(tailoring, xccdf_policy_model_get_benchmark(model));
1636
1637 return true;
1638 }
1639
1640 struct xccdf_tailoring *xccdf_policy_model_get_tailoring(struct xccdf_policy_model *model)
1641 {
1642 return model->tailoring;
1643 }
1644
1645 bool xccdf_policy_model_add_cpe_dict_source(struct xccdf_policy_model * model, struct oscap_source *source)
1646 {
1647 __attribute__nonnull__(model);
1648 __attribute__nonnull__(source);
1649
1650 return cpe_session_add_cpe_dict_source(model->cpe, source);
1651 }
1652
1653 bool xccdf_policy_model_add_cpe_dict(struct xccdf_policy_model *model, const char * cpe_dict)
1654 {
1655 __attribute__nonnull__(model);
1656 __attribute__nonnull__(cpe_dict);
1657
1658 struct oscap_source *source = oscap_source_new_from_file(cpe_dict);
1659 bool ret = cpe_session_add_cpe_dict_source(model->cpe, source);
1660 oscap_source_free(source);
1661 return ret;
1662 }
1663
1664 bool xccdf_policy_model_add_cpe_lang_model_source(struct xccdf_policy_model *model, struct oscap_source *source)
1665 {
1666 __attribute__nonnull__(model);
1667 __attribute__nonnull__(source);
1668
1669 return cpe_session_add_cpe_lang_model_source(model->cpe, source);
1670 }
1671
1672 bool xccdf_policy_model_add_cpe_autodetect_source(struct xccdf_policy_model *model, struct oscap_source *source)
1673 {
1674 __attribute__nonnull__(model);
1675 __attribute__nonnull__(source);
1676
1677 return cpe_session_add_cpe_autodetect_source(model->cpe, source);
1678 }
1679
1680 struct oscap_htable_iterator *xccdf_policy_model_get_cpe_oval_sessions(struct xccdf_policy_model *model)
1681 {
1682 return oscap_htable_iterator_new(model->cpe->oval_sessions);
1683 }
1684
1685 struct cpe_session *xccdf_policy_model_get_cpe_session(struct xccdf_policy_model *model)
1686 {
1687 return model->cpe;
1688 }
1689
1690 /**
1691 * Get ID of XCCDF Profile that belongs to XCCDF Policy
1692 */
1693 const char * xccdf_policy_get_id(struct xccdf_policy * policy)
1694 {
1695 if (policy->profile != NULL)
1696 return xccdf_profile_get_id(xccdf_policy_get_profile(policy));
1697 else return NULL;
1698 }
1699
1700 bool
1701 xccdf_policy_model_register_engine_and_query_callback(struct xccdf_policy_model *model, char *sys, xccdf_policy_engine_eval_fn eval_fn, void *usr, xccdf_policy_engine_query_fn query_fn)
1702 {
1703 __attribute__nonnull__(model);
1704 struct xccdf_policy_engine *engine = xccdf_policy_engine_new(sys, eval_fn, usr, query_fn);
1705 return oscap_list_add(model->engines, engine);
1706 }
1707
1708 void xccdf_policy_model_unregister_engines(struct xccdf_policy_model *model, const char *sys)
1709 {
1710 __attribute__nonnull__(model);
1711 if (sys == NULL)
1712 oscap_list_free(model->engines, (oscap_destruct_func) free);
1713 else {
1714 struct oscap_list *rest = oscap_list_new();
1715 struct oscap_iterator *cb_it = oscap_iterator_new(model->engines);
1716 while (oscap_iterator_has_more(cb_it)) {
1717 struct xccdf_policy_engine *engine = oscap_iterator_next(cb_it);
1718 if (xccdf_policy_engine_filter(engine, sys))
1719 free(engine);
1720 else
1721 oscap_list_add(rest, engine);
1722 }
1723 oscap_iterator_free(cb_it);
1724 oscap_list_free0(model->engines);
1725 model->engines = rest;
1726 }
1727 }
1728
1729 bool xccdf_policy_model_register_start_callback(struct xccdf_policy_model * model, policy_reporter_start func, void * usr)
1730 {
1731
1732 __attribute__nonnull__(model);
1733 struct reporter *reporter = reporter_new(XCCDF_POLICY_OUTCB_START, func, usr);
1734 return oscap_list_add(model->callbacks, reporter);
1735 }
1736
1737 bool xccdf_policy_model_register_output_callback(struct xccdf_policy_model * model, policy_reporter_output func, void * usr)
1738 {
1739
1740 __attribute__nonnull__(model);
1741 struct reporter *reporter = reporter_new(XCCDF_POLICY_OUTCB_END, func, usr);
1742 return oscap_list_add(model->callbacks, reporter);
1743 }
1744
1745 bool xccdf_policy_model_register_multicheck_callback(struct xccdf_policy_model *model, policy_reporter_multicheck func, void *usr)
1746 {
1747 __attribute__nonnull__(model);
1748 struct reporter *reporter = reporter_new(XCCDF_POLICY_OUTCB_MULTICHECK, func, usr);
1749 return oscap_list_add(model->callbacks, reporter);
1750 }
1751
1752 struct xccdf_result * xccdf_policy_get_result_by_id(struct xccdf_policy * policy, const char * id) {
1753
1754 struct xccdf_result_iterator * result_it;
1755 struct xccdf_result * result;
1756
1757 result_it = xccdf_policy_get_results(policy);
1758 while (xccdf_result_iterator_has_more(result_it)){
1759 result = xccdf_result_iterator_next(result_it);
1760 if (!strcmp(xccdf_result_get_id(result), id) ){
1761 xccdf_result_iterator_free(result_it);
1762 return result;
1763 }
1764 }
1765 xccdf_result_iterator_free(result_it);
1766 return NULL;
1767 }
1768
1769 /***************************************************************************/
1770 /* Constructors of XCCDF POLICY structures xccdf_policy_*<structure>*_new()
1771 * More info in representive header file.
1772 * returns the type of <structure>
1773 */
1774
1775 /**
1776 * New XCCDF Policy model. Create new structure and fill the policies list with
1777 * policy entries that are inherited from XCCDF benchmark Profile elements. For each
1778 * profile element call xccdf_policy_new function.
1779 */
1780 struct xccdf_policy_model * xccdf_policy_model_new(struct xccdf_benchmark * benchmark) {
1781
1782 __attribute__nonnull__(benchmark);
1783
1784 struct xccdf_policy_model * model;
1785
1786 model = malloc(sizeof(struct xccdf_policy_model));
1787 if (model == NULL)
1788 return NULL;
1789 memset(model, 0, sizeof(struct xccdf_policy_model));
1790
1791 model->benchmark = benchmark;
1792 model->tailoring = NULL;
1793 model->policies = oscap_list_new();
1794 model->callbacks = oscap_list_new();
1795 model->engines = oscap_list_new();
1796
1797 model->cpe = cpe_session_new();
1798
1799 /* Resolve document */
1800 xccdf_benchmark_resolve(benchmark);
1801 return model;
1802 }
1803
1804 static inline bool
1805 _xccdf_policy_add_selector_internal(struct xccdf_policy *policy, struct xccdf_benchmark *benchmark, struct xccdf_select *sel, bool resolve)
1806 {
1807 /* This is the only way, how one can add selector to a policy. */
1808 bool result = oscap_list_add(policy->selects, sel);
1809
1810 struct xccdf_item *item = xccdf_benchmark_get_member(benchmark, XCCDF_ITEM, xccdf_select_get_item(sel));
1811 if (item != NULL) {
1812 /* If selector points to a single item. Update internal dictionary. */
1813 oscap_htable_detach(policy->selected_internal, xccdf_select_get_item(sel));
1814 result &= oscap_htable_add(policy->selected_internal, xccdf_select_get_item(sel), sel);
1815 if (resolve) {
1816 const struct xccdf_item *parent = xccdf_item_get_parent(item);
1817 /* If we are adding a selector to a top-level XCCDF Group (its parent is the Benchmark),
1818 we have to consider the parent selected even though it is not in
1819 the final selected hashmap. XCCDF Benchmark can't be unselected. */
1820 xccdf_policy_resolve_item(policy, item, (parent == NULL || xccdf_item_get_type(parent) == XCCDF_BENCHMARK) ?
1821 true : xccdf_policy_is_item_selected(policy, xccdf_item_get_id(parent)));
1822 }
1823 return result;
1824 }
1825
1826 /** If the selector doesn't point to an item it can still point to the cluster. */
1827 struct oscap_htable_iterator *hit = xccdf_benchmark_get_cluster_items(benchmark, xccdf_select_get_item(sel));
1828 if (hit == NULL) {
1829 dW("Selector ID(%s) does not exist in Benchmark and it will be ignored.", xccdf_select_get_item(sel));
1830 return false;
1831 }
1832 if (!oscap_htable_iterator_has_more(hit)) {
1833 oscap_htable_iterator_free(hit);
1834 return false;
1835 }
1836 while (oscap_htable_iterator_has_more(hit)) {
1837 const char *item_id = oscap_htable_iterator_next_key(hit);
1838 if (item_id == NULL) {
1839 assert(item_id != NULL);
1840 continue;
1841 }
1842 oscap_htable_detach(policy->selected_internal, item_id);
1843 result &= oscap_htable_add(policy->selected_internal, item_id, sel);
1844 }
1845 if (resolve) {
1846 oscap_htable_iterator_reset(hit);
1847 while (oscap_htable_iterator_has_more(hit)) {
1848 item = (struct xccdf_item *) oscap_htable_iterator_next_value(hit);
1849 if (item == NULL)
1850 continue;
1851 const struct xccdf_item *parent = xccdf_item_get_parent(item);
1852 xccdf_policy_resolve_item(policy, item, (parent == NULL || xccdf_item_get_type(parent) == XCCDF_BENCHMARK) ?
1853 true : xccdf_policy_is_item_selected(policy, xccdf_item_get_id(parent)));
1854 }
1855 }
1856 oscap_htable_iterator_free(hit);
1857 return result;
1858 }
1859
1860 bool
1861 xccdf_policy_add_select(struct xccdf_policy *policy, struct xccdf_select *sel)
1862 {
1863 struct xccdf_benchmark *benchmark = xccdf_policy_model_get_benchmark(xccdf_policy_get_model(policy));
1864 return _xccdf_policy_add_selector_internal(policy, benchmark, sel, true);
1865 }
1866
1867 static void _xccdf_policy_add_profile_selectors(struct xccdf_policy* policy, struct xccdf_benchmark *benchmark, struct xccdf_profile *profile) {
1868 if (!profile)
1869 return;
1870
1871 const char *parent_profile_id = xccdf_profile_get_extends(profile);
1872 struct xccdf_profile *parent_profile = NULL;
1873
1874 if (parent_profile_id != NULL) {
1875 if (strcmp(parent_profile_id, xccdf_profile_get_id(profile)) == 0) {
1876 // We are shadowing a profile, we need to get the original profile from
1877 // benchmark directly to avoid an endless loop.
1878 parent_profile = xccdf_benchmark_get_profile_by_id(benchmark, parent_profile_id);
1879 }
1880 else {
1881 struct xccdf_policy *parent_profile_policy = xccdf_policy_model_get_policy_by_id(xccdf_policy_get_model(policy), parent_profile_id);
1882 parent_profile = parent_profile_policy != NULL ? xccdf_policy_get_profile(parent_profile_policy) : NULL;
1883 }
1884 }
1885
1886 // Add selectors from parent profile if any, these selectors should be added
1887 // before selectors from the "real" policy profile are added so that they
1888 // can be overridden.
1889 if (parent_profile)
1890 _xccdf_policy_add_profile_selectors(policy, benchmark, parent_profile);
1891
1892 struct xccdf_select_iterator *sel_it = xccdf_profile_get_selects(profile);
1893 /* Iterate through selects in profile */
1894 while (xccdf_select_iterator_has_more(sel_it)) {
1895 struct xccdf_select *sel = xccdf_select_iterator_next(sel_it);
1896 if (sel == NULL) {
1897 assert(false);
1898 continue;
1899 }
1900
1901 struct xccdf_select *clone = xccdf_select_clone(sel);
1902 _xccdf_policy_add_selector_internal(policy, benchmark, clone, false);
1903 }
1904 xccdf_select_iterator_free(sel_it);
1905 }
1906
1907 /**
1908 * Constructor for structure XCCDF Policy. Create the structure and resolve all rules
1909 * from benchmark that are not present in selectors. This step is necessary because of
1910 * default values of groups / rules that can be added to Policy whether or not they have
1911 * their selector. Last step is filling the list of value bindings (see more in
1912 * xccdf_value_binding_proccess).
1913 */
1914 struct xccdf_policy * xccdf_policy_new(struct xccdf_policy_model * model, struct xccdf_profile * profile) {
1915
1916 struct xccdf_policy * policy;
1917 struct xccdf_benchmark * benchmark;
1918 struct xccdf_item_iterator * item_it;
1919 struct xccdf_item * item;
1920
1921 policy = malloc(sizeof(struct xccdf_policy));
1922 if (policy == NULL)
1923 return NULL;
1924 memset(policy, 0, sizeof(struct xccdf_policy));
1925
1926 policy->profile = profile;
1927 policy->rules = oscap_htable_new();
1928 policy->rules_found = oscap_htable_new();
1929 policy->skip_rules = oscap_htable_new();
1930 policy->selects = oscap_list_new();
1931 policy->values = oscap_list_new();
1932 policy->results = oscap_list_new();
1933
1934 policy->selected_internal = oscap_htable_new();
1935 policy->selected_final = oscap_htable_new();
1936 policy->refine_rules_internal = oscap_htable_new();
1937 policy->model = model;
1938
1939 policy->reference_filter.active = false;
1940 policy->reference_filter.href = NULL;
1941 policy->reference_filter.title = NULL;
1942
1943 benchmark = xccdf_policy_model_get_benchmark(model);
1944
1945 if (profile) {
1946 _xccdf_policy_add_profile_selectors(policy, benchmark, profile);
1947 xccdf_policy_add_profile_refine_rules(policy, benchmark, profile);
1948 }
1949
1950 /* Iterate through items in benchmark and resolve rules */
1951 item_it = xccdf_benchmark_get_content(benchmark);
1952 while (xccdf_item_iterator_has_more(item_it)) {
1953 item = xccdf_item_iterator_next(item_it);
1954 xccdf_policy_resolve_item(policy, item, true);
1955 }
1956 xccdf_item_iterator_free(item_it);
1957 policy->show_rule_details = model->show_rule_details;
1958 return policy;
1959 }
1960
1961 /**
1962 * Constructor for structure XCCDF Policy Value bindings
1963 */
1964 struct xccdf_value_binding * xccdf_value_binding_new() {
1965
1966 struct xccdf_value_binding * binding;
1967
1968 /* Initialization */
1969 binding = malloc(sizeof(struct xccdf_value_binding));
1970 if (binding == NULL)
1971 return NULL;
1972 memset(binding, 0, sizeof(struct xccdf_value_binding));
1973
1974 binding->name = NULL;
1975 binding->type = 0;
1976 binding->value = NULL;
1977 binding->setvalue = NULL;
1978 binding->operator = XCCDF_OPERATOR_EQUALS;
1979
1980 return binding;
1981 }
1982
1983 /***************************************************************************/
1984
1985 /**
1986 * If policy has the select specified by item_id return the select, NULL otherwise
1987 */
1988 struct xccdf_select * xccdf_policy_get_select_by_id(struct xccdf_policy * policy, const char *item_id)
1989 {
1990 __attribute__nonnull__(policy);
1991 return oscap_htable_get(policy->selected_internal, item_id);
1992 }
1993
1994
1995 struct xccdf_select_iterator * xccdf_policy_get_selected_rules(struct xccdf_policy * policy) {
1996
1997 return (struct xccdf_select_iterator *) oscap_iterator_new_filter( policy->selects,
1998 (oscap_filter_func) xccdf_policy_filter_selected,
1999 policy);
2000 }
2001
2002 /**
2003 * Get Policy from Policy model by it's id.
2004 */
2005 struct xccdf_policy * xccdf_policy_model_get_policy_by_id(struct xccdf_policy_model * policy_model, const char * id)
2006 {
2007 struct xccdf_policy *policy = xccdf_policy_model_get_existing_policy_by_id(policy_model, id);
2008 if (policy != NULL) {
2009 return policy;
2010 }
2011
2012 policy = xccdf_policy_model_create_policy_by_id(policy_model, id);
2013 if (policy != NULL)
2014 oscap_list_add(policy_model->policies, policy);
2015 return policy;
2016 }
2017
2018 /**
2019 * Resolve benchmark - apply all refine_rules to the benchmark items.
2020 * Beware ! Benchmark properties will be changed ! For discarding changes you have to load
2021 * benchmark from XML file again.
2022 */
2023 bool xccdf_policy_resolve(struct xccdf_policy * policy)
2024 {
2025
2026 struct xccdf_refine_rule_iterator * r_rule_it;
2027 struct xccdf_refine_rule * r_rule;
2028 struct xccdf_item * item;
2029
2030 struct xccdf_policy_model * policy_model = xccdf_policy_get_model(policy);
2031 struct xccdf_benchmark * benchmark = xccdf_policy_model_get_benchmark(policy_model);
2032 struct xccdf_profile * profile = xccdf_policy_get_profile(policy);
2033
2034 /* Proccess refine rules; Changing Rules and Groups */
2035 r_rule_it = xccdf_profile_get_refine_rules(profile);
2036 while (xccdf_refine_rule_iterator_has_more(r_rule_it)) {
2037 r_rule = xccdf_refine_rule_iterator_next(r_rule_it);
2038 item = xccdf_benchmark_get_item(benchmark, xccdf_refine_rule_get_item(r_rule));
2039 if (item != NULL) {
2040 /* Proccess refine rule appliement */
2041 /* In r_rule we have refine rule that match - no more then one !*/
2042 if (xccdf_item_get_type(item) == XCCDF_GROUP) {
2043 /* Perform check of weight attribute - ignore other attributes */
2044 if (xccdf_refine_rule_weight_defined(r_rule)) {
2045 oscap_seterr(OSCAP_EFAMILY_XCCDF, "'Weight' attribute not specified, only 'weight' attribute applies to groups items");
2046 xccdf_refine_rule_iterator_free(r_rule_it);
2047 return false;
2048 }
2049 else {
2050 /* Apply the rule changes */
2051 xccdf_group_set_weight((struct xccdf_group *) item, xccdf_refine_rule_get_weight(r_rule) );
2052 }
2053
2054 } else if (xccdf_item_get_type(item) == XCCDF_RULE) {
2055 /* Perform all changes in rule */
2056 if ((int)xccdf_refine_rule_get_role(r_rule) > 0)
2057 xccdf_rule_set_role((struct xccdf_rule *) item, xccdf_refine_rule_get_role(r_rule));
2058 if ((int)xccdf_refine_rule_get_severity(r_rule) != XCCDF_LEVEL_NOT_DEFINED)
2059 xccdf_rule_set_severity((struct xccdf_rule *) item, xccdf_refine_rule_get_severity(r_rule));
2060
2061 } else {}/* TODO oscap_err ? */;
2062
2063 } else {
2064 oscap_seterr(OSCAP_EFAMILY_XCCDF, "Refine rule item points to nonexisting XCCDF item");
2065 xccdf_refine_rule_iterator_free(r_rule_it);
2066 return false;
2067 }
2068 }
2069 xccdf_refine_rule_iterator_free(r_rule_it);
2070
2071 return true;
2072 }
2073
2074 static void xccdf_policy_add_final_setvalue(struct xccdf_policy *policy, struct xccdf_value *value, struct xccdf_result *result)
2075 {
2076 const char *idref = xccdf_value_get_id(value);
2077 const char *content = xccdf_policy_get_value_of_item(policy, xccdf_value_to_item(value));
2078
2079 struct xccdf_setvalue *setvalue = xccdf_setvalue_new();
2080 xccdf_setvalue_set_item(setvalue, idref);
2081 xccdf_setvalue_set_value(setvalue, content);
2082
2083 xccdf_result_add_setvalue(result, setvalue);
2084 }
2085
2086 static void xccdf_policy_add_final_setvalues(struct xccdf_policy *policy, struct xccdf_item *item, struct xccdf_result *result)
2087 {
2088 struct xccdf_item_iterator *child_it = NULL;
2089 struct xccdf_value_iterator *value_it = NULL;
2090
2091 xccdf_type_t itype = xccdf_item_get_type(item);
2092 switch (itype) {
2093 case XCCDF_GROUP:
2094 value_it = xccdf_group_get_values(xccdf_item_to_group(item));
2095 while (xccdf_value_iterator_has_more(value_it)) {
2096 struct xccdf_value *value = xccdf_value_iterator_next(value_it);
2097 xccdf_policy_add_final_setvalue(policy, value, result);
2098 }
2099 xccdf_value_iterator_free(value_it);
2100
2101 child_it = xccdf_group_get_content(xccdf_item_to_group(item));
2102 while (xccdf_item_iterator_has_more(child_it)) {
2103 struct xccdf_item *child = xccdf_item_iterator_next(child_it);
2104 xccdf_policy_add_final_setvalues(policy, child, result);
2105 }
2106 xccdf_item_iterator_free(child_it);
2107 break;
2108 case XCCDF_BENCHMARK:
2109 value_it = xccdf_benchmark_get_values(xccdf_item_to_benchmark(item));
2110 while (xccdf_value_iterator_has_more(value_it)) {
2111 struct xccdf_value *value = xccdf_value_iterator_next(value_it);
2112 xccdf_policy_add_final_setvalue(policy, value, result);
2113 }
2114 xccdf_value_iterator_free(value_it);
2115
2116 child_it = xccdf_benchmark_get_content(xccdf_item_to_benchmark(item));
2117 while (xccdf_item_iterator_has_more(child_it)) {
2118 struct xccdf_item *child = xccdf_item_iterator_next(child_it);
2119 xccdf_policy_add_final_setvalues(policy, child, result);
2120 }
2121 xccdf_item_iterator_free(child_it);
2122 break;
2123
2124 default:
2125 break;
2126 }
2127 }
2128
2129 /**
2130 * Evaluate XCCDF Policy
2131 * Iterate through Benchmark items and evaluate one by one by calling
2132 * callback for checking system that is defined by particular rules
2133 * Callbacks for checking systems have to be defined before calling this function, otherwise
2134 * rules would not be evaluated and process ends with error.
2135 */
2136 struct xccdf_result * xccdf_policy_evaluate(struct xccdf_policy * policy)
2137 {
2138 struct xccdf_benchmark * benchmark;
2139 int ret = -1;
2140 const char * doc_version = NULL;
2141
2142 __attribute__nonnull__(policy);
2143
2144 /* Add result to policy */
2145 struct xccdf_result * result = xccdf_result_new();
2146
2147 xccdf_result_set_start_time_current(result);
2148 xccdf_result_set_test_system(result, "cpe:/a:redhat:openscap:" OPENSCAP_VERSION);
2149
2150 /** Set ID of TestResult */
2151 char *id = NULL;
2152 if ((xccdf_policy_get_profile(policy) != NULL) && (xccdf_profile_get_id(xccdf_policy_get_profile(policy)) != NULL)) {
2153 id = oscap_strdup(xccdf_profile_get_id(xccdf_policy_get_profile(policy)));
2154 xccdf_result_set_profile(result, id);
2155 } else {
2156 id = oscap_strdup("default-profile");
2157 }
2158
2159 dI("Evaluating a XCCDF policy with selected '%s' profile.", id);
2160
2161 /* Get all constant information */
2162 benchmark = xccdf_policy_model_get_benchmark(xccdf_policy_get_model(policy));
2163 const struct xccdf_version_info* version_info = xccdf_benchmark_get_schema_version(benchmark);
2164 doc_version = xccdf_version_info_get_version(version_info);
2165
2166 const char *rid_prefix;
2167 #ifdef __USE_GNU
2168 if (strverscmp("1.2", doc_version) >= 0)
2169 #else
2170 if (strcmp("1.2", doc_version) >= 0)
2171 #endif
2172 {
2173 // we have to enforce a certain type of ids for XCCDF 1.2+
2174 rid_prefix = "xccdf_org.open-scap_testresult_";
2175 } else {
2176 // previous behaviour for backwards compatibility
2177 rid_prefix = "OSCAP-Test-";
2178 }
2179 const size_t rid_len = strlen(rid_prefix) + strlen(id) + 1; // + 1 for terminating '\0'
2180 char *rid = malloc(rid_len);
2181 snprintf(rid, rid_len, "%s%s", rid_prefix, id);
2182 xccdf_result_set_id(result, rid);
2183 free(rid);
2184
2185 free(id);
2186
2187 /** We need to process document top-down order.
2188 * See conflicts/requires and Item Processing Algorithm */
2189 struct xccdf_item_iterator *item_it = xccdf_benchmark_get_content(benchmark);
2190 while (xccdf_item_iterator_has_more(item_it)) {
2191 struct xccdf_item *item = xccdf_item_iterator_next(item_it);
2192 ret = xccdf_policy_item_evaluate(policy, item, result, true);
2193 if (ret == -1) {
2194 xccdf_item_iterator_free(item_it);
2195 xccdf_result_free(result);
2196 return NULL;
2197 }
2198 if (ret != 0)
2199 break;
2200 }
2201 xccdf_item_iterator_free(item_it);
2202
2203 struct oscap_htable_iterator *rit = oscap_htable_iterator_new(policy->rules);
2204 while (oscap_htable_iterator_has_more(rit)) {
2205 const char *rule_id = oscap_htable_iterator_next_key(rit);
2206 if (oscap_htable_get(policy->rules_found, rule_id) == NULL) {
2207 oscap_seterr(OSCAP_EFAMILY_XCCDF,
2208 "Rule '%s' not found in selected profile.", rule_id);
2209 oscap_htable_iterator_free(rit);
2210 return NULL;
2211 }
2212 }
2213 oscap_htable_iterator_free(rit);
2214
2215 xccdf_policy_add_final_setvalues(policy, xccdf_benchmark_to_item(benchmark), result);
2216
2217 struct oscap_htable_iterator *it = oscap_htable_iterator_new(policy->model->cpe->applicable_platforms);
2218 while (oscap_htable_iterator_has_more(it)) {
2219 const char *key = oscap_htable_iterator_next_key(it);
2220 xccdf_result_add_applicable_platform(result, key);
2221 }
2222 oscap_htable_iterator_free(it);
2223
2224 xccdf_policy_add_result(policy, result);
2225
2226 xccdf_result_set_end_time_current(result);
2227
2228 return result;
2229 }
2230
2231 struct xccdf_score * xccdf_policy_get_score(struct xccdf_policy * policy, struct xccdf_result * test_result, const char * scsystem)
2232 {
2233 struct xccdf_benchmark * benchmark = xccdf_policy_model_get_benchmark(xccdf_policy_get_model(policy));
2234 return xccdf_result_calculate_score(test_result, (struct xccdf_item *) benchmark, scsystem);
2235 }
2236
2237 int xccdf_policy_recalculate_score(struct xccdf_policy * policy, struct xccdf_result * test_result)
2238 {
2239 struct xccdf_benchmark * benchmark = xccdf_policy_model_get_benchmark(xccdf_policy_get_model(policy));
2240 return xccdf_result_recalculate_scores(test_result, (struct xccdf_item *) benchmark);
2241 }
2242
2243 const char *xccdf_policy_get_value_of_item(struct xccdf_policy * policy, struct xccdf_item * item)
2244 {
2245 struct xccdf_profile * profile = xccdf_policy_get_profile(policy);
2246 const char *selector = NULL;
2247
2248 if (profile != NULL) {
2249 /* Get set_value for this item */
2250 struct xccdf_setvalue *s_value = NULL;
2251 struct xccdf_setvalue *last_s_value = NULL;
2252 struct xccdf_setvalue_iterator *s_value_it = xccdf_profile_get_setvalues(profile);
2253 while (xccdf_setvalue_iterator_has_more(s_value_it)) {
2254 s_value = xccdf_setvalue_iterator_next(s_value_it);
2255 if (strcmp(xccdf_setvalue_get_item(s_value), xccdf_value_get_id((struct xccdf_value *) item)) == 0)
2256 last_s_value = s_value;
2257 }
2258 xccdf_setvalue_iterator_free(s_value_it);
2259 if (last_s_value != NULL)
2260 return xccdf_setvalue_get_value(last_s_value);
2261
2262 /* We don't have set-value in profile, look for refine-value */
2263 struct xccdf_refine_value_iterator *r_value_it = xccdf_profile_get_refine_values(profile);
2264 while (xccdf_refine_value_iterator_has_more(r_value_it)) {
2265 struct xccdf_refine_value *r_value = xccdf_refine_value_iterator_next(r_value_it);
2266 if (!strcmp(xccdf_refine_value_get_item(r_value), xccdf_value_get_id((struct xccdf_value *) item))) {
2267 selector = xccdf_refine_value_get_selector(r_value);
2268 break;
2269 }
2270 }
2271 xccdf_refine_value_iterator_free(r_value_it);
2272 }
2273
2274 struct xccdf_value_instance *instance = xccdf_value_get_instance_by_selector((struct xccdf_value *) item, selector);
2275 if (instance == NULL) {
2276 oscap_seterr(OSCAP_EFAMILY_XCCDF, "Invalid selector '%s' for xccdf:value/@id='%s'. Using null value instead.",
2277 selector, xccdf_value_get_id((struct xccdf_value *) item));
2278 return NULL;
2279 } else {
2280 return xccdf_value_instance_get_value(instance);
2281 }
2282 }
2283
2284 struct oscap_file_entry_list * xccdf_policy_model_get_systems_and_files(struct xccdf_policy_model * policy_model)
2285 {
2286 return xccdf_item_get_systems_and_files((struct xccdf_item *) xccdf_policy_model_get_benchmark(policy_model));
2287 }
2288
2289 struct oscap_stringlist * xccdf_policy_model_get_files(struct xccdf_policy_model * policy_model)
2290 {
2291 return xccdf_item_get_files((struct xccdf_item *) xccdf_policy_model_get_benchmark(policy_model));
2292 }
2293
2294 struct xccdf_benchmark *xccdf_policy_get_benchmark(const struct xccdf_policy *policy)
2295 {
2296 if (policy == NULL)
2297 return NULL;
2298 const struct xccdf_policy_model *model = xccdf_policy_get_model(policy);
2299 if (model == NULL)
2300 return NULL;
2301 return xccdf_policy_model_get_benchmark(model);
2302 }
2303
2304 void xccdf_policy_model_free(struct xccdf_policy_model * model) {
2305
2306 oscap_list_free(model->policies, (oscap_destruct_func) xccdf_policy_free);
2307 xccdf_policy_model_unregister_engines(model, NULL);
2308 oscap_list_free(model->callbacks, (oscap_destruct_func) free);
2309 xccdf_tailoring_free(model->tailoring);
2310 xccdf_benchmark_free(model->benchmark);
2311 cpe_session_free(model->cpe);
2312 free(model);
2313 }
2314
2315 static const char *_find_reference_uri_by_key(struct xccdf_benchmark *benchmark, const char *key)
2316 {
2317 const char *uri = NULL;
2318 struct oscap_reference_iterator *benchmark_references = xccdf_item_get_references((struct xccdf_item *)benchmark);
2319 while (oscap_reference_iterator_has_more(benchmark_references)) {
2320 struct oscap_reference *ref = oscap_reference_iterator_next(benchmark_references);
2321 const char *title = oscap_reference_get_title(ref);
2322 if (!strcmp(key, title)) {
2323 uri = oscap_reference_get_href(ref);
2324 break;
2325 }
2326 }
2327 oscap_reference_iterator_free(benchmark_references);
2328 return uri;
2329 }
2330
2331 void xccdf_policy_set_reference_filter(struct xccdf_policy *policy, const char *reference_parameter)
2332 {
2333 if (!reference_parameter) {
2334 return;
2335 }
2336 char *reference_parameter_dup = strdup(reference_parameter);
2337 char **split = oscap_split(reference_parameter_dup, ":");
2338 struct xccdf_benchmark *benchmark = policy->model->benchmark;
2339 char *key = split[0];
2340 const char *uri = _find_reference_uri_by_key(benchmark, key);
2341 if (!uri) {
2342 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Reference type '%s' isn't available in this benchmark", key);
2343 goto cleanup;
2344 }
2345 char *title = split[1];
2346 if (!title) {
2347 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Reference identifier hasn't been provided");
2348 goto cleanup;
2349 }
2350 policy->reference_filter.active = true;
2351 policy->reference_filter.href = strdup(uri);
2352 policy->reference_filter.title = strdup(title);
2353 cleanup:
2354 free(split);
2355 free(reference_parameter_dup);
2356 }
2357
2358
2359 void xccdf_policy_free(struct xccdf_policy * policy) {
2360
2361 /* A policy which is set to use default profile has its profile member set to NULL,
2362 * check it so we don't try to get the ID from a NULL profile.
2363 * */
2364 if (policy->profile && (
2365 (xccdf_profile_get_id(policy->profile) == NULL) ||
2366 (strcmp(xccdf_profile_get_id(policy->profile), "(all)") == 0)))
2367 /* If ID of policy's profile is NULL or "(all)" then this
2368 * profile is created by Policy layer and need
2369 * to be freed
2370 */
2371 xccdf_profile_free((struct xccdf_item *) policy->profile);
2372
2373 oscap_htable_free0(policy->rules);
2374 oscap_htable_free0(policy->skip_rules);
2375 oscap_htable_free0(policy->rules_found);
2376 oscap_list_free(policy->selects, (oscap_destruct_func) xccdf_select_free);
2377 oscap_list_free(policy->values, (oscap_destruct_func) xccdf_value_binding_free);
2378 oscap_list_free(policy->results, (oscap_destruct_func) xccdf_result_free);
2379 oscap_htable_free0(policy->selected_internal);
2380 oscap_htable_free0(policy->selected_final);
2381 oscap_htable_free(policy->refine_rules_internal, (oscap_destruct_func) xccdf_refine_rule_internal_free);
2382 free(policy->reference_filter.href);
2383 free(policy->reference_filter.title);
2384 free(policy);
2385 }
2386
2387 void xccdf_value_binding_free(struct xccdf_value_binding * binding) {
2388
2389 free(binding->name);
2390 free(binding->value);
2391 free(binding->setvalue);
2392 free(binding);
2393 }
2394
2395 void xccdf_policy_model_set_show_rule_details(struct xccdf_policy_model *policy_model, bool show_rule_details)
2396 {
2397 policy_model->show_rule_details = show_rule_details;
2398 }