2018-12-03 13:17:07 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015-2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
|
|
|
|
* 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>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#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
|
|
|
|
|
|
|
|
#include "libclamav/clamav.h"
|
|
|
|
#include "shared/optparser.h"
|
|
|
|
#include "shared/output.h"
|
|
|
|
#include "shared/misc.h"
|
|
|
|
#include "shared/actions.h"
|
|
|
|
#include "shared/clamdcom.h"
|
|
|
|
|
|
|
|
#include "libclamav/str.h"
|
|
|
|
#include "libclamav/others.h"
|
|
|
|
|
|
|
|
#include "onaccess_client.h"
|
2018-12-17 11:47:18 -05:00
|
|
|
#include "onaccess_proto.h"
|
2018-12-03 13:17:07 -05:00
|
|
|
|
2019-02-28 13:13:14 -05:00
|
|
|
#include "../clamonacc.h"
|
|
|
|
|
2018-12-03 13:17:07 -05:00
|
|
|
struct sockaddr_un nixsock;
|
|
|
|
|
2019-02-28 13:13:14 -05:00
|
|
|
static void print_server_version(struct onas_context **ctx)
|
|
|
|
{
|
|
|
|
if(onas_get_clamd_version(ctx)) {
|
|
|
|
/* can't get version from server, fallback */
|
|
|
|
printf("ClamAV %s\n", get_version());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-03-26 11:34:54 -04:00
|
|
|
int onas_check_remote(struct onas_context **ctx) {
|
2018-12-03 13:17:07 -05:00
|
|
|
int s, ret;
|
|
|
|
const struct optstruct *opt;
|
2019-02-28 13:13:14 -05:00
|
|
|
char *ipaddr = NULL;
|
|
|
|
char port[10];
|
2018-12-03 13:17:07 -05:00
|
|
|
struct addrinfo hints, *info, *p;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2019-02-28 13:13:14 -05:00
|
|
|
if((opt = optget((*ctx)->clamdopts, "LocalSocket"))->enabled) {
|
2018-12-03 13:17:07 -05:00
|
|
|
memset((void *)&nixsock, 0, sizeof(nixsock));
|
|
|
|
nixsock.sun_family = AF_UNIX;
|
|
|
|
strncpy(nixsock.sun_path, opt->strarg, sizeof(nixsock.sun_path));
|
|
|
|
nixsock.sun_path[sizeof(nixsock.sun_path)-1]='\0';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2019-02-28 13:13:14 -05:00
|
|
|
if(!(opt = optget((*ctx)->clamdopts, "TCPSocket"))->enabled)
|
2018-12-03 13:17:07 -05:00
|
|
|
return 0;
|
|
|
|
|
2019-02-28 13:13:14 -05:00
|
|
|
snprintf(port, sizeof(port), "%lld", optget((*ctx)->clamdopts, "TCPSocket")->numarg);
|
2018-12-03 13:17:07 -05:00
|
|
|
|
2019-02-28 13:13:14 -05:00
|
|
|
opt = optget((*ctx)->clamdopts, "TCPAddr");
|
2018-12-03 13:17:07 -05:00
|
|
|
while (opt) {
|
2019-02-28 13:13:14 -05:00
|
|
|
|
2018-12-03 13:17:07 -05:00
|
|
|
if (opt->strarg)
|
|
|
|
ipaddr = (!strcmp(opt->strarg, "any") ? NULL : opt->strarg);
|
|
|
|
|
|
|
|
memset(&hints, 0x00, sizeof(struct addrinfo));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
|
|
|
|
if ((res = getaddrinfo(ipaddr, port, &hints, &info))) {
|
|
|
|
logg("!Can't lookup clamd hostname: %s\n", gai_strerror(res));
|
|
|
|
opt = opt->nextarg;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (p = info; p != NULL; p = p->ai_next) {
|
|
|
|
if((s = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0) {
|
|
|
|
logg("isremote: socket() returning: %s.\n", strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (p->ai_family) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = bind(s, p->ai_addr, p->ai_addrlen);
|
|
|
|
if (ret) {
|
|
|
|
if (errno == EADDRINUSE) {
|
2019-02-28 13:13:14 -05:00
|
|
|
/*
|
2018-12-03 13:17:07 -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);
|
|
|
|
freeaddrinfo(info);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
closesocket(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(info);
|
|
|
|
|
|
|
|
opt = opt->nextarg;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-28 13:13:14 -05:00
|
|
|
cl_error_t onas_setup_client (struct onas_context **ctx) {
|
|
|
|
|
|
|
|
const struct optstruct *opts;
|
|
|
|
const struct optstruct *opt;
|
|
|
|
errno = 0;
|
|
|
|
int remote;
|
|
|
|
|
|
|
|
opts = (*ctx)->opts;
|
|
|
|
|
|
|
|
if(optget(opts, "verbose")->enabled) {
|
2019-04-04 10:43:19 -04:00
|
|
|
mprintf_verbose = 1;
|
2019-02-28 13:13:14 -05:00
|
|
|
logg_verbose = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(optget(opts, "version")->enabled) {
|
|
|
|
print_server_version(ctx);
|
|
|
|
return CL_BREAK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(optget(opts, "help")->enabled) {
|
|
|
|
help();
|
|
|
|
return CL_BREAK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(optget(opts, "infected")->enabled) {
|
|
|
|
(*ctx)->printinfected = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize logger */
|
|
|
|
|
|
|
|
if((opt = optget(opts, "log"))->enabled) {
|
|
|
|
logg_file = opt->strarg;
|
|
|
|
if(logg("--------------------------------------\n")) {
|
2019-04-04 10:43:19 -04:00
|
|
|
logg("!ClamClient: problem with internal logger\n");
|
2019-02-28 13:13:14 -05:00
|
|
|
return CL_EARG;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
logg_file = NULL;
|
|
|
|
|
|
|
|
if(actsetup(opts)) {
|
|
|
|
return CL_EARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (onas_check_remote(ctx)) {
|
|
|
|
(*ctx)->isremote = 1;
|
|
|
|
} else if (errno == EADDRINUSE) {
|
|
|
|
return CL_EARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
remote = (*ctx)->isremote | optget(opts, "stream")->enabled;
|
|
|
|
#ifdef HAVE_FD_PASSING
|
|
|
|
if(!remote && optget((*ctx)->clamdopts, "LocalSocket")->enabled && (optget(opts, "fdpass")->enabled)) {
|
2019-04-04 10:43:19 -04:00
|
|
|
logg("*ClamClient: client setup to scan via fd passing\n");
|
2019-02-28 13:13:14 -05:00
|
|
|
(*ctx)->scantype = FILDES;
|
|
|
|
(*ctx)->session = optget(opts, "multiscan")->enabled;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if(remote) {
|
2019-04-04 10:43:19 -04:00
|
|
|
logg("*ClamClient: client setup to scan via streaming\n");
|
2019-02-28 13:13:14 -05:00
|
|
|
(*ctx)->scantype = STREAM;
|
|
|
|
(*ctx)->session = optget(opts, "multiscan")->enabled;
|
|
|
|
} else if(optget(opts, "multiscan")->enabled) {
|
2019-04-04 10:43:19 -04:00
|
|
|
logg("*ClamClient: client setup to scan in multiscan mode\n");
|
2019-02-28 13:13:14 -05:00
|
|
|
(*ctx)->scantype = MULTI;
|
|
|
|
} else if(optget(opts, "allmatch")->enabled) {
|
2019-04-04 10:43:19 -04:00
|
|
|
logg("*ClamClient: client setup to scan in all-match mode\n");
|
2019-02-28 13:13:14 -05:00
|
|
|
(*ctx)->scantype = ALLMATCH;
|
|
|
|
} else {
|
2019-04-04 10:43:19 -04:00
|
|
|
logg("*ClamClient: client setup for continuous scanning\n");
|
2019-02-28 13:13:14 -05:00
|
|
|
(*ctx)->scantype = CONT;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
/* Turns a relative path into an absolute one
|
2019-02-28 13:13:14 -05:00
|
|
|
* Returns a pointer to the path (which must be
|
2018-12-03 13:17:07 -05:00
|
|
|
* freed by the caller) or NULL on error */
|
2019-02-28 13:13:14 -05:00
|
|
|
static char *onas_make_absolute(const char *basepath) {
|
2018-12-03 13:17:07 -05:00
|
|
|
int namelen;
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
if(!(ret = malloc(PATH_MAX + 1))) {
|
2019-03-26 11:34:54 -04:00
|
|
|
logg("^ClamClient: can't make room for fullpath\n");
|
2018-12-03 13:17:07 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if(!cli_is_abspath(basepath)) {
|
|
|
|
if(!getcwd(ret, PATH_MAX)) {
|
2019-04-04 10:43:19 -04:00
|
|
|
logg("^ClamClient: can't get absolute pathname of current working directory.\n");
|
2018-12-03 13:17:07 -05:00
|
|
|
free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if(*basepath == '\\') {
|
|
|
|
namelen = 2;
|
|
|
|
basepath++;
|
2019-02-28 13:13:14 -05:00
|
|
|
} else {
|
|
|
|
namelen = strlen(ret);
|
|
|
|
}
|
2018-12-03 13:17:07 -05:00
|
|
|
snprintf(&ret[namelen], PATH_MAX - namelen, PATHSEP"%s", basepath);
|
|
|
|
} else {
|
|
|
|
strncpy(ret, basepath, PATH_MAX);
|
|
|
|
}
|
|
|
|
ret[PATH_MAX] = '\0';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
int len, sockd;
|
|
|
|
struct RCVLN rcv;
|
|
|
|
|
2019-02-28 13:13:14 -05:00
|
|
|
onas_check_remote(ctx);
|
2019-04-04 10:43:19 -04:00
|
|
|
if((sockd = onas_dconnect(ctx)) < 0) {
|
2019-02-28 13:13:14 -05:00
|
|
|
return 2;
|
|
|
|
}
|
2018-12-03 13:17:07 -05:00
|
|
|
recvlninit(&rcv, sockd);
|
|
|
|
|
|
|
|
if(sendln(sockd, "zVERSION", 9)) {
|
|
|
|
closesocket(sockd);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
while((len = recvln(&rcv, &buff, NULL))) {
|
|
|
|
if(len == -1) {
|
2019-04-04 10:43:19 -04:00
|
|
|
logg("*ClamClient: clamd did not respond with version information\n");
|
2018-12-03 13:17:07 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf("%s\n", buff);
|
|
|
|
}
|
|
|
|
|
|
|
|
closesocket(sockd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-04 10:43:19 -04:00
|
|
|
int onas_client_scan(struct onas_context **ctx, const char *fname, STATBUF sb, int *infected, int *err, cl_error_t *ret_code)
|
2018-12-03 13:17:07 -05:00
|
|
|
{
|
2019-02-28 13:13:14 -05:00
|
|
|
int scantype, errors = 0;
|
|
|
|
int sockd, ret;
|
2018-12-03 13:17:07 -05:00
|
|
|
|
2019-02-28 13:13:14 -05:00
|
|
|
*infected = 0;
|
2018-12-03 13:17:07 -05:00
|
|
|
|
2019-02-28 13:13:14 -05:00
|
|
|
if((sb.st_mode & S_IFMT) != S_IFREG) {
|
|
|
|
scantype = STREAM;
|
|
|
|
} else {
|
|
|
|
scantype = (*ctx)->scantype;
|
|
|
|
}
|
2018-12-03 13:17:07 -05:00
|
|
|
|
2019-04-04 10:43:19 -04:00
|
|
|
/* logg here is noisy even for debug, enable only for dev work if something has gone very wrong. */
|
|
|
|
//logg("*ClamClient: connecting to daemon ...\n");
|
|
|
|
if((sockd = onas_dconnect(ctx)) >= 0 && (ret = onas_dsresult(ctx, sockd, scantype, fname, &ret, err, ret_code)) >= 0) {
|
2019-02-28 13:13:14 -05:00
|
|
|
*infected = ret;
|
|
|
|
} else {
|
2019-04-15 07:29:20 -07:00
|
|
|
logg("*ClamClient: connection could not be established ... return code %d\n", *ret_code);
|
2019-02-28 13:13:14 -05:00
|
|
|
errors = 1;
|
|
|
|
}
|
|
|
|
if(sockd >= 0) {
|
2019-04-04 10:43:19 -04:00
|
|
|
/* logg here is noisy even for debug, enable only for dev work if something has gone very wrong. */
|
|
|
|
//logg("*ClamClient: done, closing connection ...\n");
|
2019-02-28 13:13:14 -05:00
|
|
|
closesocket(sockd);
|
|
|
|
}
|
2018-12-03 13:17:07 -05:00
|
|
|
|
2019-02-28 13:13:14 -05:00
|
|
|
return *infected ? 1 : (errors ? 2 : 0);
|
2018-12-03 13:17:07 -05:00
|
|
|
}
|