clamav-devel/sigtool/sigtool.c
e3aaff8e
 /*
  *  Copyright (C) 2002, 2003 Tomasz Kojm <zolw@konarski.edu.pl>
  *
  *  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) */
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
8139fd99
 #include <zlib.h>
 #include <time.h>
 #include <locale.h>
ae307914
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
e3aaff8e
 #include <clamav.h>
 
 #include "options.h"
 #include "others.h"
 #include "shared.h"
 #include "strings.h"
 
 #define LINE 1024
d71dd823
 
 #define MIN_LENGTH 15
 #define MAX_LENGTH 200
 
e3aaff8e
 void help(void);
ae307914
 char *getdsig(const char *host, const char *user, const char *data);
 void cvdinfo(struct optstruct *opt);
e3aaff8e
 
 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);
 	    return 1; /* found */
 	}
     }
 
     free(command);
     fclose(pd);
     return 0; /* substring not found */
 }
 
 char *cut(const char *file, long int start, long int end)
 {
f7148839
 	char *fname = NULL, buffer[FILEBUFF];
e3aaff8e
 	int bytes, size, sum;
 	FILE *rd, *wd;
 
 
     if((rd = fopen(file, "rb")) == NULL) {
 	mprintf("!File %s doesn't exist.\n", file);
 	exit(13);
     }
 
d71dd823
     if((fname = cl_gentemp(".")) == NULL) {
 	mprintf("!Can't generate temporary file name.\n");
 	exit(1);
     }
 
e3aaff8e
     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;
 
f7148839
     while((bytes = fread(buffer, 1, FILEBUFF, rd)) > 0) {
e3aaff8e
 	if(sum + bytes >= size) {
 	    fwrite(buffer, 1, size - sum, wd);
 	    break;
 	} else
 	    fwrite(buffer, 1, bytes, wd);
 
 	sum += bytes;
     }
 
     fclose(rd);
     fclose(wd);
d71dd823
 
e3aaff8e
     return fname;
 }
 
 char *change(const char *file, long int x)
 {
f7148839
 	char *fname = NULL, buffer[FILEBUFF];
e3aaff8e
 	int bytes, size, sum, ch;
 	FILE *rd, *wd;
 
 
     if((rd = fopen(file, "rb")) == NULL) {
 	mprintf("!File %s doesn't exist.\n", file);
 	exit(13);
     }
 
d71dd823
     if((fname = cl_gentemp(".")) == NULL) {
 	mprintf("!Can't generate temporary file name.\n");
 	exit(1);
     }
 
e3aaff8e
     if((wd = fopen(fname, "wb+")) == NULL) {
 	mprintf("!Can't create temporary file %s\n", fname);
 	exit(14);
     }
 
f7148839
     while((bytes = fread(buffer, 1, FILEBUFF, rd)) > 0)
e3aaff8e
 	fwrite(buffer, 1, bytes, wd);
 
     fclose(rd);
 
d71dd823
     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);
     }
 
