clamdscan/proto.c
f6870133
 /*
c442ca9c
  *  Copyright (C) 2013-2019 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  *  Copyright (C) 2009-2013 Sourcefire, Inc.
f6870133
  *
  *  Authors: Tomasz Kojm, aCaB
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
  *  published by the Free Software Foundation.
  *
  *  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., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
  */
 
bfd89d7c
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
a3962a62
 #if defined(C_SOLARIS)
ac5da57a
 #ifndef __EXTENSIONS__
a3962a62
 #define __EXTENSIONS__
 #endif
ac5da57a
 #endif
a3962a62
 
bfd89d7c
 /* must be first because it may define _XOPEN_SOURCE */
 #include "shared/fdpassing.h"
f6870133
 #include <stdio.h>
4cd80898
 #ifdef HAVE_UNISTD_H
f6870133
 #include <unistd.h>
4cd80898
 #endif
f6870133
 #include <string.h>
 #include <errno.h>
5ad17a13
 #include <stdlib.h>
f6870133
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <sys/types.h>
be4bf7f4
 #ifdef HAVE_SYS_SELECT_H
ea2799a2
 #include <sys/select.h>
be4bf7f4
 #endif
4cd80898
 #ifndef _WIN32
f6870133
 #include <arpa/inet.h>
4cd80898
 #include <sys/socket.h>
68900531
 #include <sys/un.h>
a3962a62
 #include <netdb.h>
4cd80898
 #endif
f6870133
 
60d8d2c3
 #include "libclamav/clamav.h"
f6870133
 #include "libclamav/others.h"
ee6702ab
 #include "shared/actions.h"
f6870133
 #include "shared/output.h"
247fe3ae
 #include "shared/misc.h"
3507891f
 #include "shared/clamdcom.h"
f6870133
 
 #include "proto.h"
 #include "client.h"
 
9877f2d8
 extern unsigned long int maxstream;
fadd3046
 int printinfected;
247fe3ae
 extern struct optstruct *clamdopts;
f0e1c2e6
 #ifndef _WIN32
 extern struct sockaddr_un nixsock;
 #endif
f6870133
 
6ad45a29
 static const char *scancmd[] = { "CONTSCAN", "MULTISCAN", "INSTREAM", "FILDES", "ALLMATCHSCAN" };
