2018-12-03 13:17:07 -05:00
|
|
|
/*
|
2023-02-07 19:35:18 -08:00
|
|
|
* Copyright (C) 2015-2023 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
|
2018-12-03 13:17:07 -05:00
|
|
|
* Copyright (C) 2009 Sourcefire, Inc.
|
|
|
|
*
|
|
|
|
* Authors: Tomasz Kojm, aCaB, Mickey Sola
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
#include "clamav-config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2019-04-25 16:11:39 -04:00
|
|
|
#include <curl/curl.h>
|
2019-07-25 12:42:08 -04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2018-12-03 13:17:07 -05:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#ifdef HAVE_SYS_LIMITS_H
|
|
|
|
#include <sys/limits.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <utime.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_UIO_H
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#endif
|
|
|
|
|
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 "str.h"
|
|
|
|
#include "others.h"
|
2019-07-25 12:21:07 -04:00
|
|
|
|
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 "optparser.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "actions.h"
|
2021-08-27 09:14:45 -07:00
|
|
|
#include "clamdcom.h"
|
2019-07-25 12:21:07 -04:00
|
|
|
|
|
|
|
#include "communication.h"
|
|
|
|
#include "client.h"
|
|
|
|
#include "protocol.h"
|
2020-12-02 18:47:03 -08:00
|
|
|
#include "socket.h"
|
2018-12-03 13:17:07 -05:00
|
|
|
|
2019-02-28 13:13:14 -05:00
|
|
|
#include "../clamonacc.h"
|
|
|
|
|
2019-06-10 17:39:41 -04:00
|
|
|
void onas_print_server_version(struct onas_context **ctx)
|
2019-02-28 13:13:14 -05:00
|
|
|
{
|
2019-07-25 12:42:08 -04:00
|
|
|
if (onas_get_clamd_version(ctx)) {
|
|
|
|
/* can't get version from server, fallback */
|
|
|
|
printf("ClamAV %s\n", get_version());
|
|
|
|
}
|
2019-02-28 13:13:14 -05:00
|
|
|
}
|
|
|
|
|
2018-12-03 13:17:07 -05:00
|
|
|
/* Inits the communication layer
|
|
|
|
* Returns 0 if clamd is local, non zero if clamd is remote */
|
2019-07-25 12:42:08 -04:00
|
|
|
int onas_check_remote(struct onas_context **ctx, cl_error_t *err)
|
|
|
|
{
|
2020-01-28 14:05:44 -05:00
|
|
|
int ret;
|
2019-07-25 12:42:08 -04:00
|
|
|
const struct optstruct *opt;
|
|
|
|
CURL *curl;
|
|
|
|
CURLcode curlcode;
|
|
|
|
char *ipaddr = NULL;
|
2019-05-07 18:00:47 -04:00
|
|
|
int64_t timeout;
|
2021-05-28 16:48:10 +02:00
|
|
|
const char zPING[] = "zPING";
|
2019-05-07 18:00:47 -04:00
|
|
|
|
|
|
|
timeout = optget((*ctx)->clamdopts, "OnAccessCurlTimeout")->numarg;
|
|
|
|
|
2019-07-25 12:42:08 -04:00
|
|
|
*err = CL_SUCCESS;
|
2018-12-03 13:17:07 -05:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
2019-07-25 12:42:08 -04:00
|
|
|
if ((opt = optget((*ctx)->clamdopts, "LocalSocket"))->enabled) {
|
|
|
|
opt = optget((*ctx)->clamdopts, "LocalSocket");
|
|
|
|
(*ctx)->portnum = 0;
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
opt = optget((*ctx)->clamdopts, "TCPAddr");
|
|
|
|
(*ctx)->portnum = optget((*ctx)->clamdopts, "TCPSocket")->numarg;
|
|
|
|
ret = 1;
|
|
|
|
}
|
2019-07-22 14:51:13 -04:00
|
|
|
#else
|
2019-07-25 12:42:08 -04:00
|
|
|
if (!(opt = optget((*ctx)->clamdopts, "TCPSocket"))->enabled) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-07-22 14:51:13 -04:00
|
|
|
#endif
|
2018-12-03 13:17:07 -05:00
|
|
|
|
2019-07-25 12:42:08 -04:00
|
|
|
while (opt) {
|
2019-04-25 16:11:39 -04:00
|
|
|
|
2019-07-25 12:42:08 -04:00
|
|
|
if (opt->strarg) {
|
|
|
|
ipaddr = (!strcmp(opt->strarg, "any") ? NULL : opt->strarg);
|
|
|
|
}
|
2019-04-25 16:11:39 -04:00
|
|
|
|
2019-07-25 12:42:08 -04:00
|
|
|
if (NULL == ipaddr) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: Clamonacc does not support binding to INADDR_ANY, \
|
2019-04-25 16:11:39 -04:00
|
|
|
please specify an address with TCPAddr in your clamd.conf config file\n");
|
2019-07-25 12:42:08 -04:00
|
|
|
*err = CL_EARG;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
curlcode = onas_curl_init(&curl, ipaddr, (*ctx)->portnum, timeout);
|
|
|
|
if (CURLE_OK != curlcode) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: could not init curl, %s\n", curl_easy_strerror(curlcode));
|
2019-07-25 12:42:08 -04:00
|
|
|
*err = CL_EARG;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
curlcode = curl_easy_perform(curl);
|
|
|
|
if (CURLE_OK != curlcode) {
|
2021-01-03 18:40:48 -08:00
|
|
|
if (optget((*ctx)->opts, "ping")->enabled || optget((*ctx)->opts, "wait")->enabled) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_INFO, "ClamClient: Initial connection failed, %s. Will try again...\n", curl_easy_strerror(curlcode));
|
2021-01-03 18:40:48 -08:00
|
|
|
} else {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: Could not connect to clamd, %s\n", curl_easy_strerror(curlcode));
|
2021-01-03 18:40:48 -08:00
|
|
|
}
|
2021-07-09 14:35:05 -04:00
|
|
|
curl_easy_cleanup(curl);
|
2019-07-25 12:42:08 -04:00
|
|
|
*err = CL_EARG;
|
|
|
|
return ret;
|
|
|
|
}
|
2019-04-25 16:11:39 -04:00
|
|
|
|
2019-05-07 18:00:47 -04:00
|
|
|
#ifndef ONAS_DEBUG
|
2021-05-28 16:48:10 +02:00
|
|
|
if (onas_sendln(curl, zPING, sizeof(zPING), timeout)) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: could not ping clamd, %s\n", curl_easy_strerror(curlcode));
|
2019-07-25 12:42:08 -04:00
|
|
|
*err = CL_EARG;
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
return ret;
|
|
|
|
}
|
2019-05-07 18:00:47 -04:00
|
|
|
#endif
|
|
|
|
|
2019-07-25 12:42:08 -04:00
|
|
|
curl_easy_cleanup(curl);
|
2019-04-25 16:11:39 -04:00
|
|
|
|
2019-07-25 12:42:08 -04:00
|
|
|
opt = opt->nextarg;
|
|
|
|
}
|
2018-12-03 13:17:07 -05:00
|
|
|
|
2019-07-25 12:42:08 -04:00
|
|
|
return ret;
|
2019-04-25 16:11:39 -04:00
|
|
|
}
|
2018-12-03 13:17:07 -05:00
|
|
|
|
2019-12-20 17:04:23 -05:00
|
|
|
/* pings clamd at the specified interval the number of time specified
|
|
|
|
* return 0 on a succesful connection, 1 upon timeout, -1 on error */
|
|
|
|
int16_t onas_ping_clamd(struct onas_context **ctx)
|
|
|
|
{
|
|
|
|
|
|
|
|
uint64_t attempts = 0;
|
|
|
|
uint64_t interval = 0;
|
|
|
|
char *attempt_str = NULL;
|
|
|
|
char *interval_str = NULL;
|
|
|
|
char *errchk = NULL;
|
|
|
|
uint64_t i = 0;
|
|
|
|
const struct optstruct *opt = NULL;
|
2020-07-24 08:32:47 -07:00
|
|
|
CURL *curl = NULL;
|
2019-12-20 17:04:23 -05:00
|
|
|
CURLcode curlcode;
|
|
|
|
cl_error_t err = CL_SUCCESS;
|
2020-07-24 08:32:47 -07:00
|
|
|
int b_remote = 0;
|
|
|
|
uint16_t ret = 0;
|
2019-12-20 17:04:23 -05:00
|
|
|
int64_t timeout;
|
2021-05-28 16:48:10 +02:00
|
|
|
const char zPING[] = "zPING";
|
2019-12-20 17:04:23 -05:00
|
|
|
|
|
|
|
if (ctx == NULL) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "null parameter was passed\n");
|
2019-12-20 17:04:23 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout = optget((*ctx)->clamdopts, "OnAccessCurlTimeout")->numarg;
|
|
|
|
|
|
|
|
b_remote = onas_check_remote(ctx, &err);
|
|
|
|
if (CL_SUCCESS != err) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_DEBUG, "ClamClient: could not check to see if daemon was remote... PINGing again...\n");
|
2019-12-20 17:04:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!b_remote) {
|
|
|
|
curlcode = onas_curl_init(&curl, optget((*ctx)->clamdopts, "LocalSocket")->strarg, 0, timeout);
|
|
|
|
} else {
|
|
|
|
curlcode = onas_curl_init(&curl, optget((*ctx)->clamdopts, "TCPAddr")->strarg, (*ctx)->portnum, timeout);
|
|
|
|
if (CURLE_OK != curlcode) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: could not setup curl with tcp address and port, %s\n", curl_easy_strerror(curlcode));
|
2019-12-20 17:04:23 -05:00
|
|
|
/* curl cleanup done in onas_curl_init on error */
|
|
|
|
ret = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ping command takes the form --ping [attempts[:interval]] */
|
2020-01-28 14:05:44 -05:00
|
|
|
opt = optget((*ctx)->opts, "ping");
|
|
|
|
|
|
|
|
if (opt) {
|
|
|
|
attempt_str = cli_strdup(opt->strarg);
|
|
|
|
if (attempt_str) {
|
2019-12-20 17:04:23 -05:00
|
|
|
if (NULL == attempt_str) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "could not allocate memory for string\n");
|
2019-12-20 17:04:23 -05:00
|
|
|
ret = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
interval_str = strchr(attempt_str, ':');
|
2020-08-13 14:33:22 -07:00
|
|
|
if ((NULL != interval_str) && (interval_str[0] != '\0')) {
|
2019-12-20 17:04:23 -05:00
|
|
|
interval_str[0] = '\0';
|
|
|
|
interval_str++;
|
|
|
|
interval = cli_strntoul(interval_str, strlen(interval_str), &errchk, 10);
|
|
|
|
if (interval_str + strlen(interval_str) > errchk) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_WARNING, "interval_str would go past end of buffer\n");
|
2019-12-20 17:04:23 -05:00
|
|
|
ret = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
interval = ONAS_DEFAULT_PING_INTERVAL;
|
|
|
|
}
|
|
|
|
attempts = cli_strntoul(attempt_str, strlen(attempt_str), &errchk, 10);
|
|
|
|
if (attempt_str + strlen(attempt_str) > errchk) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_WARNING, "attempt_str would go past end of buffer\n");
|
2019-12-20 17:04:23 -05:00
|
|
|
ret = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
attempts = ONAS_DEFAULT_PING_ATTEMPTS;
|
|
|
|
interval = ONAS_DEFAULT_PING_INTERVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
curlcode = curl_easy_perform(curl);
|
|
|
|
if (CURLE_OK != curlcode) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_DEBUG, "ClamClient: could not connect to clamd, %s\n", curl_easy_strerror(curlcode));
|
2021-05-28 16:48:10 +02:00
|
|
|
} else if (CURLE_OK == onas_sendln(curl, zPING, sizeof(zPING), timeout)) {
|
2019-12-20 17:04:23 -05:00
|
|
|
|
|
|
|
if (!optget((*ctx)->opts, "wait")->enabled) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_INFO, "PONG\n");
|
2021-01-03 18:40:48 -08:00
|
|
|
} else {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_DEBUG, "ClamClient: Connected.\n");
|
2019-12-20 17:04:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i + 1 < attempts) {
|
2021-01-03 18:40:48 -08:00
|
|
|
if (optget((*ctx)->opts, "wait")->enabled) {
|
|
|
|
if (interval == 1)
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_DEBUG, "Will try again in %lu second\n", interval);
|
2021-01-03 18:40:48 -08:00
|
|
|
else
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_DEBUG, "Will try again in %lu seconds\n", interval);
|
2021-01-03 18:40:48 -08:00
|
|
|
} else {
|
|
|
|
if (interval == 1)
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_INFO, "PINGing again in %lu second\n", interval);
|
2021-01-03 18:40:48 -08:00
|
|
|
else
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_INFO, "PINGing again in %lu seconds\n", interval);
|
2021-01-03 18:40:48 -08:00
|
|
|
}
|
2019-12-20 17:04:23 -05:00
|
|
|
sleep(interval);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
} while (i < attempts);
|
|
|
|
|
|
|
|
/* timed out */
|
|
|
|
ret = 1;
|
2021-01-03 18:40:48 -08:00
|
|
|
if (optget((*ctx)->opts, "wait")->enabled) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_INFO, "Wait timeout exceeded; Could not connect to clamd\n");
|
2021-01-03 18:40:48 -08:00
|
|
|
} else {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_INFO, "PING timeout exceeded; No response from clamd\n");
|
2021-01-03 18:40:48 -08:00
|
|
|
}
|
2020-08-14 17:55:17 -07:00
|
|
|
|
2019-12-20 17:04:23 -05:00
|
|
|
done:
|
|
|
|
if (curl) {
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attempt_str) {
|
|
|
|
free(attempt_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
attempt_str = NULL;
|
|
|
|
interval_str = NULL;
|
|
|
|
errchk = NULL;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-24 12:05:36 -04:00
|
|
|
/**
|
|
|
|
* @brief initialises a curl connection for the onaccess client; curl must be initialised globally before use
|
|
|
|
*
|
|
|
|
* @param curl pointer to the curl object to be used in the connection attempt
|
|
|
|
* @param ipaddr string which refers to either the TCPaddress or the local socket to connect to
|
|
|
|
* @param port the port to use in case of TCP connection, set to 0 if connecting to a local socket
|
|
|
|
* @param timeout time in ms to allow curl before timing out connection attempts
|
|
|
|
*/
|
2019-07-25 12:42:08 -04:00
|
|
|
CURLcode onas_curl_init(CURL **curl, const char *ipaddr, int64_t port, int64_t timeout)
|
|
|
|
{
|
|
|
|
|
|
|
|
CURLcode curlcode = CURLE_OK;
|
|
|
|
|
|
|
|
if (!curl || !ipaddr) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: invalid (NULL) args passed to onas_curl_init\n");
|
2019-07-25 12:42:08 -04:00
|
|
|
return CURLE_FAILED_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup here, but caller needs to cleanup */
|
|
|
|
*curl = curl_easy_init();
|
|
|
|
|
|
|
|
if (!port) {
|
|
|
|
|
2020-07-27 10:23:52 -07:00
|
|
|
#if ((LIBCURL_VERSION_MAJOR > 7) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 40))
|
2019-07-25 12:42:08 -04:00
|
|
|
/* "ipaddr" is actually our unix socket path here */
|
|
|
|
curlcode = curl_easy_setopt(*curl, CURLOPT_UNIX_SOCKET_PATH, ipaddr);
|
2020-07-27 10:23:52 -07:00
|
|
|
#endif
|
2019-07-25 12:42:08 -04:00
|
|
|
if (CURLE_OK != curlcode) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: could not setup curl with local unix socket, %s\n", curl_easy_strerror(curlcode));
|
2019-07-25 12:42:08 -04:00
|
|
|
curl_easy_cleanup(*curl);
|
|
|
|
return curlcode;
|
2019-04-25 16:11:39 -04:00
|
|
|
}
|
2018-12-03 13:17:07 -05:00
|
|
|
|
2019-07-25 12:42:08 -04:00
|
|
|
curlcode = curl_easy_setopt(*curl, CURLOPT_URL, "http://localhost/");
|
|
|
|
if (CURLE_OK != curlcode) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: could not setup curl with local address, %s\n", curl_easy_strerror(curlcode));
|
2019-07-25 12:42:08 -04:00
|
|
|
curl_easy_cleanup(*curl);
|
|
|
|
return curlcode;
|
|
|
|
}
|
2019-05-07 18:00:47 -04:00
|
|
|
|
2019-07-25 12:42:08 -04:00
|
|
|
} else {
|
|
|
|
|
|
|
|
curlcode = curl_easy_setopt(*curl, CURLOPT_PORT, port);
|
|
|
|
if (CURLE_OK != curlcode) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: could not setup curl with tcp port, %s\n", curl_easy_strerror(curlcode));
|
2019-07-25 12:42:08 -04:00
|
|
|
curl_easy_cleanup(*curl);
|
|
|
|
return curlcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
curlcode = curl_easy_setopt(*curl, CURLOPT_URL, ipaddr);
|
|
|
|
if (CURLE_OK != curlcode) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: could not setup curl with tcp address, %s\n", curl_easy_strerror(curlcode));
|
2019-07-25 12:42:08 -04:00
|
|
|
curl_easy_cleanup(*curl);
|
|
|
|
return curlcode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
curlcode = curl_easy_setopt(*curl, CURLOPT_NOSIGNAL, 1L);
|
|
|
|
if (CURLE_OK != curlcode) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: could not setup curl to not use signals, %s\n", curl_easy_strerror(curlcode));
|
2019-07-25 12:42:08 -04:00
|
|
|
curl_easy_cleanup(*curl);
|
|
|
|
return curlcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
curlcode = curl_easy_setopt(*curl, CURLOPT_CONNECTTIMEOUT_MS, (long)timeout);
|
|
|
|
if (CURLE_OK != curlcode) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: could not setup curl with connect timeout, %s\n", curl_easy_strerror(curlcode));
|
2019-07-25 12:42:08 -04:00
|
|
|
curl_easy_cleanup(*curl);
|
|
|
|
return curlcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we implement our own transfer protocol via send and recv, so we only need to connect */
|
|
|
|
curlcode = curl_easy_setopt(*curl, CURLOPT_CONNECT_ONLY, 1L);
|
|
|
|
if (CURLE_OK != curlcode) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: could not setup curl to connect only, %s\n", curl_easy_strerror(curlcode));
|
2019-07-25 12:42:08 -04:00
|
|
|
curl_easy_cleanup(*curl);
|
|
|
|
return curlcode;
|
|
|
|
}
|
2019-05-07 18:00:47 -04:00
|
|
|
|
|
|
|
#ifdef ONAS_DEBUG
|
2019-07-25 12:42:08 -04:00
|
|
|
curlcode = curl_easy_setopt(*curl, CURLOPT_VERBOSE, 1L);
|
|
|
|
if (CURLE_OK != curlcode) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: could not tell curl to be verbose, %s\n", curl_easy_strerror(curlcode));
|
2019-07-25 12:42:08 -04:00
|
|
|
curl_easy_cleanup(*curl);
|
|
|
|
return curlcode;
|
|
|
|
}
|
2019-05-07 18:00:47 -04:00
|
|
|
#endif
|
|
|
|
|
2019-07-25 12:42:08 -04:00
|
|
|
/* don't care about the body of the return message */
|
|
|
|
curlcode = curl_easy_setopt(*curl, CURLOPT_NOBODY, 1L);
|
|
|
|
if (CURLE_OK != curlcode) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: could not setup curl to send HEAD request, %s\n", curl_easy_strerror(curlcode));
|
2019-07-25 12:42:08 -04:00
|
|
|
curl_easy_cleanup(*curl);
|
|
|
|
return curlcode;
|
|
|
|
}
|
2019-05-07 18:00:47 -04:00
|
|
|
|
2019-07-25 12:42:08 -04:00
|
|
|
curlcode = curl_easy_setopt(*curl, CURLOPT_HEADER, 0L);
|
|
|
|
if (CURLE_OK != curlcode) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: could not setup curl to not send header, %s\n", curl_easy_strerror(curlcode));
|
2019-07-25 12:42:08 -04:00
|
|
|
curl_easy_cleanup(*curl);
|
|
|
|
return curlcode;
|
|
|
|
}
|
|
|
|
return curlcode;
|
2018-12-03 13:17:07 -05:00
|
|
|
}
|
|
|
|
|
2019-07-25 12:42:08 -04:00
|
|
|
cl_error_t onas_setup_client(struct onas_context **ctx)
|
|
|
|
{
|
2019-02-28 13:13:14 -05:00
|
|
|
|
|
|
|
const struct optstruct *opts;
|
2019-04-25 16:11:39 -04:00
|
|
|
cl_error_t err;
|
2019-02-28 13:13:14 -05:00
|
|
|
int remote;
|
|
|
|
|
2019-04-25 16:11:39 -04:00
|
|
|
errno = 0;
|
|
|
|
|
2019-02-28 13:13:14 -05:00
|
|
|
opts = (*ctx)->opts;
|
|
|
|
|
2019-07-25 12:42:08 -04:00
|
|
|
if (optget(opts, "infected")->enabled) {
|
|
|
|
(*ctx)->printinfected = 1;
|
2019-02-28 13:13:14 -05:00
|
|
|
}
|
|
|
|
|
2019-07-25 12:42:08 -04:00
|
|
|
if (actsetup(opts)) {
|
|
|
|
return CL_EARG;
|
2019-02-28 13:13:14 -05:00
|
|
|
}
|
|
|
|
|
2019-07-25 12:42:08 -04:00
|
|
|
(*ctx)->timeout = optget((*ctx)->clamdopts, "OnAccessCurlTimeout")->numarg;
|
2019-06-03 16:56:35 -04:00
|
|
|
(*ctx)->retry_attempts = optget((*ctx)->clamdopts, "OnAccessRetryAttempts")->numarg;
|
|
|
|
(*ctx)->retry_attempts ? ((*ctx)->retry_on_error = 1) : ((*ctx)->retry_on_error = 0);
|
2019-07-25 12:42:08 -04:00
|
|
|
optget((*ctx)->clamdopts, "OnAccessDenyOnError")->enabled ? ((*ctx)->deny_on_error = 1) : ((*ctx)->deny_on_error = 0);
|
2019-05-09 12:42:33 -04:00
|
|
|
|
2019-04-25 16:11:39 -04:00
|
|
|
(*ctx)->isremote = onas_check_remote(ctx, &err);
|
|
|
|
if (err) {
|
2019-02-28 13:13:14 -05:00
|
|
|
return CL_EARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
remote = (*ctx)->isremote | optget(opts, "stream")->enabled;
|
|
|
|
#ifdef HAVE_FD_PASSING
|
2019-07-25 12:42:08 -04:00
|
|
|
if (!remote && optget((*ctx)->clamdopts, "LocalSocket")->enabled && (optget(opts, "fdpass")->enabled)) {
|
2020-12-02 18:47:03 -08:00
|
|
|
if (onas_set_sock_only_once(*ctx) == CL_EWRITE) {
|
|
|
|
return CL_EWRITE;
|
|
|
|
}
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_DEBUG, "ClamClient: client setup to scan via fd passing\n");
|
2019-02-28 13:13:14 -05:00
|
|
|
(*ctx)->scantype = FILDES;
|
2019-07-25 12:42:08 -04:00
|
|
|
(*ctx)->session = optget(opts, "multiscan")->enabled;
|
2019-02-28 13:13:14 -05:00
|
|
|
} else
|
|
|
|
#endif
|
2019-07-25 12:42:08 -04:00
|
|
|
if (remote) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_DEBUG, "ClamClient: client setup to scan via streaming\n");
|
2019-07-25 12:42:08 -04:00
|
|
|
(*ctx)->scantype = STREAM;
|
|
|
|
(*ctx)->session = optget(opts, "multiscan")->enabled;
|
|
|
|
} else if (optget(opts, "multiscan")->enabled) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_DEBUG, "ClamClient: client setup to scan in multiscan mode\n");
|
2019-07-25 12:42:08 -04:00
|
|
|
(*ctx)->scantype = MULTI;
|
|
|
|
} else if (optget(opts, "allmatch")->enabled) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_DEBUG, "ClamClient: client setup to scan in all-match mode\n");
|
2019-07-25 12:42:08 -04:00
|
|
|
(*ctx)->scantype = ALLMATCH;
|
|
|
|
} else {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_DEBUG, "ClamClient: client setup for continuous scanning\n");
|
2019-07-25 12:42:08 -04:00
|
|
|
(*ctx)->scantype = CONT;
|
|
|
|
}
|
2019-02-28 13:13:14 -05:00
|
|
|
|
2019-04-04 10:43:19 -04:00
|
|
|
(*ctx)->maxstream = optget((*ctx)->clamdopts, "StreamMaxLength")->numarg;
|
2019-02-28 13:13:14 -05:00
|
|
|
|
|
|
|
return CL_SUCCESS;
|
|
|
|
}
|
2018-12-03 13:17:07 -05:00
|
|
|
|
2019-02-28 13:13:14 -05:00
|
|
|
int onas_get_clamd_version(struct onas_context **ctx)
|
2018-12-03 13:17:07 -05:00
|
|
|
{
|
|
|
|
char *buff;
|
2019-04-25 16:11:39 -04:00
|
|
|
CURL *curl;
|
|
|
|
CURLcode curlcode;
|
|
|
|
cl_error_t err = CL_SUCCESS;
|
|
|
|
int b_remote;
|
2020-01-28 14:05:44 -05:00
|
|
|
int len;
|
2020-12-02 18:47:03 -08:00
|
|
|
struct onas_rcvln rcv;
|
2019-05-07 18:00:47 -04:00
|
|
|
int64_t timeout;
|
2021-05-28 16:48:10 +02:00
|
|
|
const char zVERSION[] = "zVERSION";
|
2019-05-07 18:00:47 -04:00
|
|
|
|
|
|
|
timeout = optget((*ctx)->clamdopts, "OnAccessCurlTimeout")->numarg;
|
2018-12-03 13:17:07 -05:00
|
|
|
|
2019-04-25 16:11:39 -04:00
|
|
|
b_remote = onas_check_remote(ctx, &err);
|
|
|
|
if (CL_SUCCESS != err) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: could not check to see if daemon was remote\n");
|
2019-07-25 12:42:08 -04:00
|
|
|
return 2;
|
2019-04-25 16:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!b_remote) {
|
2019-07-25 12:42:08 -04:00
|
|
|
curlcode = onas_curl_init(&curl, optget((*ctx)->clamdopts, "LocalSocket")->strarg, 0, timeout);
|
2019-04-25 16:11:39 -04:00
|
|
|
} else {
|
2019-07-25 12:42:08 -04:00
|
|
|
curlcode = onas_curl_init(&curl, optget((*ctx)->clamdopts, "TCPAddr")->strarg, (*ctx)->portnum, timeout);
|
|
|
|
if (CURLE_OK != curlcode) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: could not setup curl with tcp address and port, %s\n", curl_easy_strerror(curlcode));
|
2019-07-25 12:42:08 -04:00
|
|
|
/* curl cleanup done in onas_curl_init on error */
|
|
|
|
return 2;
|
|
|
|
}
|
2019-02-28 13:13:14 -05:00
|
|
|
}
|
2018-12-03 13:17:07 -05:00
|
|
|
|
2020-12-02 18:47:03 -08:00
|
|
|
onas_recvlninit(&rcv, curl, 0);
|
2019-04-25 16:11:39 -04:00
|
|
|
|
|
|
|
curlcode = curl_easy_perform(curl);
|
|
|
|
if (CURLE_OK != curlcode) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_DEBUG, "ClamClient: could not connect to clamd, %s\n", curl_easy_strerror(curlcode));
|
2021-07-09 14:35:05 -04:00
|
|
|
curl_easy_cleanup(curl);
|
2019-07-25 12:42:08 -04:00
|
|
|
return 2;
|
2019-04-25 16:11:39 -04:00
|
|
|
}
|
|
|
|
|
2021-05-28 16:48:10 +02:00
|
|
|
if (onas_sendln(curl, zVERSION, sizeof(zVERSION), timeout)) {
|
2019-05-07 18:00:47 -04:00
|
|
|
curl_easy_cleanup(curl);
|
2018-12-03 13:17:07 -05:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2019-07-25 12:42:08 -04:00
|
|
|
while ((len = onas_recvln(&rcv, &buff, NULL, timeout))) {
|
|
|
|
if (len == -1) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_DEBUG, "ClamClient: clamd did not respond with version information\n");
|
2018-12-03 13:17:07 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf("%s\n", buff);
|
|
|
|
}
|
|
|
|
|
2019-05-07 18:00:47 -04:00
|
|
|
curl_easy_cleanup(curl);
|
2018-12-03 13:17:07 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-24 12:05:36 -04:00
|
|
|
/**
|
|
|
|
* @brief kick off scanning and return results
|
|
|
|
*
|
|
|
|
* @param tcpaddr string string which refers to either the TCPaddress or the local socket to connect to
|
|
|
|
* @param portnum the port to use in case of TCP connection, set to 0 if connecting to a local socket
|
|
|
|
* @param scantype the type of scan to perform, e.g. fdpass, stream
|
|
|
|
* @param maxstream the max streamsize (in bytes) allowed across the socket per file
|
|
|
|
* @param fname the name of the file to be scanned
|
|
|
|
* @param fd the file descriptor for the file to be scanned, often (but not always) this is held by fanotify
|
|
|
|
* @param timeout time in ms to allow curl before timing out connection attempts
|
|
|
|
* @param sb variable to store and pass all of our stat info on the file so we don't have to access it multiple times (triggering multiple events)
|
|
|
|
* @param infected return variable indincating whether daemon returned with an infected verdict or not
|
|
|
|
* @param err return variable passed to the daemon protocol interface indicating how many things went wrong in the course of scanning
|
|
|
|
* @param ret_code return variable passed to the daemon protocol interface indicating last known issue or success
|
|
|
|
*/
|
2019-05-22 15:36:54 -04:00
|
|
|
int onas_client_scan(const char *tcpaddr, int64_t portnum, int32_t scantype, uint64_t maxstream, const char *fname, int fd, int64_t timeout, STATBUF sb, int *infected, int *err, cl_error_t *ret_code)
|
2018-12-03 13:17:07 -05:00
|
|
|
{
|
2019-07-25 12:42:08 -04:00
|
|
|
CURL *curl = NULL;
|
|
|
|
CURLcode curlcode = CURLE_OK;
|
|
|
|
int errors = 0;
|
2020-01-28 14:05:44 -05:00
|
|
|
int ret;
|
2021-01-03 18:40:48 -08:00
|
|
|
static bool disconnected = false;
|
2019-07-25 12:42:08 -04:00
|
|
|
|
|
|
|
*infected = 0;
|
|
|
|
|
|
|
|
if ((sb.st_mode & S_IFMT) != S_IFREG) {
|
|
|
|
scantype = STREAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
curlcode = onas_curl_init(&curl, tcpaddr, portnum, timeout);
|
|
|
|
if (CURLE_OK != curlcode) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: could not init curl for scanning, %s\n", curl_easy_strerror(curlcode));
|
2019-07-25 12:42:08 -04:00
|
|
|
/* curl cleanup done in onas_curl_init on error */
|
|
|
|
return CL_ECREAT;
|
|
|
|
}
|
2019-04-25 16:11:39 -04:00
|
|
|
|
2019-07-25 12:42:08 -04:00
|
|
|
curlcode = curl_easy_perform(curl);
|
|
|
|
if (CURLE_OK != curlcode) {
|
2021-01-03 18:40:48 -08:00
|
|
|
if (!disconnected) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "ClamClient: Connection to clamd failed, %s.\n", curl_easy_strerror(curlcode));
|
2021-01-03 18:40:48 -08:00
|
|
|
disconnected = true;
|
|
|
|
}
|
2021-07-09 14:35:05 -04:00
|
|
|
curl_easy_cleanup(curl);
|
2019-07-25 12:42:08 -04:00
|
|
|
return CL_ECREAT;
|
|
|
|
}
|
2021-01-03 18:40:48 -08:00
|
|
|
if (disconnected) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_INFO, "ClamClient: Connection to clamd re-established.\n");
|
2021-01-03 18:40:48 -08:00
|
|
|
disconnected = false;
|
|
|
|
}
|
2019-07-25 12:42:08 -04:00
|
|
|
|
|
|
|
if ((ret = onas_dsresult(curl, scantype, maxstream, fname, fd, timeout, &ret, err, ret_code)) >= 0) {
|
|
|
|
*infected = ret;
|
|
|
|
} else {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_DEBUG, "ClamClient: connection could not be established ... return code %d\n", *ret_code);
|
2019-07-25 12:42:08 -04:00
|
|
|
errors = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
return *infected ? CL_VIRUS : (errors ? CL_ECREAT : CL_CLEAN);
|
|
|
|
}
|