e3aaff8e
     fclose(wd);
     return fname;
 }
 
 void sigtool(struct optstruct *opt)
 {
f7148839
 	    char buffer[FILEBUFF];
e3aaff8e
 	    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;
 
6a2532ca
     if(optl(opt, "debug"))
 	cl_debug();
 
e3aaff8e
     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")) {
 
f7148839
 	while((bytes = read(0, buffer, FILEBUFF)) > 0) {
e3aaff8e
 	    pt = cl_str2hex(buffer, bytes);
 	    write(1, pt, 2 * bytes);
 	    free(pt);
 	}
 
8139fd99
     } else if(optc(opt, 'b')) {
ae307914
 	if(!optc(opt, 's')) {
 	    mprintf("!--server, -s is required in this mode\n");
 	    exit(10);
 	}
8139fd99
 
 	build(opt);
 
0f387b1b
     } else if(optc(opt, 'u')) {
 
 	unpack(opt);
 
55216b6e
     } else if(optl(opt, "unpack-current")) {
 
 	unpack(opt);
 
8139fd99
     } else if(optc(opt, 'i')) {
 
 	cvdinfo(opt);
 
e3aaff8e
     } else {
d71dd823
 	    int jmp, lastjmp, start, end, found = 0, exec = 0, pos, filesize,
 		maxsize = 0, ret;
e3aaff8e
 	    char *c, *s, *f, *tmp, *signame, *bsigname, *f2;
 	    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;
 
d71dd823
 	/* find signature end */
e3aaff8e
 	while(1) {
 	    tmp = cut(f, 0, end);
 	    exec++;
d71dd823
 	    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;
 
e3aaff8e
 	    } else {
 		mprintf("Not detected at %d, moving forward.\n", end);
 		if(jmp == 1) {
d71dd823
 
049a18b9
 		    while(end <= filesize) {
e3aaff8e
 			tmp = cut(f, 0, end);
 			exec++;
 			if(scanfile(c, s, tmp) == 1) {
d71dd823
 			    mprintf(" *** Signature end found at %d\n", end);
e3aaff8e
 			    found = 1;
d71dd823
 			    f2 = strdup(tmp); /* remember this file */
e3aaff8e
 			    free(tmp);
 			    break;
 			} else {
 			    unlink(tmp);
 			    free(tmp);
 			    mprintf("Increasing end %d -> %d\n", end, end + 1);
 			}
 			end++;
 		    }
049a18b9
 
e3aaff8e
 		    if(found) break;
 		}
d71dd823
 
e3aaff8e
 		if(jmp)
 		    jmp--;
 		jmp = jmp/2 + 1;
 		end += jmp;
 		if(end > filesize)
 		    end = filesize;
 
 	    }
 
 	}
 
0f387b1b
 	/* find signature start */
049a18b9
 	found = 0;
e3aaff8e
 	jmp = 50;
 	pos = end - jmp;
 
 	while(1) {
 
 	    tmp = change(f2, pos);
 	    if(scanfile(c, s, tmp) != 1) {
d71dd823
 		exec++;
e3aaff8e
 		unlink(tmp);
 		free(tmp);
 
d71dd823
 		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);
 		}
 
e3aaff8e
 	    } else {
 		mprintf("Detected at %d, moving forward.\n", pos);
049a18b9
 		if(jmp == 1 && lastjmp == 1) {
e3aaff8e
 		    unlink(tmp);
 		    free(tmp);
 		    while(pos < end) {
 			tmp = change(f2, pos);
 			exec++;
d71dd823
 			ret = scanfile(c, s, tmp);
 			unlink(tmp);
 			free(tmp);
 			if(ret == 1) {
049a18b9
 			    mprintf("Moving forward %d -> %d\n", pos, pos + 1);
 			    pos++;
d71dd823
 
 			    if(end - pos < MIN_LENGTH) {
 				mprintf("!Generated signature is too small.\n");
 				unlink(f2);
 				free(f2);
 				exit(1);
 			    }
 
e3aaff8e
 			} else {
26c51827
 			    mprintf(" *** Signature start found at %d\n", pos);
049a18b9
 			    found = 1;
 			    break;
e3aaff8e
 			}
 		    }
d71dd823
 
 		    if(pos >= end) {
 		        mprintf("!Can't generate a proper signature.\n");
 			unlink(f2);
 			free(f2);
 		        exit(1);
 		    }
 
 		    if(found)
 			break;
e3aaff8e
 		}
049a18b9
 
 		lastjmp = jmp;
d71dd823
 		if(jmp > 0)
e3aaff8e
 		    jmp--;
d71dd823
 		jmp = jmp/2 + 1;
e3aaff8e
 		pos += jmp;
 
d71dd823
 		if(pos >= end - 2 * jmp)
e3aaff8e
 		    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);
0f387b1b
 	mprintf("The signature length is %d (%d hex)\n", end - pos, 2 * (end - pos));
e3aaff8e
 
d71dd823
 	if(end - pos < MIN_LENGTH) {
 	    mprintf("\nWARNING: THE SIGNATURE IS TOO SMALL (PROBABLY ONLY A PART OF A REAL SIGNATURE).\n");
e3aaff8e
 	    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);
8139fd99
 	    signame = cl_gentemp(".");
e3aaff8e
 	}
 
 	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);
8139fd99
 	    bsigname = cl_gentemp(".");
