sigtool/sigtool.c
b151ef55
 /*
b2e70235
  *  Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net>
b151ef55
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
 
8b242bb9
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
b151ef55
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
4cd4319e
 #include <zlib.h>
 #include <time.h>
 #include <locale.h>
4bf2cf8d
 #include <sys/types.h>
f852d214
 #include <sys/stat.h>
 #include <fcntl.h>
4bf2cf8d
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
b151ef55
 #include <clamav.h>
e8217f5a
 #include <sys/wait.h>
d5314812
 #include <dirent.h>
b151ef55
 
 #include "options.h"
 #include "others.h"
 #include "strings.h"
e8217f5a
 #include "md5.h"
 #include "cvd.h"
 #include "str.h"
36f2038b
 #include "memory.h"
 #include "output.h"
cfe76364
 #include "cfgparser.h"
e441c102
 #include "misc.h"
f58ad3be
 #include "vba.h"
f91f55e0
 #include "../clamscan/others.h"
 #include "../libclamav/others.h"
 #include "../libclamav/str.h"
f58ad3be
 #include "../libclamav/ole2_extract.h"
 #include "../libclamav/htmlnorm.h"
b151ef55
 
78a9e624
 #ifndef	O_BINARY
 #define	O_BINARY	0
 #endif
13847a2d
 
b151ef55
 void help(void);
4bf2cf8d
 char *getdsig(const char *host, const char *user, const char *data);
 void cvdinfo(struct optstruct *opt);
e8217f5a
 int build(struct optstruct *opt);
 int unpack(struct optstruct *opt);
d5314812
 int listdb(const char *filename);
 int listdir(const char *dirname);
 void listsigs(struct optstruct *opt);
b151ef55
 
 
 void sigtool(struct optstruct *opt)
 {
 
25133aef
     if(optl(opt, "quiet"))
 	mprintf_quiet = 1;
b151ef55
 
25133aef
     if(optl(opt, "stdout"))
 	mprintf_stdout = 1;
b151ef55
 
c0eb3ceb
     if(optl(opt, "debug"))
 	cl_debug();
 
b151ef55
     if(optc(opt, 'V')) {
e441c102
 	print_version();
b151ef55
 	exit(0);
     }
 
     if(optc(opt, 'h')) {
 	free_opt(opt);
     	help();
     }
 
     if(optl(opt, "hex-dump")) {
25133aef
 	    char buffer[FILEBUFF];
 	    int bytes;
 	    char *pt;
b151ef55
 
7b7b3ca5
 	while((bytes = read(0, buffer, FILEBUFF)) > 0) {
f91f55e0
 	    pt = cli_str2hex(buffer, bytes);
b151ef55
 	    write(1, pt, 2 * bytes);
 	    free(pt);
 	}
 
94da957a
     } else if(optl(opt, "md5")) {
25133aef
 	    char *md5, *filename;
 	    int i;
 	    struct stat sb;
94da957a
 
25133aef
 	mprintf_stdout = 1;
 
 	if(opt->filename) {
 
 	    for(i = 0; (filename = cli_strtok(opt->filename, i, "\t")); i++) {
 		if(stat(filename, &sb) == -1) {
 		    mprintf("!Can't access file %s\n", filename);
 		    perror(filename);
 		} else {
 		    if((sb.st_mode & S_IFMT) == S_IFREG) {
 			if((md5 = cli_md5file(filename))) {
 			    mprintf("%s:%d:%s\n", md5, sb.st_size, filename);
 			    free(md5);
 			} else
 			    mprintf("!Can't generate MD5 checksum for %s\n", filename);
 		    }
 		}
 
 		free(filename);
 	    }
 
 	} else {
 
d2a12ffd
 	    md5 = cli_md5stream(stdin, NULL);
25133aef
 	    mprintf("%s\n", md5);
 	    free(md5);
 	}
94da957a
 
f852d214
     } else if(optl(opt, "html-normalise")) {
 	    int fd;
 
 	if((fd = open(getargl(opt, "html-normalise"), O_RDONLY)) == -1) {
 	    mprintf("Can't open file %s\n", getargl(opt, "html-normalise"));
 	    exit(1);
 	}
 
 	html_normalise_fd(fd, ".", NULL);
 
 	close(fd);
 
4cd4319e
     } else if(optc(opt, 'b')) {
6765b5df
 	if(!optl(opt, "server")) {
 	    mprintf("!--server is required in this mode\n");
4bf2cf8d
 	    exit(10);
 	}
4cd4319e
 
 	build(opt);
 
42e6f5a6
     } else if(optc(opt, 'u')) {
 
 	unpack(opt);
 
e1ef4066
     } else if(optl(opt, "unpack-current")) {
 
 	unpack(opt);
 
4cd4319e
     } else if(optc(opt, 'i')) {
 
 	cvdinfo(opt);
 
50099661
     } else if(optc(opt, 'l')) {
d5314812
 
 	listsigs(opt);
 
d6bf11e3
     } else if(optl(opt, "vba") || optl(opt, "vba-hex")) {
             int fd, hex_output=0;
 	    char *dir;
 
 
 	if (optl(opt, "vba-hex"))
 		hex_output = 1;
  
         /* generate the temporary directory */
