e3aaff8e |
/* |
0fc63750 |
* Copyright (C) 2002 - 2005 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 |
48b7b4a7 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA. |
e3aaff8e |
*
* Sat May 18 15:23:21 CEST 2002: included cpu autodetection from Magnus Ekdahl
* Wed Mar 5 03:45:31 CET 2003: included --move code from Damien Curtain
*/
|
5c07666a |
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
|
e3aaff8e |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h> |
4790a32f |
#include <utime.h> |
e3aaff8e |
#include <grp.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <clamav.h>
#include <errno.h>
#include "defaults.h"
#include "others.h"
#include "options.h"
#include "manager.h"
#include "treewalk.h"
#include "shared.h" |
2d70a403 |
#include "str.h"
#include "strrcpy.h" |
afb48b28 |
#include "memory.h"
#include "output.h" |
7b304dee |
#include "misc.h" |
8000d078 |
#include "../libclamav/others.h" |
83fa5305 |
#include "../libclamav/matcher-ac.h" |
e3aaff8e |
#ifdef C_LINUX
dev_t procdev;
#endif
|
8000d078 |
|
e3aaff8e |
int scanmanager(const struct optstruct *opt)
{
mode_t fmode; |
493b5445 |
int ret = 0, compression = 0, fmodeint, options = 0, i, x; |
d6449522 |
unsigned int dboptions = 0; |
e3aaff8e |
struct cl_node *trie = NULL;
struct cl_limits *limits = NULL;
struct passwd *user = NULL;
struct stat sb; |
b782aece |
char *fullpath = NULL, cwd[1024]; |
7b304dee |
|
e3aaff8e |
/* njh@bandsman.co.uk: BeOS */ |
e871e527 |
#if !defined(C_CYGWIN) && !defined(C_OS2) && !defined(C_BEOS) |
11f30313 |
if(!geteuid()) { |
e3aaff8e |
if((user = getpwnam(UNPUSER)) == NULL) { |
0ae41a2d |
logg("^Can't get information about user "UNPUSER"\n"); |
e3aaff8e |
exit(60); /* this is critical problem, so we just exit here */
}
}
#endif
|
84446268 |
if(optl(opt, "unzip") || optl(opt, "unrar") || optl(opt, "arj") ||
optl(opt, "unzoo") || optl(opt, "jar") || optl(opt, "lha") ||
optl(opt, "tar") || optl(opt, "tgz") || optl(opt, "deb")) |
e3aaff8e |
compression = 1;
|
f51e962f |
if(optl(opt, "hwaccel"))
dboptions |= CL_DB_HWACCEL;
|
d6449522 |
if(optl(opt, "no-phishing"))
dboptions |= CL_DB_NOPHISHING; |
e3aaff8e |
|
83fa5305 |
if(optl(opt, "dev-ac-only")) {
dboptions |= CL_DB_ACONLY;
if(optl(opt, "dev-ac-depth"))
cli_ac_setdepth(atoi(getargl(opt, "dev-ac-depth")));
}
|
e3aaff8e |
if(optc(opt, 'd')) { |
d6449522 |
if((ret = cl_load(getargc(opt, 'd'), &trie, &claminfo.signs, dboptions))) {
logg("^%s\n", cl_strerror(ret));
return 50; |
e3aaff8e |
}
} else { |
98ce643b |
char *dbdir = freshdbdir(); |
908db4df |
|
d6449522 |
if((ret = cl_load(dbdir, &trie, &claminfo.signs, dboptions))) { |
0ae41a2d |
logg("^%s\n", cl_strerror(ret)); |
98ce643b |
free(dbdir); |
e3aaff8e |
return 50;
} |
98ce643b |
free(dbdir); |
e3aaff8e |
}
if(!trie) { |
0ae41a2d |
logg("^Can't initialize the virus database\n"); |
e3aaff8e |
return 50;
}
|
8000d078 |
if((ret = cl_build(trie)) != 0) { |
0ae41a2d |
logg("^Database initialization error: %s\n", cl_strerror(ret));; |
2d70a403 |
return 50;
} |
e3aaff8e |
/* set (default) limits */
limits = (struct cl_limits *) calloc(1, sizeof(struct cl_limits));
if(optl(opt, "max-space")) {
char *cpy, *ptr;
ptr = getargl(opt, "max-space");
if(tolower(ptr[strlen(ptr) - 1]) == 'm') {
cpy = mcalloc(strlen(ptr), sizeof(char)); |
658f19f8 |
strncpy(cpy, ptr, strlen(ptr) - 1); |
e3aaff8e |
limits->maxfilesize = atoi(cpy) * 1024 * 1024;
free(cpy);
} else
limits->maxfilesize = atoi(ptr) * 1024;
} else
limits->maxfilesize = 10485760;
if(optl(opt, "max-files"))
limits->maxfiles = atoi(getargl(opt, "max-files"));
else
limits->maxfiles = 500;
if(optl(opt, "max-recursion"))
limits->maxreclevel = atoi(getargl(opt, "max-recursion"));
else |
f24181ef |
limits->maxreclevel = 8; |
e3aaff8e |
|
467f8b1e |
if(optl(opt, "max-ratio"))
limits->maxratio = atoi(getargl(opt, "max-ratio"));
else |
0fc63750 |
limits->maxratio = 250; |
e3aaff8e |
|
6ef42bc3 |
/* set options */
if(optl(opt, "disable-archive") || optl(opt, "no-archive")) |
3805ebcb |
options &= ~CL_SCAN_ARCHIVE; |
6ef42bc3 |
else |
3805ebcb |
options |= CL_SCAN_ARCHIVE; |
6ef42bc3 |
if(optl(opt, "detect-broken")) |
453581ae |
options |= CL_SCAN_BLOCKBROKEN; |
6ef42bc3 |
if(optl(opt, "block-encrypted")) |
08d6b1e3 |
options |= CL_SCAN_BLOCKENCRYPTED; |
6ef42bc3 |
|
d272908a |
if(optl(opt, "block-max")) |
3805ebcb |
options |= CL_SCAN_BLOCKMAX; |
d272908a |
|
6ef42bc3 |
if(optl(opt, "no-pe")) |
3805ebcb |
options &= ~CL_SCAN_PE; |
6ef42bc3 |
else |
3805ebcb |
options |= CL_SCAN_PE; |
6ef42bc3 |
if(optl(opt, "no-ole2")) |
3805ebcb |
options &= ~CL_SCAN_OLE2; |
6ef42bc3 |
else |
3805ebcb |
options |= CL_SCAN_OLE2; |
6ef42bc3 |
if(optl(opt, "no-html")) |
3805ebcb |
options &= ~CL_SCAN_HTML; |
6ef42bc3 |
else |
3805ebcb |
options |= CL_SCAN_HTML; |
6ef42bc3 |
if(optl(opt, "no-mail")) { |
3805ebcb |
options &= ~CL_SCAN_MAIL; |
6ef42bc3 |
} else { |
3805ebcb |
options |= CL_SCAN_MAIL; |
6ef42bc3 |
if(optl(opt, "mail-follow-urls")) |
3f0112d0 |
#ifdef WITH_CURL |
3805ebcb |
options |= CL_SCAN_MAILURL; |
3f0112d0 |
#else
logg("^Support for URLs downloading with libcurl not compiled in\n");
#endif |
6ef42bc3 |
}
|
1b661cef |
if(optl(opt, "no-algo"))
options &= ~CL_SCAN_ALGO;
else
options |= CL_SCAN_ALGO;
|
e3aaff8e |
#ifdef C_LINUX |
d9b55a82 |
procdev = (dev_t) 0; |
53c7b870 |
if(stat("/proc", &sb) != -1 && !sb.st_size) |
e3aaff8e |
procdev = sb.st_dev;
#endif
/* check filetype */ |
2d70a403 |
if(opt->filename == NULL || strlen(opt->filename) == 0) {
/* we need full path for some reasons (eg. archive handling) */ |
b782aece |
if(!getcwd(cwd, sizeof(cwd))) { |
0ae41a2d |
logg("^Can't get absolute pathname of current working directory\n"); |
2d70a403 |
ret = 57;
} else |
6ef42bc3 |
ret = scandirs(cwd, trie, user, opt, limits, options); |
2d70a403 |
} else if(!strcmp(opt->filename, "-")) { /* read data from stdin */ |
6ef42bc3 |
ret = checkstdin(trie, limits, options); |
e3aaff8e |
} else { |
2d70a403 |
char *thefilename; |
493b5445 |
for (x = 0; (thefilename = cli_strtok(opt->filename, x, "\t")) != NULL; x++) { |
2d70a403 |
if((fmodeint = fileinfo(thefilename, 2)) == -1) { |
0ae41a2d |
logg("^Can't access file %s\n", thefilename); |
2d70a403 |
perror(thefilename);
ret = 56;
} else { |
a8b056dc |
int slash = 1;
for(i = strlen(thefilename) - 1; i > 0 && slash; i--) {
if(thefilename[i] == '/')
thefilename[i] = 0;
else
slash = 0;
}
|
2d70a403 |
fmode = (mode_t) fmodeint;
|
a8b056dc |
if(compression && (thefilename[0] != '/' && thefilename[0] != '\\' && thefilename[1] != ':')) { |
2d70a403 |
/* we need to complete the path */ |
b782aece |
if(!getcwd(cwd, sizeof(cwd))) { |
0ae41a2d |
logg("^Can't get absolute pathname of current working directory\n"); |
ea2e5b5f |
free(limits); |
2d70a403 |
return 57;
} else {
fullpath = mcalloc(512, sizeof(char)); |
e3aaff8e |
#ifdef NO_SNPRINTF |
2d70a403 |
sprintf(fullpath, "%s/%s", cwd, thefilename); |
e3aaff8e |
#else |
2d70a403 |
snprintf(fullpath, 512, "%s/%s", cwd, thefilename); |
e3aaff8e |
#endif |
0ae41a2d |
logg("*Full path: %s\n", fullpath); |
2d70a403 |
}
} else |
a8b056dc |
fullpath = thefilename; |
2d70a403 |
switch(fmode & S_IFMT) {
case S_IFREG: |
6ef42bc3 |
ret = scanfile(fullpath, trie, user, opt, limits, options); |
2d70a403 |
break;
case S_IFDIR: |
6ef42bc3 |
ret = scandirs(fullpath, trie, user, opt, limits, options); |
2d70a403 |
break;
default: |
0ae41a2d |
logg("^Not supported file type (%s)\n", thefilename); |
2d70a403 |
ret = 52;
} |
e3aaff8e |
|
a8b056dc |
if(compression && (thefilename[0] != '/' && thefilename[0] != '\\' && thefilename[1] != ':')) { |
2d70a403 |
free(fullpath);
fullpath = NULL;
}
}
free(thefilename); |
e3aaff8e |
}
}
/* free the trie */ |
8000d078 |
cl_free(trie); |
e3aaff8e |
free(limits);
/* overwrite return code */
if(claminfo.ifiles)
ret = 1;
else if(ret < 50) /* hopefully no error detected */
ret = 0; /* just make sure it's 0 */
return ret;
}
|
6ef42bc3 |
int scanfile(const char *filename, struct cl_node *root, const struct passwd *user, const struct optstruct *opt, const struct cl_limits *limits, int options) |
e3aaff8e |
{ |
914ce12d |
int ret, included, printclean = 1; |
e3aaff8e |
struct optnode *optnode;
char *argument;
#ifdef C_LINUX
struct stat sb;
/* argh, don't scan /proc files */
if(procdev)
if(stat(filename, &sb) != -1)
if(sb.st_dev == procdev) { |
8515ab9e |
if(!printinfected) |
0ae41a2d |
logg("%s: Excluded (/proc)\n", filename); |
e3aaff8e |
return 0;
} |
266f3967 |
#endif |
8e65f09c |
|
e3aaff8e |
if(optl(opt, "exclude")) {
argument = getfirstargl(opt, "exclude", &optnode); |
8e65f09c |
while(argument) { |
266f3967 |
if(match_regex(filename, argument) == 1) { |
e3aaff8e |
if(!printinfected) |
0ae41a2d |
logg("%s: Excluded\n", filename); |
e3aaff8e |
return 0;
}
argument = getnextargl(&optnode, "exclude");
}
} |
8e65f09c |
|
266f3967 |
if(optl(opt, "include")) { |
e3aaff8e |
included = 0; |
8e65f09c |
argument = getfirstargl(opt, "include", &optnode);
while(argument && !included) {
if(match_regex(filename, argument) == 1) { |
e3aaff8e |
included = 1; |
8e65f09c |
break;
} |
e3aaff8e |
argument = getnextargl(&optnode, "include");
}
|
8e65f09c |
if(!included) { |
e3aaff8e |
if(!printinfected) |
0ae41a2d |
logg("%s: Excluded\n", filename); |
e3aaff8e |
return 0;
}
}
if(fileinfo(filename, 1) == 0) {
if(!printinfected) |
0ae41a2d |
logg("%s: Empty file\n", filename); |
e3aaff8e |
return 0;
}
|
2df4fcca |
if(geteuid())
if(checkaccess(filename, NULL, R_OK) != 1) {
if(!printinfected) |
0ae41a2d |
logg("%s: Access denied\n", filename); |
2df4fcca |
return 0;
}
claminfo.files++;
|
e3aaff8e |
/*
* check the extension - this is a special case, normally we don't need to
* do this (libclamav detects archive by its magic string), but here we
* want to know the exit code from internal unpacker and try to use
* external (if provided) when internal cannot extract data.
*/
|
3805ebcb |
if((cli_strbcasestr(filename, ".zip") || cli_strbcasestr(filename, ".rar")) && (options & CL_SCAN_ARCHIVE)) { |
e3aaff8e |
/* try to use internal archivers */ |
888030a5 |
if((ret = checkfile(filename, root, limits, options, 1)) == CL_VIRUS) { |
e3aaff8e |
if(optl(opt, "remove")) {
if(unlink(filename)) { |
8e65f09c |
logg("%s: Can't remove\n", filename); |
e3aaff8e |
claminfo.notremoved++;
} else { |
8e65f09c |
logg("%s: Removed\n", filename); |
e3aaff8e |
}
} else if (optl(opt, "move"))
move_infected(filename, opt);
return 1;
|
5f20a6ce |
} else if(ret == CL_CLEAN) { |
e3aaff8e |
return 0; |
5f20a6ce |
} else if(ret == 54) {
return ret;
}
|
e3aaff8e |
/* in other case try to continue with external archivers */ |
888030a5 |
options &= ~CL_SCAN_ARCHIVE; /* and disable decompression for the checkfile() below */ |
914ce12d |
printclean = 0; |
e3aaff8e |
}
|
afb48b28 |
if((cli_strbcasestr(filename, ".zip") && optl(opt, "unzip"))
|| (cli_strbcasestr(filename, ".rar") && optl(opt, "unrar"))
|| (cli_strbcasestr(filename, ".arj") && optl(opt, "arj"))
|| (cli_strbcasestr(filename, ".zoo") && optl(opt, "unzoo"))
|| (cli_strbcasestr(filename, ".jar") && optl(opt, "jar"))
|| (cli_strbcasestr(filename, ".lzh") && optl(opt, "lha"))
|| (cli_strbcasestr(filename, ".tar") && optl(opt, "tar"))
|| (cli_strbcasestr(filename, ".deb") && optl(opt, "deb"))
|| ((cli_strbcasestr(filename, ".tar.gz") || cli_strbcasestr(filename, ".tgz")) |
e3aaff8e |
&& (optl(opt, "tgz") || optl(opt, "deb"))) ) {
/* check permissions */ |
56bfccb2 |
switch(checkaccess(filename, UNPUSER, R_OK)) { |
e3aaff8e |
case -1: |
0ae41a2d |
logg("^Can't get information about user "UNPUSER"\n"); |
56bfccb2 |
exit(60); /* this is a critical problem so we just exit here */ |
e3aaff8e |
case -2: |
0ae41a2d |
logg("^Can't fork\n"); |
56bfccb2 |
exit(61); |
e3aaff8e |
case 0: /* read access denied */ |
11f30313 |
if(geteuid()) { |
e3aaff8e |
if(!printinfected) |
0ae41a2d |
logg("%s: Access denied to archive\n", filename); |
e3aaff8e |
} else {
if(limits && limits->maxfilesize)
if(fileinfo(filename, 1) / 1024 > limits->maxfilesize) {
if(!printinfected) |
0ae41a2d |
logg("%s: Archive too big\n", filename); |
e3aaff8e |
return 0;
}
|
6ef42bc3 |
return(scandenied(filename, root, user, opt, limits, options)); |
e3aaff8e |
}
return 0;
case 1: |
6ef42bc3 |
return(scancompressed(filename, root, user, opt, limits, options)); |
e3aaff8e |
}
}
|
914ce12d |
if((ret = checkfile(filename, root, limits, options, printclean)) == CL_VIRUS) { |
e3aaff8e |
if(optl(opt, "remove")) {
if(unlink(filename)) { |
8e65f09c |
logg("%s: Can't remove\n", filename); |
e3aaff8e |
claminfo.notremoved++;
} else { |
8e65f09c |
logg("%s: Removed\n", filename); |
e3aaff8e |
}
} else if (optl(opt, "move"))
move_infected(filename, opt);
}
return ret;
}
/* it has guaranteed read access to the archive */ |
6ef42bc3 |
int scancompressed(const char *filename, struct cl_node *root, const struct passwd *user, const struct optstruct *opt, const struct cl_limits *limits, int options) |
e3aaff8e |
{
int ret = 0;
char *tmpdir, *gendir, *userprg;
struct stat statbuf;
|
14dee074 |
|
e3aaff8e |
stat(filename, &statbuf);
if(!S_ISREG(statbuf.st_mode)) { |
0ae41a2d |
logg("^Suspected archive %s is not a regular file\n", filename); |
e3aaff8e |
return 0; /* hmm ? */
}
/* check write access */
|
590135f9 |
tmpdir = getenv("TMPDIR"); |
e3aaff8e |
if(tmpdir == NULL)
#ifdef P_tmpdir
tmpdir = P_tmpdir;
#else
tmpdir = "/tmp";
#endif
|
56bfccb2 |
if(checkaccess(tmpdir, UNPUSER, W_OK) != 1) { |
0ae41a2d |
logg("^Can't write to the temporary directory\n"); |
e3aaff8e |
exit(64);
}
/* generate the temporary directory */
|
8000d078 |
gendir = cli_gentemp(tmpdir); |
e3aaff8e |
if(mkdir(gendir, 0700)) { |
0ae41a2d |
logg("^Can't create the temporary directory %s\n", gendir); |
e3aaff8e |
exit(63); /* critical */
}
|
75ccac9f |
#ifndef C_OS2 |
e3aaff8e |
if(user)
chown(gendir, user->pw_uid, user->pw_gid); |
75ccac9f |
#endif |
e3aaff8e |
/* unpack file - as unprivileged user */ |
afb48b28 |
if(cli_strbcasestr(filename, ".zip")) { |
0aaf4b37 |
char *args[] = { "unzip", "-P", "clam", "-o", NULL, NULL };
/* Sun's SUNWspro C compiler doesn't allow direct initialisation
* with a variable
*/
args[4] = (char *) filename; |
e3aaff8e |
if((userprg = getargl(opt, "unzip")))
ret = clamav_unpack(userprg, args, gendir, user, opt);
else
ret = clamav_unpack("unzip", args, gendir, user, opt);
|
afb48b28 |
} else if(cli_strbcasestr(filename, ".rar")) { |
0aaf4b37 |
char *args[] = { "unrar", "x", "-p-", "-y", NULL, NULL };
args[4] = (char *) filename; |
e3aaff8e |
if((userprg = getargl(opt, "unrar")))
ret = clamav_unpack(userprg, args, gendir, user, opt);
else
ret = clamav_unpack("unrar", args, gendir, user, opt);
|
afb48b28 |
} else if(cli_strbcasestr(filename, ".arj")) { |
0aaf4b37 |
char *args[] = { "arj", "x","-y", NULL, NULL };
args[3] = (char *) filename; |
e3aaff8e |
if((userprg = getargl(opt, "arj")))
ret = clamav_unpack(userprg, args, gendir, user, opt);
else
ret = clamav_unpack("arj", args, gendir, user, opt);
|
afb48b28 |
} else if(cli_strbcasestr(filename, ".zoo")) { |
0aaf4b37 |
char *args[] = { "unzoo", "-x","-j","./", NULL, NULL };
args[4] = (char *) filename; |
e3aaff8e |
if((userprg = getargl(opt, "unzoo")))
ret = clamav_unpack(userprg, args, gendir, user, opt);
else
ret = clamav_unpack("unzoo", args, gendir, user, opt);
|
afb48b28 |
} else if(cli_strbcasestr(filename, ".jar")) { |
0aaf4b37 |
char *args[] = { "unzip", "-P", "clam", "-o", NULL, NULL };
args[4] = (char *) filename; |
e3aaff8e |
if((userprg = getargl(opt, "jar")))
ret = clamav_unpack(userprg, args, gendir, user, opt);
else
ret = clamav_unpack("unzip", args, gendir, user, opt);
|
afb48b28 |
} else if(cli_strbcasestr(filename, ".lzh")) { |
0aaf4b37 |
char *args[] = { "lha", "xf", NULL, NULL };
args[2] = (char *) filename; |
e3aaff8e |
if((userprg = getargl(opt, "lha")))
ret = clamav_unpack(userprg, args, gendir, user, opt);
else
ret = clamav_unpack("lha", args, gendir, user, opt);
|
afb48b28 |
} else if(cli_strbcasestr(filename, ".tar")) { |
0aaf4b37 |
char *args[] = { "tar", "-xpvf", NULL, NULL };
args[2] = (char *) filename; |
e3aaff8e |
if((userprg = getargl(opt, "tar")))
ret = clamav_unpack(userprg, args, gendir, user, opt);
else
ret = clamav_unpack("tar", args, gendir, user, opt);
|
afb48b28 |
} else if(cli_strbcasestr(filename, ".deb")) { |
0aaf4b37 |
char *args[] = { "ar", "x", NULL, NULL };
args[2] = (char *) filename; |
e3aaff8e |
if((userprg = getargl(opt, "deb")))
ret = clamav_unpack(userprg, args, gendir, user, opt);
else
ret = clamav_unpack("ar", args, gendir, user, opt);
|
afb48b28 |
} else if((cli_strbcasestr(filename, ".tar.gz") || cli_strbcasestr(filename, ".tgz"))) { |
0aaf4b37 |
char *args[] = { "tar", "-zxpvf", NULL, NULL };
args[2] = (char *) filename; |
e3aaff8e |
if((userprg = getargl(opt, "tgz")))
ret = clamav_unpack(userprg, args, gendir, user, opt);
else
ret = clamav_unpack("tar", args, gendir, user, opt);
}
/* fix permissions of extracted files */
fixperms(gendir);
|
14dee074 |
if(!ret) { /* execute successful */
short oldrec = recursion;
recursion = 1;
ret = treewalk(gendir, root, user, opt, limits, options, 1);
recursion = oldrec;
} |
e3aaff8e |
/* remove the directory - as clamav */ |
d1eb32e7 |
if(!optl(opt, "leave-temps"))
clamav_rmdirs(gendir); |
e3aaff8e |
/* free gendir - it's not necessary now */
free(gendir);
switch(ret) {
case -1: |
0ae41a2d |
logg("^Can't fork()\n"); |
e3aaff8e |
exit(61); /* this is critical problem, so we just exit here */
case -2: |
0ae41a2d |
logg("^Can't execute some unpacker. Check paths and permissions on the temporary directory\n"); |
e3aaff8e |
/* This is no longer a critical error (since 0.24). We scan
* raw archive.
*/ |
888030a5 |
if((ret = checkfile(filename, root, limits, 0, 0)) == CL_VIRUS) { |
e3aaff8e |
if(optl(opt, "remove")) {
if(unlink(filename)) { |
8e65f09c |
logg("%s: Can't remove\n", filename); |
e3aaff8e |
claminfo.notremoved++;
} else { |
8e65f09c |
logg("%s: Removed\n", filename); |
e3aaff8e |
}
} else if (optl(opt, "move"))
move_infected(filename, opt);
}
return ret;
case -3:
return 0;
case 0: |
14dee074 |
/* no viruses found in archive, we scan just in case a raw file |
e3aaff8e |
*/ |
888030a5 |
if((ret = checkfile(filename, root, limits, 0, 1)) == CL_VIRUS) { |
e3aaff8e |
if(optl(opt, "remove")) {
if(unlink(filename)) { |
8e65f09c |
logg("%s: Can't remove\n", filename); |
e3aaff8e |
claminfo.notremoved++;
} else { |
8e65f09c |
logg("%s: Removed\n", filename); |
e3aaff8e |
}
} else if (optl(opt, "move"))
move_infected(filename, opt);
}
return ret;
case 1: |
14dee074 |
logg("%s: Infected.Archive FOUND\n", filename); |
af22ece1 |
if(bell) |
ed026d36 |
fprintf(stderr, "\007"); |
af22ece1 |
|
e3aaff8e |
if(optl(opt, "remove")) {
if(unlink(filename)) { |
8e65f09c |
logg("%s: Can't remove\n", filename); |
e3aaff8e |
claminfo.notremoved++;
} else { |
8e65f09c |
logg("%s: Removed\n", filename); |
e3aaff8e |
}
} else if (optl(opt, "move"))
move_infected(filename, opt);
return 1;
default: |
0ae41a2d |
logg("^Strange value (%d) returned in scancompressed()\n", ret); |
e3aaff8e |
return 0;
}
}
|
6ef42bc3 |
int scandenied(const char *filename, struct cl_node *root, const struct passwd *user, const struct optstruct *opt, const struct cl_limits *limits, int options) |
e3aaff8e |
{
char *tmpdir, *gendir, *tmpfile, *pt;
struct stat statbuf;
int ret;
stat(filename, &statbuf);
if(!S_ISREG(statbuf.st_mode)) { |
0ae41a2d |
logg("^Suspected archive %s is not a regular file\n", filename); |
e3aaff8e |
return 0;
}
/* check write access */
|
590135f9 |
tmpdir = getenv("TMPDIR"); |
e3aaff8e |
if(tmpdir == NULL)
#ifdef P_tmpdir
tmpdir = P_tmpdir;
#else
tmpdir = "/tmp";
#endif
|
56bfccb2 |
if(checkaccess(tmpdir, UNPUSER, W_OK) != 1) { |
0ae41a2d |
logg("^Can't write to the temporary directory %s\n", tmpdir); |
e3aaff8e |
exit(64);
}
/* generate the temporary directory */ |
8000d078 |
gendir = cli_gentemp(tmpdir); |
e3aaff8e |
if(mkdir(gendir, 0700)) { |
0ae41a2d |
logg("^Can't create the temporary directory %s\n", gendir); |
e3aaff8e |
exit(63); /* critical */
}
tmpfile = (char *) mcalloc(strlen(gendir) + strlen(filename) + 10, sizeof(char));
pt = strrchr(filename, '/');
if(!pt)
pt = (char *) filename;
else
pt += 1;
sprintf(tmpfile, "%s/%s", gendir, pt);
if(filecopy(filename, tmpfile) == -1) { |
0ae41a2d |
logg("!I/O error\n"); |
e3aaff8e |
perror("copyfile()");
exit(58);
}
fixperms(gendir);
|
75ccac9f |
#ifndef C_OS2 |
e3aaff8e |
if(user) {
chown(gendir, user->pw_uid, user->pw_gid);
chown(tmpfile, user->pw_uid, user->pw_gid);
} |
75ccac9f |
#endif |
e3aaff8e |
|
14dee074 |
if((ret = treewalk(gendir, root, user, opt, limits, options, 1)) == 1) { |
e3aaff8e |
logg("(Real infected archive: %s)\n", filename);
if(optl(opt, "remove")) {
if(unlink(filename)) { |
8e65f09c |
logg("%s: Can't remove\n", filename); |
e3aaff8e |
claminfo.notremoved++;
} else { |
8e65f09c |
logg("%s: Removed\n", filename); |
e3aaff8e |
}
} else if (optl(opt, "move"))
move_infected(filename, opt);
}
/* remove the directory - as clamav */
clamav_rmdirs(gendir);
free(gendir);
free(tmpfile);
return ret;
}
|
6ef42bc3 |
int scandirs(const char *dirname, struct cl_node *root, const struct passwd *user, const struct optstruct *opt, const struct cl_limits *limits, int options) |
e3aaff8e |
{ |
14dee074 |
return treewalk(dirname, root, user, opt, limits, options, 1); |
e3aaff8e |
}
|
888030a5 |
int checkfile(const char *filename, const struct cl_node *root, const struct cl_limits *limits, int options, short printclean) |
e3aaff8e |
{
int fd, ret; |
fb787a06 |
const char *virname; |
e3aaff8e |
|
a9082ea2 |
|
0ae41a2d |
logg("*Scanning %s\n", filename); |
a9082ea2 |
|
e3aaff8e |
if((fd = open(filename, O_RDONLY)) == -1) { |
0ae41a2d |
logg("^Can't open file %s\n", filename); |
e3aaff8e |
return 54;
}
if((ret = cl_scandesc(fd, &virname, &claminfo.blocks, root, limits, options)) == CL_VIRUS) {
logg("%s: %s FOUND\n", filename, virname);
claminfo.ifiles++; |
af22ece1 |
if(bell) |
ed026d36 |
fprintf(stderr, "\007"); |
af22ece1 |
|
e3aaff8e |
} else if(ret == CL_CLEAN) { |
888030a5 |
if(!printinfected && printclean) |
914ce12d |
mprintf("%s: OK\n", filename); |
e3aaff8e |
} else
if(!printinfected) |
0ae41a2d |
logg("%s: %s\n", filename, cl_strerror(ret)); |
e3aaff8e |
close(fd);
return ret;
}
|
6ef42bc3 |
int checkstdin(const struct cl_node *root, const struct cl_limits *limits, int options) |
e3aaff8e |
{
int ret; |
6ef42bc3 |
const char *virname, *tmpdir;
char *file, buff[FILEBUFF];
FILE *fs;
/* check write access */
tmpdir = getenv("TMPDIR");
if(tmpdir == NULL)
#ifdef P_tmpdir
tmpdir = P_tmpdir;
#else
tmpdir = "/tmp";
#endif
if(checkaccess(tmpdir, UNPUSER, W_OK) != 1) { |
0ae41a2d |
logg("^Can't write to temporary directory\n"); |
6ef42bc3 |
return 64;
}
file = cli_gentemp(tmpdir);
if(!(fs = fopen(file, "wb"))) { |
0ae41a2d |
logg("^Can't open %s for writing\n", file); |
6ef42bc3 |
return 63;
}
while((ret = fread(buff, 1, FILEBUFF, stdin)))
fwrite(buff, 1, ret, fs); |
e3aaff8e |
|
6ef42bc3 |
fclose(fs); |
e3aaff8e |
|
0ae41a2d |
logg("*Checking %s\n", file); |
e3aaff8e |
claminfo.files++;
|
6ef42bc3 |
if((ret = cl_scanfile(file, &virname, &claminfo.blocks, root, limits, options)) == CL_VIRUS) { |
0ae41a2d |
logg("stdin: %s FOUND\n", virname); |
e3aaff8e |
claminfo.ifiles++; |
af22ece1 |
if(bell) |
ed026d36 |
fprintf(stderr, "\007"); |
af22ece1 |
|
e3aaff8e |
} else if(ret == CL_CLEAN) {
if(!printinfected) |
914ce12d |
mprintf("stdin: OK\n"); |
e3aaff8e |
} else
if(!printinfected) |
0ae41a2d |
logg("stdin: %s\n", cl_strerror(ret)); |
e3aaff8e |
|
6ef42bc3 |
unlink(file);
free(file); |
e3aaff8e |
return ret;
}
/*
* -1 -> can't fork
* -2 -> can't execute
* -3 -> external signal
* 0 -> OK
*/
int clamav_unpack(const char *prog, char **args, const char *tmpdir, const struct passwd *user, const struct optstruct *opt)
{
pid_t pid;
int status, wret, maxfiles, maxspace, fdevnull;
struct s_du n;
if(optl(opt, "max-files"))
maxfiles = atoi(getargl(opt, "max-files"));
else
maxfiles = 0;
if(optl(opt, "max-space")) {
char *cpy, *ptr;
ptr = getargl(opt, "max-space");
if(tolower(ptr[strlen(ptr) - 1]) == 'm') { /* megabytes */
cpy = mcalloc(strlen(ptr), sizeof(char)); |
658f19f8 |
strncpy(cpy, ptr, strlen(ptr) - 1); |
e3aaff8e |
maxspace = atoi(cpy) * 1024;
free(cpy);
} else /* default - kilobytes */
maxspace = atoi(ptr);
} else
maxspace = 0;
switch(pid = fork()) {
case -1:
return -1;
case 0:
#ifndef C_CYGWIN |
11f30313 |
if(!geteuid() && user) { |
a7d9bef2 |
#ifdef HAVE_SETGROUPS |
9f51cb51 |
if(setgroups(1, &user->pw_gid)) { |
8e65f09c |
fprintf(stderr, "ERROR: setgroups() failed\n"); |
9f51cb51 |
exit(1);
} |
a7d9bef2 |
#endif |
9f51cb51 |
if(setgid(user->pw_gid)) { |
8e65f09c |
fprintf(stderr, "ERROR: setgid(%d) failed\n", (int) user->pw_gid); |
9f51cb51 |
exit(1);
}
if(setuid(user->pw_uid)) { |
8e65f09c |
fprintf(stderr, "ERROR: setuid(%d) failed\n", (int) user->pw_uid); |
9f51cb51 |
exit(1);
} |
e3aaff8e |
}
#endif
chdir(tmpdir);
if(printinfected) {
fdevnull = open("/dev/null", O_WRONLY);
if(fdevnull == -1) { |
0ae41a2d |
logg("Non fatal error: cannot open /dev/null. Continuing with full output\n"); |
e3aaff8e |
printinfected = 0;
} else {
dup2(fdevnull,1);
dup2(fdevnull,2);
}
}
if(strchr(prog, '/')) /* we have full path */
execv(prog, args);
else
execvp(prog, args);
perror("execv(p)");
abort();
break;
default:
if(maxfiles || maxspace) {
while(!(wret = waitpid(pid, &status, WNOHANG))) {
memset(&n, 0, sizeof(struct s_du));
if(!du(tmpdir, &n))
if((maxfiles && n.files > maxfiles) || (maxspace && n.space > maxspace)) { |
0ae41a2d |
logg("*n.files: %d, n.space: %d\n", n.files, n.space); |
e3aaff8e |
kill(pid, 9); /* stop it immediately */
}
}
} else
waitpid(pid, &status, 0);
if(WIFSIGNALED(status)) {
switch(WTERMSIG(status)) {
case 9: |
0ae41a2d |
logg("\nUnpacker process %d stopped due to exceeded limits\n", pid); |
e3aaff8e |
return 0;
case 6: /* abort */ |
0ae41a2d |
logg("^Can't run %s\n", prog); |
e3aaff8e |
return -2;
default: |
0ae41a2d |
logg("^\nUnpacker stopped with external signal %d\n", WTERMSIG(status)); |
e3aaff8e |
return -3;
}
} else if(WIFEXITED(status))
return 0;
}
return 0;
}
void move_infected(const char *filename, const struct optstruct *opt)
{ |
4790a32f |
char *movedir, *movefilename, *tmp, numext[4 + 1];
struct stat fstat, mfstat;
int n, len, movefilename_size;
struct utimbuf ubuf; |
e3aaff8e |
|
266f3967 |
if(!(movedir = getargl(opt, "move"))) { |
e3aaff8e |
/* Should never reach here */ |
0ae41a2d |
logg("^getargc() returned NULL\n", filename); |
e3aaff8e |
claminfo.notmoved++;
return;
}
|
266f3967 |
if(access(movedir, W_OK|X_OK) == -1) { |
0ae41a2d |
logg("^error moving file '%s': cannot write to '%s': %s\n", filename, movedir, strerror(errno)); |
e3aaff8e |
claminfo.notmoved++;
return;
} |
266f3967 |
|
4790a32f |
if(!(tmp = strrchr(filename, '/')))
tmp = (char *) filename; |
266f3967 |
|
4790a32f |
movefilename_size = sizeof(char) * (strlen(movedir) + strlen(tmp) + sizeof(numext) + 2); |
266f3967 |
|
4790a32f |
if(!(movefilename = mmalloc(movefilename_size))) { |
0ae41a2d |
logg("^Memory allocation error\n"); |
4790a32f |
exit(71); |
e3aaff8e |
} |
266f3967 |
if(!(strrcpy(movefilename, movedir))) { |
0ae41a2d |
logg("^strrcpy() returned NULL\n"); |
e3aaff8e |
claminfo.notmoved++;
free(movefilename);
return;
} |
266f3967 |
|
4790a32f |
strcat(movefilename, "/");
|
266f3967 |
if(!(strcat(movefilename, tmp))) { |
0ae41a2d |
logg("^strcat() returned NULL\n"); |
e3aaff8e |
claminfo.notmoved++;
free(movefilename);
return;
} |
266f3967 |
|
e3aaff8e |
stat(filename, &fstat);
|
266f3967 |
if(!stat(movefilename, &mfstat)) { |
e3aaff8e |
if(fstat.st_ino == mfstat.st_ino) { /* It's the same file*/ |
4790a32f |
logg("File excluded '%s'\n", filename); |
e3aaff8e |
claminfo.notmoved++;
free(movefilename);
return; |
266f3967 |
} else {
/* file exists - try to append an ordinal number to the
* quranatined file in an attempt not to overwrite existing
* files in quarantine
*/
len = strlen(movefilename);
n = 0;
do {
/* reset the movefilename to it's initial value by
* truncating to the original filename length
*/
movefilename[len] = 0;
/* append .XXX */
sprintf(numext, ".%03d", n++);
strcat(movefilename, numext);
} while(!stat(movefilename, &mfstat) && (n < 1000));
} |
e3aaff8e |
} |
266f3967 |
|
4790a32f |
if(rename(filename, movefilename) == -1) {
if(filecopy(filename, movefilename) == -1) { |
0ae41a2d |
logg("^cannot move '%s' to '%s': %s\n", filename, movefilename, strerror(errno)); |
4790a32f |
claminfo.notmoved++;
free(movefilename);
return;
} |
e3aaff8e |
|
4790a32f |
chmod(movefilename, fstat.st_mode); |
75ccac9f |
#ifndef C_OS2 |
4790a32f |
chown(movefilename, fstat.st_uid, fstat.st_gid); |
75ccac9f |
#endif |
266f3967 |
|
4790a32f |
ubuf.actime = fstat.st_atime;
ubuf.modtime = fstat.st_mtime;
utime(movefilename, &ubuf);
if(unlink(filename)) { |
0ae41a2d |
logg("^cannot unlink '%s': %s\n", filename, strerror(errno)); |
4790a32f |
claminfo.notremoved++;
free(movefilename);
return;
} |
e3aaff8e |
}
|
4790a32f |
logg("%s: moved to '%s'\n", filename, movefilename);
|
e3aaff8e |
free(movefilename);
} |