clamscan/manager.c
e3aaff8e
 /*
7a2997f1
  *  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
  *
  *  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>
34f71e0e
 #ifdef C_WINDOWS
 #include <sys/utime.h>
 #else
e3aaff8e
 #include <sys/wait.h>
4790a32f
 #include <utime.h>
34f71e0e
 #endif
ec6429ab
 #ifndef C_WINDOWS
 #include <dirent.h>
9a03413e
 #include <sys/time.h>
 #include <sys/resource.h>
34f71e0e
 #endif
e3aaff8e
 #include <fcntl.h>
34f71e0e
 #ifdef	HAVE_UNISTD_H
e3aaff8e
 #include <unistd.h>
34f71e0e
 #endif
e3aaff8e
 #include <sys/types.h>
 #include <signal.h>
 #include <errno.h>
 
 #include "manager.h"
7a2997f1
 #include "others.h"
 #include "global.h"
 
 #include "shared/options.h"
 #include "shared/output.h"
 #include "shared/misc.h"
 
 #include "libclamav/clamav.h"
 #include "libclamav/others.h"
 #include "libclamav/matcher-ac.h"
 #include "libclamav/str.h"
05f92e64
 #include "libclamav/readdb.h"
e3aaff8e
 
 #ifdef C_LINUX
 dev_t procdev;
 #endif
 
34f71e0e
 #ifdef C_WINDOWS
 #undef P_tmpdir
 #define P_tmpdir    "C:\\WINDOWS\\TEMP"
 #endif
 
 #ifndef	O_BINARY
 #define	O_BINARY    0
 #endif
 
ec6429ab
 static void move_infected(const char *filename, const struct optstruct *opt);
 
 static int scanfile(const char *filename, struct cl_engine *engine, const struct optstruct *opt, const struct cl_limits *limits, unsigned int options)
7a2997f1
 {
ec6429ab
 	int ret = 0, fd, included, printclean = 1;
 	const struct optnode *optnode;
 	char *argument;
 	const char *virname;
 #ifdef C_LINUX
 	struct stat sb;
 
     /* argh, don't scan /proc files */
     if(procdev)
 	if(stat(filename, &sb) != -1)
 	    if(sb.st_dev == procdev) {
 		if(!printinfected)
 		    logg("~%s: Excluded (/proc)\n", filename);
 		return 0;
 	    }
 #endif    
 
     if(opt_check(opt, "exclude")) {
 	argument = opt_firstarg(opt, "exclude", &optnode);
 	while(argument) {
 	    if(match_regex(filename, argument) == 1) {
 		if(!printinfected)
 		    logg("~%s: Excluded\n", filename);
 		return 0;
 	    }
 	    argument = opt_nextarg(&optnode, "exclude");
 	}
     }
 
    if(opt_check(opt, "include")) {
 	included = 0;
 	argument = opt_firstarg(opt, "include", &optnode);
 	while(argument && !included) {
 	    if(match_regex(filename, argument) == 1) {
 		included = 1;
 		break;
 	    }
 	    argument = opt_nextarg(&optnode, "include");
 	}
 
 	if(!included) {
 	    if(!printinfected)
 		logg("~%s: Excluded\n", filename);
 	    return 0;
 	}
     }
 
     if(fileinfo(filename, 1) == 0) {
 	if(!printinfected)
 	    logg("~%s: Empty file\n", filename);
 	return 0;
     }
 
 #ifndef C_WINDOWS
     if(geteuid())
 	if(checkaccess(filename, NULL, R_OK) != 1) {
 	    if(!printinfected)
 		logg("~%s: Access denied\n", filename);
 	    return 0;
 	}
 #endif
 
     logg("*Scanning %s\n", filename);
 
     if((fd = open(filename, O_RDONLY|O_BINARY)) == -1) {
 	logg("^Can't open file %s\n", filename);
 	return 54;
     }
 
     info.files++;
 
     if((ret = cl_scandesc(fd, &virname, &info.blocks, engine, limits, options)) == CL_VIRUS) {
 	logg("~%s: %s FOUND\n", filename, virname);
 	info.ifiles++;
 
 	if(bell)
 	    fprintf(stderr, "\007");
 
     } else if(ret == CL_CLEAN) {
 	if(!printinfected && printclean)
 	    mprintf("~%s: OK\n", filename);
     } else
 	if(!printinfected)
 	    logg("~%s: %s\n", filename, cl_strerror(ret));
 
     close(fd);
 
     if(ret == CL_VIRUS) {
 	if(opt_check(opt, "remove")) {
 	    if(unlink(filename)) {
 		logg("^%s: Can't remove\n", filename);
 		info.notremoved++;
 	    } else {
 		logg("~%s: Removed\n", filename);
 	    }
 	} else if(opt_check(opt, "move") || opt_check(opt, "copy"))
             move_infected(filename, opt);
     }
 
     return ret;
 }
 
 static int scandirs(const char *dirname, struct cl_engine *engine, const struct optstruct *opt, const struct cl_limits *limits, unsigned int options, unsigned int depth)
 {
 	DIR *dd;
 	struct dirent *dent;
 	struct stat statbuf;
 	char *fname;
 	int scanret = 0, included;
 	unsigned int maxdepth;
 	const struct optnode *optnode;
 	char *argument;
 
 
     if(opt_check(opt, "exclude-dir")) {
 	argument = opt_firstarg(opt, "exclude-dir", &optnode);
 	while(argument) {
 	    if(match_regex(dirname, argument) == 1) {
 		if(!printinfected)
 		    logg("~%s: Excluded\n", dirname);
 		return 0;
 	    }
 	    argument = opt_nextarg(&optnode, "exclude-dir");
 	}
     }
 
    if(opt_check(opt, "include-dir")) {
 	included = 0;
 	argument = opt_firstarg(opt, "include-dir", &optnode);
 	while(argument && !included) {
 	    if(match_regex(dirname, argument) == 1) {
 		included = 1;
 		break;
 	    }
 	    argument = opt_nextarg(&optnode, "include-dir");
 	}
 
 	if(!included) {
 	    if(!printinfected)
 		logg("~%s: Excluded\n", dirname);
 	    return 0;
 	}
     }
 
     if(opt_check(opt, "max-dir-recursion"))
         maxdepth = atoi(opt_arg(opt, "max-dir-recursion"));
     else
         maxdepth = 15;
 
     if(depth > maxdepth)
 	return 0;
 
     info.dirs++;
     depth++;
 
     if((dd = opendir(dirname)) != NULL) {
 	while((dent = readdir(dd))) {
6670d61d
 #if !defined(C_INTERIX) && !defined(C_WINDOWS)
ec6429ab
 	    if(dent->d_ino)
 #endif
 	    {
 		if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
 		    /* build the full name */
 		    fname = malloc(strlen(dirname) + strlen(dent->d_name) + 2);
 		    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) && recursion) {
 			    if(scandirs(fname, engine, opt, limits, options, depth) == 1)
 				scanret++;
 			} else {
 			    if(S_ISREG(statbuf.st_mode))
 				scanret += scanfile(fname, engine, opt, limits, options);
 			}
 		    }
 		    free(fname);
 		}
 
 	    }
 	}
     } else {
 	if(!printinfected)
 	    logg("~%s: Can't open directory.\n", dirname);
 	return 53;
     }
 
     closedir(dd);
 
     if(scanret)
 	return 1;
     else
 	return 0;
 
