clamscan/clamscan.c
e3aaff8e
 /*
f51e962f
  *  Copyright (C) 2002 - 2006 Tomasz Kojm <tkojm@clamav.net>
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
 
e3aaff8e
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
34f71e0e
 #ifdef	HAVE_UNISTD_H
e3aaff8e
 #include <unistd.h>
34f71e0e
 #endif
 #ifdef	C_WINDOWS
 #include <fcntl.h>
 #else
e3aaff8e
 #include <sys/time.h>
34f71e0e
 #endif
e3aaff8e
 #include <time.h>
7a2997f1
 #ifdef C_LINUX
 #include <sys/resource.h>
 #endif
e3aaff8e
 
7b8edc5c
 #include "clamscan_opt.h"
e3aaff8e
 #include "others.h"
7a2997f1
 #include "global.h"
e3aaff8e
 #include "manager.h"
 #include "treewalk.h"
afb48b28
 
7a2997f1
 #include "shared/misc.h"
 #include "shared/output.h"
 #include "shared/options.h"
fc56deed
 
e3aaff8e
 void help(void);
 
34f71e0e
 #if defined(C_WINDOWS) && defined(CL_DEBUG)
 #include <crtdbg.h>
 #endif
 
7a2997f1
 struct s_info info;
58bcf502
 short recursion = 0, printinfected = 0, bell = 0;
 
7b8edc5c
 int main(int argc, char **argv)
e3aaff8e
 {
 	int ds, dms, ret;
 	double mb;
 	struct timeval t1, t2;
34f71e0e
 #ifndef C_WINDOWS
e3aaff8e
 	struct timezone tz;
34f71e0e
 #endif
7b8edc5c
 	struct optstruct *opt;
 	const char *pt;
e3aaff8e
 
34f71e0e
 #if defined(C_WINDOWS) && defined(CL_THREAD_SAFE)
     if(!pthread_win32_process_attach_np()) {
 	mprintf("!Can't start the win32 pthreads layer\n");
 	return 72;
     }
 #endif
e3aaff8e
 
7b8edc5c
     opt = opt_parse(argc, argv, clamscan_shortopt, clamscan_longopt, NULL);
     if(!opt) {
 	mprintf("!Can't parse the command line\n");
 	return 40;
     }
 
     if(opt_check(opt, "verbose")) {
afb48b28
 	mprintf_verbose = 1;
 	logg_verbose = 1;
     }
e3aaff8e
 
7b8edc5c
     if(opt_check(opt, "quiet"))
58bcf502
 	mprintf_quiet = 1;
e3aaff8e
 
7b8edc5c
     if(opt_check(opt, "stdout"))
58bcf502
 	mprintf_stdout = 1;
e3aaff8e
 
7b8edc5c
 
     if(opt_check(opt, "debug")) {
fc56deed
 #if defined(C_LINUX)
 	    /* njh@bandsman.co.uk: create a dump if needed */
 	    struct rlimit rlim;
 
 	rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
 	if(setrlimit(RLIMIT_CORE, &rlim) < 0)
 	    perror("setrlimit");
 #endif
 	cl_debug(); /* enable debug messages */
     }
d4d14218
 
7b8edc5c
     if(opt_check(opt, "version")) {
0aa3ba06
 	print_version(opt_arg(opt, "database"));
7b8edc5c
 	opt_free(opt);
c32360c1
 	return 0;
e3aaff8e
     }
 
7b8edc5c
     if(opt_check(opt, "help")) {
 	opt_free(opt);
e3aaff8e
     	help();
7b8edc5c
 	return 0;
e3aaff8e
     }
 
7b8edc5c
     if(opt_check(opt, "recursive"))
58bcf502
 	recursion = 1;
e3aaff8e
 
7b8edc5c
     if(opt_check(opt, "infected"))
58bcf502
 	printinfected = 1;
e3aaff8e
 
7b8edc5c
     if(opt_check(opt, "bell"))
58bcf502
 	bell = 1;
