libclamav/scanners.c
e3aaff8e
 /*
2023340a
  *  Copyright (C) 2007-2008 Sourcefire, Inc.
c7543866
  *
2023340a
  *  Authors: Tomasz Kojm
e3aaff8e
  *
  *  This program is free software; you can redistribute it and/or modify
bb34cb31
  *  it under the terms of the GNU General Public License version 2 as
  *  published by the Free Software Foundation.
e3aaff8e
  *
  *  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
48b7b4a7
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
e3aaff8e
  */
 
6d6e8271
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
517f7b7a
 #ifndef _WIN32
ee1b2a6c
 #include <sys/time.h>
517f7b7a
 #endif
e3aaff8e
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
0a80bd02
 #include <errno.h>
e3aaff8e
 #include <sys/types.h>
 #include <sys/stat.h>
b58fdfc2
 #ifdef	HAVE_UNISTD_H
a7f5fd00
 #include <unistd.h>
b58fdfc2
 #endif
 #ifdef	HAVE_SYS_PARAM_H
15edd45f
 #include <sys/param.h>
b58fdfc2
 #endif
e3aaff8e
 #include <fcntl.h>
 #include <dirent.h>
888f5794
 
bc93eda0
 #define DCONF_ARCH  ctx->dconf->archive
 #define DCONF_DOC   ctx->dconf->doc
 #define DCONF_MAIL  ctx->dconf->mail
 #define DCONF_OTHER ctx->dconf->other
 
e3aaff8e
 #include "clamav.h"
 #include "others.h"
bc93eda0
 #include "dconf.h"
85dd8460
 #include "scanners.h"
8000d078
 #include "matcher-ac.h"
 #include "matcher-bm.h"
e3aaff8e
 #include "matcher.h"
47bbbc56
 #include "ole2_extract.h"
 #include "vba_extract.h"
341e5433
 #include "msexpand.h"
8d3aca30
 #include "mbox.h"
a5373b64
 #include "chmunpack.h"
a9082ea2
 #include "pe.h"
8d3aca30
 #include "elf.h"
888f5794
 #include "filetypes.h"
 #include "htmlnorm.h"
2b259453
 #include "untar.h"
c3a3be2d
 #include "special.h"
6a31c2b4
 #include "binhex.h"
8d3aca30
 /* #include "uuencode.h" */
7bbd5f7f
 #include "tnef.h"
bf45bf13
 #include "sis.h"
2c313298
 #include "pdf.h"
bd988961
 #include "str.h"
24fd05e1
 #include "mspack.h"
 #include "cab.h"
52c2a8bd
 #include "rtf.h"
9d96e4b6
 #include "unarj.h"
517f7b7a
 #include "nsis/nulsft.h"
ed93f138
 #include "autoit.h"
015ce4a8
 #include "textnorm.h"
e3aaff8e
 #include <zlib.h>
f133da7a
 #include "unzip.h"
a6e38800
 #include "dlp.h"
c4934f41
 #include "default.h"
85885226
 #include "cpio.h"
89c14869
 #include "macho.h"
7ebede9b
 #include "ishield.h"
81fded11
 #include "7z.h"
998bcfa7
 #include "fmap.h"
511c2e79
 #include "cache.h"
ab769e30
 #include "events.h"
e3aaff8e
 
 #ifdef HAVE_BZLIB_H
 #include <bzlib.h>
 #endif
 
2bb229f6
 #if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
 #include <limits.h>
88794204
 #include <stddef.h>
2bb229f6
 #endif
 
3c91998b
 static int cli_scanfile(const char *filename, cli_ctx *ctx);
4048c4f6
 
cb680655
 static int cli_scandir(const char *dirname, cli_ctx *ctx)
