Browse code

Update

git-svn: trunk@90

Tomasz Kojm authored on 2003/10/31 11:23:54
Showing 5 changed files
... ...
@@ -1,3 +1,10 @@
1
+Fri Oct 31 02:51:59 CET 2003 (tk)
2
+---------------------------------
3
+  * clamd: initialize a session start-time in proper order (thanks to Michael
4
+	   Dankov). The fix eliminates potential termination (because of
5
+	   time out) of just started threads.
6
+  * sigtool: fixes
7
+
1 8
 Mon Oct 27 22:08:53 CET 2003 (tk)
2 9
 ---------------------------------
3 10
   * freshclam: fixed a segmentation fault (thanks to Graham Murray); only
... ...
@@ -81,7 +81,7 @@ dnl there is now a CREATE_PREFIX_TARGET_H in this file as a shorthand for
81 81
 dnl PREFIX_CONFIG_H from a target.h file, however w/o the target.h ever created
82 82
 dnl (the prefix is a bit different, since we add an extra -target- and -host-)
83 83
 dnl 
84
-dnl @version: $Id: aclocal.m4,v 1.9 2003/10/26 06:00:55 kojm Exp $
84
+dnl @version: $Id: aclocal.m4,v 1.10 2003/10/31 02:23:54 kojm Exp $
85 85
 dnl @author Guido Draheim <guidod@gmx.de>                 STATUS: used often
86 86
 
87 87
 AC_DEFUN([AC_CREATE_TARGET_H],
... ...
@@ -4041,7 +4041,7 @@ dnl      AC_COMPILE_CHECK_SIZEOF(ptrdiff_t, $headers)
4041 4041
 dnl      AC_COMPILE_CHECK_SIZEOF(off_t, $headers)
4042 4042
 dnl
4043 4043
 dnl @author Kaveh Ghazi <ghazi@caip.rutgers.edu>
4044
-dnl @version $Id: aclocal.m4,v 1.9 2003/10/26 06:00:55 kojm Exp $
4044
+dnl @version $Id: aclocal.m4,v 1.10 2003/10/31 02:23:54 kojm Exp $
4045 4045
 dnl
4046 4046
 AC_DEFUN([AC_COMPILE_CHECK_SIZEOF],
4047 4047
 [changequote(<<, >>)dnl
... ...
@@ -582,9 +582,21 @@ int acceptloop(int socketd, struct cl_node *root, const struct cfgstruct *copt)
582 582
 	tharg->options = options;
583 583
 
584 584
 	ths[i].desc = acceptd;
585
-	ths[i].active = 1;
586
-	pthread_create(&ths[i].id, &thattr, threadscanner, tharg);
587 585
 	ths[i].start = time(NULL);
586
+	ths[i].active = 1; /* the structure must be activated exactly here
587
+			    * because we will surely create a race condition 
588
+			    * in other places (if activated in the new thread
589
+			    * there * will be a race in the main thread (it
590
+			    * may assign the same thread session once more);
591
+			    * if activated after pthread_create() the new
592
+			    * thread may be already finished).
593
+			    */
594
+
595
+	if(pthread_create(&ths[i].id, &thattr, threadscanner, tharg)) {
596
+	    logg("!Session(%d) did not start. Dropping connection.", i);
597
+	    close(acceptd);
598
+	    ths[i].active = 0;
599
+	}
588 600
     }
589 601
 }
590 602
 
... ...
@@ -38,7 +38,7 @@ int main(int argc, char **argv)
38 38
 	int ret, opt_index, i, len;
39 39
 	struct optstruct *opt;
40 40
 
41
-	const char *getopt_parameters = "hvVc:s:f:b:i:s:";
41
+	const char *getopt_parameters = "hvVc:s:f:b:i:s:u:";
42 42
 
43 43
 	static struct option long_options[] = {
44 44
 	    {"help", 0, 0, 'h'},
... ...
@@ -54,6 +54,7 @@ int main(int argc, char **argv)
54 54
 	    {"file", 1, 0, 'f'},
55 55
 	    {"build", 1, 0, 'b'},
56 56
 	    {"server", 1, 0, 's'},
57
+	    {"unpack", 1, 0, 'u'},
57 58
 	    {"info", 1, 0, 'i'},
58 59
 	    {0, 0, 0, 0}
59 60
     	};
... ...
@@ -197,6 +197,10 @@ void sigtool(struct optstruct *opt)
197 197
 
198 198
 	build(opt);
199 199
 
200
+    } else if(optc(opt, 'u')) {
201
+
202
+	unpack(opt);
203
+
200 204
     } else if(optc(opt, 'i')) {
201 205
 
202 206
 	cvdinfo(opt);
... ...
@@ -281,13 +285,11 @@ void sigtool(struct optstruct *opt)
281 281
 		if(end > filesize)
282 282
 		    end = filesize;
283 283
 
284
-		unlink(tmp);
285
-		free(tmp);
286 284
 	    }
