Debian Codegraph @

src/OVAL/oval_objectContent.c

raw symbols SCIP
1 /**
2 * @file oval_objectContent.c
3 * \brief Open Vulnerability and Assessment Language
4 *
5 * See more details at http://oval.mitre.org/
6 */
7
8 /*
9 * Copyright 2009--2013 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/util.h"
42 #include "common/debug_priv.h"
43
44 typedef struct oval_object_content {
45 struct oval_definition_model *model;
46 char *fieldName;
47 oval_object_content_type_t type;
48 } oval_object_content_t;
49
50 typedef struct oval_object_content_ENTITY {
51 struct oval_definition_model *model;
52 char *fieldName;
53 oval_object_content_type_t type;
54 struct oval_entity *entity; /*type == OVAL_OBJECTCONTENT_ENTITY */
55 oval_check_t varCheck; /*type == OVAL_OBJECTCONTENT_ENTITY */
56 } oval_object_content_ENTITY_t;
57
58 typedef struct oval_object_content_SET {
59 struct oval_definition_model *model;
60 char *fieldName;
61 oval_object_content_type_t type;
62 struct oval_setobject *set; /*type == OVAL_OBJECTCONTENT_SET */
63 } oval_object_content_SET_t;
64
65 typedef struct oval_object_content_FILTER {
66 struct oval_definition_model *model;
67 char *fieldName;
68 oval_object_content_type_t type;
69 struct oval_filter *filter; /*type == OVAL_OBJECTCONTENT_FILTER */
70 } oval_object_content_FILTER_t;
71
72 bool oval_object_content_iterator_has_more(struct oval_object_content_iterator
73 *oc_object_content)
74 {
75 return oval_collection_iterator_has_more((struct oval_iterator *)
76 oc_object_content);
77 }
78
79 struct oval_object_content *oval_object_content_iterator_next(struct
80 oval_object_content_iterator
81 *oc_object_content)
82 {
83 return (struct oval_object_content *)
84 oval_collection_iterator_next((struct oval_iterator *)
85 oc_object_content);
86 }
87
88 void oval_object_content_iterator_free(struct
89 oval_object_content_iterator
90 *oc_object_content)
91 {
92 oval_collection_iterator_free((struct oval_iterator *)
93 oc_object_content);
94 }
95
96 char *oval_object_content_get_field_name(struct oval_object_content *content)
97 {
98 return ((struct oval_object_content *)content)->fieldName;
99 }
100
101 oval_object_content_type_t oval_object_content_get_type(struct
102 oval_object_content
103 *content)
104 {
105 __attribute__nonnull__(content);
106
107 return ((struct oval_object_content *)content)->type;
108 }
109
110 struct oval_entity *oval_object_content_get_entity(struct oval_object_content
111 *content)
112 {
113 __attribute__nonnull__(content);
114
115 /*type == OVAL_OBJECTCONTENT_ENTITY */
116 struct oval_entity *entity = NULL;
117 if (oval_object_content_get_type(content) == OVAL_OBJECTCONTENT_ENTITY) {
118 entity = ((struct oval_object_content_ENTITY *)content)->entity;
119 }
120 return entity;
121 }
122
123 oval_check_t oval_object_content_get_varCheck(struct oval_object_content * content)
124 {
125 __attribute__nonnull__(content);
126
127 /*type == OVAL_OBJECTCONTENT_ENTITY */
128 oval_check_t varCheck = OVAL_CHECK_UNKNOWN;
129 if (oval_object_content_get_type(content) == OVAL_OBJECTCONTENT_ENTITY) {
130 varCheck = ((struct oval_object_content_ENTITY *)content)->varCheck;
131 }
132 return varCheck;
133 }
134
135 struct oval_setobject *oval_object_content_get_setobject(struct oval_object_content *content)
136 {
137 __attribute__nonnull__(content);
138
139 /*type == OVAL_OBJECTCONTENT_SET */
140 struct oval_setobject *set = NULL;
141 if (oval_object_content_get_type(content) == OVAL_OBJECTCONTENT_SET) {
142 set = ((struct oval_object_content_SET *)content)->set;
143 }
144 return set;
145 }
146
147 struct oval_filter *oval_object_content_get_filter(struct oval_object_content *content)
148 {
149 __attribute__nonnull__(content);
150
151 /* type == OVAL_OBJECTCONTENT_FILTER */
152 struct oval_filter *filter = NULL;
153 if (oval_object_content_get_type(content) == OVAL_OBJECTCONTENT_FILTER) {
154 filter = ((struct oval_object_content_FILTER *)content)->filter;
155 }
156 return filter;
157 }
158
159 struct oval_object_content
160 *oval_object_content_new(struct oval_definition_model *model, oval_object_content_type_t type)
161 {
162 struct oval_object_content *content = NULL;
163 switch (type) {
164 case OVAL_OBJECTCONTENT_ENTITY:{
165 struct oval_object_content_ENTITY *entity =
166 (oval_object_content_ENTITY_t *) malloc(sizeof(oval_object_content_ENTITY_t));
167 if (entity == NULL)
168 return NULL;
169
170 content = (oval_object_content_t *) entity;
171 entity->entity = NULL;
172 entity->varCheck = OVAL_CHECK_UNKNOWN;
173 }
174 break;
175 case OVAL_OBJECTCONTENT_SET:{
176 struct oval_object_content_SET *set =
177 (oval_object_content_SET_t *) malloc(sizeof(oval_object_content_SET_t));
178 if (set == NULL)
179 return NULL;
180
181 set->set = NULL;
182 content = (oval_object_content_t *) set;
183 }
184 break;
185 case OVAL_OBJECTCONTENT_FILTER:{
186 struct oval_object_content_FILTER *filter =
187 (oval_object_content_FILTER_t *) malloc(sizeof(oval_object_content_FILTER_t));
188 if (filter == NULL)
189 return NULL;
190
191 filter->filter = NULL;
192 content = (oval_object_content_t *) filter;
193 }
194 break;
195 default:
196 dE("Unsupported object content type: %d.", type);
197 return NULL;
198 }
199 content->model = model;
200 content->fieldName = NULL;
201 content->type = type;
202 return content;
203 }
204
205 struct oval_object_content *oval_object_content_clone
206 (struct oval_definition_model *new_model, struct oval_object_content *old_content) {
207 struct oval_object_content *new_content = oval_object_content_new(new_model, old_content->type);
208 char *name = oval_object_content_get_field_name(old_content);
209 oval_object_content_set_field_name(new_content, name);
210 switch (new_content->type) {
211 case OVAL_OBJECTCONTENT_ENTITY:{
212 struct oval_entity *entity = oval_entity_clone(new_model, oval_object_content_get_entity(old_content));
213 oval_object_content_set_entity(new_content, entity);
214 oval_check_t check = oval_object_content_get_varCheck(old_content);
215 oval_object_content_set_varCheck(new_content, check);
216 } break;
217 case OVAL_OBJECTCONTENT_SET:{
218 struct oval_setobject *set = oval_object_content_get_setobject(old_content);
219 oval_object_content_set_setobject(new_content, oval_setobject_clone(new_model, set));
220 } break;
221 case OVAL_OBJECTCONTENT_FILTER:{
222 struct oval_filter *filter = oval_object_content_get_filter(old_content);
223 oval_object_content_set_filter(new_content, oval_filter_clone(new_model, filter));
224 } break;
225 default:
226 /*NOOP*/;
227 }
228 return new_content;
229 }
230
231 void oval_object_content_free(struct oval_object_content *content)
232 {
233 __attribute__nonnull__(content);
234
235 if (content->fieldName != NULL)
236 free(content->fieldName);
237 content->fieldName = NULL;
238 switch (content->type) {
239 case OVAL_OBJECTCONTENT_ENTITY:{
240 struct oval_object_content_ENTITY *entity = (oval_object_content_ENTITY_t *) content;
241 if (entity->entity != NULL)
242 oval_entity_free(entity->entity);
243 entity->entity = NULL;
244 }
245 break;
246 case OVAL_OBJECTCONTENT_SET:{
247 struct oval_object_content_SET *set = (oval_object_content_SET_t *) content;
248 if (set->set != NULL)
249 oval_setobject_free(set->set);
250 set->set = NULL;
251 }
252 break;
253 case OVAL_OBJECTCONTENT_FILTER:{
254 struct oval_object_content_FILTER *filter = (oval_object_content_FILTER_t *) content;
255 if (filter->filter != NULL)
256 oval_filter_free(filter->filter);
257 filter->filter = NULL;
258 }
259 break;
260 case OVAL_OBJECTCONTENT_UNKNOWN:
261 break;
262 }
263 free(content);
264 }
265
266 void oval_object_content_set_type(struct oval_object_content *content, oval_object_content_type_t type)
267 {
268 __attribute__nonnull__(content);
269 content->type = type;
270 }
271
272 void oval_object_content_set_field_name(struct oval_object_content *content, char *name)
273 {
274 __attribute__nonnull__(content);
275 if (content->fieldName != NULL)
276 free(content->fieldName);
277 content->fieldName = (name == NULL) ? NULL : oscap_strdup(name);
278 }
279
280 void oval_object_content_set_entity(struct oval_object_content *content, struct oval_entity *entity)
281 { /*type == OVAL_OBJECTCONTENT_ENTITY */
282 __attribute__nonnull__(content);
283 if (content->type == OVAL_OBJECTCONTENT_ENTITY) {
284 oval_object_content_ENTITY_t *content_ENTITY = (oval_object_content_ENTITY_t *) content;
285 content_ENTITY->entity = entity;
286 }
287 }
288
289 void oval_object_content_set_varCheck(struct oval_object_content *content, oval_check_t check)
290 { /*type == OVAL_OBJECTCONTENT_ENTITY */
291 __attribute__nonnull__(content);
292 if (content->type == OVAL_OBJECTCONTENT_ENTITY) {
293 oval_object_content_ENTITY_t *content_ENTITY = (oval_object_content_ENTITY_t *) content;
294 content_ENTITY->varCheck = check;
295 }
296 }
297
298 void oval_object_content_set_setobject(struct oval_object_content *content, struct oval_setobject *set)
299 { /*type == OVAL_OBJECTCONTENT_SET */
300 __attribute__nonnull__(content);
301 if (content->type == OVAL_OBJECTCONTENT_SET) {
302 oval_object_content_SET_t *content_SET = (oval_object_content_SET_t *) content;
303 content_SET->set = set;
304 }
305 }
306
307 void oval_object_content_set_filter(struct oval_object_content *content, struct oval_filter *filter)
308 { /*type == OVAL_OBJECTCONTENT_FILTER */
309 __attribute__nonnull__(content);
310 if (content->type == OVAL_OBJECTCONTENT_FILTER) {
311 oval_object_content_FILTER_t *content_FILTER = (oval_object_content_FILTER_t *) content;
312 content_FILTER->filter = filter;
313 }
314 }
315
316 /*typedef void (*oval_object_content_consumer)(struct oval_object_content*,void*);*/
317 static void oval_consume_entity(struct oval_entity *entity, void *content_entity)
318 {
319 __attribute__nonnull__(entity);
320
321 ((struct oval_object_content_ENTITY *)content_entity)->entity = entity;
322 }
323
324 static void oval_consume_set(struct oval_setobject *set, void *content_set)
325 {
326 __attribute__nonnull__(content_set);
327
328 ((struct oval_object_content_SET *)content_set)->set = set;
329 }
330
331 static void oval_consume_filter(struct oval_filter *filter, void *content_filter)
332 {
333 __attribute__nonnull__(content_filter);
334
335 ((struct oval_object_content_FILTER *)content_filter)->filter = filter;
336 }
337
338 /* prevent gcc false warning */
339 //#pragma GCC diagnostic push
340 #pragma GCC diagnostic ignored "-Wswitch"
341 int oval_object_content_parse_tag(xmlTextReaderPtr reader,
342 struct oval_parser_context *context,
343 oval_object_content_consumer consumer, void *user)
344 {
345 __attribute__nonnull__(context);
346
347 char *tagname = (char *)xmlTextReaderLocalName(reader);
348 xmlChar *namespace = xmlTextReaderNamespaceUri(reader);
349 oval_object_content_type_t type = OVAL_OBJECTCONTENT_UNKNOWN;
350 struct oval_object_content *content;
351 int return_code = 0;
352
353 if (!strcmp(tagname, "set")) {
354 type = OVAL_OBJECTCONTENT_SET;
355 } else if (!strcmp(tagname, "filter")) {
356 type = OVAL_OBJECTCONTENT_FILTER;
357 } else {
358 type = OVAL_OBJECTCONTENT_ENTITY;
359 }
360
361 content = oval_object_content_new(context->definition_model, type);
362 if (content == NULL)
363 return -1;
364
365 content->fieldName = tagname;
366 switch (type) {
367 case OVAL_OBJECTCONTENT_ENTITY:{
368 struct oval_object_content_ENTITY *content_entity =
369 (struct oval_object_content_ENTITY *)content;
370 return_code =
371 oval_entity_parse_tag(reader, context,
372 (oscap_consumer_func) oval_consume_entity, content_entity);
373 content_entity->varCheck = oval_check_parse(reader, "var_check", OVAL_CHECK_ALL);
374 };
375 break;
376 case OVAL_OBJECTCONTENT_SET:{
377 struct oval_object_content_SET *content_set = (struct oval_object_content_SET *)content;
378 return_code = oval_set_parse_tag(reader, context, &oval_consume_set, content_set);
379 };
380 break;
381 case OVAL_OBJECTCONTENT_FILTER:{
382 struct oval_object_content_FILTER *content_filter =
383 (struct oval_object_content_FILTER *) content;
384 return_code = oval_filter_parse_tag(reader, context, &oval_consume_filter, content_filter);
385 };
386 break;
387 }
388
389 (*consumer) (content, user);
390
391 if (return_code != 0)
392 dW("Parsing of <%s> terminated by an error at line %d.",tagname, xmlTextReaderGetParserLineNumber(reader));
393
394 free(namespace);
395 return return_code;
396 }
397 //#pragma GCC diagnostic pop
398
399 xmlNode *oval_object_content_to_dom(struct oval_object_content *content, xmlDoc * doc, xmlNode * parent) {
400 xmlNode *content_node;
401 switch (oval_object_content_get_type(content)) {
402 case OVAL_OBJECTCONTENT_ENTITY:{
403 struct oval_entity *entity = oval_object_content_get_entity(content);
404 content_node = oval_entity_to_dom(entity, doc, parent);
405 oval_check_t check = oval_object_content_get_varCheck(content);
406
407 if (check != OVAL_CHECK_ALL || xmlHasProp(content_node, BAD_CAST "var_ref"))
408 xmlNewProp(content_node, BAD_CAST "var_check", BAD_CAST oval_check_get_text(check));
409 }
410 break;
411 case OVAL_OBJECTCONTENT_SET:{
412 struct oval_setobject *set = oval_object_content_get_setobject(content);
413 content_node = oval_set_to_dom(set, doc, parent);
414 } break;
415 case OVAL_OBJECTCONTENT_FILTER:{
416 struct oval_filter *filter;
417
418 filter = oval_object_content_get_filter(content);
419 content_node = oval_filter_to_dom(filter, doc, parent);
420 }
421 break;
422 default:
423 content_node = NULL;
424 }
425
426 return content_node;
427 }