| 1 |
// ClamD 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/time.h> |
| 20 |
#include <sys/types.h> |
| 21 |
#include <sys/stat.h> |
| 22 |
#include <unistd.h> |
| 23 |
|
| 24 |
// GLOBALS |
| 25 |
|
| 26 |
extern OptionContainer o; |
| 27 |
|
| 28 |
// DECLARATIONS |
| 29 |
|
| 30 |
// class name is relevant! |
| 31 |
class clamdinstance : public CSPlugin |
| 32 |
{ |
| 33 |
public: |
| 34 |
clamdinstance(ConfigVar &definition) |
| 35 |
: CSPlugin(definition), archivewarn(false){}; |
| 36 |
|
| 37 |
// we are not replacing scanTest or scanMemory |
| 38 |
// but for scanFile and the default scanMemory to work, we need a working scanFile implementation |
| 39 |
int scanFile(HTTPHeader *requestheader, HTTPHeader *docheader, const char *user, FOptionContainer* &foc, |
| 40 |
const char *ip, const char *filename, NaughtyFilter *checkme, |
| 41 |
const String *disposition, const String *mimetype); |
| 42 |
|
| 43 |
int init(void *args); |
| 44 |
|
| 45 |
private: |
| 46 |
// ClamD UNIX domain socket path |
| 47 |
String udspath; |
| 48 |
// File path prefix for chrooted ClamD |
| 49 |
String pathprefix; |
| 50 |
// Whether or not to just issue a warning on archive limit/encryption warnings |
| 51 |
bool archivewarn; |
| 52 |
}; |
| 53 |
|
| 54 |
// IMPLEMENTATION |
| 55 |
|
| 56 |
// class factory code *MUST* be included in every plugin |
| 57 |
|
| 58 |
CSPlugin *clamdcreate(ConfigVar &definition) |
| 59 |
{ |
| 60 |
return new clamdinstance(definition); |
| 61 |
} |
| 62 |
|
| 63 |
// end of Class factory |
| 64 |
|
| 65 |
// initialise the plugin |
| 66 |
int clamdinstance::init(void *args) |
| 67 |
{ |
| 68 |
int rc; |
| 69 |
if ((rc = CSPlugin::init(args)) != E2CS_OK) |
| 70 |
return rc; |
| 71 |
|
| 72 |
// read in ClamD UNIX domain socket path |
| 73 |
udspath = cv["clamdudsfile"]; |
| 74 |
if (udspath.length() < 3) { |
| 75 |
E2LOGGER_error("Error reading clamdudsfile option."); |
| 76 |
return E2CS_ERROR; |
| 77 |
// it would be far better to do a test connection to the file but |
| 78 |
// could not be arsed for now |
| 79 |
} |
| 80 |
|
| 81 |
// read in path prefix |
| 82 |
pathprefix = cv["pathprefix"]; |
| 83 |
|
| 84 |
archivewarn = cv["archivewarn"] == "on"; |
| 85 |
|
| 86 |
return E2CS_OK; |
| 87 |
} |
| 88 |
|
| 89 |
// no need to replace the inheritied scanMemory() which just calls scanFile() |
| 90 |
// there is no capability to scan memory with clamdscan as we pass it |
| 91 |
// a file name to scan. So we save the memory to disk and pass that. |
| 92 |
// Then delete the temp file. |
| 93 |
int clamdinstance::scanFile(HTTPHeader *requestheader, HTTPHeader *docheader, const char *user, |
| 94 |
FOptionContainer* &foc , const char *ip, const char *filename, NaughtyFilter *checkme, |
| 95 |
const String *disposition, const String *mimetype) |
| 96 |
{ |
| 97 |
lastmessage = lastvirusname = ""; |
| 98 |
// mkstemp seems to only set owner permissions, so our AV daemon won't be |
| 99 |
// able to read the file, unless it's running as the same user as us. that's |
| 100 |
// not usually very convenient. so instead, just allow group read on the |
| 101 |
// file, and tell users to make sure the daemongroup option is friendly to |
| 102 |
// the AV daemon's group membership. |
| 103 |
if (chmod(filename, S_IRGRP | S_IRUSR ) != 0) { |
| 104 |
lastmessage = "Error giving ClamD read access to temp file "; |
| 105 |
E2LOGGER_error(lastmessage, strerror(errno)); |
| 106 |
return E2CS_SCANERROR; |
| 107 |
}; |
| 108 |
String command("SCAN "); |
| 109 |
if (pathprefix.length()) { |
| 110 |
String fname(filename); |
| 111 |
command += fname.after(pathprefix.toCharArray()); |
| 112 |
} else { |
| 113 |
command += filename; |
| 114 |
} |
| 115 |
command += "\r\n"; |
| 116 |
DEBUG_avscan("clamdscan command:", command); |
| 117 |
|
| 118 |
UDSocket stripedsocks; |
| 119 |
if (stripedsocks.getFD() < 0) { |
| 120 |
lastmessage = "Error opening socket to talk to ClamD"; |
| 121 |
E2LOGGER_error(lastmessage); |
| 122 |
return E2CS_SCANERROR; |
| 123 |
} |
| 124 |
if (stripedsocks.connect(udspath.toCharArray()) < 0) { |
| 125 |
lastmessage = "Error connecting to ClamD socket"; |
| 126 |
E2LOGGER_error(lastmessage); |
| 127 |
stripedsocks.close(); |
| 128 |
return E2CS_SCANERROR; |
| 129 |
} |
| 130 |
if( ! stripedsocks.writeString(command.toCharArray())) { |
| 131 |
lastmessage = "Exception whilst writing to ClamD socket: "; |
| 132 |
String t = stripedsocks.getErrno(); |
| 133 |
lastmessage += t; |
| 134 |
if (stripedsocks.isTimedout()) lastmessage += " TimedOut"; |
| 135 |
if (stripedsocks.isHup()) lastmessage += " HUPed"; |
| 136 |
if (stripedsocks.isNoWrite()) lastmessage += " NotWritable"; |
| 137 |
E2LOGGER_error(lastmessage); |
| 138 |
DEBUG_avscan(lastmessage); |
| 139 |
stripedsocks.close(); |
| 140 |
return E2CS_SCANERROR; |
| 141 |
} |
| 142 |
char *buff = new char[4096]; |
| 143 |
int rc; |
| 144 |
rc = stripedsocks.getLine(buff, 4096, o.content.content_scanner_timeout); |
| 145 |
if (rc < 1) { |
| 146 |
delete[] buff; |
| 147 |
lastmessage = "Exception whist reading ClamD socket: "; |
| 148 |
String t = stripedsocks.getErrno(); |
| 149 |
lastmessage +=t; |
| 150 |
if (stripedsocks.isTimedout()) lastmessage += " TimedOut"; |
| 151 |
if (stripedsocks.isHup()) lastmessage += " HUPed"; |
| 152 |
if (stripedsocks.isNoRead()) lastmessage += " NotReadable"; |
| 153 |
E2LOGGER_error(lastmessage); |
| 154 |
DEBUG_avscan(lastmessage); |
| 155 |
stripedsocks.close(); |
| 156 |
return E2CS_SCANERROR; |
| 157 |
} |
| 158 |
String reply(buff); |
| 159 |
delete[] buff; |
| 160 |
reply.removeWhiteSpace(); |
| 161 |
DEBUG_avscan("Got from clamdscan: ", reply); |
| 162 |
stripedsocks.close(); |
| 163 |
if (reply.endsWith("ERROR")) { |
| 164 |
lastmessage = "ClamD error: " + reply; |
| 165 |
E2LOGGER_error(lastmessage); |
| 166 |
return E2CS_SCANERROR; |
| 167 |
} else if (reply.endsWith("FOUND")) { |
| 168 |
lastvirusname = reply.after(": ").before(" FOUND"); |
| 169 |
// format is: |
| 170 |
// /foo/path/file: foovirus FOUND |
| 171 |
|
| 172 |
DEBUG_avscan("clamdscan INFECTED! with: ", lastvirusname); |
| 173 |
if (archivewarn && (lastvirusname.contains(".Exceeded") || lastvirusname.contains(".Encrypted"))) { |
| 174 |
DEBUG_avscan("clamdscan: detected an ArchiveBlockMax \"virus\"; logging warning only"); |
| 175 |
lastmessage = "Archive not fully scanned: " + lastvirusname; |
| 176 |
|
| 177 |
return E2CS_WARNING; |
| 178 |
} |
| 179 |
|
| 180 |
blockFile(NULL, NULL, checkme); |
| 181 |
return E2CS_INFECTED; |
| 182 |
} |
| 183 |
// must be clean |
| 184 |
// Note: we should really check what the output of a "clean" message actually looks like, |
| 185 |
// and check explicitly for that, but the ClamD documentation is sparse on output formats. |
| 186 |
DEBUG_avscan("clamdscan - he say yes (clean)"); |
| 187 |
return E2CS_CLEAN; |
| 188 |
} |