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>
|
2004-02-17 00:08:25 +00:00
|
|
|
* Trog <trog@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
|
|
|
*/
|
|
|
|
|
2006-09-12 20:55:09 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <winsock.h>
|
|
|
|
#endif
|
|
|
|
|
2006-09-13 22:06:13 +00:00
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
#include "clamav-config.h"
|
|
|
|
#endif
|
|
|
|
|
2003-07-29 15:48:06 +00:00
|
|
|
#include <pthread.h>
|
2004-02-17 00:08:25 +00:00
|
|
|
#include <errno.h>
|
2003-07-29 15:48:06 +00:00
|
|
|
#include <signal.h>
|
2004-02-17 00:08:25 +00:00
|
|
|
#include <stdio.h>
|
2004-02-20 15:49:29 +00:00
|
|
|
#include <string.h>
|
2004-02-17 00:08:25 +00:00
|
|
|
#include <time.h>
|
2004-02-20 15:49:29 +00:00
|
|
|
#include <sys/types.h>
|
2006-09-12 20:55:09 +00:00
|
|
|
#ifndef C_WINDOWS
|
2004-02-20 15:49:29 +00:00
|
|
|
#include <sys/socket.h>
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-03-01 13:15:55 +00:00
|
|
|
#include <unistd.h>
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif
|
2006-09-05 20:45:39 +00:00
|
|
|
|
|
|
|
#include "libclamav/clamav.h"
|
|
|
|
|
|
|
|
#include "shared/memory.h"
|
|
|
|
#include "shared/output.h"
|
2003-07-29 15:48:06 +00:00
|
|
|
|
|
|
|
#include "server.h"
|
2004-02-17 00:08:25 +00:00
|
|
|
#include "thrmgr.h"
|
2004-01-20 10:37:54 +00:00
|
|
|
#include "session.h"
|
2004-02-17 00:08:25 +00:00
|
|
|
#include "clamuko.h"
|
|
|
|
#include "others.h"
|
2004-03-29 00:00:58 +00:00
|
|
|
#include "shared.h"
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2006-09-12 20:55:09 +00:00
|
|
|
#ifndef C_WINDOWS
|
|
|
|
#define closesocket(s) close(s)
|
|
|
|
#endif
|
|
|
|
|
2004-02-17 00:08:25 +00:00
|
|
|
#define BUFFSIZE 1024
|
2006-09-12 20:55:09 +00:00
|
|
|
#ifndef FALSE
|
2004-02-17 00:08:25 +00:00
|
|
|
#define FALSE (0)
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif
|
|
|
|
#ifndef TRUE
|
2004-02-17 00:08:25 +00:00
|
|
|
#define TRUE (1)
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2005-06-23 14:00:08 +00:00
|
|
|
int progexit = 0;
|
|
|
|
pthread_mutex_t exit_mutex;
|
|
|
|
int reload = 0;
|
|
|
|
time_t reloaded_time = 0;
|
|
|
|
pthread_mutex_t reload_mutex;
|
|
|
|
int sighup = 0;
|
2006-09-14 22:43:31 +00:00
|
|
|
static struct cl_stat *dbstat = NULL;
|
2004-01-23 11:06:00 +00:00
|
|
|
|
2004-02-17 00:08:25 +00:00
|
|
|
typedef struct client_conn_tag {
|
|
|
|
int sd;
|
|
|
|
int options;
|
|
|
|
const struct cfgstruct *copt;
|
2005-03-07 00:34:04 +00:00
|
|
|
struct cl_node *root;
|
|
|
|
time_t root_timestamp;
|
2004-02-17 00:08:25 +00:00
|
|
|
const struct cl_limits *limits;
|
2005-06-22 15:54:28 +00:00
|
|
|
int *socketds;
|
|
|
|
int nsockets;
|
2004-02-17 00:08:25 +00:00
|
|
|
} client_conn_t;
|
2004-01-20 10:37:54 +00:00
|
|
|
|
2004-02-17 00:08:25 +00:00
|
|
|
void scanner_thread(void *arg)
|
2003-07-29 15:48:06 +00:00
|
|
|
{
|
2004-02-17 00:08:25 +00:00
|
|
|
client_conn_t *conn = (client_conn_t *) arg;
|
2006-09-12 20:55:09 +00:00
|
|
|
#ifndef C_WINDOWS
|
2003-07-29 15:48:06 +00:00
|
|
|
sigset_t sigset;
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif
|
2005-06-22 15:54:28 +00:00
|
|
|
int ret, timeout, i, session=FALSE;
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2004-02-17 00:08:25 +00:00
|
|
|
|
2006-09-12 20:55:09 +00:00
|
|
|
#ifndef C_WINDOWS
|
2003-07-29 15:48:06 +00:00
|
|
|
/* ignore all signals */
|
|
|
|
sigfillset(&sigset);
|
|
|
|
pthread_sigmask(SIG_SETMASK, &sigset, NULL);
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
timeout = cfgopt(conn->copt, "ReadTimeout")->numarg;
|
2004-09-15 21:12:26 +00:00
|
|
|
if(!timeout)
|
|
|
|
timeout = -1;
|
|
|
|
|
2005-01-26 17:28:36 +00:00
|
|
|
do {
|
|
|
|
ret = command(conn->sd, conn->root, conn->limits, conn->options, conn->copt, timeout);
|
2005-11-03 22:05:35 +00:00
|
|
|
if (ret < 0) {
|
2005-01-26 17:28:36 +00:00
|
|
|
break;
|
2004-02-17 00:08:25 +00:00
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2005-01-26 17:28:36 +00:00
|
|
|
switch(ret) {
|
|
|
|
case COMMAND_SHUTDOWN:
|
|
|
|
pthread_mutex_lock(&exit_mutex);
|
|
|
|
progexit = 1;
|
2005-06-22 15:54:28 +00:00
|
|
|
for(i = 0; i < conn->nsockets; i++) {
|
|
|
|
shutdown(conn->socketds[i], 2);
|
2006-09-12 20:55:09 +00:00
|
|
|
closesocket(conn->socketds[i]);
|
2005-06-22 15:54:28 +00:00
|
|
|
}
|
2005-01-26 17:28:36 +00:00
|
|
|
pthread_mutex_unlock(&exit_mutex);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COMMAND_RELOAD:
|
|
|
|
pthread_mutex_lock(&reload_mutex);
|
|
|
|
reload = 1;
|
|
|
|
pthread_mutex_unlock(&reload_mutex);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COMMAND_SESSION:
|
2005-06-23 14:00:08 +00:00
|
|
|
session = TRUE;
|
2005-01-26 17:28:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case COMMAND_END:
|
2005-06-23 14:00:08 +00:00
|
|
|
session = FALSE;
|
2005-01-26 17:28:36 +00:00
|
|
|
break;
|
2005-01-26 18:23:38 +00:00
|
|
|
}
|
|
|
|
if (session) {
|
|
|
|
pthread_mutex_lock(&exit_mutex);
|
|
|
|
if(progexit) {
|
|
|
|
session = FALSE;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&exit_mutex);
|
|
|
|
pthread_mutex_lock(&reload_mutex);
|
2005-03-07 00:34:04 +00:00
|
|
|
if (conn->root_timestamp != reloaded_time) {
|
2005-01-26 18:23:38 +00:00
|
|
|
session = FALSE;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&reload_mutex);
|
|
|
|
}
|
2005-01-26 17:28:36 +00:00
|
|
|
} while (session);
|
|
|
|
|
2006-09-12 20:55:09 +00:00
|
|
|
closesocket(conn->sd);
|
2005-03-07 00:34:04 +00:00
|
|
|
cl_free(conn->root);
|
2004-02-17 00:08:25 +00:00
|
|
|
free(conn);
|
|
|
|
return;
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
|
|
|
|
2004-02-17 00:08:25 +00:00
|
|
|
void sighandler_th(int sig)
|
2003-07-29 15:48:06 +00:00
|
|
|
{
|
2004-02-17 00:08:25 +00:00
|
|
|
switch(sig) {
|
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
progexit = 1;
|
2004-03-01 13:15:55 +00:00
|
|
|
break;
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2006-09-12 20:55:09 +00:00
|
|
|
#ifdef SIGHUP
|
2004-03-01 13:15:55 +00:00
|
|
|
case SIGHUP:
|
|
|
|
sighup = 1;
|
|
|
|
break;
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif
|
2004-03-02 12:58:04 +00:00
|
|
|
|
2006-09-12 20:55:09 +00:00
|
|
|
#ifdef SIGUSR2
|
2004-03-13 12:50:39 +00:00
|
|
|
case SIGUSR2:
|
|
|
|
reload = 1;
|
|
|
|
break;
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif
|
2004-03-13 12:50:39 +00:00
|
|
|
|
2004-03-02 12:58:04 +00:00
|
|
|
default:
|
|
|
|
break; /* Take no action on other signals - e.g. SIGPIPE */
|
2004-02-17 00:08:25 +00:00
|
|
|
}
|
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2006-09-14 22:43:31 +00:00
|
|
|
static struct cl_node *reload_db(struct cl_node *root, unsigned int dboptions, const struct cfgstruct *copt, int do_check, int *ret)
|
2004-02-17 00:08:25 +00:00
|
|
|
{
|
2004-03-14 20:45:58 +00:00
|
|
|
const char *dbdir;
|
2006-09-05 20:45:39 +00:00
|
|
|
int retval;
|
|
|
|
unsigned int sigs = 0;
|
2003-12-01 19:28:40 +00:00
|
|
|
|
2006-09-14 22:43:31 +00:00
|
|
|
*ret = 0;
|
2004-02-17 00:08:25 +00:00
|
|
|
if(do_check) {
|
|
|
|
if(dbstat == NULL) {
|
|
|
|
logg("No stats for Database check - forcing reload\n");
|
|
|
|
return root;
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
|
|
|
|
2004-02-17 00:08:25 +00:00
|
|
|
if(cl_statchkdir(dbstat) == 1) {
|
|
|
|
logg("SelfCheck: Database modification detected. Forcing reload.\n");
|
|
|
|
return root;
|
|
|
|
} else {
|
|
|
|
logg("SelfCheck: Database status OK.\n");
|
|
|
|
return NULL;
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
2004-02-17 00:08:25 +00:00
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2004-02-17 00:08:25 +00:00
|
|
|
/* release old structure */
|
|
|
|
if(root) {
|
2004-07-19 17:54:40 +00:00
|
|
|
cl_free(root);
|
2004-02-17 00:08:25 +00:00
|
|
|
root = NULL;
|
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
dbdir = cfgopt(copt, "DatabaseDirectory")->strarg;
|
2004-02-17 00:08:25 +00:00
|
|
|
logg("Reading databases from %s\n", dbdir);
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2004-02-17 00:08:25 +00:00
|
|
|
if(dbstat == NULL) {
|
|
|
|
dbstat = (struct cl_stat *) mmalloc(sizeof(struct cl_stat));
|
2006-09-14 22:43:31 +00:00
|
|
|
if(!dbstat) {
|
|
|
|
logg("!Can't allocate memory for dbstat\n");
|
|
|
|
*ret = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-02-17 00:08:25 +00:00
|
|
|
} else {
|
|
|
|
cl_statfree(dbstat);
|
|
|
|
}
|
2003-12-12 17:48:47 +00:00
|
|
|
|
2004-02-17 00:08:25 +00:00
|
|
|
memset(dbstat, 0, sizeof(struct cl_stat));
|
|
|
|
cl_statinidir(dbdir, dbstat);
|
2005-11-13 23:38:41 +00:00
|
|
|
|
2006-09-05 20:45:39 +00:00
|
|
|
if((retval = cl_load(dbdir, &root, &sigs, dboptions))) {
|
2004-02-17 00:08:25 +00:00
|
|
|
logg("!reload db failed: %s\n", cl_strerror(retval));
|
2006-09-14 22:43:31 +00:00
|
|
|
*ret = 1;
|
|
|
|
return NULL;
|
2004-02-17 00:08:25 +00:00
|
|
|
}
|
2003-12-12 17:48:47 +00:00
|
|
|
|
2004-02-17 00:08:25 +00:00
|
|
|
if(!root) {
|
2006-09-14 22:43:31 +00:00
|
|
|
logg("!reload db failed: %s\n", cl_strerror(retval));
|
|
|
|
*ret = 1;
|
|
|
|
return NULL;
|
2004-02-17 00:08:25 +00:00
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2004-07-19 17:54:40 +00:00
|
|
|
if((retval = cl_build(root)) != 0) {
|
2006-09-14 22:43:31 +00:00
|
|
|
logg("!Database initialization error: can't build engine: %s\n", cl_strerror(retval));
|
|
|
|
*ret = 1;
|
|
|
|
return NULL;
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
2006-09-05 20:45:39 +00:00
|
|
|
logg("Database correctly reloaded (%d signatures)\n", sigs);
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2004-02-17 00:08:25 +00:00
|
|
|
return root;
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
|
|
|
|
2006-09-14 19:08:59 +00:00
|
|
|
int acceptloop_th(int *socketds, int nsockets, struct cl_node *root, unsigned int dboptions, const struct cfgstruct *copt)
|
2003-07-29 15:48:06 +00:00
|
|
|
{
|
2006-09-14 22:45:57 +00:00
|
|
|
int new_sd, max_threads, i, ret = 0;
|
2004-09-27 20:53:24 +00:00
|
|
|
unsigned int options = 0;
|
2004-02-25 11:07:00 +00:00
|
|
|
threadpool_t *thr_pool;
|
2006-09-12 20:55:09 +00:00
|
|
|
#ifndef C_WINDOWS
|
2004-02-17 00:08:25 +00:00
|
|
|
struct sigaction sigact;
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif
|
2004-02-17 00:08:25 +00:00
|
|
|
mode_t old_umask;
|
2003-07-29 15:48:06 +00:00
|
|
|
struct cl_limits limits;
|
2006-09-12 20:55:09 +00:00
|
|
|
#ifndef C_WINDOWS
|
2003-07-29 15:48:06 +00:00
|
|
|
sigset_t sigset;
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif
|
2004-02-17 00:08:25 +00:00
|
|
|
client_conn_t *client_conn;
|
|
|
|
struct cfgstruct *cpt;
|
2004-03-10 00:37:55 +00:00
|
|
|
#ifdef HAVE_STRERROR_R
|
|
|
|
char buff[BUFFSIZE + 1];
|
|
|
|
#endif
|
2004-02-17 00:08:25 +00:00
|
|
|
unsigned int selfchk;
|
|
|
|
time_t start_time, current_time;
|
2004-03-07 23:34:29 +00:00
|
|
|
pid_t mainpid;
|
2004-10-01 00:31:18 +00:00
|
|
|
int idletimeout;
|
2004-03-07 23:34:29 +00:00
|
|
|
|
2003-07-29 15:48:06 +00:00
|
|
|
#if defined(C_BIGSTACK) || defined(C_BSD)
|
2004-02-17 00:08:25 +00:00
|
|
|
size_t stacksize;
|
2003-07-29 15:48:06 +00:00
|
|
|
#endif
|
|
|
|
|
2004-02-17 00:08:25 +00:00
|
|
|
#ifdef CLAMUKO
|
|
|
|
pthread_t clamuko_pid;
|
|
|
|
pthread_attr_t clamuko_attr;
|
2004-03-29 00:00:58 +00:00
|
|
|
struct thrarg *tharg = NULL; /* shut up gcc */
|
2004-02-17 00:08:25 +00:00
|
|
|
#endif
|
2006-09-12 20:55:09 +00:00
|
|
|
|
|
|
|
#ifndef C_WINDOWS
|
2004-02-17 00:08:25 +00:00
|
|
|
memset(&sigact, 0, sizeof(struct sigaction));
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif
|
2003-07-29 15:48:06 +00:00
|
|
|
|
|
|
|
/* save the PID */
|
2004-03-07 23:34:29 +00:00
|
|
|
mainpid = getpid();
|
2005-06-01 00:42:33 +00:00
|
|
|
if((cpt = cfgopt(copt, "PidFile"))->enabled) {
|
2003-07-29 15:48:06 +00:00
|
|
|
FILE *fd;
|
2004-02-17 00:08:25 +00:00
|
|
|
old_umask = umask(0006);
|
2003-07-29 15:48:06 +00:00
|
|
|
if((fd = fopen(cpt->strarg, "w")) == NULL) {
|
|
|
|
logg("!Can't save PID in file %s\n", cpt->strarg);
|
|
|
|
} else {
|
2004-03-07 23:34:29 +00:00
|
|
|
fprintf(fd, "%d", (int) mainpid);
|
2003-07-29 15:48:06 +00:00
|
|
|
fclose(fd);
|
|
|
|
}
|
|
|
|
umask(old_umask);
|
|
|
|
}
|
|
|
|
|
|
|
|
logg("*Listening daemon: PID: %d\n", getpid());
|
2005-06-01 00:42:33 +00:00
|
|
|
max_threads = cfgopt(copt, "MaxThreads")->numarg;
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2006-09-14 18:55:36 +00:00
|
|
|
#ifdef CL_EXPERIMENTAL
|
2006-12-20 01:23:50 +00:00
|
|
|
if(cfgopt(copt,"PhishingScanURLs")->enabled)
|
|
|
|
dboptions |= CL_DB_PHISHING_URLS;
|
2006-10-15 14:16:05 +00:00
|
|
|
if(cfgopt(copt,"PhishingStrictURLCheck")->enabled)
|
2006-12-20 01:23:50 +00:00
|
|
|
options |= CL_SCAN_PHISHING_DOMAINLIST;
|
2006-09-14 18:55:36 +00:00
|
|
|
#endif
|
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "ScanArchive")->enabled || cfgopt(copt, "ClamukoScanArchive")->enabled) {
|
2003-07-29 15:48:06 +00:00
|
|
|
|
|
|
|
/* set up limits */
|
2003-11-16 01:37:00 +00:00
|
|
|
memset(&limits, 0, sizeof(struct cl_limits));
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if((limits.maxfilesize = cfgopt(copt, "ArchiveMaxFileSize")->numarg)) {
|
2004-09-27 20:53:24 +00:00
|
|
|
logg("Archive: Archived file size limit set to %d bytes.\n", limits.maxfilesize);
|
2005-06-01 00:42:33 +00:00
|
|
|
} else {
|
|
|
|
logg("^Archive: File size limit protection disabled.\n");
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if((limits.maxreclevel = cfgopt(copt, "ArchiveMaxRecursion")->numarg)) {
|
2004-09-27 20:53:24 +00:00
|
|
|
logg("Archive: Recursion level limit set to %d.\n", limits.maxreclevel);
|
2005-06-01 00:42:33 +00:00
|
|
|
} else {
|
|
|
|
logg("^Archive: Recursion level limit protection disabled.\n");
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if((limits.maxfiles = cfgopt(copt, "ArchiveMaxFiles")->numarg)) {
|
2004-09-27 20:53:24 +00:00
|
|
|
logg("Archive: Files limit set to %d.\n", limits.maxfiles);
|
2005-06-01 00:42:33 +00:00
|
|
|
} else {
|
|
|
|
logg("^Archive: Files limit protection disabled.\n");
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if((limits.maxratio = cfgopt(copt, "ArchiveMaxCompressionRatio")->numarg)) {
|
2004-09-27 20:53:24 +00:00
|
|
|
logg("Archive: Compression ratio limit set to %d.\n", limits.maxratio);
|
2005-06-01 00:42:33 +00:00
|
|
|
} else {
|
|
|
|
logg("^Archive: Compression ratio limit disabled.\n");
|
2003-12-01 22:55:35 +00:00
|
|
|
}
|
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "ArchiveLimitMemoryUsage")->enabled) {
|
2003-07-29 15:48:06 +00:00
|
|
|
limits.archivememlim = 1;
|
2003-12-01 19:28:40 +00:00
|
|
|
logg("Archive: Limited memory usage.\n");
|
2004-02-17 00:08:25 +00:00
|
|
|
} else {
|
2003-07-29 15:48:06 +00:00
|
|
|
limits.archivememlim = 0;
|
2004-02-17 00:08:25 +00:00
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "ScanArchive")->enabled) {
|
2003-07-29 15:48:06 +00:00
|
|
|
logg("Archive support enabled.\n");
|
2004-09-13 16:44:01 +00:00
|
|
|
options |= CL_SCAN_ARCHIVE;
|
2003-09-29 11:44:52 +00:00
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "ArchiveBlockEncrypted")->enabled) {
|
2004-09-13 01:43:57 +00:00
|
|
|
logg("Archive: Blocking encrypted archives.\n");
|
2004-09-18 19:26:08 +00:00
|
|
|
options |= CL_SCAN_BLOCKENCRYPTED;
|
2004-03-04 02:27:37 +00:00
|
|
|
}
|
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "ArchiveBlockMax")->enabled) {
|
2004-09-13 01:43:57 +00:00
|
|
|
logg("Archive: Blocking archives that exceed limits.\n");
|
2004-09-13 16:44:01 +00:00
|
|
|
options |= CL_SCAN_BLOCKMAX;
|
2004-09-13 01:43:57 +00:00
|
|
|
}
|
|
|
|
|
2003-07-29 15:48:06 +00:00
|
|
|
} else {
|
|
|
|
logg("Archive support disabled.\n");
|
|
|
|
}
|
|
|
|
|
2006-05-12 18:07:19 +00:00
|
|
|
if(cfgopt(copt, "AlgorithmicDetection")->enabled) {
|
2005-12-12 18:44:37 +00:00
|
|
|
logg("Algorithmic detection enabled.\n");
|
|
|
|
options |= CL_SCAN_ALGO;
|
|
|
|
} else {
|
|
|
|
logg("Algorithmic detection disabled.\n");
|
|
|
|
}
|
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "ScanPE")->enabled) {
|
2004-07-05 23:50:55 +00:00
|
|
|
logg("Portable Executable support enabled.\n");
|
2004-09-13 16:44:01 +00:00
|
|
|
options |= CL_SCAN_PE;
|
2006-10-28 22:01:51 +00:00
|
|
|
} else {
|
|
|
|
logg("Portable Executable support disabled.\n");
|
|
|
|
}
|
2004-08-04 20:11:18 +00:00
|
|
|
|
2006-10-28 22:01:51 +00:00
|
|
|
if(cfgopt(copt, "ScanELF")->enabled) {
|
|
|
|
logg("ELF support enabled.\n");
|
|
|
|
options |= CL_SCAN_ELF;
|
|
|
|
} else {
|
|
|
|
logg("ELF support disabled.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cfgopt(copt, "ScanPE")->enabled || cfgopt(copt, "ScanELF")->enabled) {
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "DetectBrokenExecutables")->enabled) {
|
2004-08-04 20:11:18 +00:00
|
|
|
logg("Detection of broken executables enabled.\n");
|
2004-09-19 19:23:37 +00:00
|
|
|
options |= CL_SCAN_BLOCKBROKEN;
|
2004-08-04 20:11:18 +00:00
|
|
|
}
|
2004-07-05 23:50:55 +00:00
|
|
|
}
|
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "ScanMail")->enabled) {
|
2003-07-29 15:48:06 +00:00
|
|
|
logg("Mail files support enabled.\n");
|
2004-09-13 16:44:01 +00:00
|
|
|
options |= CL_SCAN_MAIL;
|
2004-08-18 15:22:48 +00:00
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "MailFollowURLs")->enabled) {
|
2004-08-18 15:22:48 +00:00
|
|
|
logg("Mail: URL scanning enabled.\n");
|
2004-09-13 16:44:01 +00:00
|
|
|
options |= CL_SCAN_MAILURL;
|
2004-08-18 15:22:48 +00:00
|
|
|
}
|
|
|
|
|
2006-12-12 14:00:13 +00:00
|
|
|
if((limits.maxmailrec = cfgopt(copt, "MailMaxRecursion")->numarg)) {
|
|
|
|
logg("Mail: Recursion level limit set to %u.\n", limits.maxmailrec);
|
|
|
|
} else {
|
|
|
|
logg("^Mail: Recursion level limit protection disabled.\n");
|
|
|
|
}
|
|
|
|
|
2003-07-29 15:48:06 +00:00
|
|
|
} else {
|
|
|
|
logg("Mail files support disabled.\n");
|
|
|
|
}
|
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "ScanOLE2")->enabled) {
|
2004-01-23 11:06:00 +00:00
|
|
|
logg("OLE2 support enabled.\n");
|
2004-09-13 16:44:01 +00:00
|
|
|
options |= CL_SCAN_OLE2;
|
2004-01-23 11:06:00 +00:00
|
|
|
} else {
|
|
|
|
logg("OLE2 support disabled.\n");
|
|
|
|
}
|
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "ScanHTML")->enabled) {
|
2004-07-02 23:00:58 +00:00
|
|
|
logg("HTML support enabled.\n");
|
2004-09-13 16:44:01 +00:00
|
|
|
options |= CL_SCAN_HTML;
|
2004-07-02 23:00:58 +00:00
|
|
|
} else {
|
|
|
|
logg("HTML support disabled.\n");
|
|
|
|
}
|
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
selfchk = cfgopt(copt, "SelfCheck")->numarg;
|
2004-02-17 00:08:25 +00:00
|
|
|
if(!selfchk) {
|
|
|
|
logg("Self checking disabled.\n");
|
|
|
|
} else {
|
|
|
|
logg("Self checking every %d seconds.\n", selfchk);
|
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "ClamukoScanOnAccess")->enabled)
|
2003-07-29 15:48:06 +00:00
|
|
|
#ifdef CLAMUKO
|
2004-02-29 17:29:09 +00:00
|
|
|
{
|
|
|
|
pthread_attr_init(&clamuko_attr);
|
|
|
|
pthread_attr_setdetachstate(&clamuko_attr, PTHREAD_CREATE_JOINABLE);
|
2004-02-17 00:08:25 +00:00
|
|
|
|
2004-02-29 17:29:09 +00:00
|
|
|
tharg = (struct thrarg *) mmalloc(sizeof(struct thrarg));
|
|
|
|
tharg->copt = copt;
|
|
|
|
tharg->root = root;
|
|
|
|
tharg->limits = &limits;
|
|
|
|
tharg->options = options;
|
2004-02-17 00:08:25 +00:00
|
|
|
|
2004-02-29 17:29:09 +00:00
|
|
|
pthread_create(&clamuko_pid, &clamuko_attr, clamukoth, tharg);
|
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
#else
|
2004-03-01 13:01:49 +00:00
|
|
|
logg("Clamuko is not available.\n");
|
2003-07-29 15:48:06 +00:00
|
|
|
#endif
|
|
|
|
|
2006-09-12 20:55:09 +00:00
|
|
|
#ifndef C_WINDOWS
|
2003-07-29 15:48:06 +00:00
|
|
|
/* set up signal handling */
|
|
|
|
sigfillset(&sigset);
|
|
|
|
sigdelset(&sigset, SIGINT);
|
|
|
|
sigdelset(&sigset, SIGTERM);
|
2003-09-05 13:10:21 +00:00
|
|
|
sigdelset(&sigset, SIGSEGV);
|
2003-09-14 18:55:56 +00:00
|
|
|
sigdelset(&sigset, SIGHUP);
|
2004-03-02 12:58:04 +00:00
|
|
|
sigdelset(&sigset, SIGPIPE);
|
2004-03-13 12:50:39 +00:00
|
|
|
sigdelset(&sigset, SIGUSR2);
|
2003-07-29 15:48:06 +00:00
|
|
|
sigprocmask(SIG_SETMASK, &sigset, NULL);
|
2004-02-17 00:08:25 +00:00
|
|
|
|
2003-09-05 13:10:21 +00:00
|
|
|
/* SIGINT, SIGTERM, SIGSEGV */
|
2003-11-29 03:38:55 +00:00
|
|
|
sigact.sa_handler = sighandler_th;
|
2003-07-29 15:48:06 +00:00
|
|
|
sigemptyset(&sigact.sa_mask);
|
|
|
|
sigaddset(&sigact.sa_mask, SIGINT);
|
|
|
|
sigaddset(&sigact.sa_mask, SIGTERM);
|
2003-09-14 18:55:56 +00:00
|
|
|
sigaddset(&sigact.sa_mask, SIGHUP);
|
2004-03-02 12:58:04 +00:00
|
|
|
sigaddset(&sigact.sa_mask, SIGPIPE);
|
2004-03-13 12:50:39 +00:00
|
|
|
sigaddset(&sigact.sa_mask, SIGUSR2);
|
2003-07-29 15:48:06 +00:00
|
|
|
sigaction(SIGINT, &sigact, NULL);
|
|
|
|
sigaction(SIGTERM, &sigact, NULL);
|
2004-03-02 12:58:04 +00:00
|
|
|
sigaction(SIGHUP, &sigact, NULL);
|
|
|
|
sigaction(SIGPIPE, &sigact, NULL);
|
2004-03-13 12:50:39 +00:00
|
|
|
sigaction(SIGUSR2, &sigact, NULL);
|
|
|
|
|
|
|
|
if(!debug_mode) {
|
|
|
|
sigaddset(&sigact.sa_mask, SIGHUP);
|
2004-02-17 00:08:25 +00:00
|
|
|
sigaction(SIGSEGV, &sigact, NULL);
|
2004-03-13 12:50:39 +00:00
|
|
|
}
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif
|
2004-02-17 00:08:25 +00:00
|
|
|
|
|
|
|
pthread_mutex_init(&exit_mutex, NULL);
|
|
|
|
pthread_mutex_init(&reload_mutex, NULL);
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
idletimeout = cfgopt(copt, "IdleTimeout")->numarg;
|
2004-10-01 00:31:18 +00:00
|
|
|
|
2005-06-23 14:00:08 +00:00
|
|
|
if((thr_pool=thrmgr_new(max_threads, idletimeout, scanner_thread)) == NULL) {
|
2004-03-19 20:38:55 +00:00
|
|
|
logg("!thrmgr_new failed\n");
|
2004-02-17 00:08:25 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2004-02-17 00:08:25 +00:00
|
|
|
time(&start_time);
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2005-06-23 14:00:08 +00:00
|
|
|
for(;;) {
|
2005-06-22 15:54:28 +00:00
|
|
|
int socketd = socketds[0];
|
|
|
|
if(nsockets > 1) {
|
|
|
|
int pollret = poll_fds(socketds, nsockets, -1);
|
|
|
|
if(pollret > 0) {
|
|
|
|
socketd = socketds[pollret - 1];
|
|
|
|
} else {
|
|
|
|
socketd = socketds[0]; /* on a poll error use the first socket */
|
|
|
|
}
|
|
|
|
}
|
2004-02-17 00:08:25 +00:00
|
|
|
new_sd = accept(socketd, NULL, NULL);
|
|
|
|
if((new_sd == -1) && (errno != EINTR)) {
|
2005-06-22 15:54:28 +00:00
|
|
|
if(progexit) {
|
|
|
|
break;
|
|
|
|
}
|
2004-02-17 00:08:25 +00:00
|
|
|
/* very bad - need to exit or restart */
|
2004-03-10 00:37:55 +00:00
|
|
|
#ifdef HAVE_STRERROR_R
|
2004-03-19 20:38:55 +00:00
|
|
|
logg("!accept() failed: %s\n", strerror_r(errno, buff, BUFFSIZE));
|
2004-03-10 00:37:55 +00:00
|
|
|
#else
|
2005-06-23 14:00:08 +00:00
|
|
|
logg("!accept() failed\n");
|
2004-03-10 00:37:55 +00:00
|
|
|
#endif
|
2003-07-29 15:48:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-02-17 17:07:50 +00:00
|
|
|
if (sighup) {
|
|
|
|
logg("SIGHUP caught: re-opening log file.\n");
|
|
|
|
logg_close();
|
|
|
|
sighup = 0;
|
2005-06-01 00:42:33 +00:00
|
|
|
if(!logg_file && (cpt = cfgopt(copt, "LogFile"))->enabled)
|
2004-11-08 14:26:07 +00:00
|
|
|
logg_file = cpt->strarg;
|
2004-02-17 17:07:50 +00:00
|
|
|
}
|
|
|
|
|
2004-08-18 14:25:58 +00:00
|
|
|
if (!progexit && new_sd >= 0) {
|
2004-02-17 17:07:50 +00:00
|
|
|
client_conn = (client_conn_t *) mmalloc(sizeof(struct client_conn_tag));
|
|
|
|
client_conn->sd = new_sd;
|
|
|
|
client_conn->options = options;
|
|
|
|
client_conn->copt = copt;
|
2005-03-07 00:34:04 +00:00
|
|
|
client_conn->root = cl_dup(root);
|
|
|
|
client_conn->root_timestamp = reloaded_time;
|
2004-02-17 17:07:50 +00:00
|
|
|
client_conn->limits = &limits;
|
2005-06-22 15:54:28 +00:00
|
|
|
client_conn->socketds = socketds;
|
|
|
|
client_conn->nsockets = nsockets;
|
2004-03-10 13:31:32 +00:00
|
|
|
if (!thrmgr_dispatch(thr_pool, client_conn)) {
|
|
|
|
close(client_conn->sd);
|
|
|
|
free(client_conn);
|
2004-03-19 20:38:55 +00:00
|
|
|
logg("!thread dispatch failed\n");
|
2004-03-10 13:31:32 +00:00
|
|
|
}
|
2004-02-17 17:07:50 +00:00
|
|
|
}
|
2004-02-17 00:08:25 +00:00
|
|
|
|
|
|
|
pthread_mutex_lock(&exit_mutex);
|
|
|
|
if(progexit) {
|
2004-08-18 14:25:58 +00:00
|
|
|
if (new_sd >= 0) {
|
|
|
|
close(new_sd);
|
|
|
|
}
|
2004-02-17 00:08:25 +00:00
|
|
|
pthread_mutex_unlock(&exit_mutex);
|
|
|
|
break;
|
2003-07-29 15:48:06 +00:00
|
|
|
}
|
2004-02-17 00:08:25 +00:00
|
|
|
pthread_mutex_unlock(&exit_mutex);
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2004-02-17 00:08:25 +00:00
|
|
|
if(selfchk) {
|
|
|
|
time(¤t_time);
|
2005-03-18 13:52:06 +00:00
|
|
|
if((current_time - start_time) > (time_t)selfchk) {
|
2006-09-14 22:43:31 +00:00
|
|
|
if(reload_db(root, dboptions, copt, TRUE, &ret)) {
|
2004-02-17 00:08:25 +00:00
|
|
|
pthread_mutex_lock(&reload_mutex);
|
|
|
|
reload = 1;
|
|
|
|
pthread_mutex_unlock(&reload_mutex);
|
|
|
|
}
|
|
|
|
time(&start_time);
|
|
|
|
}
|
2003-10-31 02:23:54 +00:00
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2004-02-17 00:08:25 +00:00
|
|
|
pthread_mutex_lock(&reload_mutex);
|
|
|
|
if(reload) {
|
|
|
|
pthread_mutex_unlock(&reload_mutex);
|
2006-09-14 22:43:31 +00:00
|
|
|
root = reload_db(root, dboptions, copt, FALSE, &ret);
|
|
|
|
if(ret) {
|
|
|
|
logg("Terminating because of a fatal error.");
|
|
|
|
if(new_sd >= 0)
|
|
|
|
close(new_sd);
|
|
|
|
break;
|
|
|
|
}
|
2005-01-26 17:28:36 +00:00
|
|
|
pthread_mutex_lock(&reload_mutex);
|
|
|
|
reload = 0;
|
2005-06-23 14:00:08 +00:00
|
|
|
time(&reloaded_time);
|
2005-01-26 17:28:36 +00:00
|
|
|
pthread_mutex_unlock(&reload_mutex);
|
2004-02-17 00:08:25 +00:00
|
|
|
#ifdef CLAMUKO
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "ClamukoScanOnAccess")->enabled) {
|
2004-02-29 17:29:09 +00:00
|
|
|
logg("Stopping and restarting Clamuko.\n");
|
|
|
|
pthread_kill(clamuko_pid, SIGUSR1);
|
|
|
|
pthread_join(clamuko_pid, NULL);
|
|
|
|
tharg->root = root;
|
|
|
|
pthread_create(&clamuko_pid, &clamuko_attr, clamukoth, tharg);
|
|
|
|
}
|
2003-12-02 22:48:56 +00:00
|
|
|
#endif
|
2004-02-17 00:08:25 +00:00
|
|
|
} else {
|
|
|
|
pthread_mutex_unlock(&reload_mutex);
|
|
|
|
}
|
|
|
|
}
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2005-03-07 00:34:04 +00:00
|
|
|
/* Destroy the thread manager.
|
|
|
|
* This waits for all current tasks to end
|
|
|
|
*/
|
|
|
|
thrmgr_destroy(thr_pool);
|
2004-02-17 00:08:25 +00:00
|
|
|
#ifdef CLAMUKO
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "ClamukoScanOnAccess")->enabled) {
|
2004-02-29 17:29:09 +00:00
|
|
|
logg("Stopping Clamuko.\n");
|
|
|
|
pthread_kill(clamuko_pid, SIGUSR1);
|
|
|
|
pthread_join(clamuko_pid, NULL);
|
|
|
|
}
|
2003-10-08 10:40:53 +00:00
|
|
|
#endif
|
2006-09-14 22:43:31 +00:00
|
|
|
if(root)
|
|
|
|
cl_free(root);
|
|
|
|
|
|
|
|
if(dbstat)
|
|
|
|
cl_statfree(dbstat);
|
2005-06-22 15:54:28 +00:00
|
|
|
logg("*Shutting down the main socket%s.\n", (nsockets > 1) ? "s" : "");
|
|
|
|
for (i = 0; i < nsockets; i++)
|
|
|
|
shutdown(socketds[i], 2);
|
|
|
|
logg("*Closing the main socket%s.\n", (nsockets > 1) ? "s" : "");
|
|
|
|
for (i = 0; i < nsockets; i++)
|
2006-09-12 20:55:09 +00:00
|
|
|
closesocket(socketds[i]);
|
2004-12-20 01:37:36 +00:00
|
|
|
#ifndef C_OS2
|
2005-06-01 00:42:33 +00:00
|
|
|
if((cpt = cfgopt(copt, "LocalSocket"))->enabled) {
|
2004-03-01 13:01:49 +00:00
|
|
|
if(unlink(cpt->strarg) == -1)
|
|
|
|
logg("!Can't unlink the socket file %s\n", cpt->strarg);
|
|
|
|
else
|
|
|
|
logg("Socket file removed.\n");
|
2004-12-20 01:37:36 +00:00
|
|
|
}
|
|
|
|
#endif
|
2004-03-01 13:01:49 +00:00
|
|
|
|
2005-06-01 00:42:33 +00:00
|
|
|
if((cpt = cfgopt(copt, "PidFile"))->enabled) {
|
2004-03-01 13:01:49 +00:00
|
|
|
if(unlink(cpt->strarg) == -1)
|
|
|
|
logg("!Can't unlink the pid file %s\n", cpt->strarg);
|
|
|
|
else
|
|
|
|
logg("Pid file removed.\n");
|
|
|
|
}
|
|
|
|
|
2004-03-01 13:15:55 +00:00
|
|
|
time(¤t_time);
|
|
|
|
logg("--- Stopped at %s", ctime(¤t_time));
|
|
|
|
|
2006-09-14 22:45:57 +00:00
|
|
|
return ret;
|
2003-09-05 13:10:21 +00:00
|
|
|
}
|