f6870133
 
 /* Connects to clamd 
  * Returns a FD or -1 on error */
 int dconnect() {
1c1d726d
     int sockd, res;
     const struct optstruct *opt;
     struct addrinfo hints, *info, *p;
     char port[10];
6432bbfb
     char *ipaddr;
f6870133
 
f0e1c2e6
 #ifndef _WIN32
     opt = optget(clamdopts, "LocalSocket");
     if (opt->enabled) {
         if ((sockd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0) {
             if (connect(sockd, (struct sockaddr *)&nixsock, sizeof(nixsock)) == 0)
                 return sockd;
f08ce33c
             else {
                 logg("!Could not connect to clamd on LocalSocket %s: %s\n", opt->strarg, strerror(errno));
f0e1c2e6
                 close(sockd);
f08ce33c
             }
f0e1c2e6
         }
     }
 #endif
 
1c1d726d
     snprintf(port, sizeof(port), "%lld", optget(clamdopts, "TCPSocket")->numarg);
f6870133
 
1c1d726d
     opt = optget(clamdopts, "TCPAddr");
     while (opt) {
bb30678c
         if (opt->enabled) {
             ipaddr = NULL;
             if (opt->strarg)
                 ipaddr = (!strcmp(opt->strarg, "any") ? NULL : opt->strarg);
 
             memset(&hints, 0x00, sizeof(struct addrinfo));
             hints.ai_family = AF_UNSPEC;
             hints.ai_socktype = SOCK_STREAM;
 
             if ((res = getaddrinfo(ipaddr, port, &hints, &info))) {
                 logg("!Could not lookup %s: %s\n", ipaddr ? ipaddr : "", gai_strerror(res));
                 opt = opt->nextarg;
1c1d726d
                 continue;
             }
 
bb30678c
             for (p = info; p != NULL; p = p->ai_next) {
                 if((sockd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0) {
                     logg("!Can't create the socket: %s\n", strerror(errno));
                     continue;
                 }
 
                 if(connect(sockd, p->ai_addr, p->ai_addrlen) < 0) {
                     logg("!Could not connect to clamd on %s: %s\n", opt->strarg, strerror(errno));
                     closesocket(sockd);
                     continue;
                 }
 
                 freeaddrinfo(info);
                 return sockd;
1c1d726d
             }
 
             freeaddrinfo(info);
         }
6432bbfb
         opt = opt->nextarg;
f6870133
     }
1c1d726d
 
     return -1;
f6870133
 }
 
 /* Issues an INSTREAM command to clamd and streams the given file
f592af6c
  * Returns >0 on success, 0 soft fail, -1 hard fail */
f6870133
 static int send_stream(int sockd, const char *filename) {
     uint32_t buf[BUFSIZ/sizeof(uint32_t)];
     int fd, len;
83c52f7e
     unsigned long int todo = maxstream;
f6870133
 
     if(filename) {
89e12eec
 	if((fd = safe_open(filename, O_RDONLY | O_BINARY))<0) {
f592af6c
 	    logg("~%s: Access denied. ERROR\n", filename);
 	    return 0;
f6870133
 	}
     } else fd = 0;
 
9b3e1e85
     if(sendln(sockd, "zINSTREAM", 10)) {
 	close(fd);
 	return -1;
     }
f6870133
 
ff989b7d
     while((len = read(fd, &buf[1], sizeof(buf) - sizeof(uint32_t))) > 0) {
9877f2d8
 	if((unsigned int)len > todo) len = todo;
f6870133
 	buf[0] = htonl(len);
83c52f7e
 	if(sendln(sockd, (const char *)buf, len+sizeof(uint32_t))) {
f6870133
 	    close(fd);
f592af6c
 	    return -1;
f6870133
 	}
9877f2d8
 	todo -= len;
 	if(!todo) {
 	    len = 0;
 	    break;
 	}
f6870133
     }
     close(fd);
     if(len) {
5b1eee09
 	logg("!Failed to read from %s.\n", filename ? filename : "STDIN");
f592af6c
 	return 0;
f6870133
     }
83c52f7e
     *buf=0;
     sendln(sockd, (const char *)buf, 4);
f592af6c
     return 1;
f6870133
 }
 
ea2799a2
 #ifdef HAVE_FD_PASSING
f6870133
 /* Issues a FILDES command and pass a FD to clamd
f592af6c
  * Returns >0 on success, 0 soft fail, -1 hard fail */
f6870133
 static int send_fdpass(int sockd, const char *filename) {
     struct iovec iov[1];
     struct msghdr msg;
     struct cmsghdr *cmsg;
     unsigned char fdbuf[CMSG_SPACE(sizeof(int))];
     char dummy[]="";
     int fd;
 
     if(filename) {
284982ca
 	if((fd = open(filename, O_RDONLY))<0) {
f592af6c
 	    logg("~%s: Access denied. ERROR\n", filename);
 	    return 0;
f6870133
 	}
     } else fd = 0;
ea2799a2
     if(sendln(sockd, "zFILDES", 8)) {
       close(fd);
f592af6c
       return -1;
ea2799a2
     }
f6870133
 
     iov[0].iov_base = dummy;
     iov[0].iov_len = 1;
     memset(&msg, 0, sizeof(msg));
     msg.msg_control = fdbuf;
     msg.msg_iov = iov;
     msg.msg_iovlen = 1;
     msg.msg_controllen = CMSG_LEN(sizeof(int));
     cmsg = CMSG_FIRSTHDR(&msg);
     cmsg->cmsg_len = CMSG_LEN(sizeof(int));
     cmsg->cmsg_level = SOL_SOCKET;
     cmsg->cmsg_type = SCM_RIGHTS;
     *(int *)CMSG_DATA(cmsg) = fd;
     if(sendmsg(sockd, &msg, 0) == -1) {
4ca3fc95
 	logg("!FD send failed: %s\n", strerror(errno));
ea2799a2
 	close(fd);
f592af6c
 	return -1;
f6870133
     }
ea2799a2
     close(fd);
f592af6c
     return 1;
f6870133
 }
ea2799a2
 #endif
f6870133
 
247fe3ae
 /* 0: scan, 1: skip */
 static int chkpath(const char *path)
 {
 	const struct optstruct *opt;
 
    if((opt = optget(clamdopts, "ExcludePath"))->enabled) {
 	while(opt) {
 	    if(match_regex(path, opt->strarg) == 1) {
ee353f12
                 if (printinfected != 1)
                     logg("~%s: Excluded\n", path);
247fe3ae
 		return 1;
 	    }
 	    opt = opt->nextarg;
 	}
     }
     return 0;
 }
 
eab93adf
 static int ftw_chkpath(const char *path, struct cli_ftw_cbdata *data)
 {
6df13d04
     UNUSEDPARAM(data);
eab93adf
     return chkpath(path);
 }
 
f6870133
 /* Sends a proper scan request to clamd and parses its replies
  * This is used only in non IDSESSION mode
339f62ef
  * Returns the number of infected files or -1 on error
  * NOTE: filename may be NULL for STREAM scantype. */
dd5092ff
 int dsresult(int sockd, int scantype, const char *filename, int *printok, int *errors) {
b33354e5
     int infected = 0, len = 0, beenthere = 0;
5b1eee09
     char *bol, *eol;
     struct RCVLN rcv;
a2a004df
     STATBUF sb;
f6870133
 
339f62ef
     if(filename && chkpath(filename))
247fe3ae
 	return 0;
f6870133
     recvlninit(&rcv, sockd);
 
     switch(scantype) {
     case MULTI:
     case CONT:
6ad45a29
     case ALLMATCH:
339f62ef
     if (!filename) {
 	logg("Filename cannot be NULL for MULTISCAN or CONTSCAN.\n");
 	return -1;
     }
f6870133
     len = strlen(filename) + strlen(scancmd[scantype]) + 3;
     if (!(bol = malloc(len))) {
f592af6c
 	logg("!Cannot allocate a command buffer: %s\n", strerror(errno));
f6870133
 	return -1;
     }
     sprintf(bol, "z%s %s", scancmd[scantype], filename);
ede343e0
     if(sendln(sockd, bol, len)) {
 	free(bol);
 	return -1;
     }
f6870133
     free(bol);
     break;
 
     case STREAM:
339f62ef
         /* NULL filename safe in send_stream() */
f592af6c
 	len = send_stream(sockd, filename);
d9237414
 	break;
f6870133
 #ifdef HAVE_FD_PASSING
     case FILDES:
339f62ef
         /* NULL filename safe in send_fdpass() */
f592af6c
 	len = send_fdpass(sockd, filename);
f6870133
 	break;
 #endif
     }
 
22446430
     if(len <=0) {
 	*printok = 0;
d8dcfe2e
 	if(errors)
 	    (*errors)++;
22446430
 	return len;
     }
f592af6c
 
f6870133
     while((len = recvln(&rcv, &bol, &eol))) {
5b1eee09
 	if(len == -1) return -1;
6a779d44
 	beenthere = 1;
f6870133
 	if(!filename) logg("~%s\n", bol);
 	if(len > 7) {
4a9f7873
 	    char *colon = strrchr(bol, ':');
fae973a2
 	    if(colon && colon[1] != ' ') {
bdf54240
 		char *br;
fae973a2
 		*colon = 0;
bdf54240
 		br = strrchr(bol, '(');
 		if(br)
 		    *br = 0;
 		colon = strrchr(bol, ':');
fae973a2
 	    }
f6870133
 	    if(!colon) {
6ad45a29
 		char * unkco = "UNKNOWN COMMAND";
 		if (!strncmp(bol, unkco, sizeof(unkco) - 1))
 		    logg("clamd replied \"UNKNOWN COMMAND\". Command was %s\n", 
 			 (scantype < 0 || scantype > MAX_SCANTYPE) ? "unidentified" :
 			                                             scancmd[scantype]);
 		else
 		    logg("Failed to parse reply: \"%s\"\n", bol);
5b1eee09
 		return -1;
f6870133
 	    } else if(!memcmp(eol - 7, " FOUND", 6)) {
1f1bf36b
                 static char last_filename[PATH_MAX+1] = {'\0'};
cbc0c5f4
 		*(eol - 7) = 0;
22446430
 		*printok = 0;
9989e980
                 if (scantype != ALLMATCH) {
1f1bf36b
                     infected++;
9989e980
                 } else {
                     if (filename != NULL && strcmp(filename, last_filename)) {
                         infected++;
                         strncpy(last_filename, filename, PATH_MAX);
                         last_filename[PATH_MAX] = '\0';
                     }
1f1bf36b
                 }
f6870133
 		if(filename) {
 		    if(scantype >= STREAM) {
bdf54240
 			logg("~%s%s FOUND\n", filename, colon);
f6870133
 			if(action) action(filename);
 		    } else {
bdf54240
 			logg("~%s FOUND\n", bol);
f6870133
 			*colon = '\0';
 			if(action)
 			    action(bol);
 		    }
 		}
 	    } else if(!memcmp(eol-7, " ERROR", 6)) {
d8dcfe2e
 		if(errors)
 		    (*errors)++;
22446430
 		*printok = 0;
f6870133
 		if(filename) {
 		    if(scantype >= STREAM)
 			logg("~%s%s\n", filename, colon);
 		    else
 			logg("~%s\n", bol);
 		}
 	    }
 	}
     }
6a779d44
     if(!beenthere) {
339f62ef
         if (!filename) {
 	    logg("STDIN: noreply from clamd\n.");
 	    return -1;
 	}
d9b6b8c7
         if(CLAMSTAT(filename, &sb) == -1) {
339f62ef
 	    logg("~%s: stat() failed with %s, clamd may not be responding\n",
 		 filename, strerror(errno));
 	    return -1;
 	}
9cdf477e
 	if(!S_ISDIR(sb.st_mode)) {
339f62ef
 	    logg("~%s: no reply from clamd\n", filename);
9cdf477e
 	    return -1;
 	}
6a779d44
     }
5b1eee09
     return infected;
f6870133
 }
 
 /* Used by serial_callback() */
 struct client_serial_data {
     int infected;
     int scantype;
22446430
     int printok;
9cdf477e
     int files;
     int errors;
f6870133
 };
 
83c52f7e
 /* FTW callback for scanning in non IDSESSION mode
  * Returns SUCCESS or BREAK on success, CL_EXXX on error */
a2a004df
 static int serial_callback(STATBUF *sb, char *filename, const char *path, enum cli_ftw_reason reason, struct cli_ftw_cbdata *data) {
f6870133
     struct client_serial_data *c = (struct client_serial_data *)data->data;
     int sockd, ret;
     const char *f = filename;
 
6df13d04
     UNUSEDPARAM(sb);
 
eab93adf
     if(chkpath(path))
 	return CL_SUCCESS;
dd5092ff
     c->files++;
f6870133
     switch(reason) {
     case error_stat:
9cdf477e
 	logg("!Can't access file %s\n", path);
dd5092ff
 	c->errors++;
f6870133
 	return CL_SUCCESS;
     case error_mem:
9cdf477e
 	logg("!Memory allocation failed in ftw\n");
 	c->errors++;
f6870133
 	return CL_EMEM;
     case warning_skipped_dir:
 	logg("^Directory recursion limit reached\n");
39667b9f
     case warning_skipped_link:
f6870133
 	return CL_SUCCESS;
     case warning_skipped_special:
c6d1bd9a
 	logg("^%s: Not supported file type\n", path);
9cdf477e
 	c->errors++;
f6870133
 	return CL_SUCCESS;
ea2799a2
     case visit_directory_toplev:
 	if(c->scantype >= STREAM)
f6870133
 	    return CL_SUCCESS;
 	f = path;
5b1eee09
 	filename = NULL;
     case visit_file:
ea2799a2
 	break;
f6870133
     }
ea2799a2
 
f6870133
     if((sockd = dconnect()) < 0) {
5b1eee09
 	if(filename) free(filename);
dd5092ff
 	c->errors++;
7609af89
 	return CL_EOPEN;
f6870133
     }
dd5092ff
     ret = dsresult(sockd, c->scantype, f, &c->printok, &c->errors);
5b1eee09
     if(filename) free(filename);
081f6473
     closesocket(sockd);
dd5092ff
     if(ret < 0) {
 	c->errors++;
 	return CL_EOPEN;
     }
5b1eee09
     c->infected += ret;
f6870133
     if(reason == visit_directory_toplev)
 	return CL_BREAK;
     return CL_SUCCESS;
 }
 
 /* Non-IDSESSION handler
83c52f7e
  * Returns non zero for serious errors, zero otherwise */
dd5092ff
 int serial_client_scan(char *file, int scantype, int *infected, int *err, int maxlevel, int flags) {
f6870133
     struct cli_ftw_cbdata data;
     struct client_serial_data cdata;
83c52f7e
     int ftw;
f6870133
 
     cdata.infected = 0;
9cdf477e
     cdata.files = 0;
     cdata.errors = 0;
22446430
     cdata.printok = printinfected^1;
f6870133
     cdata.scantype = scantype;
     data.data = &cdata;
 
eab93adf
     ftw = cli_ftw(file, flags, maxlevel ? maxlevel : INT_MAX, serial_callback, &data, ftw_chkpath);
f6870133
     *infected += cdata.infected;
dd5092ff
     *err += cdata.errors;
83c52f7e
 
dd5092ff
     if(!cdata.errors && (ftw == CL_SUCCESS || ftw == CL_BREAK)) {
22446430
 	if(cdata.printok)
83c52f7e
 	    logg("~%s: OK\n", file);
 	return 0;
d8dcfe2e
     } else if(!cdata.files) {
 	logg("~%s: No files scanned\n", file);
76d92cf0
 	return 0;
83c52f7e
     }
     return 1;
f6870133
 }
 
 /* Used in IDSESSION mode */
 struct client_parallel_data {
     int infected;
9cdf477e
     int files;
     int errors;
f6870133
     int scantype;
     int sockd;
     int lastid;
22446430
     int printok;
f6870133
     struct SCANID {
 	unsigned int id;
 	const char *file;
 	struct SCANID *next;
     } *ids;
 };
 
 /* Sends a proper scan request to clamd and parses its replies
  * This is used only in IDSESSION mode
d90f43a1
  * Returns 0 on success, 1 on hard failures, 2 on len == 0 (bb#1717) */
0115f12f
 static int dspresult(struct client_parallel_data *c) {
f6870133
     const char *filename;
     char *bol, *eol;
     unsigned int rid;
     int len;
     struct SCANID **id = NULL;
     struct RCVLN rcv;
 
     recvlninit(&rcv, c->sockd);
     do {
 	len = recvln(&rcv, &bol, &eol);
5b1eee09
 	if(len < 0) return 1;
d90f43a1
 	if(!len) return 2;
f6870133
 	if((rid = atoi(bol))) {
 	    id = &c->ids;
 	    while(*id) {
 		if((*id)->id == rid) break;
 		id = &((*id)->next);
 	    }
 	    if(!*id) id = NULL;
 	}
 	if(!id) {
 	    logg("!Bogus session id from clamd\n");
83c52f7e
 	    return 1;
f6870133
 	}
 	filename = (*id)->file;
 	if(len > 7) {
4a9f7873
 	    char *colon = strrchr(bol, ':');
f6870133
 	    if(!colon) {
5b1eee09
 		logg("!Failed to parse reply\n");
 		free((void *)filename);
 		return 1;
f6870133
 	    } else if(!memcmp(eol - 7, " FOUND", 6)) {
 		c->infected++;
22446430
 		c->printok = 0;
f6870133
 		logg("~%s%s\n", filename, colon);
 		if(action) action(filename);
 	    } else if(!memcmp(eol-7, " ERROR", 6)) {
d8dcfe2e
 		c->errors++;
22446430
 		c->printok = 0;
5b1eee09
 		logg("~%s%s\n", filename, colon);
f6870133
 	    }
 	}
 	free((void *)filename);
 	bol = (char *)*id;
 	*id = (*id)->next;
 	free(bol);
     } while(rcv.cur != rcv.buf); /* clamd sends whole lines, so, on partial lines, we just assume
 				    more data can be recv()'d with close to zero latency */
     return 0;
 }
 
83c52f7e
 /* FTW callback for scanning in IDSESSION mode
5b1eee09
  * Returns SUCCESS on success, CL_EXXX or BREAK on error */
a2a004df
 static int parallel_callback(STATBUF *sb, char *filename, const char *path, enum cli_ftw_reason reason, struct cli_ftw_cbdata *data) {
f6870133
     struct client_parallel_data *c = (struct client_parallel_data *)data->data;
f592af6c
     struct SCANID *cid;
b33354e5
     int res = CL_CLEAN;
f6870133
 
6df13d04
     UNUSEDPARAM(sb);
 
eab93adf
     if(chkpath(path))
 	return CL_SUCCESS;
dd5092ff
     c->files++;
f6870133
     switch(reason) {
     case error_stat:
9cdf477e
 	logg("!Can't access file %s\n", path);
dd5092ff
 	c->errors++;
f6870133
 	return CL_SUCCESS;
     case error_mem:
9cdf477e
 	logg("!Memory allocation failed in ftw\n");
 	c->errors++;
f6870133
 	return CL_EMEM;
     case warning_skipped_dir:
 	logg("^Directory recursion limit reached\n");
 	return CL_SUCCESS;
     case warning_skipped_special:
c6d1bd9a
 	logg("^%s: Not supported file type\n", path);
9cdf477e
 	c->errors++;
5b1eee09
     case warning_skipped_link:
f6870133
     case visit_directory_toplev:
 	return CL_SUCCESS;
5b1eee09
     case visit_file:
f6870133
 	break;
     }
 
     while(1) {
 	/* consume all the available input to let some of the clamd
 	 * threads blocked on send() to be dead.
 	 * by doing so we shouldn't deadlock on the next recv() */
 	fd_set rfds, wfds;
 	FD_ZERO(&rfds);
 	FD_SET(c->sockd, &rfds);
 	FD_ZERO(&wfds);
 	FD_SET(c->sockd, &wfds);
 	if(select(c->sockd + 1, &rfds, &wfds, NULL, NULL) < 0) {
 	    if(errno == EINTR) continue;
 	    free(filename);
f592af6c
 	    logg("!select() failed during session: %s\n", strerror(errno));
83c52f7e
 	    return CL_BREAK;
f6870133
 	}
 	if(FD_ISSET(c->sockd, &rfds)) {
 	    if(dspresult(c)) {
 		free(filename);
 		return CL_BREAK;
 	    } else continue;
 	}
 	if(FD_ISSET(c->sockd, &wfds)) break;
     }
 
fadd3046
     cid = (struct SCANID *)malloc(sizeof(struct SCANID));
83c52f7e
     if(!cid) {
 	free(filename);
22446430
 	logg("!Failed to allocate scanid entry: %s\n", strerror(errno));
83c52f7e
 	return CL_BREAK;
     }
f6870133
     cid->id = ++c->lastid;
     cid->file = filename;
f592af6c
     cid->next = c->ids;
     c->ids = cid;
f6870133
 
     switch(c->scantype) {
b7990b9c
 #ifdef HAVE_FD_PASSING
f6870133
     case FILDES:
f592af6c
 	res = send_fdpass(c->sockd, filename);
f6870133
 	break;
b7990b9c
 #endif
f6870133
     case STREAM:
f592af6c
 	res = send_stream(c->sockd, filename);
f6870133
 	break;
     }
f592af6c
     if(res <= 0) {
22446430
 	c->printok = 0;
d8dcfe2e
 	c->errors++;
f592af6c
 	c->ids = cid->next;
 	c->lastid--;
 	free(cid);
 	free(filename);
 	return res ? CL_BREAK : CL_SUCCESS;
     }
f6870133
     return CL_SUCCESS;
 }
 
b7990b9c
 /* IDSESSION handler
83c52f7e
  * Returns non zero for serious errors, zero otherwise */
dd5092ff
 int parallel_client_scan(char *file, int scantype, int *infected, int *err, int maxlevel, int flags) {
f6870133
     struct cli_ftw_cbdata data;
     struct client_parallel_data cdata;
83c52f7e
     int ftw;
f6870133
 
     if((cdata.sockd = dconnect()) < 0)
 	return 1;
 
     if(sendln(cdata.sockd, "zIDSESSION", 11)) {
081f6473
 	closesocket(cdata.sockd);
f6870133
 	return 1;
     }
 
     cdata.infected = 0;
9cdf477e
     cdata.files = 0;
     cdata.errors = 0;
f6870133
     cdata.scantype = scantype;
     cdata.lastid = 0;
4a9f7873
     cdata.ids = NULL;
22446430
     cdata.printok = printinfected^1;
f6870133
     data.data = &cdata;
 
eab93adf
     ftw = cli_ftw(file, flags, maxlevel ? maxlevel : INT_MAX, parallel_callback, &data, ftw_chkpath);
83c52f7e
 
5b1eee09
     if(ftw != CL_SUCCESS) {
dd5092ff
 	*err += cdata.errors;
2097428f
 	*infected += cdata.infected;
081f6473
 	closesocket(cdata.sockd);
83c52f7e
 	return 1;
     }
f6870133
 
6ef6ea82
     sendln(cdata.sockd, "zEND", 5);
533fa895
     while(cdata.ids && !dspresult(&cdata));
081f6473
     closesocket(cdata.sockd);
6ef6ea82
 
2097428f
     *infected += cdata.infected;
dd5092ff
     *err += cdata.errors;
2097428f
 
533fa895
     if(cdata.ids) {
5b1eee09
 	logg("!Clamd closed the connection before scanning all files.\n");
83c52f7e
 	return 1;
533fa895
     }
dd5092ff
     if(cdata.errors)
 	return 1;
 
76d92cf0
     if(!cdata.files)
 	return 0;
dd5092ff
 
22446430
     if(cdata.printok)
83c52f7e
 	logg("~%s: OK\n", file);
     return 0;
f6870133
 }