Browse code

add support for .cld containers

git-svn: trunk@3444

Tomasz Kojm authored on 2007/12/20 07:06:32
Showing 13 changed files
... ...
@@ -1,3 +1,12 @@
1
+Wed Dec 19 22:11:17 CET 2007 (tk)
2
+---------------------------------
3
+  * freshclam, libclamav: drop support for .inc directories and instead use
4
+			  local containers with .cvd-like structure but
5
+			  uncompressed by default
6
+  * libclamav/lockdb.[ch]: remove files (no longer needed)
7
+  * TODO: sigtool, optional compression for .cld in freshclam
8
+  * .inc dirs are no longer used and should be removed manually
9
+
1 10
 Wed Dec 19 16:22:24 CET 2007 (tk)
2 11
 ---------------------------------
3 12
   * shared/tar.[ch]: minimalistic tar archiver for sigtool and freshclam
... ...
@@ -33,6 +33,8 @@ freshclam_SOURCES = \
33 33
     $(top_srcdir)/shared/sha256.c \
34 34
     $(top_srcdir)/shared/cdiff.c \
35 35
     $(top_srcdir)/shared/cdiff.h \
36
+    $(top_srcdir)/shared/tar.c \
37
+    $(top_srcdir)/shared/tar.h \
36 38
     freshclam.c \
37 39
     manager.c \
38 40
     manager.h \
... ...
@@ -68,9 +68,10 @@ binPROGRAMS_INSTALL = $(INSTALL_PROGRAM)
68 68
 PROGRAMS = $(bin_PROGRAMS)
69 69
 am_freshclam_OBJECTS = output.$(OBJEXT) cfgparser.$(OBJEXT) \
70 70
 	getopt.$(OBJEXT) misc.$(OBJEXT) options.$(OBJEXT) \
71
-	sha256.$(OBJEXT) cdiff.$(OBJEXT) freshclam.$(OBJEXT) \
72
-	manager.$(OBJEXT) notify.$(OBJEXT) dns.$(OBJEXT) \
73
-	execute.$(OBJEXT) nonblock.$(OBJEXT) mirman.$(OBJEXT)
71
+	sha256.$(OBJEXT) cdiff.$(OBJEXT) tar.$(OBJEXT) \
72
+	freshclam.$(OBJEXT) manager.$(OBJEXT) notify.$(OBJEXT) \
73
+	dns.$(OBJEXT) execute.$(OBJEXT) nonblock.$(OBJEXT) \
74
+	mirman.$(OBJEXT)
74 75
 freshclam_OBJECTS = $(am_freshclam_OBJECTS)
75 76
 freshclam_LDADD = $(LDADD)
76 77
 DEFAULT_INCLUDES = -I. -I$(top_builddir)@am__isrc@
... ...
@@ -228,6 +229,8 @@ freshclam_SOURCES = \
228 228
     $(top_srcdir)/shared/sha256.c \
229 229
     $(top_srcdir)/shared/cdiff.c \
230 230
     $(top_srcdir)/shared/cdiff.h \
231
+    $(top_srcdir)/shared/tar.c \
232
+    $(top_srcdir)/shared/tar.h \
231 233
     freshclam.c \
232 234
     manager.c \
233 235
     manager.h \
... ...
@@ -328,6 +331,7 @@ distclean-compile:
328 328
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/options.Po@am__quote@
329 329
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/output.Po@am__quote@
330 330
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sha256.Po@am__quote@
331
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tar.Po@am__quote@
331 332
 
332 333
 .c.o:
333 334
 @am__fastdepCC_TRUE@	$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
... ...
@@ -448,6 +452,20 @@ cdiff.obj: $(top_srcdir)/shared/cdiff.c
448 448
 @AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
449 449
 @am__fastdepCC_FALSE@	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o cdiff.obj `if test -f '$(top_srcdir)/shared/cdiff.c'; then $(CYGPATH_W) '$(top_srcdir)/shared/cdiff.c'; else $(CYGPATH_W) '$(srcdir)/$(top_srcdir)/shared/cdiff.c'; fi`
450 450
 
451
+tar.o: $(top_srcdir)/shared/tar.c
452
+@am__fastdepCC_TRUE@	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT tar.o -MD -MP -MF $(DEPDIR)/tar.Tpo -c -o tar.o `test -f '$(top_srcdir)/shared/tar.c' || echo '$(srcdir)/'`$(top_srcdir)/shared/tar.c
453
+@am__fastdepCC_TRUE@	mv -f $(DEPDIR)/tar.Tpo $(DEPDIR)/tar.Po
454
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	source='$(top_srcdir)/shared/tar.c' object='tar.o' libtool=no @AMDEPBACKSLASH@
455
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
456
+@am__fastdepCC_FALSE@	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o tar.o `test -f '$(top_srcdir)/shared/tar.c' || echo '$(srcdir)/'`$(top_srcdir)/shared/tar.c
457
+
458
+tar.obj: $(top_srcdir)/shared/tar.c
459
+@am__fastdepCC_TRUE@	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT tar.obj -MD -MP -MF $(DEPDIR)/tar.Tpo -c -o tar.obj `if test -f '$(top_srcdir)/shared/tar.c'; then $(CYGPATH_W) '$(top_srcdir)/shared/tar.c'; else $(CYGPATH_W) '$(srcdir)/$(top_srcdir)/shared/tar.c'; fi`
460
+@am__fastdepCC_TRUE@	mv -f $(DEPDIR)/tar.Tpo $(DEPDIR)/tar.Po
461
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	source='$(top_srcdir)/shared/tar.c' object='tar.obj' libtool=no @AMDEPBACKSLASH@
462
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
463
+@am__fastdepCC_FALSE@	$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o tar.obj `if test -f '$(top_srcdir)/shared/tar.c'; then $(CYGPATH_W) '$(top_srcdir)/shared/tar.c'; else $(CYGPATH_W) '$(srcdir)/$(top_srcdir)/shared/tar.c'; fi`
464
+
451 465
 mostlyclean-libtool:
452 466
 	-rm -f *.lo
453 467
 
... ...
@@ -49,6 +49,7 @@
49 49
 #include <time.h>
50 50
 #include <fcntl.h>
51 51
 #include <sys/stat.h>
52
+#include <dirent.h>
52 53
 #include <errno.h>
53 54
 
54 55
 #include "manager.h"
... ...
@@ -63,12 +64,12 @@
63 63
 #include "shared/output.h"
64 64
 #include "shared/misc.h"
65 65
 #include "shared/cdiff.h"
66
+#include "shared/tar.h"
66 67
 
67 68
 #include "libclamav/clamav.h"
68 69
 #include "libclamav/others.h"
69 70
 #include "libclamav/str.h"
70 71
 #include "libclamav/cvd.h"
71
-#include "libclamav/lockdb.h"
72 72
 
73 73
 #ifndef	O_BINARY
74 74
 #define	O_BINARY	0
... ...
@@ -676,97 +677,78 @@ static int getfile(const char *srcfile, const char *destfile, const char *hostna
676 676
     return 0;
677 677
 }
678 678
 
