2003-07-29 15:48:06 +00:00
|
|
|
/*
|
2005-06-01 00:42:33 +00:00
|
|
|
* Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net>
|
2003-07-29 15:48:06 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <errno.h>
|
2006-12-20 15:33:55 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2006-09-05 20:45:39 +00:00
|
|
|
#include "libclamav/clamav.h"
|
|
|
|
|
|
|
|
#include "shared/options.h"
|
|
|
|
#include "shared/cfgparser.h"
|
|
|
|
|
2003-07-29 15:48:06 +00:00
|
|
|
#include "others.h"
|
|
|
|
#include "server.h"
|
2004-03-29 00:00:58 +00:00
|
|
|
#include "output.h"
|
2007-02-11 00:41:13 +00:00
|
|
|
#include "localserver.h"
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2006-09-12 20:55:09 +00:00
|
|
|
#ifdef C_WINDOWS
|
2007-01-30 21:26:43 +00:00
|
|
|
int localserver(const struct cfgstruct *copt)
|
2006-09-12 20:55:09 +00:00
|
|
|
{
|
|
|
|
logg("!Localserver is not supported on this platform");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2006-09-05 20:45:39 +00:00
|
|
|
int localserver(const struct cfgstruct *copt)
|
2003-07-29 15:48:06 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_un server;
|
|
|
|
int sockfd, backlog;
|
|
|
|
struct stat foo;
|
|
|
|
char *estr;
|
|
|
|
|
|
|
|
memset((char *) &server, 0, sizeof(server));
|
|
|
|
server.sun_family = AF_UNIX;
|
2004-04-06 22:43:44 +00:00
|
|
|
strncpy(server.sun_path, cfgopt(copt, "LocalSocket")->strarg, sizeof(server.sun_path));
|
2003-07-29 15:48:06 +00:00
|
|
|
|
|
|
|
if((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
|
|
|
estr = strerror(errno);
|
2003-08-29 14:27:15 +00:00
|
|
|
logg("!Socket allocation error: %s\n", estr);
|
2006-09-05 20:45:39 +00:00
|
|
|
return -1;
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) == -1) {
|
2003-08-29 14:27:15 +00:00
|
|
|
if(errno == EADDRINUSE) {
|
|
|
|
if(connect(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) >= 0) {
|
|
|
|
logg("!Socket file %s is in use by another process.\n", server.sun_path);
|
2006-09-05 20:45:39 +00:00
|
|
|
close(sockfd);
|
|
|
|
return -1;
|
2003-08-29 14:27:15 +00:00
|
|
|
}
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "FixStaleSocket")->enabled) {
|
2003-08-29 14:27:15 +00:00
|
|
|
logg("^Socket file %s exists. Unclean shutdown? Removing...\n", server.sun_path);
|
|
|
|
if(unlink(server.sun_path) == -1) {
|
|
|
|
estr = strerror(errno);
|
|
|
|
logg("!Socket file %s could not be removed: %s\n", server.sun_path, estr);
|
2006-09-05 20:45:39 +00:00
|
|
|
close(sockfd);
|
|
|
|
return -1;
|
2003-08-29 14:27:15 +00:00
|
|
|
}
|
|
|
|
if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) == -1) {
|
|
|
|
estr = strerror(errno);
|
|
|
|
logg("!Socket file %s could not be bound: %s (unlink tried)\n", server.sun_path, estr);
|
2006-09-05 20:45:39 +00:00
|
|
|
close(sockfd);
|
|
|
|
return -1;
|
2003-08-29 14:27:15 +00:00
|
|
|
}
|
|
|
|
} else if(stat(server.sun_path, &foo) != -1) {
|
|
|
|
logg("!Socket file %s exists. Either remove it, or configure a different one.\n", server.sun_path);
|
2006-09-05 20:45:39 +00:00
|
|
|
close(sockfd);
|
|
|
|
return -1;
|
2003-08-29 14:27:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
estr = strerror(errno);
|
|
|
|
logg("!Socket file %s could not be bound: %s\n", server.sun_path, estr);
|
2006-09-05 20:45:39 +00:00
|
|
|
close(sockfd);
|
|
|
|
return -1;
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
2003-08-29 14:27:15 +00:00
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2003-08-29 14:27:15 +00:00
|
|
|
logg("Unix socket file %s\n", server.sun_path);
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
backlog = cfgopt(copt, "MaxConnectionQueueLength")->numarg;
|
2003-07-29 15:48:06 +00:00
|
|
|
logg("Setting connection queue length to %d\n", backlog);
|
|
|
|
|
|
|
|
if(listen(sockfd, backlog) == -1) {
|
|
|
|
estr = strerror(errno);
|
|
|
|
logg("!listen() error: %s\n", estr);
|
2006-09-05 20:45:39 +00:00
|
|
|
close(sockfd);
|
|
|
|
return -1;
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
|
|
|
|
2005-06-22 15:54:28 +00:00
|
|
|
return sockfd;
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif /* C_WINDOWS */
|