c7543866
 {
 	DIR *dd;
 	struct dirent *dent;
 #if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
 	union {
 	    struct dirent d;
 	    char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
 	} result;
 #endif
 	struct stat statbuf;
 	char *fname;
fb0a54dd
 	unsigned int viruses_found = 0;
c7543866
 
     if((dd = opendir(dirname)) != NULL) {
 #ifdef HAVE_READDIR_R_3
 	while(!readdir_r(dd, &result.d, &dent) && dent) {
 #elif defined(HAVE_READDIR_R_2)
 	while((dent = (struct dirent *) readdir_r(dd, &result.d))) {
 #else
 	while((dent = readdir(dd))) {
 #endif
 	    if(dent->d_ino)
 	    {
 		if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
 		    /* build the full name */
 		    fname = cli_malloc(strlen(dirname) + strlen(dent->d_name) + 2);
 		    if(!fname) {
 			closedir(dd);
 			return CL_EMEM;
 		    }
 
58481352
 		    sprintf(fname, "%s"PATHSEP"%s", dirname, dent->d_name);
c7543866
 
 		    /* stat the file */
 		    if(lstat(fname, &statbuf) != -1) {
 			if(S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) {
cb680655
 			    if(cli_scandir(fname, ctx) == CL_VIRUS) {
c7543866
 				free(fname);
fb0a54dd
 
 				if (SCAN_ALL) {
 				    viruses_found++;
 				    continue;
 				}
 
                                 closedir(dd);
                                 return CL_VIRUS;
  			    }
c7543866
 			} else {
 			    if(S_ISREG(statbuf.st_mode)) {
 				if(cli_scanfile(fname, ctx) == CL_VIRUS) {
 				    free(fname);
fb0a54dd
 
 				    if (SCAN_ALL) {
 					viruses_found++;
 					continue;
 				    }
 
                                     closedir(dd);
                                     return CL_VIRUS;
  				}
c7543866
 			    }
 			}
 		    }
 		    free(fname);
 		}
 	    }
 	}
     } else {
 	cli_dbgmsg("cli_scandir: Can't open directory %s.\n", dirname);
 	return CL_EOPEN;
     }
 
     closedir(dd);
fb0a54dd
     if (SCAN_ALL && viruses_found)
 	return CL_VIRUS;
c7543866
     return CL_CLEAN;
 }
 
5ca7fd18
 static int cli_unrar_scanmetadata(int desc, unrar_metadata_t *metadata, cli_ctx *ctx, unsigned int files, uint32_t* sfx_check)
e3aaff8e
 {
133538e0
 	int ret = CL_SUCCESS;
e3aaff8e
 
133538e0
     if(files == 1 && sfx_check) {
 	if(*sfx_check == metadata->crc)
 	    return CL_BREAK;/* break extract loop */
 	else
 	    *sfx_check = metadata->crc;
e3aaff8e
     }
 
d91ab809
     cli_dbgmsg("RAR: %s, crc32: 0x%x, encrypted: %u, compressed: %u, normal: %u, method: %u, ratio: %u\n",
794f3f23
 	metadata->filename, metadata->crc, metadata->encrypted, (unsigned int) metadata->pack_size,
 	(unsigned int) metadata->unpack_size, metadata->method,
d91ab809
 	metadata->pack_size ? (unsigned int) (metadata->unpack_size / metadata->pack_size) : 0);
a62ae54f
 
570b1d00
     if(cli_matchmeta(ctx, metadata->filename, metadata->pack_size, metadata->unpack_size, metadata->encrypted, files, metadata->crc, NULL) == CL_VIRUS)
6416cdef
 	return CL_VIRUS;
a62ae54f
 
133538e0
     if(DETECT_ENCRYPTED && metadata->encrypted) {
 	cli_dbgmsg("RAR: Encrypted files found in archive.\n");
 	lseek(desc, 0, SEEK_SET);
ffa9b060
 	ret = cli_scandesc(desc, ctx, 0, 0, NULL, AC_SCAN_VIR, NULL);
133538e0
 	if(ret != CL_VIRUS) {
fb0a54dd
 	    cli_append_virus(ctx, "Heuristics.Encrypted.RAR");
133538e0
 	    return CL_VIRUS;
a62ae54f
 	}
133538e0
     }
a62ae54f
 
2dbcf700
     return ret;
 }
 
133538e0
 static int cli_scanrar(int desc, cli_ctx *ctx, off_t sfx_offset, uint32_t *sfx_check)
 {
 	int ret = CL_CLEAN;
5ca7fd18
 	unrar_metadata_t *metadata, *metadata_tmp;
133538e0
 	char *dir;
5ca7fd18
 	unrar_state_t rar_state;
fb0a54dd
 	unsigned int viruses_found = 0;
133538e0
 
     cli_dbgmsg("in scanrar()\n");
 
2b459819
     if(sfx_offset)
 	if(lseek(desc, sfx_offset, SEEK_SET) == -1)
871177cd
 	    return CL_ESEEK;
2b459819
 
133538e0
     /* generate the temporary directory */
33068e09
     if(!(dir = cli_gentemp(ctx->engine->tmpdir)))
5fc380f1
 	return CL_EMEM;
 
133538e0
     if(mkdir(dir, 0700)) {
 	cli_dbgmsg("RAR: Can't create temporary directory %s\n", dir);
 	free(dir);
 	return CL_ETMPDIR;
     }
 
bb7bd3ab
     if((ret = cli_unrar_open(desc, dir, &rar_state)) != UNRAR_OK) {
33068e09
 	if(!ctx->engine->keeptmp)
133538e0
 	    cli_rmdirs(dir);
 	free(dir);
06fd4ce9
 	if(ret == UNRAR_PASSWD) {
 	    cli_dbgmsg("RAR: Encrypted main header\n");
 	    if(DETECT_ENCRYPTED) {
 		lseek(desc, 0, SEEK_SET);
ffa9b060
 		ret = cli_scandesc(desc, ctx, 0, 0, NULL, AC_SCAN_VIR, NULL);
06fd4ce9
 		if(ret != CL_VIRUS)
fb0a54dd
 		    cli_append_virus(ctx, "Heuristics.Encrypted.RAR");
06fd4ce9
 		return CL_VIRUS;
 	    }
 	    return CL_CLEAN;
 	} if(ret == UNRAR_EMEM) {
5ca7fd18
 	    return CL_EMEM;
06fd4ce9
 	} else {
871177cd
 	    return CL_EUNPACK;
06fd4ce9
 	}
133538e0
     }
 
     do {
 	int rc;
5ca7fd18
 	rar_state.ofd = -1;
bb7bd3ab
 	ret = cli_unrar_extract_next_prepare(&rar_state,dir);
5ca7fd18
 	if(ret != UNRAR_OK) {
 	    if(ret == UNRAR_BREAK)
 		ret = CL_BREAK;
 	    else if(ret == UNRAR_EMEM)
 		ret = CL_EMEM;
 	    else
871177cd
 		ret = CL_EUNPACK;
2dbcf700
 	    break;
5ca7fd18
 	}
f009e241
 	if(ctx->engine->maxscansize && ctx->scansize >= ctx->engine->maxscansize) {
49dca2e3
 	    free(rar_state.file_header->filename);
 	    free(rar_state.file_header);
2dbcf700
 	    ret = CL_CLEAN;
f009e241
 	    break;
2dbcf700
 	}
f009e241
 	if(ctx->engine->maxscansize && ctx->scansize + ctx->engine->maxfilesize >= ctx->engine->maxscansize)
 	    rar_state.maxfilesize = ctx->engine->maxscansize - ctx->scansize;
 	else
 	    rar_state.maxfilesize = ctx->engine->maxfilesize;
 
bb7bd3ab
 	ret = cli_unrar_extract_next(&rar_state,dir);
5ca7fd18
 	if(ret == UNRAR_OK)
 	    ret = CL_SUCCESS;
 	else if(ret == UNRAR_EMEM)
 	    ret = CL_EMEM;
 	else
871177cd
 	    ret = CL_EFORMAT;
5ca7fd18
 
 	if(rar_state.ofd > 0) {
 	    lseek(rar_state.ofd,0,SEEK_SET);
 	    rc = cli_magic_scandesc(rar_state.ofd,ctx);
 	    close(rar_state.ofd);
33068e09
 	    if(!ctx->engine->keeptmp) 
871177cd
 		if (cli_unlink(rar_state.filename)) ret = CL_EUNLINK;
133538e0
 	    if(rc == CL_VIRUS ) {
fb0a54dd
 		cli_dbgmsg("RAR: infected with %s\n", cli_get_last_virus(ctx));
133538e0
 		ret = CL_VIRUS;
fb0a54dd
 		viruses_found++;
e3aaff8e
 	    }
 	}
 
fb0a54dd
 	if(ret == CL_VIRUS) {
 	    if(SCAN_ALL)
 		ret = CL_SUCCESS;
 	    else
 		break;
 	}
 
133538e0
 	if(ret == CL_SUCCESS)
44c3b2c8
 	    ret = cli_unrar_scanmetadata(desc,rar_state.metadata_tail, ctx, rar_state.file_count, sfx_check);
133538e0
 
     } while(ret == CL_SUCCESS);
 
     if(ret == CL_BREAK)
 	ret = CL_CLEAN;
 
     metadata = metadata_tmp = rar_state.metadata; 
 
cb680655
     if(cli_scandir(rar_state.comment_dir, ctx) == CL_VIRUS)
133538e0
 	ret = CL_VIRUS;
e3aaff8e
 
bb7bd3ab
     cli_unrar_close(&rar_state);
cf68af72
 
33068e09
     if(!ctx->engine->keeptmp)
478ce4b2
         cli_rmdirs(dir);
e3aaff8e
 
478ce4b2
     free(dir);
133538e0
 
478ce4b2
     metadata = metadata_tmp;
     while (metadata) {
     	metadata_tmp = metadata->next;
     	free(metadata->filename);
     	free(metadata);
     	metadata = metadata_tmp;
e3aaff8e
     }
2b259453
     cli_dbgmsg("RAR: Exit code: %d\n", ret);
7761c6eb
 
fb0a54dd
     if (SCAN_ALL && viruses_found)
 	return CL_VIRUS;
e3aaff8e
     return ret;
 }
 
9d96e4b6
 static int cli_scanarj(int desc, cli_ctx *ctx, off_t sfx_offset, uint32_t *sfx_check)
 {
570b1d00
 	int ret = CL_CLEAN, rc, file = 0;
9d96e4b6
 	arj_metadata_t metadata;
 	char *dir;
 
     cli_dbgmsg("in cli_scanarj()\n");
 
      /* generate the temporary directory */
33068e09
     if(!(dir = cli_gentemp(ctx->engine->tmpdir)))
5fc380f1
 	return CL_EMEM;
 
9d96e4b6
     if(mkdir(dir, 0700)) {
b865e44a
 	cli_dbgmsg("ARJ: Can't create temporary directory %s\n", dir);
9d96e4b6
 	free(dir);
 	return CL_ETMPDIR;
     }
 
     if(sfx_offset)
 	lseek(desc, sfx_offset, SEEK_SET);
 
     ret = cli_unarj_open(desc, dir);
     if (ret != CL_SUCCESS) {
33068e09
 	if(!ctx->engine->keeptmp)
9d96e4b6
 	    cli_rmdirs(dir);
 	free(dir);
 	cli_dbgmsg("ARJ: Error: %s\n", cl_strerror(ret));
 	return ret;
     }
     
    do {
fc355be4
         metadata.filename = NULL;
9d96e4b6
 	ret = cli_unarj_prepare_file(desc, dir, &metadata);
 	if (ret != CL_SUCCESS) {
 	   break;
 	}
570b1d00
 	file++;
 	if(cli_matchmeta(ctx, metadata.filename, metadata.comp_size, metadata.orig_size, metadata.encrypted, file, 0, NULL) == CL_VIRUS)
 	    return CL_VIRUS;
 
d91ab809
 	if ((ret = cli_checklimits("ARJ", ctx, metadata.orig_size, metadata.comp_size, 0))!=CL_CLEAN) {
 	    ret = CL_SUCCESS;
fc355be4
 	    if (metadata.filename)
 		free(metadata.filename);
d91ab809
 	    continue;
9d96e4b6
 	}
 	ret = cli_unarj_extract_file(desc, dir, &metadata);
 	if (metadata.ofd >= 0) {
 	    lseek(metadata.ofd, 0, SEEK_SET);
 	    rc = cli_magic_scandesc(metadata.ofd, ctx);
 	    close(metadata.ofd);
 	    if (rc == CL_VIRUS) {
fb0a54dd
 		cli_dbgmsg("ARJ: infected with %s\n", cli_get_last_virus(ctx));
9d96e4b6
 		ret = CL_VIRUS;
fc355be4
 		if (metadata.filename) {
 		    free(metadata.filename);
 		    metadata.filename = NULL;
 		}
9d96e4b6
 		break;
 	    }
 	}
 	if (metadata.filename) {
 		free(metadata.filename);
 		metadata.filename = NULL;
 	}
 
     } while(ret == CL_SUCCESS);
     
33068e09
     if(!ctx->engine->keeptmp)
9d96e4b6
 	cli_rmdirs(dir);
 
     free(dir);
     if (metadata.filename) {
 	free(metadata.filename);
     }
 
     cli_dbgmsg("ARJ: Exit code: %d\n", ret);
9f851a24
     if (ret == CL_BREAK)
 	ret = CL_CLEAN;
9d96e4b6
 
     return ret;
 }
e3aaff8e
 
5a72fce6
 
 static int cli_scangzip_with_zib_from_the_80s(cli_ctx *ctx, unsigned char *buff) {
     int fd, ret, outsize = 0, bytes;
     fmap_t *map = *ctx->fmap;
     char *tmpname;
     gzFile gz;
 
     fd = dup(map->fd);
     if(fd < 0)
 	return CL_EDUP;
 
     lseek(fd, 0, SEEK_SET);
     if(!(gz = gzdopen(fd, "rb"))) {
 	close(fd);
 	return CL_EOPEN;
     }
 
     if((ret = cli_gentempfd(ctx->engine->tmpdir, &tmpname, &fd)) != CL_SUCCESS) {
 	cli_dbgmsg("GZip: Can't generate temporary file.\n");
 	gzclose(gz);
 	return ret;
     }
     
     while((bytes = gzread(gz, buff, FILEBUFF)) > 0) {
 	outsize += bytes;
 	if(cli_checklimits("GZip", ctx, outsize, 0, 0)!=CL_CLEAN)
 	    break;
 	if(cli_writen(fd, buff, bytes) != bytes) {
 	    close(fd);
 	    gzclose(gz);
 	    if(cli_unlink(tmpname)) {
 		free(tmpname);
 		return CL_EUNLINK;
 	    }
 	    free(tmpname);
 	    return CL_EWRITE;
 	}
     }
 
     gzclose(gz);
 
     if((ret = cli_magic_scandesc(fd, ctx)) == CL_VIRUS) {
fb0a54dd
 	cli_dbgmsg("GZip: Infected with %s\n", cli_get_last_virus(ctx));
5a72fce6
 	close(fd);
 	if(!ctx->engine->keeptmp) {
 	    if (cli_unlink(tmpname)) {
 	    	free(tmpname);
 		return CL_EUNLINK;
 	    }
 	}
 	free(tmpname);
 	return CL_VIRUS;
     }
     close(fd);
     if(!ctx->engine->keeptmp)
 	if (cli_unlink(tmpname)) ret = CL_EUNLINK;
     free(tmpname);
     return ret;
 }
 
686ed348
 static int cli_scangzip(cli_ctx *ctx)
e3aaff8e
 {
686ed348
 	int fd, ret = CL_CLEAN;
 	unsigned char buff[FILEBUFF];
0e621e7d
 	char *tmpname;
686ed348
 	z_stream z;
5a72fce6
 	size_t at = 0, outsize = 0;
49cc1e3c
 	fmap_t *map = *ctx->fmap;
686ed348
  	
fc56deed
     cli_dbgmsg("in cli_scangzip()\n");
 
686ed348
     memset(&z, 0, sizeof(z));
be2f94dd
     if((ret = inflateInit2(&z, MAX_WBITS + 16)) != Z_OK) {
 	cli_dbgmsg("GZip: InflateInit failed: %d\n", ret);
5a72fce6
 	return cli_scangzip_with_zib_from_the_80s(ctx, buff);
e3aaff8e
     }
 
686ed348
     if((ret = cli_gentempfd(ctx->engine->tmpdir, &tmpname, &fd)) != CL_SUCCESS) {
2b259453
 	cli_dbgmsg("GZip: Can't generate temporary file.\n");
686ed348
 	inflateEnd(&z);
a7ac5978
 	return ret;
e3aaff8e
     }
 
686ed348
     while (at < map->len) {
 	unsigned int bytes = MIN(map->len - at, map->pgsz);
 	if(!(z.next_in = fmap_need_off_once(map, at, bytes))) {
 	    cli_dbgmsg("GZip: Can't read %u bytes @ %lu.\n", bytes, (long unsigned)at);
 	    inflateEnd(&z);
 	    close(fd);
 	    if (cli_unlink(tmpname)) {
 		free(tmpname);
871177cd
 		return CL_EUNLINK;
997a0e0b
 	    }
686ed348
 	    free(tmpname);
 	    return CL_EREAD;
997a0e0b
 	}
686ed348
 	at += bytes;
 	z.avail_in = bytes;
 	do {
 	    int inf;
 	    z.avail_out = sizeof(buff);
             z.next_out = buff;
 	    inf = inflate(&z, Z_NO_FLUSH);
 	    if(inf != Z_OK && inf != Z_STREAM_END && inf != Z_BUF_ERROR) {
 		cli_dbgmsg("GZip: Bad stream.\n");
 		at = map->len;
 		break;
 	    }
 	    if(cli_writen(fd, buff, sizeof(buff) - z.avail_out) < 0) {
 		inflateEnd(&z);	    
 		close(fd);
 		if (cli_unlink(tmpname)) {
997a0e0b
 		    free(tmpname);
871177cd
 		    return CL_EUNLINK;
997a0e0b
 		}
5a72fce6
 		free(tmpname);
 		return CL_EWRITE;
 	    }
 	    outsize += sizeof(buff) - z.avail_out;
 	    if(cli_checklimits("GZip", ctx, outsize, 0, 0)!=CL_CLEAN) {
 		at = map->len;
 		break;
997a0e0b
 	    }
786766eb
 	    if(inf == Z_STREAM_END) {
 		at -= z.avail_in;
 		inflateReset(&z);
 		break;
 	    }
686ed348
 	} while (z.avail_out == 0);
e3aaff8e
     }
 
686ed348
     inflateEnd(&z);	    
985d9958
 
5a72fce6
     if((ret = cli_magic_scandesc(fd, ctx)) == CL_VIRUS) {
fb0a54dd
 	cli_dbgmsg("GZip: Infected with %s\n", cli_get_last_virus(ctx));
a7ac5978
 	close(fd);
33068e09
 	if(!ctx->engine->keeptmp) {
997a0e0b
 	    if (cli_unlink(tmpname)) {
 	    	free(tmpname);
871177cd
 		return CL_EUNLINK;
997a0e0b
 	    }
 	}
686ed348
 	free(tmpname);
e3aaff8e
 	return CL_VIRUS;
     }
a7ac5978
     close(fd);
33068e09
     if(!ctx->engine->keeptmp)
871177cd
 	if (cli_unlink(tmpname)) ret = CL_EUNLINK;
686ed348
     free(tmpname);
e3aaff8e
     return ret;
 }
 
de472237
 
 #ifndef HAVE_BZLIB_H
 static int cli_scanbzip(int desc, cli_ctx *ctx) {
     cli_warnmsg("cli_scanbzip: bzip2 support not compiled in\n");
     return CL_CLEAN;
 }
 
 #else
e3aaff8e
 
 #ifdef NOBZ2PREFIX
 #define BZ2_bzReadOpen bzReadOpen
 #define BZ2_bzReadClose bzReadClose
 #define BZ2_bzRead bzRead
 #endif
 
3c91998b
 static int cli_scanbzip(int desc, cli_ctx *ctx)
e3aaff8e
 {
 	int fd, bytes, ret = CL_CLEAN, bzerror = 0;
33f89aa5
 	unsigned long int size = 0;
f7148839
 	char *buff;
a7ac5978
 	FILE *fs;
0e621e7d
 	char *tmpname;
e3aaff8e
 	BZFILE *bfd;
 
 
9e431a95
     if((fs = fdopen(dup(desc), "rb")) == NULL) {
2b259453
 	cli_dbgmsg("Bzip: Can't open descriptor %d.\n", desc);
871177cd
 	return CL_EOPEN;
e3aaff8e
     }
 
724b2bf7
     if((bfd = BZ2_bzReadOpen(&bzerror, fs, 0, 0, NULL, 0)) == NULL) {
2b259453
 	cli_dbgmsg("Bzip: Can't initialize bzip2 library (descriptor: %d).\n", desc);
9e431a95
 	fclose(fs);
871177cd
 	return CL_EOPEN;
e3aaff8e
     }
 
33068e09
     if((ret = cli_gentempfd(ctx->engine->tmpdir, &tmpname, &fd))) {
2b259453
 	cli_dbgmsg("Bzip: Can't generate temporary file.\n");
e3aaff8e
 	BZ2_bzReadClose(&bzerror, bfd);
9e431a95
 	fclose(fs);
a7ac5978
 	return ret;
e3aaff8e
     }
 
cd69cf20
     if(!(buff = (char *) cli_malloc(FILEBUFF))) {
 	cli_dbgmsg("Bzip: Unable to malloc %u bytes.\n", FILEBUFF);
a7ac5978
 	close(fd);
33068e09
 	if(!ctx->engine->keeptmp) {
997a0e0b
 	    if (cli_unlink(tmpname)) {
 	    	free(tmpname);
 		fclose(fs);
 		BZ2_bzReadClose(&bzerror, bfd);
871177cd
 		return CL_EUNLINK;
997a0e0b
 	    }
 	}
0e621e7d
 	free(tmpname);	
9e431a95
 	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;
 
d91ab809
 	if(cli_checklimits("Bzip", ctx, size + FILEBUFF, 0, 0)!=CL_CLEAN)
 	    break;
e3aaff8e
 
4152ad50
 	if(cli_writen(fd, buff, bytes) != bytes) {
2b259453
 	    cli_dbgmsg("Bzip: Can't write to file.\n");
e3aaff8e
 	    BZ2_bzReadClose(&bzerror, bfd);
a7ac5978
 	    close(fd);
33068e09
 	    if(!ctx->engine->keeptmp) {
997a0e0b
 		if (cli_unlink(tmpname)) {
 		    free(tmpname);
 		    free(buff);
 		    fclose(fs);
871177cd
 		    return CL_EUNLINK;
997a0e0b
 		}
 	    }
0e621e7d
 	    free(tmpname);	
f7148839
 	    free(buff);
9e431a95
 	    fclose(fs);
871177cd
 	    return CL_EWRITE;
e3aaff8e
 	}
     }
 
f7148839
     free(buff);
e3aaff8e
     BZ2_bzReadClose(&bzerror, bfd);
985d9958
 
     if(ret == CL_VIRUS) {
a7ac5978
 	close(fd);
33068e09
 	if(!ctx->engine->keeptmp)
871177cd
 	    if (cli_unlink(tmpname)) ret = CL_EUNLINK;
0e621e7d
 	free(tmpname);	
985d9958
 	fclose(fs);
 	return ret;
     }
 
e3aaff8e
     lseek(fd, 0, SEEK_SET);
3c91998b
     if((ret = cli_magic_scandesc(fd, ctx)) == CL_VIRUS ) {
fb0a54dd
 	cli_dbgmsg("Bzip: Infected with %s\n", cli_get_last_virus(ctx));
e3aaff8e
     }
a7ac5978
     close(fd);
33068e09
     if(!ctx->engine->keeptmp)
871177cd
 	if (cli_unlink(tmpname)) ret = CL_EUNLINK;
0e621e7d
     free(tmpname);	
9e431a95
     fclose(fs);
e3aaff8e
 
     return ret;
 }
 #endif
 
3c91998b
 static int cli_scanszdd(int desc, cli_ctx *ctx)
341e5433
 {
6e47a652
 	int ofd, ret;
0e621e7d
 	char *tmpname;
341e5433
 
2b259453
 
5da612a4
     cli_dbgmsg("in cli_scanszdd()\n");
 
33068e09
     if((ret = cli_gentempfd(ctx->engine->tmpdir, &tmpname, &ofd))) {
6e47a652
 	cli_dbgmsg("MSEXPAND: Can't generate temporary file/descriptor\n");
 	return ret;
341e5433
     }
 
6e47a652
     lseek(desc, 0, SEEK_SET);
     ret = cli_msexpand(desc, ofd, ctx);
341e5433
 
6e47a652
     if(ret != CL_SUCCESS) { /* CL_VIRUS or some error */
 	close(ofd);
33068e09
 	if(!ctx->engine->keeptmp)
871177cd
 	    if (cli_unlink(tmpname)) ret = CL_EUNLINK;
0e621e7d
 	free(tmpname);	
6e47a652
 	return ret;
341e5433
     }
 
6e47a652
     cli_dbgmsg("MSEXPAND: Decompressed into %s\n", tmpname);
     lseek(ofd, 0, SEEK_SET);
     ret = cli_magic_scandesc(ofd, ctx);
     close(ofd);
33068e09
     if(!ctx->engine->keeptmp)
871177cd
 	if (cli_unlink(tmpname)) ret = CL_EUNLINK;
0e621e7d
     free(tmpname);	
6e47a652
 
341e5433
     return ret;
 }
 
d8a5c616
 static int cli_scanmscab(int desc, cli_ctx *ctx, off_t sfx_offset)
414abe87
 {
 	char *tempname;
24fd05e1
 	int ret;
 	unsigned int files = 0;
 	struct cab_archive cab;
 	struct cab_file *file;
c6b9d863
 	unsigned int corrupted_input;
fb0a54dd
 	unsigned int viruses_found = 0;
414abe87
 
     cli_dbgmsg("in cli_scanmscab()\n");
 
24fd05e1
     if((ret = cab_open(desc, sfx_offset, &cab)))
 	return ret;
414abe87
 
24fd05e1
     for(file = cab.files; file; file = file->next) {
 	files++;
52ab3176
 
570b1d00
 	if(cli_matchmeta(ctx, file->name, 0, file->length, 0, files, 0, NULL) == CL_VIRUS) {
fb0a54dd
 	    if (!SCAN_ALL) {
 		ret = CL_VIRUS;
 		break;
 	    }
 	    viruses_found++;
5fc380f1
 	}
8df99a92
 
 	if(ctx->engine->maxscansize && ctx->scansize >= ctx->engine->maxscansize) {
 	    ret = CL_CLEAN;
 	    break;
 	}
570b1d00
 
 	if(!(tempname = cli_gentemp(ctx->engine->tmpdir))) {
 	    ret = CL_EMEM;
 	    break;
 	}
 
8df99a92
 	if(ctx->engine->maxscansize && ctx->scansize + ctx->engine->maxfilesize >= ctx->engine->maxscansize)
 	    file->max_size = ctx->engine->maxscansize - ctx->scansize;
24fd05e1
 	else
81c1e5f5
 	    file->max_size = ctx->engine->maxfilesize ? ctx->engine->maxfilesize : 0xffffffff;
24fd05e1
 
8df99a92
 	cli_dbgmsg("CAB: Extracting file %s to %s, size %u, max_size: %u\n", file->name, tempname, file->length, (unsigned int) file->max_size);
 	file->written_size = 0;
 	if((ret = cab_extract(file, tempname))) {
 	    cli_dbgmsg("CAB: Failed to extract file: %s\n", cl_strerror(ret));
 	} else {
c6b9d863
 	    corrupted_input = ctx->corrupted_input;
 	    if(file->length != file->written_size) {
8df99a92
 		cli_dbgmsg("CAB: Length from header %u but wrote %u bytes\n", (unsigned int) file->length, (unsigned int) file->written_size);
c6b9d863
 		ctx->corrupted_input = 1;
 	    }
8df99a92
 	    ret = cli_scanfile(tempname, ctx);
c6b9d863
 	    ctx->corrupted_input = corrupted_input;
8df99a92
 	}
33068e09
 	if(!ctx->engine->keeptmp) {
fd45238e
 	    if (!access(tempname, R_OK) && cli_unlink(tempname)) {
997a0e0b
 	    	free(tempname);
871177cd
 		ret = CL_EUNLINK;
997a0e0b
 		break;
 	    }
 	}
24fd05e1
 	free(tempname);
fb0a54dd
 	if(ret == CL_VIRUS) {
 	    if (SCAN_ALL)
 		viruses_found++;
 	    else
 		break;
 	}
414abe87
     }
 
24fd05e1
     cab_free(&cab);
fb0a54dd
     if (viruses_found)
 	return CL_VIRUS;
414abe87
     return ret;
 }
 
eeb1f5af
 static int vba_scandata(const unsigned char *data, unsigned int len, cli_ctx *ctx)
 {
 	struct cli_matcher *groot = ctx->engine->root[0];
 	struct cli_matcher *troot = ctx->engine->root[2];
 	struct cli_ac_data gmdata, tmdata;
 	struct cli_ac_data *mdata[2];
 	int ret;
fb0a54dd
 	unsigned int viruses_found = 0;
eeb1f5af
 
     if((ret = cli_ac_initdata(&tmdata, troot->ac_partsigs, troot->ac_lsigs, troot->ac_reloff_num, CLI_DEFAULT_AC_TRACKLEN)))
 	return ret;
 
     if((ret = cli_ac_initdata(&gmdata, groot->ac_partsigs, groot->ac_lsigs, groot->ac_reloff_num, CLI_DEFAULT_AC_TRACKLEN))) {
 	cli_ac_freedata(&tmdata);
 	return ret;
     }
     mdata[0] = &tmdata;
     mdata[1] = &gmdata;
 
     ret = cli_scanbuff(data, len, 0, ctx, CL_TYPE_MSOLE2, mdata);
 
fb0a54dd
     if(ret != CL_VIRUS || SCAN_ALL) {
 	if (SCAN_ALL)
 	    viruses_found++;
eeb1f5af
 	ret = cli_lsig_eval(ctx, troot, &tmdata, NULL, NULL);
fb0a54dd
 	if(ret != CL_VIRUS || SCAN_ALL)
 	    if (SCAN_ALL)
 		viruses_found++;
eeb1f5af
 	    ret = cli_lsig_eval(ctx, groot, &gmdata, NULL, NULL);
     }
     cli_ac_freedata(&tmdata);
     cli_ac_freedata(&gmdata);
 
fb0a54dd
     if (viruses_found)
 	return CL_VIRUS;
eeb1f5af
     return ret;
 }
 
72ce4b70
 static int cli_vba_scandir(const char *dirname, cli_ctx *ctx, struct uniq *U)
e3aaff8e
 {
6bee45b3
 	int ret = CL_CLEAN, i, j, fd, data_len, hasmacros = 0;
467f8b1e
 	vba_project_t *vba_project;
e3aaff8e
 	DIR *dd;
 	struct dirent *dent;
72a1b240
 #if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
88794204
 	union {
 	    struct dirent d;
 	    char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
 	} result;
2bb229f6
 #endif
e3aaff8e
 	struct stat statbuf;
72ce4b70
 	char *fullname, vbaname[1024];
467f8b1e
 	unsigned char *data;
937ade08
 	char *hash;
 	uint32_t hashcnt;
fb0a54dd
 	unsigned int viruses_found = 0;
467f8b1e
 
2b259453
 
     cli_dbgmsg("VBADir: %s\n", dirname);
72ce4b70
     hashcnt = uniq_get(U, "_vba_project", 12, NULL);
     while(hashcnt--) {
 	if(!(vba_project = (vba_project_t *)cli_vba_readdir(dirname, U, hashcnt))) continue;
e3aaff8e
 
467f8b1e
 	for(i = 0; i < vba_project->count; i++) {
937ade08
 	    for(j = 0; (unsigned int)j < vba_project->colls[i]; j++) {
58481352
 		snprintf(vbaname, 1024, "%s"PATHSEP"%s_%u", vba_project->dir, vba_project->name[i], j);
72ce4b70
 		vbaname[sizeof(vbaname)-1] = '\0';
 		fd = open(vbaname, O_RDONLY|O_BINARY);
 		if(fd == -1) continue;
937ade08
 		cli_dbgmsg("VBADir: Decompress VBA project '%s_%u'\n", vba_project->name[i], j);
72ce4b70
 		data = (unsigned char *)cli_vba_inflate(fd, vba_project->offset[i], &data_len);
 		close(fd);
6bee45b3
 		hasmacros++;
72ce4b70
 		if(!data) {
937ade08
 		    cli_dbgmsg("VBADir: WARNING: VBA project '%s_%u' decompressed to NULL\n", vba_project->name[i], j);
72ce4b70
 		} else {
 		    /* cli_dbgmsg("Project content:\n%s", data); */
 		    if(ctx->scanned)
 			*ctx->scanned += data_len / CL_COUNT_PRECISION;
eeb1f5af
 		    if(vba_scandata(data, data_len, ctx) == CL_VIRUS) {
fb0a54dd
 			if (SCAN_ALL) 
 			    viruses_found++;
 			else {
 			    free(data);
 			    ret = CL_VIRUS;
 			    break;
 			}
72ce4b70
 		    }
467f8b1e
 		    free(data);
 		}
 	    }
 	}
 
 	free(vba_project->name);
72ce4b70
 	free(vba_project->colls);
467f8b1e
 	free(vba_project->dir);
 	free(vba_project->offset);
 	free(vba_project);
fb0a54dd
 	if (ret == CL_VIRUS && !SCAN_ALL)
 	    break;
72ce4b70
     }
 
fb0a54dd
     if((ret == CL_CLEAN || (ret == CL_VIRUS && SCAN_ALL)) && 
 	(hashcnt = uniq_get(U, "powerpoint document", 19, &hash))) {
72ce4b70
 	while(hashcnt--) {
58481352
 	    snprintf(vbaname, 1024, "%s"PATHSEP"%s_%u", dirname, hash, hashcnt);
72ce4b70
 	    vbaname[sizeof(vbaname)-1] = '\0';
 	    fd = open(vbaname, O_RDONLY|O_BINARY);
 	    if (fd == -1) continue;
33068e09
 	    if ((fullname = cli_ppt_vba_read(fd, ctx))) {
cb680655
 		if(cli_scandir(fullname, ctx) == CL_VIRUS) {
72ce4b70
 		    ret = CL_VIRUS;
fb0a54dd
 		    viruses_found++;
2e53806b
 		}
33068e09
 		if(!ctx->engine->keeptmp)
72ce4b70
 		    cli_rmdirs(fullname);
2e53806b
 		free(fullname);
72ce4b70
 	    }
 	    close(fd);
 	}
     }
 
fb0a54dd
     if ((ret == CL_CLEAN || (ret == CL_VIRUS && SCAN_ALL)) && 
 	(hashcnt = uniq_get(U, "worddocument", 12, &hash))) {
72ce4b70
 	while(hashcnt--) {
58481352
 	    snprintf(vbaname, sizeof(vbaname), "%s"PATHSEP"%s_%u", dirname, hash, hashcnt);
72ce4b70
 	    vbaname[sizeof(vbaname)-1] = '\0';
 	    fd = open(vbaname, O_RDONLY|O_BINARY);
 	    if (fd == -1) continue;
 	    
 	    if (!(vba_project = (vba_project_t *)cli_wm_readdir(fd))) {
2e53806b
 		close(fd);
72ce4b70
 		continue;
 	    }
 
 	    for (i = 0; i < vba_project->count; i++) {
 		cli_dbgmsg("VBADir: Decompress WM project macro:%d key:%d length:%d\n", i, vba_project->key[i], vba_project->length[i]);
 		data = (unsigned char *)cli_wm_decrypt_macro(fd, vba_project->offset[i], vba_project->length[i], vba_project->key[i]);
2e53806b
 		if(!data) {
2b259453
 			cli_dbgmsg("VBADir: WARNING: WM project '%s' macro %d decrypted to NULL\n", vba_project->name[i], i);
2e53806b
 		} else {
72ce4b70
 			cli_dbgmsg("Project content:\n%s", data);
3c91998b
 			if(ctx->scanned)
 			    *ctx->scanned += vba_project->length[i] / CL_COUNT_PRECISION;
eeb1f5af
 			if(vba_scandata(data, vba_project->length[i], ctx) == CL_VIRUS) {
fb0a54dd
 			    if (SCAN_ALL)
 				viruses_found++;
 			    else {
2e53806b
 				free(data);
 				ret = CL_VIRUS;
 				break;
fb0a54dd
 			    }
2e53806b
 			}
 			free(data);
 		}
72ce4b70
 	    }
 
 	    close(fd);
 	    free(vba_project->name);
 	    free(vba_project->colls);
 	    free(vba_project->dir);
 	    free(vba_project->offset);
 	    free(vba_project->key);
 	    free(vba_project->length);
 	    free(vba_project);
fb0a54dd
 	    if(ret == CL_VIRUS) {
 		if (SCAN_ALL)
 		    viruses_found++;
 		else
 		    break;
 	    }
2e53806b
 	}
467f8b1e
     }
cd69cf20
 
fb0a54dd
     if(ret != CL_CLEAN && !(ret == CL_VIRUS && SCAN_ALL))
467f8b1e
     	return ret;
e3aaff8e
 
892d2f56
     /* Check directory for embedded OLE objects */
72ce4b70
     hashcnt = uniq_get(U, "_1_ole10native", 14, &hash);
     while(hashcnt--) {
58481352
 	snprintf(vbaname, sizeof(vbaname), "%s"PATHSEP"%s_%u", dirname, hash, hashcnt);
72ce4b70
 	vbaname[sizeof(vbaname)-1] = '\0';
 
 	fd = open(vbaname, O_RDONLY|O_BINARY);
 	if (fd >= 0) {
 	    ret = cli_scan_ole10(fd, ctx);
 	    close(fd);
fb0a54dd
 	    if(ret != CL_CLEAN && !(ret == CL_VIRUS && SCAN_ALL))
72ce4b70
 		return ret;
892d2f56
 	}
     }
 
72ce4b70
 
     /* ACAB: since we now hash filenames and handle collisions we
      * could avoid recursion by removing the block below and by
      * flattening the paths in ole2_walk_property_tree (case 1) */
 
e3aaff8e
     if((dd = opendir(dirname)) != NULL) {
72a1b240
 #ifdef HAVE_READDIR_R_3
88794204
 	while(!readdir_r(dd, &result.d, &dent) && dent) {
72a1b240
 #elif defined(HAVE_READDIR_R_2)
88794204
 	while((dent = (struct dirent *) readdir_r(dd, &result.d))) {
72a1b240
 #else
e3aaff8e
 	while((dent = readdir(dd))) {
72a1b240
 #endif
feeaa333
 	    if(dent->d_ino)
 	    {
e3aaff8e
 		if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
 		    /* build the full name */
72ce4b70
 		    fullname = cli_malloc(strlen(dirname) + strlen(dent->d_name) + 2);
 		    if(!fullname) {
cd69cf20
 			ret = CL_EMEM;
 			break;
 		    }
58481352
 		    sprintf(fullname, "%s"PATHSEP"%s", dirname, dent->d_name);
e3aaff8e
 
 		    /* stat the file */
72ce4b70
 		    if(lstat(fullname, &statbuf) != -1) {
e3aaff8e
 			if(S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode))
72ce4b70
 			  if (cli_vba_scandir(fullname, ctx, U) == CL_VIRUS) {
fb0a54dd
 			      if (SCAN_ALL)
 				  viruses_found++;
 			      else {
 				  ret = CL_VIRUS;
 				  free(fullname);
 				  break;
 			      }
 			  }
e3aaff8e
 		    }
72ce4b70
 		    free(fullname);
e3aaff8e
 		}
 	    }
 	}
     } else {
2b259453
 	cli_dbgmsg("VBADir: Can't open directory %s.\n", dirname);
e3aaff8e
 	return CL_EOPEN;
     }
 
     closedir(dd);
6bee45b3
     if(BLOCK_MACROS && hasmacros) {
fb0a54dd
 	cli_append_virus(ctx, "Heuristics.OLE2.ContainsMacros");
6bee45b3
 	ret = CL_VIRUS;
fb0a54dd
 	viruses_found++;
6bee45b3
     }
fb0a54dd
     if (SCAN_ALL && viruses_found)
 	return CL_VIRUS;
467f8b1e
     return ret;
 }
 
084d19aa
 static int cli_scanhtml(cli_ctx *ctx)
a92110df
 {
 	char *tempname, fullname[1024];
 	int ret=CL_CLEAN, fd;
49cc1e3c
 	fmap_t *map = *ctx->fmap;
fb0a54dd
 	unsigned int viruses_found = 0;
a92110df
 
     cli_dbgmsg("in cli_scanhtml()\n");
 
572a986c
     /* Because HTML detection is FP-prone and html_normalise_fd() needs to
      * mmap the file don't normalise files larger than 10 MB.
      */
08f0150f
 
084d19aa
     if(map->len > 10485760) {
572a986c
 	cli_dbgmsg("cli_scanhtml: exiting (file larger than 10 MB)\n");
 	return CL_CLEAN;
     }
 
33068e09
     if(!(tempname = cli_gentemp(ctx->engine->tmpdir)))
5fc380f1
 	return CL_EMEM;
 
a92110df
     if(mkdir(tempname, 0700)) {
572a986c
         cli_errmsg("cli_scanhtml: Can't create temporary directory %s\n", tempname);
f0bc32bd
 	free(tempname);
a92110df
         return CL_ETMPDIR;
     }
 
5f2fa151
     cli_dbgmsg("cli_scanhtml: using tempdir %s\n", tempname);
 
084d19aa
     html_normalise_map(map, tempname, NULL, ctx->dconf);
58481352
     snprintf(fullname, 1024, "%s"PATHSEP"nocomment.html", tempname);
b58fdfc2
     fd = open(fullname, O_RDONLY|O_BINARY);
a92110df
     if (fd >= 0) {
fb0a54dd
 	if ((ret = cli_scandesc(fd, ctx, CL_TYPE_HTML, 0, NULL, AC_SCAN_VIR, NULL)) == CL_VIRUS)
 	    viruses_found++;
 	close(fd);
a92110df
     }
 
fb0a54dd
     if((ret == CL_CLEAN || (ret == CL_VIRUS && SCAN_ALL)) && map->len < 2097152) {
08f0150f
 	    /* limit to 2 MB, we're not interesting in scanning large files in notags form */
 	    /* TODO: don't even create notags if file is over 2 MB */
58481352
 	    snprintf(fullname, 1024, "%s"PATHSEP"notags.html", tempname);
0664128a
 	    fd = open(fullname, O_RDONLY|O_BINARY);
 	    if(fd >= 0) {
fb0a54dd
 		if ((ret = cli_scandesc(fd, ctx, CL_TYPE_HTML, 0, NULL, AC_SCAN_VIR, NULL)) == CL_VIRUS) 
 		    viruses_found++;
 		close(fd);
0664128a
 	    }
a92110df
     }
 
fb0a54dd
     if(ret == CL_CLEAN || (ret == CL_VIRUS && SCAN_ALL)) {
58481352
 	    snprintf(fullname, 1024, "%s"PATHSEP"javascript", tempname);
8be1d5a4
 	    fd = open(fullname, O_RDONLY|O_BINARY);
 	    if(fd >= 0) {
fb0a54dd
 		if ((ret = cli_scandesc(fd, ctx, CL_TYPE_HTML, 0, NULL, AC_SCAN_VIR, NULL)) == CL_VIRUS)
 		    viruses_found++;
 		if (ret == CL_CLEAN || (ret == CL_VIRUS && SCAN_ALL)) {
 		    if ((ret = cli_scandesc(fd, ctx, CL_TYPE_TEXT_ASCII, 0, NULL, AC_SCAN_VIR, NULL)) == CL_VIRUS)
 			viruses_found++;
 		}
 		close(fd);
8be1d5a4
 	    }
     }
 
fb0a54dd
     if (ret == CL_CLEAN || (ret == CL_VIRUS && SCAN_ALL)) {
58481352
 	snprintf(fullname, 1024, "%s"PATHSEP"rfc2397", tempname);
cb680655
 	ret = cli_scandir(fullname, ctx);
a92110df
     }
 
33068e09
     if(!ctx->engine->keeptmp)
a92110df
         cli_rmdirs(tempname);
 
     free(tempname);
fb0a54dd
     if (SCAN_ALL && viruses_found)
 	return CL_VIRUS;
a92110df
     return ret;
 }
 
ee1b2a6c
 static int cli_scanscript(cli_ctx *ctx)
015ce4a8
 {
ee1b2a6c
 	unsigned char *buff;
2a138cc5
 	unsigned char* normalized;
015ce4a8
 	struct text_norm_state state;
 	char *tmpname = NULL;
 	int ofd = -1, ret;
60dbee52
 	struct cli_matcher *troot = ctx->engine->root[7];
e06afe8e
 	uint32_t maxpatlen = troot ? troot->maxpatlen : 0, offset = 0;
c4934f41
 	struct cli_matcher *groot = ctx->engine->root[0];
 	struct cli_ac_data gmdata, tmdata;
 	struct cli_ac_data *mdata[2];
49cc1e3c
 	fmap_t *map = *ctx->fmap;
ee1b2a6c
 	size_t at = 0;
fb0a54dd
         unsigned int viruses_found = 0;
015ce4a8
 
2a138cc5
 	cli_dbgmsg("in cli_scanscript()\n");
015ce4a8
 
ee1b2a6c
 	if(map->len > 5242880) {
3a094ba0
 		cli_dbgmsg("cli_scanscript: exiting (file larger than 5 MB)\n");
015ce4a8
 		return CL_CLEAN;
 	}
 
 	/* dump to disk only if explicitly asked to,
 	 * otherwise we can process just in-memory */
33068e09
 	if(ctx->engine->keeptmp) {
 		if((ret = cli_gentempfd(ctx->engine->tmpdir, &tmpname, &ofd))) {
015ce4a8
 			cli_dbgmsg("cli_scanscript: Can't generate temporary file/descriptor\n");
 			return ret;
 		}
22a16acb
 		cli_dbgmsg("cli_scanscript: saving normalized file to %s\n", tmpname);
015ce4a8
 	}
 
2865ac2b
 	if(!(normalized = cli_malloc(SCANBUFF + maxpatlen))) {
2a138cc5
 		cli_dbgmsg("cli_scanscript: Unable to malloc %u bytes\n", SCANBUFF);
 		return CL_EMEM;
 	}
 
2865ac2b
 	text_normalize_init(&state, normalized, SCANBUFF + maxpatlen);
015ce4a8
 	ret = CL_CLEAN;
 
aca9ea82
 	if ((ret = cli_ac_initdata(&tmdata, troot->ac_partsigs, troot->ac_lsigs, troot->ac_reloff_num, CLI_DEFAULT_AC_TRACKLEN)))
c4934f41
 	    return ret;
 
aca9ea82
 	if ((ret = cli_ac_initdata(&gmdata, groot->ac_partsigs, groot->ac_lsigs, groot->ac_reloff_num, CLI_DEFAULT_AC_TRACKLEN))) {
c4934f41
 	    cli_ac_freedata(&tmdata);
 	    return ret;
 	}
 	mdata[0] = &tmdata;
 	mdata[1] = &gmdata;
 
ee1b2a6c
 	while(1) {
 	    size_t len = MIN(map->pgsz, map->len - at);
 	    buff = fmap_need_off_once(map, at, len);
 	    at += len;
 	    if(!buff || !len || state.out_pos + len > state.out_len) {
 		/* flush if error/EOF, or too little buffer space left */
 		if((ofd != -1) && (write(ofd, state.out, state.out_pos) == -1)) {
 		    cli_errmsg("cli_scanscript: can't write to file %s\n",tmpname);
 		    close(ofd);
 		    ofd = -1;
 		    /* we can continue to scan in memory */
015ce4a8
 		}
ee1b2a6c
 		/* when we flush the buffer also scan */
 		if(cli_scanbuff(state.out, state.out_pos, offset, ctx, CL_TYPE_TEXT_ASCII, mdata) == CL_VIRUS) {
fb0a54dd
 		    if (SCAN_ALL)
 			viruses_found++;
 		    else {
 			ret = CL_VIRUS;
 			break;
 		    }
015ce4a8
 		}
ee1b2a6c
 		if(ctx->scanned)
 		    *ctx->scanned += state.out_pos / CL_COUNT_PRECISION;
 		offset += state.out_pos;
 		/* carry over maxpatlen from previous buffer */
 		if (state.out_pos > maxpatlen)
 		    memmove(state.out, state.out + state.out_pos - maxpatlen, maxpatlen); 
 		text_normalize_reset(&state);
 		state.out_pos = maxpatlen;
 	    }
 	    if(!len) break;
 	    if(text_normalize_buffer(&state, buff, len) != len) {
 		cli_dbgmsg("cli_scanscript: short read during normalizing\n");
 	    }
 	}
33068e09
 	if(ctx->engine->keeptmp) {
015ce4a8
 		free(tmpname);
 		close(ofd);
 	}
2a138cc5
 	free(normalized);
fb0a54dd
 	if(ret != CL_VIRUS || SCAN_ALL)  {
 	    if ((ret = cli_lsig_eval(ctx, troot, &tmdata, NULL, NULL)) == CL_VIRUS)
 		viruses_found++;
 	    if(ret != CL_VIRUS || SCAN_ALL)
 		if ((ret = cli_lsig_eval(ctx, groot, &gmdata, NULL, NULL)) == CL_VIRUS)
 		    viruses_found++;
60dbee52
 	}
 	cli_ac_freedata(&tmdata);
 	cli_ac_freedata(&gmdata);
015ce4a8
 
fb0a54dd
 	if (SCAN_ALL && viruses_found)
 	    return CL_VIRUS;
015ce4a8
 	return ret;
 }
 
084d19aa
 static int cli_scanhtml_utf16(cli_ctx *ctx)
bd988961
 {
084d19aa
 	char *tempname, *decoded, *buff;
bd988961
 	int ret = CL_CLEAN, fd, bytes;
084d19aa
 	size_t at = 0;
49cc1e3c
 	fmap_t *map = *ctx->fmap;
bd988961
 
     cli_dbgmsg("in cli_scanhtml_utf16()\n");
 
33068e09
     if(!(tempname = cli_gentemp(ctx->engine->tmpdir)))
5fc380f1
 	return CL_EMEM;
 
bd988961
     if((fd = open(tempname, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, S_IRWXU)) < 0) {
 	cli_errmsg("cli_scanhtml_utf16: Can't create file %s\n", tempname);
 	free(tempname);
871177cd
 	return CL_EOPEN;
bd988961
     }
 
5f2fa151
     cli_dbgmsg("cli_scanhtml_utf16: using tempfile %s\n", tempname);
 
084d19aa
     while(at < map->len) {
017bbfbf
 	bytes = MIN(map->len - at, map->pgsz * 16);
084d19aa
 	if(!(buff = fmap_need_off_once(map, at, bytes))) {
 	    close(fd);
 	    cli_unlink(tempname);
 	    free(tempname);
 	    return CL_EREAD;
 	}
017bbfbf
 	at += bytes;
bd988961
 	decoded = cli_utf16toascii(buff, bytes);
 	if(decoded) {
020ba3ce
 	    if(write(fd, decoded, bytes / 2) == -1) {
bd988961
 		cli_errmsg("cli_scanhtml_utf16: Can't write to file %s\n", tempname);
 		free(decoded);
401ea243
 		close(fd);
c0a95e0c
 		cli_unlink(tempname);
bd988961
 		free(tempname);
871177cd
 		return CL_EWRITE;
bd988961
 	    }
 	    free(decoded);
 	}
     }
 
084d19aa
     *ctx->fmap = fmap(fd, 0, 0);
     if(*ctx->fmap) {
 	ret = cli_scanhtml(ctx);
49cc1e3c
 	funmap(*ctx->fmap);
084d19aa
     } else
 	cli_errmsg("cli_scanhtml_utf16: fmap of %s failed\n", tempname);
 
     *ctx->fmap = map;
bd988961
     close(fd);
 
33068e09
     if(!ctx->engine->keeptmp) {
871177cd
 	if (cli_unlink(tempname)) ret = CL_EUNLINK;
62ae31f3
     } else
bd988961
 	cli_dbgmsg("cli_scanhtml_utf16: Decoded HTML data saved in %s\n", tempname);
     free(tempname);
 
     return ret;
 }
 
034c02fd
 static int cli_scanole2(cli_ctx *ctx)
467f8b1e
 {
56bfccb2
 	char *dir;
 	int ret = CL_CLEAN;
72ce4b70
 	struct uniq *vba = NULL;
2b259453
 
467f8b1e
     cli_dbgmsg("in cli_scanole2()\n");
 
724b2bf7
     if(ctx->engine->maxreclevel && ctx->recursion >= ctx->engine->maxreclevel)
bbd6ca3f
         return CL_EMAXREC;
 
467f8b1e
     /* generate the temporary directory */
33068e09
     if(!(dir = cli_gentemp(ctx->engine->tmpdir)))
5fc380f1
 	return CL_EMEM;
 
467f8b1e
     if(mkdir(dir, 0700)) {
2b259453
 	cli_dbgmsg("OLE2: Can't create temporary directory %s\n", dir);
f0bc32bd
 	free(dir);
467f8b1e
 	return CL_ETMPDIR;
     }
 
034c02fd
     ret = cli_ole2_extract(dir, ctx, &vba);
72ce4b70
     if(ret!=CL_CLEAN && ret!=CL_VIRUS) {
2b259453
 	cli_dbgmsg("OLE2: %s\n", cl_strerror(ret));
33068e09
 	if(!ctx->engine->keeptmp)
8d5e0ac0
 	    cli_rmdirs(dir);
467f8b1e
 	free(dir);
 	return ret;
     }
 
72ce4b70
     if (vba) {
         ctx->recursion++;
bbd6ca3f
 
72ce4b70
 	ret = cli_vba_scandir(dir, ctx, vba);
53321a4c
 	uniq_free(vba);
72ce4b70
 	if(ret != CL_VIRUS)
cb680655
 	    if(cli_scandir(dir, ctx) == CL_VIRUS)
72ce4b70
 	        ret = CL_VIRUS;
 	ctx->recursion--;
467f8b1e
     }
 
33068e09
     if(!ctx->engine->keeptmp)
8d5e0ac0
 	cli_rmdirs(dir);
467f8b1e
     free(dir);
     return ret;
e3aaff8e
 }
 
3c91998b
 static int cli_scantar(int desc, cli_ctx *ctx, unsigned int posix)
2b259453
 {
 	char *dir;
 	int ret = CL_CLEAN;
 
 
     cli_dbgmsg("in cli_scantar()\n");
 
     /* generate temporary directory */
33068e09
     if(!(dir = cli_gentemp(ctx->engine->tmpdir)))
5fc380f1
 	return CL_EMEM;
 
2b259453
     if(mkdir(dir, 0700)) {
 	cli_errmsg("Tar: Can't create temporary directory %s\n", dir);
f0bc32bd
 	free(dir);
2b259453
 	return CL_ETMPDIR;
     }
 
d0d1afd7
     ret = cli_untar(dir, desc, posix, ctx);
2b259453
 
33068e09
     if(!ctx->engine->keeptmp)
2b259453
 	cli_rmdirs(dir);
 
     free(dir);
     return ret;
 }
 
3c91998b
 static int cli_scanmschm(int desc, cli_ctx *ctx)
a5373b64
 {
b865e44a
 	int ret = CL_CLEAN, rc;
 	chm_metadata_t metadata;
 	char *dir;
fb0a54dd
 	unsigned int viruses_found = 0;
a5373b64
 
b865e44a
     cli_dbgmsg("in cli_scanmschm()\n");
a5373b64
 
b865e44a
      /* generate the temporary directory */
33068e09
     if(!(dir = cli_gentemp(ctx->engine->tmpdir)))
5fc380f1
 	return CL_EMEM;
 
b865e44a
     if(mkdir(dir, 0700)) {
 	cli_dbgmsg("CHM: Can't create temporary directory %s\n", dir);
 	free(dir);
a5373b64
 	return CL_ETMPDIR;
     }
 
8df99a92
     ret = cli_chm_open(desc, dir, &metadata, ctx);
b865e44a
     if (ret != CL_SUCCESS) {
33068e09
 	if(!ctx->engine->keeptmp)
b865e44a
 	    cli_rmdirs(dir);
 	free(dir);
 	cli_dbgmsg("CHM: Error: %s\n", cl_strerror(ret));
 	return ret;
     }
 
    do {
8bcbfe4a
 	ret = cli_chm_prepare_file(&metadata);
b865e44a
 	if (ret != CL_SUCCESS) {
 	   break;
 	}
8bcbfe4a
 	ret = cli_chm_extract_file(dir, &metadata, ctx);
b865e44a
 	if (ret == CL_SUCCESS) {
 	    lseek(metadata.ofd, 0, SEEK_SET);
 	    rc = cli_magic_scandesc(metadata.ofd, ctx);
 	    close(metadata.ofd);
 	    if (rc == CL_VIRUS) {
fb0a54dd
 		cli_dbgmsg("CHM: infected with %s\n", cli_get_last_virus(ctx));
 		if (SCAN_ALL)
 		    viruses_found++;
 		else {
 		    ret = CL_VIRUS;
 		    break;
 		}
b865e44a
 	    }
 	}
a5373b64
 
b865e44a
     } while(ret == CL_SUCCESS);
3c9e2d22
 
     cli_chm_close(&metadata);
    
33068e09
     if(!ctx->engine->keeptmp)
b865e44a
 	cli_rmdirs(dir);
 
     free(dir);
 
     cli_dbgmsg("CHM: Exit code: %d\n", ret);
     if (ret == CL_BREAK)
 	ret = CL_CLEAN;
a5373b64
 
fb0a54dd
     if (SCAN_ALL && viruses_found)
 	return CL_VIRUS;
a5373b64
     return ret;
 }
 
3c91998b
 static int cli_scanscrenc(int desc, cli_ctx *ctx)
e57fa318
 {
 	char *tempname;
 	int ret = CL_CLEAN;
 
     cli_dbgmsg("in cli_scanscrenc()\n");
 
33068e09
     if(!(tempname = cli_gentemp(ctx->engine->tmpdir)))
5fc380f1
 	return CL_EMEM;
 
e57fa318
     if(mkdir(tempname, 0700)) {
 	cli_dbgmsg("CHM: Can't create temporary directory %s\n", tempname);
f0bc32bd
 	free(tempname);
e57fa318
 	return CL_ETMPDIR;
     }
 
     if (html_screnc_decode(desc, tempname))
cb680655
 	ret = cli_scandir(tempname, ctx);
e57fa318
 
33068e09
     if(!ctx->engine->keeptmp)
e57fa318
 	cli_rmdirs(tempname);
 
     free(tempname);
     return ret;
 }
015e31e1
 
e4101980
 static int cli_scanriff(int desc, cli_ctx *ctx)
eb308794
 {
 	int ret = CL_CLEAN;
 
     if(cli_check_riff_exploit(desc) == 2) {
6416cdef
 	ret = CL_VIRUS;
fb0a54dd
 	cli_append_virus(ctx, "Heuristics.Exploit.W32.MS05-002");
eb308794
     }
 
     return ret;
 }
 
be63d0ad
 static int cli_scanjpeg(int desc, cli_ctx *ctx)
7afdc309
 {
 	int ret = CL_CLEAN;
 
be63d0ad
     if(cli_check_jpeg_exploit(desc, ctx) == 1) {
6416cdef
 	ret = CL_VIRUS;
fb0a54dd
 	cli_append_virus(ctx, "Heuristics.Exploit.W32.MS04-028");
7afdc309
     }
 
     return ret;
 }
 
3c91998b
 static int cli_scancryptff(int desc, cli_ctx *ctx)
2c6f9d57
 {
e12c29d2
 	int ret = CL_CLEAN, ndesc;
 	unsigned int length, i;
2c6f9d57
 	unsigned char *src = NULL, *dest = NULL;
 	char *tempfile;
 	struct stat sb;
 
 
     if(fstat(desc, &sb) == -1) {
a687d96c
 	cli_errmsg("CryptFF: Can't fstat descriptor %d\n", desc);
871177cd
 	return CL_ESTAT;
2c6f9d57
     }
 
     /* Skip the CryptFF file header */
     if(lseek(desc, 0x10, SEEK_SET) < 0) {
a687d96c
 	cli_errmsg("CryptFF: Can't lseek descriptor %d\n", desc);
2c6f9d57
 	return ret;
     }
 
     length = sb.st_size  - 0x10;
  
0d0fc120
     if((dest = (unsigned char *) cli_malloc(length)) == NULL) {
2c6f9d57
 	cli_dbgmsg("CryptFF: Can't allocate memory\n");
         return CL_EMEM;
     }
 
0d0fc120
     if((src = (unsigned char *) cli_malloc(length)) == NULL) {
2c6f9d57
 	cli_dbgmsg("CryptFF: Can't allocate memory\n");
 	free(dest);
         return CL_EMEM;
     }
 
     if((unsigned int) read(desc, src, length) != length) {
 	cli_dbgmsg("CryptFF: Can't read from descriptor %d\n", desc);
 	free(dest);
 	free(src);
871177cd
 	return CL_EREAD;
2c6f9d57
     }
 
     for(i = 0; i < length; i++)
 	dest[i] = src[i] ^ (unsigned char) 0xff;
 
     free(src);
 
33068e09
     if(!(tempfile = cli_gentemp(ctx->engine->tmpdir))) {
5fc380f1
 	free(dest);
 	return CL_EMEM;
     }
 
b58fdfc2
     if((ndesc = open(tempfile, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, S_IRWXU)) < 0) {
2c6f9d57
 	cli_errmsg("CryptFF: Can't create file %s\n", tempfile);
 	free(dest);
 	free(tempfile);
871177cd
 	return CL_ECREAT;
2c6f9d57
     }
 
     if(write(ndesc, dest, length) == -1) {
 	cli_dbgmsg("CryptFF: Can't write to descriptor %d\n", ndesc);
 	free(dest);
 	close(ndesc);
 	free(tempfile);
871177cd
 	return CL_EWRITE;
2c6f9d57
     }
 
     free(dest);
 
     lseek(ndesc, 0, SEEK_SET);
 
     cli_dbgmsg("CryptFF: Scanning decrypted data\n");
 
3c91998b
     if((ret = cli_magic_scandesc(ndesc, ctx)) == CL_VIRUS)
fb0a54dd
 	cli_dbgmsg("CryptFF: Infected with %s\n", cli_get_last_virus_str(ctx));
2c6f9d57
 
     close(ndesc);
 
33068e09
     if(ctx->engine->keeptmp)
2c6f9d57
 	cli_dbgmsg("CryptFF: Decompressed data saved in %s\n", tempfile);
     else
871177cd
 	if (cli_unlink(tempfile)) ret = CL_EUNLINK;
2c6f9d57
 
     free(tempfile);
     return ret;
 }
 
2d5dbc37
 static int cli_scanpdf(cli_ctx *ctx, off_t offset)
798308de
 {
 	int ret;
33068e09
 	char *dir = cli_gentemp(ctx->engine->tmpdir);
798308de
 
5fc380f1
     if(!dir)
 	return CL_EMEM;
798308de
 
     if(mkdir(dir, 0700)) {
 	cli_dbgmsg("Can't create temporary directory for PDF file %s\n", dir);
 	free(dir);
 	return CL_ETMPDIR;
     }
 
2d5dbc37
     ret = cli_pdf(dir, ctx, offset);
798308de
 
33068e09
     if(!ctx->engine->keeptmp)
798308de
 	cli_rmdirs(dir);
 
     free(dir);
     return ret;
 }
 
3c91998b
 static int cli_scantnef(int desc, cli_ctx *ctx)
f0bc32bd
 {
 	int ret;
33068e09
 	char *dir = cli_gentemp(ctx->engine->tmpdir);
f0bc32bd
 
5fc380f1
     if(!dir)
 	return CL_EMEM;
f0bc32bd
 
     if(mkdir(dir, 0700)) {
 	cli_dbgmsg("Can't create temporary directory for tnef file %s\n", dir);
 	free(dir);
 	return CL_ETMPDIR;
     }
 
33068e09
     ret = cli_tnef(dir, desc, ctx);
f0bc32bd
 
     if(ret == CL_CLEAN)
cb680655
 	ret = cli_scandir(dir, ctx);
f0bc32bd
 
33068e09
     if(!ctx->engine->keeptmp)
f0bc32bd
 	cli_rmdirs(dir);
 
     free(dir);
     return ret;
 }
 
ee1b2a6c
 static int cli_scanuuencoded(cli_ctx *ctx)
3953039b
 {
 	int ret;
33068e09
 	char *dir = cli_gentemp(ctx->engine->tmpdir);
3953039b
 
5fc380f1
     if(!dir)
 	return CL_EMEM;
 
3953039b
     if(mkdir(dir, 0700)) {
 	cli_dbgmsg("Can't create temporary directory for uuencoded file %s\n", dir);
 	free(dir);
 	return CL_ETMPDIR;
     }
 
ee1b2a6c
     ret = cli_uuencode(dir, *ctx->fmap);
3953039b
 
     if(ret == CL_CLEAN)
cb680655
 	ret = cli_scandir(dir, ctx);
3953039b
 
33068e09
     if(!ctx->engine->keeptmp)
3953039b
 	cli_rmdirs(dir);
 
     free(dir);
     return ret;
 }
 
3c91998b
 static int cli_scanmail(int desc, cli_ctx *ctx)
e3aaff8e
 {
 	char *dir;
 	int ret;
fb0a54dd
 	unsigned int viruses_found = 0;
e3aaff8e
 
d91ab809
     cli_dbgmsg("Starting cli_scanmail(), recursion = %u\n", ctx->recursion);
e3aaff8e
 
0c7019c5
     /* generate the temporary directory */
33068e09
     if(!(dir = cli_gentemp(ctx->engine->tmpdir)))
5fc380f1
 	return CL_EMEM;
 
0c7019c5
     if(mkdir(dir, 0700)) {
 	cli_dbgmsg("Mail: Can't create temporary directory %s\n", dir);
 	free(dir);
 	return CL_ETMPDIR;
     }
e3aaff8e
 
0c7019c5
     /*
      * Extract the attachments into the temporary directory
      */
0f7f7682
     if((ret = cli_mbox(dir, desc, ctx))) {
fb0a54dd
 	if (ret == CL_VIRUS && SCAN_ALL)
 	    viruses_found++;
 	else {
 	    if(!ctx->engine->keeptmp)
 		cli_rmdirs(dir);
 	    free(dir);
 	    return ret;
 	}
0c7019c5
     }
 
cb680655
     ret = cli_scandir(dir, ctx);
0c7019c5
 
33068e09
     if(!ctx->engine->keeptmp)
0c7019c5
 	cli_rmdirs(dir);
 
     free(dir);
fb0a54dd
     if (SCAN_ALL && viruses_found)
 	return CL_VIRUS;
0c7019c5
     return ret;
e3aaff8e
 }
 
a6e38800
 static int cli_scan_structured(int desc, cli_ctx *ctx)
 {
 	char buf[8192];
 	int result = 0;
 	unsigned int cc_count = 0;
 	unsigned int ssn_count = 0;
 	int done = 0;
 	int (*ccfunc)(const unsigned char *buffer, int length);
 	int (*ssnfunc)(const unsigned char *buffer, int length);
fb0a54dd
 	unsigned int viruses_found = 0;
a6e38800
 
724b2bf7
     if(ctx == NULL)
a6e38800
 	return CL_ENULLARG;
 
724b2bf7
     if(ctx->engine->min_cc_count == 1)
a6e38800
 	ccfunc = dlp_has_cc;
     else
 	ccfunc = dlp_get_cc_count;
 
26fbf6bd
     switch((ctx->options & CL_SCAN_STRUCTURED_SSN_NORMAL) | (ctx->options & CL_SCAN_STRUCTURED_SSN_STRIPPED)) {
a6e38800
 
26fbf6bd
 	case (CL_SCAN_STRUCTURED_SSN_NORMAL | CL_SCAN_STRUCTURED_SSN_STRIPPED):
724b2bf7
 	    if(ctx->engine->min_ssn_count == 1)
a6e38800
 		ssnfunc = dlp_has_ssn;
 	    else
 		ssnfunc = dlp_get_ssn_count;
 	    break;
 
26fbf6bd
 	case CL_SCAN_STRUCTURED_SSN_NORMAL:
724b2bf7
 	    if(ctx->engine->min_ssn_count == 1)
a6e38800
 		ssnfunc = dlp_has_normal_ssn;
 	    else
 		ssnfunc = dlp_get_normal_ssn_count;
 	    break;
 
26fbf6bd
 	case CL_SCAN_STRUCTURED_SSN_STRIPPED:
724b2bf7
 	    if(ctx->engine->min_ssn_count == 1)
a6e38800
 		ssnfunc = dlp_has_stripped_ssn;
 	    else
 		ssnfunc = dlp_get_stripped_ssn_count;
 	    break;
26fbf6bd
 
 	default:
 	    ssnfunc = NULL;
a6e38800
     }
 
26fbf6bd
     while(!done && ((result = cli_readn(desc, buf, 8191)) > 0)) {
724b2bf7
 	if((cc_count += ccfunc((const unsigned char *)buf, result)) >= ctx->engine->min_cc_count)
a6e38800
 	    done = 1;
26fbf6bd
 
724b2bf7
 	if(ssnfunc && ((ssn_count += ssnfunc((const unsigned char *)buf, result)) >= ctx->engine->min_ssn_count))
a6e38800
 	    done = 1;
     }
 
724b2bf7
     if(cc_count != 0 && cc_count >= ctx->engine->min_cc_count) {
a6e38800
 	cli_dbgmsg("cli_scan_structured: %u credit card numbers detected\n", cc_count);
fb0a54dd
 	cli_append_virus(ctx,"Heuristics.Structured.CreditCardNumber");
 	if (SCAN_ALL)
 	    viruses_found++;
 	else
 	    return CL_VIRUS;
a6e38800
     }
 
724b2bf7
     if(ssn_count != 0 && ssn_count >= ctx->engine->min_ssn_count) {
a6e38800
 	cli_dbgmsg("cli_scan_structured: %u social security numbers detected\n", ssn_count);
fb0a54dd
 	cli_append_virus(ctx,"Heuristics.Structured.SSN");
 	if (SCAN_ALL)
 	    viruses_found++;
 	else
 	    return CL_VIRUS;
a6e38800
     }
 
fb0a54dd
     if (SCAN_ALL && viruses_found)
 	return CL_VIRUS;
a6e38800
     return CL_CLEAN;
 }
 
822160b5
 static int cli_scanembpe(cli_ctx *ctx, off_t offset)
ee99255a
 {
 	int fd, bytes, ret = CL_CLEAN;
822160b5
 	unsigned long int size = 0, todo;
 	char *buff;
ee99255a
 	char *tmpname;
822160b5
 	fmap_t *map = *ctx->fmap;
cfe6b4a2
 	unsigned int corrupted_input;
ee99255a
 
33068e09
     tmpname = cli_gentemp(ctx->engine->tmpdir);
ee99255a
     if(!tmpname)
 	return CL_EMEM;
 
     if((fd = open(tmpname, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, S_IRWXU)) < 0) {
 	cli_errmsg("cli_scanembpe: Can't create file %s\n", tmpname);
 	free(tmpname);
871177cd
 	return CL_ECREAT;
ee99255a
     }
 
822160b5
     todo = map->len - offset;
     while(1) {
 	bytes = MIN(todo, map->pgsz);
 	if(!bytes)
 	    break;
 
 	if(!(buff = fmap_need_off_once(map, offset + size, bytes))) {
 	    close(fd);
 	    if(!ctx->engine->keeptmp) {
 		if (cli_unlink(tmpname)) {
 		    free(tmpname);
 		    return CL_EUNLINK;
 		}
 	    }
 	    free(tmpname);
 	    return CL_EREAD;
 	}
ee99255a
 	size += bytes;
822160b5
 	todo -= bytes;
ee99255a
 
822160b5
 	if(cli_checklimits("cli_scanembpe", ctx, size, 0, 0)!=CL_CLEAN)
ee99255a
 	    break;
 
 	if(cli_writen(fd, buff, bytes) != bytes) {
 	    cli_dbgmsg("cli_scanembpe: Can't write to temporary file\n");
 	    close(fd);
33068e09
 	    if(!ctx->engine->keeptmp) {
997a0e0b
 		if (cli_unlink(tmpname)) {
 		    free(tmpname);
871177cd
 		    return CL_EUNLINK;
997a0e0b
 		}
 	    }
822160b5
 	    free(tmpname);
871177cd
 	    return CL_EWRITE;
ee99255a
 	}
     }
 
f0f7f92f
     ctx->recursion++;
ee99255a
     lseek(fd, 0, SEEK_SET);
cfe6b4a2
     corrupted_input = ctx->corrupted_input;
     ctx->corrupted_input = 1;
     ret = cli_magic_scandesc(fd, ctx);
     ctx->corrupted_input = corrupted_input;
     if(ret == CL_VIRUS) {
fb0a54dd
 	cli_dbgmsg("cli_scanembpe: Infected with %s\n", cli_get_last_virus(ctx));
ee99255a
 	close(fd);
33068e09
 	if(!ctx->engine->keeptmp) {
997a0e0b
 	    if (cli_unlink(tmpname)) {
 	    	free(tmpname);
871177cd
 		return CL_EUNLINK;
997a0e0b
 	    }
 	}
ee99255a
 	free(tmpname);	
 	return CL_VIRUS;
     }
f0f7f92f
     ctx->recursion--;
ee99255a
 
     close(fd);
33068e09
     if(!ctx->engine->keeptmp) {
997a0e0b
 	if (cli_unlink(tmpname)) {
 	    free(tmpname);
871177cd
 	    return CL_EUNLINK;
997a0e0b
 	}
     }
ee99255a
     free(tmpname);
 
     /* intentionally ignore possible errors from cli_magic_scandesc */
     return CL_CLEAN;
 }
 
548b55be
 static int cli_scanraw(cli_ctx *ctx, cli_file_t type, uint8_t typercg, cli_file_t *dettype, unsigned char *refhash)
555c5390
 {
6191ee2c
 	int ret = CL_CLEAN, nret = CL_CLEAN;
555c5390
 	struct cli_matched_type *ftoffset = NULL, *fpt;
994ae437
 	uint32_t lastzip, lastrar;
ee99255a
 	struct cli_exe_info peinfo;
6038397e
 	unsigned int acmode = AC_SCAN_VIR, break_loop = 0;
49cc1e3c
 	fmap_t *map = *ctx->fmap;
a5b0f84b
 	cli_file_t current_container_type = ctx->container_type;
 	size_t current_container_size = ctx->container_size;
555c5390
 
 
8df99a92
     if(ctx->engine->maxreclevel && ctx->recursion >= ctx->engine->maxreclevel)
         return CL_EMAXREC;
 
     if(typercg)
 	acmode |= AC_SCAN_FT;
555c5390
 
ffa9b060
     ret = cli_fmap_scandesc(ctx, type == CL_TYPE_TEXT_ASCII ? 0 : type, 0, &ftoffset, acmode, NULL, refhash);
555c5390
 
c8b0d9a2
     if(ret >= CL_TYPENO) {
8df99a92
 	ctx->recursion++;
 	if(nret != CL_VIRUS) {
d68a73d1
 	    lastzip = lastrar = 0xdeadbeef;
 	    fpt = ftoffset;
 	    while(fpt) {
8df99a92
 		if(fpt->offset) switch(fpt->type) {
d68a73d1
 		    case CL_TYPE_RARSFX:
09b29e1e
 			if(type != CL_TYPE_RAR && have_rar && SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_RAR)) {
a5b0f84b
 			    ctx->container_type = CL_TYPE_RAR;
 			    ctx->container_size = map->len - fpt->offset; /* not precise */
8df99a92
 			    cli_dbgmsg("RAR/RAR-SFX signature found at %u\n", (unsigned int) fpt->offset);
2d5dbc37
 			    nret = cli_scanrar(map->fd, ctx, fpt->offset, &lastrar);
d68a73d1
 			}
 			break;
555c5390
 
d68a73d1
 		    case CL_TYPE_ZIPSFX:
09b29e1e
 			if(type != CL_TYPE_ZIP && SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_ZIP)) {
a5b0f84b
 			    ctx->container_type = CL_TYPE_ZIP;
 			    ctx->container_size = map->len - fpt->offset; /* not precise */
8df99a92
 			    cli_dbgmsg("ZIP/ZIP-SFX signature found at %u\n", (unsigned int) fpt->offset);
2d5dbc37
 			    nret = cli_unzip_single(ctx, fpt->offset);
d68a73d1
 			}
 			break;
d8a5c616
 
d68a73d1
 		    case CL_TYPE_CABSFX:
09b29e1e
 			if(type != CL_TYPE_MSCAB && SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_CAB)) {
a5b0f84b
 			    ctx->container_type = CL_TYPE_MSCAB;
 			    ctx->container_size = map->len - fpt->offset; /* not precise */
8df99a92
 			    cli_dbgmsg("CAB/CAB-SFX signature found at %u\n", (unsigned int) fpt->offset);
2d5dbc37
 			    nret = cli_scanmscab(map->fd, ctx, fpt->offset);
555c5390
 			}
d68a73d1
 			break;
9d96e4b6
 		    case CL_TYPE_ARJSFX:
09b29e1e
 			if(type != CL_TYPE_ARJ && SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_ARJ)) {
a5b0f84b
 			    ctx->container_type = CL_TYPE_ARJ;
 			    ctx->container_size = map->len - fpt->offset; /* not precise */
9d96e4b6
 			    cli_dbgmsg("ARJ-SFX signature found at %u\n", (unsigned int) fpt->offset);
2d5dbc37
 			    nret = cli_scanarj(map->fd, ctx, fpt->offset, &lastrar);
9d96e4b6
 			}
 			break;
555c5390
 
faaf436a
 		    case CL_TYPE_NULSFT:
8fb8d069
 		        if(SCAN_ARCHIVE && type == CL_TYPE_MSEXE && (DCONF_ARCH & ARCH_CONF_NSIS) && fpt->offset > 4) {
a5b0f84b
 			    ctx->container_type = CL_TYPE_NULSFT;
 			    ctx->container_size = map->len - fpt->offset; /* not precise */
faaf436a
 			    cli_dbgmsg("NSIS signature found at %u\n", (unsigned int) fpt->offset-4);
2d5dbc37
 			    nret = cli_scannulsft(map->fd, ctx, fpt->offset - 4);
faaf436a
 			}
 			break;
 
ed93f138
 		    case CL_TYPE_AUTOIT:
7c06afc6
 		        if(SCAN_ARCHIVE && type == CL_TYPE_MSEXE && (DCONF_ARCH & ARCH_CONF_AUTOIT)) {
a5b0f84b
 			    ctx->container_type = CL_TYPE_AUTOIT;
 			    ctx->container_size = map->len - fpt->offset; /* not precise */
ed93f138
 			    cli_dbgmsg("AUTOIT signature found at %u\n", (unsigned int) fpt->offset);
ac867aad
 			    nret = cli_scanautoit(ctx, fpt->offset + 23);
ed93f138
 			}
 			break;
 
cadaa703
 		    case CL_TYPE_ISHIELD_MSI:
9981dfc4
 		        if(SCAN_ARCHIVE && type == CL_TYPE_MSEXE && (DCONF_ARCH & ARCH_CONF_ISHIELD)) {
a5b0f84b
 			    ctx->container_type = CL_TYPE_AUTOIT;
 			    ctx->container_size = map->len - fpt->offset; /* not precise */
cadaa703
 			    cli_dbgmsg("ISHIELD-MSI signature found at %u\n", (unsigned int) fpt->offset);
9a4da6af
 			    nret = cli_scanishield_msi(ctx, fpt->offset + 14);
cadaa703
 			}
 			break;
 
72ce4b70
 		    case CL_TYPE_PDF:
8df99a92
 			if(type != CL_TYPE_PDF && SCAN_PDF && (DCONF_DOC & DOC_CONF_PDF)) {
a5b0f84b
 			    ctx->container_type = CL_TYPE_PDF;
 			    ctx->container_size = map->len - fpt->offset; /* not precise */
72ce4b70
 			    cli_dbgmsg("PDF signature found at %u\n", (unsigned int) fpt->offset);
2d5dbc37
 			    nret = cli_scanpdf(ctx, fpt->offset);
72ce4b70
 			}
 			break;
 
d68a73d1
 		    case CL_TYPE_MSEXE:
8df99a92
  			if(SCAN_PE && (type == CL_TYPE_MSEXE || type == CL_TYPE_ZIP || type == CL_TYPE_MSOLE2) && ctx->dconf->pe) {
2d5dbc37
 			    if(map->len > 10485760)
198c851c
 				break;
a5b0f84b
 			    ctx->container_type = CL_TYPE_MSEXE; /* PE is a container for another executable here */
 			    ctx->container_size = map->len - fpt->offset; /* not precise */
ee99255a
 			    memset(&peinfo, 0, sizeof(struct cli_exe_info));
 			    peinfo.offset = fpt->offset;
048d7677
 			    if(cli_peheader(map, &peinfo) == 0) {
198c851c
 				cli_dbgmsg("*** Detected embedded PE file at %u ***\n", (unsigned int) fpt->offset);
ee99255a
 				if(peinfo.section)
 				    free(peinfo.section);
5ba28e73
 				cli_hashset_destroy(&peinfo.vinfo);
ee99255a
 
822160b5
 				nret = cli_scanembpe(ctx, fpt->offset);
d68a73d1
 				break_loop = 1; /* we can stop here and other
 						 * embedded executables will
 						 * be found recursively
 						 * through the above call
 						 */
ee99255a
 			    }
 			}
d68a73d1
 			break;
 
 		    default:
 			cli_warnmsg("cli_scanraw: Type %u not handled in fpt loop\n", fpt->type);
ee99255a
 		}
d68a73d1
 
 		if(nret == CL_VIRUS || break_loop)
 		    break;
 
 		fpt = fpt->next;
 	    }