7a2997f1
 }
 
 static int scanstdin(const struct cl_engine *engine, const struct cl_limits *limits, int options)
 {
 	int ret;
 	const char *virname, *tmpdir;
 	char *file, buff[FILEBUFF];
ec6429ab
 	size_t bread;
7a2997f1
 	FILE *fs;
 
 
     /* check write access */
     tmpdir = getenv("TMPDIR");
 
     if(tmpdir == NULL)
 #ifdef P_tmpdir
 	tmpdir = P_tmpdir;
 #else
 	tmpdir = "/tmp";
 #endif
 
     if(checkaccess(tmpdir, CLAMAVUSER, W_OK) != 1) {
 	logg("!Can't write to temporary directory\n");
 	return 64;
     }
 
     file = cli_gentemp(tmpdir);
 
     if(!(fs = fopen(file, "wb"))) {
 	logg("!Can't open %s for writing\n", file);
38fe8af4
 	free(file);
7a2997f1
 	return 63;
     }
 
ec6429ab
     while((bread = fread(buff, 1, FILEBUFF, stdin)))
 	if(fwrite(buff, 1, bread, fs) < bread) {
38fe8af4
 	    logg("!Can't write to %s\n", file);
 	    free(file);
 	    return 58;
 	}
7a2997f1
 
     fclose(fs);
 
     logg("*Checking %s\n", file);
     info.files++;
 
     if((ret = cl_scanfile(file, &virname, &info.blocks, engine, limits, options)) == CL_VIRUS) {
 	logg("stdin: %s FOUND\n", virname);
 	info.ifiles++;
 
 	if(bell)
 	    fprintf(stderr, "\007");
 
     } else if(ret == CL_CLEAN) {
 	if(!printinfected)
 	    mprintf("stdin: OK\n");
     } else
 	if(!printinfected)
 	    logg("stdin: %s\n", cl_strerror(ret));
 
     unlink(file);
     free(file);
     return ret;
 }
