2
* Copyright (C) 2002 - 2007 Tomasz Kojm <tkojm@clamav.net>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 2 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
#include "clamav-config.h"
30
#include <sys/types.h>
50
#include "shared/options.h"
51
#include "shared/output.h"
52
#include "shared/misc.h"
54
#include "libclamav/clamav.h"
55
#include "libclamav/others.h"
57
int treewalk(const char *dirname, struct cl_engine *engine, const struct passwd *user, const struct optstruct *opt, const struct cl_limits *limits, unsigned int options, unsigned int depth)
63
int scanret = 0, included;
64
unsigned int maxdepth;
65
const struct optnode *optnode;
69
if(opt_check(opt, "exclude-dir")) {
70
argument = opt_firstarg(opt, "exclude-dir", &optnode);
72
if(match_regex(dirname, argument) == 1) {
74
logg("~%s: Excluded\n", dirname);
77
argument = opt_nextarg(&optnode, "exclude-dir");
81
if(opt_check(opt, "include-dir")) {
83
argument = opt_firstarg(opt, "include-dir", &optnode);
84
while(argument && !included) {
85
if(match_regex(dirname, argument) == 1) {
89
argument = opt_nextarg(&optnode, "include-dir");
94
logg("~%s: Excluded\n", dirname);
99
if(opt_check(opt, "max-dir-recursion"))
100
maxdepth = atoi(opt_arg(opt, "max-dir-recursion"));
110
if((dd = opendir(dirname)) != NULL) {
111
while((dent = readdir(dd))) {
112
#if !defined(C_INTERIX) && !defined(C_WINDOWS) && !defined(C_CYGWIN)
116
if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
117
/* build the full name */
118
fname = malloc(strlen(dirname) + strlen(dent->d_name) + 2);
119
sprintf(fname, "%s/%s", dirname, dent->d_name);
122
if(lstat(fname, &statbuf) != -1) {
123
if(S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode) && recursion) {
124
if(treewalk(fname, engine, user, opt, limits, options, depth) == 1)
127
if(S_ISREG(statbuf.st_mode))
128
scanret += scanfile(fname, engine, user, opt, limits, options);
138
logg("~%s: Can't open directory.\n", dirname);
152
int clamav_rmdirs(const char *dir)
154
return cli_rmdirs(dir);
157
int clamav_rmdirs(const char *dir)
166
switch(pid = fork()) {
172
if((user = getpwnam(CLAMAVUSER)) == NULL)
175
#ifdef HAVE_SETGROUPS
176
if(setgroups(1, &user->pw_gid)) {
177
fprintf(stderr, "ERROR: setgroups() failed.\n");
182
if(setgid(user->pw_gid)) {
183
fprintf(stderr, "ERROR: setgid(%d) failed.\n", (int) user->pw_gid);
187
if(setuid(user->pw_uid)) {
188
fprintf(stderr, "ERROR: setuid(%d) failed.\n", (int) user->pw_uid);
197
waitpid(pid, &status, 0);
198
if(WIFEXITED(status))
206
int fixperms(const char *dirname)
214
if((dd = opendir(dirname)) != NULL) {
215
while((dent = readdir(dd))) {
216
#if !defined(C_INTERIX) && !defined(C_WINDOWS) && !defined(C_CYGWIN)
220
if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
221
/* build full name */
222
fname = malloc(strlen(dirname) + strlen(dent->d_name) + 2);
223
sprintf(fname, "%s/%s", dirname, dent->d_name);
226
if(lstat(fname, &statbuf) != -1) {
227
if(S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) {
230
} else if(S_ISREG(statbuf.st_mode))
240
logg("~%s: Can't open directory.\n", dirname);
253
int du(const char *dirname, struct s_du *n)
260
if((dd = opendir(dirname)) != NULL) {
261
while((dent = readdir(dd))) {
262
#if !defined(C_INTERIX) && !defined(C_WINDOWS) && !defined(C_CYGWIN)
266
if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
269
/* build the full name */
270
fname = malloc(strlen(dirname) + strlen(dent->d_name) + 2);
271
sprintf(fname, "%s/%s", dirname, dent->d_name);
274
if(lstat(fname, &statbuf) != -1) {
275
if(S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) {
278
n->space += statbuf.st_size / 1024;
288
logg("~%s: Can't open directory.\n", dirname);