Debian Codegraph @

src/contentscanners/avastdscan.cpp

raw
1 // AvastD 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 // INCLUDES
8 #ifdef HAVE_CONFIG_H
9 #include "e2config.h"
10 #endif
11
12 #include "../String.hpp"
13
14 #include "../ContentScanner.hpp"
15 #include "../UDSocket.hpp"
16 #include "../OptionContainer.hpp"
17 #include "../Logger.hpp"
18
19 #include <sys/stat.h>
20 #include <unistd.h>
21
22 // GLOBALS
23
24 extern OptionContainer o;
25
26 // DECLARATIONS
27
28 // class name is relevant!
29 class avastdinstance : public CSPlugin
30 {
31 public:
32 avastdinstance(ConfigVar &definition)
33 : CSPlugin(definition), archivewarn(false){};
34
35 // we are not replacing scanTest or scanMemory
36 // but for scanFile and the default scanMemory to work, we need a working scanFile implementation
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 // AvastD UNIX domain socket path
45 String udspath;
46 // Whether or not to just issue a warning on archive limit/encryption warnings
47 bool archivewarn;
48
49 static String encode(const String &Str);
50 // Set avastd protocol for new deamon version
51 String avastprotocol;
52 String scanreturncode;
53 };
54
55 // IMPLEMENTATION
56
57 // class factory code *MUST* be included in every plugin
58
59 CSPlugin *avastdcreate(ConfigVar &definition)
60 {
61 return new avastdinstance(definition);
62 }
63
64 // end of Class factory
65
66 // initialise the plugin
67 int avastdinstance::init(void *args)
68 {
69 int rc;
70 if ((rc = CSPlugin::init(args)) != E2CS_OK)
71 return rc;
72
73 // read in AvastD UNIX domain socket path
74 udspath = cv["avastdudsfile"];
75 if (udspath.length() < 3) {
76 E2LOGGER_error("Error reading avastdudsfile option.", udspath);
77 return E2CS_ERROR;
78 // it would be far better to do a test connection to the file but
79 // could not be arsed for now
80 }
81
82 archivewarn = cv["archivewarn"] == "on";
83 DEBUG_avscan("avastd configuration: archivewarn = ", archivewarn);
84 avastprotocol = cv["avastprotocol"];
85 if (avastprotocol.length() < 3) {
86 avastprotocol = "avast4";
87 E2LOGGER_error("avasd configuration missing avastprotocol: use ", avastprotocol);
88 }
89 if (avastprotocol.compare("avast4") != 0 && avastprotocol.compare("avast2014") != 0) {
90 E2LOGGER_error("Error reading avastprotocol option.");
91 return E2CS_ERROR;
92 }
93 DEBUG_avscan("avastd configuration: avastprotocol = ", avastprotocol);
94
95 // set some parameter by avastd protocol version
96 if (avastprotocol.compare("avast4") == 0) {
97 scanreturncode = "200 ";
98 } else
99 scanreturncode = "210 ";
100 return E2CS_OK;
101 }
102
103 // no need to replace the inheritied scanMemory() which just calls scanFile()
104 // there is no capability to scan memory with avastdscan as we pass it
105 // a file name to scan. So we save the memory to disk and pass that.
106 // Then delete the temp file.
107 int avastdinstance::scanFile(HTTPHeader *requestheader, HTTPHeader *docheader, const char *user,
108 FOptionContainer* &foc, const char *ip, const char *filename, NaughtyFilter *checkme,
109 const String *disposition, const String *mimetype)
110 {
111 lastmessage = lastvirusname = String();
112 // mkstemp seems to only set owner permissions, so our AV daemon won't be
113 // able to read the file, unless it's running as the same user as us. that's
114 // not usually very convenient. so instead, just allow group read on the
115 // file, and tell users to make sure the daemongroup option is friendly to
116 // the AV daemon's group membership.
117 // TODO? chmod can error out with EINTR, we may wish to ignore this
118 if (chmod(filename, S_IRGRP | S_IRUSR) != 0) {
119 lastmessage = "Error giving AvastD read access to temp file";
120 E2LOGGER_error("Could not change file ownership to give AvastD read access: ", strerror(errno));
121 return E2CS_SCANERROR;
122 };
123
124 UDSocket stripedsocks;
125 if (stripedsocks.getFD() < 0) {
126 lastmessage = "Error opening socket to talk to AvastD";
127 E2LOGGER_error("Error creating socket for talking to AvastD");
128 return E2CS_SCANERROR;
129 }
130 if (stripedsocks.connect(udspath.toCharArray()) < 0) {
131 lastmessage = "Error connecting to AvastD socket";
132 E2LOGGER_error("Error connecting to AvastD socket");
133 return E2CS_SCANERROR;
134 }
135
136 char buffer[4096];
137 int rc;
138 bool infected = false;
139 bool warning = false;
140 bool truncated = false;
141
142 try {
143 // After connecting, the daemon sends the following welcome message:
144 // 220 Welcome to avast! Virus scanning daemon x.x (VPS yy-yy dd.mm.yyyy)
145 rc = stripedsocks.getLine(buffer, sizeof(buffer), o.content.content_scanner_timeout);
146 DEBUG_avscan("Got from avastd: ", encode(buffer));
147
148 if (strncmp(buffer, "220 ", 4) != 0) {
149 lastmessage = "Unexpected reply during AvastD handshake: ";
150 String ebuffer(encode(buffer));
151 lastmessage += ebuffer;
152 E2LOGGER_error(lastmessage);
153 return E2CS_SCANERROR;
154 }
155 // Syntax:
156 // SCAN FileName (with some escaping)
157 String command("SCAN ");
158 command += encode(filename);
159 command += "\r\n";
160 DEBUG_avscan("avastd command: ", encode(command));
161 stripedsocks.writeString(command.toCharArray());
162
163 // Possible return codes:
164 // One of the following:
165 // 501 Syntax error in arguments
166 // 451 Engine error %d
167 // 200 OK
168
169 rc = stripedsocks.getLine(buffer, sizeof(buffer), o.content.content_scanner_timeout);
170 DEBUG_avscan("Got from avastd: ", encode(buffer));
171
172 if (strncmp(buffer, scanreturncode.toCharArray(), 4) != 0) {
173 lastmessage = "Unexpected reply to scan command: ";
174 String ebuffer(encode(buffer));
175 lastmessage += ebuffer;
176 E2LOGGER_error(lastmessage);
177 return E2CS_SCANERROR;
178 }
179
180 // Scan response format:
181 // avast4: Filepath\t[Status]\tMoreInfo
182 // avast2014: SCAN\sFilepath\t[Statos]\tMoreInfo\tVirusName
183 // where:
184 // \t is ASCII character 9 (tab)
185 // FilePath is full path to the scanned file
186 // [Status] is one of the following values
187 // [+] - scan succeeded, the file is clean
188 // [L] - scan succeeded, the file is infected, for more info see
189 // Following these lines there is a blank line which signals the end of data
190 // transter from the daemon side.
191
192 for (rc = stripedsocks.getLine(buffer, sizeof(buffer), o.content.content_scanner_timeout, NULL, &truncated);
193 rc > 0 && !truncated && buffer[0] != '\r';
194
195 rc = stripedsocks.getLine(buffer, sizeof(buffer), o.content.content_scanner_timeout, NULL, &truncated)) {
196 DEBUG_avscan("Got from avastd: ", encode(buffer));
197 // If a line can't fit in our buffer, we're probably dealing with a zip bomb or
198 // something similarly nasty. Let's consider it an error, whatever archivewarn says.
199 if (buffer[rc - 1] != '\r') {
200 lastmessage = "Error whilst reading AvastD socket: can't fit line in buffer.";
201 E2LOGGER_error(lastmessage);
202 return E2CS_SCANERROR;
203 }
204
205 // We're looking for this kind of string: ^[^\t]*\t\[.\](\t.*)?\r$
206 char *result = strchr(buffer, '\t');
207 if (strncmp(buffer, "200 ", 4) == 0 && avastprotocol.compare("avast2014") == 0) {
208 DEBUG_avscan("ignore 200 SCAN OK and exit loop");
209 break;
210 } else {
211 if ((avastprotocol.compare("avast4") == 0 && (result == NULL || result[1] != '[' || result[1] == '\0' || result[3] != ']' || (result[4] != '\t' && result[4] != '\r'))) || (avastprotocol.compare("avast2014") == 0 && (result == NULL || result[1] != '[' || result[1] == '\0' || result[3] != ']'))) {
212 lastmessage = "Unexpected reply in scan results: ";
213 String ebuffer(encode(buffer));
214 lastmessage += ebuffer;
215 E2LOGGER_error(lastmessage);
216 return E2CS_SCANERROR;
217 }
218 *result = '\0';
219 result += 5;
220 switch (result[-3]) {
221 case '+':
222 // Clean!
223 DEBUG_avscan("avastd result: ", encode(buffer) "\tclean!");
224 break;
225
226 case 'L':
227 // Infected!
228 DEBUG_avscan("avastd result: ", encode(buffer), "\tinfected with ", result);
229 if (!lastvirusname.empty())
230 lastvirusname += " ";
231 {
232 char *r = strchr(result, '\r');
233 lastvirusname += r == NULL ? result : String(result, r - result);
234 }
235 infected = true;
236 break;
237
238 default:
239 // Can't interpret result.
240 DEBUG_avscan("avastd result: ", encode(buffer), "\tcan't analyze (", result, ")" );
241 if (!lastvirusname.empty())
242 lastvirusname += " ";
243 lastvirusname += "Encrypted";
244 warning = true;
245 break;
246 }
247 }
248 }
249 } catch (std::exception &e) {
250 lastmessage = "Exception whilst reading AvastD socket: ";
251 lastmessage += e.what();
252 E2LOGGER_error(lastmessage);
253 return E2CS_SCANERROR;
254 }
255 DEBUG_avscan("avastd final result: infected: ", infected, "\twarning: ", warning, "\tlastvirusname: ", lastvirusname, "\ttruncated: ", truncated);
256
257 // Socket unexpectedly closed.
258 if (rc == 0 || truncated || (avastprotocol.compare("avast4") == 0 && buffer[0] != '\r')) {
259 lastmessage = "Error whilst reading AvastD socket: truncated data.";
260 E2LOGGER_error(lastmessage);
261 return E2CS_SCANERROR;
262 }
263
264 if (infected || (warning && archivewarn)) {
265 blockFile(NULL, NULL, checkme);
266 return E2CS_INFECTED;
267 }
268 return E2CS_CLEAN;
269 }
270
271 String avastdinstance::encode(const String &Str)
272 {
273 char Enc[Str.length() * 2];
274 char *p = Enc;
275
276 for (String::const_iterator i = Str.begin(); i != Str.end(); ++i)
277 switch (*i) {
278 case '\t':
279 *(p++) = '\\';
280 *(p++) = 't';
281 break;
282 case '\n':
283 *(p++) = '\\';
284 *(p++) = 'n';
285 break;
286 case '\r':
287 *(p++) = '\\';
288 *(p++) = 'r';
289 break;
290 case '\\':
291 *(p++) = '\\';
292 *(p++) = '\\';
293 break;
294 case '\0':
295 // This shouldn't happen.
296 DEBUG_avscan("Warning: '\\0' found in filename.");
297 *(p++) = '\\';
298 *(p++) = '0';
299 break;
300 default:
301 *(p++) = *i;
302 break;
303 }
304
305 // No need to allocate new memory if no escapes were inserted.
306 if (p - Enc == Str.length())
307 return String(Str);
308 else
309 return String(Enc, p - Enc);
310 }