clamav-devel/libclamav/scanners.c
e3aaff8e
 /*
f893c0f3
  *  Copyright (C) 2002 - 2004 Tomasz Kojm <tkojm@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 <string.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <dirent.h>
d66387b9
 #include <netinet/in.h>
 
e3aaff8e
 
888f5794
 #if HAVE_MMAP
 #if HAVE_SYS_MMAN_H
 #include <sys/mman.h>
 #else /* HAVE_SYS_MMAN_H */
 #undef HAVE_MMAP
 #endif
 #endif
 
414abe87
 #include <mspack.h>
 
e3aaff8e
 #ifdef CL_THREAD_SAFE
 #  include <pthread.h>
 pthread_mutex_t cli_scanrar_mutex = PTHREAD_MUTEX_INITIALIZER;
 #endif
6d6e8271
 int cli_scanrar_inuse = 0;
e3aaff8e
 
414abe87
 extern short cli_leavetemps_flag;
 
bbd2d959
 extern int cli_mbox(const char *dir, int desc, unsigned int options); /* FIXME */
8000d078
 
e3aaff8e
 #include "clamav.h"
 #include "others.h"
85dd8460
 #include "scanners.h"
8000d078
 #include "matcher-ac.h"
 #include "matcher-bm.h"
e3aaff8e
 #include "matcher.h"
 #include "unrarlib.h"
47bbbc56
 #include "ole2_extract.h"
 #include "vba_extract.h"
341e5433
 #include "msexpand.h"
a5373b64
 #include "chmunpack.h"
a9082ea2
 #include "pe.h"
888f5794
 #include "filetypes.h"
 #include "htmlnorm.h"
2b259453
 #include "untar.h"
c3a3be2d
 #include "special.h"
e3aaff8e
 
 #ifdef HAVE_ZLIB_H
 #include <zlib.h>
 #include <zzip.h>
 #endif
 
 #ifdef HAVE_BZLIB_H
 #include <bzlib.h>
 #endif
 
3805ebcb
 #define SCAN_ARCHIVE	    (options & CL_SCAN_ARCHIVE)
 #define SCAN_MAIL	    (options & CL_SCAN_MAIL)
 #define SCAN_OLE2	    (options & CL_SCAN_OLE2)
 #define SCAN_HTML	    (options & CL_SCAN_HTML)
 #define SCAN_PE		    (options & CL_SCAN_PE)
 #define DISABLE_RAR	    (options & CL_SCAN_DISABLERAR)
08d6b1e3
 #define DETECT_ENCRYPTED    (options & CL_SCAN_BLOCKENCRYPTED)
3805ebcb
 #define BLOCKMAX	    (options & CL_SCAN_BLOCKMAX)
6d6e8271
 
03a2d04a
 #define MAX_MAIL_RECURSION  15
e3aaff8e
 
3805ebcb
 static int cli_scanfile(const char *filename, const char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec);
e3aaff8e
 
4048c4f6
 
e3aaff8e
 #ifdef CL_THREAD_SAFE
68a6f51f
 static void cli_unlock_mutex(void *mtx)
e3aaff8e
 {
     cli_dbgmsg("Pthread cancelled. Unlocking mutex.\n");
     pthread_mutex_unlock(mtx);
 }
 #endif
 
