| 1 |
// ICAP server 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 "../OptionContainer.hpp" |
| 16 |
#include "../Logger.hpp" |
| 17 |
|
| 18 |
#include <string.h> |
| 19 |
#include <sys/time.h> |
| 20 |
#include <sys/types.h> |
| 21 |
#include <fcntl.h> |
| 22 |
#include <unistd.h> |
| 23 |
#include <netdb.h> // for gethostby |
| 24 |
#include <cstdio> |
| 25 |
|
| 26 |
// DEFINES |
| 27 |
|
| 28 |
#define ICAP_CONTINUE E2CS_MAX + 1 |
| 29 |
#define ICAP_NODATA E2CS_MAX + 2 |
| 30 |
|
| 31 |
// GLOBALS |
| 32 |
|
| 33 |
extern OptionContainer o; |
| 34 |
|
| 35 |
// DECLARATIONS |
| 36 |
|
| 37 |
// class name is relevant! |
| 38 |
class icapinstance : public CSPlugin |
| 39 |
{ |
| 40 |
public: |
| 41 |
icapinstance(ConfigVar &definition) |
| 42 |
: CSPlugin(definition), usepreviews(false), previewsize(0), supportsXIF(false), needsBody(false){}; |
| 43 |
|
| 44 |
int willScanRequest(const String &url, const char *user, FOptionContainer* &foc, const char *ip, bool post, |
| 45 |
bool reconstituted, bool exception, bool bypass); |
| 46 |
|
| 47 |
int scanMemory(HTTPHeader *requestheader, HTTPHeader *docheader, const char *user, FOptionContainer* &foc, |
| 48 |
const char *ip, const char *object, unsigned int objectsize, NaughtyFilter *checkme, |
| 49 |
const String *disposition, const String *mimetype); |
| 50 |
int scanFile(HTTPHeader *requestheader, HTTPHeader *docheader, const char *user, FOptionContainer* &foc, |
| 51 |
const char *ip, const char *filename, NaughtyFilter *checkme, |
| 52 |
const String *disposition, const String *mimetype); |
| 53 |
|
| 54 |
int init(void *args); |
| 55 |
|
| 56 |
private: |
| 57 |
// ICAP server hostname, IP and port |
| 58 |
String icaphost; |
| 59 |
String icapip; |
| 60 |
unsigned int icapport; |
| 61 |
// URL for the AV service |
| 62 |
String icapurl; |
| 63 |
// whether or not to send ICAP message previews, and the preview object size |
| 64 |
bool usepreviews; |
| 65 |
unsigned int previewsize; |
| 66 |
// supports X-Infection-Found and/or needs us to look at the whole body |
| 67 |
bool supportsXIF; |
| 68 |
bool needsBody; |
| 69 |
|
| 70 |
// Send ICAP request headers to server |
| 71 |
bool doHeaders(Socket &icapsock, HTTPHeader *reqheader, HTTPHeader *respheader, unsigned int objectsize); |
| 72 |
// Check data returned from ICAP server and return one of our standard return codes |
| 73 |
int doScan(Socket &icapsock, HTTPHeader *docheader, const char *object, unsigned int objectsize, NaughtyFilter *checkme); |
| 74 |
}; |
| 75 |
|
| 76 |
// IMPLEMENTATION |
| 77 |
|
| 78 |
// class factory code *MUST* be included in every plugin |
| 79 |
|
| 80 |
CSPlugin *icapcreate(ConfigVar &definition) |
| 81 |
{ |
| 82 |
return new icapinstance(definition); |
| 83 |
} |
| 84 |
|
| 85 |
// end of Class factory |
| 86 |
|
| 87 |
// don't scan POST data or reconstituted data - wouldn't work for multi-part posts |
| 88 |
// without faking request headers, as we are only passed a single part, not the whole request verbatim |
| 89 |
int icapinstance::willScanRequest(const String &url, const char *user, FOptionContainer* &foc, const char *ip, |
| 90 |
bool post, bool reconstituted, bool exception, bool bypass) |
| 91 |
{ |
| 92 |
if (post || reconstituted) |
| 93 |
return E2CS_NOSCAN; |
| 94 |
else { |
| 95 |
return CSPlugin::willScanRequest(url, user, foc, ip, |
| 96 |
post, reconstituted, exception, bypass); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
// initialise the plugin - determine icap ip, port & url |
| 101 |
int icapinstance::init(void *args) |
| 102 |
{ |
| 103 |
// always include these lists |
| 104 |
if (!readStandardLists()) { |
| 105 |
return E2CS_ERROR; |
| 106 |
} |
| 107 |
|
| 108 |
icapurl = cv["icapurl"]; // format: icap://icapserver:1344/avscan |
| 109 |
if (icapurl.length() < 3) { |
| 110 |
E2LOGGER_error("Error reading icapurl option."); |
| 111 |
return E2CS_ERROR; |
| 112 |
// it would be far better to do a test connection |
| 113 |
} |
| 114 |
icaphost = icapurl.after("//"); |
| 115 |
icapport = icaphost.after(":").before("/").toInteger(); |
| 116 |
if (icapport == 0) { |
| 117 |
icapport = 1344; |
| 118 |
} |
| 119 |
icaphost = icaphost.before("/"); |
| 120 |
if (icaphost.contains(":")) { |
| 121 |
icaphost = icaphost.before(":"); |
| 122 |
} |
| 123 |
struct hostent *host; |
| 124 |
if ((host = gethostbyname(icaphost.toCharArray())) == 0) { |
| 125 |
E2LOGGER_error("Error resolving icap host address."); |
| 126 |
return E2CS_ERROR; |
| 127 |
} |
| 128 |
icapip = inet_ntoa(*(struct in_addr *)host->h_addr_list[0]); |
| 129 |
|
| 130 |
DEBUG_avscan("ICAP server is ", icapip ); |
| 131 |
|
| 132 |
// try to connect to the ICAP server and perform an OPTIONS request |
| 133 |
Socket icapsock; |
| 134 |
try { |
| 135 |
if (icapsock.connect(icapip.toCharArray(), icapport) < 0) { |
| 136 |
throw std::runtime_error("Could not connect to server"); |
| 137 |
} |
| 138 |
String line("OPTIONS " + icapurl + " ICAP/1.0\r\nHost: " + icaphost + "\r\n\r\n"); |
| 139 |
icapsock.writeString(line.toCharArray()); |
| 140 |
// parse the response |
| 141 |
char buff[8192]; |
| 142 |
// first line - look for 200 OK |
| 143 |
icapsock.getLine(buff, 8192, o.content.content_scanner_timeout); |
| 144 |
line = buff; |
| 145 |
|
| 146 |
DEBUG_avscan("ICAP/1.0 OPTIONS response: ", line); |
| 147 |
|
| 148 |
if (line.after(" ").before(" ") != "200") { |
| 149 |
E2LOGGER_error("ICAP response not 200 OK"); |
| 150 |
return E2CS_WARNING; |
| 151 |
//throw std::runtime_error("Response not 200 OK"); |
| 152 |
} |
| 153 |
while (icapsock.getLine(buff, 8192, o.content.content_scanner_timeout) > 0) { |
| 154 |
line = buff; |
| 155 |
|
| 156 |
if (line.startsWith("\r")) { |
| 157 |
break; |
| 158 |
} else if (line.startsWith("Preview:")) { |
| 159 |
previewsize = line.after(": ").toInteger(); |
| 160 |
if (previewsize > 0) |
| 161 |
usepreviews = true; |
| 162 |
} else if (line.startsWith("Server:")) { |
| 163 |
if (line.contains("AntiVir-WebGate")) { |
| 164 |
needsBody = true; |
| 165 |
} |
| 166 |
} else if (line.startsWith("Service-ID:")) { |
| 167 |
if (line.contains("KAVIcap")) { |
| 168 |
needsBody = true; |
| 169 |
} |
| 170 |
} else if (line.startsWith("X-Allow-Out:")) { |
| 171 |
if (line.contains("X-Infection-Found")) { |
| 172 |
supportsXIF = true; |
| 173 |
} |
| 174 |
// Dr web bug ICAP response header without suportXIF |
| 175 |
} else if (line.startsWith("Service: Dr.Web")) { |
| 176 |
supportsXIF = true; |
| 177 |
} |
| 178 |
|
| 179 |
DEBUG_avscan("ICAP/1.0 OPTIONS response part: ", line); |
| 180 |
} |
| 181 |
icapsock.close(); |
| 182 |
} catch (std::exception &e) { |
| 183 |
E2LOGGER_error("ICAP server did not respond to OPTIONS request: ", e.what()); |
| 184 |
return E2CS_ERROR; |
| 185 |
} |
| 186 |
|
| 187 |
if (usepreviews){ |
| 188 |
DEBUG_avscan( "Message previews enabled; size: ", previewsize); |
| 189 |
} else { |
| 190 |
DEBUG_avscan("Message previews enabled; size: disabled"); |
| 191 |
} |
| 192 |
|
| 193 |
return E2CS_OK; |
| 194 |
} |
| 195 |
|
| 196 |
// send memory buffer to ICAP server for scanning |
| 197 |
int icapinstance::scanMemory(HTTPHeader *requestheader, HTTPHeader *docheader, const char *user, FOptionContainer* &foc, |
| 198 |
const char *ip, const char *object, unsigned int objectsize, NaughtyFilter *checkme, |
| 199 |
const String *disposition, const String *mimetype) |
| 200 |
{ |
| 201 |
lastvirusname = lastmessage = ""; |
| 202 |
|
| 203 |
Socket icapsock; |
| 204 |
|
| 205 |
if (not doHeaders(icapsock, requestheader, docheader, objectsize)) { |
| 206 |
icapsock.close(); |
| 207 |
return E2CS_SCANERROR; |
| 208 |
} |
| 209 |
|
| 210 |
if (usepreviews && (objectsize > previewsize)){ |
| 211 |
DEBUG_avscan("Sending memory date to icap preview first"); |
| 212 |
} |
| 213 |
|
| 214 |
unsigned int sent = 0; |
| 215 |
if (usepreviews && (objectsize > previewsize)) { |
| 216 |
try { |
| 217 |
if (!icapsock.writeToSocket(object, previewsize, 0, o.content.content_scanner_timeout)) { |
| 218 |
throw std::runtime_error("standard error"); |
| 219 |
} |
| 220 |
sent += previewsize; |
| 221 |
icapsock.writeString("\r\n0\r\n\r\n"); |
| 222 |
int rc = doScan(icapsock, docheader, object, objectsize, checkme); |
| 223 |
if (rc != ICAP_CONTINUE) |
| 224 |
return rc; |
| 225 |
// some servers send "continue" immediately followed by another response |
| 226 |
if (icapsock.checkForInput()) { |
| 227 |
int rc = doScan(icapsock, docheader, object, objectsize, checkme); |
| 228 |
if (rc != ICAP_NODATA) |
| 229 |
return rc; |
| 230 |
} |
| 231 |
char objectsizehex[32]; |
| 232 |
snprintf(objectsizehex, sizeof(objectsizehex), "%x\r\n", objectsize - previewsize); |
| 233 |
icapsock.writeString(objectsizehex); |
| 234 |
} catch (std::exception &e) { |
| 235 |
|
| 236 |
DEBUG_avscan("Exception sending message preview to ICAP: ", e.what()); |
| 237 |
// this *might* just be an early response & closed connection |
| 238 |
if (icapsock.checkForInput()) { |
| 239 |
int rc = doScan(icapsock, docheader, object, objectsize, checkme); |
| 240 |
if (rc != ICAP_NODATA) |
| 241 |
return rc; |
| 242 |
} |
| 243 |
icapsock.close(); |
| 244 |
lastmessage = "Exception sending message preview to ICAP"; |
| 245 |
E2LOGGER_error(lastmessage, e.what()); |
| 246 |
return E2CS_SCANERROR; |
| 247 |
} |
| 248 |
} |
| 249 |
try { |
| 250 |
if(icapsock.writeToSocket(object + sent, objectsize - sent, 0, o.content.content_scanner_timeout)) { |
| 251 |
|
| 252 |
DEBUG_avscan("total sent to icap: ", objectsize); |
| 253 |
|
| 254 |
icapsock.writeString("\r\n0\r\n\r\n"); // end marker |
| 255 |
DEBUG_avscan("memory was sent to icap"); |
| 256 |
|
| 257 |
} |
| 258 |
} catch (std::exception &e) { |
| 259 |
|
| 260 |
DEBUG_avscan("Exception sending memory file to ICAP: ", e.what()); |
| 261 |
|
| 262 |
// this *might* just be an early response & closed connection |
| 263 |
if (icapsock.checkForInput()) { |
| 264 |
int rc = doScan(icapsock, docheader, object, objectsize, checkme); |
| 265 |
if (rc != ICAP_NODATA) |
| 266 |
return rc; |
| 267 |
} |
| 268 |
icapsock.close(); |
| 269 |
lastmessage = "Exception sending memory file to ICAP"; |
| 270 |
E2LOGGER_error(lastmessage, e.what()); |
| 271 |
return E2CS_SCANERROR; |
| 272 |
} |
| 273 |
|
| 274 |
return doScan(icapsock, docheader, object, objectsize, checkme); |
| 275 |
} |
| 276 |
|
| 277 |
// send file contents for scanning |
| 278 |
int icapinstance::scanFile(HTTPHeader *requestheader, HTTPHeader *docheader, const char *user, |
| 279 |
FOptionContainer* &foc, const char *ip, const char *filename, NaughtyFilter *checkme, |
| 280 |
const String *disposition, const String *mimetype) |
| 281 |
{ |
| 282 |
lastmessage = lastvirusname = ""; |
| 283 |
int filefd = open(filename, O_RDONLY); |
| 284 |
if (filefd < 0) { |
| 285 |
|
| 286 |
DEBUG_avscan("Error opening file (", filename, "): ", strerror(errno)); |
| 287 |
|
| 288 |
lastmessage = "Error opening file to send to ICAP"; |
| 289 |
E2LOGGER_error(lastmessage, strerror(errno)); |
| 290 |
return E2CS_SCANERROR; |
| 291 |
} |
| 292 |
lseek(filefd, 0, SEEK_SET); |
| 293 |
unsigned int filesize = lseek(filefd, 0, SEEK_END); |
| 294 |
|
| 295 |
Socket icapsock; |
| 296 |
if (not doHeaders(icapsock, requestheader, docheader, filesize)) { |
| 297 |
icapsock.close(); |
| 298 |
close(filefd); |
| 299 |
return E2CS_SCANERROR; |
| 300 |
} |
| 301 |
|
| 302 |
lseek(filefd, 0, SEEK_SET); |
| 303 |
unsigned int sent = 0; |
| 304 |
char *data = new char[previewsize]; |
| 305 |
char *object = new char[100]; |
| 306 |
int objectsize = 0; |
| 307 |
|
| 308 |
DEBUG_avscan("About to send file data to icap"); |
| 309 |
|
| 310 |
if (usepreviews && (filesize > previewsize)) { |
| 311 |
try { |
| 312 |
while (sent < previewsize) { |
| 313 |
int rc = read(filefd, data, previewsize); |
| 314 |
if (rc < 0) { |
| 315 |
throw std::runtime_error("could not read from file"); |
| 316 |
} |
| 317 |
if (rc == 0) { |
| 318 |
break; // should never happen |
| 319 |
} |
| 320 |
if (!icapsock.writeToSocket(data, rc, 0, o.content.content_scanner_timeout)) { |
| 321 |
throw std::runtime_error("could not write to socket"); |
| 322 |
} |
| 323 |
memcpy(object, data, (rc > 100) ? 100 : rc); |
| 324 |
objectsize += (rc > 100) ? 100 : rc; |
| 325 |
sent += rc; |
| 326 |
} |
| 327 |
icapsock.writeString("\r\n0\r\n\r\n"); |
| 328 |
int rc = doScan(icapsock, docheader, object, objectsize, checkme); |
| 329 |
if (rc != ICAP_CONTINUE) { |
| 330 |
delete[] data; |
| 331 |
close(filefd); |
| 332 |
return rc; |
| 333 |
} |
| 334 |
// some servers send "continue" immediately followed by another response |
| 335 |
if (icapsock.checkForInput()) { |
| 336 |
int rc = doScan(icapsock, docheader, object, objectsize, checkme); |
| 337 |
if (rc != ICAP_NODATA) { |
| 338 |
delete[] data; |
| 339 |
close(filefd); |
| 340 |
return rc; |
| 341 |
} |
| 342 |
} |
| 343 |
char objectsizehex[32]; |
| 344 |
snprintf(objectsizehex, sizeof(objectsizehex), "%x\r\n", filesize - previewsize); |
| 345 |
icapsock.writeString(objectsizehex); |
| 346 |
} catch (std::exception &e) { |
| 347 |
|
| 348 |
icapsock.close(); |
| 349 |
lastmessage = "Exception sending message preview to ICAP"; |
| 350 |
E2LOGGER_error(lastmessage, e.what()); |
| 351 |
DEBUG_avscan(lastmessage, e.what()); |
| 352 |
delete[] data; |
| 353 |
close(filefd); |
| 354 |
// this *might* just be an early response & closed connection |
| 355 |
if (icapsock.checkForInput()) { |
| 356 |
int rc = doScan(icapsock, docheader, object, objectsize, checkme); |
| 357 |
if (rc != ICAP_NODATA) |
| 358 |
return rc; |
| 359 |
} |
| 360 |
return E2CS_SCANERROR; |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
delete[] data; |
| 365 |
data = new char[256 * 1024]; // 256k |
| 366 |
|
| 367 |
try { |
| 368 |
while (sent < filesize) { |
| 369 |
int rc = read(filefd, data, 256 * 1024); |
| 370 |
DEBUG_avscan("reading icap file rc: ", rc); |
| 371 |
if (rc < 0) { |
| 372 |
DEBUG_avscan("error reading icap file so throwing exception"); |
| 373 |
throw std::runtime_error("could not read from file"); |
| 374 |
} |
| 375 |
if (rc == 0) { |
| 376 |
DEBUG_avscan("got zero bytes reading icap file"); |
| 377 |
break; // should never happen |
| 378 |
} |
| 379 |
memcpy(object + objectsize, data, (rc > (100 - objectsize)) ? (100 - objectsize) : rc); |
| 380 |
objectsize += (rc > (100 - objectsize)) ? (100 - objectsize) : rc; |
| 381 |
if (!icapsock.writeToSocket(data, rc, 0, o.content.content_scanner_timeout)) { |
| 382 |
throw std::runtime_error("Unable to write to socket"); |
| 383 |
}; |
| 384 |
sent += rc; |
| 385 |
} |
| 386 |
|
| 387 |
DEBUG_avscan("total sent to icap: ", sent); |
| 388 |
|
| 389 |
icapsock.writeString("\r\n0\r\n\r\n"); // end marker |
| 390 |
|
| 391 |
DEBUG_avscan("file was sent to icap"); |
| 392 |
|
| 393 |
} catch (std::exception &e) { |
| 394 |
|
| 395 |
DEBUG_avscan( "Exception sending file to ICAP: ", e.what()); |
| 396 |
lastmessage = "Exception sending file to ICAP"; |
| 397 |
E2LOGGER_error(lastmessage, e.what()); |
| 398 |
delete[] data; |
| 399 |
close(filefd); |
| 400 |
// this *might* just be an early response & closed connection |
| 401 |
if (icapsock.checkForInput()) { |
| 402 |
int rc = doScan(icapsock, docheader, object, objectsize, checkme); |
| 403 |
if (rc != ICAP_NODATA) |
| 404 |
return rc; |
| 405 |
} |
| 406 |
return E2CS_SCANERROR; |
| 407 |
} |
| 408 |
close(filefd); |
| 409 |
delete[] data; |
| 410 |
return doScan(icapsock, docheader, object, objectsize, checkme); |
| 411 |
} |
| 412 |
|
| 413 |
// send ICAP request headers, returning success or failure |
| 414 |
bool icapinstance::doHeaders(Socket &icapsock, HTTPHeader *reqheader, HTTPHeader *respheader, unsigned int objectsize) |
| 415 |
{ |
| 416 |
int rc = icapsock.connect(icapip.toCharArray(), icapport); |
| 417 |
if (rc) { |
| 418 |
lastmessage = "Error connecting to ICAP server"; |
| 419 |
DEBUG_avscan(lastmessage); |
| 420 |
E2LOGGER_error(lastmessage); |
| 421 |
return false; |
| 422 |
} |
| 423 |
char objectsizehex[32]; |
| 424 |
// encapsulated HTTP request header: |
| 425 |
// use a dummy unless it proves absolutely necessary to do otherwise, |
| 426 |
// as using real data could lead to e.g. yet another source of password |
| 427 |
// leakage over the network. |
| 428 |
String encapsulatedheader("GET " + reqheader->getUrl() + " HTTP/1.0\r\n\r\n"); |
| 429 |
// body chunk size in hex - either full body, or just preview |
| 430 |
if (usepreviews && (objectsize > previewsize)) { |
| 431 |
snprintf(objectsizehex, sizeof(objectsizehex), "%x\r\n", previewsize); |
| 432 |
} else { |
| 433 |
snprintf(objectsizehex, sizeof(objectsizehex), "%x\r\n", objectsize); |
| 434 |
} |
| 435 |
// encapsulated HTTP response header: |
| 436 |
// use real data, because scanners can use this to aid the process |
| 437 |
/*String httpresponseheader; |
| 438 |
for (std::deque<String>::iterator i = respheader->header.begin(); i != respheader->header.end(); i++) { |
| 439 |
httpresponseheader += (*i) + "\r\n"; |
| 440 |
} |
| 441 |
httpresponseheader += "\r\n";*/ |
| 442 |
String httpresponseheader("HTTP/1.0 200 OK\r\n\r\n"); |
| 443 |
// ICAP header itself |
| 444 |
String icapheader("RESPMOD " + icapurl + " ICAP/1.0\r\nHost: " + icaphost + "\r\nAllow: 204\r\nEncapsulated: req-hdr=0, res-hdr=" + String(encapsulatedheader.length()) + ", res-body=" + String(httpresponseheader.length() + encapsulatedheader.length())); |
| 445 |
if (usepreviews && (objectsize > previewsize)) { |
| 446 |
icapheader += "\r\nPreview: " + String(previewsize); |
| 447 |
} |
| 448 |
icapheader += "\r\n\r\n"; |
| 449 |
|
| 450 |
DEBUG_avscan("About to send icapheader:\n", icapheader, encapsulatedheader, httpresponseheader, objectsizehex); |
| 451 |
|
| 452 |
try { |
| 453 |
icapsock.writeString(icapheader.toCharArray()); |
| 454 |
icapsock.writeString(encapsulatedheader.toCharArray()); |
| 455 |
icapsock.writeString(httpresponseheader.toCharArray()); |
| 456 |
icapsock.writeString(objectsizehex); |
| 457 |
} catch (std::exception &e) { |
| 458 |
|
| 459 |
lastmessage = "Exception sending headers to ICAP"; |
| 460 |
DEBUG_avscan(lastmessage, e.what()); |
| 461 |
E2LOGGER_error(lastmessage, e.what()); |
| 462 |
return false; |
| 463 |
} |
| 464 |
return true; |
| 465 |
} |
| 466 |
|
| 467 |
// check data received from ICAP server and interpret as virus name & return value |
| 468 |
int icapinstance::doScan(Socket &icapsock, HTTPHeader *docheader, const char *object, unsigned int objectsize, NaughtyFilter *checkme) |
| 469 |
{ |
| 470 |
char *data = new char[8192]; |
| 471 |
try { |
| 472 |
String line; |
| 473 |
int rc = icapsock.getLine(data, 8192, o.content.content_scanner_timeout); |
| 474 |
if (rc == 0) |
| 475 |
return ICAP_NODATA; |
| 476 |
line = data; |
| 477 |
DEBUG_avscan("reply from icap: ", line); |
| 478 |
|
| 479 |
// reply is of the format: |
| 480 |
// ICAP/1.0 204 No Content Necessary (etc) |
| 481 |
|
| 482 |
String returncode(line.after(" ").before(" ")); |
| 483 |
|
| 484 |
if (returncode == "204") { |
| 485 |
DEBUG_avscan("ICAP says clean!"); |
| 486 |
delete[] data; |
| 487 |
return E2CS_CLEAN; |
| 488 |
} else if (returncode == "100") { |
| 489 |
|
| 490 |
DEBUG_avscan("ICAP says continue!"); |
| 491 |
|
| 492 |
// discard rest of headers (usually just a blank line) |
| 493 |
// this is so we are in the right place in the data stream to |
| 494 |
// call doScan() again later, because people like Symantec seem |
| 495 |
// to think sending code 100 then code 204 one after the other |
| 496 |
// is not an abuse of the ICAP specification. |
| 497 |
while (icapsock.getLine(data, 8192, o.content.content_scanner_timeout) > 0) { |
| 498 |
if (data[0] == 13) |
| 499 |
break; |
| 500 |
} |
| 501 |
delete[] data; |
| 502 |
return ICAP_CONTINUE; |
| 503 |
} else if (returncode == "200") { |
| 504 |
|
| 505 |
DEBUG_avscan("ICAP says maybe not clean!"); |
| 506 |
|
| 507 |
while (icapsock.getLine(data, 8192, o.content.content_scanner_timeout) > 0) { |
| 508 |
if (data[0] == 13) // end marker |
| 509 |
break; |
| 510 |
line = data; |
| 511 |
// Symantec's engine gives us the virus name in the ICAP headers |
| 512 |
if (supportsXIF && line.startsWith("X-Infection-Found")) { |
| 513 |
DEBUG_avscan("ICAP says infected! (X-Infection-Found)"); |
| 514 |
lastvirusname = line.after("Threat=").before(";"); |
| 515 |
delete[] data; |
| 516 |
|
| 517 |
blockFile(NULL, NULL, checkme); |
| 518 |
return E2CS_INFECTED; |
| 519 |
} |
| 520 |
} |
| 521 |
// AVIRA's and KAV Antivir gives us 200 in all cases, so |
| 522 |
// - unfortunately - we must pay attention to the encapsulated |
| 523 |
// header/body. |
| 524 |
if (needsBody) { |
| 525 |
// grab & compare the HTTP return code from modified response |
| 526 |
// if it's been modified, assume there's an infection |
| 527 |
icapsock.getLine(data, 8192, o.content.content_scanner_timeout); |
| 528 |
line = data; |
| 529 |
|
| 530 |
DEBUG_avscan( "Comparing original return code to modified:", docheader->header.front(), " ", line); |
| 531 |
|
| 532 |
int respmodReturnCode = line.after(" ").before(" ").toInteger(); |
| 533 |
if (respmodReturnCode != docheader->returnCode()) { |
| 534 |
|
| 535 |
DEBUG_avscan("ICAP says infected! (returned header comparison)"); |
| 536 |
|
| 537 |
delete[] data; |
| 538 |
lastvirusname = "Unknown"; |
| 539 |
|
| 540 |
blockFile(NULL, NULL, checkme); |
| 541 |
return E2CS_INFECTED; |
| 542 |
} |
| 543 |
// ok - headers were identical, so look at encapsulated body |
| 544 |
// discard the rest of the encapsulated headers |
| 545 |
while (icapsock.getLine(data, 8192, o.content.content_scanner_timeout) > 0) { |
| 546 |
if (data[0] == 13) |
| 547 |
break; |
| 548 |
} |
| 549 |
// grab body chunk size |
| 550 |
icapsock.getLine(data, 8192, o.content.content_scanner_timeout); |
| 551 |
line = data; |
| 552 |
|
| 553 |
DEBUG_avscan("Comparing original body data to modified"); |
| 554 |
|
| 555 |
int bodysize = line.hexToInteger(); |
| 556 |
// get, say, the first 100 bytes and compare them to what we |
| 557 |
// originally sent to see if it has been modified |
| 558 |
unsigned int chunksize = (bodysize < 100) ? bodysize : 100; |
| 559 |
if (chunksize > objectsize) |
| 560 |
chunksize = objectsize; |
| 561 |
icapsock.readFromSocket(data, chunksize, 0, o.content.content_scanner_timeout); |
| 562 |
if (memcmp(data, object, chunksize) == 0) { |
| 563 |
|
| 564 |
DEBUG_avscan("ICAP says clean! (body byte comparison)"); |
| 565 |
|
| 566 |
delete[] data; |
| 567 |
return E2CS_CLEAN; |
| 568 |
} else { |
| 569 |
|
| 570 |
DEBUG_avscan("ICAP says infected! (body byte comparison)"); |
| 571 |
|
| 572 |
delete[] data; |
| 573 |
lastvirusname = "Unknown"; |
| 574 |
|
| 575 |
blockFile(NULL, NULL, checkme); |
| 576 |
return E2CS_INFECTED; |
| 577 |
} |
| 578 |
} |
| 579 |
// even if we don't find an X-Infection-Found header, |
| 580 |
// the file is still infected! |
| 581 |
|
| 582 |
DEBUG_avscan("ICAP says infected! (no further tests)"); |
| 583 |
|
| 584 |
delete[] data; |
| 585 |
lastvirusname = "Unknown"; |
| 586 |
|
| 587 |
blockFile(NULL, NULL, checkme); |
| 588 |
return E2CS_INFECTED; |
| 589 |
} else if (returncode == "404") { |
| 590 |
|
| 591 |
DEBUG_avscan("ICAP says no such service!"); |
| 592 |
|
| 593 |
lastmessage = "ICAP reports no such service"; |
| 594 |
E2LOGGER_error(lastmessage, " check your server URL"); |
| 595 |
delete[] data; |
| 596 |
return E2CS_SCANERROR; |
| 597 |
} else { |
| 598 |
|
| 599 |
lastmessage = "ICAP returned unrecognised response code."; |
| 600 |
DEBUG_avscan(lastmessage, returncode); |
| 601 |
E2LOGGER_error(lastmessage, returncode); |
| 602 |
delete[] data; |
| 603 |
return E2CS_SCANERROR; |
| 604 |
} |
| 605 |
delete[] data; |
| 606 |
} catch (std::exception &e) { |
| 607 |
|
| 608 |
lastmessage = "Exception getting reply from ICAP."; |
| 609 |
E2LOGGER_error(lastmessage, e.what()); |
| 610 |
DEBUG_avscan(lastmessage, e.what()); |
| 611 |
delete[] data; |
| 612 |
return E2CS_SCANERROR; |
| 613 |
} |
| 614 |
// it is generally NOT a good idea, when using virus scanning, |
| 615 |
// to continue as if nothing went wrong by default! |
| 616 |
return E2CS_SCANERROR; |
| 617 |
} |