1
/* $Id: clamd_fdscan.c,v 1.2 2007/01/18 16:59:50 mbalmer Exp $ */
4
* Copyright (c) 2007 Marc Balmer <mbalmer@openbsd.org>
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20
#include "clamav-config.h"
23
#ifdef HAVE_FD_PASSING
24
#ifdef FDPASS_NEED_XOPEN
25
#define _XOPEN_SOURCE 500
28
#include <sys/types.h>
29
#include <sys/socket.h>
37
#include "clamd_fdscan.h"
39
#define CLAMD_BUFSIZ 256
41
size_t cli_strlcpy(char *dst, const char *src, size_t siz);
43
* clamd_fdscan lets a running clamd process scan the contents of an open
44
* filedescriptor by passing the filedescriptor to clamd. The parameters
46
* s socket connected to clamd
47
* fd the open filedescriptor to pass for scanning
48
* name virus name, if a virus is found
49
* len max len of the virus name
51
* The functions returns 0 if the file was scanned and contains no virus,
52
* -1 if an error occurs and 1 if a virus is found.
55
clamd_fdscan(int s, int fd, char *name, size_t len)
59
unsigned char fdbuf[CMSG_SPACE(sizeof(int))];
61
char buf[CLAMD_BUFSIZ], *p, *q;
66
iov[0].iov_base = dummy;
69
pos = lseek(fd, 0, SEEK_CUR);
71
memset(&msg, 0, sizeof(msg));
72
msg.msg_control = fdbuf;
73
/* must send/receive at least one byte */
76
msg.msg_controllen = CMSG_LEN(sizeof(int));
78
cmsg = CMSG_FIRSTHDR(&msg);
79
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
80
cmsg->cmsg_level = SOL_SOCKET;
81
cmsg->cmsg_type = SCM_RIGHTS;
82
*(int *)CMSG_DATA(cmsg) = fd;
84
if(write(s, "FILDES\n", sizeof("FILDES\n")-1) != sizeof("FILDES\n")-1) {
89
if (sendmsg(s, &msg, 0) == -1) {
96
if(!fgets(buf, sizeof(buf), sp)) {
105
lseek(fd, pos, SEEK_SET);
106
if ((p = strrchr(buf, ' ')) != NULL) {
108
if (!strncmp(p, "OK", 2))
110
else if (!strncmp(p, "FOUND", 5)) {
113
q = strrchr(buf, ' ') + 1;
114
cli_strlcpy(name, q, len);