3805ebcb
 static int cli_scanrar(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
e3aaff8e
 {
fc56deed
 	FILE *tmp = NULL;
b5f8af0d
 	int files = 0, fd, ret = CL_CLEAN, afiles;
e3aaff8e
 	ArchiveList_struct *rarlist = NULL;
68a6f51f
 	ArchiveList_struct *rarlist_head = NULL;
e3aaff8e
 	char *rar_data_ptr;
 	unsigned long rar_data_size;
 
2b259453
 
     cli_dbgmsg("in scanrar()\n");
e3aaff8e
 
 
 #ifdef CL_THREAD_SAFE
     pthread_cleanup_push(cli_unlock_mutex, &cli_scanrar_mutex);
     pthread_mutex_lock(&cli_scanrar_mutex);
     cli_scanrar_inuse = 1;
 #endif
 
b5f8af0d
     if(! (afiles = urarlib_list(desc, (ArchiveList_struct *) &rarlist))) {
e3aaff8e
 #ifdef CL_THREAD_SAFE
 	pthread_mutex_unlock(&cli_scanrar_mutex);
 	cli_scanrar_inuse = 0;
 #endif
 	return CL_ERAR;
     }
 
2b259453
     cli_dbgmsg("RAR: Number of archived files: %d\n", afiles);
b5f8af0d
 
68a6f51f
     rarlist_head = rarlist;
 
e3aaff8e
     while(rarlist) {
dfe7ca62
 	if(DETECT_ENCRYPTED && (rarlist->item.Flags & 4)) {
 	    files++;
2b259453
 	    cli_dbgmsg("RAR: Encrypted files found in archive.\n");
32c970f8
 	    lseek(desc, 0, SEEK_SET);
b68d11d2
 	    if(cli_scandesc(desc, virname, scanned, root, 0, 0) != CL_VIRUS)
32c970f8
 		*virname = "Encrypted.RAR";
dfe7ca62
 	    ret = CL_VIRUS;
 	    break;
 	}
e3aaff8e
 
 	if(limits) {
d272908a
 
 	    if(limits->maxratio && rarlist->item.UnpSize && rarlist->item.PackSize) {
 		if((unsigned int) rarlist->item.UnpSize / (unsigned int) rarlist->item.PackSize >= limits->maxratio) {
 		    cli_dbgmsg("RAR: Max ratio reached (normal: %d, compressed: %d, max: %ld)\n", (int) rarlist->item.UnpSize, (int) rarlist->item.PackSize, limits->maxratio);
 		    *virname = "Oversized.RAR";
 		    ret = CL_VIRUS;
 		    break;
 		}
 	    }
 
ba174707
 	    if(limits->maxfilesize && (rarlist->item.UnpSize > (unsigned int) limits->maxfilesize)) {
2b259453
 		cli_dbgmsg("RAR: %s: Size exceeded (%u, max: %lu)\n", rarlist->item.Name, (unsigned int) rarlist->item.UnpSize, limits->maxfilesize);
e3aaff8e
 		rarlist = rarlist->next;
 		files++;
d272908a
 		if(BLOCKMAX) {
 		    *virname = "RAR.ExceededFileSize";
 		    ret = CL_VIRUS;
 		    break;
 		}
e3aaff8e
 		continue;
 	    }
 
 	    if(limits->maxfiles && (files > limits->maxfiles)) {
 		cli_dbgmsg("RAR: Files limit reached (max: %d)\n", limits->maxfiles);
d272908a
 		if(BLOCKMAX) {
 		    *virname = "RAR.ExceededFilesLimit";
 		    ret = CL_VIRUS;
 		    break;
 		}
e3aaff8e
 		break;
 	    }
 	}
 
e332ad07
         if(rarlist->item.FileAttr & RAR_FENTRY_ATTR_DIRECTORY) {
68a6f51f
             rarlist = rarlist->next;
             files++;
             continue;
         }
 
e3aaff8e
 	if((tmp = tmpfile()) == NULL) {
2b259453
 	    cli_dbgmsg("RAR: Can't generate temporary file.\n");
e3aaff8e
 #ifdef CL_THREAD_SAFE
 	    pthread_mutex_unlock(&cli_scanrar_mutex);
 	    cli_scanrar_inuse = 0;
 #endif
 	    return CL_ETMPFILE;
 	}
 	fd = fileno(tmp);
 
68a6f51f
 	if( urarlib_get(&rar_data_ptr, &rar_data_size, rarlist->item.Name, desc, "clam")) {
2b259453
 	    cli_dbgmsg("RAR: Extracted: %s, size: %lu\n", rarlist->item.Name, rar_data_size);
68a6f51f
 	    if(fwrite(rar_data_ptr, 1, rar_data_size, tmp) != rar_data_size) {
2b259453
 		cli_dbgmsg("RAR: Can't write to file.\n");
cdb0ae9c
 		fclose(tmp);
e3aaff8e
 		tmp = NULL;
 		ret = CL_ERAR;
cdb0ae9c
 		if(rar_data_ptr) {
e3aaff8e
 		    free(rar_data_ptr);
cdb0ae9c
 		    rar_data_ptr = NULL;
 		}
27a3f44a
 		break;
e3aaff8e
 	    }
 
cdb0ae9c
 	    if(rar_data_ptr) {
e3aaff8e
 		free(rar_data_ptr);
cdb0ae9c
 		rar_data_ptr = NULL;
 	    }
 	    if(fflush(tmp) != 0) {
2b259453
 		cli_dbgmsg("RAR: fflush() failed: %s\n", strerror(errno));
cdb0ae9c
 		fclose(tmp);
68a6f51f
 		urarlib_freelist(rarlist_head);
e3aaff8e
 #ifdef CL_THREAD_SAFE
 		pthread_mutex_unlock(&cli_scanrar_mutex);
 		cli_scanrar_inuse = 0;
 #endif
 		return CL_EFSYNC;
 	    }
 
 	    lseek(fd, 0, SEEK_SET);
22275b15
 	    if((ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, arec, mrec)) == CL_VIRUS ) {
2b259453
 		cli_dbgmsg("RAR: Infected with %s\n", *virname);
6d6e8271
 
cdb0ae9c
 		fclose(tmp);
e3aaff8e
 		urarlib_freelist(rarlist);
 #ifdef CL_THREAD_SAFE
 		pthread_mutex_unlock(&cli_scanrar_mutex);
 		cli_scanrar_inuse = 0;
 #endif
68a6f51f
   		return ret;
e3aaff8e
 	    }
 
 	} else {
2b259453
 	    cli_dbgmsg("RAR: Can't decompress file %s\n", rarlist->item.Name);
cdb0ae9c
 	    fclose(tmp);
7761c6eb
 	    tmp = NULL;
cdb0ae9c
 	    ret = CL_ERAR; /* WinRAR 3.0 ? */
 	    break;
e3aaff8e
 	}
 
cdb0ae9c
 	fclose(tmp);
e3aaff8e
 	tmp = NULL;
 	rarlist = rarlist->next;
 	files++;
     }
 
68a6f51f
     urarlib_freelist(rarlist_head);
e3aaff8e
 #ifdef CL_THREAD_SAFE
     pthread_mutex_unlock(&cli_scanrar_mutex);
     cli_scanrar_inuse = 0;
     pthread_cleanup_pop(0);
 #endif
7761c6eb
     
2b259453
     cli_dbgmsg("RAR: Exit code: %d\n", ret);
7761c6eb
 
e3aaff8e
     return ret;
 }
 
 #ifdef HAVE_ZLIB_H
