| 1 | /* |
| 2 | * Copyright 2010--2017 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 | * Maros Barabas <[email protected]> |
| 21 | * Peter Vrabec <[email protected]> |
| 22 | */ |
| 23 | |
| 24 | #ifdef HAVE_CONFIG_H |
| 25 | #include <config.h> |
| 26 | #endif |
| 27 | |
| 28 | /* Standard header files */ |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #ifdef HAVE_GETOPT_H |
| 33 | #include <getopt.h> |
| 34 | #endif |
| 35 | #include <errno.h> |
| 36 | #include <limits.h> |
| 37 | |
| 38 | #include <oscap.h> |
| 39 | #include <oscap_error.h> |
| 40 | #include "oscap-tool.h" |
| 41 | #include "check_engine_plugin.h" |
| 42 | #include "oscap_source.h" |
| 43 | #if defined(OVAL_PROBES_ENABLED) |
| 44 | # include "probe-table.h" |
| 45 | #endif |
| 46 | |
| 47 | static bool getopt_root(int argc, char **argv, struct oscap_action *action); |
| 48 | static int print_versions(const struct oscap_action*); |
| 49 | extern struct oscap_module OSCAP_VERSION_MODULE; |
| 50 | |
| 51 | struct oscap_module* OSCAP_ROOT_SUBMODULES[] = { |
| 52 | &OSCAP_DS_MODULE, |
| 53 | &OSCAP_OVAL_MODULE, |
| 54 | &OSCAP_XCCDF_MODULE, |
| 55 | &OSCAP_CPE_MODULE, |
| 56 | &OSCAP_VERSION_MODULE, |
| 57 | &OSCAP_INFO_MODULE, |
| 58 | NULL |
| 59 | }; |
| 60 | |
| 61 | struct oscap_module OSCAP_ROOT_MODULE = { |
| 62 | .name = "oscap", |
| 63 | .usage = "[options]", |
| 64 | .usage_extra = "module operation [operation-options-and-arguments]", |
| 65 | .summary = "OpenSCAP command-line tool", |
| 66 | .help = |
| 67 | "oscap options:\n" |
| 68 | " -h --help - show this help\n" |
| 69 | " -q --quiet - quiet mode\n" |
| 70 | " -V --version - print info about supported SCAP versions", |
| 71 | .opt_parser = getopt_root, |
| 72 | .submodules = OSCAP_ROOT_SUBMODULES |
| 73 | }; |
| 74 | |
| 75 | struct oscap_module OSCAP_VERSION_MODULE = { |
| 76 | .name = "version", |
| 77 | .hidden = true, |
| 78 | .parent = &OSCAP_ROOT_MODULE, |
| 79 | .func = print_versions |
| 80 | }; |
| 81 | |
| 82 | int main(int argc, char **argv) |
| 83 | { |
| 84 | oscap_init(); |
| 85 | int ret = oscap_module_process(&OSCAP_ROOT_MODULE, argc, argv); |
| 86 | oscap_cleanup(); |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | bool getopt_root(int argc, char **argv, struct oscap_action *action) |
| 91 | { |
| 92 | while (1) { |
| 93 | static struct option long_options[] = { |
| 94 | {"quiet", 0, 0, 'q'}, |
| 95 | {"version", 0, 0, 'V'}, |
| 96 | {0, 0, 0, 0} |
| 97 | }; |
| 98 | |
| 99 | int c = getopt_long(argc, argv, "+qV", long_options, NULL); |
| 100 | if (c == -1) break; |
| 101 | |
| 102 | switch (c) { |
| 103 | case 'q': |
| 104 | printf("Warning: '-q' is obsoleted option, please use '#oscap ... >/dev/null 2>&1' instead\n"); |
| 105 | break; |
| 106 | case 'V': action->module = &OSCAP_VERSION_MODULE; break; |
| 107 | case '?': return oscap_module_usage(action->module, stderr, NULL); |
| 108 | } |
| 109 | } |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | static int print_versions(const struct oscap_action *action) |
| 115 | { |
| 116 | printf("OpenSCAP command line tool (oscap) %s\n" "Copyright 2009--2023 Red Hat Inc., Durham, North Carolina.\n\n", |
| 117 | oscap_get_version()); |
| 118 | |
| 119 | printf("==== Supported specifications ====\n"); |
| 120 | printf("SCAP Version: 1.3\n"); |
| 121 | printf("XCCDF Version: %s\n", xccdf_benchmark_supported()); |
| 122 | printf("OVAL Version: %s\n", oval_definition_model_supported()); |
| 123 | printf("CPE Version: %s\n", cpe_dict_model_supported()); |
| 124 | printf("Asset Identification Version: %s\n", "1.1"); |
| 125 | printf("Asset Reporting Format Version: %s\n", "1.1"); |
| 126 | printf("\n"); |
| 127 | |
| 128 | printf("==== Capabilities added by auto-loaded plugins ====\n"); |
| 129 | |
| 130 | const char * const *known_plugins = check_engine_plugin_get_known_plugins(); |
| 131 | bool known_plugin_found = false; |
| 132 | while (*known_plugins) { |
| 133 | // try to load the plugin but output no errors if it fails (quiet=true) |
| 134 | struct check_engine_plugin_def *plugin = check_engine_plugin_load2(*known_plugins, true); |
| 135 | if (plugin) { |
| 136 | printf("%s (from %s)\n", check_engine_plugin_get_capabilities(plugin), *known_plugins); |
| 137 | check_engine_plugin_unload(plugin); |
| 138 | known_plugin_found = true; |
| 139 | } |
| 140 | known_plugins++; |
| 141 | } |
| 142 | |
| 143 | if (!known_plugin_found) |
| 144 | printf("No plugins have been auto-loaded...\n"); |
| 145 | |
| 146 | // We do not report failure when a known plugin doesn't load properly, that's because they |
| 147 | // are optional and we don't know if it's not there or if it just failed to load. |
| 148 | oscap_clearerr(); |
| 149 | |
| 150 | printf("\n"); |
| 151 | |
| 152 | printf("==== Paths ====\n"); |
| 153 | printf("Schema files: %s\n", oscap_path_to_schemas()); |
| 154 | printf("Default CPE files: %s\n", oscap_path_to_cpe()); |
| 155 | printf("\n"); |
| 156 | |
| 157 | printf("==== Inbuilt CPE names ====\n"); |
| 158 | char default_cpe_path[PATH_MAX]; |
| 159 | snprintf(default_cpe_path, PATH_MAX, "%s/openscap-cpe-dict.xml", oscap_path_to_cpe()); |
| 160 | struct oscap_source *source = oscap_source_new_from_file(default_cpe_path); |
| 161 | struct cpe_dict_model* cpe_dict = cpe_dict_model_import_source(source); |
| 162 | oscap_source_free(source); |
| 163 | if (cpe_dict != NULL) { |
| 164 | |
| 165 | struct cpe_item_iterator* cpe_items = cpe_dict_model_get_items(cpe_dict); |
| 166 | while (cpe_item_iterator_has_more(cpe_items)) |
| 167 | { |
| 168 | struct cpe_item* cpe_item = cpe_item_iterator_next(cpe_items); |
| 169 | |
| 170 | struct oscap_text_iterator* titles = cpe_item_get_titles(cpe_item); |
| 171 | char* str_title = oscap_textlist_get_preferred_plaintext(titles, NULL); |
| 172 | oscap_text_iterator_free(titles); |
| 173 | |
| 174 | struct cpe_name* name = cpe_item_get_name(cpe_item); |
| 175 | char * str_name = cpe_name_get_as_format(name, CPE_FORMAT_URI); |
| 176 | |
| 177 | printf("%s - %s\n", str_title, str_name); |
| 178 | |
| 179 | free(str_name); |
| 180 | free(str_title); |
| 181 | } |
| 182 | cpe_item_iterator_free(cpe_items); |
| 183 | cpe_dict_model_free(cpe_dict); |
| 184 | } |
| 185 | printf("\n"); |
| 186 | #if defined(OVAL_PROBES_ENABLED) |
| 187 | printf("==== Supported OVAL objects and associated OpenSCAP probes ====\n"); |
| 188 | printf("%-14s%-28s %-28s\n", "OVAL family", "OVAL object", "OpenSCAP probe"); |
| 189 | printf("%-14s%-28s %-28s\n", "----------", "----------", "----------"); |
| 190 | probe_table_list(stdout); |
| 191 | #endif |
| 192 | |
| 193 | return OSCAP_OK; |
| 194 | } |
| 195 | |
| 196 | int reporter(const char *file, int line, const char *msg, void *arg) { |
| 197 | fprintf(stderr, "File '%s' line %d: %s", file, line, msg); |
| 198 | return 0; |
| 199 | } |
| 200 |