clamdscan/client.c
e3aaff8e
 /*
8ca8a18e
  *  Copyright (C) 2002 - 2007 Tomasz Kojm <tkojm@clamav.net>
e3aaff8e
  *
  *  This program is free software; you can redistribute it and/or modify
bb34cb31
  *  it under the terms of the GNU General Public License version 2 as
  *  published by the Free Software Foundation.
e3aaff8e
  *
  *  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
48b7b4a7
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
e3aaff8e
  */
 
6d6e8271
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
e3aaff8e
 #include <stdio.h>
63abd169
 #include <unistd.h>
 #include <string.h>
e3aaff8e
 #include <sys/types.h>
afb48b28
 #include <sys/stat.h>
e3aaff8e
 #include <sys/socket.h>
cfef445d
 #ifdef HAVE_SYS_LIMITS_H
 #include <sys/limits.h>
 #endif
e3aaff8e
 #include <sys/un.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
f8f80da9
 #include <netdb.h>
63abd169
 #include <utime.h>
 #include <errno.h>
e3aaff8e
 
7708ddfc
 #ifdef HAVE_SYS_UIO_H
 #include <sys/uio.h>
 #endif
 
a68d5e2f
 #include "shared/optparser.h"
 #include "shared/output.h"
 #include "shared/misc.h"
 #include "libclamav/str.h"
 
fc83da82
 #include "client.h"
5cd3f734
 #include "clamd_fdscan.h"
e3aaff8e
 
eb4300ad
 #define SOCKET_INET	AF_INET
b8cdcd2e
 
63abd169
 int notremoved = 0, notmoved = 0;
d61aeda2
 int printinfected = 0;
fe6c6a02
 
abd6d2c7
 static void (*action)(const char *) = NULL;
 static char *actarget;
 static void move_infected(const char *filename, int move);
 static void action_move(const char *filename) {
     move_infected(filename, 1);
 }
 static void action_copy(const char *filename) {
     move_infected(filename, 0);
 }
 static void action_remove(const char *filename) {
     if(unlink(filename)) {
 	logg("!%s: Can't remove.\n", filename);
 	notremoved++;
     } else {
 	logg("~%s: Removed.\n", filename);
     }
 }
 
 void actsetup(const struct optstruct *opts) {
     if(optget(opts, "move")->enabled) {
 	actarget = optget(opts, "move")->strarg;
 	action = action_move;
     } else if (optget(opts, "copy")->enabled) {
 	actarget = optget(opts, "copy")->strarg;
 	action = action_copy;
     } else if(optget(opts, "remove")->enabled) {
 	action = action_remove;
     }
 }
 
 static int dsresult(int sockd, const char *scantype, const char *filename)
fe6c6a02
 {
 	int infected = 0, waserror = 0;
2599ba97
 	unsigned int len;
 	char buff[PATH_MAX+24], *pt, *bol, *eol;
fe6c6a02
 
2599ba97
     len = strlen(filename) + strlen(scantype) + 3;
     if (!(bol = malloc(len))) {
 	logg("!Cannot allocate a command buffer\n");
fe6c6a02
 	return -1;
     }
2599ba97
     pt = bol;
     sprintf(pt, "z%s %s", scantype, filename);
     while(len) {
 	int sent = send(sockd, pt, len, 0);
 	if(sent <= 0) {
 	    logg("!Can't send request to clamd\n");
 	    return -1;
 	}
 	pt += sent;
 	len -= sent;
     }
     free(bol);
 
     len = sizeof(buff);
     bol = pt = buff;
     while(1) {
 	int r = recv(sockd, pt, len, 0);
 	if(r<=0) {
 	    if(r || pt!=buff) {
 		logg("!Communication error\n");
 		waserror = 1;
 	    }
 	    break;
 	}
 	while(r && (eol = memchr(pt, 0, r))) {
01925d65
 	    logg("~%s\n", bol);
4793a835
 	    if(eol-bol > 7) {
03f214fb
 		if(!memcmp(eol - 6, " FOUND", 6)) {
2599ba97
 		    infected++;
abd6d2c7
 		    if(action) {
 			char *comma = strrchr(bol, ':');
 			if(comma) {
 			    *comma = '\0';
 			    action(bol);
2599ba97
 			}
54c55863
 		    }
01925d65
 		} else if(!memcmp(eol-6, " ERROR", 6)) {
2599ba97
 		    waserror = 1;
63abd169
 		}
 	    }
2599ba97
 	    eol++;
 	    r -= eol - pt;
 	    bol = pt = eol;
fe6c6a02
 	}
2599ba97
 	r = &pt[r]-bol;
 	len = sizeof(buff) - r;
 	if(!len) {
 	    logg("!Overlong reply from clamd\n");
fe6c6a02
 	    waserror = 1;
2599ba97
 	    break;
fe6c6a02
 	}
2599ba97
 	if(r && bol!=buff) /* memmove is stupid in older glibc's */
 	    memmove(buff, bol, r);
 	pt = &buff[r];
03f214fb
 	bol = buff;
fe6c6a02
     }
 
2599ba97
     return infected ? infected : (waserror ? -1 : 0);
fe6c6a02
 }
 
