libclamav/scanners.c
b151ef55
 /*
5918723d
  *  Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@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 <string.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
255e314c
 #include <unistd.h>
771e8818
 #include <sys/param.h>
b151ef55
 #include <fcntl.h>
 #include <dirent.h>
2148d690
 #include <netinet/in.h>
 
b151ef55
 
2fe19b26
 #if HAVE_MMAP
 #if HAVE_SYS_MMAN_H
 #include <sys/mman.h>
 #else /* HAVE_SYS_MMAN_H */
 #undef HAVE_MMAP
 #endif
 #endif
 
1ed6a845
 #include <mspack.h>
 
b151ef55
 #ifdef CL_THREAD_SAFE
 #  include <pthread.h>
 pthread_mutex_t cli_scanrar_mutex = PTHREAD_MUTEX_INITIALIZER;
 #endif
8b242bb9
 int cli_scanrar_inuse = 0;
b151ef55
 
1ed6a845
 extern short cli_leavetemps_flag;
 
5431ebba
 extern int cli_mbox(const char *dir, int desc, unsigned int options); /* FIXME */
f91f55e0
 
b151ef55
 #include "clamav.h"
 #include "others.h"
5ede41fa
 #include "scanners.h"
f91f55e0
 #include "matcher-ac.h"
 #include "matcher-bm.h"
b151ef55
 #include "matcher.h"
 #include "unrarlib.h"
c561d2a3
 #include "ole2_extract.h"
 #include "vba_extract.h"
e2dc6ace
 #include "msexpand.h"
4ad876ad
 #include "chmunpack.h"
c2484690
 #include "pe.h"
2fe19b26
 #include "filetypes.h"
 #include "htmlnorm.h"
26c6ace2
 #include "untar.h"
958dc41c
 #include "special.h"
f58ad3be
 #include "binhex.h"
b151ef55
 
 #ifdef HAVE_ZLIB_H
 #include <zlib.h>
 #include <zzip.h>
 #endif
 
 #ifdef HAVE_BZLIB_H
 #include <bzlib.h>
 #endif
 
a45ec9cc
 #if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
 #include <limits.h>
8c7e16b8
 #include <stddef.h>
a45ec9cc
 #endif
 
771e8818
 /* Maximum filenames under various systems - njh */
 #ifndef	NAME_MAX	/* e.g. Linux */
 # ifdef	MAXNAMELEN	/* e.g. Solaris */
 #   define	NAME_MAX	MAXNAMELEN
 # else
 #   ifdef	FILENAME_MAX	/* e.g. SCO */
 #     define	NAME_MAX	FILENAME_MAX
8425b1a6
 #   else
 #     define	NAME_MAX	256
771e8818
 #   endif
 # endif
 #endif
a45ec9cc
 
06d4e856
 #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)
f852d214
 #define DETECT_ENCRYPTED    (options & CL_SCAN_BLOCKENCRYPTED)
06d4e856
 #define BLOCKMAX	    (options & CL_SCAN_BLOCKMAX)
8b242bb9
 
5115a5eb
 #define MAX_MAIL_RECURSION  15
b151ef55
 
a8b53539
 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, unsigned int arec, unsigned int mrec);
b151ef55
 
ff8cb48b
 
b151ef55
 #ifdef CL_THREAD_SAFE
41b894c7
 static void cli_unlock_mutex(void *mtx)
b151ef55
 {
     cli_dbgmsg("Pthread cancelled. Unlocking mutex.\n");
     pthread_mutex_unlock(mtx);
 }
 #endif
 
a8b53539
 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, unsigned int arec, unsigned int mrec)
b151ef55
 {
97de3c9d
 	FILE *tmp = NULL;
a8b53539
 	int fd, ret = CL_CLEAN;
 	unsigned int files = 0, encrypted, afiles;
b151ef55
 	ArchiveList_struct *rarlist = NULL;
41b894c7
 	ArchiveList_struct *rarlist_head = NULL;
b151ef55
 	char *rar_data_ptr;
 	unsigned long rar_data_size;
92152740
 	struct cli_meta_node *mdata;
b151ef55
 
26c6ace2
 
     cli_dbgmsg("in scanrar()\n");
b151ef55
 
 #ifdef CL_THREAD_SAFE
     pthread_cleanup_push(cli_unlock_mutex, &cli_scanrar_mutex);
     pthread_mutex_lock(&cli_scanrar_mutex);
     cli_scanrar_inuse = 1;
 #endif
 
92152740
     if(!(afiles = urarlib_list(desc, (ArchiveList_struct *) &rarlist))) {
b151ef55
 #ifdef CL_THREAD_SAFE
 	pthread_mutex_unlock(&cli_scanrar_mutex);
 	cli_scanrar_inuse = 0;
 #endif
 	return CL_ERAR;
     }
 
26c6ace2
     cli_dbgmsg("RAR: Number of archived files: %d\n", afiles);
1d1ffc91
 
41b894c7
     rarlist_head = rarlist;
 
b151ef55
     while(rarlist) {
92152740
 
 	files++;
 	encrypted = rarlist->item.Flags & 0x04;
 
a8b53539
 	cli_dbgmsg("RAR: %s, crc32: 0x%x, encrypted: %d, compressed: %u, normal: %u, method: %d, ratio: %d (max: %d)\n", rarlist->item.Name, rarlist->item.FileCRC, encrypted, rarlist->item.PackSize, rarlist->item.UnpSize, rarlist->item.Method, rarlist->item.PackSize ? ((unsigned int) rarlist->item.UnpSize / (unsigned int) rarlist->item.PackSize) : 0, limits ? limits->maxratio : 0);
92152740
 
 	/* Scan metadata */
 	mdata = root->rar_mlist;
 	if(mdata) do {
 	    if(mdata->encrypted != encrypted)
 		continue;
 
a8b53539
 	    if(mdata->crc32 && (unsigned int) mdata->crc32 != rarlist->item.FileCRC)
92152740
 		continue;
 
a8b53539
 	    if(mdata->csize > 0 && (unsigned int) mdata->csize != rarlist->item.PackSize)
92152740
 		continue;
 
a8b53539
 	    if(mdata->size >= 0 && (unsigned int) mdata->size != rarlist->item.UnpSize)
92152740
 		continue;
 
e483f51a
 	    if(mdata->method >= 0 && mdata->method != rarlist->item.Method)
92152740
 		continue;
 
 	    if(mdata->fileno && mdata->fileno != files)
 		continue;
 
a8b53539
 	    if(mdata->maxdepth && arec > mdata->maxdepth)
92152740
 		continue;
 
 	    /* TODO add support for regex */
 	    /*if(mdata->filename && !strstr(zdirent.d_name, mdata->filename))*/
 	    if(mdata->filename && strcmp(rarlist->item.Name, mdata->filename))
 		continue;
 
 	    break; /* matched */
 
 	} while((mdata = mdata->next));
 
 	if(mdata) {
 	    *virname = mdata->virname;
 	    ret = CL_VIRUS;
 	    break;
 	}
 
3f6e5a35
 	if(DETECT_ENCRYPTED && (rarlist->item.Flags & 0x04)) {
26c6ace2
 	    cli_dbgmsg("RAR: Encrypted files found in archive.\n");
3cb43aaa
 	    lseek(desc, 0, SEEK_SET);
a5db7190
 	    ret = cli_scandesc(desc, virname, scanned, root, 0, 0);
 	    if(ret < 0) {
 		break;
 	    } else if(ret != CL_VIRUS) {
3cb43aaa
 		*virname = "Encrypted.RAR";
a5db7190
 		ret = CL_VIRUS;
 	    }
96a39a27
 	    break;
 	}
b151ef55
 
3f6e5a35
 	if((rarlist->item.Flags & 0x03) != 0) {
 	    cli_dbgmsg("RAR: Skipping %s (splitted)\n", rarlist->item.Name);
 	    rarlist = rarlist->next;
 	    continue;
 	}
 
b151ef55
 	if(limits) {
728f8802
 
 	    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;
 		}
 	    }
 
896e9af1
 	    if(limits->maxfilesize && (rarlist->item.UnpSize > (unsigned int) limits->maxfilesize)) {
26c6ace2
 		cli_dbgmsg("RAR: %s: Size exceeded (%u, max: %lu)\n", rarlist->item.Name, (unsigned int) rarlist->item.UnpSize, limits->maxfilesize);
b151ef55
 		rarlist = rarlist->next;
728f8802
 		if(BLOCKMAX) {
 		    *virname = "RAR.ExceededFileSize";
 		    ret = CL_VIRUS;
 		    break;
 		}
b151ef55
 		continue;
 	    }
 
 	    if(limits->maxfiles && (files > limits->maxfiles)) {
 		cli_dbgmsg("RAR: Files limit reached (max: %d)\n", limits->maxfiles);
728f8802
 		if(BLOCKMAX) {
 		    *virname = "RAR.ExceededFilesLimit";
 		    ret = CL_VIRUS;
 		    break;
 		}
b151ef55
 		break;
 	    }
 	}
 
