Debian Codegraph @

src/XCCDF_POLICY/xccdf_policy_model.c

raw symbols SCIP
1 /*
2 * Copyright 2016 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 * Šimon Lukašík
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "common/list.h"
28 #include "public/oscap_text.h"
29 #include "public/xccdf_benchmark.h"
30 #include "public/xccdf_policy.h"
31 #include "xccdf_policy_model_priv.h"
32 #include "xccdf_policy_priv.h"
33 #include "XCCDF/item.h"
34 #include "XCCDF/helpers.h"
35
36 struct xccdf_policy *xccdf_policy_model_get_existing_policy_by_id(struct xccdf_policy_model *policy_model, const char *profile_id)
37 {
38 struct xccdf_policy_iterator *policy_it = xccdf_policy_model_get_policies(policy_model);
39 while (xccdf_policy_iterator_has_more(policy_it)) {
40 struct xccdf_policy *policy = xccdf_policy_iterator_next(policy_it);
41 if (oscap_streq(xccdf_policy_get_id(policy), profile_id)) {
42 xccdf_policy_iterator_free(policy_it);
43 return policy;
44 }
45 }
46 xccdf_policy_iterator_free(policy_it);
47 return NULL;
48 }
49
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
77 struct xccdf_policy *xccdf_policy_model_create_policy_by_id(struct xccdf_policy_model *policy_model, const char *id)
78 {
79 struct xccdf_profile *profile = NULL;
80 struct xccdf_tailoring *tailoring = xccdf_policy_model_get_tailoring(policy_model);
81
82 // Tailoring profiles take precedence over Benchmark profiles.
83 if (tailoring) {
84 profile = xccdf_tailoring_get_profile_by_id(tailoring, id);
85 }
86
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
90 if (!profile) {
91 if (id == NULL) {
92 profile = xccdf_profile_new();
93 xccdf_profile_set_id(profile, NULL);
94 struct oscap_text * title = oscap_text_new();
95 oscap_text_set_text(title, "No profile (default benchmark)");
96 oscap_text_set_lang(title, "en");
97 xccdf_profile_add_title(profile, title);
98 } else {
99 struct xccdf_benchmark *benchmark = xccdf_policy_model_get_benchmark(policy_model);
100 if (benchmark == NULL) {
101 assert(benchmark != NULL);
102 return NULL;
103 }
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 }
119 }
120 }
121
122 return xccdf_policy_new(policy_model, profile);
123 }
124
125 static int _xccdf_policy_model_create_policy_if_useful(struct xccdf_policy_model *policy_model, const char *profile_id)
126 {
127 if (xccdf_policy_model_get_existing_policy_by_id(policy_model, profile_id) == NULL) {
128 struct xccdf_policy *policy = xccdf_policy_model_create_policy_by_id(policy_model, profile_id);
129 if (policy == NULL) {
130 return -1;
131 }
132 if (xccdf_policy_get_selected_rules_count(policy) > 0) {
133 xccdf_policy_model_add_policy(policy_model, policy);
134 }
135 }
136 return 0;
137 }
138
139 static int _xccdf_policy_model_create_policies_if_useful(struct xccdf_policy_model *policy_model, struct xccdf_profile_iterator *profit)
140 {
141 while (xccdf_profile_iterator_has_more(profit)) {
142 struct xccdf_profile *profile = xccdf_profile_iterator_next(profit);
143 const char *profile_id = xccdf_profile_get_id(profile);
144 if (_xccdf_policy_model_create_policy_if_useful(policy_model, profile_id) != 0) {
145 xccdf_profile_iterator_free(profit);
146 return -1;
147 }
148 }
149 xccdf_profile_iterator_free(profit);
150 return 0;
151 }
152
153 int xccdf_policy_model_build_all_useful_policies(struct xccdf_policy_model *policy_model)
154 {
155 if (_xccdf_policy_model_create_policy_if_useful(policy_model, NULL) != 0) {
156 return -1;
157 }
158 struct xccdf_tailoring *tailoring = xccdf_policy_model_get_tailoring(policy_model);
159 if (tailoring != NULL) {
160 struct xccdf_profile_iterator *profit = xccdf_tailoring_get_profiles(tailoring);
161 if (_xccdf_policy_model_create_policies_if_useful(policy_model, profit) != 0) {
162 return -1;
163 }
164 }
165
166 struct xccdf_benchmark *benchmark = xccdf_policy_model_get_benchmark(policy_model);
167 if (benchmark == NULL) {
168 return -1;
169 }
170 struct xccdf_profile_iterator *profit = xccdf_benchmark_get_profiles(benchmark);
171 if (_xccdf_policy_model_create_policies_if_useful(policy_model, profit) != 0) {
172 return -1;
173 }
174 return 0;
175 }