clamscan/treewalk.c
e3aaff8e
 /*
8ca8a18e
  *  Copyright (C) 2002 - 2007 Tomasz Kojm <tkojm@clamav.net>
e3aaff8e
  *
  *  This program is free software; you can redistribute it and/or modify
bb34cb31
  *  it under the terms of the GNU General Public License version 2 as
  *  published by the Free Software Foundation.
e3aaff8e
  *
  *  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
  */
 
6d6e8271
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
e3aaff8e
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
34f71e0e
 #ifdef	HAVE_UNISTD_H
e3aaff8e
 #include <unistd.h>
34f71e0e
 #endif
e3aaff8e
 #include <sys/stat.h>
 #include <sys/types.h>
34f71e0e
 #ifndef C_WINDOWS
e3aaff8e
 #include <sys/wait.h>
34f71e0e
 #endif
 #ifdef HAVE_PWD_H
 #include <pwd.h>
 #endif
 #ifdef HAVE_GRP_H
e3aaff8e
 #include <grp.h>
34f71e0e
 #endif
 #ifndef C_WINDOWS
e3aaff8e
 #include <dirent.h>
34f71e0e
 #endif
e3aaff8e
 #include <errno.h>
 
7a2997f1
 #include "global.h"
e3aaff8e
 #include "manager.h"
 #include "others.h"
 #include "treewalk.h"
 
7a2997f1
 #include "shared/options.h"
 #include "shared/output.h"
 #include "shared/misc.h"
 
 #include "libclamav/clamav.h"
34efd8f1
 #include "libclamav/others.h"
7a2997f1
 
 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)
e3aaff8e
 {
 	DIR *dd;
 	struct dirent *dent;
 	struct stat statbuf;
 	char *fname;
8e65f09c
 	int scanret = 0, included;
14dee074
 	unsigned int maxdepth;
6f030f8a
 	const struct optnode *optnode;
8e65f09c
 	char *argument;
 
 
7b8edc5c
     if(opt_check(opt, "exclude-dir")) {
 	argument = opt_firstarg(opt, "exclude-dir", &optnode);
8e65f09c
 	while(argument) {
 	    if(match_regex(dirname, argument) == 1) {
 		if(!printinfected)
0ae41a2d
 		    logg("%s: Excluded\n", dirname);
8e65f09c
 		return 0;
 	    }
7b8edc5c
 	    argument = opt_nextarg(&optnode, "exclude-dir");
8e65f09c
 	}
     }
 
7b8edc5c
    if(opt_check(opt, "include-dir")) {
8e65f09c
 	included = 0;
7b8edc5c
 	argument = opt_firstarg(opt, "include-dir", &optnode);
8e65f09c
 	while(argument && !included) {
 	    if(match_regex(dirname, argument) == 1) {
 		included = 1;
 		break;
 	    }
f417ccce
 	    argument = opt_nextarg(&optnode, "include-dir");
8e65f09c
 	}
 
 	if(!included) {
 	    if(!printinfected)
0ae41a2d
 		logg("%s: Excluded\n", dirname);
8e65f09c
 	    return 0;
 	}
     }
e3aaff8e
 
7b8edc5c
     if(opt_check(opt, "max-dir-recursion"))
         maxdepth = atoi(opt_arg(opt, "max-dir-recursion"));
14dee074
     else
         maxdepth = 15;
 
     if(depth > maxdepth)
 	return 0;
 
7a2997f1
     info.dirs++;
f08e26c6
     depth++;
e3aaff8e
 
     if((dd = opendir(dirname)) != NULL) {
 	while((dent = readdir(dd))) {
34f71e0e
 #if !defined(C_INTERIX) && !defined(C_WINDOWS) && !defined(C_CYGWIN)
feeaa333
 	    if(dent->d_ino)
 #endif
 	    {
e3aaff8e
 		if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
 		    /* build the full name */
8ca8a18e
 		    fname = malloc(strlen(dirname) + strlen(dent->d_name) + 2);
e3aaff8e
 		    sprintf(fname, "%s/%s", dirname, dent->d_name);
 
 		    /* stat the file */
 		    if(lstat(fname, &statbuf) != -1) {
d1eb32e7
 			if(S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode) && recursion) {
7a2997f1
 			    if(treewalk(fname, engine, user, opt, limits, options, depth) == 1)
d1eb32e7
 				scanret++;
 			} else {
e3aaff8e
 			    if(S_ISREG(statbuf.st_mode))
7a2997f1
 				scanret += scanfile(fname, engine, user, opt, limits, options);
d1eb32e7
 			}
e3aaff8e
 		    }
 		    free(fname);
 		}
 
 	    }
 	}
     } else {
 	if(!printinfected)
0ae41a2d
 	    logg("%s: Can't open directory.\n", dirname);
e3aaff8e
 	return 53;
     }
 
     closedir(dd);
 
     if(scanret)
 	return 1;
     else
 	return 0;
 
 }
 