8000d078
 
e3aaff8e
 int scanmanager(const struct optstruct *opt)
 {
 	mode_t fmode;
ec6429ab
 	int ret = 0, fmodeint, i, x;
7a2997f1
 	unsigned int options = 0, dboptions = 0;
 	struct cl_engine *engine = NULL;
 	struct cl_limits limits;
e3aaff8e
 	struct stat sb;
b023c36d
 	char *file, cwd[1024], *pua_cats = NULL, *argument;
 	const struct optnode *optnode;
9a03413e
 #ifndef C_WINDOWS
 	struct rlimit rlim;
 #endif
e3aaff8e
 
 
af7d0dde
     if(!opt_check(opt, "no-phishing-sigs"))
9f8098c0
 	dboptions |= CL_DB_PHISHING;
e3aaff8e
 
9f8098c0
     if(!opt_check(opt,"no-phishing-scan-urls"))
 	dboptions |= CL_DB_PHISHING_URLS;
19b3e182
     if(opt_check(opt,"phishing-ssl")) {
6e965052
 	options |= CL_SCAN_PHISHING_BLOCKSSL;
19b3e182
     }
     if(opt_check(opt,"phishing-cloak")) {
6e965052
 	options |= CL_SCAN_PHISHING_BLOCKCLOAK;
19b3e182
     }
7f0d1148
     if(opt_check(opt,"heuristic-scan-precedence")) {
 	options |= CL_SCAN_HEURISTIC_PRECEDENCE;
     }
a68507c5
 
a4453bce
     if(opt_check(opt, "dev-ac-only"))
83fa5305
 	dboptions |= CL_DB_ACONLY;
 
3d53538b
     if(opt_check(opt, "dev-ac-depth"))
 	cli_ac_setdepth(AC_DEFAULT_MIN_DEPTH, atoi(opt_arg(opt, "dev-ac-depth")));
 
b023c36d
     if(opt_check(opt, "detect-pua")) {
70edb085
 	dboptions |= CL_DB_PUA;
 
b023c36d
 	if(opt_check(opt, "exclude-pua")) {
 	    dboptions |= CL_DB_PUA_EXCLUDE;
 	    argument = opt_firstarg(opt, "exclude-pua", &optnode);
 	    i = 0;
 	    while(argument) {
 		if(!(pua_cats = realloc(pua_cats, i + strlen(argument) + 3))) {
 		    logg("!Can't allocate memory for pua_cats\n");
 		    return 70;
 		}
 		sprintf(pua_cats + i, ".%s", argument);
 		i += strlen(argument) + 1;
 		pua_cats[i] = 0;
 		argument = opt_nextarg(&optnode, "exclude-pua");
 	    }
 	    pua_cats[i] = '.';
 	    pua_cats[i + 1] = 0;
 	}
 
 	if(opt_check(opt, "include-pua")) {
 	    if(pua_cats) {
 		logg("!--exclude-pua and --include-pua cannot be used at the same time\n");
 		free(pua_cats);
 		return 40;
 	    }
 	    dboptions |= CL_DB_PUA_INCLUDE;
 	    argument = opt_firstarg(opt, "include-pua", &optnode);
 	    i = 0;
 	    while(argument) {
 		if(!(pua_cats = realloc(pua_cats, i + strlen(argument) + 3))) {
 		    logg("!Can't allocate memory for pua_cats\n");
 		    return 70;
 		}
 		sprintf(pua_cats + i, ".%s", argument);
 		i += strlen(argument) + 1;
 		pua_cats[i] = 0;
 		argument = opt_nextarg(&optnode, "include-pua");
 	    }
 	    pua_cats[i] = '.';
 	    pua_cats[i + 1] = 0;
 	}
 
 	if(pua_cats) {
 	    /* FIXME with the new API */
 	    if((ret = cli_initengine(&engine, dboptions))) {
 		logg("!cli_initengine() failed: %s\n", cl_strerror(ret));
 		free(pua_cats);
 		return 50;
 	    }
 	    engine->pua_cats = pua_cats;
 	}
     }
 
7b8edc5c
     if(opt_check(opt, "database")) {
7a2997f1
 	if((ret = cl_load(opt_arg(opt, "database"), &engine, &info.sigs, dboptions))) {
9f0e5585
 	    logg("!%s\n", cl_strerror(ret));
d6449522
 	    return 50;
e3aaff8e
 	}
 
     } else {
98ce643b
 	    char *dbdir = freshdbdir();
908db4df
 
7a2997f1
 	if((ret = cl_load(dbdir, &engine, &info.sigs, dboptions))) {
9f0e5585
 	    logg("!%s\n", cl_strerror(ret));
98ce643b
 	    free(dbdir);
e3aaff8e
 	    return 50;
 	}
98ce643b
 	free(dbdir);
e3aaff8e
     }
 
7a2997f1
     if(!engine) {
9f0e5585
 	logg("!Can't initialize the virus database\n");
e3aaff8e
 	return 50;
     }
 
7a2997f1
     if((ret = cl_build(engine)) != 0) {
9f0e5585
 	logg("!Database initialization error: %s\n", cl_strerror(ret));;
2d70a403
 	return 50;
     }
e3aaff8e
 
7a2997f1
     /* set limits */
     memset(&limits, 0, sizeof(struct cl_limits));
e3aaff8e
 
281c7642
     if(opt_check(opt, "max-scansize")) {
e3aaff8e
 	char *cpy, *ptr;
281c7642
 	ptr = opt_arg(opt, "max-scansize");
 	if(tolower(ptr[strlen(ptr) - 1]) == 'm') {
 	    cpy = calloc(strlen(ptr), 1);
 	    strncpy(cpy, ptr, strlen(ptr) - 1);
72ce4b70
 	    cpy[strlen(ptr)-1]='\0';
38139150
 	    limits.maxscansize = atoi(cpy) * 1024 * 1024;
281c7642
 	    free(cpy);
 	} else
 	    limits.maxscansize = atoi(ptr) * 1024;
     } else
a5d91be7
 	limits.maxscansize = 104857600;
281c7642
 
     if(opt_check(opt, "max-filesize")) {
 	char *cpy, *ptr;
 	ptr = opt_arg(opt, "max-filesize");
e3aaff8e
 	if(tolower(ptr[strlen(ptr) - 1]) == 'm') {
65d08d61
 	    cpy = calloc(strlen(ptr), 1);
658f19f8
 	    strncpy(cpy, ptr, strlen(ptr) - 1);
72ce4b70
 	    cpy[strlen(ptr)-1]='\0';
7a2997f1
 	    limits.maxfilesize = atoi(cpy) * 1024 * 1024;
e3aaff8e
 	    free(cpy);
 	} else
7a2997f1
 	    limits.maxfilesize = atoi(ptr) * 1024;
e3aaff8e
     } else
a5d91be7
 	limits.maxfilesize = 26214400;
e3aaff8e
 
9a03413e
 #ifndef C_WINDOWS
     if(getrlimit(RLIMIT_FSIZE, &rlim) == 0) {
 	if((rlim.rlim_max < limits.maxfilesize) || (rlim.rlim_max < limits.maxscansize))
 	    logg("^System limit for file size is lower than maxfilesize or maxscansize\n");
     } else {
 	logg("^Cannot obtain resource limits for file size\n");
     }
 #endif
 
7b8edc5c
     if(opt_check(opt, "max-files"))
7a2997f1
 	limits.maxfiles = atoi(opt_arg(opt, "max-files"));
e3aaff8e
     else
a5d91be7
         limits.maxfiles = 10000;
e3aaff8e
 
7b8edc5c
     if(opt_check(opt, "max-recursion"))
7a2997f1
         limits.maxreclevel = atoi(opt_arg(opt, "max-recursion"));
e3aaff8e
     else
a5d91be7
         limits.maxreclevel = 16;
e3aaff8e
 
6ef42bc3
     /* set options */
 
7b8edc5c
     if(opt_check(opt, "disable-archive") || opt_check(opt, "no-archive"))
3805ebcb
 	options &= ~CL_SCAN_ARCHIVE;
6ef42bc3
     else
3805ebcb
 	options |= CL_SCAN_ARCHIVE;
6ef42bc3
 
7b8edc5c
     if(opt_check(opt, "detect-broken"))
453581ae
 	options |= CL_SCAN_BLOCKBROKEN;
6ef42bc3
 
7b8edc5c
     if(opt_check(opt, "block-encrypted"))
08d6b1e3
 	options |= CL_SCAN_BLOCKENCRYPTED;
6ef42bc3
 
7b8edc5c
     if(opt_check(opt, "no-pe"))
3805ebcb
 	options &= ~CL_SCAN_PE;
6ef42bc3
     else
3805ebcb
 	options |= CL_SCAN_PE;
6ef42bc3
 
3f97a1e7
     if(opt_check(opt, "no-elf"))
 	options &= ~CL_SCAN_ELF;
     else
 	options |= CL_SCAN_ELF;
 
7b8edc5c
     if(opt_check(opt, "no-ole2"))
3805ebcb
 	options &= ~CL_SCAN_OLE2;
6ef42bc3
     else
3805ebcb
 	options |= CL_SCAN_OLE2;
6ef42bc3
 
c5107e70
     if(opt_check(opt, "no-pdf"))
 	options &= ~CL_SCAN_PDF;
     else
 	options |= CL_SCAN_PDF;
 
7b8edc5c
     if(opt_check(opt, "no-html"))
3805ebcb
 	options &= ~CL_SCAN_HTML;
6ef42bc3
     else
3805ebcb
 	options |= CL_SCAN_HTML;
6ef42bc3
 
7b8edc5c
     if(opt_check(opt, "no-mail")) {
3805ebcb
 	options &= ~CL_SCAN_MAIL;
6ef42bc3
     } else {
3805ebcb
 	options |= CL_SCAN_MAIL;
6ef42bc3
 
7b8edc5c
 	if(opt_check(opt, "mail-follow-urls"))
3805ebcb
 	    options |= CL_SCAN_MAILURL;
6ef42bc3
     }
 
7b8edc5c
     if(opt_check(opt, "no-algorithmic"))
6fd2fb47
 	options &= ~CL_SCAN_ALGORITHMIC;
1b661cef
     else
6fd2fb47
 	options |= CL_SCAN_ALGORITHMIC;
1b661cef
 
a6e38800
     if(opt_check(opt, "detect-structured")) {
 	options |= CL_SCAN_STRUCTURED;
5fe6e72b
 
 	if(opt_check(opt, "structured-ssn-format")) {
 	    switch(atoi(opt_arg(opt, "structured-ssn-format"))) {
 		case 0:
 		    options |= CL_SCAN_STRUCTURED_SSN_NORMAL;
 		    break;
 		case 1:
 		    options |= CL_SCAN_STRUCTURED_SSN_STRIPPED;
 		    break;
 		case 2:
 		    options |= (CL_SCAN_STRUCTURED_SSN_NORMAL | CL_SCAN_STRUCTURED_SSN_STRIPPED);
 		    break;
 		default:
 		    logg("!Invalid argument for --structured-ssn-format\n");
 		    return 40;
 	    }
 	} else {
 	    options |= (CL_SCAN_STRUCTURED_SSN_NORMAL | CL_SCAN_STRUCTURED_SSN_STRIPPED);
 	}
 
 	if(opt_check(opt, "structured-ssn-count"))
 	    limits.min_ssn_count = atoi(opt_arg(opt, "structured-ssn-count"));
 	else
57e1a05e
 	    limits.min_ssn_count = 3;
5fe6e72b
 
 	if(opt_check(opt, "structured-cc-count"))
 	    limits.min_cc_count = atoi(opt_arg(opt, "structured-cc-count"));
 	else
57e1a05e
 	    limits.min_cc_count = 3;
5fe6e72b
 
a6e38800
     } else
 	options &= ~CL_SCAN_STRUCTURED;
 
 
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))) {
9f0e5585
 	    logg("!Can't get absolute pathname of current working directory\n");
2d70a403
 	    ret = 57;
 	} else