e3aaff8e
 	}
 
 	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);
 
f7148839
 	while((bytes = fread(buffer, 1, FILEBUFF, fd)) > 0) {
e3aaff8e
 	    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);
     }
 
d71dd823
     /* free_opt(opt); */
e3aaff8e
 }
 
e4ae7726
 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;
 }
 
8139fd99
 int build(struct optstruct *opt)
 {
0f387b1b
 	int ret, no = 0, realno = 0, bytes, itmp;
8139fd99
 	struct stat foo;
f7148839
 	char buffer[FILEBUFF], *tarfile = NULL, *gzfile = NULL, header[257],
8139fd99
 	     smbuff[25], *pt;
         struct cl_node *root = NULL;
ae307914
 	FILE *tar, *cvd, *fd;
8139fd99
 	gzFile *gz;
 	time_t timet;
 	struct tm *brokent;
0f387b1b
 	struct cl_cvd *oldcvd = NULL;
8139fd99
 
     /* build a tar.gz archive
d71dd823
      * we need: COPYING and {viruses.db, viruses.db2}+
8139fd99
      * in current working directory
      */
 
     if(stat("COPYING", &foo) == -1) {
 	mprintf("COPYING file not found in current working directory.\n");
 	exit(1);
     }
 
6a2532ca
     if(stat("viruses.db", &foo) == -1 && stat("viruses.db2", &foo) == -1) {
8139fd99
 	mprintf("Virus database not found in current working directory.\n");
 	exit(1);
     }
 
     cl_debug(); /* enable debug messages */
 
e4ae7726
     if((ret = cl_loaddbdir(".", &root, &no))) {
8139fd99
 	mprintf("!Can't load database: %s\n", cl_strerror(ret));
0f387b1b
 	exit(1);
8139fd99
     }
 
     cl_freetrie(root);
 
     mprintf("Database properly parsed.\n");
 
0f387b1b
     if(!no) {
8139fd99
 	mprintf("WARNING: There are no signatures in the database(s).\n");
0f387b1b
     } else {
8139fd99
 	mprintf("Signatures: %d\n", no);
0f387b1b
 	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);
 	}
     }
8139fd99
 
     tarfile = cl_gentemp(".");
 
     switch(fork()) {
 	case -1:
 	    mprintf("!Can't fork.\n");
 	    exit(1);
 	case 0:
 	    {
0f387b1b
 		char *args[] = { "tar", "-cvf", tarfile, "COPYING", "viruses.db", "viruses.db2", "Notes", NULL };
8139fd99
 		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);
     }
 
f7148839
     while((bytes = fread(buffer, 1, FILEBUFF, tar)) > 0)
8139fd99
 	gzwrite(gz, buffer, bytes);
 
     fclose(tar);
     unlink(tarfile);
     free(tarfile);
 
     gzclose(gz);
 
0f387b1b
 
     /* 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);
 
8139fd99
     /* generate header */
 
     /* magic string */
 
     strcpy(header, "ClamAV-VDB:");
 
     /* time */
 
     time(&timet);
     brokent = localtime(&timet);
     setlocale(LC_TIME, "C");
5def21ff
     strftime(smbuff, 24, "%d %b %Y %H-%M %z:", brokent);
8139fd99
     strcat(header, smbuff);
 
     /* version number */
 
d71dd823
     /* ... increment version number by one */
8139fd99
 
0f387b1b
     if(oldcvd) {
 	sprintf(smbuff, "%d:", oldcvd->version + 1);
     } else {
 	fflush(stdin);
 	mprintf("Version number: ");
 	scanf("%d", &itmp);
 	sprintf(smbuff, "%d:", itmp);
     }
8139fd99
     strcat(header, smbuff);
 
     /* number of signatures */
     sprintf(smbuff, "%d:", no);
     strcat(header, smbuff);
 
d71dd823
     /* functionality level (TODO: use cl_funclevel()) */
8139fd99
     sprintf(smbuff, "%d:", 1);
     strcat(header, smbuff);
 
     /* MD5 */
     pt = cl_md5file(gzfile);
     strcat(header, pt);
ae307914
     free(pt);
