~ubuntu-branches/ubuntu/intrepid/dansguardian/intrepid-security

« back to all changes in this revision

Viewing changes to KAVScan.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Wirt
  • Date: 2008-04-06 14:47:06 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080406144706-2r26l1rougdmb1sd
Tags: 2.9.9.3-2
This time build with gcc 4.3 (Closes: #454889) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//  This program is free software; you can redistribute it and/or modify
2
 
//  it under the terms of the GNU General Public License as published by
3
 
//  the Free Software Foundation; either version 2 of the License, or
4
 
//  (at your option) any later version.
5
 
//
6
 
//  This program is distributed in the hope that it will be useful,
7
 
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
8
 
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9
 
//  GNU General Public License for more details.
10
 
//
11
 
//  You should have received a copy of the GNU General Public License
12
 
//  along with this program; if not, write to the Free Software
13
 
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14
 
 
15
 
//  based on exim/exiscan patch by Tom Kistner.
16
 
//  adapted by m4d.
17
 
//
18
 
 
19
 
#include "KAVScan.hpp"
20
 
 
21
 
 
22
 
extern OptionContainer o;
23
 
 
24
 
 
25
 
// Constructor
26
 
KAV5Engine::KAV5Engine ()
27
 
{
28
 
}
29
 
 
30
 
// Deconstructor
31
 
KAV5Engine::~KAV5Engine ()
32
 
{
33
 
};
34
 
 
35
 
 
36
 
int
37
 
KAV5Engine::scanFile (const char *_fname)
38
 
{
39
 
  int sock = 0;
40
 
  int infected = AV_CLEAN;
41
 
  struct sockaddr_un server;
42
 
 
43
 
#ifdef DGDEBUG
44
 
  cout << "Entering KAV5Engine scanFile" << endl;
45
 
  cout << "Before connect" << endl;
46
 
#endif
47
 
 
48
 
  //open the aveserver socket
49
 
  sock = socket (AF_UNIX, SOCK_STREAM, 0);
50
 
  if (sock < 0)
51
 
    {
52
 
#ifdef DGDEBUG
53
 
      cout << "socket creation failed" << endl;
54
 
#endif
55
 
      setErrString ("socket creation failed");
56
 
      return AV_FAIL;
57
 
    }
58
 
 
59
 
  server.sun_family = AF_UNIX;
60
 
  strcpy (server.sun_path, o.avesocket.c_str ());
61
 
  if (connect (sock, (struct sockaddr *) &server, sizeof (struct sockaddr_un)) < 0)
62
 
    {
63
 
      close (sock);
64
 
#ifdef DGDEBUG
65
 
      cout << "socket connect failed" << endl;
66
 
#endif
67
 
      setErrString ("socket connect failed");
68
 
      return AV_FAIL;
69
 
    }
70
 
 
71
 
  /* read aveserver's greeting and see if it is ready (2xx greeting) */
72
 
  char buf[32768];
73
 
  recv_line (sock, buf, sizeof (buf));
74
 
 
75
 
  if (buf[0] != '2')
76
 
    {
77
 
      /* aveserver is having problems */
78
 
      close (sock);
79
 
#ifdef DGDEBUG
80
 
      cout << "aveserver is unavailable (returned: " << ((buf[0] != 0) ? buf : (char *) "nothing") << ")" << endl;
81
 
#endif
82
 
      setErrString ("aveserver is unavailable");
83
 
      return AV_FAIL;
84
 
    };
85
 
 
86
 
  /* prepare our command */
87
 
  snprintf (buf, 32768, "SCAN bPQRSTUW %s\r\n", _fname);
88
 
 
89
 
  /* and send it */
90
 
  if (send (sock, buf, strlen (buf), 0) < 0)
91
 
    {
92
 
      close (sock);
93
 
#ifdef DGDEBUG
94
 
      cout << "unable to write to aveserver socket (" << o.avesocket << ")" << endl;
95
 
#endif
96
 
      setErrString ("unable to write to aveserver socket");
97
 
      return AV_FAIL;
98
 
    }
99
 
 
100
 
  /* read response lines, find malware name and final response */
101
 
  while (recv_line (sock, buf, 32768) > 0)
102
 
    {
103
 
#ifdef DGDEBUG
104
 
      cout << "aveserver response: " << buf << endl;
105
 
#endif
106
 
      if (buf[0] == '2')
107
 
        break;
108
 
      if (strncmp (buf, "322", 3) == 0)
109
 
        {
110
 
          char *p = strchr (&buf[4], ' ');
111
 
          *p = '\0';
112
 
          setVirusName (&buf[4]);
113
 
          infected = AV_VIRUS;
114
 
        };
115
 
    }
116
 
  close (sock);
117
 
 
118
 
  return infected;
119
 
}
120
 
 
121
 
/* simple wrapper for reading lines from sockets */
122
 
int
123
 
KAV5Engine::recv_line (int _sock, char *_buf, int _size)
124
 
{
125
 
  char *p = _buf;
126
 
 
127
 
  // clear buffer
128
 
  memset (_buf, 0, _size);
129
 
 
130
 
  // read until \n
131
 
  while (recv (_sock, p, 1, 0) > -1)
132
 
    {
133
 
      if ((p - _buf) > (_size - 2))
134
 
        break;
135
 
      if (*p == '\n')
136
 
        break;
137
 
      if (*p != '\r')
138
 
        p++;
139
 
    }
140
 
  *p = '\0';
141
 
 
142
 
  return p - _buf;
143
 
}