7f01d774
         if(rarlist->item.FileAttr & RAR_FENTRY_ATTR_DIRECTORY) {
41b894c7
             rarlist = rarlist->next;
             continue;
         }
 
b151ef55
 	if((tmp = tmpfile()) == NULL) {
26c6ace2
 	    cli_dbgmsg("RAR: Can't generate temporary file.\n");
b151ef55
 #ifdef CL_THREAD_SAFE
 	    pthread_mutex_unlock(&cli_scanrar_mutex);
 	    cli_scanrar_inuse = 0;
 #endif
 	    return CL_ETMPFILE;
 	}
 	fd = fileno(tmp);
 
41b894c7
 	if( urarlib_get(&rar_data_ptr, &rar_data_size, rarlist->item.Name, desc, "clam")) {
26c6ace2
 	    cli_dbgmsg("RAR: Extracted: %s, size: %lu\n", rarlist->item.Name, rar_data_size);
41b894c7
 	    if(fwrite(rar_data_ptr, 1, rar_data_size, tmp) != rar_data_size) {
26c6ace2
 		cli_dbgmsg("RAR: Can't write to file.\n");
d3d2fb1e
 		fclose(tmp);
b151ef55
 		tmp = NULL;
 		ret = CL_ERAR;
d3d2fb1e
 		if(rar_data_ptr) {
b151ef55
 		    free(rar_data_ptr);
d3d2fb1e
 		    rar_data_ptr = NULL;
 		}
60286ab1
 		break;
b151ef55
 	    }
 
d3d2fb1e
 	    if(rar_data_ptr) {
b151ef55
 		free(rar_data_ptr);
d3d2fb1e
 		rar_data_ptr = NULL;
 	    }
 	    if(fflush(tmp) != 0) {
26c6ace2
 		cli_dbgmsg("RAR: fflush() failed: %s\n", strerror(errno));
d3d2fb1e
 		fclose(tmp);
41b894c7
 		urarlib_freelist(rarlist_head);
b151ef55
 #ifdef CL_THREAD_SAFE
 		pthread_mutex_unlock(&cli_scanrar_mutex);
 		cli_scanrar_inuse = 0;
 #endif
 		return CL_EFSYNC;
 	    }
 
 	    lseek(fd, 0, SEEK_SET);
0da7859b
 	    if((ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, arec, mrec)) == CL_VIRUS ) {
26c6ace2
 		cli_dbgmsg("RAR: Infected with %s\n", *virname);
8b242bb9
 
d3d2fb1e
 		fclose(tmp);
b151ef55
 		urarlib_freelist(rarlist);
 #ifdef CL_THREAD_SAFE
 		pthread_mutex_unlock(&cli_scanrar_mutex);
 		cli_scanrar_inuse = 0;
 #endif
41b894c7
   		return ret;
b151ef55
 	    }
 
 	} else {
26c6ace2
 	    cli_dbgmsg("RAR: Can't decompress file %s\n", rarlist->item.Name);
d3d2fb1e
 	    fclose(tmp);
2c7d6b9a
 	    tmp = NULL;
d3d2fb1e
 	    ret = CL_ERAR; /* WinRAR 3.0 ? */
 	    break;
b151ef55
 	}
 
d3d2fb1e
 	fclose(tmp);
b151ef55
 	tmp = NULL;
 	rarlist = rarlist->next;
     }
 
41b894c7
     urarlib_freelist(rarlist_head);
b151ef55
 #ifdef CL_THREAD_SAFE
     pthread_mutex_unlock(&cli_scanrar_mutex);
     cli_scanrar_inuse = 0;
     pthread_cleanup_pop(0);
 #endif
2c7d6b9a
     
26c6ace2
     cli_dbgmsg("RAR: Exit code: %d\n", ret);
2c7d6b9a
 
b151ef55
     return ret;
 }
 
 #ifdef HAVE_ZLIB_H
a8b53539
 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, unsigned int arec, unsigned int mrec)