ec6429ab
 	    ret = scandirs(cwd, engine, opt, &limits, options, 1);
2d70a403
 
     } else if(!strcmp(opt->filename, "-")) { /* read data from stdin */
7a2997f1
 	ret = scanstdin(engine, &limits, options);
e3aaff8e
 
     } else {
ec6429ab
 	for (x = 0; (file = cli_strtok(opt->filename, x, "\t")) != NULL; x++) {
 	    if((fmodeint = fileinfo(file, 2)) == -1) {
 		logg("^Can't access file %s\n", file);
 		perror(file);
2d70a403
 		ret = 56;
 	    } else {
a8b056dc
 		int slash = 1;
ec6429ab
 		for(i = strlen(file) - 1; i > 0 && slash; i--) {
 		    if(file[i] == '/')
 			file[i] = 0;
a8b056dc
 		    else
 			slash = 0;
 		}
 
2d70a403
 		fmode = (mode_t) fmodeint;
 
 		switch(fmode & S_IFMT) {
 		    case S_IFREG:
ec6429ab
 			ret = scanfile(file, engine, opt, &limits, options);
2d70a403
 			break;
 
 		    case S_IFDIR:
ec6429ab
 			ret = scandirs(file, engine, opt, &limits, options, 1);
2d70a403
 			break;
 
 		    default:
ec6429ab
 			logg("!Not supported file type (%s)\n", file);
2d70a403
 			ret = 52;
 		}
 	    }
ec6429ab
 	    free(file);
e3aaff8e
 	}
     }
 
