| 1 | /* |
| 2 | * Copyright 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 | * Author: |
| 20 | * Šimon Lukašík |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <string.h> |
| 28 | #include <fcntl.h> |
| 29 | #ifdef OS_WINDOWS |
| 30 | #include <io.h> |
| 31 | #else |
| 32 | #include <unistd.h> |
| 33 | #endif |
| 34 | #include <libxml/parser.h> |
| 35 | #include <libxml/xmlreader.h> |
| 36 | #include <libxml/xmlerror.h> |
| 37 | |
| 38 | #include "common/elements.h" |
| 39 | #include "common/_error.h" |
| 40 | #include "common/debug_priv.h" |
| 41 | #include "common/public/oscap.h" |
| 42 | #include "common/util.h" |
| 43 | #include "CPE/public/cpe_lang.h" |
| 44 | #include "CPE/cpedict_priv.h" |
| 45 | #include "CPE/cpelang_priv.h" |
| 46 | #include "doc_type_priv.h" |
| 47 | #include "oscap_source.h" |
| 48 | #include "common/oscap_string.h" |
| 49 | #include "oscap_source_priv.h" |
| 50 | #include "OVAL/oval_parser_impl.h" |
| 51 | #include "OVAL/public/oval_definitions.h" |
| 52 | #include "source/bz2_priv.h" |
| 53 | #include "source/schematron_priv.h" |
| 54 | #include "source/validate_priv.h" |
| 55 | #include "XCCDF/elements.h" |
| 56 | #include "XCCDF/public/xccdf_benchmark.h" |
| 57 | #include "DS/sds_priv.h" |
| 58 | |
| 59 | typedef enum oscap_source_type { |
| 60 | OSCAP_SRC_FROM_USER_XML_FILE = 1, ///< The source originated from XML file supplied by user |
| 61 | OSCAP_SRC_FROM_USER_MEMORY, ///< The source originated from memory supplied by user |
| 62 | OSCAP_SRC_FROM_XML_DOM, ///< The source originated from XML DOM (most often from DataStream). |
| 63 | // TODO: downloaded from an http address (XCCDF can refer to remote sources) |
| 64 | } oscap_source_type_t; |
| 65 | |
| 66 | struct oscap_source { |
| 67 | oscap_document_type_t scap_type; ///< Type of SCAP document (XCCDF, OVAL, ...) |
| 68 | struct { |
| 69 | oscap_source_type_t type; ///< Internal type of the oscap_source |
| 70 | char *version; ///< Version of the particular document type |
| 71 | char *filepath; ///< Filepath (if originated from file) |
| 72 | char *memory; ///< Memory buffer (if originated from memory) |
| 73 | size_t memory_size; ///< Size of the memory buffer (if originated from memory) |
| 74 | } origin; /// |
| 75 | struct { |
| 76 | xmlDoc *doc; /// DOM |
| 77 | } xml; |
| 78 | }; |
| 79 | |
| 80 | struct oscap_source *oscap_source_new_from_file(const char *filepath) |
| 81 | { |
| 82 | /* TODO: At the end of the day, this shall be the only place in |
| 83 | * the library where a path to filename is set from the outside. |
| 84 | */ |
| 85 | struct oscap_source *source = (struct oscap_source *) calloc(1, sizeof(struct oscap_source)); |
| 86 | source->origin.filepath = oscap_strdup(filepath); |
| 87 | source->origin.type = OSCAP_SRC_FROM_USER_XML_FILE; |
| 88 | return source; |
| 89 | } |
| 90 | |
| 91 | struct oscap_source *oscap_source_clone(struct oscap_source *old) |
| 92 | { |
| 93 | struct oscap_source *new = (struct oscap_source *) calloc(1, sizeof(struct oscap_source)); |
| 94 | new->scap_type = old->scap_type; |
| 95 | new->origin.type = old->origin.type; |
| 96 | new->origin.version = oscap_strdup(old->origin.version); |
| 97 | new->origin.filepath = oscap_strdup(old->origin.filepath); |
| 98 | new->origin.memory = oscap_strdup(old->origin.memory); |
| 99 | new->origin.memory_size = old->origin.memory_size; |
| 100 | new->xml.doc = xmlCopyDoc(old->xml.doc, true); |
| 101 | return new; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Allocate oscap_source struct and fill for memory data |
| 106 | * @param size_t size Size of data |
| 107 | * @param const char* filepath Path of file |
| 108 | * @return Allocated struct |
| 109 | */ |
| 110 | static struct oscap_source* _create_oscap_source(size_t size, const char* filepath) |
| 111 | { |
| 112 | struct oscap_source *source = (struct oscap_source *) calloc(1, sizeof(struct oscap_source)); |
| 113 | source->origin.memory_size = size; |
| 114 | source->origin.type = OSCAP_SRC_FROM_USER_MEMORY; |
| 115 | source->origin.filepath = oscap_strdup(filepath ? filepath : "NONEXISTENT"); |
| 116 | return source; |
| 117 | } |
| 118 | |
| 119 | struct oscap_source *oscap_source_new_from_memory(const char *buffer, size_t size, const char *filepath) |
| 120 | { |
| 121 | struct oscap_source *source = _create_oscap_source(size, filepath); |
| 122 | source->origin.memory = calloc(1, size); |
| 123 | memcpy(source->origin.memory, buffer, size); |
| 124 | return source; |
| 125 | } |
| 126 | |
| 127 | struct oscap_source *oscap_source_new_take_memory(char *buffer, size_t size, const char *filepath) |
| 128 | { |
| 129 | struct oscap_source* source = _create_oscap_source(size, filepath); |
| 130 | source->origin.memory = buffer; |
| 131 | return source; |
| 132 | } |
| 133 | |
| 134 | struct oscap_source *oscap_source_new_from_xmlDoc(xmlDoc *doc, const char *filepath) |
| 135 | { |
| 136 | struct oscap_source *source = (struct oscap_source *) calloc(1, sizeof(struct oscap_source)); |
| 137 | source->origin.type = OSCAP_SRC_FROM_XML_DOM; |
| 138 | source->origin.filepath = oscap_strdup(filepath ? filepath : "NONEXISTENT"); |
| 139 | source->xml.doc = doc; |
| 140 | return source; |
| 141 | } |
| 142 | |
| 143 | void oscap_source_free(struct oscap_source *source) |
| 144 | { |
| 145 | if (source != NULL) { |
| 146 | free(source->origin.filepath); |
| 147 | free(source->origin.memory); |
| 148 | if (source->xml.doc != NULL) { |
| 149 | xmlFreeDoc(source->xml.doc); |
| 150 | } |
| 151 | free(source->origin.version); |
| 152 | free(source); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void oscap_source_free_xmlDoc(struct oscap_source *source) |
| 157 | { |
| 158 | if (source != NULL) { |
| 159 | if (source->xml.doc != NULL) { |
| 160 | xmlFreeDoc(source->xml.doc); |
| 161 | source->xml.doc = NULL; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Returns human readable description of oscap_source origin |
| 168 | */ |
| 169 | const char *oscap_source_readable_origin(const struct oscap_source *source) |
| 170 | { |
| 171 | // TODO: This may just return NONEXISTANT for sources from raw memory or xmlDoc |
| 172 | // and that's not very useful. |
| 173 | return source->origin.filepath; |
| 174 | } |
| 175 | |
| 176 | xmlTextReader *oscap_source_get_xmlTextReader(struct oscap_source *source) |
| 177 | { |
| 178 | xmlDoc *doc = oscap_source_get_xmlDoc(source); |
| 179 | if (doc == NULL) { |
| 180 | return NULL; |
| 181 | } |
| 182 | xmlTextReader *reader = xmlReaderWalker(doc); |
| 183 | if (reader == NULL) { |
| 184 | oscap_seterr(OSCAP_EFAMILY_XML, "Unable to create xmlTextReader for %s", oscap_source_readable_origin(source)); |
| 185 | oscap_setxmlerr(xmlGetLastError()); |
| 186 | } |
| 187 | return reader; |
| 188 | } |
| 189 | |
| 190 | oscap_document_type_t oscap_source_get_scap_type(struct oscap_source *source) |
| 191 | { |
| 192 | if (source->scap_type == OSCAP_DOCUMENT_UNKNOWN) { |
| 193 | xmlTextReader *reader = oscap_source_get_xmlTextReader(source); |
| 194 | if (reader == NULL) { |
| 195 | // the oscap error is already set |
| 196 | return OSCAP_DOCUMENT_UNKNOWN; |
| 197 | } |
| 198 | if (oscap_determine_document_type_reader(reader, &(source->scap_type)) == -1) { |
| 199 | oscap_seterr(OSCAP_EFAMILY_XML, "Unknown document type: '%s'", oscap_source_readable_origin(source)); |
| 200 | // in case of error scap_type must remain UNKNOWN |
| 201 | assert(source->scap_type == OSCAP_DOCUMENT_UNKNOWN); |
| 202 | } |
| 203 | xmlFreeTextReader(reader); |
| 204 | } |
| 205 | return source->scap_type; |
| 206 | } |
| 207 | |
| 208 | const char *oscap_source_get_filepath(struct oscap_source *source) |
| 209 | { |
| 210 | return source->origin.filepath; |
| 211 | } |
| 212 | |
| 213 | static void xmlErrorCb(struct oscap_string *buffer, const char * format, ...) |
| 214 | { |
| 215 | va_list ap; |
| 216 | va_start(ap, format); |
| 217 | |
| 218 | char* error_msg = oscap_vsprintf(format, ap); |
| 219 | oscap_string_append_string(buffer, error_msg); |
| 220 | free(error_msg); |
| 221 | |
| 222 | va_end(ap); |
| 223 | } |
| 224 | |
| 225 | static bool fd_file_is_executable(int fd) |
| 226 | { |
| 227 | int fd_dup = dup(fd); |
| 228 | if (fd_dup == -1) { |
| 229 | return false; |
| 230 | } |
| 231 | lseek(fd_dup, 0, SEEK_SET); |
| 232 | FILE* file = fdopen(fd_dup, "r"); |
| 233 | |
| 234 | if (file == NULL) { |
| 235 | // Cannot determine type of file we cannot open |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | // Check SHEBANG |
| 240 | // When we read after end of file, we get EOF (-1), |
| 241 | // So we don't have to do any special check |
| 242 | bool is_exec = true; |
| 243 | if ( fgetc(file) != (int)'#' ) is_exec = false; |
| 244 | if ( fgetc(file) != (int)'!' ) is_exec = false; |
| 245 | |
| 246 | fclose(file); |
| 247 | lseek(fd, 0, SEEK_SET); |
| 248 | return is_exec; |
| 249 | } |
| 250 | |
| 251 | static bool memory_file_is_executable(const char* memory, const size_t size) |
| 252 | { |
| 253 | if (size < 2){ |
| 254 | return false; // Cannot read SHEBANG |
| 255 | } |
| 256 | |
| 257 | // Check that file in memory starts with SHEBANG |
| 258 | if ( memory[0] != '#' ) return false; |
| 259 | if ( memory[1] != '!' ) return false; |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | xmlDoc *oscap_source_get_xmlDoc(struct oscap_source *source) |
| 264 | { |
| 265 | // We check origin.memory first because even with it being non-NULL |
| 266 | // filepath will be non-NULL, it will contain the filepath hint. |
| 267 | struct oscap_string *xml_error_string = oscap_string_new(); |
| 268 | xmlSetGenericErrorFunc(xml_error_string, (xmlGenericErrorFunc)xmlErrorCb); |
| 269 | |
| 270 | if (source->xml.doc == NULL) { |
| 271 | if (source->origin.memory != NULL) { |
| 272 | if (bz2_memory_is_bzip(source->origin.memory, source->origin.memory_size)) { |
| 273 | #ifdef BZIP2_FOUND |
| 274 | source->xml.doc = bz2_mem_read_doc(source->origin.memory, source->origin.memory_size); |
| 275 | #else |
| 276 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unable to unpack bz2 from buffer memory '%s'. Please compile OpenSCAP with bz2 support.", oscap_source_readable_origin(source)); |
| 277 | #endif |
| 278 | } else |
| 279 | { |
| 280 | source->xml.doc = xmlReadMemory(source->origin.memory, source->origin.memory_size, NULL, NULL, 0); |
| 281 | if (source->xml.doc == NULL) { |
| 282 | if (memory_file_is_executable(source->origin.memory, source->origin.memory_size)) { |
| 283 | dI("oscap-source in memory was detected as executable file '%s'. Skipped XML parsing", oscap_source_readable_origin(source)); |
| 284 | oscap_string_clear(xml_error_string); |
| 285 | } else { |
| 286 | oscap_setxmlerr(xmlGetLastError()); |
| 287 | const char *error_msg = oscap_string_get_cstr(xml_error_string); |
| 288 | oscap_seterr(OSCAP_EFAMILY_XML, "%sUnable to parse XML from user memory buffer", error_msg); |
| 289 | oscap_string_clear(xml_error_string); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | else { |
| 295 | int fd = open(source->origin.filepath, O_RDONLY); |
| 296 | if ( fd == -1 ){ |
| 297 | source->xml.doc = NULL; |
| 298 | oscap_seterr(OSCAP_EFAMILY_GLIBC, "Unable to open file: '%s'", oscap_source_readable_origin(source)); |
| 299 | } else { |
| 300 | if (bz2_fd_is_bzip(fd)) { |
| 301 | #ifdef BZIP2_FOUND |
| 302 | source->xml.doc = bz2_fd_read_doc(fd); |
| 303 | #else |
| 304 | source->xml.doc = NULL; |
| 305 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unable to unpack bz2 file '%s'. Please compile OpenSCAP with bz2 support.", oscap_source_readable_origin(source)); |
| 306 | #endif |
| 307 | } else |
| 308 | { |
| 309 | source->xml.doc = xmlReadFd(fd, NULL, NULL, 0); |
| 310 | if (source->xml.doc == NULL) { |
| 311 | if (fd_file_is_executable(fd)) { |
| 312 | dI("oscap-source file was detected as executable file '%s'. Skipped XML parsing", oscap_source_readable_origin(source)); |
| 313 | oscap_string_clear(xml_error_string); |
| 314 | } else { |
| 315 | oscap_setxmlerr(xmlGetLastError()); |
| 316 | const char *error_msg = oscap_string_get_cstr(xml_error_string); |
| 317 | oscap_seterr(OSCAP_EFAMILY_XML, "%sUnable to parse XML at: '%s'", error_msg, oscap_source_readable_origin(source)); |
| 318 | oscap_string_clear(xml_error_string); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | close(fd); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | xmlSetGenericErrorFunc(stderr, NULL); |
| 328 | |
| 329 | if (!oscap_string_empty(xml_error_string)) { |
| 330 | const char *error_msg = oscap_string_get_cstr(xml_error_string); |
| 331 | oscap_seterr(OSCAP_EFAMILY_XML, "%sFound xml error.", error_msg); |
| 332 | } |
| 333 | |
| 334 | oscap_string_free(xml_error_string); |
| 335 | return source->xml.doc; |
| 336 | } |
| 337 | |
| 338 | xmlDoc *oscap_source_pop_xmlDoc(struct oscap_source *source) |
| 339 | { |
| 340 | xmlDoc *doc = oscap_source_get_xmlDoc(source); |
| 341 | source->xml.doc = NULL; |
| 342 | return doc; |
| 343 | } |
| 344 | |
| 345 | int oscap_source_validate(struct oscap_source *source, xml_reporter reporter, void *user) |
| 346 | { |
| 347 | int ret; |
| 348 | oscap_document_type_t scap_type = oscap_source_get_scap_type(source); |
| 349 | |
| 350 | if (scap_type == OSCAP_DOCUMENT_UNKNOWN) { |
| 351 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Unrecognized document type for: %s", oscap_source_readable_origin(source)); |
| 352 | ret = -1; |
| 353 | } else { |
| 354 | const char *schema_version = oscap_source_get_schema_version(source); |
| 355 | if (!schema_version) { |
| 356 | schema_version = "unknown schema version"; |
| 357 | } |
| 358 | const char *type_name = oscap_document_type_to_string(scap_type); |
| 359 | const char *origin = oscap_source_readable_origin(source); |
| 360 | dD("Validating %s (%s) document from %s.", type_name, schema_version, origin); |
| 361 | ret = oscap_source_validate_priv(source, scap_type, schema_version, reporter, user); |
| 362 | if (ret != 0) { |
| 363 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Invalid %s (%s) content in %s.", type_name, schema_version, origin); |
| 364 | } |
| 365 | } |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | int oscap_source_validate_schematron(struct oscap_source *source) |
| 370 | { |
| 371 | oscap_document_type_t scap_type = oscap_source_get_scap_type(source); |
| 372 | const char *schema_version = oscap_source_get_schema_version(source); |
| 373 | if (!schema_version) { |
| 374 | schema_version = "unknown schema version"; |
| 375 | } |
| 376 | int ret = oscap_source_validate_schematron_priv(source, scap_type, schema_version); |
| 377 | if (ret != 0) { |
| 378 | const char *type_name = oscap_document_type_to_string(scap_type); |
| 379 | const char *origin = oscap_source_readable_origin(source); |
| 380 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Invalid %s (%s) content in %s.", type_name, schema_version, origin); |
| 381 | } |
| 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | const char *oscap_source_get_schema_version(struct oscap_source *source) |
| 386 | { |
| 387 | if (source->origin.version == NULL) { |
| 388 | xmlTextReader *reader = oscap_source_get_xmlTextReader(source); |
| 389 | if (reader == NULL) { |
| 390 | return NULL; |
| 391 | } |
| 392 | switch (oscap_source_get_scap_type(source)) { |
| 393 | case OSCAP_DOCUMENT_SDS: |
| 394 | source->origin.version = ds_sds_detect_version(reader); |
| 395 | break; |
| 396 | case OSCAP_DOCUMENT_ARF: |
| 397 | source->origin.version = oscap_strdup("1.1"); |
| 398 | break; |
| 399 | case OSCAP_DOCUMENT_OVAL_DEFINITIONS: |
| 400 | case OSCAP_DOCUMENT_OVAL_VARIABLES: |
| 401 | case OSCAP_DOCUMENT_OVAL_DIRECTIVES: |
| 402 | case OSCAP_DOCUMENT_OVAL_SYSCHAR: |
| 403 | case OSCAP_DOCUMENT_OVAL_RESULTS: |
| 404 | source->origin.version = oval_determine_document_schema_version_priv( |
| 405 | reader, oscap_source_get_scap_type(source)); |
| 406 | break; |
| 407 | case OSCAP_DOCUMENT_XCCDF: |
| 408 | case OSCAP_DOCUMENT_XCCDF_TAILORING: |
| 409 | source->origin.version = xccdf_detect_version_priv(reader); |
| 410 | break; |
| 411 | case OSCAP_DOCUMENT_CPE_DICTIONARY: |
| 412 | source->origin.version = cpe_dict_detect_version_priv(reader); |
| 413 | break; |
| 414 | case OSCAP_DOCUMENT_CPE_LANGUAGE: |
| 415 | source->origin.version = cpe_lang_model_detect_version_priv(reader); |
| 416 | break; |
| 417 | case OSCAP_DOCUMENT_SCE_RESULT: |
| 418 | source->origin.version = oscap_strdup("1.0"); |
| 419 | break; |
| 420 | case OSCAP_DOCUMENT_OCIL: |
| 421 | source->origin.version = oscap_strdup("2.0"); |
| 422 | break; |
| 423 | default: |
| 424 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not determine origin.version for document %s: Unknown type: %s", |
| 425 | oscap_source_readable_origin(source), |
| 426 | oscap_document_type_to_string(oscap_source_get_scap_type(source))); |
| 427 | break; |
| 428 | } |
| 429 | xmlFreeTextReader(reader); |
| 430 | } |
| 431 | return source->origin.version; |
| 432 | } |
| 433 | |
| 434 | int oscap_source_save_as(struct oscap_source *source, const char *filename) |
| 435 | { |
| 436 | // TODO: This assumes XML and xmlDoc being available |
| 437 | const char *target = filename != NULL ? filename : oscap_source_readable_origin(source); |
| 438 | xmlDoc *doc = oscap_source_get_xmlDoc(source); |
| 439 | if (doc == NULL) { |
| 440 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not save document to %s: DOM representation not available.", target); |
| 441 | return -1; |
| 442 | } |
| 443 | return oscap_xml_save_filename(target, doc) == 1 ? 0 : -1; |
| 444 | } |
| 445 | |
| 446 | int oscap_source_to_fd(struct oscap_source *source, int fd) |
| 447 | { |
| 448 | xmlDoc *doc = oscap_source_get_xmlDoc(source); |
| 449 | if (doc == NULL) { |
| 450 | oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not save document: DOM representation not available."); |
| 451 | return -1; |
| 452 | } |
| 453 | return oscap_xml_save_fd(fd, doc) == 1 ? 0 : -1; |
| 454 | |
| 455 | } |
| 456 | |
| 457 | int oscap_source_get_raw_memory(struct oscap_source *source, char **buffer, size_t *size) |
| 458 | { |
| 459 | if (source->origin.memory != NULL) { |
| 460 | char *ret = (char*)malloc(source->origin.memory_size); |
| 461 | memcpy(ret, source->origin.memory, source->origin.memory_size); |
| 462 | *buffer = ret; |
| 463 | *size = source->origin.memory_size; |
| 464 | return 0; |
| 465 | } |
| 466 | else { |
| 467 | xmlDoc *doc = oscap_source_get_xmlDoc(source); |
| 468 | |
| 469 | if (doc == NULL) { |
| 470 | oscap_seterr(OSCAP_EFAMILY_OSCAP, |
| 471 | "Can't retrieve raw memory. Given oscap_source doesn't originate from " |
| 472 | "raw memory and xmlDoc isn't available."); |
| 473 | return 1; |
| 474 | } |
| 475 | |
| 476 | // libxml2 asks us to use xmlFree on the returned buffer, |
| 477 | // free works fine however. Instead of doing a memory copy dance |
| 478 | // here we just let libxml2 fill the buffer. |
| 479 | // However int and size_t can be different so we do the safe thing. |
| 480 | int isize = 0; |
| 481 | xmlDocDumpMemory(doc, (xmlChar**)buffer, &isize); |
| 482 | *size = isize; |
| 483 | return 0; |
| 484 | } |
| 485 | } |