a5b0f84b
 	    ctx->container_type = current_container_type;
 	    ctx->container_size = current_container_size;
d68a73d1
 	}
 
 	if(nret != CL_VIRUS) switch(ret) {
 	    case CL_TYPE_HTML:
a810ba50
 		if(SCAN_HTML && type == CL_TYPE_TEXT_ASCII && (DCONF_DOC & DOC_CONF_HTML)) {
 		    *dettype = CL_TYPE_HTML;
084d19aa
 		    nret = cli_scanhtml(ctx);
a810ba50
 		}
d68a73d1
 		break;
 
 	    case CL_TYPE_MAIL:
a5b0f84b
 		ctx->container_type = CL_TYPE_MAIL;
 		ctx->container_size = map->len;
ee50848a
 		if(SCAN_MAIL && type == CL_TYPE_TEXT_ASCII && (DCONF_MAIL & MAIL_CONF_MBOX)) {
 		    *dettype = CL_TYPE_MAIL;
2d5dbc37
 		    nret = cli_scanmail(map->fd, ctx);
ee50848a
 		}
a5b0f84b
 		ctx->container_type = current_container_type;
 		ctx->container_size = current_container_size;
ee99255a
 		break;
 
555c5390
 	    default:
 		break;
 	}
d91ab809
 	ctx->recursion--;
