| 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 <assert.h> |
| 31 | #include <string.h> |
| 32 | |
| 33 | #include "item.h" |
| 34 | #include "elements.h" |
| 35 | #include "helpers.h" |
| 36 | #include "xccdf_impl.h" |
| 37 | #include "common/debug_priv.h" |
| 38 | #include "oscap_helpers.h" |
| 39 | #include "oscap_string.h" |
| 40 | |
| 41 | bool xccdf_content_parse(xmlTextReaderPtr reader, struct xccdf_item *parent) |
| 42 | { |
| 43 | assert(parent != NULL); |
| 44 | |
| 45 | struct oscap_list *list = NULL; |
| 46 | struct xccdf_item *item = NULL; |
| 47 | |
| 48 | switch (parent->type) { |
| 49 | case XCCDF_BENCHMARK: |
| 50 | list = parent->sub.benchmark.content; |
| 51 | break; |
| 52 | case XCCDF_GROUP: |
| 53 | list = parent->sub.group.content; |
| 54 | break; |
| 55 | default: |
| 56 | assert(false); |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | switch (xccdf_element_get(reader)) { |
| 61 | case XCCDFE_RULE: |
| 62 | item = xccdf_rule_parse(reader, parent); |
| 63 | break; |
| 64 | case XCCDFE_GROUP: |
| 65 | item = xccdf_group_parse(reader, parent); |
| 66 | break; |
| 67 | default: |
| 68 | assert(false); |
| 69 | } |
| 70 | |
| 71 | if (item != NULL) { |
| 72 | oscap_list_add(list, item); |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | static void xccdf_deps_get(struct xccdf_item *item, struct oscap_list **conflicts, struct oscap_list **requires) |
| 80 | { |
| 81 | switch (item->type) { |
| 82 | case XCCDF_RULE: |
| 83 | if (conflicts) |
| 84 | *conflicts = item->sub.rule.conflicts; |
| 85 | if (requires) |
| 86 | *requires = item->sub.rule.requires_; |
| 87 | break; |
| 88 | case XCCDF_GROUP: |
| 89 | if (conflicts) |
| 90 | *conflicts = item->sub.group.conflicts; |
| 91 | if (requires) |
| 92 | *requires = item->sub.group.requires_; |
| 93 | break; |
| 94 | default: |
| 95 | assert(false); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | static bool xccdf_item_parse_deps(xmlTextReaderPtr reader, struct xccdf_item *item) |
| 100 | { |
| 101 | struct oscap_list *conflicts = NULL; |
| 102 | struct oscap_list *requires = NULL; |
| 103 | xccdf_deps_get(item, &conflicts, &requires); |
| 104 | |
| 105 | switch (xccdf_element_get(reader)) { |
| 106 | case XCCDFE_REQUIRES:{ |
| 107 | struct oscap_list *reqs = oscap_list_new(); |
| 108 | char *ids = xccdf_attribute_copy(reader, XCCDFA_IDREF), *idsstr = ids, *id; |
| 109 | |
| 110 | while ((id = strsep(&ids, " ")) != NULL) { |
| 111 | if (strcmp(id, "") == 0) continue; |
| 112 | oscap_list_add(reqs, oscap_strdup(id)); |
| 113 | } |
| 114 | if (reqs->itemcount == 0) { |
| 115 | oscap_list_free(reqs, NULL); |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | oscap_list_add(requires, reqs); |
| 120 | free(idsstr); |
| 121 | break; |
| 122 | } |
| 123 | case XCCDFE_CONFLICTS: |
| 124 | oscap_list_add(conflicts, xccdf_attribute_copy(reader, XCCDFA_IDREF)); |
| 125 | break; |
| 126 | default: |
| 127 | assert(false); |
| 128 | } |
| 129 | |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | static void xccdf_items_print_id_list(struct oscap_list *items, const char *sep) |
| 134 | { |
| 135 | struct oscap_list_item *it; |
| 136 | if (sep == NULL) |
| 137 | sep = ", "; |
| 138 | for (it = items->first; it; it = it->next) { |
| 139 | if (it != items->first) |
| 140 | printf("%s", sep); |
| 141 | printf("%s", (const char *) it->data); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | static void xccdf_item_dump_deps(struct xccdf_item *item, int depth) |
| 146 | { |
| 147 | struct oscap_list *conflicts = NULL, *requires = NULL; |
| 148 | xccdf_deps_get(item, &conflicts, &requires); |
| 149 | |
| 150 | if (conflicts == NULL || requires == NULL) { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | if (requires->itemcount > 0) { |
| 155 | struct oscap_list_item *it; |
| 156 | xccdf_print_depth(depth); |
| 157 | printf("requires: "); |
| 158 | for (it = requires->first; it; it = it->next) { |
| 159 | struct oscap_list *nlist = it->data; |
| 160 | if (it != requires->first) |
| 161 | printf(" & "); |
| 162 | if (nlist->itemcount == 0) |
| 163 | continue; |
| 164 | if (nlist->itemcount > 1) |
| 165 | printf("("); |
| 166 | xccdf_items_print_id_list(nlist, " | "); |
| 167 | if (nlist->itemcount > 1) |
| 168 | printf(")"); |
| 169 | } |
| 170 | printf("\n"); |
| 171 | } |
| 172 | |
| 173 | if (conflicts->itemcount > 0) { |
| 174 | xccdf_print_depth(depth); |
| 175 | printf("conflicts: "); |
| 176 | xccdf_items_print_id_list(conflicts, " | "); |
| 177 | printf("\n"); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | struct xccdf_item *xccdf_group_new_internal(struct xccdf_item *parent) |
| 182 | { |
| 183 | struct xccdf_item *group = xccdf_item_new(XCCDF_GROUP, parent); |
| 184 | group->sub.group.content = oscap_list_new(); |
| 185 | group->sub.group.requires_ = oscap_list_new(); |
| 186 | group->sub.group.conflicts = oscap_list_new(); |
| 187 | group->sub.group.values = oscap_list_new(); |
| 188 | return group; |
| 189 | } |
| 190 | |
| 191 | struct xccdf_group *xccdf_group_new(void) |
| 192 | { |
| 193 | return XGROUP(xccdf_group_new_internal(NULL)); |
| 194 | } |
| 195 | |
| 196 | struct xccdf_group * xccdf_group_clone(const struct xccdf_group * group) |
| 197 | { |
| 198 | struct xccdf_item *new_group = calloc(1, sizeof(struct xccdf_item) + sizeof(struct xccdf_group_item)); |
| 199 | struct xccdf_item *old = XITEM(group); |
| 200 | xccdf_item_base_clone(&new_group->item, &(old->item)); |
| 201 | new_group->type = old->type; |
| 202 | xccdf_group_item_clone(new_group, &(old->sub.group)); |
| 203 | return XGROUP(new_group); |
| 204 | } |
| 205 | |
| 206 | struct xccdf_item *xccdf_group_parse(xmlTextReaderPtr reader, struct xccdf_item *parent) |
| 207 | { |
| 208 | XCCDF_ASSERT_ELEMENT(reader, XCCDFE_GROUP); |
| 209 | |
| 210 | struct xccdf_item *group = xccdf_group_new_internal(parent); |
| 211 | |
| 212 | if (!xccdf_item_process_attributes(group, reader)) { |
| 213 | xccdf_group_free(group); |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | int depth = oscap_element_depth(reader) + 1; |
| 218 | |
| 219 | while (oscap_to_start_element(reader, depth)) { |
| 220 | switch (xccdf_element_get(reader)) { |
| 221 | case XCCDFE_REQUIRES: |
| 222 | case XCCDFE_CONFLICTS: |
| 223 | xccdf_item_parse_deps(reader, group); |
| 224 | break; |
| 225 | case XCCDFE_GROUP: |
| 226 | case XCCDFE_RULE: |
| 227 | xccdf_content_parse(reader, group); |
| 228 | break; |
| 229 | case XCCDFE_VALUE: |
| 230 | oscap_list_add(group->sub.group.values, xccdf_value_parse(reader, group)); |
| 231 | break; |
| 232 | default: |
| 233 | if (!xccdf_item_process_element(group, reader)) |
| 234 | dW("Encountered an unknown element '%s' while parsing XCCDF group.", |
| 235 | xmlTextReaderConstLocalName(reader)); |
| 236 | } |
| 237 | xmlTextReaderRead(reader); |
| 238 | } |
| 239 | |
| 240 | return group; |
| 241 | } |
| 242 | |
| 243 | void xccdf_group_dump(struct xccdf_item *group, int depth) |
| 244 | { |
| 245 | xccdf_print_depth(depth); |
| 246 | printf("Group : %s\n", (group ? group->item.id : "(NULL)")); |
| 247 | if (group) { |
| 248 | xccdf_item_print(group, depth + 1); |
| 249 | xccdf_print_depth(depth + 1); |
| 250 | printf("values"); |
| 251 | oscap_list_dump(group->sub.group.values, (oscap_dump_func) xccdf_value_dump, depth + 2); |
| 252 | xccdf_print_depth(depth + 1); |
| 253 | printf("content"); |
| 254 | oscap_list_dump(group->sub.group.content, (oscap_dump_func) xccdf_item_dump, depth + 2); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | static void xccdf_free_strlist(struct oscap_list *list) |
| 259 | { |
| 260 | if (list) oscap_list_free(list, free); |
| 261 | } |
| 262 | |
| 263 | void xccdf_group_free(struct xccdf_item *group) |
| 264 | { |
| 265 | if (group) { |
| 266 | oscap_list_free(group->sub.group.content, (oscap_destruct_func) xccdf_item_free); |
| 267 | oscap_list_free(group->sub.group.values, (oscap_destruct_func) xccdf_value_free); |
| 268 | oscap_list_free(group->sub.group.requires_, (oscap_destruct_func) xccdf_free_strlist); |
| 269 | oscap_list_free(group->sub.group.conflicts, free); |
| 270 | xccdf_item_release(group); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | struct xccdf_item *xccdf_rule_new_internal(struct xccdf_item *parent) |
| 275 | { |
| 276 | struct xccdf_item *rule = xccdf_item_new(XCCDF_RULE, parent); |
| 277 | rule->sub.rule.role = XCCDF_ROLE_FULL; |
| 278 | rule->sub.rule.severity = XCCDF_UNKNOWN; |
| 279 | |
| 280 | rule->sub.rule.idents = oscap_list_new(); |
| 281 | rule->sub.rule.checks = oscap_list_new(); |
| 282 | rule->sub.rule.requires_ = oscap_list_new(); |
| 283 | rule->sub.rule.conflicts = oscap_list_new(); |
| 284 | rule->sub.rule.profile_notes = oscap_list_new(); |
| 285 | rule->sub.rule.fixes = oscap_list_new(); |
| 286 | rule->sub.rule.fixtexts = oscap_list_new(); |
| 287 | |
| 288 | return rule; |
| 289 | } |
| 290 | |
| 291 | struct xccdf_rule *xccdf_rule_new(void) |
| 292 | { |
| 293 | return XRULE(xccdf_rule_new_internal(NULL)); |
| 294 | } |
| 295 | |
| 296 | struct xccdf_rule *xccdf_rule_clone(const struct xccdf_rule * rule) |
| 297 | { |
| 298 | struct xccdf_item *new_rule = calloc(1, sizeof(struct xccdf_item) + sizeof(struct xccdf_rule_item)); |
| 299 | struct xccdf_item *old = XITEM(rule); |
| 300 | xccdf_item_base_clone(&new_rule->item, &(old->item)); |
| 301 | new_rule->type = old->type; |
| 302 | xccdf_rule_item_clone(&new_rule->sub.rule, &old->sub.rule); |
| 303 | return XRULE(new_rule); |
| 304 | } |
| 305 | |
| 306 | struct xccdf_item *xccdf_rule_parse(xmlTextReaderPtr reader, struct xccdf_item *parent) |
| 307 | { |
| 308 | XCCDF_ASSERT_ELEMENT(reader, XCCDFE_RULE); |
| 309 | |
| 310 | struct xccdf_item *rule = xccdf_rule_new_internal(parent); |
| 311 | |
| 312 | if (!xccdf_item_process_attributes(rule, reader)) { |
| 313 | xccdf_rule_free(rule); |
| 314 | return NULL; |
| 315 | } |
| 316 | if (xccdf_attribute_has(reader, XCCDFA_ROLE)) { |
| 317 | rule->sub.rule.role = oscap_string_to_enum(XCCDF_ROLE_MAP, xccdf_attribute_get(reader, XCCDFA_ROLE)); |
| 318 | rule->item.defined_flags.role = true; |
| 319 | } |
| 320 | if (xccdf_attribute_has(reader, XCCDFA_SEVERITY)) { |
| 321 | rule->sub.rule.severity = |
| 322 | oscap_string_to_enum(XCCDF_LEVEL_MAP, xccdf_attribute_get(reader, XCCDFA_SEVERITY)); |
| 323 | rule->item.defined_flags.severity = true; |
| 324 | } |
| 325 | |
| 326 | int depth = oscap_element_depth(reader) + 1; |
| 327 | |
| 328 | while (oscap_to_start_element(reader, depth)) { |
| 329 | switch (xccdf_element_get(reader)) { |
| 330 | case XCCDFE_REQUIRES: |
| 331 | case XCCDFE_CONFLICTS: |
| 332 | xccdf_item_parse_deps(reader, rule); |
| 333 | break; |
| 334 | case XCCDFE_PROFILE_NOTE:{ |
| 335 | const char *tag = xccdf_attribute_get(reader, XCCDFA_TAG); |
| 336 | if (tag == NULL) |
| 337 | break; |
| 338 | struct xccdf_profile_note *note = xccdf_profile_note_new(); |
| 339 | note->reftag = oscap_strdup(tag); |
| 340 | note->text = oscap_text_new_parse(XCCDF_TEXT_PROFNOTE, reader); |
| 341 | oscap_list_add(rule->sub.rule.profile_notes, note); |
| 342 | break; |
| 343 | } |
| 344 | case XCCDFE_COMPLEX_CHECK: |
| 345 | case XCCDFE_CHECK:{ |
| 346 | struct xccdf_check *check = xccdf_check_parse(reader); |
| 347 | if (check == NULL) |
| 348 | break; |
| 349 | oscap_list_add(rule->sub.rule.checks, check); |
| 350 | break; |
| 351 | } |
| 352 | case XCCDFE_FIX: |
| 353 | oscap_list_add(rule->sub.rule.fixes, xccdf_fix_parse(reader)); |
| 354 | break; |
| 355 | case XCCDFE_FIXTEXT: |
| 356 | oscap_list_add(rule->sub.rule.fixtexts, xccdf_fixtext_parse(reader)); |
| 357 | break; |
| 358 | case XCCDFE_IDENT: |
| 359 | oscap_list_add(rule->sub.rule.idents, xccdf_ident_parse(reader)); |
| 360 | break; |
| 361 | default: |
| 362 | if (!xccdf_item_process_element(rule, reader)) |
| 363 | dW("Encountered an unknown element '%s' while parsing XCCDF group.", |
| 364 | xmlTextReaderConstLocalName(reader)); |
| 365 | } |
| 366 | xmlTextReaderRead(reader); |
| 367 | } |
| 368 | |
| 369 | return rule; |
| 370 | } |
| 371 | |
| 372 | void xccdf_rule_dump(struct xccdf_item *rule, int depth) |
| 373 | { |
| 374 | xccdf_print_depth(depth); |
| 375 | printf("Rule : %s\n", (rule ? rule->item.id : "(NULL)")); |
| 376 | if (rule) { |
| 377 | xccdf_item_print(rule, depth + 1); |
| 378 | xccdf_item_dump_deps(rule, depth + 1); |
| 379 | xccdf_print_depth(depth + 1); |
| 380 | printf("idents"); |
| 381 | oscap_list_dump(rule->sub.rule.idents, (oscap_dump_func) xccdf_ident_dump, depth + 2); |
| 382 | xccdf_print_depth(depth + 1); |
| 383 | printf("checks"); |
| 384 | oscap_list_dump(rule->sub.rule.checks, (oscap_dump_func) xccdf_check_dump, depth + 2); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | void xccdf_rule_free(struct xccdf_item *rule) |
| 389 | { |
| 390 | if (rule) { |
| 391 | oscap_list_free(rule->sub.rule.idents, (oscap_destruct_func) xccdf_ident_free); |
| 392 | oscap_list_free(rule->sub.rule.checks, (oscap_destruct_func) xccdf_check_free); |
| 393 | oscap_list_free(rule->sub.rule.profile_notes, (oscap_destruct_func) xccdf_profile_note_free); |
| 394 | oscap_list_free(rule->sub.rule.fixes, (oscap_destruct_func) xccdf_fix_free); |
| 395 | oscap_list_free(rule->sub.rule.fixtexts, (oscap_destruct_func) xccdf_fixtext_free); |
| 396 | oscap_list_free(rule->sub.rule.requires_, (oscap_destruct_func) xccdf_free_strlist); |
| 397 | oscap_list_free(rule->sub.rule.conflicts, free); |
| 398 | xccdf_item_release(rule); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Filter function returning true if the check/@selector matches with selectid |
| 404 | */ |
| 405 | static bool |
| 406 | _xccdf_check_filter_selector(struct xccdf_check *check, char *selectid) |
| 407 | { |
| 408 | return !oscap_strcmp((char*) xccdf_check_get_selector(check), selectid); |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Filter all checks within a rule by their @selector attribute. |
| 413 | * @param rule which encapsulates the checks |
| 414 | * @param selector string to filter these checks. NULL or "" values might be used |
| 415 | * in order to filter checks without @selector attribute. |
| 416 | */ |
| 417 | struct xccdf_check_iterator * |
| 418 | xccdf_rule_get_checks_filtered(struct xccdf_item *rule, char *selector) |
| 419 | { |
| 420 | return (struct xccdf_check_iterator *) oscap_iterator_new_filter(rule->sub.rule.checks, (oscap_filter_func) _xccdf_check_filter_selector, selector); |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Filter function returning true if the xccdf_check is complex-check |
| 425 | */ |
| 426 | static bool |
| 427 | _xccdf_check_filter_complex(struct xccdf_check *check, void *unused) |
| 428 | { |
| 429 | return xccdf_check_get_complex(check); |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Filter checks and return complex-check. |
| 434 | * Note: In valid XCCDF 1.2 documents there must not be more than one check within the given rule. |
| 435 | */ |
| 436 | struct xccdf_check_iterator * |
| 437 | xccdf_rule_get_complex_checks(struct xccdf_item *rule) |
| 438 | { |
| 439 | return (struct xccdf_check_iterator *) oscap_iterator_new_filter(rule->sub.rule.checks, (oscap_filter_func) _xccdf_check_filter_complex, NULL); |
| 440 | } |
| 441 | |
| 442 | struct xccdf_ident * xccdf_ident_clone(const struct xccdf_ident * ident) |
| 443 | { |
| 444 | struct xccdf_ident * clone = xccdf_ident_new(); |
| 445 | clone->id = oscap_strdup(ident->id); |
| 446 | clone->system = oscap_strdup(ident->system); |
| 447 | return clone; |
| 448 | } |
| 449 | |
| 450 | struct xccdf_ident *xccdf_ident_new(void) |
| 451 | { |
| 452 | return calloc(1, sizeof(struct xccdf_ident)); |
| 453 | } |
| 454 | |
| 455 | struct xccdf_ident *xccdf_ident_new_fill(const char *id, const char *sys) |
| 456 | { |
| 457 | struct xccdf_ident *ident = xccdf_ident_new(); |
| 458 | ident->id = oscap_strdup(id); |
| 459 | ident->system = oscap_strdup(sys); |
| 460 | return ident; |
| 461 | } |
| 462 | |
| 463 | void xccdf_ident_set_id(struct xccdf_ident * ident, const char *id) |
| 464 | { |
| 465 | ident->id = oscap_strdup(id); |
| 466 | } |
| 467 | |
| 468 | void xccdf_ident_set_system(struct xccdf_ident * ident, const char *sys) |
| 469 | { |
| 470 | ident->system = oscap_strdup(sys); |
| 471 | } |
| 472 | |
| 473 | struct xccdf_ident *xccdf_ident_parse(xmlTextReaderPtr reader) |
| 474 | { |
| 475 | XCCDF_ASSERT_ELEMENT(reader, XCCDFE_IDENT); |
| 476 | const char *sys = xccdf_attribute_get(reader, XCCDFA_SYSTEM); |
| 477 | const char *id = oscap_element_string_get(reader); |
| 478 | return xccdf_ident_new_fill(id, sys); |
| 479 | } |
| 480 | |
| 481 | void xccdf_ident_dump(struct xccdf_ident *ident, int depth) |
| 482 | { |
| 483 | xccdf_print_depth(depth); |
| 484 | printf("ident : %s => %s\n", ident->system, ident->id); |
| 485 | } |
| 486 | |
| 487 | void xccdf_ident_free(struct xccdf_ident *ident) |
| 488 | { |
| 489 | if (ident) { |
| 490 | free(ident->id); |
| 491 | free(ident->system); |
| 492 | free(ident); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | struct xccdf_profile_note *xccdf_profile_note_new(void) |
| 497 | { |
| 498 | return calloc(1, sizeof(struct xccdf_profile_note)); |
| 499 | } |
| 500 | |
| 501 | struct xccdf_profile_note * xccdf_profile_note_clone(const struct xccdf_profile_note * note) |
| 502 | { |
| 503 | struct xccdf_profile_note * clone = xccdf_profile_note_new(); |
| 504 | clone->reftag = oscap_strdup(note->reftag); |
| 505 | clone->text = oscap_text_clone(note->text); |
| 506 | return clone; |
| 507 | } |
| 508 | |
| 509 | void xccdf_profile_note_free(struct xccdf_profile_note *note) |
| 510 | { |
| 511 | if (note) { |
| 512 | free(note->reftag); |
| 513 | oscap_text_free(note->text); |
| 514 | free(note); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | XCCDF_GENERIC_GETTER(const char *, ident, id) XCCDF_GENERIC_GETTER(const char *, ident, system) |
| 519 | |
| 520 | struct xccdf_check *xccdf_check_new(void) |
| 521 | { |
| 522 | struct xccdf_check *check = calloc(1, sizeof(struct xccdf_check)); |
| 523 | check->content_refs = oscap_list_new(); |
| 524 | check->imports = oscap_list_new(); |
| 525 | check->exports = oscap_list_new(); |
| 526 | check->children = oscap_list_new(); |
| 527 | return check; |
| 528 | } |
| 529 | |
| 530 | /* Performs a deep copy of a provided xccdf_check, returns a pointer to that copy */ |
| 531 | struct xccdf_check *xccdf_check_clone(const struct xccdf_check* old_check) |
| 532 | { |
| 533 | struct xccdf_check *new_check = calloc(1, sizeof(struct xccdf_check)); |
| 534 | |
| 535 | new_check->id = oscap_strdup(old_check->id); |
| 536 | new_check->system = oscap_strdup(old_check->system); |
| 537 | new_check->selector = oscap_strdup(old_check->selector); |
| 538 | new_check->content = oscap_strdup(old_check->content); |
| 539 | new_check->oper = old_check->oper; |
| 540 | new_check->flags = old_check->flags; |
| 541 | |
| 542 | new_check->imports = oscap_list_clone(old_check->imports, (oscap_clone_func) xccdf_check_import_clone); |
| 543 | new_check->exports = oscap_list_clone(old_check->exports, (oscap_clone_func) xccdf_check_export_clone); |
| 544 | new_check->content_refs = oscap_list_clone(old_check->content_refs, (oscap_clone_func) xccdf_check_content_ref_clone); |
| 545 | new_check->children = oscap_list_clone(old_check->children, (oscap_clone_func) xccdf_check_clone); |
| 546 | |
| 547 | return new_check; |
| 548 | } |
| 549 | |
| 550 | /* Performs a deep copy of a provided xccdf_check_import, returns a pointer to that copy */ |
| 551 | struct xccdf_check_import *xccdf_check_import_clone(const struct xccdf_check_import* old_import) |
| 552 | { |
| 553 | struct xccdf_check_import *new_import = xccdf_check_import_new(); |
| 554 | new_import->name = oscap_strdup(old_import->name); |
| 555 | if (old_import->xpath) |
| 556 | new_import->xpath = oscap_strdup(old_import->xpath); |
| 557 | new_import->content = oscap_strdup(old_import->content); |
| 558 | return new_import; |
| 559 | } |
| 560 | |
| 561 | /* Performs a deep copy of a provided xccdf_check_export, returns a pointer to that copy */ |
| 562 | struct xccdf_check_export *xccdf_check_export_clone(const struct xccdf_check_export* old_export) |
| 563 | { |
| 564 | struct xccdf_check_export *new_export = xccdf_check_export_new(); |
| 565 | new_export->name = oscap_strdup(old_export->name); |
| 566 | new_export->value = oscap_strdup(old_export->value); |
| 567 | return new_export; |
| 568 | } |
| 569 | |
| 570 | /* Performs a deep copy of a provided xcdf_check_content_ref, returns a pointer to that copy */ |
| 571 | struct xccdf_check_content_ref *xccdf_check_content_ref_clone(const struct xccdf_check_content_ref* old_ref) |
| 572 | { |
| 573 | struct xccdf_check_content_ref *new_ref = xccdf_check_content_ref_new(); |
| 574 | new_ref->name = oscap_strdup(old_ref->name); |
| 575 | new_ref->href = oscap_strdup(old_ref->href); |
| 576 | return new_ref; |
| 577 | } |
| 578 | |
| 579 | struct xccdf_check *xccdf_check_parse(xmlTextReaderPtr reader) |
| 580 | { |
| 581 | xccdf_element_t el = xccdf_element_get(reader); |
| 582 | if (el != XCCDFE_CHECK && el != XCCDFE_COMPLEX_CHECK) |
| 583 | return NULL; |
| 584 | struct xccdf_check *check = xccdf_check_new(); |
| 585 | |
| 586 | check->id = xccdf_attribute_copy(reader, XCCDFA_ID); |
| 587 | check->system = xccdf_attribute_copy(reader, XCCDFA_SYSTEM); |
| 588 | check->selector = xccdf_attribute_copy(reader, XCCDFA_SELECTOR); |
| 589 | check->oper = oscap_string_to_enum(XCCDF_BOOLOP_MAP, xccdf_attribute_get(reader, XCCDFA_OPERATOR)); |
| 590 | if (xccdf_attribute_has(reader, XCCDFA_MULTICHECK) && el != XCCDFE_COMPLEX_CHECK) { |
| 591 | check->flags.def_multicheck = true; |
| 592 | check->flags.multicheck = xccdf_attribute_get_bool(reader, XCCDFA_MULTICHECK); |
| 593 | } |
| 594 | check->flags.def_negate = xccdf_attribute_has(reader, XCCDFA_NEGATE); |
| 595 | check->flags.negate = xccdf_attribute_get_bool(reader, XCCDFA_NEGATE); |
| 596 | |
| 597 | int depth = oscap_element_depth(reader) + 1; |
| 598 | |
| 599 | while (oscap_to_start_element(reader, depth)) { |
| 600 | switch (xccdf_element_get(reader)) { |
| 601 | case XCCDFE_CHECK: |
| 602 | case XCCDFE_COMPLEX_CHECK: |
| 603 | if (check->oper == 0) |
| 604 | break; |
| 605 | oscap_list_add(check->children, xccdf_check_parse(reader)); |
| 606 | break; |
| 607 | case XCCDFE_CHECK_CONTENT_REF:{ |
| 608 | const char *href = xccdf_attribute_get(reader, XCCDFA_HREF); |
| 609 | if (href == NULL) |
| 610 | break; |
| 611 | struct xccdf_check_content_ref *ref = xccdf_check_content_ref_new(); |
| 612 | ref->name = xccdf_attribute_copy(reader, XCCDFA_NAME); |
| 613 | ref->href = oscap_strdup(href); |
| 614 | oscap_list_add(check->content_refs, ref); |
| 615 | break; |
| 616 | } |
| 617 | case XCCDFE_CHECK_CONTENT: |
| 618 | if (check->content == NULL) |
| 619 | check->content = oscap_get_xml(reader); |
| 620 | break; |
| 621 | case XCCDFE_CHECK_IMPORT:{ |
| 622 | const char *name = xccdf_attribute_get(reader, XCCDFA_IMPORT_NAME); |
| 623 | const char *xpath = xccdf_attribute_get(reader, XCCDFA_IMPORT_XPATH); |
| 624 | if (name == NULL) // @import-name is a required attribute |
| 625 | break; |
| 626 | struct xccdf_check_import *imp = xccdf_check_import_new(); |
| 627 | imp->name = oscap_strdup(name); |
| 628 | if (xpath) // @import-xpath is just optional |
| 629 | imp->xpath = oscap_strdup(xpath); |
| 630 | imp->content = oscap_element_string_copy(reader); |
| 631 | oscap_list_add(check->imports, imp); |
| 632 | break; |
| 633 | } |
| 634 | case XCCDFE_CHECK_EXPORT:{ |
| 635 | const char *name = xccdf_attribute_get(reader, XCCDFA_EXPORT_NAME); |
| 636 | if (name == NULL) |
| 637 | break; |
| 638 | struct xccdf_check_export *exp = xccdf_check_export_new(); |
| 639 | exp->name = oscap_strdup(name); |
| 640 | exp->value = xccdf_attribute_copy(reader, XCCDFA_VALUE_ID); |
| 641 | oscap_list_add(check->exports, exp); |
| 642 | break; |
| 643 | } |
| 644 | default: |
| 645 | break; |
| 646 | } |
| 647 | xmlTextReaderRead(reader); |
| 648 | } |
| 649 | |
| 650 | return check; |
| 651 | } |
| 652 | |
| 653 | void xccdf_check_dump(struct xccdf_check *check, int depth) |
| 654 | { |
| 655 | if (check->id) { |
| 656 | xccdf_print_depth(depth); |
| 657 | printf("id : %s\n", check->id); |
| 658 | } |
| 659 | if (check->system) { |
| 660 | xccdf_print_depth(depth); |
| 661 | printf("system : %s\n", check->system); |
| 662 | } |
| 663 | if (check->selector) { |
| 664 | xccdf_print_depth(depth); |
| 665 | printf("selector: %s\n", check->selector); |
| 666 | } |
| 667 | if (check->content) { |
| 668 | xccdf_print_depth(depth); |
| 669 | printf("content : %s\n", check->content); |
| 670 | } |
| 671 | xccdf_print_depth(depth); |
| 672 | printf("content-refs"); |
| 673 | oscap_list_dump(check->content_refs, (oscap_dump_func) xccdf_check_content_ref_dump, depth + 1); |
| 674 | } |
| 675 | |
| 676 | /** |
| 677 | * Enforce given content with given name to be the only check-content of the check. |
| 678 | * This may turn to be usefull,when processing check within rule-result -- |
| 679 | * such check may (and in some cases must) refer only to the executed content. |
| 680 | */ |
| 681 | bool |
| 682 | xccdf_check_inject_content_ref(struct xccdf_check *check, const struct xccdf_check_content_ref *content, const char *name) |
| 683 | { |
| 684 | struct xccdf_check_content_ref *content_clone = xccdf_check_content_ref_clone(content); |
| 685 | if (name != NULL) |
| 686 | xccdf_check_content_ref_set_name(content_clone, name); |
| 687 | oscap_list_free(check->content_refs, (oscap_destruct_func) xccdf_check_content_ref_free); |
| 688 | check->content_refs = oscap_list_new(); |
| 689 | return oscap_list_add(check->content_refs, content_clone); |
| 690 | } |
| 691 | |
| 692 | bool xccdf_check_get_complex(const struct xccdf_check *check) |
| 693 | { |
| 694 | return check->oper ? true : false; |
| 695 | } |
| 696 | |
| 697 | void xccdf_check_free(struct xccdf_check *check) |
| 698 | { |
| 699 | if (check) { |
| 700 | oscap_list_free(check->content_refs, (oscap_destruct_func) xccdf_check_content_ref_free); |
| 701 | oscap_list_free(check->imports, (oscap_destruct_func) xccdf_check_import_free); |
| 702 | oscap_list_free(check->exports, (oscap_destruct_func) xccdf_check_export_free); |
| 703 | oscap_list_free(check->children, (oscap_destruct_func) xccdf_check_free); |
| 704 | free(check->id); |
| 705 | free(check->system); |
| 706 | free(check->selector); |
| 707 | free(check->content); |
| 708 | free(check); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | void xccdf_check_content_ref_dump(struct xccdf_check_content_ref *ref, int depth) |
| 713 | { |
| 714 | xccdf_print_depth(depth); |
| 715 | printf("%s (%s)\n", ref->href, ref->name); |
| 716 | } |
| 717 | |
| 718 | struct xccdf_check_content_ref *xccdf_check_content_ref_new(void) |
| 719 | { |
| 720 | return calloc(1, sizeof(struct xccdf_check_content_ref)); |
| 721 | } |
| 722 | |
| 723 | void xccdf_check_content_ref_free(struct xccdf_check_content_ref *ref) |
| 724 | { |
| 725 | if (ref) { |
| 726 | free(ref->name); |
| 727 | free(ref->href); |
| 728 | free(ref); |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | struct xccdf_check_import *xccdf_check_import_new(void) |
| 733 | { |
| 734 | return calloc(1, sizeof(struct xccdf_check_import)); |
| 735 | } |
| 736 | |
| 737 | void xccdf_check_import_free(struct xccdf_check_import *item) |
| 738 | { |
| 739 | if (item) { |
| 740 | free(item->name); |
| 741 | free(item->xpath); |
| 742 | free(item->content); |
| 743 | free(item); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | struct xccdf_check_export *xccdf_check_export_new(void) |
| 748 | { |
| 749 | return calloc(1, sizeof(struct xccdf_check_export)); |
| 750 | } |
| 751 | |
| 752 | void xccdf_check_export_free(struct xccdf_check_export *item) |
| 753 | { |
| 754 | if (item) { |
| 755 | free(item->name); |
| 756 | free(item->value); |
| 757 | free(item); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | const struct oscap_string_map XCCDF_STRATEGY_MAP[] = { |
| 762 | {XCCDF_STRATEGY_CONFIGURE, "configure"}, |
| 763 | {XCCDF_STRATEGY_DISABLE, "disable"}, |
| 764 | {XCCDF_STRATEGY_ENABLE, "enable"}, |
| 765 | {XCCDF_STRATEGY_PATCH, "patch"}, |
| 766 | {XCCDF_STRATEGY_POLICY, "policy"}, |
| 767 | {XCCDF_STRATEGY_RESTRICT, "restrict"}, |
| 768 | {XCCDF_STRATEGY_UPDATE, "update"}, |
| 769 | {XCCDF_STRATEGY_COMBINATION, "combination"}, |
| 770 | {XCCDF_STRATEGY_UNKNOWN, NULL} |
| 771 | }; |
| 772 | |
| 773 | struct xccdf_fix *xccdf_fix_new(void) |
| 774 | { |
| 775 | return calloc(1, sizeof(struct xccdf_fix)); |
| 776 | } |
| 777 | |
| 778 | /* Creates a deep copy of a provided xccdf_fix, returns a pointer to that copy */ |
| 779 | struct xccdf_fix *xccdf_fix_clone(const struct xccdf_fix *old_fix) |
| 780 | { |
| 781 | struct xccdf_fix *new_fix = calloc(1, sizeof(struct xccdf_fix)); |
| 782 | |
| 783 | new_fix->reboot = old_fix->reboot; |
| 784 | new_fix->strategy = old_fix->strategy; |
| 785 | new_fix->disruption = old_fix->disruption; |
| 786 | new_fix->complexity = old_fix->complexity; |
| 787 | |
| 788 | new_fix->id = oscap_strdup(old_fix->id); |
| 789 | new_fix->content = oscap_strdup(old_fix->content); |
| 790 | new_fix->system = oscap_strdup(old_fix->system); |
| 791 | new_fix->platform = oscap_strdup(old_fix->platform); |
| 792 | |
| 793 | return new_fix; |
| 794 | } |
| 795 | |
| 796 | |
| 797 | |
| 798 | struct xccdf_fix *xccdf_fix_parse(xmlTextReaderPtr reader) |
| 799 | { |
| 800 | struct xccdf_fix *fix = xccdf_fix_new(); |
| 801 | fix->id = xccdf_attribute_copy(reader, XCCDFA_ID); |
| 802 | fix->system = xccdf_attribute_copy(reader, XCCDFA_SYSTEM); |
| 803 | fix->platform = xccdf_attribute_copy(reader, XCCDFA_PLATFORM); |
| 804 | fix->reboot = xccdf_attribute_get_bool(reader, XCCDFA_REBOOT); |
| 805 | fix->strategy = oscap_string_to_enum(XCCDF_STRATEGY_MAP, xccdf_attribute_get(reader, XCCDFA_STRATEGY)); |
| 806 | fix->disruption = oscap_string_to_enum(XCCDF_LEVEL_MAP, xccdf_attribute_get(reader, XCCDFA_DISRUPTION)); |
| 807 | fix->complexity = oscap_string_to_enum(XCCDF_LEVEL_MAP, xccdf_attribute_get(reader, XCCDFA_COMPLEXITY)); |
| 808 | fix->content = oscap_get_xml(reader); |
| 809 | return fix; |
| 810 | } |
| 811 | |
| 812 | struct xccdf_fixtext *xccdf_fixtext_new(void) |
| 813 | { |
| 814 | return calloc(1, sizeof(struct xccdf_fixtext)); |
| 815 | } |
| 816 | |
| 817 | struct xccdf_fixtext * xccdf_fixtext_clone(const struct xccdf_fixtext * fixtext) |
| 818 | { |
| 819 | struct xccdf_fixtext * clone = xccdf_fixtext_new(); |
| 820 | clone->reboot = fixtext->reboot; |
| 821 | clone->strategy = fixtext->strategy; |
| 822 | clone->disruption = fixtext->disruption; |
| 823 | clone->complexity = fixtext->complexity; |
| 824 | clone->fixref = oscap_strdup(fixtext->fixref); |
| 825 | clone->text = oscap_text_clone(fixtext->text); |
| 826 | return clone; |
| 827 | } |
| 828 | |
| 829 | struct xccdf_fixtext *xccdf_fixtext_parse(xmlTextReaderPtr reader) |
| 830 | { |
| 831 | struct xccdf_fixtext *fix = xccdf_fixtext_new(); |
| 832 | fix->fixref = xccdf_attribute_copy(reader, XCCDFA_FIXREF); |
| 833 | fix->text = oscap_text_new_parse(XCCDF_TEXT_HTMLSUB, reader); |
| 834 | fix->reboot = xccdf_attribute_get_bool(reader, XCCDFA_REBOOT); |
| 835 | fix->strategy = oscap_string_to_enum(XCCDF_STRATEGY_MAP, xccdf_attribute_get(reader, XCCDFA_STRATEGY)); |
| 836 | fix->disruption = oscap_string_to_enum(XCCDF_LEVEL_MAP, xccdf_attribute_get(reader, XCCDFA_DISRUPTION)); |
| 837 | fix->complexity = oscap_string_to_enum(XCCDF_LEVEL_MAP, xccdf_attribute_get(reader, XCCDFA_COMPLEXITY)); |
| 838 | return fix; |
| 839 | } |
| 840 | |
| 841 | void xccdf_fixtext_free(struct xccdf_fixtext *item) |
| 842 | { |
| 843 | if (item) { |
| 844 | oscap_text_free(item->text); |
| 845 | free(item->fixref); |
| 846 | free(item); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | void xccdf_fix_free(struct xccdf_fix *item) |
| 851 | { |
| 852 | if (item) { |
| 853 | free(item->id); |
| 854 | free(item->system); |
| 855 | free(item->platform); |
| 856 | free(item->content); |
| 857 | free(item); |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | struct oscap_string_iterator *xccdf_item_get_conflicts(const struct xccdf_item* item) |
| 862 | { |
| 863 | if (item == NULL) return NULL; |
| 864 | if (item->type == XCCDF_RULE) return xccdf_rule_get_conflicts ( XRULE(item)); |
| 865 | if (item->type == XCCDF_GROUP) return xccdf_group_get_conflicts(XGROUP(item)); |
| 866 | return NULL; |
| 867 | } |
| 868 | struct oscap_string_iterator *xccdf_rule_get_conflicts(const struct xccdf_rule* rule) |
| 869 | { |
| 870 | return oscap_iterator_new(XITEM(rule)->sub.rule.conflicts); |
| 871 | } |
| 872 | struct oscap_string_iterator *xccdf_group_get_conflicts(const struct xccdf_group* group) |
| 873 | { |
| 874 | return oscap_iterator_new(XITEM(group)->sub.group.conflicts); |
| 875 | } |
| 876 | |
| 877 | struct oscap_stringlist_iterator *xccdf_item_get_requires(const struct xccdf_item* item) |
| 878 | { |
| 879 | if (item == NULL) return NULL; |
| 880 | if (item->type == XCCDF_RULE) return xccdf_rule_get_requires ( XRULE(item)); |
| 881 | if (item->type == XCCDF_GROUP) return xccdf_group_get_requires(XGROUP(item)); |
| 882 | return NULL; |
| 883 | } |
| 884 | struct oscap_stringlist_iterator *xccdf_rule_get_requires(const struct xccdf_rule* rule) |
| 885 | { |
| 886 | return oscap_iterator_new(XITEM(rule)->sub.rule.requires_); |
| 887 | } |
| 888 | struct oscap_stringlist_iterator *xccdf_group_get_requires(const struct xccdf_group* group) |
| 889 | { |
| 890 | return oscap_iterator_new(XITEM(group)->sub.group.requires_); |
| 891 | } |
| 892 | |
| 893 | bool xccdf_rule_add_requires(struct xccdf_rule* rule, struct oscap_stringlist* requires_) |
| 894 | { |
| 895 | oscap_list_add(XITEM(rule)->sub.rule.requires_, requires_); |
| 896 | return true; |
| 897 | } |
| 898 | bool xccdf_group_add_requires(struct xccdf_group* group, struct oscap_stringlist* requires_) |
| 899 | { |
| 900 | oscap_list_add(XITEM(group)->sub.group.requires_, requires_); |
| 901 | return true; |
| 902 | } |
| 903 | bool xccdf_item_add_requires(struct xccdf_item* item, struct oscap_stringlist* requires_) |
| 904 | { |
| 905 | if (item == NULL) return NULL; |
| 906 | if (item->type == XCCDF_RULE) return xccdf_rule_add_requires ( XRULE(item), requires_); |
| 907 | if (item->type == XCCDF_GROUP) return xccdf_group_add_requires(XGROUP(item), requires_); |
| 908 | return false; |
| 909 | } |
| 910 | |
| 911 | bool xccdf_rule_add_conflicts(struct xccdf_rule* rule, const char* conflicts) |
| 912 | { |
| 913 | oscap_stringlist_add_string((struct oscap_stringlist*)(XITEM(rule)->sub.rule.conflicts), conflicts); |
| 914 | return true; |
| 915 | } |
| 916 | bool xccdf_group_add_conflicts(struct xccdf_group* group, const char* conflicts) |
| 917 | { |
| 918 | oscap_stringlist_add_string((struct oscap_stringlist*)(XITEM(group)->sub.group.conflicts), conflicts); |
| 919 | return true; |
| 920 | } |
| 921 | bool xccdf_item_add_conflicts(struct xccdf_item* item, const char* conflicts) |
| 922 | { |
| 923 | if (item == NULL) return NULL; |
| 924 | if (item->type == XCCDF_RULE) return xccdf_rule_add_conflicts ( XRULE(item), conflicts); |
| 925 | if (item->type == XCCDF_GROUP) return xccdf_group_add_conflicts(XGROUP(item), conflicts); |
| 926 | return false; |
| 927 | } |
| 928 | |
| 929 | |
| 930 | void xccdf_rule_to_dom(struct xccdf_rule *rule, xmlNode *rule_node, xmlDoc *doc, xmlNode *parent) |
| 931 | { |
| 932 | const struct xccdf_version_info* version_info = xccdf_item_get_schema_version(XITEM(rule)); |
| 933 | xmlNs *ns_xccdf = lookup_xccdf_ns(doc, parent, version_info); |
| 934 | |
| 935 | /* Handle Attributes */ |
| 936 | const char *extends = xccdf_rule_get_extends(rule); |
| 937 | if (extends) |
| 938 | xmlNewProp(rule_node, BAD_CAST "extends", BAD_CAST extends); |
| 939 | |
| 940 | if (xccdf_rule_get_multiple(rule)) |
| 941 | xmlNewProp(rule_node, BAD_CAST "multiple", BAD_CAST "true"); |
| 942 | |
| 943 | if (xccdf_rule_get_selected(rule)) |
| 944 | xmlNewProp(rule_node, BAD_CAST "selected", BAD_CAST "true"); |
| 945 | else |
| 946 | xmlNewProp(rule_node, BAD_CAST "selected", BAD_CAST "false"); |
| 947 | |
| 948 | if (XITEM(rule)->item.defined_flags.weight) { |
| 949 | char *weight_str = oscap_sprintf("%f", xccdf_rule_get_weight(rule)); |
| 950 | xmlNewProp(rule_node, BAD_CAST "weight", BAD_CAST weight_str); |
| 951 | free(weight_str); |
| 952 | } |
| 953 | |
| 954 | xccdf_role_t role = xccdf_rule_get_role(rule); |
| 955 | if (role != 0) |
| 956 | xmlNewProp(rule_node, BAD_CAST "role", BAD_CAST XCCDF_ROLE_MAP[role - 1].string); |
| 957 | |
| 958 | xccdf_level_t severity = xccdf_rule_get_severity(rule); |
| 959 | if (severity != XCCDF_LEVEL_NOT_DEFINED) |
| 960 | xmlNewProp(rule_node, BAD_CAST "severity", BAD_CAST XCCDF_LEVEL_MAP[severity - 1].string); |
| 961 | |
| 962 | /* Handle Child Nodes */ |
| 963 | xccdf_texts_to_dom(xccdf_rule_get_rationale(rule), rule_node, "rationale"); |
| 964 | |
| 965 | struct oscap_string_iterator *platforms = xccdf_rule_get_platforms(rule); |
| 966 | while (oscap_string_iterator_has_more(platforms)) { |
| 967 | const char *platform = oscap_string_iterator_next(platforms); |
| 968 | xmlNode * child = xmlNewTextChild(rule_node, ns_xccdf, BAD_CAST "platform", BAD_CAST NULL); |
| 969 | xmlNewProp(child, BAD_CAST "idref", BAD_CAST platform); |
| 970 | } |
| 971 | oscap_string_iterator_free(platforms); |
| 972 | |
| 973 | struct oscap_stringlist_iterator *lists = xccdf_rule_get_requires(rule); |
| 974 | while (oscap_stringlist_iterator_has_more(lists)) { |
| 975 | struct oscap_stringlist *list = oscap_stringlist_iterator_next(lists); |
| 976 | struct oscap_string_iterator *strings = oscap_stringlist_get_strings(list); |
| 977 | struct oscap_string *all_requires = oscap_string_new(); |
| 978 | while (oscap_string_iterator_has_more(strings)) { |
| 979 | const char *requires = oscap_string_iterator_next(strings); |
| 980 | if (!oscap_string_empty(all_requires)) |
| 981 | oscap_string_append_string(all_requires, " "); |
| 982 | oscap_string_append_string(all_requires, requires); |
| 983 | } |
| 984 | const char *all_reqs = oscap_string_get_cstr(all_requires); |
| 985 | xmlNode * child = xmlNewTextChild(rule_node, ns_xccdf, BAD_CAST "requires", BAD_CAST NULL); |
| 986 | xmlNewProp(child, BAD_CAST "idref", BAD_CAST all_reqs); |
| 987 | oscap_string_free(all_requires); |
| 988 | oscap_string_iterator_free(strings); |
| 989 | } |
| 990 | oscap_stringlist_iterator_free(lists); |
| 991 | |
| 992 | struct oscap_string_iterator *conflicts = xccdf_rule_get_conflicts(rule); |
| 993 | while (oscap_string_iterator_has_more(conflicts)) { |
| 994 | const char *conflict = oscap_string_iterator_next(conflicts); |
| 995 | xmlNode * child = xmlNewTextChild(rule_node, ns_xccdf, BAD_CAST "conflicts", BAD_CAST NULL); |
| 996 | xmlNewProp(child, BAD_CAST "idref", BAD_CAST conflict); |
| 997 | } |
| 998 | oscap_string_iterator_free(conflicts); |
| 999 | |
| 1000 | struct xccdf_ident_iterator *idents = xccdf_rule_get_idents(rule); |
| 1001 | while (xccdf_ident_iterator_has_more(idents)) { |
| 1002 | struct xccdf_ident *ident = xccdf_ident_iterator_next(idents); |
| 1003 | xccdf_ident_to_dom(ident, doc, rule_node, version_info); |
| 1004 | } |
| 1005 | xccdf_ident_iterator_free(idents); |
| 1006 | |
| 1007 | struct xccdf_profile_note_iterator *notes = xccdf_rule_get_profile_notes(rule); |
| 1008 | while (xccdf_profile_note_iterator_has_more(notes)) { |
| 1009 | struct xccdf_profile_note *note = xccdf_profile_note_iterator_next(notes); |
| 1010 | xccdf_profile_note_to_dom(note, doc, rule_node); |
| 1011 | } |
| 1012 | xccdf_profile_note_iterator_free(notes); |
| 1013 | |
| 1014 | struct xccdf_fixtext_iterator *fixtexts = xccdf_rule_get_fixtexts(rule); |
| 1015 | while (xccdf_fixtext_iterator_has_more(fixtexts)) { |
| 1016 | struct xccdf_fixtext *fixtext = xccdf_fixtext_iterator_next(fixtexts); |
| 1017 | xccdf_fixtext_to_dom(fixtext, doc, rule_node); |
| 1018 | } |
| 1019 | xccdf_fixtext_iterator_free(fixtexts); |
| 1020 | |
| 1021 | struct xccdf_fix_iterator *fixes = xccdf_rule_get_fixes(rule); |
| 1022 | while (xccdf_fix_iterator_has_more(fixes)) { |
| 1023 | struct xccdf_fix *fix = xccdf_fix_iterator_next(fixes); |
| 1024 | xccdf_fix_to_dom(fix, doc, rule_node, version_info); |
| 1025 | } |
| 1026 | xccdf_fix_iterator_free(fixes); |
| 1027 | |
| 1028 | struct xccdf_check_iterator *checks = xccdf_rule_get_checks(rule); |
| 1029 | while (xccdf_check_iterator_has_more(checks)) { |
| 1030 | struct xccdf_check *check = xccdf_check_iterator_next(checks); |
| 1031 | xccdf_check_to_dom(check, doc, rule_node, version_info); |
| 1032 | } |
| 1033 | xccdf_check_iterator_free(checks); |
| 1034 | } |
| 1035 | |
| 1036 | void xccdf_group_to_dom(struct xccdf_group *group, xmlNode *group_node, xmlDoc *doc, xmlNode *parent) |
| 1037 | { |
| 1038 | const struct xccdf_version_info* version_info = xccdf_item_get_schema_version(XITEM(group)); |
| 1039 | xmlNs *ns_xccdf = lookup_xccdf_ns(doc, parent, version_info); |
| 1040 | |
| 1041 | /* Handle Attributes */ |
| 1042 | const char *extends = xccdf_group_get_extends(group); |
| 1043 | if (extends) |
| 1044 | xmlNewProp(group_node, BAD_CAST "extends", BAD_CAST extends); |
| 1045 | |
| 1046 | if (XITEM(group)->item.defined_flags.selected) { |
| 1047 | if (xccdf_group_get_selected(group)) |
| 1048 | xmlNewProp(group_node, BAD_CAST "selected", BAD_CAST "true"); |
| 1049 | else |
| 1050 | xmlNewProp(group_node, BAD_CAST "selected", BAD_CAST "false"); |
| 1051 | } |
| 1052 | |
| 1053 | if (XITEM(group)->item.defined_flags.weight) { |
| 1054 | float weight = xccdf_group_get_weight(group); |
| 1055 | char *weight_str = oscap_sprintf("%f", weight); |
| 1056 | xmlNewProp(group_node, BAD_CAST "weight", BAD_CAST weight_str); |
| 1057 | free(weight_str); |
| 1058 | } |
| 1059 | |
| 1060 | /* Handle Child Nodes */ |
| 1061 | xccdf_texts_to_dom(xccdf_group_get_rationale(group), group_node, "rationale"); |
| 1062 | |
| 1063 | struct oscap_string_iterator *platforms = xccdf_group_get_platforms(group); |
| 1064 | while (oscap_string_iterator_has_more(platforms)) { |
| 1065 | const char *platform = oscap_string_iterator_next(platforms); |
| 1066 | xmlNode * child = xmlNewTextChild(group_node, ns_xccdf, BAD_CAST "platform", BAD_CAST NULL); |
| 1067 | xmlNewProp(child, BAD_CAST "idref", BAD_CAST platform); |
| 1068 | } |
| 1069 | oscap_string_iterator_free(platforms); |
| 1070 | |
| 1071 | struct oscap_stringlist_iterator *lists = xccdf_group_get_requires(group); |
| 1072 | while (oscap_stringlist_iterator_has_more(lists)) { |
| 1073 | struct oscap_stringlist *list = oscap_stringlist_iterator_next(lists); |
| 1074 | struct oscap_string_iterator *strings = oscap_stringlist_get_strings(list); |
| 1075 | struct oscap_string *all_requires = oscap_string_new(); |
| 1076 | while (oscap_string_iterator_has_more(strings)) { |
| 1077 | const char *requires = oscap_string_iterator_next(strings); |
| 1078 | if (!oscap_string_empty(all_requires)) |
| 1079 | oscap_string_append_string(all_requires, " "); |
| 1080 | oscap_string_append_string(all_requires, requires); |
| 1081 | } |
| 1082 | const char *all_reqs = oscap_string_get_cstr(all_requires); |
| 1083 | xmlNode * child = xmlNewTextChild(group_node, ns_xccdf, BAD_CAST "requires", BAD_CAST NULL); |
| 1084 | xmlNewProp(child, BAD_CAST "idref", BAD_CAST all_reqs); |
| 1085 | oscap_string_free(all_requires); |
| 1086 | oscap_string_iterator_free(strings); |
| 1087 | } |
| 1088 | oscap_stringlist_iterator_free(lists); |
| 1089 | |
| 1090 | struct oscap_string_iterator *conflicts = xccdf_group_get_conflicts(group); |
| 1091 | while (oscap_string_iterator_has_more(conflicts)) { |
| 1092 | const char *conflict = oscap_string_iterator_next(conflicts); |
| 1093 | xmlNode * child = xmlNewTextChild(group_node, ns_xccdf, BAD_CAST "conflicts", BAD_CAST NULL); |
| 1094 | xmlNewProp(child, BAD_CAST "idref", BAD_CAST conflict); |
| 1095 | } |
| 1096 | oscap_string_iterator_free(conflicts); |
| 1097 | |
| 1098 | struct xccdf_value_iterator *values = xccdf_group_get_values(group); |
| 1099 | while (xccdf_value_iterator_has_more(values)) { |
| 1100 | struct xccdf_value *value = xccdf_value_iterator_next(values); |
| 1101 | if (XGROUP(xccdf_value_get_parent(value)) == group) { |
| 1102 | xccdf_item_to_dom((struct xccdf_item *)value, doc, group_node, version_info); |
| 1103 | } |
| 1104 | } |
| 1105 | xccdf_value_iterator_free(values); |
| 1106 | |
| 1107 | struct xccdf_item_iterator *items = xccdf_group_get_content(group); |
| 1108 | while (xccdf_item_iterator_has_more(items)) { |
| 1109 | struct xccdf_item *item = xccdf_item_iterator_next(items); |
| 1110 | if (XGROUP(xccdf_item_get_parent(item)) == group) { |
| 1111 | xccdf_item_to_dom(item, doc, group_node, version_info); |
| 1112 | } |
| 1113 | } |
| 1114 | xccdf_item_iterator_free(items); |
| 1115 | |
| 1116 | } |
| 1117 | |
| 1118 | struct oscap_stringlist *xccdf_rule_get_warnings_strings(struct xccdf_rule *rule) |
| 1119 | { |
| 1120 | struct oscap_stringlist *warnings_list = oscap_stringlist_new(); |
| 1121 | /* Handle generic item child nodes */ |
| 1122 | struct xccdf_warning_iterator *warnings = xccdf_rule_get_warnings(rule); |
| 1123 | while (xccdf_warning_iterator_has_more(warnings)) { |
| 1124 | struct xccdf_warning *warning = xccdf_warning_iterator_next(warnings); |
| 1125 | struct oscap_text *warning_text = xccdf_warning_get_text(warning); |
| 1126 | const char *warning_cstr = oscap_text_get_text(warning_text); |
| 1127 | char *warning_plaintext = _xhtml_to_plaintext(warning_cstr); |
| 1128 | oscap_stringlist_add_string(warnings_list, warning_plaintext); |
| 1129 | free(warning_plaintext); |
| 1130 | } |
| 1131 | xccdf_warning_iterator_free(warnings); |
| 1132 | return warnings_list; |
| 1133 | } |
| 1134 | |
| 1135 | XCCDF_STATUS_CURRENT(rule) |
| 1136 | XCCDF_STATUS_CURRENT(group) |
| 1137 | XCCDF_GROUP_IGETTER(item, content) |
| 1138 | XCCDF_GROUP_IGETTER(value, values) |
| 1139 | |
| 1140 | XCCDF_ITEM_ADDER_REG(group, rule, content) |
| 1141 | XCCDF_ITEM_ADDER_REG(group, group, content) |
| 1142 | XCCDF_ITEM_ADDER_REG(group, value, values) |
| 1143 | |
| 1144 | bool xccdf_group_add_content(struct xccdf_group *rule, struct xccdf_item *item) |
| 1145 | { |
| 1146 | if (item == NULL) return false; |
| 1147 | switch (xccdf_item_get_type(item)) { |
| 1148 | case XCCDF_RULE: return xccdf_group_add_rule (rule, XRULE(item)); |
| 1149 | case XCCDF_GROUP: return xccdf_group_add_group(rule, XGROUP(item)); |
| 1150 | case XCCDF_VALUE: return xccdf_group_add_value(rule, XVALUE(item)); |
| 1151 | default: return false; |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | XCCDF_ACCESSOR_STRING(rule, impact_metric) |
| 1156 | XCCDF_ACCESSOR_SIMPLE(rule, xccdf_role_t, role) |
| 1157 | XCCDF_ACCESSOR_SIMPLE(rule, xccdf_level_t, severity) |
| 1158 | XCCDF_LISTMANIP(rule, ident, idents) |
| 1159 | XCCDF_LISTMANIP(rule, check, checks) |
| 1160 | XCCDF_LISTMANIP(rule, profile_note, profile_notes) |
| 1161 | XCCDF_LISTMANIP(rule, fix, fixes) |
| 1162 | XCCDF_LISTMANIP(rule, fixtext, fixtexts) |
| 1163 | XCCDF_ITERATOR_GEN_S(ident) |
| 1164 | |
| 1165 | OSCAP_ACCESSOR_STRING(xccdf_check, id) |
| 1166 | OSCAP_ACCESSOR_STRING(xccdf_check, system) |
| 1167 | OSCAP_ACCESSOR_STRING(xccdf_check, selector) |
| 1168 | OSCAP_ACCESSOR_STRING(xccdf_check, content) |
| 1169 | OSCAP_ACCESSOR_EXP(bool, xccdf_check, multicheck, flags.multicheck) |
| 1170 | OSCAP_ACCESSOR_EXP(bool, xccdf_check, negate, flags.negate); |
| 1171 | OSCAP_ACCESSOR_SIMPLE(xccdf_bool_operator_t, xccdf_check, oper) |
| 1172 | OSCAP_IGETINS(xccdf_check_import, xccdf_check, imports, import) |
| 1173 | OSCAP_IGETINS(xccdf_check_export, xccdf_check, exports, export) |
| 1174 | OSCAP_IGETINS(xccdf_check_content_ref, xccdf_check, content_refs, content_ref) |
| 1175 | OSCAP_IGETINS(xccdf_check, xccdf_check, children, child) |
| 1176 | XCCDF_ITERATOR_GEN_S(check_content_ref) |
| 1177 | XCCDF_ITERATOR_GEN_S(check_export) XCCDF_ITERATOR_GEN_S(check_import) XCCDF_ITERATOR_GEN_S(check) |
| 1178 | |
| 1179 | OSCAP_ACCESSOR_STRING(xccdf_profile_note, reftag) OSCAP_ACCESSOR_TEXT(xccdf_profile_note, text) |
| 1180 | OSCAP_ACCESSOR_STRING(xccdf_check_import, name) OSCAP_ACCESSOR_STRING(xccdf_check_import, xpath) |
| 1181 | OSCAP_ACCESSOR_STRING(xccdf_check_import, content) |
| 1182 | OSCAP_ACCESSOR_STRING(xccdf_check_export, name) OSCAP_ACCESSOR_STRING(xccdf_check_export, value) |
| 1183 | OSCAP_ACCESSOR_STRING(xccdf_check_content_ref, name) OSCAP_ACCESSOR_STRING(xccdf_check_content_ref, href) |
| 1184 | |
| 1185 | OSCAP_ACCESSOR_SIMPLE(xccdf_strategy_t, xccdf_fixtext, strategy) |
| 1186 | OSCAP_ACCESSOR_SIMPLE(xccdf_level_t, xccdf_fixtext, disruption) |
| 1187 | OSCAP_ACCESSOR_SIMPLE(xccdf_level_t, xccdf_fixtext, complexity) |
| 1188 | OSCAP_ACCESSOR_SIMPLE(bool, xccdf_fixtext, reboot) |
| 1189 | OSCAP_ACCESSOR_TEXT(xccdf_fixtext, text) |
| 1190 | OSCAP_ACCESSOR_STRING(xccdf_fixtext, fixref) |
| 1191 | XCCDF_ITERATOR_GEN_S(fixtext) |
| 1192 | |
| 1193 | OSCAP_ACCESSOR_SIMPLE(xccdf_strategy_t, xccdf_fix, strategy) |
| 1194 | OSCAP_ACCESSOR_SIMPLE(xccdf_level_t, xccdf_fix, disruption) |
| 1195 | OSCAP_ACCESSOR_SIMPLE(xccdf_level_t, xccdf_fix, complexity) |
| 1196 | OSCAP_ACCESSOR_SIMPLE(bool, xccdf_fix, reboot) |
| 1197 | OSCAP_ACCESSOR_STRING(xccdf_fix, content) |
| 1198 | OSCAP_ACCESSOR_STRING(xccdf_fix, system) |
| 1199 | OSCAP_ACCESSOR_STRING(xccdf_fix, platform) |
| 1200 | OSCAP_ACCESSOR_STRING(xccdf_fix, id) |
| 1201 | XCCDF_ITERATOR_GEN_S(fix) |
| 1202 |