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