e3aaff8e |
/* |
e1cbc270 |
* Copyright (C) 2013-2019 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
* Copyright (C) 2002-2013 Sourcefire, Inc. |
e3aaff8e |
*
* This program is free software; you can redistribute it and/or modify |
bb34cb31 |
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. |
e3aaff8e |
*
* 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 |
48b7b4a7 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA. |
e3aaff8e |
*/
|
6d6e8271 |
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
|
d71dd823 |
#ifdef BUILD_CLAMD
|
e3aaff8e |
#include <stdio.h> |
288057e9 |
#ifdef HAVE_UNISTD_H |
e3aaff8e |
#include <unistd.h> |
17ad1c5b |
#endif |
e3aaff8e |
#include <sys/types.h> |
288057e9 |
#ifndef _WIN32 |
e3aaff8e |
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h> |
a616f2de |
#include <netdb.h> |
17ad1c5b |
#endif |
ee039e40 |
#include <string.h> |
4542f1cd |
#include <errno.h> |
e3aaff8e |
|
3f7802c9 |
#include "shared/optparser.h" |
a889f40e |
#include "shared/output.h" |
3507891f |
#include "shared/clamdcom.h"
|
fc83da82 |
#include "notify.h" |
e3aaff8e |
|
288057e9 |
int clamd_connect(const char *cfgfile, const char *option) |
e3aaff8e |
{ |
288057e9 |
#ifndef _WIN32 |
6df88f29 |
struct sockaddr_un server; |
17ad1c5b |
#endif |
bd7db3da |
|
19470623 |
struct addrinfo hints, *res, *p; |
6df88f29 |
char port[6];
int ret; |
bd7db3da |
|
6df88f29 |
struct optstruct *opts;
const struct optstruct *opt;
int sockd; |
e3aaff8e |
|
288057e9 |
if ((opts = optparse(cfgfile, 0, NULL, 1, OPT_CLAMD, 0, NULL)) == NULL) {
logg("!%s: Can't find or parse configuration file %s\n", option,
cfgfile); |
6df88f29 |
return -11; |
e3aaff8e |
}
|
288057e9 |
#ifndef _WIN32
if ((opt = optget(opts, "LocalSocket"))->enabled) { |
2f6591fc |
memset(&server, 0x00, sizeof(server)); |
6df88f29 |
server.sun_family = AF_UNIX; |
288057e9 |
strncpy(server.sun_path, opt->strarg, sizeof(server.sun_path));
server.sun_path[sizeof(server.sun_path) - 1] = '\0';
if ((sockd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
logg("^Clamd was NOT notified: Can't create socket endpoint for %s: %s\n",
opt->strarg, strerror(errno));
optfree(opts); |
6df88f29 |
return -1;
}
|
288057e9 |
if (connect(sockd, (struct sockaddr *)&server,
sizeof(struct sockaddr_un)) < 0) {
logg("^Clamd was NOT notified: Can't connect to clamd through %s: %s\n",
opt->strarg, strerror(errno));
closesocket(sockd);
optfree(opts); |
6df88f29 |
return -11;
}
|
0c58ddd2 |
return sockd;
|
288057e9 |
} else |
17ad1c5b |
#endif |
288057e9 |
if ((opt = optget(opts, "TCPSocket"))->enabled) {
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; |
6df88f29 |
hints.ai_socktype = SOCK_STREAM; |
288057e9 |
hints.ai_flags = AI_PASSIVE; |
19470623 |
|
288057e9 |
snprintf(port, sizeof(port), "%u", (unsigned int)opt->numarg); |
6df88f29 |
port[5] = 0;
|
19470623 |
opt = optget(opts, "TCPAddr");
while (opt) { |
288057e9 |
ret = getaddrinfo(opt->strarg, port, &hints, &res);
if (ret) {
logg("!%s: Can't resolve hostname %s (%s)\n", option,
opt->strarg ? opt->strarg : "",
(ret ==
EAI_SYSTEM)
? strerror(errno)
: gai_strerror(ret)); |
19470623 |
opt = opt->nextarg;
continue;
}
for (p = res; p != NULL; p = p->ai_next) { |
288057e9 |
if ((sockd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0) {
logg("!%s: Can't create TCP socket to connect to %s: %s\n",
option, opt->strarg ? opt->strarg : "localhost", strerror(errno)); |
19470623 |
continue;
}
|
288057e9 |
if (connect(sockd, p->ai_addr, p->ai_addrlen) == -1) {
logg("!%s: Can't connect to clamd on %s:%s: %s\n", option,
opt->strarg ? opt->strarg : "localhost", port, strerror(errno));
closesocket(sockd); |
19470623 |
continue;
}
optfree(opts);
freeaddrinfo(res); |
0c58ddd2 |
|
19470623 |
return sockd;
} |
6df88f29 |
|
288057e9 |
freeaddrinfo(res); |
19470623 |
opt = opt->nextarg; |
6df88f29 |
} |
288057e9 |
} else {
logg("!%s: No communication socket specified in %s\n", option,
cfgfile);
optfree(opts); |
6df88f29 |
return 1; |
e3aaff8e |
}
|
288057e9 |
optfree(opts); |
0c58ddd2 |
return -1; |
3507891f |
}
|
288057e9 |
int notify(const char *cfgfile) |
3507891f |
{ |
6df88f29 |
char buff[20];
int sockd, bread;
|
288057e9 |
if ((sockd = clamd_connect(cfgfile, "NotifyClamd")) < 0) |
6df88f29 |
return 1;
|
288057e9 |
if (sendln(sockd, "RELOAD", 7) < 0) {
logg("!NotifyClamd: Could not write to clamd socket: %s\n", strerror(errno));
closesocket(sockd); |
6df88f29 |
return 1; |
e3aaff8e |
}
|
288057e9 |
memset(buff, 0, sizeof(buff));
if ((bread = recv(sockd, buff, sizeof(buff), 0)) > 0) {
if (!strstr(buff, "RELOADING")) {
logg("!NotifyClamd: Unknown answer from clamd: '%s'\n", buff);
closesocket(sockd); |
0c58ddd2 |
return -1; |
6df88f29 |
} |
3507891f |
} |
e3aaff8e |
|
288057e9 |
closesocket(sockd);
logg("Clamd successfully notified about the update.\n"); |
e3aaff8e |
return 0;
} |
d71dd823 |
#endif |