2003-07-29 15:48:06 +00:00
|
|
|
/*
|
2009-02-13 10:55:45 +00:00
|
|
|
* Copyright (C) 2007-2009 Sourcefire, Inc.
|
|
|
|
|
*
|
|
|
|
|
* Authors: Tomasz Kojm
|
2003-07-29 15:48:06 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-03-31 20:31:04 +00:00
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
|
* published by the Free Software Foundation.
|
2003-07-29 15:48:06 +00:00
|
|
|
*
|
|
|
|
|
* 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>
|
2010-01-28 23:36:37 +01:00
|
|
|
#ifndef _WIN32
|
2003-07-29 15:48:06 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/un.h>
|
2007-02-20 16:58:46 +00:00
|
|
|
#endif
|
2010-01-28 23:36:37 +01:00
|
|
|
#include <sys/stat.h>
|
2003-07-29 15:48:06 +00:00
|
|
|
#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"
|
|
|
|
|
|
2008-12-17 21:42:54 +00:00
|
|
|
#include "shared/optparser.h"
|
2010-01-28 23:36:37 +01:00
|
|
|
#include "shared/output.h"
|
2006-09-05 20:45:39 +00:00
|
|
|
|
2003-07-29 15:48:06 +00:00
|
|
|
#include "others.h"
|
|
|
|
|
#include "server.h"
|
2007-02-11 00:41:13 +00:00
|
|
|
#include "localserver.h"
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2010-01-28 23:36:37 +01:00
|
|
|
#ifdef _WIN32
|
2008-12-17 21:42:54 +00:00
|
|
|
int localserver(const struct optstruct *opts)
|
2006-09-12 20:55:09 +00:00
|
|
|
{
|
|
|
|
|
logg("!Localserver is not supported on this platform");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
2008-12-17 21:42:54 +00:00
|
|
|
int localserver(const struct optstruct *opts)
|
2003-07-29 15:48:06 +00:00
|
|
|
{
|
|
|
|
|
struct sockaddr_un server;
|
|
|
|
|
int sockfd, backlog;
|
2012-07-16 15:36:49 -04:00
|
|
|
STATBUF foo;
|
2003-07-29 15:48:06 +00:00
|
|
|
char *estr;
|
|
|
|
|
|
|
|
|
|
memset((char *) &server, 0, sizeof(server));
|
|
|
|
|
server.sun_family = AF_UNIX;
|
2008-12-17 21:42:54 +00:00
|
|
|
strncpy(server.sun_path, optget(opts, "LocalSocket")->strarg, sizeof(server.sun_path));
|
2008-05-27 16:30:47 +00:00
|
|
|
server.sun_path[sizeof(server.sun_path)-1]='\0';
|
2003-07-29 15:48:06 +00:00
|
|
|
|
|
|
|
|
if((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
|
|
|
|
estr = strerror(errno);
|
2008-02-15 17:37:53 +00:00
|
|
|
logg("!LOCAL: 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) {
|
2008-02-15 17:37:53 +00:00
|
|
|
logg("!LOCAL: 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
|
|
|
}
|
2008-12-17 21:42:54 +00:00
|
|
|
if(optget(opts, "FixStaleSocket")->enabled) {
|
2008-02-15 17:37:53 +00:00
|
|
|
logg("#LOCAL: Removing stale socket file %s\n", server.sun_path);
|
2003-08-29 14:27:15 +00:00
|
|
|
if(unlink(server.sun_path) == -1) {
|
|
|
|
|
estr = strerror(errno);
|
2008-02-15 17:37:53 +00:00
|
|
|
logg("!LOCAL: 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);
|
2008-02-15 17:37:53 +00:00
|
|
|
logg("!LOCAL: 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
|
|
|
}
|
2013-09-25 16:22:18 -04:00
|
|
|
} else if(CLAMSTAT(server.sun_path, &foo) != -1) {
|
2008-02-15 17:37:53 +00:00
|
|
|
logg("!LOCAL: 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);
|
2008-02-15 17:37:53 +00:00
|
|
|
logg("!LOCAL: 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
|
|
|
|
2008-02-15 17:37:53 +00:00
|
|
|
logg("#LOCAL: Unix socket file %s\n", server.sun_path);
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2008-12-17 21:42:54 +00:00
|
|
|
backlog = optget(opts, "MaxConnectionQueueLength")->numarg;
|
2008-02-15 17:37:53 +00:00
|
|
|
logg("#LOCAL: Setting connection queue length to %d\n", backlog);
|
2003-07-29 15:48:06 +00:00
|
|
|
|
|
|
|
|
if(listen(sockfd, backlog) == -1) {
|
|
|
|
|
estr = strerror(errno);
|
2008-02-15 17:37:53 +00:00
|
|
|
logg("!LOCAL: 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
|
|
|
}
|
2010-01-28 23:36:37 +01:00
|
|
|
#endif
|