Blame utils/oscap-cvrf.c

Packit 517ee8
/*
Packit 517ee8
 * Copyright 2017 Red Hat Inc., Durham, North Carolina.
Packit 517ee8
 * All Rights Reserved.
Packit 517ee8
 *
Packit 517ee8
 * This library is free software; you can redistribute it and/or
Packit 517ee8
 * modify it under the terms of the GNU Lesser General Public
Packit 517ee8
 * License as published by the Free Software Foundation; either
Packit 517ee8
 * version 2.1 of the License, or (at your option) any later version.
Packit 517ee8
 *
Packit 517ee8
 * This library is distributed in the hope that it will be useful,
Packit 517ee8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 517ee8
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 517ee8
 * Lesser General Public License for more details.
Packit 517ee8
 *
Packit 517ee8
 * You should have received a copy of the GNU Lesser General Public
Packit 517ee8
 * License along with this library; if not, write to the Free Software
Packit 517ee8
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Packit 517ee8
 *
Packit 517ee8
 */
Packit 517ee8
#ifdef HAVE_CONFIG_H
Packit 517ee8
#include <config.h>
Packit 517ee8
#endif
Packit 517ee8
Packit 517ee8
/* Standard header files */
Packit 517ee8
#include <stdio.h>
Packit 517ee8
#include <stdlib.h>
Packit 517ee8
#include <string.h>
Packit 517ee8
#ifdef HAVE_GETOPT_H
Packit 517ee8
#include <getopt.h>
Packit 517ee8
#endif
Packit 517ee8
#include <assert.h>
Packit 517ee8
#include <math.h>
Packit 517ee8
Packit 517ee8
#include <oscap.h>
Packit 517ee8
#include <oscap_error.h>
Packit 517ee8
#include <cvrf.h>
Packit 517ee8
#include <oscap_source.h>
Packit 517ee8
Packit 517ee8
#include "oscap-tool.h"
Packit 517ee8
Packit 517ee8
static bool getopt_cvrf(int argc, char **argv, struct oscap_action *action);
Packit 517ee8
static int app_cvrf_evaluate(const struct oscap_action *action);
Packit 517ee8
static int app_cvrf_export(const struct oscap_action *action);
Packit 517ee8
static int app_cvrf_validate(const struct oscap_action *action);
Packit 517ee8
Packit 517ee8
static struct oscap_module* CVRF_SUBMODULES[4];
Packit 517ee8
Packit 517ee8
struct oscap_module OSCAP_CVRF_MODULE = {
Packit 517ee8
	.name = "cvrf",
Packit 517ee8
	.parent = &OSCAP_ROOT_MODULE,
Packit 517ee8
	.summary = "Common Vulnerability Reporting Framework",
Packit 517ee8
	.submodules = CVRF_SUBMODULES
Packit 517ee8
};
Packit 517ee8
Packit 517ee8
static struct oscap_module CVRF_EVALUATE_MODULE = {
Packit 517ee8
	.name = "eval",
Packit 517ee8
	.parent = &OSCAP_CVRF_MODULE,
Packit 517ee8
	.summary = "Evaluate system for vulnerabilities",
Packit 517ee8
	.usage = "[options] <cvrf file.xml>",
Packit 517ee8
	.opt_parser = getopt_cvrf,
Packit 517ee8
	.func = app_cvrf_evaluate,
Packit 517ee8
	.help = "Options:\n"
Packit 517ee8
		"   --index                       - Use index file to evaluate a directory of CVRF files.\n"
Packit 517ee8
		"   --results                     - Filename to which evaluation results will be saved.\n",
Packit 517ee8
};
Packit 517ee8
Packit 517ee8
static struct oscap_module CVRF_EXPORT_MODULE = {
Packit 517ee8
	.name = "export",
Packit 517ee8
	.parent = &OSCAP_CVRF_MODULE,
Packit 517ee8
	.summary = "Download and export CVRF file to system",
Packit 517ee8
	.usage = "[options] <cvrf file.xml>",
Packit 517ee8
	.opt_parser = getopt_cvrf,
Packit 517ee8
	.func = app_cvrf_export,
Packit 517ee8
	.help = "Options:\n"
Packit 517ee8
		"   --index                       - Use index file to export a directory of CVRF files \n"
Packit 517ee8
		"   --output                      - Filename to which exported CVRF document will be saved.\n",
Packit 517ee8
};
Packit 517ee8
Packit 517ee8
static struct oscap_module CVRF_VALIDATE_MODULE = {
Packit 517ee8
	.name = "validate",
Packit 517ee8
	.parent = &OSCAP_CVRF_MODULE,
Packit 517ee8
	.summary = "Ensure that provided file input follows CVRF format correctly",
Packit 517ee8
	.usage = "[options] <cvrf file.xml>",
Packit 517ee8
	.opt_parser = getopt_cvrf,
Packit 517ee8
	.func = app_cvrf_validate,
Packit 517ee8
	.help = "Options:\n"
Packit 517ee8
		"   --index                       - Use index file to validate a directory of CVRF files \n",
Packit 517ee8
};
Packit 517ee8
Packit 517ee8
static struct oscap_module* CVRF_SUBMODULES[] = {
Packit 517ee8
	&CVRF_EVALUATE_MODULE,
Packit 517ee8
	&CVRF_EXPORT_MODULE,
Packit 517ee8
	&CVRF_VALIDATE_MODULE,
Packit 517ee8
	NULL
Packit 517ee8
};
Packit 517ee8
Packit 517ee8
static int app_cvrf_evaluate(const struct oscap_action *action) {
Packit 517ee8
	int result = OSCAP_OK;
Packit 517ee8
	// Temporary hardcoded CPE until CPE name can be found without input by CVRF functions
Packit 517ee8
	// themselves
Packit 517ee8
	const char *os_name = "Red Hat Enterprise Linux Desktop Supplementary (v. 6)";
Packit 517ee8
	struct oscap_source *import_source = oscap_source_new_from_file(action->cvrf_action->f_cvrf);
Packit 517ee8
	if (import_source == NULL)
Packit 517ee8
		return OSCAP_ERROR;
Packit 517ee8
Packit 517ee8
	int ret = oscap_source_validate(import_source, reporter, (void *) action);
Packit 517ee8
	if (ret != 0) {
Packit 517ee8
		result = OSCAP_ERROR;
Packit 517ee8
		goto cleanup;
Packit 517ee8
	}
Packit 517ee8
Packit 517ee8
	struct oscap_source *export_source = cvrf_model_get_results_source(import_source, os_name);
Packit 517ee8
	if (export_source == NULL) {
Packit 517ee8
		result = OSCAP_ERROR;
Packit 517ee8
		goto cleanup;
Packit 517ee8
	}
Packit 517ee8
Packit 517ee8
	if (oscap_source_save_as(export_source, action->cvrf_action->f_results) == -1) {
Packit 517ee8
		result = OSCAP_ERROR;
Packit 517ee8
	}
Packit 517ee8
	oscap_source_free(export_source);
Packit 517ee8
Packit 517ee8
cleanup:
Packit 517ee8
	if (oscap_err())
Packit 517ee8
		fprintf(stderr, "%s %s\n", OSCAP_ERR_MSG, oscap_err_desc());
Packit 517ee8
	oscap_source_free(import_source);
Packit 517ee8
	free(action->cvrf_action);
Packit 517ee8
	return result;
Packit 517ee8
}
Packit 517ee8
Packit 517ee8
static int app_cvrf_export(const struct oscap_action *action) {
Packit 517ee8
	struct oscap_source *import_source = oscap_source_new_from_file(action->cvrf_action->f_cvrf);
Packit 517ee8
	if (import_source == NULL)
Packit 517ee8
		return OSCAP_ERROR;
Packit 517ee8
Packit 517ee8
	int result = OSCAP_OK;
Packit 517ee8
	if (action->cvrf_action->index == 1) {
Packit 517ee8
		struct cvrf_index *index = cvrf_index_import(import_source);
Packit 517ee8
		if (index == NULL) {
Packit 517ee8
			result = OSCAP_ERROR;
Packit 517ee8
			goto cleanup;
Packit 517ee8
		}
Packit 517ee8
		struct oscap_source *export_source = cvrf_index_get_export_source(index);
Packit 517ee8
		if (oscap_source_save_as(export_source, action->cvrf_action->f_output) != 0)
Packit 517ee8
			result = OSCAP_ERROR;
Packit 517ee8
		oscap_source_free(export_source);
Packit 517ee8
		cvrf_index_free(index);
Packit 517ee8
	} else {
Packit 517ee8
		struct cvrf_model *model = cvrf_model_import(import_source);
Packit 517ee8
		if(model == NULL) {
Packit 517ee8
			result = OSCAP_ERROR;
Packit 517ee8
			goto cleanup;
Packit 517ee8
		}
Packit 517ee8
		struct oscap_source *export_source = cvrf_model_get_export_source(model);
Packit 517ee8
		if (oscap_source_save_as(export_source, action->cvrf_action->f_output) != 0)
Packit 517ee8
			result = OSCAP_ERROR;
Packit 517ee8
		oscap_source_free(export_source);
Packit 517ee8
		cvrf_model_free(model);
Packit 517ee8
	}
Packit 517ee8
Packit 517ee8
	cleanup:
Packit 517ee8
		if (oscap_err())
Packit 517ee8
			fprintf(stderr, "%s %s\n", OSCAP_ERR_MSG, oscap_err_desc());
Packit 517ee8
Packit 517ee8
	oscap_source_free(import_source);
Packit 517ee8
	free(action->cvrf_action);
Packit 517ee8
	return result;
Packit 517ee8
}
Packit 517ee8
Packit 517ee8
static int app_cvrf_validate(const struct oscap_action *action) {
Packit 517ee8
	int result;
Packit 517ee8
	struct oscap_source *source = oscap_source_new_from_file(action->cvrf_action->f_cvrf);
Packit 517ee8
	int ret = oscap_source_validate(source, reporter, (void *) action);
Packit 517ee8
Packit 517ee8
	if (ret==-1) {
Packit 517ee8
		result=OSCAP_ERROR;
Packit 517ee8
	} else if (ret==1) {
Packit 517ee8
		result=OSCAP_FAIL;
Packit 517ee8
	} else {
Packit 517ee8
		result=OSCAP_OK;
Packit 517ee8
	}
Packit 517ee8
Packit 517ee8
	oscap_source_free(source);
Packit 517ee8
	oscap_print_error();
Packit 517ee8
	return result;
Packit 517ee8
}
Packit 517ee8
Packit 517ee8
// There will likely be more options needed in the future; this is
Packit 517ee8
// just a basic set up for the command line usage
Packit 517ee8
enum cvrf_opt {
Packit 517ee8
	CVRF_OPT_INDEX,
Packit 517ee8
	CVRF_OPT_RESULT_FILE,
Packit 517ee8
	CVRF_OPT_OUTPUT_FILE,
Packit 517ee8
};
Packit 517ee8
Packit 517ee8
bool getopt_cvrf(int argc, char **argv, struct oscap_action *action) {
Packit 517ee8
	action->doctype = OSCAP_DOCUMENT_CVRF_FEED;
Packit 517ee8
	action->cvrf_action = malloc(sizeof(struct cvrf_action));
Packit 517ee8
	struct cvrf_action *cvrf_action = action->cvrf_action;
Packit 517ee8
Packit 517ee8
	static const struct option long_options[] = {
Packit 517ee8
		{"index", 0, NULL, CVRF_OPT_INDEX},
Packit 517ee8
		{"results", 1, NULL, CVRF_OPT_RESULT_FILE},
Packit 517ee8
		{"output", 1, NULL, CVRF_OPT_OUTPUT_FILE},
Packit 517ee8
		{0, 0, 0, 0}
Packit 517ee8
	};
Packit 517ee8
Packit 517ee8
	int c;
Packit 517ee8
	while ((c = getopt_long(argc, argv, "+", long_options, NULL)) != -1) {
Packit 517ee8
		switch (c) {
Packit 517ee8
			case CVRF_OPT_INDEX:
Packit 517ee8
				cvrf_action->index = 1;
Packit 517ee8
				break;
Packit 517ee8
			case CVRF_OPT_RESULT_FILE:
Packit 517ee8
				cvrf_action->f_results = optarg;
Packit 517ee8
				break;
Packit 517ee8
			case CVRF_OPT_OUTPUT_FILE:
Packit 517ee8
				cvrf_action->f_output = optarg;
Packit 517ee8
				break;
Packit 517ee8
			default:
Packit 517ee8
				return oscap_module_usage(action->module, stderr, NULL);
Packit 517ee8
		}
Packit 517ee8
	}
Packit 517ee8
Packit 517ee8
	if (optind >= argc)
Packit 517ee8
		return oscap_module_usage(action->module, stderr, "CVRF file needs to be specified!\n");
Packit 517ee8
	cvrf_action->f_cvrf = argv[optind];
Packit 517ee8
	return true;
Packit 517ee8
}