Debian Codegraph @

src/OVAL/oval_directives.c

raw symbols SCIP
1 /**
2 * @file oval_directives.c
3 * \brief Open Vulnerability and Assessment Language
4 *
5 * See more details at http://oval.mitre.org/
6 * @cond
7 */
8
9 /*
10 * Copyright 2011--2014 Red Hat Inc., Durham, North Carolina.
11 * All Rights Reserved.
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 *
27 * Authors:
28 * "Peter Vrabec" <[email protected]>
29 */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <stdbool.h>
38 #include <string.h>
39
40 #include "oval_directives_impl.h"
41 #include "adt/oval_collection_impl.h"
42 #include "oval_parser_impl.h"
43
44 #include "common/util.h"
45 #include "common/debug_priv.h"
46 #include "common/_error.h"
47 #include "common/elements.h"
48 #include "oscap_source.h"
49 #include "source/oscap_source_priv.h"
50
51 #define NUMBER_OF_RESULTS 6
52 #define NUMBER_OF_CLASSES 5
53
54
55 struct oval_result_directives {
56 struct {
57 bool reported;
58 oval_result_directive_content_t content;
59 } directive[NUMBER_OF_RESULTS];
60 bool inc_definitions;
61 };
62
63 struct oval_directives_model {
64 struct oval_generator *generator;
65 struct oval_result_directives * def_directives;
66 struct oval_result_directives * class_directives[NUMBER_OF_CLASSES];
67 };
68
69
70 static int oval_directives_model_parse(xmlTextReaderPtr, struct oval_parser_context *);
71
72 struct oval_directives_model *oval_directives_model_new(void) {
73
74 struct oval_directives_model * new_model = (struct oval_directives_model *)
75 malloc(sizeof(struct oval_directives_model));
76
77 new_model->generator = oval_generator_new();
78 new_model->def_directives = oval_result_directives_new();
79
80 /* no class directives */
81 for(int i=0; i<NUMBER_OF_CLASSES; i++)
82 new_model->class_directives[i] = NULL;
83
84 return new_model;
85 }
86
87 void oval_directives_model_free(struct oval_directives_model *model) {
88 __attribute__nonnull__(model);
89
90 for(int i=0; i<NUMBER_OF_CLASSES; i++) {
91 if (model->class_directives[i]) {
92 free(model->class_directives[i]);
93 model->class_directives[i]=NULL;
94 }
95 }
96
97 free(model->def_directives);
98 model->def_directives=NULL;
99
100 oval_generator_free(model->generator);
101 model->generator=NULL;
102
103 free(model);
104 }
105
106 int oval_directives_model_import_source(struct oval_directives_model *model, struct oscap_source *source)
107 {
108 int ret = 0;
109 char *tagname = NULL;
110 char *namespace = NULL;
111
112 /* setup context */
113 struct oval_parser_context context;
114 context.reader = oscap_source_get_xmlTextReader(source);
115 if (context.reader == NULL) {
116 return -1;
117 }
118 context.directives_model = model;
119 context.user_data = NULL;
120 /* jump into oval_system_characteristics */
121 xmlTextReaderRead(context.reader);
122
123 /* make sure this is a right schema and tag */
124 tagname = (char *)xmlTextReaderLocalName(context.reader);
125 namespace = (char *)xmlTextReaderNamespaceUri(context.reader);
126 int is_ovaldir = 0;
127 if (namespace != NULL) {
128 is_ovaldir = strcmp((const char *)OVAL_DIRECTIVES_NAMESPACE, namespace) == 0;
129 }
130 /* start parsing */
131 if (is_ovaldir && (strcmp(tagname, OVAL_ROOT_ELM_DIRECTIVES) == 0)) {
132 ret = oval_directives_model_parse(context.reader, &context);
133 } else {
134 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Missing \"oval_directives\" element");
135 ret = -1;
136 }
137
138 free(tagname);
139 free(namespace);
140 xmlFreeTextReader(context.reader);
141 return ret;
142 }
143
144 int oval_directives_model_export(struct oval_directives_model *model, const char *file) {
145 __attribute__nonnull__(model);
146
147 xmlDocPtr doc;
148
149 doc = xmlNewDoc(BAD_CAST "1.0");
150 if (doc == NULL) {
151 oscap_setxmlerr(xmlGetLastError());
152 return -1;
153 }
154
155 oval_directives_model_to_dom(model, doc, NULL);
156 return oscap_xml_save_filename_free(file, doc);
157 }
158
159
160
161 struct oval_generator *oval_directives_model_get_generator(struct oval_directives_model *model)
162 {
163 return model->generator;
164 }
165
166 struct oval_result_directives *oval_directives_model_get_defdirs(struct oval_directives_model *model)
167 {
168 return model->def_directives;
169 }
170
171 struct oval_result_directives *oval_directives_model_get_classdir(struct oval_directives_model *model, oval_definition_class_t classdir)
172 {
173 /* enum -> index */
174 int classind = --classdir;
175
176 if (classind < NUMBER_OF_CLASSES)
177 return model->class_directives[classind];
178
179 return NULL;
180 }
181
182 struct oval_result_directives *oval_directives_model_get_new_classdir(struct oval_directives_model *model, oval_definition_class_t classdir)
183 {
184 int classind = --classdir;
185
186 if (classind < NUMBER_OF_CLASSES) {
187 if (model->class_directives[classind] == NULL)
188 model->class_directives[classind] = oval_result_directives_new();
189
190 return model->class_directives[classind];
191 }
192
193 return NULL;
194 }
195
196
197 struct oval_result_directives *oval_result_directives_new(void)
198 {
199 struct oval_result_directives *directives = (struct oval_result_directives *)
200 malloc(sizeof(struct oval_result_directives));
201 if (directives == NULL)
202 return NULL;
203
204 /* full report by default */
205 oval_result_directives_set_reported(directives,
206 OVAL_RESULT_TRUE | OVAL_RESULT_FALSE |
207 OVAL_RESULT_UNKNOWN | OVAL_RESULT_NOT_EVALUATED |
208 OVAL_RESULT_ERROR | OVAL_RESULT_NOT_APPLICABLE,
209 true);
210
211 oval_result_directives_set_content(directives,
212 OVAL_RESULT_TRUE | OVAL_RESULT_FALSE |
213 OVAL_RESULT_UNKNOWN | OVAL_RESULT_NOT_EVALUATED |
214 OVAL_RESULT_NOT_APPLICABLE | OVAL_RESULT_ERROR,
215 OVAL_DIRECTIVE_CONTENT_FULL);
216 /* default value */
217 directives->inc_definitions=true;
218
219 return directives;
220 }
221
222 void oval_result_directives_free(struct oval_result_directives *directives)
223 {
224 free(directives);
225 }
226
227 bool oval_result_directives_get_reported(struct oval_result_directives *directives, oval_result_t type) {
228 __attribute__nonnull__(directives);
229
230 int i=-1;
231 while (type) {
232 i = i + 1;
233 type = type >> 1;
234 }
235
236 if (i < 0 || i >= NUMBER_OF_RESULTS) {
237 return false;
238 }
239
240 return directives->directive[i].reported;
241 }
242
243 oval_result_directive_content_t oval_result_directives_get_content
244 (struct oval_result_directives * directives, oval_result_t type) {
245 __attribute__nonnull__(directives);
246
247 int i=-1;
248 while (type) {
249 i = i + 1;
250 type = type >> 1;
251 }
252
253 if (i < 0 || i >= NUMBER_OF_RESULTS) {
254 return OVAL_DIRECTIVE_CONTENT_UNKNOWN;
255 }
256
257 return directives->directive[i].content;
258 }
259
260 bool oval_result_directives_get_included(struct oval_result_directives *directives) {
261 __attribute__nonnull__(directives);
262
263 return directives->inc_definitions;
264 }
265
266 void oval_result_directives_set_included(struct oval_result_directives *directives, bool val) {
267 __attribute__nonnull__(directives);
268
269 directives->inc_definitions = val;
270 }
271
272 void oval_result_directives_set_reported(struct oval_result_directives *directives, int flag, bool val) {
273 __attribute__nonnull__(directives);
274
275 int i=0;
276 while (flag>>i) {
277 if( flag & (1 << i) )
278 directives->directive[i].reported = val;
279 i = i + 1;
280 }
281 }
282
283 void oval_result_directives_set_content(struct oval_result_directives *directives, int flag, oval_result_directive_content_t content)
284 {
285 __attribute__nonnull__(directives);
286 int i=0;
287 while (flag>>i) {
288 if( flag & (1 << i) )
289 directives->directive[i].content = content;
290 i = i + 1;
291 }
292 }
293
294
295 static int oval_directives_model_parse(xmlTextReaderPtr reader, struct oval_parser_context *context)
296 {
297 int depth = xmlTextReaderDepth(reader);
298 int ret = 0;
299
300 xmlTextReaderRead(reader);
301 while ((xmlTextReaderDepth(reader) > depth) && (ret != -1 )) {
302 if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
303 char *tagname = (char *)xmlTextReaderLocalName(reader);
304 char *namespace = (char *)xmlTextReaderNamespaceUri(reader);
305
306 int is_ovaldir = 0;
307 if (namespace != NULL) {
308 is_ovaldir = strcmp((const char*)OVAL_DIRECTIVES_NAMESPACE, namespace) == 0;
309 }
310 if (is_ovaldir && (strcmp(tagname, "generator") == 0)) {
311 struct oval_generator *gen;
312 gen = oval_directives_model_get_generator(context->directives_model);
313 ret = oval_parser_parse_tag(reader, context, &oval_generator_parse_tag, gen);
314 } else if (is_ovaldir && (strcmp(tagname, "directives") == 0)) {
315 struct oval_result_directives *def_dirs;
316 bool val;
317 def_dirs = oval_directives_model_get_defdirs(context->directives_model);
318 val=oval_parser_boolean_attribute(reader, "include_source_definitions", 1);
319 oval_result_directives_set_included(def_dirs, val);
320 ret = oval_parser_parse_tag(reader, context, &oval_result_directives_parse_tag, def_dirs);
321 } else if (is_ovaldir && (strcmp(tagname, "class_directives") == 0)) {
322 struct oval_result_directives *class_dir;
323 oval_definition_class_t class_type;
324 char *class_str = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "class");
325 class_type = oval_definition_class_enum(class_str);
326 class_dir = oval_directives_model_get_new_classdir(context->directives_model, class_type);
327 ret = oval_parser_parse_tag(reader, context, &oval_result_directives_parse_tag, class_dir);
328 free(class_str);
329 } else {
330 dW("Unprocessed tag: <%s:%s>.", namespace, tagname);
331 oval_parser_skip_tag(reader, context);
332 }
333
334 free(tagname);
335 free(namespace);
336 } else {
337 if (xmlTextReaderRead(reader) != XML_READER_TYPE_ELEMENT) {
338 ret = -1;
339 break;
340 }
341 }
342 }
343
344 return ret;
345 }
346
347 static const struct oscap_string_map OVAL_DIRECTIVE_MAP[] = {
348 {OVAL_RESULT_TRUE, "definition_true"},
349 {OVAL_RESULT_FALSE, "definition_false"},
350 {OVAL_RESULT_UNKNOWN, "definition_unknown"},
351 {OVAL_RESULT_ERROR, "definition_error"},
352 {OVAL_RESULT_NOT_EVALUATED, "definition_not_evaluated"},
353 {OVAL_RESULT_NOT_APPLICABLE, "definition_not_applicable"},
354 {OVAL_ENUMERATION_INVALID, NULL}
355 };
356
357 int oval_result_directives_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *usr) {
358 struct oval_result_directives *directives = (struct oval_result_directives *)usr;
359 int retcode = 0;
360
361 xmlChar *name = xmlTextReaderLocalName(reader);
362 oval_result_t type = oscap_string_to_enum(OVAL_DIRECTIVE_MAP, (const char *)name);
363
364 if ( (int) type != OVAL_ENUMERATION_INVALID) {
365 /*reported */
366 xmlChar *boolstr = xmlTextReaderGetAttribute(reader, BAD_CAST "reported");
367 bool reported = (strcmp((const char *)boolstr, "1") == 0) || (strcmp((const char *)boolstr, "true") == 0);
368 free(boolstr);
369 oval_result_directives_set_reported(directives, type, reported);
370
371 /*content */
372 xmlChar *contentstr = xmlTextReaderGetAttribute(reader, BAD_CAST "content");
373 oval_result_directive_content_t content = OVAL_DIRECTIVE_CONTENT_UNKNOWN;
374 if (contentstr) {
375 if (strcmp("thin", (const char *)contentstr) == 0)
376 content = OVAL_DIRECTIVE_CONTENT_THIN;
377 else if (strcmp("full", (const char *)contentstr) == 0)
378 content = OVAL_DIRECTIVE_CONTENT_FULL;
379
380 if (content != OVAL_DIRECTIVE_CONTENT_UNKNOWN) {
381 oval_result_directives_set_content(directives, type, content);
382 } else {
383 dW("Cannot resolve @content: \"%s\".", contentstr);
384 retcode = 1;
385 }
386 free(contentstr);
387 } else {
388 content = OVAL_DIRECTIVE_CONTENT_FULL;
389 }
390 } else {
391 dW("Cannot resolve <%s>.", name);
392 retcode = 1;
393 }
394 free(name);
395 return retcode;
396 }
397
398 xmlNode *oval_directives_model_to_dom(struct oval_directives_model *model, xmlDocPtr doc, xmlNode * parent)
399 {
400 xmlNode *root_node;
401 xmlNode *directives_node;
402 const char *class_str;
403 struct oval_result_directives * dirs;
404
405
406
407 if(parent == NULL) { /* standalone OVAL Directives Document */
408 root_node = xmlNewNode(NULL, BAD_CAST OVAL_ROOT_ELM_DIRECTIVES);
409 xmlDocSetRootElement(doc, root_node);
410
411 /*schemalocation */
412 xmlNewNsProp(root_node, lookup_xsi_ns(doc), BAD_CAST "schemaLocation", BAD_CAST OVAL_DIR_SCHEMA_LOCATION);
413
414 /* NS */
415 xmlNs *ns_common = xmlNewNs(root_node, OVAL_COMMON_NAMESPACE, BAD_CAST "oval");
416 xmlNs *ns_results = xmlNewNs(root_node, OVAL_RESULTS_NAMESPACE, BAD_CAST "oval-res");
417 xmlNs *ns_directives = xmlNewNs(root_node, OVAL_DIRECTIVES_NAMESPACE, NULL);
418 xmlSetNs(root_node, ns_common);
419 xmlSetNs(root_node, ns_results);
420 xmlSetNs(root_node, ns_directives);
421
422 /* Report generator */
423 oval_generator_to_dom(model->generator, doc, root_node);
424 }
425 else { /* Directives are part of OVAL Results Document */
426 root_node = parent;
427 }
428
429
430 /* Report default directives */
431 xmlNs *ns_search = xmlSearchNsByHref(doc, parent, OVAL_RESULTS_NAMESPACE);
432 directives_node = xmlNewTextChild(root_node, ns_search, BAD_CAST "directives", NULL);
433 dirs = oval_directives_model_get_defdirs(model);
434 if(!oval_result_directives_get_included(dirs))
435 xmlNewProp(directives_node, BAD_CAST "include_source_definitions", BAD_CAST "false");
436 oval_result_directives_to_dom(dirs, doc, directives_node);
437
438 /* Report class directives */
439 for(int i=OVAL_CLASS_COMPLIANCE; i<=OVAL_CLASS_VULNERABILITY; i++) {
440 dirs = oval_directives_model_get_classdir(model, i);
441 if (dirs) {
442 directives_node = xmlNewTextChild(root_node, ns_search, BAD_CAST "class_directives", NULL);
443 class_str = oval_definition_class_text(i);
444 xmlNewProp(directives_node, BAD_CAST "class", BAD_CAST class_str);
445 oval_result_directives_to_dom(dirs, doc, directives_node);
446 }
447 }
448
449 return root_node;
450 }
451
452 int oval_result_directives_to_dom(struct oval_result_directives *directives, xmlDoc * doc, xmlNode * parent) {
453 int retcode = 1;
454 xmlNs *ns_results = xmlSearchNsByHref(doc, parent, OVAL_RESULTS_NAMESPACE);
455 xmlNode *directives_node = parent;
456
457 const struct oscap_string_map *map;
458 for (map = OVAL_DIRECTIVE_MAP; map->string; map++) {
459 oval_result_t directive = (oval_result_t) map->value;
460 bool reported = oval_result_directives_get_reported(directives, directive);
461 oval_result_directive_content_t content = oval_result_directives_get_content(directives, directive);
462 xmlNode *directive_node = xmlNewTextChild(directives_node, ns_results, BAD_CAST(map->string), NULL);
463 char *val_reported = (reported) ? "true" : "false";
464 char *val_content = (content == OVAL_DIRECTIVE_CONTENT_FULL) ? "full" : "thin";
465 xmlNewProp(directive_node, BAD_CAST "reported", BAD_CAST val_reported);
466 xmlNewProp(directive_node, BAD_CAST "content", BAD_CAST val_content);
467 }
468 return retcode;
469 }
470
471 /**
472 * @endcond
473 */