clamav/clamav-devel/clamd/scanner.c

479 lines
12 KiB
C
Raw Normal View History

2003-07-29 15:48:06 +00:00
/*
2005-01-26 17:45:25 +00:00
* Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
2003-07-29 15:48:06 +00:00
*/
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
2003-07-29 15:48:06 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
2003-07-29 15:48:06 +00:00
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
2003-07-29 15:48:06 +00:00
#include <sys/wait.h>
#include <sys/param.h>
2003-07-29 15:48:06 +00:00
#include <dirent.h>
#include <sys/socket.h>
#include <netinet/in.h>
2004-04-05 13:30:09 +00:00
#include <arpa/inet.h>
#include <netdb.h>
#include <pthread.h>
#if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
#include <limits.h>
#include <stddef.h>
#endif
2006-09-05 20:45:39 +00:00
#include "libclamav/clamav.h"
#include "libclamav/others.h"
#include "shared/cfgparser.h"
#include "shared/memory.h"
#include "shared/output.h"
2003-07-29 15:48:06 +00:00
#include "others.h"
#include "scanner.h"
2004-03-29 00:00:58 +00:00
#include "shared.h"
#include "network.h"
2003-07-29 15:48:06 +00:00
2004-05-11 00:14:14 +00:00
#ifdef C_LINUX
dev_t procdev; /* /proc device */
#endif
/* Maximum filenames under various systems - njh */
#ifndef NAME_MAX /* e.g. Linux */
# ifdef MAXNAMELEN /* e.g. Solaris */
# define NAME_MAX MAXNAMELEN
# else
# ifdef FILENAME_MAX /* e.g. SCO */
# define NAME_MAX FILENAME_MAX
# endif
# endif
#endif
static int checksymlink(const char *path)
2003-07-29 15:48:06 +00:00
{
struct stat statbuf;
if(stat(path, &statbuf) == -1)
return -1;
if(S_ISDIR(statbuf.st_mode))
return 1;
if(S_ISREG(statbuf.st_mode))
return 2;
return 0;
}
/* :set nowrap, if you don't like this style ;)) */
int dirscan(const char *dirname, const char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, const struct cfgstruct *copt, int odesc, unsigned int *reclev, short contscan)
2003-07-29 15:48:06 +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
2003-07-29 15:48:06 +00:00
struct stat statbuf;
char *fname;
2006-09-05 20:45:39 +00:00
int ret = 0, scanret = 0;
unsigned int maxdirrec = 0;
2003-07-29 15:48:06 +00:00
2004-09-27 20:53:24 +00:00
maxdirrec = cfgopt(copt, "MaxDirectoryRecursion")->numarg;
2004-09-27 20:53:24 +00:00
if(maxdirrec) {
if(*reclev > maxdirrec) {
logg("*Directory recursion limit exceeded at %s\n", dirname);
return 0;
2003-07-29 15:48:06 +00:00
}
2004-09-27 20:53:24 +00:00
(*reclev)++;
2003-07-29 15:48:06 +00:00
}
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
2003-07-29 15:48:06 +00:00
while((dent = readdir(dd))) {
#endif
if (!is_fd_connected(odesc)) {
logg("Client disconnected\n");
closedir(dd);
return 1;
}
2004-06-27 07:22:19 +00:00
#ifndef C_INTERIX
if(dent->d_ino)
#endif
{
2003-07-29 15:48:06 +00:00
if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
/* build the full name */
2004-03-29 00:00:58 +00:00
fname = (char *) mcalloc(strlen(dirname) + strlen(dent->d_name) + 2, sizeof(char));
2003-07-29 15:48:06 +00:00
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)) {
2003-07-29 15:48:06 +00:00
if(dirscan(fname, virname, scanned, root, limits, options, copt, odesc, reclev, contscan) == 1) {
free(fname);
closedir(dd);
return 1;
}
} else {
if(S_ISREG(statbuf.st_mode) || (S_ISLNK(statbuf.st_mode) && (checksymlink(fname) == 2) && cfgopt(copt, "FollowFileSymlinks")->enabled)) {
2004-05-11 00:14:14 +00:00
#ifdef C_LINUX
2004-06-21 19:29:21 +00:00
if(procdev && (statbuf.st_dev == procdev))
scanret = CL_CLEAN;
else
2004-05-11 00:14:14 +00:00
#endif
2004-06-21 19:29:21 +00:00
scanret = cl_scanfile(fname, virname, scanned, root, limits, options);
2004-05-11 00:14:14 +00:00
if(scanret == CL_VIRUS) {
2003-07-29 15:48:06 +00:00
mdprintf(odesc, "%s: %s FOUND\n", fname, *virname);
logg("%s: %s FOUND\n", fname, *virname);
virusaction(fname, *virname, copt);
2003-07-29 15:48:06 +00:00
if(!contscan) {
closedir(dd);
free(fname);
return 1;
} else
ret = 2;
2003-10-26 06:01:03 +00:00
} else if(scanret != CL_CLEAN) {
2003-11-05 21:35:20 +00:00
mdprintf(odesc, "%s: %s ERROR\n", fname, cl_strerror(scanret));
2003-11-29 03:38:55 +00:00
logg("%s: %s ERROR\n", fname, cl_strerror(scanret));
if(scanret == CL_EMEM) {
closedir(dd);
free(fname);
return -2;
}
2004-05-11 00:14:14 +00:00
} else if(logok) {
2004-02-01 01:18:57 +00:00
logg("%s: OK\n", fname);
2003-07-29 15:48:06 +00:00
}
}
}
2003-07-29 15:48:06 +00:00
}
free(fname);
}
}
}
closedir(dd);
2003-07-29 15:48:06 +00:00
} else {
return -1;
}
(*reclev)--;
return ret;
}
int scan(const char *filename, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, const struct cfgstruct *copt, int odesc, short contscan)
{
struct stat sb;
2006-09-05 20:45:39 +00:00
int ret = 0;
unsigned int reclev = 0;
const char *virname;
2003-07-29 15:48:06 +00:00
/* stat file */
if(lstat(filename, &sb) == -1) {
2004-06-29 21:34:05 +00:00
mdprintf(odesc, "%s: lstat() failed. ERROR\n", filename);
2003-07-29 15:48:06 +00:00
return -1;
}
2006-03-26 19:39:05 +00:00
/* check permissions */
if(access(filename, R_OK)) {
mdprintf(odesc, "%s: Access denied. ERROR\n", filename);
return -1;
}
2003-07-29 15:48:06 +00:00
switch(sb.st_mode & S_IFMT) {
case S_IFLNK:
if(!cfgopt(copt, "FollowFileSymlinks")->enabled)
2003-07-29 15:48:06 +00:00
break;
/* else go to the next case */
case S_IFREG:
if(sb.st_size == 0) { /* empty file */
mdprintf(odesc, "%s: Empty file\n", filename);
return 0;
}
2004-05-11 00:14:14 +00:00
#ifdef C_LINUX
2004-06-21 19:29:21 +00:00
if(procdev && (sb.st_dev == procdev))
ret = CL_CLEAN;
else
2004-05-11 00:14:14 +00:00
#endif
2004-06-21 19:29:21 +00:00
ret = cl_scanfile(filename, &virname, scanned, root, limits, options);
2004-05-11 00:14:14 +00:00
2003-07-29 15:48:06 +00:00
if(ret == CL_VIRUS) {
mdprintf(odesc, "%s: %s FOUND\n", filename, virname);
logg("%s: %s FOUND\n", filename, virname);
virusaction(filename, virname, copt);
2003-07-29 15:48:06 +00:00
} else if(ret != CL_CLEAN) {
2003-08-29 14:27:15 +00:00
mdprintf(odesc, "%s: %s ERROR\n", filename, cl_strerror(ret));
logg("%s: %s ERROR\n", filename, cl_strerror(ret));
if(ret == CL_EMEM)
return -2;
2004-02-01 01:18:57 +00:00
} else if (logok) {
logg("%s: OK\n", filename);
}
2003-07-29 15:48:06 +00:00
break;
case S_IFDIR:
ret = dirscan(filename, &virname, scanned, root, limits, options, copt, odesc, &reclev, contscan);
break;
default:
2004-06-29 21:34:05 +00:00
mdprintf(odesc, "%s: Not supported file type. ERROR\n", filename);
2003-07-29 15:48:06 +00:00
return -1;
}
if(!ret)
mdprintf(odesc, "%s: OK\n", filename);
mdprintf(odesc, "\n"); /* Terminate response with a blank line boundary */
2003-07-29 15:48:06 +00:00
return ret;
}
2006-09-05 20:45:39 +00:00
int scanfd(const int fd, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, const struct cfgstruct *copt, int odesc)
{
int ret;
const char *virname;
struct stat statbuf;
char fdstr[32];
if(fstat(fd, &statbuf) == -1)
return -1;
if(!S_ISREG(statbuf.st_mode))
return -1;
snprintf(fdstr, sizeof(fdstr), "fd[%d]", fd);
ret = cl_scandesc(fd, &virname, scanned, root, limits, options);
if(ret == CL_VIRUS) {
mdprintf(odesc, "%s: %s FOUND\n", fdstr, virname);
logg("%s: %s FOUND\n", fdstr, virname);
virusaction(fdstr, virname, copt);
} else if(ret != CL_CLEAN) {
mdprintf(odesc, "%s: %s ERROR\n", fdstr, cl_strerror(ret));
logg("%s: %s ERROR\n", fdstr, cl_strerror(ret));
} else {
mdprintf(odesc, "%s: OK\n", fdstr);
if(logok)
logg("%s: OK\n", fdstr);
}
return ret;
}
2003-07-29 15:48:06 +00:00
int scanstream(int odesc, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, const struct cfgstruct *copt)
{
int ret, portscan = 1000, sockfd, port = 0, acceptd;
int tmpd, bread, retval, timeout, btread, min_port, max_port;
2003-07-29 15:48:06 +00:00
long int size = 0, maxsize = 0;
short bound = 0, rnd_port_first = 1;
const char *virname;
2004-04-05 13:30:09 +00:00
char buff[FILEBUFF];
2003-07-29 15:48:06 +00:00
struct sockaddr_in server;
struct hostent he;
2003-07-29 15:48:06 +00:00
struct cfgstruct *cpt;
char *tmpname;
2003-07-29 15:48:06 +00:00
2004-05-01 19:33:05 +00:00
/* get min port */
min_port = cfgopt(copt, "StreamMinPort")->numarg;
if(min_port < 1024 || min_port > 65535)
min_port = 1024;
/* get max port */
max_port = cfgopt(copt, "StreamMaxPort")->numarg;
if(max_port < min_port || max_port > 65535)
max_port = 65535;
/* bind to a free port */
while(!bound && --portscan) {
if(rnd_port_first) {
/* try a random port first */
port = min_port + cli_rndnum(max_port - min_port + 1);
rnd_port_first = 0;
} else {
/* try the neighbor ports */
if(--port < min_port)
port=max_port;
}
2003-07-29 15:48:06 +00:00
memset((char *) &server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(port);
2004-04-05 13:30:09 +00:00
if((cpt = cfgopt(copt, "TCPAddr"))->enabled) {
if(r_gethostbyname(cpt->strarg, &he, buff, sizeof(buff)) == -1) {
logg("!r_gethostbyname(%s) error: %s\n", cpt->strarg, strerror(errno));
mdprintf(odesc, "r_gethostbyname(%s) ERROR\n", cpt->strarg);
2004-04-05 13:30:09 +00:00
return -1;
}
server.sin_addr = *(struct in_addr *) he.h_addr_list[0];
2004-04-05 13:30:09 +00:00
} else
server.sin_addr.s_addr = INADDR_ANY;
2003-07-29 15:48:06 +00:00
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
continue;
if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) == -1)
close(sockfd);
else
2003-11-01 03:16:25 +00:00
bound = 1;
2003-07-29 15:48:06 +00:00
}
2004-05-01 19:33:05 +00:00
timeout = cfgopt(copt, "ReadTimeout")->numarg;
2004-05-01 19:33:05 +00:00
if(timeout == 0)
timeout = -1;
2003-07-29 15:48:06 +00:00
2003-11-01 03:16:25 +00:00
if(!bound && !portscan) {
2003-07-29 15:48:06 +00:00
logg("!ScanStream: Can't find any free port.\n");
2004-06-29 21:34:05 +00:00
mdprintf(odesc, "Can't find any free port. ERROR\n");
2004-05-01 19:33:05 +00:00
close(sockfd);
2003-07-29 15:48:06 +00:00
return -1;
} else {
listen(sockfd, 1);
if(mdprintf(odesc, "PORT %d\n", port) <= 0) {
logg("!ScanStream: error transmitting port.\n");
close(sockfd);
return -1;
}
2003-07-29 15:48:06 +00:00
}
2004-05-01 19:33:05 +00:00
switch(retval = poll_fd(sockfd, timeout)) {
case 0: /* timeout */
2004-06-29 21:34:05 +00:00
mdprintf(odesc, "Accept timeout. ERROR\n");
logg("!ScanStream %d: accept timeout.\n", port);
2004-05-01 19:33:05 +00:00
close(sockfd);
return -1;
case -1:
2004-06-29 21:34:05 +00:00
mdprintf(odesc, "Accept poll. ERROR\n");
logg("!ScanStream %d: accept poll failed.\n", port);
2004-05-01 19:33:05 +00:00
close(sockfd);
return -1;
}
2003-07-29 15:48:06 +00:00
if((acceptd = accept(sockfd, NULL, NULL)) == -1) {
close(sockfd);
mdprintf(odesc, "accept() ERROR\n");
logg("!ScanStream %d: accept() failed.\n", port);
2003-07-29 15:48:06 +00:00
return -1;
}
2003-11-01 03:16:25 +00:00
logg("*Accepted connection on port %d, fd %d\n", port, acceptd);
2003-09-29 11:44:52 +00:00
if ((tmpname = cli_gentempdesc(NULL, &tmpd)) == NULL) {
2004-05-01 19:33:05 +00:00
shutdown(sockfd, 2);
close(sockfd);
close(acceptd);
2004-06-29 21:34:05 +00:00
mdprintf(odesc, "tempfile() failed. ERROR\n");
logg("!ScanStream %d: Can't create temporary file.\n", port);
2004-05-01 19:33:05 +00:00
return -1;
}
maxsize = cfgopt(copt, "StreamMaxLength")->numarg;
2004-05-01 19:33:05 +00:00
btread = sizeof(buff);
while((retval = poll_fd(acceptd, timeout)) == 1) {
bread = read(acceptd, buff, btread);
if(bread <= 0)
break;
size += bread;
if(writen(tmpd, buff, bread) != bread) {
2003-07-29 15:48:06 +00:00
shutdown(sockfd, 2);
close(sockfd);
2003-11-01 03:16:25 +00:00
close(acceptd);
2004-05-01 19:33:05 +00:00
mdprintf(odesc, "Temporary file -> write ERROR\n");
logg("!ScanStream %d: Can't write to temporary file.\n", port);
close(tmpd);
if(!cfgopt(copt, "LeaveTemporaryFiles")->enabled)
unlink(tmpname);
free(tmpname);
2003-07-29 15:48:06 +00:00
return -1;
}
2004-05-01 19:33:05 +00:00
if(maxsize && (size + btread >= maxsize)) {
btread = (maxsize - size); /* only read up to max */
2003-07-29 15:48:06 +00:00
2005-04-06 22:17:26 +00:00
if(btread <= 0) {
logg("^ScanStream %d: Size limit reached (max: %d)\n", port, maxsize);
2004-05-01 19:33:05 +00:00
break; /* Scan what we have */
}
2003-07-29 15:48:06 +00:00
}
2004-05-01 19:33:05 +00:00
}
2003-07-29 15:48:06 +00:00
2004-05-01 19:33:05 +00:00
switch(retval) {
case 0: /* timeout */
2004-05-16 01:09:43 +00:00
mdprintf(odesc, "read timeout ERROR\n");
logg("!ScanStream %d: read timeout.\n", port);
break;
2004-05-01 19:33:05 +00:00
case -1:
2004-05-16 01:09:43 +00:00
mdprintf(odesc, "read poll ERROR\n");
logg("!ScanStream %d: read poll failed.\n", port);
break;
2004-05-01 19:33:05 +00:00
}
2003-07-29 15:48:06 +00:00
if(retval == 1) {
lseek(tmpd, 0, SEEK_SET);
ret = cl_scandesc(tmpd, &virname, scanned, root, limits, options);
} else {
ret = -1;
}
close(tmpd);
if(!cfgopt(copt, "LeaveTemporaryFiles")->enabled)
unlink(tmpname);
free(tmpname);
2003-07-29 15:48:06 +00:00
close(acceptd);
close(sockfd);
if(ret == CL_VIRUS) {
mdprintf(odesc, "stream: %s FOUND\n", virname);
logg("stream %d: %s FOUND\n", port, virname);
virusaction("stream", virname, copt);
2003-07-29 15:48:06 +00:00
} else if(ret != CL_CLEAN) {
if(retval == 1) {
mdprintf(odesc, "stream: %s ERROR\n", cl_strerror(ret));
logg("stream %d: %s ERROR\n", port, cl_strerror(ret));
}
2004-02-01 01:18:57 +00:00
} else {
2003-07-29 15:48:06 +00:00
mdprintf(odesc, "stream: OK\n");
if(logok)
logg("stream %d: OK\n", port);
2004-02-01 01:18:57 +00:00
}
2003-07-29 15:48:06 +00:00
return ret;
}