af22ece1
 
7b8edc5c
     if(opt_check(opt, "tempdir"))
 	cl_settempdir(opt_arg(opt, "tempdir"), 0);
590135f9
 
7b8edc5c
     if(opt_check(opt, "leave-temps"))
590135f9
 	cl_settempdir(NULL, 1);
 
e3aaff8e
     /* initialize logger */
441eafdf
     if(opt_check(opt, "log")) {
 	logg_file = opt_arg(opt, "log");
914ce12d
 	if(logg("#\n-------------------------------------------------------------------------------\n\n")) {
e3aaff8e
 	    mprintf("!Problem with internal logger.\n");
7b8edc5c
 	    opt_free(opt);
932616b1
 	    return 62;
e3aaff8e
 	}
     } else 
afb48b28
 	logg_file = NULL;
e3aaff8e
 
7b8edc5c
 
     /* validate some numerical options */
 
281c7642
     if(opt_check(opt, "max-scansize")) {
 	pt = opt_arg(opt, "max-scansize");
7b8edc5c
 	if(!strchr(pt, 'M') && !strchr(pt, 'm')) {
 	    if(!isnumb(pt)) {
281c7642
 		logg("!--max-scansize requires a natural number\n");
 		opt_free(opt);
 		return 40;
 	    }
 	}
     }
 
     if(opt_check(opt, "max-filesize")) {
 	pt = opt_arg(opt, "max-filesize");
 	if(!strchr(pt, 'M') && !strchr(pt, 'm')) {
 	    if(!isnumb(pt)) {
 		logg("!--max-filesize requires a natural number\n");
7b8edc5c
 		opt_free(opt);
932616b1
 		return 40;
e3aaff8e
 	    }
7b8edc5c
 	}
     }
e3aaff8e
 
7b8edc5c
     if(opt_check(opt, "max-files")) {
 	if(!isnumb(opt_arg(opt, "max-files"))) {
 	    logg("!--max-files requires a natural number\n");
 	    opt_free(opt);
932616b1
 	    return 40;
e3aaff8e
 	}
7b8edc5c
     }
e3aaff8e
 
7b8edc5c
     if(opt_check(opt, "max-recursion")) {
 	if(!isnumb(opt_arg(opt, "max-recursion"))) {
 	    logg("!--max-recursion requires a natural number\n");
 	    opt_free(opt);
932616b1
 	    return 40;
e3aaff8e
 	}
7b8edc5c
     }
e3aaff8e
 
dab42957
     if(opt_check(opt, "max-mail-recursion")) {
 	if(!isnumb(opt_arg(opt, "max-mail-recursion"))) {
 	    logg("!--max-mail-recursion requires a natural number\n");
 	    opt_free(opt);
 	    return 40;
 	}
     }
 
7b8edc5c
     if(opt_check(opt, "max-dir-recursion")) {
 	if(!isnumb(opt_arg(opt, "max-dir-recursion"))) {
 	    logg("!--max-dir-recursion requires a natural number\n");
 	    opt_free(opt);
75367c6a
 	    return 40;
 	}
7b8edc5c
     }
75367c6a
 
7b8edc5c
     if(opt_check(opt, "max-ratio")) {
 	if(!isnumb(opt_arg(opt, "max-ratio"))) {
 	    logg("!--max-ratio requires a natural number\n");
 	    opt_free(opt);
75367c6a
 	    return 40;
 	}
7b8edc5c
     }
e3aaff8e
 
7a2997f1
     memset(&info, 0, sizeof(struct s_info));
e3aaff8e
 
34f71e0e
 #ifdef C_WINDOWS
     _set_fmode(_O_BINARY);
 #ifdef CL_DEBUG
     {
 	_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
 	_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
     }
 #endif	
     gettimeofday(&t1, NULL);
 #else
e3aaff8e
     gettimeofday(&t1, &tz);
34f71e0e
 #endif
 
e3aaff8e
     ret = scanmanager(opt);
 
