Skip to content

Commit ff5d42d

Browse files
author
Martin Preisler
committed
Introduce a "virtual" "(all)" profile that will select all groups and all rules
This is useful for testing and debugging. It will use default values but will select everything in the benchmark.
1 parent 57bfc51 commit ff5d42d

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/XCCDF_POLICY/xccdf_policy.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,8 +2283,10 @@ void xccdf_policy_free(struct xccdf_policy * policy) {
22832283
/* A policy which is set to use default profile has its profile member set to NULL,
22842284
* check it so we don't try to get the ID from a NULL profile.
22852285
* */
2286-
if (policy->profile && xccdf_profile_get_id(policy->profile) == NULL)
2287-
/* If ID of policy's profile is NULL then this
2286+
if (policy->profile && (
2287+
(xccdf_profile_get_id(policy->profile) == NULL) ||
2288+
(strcmp(xccdf_profile_get_id(policy->profile), "(all)") == 0)))
2289+
/* If ID of policy's profile is NULL or "(all)" then this
22882290
* profile is created by Policy layer and need
22892291
* to be freed
22902292
*/

src/XCCDF_POLICY/xccdf_policy_model.c

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "xccdf_policy_model_priv.h"
3232
#include "xccdf_policy_priv.h"
3333
#include "XCCDF/item.h"
34+
#include "XCCDF/helpers.h"
3435

3536
struct xccdf_policy *xccdf_policy_model_get_existing_policy_by_id(struct xccdf_policy_model *policy_model, const char *profile_id)
3637
{
@@ -46,6 +47,38 @@ struct xccdf_policy *xccdf_policy_model_get_existing_policy_by_id(struct xccdf_p
4647
return NULL;
4748
}
4849

50+
static inline void _add_selectors_for_all_items(struct xccdf_profile *profile, struct xccdf_item *item)
51+
{
52+
struct xccdf_item_iterator *children = NULL;
53+
if (xccdf_item_get_type(item) == XCCDF_BENCHMARK) {
54+
children = xccdf_benchmark_get_content(XBENCHMARK(item));
55+
}
56+
else if (xccdf_item_get_type(item) == XCCDF_GROUP) {
57+
children = xccdf_group_get_content(XGROUP(item));
58+
59+
struct xccdf_select *select = xccdf_select_new();
60+
xccdf_select_set_item(select, xccdf_item_get_id(item));
61+
xccdf_select_set_selected(select, true);
62+
xccdf_profile_add_select(profile, select);
63+
printf("g: %s\n", xccdf_item_get_id(item));
64+
}
65+
else if (xccdf_item_get_type(item) == XCCDF_RULE) {
66+
struct xccdf_select *select = xccdf_select_new();
67+
xccdf_select_set_item(select, xccdf_item_get_id(item));
68+
xccdf_select_set_selected(select, true);
69+
xccdf_profile_add_select(profile, select);
70+
printf("r: %s\n", xccdf_item_get_id(item));
71+
}
72+
73+
if (children) {
74+
while (xccdf_item_iterator_has_more(children)) {
75+
struct xccdf_item *current = xccdf_item_iterator_next(children);
76+
_add_selectors_for_all_items(profile, current);
77+
}
78+
xccdf_item_iterator_free(children);
79+
}
80+
}
81+
4982
struct xccdf_policy *xccdf_policy_model_create_policy_by_id(struct xccdf_policy_model *policy_model, const char *id)
5083
{
5184
struct xccdf_profile *profile = NULL;
@@ -71,9 +104,22 @@ struct xccdf_policy *xccdf_policy_model_create_policy_by_id(struct xccdf_policy_
71104
assert(benchmark != NULL);
72105
return NULL;
73106
}
74-
profile = xccdf_benchmark_get_profile_by_id(benchmark, id);
75-
if (profile == NULL)
76-
return NULL;
107+
108+
if (strcmp(id, "(all)") == 0) {
109+
profile = xccdf_profile_new();
110+
xccdf_profile_set_id(profile, "(all)");
111+
struct oscap_text *title = oscap_text_new();
112+
oscap_text_set_text(title, "(all) profile (all rules selected)");
113+
oscap_text_set_lang(title, "en");
114+
xccdf_profile_add_title(profile, title);
115+
116+
_add_selectors_for_all_items(profile, XITEM(benchmark));
117+
}
118+
else {
119+
profile = xccdf_benchmark_get_profile_by_id(benchmark, id);
120+
if (profile == NULL)
121+
return NULL;
122+
}
77123
}
78124
}
79125

0 commit comments

Comments
 (0)