Debian Codegraph @

src/XCCDF/item.c

raw symbols SCIP
1 /*
2 * Copyright 2009--2013 Red Hat Inc., Durham, North Carolina.
3 * Copyright (C) 2010 Tresys Technology, LLC
4 * All Rights Reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Lukas Kuklinek <[email protected]>
22 * Josh Adams <[email protected]>
23 * Šimon Lukašík
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <string.h>
31 #include <time.h>
32 #include <math.h>
33
34 #include <libxml/tree.h>
35 #include <libxml/xpath.h>
36 #include <libxml/parser.h>
37 #include <libxml/xpathInternals.h>
38
39 #include "item.h"
40 #include "helpers.h"
41 #include "xccdf_impl.h"
42 #include "common/util.h"
43 #include "common/oscap_pcre.h"
44 #include "oscap_helpers.h"
45
46 /* According to `man 3 pcreapi`, the number passed in ovecsize should always
47 * be a multiple of three.
48 */
49 #define OVECTOR_LEN 30
50
51 const struct oscap_string_map XCCDF_OPERATOR_MAP[] = {
52 {XCCDF_OPERATOR_EQUALS, "equals"},
53 {XCCDF_OPERATOR_NOT_EQUAL, "not equal"},
54 {XCCDF_OPERATOR_GREATER, "greater than"},
55 {XCCDF_OPERATOR_GREATER_EQUAL, "greater than or equal"},
56 {XCCDF_OPERATOR_LESS, "less than"},
57 {XCCDF_OPERATOR_LESS_EQUAL, "less than or equal"},
58 {XCCDF_OPERATOR_PATTERN_MATCH, "pattern match"},
59 {0, NULL}
60 };
61
62 const struct oscap_string_map XCCDF_LEVEL_MAP[] = {
63 {XCCDF_UNKNOWN, "unknown"},
64 {XCCDF_INFO, "info"},
65 {XCCDF_LOW, "low"},
66 {XCCDF_MEDIUM, "medium"},
67 {XCCDF_HIGH, "high"},
68 {XCCDF_LEVEL_NOT_DEFINED, NULL}
69 };
70
71 const struct oscap_string_map XCCDF_BOOLOP_MAP[] = {
72 {XCCDF_OPERATOR_AND, "AND"},
73 {XCCDF_OPERATOR_OR, "OR"},
74 {XCCDF_OPERATOR_AND, "and"},
75 {XCCDF_OPERATOR_OR, "or"},
76 {0, NULL}
77 };
78
79 static const struct oscap_string_map XCCDF_STATUS_MAP[] = {
80 {XCCDF_STATUS_ACCEPTED, "accepted"},
81 {XCCDF_STATUS_DEPRECATED, "deprecated"},
82 {XCCDF_STATUS_DRAFT, "draft"},
83 {XCCDF_STATUS_INCOMPLETE, "incomplete"},
84 {XCCDF_STATUS_INTERIM, "interim"},
85 {XCCDF_STATUS_NOT_SPECIFIED, NULL}
86 };
87
88 static const struct oscap_string_map XCCDF_WARNING_MAP[] = {
89 {XCCDF_WARNING_GENERAL, "general"},
90 {XCCDF_WARNING_FUNCTIONALITY, "functionality"},
91 {XCCDF_WARNING_PERFORMANCE, "performance"},
92 {XCCDF_WARNING_HARDWARE, "hardware"},
93 {XCCDF_WARNING_LEGAL, "legal"},
94 {XCCDF_WARNING_REGULATORY, "regulatory"},
95 {XCCDF_WARNING_MANAGEMENT, "management"},
96 {XCCDF_WARNING_AUDIT, "audit"},
97 {XCCDF_WARNING_DEPENDENCY, "dependency"},
98 {XCCDF_WARNING_NOT_SPECIFIED, NULL}
99 };
100
101 struct xccdf_item *xccdf_item_new(xccdf_type_t type, struct xccdf_item *parent)
102 {
103 struct xccdf_item *item;
104 size_t size = sizeof(*item) - sizeof(item->sub);
105
106 switch (type) {
107 case XCCDF_BENCHMARK:
108 size += sizeof(struct xccdf_benchmark_item);
109 break;
110 case XCCDF_RULE:
111 size += sizeof(struct xccdf_rule_item);
112 break;
113 case XCCDF_GROUP:
114 size += sizeof(struct xccdf_group_item);
115 break;
116 case XCCDF_VALUE:
117 size += sizeof(struct xccdf_value_item);
118 break;
119 case XCCDF_RESULT:
120 size += sizeof(struct xccdf_result_item);
121 break;
122 default:
123 size += sizeof(item->sub);
124 break;
125 }
126
127 item = calloc(1, size);
128 item->type = type;
129 item->item.title = oscap_list_new();
130 item->item.description = oscap_list_new();
131 item->item.question = oscap_list_new();
132 item->item.rationale = oscap_list_new();
133 item->item.statuses = oscap_list_new();
134 item->item.dc_statuses = oscap_list_new();
135 item->item.platforms = oscap_list_new();
136 item->item.warnings = oscap_list_new();
137 item->item.references = oscap_list_new();
138 item->item.weight = 1.0;
139 item->item.flags.selected = true;
140 item->item.parent = parent;
141 item->item.metadata = (struct oscap_list*)oscap_stringlist_new();
142
143 return item;
144 }
145
146 struct xccdf_item *xccdf_item_clone(const struct xccdf_item *old_item)
147 {
148 struct xccdf_item *new_item = calloc(1, sizeof(struct xccdf_item));
149
150 xccdf_item_base_clone(&new_item->item, &(old_item->item));
151 new_item->type = old_item->type;
152
153 switch (new_item->type) {
154 case XCCDF_BENCHMARK:
155 xccdf_benchmark_item_clone(new_item, XBENCHMARK(old_item));
156 break;
157 case XCCDF_RULE:
158 xccdf_rule_item_clone(&new_item->sub.rule, &old_item->sub.rule);
159 break;
160 case XCCDF_GROUP:
161 xccdf_group_item_clone(new_item, &old_item->sub.group);
162 break;
163 case XCCDF_VALUE:
164 xccdf_value_item_clone(&new_item->sub.value, &old_item->sub.value);
165 break;
166 case XCCDF_RESULT:
167 xccdf_result_item_clone(&new_item->sub.result, &old_item->sub.result);
168 break;
169 case XCCDF_PROFILE:
170 xccdf_profile_item_clone(&new_item->sub.profile, &old_item->sub.profile);
171 break;
172 default:
173 //dont initialize the sub item.
174 break;
175 }
176
177 return new_item;
178 }
179
180 /* Performs a deep copy of xccdf_item_base and returns a pointer to that copy */
181 void xccdf_item_base_clone(struct xccdf_item_base *new_base, const struct xccdf_item_base *old_base)
182 {
183 new_base->id = oscap_strdup(old_base->id);
184 new_base->cluster_id = oscap_strdup(old_base->cluster_id);
185 new_base->extends = oscap_strdup(old_base->extends);
186
187 new_base->version = oscap_strdup(old_base->version);
188 new_base->version_update = oscap_strdup(old_base->version_update);
189 new_base->version_time = oscap_strdup(old_base->version_time);
190
191
192 new_base->weight = old_base->weight;
193 new_base->parent = NULL;
194
195 new_base->title = oscap_list_clone(old_base->title, (oscap_clone_func) oscap_text_clone);
196 new_base->description = oscap_list_clone(old_base->description, (oscap_clone_func) oscap_text_clone);
197 new_base->question = oscap_list_clone(old_base->question, (oscap_clone_func) oscap_text_clone);
198 new_base->rationale = oscap_list_clone(old_base->rationale, (oscap_clone_func) oscap_text_clone);
199
200 new_base->warnings = oscap_list_clone(old_base->warnings, (oscap_clone_func) xccdf_warning_clone);
201 new_base->statuses = oscap_list_clone(old_base->statuses, (oscap_clone_func) xccdf_status_clone);
202 new_base->dc_statuses = oscap_list_clone(old_base->dc_statuses, (oscap_clone_func) oscap_reference_clone);
203 new_base->references = oscap_list_clone(old_base->references, (oscap_clone_func) oscap_reference_clone);
204 new_base->platforms = oscap_list_clone(old_base->platforms, (oscap_clone_func) oscap_strdup);
205
206 /* Handling flags */
207 new_base->flags = old_base->flags;
208 new_base->defined_flags = old_base->defined_flags;
209
210 new_base->metadata = (struct oscap_list*)oscap_stringlist_clone((struct oscap_stringlist*)(old_base->metadata));
211 }
212
213 /* Performs a deep copy of xccdf_status and returns a pointer to that copy */
214 struct xccdf_status *xccdf_status_clone(const struct xccdf_status *old_status)
215 {
216 struct xccdf_status *new_status = calloc(1, sizeof(struct xccdf_status));
217 new_status->status = old_status->status;
218 new_status->date = old_status->date;
219 return new_status;
220 }
221
222 /* Performs a deep copy of xccdf_warning and returns a pointer to that copy */
223 struct xccdf_warning *xccdf_warning_clone(const struct xccdf_warning *old_warning)
224 {
225 struct xccdf_warning *new_warning = calloc(1, sizeof(struct xccdf_warning));
226 new_warning->text = oscap_text_clone(old_warning->text);
227 new_warning->category = old_warning->category;
228 return new_warning;
229 }
230
231
232 void xccdf_item_release(struct xccdf_item *item)
233 {
234 if (item) {
235 oscap_list_free(item->item.statuses, (oscap_destruct_func) xccdf_status_free);
236 oscap_list_free(item->item.dc_statuses, (oscap_destruct_func) oscap_reference_free);
237 oscap_list_free(item->item.platforms, free);
238 oscap_list_free(item->item.title, (oscap_destruct_func) oscap_text_free);
239 oscap_list_free(item->item.description, (oscap_destruct_func) oscap_text_free);
240 oscap_list_free(item->item.rationale, (oscap_destruct_func) oscap_text_free);
241 oscap_list_free(item->item.question, (oscap_destruct_func) oscap_text_free);
242 oscap_list_free(item->item.warnings, (oscap_destruct_func) xccdf_warning_free);
243 oscap_list_free(item->item.references, (oscap_destruct_func) oscap_reference_free);
244 if (item->type != XCCDF_BENCHMARK) xccdf_benchmark_unregister_item(item);
245 free(item->item.id);
246 free(item->item.cluster_id);
247 free(item->item.version_time);
248 free(item->item.version_update);
249 free(item->item.version);
250 free(item->item.extends);
251 oscap_stringlist_free((struct oscap_stringlist*)(item->item.metadata));
252
253 free(item);
254 }
255 }
256
257 void xccdf_item_free(struct xccdf_item *item)
258 {
259 if (item == NULL)
260 return;
261 switch (item->type) {
262 case XCCDF_BENCHMARK:
263 xccdf_benchmark_free(XBENCHMARK(item));
264 break;
265 case XCCDF_GROUP:
266 xccdf_group_free(item);
267 break;
268 case XCCDF_RULE:
269 xccdf_rule_free(item);
270 break;
271 case XCCDF_VALUE:
272 xccdf_value_free(item);
273 break;
274 case XCCDF_RESULT:
275 xccdf_result_free(XRESULT(item));
276 break;
277 default:
278 assert((fprintf(stderr, "Deletion of item of type no. %u is not yet supported.", item->type), false));
279 }
280 }
281
282 void xccdf_item_dump(struct xccdf_item *item, int depth)
283 {
284 if (item == NULL)
285 return;
286 switch (item->type) {
287 case XCCDF_BENCHMARK:
288 xccdf_benchmark_dump(XBENCHMARK(item));
289 break;
290 case XCCDF_GROUP:
291 xccdf_group_dump(item, depth);
292 break;
293 case XCCDF_RULE:
294 xccdf_rule_dump(item, depth);
295 break;
296 default:
297 xccdf_print_depth(depth);
298 fprintf(stderr, "I cannot yet dump an item of type no. %u.", item->type);
299 }
300 }
301
302 void xccdf_item_print(struct xccdf_item *item, int depth)
303 {
304 if (item) {
305 if (item->item.parent) {
306 xccdf_print_depth(depth);
307 printf("parent : %s\n", item->item.parent->item.id);
308 }
309 if (item->item.extends) {
310 xccdf_print_depth(depth);
311 printf("extends : %s\n", item->item.extends);
312 }
313 if (item->type == XCCDF_BENCHMARK) {
314 xccdf_print_depth(depth);
315 printf("resolved: %d\n", item->item.flags.resolved);
316 }
317 if (item->type & XCCDF_CONTENT) {
318 xccdf_print_depth(depth);
319 printf("selected: %d\n", item->item.flags.selected);
320 }
321 if (item->item.version) {
322 xccdf_print_depth(depth);
323 printf("version : %s\n", item->item.version);
324 }
325 xccdf_print_depth(depth);
326 printf("title : ");
327 xccdf_print_textlist(oscap_iterator_new(item->item.title), depth + 1, 70, "...");
328 xccdf_print_depth(depth);
329 printf("desc : ");
330 xccdf_print_textlist(oscap_iterator_new(item->item.description), depth + 1, 70, "...");
331 xccdf_print_depth(depth);
332 printf("platforms ");
333 oscap_list_dump(item->item.platforms, xccdf_cstring_dump, depth + 1);
334 xccdf_print_depth(depth);
335 printf("status (cur = ");
336 const struct xccdf_status *status = xccdf_item_get_current_status(item);
337 if (status != NULL)
338 printf("%d)", xccdf_status_get_status(status));
339 else
340 printf("(NULL))");
341 oscap_list_dump(item->item.statuses, xccdf_status_dump, depth + 1);
342 }
343 }
344
345 void xccdf_texts_to_dom(struct oscap_text_iterator *texts, xmlNode *parent, const char *elname)
346 {
347 OSCAP_FOR(oscap_text, text, texts)
348 oscap_text_to_dom(text, parent, elname);
349 }
350
351 xmlNode *xccdf_item_to_dom(struct xccdf_item *item, xmlDoc *doc, xmlNode *parent, const struct xccdf_version_info *version_info)
352 {
353 /*
354 We have 2 special cases here, either we have a parent node or we don't.
355 In case we have a parent node we can look for XCCDF namespace or create
356 it there. But if we don't we have no node to create the namespace in!
357 In this case we create a node with no namespace, then create the namespace
358 in it and only then we set the node to the new namespace. This avoids
359 undefined / undocumented behavior that we relied on previously.
360 */
361
362 xmlNs *ns_xccdf = NULL;
363 xmlNode *item_node = NULL;
364 if (parent == NULL) {
365 item_node = xmlNewNode(NULL, BAD_CAST "Item");
366 // this creates the namespace and item_node carries it
367 ns_xccdf = lookup_xccdf_ns(doc, item_node, version_info);
368 xmlSetNs(item_node, ns_xccdf);
369 }
370 else {
371 ns_xccdf = lookup_xccdf_ns(doc, parent, version_info);
372 item_node = xmlNewTextChild(parent, ns_xccdf, BAD_CAST "Item", NULL);
373 }
374
375 /* Handle generic item attributes */
376 const char *id = xccdf_item_get_id(item);
377 xmlNewProp(item_node, BAD_CAST "id", BAD_CAST id);
378
379 const char *cluster_id = xccdf_item_get_cluster_id(item);
380 if (cluster_id)
381 xmlNewProp(item_node, BAD_CAST "cluster-id", BAD_CAST cluster_id);
382
383 if (item->item.defined_flags.hidden) {
384 if (xccdf_item_get_hidden(item))
385 xmlNewProp(item_node, BAD_CAST "hidden", BAD_CAST "true");
386 else
387 xmlNewProp(item_node, BAD_CAST "hidden", BAD_CAST "false");
388 }
389
390 if (xccdf_item_get_prohibit_changes(item))
391 xmlNewProp(item_node, BAD_CAST "prohibitChanges", BAD_CAST "true");
392
393 if (xccdf_item_get_abstract(item))
394 xmlNewProp(item_node, BAD_CAST "abstract", BAD_CAST "true");
395
396 struct xccdf_status_iterator *statuses = xccdf_item_get_statuses(item);
397 while (xccdf_status_iterator_has_more(statuses)) {
398 struct xccdf_status *status = xccdf_status_iterator_next(statuses);
399 xccdf_status_to_dom(status, doc, item_node, version_info);
400 }
401 xccdf_status_iterator_free(statuses);
402
403 struct oscap_reference_iterator *dc_statuses = xccdf_item_get_dc_statuses(item);
404 while (oscap_reference_iterator_has_more(dc_statuses)) {
405 struct oscap_reference *ref = oscap_reference_iterator_next(dc_statuses);
406 oscap_reference_to_dom(ref, item_node, doc, "dc-status");
407 }
408 oscap_reference_iterator_free(dc_statuses);
409
410 /* version and attributes */
411 const char *version = xccdf_item_get_version(item);
412 if ((xccdf_item_get_type(item) != XCCDF_BENCHMARK && xccdf_item_get_type(item) != XCCDF_RESULT) && version) {
413 xmlNode* version_node = xmlNewTextChild(item_node, ns_xccdf, BAD_CAST "version", BAD_CAST version);
414
415 const char *version_update = xccdf_item_get_version_update(item);
416 if (version_update)
417 xmlNewProp(version_node, BAD_CAST "update", BAD_CAST version_update);
418
419 const char *version_time = xccdf_item_get_version_time(item);
420 if (version_time)
421 xmlNewProp(version_node, BAD_CAST "time", BAD_CAST version_time);
422 }
423
424 /* Handle generic item child nodes */
425 xccdf_texts_to_dom(xccdf_item_get_title(item), item_node, "title");
426 xccdf_texts_to_dom(xccdf_item_get_description(item), item_node, "description");
427
428 /* Handle some type specific children */
429 if (xccdf_item_get_type(item)==XCCDF_BENCHMARK) {
430 struct xccdf_benchmark *benchmark = XBENCHMARK(item);
431
432 struct xccdf_notice_iterator *notices = xccdf_benchmark_get_notices(benchmark);
433 while (xccdf_notice_iterator_has_more(notices)) {
434 struct xccdf_notice *notice = xccdf_notice_iterator_next(notices);
435 xmlNode *notice_node = oscap_text_to_dom(xccdf_notice_get_text(notice), item_node, "notice");
436
437 const char * notice_id = xccdf_notice_get_id(notice);
438 if (notice_id)
439 xmlNewProp(notice_node, BAD_CAST "id", BAD_CAST notice_id);
440 }
441 xccdf_notice_iterator_free(notices);
442
443 xccdf_texts_to_dom(xccdf_benchmark_get_front_matter(benchmark), item_node, "front-matter");
444 xccdf_texts_to_dom(xccdf_benchmark_get_rear_matter(benchmark), item_node, "rear-matter");
445 }
446
447 /* Handle generic item child nodes */
448 struct xccdf_warning_iterator *warnings = xccdf_item_get_warnings(item);
449 while (xccdf_warning_iterator_has_more(warnings)) {
450 struct xccdf_warning *warning = xccdf_warning_iterator_next(warnings);
451 xccdf_warning_to_dom(warning, doc, item_node);
452 }
453 xccdf_warning_iterator_free(warnings);
454
455 xccdf_texts_to_dom(xccdf_item_get_question(item), item_node, "question");
456
457 struct oscap_reference_iterator *references = xccdf_item_get_references(item);
458 while (oscap_reference_iterator_has_more(references)) {
459 struct oscap_reference *ref = oscap_reference_iterator_next(references);
460 oscap_reference_to_dom(ref, item_node, doc, "reference");
461 }
462 oscap_reference_iterator_free(references);
463
464 if (xccdf_item_get_type(item)!=XCCDF_BENCHMARK) {
465 struct oscap_string_iterator* metadata = xccdf_item_get_metadata(item);
466 while (oscap_string_iterator_has_more(metadata))
467 {
468 const char* meta = oscap_string_iterator_next(metadata);
469 xmlNode *m = oscap_xmlstr_to_dom(item_node, "metadata", meta);
470 xmlSetNs(m, ns_xccdf);
471 }
472 oscap_string_iterator_free(metadata);
473 }
474
475 /* Handle type specific attributes and children */
476 switch (xccdf_item_get_type(item)) {
477 case XCCDF_RULE:
478 xmlNodeSetName(item_node,BAD_CAST "Rule");
479 xccdf_rule_to_dom(XRULE(item), item_node, doc, parent);
480 break;
481 case XCCDF_BENCHMARK:
482 xmlNodeSetName(item_node,BAD_CAST "Benchmark");
483 break;
484 case XCCDF_PROFILE:
485 xmlNodeSetName(item_node,BAD_CAST "Profile");
486 xccdf_profile_to_dom(XPROFILE(item), item_node, doc, parent, version_info);
487 break;
488 case XCCDF_RESULT:
489 xmlNodeSetName(item_node,BAD_CAST "TestResult");
490 if (parent) xccdf_result_to_dom(XRESULT(item), item_node, doc, parent, false);
491 break;
492 case XCCDF_GROUP:
493 xmlNodeSetName(item_node,BAD_CAST "Group");
494 xccdf_group_to_dom(XGROUP(item), item_node, doc, parent);
495 break;
496 case XCCDF_VALUE:
497 xmlNodeSetName(item_node,BAD_CAST "Value");
498 xccdf_value_to_dom(XVALUE(item), item_node, doc, parent);
499 break;
500 case XCCDF_CONTENT:
501 xmlNodeSetName(item_node,BAD_CAST "Content");
502 break;
503 case XCCDF_OBJECT:
504 xmlNodeSetName(item_node,BAD_CAST "Object");
505 break;
506 default:
507 return item_node;
508 }
509
510 return item_node;
511 }
512
513 xmlNode *xccdf_profile_note_to_dom(struct xccdf_profile_note *note, xmlDoc *doc, xmlNode *parent)
514 {
515 xmlNode *note_node = oscap_text_to_dom(xccdf_profile_note_get_text(note), parent, "profile-note");
516 xmlNewProp(note_node, BAD_CAST "tag", BAD_CAST xccdf_profile_note_get_reftag(note));
517
518 return note_node;
519 }
520
521 xmlNode *xccdf_warning_to_dom(struct xccdf_warning *warning, xmlDoc *doc, xmlNode *parent)
522 {
523 xmlNode *warning_node = NULL;
524 xccdf_warning_category_t category = xccdf_warning_get_category(warning);
525
526 warning_node = oscap_text_to_dom(xccdf_warning_get_text(warning), parent, "warning");
527 if (category != XCCDF_WARNING_NOT_SPECIFIED)
528 xmlNewProp(warning_node, BAD_CAST "category", BAD_CAST XCCDF_WARNING_MAP[category - 1].string);
529
530 return warning_node;
531 }
532
533 xmlNode *xccdf_status_to_dom(struct xccdf_status *status, xmlDoc *doc, xmlNode *parent, const struct xccdf_version_info* version_info)
534 {
535 xmlNs *ns_xccdf = lookup_xccdf_ns(doc, parent, version_info);
536
537 xmlNode *status_node = NULL;
538 xccdf_status_type_t type = xccdf_status_get_status(status);
539 if (type != XCCDF_STATUS_NOT_SPECIFIED)
540 status_node = xmlNewTextChild(parent, ns_xccdf, BAD_CAST "status",
541 BAD_CAST XCCDF_STATUS_MAP[type - 1].string);
542
543 time_t date_time = xccdf_status_get_date(status);
544 if (date_time) {
545 struct tm result;
546 struct tm *date = localtime_r(&date_time, &result);
547 /* "YYYY-DD-MM" */
548 int date_len = snprintf(NULL, 0, "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday);
549 if (date_len < 0) {
550 return status_node;
551 }
552 date_len++; // +1 for terminating '\0'
553 char *date_str = malloc(date_len);
554 snprintf(date_str, date_len, "%04d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday);
555 xmlNewProp(status_node, BAD_CAST "date", BAD_CAST date_str);
556 free(date_str);
557 }
558
559 return status_node;
560 }
561
562 xmlNode *xccdf_fixtext_to_dom(struct xccdf_fixtext *fixtext, xmlDoc *doc, xmlNode *parent)
563 {
564 xmlNode *fixtext_node = oscap_text_to_dom(xccdf_fixtext_get_text(fixtext), parent, "fixtext");
565
566 if (xccdf_fixtext_get_reboot(fixtext))
567 xmlNewProp(fixtext_node, BAD_CAST "reboot", BAD_CAST "true");
568
569 const char *fixref = xccdf_fixtext_get_fixref(fixtext);
570 if (fixref)
571 xmlNewProp(fixtext_node, BAD_CAST "fixref", BAD_CAST fixref);
572
573 xccdf_level_t complexity = xccdf_fixtext_get_complexity(fixtext);
574 if (complexity != XCCDF_LEVEL_NOT_DEFINED)
575 xmlNewProp(fixtext_node, BAD_CAST "complexity", BAD_CAST XCCDF_LEVEL_MAP[complexity-1].string);
576
577 xccdf_level_t disruption = xccdf_fixtext_get_disruption(fixtext);
578 if (disruption != XCCDF_LEVEL_NOT_DEFINED)
579 xmlNewProp(fixtext_node, BAD_CAST "disruption", BAD_CAST XCCDF_LEVEL_MAP[disruption-1].string);
580
581 xccdf_strategy_t strategy = xccdf_fixtext_get_strategy(fixtext);
582 if (strategy != XCCDF_STRATEGY_UNKNOWN)
583 xmlNewProp(fixtext_node, BAD_CAST "strategy", BAD_CAST XCCDF_STRATEGY_MAP[strategy].string);
584
585 return fixtext_node;
586 }
587
588 xmlNode *xccdf_fix_to_dom(struct xccdf_fix *fix, xmlDoc *doc, xmlNode *parent, const struct xccdf_version_info* version_info)
589 {
590 xmlNode *fix_node = oscap_xmlstr_to_dom(parent, "fix", xccdf_fix_get_content(fix));
591 xmlNs *ns_xccdf = lookup_xccdf_ns(doc, parent, version_info);
592 xmlSetNs(fix_node, ns_xccdf);
593
594 const char *id = xccdf_fix_get_id(fix);
595 if (id != NULL) xmlNewProp(fix_node, BAD_CAST "id", BAD_CAST id);
596
597 const char *sys = xccdf_fix_get_system(fix);
598 if (sys != NULL) xmlNewProp(fix_node, BAD_CAST "system", BAD_CAST sys);
599
600 const char *platform = xccdf_fix_get_platform(fix);
601 if (platform != NULL) xmlNewProp(fix_node, BAD_CAST "platform", BAD_CAST platform);
602
603 if (xccdf_fix_get_reboot(fix))
604 xmlNewProp(fix_node, BAD_CAST "reboot", BAD_CAST "true");
605
606 xccdf_level_t complexity = xccdf_fix_get_complexity(fix);
607 if (complexity != XCCDF_LEVEL_NOT_DEFINED)
608 xmlNewProp(fix_node, BAD_CAST "complexity", BAD_CAST XCCDF_LEVEL_MAP[complexity-1].string);
609
610 xccdf_level_t disruption = xccdf_fix_get_disruption(fix);
611 if (disruption != XCCDF_LEVEL_NOT_DEFINED)
612 xmlNewProp(fix_node, BAD_CAST "disruption", BAD_CAST XCCDF_LEVEL_MAP[disruption-1].string);
613
614 xccdf_strategy_t strategy = xccdf_fix_get_strategy(fix);
615 if (strategy != XCCDF_STRATEGY_UNKNOWN)
616 xmlNewProp(fix_node, BAD_CAST "strategy", BAD_CAST XCCDF_STRATEGY_MAP[strategy-1].string);
617
618 // Sub element is used to store XCCDF value substitutions, not a content
619 //xmlNewTextChild(fix_node, ns_xccdf, BAD_CAST "sub", BAD_CAST content);
620
621 // This is in the XCCDF Spec, but not implemented in OpenSCAP
622 //const char *instance = xccdf_fix_get_instance(fix);
623 //xmlNewTextChild(fix_node, ns_xccdf, BAD_CAST "instance", BAD_CAST instance);
624
625 return fix_node;
626 }
627
628 xmlNode *xccdf_ident_to_dom(struct xccdf_ident *ident, xmlDoc *doc, xmlNode *parent, const struct xccdf_version_info* version_info)
629 {
630 xmlNs *ns_xccdf = lookup_xccdf_ns(doc, parent, version_info);
631
632 const char *id = xccdf_ident_get_id(ident);
633 xmlNode *ident_node = xmlNewTextChild(parent, ns_xccdf, BAD_CAST "ident", BAD_CAST id);
634
635 const char *sys = xccdf_ident_get_system(ident);
636 xmlNewProp(ident_node, BAD_CAST "system", BAD_CAST sys);
637
638 return ident_node;
639 }
640
641 xmlNode *xccdf_check_to_dom(struct xccdf_check *check, xmlDoc *doc, xmlNode *parent, const struct xccdf_version_info* version_info)
642 {
643 xmlNs *ns_xccdf = lookup_xccdf_ns(doc, parent, version_info);
644
645 xmlNode *check_node = NULL;
646 if (xccdf_check_get_complex(check))
647 check_node = xmlNewTextChild(parent, ns_xccdf, BAD_CAST "complex-check", NULL);
648 else {
649 check_node = xmlNewTextChild(parent, ns_xccdf, BAD_CAST "check", NULL);
650 const char *sys = xccdf_check_get_system(check);
651 xmlNewProp(check_node, BAD_CAST "system", BAD_CAST sys);
652 }
653
654
655 const char *id = xccdf_check_get_id(check);
656 if (id)
657 xmlNewProp(check_node, BAD_CAST "id", BAD_CAST id);
658
659 const char *selector = xccdf_check_get_selector(check);
660 if (selector)
661 xmlNewProp(check_node, BAD_CAST "selector", BAD_CAST selector);
662 if (!xccdf_check_get_complex(check) && (check->flags.def_multicheck || xccdf_check_get_multicheck(check))) {
663 xmlNewProp(check_node, BAD_CAST "multi-check",
664 BAD_CAST (xccdf_check_get_multicheck(check) ? "true" : "false"));
665 }
666 if (check->flags.def_negate || xccdf_check_get_negate(check))
667 xmlNewProp(check_node, BAD_CAST "negate",
668 BAD_CAST (xccdf_check_get_negate(check) ? "true" : "false"));
669 if (xccdf_check_get_complex(check))
670 xmlNewProp(check_node, BAD_CAST "operator",
671 BAD_CAST oscap_enum_to_string(XCCDF_BOOLOP_MAP, xccdf_check_get_oper(check)));
672
673 /* Handle complex checks */
674 struct xccdf_check_iterator *checks = xccdf_check_get_children(check);
675 while (xccdf_check_iterator_has_more(checks)) {
676 struct xccdf_check *new_check = xccdf_check_iterator_next(checks);
677 xccdf_check_to_dom(new_check, doc, check_node, version_info);
678 }
679 xccdf_check_iterator_free(checks);
680
681 struct xccdf_check_import_iterator *imports = xccdf_check_get_imports(check);
682 while (xccdf_check_import_iterator_has_more(imports)) {
683 struct xccdf_check_import *import = xccdf_check_import_iterator_next(imports);
684 const char *name = xccdf_check_import_get_name(import);
685 const char *xpath = xccdf_check_import_get_xpath(import);
686 const char *content = xccdf_check_import_get_content(import);
687 xmlNode *import_node = xmlNewChild(check_node, ns_xccdf, BAD_CAST "check-import", BAD_CAST content);
688 xmlNewProp(import_node, BAD_CAST "import-name", BAD_CAST name);
689 if (xpath)
690 xmlNewProp(import_node, BAD_CAST "import-xpath", BAD_CAST xpath);
691 }
692 xccdf_check_import_iterator_free(imports);
693
694 struct xccdf_check_export_iterator *exports = xccdf_check_get_exports(check);
695 while (xccdf_check_export_iterator_has_more(exports)) {
696 struct xccdf_check_export *export = xccdf_check_export_iterator_next(exports);
697 const char *name = xccdf_check_export_get_name(export);
698 const char *value= xccdf_check_export_get_value(export);
699 xmlNode *export_node = xmlNewTextChild(check_node, ns_xccdf, BAD_CAST "check-export", NULL);
700 xmlNewProp(export_node, BAD_CAST "export-name", BAD_CAST name);
701 xmlNewProp(export_node, BAD_CAST "value-id", BAD_CAST value);
702 }
703 xccdf_check_export_iterator_free(exports);
704
705 struct xccdf_check_content_ref_iterator *refs = xccdf_check_get_content_refs(check);
706 while (xccdf_check_content_ref_iterator_has_more(refs)) {
707 struct xccdf_check_content_ref *ref = xccdf_check_content_ref_iterator_next(refs);
708 xmlNode *ref_node = xmlNewTextChild(check_node, ns_xccdf, BAD_CAST "check-content-ref", NULL);
709
710 const char *name = xccdf_check_content_ref_get_name(ref);
711 if (name != NULL)
712 xmlNewProp(ref_node, BAD_CAST "name", BAD_CAST name);
713
714 const char *href = xccdf_check_content_ref_get_href(ref);
715 xmlNewProp(ref_node, BAD_CAST "href", BAD_CAST href);
716 }
717 xccdf_check_content_ref_iterator_free(refs);
718
719 const char *content = xccdf_check_get_content(check);
720 if (content) {
721 oscap_xmlstr_to_dom(check_node, "check-content", content);
722 }
723
724 return check_node;
725 }
726
727 #define XCCDF_ITEM_PROCESS_FLAG(reader,flag,attr) \
728 if (xccdf_attribute_has((reader), (attr))) { \
729 item->item.flags.flag = xccdf_attribute_get_bool((reader), (attr)); \
730 item->item.defined_flags.flag = true; }
731
732 bool xccdf_item_process_attributes(struct xccdf_item *item, xmlTextReaderPtr reader)
733 {
734 item->item.id = xccdf_attribute_copy(reader, XCCDFA_ID);
735
736 XCCDF_ITEM_PROCESS_FLAG(reader, resolved, XCCDFA_RESOLVED);
737 XCCDF_ITEM_PROCESS_FLAG(reader, hidden, XCCDFA_HIDDEN);
738 XCCDF_ITEM_PROCESS_FLAG(reader, selected, XCCDFA_SELECTED);
739 XCCDF_ITEM_PROCESS_FLAG(reader, prohibit_changes, XCCDFA_PROHIBITCHANGES);
740 XCCDF_ITEM_PROCESS_FLAG(reader, multiple, XCCDFA_MULTIPLE);
741 XCCDF_ITEM_PROCESS_FLAG(reader, abstract, XCCDFA_ABSTRACT);
742 XCCDF_ITEM_PROCESS_FLAG(reader, interactive, XCCDFA_INTERACTIVE);
743
744 if (xccdf_attribute_has(reader, XCCDFA_WEIGHT)) {
745 item->item.weight = xccdf_attribute_get_float(reader, XCCDFA_WEIGHT);
746 item->item.defined_flags.weight = true;
747 }
748 if (xccdf_attribute_has(reader, XCCDFA_EXTENDS))
749 item->item.extends = xccdf_attribute_copy(reader, XCCDFA_EXTENDS);
750 item->item.cluster_id = xccdf_attribute_copy(reader, XCCDFA_CLUSTER_ID);
751
752 if (item->item.id) {
753 struct xccdf_item *bench = XITEM(xccdf_item_get_benchmark_internal(item));
754 if (bench != NULL && bench != item)
755 xccdf_benchmark_register_item(xccdf_item_get_benchmark(item), item);
756 }
757 return item->item.id != NULL;
758 }
759
760 void xccdf_item_add_applicable_platform(struct xccdf_item *item, xmlTextReaderPtr reader)
761 {
762 char *platform_idref = xccdf_attribute_copy(reader, XCCDFA_IDREF);
763
764 /* Official Windows 7 CPE according to National Vulnerability Database
765 * CPE Dictionary as of 2018-08-29 is 'cpe:/o:microsoft:windows_7'.
766 * However, content exported from Microsoft Security Compliance Manager
767 * as of version 4.0.0.1 in CAB archive using 'Export in SCAP 1.0' is
768 * 'cpe:/o:microsoft:windows7'. If this pattern is matched, we will add
769 * an underscore to workaround the situation that this XCCDF benchmark is
770 * not applicable.
771 */
772 char *pcreerror = NULL;
773 int erroffset = 0;
774 oscap_pcre_t *regex = oscap_pcre_compile("^(cpe:/o:microsoft:windows)(7.*)", 0, &pcreerror, &erroffset);
775 if (regex == NULL) {
776 free(platform_idref);
777 oscap_pcre_err_free(pcreerror);
778 return;
779 }
780 int ovector[OVECTOR_LEN];
781 int rc = oscap_pcre_exec(regex, platform_idref, strlen(platform_idref), 0, 0, ovector, OVECTOR_LEN);
782 oscap_pcre_free(regex);
783 /* 1 pattern + 2 groups = 3 */
784 if (rc == 3) {
785 const int first_group_start = ovector[2];
786 const int first_group_end = ovector[3];
787 size_t first_group_len = first_group_end - first_group_start;
788 char *first_group = malloc(first_group_len + 1); // + 1 for '\0'
789 strncpy(first_group, platform_idref + first_group_start, first_group_len);
790 first_group[first_group_len] = '\0';
791 const int second_group_start = ovector[4];
792 const int second_group_end = ovector[5];
793 size_t second_group_len = second_group_end - second_group_start;
794 char *second_group = malloc(second_group_len + 1); // + 1 for '\0'
795 strncpy(second_group, platform_idref + second_group_start, second_group_len);
796 second_group[second_group_len] = '\0';
797 char *alternate_platform_idref = oscap_sprintf("%s_%s", first_group, second_group);
798 free(first_group);
799 free(second_group);
800 oscap_list_add(item->item.platforms, alternate_platform_idref);
801 }
802
803 oscap_list_add(item->item.platforms, platform_idref);
804 }
805
806 bool xccdf_item_process_element(struct xccdf_item * item, xmlTextReaderPtr reader)
807 {
808 xccdf_element_t el = xccdf_element_get(reader);
809
810 switch (el) {
811 case XCCDFE_TITLE:
812 oscap_list_add(item->item.title, oscap_text_new_parse(XCCDF_TEXT_PLAINSUB, reader));
813 return true;
814 case XCCDFE_DESCRIPTION:
815 oscap_list_add(item->item.description, oscap_text_new_parse(XCCDF_TEXT_HTMLSUB, reader));
816 return true;
817 case XCCDFE_WARNING:
818 oscap_list_add(item->item.warnings, xccdf_warning_new_parse(reader));
819 return true;
820 case XCCDFE_REFERENCE:
821 oscap_list_add(item->item.references, oscap_reference_new_parse(reader));
822 return true;
823 case XCCDFE_STATUS:{
824 const char *date = xccdf_attribute_get(reader, XCCDFA_DATE);
825 char *str = oscap_element_string_copy(reader);
826 struct xccdf_status *status = xccdf_status_new_fill(str, date);
827 free(str);
828 if (status) {
829 oscap_list_add(item->item.statuses, status);
830 return true;
831 }
832 break;
833 }
834 case XCCDFE_DC_STATUS:
835 oscap_list_add(item->item.dc_statuses, oscap_reference_new_parse(reader));
836 return true;
837 case XCCDFE_VERSION: {
838 xmlNode *ver = xmlTextReaderExpand(reader);
839 /* optional attributes */
840 item->item.version_time = (char*) xmlGetProp(ver, BAD_CAST "time");
841 item->item.version_update = (char*) xmlGetProp(ver, BAD_CAST "update");
842 /* content */
843 item->item.version = (char *) xmlNodeGetContent(ver);
844 if (oscap_streq(item->item.version, "")) {
845 free(item->item.version);
846 item->item.version = NULL;
847 }
848 return true;
849 }
850 case XCCDFE_RATIONALE:
851 oscap_list_add(item->item.rationale, oscap_text_new_parse(XCCDF_TEXT_HTMLSUB, reader));
852 return true;
853 case XCCDFE_PLATFORM:
854 xccdf_item_add_applicable_platform(item, reader);
855 return true;
856 case XCCDFE_QUESTION:
857 oscap_list_add(item->item.question, oscap_text_new_parse(XCCDF_TEXT_PLAIN, reader));
858 return true;
859 case XCCDFE_METADATA: {
860 char* xml = oscap_get_xml(reader);
861 xccdf_item_add_metadata(item, xml);
862 free(xml);
863 return true;
864 }
865 default:
866 break;
867 }
868 return false;
869 }
870
871 inline struct xccdf_item* xccdf_item_get_benchmark_internal(struct xccdf_item* item)
872 {
873 if (item == NULL) return NULL;
874 while (xccdf_item_get_parent(item) != NULL)
875 item = xccdf_item_get_parent(item);
876 return (xccdf_item_get_type(item) == XCCDF_BENCHMARK ? item : NULL);
877 }
878
879 #define XCCDF_BENCHGETTER(TYPE) \
880 struct xccdf_benchmark* xccdf_##TYPE##_get_benchmark(const struct xccdf_##TYPE* item) \
881 { return XBENCHMARK(xccdf_item_get_benchmark_internal(XITEM(item))); }
882 XCCDF_BENCHGETTER(item) XCCDF_BENCHGETTER(profile) XCCDF_BENCHGETTER(rule)
883 XCCDF_BENCHGETTER(group) XCCDF_BENCHGETTER(value) XCCDF_BENCHGETTER(result)
884 #undef XCCDF_BENCHGETTER
885
886 const struct xccdf_version_info* xccdf_item_get_schema_version(struct xccdf_item* item)
887 {
888 struct xccdf_benchmark* top_benchmark = xccdf_item_get_benchmark(item);
889 if (top_benchmark == NULL) {
890 if (xccdf_item_get_type(item) == XCCDF_RESULT) {
891 // TestResult is special item, it may not have parent benchmark
892 return xccdf_result_get_schema_version(XRESULT(item));
893 } else {
894 return NULL;
895 }
896 }
897 return xccdf_benchmark_get_schema_version(top_benchmark);
898 }
899
900 static void *xccdf_item_convert(struct xccdf_item *item, xccdf_type_t type)
901 {
902 return ((item != NULL && (item->type & type)) ? item : NULL);
903 }
904
905 #define XCCDF_ITEM_CONVERT(T1,T2) struct xccdf_##T1* xccdf_item_to_##T1(struct xccdf_item* item) { return xccdf_item_convert(item, XCCDF_##T2); }
906 XCCDF_ITEM_CONVERT(benchmark, BENCHMARK)
907 XCCDF_ITEM_CONVERT(profile, PROFILE)
908 XCCDF_ITEM_CONVERT(rule, RULE)
909 XCCDF_ITEM_CONVERT(group, GROUP)
910 XCCDF_ITEM_CONVERT(value, VALUE)
911 XCCDF_ITEM_CONVERT(result, RESULT)
912 #undef XCCDF_ITEM_CONVERT
913
914 #define XCCDF_ITEM_UPCAST(T) struct xccdf_item *xccdf_##T##_to_item(struct xccdf_##T *item) { return XITEM(item); }
915 XCCDF_ITEM_UPCAST(benchmark) XCCDF_ITEM_UPCAST(profile) XCCDF_ITEM_UPCAST(rule)
916 XCCDF_ITEM_UPCAST(group) XCCDF_ITEM_UPCAST(value) XCCDF_ITEM_UPCAST(result)
917 #undef XCCDF_ITEM_UPCAST
918
919 XCCDF_ABSTRACT_GETTER(xccdf_type_t, item, type, type)
920 XCCDF_ITEM_GETTER(const char *, id)
921
922 XCCDF_ITEM_TIGETTER(question);
923 XCCDF_ITEM_TIGETTER(rationale);
924 XCCDF_ITEM_TIGETTER(title);
925 XCCDF_ITEM_TIGETTER(description);
926 XCCDF_ITEM_ADDER(struct oscap_text *, question, question)
927 XCCDF_ITEM_ADDER(struct oscap_text *, title, title)
928 XCCDF_ITEM_ADDER(struct oscap_text *, description, description)
929 XCCDF_ITEM_ADDER(struct oscap_text *, rationale, rationale)
930
931 XCCDF_ITEM_GETTER(const char *, version)
932 XCCDF_ITEM_GETTER(const char *, cluster_id)
933 XCCDF_ITEM_GETTER(const char *, version_update)
934 XCCDF_ITEM_GETTER(const char *, version_time)
935 XCCDF_ITEM_GETTER(float, weight)
936 XCCDF_ITEM_GETTER(struct xccdf_item *, parent)
937 XCCDF_ITEM_GETTER(const char *, extends)
938 XCCDF_FLAG_GETTER(resolved)
939 XCCDF_FLAG_GETTER(hidden)
940 XCCDF_FLAG_GETTER(selected)
941 XCCDF_ITEM_SIGETTER(metadata);
942 XCCDF_ITEM_ADDER_ONE(item, metadata, metadata, const char*, oscap_strdup);
943 XCCDF_ITEM_ADDER_ONE(benchmark, metadata, metadata, const char*, oscap_strdup);
944 XCCDF_ITEM_ADDER_ONE(profile, metadata, metadata, const char*, oscap_strdup);
945 XCCDF_ITEM_ADDER_ONE(group, metadata, metadata, const char*, oscap_strdup);
946 XCCDF_ITEM_ADDER_ONE(rule, metadata, metadata, const char*, oscap_strdup);
947 XCCDF_ITEM_ADDER_ONE(value, metadata, metadata, const char*, oscap_strdup);
948 XCCDF_ITEM_ADDER_ONE(result, metadata, metadata, const char*, oscap_strdup);
949 XCCDF_FLAG_GETTER(multiple)
950 XCCDF_FLAG_GETTER(prohibit_changes)
951 XCCDF_FLAG_GETTER(abstract)
952 XCCDF_FLAG_GETTER(interactive)
953 XCCDF_ITEM_SIGETTER(platforms)
954 XCCDF_ITEM_ADDER_STRING(platform, platforms)
955 XCCDF_ITEM_IGETTER(warning, warnings)
956 XCCDF_ITEM_IGETTER(status, statuses)
957 XCCDF_ITEM_ADDER(struct oscap_reference *, reference, references)
958 XCCDF_ITEM_ADDER(struct xccdf_warning *, warning, warnings)
959 XCCDF_ITEM_ADDER(struct xccdf_status *, status, statuses)
960 XCCDF_ITEM_ADDER(struct oscap_reference *, dc_status, dc_statuses)
961 XCCDF_ITERATOR_GEN_S(item) XCCDF_ITERATOR_GEN_S(status)
962 OSCAP_ITERATOR_GEN(xccdf_warning)
963 OSCAP_ITERATOR_REMOVE_F(xccdf_warning)
964
965 XCCDF_ITEM_SETTER_SIMPLE(xccdf_numeric, weight)
966 XCCDF_ITEM_SETTER_STRING(version_time)
967 XCCDF_ITEM_SETTER_STRING(version)
968 XCCDF_ITEM_SETTER_STRING(version_update)
969 XCCDF_ITEM_SETTER_STRING(extends)
970 XCCDF_ITEM_SETTER_STRING(cluster_id)
971
972 #define XCCDF_SETTER_ID(T) bool xccdf_##T##_set_id(struct xccdf_##T *item, const char *newval) \
973 { return xccdf_benchmark_rename_item(XITEM(item), newval); }
974 XCCDF_SETTER_ID(item) XCCDF_SETTER_ID(benchmark) XCCDF_SETTER_ID(profile)
975 XCCDF_SETTER_ID(rule) XCCDF_SETTER_ID(group) XCCDF_SETTER_ID(value) XCCDF_SETTER_ID(result)
976 #undef XCCDF_SETTER_ID
977
978 struct oscap_reference_iterator *xccdf_item_get_references(const struct xccdf_item *item) { return oscap_iterator_new(item->item.references); }
979 struct oscap_reference_iterator *xccdf_item_get_dc_statuses(const struct xccdf_item *item) { return oscap_iterator_new(item->item.dc_statuses); }
980 struct oscap_reference_iterator *xccdf_benchmark_get_references(const struct xccdf_benchmark *item) { return oscap_iterator_new(XITEM(item)->item.references); }
981 struct oscap_reference_iterator *xccdf_benchmark_get_dc_statuses(const struct xccdf_benchmark *item) { return oscap_iterator_new(XITEM(item)->item.dc_statuses); }
982 struct oscap_reference_iterator *xccdf_value_get_references(const struct xccdf_value *item) { return oscap_iterator_new(XITEM(item)->item.references); }
983 struct oscap_reference_iterator *xccdf_value_get_dc_statuses(const struct xccdf_value *item) { return oscap_iterator_new(XITEM(item)->item.dc_statuses); }
984 struct oscap_reference_iterator *xccdf_group_get_references(const struct xccdf_group *item) { return oscap_iterator_new(XITEM(item)->item.references); }
985 struct oscap_reference_iterator *xccdf_group_get_dc_statuses(const struct xccdf_group *item) { return oscap_iterator_new(XITEM(item)->item.dc_statuses); }
986 struct oscap_reference_iterator *xccdf_rule_get_references(const struct xccdf_rule *item) { return oscap_iterator_new(XITEM(item)->item.references); }
987 struct oscap_reference_iterator *xccdf_rule_get_dc_statuses(const struct xccdf_rule *item) { return oscap_iterator_new(XITEM(item)->item.dc_statuses); }
988 struct oscap_reference_iterator *xccdf_profile_get_references(const struct xccdf_profile *item) { return oscap_iterator_new(XITEM(item)->item.references); }
989 struct oscap_reference_iterator *xccdf_profile_get_dc_statuses(const struct xccdf_profile *item) { return oscap_iterator_new(XITEM(item)->item.dc_statuses); }
990
991 struct xccdf_item_iterator *xccdf_item_get_content(const struct xccdf_item *item)
992 {
993 if (item == NULL) return NULL;
994 switch (xccdf_item_get_type(item)) {
995 case XCCDF_GROUP: return xccdf_group_get_content(XGROUP(item));
996 case XCCDF_BENCHMARK: return xccdf_benchmark_get_content(XBENCHMARK(item));
997 default: return NULL;
998 }
999 }
1000
1001 struct xccdf_status *xccdf_status_new_fill(const char *status, const char *date)
1002 {
1003 struct xccdf_status *ret;
1004 if (!status)
1005 return NULL;
1006 ret = calloc(1, sizeof(struct xccdf_status));
1007 if ((ret->status = oscap_string_to_enum(XCCDF_STATUS_MAP, status)) == XCCDF_STATUS_NOT_SPECIFIED) {
1008 free(ret);
1009 return NULL;
1010 }
1011 ret->date = oscap_get_date(date);
1012 return ret;
1013 }
1014
1015 struct xccdf_status *xccdf_status_new(void)
1016 {
1017 return calloc(1, sizeof(struct xccdf_status));
1018 }
1019
1020 const char *xccdf_status_type_to_text(xccdf_status_type_t id)
1021 {
1022 return oscap_enum_to_string(XCCDF_STATUS_MAP, id);
1023 }
1024
1025 void xccdf_status_dump(struct xccdf_status *status, int depth)
1026 {
1027 xccdf_print_depth(depth);
1028 time_t date = xccdf_status_get_date(status);
1029 printf("%-10s (%24.24s)\n", oscap_enum_to_string(XCCDF_STATUS_MAP, xccdf_status_get_status(status)),
1030 (date ? ctime(&date) : " date not specified "));
1031 }
1032
1033 void xccdf_status_free(struct xccdf_status *status)
1034 {
1035 free(status);
1036 }
1037
1038 OSCAP_ACCESSOR_SIMPLE(time_t, xccdf_status, date)
1039 OSCAP_ACCESSOR_SIMPLE(xccdf_status_type_t, xccdf_status, status)
1040
1041 struct xccdf_status * xccdf_item_get_current_status(const struct xccdf_item *item)
1042 {
1043 time_t maxtime = 0;
1044 struct xccdf_status *max_status = NULL;
1045 struct xccdf_status *status;
1046 const struct oscap_list_item *li = item->item.statuses->first;
1047 while (li) {
1048 status = li->data;
1049 if (status->date == 0 || status->date >= maxtime) {
1050 maxtime = status->date;
1051 max_status = status;
1052 }
1053 li = li->next;
1054 }
1055 return max_status;
1056 }
1057
1058 struct xccdf_model *xccdf_model_clone(const struct xccdf_model *old_model)
1059 {
1060 struct xccdf_model *new_model = calloc(1, sizeof(struct xccdf_model));
1061 new_model->system = oscap_strdup(old_model->system);
1062
1063 //params maps char * to char * so we will need to oscap_strdup the items.
1064 new_model->params = oscap_htable_clone(old_model->params, (oscap_clone_func) oscap_strdup);
1065 //new_model->params = NULL;
1066 return new_model;
1067 }
1068
1069 struct xccdf_model *xccdf_model_new(void)
1070 {
1071 struct xccdf_model *model = calloc(1, sizeof(struct xccdf_model));
1072 model->params = oscap_htable_new();
1073 return model;
1074 }
1075
1076 struct xccdf_model *xccdf_model_new_xml(xmlTextReaderPtr reader)
1077 {
1078 xccdf_element_t el = xccdf_element_get(reader);
1079 int depth = oscap_element_depth(reader) + 1;
1080 struct xccdf_model *model;
1081
1082 if (el != XCCDFE_MODEL)
1083 return NULL;
1084
1085 model = xccdf_model_new();
1086 model->system = xccdf_attribute_copy(reader, XCCDFA_SYSTEM);
1087
1088 while (oscap_to_start_element(reader, depth)) {
1089 if (xccdf_element_get(reader) == XCCDFE_PARAM) {
1090 const char *name = xccdf_attribute_get(reader, XCCDFA_NAME);
1091 char *value = oscap_element_string_copy(reader);
1092 if (!name || !value || !oscap_htable_add(model->params, name, value))
1093 free(value);
1094 }
1095 }
1096
1097 return model;
1098 }
1099
1100 OSCAP_ACCESSOR_STRING(xccdf_model, system)
1101
1102 void xccdf_model_free(struct xccdf_model *model)
1103 {
1104 if (model) {
1105 free(model->system);
1106 oscap_htable_free(model->params, free);
1107 free(model);
1108 }
1109 }
1110
1111 void xccdf_cstring_dump(const char *data, int depth)
1112 {
1113 xccdf_print_depth(depth);
1114 printf("%s\n", data);
1115 }
1116
1117 struct xccdf_warning *xccdf_warning_new(void)
1118 {
1119 struct xccdf_warning *w = calloc(1, sizeof(struct xccdf_warning));
1120 w->category = XCCDF_WARNING_GENERAL;
1121 return w;
1122 }
1123
1124 struct xccdf_warning *xccdf_warning_new_parse(xmlTextReaderPtr reader)
1125 {
1126 struct xccdf_warning *w = xccdf_warning_new();
1127 w->category = oscap_string_to_enum(XCCDF_WARNING_MAP, xccdf_attribute_get(reader, XCCDFA_CATEGORY));
1128 w->text = oscap_text_new_parse(XCCDF_TEXT_HTMLSUB, reader);
1129 return w;
1130 }
1131
1132 void xccdf_warning_free(struct xccdf_warning * w)
1133 {
1134 if (w != NULL) {
1135 oscap_text_free(w->text);
1136 free(w);
1137 }
1138 }
1139
1140 OSCAP_ACCESSOR_SIMPLE(xccdf_warning_category_t, xccdf_warning, category)
1141 OSCAP_ACCESSOR_TEXT(xccdf_warning, text)
1142
1143 //clones the specific types of items
1144 void xccdf_profile_item_clone(struct xccdf_profile_item *clone, const struct xccdf_profile_item * item)
1145 {
1146 clone->note_tag = oscap_strdup(item->note_tag);
1147 clone->selects = oscap_list_clone(item->selects, (oscap_clone_func) xccdf_select_clone);
1148 clone->setvalues = oscap_list_clone(item->setvalues, (oscap_clone_func) xccdf_setvalue_clone);
1149 clone->refine_values = oscap_list_clone(item->refine_values, (oscap_clone_func) xccdf_refine_value_clone);
1150 clone->refine_rules = oscap_list_clone(item->refine_rules, (oscap_clone_func) xccdf_refine_rule_clone);
1151 clone->tailoring = item->tailoring;
1152 }
1153
1154 struct xccdf_benchmark_item * xccdf_benchmark_item_clone(struct xccdf_item *parent, const struct xccdf_benchmark * bench)
1155 {
1156 struct xccdf_benchmark_item *item = &XITEM(bench)->sub.benchmark;
1157 struct xccdf_benchmark_item *clone = &parent->sub.benchmark;
1158
1159 clone->schema_version = item->schema_version;
1160
1161 clone->items_dict = oscap_htable_new();
1162 clone->profiles_dict = oscap_htable_new();
1163 clone->results_dict = oscap_htable_new();
1164 clone->clusters_dict = oscap_htable_new();
1165 clone->notices = oscap_list_clone(item->notices, (oscap_clone_func) xccdf_notice_clone);
1166 clone->plain_texts = oscap_list_clone(item->plain_texts, (oscap_clone_func) xccdf_plain_text_clone);
1167
1168 clone->style = oscap_strdup(item->style);
1169 clone->style_href = oscap_strdup(item->style_href);
1170 clone->lang = oscap_strdup(item->lang);
1171
1172 clone->rear_matter = oscap_list_clone(item->rear_matter, (oscap_clone_func) oscap_text_clone);
1173 clone->front_matter = oscap_list_clone(item->front_matter, (oscap_clone_func) oscap_text_clone);
1174
1175 clone->models = oscap_list_clone(item->models, (oscap_clone_func) xccdf_model_clone);
1176 clone->content = oscap_list_clone(item->content, (oscap_clone_func) xccdf_item_clone);
1177 xccdf_reparent_list(clone->content, parent);
1178 clone->values = oscap_list_clone(item->values, (oscap_clone_func) xccdf_value_clone);
1179 xccdf_reparent_list(clone->values, parent);
1180 clone->profiles = oscap_list_clone(item->profiles, (oscap_clone_func) xccdf_profile_clone);
1181 xccdf_reparent_list(clone->profiles, parent);
1182 clone->results = oscap_list_clone(item->results, (oscap_clone_func) xccdf_result_clone);
1183 xccdf_reparent_list(clone->results, parent);
1184 return clone;
1185 }
1186
1187 void xccdf_rule_item_clone(struct xccdf_rule_item *clone, const struct xccdf_rule_item * item)
1188 {
1189 clone->role = item->role;
1190 clone->severity = item->severity;
1191 clone->idents = oscap_list_clone(item->idents, (oscap_clone_func) xccdf_ident_clone );
1192 clone->checks = oscap_list_clone(item->checks, (oscap_clone_func) xccdf_check_clone );
1193 //requires is an oscap_list of oscap_lists of char *
1194 clone->requires_ = oscap_list_clone(item->requires_, (oscap_clone_func) oscap_stringlist_clone);
1195 //conflicts is an oscap_list of char *
1196 clone->conflicts = oscap_list_clone(item->conflicts, (oscap_clone_func) oscap_strdup);
1197 clone->profile_notes = oscap_list_clone(item->profile_notes, (oscap_clone_func) xccdf_profile_note_clone);
1198 clone->fixes = oscap_list_clone(item->fixes, (oscap_clone_func) xccdf_fix_clone);
1199 clone->fixtexts = oscap_list_clone(item->fixtexts, (oscap_clone_func) xccdf_fixtext_clone);
1200 }
1201
1202 void xccdf_group_item_clone(struct xccdf_item *parent, const struct xccdf_group_item * item)
1203 {
1204 struct xccdf_group_item *clone = &parent->sub.group;
1205 //requires is an oscap_list of oscap_lists of char *
1206 clone->requires_ = oscap_list_clone(item->requires_, (oscap_clone_func) oscap_stringlist_clone);
1207 //conflicts is an oscap_list of char *
1208 clone->conflicts = oscap_list_clone(item->conflicts, (oscap_clone_func) oscap_strdup);
1209 clone->values = oscap_list_clone(item->values, (oscap_clone_func) xccdf_value_clone);
1210 xccdf_reparent_list(clone->values, parent);
1211 clone->content = oscap_list_clone(item->content, (oscap_clone_func) xccdf_item_clone);
1212 xccdf_reparent_list(clone->content, parent);
1213 }
1214
1215 struct xccdf_value_instance * xccdf_value_instance_clone(const struct xccdf_value_instance * val)
1216 {
1217 struct xccdf_value_instance * clone = calloc(1, sizeof(struct xccdf_value_instance));
1218 clone->type = val->type;
1219
1220 clone->value = oscap_strdup(val->value);
1221 clone->defval = oscap_strdup(val->defval);
1222 clone->choices = oscap_list_clone(val->choices, (oscap_clone_func) oscap_strdup);
1223 clone->match = oscap_strdup(val->match);
1224 clone->lower_bound = val->lower_bound;
1225 clone->upper_bound = val->upper_bound;
1226
1227 clone->flags = val->flags;
1228 xccdf_value_instance_set_selector(clone, xccdf_value_instance_get_selector(val));
1229 return clone;
1230 }
1231
1232 void xccdf_value_item_clone(struct xccdf_value_item *clone, const struct xccdf_value_item * item)
1233 {
1234 //these three are values which can be directly assigned.
1235 clone->type = item->type;
1236 clone->interface_hint = item->interface_hint;
1237 clone->oper = item->oper;
1238
1239 //the rest need deep copy
1240 clone->instances = oscap_list_clone(item->instances, (oscap_clone_func) xccdf_value_instance_clone);
1241 clone->sources = oscap_list_clone(item->sources, (oscap_clone_func) oscap_strdup);
1242 }
1243
1244 struct xccdf_identity * xccdf_identity_clone(const struct xccdf_identity * identity)
1245 {
1246 struct xccdf_identity * clone = calloc(1, sizeof(struct xccdf_identity));
1247 clone->sub.authenticated = identity->sub.authenticated;
1248 clone->sub.privileged = identity->sub.privileged;
1249 clone->name = oscap_strdup(identity->name);
1250 return clone;
1251 }
1252
1253 struct xccdf_target_fact * xccdf_target_fact_clone(const struct xccdf_target_fact * tf)
1254 {
1255 struct xccdf_target_fact * clone = calloc(1, sizeof(struct xccdf_target_fact));
1256 clone->type = tf->type;
1257 clone->name = oscap_strdup(tf->name);
1258 clone->value = oscap_strdup(tf->value);
1259 return clone;
1260 }
1261
1262 struct xccdf_override * xccdf_override_clone(const struct xccdf_override * override)
1263 {
1264 struct xccdf_override * clone = calloc(1, sizeof(struct xccdf_override));
1265 clone->time = override->time;
1266 clone->authority = oscap_strdup(clone->authority);
1267 clone->old_result = override->old_result;
1268 clone->new_result = override->new_result;
1269 clone->remark = oscap_text_clone(override->remark);
1270 return clone;
1271 }
1272
1273 struct xccdf_message * xccdf_message_clone(const struct xccdf_message * message)
1274 {
1275 struct xccdf_message * clone = calloc(1, sizeof(struct xccdf_message));
1276 clone->content = oscap_strdup(message->content);
1277 clone->severity = message->severity;
1278 return clone;
1279 }
1280
1281 struct xccdf_instance * xccdf_instance_clone(const struct xccdf_instance * instance)
1282 {
1283 struct xccdf_instance * clone = calloc(1, sizeof(struct xccdf_instance));
1284 clone->context = oscap_strdup(instance->context);
1285 clone->parent_context = oscap_strdup(instance->parent_context);
1286 clone->content = oscap_strdup(instance->content);
1287 return clone;
1288 }
1289
1290 struct xccdf_rule_result * xccdf_rule_result_clone(const struct xccdf_rule_result * result)
1291 {
1292 struct xccdf_rule_result * clone = calloc(1, sizeof(struct xccdf_rule_result));
1293 clone->idref = oscap_strdup(result->idref);
1294 clone->role = result->role;
1295 clone->time = oscap_strdup(result->time);
1296 clone->weight = result->weight;
1297 clone->severity = result->severity;
1298 clone->result = result->result;
1299 clone->version = oscap_strdup(result->version);
1300 clone->overrides = oscap_list_clone(result->overrides, (oscap_clone_func) xccdf_override_clone);
1301 clone->idents = oscap_list_clone(result->idents, (oscap_clone_func) xccdf_ident_clone);
1302 clone->messages = oscap_list_clone(result->messages, (oscap_clone_func) xccdf_message_clone);
1303 clone->instances = oscap_list_clone(result->instances, (oscap_clone_func) xccdf_instance_clone);
1304 clone->fixes = oscap_list_clone(result->fixes, (oscap_clone_func) xccdf_fix_clone);
1305 clone->checks = oscap_list_clone(result->checks, (oscap_clone_func) xccdf_check_clone);
1306 return clone;
1307 }
1308
1309 struct xccdf_score * xccdf_score_clone(const struct xccdf_score * score)
1310 {
1311 struct xccdf_score * clone = calloc(1, sizeof(struct xccdf_score));
1312 clone->maximum = score->maximum;
1313 clone->score = score->score;
1314 clone->system = oscap_strdup(score->system);
1315 return clone;
1316 }
1317
1318 void xccdf_result_item_clone(struct xccdf_result_item *clone, const struct xccdf_result_item * item)
1319 {
1320 clone->start_time = oscap_strdup(item->start_time);
1321 clone->end_time = oscap_strdup(item->end_time);
1322 clone->test_system = oscap_strdup(item->test_system);
1323 clone->benchmark_uri = oscap_strdup(item->benchmark_uri);
1324 clone->profile = oscap_strdup(item->profile);
1325
1326 clone->identities = oscap_list_clone(item->identities, (oscap_clone_func) xccdf_identity_clone);
1327 clone->targets = oscap_list_clone(item->targets, (oscap_clone_func) oscap_strdup);
1328 clone->organizations = oscap_list_clone(item->organizations, (oscap_clone_func) oscap_strdup);
1329 clone->remarks = oscap_list_clone(item->remarks, (oscap_clone_func) oscap_text_clone);
1330 clone->target_addresses = oscap_list_clone(item->target_addresses, (oscap_clone_func) oscap_strdup);
1331 clone->target_facts = oscap_list_clone(item->target_facts, (oscap_clone_func) xccdf_target_fact_clone);
1332 clone->target_id_refs = oscap_list_clone(item->target_id_refs, (oscap_clone_func) xccdf_target_identifier_clone);
1333 clone->applicable_platforms = oscap_list_clone(item->applicable_platforms, (oscap_clone_func) oscap_strdup);
1334 clone->setvalues = oscap_list_clone(item->setvalues, (oscap_clone_func) xccdf_setvalue_clone);
1335 clone->rule_results = oscap_list_clone(item->rule_results, (oscap_clone_func) xccdf_rule_result_clone);
1336 clone->scores = oscap_list_clone(item->scores, (oscap_clone_func) xccdf_score_clone);
1337 }
1338
1339 void xccdf_reparent_list(struct oscap_list * item_list, struct xccdf_item * parent)
1340 {
1341 struct oscap_iterator *it = oscap_iterator_new(item_list);
1342 while(oscap_iterator_has_more(it))
1343 {
1344 struct xccdf_item * item = oscap_iterator_next(it);
1345 xccdf_reparent_item(item, parent);
1346 }
1347 oscap_iterator_free(it);
1348 }
1349
1350 void xccdf_reparent_item(struct xccdf_item * item, struct xccdf_item * parent)
1351 {
1352 if(item != NULL)
1353 {
1354 item->item.parent = parent;
1355 xccdf_benchmark_register_item(xccdf_item_get_benchmark(item), item);
1356 }
1357 }
1358
1359 const struct oscap_text_traits XCCDF_TEXT_PLAIN = { .can_override = true };
1360 const struct oscap_text_traits XCCDF_TEXT_HTML = { .html = true, .can_override = true };
1361 const struct oscap_text_traits XCCDF_TEXT_PLAINSUB = { .can_override = true, .can_substitute = true };
1362 const struct oscap_text_traits XCCDF_TEXT_HTMLSUB = { .html = true, .can_override = true, .can_substitute = true };
1363 const struct oscap_text_traits XCCDF_TEXT_NOTICE = { .html = true };
1364 const struct oscap_text_traits XCCDF_TEXT_PROFNOTE = { .html = true, .can_substitute = true };