6191ee2c
 	ret = nret;
555c5390
     }
 
c8b0d9a2
     while(ftoffset) {
 	fpt = ftoffset;
 	ftoffset = ftoffset->next;
 	free(fpt);
     }
 
     if(ret == CL_VIRUS)
fb0a54dd
 	cli_dbgmsg("%s found\n", cli_get_last_virus(ctx));
c8b0d9a2
 
555c5390
     return ret;
 }
 
aef8d4ac
 
742b3a0e
 static void emax_reached(cli_ctx *ctx) {
     fmap_t **ctx_fmap = ctx->fmap;
b6c03fb9
     if (!ctx_fmap)
 	return;
742b3a0e
     while(*ctx_fmap) {
 	fmap_t *map = *ctx_fmap;
 	map->dont_cache_flag = 1;
 	ctx_fmap--;
     }
     cli_dbgmsg("emax_reached: marked parents as non cacheable\n");
 }
 
aef8d4ac
 #define LINESTR(x) #x
 #define LINESTR2(x) LINESTR(x)
 #define __AT__  " at line "LINESTR2(__LINE__)
eb422a03
 #define ret_from_magicscan(retcode) do {							\
     cli_dbgmsg("cli_magic_scandesc: returning %d %s\n", retcode, __AT__);			\
     if(ctx->engine->cb_post_scan) {								\
fb0a54dd
 	switch(ctx->engine->cb_post_scan(desc, retcode, retcode == CL_VIRUS ? cli_get_last_virus(ctx) : NULL, ctx->cb_ctx)) { \
eb422a03
 	case CL_BREAK:										\
 	    cli_dbgmsg("cli_magic_scandesc: file whitelisted by callback\n");			\
 	    return CL_CLEAN;									\
 	case CL_VIRUS:										\
 	    cli_dbgmsg("cli_magic_scandesc: file blacklisted by callback\n");			\
fb0a54dd
 	    cli_append_virus(ctx, "Detected.By.Callback");					\
eb422a03
 	    return CL_VIRUS;									\
 	case CL_CLEAN:										\
 	    break;										\
 	default:										\
 	    cli_warnmsg("cli_magic_scandesc: ignoring bad return code from callback\n");	\
 	}											\
769f37a6
     }\
