clamdscan/clamdscan.c
e3aaff8e
 /*
086eab5c
  *  Copyright (C) 2007-2009 Sourcefire, Inc.
  *
  *  Authors: Tomasz Kojm, aCaB
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 <string.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/time.h>
 #include <time.h>
fe6c6a02
 #include <signal.h>
e3aaff8e
 
a68d5e2f
 #include "shared/output.h"
 #include "shared/misc.h"
 #include "shared/optparser.h"
ee6702ab
 #include "shared/actions.h"
e3aaff8e
 
a68d5e2f
 #include "client.h"
7b8edc5c
 
e3aaff8e
 void help(void);
 
d61aeda2
 extern int printinfected;
63abd169
 
2a363377
 static void print_server_version(const struct optstruct *opt)
 {
     if(get_clamd_version(opt)) {
 	/* can't get version from server, fallback */
add738d2
 	printf("ClamAV %s\n", get_version());
2a363377
     }
 }
 
7b8edc5c
 int main(int argc, char **argv)
e3aaff8e
 {
fe6c6a02
 	int ds, dms, ret, infected;
e3aaff8e
 	struct timeval t1, t2;
 	struct timezone tz;
 	time_t starttime;
a68d5e2f
         struct optstruct *opts;
         const struct optstruct *opt;
c72f64c8
 	struct sigaction sigact;
a68d5e2f
 
 
     if((opts = optparse(NULL, argc, argv, 1, OPT_CLAMDSCAN, OPT_CLAMSCAN, NULL)) == NULL) {
 	mprintf("!Can't parse command line options\n");
7b8edc5c
 	return 2;
     }
e3aaff8e
 
a68d5e2f
     if(optget(opts, "verbose")->enabled) {
afb48b28
 	mprintf_verbose = 1;
 	logg_verbose = 1;
     }
e3aaff8e
 
a68d5e2f
     if(optget(opts, "quiet")->enabled)
58bcf502
 	mprintf_quiet = 1;
e3aaff8e
 
a68d5e2f
     if(optget(opts, "stdout")->enabled)
58bcf502
 	mprintf_stdout = 1;
e3aaff8e
 
a68d5e2f
     if(optget(opts, "version")->enabled) {
 	print_server_version(opts);
 	optfree(opts);
58bcf502
 	exit(0);
e3aaff8e
     }
 
a68d5e2f
     if(optget(opts, "help")->enabled) {
 	optfree(opts);
e3aaff8e
     	help();
     }
 
a68d5e2f
     if(optget(opts, "infected")->enabled)
58bcf502
 	printinfected = 1;
e3aaff8e
 
     /* initialize logger */
 
a68d5e2f
     if((opt = optget(opts, "log"))->enabled) {
 	logg_file = opt->strarg;
e3aaff8e
 	if(logg("--------------------------------------\n")) {
 	    mprintf("!Problem with internal logger.\n");
a68d5e2f
 	    optfree(opts);
58bcf502
 	    exit(2);
e3aaff8e
 	}
     } else 
afb48b28
 	logg_file = NULL;
e3aaff8e
 
 
a68d5e2f
     if(optget(opts, "reload")->enabled) {
3bf21904
 	ret = reload_clamd_database(opts);
a68d5e2f
 	optfree(opts);
c1c9d9f9
 	logg_close();
 	exit(ret);
     }
 
ee6702ab
     if(actsetup(opts)) {
 	optfree(opts);
 	logg_close();
 	exit(2);
     }
 
c72f64c8
     memset(&sigact, 0, sizeof(struct sigaction));
     sigact.sa_handler = SIG_IGN;
     sigemptyset(&sigact.sa_mask);
     sigaddset(&sigact.sa_mask, SIGPIPE);
     sigaction(SIGPIPE, &sigact, NULL);
abd6d2c7
 
e3aaff8e
     time(&starttime);
     /* ctime() does \n, but I need it once more */
 
     gettimeofday(&t1, &tz);
fe6c6a02
 
a68d5e2f
     ret = client(opts, &infected);
e3aaff8e
 
7b8edc5c
     /* TODO: Implement STATUS in clamd */
a68d5e2f
     if((infected || ret != 2) && !optget(opts, "no-summary")->enabled) {
e3aaff8e
 	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);
0ae41a2d
 	logg("\n----------- SCAN SUMMARY -----------\n");
 	logg("Infected files: %d\n", infected);
63abd169
 	if(notremoved) {
0ae41a2d
 	    logg("Not removed: %d\n", notremoved);
63abd169
 	}
 	if(notmoved) {
0ae41a2d
 	    logg("Not moved: %d\n", notmoved);
63abd169
 	}
0ae41a2d
 	logg("Time: %d.%3.3d sec (%d m %d s)\n", ds, dms/1000, ds/60, ds%60);
e3aaff8e
     }
 
c1c9d9f9
     logg_close();
a68d5e2f
     optfree(opts);
58bcf502
     exit(ret);
e3aaff8e
 }
 
 void help(void)
 {
 
     mprintf_stdout = 1;
 
     mprintf("\n");
add738d2
     mprintf("                       ClamAV Daemon Client %s\n", get_version());
6670464f
     printf("           By The ClamAV Team: http://www.clamav.net/team\n");
     printf("           (C) 2007-2009 Sourcefire, Inc.\n\n");
5def21ff
 
     mprintf("    --help              -h             Show help\n");
     mprintf("    --version           -V             Print version number and exit\n");
     mprintf("    --verbose           -v             Be verbose\n");
     mprintf("    --quiet                            Be quiet, only output error messages\n");
     mprintf("    --stdout                           Write to stdout instead of stderr\n");
     mprintf("                                       (this help is always written to stdout)\n");
     mprintf("    --log=FILE          -l FILE        Save scan report in FILE\n");
c2b6681b
     mprintf("    --file-list=FILE    -f FILE        Scan files from FILE\n");
63abd169
     mprintf("    --remove                           Remove infected files. Be careful!\n");
     mprintf("    --move=DIRECTORY                   Move infected files into DIRECTORY\n");
c6d2bbbc
     mprintf("    --copy=DIRECTORY                   Copy infected files into DIRECTORY\n");
5def21ff
     mprintf("    --config-file=FILE                 Read configuration from FILE.\n");
3bb3357a
     mprintf("    --multiscan           -m           Force MULTISCAN mode\n");
     mprintf("    --infected            -i           Only print infected files\n");
dc1e77db
     mprintf("    --no-summary                       Disable summary at end of scanning\n");
c1c9d9f9
     mprintf("    --reload                           Request clamd to reload virus database\n");
408be01f
     mprintf("    --fdpass                           Pass filedescriptor to clamd (useful if clamd is running as a different user)\n");
     mprintf("    --stream                           Force streaming files to clamd (for debugging and unit testing)\n");
5def21ff
     mprintf("\n");
e3aaff8e
 
     exit(0);
 }