Debian Codegraph @

src/OVAL/results/oval_resultTest.c

raw symbols SCIP
1 /**
2 * @file oval_resultTest.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 * "Peter Vrabec" <[email protected]>
29 * Šimon Lukašík
30 */
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #include <string.h>
37 #include <ctype.h>
38 #include "oval_agent_api_impl.h"
39 #ifdef OVAL_PROBES_ENABLED
40 #include "oval_probe_impl.h"
41 #endif
42 #include "results/oval_results_impl.h"
43 #include "results/oval_status_counter.h"
44 #include "oval_cmp_impl.h"
45 #include "adt/oval_collection_impl.h"
46 #include "adt/oval_string_map_impl.h"
47 #include "collectVarRefs_impl.h"
48 #include "public/oval_types.h"
49 #include "common/util.h"
50 #include "common/debug_priv.h"
51 #include "common/_error.h"
52
53 typedef struct oval_result_test {
54 struct oval_result_system *system;
55 struct oval_test *test;
56 oval_result_t result;
57 struct oval_collection *messages;
58 struct oval_collection *items;
59 struct oval_collection *bindings;
60 int instance;
61 bool bindings_initialized;
62 } oval_result_test_t;
63
64 struct oval_result_test *oval_result_test_new(struct oval_result_system *sys, char *tstid)
65 {
66 oval_result_test_t *test = (oval_result_test_t *)
67 malloc(sizeof(oval_result_test_t));
68 if (test == NULL)
69 return NULL;
70
71 struct oval_syschar_model *syschar_model = oval_result_system_get_syschar_model(sys);
72 struct oval_definition_model *definition_model = oval_syschar_model_get_definition_model(syschar_model);
73 test->system = sys;
74 test->test = oval_definition_model_get_new_test(definition_model, tstid);
75 test->messages = oval_collection_new();
76 test->result = OVAL_RESULT_NOT_EVALUATED;
77 test->instance = 1;
78 test->items = oval_collection_new();
79 test->bindings = oval_collection_new();
80 test->bindings_initialized = false;
81 return test;
82 }
83
84 struct oval_result_test *oval_result_test_clone
85 (struct oval_result_system *new_system, struct oval_result_test *old_test) {
86 __attribute__nonnull__(old_test);
87
88 const char *testid = oval_result_test_get_id(old_test);
89 struct oval_result_test *new_test = oval_result_test_new(new_system, (char *) testid);
90 struct oval_result_item_iterator *old_items = oval_result_test_get_items(old_test);
91 while (oval_result_item_iterator_has_more(old_items)) {
92 struct oval_result_item *old_item = oval_result_item_iterator_next(old_items);
93 struct oval_result_item *new_item = oval_result_item_clone(new_system, old_item);
94 oval_result_test_add_item(new_test, new_item);
95 }
96 oval_result_item_iterator_free(old_items);
97
98 struct oval_variable_binding_iterator *old_bindings = oval_result_test_get_bindings(old_test);
99 while (oval_variable_binding_iterator_has_more(old_bindings)) {
100 struct oval_variable_binding *old_binding = oval_variable_binding_iterator_next(old_bindings);
101 struct oval_variable_binding *new_binding = oval_variable_binding_clone(old_binding, NULL);
102 oval_result_test_add_binding(new_test, new_binding);
103 }
104 oval_variable_binding_iterator_free(old_bindings);
105
106 struct oval_message_iterator *old_messages = oval_result_test_get_messages(old_test);
107 while (oval_message_iterator_has_more(old_messages)) {
108 struct oval_message *old_message = oval_message_iterator_next(old_messages);
109 struct oval_message *new_message = oval_message_clone(old_message);
110 oval_result_test_add_message(new_test, new_message);
111 }
112 oval_message_iterator_free(old_messages);
113
114 oval_result_test_set_instance(new_test, oval_result_test_get_instance(old_test));
115 oval_result_test_set_result(new_test, old_test->result);
116
117 return new_test;
118 }
119
120 struct oval_result_test *make_result_test_from_oval_test(struct oval_result_system *sys, struct oval_test *oval_test, int variable_instance) {
121 char *test_id = oval_test_get_id(oval_test);
122 struct oval_result_test *test = oval_result_test_new(sys, test_id);
123 oval_result_test_set_instance(test, variable_instance);
124 return test;
125 }
126
127 void oval_result_test_free(struct oval_result_test *test)
128 {
129 __attribute__nonnull__(test);
130
131 oval_collection_free_items(test->messages, (oscap_destruct_func) oval_message_free);
132 oval_collection_free_items(test->items, (oscap_destruct_func) oval_result_item_free);
133 oval_collection_free_items(test->bindings, (oscap_destruct_func) oval_variable_binding_free);
134
135 test->system = NULL;
136 test->test = NULL;
137 test->messages = NULL;
138 test->result = OVAL_RESULT_NOT_EVALUATED;
139 test->items = NULL;
140 test->bindings = NULL;
141 test->instance = 1;
142 free(test);
143 }
144
145 struct oval_result_system *oval_result_test_get_system(struct oval_result_test *rtest)
146 {
147 __attribute__nonnull__(rtest);
148
149 return rtest->system;
150 }
151
152 struct oval_test *oval_result_test_get_test(struct oval_result_test *rtest)
153 {
154 __attribute__nonnull__(rtest);
155
156 return ((struct oval_result_test *)rtest)->test;
157 }
158
159 int ores_add_res(struct oresults *ores, oval_result_t res)
160 {
161 switch (res) {
162 case OVAL_RESULT_TRUE:
163 ores->true_cnt++;
164 break;
165 case OVAL_RESULT_FALSE:
166 ores->false_cnt++;
167 break;
168 case OVAL_RESULT_UNKNOWN:
169 ores->unknown_cnt++;
170 break;
171 case OVAL_RESULT_ERROR:
172 ores->error_cnt++;
173 break;
174 case OVAL_RESULT_NOT_EVALUATED:
175 ores->noteval_cnt++;
176 break;
177 case OVAL_RESULT_NOT_APPLICABLE:
178 ores->notappl_cnt++;
179 break;
180 default:
181 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Invalid oval result type: %d.", res);
182 return 1;
183 }
184
185 return 0;
186 }
187
188 void ores_clear(struct oresults *ores)
189 {
190 memset(ores, 0, sizeof (*ores));
191 }
192
193 oval_result_t ores_get_result_bychk(struct oresults *ores, oval_check_t check)
194 {
195 oval_result_t result = OVAL_RESULT_ERROR;
196
197 if (ores->true_cnt == 0 &&
198 ores->false_cnt == 0 &&
199 ores->error_cnt == 0 &&
200 ores->unknown_cnt == 0 &&
201 ores->notappl_cnt == 0 &&
202 ores->noteval_cnt == 0)
203 return OVAL_RESULT_UNKNOWN;
204
205 if (ores->notappl_cnt > 0 &&
206 ores->noteval_cnt == 0 &&
207 ores->false_cnt == 0 && ores->error_cnt == 0 && ores->unknown_cnt == 0 && ores->true_cnt == 0)
208 return OVAL_RESULT_NOT_APPLICABLE;
209
210 switch (check) {
211 case OVAL_CHECK_ALL:
212 if (ores->true_cnt > 0 &&
213 ores->false_cnt == 0 && ores->error_cnt == 0 && ores->unknown_cnt == 0 && ores->noteval_cnt == 0) {
214 result = OVAL_RESULT_TRUE;
215 } else if (ores->false_cnt > 0) {
216 result = OVAL_RESULT_FALSE;
217 } else if (ores->false_cnt == 0 && ores->error_cnt > 0) {
218 result = OVAL_RESULT_ERROR;
219 } else if (ores->false_cnt == 0 && ores->error_cnt == 0 && ores->unknown_cnt > 0) {
220 result = OVAL_RESULT_UNKNOWN;
221 } else if (ores->false_cnt == 0 && ores->error_cnt == 0 && ores->unknown_cnt == 0 && ores->noteval_cnt > 0) {
222 result = OVAL_RESULT_NOT_EVALUATED;
223 }
224 break;
225 case OVAL_CHECK_AT_LEAST_ONE:
226 if (ores->true_cnt > 0) {
227 result = OVAL_RESULT_TRUE;
228 } else if (ores->false_cnt > 0 &&
229 ores->true_cnt == 0 &&
230 ores->unknown_cnt == 0 && ores->error_cnt == 0 && ores->noteval_cnt == 0) {
231 result = OVAL_RESULT_FALSE;
232 } else if (ores->true_cnt == 0 && ores->error_cnt > 0) {
233 result = OVAL_RESULT_ERROR;
234 } else if (ores->false_cnt == 0 && ores->error_cnt == 0 && ores->unknown_cnt > 0) {
235 result = OVAL_RESULT_UNKNOWN;
236 } else if (ores->false_cnt == 0 && ores->error_cnt == 0 && ores->unknown_cnt == 0 && ores->noteval_cnt > 0) {
237 result = OVAL_RESULT_NOT_EVALUATED;
238 }
239 break;
240 case OVAL_CHECK_NONE_EXIST:
241 dW("The 'none exist' CheckEnumeration value has been deprecated. "
242 "Converted to check='none satisfy'.");
243 /* FALLTHROUGH */
244 case OVAL_CHECK_NONE_SATISFY:
245 if (ores->true_cnt > 0) {
246 result = OVAL_RESULT_FALSE;
247 } else if (ores->true_cnt == 0 && ores->error_cnt > 0) {
248 result = OVAL_RESULT_ERROR;
249 } else if (ores->true_cnt == 0 && ores->error_cnt == 0 && ores->unknown_cnt > 0) {
250 result = OVAL_RESULT_UNKNOWN;
251 } else if (ores->true_cnt == 0 && ores->error_cnt == 0 && ores->unknown_cnt == 0 && ores->noteval_cnt > 0) {
252 result = OVAL_RESULT_NOT_EVALUATED;
253 } else if (ores->false_cnt > 0 &&
254 ores->error_cnt == 0 &&
255 ores->unknown_cnt == 0 && ores->noteval_cnt == 0 && ores->true_cnt == 0) {
256 result = OVAL_RESULT_TRUE;
257 }
258 break;
259 case OVAL_CHECK_ONLY_ONE:
260 if (ores->true_cnt == 1 && ores->error_cnt == 0 && ores->unknown_cnt == 0 && ores->noteval_cnt == 0) {
261 result = OVAL_RESULT_TRUE;
262 } else if (ores->true_cnt > 1) {
263 result = OVAL_RESULT_FALSE;
264 } else if (ores->true_cnt < 2 && ores->error_cnt > 0) {
265 result = OVAL_RESULT_ERROR;
266 } else if (ores->true_cnt < 2 && ores->error_cnt == 0 && ores->unknown_cnt > 0) {
267 result = OVAL_RESULT_UNKNOWN;
268 } else if (ores->true_cnt < 2 && ores->error_cnt == 0 && ores->unknown_cnt == 0 && ores->noteval_cnt > 0) {
269 result = OVAL_RESULT_NOT_EVALUATED;
270 } else if (ores->true_cnt != 1 && ores->false_cnt > 0) {
271 result = OVAL_RESULT_FALSE;
272 }
273 break;
274 default:
275 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Invalid check value: %d.", check);
276 result = OVAL_RESULT_ERROR;
277 }
278
279 return result;
280 }
281
282 oval_result_t ores_get_result_byopr(struct oresults *ores, oval_operator_t op)
283 {
284 oval_result_t result = OVAL_RESULT_ERROR;
285
286 if (ores->true_cnt == 0 &&
287 ores->false_cnt == 0 &&
288 ores->error_cnt == 0 &&
289 ores->unknown_cnt == 0 &&
290 ores->notappl_cnt == 0 &&
291 ores->noteval_cnt == 0)
292 return OVAL_RESULT_UNKNOWN;
293
294 if (ores->notappl_cnt > 0 &&
295 ores->noteval_cnt == 0 &&
296 ores->false_cnt == 0 && ores->error_cnt == 0 && ores->unknown_cnt == 0 && ores->true_cnt == 0)
297 return OVAL_RESULT_NOT_APPLICABLE;
298
299 switch (op) {
300 case OVAL_OPERATOR_AND:
301 if (ores->true_cnt > 0 &&
302 ores->false_cnt == 0 && ores->error_cnt == 0 && ores->unknown_cnt == 0 && ores->noteval_cnt == 0) {
303 result = OVAL_RESULT_TRUE;
304 } else if (ores->false_cnt > 0) {
305 result = OVAL_RESULT_FALSE;
306 } else if (ores->false_cnt == 0 && ores->error_cnt > 0) {
307 result = OVAL_RESULT_ERROR;
308 } else if (ores->false_cnt == 0 && ores->error_cnt == 0 && ores->unknown_cnt > 0) {
309 result = OVAL_RESULT_UNKNOWN;
310 } else if (ores->false_cnt == 0 && ores->error_cnt == 0 && ores->unknown_cnt == 0 && ores->noteval_cnt > 0) {
311 result = OVAL_RESULT_NOT_EVALUATED;
312 }
313 break;
314 case OVAL_OPERATOR_ONE:
315 if (ores->true_cnt == 1 &&
316 ores->false_cnt >= 0 &&
317 ores->error_cnt == 0 && ores->unknown_cnt == 0 && ores->noteval_cnt == 0 && ores->notappl_cnt >= 0) {
318 result = OVAL_RESULT_TRUE;
319 } else if (ores->true_cnt >= 2 &&
320 ores->false_cnt >= 0 &&
321 ores->error_cnt >= 0 &&
322 ores->unknown_cnt >= 0 && ores->noteval_cnt >= 0 && ores->notappl_cnt >= 0) {
323 result = OVAL_RESULT_FALSE;
324 } else if (ores->true_cnt == 0 &&
325 ores->false_cnt >= 0 &&
326 ores->error_cnt == 0 &&
327 ores->unknown_cnt == 0 && ores->noteval_cnt == 0 && ores->notappl_cnt >= 0) {
328 result = OVAL_RESULT_FALSE;
329 } else if (ores->true_cnt < 2 &&
330 ores->false_cnt >= 0 &&
331 ores->error_cnt > 0 &&
332 ores->unknown_cnt >= 0 && ores->noteval_cnt >= 0 && ores->notappl_cnt >= 0) {
333 result = OVAL_RESULT_ERROR;
334 } else if (ores->true_cnt < 2 &&
335 ores->false_cnt >= 0 &&
336 ores->error_cnt == 0 &&
337 ores->unknown_cnt >= 1 && ores->noteval_cnt >= 0 && ores->notappl_cnt >= 0) {
338 result = OVAL_RESULT_UNKNOWN;
339 } else if (ores->true_cnt < 2 &&
340 ores->false_cnt >= 0 &&
341 ores->error_cnt == 0 &&
342 ores->unknown_cnt == 0 && ores->noteval_cnt > 0 && ores->notappl_cnt >= 0) {
343 result = OVAL_RESULT_NOT_EVALUATED;
344 }
345 break;
346 case OVAL_OPERATOR_OR:
347 if (ores->true_cnt > 0) {
348 result = OVAL_RESULT_TRUE;
349 } else if (ores->true_cnt == 0 &&
350 ores->false_cnt > 0 &&
351 ores->error_cnt == 0 && ores->unknown_cnt == 0 && ores->noteval_cnt == 0) {
352 result = OVAL_RESULT_FALSE;
353 } else if (ores->true_cnt == 0 && ores->error_cnt > 0) {
354 result = OVAL_RESULT_ERROR;
355 } else if (ores->true_cnt == 0 && ores->error_cnt == 0 && ores->unknown_cnt > 0) {
356 result = OVAL_RESULT_UNKNOWN;
357 } else if (ores->true_cnt == 0 && ores->error_cnt == 0 && ores->unknown_cnt == 0 && ores->noteval_cnt > 0) {
358 result = OVAL_RESULT_NOT_EVALUATED;
359 }
360 break;
361 case OVAL_OPERATOR_XOR:
362 if ((ores->true_cnt % 2) == 1 && ores->error_cnt == 0 && ores->unknown_cnt == 0 && ores->noteval_cnt == 0) {
363 result = OVAL_RESULT_TRUE;
364 } else if ((ores->true_cnt % 2) == 0 &&
365 ores->error_cnt == 0 && ores->unknown_cnt == 0 && ores->noteval_cnt == 0) {
366 result = OVAL_RESULT_FALSE;
367 } else if (ores->error_cnt > 0) {
368 result = OVAL_RESULT_ERROR;
369 } else if (ores->error_cnt == 0 && ores->unknown_cnt > 0) {
370 result = OVAL_RESULT_UNKNOWN;
371 } else if (ores->error_cnt == 0 && ores->unknown_cnt == 0 && ores->noteval_cnt > 0) {
372 result = OVAL_RESULT_NOT_EVALUATED;
373 }
374 break;
375 default:
376 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Invalid operator value: %d.", op);
377 result = OVAL_RESULT_ERROR;
378 break;
379 }
380
381 return result;
382 }
383
384 static inline oval_result_t _evaluate_sysent_with_variable(struct oval_syschar_model *syschar_model, struct oval_variable *state_entity_var, const char *sys_data, oval_operation_t state_entity_operation, oval_check_t var_check)
385 {
386 oval_syschar_collection_flag_t flag;
387 oval_result_t ent_val_res;
388
389 if (0 != oval_syschar_model_compute_variable(syschar_model, state_entity_var)) {
390 return -1;
391 }
392
393 flag = oval_variable_get_collection_flag(state_entity_var);
394 switch (flag) {
395 case SYSCHAR_FLAG_COMPLETE:
396 case SYSCHAR_FLAG_INCOMPLETE:{
397 struct oresults var_ores;
398 struct oval_value_iterator *val_itr;
399
400 ores_clear(&var_ores);
401
402 val_itr = oval_variable_get_values(state_entity_var);
403 while (oval_value_iterator_has_more(val_itr)) {
404 struct oval_value *var_val;
405 char *state_entity_val_text = NULL;
406 oval_result_t var_val_res;
407
408 var_val = oval_value_iterator_next(val_itr);
409 state_entity_val_text = oval_value_get_text(var_val);
410 if (state_entity_val_text == NULL) {
411 dE("Found NULL variable value text.");
412 ores_add_res(&var_ores, OVAL_RESULT_ERROR);
413 break;
414 }
415 oval_datatype_t state_entity_val_datatype = oval_value_get_datatype(var_val);
416
417 var_val_res = oval_str_cmp_str(state_entity_val_text, state_entity_val_datatype, sys_data, state_entity_operation);
418 if (var_val_res == OVAL_RESULT_ERROR) {
419 dW("Can't compare variable '%s' value = '%s' with collected item entity = '%s'",
420 oval_variable_get_id(state_entity_var), state_entity_val_text, sys_data);
421 }
422 ores_add_res(&var_ores, var_val_res);
423 }
424 oval_value_iterator_free(val_itr);
425
426 ent_val_res = ores_get_result_bychk(&var_ores, var_check);
427 } break;
428 case SYSCHAR_FLAG_ERROR:
429 case SYSCHAR_FLAG_DOES_NOT_EXIST:
430 case SYSCHAR_FLAG_NOT_COLLECTED:
431 case SYSCHAR_FLAG_NOT_APPLICABLE:
432 ent_val_res = OVAL_RESULT_ERROR;
433 break;
434 default:
435 ent_val_res = -1;
436 }
437
438 return ent_val_res;
439 }
440
441 struct record_field_instance {
442 char *name;
443 char *value;
444 oval_datatype_t data_type;
445 oval_check_t ent_check;
446 oval_operation_t operation;
447 struct oval_variable *var;
448 oval_check_t var_check;
449 };
450
451 static struct record_field_instance _oval_record_field_iterator_next_instance(struct oval_record_field_iterator *iterator)
452 {
453 struct record_field_instance instance;
454 struct oval_record_field *rf = oval_record_field_iterator_next(iterator);
455 instance.name = oval_record_field_get_name(rf);
456 instance.value = oval_record_field_get_value(rf);
457 instance.data_type = oval_record_field_get_datatype(rf);
458 if (oval_record_field_get_type(rf) == OVAL_RECORD_FIELD_STATE) {
459 instance.ent_check = oval_record_field_get_ent_check(rf);
460 instance.operation = oval_record_field_get_operation(rf);
461 instance.var = oval_record_field_get_variable(rf);
462 instance.var_check = oval_record_field_get_var_check(rf);
463 }
464 return instance;
465 }
466
467 static oval_result_t _evaluate_sysent_record(struct oval_syschar_model *syschar_model, struct oval_state_content *state_content, struct oval_sysent *item_entity)
468 {
469 struct oresults record_ores;
470 ores_clear(&record_ores);
471 /* During analysis of a system characteristics item, each record field is
472 * analyzed and then the overall result for elements of the record type is
473 * computed by logically ANDing the results for each field and applying
474 * the entity_check attribute.
475 */
476 struct oval_record_field_iterator *state_it = oval_state_content_get_record_fields(state_content);
477 while (oval_record_field_iterator_has_more(state_it)) {
478 struct record_field_instance state_rf = _oval_record_field_iterator_next_instance(state_it);
479 bool field_found = false;
480 struct oresults field_ores;
481 ores_clear(&field_ores);
482 struct oval_record_field_iterator *item_it = oval_sysent_get_record_fields(item_entity);
483 while (oval_record_field_iterator_has_more(item_it)) {
484 struct record_field_instance item_rf = _oval_record_field_iterator_next_instance(item_it);
485 if (strcmp(state_rf.name, item_rf.name) == 0) {
486 field_found = true;
487 oval_result_t fields_comparison_result;
488 if (state_rf.var != NULL) {
489 fields_comparison_result = _evaluate_sysent_with_variable(syschar_model, state_rf.var, item_rf.value, state_rf.operation, state_rf.var_check);
490 } else {
491 fields_comparison_result = oval_str_cmp_str(state_rf.value, state_rf.data_type, item_rf.value, state_rf.operation);
492 }
493 ores_add_res(&field_ores, fields_comparison_result);
494 }
495 }
496 oval_record_field_iterator_free(item_it);
497 /* When analyzing system characteristics an error should be reported
498 * for the result of a field that is present in the OVAL State, but
499 * not found in the system characteristics item.
500 */
501 if (!field_found) {
502 ores_add_res(&record_ores, OVAL_RESULT_ERROR);
503 } else {
504 oval_result_t field_result = ores_get_result_bychk(&field_ores, state_rf.ent_check);
505 ores_add_res(&record_ores, field_result);
506 }
507 }
508 oval_record_field_iterator_free(state_it);
509 return ores_get_result_byopr(&record_ores, OVAL_OPERATOR_AND);
510 }
511
512 static inline oval_result_t _evaluate_sysent(struct oval_syschar_model *syschar_model, struct oval_sysent *item_entity, struct oval_entity *state_entity, oval_operation_t state_entity_operation, struct oval_state_content *content)
513 {
514 if (oval_sysent_get_status(item_entity) == SYSCHAR_STATUS_DOES_NOT_EXIST) {
515 return OVAL_RESULT_FALSE;
516 } else if (oval_entity_get_varref_type(state_entity) == OVAL_ENTITY_VARREF_ATTRIBUTE) {
517 struct oval_variable *state_entity_var;
518 if ((state_entity_var = oval_entity_get_variable(state_entity)) == NULL) {
519 oscap_seterr(OSCAP_EFAMILY_OVAL, "OVAL internal error: found NULL variable");
520 return -1;
521 }
522 const char *sys_data = oval_sysent_get_value(item_entity);
523 oval_check_t var_check = oval_state_content_get_var_check(content);
524
525 return _evaluate_sysent_with_variable(syschar_model,
526 state_entity_var, sys_data,
527 state_entity_operation, var_check);
528 } else {
529 struct oval_value *state_entity_val;
530 char *state_entity_val_text;
531 oval_datatype_t state_entity_val_datatype;
532
533 oval_datatype_t state_entity_type = oval_entity_get_datatype(state_entity);
534 if (state_entity_type == OVAL_DATATYPE_RECORD) {
535 if (state_entity_operation != OVAL_OPERATION_EQUALS) {
536 dE("The only allowed operation for comparing record types is 'equals'.");
537 return OVAL_RESULT_ERROR;
538 }
539 return _evaluate_sysent_record(syschar_model, content, item_entity);
540 } else {
541 if ((state_entity_val = oval_entity_get_value(state_entity)) == NULL) {
542 oscap_seterr(OSCAP_EFAMILY_OVAL, "OVAL internal error: found NULL entity value");
543 return -1;
544 }
545 if ((state_entity_val_text = oval_value_get_text(state_entity_val)) == NULL) {
546 oscap_seterr(OSCAP_EFAMILY_OVAL, "OVAL internal error: found NULL entity value text");
547 return -1;
548 }
549 state_entity_val_datatype = oval_value_get_datatype(state_entity_val);
550
551 const char *sys_data = oval_sysent_get_value(item_entity);
552 return oval_str_cmp_str(state_entity_val_text, state_entity_val_datatype, sys_data, state_entity_operation);
553 }
554 }
555 }
556
557 static oval_result_t eval_item(struct oval_syschar_model *syschar_model, struct oval_sysitem *cur_sysitem, struct oval_state *state)
558 {
559 struct oval_state_content_iterator *state_contents_itr;
560 struct oresults ste_ores;
561 oval_operator_t operator;
562 oval_result_t result = OVAL_RESULT_ERROR;
563
564 ores_clear(&ste_ores);
565
566 state_contents_itr = oval_state_get_contents(state);
567 while (oval_state_content_iterator_has_more(state_contents_itr)) {
568 struct oval_state_content *content;
569 struct oval_entity *state_entity;
570 char *state_entity_name;
571 oval_operation_t state_entity_operation;
572 oval_check_t entity_check;
573 oval_existence_t check_existence;
574 oval_result_t ste_ent_res;
575 struct oval_sysent_iterator *item_entities_itr;
576 struct oresults ent_ores;
577 struct oval_status_counter counter;
578 bool found_matching_item;
579
580 if ((content = oval_state_content_iterator_next(state_contents_itr)) == NULL) {
581 oscap_seterr(OSCAP_EFAMILY_OVAL, "OVAL internal error: found NULL state content");
582 goto fail;
583 }
584 if ((state_entity = oval_state_content_get_entity(content)) == NULL) {
585 oscap_seterr(OSCAP_EFAMILY_OVAL, "OVAL internal error: found NULL entity");
586 goto fail;
587 }
588 if ((state_entity_name = oval_entity_get_name(state_entity)) == NULL) {
589 oscap_seterr(OSCAP_EFAMILY_OVAL, "OVAL internal error: found NULL entity name");
590 goto fail;
591 }
592
593 if (oscap_streq(state_entity_name, "line") &&
594 oval_state_get_subtype(state) == (oval_subtype_t) OVAL_INDEPENDENT_TEXT_FILE_CONTENT) {
595 /* Hack: textfilecontent_state/line shall be compared against textfilecontent_item/text.
596 *
597 * textfilecontent_test and textfilecontent54_test share the same syschar
598 * (textfilecontent_item). In OVAL 5.3 and below this syschar did not hold any usable
599 * information ('text' ent). In OVAL 5.4 textfilecontent_test was deprecated. But the
600 * 'text' ent has been added to textfilecontent_item, making it potentially usable. */
601 oval_schema_version_t over = oval_state_get_platform_schema_version(state);
602 if (oval_schema_version_cmp(over, OVAL_SCHEMA_VERSION(5.4)) >= 0) {
603 /* The OVAL-5.3 does not have textfilecontent_item/text */
604 state_entity_name = "text";
605 }
606 }
607
608 entity_check = oval_state_content_get_ent_check(content);
609 check_existence = oval_state_content_get_check_existence(content);
610 state_entity_operation = oval_entity_get_operation(state_entity);
611
612 ores_clear(&ent_ores);
613 found_matching_item = false;
614 oval_status_counter_clear(&counter);
615
616 item_entities_itr = oval_sysitem_get_sysents(cur_sysitem);
617 while (oval_sysent_iterator_has_more(item_entities_itr)) {
618 struct oval_sysent *item_entity;
619 oval_result_t ent_val_res;
620 char *item_entity_name;
621 oval_syschar_status_t item_status;
622
623 item_entity = oval_sysent_iterator_next(item_entities_itr);
624 if (item_entity == NULL) {
625 oscap_seterr(OSCAP_EFAMILY_OVAL, "OVAL internal error: found NULL sysent");
626 oval_sysent_iterator_free(item_entities_itr);
627 goto fail;
628 }
629 item_status = oval_sysent_get_status(item_entity);
630 oval_status_counter_add_status(&counter, item_status);
631
632 item_entity_name = oval_sysent_get_name(item_entity);
633 if (strcmp(item_entity_name, state_entity_name))
634 continue;
635
636 found_matching_item = true;
637
638 /* copy mask attribute from state to item */
639 if (oval_entity_get_mask(state_entity))
640 oval_sysent_set_mask(item_entity,1);
641
642 ent_val_res = _evaluate_sysent(syschar_model, item_entity, state_entity,
643 state_entity_operation, content);
644 if (ent_val_res == OVAL_RESULT_TRUE) {
645 dI("Entity '%s'='%s' of item '%s' matches corresponding entity in state '%s'.",
646 oval_sysent_get_name(item_entity),
647 oval_sysent_get_value(item_entity),
648 oval_sysitem_get_id(cur_sysitem), oval_state_get_id(state));
649 }
650 if (ent_val_res == OVAL_RESULT_ERROR) {
651 dI("Comparing entity '%s'='%s' of item '%s' to corresponding entity in state '%s' was not successful.",
652 oval_sysent_get_name(item_entity),
653 oval_sysent_get_value(item_entity),
654 oval_sysitem_get_id(cur_sysitem), oval_state_get_id(state));
655 }
656 if (((signed) ent_val_res) == -1) {
657 oval_sysent_iterator_free(item_entities_itr);
658 goto fail;
659 }
660
661 ores_add_res(&ent_ores, ent_val_res);
662 }
663 oval_sysent_iterator_free(item_entities_itr);
664
665 if (!found_matching_item)
666 dW("Entity name '%s' from state (id: '%s') not found in item (id: '%s').",
667 state_entity_name, oval_state_get_id(state), oval_sysitem_get_id(cur_sysitem));
668
669 oval_result_t cres = oval_status_counter_get_result(&counter, check_existence);
670 /* The entity check results are only relevant when the check existence is satisfied */
671 if (cres == OVAL_RESULT_TRUE) {
672 ste_ent_res = ores_get_result_bychk(&ent_ores, entity_check);
673 ores_add_res(&ste_ores, ste_ent_res);
674 } else {
675 ores_add_res(&ste_ores, cres);
676 }
677 }
678 oval_state_content_iterator_free(state_contents_itr);
679
680 operator = oval_state_get_operator(state);
681 result = ores_get_result_byopr(&ste_ores, operator);
682 dI("Item '%s' compared to state '%s' with result %s.",
683 oval_sysitem_get_id(cur_sysitem), oval_state_get_id(state),
684 oval_result_get_text(result));
685
686 return result;
687
688 fail:
689 oval_state_content_iterator_free(state_contents_itr);
690
691 return OVAL_RESULT_ERROR;
692 }
693
694 #define ITEMMAP (struct oval_string_map *)args[2]
695 #define TEST (struct oval_result_test *)args[1]
696 #define SYSTEM (struct oval_result_system *)args[0]
697
698 static void _oval_test_item_consumer(struct oval_result_item *item, void **args) {
699 struct oval_sysitem *oval_sysitem = oval_result_item_get_sysitem(item);
700 char *item_id = oval_sysitem_get_id(oval_sysitem);
701 struct oval_result_item *mapped_item = oval_string_map_get_value(ITEMMAP, item_id);
702 if (mapped_item == NULL) {
703 oval_string_map_put(ITEMMAP, item_id, item);
704 oval_result_test_add_item(TEST, item);
705 } else {
706 oval_result_item_free(item);
707 }
708 }
709
710 static oval_result_t eval_check_state(struct oval_test *test, void **args)
711 {
712 struct oval_syschar_model *syschar_model;
713 struct oval_result_item_iterator *ritems_itr;
714 struct oresults item_ores;
715 oval_result_t result;
716 oval_check_t ste_check;
717 oval_operator_t ste_opr;
718
719 ste_check = oval_test_get_check(test);
720 ste_opr = oval_test_get_state_operator(test);
721 syschar_model = oval_result_system_get_syschar_model(SYSTEM);
722 ores_clear(&item_ores);
723
724 char *state_names = oval_test_get_state_names(test);
725 if (state_names) {
726 dI("In test '%s' %s of the collected items must satisfy these states: %s.",
727 oval_test_get_id(test), oval_check_get_description(ste_check), state_names);
728 free(state_names);
729 }
730
731 ritems_itr = oval_result_test_get_items(TEST);
732 while (oval_result_item_iterator_has_more(ritems_itr)) {
733 struct oval_result_item *ritem;
734 struct oval_sysitem *item;
735 oval_syschar_status_t item_status;
736 struct oresults ste_ores;
737 struct oval_state_iterator *ste_itr;
738 oval_result_t item_res;
739
740 ritem = oval_result_item_iterator_next(ritems_itr);
741 item = oval_result_item_get_sysitem(ritem);
742
743 item_status = oval_sysitem_get_status(item);
744 switch (item_status) {
745 case SYSCHAR_STATUS_ERROR:
746 case SYSCHAR_STATUS_NOT_COLLECTED:
747 item_res = OVAL_RESULT_ERROR;
748 ores_add_res(&item_ores, item_res);
749 oval_result_item_set_result(ritem, item_res);
750 continue;
751 case SYSCHAR_STATUS_DOES_NOT_EXIST:
752 item_res = OVAL_RESULT_FALSE;
753 ores_add_res(&item_ores, item_res);
754 oval_result_item_set_result(ritem, item_res);
755 continue;
756 default:
757 break;
758 }
759
760 ores_clear(&ste_ores);
761
762 ste_itr = oval_test_get_states(test);
763 while (oval_state_iterator_has_more(ste_itr)) {
764 struct oval_state *ste;
765 oval_result_t ste_res;
766
767 ste = oval_state_iterator_next(ste_itr);
768 ste_res = eval_item(syschar_model, item, ste);
769 ores_add_res(&ste_ores, ste_res);
770 }
771 oval_state_iterator_free(ste_itr);
772
773 item_res = ores_get_result_byopr(&ste_ores, ste_opr);
774 ores_add_res(&item_ores, item_res);
775 oval_result_item_set_result(ritem, item_res);
776 }
777 oval_result_item_iterator_free(ritems_itr);
778
779 result = ores_get_result_bychk(&item_ores, ste_check);
780
781 return result;
782 }
783
784 static oval_result_t eval_check_existence(oval_existence_t check_existence, int exists_cnt, int error_cnt)
785 {
786 oval_result_t result = OVAL_RESULT_ERROR;
787
788 switch (check_existence) {
789 case OVAL_ALL_EXIST:
790 if (exists_cnt >= 1 && error_cnt == 0) {
791 result = OVAL_RESULT_TRUE;
792 } else if (exists_cnt == 0 && error_cnt == 0) {
793 result = OVAL_RESULT_FALSE;
794 } else if (exists_cnt >= 0 && error_cnt >= 1) {
795 result = OVAL_RESULT_ERROR;
796 }
797 break;
798 case OVAL_ANY_EXIST:
799 if (exists_cnt >= 0 && error_cnt == 0) {
800 result = OVAL_RESULT_TRUE;
801 } else if (exists_cnt >= 1 && error_cnt >= 1) {
802 result = OVAL_RESULT_TRUE;
803 } else if (exists_cnt == 0 && error_cnt >= 1) {
804 result = OVAL_RESULT_ERROR;
805 }
806 break;
807 case OVAL_AT_LEAST_ONE_EXISTS:
808 if (exists_cnt >= 1 && error_cnt >= 0) {
809 result = OVAL_RESULT_TRUE;
810 } else if (exists_cnt == 0 && error_cnt == 0) {
811 result = OVAL_RESULT_FALSE;
812 } else if (exists_cnt == 0 && error_cnt >= 1) {
813 result = OVAL_RESULT_ERROR;
814 }
815 break;
816 case OVAL_NONE_EXIST:
817 if (exists_cnt == 0 && error_cnt == 0) {
818 result = OVAL_RESULT_TRUE;
819 } else if (exists_cnt >= 1 && error_cnt == 0) {
820 result = OVAL_RESULT_FALSE;
821 } else if (exists_cnt == 0 && error_cnt >= 1) {
822 result = OVAL_RESULT_ERROR;
823 }
824 break;
825 case OVAL_ONLY_ONE_EXISTS:
826 if (exists_cnt == 1 && error_cnt == 0) {
827 result = OVAL_RESULT_TRUE;
828 } else if (exists_cnt >= 2 && error_cnt >= 0) {
829 result = OVAL_RESULT_FALSE;
830 } else if (exists_cnt == 0 && error_cnt == 0) {
831 result = OVAL_RESULT_FALSE;
832 } else if (exists_cnt == 0 && error_cnt >= 1) {
833 result = OVAL_RESULT_ERROR;
834 } else if (exists_cnt == 1 && error_cnt >= 1) {
835 result = OVAL_RESULT_ERROR;
836 }
837 break;
838 default:
839 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Invalid check_existence value: %d.", check_existence);
840 result = OVAL_RESULT_ERROR;
841 break;
842 }
843
844 return result;
845 }
846
847 static oval_result_t
848 _oval_result_test_evaluate_items(struct oval_test *test, struct oval_syschar *syschar_object, void **args)
849 {
850 struct oval_sysitem_iterator *collected_items_itr;
851 oval_result_t result;
852 int exists_cnt, error_cnt;
853 bool hasstate;
854 const char *test_id, *object_id, *flag_text;
855 oval_check_t test_check;
856 oval_existence_t test_check_existence;
857 struct oval_state_iterator *ste_itr;
858 oval_syschar_collection_flag_t flag;
859 struct oval_object *object;
860
861 exists_cnt = error_cnt = 0;
862 test_id = oval_test_get_id(test);
863 collected_items_itr = oval_syschar_get_sysitem(syschar_object);
864 while (oval_sysitem_iterator_has_more(collected_items_itr)) {
865 struct oval_sysitem *item;
866 char *item_id;
867 oval_syschar_status_t item_status;
868 struct oval_result_item *ritem;
869
870 item = oval_sysitem_iterator_next(collected_items_itr);
871 if (item == NULL) {
872 oscap_seterr(OSCAP_EFAMILY_OVAL, "Iterator returned null.");
873 oval_sysitem_iterator_free(collected_items_itr);
874 return OVAL_RESULT_ERROR;
875 }
876
877 item_status = oval_sysitem_get_status(item);
878 if (item_status == SYSCHAR_STATUS_EXISTS)
879 exists_cnt++;
880 if (item_status == SYSCHAR_STATUS_ERROR)
881 error_cnt++;
882
883 item_id = oval_sysitem_get_id(item);
884 ritem = oval_result_item_new(SYSTEM, item_id);
885 oval_result_item_set_result(ritem, OVAL_RESULT_NOT_EVALUATED);
886 _oval_test_item_consumer(ritem, args);
887 }
888 oval_sysitem_iterator_free(collected_items_itr);
889
890 test_check = oval_test_get_check(test);
891 test_check_existence = oval_test_get_existence(test);
892 ste_itr = oval_test_get_states(test);
893 hasstate = oval_state_iterator_has_more(ste_itr);
894 oval_state_iterator_free(ste_itr);
895 object = oval_syschar_get_object(syschar_object);
896 object_id = object ? oval_object_get_id(object) : "<UNKNOWN>";
897
898 switch (test_check_existence) {
899 case OVAL_ALL_EXIST:
900 dI("Test '%s' requires that every object defined by '%s' exists on the system.", test_id, object_id);
901 break;
902 case OVAL_ANY_EXIST:
903 dI("Test '%s' requires that zero or more objects defined by '%s' exist on the system.", test_id, object_id);
904 break;
905 case OVAL_AT_LEAST_ONE_EXISTS:
906 dI("Test '%s' requires that at least one object defined by '%s' exists on the system.", test_id, object_id);
907 break;
908 case OVAL_NONE_EXIST:
909 dI("Test '%s' requires that none of the objects defined by '%s' exist on the system.", test_id, object_id);
910 break;
911 case OVAL_ONLY_ONE_EXISTS:
912 dI("Test '%s' requires that only one object defined by '%s' exists on the system.", test_id, object_id);
913 break;
914 default:
915 oscap_seterr(OSCAP_EFAMILY_OVAL, "Check_existence parameter of test '%s' is unknown. This may indicate a bug in OpenSCAP.", test_id);
916 }
917
918 dI("%d objects defined by '%s' exist on the system.", exists_cnt, object_id);
919 if (!hasstate) {
920 dI("Test '%s' does not contain any state to compare object with.", test_id);
921 }
922 flag = oval_syschar_get_flag(syschar_object);
923 flag_text = oval_syschar_collection_flag_get_text(flag);
924
925 switch (flag) {
926 case SYSCHAR_FLAG_ERROR:
927 dI("An error occurred while collecting items matching object '%s'. (flag=%s)", object_id, flag_text);
928 if (test_check_existence == OVAL_ANY_EXIST
929 && !hasstate) {
930 result = OVAL_RESULT_TRUE;
931 } else {
932 result = OVAL_RESULT_ERROR;
933 }
934 break;
935 case SYSCHAR_FLAG_NOT_COLLECTED:
936 dI("No attempt was made to collect items matching object '%s'. (flag=%s)", object_id, flag_text);
937 if (test_check_existence == OVAL_ANY_EXIST
938 && !hasstate) {
939 result = OVAL_RESULT_TRUE;
940 } else {
941 result = OVAL_RESULT_UNKNOWN;
942 }
943 break;
944 case SYSCHAR_FLAG_NOT_APPLICABLE:
945 dI("Object '%s' is not applicable to the system. (flag=%s)", object_id, flag_text);
946 if (test_check_existence == OVAL_ANY_EXIST
947 && !hasstate) {
948 result = OVAL_RESULT_TRUE;
949 } else {
950 result = OVAL_RESULT_NOT_APPLICABLE;
951 }
952 break;
953 case SYSCHAR_FLAG_DOES_NOT_EXIST:
954 dI("No item matching object '%s' was found on the system. (flag=%s)", object_id, flag_text);
955 if (test_check_existence == OVAL_NONE_EXIST
956 || test_check_existence == OVAL_ANY_EXIST) {
957 result = OVAL_RESULT_TRUE;
958 } else {
959 result = OVAL_RESULT_FALSE;
960 }
961 break;
962 case SYSCHAR_FLAG_COMPLETE:
963 dI("All items matching object '%s' were collected. (flag=%s)", object_id, flag_text);
964 result = eval_check_existence(test_check_existence, exists_cnt, error_cnt);
965 if (result == OVAL_RESULT_TRUE
966 && hasstate) {
967 result = eval_check_state(test, args);
968 }
969 break;
970 case SYSCHAR_FLAG_INCOMPLETE:
971 dI("Only some of items matching object '%s' have been collected from the system. It is unknown if other matching items also exist. (flag=%s)", object_id, flag_text);
972 if (test_check_existence == OVAL_ANY_EXIST) {
973 result = OVAL_RESULT_TRUE;
974 } else if (test_check_existence == OVAL_AT_LEAST_ONE_EXISTS
975 && exists_cnt > 0) {
976 result = OVAL_RESULT_TRUE;
977 } else if (test_check_existence == OVAL_NONE_EXIST
978 && exists_cnt > 0) {
979 result = OVAL_RESULT_FALSE;
980 } else if (test_check_existence == OVAL_ONLY_ONE_EXISTS
981 && exists_cnt > 1) {
982 result = OVAL_RESULT_FALSE;
983 } else {
984 result = OVAL_RESULT_UNKNOWN;
985 }
986
987 if (result == OVAL_RESULT_TRUE
988 && hasstate) {
989 result = eval_check_state(test, args);
990 if (result == OVAL_RESULT_TRUE) {
991 if (test_check != OVAL_CHECK_AT_LEAST_ONE) {
992 result = OVAL_RESULT_UNKNOWN;
993 }
994 } else if (result != OVAL_RESULT_FALSE) {
995 result = OVAL_RESULT_UNKNOWN;
996 }
997 }
998 break;
999 default: {
1000 oscap_seterr(OSCAP_EFAMILY_OVAL, "Item corresponding to object '%s' from test '%s' has an unknown flag. This may indicate a bug in OpenSCAP.",
1001 object_id, test_id);
1002 return OVAL_RESULT_ERROR;
1003 }
1004 }
1005
1006 return result;
1007 }
1008
1009 /* this function will gather all the necessary ingredients and call 'evaluate_items' when it finds them */
1010 static oval_result_t _oval_result_test_result(struct oval_result_test *rtest, void **args)
1011 {
1012 __attribute__nonnull__(rtest);
1013
1014 /* is the test already evaluated? */
1015 if (rtest->result != OVAL_RESULT_NOT_EVALUATED) {
1016 dI("Found result from previous evaluation: %d, returning without further processing.", rtest->result);
1017 return (rtest->result);
1018 }
1019
1020 #if defined(OVAL_PROBES_ENABLED)
1021 /* get syschar of rtest */
1022 struct oval_test *test = oval_result_test_get_test(rtest);
1023 struct oval_object * object = oval_test_get_object(test);
1024 char * object_id = oval_object_get_id(object);
1025
1026 struct oval_result_system *sys = oval_result_test_get_system(rtest);
1027 struct oval_results_model *results_model = oval_result_system_get_results_model(sys);
1028 struct oval_probe_session *probe_session = oval_results_model_get_probe_session(results_model);
1029 if (probe_session != NULL) {
1030 /* probe test */
1031 int ret = oval_probe_query_test(probe_session, test);
1032 if (ret != 0) {
1033 return ret;
1034 }
1035 }
1036
1037 struct oval_syschar_model *syschar_model = oval_result_system_get_syschar_model(sys);
1038
1039 struct oval_syschar * syschar = oval_syschar_model_get_syschar(syschar_model, object_id);
1040 if (syschar == NULL) {
1041 dW("No syschar for object: %s", object_id);
1042 return OVAL_RESULT_UNKNOWN;
1043 }
1044
1045 /* evaluate items */
1046 oval_result_t result = _oval_result_test_evaluate_items(test, syschar, args);
1047
1048 return result;
1049 #else
1050 return OVAL_RESULT_UNKNOWN;
1051 #endif
1052 }
1053
1054 static void _oval_result_test_initialize_bindings(struct oval_result_test *rslt_test)
1055 {
1056 __attribute__nonnull__(rslt_test);
1057
1058 struct oval_test *oval_test = oval_result_test_get_test(rslt_test);
1059 struct oval_string_map *vm;
1060 struct oval_state_iterator *ste_itr;
1061 struct oval_iterator *var_itr;
1062
1063 vm = oval_string_map_new();
1064
1065 /* Gather bindings pertaining to the referenced states */
1066 /* TODO: cache bindings collected for each state */
1067 ste_itr = oval_test_get_states(oval_test);
1068 while (oval_state_iterator_has_more(ste_itr)) {
1069 struct oval_state *ste;
1070
1071 ste = oval_state_iterator_next(ste_itr);
1072 oval_ste_collect_var_refs(ste, vm);
1073 }
1074 oval_state_iterator_free(ste_itr);
1075
1076 var_itr = oval_string_map_values(vm);
1077 while (oval_collection_iterator_has_more(var_itr)) {
1078 struct oval_variable *var;
1079 struct oval_value_iterator *val_itr;
1080 struct oval_variable_binding *binding;
1081
1082 var = oval_collection_iterator_next(var_itr);
1083 binding = oval_variable_binding_new(var, NULL);
1084
1085 val_itr = oval_variable_get_values(var);
1086 while (oval_value_iterator_has_more(val_itr)) {
1087 struct oval_value *val;
1088 char *txt;
1089
1090 val = oval_value_iterator_next(val_itr);
1091 txt = oval_value_get_text(val);
1092 txt = oscap_strdup(txt);
1093 oval_variable_binding_add_value(binding, txt);
1094 }
1095 oval_value_iterator_free(val_itr);
1096 oval_result_test_add_binding(rslt_test, binding);
1097 }
1098 oval_collection_iterator_free(var_itr);
1099
1100 /* Gather bindings pertaining to the collected object */
1101 struct oval_object *oval_object = oval_test_get_object(oval_test);
1102 if (oval_object) {
1103 char *object_id = oval_object_get_id(oval_object);
1104 struct oval_result_system *sys = oval_result_test_get_system(rslt_test);
1105 struct oval_syschar_model *syschar_model = oval_result_system_get_syschar_model(sys);
1106 struct oval_syschar *syschar = oval_syschar_model_get_syschar(syschar_model, object_id);
1107 /* no syschar if system characteristics was a subset of definitions */
1108 if(syschar) {
1109 struct oval_variable_binding_iterator *bindings = oval_syschar_get_variable_bindings(syschar);
1110 while (oval_variable_binding_iterator_has_more(bindings)) {
1111 struct oval_variable *var;
1112 char *var_id;
1113 struct oval_variable_binding *binding = oval_variable_binding_iterator_next(bindings);
1114
1115 var = oval_variable_binding_get_variable(binding);
1116 var_id = oval_variable_get_id(var);
1117 /* Don't add bindings that were already
1118 * collected from states. Assumtion is made
1119 * that object's own bindings don't contain
1120 * duplicates.
1121 */
1122 if (oval_string_map_get_value(vm, var_id) == NULL) {
1123 struct oval_definition_model *definition_model = oval_syschar_model_get_definition_model(syschar_model);
1124 struct oval_variable_binding *binding_copy = oval_variable_binding_clone(binding, definition_model);
1125 oval_result_test_add_binding(rslt_test, binding_copy);
1126 }
1127 }
1128 oval_variable_binding_iterator_free(bindings);
1129 }
1130 }
1131
1132 oval_string_map_free(vm, NULL);
1133
1134 rslt_test->bindings_initialized = true;
1135 }
1136
1137 oval_result_t oval_result_test_eval(struct oval_result_test *rtest)
1138 {
1139 __attribute__nonnull__(rtest);
1140
1141 struct oval_test *test = oval_result_test_get_test(rtest);
1142 const char *type = oval_subtype_get_text(oval_test_get_subtype(test));
1143 const char *test_id = oval_test_get_id(test);
1144 const char *comment = oval_test_get_comment(test);
1145 dI("Evaluating %s test '%s': %s.", type, test_id, comment);
1146
1147 if (rtest->result == OVAL_RESULT_NOT_EVALUATED) {
1148 if (oval_test_get_subtype(oval_result_test_get_test(rtest)) != OVAL_INDEPENDENT_UNKNOWN) {
1149 struct oval_string_map *tmp_map = oval_string_map_new();
1150 void *args[] = { rtest->system, rtest, tmp_map };
1151 dIndent(1);
1152 rtest->result = _oval_result_test_result(rtest, args);
1153 dIndent(-1);
1154 oval_string_map_free(tmp_map, NULL);
1155
1156 if (!rtest->bindings_initialized) {
1157 _oval_result_test_initialize_bindings(rtest);
1158 }
1159 }
1160 else
1161 rtest->result = OVAL_RESULT_UNKNOWN;
1162 }
1163
1164 dI("Test '%s' evaluated as %s.", test_id, oval_result_get_text(rtest->result));
1165
1166 return rtest->result;
1167 }
1168
1169 oval_result_t oval_result_test_get_result(struct oval_result_test * rtest)
1170 {
1171 __attribute__nonnull__(rtest);
1172
1173 return rtest->result;
1174 }
1175
1176 int oval_result_test_get_instance(struct oval_result_test *rtest)
1177 {
1178 __attribute__nonnull__(rtest);
1179
1180 return rtest->instance;
1181 }
1182
1183 struct oval_message_iterator *oval_result_test_get_messages(struct oval_result_test *rtest)
1184 {
1185 __attribute__nonnull__(rtest);
1186
1187 return (struct oval_message_iterator *) oval_collection_iterator(rtest->messages);
1188 }
1189
1190 struct oval_result_item_iterator *oval_result_test_get_items(struct oval_result_test *rtest)
1191 {
1192 __attribute__nonnull__(rtest);
1193
1194 return (struct oval_result_item_iterator *)
1195 oval_collection_iterator(rtest->items);
1196 }
1197
1198 struct oval_variable_binding_iterator *oval_result_test_get_bindings(struct oval_result_test
1199 *rtest)
1200 {
1201 __attribute__nonnull__(rtest);
1202
1203 return (struct oval_variable_binding_iterator *)
1204 oval_collection_iterator(rtest->bindings);
1205 }
1206
1207 void oval_result_test_set_result(struct oval_result_test *test, oval_result_t result)
1208 {
1209 __attribute__nonnull__(test);
1210 test->result = result;
1211 }
1212
1213 void oval_result_test_set_instance(struct oval_result_test *test, int instance)
1214 {
1215 __attribute__nonnull__(test);
1216 test->instance = instance;
1217 }
1218
1219 void oval_result_test_add_message(struct oval_result_test *test, struct oval_message *message)
1220 {
1221 __attribute__nonnull__(test);
1222 oval_collection_add(test->messages, message);
1223 }
1224
1225 void oval_result_test_add_item(struct oval_result_test *test, struct oval_result_item *item)
1226 {
1227 __attribute__nonnull__(test);
1228 oval_collection_add(test->items, item);
1229 }
1230
1231 void oval_result_test_add_binding(struct oval_result_test *test, struct oval_variable_binding *binding)
1232 {
1233 __attribute__nonnull__(test);
1234 oval_collection_add(test->bindings, binding);
1235 }
1236
1237 //void(*oscap_consumer_func)(void*, void*);
1238 static void _oval_test_message_consumer(struct oval_message *message, struct oval_result_test *test) {
1239 oval_result_test_add_message(test, message);
1240 }
1241
1242 static int _oval_result_test_binding_parse(xmlTextReaderPtr reader, struct oval_parser_context *context, void **args) {
1243 int return_code = 0;
1244
1245 xmlChar *variable_id = xmlTextReaderGetAttribute(reader, BAD_CAST "variable_id");
1246
1247 struct oval_syschar_model *syschar_model = oval_result_system_get_syschar_model(SYSTEM);
1248 struct oval_definition_model *definition_model = oval_syschar_model_get_definition_model(syschar_model);
1249
1250 struct oval_variable *variable = oval_definition_model_get_new_variable
1251 (definition_model, (char *)variable_id, OVAL_VARIABLE_UNKNOWN);
1252
1253 xmlChar *value = xmlTextReaderValue(reader);
1254
1255 struct oval_variable_binding *binding = oval_variable_binding_new(variable, oscap_strdup((char *) value));
1256 oval_result_test_add_binding(TEST, binding);
1257
1258 xmlFree(value);
1259 xmlFree(variable_id);
1260
1261 return return_code;
1262 }
1263
1264 static int _oval_result_test_parse(xmlTextReaderPtr reader, struct oval_parser_context *context, void **args) {
1265 int return_code = 0;
1266 xmlChar *localName = xmlTextReaderLocalName(reader);
1267
1268 if (strcmp((const char *)localName, "message") == 0) {
1269 return_code = oval_message_parse_tag(reader, context, (oscap_consumer_func) _oval_test_message_consumer, TEST);
1270 } else if (strcmp((const char *)localName, "tested_item") == 0) {
1271 return_code = oval_result_item_parse_tag(reader, context, SYSTEM, (oscap_consumer_func) _oval_test_item_consumer, args);
1272 } else if (strcmp((const char *)localName, "tested_variable") == 0) {
1273 return_code = _oval_result_test_binding_parse(reader, context, args);
1274 } else {
1275 dW("Unhandled tag: <%s>.", localName);
1276 oval_parser_skip_tag(reader, context);
1277 }
1278
1279 free(localName);
1280
1281 return return_code;
1282 }
1283
1284 int oval_result_test_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *usr) {
1285
1286 struct oval_result_system *sys = (struct oval_result_system *) usr;
1287 int return_code = 0;
1288 struct oval_definition_model *dmod;
1289 struct oval_test *dtst;
1290 struct oval_result_test *test;
1291 xmlChar *test_id = xmlTextReaderGetAttribute(reader, BAD_CAST "test_id");
1292
1293 dmod = context->definition_model;
1294 dtst = oval_definition_model_get_new_test(dmod, (char *) test_id);
1295 oval_result_t result = oval_result_parse(reader, "result", 0);
1296 int variable_instance = oval_parser_int_attribute(reader, "variable_instance", 1);
1297
1298 test = oval_result_system_get_new_test(sys, dtst, variable_instance);
1299 if (test == NULL)
1300 return -1;
1301 oval_result_test_set_result(test, result);
1302 oval_result_test_set_instance(test, variable_instance);
1303
1304 struct oval_test *ovaltst = oval_result_test_get_test(test);
1305
1306 oval_existence_t check_existence = oval_existence_parse(reader, "check_existence", OVAL_AT_LEAST_ONE_EXISTS);
1307 oval_existence_t tst_check_existence = oval_test_get_existence(ovaltst);
1308 if (tst_check_existence == OVAL_EXISTENCE_UNKNOWN) {
1309 oval_test_set_existence(ovaltst, check_existence);
1310 } else if (tst_check_existence != check_existence) {
1311 dW("@check_existence does not match, test_id: %s.", test_id);
1312 }
1313
1314 oval_check_t check = oval_check_parse(reader, "check", OVAL_CHECK_UNKNOWN);
1315 oval_check_t tst_check = oval_test_get_check(ovaltst);
1316 if (tst_check == OVAL_CHECK_UNKNOWN) {
1317 oval_test_set_check(ovaltst, check);
1318 } else if (tst_check != check) {
1319 dW("@check does not match, test_id: %s.", test_id);
1320 }
1321
1322 int version = oval_parser_int_attribute(reader, "version", 0);
1323 int tst_version = oval_test_get_version(ovaltst);
1324 if (tst_version == 0) {
1325 oval_test_set_version(ovaltst, version);
1326 } else if (tst_version != version) {
1327 dW("@version does not match, test_id: %s.", test_id);
1328 }
1329
1330 struct oval_string_map *itemmap = oval_string_map_new();
1331 void *args[] = { sys, test, itemmap };
1332 return_code = oval_parser_parse_tag(reader, context, (oval_xml_tag_parser) _oval_result_test_parse, args);
1333 oval_string_map_free(itemmap, NULL);
1334 test->bindings_initialized = true;
1335
1336 free(test_id);
1337 return return_code;
1338 }
1339
1340 static void _oval_result_binding_to_dom(struct oval_variable_binding *binding, xmlDocPtr doc, xmlNode *parent) {
1341 xmlNs *ns_results = xmlSearchNsByHref(doc, parent, OVAL_RESULTS_NAMESPACE);
1342 struct oval_variable *oval_variable = oval_variable_binding_get_variable(binding);
1343 char *variable_id = oval_variable_get_id(oval_variable);
1344 struct oval_string_iterator *str_itr;
1345
1346 str_itr = oval_variable_binding_get_values(binding);
1347 while (oval_string_iterator_has_more(str_itr)) {
1348 char *value;
1349 xmlNode *binding_node;
1350
1351 value = oval_string_iterator_next(str_itr);
1352 binding_node = xmlNewTextChild(parent, ns_results, BAD_CAST "tested_variable", BAD_CAST value);
1353 xmlNewProp(binding_node, BAD_CAST "variable_id", BAD_CAST variable_id);
1354 }
1355 oval_string_iterator_free(str_itr);
1356 }
1357
1358 xmlNode *oval_result_test_to_dom(struct oval_result_test *rslt_test, xmlDocPtr doc, xmlNode * parent) {
1359 __attribute__nonnull__(rslt_test);
1360
1361 xmlNs *ns_results = xmlSearchNsByHref(doc, parent, OVAL_RESULTS_NAMESPACE);
1362 xmlNode *test_node = xmlNewTextChild(parent, ns_results, BAD_CAST "test", NULL);
1363
1364 struct oval_test *oval_test = oval_result_test_get_test(rslt_test);
1365 char *test_id = oval_test_get_id(oval_test);
1366 xmlNewProp(test_node, BAD_CAST "test_id", BAD_CAST test_id);
1367
1368 char version[10];
1369 *version = '\0';
1370 snprintf(version, sizeof(version), "%d", oval_test_get_version(oval_test));
1371 xmlNewProp(test_node, BAD_CAST "version", BAD_CAST version);
1372
1373 oval_existence_t existence = oval_test_get_existence(oval_test);
1374 if (existence != OVAL_AT_LEAST_ONE_EXISTS) {
1375 xmlNewProp(test_node, BAD_CAST "check_existence", BAD_CAST oval_existence_get_text(existence));
1376 }
1377
1378 oval_check_t check = oval_test_get_check(oval_test);
1379 xmlNewProp(test_node, BAD_CAST "check", BAD_CAST oval_check_get_text(check));
1380
1381 int instance_val = oval_result_test_get_instance(rslt_test);
1382 if (instance_val > 1) {
1383 char instance[11];
1384 *instance = '\0';
1385 snprintf(instance, sizeof(instance), "%d", instance_val);
1386 xmlNewProp(test_node, BAD_CAST "variable_instance", BAD_CAST instance);
1387 }
1388
1389 oval_result_t result = oval_result_test_get_result(rslt_test);
1390 xmlNewProp(test_node, BAD_CAST "result", BAD_CAST oval_result_get_text(result));
1391
1392 /* does not make sense to report these when test(definition) is not evaluated */
1393 if( result != OVAL_RESULT_NOT_EVALUATED) {
1394 struct oval_result_item_iterator *items = oval_result_test_get_items(rslt_test);
1395 while (oval_result_item_iterator_has_more(items)) {
1396 struct oval_result_item *item = oval_result_item_iterator_next(items);
1397 oval_result_item_to_dom(item, doc, test_node);
1398 }
1399 oval_result_item_iterator_free(items);
1400
1401 struct oval_variable_binding_iterator *bindings = oval_result_test_get_bindings(rslt_test);
1402 while (oval_variable_binding_iterator_has_more(bindings)) {
1403 struct oval_variable_binding *binding = oval_variable_binding_iterator_next(bindings);
1404 _oval_result_binding_to_dom(binding, doc, test_node);
1405 }
1406 oval_variable_binding_iterator_free(bindings);
1407 }
1408
1409 return test_node;
1410 }
1411
1412 const char *oval_result_test_get_id(const struct oval_result_test *rslt_test)
1413 {
1414 __attribute__nonnull__(rslt_test);
1415 struct oval_test *test = oval_result_test_get_test((struct oval_result_test *) rslt_test);
1416 return (test == NULL) ? NULL : oval_test_get_id(test);
1417 }