3a18431e
         dir = cli_gentemp(NULL);
d6bf11e3
         if(mkdir(dir, 0700)) {
             mprintf("vba dump: Can't create temporary directory %s\n", dir);
             return;
         }
3a18431e
 
d6bf11e3
         if((fd = open(getargl(opt, "vba"), O_RDONLY)) == -1) {
 	    if((fd = open(getargl(opt, "vba-hex"), O_RDONLY)) == -1) {
         	mprintf("Can't open file %s\n", getargl(opt, "vba"));
         	exit(1);
 	    }
         }
 
         if(cli_ole2_extract(fd, dir, NULL)) {
             cli_rmdirs(dir);
             free(dir);
 	    close(fd);
             return;
3a18431e
         }
d6bf11e3
 
 	close(fd);
         sigtool_vba_scandir(dir, hex_output);
         cli_rmdirs(dir);
         free(dir);
 
b151ef55
     } else {
 
94da957a
 	help();
b151ef55
     }
 
 }
 
442d8407
 int countlines(const char *filename)
 {
 	FILE *fd;
 	char buff[65536];
 	int lines = 0;
 
eceef468
 
442d8407
     if((fd = fopen(filename, "r")) == NULL)
 	return 0;
 
eceef468
     while(fgets(buff, sizeof(buff), fd)) {
 	if(buff[0] == '#') continue;
442d8407
 	lines++;
eceef468
     }
442d8407
 
     fclose(fd);
     return lines;
 }
 
