Debian Codegraph @

src/XCCDF/value.c

raw symbols SCIP
1 /*
2 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Lukas Kuklinek <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <math.h>
28 #include <string.h>
29
30 #include "item.h"
31 #include "elements.h"
32 #include "helpers.h"
33 #include "xccdf_impl.h"
34 #include "common/elements.h"
35 #include "common/debug_priv.h"
36 #include "oscap_helpers.h"
37
38 static struct xccdf_value_instance *xccdf_value_instance_new(xccdf_value_type_t type);
39 static struct xccdf_value_instance *_xccdf_value_get_instance_by_selector_internal(const struct xccdf_value *value, const char *selector);
40
41 struct xccdf_item *xccdf_value_new_internal(struct xccdf_item *parent, xccdf_value_type_t type)
42 {
43 struct xccdf_item *val = xccdf_item_new(XCCDF_VALUE, parent);
44 val->sub.value.type = type;
45 val->sub.value.interface_hint = 0;
46 val->sub.value.oper = 0;
47
48 val->sub.value.instances = oscap_list_new();
49 val->sub.value.sources = oscap_list_new();
50
51 return val;
52 }
53
54 struct xccdf_value *xccdf_value_new(xccdf_value_type_t type)
55 {
56 return XVALUE(xccdf_value_new_internal(NULL, type));
57 }
58
59 struct xccdf_value * xccdf_value_clone(const struct xccdf_value * value)
60 {
61 struct xccdf_item *new_value = calloc(1, sizeof(struct xccdf_item) + sizeof(struct xccdf_value_item));
62 struct xccdf_item *old = XITEM(value);
63 xccdf_item_base_clone(&new_value->item, &old->item);
64 new_value->type = old->type;
65 xccdf_value_item_clone(&new_value->sub.value, &XITEM(value)->sub.value);
66 return XVALUE(new_value);
67 }
68
69 static const struct oscap_string_map XCCDF_VALUE_TYPE_MAP[] = {
70 {XCCDF_TYPE_NUMBER, "number"},
71 {XCCDF_TYPE_STRING, "string"},
72 {XCCDF_TYPE_BOOLEAN, "boolean"},
73 {XCCDF_TYPE_STRING, NULL}
74 };
75
76 static const struct oscap_string_map XCCDF_IFACE_HINT_MAP[] = {
77 {XCCDF_IFACE_HINT_CHOICE, "choice"},
78 {XCCDF_IFACE_HINT_TEXTLINE, "textline"},
79 {XCCDF_IFACE_HINT_TEXT, "text"},
80 {XCCDF_IFACE_HINT_DATE, "date"},
81 {XCCDF_IFACE_HINT_DATETIME, "datetime"},
82 {XCCDF_IFACE_HINT_NONE, NULL}
83 };
84
85 static struct xccdf_value_instance *
86 _xccdf_value_find_or_create_instance(struct xccdf_value *value, const char *selector, xccdf_value_type_t type)
87 {
88 struct xccdf_value_instance *val = _xccdf_value_get_instance_by_selector_internal(value, selector);
89 if (val == NULL) {
90 val = xccdf_value_instance_new(type);
91 assert(val != NULL);
92 xccdf_value_instance_set_selector(val, selector);
93 oscap_list_add(XITEM(value)->sub.value.instances, val);
94 }
95 else
96 assert(type == xccdf_value_instance_get_type(val));
97 return val;
98 }
99
100 struct xccdf_item *xccdf_value_parse(xmlTextReaderPtr reader, struct xccdf_item *parent)
101 {
102 if (xccdf_element_get(reader) != XCCDFE_VALUE)
103 return NULL;
104 xccdf_value_type_t type = oscap_string_to_enum(XCCDF_VALUE_TYPE_MAP, xccdf_attribute_get(reader, XCCDFA_TYPE));
105 struct xccdf_item *value = xccdf_value_new_internal(parent, type);
106
107 value->sub.value.oper = oscap_string_to_enum(XCCDF_OPERATOR_MAP, xccdf_attribute_get(reader, XCCDFA_OPERATOR));
108 value->sub.value.interface_hint =
109 oscap_string_to_enum(XCCDF_IFACE_HINT_MAP, xccdf_attribute_get(reader, XCCDFA_INTERFACEHINT));
110 if (!xccdf_item_process_attributes(value, reader)) {
111 xccdf_value_free(value);
112 return NULL;
113 }
114
115 int depth = oscap_element_depth(reader) + 1;
116
117 struct xccdf_value_instance *val;
118 while (oscap_to_start_element(reader, depth)) {
119 xccdf_element_t el = xccdf_element_get(reader);
120 const char *selector = xccdf_attribute_get(reader, XCCDFA_SELECTOR);
121 if (selector == NULL) selector = "";
122
123 val = NULL;
124 switch (el) {
125 case XCCDFE_SOURCE:
126 oscap_list_add(value->sub.value.sources, oscap_element_string_copy(reader));
127 break;
128 case XCCDFE_VALUE_VAL:
129 val = _xccdf_value_find_or_create_instance(XVALUE(value), selector, type);
130 val->value = oscap_element_string_copy(reader);
131 val->flags.value_given = true;
132 break;
133 case XCCDFE_DEFAULT:
134 val = _xccdf_value_find_or_create_instance(XVALUE(value), selector, type);
135 val->defval = oscap_element_string_copy(reader);
136 val->flags.defval_given = true;
137 break;
138 case XCCDFE_MATCH:
139 if (type == XCCDF_TYPE_STRING) {
140 val = _xccdf_value_find_or_create_instance(XVALUE(value), selector, type);
141 val->match = oscap_element_string_copy(reader);
142 }
143 break;
144 case XCCDFE_LOWER_BOUND:
145 if (type == XCCDF_TYPE_NUMBER) {
146 val = _xccdf_value_find_or_create_instance(XVALUE(value), selector, type);
147 val->lower_bound = atof(oscap_element_string_get(reader));
148 }
149 break;
150 case XCCDFE_UPPER_BOUND:
151 if (type == XCCDF_TYPE_NUMBER) {
152 val = _xccdf_value_find_or_create_instance(XVALUE(value), selector, type);
153 val->upper_bound = atof(oscap_element_string_get(reader));
154 }
155 break;
156 case XCCDFE_CHOICES:
157 val = _xccdf_value_find_or_create_instance(XVALUE(value), selector, type);
158 val->flags.must_match = xccdf_attribute_get_bool(reader, XCCDFA_MUSTMATCH);
159 val->flags.must_match_given = true;
160 xmlTextReaderRead(reader); /* Move to the next node (subnode of <choices>)*/
161 while (oscap_to_start_element(reader, depth + 1)) {
162 if (xccdf_element_get(reader) == XCCDFE_CHOICE)
163 oscap_list_add(val->choices, oscap_element_string_copy(reader));
164 xmlTextReaderRead(reader);
165 }
166 break;
167 default:
168 if (!xccdf_item_process_element(value, reader))
169 dW("Encountered an unknown element '%s' while parsing XCCDF group.",
170 xmlTextReaderConstLocalName(reader));
171 break;
172 }
173 xmlTextReaderRead(reader);
174 }
175
176 return value;
177 }
178
179 static void xccdf_unit_node(struct oscap_list *list, xccdf_value_type_t type, const char *u, bool given,
180 xmlNs *ns, const char *elname, const char *selector)
181 {
182 if (!given) return;
183 xmlNode *node = xmlNewNode(ns, BAD_CAST elname);
184 if ((selector) && (strlen(selector) > 0)) xmlNewProp(node, BAD_CAST "selector", BAD_CAST selector);
185 xmlChar *escaped = xmlEncodeSpecialChars(NULL, BAD_CAST u);
186 xmlNodeSetContent(node, escaped);
187 xmlFree(escaped);
188 oscap_list_add(list, node);
189 }
190
191 void xccdf_value_to_dom(struct xccdf_value *value, xmlNode *value_node, xmlDoc *doc, xmlNode *parent)
192 {
193 const struct xccdf_version_info* version_info = xccdf_item_get_schema_version(XITEM(value));
194 xmlNs *ns_xccdf = lookup_xccdf_ns(doc, parent, version_info);
195
196 /* Handle Attributes */
197 const char *extends = xccdf_value_get_extends(value);
198 if (extends)
199 xmlNewProp(value_node, BAD_CAST "extends", BAD_CAST extends);
200
201 xccdf_operator_t operator = xccdf_value_get_oper(value);
202 if (operator != 0)
203 xmlNewProp(value_node, BAD_CAST "operator", BAD_CAST XCCDF_OPERATOR_MAP[operator - 1].string);
204
205 xccdf_value_type_t type = xccdf_value_get_type(value);
206 if (type != 0)
207 xmlNewProp(value_node, BAD_CAST "type", BAD_CAST XCCDF_VALUE_TYPE_MAP[type - 1].string);
208
209 if (xccdf_value_get_interactive(value))
210 xmlNewProp(value_node, BAD_CAST "interactive", BAD_CAST "true");
211
212 xccdf_interface_hint_t hint = xccdf_value_get_interface_hint(value);
213 if (hint != XCCDF_IFACE_HINT_NONE)
214 xmlNewProp(value_node, BAD_CAST "interfaceHint", BAD_CAST XCCDF_IFACE_HINT_MAP[hint - 1].string);
215
216 struct oscap_list *value_nodes = oscap_list_new();
217 struct oscap_list *defval_nodes = oscap_list_new();
218 struct oscap_list *choices_nodes = oscap_list_new();
219 struct oscap_list *lower_bound_nodes = oscap_list_new();
220 struct oscap_list *upper_bound_nodes = oscap_list_new();
221 struct oscap_list *match_nodes = oscap_list_new();
222
223 OSCAP_FOR(xccdf_value_instance, inst, xccdf_value_get_instances(value)) {
224 char buff[16]; buff[15] = '\0';
225 xccdf_unit_node(value_nodes, inst->type, inst->value, inst->flags.value_given, ns_xccdf, "value", inst->selector);
226 xccdf_unit_node(defval_nodes, inst->type, inst->defval, inst->flags.defval_given, ns_xccdf, "default", inst->selector);
227 snprintf(buff, 15, "%f", inst->lower_bound);
228 xccdf_unit_node(lower_bound_nodes, inst->type, buff,
229 inst->type == XCCDF_TYPE_NUMBER && !isnan(inst->lower_bound) && buff[0] != 'n', ns_xccdf, "lower-bound", inst->selector);
230 snprintf(buff, 15, "%f", inst->upper_bound);
231 xccdf_unit_node(upper_bound_nodes, inst->type, buff,
232 inst->type == XCCDF_TYPE_NUMBER && !isnan(inst->upper_bound) && buff[0] != 'n', ns_xccdf, "upper-bound", inst->selector);
233 xccdf_unit_node(match_nodes, inst->type, inst->match,
234 inst->type == XCCDF_TYPE_STRING && inst->match != NULL, ns_xccdf, "match", inst->selector);
235 if (inst->choices->first) {
236 xmlNode *choices = xmlNewNode(ns_xccdf, BAD_CAST "choices");
237 if ((inst->flags.must_match_given) && (inst->flags.must_match))
238 xmlNewProp(choices, BAD_CAST "mustMatch", BAD_CAST oscap_enum_to_string(OSCAP_BOOL_MAP, inst->flags.must_match));
239 if ((inst->selector)&&(strlen(inst->selector) > 0)) xmlNewProp(choices, BAD_CAST "selector", BAD_CAST inst->selector);
240 OSCAP_FOR_STR(ch, xccdf_value_instance_get_choices(inst))
241 xmlNewTextChild(choices, ns_xccdf, BAD_CAST "choice", BAD_CAST ch);
242 oscap_list_add(choices_nodes, choices);
243 }
244 }
245
246 struct oscap_list *all_nodes = oscap_list_destructive_join(oscap_list_destructive_join(value_nodes, defval_nodes),
247 oscap_list_destructive_join(oscap_list_destructive_join(match_nodes, lower_bound_nodes),
248 oscap_list_destructive_join(upper_bound_nodes, choices_nodes)));
249
250 struct oscap_iterator *it = oscap_iterator_new(all_nodes);
251 while (oscap_iterator_has_more(it)) xmlAddChild(value_node, oscap_iterator_next(it));
252 oscap_iterator_free(it);
253 oscap_list_free(all_nodes, NULL);
254
255 struct oscap_string_iterator *sources = xccdf_value_get_sources(value);
256 while (oscap_string_iterator_has_more(sources)) {
257 const char *source = oscap_string_iterator_next(sources);
258 xmlNewTextChild(value_node, ns_xccdf, BAD_CAST "source", BAD_CAST source);
259 }
260 oscap_string_iterator_free(sources);
261 }
262
263
264 void xccdf_value_free(struct xccdf_item *val)
265 {
266 if (val) {
267 oscap_list_free(val->sub.value.instances, (oscap_destruct_func)xccdf_value_instance_free);
268 oscap_list_free(val->sub.value.sources, free);
269 xccdf_item_release(val);
270 }
271 }
272
273 bool xccdf_value_set_oper(struct xccdf_value * value, xccdf_operator_t oper)
274 {
275 __attribute__nonnull__(value);
276
277 xccdf_value_to_item(value)->sub.value.oper = oper;
278 return true;
279
280 }
281
282 static void xccdf_string_dump(const char *s, int depth)
283 {
284 xccdf_print_depth(depth);
285 printf("%s\n", s);
286 }
287
288 static void
289 _xccdf_value_type_dump(xccdf_value_type_t type, int depth)
290 {
291 xccdf_print_depth(depth);
292 printf("type: ");
293 switch (type) {
294 case XCCDF_TYPE_NUMBER: printf("number\n"); break;
295 case XCCDF_TYPE_STRING: printf("string\n"); break;
296 case XCCDF_TYPE_BOOLEAN: printf("boolean\n"); break;
297 default:
298 assert(false);
299 }
300 }
301
302 static void
303 _xccdf_value_instance_dump(struct xccdf_value_instance *vi, int depth)
304 {
305 xccdf_print_depth(depth++);
306 printf("Value Instance: ");
307 if (vi == NULL) {
308 printf("(NULL)\n");
309 return;
310 }
311 printf("\n");
312 xccdf_print_depth(depth);
313 printf("selector: '%s'\n", vi->selector == NULL ? "(NULL)" : vi->selector);
314 _xccdf_value_type_dump(vi->type, depth);
315 xccdf_print_depth(depth);
316 printf("value: '%s'\n", vi->value == NULL ? "(NULL)" : vi->value);
317 xccdf_print_depth(depth);
318 printf("defval:'%s'\n", vi->defval == NULL ? "(NULL)" : vi->defval);
319 }
320
321 void xccdf_value_dump(struct xccdf_item *value, int depth)
322 {
323 xccdf_print_depth(depth++);
324 printf("Value : %s\n", (value ? value->item.id : "(NULL)"));
325 if (!value)
326 return;
327 xccdf_item_print(value, depth);
328 _xccdf_value_type_dump(value->sub.value.type, depth);
329 xccdf_print_depth(depth);
330 printf("values");
331 oscap_list_dump(value->sub.value.instances, (oscap_dump_func) _xccdf_value_instance_dump, depth + 1);
332 if (value->sub.value.sources->itemcount != 0) {
333 xccdf_print_depth(depth);
334 printf("sources");
335 oscap_list_dump(value->sub.value.sources, (oscap_dump_func) xccdf_string_dump, depth + 1);
336 }
337 }
338
339 static bool xccdf_value_has_selector(void *inst, void *sel)
340 {
341 if (inst == NULL)
342 return false;
343 return oscap_streq(((struct xccdf_value_instance *)inst)->selector, sel);
344 }
345
346 static inline struct xccdf_value_instance *
347 _xccdf_value_get_instance_by_selector_internal(const struct xccdf_value *value, const char *selector)
348 {
349 return oscap_list_find(XITEM(value)->sub.value.instances, (void*)selector, xccdf_value_has_selector);
350 }
351
352 struct xccdf_value_instance *xccdf_value_get_instance_by_selector(const struct xccdf_value *value, const char *selector)
353 {
354 struct xccdf_value_instance *val = _xccdf_value_get_instance_by_selector_internal(value, selector);
355 if (val == NULL && oscap_streq(selector, "")) {
356 /* From NISTIR-7275r4:
357 * If there is no <xccdf:value> or <xccdf:complex-value> element with an empty
358 * or absent @selector, the first <xccdf:value> or <xccdf:complex-value>
359 * element in top-down processing of the XML SHALL be the default element. */
360 struct xccdf_value_instance_iterator *instance_it = xccdf_value_get_instances(value);
361 if (xccdf_value_instance_iterator_has_more(instance_it))
362 val = xccdf_value_instance_iterator_next(instance_it);
363 xccdf_value_instance_iterator_free(instance_it);
364 }
365 return val;
366 }
367
368 bool xccdf_value_add_instance(struct xccdf_value *value, struct xccdf_value_instance *instance)
369 {
370 if (instance == NULL || value == NULL || xccdf_value_get_type(value) != instance->type)
371 return false;
372 oscap_list_add(XITEM(value)->sub.value.instances, instance);
373 return true;
374 }
375
376 XCCDF_STATUS_CURRENT(value)
377 XCCDF_VALUE_GETTER(xccdf_value_type_t, type)
378 XCCDF_VALUE_GETTER(xccdf_interface_hint_t, interface_hint)
379 XCCDF_VALUE_GETTER(xccdf_operator_t, oper)
380 XCCDF_VALUE_IGETTER(value_instance, instances)
381 XCCDF_SIGETTER(value, sources)
382 XCCDF_ITERATOR_GEN_S(value)
383 OSCAP_ITERATOR_GEN(xccdf_value_instance)
384 OSCAP_ITERATOR_REMOVE_F(xccdf_value_instance)
385
386
387 struct xccdf_value_instance *xccdf_value_instance_new(xccdf_value_type_t type)
388 {
389 struct xccdf_value_instance *inst = calloc(1, sizeof(struct xccdf_value_instance));
390 inst->lower_bound = NAN;
391 inst->upper_bound = NAN;
392
393 inst->type = type;
394 inst->choices = oscap_list_new();
395 return inst;
396 }
397
398 void xccdf_value_instance_free(struct xccdf_value_instance *inst)
399 {
400 if (inst != NULL) {
401 oscap_list_free(inst->choices, free);
402 free(inst->selector);
403 free(inst->match);
404 free(inst->value);
405 free(inst->defval);
406 free(inst);
407 }
408 }
409
410 struct xccdf_value_instance *xccdf_value_new_instance(struct xccdf_value *val)
411 {
412 if (val == NULL) return NULL;
413 return xccdf_value_instance_new(XITEM(val)->sub.value.type);
414 }
415
416 #pragma GCC diagnostic ignored "-Wunused-value"
417 #define XCCDF_VALUE_INSTANCE_VALUE_ACCESSOR_STR(WHAT) \
418 const char *xccdf_value_instance_get_##WHAT##_string(const struct xccdf_value_instance *inst) { return inst->WHAT; } \
419 bool xccdf_value_instance_set_##WHAT##_string(struct xccdf_value_instance *inst, const char *newval) { \
420 free(inst->WHAT); inst->WHAT = oscap_strdup(newval); inst->flags.WHAT##_given = true; return true; }
421 // TODO: Parse floats correctly instead of converting long to float
422 #define XCCDF_VALUE_INSTANCE_VALUE_ACCESSOR_NUM(WHAT) \
423 xccdf_numeric xccdf_value_instance_get_##WHAT##_number(const struct xccdf_value_instance *inst) { return inst->WHAT == NULL ? NAN : (float)strtol(inst->WHAT, NULL, 10); } \
424 bool xccdf_value_instance_set_##WHAT##_number(struct xccdf_value_instance *inst, xccdf_numeric newval) { \
425 free(inst->WHAT); inst->WHAT = oscap_sprintf("%f", newval); inst->flags.WHAT##_given = true; return true; }
426 #define XCCDF_VALUE_INSTANCE_VALUE_ACCESSOR_BOOL(WHAT) \
427 bool xccdf_value_instance_get_##WHAT##_boolean(const struct xccdf_value_instance *inst) \
428 { return oscap_string_to_enum(OSCAP_BOOL_MAP, inst->WHAT); } \
429 bool xccdf_value_instance_set_##WHAT##_boolean(struct xccdf_value_instance *inst, bool newval) \
430 { free(inst->WHAT); inst->WHAT = oscap_strdup(oscap_enum_to_string(OSCAP_BOOL_MAP, newval)); inst->flags.WHAT##_given = true; return true; }
431
432 OSCAP_ACCESSOR_STRING(xccdf_value_instance, selector)
433 OSCAP_GETTER(xccdf_value_type_t, xccdf_value_instance, type)
434 OSCAP_ACCESSOR_EXP(bool, xccdf_value_instance, must_match, flags.must_match)
435 OSCAP_ACCESSOR_SIMPLE(xccdf_numeric, xccdf_value_instance, upper_bound)
436 OSCAP_ACCESSOR_SIMPLE(xccdf_numeric, xccdf_value_instance, lower_bound)
437 OSCAP_ACCESSOR_STRING(xccdf_value_instance, match)
438 OSCAP_GETTER(const char*, xccdf_value_instance, value)
439 OSCAP_IGETTER(oscap_string, xccdf_value_instance, choices)
440 XCCDF_VALUE_INSTANCE_VALUE_ACCESSOR_STR(value)
441 XCCDF_VALUE_INSTANCE_VALUE_ACCESSOR_STR(defval)
442 XCCDF_VALUE_INSTANCE_VALUE_ACCESSOR_NUM(value)
443 XCCDF_VALUE_INSTANCE_VALUE_ACCESSOR_NUM(defval)
444 XCCDF_VALUE_INSTANCE_VALUE_ACCESSOR_BOOL(value)
445 XCCDF_VALUE_INSTANCE_VALUE_ACCESSOR_BOOL(defval)