libclamav/others.c
b151ef55
 /*
8890798d
  *  Copyright (C) 1999 - 2004 Tomasz Kojm <tk@clamav.net>
b151ef55
  *
  *  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.
  *
  */
 
8b242bb9
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
b151ef55
 #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>
c4f87775
 #include <sys/time.h>
b151ef55
 
0d01fcb2
 #ifdef CL_THREAD_SAFE
 #  include <pthread.h>
 pthread_mutex_t cl_gentemp_mutex = PTHREAD_MUTEX_INITIALIZER;
 #endif
 
b151ef55
 #include "clamav.h"
 #include "others.h"
 #include "md5.h"
 
ead674a2
 #define CL_FLEVEL 1 /* don't touch it */
 
c4f87775
 
0bcad2b1
 int cli_debug_flag = 0;
 
c4f87775
 static unsigned char oldmd5buff[16] = { 16, 38, 97, 12, 8, 4, 72, 196, 217, 144, 33, 124, 18, 11, 17, 253 };
 
b151ef55
 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;
 
0bcad2b1
     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;
b151ef55
 }
 
ead674a2
 int cl_retflevel(void)
 {
     return CL_FLEVEL;
 }
 
41b894c7
 const char *cl_strerror(int clerror)
b151ef55
 {
     switch(clerror) {
 	case CL_CLEAN:
 	    return "Virus NOT found.";
 	case CL_VIRUS:
 	    return "Virus(es) detected.";
 	case CL_EMAXREC:
 	    return "Recursion limit exceeded.";
 	case CL_EMAXSIZE:
 	    return "File size limit exceeded.";
 	case CL_EMAXFILES:
 	    return "Files number limit exceeded.";
 	case CL_ERAR:
 	    return "RAR module failure.";
 	case CL_EZIP:
 	    return "Zip module failure.";
 	case CL_EMALFZIP:
 	    return "Malformed Zip detected.";
 	case CL_EGZIP:
 	    return "GZip module failure.";
c561d2a3
 	case CL_EOLE2:
 	    return "OLE2 module failure.";
b151ef55
 	case CL_ETMPFILE:
 	    return "Unable to create temporary file.";
 	case CL_ETMPDIR:
 	    return "Unable to create temporary directory.";
 	case CL_EFSYNC:
 	    return "Unable to synchronize file <-> disk.";
 	case CL_EMEM:
 	    return "Unable to allocate memory.";
 	case CL_EOPEN:
 	    return "Unable to open file or directory.";
 	case CL_EMALFDB:
 	    return "Malformed database.";
 	case CL_EPATSHORT:
 	    return "Too short pattern detected.";
183ee7e7
 	case CL_ECVD:
 	    return "Broken or not a CVD file.";
4cd4319e
 	case CL_ECVDEXTR:
183ee7e7
 	    return "CVD extraction failure.";
 	case CL_EMD5:
 	    return "MD5 verification error.";
 	case CL_EDSIG:
 	    return "Digital signature verification error.";
b151ef55
 	case CL_ENULLARG:
4cd4319e
 	    return "Null argument passed while initialized is required.";
b151ef55
 	default:
 	    return "Unknown error code.";
     }
 }
 
41b894c7
 const char *cl_perror(int clerror)
c6259ac5
 {
     return cl_strerror(clerror);
 }
 
b151ef55
 char *cl_md5file(const char *filename)
 {
 	FILE *fd;
 	unsigned char buffer[16];
 	char *md5str;
 	int i, cnt=0;
 
     if((fd = fopen(filename, "rb")) == NULL) {
 	cli_errmsg("md5_file(): Can't read file %s\n", filename);
 	return NULL;
     }
 
     md5_stream(fd, &buffer);
     fclose(fd);
 
     md5str = (char*) calloc(32 + 1, sizeof(char));
 
     for(i=0; i<16; i++)
 	cnt += sprintf(md5str + cnt, "%02x", buffer[i]);
 
     return(md5str);
 }
 
183ee7e7
 char *cli_md5stream(FILE *fd)
 {
 	unsigned char buffer[16];
 	char *md5str;
 	int i, cnt=0;
 
     md5_stream(fd, &buffer);
 
     md5str = (char*) calloc(32 + 1, sizeof(char));
 
     for(i=0; i<16; i++)
 	cnt += sprintf(md5str + cnt, "%02x", buffer[i]);
 
     return(md5str);
 }
 
