Debian Codegraph @

src/XCCDF/tailoring.c

raw symbols SCIP
1 /*
2 * Copyright 2009 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 * Martin Preisler <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28
29 #include "item.h"
30 #include "helpers.h"
31 #include "xccdf_impl.h"
32 #include "common/_error.h"
33 #include "common/debug_priv.h"
34 #include "common/elements.h"
35 #include "source/oscap_source_priv.h"
36 #include "source/public/oscap_source.h"
37
38 struct xccdf_tailoring *xccdf_tailoring_new(void)
39 {
40 struct xccdf_tailoring *tailoring = calloc(1, sizeof(struct xccdf_tailoring));
41
42 tailoring->id = NULL;
43
44 tailoring->benchmark_ref = NULL;
45 tailoring->benchmark_ref_version = NULL;
46
47 tailoring->statuses = oscap_list_new();
48 tailoring->dc_statuses = oscap_list_new();
49
50 tailoring->version = NULL;
51 tailoring->version_update = NULL;
52 tailoring->version_time = NULL;
53
54 tailoring->metadata = oscap_list_new();
55 tailoring->profiles = oscap_list_new();
56
57 return tailoring;
58 }
59
60 void xccdf_tailoring_free(struct xccdf_tailoring *tailoring)
61 {
62 if (tailoring) {
63 free(tailoring->id);
64
65 free(tailoring->benchmark_ref);
66 free(tailoring->benchmark_ref_version);
67
68 oscap_list_free(tailoring->statuses, (oscap_destruct_func) xccdf_status_free);
69 oscap_list_free(tailoring->dc_statuses, (oscap_destruct_func) oscap_reference_free);
70
71 free(tailoring->version);
72 free(tailoring->version_update);
73 free(tailoring->version_time);
74
75 oscap_list_free(tailoring->metadata, (oscap_destruct_func) free);
76 oscap_list_free(tailoring->profiles, (oscap_destruct_func) xccdf_profile_free);
77
78 free(tailoring);
79 }
80 }
81
82 bool xccdf_tailoring_add_profile(struct xccdf_tailoring *tailoring, struct xccdf_profile *profile)
83 {
84 xccdf_profile_set_tailoring(profile, true);
85 return oscap_list_add(tailoring->profiles, XITEM(profile));
86 }
87
88 static bool _list_ptreq_cmp(void *a, void *b)
89 {
90 return a == b;
91 }
92
93 bool xccdf_tailoring_remove_profile(struct xccdf_tailoring *tailoring, struct xccdf_profile *profile)
94 {
95 assert(xccdf_profile_get_tailoring(profile));
96
97 // We have to make sure there is no other profile in tailoring that inherits
98 // the profile we are about to remove.
99
100 const char *profile_id = xccdf_profile_get_id(profile);
101
102 struct xccdf_profile_iterator* it = xccdf_tailoring_get_profiles(tailoring);
103 while (xccdf_profile_iterator_has_more(it)) {
104 struct xccdf_profile* prof = xccdf_profile_iterator_next(it);
105
106 if (prof == profile)
107 continue;
108
109 const char *extends = xccdf_profile_get_extends(prof);
110 if (oscap_strcmp(profile_id, extends) == 0) {
111 oscap_seterr(OSCAP_EFAMILY_XML,
112 "Can't remove given profile '%s' from tailoring. Other profiles are inheriting from it!",
113 profile_id);
114
115 xccdf_profile_iterator_free(it);
116 return false;
117 }
118 }
119 xccdf_profile_iterator_free(it);
120
121 return oscap_list_remove(tailoring->profiles, XITEM(profile), (oscap_cmp_func)_list_ptreq_cmp, NULL);
122 }
123
124 struct xccdf_tailoring *xccdf_tailoring_parse(xmlTextReaderPtr reader, struct xccdf_item *benchmark)
125 {
126 XCCDF_ASSERT_ELEMENT(reader, XCCDFE_TAILORING);
127
128 struct xccdf_tailoring *tailoring = xccdf_tailoring_new();
129
130 const char *id = xccdf_attribute_get(reader, XCCDFA_ID);
131 xccdf_tailoring_set_id(tailoring, id);
132
133 int depth = oscap_element_depth(reader) + 1;
134
135 // Read to the inside of Tailoring.
136 xmlTextReaderRead(reader);
137
138 while (oscap_to_start_element(reader, depth)) {
139 switch (xccdf_element_get(reader)) {
140 case XCCDFE_BENCHMARK_REF: {
141 free(tailoring->benchmark_ref);
142 tailoring->benchmark_ref = 0;
143
144 free(tailoring->benchmark_ref_version);
145 tailoring->benchmark_ref_version = 0;
146
147 const char *ref = xccdf_attribute_get(reader, XCCDFA_HREF);
148 if (ref)
149 tailoring->benchmark_ref = oscap_strdup(ref);
150
151 const char *ref_version = xccdf_attribute_get(reader, XCCDFA_VERSION);
152 if (ref_version)
153 tailoring->benchmark_ref_version = oscap_strdup(ref_version);
154
155 break;
156 }
157 case XCCDFE_STATUS: {
158 const char *date = xccdf_attribute_get(reader, XCCDFA_DATE);
159 char *str = oscap_element_string_copy(reader);
160 struct xccdf_status *status = xccdf_status_new_fill(str, date);
161 free(str);
162 oscap_list_add(tailoring->statuses, status);
163 break;
164 }
165 case XCCDFE_DC_STATUS:
166 oscap_list_add(tailoring->dc_statuses, oscap_reference_new_parse(reader));
167 break;
168 case XCCDFE_VERSION: {
169 xmlNode *ver = xmlTextReaderExpand(reader);
170 /* optional attributes */
171 tailoring->version_time = (char*) xmlGetProp(ver, BAD_CAST "time");
172 tailoring->version_update = (char*) xmlGetProp(ver, BAD_CAST "update");
173 /* content */
174 tailoring->version = (char *) xmlNodeGetContent(ver);
175 if (oscap_streq(tailoring->version, "")) {
176 free(tailoring->version);
177 tailoring->version = NULL;
178 }
179 break;
180 }
181 case XCCDFE_METADATA: {
182 char* xml = oscap_get_xml(reader);
183 oscap_list_add(tailoring->metadata, oscap_strdup(xml));
184 free(xml);
185 break;
186 }
187 case XCCDFE_PROFILE: {
188 if (benchmark != NULL) {
189 dI("Parsing Tailoring Profiles without reference to Benchmark");
190 }
191 struct xccdf_item *item = xccdf_profile_parse(reader, benchmark);
192 if (!xccdf_tailoring_add_profile(tailoring, XPROFILE(item))) {
193 dW("Failed to add profile to tailoring while parsing!");
194 }
195 break;
196 }
197 default:
198 dW("Encountered an unknown element '%s' while parsing XCCDF Tailoring element.",
199 xmlTextReaderConstLocalName(reader));
200 }
201 xmlTextReaderRead(reader);
202 }
203
204 return tailoring;
205 }
206
207 struct xccdf_tailoring *xccdf_tailoring_import_source(struct oscap_source *source, struct xccdf_benchmark *benchmark)
208 {
209 xmlTextReaderPtr reader = oscap_source_get_xmlTextReader(source);
210 if (!reader) {
211 return NULL;
212 }
213
214 while (xmlTextReaderRead(reader) == 1 && xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT) ;
215 struct xccdf_tailoring *tailoring = xccdf_tailoring_parse(reader, XITEM(benchmark));
216 xmlFreeTextReader(reader);
217 if (!tailoring) { // parsing fatal error
218 oscap_seterr(OSCAP_EFAMILY_XML, "Failed to parse tailoring from '%s'.", oscap_source_readable_origin(source));
219 }
220 return tailoring;
221 }
222
223 xmlNodePtr xccdf_tailoring_to_dom(struct xccdf_tailoring *tailoring, xmlDocPtr doc, xmlNodePtr parent, const struct xccdf_version_info *version_info)
224 {
225 xmlNs *ns_tailoring = NULL;
226
227 xmlNode *tailoring_node = xmlNewNode(NULL, BAD_CAST "Tailoring");
228
229 const char *xccdf_version = xccdf_version_info_get_version(version_info);
230 #ifdef __USE_GNU
231 if (strverscmp(xccdf_version, "1.1") >= 0 && strverscmp(xccdf_version, "1.2") < 0) {
232 #else
233 if (strcmp(xccdf_version, "1.1") >= 0 && strcmp(xccdf_version, "1.2") < 0) {
234 #endif
235 // XCCDF 1.1 does not support Tailoring!
236 // However we will allow Tailoring export if it is done to an external
237 // file. The namespace will be our custom xccdf-1.1-tailoring extension
238 // namespace.
239
240 if (parent != NULL) {
241 oscap_seterr(OSCAP_EFAMILY_XML, "XCCDF 1.1 does not support embedded Tailoring elements!");
242 xmlFreeNode(tailoring_node);
243 return NULL;
244 }
245
246 ns_tailoring = xmlNewNs(tailoring_node,
247 BAD_CAST "http://open-scap.org/page/Xccdf-1.1-tailoring",
248 BAD_CAST "cdf-11-tailoring"
249 );
250 }
251 #ifdef __USE_GNU
252 else if (strverscmp(xccdf_version, "1.1") < 0) {
253 #else
254 else if (strcmp(xccdf_version, "1.1") < 0) {
255 #endif
256 oscap_seterr(OSCAP_EFAMILY_XML, "XCCDF Tailoring isn't supported in XCCDF version '%s',"
257 "nor does openscap have a custom extension for this scenario. "
258 "XCCDF Tailoring requires XCCDF 1.1 and higher, 1.2 is recommended.",
259 xccdf_version);
260
261 xmlFreeNode(tailoring_node);
262 return NULL;
263 }
264
265 xmlNs *ns_xccdf = parent != NULL ? lookup_xccdf_ns(doc, parent, version_info) : NULL;
266 if (!ns_xccdf) {
267 // In cases where tailoring ends up being the root node we have to create
268 // a namespace at the node itself.
269 ns_xccdf = xmlNewNs(tailoring_node,
270 BAD_CAST xccdf_version_info_get_namespace_uri(version_info),
271 BAD_CAST "xccdf");
272 }
273
274 if (!ns_tailoring)
275 ns_tailoring = ns_xccdf;
276
277 // We intentionally set the wrong namespace here since children of tailoring
278 // will reuse it and we want them to have the xccdf namespace, the namespace
279 // is set to the proper namespace before returning the tailoring.
280 xmlSetNs(tailoring_node, ns_xccdf);
281
282 if (parent)
283 xmlAddChild(parent, tailoring_node);
284 else
285 xmlDocSetRootElement(doc, tailoring_node);
286
287 if (tailoring->id) {
288 xmlNewProp(tailoring_node, BAD_CAST "id", BAD_CAST tailoring->id);
289 }
290
291 if (tailoring->benchmark_ref || tailoring->benchmark_ref_version) {
292 xmlNodePtr benchmark_ref_node = xmlNewChild(tailoring_node, ns_tailoring, BAD_CAST "benchmark", NULL);
293
294 if (tailoring->benchmark_ref)
295 xmlNewProp(benchmark_ref_node, BAD_CAST "href", BAD_CAST tailoring->benchmark_ref);
296
297 if (tailoring->benchmark_ref_version)
298 xmlNewProp(benchmark_ref_node, BAD_CAST "version", BAD_CAST tailoring->benchmark_ref_version);
299 }
300
301 struct xccdf_status_iterator *statuses = xccdf_tailoring_get_statuses(tailoring);
302 while (xccdf_status_iterator_has_more(statuses)) {
303 struct xccdf_status *status = xccdf_status_iterator_next(statuses);
304 xccdf_status_to_dom(status, doc, tailoring_node, version_info);
305 }
306 xccdf_status_iterator_free(statuses);
307
308 struct oscap_reference_iterator *dc_statuses = xccdf_tailoring_get_dc_statuses(tailoring);
309 while (oscap_reference_iterator_has_more(dc_statuses)) {
310 struct oscap_reference *ref = oscap_reference_iterator_next(dc_statuses);
311 oscap_reference_to_dom(ref, tailoring_node, doc, "dc-status");
312 }
313 oscap_reference_iterator_free(dc_statuses);
314
315 /* version and attributes */
316 const char *version = xccdf_tailoring_get_version(tailoring);
317 if (version) {
318 xmlNode* version_node = xmlNewTextChild(tailoring_node, ns_tailoring, BAD_CAST "version", BAD_CAST version);
319
320 const char *version_update = xccdf_tailoring_get_version_update(tailoring);
321 if (version_update)
322 xmlNewProp(version_node, BAD_CAST "update", BAD_CAST version_update);
323
324 const char *version_time = xccdf_tailoring_get_version_time(tailoring);
325 if (version_time)
326 xmlNewProp(version_node, BAD_CAST "time", BAD_CAST version_time);
327 }
328
329 struct oscap_string_iterator* metadata = xccdf_tailoring_get_metadata(tailoring);
330 while (oscap_string_iterator_has_more(metadata))
331 {
332 const char* meta = oscap_string_iterator_next(metadata);
333 xmlNode *m = oscap_xmlstr_to_dom(tailoring_node, "metadata", meta);
334 xmlSetNs(m, ns_xccdf);
335 }
336 oscap_string_iterator_free(metadata);
337
338 struct xccdf_profile_iterator *profiles = xccdf_tailoring_get_profiles(tailoring);
339 while (xccdf_profile_iterator_has_more(profiles)) {
340 struct xccdf_profile *profile = xccdf_profile_iterator_next(profiles);
341 xccdf_item_to_dom(XITEM(profile), doc, tailoring_node, version_info);
342 }
343 xccdf_profile_iterator_free(profiles);
344
345 xmlSetNs(tailoring_node, ns_tailoring);
346
347 return tailoring_node;
348 }
349
350 int xccdf_tailoring_export(struct xccdf_tailoring *tailoring, const char *file, const struct xccdf_version_info *version_info)
351 {
352 __attribute__nonnull__(file);
353
354 LIBXML_TEST_VERSION;
355
356 xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
357 if (doc == NULL) {
358 oscap_setxmlerr(xmlGetLastError());
359 return -1;
360 }
361
362 xccdf_tailoring_to_dom(tailoring, doc, NULL, version_info);
363
364 return oscap_xml_save_filename_free(file, doc);
365 }
366
367 const char *xccdf_tailoring_get_id(const struct xccdf_tailoring *tailoring)
368 {
369 return tailoring->id;
370 }
371
372 const char *xccdf_tailoring_get_version(const struct xccdf_tailoring *tailoring)
373 {
374 return tailoring->version;
375 }
376
377 const char *xccdf_tailoring_get_version_update(const struct xccdf_tailoring *tailoring)
378 {
379 return tailoring->version_update;
380 }
381
382 const char *xccdf_tailoring_get_version_time(const struct xccdf_tailoring *tailoring)
383 {
384 return tailoring->version_time;
385 }
386
387 const char *xccdf_tailoring_get_benchmark_ref(const struct xccdf_tailoring *tailoring)
388 {
389 return tailoring->benchmark_ref;
390 }
391
392 const char *xccdf_tailoring_get_benchmark_ref_version(const struct xccdf_tailoring *tailoring)
393 {
394 return tailoring->benchmark_ref_version;
395 }
396
397 bool xccdf_tailoring_set_id(struct xccdf_tailoring *tailoring, const char* newval)
398 {
399 if (tailoring->id)
400 free(tailoring->id);
401
402 tailoring->id = oscap_strdup(newval);
403 return true;
404 }
405
406 bool xccdf_tailoring_set_version(struct xccdf_tailoring *tailoring, const char *newval)
407 {
408 if (tailoring->version)
409 free(tailoring->version);
410
411 tailoring->version = oscap_strdup(newval);
412 return true;
413 }
414
415 bool xccdf_tailoring_set_version_update(struct xccdf_tailoring *tailoring, const char *newval)
416 {
417 if (tailoring->version_update)
418 free(tailoring->version_update);
419
420 tailoring->version_update = oscap_strdup(newval);
421 return true;
422 }
423
424 bool xccdf_tailoring_set_version_time(struct xccdf_tailoring *tailoring, const char *newval)
425 {
426 if (tailoring->version_time)
427 free(tailoring->version_time);
428
429 tailoring->version_time = oscap_strdup(newval);
430 return true;
431 }
432
433 bool xccdf_tailoring_set_benchmark_ref(struct xccdf_tailoring *tailoring, const char *newval)
434 {
435 if (tailoring->benchmark_ref)
436 free(tailoring->benchmark_ref);
437
438 tailoring->benchmark_ref = oscap_strdup(newval);
439 return true;
440 }
441
442 bool xccdf_tailoring_set_benchmark_ref_version(struct xccdf_tailoring *tailoring, const char *newval)
443 {
444 if (tailoring->benchmark_ref_version)
445 free(tailoring->benchmark_ref_version);
446
447 tailoring->benchmark_ref_version = oscap_strdup(newval);
448 return true;
449 }
450
451 struct oscap_string_iterator *xccdf_tailoring_get_metadata(const struct xccdf_tailoring *tailoring)
452 {
453 return (struct oscap_string_iterator*) oscap_iterator_new(tailoring->metadata);
454 }
455
456 struct xccdf_profile_iterator *xccdf_tailoring_get_profiles(const struct xccdf_tailoring *tailoring)
457 {
458 return (struct xccdf_profile_iterator*) oscap_iterator_new(tailoring->profiles);
459 }
460
461 struct xccdf_status_iterator *xccdf_tailoring_get_statuses(const struct xccdf_tailoring *tailoring)
462 {
463 return (struct xccdf_status_iterator*) oscap_iterator_new(tailoring->statuses);
464 }
465
466 struct oscap_reference_iterator *xccdf_tailoring_get_dc_statuses(const struct xccdf_tailoring *tailoring)
467 {
468 return (struct oscap_reference_iterator*) oscap_iterator_new(tailoring->dc_statuses);
469 }
470
471 struct xccdf_profile *
472 xccdf_tailoring_get_profile_by_id(const struct xccdf_tailoring *tailoring, const char *profile_id)
473 {
474 struct xccdf_profile_iterator *profit = xccdf_tailoring_get_profiles(tailoring);
475 while (xccdf_profile_iterator_has_more(profit)) {
476 struct xccdf_profile *profile = xccdf_profile_iterator_next(profit);
477 if (profile == NULL) {
478 assert(profile != NULL);
479 continue;
480 }
481 if (oscap_streq(xccdf_profile_get_id(profile), profile_id)) {
482 xccdf_profile_iterator_free(profit);
483 return profile;
484 }
485 }
486 xccdf_profile_iterator_free(profit);
487 return NULL;
488 }