| 1 |
// Trickle download manager - sends parts of a file being downloaded, a byte |
| 2 |
// at a time. |
| 3 |
// WARNING: Files which are/can be processed before they are complete - such |
| 4 |
// as certain image formats, shell scripts, and multimedia files - MAY have a |
| 5 |
// working, malicious portion sent to the browser before scanning has |
| 6 |
// completed! |
| 7 |
|
| 8 |
// For all support, instructions and copyright go to: |
| 9 |
// http://e2guardian.org/ |
| 10 |
// Released under the GPL v2, with the OpenSSL exception described in the README file. |
| 11 |
|
| 12 |
// INCLUDES |
| 13 |
#ifdef HAVE_CONFIG_H |
| 14 |
#include "e2config.h" |
| 15 |
#endif |
| 16 |
|
| 17 |
#include "../DownloadManager.hpp" |
| 18 |
#include "../OptionContainer.hpp" |
| 19 |
#include "../Logger.hpp" |
| 20 |
|
| 21 |
#include <string.h> |
| 22 |
#include <sys/time.h> |
| 23 |
#include <sys/types.h> |
| 24 |
#include <unistd.h> |
| 25 |
|
| 26 |
// GLOBALS |
| 27 |
|
| 28 |
extern OptionContainer o; |
| 29 |
extern thread_local std::string thread_id; |
| 30 |
extern bool is_daemonised; |
| 31 |
|
| 32 |
// DECLARATIONS |
| 33 |
|
| 34 |
class trickledm : public DMPlugin |
| 35 |
{ |
| 36 |
public: |
| 37 |
trickledm(ConfigVar &definition) |
| 38 |
: DMPlugin(definition){}; |
| 39 |
int in(DataBuffer *d, Socket *sock, Socket *peersock, HTTPHeader *requestheader, |
| 40 |
HTTPHeader *docheader, bool wantall, int *headersent, bool *toobig); |
| 41 |
int init(void *args); |
| 42 |
|
| 43 |
}; |
| 44 |
|
| 45 |
// IMPLEMENTATION |
| 46 |
|
| 47 |
// class factory code *MUST* be included in every plugin |
| 48 |
|
| 49 |
DMPlugin *trickledmcreate(ConfigVar &definition) |
| 50 |
{ |
| 51 |
DEBUG_trace("Creating trickle DM"); |
| 52 |
return new trickledm(definition); |
| 53 |
} |
| 54 |
|
| 55 |
// end of Class factory |
| 56 |
|
| 57 |
// uncomment these if you wish to replace the default inherited functions |
| 58 |
// < 0 = error |
| 59 |
// = 0 = ok |
| 60 |
// > 0 = warning |
| 61 |
|
| 62 |
//int dminstance::init(void* args) { |
| 63 |
// return 0; |
| 64 |
//} |
| 65 |
//int dminstance::quit(void) { |
| 66 |
// return 0; |
| 67 |
//} |
| 68 |
|
| 69 |
int trickledm::init(void *args) |
| 70 |
{ |
| 71 |
DMPlugin::init(args); |
| 72 |
|
| 73 |
StoryBoardOptions::SB_entry_map sen; |
| 74 |
sen.entry_function = cv["story_function"]; |
| 75 |
if (sen.entry_function.length() > 0) { |
| 76 |
sen.entry_id = ENT_STORYB_DM_TRICKLE; |
| 77 |
story_entry = sen.entry_id; |
| 78 |
o.story.dm_entry_dq.push_back(sen); |
| 79 |
return 0; |
| 80 |
} else { |
| 81 |
E2LOGGER_error("No story_function defined in trickle DM plugin config"); |
| 82 |
return -1; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// download body for this request |
| 87 |
int trickledm::in(DataBuffer *d, Socket *sock, Socket *peersock, class HTTPHeader *requestheader, |
| 88 |
class HTTPHeader *docheader, bool wantall, int *headersent, bool *toobig) |
| 89 |
{ |
| 90 |
|
| 91 |
//DataBuffer *d = where to stick the data back into |
| 92 |
//Socket *sock = where to read from |
| 93 |
//Socket *peersock = browser to send stuff to for keeping it alive |
| 94 |
//HTTPHeader *requestheader = header client used to request |
| 95 |
//HTTPHeader *docheader = header used for sending first line of reply |
| 96 |
//bool wantall = to determine if just content filter or a full scan |
| 97 |
//int *headersent = to use to send the first line of header if needed |
| 98 |
// or to mark the header has already been sent |
| 99 |
//bool *toobig = flag to modify to say if it could not all be downloaded |
| 100 |
|
| 101 |
DEBUG_trace("Inside trickle download manager plugin"); |
| 102 |
|
| 103 |
// To access settings for the plugin use the following example: |
| 104 |
// std::cout << "cvtest:" << cv["dummy"] << std::endl; |
| 105 |
|
| 106 |
//int rc = 0; |
| 107 |
d->bytesalreadysent = 0; |
| 108 |
d->bytes_toget = docheader->contentLength(); |
| 109 |
|
| 110 |
if (!d->icap) { |
| 111 |
DEBUG_dwload("tranencodeing is ", docheader->transferEncoding()); |
| 112 |
d->chunked = docheader->transferEncoding().contains("chunked"); |
| 113 |
} |
| 114 |
|
| 115 |
DEBUG_dwload("bytes remaining is ", d->bytes_toget); |
| 116 |
// if using non-persistent connections, some servers will not report |
| 117 |
// a content-length. in these situations, just download everything. |
| 118 |
d->geteverything = false; |
| 119 |
if ((d->bytes_toget < 0) || (d->chunked)) |
| 120 |
d->geteverything = true; |
| 121 |
|
| 122 |
d->swappedtodisk = false; |
| 123 |
d->doneinitialdelay = false; |
| 124 |
|
| 125 |
// struct timeval themdays; |
| 126 |
// struct timeval nowadays; |
| 127 |
// gettimeofday(&themdays, NULL); |
| 128 |
|
| 129 |
// buffer size for streaming downloads |
| 130 |
off_t blocksize = 32768; |
| 131 |
// set to a sensible minimum |
| 132 |
if (!wantall && (blocksize > o.content.max_content_filter_size)) |
| 133 |
blocksize = o.content.max_content_filter_size; |
| 134 |
else if (wantall && (blocksize > o.content.max_content_ramcache_scan_size)) |
| 135 |
blocksize = o.content.max_content_ramcache_scan_size; |
| 136 |
DEBUG_dwload("blocksize: ", blocksize); |
| 137 |
|
| 138 |
while ((d->bytes_toget > 0) || d->geteverything) { |
| 139 |
// send keep-alive bytes here |
| 140 |
if (o.content.trickle_delay > 0) { |
| 141 |
// themdays.tv_sec = nowadays.tv_sec; |
| 142 |
d->doneinitialdelay = true; |
| 143 |
if ((*headersent) < 1) { |
| 144 |
DEBUG_dwload("sending header first"); |
| 145 |
docheader->out(NULL, peersock, __E2HEADER_SENDALL); |
| 146 |
(*headersent) = 2; |
| 147 |
} |
| 148 |
if (!d->swappedtodisk) { |
| 149 |
// leave a kilobyte "barrier" so the whole file does not get sent before scanning |
| 150 |
if ((d->data_length > 1024) && (d->bytesalreadysent < (d->data_length - 1024))) { |
| 151 |
DEBUG_dwload("trickle delay - sending a byte from the memory buffer"); |
| 152 |
peersock->writeToSocket(d->data + (d->bytesalreadysent++), 1, 0, d->timeout); |
| 153 |
} |
| 154 |
else |
| 155 |
DEBUG_dwload("trickle delay - no unsent bytes remaining! (memory)"); |
| 156 |
|
| 157 |
} else { |
| 158 |
// check the file is at least one kilobyte ahead of our send pointer, so |
| 159 |
// the whole file does not get sent before scanning |
| 160 |
if (lseek(d->tempfilefd, d->bytesalreadysent + 1024, SEEK_SET) != (off_t) -1) { |
| 161 |
// ssize_t bytes_written; //new just remove GCC warning |
| 162 |
lseek(d->tempfilefd, d->bytesalreadysent, SEEK_SET); |
| 163 |
DEBUG_dwload("trickle delay - sending a byte from the file"); |
| 164 |
char byte; |
| 165 |
// bytes_written = read(d->tempfilefd, &byte, 1); |
| 166 |
peersock->writeToSocket(&byte, 1, 0, d->timeout); |
| 167 |
d->bytesalreadysent++; |
| 168 |
} |
| 169 |
else |
| 170 |
DEBUG_dwload("trickle delay - no unsent bytes remaining! (file)"); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
int read_res; |
| 175 |
int rc; |
| 176 |
int bsize = blocksize; |
| 177 |
if ((!d->geteverything) && (d->bytes_toget < bsize)) |
| 178 |
bsize = d->bytes_toget; |
| 179 |
DEBUG_dwload("bsize is ", bsize); |
| 180 |
|
| 181 |
rc = d->readInFromSocket(sock, bsize, wantall, read_res); |
| 182 |
if (read_res & DB_TOBIG) |
| 183 |
*toobig = true; |
| 184 |
if (rc <= 0) break; |
| 185 |
} |
| 186 |
|
| 187 |
if (!(*toobig) && !d->swappedtodisk) { // won't deflate stuff swapped to disk |
| 188 |
if (d->decompress.contains("deflate")) { |
| 189 |
DEBUG_dwload("zlib format"); |
| 190 |
d->zlibinflate(false); // incoming stream was zlib compressed |
| 191 |
} else if (d->decompress.contains("gzip")) { |
| 192 |
DEBUG_dwload("gzip format"); |
| 193 |
d->zlibinflate(true); // incoming stream was gzip compressed |
| 194 |
} |
| 195 |
} |
| 196 |
DEBUG_trace("Leaving trickle download manager plugin"); |
| 197 |
return 0; |
| 198 |
} |