Skip to content

Commit 2a739d4

Browse files
authored
Merge pull request #1193 from mpreisler/all_profile
Introduce a "virtual" "(all)" profile that will select all groups and…
2 parents 8890caa + a7e1395 commit 2a739d4

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
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: 47 additions & 5 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,33 @@ struct xccdf_policy *xccdf_policy_model_get_existing_policy_by_id(struct xccdf_p
4647
return NULL;
4748
}
4849

50+
static void _add_selectors_for_all_xccdf_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+
} else if (xccdf_item_get_type(item) == XCCDF_GROUP) {
56+
children = xccdf_group_get_content(XGROUP(item));
57+
}
58+
59+
if (xccdf_item_get_type(item) == XCCDF_RULE ||
60+
xccdf_item_get_type(item) == XCCDF_GROUP)
61+
{
62+
struct xccdf_select *select = xccdf_select_new();
63+
xccdf_select_set_item(select, xccdf_item_get_id(item));
64+
xccdf_select_set_selected(select, true);
65+
xccdf_profile_add_select(profile, select);
66+
}
67+
68+
if (children) {
69+
while (xccdf_item_iterator_has_more(children)) {
70+
struct xccdf_item *current = xccdf_item_iterator_next(children);
71+
_add_selectors_for_all_xccdf_items(profile, current);
72+
}
73+
xccdf_item_iterator_free(children);
74+
}
75+
}
76+
4977
struct xccdf_policy *xccdf_policy_model_create_policy_by_id(struct xccdf_policy_model *policy_model, const char *id)
5078
{
5179
struct xccdf_profile *profile = NULL;
@@ -56,6 +84,9 @@ struct xccdf_policy *xccdf_policy_model_create_policy_by_id(struct xccdf_policy_
5684
profile = xccdf_tailoring_get_profile_by_id(tailoring, id);
5785
}
5886

87+
// The (default) and (all) profiles are de-facto owned by the xccdf_policy
88+
// and will be freed by it when it's freed. See xccdf_policy_free.
89+
5990
if (!profile) {
6091
if (id == NULL) {
6192
profile = xccdf_profile_new();
@@ -64,16 +95,27 @@ struct xccdf_policy *xccdf_policy_model_create_policy_by_id(struct xccdf_policy_
6495
oscap_text_set_text(title, "No profile (default benchmark)");
6596
oscap_text_set_lang(title, "en");
6697
xccdf_profile_add_title(profile, title);
67-
}
68-
else {
98+
} else {
6999
struct xccdf_benchmark *benchmark = xccdf_policy_model_get_benchmark(policy_model);
70100
if (benchmark == NULL) {
71101
assert(benchmark != NULL);
72102
return NULL;
73103
}
74-
profile = xccdf_benchmark_get_profile_by_id(benchmark, id);
75-
if (profile == NULL)
76-
return NULL;
104+
105+
if (strcmp(id, "(all)") == 0) {
106+
profile = xccdf_profile_new();
107+
xccdf_profile_set_id(profile, "(all)");
108+
struct oscap_text *title = oscap_text_new();
109+
oscap_text_set_text(title, "(all) profile (all rules selected)");
110+
oscap_text_set_lang(title, "en");
111+
xccdf_profile_add_title(profile, title);
112+
113+
_add_selectors_for_all_xccdf_items(profile, XITEM(benchmark));
114+
} else {
115+
profile = xccdf_benchmark_get_profile_by_id(benchmark, id);
116+
if (profile == NULL)
117+
return NULL;
118+
}
77119
}
78120
}
79121

utils/oscap.8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ You may specify OVAL Definition files as the last parameter, XCCDF evaluation wi
8383
.TP
8484
\fB\-\-profile PROFILE\fR
8585
.RS
86-
Select a particular profile from XCCDF document.
86+
Select a particular profile from XCCDF document. If "(all)" is given a virtual profile that selects all groups and rules will be used.
8787
.RE
8888
.TP
8989
\fB\-\-rule RULE\fR

0 commit comments

Comments
 (0)