7b8edc5c
     if(!opt_check(opt, "disable-summary") && !opt_check(opt, "no-summary")) {
34f71e0e
 #ifdef C_WINDOWS
 	gettimeofday(&t2, NULL);
 #else
e3aaff8e
 	gettimeofday(&t2, &tz);
34f71e0e
 #endif
e3aaff8e
 	ds = t2.tv_sec - t1.tv_sec;
 	dms = t2.tv_usec - t1.tv_usec;
 	ds -= (dms < 0) ? (1):(0);
 	dms += (dms < 0) ? (1000000):(0);
0ae41a2d
 	logg("\n----------- SCAN SUMMARY -----------\n");
7a2997f1
 	logg("Known viruses: %u\n", info.sigs);
09eab32a
 	logg("Engine version: %s\n", cl_retver());
7a2997f1
 	logg("Scanned directories: %u\n", info.dirs);
 	logg("Scanned files: %u\n", info.files);
 	logg("Infected files: %u\n", info.ifiles);
 	if(info.notremoved) {
 	    logg("Not removed: %u\n", info.notremoved);
e3aaff8e
 	}
7a2997f1
 	if(info.notmoved) {
 	    logg("Not %s: %u\n", opt_check(opt, "copy") ? "moved" : "copied", info.notmoved);
e3aaff8e
 	}
7a2997f1
 	mb = info.blocks * (CL_COUNT_PRECISION / 1024) / 1024.0;
0ae41a2d
 	logg("Data scanned: %2.2lf MB\n", mb);
7a2997f1
 	logg("Time: %u.%3.3u sec (%u m %u s)\n", ds, dms/1000, ds/60, ds%60);
e3aaff8e
     }
 
7b8edc5c
     opt_free(opt);
34f71e0e
 
 #if defined(C_WINDOWS) && defined(CL_THREAD_SAFE)
     if(!pthread_win32_process_detach_np()) {
 	logg("!Can't stop the win32 pthreads layer\n");
 	return 72;
     }
 #endif
 
c32360c1
     return ret;
