clamscan/manager.c
e3aaff8e
 /*
086eab5c
  *  Copyright (C) 2007-2009 Sourcefire, Inc.
  *
  *  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
  *
  */
 
5c07666a
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
e3aaff8e
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
 #include <sys/stat.h>
 #include <sys/types.h>
34f71e0e
 #ifdef C_WINDOWS
 #include <sys/utime.h>
 #else
e3aaff8e
 #include <sys/wait.h>
4790a32f
 #include <utime.h>
34f71e0e
 #endif
ec6429ab
 #ifndef C_WINDOWS
 #include <dirent.h>
9a03413e
 #include <sys/time.h>
 #include <sys/resource.h>
34f71e0e
 #endif
e3aaff8e
 #include <fcntl.h>
34f71e0e
 #ifdef	HAVE_UNISTD_H
e3aaff8e
 #include <unistd.h>
34f71e0e
 #endif
e3aaff8e
 #include <sys/types.h>
 #include <signal.h>
 #include <errno.h>
 
 #include "manager.h"
7a2997f1
 #include "others.h"
 #include "global.h"
 
269d520d
 #include "shared/optparser.h"
ee6702ab
 #include "shared/actions.h"
7a2997f1
 #include "shared/output.h"
 #include "shared/misc.h"
 
 #include "libclamav/clamav.h"
 #include "libclamav/others.h"
 #include "libclamav/matcher-ac.h"
 #include "libclamav/str.h"
05f92e64
 #include "libclamav/readdb.h"
370892d0
 #include "libclamav/cltypes.h"
e3aaff8e
 
 #ifdef C_LINUX
 dev_t procdev;
 #endif
 
34f71e0e
 #ifdef C_WINDOWS
 #undef P_tmpdir
 #define P_tmpdir    "C:\\WINDOWS\\TEMP"
 #endif
 
 #ifndef	O_BINARY
 #define	O_BINARY    0
 #endif
 
269d520d
 static int scanfile(const char *filename, struct cl_engine *engine, const struct optstruct *opts, unsigned int options)
7a2997f1
 {
5da3127b
   int ret = 0, fd, included, printclean = 1, fsize;
269d520d
 	const struct optstruct *opt;
ec6429ab
 	const char *virname;
 #ifdef C_LINUX
 	struct stat sb;
 
     /* argh, don't scan /proc files */
     if(procdev)
 	if(stat(filename, &sb) != -1)
 	    if(sb.st_dev == procdev) {
 		if(!printinfected)
 		    logg("~%s: Excluded (/proc)\n", filename);
 		return 0;
 	    }
 #endif    
 
269d520d
     if((opt = optget(opts, "exclude"))->enabled) {
 	while(opt) {
 	    if(match_regex(filename, opt->strarg) == 1) {
ec6429ab
 		if(!printinfected)
 		    logg("~%s: Excluded\n", filename);
 		return 0;
 	    }
269d520d
 	    opt = opt->nextarg;
ec6429ab
 	}
     }
 
269d520d
     if((opt = optget(opts, "include"))->enabled) {
ec6429ab
 	included = 0;
269d520d
 	while(opt) {
 	    if(match_regex(filename, opt->strarg) == 1) {
ec6429ab
 		included = 1;
 		break;
 	    }
269d520d
 	    opt = opt->nextarg;
ec6429ab
 	}
 	if(!included) {
 	    if(!printinfected)
 		logg("~%s: Excluded\n", filename);
 	    return 0;
 	}
     }
 
5da3127b
     fsize = fileinfo(filename, 1);
     if(fsize == 0) {
ec6429ab
 	if(!printinfected)
 	    logg("~%s: Empty file\n", filename);
 	return 0;
     }
5da3127b
     info.rblocks += fsize / CL_COUNT_PRECISION;
ec6429ab
 #ifndef C_WINDOWS
     if(geteuid())
 	if(checkaccess(filename, NULL, R_OK) != 1) {
 	    if(!printinfected)
 		logg("~%s: Access denied\n", filename);
 	    return 0;
 	}
 #endif
 
     logg("*Scanning %s\n", filename);
 
     if((fd = open(filename, O_RDONLY|O_BINARY)) == -1) {
 	logg("^Can't open file %s\n", filename);
 	return 54;
     }
 
     info.files++;
 
370892d0
     if((ret = cl_scandesc(fd, &virname, &info.blocks, engine, options)) == CL_VIRUS) {
ec6429ab
 	logg("~%s: %s FOUND\n", filename, virname);
 	info.ifiles++;
 
 	if(bell)
 	    fprintf(stderr, "\007");
 
     } else if(ret == CL_CLEAN) {
 	if(!printinfected && printclean)
 	    mprintf("~%s: OK\n", filename);
     } else
 	if(!printinfected)
52b40b1e
 	    logg("~%s: %s ERROR\n", filename, cl_strerror(ret));
ec6429ab
 
     close(fd);
 
ee6702ab
     if(ret == CL_VIRUS && action)
 	action(filename);
ec6429ab
 
     return ret;
 }
 
