| 1 | /* |
| 2 | * Copyright 2013 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | */ |
| 20 | #ifdef HAVE_CONFIG_H |
| 21 | #include "config.h" |
| 22 | #endif |
| 23 | |
| 24 | #include <string.h> |
| 25 | #include <ctype.h> |
| 26 | #include <errno.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
| 29 | |
| 30 | #ifdef OSCAP_UNIX |
| 31 | #include <sys/wait.h> |
| 32 | #endif |
| 33 | |
| 34 | #ifdef OS_WINDOWS |
| 35 | #include <io.h> |
| 36 | #else |
| 37 | #include <unistd.h> |
| 38 | #endif |
| 39 | |
| 40 | #include <libxml/tree.h> |
| 41 | |
| 42 | #include "XCCDF/item.h" |
| 43 | #include "common/_error.h" |
| 44 | #include "common/debug_priv.h" |
| 45 | #include "common/oscap_acquire.h" |
| 46 | #include "common/oscap_pcre.h" |
| 47 | #include "xccdf_policy_priv.h" |
| 48 | #include "xccdf_policy_model_priv.h" |
| 49 | #include "public/xccdf_policy.h" |
| 50 | #include "oscap_helpers.h" |
| 51 | |
| 52 | struct kickstart_commands { |
| 53 | struct oscap_list *package_install; |
| 54 | struct oscap_list *package_remove; |
| 55 | struct oscap_list *service_enable; |
| 56 | struct oscap_list *service_disable; |
| 57 | struct oscap_list *pre; |
| 58 | struct oscap_list *post; |
| 59 | struct oscap_list *logvol; |
| 60 | struct oscap_list *bootloader; |
| 61 | struct oscap_list *firewall_enable; |
| 62 | struct oscap_list *firewall_disable; |
| 63 | bool enable_kdump; |
| 64 | }; |
| 65 | |
| 66 | struct logvol_cmd { |
| 67 | char *path; |
| 68 | char *size; |
| 69 | }; |
| 70 | |
| 71 | struct bootc_commands { |
| 72 | struct oscap_list *dnf_install; |
| 73 | struct oscap_list *dnf_remove; |
| 74 | }; |
| 75 | |
| 76 | static int _rule_add_info_message(struct xccdf_rule_result *rr, ...) |
| 77 | { |
| 78 | va_list ap; |
| 79 | const char *fmt; |
| 80 | char *text; |
| 81 | struct xccdf_message *msg; |
| 82 | |
| 83 | va_start(ap, rr); |
| 84 | fmt = va_arg(ap, const char *); |
| 85 | text = oscap_vsprintf(fmt, ap); |
| 86 | va_end(ap); |
| 87 | |
| 88 | msg = xccdf_message_new(); |
| 89 | xccdf_message_set_content(msg, text); |
| 90 | dI("[%s]->msg: %s", xccdf_rule_result_get_idref(rr), text); |
| 91 | free(text); |
| 92 | xccdf_message_set_severity(msg, XCCDF_MSG_INFO); |
| 93 | xccdf_rule_result_add_message(rr, msg); |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static inline bool _file_exists(const char *file) |
| 98 | { |
| 99 | struct stat sb; |
| 100 | return file != NULL && stat(file, &sb) == 0; |
| 101 | } |
| 102 | |
| 103 | static int _write_text_to_fd(int output_fd, const char* text) { |
| 104 | |
| 105 | ssize_t written = 0; |
| 106 | const ssize_t length = strlen(text); |
| 107 | |
| 108 | while (written < length) { |
| 109 | ssize_t w = write(output_fd, text + written, length - written); |
| 110 | if (w < 0) |
| 111 | break; |
| 112 | written += w; |
| 113 | } |
| 114 | |
| 115 | return written != length; |
| 116 | |
| 117 | } |
| 118 | |
| 119 | static int _write_text_to_fd_and_free(int output_fd, char *text) |
| 120 | { |
| 121 | const int ret = _write_text_to_fd(output_fd, text); |
| 122 | free(text); |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | static int _write_remediation_to_fd_and_free(int output_fd, const char* template, char* text) |
| 127 | { |
| 128 | if (text == NULL) |
| 129 | return 0; |
| 130 | if (oscap_streq(template, "urn:xccdf:fix:script:ansible")) { |
| 131 | // Add required indentation in front of every single line |
| 132 | |
| 133 | const char delim = '\n'; |
| 134 | const char *indentation = " "; |
| 135 | |
| 136 | char *current = text; |
| 137 | char *next_delim = NULL; |
| 138 | char *end = NULL; |
| 139 | |
| 140 | do { |
| 141 | next_delim = strchr(current, delim); |
| 142 | if (next_delim != NULL) { |
| 143 | *next_delim = '\0'; |
| 144 | } |
| 145 | |
| 146 | // remove all trailing whitespaces |
| 147 | size_t len = strlen(current); |
| 148 | if (len > 0) { |
| 149 | end = current + len - 1; |
| 150 | while (isspace(*end)) { |
| 151 | *end = '\0'; |
| 152 | if (end == current) |
| 153 | break; |
| 154 | end--; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (strlen(current) > 0) { |
| 159 | // write indentation |
| 160 | if (_write_text_to_fd(output_fd, indentation) != 0) { |
| 161 | free(text); |
| 162 | return 1; |
| 163 | } |
| 164 | if (_write_text_to_fd(output_fd, current) != 0) { |
| 165 | free(text); |
| 166 | return 1; |
| 167 | } |
| 168 | } |
| 169 | if (_write_text_to_fd(output_fd, "\n") != 0) { |
| 170 | free(text); |
| 171 | return 1; |
| 172 | } |
| 173 | |
| 174 | if (next_delim != NULL) { |
| 175 | // text is NULL terminated to this is guaranteed to point to valid memory |
| 176 | current = next_delim + 1; |
| 177 | } |
| 178 | } while (next_delim != NULL); |
| 179 | |
| 180 | if (_write_text_to_fd(output_fd, "\n") != 0) { |
| 181 | free(text); |
| 182 | return 1; |
| 183 | } |
| 184 | |
| 185 | free(text); |
| 186 | return 0; |
| 187 | |
| 188 | } else { |
| 189 | // no extra processing is needed |
| 190 | return _write_text_to_fd_and_free(output_fd, text); |
| 191 | } |
| 192 | } |
| 193 | struct _interpret_map { |
| 194 | const char *sys; |
| 195 | const char *interpret; |
| 196 | }; |
| 197 | |
| 198 | typedef const char * (*_search_interpret_map_fn) (const char *, const struct _interpret_map *); |
| 199 | |
| 200 | static const char *_search_interpret_map(const char *sys, const struct _interpret_map *map) |
| 201 | { |
| 202 | const struct _interpret_map *mapptr; |
| 203 | for (mapptr = map; mapptr->sys != NULL; ++mapptr) |
| 204 | if (oscap_streq(mapptr->sys, sys)) |
| 205 | return mapptr->interpret; |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | static const char *_get_supported_interpret(const char *sys, const struct _interpret_map *unused) |
| 210 | { |
| 211 | static const struct _interpret_map _openscap_supported_interprets[] = { |
| 212 | #if defined(OS_FREEBSD) |
| 213 | {"urn:xccdf:fix:commands", "/usr/local/bin/bash"}, |
| 214 | {"urn:xccdf:fix:script:sh", "/usr/local/bin/bash"}, |
| 215 | {"urn:xccdf:fix:script:perl", "/usr/local/bin/perl"}, |
| 216 | #else |
| 217 | {"urn:xccdf:fix:commands", "/bin/bash"}, |
| 218 | {"urn:xccdf:fix:script:sh", "/bin/bash"}, |
| 219 | {"urn:xccdf:fix:script:perl", "/usr/bin/perl"}, |
| 220 | #endif |
| 221 | |
| 222 | #ifdef PREFERRED_PYTHON_PATH |
| 223 | {"urn:xccdf:fix:script:python", PREFERRED_PYTHON_PATH}, |
| 224 | #endif |
| 225 | #ifdef PYTHON2_PATH |
| 226 | {"urn:xccdf:fix:script:python2", PYTHON2_PATH}, |
| 227 | #endif |
| 228 | #ifdef PYTHON3_PATH |
| 229 | {"urn:xccdf:fix:script:python3", PYTHON3_PATH}, |
| 230 | #endif |
| 231 | {"urn:xccdf:fix:script:csh", "/bin/csh"}, |
| 232 | {"urn:xccdf:fix:script:tclsh", "/usr/bin/tclsh"}, |
| 233 | {"urn:xccdf:fix:script:javascript", "/usr/bin/js"}, |
| 234 | |
| 235 | // Current Ansible remediations are only Ansible snippets and are |
| 236 | // not runnable without header. |
| 237 | // {"urn:xccdf:fix:script:ansible", "/usr/bin/ansible-playbook"}, |
| 238 | {NULL, NULL} |
| 239 | }; |
| 240 | const char *interpret = _search_interpret_map(sys, _openscap_supported_interprets); |
| 241 | return _file_exists(interpret) ? interpret : NULL; |
| 242 | } |
| 243 | |
| 244 | static inline struct xccdf_rule *_lookup_rule_by_rule_result(const struct xccdf_policy *policy, const struct xccdf_rule_result *rr) |
| 245 | { |
| 246 | const struct xccdf_benchmark *benchmark = xccdf_policy_get_benchmark(policy); |
| 247 | if (benchmark == NULL) |
| 248 | return NULL; |
| 249 | return (struct xccdf_rule *) xccdf_benchmark_get_item(benchmark, xccdf_rule_result_get_idref(rr)); |
| 250 | } |
| 251 | |
| 252 | static inline bool _is_platform_applicable(struct xccdf_policy *policy, const char *platform) |
| 253 | { |
| 254 | if (oscap_streq("", platform)) |
| 255 | return true; |
| 256 | struct oscap_stringlist *platform_list = oscap_stringlist_new(); |
| 257 | oscap_stringlist_add_string(platform_list, platform); |
| 258 | struct oscap_string_iterator *platform_it = oscap_stringlist_get_strings(platform_list); |
| 259 | bool ret = xccdf_policy_model_platforms_are_applicable(xccdf_policy_get_model(policy), platform_it); |
| 260 | oscap_string_iterator_free(platform_it); |
| 261 | oscap_stringlist_free(platform_list); |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | static struct oscap_list *_get_fixes(struct xccdf_policy *policy, const struct xccdf_rule *rule) |
| 266 | { |
| 267 | struct oscap_list *result = oscap_list_new(); |
| 268 | |
| 269 | struct xccdf_fix_iterator *fix_it = xccdf_rule_get_fixes(rule); |
| 270 | while (xccdf_fix_iterator_has_more(fix_it)) { |
| 271 | struct xccdf_fix *fix = xccdf_fix_iterator_next(fix_it); |
| 272 | oscap_list_add(result, fix); |
| 273 | } |
| 274 | xccdf_fix_iterator_free(fix_it); |
| 275 | return result; |
| 276 | } |
| 277 | |
| 278 | static struct oscap_list *_filter_fixes_by_applicability(struct xccdf_policy *policy, const struct xccdf_rule *rule) |
| 279 | { |
| 280 | /* Filters out the fixes which are not applicable */ |
| 281 | struct oscap_list *result = oscap_list_new(); |
| 282 | if (!xccdf_policy_model_item_is_applicable(xccdf_policy_get_model(policy), (struct xccdf_item *) rule)) |
| 283 | /* The fix element is applicable only when the all the parent elements are. */ |
| 284 | return result; |
| 285 | struct xccdf_fix_iterator *fix_it = xccdf_rule_get_fixes(rule); |
| 286 | while (xccdf_fix_iterator_has_more(fix_it)) { |
| 287 | struct xccdf_fix *fix = xccdf_fix_iterator_next(fix_it); |
| 288 | const char *platform = xccdf_fix_get_platform(fix); |
| 289 | if (_is_platform_applicable(policy, platform)) |
| 290 | oscap_list_add(result, fix); |
| 291 | } |
| 292 | xccdf_fix_iterator_free(fix_it); |
| 293 | return result; |
| 294 | } |
| 295 | |
| 296 | static struct oscap_list *_filter_fixes_by_system(struct oscap_list *fixes, _search_interpret_map_fn filter, const struct _interpret_map *allowed_systems) |
| 297 | { |
| 298 | struct oscap_iterator *fix_it = oscap_iterator_new(fixes); |
| 299 | while (oscap_iterator_has_more(fix_it)) { |
| 300 | struct xccdf_fix *fix = (struct xccdf_fix *) oscap_iterator_next(fix_it); |
| 301 | const char *sys = xccdf_fix_get_system(fix); |
| 302 | if (sys == NULL) |
| 303 | sys = ""; |
| 304 | if (filter(sys, allowed_systems) == NULL) |
| 305 | oscap_iterator_detach(fix_it); |
| 306 | } |
| 307 | oscap_iterator_free(fix_it); |
| 308 | return fixes; |
| 309 | } |
| 310 | |
| 311 | static struct oscap_list *_filter_fixes_by_distruption_and_reboot(struct oscap_list *fixes) |
| 312 | { |
| 313 | bool reboot = true; // Let's assuming worse case and flip when fix/@rebot=false is found |
| 314 | xccdf_level_t disruption = XCCDF_HIGH; |
| 315 | |
| 316 | struct oscap_iterator *fix_it = oscap_iterator_new(fixes); |
| 317 | while (oscap_iterator_has_more(fix_it)) { |
| 318 | struct xccdf_fix *fix = (struct xccdf_fix *) oscap_iterator_next(fix_it); |
| 319 | if (!xccdf_fix_get_reboot(fix)) |
| 320 | reboot = false; |
| 321 | } |
| 322 | oscap_iterator_reset(fix_it); |
| 323 | while (oscap_iterator_has_more(fix_it)) { |
| 324 | struct xccdf_fix *fix = (struct xccdf_fix *) oscap_iterator_next(fix_it); |
| 325 | if (reboot == false && xccdf_fix_get_reboot(fix)) { |
| 326 | oscap_iterator_detach(fix_it); |
| 327 | } else { |
| 328 | xccdf_level_t dis = xccdf_fix_get_disruption(fix); |
| 329 | if (dis == XCCDF_MEDIUM || dis == XCCDF_LOW) |
| 330 | // Preferring "medium" and "low" over any other |
| 331 | disruption = dis; |
| 332 | } |
| 333 | } |
| 334 | if (disruption == XCCDF_MEDIUM || disruption == XCCDF_LOW) { |
| 335 | oscap_iterator_reset(fix_it); |
| 336 | while (oscap_iterator_has_more(fix_it)) { |
| 337 | struct xccdf_fix *fix = (struct xccdf_fix *) oscap_iterator_next(fix_it); |
| 338 | if (disruption != xccdf_fix_get_disruption(fix)) |
| 339 | oscap_iterator_detach(fix_it); |
| 340 | } |
| 341 | } |
| 342 | oscap_iterator_free(fix_it); |
| 343 | return fixes; |
| 344 | } |
| 345 | |
| 346 | static inline struct xccdf_fix *_find_suitable_fix(struct xccdf_policy *policy, struct xccdf_rule_result *rr) |
| 347 | { |
| 348 | /* In XCCDF 1.2, there is nothing like a default fix. However we use |
| 349 | * the following heuristics to find out some suitable fix: |
| 350 | * - remove fixes which are not appplicable (CPE) |
| 351 | * - remove fixes we cannot execute |
| 352 | * - choose fixes with the least disruption |
| 353 | * - choose fixes which do not require reboot |
| 354 | * - choose the first fix |
| 355 | */ |
| 356 | struct xccdf_fix *fix = NULL; |
| 357 | const struct xccdf_rule *rule = _lookup_rule_by_rule_result(policy, rr); |
| 358 | if (rule == NULL) |
| 359 | return NULL; |
| 360 | struct oscap_list *fixes = _filter_fixes_by_applicability(policy, rule); |
| 361 | fixes = _filter_fixes_by_system(fixes, _get_supported_interpret, NULL); |
| 362 | fixes = _filter_fixes_by_distruption_and_reboot(fixes); |
| 363 | struct xccdf_fix_iterator *fix_it = oscap_iterator_new(fixes); |
| 364 | if (xccdf_fix_iterator_has_more(fix_it)) |
| 365 | fix = xccdf_fix_iterator_next(fix_it); |
| 366 | xccdf_fix_iterator_free(fix_it); |
| 367 | oscap_list_free0(fixes); |
| 368 | return fix; |
| 369 | } |
| 370 | |
| 371 | static inline int _xccdf_fix_decode_xml(struct xccdf_fix *fix, char **result) |
| 372 | { |
| 373 | /* We need to decode & and similar sequences. That is a process reverse |
| 374 | * to the xmlEncodeSpecialChars()). Further we need to drop XML commentaries |
| 375 | * and expand CDATA blobs. |
| 376 | */ |
| 377 | *result = NULL; |
| 378 | char *str = oscap_sprintf("<x xmlns:xhtml='http://www.w3.org/1999/xhtml'>%s</x>", |
| 379 | xccdf_fix_get_content(fix)); |
| 380 | xmlDoc *doc = xmlReadMemory(str, strlen(str), NULL, NULL, XML_PARSE_RECOVER | |
| 381 | XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET | XML_PARSE_NSCLEAN); |
| 382 | dI("Following script will be executed: '''%s'''", str); |
| 383 | free(str); |
| 384 | |
| 385 | xmlBuffer *buff = xmlBufferCreate(); |
| 386 | xmlNodePtr child = xmlDocGetRootElement(doc)->children; |
| 387 | while (child != NULL) { |
| 388 | switch (child->type) { |
| 389 | case XML_ELEMENT_NODE:{ |
| 390 | /* Remaining child elements are suspicious. Perhaps it is an unresolved |
| 391 | * substitution element The execution would be dangerous, i.e. bash could |
| 392 | * interpret < and > characters of the element as pipe commands. */ |
| 393 | xmlFreeDoc(doc); |
| 394 | xmlBufferFree(buff); |
| 395 | return 1; |
| 396 | }; break; |
| 397 | case XML_TEXT_NODE: |
| 398 | case XML_CDATA_SECTION_NODE:{ |
| 399 | xmlNodeBufGetContent(buff, child); |
| 400 | }; break; |
| 401 | default: |
| 402 | break; |
| 403 | } |
| 404 | child = child->next; |
| 405 | } |
| 406 | xmlFreeDoc(doc); |
| 407 | *result = oscap_strdup((char *)xmlBufferContent(buff)); |
| 408 | xmlBufferFree(buff); |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | #if defined(unix) || defined(__unix__) || defined(__unix) |
| 413 | static inline int _xccdf_fix_execute(struct xccdf_rule_result *rr, struct xccdf_fix *fix) |
| 414 | { |
| 415 | if (rr == NULL) { |
| 416 | return 1; |
| 417 | } |
| 418 | |
| 419 | if (fix == NULL || oscap_streq(xccdf_fix_get_content(fix), NULL)) { |
| 420 | _rule_add_info_message(rr, "No fix available."); |
| 421 | return 1; |
| 422 | } |
| 423 | |
| 424 | const char *interpret = NULL; |
| 425 | if ((interpret = _get_supported_interpret(xccdf_fix_get_system(fix), NULL)) == NULL) { |
| 426 | _rule_add_info_message(rr, "Not supported xccdf:fix/@system='%s' or missing interpreter.", |
| 427 | xccdf_fix_get_system(fix) == NULL ? "" : xccdf_fix_get_system(fix)); |
| 428 | return 1; |
| 429 | } |
| 430 | |
| 431 | char *fix_text = NULL; |
| 432 | if (_xccdf_fix_decode_xml(fix, &fix_text) != 0) { |
| 433 | _rule_add_info_message(rr, "Fix element contains unresolved child elements."); |
| 434 | return 1; |
| 435 | } |
| 436 | |
| 437 | int result = 1; |
| 438 | |
| 439 | char *temp_dir = oscap_acquire_temp_dir(); |
| 440 | if (temp_dir == NULL) |
| 441 | goto cleanup; |
| 442 | // TODO: Directory and files shall be labeled with SELinux to prevent |
| 443 | // confined processes with less priviledges to transit to oscap domain |
| 444 | // and become basically unconfined. |
| 445 | char *temp_file = NULL; |
| 446 | int fd = oscap_acquire_temp_file(temp_dir, "fix-XXXXXXXX", &temp_file); |
| 447 | if (fd == -1) { |
| 448 | _rule_add_info_message(rr, "mkstemp failed: %s", strerror(errno)); |
| 449 | free(temp_file); |
| 450 | goto cleanup; |
| 451 | } |
| 452 | |
| 453 | if (_write_text_to_fd(fd, fix_text) != 0) { |
| 454 | _rule_add_info_message(rr, "Could not write to the temp file: %s", strerror(errno)); |
| 455 | (void) close(fd); |
| 456 | free(temp_file); |
| 457 | goto cleanup; |
| 458 | } |
| 459 | |
| 460 | if (close(fd) != 0) |
| 461 | _rule_add_info_message(rr, "Could not close temp file: %s", strerror(errno)); |
| 462 | |
| 463 | int pipefd[2]; |
| 464 | if (pipe(pipefd) == -1) { |
| 465 | _rule_add_info_message(rr, "Could not create pipe: %s", strerror(errno)); |
| 466 | free(temp_file); |
| 467 | goto cleanup; |
| 468 | } |
| 469 | |
| 470 | int fork_result = fork(); |
| 471 | if (fork_result >= 0) { |
| 472 | /* fork succeeded */ |
| 473 | if (fork_result == 0) { |
| 474 | /* Execute fix and forward output to the parent. */ |
| 475 | close(pipefd[0]); |
| 476 | dup2(pipefd[1], fileno(stdout)); |
| 477 | dup2(pipefd[1], fileno(stderr)); |
| 478 | close(pipefd[1]); |
| 479 | |
| 480 | char *const argvp[3] = { |
| 481 | (char *)interpret, |
| 482 | temp_file, |
| 483 | NULL |
| 484 | }; |
| 485 | |
| 486 | // We are inheriting openscap environment |
| 487 | execve(interpret, argvp, environ); |
| 488 | /* Wow, execve returned. In this special case, we failed to execute the fix |
| 489 | * and we return 0 from function. At least the following error message will |
| 490 | * indicate the problem in xccdf:message. */ |
| 491 | printf("Error while executing fix script: execve returned: %s\n", strerror(errno)); |
| 492 | exit(42); |
| 493 | } else { |
| 494 | free(temp_file); |
| 495 | close(pipefd[1]); |
| 496 | char *stdout_buff = oscap_acquire_pipe_to_string(pipefd[0]); |
| 497 | int wstatus; |
| 498 | waitpid(fork_result, &wstatus, 0); |
| 499 | _rule_add_info_message(rr, "Fix execution completed and returned: %d", WEXITSTATUS(wstatus)); |
| 500 | if (stdout_buff != NULL && stdout_buff[0] != '\0') |
| 501 | _rule_add_info_message(rr, stdout_buff); |
| 502 | free(stdout_buff); |
| 503 | /* We return zero to indicate success. Rather than returning the exit code. */ |
| 504 | result = 0; |
| 505 | } |
| 506 | } else { |
| 507 | _rule_add_info_message(rr, "Failed to fork. %s", strerror(errno)); |
| 508 | free(temp_file); |
| 509 | } |
| 510 | |
| 511 | cleanup: |
| 512 | oscap_acquire_cleanup_dir(&temp_dir); |
| 513 | free(fix_text); |
| 514 | return result; |
| 515 | } |
| 516 | #else |
| 517 | static inline int _xccdf_fix_execute(struct xccdf_rule_result *rr, struct xccdf_fix *fix) |
| 518 | { |
| 519 | if (rr == NULL) { |
| 520 | return 1; |
| 521 | } |
| 522 | |
| 523 | if (fix == NULL || oscap_streq(xccdf_fix_get_content(fix), NULL)) { |
| 524 | _rule_add_info_message(rr, "No fix available."); |
| 525 | return 1; |
| 526 | } else { |
| 527 | _rule_add_info_message(rr, "Cannot execute the fix script: not implemented"); |
| 528 | } |
| 529 | |
| 530 | return 1; |
| 531 | } |
| 532 | #endif |
| 533 | |
| 534 | int xccdf_policy_rule_result_remediate(struct xccdf_policy *policy, struct xccdf_rule_result *rr, struct xccdf_fix *fix, struct xccdf_result *test_result) |
| 535 | { |
| 536 | if (policy == NULL || rr == NULL) |
| 537 | return 1; |
| 538 | if (xccdf_rule_result_get_result(rr) != XCCDF_RESULT_FAIL) |
| 539 | return 0; |
| 540 | |
| 541 | // if a miscellaneous error happens (fix unsuitable or if we want to skip it for any reason |
| 542 | // we set misc_error to one, and the fix will be reported as error (and not skipped without log like before) |
| 543 | int misc_error=0; |
| 544 | |
| 545 | if (fix == NULL) { |
| 546 | fix = _find_suitable_fix(policy, rr); |
| 547 | if (fix == NULL) { |
| 548 | // We want to append xccdf:message about missing fix. |
| 549 | _rule_add_info_message(rr, "No suitable fix found."); |
| 550 | xccdf_rule_result_set_result(rr, XCCDF_RESULT_FAIL); |
| 551 | misc_error=1; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | struct xccdf_check *check = NULL; |
| 556 | struct xccdf_check_iterator *check_it = xccdf_rule_result_get_checks(rr); |
| 557 | while (xccdf_check_iterator_has_more(check_it)) |
| 558 | check = xccdf_check_iterator_next(check_it); |
| 559 | xccdf_check_iterator_free(check_it); |
| 560 | |
| 561 | if(misc_error == 0){ |
| 562 | /* Initialize the fix. */ |
| 563 | struct xccdf_fix *cfix = xccdf_fix_clone(fix); |
| 564 | int res = xccdf_policy_resolve_fix_substitution(policy, cfix, rr, test_result); |
| 565 | xccdf_rule_result_add_fix(rr, cfix); |
| 566 | if (res != 0) { |
| 567 | _rule_add_info_message(rr, "Fix execution was aborted: Text substitution failed."); |
| 568 | xccdf_rule_result_set_result(rr, XCCDF_RESULT_ERROR); |
| 569 | misc_error=1; |
| 570 | }else{ |
| 571 | |
| 572 | /* Execute the fix. */ |
| 573 | res = _xccdf_fix_execute(rr, cfix); |
| 574 | if (res != 0) { |
| 575 | _rule_add_info_message(rr, "Fix was not executed. Execution was aborted."); |
| 576 | xccdf_rule_result_set_result(rr, XCCDF_RESULT_ERROR); |
| 577 | misc_error=1; |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | /* We report rule during remediation even if fix isn't executed due to a miscellaneous error */ |
| 583 | int report = 0; |
| 584 | struct xccdf_rule *rule = _lookup_rule_by_rule_result(policy, rr); |
| 585 | if (rule == NULL) { |
| 586 | // Sadly, we cannot handle this since b9d123d53140c6e369b7f2206e4e3e63dc556fd1. |
| 587 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not find xccdf:Rule/@id=%s.", xccdf_rule_result_get_idref(rr)); |
| 588 | } |
| 589 | else { |
| 590 | report = xccdf_policy_report_cb(policy, XCCDF_POLICY_OUTCB_START, (void *) rule); |
| 591 | if (report != 0) |
| 592 | return report; |
| 593 | } |
| 594 | |
| 595 | if(misc_error == 0){ |
| 596 | /* Verify fix if applied by calling OVAL again */ |
| 597 | if (check == NULL) { |
| 598 | xccdf_rule_result_set_result(rr, XCCDF_RESULT_ERROR); |
| 599 | _rule_add_info_message(rr, "Failed to verify applied fix: Missing xccdf:check."); |
| 600 | } else { |
| 601 | int new_result = xccdf_policy_check_evaluate(policy, check); |
| 602 | if (new_result == XCCDF_RESULT_PASS) |
| 603 | xccdf_rule_result_set_result(rr, XCCDF_RESULT_FIXED); |
| 604 | else { |
| 605 | xccdf_rule_result_set_result(rr, XCCDF_RESULT_ERROR); |
| 606 | _rule_add_info_message(rr, "Failed to verify applied fix: Checking engine returns: %s", |
| 607 | new_result <= 0 ? "internal error" : xccdf_test_result_type_get_text(new_result)); |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | xccdf_rule_result_set_time_current(rr); |
| 613 | return rule == NULL ? 0 : xccdf_policy_report_cb(policy, XCCDF_POLICY_OUTCB_END, (void *) rr); |
| 614 | } |
| 615 | |
| 616 | int xccdf_policy_remediate(struct xccdf_policy *policy, struct xccdf_result *result) |
| 617 | { |
| 618 | __attribute__nonnull__(result); |
| 619 | struct xccdf_rule_result_iterator *rr_it = xccdf_result_get_rule_results(result); |
| 620 | while (xccdf_rule_result_iterator_has_more(rr_it)) { |
| 621 | struct xccdf_rule_result *rr = xccdf_rule_result_iterator_next(rr_it); |
| 622 | xccdf_policy_rule_result_remediate(policy, rr, NULL, result); |
| 623 | } |
| 624 | xccdf_rule_result_iterator_free(rr_it); |
| 625 | xccdf_result_set_end_time_current(result); |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | /* --- Follows functions for generating XCCDF:Fix script --- */ |
| 630 | static const struct xccdf_fix *_find_fix_for_template(struct xccdf_policy *policy, struct xccdf_rule *rule, const char *template) |
| 631 | { |
| 632 | struct xccdf_fix *fix = NULL; |
| 633 | struct oscap_list *fixes = _get_fixes(policy, rule); |
| 634 | |
| 635 | if (template) { |
| 636 | const struct _interpret_map map[] = { {template, "Cloud!"}, |
| 637 | {NULL, NULL}}; |
| 638 | fixes = _filter_fixes_by_system(fixes, _search_interpret_map, map); |
| 639 | } |
| 640 | fixes = _filter_fixes_by_distruption_and_reboot(fixes); |
| 641 | struct xccdf_fix_iterator *fix_it = oscap_iterator_new(fixes); |
| 642 | if (xccdf_fix_iterator_has_more(fix_it)) |
| 643 | fix = xccdf_fix_iterator_next(fix_it); |
| 644 | xccdf_fix_iterator_free(fix_it); |
| 645 | oscap_list_free0(fixes); |
| 646 | return fix; |
| 647 | } |
| 648 | |
| 649 | static int _write_fix_header_to_fd(const char *sys, int output_fd, struct xccdf_rule *rule, unsigned int current, unsigned int total) |
| 650 | { |
| 651 | if (oscap_streq(sys, "") || oscap_streq(sys, "urn:xccdf:fix:script:sh") || oscap_streq(sys, "urn:xccdf:fix:commands")) { |
| 652 | char *fix_header = oscap_sprintf( |
| 653 | "###############################################################################\n" |
| 654 | "# BEGIN fix (%i / %i) for '%s'\n" |
| 655 | "###############################################################################\n" |
| 656 | "(>&2 echo \"Remediating rule %i/%i: '%s'\"); (\n", |
| 657 | current, total, xccdf_rule_get_id(rule), current, total, xccdf_rule_get_id(rule)); |
| 658 | return _write_text_to_fd_and_free(output_fd, fix_header); |
| 659 | } else { |
| 660 | return 0; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | static int _write_fix_footer_to_fd(const char *sys, int output_fd, struct xccdf_rule *rule) |
| 665 | { |
| 666 | if (oscap_streq(sys, "") || oscap_streq(sys, "urn:xccdf:fix:script:sh") || oscap_streq(sys, "urn:xccdf:fix:commands")) { |
| 667 | char *fix_footer = oscap_sprintf("\n) # END fix for '%s'\n\n", xccdf_rule_get_id(rule)); |
| 668 | return _write_text_to_fd_and_free(output_fd, fix_footer); |
| 669 | } else { |
| 670 | return 0; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | static int _write_fix_missing_warning_to_fd(const char *sys, int output_fd, struct xccdf_rule *rule) |
| 675 | { |
| 676 | if (oscap_streq(sys, "") || oscap_streq(sys, "urn:xccdf:fix:script:sh") || oscap_streq(sys, "urn:xccdf:fix:commands")) { |
| 677 | char *fix_footer = oscap_sprintf("(>&2 echo \"FIX FOR THIS RULE '%s' IS MISSING!\")\n", xccdf_rule_get_id(rule)); |
| 678 | return _write_text_to_fd_and_free(output_fd, fix_footer); |
| 679 | } else { |
| 680 | return 0; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | struct blueprint_entries { |
| 685 | const char *pattern; |
| 686 | struct oscap_list *list; |
| 687 | oscap_pcre_t *re; |
| 688 | }; |
| 689 | |
| 690 | struct blueprint_customizations { |
| 691 | struct oscap_list *generic; |
| 692 | struct oscap_list *services_enable; |
| 693 | struct oscap_list *services_disable; |
| 694 | struct oscap_list *services_mask; |
| 695 | struct oscap_list *kernel_append; |
| 696 | }; |
| 697 | |
| 698 | static inline int _parse_blueprint_fix(const char *fix_text, struct blueprint_customizations *customizations) |
| 699 | { |
| 700 | char *err = NULL; |
| 701 | int errofs; |
| 702 | int ret = 0; |
| 703 | |
| 704 | struct blueprint_entries tab[] = { |
| 705 | {"\\[customizations\\.services\\]\\s+enabled[=\\s]+\\[([^\\]]+)\\]\\s+", customizations->services_enable, NULL}, |
| 706 | {"\\[customizations\\.services\\]\\s+disabled[=\\s]+\\[([^\\]]+)\\]\\s+", customizations->services_disable, NULL}, |
| 707 | {"\\[customizations\\.services\\]\\s+masked[=\\s]+\\[([^\\]]+)\\]\\s+", customizations->services_mask, NULL}, |
| 708 | {"\\[customizations\\.kernel\\]\\s+append[=\\s\"]+([^\"]+)[\\s\"]+", customizations->kernel_append, NULL}, |
| 709 | // We do this only to pop the 'distro' entry to the top of the generic list, |
| 710 | // effectively placing it to the root of the TOML document. |
| 711 | {"\\s+(distro[=\\s\"]+[^\"]+[\\s\"]+)", customizations->generic, NULL}, |
| 712 | {NULL, NULL, NULL} |
| 713 | }; |
| 714 | |
| 715 | for (int i = 0; tab[i].pattern != NULL; i++) { |
| 716 | tab[i].re = oscap_pcre_compile(tab[i].pattern, OSCAP_PCRE_OPTS_UTF8, &err, &errofs); |
| 717 | if (tab[i].re == NULL) { |
| 718 | dE("Unable to compile /%s/ regex pattern, oscap_pcre_compile() returned error (offset: %d): '%s'.\n", tab[i].pattern, errofs, err); |
| 719 | ret = 1; |
| 720 | goto exit; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | const size_t fix_text_len = strlen(fix_text); |
| 725 | size_t start_offset = 0; |
| 726 | int ovector[6] = {0}; |
| 727 | |
| 728 | for (int i = 0; tab[i].pattern != NULL; i++) { |
| 729 | while (true) { |
| 730 | const int match = oscap_pcre_exec(tab[i].re, fix_text, fix_text_len, start_offset, |
| 731 | 0, ovector, sizeof(ovector) / sizeof(ovector[0])); |
| 732 | if (match == -1) |
| 733 | break; |
| 734 | |
| 735 | if (match != 2) { |
| 736 | dE("Expected 1 capture group matches per entry. Found %i!", match - 1); |
| 737 | ret = 1; |
| 738 | goto exit; |
| 739 | } |
| 740 | |
| 741 | char *val = malloc((ovector[3] - ovector[2] + 1) * sizeof(char)); |
| 742 | memcpy(val, &fix_text[ovector[2]], ovector[3] - ovector[2]); |
| 743 | val[ovector[3] - ovector[2]] = '\0'; |
| 744 | |
| 745 | if (!oscap_list_contains(tab[i].list, val, (oscap_cmp_func) oscap_streq)) { |
| 746 | oscap_list_prepend(tab[i].list, val); |
| 747 | } else { |
| 748 | free(val); |
| 749 | } |
| 750 | |
| 751 | start_offset = ovector[1]; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | if (start_offset < fix_text_len-1) { |
| 756 | oscap_list_add(customizations->generic, strdup(fix_text + start_offset)); |
| 757 | } |
| 758 | |
| 759 | exit: |
| 760 | oscap_pcre_err_free(err); |
| 761 | for (int i = 0; tab[i].pattern != NULL; i++) |
| 762 | oscap_pcre_free(tab[i].re); |
| 763 | |
| 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | static inline int _parse_ansible_fix(const char *fix_text, struct oscap_list *variables, struct oscap_list *tasks) |
| 768 | { |
| 769 | // TODO: Tolerate different indentation styles in this regex |
| 770 | const char *pattern = |
| 771 | "- name: XCCDF Value [^ ]+ # promote to variable\n set_fact:\n" |
| 772 | " ([^:]+): (.+)\n tags:\n - always\n"; |
| 773 | char *err; |
| 774 | int errofs; |
| 775 | |
| 776 | oscap_pcre_t *re = oscap_pcre_compile(pattern, OSCAP_PCRE_OPTS_UTF8, &err, &errofs); |
| 777 | if (re == NULL) { |
| 778 | dE("Unable to compile regex pattern, " |
| 779 | "oscap_pcre_compile() returned error (offset: %d): '%s'.\n", errofs, err); |
| 780 | oscap_pcre_err_free(err); |
| 781 | return 1; |
| 782 | } |
| 783 | |
| 784 | // ovector sizing: |
| 785 | // 2 elements are used for the whole needle, |
| 786 | // 4 elements are used for the 2 capture groups |
| 787 | // pcre documentation says we should allocate a third extra for additional |
| 788 | // workspace. |
| 789 | // (2 + 4) * (3 / 2) = 9 |
| 790 | int ovector[9]; |
| 791 | |
| 792 | const size_t fix_text_len = strlen(fix_text); |
| 793 | int start_offset = 0; |
| 794 | while (true) { |
| 795 | const int match = oscap_pcre_exec(re, fix_text, fix_text_len, start_offset, |
| 796 | 0, ovector, sizeof(ovector) / sizeof(ovector[0])); |
| 797 | if (match == -1) |
| 798 | break; |
| 799 | if (match != 3) { |
| 800 | dE("Expected 2 capture group matches per XCCDF variable. Found %i!", |
| 801 | match - 1); |
| 802 | oscap_pcre_free(re); |
| 803 | return 1; |
| 804 | } |
| 805 | |
| 806 | // ovector[0] and [1] hold the start and end of the whole needle match |
| 807 | // ovector[2] and [3] hold the start and end of the first capture group |
| 808 | // ovector[4] and [5] hold the start and end of the second capture group |
| 809 | char *variable_name = malloc((ovector[3] - ovector[2] + 1) * sizeof(char)); |
| 810 | memcpy(variable_name, &fix_text[ovector[2]], ovector[3] - ovector[2]); |
| 811 | variable_name[ovector[3] - ovector[2]] = '\0'; |
| 812 | |
| 813 | char *variable_value = malloc((ovector[5] - ovector[4] + 1) * sizeof(char)); |
| 814 | memcpy(variable_value, &fix_text[ovector[4]], ovector[5] - ovector[4]); |
| 815 | variable_value[ovector[5] - ovector[4]] = '\0'; |
| 816 | |
| 817 | char *var_line = oscap_sprintf(" %s: %s\n", variable_name, variable_value); |
| 818 | |
| 819 | free(variable_name); |
| 820 | free(variable_value); |
| 821 | |
| 822 | if (!oscap_list_contains(variables, var_line, (oscap_cmp_func) oscap_streq)) { |
| 823 | oscap_list_add(variables, var_line); |
| 824 | } |
| 825 | |
| 826 | // Remarks: ovector doesn't contain values relative to start_offset, it contains |
| 827 | // absolute indices of fix_text. |
| 828 | const int length_between_matches = ovector[0] - start_offset; |
| 829 | char *remediation_part = malloc((length_between_matches + 1) * sizeof(char)); |
| 830 | memcpy(remediation_part, &fix_text[start_offset], length_between_matches); |
| 831 | remediation_part[length_between_matches] = '\0'; |
| 832 | oscap_list_add(tasks, remediation_part); |
| 833 | |
| 834 | start_offset = ovector[1]; // next time start after the entire pattern |
| 835 | } |
| 836 | |
| 837 | if (fix_text_len - start_offset > 0) { |
| 838 | char *remediation_part = malloc((fix_text_len - start_offset + 1) * sizeof(char)); |
| 839 | memcpy(remediation_part, &fix_text[start_offset], fix_text_len - start_offset); |
| 840 | remediation_part[fix_text_len - start_offset] = '\0'; |
| 841 | oscap_list_add(tasks, remediation_part); |
| 842 | } |
| 843 | |
| 844 | oscap_pcre_free(re); |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | static int _xccdf_policy_rule_get_fix_text(struct xccdf_policy *policy, struct xccdf_rule *rule, const char *template, char **fix_text) |
| 849 | { |
| 850 | // Ensure that given Rule is selected and applicable (CPE). |
| 851 | const bool is_selected = xccdf_policy_is_item_selected(policy, xccdf_rule_get_id(rule)); |
| 852 | if (!is_selected) { |
| 853 | dI("Skipping unselected Rule/@id=\"%s\"", xccdf_rule_get_id(rule)); |
| 854 | return 0; |
| 855 | } |
| 856 | // Find the most suitable fix. |
| 857 | const struct xccdf_fix *fix = _find_fix_for_template(policy, rule, template); |
| 858 | if (fix == NULL) { |
| 859 | dI("No fix element was found for Rule/@id=\"%s\"", xccdf_rule_get_id(rule)); |
| 860 | return 0; |
| 861 | } |
| 862 | dI("Processing a fix for Rule/@id=\"%s\"", xccdf_rule_get_id(rule)); |
| 863 | |
| 864 | // Process Text Substitute within the fix |
| 865 | struct xccdf_fix *cfix = xccdf_fix_clone(fix); |
| 866 | int res = xccdf_policy_resolve_fix_substitution(policy, cfix, NULL, NULL); |
| 867 | if (res != 0) { |
| 868 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "A fix for Rule/@id=\"%s\" was skipped: Text substitution failed.", |
| 869 | xccdf_rule_get_id(rule)); |
| 870 | xccdf_fix_free(cfix); |
| 871 | return res == 1; // Value 2 indicates warning. |
| 872 | } |
| 873 | // Refine. Resolve XML comments, CDATA and remaining elements |
| 874 | if (_xccdf_fix_decode_xml(cfix, fix_text) != 0) { |
| 875 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "A fix element for Rule/@id=\"%s\" contains unresolved child elements.", |
| 876 | xccdf_rule_get_id(rule)); |
| 877 | xccdf_fix_free(cfix); |
| 878 | return 1; |
| 879 | } |
| 880 | xccdf_fix_free(cfix); |
| 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | static int _xccdf_policy_rule_generate_fix(struct xccdf_policy *policy, struct xccdf_rule *rule, const char *template, int output_fd, unsigned int current, unsigned int total) |
| 885 | { |
| 886 | int ret = _write_fix_header_to_fd(template, output_fd, rule, current, total); |
| 887 | if (ret != 0) { |
| 888 | return ret; |
| 889 | } |
| 890 | char *fix_text = NULL; |
| 891 | ret = _xccdf_policy_rule_get_fix_text(policy, rule, template, &fix_text); |
| 892 | if (fix_text == NULL || ret != 0) { |
| 893 | ret = _write_fix_missing_warning_to_fd(template, output_fd, rule); |
| 894 | } else { |
| 895 | ret = _write_remediation_to_fd_and_free(output_fd, template, fix_text); |
| 896 | } |
| 897 | if (ret != 0) { |
| 898 | return ret; |
| 899 | } |
| 900 | ret = _write_fix_footer_to_fd(template, output_fd, rule); |
| 901 | return ret; |
| 902 | } |
| 903 | |
| 904 | static int _xccdf_policy_rule_generate_blueprint_fix(struct xccdf_policy *policy, struct xccdf_rule *rule, const char *template, struct blueprint_customizations *customizations) |
| 905 | { |
| 906 | char *fix_text = NULL; |
| 907 | int ret = _xccdf_policy_rule_get_fix_text(policy, rule, template, &fix_text); |
| 908 | if (fix_text == NULL) { |
| 909 | return ret; |
| 910 | } |
| 911 | ret = _parse_blueprint_fix(fix_text, customizations); |
| 912 | free(fix_text); |
| 913 | return ret; |
| 914 | } |
| 915 | |
| 916 | static int _parse_line(const char *line, struct kickstart_commands *cmds) |
| 917 | { |
| 918 | int ret = 0; |
| 919 | char *dup = strdup(line); |
| 920 | char **words = oscap_split(dup, " "); |
| 921 | enum states { |
| 922 | KS_START, |
| 923 | KS_PACKAGE, |
| 924 | KS_PACKAGE_INSTALL, |
| 925 | KS_PACKAGE_REMOVE, |
| 926 | KS_SERVICE, |
| 927 | KS_SERVICE_ENABLE, |
| 928 | KS_SERVICE_DISABLE, |
| 929 | KS_LOGVOL, |
| 930 | KS_LOGVOL_SIZE, |
| 931 | KS_BOOTLOADER, |
| 932 | KS_KDUMP, |
| 933 | KS_FIREWALL, |
| 934 | KS_FIREWALL_ENABLE, |
| 935 | KS_FIREWALL_DISABLE, |
| 936 | KS_ERROR |
| 937 | }; |
| 938 | int state = KS_START; |
| 939 | struct logvol_cmd *current_logvol_cmd = NULL; |
| 940 | for (unsigned int i = 0; words[i] != NULL; i++) { |
| 941 | char *word = oscap_trim(words[i]); |
| 942 | if (*word == '\0') |
| 943 | continue; |
| 944 | switch (state) { |
| 945 | case KS_START: |
| 946 | if (!strcmp(word, "package")) { |
| 947 | state = KS_PACKAGE; |
| 948 | } else if (!strcmp(word, "service")) { |
| 949 | state = KS_SERVICE; |
| 950 | } else if (!strcmp(word, "logvol")) { |
| 951 | state = KS_LOGVOL; |
| 952 | } else if (!strcmp(word, "bootloader")) { |
| 953 | state = KS_BOOTLOADER; |
| 954 | } else if (!strcmp(word, "kdump")) { |
| 955 | state = KS_KDUMP; |
| 956 | } else if (!strcmp(word, "firewall")) { |
| 957 | state = KS_FIREWALL; |
| 958 | } else { |
| 959 | ret = 1; |
| 960 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unsupported command keyword '%s' in command: '%s'", word, line); |
| 961 | goto cleanup; |
| 962 | } |
| 963 | break; |
| 964 | case KS_PACKAGE: |
| 965 | if (!strcmp(word, "install")) { |
| 966 | state = KS_PACKAGE_INSTALL; |
| 967 | } else if (!strcmp(word, "remove")) { |
| 968 | state = KS_PACKAGE_REMOVE; |
| 969 | } else { |
| 970 | ret = 1; |
| 971 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unsupported 'package' command keyword '%s' in command:'%s'", word, line); |
| 972 | goto cleanup; |
| 973 | } |
| 974 | break; |
| 975 | case KS_PACKAGE_INSTALL: |
| 976 | oscap_list_add(cmds->package_install, strdup(word)); |
| 977 | break; |
| 978 | case KS_PACKAGE_REMOVE: |
| 979 | oscap_list_add(cmds->package_remove, strdup(word)); |
| 980 | break; |
| 981 | case KS_SERVICE: |
| 982 | if (!strcmp(word, "enable")) { |
| 983 | state = KS_SERVICE_ENABLE; |
| 984 | } else if (!strcmp(word, "disable")) { |
| 985 | state = KS_SERVICE_DISABLE; |
| 986 | } else { |
| 987 | ret = 1; |
| 988 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unsupported 'service' command keyword '%s' in command: '%s'", word, line); |
| 989 | goto cleanup; |
| 990 | } |
| 991 | break; |
| 992 | case KS_SERVICE_ENABLE: |
| 993 | oscap_list_add(cmds->service_enable, strdup(word)); |
| 994 | break; |
| 995 | case KS_SERVICE_DISABLE: |
| 996 | oscap_list_add(cmds->service_disable, strdup(word)); |
| 997 | break; |
| 998 | case KS_LOGVOL: |
| 999 | current_logvol_cmd = malloc(sizeof(struct logvol_cmd)); |
| 1000 | current_logvol_cmd->path = strdup(word); |
| 1001 | state = KS_LOGVOL_SIZE; |
| 1002 | break; |
| 1003 | case KS_LOGVOL_SIZE: |
| 1004 | current_logvol_cmd->size = strdup(word); |
| 1005 | oscap_list_add(cmds->logvol, current_logvol_cmd); |
| 1006 | current_logvol_cmd = NULL; |
| 1007 | state = KS_ERROR; |
| 1008 | break; |
| 1009 | case KS_BOOTLOADER: |
| 1010 | oscap_list_add(cmds->bootloader, strdup(word)); |
| 1011 | break; |
| 1012 | case KS_KDUMP: |
| 1013 | if (!strcmp(word, "disable")) { |
| 1014 | cmds->enable_kdump = false; |
| 1015 | } else { |
| 1016 | ret = 1; |
| 1017 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unsupported 'kdump' command keyword '%s' in command: '%s'", word, line); |
| 1018 | goto cleanup; |
| 1019 | } |
| 1020 | break; |
| 1021 | case KS_FIREWALL: |
| 1022 | if (!strcmp(word, "enable")) { |
| 1023 | state = KS_FIREWALL_ENABLE; |
| 1024 | } else if (!strcmp(word, "disable")) { |
| 1025 | state = KS_FIREWALL_DISABLE; |
| 1026 | } else { |
| 1027 | ret = 1; |
| 1028 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unsupported 'firewall' command keyword '%s' in command: '%s'", word, line); |
| 1029 | goto cleanup; |
| 1030 | } |
| 1031 | break; |
| 1032 | case KS_FIREWALL_ENABLE: |
| 1033 | oscap_list_add(cmds->firewall_enable, strdup(word)); |
| 1034 | break; |
| 1035 | case KS_FIREWALL_DISABLE: |
| 1036 | oscap_list_add(cmds->firewall_disable, strdup(word)); |
| 1037 | break; |
| 1038 | case KS_ERROR: |
| 1039 | ret = 1; |
| 1040 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unexpected string '%s' in command: '%s'", word, line); |
| 1041 | goto cleanup; |
| 1042 | default: |
| 1043 | break; |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | cleanup: |
| 1048 | free(words); |
| 1049 | free(dup); |
| 1050 | return ret; |
| 1051 | } |
| 1052 | |
| 1053 | static int _xccdf_policy_rule_generate_kickstart_fix(struct xccdf_policy *policy, struct xccdf_rule *rule, const char *template, struct kickstart_commands *cmds) |
| 1054 | { |
| 1055 | char *fix_text = NULL; |
| 1056 | int ret = _xccdf_policy_rule_get_fix_text(policy, rule, template, &fix_text); |
| 1057 | if (fix_text == NULL) { |
| 1058 | return ret; |
| 1059 | } |
| 1060 | char *dup = strdup(fix_text); |
| 1061 | char **lines = oscap_split(dup, "\n"); |
| 1062 | enum states { |
| 1063 | KS_R_P_NORMAL, |
| 1064 | KS_R_P_PRE_BLOCK, |
| 1065 | KS_R_P_POST_BLOCK |
| 1066 | }; |
| 1067 | int state = KS_R_P_NORMAL; |
| 1068 | char *block = NULL; |
| 1069 | for (unsigned int i = 0; lines[i] != NULL; i++) { |
| 1070 | char *line = lines[i]; |
| 1071 | char *trim_line = oscap_trim(strdup(line)); |
| 1072 | switch (state) { |
| 1073 | case KS_R_P_NORMAL: |
| 1074 | if (oscap_str_startswith(trim_line, "%pre")) { |
| 1075 | state = KS_R_P_PRE_BLOCK; |
| 1076 | block = strdup(trim_line); |
| 1077 | block = oscap_concat(block, "\n"); |
| 1078 | } else if (oscap_str_startswith(trim_line, "%post")) { |
| 1079 | state = KS_R_P_POST_BLOCK; |
| 1080 | block = strdup(trim_line); |
| 1081 | block = oscap_concat(block, "\n"); |
| 1082 | } else if (*trim_line != '#' && *trim_line != '\0') { |
| 1083 | _parse_line(trim_line, cmds); |
| 1084 | } |
| 1085 | break; |
| 1086 | case KS_R_P_PRE_BLOCK: |
| 1087 | if (oscap_str_startswith(trim_line, "%end")) { |
| 1088 | state = KS_R_P_NORMAL; |
| 1089 | block = oscap_concat(block, trim_line); |
| 1090 | block = oscap_concat(block, "\n"); |
| 1091 | oscap_list_add(cmds->pre, block); |
| 1092 | block = NULL; |
| 1093 | } else { |
| 1094 | block = oscap_concat(block, line); |
| 1095 | block = oscap_concat(block, "\n"); |
| 1096 | } |
| 1097 | break; |
| 1098 | case KS_R_P_POST_BLOCK: |
| 1099 | if (oscap_str_startswith(trim_line, "%end")) { |
| 1100 | state = KS_R_P_NORMAL; |
| 1101 | block = oscap_concat(block, trim_line); |
| 1102 | block = oscap_concat(block, "\n"); |
| 1103 | oscap_list_add(cmds->post, block); |
| 1104 | block = NULL; |
| 1105 | } else { |
| 1106 | block = oscap_concat(block, line); |
| 1107 | block = oscap_concat(block, "\n"); |
| 1108 | } |
| 1109 | default: |
| 1110 | break; |
| 1111 | } |
| 1112 | free(trim_line); |
| 1113 | } |
| 1114 | if (state != KS_R_P_NORMAL) { |
| 1115 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unfinished block in kickstart remediation in rule %s\n", xccdf_rule_get_id(rule)); |
| 1116 | } |
| 1117 | free(block); |
| 1118 | free(lines); |
| 1119 | free(dup); |
| 1120 | free(fix_text); |
| 1121 | return ret; |
| 1122 | } |
| 1123 | |
| 1124 | static int _xccdf_policy_rule_generate_ansible_fix(struct xccdf_policy *policy, struct xccdf_rule *rule, const char *template, struct oscap_list *variables, struct oscap_list *tasks) |
| 1125 | { |
| 1126 | char *fix_text = NULL; |
| 1127 | int ret = _xccdf_policy_rule_get_fix_text(policy, rule, template, &fix_text); |
| 1128 | if (fix_text == NULL) { |
| 1129 | return ret; |
| 1130 | } |
| 1131 | ret = _parse_ansible_fix(fix_text, variables, tasks); |
| 1132 | free(fix_text); |
| 1133 | return ret; |
| 1134 | } |
| 1135 | |
| 1136 | static int _xccdf_item_recursive_gather_selected_rules(struct xccdf_policy *policy, struct xccdf_item *item, struct oscap_list *rule_list) |
| 1137 | { |
| 1138 | int ret = 0; |
| 1139 | const bool is_selected = xccdf_policy_is_item_selected(policy, xccdf_item_get_id(item)); |
| 1140 | if (!is_selected) { |
| 1141 | return ret; |
| 1142 | } |
| 1143 | switch (xccdf_item_get_type(item)) { |
| 1144 | case XCCDF_GROUP:{ |
| 1145 | struct xccdf_item_iterator *child_it = xccdf_group_get_content((struct xccdf_group *) item); |
| 1146 | while (xccdf_item_iterator_has_more(child_it)) { |
| 1147 | struct xccdf_item *child = xccdf_item_iterator_next(child_it); |
| 1148 | ret = _xccdf_item_recursive_gather_selected_rules(policy, child, rule_list); |
| 1149 | if (ret != 0) |
| 1150 | break; |
| 1151 | } |
| 1152 | xccdf_item_iterator_free(child_it); |
| 1153 | } break; |
| 1154 | case XCCDF_RULE:{ |
| 1155 | oscap_list_add(rule_list, (struct xccdf_rule *) item); |
| 1156 | } break; |
| 1157 | default: |
| 1158 | assert(false); |
| 1159 | break; |
| 1160 | } |
| 1161 | return ret; |
| 1162 | } |
| 1163 | |
| 1164 | static void _trim_trailing_whitespace(char *str, size_t str_len) |
| 1165 | { |
| 1166 | char *last_char = str + str_len - 1; |
| 1167 | while (isspace(*last_char)) { |
| 1168 | *last_char = '\0'; |
| 1169 | last_char--; |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | /* Handles multiline strings in profile title and description. |
| 1174 | * Puts a '#' at the beginning of each line. |
| 1175 | * Also removes trailing and leading whitespaces on each line. |
| 1176 | */ |
| 1177 | static char *_comment_multiline_text(char *text) |
| 1178 | { |
| 1179 | if (text == NULL) { |
| 1180 | return oscap_strdup("Not available"); |
| 1181 | } |
| 1182 | const char *filler = "\n# "; |
| 1183 | size_t buffer_size = strlen(text) + 1; // +1 for terminating '\0' |
| 1184 | char *buffer = malloc(buffer_size); |
| 1185 | if (buffer == NULL) |
| 1186 | return NULL; |
| 1187 | char *saveptr; |
| 1188 | size_t filler_len = strlen(filler); |
| 1189 | size_t result_len = 0; |
| 1190 | bool first = true; |
| 1191 | char *str = text; |
| 1192 | while (true) { |
| 1193 | char *token = oscap_strtok_r(str, "\n", &saveptr); |
| 1194 | if (token == NULL) { |
| 1195 | break; |
| 1196 | } |
| 1197 | /* Strip leading whitespace */ |
| 1198 | while (isspace(*token)) { |
| 1199 | token++; |
| 1200 | } |
| 1201 | size_t token_len = strlen(token); |
| 1202 | if (token_len > 0) { |
| 1203 | /* Strip trailing whitespace */ |
| 1204 | _trim_trailing_whitespace(token, token_len); |
| 1205 | token_len = strlen(token); |
| 1206 | } |
| 1207 | if (token_len > 0) { |
| 1208 | /* Copy filler to output buffer */ |
| 1209 | if (!first) { |
| 1210 | if (buffer_size < result_len + filler_len + 1) { |
| 1211 | buffer_size += filler_len; |
| 1212 | void *new_buffer = realloc(buffer, buffer_size); |
| 1213 | if (new_buffer == NULL) { |
| 1214 | free(buffer); |
| 1215 | return NULL; |
| 1216 | } |
| 1217 | buffer = new_buffer; |
| 1218 | } |
| 1219 | strncpy(buffer + result_len, filler, filler_len + 1); |
| 1220 | result_len += filler_len; |
| 1221 | } |
| 1222 | if (buffer_size < result_len + token_len + 1) { |
| 1223 | buffer_size += token_len; |
| 1224 | void *new_buffer = realloc(buffer, buffer_size); |
| 1225 | if (new_buffer == NULL) { |
| 1226 | free(buffer); |
| 1227 | return NULL; |
| 1228 | } |
| 1229 | buffer = new_buffer; |
| 1230 | } |
| 1231 | /* Copy token to output buffer */ |
| 1232 | strncpy(buffer + result_len, token, token_len + 1); |
| 1233 | result_len += token_len; |
| 1234 | first = false; |
| 1235 | } |
| 1236 | str = NULL; |
| 1237 | } |
| 1238 | *(buffer + result_len) = '\0'; |
| 1239 | return buffer; |
| 1240 | } |
| 1241 | |
| 1242 | static int _write_script_header_to_fd(struct xccdf_policy *policy, struct xccdf_result *result, const char *sys, const char *input_file_name, const char *tailoring_file_name, int output_fd, int raw) |
| 1243 | { |
| 1244 | if (!(oscap_streq(sys, "") || oscap_streq(sys, "urn:xccdf:fix:script:sh") || oscap_streq(sys, "urn:xccdf:fix:commands") || |
| 1245 | oscap_streq(sys, "urn:xccdf:fix:script:ansible") || oscap_streq(sys, "urn:redhat:osbuild:blueprint") || oscap_streq(sys, "urn:xccdf:fix:script:kickstart") )) |
| 1246 | return 0; // no header required |
| 1247 | |
| 1248 | const char *oscap_version = oscap_get_version(); |
| 1249 | char *how_to_apply = ""; |
| 1250 | char *format = (char *)sys; |
| 1251 | char *remediation_type = "Unknown"; |
| 1252 | char *shebang_with_newline = ""; |
| 1253 | |
| 1254 | if (oscap_streq(sys, "urn:xccdf:fix:script:ansible")) { |
| 1255 | how_to_apply = "# $ ansible-playbook -i \"localhost,\" -c local playbook.yml\n" |
| 1256 | "# $ ansible-playbook -i \"192.168.1.155,\" playbook.yml\n" |
| 1257 | "# $ ansible-playbook -i inventory.ini playbook.yml"; |
| 1258 | format = "ansible"; |
| 1259 | remediation_type = "Ansible Playbook"; |
| 1260 | } |
| 1261 | |
| 1262 | if (oscap_streq(sys, "urn:redhat:osbuild:blueprint")) { |
| 1263 | how_to_apply = "# composer-cli blueprints push blueprint.toml"; |
| 1264 | format = "blueprint"; |
| 1265 | remediation_type = "Blueprint"; |
| 1266 | } |
| 1267 | |
| 1268 | if (oscap_streq(sys, "") || oscap_streq(sys, "urn:xccdf:fix:script:sh") || oscap_streq(sys, "urn:xccdf:fix:commands")) { |
| 1269 | how_to_apply = "# $ sudo ./remediation-script.sh"; |
| 1270 | format = "bash"; |
| 1271 | remediation_type = "Bash Remediation Script"; |
| 1272 | shebang_with_newline = "#!/usr/bin/env bash\n"; |
| 1273 | } |
| 1274 | |
| 1275 | if (oscap_streq(sys, "urn:xccdf:fix:script:kickstart")) { |
| 1276 | how_to_apply = "# Review the kickstart and customize the kickstart for your deployment.\n" |
| 1277 | "# Pay attention to items marked as \"required for security compliance\".\n" |
| 1278 | "# Install the operating system using this kickstart."; |
| 1279 | format = "kickstart"; |
| 1280 | remediation_type = "Kickstart"; |
| 1281 | } |
| 1282 | |
| 1283 | char *fix_header; |
| 1284 | |
| 1285 | struct xccdf_profile *profile = xccdf_policy_get_profile(policy); |
| 1286 | const char *profile_id = xccdf_profile_get_id(profile); |
| 1287 | |
| 1288 | struct xccdf_benchmark *benchmark = xccdf_policy_get_benchmark(policy); |
| 1289 | const char *benchmark_version_info = benchmark ? xccdf_benchmark_get_version(benchmark) : "Unknown"; |
| 1290 | const char *benchmark_id = benchmark ? xccdf_benchmark_get_id(benchmark) : "Unknown"; |
| 1291 | |
| 1292 | // Title |
| 1293 | struct oscap_text_iterator *title_iterator = xccdf_profile_get_title(profile); |
| 1294 | char *raw_profile_title = oscap_textlist_get_preferred_plaintext(title_iterator, NULL); |
| 1295 | oscap_text_iterator_free(title_iterator); |
| 1296 | char *profile_title = _comment_multiline_text(raw_profile_title); |
| 1297 | free(raw_profile_title); |
| 1298 | |
| 1299 | if (raw > 0) { |
| 1300 | fix_header = strdup(""); |
| 1301 | } else if (result == NULL) { |
| 1302 | // Profile-based remediation fix |
| 1303 | // Description |
| 1304 | struct oscap_text_iterator *description_iterator = xccdf_profile_get_description(profile); |
| 1305 | char *profile_description = description_iterator != NULL ? |
| 1306 | oscap_textlist_get_preferred_plaintext(description_iterator, NULL) : NULL; |
| 1307 | oscap_text_iterator_free(description_iterator); |
| 1308 | char *commented_profile_description = _comment_multiline_text(profile_description); |
| 1309 | free(profile_description); |
| 1310 | |
| 1311 | const struct xccdf_version_info *xccdf_version = benchmark ? xccdf_benchmark_get_schema_version(benchmark) : NULL; |
| 1312 | char *profile_id_banner = profile_id ? oscap_sprintf("# Profile ID: %s\n", profile_id) : strdup(""); |
| 1313 | const char *xccdf_version_name = xccdf_version ? xccdf_version_info_get_version(xccdf_version) : "Unknown"; |
| 1314 | char *tailoring_option = tailoring_file_name ? oscap_sprintf(" --tailoring-file %s", tailoring_file_name) : strdup(""); |
| 1315 | char *profile_option = profile_id ? oscap_sprintf(" --profile %s", profile_id) : strdup(""); |
| 1316 | |
| 1317 | fix_header = oscap_sprintf( |
| 1318 | "%s" |
| 1319 | "###############################################################################\n" |
| 1320 | "#\n" |
| 1321 | "# %s for %s\n" |
| 1322 | "#\n" |
| 1323 | "# Profile Description:\n" |
| 1324 | "# %s\n" |
| 1325 | "#\n" |
| 1326 | "%s" |
| 1327 | "# Benchmark ID: %s\n" |
| 1328 | "# Benchmark Version: %s\n" |
| 1329 | "# XCCDF Version: %s\n" |
| 1330 | "#\n" |
| 1331 | "# This file was generated by OpenSCAP %s using:\n" |
| 1332 | "# $ oscap xccdf generate fix%s%s --fix-type %s %s\n" |
| 1333 | "#\n" |
| 1334 | "# This %s is generated from an OpenSCAP profile without preliminary evaluation.\n" |
| 1335 | "# It attempts to fix every selected rule, even if the system is already compliant.\n" |
| 1336 | "#\n" |
| 1337 | "# How to apply this %s:\n" |
| 1338 | "%s\n" |
| 1339 | "#\n" |
| 1340 | "###############################################################################\n\n", |
| 1341 | shebang_with_newline, remediation_type, profile_title, |
| 1342 | commented_profile_description, |
| 1343 | profile_id_banner, benchmark_id, benchmark_version_info, xccdf_version_name, |
| 1344 | oscap_version, tailoring_option, profile_option, format, input_file_name, remediation_type, |
| 1345 | remediation_type, how_to_apply |
| 1346 | ); |
| 1347 | |
| 1348 | free(profile_id_banner); |
| 1349 | free(tailoring_option); |
| 1350 | free(commented_profile_description); |
| 1351 | free(profile_option); |
| 1352 | } else { |
| 1353 | // Results-based remediation fix |
| 1354 | const char *start_time = xccdf_result_get_start_time(result); |
| 1355 | const char *end_time = xccdf_result_get_end_time(result); |
| 1356 | const char *result_id = xccdf_result_get_id(result); |
| 1357 | const struct xccdf_version_info *xccdf_version = xccdf_result_get_schema_version(result); |
| 1358 | const char *xccdf_version_name = xccdf_version_info_get_version(xccdf_version); |
| 1359 | |
| 1360 | fix_header = oscap_sprintf( |
| 1361 | "%s" |
| 1362 | "###############################################################################\n" |
| 1363 | "#\n" |
| 1364 | "# %s generated from evaluation of %s\n" |
| 1365 | "#\n" |
| 1366 | "# Profile ID: %s\n" |
| 1367 | "# XCCDF Version: %s\n#\n" |
| 1368 | "# Evaluation Start Time: %s\n" |
| 1369 | "# Evaluation End Time: %s\n#\n" |
| 1370 | "# This file was generated by OpenSCAP %s using:\n" |
| 1371 | "# $ oscap xccdf generate fix --result-id %s --fix-type %s xccdf-results.xml\n" |
| 1372 | "#\n" |
| 1373 | "# This %s is generated from the results of a profile evaluation.\n" |
| 1374 | "# It attempts to remediate all issues from the selected rules that failed the test.\n" |
| 1375 | "#\n" |
| 1376 | "# How to apply this %s:\n" |
| 1377 | "%s\n" |
| 1378 | "#\n" |
| 1379 | "###############################################################################\n\n", |
| 1380 | shebang_with_newline, remediation_type, profile_title, profile_id, xccdf_version_name, |
| 1381 | start_time != NULL ? start_time : "Unknown", end_time, oscap_version, |
| 1382 | result_id, format, remediation_type, remediation_type, how_to_apply |
| 1383 | ); |
| 1384 | } |
| 1385 | |
| 1386 | if (oscap_streq(sys, "urn:xccdf:fix:script:ansible")) { |
| 1387 | char *ansible_fix_header = oscap_sprintf( |
| 1388 | "---\n" |
| 1389 | "%s\n" |
| 1390 | "- hosts: all\n", |
| 1391 | fix_header); |
| 1392 | free(fix_header); |
| 1393 | free(profile_title); |
| 1394 | return _write_text_to_fd_and_free(output_fd, ansible_fix_header); |
| 1395 | } else if (oscap_streq(sys, "urn:redhat:osbuild:blueprint")) { |
| 1396 | char *customization_instructions = "# If your hardening data stream is not part of the 'scap-security-guide' package\n" |
| 1397 | "# provide the absolute path to it (from the root of the image filesystem).\n" |
| 1398 | "# datastream = \"/usr/share/xml/scap/ssg/content/ssg-xxxxx-ds.xml\"\n"; |
| 1399 | char *blueprint_fix_header = oscap_sprintf( |
| 1400 | "%s" |
| 1401 | "name = \"hardened_%s\"\n" |
| 1402 | "description = \"%s\"\n" |
| 1403 | "version = \"%s\"\n\n" |
| 1404 | "[customizations.openscap]\n" |
| 1405 | "profile_id = \"%s\"\n" |
| 1406 | "%s\n", |
| 1407 | fix_header, profile_id, profile_title, benchmark_version_info, profile_id, raw == 0 ? customization_instructions : ""); |
| 1408 | free(fix_header); |
| 1409 | free(profile_title); |
| 1410 | return _write_text_to_fd_and_free(output_fd, blueprint_fix_header); |
| 1411 | } else { |
| 1412 | free(profile_title); |
| 1413 | return _write_text_to_fd_and_free(output_fd, fix_header); |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | static inline void _format_and_write_list_into_blueprint_fd(struct oscap_list *list_, const char *separator, int output_fd) |
| 1418 | { |
| 1419 | struct oscap_iterator *it = oscap_iterator_new(list_); |
| 1420 | while(oscap_iterator_has_more(it)) { |
| 1421 | char *var_line = (char *)oscap_iterator_next(it); |
| 1422 | _write_text_to_fd(output_fd, var_line); |
| 1423 | if (oscap_iterator_has_more(it)) |
| 1424 | _write_text_to_fd(output_fd, separator); |
| 1425 | } |
| 1426 | oscap_iterator_free(it); |
| 1427 | } |
| 1428 | |
| 1429 | static int _xccdf_policy_generate_fix_blueprint(struct oscap_list *rules_to_fix, struct xccdf_policy *policy, const char *sys, int output_fd) |
| 1430 | { |
| 1431 | int ret = 0; |
| 1432 | struct blueprint_customizations customizations = { |
| 1433 | .generic = oscap_list_new(), |
| 1434 | .services_enable = oscap_list_new(), |
| 1435 | .services_disable = oscap_list_new(), |
| 1436 | .services_mask = oscap_list_new(), |
| 1437 | .kernel_append = oscap_list_new() |
| 1438 | }; |
| 1439 | |
| 1440 | struct oscap_iterator *rules_to_fix_it = oscap_iterator_new(rules_to_fix); |
| 1441 | while (oscap_iterator_has_more(rules_to_fix_it)) { |
| 1442 | struct xccdf_rule *rule = (struct xccdf_rule*)oscap_iterator_next(rules_to_fix_it); |
| 1443 | ret = _xccdf_policy_rule_generate_blueprint_fix(policy, rule, sys, &customizations); |
| 1444 | if (ret != 0) |
| 1445 | break; |
| 1446 | } |
| 1447 | oscap_iterator_free(rules_to_fix_it); |
| 1448 | |
| 1449 | struct oscap_iterator *generic_it = oscap_iterator_new(customizations.generic); |
| 1450 | while(oscap_iterator_has_more(generic_it)) { |
| 1451 | char *var_line = (char *) oscap_iterator_next(generic_it); |
| 1452 | _write_text_to_fd(output_fd, var_line); |
| 1453 | } |
| 1454 | _write_text_to_fd(output_fd, "\n"); |
| 1455 | oscap_iterator_free(generic_it); |
| 1456 | |
| 1457 | _write_text_to_fd(output_fd, "[customizations.kernel]\nappend = \""); |
| 1458 | _format_and_write_list_into_blueprint_fd(customizations.kernel_append, " ", output_fd); |
| 1459 | _write_text_to_fd(output_fd, "\"\n\n"); |
| 1460 | |
| 1461 | _write_text_to_fd(output_fd, "[customizations.services]\n"); |
| 1462 | _write_text_to_fd(output_fd, "enabled = ["); |
| 1463 | _format_and_write_list_into_blueprint_fd(customizations.services_enable, ",", output_fd); |
| 1464 | _write_text_to_fd(output_fd, "]\n"); |
| 1465 | |
| 1466 | _write_text_to_fd(output_fd, "disabled = ["); |
| 1467 | _format_and_write_list_into_blueprint_fd(customizations.services_disable, ",", output_fd); |
| 1468 | _write_text_to_fd(output_fd, "]\n"); |
| 1469 | |
| 1470 | _write_text_to_fd(output_fd, "masked = ["); |
| 1471 | _format_and_write_list_into_blueprint_fd(customizations.services_mask, ",", output_fd); |
| 1472 | _write_text_to_fd(output_fd, "]\n\n"); |
| 1473 | |
| 1474 | oscap_list_free(customizations.services_mask, free); |
| 1475 | oscap_list_free(customizations.services_disable, free); |
| 1476 | oscap_list_free(customizations.kernel_append, free); |
| 1477 | oscap_list_free(customizations.services_enable, free); |
| 1478 | oscap_list_free(customizations.generic, free); |
| 1479 | |
| 1480 | return ret; |
| 1481 | } |
| 1482 | |
| 1483 | static int _xccdf_policy_generate_fix_ansible(struct oscap_list *rules_to_fix, struct xccdf_policy *policy, const char *sys, int output_fd) |
| 1484 | { |
| 1485 | int ret = 0; |
| 1486 | struct oscap_list *variables = oscap_list_new(); |
| 1487 | struct oscap_list *tasks = oscap_list_new(); |
| 1488 | struct oscap_iterator *rules_to_fix_it = oscap_iterator_new(rules_to_fix); |
| 1489 | while (oscap_iterator_has_more(rules_to_fix_it)) { |
| 1490 | struct xccdf_rule *rule = (struct xccdf_rule*)oscap_iterator_next(rules_to_fix_it); |
| 1491 | ret = _xccdf_policy_rule_generate_ansible_fix(policy, rule, sys, variables, tasks); |
| 1492 | if (ret != 0) |
| 1493 | break; |
| 1494 | } |
| 1495 | oscap_iterator_free(rules_to_fix_it); |
| 1496 | |
| 1497 | _write_text_to_fd(output_fd, " vars:\n"); |
| 1498 | struct oscap_iterator *variables_it = oscap_iterator_new(variables); |
| 1499 | while(oscap_iterator_has_more(variables_it)) { |
| 1500 | char *var_line = (char *) oscap_iterator_next(variables_it); |
| 1501 | _write_text_to_fd(output_fd, var_line); |
| 1502 | } |
| 1503 | oscap_iterator_free(variables_it); |
| 1504 | oscap_list_free(variables, free); |
| 1505 | |
| 1506 | _write_text_to_fd(output_fd, " tasks:\n"); |
| 1507 | struct oscap_iterator *tasks_it = oscap_iterator_new(tasks); |
| 1508 | while(oscap_iterator_has_more(tasks_it)) { |
| 1509 | char *var_line = strdup((char *) oscap_iterator_next(tasks_it)); |
| 1510 | _write_remediation_to_fd_and_free(output_fd, sys, var_line); |
| 1511 | } |
| 1512 | oscap_iterator_free(tasks_it); |
| 1513 | oscap_list_free(tasks, free); |
| 1514 | return ret; |
| 1515 | } |
| 1516 | |
| 1517 | static int _xccdf_policy_generate_fix_other(struct oscap_list *rules_to_fix, struct xccdf_policy *policy, const char *sys, int output_fd) |
| 1518 | { |
| 1519 | int ret = 0; |
| 1520 | const unsigned int total = oscap_list_get_itemcount(rules_to_fix); |
| 1521 | unsigned int current = 1; |
| 1522 | struct oscap_iterator *rules_to_fix_it = oscap_iterator_new(rules_to_fix); |
| 1523 | while (oscap_iterator_has_more(rules_to_fix_it)) { |
| 1524 | struct xccdf_rule *rule = (struct xccdf_rule *) oscap_iterator_next(rules_to_fix_it); |
| 1525 | ret = _xccdf_policy_rule_generate_fix(policy, rule, sys, output_fd, current++, total); |
| 1526 | if (ret != 0) |
| 1527 | break; |
| 1528 | } |
| 1529 | oscap_iterator_free(rules_to_fix_it); |
| 1530 | return ret; |
| 1531 | } |
| 1532 | |
| 1533 | static void _write_it_comma_list(struct oscap_iterator *it, const char *option, int output_fd) |
| 1534 | { |
| 1535 | if (!oscap_iterator_has_more(it)) |
| 1536 | return; |
| 1537 | _write_text_to_fd(output_fd, " "); |
| 1538 | _write_text_to_fd(output_fd, option); |
| 1539 | while (oscap_iterator_has_more(it)) { |
| 1540 | char *item = (char *) oscap_iterator_next(it); |
| 1541 | _write_text_to_fd(output_fd, item); |
| 1542 | if (oscap_iterator_has_more(it)) |
| 1543 | _write_text_to_fd(output_fd, ","); |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | static int _generate_kickstart_services(struct kickstart_commands *cmds, int output_fd, int raw) |
| 1548 | { |
| 1549 | struct oscap_iterator *service_disable_it = oscap_iterator_new(cmds->service_disable); |
| 1550 | struct oscap_iterator *service_enable_it = oscap_iterator_new(cmds->service_enable); |
| 1551 | if (oscap_iterator_has_more(service_disable_it) || oscap_iterator_has_more(service_enable_it)) { |
| 1552 | if (raw == 0) |
| 1553 | _write_text_to_fd(output_fd, "# Disable and enable systemd services (required for security compliance)\n"); |
| 1554 | _write_text_to_fd(output_fd, "services"); |
| 1555 | _write_it_comma_list(service_disable_it, "--disabled=", output_fd); |
| 1556 | _write_it_comma_list(service_enable_it, "--enabled=", output_fd); |
| 1557 | _write_text_to_fd(output_fd, "\n\n"); |
| 1558 | } |
| 1559 | oscap_iterator_free(service_disable_it); |
| 1560 | oscap_iterator_free(service_enable_it); |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
| 1564 | static int _generate_kickstart_firewall(struct kickstart_commands *cmds, int output_fd, int raw) |
| 1565 | { |
| 1566 | struct oscap_iterator *disable_it = oscap_iterator_new(cmds->firewall_disable); |
| 1567 | struct oscap_iterator *enable_it = oscap_iterator_new(cmds->firewall_enable); |
| 1568 | if (oscap_iterator_has_more(disable_it) || oscap_iterator_has_more(enable_it)) { |
| 1569 | if (raw == 0) |
| 1570 | _write_text_to_fd(output_fd, "# Disable and enable services in firewall (required for security compliance)\n"); |
| 1571 | _write_text_to_fd(output_fd, "firewall"); |
| 1572 | _write_it_comma_list(disable_it, "--remove-service=", output_fd); |
| 1573 | _write_it_comma_list(enable_it, "--service=", output_fd); |
| 1574 | _write_text_to_fd(output_fd, "\n\n"); |
| 1575 | } |
| 1576 | oscap_iterator_free(disable_it); |
| 1577 | oscap_iterator_free(enable_it); |
| 1578 | return 0; |
| 1579 | } |
| 1580 | |
| 1581 | static int _generate_kickstart_packages(struct kickstart_commands *cmds, int output_fd, int raw) |
| 1582 | { |
| 1583 | if (raw == 0) |
| 1584 | _write_text_to_fd(output_fd, "# Packages selection (required for security compliance)\n"); |
| 1585 | _write_text_to_fd(output_fd, "%packages\n"); |
| 1586 | /* openscap-scanner and scap-security-guide needs to be installed because we will run oscap in the %post section */ |
| 1587 | _write_text_to_fd(output_fd, "openscap-scanner\n"); |
| 1588 | _write_text_to_fd(output_fd, "scap-security-guide\n"); |
| 1589 | struct oscap_iterator *package_install_it = oscap_iterator_new(cmds->package_install); |
| 1590 | while (oscap_iterator_has_more(package_install_it)) { |
| 1591 | char *package = (char *) oscap_iterator_next(package_install_it); |
| 1592 | _write_text_to_fd(output_fd, package); |
| 1593 | _write_text_to_fd(output_fd, "\n"); |
| 1594 | } |
| 1595 | oscap_iterator_free(package_install_it); |
| 1596 | struct oscap_iterator *package_remove_it = oscap_iterator_new(cmds->package_remove); |
| 1597 | while (oscap_iterator_has_more(package_remove_it)) { |
| 1598 | char *package = (char *) oscap_iterator_next(package_remove_it); |
| 1599 | _write_text_to_fd(output_fd, "-"); |
| 1600 | _write_text_to_fd(output_fd, package); |
| 1601 | _write_text_to_fd(output_fd, "\n"); |
| 1602 | } |
| 1603 | oscap_iterator_free(package_remove_it); |
| 1604 | _write_text_to_fd(output_fd, "%end\n"); |
| 1605 | _write_text_to_fd(output_fd, "\n"); |
| 1606 | return 0; |
| 1607 | } |
| 1608 | |
| 1609 | static int _write_tailoring_to_fd(struct oscap_source *tailoring, int output_fd) |
| 1610 | { |
| 1611 | if (tailoring == NULL) |
| 1612 | return 0; |
| 1613 | _write_text_to_fd(output_fd, "cat >/root/oscap_tailoring.xml <<END_OF_TAILORING\n"); |
| 1614 | if (oscap_source_to_fd(tailoring, output_fd) != 0) |
| 1615 | return -1; |
| 1616 | _write_text_to_fd(output_fd, "END_OF_TAILORING\n"); |
| 1617 | return 0; |
| 1618 | } |
| 1619 | |
| 1620 | static int _generate_kickstart_oscap_post(struct kickstart_commands *cmds, const char *profile_id, const char *input_path, struct oscap_source *tailoring, int output_fd) |
| 1621 | { |
| 1622 | _write_text_to_fd(output_fd, "%post --erroronfail\n"); |
| 1623 | if (_write_tailoring_to_fd(tailoring, output_fd) != 0) |
| 1624 | return -1; |
| 1625 | const char *fmt = "oscap xccdf eval --remediate%s--results-arf /root/oscap_arf.xml --report /root/oscap_report.html%s/usr/share/xml/scap/ssg/content/%s\n"; |
| 1626 | const char *tailoring_part; |
| 1627 | if (tailoring != NULL) { |
| 1628 | tailoring_part = " --tailoring-file /root/oscap_tailoring.xml "; |
| 1629 | } else { |
| 1630 | tailoring_part = " "; |
| 1631 | } |
| 1632 | char *profile_part; |
| 1633 | if (profile_id != NULL) { |
| 1634 | profile_part = oscap_sprintf(" --profile '%s' ", profile_id); |
| 1635 | } else { |
| 1636 | profile_part = strdup(" "); |
| 1637 | } |
| 1638 | char *dup = strdup(input_path); |
| 1639 | char *basename = oscap_basename(dup); |
| 1640 | free(dup); |
| 1641 | char *oscap_command = oscap_sprintf(fmt, tailoring_part, profile_part, basename); |
| 1642 | free(profile_part); |
| 1643 | free(basename); |
| 1644 | _write_text_to_fd_and_free(output_fd, oscap_command); |
| 1645 | _write_text_to_fd(output_fd, "[ $? -eq 0 -o $? -eq 2 ] || exit 1\n"); |
| 1646 | _write_text_to_fd(output_fd, "%end\n"); |
| 1647 | _write_text_to_fd(output_fd, "\n"); |
| 1648 | return 0; |
| 1649 | } |
| 1650 | |
| 1651 | static int _generate_kickstart_pre(struct kickstart_commands *cmds, int output_fd, int raw) |
| 1652 | { |
| 1653 | struct oscap_iterator *pre_it = oscap_iterator_new(cmds->pre); |
| 1654 | while (oscap_iterator_has_more(pre_it)) { |
| 1655 | if (raw == 0) |
| 1656 | _write_text_to_fd(output_fd, "# Additional %pre section (required for security compliance)\n"); |
| 1657 | char *pre_content = (char *) oscap_iterator_next(pre_it); |
| 1658 | _write_text_to_fd(output_fd, pre_content); |
| 1659 | _write_text_to_fd(output_fd, "\n"); |
| 1660 | } |
| 1661 | oscap_iterator_free(pre_it); |
| 1662 | return 0; |
| 1663 | } |
| 1664 | |
| 1665 | static int _generate_kickstart_post(struct kickstart_commands *cmds, int output_fd, int raw) |
| 1666 | { |
| 1667 | struct oscap_iterator *post_it = oscap_iterator_new(cmds->post); |
| 1668 | while (oscap_iterator_has_more(post_it)) { |
| 1669 | if (raw == 0) |
| 1670 | _write_text_to_fd(output_fd, "# Additional %post section (required for security compliance)\n"); |
| 1671 | char *post_content = (char *) oscap_iterator_next(post_it); |
| 1672 | _write_text_to_fd(output_fd, post_content); |
| 1673 | _write_text_to_fd(output_fd, "\n"); |
| 1674 | } |
| 1675 | oscap_iterator_free(post_it); |
| 1676 | return 0; |
| 1677 | } |
| 1678 | |
| 1679 | const char *common_partition = ( |
| 1680 | "zerombr\n" |
| 1681 | "clearpart --all --initlabel\n" |
| 1682 | "reqpart --add-boot\n" |
| 1683 | "part pv.01 --grow --size=1\n" |
| 1684 | "volgroup system pv.01\n" |
| 1685 | "logvol / --name=root --vgname=system --size=2000 --grow\n" |
| 1686 | "logvol swap --name=swap --vgname=system --size=1000\n" |
| 1687 | ); |
| 1688 | |
| 1689 | /* Fallback partition layout for profiles that don't specify partitioning layout */ |
| 1690 | /* We need to specify at least some layout in the kickstart to make the installation fully automated. */ |
| 1691 | const char *fallback_partition = ( |
| 1692 | "zerombr\n" |
| 1693 | "clearpart --all --initlabel\n" |
| 1694 | "autopart --type=lvm\n" |
| 1695 | ); |
| 1696 | |
| 1697 | static int _generate_kickstart_logvol(struct kickstart_commands *cmds, int output_fd, int raw) |
| 1698 | { |
| 1699 | struct oscap_iterator *logvol_it = oscap_iterator_new(cmds->logvol); |
| 1700 | if (oscap_iterator_has_more(logvol_it)) { |
| 1701 | if (raw == 0) { |
| 1702 | _write_text_to_fd(output_fd, "# Create partition layout scheme (required for security compliance)\n"); |
| 1703 | _write_text_to_fd(output_fd, common_partition); |
| 1704 | } else { |
| 1705 | _write_text_to_fd(output_fd, "# %partitions\n"); |
| 1706 | _write_text_to_fd(output_fd, common_partition); |
| 1707 | _write_text_to_fd(output_fd, "# %logvols\n"); |
| 1708 | } |
| 1709 | } else { |
| 1710 | if (raw == 0) { |
| 1711 | _write_text_to_fd(output_fd, "# Default partition layout scheme (optional)\n"); |
| 1712 | _write_text_to_fd(output_fd, fallback_partition); |
| 1713 | } |
| 1714 | } |
| 1715 | while (oscap_iterator_has_more(logvol_it)) { |
| 1716 | struct logvol_cmd *command = (struct logvol_cmd *) oscap_iterator_next(logvol_it); |
| 1717 | char *name = strdup(command->path); |
| 1718 | oscap_strrm(name, "/"); |
| 1719 | char *fmt = oscap_sprintf("logvol %s --name=%s --vgname=system --size=%s\n", command->path, name, command->size); |
| 1720 | _write_text_to_fd(output_fd, fmt); |
| 1721 | free(name); |
| 1722 | free(fmt); |
| 1723 | if (!oscap_iterator_has_more(logvol_it)) { |
| 1724 | if (raw > 0) |
| 1725 | _write_text_to_fd(output_fd, "# %end\n"); |
| 1726 | _write_text_to_fd(output_fd, "\n"); |
| 1727 | } |
| 1728 | } |
| 1729 | oscap_iterator_free(logvol_it); |
| 1730 | return 0; |
| 1731 | } |
| 1732 | |
| 1733 | static int _generate_kickstart_kdump(struct kickstart_commands *cmds, int output_fd, int raw) |
| 1734 | { |
| 1735 | if (!cmds->enable_kdump) { |
| 1736 | if (raw == 0) |
| 1737 | _write_text_to_fd(output_fd, "# Disable the kdump kernel crash dumping mechanism (required for security compliance)\n"); |
| 1738 | _write_text_to_fd(output_fd, |
| 1739 | "%addon com_redhat_kdump --disable\n" |
| 1740 | "%end\n" |
| 1741 | "\n" |
| 1742 | ); |
| 1743 | } |
| 1744 | return 0; |
| 1745 | } |
| 1746 | |
| 1747 | static int _generate_kickstart_bootloader(struct kickstart_commands *cmds, int output_fd, int raw) |
| 1748 | { |
| 1749 | struct oscap_iterator *bl_it = oscap_iterator_new(cmds->bootloader); |
| 1750 | if (!oscap_iterator_has_more(bl_it)) { |
| 1751 | oscap_iterator_free(bl_it); |
| 1752 | return 0; |
| 1753 | } |
| 1754 | if (raw == 0) |
| 1755 | _write_text_to_fd(output_fd, "# Configure boot loader options (required for security compliance)\n"); |
| 1756 | _write_text_to_fd(output_fd, "bootloader --append=\""); |
| 1757 | while (oscap_iterator_has_more(bl_it)) { |
| 1758 | char *optval = (char *) oscap_iterator_next(bl_it); |
| 1759 | _write_text_to_fd(output_fd, optval); |
| 1760 | if (oscap_iterator_has_more(bl_it)) { |
| 1761 | _write_text_to_fd(output_fd, " "); |
| 1762 | } else { |
| 1763 | _write_text_to_fd(output_fd, "\"\n"); |
| 1764 | _write_text_to_fd(output_fd, "\n"); |
| 1765 | } |
| 1766 | } |
| 1767 | oscap_iterator_free(bl_it); |
| 1768 | return 0; |
| 1769 | } |
| 1770 | |
| 1771 | static void logvol_cmd_free(void *ptr) |
| 1772 | { |
| 1773 | struct logvol_cmd *cmd = (struct logvol_cmd *) ptr; |
| 1774 | free(cmd->path); |
| 1775 | free(cmd->size); |
| 1776 | free(cmd); |
| 1777 | } |
| 1778 | |
| 1779 | static int _xccdf_policy_generate_fix_kickstart(struct oscap_list *rules_to_fix, struct xccdf_policy *policy, const char *sys, const char *input_file_name, struct oscap_source *tailoring, int raw, int output_fd) |
| 1780 | { |
| 1781 | int ret = 0; |
| 1782 | struct kickstart_commands cmds = { |
| 1783 | .package_install = oscap_list_new(), |
| 1784 | .package_remove = oscap_list_new(), |
| 1785 | .service_enable = oscap_list_new(), |
| 1786 | .service_disable = oscap_list_new(), |
| 1787 | .pre = oscap_list_new(), |
| 1788 | .post = oscap_list_new(), |
| 1789 | .logvol = oscap_list_new(), |
| 1790 | .bootloader = oscap_list_new(), |
| 1791 | .firewall_enable = oscap_list_new(), |
| 1792 | .firewall_disable = oscap_list_new(), |
| 1793 | .enable_kdump = true, |
| 1794 | }; |
| 1795 | |
| 1796 | struct oscap_iterator *rules_to_fix_it = oscap_iterator_new(rules_to_fix); |
| 1797 | while (oscap_iterator_has_more(rules_to_fix_it)) { |
| 1798 | struct xccdf_rule *rule = (struct xccdf_rule *) oscap_iterator_next(rules_to_fix_it); |
| 1799 | ret = _xccdf_policy_rule_generate_kickstart_fix(policy, rule, sys, &cmds); |
| 1800 | if (ret != 0) |
| 1801 | break; |
| 1802 | } |
| 1803 | oscap_iterator_free(rules_to_fix_it); |
| 1804 | |
| 1805 | const char *common_template = ( |
| 1806 | "\n# Default values for automated installation (optional)\n" |
| 1807 | "lang en_US.UTF-8\n" |
| 1808 | "keyboard --vckeymap us\n" |
| 1809 | "timezone --utc America/New_York\n" |
| 1810 | "\n" |
| 1811 | "# Root password is required for system rescue tasks (optional)\n" |
| 1812 | "rootpw %s\n" |
| 1813 | "\n" |
| 1814 | ); |
| 1815 | if (raw == 0) { |
| 1816 | char *password = oscap_generate_random_string(24, NULL); |
| 1817 | char *common = oscap_sprintf(common_template, password); |
| 1818 | _write_text_to_fd(output_fd, common); |
| 1819 | free(password); |
| 1820 | free(common); |
| 1821 | } |
| 1822 | |
| 1823 | _generate_kickstart_pre(&cmds, output_fd, raw); |
| 1824 | |
| 1825 | _generate_kickstart_logvol(&cmds, output_fd, raw); |
| 1826 | |
| 1827 | _generate_kickstart_bootloader(&cmds, output_fd, raw); |
| 1828 | |
| 1829 | _generate_kickstart_kdump(&cmds, output_fd, raw); |
| 1830 | |
| 1831 | _generate_kickstart_firewall(&cmds, output_fd, raw); |
| 1832 | |
| 1833 | _generate_kickstart_services(&cmds, output_fd, raw); |
| 1834 | |
| 1835 | _generate_kickstart_packages(&cmds, output_fd, raw); |
| 1836 | |
| 1837 | if (raw == 0) |
| 1838 | _write_text_to_fd(output_fd, "# Perform OpenSCAP hardening (required for security compliance)\n"); |
| 1839 | const char *profile_id = xccdf_profile_get_id(xccdf_policy_get_profile(policy)); |
| 1840 | if (_generate_kickstart_oscap_post(&cmds, profile_id, input_file_name, tailoring, output_fd) != 0) { |
| 1841 | ret = -1; |
| 1842 | goto cleanup; |
| 1843 | } |
| 1844 | |
| 1845 | _generate_kickstart_post(&cmds, output_fd, raw); |
| 1846 | |
| 1847 | if (raw == 0) |
| 1848 | _write_text_to_fd(output_fd, "# Reboot after the installation is complete\nreboot\n"); |
| 1849 | |
| 1850 | cleanup: |
| 1851 | oscap_list_free(cmds.package_install, free); |
| 1852 | oscap_list_free(cmds.package_remove, free); |
| 1853 | oscap_list_free(cmds.service_enable, free); |
| 1854 | oscap_list_free(cmds.service_disable, free); |
| 1855 | oscap_list_free(cmds.pre, free); |
| 1856 | oscap_list_free(cmds.post, free); |
| 1857 | oscap_list_free(cmds.logvol, logvol_cmd_free); |
| 1858 | oscap_list_free(cmds.bootloader, free); |
| 1859 | oscap_list_free(cmds.firewall_enable, free); |
| 1860 | oscap_list_free(cmds.firewall_disable, free); |
| 1861 | return ret; |
| 1862 | } |
| 1863 | |
| 1864 | static int _parse_bootc_line(const char *line, struct bootc_commands *cmds) |
| 1865 | { |
| 1866 | int ret = 0; |
| 1867 | char *dup = strdup(line); |
| 1868 | char **words = oscap_split(dup, " "); |
| 1869 | enum states { |
| 1870 | BOOTC_START, |
| 1871 | BOOTC_DNF, |
| 1872 | BOOTC_DNF_INSTALL, |
| 1873 | BOOTC_DNF_REMOVE, |
| 1874 | BOOTC_ERROR |
| 1875 | }; |
| 1876 | int state = BOOTC_START; |
| 1877 | for (unsigned int i = 0; words[i] != NULL; i++) { |
| 1878 | char *word = oscap_trim(words[i]); |
| 1879 | if (*word == '\0') |
| 1880 | continue; |
| 1881 | switch (state) { |
| 1882 | case BOOTC_START: |
| 1883 | if (!strcmp(word, "dnf")) { |
| 1884 | state = BOOTC_DNF; |
| 1885 | } else { |
| 1886 | ret = 1; |
| 1887 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unsupported command keyword '%s' in command: '%s'", word, line); |
| 1888 | goto cleanup; |
| 1889 | } |
| 1890 | break; |
| 1891 | case BOOTC_DNF: |
| 1892 | if (!strcmp(word, "install")) { |
| 1893 | state = BOOTC_DNF_INSTALL; |
| 1894 | } else if (!strcmp(word, "remove")) { |
| 1895 | state = BOOTC_DNF_REMOVE; |
| 1896 | } else { |
| 1897 | ret = 1; |
| 1898 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unsupported 'dnf' command keyword '%s' in command:'%s'", word, line); |
| 1899 | goto cleanup; |
| 1900 | } |
| 1901 | break; |
| 1902 | case BOOTC_DNF_INSTALL: |
| 1903 | oscap_list_add(cmds->dnf_install, strdup(word)); |
| 1904 | break; |
| 1905 | case BOOTC_DNF_REMOVE: |
| 1906 | oscap_list_add(cmds->dnf_remove, strdup(word)); |
| 1907 | break; |
| 1908 | case BOOTC_ERROR: |
| 1909 | ret = 1; |
| 1910 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unexpected string '%s' in command: '%s'", word, line); |
| 1911 | goto cleanup; |
| 1912 | default: |
| 1913 | break; |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | cleanup: |
| 1918 | free(words); |
| 1919 | free(dup); |
| 1920 | return ret; |
| 1921 | } |
| 1922 | |
| 1923 | static int _xccdf_policy_rule_generate_bootc_fix(struct xccdf_policy *policy, struct xccdf_rule *rule, const char *template, struct bootc_commands *cmds) |
| 1924 | { |
| 1925 | char *fix_text = NULL; |
| 1926 | int ret = _xccdf_policy_rule_get_fix_text(policy, rule, template, &fix_text); |
| 1927 | if (fix_text == NULL) { |
| 1928 | return ret; |
| 1929 | } |
| 1930 | char *dup = strdup(fix_text); |
| 1931 | char **lines = oscap_split(dup, "\n"); |
| 1932 | for (unsigned int i = 0; lines[i] != NULL; i++) { |
| 1933 | char *line = lines[i]; |
| 1934 | char *trim_line = oscap_trim(strdup(line)); |
| 1935 | if (*trim_line != '#' && *trim_line != '\0') { |
| 1936 | _parse_bootc_line(trim_line, cmds); |
| 1937 | } |
| 1938 | free(trim_line); |
| 1939 | } |
| 1940 | free(lines); |
| 1941 | free(dup); |
| 1942 | free(fix_text); |
| 1943 | return ret; |
| 1944 | } |
| 1945 | |
| 1946 | static int _generate_bootc_dnf(struct bootc_commands *cmds, int output_fd) |
| 1947 | { |
| 1948 | struct oscap_iterator *dnf_install_it = oscap_iterator_new(cmds->dnf_install); |
| 1949 | if (oscap_iterator_has_more(dnf_install_it)) { |
| 1950 | _write_text_to_fd(output_fd, "dnf -y install \\\n"); |
| 1951 | while (oscap_iterator_has_more(dnf_install_it)) { |
| 1952 | char *package = (char *) oscap_iterator_next(dnf_install_it); |
| 1953 | _write_text_to_fd(output_fd, " "); |
| 1954 | _write_text_to_fd(output_fd, package); |
| 1955 | if (oscap_iterator_has_more(dnf_install_it)) |
| 1956 | _write_text_to_fd(output_fd, " \\\n"); |
| 1957 | } |
| 1958 | _write_text_to_fd(output_fd, "\n\n"); |
| 1959 | } |
| 1960 | oscap_iterator_free(dnf_install_it); |
| 1961 | |
| 1962 | struct oscap_iterator *dnf_remove_it = oscap_iterator_new(cmds->dnf_remove); |
| 1963 | if (oscap_iterator_has_more(dnf_remove_it)) { |
| 1964 | _write_text_to_fd(output_fd, "dnf -y remove \\\n"); |
| 1965 | while (oscap_iterator_has_more(dnf_remove_it)) { |
| 1966 | char *package = (char *) oscap_iterator_next(dnf_remove_it); |
| 1967 | _write_text_to_fd(output_fd, " "); |
| 1968 | _write_text_to_fd(output_fd, package); |
| 1969 | if (oscap_iterator_has_more(dnf_remove_it)) |
| 1970 | _write_text_to_fd(output_fd, " \\\n"); |
| 1971 | } |
| 1972 | _write_text_to_fd(output_fd, "\n"); |
| 1973 | } |
| 1974 | oscap_iterator_free(dnf_remove_it); |
| 1975 | return 0; |
| 1976 | } |
| 1977 | |
| 1978 | static int _xccdf_policy_generate_fix_bootc(struct oscap_list *rules_to_fix, struct xccdf_policy *policy, const char *sys, int output_fd) |
| 1979 | { |
| 1980 | struct bootc_commands cmds = { |
| 1981 | .dnf_install = oscap_list_new(), |
| 1982 | .dnf_remove = oscap_list_new(), |
| 1983 | }; |
| 1984 | int ret = 0; |
| 1985 | struct oscap_iterator *rules_to_fix_it = oscap_iterator_new(rules_to_fix); |
| 1986 | while (oscap_iterator_has_more(rules_to_fix_it)) { |
| 1987 | struct xccdf_rule *rule = (struct xccdf_rule *) oscap_iterator_next(rules_to_fix_it); |
| 1988 | ret = _xccdf_policy_rule_generate_bootc_fix(policy, rule, sys, &cmds); |
| 1989 | if (ret != 0) |
| 1990 | break; |
| 1991 | } |
| 1992 | oscap_iterator_free(rules_to_fix_it); |
| 1993 | |
| 1994 | _write_text_to_fd(output_fd, "#!/bin/bash\n"); |
| 1995 | _generate_bootc_dnf(&cmds, output_fd); |
| 1996 | |
| 1997 | oscap_list_free(cmds.dnf_install, free); |
| 1998 | oscap_list_free(cmds.dnf_remove, free); |
| 1999 | return ret; |
| 2000 | } |
| 2001 | |
| 2002 | int xccdf_policy_generate_fix(struct xccdf_policy *policy, struct xccdf_result *result, const char *sys, const char *input_file_name, struct oscap_source *tailoring, int output_fd, int raw) |
| 2003 | { |
| 2004 | __attribute__nonnull__(policy); |
| 2005 | int ret = 0; |
| 2006 | const char *tailoring_file_name = tailoring ? oscap_source_get_filepath(tailoring) : NULL; |
| 2007 | |
| 2008 | struct oscap_list *rules_to_fix = oscap_list_new(); |
| 2009 | if (result == NULL) { |
| 2010 | // No TestResult is available. Generate fix from the stock profile. |
| 2011 | dI("Generating profile-oriented fixes for policy(profile/@id=%s)", xccdf_policy_get_id(policy)); |
| 2012 | struct xccdf_benchmark *benchmark = xccdf_policy_get_benchmark(policy); |
| 2013 | |
| 2014 | if (benchmark == NULL) { |
| 2015 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not find benchmark model for policy id='%s' when generating fixes.", xccdf_policy_get_id(policy)); |
| 2016 | oscap_list_free(rules_to_fix, NULL); |
| 2017 | return 1; |
| 2018 | } |
| 2019 | |
| 2020 | if (_write_script_header_to_fd(policy, result, sys, input_file_name, tailoring_file_name, output_fd, raw) != 0) { |
| 2021 | oscap_list_free(rules_to_fix, NULL); |
| 2022 | return 1; |
| 2023 | } |
| 2024 | |
| 2025 | struct xccdf_item_iterator *item_it = xccdf_benchmark_get_content(benchmark); |
| 2026 | while (xccdf_item_iterator_has_more(item_it)) { |
| 2027 | struct xccdf_item *item = xccdf_item_iterator_next(item_it); |
| 2028 | ret = _xccdf_item_recursive_gather_selected_rules(policy, item, rules_to_fix); |
| 2029 | if (ret != 0) |
| 2030 | break; |
| 2031 | } |
| 2032 | xccdf_item_iterator_free(item_it); |
| 2033 | } |
| 2034 | else { |
| 2035 | dI("Generating result-oriented fixes for policy(result/@id=%s)", xccdf_result_get_id(result)); |
| 2036 | |
| 2037 | if (_write_script_header_to_fd(policy, result, sys, input_file_name, tailoring_file_name, output_fd, raw) != 0) { |
| 2038 | oscap_list_free(rules_to_fix, NULL); |
| 2039 | return 1; |
| 2040 | } |
| 2041 | |
| 2042 | struct xccdf_rule_result_iterator *rr_it = xccdf_result_get_rule_results(result); |
| 2043 | while (xccdf_rule_result_iterator_has_more(rr_it)) { |
| 2044 | struct xccdf_rule_result *rr = xccdf_rule_result_iterator_next(rr_it); |
| 2045 | if (xccdf_rule_result_get_result(rr) != XCCDF_RESULT_FAIL) |
| 2046 | continue; |
| 2047 | struct xccdf_rule *rule = _lookup_rule_by_rule_result(policy, rr); |
| 2048 | oscap_list_add(rules_to_fix, rule); |
| 2049 | } |
| 2050 | xccdf_rule_result_iterator_free(rr_it); |
| 2051 | } |
| 2052 | |
| 2053 | // Ansible Playbooks are generated using a different function because |
| 2054 | // in Ansible we have to generate variables first and then tasks |
| 2055 | if (strcmp(sys, "urn:xccdf:fix:script:ansible") == 0) { |
| 2056 | ret = _xccdf_policy_generate_fix_ansible(rules_to_fix, policy, sys, output_fd); |
| 2057 | } else if (strcmp(sys, "urn:redhat:osbuild:blueprint") == 0) { |
| 2058 | ret = _xccdf_policy_generate_fix_blueprint(rules_to_fix, policy, sys, output_fd); |
| 2059 | } else if (strcmp(sys, "urn:xccdf:fix:script:kickstart") == 0) { |
| 2060 | ret = _xccdf_policy_generate_fix_kickstart(rules_to_fix, policy, sys, input_file_name, tailoring, raw, output_fd); |
| 2061 | } else if (strcmp(sys, "urn:xccdf:fix:script:bootc") == 0) { |
| 2062 | ret = _xccdf_policy_generate_fix_bootc(rules_to_fix, policy, sys, output_fd); |
| 2063 | } else { |
| 2064 | ret = _xccdf_policy_generate_fix_other(rules_to_fix, policy, sys, output_fd); |
| 2065 | } |
| 2066 | |
| 2067 | oscap_list_free(rules_to_fix, NULL); |
| 2068 | |
| 2069 | return ret; |
| 2070 | } |
| 2071 |