clamdscan/clamdscan.c
e3aaff8e
 /*
e1cbc270
  *  Copyright (C) 2013-2019 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  *  Copyright (C) 2007-2013 Sourcefire, Inc.
086eab5c
  *
  *  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>
80dea059
 #ifdef HAVE_UNISTD_H
e3aaff8e
 #include <unistd.h>
80dea059
 #endif
4cd80898
 #ifndef _WIN32
e3aaff8e
 #include <sys/time.h>
4cd80898
 #endif
e3aaff8e
 #include <time.h>
fe6c6a02
 #include <signal.h>
e3aaff8e
 
bd249d78
 #include "clamav.h"
 
60d8d2c3
 #include "libclamav/clamav.h"
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;
247fe3ae
 struct optstruct *clamdopts = NULL;
63abd169
 
2a363377
 static void print_server_version(const struct optstruct *opt)
 {
288057e9
     if (get_clamd_version(opt)) {
         /* can't get version from server, fallback */
         printf("ClamAV %s\n", get_version());
2a363377
     }
 }
 
7b8edc5c
 int main(int argc, char **argv)
e3aaff8e
 {
288057e9
     int ds, dms, ret, infected = 0, err = 0;
     struct timeval t1, t2;
     time_t starttime;
     struct optstruct *opts;
     const struct optstruct *opt;
80dea059
 #ifndef _WIN32
288057e9
     struct sigaction sigact;
80dea059
 #endif
a68d5e2f
 
288057e9
     if ((opts = optparse(NULL, argc, argv, 1, OPT_CLAMDSCAN, OPT_CLAMSCAN, NULL)) == NULL) {
         mprintf("!Can't parse command line options\n");
         return 2;
7b8edc5c
     }
e3aaff8e
 
288057e9
     if ((clamdopts = optparse(optget(opts, "config-file")->strarg, 0, NULL, 1, OPT_CLAMD, 0, NULL)) == NULL) {
         logg("!Can't parse clamd configuration file %s\n", optget(opts, "config-file")->strarg);
         return 2;
247fe3ae
     }
 
288057e9
     if (optget(opts, "verbose")->enabled) {
         mprintf_verbose = 1;
         logg_verbose    = 1;
afb48b28
     }
e3aaff8e
 
288057e9
     if (optget(opts, "quiet")->enabled)
         mprintf_quiet = 1;
e3aaff8e
 
288057e9
     if (optget(opts, "stdout")->enabled)
         mprintf_stdout = 1;
e3aaff8e
 
288057e9
     if (optget(opts, "version")->enabled) {
         print_server_version(opts);
         optfree(opts);
         optfree(clamdopts);
         exit(0);
e3aaff8e
     }
 
288057e9
     if (optget(opts, "help")->enabled) {
         optfree(opts);
         optfree(clamdopts);
         help();
e3aaff8e
     }
 
288057e9
     if (optget(opts, "infected")->enabled)
         printinfected = 1;
e3aaff8e
 
     /* initialize logger */
 
288057e9
     if ((opt = optget(opts, "log"))->enabled) {
         logg_file = opt->strarg;
         if (logg("--------------------------------------\n")) {
             mprintf("!Problem with internal logger.\n");
             optfree(opts);
             optfree(clamdopts);
             exit(2);
         }
     } else
         logg_file = NULL;
 
     if (optget(opts, "reload")->enabled) {
         ret = reload_clamd_database(opts);
         optfree(opts);
         optfree(clamdopts);
         logg_close();
         exit(ret);
c1c9d9f9
     }
 
288057e9
     if (actsetup(opts)) {
         optfree(opts);
         optfree(clamdopts);
         logg_close();
         exit(2);
ee6702ab
     }
 
80dea059
 #ifndef _WIN32
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);
80dea059
 #endif
abd6d2c7
 
e3aaff8e
     time(&starttime);
     /* ctime() does \n, but I need it once more */
 
b8f3f028
     gettimeofday(&t1, NULL);
fe6c6a02
 
dd5092ff
     ret = client(opts, &infected, &err);
247fe3ae
     optfree(clamdopts);
e3aaff8e
 
7b8edc5c
     /* TODO: Implement STATUS in clamd */
288057e9
     if (!optget(opts, "no-summary")->enabled) {
         gettimeofday(&t2, NULL);
         ds  = t2.tv_sec - t1.tv_sec;
         dms = t2.tv_usec - t1.tv_usec;
         ds -= (dms < 0) ? (1) : (0);
         dms += (dms < 0) ? (1000000) : (0);
         logg("\n----------- SCAN SUMMARY -----------\n");
         logg("Infected files: %d\n", infected);
         if (err)
             logg("Total errors: %d\n", err);
         if (notremoved) {
             logg("Not removed: %d\n", notremoved);
         }
         if (notmoved) {
             logg("Not moved: %d\n", notmoved);
         }
         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);
694e7882
 
58bcf502
     exit(ret);
e3aaff8e
 }
 
 void help(void)
 {
     mprintf_stdout = 1;
 
     mprintf("\n");
e098cdc5
     mprintf("                      Clam AntiVirus: Daemon Client %s\n", get_version());
964a1e73
     mprintf("           By The ClamAV Team: https://www.clamav.net/about.html#credits\n");
e1cbc270
     mprintf("           (C) 2019 Cisco Systems, Inc.\n");
e098cdc5
     mprintf("\n");
     mprintf("    clamdscan [options] [file/directory/-]\n");
     mprintf("\n");
     mprintf("    --help              -h             Show this help\n");
5def21ff
     mprintf("    --version           -V             Print version number and exit\n");
     mprintf("    --verbose           -v             Be verbose\n");
     mprintf("    --quiet                            Be quiet, only output error messages\n");
edb23158
     mprintf("    --stdout                           Write to stdout instead of stderr. Does not affect 'debug' messages.\n");
5def21ff
     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");
1feaa72a
     mprintf("    --allmatch            -z           Continue scanning within file after finding a match.\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);
 }