eb422a03
     return retcode;										\
aef8d4ac
     } while(0)
742b3a0e
 
7770d314
 static int magic_scandesc(int desc, cli_ctx *ctx, cli_file_t type)
e3aaff8e
 {
c0a16479
 	int ret = CL_CLEAN, res;
7770d314
 	cli_file_t dettype = 0;
a7f5fd00
 	struct stat sb;
ded60d81
 	uint8_t typercg = 1;
a5b0f84b
 	cli_file_t current_container_type = ctx->container_type;
52f52931
 	size_t current_container_size = ctx->container_size, hashed_size;
511c2e79
 	unsigned char hash[16];
f4e34215
 	bitset_t *old_hook_lsig_matches;
fb0a54dd
 	unsigned int viruses_found = 0;
a7f5fd00
 
16b28d07
 #ifdef HAVE__INTERNAL__SHA_COLLECT
     if(ctx->sha_collect>0) ctx->sha_collect = 0;
 #endif
 
aef8d4ac
     cli_dbgmsg("in cli_magic_scandesc (reclevel: %u/%u)\n", ctx->recursion, ctx->engine->maxreclevel);
8df99a92
     if(ctx->engine->maxreclevel && ctx->recursion > ctx->engine->maxreclevel) {
         cli_dbgmsg("cli_magic_scandesc: Archive recursion limit exceeded (%u, max: %u)\n", ctx->recursion, ctx->engine->maxreclevel);
742b3a0e
 	emax_reached(ctx);
 	ret_from_magicscan(CL_CLEAN);
8df99a92
     }
a7f5fd00
 
     if(fstat(desc, &sb) == -1) {
c17ed38c
 	cli_errmsg("magic_scandesc: Can't fstat descriptor %d\n", desc);
aef8d4ac
 	ret_from_magicscan(CL_ESTAT);
a7f5fd00
     }
e3aaff8e
 
a7f5fd00
     if(sb.st_size <= 5) {
794f3f23
 	cli_dbgmsg("Small data (%u bytes)\n", (unsigned int) sb.st_size);
aef8d4ac
 	ret_from_magicscan(CL_CLEAN);
a7f5fd00
     }
e3aaff8e
 
3c91998b
     if(!ctx->engine) {
5612732c
 	cli_errmsg("CRITICAL: engine == NULL\n");
aef8d4ac
 	ret_from_magicscan(CL_ENULLARG);
b3df93db
     }
 
     if(!(ctx->engine->dboptions & CL_DB_COMPILED)) {
 	cli_errmsg("CRITICAL: engine not compiled\n");
aef8d4ac
 	ret_from_magicscan(CL_EMALFDB);
e3aaff8e
     }
 
742b3a0e
     if(cli_updatelimits(ctx, sb.st_size)!=CL_CLEAN) {
 	emax_reached(ctx);
         ret_from_magicscan(CL_CLEAN);
     }
e3aaff8e
 
998bcfa7
     ctx->fmap++;
     if(!(*ctx->fmap = fmap(desc, 0, sb.st_size))) {
 	cli_errmsg("CRITICAL: fmap() failed\n");
60b21251
 	ctx->fmap--;
aef8d4ac
 	ret_from_magicscan(CL_EMEM);
998bcfa7
     }
 
eb422a03
     if(ctx->engine->cb_pre_scan) {
aa7380df
 	switch(ctx->engine->cb_pre_scan(desc, ctx->cb_ctx)) {
eb422a03
 	case CL_BREAK:
 	    cli_dbgmsg("cli_magic_scandesc: file whitelisted by callback\n");
 	    funmap(*ctx->fmap);
 	    ctx->fmap--;
 	    ret_from_magicscan(CL_CLEAN);
 	case CL_VIRUS:
 	    cli_dbgmsg("cli_magic_scandesc: file blacklisted by callback\n");
fb0a54dd
 	    cli_append_virus(ctx, "Detected.By.Callback");
eb422a03
 	    funmap(*ctx->fmap);
 	    ctx->fmap--;
 	    ret_from_magicscan(CL_VIRUS);
 	case CL_CLEAN:
 	    break;
 	default:
 	    cli_warnmsg("cli_magic_scandesc: ignoring bad return code from callback\n");
 	}
     }
 
c0a16479
     res = cache_check(hash, ctx);
     if(res != CL_VIRUS) {
60b21251
 	funmap(*ctx->fmap);
 	ctx->fmap--;
c0a16479
 	ret_from_magicscan(res);
60b21251
     }
52f52931
     hashed_size = (*ctx->fmap)->len;
f4e34215
     old_hook_lsig_matches = ctx->hook_lsig_matches;
     ctx->hook_lsig_matches = NULL;
52f52931
 
fb0a54dd
     if(!(ctx->options&~CL_SCAN_ALLMATCHES) || (ctx->recursion == ctx->engine->maxreclevel)) { /* raw mode (stdin, etc.) or last level of recursion */
8df99a92
 	if(ctx->recursion == ctx->engine->maxreclevel)
 	    cli_dbgmsg("cli_magic_scandesc: Hit recursion limit, only scanning raw file\n");
 	else
 	    cli_dbgmsg("Raw mode: No support for special files\n");
ea6f1300
 
ffa9b060
 	if((ret = cli_fmap_scandesc(ctx, 0, 0, NULL, AC_SCAN_VIR, NULL, hash)) == CL_VIRUS)
fb0a54dd
 	    cli_dbgmsg("%s found in descriptor %d\n", cli_get_last_virus(ctx), desc);
aef8d4ac
 	else if(ret == CL_CLEAN) {
 	    if(ctx->recursion != ctx->engine->maxreclevel)
 		cache_add(hash, hashed_size, ctx); /* Only cache if limits are not reached */
 	    else 
742b3a0e
 		emax_reached(ctx);
aef8d4ac
 	}
 
17c20e12
 	ctx->hook_lsig_matches = old_hook_lsig_matches;
49cc1e3c
 	funmap(*ctx->fmap);
d2a0d381
 	ctx->fmap--;
aef8d4ac
 	ret_from_magicscan(ret);
d91ab809
     }
e3aaff8e
 
7770d314
     if(type == CL_TYPE_ANY)
 	type = cli_filetype2(*ctx->fmap, ctx->engine); /* FIXMEFMAP: port to fmap */
7db77fbf
     if(type == CL_TYPE_ERROR) {
 	cli_dbgmsg("cli_magic_scandesc: cli_filetype2 returned CL_TYPE_ERROR\n");
49cc1e3c
 	funmap(*ctx->fmap);
17c20e12
 	ctx->fmap--;
 	ctx->hook_lsig_matches = old_hook_lsig_matches;
aef8d4ac
 	ret_from_magicscan(CL_EREAD);
7db77fbf
     }
16b28d07
 
 #ifdef HAVE__INTERNAL__SHA_COLLECT
     if(!ctx->sha_collect && type==CL_TYPE_MSEXE) ctx->sha_collect = 1;
 #endif
998bcfa7
     lseek(desc, 0, SEEK_SET); /* FIXMEFMAP: remove ? */
6d6e8271
 
f4e34215
     ctx->hook_lsig_matches = cli_bitset_init();
17c20e12
     if (!ctx->hook_lsig_matches) {
 	ctx->hook_lsig_matches = old_hook_lsig_matches;
cbb9db19
 	ctx->fmap--;
aef8d4ac
 	ret_from_magicscan(CL_EMEM);
17c20e12
     }
f4e34215
 
7021b545
     if(type != CL_TYPE_IGNORED && ctx->engine->sdb) {
548b55be
 	if((ret = cli_scanraw(ctx, type, 0, &dettype, hash)) == CL_VIRUS) {
32b1e04e
 	    ret = cli_checkfp(hash, hashed_size, ctx);
49cc1e3c
 	    funmap(*ctx->fmap);
f4e34215
 	    ctx->fmap--;
 	    cli_bitset_free(ctx->hook_lsig_matches);
 	    ctx->hook_lsig_matches = old_hook_lsig_matches;
aef8d4ac
 	    ret_from_magicscan(ret);
998bcfa7
 	}
 	lseek(desc, 0, SEEK_SET); /* FIXMEFMAP: remove ? */
555c5390
     }
 
d91ab809
     ctx->recursion++;
467f8b1e
     switch(type) {
7021b545
 	case CL_TYPE_IGNORED:
 	    break;
 
3805ebcb
 	case CL_TYPE_RAR:
55094a9c
 	    ctx->container_type = CL_TYPE_RAR;
 	    ctx->container_size = sb.st_size;
bb7bd3ab
 	    if(have_rar && SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_RAR))
994ae437
 		ret = cli_scanrar(desc, ctx, 0, NULL);
467f8b1e
 	    break;
6d6e8271
 
3805ebcb
 	case CL_TYPE_ZIP:
55094a9c
 	    ctx->container_type = CL_TYPE_ZIP;
 	    ctx->container_size = sb.st_size;
bc93eda0
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_ZIP))
bd7f7684
 		ret = cli_unzip(ctx);
