| 1 | /** |
| 2 | * @file oval_resultCriteriaNode.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 | * Šimon Lukašík |
| 29 | */ |
| 30 | |
| 31 | #ifdef HAVE_CONFIG_H |
| 32 | #include <config.h> |
| 33 | #endif |
| 34 | |
| 35 | #include <stdlib.h> |
| 36 | #include <stdio.h> |
| 37 | #include <stdarg.h> |
| 38 | #include <string.h> |
| 39 | |
| 40 | #include "oval_agent_api_impl.h" |
| 41 | #include "results/oval_results_impl.h" |
| 42 | #include "adt/oval_collection_impl.h" |
| 43 | #include "common/util.h" |
| 44 | #include "common/debug_priv.h" |
| 45 | |
| 46 | typedef struct oval_result_criteria_node { |
| 47 | struct oval_result_system *sys; |
| 48 | oval_criteria_node_type_t type; |
| 49 | oval_result_t result; |
| 50 | int negate; |
| 51 | int applicability_check; |
| 52 | } oval_result_criteria_node_t; |
| 53 | |
| 54 | typedef struct oval_result_criteria_node_CRITERIA { |
| 55 | struct oval_result_system *sys; |
| 56 | oval_criteria_node_type_t type; |
| 57 | oval_result_t result; |
| 58 | int negate; |
| 59 | int applicability_check; |
| 60 | oval_operator_t operator; |
| 61 | struct oval_collection *subnodes; |
| 62 | } oval_result_criteria_node_CRITERIA_t; |
| 63 | |
| 64 | typedef struct oval_result_criteria_node_CRITERION { |
| 65 | struct oval_result_system *sys; |
| 66 | oval_criteria_node_type_t type; |
| 67 | oval_result_t result; |
| 68 | int negate; |
| 69 | int applicability_check; |
| 70 | int variable_instance; |
| 71 | struct oval_result_test *test; |
| 72 | } oval_result_criteria_node_CRITERION_t; |
| 73 | |
| 74 | typedef struct oval_result_criteria_node_EXTENDDEF { |
| 75 | struct oval_result_system *sys; |
| 76 | oval_criteria_node_type_t type; |
| 77 | oval_result_t result; |
| 78 | int negate; |
| 79 | int applicability_check; |
| 80 | int variable_instance; |
| 81 | struct oval_result_definition *extends; |
| 82 | } oval_result_criteria_node_EXTENDDEF_t; |
| 83 | |
| 84 | struct oval_result_criteria_node *oval_result_criteria_node_new(struct oval_result_system *sys, |
| 85 | oval_criteria_node_type_t type, |
| 86 | int negate, |
| 87 | int applicability_check, |
| 88 | ...) { |
| 89 | |
| 90 | oval_result_criteria_node_t *node = NULL; |
| 91 | va_list ap; |
| 92 | |
| 93 | va_start(ap, applicability_check); |
| 94 | switch (type) { |
| 95 | case OVAL_NODETYPE_CRITERIA:{ |
| 96 | /*(NODETYPE_CRITERIA, negate, applicability_check, operator); */ |
| 97 | node = (oval_result_criteria_node_t *) |
| 98 | malloc(sizeof(oval_result_criteria_node_CRITERIA_t)); |
| 99 | if (node == NULL) { |
| 100 | va_end(ap); |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | oval_result_criteria_node_CRITERIA_t *criteria = (oval_result_criteria_node_CRITERIA_t *) node; |
| 105 | criteria->operator =(oval_operator_t) va_arg(ap, int); |
| 106 | criteria->subnodes = oval_collection_new(); |
| 107 | }; |
| 108 | break; |
| 109 | case OVAL_NODETYPE_CRITERION:{ |
| 110 | /*(NODETYPE_CRITERION, negate, applicability_check, test, variable_instance); */ |
| 111 | node = (oval_result_criteria_node_t *) |
| 112 | malloc(sizeof(oval_result_criteria_node_CRITERION_t)); |
| 113 | if (node == NULL) { |
| 114 | va_end(ap); |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | oval_result_criteria_node_CRITERION_t *criterion = |
| 119 | (oval_result_criteria_node_CRITERION_t *) node; |
| 120 | criterion->test = (struct oval_result_test *)va_arg(ap, void *); |
| 121 | criterion->variable_instance = va_arg(ap, int); |
| 122 | }; |
| 123 | break; |
| 124 | case OVAL_NODETYPE_EXTENDDEF:{ |
| 125 | /*(NODETYPE_EXTENDDEF, negate, applicability_check, definition, variable_instance); */ |
| 126 | node = (oval_result_criteria_node_t *) |
| 127 | malloc(sizeof(oval_result_criteria_node_EXTENDDEF_t)); |
| 128 | if (node == NULL) { |
| 129 | va_end(ap); |
| 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | oval_result_criteria_node_EXTENDDEF_t *extenddef = |
| 134 | (oval_result_criteria_node_EXTENDDEF_t *) node; |
| 135 | extenddef->extends = (struct oval_result_definition *)va_arg(ap, void *); |
| 136 | extenddef->variable_instance = va_arg(ap, int); |
| 137 | } break; |
| 138 | default: |
| 139 | va_end(ap); |
| 140 | dE("Unsupported criteria node type: %d.", type); |
| 141 | return NULL; |
| 142 | } |
| 143 | node->sys = sys; |
| 144 | node->negate = negate; |
| 145 | node->applicability_check = applicability_check; |
| 146 | node->result = OVAL_RESULT_NOT_EVALUATED; |
| 147 | node->type = type; |
| 148 | va_end(ap); |
| 149 | return node; |
| 150 | } |
| 151 | |
| 152 | struct oval_result_criteria_node *oval_result_criteria_node_clone |
| 153 | (struct oval_result_system *new_system, struct oval_result_criteria_node *old_node) { |
| 154 | oval_criteria_node_type_t type = oval_result_criteria_node_get_type(old_node); |
| 155 | struct oval_result_criteria_node *new_node = NULL; |
| 156 | bool negate = oval_result_criteria_node_get_negate(old_node); |
| 157 | bool applicability_check = oval_result_criteria_node_get_applicability_check(old_node); |
| 158 | switch (type) { |
| 159 | case OVAL_NODETYPE_CRITERIA:{ |
| 160 | oval_operator_t operator = oval_result_criteria_node_get_operator(old_node); |
| 161 | new_node = oval_result_criteria_node_new(new_system, |
| 162 | OVAL_NODETYPE_CRITERIA, |
| 163 | negate, |
| 164 | applicability_check, |
| 165 | operator); |
| 166 | struct oval_result_criteria_node_iterator *old_subs = |
| 167 | oval_result_criteria_node_get_subnodes(old_node); |
| 168 | while (oval_result_criteria_node_iterator_has_more(old_subs)) { |
| 169 | struct oval_result_criteria_node *old_sub = |
| 170 | oval_result_criteria_node_iterator_next(old_subs); |
| 171 | struct oval_result_criteria_node *new_sub = |
| 172 | oval_result_criteria_node_clone(new_system, old_sub); |
| 173 | oval_result_criteria_node_add_subnode(new_node, new_sub); |
| 174 | } |
| 175 | oval_result_criteria_node_iterator_free(old_subs); |
| 176 | }; |
| 177 | break; |
| 178 | case OVAL_NODETYPE_CRITERION:{ |
| 179 | struct oval_result_test *old_test = oval_result_criteria_node_get_test(old_node); |
| 180 | struct oval_result_test *new_test = oval_result_test_clone(new_system, old_test); |
| 181 | new_node = oval_result_criteria_node_new(new_system, |
| 182 | OVAL_NODETYPE_CRITERION, |
| 183 | negate, |
| 184 | applicability_check, |
| 185 | new_test, |
| 186 | 1); |
| 187 | }; |
| 188 | break; |
| 189 | case OVAL_NODETYPE_EXTENDDEF:{ |
| 190 | struct oval_result_definition *old_def = oval_result_criteria_node_get_extends(old_node); |
| 191 | struct oval_result_definition *new_def = oval_result_definition_clone(new_system, old_def); |
| 192 | new_node = oval_result_criteria_node_new(new_system, |
| 193 | OVAL_NODETYPE_EXTENDDEF, |
| 194 | negate, |
| 195 | applicability_check, |
| 196 | new_def, 1); |
| 197 | } break; |
| 198 | default: |
| 199 | break; |
| 200 | } |
| 201 | oval_result_criteria_node_set_result(old_node, oval_result_criteria_node_get_result(old_node)); |
| 202 | return new_node; |
| 203 | } |
| 204 | |
| 205 | void oval_result_criteria_node_free(struct oval_result_criteria_node *node) |
| 206 | { |
| 207 | __attribute__nonnull__(node); |
| 208 | |
| 209 | switch (node->type) { |
| 210 | case OVAL_NODETYPE_CRITERIA:{ |
| 211 | oval_result_criteria_node_CRITERIA_t *criteria = (oval_result_criteria_node_CRITERIA_t *) node; |
| 212 | criteria->operator = OVAL_OPERATOR_UNKNOWN; |
| 213 | oval_collection_free_items |
| 214 | (criteria->subnodes, (oscap_destruct_func) oval_result_criteria_node_free); |
| 215 | }; |
| 216 | break; |
| 217 | case OVAL_NODETYPE_CRITERION:{ |
| 218 | oval_result_criteria_node_CRITERION_t *criterion = |
| 219 | (oval_result_criteria_node_CRITERION_t *) node; |
| 220 | criterion->test = NULL; |
| 221 | }; |
| 222 | break; |
| 223 | case OVAL_NODETYPE_EXTENDDEF:{ |
| 224 | oval_result_criteria_node_EXTENDDEF_t *extenddef = |
| 225 | (oval_result_criteria_node_EXTENDDEF_t *) node; |
| 226 | extenddef->extends = NULL; |
| 227 | } |
| 228 | break; |
| 229 | default: |
| 230 | break; |
| 231 | } |
| 232 | node->result = OVAL_RESULT_UNKNOWN; |
| 233 | node->type = OVAL_NODETYPE_UNKNOWN; |
| 234 | free(node); |
| 235 | } |
| 236 | |
| 237 | struct oval_result_criteria_node *make_result_criteria_node_from_oval_criteria_node( |
| 238 | struct oval_result_system *sys, struct oval_criteria_node *oval_node, |
| 239 | struct oscap_list *visited_definitions, int variable_instance) |
| 240 | { |
| 241 | struct oval_result_criteria_node *rslt_node = NULL; |
| 242 | if (oval_node) { |
| 243 | oval_criteria_node_type_t type = oval_criteria_node_get_type(oval_node); |
| 244 | bool negate = oval_criteria_node_get_negate(oval_node); |
| 245 | bool applicability_check = oval_criteria_node_get_applicability_check(oval_node); |
| 246 | switch (type) { |
| 247 | case OVAL_NODETYPE_CRITERIA:{ |
| 248 | oval_operator_t operator = oval_criteria_node_get_operator(oval_node); |
| 249 | rslt_node = oval_result_criteria_node_new(sys, |
| 250 | type, |
| 251 | negate, |
| 252 | applicability_check, |
| 253 | operator); |
| 254 | struct oval_criteria_node_iterator *oval_subnodes |
| 255 | = oval_criteria_node_get_subnodes(oval_node); |
| 256 | while (oval_criteria_node_iterator_has_more(oval_subnodes)) { |
| 257 | struct oval_criteria_node *oval_subnode |
| 258 | = oval_criteria_node_iterator_next(oval_subnodes); |
| 259 | struct oval_result_criteria_node *rslt_subnode |
| 260 | = make_result_criteria_node_from_oval_criteria_node(sys, oval_subnode, visited_definitions, variable_instance); |
| 261 | if (rslt_subnode == NULL) { |
| 262 | oval_criteria_node_iterator_free(oval_subnodes); |
| 263 | oval_result_criteria_node_free(rslt_node); |
| 264 | return NULL; |
| 265 | } |
| 266 | oval_result_criteria_node_add_subnode(rslt_node, rslt_subnode); |
| 267 | } |
| 268 | oval_criteria_node_iterator_free(oval_subnodes); |
| 269 | } break; |
| 270 | case OVAL_NODETYPE_CRITERION:{ |
| 271 | struct oval_test *oval_test = oval_criteria_node_get_test(oval_node); |
| 272 | struct oval_result_test *rslt_test = oval_result_system_get_new_test(sys, oval_test, variable_instance); |
| 273 | rslt_node = oval_result_criteria_node_new(sys, |
| 274 | type, |
| 275 | negate, |
| 276 | applicability_check, |
| 277 | rslt_test, 1); |
| 278 | } break; |
| 279 | case OVAL_NODETYPE_EXTENDDEF:{ |
| 280 | struct oval_definition *oval_definition = oval_criteria_node_get_definition(oval_node); |
| 281 | struct oval_result_definition *rslt_definition |
| 282 | = oval_result_system_get_new_definition_with_check(sys, oval_definition, visited_definitions, variable_instance); |
| 283 | if (rslt_definition == NULL) |
| 284 | return NULL; |
| 285 | rslt_node = oval_result_criteria_node_new(sys, |
| 286 | type, |
| 287 | negate, |
| 288 | applicability_check, |
| 289 | rslt_definition, 1); |
| 290 | } break; |
| 291 | default: |
| 292 | rslt_node = NULL; |
| 293 | } |
| 294 | } |
| 295 | return rslt_node; |
| 296 | } |
| 297 | |
| 298 | bool oval_result_criteria_node_iterator_has_more(struct oval_result_criteria_node_iterator * oc_result_criteria_node) |
| 299 | { |
| 300 | return oval_collection_iterator_has_more((struct oval_iterator *) |
| 301 | oc_result_criteria_node); |
| 302 | } |
| 303 | |
| 304 | struct oval_result_criteria_node *oval_result_criteria_node_iterator_next(struct |
| 305 | oval_result_criteria_node_iterator |
| 306 | *oc_result_criteria_node) |
| 307 | { |
| 308 | return (struct oval_result_criteria_node *) |
| 309 | oval_collection_iterator_next((struct oval_iterator *) |
| 310 | oc_result_criteria_node); |
| 311 | } |
| 312 | |
| 313 | void oval_result_criteria_node_iterator_free(struct |
| 314 | oval_result_criteria_node_iterator |
| 315 | *oc_result_criteria_node) |
| 316 | { |
| 317 | oval_collection_iterator_free((struct oval_iterator *) |
| 318 | oc_result_criteria_node); |
| 319 | } |
| 320 | |
| 321 | oval_criteria_node_type_t oval_result_criteria_node_get_type(struct oval_result_criteria_node *node) |
| 322 | { |
| 323 | __attribute__nonnull__(node); |
| 324 | |
| 325 | oval_criteria_node_type_t type = ((struct oval_result_criteria_node *)node)->type; |
| 326 | return type; |
| 327 | } |
| 328 | |
| 329 | static struct oval_result_system *oval_result_criteria_get_system(struct oval_result_criteria_node *node) |
| 330 | { |
| 331 | __attribute__nonnull__(node); |
| 332 | |
| 333 | return node->sys; |
| 334 | } |
| 335 | |
| 336 | oval_result_t oval_result_criteria_node_negate(struct oval_result_criteria_node *node, oval_result_t result) |
| 337 | { |
| 338 | bool negate = node->negate; |
| 339 | return (negate && result == OVAL_RESULT_TRUE) ? OVAL_RESULT_FALSE : |
| 340 | (negate && result == OVAL_RESULT_FALSE) ? OVAL_RESULT_TRUE : result; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | static oval_result_t _oval_result_criteria_node_result(struct oval_result_criteria_node *node) { |
| 345 | __attribute__nonnull__(node); |
| 346 | |
| 347 | oval_result_t result; |
| 348 | switch (node->type) { |
| 349 | case OVAL_NODETYPE_CRITERIA:{ |
| 350 | struct oval_result_criteria_node_iterator *subnodes |
| 351 | = oval_result_criteria_node_get_subnodes(node); |
| 352 | oval_operator_t operator = oval_result_criteria_node_get_operator(node); |
| 353 | struct oresults node_res; |
| 354 | ores_clear(&node_res); |
| 355 | while (oval_result_criteria_node_iterator_has_more(subnodes)) { |
| 356 | struct oval_result_criteria_node *subnode |
| 357 | = oval_result_criteria_node_iterator_next(subnodes); |
| 358 | oval_result_t subres = oval_result_criteria_node_eval(subnode); |
| 359 | ores_add_res(&node_res, subres); |
| 360 | } |
| 361 | oval_result_criteria_node_iterator_free(subnodes); |
| 362 | result = ores_get_result_byopr(&node_res, operator); |
| 363 | |
| 364 | } break; |
| 365 | case OVAL_NODETYPE_CRITERION:{ |
| 366 | struct oval_result_test *test = oval_result_criteria_node_get_test(node); |
| 367 | result = oval_result_test_eval(test); |
| 368 | } break; |
| 369 | case OVAL_NODETYPE_EXTENDDEF:{ |
| 370 | struct oval_result_definition *extends = oval_result_criteria_node_get_extends(node); |
| 371 | const char *def_id = oval_result_definition_get_id(extends); |
| 372 | dI("Criteria are extended by definition '%s'.", def_id); |
| 373 | result = oval_result_definition_eval(extends); |
| 374 | } break; |
| 375 | default: |
| 376 | abort (); |
| 377 | break; |
| 378 | } |
| 379 | |
| 380 | result = oval_result_criteria_node_negate(node, result); |
| 381 | |
| 382 | return result; |
| 383 | } |
| 384 | |
| 385 | oval_result_t oval_result_criteria_node_eval(struct oval_result_criteria_node * node) |
| 386 | { |
| 387 | __attribute__nonnull__(node); |
| 388 | |
| 389 | if (node->result == OVAL_RESULT_NOT_EVALUATED) { |
| 390 | node->result = _oval_result_criteria_node_result(node); |
| 391 | } |
| 392 | return node->result; |
| 393 | } |
| 394 | |
| 395 | oval_result_t oval_result_criteria_node_get_result(struct oval_result_criteria_node * node) |
| 396 | { |
| 397 | __attribute__nonnull__(node); |
| 398 | |
| 399 | return node->result; |
| 400 | } |
| 401 | |
| 402 | bool oval_result_criteria_node_get_negate(struct oval_result_criteria_node * node) { |
| 403 | __attribute__nonnull__(node); |
| 404 | |
| 405 | return node->negate; |
| 406 | } |
| 407 | |
| 408 | bool oval_result_criteria_node_get_applicability_check(struct oval_result_criteria_node * node) { |
| 409 | __attribute__nonnull__(node); |
| 410 | |
| 411 | return node->applicability_check; |
| 412 | } |
| 413 | |
| 414 | oval_operator_t oval_result_criteria_node_get_operator(struct oval_result_criteria_node * node) |
| 415 | { |
| 416 | /*type==NODETYPE_CRITERIA */ |
| 417 | oval_operator_t operator = OVAL_OPERATOR_UNKNOWN; |
| 418 | if (oval_result_criteria_node_get_type(node) == OVAL_NODETYPE_CRITERIA) { |
| 419 | operator =((struct oval_result_criteria_node_CRITERIA *)node)->operator; |
| 420 | } |
| 421 | return operator; |
| 422 | } |
| 423 | |
| 424 | struct oval_result_criteria_node_iterator *oval_result_criteria_node_get_subnodes(struct |
| 425 | oval_result_criteria_node |
| 426 | *node) |
| 427 | { |
| 428 | /*type==NODETYPE_CRITERIA */ |
| 429 | struct oval_result_criteria_node_iterator *subnodes = NULL; |
| 430 | if (oval_result_criteria_node_get_type(node) == OVAL_NODETYPE_CRITERIA) { |
| 431 | struct oval_result_criteria_node_CRITERIA *criteria = (struct oval_result_criteria_node_CRITERIA *)node; |
| 432 | subnodes = (struct oval_result_criteria_node_iterator *) |
| 433 | oval_collection_iterator(criteria->subnodes); |
| 434 | } |
| 435 | return subnodes; |
| 436 | } |
| 437 | |
| 438 | struct oval_result_test *oval_result_criteria_node_get_test(struct oval_result_criteria_node *node) { |
| 439 | /*type==NODETYPE_CRITERION */ |
| 440 | struct oval_result_test *test = NULL; |
| 441 | if (oval_result_criteria_node_get_type(node) == OVAL_NODETYPE_CRITERION) { |
| 442 | test = ((struct oval_result_criteria_node_CRITERION *)node)->test; |
| 443 | } |
| 444 | return test; |
| 445 | } |
| 446 | |
| 447 | struct oval_result_definition *oval_result_criteria_node_get_extends(struct oval_result_criteria_node *node) { |
| 448 | /*type==NODETYPE_EXTENDDEF */ |
| 449 | struct oval_result_definition *extends = NULL; |
| 450 | if (oval_result_criteria_node_get_type(node) == OVAL_NODETYPE_EXTENDDEF) { |
| 451 | extends = ((struct oval_result_criteria_node_EXTENDDEF *)node)->extends; |
| 452 | } |
| 453 | return extends; |
| 454 | } |
| 455 | |
| 456 | void oval_result_criteria_node_set_result(struct oval_result_criteria_node *node, oval_result_t result) |
| 457 | { |
| 458 | __attribute__nonnull__(node); |
| 459 | node->result = result; |
| 460 | } |
| 461 | |
| 462 | void oval_result_criteria_node_set_negate(struct oval_result_criteria_node *node, bool negate) |
| 463 | { |
| 464 | __attribute__nonnull__(node); |
| 465 | node->negate = negate; |
| 466 | } |
| 467 | |
| 468 | void oval_result_criteria_node_set_applicability_check(struct oval_result_criteria_node *node, |
| 469 | bool applicability_check) |
| 470 | { |
| 471 | __attribute__nonnull__(node); |
| 472 | node->applicability_check = applicability_check; |
| 473 | } |
| 474 | |
| 475 | void oval_result_criteria_node_add_subnode(struct oval_result_criteria_node *node, struct oval_result_criteria_node *subnode) |
| 476 | { |
| 477 | __attribute__nonnull__(node); |
| 478 | |
| 479 | /*type==NODETYPE_CRITERIA */ |
| 480 | if (oval_result_criteria_node_get_type(node) == OVAL_NODETYPE_CRITERIA) { |
| 481 | struct oval_result_criteria_node_CRITERIA *criteria = ((struct oval_result_criteria_node_CRITERIA *)node); |
| 482 | oval_collection_add(criteria->subnodes, subnode); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | void oval_result_criteria_node_set_operator(struct oval_result_criteria_node *node, oval_operator_t operator) |
| 487 | { |
| 488 | __attribute__nonnull__(node); |
| 489 | /*type==NODETYPE_CRITERIA */ |
| 490 | if (oval_result_criteria_node_get_type(node) == OVAL_NODETYPE_CRITERIA) { |
| 491 | ((struct oval_result_criteria_node_CRITERIA *)node)->operator = operator; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | void oval_result_criteria_node_set_test(struct oval_result_criteria_node *node, struct oval_result_test *test) |
| 496 | { |
| 497 | __attribute__nonnull__(node); |
| 498 | /*type==NODETYPE_CRITERION */ |
| 499 | if (oval_result_criteria_node_get_type(node) == OVAL_NODETYPE_CRITERION) { |
| 500 | ((struct oval_result_criteria_node_CRITERION *)node)->test = test; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | void oval_result_criteria_node_set_extends(struct oval_result_criteria_node *node, struct oval_result_definition *extends) |
| 505 | { |
| 506 | __attribute__nonnull__(node); |
| 507 | /*type==NODETYPE_EXTENDDEF */ |
| 508 | if (oval_result_criteria_node_get_type(node) == OVAL_NODETYPE_EXTENDDEF) { |
| 509 | ((struct oval_result_criteria_node_EXTENDDEF *)node)->extends = extends; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | static void _oval_result_criteria_consume_subnode(struct oval_result_criteria_node *subnode, struct oval_result_criteria_node *node) { |
| 514 | oval_result_criteria_node_add_subnode(node, subnode); |
| 515 | } |
| 516 | |
| 517 | static int _oval_result_criteria_parse(xmlTextReaderPtr reader, struct oval_parser_context *context, void *usr) { |
| 518 | struct oval_result_system *sys = oval_result_criteria_get_system(usr); |
| 519 | return oval_result_criteria_node_parse(reader, context, sys, (oscap_consumer_func) _oval_result_criteria_consume_subnode, usr); |
| 520 | } |
| 521 | |
| 522 | int oval_result_criteria_node_parse(xmlTextReaderPtr reader, |
| 523 | struct oval_parser_context *context, |
| 524 | struct oval_result_system *sys, |
| 525 | oscap_consumer_func consumer, |
| 526 | void *client) |
| 527 | { |
| 528 | xmlChar *localName = xmlTextReaderLocalName(reader); |
| 529 | int rc = 0; |
| 530 | |
| 531 | struct oval_result_criteria_node *node = NULL; |
| 532 | if (strcmp((const char *)localName, "criteria") == 0) { |
| 533 | oval_operator_t operator = oval_operator_parse(reader, "operator", OVAL_OPERATOR_UNKNOWN); |
| 534 | int negate = oval_parser_boolean_attribute(reader, "negate", false); |
| 535 | int applicability_check = oval_parser_boolean_attribute(reader, "applicability_check", false); |
| 536 | node = oval_result_criteria_node_new(sys, |
| 537 | OVAL_NODETYPE_CRITERIA, |
| 538 | negate, |
| 539 | applicability_check, |
| 540 | operator); |
| 541 | rc = oval_parser_parse_tag(reader, context, _oval_result_criteria_parse, node); |
| 542 | } else if (strcmp((const char *)localName, "criterion") == 0) { |
| 543 | xmlChar *test_ref = xmlTextReaderGetAttribute(reader, BAD_CAST "test_ref"); |
| 544 | int variable_instance = oval_parser_int_attribute(reader, "variable_instance", 1); |
| 545 | int negate = oval_parser_boolean_attribute(reader, "negate", false); |
| 546 | int applicability_check = oval_parser_boolean_attribute(reader, "applicability_check", false); |
| 547 | |
| 548 | struct oval_syschar_model *syschar_model = oval_result_system_get_syschar_model(sys); |
| 549 | struct oval_definition_model *definition_model = oval_syschar_model_get_definition_model(syschar_model); |
| 550 | struct oval_test *oval_test = oval_definition_model_get_new_test(definition_model, (char *)test_ref); |
| 551 | struct oval_result_test *rslt_test = oval_result_system_get_new_test(sys, oval_test, variable_instance); |
| 552 | |
| 553 | node = oval_result_criteria_node_new(sys, |
| 554 | OVAL_NODETYPE_CRITERION, |
| 555 | negate, |
| 556 | applicability_check, |
| 557 | rslt_test, |
| 558 | variable_instance); |
| 559 | free(test_ref); |
| 560 | } else if (strcmp((const char *)localName, "extend_definition") == 0) { |
| 561 | xmlChar *definition_ref = xmlTextReaderGetAttribute(reader, BAD_CAST "definition_ref"); |
| 562 | int variable_instance = oval_parser_int_attribute(reader, "variable_instance", 1); |
| 563 | int negate = oval_parser_boolean_attribute(reader, "negate", false); |
| 564 | int applicability_check = oval_parser_boolean_attribute(reader, "applicability_check", false); |
| 565 | struct oval_syschar_model *syschar_model = oval_result_system_get_syschar_model(sys); |
| 566 | struct oval_definition_model *definition_model = oval_syschar_model_get_definition_model(syschar_model); |
| 567 | struct oval_definition *oval_definition = oval_definition_model_get_new_definition(definition_model, (char *)definition_ref); |
| 568 | struct oval_result_definition *rslt_definition = oval_result_system_get_new_definition(sys, oval_definition, variable_instance); |
| 569 | node = oval_result_criteria_node_new(sys, |
| 570 | OVAL_NODETYPE_EXTENDDEF, |
| 571 | negate, |
| 572 | applicability_check, |
| 573 | rslt_definition, |
| 574 | variable_instance); |
| 575 | free(definition_ref); |
| 576 | } else { |
| 577 | dW("unhandled criteria node: <%s>.",(char *)localName); |
| 578 | oval_parser_skip_tag(reader, context); |
| 579 | } |
| 580 | |
| 581 | if (node==NULL || rc==-1) { |
| 582 | dE("Can't parse result criteria node."); |
| 583 | return 1; |
| 584 | } |
| 585 | |
| 586 | oval_result_t result = oval_result_parse(reader, "result", 0); |
| 587 | oval_result_criteria_node_set_result(node, result); |
| 588 | |
| 589 | (*consumer) (node, client); |
| 590 | |
| 591 | |
| 592 | free(localName); |
| 593 | return rc; |
| 594 | } |
| 595 | |
| 596 | static xmlNode *_oval_result_CRITERIA_to_dom(struct oval_result_criteria_node *node, xmlDocPtr doc, xmlNode * parent) { |
| 597 | xmlNs *ns_results = xmlSearchNsByHref(doc, parent, OVAL_RESULTS_NAMESPACE); |
| 598 | xmlNode *node_root = xmlNewTextChild(parent, ns_results, BAD_CAST "criteria", NULL); |
| 599 | |
| 600 | oval_operator_t operator = oval_result_criteria_node_get_operator(node); |
| 601 | const char *operator_att = oval_operator_get_text(operator); |
| 602 | xmlNewProp(node_root, BAD_CAST "operator", BAD_CAST operator_att); |
| 603 | |
| 604 | struct oval_result_criteria_node_iterator *subnodes = oval_result_criteria_node_get_subnodes(node); |
| 605 | while (oval_result_criteria_node_iterator_has_more(subnodes)) { |
| 606 | struct oval_result_criteria_node *subnode = oval_result_criteria_node_iterator_next(subnodes); |
| 607 | oval_result_criteria_node_to_dom(subnode, doc, node_root); |
| 608 | } |
| 609 | oval_result_criteria_node_iterator_free(subnodes); |
| 610 | return node_root; |
| 611 | } |
| 612 | |
| 613 | static xmlNode *_oval_result_CRITERION_to_dom(struct oval_result_criteria_node *node, xmlDocPtr doc, xmlNode * parent) { |
| 614 | xmlNs *ns_results = xmlSearchNsByHref(doc, parent, OVAL_RESULTS_NAMESPACE); |
| 615 | xmlNode *node_root = xmlNewTextChild(parent, ns_results, BAD_CAST "criterion", NULL); |
| 616 | |
| 617 | struct oval_result_test *rslt_test = oval_result_criteria_node_get_test(node); |
| 618 | struct oval_test *oval_test = oval_result_test_get_test(rslt_test); |
| 619 | char *test_ref = oval_test_get_id(oval_test); |
| 620 | xmlNewProp(node_root, BAD_CAST "test_ref", BAD_CAST test_ref); |
| 621 | |
| 622 | char version[10]; |
| 623 | *version = '\0'; |
| 624 | snprintf(version, sizeof(version), "%d", oval_test_get_version(oval_test)); |
| 625 | xmlNewProp(node_root, BAD_CAST "version", BAD_CAST version); |
| 626 | |
| 627 | int instance = ((struct oval_result_criteria_node_CRITERION *) node)->variable_instance; |
| 628 | if (instance != 1) { |
| 629 | char instance_att[10] = ""; |
| 630 | snprintf(instance_att, sizeof (instance_att), "%d", instance); |
| 631 | xmlNewProp(node_root, BAD_CAST "variable_instance", BAD_CAST instance_att); |
| 632 | } |
| 633 | |
| 634 | return node_root; |
| 635 | |
| 636 | } |
| 637 | |
| 638 | static xmlNode *_oval_result_EXTENDDEF_to_dom(struct oval_result_criteria_node *node, xmlDocPtr doc, xmlNode * parent) { |
| 639 | xmlNs *ns_results = xmlSearchNsByHref(doc, parent, OVAL_RESULTS_NAMESPACE); |
| 640 | xmlNode *node_root = xmlNewTextChild(parent, ns_results, BAD_CAST "extend_definition", NULL); |
| 641 | |
| 642 | struct oval_result_definition *rslt_definition = oval_result_criteria_node_get_extends(node); |
| 643 | struct oval_definition *oval_definition = oval_result_definition_get_definition(rslt_definition); |
| 644 | |
| 645 | char *definition_ref = oval_definition_get_id(oval_definition); |
| 646 | xmlNewProp(node_root, BAD_CAST "definition_ref", BAD_CAST definition_ref); |
| 647 | |
| 648 | int version = oval_definition_get_version(oval_definition); |
| 649 | char version_att[10] = ""; |
| 650 | snprintf(version_att, sizeof(version_att), "%d", version); |
| 651 | xmlNewProp(node_root, BAD_CAST "version", BAD_CAST version_att); |
| 652 | |
| 653 | int instance = ((struct oval_result_criteria_node_EXTENDDEF *) node)->variable_instance; |
| 654 | if (instance != 1) { |
| 655 | char instance_att[10] = ""; |
| 656 | snprintf(instance_att, sizeof (instance_att), "%d", instance); |
| 657 | xmlNewProp(node_root, BAD_CAST "variable_instance", BAD_CAST instance_att); |
| 658 | } |
| 659 | |
| 660 | return node_root; |
| 661 | |
| 662 | } |
| 663 | |
| 664 | xmlNode *oval_result_criteria_node_to_dom(struct oval_result_criteria_node * node, xmlDocPtr doc, xmlNode * parent) { |
| 665 | xmlNode *criteria_node = NULL; |
| 666 | switch (oval_result_criteria_node_get_type(node)) { |
| 667 | case OVAL_NODETYPE_CRITERIA: |
| 668 | criteria_node = _oval_result_CRITERIA_to_dom(node, doc, parent); |
| 669 | break; |
| 670 | case OVAL_NODETYPE_CRITERION: |
| 671 | criteria_node = _oval_result_CRITERION_to_dom(node, doc, parent); |
| 672 | break; |
| 673 | case OVAL_NODETYPE_EXTENDDEF: |
| 674 | criteria_node = _oval_result_EXTENDDEF_to_dom(node, doc, parent); |
| 675 | break; |
| 676 | default: |
| 677 | break; |
| 678 | } |
| 679 | |
| 680 | if (criteria_node) { |
| 681 | oval_result_t result = oval_result_criteria_node_get_result(node); |
| 682 | const char *result_att = oval_result_get_text(result); |
| 683 | xmlNewProp(criteria_node, BAD_CAST "result", BAD_CAST result_att); |
| 684 | |
| 685 | bool negate = oval_result_criteria_node_get_negate(node); |
| 686 | if (negate != false) { |
| 687 | xmlNewProp(criteria_node, BAD_CAST "negate", BAD_CAST "true"); |
| 688 | } |
| 689 | |
| 690 | bool applicability_check = oval_result_criteria_node_get_applicability_check(node); |
| 691 | if (applicability_check != false) { |
| 692 | xmlNewProp(criteria_node, BAD_CAST "applicability_check", BAD_CAST "true"); |
| 693 | } |
| 694 | } |
| 695 | return criteria_node; |
| 696 | } |