269d520d
 static int scandirs(const char *dirname, struct cl_engine *engine, const struct optstruct *opts, unsigned int options, unsigned int depth)
ec6429ab
 {
 	DIR *dd;
 	struct dirent *dent;
 	struct stat statbuf;
 	char *fname;
 	int scanret = 0, included;
269d520d
 	const struct optstruct *opt;
ec6429ab
 
 
269d520d
     if((opt = optget(opts, "exclude-dir"))->enabled) {
 	while(opt) {
 	    if(match_regex(dirname, opt->strarg) == 1) {
ec6429ab
 		if(!printinfected)
 		    logg("~%s: Excluded\n", dirname);
 		return 0;
 	    }
269d520d
 	    opt = opt->nextarg;
ec6429ab
 	}
     }
 
269d520d
     if((opt = optget(opts, "include-dir"))->enabled) {
ec6429ab
 	included = 0;
269d520d
 	while(opt) {
 	    if(match_regex(dirname, opt->strarg) == 1) {
ec6429ab
 		included = 1;
 		break;
 	    }
269d520d
 	    opt = opt->nextarg;
ec6429ab
 	}
 	if(!included) {
 	    if(!printinfected)
 		logg("~%s: Excluded\n", dirname);
 	    return 0;
 	}
     }
 
269d520d
     if(depth > (unsigned int) optget(opts, "max-dir-recursion")->numarg)
ec6429ab
 	return 0;
 
     info.dirs++;
     depth++;
 
     if((dd = opendir(dirname)) != NULL) {
 	while((dent = readdir(dd))) {
6670d61d
 #if !defined(C_INTERIX) && !defined(C_WINDOWS)
ec6429ab
 	    if(dent->d_ino)
 #endif
 	    {
 		if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
 		    /* build the full name */
 		    fname = malloc(strlen(dirname) + strlen(dent->d_name) + 2);
1464e7a1
 		    if(!strcmp(dirname, "/"))
 			sprintf(fname, "/%s", dent->d_name);
 		    else
 			sprintf(fname, "%s/%s", dirname, dent->d_name);
ec6429ab
 
 		    /* stat the file */
 		    if(lstat(fname, &statbuf) != -1) {
 			if(S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode) && recursion) {
269d520d
 			    if(scandirs(fname, engine, opts, options, depth) == 1)
ec6429ab
 				scanret++;
 			} else {
 			    if(S_ISREG(statbuf.st_mode))
269d520d
 				scanret += scanfile(fname, engine, opts, options);
ec6429ab
 			}
 		    }
 		    free(fname);
 		}
 	    }
 	}
     } else {
 	if(!printinfected)
 	    logg("~%s: Can't open directory.\n", dirname);
 	return 53;
     }
 
     closedir(dd);
 
     if(scanret)
 	return 1;
     else
 	return 0;
 
7a2997f1
 }
 
269d520d
 static int scanstdin(const struct cl_engine *engine, const struct optstruct *opts, int options)