4cd4319e
 int build(struct optstruct *opt)
 {
42e6f5a6
 	int ret, no = 0, realno = 0, bytes, itmp;
4cd4319e
 	struct stat foo;
372063ec
 	char buffer[FILEBUFF], *tarfile = NULL, *gzfile = NULL, header[512],
b2e70235
 	     smbuff[30], *pt, *dbdir;
4cd4319e
         struct cl_node *root = NULL;
4bf2cf8d
 	FILE *tar, *cvd, *fd;
4cd4319e
 	gzFile *gz;
 	time_t timet;
 	struct tm *brokent;
42e6f5a6
 	struct cl_cvd *oldcvd = NULL;
4cd4319e
 
 
     if(stat("COPYING", &foo) == -1) {
 	mprintf("COPYING file not found in current working directory.\n");
 	exit(1);
     }
 
92152740
     if(stat("main.db", &foo) == -1 && stat("daily.db", &foo) == -1 && stat("main.hdb", &foo) == -1 && stat("daily.hdb", &foo) == -1 && stat("main.ndb", &foo) == -1 && stat("daily.ndb", &foo) == -1 && stat("main.zmd", &foo) == -1 && stat("main.rmd", &foo) == -1 && stat("daily.zmd", &foo) == -1 && stat("daily.rmd", &foo) == -1) {
4cd4319e
 	mprintf("Virus database not found in current working directory.\n");
 	exit(1);
     }
 
     cl_debug(); /* enable debug messages */
 
442d8407
     if((ret = cl_loaddbdir(".", &root, &no))) {
4cd4319e
 	mprintf("!Can't load database: %s\n", cl_strerror(ret));
42e6f5a6
 	exit(1);
4cd4319e
     }
 
f91f55e0
     cl_free(root);
4cd4319e
 
     mprintf("Database properly parsed.\n");
 
42e6f5a6
     if(!no) {
4cd4319e
 	mprintf("WARNING: There are no signatures in the database(s).\n");
42e6f5a6
     } else {
4cd4319e
 	mprintf("Signatures: %d\n", no);
e483f51a
 	realno = countlines("main.db") + countlines("daily.db") + countlines("main.hdb") + countlines("daily.hdb") + countlines("main.ndb") + countlines("daily.ndb") + countlines("main.zmd") + countlines("daily.zmd") + countlines("main.rmd") + countlines("daily.rmd") + countlines("main.fp") + countlines("daily.fp");
42e6f5a6
 	if(realno != no) {
 	    mprintf("!Signatures in database: %d. Loaded: %d.\n", realno, no);
 	    mprintf("Please check the current directory and remove unnecessary databases\n");
 	    mprintf("or install the latest ClamAV version.\n");
 	    exit(1);
 	}
     }
4cd4319e
 
f91f55e0
     tarfile = cli_gentemp(".");
4cd4319e
 
     switch(fork()) {
 	case -1:
 	    mprintf("!Can't fork.\n");
 	    exit(1);
 	case 0:
 	    {
92152740
 		char *args[] = { "tar", "-cvf", NULL, "COPYING", "main.db", "daily.db", "Notes", "viruses.db3", "main.hdb", "daily.hdb", "main.ndb", "daily.ndb", "main.zmd", "daily.zmd", "main.rmd", "daily.rmd", "main.fp", "daily.fp", NULL };
2457e0a8
 		args[2] = tarfile;
4cd4319e
 		execv("/bin/tar", args);
 		mprintf("!Can't execute tar\n");
 		perror("tar");
 		exit(1);
 	    }
 	default:
 	    wait(NULL);
     }
 
     if(stat(tarfile, &foo) == -1) {
 	mprintf("!Can't generate tar file.\n");
 	exit(1);
     }
 
     if((tar = fopen(tarfile, "rb")) == NULL) {
 	mprintf("!Can't open file %s\n", tarfile);
 	exit(1);
     }
 
f91f55e0
     gzfile = cli_gentemp(".");
4cd4319e
     if((gz = gzopen(gzfile, "wb")) == NULL) {
 	mprintf("!Can't open file %s to write.\n", gzfile);
 	exit(1);
     }
 
7b7b3ca5
     while((bytes = fread(buffer, 1, FILEBUFF, tar)) > 0)
4cd4319e
 	gzwrite(gz, buffer, bytes);
 
     fclose(tar);
     unlink(tarfile);
     free(tarfile);
 
     gzclose(gz);
 
42e6f5a6
 
     /* try to read cvd header of old database */
b2e70235
     dbdir = freshdbdir();
     sprintf(buffer, "%s/%s", dbdir, getargc(opt, 'b'));
     free(dbdir);
42e6f5a6
     if((oldcvd = cl_cvdhead(buffer)) == NULL)
 	mprintf("WARNING: CAN'T READ CVD HEADER OF CURRENT DATABASE %s\n", buffer);
 
4cd4319e
     /* generate header */
 
     /* magic string */
 
7cc3891c
     strcpy(header, "ClamAV-VDB:");
4cd4319e
 
     /* time */
 
     time(&timet);
     brokent = localtime(&timet);
     setlocale(LC_TIME, "C");
6765b5df
     strftime(smbuff, 30, "%d %b %Y %H-%M %z", brokent);
7cc3891c
     strcat(header, smbuff);
4cd4319e
 
     /* version number */
 
13847a2d
     /* ... increment version number by one */
4cd4319e
 
42e6f5a6
     if(oldcvd) {
6765b5df
 	sprintf(smbuff, ":%d:", oldcvd->version + 1);
42e6f5a6
     } else {
 	fflush(stdin);
 	mprintf("Version number: ");
 	scanf("%d", &itmp);
 	sprintf(smbuff, "%d:", itmp);
     }
7cc3891c
     strcat(header, smbuff);
4cd4319e
 
     /* number of signatures */
     sprintf(smbuff, "%d:", no);
7cc3891c
     strcat(header, smbuff);
4cd4319e
 
1309a55c
     /* functionality level */
     sprintf(smbuff, "%d:", cl_retflevel());
7cc3891c
     strcat(header, smbuff);
4cd4319e
 
     /* MD5 */
f91f55e0
     pt = cli_md5file(gzfile);
7cc3891c
     strcat(header, pt);
4bf2cf8d
     free(pt);
7cc3891c
     strcat(header, ":");
4cd4319e
 
372063ec
     /* ask for builder name */
4bf2cf8d
     fflush(stdin);
     mprintf("Builder id: ");
d5314812
     fscanf(stdin, "%s", smbuff);
4bf2cf8d
 
4cd4319e
     /* digital signature */
4bf2cf8d
     fd = fopen(gzfile, "rb");
d2a12ffd
     pt = cli_md5stream(fd, buffer);
4bf2cf8d
     fclose(fd);
d2a12ffd
     free(pt);
6765b5df
     if(!(pt = getdsig(getargl(opt, "server"), smbuff, buffer))) {
4bf2cf8d
 	mprintf("No digital signature - no CVD file...\n");
42e6f5a6
 	unlink(gzfile);
4bf2cf8d
 	exit(1);
     }
 
7cc3891c
     strcat(header, pt);
4bf2cf8d
     free(pt);
7cc3891c
     strcat(header, ":");
4cd4319e
 
372063ec
     /* add builder */
7cc3891c
     strcat(header, smbuff);
4cd4319e
 
372063ec
     /* add current time */
     sprintf(header + strlen(header), ":%d", (int) timet);
 
4cd4319e
     /* fill up with spaces */
 
c0eb3ceb
     while(strlen(header) < 512)
7cc3891c
 	strcat(header, " ");
4cd4319e
 
     /* build the final database */
 
     pt = getargc(opt, 'b');
     if((cvd = fopen(pt, "wb")) == NULL) {
 	mprintf("!Can't write the final database %s\n", pt);
42e6f5a6
 	unlink(gzfile);
4cd4319e
 	exit(1);
     }
 
c0eb3ceb
     fwrite(header, 1, 512, cvd);
4cd4319e
 
     if((tar = fopen(gzfile, "rb")) == NULL) {
 	mprintf("!Can't open file %s for reading.\n", gzfile);
 	exit(1);
     }
 
7b7b3ca5
     while((bytes = fread(buffer, 1, FILEBUFF, tar)) > 0)
4cd4319e
 	fwrite(buffer, 1, bytes, cvd);
 
     fclose(tar);
     fclose(cvd);
 
     unlink(gzfile);
     free(gzfile);
 
     mprintf("Database %s created.\n", pt);
 
13847a2d
     /* try to load final cvd */
e8217f5a
     return 0;
4cd4319e
 }
 
 void cvdinfo(struct optstruct *opt)
 {
 	struct cl_cvd *cvd;
 	char *pt;
c0eb3ceb
 	int ret;
4cd4319e
 
c0eb3ceb
     pt = getargc(opt, 'i');
     if((cvd = cl_cvdhead(pt)) == NULL) {
03186fe8
 	mprintf("!Can't read/parse CVD header from %s\n", pt);
4cd4319e
 	exit(1);
     }
 
e1ef4066
     mprintf("Build time: %s\n", cvd->time);
4cd4319e
     mprintf("Version: %d\n", cvd->version);
     mprintf("# of signatures: %d\n", cvd->sigs);
     mprintf("Functionality level: %d\n", cvd->fl);
     mprintf("Builder: %s\n", cvd->builder);
     mprintf("MD5: %s\n", cvd->md5);
 
     mprintf("Digital signature: %s\n", cvd->dsig);
 
c0eb3ceb
 #ifndef HAVE_GMP
     mprintf("Digital signature support not compiled in.\n");
 #endif
 
     if((ret = cl_cvdverify(pt)))
 	mprintf("!Verification: %s\n", cl_strerror(ret));
     else
 	mprintf("Verification OK.\n");
 
13847a2d
     /* free */
4cd4319e
 }
 
