freshclam/notify.c
e3aaff8e
 /*
81837459
  *  Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net>
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>
17ad1c5b
 #ifdef	HAVE_UNISTD_H
e3aaff8e
 #include <unistd.h>
17ad1c5b
 #endif
e3aaff8e
 #include <sys/types.h>
081f6473
 #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"
fc83da82
 #include "notify.h"
e3aaff8e
 
 int notify(const char *cfgfile)
 {
 	char buff[20];
4cd80898
 #ifndef	_WIN32
e3aaff8e
 	struct sockaddr_un server;
17ad1c5b
 #endif
3027857c
 #ifdef HAVE_GETADDRINFO
b54eb319
 	struct addrinfo hints, *res;
 	char port[6];
 	const char *addr;
 	int ret;
 #else
e3aaff8e
         struct sockaddr_in server2;
a616f2de
 	struct hostent *he;
b54eb319
 #endif
3f7802c9
 	struct optstruct *opts;
 	const struct optstruct *opt;
e3aaff8e
 	int sockd, bread;
c8e620d2
 	const char *socktype;
e3aaff8e
 
 
a68d5e2f
     if((opts = optparse(cfgfile, 0, NULL, 1, OPT_CLAMD, 0, NULL)) == NULL) {
0ae41a2d
 	logg("^Clamd was NOT notified: Can't find or parse configuration file %s\n", cfgfile);
e3aaff8e
 	return 1;
     }
 
4cd80898
 #ifndef	_WIN32
3f7802c9
     if((opt = optget(opts, "LocalSocket"))->enabled) {
c23ac9c2
 	socktype = "UNIX";
e3aaff8e
 	server.sun_family = AF_UNIX;
3f7802c9
 	strncpy(server.sun_path, opt->strarg, sizeof(server.sun_path));
72ce4b70
 	server.sun_path[sizeof(server.sun_path)-1]='\0';
e3aaff8e
 
 	if((sockd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
3f7802c9
 	    logg("^Clamd was NOT notified: Can't create socket endpoint for %s\n", opt->strarg);
e3aaff8e
 	    perror("socket()");
3f7802c9
 	    optfree(opts);
e3aaff8e
 	    return 1;
 	}
 
 	if(connect(sockd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
17ad1c5b
 	    closesocket(sockd);
3f7802c9
 	    logg("^Clamd was NOT notified: Can't connect to clamd through %s\n", opt->strarg);
e3aaff8e
 	    perror("connect()");
3f7802c9
 	    optfree(opts);
e3aaff8e
 	    return 1;
 	}
 
17ad1c5b
     } else
 #endif
3f7802c9
     if((opt = optget(opts, "TCPSocket"))->enabled) {
c23ac9c2
 	socktype = "TCP";
b54eb319
 
3027857c
 #ifdef HAVE_GETADDRINFO
b54eb319
 	memset(&hints, 0, sizeof(hints));
3027857c
 #ifdef SUPPORT_IPv6
b54eb319
 	hints.ai_family = AF_UNSPEC;
3027857c
 #else
 	hints.ai_family = AF_INET;
 #endif
b54eb319
 	hints.ai_socktype = SOCK_STREAM;
593970e0
 	snprintf(port, sizeof(port), "%u", (unsigned int) opt->numarg);
b54eb319
 	port[5] = 0;
 
3f7802c9
 	if((opt = optget(opts, "TCPAddr"))->enabled)
 	    addr = opt->strarg;
b54eb319
 	else
 	    addr = NULL;
 
 	ret = getaddrinfo(addr, port, &hints, &res);
 
 	if(ret) {
6591b483
 	    logg("^Clamd was NOT notified: Can't resolve hostname %s (%s)\n", addr ? addr : "", (ret == EAI_SYSTEM) ? strerror(errno) : gai_strerror(ret));
3f7802c9
 	    optfree(opts);
b54eb319
 	    return 1;
 	}
 
 	if((sockd = socket(res->ai_family, SOCK_STREAM, 0)) < 0) {
 	    perror("socket()");
 	    logg("^Clamd was NOT notified: Can't create TCP socket\n");
3f7802c9
 	    optfree(opts);
b54eb319
 	    freeaddrinfo(res);
 	    return 1;
 	}
 
 	if(connect(sockd, res->ai_addr, res->ai_addrlen) == -1) {
 	    perror("connect()");
 	    closesocket(sockd);
56e4c129
 	    logg("^Clamd was NOT notified: Can't connect to clamd on %s:%s\n", addr ? addr : "localhost", port);
3f7802c9
 	    optfree(opts);
b54eb319
 	    freeaddrinfo(res);
 	    return 1;
 	}
 	freeaddrinfo(res);
 
 #else /* IPv4 */
 