679
-static int getcvd(const char *dbfile, const char *hostname, char *ip, const char *localip, const char *proxy, int port, const char *user, const char *pass, const char *uas, int nodb, unsigned int newver, int ctimeout, int rtimeout, struct mirdat *mdat)
679
+static int getcvd(const char *cvdfile, const char *newfile, const char *hostname, char *ip, const char *localip, const char *proxy, int port, const char *user, const char *pass, const char *uas, int nodb, unsigned int newver, int ctimeout, int rtimeout, struct mirdat *mdat)
680 680
 {
681
-	char *tempname;
682 681
 	struct cl_cvd *cvd;
683 682
 	int ret;
684 683
 
685 684
 
686
-    tempname = cli_gentemp(".");
687
-
688
-    logg("*Retrieving http://%s/%s\n", hostname, dbfile);
689
-    if((ret = getfile(dbfile, tempname, hostname, ip, localip, proxy, port, user, pass, uas, ctimeout, rtimeout, mdat))) {
690
-        logg("!Can't download %s from %s\n", dbfile, hostname);
691
-        unlink(tempname);
692
-        free(tempname);
685
+    logg("*Retrieving http://%s/%s\n", hostname, cvdfile);
686
+    if((ret = getfile(cvdfile, newfile, hostname, ip, localip, proxy, port, user, pass, uas, ctimeout, rtimeout, mdat))) {
687
+        logg("!Can't download %s from %s\n", cvdfile, hostname);
688
+        unlink(newfile);
693 689
         return ret;
694 690
     }
695 691
 
696
-    if((ret = cl_cvdverify(tempname))) {
692
+    if((ret = cl_cvdverify(newfile))) {
697 693
         logg("!Verification: %s\n", cl_strerror(ret));
698
-        unlink(tempname);
699
-        free(tempname);
694
+        unlink(newfile);
700 695
         return 54;
701 696
     }
702 697
 
703
-    if(!(cvd = cl_cvdhead(tempname))) {
704
-	logg("!Can't read CVD header of new %s database.\n", dbfile);
705
-	unlink(tempname);
706
-	free(tempname);
698
+    if(!(cvd = cl_cvdhead(newfile))) {
699
+	logg("!Can't read CVD header of new %s database.\n", cvdfile);
700
+	unlink(newfile);
707 701
 	return 54;
708 702
     }
709 703
 
710 704
     if(cvd->version < newver) {
711 705
 	logg("^Mirror %s is not synchronized.\n", ip);
712 706
     	cl_cvdfree(cvd);
713
-	unlink(tempname);
714
-	free(tempname);
707
+	unlink(newfile);
715 708
 	return 59;
716 709
     }
717 710
 
718
-    if(!nodb && unlink(dbfile)) {
719
-	logg("!Can't unlink %s. Please fix it and try again.\n", dbfile);
720
-    	cl_cvdfree(cvd);
721
-	unlink(tempname);
722
-	free(tempname);
723
-	return 53;
724
-    } else {
725
-    	if(rename(tempname, dbfile) == -1) {
726
-    	    if(errno == EEXIST) {
727
-    	        unlink(dbfile);
728
-    	        if(rename(tempname, dbfile) == -1) {
729
-                   logg("!All attempts to rename the temporary file failed: %s\n", strerror(errno));
730
-		   return 57;
731
-		}
732
-            }
733
-        }
734
-    }
735
-
711
+    cl_cvdfree(cvd);
736 712
     return 0;
737 713
 }
738 714
 
739
-static int chdir_inc(const char *dbname)
715
+static int chdir_tmp(const char *dbname, const char *tmpdir)
740 716
 {
741
-	struct stat sb;
742
-	char path[32], dbfile[32];
717
+	char cvdfile[32];
743 718
 
744 719
 
745
-    sprintf(path, "%s.inc", dbname);
746
-    sprintf(dbfile, "%s.cvd", dbname);
720
+    if(access(tmpdir, R_OK|W_OK) == -1) {
721
+	sprintf(cvdfile, "%s.cvd", dbname);
722
+	if(access(cvdfile, R_OK) == -1) {
723
+	    sprintf(cvdfile, "%s.cld", dbname);
724
+	    if(access(cvdfile, R_OK) == -1) {
725
+		logg("!chdir_tmp: Can't access local %s database\n", dbname);
726
+		return -1;
727
+	    }
728
+	}
747 729
 
748
-    if(stat(path, &sb) == -1) {
749
-	if(mkdir(path, 0755) == -1) {
750
-	    logg("!chdir_inc: Can't create directory %s\n", path);
730
+	if(mkdir(tmpdir, 0755) == -1) {
731
+	    logg("!chdir_tmp: Can't create directory %s\n", tmpdir);
751 732
 	    return -1;
752 733
 	}
753 734
 
754
-	if(cvd_unpack(dbfile, path) == -1) {
755
-	    logg("!chdir_inc: Can't unpack %s into %s\n", dbfile, path);
756
-	    cli_rmdirs(path);
735
+	if(cvd_unpack(cvdfile, tmpdir) == -1) {
736
+	    logg("!chdir_tmp: Can't unpack %s into %s\n", cvdfile, tmpdir);
737
+	    cli_rmdirs(tmpdir);
757 738
 	    return -1;
758 739
 	}
759 740
     }
760 741
 
761
-    if(chdir(path) == -1) {
762
-	logg("!chdir_inc: Can't change directory to %s\n", path);
742
+    if(chdir(tmpdir) == -1) {
743
+	logg("!chdir_tmp: Can't change directory to %s\n", tmpdir);
763 744
 	return -1;
764 745
     }
765 746
 
766 747
     return 0;
767 748
 }
768 749
 
769
-static int getpatch(const char *dbname, int version, const char *hostname, char *ip, const char *localip, const char *proxy, int port, const char *user, const char *pass, const char *uas, int ctimeout, int rtimeout, struct mirdat *mdat)
750
+static int getpatch(const char *dbname, const char *tmpdir, int version, const char *hostname, char *ip, const char *localip, const char *proxy, int port, const char *user, const char *pass, const char *uas, int ctimeout, int rtimeout, struct mirdat *mdat)
770 751
 {
771 752
 	char *tempname, patch[32], olddir[512];
772 753
 	int ret, fd;
... ...
@@ -777,7 +759,7 @@ static int getpatch(const char *dbname, int version, const char *hostname, char
777 777
 	return 50; /* FIXME */
778 778
     }
779 779
 
780
-    if(chdir_inc(dbname) == -1)
780
+    if(chdir_tmp(dbname, tmpdir) == -1)
781 781
 	return 50;
782 782
 
783 783
     tempname = cli_gentemp(".");
... ...
@@ -816,64 +798,129 @@ static int getpatch(const char *dbname, int version, const char *hostname, char
816 816
     return 0;
817 817
 }
818 818
 
819
-static struct cl_cvd *currentdb(const char *dbname, unsigned int *inc)
819
+static struct cl_cvd *currentdb(const char *dbname, char *localname)
820 820
 {
821
-	struct stat sb;
822
-	char path[512];
821
+	char db[32];
823 822
 	struct cl_cvd *cvd = NULL;
824 823
 
825 824
 
826
-    snprintf(path, sizeof(path), "%s.inc", dbname);
827
-    if(stat(path, &sb) != -1) {
828
-	snprintf(path, sizeof(path), "%s.inc/%s.info", dbname, dbname);
829
-	if(inc)
830
-	    *inc = 1;
831
-    } else {
832
-	snprintf(path, sizeof(path), "%s.cvd", dbname);
833
-	if(inc)
834
-	    *inc = 0;
825
+    snprintf(db, sizeof(db), "%s.cvd", dbname);
826
+    if(localname)
827
+	strcpy(localname, db);
828
+
829
+    if(access(db, R_OK) == -1) {
830
+	snprintf(db, sizeof(db), "%s.cld", dbname);
831
+	if(localname)
832
+	    strcpy(localname, db);
835 833
     }
836 834
 
837
-    if(!access(path, R_OK))
838
-	cvd = cl_cvdhead(path);
835
+    if(!access(db, R_OK))
836
+	cvd = cl_cvdhead(db);
839 837
 
840 838
     return cvd;
841 839
 }
842 840
 
841
+static int buildcld(const char *tmpdir, const char *dbname, const char *newfile)
842
+{
843
+	DIR *dir;
844
+	char cwd[512], info[32], buff[512], *pt;
845
+	struct dirent *dent;
846
+	int fd;
847
+
848
+
849
+    getcwd(cwd, sizeof(cwd));
850
+    if(chdir(tmpdir) == -1) {
851
+	logg("!buildcld: Can't access directory %s\n", tmpdir);
852
+	return -1;
853
+    }
854
+
855
+    snprintf(info, sizeof(info), "%s.info", dbname);
856
+    if((fd = open(info, O_RDONLY|O_BINARY)) == -1) {
857
+	logg("!buildcld: Can't open %s\n", info);
858
+	chdir(cwd);
859
+	return -1;
860
+    }
861
+
862
+    if(read(fd, buff, 512) == -1) {
863
+	logg("!buildcld: Can't read %s\n", info);
864
+	chdir(cwd);
865
+	close(fd);
866
+	return -1;
867
+    }
868
+    close(fd);
869
+
870
+    if(!(pt = strchr(buff, '\n'))) {
871
+	logg("!buildcld: Bad format of %s\n", info);
872
+	chdir(cwd);
873
+	return -1;
874
+    }
875
+    memset(pt, ' ', 512 + buff - pt);
876
+
877
+    if((fd = open(newfile, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644)) == -1) {
878
+	logg("!buildcld: Can't open %s for writing\n", newfile);
879
+	chdir(cwd);
880
+	return -1;
881
+    }
882
+    if(write(fd, buff, 512) != 512) {
883
+	logg("!buildcld: Can't write to %s\n", newfile);
884
+	chdir(cwd);
885
+	unlink(newfile);
886
+	close(fd);
887
+	return -1;
888
+    }
889
+    close(fd);
890
+
891
+    if((dir = opendir(".")) == NULL) {
892
+	logg("!buildcld: Can't open directory %s\n", tmpdir);
893
+	chdir(cwd);
894
+	unlink(newfile);
895
+	return -1;
896
+    }
897
+
898
+    while((dent = readdir(dir))) {
899
+#ifndef C_INTERIX
900
+	if(dent->d_ino)
901
+#endif
902
+	{
903
+	    if(!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
904
+		continue;
905
+
906
+	    if(tar_addfile(newfile, dent->d_name) == -1) {
907
+		logg("!buildcld: Can't add %s to .cld file\n", dent->d_name);
908
+		chdir(cwd);
909
+		unlink(newfile);
910
+		closedir(dir);
911
+		return -1;
912
+	    }
913
+	}
914
+    }
915
+    closedir(dir);
916
+
917
+    if(chdir(cwd) == -1) {
918
+	logg("!buildcld: Can't return to previous directory %s\n", cwd);
919
+	return -1;
920
+    }
921
+
922
+    return 0;
923
+}
924
+
843 925
 static int updatedb(const char *dbname, const char *hostname, char *ip, int *signo, const struct cfgstruct *copt, const char *dnsreply, char *localip, int outdated, struct mirdat *mdat)
844 926
 {
845 927
 	struct cl_cvd *current, *remote;
846 928
 	const struct cfgstruct *cpt;
847 929
 	unsigned int nodb = 0, currver = 0, newver = 0, port = 0, i, j;
848 930
 	int ret, ims = -1;
849
-	char *pt, dbfile[32], dbinc[32], *bacinc = NULL;
931
+	char *pt, cvdfile[32], localname[32], *tmpdir = NULL, *newfile, cwd[512];
850 932
 	const char *proxy = NULL, *user = NULL, *pass = NULL, *uas = NULL;
851
-	unsigned int flevel = cl_retflevel(), inc, maxattempts;
852
-	struct stat sb;
933
+	unsigned int flevel = cl_retflevel(), maxattempts;
853 934
 	int ctimeout, rtimeout;
854 935
 
855 936
 
856
-    snprintf(dbfile, sizeof(dbfile), "%s.cvd", dbname);
857
-    snprintf(dbinc, sizeof(dbinc), "%s.inc", dbname);
937
+    snprintf(cvdfile, sizeof(cvdfile), "%s.cvd", dbname);
858 938
 
859
-    if(!(current = currentdb(dbname, &inc))) {
939
+    if(!(current = currentdb(dbname, localname))) {
860 940
 	nodb = 1;
861
-	inc = 0;
862
-	if(stat(dbinc, &sb) != -1) {
863
-	    logg("^Removing corrupted incremental directory %s\n", dbinc);
864
-	    if(cli_rmdirs(dbinc)) {
865
-		logg("!Can't remove incremental directory\n");
866
-		return 53;
867
-	    }
868
-
869
-	    if(stat(dbfile, &sb) != -1) {
870
-		logg("^Removing obsolete %s\n", dbfile);
871
-		if(unlink(dbfile)) {
872
-		    logg("!Can't unlink %s\n", dbfile);
873
-		    return 53;
874
-		}
875
-	    }
876
-	}
941
+	strcpy(localname, cvdfile);
877 942
     } else {
878 943
 	mdat->dbflevel = current->fl;
879 944
     }
... ...
@@ -896,7 +943,7 @@ static int updatedb(const char *dbname, const char *hostname, char *ip, int *sig
896 896
 		logg("^Broken database version in TXT record.\n");
897 897
 	    } else {
898 898
 		newver = atoi(pt);
899
-		logg("*%s version from DNS: %d\n", dbfile, newver);
899
+		logg("*%s version from DNS: %d\n", cvdfile, newver);
900 900
 	    }
901 901
 	    free(pt);
902 902
 	} else {
... ...
@@ -936,17 +983,17 @@ static int updatedb(const char *dbname, const char *hostname, char *ip, int *sig
936 936
 
937 937
     if(!nodb && !newver) {
938 938
 
939
-	remote = remote_cvdhead(dbfile, hostname, ip, localip, proxy, port, user, pass, uas, &ims, ctimeout, rtimeout, mdat);
939
+	remote = remote_cvdhead(cvdfile, hostname, ip, localip, proxy, port, user, pass, uas, &ims, ctimeout, rtimeout, mdat);
940 940
 
941 941
 	if(!nodb && !ims) {
942
-	    logg("%s is up to date (version: %d, sigs: %d, f-level: %d, builder: %s)\n", inc ? dbinc : dbfile, current->version, current->sigs, current->fl, current->builder);
942
+	    logg("%s is up to date (version: %d, sigs: %d, f-level: %d, builder: %s)\n", localname, current->version, current->sigs, current->fl, current->builder);
943 943
 	    *signo += current->sigs;
944 944
 	    cl_cvdfree(current);
945 945
 	    return 1;
946 946
 	}
947 947
 
948 948
 	if(!remote) {
949
-	    logg("^Can't read %s header from %s (IP: %s)\n", dbfile, hostname, ip);
949
+	    logg("^Can't read %s header from %s (IP: %s)\n", cvdfile, hostname, ip);
950 950
 	    cl_cvdfree(current);
951 951
 	    return 58;
952 952
 	}
... ...
@@ -956,7 +1003,7 @@ static int updatedb(const char *dbname, const char *hostname, char *ip, int *sig
956 956
     }
957 957
 
958 958
     if(!nodb && (current->version >= newver)) {
959
-	logg("%s is up to date (version: %d, sigs: %d, f-level: %d, builder: %s)\n", inc ? dbinc : dbfile, current->version, current->sigs, current->fl, current->builder);
959
+	logg("%s is up to date (version: %d, sigs: %d, f-level: %d, builder: %s)\n", localname, current->version, current->sigs, current->fl, current->builder);
960 960
 
961 961
 	if(!outdated && flevel < current->fl) {
962 962
 	    /* display warning even for already installed database */
... ...
@@ -997,29 +1044,26 @@ static int updatedb(const char *dbname, const char *hostname, char *ip, int *sig
997 997
     if(!cfgopt(copt, "ScriptedUpdates")->enabled)
998 998
 	nodb = 1;
999 999
 
1000
+    getcwd(cwd, sizeof(cwd));
1001
+    newfile = cli_gentemp(cwd);
1002
+
1000 1003
     if(nodb) {
1001
-	ret = getcvd(dbfile, hostname, ip, localip, proxy, port, user, pass, uas, nodb, newver, ctimeout, rtimeout, mdat);
1004
+	ret = getcvd(cvdfile, newfile, hostname, ip, localip, proxy, port, user, pass, uas, nodb, newver, ctimeout, rtimeout, mdat);
1002 1005
 	if(ret) {
1003 1006
 	    memset(ip, 0, 16);
1007
+	    free(newfile);
1004 1008
 	    return ret;
1005 1009
 	}
1010
+	snprintf(localname, sizeof(localname), "%s.cvd", dbname);
1006 1011
 
1007 1012
     } else {
1008 1013
 	ret = 0;
1009 1014
 
1010
-	if(!access(dbinc, X_OK)) {
1011
-	    if((bacinc = cli_gentemp("."))) {
1012
-		if(dircopy(dbinc, bacinc) == -1) {
1013
-		    free(bacinc);
1014
-		    bacinc = NULL;
1015
-		}
1016
-	    }
1017
-	}
1018
-
1015
+	tmpdir = cli_gentemp(".");
1019 1016
 	maxattempts = cfgopt(copt, "MaxAttempts")->numarg;
1020 1017
 	for(i = currver + 1; i <= newver; i++) {
1021 1018
 	    for(j = 0; j < maxattempts; j++) {
1022
-		ret = getpatch(dbname, i, hostname, ip, localip, proxy, port, user, pass, uas, ctimeout, rtimeout, mdat);
1019
+		ret = getpatch(dbname, tmpdir, i, hostname, ip, localip, proxy, port, user, pass, uas, ctimeout, rtimeout, mdat);
1023 1020
 		if(ret == 52 || ret == 58) {
1024 1021
 		    memset(ip, 0, 16);
1025 1022
 		    continue;
... ...
@@ -1032,43 +1076,58 @@ static int updatedb(const char *dbname, const char *hostname, char *ip, int *sig
1032 1032
 	}
1033 1033
 
1034 1034
 	if(ret) {
1035
-	    logg("^Incremental update failed, trying to download %s\n", dbfile);
1036
-
1037
-	    ret = getcvd(dbfile, hostname, ip, localip, proxy, port, user, pass, uas, 1, newver, ctimeout, rtimeout, mdat);
1035
+	    cli_rmdirs(tmpdir);
1036
+	    free(tmpdir);
1037
+	    logg("^Incremental update failed, trying to download %s\n", cvdfile);
1038
+	    ret = getcvd(cvdfile, newfile, hostname, ip, localip, proxy, port, user, pass, uas, 1, newver, ctimeout, rtimeout, mdat);
1038 1039
 	    if(ret) {
1039
-		if(bacinc) {
1040
-		    logg("*Restoring incremental directory %s from backup\n", dbinc);
1041
-		    cli_rmdirs(dbinc);
1042
-		    rename(bacinc, dbinc);
1043
-		    free(bacinc);
1044
-		}
1040
+		free(newfile);
1045 1041
 		return ret;
1046
-	    } else {
1047
-		logg("*Removing incremental directory %s\n", dbinc);
1048
-		cli_rmdirs(dbinc);
1049 1042
 	    }
1050
-
1043
+	    snprintf(localname, sizeof(localname), "%s.cvd", dbname);
1051 1044
 	} else {
1052
-	    unlink(dbfile);
1045
+	    if(buildcld(tmpdir, dbname, newfile) == -1) {
1046
+		logg("!Can't create local database\n");
1047
+		cli_rmdirs(tmpdir);
1048
+		free(tmpdir);
1049
+		free(newfile);
1050
+		return 70; /* FIXME */
1051
+	    }
1052
+	    snprintf(localname, sizeof(localname), "%s.cld", dbname);
1053
+	    cli_rmdirs(tmpdir);
1054
+	    free(tmpdir);
1053 1055
 	}
1056
+    }
1054 1057
 
1055
-	if(bacinc) {
1056
-	    logg("*Removing backup directory %s\n", bacinc);
1057
-	    cli_rmdirs(bacinc);
1058
-	    free(bacinc);
1059
-	}
1058
+    if(!nodb && !access(cvdfile, R_OK) && unlink(cvdfile)) {
1059
+	logg("!Can't unlink %s. Please fix it and try again.\n", cvdfile);
1060
+	unlink(newfile);
1061
+	free(newfile);
1062
+	return 53;
1060 1063
     }
1061 1064
 
1062
-    if(!(current = currentdb(dbname, NULL))) {
1063
-	/* should never be reached */
1064
-	logg("!Can't parse new database\n");
1065
-	return 55; /* FIXME */
1065
+    if(!nodb && strcmp(cvdfile, localname) && !access(localname, R_OK) && unlink(localname)) {
1066
+	logg("!Can't unlink %s. Please fix it and try again.\n", localname);
1067
+	unlink(newfile);
1068
+	free(newfile);
1069
+	return 53;
1066 1070
     }
1067 1071
 
1068
-    if(nodb && inc)
1069
-	cli_rmdirs(dbinc);
1072
+    if(rename(newfile, localname) == -1) {
1073
+	logg("!Can't rename %s to %s\n", newfile, localname);
1074
+	unlink(newfile);
1075
+	free(newfile);
1076
+	return 57;
1077
+    }
1078
+    free(newfile);
1070 1079
 
1071
-    logg("%s updated (version: %d, sigs: %d, f-level: %d, builder: %s)\n", inc ? dbinc : dbfile, current->version, current->sigs, current->fl, current->builder);
1080
+    if(!(current = cl_cvdhead(localname))) {
1081
+	logg("!Can't parse new database %s\n", localname);
1082
+	unlink(localname);
1083
+	return 55; /* FIXME */
1084
+    }
1085
+
1086
+    logg("%s updated (version: %d, sigs: %d, f-level: %d, builder: %s)\n", localname, current->version, current->sigs, current->fl, current->builder);
1072 1087
 
1073 1088
     if(flevel < current->fl) {
1074 1089
 	logg("^Your ClamAV installation is OUTDATED!\n");
... ...
@@ -1085,7 +1144,7 @@ int downloadmanager(const struct cfgstruct *copt, const struct optstruct *opt, c
1085 1085
 {
1086 1086
 	time_t currtime;
1087 1087
 	int ret, updated = 0, outdated = 0, signo = 0;
1088
-	unsigned int ttl, try = 0;
1088
+	unsigned int ttl;
1089 1089
 	char ipaddr[16], *dnsreply = NULL, *pt, *localip = NULL, *newver = NULL;
1090 1090
 	const char *arg = NULL;
1091 1091
 	const struct cfgstruct *cpt;
... ...
@@ -1173,19 +1232,6 @@ int downloadmanager(const struct cfgstruct *copt, const struct optstruct *opt, c
1173 1173
 	localip = cpt->strarg;
1174 1174
     }
1175 1175
 
1176
-    while(cli_writelockdb(dbdir, 0) == CL_ELOCKDB) {
1177
-	logg("*Waiting to lock database directory: %s\n", dbdir);
1178
-	sleep(5);
1179
-	if(++try > 12) {
1180
-	    logg("!Can't lock database directory: %s\n", dbdir);
1181
-	    if(dnsreply)
1182
-		free(dnsreply);
1183
-	    if(newver)
1184
-		free(newver);
1185
-	    return 61; 
1186
-	}
1187
-    }
1188
-
1189 1176
     if(cfgopt(copt, "HTTPProxyServer")->enabled)
1190 1177
 	mirman_read("mirrors.dat", &mdat, 0);
1191 1178
     else
... ...
@@ -1201,7 +1247,6 @@ int downloadmanager(const struct cfgstruct *copt, const struct optstruct *opt, c
1201 1201
 	    free(newver);
1202 1202
 
1203 1203
 	mirman_write("mirrors.dat", &mdat);
1204
-	cli_unlockdb(dbdir);
1205 1204
 	return ret;
1206 1205
 
1207 1206
     } else if(ret == 0)
... ...
@@ -1216,7 +1261,6 @@ int downloadmanager(const struct cfgstruct *copt, const struct optstruct *opt, c
1216 1216
 	    free(newver);
1217 1217
 
1218 1218
 	mirman_write("mirrors.dat", &mdat);
1219
-	cli_unlockdb(dbdir);
1220 1219
 	return ret;
1221 1220
 
1222 1221
     } else if(ret == 0)
... ...
@@ -1226,7 +1270,6 @@ int downloadmanager(const struct cfgstruct *copt, const struct optstruct *opt, c
1226 1226
 	free(dnsreply);
1227 1227
 
1228 1228
     mirman_write("mirrors.dat", &mdat);
1229
-    cli_unlockdb(dbdir);
1230 1229
 
1231 1230
     if(updated) {
1232 1231
 	if(cfgopt(copt, "HTTPProxyServer")->enabled) {
... ...
@@ -175,8 +175,6 @@ libclamav_la_SOURCES = \
175 175
 	hashtab.h \
176 176
 	dconf.c \
177 177
 	dconf.h \
178
-	lockdb.c \
179
-	lockdb.h \
180 178
 	lzma_iface.c \
181 179
 	lzma_iface.h
182 180
 
... ...
@@ -87,7 +87,7 @@ am_libclamav_la_OBJECTS = matcher-ac.lo matcher-bm.lo matcher.lo \
87 87
 	pdf.lo spin.lo yc.lo elf.lo sis.lo uuencode.lo pst.lo \
88 88
 	phishcheck.lo phish_domaincheck_db.lo phish_whitelist.lo \
89 89
 	regex_list.lo mspack.lo cab.lo entconv.lo hashtab.lo dconf.lo \
90
-	lockdb.lo lzma_iface.lo
90
+	lzma_iface.lo
91 91
 libclamav_la_OBJECTS = $(am_libclamav_la_OBJECTS)
92 92
 libclamav_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \
93 93
 	$(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
... ...
@@ -396,8 +396,6 @@ libclamav_la_SOURCES = \
396 396
 	hashtab.h \
397 397
 	dconf.c \
398 398
 	dconf.h \
399
-	lockdb.c \
400
-	lockdb.h \
401 399
 	lzma_iface.c \
402 400
 	lzma_iface.h
403 401
 
... ...
@@ -491,7 +489,6 @@ distclean-compile:
491 491
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/inflate64.Plo@am__quote@
492 492
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/is_tar.Plo@am__quote@
493 493
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/line.Plo@am__quote@
494
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lockdb.Plo@am__quote@
495 494
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lzma_iface.Plo@am__quote@
496 495
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/matcher-ac.Plo@am__quote@
497 496
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/matcher-bm.Plo@am__quote@
... ...
@@ -184,7 +184,7 @@ static int cli_tgzload(int fd, struct cl_engine **engine, unsigned int *signo, u
184 184
 	z_off_t off;
185 185
 
186 186
 
187
-    cli_dbgmsg("in cli_untgz()\n");
187
+    cli_dbgmsg("in cli_tgzload()\n");
188 188
 
189 189
     if((fdd = dup(fd)) == -1) {
190 190
 	cli_errmsg("cli_tgzload: Can't duplicate descriptor %d\n", fd);
... ...
@@ -391,7 +391,7 @@ void cl_cvdfree(struct cl_cvd *cvd)
391 391
     free(cvd);
392 392
 }
393 393
 
394
-static int cli_cvdverify(FILE *fs, struct cl_cvd *cvdpt)
394
+static int cli_cvdverify(FILE *fs, struct cl_cvd *cvdpt, unsigned int cld)
395 395
 {
396 396
 	struct cl_cvd *cvd;
397 397
 	char *md5, head[513];
... ...
@@ -413,6 +413,11 @@ static int cli_cvdverify(FILE *fs, struct cl_cvd *cvdpt)
413 413
     if(cvdpt)
414 414
 	memcpy(cvdpt, cvd, sizeof(struct cl_cvd));
415 415
 
416
+    if(cld) {
417
+	cl_cvdfree(cvd);
418
+	return CL_SUCCESS;
419
+    }
420
+
416 421
     md5 = cli_md5stream(fs, NULL);
417 422
     cli_dbgmsg("MD5(.tar.gz) = %s\n", md5);
418 423
 
... ...
@@ -434,7 +439,7 @@ static int cli_cvdverify(FILE *fs, struct cl_cvd *cvdpt)
434 434
 
435 435
     free(md5);
436 436
     cl_cvdfree(cvd);
437
-    return 0;
437
+    return CL_SUCCESS;
438 438
 }
439 439
 
440 440
 int cl_cvdverify(const char *file)
... ...
@@ -448,13 +453,13 @@ int cl_cvdverify(const char *file)
448 448
 	return CL_EOPEN;
449 449
     }
450 450
 
451
-    ret = cli_cvdverify(fs, NULL);
451
+    ret = cli_cvdverify(fs, NULL, 0);
452 452
     fclose(fs);
453 453
 
454 454
     return ret;
455 455
 }
456 456
 
457
-int cli_cvdload(FILE *fs, struct cl_engine **engine, unsigned int *signo, short warn, unsigned int options)
457
+int cli_cvdload(FILE *fs, struct cl_engine **engine, unsigned int *signo, short warn, unsigned int options, unsigned int cld)
458 458
 {
459 459
         char *dir;
460 460
 	struct cl_cvd cvd;
... ...
@@ -466,7 +471,7 @@ int cli_cvdload(FILE *fs, struct cl_engine **engine, unsigned int *signo, short
466 466
 
467 467
     /* verify */
468 468
 
469
-    if((ret = cli_cvdverify(fs, &cvd)))
469
+    if((ret = cli_cvdverify(fs, &cvd, cld)))
470 470
 	return ret;
471 471
 
472 472
     if(cvd.stime && warn) {
... ...
@@ -22,7 +22,7 @@
22 22
 #include <stdio.h>
23 23
 #include "clamav.h"
24 24
 
25
-int cli_cvdload(FILE *fs, struct cl_engine **engine, unsigned int *signo, short warn, unsigned int options);
25
+int cli_cvdload(FILE *fs, struct cl_engine **engine, unsigned int *signo, short warn, unsigned int options, unsigned int cld);
26 26
 int cli_untgz(int fd, const char *destdir);
27 27
 
28 28
 #endif
29 29
deleted file mode 100644
... ...
@@ -1,335 +0,0 @@
1
-/*
2
- *  Copyright (C) 2006 Mark Pizzolato <clamav-devel@subscriptions.pizzolato.net>
3
- *
4
- *  This program is free software; you can redistribute it and/or modify
5
- *  it under the terms of the GNU General Public License as published by
6
- *  the Free Software Foundation; either version 2 of the License, or
7
- *  (at your option) any later version.
8
- *
9
- *  This program is distributed in the hope that it will be useful,
10
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
- *  GNU General Public License for more details.
13
- *
14
- *  You should have received a copy of the GNU General Public License
15
- *  along with this program; if not, write to the Free Software
16
- *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17
- *  MA 02110-1301, USA.
18
- */
19
-
20
-/*
21
- * This is a problem, which from a purist point of view, best wants an 
22
- * RW locking mechanism.
23
- * On Posix platforms, we leverage advisory locks provided by fcntl().
24
- * Windows doesn't have a native interprocess RW exclusion mechanism, 
25
- * one could be constructed from the services available, but it is somewhat
26
- * complicated.  Meanwhile, we observe that in ClamAV, it is extremely rare 
27
- * that there will ever be an occasion when multiple processes will be 
28
- * reading the ClamAV database from a given directory at the same, and in 
29
- * none of those possible cases would it matter if they serialized their 
30
- * accesses.  So, a simple mutual exclusion mechanism will suffice for both 
31
- * the reader and writer locks on Windows.
32
- */
33
-#ifdef	_MSC_VER
34
-#include <windows.h>
35
-#endif
36
-
37
-#if HAVE_CONFIG_H
38
-#include "clamav-config.h"
39
-#endif
40
-
41
-#include <stdio.h>
42
-#include <stdarg.h>
43
-#include <stdlib.h>
44
-#include <string.h>
45
-#include <ctype.h>
46
-#ifdef HAVE_UNISTD_H
47
-#include <unistd.h>
48
-#endif
49
-#ifdef	HAVE_SYS_STAT_H
50
-#include <sys/stat.h>
51
-#endif
52
-#include <fcntl.h>
53
-#include <errno.h>
54
-
55
-#include "clamav.h"
56
-#include "others.h"
57
-#include "lockdb.h"
58
-
59
-#ifdef CL_THREAD_SAFE
60
-#include <pthread.h>
61
-pthread_mutex_t lock_mutex = PTHREAD_MUTEX_INITIALIZER;
62
-#else
63
-#define pthread_mutex_lock(arg)
64
-#define pthread_mutex_unlock(arg)
65
-#endif
66
-
67
-#ifdef C_WINDOWS /* FIXME */
68
-#define DONT_LOCK_DBDIRS
69
-#endif
70
-
71
-struct dblock {
72
-	struct dblock *lock_link;
73
-	char lock_file[NAME_MAX];
74
-#ifndef C_WINDOWS
75
-	int lock_fd;
76
-#else
77
-	HANDLE lock_fd;
78
-#endif
79
-	int lock_type;
80
-};
81
-
82
-static struct dblock *dblocks = NULL;
83
-
84
-static void cli_lockname(char *lock_file, size_t lock_file_size, const char *dbdirpath);
85
-static int cli_lockdb(const char *dbdirpath, int wait, int writelock);
86
-
87
-#ifdef DONT_LOCK_DBDIRS
88
-
89
-int cli_readlockdb(const char *dbdirpath, int wait)
90
-{
91
-    return CL_SUCCESS;
92
-}
93
-
94
-int cli_writelockdb(const char *dbdirpath, int wait)
95
-{
96
-    return CL_SUCCESS;
97
-}
98
-
99
-int cli_unlockdb(const char *dbdirpath)
100
-{
101
-    return CL_SUCCESS;
102
-}
103
-
104
-int cli_freelocks(void)
105
-{
106
-	return CL_SUCCESS;
107
-}
108
-
109
-#else /* !DONT_LOCK_DBDIRS */
110
-
111
-int cli_readlockdb(const char *dbdirpath, int wait)
112
-{
113
-    return cli_lockdb(dbdirpath, wait, 0);
114
-}
115
-
116
-int cli_writelockdb(const char *dbdirpath, int wait)
117
-{
118
-    return cli_lockdb(dbdirpath, wait, 1);
119
-}
120
-
121
-int cli_freelocks(void)
122
-{
123
-	struct dblock * lock, *nextlock, *usedlocks = NULL;
124
-
125
-	pthread_mutex_lock(&lock_mutex);
126
-	for(lock = dblocks; lock; lock = nextlock) {
127
-		/* there might be some locks in use, eg: during a db reload, a failure can lead 
128
-		 * to cl_free being called */
129
-		nextlock = lock->lock_link;
130
-		if(lock->lock_type != -1 && lock->lock_fd != -1) {
131
-			lock->lock_link = usedlocks;
132
-			usedlocks = lock;
133
-		}
134
-		else {
135
-			free(lock);
136
-		}
137
-	}
138
-	dblocks = usedlocks;
139
-	pthread_mutex_unlock(&lock_mutex);
140
-	return CL_SUCCESS;
141
-}
142
-
143
-
144
-int cli_unlockdb(const char *dbdirpath)
145
-{
146
-	char lock_file[NAME_MAX];
147
-	struct dblock *lock;
148
-#ifndef C_WINDOWS
149
-	struct flock fl;
150
-#endif
151
-
152
-    cli_lockname(lock_file, sizeof(lock_file), dbdirpath);
153
-    pthread_mutex_lock(&lock_mutex);
154
-    for(lock=dblocks; lock; lock=lock->lock_link)
155
-	if(!strcmp(lock_file, lock->lock_file))
156
-	    break;
157
-    if((!lock) || (lock->lock_type == -1)) {
158
-	cli_errmsg("Database Directory: %s not locked\n", dbdirpath);
159
-	pthread_mutex_unlock(&lock_mutex);
160
-	return CL_ELOCKDB;
161
-    }
162
-#ifndef C_WINDOWS
163
-    memset(&fl, 0, sizeof(fl));
164
-    fl.l_type = F_UNLCK;
165
-    if(fcntl(lock->lock_fd, F_SETLK, &fl) == -1) {
166
-#else
167
-    if(!ReleaseMutex(lock->lock_fd)) {
168
-#endif
169
-	cli_errmsg("Error Unlocking Database Directory %s\n", dbdirpath);
170
-	pthread_mutex_unlock(&lock_mutex);
171
-#ifndef C_WINDOWS
172
-	close(lock->lock_fd);
173
-	lock->lock_fd=-1;
174
-	unlink(lock->lock_file);
175
-#endif
176
-	return CL_ELOCKDB;
177
-    }
178
-    lock->lock_type = -1;
179
-#ifndef C_WINDOWS
180
-    close(lock->lock_fd);
181
-    lock->lock_fd=-1;
182
-    unlink(lock->lock_file);
183
-#endif
184
-    pthread_mutex_unlock(&lock_mutex);
185
-
186
-    return CL_SUCCESS;
187
-}
188
-
189
-static int cli_lockdb(const char *dbdirpath, int wait, int writelock)
190
-{
191
-	char lock_file[NAME_MAX];
192
-	struct dblock *lock;
193
-#ifndef C_WINDOWS
194
-	struct flock fl;
195
-	mode_t old_mask;
196
-	unsigned int existing = 0;
197
-#else
198
-	DWORD LastError;
199
-	SECURITY_ATTRIBUTES saAttr;
200
-	SECURITY_DESCRIPTOR sdDesc;
201
-#endif
202
-
203
-    cli_lockname(lock_file, sizeof(lock_file), dbdirpath);
204
-    pthread_mutex_lock(&lock_mutex);
205
-    for(lock=dblocks; lock; lock=lock->lock_link)
206
-	if(!strcmp(lock_file, lock->lock_file))
207
-	    break;
208
-    if(!lock) {
209
-	lock = cli_calloc(1, sizeof(*lock));
210
-	if(!lock) {
211
-	    cli_errmsg("cli_lockdb(): Can't allocate lock structure to lock Database Directory: %s\n", dbdirpath);
212
-	    pthread_mutex_unlock(&lock_mutex);
213
-	    return CL_EMEM;
214
-	}
215
-	lock->lock_link = dblocks;
216
-	strcpy(lock->lock_file, lock_file);
217
-	lock->lock_fd = -1;
218
-	lock->lock_type = -1;
219
-	dblocks = lock;
220
-    }
221
-    if(lock->lock_type != -1) {
222
-	cli_dbgmsg("Database Directory: %s already %s locked\n", dbdirpath, (lock->lock_type? "write" : "read"));
223
-	pthread_mutex_unlock(&lock_mutex);
224
-	return CL_ELOCKDB;
225
-    }
226
-#ifndef C_WINDOWS
227
-    if(lock->lock_fd == -1) {
228
-	old_mask = umask(0);
229
-	if(-1 == (lock->lock_fd = open(lock->lock_file, O_RDWR|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG|S_IROTH))) {
230
-	    if((writelock) ||
231
-	       (-1 == (lock->lock_fd = open(lock->lock_file, O_RDONLY)))) {
232
-		cli_dbgmsg("Can't %s Lock file for Database Directory: %s\n", (writelock ? "create" : "open"), dbdirpath);
233
-		umask(old_mask);
234
-		pthread_mutex_unlock(&lock_mutex);
235
-		return CL_EIO; /* or CL_EACCESS */
236
-	    } else {
237
-		existing = 1;
238
-	    }
239
-	}
240
-	umask(old_mask);
241
-    }
242
-#else
243
-    if(lock->lock_fd == -1) {
244
-	/* Create a security descriptor which allows any process to acquire the Mutex */
245
-	InitializeSecurityDescriptor(&sdDesc, SECURITY_DESCRIPTOR_REVISION);
246
-	SetSecurityDescriptorDacl(&sdDesc, TRUE, NULL, FALSE);
247
-	saAttr.nLength = sizeof(saAttr);
248
-	saAttr.bInheritHandle = FALSE;
249
-	saAttr.lpSecurityDescriptor = &sdDesc;
250
-	if(!(lock->lock_fd = CreateMutexA(&saAttr, TRUE, lock->lock_file))) {
251
-	    if((GetLastError() != ERROR_ACCESS_DENIED) || 
252
-	       (!(lock->lock_fd = OpenMutexA(MUTEX_MODIFY_STATE, FALSE, lock->lock_file)))) {
253
-		cli_dbgmsg("Can't Create Mutex Lock for Database Directory: %s\n", dbdirpath);
254
-		pthread_mutex_unlock(&lock_mutex);
255
-		return CL_EIO;
256
-	    }
257
-	    LastError = ERROR_ALREADY_EXISTS;
258
-	}
259
-	LastError = GetLastError();
260
-    } else {
261
-	LastError = ERROR_ALREADY_EXISTS;
262
-    }
263
-#endif
264
-    pthread_mutex_unlock(&lock_mutex);
265
-
266
-#ifndef C_WINDOWS
267
-    memset(&fl, 0, sizeof(fl));
268
-    fl.l_type = (writelock ? F_WRLCK : F_RDLCK);
269
-    if(fcntl(lock->lock_fd, ((wait) ? F_SETLKW : F_SETLK), &fl) == -1) {
270
-#ifndef C_WINDOWS
271
-	close(lock->lock_fd);
272
-	lock->lock_fd = -1;
273
-	if(errno != EACCES && errno != EAGAIN) {
274
-	    if(!existing)
275
-		unlink(lock->lock_file);
276
-	    cli_errmsg("Can't acquire %s lock: %s\n", writelock ? "write" : "read", strerror(errno));
277
-	    return CL_EIO;
278
-	}
279
-#endif
280
-	return CL_ELOCKDB;
281
-    }
282
-#else
283
-    if(LastError == ERROR_ALREADY_EXISTS) {
284
-	if(WAIT_TIMEOUT == WaitForSingleObject(lock->lock_fd, ((wait) ? INFINITE : 0))) {
285
-	    lock->lock_type = -1;
286
-	    return CL_ELOCKDB;
287
-	}
288
-    }
289
-#endif
290
-    lock->lock_type = writelock;
291
-
292
-    return CL_SUCCESS;
293
-}
294
-
295
-static void cli_lockname(char *lock_file, size_t lock_file_size, const char *dbdirpath)
296
-{
297
-	char *c;
298
-
299
-    lock_file[lock_file_size-1] = '\0';
300
-#ifndef C_WINDOWS
301
-    snprintf(lock_file, lock_file_size-1, "%s/.dbLock", dbdirpath);
302
-    for (c=lock_file; *c; ++c) {
303
-#else
304
-    snprintf(lock_file, lock_file_size-1, "Global\\ClamAVDB-%s", dbdirpath);
305
-    for (c=lock_file+16; *c; ++c) {
306
-#endif
307
-	switch (*c) {
308
-#ifdef C_WINDOWS
309
-	case '\\':
310
-	    *c = '/';
311
-#endif
312
-	case '/':
313
-	    if(c!=lock_file && *(c-1) == '/') { /* compress imbedded // */
314
-		--c;
315
-		memmove(c, c+1,strlen(c+1)+1);
316
-            } else if(c > lock_file+1 && (*(c-2) == '/') && (*(c-1) == '.')) { /* compress imbedded /./ */
317
-		c -= 2;
318
-		memmove(c, c+2,strlen(c+2)+1);
319
-            }
320
-	    break;
321
-#ifdef C_WINDOWS
322
-	default:
323
-	    if(islower(*c)) /* Normalize to upper case */
324
-		*c = toupper(*c);
325
-	    break;
326
-#endif
327
-	}
328
-    }
329
-#ifdef C_WINDOWS
330
-    if('/' == lock_file[strlen(lock_file)-1]) /* Remove trailing / */
331
-	lock_file[strlen(lock_file)-1] = '\0';
332
-#endif
333
-}
334
-
335
-#endif /* DONT_LOCK_DBDIRS */
336 1
deleted file mode 100644
... ...
@@ -1,28 +0,0 @@
1
-/*
2
- *  Copyright (C) 2006 Mark Pizzolato <clamav-devel@subscriptions.pizzolato.net>
3
- *
4
- *  This program is free software; you can redistribute it and/or modify
5
- *  it under the terms of the GNU General Public License as published by
6
- *  the Free Software Foundation; either version 2 of the License, or
7
- *  (at your option) any later version.
8
- *
9
- *  This program is distributed in the hope that it will be useful,
10
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
- *  GNU General Public License for more details.
13
- *
14
- *  You should have received a copy of the GNU General Public License
15
- *  along with this program; if not, write to the Free Software
16
- *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17
- *  MA 02110-1301, USA.
18
- */
19
-
20
-#ifndef __LOCKDB_H
21
-#define __LOCKDB_H
22
-
23
-int cli_writelockdb(const char *dbdirpath, int wait);
24
-int cli_readlockdb(const char *dbdirpath, int wait);
25
-int cli_unlockdb(const char *dbdirpath);
26
-int cli_freelocks(void);
27
-
28
-#endif
... ...
@@ -53,7 +53,6 @@
53 53
 #include "others.h"
54 54
 #include "str.h"
55 55
 #include "dconf.h"
56
-#include "lockdb.h"
57 56
 #include "filetypes.h"
58 57
 #include "filetypes_int.h"
59 58
 #include "readdb.h"
... ...
@@ -1069,7 +1068,15 @@ int cli_load(const char *filename, struct cl_engine **engine, unsigned int *sign
1069 1069
 	if(strstr(filename, "daily.cvd"))
1070 1070
 	    warn = 1;
1071 1071
 
1072
-	ret = cli_cvdload(fs, engine, signo, warn, options);
1072
+	ret = cli_cvdload(fs, engine, signo, warn, options, 0);
1073
+
1074
+    } else if(cli_strbcasestr(filename, ".cld")) {
1075
+	    int warn = 0;
1076
+
1077
+	if(strstr(filename, "daily.cld"))
1078
+	    warn = 1;
1079
+
1080
+	ret = cli_cvdload(fs, engine, signo, warn, options | CL_DB_CVDNOTMP, 1);
1073 1081
 
1074 1082
     } else if(cli_strbcasestr(filename, ".hdb")) {
1075 1083
 	ret = cli_loadmd5(fs, engine, signo, MD5_HDB, options, gzs, gzrsize);
... ...
@@ -1150,7 +1157,7 @@ int cl_loaddb(const char *filename, struct cl_engine **engine, unsigned int *sig
1150 1150
     return cli_load(filename, engine, signo, CL_DB_STDOPT, NULL, 0);
1151 1151
 }
1152 1152
 
1153
-static int cli_loaddbdir_l(const char *dirname, struct cl_engine **engine, unsigned int *signo, unsigned int options)
1153
+static int cli_loaddbdir(const char *dirname, struct cl_engine **engine, unsigned int *signo, unsigned int options)
1154 1154
 {
1155 1155
 	DIR *dd;
1156 1156
 	struct dirent *dent;
... ...
@@ -1206,11 +1213,7 @@ static int cli_loaddbdir_l(const char *dirname, struct cl_engine **engine, unsig
1206 1206
 		    return CL_EMEM;
1207 1207
 		}
1208 1208
 		sprintf(dbfile, "%s/%s", dirname, dent->d_name);
1209
-
1210
-		if(cli_strbcasestr(dbfile, ".inc"))
1211
-		    ret = cli_loaddbdir(dbfile, engine, signo, options);
1212
-		else
1213
-		    ret = cli_load(dbfile, engine, signo, options, NULL, 0);
1209
+		ret = cli_load(dbfile, engine, signo, options, NULL, 0);
1214 1210
 
1215 1211
 		if(ret) {
1216 1212
 		    cli_dbgmsg("cli_loaddbdir(): error loading database %s\n", dbfile);
... ...
@@ -1230,31 +1233,6 @@ static int cli_loaddbdir_l(const char *dirname, struct cl_engine **engine, unsig
1230 1230
     return ret;
1231 1231
 }
1232 1232
 
1233
-static int cli_loaddbdir(const char *dirname, struct cl_engine **engine, unsigned int *signo, unsigned int options)
1234
-{
1235
-	int ret, try = 0, lock;
1236
-
1237
-
1238
-    cli_dbgmsg("cli_loaddbdir: Acquiring dbdir lock\n");
1239
-    while((lock = cli_readlockdb(dirname, 0)) == CL_ELOCKDB) {
1240
-#ifdef C_WINDOWS
1241
-	Sleep(5);
1242
-#else
1243
-	sleep(5);
1244
-#endif
1245
-	if(try++ > 24) {
1246
-	    cli_errmsg("cl_load(): Unable to lock database directory: %s\n", dirname);
1247
-	    return CL_ELOCKDB;
1248
-	}
1249
-    }
1250
-
1251
-    ret = cli_loaddbdir_l(dirname, engine, signo, options);
1252
-    if(lock == CL_SUCCESS)
1253
-	cli_unlockdb(dirname);
1254
-
1255
-    return ret;
1256
-}
1257
-
1258 1233
 int cl_loaddbdir(const char *dirname, struct cl_engine **engine, unsigned int *signo) {
1259 1234
     return cli_loaddbdir(dirname, engine, signo, CL_DB_STDOPT);
1260 1235
 }
... ...
@@ -1365,12 +1343,7 @@ int cl_statinidir(const char *dirname, struct cl_stat *dbstat)
1365 1365
 		    closedir(dd);
1366 1366
 		    return CL_EMEM;
1367 1367
 		}
1368
-
1369
-		if(cli_strbcasestr(dent->d_name, ".inc")) {
1370
-		    sprintf(fname, "%s/%s/%s.info", dirname, dent->d_name, strstr(dent->d_name, "daily") ? "daily" : "main");
1371
-		} else {
1372
-		    sprintf(fname, "%s/%s", dirname, dent->d_name);
1373
-		}
1368
+		sprintf(fname, "%s/%s", dirname, dent->d_name);
1374 1369
 #if defined(C_INTERIX) || defined(C_OS2)
1375 1370
 		dbstat->statdname[dbstat->entries - 1] = (char *) cli_malloc(strlen(dent->d_name) + 1);
1376 1371
 		if(!dbstat->statdname[dbstat->entries - 1]) {
... ...
@@ -1436,11 +1409,7 @@ int cl_statchkdir(const struct cl_stat *dbstat)
1436 1436
 		    return CL_EMEM;
1437 1437
 		}
1438 1438
 
1439
-		if(cli_strbcasestr(dent->d_name, ".inc")) {
1440
-		    sprintf(fname, "%s/%s/%s.info", dbstat->dir, dent->d_name, strstr(dent->d_name, "daily") ? "daily" : "main");
1441
-		} else {
1442
-		    sprintf(fname, "%s/%s", dbstat->dir, dent->d_name);
1443
-		}
1439
+		sprintf(fname, "%s/%s", dbstat->dir, dent->d_name);
1444 1440
 		stat(fname, &sb);
1445 1441
 		free(fname);
1446 1442
 
... ...
@@ -1590,7 +1559,6 @@ void cl_free(struct cl_engine *engine)
1590 1590
 	free(engine->dconf);
1591 1591
 
1592 1592
     cli_ftfree(engine->ftypes);
1593
-    cli_freelocks();
1594 1593
     free(engine);
1595 1594
 }
1596 1595
 
... ...
@@ -43,8 +43,8 @@
43 43
 	cli_strbcasestr(ext, ".pdb")   ||	\
44 44
 	cli_strbcasestr(ext, ".wdb")   ||	\
45 45
 	cli_strbcasestr(ext, ".ft")    ||	\
46
-	cli_strbcasestr(ext, ".inc")   ||	\
47
-	cli_strbcasestr(ext, ".cvd")		\
46
+	cli_strbcasestr(ext, ".cvd")   ||	\
47
+	cli_strbcasestr(ext, ".cld")		\
48 48
     )
49 49
 
50 50
 
... ...
@@ -30,6 +30,8 @@
30 30
 #include <unistd.h>
31 31
 #endif
32 32
 
33
+#include "tar.h"
34
+
33 35
 #ifndef O_BINARY
34 36
 #define O_BINARY 0
35 37
 #endif