b151ef55
 {
 	ZZIP_DIR *zdir;
 	ZZIP_DIRENT zdirent;
 	ZZIP_FILE *zfp;
97de3c9d
 	FILE *tmp = NULL;
7b7b3ca5
 	char *buff;
a8b53539
 	int fd, bytes, ret = CL_CLEAN;
d2f07436
 	unsigned long int size = 0;
a8b53539
 	unsigned int files = 0, encrypted;
97de3c9d
 	struct stat source;
92152740
 	struct cli_meta_node *mdata;
50099661
 	zzip_error_t err;
b151ef55
 
26c6ace2
 
     cli_dbgmsg("in scanzip()\n");
b151ef55
 
c6259ac5
     if((zdir = zzip_dir_fdopen(dup(desc), &err)) == NULL) {
26c6ace2
 	cli_dbgmsg("Zip: Not supported file format ?.\n");
 	cli_dbgmsg("Zip: zzip_dir_fdopen() return code: %d\n", err);
10971328
 	/* no return with CL_EZIP due to password protected zips */
 	return CL_CLEAN;
b151ef55
     }
 
97de3c9d
     fstat(desc, &source);
 
f5cd5991
     if(!(buff = (char *) cli_malloc(FILEBUFF))) {
26c6ace2
 	cli_dbgmsg("Zip: unable to malloc(%d)\n", FILEBUFF);
f5cd5991
 	zzip_dir_close(zdir);
 	return CL_EMEM;
     }
 
b151ef55
     while(zzip_dir_read(zdir, &zdirent)) {
eceef468
 	files++;
97de3c9d
 
 	if(!zdirent.d_name || !strlen(zdirent.d_name)) { /* Mimail fix */
26c6ace2
 	    cli_dbgmsg("Zip: strlen(zdirent.d_name) == %d\n", strlen(zdirent.d_name));
c53b8b0f
 	    *virname = "Suspect.Zip";
97de3c9d
 	    ret = CL_VIRUS;
 	    break;
 	}
 
a02f38dd
         /* Bit 0: file is encrypted
 	 * Bit 6: Strong encryption was used
 	 * Bit 13: Encrypted central directory
 	 */
 	encrypted = (zdirent.d_flags & 0x2041 != 0);
34e96745
 
a8b53539
 	cli_dbgmsg("Zip: %s, crc32: 0x%x, encrypted: %d, compressed: %u, normal: %u, method: %d, ratio: %d (max: %d)\n", zdirent.d_name, zdirent.d_crc32, encrypted, zdirent.d_csize, zdirent.st_size, zdirent.d_compr, zdirent.d_csize ? (zdirent.st_size / zdirent.d_csize) : 0, limits ? limits->maxratio : 0);
97de3c9d
 
9bdb71d0
 	if(!zdirent.st_size) {
70567086
 	    if(zdirent.d_crc32) {
 		cli_dbgmsg("Zip: Broken file or modified information in local header part of archive\n");
c53b8b0f
 		*virname = "Exploit.Zip.ModifiedHeaders";
70567086
 		ret = CL_VIRUS;
 		break;
 	    }
b151ef55
 	    continue;
 	}
 
34e96745
 	/* Scan metadata */
 	mdata = root->zip_mlist;
54f56eaf
 	if(mdata) do {
34e96745
 	    if(mdata->encrypted != encrypted)
 		continue;
 
a8b53539
 	    if(mdata->crc32 && mdata->crc32 != (unsigned int) zdirent.d_crc32)
34e96745
 		continue;
 
 	    if(mdata->csize > 0 && mdata->csize != zdirent.d_csize)
 		continue;
 
 	    if(mdata->size >= 0 && mdata->size != zdirent.st_size)
 		continue;
 
e483f51a
 	    if(mdata->method >= 0 && mdata->method != (unsigned int) zdirent.d_compr)
34e96745
 		continue;
 
eceef468
 	    if(mdata->fileno && mdata->fileno != files)
 		continue;
 
a8b53539
 	    if(mdata->maxdepth && arec > mdata->maxdepth)
eceef468
 		continue;
 
 	    /* TODO add support for regex */
34e96745
 	    /*if(mdata->filename && !strstr(zdirent.d_name, mdata->filename))*/
 	    if(mdata->filename && strcmp(zdirent.d_name, mdata->filename))
 		continue;
 
 	    break; /* matched */
 
 	} while((mdata = mdata->next));
 
 	if(mdata) {
 	    *virname = mdata->virname;
 	    ret = CL_VIRUS;
 	    break;
 	}
 
66ae09c6
 	/* 
 	 * Workaround for archives created with ICEOWS.
 	 * ZZIP_DIRENT does not contain information on file type
 	 * so we try to determine a directory via a filename
 	 */
 	if(zdirent.d_name[strlen(zdirent.d_name) - 1] == '/') {
 	    cli_dbgmsg("Zip: Directory entry with st_size != 0\n");
 	    continue;
 	}
 
b151ef55
 	/* work-around for problematic zips (zziplib crashes with them) */
b44f4315
 	if(zdirent.d_csize <= 0 || zdirent.st_size < 0) {
26c6ace2
 	    cli_dbgmsg("Zip: Malformed archive detected.\n");
c53b8b0f
 	    *virname = "Suspect.Zip";
97de3c9d
 	    ret = CL_VIRUS;
b151ef55
 	    break;
 	}
 
b44f4315
 	if(limits && limits->maxratio > 0 && ((unsigned) zdirent.st_size / (unsigned) zdirent.d_csize) >= limits->maxratio) {
 	    *virname = "Oversized.Zip";
 	    ret = CL_VIRUS;
 	    break;
         }
 
34e96745
 	if(DETECT_ENCRYPTED && encrypted) {
26c6ace2
 	    cli_dbgmsg("Zip: Encrypted files found in archive.\n");
3cb43aaa
 	    lseek(desc, 0, SEEK_SET);
a5db7190
 	    ret = cli_scandesc(desc, virname, scanned, root, 0, 0);
 	    if(ret < 0) {
 		break;
 	    } else if(ret != CL_VIRUS) {
3cb43aaa
 		*virname = "Encrypted.Zip";
a5db7190
 		ret = CL_VIRUS;
 	    }
510c466b
 	    break;
 	}
 
b151ef55
 	if(limits) {
a8b53539
 	    if(limits->maxfilesize && ((unsigned int) zdirent.st_size > limits->maxfilesize)) {
26c6ace2
 		cli_dbgmsg("Zip: %s: Size exceeded (%d, max: %ld)\n", zdirent.d_name, zdirent.st_size, limits->maxfilesize);
ae1f747c
 		/* ret = CL_EMAXSIZE; */
728f8802
 		if(BLOCKMAX) {
 		    *virname = "Zip.ExceededFileSize";
 		    ret = CL_VIRUS;
 		    break;
 		}
 		continue; /* continue scanning */
b151ef55
 	    }
 
 	    if(limits->maxfiles && (files > limits->maxfiles)) {
 		cli_dbgmsg("Zip: Files limit reached (max: %d)\n", limits->maxfiles);
728f8802
 		if(BLOCKMAX) {
 		    *virname = "Zip.ExceededFilesLimit";
 		    ret = CL_VIRUS;
 		    break;
 		}
b151ef55
 		break;
 	    }
 	}
 
e4819192
 	if((zfp = zzip_file_open(zdir, zdirent.d_name, 0)) == NULL) {
 	    cli_dbgmsg("Zip: Can't open file %s\n", zdirent.d_name);
 	    ret = CL_EZIP;
 	    break;
 	}
 
b151ef55
 	/* generate temporary file and get its descriptor */
 	if((tmp = tmpfile()) == NULL) {
26c6ace2
 	    cli_dbgmsg("Zip: Can't generate tmpfile().\n");
8f6e5532
 	    zzip_file_close(zfp);
97de3c9d
 	    ret = CL_ETMPFILE;
 	    break;
b151ef55
 	}
 
d2f07436
 	size = 0;
7b7b3ca5
 	while((bytes = zzip_file_read(zfp, buff, FILEBUFF)) > 0) {
d2f07436
 	    size += bytes;
a19f21b6
 	    if(fwrite(buff, 1, bytes, tmp) != (size_t) bytes) {
26c6ace2
 		cli_dbgmsg("Zip: Can't write to file.\n");
b151ef55
 		zzip_file_close(zfp);
f5cd5991
 		zzip_dir_close(zdir);
 		fclose(tmp);
7b7b3ca5
 		free(buff);
e4819192
 		return CL_EIO;
b151ef55
 	    }
 	}
 
 	zzip_file_close(zfp);
 
e483f51a
 	if(!encrypted && size != zdirent.st_size) {
d2f07436
 	    cli_dbgmsg("Zip: Incorrectly decompressed (%d != %d)\n", size, zdirent.st_size);
e483f51a
 	    ret = CL_EZIP;
d2f07436
 	    break;
 	}
 
d3d2fb1e
 	if(fflush(tmp) != 0) {
26c6ace2
 	    cli_dbgmsg("Zip: fflush() failed: %s\n", strerror(errno));
97de3c9d
 	    ret = CL_EFSYNC;
 	    break;
b151ef55
 	}
 
d3d2fb1e
 	fd = fileno(tmp);
 
b151ef55
 	lseek(fd, 0, SEEK_SET);
0da7859b
 	if((ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, arec, mrec)) == CL_VIRUS ) {
26c6ace2
 	    cli_dbgmsg("Zip: Infected with %s\n", *virname);
b151ef55
 	    ret = CL_VIRUS;
 	    break;
 	} else if(ret == CL_EMALFZIP) {
26c6ace2
 	    cli_dbgmsg("Zip: Malformed Zip file, scanning stopped.\n");
c53b8b0f
 	    *virname = "Suspect.Zip";
b151ef55
 	    ret = CL_VIRUS;
 	    break;
 	}
 
d3d2fb1e
 	if (tmp) {
 	    fclose(tmp);
 	    tmp = NULL;
 	}
b151ef55
     }
 
     zzip_dir_close(zdir);
