| 1 | /* |
| 2 | * Copyright 2011--2014 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 | * Tomas Heinrich <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #include <config.h> |
| 24 | #include <libxml/tree.h> |
| 25 | #include "oval_definitions_impl.h" |
| 26 | #include "oval_agent_api_impl.h" |
| 27 | #include "oval_system_characteristics_impl.h" |
| 28 | #include "adt/oval_collection_impl.h" |
| 29 | #include "oval_parser_impl.h" |
| 30 | |
| 31 | #include "common/util.h" |
| 32 | #include "common/debug_priv.h" |
| 33 | #include "common/elements.h" |
| 34 | |
| 35 | typedef struct oval_record_field { |
| 36 | oval_record_field_type_t record_field_type; |
| 37 | char *name; |
| 38 | char *value; |
| 39 | oval_datatype_t datatype; |
| 40 | int mask; |
| 41 | } oval_record_field_t; |
| 42 | |
| 43 | typedef struct oval_record_field_STATE { |
| 44 | oval_record_field_type_t record_field_type; |
| 45 | char *name; |
| 46 | char *value; |
| 47 | oval_datatype_t datatype; |
| 48 | int mask; |
| 49 | oval_operation_t operation; |
| 50 | struct oval_variable *variable; |
| 51 | oval_check_t var_check; |
| 52 | oval_check_t ent_check; |
| 53 | } oval_record_field_STATE_t; |
| 54 | |
| 55 | typedef struct oval_record_field_ITEM { |
| 56 | oval_record_field_type_t record_field_type; |
| 57 | char *name; |
| 58 | char *value; |
| 59 | oval_datatype_t datatype; |
| 60 | int mask; |
| 61 | oval_syschar_status_t status; |
| 62 | } oval_record_field_ITEM_t; |
| 63 | |
| 64 | bool oval_record_field_iterator_has_more(struct oval_record_field_iterator *itr) |
| 65 | { |
| 66 | return oval_collection_iterator_has_more((struct oval_iterator *) itr); |
| 67 | } |
| 68 | |
| 69 | struct oval_record_field *oval_record_field_iterator_next(struct oval_record_field_iterator *itr) |
| 70 | { |
| 71 | return (struct oval_record_field *) |
| 72 | oval_collection_iterator_next((struct oval_iterator *) itr); |
| 73 | } |
| 74 | |
| 75 | void oval_record_field_iterator_free(struct oval_record_field_iterator *itr) |
| 76 | { |
| 77 | oval_collection_iterator_free((struct oval_iterator *) itr); |
| 78 | } |
| 79 | |
| 80 | struct oval_record_field *oval_record_field_new(oval_record_field_type_t type) |
| 81 | { |
| 82 | struct oval_record_field *rf; |
| 83 | |
| 84 | switch (type) { |
| 85 | case OVAL_RECORD_FIELD_STATE: |
| 86 | { |
| 87 | struct oval_record_field_STATE *rfs; |
| 88 | |
| 89 | rfs = malloc(sizeof(*rfs)); |
| 90 | if (rfs == NULL) |
| 91 | return NULL; |
| 92 | |
| 93 | rfs->operation = OVAL_OPERATION_UNKNOWN; |
| 94 | rfs->variable = NULL; |
| 95 | rfs->var_check = OVAL_CHECK_UNKNOWN; |
| 96 | rfs->ent_check = OVAL_CHECK_UNKNOWN; |
| 97 | rf = (struct oval_record_field *) rfs; |
| 98 | break; |
| 99 | } |
| 100 | case OVAL_RECORD_FIELD_ITEM: |
| 101 | { |
| 102 | struct oval_record_field_ITEM *rfi; |
| 103 | |
| 104 | rfi = malloc(sizeof(*rfi)); |
| 105 | if (rfi == NULL) |
| 106 | return NULL; |
| 107 | |
| 108 | rfi->status = SYSCHAR_STATUS_UNKNOWN; |
| 109 | rf = (struct oval_record_field *) rfi; |
| 110 | break; |
| 111 | } |
| 112 | default: |
| 113 | dE("Unsupported record field type: %d.", type); |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | rf->record_field_type = type; |
| 118 | rf->name = NULL; |
| 119 | rf->value = NULL; |
| 120 | rf->datatype = OVAL_DATATYPE_UNKNOWN; |
| 121 | rf->mask = 0; |
| 122 | |
| 123 | return rf; |
| 124 | } |
| 125 | |
| 126 | struct oval_record_field *oval_record_field_clone(struct oval_record_field *old_rf) |
| 127 | { |
| 128 | struct oval_record_field *new_rf; |
| 129 | |
| 130 | switch (old_rf->record_field_type) { |
| 131 | case OVAL_RECORD_FIELD_STATE: |
| 132 | { |
| 133 | struct oval_record_field_STATE *new_rfs, *old_rfs; |
| 134 | |
| 135 | new_rfs = malloc(sizeof(*new_rfs)); |
| 136 | if (new_rfs == NULL) |
| 137 | return NULL; |
| 138 | |
| 139 | old_rfs = (struct oval_record_field_STATE *) old_rf; |
| 140 | new_rfs->operation = old_rfs->operation; |
| 141 | new_rfs->variable = old_rfs->variable; |
| 142 | new_rfs->var_check = old_rfs->var_check; |
| 143 | new_rfs->ent_check = old_rfs->ent_check; |
| 144 | new_rf = (struct oval_record_field *) new_rfs; |
| 145 | break; |
| 146 | } |
| 147 | case OVAL_RECORD_FIELD_ITEM: |
| 148 | { |
| 149 | struct oval_record_field_ITEM *new_rfi, *old_rfi; |
| 150 | |
| 151 | new_rfi = malloc(sizeof(*new_rfi)); |
| 152 | if (new_rfi == NULL) |
| 153 | return NULL; |
| 154 | |
| 155 | old_rfi = (struct oval_record_field_ITEM *) old_rf; |
| 156 | new_rfi->status = old_rfi->status; |
| 157 | new_rf = (struct oval_record_field *) new_rfi; |
| 158 | break; |
| 159 | } |
| 160 | default: |
| 161 | dE("Unsupported record field type: %d.", old_rf->record_field_type); |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | new_rf->record_field_type = old_rf->record_field_type; |
| 166 | new_rf->name = oscap_strdup(old_rf->name); |
| 167 | new_rf->value = oscap_strdup(old_rf->value); |
| 168 | new_rf->datatype = old_rf->datatype; |
| 169 | new_rf->mask = old_rf->mask; |
| 170 | |
| 171 | return new_rf; |
| 172 | } |
| 173 | |
| 174 | void oval_record_field_free(struct oval_record_field *rf) |
| 175 | { |
| 176 | if (rf == NULL) |
| 177 | return; |
| 178 | |
| 179 | if (rf->name != NULL) |
| 180 | free(rf->name); |
| 181 | if (rf->value != NULL) |
| 182 | free(rf->value); |
| 183 | |
| 184 | rf->name = rf->value = NULL; |
| 185 | free(rf); |
| 186 | } |
| 187 | |
| 188 | void oval_record_field_set_name(struct oval_record_field *rf, char *name) |
| 189 | { |
| 190 | rf->name = oscap_strdup(name); |
| 191 | } |
| 192 | |
| 193 | void oval_record_field_set_value(struct oval_record_field *rf, char *value) |
| 194 | { |
| 195 | rf->value = oscap_strdup(value); |
| 196 | } |
| 197 | |
| 198 | void oval_record_field_set_datatype(struct oval_record_field *rf, oval_datatype_t dt) |
| 199 | { |
| 200 | rf->datatype = dt; |
| 201 | } |
| 202 | |
| 203 | void oval_record_field_set_mask(struct oval_record_field *rf, int mask) |
| 204 | { |
| 205 | rf->mask = mask; |
| 206 | } |
| 207 | |
| 208 | void oval_record_field_set_operation(struct oval_record_field *rf, oval_operation_t operation) |
| 209 | { |
| 210 | if (rf->record_field_type != OVAL_RECORD_FIELD_STATE) { |
| 211 | dE("Wrong record field type (set operation): %d.", rf->record_field_type); |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | ((struct oval_record_field_STATE *) rf)->operation = operation; |
| 216 | } |
| 217 | |
| 218 | void oval_record_field_set_variable(struct oval_record_field *rf, struct oval_variable *var) |
| 219 | { |
| 220 | if (rf->record_field_type != OVAL_RECORD_FIELD_STATE) { |
| 221 | dE("Wrong record field type (set variable): %d.", rf->record_field_type); |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | ((struct oval_record_field_STATE *) rf)->variable = var; |
| 226 | } |
| 227 | |
| 228 | void oval_record_field_set_var_check(struct oval_record_field *rf, oval_check_t var_check) |
| 229 | { |
| 230 | if (rf->record_field_type != OVAL_RECORD_FIELD_STATE) { |
| 231 | dE("Wrong record field type (set var_check): %d.", rf->record_field_type); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | ((struct oval_record_field_STATE *) rf)->var_check = var_check; |
| 236 | } |
| 237 | |
| 238 | void oval_record_field_set_ent_check(struct oval_record_field *rf, oval_check_t ent_check) |
| 239 | { |
| 240 | if (rf->record_field_type != OVAL_RECORD_FIELD_STATE) { |
| 241 | dE("Wrong record field type (set ent_check): %d.", rf->record_field_type); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | ((struct oval_record_field_STATE *) rf)->ent_check = ent_check; |
| 246 | } |
| 247 | |
| 248 | void oval_record_field_set_status(struct oval_record_field *rf, oval_syschar_status_t status) |
| 249 | { |
| 250 | if (rf->record_field_type != OVAL_RECORD_FIELD_ITEM) { |
| 251 | dE("Wrong record field type (set status): %d.", rf->record_field_type); |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | ((struct oval_record_field_ITEM *) rf)->status = status; |
| 256 | } |
| 257 | |
| 258 | oval_record_field_type_t oval_record_field_get_type(struct oval_record_field *rf) |
| 259 | { |
| 260 | return rf->record_field_type; |
| 261 | } |
| 262 | |
| 263 | char *oval_record_field_get_name(struct oval_record_field *rf) |
| 264 | { |
| 265 | return rf->name; |
| 266 | } |
| 267 | |
| 268 | char *oval_record_field_get_value(struct oval_record_field *rf) |
| 269 | { |
| 270 | return rf->value; |
| 271 | } |
| 272 | |
| 273 | oval_datatype_t oval_record_field_get_datatype(struct oval_record_field *rf) |
| 274 | { |
| 275 | return rf->datatype; |
| 276 | } |
| 277 | |
| 278 | int oval_record_field_get_mask(struct oval_record_field *rf) |
| 279 | { |
| 280 | return rf->mask; |
| 281 | } |
| 282 | |
| 283 | oval_operation_t oval_record_field_get_operation(struct oval_record_field *rf) |
| 284 | { |
| 285 | if (rf->record_field_type != OVAL_RECORD_FIELD_STATE) { |
| 286 | dE("Wrong record field type (get operation): %d.", rf->record_field_type); |
| 287 | return OVAL_OPERATION_UNKNOWN; |
| 288 | } |
| 289 | |
| 290 | return ((struct oval_record_field_STATE *) rf)->operation; |
| 291 | } |
| 292 | |
| 293 | struct oval_variable *oval_record_field_get_variable(struct oval_record_field *rf) |
| 294 | { |
| 295 | if (rf->record_field_type != OVAL_RECORD_FIELD_STATE) { |
| 296 | dE("Wrong record field type (get variable): %d.", rf->record_field_type); |
| 297 | return NULL; |
| 298 | } |
| 299 | |
| 300 | return ((struct oval_record_field_STATE *) rf)->variable; |
| 301 | } |
| 302 | |
| 303 | oval_check_t oval_record_field_get_var_check(struct oval_record_field *rf) |
| 304 | { |
| 305 | if (rf->record_field_type != OVAL_RECORD_FIELD_STATE) { |
| 306 | dE("Wrong record field type (get var_check): %d.", rf->record_field_type); |
| 307 | return OVAL_CHECK_UNKNOWN; |
| 308 | } |
| 309 | |
| 310 | return ((struct oval_record_field_STATE *) rf)->var_check; |
| 311 | } |
| 312 | |
| 313 | oval_check_t oval_record_field_get_ent_check(struct oval_record_field *rf) |
| 314 | { |
| 315 | if (rf->record_field_type != OVAL_RECORD_FIELD_STATE) { |
| 316 | dE("Wrong record field type (get ent_check): %d.", rf->record_field_type); |
| 317 | return OVAL_CHECK_UNKNOWN; |
| 318 | } |
| 319 | |
| 320 | return ((struct oval_record_field_STATE *) rf)->ent_check; |
| 321 | } |
| 322 | |
| 323 | oval_syschar_status_t oval_record_field_get_status(struct oval_record_field *rf) |
| 324 | { |
| 325 | if (rf->record_field_type != OVAL_RECORD_FIELD_ITEM) { |
| 326 | dE("Wrong record field type (get status): %d.", rf->record_field_type); |
| 327 | return SYSCHAR_STATUS_UNKNOWN; |
| 328 | } |
| 329 | |
| 330 | return ((struct oval_record_field_ITEM *) rf)->status; |
| 331 | } |
| 332 | |
| 333 | static void _oval_record_field_value_consumer(char *value, void *rf) |
| 334 | { |
| 335 | oval_record_field_set_value(rf, value); |
| 336 | } |
| 337 | |
| 338 | int oval_record_field_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, |
| 339 | oscap_consumer_func consumer, void *user, |
| 340 | oval_record_field_type_t record_field_type) |
| 341 | { |
| 342 | int ret = 0, mask; |
| 343 | char *name; |
| 344 | oval_datatype_t datatype; |
| 345 | struct oval_record_field *rf; |
| 346 | |
| 347 | rf = oval_record_field_new(record_field_type); |
| 348 | if (rf == NULL) |
| 349 | return -1; |
| 350 | |
| 351 | name = (char *) xmlTextReaderGetAttribute(reader, BAD_CAST "name"); |
| 352 | datatype = oval_datatype_parse(reader, "datatype", OVAL_DATATYPE_STRING); |
| 353 | mask = oval_parser_boolean_attribute(reader, "mask", 0); |
| 354 | |
| 355 | oval_record_field_set_name(rf, name); |
| 356 | xmlFree(name); |
| 357 | oval_record_field_set_datatype(rf, datatype); |
| 358 | oval_record_field_set_mask(rf, mask); |
| 359 | |
| 360 | switch (record_field_type) { |
| 361 | case OVAL_RECORD_FIELD_STATE: |
| 362 | { |
| 363 | oval_operation_t opr; |
| 364 | oval_check_t var_check; |
| 365 | oval_check_t ent_check; |
| 366 | char *var_ref; |
| 367 | |
| 368 | opr = oval_operation_parse(reader, "operation", OVAL_OPERATION_EQUALS); |
| 369 | oval_record_field_set_operation(rf, opr); |
| 370 | var_check = oval_check_parse(reader, "var_check", OVAL_CHECK_ALL); |
| 371 | oval_record_field_set_var_check(rf, var_check); |
| 372 | ent_check = oval_check_parse(reader, "entity_check", OVAL_CHECK_ALL); |
| 373 | oval_record_field_set_ent_check(rf, ent_check); |
| 374 | |
| 375 | var_ref = (char *) xmlTextReaderGetAttribute(reader, BAD_CAST "var_ref"); |
| 376 | if (var_ref == NULL) { |
| 377 | ret = oscap_parser_text_value(reader, &_oval_record_field_value_consumer, rf); |
| 378 | } else { |
| 379 | struct oval_definition_model *model; |
| 380 | struct oval_variable *var; |
| 381 | |
| 382 | model = context->definition_model; |
| 383 | var = oval_definition_model_get_new_variable(model, var_ref, OVAL_VARIABLE_UNKNOWN); |
| 384 | oval_record_field_set_variable(rf, var); |
| 385 | xmlFree(var_ref); |
| 386 | } |
| 387 | break; |
| 388 | } |
| 389 | case OVAL_RECORD_FIELD_ITEM: |
| 390 | { |
| 391 | oval_syschar_status_t status; |
| 392 | |
| 393 | status = oval_syschar_status_parse(reader, "status", SYSCHAR_STATUS_EXISTS); |
| 394 | oval_record_field_set_status(rf, status); |
| 395 | ret = oscap_parser_text_value(reader, &_oval_record_field_value_consumer, rf); |
| 396 | break; |
| 397 | } |
| 398 | default: |
| 399 | dE("Impossible happened."); |
| 400 | } |
| 401 | |
| 402 | (*consumer) (rf, user); |
| 403 | |
| 404 | return ret; |
| 405 | } |
| 406 | |
| 407 | xmlNode *oval_record_field_to_dom(struct oval_record_field *rf, bool parent_mask, xmlDoc *doc, xmlNode *parent, xmlNs *namespace) |
| 408 | { |
| 409 | char *name, *value; |
| 410 | bool rf_mask, masked; |
| 411 | xmlNode *node, *root_node; |
| 412 | oval_datatype_t datatype; |
| 413 | |
| 414 | if (rf->record_field_type != OVAL_RECORD_FIELD_STATE |
| 415 | && rf->record_field_type != OVAL_RECORD_FIELD_ITEM) { |
| 416 | dE("Unsupported record field type: %d.", rf->record_field_type); |
| 417 | return NULL; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | |
| 422 | root_node = xmlDocGetRootElement(doc); |
| 423 | name = oval_record_field_get_name(rf); |
| 424 | rf_mask = oval_record_field_get_mask(rf); |
| 425 | if (!xmlStrcmp(root_node->name, BAD_CAST OVAL_ROOT_ELM_RESULTS) |
| 426 | && (rf_mask || parent_mask)) { |
| 427 | value = NULL; |
| 428 | masked = true; |
| 429 | } else { |
| 430 | value = oval_record_field_get_value(rf); |
| 431 | masked = false; |
| 432 | } |
| 433 | |
| 434 | node = xmlNewTextChild(parent, namespace, BAD_CAST "field", BAD_CAST value); |
| 435 | xmlNewProp(node, BAD_CAST "name", BAD_CAST name); |
| 436 | datatype = oval_record_field_get_datatype(rf); |
| 437 | if (datatype != OVAL_DATATYPE_STRING) |
| 438 | xmlNewProp(node, BAD_CAST "datatype", BAD_CAST oval_datatype_get_text(datatype)); |
| 439 | if (rf_mask) |
| 440 | xmlNewProp(node, BAD_CAST "mask", BAD_CAST "true"); |
| 441 | |
| 442 | switch (rf->record_field_type) { |
| 443 | case OVAL_RECORD_FIELD_STATE: |
| 444 | { |
| 445 | struct oval_variable *var; |
| 446 | oval_check_t var_check; |
| 447 | oval_check_t ent_check; |
| 448 | |
| 449 | if (!masked) { |
| 450 | oval_operation_t opr; |
| 451 | |
| 452 | opr = oval_record_field_get_operation(rf); |
| 453 | if (opr != OVAL_OPERATION_EQUALS) |
| 454 | xmlNewProp(node, BAD_CAST "operation", BAD_CAST oval_operation_get_text(opr)); |
| 455 | } |
| 456 | var = oval_record_field_get_variable(rf); |
| 457 | if (var != NULL) |
| 458 | xmlNewProp(node, BAD_CAST "var_ref", BAD_CAST oval_variable_get_id(var)); |
| 459 | var_check = oval_record_field_get_var_check(rf); |
| 460 | if (var_check != OVAL_CHECK_ALL) |
| 461 | xmlNewProp(node, BAD_CAST "var_check", BAD_CAST oval_check_get_text(var_check)); |
| 462 | ent_check = oval_record_field_get_ent_check(rf); |
| 463 | if (ent_check != OVAL_CHECK_ALL) |
| 464 | xmlNewProp(node, BAD_CAST "entity_check", BAD_CAST oval_check_get_text(ent_check)); |
| 465 | break; |
| 466 | } |
| 467 | case OVAL_RECORD_FIELD_ITEM: |
| 468 | { |
| 469 | oval_syschar_status_t status; |
| 470 | |
| 471 | status = oval_record_field_get_status(rf); |
| 472 | if (status != SYSCHAR_STATUS_EXISTS) |
| 473 | xmlNewProp(node, BAD_CAST "status", BAD_CAST oval_syschar_status_get_text(status)); |
| 474 | break; |
| 475 | } |
| 476 | default: |
| 477 | dE("Impossible happened."); |
| 478 | } |
| 479 | |
| 480 | return node; |
| 481 | } |