467f8b1e
 	    break;
6d6e8271
 
3805ebcb
 	case CL_TYPE_GZ:
bc93eda0
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_GZ))
686ed348
 		ret = cli_scangzip(ctx);
467f8b1e
 	    break;
6d6e8271
 
3805ebcb
 	case CL_TYPE_BZ:
bc93eda0
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_BZ))
3c91998b
 		ret = cli_scanbzip(desc, ctx);
467f8b1e
 	    break;
55094a9c
 
9d96e4b6
 	case CL_TYPE_ARJ:
55094a9c
 	    ctx->container_type = CL_TYPE_ARJ;
 	    ctx->container_size = sb.st_size;
9d96e4b6
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_ARJ))
 		ret = cli_scanarj(desc, ctx, 0, NULL);
 	    break;
6d6e8271
 
faaf436a
         case CL_TYPE_NULSFT:
55094a9c
 	    ctx->container_type = CL_TYPE_NULSFT;
 	    ctx->container_size = sb.st_size;
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_NSIS))
faaf436a
 		ret = cli_scannulsft(desc, ctx, 0);
 	    break;
ed93f138
 
         case CL_TYPE_AUTOIT:
55094a9c
 	    ctx->container_type = CL_TYPE_AUTOIT;
 	    ctx->container_size = sb.st_size;