d3d2fb1e
     if (tmp) {
 	fclose(tmp);
 	tmp = NULL;
     }
f5cd5991
 
     free(buff);
b151ef55
     return ret;
 }
 
a8b53539
 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, unsigned int arec, unsigned int mrec)
b151ef55
 {
 	int fd, bytes, ret = CL_CLEAN;
a8b53539
 	unsigned long int size = 0;
7b7b3ca5
 	char *buff;
97de3c9d
 	FILE *tmp = NULL;
b151ef55
 	gzFile gd;
 
 
97de3c9d
     cli_dbgmsg("in cli_scangzip()\n");
 
b151ef55
     if((gd = gzdopen(dup(desc), "rb")) == NULL) {
26c6ace2
 	cli_dbgmsg("GZip: Can't open descriptor %d\n", desc);
b151ef55
 	return CL_EGZIP;
     }
 
     if((tmp = tmpfile()) == NULL) {
26c6ace2
 	cli_dbgmsg("GZip: Can't generate temporary file.\n");
b151ef55
 	gzclose(gd);
 	return CL_ETMPFILE;
     }
     fd = fileno(tmp);
 
97de3c9d
     if(!(buff = (char *) cli_malloc(FILEBUFF))) {
26c6ace2
 	cli_dbgmsg("GZip: Unable to malloc %d bytes.\n", FILEBUFF);
97de3c9d
 	gzclose(gd);
7b7b3ca5
 	return CL_EMEM;
97de3c9d
     }
7b7b3ca5
 
97de3c9d
     while((bytes = gzread(gd, buff, FILEBUFF)) > 0) {
b151ef55
 	size += bytes;
 
 	if(limits)
97de3c9d
 	    if(limits->maxfilesize && (size + FILEBUFF > limits->maxfilesize)) {
26c6ace2
 		cli_dbgmsg("GZip: Size exceeded (stopped at %ld, max: %ld)\n", size, limits->maxfilesize);
9c8806fb
 		if(BLOCKMAX) {
 		    *virname = "GZip.ExceededFileSize";
 		    ret = CL_VIRUS;
 		}
b151ef55
 		break;
 	    }
 
3fc8c606
 	if(cli_writen(fd, buff, bytes) != bytes) {
26c6ace2
 	    cli_dbgmsg("GZip: Can't write to file.\n");
d3d2fb1e
 	    fclose(tmp);
b151ef55
 	    gzclose(gd);
7b7b3ca5
 	    free(buff);
b151ef55
 	    return CL_EGZIP;
 	}
     }
 
7b7b3ca5
     free(buff);
b151ef55
     gzclose(gd);
9c8806fb
 
     if(ret == CL_VIRUS) {
 	fclose(tmp);
 	return ret;
     }
 
b151ef55
     if(fsync(fd) == -1) {
26c6ace2
 	cli_dbgmsg("GZip: Can't synchronise descriptor %d\n", fd);
d3d2fb1e
 	fclose(tmp);
b151ef55
 	return CL_EFSYNC;
     }
 
     lseek(fd, 0, SEEK_SET);
0da7859b
     if((ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, arec, mrec)) == CL_VIRUS ) {
26c6ace2
 	cli_dbgmsg("GZip: Infected with %s\n", *virname);
d3d2fb1e
 	fclose(tmp);
b151ef55
 	return CL_VIRUS;
     }
d3d2fb1e
     fclose(tmp);
b151ef55
 
     return ret;
 }
 #endif
 
 #ifdef HAVE_BZLIB_H
 
 #ifdef NOBZ2PREFIX
 #define BZ2_bzReadOpen bzReadOpen
 #define BZ2_bzReadClose bzReadClose
 #define BZ2_bzRead bzRead
 #endif
 
a8b53539
 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, unsigned int arec, unsigned int mrec)
b151ef55
 {
 	int fd, bytes, ret = CL_CLEAN, bzerror = 0;
 	short memlim = 0;
a8b53539
 	unsigned long int size = 0;
7b7b3ca5
 	char *buff;
97de3c9d
 	FILE *fs, *tmp = NULL;
b151ef55
 	BZFILE *bfd;
 
 
9c1c9007
     if((fs = fdopen(dup(desc), "rb")) == NULL) {
26c6ace2
 	cli_dbgmsg("Bzip: Can't open descriptor %d.\n", desc);
b151ef55
 	return CL_EBZIP;
     }
 
     if(limits)
 	if(limits->archivememlim)
 	    memlim = 1;
 
61ff3bda
     if((bfd = BZ2_bzReadOpen(&bzerror, fs, 0, memlim, NULL, 0)) == NULL) {
26c6ace2
 	cli_dbgmsg("Bzip: Can't initialize bzip2 library (descriptor: %d).\n", desc);
9c1c9007
 	fclose(fs);
b151ef55
 	return CL_EBZIP;
     }
 
     if((tmp = tmpfile()) == NULL) {
26c6ace2
 	cli_dbgmsg("Bzip: Can't generate temporary file.\n");
b151ef55
 	BZ2_bzReadClose(&bzerror, bfd);
9c1c9007
 	fclose(fs);
b151ef55
 	return CL_ETMPFILE;
     }
     fd = fileno(tmp);
 
9c1c9007
     if(!(buff = (char *) malloc(FILEBUFF))) {
26c6ace2
 	cli_dbgmsg("Bzip: Unable to malloc %d bytes.\n", FILEBUFF);
9c1c9007
 	fclose(tmp);
 	fclose(fs);
 	BZ2_bzReadClose(&bzerror, bfd);
7b7b3ca5
 	return CL_EMEM;
9c1c9007
     }
7b7b3ca5
 
97de3c9d
     while((bytes = BZ2_bzRead(&bzerror, bfd, buff, FILEBUFF)) > 0) {
b151ef55
 	size += bytes;
 
 	if(limits)
97de3c9d
 	    if(limits->maxfilesize && (size + FILEBUFF > limits->maxfilesize)) {
26c6ace2
 		cli_dbgmsg("Bzip: Size exceeded (stopped at %ld, max: %ld)\n", size, limits->maxfilesize);
9c8806fb
 		if(BLOCKMAX) {
 		    *virname = "BZip.ExceededFileSize";
 		    ret = CL_VIRUS;
 		}
b151ef55
 		break;
 	    }
 
3fc8c606
 	if(cli_writen(fd, buff, bytes) != bytes) {
26c6ace2
 	    cli_dbgmsg("Bzip: Can't write to file.\n");
b151ef55
 	    BZ2_bzReadClose(&bzerror, bfd);
d3d2fb1e
 	    fclose(tmp);
7b7b3ca5
 	    free(buff);
9c1c9007
 	    fclose(fs);
b151ef55
 	    return CL_EGZIP;
 	}
     }
 
7b7b3ca5
     free(buff);
b151ef55
     BZ2_bzReadClose(&bzerror, bfd);
9c8806fb
 
     if(ret == CL_VIRUS) {
 	fclose(tmp);
 	fclose(fs);
 	return ret;
     }
 
b151ef55
     if(fsync(fd) == -1) {
26c6ace2
 	cli_dbgmsg("Bzip: Synchronisation failed for descriptor %d\n", fd);
d3d2fb1e
 	fclose(tmp);
9c1c9007
 	fclose(fs);
b151ef55
 	return CL_EFSYNC;
     }
 
     lseek(fd, 0, SEEK_SET);
0da7859b
     if((ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, arec, mrec)) == CL_VIRUS ) {
26c6ace2
 	cli_dbgmsg("Bzip: Infected with %s\n", *virname);
b151ef55
     }
