2009-02-07 00:59:58 +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) 2009-2013 Sourcefire, Inc.
|
2009-02-07 00:59:58 +00:00
|
|
|
*
|
|
|
|
* Authors: Tomasz Kojm, aCaB
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2009-02-13 11:05:14 +00:00
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
#include "clamav-config.h"
|
|
|
|
#endif
|
|
|
|
|
2014-05-09 13:42:44 -04:00
|
|
|
#if defined(C_SOLARIS)
|
2015-08-21 12:49:08 -04:00
|
|
|
#ifndef __EXTENSIONS__
|
2014-05-09 13:42:44 -04:00
|
|
|
#define __EXTENSIONS__
|
|
|
|
#endif
|
2015-08-21 12:49:08 -04:00
|
|
|
#endif
|
2014-05-09 13:42:44 -04:00
|
|
|
|
2009-02-13 11:05:14 +00:00
|
|
|
/* must be first because it may define _XOPEN_SOURCE */
|
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 "fdpassing.h"
|
2009-02-07 00:59:58 +00:00
|
|
|
#include <stdio.h>
|
2009-09-24 19:23:21 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2009-02-07 00:59:58 +00:00
|
|
|
#include <unistd.h>
|
2009-09-24 19:23:21 +02:00
|
|
|
#endif
|
2009-02-07 00:59:58 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2009-02-28 08:11:04 +00:00
|
|
|
#include <stdlib.h>
|
2009-02-07 00:59:58 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2009-09-24 16:08:52 +02:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
2009-02-07 12:36:22 +00:00
|
|
|
#include <sys/select.h>
|
2009-09-24 16:08:52 +02:00
|
|
|
#endif
|
2009-09-24 19:23:21 +02:00
|
|
|
#ifndef _WIN32
|
2009-02-07 00:59:58 +00:00
|
|
|
#include <arpa/inet.h>
|
2009-09-24 19:23:21 +02:00
|
|
|
#include <sys/socket.h>
|
2015-08-25 10:53:23 -04:00
|
|
|
#include <sys/un.h>
|
2014-05-09 13:42:44 -04:00
|
|
|
#include <netdb.h>
|
2009-09-24 19:23:21 +02:00
|
|
|
#endif
|
2009-02-07 00:59:58 +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
|
|
|
|
#include "clamav.h"
|
|
|
|
#include "others.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 "actions.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "clamdcom.h"
|
2009-02-07 00:59:58 +00:00
|
|
|
|
|
|
|
#include "proto.h"
|
|
|
|
#include "client.h"
|
|
|
|
|
2009-02-08 15:06:10 +00:00
|
|
|
extern unsigned long int maxstream;
|
2009-02-08 14:43:17 +00:00
|
|
|
int printinfected;
|
2010-05-07 21:14:34 +02:00
|
|
|
extern struct optstruct *clamdopts;
|
|
|
|
|
2011-03-09 15:43:21 +01:00
|
|
|
static int ftw_chkpath(const char *path, struct cli_ftw_cbdata *data)
|
|
|
|
{
|
2014-07-11 09:30:58 -04:00
|
|
|
UNUSEDPARAM(data);
|
2021-08-27 09:14:45 -07:00
|
|
|
return chkpath(path, clamdopts);
|
2009-02-07 00:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Used by serial_callback() */
|
|
|
|
struct client_serial_data {
|
|
|
|
int infected;
|
|
|
|
int scantype;
|
2009-03-07 15:32:16 +00:00
|
|
|
int printok;
|
2009-10-16 14:57:15 +02:00
|
|
|
int files;
|
|
|
|
int errors;
|
2009-02-07 00:59:58 +00:00
|
|
|
};
|
|
|
|
|
2009-02-09 16:23:38 +00:00
|
|
|
/* FTW callback for scanning in non IDSESSION mode
|
|
|
|
* Returns SUCCESS or BREAK on success, CL_EXXX on error */
|
2020-08-05 22:33:44 -07:00
|
|
|
static cl_error_t serial_callback(STATBUF *sb, char *filename, const char *path, enum cli_ftw_reason reason, struct cli_ftw_cbdata *data)
|
2018-12-03 12:40:13 -05:00
|
|
|
{
|
2020-08-05 22:33:44 -07:00
|
|
|
int status = CL_EOPEN;
|
|
|
|
|
2009-02-07 00:59:58 +00:00
|
|
|
struct client_serial_data *c = (struct client_serial_data *)data->data;
|
|
|
|
int sockd, ret;
|
2020-08-05 22:33:44 -07:00
|
|
|
const char *f = filename;
|
|
|
|
char *real_filename = NULL;
|
2009-02-07 00:59:58 +00:00
|
|
|
|
2014-07-11 09:30:58 -04:00
|
|
|
UNUSEDPARAM(sb);
|
|
|
|
|
2020-08-05 22:33:44 -07:00
|
|
|
if (reason != visit_directory_toplev) {
|
|
|
|
if (CL_SUCCESS != cli_realpath((const char *)path, &real_filename)) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_DEBUG, "Failed to determine real filename of %s.\n", path);
|
|
|
|
logg(LOGG_DEBUG, "Quarantine of the file may fail if file path contains symlinks.\n");
|
2020-08-05 22:33:44 -07:00
|
|
|
} else {
|
|
|
|
path = real_filename;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 09:14:45 -07:00
|
|
|
if (chkpath(path, clamdopts)) {
|
2021-06-29 17:27:07 -07:00
|
|
|
/* Exclude the path */
|
2020-08-05 22:33:44 -07:00
|
|
|
status = CL_SUCCESS;
|
|
|
|
goto done;
|
|
|
|
}
|
2010-02-03 18:23:30 +01:00
|
|
|
c->files++;
|
2018-12-03 12:40:13 -05:00
|
|
|
switch (reason) {
|
|
|
|
case error_stat:
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "Can't access file %s\n", path);
|
2018-12-03 12:40:13 -05:00
|
|
|
c->errors++;
|
2020-08-05 22:33:44 -07:00
|
|
|
status = CL_SUCCESS;
|
|
|
|
goto done;
|
2018-12-03 12:40:13 -05:00
|
|
|
case error_mem:
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "Memory allocation failed in ftw\n");
|
2018-12-03 12:40:13 -05:00
|
|
|
c->errors++;
|
2020-08-05 22:33:44 -07:00
|
|
|
status = CL_EMEM;
|
|
|
|
goto done;
|
2018-12-03 12:40:13 -05:00
|
|
|
case warning_skipped_dir:
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_WARNING, "Directory recursion limit reached\n");
|
2020-08-14 17:55:17 -07:00
|
|
|
/* fall-through */
|
2018-12-03 12:40:13 -05:00
|
|
|
case warning_skipped_link:
|
2020-08-05 22:33:44 -07:00
|
|
|
status = CL_SUCCESS;
|
|
|
|
goto done;
|
2018-12-03 12:40:13 -05:00
|
|
|
case warning_skipped_special:
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_WARNING, "%s: Not supported file type\n", path);
|
2018-12-03 12:40:13 -05:00
|
|
|
c->errors++;
|
2020-08-05 22:33:44 -07:00
|
|
|
status = CL_SUCCESS;
|
|
|
|
goto done;
|
2018-12-03 12:40:13 -05:00
|
|
|
case visit_directory_toplev:
|
2020-08-05 22:33:44 -07:00
|
|
|
if (c->scantype >= STREAM) {
|
|
|
|
status = CL_SUCCESS;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
f = path;
|
2018-12-03 12:40:13 -05:00
|
|
|
case visit_file:
|
|
|
|
break;
|
2009-02-07 00:59:58 +00:00
|
|
|
}
|
2009-02-07 12:36:22 +00:00
|
|
|
|
2021-08-27 09:14:45 -07:00
|
|
|
if ((sockd = dconnect(clamdopts)) < 0) {
|
2018-12-03 12:40:13 -05:00
|
|
|
c->errors++;
|
2020-08-05 22:33:44 -07:00
|
|
|
goto done;
|
2009-02-07 00:59:58 +00:00
|
|
|
}
|
2021-08-27 09:14:45 -07:00
|
|
|
ret = dsresult(sockd, c->scantype, f, &c->printok, &c->errors, clamdopts);
|
2009-09-24 16:21:51 +02:00
|
|
|
closesocket(sockd);
|
2018-12-03 12:40:13 -05:00
|
|
|
if (ret < 0) {
|
|
|
|
c->errors++;
|
2020-08-05 22:33:44 -07:00
|
|
|
goto done;
|
2010-02-03 18:23:30 +01:00
|
|
|
}
|
2009-03-07 01:55:07 +00:00
|
|
|
c->infected += ret;
|
2020-08-05 22:33:44 -07:00
|
|
|
if (reason == visit_directory_toplev) {
|
|
|
|
status = CL_BREAK;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = CL_SUCCESS;
|
|
|
|
done:
|
2021-06-29 17:27:07 -07:00
|
|
|
if (NULL != real_filename) {
|
|
|
|
free(real_filename);
|
|
|
|
}
|
2020-08-05 22:33:44 -07:00
|
|
|
free(filename);
|
|
|
|
return status;
|
2009-02-07 00:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Non-IDSESSION handler
|
2009-02-09 16:23:38 +00:00
|
|
|
* Returns non zero for serious errors, zero otherwise */
|
2018-12-03 12:40:13 -05:00
|
|
|
int serial_client_scan(char *file, int scantype, int *infected, int *err, int maxlevel, int flags)
|
|
|
|
{
|
2009-02-07 00:59:58 +00:00
|
|
|
struct cli_ftw_cbdata data;
|
|
|
|
struct client_serial_data cdata;
|
2009-02-09 16:23:38 +00:00
|
|
|
int ftw;
|
2009-02-07 00:59:58 +00:00
|
|
|
|
|
|
|
cdata.infected = 0;
|
2018-12-03 12:40:13 -05:00
|
|
|
cdata.files = 0;
|
|
|
|
cdata.errors = 0;
|
|
|
|
cdata.printok = printinfected ^ 1;
|
2009-02-07 00:59:58 +00:00
|
|
|
cdata.scantype = scantype;
|
2018-12-03 12:40:13 -05:00
|
|
|
data.data = &cdata;
|
2009-02-07 00:59:58 +00:00
|
|
|
|
2011-03-09 15:43:21 +01:00
|
|
|
ftw = cli_ftw(file, flags, maxlevel ? maxlevel : INT_MAX, serial_callback, &data, ftw_chkpath);
|
2009-02-07 00:59:58 +00:00
|
|
|
*infected += cdata.infected;
|
2010-02-03 18:23:30 +01:00
|
|
|
*err += cdata.errors;
|
2009-02-09 16:23:38 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!cdata.errors && (ftw == CL_SUCCESS || ftw == CL_BREAK)) {
|
|
|
|
if (cdata.printok)
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_INFO, "%s: OK\n", file);
|
2018-12-03 12:40:13 -05:00
|
|
|
return 0;
|
|
|
|
} else if (!cdata.files) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_INFO, "%s: No files scanned\n", file);
|
2018-12-03 12:40:13 -05:00
|
|
|
return 0;
|
2009-02-09 16:23:38 +00:00
|
|
|
}
|
|
|
|
return 1;
|
2009-02-07 00:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Used in IDSESSION mode */
|
|
|
|
struct client_parallel_data {
|
|
|
|
int infected;
|
2009-10-16 14:57:15 +02:00
|
|
|
int files;
|
|
|
|
int errors;
|
2009-02-07 00:59:58 +00:00
|
|
|
int scantype;
|
|
|
|
int sockd;
|
|
|
|
int lastid;
|
2009-03-07 15:32:16 +00:00
|
|
|
int printok;
|
2009-02-07 00:59:58 +00:00
|
|
|
struct SCANID {
|
2018-12-03 12:40:13 -05:00
|
|
|
unsigned int id;
|
|
|
|
const char *file;
|
|
|
|
struct SCANID *next;
|
2024-01-09 19:41:17 -05:00
|
|
|
} *ids;
|
2009-02-07 00:59:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Sends a proper scan request to clamd and parses its replies
|
|
|
|
* This is used only in IDSESSION mode
|
2009-10-26 13:09:19 +01:00
|
|
|
* Returns 0 on success, 1 on hard failures, 2 on len == 0 (bb#1717) */
|
2018-12-03 12:40:13 -05:00
|
|
|
static int dspresult(struct client_parallel_data *c)
|
|
|
|
{
|
2009-02-07 00:59:58 +00:00
|
|
|
const char *filename;
|
|
|
|
char *bol, *eol;
|
|
|
|
unsigned int rid;
|
|
|
|
int len;
|
|
|
|
struct SCANID **id = NULL;
|
|
|
|
struct RCVLN rcv;
|
|
|
|
|
|
|
|
recvlninit(&rcv, c->sockd);
|
|
|
|
do {
|
2018-12-03 12:40:13 -05:00
|
|
|
len = recvln(&rcv, &bol, &eol);
|
|
|
|
if (len < 0) return 1;
|
|
|
|
if (!len) return 2;
|
|
|
|
if ((rid = atoi(bol))) {
|
|
|
|
id = &c->ids;
|
|
|
|
while (*id) {
|
|
|
|
if ((*id)->id == rid) break;
|
|
|
|
id = &((*id)->next);
|
|
|
|
}
|
|
|
|
if (!*id) id = NULL;
|
|
|
|
}
|
|
|
|
if (!id) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "Bogus session id from clamd\n");
|
2018-12-03 12:40:13 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
filename = (*id)->file;
|
|
|
|
if (len > 7) {
|
|
|
|
char *colon = strrchr(bol, ':');
|
|
|
|
if (!colon) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "Failed to parse reply\n");
|
2018-12-03 12:40:13 -05:00
|
|
|
free((void *)filename);
|
|
|
|
return 1;
|
|
|
|
} else if (!memcmp(eol - 7, " FOUND", 6)) {
|
|
|
|
c->infected++;
|
|
|
|
c->printok = 0;
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_INFO, "%s%s\n", filename, colon);
|
2018-12-03 12:40:13 -05:00
|
|
|
if (action) action(filename);
|
|
|
|
} else if (!memcmp(eol - 7, " ERROR", 6)) {
|
|
|
|
c->errors++;
|
|
|
|
c->printok = 0;
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_INFO, "%s%s\n", filename, colon);
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free((void *)filename);
|
|
|
|
bol = (char *)*id;
|
|
|
|
*id = (*id)->next;
|
|
|
|
free(bol);
|
|
|
|
} while (rcv.cur != rcv.buf); /* clamd sends whole lines, so, on partial lines, we just assume
|
2022-02-16 00:13:55 +01:00
|
|
|
more data can be recv()'d with close to zero latency */
|
2009-02-07 00:59:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-09 16:23:38 +00:00
|
|
|
/* FTW callback for scanning in IDSESSION mode
|
2009-03-07 01:55:07 +00:00
|
|
|
* Returns SUCCESS on success, CL_EXXX or BREAK on error */
|
2020-08-05 22:33:44 -07:00
|
|
|
static cl_error_t parallel_callback(STATBUF *sb, char *filename, const char *path, enum cli_ftw_reason reason, struct cli_ftw_cbdata *data)
|
2018-12-03 12:40:13 -05:00
|
|
|
{
|
2020-08-05 22:33:44 -07:00
|
|
|
cl_error_t status = CL_EOPEN;
|
|
|
|
|
2009-02-07 00:59:58 +00:00
|
|
|
struct client_parallel_data *c = (struct client_parallel_data *)data->data;
|
2020-08-05 22:33:44 -07:00
|
|
|
struct SCANID *cid = NULL;
|
|
|
|
int res = CL_CLEAN;
|
|
|
|
|
|
|
|
char *real_filename = NULL;
|
2009-02-07 00:59:58 +00:00
|
|
|
|
2014-07-11 09:30:58 -04:00
|
|
|
UNUSEDPARAM(sb);
|
2020-08-05 22:33:44 -07:00
|
|
|
UNUSEDPARAM(path);
|
|
|
|
|
|
|
|
if (reason != visit_directory_toplev) {
|
|
|
|
if (CL_SUCCESS != cli_realpath((const char *)filename, &real_filename)) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_DEBUG, "Failed to determine real filename of %s.\n", filename);
|
|
|
|
logg(LOGG_DEBUG, "Quarantine of the file may fail if file path contains symlinks.\n");
|
2020-08-05 22:33:44 -07:00
|
|
|
} else {
|
2021-06-29 17:27:07 -07:00
|
|
|
free(filename); /* callback is responsible for free'ing filename parameter. */
|
2020-08-05 22:33:44 -07:00
|
|
|
filename = real_filename;
|
|
|
|
}
|
|
|
|
}
|
2014-07-11 09:30:58 -04:00
|
|
|
|
2021-08-27 09:14:45 -07:00
|
|
|
if (chkpath(filename, clamdopts)) {
|
2021-06-29 17:27:07 -07:00
|
|
|
/* Exclude the path */
|
|
|
|
status = CL_SUCCESS;
|
2020-08-05 22:33:44 -07:00
|
|
|
goto done;
|
|
|
|
}
|
2010-02-03 18:23:30 +01:00
|
|
|
c->files++;
|
2018-12-03 12:40:13 -05:00
|
|
|
switch (reason) {
|
|
|
|
case error_stat:
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "Can't access file %s\n", filename);
|
2018-12-03 12:40:13 -05:00
|
|
|
c->errors++;
|
2020-08-05 22:33:44 -07:00
|
|
|
status = CL_SUCCESS;
|
|
|
|
goto done;
|
2018-12-03 12:40:13 -05:00
|
|
|
case error_mem:
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "Memory allocation failed in ftw\n");
|
2018-12-03 12:40:13 -05:00
|
|
|
c->errors++;
|
2020-08-05 22:33:44 -07:00
|
|
|
status = CL_EMEM;
|
|
|
|
goto done;
|
2018-12-03 12:40:13 -05:00
|
|
|
case warning_skipped_dir:
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_WARNING, "Directory recursion limit reached\n");
|
2020-08-05 22:33:44 -07:00
|
|
|
status = CL_SUCCESS;
|
|
|
|
goto done;
|
2018-12-03 12:40:13 -05:00
|
|
|
case warning_skipped_special:
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_WARNING, "%s: Not supported file type\n", filename);
|
2018-12-03 12:40:13 -05:00
|
|
|
c->errors++;
|
2020-08-14 17:55:17 -07:00
|
|
|
/* fall-through */
|
2018-12-03 12:40:13 -05:00
|
|
|
case warning_skipped_link:
|
|
|
|
case visit_directory_toplev:
|
2020-08-05 22:33:44 -07:00
|
|
|
status = CL_SUCCESS;
|
|
|
|
goto done;
|
2018-12-03 12:40:13 -05:00
|
|
|
case visit_file:
|
|
|
|
break;
|
2009-02-07 00:59:58 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
while (1) {
|
|
|
|
/* consume all the available input to let some of the clamd
|
2022-02-16 00:13:55 +01:00
|
|
|
* threads blocked on send() to be dead.
|
|
|
|
* by doing so we shouldn't deadlock on the next recv() */
|
2018-12-03 12:40:13 -05:00
|
|
|
fd_set rfds, wfds;
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(c->sockd, &rfds);
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
FD_SET(c->sockd, &wfds);
|
|
|
|
if (select(c->sockd + 1, &rfds, &wfds, NULL, NULL) < 0) {
|
|
|
|
if (errno == EINTR) continue;
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "select() failed during session: %s\n", strerror(errno));
|
2020-08-05 22:33:44 -07:00
|
|
|
status = CL_BREAK;
|
|
|
|
goto done;
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
|
|
|
if (FD_ISSET(c->sockd, &rfds)) {
|
|
|
|
if (dspresult(c)) {
|
2020-08-05 22:33:44 -07:00
|
|
|
status = CL_BREAK;
|
|
|
|
goto done;
|
2018-12-03 12:40:13 -05:00
|
|
|
} else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (FD_ISSET(c->sockd, &wfds)) break;
|
2009-02-07 00:59:58 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
switch (c->scantype) {
|
2009-02-11 12:17:20 +00:00
|
|
|
#ifdef HAVE_FD_PASSING
|
2018-12-03 12:40:13 -05:00
|
|
|
case FILDES:
|
|
|
|
res = send_fdpass(c->sockd, filename);
|
|
|
|
break;
|
2009-02-11 12:17:20 +00:00
|
|
|
#endif
|
2018-12-03 12:40:13 -05:00
|
|
|
case STREAM:
|
2021-08-27 09:14:45 -07:00
|
|
|
res = send_stream(c->sockd, filename, clamdopts);
|
2018-12-03 12:40:13 -05:00
|
|
|
break;
|
2009-02-07 00:59:58 +00:00
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
if (res <= 0) {
|
|
|
|
c->printok = 0;
|
|
|
|
c->errors++;
|
2020-08-05 22:33:44 -07:00
|
|
|
status = res ? CL_BREAK : CL_SUCCESS;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
cid = (struct SCANID *)malloc(sizeof(struct SCANID));
|
|
|
|
if (!cid) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "Failed to allocate scanid entry: %s\n", strerror(errno));
|
2020-08-05 22:33:44 -07:00
|
|
|
status = CL_BREAK;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
cid->id = ++c->lastid;
|
|
|
|
cid->file = filename;
|
|
|
|
cid->next = c->ids;
|
|
|
|
c->ids = cid;
|
|
|
|
|
2023-11-26 15:01:19 -08:00
|
|
|
/* Give up ownership of the filename to the client parallel scan ID list */
|
2020-08-05 22:33:44 -07:00
|
|
|
filename = NULL;
|
|
|
|
|
|
|
|
status = CL_SUCCESS;
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (NULL != filename) {
|
2018-12-03 12:40:13 -05:00
|
|
|
free(filename);
|
2009-03-07 15:32:09 +00:00
|
|
|
}
|
2020-08-05 22:33:44 -07:00
|
|
|
return status;
|
2009-02-07 00:59:58 +00:00
|
|
|
}
|
|
|
|
|
2009-02-11 12:17:20 +00:00
|
|
|
/* IDSESSION handler
|
2009-02-09 16:23:38 +00:00
|
|
|
* Returns non zero for serious errors, zero otherwise */
|
2018-12-03 12:40:13 -05:00
|
|
|
int parallel_client_scan(char *file, int scantype, int *infected, int *err, int maxlevel, int flags)
|
|
|
|
{
|
2009-02-07 00:59:58 +00:00
|
|
|
struct cli_ftw_cbdata data;
|
|
|
|
struct client_parallel_data cdata;
|
2009-02-09 16:23:38 +00:00
|
|
|
int ftw;
|
2021-05-28 16:35:35 +02:00
|
|
|
const char zIDSESSION[] = "zIDSESSION";
|
2021-06-18 12:56:38 -07:00
|
|
|
const char zEND[] = "zEND";
|
2009-02-07 00:59:58 +00:00
|
|
|
|
2021-08-27 09:14:45 -07:00
|
|
|
if ((cdata.sockd = dconnect(clamdopts)) < 0)
|
2018-12-03 12:40:13 -05:00
|
|
|
return 1;
|
2009-02-07 00:59:58 +00:00
|
|
|
|
2021-05-28 16:35:35 +02:00
|
|
|
if (sendln(cdata.sockd, zIDSESSION, sizeof(zIDSESSION))) {
|
2018-12-03 12:40:13 -05:00
|
|
|
closesocket(cdata.sockd);
|
|
|
|
return 1;
|
2009-02-07 00:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cdata.infected = 0;
|
2018-12-03 12:40:13 -05:00
|
|
|
cdata.files = 0;
|
|
|
|
cdata.errors = 0;
|
2009-02-07 00:59:58 +00:00
|
|
|
cdata.scantype = scantype;
|
2018-12-03 12:40:13 -05:00
|
|
|
cdata.lastid = 0;
|
|
|
|
cdata.ids = NULL;
|
|
|
|
cdata.printok = printinfected ^ 1;
|
|
|
|
data.data = &cdata;
|
2009-02-07 00:59:58 +00:00
|
|
|
|
2011-03-09 15:43:21 +01:00
|
|
|
ftw = cli_ftw(file, flags, maxlevel ? maxlevel : INT_MAX, parallel_callback, &data, ftw_chkpath);
|
2009-02-09 16:23:38 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (ftw != CL_SUCCESS) {
|
|
|
|
*err += cdata.errors;
|
|
|
|
*infected += cdata.infected;
|
|
|
|
closesocket(cdata.sockd);
|
|
|
|
return 1;
|
2009-02-09 16:23:38 +00:00
|
|
|
}
|
2009-02-07 00:59:58 +00:00
|
|
|
|
2021-05-28 16:35:35 +02:00
|
|
|
sendln(cdata.sockd, zEND, sizeof(zEND));
|
2018-12-03 12:37:58 -05:00
|
|
|
while (cdata.ids && !dspresult(&cdata)) continue;
|
2009-09-24 16:21:51 +02:00
|
|
|
closesocket(cdata.sockd);
|
2009-02-07 17:53:33 +00:00
|
|
|
|
2009-02-10 14:24:06 +00:00
|
|
|
*infected += cdata.infected;
|
2010-02-03 18:23:30 +01:00
|
|
|
*err += cdata.errors;
|
2009-02-10 14:24:06 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (cdata.ids) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "Clamd closed the connection before scanning all files.\n");
|
2018-12-03 12:40:13 -05:00
|
|
|
return 1;
|
2009-02-08 15:45:38 +00:00
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
if (cdata.errors)
|
|
|
|
return 1;
|
2010-02-03 18:23:30 +01:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!cdata.files)
|
|
|
|
return 0;
|
2010-02-03 18:23:30 +01:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (cdata.printok)
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_INFO, "%s: OK\n", file);
|
2009-02-09 16:23:38 +00:00
|
|
|
return 0;
|
2009-02-07 00:59:58 +00:00
|
|
|
}
|