b151ef55 |
/* |
8b242bb9 |
* Copyright (C) 2002 - 2004 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.
*/
/* some things may need to be tuned here (look at jmp variables) */
|
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>
#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" |
b151ef55 |
#define LINE 1024 |
13847a2d |
#define MIN_LENGTH 15
#define MAX_LENGTH 200
|
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);
int cli_rmdirs(const char *dirname); /* libclamav's internal */ |
b151ef55 |
int scanfile(const char *cmd, const char *str, const char *file)
{
FILE *pd;
char *command, buffer[LINE];
/* build the command */
command = (char *) mcalloc(strlen(cmd) + strlen(file) + 10, sizeof(char));
sprintf(command, "%s %s", cmd, file);
if((pd = popen(command, "r")) == NULL) {
mprintf("!popen() failed\n");
return 3;
}
while(fgets(buffer, LINE, pd)) {
if(strstr(buffer, str)) {
free(command); |
cf0744c5 |
fclose(pd); |
b151ef55 |
return 1; /* found */
}
}
free(command);
fclose(pd);
return 0; /* substring not found */
}
char *cut(const char *file, long int start, long int end)
{ |
7b7b3ca5 |
char *fname = NULL, buffer[FILEBUFF]; |
b151ef55 |
int bytes, size, sum;
FILE *rd, *wd;
if((rd = fopen(file, "rb")) == NULL) {
mprintf("!File %s doesn't exist.\n", file);
exit(13);
}
|
13847a2d |
if((fname = cl_gentemp(".")) == NULL) {
mprintf("!Can't generate temporary file name.\n");
exit(1);
}
|
b151ef55 |
if((wd = fopen(fname, "wb")) == NULL) {
mprintf("!Can't create temporary file %s\n", fname);
exit(14);
}
fseek(rd, start, SEEK_SET);
size = end - start;
sum = 0;
|
7b7b3ca5 |
while((bytes = fread(buffer, 1, FILEBUFF, rd)) > 0) { |
b151ef55 |
if(sum + bytes >= size) {
fwrite(buffer, 1, size - sum, wd);
break;
} else
fwrite(buffer, 1, bytes, wd);
sum += bytes;
}
fclose(rd);
fclose(wd); |
13847a2d |
|
b151ef55 |
return fname;
}
char *change(const char *file, long int x)
{ |
7b7b3ca5 |
char *fname = NULL, buffer[FILEBUFF]; |
e8217f5a |
int bytes, ch; |
b151ef55 |
FILE *rd, *wd;
if((rd = fopen(file, "rb")) == NULL) {
mprintf("!File %s doesn't exist.\n", file);
exit(13);
}
|
13847a2d |
if((fname = cl_gentemp(".")) == NULL) {
mprintf("!Can't generate temporary file name.\n");
exit(1);
}
|
b151ef55 |
if((wd = fopen(fname, "wb+")) == NULL) {
mprintf("!Can't create temporary file %s\n", fname);
exit(14);
}
|
7b7b3ca5 |
while((bytes = fread(buffer, 1, FILEBUFF, rd)) > 0) |
b151ef55 |
fwrite(buffer, 1, bytes, wd);
fclose(rd);
|
13847a2d |
if(x) { /* don't alter first character in the file */
fflush(wd);
fseek(wd, x, SEEK_SET);
ch = fgetc(wd);
fseek(wd, -1, SEEK_CUR);
fputc(++ch, wd);
}
|
b151ef55 |
fclose(wd);
return fname;
}
void sigtool(struct optstruct *opt)
{ |
7b7b3ca5 |
char buffer[FILEBUFF]; |
b151ef55 |
int bytes;
char *pt;
if(optl(opt, "quiet")) mprintf_quiet = 1;
else mprintf_quiet = 0;
if(optl(opt, "stdout")) mprintf_stdout = 1;
else mprintf_stdout = 0;
|
c0eb3ceb |
if(optl(opt, "debug"))
cl_debug();
|
b151ef55 |
if(optc(opt, 'V')) {
mprintf("sigtool / ClamAV version "VERSION"\n");
exit(0);
}
if(optc(opt, 'h')) {
free_opt(opt);
help();
}
if(optl(opt, "hex-dump")) {
|
7b7b3ca5 |
while((bytes = read(0, buffer, FILEBUFF)) > 0) { |
b151ef55 |
pt = cl_str2hex(buffer, bytes);
write(1, pt, 2 * bytes);
free(pt);
}
|
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);
|
b151ef55 |
} else { |
d5314812 |
int jmp, lastjmp = 0, end, found = 0, exec = 0, pos, filesize, |
13847a2d |
maxsize = 0, ret; |
d5314812 |
char *c, *s, *f, *tmp, *signame, *bsigname, *f2 = NULL; |
b151ef55 |
FILE *fd, *wd;
if(!optc(opt, 'c')) {
mprintf("!--command, -c is required in this mode\n");
exit(10);
} else if(!optc(opt, 's')) {
mprintf("!--string, -s is required in this mode\n");
exit(10);
} else if(!optc(opt, 'f')) {
mprintf("!--file, -f is required in this mode\n");
exit(10);
}
/* these are pointers to corresponding strings in option list */
c = getargc(opt, 'c');
s = getargc(opt, 's');
f = getargc(opt, 'f');
if(scanfile(c, s, f) != 1) {
mprintf("!String %s not found in scanner's output.\n", s);
mprintf("Please check it and try again.\n");
mprintf("Does the scanner write to stdout ? It has to.\n");
exit(11);
}
/* initial values */
filesize = end = fileinfo(f, 1);
jmp = end / 5 + 1;
|
13847a2d |
/* find signature end */ |
b151ef55 |
while(1) {
tmp = cut(f, 0, end);
exec++; |
13847a2d |
ret = scanfile(c, s, tmp);
unlink(tmp);
free(tmp);
if(ret == 1) {
if(end >= jmp) {
mprintf("Detected, decreasing end %d -> %d\n", end, end - jmp);
end -= jmp;
} else
end = 0;
|
b151ef55 |
} else {
mprintf("Not detected at %d, moving forward.\n", end);
if(jmp == 1) { |
13847a2d |
|
c6259ac5 |
while(end <= filesize) { |
b151ef55 |
tmp = cut(f, 0, end);
exec++;
if(scanfile(c, s, tmp) == 1) { |
13847a2d |
mprintf(" *** Signature end found at %d\n", end); |
b151ef55 |
found = 1; |
13847a2d |
f2 = strdup(tmp); /* remember this file */ |
b151ef55 |
free(tmp);
break;
} else {
unlink(tmp);
free(tmp);
mprintf("Increasing end %d -> %d\n", end, end + 1);
}
end++;
} |
c6259ac5 |
|
b151ef55 |
if(found) break;
} |
13847a2d |
|
b151ef55 |
if(jmp)
jmp--;
jmp = jmp/2 + 1;
end += jmp;
if(end > filesize)
end = filesize;
}
}
|
42e6f5a6 |
/* find signature start */ |
c6259ac5 |
found = 0; |
b151ef55 |
jmp = 50;
pos = end - jmp;
while(1) {
tmp = change(f2, pos);
if(scanfile(c, s, tmp) != 1) { |
13847a2d |
exec++; |
b151ef55 |
unlink(tmp);
free(tmp);
|
13847a2d |
if(pos >= jmp) {
mprintf("Not detected, moving backward %d -> %d\n", pos, pos - jmp);
pos -= jmp;
maxsize += jmp;
} else {
mprintf("Not detected, using the beginning of the file.\n");
pos = 0;
break;
}
if(maxsize > MAX_LENGTH) {
mprintf("!Generated signature is too big.\n");
unlink(f2);
free(f2);
exit(1);
}
|
b151ef55 |
} else {
mprintf("Detected at %d, moving forward.\n", pos); |
c6259ac5 |
if(jmp == 1 && lastjmp == 1) { |
b151ef55 |
unlink(tmp);
free(tmp);
while(pos < end) {
tmp = change(f2, pos);
exec++; |
13847a2d |
ret = scanfile(c, s, tmp);
unlink(tmp);
free(tmp);
if(ret == 1) { |
c6259ac5 |
mprintf("Moving forward %d -> %d\n", pos, pos + 1);
pos++; |
13847a2d |
if(end - pos < MIN_LENGTH) {
mprintf("!Generated signature is too small.\n");
unlink(f2);
free(f2);
exit(1);
}
|
b151ef55 |
} else { |
6ab9efc7 |
mprintf(" *** Signature start found at %d\n", pos); |
c6259ac5 |
found = 1;
break; |
b151ef55 |
}
} |
13847a2d |
if(pos >= end) {
mprintf("!Can't generate a proper signature.\n");
unlink(f2);
free(f2);
exit(1);
}
if(found)
break; |
b151ef55 |
} |
c6259ac5 |
lastjmp = jmp; |
13847a2d |
if(jmp > 0) |
b151ef55 |
jmp--; |
13847a2d |
jmp = jmp/2 + 1; |
b151ef55 |
pos += jmp;
|
13847a2d |
if(pos >= end - 2 * jmp) |
b151ef55 |
pos = end - 2 * jmp;
unlink(tmp);
free(tmp);
}
}
unlink(f2);
free(f2);
tmp = cut(f, pos, end);
mprintf("\nThe scanner was executed %d times.\n", exec); |
42e6f5a6 |
mprintf("The signature length is %d (%d hex)\n", end - pos, 2 * (end - pos)); |
b151ef55 |
|
13847a2d |
if(end - pos < MIN_LENGTH) {
mprintf("\nWARNING: THE SIGNATURE IS TOO SMALL (PROBABLY ONLY A PART OF A REAL SIGNATURE).\n"); |
b151ef55 |
mprintf(" PLEASE DON'T USE IT.\n\n");
}
if((fd = fopen(tmp, "rb")) == NULL) {
mprintf("!Can't believe. Where is my signature, dude ?\n");
exit(99);
}
signame = (char *) mcalloc(strlen(f) + 10, sizeof(char));
sprintf(signame, "%s.sig", f);
if(fileinfo(signame, 1) != -1) {
mprintf("File %s exists.\n", signame);
free(signame); |
4cd4319e |
signame = cl_gentemp("."); |
b151ef55 |
}
bsigname = (char *) mcalloc(strlen(f) + 10, sizeof(char));
sprintf(bsigname, "%s.bsig", f);
if(fileinfo(bsigname, 1) != -1) {
mprintf("File %s exists.\n", bsigname);
free(bsigname); |
4cd4319e |
bsigname = cl_gentemp("."); |
b151ef55 |
}
if((wd = fopen(signame, "wb")) == NULL) {
mprintf("Can't write to %s\n", signame);
unlink(tmp);
free(tmp);
exit(15);
}
mprintf("Saving signature in %s file.\n", signame);
|
7b7b3ca5 |
while((bytes = fread(buffer, 1, FILEBUFF, fd)) > 0) { |
b151ef55 |
pt = cl_str2hex(buffer, bytes);
fwrite(pt, 1, 2 * bytes, wd);
free(pt);
}
mprintf("Saving binary signature in %s file.\n", bsigname);
rename(tmp, bsigname);
fclose(fd);
fclose(wd);
free(tmp);
free(signame);
free(bsigname);
}
|
13847a2d |
/* free_opt(opt); */ |
b151ef55 |
}
|
442d8407 |
int countlines(const char *filename)
{
FILE *fd;
char buff[65536];
int lines = 0;
if((fd = fopen(filename, "r")) == NULL)
return 0;
while(fgets(buff, sizeof(buff), fd))
lines++;
fclose(fd);
return lines;
}
|
4cd4319e |
int build(struct optstruct *opt)
{ |
42e6f5a6 |
int ret, no = 0, realno = 0, bytes, itmp; |
4cd4319e |
struct stat foo; |
7cc3891c |
char buffer[FILEBUFF], *tarfile = NULL, *gzfile = NULL, header[257], |
6765b5df |
smbuff[30], *pt; |
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 |
/* build a tar.gz archive |
13847a2d |
* we need: COPYING and {viruses.db, viruses.db2}+ |
4cd4319e |
* in current working directory
*/
if(stat("COPYING", &foo) == -1) {
mprintf("COPYING file not found in current working directory.\n");
exit(1);
}
|
c0eb3ceb |
if(stat("viruses.db", &foo) == -1 && stat("viruses.db2", &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 |
}
cl_freetrie(root);
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); |
42e6f5a6 |
realno = countlines("viruses.db") + countlines("viruses.db2");
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 |
tarfile = cl_gentemp(".");
switch(fork()) {
case -1:
mprintf("!Can't fork.\n");
exit(1);
case 0:
{ |
42e6f5a6 |
char *args[] = { "tar", "-cvf", tarfile, "COPYING", "viruses.db", "viruses.db2", "Notes", NULL }; |
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);
}
gzfile = cl_gentemp(".");
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 */
sprintf(buffer, "%s/%s", cl_retdbdir(), getargc(opt, 'b'));
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 |
|
13847a2d |
/* functionality level (TODO: use cl_funclevel()) */ |
4cd4319e |
sprintf(smbuff, "%d:", 1); |
7cc3891c |
strcat(header, smbuff); |
4cd4319e |
/* MD5 */
pt = cl_md5file(gzfile); |
7cc3891c |
strcat(header, pt); |
4bf2cf8d |
free(pt); |
7cc3891c |
strcat(header, ":"); |
4cd4319e |
|
4bf2cf8d |
/* builder - question */
fflush(stdin);
mprintf("Builder id: "); |
d5314812 |
fscanf(stdin, "%s", smbuff); |
4bf2cf8d |
|
4cd4319e |
/* digital signature */ |
4bf2cf8d |
fd = fopen(gzfile, "rb");
__md5_stream(fd, &buffer);
fclose(fd); |
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 |
|
4bf2cf8d |
/* builder - add */ |
7cc3891c |
strcat(header, smbuff); |
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)
{
FILE *fd; |
e1ef4066 |
char *name; |
42e6f5a6 |
|
e1ef4066 |
if(optl(opt, "unpack-current")) {
name = mcalloc(300, sizeof(char)); /* FIXME */
sprintf(name, "%s/%s", cl_retdbdir(), getargl(opt, "unpack-current"));
} else
name = getargc(opt, 'u'); |
42e6f5a6 |
|
e1ef4066 |
if((fd = fopen(name, "rb")) == NULL) {
mprintf("!Can't open CVD file %s\n", name); |
42e6f5a6 |
exit(1);
}
fseek(fd, 512L, SEEK_SET);
if(cli_untgz(fileno(fd), ".")) {
mprintf("!Can't unpack file.\n");
fclose(fd);
exit(1);
}
fclose(fd);
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");
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
dir = cl_gentemp(tmpdir);
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 */
tmp = cl_gentemp(tmpdir);
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);
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;
}
/* 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;
}
start = buffer;
*pt = 0;
if((pt = strstr(start, " (Clam)")))
*pt = 0;
mprintf("%s\n", start);
}
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))) {
if(dent->d_ino) {
if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..") &&
(cli_strbcasestr(dent->d_name, ".db") ||
cli_strbcasestr(dent->d_name, ".db2") ||
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;
mprintf_stdout = 1;
|
50099661 |
if((name = getargc(opt, 'l'))) |
d5314812 |
ret = listdb(name);
else
ret = listdir(cl_retdbdir());
|
50099661 |
ret ? exit(1) : exit(0);
}
void help(void)
{
mprintf("\n");
mprintf(" Clam AntiVirus: Signature Tool (sigtool) "VERSION"\n");
mprintf(" (C) 2002 - 2004 Tomasz Kojm <tkojm@clamav.net>\n\n");
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(" (this help is always written to stdout)\n");
mprintf(" --hex-dump convert data from stdin to a hex\n");
mprintf(" string and print it on stdout\n");
mprintf(" --command -c scanner command string, with options\n");
mprintf(" --string -s 'virus found' string in scan. output\n");
mprintf(" --file -f infected file\n");
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");
mprintf("\n");
exit(0); |
d5314812 |
} |