preliminary zCMD sport

git-svn-id: file:///var/lib/svn/clamav-devel/branches/clamd-proto@4609 77e5149b-7576-45b1-b177-96237e5ba77b
0.95
aCaB 17 years ago
parent 2599ba97e0
commit 3ec259a602
  1. 6
      clamd/others.c
  2. 35
      clamd/scanner.c
  3. 2
      clamd/scanner.h
  4. 26
      clamd/server-th.c
  5. 11
      clamd/server.h
  6. 17
      clamd/session.c
  7. 3
      clamd/session.h

@ -315,11 +315,15 @@ static int read_fd_data(struct fd_buf *buf)
int fds_add(struct fd_data *data, int fd, int listen_only)
{
struct fd_buf *buf;
unsigned n = data->nfds + 1;
unsigned n;
if (fd < 0) {
logg("!add_fd: invalid fd passed to add_fd\n");
return -1;
}
for (n = 0; n < data->nfds; n++)
if (data->buf[n].fd == fd) return 0;
n++;
buf = realloc(data->buf, n*sizeof(*buf));
if (!buf) {
logg("!add_fd: Memory allocation failed for fd_buf\n");

@ -100,7 +100,7 @@ static int checksymlink(const char *path)
return 0;
}
static int dirscan(const char *dirname, const char **virname, unsigned long int *scanned, const struct cl_engine *engine, unsigned int options, const struct optstruct *opts, int odesc, unsigned int *reclev, unsigned int type, threadpool_t *multi_pool)
static int dirscan(const char *dirname, const char *term, const char **virname, unsigned long int *scanned, const struct cl_engine *engine, unsigned int options, const struct optstruct *opts, int odesc, unsigned int *reclev, unsigned int type, threadpool_t *multi_pool)
{
DIR *dd;
struct dirent *dent;
@ -121,7 +121,7 @@ static int dirscan(const char *dirname, const char **virname, unsigned long int
if((opt = optget(opts, "ExcludePath"))->enabled) {
while(opt) {
if(match_regex(dirname, opt->strarg) == 1) {
mdprintf(odesc, "%s: Excluded\n", dirname);
mdprintf(odesc, "%s: Excluded%c", dirname, term);
return 0;
}
opt = (struct optstruct *) opt->nextarg;
@ -173,7 +173,7 @@ static int dirscan(const char *dirname, const char **virname, unsigned long int
/* 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) && optget(opts, "FollowDirectorySymlinks")->enabled)) {
if(dirscan(fname, virname, scanned, engine, options, opts, odesc, reclev, type, multi_pool) == 1) {
if(dirscan(fname, term, virname, scanned, engine, options, opts, odesc, reclev, type, multi_pool) == 1) {
free(fname);
closedir(dd);
return 1;
@ -204,7 +204,7 @@ static int dirscan(const char *dirname, const char **virname, unsigned long int
scandata->engine = engine;
if(!thrmgr_dispatch(multi_pool, scandata)) {
logg("!thread dispatch failed for multi_pool (file %s)\n", fname);
mdprintf(odesc, "ERROR: Can't scan file %s\n", fname);
mdprintf(odesc, "ERROR: Can't scan file %s%c", fname, term);
free(fname);
free(scandata);
closedir(dd);
@ -224,7 +224,7 @@ static int dirscan(const char *dirname, const char **virname, unsigned long int
if(scanret == CL_VIRUS) {
mdprintf(odesc, "%s: %s FOUND\n", fname, *virname);
mdprintf(odesc, "%s: %s FOUND%c", fname, *virname, term);
logg("~%s: %s FOUND\n", fname, *virname);
virusaction(fname, *virname, opts);
if(type == TYPE_SCAN) {
@ -235,7 +235,7 @@ static int dirscan(const char *dirname, const char **virname, unsigned long int
ret = 2;
} else if(scanret != CL_CLEAN) {
mdprintf(odesc, "%s: %s ERROR\n", fname, cl_strerror(scanret));
mdprintf(odesc, "%s: %s ERROR%c", fname, cl_strerror(scanret), term);
logg("~%s: %s ERROR\n", fname, cl_strerror(scanret));
if(scanret == CL_EMEM) {
closedir(dd);
@ -305,7 +305,7 @@ static void multiscanfile(void *arg)
return;
}
int scan(const char *filename, unsigned long int *scanned, const struct cl_engine *engine, unsigned int options, const struct optstruct *opts, int odesc, unsigned int type)
int scan(const char *filename, const char term, unsigned long int *scanned, const struct cl_engine *engine, unsigned int options, const struct optstruct *opts, int odesc, unsigned int type)
{
struct stat sb;
int ret = 0;
@ -317,19 +317,19 @@ int scan(const char *filename, unsigned long int *scanned, const struct cl_engin
/* stat file */
if(lstat(filename, &sb) == -1) {
mdprintf(odesc, "%s: lstat() failed. ERROR\n", filename);
mdprintf(odesc, "%s: lstat() failed. ERROR%c", filename, term);
return -1;
}
/* check permissions */
if(access(filename, R_OK)) {
mdprintf(odesc, "%s: Access denied. ERROR\n", filename);
mdprintf(odesc, "%s: Access denied. ERROR%c", filename, term);
return -1;
}
if((opt = optget(opts, "ExcludePath"))->enabled) {
if(match_regex(filename, opt->strarg) == 1) {
mdprintf(odesc, "%s: Excluded\n", filename);
mdprintf(odesc, "%s: Excluded%c", filename, term);
return 0;
}
}
@ -343,7 +343,7 @@ int scan(const char *filename, unsigned long int *scanned, const struct cl_engin
#endif
case S_IFREG:
if(sb.st_size == 0) { /* empty file */
mdprintf(odesc, "%s: Empty file\n", filename);
mdprintf(odesc, "%s: Empty file%c", filename, term);
return 0;
}
#ifdef C_LINUX
@ -358,11 +358,11 @@ int scan(const char *filename, unsigned long int *scanned, const struct cl_engin
}
if(ret == CL_VIRUS) {
mdprintf(odesc, "%s: %s FOUND\n", filename, virname);
mdprintf(odesc, "%s: %s FOUND%c", filename, virname, term);
logg("~%s: %s FOUND\n", filename, virname);
virusaction(filename, virname, opts);
} else if(ret != CL_CLEAN) {
mdprintf(odesc, "%s: %s ERROR\n", filename, cl_strerror(ret));
mdprintf(odesc, "%s: %s ERROR%c", filename, cl_strerror(ret), term);
logg("~%s: %s ERROR\n", filename, cl_strerror(ret));
if(ret == CL_EMEM)
return -2;
@ -377,26 +377,25 @@ int scan(const char *filename, unsigned long int *scanned, const struct cl_engin
if((multi_pool = thrmgr_new(max_threads, idletimeout, multiscanfile)) == NULL) {
logg("!thrmgr_new failed for multi_pool\n");
mdprintf(odesc, "thrmgr_new failed for multi_pool ERROR\n");
mdprintf(odesc, "thrmgr_new failed for multi_pool ERROR%c", term);
return -1;
}
}
ret = dirscan(filename, &virname, scanned, engine, options, opts, odesc, &reclev, type, multi_pool);
ret = dirscan(filename, term, &virname, scanned, engine, options, opts, odesc, &reclev, type, multi_pool);
if(multi_pool)
thrmgr_destroy(multi_pool);
break;
default:
mdprintf(odesc, "%s: Not supported file type. ERROR\n", filename);
mdprintf(odesc, "%s: Not supported file type. ERROR%c", filename, term);
return -1;
}
if(!ret)
mdprintf(odesc, "%s: OK\n", filename);
mdprintf(odesc, "%s: OK%c", filename, term);
/* mdprintf(odesc, "\n"); */ /* Terminate response with a blank line boundary */
return ret;
}

@ -26,7 +26,7 @@
#include "libclamav/clamav.h"
#include "shared/optparser.h"
int scan(const char *filename, unsigned long int *scanned, const struct cl_engine *engine, unsigned int options, const struct optstruct *opts, int odesc, unsigned int type);
int scan(const char *filename, const char term, unsigned long int *scanned, const struct cl_engine *engine, unsigned int options, const struct optstruct *opts, int odesc, unsigned int type);
int scanfd(const int fd, unsigned long int *scanned, const struct cl_engine *engine, unsigned int options, const struct optstruct *opts, int odesc);

@ -77,16 +77,6 @@ pthread_mutex_t reload_mutex = PTHREAD_MUTEX_INITIALIZER;
int sighup = 0;
static struct cl_stat *dbstat = NULL;
typedef struct client_conn_tag {
char *cmd;
size_t cmdlen;
int sd;
unsigned int options;
const struct optstruct *opts;
struct cl_engine *engine;
time_t engine_timestamp;
} client_conn_t;
static void scanner_thread(void *arg)
{
client_conn_t *conn = (client_conn_t *) arg;
@ -115,7 +105,7 @@ static void scanner_thread(void *arg)
timeout = -1;
do {
ret = command(conn->sd, conn->cmd, conn->cmdlen, conn->engine, conn->options, conn->opts, timeout);
ret = command(conn, timeout);
if (ret < 0) {
break;
}
@ -283,18 +273,21 @@ struct rcvloop {
struct fd_data *fds;
};
static const char *get_cmd(struct fd_buf *buf, size_t off, size_t *len)
static const char *get_cmd(struct fd_buf *buf, size_t off, size_t *len, char *term)
{
unsigned char *pos;
if (!buf->off || off >= buf->off) {
*len = 0;
return NULL;
}
*term = '\n';
switch (buf->buffer[0]) {
/* commands terminated by delimiters */
case 'n':
case 'z':
pos = memchr(buf->buffer + off, buf->buffer[0] == 'n' ? '\n' : '\0', buf->off);
*term = '\0';
case 'n':
pos = memchr(buf->buffer + off, *term, buf->off);
if (!pos) {
/* we don't have another full command yet */
*len = 0;
@ -665,7 +658,6 @@ int acceptloop_th(int *socketds, unsigned nsockets, struct cl_engine *engine, un
if (!buf->buffer) {
/* listen only socket */
new_sd = accept(fds.buf[i].fd, NULL, NULL);
if (new_sd >= 0) {
if (fds_add(&fds, new_sd, 0) == -1) {
logg("!fds_add failed\n");
@ -693,15 +685,17 @@ int acceptloop_th(int *socketds, unsigned nsockets, struct cl_engine *engine, un
size_t cmdlen = 0;
size_t pos = 0;
int error = 0;
char term;
/* New data available to read on socket. */
/* Parse & dispatch commands */
while ((cmd = get_cmd(buf, pos, &cmdlen)) != NULL) {
while ((cmd = get_cmd(buf, pos, &cmdlen, &term)) != NULL) {
client_conn = (client_conn_t *) malloc(sizeof(struct client_conn_tag));
if(client_conn) {
client_conn->sd = buf->fd;
client_conn->cmdlen = cmdlen;
client_conn->cmd = malloc(cmdlen+1);
client_conn->term = term;
if (!client_conn->cmd) {
logg("!acceptloop_th: failed to allocate memory for command\n");
error = 1;

@ -43,6 +43,17 @@ struct thrwarg {
unsigned int options;
};
typedef struct client_conn_tag {
char *cmd;
size_t cmdlen;
int sd;
unsigned int options;
const struct optstruct *opts;
struct cl_engine *engine;
time_t engine_timestamp;
char term;
} client_conn_t;
int acceptloop_th(int *socketds, unsigned nsockets, struct cl_engine *engine, unsigned int dboptions, const struct optstruct *opts);
void sighandler(int sig);
void sighandler_th(int sig);

@ -114,14 +114,23 @@ static int recvfd_and_scan(int desc, const struct cl_engine *engine, unsigned in
}
#endif
int command(int desc, char *buff, size_t cmdlen, const struct cl_engine *engine, unsigned int options, const struct optstruct *opts, int timeout)
int command(client_conn_t *conn, int timeout)
{
int desc = conn->sd;
char *buff = conn->cmd;
size_t cmdlen = conn->cmdlen;
const struct cl_engine *engine = conn->engine;
unsigned int options = conn->options;
const struct optstruct *opts = conn->opts;
const char term = conn->term;
cli_chomp(buff);
thrmgr_setactiveengine(engine);
if(!strncmp(buff, CMD1, strlen(CMD1))) { /* SCAN */
thrmgr_setactivetask(NULL, CMD1);
if(scan(buff + strlen(CMD1) + 1, NULL, engine, options, opts, desc, TYPE_SCAN) == -2)
if(scan(buff + strlen(CMD1) + 1, term, NULL, engine, options, opts, desc, TYPE_SCAN) == -2)
if(optget(opts, "ExitOnOOM")->enabled)
return COMMAND_SHUTDOWN;
@ -144,7 +153,7 @@ int command(int desc, char *buff, size_t cmdlen, const struct cl_engine *engine,
} else if(!strncmp(buff, CMD6, strlen(CMD6))) { /* CONTSCAN */
thrmgr_setactivetask(NULL, CMD6);
if(scan(buff + strlen(CMD6) + 1, NULL, engine, options, opts, desc, TYPE_CONTSCAN) == -2)
if(scan(buff + strlen(CMD6) + 1, term, NULL, engine, options, opts, desc, TYPE_CONTSCAN) == -2)
if(optget(opts, "ExitOnOOM")->enabled)
return COMMAND_SHUTDOWN;
@ -183,7 +192,7 @@ int command(int desc, char *buff, size_t cmdlen, const struct cl_engine *engine,
} else if(!strncmp(buff, CMD13, strlen(CMD13))) { /* MULTISCAN */
thrmgr_setactivetask(buff+strlen(CMD13)+1, CMD13);
if(scan(buff + strlen(CMD13) + 1, NULL, engine, options, opts, desc, TYPE_MULTISCAN) == -2)
if(scan(buff + strlen(CMD13) + 1, term, NULL, engine, options, opts, desc, TYPE_MULTISCAN) == -2)
if(optget(opts, "ExitOnOOM")->enabled)
return COMMAND_SHUTDOWN;

@ -42,7 +42,8 @@
#include "libclamav/clamav.h"
#include "shared/optparser.h"
#include "server.h"
int command(int desc, char *cmd, size_t cmdlen ,const struct cl_engine *engine, unsigned int options, const struct optstruct *opts, int timeout);
int command(client_conn_t *conn, int timeout);
#endif

Loading…
Cancel
Save