34f71e0e
 #ifdef C_WINDOWS
 int clamav_rmdirs(const char *dir)
 {
     return cli_rmdirs(dir);
 }
 #else
e3aaff8e
 int clamav_rmdirs(const char *dir)
 {
 #ifndef C_CYGWIN
 	struct passwd *user;
 #endif
 	pid_t pid;
 	int status;
 
 
     switch(pid = fork()) {
 	case -1:
 	    return -1;
 	case 0:
 #ifndef C_CYGWIN
11f30313
 	    if(!geteuid()) { 
7a2997f1
 		if((user = getpwnam(CLAMAVUSER)) == NULL)
e3aaff8e
 		    return -3;
 
a7d9bef2
 #ifdef HAVE_SETGROUPS
9f51cb51
 		if(setgroups(1, &user->pw_gid)) {
 		    fprintf(stderr, "ERROR: setgroups() failed.\n");
 		    return -3;
 		}
a7d9bef2
 #endif
9f51cb51
 
 		if(setgid(user->pw_gid)) {
 		    fprintf(stderr, "ERROR: setgid(%d) failed.\n", (int) user->pw_gid);
 		    return -3;
 		}
 
 		if(setuid(user->pw_uid)) {
 		    fprintf(stderr, "ERROR: setuid(%d) failed.\n", (int) user->pw_uid);
 		    return -3;
 		}
e3aaff8e
 	    }
 #endif
66ba1785
 	    cli_rmdirs(dir);
e3aaff8e
 	    exit(0);
 	    break;
 	default:
 	    waitpid(pid, &status, 0);
 	    if(WIFEXITED(status))
 		return 0;
 	    else
 		return -2;
     }
 }
34f71e0e
 #endif
e3aaff8e
 
 int fixperms(const char *dirname)
 {
 	DIR *dd;
 	struct dirent *dent;
 	struct stat statbuf;
 	char *fname;
 	int scanret = 0;
 
     if((dd = opendir(dirname)) != NULL) {
 	while((dent = readdir(dd))) {
34f71e0e
 #if !defined(C_INTERIX) && !defined(C_WINDOWS) && !defined(C_CYGWIN)
feeaa333
 	    if(dent->d_ino)
 #endif
 	    {
e3aaff8e
 		if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
 		    /* build full name */
8ca8a18e
 		    fname = malloc(strlen(dirname) + strlen(dent->d_name) + 2);
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)) {
 			    chmod(fname, 0700);
 			    fixperms(fname);
6d65029a
 			} else if(S_ISREG(statbuf.st_mode))
e3aaff8e
 			    chmod(fname, 0700);
 		    }
 
 		    free(fname);
 		}
 	    }
 	}
     } else {
 	if(!printinfected)
0ae41a2d
 	    logg("%s: Can't open directory.\n", dirname);
e3aaff8e
 	return 53;
     }
 
     closedir(dd);
 
     if(scanret)
 	return 1;
     else
 	return 0;
 
 }
 
 int du(const char *dirname, struct s_du *n)
 {
 	DIR *dd;
 	struct dirent *dent;
 	struct stat statbuf;
 	char *fname;
 
     if((dd = opendir(dirname)) != NULL) {
 	while((dent = readdir(dd))) {
34f71e0e
 #if !defined(C_INTERIX) && !defined(C_WINDOWS) && !defined(C_CYGWIN)
feeaa333
 	    if(dent->d_ino)
 #endif
 	    {
e3aaff8e
 		if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
 		    n->files++;
 
 		    /* build the full name */
8ca8a18e
 		    fname = malloc(strlen(dirname) + strlen(dent->d_name) + 2);
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)) {
 			    du(fname, n);
 			} else {
 			    n->space += statbuf.st_size / 1024;
 			}
 		    }
 
 		    free(fname);
 		}
 	    }
 	}
     } else {
 	if(!printinfected)
0ae41a2d
 	    logg("%s: Can't open directory.\n", dirname);
e3aaff8e
 	return 53;
     }
 
     closedir(dd);
 
     return 0;
 }