e3aaff8e
 }
 
 void help(void)
 {
 
     mprintf_stdout = 1;
 
     mprintf("\n");
4d89cb07
     mprintf("                       Clam AntiVirus Scanner "VERSION"\n");
61409916
     mprintf("      (C) 2002 - 2007 ClamAV Team - http://www.clamav.net/team\n\n");
5def21ff
 
a36e6e5c
     mprintf("    --help                -h             Print this help screen\n");
     mprintf("    --version             -V             Print version number\n");
5def21ff
     mprintf("    --verbose             -v             Be verbose\n");
a36e6e5c
     mprintf("    --debug                              Enable libclamav's debug messages\n");
     mprintf("    --quiet                              Only output error messages\n");
5def21ff
     mprintf("    --stdout                             Write to stdout instead of stderr\n");
a36e6e5c
     mprintf("    --no-summary                         Disable summary at end of scanning\n");
     mprintf("    --infected            -i             Only print infected files\n");
     mprintf("    --bell                               Sound bell on virus detection\n");
 
e3aaff8e
     mprintf("\n");
a9082ea2
     mprintf("    --tempdir=DIRECTORY                  Create temporary files in DIRECTORY\n");
     mprintf("    --leave-temps                        Do not remove temporary files\n");
5def21ff
     mprintf("    --database=FILE/DIR   -d FILE/DIR    Load virus database from FILE or load\n");
908db4df
     mprintf("                                         all .cvd and .db[2] files from DIR\n");
a9ebff44
     mprintf("    --log=FILE            -l FILE        Save scan report to FILE\n");
a36e6e5c
     mprintf("    --recursive           -r             Scan subdirectories recursively\n");
     mprintf("    --remove                             Remove infected files. Be careful!\n");
5def21ff
     mprintf("    --move=DIRECTORY                     Move infected files into DIRECTORY\n");
c6d2bbbc
     mprintf("    --copy=DIRECTORY                     Copy infected files into DIRECTORY\n");
266f3967
 #ifdef HAVE_REGEX_H
     mprintf("    --exclude=REGEX                      Don't scan file names matching REGEX\n");
66ceca09
     mprintf("    --exclude-dir=REGEX                  Don't scan directories matching REGEX\n");
266f3967
     mprintf("    --include=REGEX                      Only scan file names matching REGEX\n");
66ceca09
     mprintf("    --include-dir=REGEX                  Only scan directories matching REGEX\n");
266f3967
 #else
5def21ff
     mprintf("    --exclude=PATT                       Don't scan file names containing PATT\n");
66ceca09
     mprintf("    --exclude-dir=PATT                   Don't scan directories containing PATT\n");
5def21ff
     mprintf("    --include=PATT                       Only scan file names containing PATT\n");
66ceca09
     mprintf("    --include-dir=PATT                   Only scan directories containing PATT\n");
266f3967
 #endif
e3aaff8e
     mprintf("\n");
70edb085
     mprintf("    --detect-pua                         Detect Possibly Unwanted Applications\n");
a36e6e5c
     mprintf("    --no-mail                            Disable mail file support\n");
af7d0dde
     mprintf("    --no-phishing-sigs                   Disable signature-based phishing detection\n");
a68507c5
     mprintf("    --no-phishing-scan-urls              Disable url-based phishing detection\n");
19b3e182
     mprintf("    --no-phishing-restrictedscan         Enable phishing detection for all domains (might lead to false positives!)\n");
     mprintf("    --phishing-ssl                       Always block SSL mismatches in URLs (phishing module)\n");
     mprintf("    --phishing-cloak                     Always block cloaked URLs (phishing module)\n");
47138a98
     mprintf("    --no-algorithmic                     Disable algorithmic detection\n");
a9082ea2
     mprintf("    --no-pe                              Disable PE analysis\n");
3f97a1e7
     mprintf("    --no-elf                             Disable ELF support\n");
47bbbc56
     mprintf("    --no-ole2                            Disable OLE2 support\n");
c5107e70
     mprintf("    --no-pdf                             Disable PDF support\n");
888f5794
     mprintf("    --no-html                            Disable HTML support\n");
af22ece1
     mprintf("    --no-archive                         Disable libclamav archive support\n");
20c3d44d
     mprintf("    --detect-broken                      Try to detect broken executable files\n");
     mprintf("    --block-encrypted                    Block encrypted archives\n");
d272908a
     mprintf("    --block-max                          Block archives that exceed limits\n");
a36e6e5c
     mprintf("    --mail-follow-urls                   Download and scan URLs\n");
     mprintf("\n");
281c7642
     mprintf("    --max-scansize=#n                    FIXMELIMITS\n");
     mprintf("    --max-filesize=#n                    FIXMELIMITS\n");
     mprintf("    --max-files=#n                       FIXMELIMITS\n");
dab42957
     mprintf("    --max-recursion=#n                   Maximum archive recursion level\n");
14dee074
     mprintf("    --max-dir-recursion=#n               Maximum directory recursion level\n");
5def21ff
     mprintf("    --unzip[=FULLPATH]                   Enable support for .zip files\n");
     mprintf("    --unrar[=FULLPATH]                   Enable support for .rar files\n");
     mprintf("    --arj[=FULLPATH]                     Enable support for .arj files\n");
     mprintf("    --unzoo[=FULLPATH]                   Enable support for .zoo files\n");
     mprintf("    --lha[=FULLPATH]                     Enable support for .lha files\n");
     mprintf("    --jar[=FULLPATH]                     Enable support for .jar files\n");
     mprintf("    --tar[=FULLPATH]                     Enable support for .tar files\n");
a36e6e5c
     mprintf("    --deb[=FULLPATH to ar]               Enable support for .deb files\n");
d6449522
     mprintf("    --tgz[=FULLPATH]                     Enable support for .tar.gz, .tgz files\n\n");
e3aaff8e
 }