4bf2cf8d
 char *getdsig(const char *host, const char *user, const char *data)
 {
 	char buff[300], cmd[100], *pass, *pt;
         struct sockaddr_in server;
 	int sockd, bread, len;
 
 
 #ifdef PF_INET
     if((sockd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
 #else
     if((sockd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
 #endif
 	perror("socket()");
 	mprintf("!Can't create the socket.\n");
 	return NULL;
     }
 
     server.sin_family = AF_INET;
     server.sin_addr.s_addr = inet_addr(host);
     server.sin_port = htons(33101);
 
     if(connect(sockd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0) {
         close(sockd);
 	perror("connect()");
 	mprintf("!Can't connect to ClamAV Signing Service at %s.\n", host);
 	return NULL;
     }
 
     memset(cmd, 0, sizeof(cmd));
     pass = getpass("Password:");
     sprintf(cmd, "ClamSign:%s:%s:", user, pass);
     len = strlen(cmd);
     pt = cmd;
     pt += len;
     memcpy(pt, data, 16);
     len += 16;
 
     if(write(sockd, cmd, len) < 0) {
 	mprintf("!Can't write to the socket.\n");
 	close(sockd);
 	memset(cmd, 0, len);
 	memset(pass, 0, strlen(pass));
 	return NULL;
     }
 
     memset(cmd, 0, len);
     memset(pass, 0, strlen(pass));
 
     memset(buff, 0, sizeof(buff));
e8217f5a
     if((bread = read(sockd, buff, sizeof(buff))) > 0) {
4bf2cf8d
 	if(!strstr(buff, "Signature:")) {
 	    mprintf("!Signature generation error.\n");
 	    mprintf("ClamAV SDaemon: %s.\n", buff);
 	    close(sockd);
 	    return NULL;
e8217f5a
 	} else {
4bf2cf8d
 	    mprintf("Signature received (length = %d).\n", strlen(buff) - 10);
e8217f5a
 	}
     }
4bf2cf8d
 
     close(sockd);
     pt = buff;
     pt += 10;
     return strdup(pt);
 }
42e6f5a6
 
 int unpack(struct optstruct *opt)
 {
78a9e624
 	int fd;
b2e70235
 	char *name, *dbdir;
42e6f5a6
 
e1ef4066
     if(optl(opt, "unpack-current")) {
b2e70235
 	dbdir = freshdbdir();
 	name = mcalloc(strlen(dbdir) + strlen(getargl(opt, "unpack-current")) + 2, sizeof(char));
 	sprintf(name, "%s/%s", dbdir, getargl(opt, "unpack-current"));
 	free(dbdir);
e1ef4066
     } else
78a9e624
 	name = strdup(getargc(opt, 'u'));
42e6f5a6
 
78a9e624
     if((fd = open(name, O_RDONLY|O_BINARY)) == -1) {
e1ef4066
 	mprintf("!Can't open CVD file %s\n", name);
78a9e624
 	free(name);
42e6f5a6
 	exit(1);
     }
 
78a9e624
     free(name);
     lseek(fd, 512, SEEK_SET);
42e6f5a6
 
78a9e624
     if(cli_untgz(fd, ".")) {
42e6f5a6
 	mprintf("!Can't unpack file.\n");
78a9e624
 	close(fd);
42e6f5a6
 	exit(1);
     }
 
78a9e624
     close(fd);
42e6f5a6
     exit(0);
 }
d5314812
 
 int listdb(const char *filename)
 {
 	FILE *fd, *tmpd;
 	char *buffer, *pt, *start, *dir, *tmp;
 	int line = 0, bytes;
 	const char *tmpdir;
 
 
     if((fd = fopen(filename, "rb")) == NULL) {
 	mprintf("!listdb(): Can't open file %s\n", filename);
 	return -1;
     }
 
     if(!(buffer = (char *) mmalloc(FILEBUFF))) {
 	mprintf("!listdb(): Can't allocate memory.\n");
25133aef
 	fclose(fd);
d5314812
 	return -1;
     }
 
     memset(buffer, 0, FILEBUFF);
     /* check for CVD file */
     fgets(buffer, 12, fd);
     rewind(fd);
 
     if(!strncmp(buffer, "ClamAV-VDB:", 11)) {
 
 	fseek(fd, 512, SEEK_SET);
 
 	tmpdir = getenv("TMPDIR");
 	if(tmpdir == NULL)
 #ifdef P_tmpdir
 	    tmpdir = P_tmpdir;
 #else
 	    tmpdir = "/tmp";
 #endif
 
f91f55e0
 	dir = cli_gentemp(tmpdir);
d5314812
 	if(mkdir(dir, 0700)) {
 	    mprintf("!listdb(): Can't create temporary directory %s\n", dir);
 	    free(buffer);
 	    fclose(fd);
 	    return -1;
 	}
 
 	/* FIXME: it seems there is some problem with current position indicator
 	* after gzdopen() call in cli_untgz(). Temporarily we need this wrapper:
 	*/
 
 	/* start */
 
f91f55e0
 	tmp = cli_gentemp(tmpdir);
d5314812
 	if((tmpd = fopen(tmp, "wb+")) == NULL) {
 	    mprintf("!listdb(): Can't create temporary file %s\n", tmp);
 	    free(dir);
 	    free(tmp);
 	    free(buffer);
 	    fclose(fd);
 	    return -1;
 	}
 
 	while((bytes = fread(buffer, 1, FILEBUFF, fd)) > 0)
 	    fwrite(buffer, 1, bytes, tmpd);
 
 	free(buffer);
 	fclose(fd);
 
 	fflush(tmpd);
 	fseek(tmpd, 0L, SEEK_SET);
 
 	if(cli_untgz(fileno(tmpd), dir)) {
 	    mprintf("!listdb(): Can't unpack CVD file.\n");
 	    cli_rmdirs(dir);
 	    free(dir);
25133aef
 	    fclose(tmpd);
d5314812
 	    unlink(tmp);
 	    free(tmp);
 	    free(buffer);
 	    return -1;
 	}
 
 	fclose(tmpd);
 	unlink(tmp);
 	free(tmp);
 
 	/* wrapper end */
 
 	/* list extracted directory */
 	listdir(dir);
 
 	cli_rmdirs(dir);
 	free(dir);
 
 	return 0;
     }
 
372063ec
     if(cli_strbcasestr(filename, ".db") || cli_strbcasestr(filename, ".db2")) {
 	/* old style database */
 
 	while(fgets(buffer, FILEBUFF, fd)) {
 	    line++;
 	    pt = strchr(buffer, '=');
 	    if(!pt) {
 		mprintf("!listdb(): Malformed pattern line %d (file %s).\n", line, filename);
 		fclose(fd);
 		free(buffer);
 		return -1;
 	    }
d5314812
 
372063ec
 	    start = buffer;
 	    *pt = 0;
d5314812
 
372063ec
 	    if((pt = strstr(start, " (Clam)")))
 		*pt = 0;
 
 	    mprintf("%s\n", start);
d5314812
 	}
 
856d9c84
     } else if(cli_strbcasestr(filename, ".hdb")) {
d5314812
 
372063ec
 	while(fgets(buffer, FILEBUFF, fd)) {
 	    line++;
 	    cli_chomp(buffer);
 	    start = cli_strtok(buffer, 2, ":");
 
 	    if(!start) {
 		mprintf("!listdb(): Malformed pattern line %d (file %s).\n", line, filename);
 		fclose(fd);
 		free(buffer);
 		return -1;
 	    }
 
 	    if((pt = strstr(start, " (Clam)")))
 		*pt = 0;
 
 	    mprintf("%s\n", start);
 	    free(start);
 	}
d5314812
 
92152740
     } else if(cli_strbcasestr(filename, ".ndb") || cli_strbcasestr(filename, ".zmd") || cli_strbcasestr(filename, ".rmd")) {
856d9c84
 
 	while(fgets(buffer, FILEBUFF, fd)) {
 	    line++;
 	    cli_chomp(buffer);
 	    start = cli_strtok(buffer, 0, ":");
 
 	    if(!start) {
 		mprintf("!listdb(): Malformed pattern line %d (file %s).\n", line, filename);
 		fclose(fd);
 		free(buffer);
 		return -1;
 	    }
 
 	    if((pt = strstr(start, " (Clam)")))
 		*pt = 0;
 
 	    mprintf("%s\n", start);
 	    free(start);
 	}
d5314812
     }
 
     fclose(fd);
     free(buffer);
     return 0;
 }
 
 int listdir(const char *dirname)
 {
 	DIR *dd;
 	struct dirent *dent;
 	char *dbfile;
 
 
     if((dd = opendir(dirname)) == NULL) {
         mprintf("!Can't open directory %s\n", dirname);
         return -1;
     }
 
     while((dent = readdir(dd))) {
618a038b
 #ifndef C_INTERIX
 	if(dent->d_ino)
 #endif
 	{
d5314812
 	    if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..") &&
 	    (cli_strbcasestr(dent->d_name, ".db")  ||
 	     cli_strbcasestr(dent->d_name, ".db2") ||
372063ec
 	     cli_strbcasestr(dent->d_name, ".hdb") ||
856d9c84
 	     cli_strbcasestr(dent->d_name, ".ndb") ||
eceef468
 	     cli_strbcasestr(dent->d_name, ".zmd") ||
92152740
 	     cli_strbcasestr(dent->d_name, ".rmd") ||
d5314812
 	     cli_strbcasestr(dent->d_name, ".cvd"))) {
 
 		dbfile = (char *) mcalloc(strlen(dent->d_name) + strlen(dirname) + 2, sizeof(char));
 
 		if(!dbfile) {
 		    mprintf("!listdir(): Can't allocate memory.\n");
 		    closedir(dd);
 		    return -1;
 		}
 		sprintf(dbfile, "%s/%s", dirname, dent->d_name);
 
 		if(listdb(dbfile)) {
 		    mprintf("!listdb(): error listing database %s\n", dbfile);
 		    free(dbfile);
 		    closedir(dd);
 		    return -1;
 		}
 		free(dbfile);
 	    }
 	}
     }
 
     closedir(dd);
     return 0;
 }
 
 void listsigs(struct optstruct *opt)
 {
 	int ret;
 	const char *name;
b2e70235
 	char *dbdir;
d5314812
 
     mprintf_stdout = 1;
 
b2e70235
     if((name = getargc(opt, 'l'))) {
d5314812
 	ret = listdb(name);
b2e70235
     } else {
 	dbdir = freshdbdir();
 	ret = listdir(dbdir);
 	free(dbdir);
     }
d5314812
 
50099661
     ret ? exit(1) : exit(0);
 }
 
 void help(void)
 {
     mprintf("\n");
8ef2ff6f
     mprintf("             Clam AntiVirus: Signature Tool (sigtool)  "VERSION"\n");
     mprintf("    (C) 2002 - 2005 ClamAV Team - http://www.clamav.net/team.html\n\n");
50099661
 
     mprintf("    --help                 -h              show help\n");
     mprintf("    --version              -V              print version number and exit\n");
     mprintf("    --quiet                                be quiet, output only error messages\n");
     mprintf("    --debug                                enable debug messages\n");
     mprintf("    --stdout                               write to stdout instead of stderr\n");
     mprintf("    --hex-dump                             convert data from stdin to a hex\n");
     mprintf("                                           string and print it on stdout\n");
25133aef
     mprintf("    --md5 [FILES]                          generate MD5 checksum from stdin\n");
     mprintf("                                           or MD5 sigs for FILES\n");
f852d214
     mprintf("    --html-normalise=FILE                  create normalised parts of HTML file\n");
50099661
     mprintf("    --info=FILE            -i FILE         print database information\n");
     mprintf("    --build=NAME           -b NAME         build a CVD file\n");
     mprintf("    --server=ADDR                          ClamAV Signing Service address\n");
     mprintf("    --unpack=FILE          -u FILE         Unpack a CVD file\n");
     mprintf("    --unpack-current=NAME                  Unpack local CVD\n");
     mprintf("    --list-sigs[=FILE]     -l[FILE]        List signature names\n");
d6bf11e3
     mprintf("    --vba=FILE                             Extract VBA/Word6 macro code\n");
     mprintf("    --vba-hex=FILE                         Extract Word6 macro code with hex values\n");
50099661
     mprintf("\n");
 
     exit(0);
d5314812
 }