Debian Codegraph @

src/contentscanners/kavdscan.cpp

raw
1 // Kaspersky AV Daemon content scanning plugin
2
3 // For all support, instructions and copyright go to:
4 // http://e2guardian.org/
5 // Released under the GPL v2, with the OpenSSL exception described in the README file.
6
7
8 // INCLUDES
9 #ifdef HAVE_CONFIG_H
10 #include "e2config.h"
11 #endif
12
13 #include "../String.hpp"
14
15 #include "../ContentScanner.hpp"
16 #include "../UDSocket.hpp"
17 #include "../OptionContainer.hpp"
18 #include "../Logger.hpp"
19
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25 // GLOBALS
26
27 extern OptionContainer o;
28
29 // IMPLEMENTATION
30
31 // class name is relevant
32 class kavdinstance : public CSPlugin
33 {
34 public:
35 kavdinstance(ConfigVar &definition)
36 : CSPlugin(definition){};
37 int scanFile(HTTPHeader *requestheader, HTTPHeader *docheader, const char *user, FOptionContainer* &foc,
38 const char *ip, const char *filename, NaughtyFilter *checkme,
39 const String *disposition, const String *mimetype);
40
41 int init(void *args);
42
43 private:
44 // UNIX domain socket path for KAVD
45 String udspath;
46 // File path prefix for chrooted KAVD
47 String pathprefix;
48 };
49
50 // class factory code *MUST* be included in every plugin
51
52 CSPlugin *kavdcreate(ConfigVar &definition)
53 {
54 return new kavdinstance(definition);
55 }
56
57 // end of Class factory
58
59 // initialise plugin
60 int kavdinstance::init(void *args)
61 {
62 int rc;
63 if ((rc = CSPlugin::init(args)) != E2CS_OK)
64 return rc;
65
66 udspath = cv["kavdudsfile"];
67 if (udspath.length() < 3) {
68 E2LOGGER_error("Error reading kavdudsfile option.");
69 return E2CS_ERROR;
70 // it would be far better to do a test connection to the file but
71 // could not be arsed for now
72 }
73
74 // read in path prefix
75 pathprefix = cv["pathprefix"];
76
77 return E2CS_OK;
78 }
79
80 // no need to replace the inheritied scanMemory() which just calls scanFile()
81 // there is no capability to scan memory with kavdscan as we pass it
82 // a file name to scan. So we save the memory to disk and pass that.
83 // Then delete the temp file.
84 int kavdinstance::scanFile(HTTPHeader *requestheader, HTTPHeader *docheader, const char *user, FOptionContainer* &foc,
85 const char *ip, const char *filename, NaughtyFilter *checkme, const String *disposition, const String *mimetype)
86 {
87 lastvirusname = lastmessage = "";
88 // mkstemp seems to only set owner permissions, so our AV daemon won't be
89 // able to read the file, unless it's running as the same user as us. that's
90 // not usually very convenient. so instead, just allow group read on the
91 // file, and tell users to make sure the daemongroup option is friendly to
92 // the AV daemon's group membership.
93 // chmod can error with EINTR, ignore this?
94 if (chmod(filename, S_IRGRP | S_IRUSR) != 0) {
95 E2LOGGER_error("Could not change file ownership to give kavd read access: ", strerror(errno));
96 return E2CS_SCANERROR;
97 };
98 String command("SCAN bPQRSTUW ");
99 if (pathprefix.length()) {
100 String fname(filename);
101 command += fname.after(pathprefix.toCharArray());
102 } else {
103 command += filename;
104 }
105 command += "\r\n";
106 DEBUG_avscan("kavdscan command:", command);
107
108 UDSocket stripedsocks;
109 if (stripedsocks.getFD() < 0) {
110 E2LOGGER_error("Error creating socket for talking to kavdscan");
111 return E2CS_SCANERROR;
112 }
113 if (stripedsocks.connect(udspath.toCharArray()) < 0) {
114 E2LOGGER_error("Error connecting to kavdscan socket");
115 stripedsocks.close();
116 return E2CS_SCANERROR;
117 }
118 char *buff = new char[4096];
119 memset(buff, 0, 4096);
120 int rc;
121 try {
122 // read kaspersky kavdscan (AV Enging Server) - format: 2xx greeting
123 rc = stripedsocks.getLine(buff, 4096, o.content.content_scanner_timeout);
124 } catch (std::exception &e) {
125 }
126 if (buff[0] != '2') {
127 delete[] buff;
128 stripedsocks.close();
129 E2LOGGER_error("kavdscan did not return ok");
130 return E2CS_SCANERROR;
131 }
132 try {
133 stripedsocks.writeString(command.toCharArray());
134 } catch (std::exception &e) {
135 delete[] buff;
136 stripedsocks.close();
137 E2LOGGER_error("unable to write to kavdscan");
138 return E2CS_SCANERROR;
139 }
140 try {
141 rc = stripedsocks.getLine(buff, 4096, o.content.content_scanner_timeout);
142 } catch (std::exception &e) {
143 delete[] buff;
144 stripedsocks.close();
145 E2LOGGER_error("Error reading kavdscan socket");
146 return E2CS_SCANERROR;
147 }
148 String reply(buff);
149 DEBUG_avscan("Got from kavdscan:", reply);
150
151 if (reply[0] == '2') { // clean
152 DEBUG_avscan("kavdscan - clean");
153 delete[] buff;
154 stripedsocks.close();
155 return E2CS_CLEAN;
156 }
157 if (reply.startsWith("322")) { // infected
158 // patch to handle multiple virii in kavd response
159 // originally submitted by cahya <[email protected]>
160 while (reply[0] != '2' && rc != 0) {
161 reply.removeWhiteSpace();
162 lastvirusname = lastvirusname + " " + reply.after("322-").before(" ");
163 try {
164 rc = stripedsocks.getLine(buff, 4096, o.content.content_scanner_timeout);
165 } catch (std::exception &e) {
166 delete[] buff;
167 stripedsocks.close();
168 E2LOGGER_error("Error reading kavdscan socket");
169 return E2CS_SCANERROR;
170 }
171 reply = buff;
172 DEBUG_avscan("Got from kavdscan:", reply);
173 }
174 E2LOGGER_error("lastvirusname: ", lastvirusname);
175 delete[] buff;
176 stripedsocks.close();
177
178 // format: 322 nastyvirus blah
179 blockFile(NULL, NULL, checkme);
180 return E2CS_INFECTED;
181 }
182 delete[] buff;
183 stripedsocks.close();
184 // must be an error then
185 lastmessage = reply;
186 return E2CS_SCANERROR;
187 }