Debian Codegraph @

utils/oscap-xccdf.c

raw symbols SCIP
1 /*
2 * Copyright 2010--2014 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Peter Vrabec <[email protected]>
21 */
22
23 #include "oscap_string.h"
24 #include "oscap_text.h"
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #if defined(OVAL_PROBES_ENABLED)
30 # include <oval_probe.h>
31 #endif
32 #include <oval_agent_api.h>
33 #include <oval_agent_xccdf_api.h>
34 #include <oval_results.h>
35 #include <oval_variables.h>
36
37 #include <scap_ds.h>
38 #include <xccdf_benchmark.h>
39 #include <xccdf_policy.h>
40 #include <xccdf_session.h>
41 #include <ds_rds_session.h>
42
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #include <errno.h>
47 #include <assert.h>
48 #include <limits.h>
49 #ifdef HAVE_GETOPT_H
50 #include <getopt.h>
51 #endif
52
53 #ifdef OS_WINDOWS
54 #include <io.h>
55 #include <windows.h>
56 #else
57 #include <unistd.h>
58 #endif
59 #if defined(HAVE_SYSLOG_H)
60 #include <syslog.h>
61 #endif
62
63 #include "oscap-tool.h"
64 #include "oscap.h"
65 #include "oscap_source.h"
66 #include <oscap_debug.h>
67 #include "oscap_helpers.h"
68
69 #ifndef O_NOFOLLOW
70 #define O_NOFOLLOW 0
71 #endif
72
73 static int app_evaluate_xccdf(const struct oscap_action *action);
74 static int app_xccdf_validate(const struct oscap_action *action);
75 static int app_xccdf_resolve(const struct oscap_action *action);
76 static int app_xccdf_export_oval_variables(const struct oscap_action *action);
77 static int app_xccdf_remediate(const struct oscap_action *action);
78 static bool getopt_xccdf(int argc, char **argv, struct oscap_action *action);
79 static bool getopt_generate(int argc, char **argv, struct oscap_action *action);
80 static int app_xccdf_xslt(const struct oscap_action *action);
81 static int app_generate_fix(const struct oscap_action *action);
82 static int app_generate_guide(const struct oscap_action *action);
83
84 #define XCCDF_SUBMODULES_NUM 7
85 #define XCCDF_GEN_SUBMODULES_NUM 5 /* See actual arrays
86 initialization below. */
87 static struct oscap_module* XCCDF_SUBMODULES[XCCDF_SUBMODULES_NUM];
88 static struct oscap_module* XCCDF_GEN_SUBMODULES[XCCDF_GEN_SUBMODULES_NUM];
89
90 struct oscap_module OSCAP_XCCDF_MODULE = {
91 .name = "xccdf",
92 .parent = &OSCAP_ROOT_MODULE,
93 .summary = "eXtensible Configuration Checklist Description Format",
94 .usage_extra = "command [command-specific-options]",
95 .opt_parser = getopt_xccdf,
96 .submodules = XCCDF_SUBMODULES
97 };
98
99 static struct oscap_module XCCDF_RESOLVE = {
100 .name = "resolve",
101 .parent = &OSCAP_XCCDF_MODULE,
102 .summary = "Resolve an XCCDF document",
103 .usage = "[options] -o output-xccdf.xml input-xccdf.xml",
104 .help =
105 "Options:\n"
106 " --force - Force resolving XCCDF document even if it is aleready marked as resolved.\n"
107 " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
108 " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
109 " --verbose-log-file <file> - Write verbose information into file.\n",
110 .opt_parser = getopt_xccdf,
111 .func = app_xccdf_resolve
112 };
113
114 static struct oscap_module XCCDF_VALIDATE = {
115 .name = "validate",
116 .parent = &OSCAP_XCCDF_MODULE,
117 .summary = "Validate XCCDF XML content",
118 .usage = "xccdf-file.xml",
119 .opt_parser = getopt_xccdf,
120 .func = app_xccdf_validate,
121 .help = "Options:\n"
122 " --skip-schematron - Do not use schematron-based validation in addition to XML Schema\n"
123 " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
124 " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
125 " --verbose-log-file <file> - Write verbose information into file.\n",
126 };
127
128 static struct oscap_module XCCDF_EXPORT_OVAL_VARIABLES = {
129 .name = "export-oval-variables",
130 .parent = &OSCAP_XCCDF_MODULE,
131 .summary = "Export XCCDF values as OVAL external-variables document(s)",
132 .usage = "[options] <xccdf benchmark file> [oval definitions files]",
133 .opt_parser = getopt_xccdf,
134 .func = app_xccdf_export_oval_variables,
135 .help = "Options:\n"
136 " --profile <name> - The name of Profile to be evaluated.\n"
137 " --skip-validation - Skip validation.\n"
138 " --fetch-remote-resources - Download remote content referenced by XCCDF.\n"
139 " --local-files <dir> - Use locally downloaded copies of remote resources stored in the given directory.\n"
140 " --datastream-id <id> - ID of the data stream in the collection to use.\n"
141 " (only applicable for source data streams)\n"
142 " --xccdf-id <id> - ID of component-ref with XCCDF in the data stream that should be evaluated.\n"
143 " (only applicable for source data streams)\n"
144 " --benchmark-id <id> - ID of XCCDF Benchmark in some component in the data stream that should be evaluated.\n"
145 " (only applicable for source data streams)\n"
146 " (only applicable when datastream-id AND xccdf-id are not specified)\n"
147 " --cpe <name> - Use given CPE dictionary or language (autodetected)\n"
148 " for applicability checks.\n"
149 " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
150 " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
151 " --verbose-log-file <file> - Write verbose information into file.\n",
152 };
153
154 static struct oscap_module XCCDF_EVAL = {
155 .name = "eval",
156 .parent = &OSCAP_XCCDF_MODULE,
157 .summary = "Perform evaluation driven by XCCDF file and use OVAL as checking engine",
158 .usage = "[options] INPUT_FILE [oval-definitions-files]",
159 .help =
160 "INPUT_FILE - XCCDF file or a source data stream file\n\n"
161 "Options:\n"
162 " --profile <name> - The name of Profile to be evaluated.\n"
163 " --rule <name> - The name of a single rule to be evaluated.\n"
164 " --skip-rule <name> - The name of the rule to be skipped.\n"
165 " --reference <NAME:ID> - Evaluate only rules that have the given reference.\n"
166 " --tailoring-file <file> - Use given XCCDF Tailoring file.\n"
167 " --tailoring-id <component-id> - Use given DS component as XCCDF Tailoring file.\n"
168 " --cpe <name> - Use given CPE dictionary or language (autodetected)\n"
169 " for applicability checks.\n"
170 " --oval-results - Save OVAL results as well.\n"
171 " --check-engine-results - Save results from check engines loaded from plugins as well.\n"
172 " --export-variables - Export OVAL external variables provided by XCCDF.\n"
173 " --results <file> - Write XCCDF Results into file.\n"
174 " --results-arf <file> - Write ARF (result data stream) into file.\n"
175 " --stig-viewer <file> - Writes XCCDF results into FILE in a format readable by DISA STIG Viewer\n"
176 " --thin-results - Thin Results provides only minimal amount of information in OVAL/ARF results.\n"
177 " The option --without-syschar is automatically enabled when you use Thin Results.\n"
178 " --without-syschar - Don't provide system characteristic in OVAL/ARF result files.\n"
179 " --report <file> - Write HTML report into file.\n"
180 " --skip-validation - Skip validation.\n"
181 " --skip-signature-validation - Skip data stream signature validation.\n"
182 " (only applicable for source data streams)\n"
183 " --enforce-signature - Process only signed data streams.\n"
184 " --fetch-remote-resources - Download remote content referenced by XCCDF.\n"
185 " --local-files <dir> - Use locally downloaded copies of remote resources stored in the given directory.\n"
186 " --progress - Switch to sparse output suitable for progress reporting.\n"
187 " Format is \"$rule_id:$result\\n\".\n"
188 " --progress-full - Switch to sparse but a bit more saturated output also suitable for progress reporting.\n"
189 " Format is \"$rule_id|$rule_title|$result\\n\".\n"
190 " --datastream-id <id> - ID of the data stream in the collection to use.\n"
191 " (only applicable for source data streams)\n"
192 " --xccdf-id <id> - ID of component-ref with XCCDF in the data stream that should be evaluated.\n"
193 " (only applicable for source data streams)\n"
194 " --benchmark-id <id> - ID of XCCDF Benchmark in some component in the data stream that should be evaluated.\n"
195 " (only applicable for source data streams)\n"
196 " (only applicable when datastream-id AND xccdf-id are not specified)\n"
197 " --remediate - Automatically execute XCCDF fix elements for failed rules.\n"
198 " Use of this option is always at your own risk.\n"
199 " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
200 " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
201 " --verbose-log-file <file> - Write verbose information into file.\n"
202 " --show-rule-details - Show rule description and rationale when reporting results.\n",
203 .opt_parser = getopt_xccdf,
204 .func = app_evaluate_xccdf
205 };
206
207 static struct oscap_module XCCDF_REMEDIATE = {
208 .name = "remediate",
209 .parent = &OSCAP_XCCDF_MODULE,
210 .summary = "Perform remediation driven by XCCDF TestResult file or ARF.",
211 .usage = "[options] INPUT_FILE [oval-definitions-files]",
212 .help = "INPUT_FILE - XCCDF TestResult file or ARF\n\n"
213 "Options:\n"
214 " --result-id - TestResult ID to be processed. Default is the most recent one.\n"
215 " --skip-validation - Skip validation.\n"
216 " --cpe <name> - Use given CPE dictionary or language (autodetected)\n"
217 " for applicability checks.\n"
218 " --fetch-remote-resources - Download remote content referenced by XCCDF.\n"
219 " --local-files <dir> - Use locally downloaded copies of remote resources stored in the given directory.\n"
220 " --results <file> - Write XCCDF Results into file.\n"
221 " --results-arf <file> - Write ARF (result data stream) into file.\n"
222 " --stig-viewer <file> - Writes XCCDF results into FILE in a format readable by DISA STIG Viewer\n"
223 " --report <file> - Write HTML report into file.\n"
224 " --oval-results - Save OVAL results.\n"
225 " --export-variables - Export OVAL external variables provided by XCCDF.\n"
226 " --check-engine-results - Save results from check engines loaded from plugins as well.\n"
227 " --progress - Switch to sparse output suitable for progress reporting.\n"
228 " Format is \"$rule_id:$result\\n\".\n"
229 " --progress-full - Switch to sparse but a bit more saturated output also suitable for progress reporting.\n"
230 " Format is \"$rule_id|$rule_title|$result\\n\".\n"
231 " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
232 " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
233 " --verbose-log-file <file> - Write verbose information into file.\n",
234 .opt_parser = getopt_xccdf,
235 .func = app_xccdf_remediate
236 };
237
238 #define GEN_OPTS \
239 "Generate options:\n" \
240 " --profile <profile-id> - Apply profile with given ID to the Benchmark before further processing takes place.\n"
241
242 static struct oscap_module XCCDF_GENERATE = {
243 .name = "generate",
244 .parent = &OSCAP_XCCDF_MODULE,
245 .summary = "Convert XCCDF Benchmark to other formats",
246 .usage = "[options]",
247 .usage_extra = "<subcommand> [sub-options] benchmark-file.xml",
248 .help = GEN_OPTS,
249 .opt_parser = getopt_generate,
250 .submodules = XCCDF_GEN_SUBMODULES
251 };
252
253 static struct oscap_module XCCDF_GEN_REPORT = {
254 .name = "report",
255 .parent = &XCCDF_GENERATE,
256 .summary = "Generate results report",
257 .usage = "[options] xccdf-file.xml",
258 .help = GEN_OPTS
259 "\nReport Options:\n"
260 " --result-id <id> - TestResult ID to be processed. Default is the most recent one.\n"
261 " --output <file> - Write the document into file.\n"
262 " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
263 " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
264 " --verbose-log-file <file> - Write verbose information into file.\n",
265 .opt_parser = getopt_xccdf,
266 .user = "xccdf-report.xsl",
267 .func = app_xccdf_xslt
268 };
269
270 static struct oscap_module XCCDF_GEN_GUIDE = {
271 .name = "guide",
272 .parent = &XCCDF_GENERATE,
273 .summary = "Generate security guide",
274 .usage = "[options] xccdf-file.xml",
275 .help = GEN_OPTS
276 "\nGuide Options:\n"
277 " --output <file> - Write the document into file.\n"
278 " --hide-profile-info - This option has no effect.\n"
279 " --benchmark-id <id> - ID of XCCDF Benchmark in some component in the data stream that should be used.\n"
280 " (only applicable for source data streams)\n"
281 " --xccdf-id <id> - ID of component-ref with XCCDF in the data stream that should be evaluated.\n"
282 " (only applicable for source data streams)\n"
283 " --tailoring-file <file> - Use given XCCDF Tailoring file.\n"
284 " (only applicable for source data streams)\n"
285 " --tailoring-id <component-id> - Use given DS component as XCCDF Tailoring file.\n"
286 " (only applicable for source data streams)\n"
287 " --skip-signature-validation - Skip data stream signature validation.\n"
288 " (only applicable for source data streams)\n"
289 " --enforce-signature - Process only signed data streams.\n"
290 " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
291 " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
292 " --verbose-log-file <file> - Write verbose information into file.\n",
293 .opt_parser = getopt_xccdf,
294 .user = NULL,
295 .func = app_generate_guide
296 };
297
298 static struct oscap_module XCCDF_GEN_FIX = {
299 .name = "fix",
300 .parent = &XCCDF_GENERATE,
301 .summary = "Generate a fix script from an XCCDF file",
302 .usage = "[options] xccdf-file.xml",
303 .help = GEN_OPTS
304 "\nFix Options:\n"
305 " --fix-type <type> - Fix type. Should be one of: bash, ansible, puppet, anaconda, ignition, kubernetes,\n"
306 " blueprint, kickstart, bootc (default: bash).\n"
307 " --output <file> - Write the script into file.\n"
308 " --raw - Don't write extra headers or boilerplate instructions, only compose the content snippets.\n"
309 " --result-id <id> - Fixes will be generated for failed rule-results of the specified TestResult.\n"
310 " --benchmark-id <id> - ID of XCCDF Benchmark in some component in the data stream that should be used.\n"
311 " (only applicable for source data streams)\n"
312 " --xccdf-id <id> - ID of component-ref with XCCDF in the data stream that should be evaluated.\n"
313 " (only applicable for source data streams)\n"
314 " --tailoring-file <file> - Use given XCCDF Tailoring file.\n"
315 " (only applicable for source data streams)\n"
316 " --tailoring-id <component-id> - Use given DS component as XCCDF Tailoring file.\n"
317 " (only applicable for source data streams)\n"
318 " --skip-signature-validation - Skip data stream signature validation.\n"
319 " (only applicable for source data streams)\n"
320 " --enforce-signature - Process only signed data streams.\n"
321 " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
322 " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
323 " --verbose-log-file <file> - Write verbose information into file.\n",
324 .opt_parser = getopt_xccdf,
325 .user = "legacy-fix.xsl",
326 .func = app_generate_fix
327 };
328
329 static struct oscap_module XCCDF_GEN_CUSTOM = {
330 .name = "custom",
331 .parent = &XCCDF_GENERATE,
332 .summary = "Generate a custom output (depending on given XSLT file) from an XCCDF file",
333 .usage = "--stylesheet <file> [--output <file>] xccdf-file.xml",
334 .help = GEN_OPTS
335 "\nCustom Options:\n"
336 " --stylesheet <file> - Specify an absolute path to a custom stylesheet to format the output.\n"
337 " --output <file> - Write the document into file.\n"
338 " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
339 " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
340 " --verbose-log-file <file> - Write verbose information into file.\n",
341 .opt_parser = getopt_xccdf,
342 .user = NULL,
343 .func = app_xccdf_xslt
344 };
345
346 static struct oscap_module* XCCDF_GEN_SUBMODULES[XCCDF_GEN_SUBMODULES_NUM] = {
347 &XCCDF_GEN_REPORT,
348 &XCCDF_GEN_GUIDE,
349 &XCCDF_GEN_FIX,
350 &XCCDF_GEN_CUSTOM,
351 NULL
352 };
353
354 static struct oscap_module* XCCDF_SUBMODULES[XCCDF_SUBMODULES_NUM] = {
355 &XCCDF_EVAL,
356 &XCCDF_RESOLVE,
357 &XCCDF_VALIDATE,
358 &XCCDF_EXPORT_OVAL_VARIABLES,
359 &XCCDF_GENERATE,
360 &XCCDF_REMEDIATE,
361 NULL
362 };
363
364 enum progress_output_opt {
365 PROGRESS_OPT_NONE = 0,
366 PROGRESS_OPT_SPARSE,
367 PROGRESS_OPT_FULL,
368 };
369
370 /**
371 * XCCDF Result Colors:
372 * PASS:green(32), FAIL:red(31), ERROR:lred(1;31), UNKNOWN:grey(1;30), NOT_APPLICABLE:default bold(1), NOT_CHECKED:default bold(1),
373 * NOT_SELECTED:default dim(2), INFORMATIONAL:blue(34), FIXED:yellow(1;33)
374 */
375 #if defined(OS_WINDOWS)
376 int RESULT_COLORS[] = {0, 10, 12, 12, 8, 15, 15, 15, 9, 14};
377 #else
378 static const char * RESULT_COLORS[] = {"", "32", "31", "1;31", "1;30", "1", "1", "2", "34", "1;33" };
379 #endif
380
381 static char custom_stylesheet_path[PATH_MAX];
382
383 static void _print_rule_details(struct xccdf_policy *policy, struct xccdf_item *item)
384 {
385 if (!xccdf_policy_get_show_rule_details(policy)) {
386 return;
387 }
388 char *description = xccdf_policy_get_readable_item_description(policy, item, NULL);
389 if (strlen(description) > 0) {
390 char *indented_description = oscap_indent(description, 8);
391 printf("Description\n%s\n", indented_description);
392 free(indented_description);
393 }
394 free(description);
395 char *rationale = xccdf_policy_get_readable_item_rationale(policy, item, NULL);
396 if (strlen(rationale) > 0) {
397 char *indented_rationale = oscap_indent(rationale, 8);
398 printf("Rationale\n%s\n", indented_rationale);
399 free(indented_rationale);
400 }
401 free(rationale);
402 struct oscap_stringlist *warnings = xccdf_rule_get_warnings_strings((struct xccdf_rule *) item);
403 struct oscap_string_iterator *warnings_it = oscap_stringlist_get_strings(warnings);
404 while (oscap_string_iterator_has_more(warnings_it)) {
405 const char *warning = oscap_string_iterator_next(warnings_it);
406 char *indented_warning = oscap_indent(warning, 8);
407 printf("Warning\n%s\n", indented_warning);
408 free(indented_warning);
409 }
410 oscap_string_iterator_free(warnings_it);
411 oscap_stringlist_free(warnings);
412 }
413
414 static int callback_scr_rule(struct xccdf_rule *rule, void *arg)
415 {
416 struct xccdf_policy *policy = (struct xccdf_policy *) arg;
417 const char * rule_id = xccdf_rule_get_id(rule);
418
419 /* is rule selected? we print only selected rules */
420 const bool selected = xccdf_policy_is_item_selected(policy, rule_id);
421 if (!selected)
422 return 0;
423
424 const char *title = xccdf_policy_get_readable_item_title(policy, (struct xccdf_item *) rule, NULL);
425
426 /* print */
427 if (isatty(1)) {
428 #if defined(OS_WINDOWS)
429 HANDLE console;
430 console = GetStdHandle(STD_OUTPUT_HANDLE);
431 printf("Title");
432 SetConsoleTextAttribute(console, 15);
433 printf("\t%s\n", title);
434 SetConsoleTextAttribute(console, 7);
435 #else
436 printf("Title\r\t\033[1m%s\033[0;0m\n", title);
437 #endif
438 } else
439 printf("Title\r\t%s\n", title);
440 free((char *)title);
441 #if defined(OS_WINDOWS)
442 printf("Rule\t%s\n", rule_id);
443 #else
444 printf("Rule\r\t%s\n", rule_id);
445 #endif
446 _print_rule_details(policy, (struct xccdf_item *) rule);
447
448 struct xccdf_ident_iterator *idents = xccdf_rule_get_idents(rule);
449 while (xccdf_ident_iterator_has_more(idents)) {
450 const struct xccdf_ident *ident = xccdf_ident_iterator_next(idents);
451 const char *ident_id = xccdf_ident_get_id(ident);
452 #if defined(OS_WINDOWS)
453 printf("Ident\t%s\n", ident_id);
454 #else
455 printf("Ident\r\t%s\n", ident_id);
456 #endif
457 }
458 xccdf_ident_iterator_free(idents);
459
460 fflush(stdout);
461
462 return 0;
463 }
464
465 static int callback_scr_result(struct xccdf_rule_result *rule_result, void *arg)
466 {
467 xccdf_test_result_type_t result = xccdf_rule_result_get_result(rule_result);
468
469 /* is result from selected rule? we print only selected rules */
470 if (result == XCCDF_RESULT_NOT_SELECTED)
471 return 0;
472
473 /* print result */
474 #if defined(OS_WINDOWS)
475 printf("Result\t");
476 #else
477 printf("Result\r\t");
478 #endif
479 const char * result_str = xccdf_test_result_type_get_text(result);
480 if (isatty(1)) {
481 #if defined(OS_WINDOWS)
482 HANDLE console;
483 console = GetStdHandle(STD_OUTPUT_HANDLE);
484 SetConsoleTextAttribute(console, RESULT_COLORS[result]);
485 printf("%s", result_str);
486 SetConsoleTextAttribute(console, 7);
487 printf("\n\n");
488 #else
489 printf("\033[%sm%s\033[0m\n\n", RESULT_COLORS[result], result_str);
490 #endif
491 } else
492 printf("%s\n\n", result_str);
493
494 return 0;
495 }
496
497 static int callback_scr_result_progress(struct xccdf_rule_result *rule_result, void *arg)
498 {
499 xccdf_test_result_type_t result = xccdf_rule_result_get_result(rule_result);
500
501 /* is result from selected rule? we print only selected rules */
502 if (result == XCCDF_RESULT_NOT_SELECTED)
503 return 0;
504
505 /* print result */
506 const char *rule_id = xccdf_rule_result_get_idref(rule_result);
507 const char *result_str = xccdf_test_result_type_get_text(result);
508
509 printf("%s:%s\n", rule_id, result_str);
510 fflush(stdout);
511
512 return 0;
513 }
514
515 static int callback_scr_rule_progress_full(struct xccdf_rule *rule, void *arg)
516 {
517 const char *rule_id = xccdf_rule_get_id(rule);
518
519 if (!xccdf_policy_is_item_selected((struct xccdf_policy *)arg, rule_id))
520 return 0;
521
522 char *title = xccdf_policy_get_readable_item_title((struct xccdf_policy *)arg, (struct xccdf_item *)rule, NULL);
523 printf("%s|%s|", rule_id, title);
524 free(title);
525 fflush(stdout);
526
527 return 0;
528 }
529
530 static int callback_scr_result_progress_full(struct xccdf_rule_result *rule_result, void *arg)
531 {
532 xccdf_test_result_type_t result = xccdf_rule_result_get_result(rule_result);
533
534 /* is result from selected rule? we print only selected rules */
535 if (result == XCCDF_RESULT_NOT_SELECTED)
536 return 0;
537
538 /* print result */
539 const char *result_str = xccdf_test_result_type_get_text(result);
540
541 printf("%s\n", result_str);
542 fflush(stdout);
543
544 return 0;
545 }
546
547 static int callback_scr_multicheck(struct oval_definition *definition, void *arg)
548 {
549 printf("OVAL Definition ID\t%s\n", oval_definition_get_id(definition));
550 printf("OVAL Definition Title\t%s\n", oval_definition_get_title(definition));
551 return 0;
552 }
553
554 /*
555 * Send XCCDF Rule Results info message to syslog
556 *
557 static int callback_syslog_result(struct xccdf_rule_result *rule_result, void *arg)
558 {
559 xccdf_test_result_type_t result = xccdf_rule_result_get_result(rule_result);
560
561 // do we log it?
562 if ((result != XCCDF_RESULT_FAIL) && (result != XCCDF_RESULT_UNKNOWN))
563 return 0;
564
565 // yes we do
566 const char * result_str = xccdf_test_result_type_get_text(result);
567 const char * ident_id = NULL;
568 int priority = LOG_NOTICE;
569
570 // get ident
571 struct xccdf_ident_iterator *idents = xccdf_rule_result_get_idents(rule_result);
572 if (xccdf_ident_iterator_has_more(idents)) {
573 const struct xccdf_ident *ident = xccdf_ident_iterator_next(idents);
574 ident_id = xccdf_ident_get_id(ident);
575 }
576 xccdf_ident_iterator_free(idents);
577
578 // emit the message
579 syslog(priority, "Rule: %s, Ident: %s, Result: %s.", xccdf_rule_result_get_idref(rule_result), ident_id, result_str);
580
581 return 0;
582 }
583 */
584
585
586 static void _register_progress_callback(struct xccdf_session *session, int progress)
587 {
588 struct xccdf_policy_model *policy_model = xccdf_session_get_policy_model(session);
589 if (progress == PROGRESS_OPT_SPARSE) {
590 xccdf_policy_model_register_output_callback(policy_model, callback_scr_result_progress, NULL);
591 } else if (progress == PROGRESS_OPT_FULL) {
592 xccdf_policy_model_register_start_callback(policy_model, callback_scr_rule_progress_full,
593 (void *) xccdf_session_get_xccdf_policy(session));
594 xccdf_policy_model_register_output_callback(policy_model, callback_scr_result_progress_full, NULL);
595 } else {
596 xccdf_policy_model_register_start_callback(policy_model, callback_scr_rule,
597 (void *) xccdf_session_get_xccdf_policy(session));
598 xccdf_policy_model_register_output_callback(policy_model, callback_scr_result, NULL);
599 xccdf_policy_model_register_multicheck_callback(policy_model, callback_scr_multicheck, NULL);
600 }
601 /* xccdf_policy_model_register_output_callback(policy_model, callback_syslog_result, NULL); */
602 }
603
604 void report_missing_profile(const char *profile_suffix, const char *source_file)
605 {
606 fprintf(stderr,
607 "No profile matching suffix \"%s\" was found. Get available profiles using:\n"
608 "$ oscap info \"%s\"\n", profile_suffix, source_file);
609 }
610
611 void report_multiple_profile_matches(const char *profile_suffix, const char *source_file)
612 {
613 fprintf(stderr,
614 "At least two profiles matched suffix \"%s\". Use a more specific suffix "
615 "to get an exact match. Get list of profiles using:\n"
616 "$ oscap info \"%s\"\n", profile_suffix, source_file);
617 }
618
619 int evaluate_suffix_match_result_with_custom_reports(int suffix_match_result, const char *profile_suffix, const char *source_file,
620 void (* report_missing)(const char *, const char *), void (* report_multiple)(const char *, const char *))
621 {
622 if (suffix_match_result == OSCAP_PROFILE_NO_MATCH) {
623 if (report_missing)
624 report_missing(profile_suffix, source_file);
625 return OSCAP_ERROR;
626 } else if (suffix_match_result == OSCAP_PROFILE_MULTIPLE_MATCHES) {
627 if (report_multiple)
628 report_multiple(profile_suffix, source_file);
629 return OSCAP_ERROR;
630 }
631 return OSCAP_OK;
632 }
633
634 int evaluate_suffix_match_result(int suffix_match_result, const char *profile_suffix, const char *source_file)
635 {
636 return evaluate_suffix_match_result_with_custom_reports(suffix_match_result, profile_suffix, source_file, &report_missing_profile, &report_multiple_profile_matches);
637 }
638
639 int xccdf_set_profile_or_report_bad_id(struct xccdf_session *session, const char *profile_id, const char *source_file)
640 {
641 const int suffix_match_result = xccdf_session_set_profile_id_by_suffix(session, profile_id);
642 int return_code = evaluate_suffix_match_result(suffix_match_result, profile_id, source_file);
643 return return_code;
644 }
645
646
647 static bool _system_is_in_bootc_mode(void)
648 {
649 #ifdef OS_WINDOWS
650 return false;
651 #else
652 #define BOOTC_PATH "/usr/bin/bootc"
653 #define CHUNK_SIZE 1024
654 struct stat statbuf;
655 if (stat(BOOTC_PATH, &statbuf) == -1) {
656 return false;
657 }
658 FILE *output = popen(BOOTC_PATH " status --format json 2>/dev/null", "r");
659 if (output == NULL) {
660 return false;
661 }
662 size_t buf_size = CHUNK_SIZE;
663 char *buf = calloc(buf_size, sizeof(char));
664 if (buf == NULL) {
665 pclose(output);
666 return false;
667 }
668 int c;
669 size_t i = 0;
670 while ((c = fgetc(output)) != EOF) {
671 if (i >= buf_size) {
672 buf_size += CHUNK_SIZE;
673 char *new_buf = realloc(buf, buf_size);
674 if (new_buf == NULL) {
675 pclose(output);
676 return false;
677 }
678 buf = new_buf;
679 }
680 buf[i++] = c;
681 }
682 pclose(output);
683 bool result = (*buf != '\0' && strstr(buf, "\"booted\":null") == NULL);
684 free(buf);
685 return result;
686 #endif
687 }
688
689 /**
690 * XCCDF Processing fucntion
691 * @param action OSCAP Action structure
692 * @param sess OVAL Agent Session
693 */
694 int app_evaluate_xccdf(const struct oscap_action *action)
695 {
696 struct xccdf_session *session = NULL;
697
698 int result = OSCAP_ERROR;
699
700 if (action->remediate && _system_is_in_bootc_mode()) {
701 fprintf(stderr,
702 "Detected running Image Mode operating system. OpenSCAP can't "
703 "perform remediation of this system because majority of the "
704 "system is read-only. Please apply remediation during bootable "
705 "container image build using 'oscap-im' instead.\n");
706 return result;
707 }
708
709 #if defined(HAVE_SYSLOG_H)
710 int priority = LOG_NOTICE;
711
712 /* syslog message */
713 syslog(priority, "Evaluation started. Content: %s, Profile: %s.", action->f_xccdf, action->profile);
714 #endif
715 session = xccdf_session_new(action->f_xccdf);
716 if (session == NULL)
717 goto cleanup;
718 xccdf_session_set_validation(session, action->validate, getenv("OSCAP_FULL_VALIDATION") != NULL);
719 xccdf_session_set_signature_validation(session, action->validate_signature);
720 xccdf_session_set_signature_enforcement(session, action->enforce_signature);
721 if (action->thin_results) {
722 xccdf_session_set_thin_results(session, true);
723 xccdf_session_set_without_sys_chars_export(session, true);
724 }
725 if (xccdf_session_is_sds(session)) {
726 xccdf_session_set_datastream_id(session, action->f_datastream_id);
727 xccdf_session_set_component_id(session, action->f_xccdf_id);
728 xccdf_session_set_benchmark_id(session, action->f_benchmark_id);
729 }
730 xccdf_session_set_user_cpe(session, action->cpe);
731 // The tailoring_file may be NULL but the tailoring file may have been
732 // autonegotiated from the input file, we don't want to lose that.
733 if (action->tailoring_file != NULL)
734 xccdf_session_set_user_tailoring_file(session, action->tailoring_file);
735 xccdf_session_set_user_tailoring_cid(session, action->tailoring_id);
736 xccdf_session_configure_remote_resources(session, action->remote_resources, action->local_files, download_reporting_callback);
737 xccdf_session_set_custom_oval_files(session, action->f_ovals);
738 xccdf_session_set_product_cpe(session, OSCAP_PRODUCTNAME);
739 struct oscap_string_iterator *it = oscap_stringlist_get_strings(action->rules);
740 while (oscap_string_iterator_has_more(it)) {
741 const char *rid = oscap_string_iterator_next(it);
742 xccdf_session_add_rule(session, rid);
743 }
744 oscap_string_iterator_free(it);
745 struct oscap_string_iterator *sit = oscap_stringlist_get_strings(action->skip_rules);
746 while (oscap_string_iterator_has_more(sit)) {
747 const char *rid = oscap_string_iterator_next(sit);
748 xccdf_session_skip_rule(session, rid);
749 }
750 oscap_string_iterator_free(sit);
751 if (action->reference) {
752 if (strchr(action->reference, ':') == NULL) {
753 fprintf(stderr, "The --reference argument needs to be in form NAME:IDENTIFIER, using a colon as a separator.\n");
754 goto cleanup;
755 }
756 xccdf_session_set_reference_filter(session, action->reference);
757 }
758 if (action->show_rule_details)
759 xccdf_session_set_show_rule_details(session, true);
760
761 if (xccdf_session_load(session) != 0)
762 goto cleanup;
763
764
765 /* Select profile */
766 if (!xccdf_session_set_profile_id(session, action->profile)) {
767 if (action->profile != NULL) {
768 if (xccdf_set_profile_or_report_bad_id(session, action->profile, action->f_xccdf) == OSCAP_ERROR)
769 goto cleanup;
770 } else {
771 fprintf(stderr, "No Policy was found for default profile.\n");
772 goto cleanup;
773 }
774 }
775
776 _register_progress_callback(session, action->progress);
777
778 if (action->progress == PROGRESS_OPT_SPARSE) {
779 // Don't pronounce phases in this mode
780 } else if (action->progress == PROGRESS_OPT_FULL) {
781 printf("---evaluation\n");
782 } else {
783 printf("--- Starting Evaluation ---\n\n");
784 }
785
786 /* Perform evaluation */
787 if (xccdf_session_evaluate(session) != 0)
788 goto cleanup;
789
790 xccdf_session_set_without_sys_chars_export(session, action->without_sys_chars);
791 xccdf_session_set_oval_results_export(session, action->oval_results);
792 xccdf_session_set_oval_variables_export(session, action->export_variables);
793 xccdf_session_set_arf_export(session, action->f_results_arf);
794
795 if (xccdf_session_export_oval(session) != 0)
796 goto cleanup;
797 else if (action->validate && getenv("OSCAP_FULL_VALIDATION") != NULL &&
798 (action->oval_results == true || action->f_results_arf))
799 fprintf(stdout, "OVAL Results are exported correctly.\n");
800
801 xccdf_session_set_check_engine_plugins_results_export(session, action->check_engine_results);
802 if (xccdf_session_export_check_engine_plugins(session) != 0)
803 goto cleanup;
804
805 if (action->remediate) {
806 if (action->progress == PROGRESS_OPT_SPARSE) {
807 // Don't pronounce phases in this mode
808 } else if (action->progress == PROGRESS_OPT_FULL) {
809 printf("---remediation\n");
810 } else {
811 printf("\n--- Starting Remediation ---\n\n");
812 }
813 xccdf_session_remediate(session);
814 }
815
816 /* Get the result from TestResult model and decide if end with error or with correct return code */
817 int evaluation_result = xccdf_session_contains_fail_result(session) ? OSCAP_FAIL : OSCAP_OK;
818
819 /* syslog message */
820 #if defined(HAVE_SYSLOG_H)
821 syslog(priority, "Evaluation finished. Return code: %d, Base score %f.", evaluation_result, xccdf_session_get_base_score(session));
822 #endif
823
824 xccdf_session_set_xccdf_export(session, action->f_results);
825 xccdf_session_set_xccdf_stig_viewer_export(session, action->f_results_stig);
826 xccdf_session_set_report_export(session, action->f_report);
827 if (xccdf_session_export_all(session) != 0)
828 goto cleanup;
829
830 if (action->validate && getenv("OSCAP_FULL_VALIDATION") != NULL &&
831 (action->f_results || action->f_report || action->f_results_arf || action->f_results_stig))
832 fprintf(stdout, "XCCDF Results are exported correctly.\n");
833
834 if (action->f_results_arf && getenv("OSCAP_FULL_VALIDATION") != NULL)
835 fprintf(stdout, "Result data stream exported correctly.\n");
836
837 result = evaluation_result;
838
839 cleanup:
840 xccdf_session_free(session);
841 oscap_print_error();
842 return result;
843 }
844
845 static xccdf_test_result_type_t resolve_variables_wrapper(struct xccdf_policy *policy, const char *rule_id,
846 const char *id, const char *href, struct xccdf_value_binding_iterator *bnd_itr,
847 struct xccdf_check_import_iterator *check_import_it, void *usr)
848 {
849 if (0 != oval_agent_resolve_variables((struct oval_agent_session *) usr, bnd_itr))
850 return XCCDF_RESULT_UNKNOWN;
851
852 return XCCDF_RESULT_PASS;
853 }
854
855 static int app_xccdf_export_oval_variables(const struct oscap_action *action)
856 {
857 struct xccdf_policy *policy = NULL;
858 struct xccdf_result *xres;
859 int result = OSCAP_ERROR;
860 struct xccdf_session *session = NULL;
861
862 session = xccdf_session_new(action->f_xccdf);
863 if (session == NULL)
864 goto cleanup;
865
866 xccdf_session_set_validation(session, action->validate, getenv("OSCAP_FULL_VALIDATION") != NULL);
867
868 if (xccdf_session_is_sds(session)) {
869 xccdf_session_set_datastream_id(session, action->f_datastream_id);
870 xccdf_session_set_component_id(session, action->f_xccdf_id);
871 xccdf_session_set_benchmark_id(session, action->f_benchmark_id);
872 }
873 xccdf_session_set_user_cpe(session, action->cpe);
874 xccdf_session_configure_remote_resources(session, action->remote_resources, action->local_files, download_reporting_callback);
875 xccdf_session_set_custom_oval_files(session, action->f_ovals);
876 xccdf_session_set_custom_oval_eval_fn(session, resolve_variables_wrapper);
877
878 if (xccdf_session_load(session) != 0)
879 goto cleanup;
880
881 /* select a profile */
882 policy = xccdf_policy_model_get_policy_by_id(xccdf_session_get_policy_model(session), action->profile);
883 if (policy == NULL) {
884 if (action->profile != NULL)
885 report_missing_profile(action->profile, action->f_xccdf);
886 else
887 fprintf(stderr, "No Policy was found for default profile.\n");
888 goto cleanup;
889 }
890
891 /* perform evaluation */
892 xres = xccdf_policy_evaluate(policy);
893 if (xres == NULL)
894 goto cleanup;
895
896 xccdf_session_set_oval_variables_export(session, true);
897 if (xccdf_session_export_oval(session) == 0)
898 result = OSCAP_OK;
899
900 cleanup:
901 oscap_print_error();
902 if (session != NULL)
903 xccdf_session_free(session);
904
905 return result;
906 }
907
908 int app_xccdf_remediate(const struct oscap_action *action)
909 {
910 struct xccdf_session *session = NULL;
911 int result = OSCAP_ERROR;
912 if (_system_is_in_bootc_mode()) {
913 fprintf(stderr,
914 "Detected running Image Mode operating system. OpenSCAP can't "
915 "perform remediation of this system because majority of the "
916 "system is read-only. Please apply remediation during bootable "
917 "container image build using 'oscap-im' instead.\n");
918 return result;
919 }
920 session = xccdf_session_new(action->f_xccdf);
921 if (session == NULL)
922 goto cleanup;
923 xccdf_session_set_validation(session, action->validate, getenv("OSCAP_FULL_VALIDATION") != NULL);
924 xccdf_session_set_user_cpe(session, action->cpe);
925 xccdf_session_configure_remote_resources(session, action->remote_resources, action->local_files, download_reporting_callback);
926 xccdf_session_set_custom_oval_files(session, action->f_ovals);
927
928 if (xccdf_session_load(session) != 0)
929 goto cleanup;
930
931 if (xccdf_session_build_policy_from_testresult(session, action->id) != 0)
932 goto cleanup;
933
934 _register_progress_callback(session, action->progress);
935
936 xccdf_session_remediate(session);
937
938 xccdf_session_set_oval_results_export(session, action->oval_results);
939 xccdf_session_set_oval_variables_export(session, action->export_variables);
940 xccdf_session_set_arf_export(session, action->f_results_arf);
941 xccdf_session_set_xccdf_export(session, action->f_results);
942 xccdf_session_set_xccdf_stig_viewer_export(session, action->f_results_stig);
943 xccdf_session_set_report_export(session, action->f_report);
944
945 if (xccdf_session_export_oval(session) != 0)
946 goto cleanup;
947
948 xccdf_session_set_check_engine_plugins_results_export(session, action->check_engine_results);
949 if (xccdf_session_export_check_engine_plugins(session) != 0)
950 goto cleanup;
951
952 /* Get the result from TestResult model and decide if end with error or with correct return code */
953 int evaluation_result = xccdf_session_contains_fail_result(session) ? OSCAP_FAIL : OSCAP_OK;
954
955 if (xccdf_session_export_all(session) != 0)
956 goto cleanup;
957
958 result = evaluation_result;
959
960 cleanup:
961 xccdf_session_free(session);
962 oscap_print_error();
963 return result;
964 }
965
966 int app_xccdf_resolve(const struct oscap_action *action)
967 {
968 int ret = OSCAP_ERROR;
969 struct xccdf_benchmark *bench = NULL;
970
971 if (!action->f_xccdf) {
972 fprintf(stderr, "No input document specified!\n");
973 return OSCAP_ERROR;
974 }
975 if (!action->f_results) {
976 fprintf(stderr, "No output document filename specified!\n");
977 return OSCAP_ERROR;
978 }
979
980 struct oscap_source *source = oscap_source_new_from_file(action->f_xccdf);
981 /* validate input */
982 if (action->validate) {
983 if (oscap_source_validate(source, reporter, (void *) action) != 0) {
984 oscap_source_free(source);
985 goto cleanup;
986 }
987 }
988
989 bench = xccdf_benchmark_import_source(source);
990 oscap_source_free(source);
991 if (!bench)
992 goto cleanup;
993
994 if (action->force)
995 xccdf_benchmark_set_resolved(bench, false);
996
997 if (xccdf_benchmark_get_resolved(bench))
998 fprintf(stderr, "Benchmark is already resolved!\n");
999 else {
1000 if (!xccdf_benchmark_resolve(bench))
1001 fprintf(stderr, "Benchmark resolving failure (probably a dependency loop)!\n");
1002 else
1003 {
1004 if (xccdf_benchmark_export(bench, action->f_results) == 0) {
1005 ret = OSCAP_OK;
1006
1007 /* validate exported results */
1008 const char* full_validation = getenv("OSCAP_FULL_VALIDATION");
1009 if (action->validate && full_validation) {
1010 struct oscap_source *result_source = oscap_source_new_from_file(action->f_results);
1011 if (oscap_source_validate(result_source, reporter, (void *) action) != 0) {
1012 ret = OSCAP_ERROR;
1013 }
1014 else
1015 fprintf(stdout, "Resolved XCCDF has been exported correctly.\n");
1016 oscap_source_free(result_source);
1017 }
1018 }
1019 }
1020 }
1021
1022 cleanup:
1023 oscap_print_error();
1024 if (bench)
1025 xccdf_benchmark_free(bench);
1026
1027 return ret;
1028 }
1029
1030 static bool _some_result_exists(struct oscap_source *xccdf_source, const char *namespace)
1031 {
1032 struct xccdf_benchmark *benchmark = NULL;
1033 struct xccdf_policy_model *policy_model = NULL;
1034 struct oscap_file_entry_list *files = NULL;
1035 struct oscap_file_entry_iterator *files_it = NULL;
1036 char *oval_result = NULL;
1037 bool result = false;
1038
1039 benchmark = xccdf_benchmark_import_source(xccdf_source);
1040 if (benchmark == NULL)
1041 return false;
1042
1043 policy_model = xccdf_policy_model_new(benchmark);
1044 files = xccdf_policy_model_get_systems_and_files(policy_model);
1045 files_it = oscap_file_entry_list_get_files(files);
1046 oval_result = malloc(PATH_MAX * sizeof(char));
1047 while (oscap_file_entry_iterator_has_more(files_it)) {
1048 struct oscap_file_entry *file_entry = (struct oscap_file_entry *) oscap_file_entry_iterator_next(files_it);;
1049 struct stat sb;
1050 if (strcmp(oscap_file_entry_get_system(file_entry), namespace))
1051 continue;
1052 snprintf(oval_result, PATH_MAX, "./%s.result.xml", oscap_file_entry_get_file(file_entry));
1053 if (stat(oval_result, &sb) == 0) {
1054 result = true;
1055 break;
1056 }
1057 }
1058 free(oval_result);
1059 oscap_file_entry_iterator_free(files_it);
1060 oscap_file_entry_list_free(files);
1061 xccdf_policy_model_free(policy_model);
1062 return result;
1063 }
1064
1065 int app_generate_fix(const struct oscap_action *action)
1066 {
1067 struct xccdf_session *session = NULL;
1068 struct ds_rds_session *arf_session = NULL;
1069 const char *remediation_system = NULL;
1070
1071 if (action->fix_type != NULL) {
1072 if (strcmp(action->fix_type, "bash") == 0) {
1073 remediation_system = "urn:xccdf:fix:script:sh";
1074 } else if (strcmp(action->fix_type, "ansible") == 0) {
1075 remediation_system = "urn:xccdf:fix:script:ansible";
1076 } else if (strcmp(action->fix_type, "kickstart") == 0) {
1077 remediation_system = "urn:xccdf:fix:script:kickstart";
1078 } else if (strcmp(action->fix_type, "puppet") == 0) {
1079 remediation_system = "urn:xccdf:fix:script:puppet";
1080 } else if (strcmp(action->fix_type, "anaconda") == 0) {
1081 remediation_system = "urn:redhat:anaconda:pre";
1082 } else if (strcmp(action->fix_type, "ignition") == 0) {
1083 remediation_system = "urn:xccdf:fix:script:ignition";
1084 } else if (strcmp(action->fix_type, "kubernetes") == 0) {
1085 remediation_system = "urn:xccdf:fix:script:kubernetes";
1086 } else if (strcmp(action->fix_type, "blueprint") == 0) {
1087 remediation_system = "urn:redhat:osbuild:blueprint";
1088 } else if (strcmp(action->fix_type, "bootc") == 0) {
1089 remediation_system = "urn:xccdf:fix:script:bootc";
1090 } else {
1091 fprintf(stderr,
1092 "Unknown fix type '%s'.\n"
1093 "Please provide one of: bash, ansible, kickstart, puppet, anaconda, ignition, kubernetes, blueprint, bootc.\n"
1094 "Or provide a custom template using '--template' instead.\n",
1095 action->fix_type);
1096 return OSCAP_ERROR;
1097 }
1098 } else {
1099 remediation_system = "urn:xccdf:fix:script:sh";
1100 }
1101
1102 if (action->id != NULL && action->fix_type != NULL && !strcmp(action->fix_type, "kickstart")) {
1103 fprintf(stderr, "It isn't possible to generate results-oriented Kickstarts.\n");
1104 return OSCAP_ERROR;
1105 }
1106 if (action->id != NULL && action->fix_type != NULL && !strcmp(action->fix_type, "bootc")) {
1107 fprintf(stderr, "It isn't possible to generate results-oriented bootc remediations.\n");
1108 return OSCAP_ERROR;
1109 }
1110
1111 int ret = OSCAP_ERROR;
1112 struct oscap_source *source = oscap_source_new_from_file(action->f_xccdf);
1113 oscap_document_type_t document_type = oscap_source_get_scap_type(source);
1114 if (document_type == OSCAP_DOCUMENT_ARF) {
1115 arf_session = ds_rds_session_new_from_source(source);
1116 if (arf_session == NULL) {
1117 goto cleanup;
1118 }
1119 struct oscap_source *report_source = ds_rds_session_select_report(arf_session, NULL);
1120 if (report_source == NULL) {
1121 goto cleanup;
1122 }
1123 struct oscap_source *report_request_source = ds_rds_session_select_report_request(arf_session, NULL);
1124 if (report_request_source == NULL) {
1125 goto cleanup;
1126 }
1127 session = xccdf_session_new_from_source(oscap_source_clone(report_request_source));
1128 if (action->id != NULL) {
1129 if (xccdf_session_add_report_from_source(session, oscap_source_clone(report_source))) {
1130 goto cleanup;
1131 }
1132 }
1133 oscap_source_free(source);
1134 } else {
1135 session = xccdf_session_new_from_source(source);
1136 }
1137 if (session == NULL)
1138 goto cleanup;
1139
1140 xccdf_session_set_validation(session, action->validate, getenv("OSCAP_FULL_VALIDATION") != NULL);
1141 xccdf_session_set_signature_validation(session, action->validate_signature);
1142 xccdf_session_set_signature_enforcement(session, action->enforce_signature);
1143 xccdf_session_set_user_cpe(session, action->cpe);
1144 xccdf_session_configure_remote_resources(session, action->remote_resources, action->local_files, download_reporting_callback);
1145 xccdf_session_set_custom_oval_files(session, action->f_ovals);
1146 xccdf_session_set_user_tailoring_file(session, action->tailoring_file);
1147 xccdf_session_set_user_tailoring_cid(session, action->tailoring_id);
1148 if (xccdf_session_is_sds(session)) {
1149 xccdf_session_set_component_id(session, action->f_xccdf_id);
1150 xccdf_session_set_benchmark_id(session, action->f_benchmark_id);
1151 }
1152 xccdf_session_set_loading_flags(session, XCCDF_SESSION_LOAD_XCCDF);
1153 if (xccdf_session_load(session) != 0)
1154 goto cleanup;
1155
1156 #ifdef OS_WINDOWS
1157 int output_fd = _fileno(stdout);
1158 #else
1159 int output_fd = STDOUT_FILENO;
1160 #endif
1161 if (action->f_results != NULL) {
1162 if ((output_fd = open(action->f_results, O_CREAT|O_TRUNC|O_NOFOLLOW|O_WRONLY, 0700)) < 0) {
1163 fprintf(stderr, "Could not open %s: %s", action->f_results, strerror(errno));
1164 goto cleanup;
1165 }
1166 }
1167
1168 struct oscap_source *tailoring = xccdf_session_get_user_tailoring_file(session);
1169 if (action->id != NULL) {
1170 /* Result-oriented fixes */
1171 if (xccdf_session_build_policy_from_testresult(session, action->id) != 0)
1172 goto cleanup2;
1173
1174 struct xccdf_policy *policy = xccdf_session_get_xccdf_policy(session);
1175 struct xccdf_result *result = xccdf_policy_get_result_by_id(policy, xccdf_session_get_result_id(session));
1176 if (xccdf_policy_generate_fix(policy, result, remediation_system, action->f_xccdf, tailoring, output_fd, action->raw) == 0)
1177 ret = OSCAP_OK;
1178 } else { // Fallback to profile if result id is missing
1179 /* Profile-oriented fixes */
1180 if (!xccdf_session_set_profile_id(session, action->profile)) {
1181 if (action->profile != NULL) {
1182 if (xccdf_set_profile_or_report_bad_id(session, action->profile, action->f_xccdf) == OSCAP_ERROR)
1183 goto cleanup2;
1184 } else {
1185 fprintf(stderr, "No Policy was found for default profile.\n");
1186 goto cleanup2;
1187 }
1188 }
1189 struct xccdf_policy *policy = xccdf_session_get_xccdf_policy(session);
1190 if (xccdf_policy_generate_fix(policy, NULL, remediation_system, action->f_xccdf, tailoring, output_fd, action->raw) == 0)
1191 ret = OSCAP_OK;
1192 }
1193 cleanup2:
1194 #ifdef OS_WINDOWS
1195 if (output_fd != _fileno(stdout))
1196 #else
1197 if (output_fd != STDOUT_FILENO)
1198 #endif
1199 close(output_fd);
1200 cleanup:
1201 ds_rds_session_free(arf_session);
1202 xccdf_session_free(session);
1203 oscap_print_error();
1204 return ret;
1205 }
1206
1207 int app_generate_guide(const struct oscap_action *action)
1208 {
1209 int ret = OSCAP_ERROR;
1210
1211 struct oscap_source *source = oscap_source_new_from_file(action->f_xccdf);
1212 struct xccdf_session *session = xccdf_session_new_from_source(source);
1213 if (session == NULL) {
1214 goto cleanup;
1215 }
1216
1217 xccdf_session_set_validation(session, action->validate, getenv("OSCAP_FULL_VALIDATION") != NULL);
1218 xccdf_session_set_signature_validation(session, action->validate_signature);
1219 xccdf_session_set_signature_enforcement(session, action->enforce_signature);
1220 xccdf_session_configure_remote_resources(session, action->remote_resources, action->local_files, download_reporting_callback);
1221 xccdf_session_set_user_tailoring_file(session, action->tailoring_file);
1222 xccdf_session_set_user_tailoring_cid(session, action->tailoring_id);
1223 if (xccdf_session_is_sds(session)) {
1224 xccdf_session_set_component_id(session, action->f_xccdf_id);
1225 xccdf_session_set_benchmark_id(session, action->f_benchmark_id);
1226 }
1227 xccdf_session_set_loading_flags(session, XCCDF_SESSION_LOAD_XCCDF);
1228
1229 if (xccdf_session_load(session) != 0) {
1230 goto cleanup;
1231 }
1232
1233 if (!xccdf_session_set_profile_id(session, action->profile)) {
1234 if (action->profile != NULL) {
1235 if (xccdf_set_profile_or_report_bad_id(session, action->profile, action->f_xccdf) == OSCAP_ERROR)
1236 goto cleanup;
1237 } else {
1238 fprintf(stderr, "No Policy was found for default profile.\n");
1239 goto cleanup;
1240 }
1241 }
1242
1243 if (xccdf_session_generate_guide(session, action->f_results) == 0) {
1244 ret = OSCAP_OK;
1245 }
1246
1247 cleanup:
1248 xccdf_session_free(session);
1249 oscap_print_error();
1250 return ret;
1251 }
1252
1253 int app_xccdf_xslt(const struct oscap_action *action)
1254 {
1255 const char *oval_template = NULL;
1256 const char *sce_template = NULL;
1257
1258 if (action->module == &XCCDF_GEN_REPORT) {
1259 /* If generating the report and the option is missing -> use defaults */
1260 struct oscap_source *xccdf_source = oscap_source_new_from_file(action->f_xccdf);
1261 /* We want to define default template because we strive to serve user the
1262 * best. However, we must not offer a template, if there is a risk it might
1263 * be incorrect. Otherwise, libxml2 will throw a lot of misleading messages
1264 * to stderr. */
1265 if (_some_result_exists(xccdf_source, "http://oval.mitre.org/XMLSchema/oval-definitions-5")) {
1266 oval_template = "%.result.xml";
1267 }
1268 if (_some_result_exists(xccdf_source, "http://open-scap.org/page/SCE")) {
1269 sce_template = "%.result.xml";
1270 }
1271 oscap_source_free(xccdf_source);
1272 }
1273
1274 if (action->module == &XCCDF_GEN_CUSTOM) {
1275 action->module->user = (void*)action->stylesheet;
1276 }
1277
1278 const char *params[] = {
1279 "result-id", action->id,
1280 "benchmark_id", action->f_benchmark_id,
1281 "profile_id", action->profile,
1282 "oval-template", oval_template,
1283 "sce-template", sce_template,
1284 "verbosity", "",
1285 "hide-profile-info", action->hide_profile_info ? "yes" : NULL,
1286 NULL
1287 };
1288
1289 int ret = app_xslt(action->f_xccdf, action->module->user, action->f_results, params);
1290 return ret;
1291 }
1292
1293 bool getopt_generate(int argc, char **argv, struct oscap_action *action)
1294 {
1295 static const struct option long_options[] = {
1296 {"profile", 1, 0, 3},
1297 {0, 0, 0, 0}
1298 };
1299
1300 int c;
1301 while ((c = getopt_long(argc, argv, "+", long_options, NULL)) != -1) {
1302 switch (c) {
1303 case 3: action->profile = optarg; break;
1304 default: return oscap_module_usage(action->module, stderr, NULL);
1305 }
1306 }
1307 return true;
1308 }
1309
1310 enum oval_opt {
1311 XCCDF_OPT_RESULT_FILE = 1,
1312 XCCDF_OPT_RESULT_FILE_STIG,
1313 XCCDF_OPT_RESULT_FILE_ARF,
1314 XCCDF_OPT_DATASTREAM_ID,
1315 XCCDF_OPT_XCCDF_ID,
1316 XCCDF_OPT_BENCHMARK_ID,
1317 XCCDF_OPT_PROFILE,
1318 XCCDF_OPT_RULE,
1319 XCCDF_OPT_SKIP_RULE,
1320 XCCDF_OPT_REPORT_FILE,
1321 XCCDF_OPT_FORMAT,
1322 XCCDF_OPT_STYLESHEET_FILE,
1323 XCCDF_OPT_FILE_VERSION,
1324 XCCDF_OPT_TAILORING_FILE,
1325 XCCDF_OPT_TAILORING_ID,
1326 XCCDF_OPT_CPE,
1327 XCCDF_OPT_CPE_DICT,
1328 XCCDF_OPT_OUTPUT = 'o',
1329 XCCDF_OPT_RESULT_ID = 'i',
1330 XCCDF_OPT_FIX_TYPE,
1331 XCCDF_OPT_LOCAL_FILES,
1332 XCCDF_OPT_REFERENCE,
1333 XCCDF_OPT_VERBOSE,
1334 XCCDF_OPT_VERBOSE_LOG_FILE
1335 };
1336
1337 bool getopt_xccdf(int argc, char **argv, struct oscap_action *action)
1338 {
1339 assert(action != NULL);
1340
1341 action->doctype = OSCAP_DOCUMENT_XCCDF;
1342
1343 /* Command-options */
1344 const struct option long_options[] = {
1345 // options
1346 {"output", required_argument, NULL, XCCDF_OPT_OUTPUT},
1347 {"results", required_argument, NULL, XCCDF_OPT_RESULT_FILE},
1348 {"results-arf", required_argument, NULL, XCCDF_OPT_RESULT_FILE_ARF},
1349 {"stig-viewer", required_argument, NULL, XCCDF_OPT_RESULT_FILE_STIG},
1350 {"datastream-id", required_argument, NULL, XCCDF_OPT_DATASTREAM_ID},
1351 {"xccdf-id", required_argument, NULL, XCCDF_OPT_XCCDF_ID},
1352 {"benchmark-id", required_argument, NULL, XCCDF_OPT_BENCHMARK_ID},
1353 {"profile", required_argument, NULL, XCCDF_OPT_PROFILE},
1354 {"rule", required_argument, NULL, XCCDF_OPT_RULE},
1355 {"skip-rule", required_argument, NULL, XCCDF_OPT_SKIP_RULE},
1356 {"result-id", required_argument, NULL, XCCDF_OPT_RESULT_ID},
1357 {"report", required_argument, NULL, XCCDF_OPT_REPORT_FILE},
1358 {"stylesheet", required_argument, NULL, XCCDF_OPT_STYLESHEET_FILE},
1359 {"tailoring-file", required_argument, NULL, XCCDF_OPT_TAILORING_FILE},
1360 {"tailoring-id", required_argument, NULL, XCCDF_OPT_TAILORING_ID},
1361 {"cpe", required_argument, NULL, XCCDF_OPT_CPE},
1362 {"cpe-dict", required_argument, NULL, XCCDF_OPT_CPE_DICT}, // DEPRECATED!
1363 {"fix-type", required_argument, NULL, XCCDF_OPT_FIX_TYPE},
1364 {"local-files", required_argument, NULL, XCCDF_OPT_LOCAL_FILES},
1365 {"reference", required_argument, NULL, XCCDF_OPT_REFERENCE},
1366 {"verbose", required_argument, NULL, XCCDF_OPT_VERBOSE},
1367 {"verbose-log-file", required_argument, NULL, XCCDF_OPT_VERBOSE_LOG_FILE},
1368 // flags
1369 {"force", no_argument, &action->force, 1},
1370 {"oval-results", no_argument, &action->oval_results, 1},
1371 {"check-engine-results", no_argument, &action->check_engine_results, 1},
1372 {"skip-validation", no_argument, &action->validate, 0},
1373 {"skip-signature-validation", no_argument, &action->validate_signature, 0},
1374 {"enforce-signature", no_argument, &action->enforce_signature, 1},
1375 {"fetch-remote-resources", no_argument, &action->remote_resources, 1},
1376 {"progress", no_argument, &action->progress, PROGRESS_OPT_SPARSE},
1377 {"progress-full", no_argument, &action->progress, PROGRESS_OPT_FULL},
1378 {"remediate", no_argument, &action->remediate, 1},
1379 {"hide-profile-info", no_argument, &action->hide_profile_info, 1},
1380 {"export-variables", no_argument, &action->export_variables, 1},
1381 {"skip-schematron", no_argument, &action->schematron, 0},
1382 {"without-syschar", no_argument, &action->without_sys_chars, 1},
1383 {"thin-results", no_argument, &action->thin_results, 1},
1384 {"raw", no_argument, &action->raw, 1},
1385 {"show-rule-details", no_argument, &action->show_rule_details, 1},
1386 // end
1387 {0, 0, 0, 0}
1388 };
1389
1390 int c;
1391 while ((c = getopt_long(argc, argv, "o:i:", long_options, NULL)) != -1) {
1392
1393 switch (c) {
1394 case XCCDF_OPT_OUTPUT:
1395 case XCCDF_OPT_RESULT_FILE: action->f_results = optarg; break;
1396 case XCCDF_OPT_RESULT_FILE_STIG: action->f_results_stig = optarg; break;
1397 case XCCDF_OPT_RESULT_FILE_ARF: action->f_results_arf = optarg; break;
1398 case XCCDF_OPT_DATASTREAM_ID: action->f_datastream_id = optarg; break;
1399 case XCCDF_OPT_XCCDF_ID: action->f_xccdf_id = optarg; break;
1400 case XCCDF_OPT_BENCHMARK_ID: action->f_benchmark_id = optarg; break;
1401 case XCCDF_OPT_PROFILE: action->profile = optarg; break;
1402 case XCCDF_OPT_RULE:
1403 oscap_stringlist_add_string(action->rules, optarg);
1404 break;
1405 case XCCDF_OPT_SKIP_RULE:
1406 oscap_stringlist_add_string(action->skip_rules, optarg);
1407 break;
1408 case XCCDF_OPT_RESULT_ID: action->id = optarg; break;
1409 case XCCDF_OPT_REPORT_FILE: action->f_report = optarg; break;
1410 /* we use realpath to get an absolute path to given XSLT to prevent openscap from looking
1411 into /usr/share/openscap/xsl instead of CWD */
1412 case XCCDF_OPT_STYLESHEET_FILE: oscap_realpath(optarg, custom_stylesheet_path); action->stylesheet = custom_stylesheet_path; break;
1413 case XCCDF_OPT_TAILORING_FILE: action->tailoring_file = optarg; break;
1414 case XCCDF_OPT_TAILORING_ID: action->tailoring_id = optarg; break;
1415 case XCCDF_OPT_CPE: action->cpe = optarg; break;
1416 case XCCDF_OPT_CPE_DICT:
1417 {
1418 fprintf(stdout, "Warning: --cpe-dict is a deprecated option. Please use --cpe instead!\n\n");
1419 action->cpe = optarg; break;
1420 }
1421 case XCCDF_OPT_FIX_TYPE:
1422 action->fix_type = optarg;
1423 break;
1424 case XCCDF_OPT_LOCAL_FILES:
1425 action->local_files = optarg;
1426 break;
1427 case XCCDF_OPT_REFERENCE:
1428 action->reference = optarg;
1429 break;
1430 case XCCDF_OPT_VERBOSE:
1431 action->verbosity_level = optarg;
1432 break;
1433 case XCCDF_OPT_VERBOSE_LOG_FILE:
1434 action->f_verbose_log = optarg;
1435 break;
1436 case 0: break;
1437 default: return oscap_module_usage(action->module, stderr, NULL);
1438 }
1439 }
1440
1441 if (action->module == &XCCDF_EVAL) {
1442 /* We should have XCCDF file here */
1443 if (optind >= argc) {
1444 /* TODO */
1445 return oscap_module_usage(action->module, stderr, "XCCDF file need to be specified!");
1446 }
1447
1448 action->f_xccdf = argv[optind];
1449 if (argc > (optind+1)) {
1450 action->f_ovals = malloc((argc-(optind+1)+1) * sizeof(char *));
1451 int i = 1;
1452 while (argc > (optind+i)) {
1453 action->f_ovals[i-1] = argv[optind + i];
1454 i++;
1455 }
1456 action->f_ovals[i-1] = NULL;
1457 } else {
1458 action->f_ovals = NULL;
1459 }
1460 } else if (action->module == &XCCDF_GEN_CUSTOM) {
1461 if (!action->stylesheet) {
1462 return oscap_module_usage(action->module, stderr, "XSLT Stylesheet needs to be specified!");
1463 }
1464
1465 if (optind >= argc)
1466 return oscap_module_usage(action->module, stderr, "XCCDF file needs to be specified!");
1467 action->f_xccdf = argv[optind];
1468 } else {
1469 if (optind >= argc)
1470 return oscap_module_usage(action->module, stderr, "XCCDF file needs to be specified!");
1471 action->f_xccdf = argv[optind];
1472 }
1473
1474 return true;
1475 }
1476
1477 int app_xccdf_validate(const struct oscap_action *action) {
1478 int ret;
1479 int result = OSCAP_OK;
1480
1481 struct oscap_source *source = oscap_source_new_from_file(action->f_xccdf);
1482 ret = oscap_source_validate(source, reporter, (void *) action);
1483
1484 if (ret < 0) {
1485 result = OSCAP_ERROR;
1486 } else if (ret > 0) {
1487 result = OSCAP_FAIL;
1488 } else {
1489 if (action->schematron) {
1490 ret = oscap_source_validate_schematron(source);
1491 if (ret < 0) {
1492 result = OSCAP_ERROR;
1493 } else if (ret > 0) {
1494 result = OSCAP_FAIL;
1495 }
1496 }
1497 }
1498
1499 oscap_source_free(source);
1500 oscap_print_error();
1501
1502 return result;
1503 }