| 1 | /** |
| 2 | * @file cpelang_priv.c |
| 3 | * \brief Interface to Common Platform Enumeration (CPE) Language |
| 4 | * |
| 5 | * See more details at http://nvd.nist.gov/cpe.cfm |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Copyright 2009--2014 Red Hat Inc., Durham, North Carolina. |
| 10 | * All Rights Reserved. |
| 11 | * |
| 12 | * This library is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU Lesser General Public |
| 14 | * License as published by the Free Software Foundation; either |
| 15 | * version 2.1 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This library is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * Lesser General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public |
| 23 | * License along with this library; if not, write to the Free Software |
| 24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 | * |
| 26 | * Authors: |
| 27 | * Maros Barabas <[email protected]> |
| 28 | */ |
| 29 | |
| 30 | #ifdef HAVE_CONFIG_H |
| 31 | #include <config.h> |
| 32 | #endif |
| 33 | |
| 34 | #include <libxml/xmlreader.h> |
| 35 | #include <libxml/xmlwriter.h> |
| 36 | #include <libxml/tree.h> |
| 37 | #include <string.h> |
| 38 | |
| 39 | #include "public/cpe_lang.h" |
| 40 | #include "cpelang_priv.h" |
| 41 | |
| 42 | #include "common/util.h" |
| 43 | #include "common/list.h" |
| 44 | #include "common/text_priv.h" |
| 45 | #include "common/elements.h" |
| 46 | #include "common/_error.h" |
| 47 | #include "common/xmlns_priv.h" |
| 48 | #include "common/xmltext_priv.h" |
| 49 | #include "source/oscap_source_priv.h" |
| 50 | #include "source/public/oscap_source.h" |
| 51 | |
| 52 | /***************************************************************************/ |
| 53 | /* Variable definitions |
| 54 | * */ |
| 55 | /* |
| 56 | * */ |
| 57 | |
| 58 | OSCAP_GETTER(cpe_lang_oper_t, cpe_testexpr, oper) |
| 59 | OSCAP_ITERATOR_GEN(cpe_testexpr) |
| 60 | |
| 61 | /* |
| 62 | * */ |
| 63 | struct cpe_lang_model { |
| 64 | struct oscap_list *platforms; // list of items |
| 65 | struct oscap_htable *item; // item by ID |
| 66 | char* origin_file; // from which file the lang model was loaded |
| 67 | }; |
| 68 | OSCAP_IGETTER_GEN(cpe_platform, cpe_lang_model, platforms) |
| 69 | OSCAP_HGETTER_STRUCT(cpe_platform, cpe_lang_model, item) |
| 70 | |
| 71 | /* |
| 72 | * */ |
| 73 | struct cpe_platform { |
| 74 | struct oscap_list *titles; // human-readable platform description |
| 75 | char *id; // platform ID |
| 76 | char *remark; // remark TODO: 0-n !! |
| 77 | struct cpe_testexpr *expr; // expression for match evaluation |
| 78 | }; |
| 79 | |
| 80 | OSCAP_ACCESSOR_STRING(cpe_platform, id) |
| 81 | OSCAP_ACCESSOR_STRING(cpe_platform, remark) |
| 82 | OSCAP_IGETINS(oscap_text, cpe_platform, titles, title) |
| 83 | OSCAP_GETTER(const struct cpe_testexpr*, cpe_platform, expr) |
| 84 | |
| 85 | /* End of variable definitions |
| 86 | * */ |
| 87 | /***************************************************************************/ |
| 88 | /***************************************************************************/ |
| 89 | /* XML string variables definitions |
| 90 | * */ |
| 91 | #define TAG_PLATFORM_SPEC_STR BAD_CAST "platform-specification" |
| 92 | #define TAG_PLATFORM_STR BAD_CAST "platform" |
| 93 | #define TAG_LOGICAL_TEST_STR BAD_CAST "logical-test" |
| 94 | #define TAG_FACT_REF_STR BAD_CAST "fact-ref" |
| 95 | #define TAG_CHECK_FACT_REF_STR BAD_CAST "check-fact-ref" |
| 96 | #define TAG_REMARK_STR BAD_CAST "remark" |
| 97 | #define ATTR_TITLE_STR BAD_CAST "title" |
| 98 | #define ATTR_NAME_STR BAD_CAST "name" |
| 99 | #define ATTR_OPERATOR_STR BAD_CAST "operator" |
| 100 | #define ATTR_NEGATE_STR BAD_CAST "negate" |
| 101 | #define ATTR_ID_STR BAD_CAST "id" |
| 102 | #define ATTR_HREF_STR BAD_CAST "href" |
| 103 | #define ATTR_SYSTEM_STR BAD_CAST "system" |
| 104 | #define ATTR_ID_REF_STR BAD_CAST "id-ref" |
| 105 | #define VAL_AND_STR BAD_CAST "AND" |
| 106 | #define VAL_OR_STR BAD_CAST "OR" |
| 107 | #define VAL_FALSE_STR BAD_CAST "false" |
| 108 | #define VAL_TRUE_STR BAD_CAST "true" |
| 109 | /* End of XML string variables definitions |
| 110 | * */ |
| 111 | /***************************************************************************/ |
| 112 | /***************************************************************************/ |
| 113 | /* Declaration of static (private to this file) functions |
| 114 | * These function shoud not be called from outside. For exporting these elements |
| 115 | * has to call parent element's |
| 116 | */ |
| 117 | static char *parse_text_element(xmlTextReaderPtr reader, char *name); |
| 118 | |
| 119 | /* End of static declarations |
| 120 | * */ |
| 121 | /***************************************************************************/ |
| 122 | |
| 123 | const struct cpe_testexpr *cpe_testexpr_get_next(const struct cpe_testexpr *expr) |
| 124 | { |
| 125 | |
| 126 | __attribute__nonnull__(expr); |
| 127 | |
| 128 | return ++(expr); |
| 129 | } |
| 130 | |
| 131 | /***************************************************************************/ |
| 132 | /* Constructors of CPE structures cpe_*<structure>*_new() |
| 133 | * More info in representive header file. |
| 134 | * returns the type of <structure> |
| 135 | */ |
| 136 | |
| 137 | struct cpe_testexpr *cpe_testexpr_new() |
| 138 | { |
| 139 | struct cpe_testexpr *ret; |
| 140 | |
| 141 | ret = calloc(1, sizeof(struct cpe_testexpr)); |
| 142 | if (ret == NULL) |
| 143 | return NULL; |
| 144 | |
| 145 | ret->oper = CPE_LANG_OPER_AND; |
| 146 | ret->meta.expr = NULL; |
| 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | struct cpe_testexpr * cpe_testexpr_clone(struct cpe_testexpr * old_expr) |
| 152 | { |
| 153 | struct cpe_testexpr * new_expr = cpe_testexpr_new(); |
| 154 | new_expr->oper = old_expr->oper; |
| 155 | |
| 156 | switch (new_expr->oper & CPE_LANG_OPER_MASK) { |
| 157 | case CPE_LANG_OPER_AND: |
| 158 | case CPE_LANG_OPER_OR: |
| 159 | new_expr->meta.expr = oscap_list_clone(old_expr->meta.expr, (oscap_clone_func) cpe_testexpr_clone); |
| 160 | break; |
| 161 | case CPE_LANG_OPER_MATCH: |
| 162 | new_expr->meta.cpe = cpe_name_clone(old_expr->meta.cpe); |
| 163 | break; |
| 164 | case CPE_LANG_OPER_CHECK: |
| 165 | new_expr->meta.check.system = oscap_strdup(old_expr->meta.check.system); |
| 166 | new_expr->meta.check.href = oscap_strdup(old_expr->meta.check.href); |
| 167 | new_expr->meta.check.id = oscap_strdup(old_expr->meta.check.id); |
| 168 | break; |
| 169 | default: |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | return new_expr; |
| 174 | } |
| 175 | |
| 176 | struct cpe_lang_model *cpe_lang_model_new() |
| 177 | { |
| 178 | |
| 179 | struct cpe_lang_model *ret; |
| 180 | |
| 181 | ret = malloc(sizeof(struct cpe_lang_model)); |
| 182 | if (ret == NULL) |
| 183 | return NULL; |
| 184 | |
| 185 | ret->platforms = oscap_list_new(); |
| 186 | ret->item = oscap_htable_new(); |
| 187 | ret->origin_file = NULL; |
| 188 | |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | struct cpe_platform *cpe_platform_new() |
| 193 | { |
| 194 | |
| 195 | struct cpe_platform *ret; |
| 196 | |
| 197 | ret = malloc(sizeof(struct cpe_platform)); |
| 198 | if (ret == NULL) |
| 199 | return NULL; |
| 200 | |
| 201 | ret->titles = oscap_list_new(); |
| 202 | ret->expr = cpe_testexpr_new(); |
| 203 | ret->id = NULL; |
| 204 | ret->remark = NULL; |
| 205 | |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | /* End of CPE structures' contructors |
| 210 | * */ |
| 211 | /***************************************************************************/ |
| 212 | |
| 213 | /***************************************************************************/ |
| 214 | /* Private parsing functions cpe_*<structure>*_parse( xmlTextReaderPtr ) |
| 215 | * More info in representive header file. |
| 216 | * returns the type of <structure> |
| 217 | */ |
| 218 | |
| 219 | struct cpe_lang_model *cpe_lang_model_import_source(struct oscap_source *source) |
| 220 | { |
| 221 | xmlTextReaderPtr reader = oscap_source_get_xmlTextReader(source); |
| 222 | struct cpe_lang_model *ret = NULL; |
| 223 | |
| 224 | if (reader != NULL) { |
| 225 | xmlTextReaderNextNode(reader); |
| 226 | ret = cpe_lang_model_parse(reader); |
| 227 | if (ret != NULL) { |
| 228 | cpe_lang_model_set_origin_file(ret, oscap_source_readable_origin(source)); |
| 229 | } |
| 230 | } |
| 231 | xmlFreeTextReader(reader); |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | struct cpe_lang_model *cpe_lang_model_parse(xmlTextReaderPtr reader) |
| 236 | { |
| 237 | |
| 238 | struct cpe_lang_model *ret = NULL; |
| 239 | struct cpe_platform *platform = NULL; |
| 240 | |
| 241 | __attribute__nonnull__(reader); |
| 242 | |
| 243 | if (!xmlStrcmp(xmlTextReaderConstLocalName(reader), TAG_PLATFORM_SPEC_STR) && |
| 244 | xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) { |
| 245 | |
| 246 | ret = cpe_lang_model_new(); |
| 247 | if (ret == NULL) |
| 248 | return NULL; |
| 249 | |
| 250 | // skip nodes until new element |
| 251 | xmlTextReaderNextElementWE(reader, TAG_PLATFORM_SPEC_STR); |
| 252 | |
| 253 | while (xmlStrcmp(xmlTextReaderConstLocalName(reader), TAG_PLATFORM_STR) == 0) { |
| 254 | |
| 255 | platform = cpe_platform_parse(reader); |
| 256 | if (platform) |
| 257 | cpe_lang_model_add_platform(ret, platform); |
| 258 | xmlTextReaderNextElementWE(reader, TAG_PLATFORM_SPEC_STR); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | struct cpe_platform *cpe_platform_parse(xmlTextReaderPtr reader) |
| 266 | { |
| 267 | |
| 268 | struct cpe_platform *ret; |
| 269 | |
| 270 | __attribute__nonnull__(reader); |
| 271 | |
| 272 | // allocate platform structure here |
| 273 | ret = cpe_platform_new(); |
| 274 | if (ret == NULL) |
| 275 | return NULL; |
| 276 | |
| 277 | // parse platform attributes here |
| 278 | ret->id = (char *)xmlTextReaderGetAttribute(reader, ATTR_ID_STR); |
| 279 | if (ret->id == NULL) { |
| 280 | cpe_platform_free(ret); |
| 281 | return NULL; // if there is no "id" in platform element, return NULL |
| 282 | } |
| 283 | // skip from <platform> node to next one |
| 284 | xmlTextReaderNextNode(reader); |
| 285 | |
| 286 | // while we have element that is not "platform", it is inside this element, otherwise it's ended |
| 287 | // element </platform> and we should end. If there is no one from "if" statement cases, we are parsing |
| 288 | // attribute or text ,.. and we can continue to next node. |
| 289 | while (xmlStrcmp(xmlTextReaderConstLocalName(reader), TAG_PLATFORM_STR) != 0) { |
| 290 | |
| 291 | if (!xmlStrcmp(xmlTextReaderConstLocalName(reader), ATTR_TITLE_STR) && |
| 292 | xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) { |
| 293 | oscap_list_add(ret->titles, oscap_text_new_parse(OSCAP_TEXT_TRAITS_PLAIN, reader)); |
| 294 | } else |
| 295 | if (!xmlStrcmp(xmlTextReaderConstLocalName(reader), TAG_REMARK_STR) && |
| 296 | xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) { |
| 297 | ret->remark = parse_text_element(reader, (char *)TAG_REMARK_STR); // TODO: 0-n remarks ! |
| 298 | } else |
| 299 | if (!xmlStrcmp(xmlTextReaderConstLocalName(reader), TAG_LOGICAL_TEST_STR) && |
| 300 | xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) { |
| 301 | /* Maybe we shall not allocate this in constructor? */ |
| 302 | cpe_testexpr_free(ret->expr); |
| 303 | ret->expr = cpe_testexpr_parse(reader); |
| 304 | } else if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) |
| 305 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unknown XML element in platform"); |
| 306 | // get the next node |
| 307 | xmlTextReaderNextNode(reader); |
| 308 | } |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | struct cpe_testexpr *cpe_testexpr_parse(xmlTextReaderPtr reader) |
| 313 | { |
| 314 | |
| 315 | xmlChar *temp = NULL; |
| 316 | struct cpe_testexpr *ret = NULL; |
| 317 | |
| 318 | __attribute__nonnull__(reader); |
| 319 | |
| 320 | // allocation |
| 321 | ret = cpe_testexpr_new(); |
| 322 | if (ret == NULL) |
| 323 | return NULL; |
| 324 | |
| 325 | // it's fact-ref only, fill the structure and return it |
| 326 | if (!xmlStrcmp(xmlTextReaderConstLocalName(reader), TAG_FACT_REF_STR) && |
| 327 | xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) { |
| 328 | ret->oper = CPE_LANG_OPER_MATCH; |
| 329 | temp = xmlTextReaderGetAttribute(reader, ATTR_NAME_STR); |
| 330 | ret->meta.cpe = cpe_name_new((char *)temp); |
| 331 | xmlFree(temp); |
| 332 | return ret; |
| 333 | // it's check-fact-ref only, fill the structure and return it |
| 334 | } else if (!xmlStrcmp(xmlTextReaderConstLocalName(reader), TAG_CHECK_FACT_REF_STR) && |
| 335 | xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) { |
| 336 | ret->oper = CPE_LANG_OPER_CHECK; |
| 337 | ret->meta.check.system = (char*)xmlTextReaderGetAttribute(reader, ATTR_SYSTEM_STR); |
| 338 | ret->meta.check.href = (char*)xmlTextReaderGetAttribute(reader, ATTR_HREF_STR); |
| 339 | ret->meta.check.id = (char*)xmlTextReaderGetAttribute(reader, ATTR_ID_REF_STR); |
| 340 | return ret; |
| 341 | } else if (!xmlStrcmp(xmlTextReaderConstLocalName(reader), TAG_LOGICAL_TEST_STR) && |
| 342 | xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) { |
| 343 | // it's logical-test, fill the structure and go to next node |
| 344 | |
| 345 | temp = xmlTextReaderGetAttribute(reader, ATTR_OPERATOR_STR); |
| 346 | if (xmlStrcasecmp(temp, VAL_AND_STR) == 0) |
| 347 | ret->oper = CPE_LANG_OPER_AND; |
| 348 | else if (xmlStrcasecmp(temp, VAL_OR_STR) == 0) |
| 349 | ret->oper = CPE_LANG_OPER_OR; |
| 350 | else { |
| 351 | // unknown operator problem |
| 352 | xmlFree(temp); |
| 353 | free(ret); |
| 354 | return NULL; |
| 355 | } |
| 356 | xmlFree(temp); |
| 357 | |
| 358 | ret->meta.expr = oscap_list_new(); // initialise a list of subexpressions |
| 359 | |
| 360 | temp = xmlTextReaderGetAttribute(reader, ATTR_NEGATE_STR); |
| 361 | if (temp && xmlStrcasecmp(temp, VAL_TRUE_STR) == 0) |
| 362 | ret->oper |= CPE_LANG_OPER_NOT; |
| 363 | xmlFree(temp); |
| 364 | } else if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) |
| 365 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unknown XML element in test expression"); |
| 366 | |
| 367 | // go to next node |
| 368 | // skip to next node |
| 369 | xmlTextReaderNextNode(reader); |
| 370 | int depth = xmlTextReaderDepth(reader); |
| 371 | //printf("[%d] logical-test\n", depth); |
| 372 | // while it's not 'logical-test' or it's not ended element .. |
| 373 | //while (!xmlStrcmp(xmlTextReaderConstLocalName(reader), TAG_FACT_REF_STR) || |
| 374 | // !xmlStrcmp(xmlTextReaderConstLocalName(reader), TAG_LOGICAL_TEST_STR)) { |
| 375 | while (xmlTextReaderDepth(reader) >= depth) { |
| 376 | |
| 377 | //printf("[%d:%d] logical-test::%s\n", depth, xmlTextReaderDepth(reader), xmlTextReaderConstLocalName(reader)); |
| 378 | |
| 379 | if (xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT) { |
| 380 | xmlTextReaderNextNode(reader); |
| 381 | continue; |
| 382 | } |
| 383 | |
| 384 | // We assume that the expression is a logical one (meaning that it |
| 385 | // can have subexpressions). |
| 386 | // TODO: Enforce that assumption and don't rely on just validation. |
| 387 | |
| 388 | // .. and the next node is logical-test element, we need recursive call |
| 389 | if (!xmlStrcmp(xmlTextReaderConstLocalName(reader), TAG_LOGICAL_TEST_STR) && |
| 390 | xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) { |
| 391 | oscap_list_add(ret->meta.expr, cpe_testexpr_parse(reader)); |
| 392 | if (xmlTextReaderDepth(reader) < depth) { |
| 393 | return ret; |
| 394 | } else if (xmlTextReaderDepth(reader) == depth) continue; |
| 395 | |
| 396 | } |
| 397 | else if (!xmlStrcmp(xmlTextReaderConstLocalName(reader), TAG_FACT_REF_STR) && |
| 398 | xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) { |
| 399 | // fill the structure |
| 400 | struct cpe_testexpr *subexpr = cpe_testexpr_new(); |
| 401 | subexpr->oper = CPE_LANG_OPER_MATCH; |
| 402 | temp = xmlTextReaderGetAttribute(reader, ATTR_NAME_STR); |
| 403 | subexpr->meta.cpe = cpe_name_new((char *)temp); |
| 404 | //printf("FACT-REF: %s\n", temp); |
| 405 | xmlFree(temp); |
| 406 | oscap_list_add(ret->meta.expr, subexpr); |
| 407 | } else if (!xmlStrcmp(xmlTextReaderConstLocalName(reader), TAG_CHECK_FACT_REF_STR) && |
| 408 | xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) { |
| 409 | struct cpe_testexpr *subexpr = cpe_testexpr_new(); |
| 410 | subexpr->oper = CPE_LANG_OPER_CHECK; |
| 411 | subexpr->meta.check.system = (char*)xmlTextReaderGetAttribute(reader, ATTR_SYSTEM_STR); |
| 412 | subexpr->meta.check.href = (char*)xmlTextReaderGetAttribute(reader, ATTR_HREF_STR); |
| 413 | subexpr->meta.check.id = (char*)xmlTextReaderGetAttribute(reader, ATTR_ID_REF_STR); |
| 414 | oscap_list_add(ret->meta.expr, subexpr); |
| 415 | } else if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) { |
| 416 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unknown XML element in test expression"); |
| 417 | } |
| 418 | xmlTextReaderNextNode(reader); |
| 419 | } |
| 420 | |
| 421 | return ret; |
| 422 | } |
| 423 | |
| 424 | static char *parse_text_element(xmlTextReaderPtr reader, char *name) |
| 425 | { |
| 426 | |
| 427 | char *string = NULL; |
| 428 | |
| 429 | __attribute__nonnull__(reader); |
| 430 | __attribute__nonnull__(name); |
| 431 | |
| 432 | // parse string element attributes here (like xml:lang) |
| 433 | |
| 434 | while (xmlTextReaderNextNode(reader)) { |
| 435 | if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT && |
| 436 | !xmlStrcmp(xmlTextReaderConstLocalName(reader), BAD_CAST name)) { |
| 437 | return string; |
| 438 | } |
| 439 | |
| 440 | switch (xmlTextReaderNodeType(reader)) { |
| 441 | case XML_READER_TYPE_TEXT: |
| 442 | string = (char *)xmlTextReaderValue(reader); |
| 443 | break; |
| 444 | default: |
| 445 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unknown XML element in platform"); |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | return string; |
| 450 | } |
| 451 | |
| 452 | /* End of private parsing functions |
| 453 | * */ |
| 454 | /***************************************************************************/ |
| 455 | |
| 456 | /***************************************************************************/ |
| 457 | /* Private exporting functions cpe_*<structure>*_export( xmlTextWriterPtr ) |
| 458 | * More info in representive header file. |
| 459 | * returns the type of <structure> |
| 460 | */ |
| 461 | void cpe_lang_model_export_xml(const struct cpe_lang_model *spec, const char *file) |
| 462 | { |
| 463 | |
| 464 | __attribute__nonnull__(spec); |
| 465 | __attribute__nonnull__(file); |
| 466 | |
| 467 | // TODO: ad macro to check return value from xmlTextWriter* functions |
| 468 | xmlTextWriterPtr writer; |
| 469 | |
| 470 | writer = xmlNewTextWriterFilename(file, 0); |
| 471 | if (writer == NULL) { |
| 472 | oscap_setxmlerr(xmlGetLastError()); |
| 473 | return; |
| 474 | } |
| 475 | // Set properties of writer TODO: make public function to edit this ?? |
| 476 | xmlTextWriterSetIndent(writer, 1); |
| 477 | xmlTextWriterSetIndentString(writer, BAD_CAST " "); |
| 478 | |
| 479 | xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL); |
| 480 | |
| 481 | cpe_lang_export(spec, writer); |
| 482 | xmlTextWriterEndDocument(writer); |
| 483 | xmlFreeTextWriter(writer); |
| 484 | if (xmlGetLastError() != NULL) |
| 485 | oscap_setxmlerr(xmlGetLastError()); |
| 486 | } |
| 487 | |
| 488 | void cpe_lang_export(const struct cpe_lang_model *spec, xmlTextWriterPtr writer) |
| 489 | { |
| 490 | |
| 491 | __attribute__nonnull__(spec); |
| 492 | __attribute__nonnull__(writer); |
| 493 | |
| 494 | xmlTextWriterStartElementNS(writer, NULL, TAG_PLATFORM_SPEC_STR, BAD_CAST XMLNS_CPE2L); |
| 495 | OSCAP_FOREACH(cpe_platform, p, cpe_lang_model_get_platforms(spec), |
| 496 | // dump its contents to XML tree |
| 497 | cpe_platform_export(p, writer);) |
| 498 | xmlTextWriterEndElement(writer); |
| 499 | if (xmlGetLastError() != NULL) |
| 500 | oscap_setxmlerr(xmlGetLastError()); |
| 501 | } |
| 502 | |
| 503 | void cpe_platform_export(const struct cpe_platform *platform, xmlTextWriterPtr writer) |
| 504 | { |
| 505 | |
| 506 | __attribute__nonnull__(platform); |
| 507 | __attribute__nonnull__(writer); |
| 508 | |
| 509 | xmlTextWriterStartElementNS(writer, NULL, TAG_PLATFORM_STR, NULL); |
| 510 | if (cpe_platform_get_id(platform) != NULL) |
| 511 | xmlTextWriterWriteAttribute(writer, ATTR_ID_STR, BAD_CAST cpe_platform_get_id(platform)); |
| 512 | oscap_textlist_export(cpe_platform_get_titles(platform), writer, "title"); |
| 513 | cpe_testexpr_export(platform->expr, writer); |
| 514 | xmlTextWriterEndElement(writer); |
| 515 | if (xmlGetLastError() != NULL) |
| 516 | oscap_setxmlerr(xmlGetLastError()); |
| 517 | } |
| 518 | |
| 519 | void cpe_testexpr_export(const struct cpe_testexpr *expr, xmlTextWriterPtr writer) |
| 520 | { |
| 521 | |
| 522 | __attribute__nonnull__(writer); |
| 523 | |
| 524 | if (expr == NULL || expr->oper == CPE_LANG_OPER_INVALID) return; |
| 525 | |
| 526 | if (expr->oper == CPE_LANG_OPER_MATCH) { |
| 527 | xmlTextWriterStartElementNS(writer, NULL, TAG_FACT_REF_STR, NULL); |
| 528 | char *cpe_name_str = cpe_name_get_as_str(expr->meta.cpe); |
| 529 | xmlTextWriterWriteAttribute(writer, ATTR_NAME_STR, BAD_CAST cpe_name_str); |
| 530 | free(cpe_name_str); |
| 531 | xmlTextWriterEndElement(writer); |
| 532 | } |
| 533 | else if (expr->oper == CPE_LANG_OPER_CHECK) { |
| 534 | xmlTextWriterStartElementNS(writer, NULL, TAG_CHECK_FACT_REF_STR, NULL); |
| 535 | xmlTextWriterWriteAttribute(writer, ATTR_SYSTEM_STR, BAD_CAST expr->meta.check.system); |
| 536 | xmlTextWriterWriteAttribute(writer, ATTR_HREF_STR, BAD_CAST expr->meta.check.href); |
| 537 | xmlTextWriterWriteAttribute(writer, ATTR_ID_REF_STR, BAD_CAST expr->meta.check.id); |
| 538 | xmlTextWriterEndElement(writer); |
| 539 | } |
| 540 | else { |
| 541 | xmlTextWriterStartElementNS(writer, NULL, TAG_LOGICAL_TEST_STR, BAD_CAST XMLNS_CPE2L); |
| 542 | |
| 543 | if (expr->oper == CPE_LANG_OPER_AND) { |
| 544 | xmlTextWriterWriteAttribute(writer, ATTR_OPERATOR_STR, VAL_AND_STR); |
| 545 | xmlTextWriterWriteAttribute(writer, ATTR_NEGATE_STR, VAL_FALSE_STR); |
| 546 | } else if (expr->oper == CPE_LANG_OPER_OR) { |
| 547 | xmlTextWriterWriteAttribute(writer, ATTR_OPERATOR_STR, VAL_OR_STR); |
| 548 | xmlTextWriterWriteAttribute(writer, ATTR_NEGATE_STR, VAL_FALSE_STR); |
| 549 | } else if (expr->oper == CPE_LANG_OPER_NOR) { |
| 550 | xmlTextWriterWriteAttribute(writer, ATTR_OPERATOR_STR, VAL_OR_STR); |
| 551 | xmlTextWriterWriteAttribute(writer, ATTR_NEGATE_STR, VAL_TRUE_STR); |
| 552 | } else if (expr->oper == CPE_LANG_OPER_NAND) { |
| 553 | xmlTextWriterWriteAttribute(writer, ATTR_OPERATOR_STR, VAL_AND_STR); |
| 554 | xmlTextWriterWriteAttribute(writer, ATTR_NEGATE_STR, VAL_TRUE_STR); |
| 555 | } else { |
| 556 | /* can this happen? */ |
| 557 | return; |
| 558 | } |
| 559 | |
| 560 | if (expr->meta.expr == NULL) |
| 561 | return; |
| 562 | OSCAP_FOREACH(cpe_testexpr, subexpr, oscap_iterator_new(expr->meta.expr), |
| 563 | cpe_testexpr_export(subexpr, writer); |
| 564 | ); |
| 565 | xmlTextWriterEndElement(writer); |
| 566 | } |
| 567 | |
| 568 | if (xmlGetLastError() != NULL) |
| 569 | oscap_setxmlerr(xmlGetLastError()); |
| 570 | } |
| 571 | |
| 572 | /* End of private export functions |
| 573 | * */ |
| 574 | /***************************************************************************/ |
| 575 | |
| 576 | /***************************************************************************/ |
| 577 | /* Free functions - all are static private, do not use them outside this file |
| 578 | */ |
| 579 | |
| 580 | void cpe_lang_model_free(struct cpe_lang_model *platformspec) |
| 581 | { |
| 582 | if (platformspec == NULL) |
| 583 | return; |
| 584 | |
| 585 | oscap_htable_free0(platformspec->item); |
| 586 | oscap_list_free(platformspec->platforms, (oscap_destruct_func) cpe_platform_free); |
| 587 | free(platformspec->origin_file); |
| 588 | |
| 589 | free(platformspec); |
| 590 | } |
| 591 | |
| 592 | void cpe_platform_free(struct cpe_platform *platform) |
| 593 | { |
| 594 | if (platform == NULL) |
| 595 | return; |
| 596 | |
| 597 | xmlFree(platform->id); |
| 598 | xmlFree(platform->remark); |
| 599 | oscap_list_free(platform->titles, (oscap_destruct_func) oscap_text_free); |
| 600 | cpe_testexpr_free(platform->expr); |
| 601 | free(platform); |
| 602 | } |
| 603 | |
| 604 | static void cpe_testexpr_meta_free(struct cpe_testexpr *expr) |
| 605 | { |
| 606 | assert(expr != NULL); |
| 607 | |
| 608 | switch (expr->oper & CPE_LANG_OPER_MASK) { |
| 609 | case CPE_LANG_OPER_AND: |
| 610 | case CPE_LANG_OPER_OR: |
| 611 | oscap_list_free(expr->meta.expr, (oscap_destruct_func) cpe_testexpr_free); |
| 612 | expr->meta.expr = NULL; |
| 613 | break; |
| 614 | case CPE_LANG_OPER_MATCH: |
| 615 | cpe_name_free(expr->meta.cpe); |
| 616 | expr->meta.cpe = NULL; |
| 617 | break; |
| 618 | case CPE_LANG_OPER_CHECK: |
| 619 | free(expr->meta.check.system); |
| 620 | expr->meta.check.system = NULL; |
| 621 | free(expr->meta.check.href); |
| 622 | expr->meta.check.href = NULL; |
| 623 | free(expr->meta.check.id); |
| 624 | expr->meta.check.id = NULL; |
| 625 | break; |
| 626 | default: |
| 627 | break; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | void cpe_testexpr_free(struct cpe_testexpr *expr) |
| 632 | { |
| 633 | if (expr == NULL) |
| 634 | return; |
| 635 | |
| 636 | cpe_testexpr_meta_free(expr); |
| 637 | expr->oper = 0; |
| 638 | free(expr); |
| 639 | } |
| 640 | |
| 641 | /* End of free functions |
| 642 | * */ |
| 643 | /***************************************************************************/ |
| 644 | |
| 645 | /* ************************************************************************** |
| 646 | * Getters / setters / adders (not generated) |
| 647 | */ |
| 648 | |
| 649 | struct cpe_testexpr_iterator *cpe_testexpr_get_meta_expr(const struct cpe_testexpr *expr) |
| 650 | { |
| 651 | if (expr == NULL) return NULL; |
| 652 | if ((expr->oper & (CPE_LANG_OPER_AND | CPE_LANG_OPER_OR))) |
| 653 | return (struct cpe_testexpr_iterator*) oscap_iterator_new(expr->meta.expr); |
| 654 | return NULL; |
| 655 | } |
| 656 | |
| 657 | const struct cpe_name *cpe_testexpr_get_meta_cpe(const struct cpe_testexpr *expr) |
| 658 | { |
| 659 | assert(expr != NULL); |
| 660 | if ((expr->oper & CPE_LANG_OPER_MASK) != CPE_LANG_OPER_MATCH) |
| 661 | return NULL; |
| 662 | return expr->meta.cpe; |
| 663 | } |
| 664 | |
| 665 | const char* cpe_testexpr_get_meta_check_system(const struct cpe_testexpr *expr) |
| 666 | { |
| 667 | assert(expr != NULL); |
| 668 | if ((expr->oper & CPE_LANG_OPER_MASK) != CPE_LANG_OPER_CHECK) |
| 669 | return NULL; |
| 670 | return expr->meta.check.system; |
| 671 | } |
| 672 | |
| 673 | const char* cpe_testexpr_get_meta_check_href(const struct cpe_testexpr *expr) |
| 674 | { |
| 675 | assert(expr != NULL); |
| 676 | if ((expr->oper & CPE_LANG_OPER_MASK) != CPE_LANG_OPER_CHECK) |
| 677 | return NULL; |
| 678 | return expr->meta.check.href; |
| 679 | } |
| 680 | |
| 681 | const char* cpe_testexpr_get_meta_check_id(const struct cpe_testexpr *expr) |
| 682 | { |
| 683 | assert(expr != NULL); |
| 684 | if ((expr->oper & CPE_LANG_OPER_MASK) != CPE_LANG_OPER_CHECK) |
| 685 | return NULL; |
| 686 | return expr->meta.check.id; |
| 687 | } |
| 688 | |
| 689 | bool cpe_testexpr_set_oper(struct cpe_testexpr *expr, cpe_lang_oper_t oper) |
| 690 | { |
| 691 | assert(expr != NULL); |
| 692 | |
| 693 | cpe_testexpr_meta_free(expr); |
| 694 | expr->oper = oper; |
| 695 | return true; |
| 696 | } |
| 697 | |
| 698 | bool cpe_testexpr_set_name(struct cpe_testexpr *expr, struct cpe_name *name) |
| 699 | { |
| 700 | assert(expr != NULL); |
| 701 | |
| 702 | if ((expr->oper & CPE_LANG_OPER_MASK) != CPE_LANG_OPER_MATCH) |
| 703 | return false; |
| 704 | |
| 705 | cpe_testexpr_meta_free(expr); |
| 706 | expr->meta.cpe = name; |
| 707 | return true; |
| 708 | } |
| 709 | |
| 710 | bool cpe_testexpr_add_subexpression(struct cpe_testexpr *expr, struct cpe_testexpr *sub) |
| 711 | { |
| 712 | assert(expr != NULL); |
| 713 | assert(sub != NULL); |
| 714 | |
| 715 | int oper = expr->oper & CPE_LANG_OPER_MASK; |
| 716 | if (oper != CPE_LANG_OPER_AND && oper != CPE_LANG_OPER_OR) |
| 717 | return false; |
| 718 | oscap_list_add(expr->meta.expr, sub); |
| 719 | return true; |
| 720 | } |
| 721 | |
| 722 | bool cpe_lang_model_add_platform(struct cpe_lang_model * lang, struct cpe_platform * platform) |
| 723 | { |
| 724 | if (lang == NULL || platform == NULL || platform->id == NULL) |
| 725 | return false; |
| 726 | oscap_list_add(lang->platforms, platform); |
| 727 | oscap_htable_add(lang->item, platform->id, platform); |
| 728 | return true; |
| 729 | } |
| 730 | |
| 731 | void cpe_platform_iterator_remove(struct cpe_platform_iterator *it, struct cpe_lang_model *parent) |
| 732 | { |
| 733 | struct cpe_platform *plat = oscap_iterator_detach((struct oscap_iterator *)it); |
| 734 | if (plat) { |
| 735 | oscap_htable_detach(parent->item, cpe_platform_get_id(plat)); |
| 736 | cpe_platform_free(plat); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | bool cpe_platform_set_expr(struct cpe_platform *platform, struct cpe_testexpr *expr) |
| 741 | { |
| 742 | assert(platform != NULL); |
| 743 | |
| 744 | if (expr != NULL && !(expr->oper & (CPE_LANG_OPER_AND | CPE_LANG_OPER_OR))) |
| 745 | return false; |
| 746 | |
| 747 | cpe_testexpr_free(platform->expr); |
| 748 | platform->expr = expr; |
| 749 | return true; |
| 750 | } |
| 751 | |
| 752 | bool cpe_lang_model_set_origin_file(struct cpe_lang_model* lang_model, const char* origin_file) |
| 753 | { |
| 754 | free(lang_model->origin_file); |
| 755 | lang_model->origin_file = oscap_strdup(origin_file); |
| 756 | |
| 757 | return true; |
| 758 | } |
| 759 | |
| 760 | const char* cpe_lang_model_get_origin_file(const struct cpe_lang_model* lang_model) |
| 761 | { |
| 762 | return lang_model->origin_file; |
| 763 | } |