3805ebcb
 static int cli_scanzip(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
e3aaff8e
 {
 	ZZIP_DIR *zdir;
 	ZZIP_DIRENT zdirent;
 	ZZIP_FILE *zfp;
fc56deed
 	FILE *tmp = NULL;
f7148839
 	char *buff;
ee039e40
 	int fd, bytes, files = 0, ret = CL_CLEAN;
fc56deed
 	struct stat source;
ee039e40
 	zzip_error_t err;
e3aaff8e
 
2b259453
 
     cli_dbgmsg("in scanzip()\n");
e3aaff8e
 
049a18b9
     if((zdir = zzip_dir_fdopen(dup(desc), &err)) == NULL) {
2b259453
 	cli_dbgmsg("Zip: Not supported file format ?.\n");
 	cli_dbgmsg("Zip: zzip_dir_fdopen() return code: %d\n", err);
77be7ea9
 	/* no return with CL_EZIP due to password protected zips */
 	return CL_CLEAN;
e3aaff8e
     }
 
fc56deed
     fstat(desc, &source);
 
33f40ee5
     if(!(buff = (char *) cli_malloc(FILEBUFF))) {
2b259453
 	cli_dbgmsg("Zip: unable to malloc(%d)\n", FILEBUFF);
33f40ee5
 	zzip_dir_close(zdir);
 	return CL_EMEM;
     }
 
e3aaff8e
     while(zzip_dir_read(zdir, &zdirent)) {
fc56deed
 
 	if(!zdirent.d_name || !strlen(zdirent.d_name)) { /* Mimail fix */
2b259453
 	    cli_dbgmsg("Zip: strlen(zdirent.d_name) == %d\n", strlen(zdirent.d_name));
a0977bfe
 	    *virname = "Suspected.Zip";
fc56deed
 	    ret = CL_VIRUS;
 	    break;
 	}
 
2b259453
 	cli_dbgmsg("Zip: %s, compressed: %u, normal: %u, ratio: %d (max: %d)\n", zdirent.d_name, zdirent.d_csize, zdirent.st_size, zdirent.st_size / (zdirent.d_csize+1), limits ? limits->maxratio : -1 );
fc56deed
 
19ca43de
 	if(!zdirent.st_size) { /* omit directories and empty files */
e3aaff8e
 	    files++;
 	    continue;
 	}
 
 	/* work-around for problematic zips (zziplib crashes with them) */
19ca43de
 	if(zdirent.d_csize <= 0 || zdirent.st_size < 0) {
e3aaff8e
 	    files++;
2b259453
 	    cli_dbgmsg("Zip: Malformed archive detected.\n");
fc56deed
 	    /* ret = CL_EMALFZIP; */
 	    /* report it as a virus */
a0977bfe
 	    *virname = "Suspected.Zip";
fc56deed
 	    ret = CL_VIRUS;
e3aaff8e
 	    break;
 	}
 
19ca43de
 	if(limits && limits->maxratio > 0 && ((unsigned) zdirent.st_size / (unsigned) zdirent.d_csize) >= limits->maxratio) {
 	    *virname = "Oversized.Zip";
 	    ret = CL_VIRUS;
 	    break;
         }
 
bddfdc19
 	if(DETECT_ENCRYPTED && (zdirent.d_flags & 1 )) {
0f34221a
 	    files++;
2b259453
 	    cli_dbgmsg("Zip: Encrypted files found in archive.\n");
32c970f8
 	    lseek(desc, 0, SEEK_SET);
b68d11d2
 	    if(cli_scandesc(desc, virname, scanned, root, 0, 0) != CL_VIRUS)
32c970f8
 		*virname = "Encrypted.Zip";
0f34221a
 	    ret = CL_VIRUS;
 	    break;
 	}
 
e3aaff8e
 	if(limits) {
 	    if(limits->maxfilesize && (zdirent.st_size > limits->maxfilesize)) {
2b259453
 		cli_dbgmsg("Zip: %s: Size exceeded (%d, max: %ld)\n", zdirent.d_name, zdirent.st_size, limits->maxfilesize);
e3aaff8e
 		files++;
6ccc6990
 		/* ret = CL_EMAXSIZE; */
d272908a
 		if(BLOCKMAX) {
 		    *virname = "Zip.ExceededFileSize";
 		    ret = CL_VIRUS;
 		    break;
 		}
 		continue; /* continue scanning */
e3aaff8e
 	    }
 
 	    if(limits->maxfiles && (files > limits->maxfiles)) {
 		cli_dbgmsg("Zip: Files limit reached (max: %d)\n", limits->maxfiles);
d272908a
 		if(BLOCKMAX) {
 		    *virname = "Zip.ExceededFilesLimit";
 		    ret = CL_VIRUS;
 		    break;
 		}
e3aaff8e
 		break;
 	    }
 	}
 
 	/* generate temporary file and get its descriptor */
 	if((tmp = tmpfile()) == NULL) {
2b259453
 	    cli_dbgmsg("Zip: Can't generate tmpfile().\n");
fc56deed
 	    ret = CL_ETMPFILE;
 	    break;
e3aaff8e
 	}
 
 	if((zfp = zzip_file_open(zdir, zdirent.d_name, 0)) == NULL) {
2b259453
 	    cli_dbgmsg("Zip: Can't open file %s\n", zdirent.d_name);
e3aaff8e
 	    ret = CL_EZIP;
fc56deed
 	    break;
e3aaff8e
 	}
 
f7148839
 	while((bytes = zzip_file_read(zfp, buff, FILEBUFF)) > 0) {
467f8b1e
 	    if(fwrite(buff, 1, bytes, tmp) != (size_t) bytes) {
2b259453
 		cli_dbgmsg("Zip: Can't write to file.\n");
e3aaff8e
 		zzip_file_close(zfp);
33f40ee5
 		zzip_dir_close(zdir);
 		fclose(tmp);
f7148839
 		free(buff);
33f40ee5
 		return CL_EZIP;
e3aaff8e
 	    }
 	}
 
 	zzip_file_close(zfp);
 
65a18d2e
 
cdb0ae9c
 	if(fflush(tmp) != 0) {
2b259453
 	    cli_dbgmsg("Zip: fflush() failed: %s\n", strerror(errno));
fc56deed
 	    ret = CL_EFSYNC;
 	    break;
e3aaff8e
 	}
 
cdb0ae9c
 	fd = fileno(tmp);
 
e3aaff8e
 	lseek(fd, 0, SEEK_SET);
22275b15
 	if((ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, arec, mrec)) == CL_VIRUS ) {
2b259453
 	    cli_dbgmsg("Zip: Infected with %s\n", *virname);
e3aaff8e
 	    ret = CL_VIRUS;
 	    break;
 	} else if(ret == CL_EMALFZIP) {
 	    /* 
fc56deed
 	     * The trick with detection of ZoD only works with higher (>= 5)
e3aaff8e
 	     * recursion limit level.
 	     */
2b259453
 	    cli_dbgmsg("Zip: Malformed Zip file, scanning stopped.\n");
a0977bfe
 	    *virname = "Suspected.Zip";
e3aaff8e
 	    ret = CL_VIRUS;
 	    break;
 	}
 
cdb0ae9c
 	if (tmp) {
 	    fclose(tmp);
 	    tmp = NULL;
 	}
e3aaff8e
 	files++;
     }
 
     zzip_dir_close(zdir);
cdb0ae9c
     if (tmp) {
 	fclose(tmp);
 	tmp = NULL;
     }
33f40ee5
 
     free(buff);
e3aaff8e
     return ret;
 }
 
3805ebcb
 static int cli_scangzip(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
e3aaff8e
 {
 	int fd, bytes, ret = CL_CLEAN;
 	long int size = 0;
f7148839
 	char *buff;
fc56deed
 	FILE *tmp = NULL;
e3aaff8e
 	gzFile gd;
 
 
fc56deed
     cli_dbgmsg("in cli_scangzip()\n");
 
e3aaff8e
     if((gd = gzdopen(dup(desc), "rb")) == NULL) {
2b259453
 	cli_dbgmsg("GZip: Can't open descriptor %d\n", desc);
e3aaff8e
 	return CL_EGZIP;
     }
 
     if((tmp = tmpfile()) == NULL) {
2b259453
 	cli_dbgmsg("GZip: Can't generate temporary file.\n");
e3aaff8e
 	gzclose(gd);
 	return CL_ETMPFILE;
     }
     fd = fileno(tmp);
 
fc56deed
     if(!(buff = (char *) cli_malloc(FILEBUFF))) {
2b259453
 	cli_dbgmsg("GZip: Unable to malloc %d bytes.\n", FILEBUFF);
fc56deed
 	gzclose(gd);
f7148839
 	return CL_EMEM;
fc56deed
     }
f7148839
 
fc56deed
     while((bytes = gzread(gd, buff, FILEBUFF)) > 0) {
e3aaff8e
 	size += bytes;
 
 	if(limits)
fc56deed
 	    if(limits->maxfilesize && (size + FILEBUFF > limits->maxfilesize)) {
2b259453
 		cli_dbgmsg("GZip: Size exceeded (stopped at %ld, max: %ld)\n", size, limits->maxfilesize);
6ccc6990
 		/* ret = CL_EMAXSIZE; */
e3aaff8e
 		break;
 	    }
 
4152ad50
 	if(cli_writen(fd, buff, bytes) != bytes) {
2b259453
 	    cli_dbgmsg("GZip: Can't write to file.\n");
cdb0ae9c
 	    fclose(tmp);
e3aaff8e
 	    gzclose(gd);
f7148839
 	    free(buff);
e3aaff8e
 	    return CL_EGZIP;
 	}
     }
 
f7148839
     free(buff);
e3aaff8e
     gzclose(gd);
     if(fsync(fd) == -1) {
2b259453
 	cli_dbgmsg("GZip: Can't synchronise descriptor %d\n", fd);
cdb0ae9c
 	fclose(tmp);
e3aaff8e
 	return CL_EFSYNC;
     }
 
     lseek(fd, 0, SEEK_SET);
22275b15
     if((ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, arec, mrec)) == CL_VIRUS ) {
2b259453
 	cli_dbgmsg("GZip: Infected with %s\n", *virname);
cdb0ae9c
 	fclose(tmp);
e3aaff8e
 	return CL_VIRUS;
     }
cdb0ae9c
     fclose(tmp);
e3aaff8e
 
     return ret;
 }
 #endif
 
 #ifdef HAVE_BZLIB_H
 
 #ifdef NOBZ2PREFIX
 #define BZ2_bzReadOpen bzReadOpen
 #define BZ2_bzReadClose bzReadClose
 #define BZ2_bzRead bzRead
 #endif
 
