Debian Codegraph @

src/OVAL/oval_definition.c

raw symbols SCIP
1 /**
2 * @file oval_definition.c
3 * \brief Open Vulnerability and Assessment Language
4 *
5 * See more details at http://oval.mitre.org/
6 */
7
8 /*
9 * Copyright 2009--2014 Red Hat Inc., Durham, North Carolina.
10 * All Rights Reserved.
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 *
26 * Authors:
27 * "David Niemoller" <[email protected]>
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #include "oval_definitions_impl.h"
39 #include "adt/oval_collection_impl.h"
40 #include "adt/oval_string_map_impl.h"
41 #include "oval_agent_api_impl.h"
42
43 #include "common/util.h"
44 #include "common/debug_priv.h"
45 #include "common/elements.h"
46 #include "common/_error.h"
47
48
49 /***************************************************************************/
50 /* Variable definitions
51 * */
52
53 typedef struct oval_definition {
54 struct oval_definition_model *model;
55 char *id;
56 int version;
57 oval_definition_class_t class;
58 int deprecated;
59 char *title;
60 char *description;
61 struct oval_collection *affected;
62 struct oval_collection *reference;
63 struct oval_collection *notes;
64 char *anyxml;
65 struct oval_criteria_node *criteria;
66 } oval_definition_t;
67
68
69 /* End of variable definitions
70 * */
71 /***************************************************************************/
72
73 char *oval_definition_get_id(struct oval_definition *definition)
74 {
75 __attribute__nonnull__(definition);
76
77 return ((struct oval_definition *)definition)->id;
78 }
79
80 int oval_definition_get_version(struct oval_definition *definition)
81 {
82 __attribute__nonnull__(definition);
83
84 return ((struct oval_definition *)definition)->version;
85 }
86
87 oval_definition_class_t oval_definition_get_class(struct oval_definition *definition)
88 {
89 __attribute__nonnull__(definition);
90
91 return ((struct oval_definition *)definition)->class;
92 }
93
94 bool oval_definition_get_deprecated(struct oval_definition *definition)
95 {
96 __attribute__nonnull__(definition);
97
98 return ((struct oval_definition *)definition)->deprecated;
99 }
100
101 char *oval_definition_get_title(struct oval_definition *definition)
102 {
103 __attribute__nonnull__(definition);
104
105 return ((struct oval_definition *)definition)->title;
106 }
107
108 char *oval_definition_get_description(struct oval_definition *definition)
109 {
110 __attribute__nonnull__(definition);
111
112 return ((struct oval_definition *)definition)->description;
113 }
114
115 struct oval_affected_iterator *oval_definition_get_affected(struct oval_definition
116 *definition)
117 {
118 __attribute__nonnull__(definition);
119
120 return (struct oval_affected_iterator *)
121 oval_collection_iterator(definition->affected);
122 }
123
124 struct oval_reference_iterator *oval_definition_get_references(struct oval_definition
125 *definition)
126 {
127 __attribute__nonnull__(definition);
128
129 return (struct oval_reference_iterator *)
130 oval_collection_iterator(definition->reference);
131 }
132
133 struct oval_string_iterator *oval_definition_get_notes(struct oval_definition *definition) {
134 __attribute__nonnull__(definition);
135
136 return (struct oval_string_iterator *)
137 oval_collection_iterator(definition->notes);
138 }
139
140 struct oval_criteria_node *oval_definition_get_criteria(struct oval_definition
141 *definition)
142 {
143 __attribute__nonnull__(definition);
144
145 return ((struct oval_definition *)definition)->criteria;
146 }
147
148 struct oval_definition *oval_definition_new(struct oval_definition_model *model, const char *id)
149 {
150 __attribute__nonnull__(model);
151 struct oval_definition *definition = (struct oval_definition *)malloc(sizeof(oval_definition_t));
152
153 definition->id = oscap_strdup(id);
154 definition->version = 0;
155 definition->class = OVAL_CLASS_UNKNOWN;
156 definition->deprecated = 0;
157 definition->title = NULL;
158 definition->description = NULL;
159 definition->affected = oval_collection_new();
160 definition->reference = oval_collection_new();
161 definition->notes = oval_collection_new();
162
163 definition->anyxml = NULL;
164 definition->criteria = NULL;
165 definition->model = model;
166
167 oval_definition_model_add_definition(model, definition);
168
169 return definition;
170 }
171
172 struct oval_definition *oval_definition_clone
173 (struct oval_definition_model *new_model, struct oval_definition *old_definition) {
174 __attribute__nonnull__(old_definition);
175
176 struct oval_definition *new_definition = oval_definition_model_get_definition(new_model, old_definition->id);
177 if (new_definition == NULL) {
178
179 new_definition = oval_definition_new(new_model, old_definition->id);
180 oval_definition_set_version(new_definition, old_definition->version);
181 oval_definition_set_class(new_definition, old_definition->class);
182 oval_definition_set_deprecated(new_definition, old_definition->deprecated);
183 oval_definition_set_title(new_definition, old_definition->title);
184 oval_definition_set_description(new_definition, old_definition->description);
185
186 struct oval_affected_iterator *affecteds = oval_definition_get_affected(old_definition);
187 while (oval_affected_iterator_has_more(affecteds)) {
188 struct oval_affected *old_affected = oval_affected_iterator_next(affecteds);
189 oval_definition_add_affected(new_definition, oval_affected_clone(new_model, old_affected));
190 }
191 oval_affected_iterator_free(affecteds);
192 struct oval_reference_iterator *references = oval_definition_get_references(old_definition);
193 while (oval_reference_iterator_has_more(references)) {
194 struct oval_reference *old_reference = oval_reference_iterator_next(references);
195 oval_definition_add_reference(new_definition, oval_reference_clone(new_model, old_reference));
196 }
197 oval_reference_iterator_free(references);
198 struct oval_string_iterator *notes = oval_definition_get_notes(old_definition);
199 while (oval_string_iterator_has_more(notes)) {
200 char *old_note = oval_string_iterator_next(notes);
201 oval_definition_add_note(new_definition, old_note);
202 }
203 oval_string_iterator_free(notes);
204
205 new_definition->anyxml = strdup(old_definition->anyxml);
206
207 oval_definition_set_criteria(new_definition, oval_criteria_node_clone(new_model, old_definition->criteria));
208 }
209 return new_definition;
210 }
211
212 void oval_definition_free(struct oval_definition *definition)
213 {
214 __attribute__nonnull__(definition);
215
216 if (definition->id != NULL)
217 free(definition->id);
218 if (definition->title != NULL)
219 free(definition->title);
220 if (definition->description != NULL)
221 free(definition->description);
222 if (definition->criteria != NULL)
223 oval_criteria_node_free(definition->criteria);
224 oval_collection_free_items(definition->affected, (oscap_destruct_func) oval_affected_free);
225 oval_collection_free_items(definition->reference, (oscap_destruct_func) oval_reference_free);
226 oval_collection_free_items(definition->notes, (oscap_destruct_func) free);
227
228 free(definition->anyxml);
229
230 definition->affected = NULL;
231 definition->criteria = NULL;
232 definition->description = NULL;
233 definition->id = NULL;
234 definition->reference = NULL;
235 definition->notes = NULL;
236 definition->anyxml = NULL;
237 definition->title = NULL;
238 free(definition);
239 }
240
241 bool oval_definition_iterator_has_more(struct oval_definition_iterator
242 *oc_definition)
243 {
244 return oval_collection_iterator_has_more((struct oval_iterator *)
245 oc_definition);
246 }
247
248 struct oval_definition *oval_definition_iterator_next(struct
249 oval_definition_iterator
250 *oc_definition)
251 {
252 return (struct oval_definition *)
253 oval_collection_iterator_next((struct oval_iterator *)
254 oc_definition);
255 }
256
257 void oval_definition_iterator_free(struct
258 oval_definition_iterator
259 *oc_definition)
260 {
261 oval_collection_iterator_free((struct oval_iterator *)
262 oc_definition);
263 }
264
265 void oval_definition_set_version(struct oval_definition *definition, int version)
266 {
267 __attribute__nonnull__(definition);
268 definition->version = version;
269 }
270
271 void oval_definition_set_class(struct oval_definition *definition, oval_definition_class_t class)
272 {
273 __attribute__nonnull__(definition);
274 definition->class = class;
275 }
276
277 void oval_definition_set_deprecated(struct oval_definition *definition, bool deprecated)
278 {
279 __attribute__nonnull__(definition);
280 definition->deprecated = deprecated;
281 }
282
283 void oval_definition_set_title(struct oval_definition *definition, char *title)
284 {
285 __attribute__nonnull__(definition);
286 if (definition->title != NULL)
287 free(definition->title);
288 definition->title = (title == NULL) ? NULL : oscap_strdup(title);
289 }
290
291 void oval_definition_set_description(struct oval_definition *definition, char *description)
292 {
293 __attribute__nonnull__(definition);
294 if (definition->description)
295 free(definition->description);
296 definition->description = (description == NULL) ? NULL : oscap_strdup(description);
297 }
298
299 void oval_definition_set_criteria(struct oval_definition *definition, struct oval_criteria_node *criteria)
300 {
301 __attribute__nonnull__(definition);
302 definition->criteria = criteria;
303 }
304
305 void oval_definition_add_affected(struct oval_definition *definition, struct oval_affected *affected)
306 {
307 __attribute__nonnull__(definition);
308 oval_collection_add(definition->affected, affected);
309 }
310
311 void oval_definition_add_reference(struct oval_definition *definition, struct oval_reference *ref)
312 {
313 __attribute__nonnull__(definition);
314 oval_collection_add(definition->reference, ref);
315 }
316
317 void oval_definition_add_note(struct oval_definition *definition, char *note) {
318 __attribute__nonnull__(definition);
319 oval_collection_add(definition->notes, note);
320 }
321
322 static void _oval_definition_title_consumer(char *string, void *user)
323 {
324 __attribute__nonnull__(user);
325
326 struct oval_definition *definition = (struct oval_definition *)user;
327 char *title = definition->title;
328 if (title == NULL)
329 title = oscap_strdup(string);
330 else {
331 int newsize = strlen(title) + strlen(string) + 1;
332 char *newtitle = (char *)malloc(newsize * sizeof(char));
333 if (newtitle == NULL)
334 return;
335
336 strcpy(newtitle, title);
337 strcat(newtitle, string);
338 free(title);
339 title = newtitle;
340 }
341 definition->title = title;
342 }
343
344 static void _oval_definition_description_consumer(char *string, void *user)
345 {
346 __attribute__nonnull__(user);
347
348 struct oval_definition *definition = (struct oval_definition *)user;
349 char *description = definition->description;
350 if (description == NULL)
351 description = oscap_strdup(string);
352 else {
353 int newsize = strlen(description) + strlen(string) + 1;
354 char *newdescription = (char *)malloc(newsize * sizeof(char));
355 if (newdescription == NULL)
356 return;
357
358 *newdescription = '\0';
359 strcpy(newdescription, description);
360 strcat(newdescription, string);
361 free(description);
362 description = newdescription;
363 }
364 definition->description = description;
365 }
366
367 static void _oval_definition_affected_consumer(struct oval_affected *affected, void *user)
368 {
369 __attribute__nonnull__(user);
370
371 struct oval_definition *definition = (struct oval_definition *)user;
372 oval_collection_add(definition->affected, (void *)affected);
373 }
374
375 static void oval_reference_consume(struct oval_reference *ref, void *def)
376 {
377
378 __attribute__nonnull__(def);
379
380 struct oval_definition *definition = def;
381 oval_collection_add(definition->reference, (void *)ref);
382 }
383
384 static int _oval_definition_parse_metadata(xmlTextReaderPtr reader, struct oval_parser_context *context, void *user)
385 {
386 struct oval_definition *definition = (struct oval_definition *)user;
387 char *tagname = (char *)xmlTextReaderLocalName(reader);
388 int return_code;
389 if ((strcmp(tagname, "title") == 0)) {
390 return_code = oscap_parser_text_value(reader, &_oval_definition_title_consumer, definition);
391 } else if (strcmp(tagname, "description") == 0) {
392 return_code = oscap_parser_text_value(reader, &_oval_definition_description_consumer, definition);
393 } else if (strcmp(tagname, "affected") == 0) {
394 return_code = oval_affected_parse_tag(reader, context, &_oval_definition_affected_consumer, definition);
395 } else if (strcmp(tagname, "oval_repository") == 0) { /* NOOP */
396 return_code = oval_parser_skip_tag(reader, context);
397 } else if (strcmp(tagname, "reference") == 0) {
398 return_code = oval_reference_parse_tag(reader, context, &oval_reference_consume, definition);
399 } else {
400 definition->anyxml = (char *) xmlTextReaderReadOuterXml(reader);
401 return_code = oval_parser_skip_tag(reader, context);
402 }
403 free(tagname);
404 return return_code;
405 }
406
407 static void _oval_definition_criteria_consumer(struct oval_criteria_node *criteria, void *user)
408 {
409 __attribute__nonnull__(user);
410
411 struct oval_definition *definition = (struct oval_definition *)user;
412 definition->criteria = criteria;
413 }
414
415 static int _oval_definition_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *user)
416 {
417 struct oval_definition *definition = (struct oval_definition *)user;
418 char *tagname = (char *)xmlTextReaderLocalName(reader);
419 int return_code;
420 if ((strcmp(tagname, "metadata") == 0)) {
421 return_code = oval_parser_parse_tag(reader, context, &_oval_definition_parse_metadata, definition);
422 } else if ((strcmp(tagname, "criteria") == 0)) {
423 return_code = oval_criteria_parse_tag(reader, context, &_oval_definition_criteria_consumer, definition);
424 } else {
425 dD("Skipping tag: %s.", tagname);
426 return_code = oval_parser_skip_tag(reader, context);
427 }
428 free(tagname);
429 return return_code;
430 }
431
432 int oval_definition_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *usr)
433 {
434 struct oval_definition_model *model = context->definition_model;
435 char *id = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "id");
436 struct oval_definition *definition = oval_definition_model_get_new_definition(model, id);
437 free(id);
438 id = NULL;
439
440 char *version = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "version");
441 oval_definition_set_version(definition, atoi(version));
442 free(version);
443 version = NULL;
444
445 char *class = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "class");
446 oval_definition_set_class(definition, oval_definition_class_enum(class));
447 free(class);
448 class = NULL;
449
450 int deprecated = oval_parser_boolean_attribute(reader, "deprecated", 0);
451 oval_definition_set_deprecated(definition, deprecated);
452 int return_code = oval_parser_parse_tag(reader, context, &_oval_definition_parse_tag, definition);
453 return return_code;
454 }
455
456
457 xmlNode *oval_definition_to_dom(struct oval_definition *definition, xmlDoc * doc, xmlNode * parent)
458 {
459 xmlNode *nodestr, *nodelst;
460 xmlDoc * docstr;
461 xmlNs *ns_definitions = xmlSearchNsByHref(doc, parent, OVAL_DEFINITIONS_NAMESPACE);
462 xmlNode *definition_node = xmlNewTextChild(parent, ns_definitions, BAD_CAST "definition", NULL);
463
464 char *id = oval_definition_get_id(definition);
465 xmlNewProp(definition_node, BAD_CAST "id", BAD_CAST id);
466
467 char version[10];
468 *version = '\0';
469 snprintf(version, sizeof(version), "%d", oval_definition_get_version(definition));
470 xmlNewProp(definition_node, BAD_CAST "version", BAD_CAST version);
471
472 oval_definition_class_t class = oval_definition_get_class(definition);
473 xmlNewProp(definition_node, BAD_CAST "class", BAD_CAST oval_definition_class_text(class));
474
475 bool deprecated = oval_definition_get_deprecated(definition);
476 if (deprecated)
477 xmlNewProp(definition_node, BAD_CAST "deprecated", BAD_CAST "true");
478
479 xmlNode *metadata_node = xmlNewTextChild(definition_node, ns_definitions, BAD_CAST "metadata", NULL);
480
481 char *title = oval_definition_get_title(definition);
482 xmlNewTextChild(metadata_node, ns_definitions, BAD_CAST "title", BAD_CAST title);
483
484 struct oval_affected_iterator *affecteds = oval_definition_get_affected(definition);
485 while (oval_affected_iterator_has_more(affecteds)) {
486 xmlNode *affected_node = xmlNewTextChild(metadata_node, ns_definitions, BAD_CAST "affected", NULL);
487 struct oval_affected *affected = oval_affected_iterator_next(affecteds);
488 oval_affected_family_t family = oval_affected_get_family(affected);
489 xmlNewProp(affected_node, BAD_CAST "family", BAD_CAST oval_affected_family_get_text(family));
490 struct oval_string_iterator *platforms = oval_affected_get_platforms(affected);
491 while (oval_string_iterator_has_more(platforms)) {
492 char *platform = oval_string_iterator_next(platforms);
493 xmlNewTextChild(affected_node, ns_definitions, BAD_CAST "platform", BAD_CAST platform);
494 }
495 oval_string_iterator_free(platforms);
496 struct oval_string_iterator *products = oval_affected_get_products(affected);
497 while (oval_string_iterator_has_more(products)) {
498 char *product = oval_string_iterator_next(products);
499 xmlNewTextChild(affected_node, ns_definitions, BAD_CAST "product", BAD_CAST product);
500 }
501 oval_string_iterator_free(products);
502 }
503 oval_affected_iterator_free(affecteds);
504
505 struct oval_reference_iterator *references = oval_definition_get_references(definition);
506 while (oval_reference_iterator_has_more(references)) {
507 struct oval_reference *ref = oval_reference_iterator_next(references);
508 xmlNode *referenceNode = xmlNewTextChild(metadata_node, ns_definitions, BAD_CAST "reference", NULL);
509 char *source = oval_reference_get_source(ref);
510 char *ref_id = oval_reference_get_id(ref);
511 char *ref_url = oval_reference_get_url(ref);
512 xmlNewProp(referenceNode, BAD_CAST "source", BAD_CAST source);
513 xmlNewProp(referenceNode, BAD_CAST "ref_id", BAD_CAST ref_id);
514 if (ref_url)
515 xmlNewProp(referenceNode, BAD_CAST "ref_url", BAD_CAST ref_url);
516 }
517 oval_reference_iterator_free(references);
518
519 char *description = oval_definition_get_description(definition);
520 xmlNewTextChild(metadata_node, ns_definitions, BAD_CAST "description", BAD_CAST description);
521
522 struct oval_string_iterator *notes = oval_definition_get_notes(definition);
523 if (oval_string_iterator_has_more(notes)) {
524 xmlNode *notes_node = xmlNewTextChild(definition_node, ns_definitions, BAD_CAST "notes", NULL);
525 while (oval_string_iterator_has_more(notes)) {
526 char *note = oval_string_iterator_next(notes);
527 xmlNewTextChild(notes_node, ns_definitions, BAD_CAST "note", BAD_CAST note);
528 }
529 }
530 oval_string_iterator_free(notes);
531
532 docstr = xmlReadDoc(BAD_CAST definition->anyxml, NULL, NULL, 0);
533 nodestr = xmlDocGetRootElement(docstr);
534 nodelst = xmlDocCopyNode(nodestr, doc, 1);
535 xmlAddChildList(metadata_node, nodelst);
536 xmlFreeDoc(docstr);
537
538 struct oval_criteria_node *criteria = oval_definition_get_criteria(definition);
539 if (criteria)
540 oval_criteria_node_to_dom(criteria, doc, definition_node);
541
542 return definition_node;
543 }
544
545 const char * oval_definition_model_supported(void)
546 {
547 return OVAL_SUPPORTED;
548 }