Debian Codegraph @

src/ContentScanner.cpp

raw
1 // Implements CSPlugin class and cs_plugin_loader base class
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 #include <string>
9
10 #ifdef HAVE_CONFIG_H
11 #include "e2config.h"
12 #endif
13 #include "String.hpp"
14 #include "HTTPHeader.hpp"
15 #include "NaughtyFilter.hpp"
16 #include "ContentScanner.hpp"
17 #include "ConfigVar.hpp"
18 #include "OptionContainer.hpp"
19 #include "Logger.hpp"
20
21 #include <cstdlib>
22 #include <deque>
23 #include <iostream>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <cerrno>
29
30 // GLOBALS
31 extern OptionContainer o;
32
33 // find the class factory functions for the CS plugins we've been configured to build
34
35 #ifdef ENABLE_CLAMD
36 extern cscreate_t clamdcreate;
37 #endif
38
39 #ifdef ENABLE_AVASTD
40 extern cscreate_t avastdcreate;
41 #endif
42
43 #ifdef ENABLE_ICAP
44 extern cscreate_t icapcreate;
45 #endif
46
47 #ifdef ENABLE_KAVD
48 extern cscreate_t kavdcreate;
49 #endif
50
51 #ifdef ENABLE_COMMANDLINE
52 extern cscreate_t commandlinecreate;
53 #endif
54
55 // IMPLEMENTATION
56
57 // CSPlugin class
58
59 CSPlugin::CSPlugin(ConfigVar &definition)
60 : scanpost(false)
61 {
62 cv = definition;
63 }
64
65 // start the plugin - i.e. read in the configuration
66 int CSPlugin::init(void *args)
67 {
68 if (cv["scanpost"] == "on")
69 scanpost = true;
70 else
71 scanpost = false;
72
73 if (!readStandardLists()) { //always
74 return E2CS_ERROR; //include
75 } // these
76 return E2CS_OK;
77 }
78
79 // make a temporary file for storing data which is to be scanned
80 // returns FD in int and saves filename to String pointer
81 // filename is not used as input
82 int CSPlugin::makeTempFile(String *filename)
83 {
84 int tempfilefd;
85 String tempfilepath(o.content.download_dir.c_str());
86 tempfilepath += "/csXXXXXX";
87 char *tempfilepatharray = new char[tempfilepath.length() + 1];
88 strcpy(tempfilepatharray, tempfilepath.toCharArray());
89 // mode_t mask = umask(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); // this mask is reversed
90 umask(0007); // only allow access to e2g user and group
91 if ((tempfilefd = mkstemp(tempfilepatharray)) < 1) {
92 E2LOGGER_error("Could not create temp file for contentscanner.");
93 tempfilefd = -1;
94 } else {
95 (*filename) = tempfilepatharray;
96 }
97 delete[] tempfilepatharray;
98 return tempfilefd;
99 }
100
101 // write a temporary file containing the memory buffer which is to be scanned
102 // if your CS plugin does not have the ability to scan memory directly (e.g. clamdscan), this gets used by the default scanMemory to turn it into a file
103 int CSPlugin::writeMemoryTempFile(const char *object, unsigned int objectsize, String *filename)
104 {
105 int tempfd = makeTempFile(filename); // String gets modified
106 if (tempfd < 0) {
107 E2LOGGER_error("Error creating temp file in writeMemoryTempFile.");
108 return E2CS_ERROR;
109 }
110 errno = 0;
111 DEBUG_avscan("About to writeMemoryTempFile ", (*filename), " size: ", objectsize);
112
113 while (true) {
114 if (write(tempfd, object, objectsize) < 0) {
115 if (errno == EINTR) {
116 continue; // was interupted by a signal so restart
117 }
118 }
119 break; // end the while
120 }
121 close(tempfd); // finished writing so close file
122 return E2CS_OK; // all ok
123 }
124
125 // default implementation of scanMemory, which defers to scanFile.
126 int CSPlugin::scanMemory(HTTPHeader *requestheader, HTTPHeader *docheader, const char *user, FOptionContainer* &foc,
127 const char *ip, const char *object, unsigned int objectsize, NaughtyFilter *checkme,
128 const String *disposition, const String *mimetype)
129 {
130 // there is no capability to scan memory with some AV as we pass it
131 // a file name to scan. So we save the memory to disk and pass that.
132 // Then delete the temp file.
133 String tempfilepath;
134 if (writeMemoryTempFile(object, objectsize, &tempfilepath) != E2CS_OK) {
135 E2LOGGER_error("Error creating/writing temp file for scanMemory.");
136 return E2CS_SCANERROR;
137 }
138 int rc = scanFile(requestheader, docheader, user, foc, ip, tempfilepath.toCharArray(), checkme, disposition, mimetype);
139 #ifndef DEBUG_LOW
140 unlink(tempfilepath.toCharArray()); // delete temp file
141 #endif
142 return rc;
143 }
144
145 // read in all the lists of various things we do not wish to scan
146 bool CSPlugin::readStandardLists() // this is now done in Storyboard
147 {
148 return true;
149
150 #ifdef NOTDEF
151 exceptionvirusmimetypelist.reset(); // incase this is a reload
152 exceptionvirusextensionlist.reset();
153 exceptionvirussitelist.reset();
154 exceptionvirusurllist.reset();
155 if (!exceptionvirusmimetypelist.readItemList(cv["exceptionvirusmimetypelist"].toCharArray(), false, 0)) {
156 E2LOGGER_error("Error opening exceptionvirusmimetypelist");
157 return false;
158 }
159 exceptionvirusmimetypelist.doSort(false);
160 if (!exceptionvirusextensionlist.readItemList(cv["exceptionvirusextensionlist"].toCharArray(), false, 0)) {
161 E2LOGGER_error("Error opening exceptionvirusextensionlist");
162 return false;
163 }
164 exceptionvirusextensionlist.doSort(false);
165 if (!exceptionvirussitelist.readItemList(cv["exceptionvirussitelist"].toCharArray(), false, 0)) {
166 E2LOGGER_error("Error opening exceptionvirussitelist");
167 return false;
168 }
169 exceptionvirussitelist.doSort(false);
170 if (!exceptionvirusurllist.readItemList(cv["exceptionvirusurllist"].toCharArray(), true, 0)) {
171 E2LOGGER_error("Error opening exceptionvirusurllist");
172 return false;
173 }
174 exceptionvirusurllist.doSort(true);
175 return true;
176 #endif
177 }
178
179 // Test whether or not a particular request's incoming/outgoing data should be scanned.
180 // This is an early-stage (request headers only) test; no other info is known about
181 // the actual data itself when this is called.
182 int CSPlugin::willScanRequest(const String &url, const char *user, FOptionContainer* &foc,
183 const char *ip, bool post, bool reconstituted, bool exception, bool bypass)
184 {
185 // Most content scanners only deal with original, unmodified content
186 if (reconstituted) {
187 DEBUG_avscan("willScanRequest: ignoring reconstituted data");
188 return E2CS_NOSCAN;
189 }
190
191 // Deal with POST file uploads conditionally, but subject only to the "scanpost"
192 // option, not to the domain & URL lists - uploading files does not have the same
193 // implications as downlaoding them.
194 if (post) {
195 if (scanpost) {
196 DEBUG_avscan("willScanRequest: I'm interested in uploads");
197 return E2CS_NEEDSCAN;
198 } else {
199 DEBUG_avscan("willScanRequest: Not interested in uploads");
200 return E2CS_NOSCAN;
201 }
202 }
203
204
205 // all list checkng is now done in Storyboard request
206 // and filetype checking in Storyboard response
207 //
208 return E2CS_NEEDSCAN;
209
210 #ifdef NOTDEF
211 String urld(HTTPHeader::decode(url));
212 String lc;
213 urld.removeWhiteSpace();
214 urld.toLower();
215 urld.removePTP();
216 String domain, tempurl, foundurl, path;
217 unsigned int fl;
218 if (urld.contains("/")) {
219 domain = urld.before("/");
220 path = "/" + urld.after("/");
221 path.hexDecode();
222 path.realPath();
223 } else {
224 domain = urld;
225 }
226
227 // Don't scan the web server which hosts the access denied page
228 if (((foc->reporting_level == 1) || (foc->reporting_level == 2))
229 && domain.startsWith(foc->access_denied_domain)) {
230 DEBUG_avscan("willScanRequest: ignoring our own webserver");
231 return E2CS_NOSCAN;
232 }
233
234 // exceptionvirussitelist
235 tempurl = domain;
236 while (tempurl.contains(".")) {
237 if (exceptionvirussitelist.findInList(tempurl.toCharArray(), lc) != NULL) {
238 DEBUG_avscan("willScanRequest: ignoring exception virus site");
239 return E2CS_NOSCAN; // exact match
240 }
241 tempurl = tempurl.after("."); // check for being in higher level domains
242 }
243 if (tempurl.length() > 1) {
244 // allows matching of .tld
245 tempurl = "." + tempurl;
246 if (exceptionvirussitelist.findInList(tempurl.toCharArray(), lc) != NULL) {
247 DEBUG_avscan("willScanRequest: ignoring exception virus site");
248 return E2CS_NOSCAN; // exact match
249 }
250 }
251
252 // exceptionvirusurllist
253 tempurl = domain + path;
254 if (tempurl.endsWith("/")) {
255 tempurl.chop(); // chop off trailing / if any
256 }
257 while (tempurl.before("/").contains(".")) {
258 char *i = exceptionvirusurllist.findStartsWith(tempurl.toCharArray(),lc );
259 if (i != NULL) {
260 foundurl = i;
261 fl = foundurl.length();
262 if (tempurl.length() > fl) {
263 unsigned char c = tempurl[fl];
264 if (c == '/' || c == '?' || c == '&' || c == '=') {
265 DEBUG_avscan("willScanRequest: ignoring exception virus URL");
266 return E2CS_NOSCAN; // matches /blah/ or /blah/foo but not /blahfoo
267 }
268 } else {
269 DEBUG_avscan("willScanRequest: ignoring exception virus URL");
270 return E2CS_NOSCAN; // exact match
271 }
272 }
273 tempurl = tempurl.after("."); // check for being in higher level domains
274 }
275
276 DEBUG_avscan("willScanRequest: I'm interested");
277 return E2CS_NEEDSCAN;
278 #endif
279 }
280
281 // Test whether or not a particular request's incoming/outgoing data should be scanned.
282 // This is a later-stage test; info is known about the actual data itself when this is called.
283 int CSPlugin::willScanData(const String &url, const char *user, FOptionContainer* &foc, const char *ip, bool post,
284 bool reconstituted, bool exception, bool bypass, const String &disposition, const String &mimetype,
285 off_t size) {
286 // this function is no longer required as mime/extension exceptions are now handled by Storyboard response
287 return E2CS_NEEDSCAN; // match
288 }
289
290
291 //set the blocking information
292 void CSPlugin::blockFile(std::string *_category, std::string *_message, NaughtyFilter *checkme)
293 {
294 std::string category;
295 std::string message;
296
297 if (_category == NULL) {
298 category = "Content scanning";
299 } else {
300 category = *_category;
301 }
302 if (_message == NULL) {
303 message = lastvirusname.toCharArray();
304 } else {
305 message = *_message;
306 }
307
308 checkme->whatIsNaughty = o.language_list.getTranslation(1100);
309 if (message.length() > 0) {
310 checkme->whatIsNaughty += " ";
311 checkme->whatIsNaughty += message.c_str();
312 }
313
314 checkme->whatIsNaughtyLog = checkme->whatIsNaughty;
315 checkme->whatIsNaughtyCategories = category.c_str();
316 checkme->isItNaughty = true;
317 checkme->isException = false;
318 }
319
320 // take in a configuration file, find the CSPlugin class associated with the plugname variable, and return an instance
321 CSPlugin *cs_plugin_load(const char *pluginConfigPath)
322 {
323 ConfigVar cv;
324
325 if (cv.readVar(pluginConfigPath, "=") > 0) {
326 E2LOGGER_error("Unable to load plugin config ", pluginConfigPath);
327 return NULL;
328 }
329
330 String plugname(cv["plugname"]);
331 if (plugname.length() < 1) {
332 E2LOGGER_error("Unable read plugin config plugname variable %s",pluginConfigPath);
333 return NULL;
334 }
335
336 #ifdef ENABLE_CLAMD
337 if (plugname == "clamdscan") {
338 DEBUG_avscan("Enabling ClamDscan CS plugin");
339 return clamdcreate(cv);
340 }
341 #endif
342
343 #ifdef ENABLE_AVASTD
344 if (plugname == "avastdscan") {
345 DEBUG_avscan("Enabling AvastDscan CS plugin");
346 return avastdcreate(cv);
347 }
348 #endif
349
350 #ifdef ENABLE_KAVD
351 if (plugname == "kavdscan") {
352 DEBUG_avscan("Enabling KAVDscan CS plugin");
353 return kavdcreate(cv);
354 }
355 #endif
356
357 #ifdef ENABLE_ICAP
358 if (plugname == "icapscan") {
359 DEBUG_avscan("Enabling ICAPscan CS plugin");
360 return icapcreate(cv);
361 }
362 #endif
363
364 #ifdef ENABLE_COMMANDLINE
365 if (plugname == "commandlinescan") {
366 DEBUG_avscan("Enabling command-line CS plugin");
367 return commandlinecreate(cv);
368 }
369 #endif
370
371 E2LOGGER_error("Unable to load plugin ", pluginConfigPath);
372 return NULL;
373 }