e3aaff8e |
/* |
da99018d |
* Copyright (C) 1999 - 2004 Tomasz Kojm <tk@clamav.net> |
e3aaff8e |
*
* 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
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
|
6d6e8271 |
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
|
e3aaff8e |
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <dirent.h>
#include <time.h>
#include <fcntl.h>
#include <pwd.h>
#include <errno.h>
#include <target.h> |
feec9e31 |
#include <sys/time.h> |
e3aaff8e |
|
9b8e00a0 |
#ifdef CL_THREAD_SAFE
# include <pthread.h> |
8000d078 |
pthread_mutex_t cli_gentemp_mutex = PTHREAD_MUTEX_INITIALIZER; |
9b8e00a0 |
#endif
|
e3aaff8e |
#include "clamav.h"
#include "others.h"
#include "md5.h" |
5ae8bdd5 |
#include "cltypes.h" |
e3aaff8e |
|
09835b83 |
#define CL_FLEVEL 2 /* don't touch it */ |
a3ee0766 |
|
8d5e0ac0 |
#define MAX_ALLOCATION 134217728 |
feec9e31 |
|
590135f9 |
short cli_debug_flag = 0, cli_leavetemps_flag = 0; |
d4d14218 |
|
feec9e31 |
static unsigned char oldmd5buff[16] = { 16, 38, 97, 12, 8, 4, 72, 196, 217, 144, 33, 124, 18, 11, 17, 253 };
|
e3aaff8e |
void cli_warnmsg(const char *str, ...)
{
va_list args;
va_start(args, str);
fprintf(stderr, "LibClamAV Warning: ");
vfprintf(stderr, str, args);
va_end(args);
}
void cli_errmsg(const char *str, ...)
{
va_list args;
va_start(args, str);
fprintf(stderr, "LibClamAV Error: ");
vfprintf(stderr, str, args);
va_end(args);
}
void cli_dbgmsg(const char *str, ...)
{
va_list args;
|
d4d14218 |
if(cli_debug_flag) {
va_start(args, str);
fprintf(stderr, "LibClamAV debug: ");
vfprintf(stderr, str, args);
va_end(args);
} else
return;
}
void cl_debug(void)
{
cli_debug_flag = 1; |
e3aaff8e |
}
|
a3ee0766 |
int cl_retflevel(void)
{
return CL_FLEVEL;
}
|
8d78fc78 |
const char *cl_retver(void)
{
return VERSION;
}
|
68a6f51f |
const char *cl_strerror(int clerror) |
e3aaff8e |
{
switch(clerror) {
case CL_CLEAN: |
ac75a532 |
return "No viruses detected"; |
e3aaff8e |
case CL_VIRUS: |
ac75a532 |
return "Virus(es) detected"; |
e3aaff8e |
case CL_EMAXREC: |
ac75a532 |
return "Recursion limit exceeded"; |
e3aaff8e |
case CL_EMAXSIZE: |
ac75a532 |
return "File size limit exceeded"; |
e3aaff8e |
case CL_EMAXFILES: |
ac75a532 |
return "Files number limit exceeded"; |
e3aaff8e |
case CL_ERAR: |
ac75a532 |
return "RAR module failure"; |
e3aaff8e |
case CL_EZIP: |
ac75a532 |
return "Zip module failure"; |
e3aaff8e |
case CL_EMALFZIP: |
ac75a532 |
return "Malformed Zip detected"; |
e3aaff8e |
case CL_EGZIP: |
ac75a532 |
return "GZip module failure"; |
341e5433 |
case CL_EMSCOMP: |
ac75a532 |
return "MS Expand module failure"; |
414abe87 |
case CL_EMSCAB: |
ac75a532 |
return "MS CAB module failure"; |
47bbbc56 |
case CL_EOLE2: |
ac75a532 |
return "OLE2 module failure"; |
e3aaff8e |
case CL_ETMPFILE: |
ac75a532 |
return "Unable to create temporary file"; |
e3aaff8e |
case CL_ETMPDIR: |
ac75a532 |
return "Unable to create temporary directory"; |
e3aaff8e |
case CL_EFSYNC: |
ac75a532 |
return "Unable to synchronize file <-> disk"; |
e3aaff8e |
case CL_EMEM: |
ac75a532 |
return "Unable to allocate memory"; |
e3aaff8e |
case CL_EOPEN: |
ac75a532 |
return "Unable to open file or directory"; |
e3aaff8e |
case CL_EMALFDB: |
ac75a532 |
return "Malformed database"; |
e3aaff8e |
case CL_EPATSHORT: |
ac75a532 |
return "Too short pattern detected"; |
d9e258d5 |
case CL_ECVD: |
ac75a532 |
return "Broken or not a CVD file"; |
8139fd99 |
case CL_ECVDEXTR: |
ac75a532 |
return "CVD extraction failure"; |
d9e258d5 |
case CL_EMD5: |
ac75a532 |
return "MD5 verification error"; |
d9e258d5 |
case CL_EDSIG: |
ac75a532 |
return "Digital signature verification error"; |
e3aaff8e |
case CL_ENULLARG: |
ac75a532 |
return "Null argument passed while initialized is required";
case CL_EIO:
return "Input/Output error"; |
4a7c1c22 |
case CL_EFORMAT:
return "Bad format or broken data"; |
e3aaff8e |
default: |
ac75a532 |
return "Unknown error code"; |
e3aaff8e |
}
}
|
68a6f51f |
const char *cl_perror(int clerror) |
049a18b9 |
{
return cl_strerror(clerror);
}
|
335d1663 |
char *cli_md5stream(FILE *fs, unsigned char *digcpy) |
e3aaff8e |
{ |
335d1663 |
unsigned char digest[16];
char buff[FILEBUFF];
struct MD5Context ctx;
char *md5str, *pt;
int i, bytes;
MD5Init(&ctx); |
e3aaff8e |
|
335d1663 |
while((bytes = fread(buff, 1, FILEBUFF, fs)))
MD5Update(&ctx, buff, bytes);
MD5Final(digest, &ctx);
if(!(md5str = (char *) cli_calloc(32 + 1, sizeof(char))))
return NULL; |
e3aaff8e |
|
335d1663 |
pt = md5str;
for(i = 0; i < 16; i++) {
sprintf(pt, "%02x", digest[i]);
pt += 2;
} |
e3aaff8e |
|
335d1663 |
if(digcpy)
memcpy(digcpy, digest, 16); |
e3aaff8e |
|
335d1663 |
return md5str; |
e3aaff8e |
}
|
8000d078 |
char *cli_md5file(const char *filename) |
d9e258d5 |
{ |
335d1663 |
FILE *fs; |
d9e258d5 |
char *md5str;
|
8000d078 |
|
335d1663 |
if((fs = fopen(filename, "rb")) == NULL) { |
8000d078 |
cli_errmsg("cli_md5file(): Can't read file %s\n", filename);
return NULL;
}
|
335d1663 |
md5str = cli_md5stream(fs, NULL);
fclose(fs); |
d9e258d5 |
|
335d1663 |
return md5str; |
d9e258d5 |
}
|
8000d078 |
static char *cli_md5buff(const char *buffer, unsigned int len) |
e3aaff8e |
{ |
335d1663 |
unsigned char digest[16];
char *md5str, *pt;
struct MD5Context ctx;
int i; |
e3aaff8e |
|
335d1663 |
MD5Init(&ctx);
MD5Update(&ctx, buffer, len);
MD5Final(digest, &ctx);
memcpy(oldmd5buff, digest, 16); |
e3aaff8e |
|
335d1663 |
if(!(md5str = (char *) cli_calloc(32 + 1, sizeof(char))))
return NULL; |
e3aaff8e |
|
335d1663 |
pt = md5str;
for(i = 0; i < 16; i++) {
sprintf(pt, "%02x", digest[i]);
pt += 2;
} |
e3aaff8e |
|
335d1663 |
return md5str; |
e3aaff8e |
}
void *cli_malloc(size_t size)
{
void *alloc;
|
8d5e0ac0 |
|
5edae84a |
if(size > MAX_ALLOCATION || size < 0) {
cli_errmsg("Attempt to allocate %d bytes. Please report to bugs@clamav.net\n", size); |
8d5e0ac0 |
return NULL;
}
|
e3aaff8e |
alloc = malloc(size);
if(!alloc) {
cli_errmsg("cli_malloc(): Can't allocate memory (%d bytes).\n", size);
perror("malloc_problem"); |
b5b62ca7 |
/* _exit(1); */ |
e3aaff8e |
return NULL;
} else return alloc;
}
void *cli_calloc(size_t nmemb, size_t size)
{
void *alloc;
|
5edae84a |
if(size > MAX_ALLOCATION || size < 0) {
cli_errmsg("Attempt to allocate %d bytes. Please report to bugs@clamav.net\n", size); |
8d5e0ac0 |
return NULL;
}
|
e3aaff8e |
alloc = calloc(nmemb, size);
if(!alloc) {
cli_errmsg("cli_calloc(): Can't allocate memory (%d bytes).\n", nmemb * size);
perror("calloc_problem"); |
b5b62ca7 |
/* _exit(1); */ |
e3aaff8e |
return NULL;
} else return alloc;
}
|
8139fd99 |
void *cli_realloc(void *ptr, size_t size)
{
void *alloc;
|
8d5e0ac0 |
|
8139fd99 |
alloc = realloc(ptr, size);
if(!alloc) {
cli_errmsg("cli_realloc(): Can't re-allocate memory to %d byte.\n", size);
perror("realloc_problem");
return NULL;
} else return alloc;
}
|
8000d078 |
unsigned int cli_rndnum(unsigned int max) |
e3aaff8e |
{
struct timeval tv;
gettimeofday(&tv, (struct timezone *) 0);
srand(tv.tv_usec+clock());
return rand() % max;
}
|
590135f9 |
void cl_settempdir(const char *dir, short leavetemps)
{
char *var;
if(dir) {
var = (char *) cli_malloc(8 + strlen(dir));
sprintf(var, "TMPDIR=%s", dir);
if(!putenv(var))
cli_dbgmsg("Setting %s as global temporary directory\n", dir);
else
cli_warnmsg("Can't set TMPDIR variable - insufficient space in the environment.\n");
/* WARNING: var must not be released - see putenv(3) */
}
cli_leavetemps_flag = leavetemps;
}
|
8000d078 |
char *cli_gentemp(const char *dir) |
e3aaff8e |
{ |
68a6f51f |
char *name, *tmp;
const char *mdir; |
feec9e31 |
unsigned char salt[16 + 32];
int i; |
e3aaff8e |
struct stat foo;
|
feec9e31 |
|
e3aaff8e |
if(!dir)
mdir = "/tmp";
else
mdir = (char *) dir;
|
2b278a02 |
name = (char*) cli_calloc(strlen(mdir) + 1 + 16 + 1 + 7, sizeof(char)); |
2d70a403 |
if(name == NULL) { |
8000d078 |
cli_dbgmsg("cli_gentemp('%s'): out of memory\n", dir); |
2d70a403 |
return NULL;
} |
feec9e31 |
#ifdef CL_THREAD_SAFE |
8000d078 |
pthread_mutex_lock(&cli_gentemp_mutex); |
feec9e31 |
#endif
memcpy(salt, oldmd5buff, 16); |
e3aaff8e |
do { |
feec9e31 |
for(i = 16; i < 48; i++) |
8000d078 |
salt[i] = cli_rndnum(255); |
e3aaff8e |
|
8000d078 |
tmp = cli_md5buff(( char* ) salt, 48); |
2b278a02 |
sprintf(name, "%s/clamav-", mdir); |
658f19f8 |
strncat(name, tmp, 16); |
e3aaff8e |
free(tmp);
} while(stat(name, &foo) != -1);
|
feec9e31 |
#ifdef CL_THREAD_SAFE |
8000d078 |
pthread_mutex_unlock(&cli_gentemp_mutex); |
feec9e31 |
#endif
|
e3aaff8e |
return(name);
}
int cli_rmdirs(const char *dirname)
{
DIR *dd;
struct dirent *dent; |
72a1b240 |
#if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
struct dirent result;
#endif |
e3aaff8e |
struct stat maind, statbuf;
char *fname;
|
a9ebff44 |
chmod(dirname, 0700); |
e3aaff8e |
if((dd = opendir(dirname)) != NULL) {
while(stat(dirname, &maind) != -1) {
if(!rmdir(dirname)) break;
|
72a1b240 |
#ifdef HAVE_READDIR_R_3
while(!readdir_r(dd, &result, &dent) && dent) {
#elif defined(HAVE_READDIR_R_2)
while((dent = (struct dirent *) readdir_r(dd, &result))) {
#else |
e3aaff8e |
while((dent = readdir(dd))) { |
72a1b240 |
#endif |
feeaa333 |
#ifndef C_INTERIX
if(dent->d_ino)
#endif
{ |
e3aaff8e |
if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
fname = cli_calloc(strlen(dirname) + strlen(dent->d_name) + 2, sizeof(char));
sprintf(fname, "%s/%s", dirname, dent->d_name);
/* stat the file */
if(lstat(fname, &statbuf) != -1) {
if(S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) {
if(rmdir(fname) == -1) { /* can't be deleted */
if(errno == EACCES) {
cli_errmsg("Can't remove some temporary directories due to access problem.\n");
closedir(dd); |
049a18b9 |
free(fname); |
e3aaff8e |
return 0;
}
cli_rmdirs(fname);
}
} else
unlink(fname);
}
free(fname);
}
}
}
rewinddir(dd);
}
} else {
return 53;
}
closedir(dd);
return 0;
} |
5b25b5e8 |
/* Function: readn
Try hard to read the requested number of bytes
*/
int cli_readn(int fd, void *buff, unsigned int count)
{
int retval;
unsigned int todo;
unsigned char *current; |
4152ad50 |
|
5b25b5e8 |
todo = count;
current = (unsigned char *) buff; |
4152ad50 |
|
5b25b5e8 |
do {
retval = read(fd, current, todo);
if (retval == 0) {
return (count - todo);
}
if (retval < 0) { |
7a5530b5 |
if (errno == EINTR) {
continue;
} |
5b25b5e8 |
return -1;
}
todo -= retval;
current += retval;
} while (todo > 0); |
4152ad50 |
|
5b25b5e8 |
return count;
} |
4152ad50 |
|
5b25b5e8 |
/* Function: writen
Try hard to write the specified number of bytes
*/
int cli_writen(int fd, void *buff, unsigned int count)
{
int retval;
unsigned int todo;
unsigned char *current; |
4152ad50 |
|
5b25b5e8 |
todo = count;
current = (unsigned char *) buff; |
4152ad50 |
|
5b25b5e8 |
do {
retval = write(fd, current, todo);
if (retval < 0) { |
7a5530b5 |
if (errno == EINTR) {
continue;
} |
5b25b5e8 |
return -1;
}
todo -= retval;
current += retval;
} while (todo > 0); |
4152ad50 |
|
5b25b5e8 |
return count;
}
|
5ae8bdd5 |
int32_t cli_readint32(const char *buff)
{ |
8000d078 |
int32_t ret; |
5ae8bdd5 |
#if WORDS_BIGENDIAN == 0
ret = *(int32_t *) buff;
#else |
8000d078 |
int32_t shift, i = 0;
|
5ae8bdd5 |
ret = 0;
for(shift = 0; shift < 32; shift += 8) {
ret |= (buff[i] & 0xff ) << shift;
i++;
}
#endif
return ret;
} |
96d71792 |
int cli_memstr(const char *haystack, int hs, const char *needle, int ns)
{
const char *pt, *hay;
int n;
if(hs < ns)
return 0;
if(haystack == needle)
return 1;
if(!memcmp(haystack, needle, ns))
return 1;
pt = hay = haystack;
n = hs;
while((pt = memchr(hay, needle[0], n)) != NULL) { |
91cad009 |
n -= (int) pt - (int) hay; |
96d71792 |
if(n < ns)
break;
if(!memcmp(pt, needle, ns))
return 1;
|
91cad009 |
if(hay == pt) {
n--;
hay++;
} else {
hay = pt;
} |
96d71792 |
}
return 0;
} |