Debian Codegraph @

utils/oscap-cpe.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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 /* Standard header files */
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #ifdef HAVE_GETOPT_H
32 #include <getopt.h>
33 #endif
34
35 /* CPE */
36 #include <cpe_name.h>
37 #include <cpe_dict.h>
38 #include <cpe_lang.h>
39 #include <oscap_source.h>
40
41 #include "oscap-tool.h"
42
43 #define CPE_SUBMODULES_NUM 4 /* See actual CPE_SUBMODULES array
44 initialization below. */
45 static struct oscap_module* CPE_SUBMODULES[CPE_SUBMODULES_NUM];
46 bool getopt_cpe(int argc, char **argv, struct oscap_action *action);
47 int app_cpe_check(const struct oscap_action *action);
48 int app_cpe_match(const struct oscap_action *action);
49 int app_cpe_validate(const struct oscap_action *action);
50
51 struct oscap_module OSCAP_CPE_MODULE = {
52 .name = "cpe",
53 .parent = &OSCAP_ROOT_MODULE,
54 .summary = "Common Platform Enumeration",
55 .submodules = CPE_SUBMODULES
56 };
57
58 static struct oscap_module CPE_MATCH_MODULE = {
59 .name = "match",
60 .parent = &OSCAP_CPE_MODULE,
61 .summary = "Match CPE name against provided dictionary",
62 .usage = "name dictionary.xml",
63 .help = "Options:\n"
64 " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
65 " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
66 " --verbose-log-file <file> - Write verbose information into file.\n",
67 .opt_parser = getopt_cpe,
68 .func = app_cpe_match
69 };
70
71 static struct oscap_module CPE_CHECK_MODULE = {
72 .name = "check",
73 .parent = &OSCAP_CPE_MODULE,
74 .summary = "Check if CPE name is valid",
75 .usage = "name",
76 .help = "Options:\n"
77 " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
78 " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
79 " --verbose-log-file <file> - Write verbose information into file.\n",
80 .opt_parser = getopt_cpe,
81 .func = app_cpe_check
82 };
83
84 static struct oscap_module CPE_VALIDATE = {
85 .name = "validate",
86 .parent = &OSCAP_CPE_MODULE,
87 .summary = "Validate CPE Dictionary content",
88 .usage = "cpe-dict.xml",
89 .help = "Options:\n"
90 " --verbose <verbosity_level> - Turn on verbose mode at specified verbosity level.\n"
91 " Verbosity level must be one of: DEVEL, INFO, WARNING, ERROR.\n"
92 " --verbose-log-file <file> - Write verbose information into file.\n",
93 .opt_parser = getopt_cpe,
94 .func = app_cpe_validate
95 };
96
97 static struct oscap_module* CPE_SUBMODULES[CPE_SUBMODULES_NUM] = {
98 &CPE_MATCH_MODULE,
99 &CPE_CHECK_MODULE,
100 &CPE_VALIDATE,
101 NULL
102 };
103
104 bool getopt_cpe(int argc, char **argv, struct oscap_action *action) {
105 enum oscap_cpe_opts {
106 OSCAP_CPE_OPT_VERBOSE = 1,
107 OSCAP_CPE_OPT_VERBOSE_LOG_FILE,
108 };
109 const struct option long_options[] = {
110 // options
111 {"verbose", required_argument, NULL, OSCAP_CPE_OPT_VERBOSE},
112 {"verbose-log-file", required_argument, NULL, OSCAP_CPE_OPT_VERBOSE_LOG_FILE},
113 // end
114 {0, 0, 0, 0}
115 };
116 int c;
117 while ((c = getopt_long(argc, argv, "o:i:", long_options, NULL)) != -1) {
118 switch (c) {
119 case OSCAP_CPE_OPT_VERBOSE:
120 action->verbosity_level = optarg;
121 break;
122 case OSCAP_CPE_OPT_VERBOSE_LOG_FILE:
123 action->f_verbose_log = optarg;
124 break;
125 case 0: break;
126 default: return oscap_module_usage(action->module, stderr, NULL);
127 }
128 }
129 if (optind >= argc) {
130 return oscap_module_usage(action->module, stderr, "Wrong number of arguments!\n");
131 }
132 action->cpe_action = malloc(sizeof(struct cpe_action));
133 if (action->cpe_action == NULL) {
134 fprintf(stderr, "Memory allocation error.\n");
135 return false;
136 }
137 if (action->module == &CPE_MATCH_MODULE) {
138 action->cpe_action->name = argv[optind];
139 action->cpe_action->dict = argv[optind + 1];
140 }
141 if (action->module == &CPE_CHECK_MODULE) {
142 action->cpe_action->name = argv[optind];
143 }
144 if (action->module == &CPE_VALIDATE) {
145 action->cpe_action->dict = argv[optind];
146 }
147 return true;
148 }
149
150 int app_cpe_check(const struct oscap_action *action) {
151 int ret;
152
153 if (!cpe_name_check(action->cpe_action->name)) {
154 fprintf(stdout,"'%s' is NOT Valid CPE name.\n", action->cpe_action->name);
155 ret = OSCAP_FAIL;
156 }
157 else {
158 fprintf(stdout,"'%s' is Valid CPE name.\n", action->cpe_action->name);
159 ret = OSCAP_OK;
160 }
161 free(action->cpe_action);
162 return ret;
163 }
164
165 int app_cpe_match(const struct oscap_action *action) {
166
167 int ret;
168 struct cpe_name *candidate_cpe = NULL;
169 struct cpe_dict_model *dict = NULL;
170
171 struct oscap_source *source = NULL;
172
173 /* is CPE well formated? */
174 if( ! cpe_name_check(action->cpe_action->name) ) {
175 fprintf(stdout, "%s is not in valid CPE format.\n", action->cpe_action->name);
176 ret = OSCAP_ERROR;
177 goto clean;
178 }
179 candidate_cpe = cpe_name_new(action->cpe_action->name);
180
181 /* load dictionary */
182 source = oscap_source_new_from_file(action->cpe_action->dict);
183 if( (dict = cpe_dict_model_import_source(source)) == NULL ) {
184 fprintf(stdout, "can't load CPE dictionary from: %s.\n", action->cpe_action->dict);
185 ret = OSCAP_ERROR;
186 goto clean;
187 }
188
189 /* matching */
190 if( cpe_name_match_dict(candidate_cpe, dict) ) {
191 fprintf(stdout, "The exact CPE match is found.\n");
192 ret = OSCAP_OK;
193 }
194 else {
195 fprintf(stdout, "No match found.\n");
196 ret = OSCAP_FAIL;
197 }
198
199 /* clean up */
200 clean:
201 cpe_name_free(candidate_cpe);
202 cpe_dict_model_free(dict);
203 free(action->cpe_action);
204 oscap_source_free(source);
205 return ret;
206 }
207
208 int app_cpe_validate(const struct oscap_action *action) {
209
210 int ret;
211 int result;
212
213 struct oscap_source *source = oscap_source_new_from_file(action->cpe_action->dict);
214 ret = oscap_source_validate(source, reporter, (void *) action);
215 oscap_source_free(source);
216
217 if (ret==-1) {
218 result=OSCAP_ERROR;
219 goto cleanup;
220 }
221 else if (ret==1) {
222 result=OSCAP_FAIL;
223 }
224 else {
225 result=OSCAP_OK;
226 }
227
228 cleanup:
229 if (oscap_err())
230 fprintf(stderr, "%s %s\n", OSCAP_ERR_MSG, oscap_err_desc());
231
232 free(action->cpe_action);
233 return result;
234 }
235