Browse code

new option --copy

git-svn: trunk@2456

Tomasz Kojm authored on 2006/10/29 23:37:20
Showing 6 changed files
... ...
@@ -1,3 +1,7 @@
1
+Sun Oct 29 15:36:21 CET 2006 (tk)
2
+---------------------------------
3
+  * clamscan, clamdscan: new option --copy, patch from aCaB
4
+
1 5
 Sun Oct 29 15:06:21 CET 2006 (tk)
2 6
 ---------------------------------
3 7
   * clamd/thrmgr.c: fix possible memory leaks and improve handling of memory
... ...
@@ -53,7 +53,7 @@ int main(int argc, char **argv)
53 53
 	time_t starttime;
54 54
 	struct optstruct *opt;
55 55
 	char *clamdscan_accepted[] = { "help", "version", "verbose", "quiet",
56
-				  "stdout", "log", "move", "remove",
56
+				  "stdout", "log", "move", "copy", "remove",
57 57
 				  "config-file", "no-summary",
58 58
 				  "disable-summary", NULL };
59 59
 
... ...
@@ -149,6 +149,7 @@ void help(void)
149 149
     mprintf("    --log=FILE          -l FILE        Save scan report in FILE\n");
150 150
     mprintf("    --remove                           Remove infected files. Be careful!\n");
151 151
     mprintf("    --move=DIRECTORY                   Move infected files into DIRECTORY\n");
152
+    mprintf("    --copy=DIRECTORY                   Copy infected files into DIRECTORY\n");
152 153
     mprintf("    --config-file=FILE                 Read configuration from FILE.\n");
153 154
     mprintf("    --infected            -i             Only print infected files\n");
154 155
     mprintf("    --no-summary                       Disable summary at end of scanning\n");