3805ebcb
 static int cli_scanbzip(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
e3aaff8e
 {
 	int fd, bytes, ret = CL_CLEAN, bzerror = 0;
 	short memlim = 0;
 	long int size = 0;
f7148839
 	char *buff;
fc56deed
 	FILE *fs, *tmp = NULL;
e3aaff8e
 	BZFILE *bfd;
 
 
9e431a95
     if((fs = fdopen(dup(desc), "rb")) == NULL) {
2b259453
 	cli_dbgmsg("Bzip: Can't open descriptor %d.\n", desc);
e3aaff8e
 	return CL_EBZIP;
     }
 
     if(limits)
 	if(limits->archivememlim)
 	    memlim = 1;
 
68b96877
     if((bfd = BZ2_bzReadOpen(&bzerror, fs, 0, memlim, NULL, 0)) == NULL) {
2b259453
 	cli_dbgmsg("Bzip: Can't initialize bzip2 library (descriptor: %d).\n", desc);
9e431a95
 	fclose(fs);
e3aaff8e
 	return CL_EBZIP;
     }
 
     if((tmp = tmpfile()) == NULL) {
2b259453
 	cli_dbgmsg("Bzip: Can't generate temporary file.\n");
e3aaff8e
 	BZ2_bzReadClose(&bzerror, bfd);
9e431a95
 	fclose(fs);
e3aaff8e
 	return CL_ETMPFILE;
     }
     fd = fileno(tmp);
 
9e431a95
     if(!(buff = (char *) malloc(FILEBUFF))) {
2b259453
 	cli_dbgmsg("Bzip: Unable to malloc %d bytes.\n", FILEBUFF);
9e431a95
 	fclose(tmp);
 	fclose(fs);
 	BZ2_bzReadClose(&bzerror, bfd);
f7148839
 	return CL_EMEM;
9e431a95
     }
f7148839
 
fc56deed
     while((bytes = BZ2_bzRead(&bzerror, bfd, buff, FILEBUFF)) > 0) {
e3aaff8e
 	size += bytes;
 
 	if(limits)
fc56deed
 	    if(limits->maxfilesize && (size + FILEBUFF > limits->maxfilesize)) {
2b259453
 		cli_dbgmsg("Bzip: Size exceeded (stopped at %ld, max: %ld)\n", size, limits->maxfilesize);
6ccc6990
 		/* ret = CL_EMAXSIZE; */
e3aaff8e
 		break;
 	    }
 
4152ad50
 	if(cli_writen(fd, buff, bytes) != bytes) {
2b259453
 	    cli_dbgmsg("Bzip: Can't write to file.\n");
e3aaff8e
 	    BZ2_bzReadClose(&bzerror, bfd);
cdb0ae9c
 	    fclose(tmp);
f7148839
 	    free(buff);
9e431a95
 	    fclose(fs);
e3aaff8e
 	    return CL_EGZIP;
 	}
     }
 
f7148839
     free(buff);
e3aaff8e
     BZ2_bzReadClose(&bzerror, bfd);
     if(fsync(fd) == -1) {
2b259453
 	cli_dbgmsg("Bzip: Synchronisation failed for descriptor %d\n", fd);
cdb0ae9c
 	fclose(tmp);
9e431a95
 	fclose(fs);
e3aaff8e
 	return CL_EFSYNC;
     }
 
     lseek(fd, 0, SEEK_SET);
22275b15
     if((ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, arec, mrec)) == CL_VIRUS ) {
2b259453
 	cli_dbgmsg("Bzip: Infected with %s\n", *virname);
e3aaff8e
     }
cdb0ae9c
     fclose(tmp);
9e431a95
     fclose(fs);
e3aaff8e
 
     return ret;
 }
 #endif
 
3805ebcb
 static int cli_scanszdd(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
341e5433
 {
 	int fd, ret = CL_CLEAN;
 	FILE *tmp = NULL, *in;
 
2b259453
 
341e5433
     cli_dbgmsg("in cli_scanmscomp()\n");
 
     if((in = fdopen(dup(desc), "rb")) == NULL) {
2b259453
 	cli_dbgmsg("SZDD: Can't open descriptor %d\n", desc);
341e5433
 	return CL_EMSCOMP;
     }
 
     if((tmp = tmpfile()) == NULL) {
2b259453
 	cli_dbgmsg("SZDD: Can't generate temporary file.\n");
341e5433
 	fclose(in);
 	return CL_ETMPFILE;
     }
 
     if(cli_msexpand(in, tmp) == -1) {
2b259453
 	cli_dbgmsg("SZDD: msexpand failed.\n");
341e5433
 	return CL_EMSCOMP;
     }
 
     fclose(in);
     if(fflush(tmp)) {
2b259453
 	cli_dbgmsg("SZDD: fflush() failed.\n");
341e5433
 	fclose(tmp);
 	return CL_EFSYNC;
     }
 
     fd = fileno(tmp);
     lseek(fd, 0, SEEK_SET);
22275b15
     if((ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, arec, mrec)) == CL_VIRUS) {
2b259453
 	cli_dbgmsg("SZDD: Infected with %s\n", *virname);
341e5433
 	fclose(tmp);
 	return CL_VIRUS;
     }
 
     fclose(tmp);
     return ret;
 }
 
