Debian Codegraph @

src/OVAL/oval_object.c

raw symbols SCIP
1 /**
2 * @file oval_object.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 "oval_agent_api_impl.h"
41 #include "common/debug_priv.h"
42 #include "common/elements.h"
43 #include "public/oval_schema_version.h"
44
45 typedef struct oval_object {
46 struct oval_definition_model *model;
47 oval_subtype_t subtype;
48 struct oval_object *base_obj_ref;
49 struct oval_collection *notes;
50 char *comment;
51 char *id;
52 int deprecated;
53 int version;
54 struct oval_collection *object_content;
55 struct oval_collection *behaviors;
56 } oval_object_t;
57
58 bool oval_object_iterator_has_more(struct oval_object_iterator *oc_object)
59 {
60 return oval_collection_iterator_has_more((struct oval_iterator *)
61 oc_object);
62 }
63
64 struct oval_object *oval_object_iterator_next(struct oval_object_iterator
65 *oc_object)
66 {
67 return (struct oval_object *)
68 oval_collection_iterator_next((struct oval_iterator *)oc_object);
69 }
70
71 void oval_object_iterator_free(struct oval_object_iterator
72 *oc_object)
73 {
74 oval_collection_iterator_free((struct oval_iterator *)oc_object);
75 }
76
77 oval_family_t oval_object_get_family(struct oval_object *object)
78 {
79 __attribute__nonnull__(object);
80
81 return ((object->subtype) / 1000) * 1000;
82 }
83
84 oval_subtype_t oval_object_get_subtype(struct oval_object * object)
85 {
86 __attribute__nonnull__(object);
87
88 return ((struct oval_object *)object)->subtype;
89 }
90
91 const char *oval_object_get_name(struct oval_object *object)
92 {
93
94 __attribute__nonnull__(object);
95
96 return oval_subtype_get_text(object->subtype);
97 }
98
99 struct oval_string_iterator *oval_object_get_notes(struct oval_object *object)
100 {
101 __attribute__nonnull__(object);
102
103 return (struct oval_string_iterator *)oval_collection_iterator(object->notes);
104 }
105
106 char *oval_object_get_comment(struct oval_object *object)
107 {
108 __attribute__nonnull__(object);
109
110 return ((struct oval_object *)object)->comment;
111 }
112
113 char *oval_object_get_id(struct oval_object *object)
114 {
115 __attribute__nonnull__(object);
116
117 return ((struct oval_object *)object)->id;
118 }
119
120 bool oval_object_get_deprecated(struct oval_object *object)
121 {
122 __attribute__nonnull__(object);
123
124 return ((struct oval_object *)object)->deprecated;
125 }
126
127 int oval_object_get_version(struct oval_object *object)
128 {
129 __attribute__nonnull__(object);
130
131 return ((struct oval_object *)object)->version;
132 }
133
134 oval_schema_version_t oval_object_get_platform_schema_version(struct oval_object *object)
135 {
136 __attribute__nonnull__(object);
137 if (object->model == NULL) {
138 return OVAL_SCHEMA_VERSION_INVALID;
139 }
140 oval_family_t family = oval_object_get_family(object);
141 const char *platform = oval_family_get_text(family);
142 return oval_definition_model_get_platform_schema_version(object->model, platform);
143 }
144
145 struct oval_object_content_iterator *oval_object_get_object_contents(struct
146 oval_object
147 *object)
148 {
149 __attribute__nonnull__(object);
150
151 return (struct oval_object_content_iterator *)
152 oval_collection_iterator(object->object_content);
153 }
154
155 struct oval_behavior_iterator *oval_object_get_behaviors(struct oval_object *object)
156 {
157 __attribute__nonnull__(object);
158
159 return (struct oval_behavior_iterator *)
160 oval_collection_iterator(object->behaviors);
161 }
162
163 struct oval_object *oval_object_new(struct oval_definition_model *model, const char *id)
164 {
165 __attribute__nonnull__(model);
166 oval_object_t *object;
167
168 object = (oval_object_t *) malloc(sizeof(oval_object_t));
169 if (object == NULL)
170 return NULL;
171
172 object->comment = NULL;
173 object->id = oscap_strdup(id);
174 object->subtype = OVAL_SUBTYPE_UNKNOWN;
175 object->base_obj_ref = NULL;
176 object->deprecated = 0;
177 object->version = 0;
178 object->behaviors = oval_collection_new();
179 object->notes = oval_collection_new();
180 object->object_content = oval_collection_new();
181 object->model = model;
182
183 oval_definition_model_add_object(model, object);
184
185 return object;
186 }
187
188 struct oval_object *oval_object_clone2(struct oval_definition_model *new_model, struct oval_object *old_object, char *new_id)
189 {
190 __attribute__nonnull__(old_object);
191
192 struct oval_object *new_object;
193
194 if (new_id == NULL)
195 new_id = old_object->id;
196
197 new_object = oval_definition_model_get_object(new_model, new_id);
198 if (new_object == NULL) {
199 new_object = oval_object_new(new_model, new_id);
200 oval_object_set_comment(new_object, old_object->comment);
201 oval_object_set_subtype(new_object, old_object->subtype);
202 oval_object_set_deprecated(new_object, old_object->deprecated);
203 oval_object_set_version(new_object, old_object->version);
204
205 struct oval_behavior_iterator *behaviors = oval_object_get_behaviors(old_object);
206 while (oval_behavior_iterator_has_more(behaviors)) {
207 struct oval_behavior *behavior = oval_behavior_iterator_next(behaviors);
208 oval_object_add_behavior(new_object, oval_behavior_clone(new_model, behavior));
209 }
210 oval_behavior_iterator_free(behaviors);
211 struct oval_string_iterator *notes = oval_object_get_notes(old_object);
212 while (oval_string_iterator_has_more(notes)) {
213 char *note = oval_string_iterator_next(notes);
214 oval_object_add_note(new_object, note);
215 }
216 oval_string_iterator_free(notes);
217 struct oval_object_content_iterator *object_contents = oval_object_get_object_contents(old_object);
218 while (oval_object_content_iterator_has_more(object_contents)) {
219 struct oval_object_content *object_content = oval_object_content_iterator_next(object_contents);
220 oval_object_add_object_content(new_object,
221 oval_object_content_clone(new_model, object_content));
222 }
223 oval_object_content_iterator_free(object_contents);
224 }
225 return new_object;
226 }
227
228 struct oval_object *oval_object_clone(struct oval_definition_model *new_model, struct oval_object *old_object)
229 {
230 return oval_object_clone2(new_model, old_object, NULL);
231 }
232
233 void oval_object_free(struct oval_object *object)
234 {
235 if (object == NULL)
236 return;
237
238 if (object->comment != NULL)
239 free(object->comment);
240 if (object->id != NULL)
241 free(object->id);
242 oval_collection_free_items(object->behaviors, (oscap_destruct_func) oval_behavior_free);
243 oval_collection_free_items(object->notes, (oscap_destruct_func) free);
244 oval_collection_free_items(object->object_content, (oscap_destruct_func) oval_object_content_free);
245
246 object->comment = NULL;
247 object->id = NULL;
248 object->behaviors = NULL;
249 object->notes = NULL;
250 object->object_content = NULL;
251 free(object);
252 }
253
254 void oval_object_set_subtype(struct oval_object *object, oval_subtype_t subtype)
255 {
256 __attribute__nonnull__(object);
257 object->subtype = subtype;
258 }
259
260 void oval_object_add_note(struct oval_object *object, char *note)
261 {
262 __attribute__nonnull__(object);
263 oval_collection_add(object->notes, (void *)oscap_strdup(note));
264 }
265
266 void oval_object_set_comment(struct oval_object *object, char *comm)
267 {
268 __attribute__nonnull__(object);
269 if (object->comment != NULL)
270 free(object->comment);
271 object->comment = (comm == NULL) ? NULL : oscap_strdup(comm);
272 }
273
274 void oval_object_set_deprecated(struct oval_object *object, bool deprecated)
275 {
276 __attribute__nonnull__(object);
277 object->deprecated = deprecated;
278 }
279
280 void oval_object_set_version(struct oval_object *object, int version)
281 {
282 __attribute__nonnull__(object);
283 object->version = version;
284 }
285
286 void oval_object_add_object_content(struct oval_object *object, struct oval_object_content *content)
287 {
288 __attribute__nonnull__(object);
289 oval_collection_add(object->object_content, (void *)content);
290 }
291
292 void oval_object_add_behavior(struct oval_object *object, struct oval_behavior *behavior)
293 {
294 __attribute__nonnull__(object);
295 oval_collection_add(object->behaviors, (void *)behavior);
296 }
297
298 static void oval_note_consume(char *text, void *object)
299 {
300 oval_object_add_note(object, text);
301 }
302
303 static int _oval_object_parse_notes(xmlTextReaderPtr reader, struct oval_parser_context *context, void *user)
304 {
305 struct oval_object *object = (struct oval_object *)user;
306 return oscap_parser_text_value(reader, &oval_note_consume, object);
307 }
308
309 static void oval_behavior_consume(struct oval_behavior *behavior, void *object)
310 {
311 oval_object_add_behavior(object, behavior);
312 }
313
314 static void oval_content_consume(struct oval_object_content *content, void *object)
315 {
316 oval_object_add_object_content(object, content);
317 }
318
319 static int _oval_object_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *user)
320 {
321 struct oval_object *object = (struct oval_object *)user;
322 char *tagname = (char *)xmlTextReaderLocalName(reader);
323 xmlChar *namespace = xmlTextReaderNamespaceUri(reader);
324 int return_code = 0;
325 if ((strcmp(tagname, "notes") == 0)) {
326 return_code = oval_parser_parse_tag(reader, context, &_oval_object_parse_notes, object);
327 } else if (strcmp(tagname, "behaviors") == 0) {
328 return_code = oval_behavior_parse_tag(reader, context, oval_object_get_family(object), &oval_behavior_consume, object);
329 } else {
330 return_code = oval_object_content_parse_tag(reader, context, &oval_content_consume, object);
331 }
332
333 if (return_code != 0) {
334 dW("Parsing of <%s> terminated by an error at line %d.", tagname, xmlTextReaderGetParserLineNumber(reader));
335 }
336
337 free(tagname);
338 free(namespace);
339 return return_code;
340 }
341
342 /* -1 = error; 0 = OK; 1 = warning */
343 int oval_object_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *usr)
344 {
345 int ret;
346 char *comm = NULL;
347 char *version = NULL;
348 struct oval_definition_model *model = context->definition_model;
349
350 char *id = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "id");
351 struct oval_object *object = oval_definition_model_get_new_object(model, id);
352
353 oval_subtype_t subtype = oval_subtype_parse(reader);
354 if ( subtype == OVAL_SUBTYPE_UNKNOWN) {
355 dI("Unknown object %s, using independent/unknown", id);
356 subtype = OVAL_INDEPENDENT_UNKNOWN;
357 }
358 oval_object_set_subtype(object, subtype);
359
360 comm = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "comment");
361 if (comm != NULL) {
362 oval_object_set_comment(object, comm);
363 }
364
365 int deprecated = oval_parser_boolean_attribute(reader, "deprecated", 0);
366 oval_object_set_deprecated(object, deprecated);
367
368 version = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "version");
369 oval_object_set_version(object, atoi(version));
370
371 ret = oval_parser_parse_tag(reader, context, &_oval_object_parse_tag, object);
372
373 free(id);
374 free(comm);
375 free(version);
376 return ret;
377 }
378
379 xmlNode *oval_object_to_dom(struct oval_object *object, xmlDoc * doc, xmlNode * parent)
380 {
381 xmlNode *object_node = NULL;
382
383 /* skip unknown object */
384 oval_subtype_t subtype = oval_object_get_subtype(object);
385 if (subtype == OVAL_SUBTYPE_UNKNOWN) {
386 dW("Unable to convert unknown object %s, skipping", oval_object_get_id(object));
387 return object_node;
388 }
389
390 /* get object name */
391 const char *subtype_text = oval_subtype_get_text(subtype);
392 char *object_name = malloc(strlen(subtype_text) + 8);
393 sprintf(object_name, "%s_object", subtype_text);
394
395 oval_family_t family = oval_object_get_family(object);
396
397 /* search namespace & create child */
398 xmlNs *ns_family = oval_family_to_namespace(family, (const char *) OVAL_DEFINITIONS_NAMESPACE, doc, parent);
399 object_node = xmlNewTextChild(parent, ns_family, BAD_CAST object_name, NULL);
400 free(object_name);
401
402 char *id = oval_object_get_id(object);
403 xmlNewProp(object_node, BAD_CAST "id", BAD_CAST id);
404
405 char version[10];
406 *version = '\0';
407 snprintf(version, sizeof(version), "%d", oval_object_get_version(object));
408 xmlNewProp(object_node, BAD_CAST "version", BAD_CAST version);
409
410 char *comm = oval_object_get_comment(object);
411 if (comm)
412 xmlNewProp(object_node, BAD_CAST "comment", BAD_CAST comm);
413
414 bool deprecated = oval_object_get_deprecated(object);
415 if (deprecated)
416 xmlNewProp(object_node, BAD_CAST "deprecated", BAD_CAST "true");
417
418 struct oval_string_iterator *notes = oval_object_get_notes(object);
419 if (oval_string_iterator_has_more(notes)) {
420 xmlNs *ns_definitions = xmlSearchNsByHref(doc, parent, OVAL_DEFINITIONS_NAMESPACE);
421 xmlNode *notes_node = xmlNewTextChild(object_node, ns_definitions, BAD_CAST "notes", NULL);
422 while (oval_string_iterator_has_more(notes)) {
423 char *note = oval_string_iterator_next(notes);
424 xmlNewTextChild(notes_node, ns_definitions, BAD_CAST "note", BAD_CAST note);
425 }
426 }
427 oval_string_iterator_free(notes);
428
429 struct oval_behavior_iterator *behaviors = oval_object_get_behaviors(object);
430 if (oval_behavior_iterator_has_more(behaviors)) {
431 xmlNode *behaviors_node = xmlNewTextChild(object_node, ns_family, BAD_CAST "behaviors", NULL);
432 while (oval_behavior_iterator_has_more(behaviors)) {
433 struct oval_behavior *behavior = oval_behavior_iterator_next(behaviors);
434 char *key = oval_behavior_get_key(behavior);
435 char *value = oval_behavior_get_value(behavior);
436 xmlNewProp(behaviors_node, BAD_CAST key, BAD_CAST value);
437 }
438 }
439 oval_behavior_iterator_free(behaviors);
440
441 struct oval_object_content_iterator *contents = oval_object_get_object_contents(object);
442 while (oval_object_content_iterator_has_more(contents)) {
443 struct oval_object_content *content = oval_object_content_iterator_next(contents);
444 oval_object_content_to_dom(content, doc, object_node);
445 }
446 oval_object_content_iterator_free(contents);
447
448 return object_node;
449 }
450
451 struct oval_object *oval_object_create_internal(struct oval_object *obj, char *set_id)
452 {
453 struct oval_object *new_obj;
454 size_t oid_len, sid_len;
455 char *new_obj_id;
456
457 oid_len = strlen(obj->id);
458 set_id = strrchr(set_id, ':') + 1;
459 sid_len = strlen(set_id);
460 new_obj_id = malloc(oid_len + sid_len + 2);
461 memcpy(new_obj_id, obj->id, oid_len);
462 new_obj_id[oid_len] = 'i';
463 memcpy(new_obj_id + oid_len + 1, set_id, sid_len);
464 new_obj_id[oid_len + sid_len + 1] = '\0';
465 new_obj = oval_object_clone2(obj->model, obj, new_obj_id);
466 new_obj->base_obj_ref = obj;
467 free(new_obj_id);
468
469 return new_obj;
470 }
471
472 struct oval_object *oval_object_get_base_obj(struct oval_object *obj)
473 {
474 return obj->base_obj_ref;
475 }