... ...
@@ -80,13 +80,13 @@ static int dsresult(int sockd, const struct optstruct *opt)
80 80
 	if(strstr(buff, "FOUND\n")) {
81 81
 	    infected++;
82 82
 	    logg("%s", buff);
83
-	    if(opt_check(opt, "move")) {
83
+	    if(opt_check(opt, "move") || opt_check(opt, "copy")) {
84 84
 		/* filename: Virus FOUND */
85 85
 		if((pt = strrchr(buff, ':'))) {
86 86
 		    *pt = 0;
87 87
 		    move_infected(buff, opt);
88 88
 		} else {
89
-		    mprintf("@Broken data format. File not moved.\n");
89
+		    mprintf("@Broken data format. File not %s.\n", opt_check(opt, "move") ? "moved" : "copied");
90 90
 		}
91 91
 
92 92
 	    } else if(opt_check(opt, "remove")) {
... ...
@@ -515,10 +515,12 @@ void move_infected(const char *filename, const struct optstruct *opt)
515 515
 	char *movedir, *movefilename, *tmp, numext[4 + 1];
516 516
 	struct stat fstat, mfstat;
517 517
 	int n, len, movefilename_size;
518
+	int moveflag = opt_check(opt, "move");
518 519
 	struct utimbuf ubuf;
519 520
 
520 521
 
521
-    if(!(movedir = opt_arg(opt, "move"))) {
522
+    if((moveflag && !(movedir = opt_arg(opt, "move"))) ||
523
+        (!moveflag && !(movedir = opt_arg(opt, "copy")))) {
522 524
         /* Should never reach here */
523 525
         logg("^opt_arg() returned NULL\n");
524 526
         notmoved++;
... ...
@@ -526,7 +528,7 @@ void move_infected(const char *filename, const struct optstruct *opt)
526 526
     }
527 527
 
528 528
     if(access(movedir, W_OK|X_OK) == -1) {
529
-        logg("^error moving file '%s': cannot write to '%s': %s\n", filename, movedir, strerror(errno));
529
+        logg("^error %s file '%s': cannot write to '%s': %s\n", (moveflag) ? "moving" : "copying", filename, movedir, strerror(errno));
530 530
         notmoved++;
531 531
         return;
532 532
     }
... ...
@@ -589,9 +591,9 @@ void move_infected(const char *filename, const struct optstruct *opt)
589 589
        }
590 590
     }
591 591
 
592
-    if(rename(filename, movefilename) == -1) {
592
+    if(!moveflag || rename(filename, movefilename) == -1) {
593 593
 	if(filecopy(filename, movefilename) == -1) {
594
-	    logg("^cannot move '%s' to '%s': %s\n", filename, movefilename, strerror(errno));
594
+	    logg("^cannot %s '%s' to '%s': %s\n", (moveflag) ? "move" : "copy", filename, movefilename, strerror(errno));
595 595
 	    notmoved++;
596 596
 	    free(movefilename);
597 597
 	    return;
... ...
@@ -604,7 +606,7 @@ void move_infected(const char *filename, const struct optstruct *opt)
604 604
 	ubuf.modtime = fstat.st_mtime;
605 605
 	utime(movefilename, &ubuf);
606 606
 
607
-	if(unlink(filename)) {
607
+	if(moveflag && unlink(filename)) {
608 608
 	    logg("^cannot unlink '%s': %s\n", filename, strerror(errno));
609 609
 	    notremoved++;            
610 610
 	    free(movefilename);
... ...
@@ -612,7 +614,7 @@ void move_infected(const char *filename, const struct optstruct *opt)
612 612
 	}
613 613
     }
614 614
 
615
-    logg("%s: moved to '%s'\n", filename, movefilename);
615
+    logg("%s: %s to '%s'\n", (moveflag)?"moved":"copied", filename, movefilename);
616 616
 
617 617
     free(movefilename);
618 618
 }
... ...
@@ -196,7 +196,7 @@ int main(int argc, char **argv)
196 196
 	    logg("Not removed: %d\n", claminfo.notremoved);
197 197
 	}
198 198
 	if(claminfo.notmoved) {
199
-	    logg("Not moved: %d\n", claminfo.notmoved);
199
+	    logg("Not %s: %d\n", opt_check(opt, "copy") ? "moved" : "copied", claminfo.notmoved);
200 200
 	}
201 201
 	mb = claminfo.blocks * (CL_COUNT_PRECISION / 1024) / 1024.0;
202 202
 	logg("Data scanned: %2.2lf MB\n", mb);
... ...
@@ -235,6 +235,7 @@ void help(void)
235 235
     mprintf("    --recursive           -r             Scan subdirectories recursively\n");
236 236
     mprintf("    --remove                             Remove infected files. Be careful!\n");
237 237
     mprintf("    --move=DIRECTORY                     Move infected files into DIRECTORY\n");
238
+    mprintf("    --copy=DIRECTORY                     Copy infected files into DIRECTORY\n");
238 239
 #ifdef HAVE_REGEX_H
239 240
     mprintf("    --exclude=REGEX                      Don't scan file names matching REGEX\n");
240 241
     mprintf("    --exclude-dir=REGEX                  Don't scan directories matching REGEX\n");
... ...
@@ -51,6 +51,7 @@ static struct option clamscan_longopt[] = {
51 51
     {"threads", 1, 0, 0},	    /* not used */
52 52
     {"one-virus", 0, 0, 0},	    /* not used */
53 53
     {"move", 1, 0, 0},
54
+    {"copy", 1, 0, 0},
54 55
     {"remove", 0, 0, 0},
55 56
     {"exclude", 1, 0, 0},
56 57
     {"exclude-dir", 1, 0, 0},
... ...
@@ -391,7 +391,7 @@ int scanfile(const char *filename, struct cl_node *root, const struct passwd *us
391 391
 		} else {
392 392
 		    logg("%s: Removed\n", filename);
393 393
 		}
394
-	    } else if (opt_check(opt, "move"))
394
+	    } else if (opt_check(opt, "move") || opt_check(opt, "copy"))
395 395
 		move_infected(filename, opt);
396 396
 
397 397
 	    return 1;
... ...
@@ -455,7 +455,7 @@ int scanfile(const char *filename, struct cl_node *root, const struct passwd *us
455 455
 	    } else {
456 456
 		logg("%s: Removed\n", filename);
457 457
 	    }
458
-	} else if (opt_check(opt, "move"))
458
+	} else if (opt_check(opt, "move") || opt_check(opt, "copy"))
459 459
             move_infected(filename, opt);
460 460
     }
461 461
     return ret;
... ...
@@ -618,7 +618,7 @@ int scancompressed(const char *filename, struct cl_node *root, const struct pass
618 618
 		    } else {
619 619
 			logg("%s: Removed\n", filename);
620 620
 		    }
621
-		} else if (opt_check(opt, "move"))
621
+		} else if (opt_check(opt, "move") || opt_check(opt, "copy"))
622 622
 		    move_infected(filename, opt);
623 623
 	    }
624 624
 	    return ret;
