clamdscan/clamdscan.c
e3aaff8e
 /*
e7f5f537
  *  Copyright (C) 2013-2021 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
52cddcbc
  *  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
 
9e20cdf6
 // libclamav
bd249d78
 #include "clamav.h"
 
9e20cdf6
 // shared
 #include "output.h"
 #include "misc.h"
 #include "optparser.h"
 #include "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)
 {
72fd33c8
     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
 {
72fd33c8
     int ds, dms, ret, infected = 0, err = 0;
     struct timeval t1, t2;
898c08f0
     time_t date_start, date_end;
f47c524f
 
72fd33c8
     struct optstruct *opts;
     const struct optstruct *opt;
83d6f644
     char buffer[26];
80dea059
 #ifndef _WIN32
72fd33c8
     struct sigaction sigact;
80dea059
 #endif
a68d5e2f
 
72fd33c8
     if ((opts = optparse(NULL, argc, argv, 1, OPT_CLAMDSCAN, OPT_CLAMSCAN, NULL)) == NULL) {
         mprintf("!Can't parse command line options\n");
854d38de
         exit(2);
7b8edc5c
     }
e3aaff8e
 
bc0d9353
     if (optget(opts, "help")->enabled) {
         optfree(opts);
         help();
     }
 
72fd33c8
     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);
1c683bf0
         optfree(opts);
854d38de
         exit(2);
247fe3ae
     }
 
72fd33c8
     if (optget(opts, "verbose")->enabled) {
         mprintf_verbose = 1;
         logg_verbose    = 1;
afb48b28
     }
e3aaff8e
 
72fd33c8
     if (optget(opts, "quiet")->enabled)
         mprintf_quiet = 1;
e3aaff8e
 
72fd33c8
     if (optget(opts, "stdout")->enabled)
         mprintf_stdout = 1;
e3aaff8e
 
72fd33c8
     if (optget(opts, "version")->enabled) {
         print_server_version(opts);
         optfree(opts);
         optfree(clamdopts);
         exit(0);
e3aaff8e
     }
 
f3e895c9
     if (optget(opts, "ping")->enabled && !optget(opts, "wait")->enabled) {
854d38de
         int16_t ping_result = ping_clamd(opts);
         switch (ping_result) {
             case 0:
                 ret = 0;
                 break;
             case 1:
                 ret = (int)CL_ETIMEOUT;
                 break;
             default:
                 ret = (int)CL_ERROR;
                 break;
         }
f3e895c9
         optfree(opts);
         optfree(clamdopts);
854d38de
         exit(ret);
f3e895c9
     }
 
72fd33c8
     if (optget(opts, "infected")->enabled)
         printinfected = 1;
e3aaff8e
 
     /* initialize logger */
 
72fd33c8
     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
     }
 
72fd33c8
     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
 
898c08f0
     date_start = time(NULL);
b8f3f028
     gettimeofday(&t1, NULL);
fe6c6a02
 
dd5092ff
     ret = client(opts, &infected, &err);
247fe3ae
     optfree(clamdopts);
e3aaff8e
 
7b8edc5c
     /* TODO: Implement STATUS in clamd */
72fd33c8
     if (!optget(opts, "no-summary")->enabled) {
898c08f0
         struct tm tmp;
f47c524f
 
898c08f0
         date_end = time(NULL);
72fd33c8
         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);
f47c524f
 
 #ifdef _WIN32
898c08f0
         if (0 != localtime_s(&tmp, &date_start)) {
f47c524f
 #else
898c08f0
         if (!localtime_r(&date_start, &tmp)) {
f47c524f
 #endif
898c08f0
             logg("!Failed to get local time for Start Date.\n");
         }
f47c524f
         strftime(buffer, sizeof(buffer), "%Y:%m:%d %H:%M:%S", &tmp);
83d6f644
         logg("Start Date: %s\n", buffer);
f47c524f
 
 #ifdef _WIN32
898c08f0
         if (0 != localtime_s(&tmp, &date_end)) {
f47c524f
 #else
898c08f0
         if (!localtime_r(&date_end, &tmp)) {
f47c524f
 #endif
898c08f0
             logg("!Failed to get local time for End Date.\n");
         }
f47c524f
         strftime(buffer, sizeof(buffer), "%Y:%m:%d %H:%M:%S", &tmp);
         logg("End Date:   %s\n", buffer);
e3aaff8e
     }
 
c1c9d9f9
     logg_close();
a68d5e2f
     optfree(opts);
cef54eaf
 
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");
e7f5f537
     mprintf("           (C) 2021 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");
737ec1ef
     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");
9e08216c
     mprintf("    --ping              -p A[:I]       Ping clamd up to [A] times at optional interval [I] until it responds.\n");
     mprintf("    --wait              -w             Wait up to 30 seconds for clamd to start. Optionally use alongside --ping to set attempts [A] and interval [I] to check clamd.\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);
 }