2003-07-29 15:48:06 +00:00
|
|
|
/*
|
2024-01-12 17:03:59 -05:00
|
|
|
* Copyright (C) 2013-2024 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
|
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>
|
2009-01-20 17:46:47 +00:00
|
|
|
#include <stdlib.h>
|
2018-12-03 12:40:13 -05:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-11-28 23:26:29 +00:00
|
|
|
#include <unistd.h>
|
2009-02-12 17:31:09 +00:00
|
|
|
#endif
|
2004-11-28 23:26:29 +00:00
|
|
|
#include <string.h>
|
2003-07-29 15:48:06 +00:00
|
|
|
#include <sys/types.h>
|
2004-03-29 00:00:58 +00:00
|
|
|
#include <sys/stat.h>
|
2008-08-01 14:50:33 +00:00
|
|
|
#ifdef HAVE_SYS_LIMITS_H
|
|
|
|
#include <sys/limits.h>
|
|
|
|
#endif
|
2009-09-24 16:08:52 +02:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
2009-02-04 17:18:58 +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
|
|
|
|
#include <sys/socket.h>
|
2003-07-29 15:48:06 +00:00
|
|
|
#include <sys/un.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2003-12-05 23:45:11 +00:00
|
|
|
#include <netdb.h>
|
2004-11-28 23:26:29 +00:00
|
|
|
#include <utime.h>
|
2009-09-24 19:23:21 +02:00
|
|
|
#endif
|
2004-11-28 23:26:29 +00:00
|
|
|
#include <errno.h>
|
2009-01-20 17:46:47 +00:00
|
|
|
#include <dirent.h>
|
2009-01-22 17:29:40 +00:00
|
|
|
#include <fcntl.h>
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2004-12-07 01:53:16 +00:00
|
|
|
#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"
|
2010-11-05 15:32:51 +01: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"
|
|
|
|
#include "clamdcom.h"
|
2008-12-30 21:16:02 +00:00
|
|
|
|
2021-08-27 09:14:45 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "scanmem.h"
|
|
|
|
#endif
|
|
|
|
|
2007-02-11 00:41:13 +00:00
|
|
|
#include "client.h"
|
2009-02-07 00:59:58 +00:00
|
|
|
#include "proto.h"
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2009-02-08 15:06:10 +00:00
|
|
|
unsigned long int maxstream;
|
2010-05-07 21:14:34 +02:00
|
|
|
extern struct optstruct *clamdopts;
|
2004-12-07 01:53:16 +00:00
|
|
|
|
2009-02-08 14:43:17 +00:00
|
|
|
/* Inits the communication layer
|
|
|
|
* Returns 0 if clamd is local, non zero if clamd is remote */
|
2018-12-03 12:40:13 -05:00
|
|
|
static int isremote(const struct optstruct *opts)
|
|
|
|
{
|
2009-01-23 09:59:40 +00:00
|
|
|
int s, ret;
|
|
|
|
const struct optstruct *opt;
|
2014-01-26 14:58:21 -05:00
|
|
|
char *ipaddr, port[10];
|
|
|
|
struct addrinfo hints, *info, *p;
|
|
|
|
int res;
|
2004-06-22 16:50:58 +00:00
|
|
|
|
2014-07-11 09:30:58 -04:00
|
|
|
UNUSEDPARAM(opts);
|
|
|
|
|
2009-09-24 16:08:52 +02:00
|
|
|
#ifndef _WIN32
|
2018-12-03 12:40:13 -05:00
|
|
|
if ((opt = optget(clamdopts, "LocalSocket"))->enabled) {
|
2014-01-25 21:50:33 -05:00
|
|
|
memset((void *)&nixsock, 0, sizeof(nixsock));
|
|
|
|
nixsock.sun_family = AF_UNIX;
|
|
|
|
strncpy(nixsock.sun_path, opt->strarg, sizeof(nixsock.sun_path));
|
2018-12-03 12:40:13 -05:00
|
|
|
nixsock.sun_path[sizeof(nixsock.sun_path) - 1] = '\0';
|
2014-01-25 21:50:33 -05:00
|
|
|
return 0;
|
2004-07-28 20:28:26 +00:00
|
|
|
}
|
2009-09-24 16:08:52 +02:00
|
|
|
#endif
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!(opt = optget(clamdopts, "TCPSocket"))->enabled)
|
2014-01-25 21:50:33 -05:00
|
|
|
return 0;
|
2010-05-07 21:14:34 +02:00
|
|
|
|
2014-01-26 14:58:21 -05:00
|
|
|
snprintf(port, sizeof(port), "%lld", optget(clamdopts, "TCPSocket")->numarg);
|
2009-02-12 21:14:51 +00:00
|
|
|
|
2014-01-25 21:50:33 -05:00
|
|
|
opt = optget(clamdopts, "TCPAddr");
|
|
|
|
while (opt) {
|
2014-05-13 11:10:18 -04:00
|
|
|
ipaddr = NULL;
|
|
|
|
if (opt->strarg)
|
2014-01-26 14:58:21 -05:00
|
|
|
ipaddr = (!strcmp(opt->strarg, "any") ? NULL : opt->strarg);
|
|
|
|
|
|
|
|
memset(&hints, 0x00, sizeof(struct addrinfo));
|
2018-12-03 12:40:13 -05:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
2014-01-26 14:58:21 -05:00
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
2018-12-03 12:40:13 -05:00
|
|
|
hints.ai_flags = AI_PASSIVE;
|
2014-01-26 14:58:21 -05:00
|
|
|
|
|
|
|
if ((res = getaddrinfo(ipaddr, port, &hints, &info))) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "Can't lookup clamd hostname: %s\n", gai_strerror(res));
|
2014-05-13 11:10:18 -04:00
|
|
|
opt = opt->nextarg;
|
2014-01-26 14:58:21 -05:00
|
|
|
continue;
|
2014-01-25 21:50:33 -05:00
|
|
|
}
|
|
|
|
|
2014-01-26 14:58:21 -05:00
|
|
|
for (p = info; p != NULL; p = p->ai_next) {
|
2018-12-03 12:40:13 -05:00
|
|
|
if ((s = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_INFO, "isremote: socket() returning: %s.\n", strerror(errno));
|
2014-01-26 14:58:21 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-01-27 14:34:02 -05:00
|
|
|
switch (p->ai_family) {
|
2018-12-03 12:40:13 -05:00
|
|
|
case AF_INET:
|
|
|
|
((struct sockaddr_in *)(p->ai_addr))->sin_port = htons(INADDR_ANY);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
((struct sockaddr_in6 *)(p->ai_addr))->sin6_port = htons(INADDR_ANY);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2014-01-27 14:34:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = bind(s, p->ai_addr, p->ai_addrlen);
|
2014-01-26 14:58:21 -05:00
|
|
|
if (ret) {
|
2014-01-27 14:34:02 -05:00
|
|
|
if (errno == EADDRINUSE) {
|
2020-01-03 15:44:07 -05:00
|
|
|
/*
|
2014-01-27 14:34:02 -05:00
|
|
|
* If we can't bind, then either we're attempting to listen on an IP that isn't
|
|
|
|
* ours or that clamd is already listening on.
|
|
|
|
*/
|
|
|
|
closesocket(s);
|
|
|
|
freeaddrinfo(info);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
closesocket(s);
|
2014-01-26 14:58:21 -05:00
|
|
|
freeaddrinfo(info);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-01-27 14:34:02 -05:00
|
|
|
|
|
|
|
closesocket(s);
|
2014-01-25 21:50:33 -05:00
|
|
|
}
|
|
|
|
|
2014-01-26 14:58:21 -05:00
|
|
|
freeaddrinfo(info);
|
2014-01-25 21:50:33 -05:00
|
|
|
|
2014-05-13 11:10:18 -04:00
|
|
|
opt = opt->nextarg;
|
2012-09-14 08:00:47 -07:00
|
|
|
}
|
2014-01-26 14:58:21 -05:00
|
|
|
|
2014-01-27 14:34:02 -05:00
|
|
|
return 0;
|
2004-06-22 16:50:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 13:20:12 -05:00
|
|
|
/* pings clamd at the specified interval the number of time specified
|
2023-11-26 15:01:19 -08:00
|
|
|
* return 0 on a successful connection, 1 upon timeout, -1 on error */
|
2019-12-18 13:20:12 -05:00
|
|
|
int16_t ping_clamd(const struct optstruct *opts)
|
|
|
|
{
|
|
|
|
|
|
|
|
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;
|
|
|
|
int64_t sockd;
|
|
|
|
struct RCVLN rcv;
|
|
|
|
uint16_t ret = 0;
|
|
|
|
|
|
|
|
if (opts == NULL) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "null parameter was passed\n");
|
2019-12-18 13:20:12 -05:00
|
|
|
ret = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ping command takes the form --ping [attempts[:interval]] */
|
2020-07-31 16:04:38 -07:00
|
|
|
if (NULL != (opt = optget(opts, "ping"))) {
|
|
|
|
if (NULL != opt->strarg) {
|
2024-01-09 17:44:33 -05:00
|
|
|
if (NULL == (attempt_str = cli_safer_strdup(opt->strarg))) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "could not allocate memory for string\n");
|
2019-12-18 13:20:12 -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-18 13:20:12 -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-18 13:20:12 -05:00
|
|
|
ret = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
interval = CLAMDSCAN_DEFAULT_PING_INTERVAL;
|
|
|
|
}
|
|
|
|
attempts = cli_strntoul(attempt_str, strlen(attempt_str), &errchk, 10);
|
|
|
|
if (attempt_str + strlen(attempt_str) > errchk) {
|
2024-01-19 09:08:36 -08:00
|
|
|
logg(LOGG_WARNING, "attempt_str would go past end of buffer\n");
|
2019-12-18 13:20:12 -05:00
|
|
|
ret = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
attempts = CLAMDSCAN_DEFAULT_PING_ATTEMPTS;
|
|
|
|
interval = CLAMDSCAN_DEFAULT_PING_INTERVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isremote(opts);
|
|
|
|
do {
|
2021-08-27 09:14:45 -07:00
|
|
|
if ((sockd = dconnect(clamdopts)) >= 0) {
|
2021-05-28 16:35:35 +02:00
|
|
|
const char zPING[] = "zPING";
|
2019-12-18 13:20:12 -05:00
|
|
|
recvlninit(&rcv, sockd);
|
|
|
|
|
2021-05-28 16:35:35 +02:00
|
|
|
if (sendln(sockd, zPING, sizeof(zPING))) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_DEBUG, "PING failed...\n");
|
2019-12-18 13:20:12 -05:00
|
|
|
closesocket(sockd);
|
|
|
|
} else {
|
|
|
|
if (!optget(opts, "wait")->enabled) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_INFO, "PONG\n");
|
2019-12-18 13:20:12 -05:00
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i + 1 < attempts) {
|
2021-01-03 18:40:48 -08:00
|
|
|
if (optget(opts, "wait")->enabled) {
|
|
|
|
if (interval == 1)
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_DEBUG, "Could not connect, 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, "Could not connect, 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, "Could not connect, will PING 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, "Could not connect, will PING again in %lu seconds\n", interval);
|
2021-01-03 18:40:48 -08:00
|
|
|
}
|
2019-12-18 13:20:12 -05:00
|
|
|
sleep(interval);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
} while (i < attempts);
|
|
|
|
|
2020-08-14 17:55:17 -07:00
|
|
|
/* timed out */
|
2019-12-18 13:20:12 -05:00
|
|
|
ret = 1;
|
2021-01-03 18:40:48 -08:00
|
|
|
if (optget(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
|
|
|
}
|
2019-12-18 13:20:12 -05:00
|
|
|
|
|
|
|
done:
|
|
|
|
if (attempt_str) {
|
|
|
|
free(attempt_str);
|
|
|
|
}
|
|
|
|
attempt_str = NULL;
|
|
|
|
interval_str = NULL;
|
|
|
|
errchk = NULL;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-02-08 14:43:17 +00:00
|
|
|
/* Turns a relative path into an absolute one
|
2020-01-03 15:44:07 -05:00
|
|
|
* Returns a pointer to the path (which must be
|
2009-02-08 14:43:17 +00:00
|
|
|
* freed by the caller) or NULL on error */
|
2018-12-03 12:40:13 -05:00
|
|
|
static char *makeabs(const char *basepath)
|
|
|
|
{
|
2009-01-30 11:30:54 +00:00
|
|
|
int namelen;
|
2009-02-04 17:18:58 +00:00
|
|
|
char *ret;
|
2004-06-22 16:50:58 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!(ret = malloc(PATH_MAX + 1))) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_WARNING, "Can't make room for fullpath.\n");
|
2018-12-03 12:40:13 -05:00
|
|
|
return NULL;
|
2009-02-04 17:18:58 +00:00
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!cli_is_abspath(basepath)) {
|
|
|
|
if (!getcwd(ret, PATH_MAX)) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_WARNING, "Can't get absolute pathname of current working directory.\n");
|
2018-12-03 12:40:13 -05:00
|
|
|
free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-03-19 02:48:12 +01:00
|
|
|
#ifdef _WIN32
|
2018-12-03 12:40:13 -05:00
|
|
|
if (*basepath == '\\') {
|
|
|
|
namelen = 2;
|
|
|
|
basepath++;
|
|
|
|
} else
|
2010-03-19 02:48:12 +01:00
|
|
|
#endif
|
2018-12-03 12:40:13 -05:00
|
|
|
namelen = strlen(ret);
|
|
|
|
snprintf(&ret[namelen], PATH_MAX - namelen, PATHSEP "%s", basepath);
|
2003-07-29 15:48:06 +00:00
|
|
|
} else {
|
2018-12-03 12:40:13 -05:00
|
|
|
strncpy(ret, basepath, PATH_MAX);
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
2009-02-04 17:18:58 +00:00
|
|
|
ret[PATH_MAX] = '\0';
|
|
|
|
return ret;
|
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2009-02-08 14:43:17 +00:00
|
|
|
/* Recursively scans a path with the given scantype
|
|
|
|
* Returns non zero for serious errors, zero otherwise */
|
2018-12-03 12:40:13 -05:00
|
|
|
static int client_scan(const char *file, int scantype, int *infected, int *err, int maxlevel, int session, int flags)
|
|
|
|
{
|
2009-02-04 17:18:58 +00:00
|
|
|
int ret;
|
2021-06-29 17:27:07 -07:00
|
|
|
char *real_path = NULL;
|
|
|
|
char *fullpath = NULL;
|
|
|
|
|
|
|
|
/* Convert relative path to fullpath */
|
|
|
|
fullpath = makeabs(file);
|
|
|
|
|
|
|
|
/* Convert fullpath to the real path (evaluating symlinks and . and ..).
|
|
|
|
Doing this early on will ensure that the scan results will appear consistent
|
|
|
|
across regular scans, --fdpass scans, and --stream scans. */
|
|
|
|
if (CL_SUCCESS != cli_realpath(fullpath, &real_path)) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_DEBUG, "client_scan: Failed to determine real filename of %s.\n", fullpath);
|
2021-06-29 17:27:07 -07:00
|
|
|
} else {
|
|
|
|
free(fullpath);
|
|
|
|
fullpath = real_path;
|
|
|
|
}
|
2009-02-04 17:18:58 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!fullpath)
|
|
|
|
return 0;
|
2009-02-04 17:18:58 +00:00
|
|
|
if (!session)
|
2018-12-03 12:40:13 -05:00
|
|
|
ret = serial_client_scan(fullpath, scantype, infected, err, maxlevel, flags);
|
2009-02-04 17:18:58 +00:00
|
|
|
else
|
2018-12-03 12:40:13 -05:00
|
|
|
ret = parallel_client_scan(fullpath, scantype, infected, err, maxlevel, flags);
|
2009-02-04 17:18:58 +00:00
|
|
|
free(fullpath);
|
2009-02-08 14:43:17 +00:00
|
|
|
return ret;
|
2004-06-22 16:50:58 +00:00
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2008-12-30 21:16:02 +00:00
|
|
|
int get_clamd_version(const struct optstruct *opts)
|
2008-01-10 13:57:05 +00:00
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
char *buff;
|
|
|
|
int len, sockd;
|
|
|
|
struct RCVLN rcv;
|
2021-05-28 16:35:35 +02:00
|
|
|
const char zVERSION[] = "zVERSION";
|
2008-01-10 13:57:05 +00:00
|
|
|
|
2009-01-23 09:59:40 +00:00
|
|
|
isremote(opts);
|
2021-08-27 09:14:45 -07:00
|
|
|
if ((sockd = dconnect(clamdopts)) < 0) return 2;
|
2009-01-23 11:59:11 +00:00
|
|
|
recvlninit(&rcv, sockd);
|
2008-01-10 13:57:05 +00:00
|
|
|
|
2021-05-28 16:35:35 +02:00
|
|
|
if (sendln(sockd, zVERSION, sizeof(zVERSION))) {
|
2018-12-03 12:40:13 -05:00
|
|
|
closesocket(sockd);
|
|
|
|
return 2;
|
2008-01-10 13:57:05 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
while ((len = recvln(&rcv, &buff, NULL))) {
|
|
|
|
if (len == -1) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "Error occurred while receiving version information.\n");
|
2018-12-03 12:40:13 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf("%s\n", buff);
|
2008-01-10 13:57:05 +00:00
|
|
|
}
|
|
|
|
|
2009-09-24 16:21:51 +02:00
|
|
|
closesocket(sockd);
|
2008-01-10 13:57:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-30 21:16:02 +00:00
|
|
|
int reload_clamd_database(const struct optstruct *opts)
|
2008-09-18 08:17:47 +00:00
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
char *buff;
|
|
|
|
int len, sockd;
|
|
|
|
struct RCVLN rcv;
|
2021-05-28 16:35:35 +02:00
|
|
|
const char zRELOAD[] = "zRELOAD";
|
2008-09-18 08:17:47 +00:00
|
|
|
|
2009-01-23 09:59:40 +00:00
|
|
|
isremote(opts);
|
2021-08-27 09:14:45 -07:00
|
|
|
if ((sockd = dconnect(clamdopts)) < 0) return 2;
|
2009-01-23 11:59:11 +00:00
|
|
|
recvlninit(&rcv, sockd);
|
2008-09-18 08:17:47 +00:00
|
|
|
|
2021-05-28 16:35:35 +02:00
|
|
|
if (sendln(sockd, zRELOAD, sizeof(zRELOAD))) {
|
2018-12-03 12:40:13 -05:00
|
|
|
closesocket(sockd);
|
|
|
|
return 2;
|
2008-09-18 08:17:47 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!(len = recvln(&rcv, &buff, NULL)) || len < 10 || memcmp(buff, "RELOADING", 9)) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "Clamd did not reload the database\n");
|
2018-12-03 12:40:13 -05:00
|
|
|
closesocket(sockd);
|
|
|
|
return 2;
|
2008-09-18 08:17:47 +00:00
|
|
|
}
|
2009-09-24 16:21:51 +02:00
|
|
|
closesocket(sockd);
|
2008-09-18 08:17:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-03 18:23:30 +01:00
|
|
|
int client(const struct optstruct *opts, int *infected, int *err)
|
2004-06-22 16:50:58 +00:00
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
int remote, scantype, session = 0, errors = 0, scandash = 0, maxrec, flags = 0;
|
|
|
|
const char *fname;
|
2007-01-13 00:02:54 +00:00
|
|
|
|
2019-12-18 13:20:12 -05:00
|
|
|
if (optget(opts, "wait")->enabled) {
|
2020-08-14 17:55:17 -07:00
|
|
|
int16_t ping_result = ping_clamd(opts);
|
|
|
|
switch (ping_result) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
return (int)CL_ETIMEOUT;
|
|
|
|
default:
|
|
|
|
return (int)CL_ERROR;
|
2019-12-18 13:20:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-21 13:43:05 +00:00
|
|
|
scandash = (opts->filename && opts->filename[0] && !strcmp(opts->filename[0], "-") && !optget(opts, "file-list")->enabled && !opts->filename[1]);
|
2018-12-03 12:40:13 -05:00
|
|
|
remote = isremote(opts) | optget(opts, "stream")->enabled;
|
2009-01-22 17:29:40 +00:00
|
|
|
#ifdef HAVE_FD_PASSING
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!remote && optget(clamdopts, "LocalSocket")->enabled && (optget(opts, "fdpass")->enabled || scandash)) {
|
|
|
|
scantype = FILDES;
|
|
|
|
session = optget(opts, "multiscan")->enabled;
|
|
|
|
} else
|
2009-01-22 17:29:40 +00:00
|
|
|
#endif
|
2018-12-03 12:40:13 -05:00
|
|
|
if (remote || scandash) {
|
|
|
|
scantype = STREAM;
|
|
|
|
session = optget(opts, "multiscan")->enabled;
|
|
|
|
} else if (optget(opts, "multiscan")->enabled)
|
|
|
|
scantype = MULTI;
|
|
|
|
else if (optget(opts, "allmatch")->enabled)
|
|
|
|
scantype = ALLMATCH;
|
|
|
|
else
|
|
|
|
scantype = CONT;
|
|
|
|
|
|
|
|
maxrec = optget(clamdopts, "MaxDirectoryRecursion")->numarg;
|
2009-02-09 16:23:38 +00:00
|
|
|
maxstream = optget(clamdopts, "StreamMaxLength")->numarg;
|
2009-02-12 15:14:12 +00:00
|
|
|
if (optget(clamdopts, "FollowDirectorySymlinks")->enabled)
|
2018-12-03 12:40:13 -05:00
|
|
|
flags |= CLI_FTW_FOLLOW_DIR_SYMLINK;
|
2009-02-12 15:14:12 +00:00
|
|
|
if (optget(clamdopts, "FollowFileSymlinks")->enabled)
|
2018-12-03 12:40:13 -05:00
|
|
|
flags |= CLI_FTW_FOLLOW_FILE_SYMLINK;
|
2009-02-24 13:21:27 +00:00
|
|
|
flags |= CLI_FTW_TRIM_SLASHES;
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2009-01-22 17:29:40 +00:00
|
|
|
*infected = 0;
|
2004-03-29 00:00:58 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (scandash) {
|
|
|
|
int sockd, ret;
|
|
|
|
STATBUF sb;
|
|
|
|
if (FSTAT(0, &sb) < 0) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_INFO, "client.c: fstat failed for file name \"%s\", with %s\n.",
|
2018-12-03 12:40:13 -05:00
|
|
|
opts->filename[0], strerror(errno));
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if ((sb.st_mode & S_IFMT) != S_IFREG) scantype = STREAM;
|
2021-08-27 09:14:45 -07:00
|
|
|
if ((sockd = dconnect(clamdopts)) >= 0 && (ret = dsresult(sockd, scantype, NULL, &ret, NULL, clamdopts)) >= 0)
|
2018-12-03 12:40:13 -05:00
|
|
|
*infected = ret;
|
|
|
|
else
|
|
|
|
errors = 1;
|
|
|
|
if (sockd >= 0) closesocket(sockd);
|
|
|
|
} else if (opts->filename || optget(opts, "file-list")->enabled) {
|
|
|
|
if (opts->filename && optget(opts, "file-list")->enabled)
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_WARNING, "Only scanning files from --file-list (files passed at cmdline are ignored)\n");
|
2018-12-03 12:40:13 -05:00
|
|
|
|
|
|
|
while ((fname = filelist(opts, NULL))) {
|
|
|
|
if (!strcmp(fname, "-")) {
|
2022-02-16 00:13:55 +01:00
|
|
|
logg(LOGG_ERROR, "Scanning from standard input requires \"-\" to be the only file argument\n");
|
2018-12-03 12:40:13 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
errors += client_scan(fname, scantype, infected, err, maxrec, session, flags);
|
|
|
|
/* this may be too strict
|
2022-02-16 00:13:55 +01:00
|
|
|
if(errors >= 10) {
|
|
|
|
logg(LOGG_ERROR, "Too many errors\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*/
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
2021-08-27 09:14:45 -07:00
|
|
|
}
|
|
|
|
#ifdef _WIN32
|
|
|
|
else if (optget(opts, "memory")->enabled) {
|
|
|
|
struct mem_info minfo;
|
|
|
|
minfo.d = 1;
|
|
|
|
minfo.opts = opts;
|
|
|
|
minfo.ifiles = *infected;
|
|
|
|
minfo.errors = errors;
|
|
|
|
int res = scanmem(&minfo);
|
|
|
|
*infected = minfo.ifiles;
|
|
|
|
*err = minfo.errors;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else {
|
2018-12-03 12:40:13 -05:00
|
|
|
errors = client_scan("", scantype, infected, err, maxrec, session, flags);
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
2004-06-22 16:50:58 +00:00
|
|
|
return *infected ? 1 : (errors ? 2 : 0);
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|