d3d2fb1e
     fclose(tmp);
9c1c9007
     fclose(fs);
b151ef55
 
     return ret;
 }
 #endif
 
a8b53539
 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, unsigned int arec, unsigned int mrec)
e2dc6ace
 {
 	int fd, ret = CL_CLEAN;
 	FILE *tmp = NULL, *in;
 
26c6ace2
 
e2dc6ace
     cli_dbgmsg("in cli_scanmscomp()\n");
 
     if((in = fdopen(dup(desc), "rb")) == NULL) {
26c6ace2
 	cli_dbgmsg("SZDD: Can't open descriptor %d\n", desc);
e2dc6ace
 	return CL_EMSCOMP;
     }
 
     if((tmp = tmpfile()) == NULL) {
26c6ace2
 	cli_dbgmsg("SZDD: Can't generate temporary file.\n");
e2dc6ace
 	fclose(in);
 	return CL_ETMPFILE;
     }
 
     if(cli_msexpand(in, tmp) == -1) {
26c6ace2
 	cli_dbgmsg("SZDD: msexpand failed.\n");
e2dc6ace
 	return CL_EMSCOMP;
     }
 
     fclose(in);
     if(fflush(tmp)) {
26c6ace2
 	cli_dbgmsg("SZDD: fflush() failed.\n");
e2dc6ace
 	fclose(tmp);
 	return CL_EFSYNC;
     }
 
     fd = fileno(tmp);
     lseek(fd, 0, SEEK_SET);
0da7859b
     if((ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, arec, mrec)) == CL_VIRUS) {
26c6ace2
 	cli_dbgmsg("SZDD: Infected with %s\n", *virname);
e2dc6ace
 	fclose(tmp);
 	return CL_VIRUS;
     }
 
     fclose(tmp);
     return ret;
 }
 
a8b53539
 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, unsigned int arec, unsigned int mrec)
1ed6a845
 {
 	struct mscab_decompressor *cabd = NULL;
 	struct mscabd_cabinet *base, *cab;
 	struct mscabd_file *file;
 	char *tempname;
 	int ret = CL_CLEAN;
 
 
     cli_dbgmsg("in cli_scanmscab()\n");
 
     if((cabd = mspack_create_cab_decompressor(NULL)) == NULL) {
26c6ace2
 	cli_dbgmsg("MSCAB: Can't create libmspack CAB decompressor\n");
1ed6a845
 	return CL_EMSCAB;
     }
 
9bdb71d0
     if((base = cabd->dsearch(cabd, dup(desc))) == NULL) {
26c6ace2
 	cli_dbgmsg("MSCAB: I/O error or no valid cabinets found\n");
1ed6a845
 	mspack_destroy_cab_decompressor(cabd);
 	return CL_EMSCAB;
     }
 
     for(cab = base; cab; cab = cab->next) {
 	for(file = cab->files; file; file = file->next) {
421d947f
 
 	    if(limits && limits->maxfilesize && (file->length > (unsigned int) limits->maxfilesize)) {
 		cli_dbgmsg("MSCAB: %s: Size exceeded (%u, max: %lu)\n", file->filename, file->length, limits->maxfilesize);
 		if(BLOCKMAX) {
 		    *virname = "MSCAB.ExceededFileSize";
 		    cabd->close(cabd, base);
 		    mspack_destroy_cab_decompressor(cabd);
 		    return CL_VIRUS;
 		}
 		continue;
 	    }
 
e5fa5bab
 	    tempname = cli_gentemp(NULL);
26c6ace2
 	    cli_dbgmsg("MSCAB: Extracting data to %s\n", tempname);
1ed6a845
 	    if(cabd->extract(cabd, file, tempname)) {
26c6ace2
 		cli_dbgmsg("MSCAB: libmscab error code: %d\n", cabd->last_error(cabd));
1ed6a845
 	    } else {
0da7859b
 		ret = cli_scanfile(tempname, virname, scanned, root, limits, options, arec, mrec);
1ed6a845
 	    }
 	    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;
 }
 
a8b53539
 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, unsigned int arec, unsigned int mrec)
c561d2a3
 {
a19f21b6
 	DIR *dd;
 	struct dirent *dent;
ec748835
 #if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
8c7e16b8
 	union {
 	    struct dirent d;
 	    char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
 	} result;
a45ec9cc
 #endif
a19f21b6
 	struct stat statbuf;
 	char *fname;
c561d2a3
 
 
a19f21b6
     if((dd = opendir(dirname)) != NULL) {
ec748835
 #ifdef HAVE_READDIR_R_3
8c7e16b8
 	while(!readdir_r(dd, &result.d, &dent) && dent) {
ec748835
 #elif defined(HAVE_READDIR_R_2)
8c7e16b8
 	while((dent = (struct dirent *) readdir_r(dd, &result.d))) {
ec748835
 #else
a19f21b6
 	while((dent = readdir(dd))) {
ec748835
 #endif
618a038b
 #ifndef C_INTERIX
 	    if(dent->d_ino)
 #endif
 	    {
a19f21b6
 		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);
c561d2a3
 
a19f21b6
 		    /* stat the file */
 		    if(lstat(fname, &statbuf) != -1) {
 			if(S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) {
0da7859b
 			    if (cli_scandir(fname, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) {
a19f21b6
 				free(fname);
 				closedir(dd);
 				return CL_VIRUS;
 			    }
 			} else
 			    if(S_ISREG(statbuf.st_mode))
0da7859b
 				if(cli_scanfile(fname, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) {
a19f21b6
 				    free(fname);
 				    closedir(dd);
 				    return CL_VIRUS;
 				}
c561d2a3
 
3ccd73ad
 		    }
a19f21b6
 		    free(fname);
c561d2a3
 		}
 	    }
 	}
a19f21b6
     } else {
26c6ace2
 	cli_dbgmsg("ScanDir: Can't open directory %s.\n", dirname);
a19f21b6
 	return CL_EOPEN;
     }
c561d2a3
 
a19f21b6
     closedir(dd);
     return 0;
c561d2a3
 }
a19f21b6
 
a8b53539
 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, unsigned int arec, unsigned int mrec)
b151ef55
 {
a19f21b6
 	int ret = CL_CLEAN, i, fd, data_len;
 	vba_project_t *vba_project;
b151ef55
 	DIR *dd;
 	struct dirent *dent;
ec748835
 #if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
8c7e16b8
 	union {
 	    struct dirent d;
 	    char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
 	} result;
a45ec9cc
 #endif
b151ef55
 	struct stat statbuf;
941f3ab8
 	char *fname, *fullname;
a19f21b6
 	unsigned char *data;
 
26c6ace2
 
     cli_dbgmsg("VBADir: %s\n", dirname);
a19f21b6
     if((vba_project = (vba_project_t *) vba56_dir_read(dirname))) {
b151ef55
 
a19f21b6
 	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) {
26c6ace2
 		cli_dbgmsg("VBADir: Can't open file %s\n", fullname);
a19f21b6
 		free(fullname);
 		ret = CL_EOPEN;
 		break;
 	    }
 	    free(fullname);
26c6ace2
             cli_dbgmsg("VBADir: Decompress VBA project '%s'\n", vba_project->name[i]);
a19f21b6
 	    data = (unsigned char *) vba_decompress(fd, vba_project->offset[i], &data_len);
 	    close(fd);
 
 	    if(!data) {
26c6ace2
 		cli_dbgmsg("VBADir: WARNING: VBA project '%s' decompressed to NULL\n", vba_project->name[i]);
a19f21b6
 	    } else {
24dcd10b
 		if(scanned)
 		    *scanned += data_len / CL_COUNT_PRECISION;
 
856d9c84
 		if(cli_scanbuff(data, data_len, virname, root, CL_TYPE_MSOLE2) == CL_VIRUS) {
a19f21b6
 		    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);
dc413343
     } else if ((fullname = ppt_vba_read(dirname))) {
0da7859b
     	if(cli_scandir(fullname, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) {
dc413343
 	    ret = CL_VIRUS;
 	}
599f27c8
 	if(!cli_leavetemps_flag)
 	    cli_rmdirs(fullname);
dc413343
     	free(fullname);
943fc7fe
     } 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) {
26c6ace2
 			cli_dbgmsg("VBADir: Can't open file %s\n", fullname);
943fc7fe
 			free(fullname);
 			ret = CL_EOPEN;
 			break;
 		}
 		free(fullname);
26c6ace2
 		cli_dbgmsg("VBADir: Decompress WM project '%s' macro:%d key:%d\n", vba_project->name[i], i, vba_project->key[i]);
943fc7fe
 		data = (unsigned char *) wm_decrypt_macro(fd, vba_project->offset[i], vba_project->length[i], vba_project->key[i]);
 		close(fd);
 		
 		if(!data) {
26c6ace2
 			cli_dbgmsg("VBADir: WARNING: WM project '%s' macro %d decrypted to NULL\n", vba_project->name[i], i);
943fc7fe
 		} else {
24dcd10b
 			if(scanned)
 			    *scanned += vba_project->length[i] / CL_COUNT_PRECISION;
856d9c84
 			if(cli_scanbuff(data, vba_project->length[i], virname, root, CL_TYPE_MSOLE2) == CL_VIRUS) {
943fc7fe
 				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);
a19f21b6
     }
943fc7fe
 			
a19f21b6
     if(ret != CL_CLEAN)
     	return ret;
b151ef55
 
     if((dd = opendir(dirname)) != NULL) {
ec748835
 #ifdef HAVE_READDIR_R_3
8c7e16b8
 	while(!readdir_r(dd, &result.d, &dent) && dent) {
ec748835
 #elif defined(HAVE_READDIR_R_2)
8c7e16b8
 	while((dent = (struct dirent *) readdir_r(dd, &result.d))) {
ec748835
 #else
b151ef55
 	while((dent = readdir(dd))) {
ec748835
 #endif
618a038b
 #ifndef C_INTERIX
 	    if(dent->d_ino)
 #endif
 	    {
b151ef55
 		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))
0da7859b
 			    if (cli_vba_scandir(fname, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) {
a19f21b6
 			    	ret = CL_VIRUS;
 				free(fname);
 				break;
 			    }
b151ef55
 		    }
 		    free(fname);
 		}
 	    }
 	}
     } else {
26c6ace2
 	cli_dbgmsg("VBADir: Can't open directory %s.\n", dirname);
b151ef55
 	return CL_EOPEN;
     }
 
     closedir(dd);
a19f21b6
     return ret;
 }
 
a8b53539
 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, unsigned int arec, unsigned int mrec)