3805ebcb
 static int cli_scanmscab(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
414abe87
 {
 	struct mscab_decompressor *cabd = NULL;
 	struct mscabd_cabinet *base, *cab;
 	struct mscabd_file *file;
 	const char *tmpdir;
 	char *tempname;
 	int ret = CL_CLEAN;
 
 
     cli_dbgmsg("in cli_scanmscab()\n");
 
     if((cabd = mspack_create_cab_decompressor(NULL)) == NULL) {
2b259453
 	cli_dbgmsg("MSCAB: Can't create libmspack CAB decompressor\n");
414abe87
 	return CL_EMSCAB;
     }
 
     if((base = cabd->dsearch(cabd, desc)) == NULL) {
2b259453
 	cli_dbgmsg("MSCAB: I/O error or no valid cabinets found\n");
414abe87
 	mspack_destroy_cab_decompressor(cabd);
 	return CL_EMSCAB;
     }
 
     if((tmpdir = getenv("TMPDIR")) == NULL)
 #ifdef P_tmpdir
 	tmpdir = P_tmpdir;
 #else
 	tmpdir = "/tmp";
 #endif
 
     for(cab = base; cab; cab = cab->next) {
 	for(file = cab->files; file; file = file->next) {
8000d078
 	    tempname = cli_gentemp(tmpdir);
2b259453
 	    cli_dbgmsg("MSCAB: Extracting data to %s\n", tempname);
414abe87
 	    if(cabd->extract(cabd, file, tempname)) {
2b259453
 		cli_dbgmsg("MSCAB: libmscab error code: %d\n", cabd->last_error(cabd));
414abe87
 	    } else {
22275b15
 		ret = cli_scanfile(tempname, virname, scanned, root, limits, options, arec, mrec);
414abe87
 	    }
 	    if(!cli_leavetemps_flag)
 		unlink(tempname);
 	    free(tempname);
 	    if(ret == CL_VIRUS)
 		break;
 	}
 	if(ret == CL_VIRUS)
 	    break;
     }
 
     cabd->close(cabd, base);
     mspack_destroy_cab_decompressor(cabd);
 
     return ret;
 }
 
3805ebcb
 static int cli_scanhtml(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
888f5794
 {
e57fa318
 	char *tempname, fullname[1024];
 	const char *tmpdir;
 	int ret=CL_CLEAN, fd;
888f5794
 
3f117a05
 
888f5794
     cli_dbgmsg("in cli_scanhtml()\n");
 
e57fa318
     if((tmpdir = getenv("TMPDIR")) == NULL)
 #ifdef P_tmpdir
         tmpdir = P_tmpdir;
 #else
         tmpdir = "/tmp";
 #endif
                                                                                                                                            
     tempname = cli_gentemp(tmpdir);
                                                                                                                                            
     if(mkdir(tempname, 0700)) {
         cli_dbgmsg("ScanHTML -> Can't create temporary directory %s\n", tempname);
         return CL_ETMPDIR;
888f5794
     }
 
e57fa318
     html_normalise_fd(desc, tempname, NULL);
     snprintf(fullname, 1024, "%s/comment.html", tempname);
     fd = open(fullname, O_RDONLY);
     if (fd >= 0) {
b68d11d2
         ret = cli_scandesc(fd, virname, scanned, root, 0, CL_TYPE_HTML);
e57fa318
 	close(fd);
2d83618e
     }
 
e57fa318
     if (ret == CL_CLEAN) {
 	snprintf(fullname, 1024, "%s/nocomment.html", tempname);
 	fd = open(fullname, O_RDONLY);
 	if (fd >= 0) {
b68d11d2
 	    ret = cli_scandesc(fd, virname, scanned, root, 0, CL_TYPE_HTML);
e57fa318
 	    close(fd);
 	}
888f5794
     }
 
e57fa318
     if (ret == CL_CLEAN) {
 	snprintf(fullname, 1024, "%s/script.html", tempname);
 	fd = open(fullname, O_RDONLY);
 	if (fd >= 0) {
b68d11d2
 	    ret = cli_scandesc(fd, virname, scanned, root, 0, CL_TYPE_HTML);
e57fa318
 	    close(fd);
 	}
888f5794
     }
 
e57fa318
     if(!cli_leavetemps_flag)
         cli_rmdirs(tempname);
888f5794
 
e57fa318
     free(tempname);
888f5794
     return ret;
 }
 
3805ebcb
 static int cli_scandir(const char *dirname, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
47bbbc56
 {
467f8b1e
 	DIR *dd;
 	struct dirent *dent;
72a1b240
 #if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
 	struct dirent result;
 #endif
467f8b1e
 	struct stat statbuf;
 	char *fname;
47bbbc56
 
 
467f8b1e
     if((dd = opendir(dirname)) != NULL) {
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
467f8b1e
 	while((dent = readdir(dd))) {
72a1b240
 #endif
feeaa333
 #ifndef C_INTERIX
 	    if(dent->d_ino)
 #endif
 	    {
467f8b1e
 		if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
 		    /* build the full name */
 		    fname = cli_calloc(strlen(dirname) + strlen(dent->d_name) + 2, sizeof(char));
 		    sprintf(fname, "%s/%s", dirname, dent->d_name);
47bbbc56
 
467f8b1e
 		    /* stat the file */
 		    if(lstat(fname, &statbuf) != -1) {
 			if(S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) {
22275b15
 			    if (cli_scandir(fname, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) {
467f8b1e
 				free(fname);
 				closedir(dd);
 				return CL_VIRUS;
 			    }
 			} else
 			    if(S_ISREG(statbuf.st_mode))
22275b15
 				if(cli_scanfile(fname, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) {
467f8b1e
 				    free(fname);
 				    closedir(dd);
 				    return CL_VIRUS;
 				}
47bbbc56
 
bbb90786
 		    }
467f8b1e
 		    free(fname);
47bbbc56
 		}
 	    }
 	}
467f8b1e
     } else {
2b259453
 	cli_dbgmsg("ScanDir: Can't open directory %s.\n", dirname);
467f8b1e
 	return CL_EOPEN;
     }
47bbbc56
 
467f8b1e
     closedir(dd);
     return 0;
47bbbc56
 }
467f8b1e
 
3805ebcb
 static int cli_vba_scandir(const char *dirname, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
e3aaff8e
 {
467f8b1e
 	int ret = CL_CLEAN, i, fd, data_len;
 	vba_project_t *vba_project;
e3aaff8e
 	DIR *dd;
 	struct dirent *dent;
72a1b240
 #if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
 	struct dirent result;
 #endif
e3aaff8e
 	struct stat statbuf;
56bfccb2
 	char *fname, *fullname;
467f8b1e
 	unsigned char *data;
 
2b259453
 
     cli_dbgmsg("VBADir: %s\n", dirname);
467f8b1e
     if((vba_project = (vba_project_t *) vba56_dir_read(dirname))) {
e3aaff8e
 
467f8b1e
 	for(i = 0; i < vba_project->count; i++) {
 	    fullname = (char *) cli_malloc(strlen(vba_project->dir) + strlen(vba_project->name[i]) + 2);
 	    sprintf(fullname, "%s/%s", vba_project->dir, vba_project->name[i]);
 	    fd = open(fullname, O_RDONLY);
 	    if(fd == -1) {
2b259453
 		cli_dbgmsg("VBADir: Can't open file %s\n", fullname);
467f8b1e
 		free(fullname);
 		ret = CL_EOPEN;
 		break;
 	    }
 	    free(fullname);
2b259453
             cli_dbgmsg("VBADir: Decompress VBA project '%s'\n", vba_project->name[i]);
467f8b1e
 	    data = (unsigned char *) vba_decompress(fd, vba_project->offset[i], &data_len);
 	    close(fd);
 
 	    if(!data) {
2b259453
 		cli_dbgmsg("VBADir: WARNING: VBA project '%s' decompressed to NULL\n", vba_project->name[i]);
467f8b1e
 	    } else {
7ec67e94
 		if(cli_scanbuff(data, data_len, virname, root, CL_TYPE_MSOLE2) == CL_VIRUS) {
467f8b1e
 		    free(data);
 		    ret = CL_VIRUS;
 		    break;
 		}
 
 		free(data);
 	    }
 	}
 
 	for(i = 0; i < vba_project->count; i++)
 	    free(vba_project->name[i]);
 	free(vba_project->name);
 	free(vba_project->dir);
 	free(vba_project->offset);
 	free(vba_project);
c240c32c
     } else if ((fullname = ppt_vba_read(dirname))) {
22275b15
     	if(cli_scandir(fullname, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) {
c240c32c
 	    ret = CL_VIRUS;
 	}
 	cli_rmdirs(fullname);
     	free(fullname);
2e53806b
     } else if ((vba_project = (vba_project_t *) wm_dir_read(dirname))) {
     	for (i = 0; i < vba_project->count; i++) {
 		fullname = (char *) cli_malloc(strlen(vba_project->dir) + strlen(vba_project->name[i]) + 2);
 		sprintf(fullname, "%s/%s", vba_project->dir, vba_project->name[i]);
 		fd = open(fullname, O_RDONLY);
 		if(fd == -1) {
2b259453
 			cli_dbgmsg("VBADir: Can't open file %s\n", fullname);
2e53806b
 			free(fullname);
 			ret = CL_EOPEN;
 			break;
 		}
 		free(fullname);
2b259453
 		cli_dbgmsg("VBADir: Decompress WM project '%s' macro:%d key:%d\n", vba_project->name[i], i, vba_project->key[i]);
2e53806b
 		data = (unsigned char *) wm_decrypt_macro(fd, vba_project->offset[i], vba_project->length[i], vba_project->key[i]);
 		close(fd);
 		
 		if(!data) {
2b259453
 			cli_dbgmsg("VBADir: WARNING: WM project '%s' macro %d decrypted to NULL\n", vba_project->name[i], i);
2e53806b
 		} else {
7ec67e94
 			if(cli_scanbuff(data, vba_project->length[i], virname, root, CL_TYPE_MSOLE2) == CL_VIRUS) {
2e53806b
 				free(data);
 				ret = CL_VIRUS;
 				break;
 			}
 			free(data);
 		}
 	}
 	for(i = 0; i < vba_project->count; i++)
 	    free(vba_project->name[i]);
 	free(vba_project->key);
 	free(vba_project->length);
 	free(vba_project->offset);
 	free(vba_project->name);
 	free(vba_project->dir);
 	free(vba_project);
467f8b1e
     }
2e53806b
 			
467f8b1e
     if(ret != CL_CLEAN)
     	return ret;
e3aaff8e
 
     if((dd = opendir(dirname)) != NULL) {
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, "..")) {
 		    /* build the full 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))
22275b15
 			    if (cli_vba_scandir(fname, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) {
467f8b1e
 			    	ret = CL_VIRUS;
 				free(fname);
 				break;
 			    }
e3aaff8e
 		    }
 		    free(fname);
 		}
 	    }
 	}
     } else {
2b259453
 	cli_dbgmsg("VBADir: Can't open directory %s.\n", dirname);
e3aaff8e
 	return CL_EOPEN;
     }
 
     closedir(dd);
467f8b1e
     return ret;
 }
 
3805ebcb
 static int cli_scanole2(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
467f8b1e
 {
 	const char *tmpdir;
56bfccb2
 	char *dir;
 	int ret = CL_CLEAN;
467f8b1e
 
2b259453
 
467f8b1e
     cli_dbgmsg("in cli_scanole2()\n");
 
414abe87
     if((tmpdir = getenv("TMPDIR")) == NULL)
467f8b1e
 #ifdef P_tmpdir
 	tmpdir = P_tmpdir;
 #else
 	tmpdir = "/tmp";
 #endif
 
     /* generate the temporary directory */
8000d078
     dir = cli_gentemp(tmpdir);
467f8b1e
     if(mkdir(dir, 0700)) {
2b259453
 	cli_dbgmsg("OLE2: Can't create temporary directory %s\n", dir);
467f8b1e
 	return CL_ETMPDIR;
     }
 
e5a5b2f2
     if((ret = cli_ole2_extract(desc, dir, limits))) {
2b259453
 	cli_dbgmsg("OLE2: %s\n", cl_strerror(ret));
467f8b1e
 	cli_rmdirs(dir);
 	free(dir);
 	return ret;
     }
 
22275b15
     if((ret = cli_vba_scandir(dir, virname, scanned, root, limits, options, arec, mrec)) != CL_VIRUS) {
 	if(cli_scandir(dir, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) {
56bfccb2
 	    ret = CL_VIRUS;
467f8b1e
 	}
     }
 
     cli_rmdirs(dir);
     free(dir);
     return ret;
e3aaff8e
 }
 
3805ebcb
 static int cli_scantar(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
2b259453
 {
 	const char *tmpdir;
 	char *dir;
 	int ret = CL_CLEAN;
 
 
     cli_dbgmsg("in cli_scantar()\n");
 
     if((tmpdir = getenv("TMPDIR")) == NULL)
 #ifdef P_tmpdir
 	tmpdir = P_tmpdir;
 #else
 	tmpdir = "/tmp";
 #endif
 
     /* generate temporary directory */
     dir = cli_gentemp(tmpdir);
     if(mkdir(dir, 0700)) {
 	cli_errmsg("Tar: Can't create temporary directory %s\n", dir);
 	return CL_ETMPDIR;
     }
 
     if((ret = cli_untar(dir, desc)))
 	cli_dbgmsg("Tar: %s\n", cl_strerror(ret));
     else
 	ret = cli_scandir(dir, virname, scanned, root, limits, options, arec, mrec);
 
     if(!cli_leavetemps_flag)
 	cli_rmdirs(dir);
 
     free(dir);
     return ret;
 }
 
3805ebcb
 static int cli_scanmschm(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
a5373b64
 {
 	const char *tmpdir;
 	char *tempname;
 	int ret = CL_CLEAN;
 
 
     cli_dbgmsg("in cli_scanmschm()\n");	
 
     if((tmpdir = getenv("TMPDIR")) == NULL)
 #ifdef P_tmpdir
 	tmpdir = P_tmpdir;
 #else
 	tmpdir = "/tmp";
 #endif
 
     tempname = cli_gentemp(tmpdir);
 
     if(mkdir(tempname, 0700)) {
2b259453
 	cli_dbgmsg("CHM: Can't create temporary directory %s\n", tempname);
a5373b64
 	return CL_ETMPDIR;
     }
 
     if(chm_unpack(desc, tempname))
 	ret = cli_scandir(tempname, virname, scanned, root, limits, options, arec, mrec);
 
     if(!cli_leavetemps_flag)
 	cli_rmdirs(tempname);
 
     free(tempname);
     return ret;
 }
 
3805ebcb
 static int cli_scanscrenc(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
e57fa318
 {
 	const char *tmpdir;
 	char *tempname;
 	int ret = CL_CLEAN;
 
     cli_dbgmsg("in cli_scanscrenc()\n");
 
     if((tmpdir = getenv("TMPDIR")) == NULL)
 #ifdef P_tmpdir
         tmpdir = P_tmpdir;
 #else
         tmpdir = "/tmp";
 #endif
                                                                                                                                
     tempname = cli_gentemp(tmpdir);
                                                                                                                                
     if(mkdir(tempname, 0700)) {
 	cli_dbgmsg("CHM: Can't create temporary directory %s\n", tempname);
 	return CL_ETMPDIR;
     }
 
     if (html_screnc_decode(desc, tempname))
 	ret = cli_scandir(tempname, virname, scanned, root, limits, options, arec, mrec);
 
     if(!cli_leavetemps_flag)
 	cli_rmdirs(tempname);
 
     free(tempname);
     return ret;
 }
015e31e1
 
3805ebcb
 static int cli_scanmail(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
e3aaff8e
 {
 	const char *tmpdir;
 	char *dir;
 	int ret;
 
 
03a2d04a
     cli_dbgmsg("Starting cli_scanmail(), mrec == %d, arec == %d\n", *mrec, *arec);
e3aaff8e
 
414abe87
     if((tmpdir = getenv("TMPDIR")) == NULL)
e3aaff8e
 #ifdef P_tmpdir
 	tmpdir = P_tmpdir;
 #else
 	tmpdir = "/tmp";
 #endif
 
 	/* generate the temporary directory */
8000d078
 	dir = cli_gentemp(tmpdir);
e3aaff8e
 	if(mkdir(dir, 0700)) {
2b259453
 	    cli_dbgmsg("Mail: Can't create temporary directory %s\n", dir);
08d6b1e3
 	    free(dir);
e3aaff8e
 	    return CL_ETMPDIR;
 	}
 
 	/*
 	 * Extract the attachments into the temporary directory
 	 */
08d6b1e3
 	if((ret = cli_mbox(dir, desc, options))) {
d16da566
 	    cli_rmdirs(dir);
08d6b1e3
 	    free(dir);
015e31e1
 	    return ret;
08d6b1e3
 	}
e3aaff8e
 
22275b15
 	ret = cli_scandir(dir, virname, scanned, root, limits, options, arec, mrec);
e3aaff8e
 
 	cli_rmdirs(dir);
 	free(dir);
 
 	return ret;
 }
 
3805ebcb
 int cli_magic_scandesc(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
e3aaff8e
 {
2b259453
 	char magic[MAGIC_BUFFER_SIZE + 1];
888f5794
 	int ret = CL_CLEAN, nret;
cdb0ae9c
 	int bread = 0;
9b8e00a0
 	cli_file_t type;
e3aaff8e
 
 
     if(!root) {
414abe87
 	cli_errmsg("CRITICAL: root == NULL\n");
e3aaff8e
 	return -1;
     }
 
d6b5ae47
     if(!options) { /* raw mode (stdin, etc.) */
b68d11d2
 	cli_dbgmsg("Raw mode: No support for special files\n");
 	if((ret = cli_scandesc(desc, virname, scanned, root, 0, 0) == CL_VIRUS))
2b259453
 	    cli_dbgmsg("%s found in descriptor %d\n", *virname, desc);
d6b5ae47
 	return ret;
     }
 
467f8b1e
     if(SCAN_ARCHIVE && limits && limits->maxreclevel)
22275b15
 	if(*arec > limits->maxreclevel) {
 	    cli_dbgmsg("Archive recursion limit exceeded (arec == %d).\n", *arec);
467f8b1e
 	    /* return CL_EMAXREC; */
 	    return CL_CLEAN;
22275b15
 	}
e3aaff8e
 
22275b15
     if(SCAN_MAIL)
 	if(*mrec > MAX_MAIL_RECURSION) {
 	    cli_dbgmsg("Mail recursion level exceeded (mrec == %d).\n", *mrec);
 	    /* return CL_EMAXREC; */
 	    return CL_CLEAN;
 	}
e3aaff8e
 
467f8b1e
     lseek(desc, 0, SEEK_SET);
2b259453
     memset(magic, 0, sizeof(magic));
e3aaff8e
 
2b259453
     if((bread = read(desc, magic, MAGIC_BUFFER_SIZE)) == -1) {
 	cli_dbgmsg("Can't read from descriptor %d\n");
 	return CL_EIO;
     } else if(bread < 2) {
 	/* short read - no need to do magic */
 	return CL_CLEAN;
467f8b1e
     }
e3aaff8e
 
2b259453
     lseek(desc, 0, SEEK_SET);
467f8b1e
     type = cli_filetype(magic, bread);
6d6e8271
 
3805ebcb
     type == CL_TYPE_MAIL ? (*mrec)++ : (*arec)++;
888f5794
 
467f8b1e
     switch(type) {
3805ebcb
 	case CL_TYPE_RAR:
467f8b1e
 	    if(!DISABLE_RAR && SCAN_ARCHIVE && !cli_scanrar_inuse)
22275b15
 		ret = cli_scanrar(desc, virname, scanned, root, limits, options, arec, mrec);
467f8b1e
 	    break;
6d6e8271
 
3805ebcb
 	case CL_TYPE_ZIP:
467f8b1e
 	    if(SCAN_ARCHIVE)
22275b15
 		ret = cli_scanzip(desc, virname, scanned, root, limits, options, arec, mrec);
467f8b1e
 	    break;
6d6e8271
 
3805ebcb
 	case CL_TYPE_GZ:
467f8b1e
 	    if(SCAN_ARCHIVE)
22275b15
 		ret = cli_scangzip(desc, virname, scanned, root, limits, options, arec, mrec);
467f8b1e
 	    break;
6d6e8271
 
3805ebcb
 	case CL_TYPE_BZ:
68a6f51f
 #ifdef HAVE_BZLIB_H
467f8b1e
 	    if(SCAN_ARCHIVE)
22275b15
 		ret = cli_scanbzip(desc, virname, scanned, root, limits, options, arec, mrec);
e3aaff8e
 #endif
467f8b1e
 	    break;
6d6e8271
 
3805ebcb
 	case CL_TYPE_MSSZDD:
341e5433
 	    if(SCAN_ARCHIVE)
3805ebcb
 		ret = cli_scanszdd(desc, virname, scanned, root, limits, options, arec, mrec);
341e5433
 	    break;
 
3805ebcb
 	case CL_TYPE_MSCAB:
414abe87
 	    if(SCAN_ARCHIVE)
22275b15
 		ret = cli_scanmscab(desc, virname, scanned, root, limits, options, arec, mrec);
414abe87
 	    break;
 
3805ebcb
 	case CL_TYPE_MAIL:
467f8b1e
 	    if(SCAN_MAIL)
22275b15
 		ret = cli_scanmail(desc, virname, scanned, root, limits, options, arec, mrec);
467f8b1e
 	    break;
6d6e8271
 
3805ebcb
 	case CL_TYPE_MSCHM:
a5373b64
 	    if(SCAN_ARCHIVE)
 		ret = cli_scanmschm(desc, virname, scanned, root, limits, options, arec, mrec);
 	    break;
 
3805ebcb
 	case CL_TYPE_MSOLE2:
467f8b1e
 	    if(SCAN_OLE2)
22275b15
 		ret = cli_scanole2(desc, virname, scanned, root, limits, options, arec, mrec);
467f8b1e
 	    break;
6d6e8271
 
3805ebcb
 	case CL_TYPE_TAR:
2b259453
 	    if(SCAN_ARCHIVE)
 		ret = cli_scantar(desc, virname, scanned, root, limits, options, arec, mrec);
 	    break;
 
3805ebcb
 	case CL_TYPE_SCRENC:
e57fa318
 	    ret = cli_scanscrenc(desc, virname, scanned, root, limits, options, arec, mrec);
 	    break;
 
3805ebcb
 	case CL_TYPE_DATA:
467f8b1e
 	    /* it could be a false positive and a standard DOS .COM file */
 	    {
 		struct stat s;
 		if(fstat(desc, &s) == 0 && S_ISREG(s.st_mode) && s.st_size < 65536)
3805ebcb
 		type = CL_TYPE_UNKNOWN_DATA;
467f8b1e
 	    }
 
3805ebcb
 	case CL_TYPE_UNKNOWN_DATA:
c3a3be2d
 	    ret = cli_check_mydoom_log(desc, virname);
467f8b1e
 	    break;
216a697f
 
 	default:
 	    break;
e3aaff8e
     }
 
3805ebcb
     type == CL_TYPE_MAIL ? (*mrec)-- : (*arec)--;
467f8b1e
 
3805ebcb
     if(type != CL_TYPE_DATA && ret != CL_VIRUS) { /* scan the raw file */
77e4bb11
 	    int typerec;
888f5794
 
3805ebcb
 	type == CL_TYPE_UNKNOWN_TEXT ? (typerec = 1) : (typerec = 0);
77e4bb11
 	lseek(desc, 0, SEEK_SET);
22275b15
 
b68d11d2
 	if((nret = cli_scandesc(desc, virname, scanned, root, typerec, type)) == CL_VIRUS) {
2b259453
 	    cli_dbgmsg("%s found in descriptor %d.\n", *virname, desc);
e3aaff8e
 	    return CL_VIRUS;
888f5794
 
 	} else if(nret >= CL_TYPENO) {
 	    lseek(desc, 0, SEEK_SET);
 
 	    switch(nret) {
3805ebcb
 		case CL_TYPE_HTML:
888f5794
 		    if(SCAN_HTML)
22275b15
 			if(cli_scanhtml(desc, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS)
888f5794
 			    return CL_VIRUS;
 		    break;
ece009c0
 
3805ebcb
 		case CL_TYPE_MAIL:
ece009c0
 		    if(SCAN_MAIL)
22275b15
 			if(cli_scanmail(desc, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS)
ece009c0
 			    return CL_VIRUS;
 		    break;
888f5794
 	    }
e3aaff8e
 	}
46c2e927
     }
e3aaff8e
 
77e4bb11
     (*arec)++;
     lseek(desc, 0, SEEK_SET);
     switch(type) {
 	/* Due to performance reasons all executables were first scanned
 	 * in raw mode. Now we will try to unpack them
 	 */
3805ebcb
 	case CL_TYPE_MSEXE:
77e4bb11
 	    if(SCAN_PE)
 		ret = cli_scanpe(desc, virname, scanned, root, limits, options, arec, mrec);
 	    break;
8000d078
 
 	default:
 	    break;
77e4bb11
     }
     (*arec)--;
 
 
e3aaff8e
     return ret;
 }
 
3805ebcb
 int cl_scandesc(int desc, const char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options)
e3aaff8e
 {
22275b15
 	int arec = 0, mrec = 0;
e3aaff8e
 
22275b15
     return cli_magic_scandesc(desc, virname, scanned, root, limits, options, &arec, &mrec);
e3aaff8e
 }
 
3805ebcb
 static int cli_scanfile(const char *filename, const char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
21cf4aeb
 {
 	int fd, ret;
 
a9082ea2
 
22275b15
     /* internal version of cl_scanfile with arec/mrec preserved */
21cf4aeb
     if((fd = open(filename, O_RDONLY)) == -1)
 	return CL_EOPEN;
 
22275b15
     ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, arec, mrec);
21cf4aeb
 
     close(fd);
     return ret;
 }
 
3805ebcb
 int cl_scanfile(const char *filename, const char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options)
e3aaff8e
 {
 	int fd, ret;
 
ece009c0
 
e3aaff8e
     if((fd = open(filename, O_RDONLY)) == -1)
 	return CL_EOPEN;
 
     ret = cl_scandesc(fd, virname, scanned, root, limits, options);
     close(fd);
 
     return ret;
 }