clamscan/clamscan.c
b151ef55
 /*
3fa35dc1
  *  Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net>
b151ef55
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
8b242bb9
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
b151ef55
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <sys/time.h>
 #include <time.h>
 
 #include "options.h"
 #include "others.h"
 #include "shared.h"
 #include "manager.h"
 #include "defaults.h"
 #include "treewalk.h"
e441c102
 #include "misc.h"
b151ef55
 
36f2038b
 #include "output.h"
 
97de3c9d
 #ifdef C_LINUX
 #include <sys/resource.h>
 #endif
 
b151ef55
 void help(void);
 
7fbb6473
 struct s_info claminfo;
 short recursion = 0, printinfected = 0, bell = 0;
 
d927f46c
 int clamscan(struct optstruct *opt)
b151ef55
 {
 	int ds, dms, ret;
 	double mb;
 	struct timeval t1, t2;
 	struct timezone tz;
 	time_t starttime;
 
     /* initialize some important variables */
 
36f2038b
     if(optc(opt, 'v')) {
 	mprintf_verbose = 1;
 	logg_verbose = 1;
     }
b151ef55
 
7fbb6473
     if(optl(opt, "quiet"))
 	mprintf_quiet = 1;
b151ef55
 
7fbb6473
     if(optl(opt, "stdout"))
 	mprintf_stdout = 1;
b151ef55
 
97de3c9d
     if(optl(opt, "debug")) {
 #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 */
     }
0bcad2b1
 
b151ef55
     if(optc(opt, 'V')) {
e441c102
 	print_version();
d927f46c
 	return 0;
b151ef55
     }
 
     if(optc(opt, 'h')) {
 	free_opt(opt);
     	help();
     }
 
d99b1840
     if(strcmp(VERSION, cl_retver())) {
 	mprintf("WARNING: Version mismatch (clamscan: "VERSION", libclamav: %s)\n", cl_retver());
 	mprintf("See the FAQ at http://www.clamav.net/faq.html\n");
     }
 
b151ef55
     /* check other options */
 
7fbb6473
     if(optc(opt, 'r'))
 	recursion = 1;
b151ef55
 
7fbb6473
     if(optc(opt, 'i'))
 	printinfected = 1;
b151ef55
 
7fbb6473
     if(optl(opt, "bell"))
 	bell = 1;
026ebd88
 
3506c157
     if(optl(opt, "tempdir"))
 	cl_settempdir(getargl(opt, "tempdir"), 0);
 
     if(optl(opt, "leave-temps"))
 	cl_settempdir(NULL, 1);
 
b151ef55
     /* initialize logger */
 
     if(optc(opt, 'l')) {
36f2038b
 	logg_file = getargc(opt, 'l');
b151ef55
 	if(logg("--------------------------------------\n")) {
 	    mprintf("!Problem with internal logger.\n");
e483f51a
 	    return 62;
b151ef55
 	}
     } else 
36f2038b
 	logg_file = NULL;
b151ef55
 
     /* we need some pre-checks */
     if(optl(opt, "max-space"))
 	if(!strchr(getargl(opt, "max-space"), 'M') && !strchr(getargl(opt, "max-space"), 'm'))
 	    if(!isnumb(getargl(opt, "max-space"))) {
 		mprintf("!--max-space requires natural number.\n");
e483f51a
 		return 40;
b151ef55
 	    }
 
     if(optl(opt, "max-files"))
 	if(!isnumb(getargl(opt, "max-files"))) {
 	    mprintf("!--max-files requires natural number.\n");
e483f51a
 	    return 40;
b151ef55
 	}
 
     if(optl(opt, "max-recursion"))
 	if(!isnumb(getargl(opt, "max-recursion"))) {
 	    mprintf("!--max-recursion requires natural number.\n");
e483f51a
 	    return 40;
b151ef55
 	}
 
c840fa41
     if(optl(opt, "max-dir-recursion"))
 	if(!isnumb(getargl(opt, "max-dir-recursion"))) {
 	    logg("!--max-dir-recursion requires natural number.\n");
 	    return 40;
 	}
 
     if(optl(opt, "max-ratio"))
 	if(!isnumb(getargl(opt, "max-ratio"))) {
 	    logg("!--max-ratio requires natural number.\n");
 	    return 40;
 	}
b151ef55
 
     time(&starttime);
     /* ctime() does \n, but I need it once more */
     logg("Scan started: %s\n", ctime(&starttime));
 
     memset(&claminfo, 0, sizeof(struct s_info));
 
     gettimeofday(&t1, &tz);
     ret = scanmanager(opt);
 
