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
  */
17ad1c5b
 #ifdef        _MSC_VER
 #include <windows.h>
 #include <winsock.h>
 #endif
 
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>
17ad1c5b
 #ifndef	C_WINDOWS
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>
e3aaff8e
 
a889f40e
 #include "shared/cfgparser.h"
 #include "shared/output.h"
fc83da82
 #include "notify.h"
e3aaff8e
 
17ad1c5b
 #ifndef	C_WINDOWS
 #define	closesocket(s)	close(s)
 #endif
 
e3aaff8e
 int notify(const char *cfgfile)
 {
 	char buff[20];
17ad1c5b
 #ifndef	C_WINDOWS
e3aaff8e
 	struct sockaddr_un server;
17ad1c5b
 #endif
e3aaff8e
         struct sockaddr_in server2;
a616f2de
 	struct hostent *he;
c8e620d2
 	struct cfgstruct *copt;
 	const struct cfgstruct *cpt;
e3aaff8e
 	int sockd, bread;
c8e620d2
 	const char *socktype;
e3aaff8e
 
 
81837459
     if((copt = getcfg(cfgfile, 1)) == NULL) {
0ae41a2d
 	logg("^Clamd was NOT notified: Can't find or parse configuration file %s\n", cfgfile);
e3aaff8e
 	return 1;
     }
 
17ad1c5b
 #ifndef	C_WINDOWS
57358cc8
     if((cpt = cfgopt(copt, "LocalSocket"))->enabled) {
c23ac9c2
 	socktype = "UNIX";
e3aaff8e
 	server.sun_family = AF_UNIX;
658f19f8
 	strncpy(server.sun_path, cpt->strarg, sizeof(server.sun_path));
e3aaff8e
 
 	if((sockd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
0ae41a2d
 	    logg("^Clamd was NOT notified: Can't create socket endpoint for %s\n", cpt->strarg);
e3aaff8e
 	    perror("socket()");
c8e620d2
 	    freecfg(copt);
e3aaff8e
 	    return 1;
 	}
 
 	if(connect(sockd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
17ad1c5b
 	    closesocket(sockd);
0ae41a2d
 	    logg("^Clamd was NOT notified: Can't connect to clamd through %s\n", cpt->strarg);
e3aaff8e
 	    perror("connect()");
c8e620d2
 	    freecfg(copt);
e3aaff8e
 	    return 1;
 	}
 
17ad1c5b
     } else
 #endif
     if((cpt = cfgopt(copt, "TCPSocket"))->enabled) {
e3aaff8e
 
c23ac9c2
 	socktype = "TCP";
e3aaff8e
 #ifdef PF_INET
 	if((sockd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
 #else
 	if((sockd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
 #endif
0ae41a2d
 	    logg("^Clamd was NOT notified: Can't create TCP socket\n");
e3aaff8e
 	    perror("socket()");
c8e620d2
 	    freecfg(copt);
e3aaff8e
 	    return 1;
 	}
 
 	server2.sin_family = AF_INET;
 	server2.sin_port = htons(cpt->numarg);
 
81837459
 	if((cpt = cfgopt(copt, "TCPAddr"))->enabled) {
a616f2de
 	    if ((he = gethostbyname(cpt->strarg)) == 0) {
 		perror("gethostbyname()");
0ae41a2d
 		logg("^Clamd was NOT notified: Can't resolve hostname '%s'\n", cpt->strarg);
c8e620d2
 		freecfg(copt);
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()");
c8e620d2
 	    freecfg(copt);
e3aaff8e
 	    return 1;
 	}
 
     } else {
0ae41a2d
 	logg("^Clamd was NOT notified: No socket specified in %s\n", cfgfile);
c8e620d2
 	freecfg(copt);
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);
c8e620d2
 	freecfg(copt);
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);
c8e620d2
 	    freecfg(copt);
e3aaff8e
 	    return 1;
 	}
 
17ad1c5b
     closesocket(sockd);
e3aaff8e
     logg("Clamd successfully notified about the update.\n");
c8e620d2
     freecfg(copt);
e3aaff8e
     return 0;
 }
d71dd823
 
 #endif