287 285
 
288 286
 	}
289 287
 
290
-	/* find signature beginning */
288
+	/* find signature start */
291 289
 	found = 0;
292 290
 	jmp = 50;
293 291
 	pos = end - jmp;
... ...
@@ -377,7 +379,7 @@ void sigtool(struct optstruct *opt)
377 377
 	tmp = cut(f, pos, end);
378 378
 
379 379
 	mprintf("\nThe scanner was executed %d times.\n", exec);
380
-	mprintf("The signature length is %d, so the length of the hex string should be %d\n", end - pos, 2 * (end - pos));
380
+	mprintf("The signature length is %d (%d hex)\n", end - pos, 2 * (end - pos));
381 381
 
382 382
 	if(end - pos < MIN_LENGTH) {
383 383
 	    mprintf("\nWARNING: THE SIGNATURE IS TOO SMALL (PROBABLY ONLY A PART OF A REAL SIGNATURE).\n");
... ...
@@ -451,7 +453,7 @@ int countlines(const char *filename)
451 451
 
452 452
 int build(struct optstruct *opt)
453 453
 {
454
-	int ret, no = 0, bytes, itmp;
454
+	int ret, no = 0, realno = 0, bytes, itmp;
455 455
 	struct stat foo;
456 456
 	char buffer[BUFFSIZE], *tarfile = NULL, *gzfile = NULL, header[257],
457 457
 	     smbuff[25], *pt;
... ...
@@ -460,7 +462,7 @@ int build(struct optstruct *opt)
460 460
 	gzFile *gz;
461 461
 	time_t timet;
462 462
 	struct tm *brokent;
463
-	struct cl_cvd *old;
463
+	struct cl_cvd *oldcvd = NULL;
464 464
 
465 465
     /* build a tar.gz archive
466 466
      * we need: COPYING and {viruses.db, viruses.db2}+
... ...
@@ -481,17 +483,26 @@ int build(struct optstruct *opt)
481 481
 
482 482
     if((ret = cl_loaddbdir(".", &root, &no))) {
483 483
 	mprintf("!Can't load database: %s\n", cl_strerror(ret));
484
-        exit(1);
484
+	exit(1);
485 485
     }
486 486
 
487 487
     cl_freetrie(root);
488 488
 
489 489
     mprintf("Database properly parsed.\n");
490 490
 
491
-    if(!no)
491
+    if(!no) {
492 492
 	mprintf("WARNING: There are no signatures in the database(s).\n");
493
-    else
493
+    } else {
494 494
 	mprintf("Signatures: %d\n", no);
495
+	realno = countlines("viruses.db") + countlines("viruses.db2");
496
+
497
+	if(realno != no) {
498
+	    mprintf("!Signatures in database: %d. Loaded: %d.\n", realno, no);
499
+	    mprintf("Please check the current directory and remove unnecessary databases\n");
500
+	    mprintf("or install the latest ClamAV version.\n");
501
+	    exit(1);
502
+	}
503
+    }
495 504
 
496 505
     tarfile = cl_gentemp(".");
497 506
 
... ...
@@ -501,7 +512,7 @@ int build(struct optstruct *opt)
501 501
 	    exit(1);
502 502
 	case 0:
503 503
 	    {
504
-		char *args[] = { "tar", "-cvf", tarfile, "COPYING", "viruses.db", "viruses.db2", NULL };
504
+		char *args[] = { "tar", "-cvf", tarfile, "COPYING", "viruses.db", "viruses.db2", "Notes", NULL };
505 505
 		execv("/bin/tar", args);
506 506
 		mprintf("!Can't execute tar\n");
507 507
 		perror("tar");
... ...
@@ -536,6 +547,12 @@ int build(struct optstruct *opt)
536 536
 
537 537
     gzclose(gz);
538 538
 
539
+
540
+    /* try to read cvd header of old database */
541
+    sprintf(buffer, "%s/%s", cl_retdbdir(), getargc(opt, 'b'));
542
+    if((oldcvd = cl_cvdhead(buffer)) == NULL)
543
+	mprintf("WARNING: CAN'T READ CVD HEADER OF CURRENT DATABASE %s\n", buffer);
544
+
539 545
     /* generate header */
540 546
 
541 547
     /* magic string */
... ...
@@ -554,11 +571,14 @@ int build(struct optstruct *opt)
554 554
 
555 555
     /* ... increment version number by one */
556 556
 
557
-    mprintf("!Can't read database version number from current local database\n");
558
-    fflush(stdin);
559
-    mprintf("Please enter a version number for the new database: ");
560
-    scanf("%d", &itmp);
561
-    sprintf(smbuff, "%d:", itmp);
557
+    if(oldcvd) {
558
+	sprintf(smbuff, "%d:", oldcvd->version + 1);
559
+    } else {
560
+	fflush(stdin);
561
+	mprintf("Version number: ");
562
+	scanf("%d", &itmp);
563
+	sprintf(smbuff, "%d:", itmp);
564
+    }
562 565
     strcat(header, smbuff);
563 566
 
564 567
     /* number of signatures */
... ...
@@ -586,6 +606,7 @@ int build(struct optstruct *opt)
586 586
     fclose(fd);
587 587
     if(!(pt = getdsig(getargc(opt, 's'), smbuff, buffer))) {
588 588
 	mprintf("No digital signature - no CVD file...\n");
589
+	unlink(gzfile);
589 590
 	exit(1);
590 591
     }
591 592
 
... ...
@@ -598,7 +619,8 @@ int build(struct optstruct *opt)
598 598
 
599 599
     /* fill up with spaces */
600 600
     if(strlen(header) > 512) {
601
-	mprintf("!Generated signature is too long.\n");
601
+	mprintf("!Generated header is too long.\n");
602
+	unlink(gzfile);
602 603
 	exit(1);
603 604
     }
604 605
 
... ...
@@ -610,6 +632,7 @@ int build(struct optstruct *opt)
610 610
     pt = getargc(opt, 'b');
611 611
     if((cvd = fopen(pt, "wb")) == NULL) {
612 612
 	mprintf("!Can't write the final database %s\n", pt);
613
+	unlink(gzfile);
613 614
 	exit(1);
614 615
     }
615 616
 
... ...
@@ -670,7 +693,7 @@ void cvdinfo(struct optstruct *opt)
670 670
 void help(void)
671 671
 {
672 672
     mprintf("\n");
673
-    mprintf("		   Clam AntiVirus: Signature Tool (sigtool)  "VERSION"\n");
673
+    mprintf("		Clam AntiVirus: Signature Tool (sigtool)  "VERSION"\n");
674 674
     mprintf("	       (c) 2002, 2003 Tomasz Kojm <zolw@konarski.edu.pl>\n");
675 675
     mprintf("\n");
676 676
     mprintf("   --help		    -h		show help\n");
... ...
@@ -684,9 +707,11 @@ void help(void)
684 684
     mprintf("   --command		    -c		scanner command string, with options\n");
685 685
     mprintf("   --string		    -s		'virus found' string in scan. output\n");
686 686
     mprintf("   --file		    -f		infected file\n");
687
-    mprintf("	--info FILE	    -i FILE	print database information\n");
688
-    mprintf("   --build NAME	    -b NAME		Build database\n");
689
-    mprintf("   --server ADDR	    -s ADDR	    ClamAV Signing Service address\n");
687
+    mprintf("   --info FILE		    -i FILE	print database information\n");
688
+    mprintf("   --build NAME		    -b NAME	Build a CVD file\n");
689
+    mprintf("   --server ADDR	    -s ADDR	ClamAV Signing Service address\n");
690
+    mprintf("   --unpack FILE	    -u FILE	Unpack a CVD file\n");
691
+    mprintf("\n");
690 692
 
691 693
     exit(0);
692 694
 }
... ...
@@ -755,3 +780,26 @@ char *getdsig(const char *host, const char *user, const char *data)
755 755
     pt += 10;
756 756
     return strdup(pt);
757 757
 }
758
+
759
+int unpack(struct optstruct *opt)
760
+{
761
+	FILE *fd;
762
+	struct cl_cvd *cvd;
763
+
764
+
765
+    if((fd = fopen(getargc(opt, 'u'), "rb")) == NULL) {
766
+	mprintf("!Can't open CVD file %s\n");
767
+	exit(1);
768
+    }
769
+
770
+    fseek(fd, 512L, SEEK_SET);
771
+
772
+    if(cli_untgz(fileno(fd), ".")) {
773
+	mprintf("!Can't unpack file.\n");
774
+	fclose(fd);
775
+	exit(1);
776
+    }
777
+
778
+    fclose(fd);
779
+    exit(0);
780
+}