7a2997f1
     /* free the engine */
     cl_free(engine);
e3aaff8e
 
     /* overwrite return code */
7a2997f1
     if(info.ifiles)
e3aaff8e
 	ret = 1;
     else if(ret < 50) /* hopefully no error detected */ 
 	ret = 0; /* just make sure it's 0 */
 
     return ret;
 }
 
7a2997f1
 static void move_infected(const char *filename, const struct optstruct *opt)
 {
34efd8f1
 	char *movedir, *movefilename, numext[4 + 1];
 	const char *tmp;
 	struct stat ofstat, mfstat;
7a2997f1
 	int n, len, movefilename_size;
 	int moveflag = opt_check(opt, "move");
 	struct utimbuf ubuf;
 
 
     if((moveflag && !(movedir = opt_arg(opt, "move"))) ||
 	(!moveflag && !(movedir = opt_arg(opt, "copy")))) {
         /* Should never reach here */
34efd8f1
         logg("!opt_arg() returned NULL\n");
7a2997f1
         info.notmoved++;
         return;
e3aaff8e
     }
 
7a2997f1
     if(access(movedir, W_OK|X_OK) == -1) {
 	logg("!Can't %s file '%s': cannot write to '%s': %s\n", (moveflag) ? "move" : "copy", filename, movedir, strerror(errno));
         info.notmoved++;
         return;
e3aaff8e
     }
 
7a2997f1
     if(!(tmp = strrchr(filename, '/')))
34efd8f1
 	tmp = filename;
2df4fcca
 
7a2997f1
     movefilename_size = sizeof(char) * (strlen(movedir) + strlen(tmp) + sizeof(numext) + 2);
2df4fcca
 
8ca8a18e
     if(!(movefilename = malloc(movefilename_size))) {
         logg("!malloc() failed\n");
7a2997f1
 	exit(71);
     }
e3aaff8e
 
7a2997f1
     if(!(cli_strrcpy(movefilename, movedir))) {
         logg("!cli_strrcpy() returned NULL\n");
         info.notmoved++;
         free(movefilename);
         return;
     }
e3aaff8e
 
7a2997f1
     strcat(movefilename, "/");
e3aaff8e
 
7a2997f1
     if(!(strcat(movefilename, tmp))) {
         logg("!strcat() returned NULL\n");
         info.notmoved++;
         free(movefilename);
         return;
     }
5f20a6ce
 
34efd8f1
     stat(filename, &ofstat);
7a2997f1
 
     if(!stat(movefilename, &mfstat)) {
6eef8704
         if((ofstat.st_dev == mfstat.st_dev) && (ofstat.st_ino == mfstat.st_ino)) { /* It's the same file*/
7a2997f1
             logg("File excluded '%s'\n", filename);
             info.notmoved++;
             free(movefilename);
             return;
         } 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
     }
 
7a2997f1
     if(!moveflag || rename(filename, movefilename) == -1) {
 	if(filecopy(filename, movefilename) == -1) {
 	    logg("!Can't %s '%s' to '%s': %s\n", (moveflag) ? "move" : "copy", filename, movefilename, strerror(errno));
 	    info.notmoved++;
 	    free(movefilename);
 	    return;
 	}
e3aaff8e
 
34efd8f1
 	chmod(movefilename, ofstat.st_mode);
7a2997f1
 #ifndef C_OS2
5cd3f734
 	if(chown(movefilename, ofstat.st_uid, ofstat.st_gid) == -1) {
 		logg("!Can't chown '%s': %s\n", movefilename, strerror(errno));
 	}
7a2997f1
 #endif
e3aaff8e
 
34efd8f1
 	ubuf.actime = ofstat.st_atime;
 	ubuf.modtime = ofstat.st_mtime;
7a2997f1
 	utime(movefilename, &ubuf);
e3aaff8e
 
7a2997f1
 	if(moveflag && unlink(filename)) {
 	    logg("!Can't unlink '%s': %s\n", filename, strerror(errno));
 	    info.notremoved++;            
 	    free(movefilename);
 	    return;
e3aaff8e
 	}
     }
 
5a3aeff4
     logg("~%s: %s to '%s'\n", filename, (moveflag) ? "moved" : "copied", movefilename);
7a2997f1
 
     free(movefilename);
 }