026ebd88
     if(!optl(opt, "disable-summary") && !optl(opt, "no-summary")) {
b151ef55
 	gettimeofday(&t2, &tz);
 	ds = t2.tv_sec - t1.tv_sec;
 	dms = t2.tv_usec - t1.tv_usec;
 	ds -= (dms < 0) ? (1):(0);
 	dms += (dms < 0) ? (1000000):(0);
 	mprintf("\n----------- SCAN SUMMARY -----------\n");
 	    logg("\n-- summary --\n");
d99b1840
 	mprintf("Known viruses: %d\n", claminfo.sigs);
 	    logg("Known viruses: %d\n", claminfo.sigs);
54e4c784
 	mprintf("Engine version: %s\n", cl_retver());
 	    logg("Engine version: %s\n", cl_retver());
b151ef55
 	mprintf("Scanned directories: %d\n", claminfo.dirs);
 	    logg("Scanned directories: %d\n", claminfo.dirs);
 	mprintf("Scanned files: %d\n", claminfo.files);
 	    logg("Scanned files: %d\n", claminfo.files);
 	mprintf("Infected files: %d\n", claminfo.ifiles);
 	    logg("Infected files: %d\n", claminfo.ifiles);
 	if(claminfo.notremoved) {
 	    mprintf("Not removed: %d\n", claminfo.notremoved);
 		logg("Not removed: %d\n", claminfo.notremoved);
 	}
 	if(claminfo.notmoved) {
 	    mprintf("Not moved: %d\n", claminfo.notmoved);
 		logg("Not moved: %d\n", claminfo.notmoved);
 	}
 	mb = claminfo.blocks * (CL_COUNT_PRECISION / 1024) / 1024.0;
 	mprintf("Data scanned: %2.2lf MB\n", mb);
 	    logg("Data scanned: %2.2lf MB\n", mb);
54e4c784
 /*
7b7b3ca5
 	mprintf("I/O buffer size: %d bytes\n", SCANBUFF);
 	    logg("I/O buffer size: %d bytes\n", SCANBUFF);
54e4c784
 */
 
b151ef55
 	mprintf("Time: %d.%3.3d sec (%d m %d s)\n", ds, dms/1000, ds/60, ds%60);
 	    logg("Time: %d.%3.3d sec (%d m %d s)\n", ds, dms/1000, ds/60, ds%60);
     }
 
d927f46c
     return ret;
b151ef55
 }
 
 void help(void)
 {
 
     mprintf_stdout = 1;
 
     mprintf("\n");
8ef2ff6f
     mprintf("                       Clam AntiVirus Scanner "VERSION"\n");
     mprintf("    (C) 2002 - 2005 ClamAV Team - http://www.clamav.net/team.html\n\n");
a0faaedf
 
94da957a
     mprintf("    --help                -h             Print this help screen\n");
     mprintf("    --version             -V             Print version number\n");
a0faaedf
     mprintf("    --verbose             -v             Be verbose\n");
94da957a
     mprintf("    --debug                              Enable libclamav's debug messages\n");
     mprintf("    --quiet                              Only output error messages\n");
a0faaedf
     mprintf("    --stdout                             Write to stdout instead of stderr\n");
94da957a
     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");
 
b151ef55
     mprintf("\n");
c2484690
     mprintf("    --tempdir=DIRECTORY                  Create temporary files in DIRECTORY\n");
     mprintf("    --leave-temps                        Do not remove temporary files\n");
a0faaedf
     mprintf("    --database=FILE/DIR   -d FILE/DIR    Load virus database from FILE or load\n");
cfe76364
     mprintf("                                         all .cvd and .db[2] files from DIR\n");
521b19b4
     mprintf("    --log=FILE            -l FILE        Save scan report to FILE\n");
94da957a
     mprintf("    --recursive           -r             Scan subdirectories recursively\n");
     mprintf("    --remove                             Remove infected files. Be careful!\n");
a0faaedf
     mprintf("    --move=DIRECTORY                     Move infected files into DIRECTORY\n");
7084e554
 #ifdef HAVE_REGEX_H
     mprintf("    --exclude=REGEX                      Don't scan file names matching REGEX\n");
e9a4812d
     mprintf("    --exclude-dir=REGEX                  Don't scan directories matching REGEX\n");
7084e554
     mprintf("    --include=REGEX                      Only scan file names matching REGEX\n");
e9a4812d
     mprintf("    --include-dir=REGEX                  Only scan directories matching REGEX\n");
7084e554
 #else
a0faaedf
     mprintf("    --exclude=PATT                       Don't scan file names containing PATT\n");
e9a4812d
     mprintf("    --exclude-dir=PATT                   Don't scan directories containing PATT\n");
a0faaedf
     mprintf("    --include=PATT                       Only scan file names containing PATT\n");
e9a4812d
     mprintf("    --include-dir=PATT                   Only scan directories containing PATT\n");
7084e554
 #endif
94da957a
 
b151ef55
     mprintf("\n");
94da957a
     mprintf("    --no-mail                            Disable mail file support\n");
c2484690
     mprintf("    --no-pe                              Disable PE analysis\n");
c561d2a3
     mprintf("    --no-ole2                            Disable OLE2 support\n");
2fe19b26
     mprintf("    --no-html                            Disable HTML support\n");
026ebd88
     mprintf("    --no-archive                         Disable libclamav archive support\n");
f8355d13
     mprintf("    --detect-broken                      Try to detect broken executable files\n");
     mprintf("    --block-encrypted                    Block encrypted archives\n");
728f8802
     mprintf("    --block-max                          Block archives that exceed limits\n");
c840fa41
 #ifdef WITH_CURL
94da957a
     mprintf("    --mail-follow-urls                   Download and scan URLs\n");
c840fa41
 #endif
94da957a
     mprintf("\n");
3fa35dc1
     mprintf("    --max-space=#n                       Only extract first #n kilobytes from\n");
     mprintf("                                         archived files\n");
     mprintf("    --max-files=#n                       Only extract first #n files from\n");
     mprintf("                                         archives\n");
     mprintf("    --max-recursion=#n                   Maximum archive recursion level\n");
f8355d13
     mprintf("    --max-ratio=#n                       Maximum compression ratio limit\n");
3fa35dc1
     mprintf("    --max-dir-recursion=#n               Maximum directory recursion level\n");
a0faaedf
     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");
94da957a
     mprintf("    --deb[=FULLPATH to ar]               Enable support for .deb files\n");
a0faaedf
     mprintf("    --tgz[=FULLPATH]                     enable support for .tar.gz, .tgz files\n\n");
b151ef55
 
     exit(0);
 }