Debian Codegraph @

src/OVAL/oval_test.c

raw symbols SCIP
1 /**
2 * @file oval_test.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 "public/oval_types.h"
39 #include "oval_definitions_impl.h"
40 #include "adt/oval_collection_impl.h"
41 #include "oval_agent_api_impl.h"
42 #include "common/oscap_string.h"
43 #include "common/util.h"
44 #include "common/debug_priv.h"
45 #include "common/elements.h"
46 #include "common/_error.h"
47
48 typedef struct oval_test {
49 struct oval_definition_model *model;
50 oval_subtype_t subtype;
51 struct oval_collection *notes;
52 char *comment;
53 char *id;
54 int deprecated;
55 int version;
56 oval_existence_t existence;
57 oval_check_t check;
58 oval_operator_t state_operator;
59 struct oval_object *object;
60 struct oval_collection *states;
61 } oval_test_t;
62
63 bool oval_test_iterator_has_more(struct oval_test_iterator *oc_test)
64 {
65 return oval_collection_iterator_has_more((struct oval_iterator *)
66 oc_test);
67 }
68
69 struct oval_test *oval_test_iterator_next(struct oval_test_iterator *oc_test)
70 {
71 return (struct oval_test *)
72 oval_collection_iterator_next((struct oval_iterator *)oc_test);
73 }
74
75 void oval_test_iterator_free(struct oval_test_iterator *oc_test)
76 {
77 oval_collection_iterator_free((struct oval_iterator *)oc_test);
78 }
79
80 oval_family_t oval_test_get_family(struct oval_test *test)
81 {
82 __attribute__nonnull__(test);
83
84 return ((test->subtype) / 1000) * 1000;
85 }
86
87 oval_subtype_t oval_test_get_subtype(struct oval_test * test)
88 {
89 __attribute__nonnull__(test);
90
91 return test->subtype;
92 }
93
94 struct oval_string_iterator *oval_test_get_notes(struct oval_test *test)
95 {
96 __attribute__nonnull__(test);
97
98 return (struct oval_string_iterator *)oval_collection_iterator(test->notes);
99 }
100
101 char *oval_test_get_comment(struct oval_test *test)
102 {
103 __attribute__nonnull__(test);
104
105 return test->comment;
106 }
107
108 char *oval_test_get_id(struct oval_test *test)
109 {
110 __attribute__nonnull__(test);
111
112 return test->id;
113 }
114
115 bool oval_test_get_deprecated(struct oval_test * test)
116 {
117 __attribute__nonnull__(test);
118
119 return test->deprecated;
120 }
121
122 int oval_test_get_version(struct oval_test *test)
123 {
124 __attribute__nonnull__(test);
125
126 return test->version;
127 }
128
129 oval_existence_t oval_test_get_existence(struct oval_test * test)
130 {
131 __attribute__nonnull__(test);
132
133 return test->existence;
134 }
135
136 oval_check_t oval_test_get_check(struct oval_test * test)
137 {
138 __attribute__nonnull__(test);
139
140 return test->check;
141 }
142
143 oval_operator_t oval_test_get_state_operator(struct oval_test *test)
144 {
145 __attribute__nonnull__(test);
146
147 return test->state_operator;
148 }
149
150 struct oval_object *oval_test_get_object(struct oval_test *test)
151 {
152 __attribute__nonnull__(test);
153
154 return test->object;
155 }
156
157 struct oval_state_iterator *oval_test_get_states(struct oval_test *test)
158 {
159 __attribute__nonnull__(test);
160
161 return (struct oval_state_iterator *) oval_collection_iterator(test->states);
162 }
163
164 char *oval_test_get_state_names(struct oval_test *test)
165 {
166 __attribute__nonnull__(test);
167
168 struct oval_state_iterator *ste_itr = oval_test_get_states(test);
169 if (!oval_state_iterator_has_more(ste_itr)) {
170 oval_state_iterator_free(ste_itr);
171 return NULL;
172 }
173 struct oscap_string *state_list = oscap_string_new();
174 oscap_string_append_char(state_list, '\'');
175 while (1) {
176 struct oval_state *ste = oval_state_iterator_next(ste_itr);
177 const char *ste_id = oval_state_get_id(ste);
178 oscap_string_append_string(state_list, ste_id);
179 if (!oval_state_iterator_has_more(ste_itr)) {
180 break;
181 }
182 oscap_string_append_string(state_list, "', '");
183 }
184 oscap_string_append_char(state_list, '\'');
185 char *state_names = oscap_strdup(oscap_string_get_cstr(state_list));
186 oscap_string_free(state_list);
187 oval_state_iterator_free(ste_itr);
188 return state_names;
189 }
190
191 struct oval_test *oval_test_new(struct oval_definition_model *model, const char *id)
192 {
193 __attribute__nonnull__(model);
194 oval_test_t *test;
195
196 test = (oval_test_t *) malloc(sizeof(oval_test_t));
197 if (test == NULL)
198 return NULL;
199
200 test->deprecated = 0;
201 test->version = 0;
202 test->check = OVAL_CHECK_UNKNOWN;
203 test->existence = OVAL_EXISTENCE_UNKNOWN;
204 test->state_operator = OVAL_OPERATOR_AND;
205 test->subtype = OVAL_SUBTYPE_UNKNOWN;
206 test->comment = NULL;
207 test->id = oscap_strdup(id);
208 test->object = NULL;
209 test->states = oval_collection_new();
210 test->notes = oval_collection_new();
211 test->model = model;
212
213 oval_definition_model_add_test(model, test);
214
215 return test;
216 }
217
218 struct oval_test *oval_test_clone(struct oval_definition_model *new_model, struct oval_test *old_test) {
219 __attribute__nonnull__(old_test);
220
221 struct oval_state_iterator *ste_itr;
222 struct oval_test *new_test = oval_definition_model_get_test(new_model, old_test->id);
223 if (new_test == NULL) {
224 new_test = oval_test_new(new_model, old_test->id);
225 oval_test_set_deprecated(new_test, old_test->deprecated);
226 oval_test_set_version(new_test, old_test->version);
227 oval_test_set_check(new_test, old_test->check);
228 oval_test_set_existence(new_test, old_test->existence);
229 oval_test_set_state_operator(new_test, old_test->state_operator);
230 oval_test_set_subtype(new_test, old_test->subtype);
231 oval_test_set_comment(new_test, old_test->comment);
232
233 if (old_test->object) {
234 struct oval_object *object = oval_object_clone(new_model, old_test->object);
235 oval_test_set_object(new_test, object);
236 }
237
238 ste_itr = oval_test_get_states(old_test);
239 while (oval_state_iterator_has_more(ste_itr)) {
240 struct oval_state *ste;
241
242 ste = oval_state_iterator_next(ste_itr);
243 ste = oval_state_clone(new_model, ste);
244 oval_test_add_state(new_test, ste);
245 }
246 oval_state_iterator_free(ste_itr);
247
248 struct oval_string_iterator *notes = oval_test_get_notes(old_test);
249 while (oval_string_iterator_has_more(notes)) {
250 char *note = oval_string_iterator_next(notes);
251 oval_test_add_note(new_test, note);
252 }
253 oval_string_iterator_free(notes);
254 }
255 return new_test;
256 }
257
258 void oval_test_free(struct oval_test *test)
259 {
260 if (test == NULL)
261 return;
262
263 free(test->comment);
264 free(test->id);
265 oval_collection_free_items(test->notes, free);
266 oval_collection_free(test->states);
267
268 free(test);
269 }
270
271 void oval_test_set_deprecated(struct oval_test *test, bool deprecated)
272 {
273 __attribute__nonnull__(test);
274 test->deprecated = deprecated;
275 }
276
277 void oval_test_set_version(struct oval_test *test, int version)
278 {
279 __attribute__nonnull__(test);
280 test->version = version;
281 }
282
283 void oval_test_set_subtype(struct oval_test *test, oval_subtype_t subtype)
284 {
285 __attribute__nonnull__(test);
286 test->subtype = subtype;
287 }
288
289 void oval_test_set_comment(struct oval_test *test, char *comm)
290 {
291 __attribute__nonnull__(test);
292 if (test->comment != NULL)
293 free(test->comment);
294 test->comment = oscap_strdup(comm);
295 }
296
297 void oval_test_set_existence(struct oval_test *test, oval_existence_t existence)
298 {
299 __attribute__nonnull__(test);
300 test->existence = existence;
301 }
302
303 void oval_test_set_state_operator(struct oval_test *test, oval_operator_t state_operator)
304 {
305 __attribute__nonnull__(test);
306 test->state_operator = state_operator;
307 }
308
309 void oval_test_set_check(struct oval_test *test, oval_check_t check)
310 {
311 __attribute__nonnull__(test);
312 test->check = check;
313 }
314
315 void oval_test_set_object(struct oval_test *test, struct oval_object *object)
316 {
317 __attribute__nonnull__(test);
318 test->object = object;
319 }
320
321 void oval_test_add_state(struct oval_test *test, struct oval_state *state)
322 {
323 __attribute__nonnull__(test);
324 oval_collection_add(test->states, state);
325 }
326
327 void oval_test_add_note(struct oval_test *test, char *note)
328 {
329 __attribute__nonnull__(test);
330 oval_collection_add(test->notes, (void *)oscap_strdup(note));
331 }
332
333 static void _oval_test_parse_notes_consumer(char *text, void *test)
334 {
335 oval_test_add_note(test, text);
336 }
337
338 static int _oval_test_parse_notes(xmlTextReaderPtr reader, struct oval_parser_context *context, void *user)
339 {
340 struct oval_test *test = (struct oval_test *)user;
341 return oscap_parser_text_value(reader, &_oval_test_parse_notes_consumer, test);
342 }
343
344 static int _oval_test_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *user)
345 {
346 struct oval_test *test = (struct oval_test *)user;
347 char *tagname = (char *)xmlTextReaderLocalName(reader);
348 int return_code = 0;
349 if ((strcmp(tagname, "notes") == 0)) {
350 return_code = oval_parser_parse_tag(reader, context, &_oval_test_parse_notes, test);
351 } else if ((strcmp(tagname, "object") == 0)) {
352 char *object_ref = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "object_ref");
353 if (object_ref != NULL) {
354 struct oval_definition_model *model = context->definition_model;
355 struct oval_object *object = oval_definition_model_get_new_object(model, object_ref);
356 free(object_ref);
357 object_ref = NULL;
358 oval_test_set_object(test, object);
359 }
360 } else if ((strcmp(tagname, "state") == 0)) {
361 char *state_ref = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "state_ref");
362 if (state_ref != NULL) {
363 struct oval_definition_model *model = context->definition_model;
364 struct oval_state *state = oval_definition_model_get_new_state(model, state_ref);
365 oval_test_add_state(test, state);
366 free(state_ref);
367 state_ref = NULL;
368 }
369 } else {
370 dW("Skipping tag <%s>.", tagname);
371 return_code = oval_parser_skip_tag(reader, context);
372 }
373
374 free(tagname);
375 return return_code;
376
377 }
378
379 int oval_test_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *usr)
380 {
381 int ret = 0;
382 char *comm = NULL;
383 char *version = NULL;
384 struct oval_definition_model *model = context->definition_model;
385
386 char *id = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "id");
387 if (id == NULL) {
388 dW("Found test element without id attribute. Ignoring");
389 return 1;
390 }
391 struct oval_test *test = oval_definition_model_get_new_test(model, id);
392
393 oval_subtype_t subtype = oval_subtype_parse(reader);
394 if (subtype == OVAL_SUBTYPE_UNKNOWN) {
395 dI("Unknown test type %s, using independent/unknown", id);
396 subtype = OVAL_INDEPENDENT_UNKNOWN;
397 }
398 oval_test_set_subtype(test, subtype);
399
400 oval_operator_t ste_operator = oval_operator_parse(reader, "state_operator", OVAL_OPERATOR_AND);
401 oval_test_set_state_operator(test, ste_operator);
402
403 oval_check_t check = oval_check_parse(reader, "check", OVAL_CHECK_UNKNOWN);
404 if (check == OVAL_CHECK_NONE_EXIST) {
405 dW("The 'none exist' CheckEnumeration value has been deprecated. "
406 "Converted to check='none satisfy' and check_existence='none exist'.");
407 oval_test_set_check(test, OVAL_CHECK_NONE_SATISFY);
408 oval_test_set_existence(test, OVAL_NONE_EXIST);
409 } else {
410 oval_existence_t existence;
411
412 oval_test_set_check(test, check);
413 existence = oval_existence_parse(reader, "check_existence", OVAL_AT_LEAST_ONE_EXISTS);
414 oval_test_set_existence(test, existence);
415 }
416
417 comm = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "comment");
418 if (comm != NULL) {
419 oval_test_set_comment(test, comm);
420 }
421
422 int deprecated = oval_parser_boolean_attribute(reader, "deprecated", 0);
423 oval_test_set_deprecated(test, deprecated);
424
425 version = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "version");
426 if (version != NULL) {
427 oval_test_set_version(test, atoi(version));
428 }
429
430
431 ret = oval_parser_parse_tag(reader, context, &_oval_test_parse_tag, test);
432
433 free(version);
434 free(comm);
435 free(id);
436 return ret;
437 }
438
439
440 xmlNode *oval_test_to_dom(struct oval_test *test, xmlDoc * doc, xmlNode * parent)
441 {
442 xmlNode * test_node=NULL;
443
444 /* skip unknown test */
445 oval_subtype_t subtype = oval_test_get_subtype(test);
446 if (subtype == OVAL_SUBTYPE_UNKNOWN) {
447 dW("Unable to convert unknown test %s, skipping", oval_test_get_id(test));
448 return test_node;
449 }
450
451 /* get test name */
452 const char *subtype_text = oval_subtype_get_text(subtype);
453 char *test_name = malloc(strlen(subtype_text) + 6);
454 sprintf(test_name, "%s_test", subtype_text);
455
456 oval_family_t family = oval_test_get_family(test);
457
458 /* search namespace & create child */
459 xmlNs *ns_family = oval_family_to_namespace(family, (const char *) OVAL_DEFINITIONS_NAMESPACE, doc, parent);
460 test_node = xmlNewTextChild(parent, ns_family, BAD_CAST test_name, NULL);
461 free(test_name);
462
463 char *id = oval_test_get_id(test);
464 xmlNewProp(test_node, BAD_CAST "id", BAD_CAST id);
465
466 char version[10];
467 *version = '\0';
468 snprintf(version, sizeof(version), "%d", oval_test_get_version(test));
469 xmlNewProp(test_node, BAD_CAST "version", BAD_CAST version);
470
471 oval_existence_t existence = oval_test_get_existence(test);
472 if (existence != OVAL_AT_LEAST_ONE_EXISTS)
473 xmlNewProp(test_node, BAD_CAST "check_existence", BAD_CAST oval_existence_get_text(existence));
474
475 oval_check_t check = oval_test_get_check(test);
476 xmlNewProp(test_node, BAD_CAST "check", BAD_CAST oval_check_get_text(check));
477
478 oval_operator_t ste_operator = oval_test_get_state_operator(test);
479 if (ste_operator != OVAL_OPERATOR_AND)
480 xmlNewProp(test_node, BAD_CAST "state_operator", BAD_CAST oval_operator_get_text(ste_operator));
481
482 char *comm = oval_test_get_comment(test);
483 xmlNewProp(test_node, BAD_CAST "comment", BAD_CAST comm);
484
485 bool deprecated = oval_test_get_deprecated(test);
486 if (deprecated)
487 xmlNewProp(test_node, BAD_CAST "deprecated", BAD_CAST "true");
488
489 struct oval_string_iterator *notes = oval_test_get_notes(test);
490 if (oval_string_iterator_has_more(notes)) {
491 xmlNs *ns_definitions = xmlSearchNsByHref(doc, parent, OVAL_DEFINITIONS_NAMESPACE);
492 xmlNode *notes_node = xmlNewTextChild(test_node, ns_definitions, BAD_CAST "notes", NULL);
493 while (oval_string_iterator_has_more(notes)) {
494 char *note = oval_string_iterator_next(notes);
495 xmlNewTextChild(notes_node, ns_definitions, BAD_CAST "note", BAD_CAST note);
496 }
497 }
498 oval_string_iterator_free(notes);
499
500 struct oval_object *object = oval_test_get_object(test);
501 if (object) {
502 xmlNode *object_node = xmlNewTextChild(test_node, ns_family, BAD_CAST "object", NULL);
503 xmlNewProp(object_node, BAD_CAST "object_ref", BAD_CAST oval_object_get_id(object));
504 }
505
506 struct oval_state_iterator *ste_itr = oval_test_get_states(test);
507 while (oval_state_iterator_has_more(ste_itr)) {
508 struct oval_state *state;
509
510 state = oval_state_iterator_next(ste_itr);
511 xmlNode *state_node = xmlNewTextChild(test_node, ns_family, BAD_CAST "state", NULL);
512 xmlNewProp(state_node, BAD_CAST "state_ref", BAD_CAST oval_state_get_id(state));
513 }
514 oval_state_iterator_free(ste_itr);
515
516 return test_node;
517 }