git-svn: trunk@3654
Tomasz Kojm authored on 2008/02/19 09:30:18... | ... |
@@ -1,3 +1,12 @@ |
1 |
+Tue Feb 19 00:24:01 CET 2008 (tk) |
|
2 |
+--------------------------------- |
|
3 |
+ * sigtool/sigtool.c: - general code cleanup |
|
4 |
+ - use internal tar archiver |
|
5 |
+ - add support for .cld files |
|
6 |
+ - count PUA and don't count FP sigs (in CVD header) |
|
7 |
+ - improve error handling |
|
8 |
+ - fix main.cvd building issues and interactive mode |
|
9 |
+ |
|
1 | 10 |
Mon Feb 18 20:48:41 CET 2008 (acab) |
2 | 11 |
----------------------------------- |
3 | 12 |
* configure: make fpu endianess manually configurable |
... | ... |
@@ -68,7 +68,7 @@ binPROGRAMS_INSTALL = $(INSTALL_PROGRAM) |
68 | 68 |
PROGRAMS = $(bin_PROGRAMS) |
69 | 69 |
am_sigtool_OBJECTS = output.$(OBJEXT) getopt.$(OBJEXT) \ |
70 | 70 |
cfgparser.$(OBJEXT) misc.$(OBJEXT) options.$(OBJEXT) \ |
71 |
- sha256.$(OBJEXT) cdiff.$(OBJEXT) vba.$(OBJEXT) \ |
|
71 |
+ sha256.$(OBJEXT) cdiff.$(OBJEXT) tar.$(OBJEXT) vba.$(OBJEXT) \ |
|
72 | 72 |
sigtool.$(OBJEXT) |
73 | 73 |
sigtool_OBJECTS = $(am_sigtool_OBJECTS) |
74 | 74 |
sigtool_LDADD = $(LDADD) |
... | ... |
@@ -231,6 +231,8 @@ sigtool_SOURCES = \ |
231 | 231 |
$(top_srcdir)/shared/sha256.c \ |
232 | 232 |
$(top_srcdir)/shared/cdiff.c \ |
233 | 233 |
$(top_srcdir)/shared/cdiff.h \ |
234 |
+ $(top_srcdir)/shared/tar.c \ |
|
235 |
+ $(top_srcdir)/shared/tar.h \ |
|
234 | 236 |
vba.c \ |
235 | 237 |
vba.h \ |
236 | 238 |
sigtool.c |
... | ... |
@@ -315,6 +317,7 @@ distclean-compile: |
315 | 315 |
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/output.Po@am__quote@ |
316 | 316 |
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sha256.Po@am__quote@ |
317 | 317 |
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sigtool.Po@am__quote@ |
318 |
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tar.Po@am__quote@ |
|
318 | 319 |
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vba.Po@am__quote@ |
319 | 320 |
|
320 | 321 |
.c.o: |
... | ... |
@@ -436,6 +439,20 @@ cdiff.obj: $(top_srcdir)/shared/cdiff.c |
436 | 436 |
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ |
437 | 437 |
@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` |
438 | 438 |
|
439 |
+tar.o: $(top_srcdir)/shared/tar.c |
|
440 |
+@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 |
|
441 |
+@am__fastdepCC_TRUE@ mv -f $(DEPDIR)/tar.Tpo $(DEPDIR)/tar.Po |
|
442 |
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$(top_srcdir)/shared/tar.c' object='tar.o' libtool=no @AMDEPBACKSLASH@ |
|
443 |
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ |
|
444 |
+@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 |
|
445 |
+ |
|
446 |
+tar.obj: $(top_srcdir)/shared/tar.c |
|
447 |
+@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` |
|
448 |
+@am__fastdepCC_TRUE@ mv -f $(DEPDIR)/tar.Tpo $(DEPDIR)/tar.Po |
|
449 |
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$(top_srcdir)/shared/tar.c' object='tar.obj' libtool=no @AMDEPBACKSLASH@ |
|
450 |
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ |
|
451 |
+@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` |
|
452 |
+ |
|
439 | 453 |
mostlyclean-libtool: |
440 | 454 |
-rm -f *.lo |
441 | 455 |
|
... | ... |
@@ -1,7 +1,8 @@ |
1 | 1 |
/* |
2 |
+ * Copyright (C) 2007 - 2008 Sourcefire, Inc. |
|
2 | 3 |
* Copyright (C) 2002 - 2007 Tomasz Kojm <tkojm@clamav.net> |
3 | 4 |
* CDIFF code (C) 2006 Sensory Networks, Inc. |
4 |
- * Written by Tomasz Kojm |
|
5 |
+ * Author: Tomasz Kojm <tkojm@clamav.net> |
|
5 | 6 |
* |
6 | 7 |
* This program is free software; you can redistribute it and/or modify |
7 | 8 |
* it under the terms of the GNU General Public License version 2 as |
... | ... |
@@ -52,6 +53,7 @@ |
52 | 52 |
#include "shared/misc.h" |
53 | 53 |
#include "shared/cdiff.h" |
54 | 54 |
#include "shared/sha256.h" |
55 |
+#include "shared/tar.h" |
|
55 | 56 |
|
56 | 57 |
#include "libclamav/clamav.h" |
57 | 58 |
#include "libclamav/cvd.h" |
... | ... |
@@ -62,6 +64,36 @@ |
62 | 62 |
|
63 | 63 |
#define MAX_DEL_LOOKAHEAD 200 |
64 | 64 |
|
65 |
+static const struct dblist_s { |
|
66 |
+ const char *name; |
|
67 |
+ unsigned int count; |
|
68 |
+} dblist[] = { |
|
69 |
+ |
|
70 |
+ /* special files */ |
|
71 |
+ { "COPYING", 0 }, |
|
72 |
+ { "daily.cfg", 0 }, |
|
73 |
+ { "daily.ign", 0 }, |
|
74 |
+ { "daily.ft", 0 }, |
|
75 |
+ { "main.info", 0 }, { "daily.info", 0 }, |
|
76 |
+ |
|
77 |
+ /* databases */ |
|
78 |
+ { "main.db", 1 }, { "daily.db", 1 }, |
|
79 |
+ { "main.hdb", 1 }, { "daily.hdb", 1 }, |
|
80 |
+ { "main.hdu", 1 }, { "daily.hdu", 1 }, |
|
81 |
+ { "main.mdb", 1 }, { "daily.mdb", 1 }, |
|
82 |
+ { "main.mdu", 1 }, { "daily.mdu", 1 }, |
|
83 |
+ { "main.ndb", 1 }, { "daily.ndb", 1 }, |
|
84 |
+ { "main.ndu", 1 }, { "daily.ndu", 1 }, |
|
85 |
+ { "main.sdb", 1 }, { "daily.sdb", 1 }, |
|
86 |
+ { "main.zmd", 1 }, { "daily.zmd", 1 }, |
|
87 |
+ { "main.rmd", 1 }, { "daily.rmd", 1 }, |
|
88 |
+ { "main.fp", 0 }, { "daily.fp", 0 }, |
|
89 |
+ { "main.pdb", 0 }, { "daily.pdb", 0 }, |
|
90 |
+ { "main.wdb", 0 }, { "daily.wdb", 0 }, |
|
91 |
+ |
|
92 |
+ { NULL, 0 } |
|
93 |
+}; |
|
94 |
+ |
|
65 | 95 |
static int hexdump(void) |
66 | 96 |
{ |
67 | 97 |
char buffer[FILEBUFF], *pt; |
... | ... |
@@ -87,7 +119,7 @@ static int hexdump(void) |
87 | 87 |
static int md5sig(struct optstruct *opt, unsigned int mdb) |
88 | 88 |
{ |
89 | 89 |
char *md5, *filename; |
90 |
- int i; |
|
90 |
+ unsigned int i; |
|
91 | 91 |
struct stat sb; |
92 | 92 |
|
93 | 93 |
|
... | ... |
@@ -113,7 +145,6 @@ static int md5sig(struct optstruct *opt, unsigned int mdb) |
113 | 113 |
} |
114 | 114 |
} |
115 | 115 |
} |
116 |
- |
|
117 | 116 |
free(filename); |
118 | 117 |
} |
119 | 118 |
|
... | ... |
@@ -160,6 +191,10 @@ static int utf16decode(struct optstruct *opt) |
160 | 160 |
} |
161 | 161 |
|
162 | 162 |
newname = malloc(strlen(fname) + 7); |
163 |
+ if(!newname) { |
|
164 |
+ mprintf("!utf16decode: Can't allocate memory\n"); |
|
165 |
+ return -1; |
|
166 |
+ } |
|
163 | 167 |
sprintf(newname, "%s.ascii", fname); |
164 | 168 |
|
165 | 169 |
if((fd2 = open(newname, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU)) < 0) { |
... | ... |
@@ -194,24 +229,24 @@ static int utf16decode(struct optstruct *opt) |
194 | 194 |
|
195 | 195 |
static unsigned int countlines(const char *filename) |
196 | 196 |
{ |
197 |
- FILE *fd; |
|
197 |
+ FILE *fh; |
|
198 | 198 |
char buff[1024]; |
199 | 199 |
unsigned int lines = 0; |
200 | 200 |
|
201 | 201 |
|
202 |
- if((fd = fopen(filename, "r")) == NULL) |
|
202 |
+ if((fh = fopen(filename, "r")) == NULL) |
|
203 | 203 |
return 0; |
204 | 204 |
|
205 |
- while(fgets(buff, sizeof(buff), fd)) { |
|
205 |
+ while(fgets(buff, sizeof(buff), fh)) { |
|
206 | 206 |
if(buff[0] == '#') continue; |
207 | 207 |
lines++; |
208 | 208 |
} |
209 | 209 |
|
210 |
- fclose(fd); |
|
210 |
+ fclose(fh); |
|
211 | 211 |
return lines; |
212 | 212 |
} |
213 | 213 |
|
214 |
-static char *getdsig(const char *host, const char *user, const char *data, unsigned int datalen, unsigned short mode) |
|
214 |
+static char *getdsig(const char *host, const char *user, const unsigned char *data, unsigned int datalen, unsigned short mode) |
|
215 | 215 |
{ |
216 | 216 |
char buff[512], cmd[128], pass[30], *pt; |
217 | 217 |
struct sockaddr_in server; |
... | ... |
@@ -224,7 +259,6 @@ static char *getdsig(const char *host, const char *user, const char *data, unsig |
224 | 224 |
if((pt = getenv("SIGNDPASS"))) { |
225 | 225 |
strncpy(pass, pt, sizeof(pass)); |
226 | 226 |
} else { |
227 |
- fflush(stdin); |
|
228 | 227 |
mprintf("Password: "); |
229 | 228 |
|
230 | 229 |
#ifdef HAVE_TERMIOS_H |
... | ... |
@@ -239,13 +273,15 @@ static char *getdsig(const char *host, const char *user, const char *data, unsig |
239 | 239 |
return NULL; |
240 | 240 |
} |
241 | 241 |
#endif |
242 |
- |
|
243 |
- if(fgets(pass, sizeof(pass), stdin)) { |
|
244 |
- cli_chomp(pass); |
|
245 |
- } else { |
|
242 |
+ if(scanf("%as", &pt) == EOF) { |
|
246 | 243 |
mprintf("!getdsig: Can't get password\n"); |
244 |
+#ifdef HAVE_TERMIOS_H |
|
245 |
+ tcsetattr(0, TCSAFLUSH, &old); |
|
246 |
+#endif |
|
247 | 247 |
return NULL; |
248 | 248 |
} |
249 |
+ strncpy(pass, pt, sizeof(pass)); |
|
250 |
+ free(pt); |
|
249 | 251 |
|
250 | 252 |
#ifdef HAVE_TERMIOS_H |
251 | 253 |
if(tcsetattr(0, TCSAFLUSH, &old)) { |
... | ... |
@@ -325,17 +361,15 @@ static char *getdsig(const char *host, const char *user, const char *data, unsig |
325 | 325 |
return strdup(pt); |
326 | 326 |
} |
327 | 327 |
|
328 |
-static int writeinfo(const char *db, const char *header) |
|
328 |
+static int writeinfo(const char *dbname, const char *header) |
|
329 | 329 |
{ |
330 | 330 |
FILE *fh; |
331 |
- int i; |
|
332 |
- struct stat sb; |
|
331 |
+ unsigned int i; |
|
333 | 332 |
char file[32], *md5; |
334 |
- const char *const extlist[] = { "db", "fp", "hdb", "hdu", "mdb", "mdu", "ndb", "ndu", "pdb", "wdb", "rmd", "zmd", "sdb", "cfg", NULL }; |
|
335 | 333 |
|
336 | 334 |
|
337 |
- snprintf(file, sizeof(file), "%s.info", db); |
|
338 |
- if(stat(file, &sb) != -1) { |
|
335 |
+ snprintf(file, sizeof(file), "%s.info", dbname); |
|
336 |
+ if(!access(file, R_OK)) { |
|
339 | 337 |
if(unlink(file) == -1) { |
340 | 338 |
mprintf("!writeinfo: Can't unlink %s\n", file); |
341 | 339 |
return -1; |
... | ... |
@@ -353,15 +387,14 @@ static int writeinfo(const char *db, const char *header) |
353 | 353 |
return -1; |
354 | 354 |
} |
355 | 355 |
|
356 |
- for(i = 0; extlist[i]; i++) { |
|
357 |
- snprintf(file, sizeof(file), "%s.%s", db, extlist[i]); |
|
358 |
- if(stat(file, &sb) != -1) { |
|
359 |
- if(!(md5 = cli_md5file(file))) { |
|
356 |
+ for(i = 0; dblist[i].name; i++) { |
|
357 |
+ if(!cli_strbcasestr(dblist[i].name, ".info") && strstr(dblist[i].name, dbname) && !access(dblist[i].name, R_OK)) { |
|
358 |
+ if(!(md5 = cli_md5file(dblist[i].name))) { |
|
360 | 359 |
mprintf("!writeinfo: Can't generate MD5 checksum for %s\n", file); |
361 | 360 |
fclose(fh); |
362 | 361 |
return -1; |
363 | 362 |
} |
364 |
- if(fprintf(fh, "%s.%s:%s\n", db, extlist[i], md5) < 0) { |
|
363 |
+ if(fprintf(fh, "%s:%s\n", dblist[i].name, md5) < 0) { |
|
365 | 364 |
mprintf("!writeinfo: Can't write to info file\n"); |
366 | 365 |
fclose(fh); |
367 | 366 |
free(md5); |
... | ... |
@@ -410,7 +443,12 @@ static int script2cdiff(const char *script, const char *builder, struct optstruc |
410 | 410 |
free(cdiff); |
411 | 411 |
return -1; |
412 | 412 |
} |
413 |
- sscanf(++pt, "%u.script", &ver); |
|
413 |
+ |
|
414 |
+ if(sscanf(++pt, "%u.script", &ver) == EOF) { |
|
415 |
+ mprintf("!script2cdiff: Incorrect file name syntax\n"); |
|
416 |
+ free(cdiff); |
|
417 |
+ return -1; |
|
418 |
+ } |
|
414 | 419 |
|
415 | 420 |
if(!(cdiffh = fopen(cdiff, "wb"))) { |
416 | 421 |
mprintf("!script2cdiff: Can't open %s for writing\n", cdiff); |
... | ... |
@@ -470,7 +508,7 @@ static int script2cdiff(const char *script, const char *builder, struct optstruc |
470 | 470 |
sha256_final(&ctx); |
471 | 471 |
sha256_digest(&ctx, digest); |
472 | 472 |
|
473 |
- if(!(pt = getdsig(opt_arg(opt, "server"), builder, (char *) digest, 32, 1))) { |
|
473 |
+ if(!(pt = getdsig(opt_arg(opt, "server"), builder, digest, 32, 1))) { |
|
474 | 474 |
mprintf("!script2cdiff: Can't get digital signature from remote server\n"); |
475 | 475 |
unlink(cdiff); |
476 | 476 |
free(cdiff); |
... | ... |
@@ -495,16 +533,16 @@ static int script2cdiff(const char *script, const char *builder, struct optstruc |
495 | 495 |
|
496 | 496 |
static int build(struct optstruct *opt) |
497 | 497 |
{ |
498 |
- int ret, inc = 1, dn; |
|
498 |
+ int ret; |
|
499 | 499 |
size_t bytes; |
500 |
- unsigned int sigs = 0, oldsigs = 0, lines = 0, version, real_header, fl; |
|
500 |
+ unsigned int i, sigs = 0, oldsigs = 0, lines = 0, version, real_header, fl; |
|
501 | 501 |
struct stat foo; |
502 |
- char buffer[FILEBUFF], *tarfile, *gzfile, header[513], smbuff[32], |
|
503 |
- builder[32], *pt, olddb[512], patch[32], broken[32]; |
|
504 |
- const char *dbname; |
|
502 |
+ unsigned char buffer[FILEBUFF]; |
|
503 |
+ char *tarfile, header[513], smbuff[32], builder[32], *pt, olddb[512], patch[32], broken[32]; |
|
504 |
+ const char *dbname, *newcvd; |
|
505 | 505 |
struct cl_engine *engine = NULL; |
506 |
- FILE *tar, *cvd; |
|
507 |
- gzFile *gz; |
|
506 |
+ FILE *cvd, *fh; |
|
507 |
+ gzFile *tar; |
|
508 | 508 |
time_t timet; |
509 | 509 |
struct tm *brokent; |
510 | 510 |
struct cl_cvd *oldcvd; |
... | ... |
@@ -520,23 +558,9 @@ static int build(struct optstruct *opt) |
520 | 520 |
return -1; |
521 | 521 |
} |
522 | 522 |
|
523 |
- if(stat("main.db", &foo) == -1 && stat("daily.db", &foo) == -1 && |
|
524 |
- stat("main.hdb", &foo) == -1 && stat("daily.hdb", &foo) == -1 && |
|
525 |
- stat("main.hdu", &foo) == -1 && stat("daily.hdu", &foo) == -1 && |
|
526 |
- stat("main.mdb", &foo) == -1 && stat("daily.mdb", &foo) == -1 && |
|
527 |
- stat("main.mdu", &foo) == -1 && stat("daily.mdu", &foo) == -1 && |
|
528 |
- stat("main.ndb", &foo) == -1 && stat("daily.ndb", &foo) == -1 && |
|
529 |
- stat("main.ndu", &foo) == -1 && stat("daily.ndu", &foo) == -1 && |
|
530 |
- stat("main.pdb", &foo) == -1 && stat("daily.pdb", &foo) == -1 && |
|
531 |
- stat("main.sdb", &foo) == -1 && stat("daily.sdb", &foo) == -1 && |
|
532 |
- stat("main.zmd", &foo) == -1 && stat("daily.zmd", &foo) == -1 && |
|
533 |
- stat("main.rmd", &foo) == -1 && stat("daily.rmd", &foo) == -1) |
|
534 |
- { |
|
535 |
- mprintf("!build: No virus database file found in current directory\n"); |
|
536 |
- return -1; |
|
537 |
- } |
|
523 |
+ dbname = strstr(opt_arg(opt, "build"), "main") ? "main" : "daily"; |
|
538 | 524 |
|
539 |
- if((ret = cl_load(".", &engine, &sigs, CL_DB_STDOPT))) { |
|
525 |
+ if((ret = cl_load(".", &engine, &sigs, CL_DB_STDOPT | CL_DB_PUA))) { |
|
540 | 526 |
mprintf("!build: Can't load database: %s\n", cl_strerror(ret)); |
541 | 527 |
return -1; |
542 | 528 |
} else { |
... | ... |
@@ -546,56 +570,38 @@ static int build(struct optstruct *opt) |
546 | 546 |
if(!sigs) { |
547 | 547 |
mprintf("!build: There are no signatures in database files\n"); |
548 | 548 |
} else { |
549 |
- lines = countlines("main.db") + countlines("daily.db") + |
|
550 |
- countlines("main.hdb") + countlines("daily.hdb") + |
|
551 |
- countlines("main.hdu") + countlines("daily.hdu") + |
|
552 |
- countlines("main.mdb") + countlines("daily.mdb") + |
|
553 |
- countlines("main.mdu") + countlines("daily.mdu") + |
|
554 |
- countlines("main.ndb") + countlines("daily.ndb") + |
|
555 |
- countlines("main.ndu") + countlines("daily.ndu") + |
|
556 |
- countlines("main.sdb") + countlines("daily.sdb") + |
|
557 |
- countlines("main.zmd") + countlines("daily.zmd") + |
|
558 |
- countlines("main.rmd") + countlines("daily.rmd") + |
|
559 |
- countlines("main.fp") + countlines("daily.fp"); |
|
560 |
- |
|
561 |
- if(lines != sigs) { |
|
562 |
- mprintf("^build: Signatures in database: %d, loaded by libclamav: %d\n", lines, sigs); |
|
563 |
- mprintf("^build: Please check the current directory and remove unnecessary databases\n"); |
|
564 |
- mprintf("^build: or install the latest ClamAV version.\n"); |
|
549 |
+ for(i = 0; dblist[i].name; i++) |
|
550 |
+ if(dblist[i].count && strstr(dblist[i].name, dbname) && !access(dblist[i].name, R_OK)) |
|
551 |
+ lines += countlines(dblist[i].name); |
|
552 |
+ |
|
553 |
+ if(lines != sigs) |
|
554 |
+ mprintf("^build: Signatures in %s db files: %u, loaded by libclamav: %u\n", dbname, lines, sigs); |
|
555 |
+ |
|
556 |
+ if(!lines || (sigs > lines && sigs - lines >= 1000)) { |
|
557 |
+ mprintf("!Bad number of signatures in database files\n"); |
|
558 |
+ return -1; |
|
565 | 559 |
} |
566 | 560 |
} |
567 | 561 |
|
568 | 562 |
/* try to read cvd header of current database */ |
569 |
- dbname = opt_arg(opt, "build"); |
|
570 |
- if(strstr(dbname, "main")) |
|
571 |
- dbname = "main"; |
|
572 |
- else |
|
573 |
- dbname = "daily"; |
|
574 |
- |
|
575 |
- |
|
576 | 563 |
if(opt->filename) { |
577 |
- if(cli_strbcasestr(opt->filename, ".cvd")) { |
|
564 |
+ if(cli_strbcasestr(opt->filename, ".cvd") || cli_strbcasestr(opt->filename, ".cld")) { |
|
578 | 565 |
strncpy(olddb, opt->filename, sizeof(olddb)); |
579 |
- inc = 0; |
|
580 |
- } else if(cli_strbcasestr(opt->filename, ".inc")) { |
|
581 |
- snprintf(olddb, sizeof(olddb), "%s/%s.info", opt->filename, dbname); |
|
582 | 566 |
} else { |
583 |
- mprintf("!build: The optional argument points to neither CVD nor incremental directory\n"); |
|
567 |
+ mprintf("!build: Not a CVD/CLD file\n"); |
|
584 | 568 |
return -1; |
585 | 569 |
} |
586 | 570 |
|
587 | 571 |
} else { |
588 | 572 |
pt = freshdbdir(); |
589 |
- snprintf(olddb, sizeof(olddb), "%s/%s.inc/%s.info", pt, dbname, dbname); |
|
590 |
- if(stat(olddb, &foo) == -1) { |
|
591 |
- inc = 0; |
|
592 |
- snprintf(olddb, sizeof(olddb), "%s/%s.cvd", pt, dbname); |
|
593 |
- } |
|
573 |
+ snprintf(olddb, sizeof(olddb), "%s/%s.cvd", pt, dbname); |
|
574 |
+ if(access(olddb, R_OK)) |
|
575 |
+ snprintf(olddb, sizeof(olddb), "%s/%s.cld", pt, dbname); |
|
594 | 576 |
free(pt); |
595 | 577 |
} |
596 | 578 |
|
597 | 579 |
if(!(oldcvd = cl_cvdhead(olddb))) { |
598 |
- mprintf("^build: CAN'T READ CVD HEADER OF CURRENT DATABASE %s\n", olddb); |
|
580 |
+ mprintf("^build: CAN'T READ CVD HEADER OF CURRENT DATABASE %s (wait 3 s)\n", olddb); |
|
599 | 581 |
sleep(3); |
600 | 582 |
} |
601 | 583 |
|
... | ... |
@@ -604,9 +610,11 @@ static int build(struct optstruct *opt) |
604 | 604 |
oldsigs = oldcvd->sigs; |
605 | 605 |
cl_cvdfree(oldcvd); |
606 | 606 |
} else { |
607 |
- fflush(stdin); |
|
608 | 607 |
mprintf("Version number: "); |
609 |
- scanf("%u", &version); |
|
608 |
+ if(scanf("%u", &version) == EOF) { |
|
609 |
+ mprintf("!build: scanf() failed\n"); |
|
610 |
+ return -1; |
|
611 |
+ } |
|
610 | 612 |
} |
611 | 613 |
|
612 | 614 |
mprintf("Total sigs: %u\n", sigs); |
... | ... |
@@ -623,33 +631,22 @@ static int build(struct optstruct *opt) |
623 | 623 |
strcat(header, smbuff); |
624 | 624 |
|
625 | 625 |
/* version */ |
626 |
- sprintf(smbuff, ":%d:", version); |
|
627 |
- strcat(header, smbuff); |
|
626 |
+ sprintf(header + strlen(header), ":%u:", version); |
|
628 | 627 |
|
629 | 628 |
/* number of signatures */ |
630 |
- sprintf(smbuff, "%d:", sigs); |
|
631 |
- strcat(header, smbuff); |
|
629 |
+ sprintf(header + strlen(header), "%u:", sigs); |
|
632 | 630 |
|
633 | 631 |
/* functionality level */ |
634 | 632 |
if(!strcmp(dbname, "main")) { |
635 |
- fflush(stdin); |
|
636 | 633 |
mprintf("Functionality level: "); |
637 |
- if(fgets(smbuff, sizeof(smbuff), stdin)) { |
|
638 |
- cli_chomp(smbuff); |
|
639 |
- } else { |
|
640 |
- mprintf("!build: Can't get functionality level\n"); |
|
641 |
- return -1; |
|
642 |
- } |
|
643 |
- fl = atoi(smbuff); |
|
644 |
- if(!fl || fl > 99) { |
|
634 |
+ if(scanf("%u", &fl) == EOF || !fl || fl > 99) { |
|
645 | 635 |
mprintf("!build: Incorrect functionality level\n"); |
646 | 636 |
return -1; |
647 | 637 |
} |
648 | 638 |
} else { |
649 | 639 |
fl = cl_retflevel(); |
650 | 640 |
} |
651 |
- sprintf(smbuff, "%u:", fl); |
|
652 |
- strcat(header, smbuff); |
|
641 |
+ sprintf(header + strlen(header), "%u:", fl); |
|
653 | 642 |
|
654 | 643 |
real_header = strlen(header); |
655 | 644 |
|
... | ... |
@@ -659,22 +656,20 @@ static int build(struct optstruct *opt) |
659 | 659 |
if((pt = getenv("SIGNDUSER"))) { |
660 | 660 |
strncpy(builder, pt, sizeof(builder)); |
661 | 661 |
} else { |
662 |
- /* ask for builder name */ |
|
663 |
- fflush(stdin); |
|
664 | 662 |
mprintf("Builder name: "); |
665 |
- if(fgets(builder, sizeof(builder), stdin)) { |
|
666 |
- cli_chomp(builder); |
|
667 |
- } else { |
|
663 |
+ if(scanf("%as", &pt) == EOF) { |
|
668 | 664 |
mprintf("!build: Can't get builder name\n"); |
669 | 665 |
return -1; |
670 | 666 |
} |
667 |
+ strncpy(builder, pt, sizeof(builder)); |
|
668 |
+ free(pt); |
|
671 | 669 |
} |
672 | 670 |
|
673 | 671 |
/* add builder */ |
674 | 672 |
strcat(header, builder); |
675 | 673 |
|
676 | 674 |
/* add current time */ |
677 |
- sprintf(header + strlen(header), ":%d", (int) timet); |
|
675 |
+ sprintf(header + strlen(header), ":%u", (unsigned int) timet); |
|
678 | 676 |
|
679 | 677 |
if(writeinfo(dbname, header) == -1) { |
680 | 678 |
mprintf("!build: Can't generate info file\n"); |
... | ... |
@@ -688,225 +683,162 @@ static int build(struct optstruct *opt) |
688 | 688 |
return -1; |
689 | 689 |
} |
690 | 690 |
|
691 |
- switch(fork()) { |
|
692 |
- case -1: |
|
693 |
- mprintf("!build: Can't fork.\n"); |
|
694 |
- free(tarfile); |
|
695 |
- return -1; |
|
696 |
- case 0: |
|
697 |
- { |
|
698 |
- const char *args[] = { "tar", "-cvf", NULL, "COPYING", "main.db", |
|
699 |
- "daily.db", "main.hdb", "daily.hdb", |
|
700 |
- "main.hdu", "daily.hdu", "main.ndb", |
|
701 |
- "daily.ndb", "main.ndu", "daily.ndu", |
|
702 |
- "main.sdb", "daily.sdb", "main.zmd", |
|
703 |
- "daily.zmd", "main.rmd", "daily.rmd", |
|
704 |
- "main.fp", "daily.fp", "daily.ft", "main.mdb", |
|
705 |
- "daily.mdb", "main.mdu", "daily.mdu", |
|
706 |
- "daily.info", "main.info", "main.wdb", |
|
707 |
- "daily.wdb", "main.pdb", "daily.pdb", |
|
708 |
- "main.cfg", "daily.cfg", |
|
709 |
- NULL }; |
|
710 |
- args[2] = tarfile; |
|
711 |
- if(!opt_check(opt, "debug")) { |
|
712 |
- if((dn = open("/dev/null", O_WRONLY)) == -1) { |
|
713 |
- mprintf("^Cannot open /dev/null\n"); |
|
714 |
- close(1); |
|
715 |
- close(2); |
|
716 |
- } else { |
|
717 |
- dup2(dn, 1); |
|
718 |
- dup2(dn, 2); |
|
719 |
- close(dn); |
|
720 |
- } |
|
721 |
- } |
|
722 |
- execv("/bin/tar", args); |
|
723 |
- mprintf("!build: Can't execute tar\n"); |
|
724 |
- perror("tar"); |
|
725 |
- free(tarfile); |
|
726 |
- return -1; |
|
727 |
- } |
|
728 |
- default: |
|
729 |
- wait(NULL); |
|
730 |
- } |
|
731 |
- |
|
732 |
- if(stat(tarfile, &foo) == -1) { |
|
733 |
- mprintf("!build: Tar archive was not created\n"); |
|
734 |
- free(tarfile); |
|
735 |
- return -1; |
|
736 |
- } |
|
737 |
- |
|
738 |
- if((tar = fopen(tarfile, "rb")) == NULL) { |
|
739 |
- mprintf("!build: Can't open file %s\n", tarfile); |
|
740 |
- free(tarfile); |
|
741 |
- return -1; |
|
742 |
- } |
|
743 |
- |
|
744 |
- if(!(gzfile = cli_gentemp("."))) { |
|
745 |
- mprintf("!build: Can't generate temporary name for gzfile\n"); |
|
691 |
+ if((tar = gzopen(tarfile, "wb")) == NULL) { |
|
692 |
+ mprintf("!build: Can't open file %s for writing\n", tarfile); |
|
746 | 693 |
free(tarfile); |
747 |
- fclose(tar); |
|
748 | 694 |
return -1; |
749 | 695 |
} |
750 | 696 |
|
751 |
- if((gz = gzopen(gzfile, "wb")) == NULL) { |
|
752 |
- mprintf("!build: Can't open file %s to write.\n", gzfile); |
|
697 |
+ if(tar_addfile(-1, tar, "COPYING") == -1) { |
|
698 |
+ mprintf("!build: Can't add COPYING to tar archive\n"); |
|
699 |
+ gzclose(tar); |
|
700 |
+ unlink(tarfile); |
|
753 | 701 |
free(tarfile); |
754 |
- fclose(tar); |
|
755 |
- free(gzfile); |
|
756 | 702 |
return -1; |
757 | 703 |
} |
758 | 704 |
|
759 |
- while((bytes = fread(buffer, 1, FILEBUFF, tar)) > 0) { |
|
760 |
- if(!gzwrite(gz, buffer, bytes)) { |
|
761 |
- mprintf("!build: Can't gzwrite to %s\n", gzfile); |
|
762 |
- fclose(tar); |
|
763 |
- gzclose(gz); |
|
764 |
- free(tarfile); |
|
765 |
- free(gzfile); |
|
766 |
- return -1; |
|
705 |
+ for(i = 0; dblist[i].name; i++) { |
|
706 |
+ if(strstr(dblist[i].name, dbname) && !access(dblist[i].name, R_OK)) { |
|
707 |
+ if(tar_addfile(-1, tar, dblist[i].name) == -1) { |
|
708 |
+ gzclose(tar); |
|
709 |
+ unlink(tarfile); |
|
710 |
+ free(tarfile); |
|
711 |
+ return -1; |
|
712 |
+ } |
|
767 | 713 |
} |
768 | 714 |
} |
715 |
+ gzclose(tar); |
|
769 | 716 |
|
770 |
- fclose(tar); |
|
771 |
- gzclose(gz); |
|
772 |
- unlink(tarfile); |
|
773 |
- free(tarfile); |
|
774 |
- |
|
775 |
- /* MD5 */ |
|
776 |
- if(!(pt = cli_md5file(gzfile))) { |
|
777 |
- mprintf("!build: Can't generate MD5 checksum for gzfile\n"); |
|
778 |
- unlink(gzfile); |
|
779 |
- free(gzfile); |
|
780 |
- return -1; |
|
781 |
- } |
|
782 |
- strcat(header, pt); |
|
783 |
- free(pt); |
|
784 |
- strcat(header, ":"); |
|
785 |
- |
|
786 |
- /* digital signature */ |
|
787 |
- if(!(tar = fopen(gzfile, "rb"))) { |
|
788 |
- mprintf("!build: Can't open file %s for reading\n", gzfile); |
|
789 |
- unlink(gzfile); |
|
790 |
- free(gzfile); |
|
717 |
+ /* MD5 + dsig */ |
|
718 |
+ if(!(fh = fopen(tarfile, "rb"))) { |
|
719 |
+ mprintf("!build: Can't open file %s for reading\n", tarfile); |
|
720 |
+ unlink(tarfile); |
|
721 |
+ free(tarfile); |
|
791 | 722 |
return -1; |
792 | 723 |
} |
793 | 724 |
|
794 |
- if(!(pt = cli_md5stream(tar, (unsigned char *) buffer))) { |
|
795 |
- mprintf("!build: Can't generate MD5 checksum for %s\n", gzfile); |
|
796 |
- unlink(gzfile); |
|
797 |
- free(gzfile); |
|
725 |
+ if(!(pt = cli_md5stream(fh, buffer))) { |
|
726 |
+ mprintf("!build: Can't generate MD5 checksum for %s\n", tarfile); |
|
727 |
+ fclose(fh); |
|
728 |
+ unlink(tarfile); |
|
729 |
+ free(tarfile); |
|
798 | 730 |
return -1; |
799 | 731 |
} |
732 |
+ rewind(fh); |
|
733 |
+ sprintf(header + strlen(header), "%s:", pt); |
|
800 | 734 |
free(pt); |
801 |
- rewind(tar); |
|
802 | 735 |
|
803 | 736 |
if(!(pt = getdsig(opt_arg(opt, "server"), builder, buffer, 16, 0))) { |
804 | 737 |
mprintf("!build: Can't get digital signature from remote server\n"); |
805 |
- unlink(gzfile); |
|
806 |
- free(gzfile); |
|
807 |
- fclose(tar); |
|
738 |
+ fclose(fh); |
|
739 |
+ unlink(tarfile); |
|
740 |
+ free(tarfile); |
|
808 | 741 |
return -1; |
809 | 742 |
} |
810 |
- strcat(header, pt); |
|
743 |
+ sprintf(header + strlen(header), "%s:", pt); |
|
811 | 744 |
free(pt); |
812 |
- strcat(header, ":"); |
|
813 | 745 |
|
814 | 746 |
/* add builder */ |
815 | 747 |
strcat(header, builder); |
816 | 748 |
|
817 | 749 |
/* add current time */ |
818 |
- sprintf(header + strlen(header), ":%d", (int) timet); |
|
750 |
+ sprintf(header + strlen(header), ":%u", (unsigned int) timet); |
|
819 | 751 |
|
820 | 752 |
/* fill up with spaces */ |
821 | 753 |
while(strlen(header) < sizeof(header) - 1) |
822 | 754 |
strcat(header, " "); |
823 | 755 |
|
824 | 756 |
/* build the final database */ |
825 |
- pt = opt_arg(opt, "build"); |
|
826 |
- if(!(cvd = fopen(pt, "wb"))) { |
|
827 |
- mprintf("!build: Can't create final database %s\n", pt); |
|
828 |
- unlink(gzfile); |
|
829 |
- free(gzfile); |
|
830 |
- fclose(tar); |
|
757 |
+ newcvd = opt_arg(opt, "build"); |
|
758 |
+ if(!(cvd = fopen(newcvd, "wb"))) { |
|
759 |
+ mprintf("!build: Can't create final database %s\n", newcvd); |
|
760 |
+ fclose(fh); |
|
761 |
+ unlink(tarfile); |
|
762 |
+ free(tarfile); |
|
831 | 763 |
return -1; |
832 | 764 |
} |
833 | 765 |
|
834 | 766 |
if(fwrite(header, 1, 512, cvd) != 512) { |
835 |
- mprintf("!build: Can't write to %s\n", pt); |
|
767 |
+ mprintf("!build: Can't write to %s\n", newcvd); |
|
768 |
+ fclose(fh); |
|
769 |
+ unlink(tarfile); |
|
770 |
+ free(tarfile); |
|
836 | 771 |
fclose(cvd); |
837 |
- fclose(tar); |
|
838 |
- unlink(pt); |
|
839 |
- unlink(gzfile); |
|
840 |
- free(gzfile); |
|
772 |
+ unlink(newcvd); |
|
841 | 773 |
return -1; |
842 | 774 |
} |
843 | 775 |
|
844 |
- while((bytes = fread(buffer, 1, FILEBUFF, tar)) > 0) { |
|
776 |
+ while((bytes = fread(buffer, 1, FILEBUFF, fh)) > 0) { |
|
845 | 777 |
if(fwrite(buffer, 1, bytes, cvd) != bytes) { |
846 |
- fclose(tar); |
|
778 |
+ mprintf("!build: Can't write to %s\n", newcvd); |
|
779 |
+ fclose(fh); |
|
780 |
+ unlink(tarfile); |
|
781 |
+ free(tarfile); |
|
847 | 782 |
fclose(cvd); |
848 |
- unlink(pt); |
|
849 |
- mprintf("!build: Can't write to %s\n", gzfile); |
|
850 |
- unlink(gzfile); |
|
851 |
- free(gzfile); |
|
783 |
+ unlink(newcvd); |
|
852 | 784 |
return -1; |
853 | 785 |
} |
854 | 786 |
} |
855 | 787 |
|
856 |
- fclose(tar); |
|
788 |
+ fclose(fh); |
|
857 | 789 |
fclose(cvd); |
858 |
- if(unlink(gzfile) == -1) { |
|
859 |
- mprintf("^build: Can't unlink %s\n", gzfile); |
|
790 |
+ |
|
791 |
+ if(unlink(tarfile) == -1) { |
|
792 |
+ mprintf("^build: Can't unlink %s\n", tarfile); |
|
793 |
+ unlink(tarfile); |
|
794 |
+ free(tarfile); |
|
795 |
+ unlink(newcvd); |
|
860 | 796 |
return -1; |
861 | 797 |
} |
862 |
- free(gzfile); |
|
798 |
+ free(tarfile); |
|
863 | 799 |
|
864 |
- mprintf("Created %s\n", pt); |
|
800 |
+ mprintf("Created %s\n", newcvd); |
|
865 | 801 |
|
866 | 802 |
/* generate patch */ |
867 |
- if(opt->filename) { |
|
868 |
- strncpy(olddb, opt->filename, sizeof(olddb)); |
|
869 |
- } else { |
|
870 |
- if(inc) { |
|
871 |
- pt = freshdbdir(); |
|
872 |
- snprintf(olddb, sizeof(olddb), "%s/%s.inc", pt, dbname); |
|
873 |
- free(pt); |
|
874 |
- } else { |
|
875 |
- pt = freshdbdir(); |
|
876 |
- snprintf(olddb, sizeof(olddb), "%s/%s.cvd", pt, dbname); |
|
877 |
- free(pt); |
|
878 |
- } |
|
803 |
+ if(!(pt = cli_gentemp(NULL))) { |
|
804 |
+ mprintf("!build: Can't generate temporary name\n"); |
|
805 |
+ unlink(newcvd); |
|
806 |
+ return -1; |
|
879 | 807 |
} |
880 | 808 |
|
881 |
- if(!inc) { |
|
882 |
- pt = cli_gentemp(NULL); |
|
883 |
- if(mkdir(pt, 0700)) { |
|
884 |
- mprintf("!build: Can't create temporary directory %s\n", pt); |
|
885 |
- return -1; |
|
886 |
- } |
|
887 |
- if(cvd_unpack(olddb, pt) == -1) { |
|
888 |
- mprintf("!build: Can't unpack CVD file %s\n", olddb); |
|
889 |
- cli_rmdirs(pt); |
|
890 |
- free(pt); |
|
891 |
- return -1; |
|
892 |
- } |
|
893 |
- strncpy(olddb, pt, sizeof(olddb)); |
|
809 |
+ if(mkdir(pt, 0700)) { |
|
810 |
+ mprintf("!build: Can't create temporary directory %s\n", pt); |
|
811 |
+ free(pt); |
|
812 |
+ unlink(newcvd); |
|
813 |
+ return -1; |
|
814 |
+ } |
|
815 |
+ |
|
816 |
+ if(cvd_unpack(olddb, pt) == -1) { |
|
817 |
+ mprintf("!build: Can't unpack CVD file %s\n", olddb); |
|
818 |
+ cli_rmdirs(pt); |
|
819 |
+ free(pt); |
|
820 |
+ unlink(newcvd); |
|
821 |
+ return -1; |
|
822 |
+ } |
|
823 |
+ strncpy(olddb, pt, sizeof(olddb)); |
|
824 |
+ free(pt); |
|
825 |
+ |
|
826 |
+ if(!(pt = cli_gentemp(NULL))) { |
|
827 |
+ mprintf("!build: Can't generate temporary name\n"); |
|
828 |
+ cli_rmdirs(olddb); |
|
829 |
+ unlink(newcvd); |
|
830 |
+ return -1; |
|
894 | 831 |
} |
895 | 832 |
|
896 |
- pt = cli_gentemp(NULL); |
|
897 | 833 |
if(mkdir(pt, 0700)) { |
898 | 834 |
mprintf("!build: Can't create temporary directory %s\n", pt); |
899 | 835 |
free(pt); |
900 |
- if(!inc) |
|
901 |
- cli_rmdirs(olddb); |
|
836 |
+ cli_rmdirs(olddb); |
|
837 |
+ unlink(newcvd); |
|
902 | 838 |
return -1; |
903 | 839 |
} |
904 |
- if(cvd_unpack(opt_arg(opt, "build"), pt) == -1) { |
|
905 |
- mprintf("!build: Can't unpack CVD file %s\n", opt_arg(opt, "build")); |
|
840 |
+ |
|
841 |
+ if(cvd_unpack(newcvd, pt) == -1) { |
|
842 |
+ mprintf("!build: Can't unpack CVD file %s\n", newcvd); |
|
906 | 843 |
cli_rmdirs(pt); |
907 | 844 |
free(pt); |
908 |
- if(!inc) |
|
909 |
- cli_rmdirs(olddb); |
|
845 |
+ cli_rmdirs(olddb); |
|
846 |
+ unlink(newcvd); |
|
910 | 847 |
return -1; |
911 | 848 |
} |
912 | 849 |
|
... | ... |
@@ -921,15 +853,13 @@ static int build(struct optstruct *opt) |
921 | 921 |
free(pt); |
922 | 922 |
|
923 | 923 |
if(ret == -1) { |
924 |
- if(!inc) |
|
925 |
- cli_rmdirs(olddb); |
|
924 |
+ cli_rmdirs(olddb); |
|
925 |
+ unlink(newcvd); |
|
926 | 926 |
return -1; |
927 | 927 |
} |
928 | 928 |
|
929 | 929 |
ret = verifydiff(patch, NULL, olddb); |
930 |
- |
|
931 |
- if(!inc) |
|
932 |
- cli_rmdirs(olddb); |
|
930 |
+ cli_rmdirs(olddb); |
|
933 | 931 |
|
934 | 932 |
if(ret == -1) { |
935 | 933 |
snprintf(broken, sizeof(broken), "%s.broken", patch); |
... | ... |
@@ -948,40 +878,31 @@ static int build(struct optstruct *opt) |
948 | 948 |
|
949 | 949 |
static int unpack(struct optstruct *opt) |
950 | 950 |
{ |
951 |
- char *name, *dbdir; |
|
952 |
- struct stat sb; |
|
951 |
+ char name[512], *dbdir; |
|
953 | 952 |
|
954 | 953 |
|
955 | 954 |
if(opt_check(opt, "unpack-current")) { |
956 | 955 |
dbdir = freshdbdir(); |
957 |
- name = malloc(strlen(dbdir) + strlen(opt_arg(opt, "unpack-current")) + 32); |
|
958 |
- sprintf(name, "%s/%s.inc", dbdir, opt_arg(opt, "unpack-current")); |
|
959 |
- if(stat(name, &sb) != -1) { |
|
960 |
- |
|
961 |
- if(dircopy(name, ".") == -1) { |
|
962 |
- mprintf("!unpack: Can't copy incremental directory %s to local directory\n", name); |
|
963 |
- free(name); |
|
956 |
+ snprintf(name, sizeof(name), "%s/%s.cvd", dbdir, opt_arg(opt, "unpack-current")); |
|
957 |
+ if(access(name, R_OK)) { |
|
958 |
+ snprintf(name, sizeof(name), "%s/%s.cld", dbdir, opt_arg(opt, "unpack-current")); |
|
959 |
+ if(access(name, R_OK)) { |
|
960 |
+ mprintf("!unpack: Couldn't find %s CLD/CVD database\n", opt_arg(opt, "unpack-current")); |
|
964 | 961 |
free(dbdir); |
965 | 962 |
return -1; |
966 | 963 |
} |
967 |
- |
|
968 |
- return 0; |
|
969 |
- |
|
970 |
- } else { |
|
971 |
- sprintf(name, "%s/%s.cvd", dbdir, opt_arg(opt, "unpack-current")); |
|
972 | 964 |
} |
973 | 965 |
free(dbdir); |
974 | 966 |
|
975 |
- } else |
|
976 |
- name = strdup(opt_arg(opt, "unpack")); |
|
967 |
+ } else { |
|
968 |
+ strncpy(name, opt_arg(opt, "unpack"), sizeof(name)); |
|
969 |
+ } |
|
977 | 970 |
|
978 | 971 |
if(cvd_unpack(name, ".") == -1) { |
979 |
- mprintf("!unpack: Can't unpack CVD file %s\n", name); |
|
980 |
- free(name); |
|
972 |
+ mprintf("!unpack: Can't unpack file %s\n", name); |
|
981 | 973 |
return -1; |
982 | 974 |
} |
983 | 975 |
|
984 |
- free(name); |
|
985 | 976 |
return 0; |
986 | 977 |
} |
987 | 978 |
|
... | ... |
@@ -1002,22 +923,29 @@ static int cvdinfo(struct optstruct *opt) |
1002 | 1002 |
pt = strchr(cvd->time, '-'); |
1003 | 1003 |
*pt = ':'; |
1004 | 1004 |
mprintf("Build time: %s\n", cvd->time); |
1005 |
- mprintf("Version: %d\n", cvd->version); |
|
1006 |
- mprintf("Signatures: %d\n", cvd->sigs); |
|
1007 |
- mprintf("Functionality level: %d\n", cvd->fl); |
|
1005 |
+ mprintf("Version: %u\n", cvd->version); |
|
1006 |
+ mprintf("Signatures: %u\n", cvd->sigs); |
|
1007 |
+ mprintf("Functionality level: %u\n", cvd->fl); |
|
1008 | 1008 |
mprintf("Builder: %s\n", cvd->builder); |
1009 |
- mprintf("MD5: %s\n", cvd->md5); |
|
1010 |
- mprintf("Digital signature: %s\n", cvd->dsig); |
|
1009 |
+ |
|
1010 |
+ pt = opt_arg(opt, "info"); |
|
1011 |
+ if(cli_strbcasestr(pt, ".cvd")) { |
|
1012 |
+ mprintf("MD5: %s\n", cvd->md5); |
|
1013 |
+ mprintf("Digital signature: %s\n", cvd->dsig); |
|
1014 |
+ cl_cvdfree(cvd); |
|
1011 | 1015 |
|
1012 | 1016 |
#ifndef HAVE_LIBGMP |
1013 |
- mprintf("^Digital signature support not compiled in.\n"); |
|
1017 |
+ mprintf("^Digital signature support not compiled in.\n"); |
|
1014 | 1018 |
#endif |
1015 | 1019 |
|
1016 |
- pt = opt_arg(opt, "info"); |
|
1017 |
- if((ret = cl_cvdverify(pt))) |
|
1018 |
- mprintf("!cvdinfo: Verification: %s\n", cl_strerror(ret)); |
|
1019 |
- else |
|
1020 |
- mprintf("Verification OK.\n"); |
|
1020 |
+ if((ret = cl_cvdverify(pt))) { |
|
1021 |
+ mprintf("!cvdinfo: Verification: %s\n", cl_strerror(ret)); |
|
1022 |
+ return -1; |
|
1023 |
+ } else { |
|
1024 |
+ mprintf("Verification OK.\n"); |
|
1025 |
+ return 0; |
|
1026 |
+ } |
|
1027 |
+ } |
|
1021 | 1028 |
|
1022 | 1029 |
cl_cvdfree(cvd); |
1023 | 1030 |
return 0; |
... | ... |
@@ -1053,7 +981,6 @@ static int listdir(const char *dirname) |
1053 | 1053 |
cli_strbcasestr(dent->d_name, ".sdb") || |
1054 | 1054 |
cli_strbcasestr(dent->d_name, ".zmd") || |
1055 | 1055 |
cli_strbcasestr(dent->d_name, ".rmd") || |
1056 |
- cli_strbcasestr(dent->d_name, ".inc") || |
|
1057 | 1056 |
cli_strbcasestr(dent->d_name, ".cvd"))) { |
1058 | 1057 |
|
1059 | 1058 |
dbfile = (char *) malloc(strlen(dent->d_name) + strlen(dirname) + 2); |
... | ... |
@@ -1081,38 +1008,34 @@ static int listdir(const char *dirname) |
1081 | 1081 |
|
1082 | 1082 |
static int listdb(const char *filename) |
1083 | 1083 |
{ |
1084 |
- FILE *fd; |
|
1084 |
+ FILE *fh; |
|
1085 | 1085 |
char *buffer, *pt, *start, *dir; |
1086 |
- int line = 0; |
|
1086 |
+ unsigned int line = 0; |
|
1087 | 1087 |
const char *tmpdir; |
1088 | 1088 |
|
1089 | 1089 |
|
1090 |
- if(cli_strbcasestr(filename, ".inc")) { /* incremental directory */ |
|
1091 |
- if(listdir(filename) == -1) { |
|
1092 |
- mprintf("!listdb: Can't list incremental directory %s\n", filename); |
|
1093 |
- return -1; |
|
1094 |
- } |
|
1095 |
- return 0; |
|
1096 |
- } |
|
1097 |
- |
|
1098 |
- if((fd = fopen(filename, "rb")) == NULL) { |
|
1090 |
+ if((fh = fopen(filename, "rb")) == NULL) { |
|
1099 | 1091 |
mprintf("!listdb: Can't open file %s\n", filename); |
1100 | 1092 |
return -1; |
1101 | 1093 |
} |
1102 | 1094 |
|
1103 | 1095 |
if(!(buffer = (char *) malloc(FILEBUFF))) { |
1104 | 1096 |
mprintf("!listdb: Can't allocate memory for buffer\n"); |
1105 |
- fclose(fd); |
|
1097 |
+ fclose(fh); |
|
1106 | 1098 |
return -1; |
1107 | 1099 |
} |
1108 | 1100 |
|
1109 | 1101 |
/* check for CVD file */ |
1110 |
- fgets(buffer, 12, fd); |
|
1111 |
- rewind(fd); |
|
1102 |
+ if(!fgets(buffer, 12, fh)) { |
|
1103 |
+ mprintf("!listdb: fgets failed\n"); |
|
1104 |
+ fclose(fh); |
|
1105 |
+ return -1; |
|
1106 |
+ } |
|
1107 |
+ rewind(fh); |
|
1112 | 1108 |
|
1113 | 1109 |
if(!strncmp(buffer, "ClamAV-VDB:", 11)) { |
1114 | 1110 |
free(buffer); |
1115 |
- fclose(fd); |
|
1111 |
+ fclose(fh); |
|
1116 | 1112 |
|
1117 | 1113 |
tmpdir = getenv("TMPDIR"); |
1118 | 1114 |
if(tmpdir == NULL) |
... | ... |
@@ -1156,12 +1079,12 @@ static int listdb(const char *filename) |
1156 | 1156 |
|
1157 | 1157 |
if(cli_strbcasestr(filename, ".db")) { /* old style database */ |
1158 | 1158 |
|
1159 |
- while(fgets(buffer, FILEBUFF, fd)) { |
|
1159 |
+ while(fgets(buffer, FILEBUFF, fh)) { |
|
1160 | 1160 |
line++; |
1161 | 1161 |
pt = strchr(buffer, '='); |
1162 | 1162 |
if(!pt) { |
1163 |
- mprintf("!listdb: Malformed pattern line %d (file %s)\n", line, filename); |
|
1164 |
- fclose(fd); |
|
1163 |
+ mprintf("!listdb: Malformed pattern line %u (file %s)\n", line, filename); |
|
1164 |
+ fclose(fh); |
|
1165 | 1165 |
free(buffer); |
1166 | 1166 |
return -1; |
1167 | 1167 |
} |
... | ... |
@@ -1177,14 +1100,14 @@ static int listdb(const char *filename) |
1177 | 1177 |
|
1178 | 1178 |
} else if(cli_strbcasestr(filename, ".hdb") || cli_strbcasestr(filename, ".hdu") || cli_strbcasestr(filename, ".mdb") || cli_strbcasestr(filename, ".mdu")) { /* hash database */ |
1179 | 1179 |
|
1180 |
- while(fgets(buffer, FILEBUFF, fd)) { |
|
1180 |
+ while(fgets(buffer, FILEBUFF, fh)) { |
|
1181 | 1181 |
line++; |
1182 | 1182 |
cli_chomp(buffer); |
1183 | 1183 |
start = cli_strtok(buffer, 2, ":"); |
1184 | 1184 |
|
1185 | 1185 |
if(!start) { |
1186 |
- mprintf("!listdb: Malformed pattern line %d (file %s)\n", line, filename); |
|
1187 |
- fclose(fd); |
|
1186 |
+ mprintf("!listdb: Malformed pattern line %u (file %s)\n", line, filename); |
|
1187 |
+ fclose(fh); |
|
1188 | 1188 |
free(buffer); |
1189 | 1189 |
return -1; |
1190 | 1190 |
} |
... | ... |
@@ -1198,14 +1121,14 @@ static int listdb(const char *filename) |
1198 | 1198 |
|
1199 | 1199 |
} else if(cli_strbcasestr(filename, ".ndb") || cli_strbcasestr(filename, ".ndu") || cli_strbcasestr(filename, ".sdb") || cli_strbcasestr(filename, ".zmd") || cli_strbcasestr(filename, ".rmd")) { |
1200 | 1200 |
|
1201 |
- while(fgets(buffer, FILEBUFF, fd)) { |
|
1201 |
+ while(fgets(buffer, FILEBUFF, fh)) { |
|
1202 | 1202 |
line++; |
1203 | 1203 |
cli_chomp(buffer); |
1204 | 1204 |
start = cli_strtok(buffer, 0, ":"); |
1205 | 1205 |
|
1206 | 1206 |
if(!start) { |
1207 |
- mprintf("!listdb: Malformed pattern line %d (file %s)\n", line, filename); |
|
1208 |
- fclose(fd); |
|
1207 |
+ mprintf("!listdb: Malformed pattern line %u (file %s)\n", line, filename); |
|
1208 |
+ fclose(fh); |
|
1209 | 1209 |
free(buffer); |
1210 | 1210 |
return -1; |
1211 | 1211 |
} |
... | ... |
@@ -1217,8 +1140,7 @@ static int listdb(const char *filename) |
1217 | 1217 |
free(start); |
1218 | 1218 |
} |
1219 | 1219 |
} |
1220 |
- |
|
1221 |
- fclose(fd); |
|
1220 |
+ fclose(fh); |
|
1222 | 1221 |
free(buffer); |
1223 | 1222 |
return 0; |
1224 | 1223 |
} |
... | ... |
@@ -1260,7 +1182,12 @@ static int vbadump(struct optstruct *opt) |
1260 | 1260 |
} |
1261 | 1261 |
|
1262 | 1262 |
/* generate the temporary directory */ |
1263 |
- dir = cli_gentemp(NULL); |
|
1263 |
+ if(!(dir = cli_gentemp(NULL))) { |
|
1264 |
+ mprintf("!vbadump: Can't generate temporary name\n"); |
|
1265 |
+ close(fd); |
|
1266 |
+ return -1; |
|
1267 |
+ } |
|
1268 |
+ |
|
1264 | 1269 |
if(mkdir(dir, 0700)) { |
1265 | 1270 |
mprintf("!vbadump: Can't create temporary directory %s\n", dir); |
1266 | 1271 |
free(dir); |
... | ... |
@@ -1378,7 +1305,8 @@ static int compare(const char *oldpath, const char *newpath, FILE *diff) |
1378 | 1378 |
if((pt = strchr(tbuff, ' '))) |
1379 | 1379 |
*pt = 0; |
1380 | 1380 |
fprintf(diff, "DEL %u %s\n", oline + i, tbuff); |
1381 |
- fgets(tbuff, sizeof(tbuff), old); |
|
1381 |
+ if(!fgets(tbuff, sizeof(tbuff), old)) |
|
1382 |
+ break; |
|
1382 | 1383 |
} |
1383 | 1384 |
oline += tline; |
1384 | 1385 |
|
... | ... |
@@ -1415,7 +1343,6 @@ static int compare(const char *oldpath, const char *newpath, FILE *diff) |
1415 | 1415 |
static int verifydiff(const char *diff, const char *cvd, const char *incdir) |
1416 | 1416 |
{ |
1417 | 1417 |
char *tempdir, cwd[512], buff[1024], info[32], *md5, *pt; |
1418 |
- const char *cpt; |
|
1419 | 1418 |
FILE *fh; |
1420 | 1419 |
int ret = 0, fd; |
1421 | 1420 |
unsigned short mode; |
... | ... |
@@ -1465,7 +1392,12 @@ static int verifydiff(const char *diff, const char *cvd, const char *incdir) |
1465 | 1465 |
return -1; |
1466 | 1466 |
} |
1467 | 1467 |
|
1468 |
- getcwd(cwd, sizeof(cwd)); |
|
1468 |
+ if(!getcwd(cwd, sizeof(cwd))) { |
|
1469 |
+ mprintf("!verifydiff: getcwd() failed\n"); |
|
1470 |
+ cli_rmdirs(tempdir); |
|
1471 |
+ free(tempdir); |
|
1472 |
+ return -1; |
|
1473 |
+ } |
|
1469 | 1474 |
|
1470 | 1475 |
if(chdir(tempdir) == -1) { |
1471 | 1476 |
mprintf("!verifydiff: Can't chdir to %s\n", tempdir); |
... | ... |
@@ -1477,7 +1409,8 @@ static int verifydiff(const char *diff, const char *cvd, const char *incdir) |
1477 | 1477 |
|
1478 | 1478 |
if(cdiff_apply(fd, mode) == -1) { |
1479 | 1479 |
mprintf("!verifydiff: Can't apply %s\n", diff); |
1480 |
- chdir(cwd); |
|
1480 |
+ if(chdir(cwd) == -1) |
|
1481 |
+ mprintf("^verifydiff: Can't chdir to %s\n", cwd); |
|
1481 | 1482 |
cli_rmdirs(tempdir); |
1482 | 1483 |
free(tempdir); |
1483 | 1484 |
close(fd); |
... | ... |
@@ -1485,26 +1418,24 @@ static int verifydiff(const char *diff, const char *cvd, const char *incdir) |
1485 | 1485 |
} |
1486 | 1486 |
close(fd); |
1487 | 1487 |
|
1488 |
- cvd ? (cpt = cvd) : (cpt = incdir); |
|
1489 |
- |
|
1490 |
- if(strstr(cpt, "main")) |
|
1488 |
+ if(strstr(diff, "main")) |
|
1491 | 1489 |
strcpy(info, "main.info"); |
1492 | 1490 |
else |
1493 | 1491 |
strcpy(info, "daily.info"); |
1494 | 1492 |
|
1495 | 1493 |
if(!(fh = fopen(info, "r"))) { |
1496 | 1494 |
mprintf("!verifydiff: Can't open %s\n", info); |
1497 |
- chdir(cwd); |
|
1495 |
+ if(chdir(cwd) == -1) |
|
1496 |
+ mprintf("^verifydiff: Can't chdir to %s\n", cwd); |
|
1498 | 1497 |
cli_rmdirs(tempdir); |
1499 | 1498 |
free(tempdir); |
1500 | 1499 |
return -1; |
1501 | 1500 |
} |
1502 | 1501 |
|
1503 |
- fgets(buff, sizeof(buff), fh); |
|
1504 |
- |
|
1505 |
- if(strncmp(buff, "ClamAV-VDB", 10)) { |
|
1502 |
+ if(!fgets(buff, sizeof(buff), fh) || strncmp(buff, "ClamAV-VDB", 10)) { |
|
1506 | 1503 |
mprintf("!verifydiff: Incorrect info file %s\n", info); |
1507 |
- chdir(cwd); |
|
1504 |
+ if(chdir(cwd) == -1) |
|
1505 |
+ mprintf("^verifydiff: Can't chdir to %s\n", cwd); |
|
1508 | 1506 |
cli_rmdirs(tempdir); |
1509 | 1507 |
free(tempdir); |
1510 | 1508 |
return -1; |
... | ... |
@@ -1531,7 +1462,8 @@ static int verifydiff(const char *diff, const char *cvd, const char *incdir) |
1531 | 1531 |
} |
1532 | 1532 |
|
1533 | 1533 |
fclose(fh); |
1534 |
- chdir(cwd); |
|
1534 |
+ if(chdir(cwd) == -1) |
|
1535 |
+ mprintf("^verifydiff: Can't chdir to %s\n", cwd); |
|
1535 | 1536 |
cli_rmdirs(tempdir); |
1536 | 1537 |
free(tempdir); |
1537 | 1538 |
|
... | ... |
@@ -1553,7 +1485,10 @@ static int diffdirs(const char *old, const char *new, const char *patch) |
1553 | 1553 |
char cwd[512], opath[1024]; |
1554 | 1554 |
|
1555 | 1555 |
|
1556 |
- getcwd(cwd, sizeof(cwd)); |
|
1556 |
+ if(!getcwd(cwd, sizeof(cwd))) { |
|
1557 |
+ mprintf("!diffdirs: getcwd() failed\n"); |
|
1558 |
+ return -1; |
|
1559 |
+ } |
|
1557 | 1560 |
|
1558 | 1561 |
if(!(diff = fopen(patch, "w"))) { |
1559 | 1562 |
mprintf("!diffdirs: Can't open %s for writing\n", patch); |
... | ... |
@@ -1594,7 +1529,8 @@ static int diffdirs(const char *old, const char *new, const char *patch) |
1594 | 1594 |
|
1595 | 1595 |
fclose(diff); |
1596 | 1596 |
mprintf("Generated diff file %s\n", patch); |
1597 |
- chdir(cwd); |
|
1597 |
+ if(chdir(cwd) == -1) |
|
1598 |
+ mprintf("^diffdirs: Can't chdir to %s\n", cwd); |
|
1598 | 1599 |
|
1599 | 1600 |
return 0; |
1600 | 1601 |
} |
... | ... |
@@ -1723,16 +1659,16 @@ static void help(void) |
1723 | 1723 |
mprintf(" --html-normalise=FILE create normalised parts of HTML file\n"); |
1724 | 1724 |
mprintf(" --utf16-decode=FILE decode UTF16 encoded files\n"); |
1725 | 1725 |
mprintf(" --info=FILE -i FILE print database information\n"); |
1726 |
- mprintf(" --build=NAME [cvd/inc] -b NAME build a CVD file\n"); |
|
1726 |
+ mprintf(" --build=NAME [cvd] -b NAME build a CVD file\n"); |
|
1727 | 1727 |
mprintf(" --server=ADDR ClamAV Signing Service address\n"); |
1728 |
- mprintf(" --unpack=FILE -u FILE Unpack a CVD file\n"); |
|
1729 |
- mprintf(" --unpack-current=SHORTNAME Unpack local CVD/INCDIR in cwd\n"); |
|
1728 |
+ mprintf(" --unpack=FILE -u FILE Unpack a CVD/CLD file\n"); |
|
1729 |
+ mprintf(" --unpack-current=SHORTNAME Unpack local CVD/CLD into cwd\n"); |
|
1730 | 1730 |
mprintf(" --list-sigs[=FILE] -l[FILE] List signature names\n"); |
1731 | 1731 |
mprintf(" --vba=FILE Extract VBA/Word6 macro code\n"); |
1732 | 1732 |
mprintf(" --vba-hex=FILE Extract Word6 macro code with hex values\n"); |
1733 | 1733 |
mprintf(" --diff=OLD NEW -d OLD NEW Create diff for OLD and NEW CVDs\n"); |
1734 | 1734 |
mprintf(" --run-cdiff=FILE -r FILE Execute update script FILE in cwd\n"); |
1735 |
- mprintf(" --verify-cdiff=DIFF CVD/INCDIR Verify DIFF against CVD\n"); |
|
1735 |
+ mprintf(" --verify-cdiff=DIFF CVD/CLD Verify DIFF against CVD/CLD\n"); |
|
1736 | 1736 |
mprintf("\n"); |
1737 | 1737 |
|
1738 | 1738 |
return; |