7a2997f1
 {
 	int ret;
5da3127b
 	unsigned int fsize = 0;
7a2997f1
 	const char *virname, *tmpdir;
 	char *file, buff[FILEBUFF];
ec6429ab
 	size_t bread;
7a2997f1
 	FILE *fs;
 
269d520d
     if(optget(opts, "tempdir")->enabled) {
 	tmpdir = optget(opts, "tempdir")->strarg;
33068e09
     } else {
 	/* check write access */
 	tmpdir = getenv("TMPDIR");
7a2997f1
 
33068e09
 	if(tmpdir == NULL)
7a2997f1
 #ifdef P_tmpdir
33068e09
 	    tmpdir = P_tmpdir;
7a2997f1
 #else
33068e09
 	    tmpdir = "/tmp";
7a2997f1
 #endif
33068e09
     }
7a2997f1
 
     if(checkaccess(tmpdir, CLAMAVUSER, W_OK) != 1) {
 	logg("!Can't write to temporary directory\n");
 	return 64;
     }
 
     file = cli_gentemp(tmpdir);
 
     if(!(fs = fopen(file, "wb"))) {
 	logg("!Can't open %s for writing\n", file);
38fe8af4
 	free(file);
7a2997f1
 	return 63;
     }
 
5da3127b
     while((bread = fread(buff, 1, FILEBUFF, stdin))) {
 	fsize += bread;
ec6429ab
 	if(fwrite(buff, 1, bread, fs) < bread) {
38fe8af4
 	    logg("!Can't write to %s\n", file);
 	    free(file);
 	    return 58;
 	}
5da3127b
     }
7a2997f1
     fclose(fs);
 
     logg("*Checking %s\n", file);
     info.files++;
5da3127b
     info.rblocks += fsize / CL_COUNT_PRECISION;
7a2997f1
 
370892d0
     if((ret = cl_scanfile(file, &virname, &info.blocks, engine, options)) == CL_VIRUS) {
7a2997f1
 	logg("stdin: %s FOUND\n", virname);
 	info.ifiles++;
 
 	if(bell)
 	    fprintf(stderr, "\007");
 
     } else if(ret == CL_CLEAN) {
 	if(!printinfected)
 	    mprintf("stdin: OK\n");
     } else
 	if(!printinfected)
52b40b1e
 	    logg("stdin: %s ERROR\n", cl_strerror(ret));
7a2997f1
 
     unlink(file);
     free(file);
     return ret;
 }
8000d078
 
269d520d
 int scanmanager(const struct optstruct *opts)
