2004-01-20 10:37:54 +00:00
|
|
|
/*
|
2005-01-18 23:51:53 +00:00
|
|
|
* Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net>
|
2004-01-20 10:37:54 +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.
|
2004-01-20 10:37:54 +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
|
|
|
|
|
2004-01-20 10:37:54 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2006-09-12 20:55:09 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-01-20 10:37:54 +00:00
|
|
|
#include <unistd.h>
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif
|
2004-01-20 10:37:54 +00:00
|
|
|
#include <sys/types.h>
|
2006-09-12 20:55:09 +00:00
|
|
|
#ifndef C_WINDOWS
|
2006-09-11 23:10:31 +00:00
|
|
|
#include <dirent.h>
|
2004-01-20 10:37:54 +00:00
|
|
|
#include <sys/socket.h>
|
2004-02-23 10:38:01 +00:00
|
|
|
#include <sys/time.h>
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif
|
2004-01-20 10:37:54 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <signal.h>
|
2004-02-17 00:08:25 +00:00
|
|
|
#include <errno.h>
|
2006-09-12 20:55:09 +00:00
|
|
|
#include <stddef.h>
|
2004-01-20 10:37:54 +00:00
|
|
|
|
2006-09-05 20:45:39 +00:00
|
|
|
#include "libclamav/clamav.h"
|
|
|
|
#include "libclamav/str.h"
|
|
|
|
|
|
|
|
#include "shared/cfgparser.h"
|
|
|
|
#include "shared/memory.h"
|
|
|
|
#include "shared/output.h"
|
2006-10-29 13:41:24 +00:00
|
|
|
#include "shared/misc.h"
|
2006-09-05 20:45:39 +00:00
|
|
|
|
2004-01-20 10:37:54 +00:00
|
|
|
#include "others.h"
|
|
|
|
#include "scanner.h"
|
|
|
|
#include "server.h"
|
|
|
|
#include "clamuko.h"
|
|
|
|
#include "session.h"
|
2006-09-11 23:10:31 +00:00
|
|
|
#include "thrmgr.h"
|
|
|
|
#include "shared.h"
|
2004-01-20 10:37:54 +00:00
|
|
|
|
2005-06-22 14:54:28 +00:00
|
|
|
static pthread_mutex_t ctime_mutex = PTHREAD_MUTEX_INITIALIZER;
|
2006-09-11 23:10:31 +00:00
|
|
|
extern int progexit;
|
|
|
|
|
|
|
|
struct multi_tag {
|
|
|
|
int sd;
|
2007-01-30 21:26:43 +00:00
|
|
|
unsigned int options;
|
2006-09-11 23:10:31 +00:00
|
|
|
const struct cfgstruct *copt;
|
|
|
|
char *fname;
|
2007-01-30 21:26:43 +00:00
|
|
|
const struct cl_engine *engine;
|
2006-09-11 23:10:31 +00:00
|
|
|
const struct cl_limits *limits;
|
|
|
|
};
|
|
|
|
|
|
|
|
void multiscanfile(void *arg)
|
|
|
|
{
|
|
|
|
struct multi_tag *tag = (struct multi_tag *) arg;
|
|
|
|
const char *virname;
|
2006-09-12 20:55:09 +00:00
|
|
|
#ifndef C_WINDOWS
|
2006-09-11 23:10:31 +00:00
|
|
|
sigset_t sigset;
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif
|
2006-09-11 23:10:31 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
2006-09-12 20:55:09 +00:00
|
|
|
#ifndef C_WINDOWS
|
2006-09-11 23:10:31 +00:00
|
|
|
/* ignore all signals */
|
|
|
|
sigfillset(&sigset);
|
|
|
|
pthread_sigmask(SIG_SETMASK, &sigset, NULL);
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif
|
2006-09-11 23:10:31 +00:00
|
|
|
|
2007-01-30 21:26:43 +00:00
|
|
|
ret = cl_scanfile(tag->fname, &virname, NULL, tag->engine, tag->limits, tag->options);
|
2006-09-11 23:10:31 +00:00
|
|
|
|
|
|
|
if(ret == CL_VIRUS) {
|
|
|
|
mdprintf(tag->sd, "%s: %s FOUND\n", tag->fname, virname);
|
|
|
|
logg("%s: %s FOUND\n", tag->fname, virname);
|
|
|
|
virusaction(tag->fname, virname, tag->copt);
|
|
|
|
} else if(ret != CL_CLEAN) {
|
|
|
|
mdprintf(tag->sd, "%s: %s ERROR\n", tag->fname, cl_strerror(ret));
|
|
|
|
logg("%s: %s ERROR\n", tag->fname, cl_strerror(ret));
|
|
|
|
} else if(logok) {
|
|
|
|
logg("%s: OK\n", tag->fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(tag->fname);
|
|
|
|
free(tag);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-01-30 21:26:43 +00:00
|
|
|
static int multiscan(const char *dirname, const struct cl_engine *engine, const struct cl_limits *limits, unsigned int options, const struct cfgstruct *copt, int odesc, unsigned int *reclev, threadpool_t *multi_pool)
|
2006-09-11 23:10:31 +00:00
|
|
|
{
|
|
|
|
DIR *dd;
|
|
|
|
struct dirent *dent;
|
|
|
|
#if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
|
|
|
|
union {
|
|
|
|
struct dirent d;
|
|
|
|
char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
|
|
|
|
} result;
|
|
|
|
#endif
|
|
|
|
struct stat statbuf;
|
|
|
|
char *fname;
|
|
|
|
int scanret = 0;
|
|
|
|
unsigned int maxdirrec = 0;
|
|
|
|
struct multi_tag *scandata;
|
|
|
|
|
|
|
|
|
|
|
|
maxdirrec = cfgopt(copt, "MaxDirectoryRecursion")->numarg;
|
|
|
|
if(maxdirrec) {
|
|
|
|
if(*reclev > maxdirrec) {
|
|
|
|
logg("*multiscan: Directory recursion limit exceeded at %s\n", dirname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
(*reclev)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((dd = opendir(dirname)) != NULL) {
|
|
|
|
#ifdef HAVE_READDIR_R_3
|
|
|
|
while(!readdir_r(dd, &result.d, &dent) && dent) {
|
|
|
|
#elif defined(HAVE_READDIR_R_2)
|
|
|
|
while((dent = (struct dirent *) readdir_r(dd, &result.d))) {
|
|
|
|
#else
|
|
|
|
while((dent = readdir(dd))) {
|
|
|
|
#endif
|
|
|
|
if (!is_fd_connected(odesc)) {
|
|
|
|
logg("multiscan: Client disconnected\n");
|
|
|
|
closedir(dd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(progexit) {
|
|
|
|
closedir(dd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-09-12 20:55:09 +00:00
|
|
|
#if (!defined(C_INTERIX)) && (!defined(C_WINDOWS)) && (!defined(C_CYGWIN))
|
2006-09-11 23:10:31 +00:00
|
|
|
if(dent->d_ino)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
|
|
|
|
/* build the full name */
|
|
|
|
fname = (char *) mcalloc(strlen(dirname) + strlen(dent->d_name) + 2, sizeof(char));
|
|
|
|
if(!fname) {
|
|
|
|
logg("!multiscan: Can't allocate memory for fname\n");
|
|
|
|
closedir(dd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sprintf(fname, "%s/%s", dirname, dent->d_name);
|
|
|
|
|
|
|
|
/* stat the file */
|
|
|
|
if(lstat(fname, &statbuf) != -1) {
|
|
|
|
if((S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) || (S_ISLNK(statbuf.st_mode) && (checksymlink(fname) == 1) && cfgopt(copt, "FollowDirectorySymlinks")->enabled)) {
|
2007-01-30 21:26:43 +00:00
|
|
|
if(multiscan(fname, engine, limits, options, copt, odesc, reclev, multi_pool) == -1) {
|
2006-09-11 23:10:31 +00:00
|
|
|
free(fname);
|
|
|
|
closedir(dd);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-10-29 14:53:56 +00:00
|
|
|
free(fname);
|
2006-09-11 23:10:31 +00:00
|
|
|
} else {
|
|
|
|
if(S_ISREG(statbuf.st_mode) || (S_ISLNK(statbuf.st_mode) && (checksymlink(fname) == 2) && cfgopt(copt, "FollowFileSymlinks")->enabled)) {
|
|
|
|
|
|
|
|
#ifdef C_LINUX
|
|
|
|
if(procdev && (statbuf.st_dev == procdev))
|
|
|
|
scanret = CL_CLEAN;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
scandata = (struct multi_tag *) mmalloc(sizeof(struct multi_tag));
|
|
|
|
if(!scandata) {
|
|
|
|
logg("!multiscan: Can't allocate memory for scandata\n");
|
|
|
|
free(fname);
|
|
|
|
closedir(dd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
scandata->sd = odesc;
|
|
|
|
scandata->options = options;
|
|
|
|
scandata->copt = copt;
|
|
|
|
scandata->fname = fname;
|
2007-01-30 21:26:43 +00:00
|
|
|
scandata->engine = engine;
|
2006-09-11 23:10:31 +00:00
|
|
|
scandata->limits = limits;
|
|
|
|
if(!thrmgr_dispatch(multi_pool, scandata)) {
|
|
|
|
logg("!multiscan: thread dispatch failed for multi_pool (file %s)\n", fname);
|
|
|
|
mdprintf(odesc, "ERROR: Can't scan file %s\n", fname);
|
|
|
|
free(fname);
|
|
|
|
free(scandata);
|
|
|
|
closedir(dd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(!multi_pool->thr_idle) /* non-critical */
|
2006-09-13 22:13:20 +00:00
|
|
|
#ifdef C_WINDOWS
|
|
|
|
Sleep(1);
|
|
|
|
#else
|
2006-09-11 23:10:31 +00:00
|
|
|
usleep(200);
|
2006-09-12 20:55:09 +00:00
|
|
|
#endif
|
2006-09-11 23:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-29 14:53:56 +00:00
|
|
|
} else {
|
|
|
|
free(fname);
|
2006-09-11 23:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dd);
|
|
|
|
} else {
|
2006-10-28 12:49:53 +00:00
|
|
|
return -2;
|
2006-09-11 23:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(*reclev)--;
|
|
|
|
return 0;
|
|
|
|
}
|
2004-01-20 10:37:54 +00:00
|
|
|
|
2007-01-30 21:26:43 +00:00
|
|
|
int command(int desc, const struct cl_engine *engine, const struct cl_limits *limits, unsigned int options, const struct cfgstruct *copt, int timeout)
|
2004-01-20 10:37:54 +00:00
|
|
|
{
|
|
|
|
char buff[1025];
|
2006-09-05 20:45:39 +00:00
|
|
|
int bread, opt;
|
2004-02-23 10:38:01 +00:00
|
|
|
|
2004-09-15 21:12:26 +00:00
|
|
|
|
2006-09-04 22:30:09 +00:00
|
|
|
bread = readsock(desc, buff, sizeof(buff)-1, '\n', timeout, 0, 1);
|
|
|
|
if(bread == -2) /* timeout */
|
2005-11-03 22:05:35 +00:00
|
|
|
return -2;
|
2006-09-04 22:30:09 +00:00
|
|
|
if(bread == 0) /* Connection closed */
|
2005-01-26 17:28:36 +00:00
|
|
|
return -1;
|
2004-06-09 15:51:10 +00:00
|
|
|
if(bread < 0) {
|
2006-09-04 22:30:09 +00:00
|
|
|
mdprintf(desc, "ERROR\n");
|
|
|
|
logg("!Command: readsock() failed.\n");
|
2004-01-20 10:37:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buff[bread] = 0;
|
2004-03-29 00:00:58 +00:00
|
|
|
cli_chomp(buff);
|
2004-01-20 10:37:54 +00:00
|
|
|
|
|
|
|
if(!strncmp(buff, CMD1, strlen(CMD1))) { /* SCAN */
|
2007-01-30 21:26:43 +00:00
|
|
|
if(scan(buff + strlen(CMD1) + 1, NULL, engine, limits, options, copt, desc, 0) == -2)
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "ExitOnOOM")->enabled)
|
2004-11-08 14:26:07 +00:00
|
|
|
return COMMAND_SHUTDOWN;
|
2004-01-20 10:37:54 +00:00
|
|
|
|
|
|
|
} else if(!strncmp(buff, CMD2, strlen(CMD2))) { /* RAWSCAN */
|
2004-09-13 16:44:01 +00:00
|
|
|
opt = options & ~CL_SCAN_ARCHIVE;
|
2007-01-30 21:26:43 +00:00
|
|
|
if(scan(buff + strlen(CMD2) + 1, NULL, engine, NULL, opt, copt, desc, 0) == -2)
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "ExitOnOOM")->enabled)
|
2004-11-08 14:26:07 +00:00
|
|
|
return COMMAND_SHUTDOWN;
|
2004-01-20 10:37:54 +00:00
|
|
|
|
|
|
|
} else if(!strncmp(buff, CMD3, strlen(CMD3))) { /* QUIT */
|
2004-09-04 15:48:30 +00:00
|
|
|
return COMMAND_SHUTDOWN;
|
2004-01-20 10:37:54 +00:00
|
|
|
|
|
|
|
} else if(!strncmp(buff, CMD4, strlen(CMD4))) { /* RELOAD */
|
|
|
|
mdprintf(desc, "RELOADING\n");
|
|
|
|
return COMMAND_RELOAD;
|
|
|
|
|
|
|
|
} else if(!strncmp(buff, CMD5, strlen(CMD5))) { /* PING */
|
|
|
|
mdprintf(desc, "PONG\n");
|
|
|
|
|
|
|
|
} else if(!strncmp(buff, CMD6, strlen(CMD6))) { /* CONTSCAN */
|
2007-01-30 21:26:43 +00:00
|
|
|
if(scan(buff + strlen(CMD6) + 1, NULL, engine, limits, options, copt, desc, 1) == -2)
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "ExitOnOOM")->enabled)
|
2004-11-08 14:26:07 +00:00
|
|
|
return COMMAND_SHUTDOWN;
|
2004-01-20 10:37:54 +00:00
|
|
|
|
|
|
|
} else if(!strncmp(buff, CMD7, strlen(CMD7))) { /* VERSION */
|
2005-06-01 00:42:33 +00:00
|
|
|
const char *dbdir = cfgopt(copt, "DatabaseDirectory")->strarg;
|
2004-09-04 15:48:30 +00:00
|
|
|
char *path;
|
|
|
|
struct cl_cvd *daily;
|
2006-10-29 18:23:33 +00:00
|
|
|
struct stat foo;
|
2004-09-04 15:48:30 +00:00
|
|
|
|
2006-10-29 18:23:33 +00:00
|
|
|
|
|
|
|
if(!(path = mmalloc(strlen(dbdir) + 30))) {
|
2004-09-04 15:48:30 +00:00
|
|
|
mdprintf(desc, "Memory allocation error - SHUTDOWN forced\n");
|
|
|
|
return COMMAND_SHUTDOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(path, "%s/daily.cvd", dbdir);
|
2006-10-29 18:23:33 +00:00
|
|
|
if(stat(path, &foo) == -1)
|
|
|
|
sprintf(path, "%s/daily.inc/daily.info", dbdir);
|
2004-09-04 15:48:30 +00:00
|
|
|
|
|
|
|
if((daily = cl_cvdhead(path))) {
|
|
|
|
time_t t = (time_t) daily->stime;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&ctime_mutex);
|
|
|
|
mdprintf(desc, "ClamAV "VERSION"/%d/%s", daily->version, ctime(&t));
|
|
|
|
pthread_mutex_unlock(&ctime_mutex);
|
|
|
|
cl_cvdfree(daily);
|
|
|
|
} else {
|
|
|
|
mdprintf(desc, "ClamAV "VERSION"\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
free(path);
|
2004-01-20 10:37:54 +00:00
|
|
|
|
|
|
|
} else if(!strncmp(buff, CMD8, strlen(CMD8))) { /* STREAM */
|
2007-01-30 21:26:43 +00:00
|
|
|
if(scanstream(desc, NULL, engine, limits, options, copt) == CL_EMEM)
|
2005-06-01 00:42:33 +00:00
|
|
|
if(cfgopt(copt, "ExitOnOOM")->enabled)
|
2004-11-08 14:26:07 +00:00
|
|
|
return COMMAND_SHUTDOWN;
|
2004-01-20 10:37:54 +00:00
|
|
|
|
|
|
|
} else if(!strncmp(buff, CMD9, strlen(CMD9))) { /* SESSION */
|
2005-01-26 17:28:36 +00:00
|
|
|
return COMMAND_SESSION;
|
2004-01-20 10:37:54 +00:00
|
|
|
|
|
|
|
} else if(!strncmp(buff, CMD10, strlen(CMD10))) { /* END */
|
|
|
|
return COMMAND_END;
|
|
|
|
|
|
|
|
} else if(!strncmp(buff, CMD11, strlen(CMD11))) { /* SHUTDOWN */
|
2004-09-04 15:48:30 +00:00
|
|
|
return COMMAND_SHUTDOWN;
|
2004-01-20 10:37:54 +00:00
|
|
|
|
2004-12-07 01:53:16 +00:00
|
|
|
} else if(!strncmp(buff, CMD12, strlen(CMD12))) { /* FD */
|
|
|
|
int fd = atoi(buff + strlen(CMD12) + 1);
|
|
|
|
|
2007-01-30 21:26:43 +00:00
|
|
|
scanfd(fd, NULL, engine, limits, options, copt, desc);
|
2004-12-07 01:53:16 +00:00
|
|
|
close(fd); /* FIXME: should we close it here? */
|
|
|
|
|
2006-09-11 23:10:31 +00:00
|
|
|
} else if(!strncmp(buff, CMD13, strlen(CMD13))) { /* MULTISCAN */
|
|
|
|
threadpool_t *multi_pool;
|
|
|
|
int idletimeout = cfgopt(copt, "IdleTimeout")->numarg;
|
|
|
|
int max_threads = cfgopt(copt, "MaxThreads")->numarg;
|
|
|
|
int ret;
|
|
|
|
unsigned int reclev = 0;
|
|
|
|
const char *path = buff + strlen(CMD13) + 1;
|
|
|
|
const char *virname;
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if(stat(path, &sb) == -1) {
|
|
|
|
mdprintf(desc, "Can't stat file %s\n", path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(S_ISDIR(sb.st_mode)) {
|
|
|
|
if((multi_pool = thrmgr_new(max_threads, idletimeout, multiscanfile)) == NULL) {
|
|
|
|
logg("!thrmgr_new failed for multi_pool\n");
|
|
|
|
mdprintf(desc, "ERROR: thrmgr_new failed for multi_pool\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-01-30 21:26:43 +00:00
|
|
|
ret = multiscan(path, engine, limits, options, copt, desc, &reclev, multi_pool);
|
2006-09-11 23:10:31 +00:00
|
|
|
thrmgr_destroy(multi_pool);
|
|
|
|
|
|
|
|
if(ret < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
} else {
|
2007-01-30 21:26:43 +00:00
|
|
|
ret = cl_scanfile(path, &virname, NULL, engine, limits, options);
|
2006-09-11 23:10:31 +00:00
|
|
|
|
|
|
|
if(ret == CL_VIRUS) {
|
|
|
|
mdprintf(desc, "%s: %s FOUND\n", path, virname);
|
|
|
|
logg("%s: %s FOUND\n", path, virname);
|
|
|
|
virusaction(path, virname, copt);
|
|
|
|
} else if(ret != CL_CLEAN) {
|
|
|
|
mdprintf(desc, "%s: %s ERROR\n", path, cl_strerror(ret));
|
|
|
|
logg("%s: %s ERROR\n", path, cl_strerror(ret));
|
|
|
|
} else {
|
|
|
|
mdprintf(desc, "%s: OK\n", path);
|
|
|
|
if(logok)
|
|
|
|
logg("%s: OK\n", path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-20 10:37:54 +00:00
|
|
|
} else {
|
|
|
|
mdprintf(desc, "UNKNOWN COMMAND\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0; /* no error and no 'special' command executed */
|
|
|
|
}
|