e3aaff8e |
/* |
086eab5c |
* Copyright (C) 2007-2009 Sourcefire, Inc.
*
* Authors: Tomasz Kojm |
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
|
e3aaff8e |
#include <stdio.h>
#include <string.h>
#include <sys/types.h> |
477cfb97 |
#ifndef C_WINDOWS |
e3aaff8e |
#include <sys/socket.h> |
477cfb97 |
#endif |
e3aaff8e |
#include <sys/stat.h> |
477cfb97 |
#ifndef C_WINDOWS |
e3aaff8e |
#include <sys/un.h> |
477cfb97 |
#endif |
e3aaff8e |
#include <errno.h> |
ad3c01bf |
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif |
e3aaff8e |
|
bd8603aa |
#include "libclamav/clamav.h"
|
064b4a0c |
#include "shared/optparser.h" |
bd8603aa |
|
e3aaff8e |
#include "others.h"
#include "server.h" |
afb48b28 |
#include "output.h" |
fc83da82 |
#include "localserver.h" |
e3aaff8e |
|
477cfb97 |
#ifdef C_WINDOWS |
064b4a0c |
int localserver(const struct optstruct *opts) |
67118e92 |
{
logg("!Localserver is not supported on this platform");
return -1;
}
#else
|
064b4a0c |
int localserver(const struct optstruct *opts) |
e3aaff8e |
{
struct sockaddr_un server;
int sockfd, backlog;
struct stat foo;
char *estr;
memset((char *) &server, 0, sizeof(server));
server.sun_family = AF_UNIX; |
064b4a0c |
strncpy(server.sun_path, optget(opts, "LocalSocket")->strarg, sizeof(server.sun_path)); |
72ce4b70 |
server.sun_path[sizeof(server.sun_path)-1]='\0'; |
e3aaff8e |
if((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
estr = strerror(errno); |
e979398c |
logg("!LOCAL: Socket allocation error: %s\n", estr); |
bd8603aa |
return -1; |
e3aaff8e |
}
if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) == -1) { |
049a18b9 |
if(errno == EADDRINUSE) {
if(connect(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) >= 0) { |
e979398c |
logg("!LOCAL: Socket file %s is in use by another process.\n", server.sun_path); |
bd8603aa |
close(sockfd);
return -1; |
049a18b9 |
} |
064b4a0c |
if(optget(opts, "FixStaleSocket")->enabled) { |
e979398c |
logg("#LOCAL: Removing stale socket file %s\n", server.sun_path); |
049a18b9 |
if(unlink(server.sun_path) == -1) {
estr = strerror(errno); |
e979398c |
logg("!LOCAL: Socket file %s could not be removed: %s\n", server.sun_path, estr); |
bd8603aa |
close(sockfd);
return -1; |
049a18b9 |
}
if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) == -1) {
estr = strerror(errno); |
e979398c |
logg("!LOCAL: Socket file %s could not be bound: %s (unlink tried)\n", server.sun_path, estr); |
bd8603aa |
close(sockfd);
return -1; |
049a18b9 |
}
} else if(stat(server.sun_path, &foo) != -1) { |
e979398c |
logg("!LOCAL: Socket file %s exists. Either remove it, or configure a different one.\n", server.sun_path); |
bd8603aa |
close(sockfd);
return -1; |
049a18b9 |
}
} else {
estr = strerror(errno); |
e979398c |
logg("!LOCAL: Socket file %s could not be bound: %s\n", server.sun_path, estr); |
bd8603aa |
close(sockfd);
return -1; |
e3aaff8e |
} |
049a18b9 |
} |
e3aaff8e |
|
e979398c |
logg("#LOCAL: Unix socket file %s\n", server.sun_path); |
e3aaff8e |
|
064b4a0c |
backlog = optget(opts, "MaxConnectionQueueLength")->numarg; |
e979398c |
logg("#LOCAL: Setting connection queue length to %d\n", backlog); |
e3aaff8e |
if(listen(sockfd, backlog) == -1) {
estr = strerror(errno); |
e979398c |
logg("!LOCAL: listen() error: %s\n", estr); |
bd8603aa |
close(sockfd);
return -1; |
e3aaff8e |
}
|
57358cc8 |
return sockfd; |
e3aaff8e |
} |
67118e92 |
#endif /* C_WINDOWS */ |