e3aaff8e
 {
 	mode_t fmode;
c2b6681b
 	int ret = 0, fmodeint, i;
7a2997f1
 	unsigned int options = 0, dboptions = 0;
370892d0
 	struct cl_engine *engine;
e3aaff8e
 	struct stat sb;
269d520d
 	char *file, cwd[1024], *pua_cats = NULL;
c2b6681b
 	const char *filename;
269d520d
 	const struct optstruct *opt;
9a03413e
 #ifndef C_WINDOWS
 	struct rlimit rlim;
 #endif
e3aaff8e
 
269d520d
     if(optget(opts, "phishing-sigs")->enabled)
9f8098c0
 	dboptions |= CL_DB_PHISHING;
e3aaff8e
 
269d520d
     if(optget(opts,"phishing-scan-urls")->enabled)
9f8098c0
 	dboptions |= CL_DB_PHISHING_URLS;
a68507c5
 
370892d0
     if((ret = cl_init(CL_INIT_DEFAULT))) {
 	logg("!Can't initialize libclamav: %s\n", cl_strerror(ret));
 	return 50;
     }
 
b8fe70b3
     if(!(engine = cl_engine_new())) {
370892d0
 	logg("!Can't initialize antivirus engine\n");
 	return 50;
     }
 
269d520d
     if(optget(opts, "detect-pua")->enabled) {
70edb085
 	dboptions |= CL_DB_PUA;
269d520d
 	if((opt = optget(opts, "exclude-pua"))->enabled) {
b023c36d
 	    dboptions |= CL_DB_PUA_EXCLUDE;
c783516d
 	    i = 0;
269d520d
 	    while(opt) {
 		if(!(pua_cats = realloc(pua_cats, i + strlen(opt->strarg) + 3))) {
b023c36d
 		    logg("!Can't allocate memory for pua_cats\n");
370892d0
 		    cl_engine_free(engine);
b023c36d
 		    return 70;
 		}
269d520d
 		sprintf(pua_cats + i, ".%s", opt->strarg);
 		i += strlen(opt->strarg) + 1;
b023c36d
 		pua_cats[i] = 0;
269d520d
 		opt = opt->nextarg;
b023c36d
 	    }
 	    pua_cats[i] = '.';
 	    pua_cats[i + 1] = 0;
 	}
 
269d520d
 	if((opt = optget(opts, "include-pua"))->enabled) {
b023c36d
 	    if(pua_cats) {
 		logg("!--exclude-pua and --include-pua cannot be used at the same time\n");
370892d0
 		cl_engine_free(engine);
b023c36d
 		free(pua_cats);
 		return 40;
 	    }
 	    dboptions |= CL_DB_PUA_INCLUDE;
 	    i = 0;
269d520d
 	    while(opt) {
 		if(!(pua_cats = realloc(pua_cats, i + strlen(opt->strarg) + 3))) {
b023c36d
 		    logg("!Can't allocate memory for pua_cats\n");
 		    return 70;
 		}
269d520d
 		sprintf(pua_cats + i, ".%s", opt->strarg);
 		i += strlen(opt->strarg) + 1;
b023c36d
 		pua_cats[i] = 0;
269d520d
 		opt = opt->nextarg;
b023c36d
 	    }
 	    pua_cats[i] = '.';
 	    pua_cats[i + 1] = 0;
 	}
 
 	if(pua_cats) {
2accc66f
 	    if((ret = cl_engine_set_str(engine, CL_ENGINE_PUA_CATEGORIES, pua_cats))) {
 		logg("!cli_engine_set_str(CL_ENGINE_PUA_CATEGORIES) failed: %s\n", cl_strerror(ret));
b023c36d
 		free(pua_cats);
370892d0
 		cl_engine_free(engine);
b023c36d
 		return 50;
 	    }
370892d0
 	    free(pua_cats);
b023c36d
 	}
     }
 
2accc66f
     if(optget(opts, "dev-ac-only")->enabled)
 	cl_engine_set_num(engine, CL_ENGINE_AC_ONLY, 1);
ab0d2f05
 
2accc66f
     if(optget(opts, "dev-ac-depth")->enabled)
 	cl_engine_set_num(engine, CL_ENGINE_AC_MAXDEPTH, optget(opts, "dev-ac-depth")->numarg);
ab0d2f05
 
2accc66f
     if(optget(opts, "leave-temps")->enabled)
 	cl_engine_set_num(engine, CL_ENGINE_KEEPTMP, 1);
33068e09
 
269d520d
     if((opt = optget(opts, "tempdir"))->enabled) {
2accc66f
 	if((ret = cl_engine_set_str(engine, CL_ENGINE_TMPDIR, opt->strarg))) {
 	    logg("!cli_engine_set_str(CL_ENGINE_TMPDIR) failed: %s\n", cl_strerror(ret));
33068e09
 	    cl_engine_free(engine);
 	    return 50;
 	}
     }
 
8daa97de
     if((opt = optget(opts, "database"))->active) {
269d520d
 	if((ret = cl_load(opt->strarg, engine, &info.sigs, dboptions))) {
9f0e5585
 	    logg("!%s\n", cl_strerror(ret));
370892d0
 	    cl_engine_free(engine);
d6449522
 	    return 50;
e3aaff8e
 	}
 
     } else {
98ce643b
 	    char *dbdir = freshdbdir();
908db4df
 
370892d0
 	if((ret = cl_load(dbdir, engine, &info.sigs, dboptions))) {
9f0e5585
 	    logg("!%s\n", cl_strerror(ret));
98ce643b
 	    free(dbdir);
370892d0
 	    cl_engine_free(engine);
e3aaff8e
 	    return 50;
 	}
98ce643b
 	free(dbdir);
e3aaff8e
     }
 
370892d0
     if((ret = cl_engine_compile(engine)) != 0) {
9f0e5585
 	logg("!Database initialization error: %s\n", cl_strerror(ret));;
370892d0
 	cl_engine_free(engine);
2d70a403
 	return 50;
     }
e3aaff8e
 
7a2997f1
     /* set limits */
e3aaff8e
 
269d520d
     if((opt = optget(opts, "max-scansize"))->enabled) {
2accc66f
 	if((ret = cl_engine_set_num(engine, CL_ENGINE_MAX_SCANSIZE, opt->numarg))) {
 	    logg("!cli_engine_set_num(CL_ENGINE_MAX_SCANSIZE) failed: %s\n", cl_strerror(ret));
370892d0
 	    cl_engine_free(engine);
 	    return 50;
 	}
     }
281c7642
 
269d520d
     if((opt = optget(opts, "max-filesize"))->enabled) {
2accc66f
 	if((ret = cl_engine_set_num(engine, CL_ENGINE_MAX_FILESIZE, opt->numarg))) {
 	    logg("!cli_engine_set_num(CL_ENGINE_MAX_FILESIZE) failed: %s\n", cl_strerror(ret));
370892d0
 	    cl_engine_free(engine);
 	    return 50;
 	}
     }
e3aaff8e
 
9a03413e
 #ifndef C_WINDOWS
     if(getrlimit(RLIMIT_FSIZE, &rlim) == 0) {
655bc627
 	if(rlim.rlim_cur < (rlim_t) cl_engine_get_num(engine, CL_ENGINE_MAX_FILESIZE, NULL))
370892d0
 	    logg("^System limit for file size is lower than engine->maxfilesize\n");
655bc627
 	if(rlim.rlim_cur < (rlim_t) cl_engine_get_num(engine, CL_ENGINE_MAX_SCANSIZE, NULL))
370892d0
 	    logg("^System limit for file size is lower than engine->maxscansize\n");
9a03413e
     } else {
 	logg("^Cannot obtain resource limits for file size\n");
     }
 #endif
 
269d520d
     if((opt = optget(opts, "max-files"))->enabled) {
2accc66f
 	if((ret = cl_engine_set_num(engine, CL_ENGINE_MAX_FILES, opt->numarg))) {
 	    logg("!cli_engine_set_num(CL_ENGINE_MAX_FILES) failed: %s\n", cl_strerror(ret));
370892d0
 	    cl_engine_free(engine);
 	    return 50;
 	}
     }
e3aaff8e
 
269d520d
     if((opt = optget(opts, "max-recursion"))->enabled) {
2accc66f
 	if((ret = cl_engine_set_num(engine, CL_ENGINE_MAX_RECURSION, opt->numarg))) {
 	    logg("!cli_engine_set_num(CL_ENGINE_MAX_RECURSION) failed: %s\n", cl_strerror(ret));
370892d0
 	    cl_engine_free(engine);
 	    return 50;
 	}
     }
e3aaff8e
 
269d520d
     /* set scan options */
     if(optget(opts,"phishing-ssl")->enabled)
 	options |= CL_SCAN_PHISHING_BLOCKSSL;
6ef42bc3
 
269d520d
     if(optget(opts,"phishing-cloak")->enabled)
 	options |= CL_SCAN_PHISHING_BLOCKCLOAK;
 
     if(optget(opts,"heuristic-scan-precedence")->enabled)
 	options |= CL_SCAN_HEURISTIC_PRECEDENCE;
 
     if(optget(opts, "scan-archive")->enabled)
3805ebcb
 	options |= CL_SCAN_ARCHIVE;
6ef42bc3
 
269d520d
     if(optget(opts, "detect-broken")->enabled)
453581ae
 	options |= CL_SCAN_BLOCKBROKEN;
6ef42bc3
 
269d520d
     if(optget(opts, "block-encrypted")->enabled)
08d6b1e3
 	options |= CL_SCAN_BLOCKENCRYPTED;
6ef42bc3
 
269d520d
     if(optget(opts, "scan-pe")->enabled)
3805ebcb
 	options |= CL_SCAN_PE;
6ef42bc3
 
269d520d
     if(optget(opts, "scan-elf")->enabled)
3f97a1e7
 	options |= CL_SCAN_ELF;
 
269d520d
     if(optget(opts, "scan-ole2")->enabled)
3805ebcb
 	options |= CL_SCAN_OLE2;
6ef42bc3
 
269d520d
     if(optget(opts, "scan-pdf")->enabled)
c5107e70
 	options |= CL_SCAN_PDF;
 
269d520d
     if(optget(opts, "scan-html")->enabled)
3805ebcb
 	options |= CL_SCAN_HTML;
6ef42bc3
 
269d520d
     if(optget(opts, "scan-mail")->enabled) {
3805ebcb
 	options |= CL_SCAN_MAIL;
6ef42bc3
 
269d520d
 	if(optget(opts, "mail-follow-urls")->enabled)
3805ebcb
 	    options |= CL_SCAN_MAILURL;
6ef42bc3
     }
 
269d520d
     if(optget(opts, "algorithmic-detection")->enabled)
6fd2fb47
 	options |= CL_SCAN_ALGORITHMIC;
1b661cef
 
269d520d
     if(optget(opts, "detect-structured")->enabled) {
a6e38800
 	options |= CL_SCAN_STRUCTURED;
5fe6e72b
 
269d520d
 	if((opt = optget(opts, "structured-ssn-format"))->enabled) {
 	    switch(opt->numarg) {
5fe6e72b
 		case 0:
 		    options |= CL_SCAN_STRUCTURED_SSN_NORMAL;
 		    break;
 		case 1:
 		    options |= CL_SCAN_STRUCTURED_SSN_STRIPPED;
 		    break;
 		case 2:
 		    options |= (CL_SCAN_STRUCTURED_SSN_NORMAL | CL_SCAN_STRUCTURED_SSN_STRIPPED);
 		    break;
 		default:
 		    logg("!Invalid argument for --structured-ssn-format\n");
 		    return 40;
 	    }
 	} else {
3f9918e1
 	    options |= CL_SCAN_STRUCTURED_SSN_NORMAL;
5fe6e72b
 	}
 
269d520d
 	if((opt = optget(opts, "structured-ssn-count"))->enabled) {
2accc66f
 	    if((ret = cl_engine_set_num(engine, CL_ENGINE_MIN_SSN_COUNT, opt->numarg))) {
 		logg("!cli_engine_set_num(CL_ENGINE_MIN_SSN_COUNT) failed: %s\n", cl_strerror(ret));
370892d0
 		cl_engine_free(engine);
 		return 50;
 	    }
 	}
5fe6e72b
 
269d520d
 	if((opt = optget(opts, "structured-cc-count"))->enabled) {
2accc66f
 	    if((ret = cl_engine_set_num(engine, CL_ENGINE_MIN_CC_COUNT, opt->numarg))) {
 		logg("!cli_engine_set_num(CL_ENGINE_MIN_CC_COUNT) failed: %s\n", cl_strerror(ret));
370892d0
 		cl_engine_free(engine);
 		return 50;
 	    }
 	}
5fe6e72b
 
269d520d
     } else {
a6e38800
 	options &= ~CL_SCAN_STRUCTURED;
269d520d
     }
a6e38800
 
e3aaff8e
 #ifdef C_LINUX
d9b55a82
     procdev = (dev_t) 0;
53c7b870
     if(stat("/proc", &sb) != -1 && !sb.st_size)
e3aaff8e
 	procdev = sb.st_dev;
 #endif
 
     /* check filetype */
c2b6681b
     if(!opts->filename && !optget(opts, "file-list")->enabled) {
2d70a403
 	/* we need full path for some reasons (eg. archive handling) */
b782aece
 	if(!getcwd(cwd, sizeof(cwd))) {
9f0e5585
 	    logg("!Can't get absolute pathname of current working directory\n");
2d70a403
 	    ret = 57;
 	} else
269d520d
 	    ret = scandirs(cwd, engine, opts, options, 1);
2d70a403
 
c2b6681b
     } else if(opts->filename && !optget(opts, "file-list")->enabled && !strcmp(opts->filename[0], "-")) { /* read data from stdin */
269d520d
 	ret = scanstdin(engine, opts, options);
e3aaff8e
 
     } else {
c2b6681b
 	if(opts->filename && optget(opts, "file-list")->enabled)
 	    logg("^Only scanning files from --file-list (files passed at cmdline are ignored)\n");
 
 	while((filename = filelist(opts, &ret)) && (file = strdup(filename))) {
ec6429ab
 	    if((fmodeint = fileinfo(file, 2)) == -1) {
 		logg("^Can't access file %s\n", file);
 		perror(file);
2d70a403
 		ret = 56;
 	    } else {
1464e7a1
 		for(i = strlen(file) - 1; i > 0; i--) {
ec6429ab
 		    if(file[i] == '/')
 			file[i] = 0;
a8b056dc
 		    else
1464e7a1
 			break;
a8b056dc
 		}
 
2d70a403
 		fmode = (mode_t) fmodeint;
 
 		switch(fmode & S_IFMT) {
 		    case S_IFREG:
269d520d
 			ret = scanfile(file, engine, opts, options);
2d70a403
 			break;
 
 		    case S_IFDIR:
269d520d
 			ret = scandirs(file, engine, opts, options, 1);
2d70a403
 			break;
 
 		    default:
ec6429ab
 			logg("!Not supported file type (%s)\n", file);
2d70a403
 			ret = 52;
 		}
 	    }
ec6429ab
 	    free(file);
e3aaff8e
 	}
     }
 
7a2997f1
     /* free the engine */
370892d0
     cl_engine_free(engine);
e3aaff8e
 
     /* overwrite return code */
7a2997f1
     if(info.ifiles)
e3aaff8e
 	ret = 1;
     else if(ret < 50) /* hopefully no error detected */ 
 	ret = 0; /* just make sure it's 0 */
 
     return ret;
 }