| 1 |
// Command line 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 "../RegExp.hpp" |
| 18 |
#include "../Logger.hpp" |
| 19 |
|
| 20 |
#include <sys/time.h> |
| 21 |
#include <sys/types.h> |
| 22 |
#include <sys/stat.h> |
| 23 |
#include <sys/wait.h> |
| 24 |
#include <unistd.h> |
| 25 |
#include <list> |
| 26 |
#include <cstdio> |
| 27 |
#include <cstdlib> |
| 28 |
|
| 29 |
// GLOBALS |
| 30 |
|
| 31 |
extern OptionContainer o; |
| 32 |
|
| 33 |
// IMPLEMENTATION |
| 34 |
|
| 35 |
// class name is relevant |
| 36 |
class commandlineinstance : public CSPlugin |
| 37 |
{ |
| 38 |
public: |
| 39 |
commandlineinstance(ConfigVar &definition) |
| 40 |
: CSPlugin(definition), usevirusregexp(false), submatch(0), arguments(NULL), numarguments(0), infectedcodes(NULL), numinfectedcodes(0), cleancodes(NULL), numcleancodes(0), defaultresult(-1){}; |
| 41 |
int scanFile(HTTPHeader *requestheader, HTTPHeader *docheader, const char *user, FOptionContainer* &foc , |
| 42 |
const char *ip, const char *filename, NaughtyFilter *checkme, |
| 43 |
const String *disposition, const String *mimetype); |
| 44 |
|
| 45 |
int init(void *args); |
| 46 |
|
| 47 |
~commandlineinstance() |
| 48 |
{ |
| 49 |
delete[] infectedcodes; |
| 50 |
delete[] cleancodes; |
| 51 |
for (int i = 0; i < numarguments; i++) |
| 52 |
delete arguments[i]; |
| 53 |
delete[] arguments; |
| 54 |
}; |
| 55 |
|
| 56 |
private: |
| 57 |
// regular expression for finding virus names in program output |
| 58 |
RegExp virusregexp; |
| 59 |
RegResult virusregexpres; |
| 60 |
// whether or not the above is in use |
| 61 |
bool usevirusregexp; |
| 62 |
// which sub-match to take from the match |
| 63 |
int submatch; |
| 64 |
|
| 65 |
// path to command-line scanning program (+ initial arguments) |
| 66 |
String progname; |
| 67 |
// argument array (the above must be split on space for passing to exec) |
| 68 |
char **arguments; |
| 69 |
int numarguments; |
| 70 |
|
| 71 |
// return code(s) for infected files |
| 72 |
int *infectedcodes; |
| 73 |
int numinfectedcodes; |
| 74 |
// return code(s) for uninfected files |
| 75 |
int *cleancodes; |
| 76 |
int numcleancodes; |
| 77 |
|
| 78 |
// optional default result - can be used to e.g. define only cleancodes, |
| 79 |
// and have everything else default to infected. |
| 80 |
int defaultresult; |
| 81 |
}; |
| 82 |
|
| 83 |
// class factory code *MUST* be included in every plugin |
| 84 |
|
| 85 |
CSPlugin *commandlinecreate(ConfigVar &definition) |
| 86 |
{ |
| 87 |
return new commandlineinstance(definition); |
| 88 |
} |
| 89 |
|
| 90 |
// end of Class factory |
| 91 |
|
| 92 |
// initialise plugin |
| 93 |
int commandlineinstance::init(void *args) |
| 94 |
{ |
| 95 |
int rc; |
| 96 |
if ((rc = CSPlugin::init(args)) != E2CS_OK) |
| 97 |
return rc; |
| 98 |
|
| 99 |
// read in program name |
| 100 |
progname = cv["progname"]; |
| 101 |
if (progname.length() == 0) { |
| 102 |
E2LOGGER_error("Command-line scanner: No program specified"); |
| 103 |
return E2CS_ERROR; |
| 104 |
} |
| 105 |
|
| 106 |
// split into an argument array |
| 107 |
std::list<std::string> temparguments; |
| 108 |
char *tempprogname = new char[progname.length() + 1]; |
| 109 |
tempprogname[progname.length()] = '\0'; |
| 110 |
strncpy(tempprogname, progname.c_str(), progname.length()); |
| 111 |
char *result = strtok(tempprogname, " "); |
| 112 |
while (result) { |
| 113 |
temparguments.push_back(std::string(result)); |
| 114 |
result = strtok(NULL, " "); |
| 115 |
} |
| 116 |
delete[] tempprogname; |
| 117 |
for (int i = 0; i < numarguments; i++) |
| 118 |
delete arguments[i]; |
| 119 |
delete[] arguments; |
| 120 |
numarguments = temparguments.size(); |
| 121 |
arguments = new char *[numarguments + 2]; |
| 122 |
arguments[numarguments + 1] = NULL; |
| 123 |
int count = 0; |
| 124 |
for (std::list<std::string>::iterator i = temparguments.begin(); i != temparguments.end(); i++) { |
| 125 |
char *newthing = new char[i->length()]; |
| 126 |
strcpy(newthing, i->c_str()); |
| 127 |
arguments[count++] = newthing; |
| 128 |
} |
| 129 |
progname = cv["progname"]; |
| 130 |
|
| 131 |
#ifdef DEBUG_HIGH |
| 132 |
DEBUG_avscan("Program and arguments: "); |
| 133 |
for (int i = 0; i < numarguments; i++) { |
| 134 |
DEBUG_avscan(arguments[i], ", "); |
| 135 |
} |
| 136 |
#endif |
| 137 |
|
| 138 |
// read in virus name regular expression |
| 139 |
String ucvirusregexp(cv["virusregexp"]); |
| 140 |
if (ucvirusregexp.length()) { |
| 141 |
usevirusregexp = true; |
| 142 |
if (!virusregexp.comp(ucvirusregexp.toCharArray())) { |
| 143 |
E2LOGGER_error("Command-line scanner: Could not compile regular expression for extracting virus names"); |
| 144 |
return E2CS_ERROR; |
| 145 |
} |
| 146 |
String ssubmatch(cv["submatch"]); |
| 147 |
if (ssubmatch.length()) |
| 148 |
submatch = ssubmatch.toInteger(); |
| 149 |
} |
| 150 |
|
| 151 |
// read in the lists of good and bad program return codes |
| 152 |
String sinfectedcodes(cv["infectedcodes"]); |
| 153 |
String scleancodes(cv["cleancodes"]); |
| 154 |
std::list<int> tempinfectedcodes; |
| 155 |
std::list<int> tempcleancodes; |
| 156 |
char *tempcodes = new char[sinfectedcodes.length() + 1]; |
| 157 |
tempcodes[sinfectedcodes.length()] = '\0'; |
| 158 |
strncpy(tempcodes, sinfectedcodes.c_str(), sinfectedcodes.length()); |
| 159 |
result = strtok(tempcodes, ","); |
| 160 |
DEBUG_avscan("Infected file return codes: "); |
| 161 |
while (result) { |
| 162 |
tempinfectedcodes.push_back(atoi(result)); |
| 163 |
DEBUG_avscan(tempinfectedcodes.back() ); |
| 164 |
result = strtok(NULL, ","); |
| 165 |
} |
| 166 |
delete[] tempcodes; |
| 167 |
tempcodes = new char[scleancodes.length() + 1]; |
| 168 |
tempcodes[scleancodes.length()] = '\0'; |
| 169 |
strncpy(tempcodes, scleancodes.c_str(), scleancodes.length()); |
| 170 |
result = strtok(tempcodes, ","); |
| 171 |
|
| 172 |
DEBUG_avscan("Clean file return codes: "); |
| 173 |
while (result) { |
| 174 |
tempcleancodes.push_back(atoi(result)); |
| 175 |
DEBUG_avscan(tempcleancodes.back()); |
| 176 |
result = strtok(NULL, ","); |
| 177 |
} |
| 178 |
delete[] tempcodes; |
| 179 |
|
| 180 |
// we need at least one of our three mechanisms (cleancodes, infectedcodes and virus names) |
| 181 |
// to be defined in order to make a decision about the nature of a scanning result. |
| 182 |
numcleancodes = tempcleancodes.size(); |
| 183 |
numinfectedcodes = tempinfectedcodes.size(); |
| 184 |
if (!(usevirusregexp || numcleancodes || numinfectedcodes)) { |
| 185 |
E2LOGGER_error("Command-line scanner requires some mechanism for interpreting results. Please define cleancodes, infectedcodes, and/or a virusregexp."); |
| 186 |
return E2CS_ERROR; |
| 187 |
} |
| 188 |
|
| 189 |
// Copy return code lists out into static arrays |
| 190 |
delete[] infectedcodes; |
| 191 |
delete[] cleancodes; |
| 192 |
infectedcodes = new int[numinfectedcodes]; |
| 193 |
cleancodes = new int[numcleancodes]; |
| 194 |
count = 0; |
| 195 |
for (std::list<int>::iterator i = tempinfectedcodes.begin(); i != tempinfectedcodes.end(); i++) |
| 196 |
infectedcodes[count++] = *i; |
| 197 |
count = 0; |
| 198 |
for (std::list<int>::iterator i = tempcleancodes.begin(); i != tempcleancodes.end(); i++) |
| 199 |
cleancodes[count++] = *i; |
| 200 |
|
| 201 |
// read in default result type |
| 202 |
String sdefaultresult(cv["defaultresult"]); |
| 203 |
if (sdefaultresult.length()) { |
| 204 |
if (sdefaultresult == "clean") { |
| 205 |
defaultresult = 1; |
| 206 |
} else if (sdefaultresult == "infected") { |
| 207 |
defaultresult = 0; |
| 208 |
} else { |
| 209 |
E2LOGGER_error("Command-line scanner: Default result value not understood"); |
| 210 |
return E2CS_WARNING; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
return E2CS_OK; |
| 215 |
} |
| 216 |
|
| 217 |
// no need to replace the inheritied scanMemory() which just calls scanFile() |
| 218 |
// there is no capability to scan memory with commandlinescan as we pass it |
| 219 |
// a file name to scan. So we save the memory to disk and pass that. |
| 220 |
// Then delete the temp file. |
| 221 |
// TODO Allow for placeholders in command line for inserting content-disposition & content-type? |
| 222 |
int commandlineinstance::scanFile(HTTPHeader *requestheader, HTTPHeader *docheader, const char *user, FOptionContainer* &foc , |
| 223 |
const char *ip, const char *filename, NaughtyFilter *checkme, const String *disposition, const String *mimetype) |
| 224 |
{ |
| 225 |
// create socket pairs for child (scanner) process's stdout & stderr |
| 226 |
int scannerstdout[2]; |
| 227 |
int scannerstderr[2]; |
| 228 |
if (socketpair(AF_UNIX, SOCK_STREAM, 0, scannerstdout) == -1) { |
| 229 |
lastmessage = "Cannot create sockets for communicating with scanner"; |
| 230 |
E2LOGGER_error(lastmessage, " ", strerror(errno) ); |
| 231 |
return E2CS_SCANERROR; |
| 232 |
} |
| 233 |
if (socketpair(AF_UNIX, SOCK_STREAM, 0, scannerstderr) == -1) { |
| 234 |
lastmessage = "Cannot create sockets for communicating with scanner" ; |
| 235 |
E2LOGGER_error(lastmessage, " ", strerror(errno) ); |
| 236 |
return E2CS_SCANERROR; |
| 237 |
} |
| 238 |
int f = fork(); |
| 239 |
if (f == 0) { |
| 240 |
DEBUG_avscan("Running: ", progname, " ", filename); |
| 241 |
// close read ends of sockets |
| 242 |
close(scannerstdout[0]); |
| 243 |
close(scannerstderr[0]); |
| 244 |
// bind stdout & stderr |
| 245 |
dup2(scannerstdout[1], 1); |
| 246 |
dup2(scannerstderr[1], 2); |
| 247 |
// execute scanner |
| 248 |
arguments[numarguments] = (char *)filename; |
| 249 |
execv(arguments[0], arguments); |
| 250 |
// if we get here, an error occurred! |
| 251 |
E2LOGGER_error("Cannot exec command-line scanner (command ", progname, " ",filename, "): ", strerror(errno)); |
| 252 |
_exit(255); |
| 253 |
} else if (f == -1) { |
| 254 |
lastmessage = "Cannot launch scanner"; |
| 255 |
E2LOGGER_error("Cannot fork to launch command-line scanner (command ", progname, " ",filename, "): ", strerror(errno)); |
| 256 |
return E2CS_SCANERROR; |
| 257 |
} |
| 258 |
|
| 259 |
// close write ends of sockets |
| 260 |
close(scannerstdout[1]); |
| 261 |
close(scannerstderr[1]); |
| 262 |
|
| 263 |
char buff[8192]; |
| 264 |
std::string result; |
| 265 |
FILE *readme = fdopen(scannerstdout[0], "r"); |
| 266 |
while (fgets(buff, 8192, readme) != NULL) { |
| 267 |
#ifndef DEBUG_LOW |
| 268 |
if (usevirusregexp) |
| 269 |
#endif |
| 270 |
result += buff; |
| 271 |
} |
| 272 |
fclose(readme); |
| 273 |
readme = fdopen(scannerstderr[0], "r"); |
| 274 |
while (fgets(buff, 8192, readme) != NULL) { |
| 275 |
#ifndef DEBUG_LOW |
| 276 |
if (usevirusregexp) |
| 277 |
#endif |
| 278 |
result += buff; |
| 279 |
} |
| 280 |
fclose(readme); |
| 281 |
|
| 282 |
// close read ends too now |
| 283 |
close(scannerstdout[0]); |
| 284 |
close(scannerstderr[0]); |
| 285 |
|
| 286 |
// wait for scanner to quit & retrieve exit status |
| 287 |
int returncode; |
| 288 |
returncode = WEXITSTATUS(returncode); |
| 289 |
|
| 290 |
if (waitpid(f, &returncode, 0) == -1) { |
| 291 |
lastmessage = "Cannot get scanner return code"; |
| 292 |
E2LOGGER_error(lastmessage, " ", strerror(errno)); |
| 293 |
return E2CS_SCANERROR; |
| 294 |
} |
| 295 |
|
| 296 |
DEBUG_avscan("Scanner result: ", (result), "Code: ", returncode); |
| 297 |
if (returncode == 255) { |
| 298 |
lastmessage = "Cannot get scanner return code"; |
| 299 |
E2LOGGER_error("Cannot get command-line scanner return code: scanner exec failed"); |
| 300 |
return E2CS_SCANERROR; |
| 301 |
} |
| 302 |
|
| 303 |
lastvirusname = "Unknown"; |
| 304 |
|
| 305 |
if (usevirusregexp) { |
| 306 |
virusregexp.match(result.c_str(), virusregexpres); |
| 307 |
if (virusregexpres.matched()) { |
| 308 |
lastvirusname = virusregexpres.result(submatch); |
| 309 |
blockFile(NULL, NULL, checkme); |
| 310 |
return E2CS_INFECTED; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
if (cleancodes) { |
| 315 |
for (int i = 0; i < numcleancodes; i++) { |
| 316 |
if (returncode == cleancodes[i]) |
| 317 |
return E2CS_CLEAN; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
if (infectedcodes) { |
| 322 |
for (int i = 0; i < numinfectedcodes; i++) { |
| 323 |
if (returncode == infectedcodes[i]) { |
| 324 |
blockFile(NULL, NULL, checkme); |
| 325 |
return E2CS_INFECTED; |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
if (defaultresult == 1) |
| 331 |
return E2CS_CLEAN; |
| 332 |
else if (defaultresult == 0) { |
| 333 |
blockFile(NULL, NULL, checkme); |
| 334 |
return E2CS_INFECTED; |
| 335 |
} |
| 336 |
|
| 337 |
if (returncode != 0) |
| 338 |
return E2CS_SCANERROR; |
| 339 |
return 0; |
| 340 |
} |