47bbbc56 |
/*
* Extract component parts of OLE2 files (e.g. MS Office Documents)
* |
42438bd5 |
* Copyright (C) 2004-2007 trog@uncon.org |
47bbbc56 |
*
* This code is based on the OpenOffice and libgsf sources.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software |
48b7b4a7 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA. |
47bbbc56 |
*/
|
6d6e8271 |
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
|
47bbbc56 |
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h> |
b58fdfc2 |
#ifdef HAVE_UNISTD_H |
47bbbc56 |
#include <unistd.h> |
b58fdfc2 |
#endif |
47bbbc56 |
#include <ctype.h>
#include <stdlib.h> |
b58fdfc2 |
#include "clamav.h" |
47bbbc56 |
|
8c030302 |
#if HAVE_MMAP
#if HAVE_SYS_MMAN_H
#include <sys/mman.h>
#else /* HAVE_SYS_MMAN_H */
#undef HAVE_MMAP
#endif
#endif
|
f893c0f3 |
#include "cltypes.h" |
1dc96b6c |
#include "others.h" |
079229d6 |
#include "ole2_extract.h" |
98a3c4ef |
|
11b50569 |
#include "mbox.h"
#include "blob.h" /* sanitiseName() */
|
75282b5c |
#define ole2_endian_convert_16(v) le16_to_host(v)
#define ole2_endian_convert_32(v) le32_to_host(v) |
c83b8845 |
|
1dc96b6c |
#ifndef HAVE_ATTRIB_PACKED
#define __attribute__(x)
#endif
#ifdef HAVE_PRAGMA_PACK
#pragma pack(1)
#endif
|
ef38c83d |
#ifdef HAVE_PRAGMA_PACK_HPPA
#pragma pack 1
#endif
|
b58fdfc2 |
#ifndef O_BINARY
#define O_BINARY 0
#endif
|
47bbbc56 |
typedef struct ole2_header_tag
{ |
bda5598b |
unsigned char magic[8]; /* should be: 0xd0cf11e0a1b11ae1 */
unsigned char clsid[16]; |
e0f90556 |
uint16_t minor_version __attribute__ ((packed));
uint16_t dll_version __attribute__ ((packed));
int16_t byte_order __attribute__ ((packed)); /* -2=intel */ |
47bbbc56 |
|
e0f90556 |
uint16_t log2_big_block_size __attribute__ ((packed)); /* usually 9 (2^9 = 512) */
uint32_t log2_small_block_size __attribute__ ((packed)); /* usually 6 (2^6 = 128) */ |
47bbbc56 |
|
e0f90556 |
int32_t reserved[2] __attribute__ ((packed));
int32_t bat_count __attribute__ ((packed));
int32_t prop_start __attribute__ ((packed)); |
47bbbc56 |
|
e0f90556 |
uint32_t signature __attribute__ ((packed));
uint32_t sbat_cutoff __attribute__ ((packed)); /* cutoff for files held in small blocks (4096) */ |
47bbbc56 |
|
e0f90556 |
int32_t sbat_start __attribute__ ((packed));
int32_t sbat_block_count __attribute__ ((packed));
int32_t xbat_start __attribute__ ((packed));
int32_t xbat_count __attribute__ ((packed));
int32_t bat_array[109] __attribute__ ((packed)); |
349e0502 |
/* not part of the ole2 header, but stuff we need in order to decode */
/* must take account of the size of variables below here when
reading the header */ |
e0f90556 |
int32_t sbat_root_start __attribute__ ((packed)); |
8c030302 |
unsigned char *m_area;
off_t m_length; |
8a9c2d19 |
bitset_t *bitset; |
e0f90556 |
} ole2_header_t; |
47bbbc56 |
typedef struct property_tag
{ |
bda5598b |
char name[64]; /* in unicode */ |
f8c58685 |
uint16_t name_size __attribute__ ((packed)); |
bda5598b |
unsigned char type; /* 1=dir 2=file 5=root */
unsigned char color; /* black or red */ |
f8c58685 |
uint32_t prev __attribute__ ((packed));
uint32_t next __attribute__ ((packed));
uint32_t child __attribute__ ((packed)); |
e0f90556 |
|
bda5598b |
unsigned char clsid[16]; |
e0f90556 |
uint32_t user_flags __attribute__ ((packed));
uint32_t create_lowdate __attribute__ ((packed));
uint32_t create_highdate __attribute__ ((packed));
uint32_t mod_lowdate __attribute__ ((packed));
uint32_t mod_highdate __attribute__ ((packed)); |
f8c58685 |
uint32_t start_block __attribute__ ((packed));
uint32_t size __attribute__ ((packed)); |
bda5598b |
unsigned char reserved[4]; |
e0f90556 |
} property_t; |
47bbbc56 |
|
1dc96b6c |
#ifdef HAVE_PRAGMA_PACK
#pragma pack()
#endif
|
ef38c83d |
#ifdef HAVE_PRAGMA_PACK_HPPA
#pragma pack
#endif
|
bda5598b |
static unsigned char magic_id[] = { 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1}; |
47bbbc56 |
|
e0f90556 |
static char *get_property_name(char *name, int size) |
47bbbc56 |
{
int i, j;
char *newname;
|
f5646d91 |
if (*name == 0 || size <= 0 || size > 64) { |
47bbbc56 |
return NULL;
}
|
9e7e2c76 |
newname = (char *) cli_malloc(size*7); |
47bbbc56 |
if (!newname) {
return NULL;
}
j=0;
/* size-2 to ignore trailing NULL */
for (i=0 ; i < size-2; i+=2) { |
b58fdfc2 |
if((!(name[i]&0x80)) && isprint(name[i])) { |
47bbbc56 |
newname[j++] = name[i];
} else {
if (name[i] < 10 && name[i] >= 0) {
newname[j++] = '_';
newname[j++] = name[i] + '0';
} |
9e7e2c76 |
else {
const uint16_t x = (((uint16_t)name[i]) << 8) | name[i+1];
newname[j++] = '_';
newname[j++] = 'a'+((x&0xF));
newname[j++] = 'a'+((x>>4)&0xF);
newname[j++] = 'a'+((x>>8)&0xF);
newname[j++] = 'a'+((x>>16)&0xF);
newname[j++] = 'a'+((x>>24)&0xF);
} |
47bbbc56 |
newname[j++] = '_';
}
}
newname[j] = '\0'; |
349e0502 |
if (strlen(newname) == 0) {
free(newname);
return NULL;
} |
47bbbc56 |
return newname;
} |
e0f90556 |
static void print_property_name(char *pname, int size) |
d95b7372 |
{
char *name;
name = get_property_name(pname, size);
if (!name) {
return;
} |
e5a5b2f2 |
cli_dbgmsg("%34s ", name); |
d95b7372 |
free(name);
return;
} |
47bbbc56 |
|
e0f90556 |
static void print_ole2_property(property_t *property) |
47bbbc56 |
{ |
d95b7372 |
if (property->name_size > 64) {
cli_dbgmsg("[err name len: %d]\n", property->name_size);
return;
}
print_property_name(property->name, property->name_size); |
47bbbc56 |
switch (property->type) {
case 2: |
e5a5b2f2 |
cli_dbgmsg(" [file] "); |
47bbbc56 |
break;
case 1: |
e5a5b2f2 |
cli_dbgmsg(" [dir ] "); |
47bbbc56 |
break;
case 5: |
e5a5b2f2 |
cli_dbgmsg(" [root] "); |
47bbbc56 |
break;
default:
cli_dbgmsg(" [%d]", property->type);
}
switch (property->color) {
case 0: |
e5a5b2f2 |
cli_dbgmsg(" r "); |
47bbbc56 |
break;
case 1: |
e5a5b2f2 |
cli_dbgmsg(" b "); |
47bbbc56 |
break;
default: |
e5a5b2f2 |
cli_dbgmsg(" u "); |
47bbbc56 |
} |
f8c58685 |
cli_dbgmsg(" 0x%.8x 0x%.8x\n", property->size, property->user_flags); |
47bbbc56 |
}
|
e0f90556 |
static void print_ole2_header(ole2_header_t *hdr) |
47bbbc56 |
{
int i;
if (!hdr) {
return;
}
cli_dbgmsg("\nMagic:\t\t\t0x");
for (i=0 ; i<8; i++) {
cli_dbgmsg("%x", hdr->magic[i]);
}
cli_dbgmsg("\n");
cli_dbgmsg("CLSID:\t\t\t{");
for (i=0 ; i<16; i++) {
cli_dbgmsg("%x ", hdr->clsid[i]);
}
cli_dbgmsg("}\n");
cli_dbgmsg("Minor version:\t\t0x%x\n", hdr->minor_version);
cli_dbgmsg("DLL version:\t\t0x%x\n", hdr->dll_version);
cli_dbgmsg("Byte Order:\t\t%d\n", hdr->byte_order);
cli_dbgmsg("Big Block Size:\t\t%i\n", hdr->log2_big_block_size);
cli_dbgmsg("Small Block Size:\t%i\n", hdr->log2_small_block_size);
cli_dbgmsg("BAT count:\t\t%d\n", hdr->bat_count);
cli_dbgmsg("Prop start:\t\t%d\n", hdr->prop_start);
cli_dbgmsg("SBAT cutoff:\t\t%d\n", hdr->sbat_cutoff);
cli_dbgmsg("SBat start:\t\t%d\n", hdr->sbat_start);
cli_dbgmsg("SBat block count:\t%d\n", hdr->sbat_block_count);
cli_dbgmsg("XBat start:\t\t%d\n", hdr->xbat_start);
cli_dbgmsg("XBat block count:\t%d\n\n", hdr->xbat_count);
return;
}
|
e0f90556 |
static int ole2_read_block(int fd, ole2_header_t *hdr, void *buff, int32_t blockno) |
47bbbc56 |
{ |
c30e4a78 |
off_t offset, offend; |
47bbbc56 |
|
767c51ae |
if (blockno < 0) {
return FALSE;
}
|
bf5a1ce7 |
/* other methods: (blockno+1) * 512 or (blockno * block_size) + 512; */ |
47bbbc56 |
offset = (blockno << hdr->log2_big_block_size) + 512; /* 512 is header size */ |
8c030302 |
if (hdr->m_area == NULL) {
if (lseek(fd, offset, SEEK_SET) != offset) {
return FALSE;
}
if (cli_readn(fd, buff, (1 << hdr->log2_big_block_size)) != (1 << hdr->log2_big_block_size)) {
return FALSE;
}
} else { |
c30e4a78 |
offend = offset + (1 << hdr->log2_big_block_size);
if ((offend <= 0) || (offend > hdr->m_length)) { |
8c030302 |
return FALSE;
}
memcpy(buff, hdr->m_area+offset, (1 << hdr->log2_big_block_size)); |
47bbbc56 |
}
return TRUE;
}
|
e0f90556 |
static int32_t ole2_get_next_bat_block(int fd, ole2_header_t *hdr, int32_t current_block) |
47bbbc56 |
{ |
1dc96b6c |
int32_t bat_array_index; |
47bbbc56 |
uint32_t bat[128];
|
767c51ae |
if (current_block < 0) {
return -1;
}
|
47bbbc56 |
bat_array_index = current_block / 128;
if (bat_array_index > hdr->bat_count) {
cli_dbgmsg("bat_array index error\n");
return -10;
} |
767c51ae |
if (!ole2_read_block(fd, hdr, &bat,
ole2_endian_convert_32(hdr->bat_array[bat_array_index]))) {
return -1;
} |
c83b8845 |
return ole2_endian_convert_32(bat[current_block-(bat_array_index * 128)]); |
47bbbc56 |
}
|
e0f90556 |
static int32_t ole2_get_next_xbat_block(int fd, ole2_header_t *hdr, int32_t current_block) |
47bbbc56 |
{ |
1dc96b6c |
int32_t xbat_index, xbat_block_index, bat_index, bat_blockno; |
47bbbc56 |
uint32_t xbat[128], bat[128];
|
767c51ae |
if (current_block < 0) {
return -1;
}
|
47bbbc56 |
xbat_index = current_block / 128;
/* NB: The last entry in each XBAT points to the next XBAT block.
This reduces the number of entries in each block by 1.
*/
xbat_block_index = (xbat_index - 109) / 127;
bat_blockno = (xbat_index - 109) % 127;
bat_index = current_block % 128;
|
767c51ae |
if (!ole2_read_block(fd, hdr, &xbat, hdr->xbat_start)) {
return -1;
} |
47bbbc56 |
/* Follow the chain of XBAT blocks */
while (xbat_block_index > 0) { |
767c51ae |
if (!ole2_read_block(fd, hdr, &xbat,
ole2_endian_convert_32(xbat[127]))) {
return -1;
} |
47bbbc56 |
xbat_block_index--;
}
|
145cf777 |
if (!ole2_read_block(fd, hdr, &bat, ole2_endian_convert_32(xbat[bat_blockno]))) { |
767c51ae |
return -1;
} |
47bbbc56 |
|
c83b8845 |
return ole2_endian_convert_32(bat[bat_index]); |
47bbbc56 |
}
|
e0f90556 |
static int32_t ole2_get_next_block_number(int fd, ole2_header_t *hdr, int32_t current_block) |
47bbbc56 |
{ |
767c51ae |
if (current_block < 0) {
return -1;
}
|
47bbbc56 |
if ((current_block / 128) > 108) {
return ole2_get_next_xbat_block(fd, hdr, current_block);
} else {
return ole2_get_next_bat_block(fd, hdr, current_block);
}
}
|
e0f90556 |
static int32_t ole2_get_next_sbat_block(int fd, ole2_header_t *hdr, int32_t current_block) |
1dc96b6c |
{
int32_t iter, current_bat_block;
uint32_t sbat[128];
|
767c51ae |
if (current_block < 0) {
return -1;
}
|
1dc96b6c |
current_bat_block = hdr->sbat_start;
iter = current_block / 128;
while (iter > 0) {
current_bat_block = ole2_get_next_block_number(fd, hdr, current_bat_block);
iter--;
} |
767c51ae |
if (!ole2_read_block(fd, hdr, &sbat, current_bat_block)) {
return -1;
} |
1dc96b6c |
return ole2_endian_convert_32(sbat[current_block % 128]);
}
|
47bbbc56 |
/* Retrieve the block containing the data for the given sbat index */ |
e0f90556 |
static int32_t ole2_get_sbat_data_block(int fd, ole2_header_t *hdr, void *buff, int32_t sbat_index) |
47bbbc56 |
{ |
1dc96b6c |
int32_t block_count, current_block; |
47bbbc56 |
|
767c51ae |
if (sbat_index < 0) {
return FALSE;
}
|
349e0502 |
if (hdr->sbat_root_start < 0) { |
47bbbc56 |
cli_errmsg("No root start block\n");
return FALSE;
}
|
bf5a1ce7 |
block_count = sbat_index / 8; /* 8 small blocks per big block */ |
349e0502 |
current_block = hdr->sbat_root_start; |
47bbbc56 |
while (block_count > 0) { |
1dc96b6c |
current_block = ole2_get_next_block_number(fd, hdr, current_block); |
47bbbc56 |
block_count--;
}
/* current_block now contains the block number of the sbat array
containing the entry for the required small block */
return(ole2_read_block(fd, hdr, buff, current_block));
}
/* Read the property tree.
It is read as just an array rather than a tree */ |
bda5598b |
/* |
e0f90556 |
static void ole2_read_property_tree(int fd, ole2_header_t *hdr, const char *dir, |
d95b7372 |
int (*handler)(int fd, ole2_header_t *hdr, property_t *prop, const char *dir)) |
47bbbc56 |
{
property_t prop_block[4]; |
d95b7372 |
int32_t index, current_block, count=0; |
47bbbc56 |
current_block = hdr->prop_start;
while(current_block >= 0) { |
767c51ae |
if (!ole2_read_block(fd, hdr, prop_block,
current_block)) {
return;
} |
47bbbc56 |
for (index=0 ; index < 4 ; index++) { |
349e0502 |
if (prop_block[index].type > 0) { |
c83b8845 |
prop_block[index].name_size = ole2_endian_convert_16(prop_block[index].name_size);
prop_block[index].prev = ole2_endian_convert_32(prop_block[index].prev);
prop_block[index].next = ole2_endian_convert_32(prop_block[index].next);
prop_block[index].child = ole2_endian_convert_32(prop_block[index].child); |
25ba8c63 |
prop_block[index].user_flags = ole2_endian_convert_32(prop_block[index].user_flags); |
c83b8845 |
prop_block[index].create_lowdate = ole2_endian_convert_32(prop_block[index].create_lowdate);
prop_block[index].create_highdate = ole2_endian_convert_32(prop_block[index].create_highdate);
prop_block[index].mod_lowdate = ole2_endian_convert_32(prop_block[index].mod_lowdate);
prop_block[index].mod_highdate = ole2_endian_convert_32(prop_block[index].mod_highdate);
prop_block[index].start_block = ole2_endian_convert_32(prop_block[index].start_block);
prop_block[index].size = ole2_endian_convert_32(prop_block[index].size); |
d95b7372 |
if (prop_block[index].type > 5) {
cli_dbgmsg("ERROR: invalid property type: %d\n", prop_block[index].type);
return;
} |
47bbbc56 |
if (prop_block[index].type == 5) { |
349e0502 |
hdr->sbat_root_start = prop_block[index].start_block; |
47bbbc56 |
}
print_ole2_property(&prop_block[index]); |
d95b7372 |
if (!handler(fd, hdr, &prop_block[index], dir)) {
cli_dbgmsg("ERROR: handler failed\n");
return;
} |
47bbbc56 |
}
}
current_block = ole2_get_next_block_number(fd, hdr, current_block); |
d95b7372 |
if (++count > 100000) {
cli_dbgmsg("ERROR: loop detected\n");
return;
} |
47bbbc56 |
}
return;
} |
bda5598b |
*/ |
47bbbc56 |
|
ad2f8396 |
static void ole2_walk_property_tree(int fd, ole2_header_t *hdr, const char *dir, int32_t prop_index,
int (*handler)(int fd, ole2_header_t *hdr, property_t *prop, const char *dir), |
bda5598b |
unsigned int rec_level, unsigned int *file_count, const struct cl_limits *limits) |
ad2f8396 |
{
property_t prop_block[4]; |
cdcb8f73 |
int32_t index, current_block, i; |
bda5598b |
char *dirname; |
f8c58685 |
|
ad2f8396 |
current_block = hdr->prop_start; |
f8c58685 |
|
e5a5b2f2 |
if ((prop_index < 0) || (rec_level > 100) || (*file_count > 100000)) {
return;
} |
8a9c2d19 |
|
42034091 |
if (limits && limits->maxfiles && (*file_count > limits->maxfiles)) { |
e5a5b2f2 |
cli_dbgmsg("OLE2: File limit reached (max: %d)\n", limits->maxfiles); |
ad2f8396 |
return;
}
|
42034091 |
if (limits && limits->maxreclevel && (rec_level > limits->maxreclevel)) {
cli_dbgmsg("OLE2: Recursion limit reached (max: %d)\n", limits->maxreclevel);
return;
}
|
ad2f8396 |
index = prop_index / 4;
for (i=0 ; i < index ; i++) {
current_block = ole2_get_next_block_number(fd, hdr, current_block);
if (current_block < 0) {
return;
}
}
index = prop_index % 4;
if (!ole2_read_block(fd, hdr, prop_block,
current_block)) {
return;
}
if (prop_block[index].type <= 0) {
return;
}
prop_block[index].name_size = ole2_endian_convert_16(prop_block[index].name_size);
prop_block[index].prev = ole2_endian_convert_32(prop_block[index].prev);
prop_block[index].next = ole2_endian_convert_32(prop_block[index].next);
prop_block[index].child = ole2_endian_convert_32(prop_block[index].child);
prop_block[index].user_flags = ole2_endian_convert_32(prop_block[index].user_flags);
prop_block[index].create_lowdate = ole2_endian_convert_32(prop_block[index].create_lowdate);
prop_block[index].create_highdate = ole2_endian_convert_32(prop_block[index].create_highdate);
prop_block[index].mod_lowdate = ole2_endian_convert_32(prop_block[index].mod_lowdate);
prop_block[index].mod_highdate = ole2_endian_convert_32(prop_block[index].mod_highdate);
prop_block[index].start_block = ole2_endian_convert_32(prop_block[index].start_block);
prop_block[index].size = ole2_endian_convert_32(prop_block[index].size);
print_ole2_property(&prop_block[index]); |
8a9c2d19 |
/* Check we aren't in a loop */
if (cli_bitset_test(hdr->bitset, (unsigned long) prop_index)) {
/* Loop in property tree detected */
cli_dbgmsg("OLE2: Property tree loop detected at index %d\n", prop_index);
return;
}
if (!cli_bitset_set(hdr->bitset, (unsigned long) prop_index)) {
return;
}
|
ad2f8396 |
switch (prop_block[index].type) {
case 5: /* Root Entry */ |
cd9833ef |
if ((prop_index != 0) || (rec_level !=0) || |
e5a5b2f2 |
(*file_count != 0)) { |
ad2f8396 |
/* Can only have RootEntry as the top */
cli_dbgmsg("ERROR: illegal Root Entry\n");
return;
}
hdr->sbat_root_start = prop_block[index].start_block;
ole2_walk_property_tree(fd, hdr, dir, |
e5a5b2f2 |
prop_block[index].prev, handler, rec_level+1, file_count, limits); |
ad2f8396 |
ole2_walk_property_tree(fd, hdr, dir, |
e5a5b2f2 |
prop_block[index].next, handler, rec_level+1, file_count, limits); |
ad2f8396 |
ole2_walk_property_tree(fd, hdr, dir, |
e5a5b2f2 |
prop_block[index].child, handler, rec_level+1, file_count, limits); |
ad2f8396 |
break;
case 2: /* File */ |
e5a5b2f2 |
(*file_count)++; |
ad2f8396 |
if (!handler(fd, hdr, &prop_block[index], dir)) {
cli_dbgmsg("ERROR: handler failed\n"); |
c30e4a78 |
/* If we don't return on this error then
we can sometimes pull VBA code
from corrupted files.
*/ |
e5a5b2f2 |
|
ad2f8396 |
}
ole2_walk_property_tree(fd, hdr, dir, |
e5a5b2f2 |
prop_block[index].prev, handler, rec_level, file_count, limits); |
ad2f8396 |
ole2_walk_property_tree(fd, hdr, dir, |
e5a5b2f2 |
prop_block[index].next, handler, rec_level, file_count, limits); |
ad2f8396 |
ole2_walk_property_tree(fd, hdr, dir, |
e5a5b2f2 |
prop_block[index].child, handler, rec_level, file_count, limits); |
ad2f8396 |
break;
case 1: /* Directory */
dirname = (char *) cli_malloc(strlen(dir)+8);
if (!dirname) {
return;
}
snprintf(dirname, strlen(dir)+8, "%s/%.6d", dir, prop_index); |
cd9833ef |
if (mkdir(dirname, 0700) != 0) {
free(dirname);
return;
} |
ad2f8396 |
cli_dbgmsg("OLE2 dir entry: %s\n",dirname); |
467f8b1e |
ole2_walk_property_tree(fd, hdr, dir, |
e5a5b2f2 |
prop_block[index].prev, handler, rec_level+1, file_count, limits); |
467f8b1e |
ole2_walk_property_tree(fd, hdr, dir, |
e5a5b2f2 |
prop_block[index].next, handler, rec_level+1, file_count, limits); |
ad2f8396 |
ole2_walk_property_tree(fd, hdr, dirname, |
e5a5b2f2 |
prop_block[index].child, handler, rec_level+1, file_count, limits); |
ad2f8396 |
free(dirname);
break;
default: |
c361c7fa |
cli_dbgmsg("ERROR: unknown OLE2 entry type: %d\n", prop_block[index].type); |
ad2f8396 |
break;
}
return;
} |
47bbbc56 |
/* Write file Handler - write the contents of the entry to a file */ |
e0f90556 |
static int handler_writefile(int fd, ole2_header_t *hdr, property_t *prop, const char *dir) |
47bbbc56 |
{ |
3c21733d |
unsigned char *buff; |
1dc96b6c |
int32_t current_block, ofd, len, offset; |
47bbbc56 |
char *name, *newname; |
42438bd5 |
bitset_t *blk_bitset; |
47bbbc56 |
if (prop->type != 2) { |
bf5a1ce7 |
/* Not a file */ |
d95b7372 |
return TRUE;
}
if (prop->name_size > 64) {
cli_dbgmsg("\nERROR: property name too long: %d\n", prop->name_size);
return FALSE; |
47bbbc56 |
}
if (! (name = get_property_name(prop->name, prop->name_size))) { |
349e0502 |
/* File without a name - create a name for it */ |
cdcb8f73 |
off_t i; |
349e0502 |
i = lseek(fd, 0, SEEK_CUR); |
d95b7372 |
name = (char *) cli_malloc(11); |
349e0502 |
if (!name) { |
d95b7372 |
return FALSE; |
349e0502 |
} |
cdcb8f73 |
snprintf(name, 11, "%.10ld", i + (long int) prop); |
72d885e8 |
} else {
/* Sanitize the file name */ |
11b50569 |
sanitiseName(name); |
47bbbc56 |
}
newname = (char *) cli_malloc(strlen(name) + strlen(dir) + 2); |
bf34c7e7 |
if (!newname) {
free(name);
return FALSE;
} |
b58fdfc2 |
|
47bbbc56 |
sprintf(newname, "%s/%s", dir, name);
free(name);
|
b58fdfc2 |
ofd = open(newname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, S_IRWXU); |
47bbbc56 |
if (ofd < 0) { |
b36af080 |
cli_errmsg("ERROR: failed to create file: %s\n", newname);
free(newname); |
d95b7372 |
return FALSE; |
47bbbc56 |
}
free(newname);
current_block = prop->start_block;
len = prop->size;
|
3c21733d |
buff = (unsigned char *) cli_malloc(1 << hdr->log2_big_block_size);
if (!buff) {
close(ofd);
return FALSE;
} |
42438bd5 |
blk_bitset = cli_bitset_init();
if (!blk_bitset) {
cli_errmsg("ERROR [handler_writefile]: init bitset failed\n");
close(ofd);
return FALSE;
} |
47bbbc56 |
while((current_block >= 0) && (len > 0)) { |
42438bd5 |
/* Check we aren't in a loop */
if (cli_bitset_test(blk_bitset, (unsigned long) current_block)) {
/* Loop in block list */
cli_dbgmsg("OLE2: Block list loop detected\n");
close(ofd);
free(buff);
cli_bitset_free(blk_bitset);
return FALSE;
}
if (!cli_bitset_set(blk_bitset, (unsigned long) current_block)) {
close(ofd);
free(buff);
cli_bitset_free(blk_bitset);
return FALSE;
} |
9d3c38ba |
if (prop->size < (int64_t)hdr->sbat_cutoff) { |
bf5a1ce7 |
/* Small block file */ |
3c21733d |
if (!ole2_get_sbat_data_block(fd, hdr, buff, current_block)) { |
47bbbc56 |
cli_dbgmsg("ole2_get_sbat_data_block failed\n");
close(ofd); |
3c21733d |
free(buff); |
42438bd5 |
cli_bitset_free(blk_bitset); |
d95b7372 |
return FALSE; |
47bbbc56 |
} |
bf5a1ce7 |
/* buff now contains the block with 8 small blocks in it */ |
47bbbc56 |
offset = 64 * (current_block % 8); |
5b25b5e8 |
if (cli_writen(ofd, &buff[offset], MIN(len,64)) != MIN(len,64)) { |
47bbbc56 |
close(ofd); |
3c21733d |
free(buff); |
42438bd5 |
cli_bitset_free(blk_bitset); |
d95b7372 |
return FALSE; |
47bbbc56 |
}
len -= MIN(len,64);
current_block = ole2_get_next_sbat_block(fd, hdr, current_block);
} else { |
bf5a1ce7 |
/* Big block file */ |
3c21733d |
if (!ole2_read_block(fd, hdr, buff, current_block)) { |
47bbbc56 |
close(ofd); |
3c21733d |
free(buff); |
42438bd5 |
cli_bitset_free(blk_bitset); |
d95b7372 |
return FALSE; |
47bbbc56 |
} |
3c21733d |
if (cli_writen(ofd, buff, MIN(len,(1 << hdr->log2_big_block_size))) != |
349e0502 |
MIN(len,(1 << hdr->log2_big_block_size))) { |
47bbbc56 |
close(ofd); |
3c21733d |
free(buff); |
42438bd5 |
cli_bitset_free(blk_bitset); |
d95b7372 |
return FALSE; |
47bbbc56 |
}
current_block = ole2_get_next_block_number(fd, hdr, current_block); |
349e0502 |
len -= MIN(len,(1 << hdr->log2_big_block_size)); |
47bbbc56 |
}
}
close(ofd); |
3c21733d |
free(buff); |
42438bd5 |
cli_bitset_free(blk_bitset); |
d95b7372 |
return TRUE; |
47bbbc56 |
}
|
ef38c83d |
#if !defined(HAVE_ATTRIB_PACKED) && !defined(HAVE_PRAGMA_PACK) && !defined(HAVE_PRAGMA_PACK_HPPA) |
e0f90556 |
static int ole2_read_header(int fd, ole2_header_t *hdr) |
1dc96b6c |
{
int i;
|
5b25b5e8 |
if (cli_readn(fd, &hdr->magic, 8) != 8) { |
1dc96b6c |
return FALSE;
} |
5b25b5e8 |
if (cli_readn(fd, &hdr->clsid, 16) != 16) { |
1dc96b6c |
return FALSE;
} |
5b25b5e8 |
if (cli_readn(fd, &hdr->minor_version, 2) != 2) { |
1dc96b6c |
return FALSE;
} |
5b25b5e8 |
if (cli_readn(fd, &hdr->dll_version, 2) != 2) { |
1dc96b6c |
return FALSE;
} |
5b25b5e8 |
if (cli_readn(fd, &hdr->byte_order, 2) != 2) { |
1dc96b6c |
return FALSE;
} |
5b25b5e8 |
if (cli_readn(fd, &hdr->log2_big_block_size, 2) != 2) { |
1dc96b6c |
return FALSE;
} |
5b25b5e8 |
if (cli_readn(fd, &hdr->log2_small_block_size, 4) != 4) { |
1dc96b6c |
return FALSE;
} |
5b25b5e8 |
if (cli_readn(fd, &hdr->reserved, 8) != 8) { |
1dc96b6c |
return FALSE;
} |
5b25b5e8 |
if (cli_readn(fd, &hdr->bat_count, 4) != 4) { |
1dc96b6c |
return FALSE;
} |
5b25b5e8 |
if (cli_readn(fd, &hdr->prop_start, 4) != 4) { |
1dc96b6c |
return FALSE;
} |
5b25b5e8 |
if (cli_readn(fd, &hdr->signature, 4) != 4) { |
1dc96b6c |
return FALSE;
} |
5b25b5e8 |
if (cli_readn(fd, &hdr->sbat_cutoff, 4) != 4) { |
1dc96b6c |
return FALSE;
} |
5b25b5e8 |
if (cli_readn(fd, &hdr->sbat_start, 4) != 4) { |
1dc96b6c |
return FALSE;
} |
5b25b5e8 |
if (cli_readn(fd, &hdr->sbat_block_count, 4) != 4) { |
1dc96b6c |
return FALSE;
} |
5b25b5e8 |
if (cli_readn(fd, &hdr->xbat_start, 4) != 4) { |
1dc96b6c |
return FALSE;
} |
5b25b5e8 |
if (cli_readn(fd, &hdr->xbat_count, 4) != 4) { |
1dc96b6c |
return FALSE;
}
for (i=0 ; i < 109 ; i++) { |
5b25b5e8 |
if (cli_readn(fd, &hdr->bat_array[i], 4) != 4) { |
1dc96b6c |
return FALSE;
}
}
return TRUE;
} |
bda5598b |
#endif |
1dc96b6c |
|
e5a5b2f2 |
int cli_ole2_extract(int fd, const char *dirname, const struct cl_limits *limits) |
47bbbc56 |
{
ole2_header_t hdr; |
1dc96b6c |
int hdr_size; |
8c030302 |
struct stat statbuf; |
bda5598b |
unsigned int file_count=0; |
1dc96b6c |
|
47bbbc56 |
cli_dbgmsg("in cli_ole2_extract()\n"); |
1dc96b6c |
|
349e0502 |
/* size of header - size of other values in struct */ |
8c030302 |
hdr_size = sizeof(struct ole2_header_tag) - sizeof(int32_t) - |
8a9c2d19 |
sizeof(unsigned char *) - sizeof(off_t) - sizeof(bitset_t *); |
1dc96b6c |
|
8c030302 |
hdr.m_area = NULL;
#ifdef HAVE_MMAP
if (fstat(fd, &statbuf) == 0) { |
4c64f434 |
if (statbuf.st_size < hdr_size) {
return 0;
} |
8c030302 |
hdr.m_length = statbuf.st_size;
hdr.m_area = (unsigned char *) mmap(NULL, hdr.m_length, PROT_READ, MAP_PRIVATE, fd, 0);
if (hdr.m_area == MAP_FAILED) {
hdr.m_area = NULL; |
c96406a0 |
} else {
cli_dbgmsg("mmap'ed file\n");
memcpy(&hdr, hdr.m_area, hdr_size); |
8c030302 |
} |
1dc96b6c |
}
#endif |
349e0502 |
|
8c030302 |
if (hdr.m_area == NULL) { |
ef38c83d |
#if defined(HAVE_ATTRIB_PACKED) || defined(HAVE_PRAGMA_PACK) || defined(HAVE_PRAGMA_PACK_HPPA) |
8c030302 |
if (cli_readn(fd, &hdr, hdr_size) != hdr_size) {
return 0;
}
#else
if (!ole2_read_header(fd, &hdr)) {
return 0;
}
#endif
}
|
c83b8845 |
hdr.minor_version = ole2_endian_convert_16(hdr.minor_version);
hdr.dll_version = ole2_endian_convert_16(hdr.dll_version);
hdr.byte_order = ole2_endian_convert_16(hdr.byte_order);
hdr.log2_big_block_size = ole2_endian_convert_16(hdr.log2_big_block_size);
hdr.log2_small_block_size = ole2_endian_convert_32(hdr.log2_small_block_size);
hdr.bat_count = ole2_endian_convert_32(hdr.bat_count);
hdr.prop_start = ole2_endian_convert_32(hdr.prop_start);
hdr.sbat_cutoff = ole2_endian_convert_32(hdr.sbat_cutoff);
hdr.sbat_start = ole2_endian_convert_32(hdr.sbat_start);
hdr.sbat_block_count = ole2_endian_convert_32(hdr.sbat_block_count);
hdr.xbat_start = ole2_endian_convert_32(hdr.xbat_start);
hdr.xbat_count = ole2_endian_convert_32(hdr.xbat_count);
|
349e0502 |
hdr.sbat_root_start = -1; |
8a9c2d19 |
hdr.bitset = cli_bitset_init();
if (!hdr.bitset) {
return CL_EOLE2;
}
|
bda5598b |
if (memcmp(hdr.magic, magic_id, 8) != 0) { |
47bbbc56 |
cli_dbgmsg("OLE2 magic failed!\n"); |
b36af080 |
#ifdef HAVE_MMAP
if (hdr.m_area != NULL) {
munmap(hdr.m_area, hdr.m_length);
}
#endif |
8a9c2d19 |
cli_bitset_free(hdr.bitset); |
47bbbc56 |
return CL_EOLE2;
}
if (hdr.log2_big_block_size != 9) { |
4b6040a4 |
cli_errmsg("WARNING: not scanned; untested big block size - please report\n");
goto abort; |
47bbbc56 |
}
if (hdr.log2_small_block_size != 6) { |
4b6040a4 |
cli_errmsg("WARNING: not scanned; untested small block size - please report\n");
goto abort; |
47bbbc56 |
}
if (hdr.sbat_cutoff != 4096) { |
4b6040a4 |
cli_errmsg("WARNING: not scanned; untested sbat cutoff - please report\n");
goto abort; |
47bbbc56 |
} |
8c030302 |
|
47bbbc56 |
print_ole2_header(&hdr);
|
ad2f8396 |
/* NOTE: Select only ONE of the following two methods */
|
467f8b1e |
/* ole2_read_property_tree(fd, &hdr, dirname, handler_writefile); */ |
ad2f8396 |
/* OR */
|
e5a5b2f2 |
ole2_walk_property_tree(fd, &hdr, dirname, 0, handler_writefile, 0, &file_count, limits); |
8c030302 |
|
4b6040a4 |
abort: |
8c030302 |
#ifdef HAVE_MMAP
if (hdr.m_area != NULL) {
munmap(hdr.m_area, hdr.m_length);
}
#endif |
8a9c2d19 |
cli_bitset_free(hdr.bitset); |
47bbbc56 |
return 0;
} |