0842e139
 {
 	char *tempname, fullname[1024];
 	int ret=CL_CLEAN, fd;
 
 
     cli_dbgmsg("in cli_scanhtml()\n");
 
     tempname = cli_gentemp(NULL);
     if(mkdir(tempname, 0700)) {
         cli_dbgmsg("ScanHTML -> Can't create temporary directory %s\n", tempname);
af5f3346
 	free(tempname);
0842e139
         return CL_ETMPDIR;
     }
 
     html_normalise_fd(desc, tempname, NULL);
     snprintf(fullname, 1024, "%s/comment.html", tempname);
     fd = open(fullname, O_RDONLY);
     if (fd >= 0) {
         ret = cli_scandesc(fd, virname, scanned, root, 0, CL_TYPE_HTML);
 	close(fd);
     }
 
     if(ret < 0 || ret == CL_VIRUS) {
 	if(!cli_leavetemps_flag)
 	    cli_rmdirs(tempname);
 	free(tempname);
 	return ret;
     }
 
     if (ret == CL_CLEAN) {
 	snprintf(fullname, 1024, "%s/nocomment.html", tempname);
 	fd = open(fullname, O_RDONLY);
 	if (fd >= 0) {
 	    ret = cli_scandesc(fd, virname, scanned, root, 0, CL_TYPE_HTML);
 	    close(fd);
 	}
     }
 
     if(ret < 0 || ret == CL_VIRUS) {
 	if(!cli_leavetemps_flag)
 	    cli_rmdirs(tempname);
 	free(tempname);
 	return ret;
     }
 
     if (ret == CL_CLEAN) {
 	snprintf(fullname, 1024, "%s/script.html", tempname);
 	fd = open(fullname, O_RDONLY);
 	if (fd >= 0) {
 	    ret = cli_scandesc(fd, virname, scanned, root, 0, CL_TYPE_HTML);
 	    close(fd);
 	}
     }
 
     if(ret < 0 || ret == CL_VIRUS) {
 	if(!cli_leavetemps_flag)
 	    cli_rmdirs(tempname);
 	free(tempname);
 	return ret;
     }
 
     if (ret == CL_CLEAN) {
     	snprintf(fullname, 1024, "%s/rfc2397", tempname);
     	ret = cli_scandir(fullname, virname, scanned, root, limits, options, arec, mrec);
     }
 
     if(!cli_leavetemps_flag)
         cli_rmdirs(tempname);
 
     free(tempname);
     return ret;
 }
 
a8b53539
 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, unsigned int arec, unsigned int mrec)
a19f21b6
 {
941f3ab8
 	char *dir;
 	int ret = CL_CLEAN;
a19f21b6
 
26c6ace2
 
a19f21b6
     cli_dbgmsg("in cli_scanole2()\n");
 
     /* generate the temporary directory */
e5fa5bab
     dir = cli_gentemp(NULL);
a19f21b6
     if(mkdir(dir, 0700)) {
26c6ace2
 	cli_dbgmsg("OLE2: Can't create temporary directory %s\n", dir);
af5f3346
 	free(dir);
a19f21b6
 	return CL_ETMPDIR;
     }
 
21aa0b4d
     if((ret = cli_ole2_extract(desc, dir, limits))) {
26c6ace2
 	cli_dbgmsg("OLE2: %s\n", cl_strerror(ret));
599f27c8
 	if(!cli_leavetemps_flag)
 	    cli_rmdirs(dir);
a19f21b6
 	free(dir);
 	return ret;
     }
 
0da7859b
     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) {
941f3ab8
 	    ret = CL_VIRUS;
a19f21b6
 	}
     }
 
599f27c8
     if(!cli_leavetemps_flag)
 	cli_rmdirs(dir);
a19f21b6
     free(dir);
     return ret;
b151ef55
 }
 
255e314c
 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, unsigned int arec, unsigned int mrec, unsigned int posix)
26c6ace2
 {
 	char *dir;
 	int ret = CL_CLEAN;
 
 
     cli_dbgmsg("in cli_scantar()\n");
 
     /* generate temporary directory */
e5fa5bab
     dir = cli_gentemp(NULL);
26c6ace2
     if(mkdir(dir, 0700)) {
 	cli_errmsg("Tar: Can't create temporary directory %s\n", dir);
af5f3346
 	free(dir);
26c6ace2
 	return CL_ETMPDIR;
     }
 
255e314c
     if((ret = cli_untar(dir, desc, posix)))
26c6ace2
 	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;
 }
 
