1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/*
* Copyright 2010 Red Hat Inc., Durham, North Carolina.
* All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors:
* Peter Vrabec <pvrabec@redhat.com>
*/
/* Standard header files */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
/* CPE */
#include <cpeuri.h>
#include <cpedict.h>
#include "oscap-tool.h"
static struct oscap_module* CPE_SUBMODULES[];
bool getopt_cpe(int argc, char **argv, struct oscap_action *action);
int app_cpe_check(const struct oscap_action *action);
int app_cpe_match(const struct oscap_action *action);
struct oscap_module OSCAP_CPE_MODULE = {
.name = "cpe",
.parent = &OSCAP_ROOT_MODULE,
.summary = "Common Platform Enumeration",
.submodules = CPE_SUBMODULES
};
static struct oscap_module CPE_MATCH_MODULE = {
.name = "match",
.parent = &OSCAP_CPE_MODULE,
.summary = "Match CPE name against provided dictionary",
.usage = "name dictionary.xml",
.help = NULL,
.opt_parser = getopt_cpe,
.func = app_cpe_match
};
static struct oscap_module CPE_CHECK_MODULE = {
.name = "check",
.parent = &OSCAP_CPE_MODULE,
.summary = "Check if CPE name is valid",
.usage = "name",
.help = NULL,
.opt_parser = getopt_cpe,
.func = app_cpe_check
};
static struct oscap_module* CPE_SUBMODULES[] = {
&CPE_MATCH_MODULE,
&CPE_CHECK_MODULE,
NULL
};
bool getopt_cpe(int argc, char **argv, struct oscap_action *action) {
if( (action->module == &CPE_MATCH_MODULE) ) {
if( argc != 5 ) {
oscap_module_usage(action->module, stderr, "Wrong number of parameteres.\n");
return false;
}
action->cpe_action = malloc(sizeof(struct cpe_action));
action->cpe_action->name=argv[3];
action->cpe_action->dict=argv[4];
}
if( (action->module == &CPE_CHECK_MODULE)) {
if( argc != 4 ) {
oscap_module_usage(action->module, stderr, "Wrong number of parameteres.\n");
return false;
}
action->cpe_action = malloc(sizeof(struct cpe_action));
action->cpe_action->name=argv[3];
}
return true;
}
int app_cpe_check(const struct oscap_action *action) {
int ret;
if (!cpe_name_check(action->cpe_action->name)) {
fprintf(stdout,"'%s' is NOT Valid CPE name.\n", action->cpe_action->name);
ret = OSCAP_FAIL;
}
else {
fprintf(stdout,"'%s' is Valid CPE name.\n", action->cpe_action->name);
ret = OSCAP_OK;
}
free(action->cpe_action);
return ret;
}
int app_cpe_match(const struct oscap_action *action) {
int ret;
struct cpe_name *candidate_cpe = NULL;
struct cpe_dict_model *dict = NULL;
/* is CPE well formated? */
if( ! cpe_name_check(action->cpe_action->name) ) {
fprintf(stdout, "%s is not in valid CPE format.\n", action->cpe_action->name);
ret = OSCAP_ERROR;
goto clean;
}
candidate_cpe = cpe_name_new(action->cpe_action->name);
/* load dictionary */
if( (dict = cpe_dict_model_import (action->cpe_action->dict)) == NULL ) {
fprintf(stdout, "can't load CPE dictionary from: %s.\n", action->cpe_action->dict);
ret = OSCAP_ERROR;
goto clean;
}
/* matching */
if( cpe_name_match_dict(candidate_cpe, dict) ) {
fprintf(stdout, "The exact CPE match is found.\n");
ret = OSCAP_OK;
}
else {
fprintf(stdout, "No match found.\n");
ret = OSCAP_FAIL;
}
/* clean up */
clean:
cpe_name_free(candidate_cpe);
cpe_dict_model_free(dict);
free(action->cpe_action);
return ret;
}
|