8139fd99
     strcat(header, ":");
 
ae307914
     /* builder - question */
     fflush(stdin);
     mprintf("Builder id: ");
     fscanf(stdin, "%s", &smbuff);
 
8139fd99
     /* digital signature */
ae307914
     fd = fopen(gzfile, "rb");
     __md5_stream(fd, &buffer);
     fclose(fd);
     if(!(pt = getdsig(getargc(opt, 's'), smbuff, buffer))) {
 	mprintf("No digital signature - no CVD file...\n");
0f387b1b
 	unlink(gzfile);
ae307914
 	exit(1);
     }
 
     strcat(header, pt);
     free(pt);
8139fd99
     strcat(header, ":");
 
ae307914
     /* builder - add */
8139fd99
     strcat(header, smbuff);
 
     /* fill up with spaces */
6a2532ca
     if(strlen(header) > 512) {
0f387b1b
 	mprintf("!Generated header is too long.\n");
 	unlink(gzfile);
8139fd99
 	exit(1);
     }
 
6a2532ca
     while(strlen(header) < 512)
8139fd99
 	strcat(header, " ");
 
     /* build the final database */
 
     pt = getargc(opt, 'b');
     if((cvd = fopen(pt, "wb")) == NULL) {
 	mprintf("!Can't write the final database %s\n", pt);
0f387b1b
 	unlink(gzfile);
8139fd99
 	exit(1);
     }
 
6a2532ca
     fwrite(header, 1, 512, cvd);
8139fd99
 
     if((tar = fopen(gzfile, "rb")) == NULL) {
 	mprintf("!Can't open file %s for reading.\n", gzfile);
 	exit(1);
     }
 
f7148839
     while((bytes = fread(buffer, 1, FILEBUFF, tar)) > 0)
8139fd99
 	fwrite(buffer, 1, bytes, cvd);
 
     fclose(tar);
     fclose(cvd);
 
     unlink(gzfile);
     free(gzfile);
 
     mprintf("Database %s created.\n", pt);
 
d71dd823
     /* try to load final cvd */
8139fd99
 }
 
 void cvdinfo(struct optstruct *opt)
 {
 	struct cl_cvd *cvd;
 	char *pt;
6a2532ca
 	int ret;
8139fd99
 
6a2532ca
     pt = getargc(opt, 'i');
     if((cvd = cl_cvdhead(pt)) == NULL) {
 	mprintf("!Can't read CVD header from %s\n", pt);
8139fd99
 	exit(1);
     }
 
55216b6e
     mprintf("Build time: %s\n", cvd->time);
8139fd99
     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);
 
6a2532ca
 #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");
 
d71dd823
     /* free */
8139fd99
 }
 
e3aaff8e
 void help(void)
 {
     mprintf("\n");
5def21ff
     mprintf("                Clam AntiVirus: Signature Tool (sigtool)  "VERSION"\n");
     mprintf("                (c) 2002, 2003 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");
0f387b1b
     mprintf("\n");
6a2532ca
 
e3aaff8e
     exit(0);
 }
ae307914
 
 char *getdsig(const char *host, const char *user, const char *data)
 {
 	char buff[300], cmd[100], *pass, *pt;
         struct sockaddr_in server;
 	struct cfgstruct *copt, *cpt;
 	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));
     if((bread = read(sockd, buff, sizeof(buff))) > 0)
 	if(!strstr(buff, "Signature:")) {
 	    mprintf("!Signature generation error.\n");
 	    mprintf("ClamAV SDaemon: %s.\n", buff);
 	    close(sockd);
 	    return NULL;
 	} else
 	    mprintf("Signature received (length = %d).\n", strlen(buff) - 10);
 
     close(sockd);
     pt = buff;
     pt += 10;
     return strdup(pt);
 }
0f387b1b
 
 int unpack(struct optstruct *opt)
 {
 	FILE *fd;
 	struct cl_cvd *cvd;
55216b6e
 	char *name;
0f387b1b
 
55216b6e
     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');
0f387b1b
 
55216b6e
     if((fd = fopen(name, "rb")) == NULL) {
 	mprintf("!Can't open CVD file %s\n", name);
0f387b1b
 	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);
 }