... ...
@@ -635,7 +635,7 @@ int scancompressed(const char *filename, struct cl_node *root, const struct pass
635 635
 		    } else {
636 636
 			logg("%s: Removed\n", filename);
637 637
 		    }
638
-		} else if (opt_check(opt, "move"))
638
+		} else if (opt_check(opt, "move") || opt_check(opt, "copy"))
639 639
 		    move_infected(filename, opt);
640 640
 	    }
641 641
 	    return ret;
... ...
@@ -652,7 +652,7 @@ int scancompressed(const char *filename, struct cl_node *root, const struct pass
652 652
 		} else {
653 653
 		    logg("%s: Removed\n", filename);
654 654
 		}
655
-	    } else if (opt_check(opt, "move"))
655
+	    } else if (opt_check(opt, "move") || opt_check(opt, "copy"))
656 656
 		move_infected(filename, opt);
657 657
 
658 658
 	    return 1;
... ...
@@ -732,7 +732,7 @@ int scandenied(const char *filename, struct cl_node *root, const struct passwd *
732 732
 	    } else {
733 733
 	        logg("%s: Removed\n", filename);
734 734
 	    }
735
-	} else if (opt_check(opt, "move"))
735
+	} else if (opt_check(opt, "move") || opt_check(opt, "copy"))
736 736
 	    move_infected(filename, opt);
737 737
     }
738 738
 
... ...
@@ -957,10 +957,12 @@ void move_infected(const char *filename, const struct optstruct *opt)
957 957
 	char *movedir, *movefilename, *tmp, numext[4 + 1];
958 958
 	struct stat fstat, mfstat;
959 959
 	int n, len, movefilename_size;
960
+	int moveflag = opt_check(opt, "move");
960 961
 	struct utimbuf ubuf;
961 962
 
962 963
 
963
-    if(!(movedir = opt_arg(opt, "move"))) {
964
+    if((moveflag && !(movedir = opt_arg(opt, "move"))) ||
965
+	(!moveflag && !(movedir = opt_arg(opt, "copy")))) {
964 966
         /* Should never reach here */
965 967
         logg("!opt_arg() returned NULL\n", filename);
966 968
         claminfo.notmoved++;
... ...
@@ -968,7 +970,7 @@ void move_infected(const char *filename, const struct optstruct *opt)
968 968
     }
969 969
 
970 970
     if(access(movedir, W_OK|X_OK) == -1) {
971
-        logg("!Can't move file '%s': cannot write to '%s': %s\n", filename, movedir, strerror(errno));
971
+	logg("!Can't %s file '%s': cannot write to '%s': %s\n", (moveflag) ? "move" : "copy", filename, movedir, strerror(errno));
972 972
         claminfo.notmoved++;
973 973
         return;
974 974
     }
... ...
@@ -1026,9 +1028,9 @@ void move_infected(const char *filename, const struct optstruct *opt)
1026 1026
        }
1027 1027
     }
1028 1028
 
1029
-    if(rename(filename, movefilename) == -1) {
1029
+    if(!moveflag || rename(filename, movefilename) == -1) {
1030 1030
 	if(filecopy(filename, movefilename) == -1) {
1031
-	    logg("!Can't move '%s' to '%s': %s\n", filename, movefilename, strerror(errno));
1031
+	    logg("!Can't %s '%s' to '%s': %s\n", (moveflag) ? "move" : "copy", filename, movefilename, strerror(errno));
1032 1032
 	    claminfo.notmoved++;
1033 1033
 	    free(movefilename);
1034 1034
 	    return;
... ...
@@ -1043,7 +1045,7 @@ void move_infected(const char *filename, const struct optstruct *opt)
1043 1043
 	ubuf.modtime = fstat.st_mtime;
1044 1044
 	utime(movefilename, &ubuf);
1045 1045
 
1046
-	if(unlink(filename)) {
1046
+	if(moveflag && unlink(filename)) {
1047 1047
 	    logg("!Can't unlink '%s': %s\n", filename, strerror(errno));
1048 1048
 	    claminfo.notremoved++;            
1049 1049
 	    free(movefilename);
... ...
@@ -1051,7 +1053,7 @@ void move_infected(const char *filename, const struct optstruct *opt)
1051 1051
 	}
1052 1052
     }
1053 1053
 
1054
-    logg("%s: moved to '%s'\n", filename, movefilename);
1054
+    logg("%s: %s to '%s'\n", filename, (moveflag) ? "moved" : "copied", movefilename);
1055 1055
 
1056 1056
     free(movefilename);
1057 1057
 }