419
451
static int ooxml_core_cb(int fd, cli_ctx *ctx)
421
455
cli_dbgmsg("in ooxml_core_cb\n");
422
return ooxml_parse_document(fd, ctx);
423
//return ooxml_basic_json(fd, ctx, "CoreProperties");
456
ret = ooxml_parse_document(fd, ctx);
457
if (ret == CL_EPARSE)
458
ooxml_add_parse_error(ctx->wrkproperty, "OOXML_ERROR_CORE_XMLPARSER");
459
else if (ret == CL_EFORMAT)
460
ooxml_add_parse_error(ctx->wrkproperty, "OOXML_ERROR_CORE_MALFORMED");
426
465
static int ooxml_extn_cb(int fd, cli_ctx *ctx)
428
469
cli_dbgmsg("in ooxml_extn_cb\n");
429
return ooxml_parse_document(fd, ctx);
430
//return ooxml_basic_json(fd, ctx, "ExtendedProperties");
470
ret = ooxml_parse_document(fd, ctx);
471
if (ret == CL_EPARSE)
472
ooxml_add_parse_error(ctx->wrkproperty, "OOXML_ERROR_EXTN_XMLPARSER");
473
else if (ret == CL_EFORMAT)
474
ooxml_add_parse_error(ctx->wrkproperty, "OOXML_ERROR_EXTN_MALFORMED");
433
479
static int ooxml_content_cb(int fd, cli_ctx *ctx)
435
int ret = CL_SUCCESS, tmp, toval = 0;
481
int ret = CL_SUCCESS, tmp, toval = 0, state;
436
482
int core=0, extn=0, cust=0, dsig=0;
437
483
int mcore=0, mextn=0, mcust=0;
438
484
const xmlChar *name, *value, *CT, *PN;
439
485
xmlTextReaderPtr reader = NULL;
488
unsigned long sav_scansize = ctx->scansize;
489
unsigned int sav_scannedfiles = ctx->scannedfiles;
442
491
cli_dbgmsg("in ooxml_content_cb\n");
493
/* perform engine limit checks in temporary tracking session */
494
ret = ooxml_updatelimits(fd, ctx);
498
/* apply a reader to the document */
444
499
reader = xmlReaderForFd(fd, "[Content_Types].xml", NULL, CLAMAV_MIN_XMLREADER_FLAGS);
445
500
if (reader == NULL) {
446
501
cli_dbgmsg("ooxml_content_cb: xmlReaderForFd error for ""[Content_Types].xml""\n");
502
ooxml_add_parse_error(ctx->wrkproperty, "OOXML_ERROR_XML_READER_FD");
504
ctx->scansize = sav_scansize;
505
ctx->scannedfiles = sav_scannedfiles;
447
506
return CL_SUCCESS; // libxml2 failed!
450
/* locate core-properties, extended-properties, and custom-properties (optional) */
451
while (xmlTextReaderRead(reader) == 1) {
509
/* locate core-properties, extended-properties, and custom-properties (optional) */
510
while ((state = xmlTextReaderRead(reader)) == 1) {
452
511
if (cli_json_timeout_cycle_check(ctx, &toval) != CL_SUCCESS) {
453
512
ret = CL_ETIMEOUT;
454
513
goto ooxml_content_exit;
480
539
if (!CT && !PN) continue;
482
541
if (!xmlStrcmp(CT, (const xmlChar *)"application/vnd.openxmlformats-package.core-properties+xml")) {
484
/* default: /docProps/core.xml*/
485
tmp = unzip_search_single(ctx, (const char *)(PN+1), xmlStrlen(PN)-1, &loff);
486
if (tmp == CL_ETIMEOUT) {
489
else if (tmp != CL_VIRUS) {
490
cli_dbgmsg("cli_process_ooxml: failed to find core properties file \"%s\"!\n", PN);
494
cli_dbgmsg("ooxml_content_cb: found core properties file \"%s\" @ %x\n", PN, loff);
495
ret = unzip_single_internal(ctx, loff, ooxml_core_cb);
542
/* default: /docProps/core.xml*/
543
tmp = unzip_search_single(ctx, (const char *)(PN+1), xmlStrlen(PN)-1, &loff);
544
if (tmp == CL_ETIMEOUT) {
547
else if (tmp != CL_VIRUS) {
548
cli_dbgmsg("cli_process_ooxml: failed to find core properties file \"%s\"!\n", PN);
552
cli_dbgmsg("ooxml_content_cb: found core properties file \"%s\" @ %x\n", PN, loff);
554
tmp = unzip_single_internal(ctx, loff, ooxml_core_cb);
555
if (tmp == CL_ETIMEOUT || tmp == CL_EMEM) {
500
562
else if (!xmlStrcmp(CT, (const xmlChar *)"application/vnd.openxmlformats-officedocument.extended-properties+xml")) {
502
/* default: /docProps/app.xml */
503
tmp = unzip_search_single(ctx, (const char *)(PN+1), xmlStrlen(PN)-1, &loff);
504
if (tmp == CL_ETIMEOUT) {
507
else if (tmp != CL_VIRUS) {
508
cli_dbgmsg("cli_process_ooxml: failed to find extended properties file \"%s\"!\n", PN);
512
cli_dbgmsg("ooxml_content_cb: found extended properties file \"%s\" @ %x\n", PN, loff);
513
ret = unzip_single_internal(ctx, loff, ooxml_extn_cb);
563
/* default: /docProps/app.xml */
564
tmp = unzip_search_single(ctx, (const char *)(PN+1), xmlStrlen(PN)-1, &loff);
565
if (tmp == CL_ETIMEOUT) {
568
else if (tmp != CL_VIRUS) {
569
cli_dbgmsg("cli_process_ooxml: failed to find extended properties file \"%s\"!\n", PN);
573
cli_dbgmsg("ooxml_content_cb: found extended properties file \"%s\" @ %x\n", PN, loff);
575
tmp = unzip_single_internal(ctx, loff, ooxml_extn_cb);
576
if (tmp == CL_ETIMEOUT || tmp == CL_EMEM) {
518
583
else if (!xmlStrcmp(CT, (const xmlChar *)"application/vnd.openxmlformats-officedocument.custom-properties+xml")) {
520
/* default: /docProps/custom.xml */
521
tmp = unzip_search_single(ctx, (const char *)(PN+1), xmlStrlen(PN)-1, &loff);
522
if (tmp == CL_ETIMEOUT) {
525
else if (tmp != CL_VIRUS) {
526
cli_dbgmsg("cli_process_ooxml: failed to find custom properties file \"%s\"!\n", PN);
530
cli_dbgmsg("ooxml_content_cb: found custom properties file \"%s\" @ %x\n", PN, loff);
532
//ret = unzip_single_internal(ctx, loff, ooxml_cust_cb);
584
/* default: /docProps/custom.xml */
585
tmp = unzip_search_single(ctx, (const char *)(PN+1), xmlStrlen(PN)-1, &loff);
586
if (tmp == CL_ETIMEOUT) {
589
else if (tmp != CL_VIRUS) {
590
cli_dbgmsg("cli_process_ooxml: failed to find custom properties file \"%s\"!\n", PN);
594
cli_dbgmsg("ooxml_content_cb: found custom properties file \"%s\" @ %x\n", PN, loff);
595
/* custom properties are not parsed */
536
599
else if (!xmlStrcmp(CT, (const xmlChar *)"application/vnd.openxmlformats-package.digital-signature-xmlsignature+xml")) {
540
if (ret != CL_BREAK && ret != CL_SUCCESS)
603
if (ret != CL_SUCCESS)
541
604
goto ooxml_content_exit;
544
607
ooxml_content_exit:
546
609
cli_jsonint(ctx->wrkproperty, "CorePropertiesFileCount", core);
611
ooxml_add_parse_error(ctx->wrkproperty, "OOXML_ERROR_MULTIPLE_CORE_PROPFILES");
548
614
cli_dbgmsg("cli_process_ooxml: file does not contain core properties file\n");
550
cli_jsonint(ctx->wrkproperty, "CorePropertiesMissingFileCount", core);
616
cli_jsonint(ctx->wrkproperty, "CorePropertiesMissingFileCount", mcore);
617
ooxml_add_parse_error(ctx->wrkproperty, "OOXML_ERROR_MISSING_CORE_PROPFILES");
553
621
cli_jsonint(ctx->wrkproperty, "ExtendedPropertiesFileCount", extn);
623
ooxml_add_parse_error(ctx->wrkproperty, "OOXML_ERROR_MULTIPLE_EXTN_PROPFILES");
555
626
cli_dbgmsg("cli_process_ooxml: file does not contain extended properties file\n");
557
cli_jsonint(ctx->wrkproperty, "ExtendedPropertiesMissingFileCount", extn);
628
cli_jsonint(ctx->wrkproperty, "ExtendedPropertiesMissingFileCount", mextn);
629
ooxml_add_parse_error(ctx->wrkproperty, "OOXML_ERROR_MISSING_EXTN_PROPFILES");
560
633
cli_jsonint(ctx->wrkproperty, "CustomPropertiesFileCount", cust);
635
ooxml_add_parse_error(ctx->wrkproperty, "OOXML_ERROR_MULTIPLE_CUSTOM_PROPFILES");
562
638
cli_dbgmsg("cli_process_ooxml: file does not contain custom properties file\n");
564
cli_jsonint(ctx->wrkproperty, "CustomPropertiesMissingFileCount", cust);
640
cli_jsonint(ctx->wrkproperty, "CustomPropertiesMissingFileCount", mcust);
641
ooxml_add_parse_error(ctx->wrkproperty, "OOXML_ERROR_MISSING_CUST_PROPFILES");
567
645
cli_jsonint(ctx->wrkproperty, "DigitalSignaturesCount", dsig);
648
/* restore the engine tracking limits; resets session limit tracking */
649
ctx->scansize = sav_scansize;
650
ctx->scannedfiles = sav_scannedfiles;
570
652
xmlTextReaderClose(reader);
571
653
xmlFreeTextReader(reader);
620
702
/* find "[Content Types].xml" */
621
703
tmp = unzip_search_single(ctx, "[Content_Types].xml", 18, &loff);
622
704
if (tmp == CL_ETIMEOUT) {
705
ooxml_add_parse_error(ctx->wrkproperty, "OOXML_ERROR_TIMEOUT");
623
706
return CL_ETIMEOUT;
625
708
else if (tmp != CL_VIRUS) {
626
709
cli_dbgmsg("cli_process_ooxml: failed to find ""[Content_Types].xml""!\n");
710
ooxml_add_parse_error(ctx->wrkproperty, "OOXML_ERROR_NO_CONTENT_TYPES");
627
711
return CL_EFORMAT;
629
713
cli_dbgmsg("cli_process_ooxml: found ""[Content_Types].xml"" @ %x\n", loff);
631
return unzip_single_internal(ctx, loff, ooxml_content_cb);
715
tmp = unzip_single_internal(ctx, loff, ooxml_content_cb);
716
if (tmp == CL_ETIMEOUT)
717
ooxml_add_parse_error(ctx->wrkproperty, "OOXML_ERROR_TIMEOUT");
718
else if (tmp == CL_EMEM)
719
ooxml_add_parse_error(ctx->wrkproperty, "OOXML_ERROR_OUTOFMEM");
720
else if (tmp == CL_EMAXSIZE)
721
ooxml_add_parse_error(ctx->wrkproperty, "OOXML_ERROR_EMAXSIZE");
722
else if (tmp == CL_EMAXFILES)
723
ooxml_add_parse_error(ctx->wrkproperty, "OOXML_ERROR_EMAXFILES");
633
727
UNUSEDPARAM(ctx);
634
728
cli_dbgmsg("in cli_processooxml\n");