2599ba97
 static int dsstream(int sockd)
fe6c6a02
 {
 	int wsockd, loopw = 60, bread, port, infected = 0;
 	struct sockaddr_in server;
040a5084
 	struct sockaddr_in peer;
73b243dc
 	socklen_t peer_size;
fe6c6a02
 	char buff[4096], *pt;
 
7708ddfc
 
fe6c6a02
     if(write(sockd, "STREAM", 6) <= 0) {
a68d5e2f
 	logg("!Can't write to the socket.\n");
fe6c6a02
 	return 2;
     }
 
     while(loopw) {
38fe8af4
 	memset(buff, 0, sizeof(buff));
73b243dc
 	if(read(sockd, buff, sizeof(buff)) > 0) {
 	    if((pt = strstr(buff, "PORT"))) {
 		pt += 5;
 		sscanf(pt, "%d", &port);
 		break;
 	    }
fe6c6a02
 	}
 	loopw--;
     }
 
     if(!loopw) {
a68d5e2f
 	logg("!Daemon not ready for stream scanning.\n");
fe6c6a02
 	return -1;
     }
 
     /* connect to clamd */
 
     if((wsockd = socket(SOCKET_INET, SOCK_STREAM, 0)) < 0) {
 	perror("socket()");
a68d5e2f
 	logg("!Can't create the socket.\n");
fe6c6a02
 	return -1;
     }
 
     server.sin_family = AF_INET;
     server.sin_port = htons(port);
 
040a5084
     peer_size = sizeof(peer);
     if(getpeername(sockd, (struct sockaddr *) &peer, &peer_size) < 0) {
 	perror("getpeername()");
a68d5e2f
 	logg("!Can't get socket peer name.\n");
040a5084
 	return -1;
     }
 
7062124c
     switch (peer.sin_family) {
 	case AF_UNIX:
 	    server.sin_addr.s_addr = inet_addr("127.0.0.1");
 	    break;
 	case AF_INET:
 	    server.sin_addr.s_addr = peer.sin_addr.s_addr;
 	    break;
 	default:
a68d5e2f
 	    logg("!Unexpected socket type: %d.\n", peer.sin_family);
7062124c
 	    return -1;
     }
040a5084
 
fe6c6a02
     if(connect(wsockd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0) {
 	close(wsockd);
 	perror("connect()");
a68d5e2f
 	logg("!Can't connect to clamd [port: %d].\n", port);
fe6c6a02
 	return -1;
     }
 
     while((bread = read(0, buff, sizeof(buff))) > 0) {
 	if(write(wsockd, buff, bread) <= 0) {
a68d5e2f
 	    logg("!Can't write to the socket.\n");
fe6c6a02
 	    close(wsockd);
 	    return -1;
 	}
     }
     close(wsockd);
 
     memset(buff, 0, sizeof(buff));
     while((bread = read(sockd, buff, sizeof(buff))) > 0) {
0ae41a2d
 	logg("%s", buff);
fe6c6a02
 	if(strstr(buff, "FOUND\n")) {
 	    infected++;
63abd169
 
7708ddfc
 	} else if(strstr(buff, "ERROR\n")) {
fe6c6a02
 	    logg("%s", buff);
 	    return -1;
 	}
 	memset(buff, 0, sizeof(buff));
     }
 
     return infected;
 }
 
cfef445d
 #ifndef PATH_MAX
 #define PATH_MAX 1024
 #endif
 
079229d6
 static char *abpath(const char *filename)
fe6c6a02
 {
 	struct stat foo;
cfef445d
 	char *fullpath, cwd[PATH_MAX + 1];
fe6c6a02
 
     if(stat(filename, &foo) == -1) {
0ae41a2d
 	logg("^Can't access file %s\n", filename);
fe6c6a02
 	perror(filename);
 	return NULL;
     } else {
cfef445d
 	fullpath = malloc(PATH_MAX + strlen(filename) + 10);
 	if(!getcwd(cwd, PATH_MAX)) {
0ae41a2d
 	    logg("^Can't get absolute pathname of current working directory.\n");
fe6c6a02
 	    return NULL;
 	}
 	sprintf(fullpath, "%s/%s", cwd, filename);
     }
 
     return fullpath;
 }
 
a68d5e2f
 static int dconnect(const struct optstruct *opts, int *is_unix)
e3aaff8e
 {
 	struct sockaddr_un server;
fe6c6a02
 	struct sockaddr_in server2;
f8f80da9
 	struct hostent *he;
a68d5e2f
 	struct optstruct *clamdopts;
 	const struct optstruct *opt;
 	const char *clamd_conf = optget(opts, "config-file")->strarg;
fe6c6a02
 	int sockd;
 
c1a133a0
     if(is_unix)
a68d5e2f
 	*is_unix = 0;
e3aaff8e
 
a68d5e2f
     if((clamdopts = optparse(clamd_conf, 0, NULL, 1, OPT_CLAMD, 0, NULL)) == NULL) {
 	logg("!Can't parse clamd configuration file %s\n", clamd_conf);
fe6c6a02
 	return -1;
e3aaff8e
     }
 
75ccac9f
     memset((char *) &server, 0, sizeof(server));
     memset((char *) &server2, 0, sizeof(server2));
 
fe6c6a02
     /* Set default address to connect to */
f6f5d56f
     server2.sin_addr.s_addr = inet_addr("127.0.0.1");    
e3aaff8e
 
a68d5e2f
     if((opt = optget(clamdopts, "LocalSocket"))->enabled) {
e3aaff8e
 
 	server.sun_family = AF_UNIX;
a68d5e2f
 	strncpy(server.sun_path, opt->strarg, sizeof(server.sun_path));
72ce4b70
 	server.sun_path[sizeof(server.sun_path)-1]='\0';
e3aaff8e
 
 	if((sockd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
 	    perror("socket()");
a68d5e2f
 	    logg("!Can't create the socket.\n");
 	    optfree(clamdopts);
fe6c6a02
 	    return -1;
e3aaff8e
 	}
 
 	if(connect(sockd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
 	    close(sockd);
 	    perror("connect()");
a68d5e2f
 	    logg("!Can't connect to clamd.\n");
 	    optfree(clamdopts);
fe6c6a02
 	    return -1;
e3aaff8e
 	}
c1a133a0
 	if(is_unix)
 		*is_unix = 1;
a68d5e2f
     } else if((opt = optget(clamdopts, "TCPSocket"))->enabled) {
e3aaff8e
 
b8cdcd2e
 	if((sockd = socket(SOCKET_INET, SOCK_STREAM, 0)) < 0) {
e3aaff8e
 	    perror("socket()");
a68d5e2f
 	    logg("!Can't create the socket.\n");
 	    optfree(clamdopts);
fe6c6a02
 	    return -1;
e3aaff8e
 	}
 
 	server2.sin_family = AF_INET;
a68d5e2f
 	server2.sin_port = htons(opt->numarg);
e3aaff8e
 
a68d5e2f
 	if((opt = optget(clamdopts, "TCPAddr"))->enabled) {
 	    if ((he = gethostbyname(opt->strarg)) == 0) {
f8f80da9
 		close(sockd);
 		perror("gethostbyname()");
a68d5e2f
 		logg("!Can't lookup clamd hostname.\n");
 		optfree(clamdopts);
fe6c6a02
 		return -1;
f8f80da9
 	    }
 	    server2.sin_addr = *(struct in_addr *) he->h_addr_list[0];
f6f5d56f
 	}
f8f80da9
 
e3aaff8e
 	if(connect(sockd, (struct sockaddr *) &server2, sizeof(struct sockaddr_in)) < 0) {
 	    close(sockd);
 	    perror("connect()");
a68d5e2f
 	    logg("!Can't connect to clamd.\n");
 	    optfree(clamdopts);
fe6c6a02
 	    return -1;
e3aaff8e
 	}
 
     } else {
a68d5e2f
 	logg("!Clamd is not configured properly.\n");
 	optfree(clamdopts);
fe6c6a02
 	return -1;
e3aaff8e
     }
 
a68d5e2f
     optfree(clamdopts);
8765287e
 
fe6c6a02
     return sockd;
 }
e3aaff8e
 
a68d5e2f
 int get_clamd_version(const struct optstruct *opts)
2a363377
 {
 	char buff[64];
 	int bread, sockd;
 
 
a68d5e2f
     if((sockd = dconnect(opts, NULL)) < 0)
2a363377
 	return 2;
 
     if(write(sockd, "VERSION", 7) <= 0) {
a68d5e2f
 	logg("!Can't write to the socket.\n");
c1c9d9f9
 	close(sockd);
2a363377
 	return 2;
     }
 
     while((bread = read(sockd, buff, sizeof(buff)-1)) > 0) {
 	buff[bread] = '\0';
 	printf("%s\n", buff);
     }
 
     close(sockd);
     return 0;
 }
 
a68d5e2f
 int reload_clamd_database(const struct optstruct *opts)
c1c9d9f9
 {
 	char buff[64];
 	int bread, sockd;
 
 
a68d5e2f
     if((sockd = dconnect(opts, NULL)) < 0)
c1c9d9f9
 	return 2;
 
     if(write(sockd, "RELOAD", 6) <= 0) {
 	logg("!Can't write to the socket.\n");
 	close(sockd);
 	return 2;
     }
 
     bread = read(sockd, buff, sizeof(buff) - 1);
7801f6db
     if(bread == -1 || strncmp(buff, "RELOADING", 9)) {
c1c9d9f9
 	logg("!Incorrect reply from clamd\n");
 	close(sockd);
 	return 2;
     }
 
     close(sockd);
     return 0;
 }
 
a68d5e2f
 int client(const struct optstruct *opts, int *infected)
fe6c6a02
 {
cfef445d
 	char cwd[PATH_MAX+1], *fullpath;
fe6c6a02
 	int sockd, ret, errors = 0;
 	struct stat sb;
402abedd
 	const char *scantype = "CONTSCAN";
e3aaff8e
 
 
fe6c6a02
     *infected = 0;
e3aaff8e
 
a68d5e2f
     if(optget(opts, "multiscan")->enabled)
3bb3357a
 	scantype = "MULTISCAN";
 
8765287e
     /* parse argument list */
a68d5e2f
     if(opts->filename == NULL || strlen(opts->filename) == 0) {
fe6c6a02
 	/* scan current directory */
cfef445d
 	if(!getcwd(cwd, PATH_MAX)) {
0ae41a2d
 	    logg("^Can't get absolute pathname of current working directory.\n");
e3aaff8e
 	    return 2;
 	}
 
a68d5e2f
 	if((sockd = dconnect(opts, NULL)) < 0)
e3aaff8e
 	    return 2;
 
abd6d2c7
 	if((ret = dsresult(sockd, scantype, cwd)) >= 0)
fe6c6a02
 	    *infected += ret;
 	else
 	    errors++;
e3aaff8e
 
fe6c6a02
 	close(sockd);
afb48b28
 
a68d5e2f
     } else if(!strcmp(opts->filename, "-")) { /* scan data from stdin */
c1a133a0
         int is_unix;
a68d5e2f
 	if((sockd = dconnect(opts, &is_unix)) < 0)
e3aaff8e
 	    return 2;
 
a68d5e2f
 	if(optget(opts,"fdpass")->enabled) {
c1a133a0
 #ifndef HAVE_FD_PASSING
 		logg("^File descriptor pass support not compiled in, falling back to stream scan\n");
2599ba97
 		ret = dsstream(sockd);
c1a133a0
 #else
 		if(!is_unix) {
 			logg("^File descriptor passing can only work on local (unix) sockets! Falling back to stream scan\n");
 			/* fall back to stream */
2599ba97
 			ret = dsstream(sockd);
c1a133a0
 		} else {
 			char buff[4096];
 			memset(buff, 0, sizeof(buff));
 			ret = clamd_fdscan(sockd, 0, buff, sizeof(buff));
d61aeda2
 			if(ret == 1 || ret == -1)
 			    logg("fd: %s%s\n",buff, ret == 1 ? " FOUND" : " ERROR");
 			else if(!printinfected)
 			    logg("fd: OK\n");
c1a133a0
 		}
 #endif
 	} else
2599ba97
 		ret = dsstream(sockd);
c1a133a0
 	if(ret >= 0)
fe6c6a02
 	    *infected += ret;
 	else
 	    errors++;
e3aaff8e
 
 	close(sockd);
 
fe6c6a02
     } else {
 	int x;
 	char *thefilename;
a68d5e2f
 	for (x = 0; (thefilename = cli_strtok(opts->filename, x, "\t")) != NULL; x++) {
fe6c6a02
 	    fullpath = thefilename;
 
 	    if(stat(fullpath, &sb) == -1) {
0ae41a2d
 		logg("^Can't access file %s\n", fullpath);
fe6c6a02
 		perror(fullpath);
 		errors++;
 	    } else {
9470856d
 		if(strcmp(fullpath, "/") && (strlen(fullpath) < 2 || (fullpath[0] != '/' && fullpath[0] != '\\' && fullpath[1] != ':'))) {
fe6c6a02
 		    fullpath = abpath(thefilename);
 		    free(thefilename);
 
 		    if(!fullpath) {
0ae41a2d
 			logg("^Can't determine absolute path.\n");
fe6c6a02
 			return 2;
 		    }
 		}
 
 		switch(sb.st_mode & S_IFMT) {
 		    case S_IFREG:
 		    case S_IFDIR:
a68d5e2f
 			if((sockd = dconnect(opts, NULL)) < 0)
fe6c6a02
 			    return 2;
 
abd6d2c7
 			if((ret = dsresult(sockd, scantype, fullpath)) >= 0)
fe6c6a02
 			    *infected += ret;
 			else
 			    errors++;
 
 			close(sockd);
 			break;
 
 		    default:
0ae41a2d
 			logg("^Not supported file type (%s)\n", fullpath);
fe6c6a02
 			errors++;
 		}
 	    }
e3aaff8e
 
fe6c6a02
 	    free(fullpath);
e3aaff8e
 	}
     }
 
fe6c6a02
     return *infected ? 1 : (errors ? 2 : 0);
e3aaff8e
 }
63abd169
 
abd6d2c7
 void move_infected(const char *filename, int move)
63abd169
 {
abd6d2c7
 	char *movefilename, numext[4 + 1];
73b243dc
 	const char *tmp;
 	struct stat ofstat, mfstat;
63abd169
 	int n, len, movefilename_size;
 	struct utimbuf ubuf;
 
abd6d2c7
     if(access(actarget, W_OK|X_OK) == -1) {
         logg("!problem %s file '%s': cannot write to '%s': %s\n", (move) ? "moving" : "copying", filename, actarget, strerror(errno));
63abd169
         notmoved++;
         return;
     }
 
73b243dc
     if(stat(filename, &ofstat) == -1) {
0ae41a2d
         logg("^Can't stat file %s\n", filename);
 	logg("Try to run clamdscan with clamd privileges\n");
63abd169
         notmoved++;
 	return;
     }
 
     if(!(tmp = strrchr(filename, '/')))
73b243dc
 	tmp = filename;
63abd169
 
abd6d2c7
     movefilename_size = sizeof(char) * (strlen(actarget) + strlen(tmp) + sizeof(numext) + 2);
63abd169
 
8ca8a18e
     if(!(movefilename = malloc(movefilename_size))) {
a68d5e2f
         logg("!Memory allocation error\n");
63abd169
 	exit(2);
     }
 
abd6d2c7
     if(!(cli_strrcpy(movefilename, actarget))) {
a68d5e2f
         logg("!cli_strrcpy() returned NULL\n");
63abd169
         notmoved++;
         free(movefilename);
         return;
     }
 
     strcat(movefilename, "/");
 
     if(!(strcat(movefilename, tmp))) {
a68d5e2f
         logg("!strcat() returned NULL\n");
63abd169
         notmoved++;
         free(movefilename);
         return;
     }
 
     if(!stat(movefilename, &mfstat)) {
abd6d2c7
         if((ofstat.st_dev == mfstat.st_dev) && (ofstat.st_ino == mfstat.st_ino)) { /* It's the same file */
63abd169
             logg("File excluded '%s'\n", filename);
             notmoved++;
             free(movefilename);
             return;
         } else {
             /* file exists - try to append an ordinal number to the
 	     * quranatined file in an attempt not to overwrite existing
 	     * files in quarantine  
 	     */
             len = strlen(movefilename);
             n = 0;        		        		
             do {
                 /* reset the movefilename to it's initial value by
 		 * truncating to the original filename length
 		 */
                 movefilename[len] = 0;
                 /* append .XXX */
                 sprintf(numext, ".%03d", n++);
                 strcat(movefilename, numext);            	
             } while(!stat(movefilename, &mfstat) && (n < 1000));
        }
     }
 
abd6d2c7
     if(!move || rename(filename, movefilename) == -1) {
63abd169
 	if(filecopy(filename, movefilename) == -1) {
abd6d2c7
 	    logg("^cannot %s '%s' to '%s': %s\n", (move) ? "move" : "copy", filename, movefilename, strerror(errno));
63abd169
 	    notmoved++;
 	    free(movefilename);
 	    return;
 	}
 
73b243dc
 	chmod(movefilename, ofstat.st_mode);
 	if(chown(movefilename, ofstat.st_uid, ofstat.st_gid) == -1)
 	    logg("^chown() failed for %s: %s\n", movefilename, strerror(errno));
63abd169
 
73b243dc
 	ubuf.actime = ofstat.st_atime;
 	ubuf.modtime = ofstat.st_mtime;
63abd169
 	utime(movefilename, &ubuf);
 
abd6d2c7
 	if(move && unlink(filename)) {
0ae41a2d
 	    logg("^cannot unlink '%s': %s\n", filename, strerror(errno));
abd6d2c7
 	    notremoved++;
63abd169
 	    free(movefilename);
 	    return;
 	}
     }
 
abd6d2c7
     logg("%s: %s to '%s'\n", (move)?"moved":"copied", filename, movefilename);
63abd169
 
     free(movefilename);
 }