e3aaff8e
 	if((sockd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
0ae41a2d
 	    logg("^Clamd was NOT notified: Can't create TCP socket\n");
e3aaff8e
 	    perror("socket()");
3f7802c9
 	    optfree(opts);
e3aaff8e
 	    return 1;
 	}
 
 	server2.sin_family = AF_INET;
3f7802c9
 	server2.sin_port = htons(opt->numarg);
e3aaff8e
 
3f7802c9
 	if((opt = optget(opts, "TCPAddr"))->enabled) {
 	    if((he = gethostbyname(opt->strarg)) == 0) {
a616f2de
 		perror("gethostbyname()");
3f7802c9
 		logg("^Clamd was NOT notified: Can't resolve hostname '%s'\n", opt->strarg);
 		optfree(opts);
b54eb319
 		closesocket(sockd);
a616f2de
 		return 1;
 	    }
 	    server2.sin_addr = *(struct in_addr *) he->h_addr_list[0];
 	} else
 	    server2.sin_addr.s_addr = inet_addr("127.0.0.1");
 
 
e3aaff8e
 	if(connect(sockd, (struct sockaddr *) &server2, sizeof(struct sockaddr_in)) < 0) {
17ad1c5b
 	    closesocket(sockd);
0ae41a2d
 	    logg("^Clamd was NOT notified: Can't connect to clamd on %s:%d\n",
c23ac9c2
 		    inet_ntoa(server2.sin_addr), ntohs(server2.sin_port));
e3aaff8e
 	    perror("connect()");
3f7802c9
 	    optfree(opts);
e3aaff8e
 	    return 1;
 	}
 
b54eb319
 #endif
 
e3aaff8e
     } else {
0ae41a2d
 	logg("^Clamd was NOT notified: No socket specified in %s\n", cfgfile);
3f7802c9
 	optfree(opts);
e3aaff8e
 	return 1;
     }
 
17ad1c5b
     if(send(sockd, "RELOAD", 6, 0) < 0) {
0ae41a2d
 	logg("^Clamd was NOT notified: Could not write to %s socket\n", socktype);
c23ac9c2
 	perror("write()");
17ad1c5b
 	closesocket(sockd);
3f7802c9
 	optfree(opts);
e3aaff8e
 	return 1;
     }
 
c23ac9c2
     /* TODO: Handle timeout */
e3aaff8e
     memset(buff, 0, sizeof(buff));
17ad1c5b
     if((bread = recv(sockd, buff, sizeof(buff), 0)) > 0)
e3aaff8e
 	if(!strstr(buff, "RELOADING")) {
0ae41a2d
 	    logg("^Clamd was NOT notified: Unknown answer from clamd: '%s'\n", buff);
17ad1c5b
 	    closesocket(sockd);
3f7802c9
 	    optfree(opts);
e3aaff8e
 	    return 1;
 	}
 
17ad1c5b
     closesocket(sockd);
e3aaff8e
     logg("Clamd successfully notified about the update.\n");
3f7802c9
     optfree(opts);
e3aaff8e
     return 0;
 }
d71dd823
 
 #endif