clamdscan: support multiple arguments on command line

git-svn: trunk@627
remotes/push_mirror/metadata
Tomasz Kojm 22 years ago
parent 8f0f9d56b4
commit fe6c6a020a
  1. 5
      clamav-devel/ChangeLog
  2. 13
      clamav-devel/clamdscan/clamdscan.c
  3. 352
      clamav-devel/clamdscan/client.c
  4. 2
      clamav-devel/clamdscan/client.h

@ -1,3 +1,8 @@
Tue Jun 22 18:47:32 CEST 2004 (tk)
----------------------------------
* clamdscan: support multiple arguments on command line (requested by
Dan Egli <dan*eglifamily.dnsalias.net>); major cleanup
Tue Jun 22 11:58:06 BST 2004 (njh/trog)
---------------------------------------
* libclamav/str.c: Rewrote cli_chomp() as discussed in the clamav-devel

@ -26,6 +26,7 @@
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <signal.h>
#include "options.h"
#include "others.h"
@ -36,12 +37,11 @@
void help(void);
struct s_info claminfo;
short printinfected = 0;
void clamscan(struct optstruct *opt)
{
int ds, dms, ret;
int ds, dms, ret, infected;
struct timeval t1, t2;
struct timezone tz;
time_t starttime;
@ -89,10 +89,9 @@ void clamscan(struct optstruct *opt)
/* ctime() does \n, but I need it once more */
logg("Scan started: %s\n", ctime(&starttime));
memset(&claminfo, 0, sizeof(struct s_info));
gettimeofday(&t1, &tz);
ret = client(opt);
ret = client(opt, &infected);
/* Implement STATUS in clamd */
if(!optl(opt, "disable-summary")) {
@ -103,8 +102,8 @@ void clamscan(struct optstruct *opt)
dms += (dms < 0) ? (1000000):(0);
mprintf("\n----------- SCAN SUMMARY -----------\n");
logg("\n-- summary --\n");
mprintf("Infected files: %d\n", claminfo.ifiles);
logg("Infected files: %d\n", claminfo.ifiles);
mprintf("Infected files: %d\n", infected);
logg("Infected files: %d\n", infected);
mprintf("Time: %d.%3.3d sec (%d m %d s)\n", ds, dms/1000, ds/60, ds%60);
logg("Time: %d.%3.3d sec (%d m %d s)\n", ds, dms/1000, ds/60, ds%60);
}

@ -38,6 +38,7 @@
#include "cfgparser.h"
#include "memory.h"
#include "output.h"
#include "str.h"
#ifdef PF_INET
# define SOCKET_INET PF_INET
@ -45,36 +46,171 @@
# define SOCKET_INET AF_INET
#endif
int client(const struct optstruct *opt)
int dsfile(int sockd, const char *filename)
{
int infected = 0, waserror = 0;
char buff[4096], *scancmd;
FILE *fd;
scancmd = mcalloc(strlen(filename) + 20, sizeof(char));
sprintf(scancmd, "CONTSCAN %s", filename);
if(write(sockd, scancmd, strlen(scancmd)) <= 0) {
mprintf("@Can't write to the socket.\n");
free(scancmd);
return -1;
}
free(scancmd);
if((fd = fdopen(dup(sockd), "r")) == NULL) {
mprintf("@Can't open descriptor for reading.\n");
return -1;
}
while(fgets(buff, sizeof(buff), fd)) {
if(strstr(buff, "FOUND\n")) {
infected++;
logg("%s", buff);
mprintf("%s", buff);
}
if (strstr(buff, "ERROR\n")) {
logg("%s", buff);
mprintf("%s", buff);
waserror = 1;
}
}
fclose(fd);
if(!infected)
mprintf("%s: OK\n", filename);
return infected ? infected : (waserror ? -1 : 0);
}
int dsstream(int sockd)
{
int wsockd, loopw = 60, bread, port, infected = 0;
struct sockaddr_in server;
char buff[4096], *pt;
if(write(sockd, "STREAM", 6) <= 0) {
mprintf("@Can't write to the socket.\n");
return 2;
}
memset(buff, 0, sizeof(buff));
while(loopw) {
read(sockd, buff, sizeof(buff));
if((pt = strstr(buff, "PORT"))) {
pt += 5;
sscanf(pt, "%d", &port);
break;
}
loopw--;
}
if(!loopw) {
mprintf("@Daemon not ready for stream scanning.\n");
return -1;
}
/* connect to clamd */
if((wsockd = socket(SOCKET_INET, SOCK_STREAM, 0)) < 0) {
perror("socket()");
mprintf("@Can't create the socket.\n");
return -1;
}
server.sin_family = AF_INET;
server.sin_port = htons(port);
if(connect(wsockd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0) {
close(wsockd);
perror("connect()");
mprintf("@Can't connect to clamd [port: %d].\n", port);
return -1;
}
while((bread = read(0, buff, sizeof(buff))) > 0) {
if(write(wsockd, buff, bread) <= 0) {
mprintf("@Can't write to the socket.\n");
close(wsockd);
return -1;
}
}
close(wsockd);
memset(buff, 0, sizeof(buff));
while((bread = read(sockd, buff, sizeof(buff))) > 0) {
mprintf("%s", buff);
if(strstr(buff, "FOUND\n")) {
infected++;
logg("%s", buff);
}
if(strstr(buff, "ERROR\n")) {
logg("%s", buff);
return -1;
}
memset(buff, 0, sizeof(buff));
}
return infected;
}
char *abpath(const char *filename)
{
struct stat foo;
char *fullpath, cwd[200];
if(stat(filename, &foo) == -1) {
mprintf("@Can't access file %s\n", filename);
perror(filename);
return NULL;
} else {
fullpath = mcalloc(200 + strlen(filename) + 10, sizeof(char));
#ifdef C_CYGWIN
sprintf(fullpath, "%s", filename);
#else
if(!getcwd(cwd, 200)) {
mprintf("@Can't get absolute pathname of current working directory.\n");
return NULL;
}
sprintf(fullpath, "%s/%s", cwd, filename);
#endif
}
return fullpath;
}
int dconnect(const struct optstruct *opt)
{
char buff[4096], cwd[200], *file, *scancmd, *pt;
struct sockaddr_un server;
struct sockaddr_in server2;
struct sockaddr_in server2;
struct hostent *he;
struct cfgstruct *copt, *cpt;
int sockd, wsockd, loopw = 60, bread, port;
const char *clamav_conf = getargl(opt, "config-file");
FILE *fd;
int sockd;
if(!clamav_conf)
clamav_conf = DEFAULT_CFG;
if((copt = parsecfg(clamav_conf, 1)) == NULL) {
mprintf("@Can't parse the configuration file.\n");
return 2;
return -1;
}
/* Set default address to connect to; needed for scanning a stream and no TCP address specified */
/* Set default address to connect to */
server2.sin_addr.s_addr = inet_addr("127.0.0.1");
if(cfgopt(copt, "ScannerDaemonOutputFormat")) {
mprintf("clamdscan won't work with the ScannerDaemonOutputFormat option\n");
mprintf("enabled. Please disable it in %s\n", clamav_conf);
return 2;
}
if(cfgopt(copt, "TCPSocket") && cfgopt(copt, "LocalSocket")) {
mprintf("@Clamd is not configured properly.\n");
return 2;
return -1;
} else if((cpt = cfgopt(copt, "LocalSocket"))) {
server.sun_family = AF_UNIX;
@ -83,14 +219,14 @@ int client(const struct optstruct *opt)
if((sockd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
perror("socket()");
mprintf("@Can't create the socket.\n");
return 2;
return -1;
}
if(connect(sockd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
close(sockd);
perror("connect()");
mprintf("@Can't connect to clamd.\n");
return 2;
return -1;
}
} else if((cpt = cfgopt(copt, "TCPSocket"))) {
@ -98,7 +234,7 @@ int client(const struct optstruct *opt)
if((sockd = socket(SOCKET_INET, SOCK_STREAM, 0)) < 0) {
perror("socket()");
mprintf("@Can't create the socket.\n");
return 2;
return -1;
}
server2.sin_family = AF_INET;
@ -109,153 +245,109 @@ int client(const struct optstruct *opt)
close(sockd);
perror("gethostbyname()");
mprintf("@Can't lookup clamd hostname.\n");
return 2;
return -1;
}
server2.sin_addr = *(struct in_addr *) he->h_addr_list[0];
}
if(connect(sockd, (struct sockaddr *) &server2, sizeof(struct sockaddr_in)) < 0) {
close(sockd);
perror("connect()");
mprintf("@Can't connect to clamd.\n");
return 2;
return -1;
}
} else {
mprintf("@Clamd is not configured properly.\n");
return 2;
return -1;
}
return sockd;
}
if(opt->filename == NULL || strlen(opt->filename) == 0) {
/* we need the full path to the file */
if(!getcwd(cwd, 200)) {
mprintf("@Can't get the absolute pathname of the current working directory.\n");
return 2;
}
file = (char *) strdup(cwd);
} else if(!strcmp(opt->filename, "-")) { /* scan data from stdin */
if(write(sockd, "STREAM", 6) <= 0) {
mprintf("@Can't write to the socket.\n");
close(sockd);
return 2;
}
int client(const struct optstruct *opt, int *infected)
{
char cwd[200], *fullpath;
int sockd, ret, errors = 0;
struct stat sb;
memset(buff, 0, sizeof(buff));
while(loopw) {
read(sockd, buff, sizeof(buff));
if((pt = strstr(buff, "PORT"))) {
pt += 5;
sscanf(pt, "%d", &port);
break;
}
loopw--;
}
if(!loopw) {
mprintf("@Daemon not ready for stream scanning.\n");
return 2;
}
*infected = 0;
/* connect to clamd */
/* parse argument list */
if((wsockd = socket(SOCKET_INET, SOCK_STREAM, 0)) < 0) {
perror("socket()");
mprintf("@Can't create the socket.\n");
if(opt->filename == NULL || strlen(opt->filename) == 0) {
/* scan current directory */
if(!getcwd(cwd, 200)) {
mprintf("@Can't get absolute pathname of current working directory.\n");
return 2;
}
server2.sin_family = AF_INET;
server2.sin_port = htons(port);
if(connect(wsockd, (struct sockaddr *) &server2, sizeof(struct sockaddr_in)) < 0) {
close(wsockd);
perror("connect()");
mprintf("@Can't connect to clamd [port: %d].\n", port);
if((sockd = dconnect(opt)) < 0)
return 2;
}
while((bread = read(0, buff, sizeof(buff))) > 0) {
if(write(wsockd, buff, bread) <= 0) {
mprintf("@Can't write to the socket.\n");
close(wsockd);
return 2;
}
}
close(wsockd);
memset(buff, 0, sizeof(buff)); /* FIXME: ugly, but needed for mprintf */
while((bread = read(sockd, buff, sizeof(buff))) > 0) {
mprintf("%s", buff);
if(strstr(buff, "FOUND\n")) {
claminfo.ifiles++;
logg("%s", buff);
}
if (strstr(buff, "ERROR\n")) {
claminfo.errors++;
logg("%s", buff);
}
memset(buff, 0, sizeof(buff));
}
return claminfo.ifiles ? 1 : (claminfo.errors ? 2 : 0);
if((ret = dsfile(sockd, cwd)) >= 0)
*infected += ret;
else
errors++;
} else if(opt->filename[0] == '/') {
file = (char *) strdup(opt->filename);
} else {
struct stat foo;
close(sockd);
if(stat(opt->filename, &foo) == -1) {
mprintf("@Can't access file %s\n", opt->filename);
perror(opt->filename);
} else if(!strcmp(opt->filename, "-")) { /* scan data from stdin */
if((sockd = dconnect(opt)) < 0)
return 2;
} else {
file = mcalloc(200 + strlen(opt->filename) + 10, sizeof(char));
#ifdef C_CYGWIN
sprintf(file, "%s", opt->filename);
#else
/* we need the full path to the file */
if(!getcwd(cwd, 200)) {
mprintf("@Can't get the absolute pathname of the current working directory.\n");
return 2;
}
sprintf(file, "%s/%s", cwd, opt->filename);
#endif
}
}
if((ret = dsstream(sockd)) >= 0)
*infected += ret;
else
errors++;
scancmd = mcalloc(strlen(file) + 20, sizeof(char));
sprintf(scancmd, "CONTSCAN %s", file);
free(file);
if(write(sockd, scancmd, strlen(scancmd)) <= 0) {
mprintf("@Can't write to the socket.\n");
close(sockd);
return 2;
}
if((fd = fdopen(sockd, "r")) == NULL) {
mprintf("@Can't open descriptor %d to read.\n", sockd);
return 2;
}
} else {
int x;
char *thefilename;
for (x = 0; (thefilename = cli_strtok(opt->filename, x, "\t")) != NULL; x++) {
fullpath = thefilename;
if(stat(fullpath, &sb) == -1) {
mprintf("@Can't access file %s\n", fullpath);
perror(fullpath);
errors++;
} else {
if(fullpath[0] != '/') {
fullpath = abpath(thefilename);
free(thefilename);
if(!fullpath) {
mprintf("@Can't determine absolute path.\n");
return 2;
}
}
switch(sb.st_mode & S_IFMT) {
case S_IFREG:
case S_IFDIR:
if((sockd = dconnect(opt)) < 0)
return 2;
if((ret = dsfile(sockd, fullpath)) >= 0)
*infected += ret;
else
errors++;
close(sockd);
break;
default:
mprintf("@Not supported file type (%s)\n", fullpath);
errors++;
}
}
while(fgets(buff, sizeof(buff), fd)) {
if(strstr(buff, "FOUND\n")) {
claminfo.ifiles++;
logg("%s", buff);
free(fullpath);
}
if (strstr(buff, "ERROR\n")) {
claminfo.errors++;
logg("%s", buff);
}
mprintf("%s", buff);
}
fclose(fd);
return claminfo.ifiles ? 1 : (claminfo.errors ? 2 : 0);
return *infected ? 1 : (errors ? 2 : 0);
}

@ -21,6 +21,6 @@
#include "options.h"
int client(const struct optstruct *opt);
int client(const struct optstruct *opt, int *infected);
#endif

Loading…
Cancel
Save