b151ef55 |
/*
* Copyright (C) 2002 Nigel Horne <njh@bandsman.co.uk>
*
* 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 |
30738099 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA. |
b151ef55 |
*/ |
690d27ca |
static char const rcsid[] = "$Id: blob.c,v 1.64 2007/02/12 22:25:14 njh Exp $"; |
8b242bb9 |
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif |
b151ef55 |
|
40d54f7f |
#ifdef C_WINDOWS
#include "stdafx.h" |
e9f9dc99 |
#include <io.h> |
40d54f7f |
#endif
|
1e06e1ab |
#include <stdio.h> |
b151ef55 |
#include <stdlib.h>
#include <string.h> |
1e06e1ab |
#include <errno.h> |
b566d781 |
#include <fcntl.h> |
1e06e1ab |
|
e9f9dc99 |
#ifdef HAVE_SYS_PARAM_H |
5eeffbb9 |
#include <sys/param.h> /* for NAME_MAX */ |
40d54f7f |
#endif |
1e06e1ab |
|
88d3f0be |
#ifdef C_DARWIN |
b151ef55 |
#include <sys/types.h>
#endif |
1e06e1ab |
|
5fe84eaa |
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
|
5c86c162 |
#include "others.h" |
b151ef55 |
#include "mbox.h" |
5c86c162 |
#include "matcher.h" |
b151ef55 |
#ifndef CL_DEBUG
#define NDEBUG /* map CLAMAV debug onto standard */
#endif
|
7f80268d |
#ifndef O_BINARY
#define O_BINARY 0
#endif
|
b151ef55 |
#include <assert.h>
|
40d54f7f |
#if defined(C_MINGW) || defined(C_WINDOWS) |
df78d3be |
#include <windows.h>
#endif
|
ab255abf |
#define MAX_SCAN_SIZE 20*1024 /*
* The performance benefit of scanning
* early disappears on medium and
* large sized files
*/
|
b151ef55 |
blob *
blobCreate(void)
{
#ifdef CL_DEBUG
blob *b = (blob *)cli_calloc(1, sizeof(blob)); |
b23b3379 |
if(b) |
df78d3be |
b->magic = BLOBCLASS; |
27a375f2 |
cli_dbgmsg("blobCreate\n"); |
b151ef55 |
return b;
#else
return (blob *)cli_calloc(1, sizeof(blob));
#endif
}
void
blobDestroy(blob *b)
{ |
27a375f2 |
#ifdef CL_DEBUG
cli_dbgmsg("blobDestroy %d\n", b->magic);
#else
cli_dbgmsg("blobDestroy\n");
#endif
|
b151ef55 |
assert(b != NULL); |
df78d3be |
assert(b->magic == BLOBCLASS); |
b151ef55 |
if(b->name)
free(b->name);
if(b->data)
free(b->data);
#ifdef CL_DEBUG |
df78d3be |
b->magic = INVALIDCLASS; |
b151ef55 |
#endif
free(b);
}
void
blobArrayDestroy(blob *blobList[], int n)
{ |
4d9c0ca8 |
assert(blobList != NULL);
|
27a375f2 |
while(--n >= 0) {
cli_dbgmsg("blobArrayDestroy: %d\n", n);
if(blobList[n]) {
blobDestroy(blobList[n]);
blobList[n] = NULL;
}
} |
b151ef55 |
}
|
e6b25cd3 |
/*ARGSUSED*/ |
b151ef55 |
void |
e6b25cd3 |
blobSetFilename(blob *b, const char *dir, const char *filename) |
b151ef55 |
{
assert(b != NULL); |
df78d3be |
assert(b->magic == BLOBCLASS); |
b151ef55 |
assert(filename != NULL);
|
f92f5b94 |
cli_dbgmsg("blobSetFilename: %s\n", filename);
|
b151ef55 |
if(b->name)
free(b->name);
|
f92f5b94 |
b->name = strdup(filename); |
b151ef55 |
|
f92f5b94 |
if(b->name)
sanitiseName(b->name); |
b151ef55 |
}
const char *
blobGetFilename(const blob *b)
{
assert(b != NULL); |
df78d3be |
assert(b->magic == BLOBCLASS); |
b151ef55 |
|
1e06e1ab |
return b->name; |
b151ef55 |
}
|
ff0cb11f |
int |
b151ef55 |
blobAddData(blob *b, const unsigned char *data, size_t len)
{ |
4d9c0ca8 |
#ifdef HAVE_GETPAGESIZE |
0d252351 |
static int pagesize;
int growth; |
4d9c0ca8 |
#endif
|
b151ef55 |
assert(b != NULL); |
df78d3be |
assert(b->magic == BLOBCLASS); |
b151ef55 |
assert(data != NULL);
if(len == 0) |
ff0cb11f |
return 0; |
b151ef55 |
|
c7256385 |
if(b->isClosed) {
/*
* Should be cli_dbgmsg, but I want to see them for now,
* and cli_dbgmsg doesn't support debug levels
*/
cli_warnmsg("Reopening closed blob\n");
b->isClosed = 0;
} |
4d9c0ca8 |
/*
* The payoff here is between reducing the number of calls to
* malloc/realloc and not overallocating memory. A lot of machines
* are more tight with memory than one may imagine which is why
* we don't just allocate a *huge* amount and be done with it. Closing
* the blob helps because that reclaims memory. If you know the maximum
* size of a blob before you start adding data, use blobGrow() that's
* the most optimum
*/
#ifdef HAVE_GETPAGESIZE |
0d252351 |
if(pagesize == 0) {
pagesize = getpagesize();
if(pagesize == 0)
pagesize = 4096;
}
growth = pagesize; |
4e47c534 |
if(len >= (size_t)pagesize) |
0d252351 |
growth = ((len / pagesize) + 1) * pagesize;
|
985cc85e |
/*cli_dbgmsg("blobGrow: b->size %lu, b->len %lu, len %lu, growth = %u\n",
b->size, b->len, len, growth);*/ |
0d252351 |
|
4d9c0ca8 |
if(b->data == NULL) {
assert(b->len == 0);
assert(b->size == 0);
|
0d252351 |
b->size = growth;
b->data = cli_malloc(growth); |
86b0edc1 |
} else if(b->size < b->len + (off_t)len) { |
0d252351 |
unsigned char *p = cli_realloc(b->data, b->size + growth); |
4d9c0ca8 |
if(p == NULL) |
ff0cb11f |
return -1; |
4d9c0ca8 |
|
0d252351 |
b->size += growth; |
4d9c0ca8 |
b->data = p;
}
#else |
b151ef55 |
if(b->data == NULL) {
assert(b->len == 0); |
02c9dc2a |
assert(b->size == 0);
|
b151ef55 |
b->size = len * 4;
b->data = cli_malloc(b->size);
} else if(b->size < b->len + len) { |
c42ab0b0 |
unsigned char *p = cli_realloc(b->data, b->size + (len * 4));
if(p == NULL) |
ff0cb11f |
return -1; |
c42ab0b0 |
|
02c9dc2a |
b->size += len * 4; |
c42ab0b0 |
b->data = p; |
b151ef55 |
} |
4d9c0ca8 |
#endif |
b151ef55 |
|
b23b3379 |
if(b->data) {
memcpy(&b->data[b->len], data, len);
b->len += len;
} |
ff0cb11f |
return 0; |
b151ef55 |
}
|
6a91c55b |
unsigned char * |
b151ef55 |
blobGetData(const blob *b)
{
assert(b != NULL); |
df78d3be |
assert(b->magic == BLOBCLASS); |
b151ef55 |
|
2c66271b |
if(b->len == 0)
return NULL; |
b151ef55 |
return(b->data);
}
|
40d54f7f |
size_t |
b151ef55 |
blobGetDataSize(const blob *b)
{
assert(b != NULL); |
df78d3be |
assert(b->magic == BLOBCLASS); |
b151ef55 |
return(b->len);
} |
c7256385 |
void
blobClose(blob *b)
{ |
4d9c0ca8 |
assert(b != NULL); |
df78d3be |
assert(b->magic == BLOBCLASS); |
4fabe242 |
if(b->isClosed) { |
aea1b159 |
cli_warnmsg("Attempt to close a previously closed blob\n"); |
4fabe242 |
return;
} |
4d9c0ca8 |
|
8a88fb93 |
/*
* Nothing more is going to be added to this blob. If it'll save more
* than a trivial amount (say 64 bytes) of memory, shrink the allocation
*/
if((b->size - b->len) >= 64) { |
4d9c0ca8 |
if(b->len == 0) { /* Not likely */
free(b->data);
b->data = NULL; |
86b0edc1 |
cli_dbgmsg("blobClose: recovered all %lu bytes\n", |
dbca666a |
(unsigned long)b->size); |
4d9c0ca8 |
b->size = 0;
} else {
unsigned char *ptr = cli_realloc(b->data, b->len);
if(ptr == NULL)
return;
|
86b0edc1 |
cli_dbgmsg("blobClose: recovered %lu bytes from %lu\n", |
dbca666a |
(unsigned long)(b->size - b->len),
(unsigned long)b->size); |
4d9c0ca8 |
b->size = b->len;
b->data = ptr;
} |
c7256385 |
} |
8114307c |
b->isClosed = 1; |
c7256385 |
}
/*
* Returns 0 if the blobs are the same
*/
int
blobcmp(const blob *b1, const blob *b2)
{ |
40d54f7f |
size_t s1, s2; |
c7256385 |
assert(b1 != NULL);
assert(b2 != NULL);
if(b1 == b2)
return 0;
s1 = blobGetDataSize(b1);
s2 = blobGetDataSize(b2);
if(s1 != s2)
return 1;
|
4d9c0ca8 |
if((s1 == 0) && (s2 == 0))
return 0;
|
c7256385 |
return memcmp(blobGetData(b1), blobGetData(b2), s1);
} |
02c9dc2a |
|
9b2be218 |
/*
* Return clamav return code
*/
int |
02c9dc2a |
blobGrow(blob *b, size_t len)
{
assert(b != NULL); |
df78d3be |
assert(b->magic == BLOBCLASS); |
02c9dc2a |
if(len == 0) |
9b2be218 |
return CL_SUCCESS; |
02c9dc2a |
if(b->isClosed) {
/*
* Should be cli_dbgmsg, but I want to see them for now,
* and cli_dbgmsg doesn't support debug levels
*/
cli_warnmsg("Growing closed blob\n");
b->isClosed = 0;
}
if(b->data == NULL) {
assert(b->len == 0);
assert(b->size == 0);
b->data = cli_malloc(len); |
8114307c |
if(b->data)
b->size = len; |
02c9dc2a |
} else { |
8114307c |
unsigned char *ptr = cli_realloc(b->data, b->size + len);
if(ptr) {
b->size += len;
b->data = ptr;
} |
02c9dc2a |
} |
9b2be218 |
return (b->data) ? CL_SUCCESS : CL_EMEM; |
02c9dc2a |
} |
1e06e1ab |
fileblob *
fileblobCreate(void)
{
#ifdef CL_DEBUG
fileblob *fb = (fileblob *)cli_calloc(1, sizeof(fileblob));
if(fb) |
df78d3be |
fb->b.magic = BLOBCLASS; |
1e06e1ab |
cli_dbgmsg("blobCreate\n");
return fb;
#else
return (fileblob *)cli_calloc(1, sizeof(fileblob));
#endif
}
void
fileblobDestroy(fileblob *fb)
{
assert(fb != NULL); |
11d01b40 |
assert(fb->b.magic == BLOBCLASS); |
1e06e1ab |
|
84a3c2c2 |
if(fb->b.name && fb->fp) { |
11d01b40 |
fclose(fb->fp);
cli_dbgmsg("fileblobDestroy: %s\n", fb->b.name);
if(!fb->isNotEmpty) { |
4d9c0ca8 |
cli_dbgmsg("fileblobDestroy: not saving empty file\n");
unlink(fb->b.name);
} |
1e06e1ab |
free(fb->b.name);
assert(fb->b.data == NULL);
} else if(fb->b.data) {
free(fb->b.data); |
5e92a55d |
if(fb->b.name) { |
690d27ca |
cli_errmsg("fileblobDestroy: %s not saved: report to http://bugs.clamav.net\n", fb->b.name); |
84a3c2c2 |
free(fb->b.name); |
5e92a55d |
} else |
dbca666a |
cli_errmsg("fileblobDestroy: file not saved (%lu bytes): report to http://bugs.clamav.net\n",
(unsigned long)fb->b.len); |
1e06e1ab |
} |
11d01b40 |
#ifdef CL_DEBUG
fb->b.magic = INVALIDCLASS;
#endif |
1e06e1ab |
free(fb);
}
void
fileblobSetFilename(fileblob *fb, const char *dir, const char *filename)
{
int fd;
char fullname[NAME_MAX + 1];
if(fb->b.name)
return;
|
bb1e30f6 |
assert(filename != NULL);
assert(dir != NULL);
|
e6b25cd3 |
blobSetFilename(&fb->b, dir, filename); |
1e06e1ab |
/*
* Reload the filename, it may be different from the one we've
* asked for, e.g. '/'s taken out
*/
filename = blobGetFilename(&fb->b);
|
bb1e30f6 |
assert(filename != NULL);
|
3d68da87 |
#ifdef C_QNX6
/*
* QNX6 support from mikep@kaluga.org to fix bug where mkstemp
* can return ETOOLONG even when the file name isn't too long
*/
snprintf(fullname, sizeof(fullname), "%s/clamavtmpXXXXXXXXXXXXX", dir); |
40d54f7f |
#elif defined(C_WINDOWS) |
e9f9dc99 |
sprintf_s(fullname, sizeof(fullname) - 1, "%s\\%.*sXXXXXX", dir, |
40d54f7f |
(int)(sizeof(fullname) - 9 - strlen(dir)), filename); |
3d68da87 |
#else |
40d54f7f |
sprintf(fullname, "%s/%.*sXXXXXX", dir, |
11d01b40 |
(int)(sizeof(fullname) - 9 - strlen(dir)), filename); |
3d68da87 |
#endif
#if defined(C_LINUX) || defined(C_BSD) || defined(HAVE_MKSTEMP) || defined(C_SOLARIS) || defined(C_CYGWIN) || defined(C_QNX6) |
0d252351 |
cli_dbgmsg("fileblobSetFilename: mkstemp(%s)\n", fullname); |
1e06e1ab |
fd = mkstemp(fullname); |
bc5281bd |
if((fd < 0) && (errno == EINVAL)) {
/*
* This happens with some Linux flavours when (mis)handling
* filenames with foreign characters
*/
snprintf(fullname, sizeof(fullname), "%s/clamavtmpXXXXXXXXXXXXX", dir);
cli_dbgmsg("fileblobSetFilename: retry as mkstemp(%s)\n", fullname);
fd = mkstemp(fullname);
} |
40d54f7f |
#elif defined(C_WINDOWS)
cli_dbgmsg("fileblobSetFilename: _mktemp_s(%s)\n", fullname); |
e9f9dc99 |
if(_mktemp_s(fullname, strlen(fullname) + 1) != 0) {
char *name; |
5fe84eaa |
|
e9f9dc99 |
/* _mktemp_s only allows 26 files */
cli_dbgmsg("fileblobSetFilename: _mktemp_s(%s) failed: %s\n", fullname, strerror(errno));
name = cli_gentemp(dir);
if(name == NULL)
return;
fd = open(name, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC|O_BINARY, 0600);
free(name);
} else
fd = open(fullname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC|O_BINARY, 0600); |
1e06e1ab |
#else |
0703b66f |
cli_dbgmsg("fileblobSetFilename: mktemp(%s)\n", fullname); |
1e06e1ab |
(void)mktemp(fullname);
fd = open(fullname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC|O_BINARY, 0600);
#endif
if(fd < 0) {
cli_errmsg("Can't create temporary file %s: %s\n", fullname, strerror(errno)); |
dbca666a |
cli_dbgmsg("%lu %lu\n", (unsigned long)sizeof(fullname),
(unsigned long)strlen(fullname)); |
1e06e1ab |
return;
}
|
ab255abf |
cli_dbgmsg("Creating %s\n", fullname); |
1e06e1ab |
fb->fp = fdopen(fd, "wb");
if(fb->fp == NULL) {
cli_errmsg("Can't create file %s: %s\n", fullname, strerror(errno)); |
dbca666a |
cli_dbgmsg("%lu %lu\n", (unsigned long)sizeof(fullname),
(unsigned long)strlen(fullname)); |
1e06e1ab |
close(fd);
return;
} |
a74e1a5b |
if(fb->b.data)
if(fileblobAddData(fb, fb->b.data, fb->b.len) == 0) {
free(fb->b.data);
fb->b.data = NULL;
fb->b.len = fb->b.size = 0;
} |
1e06e1ab |
}
|
ff0cb11f |
int |
1e06e1ab |
fileblobAddData(fileblob *fb, const unsigned char *data, size_t len)
{
if(len == 0) |
ff0cb11f |
return 0; |
1e06e1ab |
|
11d01b40 |
assert(data != NULL);
|
1e06e1ab |
if(fb->fp) { |
ab255abf |
#if defined(MAX_SCAN_SIZE) && (MAX_SCAN_SIZE > 0)
const cli_ctx *ctx = fb->ctx;
|
b65d2aad |
if(fb->isInfected) /* pretend all was written */
return 0; |
ab255abf |
if(ctx) {
int do_scan = 1;
if(ctx->limits)
if(fb->bytes_scanned >= ctx->limits->maxfilesize)
do_scan = 0;
if(fb->bytes_scanned > MAX_SCAN_SIZE)
do_scan = 0;
if(do_scan) {
if(ctx->scanned)
*ctx->scanned += (unsigned long)len / CL_COUNT_PRECISION;
fb->bytes_scanned += (unsigned long)len;
|
b9cd4bef |
if((len > 5) && (cli_scanbuff(data, (unsigned int)len, ctx->virname, ctx->engine, CL_TYPE_UNKNOWN_DATA) == CL_VIRUS)) { |
ab255abf |
cli_dbgmsg("fileblobAddData: found %s\n", *ctx->virname);
fb->isInfected = 1;
} |
5c86c162 |
}
} |
ab255abf |
#endif |
5c86c162 |
|
ff0cb11f |
if(fwrite(data, len, 1, fb->fp) != 1) { |
dbca666a |
cli_errmsg("fileblobAddData: Can't write %lu bytes to temporary file %s: %s\n",
(unsigned long)len, fb->b.name, strerror(errno)); |
ff0cb11f |
return -1;
}
fb->isNotEmpty = 1;
return 0;
}
return blobAddData(&(fb->b), data, len); |
1e06e1ab |
}
const char *
fileblobGetFilename(const fileblob *fb)
{
return blobGetFilename(&(fb->b));
} |
f92f5b94 |
|
b65d2aad |
void
fileblobSetCTX(fileblob *fb, cli_ctx *ctx)
{
fb->ctx = ctx;
}
int
fileblobContainsVirus(const fileblob *fb)
{
return fb->isInfected ? TRUE : FALSE;
}
|
f92f5b94 |
/*
* Different operating systems allow different characters in their filenames |
a8dbefcd |
* FIXME: What does QNX want? There is no #ifdef C_QNX, but if there were
* it may be best to treat it like MSDOS |
f92f5b94 |
*/
void
sanitiseName(char *name)
{
while(*name) {
#ifdef C_DARWIN
*name &= '\177'; |
88d3f0be |
#endif |
eae54f60 |
/* Also check for tab - "Heinz Martin" <Martin@hemag.ch> */ |
1c8c5e09 |
#if defined(MSDOS) || defined(C_CYGWIN) || defined(C_WINDOWS) || defined(C_OS2) |
4f35d2fc |
if(strchr("%/*?<>|\\\"+=,;:\t ~", *name)) |
f92f5b94 |
#else
if(*name == '/')
#endif
*name = '_';
name++;
}
} |