2003-07-29 15:48:06 +00:00
/*
2025-02-14 10:24:30 -05:00
* Copyright ( C ) 2013 - 2025 Cisco Systems , Inc . and / or its affiliates . All rights reserved .
2019-01-25 10:15:50 -05:00
* Copyright ( C ) 2007 - 2013 Sourcefire , Inc .
2009-02-13 10:55:45 +00:00
*
* Authors : Tomasz Kojm , aCaB
2003-07-29 15:48:06 +00:00
*
* This program is free software ; you can redistribute it and / or modify
2007-03-31 20:31:04 +00:00
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation .
2003-07-29 15:48:06 +00:00
*
* 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
2006-04-09 19:59:28 +00:00
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
2003-07-29 15:48:06 +00:00
*/
2004-02-06 13:46:08 +00:00
# if HAVE_CONFIG_H
# include "clamav-config.h"
# endif
2003-07-29 15:48:06 +00:00
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
2010-01-31 17:12:34 +01:00
# ifdef HAVE_UNISTD_H
2003-07-29 15:48:06 +00:00
# include <unistd.h>
2010-01-31 17:12:34 +01:00
# endif
2009-09-24 19:23:21 +02:00
# ifndef _WIN32
2003-07-29 15:48:06 +00:00
# include <sys/time.h>
2009-09-24 19:23:21 +02:00
# endif
2003-07-29 15:48:06 +00:00
# include <time.h>
2004-06-22 16:50:58 +00:00
# include <signal.h>
2003-07-29 15:48:06 +00:00
Add CMake build tooling
This patch adds experimental-quality CMake build tooling.
The libmspack build required a modification to use "" instead of <> for
header #includes. This will hopefully be included in the libmspack
upstream project when adding CMake build tooling to libmspack.
Removed use of libltdl when using CMake.
Flex & Bison are now required to build.
If -DMAINTAINER_MODE, then GPERF is also required, though it currently
doesn't actually do anything. TODO!
I found that the autotools build system was generating the lexer output
but not actually compiling it, instead using previously generated (and
manually renamed) lexer c source. As a consequence, changes to the .l
and .y files weren't making it into the build. To resolve this, I
removed generated flex/bison files and fixed the tooling to use the
freshly generated files. Flex and bison are now required build tools.
On Windows, this adds a dependency on the winflexbison package,
which can be obtained using Chocolatey or may be manually installed.
CMake tooling only has partial support for building with external LLVM
library, and no support for the internal LLVM (to be removed in the
future). I.e. The CMake build currently only supports the bytecode
interpreter.
Many files used include paths relative to the top source directory or
relative to the current project, rather than relative to each build
target. Modern CMake support requires including internal dependency
headers the same way you would external dependency headers (albeit
with "" instead of <>). This meant correcting all header includes to
be relative to the build targets and not relative to the workspace.
For example, ...
```c
include "../libclamav/clamav.h"
include "clamd/clamd_others.h"
```
... becomes:
```c
// libclamav
include "clamav.h"
// clamd
include "clamd_others.h"
```
Fixes header name conflicts by renaming a few of the files.
Converted the "shared" code into a static library, which depends on
libclamav. The ironically named "shared" static library provides
features common to the ClamAV apps which are not required in
libclamav itself and are not intended for use by downstream projects.
This change was required for correct modern CMake practices but was
also required to use the automake "subdir-objects" option.
This eliminates warnings when running autoreconf which, in the next
version of autoconf & automake are likely to break the build.
libclamav used to build in multiple stages where an earlier stage is
a static library containing utils required by the "shared" code.
Linking clamdscan and clamdtop with this libclamav utils static lib
allowed these two apps to function without libclamav. While this is
nice in theory, the practical gains are minimal and it complicates
the build system. As such, the autotools and CMake tooling was
simplified for improved maintainability and this feature was thrown
out. clamdtop and clamdscan now require libclamav to function.
Removed the nopthreads version of the autotools
libclamav_internal_utils static library and added pthread linking to
a couple apps that may have issues building on some platforms without
it, with the intention of removing needless complexity from the
source. Kept the regular version of libclamav_internal_utils.la
though it is no longer used anywhere but in libclamav.
Added an experimental doxygen build option which attempts to build
clamav.h and libfreshclam doxygen html docs.
The CMake build tooling also may build the example program(s), which
isn't a feature in the Autotools build system.
Changed C standard to C90+ due to inline linking issues with socket.h
when linking libfreshclam.so on Linux.
Generate common.rc for win32.
Fix tabs/spaces in shared Makefile.am, and remove vestigial ifndef
from misc.c.
Add CMake files to the automake dist, so users can try the new
CMake tooling w/out having to build from a git clone.
clamonacc changes:
- Renamed FANOTIFY macro to HAVE_SYS_FANOTIFY_H to better match other
similar macros.
- Added a new clamav-clamonacc.service systemd unit file, based on
the work of ChadDevOps & Aaron Brighton.
- Added missing clamonacc man page.
Updates to clamdscan man page, add missing options.
Remove vestigial CL_NOLIBCLAMAV definitions (all apps now use
libclamav).
Rename Windows mspack.dll to libmspack.dll so all ClamAV-built
libraries have the lib-prefix with Visual Studio as with CMake.
2020-08-13 00:25:34 -07:00
// libclamav
2014-06-25 13:34:16 -04:00
# include "clamav.h"
2021-03-04 19:39:50 -08:00
// common
Add CMake build tooling
This patch adds experimental-quality CMake build tooling.
The libmspack build required a modification to use "" instead of <> for
header #includes. This will hopefully be included in the libmspack
upstream project when adding CMake build tooling to libmspack.
Removed use of libltdl when using CMake.
Flex & Bison are now required to build.
If -DMAINTAINER_MODE, then GPERF is also required, though it currently
doesn't actually do anything. TODO!
I found that the autotools build system was generating the lexer output
but not actually compiling it, instead using previously generated (and
manually renamed) lexer c source. As a consequence, changes to the .l
and .y files weren't making it into the build. To resolve this, I
removed generated flex/bison files and fixed the tooling to use the
freshly generated files. Flex and bison are now required build tools.
On Windows, this adds a dependency on the winflexbison package,
which can be obtained using Chocolatey or may be manually installed.
CMake tooling only has partial support for building with external LLVM
library, and no support for the internal LLVM (to be removed in the
future). I.e. The CMake build currently only supports the bytecode
interpreter.
Many files used include paths relative to the top source directory or
relative to the current project, rather than relative to each build
target. Modern CMake support requires including internal dependency
headers the same way you would external dependency headers (albeit
with "" instead of <>). This meant correcting all header includes to
be relative to the build targets and not relative to the workspace.
For example, ...
```c
include "../libclamav/clamav.h"
include "clamd/clamd_others.h"
```
... becomes:
```c
// libclamav
include "clamav.h"
// clamd
include "clamd_others.h"
```
Fixes header name conflicts by renaming a few of the files.
Converted the "shared" code into a static library, which depends on
libclamav. The ironically named "shared" static library provides
features common to the ClamAV apps which are not required in
libclamav itself and are not intended for use by downstream projects.
This change was required for correct modern CMake practices but was
also required to use the automake "subdir-objects" option.
This eliminates warnings when running autoreconf which, in the next
version of autoconf & automake are likely to break the build.
libclamav used to build in multiple stages where an earlier stage is
a static library containing utils required by the "shared" code.
Linking clamdscan and clamdtop with this libclamav utils static lib
allowed these two apps to function without libclamav. While this is
nice in theory, the practical gains are minimal and it complicates
the build system. As such, the autotools and CMake tooling was
simplified for improved maintainability and this feature was thrown
out. clamdtop and clamdscan now require libclamav to function.
Removed the nopthreads version of the autotools
libclamav_internal_utils static library and added pthread linking to
a couple apps that may have issues building on some platforms without
it, with the intention of removing needless complexity from the
source. Kept the regular version of libclamav_internal_utils.la
though it is no longer used anywhere but in libclamav.
Added an experimental doxygen build option which attempts to build
clamav.h and libfreshclam doxygen html docs.
The CMake build tooling also may build the example program(s), which
isn't a feature in the Autotools build system.
Changed C standard to C90+ due to inline linking issues with socket.h
when linking libfreshclam.so on Linux.
Generate common.rc for win32.
Fix tabs/spaces in shared Makefile.am, and remove vestigial ifndef
from misc.c.
Add CMake files to the automake dist, so users can try the new
CMake tooling w/out having to build from a git clone.
clamonacc changes:
- Renamed FANOTIFY macro to HAVE_SYS_FANOTIFY_H to better match other
similar macros.
- Added a new clamav-clamonacc.service systemd unit file, based on
the work of ChadDevOps & Aaron Brighton.
- Added missing clamonacc man page.
Updates to clamdscan man page, add missing options.
Remove vestigial CL_NOLIBCLAMAV definitions (all apps now use
libclamav).
Rename Windows mspack.dll to libmspack.dll so all ClamAV-built
libraries have the lib-prefix with Visual Studio as with CMake.
2020-08-13 00:25:34 -07:00
# include "output.h"
# include "misc.h"
# include "optparser.h"
# include "actions.h"
2003-07-29 15:48:06 +00:00
2008-12-30 21:16:02 +00:00
# include "client.h"
2006-05-15 18:30:18 +00:00
2003-07-29 15:48:06 +00:00
void help ( void ) ;
2008-10-07 21:29:57 +00:00
extern int printinfected ;
2010-05-07 21:14:34 +02:00
struct optstruct * clamdopts = NULL ;
2004-11-28 23:26:29 +00:00
2008-01-10 13:57:05 +00:00
static void print_server_version ( const struct optstruct * opt )
{
2018-12-03 12:40:13 -05:00
if ( get_clamd_version ( opt ) ) {
/* can't get version from server, fallback */
printf ( " ClamAV %s \n " , get_version ( ) ) ;
2008-01-10 13:57:05 +00:00
}
}
2006-05-15 18:30:18 +00:00
int main ( int argc , char * * argv )
2003-07-29 15:48:06 +00:00
{
2018-12-03 12:40:13 -05:00
int ds , dms , ret , infected = 0 , err = 0 ;
struct timeval t1 , t2 ;
2020-01-03 15:53:29 -05:00
time_t date_start , date_end ;
2019-11-03 17:05:25 -05:00
2018-12-03 12:40:13 -05:00
struct optstruct * opts ;
const struct optstruct * opt ;
2019-09-16 13:43:02 -07:00
char buffer [ 26 ] ;
2025-07-25 22:42:43 +01:00
# ifdef _WIN32
SetConsoleOutputCP ( CP_UTF8 ) ;
# else /* !_WIN32 */
2018-12-03 12:40:13 -05:00
struct sigaction sigact ;
2010-01-31 17:12:34 +01:00
# endif
2008-12-30 21:16:02 +00:00
2018-12-03 12:40:13 -05:00
if ( ( opts = optparse ( NULL , argc , argv , 1 , OPT_CLAMDSCAN , OPT_CLAMSCAN , NULL ) ) = = NULL ) {
2022-02-16 00:13:55 +01:00
mprintf ( LOGG_ERROR , " Can't parse command line options \n " ) ;
2020-08-14 17:55:17 -07:00
exit ( 2 ) ;
2006-05-15 18:30:18 +00:00
}
2003-07-29 15:48:06 +00:00
2017-04-28 01:41:25 -05:00
if ( optget ( opts , " help " ) - > enabled ) {
optfree ( opts ) ;
help ( ) ;
}
2018-12-03 12:40:13 -05:00
if ( ( clamdopts = optparse ( optget ( opts , " config-file " ) - > strarg , 0 , NULL , 1 , OPT_CLAMD , 0 , NULL ) ) = = NULL ) {
2022-02-16 00:13:55 +01:00
logg ( LOGG_ERROR , " Can't parse clamd configuration file %s \n " , optget ( opts , " config-file " ) - > strarg ) ;
2020-03-13 09:36:21 -07:00
optfree ( opts ) ;
2020-08-14 17:55:17 -07:00
exit ( 2 ) ;
2010-05-07 21:14:34 +02:00
}
2018-12-03 12:40:13 -05:00
if ( optget ( opts , " verbose " ) - > enabled ) {
mprintf_verbose = 1 ;
logg_verbose = 1 ;
2004-03-29 00:00:58 +00:00
}
2003-07-29 15:48:06 +00:00
2018-12-03 12:40:13 -05:00
if ( optget ( opts , " quiet " ) - > enabled )
mprintf_quiet = 1 ;
2003-07-29 15:48:06 +00:00
2018-12-03 12:40:13 -05:00
if ( optget ( opts , " stdout " ) - > enabled )
mprintf_stdout = 1 ;
2003-07-29 15:48:06 +00:00
2018-12-03 12:40:13 -05:00
if ( optget ( opts , " version " ) - > enabled ) {
print_server_version ( opts ) ;
optfree ( opts ) ;
optfree ( clamdopts ) ;
exit ( 0 ) ;
2003-07-29 15:48:06 +00:00
}
2019-12-18 13:20:12 -05:00
if ( optget ( opts , " ping " ) - > enabled & & ! optget ( opts , " wait " ) - > enabled ) {
2020-08-14 17:55:17 -07:00
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 ;
}
2019-12-18 13:20:12 -05:00
optfree ( opts ) ;
optfree ( clamdopts ) ;
2020-08-14 17:55:17 -07:00
exit ( ret ) ;
2019-12-18 13:20:12 -05:00
}
2018-12-03 12:40:13 -05:00
if ( optget ( opts , " infected " ) - > enabled )
printinfected = 1 ;
2003-07-29 15:48:06 +00:00
/* initialize logger */
2018-12-03 12:40:13 -05:00
if ( ( opt = optget ( opts , " log " ) ) - > enabled ) {
logg_file = opt - > strarg ;
2022-02-16 00:13:55 +01:00
if ( logg ( LOGG_INFO , " -------------------------------------- \n " ) ) {
mprintf ( LOGG_ERROR , " Problem with internal logger. \n " ) ;
2018-12-03 12:40:13 -05:00
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 ) ;
2008-09-18 08:17:47 +00:00
}
2018-12-03 12:40:13 -05:00
if ( actsetup ( opts ) ) {
optfree ( opts ) ;
optfree ( clamdopts ) ;
logg_close ( ) ;
exit ( 2 ) ;
2009-02-12 15:14:12 +00:00
}
2010-01-31 17:12:34 +01:00
# ifndef _WIN32
2009-02-08 16:46:26 +00:00
memset ( & sigact , 0 , sizeof ( struct sigaction ) ) ;
sigact . sa_handler = SIG_IGN ;
sigemptyset ( & sigact . sa_mask ) ;
sigaddset ( & sigact . sa_mask , SIGPIPE ) ;
sigaction ( SIGPIPE , & sigact , NULL ) ;
2010-01-31 17:12:34 +01:00
# endif
2009-01-16 18:29:00 +00:00
2020-01-03 15:53:29 -05:00
date_start = time ( NULL ) ;
2009-10-12 23:38:38 +02:00
gettimeofday ( & t1 , NULL ) ;
2004-06-22 16:50:58 +00:00
2010-02-03 18:23:30 +01:00
ret = client ( opts , & infected , & err ) ;
2010-05-07 21:14:34 +02:00
optfree ( clamdopts ) ;
2003-07-29 15:48:06 +00:00
2006-05-15 18:30:18 +00:00
/* TODO: Implement STATUS in clamd */
2018-12-03 12:40:13 -05:00
if ( ! optget ( opts , " no-summary " ) - > enabled ) {
2020-01-03 15:53:29 -05:00
struct tm tmp ;
2019-11-03 17:05:25 -05:00
2020-01-03 15:53:29 -05:00
date_end = time ( NULL ) ;
2018-12-03 12:40:13 -05:00
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 ) ;
2022-02-16 00:13:55 +01:00
logg ( LOGG_INFO , " \n ----------- SCAN SUMMARY ----------- \n " ) ;
logg ( LOGG_INFO , " Infected files: %d \n " , infected ) ;
2018-12-03 12:40:13 -05:00
if ( err )
2022-02-16 00:13:55 +01:00
logg ( LOGG_INFO , " Total errors: %d \n " , err ) ;
2018-12-03 12:40:13 -05:00
if ( notremoved ) {
2022-02-16 00:13:55 +01:00
logg ( LOGG_INFO , " Not removed: %d \n " , notremoved ) ;
2018-12-03 12:40:13 -05:00
}
if ( notmoved ) {
2022-02-16 00:13:55 +01:00
logg ( LOGG_INFO , " Not moved: %d \n " , notmoved ) ;
2018-12-03 12:40:13 -05:00
}
2022-02-16 00:13:55 +01:00
logg ( LOGG_INFO , " Time: %d.%3.3d sec (%d m %d s) \n " , ds , dms / 1000 , ds / 60 , ds % 60 ) ;
2019-11-03 17:05:25 -05:00
# ifdef _WIN32
2020-01-03 15:53:29 -05:00
if ( 0 ! = localtime_s ( & tmp , & date_start ) ) {
2019-11-03 17:05:25 -05:00
# else
2020-01-03 15:53:29 -05:00
if ( ! localtime_r ( & date_start , & tmp ) ) {
2019-11-03 17:05:25 -05:00
# endif
2022-02-16 00:13:55 +01:00
logg ( LOGG_ERROR , " Failed to get local time for Start Date. \n " ) ;
2020-01-03 15:53:29 -05:00
}
2019-11-03 17:05:25 -05:00
strftime ( buffer , sizeof ( buffer ) , " %Y:%m:%d %H:%M:%S " , & tmp ) ;
2022-02-16 00:13:55 +01:00
logg ( LOGG_INFO , " Start Date: %s \n " , buffer ) ;
2019-11-03 17:05:25 -05:00
# ifdef _WIN32
2020-01-03 15:53:29 -05:00
if ( 0 ! = localtime_s ( & tmp , & date_end ) ) {
2019-11-03 17:05:25 -05:00
# else
2020-01-03 15:53:29 -05:00
if ( ! localtime_r ( & date_end , & tmp ) ) {
2019-11-03 17:05:25 -05:00
# endif
2022-02-16 00:13:55 +01:00
logg ( LOGG_ERROR , " Failed to get local time for End Date. \n " ) ;
2020-01-03 15:53:29 -05:00
}
2019-11-03 17:05:25 -05:00
strftime ( buffer , sizeof ( buffer ) , " %Y:%m:%d %H:%M:%S " , & tmp ) ;
2022-02-16 00:13:55 +01:00
logg ( LOGG_INFO , " End Date: %s \n " , buffer ) ;
2003-07-29 15:48:06 +00:00
}
2008-09-18 08:17:47 +00:00
logg_close ( ) ;
2008-12-30 21:16:02 +00:00
optfree ( opts ) ;
2019-03-26 15:09:52 -04:00
2004-03-30 21:11:25 +00:00
exit ( ret ) ;
2003-07-29 15:48:06 +00:00
}
void help ( void )
{
mprintf_stdout = 1 ;
2022-02-16 00:13:55 +01:00
mprintf ( LOGG_INFO , " \n " ) ;
mprintf ( LOGG_INFO , " Clam AntiVirus: Daemon Client %s \n " , get_version ( ) ) ;
mprintf ( LOGG_INFO , " By The ClamAV Team: https://www.clamav.net/about.html#credits \n " ) ;
2025-02-14 10:24:30 -05:00
mprintf ( LOGG_INFO , " (C) 2025 Cisco Systems, Inc. \n " ) ;
2022-02-16 00:13:55 +01:00
mprintf ( LOGG_INFO , " \n " ) ;
mprintf ( LOGG_INFO , " clamdscan [options] [file/directory/-] \n " ) ;
mprintf ( LOGG_INFO , " \n " ) ;
mprintf ( LOGG_INFO , " --help -h Show this help \n " ) ;
mprintf ( LOGG_INFO , " --version -V Print version number and exit \n " ) ;
mprintf ( LOGG_INFO , " --verbose -v Be verbose \n " ) ;
mprintf ( LOGG_INFO , " --quiet Be quiet, only output error messages \n " ) ;
mprintf ( LOGG_INFO , " --stdout Write to stdout instead of stderr. Does not affect 'debug' messages. \n " ) ;
mprintf ( LOGG_INFO , " (this help is always written to stdout) \n " ) ;
mprintf ( LOGG_INFO , " --log=FILE -l FILE Save scan report in FILE \n " ) ;
mprintf ( LOGG_INFO , " --file-list=FILE -f FILE Scan files from FILE \n " ) ;
mprintf ( LOGG_INFO , " --ping -p A[:I] Ping clamd up to [A] times at optional interval [I] until it responds. \n " ) ;
mprintf ( LOGG_INFO , " --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 " ) ;
mprintf ( LOGG_INFO , " --remove Remove infected files. Be careful! \n " ) ;
mprintf ( LOGG_INFO , " --move=DIRECTORY Move infected files into DIRECTORY \n " ) ;
mprintf ( LOGG_INFO , " --copy=DIRECTORY Copy infected files into DIRECTORY \n " ) ;
2023-02-04 05:36:12 +08:00
mprintf ( LOGG_INFO , " --config-file=FILE -c Read configuration from FILE. \n " ) ;
2021-08-27 09:14:45 -07:00
# ifdef _WIN32
2022-02-16 00:13:55 +01:00
mprintf ( LOGG_INFO , " --memory Scan loaded executable modules \n " ) ;
mprintf ( LOGG_INFO , " --kill Kill/Unload infected loaded modules \n " ) ;
mprintf ( LOGG_INFO , " --unload Unload infected modules from processes \n " ) ;
2021-08-27 09:14:45 -07:00
# endif
2022-02-16 00:13:55 +01:00
mprintf ( LOGG_INFO , " --allmatch -z Continue scanning within file after finding a match. \n " ) ;
mprintf ( LOGG_INFO , " --multiscan -m Force MULTISCAN mode \n " ) ;
mprintf ( LOGG_INFO , " --infected -i Only print infected files \n " ) ;
mprintf ( LOGG_INFO , " --no-summary Disable summary at end of scanning \n " ) ;
mprintf ( LOGG_INFO , " --reload Request clamd to reload virus database \n " ) ;
mprintf ( LOGG_INFO , " --fdpass Pass filedescriptor to clamd (useful if clamd is running as a different user) \n " ) ;
mprintf ( LOGG_INFO , " --stream Force streaming files to clamd (for debugging and unit testing) \n " ) ;
mprintf ( LOGG_INFO , " \n " ) ;
2003-07-29 15:48:06 +00:00
exit ( 0 ) ;
}