~ubuntu-branches/ubuntu/raring/clamav/raring

« back to all changes in this revision

Viewing changes to clamdscan/clamd_fdscan.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen Gran
  • Date: 2008-09-05 17:25:34 UTC
  • mfrom: (0.35.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080905172534-yi3f8fkye1o7u1r3
* New upstream version (closes: #497662, #497773)
  - lots of new options for clamd.conf
  - fixes CVEs CVE-2008-3912, CVE-2008-3913, CVE-2008-3914, and
    CVE-2008-1389
* No longer supports --unzip option, so typo is gone (closes: #496276)
* Translations:
  - sv (thanks Martin Bagge <brother@bsnet.se>) (closes: #491760)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*      $Id: clamd_fdscan.c,v 1.2 2007/01/18 16:59:50 mbalmer Exp $     */
 
2
 
 
3
/*
 
4
 * Copyright (c) 2007 Marc Balmer <mbalmer@openbsd.org>
 
5
 *
 
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.
 
9
 *
 
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.
 
17
 */
 
18
 
 
19
#ifdef HAVE_CONFIG_H
 
20
#include "clamav-config.h"
 
21
#endif
 
22
 
 
23
#ifdef HAVE_FD_PASSING
 
24
#ifdef FDPASS_NEED_XOPEN
 
25
#define _XOPEN_SOURCE 500
 
26
#endif
 
27
 
 
28
#include <sys/types.h>
 
29
#include <sys/socket.h>
 
30
#include <sys/un.h>
 
31
#include <sys/uio.h>
 
32
#include <string.h>
 
33
 
 
34
#include <stdio.h>
 
35
#include <unistd.h>
 
36
 
 
37
#include "clamd_fdscan.h"
 
38
 
 
39
#define CLAMD_BUFSIZ    256
 
40
 
 
41
size_t cli_strlcpy(char *dst, const char *src, size_t siz);
 
42
/*
 
43
 * clamd_fdscan lets a running clamd process scan the contents of an open
 
44
 * filedescriptor by passing the filedescriptor to clamd.  The parameters
 
45
 * are as follows:
 
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
 
50
 *
 
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.
 
53
 */
 
54
int
 
55
clamd_fdscan(int s, int fd, char *name, size_t len)
 
56
{
 
57
        struct msghdr msg;
 
58
        struct cmsghdr *cmsg;
 
59
        unsigned char fdbuf[CMSG_SPACE(sizeof(int))];
 
60
        FILE *sp;
 
61
        char buf[CLAMD_BUFSIZ], *p, *q;
 
62
        off_t pos;
 
63
        struct iovec iov[1];
 
64
        char dummy[]="";
 
65
 
 
66
        iov[0].iov_base = dummy;
 
67
        iov[0].iov_len = 1;
 
68
 
 
69
        pos = lseek(fd, 0, SEEK_CUR);
 
70
 
 
71
        memset(&msg, 0, sizeof(msg));
 
72
        msg.msg_control = fdbuf;
 
73
        /* must send/receive at least one byte */
 
74
        msg.msg_iov = iov;
 
75
        msg.msg_iovlen = 1;
 
76
        msg.msg_controllen = CMSG_LEN(sizeof(int));
 
77
 
 
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;
 
83
 
 
84
        if(write(s, "FILDES\n", sizeof("FILDES\n")-1) != sizeof("FILDES\n")-1) {
 
85
                perror("write");
 
86
                close(s);
 
87
                return -1;
 
88
        }
 
89
        if (sendmsg(s, &msg, 0) == -1) {
 
90
                perror("sendmsg");
 
91
                close(s);
 
92
                return -1;
 
93
        }
 
94
 
 
95
        sp = fdopen(s,"r");
 
96
        if(!fgets(buf, sizeof(buf), sp)) {
 
97
                fclose(sp);
 
98
                close(s);
 
99
                return -1;
 
100
        }
 
101
        fclose(sp);
 
102
        close(s);
 
103
 
 
104
        if (pos != -1)
 
105
                lseek(fd, pos, SEEK_SET);
 
106
        if ((p = strrchr(buf, ' ')) != NULL) {
 
107
                ++p;
 
108
                if (!strncmp(p, "OK", 2))
 
109
                        return 0;
 
110
                else if (!strncmp(p, "FOUND", 5)) {
 
111
                        if (name != NULL) {
 
112
                                *--p = '\0';
 
113
                                q = strrchr(buf, ' ') + 1;
 
114
                                cli_strlcpy(name, q, len);
 
115
                        }
 
116
                        return 1;
 
117
                } else {
 
118
                        puts(buf);
 
119
                }
 
120
        }
 
121
        return -1;
 
122
}
 
123
#endif