Debian Codegraph @

src/OVAL/oval_component.c

raw symbols SCIP
1 /**
2 * @file oval_component.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 * "Peter Vrabec" <[email protected]>
29 * "Tomas Heinrich" <[email protected]>
30 * "Daniel Kopecek" <[email protected]>
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <time.h>
41 #include <stdbool.h>
42 #include <sys/types.h>
43
44 #include "oval_definitions_impl.h"
45 #include "adt/oval_collection_impl.h"
46 #include "adt/oval_string_map_impl.h"
47 #include "oval_agent_api_impl.h"
48 #include "oval_parser_impl.h"
49 #if defined(OVAL_PROBES_ENABLED)
50 #include "oval_probe.h"
51 #include "oval_probe_session.h"
52 #include "oval_probe_impl.h"
53 #endif
54 #include "common/util.h"
55 #include "common/debug_priv.h"
56 #include "common/_error.h"
57 #include "common/oscap_string.h"
58 #include "common/oscap_pcre.h"
59 #include "oval_glob_to_regex.h"
60
61 #if !defined(OVAL_PROBES_ENABLED)
62 const char *oval_subtype_to_str(oval_subtype_t subtype);
63 #endif
64
65 /***************************************************************************/
66 /* Variable definitions
67 * */
68
69 static oval_syschar_collection_flag_t _flag_agg_map[7][7] = {
70 {SYSCHAR_FLAG_UNKNOWN, SYSCHAR_FLAG_ERROR,
71 SYSCHAR_FLAG_COMPLETE, SYSCHAR_FLAG_INCOMPLETE,
72 SYSCHAR_FLAG_DOES_NOT_EXIST, SYSCHAR_FLAG_NOT_COLLECTED,
73 SYSCHAR_FLAG_NOT_APPLICABLE},
74
75 {SYSCHAR_FLAG_ERROR, SYSCHAR_FLAG_ERROR,
76 SYSCHAR_FLAG_ERROR, SYSCHAR_FLAG_ERROR,
77 SYSCHAR_FLAG_ERROR, SYSCHAR_FLAG_ERROR,
78 SYSCHAR_FLAG_ERROR},
79
80 {SYSCHAR_FLAG_COMPLETE, SYSCHAR_FLAG_ERROR,
81 SYSCHAR_FLAG_COMPLETE, SYSCHAR_FLAG_INCOMPLETE,
82 SYSCHAR_FLAG_DOES_NOT_EXIST, SYSCHAR_FLAG_NOT_COLLECTED,
83 SYSCHAR_FLAG_NOT_APPLICABLE},
84
85 {SYSCHAR_FLAG_INCOMPLETE, SYSCHAR_FLAG_ERROR,
86 SYSCHAR_FLAG_INCOMPLETE, SYSCHAR_FLAG_INCOMPLETE,
87 SYSCHAR_FLAG_DOES_NOT_EXIST, SYSCHAR_FLAG_NOT_COLLECTED,
88 SYSCHAR_FLAG_NOT_APPLICABLE},
89
90 {SYSCHAR_FLAG_DOES_NOT_EXIST, SYSCHAR_FLAG_ERROR,
91 SYSCHAR_FLAG_DOES_NOT_EXIST, SYSCHAR_FLAG_DOES_NOT_EXIST,
92 SYSCHAR_FLAG_DOES_NOT_EXIST, SYSCHAR_FLAG_NOT_COLLECTED,
93 SYSCHAR_FLAG_NOT_APPLICABLE},
94
95 {SYSCHAR_FLAG_NOT_COLLECTED, SYSCHAR_FLAG_ERROR,
96 SYSCHAR_FLAG_NOT_COLLECTED, SYSCHAR_FLAG_NOT_COLLECTED,
97 SYSCHAR_FLAG_NOT_COLLECTED, SYSCHAR_FLAG_NOT_COLLECTED,
98 SYSCHAR_FLAG_NOT_APPLICABLE},
99
100 {SYSCHAR_FLAG_NOT_APPLICABLE, SYSCHAR_FLAG_ERROR,
101 SYSCHAR_FLAG_NOT_APPLICABLE, SYSCHAR_FLAG_NOT_APPLICABLE,
102 SYSCHAR_FLAG_NOT_APPLICABLE, SYSCHAR_FLAG_NOT_APPLICABLE,
103 SYSCHAR_FLAG_NOT_APPLICABLE},
104 };
105
106 #define _AGG_FLAG(f1, f2) _flag_agg_map[f2][f1]
107 #define _COMP_TYPE(comp) oval_component_type_get_text(oval_component_get_type(comp))
108 #define _FLAG_TYPE(flag) oval_syschar_collection_flag_get_text(flag)
109
110 typedef struct {
111 enum {
112 OVAL_MODE_COMPUTE,
113 OVAL_MODE_QUERY
114 } mode;
115 union {
116 struct oval_syschar_model *sysmod;
117 #if defined(OVAL_PROBES_ENABLED)
118 oval_probe_session_t *sess;
119 #endif
120 } u;
121 } oval_argu_t;
122
123 typedef struct oval_component {
124 struct oval_definition_model *model;
125 oval_component_type_t type;
126 } oval_component_t;
127
128 typedef struct oval_component_LITERAL {
129 struct oval_definition_model *model;
130 oval_component_type_t type;
131 struct oval_value *value; /*type==OVAL_COMPONENT_LITERAL */
132 } oval_component_LITERAL_t;
133
134 typedef struct oval_component_OBJECTREF {
135 struct oval_definition_model *model;
136 oval_component_type_t type;
137 struct oval_object *object; /*type==OVAL_COMPONENT_OBJECTREF */
138 char *item_field;
139 char *record_field;
140 } oval_component_OBJECTREF_t;
141
142 typedef struct oval_component_VARREF {
143 struct oval_definition_model *model;
144 oval_component_type_t type;
145 struct oval_variable *variable; /*type==OVAL_COMPONENT_VARREF */
146 } oval_component_VARREF_t;
147
148 typedef struct oval_component_FUNCTION {
149 struct oval_definition_model *model;
150 oval_component_type_t type;
151 struct oval_collection *function_components; /*type==OVAL_COMPONENT_FUNCTION */
152 } oval_component_FUNCTION_t;
153
154 typedef struct oval_component_ARITHMETIC {
155 struct oval_definition_model *model;
156 oval_component_type_t type;
157 struct oval_collection *function_components; /*type==OVAL_COMPONENT_FUNCTION */
158 oval_arithmetic_operation_t operation; /*type==OVAL_COMPONENT_ARITHMETIC */
159 } oval_component_ARITHMETIC_t;
160
161 typedef struct oval_component_BEGEND {
162 struct oval_definition_model *model;
163 oval_component_type_t type;
164 struct oval_collection *function_components; //type==OVAL_COMPONENT_FUNCTION
165 char *character; //type==OVAL_COMPONENT_BEGIN/END
166 } oval_component_BEGEND_t;
167
168 typedef struct oval_component_SPLIT {
169 struct oval_definition_model *model;
170 oval_component_type_t type;
171 struct oval_collection *function_components; /*type==OVAL_COMPONENT_FUNCTION */
172 char *delimiter; /*type==OVAL_COMPONENT_SPLIT */
173 } oval_component_SPLIT_t;
174
175 typedef struct oval_component_GLOB {
176 struct oval_definition_model *model;
177 oval_component_type_t type;
178 struct oval_collection *function_components; /*type==OVAL_COMPONENT_FUNCTION */
179 bool glob_noescape; /*type==OVAL_COMPONENT_GLOB */
180 } oval_component_GLOB_t;
181
182 typedef struct oval_component_SUBSTRING {
183 struct oval_definition_model *model;
184 oval_component_type_t type;
185 struct oval_collection *function_components; /*type==OVAL_COMPONENT_FUNCTION */
186 int start; /*type==OVAL_COMPONENT_SUBSTRING */
187 int length; /*type==OVAL_COMPONENT_SUBSTRING */
188 } oval_component_SUBSTRING_t;
189
190 typedef struct oval_component_TIMEDIF {
191 struct oval_definition_model *model;
192 oval_component_type_t type;
193 struct oval_collection *function_components; /*type==OVAL_COMPONENT_FUNCTION */
194 oval_datetime_format_t format_1; /*/type==OVAL_COMPONENT_TIMEDIF */
195 oval_datetime_format_t format_2; /*type==OVAL_COMPONENT_TIMEDIF */
196 } oval_component_TIMEDIF_t;
197
198 typedef struct oval_component_REGEX_CAPTURE {
199 struct oval_definition_model *model;
200 oval_component_type_t type;
201 struct oval_collection *function_components; /*type==OVAL_COMPONENT_FUNCTION */
202 char *pattern; /*type==OVAL_COMPONENT_REGEX_CAPTURE */
203 } oval_component_REGEX_CAPTURE_t;
204
205 void oval_component_to_print(struct oval_component *component, char *indent, int index);
206 //typedef void (*oval_component_consumer)(struct oval_component_node *, void*);
207
208 /*
209 OVAL_COMPONENT_UNKNOWN = 0,
210 OVAL_FUNCTION_BEGIN = OVAL_FUNCTION + 1,
211 OVAL_FUNCTION_CONCAT = OVAL_FUNCTION + 2,
212 OVAL_FUNCTION_END = OVAL_FUNCTION + 3,
213 OVAL_FUNCTION_SPLIT = OVAL_FUNCTION + 4,
214 OVAL_FUNCTION_SUBSTRING = OVAL_FUNCTION + 5,
215 OVAL_FUNCTION_TIMEDIF = OVAL_FUNCTION + 6,
216 OVAL_FUNCTION_ESCAPE_REGEX = OVAL_FUNCTION + 7,
217 OVAL_FUNCTION_REGEX_CAPTURE = OVAL_FUNCTION + 8,
218 OVAL_FUNCTION_ARITHMETIC = OVAL_FUNCTION + 9
219 OVAL_FUNCTION_COUNT = OVAL_FUNCTION + 10,
220 OVAL_FUNCTION_GLOB_TO_REGEX = OVAL_FUNCTION + 12,
221
222 */
223 static const struct oscap_string_map _OVAL_COMPONENT_MAP[] = {
224 {OVAL_COMPONENT_LITERAL, "literal_component"},
225 {OVAL_COMPONENT_OBJECTREF, "object_component"},
226 {OVAL_COMPONENT_VARREF, "variable_component"},
227 {0, NULL}
228 };
229
230 static const struct oscap_string_map _OVAL_FUNCTION_MAP[] = {
231 {OVAL_FUNCTION_BEGIN, "begin"},
232 {OVAL_FUNCTION_CONCAT, "concat"},
233 {OVAL_FUNCTION_END, "end"},
234 {OVAL_FUNCTION_SPLIT, "split"},
235 {OVAL_FUNCTION_SUBSTRING, "substring"},
236 {OVAL_FUNCTION_TIMEDIF, "time_difference"},
237 {OVAL_FUNCTION_ESCAPE_REGEX, "escape_regex"},
238 {OVAL_FUNCTION_REGEX_CAPTURE, "regex_capture"},
239 {OVAL_FUNCTION_ARITHMETIC, "arithmetic"},
240 {OVAL_FUNCTION_COUNT, "count"},
241 {OVAL_FUNCTION_UNIQUE, "unique"},
242 {OVAL_FUNCTION_GLOB_TO_REGEX, "glob_to_regex"},
243 {0, NULL}
244 };
245
246 /* End of variable definitions
247 * */
248 /***************************************************************************/
249
250 bool oval_component_iterator_has_more(struct oval_component_iterator *oc_component)
251 {
252 return oval_collection_iterator_has_more((struct oval_iterator *)oc_component);
253 }
254
255 int oval_component_iterator_remaining(struct oval_component_iterator *oc_component)
256 {
257 return oval_collection_iterator_remaining((struct oval_iterator *)oc_component);
258 }
259
260 struct oval_component *oval_component_iterator_next(struct oval_component_iterator *oc_component)
261 {
262 return (struct oval_component *)
263 oval_collection_iterator_next((struct oval_iterator *)oc_component);
264 }
265
266 void oval_component_iterator_free(struct oval_component_iterator *oc_component)
267 {
268 oval_collection_iterator_free((struct oval_iterator *)oc_component);
269 }
270
271 oval_component_type_t oval_component_get_type(struct oval_component *component)
272 {
273 __attribute__nonnull__(component);
274
275 return component->type;
276 }
277
278 struct oval_value *oval_component_get_literal_value(struct oval_component *component) {
279 /* type == OVAL_COMPONENT_LITERAL */
280 struct oval_value *value = NULL;
281 if (oval_component_get_type(component) == OVAL_COMPONENT_LITERAL) {
282 value = ((struct oval_component_LITERAL *)component)->value;
283 }
284 return value;
285 }
286
287 void oval_component_set_literal_value(struct oval_component *component, struct oval_value *value) {
288 /* type == OVAL_COMPONENT_LITERAL */
289 __attribute__nonnull__(component);
290 if (oval_component_get_type(component) == OVAL_COMPONENT_LITERAL) {
291 ((struct oval_component_LITERAL *)component)->value = value;
292 }
293 }
294
295 void oval_component_set_type(struct oval_component *component, oval_component_type_t type)
296 {
297 __attribute__nonnull__(component);
298 component->type = type;
299 }
300
301 struct oval_object *oval_component_get_object(struct oval_component *component)
302 {
303 struct oval_object *object = NULL;
304 if (oval_component_get_type(component) == OVAL_COMPONENT_OBJECTREF) {
305 object = ((struct oval_component_OBJECTREF *)component)->object;
306 }
307 return object;
308 }
309
310 void oval_component_set_object(struct oval_component *component, struct oval_object *object) {
311 __attribute__nonnull__(component);
312 if (oval_component_get_type(component) == OVAL_COMPONENT_OBJECTREF) {
313 ((struct oval_component_OBJECTREF *)component)->object = object;
314 }
315 }
316
317 char *oval_component_get_item_field(struct oval_component *component) {
318 char *field = NULL;
319 if (oval_component_get_type(component) == OVAL_COMPONENT_OBJECTREF) {
320 field = ((struct oval_component_OBJECTREF *)component)->item_field;
321 }
322 return field;
323 }
324
325 void oval_component_set_item_field(struct oval_component *component, char *field) {
326 __attribute__nonnull__(component);
327 if (oval_component_get_type(component) == OVAL_COMPONENT_OBJECTREF) {
328 ((struct oval_component_OBJECTREF *)component)->item_field = oscap_strdup(field);
329 }
330 }
331
332 char *oval_component_get_record_field(struct oval_component *component) {
333 if (oval_component_get_type(component) != OVAL_COMPONENT_OBJECTREF)
334 return NULL;
335
336 return ((struct oval_component_OBJECTREF *) component)->record_field;
337 }
338
339 void oval_component_set_record_field(struct oval_component *component, char *field) {
340 __attribute__nonnull__(component);
341
342 if (oval_component_get_type(component) != OVAL_COMPONENT_OBJECTREF) {
343 dW("Wrong component type: %d.", oval_component_get_type(component));
344 return;
345 }
346
347 ((struct oval_component_OBJECTREF *) component)->record_field = oscap_strdup(field);
348 }
349
350 struct oval_variable *oval_component_get_variable(struct oval_component *component) {
351 /* type == OVAL_COMPONENT_VARREF */
352 struct oval_variable *variable = NULL;
353 if (oval_component_get_type(component) == OVAL_COMPONENT_VARREF) {
354 variable = ((struct oval_component_VARREF *)component)->variable;
355 }
356 return variable;
357 }
358
359 struct oval_component_iterator *oval_component_get_function_components(struct
360 oval_component
361 *component)
362 {
363 __attribute__nonnull__(component);
364
365 /* type == OVAL_COMPONENT_FUNCTION */
366 struct oval_component_iterator *iterator;
367 if (component->type > OVAL_COMPONENT_FUNCTION) {
368 oval_component_FUNCTION_t *function = (oval_component_FUNCTION_t *) component;
369 iterator = (struct oval_component_iterator *)
370 oval_collection_iterator(function->function_components);
371 } else
372 iterator = NULL;
373 return iterator;
374 }
375
376 oval_arithmetic_operation_t oval_component_get_arithmetic_operation(struct oval_component * component)
377 {
378 __attribute__nonnull__(component);
379
380 /* type == OVAL_COMPONENT_ARITHMETIC */
381 oval_arithmetic_operation_t operation;
382 if (component->type == OVAL_FUNCTION_ARITHMETIC) {
383 oval_component_ARITHMETIC_t *arithmetic = (oval_component_ARITHMETIC_t *) component;
384 operation = arithmetic->operation;
385 } else
386 operation = OVAL_ARITHMETIC_UNKNOWN;
387 return operation;
388 }
389
390 void oval_component_set_arithmetic_operation(struct oval_component *component, oval_arithmetic_operation_t operation)
391 {
392 __attribute__nonnull__(component);
393
394 /* type == OVAL_COMPONENT_ARITHMETIC */
395 if (component->type == OVAL_FUNCTION_ARITHMETIC) {
396 oval_component_ARITHMETIC_t *arithmetic = (oval_component_ARITHMETIC_t *) component;
397 arithmetic->operation = operation;
398 }
399 }
400
401 char *oval_component_get_prefix(struct oval_component *component)
402 {
403 __attribute__nonnull__(component);
404
405 /* type == OVAL_COMPONENT_BEGIN/END */
406 char *character;
407 if (component->type == OVAL_FUNCTION_BEGIN) {
408 oval_component_BEGEND_t *begin = (oval_component_BEGEND_t *) component;
409 character = begin->character;
410 } else
411 character = NULL;
412 return character;
413 }
414
415 void oval_component_set_prefix(struct oval_component *component, char *character)
416 {
417 __attribute__nonnull__(component);
418
419 /* type == OVAL_COMPONENT_BEGIN */
420 if (component->type == OVAL_FUNCTION_BEGIN) {
421 oval_component_BEGEND_t *begin = (oval_component_BEGEND_t *) component;
422 begin->character = oscap_strdup(character);
423 }
424 }
425
426 char *oval_component_get_suffix(struct oval_component *component)
427 {
428 __attribute__nonnull__(component);
429
430 /* type == OVAL_COMPONENT_END */
431 char *character;
432 if (component->type == OVAL_FUNCTION_END) {
433 oval_component_BEGEND_t *funcend = (oval_component_BEGEND_t *) component;
434 character = funcend->character;
435 } else
436 character = NULL;
437 return character;
438 }
439
440 void oval_component_set_suffix(struct oval_component *component, char *character) {
441 __attribute__nonnull__(component);
442
443 /* type == OVAL_COMPONENT_END */
444 if (component->type == OVAL_FUNCTION_END) {
445 oval_component_BEGEND_t *funcend = (oval_component_BEGEND_t *) component;
446 funcend->character = oscap_strdup(character);
447 }
448 }
449
450 char *oval_component_get_split_delimiter(struct oval_component *component)
451 {
452 __attribute__nonnull__(component);
453
454 /* type == OVAL_COMPONENT_SPLIT */
455 char *delimiter;
456 if (component->type == OVAL_FUNCTION_SPLIT) {
457 oval_component_SPLIT_t *split = (oval_component_SPLIT_t *) component;
458 delimiter = split->delimiter;
459 } else
460 delimiter = NULL;
461 return delimiter;
462 }
463
464 void oval_component_set_split_delimiter(struct oval_component *component, char *delimeter) {
465 __attribute__nonnull__(component);
466
467 /* type == OVAL_COMPONENT_SPLIT */
468 if (component->type == OVAL_FUNCTION_SPLIT) {
469 oval_component_SPLIT_t *split = (oval_component_SPLIT_t *) component;
470 split->delimiter = oscap_strdup(delimeter);
471 }
472 }
473
474 bool oval_component_get_glob_to_regex_glob_noescape(struct oval_component *component)
475 {
476 __attribute__nonnull__(component);
477
478 /* type == OVAL_COMPONENT_GLOB */
479 bool glob_noescape;
480 if (component->type == OVAL_FUNCTION_GLOB_TO_REGEX) {
481 oval_component_GLOB_t *glob_to_regex = (oval_component_GLOB_t *) component;
482 glob_noescape = glob_to_regex->glob_noescape;
483 } else
484 glob_noescape = false;
485 return glob_noescape;
486 }
487
488 void oval_component_set_glob_to_regex_glob_noescape(struct oval_component *component, bool glob_noescape) {
489 __attribute__nonnull__(component);
490
491 /* type == OVAL_COMPONENT_GLOB */
492 if (component->type == OVAL_FUNCTION_GLOB_TO_REGEX) {
493 oval_component_GLOB_t *glob_to_regex = (oval_component_GLOB_t *) component;
494 glob_to_regex->glob_noescape = glob_noescape;
495 }
496 }
497
498 int oval_component_get_substring_start(struct oval_component *component)
499 {
500 __attribute__nonnull__(component);
501
502 /* type == OVAL_COMPONENT_SUBSTRING */
503 int start;
504 if (component->type == OVAL_FUNCTION_SUBSTRING) {
505 oval_component_SUBSTRING_t *substring = (oval_component_SUBSTRING_t *) component;
506 start = substring->start;
507 } else
508 start = -1;
509 return start;
510 }
511
512 void oval_component_set_substring_start(struct oval_component *component, int start)
513 {
514 __attribute__nonnull__(component);
515
516 /* type==OVAL_COMPONENT_SUBSTRING */
517 if (component->type == OVAL_FUNCTION_SUBSTRING) {
518 oval_component_SUBSTRING_t *substring = (oval_component_SUBSTRING_t *) component;
519 substring->start = start;
520 }
521 }
522
523 int oval_component_get_substring_length(struct oval_component *component)
524 {
525 __attribute__nonnull__(component);
526
527 /* type == OVAL_COMPONENT_SUBSTRING */
528 int length;
529 if (component->type == OVAL_FUNCTION_SUBSTRING) {
530 oval_component_SUBSTRING_t *substring = (oval_component_SUBSTRING_t *) component;
531 length = substring->length;
532 } else
533 length = -1;
534 return length;
535 }
536
537 void oval_component_set_substring_length(struct oval_component *component, int length)
538 {
539 __attribute__nonnull__(component);
540
541 /* type == OVAL_COMPONENT_SUBSTRING */
542 if (component->type == OVAL_FUNCTION_SUBSTRING) {
543 oval_component_SUBSTRING_t *substring = (oval_component_SUBSTRING_t *) component;
544 substring->length = length;
545 }
546 }
547
548 oval_datetime_format_t oval_component_get_timedif_format_1(struct oval_component
549 *component)
550 {
551 __attribute__nonnull__(component);
552
553 /* type == OVAL_COMPONENT_TIMEDIF */
554 oval_datetime_format_t format;
555 if (component->type == OVAL_FUNCTION_TIMEDIF) {
556 oval_component_TIMEDIF_t *timedif = (oval_component_TIMEDIF_t *) component;
557 format = timedif->format_1;
558 } else
559 format = OVAL_DATETIME_UNKNOWN;
560 return format;
561 }
562
563 void oval_component_set_timedif_format_1(struct oval_component *component, oval_datetime_format_t format)
564 {
565 __attribute__nonnull__(component);
566 /* type == OVAL_COMPONENT_TIMEDIF */
567 if (component->type == OVAL_FUNCTION_TIMEDIF) {
568 oval_component_TIMEDIF_t *timedif = (oval_component_TIMEDIF_t *) component;
569 timedif->format_1 = format;
570 }
571 }
572
573 oval_datetime_format_t oval_component_get_timedif_format_2(struct oval_component
574 *component)
575 {
576 __attribute__nonnull__(component);
577
578 /* typ == OVAL_COMPONENT_TIMEDIF */
579 oval_datetime_format_t format;
580 if (component->type == OVAL_FUNCTION_TIMEDIF) {
581 oval_component_TIMEDIF_t *timedif = (oval_component_TIMEDIF_t *) component;
582 format = timedif->format_2;
583 } else
584 format = OVAL_DATETIME_UNKNOWN;
585 return format;
586 }
587
588 void oval_component_set_timedif_format_2(struct oval_component *component, oval_datetime_format_t format)
589 {
590 __attribute__nonnull__(component);
591 /* type == OVAL_COMPONENT_TIMEDIF */
592 if (component->type == OVAL_FUNCTION_TIMEDIF) {
593 oval_component_TIMEDIF_t *timedif = (oval_component_TIMEDIF_t *) component;
594 timedif->format_2 = format;
595 }
596 }
597
598 char *oval_component_get_regex_pattern(struct oval_component *component) {
599 __attribute__nonnull__(component);
600
601 /* type == OVAL_COMPONENT_REGEX_CAPTURE */
602 char *pattern = NULL;
603 if (component->type == OVAL_FUNCTION_REGEX_CAPTURE) {
604 oval_component_REGEX_CAPTURE_t *regex = (oval_component_REGEX_CAPTURE_t *) component;
605 pattern = regex->pattern;
606 }
607 return pattern;
608 }
609
610 void oval_component_set_regex_pattern(struct oval_component *component, char *pattern)
611 {
612 __attribute__nonnull__(component);
613
614 /* type == OVAL_COMPONENT_REGEX_CAPTURE */
615 if (component->type == OVAL_FUNCTION_REGEX_CAPTURE) {
616 oval_component_REGEX_CAPTURE_t *regex = (oval_component_REGEX_CAPTURE_t *) component;
617 regex->pattern = oscap_strdup(pattern);
618 }
619 }
620
621 struct oval_component *oval_component_new(struct oval_definition_model *model, oval_component_type_t type)
622 {
623 oval_component_t *component = NULL;
624
625 switch (type) {
626 case OVAL_COMPONENT_LITERAL:{
627 oval_component_LITERAL_t *literal =
628 (oval_component_LITERAL_t *) malloc(sizeof(oval_component_LITERAL_t));
629 if (literal == NULL)
630 return NULL;
631
632 component = (oval_component_t *) literal;
633 literal->value = NULL;
634 }
635 break;
636 case OVAL_COMPONENT_OBJECTREF:{
637 oval_component_OBJECTREF_t *objectref =
638 (oval_component_OBJECTREF_t *) malloc(sizeof(oval_component_OBJECTREF_t));
639 if (objectref == NULL)
640 return NULL;
641
642 component = (oval_component_t *) objectref;
643 objectref->object = NULL;
644 objectref->item_field = NULL;
645 objectref->record_field = NULL;
646 }
647 break;
648 case OVAL_COMPONENT_VARREF:{
649 oval_component_VARREF_t *varref =
650 (oval_component_VARREF_t *) malloc(sizeof(oval_component_VARREF_t));
651 if (varref == NULL)
652 return NULL;
653
654 component = (oval_component_t *) varref;
655 varref->variable = NULL;
656 }
657 break;
658 default: /* OVAL_COMPONENT_FUNCTION */
659 {
660 oval_component_FUNCTION_t *function;
661 switch (type) {
662 case OVAL_FUNCTION_ARITHMETIC:{
663 oval_component_ARITHMETIC_t *arithmetic = (oval_component_ARITHMETIC_t *)
664 (function = (oval_component_FUNCTION_t *)
665 malloc(sizeof(oval_component_ARITHMETIC_t)));
666 if (arithmetic == NULL)
667 return NULL;
668
669 arithmetic->operation = OVAL_ARITHMETIC_UNKNOWN;
670 };
671 break;
672 case OVAL_FUNCTION_BEGIN:
673 case OVAL_FUNCTION_END:{
674 oval_component_BEGEND_t *begin = (oval_component_BEGEND_t *)
675 (function = (oval_component_FUNCTION_t *)
676 malloc(sizeof(oval_component_BEGEND_t)));
677 if (begin == NULL)
678 return NULL;
679
680 begin->character = NULL;
681 };
682 break;
683 case OVAL_FUNCTION_SPLIT:{
684 oval_component_SPLIT_t *split = (oval_component_SPLIT_t *)
685 (function = (oval_component_FUNCTION_t *)
686 malloc(sizeof(oval_component_SPLIT_t)));
687 if (split == NULL)
688 return NULL;
689
690 split->delimiter = NULL;
691 };
692 break;
693 case OVAL_FUNCTION_GLOB_TO_REGEX:{
694 oval_component_GLOB_t *glob_to_regex = (oval_component_GLOB_t *)
695 (function = (oval_component_FUNCTION_t *)
696 malloc(sizeof(oval_component_GLOB_t)));
697 if (glob_to_regex == NULL)
698 return NULL;
699 glob_to_regex->glob_noescape = false;
700 };
701 break;
702 case OVAL_FUNCTION_SUBSTRING:{
703 oval_component_SUBSTRING_t *substring = (oval_component_SUBSTRING_t *)
704 (function = (oval_component_FUNCTION_t *)
705 malloc(sizeof(oval_component_SUBSTRING_t)));
706 if (substring == NULL)
707 return NULL;
708
709 substring->length = -1;
710 substring->start = -1;
711 };
712 break;
713 case OVAL_FUNCTION_TIMEDIF:{
714 oval_component_TIMEDIF_t *timedif = (oval_component_TIMEDIF_t *)
715 (function = (oval_component_FUNCTION_t *)
716 malloc(sizeof(oval_component_TIMEDIF_t)));
717 if (timedif == NULL)
718 return NULL;
719
720 timedif->format_1 = OVAL_DATETIME_UNKNOWN;
721 timedif->format_2 = OVAL_DATETIME_UNKNOWN;
722 };
723 break;
724 case OVAL_FUNCTION_REGEX_CAPTURE:{
725 oval_component_REGEX_CAPTURE_t *regex = (oval_component_REGEX_CAPTURE_t *)
726 (function = (oval_component_FUNCTION_t *)
727 malloc(sizeof(oval_component_REGEX_CAPTURE_t)));
728 if (regex == NULL)
729 return NULL;
730
731 regex->pattern = NULL;
732 };
733 break;
734 default:{
735 function = (oval_component_FUNCTION_t *)
736 malloc(sizeof(oval_component_FUNCTION_t));
737 if (function == NULL)
738 return NULL;
739 }
740 }
741
742 component = (oval_component_t *) function;
743 function->function_components = oval_collection_new();
744 }
745 break;
746
747 }
748 component->type = type;
749 component->model = model;
750 return component;
751 }
752
753 static void _oval_component_clone_subcomponents
754 (struct oval_component *old_component, struct oval_component *new_component, struct oval_definition_model *model) {
755 struct oval_component_iterator *subcomps = oval_component_get_function_components(old_component);
756 if (subcomps)
757 while (oval_component_iterator_has_more(subcomps)) {
758 struct oval_component *subcomp = oval_component_iterator_next(subcomps);
759 oval_component_add_function_component(new_component, oval_component_clone(model, subcomp));
760 }
761 oval_component_iterator_free(subcomps);
762 }
763
764 struct oval_component *oval_component_clone(struct oval_definition_model *new_model,
765 struct oval_component *old_component)
766 {
767 __attribute__nonnull__(old_component);
768
769 struct oval_component *new_component = oval_component_new(new_model, old_component->type);
770 if (new_component == NULL)
771 return NULL;
772
773 switch (new_component->type) {
774 case OVAL_FUNCTION_ARITHMETIC:{
775 oval_arithmetic_operation_t operation = oval_component_get_arithmetic_operation(old_component);
776 if (operation)
777 oval_component_set_arithmetic_operation(new_component, operation);
778 }
779 break;
780 case OVAL_FUNCTION_BEGIN:{
781 char *begchar = oval_component_get_prefix(old_component);
782 if (begchar)
783 oval_component_set_prefix(new_component, begchar);
784 }
785 break;
786 case OVAL_FUNCTION_END:{
787 char *endchar = oval_component_get_suffix(old_component);
788 if (endchar)
789 oval_component_set_suffix(new_component, endchar);
790 }
791 break;
792 case OVAL_COMPONENT_LITERAL:{
793 struct oval_value *value = oval_component_get_literal_value(old_component);
794 if (value)
795 oval_component_set_literal_value(new_component, oval_value_clone(value));
796 }
797 break;
798 case OVAL_COMPONENT_OBJECTREF:{
799 char *field;
800 struct oval_object *old_object = oval_component_get_object(old_component);
801 if (old_object) {
802 struct oval_object *new_object = oval_object_clone(new_model, old_object);
803 oval_component_set_object(new_component, new_object);
804 }
805 field = oval_component_get_item_field(old_component);
806 if (field)
807 oval_component_set_item_field(new_component, field);
808 field = oval_component_get_record_field(old_component);
809 if (field)
810 oval_component_set_record_field(new_component, field);
811 }
812 break;
813 case OVAL_FUNCTION_REGEX_CAPTURE:{
814 char *pattern = oval_component_get_regex_pattern(old_component);
815 if (pattern)
816 oval_component_set_regex_pattern(new_component, pattern);
817 }
818 break;
819 case OVAL_FUNCTION_SPLIT:{
820 char *delimiter = oval_component_get_split_delimiter(old_component);
821 if (delimiter)
822 oval_component_set_split_delimiter(new_component, delimiter);
823 }
824 break;
825 case OVAL_FUNCTION_GLOB_TO_REGEX:{
826 bool glob_noescape = oval_component_get_glob_to_regex_glob_noescape(old_component);
827 oval_component_set_glob_to_regex_glob_noescape(new_component, glob_noescape);
828 }
829 break;
830 case OVAL_FUNCTION_SUBSTRING:{
831 int length = oval_component_get_substring_length(old_component);
832 oval_component_set_substring_length(new_component, length);
833 int start = oval_component_get_substring_start(old_component);
834 oval_component_set_substring_start(new_component, start);
835 } break;
836 case OVAL_FUNCTION_TIMEDIF:{
837 oval_datetime_format_t format1 = oval_component_get_timedif_format_1(old_component);
838 if (format1)
839 oval_component_set_timedif_format_1(new_component, format1);
840 oval_datetime_format_t format2 = oval_component_get_timedif_format_2(old_component);
841 if (format2)
842 oval_component_set_timedif_format_1(new_component, format2);
843 }
844 break;
845 case OVAL_COMPONENT_VARREF:{
846 struct oval_variable *old_variable = oval_component_get_variable(old_component);
847 if (old_variable) {
848 struct oval_variable *new_variable = oval_variable_clone(new_model, old_variable);
849 oval_component_set_variable(new_component, new_variable);
850 }
851 } break;
852 default:
853 /*NOOP*/ break;
854 }
855
856 if (new_component->type > OVAL_FUNCTION)
857 _oval_component_clone_subcomponents(old_component, new_component, new_model);
858
859 return new_component;
860 }
861
862 void oval_component_free(struct oval_component *component)
863 {
864 if (component == NULL)
865 return;
866
867 switch (component->type) {
868 case OVAL_COMPONENT_LITERAL:{
869 oval_component_LITERAL_t *literal = (oval_component_LITERAL_t *) component;
870 oval_value_free(literal->value);
871 literal->value = NULL;
872 }
873 break;
874 case OVAL_COMPONENT_OBJECTREF:{
875 oval_component_OBJECTREF_t *objectref = (oval_component_OBJECTREF_t *) component;
876 free(objectref->item_field);
877 objectref->item_field = NULL;
878 free(objectref->record_field);
879 objectref->record_field = NULL;
880 }
881 break;
882 case OVAL_FUNCTION_BEGIN:
883 case OVAL_FUNCTION_END:{
884 oval_component_BEGEND_t *begin = (oval_component_BEGEND_t *) component;
885 free(begin->character);
886 begin->character = NULL;
887 };
888 break;
889 case OVAL_FUNCTION_SPLIT:{
890 oval_component_SPLIT_t *split = (oval_component_SPLIT_t *) component;
891 free(split->delimiter);
892 split->delimiter = NULL;
893 };
894 break;
895 case OVAL_FUNCTION_REGEX_CAPTURE:{
896 oval_component_REGEX_CAPTURE_t *regex = (oval_component_REGEX_CAPTURE_t *) component;
897 free(regex->pattern);
898 regex->pattern = NULL;
899 };
900 break;
901 case OVAL_FUNCTION_GLOB_TO_REGEX:
902 case OVAL_FUNCTION_CONCAT:
903 case OVAL_FUNCTION_COUNT:
904 case OVAL_FUNCTION_UNIQUE:
905 case OVAL_FUNCTION_SUBSTRING:
906 case OVAL_FUNCTION_TIMEDIF:
907 case OVAL_FUNCTION_ESCAPE_REGEX:
908 case OVAL_FUNCTION_ARITHMETIC:
909 case OVAL_FUNCTION_LAST:
910 case OVAL_COMPONENT_VARREF:
911 case OVAL_COMPONENT_FUNCTION:
912 case OVAL_COMPONENT_UNKNOWN:
913 /*NOOP*/ break;
914 }
915 if (component->type > OVAL_FUNCTION) {
916 oval_component_FUNCTION_t *function = (oval_component_FUNCTION_t *) component;
917 oval_collection_free_items(function->function_components, (oscap_destruct_func) oval_component_free);
918 function->function_components = NULL;
919 }
920 free(component);
921 }
922
923 void oval_component_add_function_component(struct oval_component *component, struct oval_component *func_component)
924 {
925 __attribute__nonnull__(component);
926
927 if (component->type > OVAL_FUNCTION) {
928 oval_component_FUNCTION_t *function = (oval_component_FUNCTION_t *) component;
929 if (func_component)
930 oval_collection_add(function->function_components, func_component);
931 }
932 }
933
934 void oval_component_set_variable(struct oval_component *component, struct oval_variable *variable)
935 {
936 __attribute__nonnull__(component);
937
938 /* type == OVAL_COMPONENT_VARREF */
939 if (component->type == OVAL_COMPONENT_VARREF) {
940 oval_component_VARREF_t *varref = (oval_component_VARREF_t *) component;
941 varref->variable = variable;
942 }
943 }
944
945 static void oval_value_consume(struct oval_value *value, void *component)
946 {
947 oval_component_set_literal_value(component, value);
948 }
949
950 static int _oval_component_parse_LITERAL_tag(xmlTextReaderPtr reader,
951 struct oval_parser_context *context, struct oval_component *component)
952 {
953
954 return oval_value_parse_tag(reader, context, oval_value_consume, component);
955 }
956
957 static int _oval_component_parse_OBJECTREF_tag(xmlTextReaderPtr reader,
958 struct oval_parser_context *context, struct oval_component *component)
959 {
960
961 struct oval_definition_model *model = context->definition_model;
962 char *objref = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "object_ref");
963 struct oval_object *object = oval_definition_model_get_new_object(model, objref);
964 char *field;
965
966 free(objref);
967 objref = NULL;
968 oval_component_set_object(component, object);
969
970 field = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "item_field");
971 oval_component_set_item_field(component, field);
972 if (field)
973 free(field);
974 field = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "record_field");
975 oval_component_set_record_field(component, field);
976 if (field)
977 free(field);
978
979 return 0;
980 }
981
982 static int _oval_component_parse_VARREF_tag(xmlTextReaderPtr reader,
983 struct oval_parser_context *context, struct oval_component *component)
984 {
985
986 struct oval_definition_model *model = context->definition_model;
987 char *varref = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "var_ref");
988 struct oval_variable *variable = oval_definition_model_get_new_variable(model, varref, OVAL_VARIABLE_UNKNOWN);
989 if (varref != NULL) {
990 free(varref);
991 varref = NULL;
992 }
993 oval_component_set_variable(component, variable);
994
995 return 0;
996 }
997
998 static void oval_subcomp_consume(struct oval_component *subcomp, void *func)
999 {
1000
1001 __attribute__nonnull__(func);
1002
1003 oval_component_FUNCTION_t *function = func;
1004 oval_collection_add(function->function_components, (void *)subcomp);
1005 }
1006
1007 static int oval_subcomp_tag_consume(xmlTextReaderPtr reader, struct oval_parser_context *context, void *func)
1008 {
1009
1010 return oval_component_parse_tag(reader, context, &oval_subcomp_consume, func);
1011 }
1012
1013 static int _oval_component_parse_FUNCTION_tag(xmlTextReaderPtr reader,
1014 struct oval_parser_context *context, struct oval_component *component)
1015 {
1016 oval_component_FUNCTION_t *function = (oval_component_FUNCTION_t *) component;
1017 return oval_parser_parse_tag(reader, context, &oval_subcomp_tag_consume, function);
1018 }
1019
1020 static int _oval_component_parse_ARITHMETIC_tag(xmlTextReaderPtr reader,
1021 struct oval_parser_context *context, struct oval_component *component)
1022 {
1023 __attribute__nonnull__(component);
1024
1025 oval_component_ARITHMETIC_t *arithmetic = (oval_component_ARITHMETIC_t *) component;
1026 oval_arithmetic_operation_t operation = oval_arithmetic_operation_parse(reader, "arithmetic_operation",
1027 OVAL_ARITHMETIC_UNKNOWN);
1028 arithmetic->operation = operation;
1029 return _oval_component_parse_FUNCTION_tag(reader, context, component);
1030 }
1031
1032 static int _oval_component_parse_BEGEND_tag(xmlTextReaderPtr reader,
1033 struct oval_parser_context *context, struct oval_component *component)
1034 {
1035
1036 __attribute__nonnull__(component);
1037
1038 oval_component_BEGEND_t *begend = (oval_component_BEGEND_t *) component;
1039 begend->character = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "character");
1040
1041 return _oval_component_parse_FUNCTION_tag(reader, context, component);
1042 }
1043
1044 static int _oval_component_parse_SPLIT_tag(xmlTextReaderPtr reader,
1045 struct oval_parser_context *context, struct oval_component *component)
1046 {
1047
1048 __attribute__nonnull__(component);
1049
1050 oval_component_SPLIT_t *split = (oval_component_SPLIT_t *) component;
1051 split->delimiter = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "delimiter");
1052
1053 return _oval_component_parse_FUNCTION_tag(reader, context, component);
1054 }
1055
1056 static int _oval_component_parse_GLOB_TO_REGEX_tag(xmlTextReaderPtr reader,
1057 struct oval_parser_context *context, struct oval_component *component)
1058 {
1059
1060 __attribute__nonnull__(component);
1061
1062 oval_component_GLOB_t *glob_to_regex = (oval_component_GLOB_t *) component;
1063 glob_to_regex->glob_noescape = oval_parser_boolean_attribute(reader, "glob_noescape", 0);
1064
1065 return _oval_component_parse_FUNCTION_tag(reader, context, component);
1066 }
1067
1068
1069 static int _oval_component_parse_SUBSTRING_tag(xmlTextReaderPtr reader,
1070 struct oval_parser_context *context, struct oval_component *component)
1071 {
1072
1073 __attribute__nonnull__(component);
1074
1075 oval_component_SUBSTRING_t *substring = (oval_component_SUBSTRING_t *) component;
1076 char *start_text = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "substring_start");
1077 char *length_text = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "substring_length");
1078 int start = (start_text == NULL) ? 0 : atoi(start_text);
1079 int length = (length_text == NULL) ? 0 : atoi(length_text);
1080 if (start_text != NULL)
1081 free(start_text);
1082 if (length_text != NULL)
1083 free(length_text);
1084 substring->start = start;
1085 substring->length = length;
1086
1087 return _oval_component_parse_FUNCTION_tag(reader, context, component);
1088 }
1089
1090 static int _oval_component_parse_TIMEDIF_tag(xmlTextReaderPtr reader,
1091 struct oval_parser_context *context, struct oval_component *component)
1092 {
1093
1094 __attribute__nonnull__(component);
1095
1096 oval_component_TIMEDIF_t *timedif = (oval_component_TIMEDIF_t *) component;
1097 oval_datetime_format_t format_1 = oval_datetime_format_parse(reader, "format_1",
1098 OVAL_DATETIME_YEAR_MONTH_DAY);
1099 oval_datetime_format_t format_2 = oval_datetime_format_parse(reader, "format_2",
1100 OVAL_DATETIME_YEAR_MONTH_DAY);
1101 timedif->format_1 = format_1;
1102 timedif->format_2 = format_2;
1103
1104 return _oval_component_parse_FUNCTION_tag(reader, context, component);
1105 }
1106
1107 static int _oval_component_parse_REGEX_CAPTURE_tag(xmlTextReaderPtr reader,
1108 struct oval_parser_context *context,
1109 struct oval_component *component)
1110 {
1111 __attribute__nonnull__(component);
1112
1113 oval_component_REGEX_CAPTURE_t *regex = (oval_component_REGEX_CAPTURE_t *) component;
1114
1115 regex->pattern = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "pattern");
1116
1117 return _oval_component_parse_FUNCTION_tag(reader, context, component);
1118 }
1119
1120 int oval_component_parse_tag(xmlTextReaderPtr reader,
1121 struct oval_parser_context *context, oval_component_consumer consumer, void *user)
1122 {
1123
1124 __attribute__nonnull__(context);
1125
1126 int return_code = 0;
1127
1128 struct oval_definition_model *model = context->definition_model;
1129 char *tagname = (char *)xmlTextReaderLocalName(reader);
1130 /* oval_component_type_enum type; */
1131 struct oval_component *component = NULL;
1132 if (strcmp(tagname, "literal_component") == 0) {
1133 component = oval_component_new(model, OVAL_COMPONENT_LITERAL);
1134 return_code = _oval_component_parse_LITERAL_tag(reader, context, component);
1135 } else if (strcmp(tagname, "object_component") == 0) {
1136 component = oval_component_new(model, OVAL_COMPONENT_OBJECTREF);
1137 return_code = _oval_component_parse_OBJECTREF_tag(reader, context, component);
1138 } else if (strcmp(tagname, "variable_component") == 0) {
1139 component = oval_component_new(model, OVAL_COMPONENT_VARREF);
1140 return_code = _oval_component_parse_VARREF_tag(reader, context, component);
1141 } else if (strcmp(tagname, "arithmetic") == 0) {
1142 component = oval_component_new(model, OVAL_FUNCTION_ARITHMETIC);
1143 return_code = _oval_component_parse_ARITHMETIC_tag(reader, context, component);
1144 } else if (strcmp(tagname, "begin") == 0) {
1145 component = oval_component_new(model, OVAL_FUNCTION_BEGIN);
1146 return_code = _oval_component_parse_BEGEND_tag(reader, context, component);
1147 } else if (strcmp(tagname, "concat") == 0) {
1148 component = oval_component_new(model, OVAL_FUNCTION_CONCAT);
1149 return_code = _oval_component_parse_FUNCTION_tag(reader, context, component);
1150 } else if (strcmp(tagname, "count") == 0) {
1151 component = oval_component_new(model, OVAL_FUNCTION_COUNT);
1152 return_code = _oval_component_parse_FUNCTION_tag(reader, context, component);
1153 } else if (strcmp(tagname, "unique") == 0) {
1154 component = oval_component_new(model, OVAL_FUNCTION_UNIQUE);
1155 return_code = _oval_component_parse_FUNCTION_tag(reader, context, component);
1156 } else if (strcmp(tagname, "end") == 0) {
1157 component = oval_component_new(model, OVAL_FUNCTION_END);
1158 return_code = _oval_component_parse_BEGEND_tag(reader, context, component);
1159 } else if (strcmp(tagname, "escape_regex") == 0) {
1160 component = oval_component_new(model, OVAL_FUNCTION_ESCAPE_REGEX);
1161 return_code = _oval_component_parse_FUNCTION_tag(reader, context, component);
1162 } else if (strcmp(tagname, "glob_to_regex") == 0) {
1163 component = oval_component_new(model, OVAL_FUNCTION_GLOB_TO_REGEX);
1164 return_code = _oval_component_parse_GLOB_TO_REGEX_tag(reader, context, component);
1165 } else if (strcmp(tagname, "split") == 0) {
1166 component = oval_component_new(model, OVAL_FUNCTION_SPLIT);
1167 return_code = _oval_component_parse_SPLIT_tag(reader, context, component);
1168 } else if (strcmp(tagname, "substring") == 0) {
1169 component = oval_component_new(model, OVAL_FUNCTION_SUBSTRING);
1170 return_code = _oval_component_parse_SUBSTRING_tag(reader, context, component);
1171 } else if (strcmp(tagname, "time_difference") == 0) {
1172 component = oval_component_new(model, OVAL_FUNCTION_TIMEDIF);
1173 return_code = _oval_component_parse_TIMEDIF_tag(reader, context, component);
1174 } else if (strcmp(tagname, "regex_capture") == 0) {
1175 component = oval_component_new(model, OVAL_FUNCTION_REGEX_CAPTURE);
1176 return_code = _oval_component_parse_REGEX_CAPTURE_tag(reader, context, component);
1177 } else {
1178 dD("Tag <%s> not handled, line: %d.", tagname,
1179 xmlTextReaderGetParserLineNumber(reader));
1180 return_code = oval_parser_skip_tag(reader, context);
1181 }
1182 if (component != NULL)
1183 (*consumer) (component, user);
1184
1185 if (return_code != 0 ) {
1186 dW("Parsing of <%s> terminated by an error at line %d.", tagname, xmlTextReaderGetParserLineNumber(reader));
1187 }
1188 free(tagname);
1189 return return_code;
1190 }
1191
1192 xmlNode *oval_component_to_dom(struct oval_component *component, xmlDoc * doc, xmlNode * parent)
1193 {
1194 oval_component_type_t type = oval_component_get_type(component);
1195 const char *local_name = type < OVAL_FUNCTION
1196 ? _OVAL_COMPONENT_MAP[type - 1].string : _OVAL_FUNCTION_MAP[type - OVAL_FUNCTION - 1].string;
1197
1198 char *content;
1199 if (type == OVAL_COMPONENT_LITERAL) {
1200 struct oval_value *value = oval_component_get_literal_value(component);
1201 content = oval_value_get_text(value);
1202 } else {
1203 content = NULL;
1204 }
1205
1206 xmlNs *ns_definitions = xmlSearchNsByHref(doc, parent, OVAL_DEFINITIONS_NAMESPACE);
1207 xmlNode *component_node = xmlNewTextChild(parent, ns_definitions,
1208 BAD_CAST local_name,
1209 BAD_CAST content);
1210
1211 switch (oval_component_get_type(component)) {
1212 case OVAL_COMPONENT_LITERAL:{
1213 struct oval_value *value = oval_component_get_literal_value(component);
1214 oval_datatype_t datatype = oval_value_get_datatype(value);
1215 if (datatype != OVAL_DATATYPE_STRING)
1216 xmlNewProp
1217 (component_node, BAD_CAST "datatype", BAD_CAST oval_datatype_get_text(datatype));
1218 }
1219 break;
1220 case OVAL_COMPONENT_OBJECTREF:{
1221 char *item_field, *record_field;
1222 struct oval_object *object = oval_component_get_object(component);
1223 char *object_ref = oval_object_get_id(object);
1224 xmlNewProp(component_node, BAD_CAST "object_ref", BAD_CAST object_ref);
1225 item_field = oval_component_get_item_field(component);
1226 xmlNewProp(component_node, BAD_CAST "item_field", BAD_CAST item_field);
1227 record_field = oval_component_get_record_field(component);
1228 if (record_field)
1229 xmlNewProp(component_node, BAD_CAST "record_field", BAD_CAST record_field);
1230 } break;
1231 case OVAL_COMPONENT_VARREF:{
1232 struct oval_variable *variable = oval_component_get_variable(component);
1233 char *var_ref = oval_variable_get_id(variable);
1234 xmlNewProp(component_node, BAD_CAST "var_ref", BAD_CAST var_ref);
1235
1236 } break;
1237 case OVAL_FUNCTION_ARITHMETIC:{
1238 oval_arithmetic_operation_t operation = oval_component_get_arithmetic_operation(component);
1239 xmlNewProp
1240 (component_node,
1241 BAD_CAST "arithmetic_operation", BAD_CAST oval_arithmetic_operation_get_text(operation));
1242 }
1243 break;
1244 case OVAL_FUNCTION_BEGIN:{
1245 char *character = oval_component_get_prefix(component);
1246 xmlNewProp(component_node, BAD_CAST "character", BAD_CAST character);
1247 } break;
1248 case OVAL_FUNCTION_END:{
1249 char *character = oval_component_get_suffix(component);
1250 xmlNewProp(component_node, BAD_CAST "character", BAD_CAST character);
1251 } break;
1252 case OVAL_FUNCTION_SUBSTRING:{
1253 int start = oval_component_get_substring_start(component);
1254 char substring_start[10];
1255 *substring_start = '\0';
1256 snprintf(substring_start, sizeof(substring_start), "%d", start);
1257 xmlNewProp(component_node, BAD_CAST "substring_start", BAD_CAST substring_start);
1258 int length = oval_component_get_substring_length(component);
1259 char substring_length[10];
1260 *substring_length = '\0';
1261 snprintf(substring_length, sizeof(substring_length), "%d", length);
1262 xmlNewProp(component_node, BAD_CAST "substring_length", BAD_CAST substring_length);
1263 }
1264 break;
1265 case OVAL_FUNCTION_TIMEDIF:{
1266 oval_datetime_format_t format_1 = oval_component_get_timedif_format_1(component);
1267 if (format_1 != OVAL_DATETIME_YEAR_MONTH_DAY) {
1268 xmlNewProp
1269 (component_node,
1270 BAD_CAST "format_1", BAD_CAST oval_datetime_format_get_text(format_1));
1271 }
1272 oval_datetime_format_t format_2 = oval_component_get_timedif_format_2(component);
1273 if (format_2 != OVAL_DATETIME_YEAR_MONTH_DAY) {
1274 xmlNewProp
1275 (component_node,
1276 BAD_CAST "format_2", BAD_CAST oval_datetime_format_get_text(format_2));
1277 }
1278 }
1279 break;
1280 case OVAL_FUNCTION_REGEX_CAPTURE:{
1281 char *pattern = oval_component_get_regex_pattern(component);
1282 xmlNewProp(component_node, BAD_CAST "pattern", BAD_CAST pattern);
1283 } break;
1284 case OVAL_FUNCTION_SPLIT:{
1285 char * delimiter = oval_component_get_split_delimiter(component);
1286 xmlNewProp(component_node, BAD_CAST "delimiter", BAD_CAST delimiter);
1287 } break;
1288 case OVAL_FUNCTION_GLOB_TO_REGEX:{
1289 bool glob_noescape = oval_component_get_glob_to_regex_glob_noescape(component);
1290 if (glob_noescape) {
1291 xmlNewProp(component_node, BAD_CAST "glob_noescape", BAD_CAST "true");
1292 } else {
1293 xmlNewProp(component_node, BAD_CAST "glob_noescape", BAD_CAST "false");
1294 }
1295 } break;
1296 case OVAL_FUNCTION_CONCAT:
1297 case OVAL_FUNCTION_ESCAPE_REGEX:
1298 break;
1299 default:
1300 break;
1301 }
1302
1303 if (type > OVAL_FUNCTION) {
1304 struct oval_component_iterator *components = oval_component_get_function_components(component);
1305 while (oval_component_iterator_has_more(components)) {
1306 struct oval_component *sub_component = oval_component_iterator_next(components);
1307 oval_component_to_dom(sub_component, doc, component_node);
1308 }
1309 oval_component_iterator_free(components);
1310 }
1311
1312 return component_node;
1313 }
1314
1315 static oval_syschar_collection_flag_t oval_component_eval_common(oval_argu_t *argu,
1316 struct oval_component *component,
1317 struct oval_collection *value_collection);
1318
1319 static oval_syschar_collection_flag_t _oval_component_evaluate_LITERAL(oval_argu_t *argu,
1320 struct oval_component *component,
1321 struct oval_collection *value_collection)
1322 {
1323 __attribute__nonnull__(component);
1324
1325 struct oval_component_LITERAL *literal = (struct oval_component_LITERAL *)component;
1326 oval_collection_add(value_collection, oval_value_clone(literal->value));
1327 return SYSCHAR_FLAG_COMPLETE;
1328 }
1329
1330 static oval_syschar_collection_flag_t _oval_component_evaluate_OBJECTREF(oval_argu_t *argu,
1331 struct oval_component *component,
1332 struct oval_collection *value_collection)
1333 {
1334 __attribute__nonnull__(component);
1335
1336 oval_syschar_collection_flag_t flag = SYSCHAR_FLAG_ERROR;
1337
1338 struct oval_component_OBJECTREF *objref = (struct oval_component_OBJECTREF *)component;
1339 struct oval_object *object = objref->object;
1340 struct oval_syschar *syschar;
1341
1342 if (!object)
1343 return flag;
1344
1345 const char *obj_id = oval_object_get_id(object);
1346 dI("Variable component references to object '%s'.", obj_id);
1347
1348 if (argu->mode == OVAL_MODE_QUERY) {
1349 #if defined(OVAL_PROBES_ENABLED)
1350 if (oval_probe_query_object(argu->u.sess, object, 0, &syschar) != 0)
1351 return flag;
1352 #else
1353 return SYSCHAR_FLAG_ERROR;
1354 #endif
1355 } else {
1356 syschar = oval_syschar_model_get_syschar(argu->u.sysmod, obj_id);
1357 }
1358
1359 if (syschar) {
1360 char *ifield_name, *rfield_name;
1361
1362 flag = oval_syschar_get_flag(syschar);
1363 ifield_name = objref->item_field;
1364 rfield_name = objref->record_field;
1365 struct oval_sysitem_iterator *sysitems = oval_syschar_get_sysitem(syschar);
1366 while (oval_sysitem_iterator_has_more(sysitems)) {
1367 struct oval_sysitem *sysitem = oval_sysitem_iterator_next(sysitems);
1368 struct oval_sysent_iterator *sysent_itr = oval_sysitem_get_sysents(sysitem);
1369 const char *oval_sysitem_id = oval_sysitem_get_id(sysitem);
1370 const char *oval_sysitem_subtype = oval_subtype_to_str(oval_sysitem_get_subtype(sysitem));
1371 bool entity_matched = false;
1372 while (oval_sysent_iterator_has_more(sysent_itr)) {
1373 oval_datatype_t dt;
1374 struct oval_sysent *sysent = oval_sysent_iterator_next(sysent_itr);
1375 char *sysent_name = oval_sysent_get_name(sysent);
1376
1377 if (strcmp(ifield_name, sysent_name))
1378 continue;
1379
1380 entity_matched = true;
1381 dt = oval_sysent_get_datatype(sysent);
1382 if (dt == OVAL_DATATYPE_RECORD && rfield_name == NULL) {
1383 oscap_seterr(OSCAP_EFAMILY_OVAL,
1384 "Unexpected record data type in %s_item (id: %s) specified by object '%s'.",
1385 oval_sysitem_subtype, oval_sysitem_id, obj_id);
1386 oval_sysent_iterator_free(sysent_itr);
1387 oval_sysitem_iterator_free(sysitems);
1388 return SYSCHAR_FLAG_ERROR;
1389 }
1390 if (dt != OVAL_DATATYPE_RECORD && rfield_name != NULL) {
1391 oscap_seterr(OSCAP_EFAMILY_OVAL,
1392 "Expected record data type, but found %s data type in %s entity in %s_item (id: %s) specified by object '%s'.",
1393 oval_datatype_get_text(dt), ifield_name, oval_sysitem_subtype, oval_sysitem_id, obj_id);
1394 oval_sysent_iterator_free(sysent_itr);
1395 oval_sysitem_iterator_free(sysitems);
1396 return SYSCHAR_FLAG_ERROR;
1397 }
1398
1399 if (dt == OVAL_DATATYPE_RECORD) {
1400 struct oval_record_field_iterator *rf_itr;
1401 bool field_matched = false;
1402
1403 rf_itr = oval_sysent_get_record_fields(sysent);
1404 while (oval_record_field_iterator_has_more(rf_itr)) {
1405 struct oval_record_field *rf;
1406 char *txtval;
1407 struct oval_value *val;
1408
1409 rf = oval_record_field_iterator_next(rf_itr);
1410 txtval = oval_record_field_get_name(rf);
1411 if (strcmp(rfield_name, txtval))
1412 continue;
1413
1414 field_matched = true;
1415 dt = oval_record_field_get_datatype(rf);
1416 txtval = oval_record_field_get_value(rf);
1417 val = oval_value_new(dt, txtval);
1418 oval_collection_add(value_collection, val);
1419 }
1420 /* throw error if none matched */
1421 if (!field_matched) {
1422 oscap_seterr(OSCAP_EFAMILY_OVAL,
1423 "Record field '%s' has not been found in %s_item (id: %s) specified by object '%s'.",
1424 rfield_name, oval_sysitem_subtype, oval_sysitem_id, obj_id);
1425 oval_record_field_iterator_free(rf_itr);
1426 oval_sysent_iterator_free(sysent_itr);
1427 oval_sysitem_iterator_free(sysitems);
1428 return SYSCHAR_FLAG_ERROR;
1429 }
1430 oval_record_field_iterator_free(rf_itr);
1431 } else {
1432 char *txtval;
1433 struct oval_value *val;
1434
1435 txtval = oval_sysent_get_value(sysent);
1436 val = oval_value_new(dt, txtval);
1437 oval_collection_add(value_collection, val);
1438 }
1439 }
1440 /* throw error if none matched */
1441 if (!entity_matched) {
1442 oscap_seterr(OSCAP_EFAMILY_OVAL,
1443 "Entity '%s' has not been found in %s_item (id: %s) specified by object '%s'.",
1444 ifield_name, oval_sysitem_subtype, oval_sysitem_id, obj_id);
1445 oval_sysent_iterator_free(sysent_itr);
1446 oval_sysitem_iterator_free(sysitems);
1447 return SYSCHAR_FLAG_ERROR;
1448 }
1449 oval_sysent_iterator_free(sysent_itr);
1450 }
1451 oval_sysitem_iterator_free(sysitems);
1452 }
1453
1454 return flag;
1455 }
1456
1457 #define _HAS_VALUES(flag) (flag==SYSCHAR_FLAG_COMPLETE || flag==SYSCHAR_FLAG_INCOMPLETE)
1458 static oval_syschar_collection_flag_t _oval_component_evaluate_VARREF(oval_argu_t *argu,
1459 struct oval_component *component,
1460 struct oval_collection *value_collection)
1461 {
1462 __attribute__nonnull__(component);
1463
1464 oval_syschar_collection_flag_t flag = SYSCHAR_FLAG_ERROR;
1465 struct oval_component_VARREF *varref = (struct oval_component_VARREF *)component;
1466 struct oval_variable *variable = varref->variable;
1467
1468 if (!variable) {
1469 oscap_seterr(OSCAP_EFAMILY_OVAL, "No variable bound to VARREF componenet");
1470 return flag;
1471 }
1472
1473 dIndent(1);
1474 dI("Variable component references %s '%s'",
1475 oval_variable_type_get_text(oval_variable_get_type(variable)),
1476 oval_variable_get_id(variable));
1477
1478 if (argu->mode == OVAL_MODE_QUERY) {
1479 #if defined(OVAL_PROBES_ENABLED)
1480 if (oval_probe_query_variable(argu->u.sess, variable) != 0) {
1481 dIndent(-1);
1482 return flag;
1483 }
1484 #else
1485 return SYSCHAR_FLAG_ERROR;
1486 #endif
1487 } else {
1488 if (oval_syschar_model_compute_variable(argu->u.sysmod, variable) != 0) {
1489 dIndent(-1);
1490 return flag;
1491 }
1492 }
1493
1494 flag = oval_variable_get_collection_flag(variable);
1495 if (_HAS_VALUES(flag)) {
1496 struct oval_value_iterator *values = oval_variable_get_values(variable);
1497 while (oval_value_iterator_has_more(values)) {
1498 struct oval_value *value = oval_value_iterator_next(values);
1499 oval_collection_add(value_collection, oval_value_clone(value));
1500 }
1501 oval_value_iterator_free(values);
1502 }
1503
1504 dIndent(-1);
1505 return flag;
1506 }
1507
1508 static oval_syschar_collection_flag_t _oval_component_evaluate_BEGIN(oval_argu_t *argu,
1509 struct oval_component *component,
1510 struct oval_collection *value_collection)
1511 {
1512 oval_syschar_collection_flag_t flag = SYSCHAR_FLAG_ERROR;
1513 char *prefix = oval_component_get_prefix(component);
1514 if (prefix) {
1515 int len_prefix = strlen(prefix);
1516 struct oval_component_iterator *subcomps = oval_component_get_function_components(component);
1517 if (oval_component_iterator_has_more(subcomps)) { /*only the first component is processed */
1518 struct oval_collection *subcoll = oval_collection_new();
1519 struct oval_component *subcomp = oval_component_iterator_next(subcomps);
1520 flag = oval_component_eval_common(argu, subcomp, subcoll);
1521 struct oval_value_iterator *values =
1522 (struct oval_value_iterator *)oval_collection_iterator(subcoll);
1523 while (oval_value_iterator_has_more(values)) {
1524 char *key = oval_value_get_text(oval_value_iterator_next(values));
1525 const size_t concat_len = len_prefix + strlen(key) + 1;
1526 char *concat = malloc(concat_len);
1527 if (strncmp(prefix, key, len_prefix)) {
1528 snprintf(concat, concat_len, "%s%s", prefix, key);
1529 } else {
1530 snprintf(concat, concat_len, "%s", key);
1531 }
1532 struct oval_value *concat_value = oval_value_new(OVAL_DATATYPE_STRING, concat);
1533 free(concat);
1534 oval_collection_add(value_collection, concat_value);
1535 }
1536 oval_value_iterator_free(values);
1537 oval_collection_free_items(subcoll, (oscap_destruct_func) oval_value_free);
1538 }
1539 oval_component_iterator_free(subcomps);
1540 } else {
1541 oscap_seterr(OSCAP_EFAMILY_OVAL, "No prefix specified for component evaluation");
1542 }
1543 return flag;
1544 }
1545
1546 static oval_syschar_collection_flag_t _oval_component_evaluate_END(oval_argu_t *argu,
1547 struct oval_component *component,
1548 struct oval_collection *value_collection) {
1549 oval_syschar_collection_flag_t flag = SYSCHAR_FLAG_ERROR;
1550 char *suffix = oval_component_get_suffix(component);
1551 if (suffix) {
1552 int len_suffix = strlen(suffix);
1553 struct oval_component_iterator *subcomps = oval_component_get_function_components(component);
1554 if (oval_component_iterator_has_more(subcomps)) { /*only the first component is processed */
1555 struct oval_collection *subcoll = oval_collection_new();
1556 struct oval_component *subcomp = oval_component_iterator_next(subcomps);
1557 flag = oval_component_eval_common(argu, subcomp, subcoll);
1558 struct oval_value_iterator *values =
1559 (struct oval_value_iterator *)oval_collection_iterator(subcoll);
1560 while (oval_value_iterator_has_more(values)) {
1561 char *key = oval_value_get_text(oval_value_iterator_next(values));
1562 int len_key = strlen(key);
1563 const size_t concat_len = len_suffix + len_key + 1;
1564 char *concat = malloc(concat_len);
1565 if ((len_suffix > len_key) || strncmp(suffix, key + len_key - len_suffix, len_suffix)) {
1566 snprintf(concat, concat_len, "%s%s", key, suffix);
1567 } else {
1568 snprintf(concat, concat_len, "%s", key);
1569 }
1570 struct oval_value *concat_value = oval_value_new(OVAL_DATATYPE_STRING, concat);
1571 free(concat);
1572 oval_collection_add(value_collection, concat_value);
1573 }
1574 oval_value_iterator_free(values);
1575 oval_collection_free_items(subcoll, (oscap_destruct_func) oval_value_free);
1576 }
1577 oval_component_iterator_free(subcomps);
1578 } else {
1579 oscap_seterr(OSCAP_EFAMILY_OVAL, "No suffix specified for component evaluation");
1580 }
1581 return flag;
1582 }
1583
1584 static oval_syschar_collection_flag_t _oval_component_evaluate_CONCAT(oval_argu_t *argu,
1585 struct oval_component *component,
1586 struct oval_collection *value_collection)
1587 {
1588 int idx0 = 0;
1589
1590 oval_syschar_collection_flag_t flag = SYSCHAR_FLAG_UNKNOWN;
1591 struct oval_component_iterator *subcomps = oval_component_get_function_components(component);
1592 int len_subcomps = oval_component_iterator_remaining(subcomps);
1593 struct oval_collection **component_colls = malloc(len_subcomps * sizeof(struct oval_collection *));
1594 for (idx0 = 0; oval_component_iterator_has_more(subcomps); idx0++) {
1595 struct oval_collection *subcoll = oval_collection_new();
1596 struct oval_component *subcomp = oval_component_iterator_next(subcomps);
1597 oval_syschar_collection_flag_t subflag = oval_component_eval_common(argu, subcomp, subcoll);
1598 flag = _AGG_FLAG(flag, subflag);
1599 component_colls[idx0] = subcoll;
1600 }
1601 bool not_finished = (len_subcomps > 0) && _HAS_VALUES(flag);
1602 if (not_finished) {
1603 struct oval_value_iterator **values = malloc(len_subcomps * sizeof(struct oval_value_iterator *));
1604 /* bool has_some[len_subcomps]; <-- unused variable */
1605 not_finished = false;
1606 char **texts = malloc(len_subcomps * sizeof(char *));
1607 int *counts = malloc(len_subcomps * sizeof(int));
1608 int catnum = 1;
1609 for (idx0 = 0; idx0 < len_subcomps; idx0++) {
1610 struct oval_value_iterator *comp_values =
1611 (struct oval_value_iterator *)oval_collection_iterator(component_colls[idx0]);
1612 counts[idx0] = oval_value_iterator_remaining(comp_values);
1613 if (counts[idx0]) {
1614 /* int dbgnum = catnum; <-- unused variable */
1615 catnum = catnum * counts[idx0];
1616 values[idx0] = comp_values;
1617 texts[idx0] = oval_value_get_text(oval_value_iterator_next(comp_values));
1618 not_finished = true;
1619 } else {
1620 oval_value_iterator_free(comp_values);
1621 oval_collection_free_items(component_colls[idx0],
1622 (oscap_destruct_func) oval_value_free);
1623 component_colls[idx0] = NULL;
1624 values[idx0] = NULL;
1625 texts[idx0] = NULL;
1626 }
1627 }
1628 for (int passnum = 1; passnum - 1 < catnum; passnum++) {
1629 int len_cat = 1;
1630 for (idx0 = 0; idx0 < len_subcomps; idx0++)
1631 if (texts[idx0])
1632 len_cat += strlen(texts[idx0]);
1633 char *concat = malloc(len_cat);
1634 *concat = '\0';
1635 for (idx0 = 0; idx0 < len_subcomps; idx0++)
1636 if (texts[idx0])
1637 strcat(concat, texts[idx0]);
1638 struct oval_value *value = oval_value_new(OVAL_DATATYPE_STRING, concat);
1639 free(concat);
1640 oval_collection_add(value_collection, value);
1641 bool rotate = true;
1642 if (passnum < catnum) {
1643 for (idx0 = 0; idx0 < len_subcomps && rotate; idx0++) {
1644 if (texts[idx0]) {
1645 if (oval_value_iterator_has_more(values[idx0])) {
1646 rotate = false;
1647 } else {
1648 oval_value_iterator_free(values[idx0]);
1649 values[idx0] =
1650 (struct oval_value_iterator *)
1651 oval_collection_iterator(component_colls[idx0]);
1652 }
1653 texts[idx0] =
1654 oval_value_get_text(oval_value_iterator_next(values[idx0]));
1655 }
1656 }
1657 }
1658 }
1659 for (idx0 = 0; idx0 < len_subcomps; idx0++) {
1660 if (counts[idx0]) {
1661 oval_value_iterator_free(values[idx0]);
1662 oval_collection_free_items(component_colls[idx0],
1663 (oscap_destruct_func) oval_value_free);
1664 }
1665 }
1666 free(counts);
1667 free(texts);
1668 free(values);
1669 } else {
1670 for (idx0 = 0; idx0 < len_subcomps; ++idx0)
1671 oval_collection_free_items(component_colls[idx0], (oscap_destruct_func) oval_value_free);
1672 }
1673 free(component_colls);
1674 oval_component_iterator_free(subcomps);
1675 return flag;
1676 }
1677
1678 static oval_syschar_collection_flag_t _oval_component_evaluate_COUNT(oval_argu_t *argu,
1679 struct oval_component *component,
1680 struct oval_collection *value_collection)
1681 {
1682 int count = 0;
1683 int idx0 = 0;
1684
1685 oval_syschar_collection_flag_t flag = SYSCHAR_FLAG_UNKNOWN;
1686 struct oval_component_iterator *subcomps = oval_component_get_function_components(component);
1687 int len_subcomps = oval_component_iterator_remaining(subcomps);
1688 struct oval_collection **component_colls = malloc(len_subcomps * sizeof(struct oval_collection *));
1689 for (idx0 = 0; oval_component_iterator_has_more(subcomps); idx0++) {
1690 struct oval_collection *subcoll = oval_collection_new();
1691 struct oval_component *subcomp = oval_component_iterator_next(subcomps);
1692 oval_syschar_collection_flag_t subflag = oval_component_eval_common(argu, subcomp, subcoll);
1693 flag = _AGG_FLAG(flag, subflag);
1694 component_colls[idx0] = subcoll;
1695 }
1696 bool not_finished = (len_subcomps > 0) && _HAS_VALUES(flag);
1697 if (not_finished) {
1698 for (idx0 = 0; idx0 < len_subcomps; idx0++) {
1699 struct oval_value_iterator *comp_values =
1700 (struct oval_value_iterator *)oval_collection_iterator(component_colls[idx0]);
1701 while (oval_value_iterator_has_more(comp_values)) {
1702 if (oval_value_get_text(oval_value_iterator_next(comp_values)))
1703 count++;
1704 }
1705 oval_value_iterator_free(comp_values);
1706
1707 }
1708 }
1709 char count_str[128];
1710 snprintf(count_str, sizeof(count_str), "%d", count);
1711 struct oval_value *value = oval_value_new(OVAL_DATATYPE_INTEGER, count_str);
1712 oval_collection_add(value_collection, value);
1713
1714 oval_component_iterator_free(subcomps);
1715
1716 for (idx0 = 0; idx0 < len_subcomps; ++idx0)
1717 oval_collection_free_items(component_colls[idx0], (oscap_destruct_func) oval_value_free);
1718
1719 free(component_colls);
1720 return flag;
1721 }
1722
1723 static oval_syschar_collection_flag_t _oval_component_evaluate_UNIQUE(oval_argu_t *argu,
1724 struct oval_component *component,
1725 struct oval_collection *value_collection)
1726 {
1727 int idx0 = 0;
1728 oval_syschar_collection_flag_t flag = SYSCHAR_FLAG_UNKNOWN;
1729 struct oval_component_iterator *subcomps = oval_component_get_function_components(component);
1730 int len_subcomps = oval_component_iterator_remaining(subcomps);
1731 struct oval_collection **component_colls = malloc(len_subcomps * sizeof(struct oval_collection *));
1732
1733 for (idx0 = 0; oval_component_iterator_has_more(subcomps); idx0++) {
1734 struct oval_collection *subcoll = oval_collection_new();
1735 struct oval_component *subcomp = oval_component_iterator_next(subcomps);
1736 oval_syschar_collection_flag_t subflag = oval_component_eval_common(argu, subcomp, subcoll);
1737 flag = _AGG_FLAG(flag, subflag);
1738 component_colls[idx0] = subcoll;
1739 }
1740
1741 oval_component_iterator_free(subcomps);
1742
1743 bool not_finished = (len_subcomps > 0) && _HAS_VALUES(flag);
1744 struct oval_string_map *valmap = oval_string_map_new();
1745
1746 if (not_finished) {
1747 for (idx0 = 0; idx0 < len_subcomps; idx0++) {
1748 struct oval_value_iterator *comp_values =
1749 (struct oval_value_iterator *)oval_collection_iterator(component_colls[idx0]);
1750
1751 while (oval_value_iterator_has_more(comp_values)) {
1752 char *valtxt;
1753 if ((valtxt = oval_value_get_text(oval_value_iterator_next(comp_values))) != NULL) {
1754 if (oval_string_map_get_value(valmap, valtxt) == NULL) {
1755 struct oval_value *value = oval_value_new(OVAL_DATATYPE_STRING, valtxt);
1756 oval_string_map_put(valmap, valtxt, value);
1757 }
1758 }
1759 }
1760 oval_value_iterator_free(comp_values);
1761 }
1762 }
1763
1764 oval_string_map_collect_values(valmap, value_collection);
1765 oval_string_map_free(valmap, NULL);
1766
1767 for (idx0 = 0; idx0 < len_subcomps; ++idx0)
1768 oval_collection_free_items(component_colls[idx0], (oscap_destruct_func) oval_value_free);
1769
1770 free(component_colls);
1771 return flag;
1772 }
1773
1774 static oval_syschar_collection_flag_t _oval_component_evaluate_SPLIT(oval_argu_t *argu,
1775 struct oval_component *component,
1776 struct oval_collection *value_collection)
1777 {
1778
1779 oval_syschar_collection_flag_t flag = SYSCHAR_FLAG_UNKNOWN;
1780 struct oval_component_iterator *subcomps = oval_component_get_function_components(component);
1781 char *delimiter = oval_component_get_split_delimiter(component);
1782 int len_delim = strlen(delimiter);
1783 if (oval_component_iterator_has_more(subcomps)) { /* Only first component is considered */
1784 struct oval_component *subcomp = oval_component_iterator_next(subcomps);
1785 struct oval_collection *subcoll = oval_collection_new();
1786 flag = oval_component_eval_common(argu, subcomp, subcoll);
1787 struct oval_value_iterator *values = (struct oval_value_iterator *)oval_collection_iterator(subcoll);
1788 struct oval_value *value;
1789 while (oval_value_iterator_has_more(values)) {
1790 char *text = oval_value_get_text(oval_value_iterator_next(values));
1791 if (len_delim) {
1792 char *split = malloc(strlen(text) + 2);
1793 char *split0 = split;
1794 *split0 = '\0';
1795 strcat(split0, text);
1796 split0[strlen(text) + 1] = '\0'; /*last two characters are EOS */
1797 char *split1;
1798 for (split1 = strstr(split0, delimiter); split1; split1 = strstr(split0, delimiter)) {
1799 *split1 = '\0'; /*terminate the text at the delimeter */
1800 value = oval_value_new(OVAL_DATATYPE_STRING, split0);
1801 oval_collection_add(value_collection, value);
1802 split0 = split1 + len_delim; /*advance split1 */
1803 }
1804 value = oval_value_new(OVAL_DATATYPE_STRING, split0);
1805 oval_collection_add(value_collection, value);
1806 free(split);
1807 } else { /*Empty delimiter, Split at every character */
1808 char split[] = { '\0', '\0' };
1809 int idx;
1810 for (idx = 0; text[idx]; idx++) {
1811 *split = text[idx];
1812 value = oval_value_new(OVAL_DATATYPE_STRING, split);
1813 oval_collection_add(value_collection, value);
1814 }
1815 }
1816 }
1817 oval_value_iterator_free(values);
1818 oval_collection_free_items(subcoll, (oscap_destruct_func) oval_value_free);
1819 }
1820 oval_component_iterator_free(subcomps);
1821 return flag;
1822 };
1823
1824 static oval_syschar_collection_flag_t _oval_component_evaluate_SUBSTRING(oval_argu_t *argu,
1825 struct oval_component *component,
1826 struct oval_collection *value_collection)
1827 {
1828 oval_syschar_collection_flag_t flag = SYSCHAR_FLAG_UNKNOWN;
1829 struct oval_component_iterator *subcomps = oval_component_get_function_components(component);
1830 ssize_t beg = oval_component_get_substring_start(component);
1831 ssize_t len = oval_component_get_substring_length(component);
1832
1833 beg = (beg < 1) ? 0 : beg - 1;
1834
1835 if (oval_component_iterator_has_more(subcomps)) { /*Only first component is considered */
1836 struct oval_component *subcomp = oval_component_iterator_next(subcomps);
1837 struct oval_collection *subcoll = oval_collection_new();
1838 flag = oval_component_eval_common(argu, subcomp, subcoll);
1839 struct oval_value_iterator *values = (struct oval_value_iterator *)oval_collection_iterator(subcoll);
1840 struct oval_value *value;
1841
1842 while (oval_value_iterator_has_more(values)) {
1843 char *text = oval_value_get_text(oval_value_iterator_next(values));
1844 size_t txtlen, sublen;
1845
1846 txtlen = strlen(text);
1847 sublen = (len < 0 || (size_t) len > txtlen) ? txtlen : (size_t) len;
1848 if ((size_t) beg < txtlen) {
1849 char *substr = malloc(sublen + 1);
1850
1851 strncpy(substr, text + beg, sublen);
1852 substr[sublen] = '\0';
1853
1854 value = oval_value_new(OVAL_DATATYPE_STRING, substr);
1855 free(substr);
1856 oval_collection_add(value_collection, value);
1857 } else {
1858 flag = SYSCHAR_FLAG_ERROR;
1859 }
1860 }
1861 oval_value_iterator_free(values);
1862 oval_collection_free_items(subcoll, (oscap_destruct_func) oval_value_free);
1863 }
1864 oval_component_iterator_free(subcomps);
1865 return flag;
1866 }
1867
1868 static long unsigned int _comp_sec(int year, int month, int day, int hour, int minute, int second)
1869 {
1870 time_t t;
1871 struct tm *ts;
1872 struct tm result;
1873
1874 t = time(NULL);
1875 ts = localtime_r(&t, &result);
1876
1877 ts->tm_year = year - 1900;
1878 ts->tm_mon = month - 1;
1879 ts->tm_mday = day;
1880 ts->tm_hour = hour;
1881 ts->tm_min = minute;
1882 ts->tm_sec = second;
1883 ts->tm_isdst = -1;
1884 t = mktime(ts);
1885 ts = localtime_r(&t, &result);
1886
1887 if (ts->tm_isdst == 1)
1888 t -= 3600;
1889
1890 return (long unsigned int) t;
1891 }
1892
1893 /*
1894 * year_month_day
1895 * The year_month_day value specifies date-time strings that follow the formats:
1896 *
1897 * strptime(3)
1898 */
1899 static const char *_dtfmt_ymd[] = {
1900 "%Y%m%d", /* yyyymmdd */
1901 "%Y/%m/%d", /* yyyy/mm/dd */
1902 "%Y-%m-%d", /* yyyy-mm-dd */
1903 "%Y%m%dT%H%M%S", /* yyyymmddThhmmss */
1904 "%Y/%m/%d%n%T", /* yyyy/mm/dd hh:mm:ss */
1905 "%Y-%m-%d%n%T" /* yyyy-mm-dd hh:mm:ss */
1906 };
1907
1908 /*
1909 * month_day_year
1910 * The month_day_year value specifies date-time strings that follow the formats:
1911 *
1912 * strptime(3)
1913 */
1914 static const char *_dtfmt_mdy[] = {
1915 "%m/%d/%Y", /* mm/dd/yyyy */
1916 "%m-%d-%Y", /* mm-dd-yyyy */
1917 "%b,%n%d%n%Y", /* AbreviatedNameOfMonth, dd yyyy */
1918 "%m/%d/%Y%n%T", /* mm/dd/yyyy hh:mm:ss */
1919 "%m-%d-%Y%n%T", /* mm-dd-yyyy hh:mm:ss */
1920 "%b,%n%d%n%Y%n%T" /* NameOfMonth, dd yyyy hh:mm:ss & AbreviatedNameOfMonth, dd yyyy hh:mm:ss */
1921 };
1922
1923 /*
1924 * day_month_year
1925 * The day_month_year value specifies date-time strings that follow the formats:
1926 *
1927 * strptime(3)
1928 */
1929 static const char *_dtfmt_dmy[] = {
1930 "%d/%m/%Y", /* dd/mm/yyyy */
1931 "%d-%m-%Y", /* dd-mm-yyyy */
1932 "%d/%m/%Y%n%T", /* dd/mm/yyyy hh:mm:ss */
1933 "%d-%m-%Y%n%T" /* dd-mm-yyyy hh:mm:ss */
1934 };
1935
1936 static long unsigned int _parse_datetime(char *datetime, const char *fmt[], size_t fmtcnt)
1937 {
1938 struct tm t;
1939 size_t i;
1940 char *r;
1941
1942 dD("Parsing datetime string \"%s\"", datetime);
1943
1944 for (i = 0; i < fmtcnt; ++i) {
1945 dD("%s", fmt[i]);
1946 memset(&t, 0, sizeof t);
1947 r = strptime(datetime, fmt[i], &t);
1948
1949 if (r != NULL) {
1950 if (*r == '\0') {
1951 dD("Success!");
1952 return _comp_sec(t.tm_year, t.tm_mon, t.tm_mday,
1953 t.tm_hour, t.tm_min, t.tm_sec);
1954 }
1955 }
1956 }
1957
1958 dE("Unable to interpret \"%s\" as a datetime string", datetime);
1959
1960 return (0);
1961 }
1962
1963 static long unsigned int _parse_fmt_win(char *dt)
1964 {
1965 // todo
1966 return 0;
1967 }
1968
1969 static long unsigned int _parse_fmt_sse(char *dt)
1970 {
1971 time_t t;
1972 struct tm *ts;
1973 struct tm result;
1974
1975 t = (time_t) atol(dt);
1976 ts = localtime_r(&t, &result);
1977 if (ts->tm_isdst == 1)
1978 t -= 3600;
1979
1980 return (long unsigned int) t;
1981 }
1982
1983 static bool _match(const char *pattern, const char *string)
1984 {
1985 bool match = false;
1986 oscap_pcre_t *re;
1987 char *error;
1988 int erroffset = -1, ovector[60], ovector_len = sizeof (ovector) / sizeof (ovector[0]);
1989 re = oscap_pcre_compile(pattern, OSCAP_PCRE_OPTS_UTF8, &error, &erroffset);
1990 if (re == NULL) {
1991 oscap_pcre_err_free(error);
1992 return false;
1993 }
1994 match = (oscap_pcre_exec(re, string, strlen(string), 0, 0, ovector, ovector_len) >= 0);
1995 oscap_pcre_free(re);
1996 return match;
1997 }
1998
1999 static long unsigned int _parse_fmt_cim(char *dt)
2000 {
2001 const char *pattern1 = "^[0-9]{14}\\.[0-9]{6}[+-][0-9]{3}$"; // yyyymmddHHMMSS.mmmmmmsUUU
2002 const char *fmt_str1 = "%4u%2u%2u%2u%2u%2u.%*6u%*[+-]%*3u";
2003 const char *pattern2 = "^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{3}$"; // yyyy-mm-dd HH:MM:SS:mmm
2004 const char *fmt_str2 = "%4d-%2d-%2d %2d:%2d:%2d:%*3d";
2005 const char *pattern3 = "^[0-9]{2}-[0-9]{2}-[0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{3}$"; // mm-dd-yyyy hh:mm:ss:mmm
2006 const char *fmt_str3 = "%2d-%2d-%4d %2d:%2d:%2d:%*3d";
2007 int year, month, day, hour, minute, second, r = 0;
2008 const int number_of_items = 6;
2009 if (_match(pattern1, dt)) {
2010 r = sscanf(dt, fmt_str1, &year, &month, &day, &hour, &minute, &second);
2011 } else if (_match(pattern2, dt)) {
2012 r = sscanf(dt, fmt_str2, &year, &month, &day, &hour, &minute, &second);
2013 } else if (_match(pattern3, dt)) {
2014 r = sscanf(dt, fmt_str3, &month, &day, &year, &hour, &minute, &second);
2015 }
2016 if (r != number_of_items) {
2017 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unable to interpret \"%s\" as a cim_datetime string\n", dt);
2018 return 0;
2019 }
2020 return _comp_sec(year, month, day, hour, minute, second);
2021 }
2022
2023 static long unsigned int _parse_fmt(struct oval_value *val, oval_datetime_format_t fmt)
2024 {
2025 long unsigned int v = 0;
2026 char *sv;
2027
2028 sv = oval_value_get_text(val);
2029
2030 switch (fmt) {
2031 case OVAL_DATETIME_YEAR_MONTH_DAY:
2032 v = _parse_datetime(sv, _dtfmt_ymd, sizeof _dtfmt_ymd/sizeof(char *));
2033 break;
2034 case OVAL_DATETIME_MONTH_DAY_YEAR:
2035 v = _parse_datetime(sv, _dtfmt_mdy, sizeof _dtfmt_mdy/sizeof(char *));
2036 break;
2037 case OVAL_DATETIME_DAY_MONTH_YEAR:
2038 v = _parse_datetime(sv, _dtfmt_dmy, sizeof _dtfmt_dmy/sizeof(char *));
2039 break;
2040 case OVAL_DATETIME_WIN_FILETIME:
2041 v = _parse_fmt_win(sv);
2042 break;
2043 case OVAL_DATETIME_SECONDS_SINCE_EPOCH:
2044 v = _parse_fmt_sse(sv);
2045 break;
2046 case OVAL_DATETIME_CIM_DATETIME:
2047 v = _parse_fmt_cim(sv);
2048 break;
2049 default:
2050 break;
2051 }
2052
2053 return v;
2054 }
2055
2056 static oval_syschar_collection_flag_t _oval_component_evaluate_TIMEDIF(oval_argu_t *argu,
2057 struct oval_component *component,
2058 struct oval_collection *value_collection)
2059 {
2060 oval_syschar_collection_flag_t flag = SYSCHAR_FLAG_UNKNOWN;
2061 struct oval_component_iterator *subcomps;
2062 int subcomp_idx;
2063 struct oval_collection *val_cols[2];
2064 struct oval_value_iterator *v1_itr;
2065 oval_datetime_format_t fmt1, fmt2;
2066
2067 subcomps = oval_component_get_function_components(component);
2068 subcomp_idx = 0;
2069 while (oval_component_iterator_has_more(subcomps) && subcomp_idx < 2) {
2070 struct oval_component *subcomp;
2071
2072 subcomp = oval_component_iterator_next(subcomps);
2073 val_cols[subcomp_idx] = oval_collection_new();
2074 flag = oval_component_eval_common(argu, subcomp, val_cols[subcomp_idx]);
2075 subcomp_idx++;
2076 }
2077
2078 if (oval_component_iterator_has_more(subcomps)) {
2079 oval_component_iterator_free(subcomps);
2080 oval_collection_free_items(val_cols[0], (oscap_destruct_func) oval_value_free);
2081 oval_collection_free_items(val_cols[1], (oscap_destruct_func) oval_value_free);
2082 return SYSCHAR_FLAG_ERROR;
2083 }
2084 oval_component_iterator_free(subcomps);
2085 if (subcomp_idx == 0) {
2086 return SYSCHAR_FLAG_ERROR;
2087 }
2088
2089 fmt1 = oval_component_get_timedif_format_1(component);
2090 fmt2 = oval_component_get_timedif_format_2(component);
2091
2092 if (subcomp_idx == 1) {
2093 struct oval_value *ov;
2094 char ts[16];
2095
2096 val_cols[1] = val_cols[0];
2097 val_cols[0] = oval_collection_new();
2098 snprintf(ts, sizeof (ts), "%lu", (long unsigned int) time(NULL));
2099 ov = oval_value_new(OVAL_DATATYPE_INTEGER, ts);
2100 oval_collection_add(val_cols[0], ov);
2101 fmt1 = OVAL_DATETIME_SECONDS_SINCE_EPOCH;
2102 }
2103
2104 v1_itr = (struct oval_value_iterator *) oval_collection_iterator(val_cols[0]);
2105 while (oval_value_iterator_has_more(v1_itr)) {
2106 struct oval_value *ov1;
2107 struct oval_value_iterator *v2_itr;
2108
2109 ov1 = oval_value_iterator_next(v1_itr);
2110
2111 v2_itr = (struct oval_value_iterator *) oval_collection_iterator(val_cols[1]);
2112 while (oval_value_iterator_has_more(v2_itr)) {
2113 long unsigned int v;
2114 char ts[16];
2115 struct oval_value *ov2, *ov;
2116
2117 ov2 = oval_value_iterator_next(v2_itr);
2118 v = _parse_fmt(ov1, fmt1) - _parse_fmt(ov2, fmt2);
2119 snprintf(ts, sizeof (ts), "%lu", v);
2120 ov = oval_value_new(OVAL_DATATYPE_INTEGER, ts);
2121 oval_collection_add(value_collection, ov);
2122 }
2123 oval_value_iterator_free(v2_itr);
2124 }
2125 oval_value_iterator_free(v1_itr);
2126
2127 oval_collection_free_items(val_cols[0], (oscap_destruct_func) oval_value_free);
2128 oval_collection_free_items(val_cols[1], (oscap_destruct_func) oval_value_free);
2129
2130 return flag;
2131 }
2132
2133 static bool _isEscape(char chr)
2134 {
2135 const char *regex_chars = "^$\\.[](){}*+?|";
2136
2137 return (strchr(regex_chars, chr) == NULL) ? false : true;
2138 }
2139
2140 static oval_syschar_collection_flag_t _oval_component_evaluate_ESCAPE_REGEX(oval_argu_t *argu,
2141 struct oval_component *component,
2142 struct oval_collection *value_collection)
2143 {
2144 oval_syschar_collection_flag_t flag = SYSCHAR_FLAG_UNKNOWN;
2145 struct oval_component_iterator *subcomps = oval_component_get_function_components(component);
2146
2147 if (oval_component_iterator_has_more(subcomps)) { //Only first component is considered
2148 struct oval_component *subcomp = oval_component_iterator_next(subcomps);
2149 struct oval_collection *subcoll = oval_collection_new();
2150 flag = oval_component_eval_common(argu, subcomp, subcoll);
2151 struct oval_value_iterator *values = (struct oval_value_iterator *)oval_collection_iterator(subcoll);
2152 while (oval_value_iterator_has_more(values)) {
2153 struct oval_value *value = oval_value_iterator_next(values);
2154 char *text = oval_value_get_text(value);
2155 int len = strlen(text);
2156 char *string = malloc(2 * len + 1);
2157 char *insert = string;
2158 while (*text) {
2159 if (_isEscape(*text))
2160 *insert++ = '\\';
2161 *insert++ = *text++;
2162 }
2163 *insert = '\0';
2164 value = oval_value_new(OVAL_DATATYPE_STRING, string);
2165 free(string);
2166 oval_collection_add(value_collection, value);
2167 }
2168 oval_value_iterator_free(values);
2169 oval_collection_free_items(subcoll, (oscap_destruct_func) oval_value_free);
2170 }
2171 oval_component_iterator_free(subcomps);
2172 return flag;
2173 }
2174
2175
2176
2177
2178
2179 static oval_syschar_collection_flag_t _oval_component_evaluate_GLOB_TO_REGEX(oval_argu_t *argu,
2180 struct oval_component *component,
2181 struct oval_collection *value_collection)
2182 {
2183 oval_syschar_collection_flag_t flag = SYSCHAR_FLAG_UNKNOWN;
2184 bool glob_noescape = oval_component_get_glob_to_regex_glob_noescape(component);
2185 struct oval_component_iterator *subcomps = oval_component_get_function_components(component);
2186 if (oval_component_iterator_has_more(subcomps)) { //Only first component is considered
2187 struct oval_component *subcomp = oval_component_iterator_next(subcomps);
2188 struct oval_collection *subcoll = oval_collection_new();
2189 flag = oval_component_eval_common(argu, subcomp, subcoll);
2190 struct oval_value_iterator *values = (struct oval_value_iterator *)oval_collection_iterator(subcoll);
2191 while (oval_value_iterator_has_more(values)) {
2192 struct oval_value *value = oval_value_iterator_next(values);
2193 char *text = oval_value_get_text(value);
2194 char *string = oval_glob_to_regex(text, glob_noescape);
2195 if (string == NULL) {
2196 flag = SYSCHAR_FLAG_ERROR;
2197 break;
2198 }
2199 value = oval_value_new(OVAL_DATATYPE_STRING, string);
2200 free(string);
2201 oval_collection_add(value_collection, value);
2202 }
2203 oval_value_iterator_free(values);
2204 oval_collection_free_items(subcoll, (oscap_destruct_func) oval_value_free);
2205 }
2206 oval_component_iterator_free(subcomps);
2207 return flag;
2208 }
2209
2210 static oval_syschar_collection_flag_t _oval_component_evaluate_REGEX_CAPTURE(oval_argu_t *argu,
2211 struct oval_component *component,
2212 struct oval_collection *value_collection)
2213 {
2214 oval_syschar_collection_flag_t flag = SYSCHAR_FLAG_UNKNOWN;
2215 int rc;
2216 char *pattern;
2217 int erroffset = -1;
2218 oscap_pcre_t *re = NULL;
2219 char *error;
2220
2221 pattern = oval_component_get_regex_pattern(component);
2222 re = oscap_pcre_compile(pattern, OSCAP_PCRE_OPTS_UTF8, &error, &erroffset);
2223 if (re == NULL) {
2224 dE("oscap_pcre_compile() failed: \"%s\".", error);
2225 oscap_pcre_err_free(error);
2226 return SYSCHAR_FLAG_ERROR;
2227 }
2228
2229 struct oval_component_iterator *subcomps = oval_component_get_function_components(component);
2230 if (oval_component_iterator_has_more(subcomps)) { //Only first component is considered
2231 struct oval_component *subcomp = oval_component_iterator_next(subcomps);
2232 struct oval_collection *subcoll = oval_collection_new();
2233 flag = oval_component_eval_common(argu, subcomp, subcoll);
2234 struct oval_value_iterator *values = (struct oval_value_iterator *)oval_collection_iterator(subcoll);
2235 while (oval_value_iterator_has_more(values)) {
2236 struct oval_value *value = oval_value_iterator_next(values);
2237 char *text = oval_value_get_text(value);
2238 char *nval = NULL;
2239 int i, ovector[60], ovector_len = sizeof (ovector) / sizeof (ovector[0]);
2240
2241 for (i = 0; i < ovector_len; ++i)
2242 ovector[i] = -1;
2243
2244 rc = oscap_pcre_exec(re, text, strlen(text), 0, 0, ovector, ovector_len);
2245 if (rc < -1) {
2246 dE("oscap_pcre_exec() failed: %d.", rc);
2247 flag = SYSCHAR_FLAG_ERROR;
2248 break;
2249 }
2250
2251 if (rc > 1 && ovector[2] != -1) {
2252 int substr_len = ovector[3] - ovector[2];
2253
2254 nval = malloc(substr_len + 1);
2255 memcpy(nval, text + ovector[2], substr_len);
2256 nval[substr_len] = '\0';
2257 } else {
2258 nval = NULL;
2259 }
2260 flag = SYSCHAR_FLAG_COMPLETE;
2261
2262 if (nval != NULL) {
2263 value = oval_value_new(OVAL_DATATYPE_STRING, nval);
2264 free(nval);
2265 } else {
2266 value = oval_value_new(OVAL_DATATYPE_STRING, "");
2267 }
2268 oval_collection_add(value_collection, value);
2269 }
2270 oval_value_iterator_free(values);
2271 oval_collection_free_items(subcoll, (oscap_destruct_func) oval_value_free);
2272 }
2273 oval_component_iterator_free(subcomps);
2274 oscap_pcre_free(re);
2275 return flag;
2276 }
2277
2278 struct val_col_lst_s {
2279 void *val_col;
2280 struct val_col_lst_s *next;
2281 };
2282
2283 static oval_syschar_collection_flag_t _oval_component_evaluate_ARITHMETIC_rec(struct val_col_lst_s *val_col_lst, double val,
2284 oval_datatype_t datatype, oval_arithmetic_operation_t op,
2285 struct oval_collection *res_val_col)
2286 {
2287 struct oval_value_iterator *val_itr;
2288
2289 if (val_col_lst == NULL) {
2290 struct oval_value *ov;
2291 char sv[32];
2292
2293 memset(sv, 0, sizeof (sv));
2294
2295 if (datatype == OVAL_DATATYPE_INTEGER) {
2296 snprintf(sv, sizeof (sv), "%ld", (long int) val);
2297 } else if (datatype == OVAL_DATATYPE_FLOAT) {
2298 snprintf(sv, sizeof (sv), "%f", val);
2299 }
2300 ov = oval_value_new(datatype, sv);
2301 oval_collection_add(res_val_col, ov);
2302
2303 return SYSCHAR_FLAG_COMPLETE;
2304 }
2305
2306 val_itr = (struct oval_value_iterator *) oval_collection_iterator(val_col_lst->val_col);
2307 while (oval_value_iterator_has_more(val_itr)) {
2308 struct oval_value *ov;
2309 oval_datatype_t dt;
2310 double new_val;
2311
2312 ov = oval_value_iterator_next(val_itr);
2313 dt = oval_value_get_datatype(ov);
2314 if (dt == OVAL_DATATYPE_STRING) {
2315 errno = 0; // Setting errno to 0 as suggested by strtod() manpage, as 0 is used both on success and failure
2316 new_val = strtod(oval_value_get_text(ov), NULL);
2317 if (errno) {
2318 oscap_seterr(OSCAP_EFAMILY_OVAL, "Unexpected content: %s.", oval_value_get_text(ov));
2319 oval_value_iterator_free(val_itr);
2320 return SYSCHAR_FLAG_ERROR;
2321 }
2322 } else if (dt == OVAL_DATATYPE_INTEGER) {
2323 new_val = (double) oval_value_get_integer(ov);
2324 } else if (dt == OVAL_DATATYPE_FLOAT) {
2325 new_val = (double) oval_value_get_float(ov);
2326 } else {
2327 oscap_seterr(OSCAP_EFAMILY_OVAL, "Unexpected value type: %s.", oval_datatype_get_text(dt));
2328 oval_value_iterator_free(val_itr);
2329 return SYSCHAR_FLAG_ERROR;
2330 }
2331
2332 if (op == OVAL_ARITHMETIC_ADD) {
2333 new_val += val;
2334 } else if (op == OVAL_ARITHMETIC_MULTIPLY) {
2335 new_val *= val;
2336 } else {
2337 oscap_seterr(OSCAP_EFAMILY_OVAL, "Unexpected arithmetic operation: %s.", oval_arithmetic_operation_get_text(op));
2338 oval_value_iterator_free(val_itr);
2339 return SYSCHAR_FLAG_ERROR;
2340 }
2341
2342 if (datatype == OVAL_DATATYPE_FLOAT)
2343 dt = OVAL_DATATYPE_FLOAT;
2344 _oval_component_evaluate_ARITHMETIC_rec(val_col_lst->next, new_val, dt, op, res_val_col);
2345 }
2346 oval_value_iterator_free(val_itr);
2347
2348 return SYSCHAR_FLAG_COMPLETE;
2349 }
2350
2351 static oval_syschar_collection_flag_t _oval_component_evaluate_ARITHMETIC(oval_argu_t *argu,
2352 struct oval_component *component,
2353 struct oval_collection *value_collection)
2354 {
2355 oval_syschar_collection_flag_t flag = SYSCHAR_FLAG_UNKNOWN;
2356 struct oval_component_iterator *subcomps;
2357 struct val_col_lst_s *vcl_root, *vcl_elm;
2358 oval_arithmetic_operation_t op;
2359 struct oval_value_iterator *val_itr;
2360
2361 op = oval_component_get_arithmetic_operation(component);
2362 if (op != OVAL_ARITHMETIC_ADD && op != OVAL_ARITHMETIC_MULTIPLY) {
2363 oscap_seterr(OSCAP_EFAMILY_OVAL, "Unexpected arithmetic operation: %s.", oval_arithmetic_operation_get_text(op));
2364 return SYSCHAR_FLAG_ERROR;
2365 }
2366
2367 vcl_root = NULL;
2368 subcomps = oval_component_get_function_components(component);
2369 while (oval_component_iterator_has_more(subcomps)) {
2370 struct oval_component *subcomp;
2371 struct oval_collection *val_col;
2372
2373 subcomp = oval_component_iterator_next(subcomps);
2374 val_col = oval_collection_new();
2375 // todo: combine flags
2376 flag = oval_component_eval_common(argu, subcomp, val_col);
2377 vcl_elm = malloc(sizeof (struct val_col_lst_s));
2378 vcl_elm->val_col = val_col;
2379 vcl_elm->next = vcl_root;
2380 vcl_root = vcl_elm;
2381 }
2382 oval_component_iterator_free(subcomps);
2383
2384 if (vcl_root == NULL) {
2385 return SYSCHAR_FLAG_ERROR;
2386 }
2387 val_itr = (struct oval_value_iterator *) oval_collection_iterator(vcl_root->val_col);
2388 while (oval_value_iterator_has_more(val_itr)) {
2389 struct oval_value *ov;
2390 oval_datatype_t datatype;
2391 double val;
2392
2393 ov = oval_value_iterator_next(val_itr);
2394 datatype = oval_value_get_datatype(ov);
2395 if (datatype == OVAL_DATATYPE_STRING) {
2396 errno = 0; // Setting errno to 0 as suggested by strtod() manpage, as 0 is used both on success and failure
2397 val = strtod(oval_value_get_text(ov), NULL);
2398 if (errno) {
2399 oscap_seterr(OSCAP_EFAMILY_OVAL, "Unexpected content: %s.", oval_value_get_text(ov));
2400 flag = SYSCHAR_FLAG_ERROR;
2401 goto cleanup;
2402 }
2403 } else if (datatype == OVAL_DATATYPE_INTEGER) {
2404 val = (double) oval_value_get_integer(ov);
2405 } else if (datatype == OVAL_DATATYPE_FLOAT) {
2406 val = (double) oval_value_get_float(ov);
2407 } else {
2408 oscap_seterr(OSCAP_EFAMILY_OVAL, "Unexpected value type: %s.", oval_datatype_get_text(datatype));
2409 flag = SYSCHAR_FLAG_ERROR;
2410 goto cleanup;
2411 }
2412
2413 flag = _oval_component_evaluate_ARITHMETIC_rec(vcl_root->next, val, datatype, op, value_collection);
2414 }
2415
2416 cleanup:
2417 oval_value_iterator_free(val_itr);
2418 while (vcl_root != NULL) {
2419 oval_collection_free_items(vcl_root->val_col, (oscap_destruct_func) oval_value_free);
2420 vcl_elm = vcl_root;
2421 vcl_root = vcl_root->next;
2422 free(vcl_elm);
2423 }
2424
2425 return flag;
2426 }
2427
2428 typedef oval_syschar_collection_flag_t(_oval_component_evaluator)
2429 (oval_argu_t *, struct oval_component *, struct oval_collection *);
2430
2431 static _oval_component_evaluator *_component_evaluators[] = {
2432 NULL,
2433 _oval_component_evaluate_LITERAL,
2434 _oval_component_evaluate_OBJECTREF,
2435 _oval_component_evaluate_VARREF,
2436 NULL,
2437 _oval_component_evaluate_BEGIN,
2438 _oval_component_evaluate_CONCAT,
2439 _oval_component_evaluate_END,
2440 _oval_component_evaluate_SPLIT,
2441 _oval_component_evaluate_SUBSTRING,
2442 _oval_component_evaluate_TIMEDIF,
2443 _oval_component_evaluate_ESCAPE_REGEX,
2444 _oval_component_evaluate_REGEX_CAPTURE,
2445 _oval_component_evaluate_ARITHMETIC,
2446 _oval_component_evaluate_COUNT,
2447 _oval_component_evaluate_UNIQUE,
2448 _oval_component_evaluate_GLOB_TO_REGEX,
2449 NULL,
2450 };
2451
2452 static oval_syschar_collection_flag_t oval_component_eval_common(oval_argu_t *argu,
2453 struct oval_component *component,
2454 struct oval_collection *value_collection)
2455 {
2456 __attribute__nonnull__(component);
2457
2458 oval_component_type_t type = component->type;
2459 int evidx = (type > OVAL_FUNCTION) ? type - OVAL_FUNCTION + OVAL_COMPONENT_FUNCTION : type;
2460 _oval_component_evaluator *evaluator = (OVAL_COMPONENT_LITERAL <= type && type <= OVAL_FUNCTION_LAST)
2461 ? _component_evaluators[evidx] : NULL;
2462 oval_syschar_collection_flag_t flag = SYSCHAR_FLAG_ERROR;
2463 if (evaluator) {
2464 flag = (*evaluator) (argu, component, value_collection);
2465 } else {
2466 oscap_seterr(OSCAP_EFAMILY_OVAL, "Component type %d not supported.", type);
2467 }
2468 return flag;
2469 }
2470
2471 oval_syschar_collection_flag_t oval_component_compute(struct oval_syschar_model *sysmod,
2472 struct oval_component *component,
2473 struct oval_collection *value_collection)
2474 {
2475 oval_argu_t argu;
2476
2477 argu.mode = OVAL_MODE_COMPUTE;
2478 argu.u.sysmod = sysmod;
2479
2480 return oval_component_eval_common(&argu, component, value_collection);
2481 }
2482 #if defined(OVAL_PROBES_ENABLED)
2483 oval_syschar_collection_flag_t oval_component_query(oval_probe_session_t *sess,
2484 struct oval_component *component,
2485 struct oval_collection *value_collection)
2486 {
2487 oval_argu_t argu;
2488
2489 argu.mode = OVAL_MODE_QUERY;
2490 argu.u.sess = sess;
2491
2492 return oval_component_eval_common(&argu, component, value_collection);
2493 }
2494 #endif /* OVAL_PROBES_ENABLED */