a8b53539
 static int cli_scanbinhex(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, unsigned int arec, unsigned int mrec)
c85a3e42
 {
 	char *dir;
 	int ret = CL_CLEAN;
 
 
     cli_dbgmsg("in cli_scanbinhex()\n");
 
     /* generate temporary directory */
     dir = cli_gentemp(NULL);
 
     if(mkdir(dir, 0700)) {
 	cli_errmsg("Binhex: Can't create temporary directory %s\n", dir);
af5f3346
 	free(dir);
c85a3e42
 	return CL_ETMPDIR;
     }
 
     if((ret = cli_binhex(dir, desc)))
 	cli_dbgmsg("Binhex: %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;
 }
 
a8b53539
 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, unsigned int arec, unsigned int mrec)
4ad876ad
 {
 	char *tempname;
 	int ret = CL_CLEAN;
 
 
     cli_dbgmsg("in cli_scanmschm()\n");	
 
e5fa5bab
     tempname = cli_gentemp(NULL);
4ad876ad
     if(mkdir(tempname, 0700)) {
26c6ace2
 	cli_dbgmsg("CHM: Can't create temporary directory %s\n", tempname);
af5f3346
 	free(tempname);
4ad876ad
 	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;
 }
 
a8b53539
 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, unsigned int arec, unsigned int mrec)
a6f7215c
 {
 	char *tempname;
 	int ret = CL_CLEAN;
 
     cli_dbgmsg("in cli_scanscrenc()\n");
 
e5fa5bab
     tempname = cli_gentemp(NULL);
a6f7215c
     if(mkdir(tempname, 0700)) {
 	cli_dbgmsg("CHM: Can't create temporary directory %s\n", tempname);
af5f3346
 	free(tempname);
a6f7215c
 	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;
 }
9bd73bcb
 
a8b53539
 static int cli_scanriff(int desc, const char **virname)
ed5307d9
 {
 	int ret = CL_CLEAN;
 
     if(cli_check_riff_exploit(desc) == 2) {
 	ret = CL_VIRUS;
 	*virname = "Exploit.W32.MS05-002";
     }
 
     return ret;
 }
 
14ae9b9f
 static int cli_scanjpeg(int desc, const char **virname)
 {
 	int ret = CL_CLEAN;
 
     if(cli_check_jpeg_exploit(desc) == 1) {
 	ret = CL_VIRUS;
 	*virname = "Exploit.W32.MS04-028";
     }
 
     return ret;
 }
 
af5f3346
 static int cli_scantnef(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, unsigned int arec, unsigned int mrec)
 {
 	int ret;
 	char *dir = cli_gentemp(NULL);
 
 
     if(mkdir(dir, 0700)) {
 	cli_dbgmsg("Can't create temporary directory for tnef file %s\n", dir);
 	free(dir);
 	return CL_ETMPDIR;
     }
 
     ret = cli_tnef(dir, desc);
 
     if(ret == CL_CLEAN)
 	ret = cli_scandir(dir, virname, scanned, root, limits, options, arec, mrec);
 
     if(!cli_leavetemps_flag)
 	cli_rmdirs(dir);
 
     free(dir);
     return ret;
 }
 
a8b53539
 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, unsigned int arec, unsigned int mrec)
b151ef55
 {
 	char *dir;
 	int ret;
 
 
a8b53539
     cli_dbgmsg("Starting cli_scanmail(), mrec == %d, arec == %d\n", mrec, arec);
b151ef55
 
e5fa5bab
     /* generate the temporary directory */
     dir = cli_gentemp(NULL);
     if(mkdir(dir, 0700)) {
 	cli_dbgmsg("Mail: Can't create temporary directory %s\n", dir);
 	free(dir);
 	return CL_ETMPDIR;
     }
b151ef55
 
e5fa5bab
     /*
      * Extract the attachments into the temporary directory
      */
     if((ret = cli_mbox(dir, desc, options))) {
599f27c8
 	if(!cli_leavetemps_flag)
 	    cli_rmdirs(dir);
b151ef55
 	free(dir);
 	return ret;
e5fa5bab
     }
 
     ret = cli_scandir(dir, virname, scanned, root, limits, options, arec, mrec);
 
     if(!cli_leavetemps_flag)
 	cli_rmdirs(dir);
 
     free(dir);
     return ret;
b151ef55
 }
 
a8b53539
 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, unsigned int arec, unsigned int mrec)
