various cleanups; fix bb#577 (move cli_* out of clamav.h)

git-svn: trunk@3196
remotes/push_mirror/metadata
Tomasz Kojm 19 years ago
parent 477961e942
commit a7ac59781d
  1. 4
      ChangeLog
  2. 22
      libclamav/clamav.h
  3. 130
      libclamav/cvd.c
  4. 1
      libclamav/jscript.c
  5. 2
      libclamav/matcher.c
  6. 15
      libclamav/matcher.h
  7. 1
      libclamav/mbox.c
  8. 2
      libclamav/nsis/nulsft.c
  9. 185
      libclamav/others.c
  10. 6
      libclamav/others.h
  11. 1
      libclamav/pe.c
  12. 2
      libclamav/readdb.c
  13. 8
      libclamav/rtf.c
  14. 79
      libclamav/scanners.c
  15. 16
      libclamav/sis.c
  16. 1
      libclamav/tnef.c

@ -1,3 +1,7 @@
Fri Aug 31 21:02:46 CEST 2007 (tk)
----------------------------------
* libclamav: various cleanups; fix bb#577 (move cli_* out of clamav.h)
Fri Aug 31 09:02:23 BST 2007 (njh)
----------------------------------
* clamav-milter: Bug 642

@ -109,22 +109,6 @@ extern "C"
#define cl_node cl_engine
#define cl_perror cl_strerror
/* internal structures */
struct cli_md5_node {
char *virname;
unsigned char *md5;
unsigned int size;
unsigned short fp;
struct cli_md5_node *next;
};
struct cli_meta_node {
int csize, size, method;
unsigned int crc32, fileno, encrypted, maxdepth;
char *filename, *virname;
struct cli_meta_node *next;
};
struct cl_engine {
unsigned int refcount; /* reference counter */
unsigned short ncore;
@ -135,16 +119,16 @@ struct cl_engine {
void **root;
/* MD5 */
struct cli_md5_node **md5_hlist;
void **md5_hlist;
/* B-M matcher for MD5 sigs for PE sections */
void *md5_sect;
/* Zip metadata */
struct cli_meta_node *zip_mlist;
void *zip_mlist;
/* RAR metadata */
struct cli_meta_node *rar_mlist;
void *rar_mlist;
/* NodalCore database handle */
void *ncdb;

@ -45,23 +45,29 @@
int cli_untgz(int fd, const char *destdir)
{
char *fullname, osize[13], name[101], type;
char *path, osize[13], name[101], type;
char block[TAR_BLOCKSIZE];
int nbytes, nread, nwritten, in_block = 0;
unsigned int size;
int nbytes, nread, nwritten, in_block = 0, fdd;
unsigned int size, pathlen = strlen(destdir) + 100 + 5;
FILE *outfile = NULL;
gzFile *infile;
cli_dbgmsg("in cli_untgz()\n");
if((infile = gzdopen(fd, "rb")) == NULL) {
cli_errmsg("Can't gzdopen() descriptor %d, errno = %d\n", fd, errno);
if((fdd = dup(fd)) == -1) {
cli_errmsg("cli_untgz: Can't duplicate descriptor %d\n", fd);
return -1;
}
if((infile = gzdopen(fdd, "rb")) == NULL) {
cli_errmsg("cli_untgz: Can't gzdopen() descriptor %d, errno = %d\n", fdd, errno);
return -1;
}
fullname = (char *) cli_calloc(sizeof(char), strlen(destdir) + 100 + 5);
if(!fullname) {
cli_errmsg("cli_untgz: Can't allocate memory for fullname\n");
path = (char *) cli_calloc(sizeof(char), pathlen);
if(!path) {
cli_errmsg("cli_untgz: Can't allocate memory for path\n");
return -1;
}
@ -69,12 +75,12 @@ int cli_untgz(int fd, const char *destdir)
nread = gzread(infile, block, TAR_BLOCKSIZE);
if(!in_block && nread == 0)
if(!in_block && !nread)
break;
if(nread != TAR_BLOCKSIZE) {
cli_errmsg("Incomplete block read.\n");
free(fullname);
cli_errmsg("cli_untgz: Incomplete block read\n");
free(path);
gzclose(infile);
return -1;
}
@ -87,16 +93,14 @@ int cli_untgz(int fd, const char *destdir)
name[100] = '\0';
if(strchr(name, '/')) {
cli_errmsg("Slash separators are not allowed in CVD.\n");
free(fullname);
cli_errmsg("cli_untgz: Slash separators are not allowed in CVD\n");
free(path);
gzclose(infile);
return -1;
}
strcpy(fullname, destdir);
strcat(fullname, "/");
strcat(fullname, name);
cli_dbgmsg("Unpacking %s\n",fullname);
snprintf(path, pathlen, "%s/%s", destdir, name);
cli_dbgmsg("cli_untgz: Unpacking %s\n", path);
type = block[156];
switch(type) {
@ -104,32 +108,31 @@ int cli_untgz(int fd, const char *destdir)
case '\0':
break;
case '5':
cli_errmsg("Directories in CVD are not supported.\n");
free(fullname);
cli_errmsg("cli_untgz: Directories are not supported in CVD\n");
free(path);
gzclose(infile);
return -1;
default:
cli_errmsg("Unknown type flag %c.\n",type);
free(fullname);
cli_errmsg("cli_untgz: Unknown type flag '%c'\n", type);
free(path);
gzclose(infile);
return -1;
}
in_block = 1;
if(outfile) {
if(fclose(outfile)) {
cli_errmsg("Cannot close file %s.\n", fullname);
free(fullname);
cli_errmsg("cli_untgz: Cannot close file %s\n", path);
free(path);
gzclose(infile);
return -1;
}
outfile = NULL;
}
if(!(outfile = fopen(fullname, "wb"))) {
cli_errmsg("Cannot create file %s.\n", fullname);
free(fullname);
if(!(outfile = fopen(path, "wb"))) {
cli_errmsg("cli_untgz: Cannot create file %s\n", path);
free(path);
gzclose(infile);
return -1;
}
@ -138,8 +141,8 @@ int cli_untgz(int fd, const char *destdir)
osize[12] = '\0';
if((sscanf(osize, "%o", &size)) == 0) {
cli_errmsg("Invalid size in header.\n");
free(fullname);
cli_errmsg("cli_untgz: Invalid size in header\n");
free(path);
gzclose(infile);
fclose(outfile);
return -1;
@ -150,8 +153,8 @@ int cli_untgz(int fd, const char *destdir)
nwritten = fwrite(block, 1, nbytes, outfile);
if(nwritten != nbytes) {
cli_errmsg("Wrote %d instead of %d (%s).\n", nwritten, nbytes, fullname);
free(fullname);
cli_errmsg("cli_untgz: Wrote %d instead of %d (%s)\n", nwritten, nbytes, path);
free(path);
gzclose(infile);
return -1;
}
@ -166,34 +169,34 @@ int cli_untgz(int fd, const char *destdir)
fclose(outfile);
gzclose(infile);
free(fullname);
free(path);
return 0;
}
struct cl_cvd *cl_cvdparse(const char *head)
{
char *pt;
struct cl_cvd *cvd;
char *pt;
if(strncmp(head, "ClamAV-VDB:", 11)) {
cli_dbgmsg("Not a CVD head.\n");
cli_errmsg("cli_cvdparse: Not a CVD file\n");
return NULL;
}
cvd = (struct cl_cvd *) cli_calloc(1, sizeof(struct cl_cvd));
if(!cvd) {
if(!(cvd = (struct cl_cvd *) cli_malloc(sizeof(struct cl_cvd)))) {
cli_errmsg("cl_cvdparse: Can't allocate memory for cvd\n");
return NULL;
}
if(!(cvd->time = cli_strtok(head, 1, ":"))) {
cli_errmsg("CVD -> Can't extract time from header.\n");
cli_errmsg("cli_cvdparse: Can't parse the creation time\n");
free(cvd);
return NULL;
}
if(!(pt = cli_strtok(head, 2, ":"))) {
cli_errmsg("CVD -> Can't extract version from header.\n");
cli_errmsg("cli_cvdparse: Can't parse the version number\n");
free(cvd->time);
free(cvd);
return NULL;
@ -202,7 +205,7 @@ struct cl_cvd *cl_cvdparse(const char *head)
free(pt);
if(!(pt = cli_strtok(head, 3, ":"))) {
cli_errmsg("CVD -> Can't extract signature number from header.\n");
cli_errmsg("cli_cvdparse: Can't parse the number of signatures\n");
free(cvd->time);
free(cvd);
return NULL;
@ -211,7 +214,7 @@ struct cl_cvd *cl_cvdparse(const char *head)
free(pt);
if(!(pt = cli_strtok(head, 4, ":"))) {
cli_errmsg("CVD -> Can't extract functionality level from header.\n");
cli_errmsg("cli_cvdparse: Can't parse the functionality level\n");
free(cvd->time);
free(cvd);
return NULL;
@ -220,14 +223,14 @@ struct cl_cvd *cl_cvdparse(const char *head)
free(pt);
if(!(cvd->md5 = cli_strtok(head, 5, ":"))) {
cli_errmsg("CVD -> Can't extract MD5 checksum from header.\n");
cli_errmsg("cli_cvdparse: Can't parse the MD5 checksum\n");
free(cvd->time);
free(cvd);
return NULL;
}
if(!(cvd->dsig = cli_strtok(head, 6, ":"))) {
cli_errmsg("CVD -> Can't extract digital signature from header.\n");
cli_errmsg("cli_cvdparse: Can't parse the digital signature\n");
free(cvd->time);
free(cvd->md5);
free(cvd);
@ -235,7 +238,7 @@ struct cl_cvd *cl_cvdparse(const char *head)
}
if(!(cvd->builder = cli_strtok(head, 7, ":"))) {
cli_errmsg("CVD -> Can't extract builder name from header.\n");
cli_errmsg("cli_cvdparse: Can't parse the builder name\n");
free(cvd->time);
free(cvd->md5);
free(cvd->dsig);
@ -246,9 +249,10 @@ struct cl_cvd *cl_cvdparse(const char *head)
if((pt = cli_strtok(head, 8, ":"))) {
cvd->stime = atoi(pt);
free(pt);
} else
cli_dbgmsg("CVD -> No creation time in seconds (old file format)\n");
} else {
cli_dbgmsg("cli_cvdparse: No creation time in seconds (old file format)\n");
cvd->stime = 0;
}
return cvd;
}
@ -262,12 +266,12 @@ struct cl_cvd *cl_cvdhead(const char *file)
if((fs = fopen(file, "rb")) == NULL) {
cli_dbgmsg("Can't open CVD file %s\n", file);
cli_errmsg("cl_cvdhead: Can't open file %s\n", file);
return NULL;
}
if(!(bread = fread(head, 1, 512, fs))) {
cli_errmsg("Can't read CVD header of %s\n", file);
cli_errmsg("cl_cvdhead: Can't read CVD header in %s\n", file);
fclose(fs);
return NULL;
}
@ -298,9 +302,10 @@ static int cli_cvdverify(FILE *fs, struct cl_cvd *cvdpt)
char *md5, head[513];
int i;
fseek(fs, 0, SEEK_SET);
if(fread(head, 1, 512, fs) != 512) {
cli_dbgmsg("Can't read CVD head from stream\n");
cli_errmsg("cli_cvdverify: Can't read CVD header\n");
return CL_ECVD;
}
@ -317,7 +322,7 @@ static int cli_cvdverify(FILE *fs, struct cl_cvd *cvdpt)
cli_dbgmsg("MD5(.tar.gz) = %s\n", md5);
if(strncmp(md5, cvd->md5, 32)) {
cli_dbgmsg("MD5 verification error.\n");
cli_dbgmsg("cli_cvdverify: MD5 verification error\n");
free(md5);
cl_cvdfree(cvd);
return CL_EMD5;
@ -325,7 +330,7 @@ static int cli_cvdverify(FILE *fs, struct cl_cvd *cvdpt)
#ifdef HAVE_GMP
if(cli_versig(md5, cvd->dsig)) {
cli_dbgmsg("Digital signature verification error.\n");
cli_dbgmsg("cli_cvdverify: Digital signature verification error\n");
free(md5);
cl_cvdfree(cvd);
return CL_EDSIG;
@ -342,8 +347,9 @@ int cl_cvdverify(const char *file)
FILE *fs;
int ret;
if((fs = fopen(file, "rb")) == NULL) {
cli_errmsg("Can't open CVD file %s\n", file);
cli_errmsg("cl_cvdverify: Can't open file %s\n", file);
return CL_EOPEN;
}
@ -357,7 +363,7 @@ int cli_cvdload(FILE *fs, struct cl_engine **engine, unsigned int *signo, short
{
char *dir;
struct cl_cvd cvd;
int ret, fd;
int ret;
time_t s_time;
@ -372,8 +378,8 @@ int cli_cvdload(FILE *fs, struct cl_engine **engine, unsigned int *signo, short
time(&s_time);
if((int) s_time - cvd.stime > 604800) {
cli_warnmsg("**************************************************\n");
cli_warnmsg("*** The virus database is older than 7 days. ***\n");
cli_warnmsg("*** Please update it IMMEDIATELY! ***\n");
cli_warnmsg("*** The virus database is older than 7 days! ***\n");
cli_warnmsg("*** Please update it as soon as possible. ***\n");
cli_warnmsg("**************************************************\n");
}
}
@ -385,14 +391,8 @@ int cli_cvdload(FILE *fs, struct cl_engine **engine, unsigned int *signo, short
cli_warnmsg("***********************************************************\n");
}
if((fd = dup(fileno(fs))) == -1) {
cli_errmsg("cli_cvdload(): Can't duplicate descriptor %d\n", fileno(fs));
return CL_EIO;
}
if(lseek(fd, 512, SEEK_SET) == -1) {
cli_errmsg("cli_cvdload(): Can't lseek descriptor %d\n", fd);
close(fd);
if(fseek(fs, 512, SEEK_SET) == -1) {
cli_errmsg("cli_cvdload(): fseek(fs, 512, SEEK_SET) failed\n");
return CL_EIO;
}
@ -400,12 +400,10 @@ int cli_cvdload(FILE *fs, struct cl_engine **engine, unsigned int *signo, short
if(mkdir(dir, 0700)) {
cli_errmsg("cli_cvdload(): Can't create temporary directory %s\n", dir);
free(dir);
close(fd);
return CL_ETMPDIR;
}
if(cli_untgz(fd, dir)) {
close(fd);
if(cli_untgz(fileno(fs), dir)) {
cli_errmsg("cli_cvdload(): Can't unpack CVD file.\n");
free(dir);
return CL_ECVDEXTR;

@ -100,7 +100,6 @@ cli_scanjs(const char *dir, int desc)
int created_output, done_header, rc;
FILE *fout;
char script_filename[NAME_MAX + 1];
extern short cli_leavetemps_flag;
cli_dbgmsg("in cli_scanjs(%s)\n", dir);

@ -48,8 +48,6 @@
static cli_file_t targettab[CL_TARGET_TABLE_SIZE] = { 0, CL_TYPE_MSEXE, CL_TYPE_MSOLE2, CL_TYPE_HTML, CL_TYPE_MAIL, CL_TYPE_GRAPHICS, CL_TYPE_ELF };
extern short cli_debug_flag;
int cli_scanbuff(const unsigned char *buffer, uint32_t length, const char **virname, const struct cl_engine *engine, cli_file_t ftype)
{
int ret = CL_CLEAN;

@ -53,6 +53,21 @@ struct cli_matcher {
uint32_t ac_partsigs, ac_nodes, ac_patterns;
};
struct cli_md5_node {
char *virname;
unsigned char *md5;
unsigned int size;
unsigned short fp;
struct cli_md5_node *next;
};
struct cli_meta_node {
int csize, size, method;
unsigned int crc32, fileno, encrypted, maxdepth;
char *filename, *virname;
struct cli_meta_node *next;
};
#define CL_TARGET_TABLE_SIZE 7
struct cli_target_info {

@ -3772,7 +3772,6 @@ rfc1341(message *m, const char *dir)
char buffer[BUFSIZ], fullname[NAME_MAX + 1];
int nblanks;
struct stat statb;
extern short cli_leavetemps_flag;
#ifndef C_CYGWIN
if(dent->d_ino == 0)

@ -38,8 +38,6 @@
#include "matcher.h"
#include "scanners.h"
extern short cli_leavetemps_flag;
#ifndef O_BINARY
#define O_BINARY 0
#endif

@ -58,7 +58,7 @@
#ifdef CL_THREAD_SAFE
# include <pthread.h>
static pthread_mutex_t cli_gentempname_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t cli_gentemp_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
#if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
@ -82,55 +82,38 @@ static pthread_mutex_t cli_gentempname_mutex = PTHREAD_MUTEX_INITIALIZER;
#define CL_FLEVEL 21 /* don't touch it */
short cli_debug_flag = 0, cli_leavetemps_flag = 0;
uint8_t cli_debug_flag = 0, cli_leavetemps_flag = 0;
static unsigned char name_salt[16] = { 16, 38, 97, 12, 8, 4, 72, 196, 217, 144, 33, 124, 18, 11, 17, 253 };
#define MSGCODE(x) \
va_list args; \
int len = sizeof(x) - 1; \
char buff[BUFSIZ]; \
strncpy(buff, x, len); \
va_start(args, str); \
vsnprintf(buff + len, sizeof(buff) - len, str, args); \
buff[sizeof(buff) - 1] = '\0'; \
fputs(buff, stderr); \
va_end(args)
void cli_warnmsg(const char *str, ...)
{
va_list args;
int sz = sizeof("LibClamAV Warning: ") - 1;
char buff[256];
strncpy(buff, "LibClamAV Warning: ", sz);
va_start(args, str);
vsnprintf(buff + sz, sizeof(buff) - sz, str, args);
buff[sizeof(buff) - 1] = '\0';
fputs(buff, stderr);
va_end(args);
MSGCODE("LibClamAV Warning: ");
}
void cli_errmsg(const char *str, ...)
{
va_list args;
int sz = sizeof("LibClamAV Error: ") - 1;
char buff[256];
strncpy(buff, "LibClamAV Error: ", sz);
va_start(args, str);
vsnprintf(buff + sz, sizeof(buff) - sz, str, args);
buff[sizeof(buff) - 1] = '\0';
fputs(buff, stderr);
va_end(args);
MSGCODE("LibClamAV Error: ");
}
void cli_dbgmsg(const char *str, ...)
{
if(cli_debug_flag) {
va_list args;
int sz = sizeof("LibClamAV debug: ") - 1;
char buff[BUFSIZ];
memcpy(buff, "LibClamAV debug: ", sz);
va_start(args, str);
vsnprintf(buff + sz, sizeof(buff) - sz, str, args);
buff[sizeof(buff) - 1] = '\0';
fputs(buff, stderr);
va_end(args);
} else
return;
MSGCODE("LibClamAV debug: ");
}
}
void cl_debug(void)
@ -333,7 +316,6 @@ void *cli_malloc(size_t size)
if(!alloc) {
cli_errmsg("cli_malloc(): Can't allocate memory (%u bytes).\n", size);
perror("malloc_problem");
/* _exit(1); */
return NULL;
} else return alloc;
}
@ -357,7 +339,6 @@ void *cli_calloc(size_t nmemb, size_t size)
if(!alloc) {
cli_errmsg("cli_calloc(): Can't allocate memory (%u bytes).\n", nmemb * size);
perror("calloc_problem");
/* _exit(1); */
return NULL;
} else return alloc;
}
@ -455,7 +436,7 @@ void cl_settempdir(const char *dir, short leavetemps)
cli_leavetemps_flag = leavetemps;
}
static char *cli_gentempname(const char *dir)
char *cli_gentemp(const char *dir)
{
char *name, *tmp;
const char *mdir;
@ -474,12 +455,12 @@ static char *cli_gentempname(const char *dir)
name = (char *) cli_calloc(strlen(mdir) + 1 + 32 + 1 + 7, sizeof(char));
if(!name) {
cli_dbgmsg("cli_gentempname('%s'): out of memory\n", mdir);
cli_dbgmsg("cli_gentemp('%s'): out of memory\n", mdir);
return NULL;
}
#ifdef CL_THREAD_SAFE
pthread_mutex_lock(&cli_gentempname_mutex);
pthread_mutex_lock(&cli_gentemp_mutex);
#endif
memcpy(salt, name_salt, 16);
@ -490,12 +471,12 @@ static char *cli_gentempname(const char *dir)
tmp = cli_md5buff(salt, 48, name_salt);
#ifdef CL_THREAD_SAFE
pthread_mutex_unlock(&cli_gentempname_mutex);
pthread_mutex_unlock(&cli_gentemp_mutex);
#endif
if(!tmp) {
free(name);
cli_dbgmsg("cli_gentempname('%s'): out of memory\n", mdir);
cli_dbgmsg("cli_gentemp('%s'): out of memory\n", mdir);
return NULL;
}
@ -510,65 +491,21 @@ static char *cli_gentempname(const char *dir)
return(name);
}
char *cli_gentemp(const char *dir)
{
char *name;
name = cli_gentempname(dir);
return(name);
}
char *cli_gentempdir(const char *dir)
{
char *name;
name = cli_gentempname(dir);
if(name && mkdir(name, 0700)) {
cli_dbgmsg("cli_gentempdir(): can't create temp directory: %s\n", name);
free(name);
name = NULL;
}
return(name);
}
char *cli_gentempdesc(const char *dir, int *fd)
int cli_gentempfd(const char *dir, char **name, int *fd)
{
char *name;
name = cli_gentempname(dir);
*name = cli_gentemp(dir);
if(!*name)
return CL_EMEM;
if(name && ((*fd = open(name, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, S_IRWXU)) < 0)) {
cli_dbgmsg("cli_gentempdesc(): can't create temp file: %s\n", name);
free(name);
name = NULL;
*fd = open(*name, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, S_IRWXU);
if(*fd == -1) {
cli_errmsg("cli_gentempfd: Can't create temporary file %s: %s\n", *name, strerror(errno));
free(*name);
return CL_EIO;
}
return(name);
}
char *cli_gentempstream(const char *dir, FILE **fs)
{
char *name;
mode_t omask;
name = cli_gentempname(dir);
if(!name)
return NULL;
omask = umask(077);
if((*fs = fopen(name, "wb+")) == NULL) {
cli_dbgmsg("cli_gentempstream(): can't create temp file: %s\n", name);
free(name);
name = NULL;
}
umask(omask);
return name;
return CL_SUCCESS;
}
#ifdef C_WINDOWS
@ -591,13 +528,13 @@ cli_rmdirs(const char *name)
if(stat(name, &statb) < 0) {
cli_warnmsg("Can't locate %s: %s\n", name, strerror(errno));
cli_warnmsg("cli_rmdirs: Can't locate %s: %s\n", name, strerror(errno));
return -1;
}
if(!S_ISDIR(statb.st_mode)) {
if(unlink(name) < 0) {
cli_warnmsg("Can't remove %s: %s\n", name, strerror(errno));
cli_warnmsg("cli_rmdirs: Can't remove %s: %s\n", name, strerror(errno));
return -1;
}
return 0;
@ -615,23 +552,23 @@ cli_rmdirs(const char *name)
#else
while((dent = readdir(dd)) != NULL) {
#endif
char *fname;
char *path;
if(strcmp(dent->d_name, ".") == 0)
continue;
if(strcmp(dent->d_name, "..") == 0)
continue;
fname = cli_malloc(strlen(name) + strlen(dent->d_name) + 2);
path = cli_malloc(strlen(name) + strlen(dent->d_name) + 2);
if(fname == NULL) {
if(path == NULL) {
closedir(dd);
return -1;
}
sprintf(fname, "%s\\%s", name, dent->d_name);
rc = cli_rmdirs(fname);
free(fname);
sprintf(path, "%s\\%s", name, dent->d_name);
rc = cli_rmdirs(path);
free(path);
if(rc != 0)
break;
}
@ -639,7 +576,7 @@ cli_rmdirs(const char *name)
closedir(dd);
if(rmdir(name) < 0) {
cli_errmsg("Can't remove temporary directory %s: %s\n", name, strerror(errno));
cli_errmsg("cli_rmdirs: Can't remove temporary directory %s: %s\n", name, strerror(errno));
return -1;
}
@ -657,8 +594,7 @@ int cli_rmdirs(const char *dirname)
} result;
#endif
struct stat maind, statbuf;
char *fname;
int ret;
char *path;
chmod(dirname, 0700);
@ -666,7 +602,7 @@ int cli_rmdirs(const char *dirname)
while(stat(dirname, &maind) != -1) {
if(!rmdir(dirname)) break;
if(errno != ENOTEMPTY && errno != EEXIST && errno != EBADF) {
cli_errmsg("Can't remove temporary directory %s: %s\n", dirname, strerror(errno));
cli_errmsg("cli_rmdirs: Can't remove temporary directory %s: %s\n", dirname, strerror(errno));
closedir(dd);
return -1;
}
@ -683,50 +619,47 @@ int cli_rmdirs(const char *dirname)
#endif
{
if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
fname = cli_malloc(strlen(dirname) + strlen(dent->d_name) + 2);
if(!fname) {
path = cli_malloc(strlen(dirname) + strlen(dent->d_name) + 2);
if(!path) {
closedir(dd);
return -1;
}
#ifdef C_WINDOWS
sprintf(fname, "%s\\%s", dirname, dent->d_name);
sprintf(path, "%s\\%s", dirname, dent->d_name);
#else
sprintf(fname, "%s/%s", dirname, dent->d_name);
sprintf(path, "%s/%s", dirname, dent->d_name);
#endif
/* stat the file */
if(lstat(fname, &statbuf) != -1) {
if(lstat(path, &statbuf) != -1) {
if(S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) {
if(rmdir(fname) == -1) { /* can't be deleted */
if(rmdir(path) == -1) { /* can't be deleted */
if(errno == EACCES) {
cli_errmsg("Can't remove some temporary directories due to access problem.\n");
cli_errmsg("cli_rmdirs: Can't remove some temporary directories due to access problem.\n");
closedir(dd);
free(fname);
free(path);
return -1;
}
ret = cli_rmdirs(fname);
if(ret) {
cli_warnmsg("Can't remove directory %s\n", fname);
free(fname);
if(cli_rmdirs(path)) {
cli_warnmsg("cli_rmdirs: Can't remove nested directory %s\n", path);
free(path);
closedir(dd);
return -1;
}
}
} else
if(unlink(fname) < 0) {
cli_warnmsg("Couldn't remove %s: %s\n", fname, strerror(errno));
free(fname);
if(unlink(path) < 0) {
cli_warnmsg("cli_rmdirs: Couldn't remove %s: %s\n", path, strerror(errno));
free(path);
closedir(dd);
return -1;
}
}
free(fname);
free(path);
}
}
}
rewinddir(dd);
}

@ -30,6 +30,8 @@
#include "clamav.h"
#include "dconf.h"
extern uint8_t cli_debug_flag, cli_leavetemps_flag;
/*
* CLI_ISCONTAINED(buf1, size1, buf2, size2) checks if buf2 is contained
* within buf1.
@ -191,9 +193,7 @@ char *cli_md5file(const char *filename);
int cli_readn(int fd, void *buff, unsigned int count);
int cli_writen(int fd, const void *buff, unsigned int count);
char *cli_gentemp(const char *dir);
char *cli_gentempdir(const char *dir);
char *cli_gentempdesc(const char *dir, int *fd);
char *cli_gentempstream(const char *dir, FILE **fs);
int cli_gentempfd(const char *dir, char **name, int *fd);
unsigned int cli_rndnum(unsigned int max);
int cli_filecopy(const char *src, const char *dest);
bitset_t *cli_bitset_init(void);

@ -80,7 +80,6 @@
#define PEALIGN(o,a) (((a))?(((o)/(a))*(a)):(o))
#define PESALIGN(o,a) (((a))?(((o)/(a)+((o)%(a)!=0))*(a)):(o))
extern short cli_leavetemps_flag;
struct offset_list {
uint32_t offset;

@ -738,7 +738,7 @@ static int cli_loadhdb(FILE *fd, struct cl_engine **engine, unsigned int *signo,
} else {
if(!(*engine)->md5_hlist) {
cli_dbgmsg("cli_loadhdb: Initializing MD5 list structure\n");
(*engine)->md5_hlist = (struct cli_md5_node **) cli_calloc(256, sizeof(struct cli_md5_node *));
(*engine)->md5_hlist = cli_calloc(256, sizeof(struct cli_md5_node *));
if(!(*engine)->md5_hlist) {
free(new->virname);
free(new->md5);

@ -124,8 +124,6 @@ static const short int hextable[256] = {
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
};
extern int short cli_leavetemps_flag;
static void init_rtf_state(struct rtf_state* state)
{
*state = base_state;
@ -267,6 +265,7 @@ static int rtf_object_process(struct rtf_state* state, const unsigned char* inpu
const unsigned char* out_data;
size_t out_cnt = 0;
size_t i;
int ret;
if(!data || !len)
return 0;
@ -394,9 +393,8 @@ static int rtf_object_process(struct rtf_state* state, const unsigned char* inpu
out_data += i;
data->bread=0;
cli_dbgmsg("Dumping rtf embedded object of size:%ld\n",data->desc_len);
data->name = cli_gentempdesc(data->tmpdir, &data->fd);
if(!data->name || data->fd < 0)
return CL_ETMPFILE;
if((ret = cli_gentempfd(data->tmpdir, &data->name, &data->fd)))
return ret;
data->internal_state = DUMP_DATA;
cli_dbgmsg("RTF: next state: DUMP_DATA\n");
}

@ -49,7 +49,6 @@
#define O_BINARY 0
#endif
extern short cli_leavetemps_flag;
#define DCONF_ARCH ctx->dconf->archive
#define DCONF_DOC ctx->dconf->doc
@ -103,6 +102,7 @@ extern short cli_leavetemps_flag;
#define MAX_MAIL_RECURSION 15
int cli_scannulsft(int desc, cli_ctx *ctx, off_t offset); /* FIXME */
static int cli_scanfile(const char *filename, cli_ctx *ctx);
@ -428,9 +428,8 @@ static int cli_scanzip(int desc, cli_ctx *ctx, off_t sfx_offset, uint32_t *sfx_c
zip_dir *zdir;
zip_dirent zdirent;
zip_file *zfp;
FILE *tmp = NULL;
char *tmpname = NULL, *buff;
int fd, bytes, ret = CL_CLEAN;
int fd = -1, bytes, ret = CL_CLEAN;
unsigned long int size = 0;
unsigned int files = 0, encrypted, bfcnt;
struct stat source;
@ -610,17 +609,13 @@ static int cli_scanzip(int desc, cli_ctx *ctx, off_t sfx_offset, uint32_t *sfx_c
while(1) {
fail = 0;
/* generate temporary file and get its descriptor */
if((tmpname = cli_gentempstream(NULL, &tmp)) == NULL) {
cli_dbgmsg("Zip: Can't generate tmpfile().\n");
ret = CL_ETMPFILE;
if((ret = cli_gentempfd(NULL, &tmpname, &fd)))
break;
}
size = 0;
while((bytes = zip_file_read(zfp, buff, FILEBUFF)) > 0) {
size += bytes;
if(fwrite(buff, 1, bytes, tmp) != (size_t) bytes) {
if(cli_writen(fd, buff, bytes) != bytes) {
cli_dbgmsg("Zip: Can't write to file.\n");
ret = CL_EIO;
break;
@ -643,14 +638,11 @@ static int cli_scanzip(int desc, cli_ctx *ctx, off_t sfx_offset, uint32_t *sfx_c
}
if(!fail) {
if(fflush(tmp) != 0) {
cli_dbgmsg("Zip: fflush() failed\n");
if(fsync(fd) == -1) {
cli_dbgmsg("Zip: fsync() failed\n");
ret = CL_EFSYNC;
break;
}
fd = fileno(tmp);
lseek(fd, 0, SEEK_SET);
if((ret = cli_magic_scandesc(fd, ctx)) == CL_VIRUS ) {
@ -658,16 +650,13 @@ static int cli_scanzip(int desc, cli_ctx *ctx, off_t sfx_offset, uint32_t *sfx_c
ret = CL_VIRUS;
break;
}
}
if(tmp) {
fclose(tmp);
if(!cli_leavetemps_flag)
unlink(tmpname);
free(tmpname);
tmp = NULL;
}
close(fd);
if(!cli_leavetemps_flag)
unlink(tmpname);
free(tmpname);
fd = -1;
if(zfp->bf[bfcnt] == -1)
break;
@ -689,12 +678,11 @@ static int cli_scanzip(int desc, cli_ctx *ctx, off_t sfx_offset, uint32_t *sfx_c
}
zip_dir_close(zdir);
if(tmp) {
fclose(tmp);
if(fd != -1) {
close(fd);
if(!cli_leavetemps_flag)
unlink(tmpname);
free(tmpname);
tmp = NULL;
}
free(buff);
@ -706,7 +694,6 @@ static int cli_scangzip(int desc, cli_ctx *ctx)
int fd, bytes, ret = CL_CLEAN;
unsigned long int size = 0;
char *buff;
FILE *tmp = NULL;
char *tmpname;
gzFile gd;
@ -718,17 +705,16 @@ static int cli_scangzip(int desc, cli_ctx *ctx)
return CL_EGZIP;
}
if((tmpname = cli_gentempstream(NULL, &tmp)) == NULL) {
if((ret = cli_gentempfd(NULL, &tmpname, &fd))) {
cli_dbgmsg("GZip: Can't generate temporary file.\n");
gzclose(gd);
return CL_ETMPFILE;
return ret;
}
fd = fileno(tmp);
if(!(buff = (char *) cli_malloc(FILEBUFF))) {
cli_dbgmsg("GZip: Unable to malloc %u bytes.\n", FILEBUFF);
gzclose(gd);
fclose(tmp);
close(fd);
if(!cli_leavetemps_flag)
unlink(tmpname);
free(tmpname);
@ -750,7 +736,7 @@ static int cli_scangzip(int desc, cli_ctx *ctx)
if(cli_writen(fd, buff, bytes) != bytes) {
cli_dbgmsg("GZip: Can't write to file.\n");
fclose(tmp);
close(fd);
if(!cli_leavetemps_flag)
unlink(tmpname);
free(tmpname);
@ -764,7 +750,7 @@ static int cli_scangzip(int desc, cli_ctx *ctx)
gzclose(gd);
if(ret == CL_VIRUS) {
fclose(tmp);
close(fd);
if(!cli_leavetemps_flag)
unlink(tmpname);
free(tmpname);
@ -773,7 +759,7 @@ static int cli_scangzip(int desc, cli_ctx *ctx)
if(fsync(fd) == -1) {
cli_dbgmsg("GZip: Can't synchronise descriptor %d\n", fd);
fclose(tmp);
close(fd);
if(!cli_leavetemps_flag)
unlink(tmpname);
free(tmpname);
@ -783,13 +769,13 @@ static int cli_scangzip(int desc, cli_ctx *ctx)
lseek(fd, 0, SEEK_SET);
if((ret = cli_magic_scandesc(fd, ctx)) == CL_VIRUS ) {
cli_dbgmsg("GZip: Infected with %s\n", *ctx->virname);
fclose(tmp);
close(fd);
if(!cli_leavetemps_flag)
unlink(tmpname);
free(tmpname);
return CL_VIRUS;
}
fclose(tmp);
close(fd);
if(!cli_leavetemps_flag)
unlink(tmpname);
free(tmpname);
@ -812,7 +798,7 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
short memlim = 0;
unsigned long int size = 0;
char *buff;
FILE *fs, *tmp = NULL;
FILE *fs;
char *tmpname;
BZFILE *bfd;
@ -832,17 +818,16 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
return CL_EBZIP;
}
if((tmpname = cli_gentempstream(NULL, &tmp)) == NULL) {
if((ret = cli_gentempfd(NULL, &tmpname, &fd))) {
cli_dbgmsg("Bzip: Can't generate temporary file.\n");
BZ2_bzReadClose(&bzerror, bfd);
fclose(fs);
return CL_ETMPFILE;
return ret;
}
fd = fileno(tmp);
if(!(buff = (char *) cli_malloc(FILEBUFF))) {
cli_dbgmsg("Bzip: Unable to malloc %u bytes.\n", FILEBUFF);
fclose(tmp);
close(fd);
if(!cli_leavetemps_flag)
unlink(tmpname);
free(tmpname);
@ -867,7 +852,7 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
if(cli_writen(fd, buff, bytes) != bytes) {
cli_dbgmsg("Bzip: Can't write to file.\n");
BZ2_bzReadClose(&bzerror, bfd);
fclose(tmp);
close(fd);
if(!cli_leavetemps_flag)
unlink(tmpname);
free(tmpname);
@ -881,7 +866,7 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
BZ2_bzReadClose(&bzerror, bfd);
if(ret == CL_VIRUS) {
fclose(tmp);
close(fd);
if(!cli_leavetemps_flag)
unlink(tmpname);
free(tmpname);
@ -891,7 +876,7 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
if(fsync(fd) == -1) {
cli_dbgmsg("Bzip: Synchronisation failed for descriptor %d\n", fd);
fclose(tmp);
close(fd);
if(!cli_leavetemps_flag)
unlink(tmpname);
free(tmpname);
@ -903,7 +888,7 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
if((ret = cli_magic_scandesc(fd, ctx)) == CL_VIRUS ) {
cli_dbgmsg("Bzip: Infected with %s\n", *ctx->virname);
}
fclose(tmp);
close(fd);
if(!cli_leavetemps_flag)
unlink(tmpname);
free(tmpname);
@ -913,6 +898,7 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
}
#endif
/*
static int cli_scanszdd(int desc, cli_ctx *ctx)
{
int fd, ret = CL_CLEAN, dcpy;
@ -976,6 +962,7 @@ static int cli_scanszdd(int desc, cli_ctx *ctx)
free(tmpname);
return ret;
}
*/
static int cli_scanmscab(int desc, cli_ctx *ctx, off_t sfx_offset)
{
@ -2088,12 +2075,12 @@ int cli_magic_scandesc(int desc, cli_ctx *ctx)
if(SCAN_ARCHIVE)
ret = cli_scannulsft(desc, ctx, 0);
break;
/*
case CL_TYPE_MSSZDD:
if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_SZDD))
ret = cli_scanszdd(desc, ctx);
break;
*/
case CL_TYPE_MSCAB:
if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_CAB))
ret = cli_scanmscab(desc, ctx, 0);

@ -51,8 +51,6 @@
#define EC32(x) le32_to_host(x) /* Convert little endian to host */
#define EC16(x) le16_to_host(x) /* Convert little endian to host */
extern short cli_leavetemps_flag;
static const char *langcodes[] = {
"", "EN", "FR", "GE", "SP", "IT", "SW", "DA", "NO", "FI", "AM",
"SF", "SG", "PO", "TU", "IC", "RU", "HU", "DU", "BL", "AU", "BG",
@ -541,19 +539,19 @@ int cli_scansis(int desc, cli_ctx *ctx)
cli_dbgmsg("SIS: Number of files: %d\n", nfiles);
cli_dbgmsg("SIS: Offset of files records: %d\n", EC32(file_hdr.pfiles));
if(!(dir = cli_gentempdir(NULL))) {
cli_errmsg("SIS: Can't generate temporary directory\n");
munmap(mfile, length);
return CL_ETMPDIR;
}
if((frecord = EC32(file_hdr.pfiles)) >= length) {
cli_errmsg("SIS: Broken file structure (frecord)\n");
munmap(mfile, length);
free(dir);
return CL_EFORMAT;
}
dir = cli_gentemp(NULL);
if(!dir || mkdir(dir, 0700) == -1) {
cli_errmsg("SIS: Can't create temporary directory %s\n", dir ? dir : "");
munmap(mfile, length);
return CL_ETMPDIR;
}
for(i = 0; i < nfiles; i++) {
cli_dbgmsg("SIS: -----\n");

@ -60,7 +60,6 @@ static int tnef_header(FILE *fp, uint8_t *part, uint16_t *type, uint16_t *tag, i
#define host16(v) le16_to_host(v)
#define host32(v) le32_to_host(v)
extern short cli_debug_flag;
int
cli_tnef(const char *dir, int desc)

Loading…
Cancel
Save