clamscan/treewalk.c
b151ef55
 /*
3fa35dc1
  *  Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net>
b151ef55
  *
  *  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
30738099
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
b151ef55
  */
 
8b242bb9
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
b151ef55
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <grp.h>
 #include <dirent.h>
 #include <errno.h>
 
 #include "shared.h"
 #include "manager.h"
 #include "others.h"
 #include "options.h"
 #include "treewalk.h"
 #include "defaults.h"
36f2038b
 #include "memory.h"
 #include "output.h"
29af2c07
 #include "misc.h"
b151ef55
 
3fa35dc1
 int treewalk(const char *dirname, struct cl_node *root, const struct passwd *user, const struct optstruct *opt, const struct cl_limits *limits, int options, unsigned int depth)
b151ef55
 {
 	DIR *dd;
 	struct dirent *dent;
 	struct stat statbuf;
 	char *fname;
4cc291aa
 	int scanret = 0, included;
3fa35dc1
 	unsigned int maxdepth;
4cc291aa
 	struct optnode *optnode;
 	char *argument;
 
 
2bc31f05
     if(opt_check(opt, "exclude-dir")) {
 	argument = opt_firstarg(opt, "exclude-dir", &optnode);
4cc291aa
 	while(argument) {
 	    if(match_regex(dirname, argument) == 1) {
 		if(!printinfected)
f1c4563e
 		    logg("%s: Excluded\n", dirname);
4cc291aa
 		return 0;
 	    }
2bc31f05
 	    argument = opt_nextarg(&optnode, "exclude-dir");
4cc291aa
 	}
     }
 
2bc31f05
    if(opt_check(opt, "include-dir")) {
4cc291aa
 	included = 0;
2bc31f05
 	argument = opt_firstarg(opt, "include-dir", &optnode);
4cc291aa
 	while(argument && !included) {
 	    if(match_regex(dirname, argument) == 1) {
 		included = 1;
 		break;
 	    }
06d14eef
 	    argument = opt_nextarg(&optnode, "include-dir");
4cc291aa
 	}
 
 	if(!included) {
 	    if(!printinfected)
f1c4563e
 		logg("%s: Excluded\n", dirname);
4cc291aa
 	    return 0;
 	}
     }
b151ef55
 
2bc31f05
     if(opt_check(opt, "max-dir-recursion"))
         maxdepth = atoi(opt_arg(opt, "max-dir-recursion"));
3fa35dc1
     else
         maxdepth = 15;
 
     if(depth > maxdepth)
 	return 0;
 
b151ef55
     claminfo.dirs++;
a7c274d5
     depth++;
b151ef55
 
     if((dd = opendir(dirname)) != NULL) {
 	while((dent = readdir(dd))) {
618a038b
 #ifndef C_INTERIX
 	    if(dent->d_ino)
 #endif
 	    {
b151ef55
 		if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
 		    /* build the full name */
 		    fname = mcalloc(strlen(dirname) + strlen(dent->d_name) + 2, sizeof(char));
 		    sprintf(fname, "%s/%s", dirname, dent->d_name);
 
 		    /* stat the file */
 		    if(lstat(fname, &statbuf) != -1) {
f39fb336
 			if(S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode) && recursion) {
a7c274d5
 			    if(treewalk(fname, root, user, opt, limits, options, depth) == 1)
f39fb336
 				scanret++;
 			} else {
b151ef55
 			    if(S_ISREG(statbuf.st_mode))
0c4f7c46
 				scanret += scanfile(fname, root, user, opt, limits, options);
f39fb336
 			}
b151ef55
 		    }
 		    free(fname);
 		}
 
 	    }
 	}
     } else {
 	if(!printinfected)
f1c4563e
 	    logg("%s: Can't open directory.\n", dirname);
b151ef55
 	return 53;
     }
 
     closedir(dd);
 
     if(scanret)
 	return 1;
     else
 	return 0;
 
 }
 
 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
a6c3fdb9
 	    if(!geteuid()) { 
b151ef55
 		if((user = getpwnam(UNPUSER)) == NULL)
 		    return -3;
 
819bbe1f
 #ifdef HAVE_SETGROUPS
eeb69538
 		if(setgroups(1, &user->pw_gid)) {
 		    fprintf(stderr, "ERROR: setgroups() failed.\n");
 		    return -3;
 		}
819bbe1f
 #endif
eeb69538
 
 		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;
 		}
b151ef55
 	    }
 #endif
 	    rmdirs(dir);
 	    exit(0);
 	    break;
 	default:
 	    waitpid(pid, &status, 0);
 	    if(WIFEXITED(status))
 		return 0;
 	    else
 		return -2;
     }
 
 }
 
 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))) {
618a038b
 #ifndef C_INTERIX
 	    if(dent->d_ino)
 #endif
 	    {
b151ef55
 		if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
 		    /* build full name */
 		    fname = mcalloc(strlen(dirname) + strlen(dent->d_name) + 2, sizeof(char));
 		    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);
0db63b59
 			} else if(S_ISREG(statbuf.st_mode))
b151ef55
 			    chmod(fname, 0700);
 		    }
 
 		    free(fname);
 		}
 	    }
 	}
     } else {
 	if(!printinfected)
f1c4563e
 	    logg("%s: Can't open directory.\n", dirname);
b151ef55
 	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))) {
618a038b
 #ifndef C_INTERIX
 	    if(dent->d_ino)
 #endif
 	    {
b151ef55
 		if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
 		    n->files++;
 
 		    /* build the full name */
 		    fname = mcalloc(strlen(dirname) + strlen(dent->d_name) + 2, sizeof(char));
 		    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)
f1c4563e
 	    logg("%s: Can't open directory.\n", dirname);
b151ef55
 	return 53;
     }
 
     closedir(dd);
 
     return 0;
 }