b151ef55
 {
2fe19b26
 	int ret = CL_CLEAN, nret;
d3d2fb1e
 	int bread = 0;
0d01fcb2
 	cli_file_t type;
255e314c
 	struct stat sb;
 
 
     if(fstat(desc, &sb) == -1) {
 	cli_errmsg("Can's fstat descriptor %d\n", desc);
 	return CL_EIO;
     }
b151ef55
 
255e314c
     if(sb.st_size <= 5) {
 	cli_dbgmsg("Small data (%d bytes)\n", sb.st_size);
 	return CL_CLEAN;
     }
b151ef55
 
     if(!root) {
1ed6a845
 	cli_errmsg("CRITICAL: root == NULL\n");
bd75bc84
 	return CL_EMALFDB;
b151ef55
     }
 
a13ed400
     if(!options) { /* raw mode (stdin, etc.) */
df757556
 	cli_dbgmsg("Raw mode: No support for special files\n");
 	if((ret = cli_scandesc(desc, virname, scanned, root, 0, 0) == CL_VIRUS))
26c6ace2
 	    cli_dbgmsg("%s found in descriptor %d\n", *virname, desc);
a13ed400
 	return ret;
     }
 
a19f21b6
     if(SCAN_ARCHIVE && limits && limits->maxreclevel)
a8b53539
 	if(arec > limits->maxreclevel) {
 	    cli_dbgmsg("Archive recursion limit exceeded (arec == %d).\n", arec);
78afb537
 	    if(BLOCKMAX) {
 		*virname = "Archive.ExceededRecursionLimit";
 		return CL_VIRUS;
 	    }
a19f21b6
 	    return CL_CLEAN;
0da7859b
 	}
b151ef55
 
0da7859b
     if(SCAN_MAIL)
a8b53539
 	if(mrec > MAX_MAIL_RECURSION) {
 	    cli_dbgmsg("Mail recursion level exceeded (mrec == %d).\n", mrec);
0da7859b
 	    /* return CL_EMAXREC; */
 	    return CL_CLEAN;
 	}
b151ef55
 
a19f21b6
     lseek(desc, 0, SEEK_SET);
255e314c
     type = cli_filetype2(desc);
26c6ace2
     lseek(desc, 0, SEEK_SET);
8b242bb9
 
a8b53539
     type == CL_TYPE_MAIL ? mrec++ : arec++;
2fe19b26
 
a19f21b6
     switch(type) {
06d4e856
 	case CL_TYPE_RAR:
a19f21b6
 	    if(!DISABLE_RAR && SCAN_ARCHIVE && !cli_scanrar_inuse)
0da7859b
 		ret = cli_scanrar(desc, virname, scanned, root, limits, options, arec, mrec);
a19f21b6
 	    break;
8b242bb9
 
06d4e856
 	case CL_TYPE_ZIP:
a19f21b6
 	    if(SCAN_ARCHIVE)
0da7859b
 		ret = cli_scanzip(desc, virname, scanned, root, limits, options, arec, mrec);
a19f21b6
 	    break;
8b242bb9
 
06d4e856
 	case CL_TYPE_GZ:
a19f21b6
 	    if(SCAN_ARCHIVE)
0da7859b
 		ret = cli_scangzip(desc, virname, scanned, root, limits, options, arec, mrec);
a19f21b6
 	    break;
8b242bb9
 
06d4e856
 	case CL_TYPE_BZ:
41b894c7
 #ifdef HAVE_BZLIB_H
a19f21b6
 	    if(SCAN_ARCHIVE)
0da7859b
 		ret = cli_scanbzip(desc, virname, scanned, root, limits, options, arec, mrec);
b151ef55
 #endif
a19f21b6
 	    break;
8b242bb9
 
06d4e856
 	case CL_TYPE_MSSZDD:
e2dc6ace
 	    if(SCAN_ARCHIVE)
06d4e856
 		ret = cli_scanszdd(desc, virname, scanned, root, limits, options, arec, mrec);
e2dc6ace
 	    break;
 
06d4e856
 	case CL_TYPE_MSCAB:
1ed6a845
 	    if(SCAN_ARCHIVE)
0da7859b
 		ret = cli_scanmscab(desc, virname, scanned, root, limits, options, arec, mrec);
1ed6a845
 	    break;
 
06d4e856
 	case CL_TYPE_MAIL:
a19f21b6
 	    if(SCAN_MAIL)
0da7859b
 		ret = cli_scanmail(desc, virname, scanned, root, limits, options, arec, mrec);
a19f21b6
 	    break;
8b242bb9
 
c5a962df
 	case CL_TYPE_TNEF:
 	    if(SCAN_MAIL)
 		ret = cli_scantnef(desc, virname, scanned, root, limits, options, arec, mrec);
 	    break;
 
06d4e856
 	case CL_TYPE_MSCHM:
4ad876ad
 	    if(SCAN_ARCHIVE)
 		ret = cli_scanmschm(desc, virname, scanned, root, limits, options, arec, mrec);
 	    break;
 
06d4e856
 	case CL_TYPE_MSOLE2:
a19f21b6
 	    if(SCAN_OLE2)
0da7859b
 		ret = cli_scanole2(desc, virname, scanned, root, limits, options, arec, mrec);
a19f21b6
 	    break;
8b242bb9
 
255e314c
 	case CL_TYPE_POSIX_TAR:
 	    if(SCAN_ARCHIVE)
 		ret = cli_scantar(desc, virname, scanned, root, limits, options, arec, mrec, 1);
 	    break;
 
 	case CL_TYPE_OLD_TAR:
26c6ace2
 	    if(SCAN_ARCHIVE)
255e314c
 		ret = cli_scantar(desc, virname, scanned, root, limits, options, arec, mrec, 0);
c85a3e42
 	    break;
 
 	case CL_TYPE_BINHEX:
 	    if(SCAN_ARCHIVE)
 		ret = cli_scanbinhex(desc, virname, scanned, root, limits, options, arec, mrec);
26c6ace2
 	    break;
 
06d4e856
 	case CL_TYPE_SCRENC:
a6f7215c
 	    ret = cli_scanscrenc(desc, virname, scanned, root, limits, options, arec, mrec);
ed5307d9
 	    break;
 
 	case CL_TYPE_RIFF:
a8b53539
 	    ret = cli_scanriff(desc, virname);
a6f7215c
 	    break;
 
14ae9b9f
 	case CL_TYPE_GRAPHICS:
 	    ret = cli_scanjpeg(desc, virname);
 	    break;
 
06d4e856
 	case CL_TYPE_DATA:
a19f21b6
 	    /* 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)
06d4e856
 		type = CL_TYPE_UNKNOWN_DATA;
a19f21b6
 	    }
 
06d4e856
 	case CL_TYPE_UNKNOWN_DATA:
958dc41c
 	    ret = cli_check_mydoom_log(desc, virname);
a19f21b6
 	    break;
2869a454
 
 	default:
 	    break;
b151ef55
     }
 
a8b53539
     type == CL_TYPE_MAIL ? mrec-- : arec--;
a19f21b6
 
06d4e856
     if(type != CL_TYPE_DATA && ret != CL_VIRUS) { /* scan the raw file */
28652280
 	    int typerec;
2fe19b26
 
06d4e856
 	type == CL_TYPE_UNKNOWN_TEXT ? (typerec = 1) : (typerec = 0);
9bdb71d0
 	if(lseek(desc, 0, SEEK_SET) < 0)
 	    cli_errmsg("lseek() failed, trying to continue anyway...\n");
0da7859b
 
df757556
 	if((nret = cli_scandesc(desc, virname, scanned, root, typerec, type)) == CL_VIRUS) {
26c6ace2
 	    cli_dbgmsg("%s found in descriptor %d.\n", *virname, desc);
b151ef55
 	    return CL_VIRUS;
2fe19b26
 
a5db7190
 	} else if(nret < 0) {
 	    return nret;
 
2fe19b26
 	} else if(nret >= CL_TYPENO) {
 	    lseek(desc, 0, SEEK_SET);
 
a8b53539
 	    nret == CL_TYPE_MAIL ? mrec++ : arec++;
2fe19b26
 	    switch(nret) {
06d4e856
 		case CL_TYPE_HTML:
2fe19b26
 		    if(SCAN_HTML)
0da7859b
 			if(cli_scanhtml(desc, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS)
2fe19b26
 			    return CL_VIRUS;
 		    break;
12565a07
 
06d4e856
 		case CL_TYPE_MAIL:
12565a07
 		    if(SCAN_MAIL)
0da7859b
 			if(cli_scanmail(desc, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS)
12565a07
 			    return CL_VIRUS;
 		    break;
2fe19b26
 	    }
a8b53539
 	    nret == CL_TYPE_MAIL ? mrec-- : arec--;
b151ef55
 	}
5aad47ca
     }
b151ef55
 
a8b53539
     arec++;
28652280
     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
 	 */
06d4e856
 	case CL_TYPE_MSEXE:
28652280
 	    if(SCAN_PE)
 		ret = cli_scanpe(desc, virname, scanned, root, limits, options, arec, mrec);
 	    break;
f91f55e0
 
 	default:
 	    break;
28652280
     }
a8b53539
     arec--;
28652280
 
bd75bc84
     if(ret == CL_EFORMAT) {
83b4c1f1
 	cli_dbgmsg("Descriptor[%d]: %s\n", desc, cl_strerror(CL_EFORMAT));
bd75bc84
 	return CL_CLEAN;
     } else {
 	return ret;
     }
b151ef55
 }
 
06d4e856
 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)
b151ef55
 {
a8b53539
     return cli_magic_scandesc(desc, virname, scanned, root, limits, options, 0, 0);
b151ef55
 }
 
a8b53539
 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, unsigned int arec, unsigned int mrec)
ad640f0b
 {
 	int fd, ret;
 
c2484690
 
0da7859b
     /* internal version of cl_scanfile with arec/mrec preserved */
ad640f0b
     if((fd = open(filename, O_RDONLY)) == -1)
 	return CL_EOPEN;
 
0da7859b
     ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, arec, mrec);
ad640f0b
 
     close(fd);
     return ret;
 }
 
06d4e856
 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)
b151ef55
 {
 	int fd, ret;
 
12565a07
 
b151ef55
     if((fd = open(filename, O_RDONLY)) == -1)
 	return CL_EOPEN;
 
     ret = cl_scandesc(fd, virname, scanned, root, limits, options);
     close(fd);
 
     return ret;
 }