e3aaff8e |
/* |
ee039e40 |
* Copyright (C) 2002 - 2004 Tomasz Kojm <tkojm@clamav.net> |
e3aaff8e |
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
|
6d6e8271 |
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
|
e3aaff8e |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h> |
31e6c6fb |
#include <sys/time.h> |
e3aaff8e |
#include <sys/wait.h>
#include <dirent.h>
#include <sys/socket.h>
#include <netinet/in.h> |
a0283d44 |
#include <arpa/inet.h>
#include <netdb.h> |
e3aaff8e |
#include <clamav.h> |
d9855a95 |
#include <pthread.h> |
31e6c6fb |
|
afb48b28 |
#include "cfgparser.h" |
e3aaff8e |
#include "others.h"
#include "scanner.h"
#include "defaults.h" |
afb48b28 |
#include "memory.h"
#include "shared.h"
#include "output.h" |
e3aaff8e |
|
8000d078 |
#include "../libclamav/others.h"
|
c695dab4 |
#ifdef C_LINUX
dev_t procdev; /* /proc device */
#endif
|
d9855a95 |
pthread_mutex_t gh_mutex = PTHREAD_MUTEX_INITIALIZER;
|
e3aaff8e |
int checksymlink(const char *path)
{
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 ;)) */ |
fb787a06 |
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) |
e3aaff8e |
{
DIR *dd;
struct dirent *dent;
struct stat statbuf;
struct cfgstruct *cpt;
char *fname; |
c6dbfbcb |
int ret = 0, scanret = 0, maxdirrec = 0; |
e3aaff8e |
|
c6dbfbcb |
if((cpt = cfgopt(copt, "MaxDirectoryRecursion")))
maxdirrec = cpt->numarg;
else
maxdirrec = CL_DEFAULT_MAXDIRREC;
if(maxdirrec) {
if(*reclev > maxdirrec) {
logg("*Directory recursion limit exceeded at %s\n", dirname);
return 0; |
e3aaff8e |
} |
c6dbfbcb |
(*reclev)++; |
e3aaff8e |
}
if((dd = opendir(dirname)) != NULL) {
while((dent = readdir(dd))) { |
e0909dc6 |
if (!is_fd_connected(odesc)) {
logg("Client disconnected\n");
closedir(dd);
return 1;
} |
feeaa333 |
#ifndef C_INTERIX
if(dent->d_ino)
#endif
{ |
e3aaff8e |
if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
/* build the full name */ |
afb48b28 |
fname = (char *) mcalloc(strlen(dirname) + strlen(dent->d_name) + 2, sizeof(char)); |
e3aaff8e |
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"))) {
if(dirscan(fname, virname, scanned, root, limits, options, copt, odesc, reclev, contscan) == 1) {
free(fname);
closedir(dd);
return 1;
} |
2d70a403 |
} else {
if(S_ISREG(statbuf.st_mode) || (S_ISLNK(statbuf.st_mode) && (checksymlink(fname) == 2) && cfgopt(copt, "FollowFileSymlinks"))) { |
c695dab4 |
#ifdef C_LINUX |
19ca43de |
if(procdev && (statbuf.st_dev == procdev))
scanret = CL_CLEAN;
else |
c695dab4 |
#endif |
19ca43de |
scanret = cl_scanfile(fname, virname, scanned, root, limits, options);
|
c695dab4 |
if(scanret == CL_VIRUS) {
|
e3aaff8e |
mdprintf(odesc, "%s: %s FOUND\n", fname, *virname);
logg("%s: %s FOUND\n", fname, *virname); |
9cb8aa62 |
virusaction(*virname, copt); |
e3aaff8e |
if(!contscan) {
closedir(dd);
free(fname);
return 1;
} else
ret = 2; |
e4ae7726 |
} else if(scanret != CL_CLEAN) { |
391916fd |
mdprintf(odesc, "%s: %s ERROR\n", fname, cl_strerror(scanret)); |
8ac7b1ce |
logg("%s: %s ERROR\n", fname, cl_strerror(scanret)); |
c695dab4 |
} else if(logok) { |
ee039e40 |
logg("%s: OK\n", fname); |
e3aaff8e |
} |
2d70a403 |
}
} |
e3aaff8e |
}
free(fname);
}
}
} |
2d70a403 |
closedir(dd); |
e3aaff8e |
} 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;
int ret = 0, reclev = 0; |
fb787a06 |
const char *virname; |
e3aaff8e |
/* check permissions */
if(access(filename, R_OK)) { |
65a18d2e |
mdprintf(odesc, "%s: Access denied. ERROR\n", filename); |
e3aaff8e |
return -1;
}
/* stat file */
if(lstat(filename, &sb) == -1) { |
65a18d2e |
mdprintf(odesc, "%s: lstat() failed. ERROR\n", filename); |
e3aaff8e |
return -1;
}
switch(sb.st_mode & S_IFMT) {
case S_IFLNK:
if(!cfgopt(copt, "FollowFileSymlinks"))
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;
} |
c695dab4 |
#ifdef C_LINUX |
19ca43de |
if(procdev && (sb.st_dev == procdev))
ret = CL_CLEAN;
else |
c695dab4 |
#endif |
19ca43de |
ret = cl_scanfile(filename, &virname, scanned, root, limits, options); |
c695dab4 |
|
e3aaff8e |
if(ret == CL_VIRUS) {
mdprintf(odesc, "%s: %s FOUND\n", filename, virname);
logg("%s: %s FOUND\n", filename, virname); |
9cb8aa62 |
virusaction(virname, copt); |
e3aaff8e |
} else if(ret != CL_CLEAN) { |
049a18b9 |
mdprintf(odesc, "%s: %s ERROR\n", filename, cl_strerror(ret));
logg("%s: %s ERROR\n", filename, cl_strerror(ret)); |
ee039e40 |
} else if (logok) {
logg("%s: OK\n", filename);
} |
e3aaff8e |
break;
case S_IFDIR:
ret = dirscan(filename, &virname, scanned, root, limits, options, copt, odesc, &reclev, contscan);
break;
default: |
65a18d2e |
mdprintf(odesc, "%s: Not supported file type. ERROR\n", filename); |
e3aaff8e |
return -1;
}
if(!ret)
mdprintf(odesc, "%s: OK\n", filename);
return ret;
}
int scanstream(int odesc, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, const struct cfgstruct *copt)
{ |
32c970f8 |
int ret, portscan = CL_DEFAULT_MAXPORTSCAN, sockfd, port, acceptd;
int tmpd, bread, retval, timeout, btread; |
e3aaff8e |
long int size = 0, maxsize = 0; |
55216b6e |
short bound = 0; |
fb787a06 |
const char *virname; |
a0283d44 |
char buff[FILEBUFF]; |
e3aaff8e |
struct sockaddr_in server; |
a0283d44 |
struct hostent *he; |
e3aaff8e |
struct cfgstruct *cpt; |
55216b6e |
FILE *tmp = NULL; |
e3aaff8e |
|
32c970f8 |
|
55216b6e |
while(!bound && portscan--) { |
8000d078 |
if((port = cli_rndnum(60000)) < 1024) |
e3aaff8e |
port += 2139;
memset((char *) &server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(port); |
a0283d44 |
if((cpt = cfgopt(copt, "TCPAddr"))) { |
d9855a95 |
pthread_mutex_lock(&gh_mutex);
if((he = gethostbyname(cpt->strarg)) == 0) { |
a0283d44 |
logg("!gethostbyname(%s) error: %s\n", cpt->strarg);
mdprintf(odesc, "gethostbyname(%s) ERROR\n", cpt->strarg); |
d9855a95 |
pthread_mutex_unlock(&gh_mutex); |
a0283d44 |
return -1;
}
server.sin_addr = *(struct in_addr *) he->h_addr_list[0]; |
07b80ad3 |
pthread_mutex_unlock(&gh_mutex); |
a0283d44 |
} else
server.sin_addr.s_addr = INADDR_ANY; |
e3aaff8e |
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
continue;
if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) == -1)
close(sockfd);
else |
55216b6e |
bound = 1; |
e3aaff8e |
} |
32c970f8 |
if((cpt = cfgopt(copt, "ReadTimeout"))) |
1e35d807 |
timeout = cpt->numarg; |
32c970f8 |
else |
1e35d807 |
timeout = CL_DEFAULT_SCANTIMEOUT; |
32c970f8 |
if(timeout == 0) |
5b9ac68d |
timeout = -1; |
e3aaff8e |
|
55216b6e |
if(!bound && !portscan) { |
e3aaff8e |
logg("!ScanStream: Can't find any free port.\n"); |
65a18d2e |
mdprintf(odesc, "Can't find any free port. ERROR\n"); |
32c970f8 |
close(sockfd); |
e3aaff8e |
return -1;
} else {
listen(sockfd, 1);
mdprintf(odesc, "PORT %d\n", port);
}
|
32c970f8 |
switch(retval = poll_fd(sockfd, timeout)) {
case 0: /* timeout */ |
65a18d2e |
mdprintf(odesc, "Accept timeout. ERROR\n"); |
32c970f8 |
logg("!ScanStream: accept timeout.\n");
close(sockfd);
return -1;
case -1: |
65a18d2e |
mdprintf(odesc, "Accept poll. ERROR\n"); |
32c970f8 |
logg("!ScanStream: accept poll failed.\n");
close(sockfd);
return -1; |
c238ac42 |
}
|
e3aaff8e |
if((acceptd = accept(sockfd, NULL, NULL)) == -1) {
close(sockfd);
mdprintf(odesc, "accept() ERROR\n");
logg("!ScanStream: accept() failed.\n");
return -1;
}
|
55216b6e |
logg("*Accepted connection on port %d, fd %d\n", port, acceptd); |
8139fd99 |
|
32c970f8 |
if((tmp = tmpfile()) == NULL) {
shutdown(sockfd, 2);
close(sockfd);
close(acceptd); |
65a18d2e |
mdprintf(odesc, "tempfile() failed. ERROR\n"); |
32c970f8 |
logg("!ScanStream: Can't create temporary file.\n");
return -1;
}
tmpd = fileno(tmp);
if((cpt = cfgopt(copt, "StreamMaxLength")))
maxsize = cpt->numarg; |
c6dbfbcb |
else
maxsize = CL_DEFAULT_STREAMMAXLEN;
|
32c970f8 |
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) { |
e3aaff8e |
shutdown(sockfd, 2);
close(sockfd); |
55216b6e |
close(acceptd); |
32c970f8 |
mdprintf(odesc, "Temporary file -> write ERROR\n");
logg("!ScanStream: Can't write to temporary file.\n");
if(tmp)
fclose(tmp); |
e3aaff8e |
return -1;
}
|
32c970f8 |
if(maxsize && (size + btread >= maxsize)) {
btread = (maxsize - size); /* only read up to max */ |
e3aaff8e |
|
32c970f8 |
if(!btread) {
logg("^ScanStream: Size limit reached ( max: %d)\n", maxsize);
break; /* Scan what we have */ |
c238ac42 |
} |
e3aaff8e |
} |
32c970f8 |
} |
e3aaff8e |
|
32c970f8 |
switch(retval) {
case 0: /* timeout */ |
720fd280 |
mdprintf(odesc, "read timeout ERROR\n");
logg("!ScanStream: read timeout.\n"); |
32c970f8 |
case -1: |
720fd280 |
mdprintf(odesc, "read poll ERROR\n");
logg("!ScanStream: read poll failed.\n"); |
32c970f8 |
} |
e3aaff8e |
|
32c970f8 |
lseek(tmpd, 0, SEEK_SET);
ret = cl_scandesc(tmpd, &virname, scanned, root, limits, options);
if(tmp)
fclose(tmp); |
e3aaff8e |
close(acceptd);
close(sockfd);
if(ret == CL_VIRUS) {
mdprintf(odesc, "stream: %s FOUND\n", virname);
logg("stream: %s FOUND\n", virname); |
9cb8aa62 |
virusaction(virname, copt); |
e3aaff8e |
} else if(ret != CL_CLEAN) { |
049a18b9 |
mdprintf(odesc, "stream: %s ERROR\n", cl_strerror(ret));
logg("stream: %s ERROR\n", cl_strerror(ret)); |
ee039e40 |
} else { |
e3aaff8e |
mdprintf(odesc, "stream: OK\n"); |
ee039e40 |
if (logok)
logg("stream: OK\n");
} |
e3aaff8e |
return ret;
} |