Debian Codegraph @

src/XCCDF/resolve.c

raw symbols SCIP
1 /*
2 * Copyright 2010 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Lukas Kuklinek <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <math.h>
28
29 #include "item.h"
30 #include "helpers.h"
31 #include "common/tsort.h"
32 #include "oscap_helpers.h"
33
34 typedef void (*xccdf_textresolve_func)(void *child, void *parent);
35
36 static void xccdf_resolve_item(struct xccdf_item *item, struct xccdf_tailoring *tailoring);
37 static void xccdf_resolve_cleanup(struct xccdf_item *item);
38
39 static struct oscap_list *xccdf_benchmark_resolve_dependencies(void *itemptr, void *userdata)
40 {
41 struct xccdf_item *item = XITEM(itemptr);
42 struct oscap_list *ret = oscap_list_new();
43 struct xccdf_value_iterator *val_it = NULL;
44
45 const char *extends = xccdf_item_get_extends(item);
46 if (extends) oscap_list_add(ret, xccdf_benchmark_get_member(xccdf_item_get_benchmark(item), xccdf_item_get_type(item), extends));
47
48 switch (xccdf_item_get_type(item)) {
49 case XCCDF_BENCHMARK: {
50 OSCAP_FOR(xccdf_profile, profile, xccdf_benchmark_get_profiles(xccdf_item_to_benchmark(item)))
51 oscap_list_add(ret, profile);
52 val_it = xccdf_benchmark_get_values(xccdf_item_to_benchmark(item));
53 break;
54 }
55 case XCCDF_GROUP:
56 val_it = xccdf_group_get_values(xccdf_item_to_group(item));
57 default: break; /* no-op */
58 }
59
60 OSCAP_FOR(xccdf_item, child, xccdf_item_get_content(item))
61 oscap_list_add(ret, child);
62
63 OSCAP_FOR(xccdf_value, val, val_it)
64 oscap_list_add(ret, val);
65
66
67 return ret;
68 }
69
70 bool xccdf_benchmark_resolve(struct xccdf_benchmark *benchmark)
71 {
72 struct oscap_list *resolve_order = NULL, *root_nodes = oscap_list_new();
73 oscap_list_add(root_nodes, benchmark);
74 bool ret = false;
75
76 if (oscap_tsort(root_nodes, &resolve_order, xccdf_benchmark_resolve_dependencies, NULL, NULL)) {
77 OSCAP_FOR(xccdf_item, item, oscap_iterator_new(resolve_order))
78 xccdf_resolve_item(item, NULL);
79 ret = true;
80 }
81
82 oscap_list_free(root_nodes, NULL);
83 oscap_list_free(resolve_order, NULL);
84
85 xccdf_resolve_cleanup(XITEM(benchmark));
86
87 return ret;
88 }
89
90 // prototypes
91 static void xccdf_resolve_textlist(struct oscap_list *child_list, struct oscap_list *parent_list, xccdf_textresolve_func more);
92 static void xccdf_resolve_appendlist(struct oscap_list **child_list, struct oscap_list *parent_list, oscap_cmp_func item_compare, oscap_clone_func cloner, bool prepend);
93 static void xccdf_resolve_value_instance(struct xccdf_value_instance *child, struct xccdf_value_instance *parent);
94 static void xccdf_resolve_profile(struct xccdf_item *child, struct xccdf_item *parent);
95 static void xccdf_resolve_group(struct xccdf_item *child, struct xccdf_item *parent);
96 static void xccdf_resolve_rule(struct xccdf_item *child, struct xccdf_item *parent);
97 static void xccdf_resolve_value(struct xccdf_item *child, struct xccdf_item *parent);
98
99 static void xccdf_resolve_warning(void *w1, void *w2) {
100 if (xccdf_warning_get_category(w1) == 0)
101 xccdf_warning_set_category(w1, xccdf_warning_get_category(w2));
102 }
103
104 static struct xccdf_profile *_xccdf_tailoring_profile_get_real_parent(struct xccdf_tailoring *tailoring, struct xccdf_profile *profile)
105 {
106 const char *extends = xccdf_profile_get_extends(profile);
107 struct xccdf_profile *parent_from_tailoring = xccdf_tailoring_get_profile_by_id(tailoring, extends);
108 if (parent_from_tailoring != NULL && parent_from_tailoring != profile) {
109 return parent_from_tailoring;
110 }
111 else {
112 return XPROFILE(xccdf_benchmark_get_member(xccdf_profile_get_benchmark(profile), XCCDF_PROFILE, extends));
113 }
114 }
115
116 /*
117 This macro will look for flags that both undefined in the item AND defined
118 in the ancestor/base item. If such a case occurs we define the flag in our
119 item to the same value as in the ancestor.
120
121 NOTE: Profile is an Item in current openscap implementation. That is not
122 how the specification describes it and there are several problems with that.
123
124 Some flags that can be defined for a Profile via the internals can't be
125 exported without the result being invalid! (e.g. Profile/@hidden)
126 */
127 #define XCCDF_RESOLVE_FLAG(ITEM,PARENT,FLAGNAME) do { \
128 if (!ITEM->item.defined_flags.FLAGNAME) { \
129 if (PARENT->item.defined_flags.FLAGNAME) { \
130 ITEM->item.flags.FLAGNAME = PARENT->item.flags.FLAGNAME; \
131 ITEM->item.defined_flags.FLAGNAME = true; \
132 } \
133 } \
134 } while (false)
135
136 static void xccdf_resolve_item(struct xccdf_item *item, struct xccdf_tailoring *tailoring)
137 {
138 assert(item != NULL);
139
140 if (xccdf_item_get_type(item) == XCCDF_BENCHMARK) {
141 xccdf_benchmark_set_resolved(xccdf_item_to_benchmark(item), true);
142 return; // benchmark has no extends
143 }
144
145 assert(!xccdf_item_get_extends(item) || xccdf_item_get_type(item) & (XCCDF_PROFILE | XCCDF_ITEM));
146 struct xccdf_item *parent = NULL;
147 if (xccdf_item_get_type(item) == XCCDF_PROFILE && tailoring != NULL) {
148 parent = XITEM(_xccdf_tailoring_profile_get_real_parent(tailoring, XPROFILE(item)));
149 }
150 else {
151 parent = xccdf_benchmark_get_member(xccdf_item_get_benchmark(item), xccdf_item_get_type(item), xccdf_item_get_extends(item));
152 }
153 if (parent == NULL) return;
154 if (xccdf_item_get_type(item) != xccdf_item_get_type(parent)) return;
155 if (xccdf_item_get_type(item) == XCCDF_GROUP && xccdf_version_cmp(xccdf_item_get_schema_version(item), "1.2") >= 0)
156 return; // Group/@extends= has been obsoleted in XCCDF 1.2
157
158 // resolve flags
159 XCCDF_RESOLVE_FLAG(item, parent, selected);
160 XCCDF_RESOLVE_FLAG(item, parent, hidden);
161 XCCDF_RESOLVE_FLAG(item, parent, prohibit_changes);
162 XCCDF_RESOLVE_FLAG(item, parent, interactive);
163 XCCDF_RESOLVE_FLAG(item, parent, multiple);
164
165 // resolve weight & version
166 if (!item->item.defined_flags.weight)
167 xccdf_item_set_weight(item, xccdf_item_get_weight(parent));
168
169 if (xccdf_item_get_version(item) == NULL) {
170 xccdf_item_set_version(item, xccdf_item_get_version(parent));
171 xccdf_item_set_version_update(item, xccdf_item_get_version_update(parent));
172 xccdf_item_set_version_time(item, xccdf_item_get_version_time(parent));
173 }
174
175 // resolve textual elements
176 xccdf_resolve_textlist(item->item.title, parent->item.title, NULL);
177 xccdf_resolve_textlist(item->item.description, parent->item.description, NULL);
178 xccdf_resolve_textlist(item->item.question, parent->item.question, NULL);
179 xccdf_resolve_textlist(item->item.rationale, parent->item.rationale, NULL);
180 xccdf_resolve_textlist(item->item.warnings, parent->item.warnings, xccdf_resolve_warning);
181 xccdf_resolve_textlist(item->item.references, parent->item.references, NULL);
182
183 // resolve platforms
184 OSCAP_FOR_STR(platform, xccdf_item_get_platforms(parent))
185 xccdf_item_add_platform(item, platform);
186
187 // resolve properties specific to particular item type
188 switch (xccdf_item_get_type(item)) {
189 case XCCDF_PROFILE: xccdf_resolve_profile(item, parent); break;
190 case XCCDF_GROUP: xccdf_resolve_group(item, parent); break;
191 case XCCDF_RULE: xccdf_resolve_rule(item, parent); break;
192 case XCCDF_VALUE: xccdf_resolve_value(item, parent); break;
193 default: assert(false);
194 }
195
196 // item resolved -> it no longer has a parent
197 xccdf_item_set_extends(item, NULL);
198 }
199
200 // resolve textlists
201 static void xccdf_resolve_textlist(struct oscap_list *child_list, struct oscap_list *parent_list, xccdf_textresolve_func more)
202 {
203 OSCAP_FOR(oscap_text, child, oscap_iterator_new(child_list)) {
204 if (oscap_text_get_overrides(child)) continue;
205
206 OSCAP_FOR(oscap_text, parent, oscap_iterator_new(parent_list)) {
207 if (oscap_streq(oscap_text_get_lang(child), oscap_text_get_lang(parent))) {
208 char *text = oscap_sprintf("%s%s", oscap_text_get_text(parent), oscap_text_get_text(child));
209 oscap_text_set_text(child, text);
210 free(text);
211 if (more) more(child, parent);
212 break;
213 }
214 }
215 oscap_text_iterator_free(parent_iter);
216 }
217 }
218
219 static void xccdf_resolve_appendlist(struct oscap_list **child_list, struct oscap_list *parent_list,
220 oscap_cmp_func item_compare, oscap_clone_func cloner, bool prepend)
221 {
222 struct oscap_iterator *parent_iter = oscap_iterator_new(parent_list);
223 struct oscap_list *to_add = oscap_list_new();
224 while (oscap_iterator_has_more(parent_iter)) {
225 void *parent = oscap_iterator_next(parent_iter);
226 struct oscap_iterator *child_iter = oscap_iterator_new(*child_list);
227 bool found = false;
228 while (oscap_iterator_has_more(child_iter))
229 if (item_compare(parent, oscap_iterator_next(child_iter)))
230 found = true;
231 if (!found) oscap_list_add(to_add, cloner(parent));
232 oscap_iterator_free(child_iter);
233 }
234 oscap_iterator_free(parent_iter);
235 *child_list = (prepend ? oscap_list_destructive_join(*child_list, to_add) : oscap_list_destructive_join(to_add, *child_list));
236 }
237
238 static bool xccdf_select_idcmp(void *s1, void *s2) {
239 return oscap_streq(((struct xccdf_select*)s1)->item, ((struct xccdf_select*)s2)->item);
240 }
241 static bool xccdf_setvalue_idcmp(void *s1, void *s2) {
242 return oscap_streq(((struct xccdf_setvalue*)s1)->item, ((struct xccdf_setvalue*)s2)->item);
243 }
244 static bool xccdf_refine_rule_idcmp(void *s1, void *s2) {
245 return oscap_streq(((struct xccdf_refine_rule*)s1)->item, ((struct xccdf_refine_rule*)s2)->item);
246 }
247 static bool xccdf_refine_value_idcmp(void *s1, void *s2) {
248 return oscap_streq(((struct xccdf_refine_value*)s1)->item, ((struct xccdf_refine_value*)s2)->item);
249 }
250
251 static void xccdf_resolve_profile(struct xccdf_item *child, struct xccdf_item *parent)
252 {
253 if (child->sub.profile.note_tag != NULL) {
254 char *note_tag = oscap_sprintf("%s %s", xccdf_profile_get_note_tag(XPROFILE(child)), xccdf_profile_get_note_tag(XPROFILE(parent)));
255 xccdf_profile_set_note_tag(XPROFILE(child), note_tag);
256 free(note_tag);
257 }
258
259 xccdf_resolve_appendlist(&child->sub.profile.selects, parent->sub.profile.selects, xccdf_select_idcmp, (oscap_clone_func)xccdf_select_clone, false);
260 xccdf_resolve_appendlist(&child->sub.profile.setvalues, parent->sub.profile.setvalues, xccdf_setvalue_idcmp, (oscap_clone_func)xccdf_setvalue_clone, false);
261 xccdf_resolve_appendlist(&child->sub.profile.refine_rules, parent->sub.profile.refine_rules, xccdf_refine_rule_idcmp, (oscap_clone_func)xccdf_refine_rule_clone, false);
262 xccdf_resolve_appendlist(&child->sub.profile.refine_values, parent->sub.profile.refine_values, xccdf_refine_value_idcmp, (oscap_clone_func)xccdf_refine_value_clone, false);
263 }
264
265 static struct xccdf_item *xccdf_resolve_copy_item(struct xccdf_item *src)
266 {
267 struct xccdf_benchmark *bench = xccdf_item_get_benchmark(src);
268 const char *prefix = NULL;
269 switch (xccdf_item_get_type(src)) {
270 case XCCDF_RULE: prefix = "inherited-rule-"; break;
271 case XCCDF_GROUP: prefix = "inherited-group-"; break;
272 case XCCDF_VALUE: prefix = "inherited-value-"; break;
273 default: assert(false);
274 }
275 char *newid = xccdf_benchmark_gen_id(bench, xccdf_item_get_type(src), prefix);
276 struct xccdf_item *clone = xccdf_item_clone(src);
277 xccdf_item_set_id(clone, newid);
278 free(newid);
279 return clone;
280 }
281
282 static bool xccdf_incomparable(void *i1, void *i2) { return false; }
283 //static void *xccdf_strlist_clone(void *l) { return oscap_list_clone(l, (oscap_clone_func)oscap_strdup); }
284
285 static void xccdf_resolve_group(struct xccdf_item *child, struct xccdf_item *parent)
286 {
287 // TODO: resolve requires properly (how?)
288 //xccdf_resolve_appendlist(&child->sub.group.requires, parent->sub.group.requires, xccdf_incomparable, xccdf_strlist_clone, false);
289 xccdf_resolve_appendlist(&child->sub.group.conflicts, parent->sub.group.conflicts, (oscap_cmp_func)oscap_streq, (oscap_clone_func)oscap_strdup, false);
290
291 OSCAP_FOR(xccdf_item, item, xccdf_group_get_content(XGROUP(parent)))
292 xccdf_group_add_content(XGROUP(child), xccdf_resolve_copy_item(item));
293 OSCAP_FOR(xccdf_value, val, xccdf_group_get_values(XGROUP(parent)))
294 xccdf_group_add_value(XGROUP(child), xccdf_item_to_value(xccdf_resolve_copy_item(XITEM(val))));
295 }
296
297 static bool xccdf_ident_idcmp(void *s1, void *s2) {
298 return oscap_streq(((struct xccdf_ident*)s1)->id, ((struct xccdf_ident*)s2)->id);
299 }
300 static void xccdf_resolve_profile_note(void *p1, void *p2) {
301 if (xccdf_profile_note_get_reftag(p1) == NULL)
302 xccdf_profile_note_set_reftag(p1, xccdf_profile_note_get_reftag(p2));
303 }
304 static void xccdf_resolve_fixtext(struct xccdf_fixtext *t1, struct xccdf_fixtext *t2) {
305 if (xccdf_fixtext_get_fixref(t1) == NULL)
306 xccdf_fixtext_set_fixref(t1, xccdf_fixtext_get_fixref(t2));
307 if (xccdf_fixtext_get_disruption(t1) == 0)
308 xccdf_fixtext_set_disruption(t1, xccdf_fixtext_get_disruption(t2));
309 if (xccdf_fixtext_get_complexity(t1) == 0)
310 xccdf_fixtext_set_complexity(t1, xccdf_fixtext_get_complexity(t2));
311 }
312
313 static void xccdf_resolve_rule(struct xccdf_item *child, struct xccdf_item *parent)
314 {
315 // TODO: resolve requires properly (how?)
316 //xccdf_resolve_appendlist(&child->sub.rule.requires, parent->sub.rule.requires, xccdf_incomparable, xccdf_strlist_clone);
317 xccdf_resolve_appendlist(&child->sub.rule.conflicts, parent->sub.rule.conflicts, (oscap_cmp_func)oscap_streq, (oscap_clone_func)oscap_strdup, false);
318 xccdf_resolve_appendlist(&child->sub.rule.idents, parent->sub.rule.idents, (oscap_cmp_func)xccdf_ident_idcmp, (oscap_clone_func)xccdf_ident_clone, false);
319 xccdf_resolve_appendlist(&child->sub.rule.fixes, parent->sub.rule.fixes, (oscap_cmp_func)xccdf_incomparable, (oscap_clone_func)xccdf_fix_clone, false);
320
321 if (oscap_list_get_itemcount(child->sub.rule.checks) == 0 && oscap_list_get_itemcount(parent->sub.rule.checks) > 0) {
322 oscap_list_free(child->sub.rule.checks, NULL);
323 child->sub.rule.checks = oscap_list_clone(parent->sub.rule.checks, (oscap_clone_func)xccdf_check_clone);
324 }
325
326 if (!child->item.defined_flags.role) child->sub.rule.role = parent->sub.rule.role;
327 if (!child->item.defined_flags.severity) child->sub.rule.severity = parent->sub.rule.severity;
328
329 xccdf_resolve_textlist(child->sub.rule.profile_notes, parent->sub.rule.profile_notes, xccdf_resolve_profile_note);
330 xccdf_resolve_textlist(child->sub.rule.fixtexts, parent->sub.rule.fixtexts, (xccdf_textresolve_func)xccdf_resolve_fixtext);
331 }
332
333
334 static void xccdf_resolve_value(struct xccdf_item *child, struct xccdf_item *parent)
335 {
336 if (xccdf_value_get_interface_hint(XVALUE(child)) == 0)
337 child->sub.value.interface_hint = xccdf_value_get_interface_hint(XVALUE(parent));
338 if (xccdf_value_get_oper(XVALUE(child)) == 0)
339 xccdf_value_set_oper(xccdf_item_to_value(child), xccdf_value_get_oper(XVALUE(parent)));
340
341 struct xccdf_value_instance *inst_child;
342 OSCAP_FOR(xccdf_value_instance, inst_parent, xccdf_value_get_instances(XVALUE(parent))) {
343 inst_child = xccdf_value_get_instance_by_selector(XVALUE(child), xccdf_value_instance_get_selector(inst_parent));
344 if (inst_child == NULL) xccdf_value_add_instance(XVALUE(child), xccdf_value_instance_clone(inst_parent));
345 else xccdf_resolve_value_instance(inst_child, inst_parent);
346 }
347 }
348
349 static void xccdf_resolve_value_instance(struct xccdf_value_instance *child, struct xccdf_value_instance *parent)
350 {
351 assert(child != NULL);
352 assert(parent != NULL);
353 assert(oscap_streq(child->selector, parent->selector));
354
355 if (!child->flags.value_given)
356 child->value = oscap_strdup(parent->value);
357 if (!child->flags.defval_given)
358 child->defval = oscap_strdup(parent->defval);
359 if (child->type == XCCDF_TYPE_STRING && child->match == NULL)
360 xccdf_value_instance_set_match(child, xccdf_value_instance_get_match(parent));
361 if (child->type == XCCDF_TYPE_NUMBER) {
362 if (isnan(child->lower_bound))
363 child->lower_bound = parent->lower_bound;
364 if (isnan(child->upper_bound))
365 child->upper_bound = parent->upper_bound;
366 }
367
368 struct oscap_iterator *it = oscap_iterator_new(parent->choices);
369 while (oscap_iterator_has_more(it)) {
370 const char *unit = oscap_iterator_next(it);
371 oscap_list_add(child->choices, oscap_strdup(unit));
372 }
373 oscap_iterator_free(it);
374 }
375
376 static void xccdf_resolve_cleanup(struct xccdf_item *item)
377 {
378 if (item == NULL) return;
379
380 switch (xccdf_item_get_type(item)) {
381 case XCCDF_BENCHMARK: case XCCDF_GROUP: break;
382 default: return;
383 }
384
385 if (xccdf_item_get_type(item) == XCCDF_BENCHMARK) {
386 OSCAP_FOR(xccdf_profile, p, xccdf_benchmark_get_profiles(xccdf_item_to_benchmark(item)))
387 if (xccdf_profile_get_abstract(p))
388 xccdf_profile_iterator_remove(p_iter);
389 }
390
391 OSCAP_FOR(xccdf_item, sub, xccdf_item_get_content(item)) {
392 xccdf_resolve_cleanup(sub);
393 if (xccdf_item_get_abstract(sub))
394 xccdf_item_iterator_remove(sub_iter);
395 }
396
397 struct xccdf_value_iterator *value_it = NULL;
398
399 switch (xccdf_item_get_type(item)) {
400 case XCCDF_BENCHMARK: value_it = xccdf_benchmark_get_values(xccdf_item_to_benchmark(item)); break;
401 case XCCDF_GROUP: value_it = xccdf_group_get_values(xccdf_item_to_group(item)); break;
402 default: break;
403 }
404
405 OSCAP_FOR(xccdf_value, val, value_it)
406 if (xccdf_value_get_abstract(val))
407 xccdf_value_iterator_remove(val_iter);
408 }
409
410 static struct oscap_list *xccdf_tailoring_resolve_dependencies(void *itemptr, void *userdata)
411 {
412 struct xccdf_tailoring *tailoring = (struct xccdf_tailoring*)userdata;
413 assert(xccdf_item_get_type(XITEM(itemptr)) == XCCDF_PROFILE);
414 struct xccdf_profile *profile = XPROFILE(itemptr);
415
416 struct oscap_list *ret = oscap_list_new();
417 oscap_list_add(ret, _xccdf_tailoring_profile_get_real_parent(tailoring, profile));
418 return ret;
419 }
420
421 bool xccdf_tailoring_resolve(struct xccdf_tailoring *tailoring, struct xccdf_benchmark *benchmark)
422 {
423 struct oscap_list *resolve_order = NULL;
424 struct oscap_list *profiles = oscap_list_new();
425
426 struct xccdf_profile_iterator *it = xccdf_tailoring_get_profiles(tailoring);
427 while (xccdf_profile_iterator_has_more(it)) {
428 struct xccdf_profile *profile = xccdf_profile_iterator_next(it);
429 oscap_list_add(profiles, profile);
430 }
431 xccdf_profile_iterator_free(it);
432
433 bool ret = false;
434
435 if (oscap_tsort(profiles, &resolve_order, xccdf_tailoring_resolve_dependencies, NULL, tailoring)) {
436 OSCAP_FOR(xccdf_profile, profile, oscap_iterator_new(resolve_order))
437 xccdf_resolve_item(XITEM(profile), tailoring);
438 ret = true;
439 }
440
441 oscap_list_free(profiles, NULL);
442 oscap_list_free(resolve_order, NULL);
443
444 return ret;
445 }