ffa6230b
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_AUTOIT))
ac867aad
 		ret = cli_scanautoit(ctx, 23);
ed93f138
 	    break;
6e47a652
 
3805ebcb
 	case CL_TYPE_MSSZDD:
bc93eda0
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_SZDD))
3c91998b
 		ret = cli_scanszdd(desc, ctx);
341e5433
 	    break;
6e47a652
 
3805ebcb
 	case CL_TYPE_MSCAB:
55094a9c
 	    ctx->container_type = CL_TYPE_MSCAB;
 	    ctx->container_size = sb.st_size;
bc93eda0
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_CAB))
d8a5c616
 		ret = cli_scanmscab(desc, ctx, 0);
414abe87
 	    break;
 
bd988961
 	case CL_TYPE_HTML:
bc93eda0
 	    if(SCAN_HTML && (DCONF_DOC & DOC_CONF_HTML))
084d19aa
 		ret = cli_scanhtml(ctx);
bd988961
 	    break;
 
 	case CL_TYPE_HTML_UTF16:
bc93eda0
 	    if(SCAN_HTML && (DCONF_DOC & DOC_CONF_HTML))
084d19aa
 		ret = cli_scanhtml_utf16(ctx);
bd988961
 	    break;
 
015ce4a8
 	case CL_TYPE_SCRIPT:
a810ba50
 	    if((DCONF_DOC & DOC_CONF_SCRIPT) && dettype != CL_TYPE_HTML)
ee1b2a6c
 	        ret = cli_scanscript(ctx);
015ce4a8
 	    break;
 
52c2a8bd
 	case CL_TYPE_RTF:
55094a9c
 	    ctx->container_type = CL_TYPE_RTF;
 	    ctx->container_size = sb.st_size;
d91ab809
 	    if(SCAN_ARCHIVE && (DCONF_DOC & DOC_CONF_RTF))
bc93eda0
 		ret = cli_scanrtf(desc, ctx);
52c2a8bd
 	    break;
 
3805ebcb
 	case CL_TYPE_MAIL:
55094a9c
 	    ctx->container_type = CL_TYPE_MAIL;
 	    ctx->container_size = sb.st_size;
bc93eda0
 	    if(SCAN_MAIL && (DCONF_MAIL & MAIL_CONF_MBOX))
3c91998b
 		ret = cli_scanmail(desc, ctx);
467f8b1e
 	    break;
6d6e8271
 
846e4186
 	case CL_TYPE_TNEF:
bc93eda0
 	    if(SCAN_MAIL && (DCONF_MAIL & MAIL_CONF_TNEF))
3c91998b
 		ret = cli_scantnef(desc, ctx);
846e4186
 	    break;
 
3953039b
 	case CL_TYPE_UUENCODED:
bc93eda0
 	    if(DCONF_OTHER & OTHER_CONF_UUENC)
ee1b2a6c
 		ret = cli_scanuuencoded(ctx);
3953039b
 	    break;
 
3805ebcb
 	case CL_TYPE_MSCHM:
55094a9c
 	    ctx->container_type = CL_TYPE_MSCHM;
 	    ctx->container_size = sb.st_size;
bc93eda0
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_CHM))
3c91998b
 		ret = cli_scanmschm(desc, ctx);
a5373b64
 	    break;
 
3805ebcb
 	case CL_TYPE_MSOLE2:
55094a9c
 	    ctx->container_type = CL_TYPE_MSOLE2;
 	    ctx->container_size = sb.st_size;
bc93eda0
 	    if(SCAN_OLE2 && (DCONF_ARCH & ARCH_CONF_OLE2))
034c02fd
 		ret = cli_scanole2(ctx);
467f8b1e
 	    break;
6d6e8271
 
81fded11
 	case CL_TYPE_7Z:
55094a9c
 	    ctx->container_type = CL_TYPE_7Z;
 	    ctx->container_size = sb.st_size;
81fded11
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_7Z))
 		ret = cli_7unz(desc, ctx);
 	    break;
 
a7f5fd00
 	case CL_TYPE_POSIX_TAR:
55094a9c
 	    ctx->container_type = CL_TYPE_POSIX_TAR;
 	    ctx->container_size = sb.st_size;
bc93eda0
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_TAR))
3c91998b
 		ret = cli_scantar(desc, ctx, 1);
a7f5fd00
 	    break;
 
 	case CL_TYPE_OLD_TAR:
55094a9c
 	    ctx->container_type = CL_TYPE_OLD_TAR;
 	    ctx->container_size = sb.st_size;
bc93eda0
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_TAR))
3c91998b
 		ret = cli_scantar(desc, ctx, 0);
667ab9c6
 	    break;
 
75e46945
 	case CL_TYPE_CPIO_OLD:
55094a9c
 	    ctx->container_type = CL_TYPE_CPIO_OLD;
 	    ctx->container_size = sb.st_size;
75e46945
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_CPIO))
 		ret = cli_scancpio_old(desc, ctx);
 	    break;
 
 	case CL_TYPE_CPIO_ODC:
55094a9c
 	    ctx->container_type = CL_TYPE_CPIO_ODC;
 	    ctx->container_size = sb.st_size;
75e46945
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_CPIO))
 		ret = cli_scancpio_odc(desc, ctx);
 	    break;
 
 	case CL_TYPE_CPIO_NEWC:
55094a9c
 	    ctx->container_type = CL_TYPE_CPIO_NEWC;
 	    ctx->container_size = sb.st_size;
75e46945
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_CPIO))
 		ret = cli_scancpio_newc(desc, ctx, 0);
 	    break;
 
 	case CL_TYPE_CPIO_CRC:
55094a9c
 	    ctx->container_type = CL_TYPE_CPIO_CRC;
 	    ctx->container_size = sb.st_size;
75e46945
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_CPIO))
 		ret = cli_scancpio_newc(desc, ctx, 1);
 	    break;
 
667ab9c6
 	case CL_TYPE_BINHEX:
bc93eda0
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_BINHEX))
124ba12d
 		ret = cli_binhex(ctx);
2b259453
 	    break;
 
3805ebcb
 	case CL_TYPE_SCRENC:
bc93eda0
 	    if(DCONF_OTHER & OTHER_CONF_SCRENC)
 		ret = cli_scanscrenc(desc, ctx);
