| 1 | /* |
| 2 | * Copyright 2010 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 | * Lukas Kuklinek <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include "oscap-tool.h" |
| 28 | #include <assert.h> |
| 29 | #ifdef HAVE_GETOPT_H |
| 30 | #include <getopt.h> |
| 31 | #endif |
| 32 | #include <string.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
| 35 | #ifdef OS_WINDOWS |
| 36 | #include <io.h> |
| 37 | #else |
| 38 | #include <unistd.h> |
| 39 | #endif |
| 40 | #include <stdarg.h> |
| 41 | #include <errno.h> |
| 42 | #include <limits.h> |
| 43 | #include <oscap_debug.h> |
| 44 | #include "oscap_helpers.h" |
| 45 | |
| 46 | #ifndef PATH_MAX |
| 47 | # define PATH_MAX 1024 |
| 48 | #else |
| 49 | # if PATH_MAX < 1 || PATH_MAX > 4096 |
| 50 | # undef PATH_MAX |
| 51 | # define PATH_MAX 2048 |
| 52 | # endif |
| 53 | #endif /* PATH_MAX */ |
| 54 | |
| 55 | static void oscap_action_init(struct oscap_action *action) |
| 56 | { |
| 57 | assert(action != NULL); |
| 58 | memset(action, 0, sizeof(*action)); |
| 59 | action->validate = 1; |
| 60 | action->schematron = 1; |
| 61 | action->validate_signature = 1; |
| 62 | action->rules = oscap_stringlist_new(); |
| 63 | action->skip_rules = oscap_stringlist_new(); |
| 64 | } |
| 65 | |
| 66 | static void oscap_action_release(struct oscap_action *action) |
| 67 | { |
| 68 | assert(action != NULL); |
| 69 | free(action->f_ovals); |
| 70 | oscap_stringlist_free(action->rules); |
| 71 | oscap_stringlist_free(action->skip_rules); |
| 72 | } |
| 73 | |
| 74 | static size_t paramlist_size(const char **p) { size_t s = 0; if (!p) return s; while (p[s]) s += 2; return s; } |
| 75 | |
| 76 | static size_t paramlist_cpy(const char **to, const char **p) { |
| 77 | size_t s = 0; |
| 78 | if (!to || !p) |
| 79 | return s; |
| 80 | for (;p && p[s]; s += 2) to[s] = p[s], to[s+1] = p[s+1]; |
| 81 | to[s] = p[s]; |
| 82 | return s; |
| 83 | } |
| 84 | |
| 85 | int app_xslt(const char *infile, const char *xsltfile, const char *outfile, const char **params) |
| 86 | { |
| 87 | char pwd[PATH_MAX]; |
| 88 | |
| 89 | if (getcwd(pwd, sizeof(pwd)) == NULL) { |
| 90 | fprintf(stderr, "ERROR: %s\n", strerror(errno)); |
| 91 | return OSCAP_ERROR; |
| 92 | } |
| 93 | |
| 94 | /* add params oscap-version & pwd */ |
| 95 | const char *stdparams[] = { "oscap-version", oscap_get_version(), "pwd", pwd, NULL }; |
| 96 | const size_t par_size = paramlist_size(params) + paramlist_size(stdparams) + 1; |
| 97 | const char **par = malloc(par_size * sizeof(const char *)); |
| 98 | size_t s = paramlist_cpy(par , params); |
| 99 | paramlist_cpy(par + s, stdparams); |
| 100 | |
| 101 | if (oscap_apply_xslt(infile, xsltfile, outfile, par)==-1) { |
| 102 | fprintf(stderr, "%s: %s\n", OSCAP_ERR_MSG, oscap_err_desc()); |
| 103 | free(par); |
| 104 | return OSCAP_ERROR; |
| 105 | } |
| 106 | |
| 107 | free(par); |
| 108 | return OSCAP_OK; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | static void check_module(struct oscap_module *module) |
| 113 | { |
| 114 | assert(module != NULL); |
| 115 | assert(module->name != NULL); |
| 116 | if (module != &OSCAP_ROOT_MODULE) |
| 117 | assert(module->parent != NULL); |
| 118 | if (module->submodules) |
| 119 | for (struct oscap_module** sub = module->submodules; *sub != NULL; ++(sub)) |
| 120 | assert((*sub)->parent == module); |
| 121 | } |
| 122 | |
| 123 | static void oscap_module_backtrace(struct oscap_module *module, FILE *out, const char *delim) |
| 124 | { |
| 125 | assert(module != NULL); |
| 126 | if (module->parent) { |
| 127 | oscap_module_backtrace(module->parent, out, delim); |
| 128 | fprintf(out, "%s", delim); |
| 129 | } |
| 130 | fprintf(out, "%s", module->name); |
| 131 | } |
| 132 | |
| 133 | static void oscap_module_print_usage(struct oscap_module *module, FILE *out, bool full) |
| 134 | { |
| 135 | assert(module != NULL); |
| 136 | if (module->parent != NULL) { |
| 137 | oscap_module_print_usage(module->parent, out, false); |
| 138 | fprintf(out, " "); |
| 139 | } |
| 140 | fprintf(out, "%s", module->name); |
| 141 | if (module->usage) fprintf(out, " %s", module->usage); |
| 142 | if (full) { |
| 143 | if (module->usage_extra) fprintf(out, " %s", module->usage_extra); |
| 144 | else if (module->submodules) fprintf(out, " command"); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static inline void oscap_module_print_summary(struct oscap_module *module, FILE *out) |
| 149 | { |
| 150 | assert(module != NULL); |
| 151 | if (!module->hidden) { |
| 152 | fprintf(out, " %s", module->name); |
| 153 | if (module->summary) fprintf(out, " - %s", module->summary); |
| 154 | fprintf(out, "\n"); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | static int oscap_print_submodules(struct oscap_module *module, FILE *out, const char *delim, bool all) |
| 159 | { |
| 160 | if (module->submodules) { |
| 161 | for (struct oscap_module** sub = module->submodules; *sub != NULL; ++(sub)) |
| 162 | if (all || !(*sub)->hidden) |
| 163 | fprintf(out, "%s%s", (*sub)->name, delim); |
| 164 | return OSCAP_OK; |
| 165 | } |
| 166 | return OSCAP_BADMODULE; |
| 167 | } |
| 168 | |
| 169 | bool oscap_module_usage(struct oscap_module *module, FILE *out, const char *err, ...) |
| 170 | { |
| 171 | assert(module != NULL); |
| 172 | |
| 173 | if (err) { |
| 174 | va_list ap; |
| 175 | fprintf(out, "ERROR: "); |
| 176 | va_start(ap, err); |
| 177 | vfprintf(out, err, ap); |
| 178 | va_end(ap); |
| 179 | fprintf(out, "\n"); |
| 180 | } |
| 181 | fprintf(out, "Usage: "); |
| 182 | oscap_module_print_usage(module, out, true); |
| 183 | if (module->submodules) { fprintf(out, "\nCommands: "); oscap_print_submodules(module, out, " ", false); } |
| 184 | fprintf(out, "\nHelp: "); |
| 185 | oscap_module_backtrace(module, out, " "); |
| 186 | fprintf(out, " -h\n"); |
| 187 | return (out != stderr); |
| 188 | } |
| 189 | |
| 190 | static void oscap_module_print_help(struct oscap_module *module, FILE *out) |
| 191 | { |
| 192 | assert(module != NULL); |
| 193 | assert(out != NULL); |
| 194 | |
| 195 | oscap_module_backtrace(module, out, " -> "); |
| 196 | fprintf(out, "\n\n"); |
| 197 | |
| 198 | if (module->summary) fprintf(out, "%s\n\n", module->summary); |
| 199 | |
| 200 | fprintf(out, "Usage: "); |
| 201 | oscap_module_print_usage(module, out, true); |
| 202 | fprintf(out, "\n\n"); |
| 203 | |
| 204 | if (module->help) |
| 205 | fprintf(out, "%s\n\n", module->help); |
| 206 | |
| 207 | if (module->submodules) { |
| 208 | fprintf(out, "Commands:\n"); |
| 209 | for (struct oscap_module** sub = module->submodules; *sub != NULL; ++(sub)) |
| 210 | oscap_module_print_summary(*sub, out); |
| 211 | fprintf(out, "\n"); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | static void oscap_module_tree(struct oscap_module *module, FILE* out, int indent) |
| 216 | { |
| 217 | assert(module); |
| 218 | if (module->hidden) return; |
| 219 | for (int i = 0; i < indent; ++i) fprintf(out, " "); |
| 220 | fprintf(out, "%s - %s\n", module->name, module->summary); |
| 221 | if (module->submodules) { |
| 222 | for (struct oscap_module** sub = module->submodules; *sub != NULL; ++(sub)) |
| 223 | oscap_module_tree(*sub, out, indent + 1); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | static struct oscap_module *oscap_module_find(struct oscap_module **list, const char *name) |
| 228 | { |
| 229 | if (name == NULL) return NULL; |
| 230 | if (list == NULL) return NULL; |
| 231 | while (*list != NULL && strcmp((*list)->name, name) != 0) ++list; |
| 232 | return *list; |
| 233 | } |
| 234 | |
| 235 | enum oscap_common_opts { |
| 236 | OPT_NONE, |
| 237 | OPT_LISTMODS, |
| 238 | OPT_LISTALLMODS, |
| 239 | OPT_MODTREE, |
| 240 | OPT_HELP = 'h', |
| 241 | OPT_VERBOSE, |
| 242 | OPT_VERBOSE_LOG_FILE |
| 243 | }; |
| 244 | |
| 245 | static enum oscap_common_opts oscap_parse_common_opts(int argc, char **argv, struct oscap_action *action) |
| 246 | { |
| 247 | static const struct option opts[] = { |
| 248 | { "help", 0, 0, OPT_HELP }, |
| 249 | { "list-submodules", 0, 0, OPT_LISTMODS }, |
| 250 | { "list-all-submodules", 0, 0, OPT_LISTALLMODS }, |
| 251 | { "module-tree", 0, 0, OPT_MODTREE }, |
| 252 | { 0, 0, 0, 0 } |
| 253 | }; |
| 254 | |
| 255 | int r; |
| 256 | opterr = 0; |
| 257 | while ((r = getopt_long(argc, argv, "+h", opts, NULL)) != -1) { |
| 258 | switch (r) { |
| 259 | case 0: |
| 260 | break; |
| 261 | case '?': |
| 262 | optind--; |
| 263 | opterr--; |
| 264 | return OPT_NONE; |
| 265 | default: |
| 266 | return r; |
| 267 | } |
| 268 | } |
| 269 | return OPT_NONE; |
| 270 | } |
| 271 | |
| 272 | int oscap_module_call(struct oscap_action *action) |
| 273 | { |
| 274 | assert(action != NULL); |
| 275 | assert(action->module != NULL); |
| 276 | |
| 277 | if (action->module->func) { |
| 278 | return action->module->func(action); |
| 279 | } |
| 280 | return OSCAP_UNIMPL_MOD; |
| 281 | } |
| 282 | |
| 283 | static void getopt_parse_env(struct oscap_module *module, int *argc, char ***argv) |
| 284 | { |
| 285 | int ofs, nargc, eargc; |
| 286 | char *opt, *opts, *state, **nargv, **eargv; |
| 287 | const char *delim = ","; |
| 288 | |
| 289 | if (!module->parent |
| 290 | || strcmp(module->parent->name, "oval")) |
| 291 | return; |
| 292 | |
| 293 | opts = getenv("OSCAP_OVAL_COMMAND_OPTIONS"); |
| 294 | if (opts == NULL) |
| 295 | return; |
| 296 | |
| 297 | /* parse the args supplied from the env var */ |
| 298 | eargv = NULL; |
| 299 | eargc = 0; |
| 300 | opts = strdup(opts); |
| 301 | opt = oscap_strtok_r(opts, delim, &state); |
| 302 | while (opt != NULL) { |
| 303 | eargc++; |
| 304 | void *new_eargv = realloc(eargv, eargc * sizeof(char *)); |
| 305 | if (new_eargv == NULL) |
| 306 | goto exit; |
| 307 | eargv = new_eargv; |
| 308 | eargv[eargc - 1] = strdup(opt); |
| 309 | opt = oscap_strtok_r(NULL, delim, &state); |
| 310 | } |
| 311 | |
| 312 | nargc = *argc + eargc; |
| 313 | nargv = malloc((nargc + 1) * sizeof(char *)); |
| 314 | /* copy main args up to the command name */ |
| 315 | for (ofs = 0; strcmp((*argv)[ofs], OSCAP_OVAL_MODULE.name); ofs++); |
| 316 | ofs += 2; |
| 317 | memcpy(nargv, *argv, ofs * sizeof(char *)); |
| 318 | /* copy env args */ |
| 319 | memcpy(nargv + ofs, eargv, eargc * sizeof(char *)); |
| 320 | /* copy rest of the main args */ |
| 321 | memcpy(nargv + ofs + eargc, *argv + ofs, (*argc - ofs) * sizeof(char *)); |
| 322 | nargv[nargc] = NULL; |
| 323 | |
| 324 | *argc = nargc; |
| 325 | *argv = nargv; |
| 326 | exit: |
| 327 | free(opts); |
| 328 | free(eargv); |
| 329 | } |
| 330 | |
| 331 | int oscap_module_process(struct oscap_module *module, int argc, char **argv) |
| 332 | { |
| 333 | assert(module != NULL); |
| 334 | assert(argv != NULL); |
| 335 | |
| 336 | int ret = OSCAP_OK; |
| 337 | struct oscap_action action; |
| 338 | oscap_action_init(&action); |
| 339 | optind = 0; |
| 340 | |
| 341 | while (module) { |
| 342 | check_module(module); |
| 343 | |
| 344 | ++optind; // skip current module key index |
| 345 | action.module = module; // update current module |
| 346 | |
| 347 | getopt_parse_env(module, &argc, &argv); |
| 348 | |
| 349 | switch (oscap_parse_common_opts(argc, argv, &action)) { |
| 350 | case OPT_HELP: oscap_module_print_help(module, stdout); goto cleanup; |
| 351 | case OPT_LISTMODS: oscap_print_submodules(module, stdout, "\n", false); goto cleanup; |
| 352 | case OPT_LISTALLMODS: oscap_print_submodules(module, stdout, "\n", true); goto cleanup; |
| 353 | case OPT_MODTREE: oscap_module_tree(module, stdout, 0); goto cleanup; |
| 354 | default: break; |
| 355 | } |
| 356 | |
| 357 | if (module->opt_parser) { |
| 358 | if (!module->opt_parser(argc, argv, &action)) { |
| 359 | ret = OSCAP_BADARGS; |
| 360 | goto cleanup; |
| 361 | } |
| 362 | module = action.module; // module might have changed |
| 363 | } |
| 364 | |
| 365 | if (module->func) { |
| 366 | if (!check_verbose_options(&action)) { |
| 367 | goto cleanup; |
| 368 | } |
| 369 | if (!oscap_set_verbose(action.verbosity_level, action.f_verbose_log)) { |
| 370 | goto cleanup; |
| 371 | } |
| 372 | oscap_print_env_vars(); |
| 373 | ret = oscap_module_call(&action); |
| 374 | goto cleanup; |
| 375 | } |
| 376 | else if (module->submodules) { |
| 377 | struct oscap_module *old_mod = module; |
| 378 | module = oscap_module_find(module->submodules, argv[optind]); |
| 379 | if (module == NULL) { |
| 380 | if (argv[optind] != NULL) |
| 381 | fprintf(stderr, "No such module: %s\n", argv[optind]); |
| 382 | else oscap_module_usage(old_mod, stderr, NULL); |
| 383 | ret = OSCAP_BADMODULE; |
| 384 | } |
| 385 | } |
| 386 | else { |
| 387 | fprintf(stderr, "Module %s does not do anything\n", module->name); |
| 388 | ret = OSCAP_UNIMPL_MOD; |
| 389 | goto cleanup; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | cleanup: |
| 394 | oscap_action_release(&action); |
| 395 | return ret; |
| 396 | } |
| 397 | |
| 398 | void oscap_print_error(void) |
| 399 | { |
| 400 | if (oscap_err()) { |
| 401 | char *err = oscap_err_get_full_error(); |
| 402 | fprintf(stderr, "%s %s\n", OSCAP_ERR_MSG, err); |
| 403 | free(err); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | bool check_verbose_options(struct oscap_action *action) |
| 408 | { |
| 409 | if (action->verbosity_level == NULL && action->f_verbose_log != NULL) { |
| 410 | oscap_module_usage(action->module, stderr, |
| 411 | "Verbosity level is not specified! Please provide --verbose VERBOSITY_LEVEL option together with --verbose-log-file."); |
| 412 | return false; |
| 413 | } |
| 414 | if (action->verbosity_level != NULL && oscap_verbosity_level_from_cstr(action->verbosity_level) == -1) { |
| 415 | oscap_module_usage(action->module, stderr, |
| 416 | "Invalid verbosity level! Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR."); |
| 417 | return false; |
| 418 | } |
| 419 | return true; |
| 420 | } |
| 421 | |
| 422 | void download_reporting_callback(bool warning, const char *format, ...) |
| 423 | { |
| 424 | FILE *dest = stderr; |
| 425 | va_list argptr; |
| 426 | va_start(argptr, format); |
| 427 | vfprintf(dest, format, argptr); |
| 428 | va_end(argptr); |
| 429 | fflush(dest); |
| 430 | } |