b151ef55
 char *cl_md5buff(const char *buffer, unsigned int len)
 {
c4f87775
 	unsigned char md5buff[16];
b151ef55
 	char *md5str;
 	struct md5_ctx ctx;
 	int i, cnt=0;
 
 
     md5_init_ctx(&ctx);
     md5_process_bytes(buffer, len, &ctx);
c4f87775
     md5_finish_ctx(&ctx, &md5buff);
     memcpy(oldmd5buff, md5buff, 16);
b151ef55
 
     md5str = (char*) cli_calloc(32 + 1, sizeof(char));
 
     for(i=0; i<16; i++)
c4f87775
 	cnt += sprintf(md5str + cnt, "%02x", md5buff[i]);
b151ef55
 
     return(md5str);
 }
 
 void *cli_malloc(size_t size)
 {
 	void *alloc;
 
     alloc = malloc(size);
 
     if(!alloc) {
 	cli_errmsg("cli_malloc(): Can't allocate memory (%d bytes).\n", size);
 	perror("malloc_problem");
fdfb0dd7
 	/* _exit(1); */
b151ef55
 	return NULL;
     } else return alloc;
 }
 
 void *cli_calloc(size_t nmemb, size_t size)
 {
 	void *alloc;
 
     alloc = calloc(nmemb, size);
 
     if(!alloc) {
 	cli_errmsg("cli_calloc(): Can't allocate memory (%d bytes).\n", nmemb * size);
 	perror("calloc_problem");
fdfb0dd7
 	/* _exit(1); */
b151ef55
 	return NULL;
     } else return alloc;
 }
 
4cd4319e
 void *cli_realloc(void *ptr, size_t size)
 {
 	void *alloc;
 
     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;
 }
 
c6259ac5
 unsigned int cl_rndnum(unsigned int max)
b151ef55
 {
     struct timeval tv;
 
   gettimeofday(&tv, (struct timezone *) 0);
   srand(tv.tv_usec+clock());
 
   return rand() % max;
 }
 
4cd4319e
 char *cl_gentemp(const char *dir)
b151ef55
 {
41b894c7
 	char *name, *tmp;
         const char *mdir;
c4f87775
 	unsigned char salt[16 + 32];
 	int i;
b151ef55
 	struct stat foo;
 
c4f87775
 
b151ef55
     if(!dir)
 	mdir = "/tmp";
     else
 	mdir = (char *) dir;
 
     name = (char*) cli_calloc(strlen(mdir) + 1 + 16 + 1, sizeof(char));
e8217f5a
     if(name == NULL) {
 	cli_dbgmsg("cl_gentemp('%s'): out of memory\n", dir);
 	return NULL;
     }
c4f87775
 
 #ifdef CL_THREAD_SAFE
     pthread_mutex_lock(&cl_gentemp_mutex);
 #endif
 
     memcpy(salt, oldmd5buff, 16);
b151ef55
 
     do {
c4f87775
 	for(i = 16; i < 48; i++)
c6259ac5
 	    salt[i] = cl_rndnum(255);
b151ef55
 
c4f87775
 	tmp = cl_md5buff(( char* ) salt, 48);
 	sprintf(name, "%s/", mdir);
7cc3891c
 	strncat(name, tmp, 16);
b151ef55
 	free(tmp);
     } while(stat(name, &foo) != -1);
 
c4f87775
 #ifdef CL_THREAD_SAFE
     pthread_mutex_unlock(&cl_gentemp_mutex);
 #endif
 
b151ef55
     return(name);
 }
 
 int cli_rmdirs(const char *dirname)
 {
 	DIR *dd;
 	struct dirent *dent;
 	struct stat maind, statbuf;
 	char *fname;
 
521b19b4
 
     chmod(dirname, 0700);
b151ef55
     if((dd = opendir(dirname)) != NULL) {
 	while(stat(dirname, &maind) != -1) {
 	    if(!rmdir(dirname)) break;
 
 	    while((dent = readdir(dd))) {
 		if(dent->d_ino) {
 		    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);
c6259ac5
 					free(fname);
b151ef55
 					return 0;
 				    }
 				    cli_rmdirs(fname);
 				}
 			    } else
 				unlink(fname);
 			}
 
 			free(fname);
 		    }
 		}
 	    }
 
 	    rewinddir(dd);
 
 	}
 
     } else { 
 	return 53;
     }
 
     closedir(dd);
     return 0;
 }
66fcd9f8
 
 /* 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;
3fc8c606
 
 
66fcd9f8
         todo = count;
         current = (unsigned char *) buff;
3fc8c606
 
66fcd9f8
         do {
                 retval = read(fd, current, todo);
                 if (retval == 0) {
                         return (count - todo);
                 }
                 if (retval < 0) {
b7fe0c97
 			if (errno == EINTR) {
 				continue;
 			}
66fcd9f8
                         return -1;
                 }
                 todo -= retval;
                 current += retval;
         } while (todo > 0);
3fc8c606
 
 
66fcd9f8
         return count;
 }
3fc8c606
 
66fcd9f8
 /* 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;
3fc8c606
 
 
66fcd9f8
         todo = count;
         current = (unsigned char *) buff;
3fc8c606
 
66fcd9f8
         do {
                 retval = write(fd, current, todo);
                 if (retval < 0) {
b7fe0c97
 			if (errno == EINTR) {
 				continue;
 			}
66fcd9f8
                         return -1;
                 }
                 todo -= retval;
                 current += retval;
         } while (todo > 0);
3fc8c606
 
 
66fcd9f8
         return count;
 }