eb308794
 	    break;
 
 	case CL_TYPE_RIFF:
bc93eda0
 	    if(SCAN_ALGO && (DCONF_OTHER & OTHER_CONF_RIFF))
e4101980
 		ret = cli_scanriff(desc, ctx);
e57fa318
 	    break;
 
7afdc309
 	case CL_TYPE_GRAPHICS:
bc93eda0
 	    if(SCAN_ALGO && (DCONF_OTHER & OTHER_CONF_JPEG))
be63d0ad
 		ret = cli_scanjpeg(desc, ctx);
7afdc309
 	    break;
 
d070d475
         case CL_TYPE_PDF: /* FIXMELIMITS: pdf should be an archive! */
55094a9c
 	    ctx->container_type = CL_TYPE_PDF;
 	    ctx->container_size = sb.st_size;
c5107e70
 	    if(SCAN_PDF && (DCONF_DOC & DOC_CONF_PDF))
2d5dbc37
 		ret = cli_scanpdf(ctx, 0);
798308de
 	    break;
 
2c6f9d57
 	case CL_TYPE_CRYPTFF:
bc93eda0
 	    if(DCONF_OTHER & OTHER_CONF_CRYPTFF)
 		ret = cli_scancryptff(desc, ctx);
2c6f9d57
 	    break;
 
3f97a1e7
 	case CL_TYPE_ELF:
bc93eda0
 	    if(SCAN_ELF && ctx->dconf->elf)
90b75c27
 		ret = cli_scanelf(ctx);
094116b2
 	    break;
 
89c14869
 	case CL_TYPE_MACHO:
 	    if(ctx->dconf->macho)
f4363389
 		ret = cli_scanmacho(ctx, NULL);
89c14869
 	    break;
 
3222a096
 	case CL_TYPE_MACHO_UNIBIN:
 	    if(ctx->dconf->macho)
f4363389
 		ret = cli_scanmacho_unibin(ctx);
89c14869
 	    break;
 
bf45bf13
 	case CL_TYPE_SIS:
55094a9c
 	    ctx->container_type = CL_TYPE_SIS;
 	    ctx->container_size = sb.st_size;
bc93eda0
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_SIS))
3c91998b
 		ret = cli_scansis(desc, ctx);
bf45bf13
 	    break;
 
c8f2d060
 	case CL_TYPE_BINARY_DATA:
3f81d93a
 	    if(SCAN_ALGO && (DCONF_OTHER & OTHER_CONF_MYDOOMLOG))
e4101980
 		ret = cli_check_mydoom_log(desc, ctx);
467f8b1e
 	    break;
216a697f
 
a6e38800
 	case CL_TYPE_TEXT_ASCII:
26fbf6bd
 	    if(SCAN_STRUCTURED && (DCONF_OTHER & OTHER_CONF_DLP))
a6e38800
 		/* TODO: consider calling this from cli_scanscript() for
 		 * a normalised text
 		 */
 		ret = cli_scan_structured(desc, ctx);
 	    break;
 
216a697f
 	default:
 	    break;
e3aaff8e
     }
d91ab809
     ctx->recursion--;
55094a9c
     ctx->container_type = current_container_type;
     ctx->container_size = current_container_size;
467f8b1e
 
998bcfa7
     if(ret == CL_VIRUS) {
32b1e04e
 	ret = cli_checkfp(hash, hashed_size, ctx);
49cc1e3c
 	funmap(*ctx->fmap);
f4e34215
 	ctx->fmap--;
 	cli_bitset_free(ctx->hook_lsig_matches);
 	ctx->hook_lsig_matches = old_hook_lsig_matches;
aef8d4ac
 	ret_from_magicscan(ret);
998bcfa7
     }
a6e38800
 
ded60d81
     if(type == CL_TYPE_ZIP && SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_ZIP)) {
 	if(sb.st_size > 1048576) {
 	    cli_dbgmsg("cli_magic_scandesc: Not checking for embedded PEs (zip file > 1 MB)\n");
 	    typercg = 0;
 	}
     }
 
08f0150f
     /* CL_TYPE_HTML: raw HTML files are not scanned, unless safety measure activated via DCONF */
a6e38800
     if(type != CL_TYPE_IGNORED && (type != CL_TYPE_HTML || !(DCONF_DOC & DOC_CONF_HTML_SKIPRAW)) && !ctx->engine->sdb) {
c0a16479
 	res = cli_scanraw(ctx, type, typercg, &dettype, hash);
 	if(res != CL_CLEAN) {
93d658a8
 	    switch(res) {
353d03eb
 		/* List of scan halts, runtime errors only! */
 		case CL_EUNLINK:
 		case CL_ESTAT:
93d658a8
 		case CL_ESEEK:
353d03eb
 		case CL_EWRITE:
 		case CL_EDUP:
 		case CL_ETMPFILE:
 		case CL_ETMPDIR:
93d658a8
 		case CL_EMEM:
353d03eb
 		case CL_ETIMEOUT:
93d658a8
 		    cli_dbgmsg("Descriptor[%d]: cli_scanraw error %s\n", desc, cl_strerror(res));
 		    funmap(*ctx->fmap);
 		    ctx->fmap--;
 		    cli_bitset_free(ctx->hook_lsig_matches);
 		    ctx->hook_lsig_matches = old_hook_lsig_matches;
 		    ret_from_magicscan(res);
 		/* CL_VIRUS = malware found, check FP and report */
 		case CL_VIRUS:
 		    ret = cli_checkfp(hash, hashed_size, ctx);
fb0a54dd
 		    if (SCAN_ALL)
 			break;
93d658a8
 		    funmap(*ctx->fmap);
 		    ctx->fmap--;
 		    cli_bitset_free(ctx->hook_lsig_matches);
 		    ctx->hook_lsig_matches = old_hook_lsig_matches;
 		    ret_from_magicscan(ret);
 		/* "MAX" conditions should still fully scan the current file */
6a879ad9
 		case CL_EMAXREC:
 		case CL_EMAXSIZE:
 		case CL_EMAXFILES:
93d658a8
 		    ret = res;
 		    cli_dbgmsg("Descriptor[%d]: Continuing after cli_scanraw reached %s\n",
 			desc, cl_strerror(res));
 		    break;
901887f6
 		/* Other errors must not block further scans below
 		 * This specifically includes CL_EFORMAT & CL_EREAD & CL_EUNPACK
 		 * Malformed/truncated files could report as any of these three.
 		 */
6a879ad9
 		default:
93d658a8
 		    ret = res;
 		    cli_dbgmsg("Descriptor[%d]: Continuing after cli_scanraw error %s\n",
 			desc, cl_strerror(res));
6a879ad9
 	    }
998bcfa7
 	}
46c2e927
     }
e3aaff8e
 
d91ab809
     ctx->recursion++;
77e4bb11
     lseek(desc, 0, SEEK_SET);
     switch(type) {
f4e34215
 	/* bytecode hooks triggered by a lsig must be a hook
 	 * called from one of the functions here */
a810ba50
 	case CL_TYPE_TEXT_ASCII:
2582d3ed
 	case CL_TYPE_TEXT_UTF16BE:
 	case CL_TYPE_TEXT_UTF16LE:
 	case CL_TYPE_TEXT_UTF8:
fb0a54dd
 	    if((DCONF_DOC & DOC_CONF_SCRIPT) && dettype != CL_TYPE_HTML && ret != CL_VIRUS)
ee1b2a6c
 	        ret = cli_scanscript(ctx);
ee50848a
 	    if(SCAN_MAIL && (DCONF_MAIL & MAIL_CONF_MBOX) && ret != CL_VIRUS && (ctx->container_type == CL_TYPE_MAIL || dettype == CL_TYPE_MAIL)) {
fb0a54dd
 		ret = cli_fmap_scandesc(ctx, CL_TYPE_MAIL, 0, NULL, AC_SCAN_VIR, NULL, NULL);
7e65889c
 	    }
a810ba50
 	    break;
77e4bb11
 	/* Due to performance reasons all executables were first scanned
 	 * in raw mode. Now we will try to unpack them
 	 */
3805ebcb
 	case CL_TYPE_MSEXE:
50177b5c
 	    if(SCAN_PE && ctx->dconf->pe) {
 		unsigned int corrupted_input = ctx->corrupted_input;
453d8180
 		ret = cli_scanpe(ctx);
50177b5c
 		ctx->corrupted_input = corrupted_input;
 	    }
77e4bb11
 	    break;
8000d078
 	default:
 	    break;
77e4bb11
     }
2587dbab
 
     if(ret == CL_VIRUS)
 	ret = cli_checkfp(hash, hashed_size, ctx);
d91ab809
     ctx->recursion--;
49cc1e3c
     funmap(*ctx->fmap);
998bcfa7
     ctx->fmap--;
f4e34215
     cli_bitset_free(ctx->hook_lsig_matches);
     ctx->hook_lsig_matches = old_hook_lsig_matches;
77e4bb11
 
d59d41b9
     switch(ret) {
901887f6
 	/* Malformed file cases */
d59d41b9
 	case CL_EFORMAT:
901887f6
 	case CL_EREAD:
 	case CL_EUNPACK:
 	/* Limits exceeded */
d59d41b9
 	case CL_EMAXREC:
 	case CL_EMAXSIZE:
 	case CL_EMAXFILES:
 	    cli_dbgmsg("Descriptor[%d]: %s\n", desc, cl_strerror(ret));
901887f6
 	    ret_from_magicscan(CL_CLEAN);
54e6b31f
 	case CL_CLEAN:
52f52931
 	    cache_add(hash, hashed_size, ctx);
aef8d4ac
 	    ret_from_magicscan(CL_CLEAN);
d59d41b9
 	default:
aef8d4ac
 	    ret_from_magicscan(ret);
322bfd03
     }
e3aaff8e
 }
 
7770d314
 int cli_magic_scandesc(int desc, cli_ctx *ctx)
 {
     return magic_scandesc(desc, ctx, CL_TYPE_ANY);
 }
 
 int cli_magic_scandesc_type(int desc, cli_ctx *ctx, cli_file_t type)
 {
     return magic_scandesc(desc, ctx, type);
 }
 
edbba730
 int cl_scandesc(int desc, const char **virname, unsigned long int *scanned, const struct cl_engine *engine, unsigned int scanoptions)
 {
769f37a6
     return cl_scandesc_callback(desc, virname, scanned, engine, scanoptions, NULL);
edbba730
 }
 
aa7380df
 int cl_scandesc_callback(int desc, const char **virname, unsigned long int *scanned, const struct cl_engine *engine, unsigned int scanoptions, void *context)
 {
     cli_ctx ctx;
     int rc;
 
     memset(&ctx, '\0', sizeof(cli_ctx));
     ctx.engine = engine;
     ctx.virname = virname;
     ctx.scanned = scanned;
     ctx.options = scanoptions;
     ctx.found_possibly_unwanted = 0;
     ctx.container_type = CL_TYPE_ANY;
     ctx.container_size = 0;
     ctx.dconf = (struct cli_dconf *) engine->dconf;
     ctx.cb_ctx = context;
     ctx.fmap = cli_calloc(sizeof(fmap_t *), ctx.engine->maxreclevel + 2);
     if(!ctx.fmap)
 	return CL_EMEM;
     if (!(ctx.hook_lsig_matches = cli_bitset_init())) {
 	free(ctx.fmap);
 	return CL_EMEM;
     }
 
 #ifdef HAVE__INTERNAL__SHA_COLLECT
     if(scanoptions & CL_SCAN_INTERNAL_COLLECT_SHA) {
 	char link[32];
 	ssize_t linksz;
 
 	snprintf(link, sizeof(link), "/proc/self/fd/%u", desc);
 	link[sizeof(link)-1]='\0';
 	if((linksz=readlink(link, ctx.entry_filename, sizeof(ctx.entry_filename)))==-1) {
 	    cli_errmsg("failed to resolve filename for descriptor %d (%s)\n", desc, link);
 	    strcpy(ctx.entry_filename, "NO_IDEA");
 	} else
 	    ctx.entry_filename[linksz]='\0';
     } while(0);
 #endif
 
769f37a6
     cli_logg_setup(&ctx);
aa7380df
     rc = cli_magic_scandesc(desc, &ctx);
 
fb0a54dd
     if (ctx.options & CL_SCAN_ALLMATCHES) {
 	*virname = (char *)ctx.virname; /* temp hack for scanall mode until api augmentation */
 	if (rc == CL_CLEAN && ctx.num_viruses)
 	    rc = CL_VIRUS;
     }
 
aa7380df
     cli_bitset_free(ctx.hook_lsig_matches);
     free(ctx.fmap);
     if(rc == CL_CLEAN && ctx.found_possibly_unwanted)
0d79b7dc
 	rc = CL_VIRUS;
4aa7baf1
     cli_logg_unsetup();
aa7380df
     return rc;
 }
 
7f0d1148
 int cli_found_possibly_unwanted(cli_ctx* ctx)
 {
fb0a54dd
     if(cli_get_last_virus(ctx)) {
 	cli_dbgmsg("found Possibly Unwanted: %s\n", cli_get_last_virus(ctx));
 	if(ctx->options & CL_SCAN_HEURISTIC_PRECEDENCE) {
 	    /* we found a heuristic match, don't scan further,
 	     * but consider it a virus. */
 	    cli_dbgmsg("cli_found_possibly_unwanted: CL_VIRUS\n");
 	    return CL_VIRUS;
7f0d1148
 	}
fb0a54dd
 	/* heuristic scan isn't taking precedence, keep scanning.
 	 * If this is part of an archive, and 
 	 * we find a real malware we report that instead of the 
 	 * heuristic match */
 	ctx->found_possibly_unwanted = 1;
     } else {
 	cli_warnmsg("cli_found_possibly_unwanted called, but virname is not set\n");
     }
     emax_reached(ctx);
     return CL_CLEAN;
7f0d1148
 }
 
3c91998b
 static int cli_scanfile(const char *filename, cli_ctx *ctx)
21cf4aeb
 {
 	int fd, ret;
 
22275b15
     /* internal version of cl_scanfile with arec/mrec preserved */
6e246c11
     if((fd = safe_open(filename, O_RDONLY|O_BINARY)) == -1)
21cf4aeb
 	return CL_EOPEN;
 
3c91998b
     ret = cli_magic_scandesc(fd, ctx);
21cf4aeb
 
     close(fd);
     return ret;
 }
 
2accc66f
 int cl_scanfile(const char *filename, const char **virname, unsigned long int *scanned, const struct cl_engine *engine, unsigned int scanoptions)
e3aaff8e
 {
769f37a6
     return cl_scanfile_callback(filename, virname, scanned, engine, scanoptions, NULL);
e3aaff8e
 }
72ce4b70
 
769f37a6
 int cl_scanfile_callback(const char *filename, const char **virname, unsigned long int *scanned, const struct cl_engine *engine, unsigned int scanoptions, void *context)
edbba730
 {
 	int fd, ret;
 
     if((fd = safe_open(filename, O_RDONLY|O_BINARY)) == -1)
 	return CL_EOPEN;
 
769f37a6
     ret = cl_scandesc_callback(fd, virname, scanned, engine, scanoptions, context);
edbba730
     close(fd);
 
     return ret;
 }
 
72ce4b70
 /*
 Local Variables:
    c-basic-offset: 4
 End:
 */