| 1 |
// Released under the GPL v2, with the OpenSSL exception described in the README file. |
| 2 |
|
| 3 |
// INCLUDES |
| 4 |
#ifdef HAVE_CONFIG_H |
| 5 |
#include "e2config.h" |
| 6 |
#endif |
| 7 |
//#include "NaughtyFilter.hpp" |
| 8 |
//#include "StoryBoard.hpp" |
| 9 |
#include "ConnectionHandler.hpp" |
| 10 |
#include "DataBuffer.hpp" |
| 11 |
#include "UDSocket.hpp" |
| 12 |
//#include "Auth.hpp" |
| 13 |
#include "FDTunnel.hpp" |
| 14 |
#include "BackedStore.hpp" |
| 15 |
#include "Queue.hpp" |
| 16 |
#include "ImageContainer.hpp" |
| 17 |
#include "Logger.hpp" |
| 18 |
|
| 19 |
#include <signal.h> |
| 20 |
#include <arpa/nameser.h> |
| 21 |
#include <resolv.h> |
| 22 |
|
| 23 |
#include "CertificateAuthority.hpp" |
| 24 |
|
| 25 |
#include <cerrno> |
| 26 |
#include <cstdio> |
| 27 |
#include <ctime> |
| 28 |
#include <algorithm> |
| 29 |
#include <netdb.h> |
| 30 |
#include <cstdlib> |
| 31 |
#include <unistd.h> |
| 32 |
#include <sys/time.h> |
| 33 |
#include <strings.h> |
| 34 |
#include <fcntl.h> |
| 35 |
#include <string.h> |
| 36 |
#include <sys/stat.h> |
| 37 |
#include <sys/types.h> |
| 38 |
#include <sys/socket.h> |
| 39 |
#include <istream> |
| 40 |
#include <sstream> |
| 41 |
#include <memory> |
| 42 |
|
| 43 |
#ifdef ENABLE_ORIG_IP |
| 44 |
#include <linux/types.h> |
| 45 |
#include <linux/netfilter_ipv4.h> |
| 46 |
#endif |
| 47 |
|
| 48 |
#include "openssl/ssl.h" |
| 49 |
#include "openssl/x509v3.h" |
| 50 |
#include "String.hpp" |
| 51 |
|
| 52 |
// GLOBALS |
| 53 |
extern OptionContainer o; |
| 54 |
extern std::atomic<bool> ttg; |
| 55 |
|
| 56 |
// IMPLEMENTATION |
| 57 |
|
| 58 |
ConnectionHandler::ConnectionHandler() |
| 59 |
: clienthost(NULL) { |
| 60 |
|
| 61 |
// initialise SBauth structure |
| 62 |
SBauth.filter_group = 0; |
| 63 |
SBauth.is_authed = false; |
| 64 |
SBauth.user_name = ""; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
// Custom exception class for POST filtering errors |
| 69 |
class postfilter_exception : public std::runtime_error { |
| 70 |
public: |
| 71 |
postfilter_exception(const char *const &msg) |
| 72 |
: std::runtime_error(msg) {}; |
| 73 |
}; |
| 74 |
|
| 75 |
// |
| 76 |
// URL cache funcs |
| 77 |
// |
| 78 |
|
| 79 |
// check the URL cache to see if we've already flagged an address as clean |
| 80 |
bool wasClean(HTTPHeader &header, String &url, const int fg) { |
| 81 |
return false; // this function needs rewriting always return false |
| 82 |
} |
| 83 |
|
| 84 |
// add a known clean URL to the cache |
| 85 |
void addToClean(String &url, const int fg) { |
| 86 |
return; // this function needs rewriting |
| 87 |
} |
| 88 |
|
| 89 |
// |
| 90 |
// ConnectionHandler class |
| 91 |
// |
| 92 |
|
| 93 |
void ConnectionHandler::peerDiag(const char *message, Socket &peersock) { |
| 94 |
if (o.conn.logconerror) { |
| 95 |
//int peerport = peersock.getPeerSourcePort(); |
| 96 |
std::string peer_ip = peersock.getPeerIP(); |
| 97 |
int err = peersock.getErrno(); |
| 98 |
|
| 99 |
if (peersock.isTimedout()) { |
| 100 |
E2LOGGER_info(message, " Client at ", peer_ip, " Connection timedout - errno: ", err); |
| 101 |
} else if (peersock.isHup()) { |
| 102 |
E2LOGGER_info(message, " Client at ", peer_ip, " has disconnected - errno: ", err); |
| 103 |
} else if (peersock.sockError()) { |
| 104 |
E2LOGGER_info(message, " Client at ", peer_ip, " Connection socket error - errno: ", err); |
| 105 |
} else if (peersock.isNoRead()) { |
| 106 |
E2LOGGER_info(message, " cant read Client Connection at ", peer_ip, " - errno: ", err); |
| 107 |
} else if (peersock.isNoWrite()) { |
| 108 |
E2LOGGER_info(message, " cant write Client Connection at ", peer_ip, " - errno: ", err); |
| 109 |
} else if (peersock.isNoOpp()) { |
| 110 |
E2LOGGER_info(message, " Client Connection is no-op - errno: ", err); |
| 111 |
} else { |
| 112 |
E2LOGGER_info(message, " Client Connection at ", peer_ip, " problem - errno: ", err); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
void ConnectionHandler::upstreamDiag(const char *message, Socket &proxysock) { |
| 118 |
if (o.conn.logconerror) { |
| 119 |
|
| 120 |
int err = proxysock.getErrno(); |
| 121 |
if (proxysock.isTimedout()) { |
| 122 |
E2LOGGER_info(message, " upstream timedout - errno: ", err); |
| 123 |
} else if (proxysock.isHup()) { |
| 124 |
E2LOGGER_info(message, " upstream has disconnected - errno: ", err); |
| 125 |
} else if (proxysock.sockError()) { |
| 126 |
E2LOGGER_info(message, " upstream socket error - errno: ", err); |
| 127 |
} else if (proxysock.isNoRead()) { |
| 128 |
E2LOGGER_info(message, " cant read upstream Connection - errno: ", err); |
| 129 |
} else if (proxysock.isNoWrite()) { |
| 130 |
E2LOGGER_info(message, " cant write upstream Connection - errno: ", err); |
| 131 |
} else if (proxysock.isNoOpp()) { |
| 132 |
E2LOGGER_info(message, " upstream Connection is no-op - errno: ", err); |
| 133 |
} else { |
| 134 |
E2LOGGER_info(message, " upstream Connection problem - errno: ", err); |
| 135 |
} |
| 136 |
} |
| 137 |
if (proxysock.isNoOpp()) |
| 138 |
proxysock.close(); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
// perform URL encoding on a string |
| 143 |
std::string ConnectionHandler::miniURLEncode(const char *s) { |
| 144 |
std::string encoded; |
| 145 |
char *buf = new char[3]; |
| 146 |
unsigned char c; |
| 147 |
for (int i = 0; i < (signed) strlen(s); i++) { |
| 148 |
c = s[i]; |
| 149 |
// allowed characters in a url that have non special meaning |
| 150 |
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) { |
| 151 |
encoded += c; |
| 152 |
continue; |
| 153 |
} |
| 154 |
// all other characters get encoded |
| 155 |
sprintf(buf, "%02x", c); |
| 156 |
encoded += "%"; |
| 157 |
encoded += buf; |
| 158 |
} |
| 159 |
delete[] buf; |
| 160 |
return encoded; |
| 161 |
} |
| 162 |
|
| 163 |
// create a temporary bypass URL for the banned page |
| 164 |
String ConnectionHandler::hashedURL(String *url, int filtergroup, std::string *clientip, |
| 165 |
bool infectionbypass, std::string *user) { |
| 166 |
// filter/virus bypass hashes last for a certain time only |
| 167 |
String timecode(time(NULL) + (infectionbypass ? (*ldl->fg[filtergroup]).infection_bypass_mode |
| 168 |
: (*ldl->fg[filtergroup]).bypass_mode)); |
| 169 |
// use the standard key in normal bypass mode, and the infection key in infection bypass mode |
| 170 |
String magic(infectionbypass ? ldl->fg[filtergroup]->imagic.c_str() : ldl->fg[filtergroup]->magic.c_str()); |
| 171 |
magic += clientip->c_str(); |
| 172 |
if(ldl->fg[filtergroup]->bypass_v2) |
| 173 |
magic += user->c_str(); |
| 174 |
magic += timecode; |
| 175 |
String res(infectionbypass ? "GIBYPASS=" : "GBYPASS="); |
| 176 |
if (!url->after("://").contains("/")) { |
| 177 |
String newurl((*url)); |
| 178 |
newurl += "/"; |
| 179 |
res += newurl.md5(magic.toCharArray()); |
| 180 |
} else { |
| 181 |
res += url->md5(magic.toCharArray()); |
| 182 |
} |
| 183 |
res += timecode; |
| 184 |
DEBUG_debug(" -generate Bypass hashedurl data ", clientip, " ", *url, " ", clientuser, " ", timecode, " result ", res); |
| 185 |
return res; |
| 186 |
} |
| 187 |
|
| 188 |
// create temporary bypass cookie |
| 189 |
String ConnectionHandler::hashedCookie(String *url, const char *magic, std::string *clientip, int bypasstimestamp) { |
| 190 |
String timecode(bypasstimestamp); |
| 191 |
String data(magic); |
| 192 |
data += clientip->c_str(); |
| 193 |
data += clientuser; |
| 194 |
data += timecode; |
| 195 |
DEBUG_debug(" -generate Bypass hashedCookie data ", clientip, " ", *url, " ", clientuser, " ", timecode); |
| 196 |
String res(url->md5(data.toCharArray())); |
| 197 |
res += timecode; |
| 198 |
DEBUG_debug(" -Bypass hashedCookie="+ res); |
| 199 |
return res; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
// is this a temporary filter bypass URL? |
| 204 |
int ConnectionHandler::isBypassURL(String url, const char *magic, const char *clientip, std::string btype, std::string &user) |
| 205 |
{ |
| 206 |
//if ((url).length() <= 45) |
| 207 |
// return 0; // Too short, can't be a bypass |
| 208 |
|
| 209 |
// check to see if this is a bypass URL of the btype type |
| 210 |
if(!(url).contains(btype.c_str())) |
| 211 |
return 0; |
| 212 |
|
| 213 |
DEBUG_debug("URL ", btype, " found checking..."); |
| 214 |
|
| 215 |
String url_left((url).before(btype.c_str())); |
| 216 |
url_left.chop(); // remove the ? or & |
| 217 |
String url_right((url).after(btype.c_str())); |
| 218 |
String url_hash(url_right.subString(0, 32)); |
| 219 |
String url_time(url_right.after(url_hash.toCharArray())); |
| 220 |
DEBUG_debug("URL: ", url_left, ", HASH: ", url_hash, ", TIME: ", url_time); |
| 221 |
|
| 222 |
String mymagic(magic); |
| 223 |
mymagic += clientip; |
| 224 |
if(ldl->fg[filtergroup]->bypass_v2) |
| 225 |
mymagic += user; |
| 226 |
mymagic += url_time; |
| 227 |
String hashed(url_left.md5(mymagic.toCharArray())); |
| 228 |
|
| 229 |
if(ldl->fg[filtergroup]->cgi_bypass_v2) { |
| 230 |
mymagic = hashed; |
| 231 |
hashed = mymagic.md5(ldl->fg[filtergroup]->cgi_magic.c_str()); |
| 232 |
} |
| 233 |
|
| 234 |
if (hashed != url_hash) { |
| 235 |
DEBUG_debug("URL ", btype, " hash mismatch"); |
| 236 |
return 0; |
| 237 |
} |
| 238 |
|
| 239 |
time_t timen = time(NULL); |
| 240 |
time_t timeu = url_time.toLong(); |
| 241 |
|
| 242 |
if (timeu < 1) { |
| 243 |
DEBUG_debug("URL ", btype, " bad time value"); |
| 244 |
return 1; // bad time value |
| 245 |
} |
| 246 |
if (timeu < timen) { // expired key |
| 247 |
DEBUG_debug("URL ", btype, " expired"); |
| 248 |
return 1; // denotes expired but there |
| 249 |
} |
| 250 |
DEBUG_debug("URL ", btype, " not expired"); |
| 251 |
return (int)timeu; |
| 252 |
} |
| 253 |
|
| 254 |
// is this a scan bypass URL? i.e. a "magic" URL for retrieving a previously scanned file |
| 255 |
bool ConnectionHandler::isScanBypassURL(String url, const char *magic, const char *clientip) |
| 256 |
{ |
| 257 |
// if ((url).length() <= 45) |
| 258 |
// return false; // Too short, can't be a bypass |
| 259 |
|
| 260 |
if (!(url).contains("GSBYPASS=")) { // If this is not a bypass url |
| 261 |
return false; |
| 262 |
} |
| 263 |
DEBUG_debug("URL GSBYPASS found checking..."); |
| 264 |
|
| 265 |
String url_left((url).before("GSBYPASS=")); |
| 266 |
url_left.chop(); // remove the ? or & |
| 267 |
String url_right((url).after("GSBYPASS=")); |
| 268 |
|
| 269 |
String url_hash(url_right.subString(0, 32)); |
| 270 |
DEBUG_debug("URL: ", url_left, ", HASH: ", url_hash); |
| 271 |
|
| 272 |
// format is: |
| 273 |
// GSBYPASS=hash(ip+url+tempfilename+mime+disposition+secret) |
| 274 |
// &N=tempfilename&M=mimetype&D=dispos |
| 275 |
|
| 276 |
String tempfilename(url_right.after("&N=")); |
| 277 |
String tempfilemime(tempfilename.after("&M=")); |
| 278 |
String tempfiledis(tempfilemime.after("&D=")); |
| 279 |
tempfilemime = tempfilemime.before("&D="); |
| 280 |
tempfilename = tempfilename.before("&M="); |
| 281 |
|
| 282 |
String tohash(clientip + url_left + tempfilename + tempfilemime + tempfiledis + magic); |
| 283 |
String hashed(tohash.md5()); |
| 284 |
|
| 285 |
if(ldl->fg[filtergroup]->cgi_bypass_v2) { |
| 286 |
tohash = hashed; |
| 287 |
hashed = tohash.md5(ldl->fg[filtergroup]->cgi_magic.c_str()); |
| 288 |
} |
| 289 |
|
| 290 |
DEBUG_debug("checking hash: ", clientip, " ", url_left, " ", tempfilename, " ", |
| 291 |
tempfilemime, " ", tempfiledis, " ", magic, " ", hashed); |
| 292 |
|
| 293 |
if (hashed == url_hash) { |
| 294 |
return true; |
| 295 |
} |
| 296 |
DEBUG_debug("URL GSBYPASS HASH mismatch"); |
| 297 |
|
| 298 |
return false; |
| 299 |
} |
| 300 |
|
| 301 |
// send a file to the client - used during bypass of blocked downloads |
| 302 |
off_t |
| 303 |
ConnectionHandler::sendFile(Socket *peerconn, NaughtyFilter &cm, String &url, bool is_icap, ICAPHeader *icap_head) { |
| 304 |
String filedis = cm.tempfiledis; |
| 305 |
int fd = open(cm.tempfilename.toCharArray(), O_RDONLY); |
| 306 |
if (fd < 0) { // file access error |
| 307 |
E2LOGGER_error("Error reading file to send: ", cm.tempfilename); |
| 308 |
|
| 309 |
String fnf(o.language_list.getTranslation(1230)); |
| 310 |
String head("HTTP/1.1 404 " + fnf + "\r\nContent-Type: text/html\r\n\r\n"); |
| 311 |
String body("<HTML><HEAD><TITLE>" + fnf + "</TITLE></HEAD><BODY><H1>" + fnf + "</H1></BODY></HTML>\r\n"); |
| 312 |
|
| 313 |
if (is_icap) { |
| 314 |
icap_head->out_res_header = head; |
| 315 |
icap_head->out_res_body = body; |
| 316 |
icap_head->out_res_hdr_flag = true; |
| 317 |
icap_head->out_res_body_flag = true; |
| 318 |
icap_head->respond(*peerconn); |
| 319 |
} else { |
| 320 |
peerconn->writeString(head.toCharArray()); |
| 321 |
peerconn->writeString(body.toCharArray()); |
| 322 |
} |
| 323 |
|
| 324 |
return 0; |
| 325 |
} |
| 326 |
|
| 327 |
off_t filesize = lseek(fd, 0, SEEK_END); |
| 328 |
lseek(fd, 0, SEEK_SET); |
| 329 |
String head("HTTP/1.1 200 OK\r\nContent-Type: " + cm.tempfilemime + "\r\nContent-Length: " + String(filesize)); |
| 330 |
if (filedis.length() == 0) { |
| 331 |
filedis = url.before("?"); |
| 332 |
while (filedis.contains("/")) |
| 333 |
filedis = filedis.after("/"); |
| 334 |
} |
| 335 |
head += "\r\nContent-disposition: attachment; filename=" + filedis; |
| 336 |
head += "\r\n\r\n"; |
| 337 |
|
| 338 |
if (is_icap) { |
| 339 |
icap_head->out_res_header = head; |
| 340 |
icap_head->out_res_hdr_flag = true; |
| 341 |
icap_head->out_res_body_flag = true; |
| 342 |
icap_head->respond(*peerconn); |
| 343 |
} else { |
| 344 |
if (!peerconn->writeString(head.toCharArray())) { |
| 345 |
close(fd); |
| 346 |
return 0; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
// perform the actual sending |
| 351 |
off_t sent = 0; |
| 352 |
int rc; |
| 353 |
//char *buffer = new char[250000]; |
| 354 |
char *buffer = new char[64000]; |
| 355 |
while (sent < filesize) { |
| 356 |
rc = read(fd, buffer, 64000); |
| 357 |
DEBUG_debug(" -reading send file rc:", String(rc)); |
| 358 |
if (rc < 0) { |
| 359 |
E2LOGGER_error(" -error reading send file so aborting"); |
| 360 |
delete[] buffer; |
| 361 |
// throw std::exception/(); |
| 362 |
//cleanThrow("error reading send file", *peerconn); |
| 363 |
return 0; |
| 364 |
} |
| 365 |
if (rc == 0) { |
| 366 |
E2LOGGER_error(" -got zero bytes reading send file"); |
| 367 |
break; // should never happen |
| 368 |
} |
| 369 |
if (is_icap) { |
| 370 |
if (!peerconn->writeChunk(buffer, rc, 100000)) { |
| 371 |
delete[] buffer; |
| 372 |
peerDiag("Error sending file to client", *peerconn); |
| 373 |
return 0; |
| 374 |
} |
| 375 |
} else { |
| 376 |
// as it's cached to disk the buffer must be reasonably big |
| 377 |
if (!peerconn->writeToSocket(buffer, rc, 0, 100000)) { |
| 378 |
delete[] buffer; |
| 379 |
peerDiag("Error sending file to client", *peerconn); |
| 380 |
return 0; |
| 381 |
// throw std::exception(); |
| 382 |
} |
| 383 |
} |
| 384 |
sent += rc; |
| 385 |
DEBUG_debug(" -total sent from temp: ", String(sent)); |
| 386 |
|
| 387 |
} |
| 388 |
if (is_icap) { |
| 389 |
String n; |
| 390 |
peerconn->writeChunkTrailer(n); |
| 391 |
} |
| 392 |
delete[] buffer; |
| 393 |
close(fd); |
| 394 |
return sent; |
| 395 |
} |
| 396 |
|
| 397 |
int |
| 398 |
ConnectionHandler::connectUpstream(Socket &sock, NaughtyFilter &cm, int port = 0) // connects to to proxy or directly |
| 399 |
{ |
| 400 |
if (port == 0) |
| 401 |
port = cm.request_header->port; |
| 402 |
String sport(port); |
| 403 |
int lerr_mess = 0; |
| 404 |
int retry = -1; |
| 405 |
bool may_be_loop = false; |
| 406 |
for (auto it = o.net.check_ports.begin(); it != o.net.check_ports.end(); it++) { |
| 407 |
if (*it == sport) { |
| 408 |
may_be_loop = true; |
| 409 |
break; |
| 410 |
} |
| 411 |
} |
| 412 |
DEBUG_debug("May_be_loop = ", may_be_loop, " ", " port ", port); |
| 413 |
|
| 414 |
sock.setTimeout(o.net.connect_timeout); |
| 415 |
|
| 416 |
while (++retry < o.net.connect_retries) { |
| 417 |
lerr_mess = 0; |
| 418 |
if (retry > 0) { |
| 419 |
if (o.conn.logconerror) |
| 420 |
E2LOGGER_info("retry ", retry, " to connect to ", cm.urldomain); |
| 421 |
if (!sock.isTimedout()) |
| 422 |
usleep(1000); // don't hammer upstream |
| 423 |
} |
| 424 |
cm.upfailure = false; |
| 425 |
if (cm.isdirect) { |
| 426 |
String des_ip; |
| 427 |
if (cm.isiphost) |
| 428 |
des_ip = cm.urldomain; |
| 429 |
if(o.conn.use_original_ip_port && cm.got_orig_ip && (cm.connect_site == cm.urldomain)) |
| 430 |
des_ip = cm.orig_ip; |
| 431 |
|
| 432 |
if(des_ip.length() > 0) { |
| 433 |
if (may_be_loop) { // check check_ip list |
| 434 |
bool do_break = false; |
| 435 |
if (o.net.check_ip.size() > 0) { |
| 436 |
for (auto it = o.net.check_ip.begin(); it != o.net.check_ip.end(); it++) { |
| 437 |
if (*it == des_ip) { |
| 438 |
do_break = true; |
| 439 |
lerr_mess = 212; |
| 440 |
break; |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
if (do_break) break; |
| 445 |
may_be_loop = false; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
DEBUG_debug("Connecting to IP ", des_ip, " port ", String(port)); |
| 450 |
|
| 451 |
int rc = sock.connect(des_ip, port); |
| 452 |
if (rc < 0) { |
| 453 |
lerr_mess = 203; |
| 454 |
continue; |
| 455 |
} |
| 456 |
return rc; |
| 457 |
} else { |
| 458 |
//dns lookup |
| 459 |
struct addrinfo hints, *infoptr; |
| 460 |
memset(&hints, 0, sizeof(addrinfo)); |
| 461 |
hints.ai_family = AF_INET; |
| 462 |
hints.ai_socktype = SOCK_STREAM; |
| 463 |
hints.ai_flags = 0; |
| 464 |
hints.ai_protocol = 0; |
| 465 |
hints.ai_canonname = NULL; |
| 466 |
hints.ai_addr = NULL; |
| 467 |
hints.ai_next = NULL; |
| 468 |
int rc = getaddrinfo(cm.connect_site.toCharArray(), NULL, &hints, &infoptr); |
| 469 |
if (rc) // problem |
| 470 |
{ |
| 471 |
DEBUG_debug("connectUpstream: getaddrinfo returned ", String(rc), |
| 472 |
" for ", cm.connect_site, " ", gai_strerror(rc) ); |
| 473 |
|
| 474 |
bool rt = false; |
| 475 |
switch (rc) { |
| 476 |
case EAI_NONAME: |
| 477 |
lerr_mess = 207; |
| 478 |
break; |
| 479 |
#ifdef EAI_NODATA |
| 480 |
case EAI_NODATA: |
| 481 |
lerr_mess = 208; |
| 482 |
break; |
| 483 |
#endif |
| 484 |
case EAI_AGAIN: |
| 485 |
lerr_mess = 209; |
| 486 |
rt = true; |
| 487 |
break; |
| 488 |
case EAI_FAIL: |
| 489 |
lerr_mess = 210; |
| 490 |
break; |
| 491 |
default: |
| 492 |
lerr_mess = 210; //TODO this should have it's own message?? |
| 493 |
break; |
| 494 |
} |
| 495 |
sock.close(); |
| 496 |
if (rt) continue; |
| 497 |
else break; |
| 498 |
} |
| 499 |
char t[256]; |
| 500 |
struct addrinfo *p; |
| 501 |
for (p = infoptr; p != NULL; p = p->ai_next) { |
| 502 |
getnameinfo(p->ai_addr, p->ai_addrlen, t, sizeof(t), NULL, 0, NI_NUMERICHOST); |
| 503 |
if (may_be_loop) { // check check_ip list |
| 504 |
bool do_break = false; |
| 505 |
if (o.net.check_ip.size() > 0) { |
| 506 |
for (auto it = o.net.check_ip.begin(); it != o.net.check_ip.end(); it++) { |
| 507 |
if (*it == t) { |
| 508 |
do_break = true; |
| 509 |
lerr_mess = 212; |
| 510 |
break; |
| 511 |
} |
| 512 |
} |
| 513 |
} |
| 514 |
if (do_break) break; |
| 515 |
may_be_loop = false; |
| 516 |
} |
| 517 |
|
| 518 |
DEBUG_debug("Connecting to IP ", t, " port ", String(port)); |
| 519 |
int rc = sock.connect(t, port); |
| 520 |
if (rc == 0) { |
| 521 |
freeaddrinfo(infoptr); |
| 522 |
DEBUG_debug("Got connection upfailure is ", String(cm.upfailure) ); |
| 523 |
return 0; |
| 524 |
} |
| 525 |
} |
| 526 |
freeaddrinfo(infoptr); |
| 527 |
if (may_be_loop) break; |
| 528 |
lerr_mess = 203; |
| 529 |
continue; |
| 530 |
} |
| 531 |
} else { //is via proxy |
| 532 |
sock.setTimeout(o.net.proxy_timeout); |
| 533 |
int rc = sock.connect(o.net.proxy_ip, o.net.proxy_port); |
| 534 |
if (rc < 0) { |
| 535 |
if (sock.isTimedout()) |
| 536 |
lerr_mess = 201; |
| 537 |
else |
| 538 |
lerr_mess = 202; |
| 539 |
continue; |
| 540 |
} |
| 541 |
return rc; |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
// only get here if failed |
| 546 |
cm.upfailure = true; |
| 547 |
cm.message_no = lerr_mess; |
| 548 |
cm.whatIsNaughty = o.language_list.getTranslation(lerr_mess); |
| 549 |
cm.whatIsNaughtyLog = cm.whatIsNaughty; |
| 550 |
cm.whatIsNaughtyCategories = ""; |
| 551 |
cm.whatIsNaughtyDisplayCategories = ""; |
| 552 |
cm.isItNaughty = true; |
| 553 |
cm.blocktype = 3; |
| 554 |
cm.isexception = false; |
| 555 |
cm.isbypass = false; |
| 556 |
return -1; |
| 557 |
} |
| 558 |
|
| 559 |
// pass data between proxy and client, filtering as we go. |
| 560 |
// this is the only public function of ConnectionHandler |
| 561 |
int ConnectionHandler::handlePeer(Socket &peerconn, String &ip, stat_rec *&dystat, unsigned int lc_type) { |
| 562 |
persistent_authed = false; |
| 563 |
is_real_user = false; |
| 564 |
int rc = 0; |
| 565 |
Socket proxysock; // also used for direct connection |
| 566 |
|
| 567 |
switch (lc_type) { |
| 568 |
case CT_PROXY: |
| 569 |
SBauth.is_proxy = true; |
| 570 |
rc = handleConnection(peerconn, ip, false, proxysock, dystat); |
| 571 |
break; |
| 572 |
|
| 573 |
case CT_THTTPS: |
| 574 |
SBauth.is_transparent = true; |
| 575 |
rc = handleTHTTPSConnection(peerconn, ip, proxysock, dystat); |
| 576 |
break; |
| 577 |
|
| 578 |
case CT_PROXY_TLS: |
| 579 |
SBauth.is_proxy = true; |
| 580 |
rc = handleProxyTLSConnection(peerconn, ip, proxysock, dystat); |
| 581 |
break; |
| 582 |
|
| 583 |
case CT_ICAP: |
| 584 |
SBauth.is_icap = true; |
| 585 |
rc = handleICAPConnection(peerconn, ip, proxysock, dystat); |
| 586 |
break; |
| 587 |
|
| 588 |
} |
| 589 |
//if ( ldl->reload_id != load_id) |
| 590 |
// rc = -1; |
| 591 |
return rc; |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
int ConnectionHandler::handleConnection(Socket &peerconn, String &ip, bool ismitm, Socket &proxysock, |
| 596 |
stat_rec *&dystat) { |
| 597 |
struct timeval thestart; |
| 598 |
gettimeofday(&thestart, NULL); |
| 599 |
|
| 600 |
//peerconn.setTimeout(o.proxy_timeout); |
| 601 |
peerconn.setTimeout(o.net.pcon_timeout); |
| 602 |
DEBUG_proxy("down_stream thread ",peerconn.down_thread_id); |
| 603 |
|
| 604 |
// ldl = o.currentLists(); |
| 605 |
|
| 606 |
HTTPHeader docheader(__HEADER_RESPONSE); // to hold the returned page header from proxy |
| 607 |
HTTPHeader header(__HEADER_REQUEST); // to hold the incoming client request headeri(ldl) |
| 608 |
|
| 609 |
// set a timeout as we don't want blocking 4 eva |
| 610 |
// this also sets how long a peerconn will wait for other requests |
| 611 |
header.setTimeout(o.net.pcon_timeout); |
| 612 |
docheader.setTimeout(o.net.exchange_timeout); |
| 613 |
|
| 614 |
|
| 615 |
//int bypasstimestamp = 0; |
| 616 |
|
| 617 |
|
| 618 |
// Content scanning plugins to use for request (POST) & response data |
| 619 |
std::deque<CSPlugin *> requestscanners; |
| 620 |
std::deque<CSPlugin *> responsescanners; |
| 621 |
|
| 622 |
std::string clientip(ip.toCharArray()); // hold the clients ip |
| 623 |
header.setClientIP(ip); |
| 624 |
|
| 625 |
if (clienthost) delete clienthost; |
| 626 |
|
| 627 |
clienthost = NULL; // and the hostname, if available |
| 628 |
matchedip = false; |
| 629 |
|
| 630 |
// clear list of parameters extracted from URL |
| 631 |
urlparams.clear(); |
| 632 |
|
| 633 |
// clear out info about POST data |
| 634 |
postparts.clear(); |
| 635 |
|
| 636 |
// debug stuff surprisingly enough |
| 637 |
DEBUG_proxy(" -got peer connection from ", clientip ); |
| 638 |
|
| 639 |
try { |
| 640 |
//int rc; |
| 641 |
#ifdef DEBUG_HIGH |
| 642 |
int pcount = 0; |
| 643 |
#else |
| 644 |
#ifdef DEBUG_LOW |
| 645 |
int pcount = 0; |
| 646 |
#endif |
| 647 |
#endif |
| 648 |
|
| 649 |
// assume all requests over the one persistent connection are from |
| 650 |
// the same user. means we only need to query the auth plugin until |
| 651 |
// we get credentials, then assume they are valid for all reqs. on |
| 652 |
// the persistent connection. |
| 653 |
std::string oldclientuser; |
| 654 |
std::string room; |
| 655 |
|
| 656 |
//int oldfg = 0; |
| 657 |
bool authed = false; |
| 658 |
//bool isbanneduser = false; |
| 659 |
//bool isscanbypass = false; |
| 660 |
//bool isbypass = false; |
| 661 |
//bool isvirusbypass = false; |
| 662 |
//int bypasstimestamp = 0; |
| 663 |
//bool iscookiebypass = false; |
| 664 |
|
| 665 |
AuthPlugin *auth_plugin = NULL; |
| 666 |
|
| 667 |
// RFC states that connections are persistent |
| 668 |
bool persistOutgoing = true; |
| 669 |
bool persistPeer = true; |
| 670 |
bool persistProxy = true; |
| 671 |
String last_domain_port; |
| 672 |
bool last_isdirect = false; |
| 673 |
|
| 674 |
bool firsttime = true; |
| 675 |
if (!header.in(&peerconn, true)) { // get header from client, allowing persistency |
| 676 |
if (o.conn.logconerror) { |
| 677 |
if (peerconn.getFD() > -1) { |
| 678 |
|
| 679 |
int err = peerconn.getErrno(); |
| 680 |
//int pport = peerconn.getPeerSourcePort(); |
| 681 |
std::string peerIP = peerconn.getPeerIP(); |
| 682 |
|
| 683 |
E2LOGGER_info("No header recd from client at ", peerIP, " - errno: ", err); |
| 684 |
} else { |
| 685 |
E2LOGGER_info("Client connection closed early - no request header received"); |
| 686 |
} |
| 687 |
} |
| 688 |
firsttime = false; |
| 689 |
persistPeer = false; |
| 690 |
} else { |
| 691 |
++dystat->reqs; |
| 692 |
} |
| 693 |
// |
| 694 |
// End of set-up section |
| 695 |
// |
| 696 |
// Start of main loop |
| 697 |
// |
| 698 |
|
| 699 |
// maintain a persistent connection |
| 700 |
while ((firsttime || persistPeer) && !ttg) |
| 701 |
// while ((firsttime || persistPeer) && !reloadconfig) |
| 702 |
{ |
| 703 |
DEBUG_proxy(" firsttime =", firsttime, " ismitm =", ismitm, " clientuser =", clientuser, " group = ", filtergroup); |
| 704 |
ldl = o.currentLists(); |
| 705 |
NaughtyFilter checkme(header, docheader, SBauth); |
| 706 |
checkme.listen_port = peerconn.getPort(); |
| 707 |
DataBuffer docbody; |
| 708 |
docbody.setTimeout(o.net.exchange_timeout); |
| 709 |
FDTunnel fdt; |
| 710 |
|
| 711 |
if (firsttime) { |
| 712 |
// reset flags & objects next time round the loop |
| 713 |
firsttime = false; |
| 714 |
gettimeofday(&thestart, NULL); |
| 715 |
checkme.thestart = thestart; |
| 716 |
|
| 717 |
// quick trick for the very first connection :-) |
| 718 |
if (!ismitm) |
| 719 |
persistProxy = false; |
| 720 |
} else { |
| 721 |
// another round... |
| 722 |
DEBUG_proxy(" -persisting (count ", ++pcount, ") - ", clientip); |
| 723 |
header.reset(); |
| 724 |
if (!header.in(&peerconn, true)) { |
| 725 |
DEBUG_proxy(" -Persistent connection closed"); |
| 726 |
break; |
| 727 |
} |
| 728 |
++dystat->reqs; |
| 729 |
|
| 730 |
// we will actually need to do *lots* of resetting of flags etc. here for pconns to work |
| 731 |
gettimeofday(&thestart, NULL); |
| 732 |
checkme.thestart = thestart; |
| 733 |
|
| 734 |
checkme.bypasstimestamp = 0; |
| 735 |
|
| 736 |
authed = false; |
| 737 |
|
| 738 |
requestscanners.clear(); |
| 739 |
responsescanners.clear(); |
| 740 |
|
| 741 |
matchedip = false; |
| 742 |
urlparams.clear(); |
| 743 |
postparts.clear(); |
| 744 |
checkme.mimetype = "-"; |
| 745 |
room = ""; // CHECK THIS - surely room is persistant????? |
| 746 |
|
| 747 |
// reset docheader & docbody |
| 748 |
// headers *should* take care of themselves on the next in() |
| 749 |
// actually not entirely true for docheader - we may read |
| 750 |
// certain properties of it (in denyAccess) before we've |
| 751 |
// actually performed the next in(), so make sure we do a full |
| 752 |
// reset now. |
| 753 |
docheader.reset(); |
| 754 |
docbody.reset(); |
| 755 |
peerconn.resetChunk(); |
| 756 |
proxysock.resetChunk(); |
| 757 |
|
| 758 |
} |
| 759 |
// |
| 760 |
// do this normalisation etc just the once at the start. |
| 761 |
checkme.setURL(ismitm); |
| 762 |
|
| 763 |
if(o.log.log_requests) { |
| 764 |
//if (e2logger.isEnabled(LoggerSource::requestlog)) { |
| 765 |
std::string fnt; |
| 766 |
if(ismitm) |
| 767 |
fnt = "MITM"; |
| 768 |
else if(header.isProxyRequest) { |
| 769 |
fnt = "PROXY"; |
| 770 |
} else fnt = "TRANS"; |
| 771 |
doRQLog(clientuser, clientip, checkme, fnt); |
| 772 |
} |
| 773 |
|
| 774 |
if(!header.isProxyRequest) // is transparent http proxy |
| 775 |
get_original_ip_port(peerconn,checkme); |
| 776 |
|
| 777 |
//If proxy connection is not persistent..// do this later after checking if direct or via proxy |
| 778 |
|
| 779 |
DEBUG_proxy("Start URL ", checkme.url, "is_ssl=", checkme.is_ssl, "ismitm=", ismitm); |
| 780 |
|
| 781 |
// checks for bad URLs to prevent security holes/domain obfuscation. |
| 782 |
if (header.malformedURL(checkme.url)) { |
| 783 |
// The requested URL is malformed. |
| 784 |
writeback_error(checkme, peerconn, 200, 0, "400 Bad Request"); |
| 785 |
proxysock.close(); // close connection to proxy |
| 786 |
break; |
| 787 |
} |
| 788 |
|
| 789 |
// TODO this needs moving is proxy operation is still to be tested |
| 790 |
if (checkme.urldomain == o.conn.internal_test_url) { |
| 791 |
peerconn.writeString( |
| 792 |
"HTTP/1.1 200 \nContent-Type: text/html\n\n<HTML><HEAD><TITLE>e2guardian internal test</TITLE></HEAD><BODY><H1>e2guardian internal test OK</H1> "); |
| 793 |
peerconn.writeString("</BODY></HTML>\n"); |
| 794 |
proxysock.close(); // close connection to proxy |
| 795 |
break; |
| 796 |
} |
| 797 |
|
| 798 |
// total block list checking now done in pre-auth story |
| 799 |
|
| 800 |
// don't let the client connection persist if the client doesn't want it to. |
| 801 |
persistOutgoing = header.isPersistent(); |
| 802 |
// now check if in input proxy mode and direct upstream if upstream needs closing |
| 803 |
if (persistProxy && last_isdirect && |
| 804 |
((last_domain_port != checkme.urldomainport)|| !o.net.no_proxy)) { |
| 805 |
proxysock.close(); |
| 806 |
persistProxy = false; |
| 807 |
} |
| 808 |
last_domain_port = checkme.urldomainport; |
| 809 |
last_isdirect = checkme.isdirect; |
| 810 |
|
| 811 |
|
| 812 |
// |
| 813 |
// |
| 814 |
// Now check if machine is banned and room-based checking |
| 815 |
// |
| 816 |
// |
| 817 |
|
| 818 |
// is this user banned? |
| 819 |
//isbanneduser = false; |
| 820 |
#ifdef NOTDEF |
| 821 |
if(!ismitm) { |
| 822 |
// pretend to use xforwarded for |
| 823 |
clientip = "192.6.6.6"; |
| 824 |
ip = clientip; |
| 825 |
} |
| 826 |
#endif |
| 827 |
|
| 828 |
if (!ismitm && o.use_xforwardedfor) { |
| 829 |
bool use_xforwardedfor; |
| 830 |
if (o.net.xforwardedfor_filter_ip.size() > 0) { |
| 831 |
use_xforwardedfor = false; |
| 832 |
for (unsigned int i = 0; i < o.net.xforwardedfor_filter_ip.size(); i++) { |
| 833 |
if (strcmp(clientip.c_str(), o.net.xforwardedfor_filter_ip[i].c_str()) == 0) { |
| 834 |
use_xforwardedfor = true; |
| 835 |
break; |
| 836 |
} |
| 837 |
} |
| 838 |
} else { |
| 839 |
use_xforwardedfor = true; |
| 840 |
} |
| 841 |
if (use_xforwardedfor) { |
| 842 |
std::string xforwardip(header.getXForwardedForIP()); |
| 843 |
if (xforwardip.length() > 6) { |
| 844 |
clientip = xforwardip; |
| 845 |
ip = clientip; |
| 846 |
header.setClientIP(ip); |
| 847 |
} |
| 848 |
DEBUG_proxy(" -using x-forwardedfor:", clientip); |
| 849 |
} |
| 850 |
} |
| 851 |
checkme.clientip = clientip; |
| 852 |
|
| 853 |
// Look up reverse DNS name of client if needed |
| 854 |
if (o.conn.reverse_client_ip_lookups) { |
| 855 |
getClientFromIP(clientip.c_str(),checkme.clienthost); |
| 856 |
// std::unique_ptr<std::deque<String> > hostnames; |
| 857 |
// hostnames.reset(ipToHostname(clientip.c_str())); |
| 858 |
// checkme.clienthost = std::string(hostnames->front().toCharArray()); |
| 859 |
} |
| 860 |
|
| 861 |
//CALL SB pre-authcheck |
| 862 |
DEBUG_trace("Run StoryA pre-authcheck"); |
| 863 |
ldl->StoryA.runFunctEntry(ENT_STORYA_PRE_AUTH, checkme); |
| 864 |
DEBUG_proxy("After StoryA pre-authcheck", |
| 865 |
" isexception ", String(checkme.isexception), |
| 866 |
" isBlocked ", String(checkme.isBlocked), |
| 867 |
" message_no ", String(checkme.message_no)); |
| 868 |
|
| 869 |
checkme.isItNaughty = checkme.isBlocked; |
| 870 |
bool isbannedip = checkme.isBlocked; |
| 871 |
bool part_banned; |
| 872 |
if (isbannedip) { |
| 873 |
// matchedip = clienthost == NULL; |
| 874 |
DEBUG_proxy("IP is banned!"); |
| 875 |
} else { |
| 876 |
if (ldl->inRoom(clientip, room, &(checkme.clienthost), &isbannedip, &part_banned, &checkme.isexception, |
| 877 |
checkme.urld)) { |
| 878 |
|
| 879 |
DEBUG_proxy(" isbannedip = ", String(isbannedip), |
| 880 |
" ispart_banned = ", String(part_banned), |
| 881 |
" isexception = ", String(checkme.isexception)); |
| 882 |
|
| 883 |
if (isbannedip) { |
| 884 |
// matchedip = clienthost == NULL; |
| 885 |
checkme.isBlocked = checkme.isItNaughty = true; |
| 886 |
} |
| 887 |
if (checkme.isexception) { |
| 888 |
// do reason codes etc |
| 889 |
checkme.whatIsNaughty = o.language_list.getTranslation(630); |
| 890 |
checkme.whatIsNaughty.append(room); |
| 891 |
checkme.whatIsNaughty.append(o.language_list.getTranslation(631)); |
| 892 |
checkme.whatIsNaughtyLog = checkme.whatIsNaughty; |
| 893 |
checkme.message_no = 632; |
| 894 |
} |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
// |
| 900 |
// |
| 901 |
// Start of Authentication Checks |
| 902 |
// |
| 903 |
// |
| 904 |
// don't have credentials for this connection yet? get some! |
| 905 |
DEBUG_trace("start Authentication"); |
| 906 |
overide_persist = false; |
| 907 |
if (!persistent_authed) { |
| 908 |
bool only_ip_auth; |
| 909 |
if (header.isProxyRequest) { |
| 910 |
filtergroup = o.filter.default_fg; |
| 911 |
SBauth.is_proxy = true; |
| 912 |
if(!peerconn.down_thread_id.empty()) |
| 913 |
SBauth.is_tlsproxy = true; |
| 914 |
only_ip_auth = false; |
| 915 |
} else { |
| 916 |
filtergroup = o.filter.default_trans_fg; |
| 917 |
SBauth.is_transparent = true; |
| 918 |
only_ip_auth = true; |
| 919 |
} |
| 920 |
SBauth.group_source = "def"; |
| 921 |
DEBUG_proxy("isProxyRequest is ", String(header.isProxyRequest), |
| 922 |
" only_ip_auth is ", String(only_ip_auth) ); |
| 923 |
|
| 924 |
if (!doAuth(checkme.auth_result, authed, filtergroup, auth_plugin, peerconn, proxysock, header, checkme, |
| 925 |
only_ip_auth, |
| 926 |
checkme.isconnect)) { |
| 927 |
if (checkme.auth_result == E2AUTH_407_SENT) { |
| 928 |
continue; |
| 929 |
} |
| 930 |
if ((checkme.auth_result == E2AUTH_REDIRECT) && checkme.isconnect && |
| 931 |
ldl->fg[filtergroup]->ssl_mitm) { |
| 932 |
checkme.gomitm = true; |
| 933 |
checkme.isdone = true; |
| 934 |
} else { |
| 935 |
break; |
| 936 |
} |
| 937 |
} |
| 938 |
} else { |
| 939 |
DEBUG_proxy(" -Already got credentials for this connection - not querying auth plugins"); |
| 940 |
authed = true; |
| 941 |
} |
| 942 |
checkme.filtergroup = filtergroup; |
| 943 |
|
| 944 |
docbody.set_current_config(ldl->fg[filtergroup]); |
| 945 |
DEBUG_proxy(" -username: " + clientuser + " -filtergroup: " + String(filtergroup)); |
| 946 |
// |
| 947 |
// End of Authentication Checking |
| 948 |
// |
| 949 |
// |
| 950 |
|
| 951 |
// Set if candidate for MITM |
| 952 |
// (Exceptions will not go MITM) |
| 953 |
DEBUG_trace("ismitmcandidate"); |
| 954 |
checkme.ismitmcandidate = checkme.isconnect && (!checkme.nomitm) && ldl->fg[filtergroup]->ssl_mitm && (header.port == 443); |
| 955 |
if (checkme.ismitmcandidate ) { |
| 956 |
if(!ldl->fg[filtergroup]->automitm) checkme.automitm = false; |
| 957 |
} else { |
| 958 |
checkme.nomitm = true; |
| 959 |
checkme.automitm = false; |
| 960 |
} |
| 961 |
|
| 962 |
if (checkme.urldomain == o.conn.internal_status_url) { |
| 963 |
peerconn.writeString( |
| 964 |
"HTTP/1.1 200 \nContent-Type: text/html\n\n<HTML><HEAD><TITLE>e2guardian internal status</TITLE></HEAD><BODY><H1>e2guardian internal status OK</H1> "); |
| 965 |
String temp = "User: "; |
| 966 |
temp += clientuser; |
| 967 |
temp += "<br>"; |
| 968 |
temp += "IP: "; |
| 969 |
temp += ip; |
| 970 |
temp += "<br>"; |
| 971 |
temp += "Filtergroup: "; |
| 972 |
temp += ldl->fg[filtergroup]->name; |
| 973 |
temp += "<br>"; |
| 974 |
temp += "Flags: "; |
| 975 |
temp += checkme.getFlags(); |
| 976 |
temp += "<br>"; |
| 977 |
temp += "e2g version: "; |
| 978 |
temp += PACKAGE_VERSION; |
| 979 |
temp += "<br>"; |
| 980 |
temp += "Server: "; |
| 981 |
temp += o.net.server_name; |
| 982 |
temp += "<br>"; |
| 983 |
peerconn.writeString(temp); |
| 984 |
peerconn.writeString("</BODY></HTML>\n"); |
| 985 |
proxysock.close(); // close connection to proxy |
| 986 |
break; |
| 987 |
} |
| 988 |
// |
| 989 |
// Start of by pass |
| 990 |
// |
| 991 |
DEBUG_trace("checkByPass"); |
| 992 |
if (!checkme.isdone && checkByPass(checkme, ldl, header, proxysock, peerconn, clientip) |
| 993 |
&& sendScanFile(peerconn, checkme)) { |
| 994 |
persistProxy = false; |
| 995 |
break; |
| 996 |
} |
| 997 |
|
| 998 |
// |
| 999 |
// End by pass |
| 1000 |
// |
| 1001 |
|
| 1002 |
// virus checking candidate? |
| 1003 |
// checkme.noviruscheck defaults to true |
| 1004 |
if (!(checkme.isdone || checkme.isconnect || checkme.ishead) // can't scan connect or head or not yet authed |
| 1005 |
&& !(checkme.isBlocked) // or already blocked |
| 1006 |
&& authed // and is authed |
| 1007 |
&& (o.plugins.csplugins.size() > 0) // and we have scan plugins |
| 1008 |
&& !ldl->fg[filtergroup]->disable_content_scan // and is not disabled |
| 1009 |
&& !(checkme.isexception && !ldl->fg[filtergroup]->content_scan_exceptions) |
| 1010 |
// and not exception unless scan exceptions enabled |
| 1011 |
) |
| 1012 |
checkme.noviruscheck = false; // note this may be reset by Storyboard to enable exceptions |
| 1013 |
|
| 1014 |
// |
| 1015 |
// Start of Storyboard checking |
| 1016 |
// |
| 1017 |
// if (!(checkme.isBlocked || checkme.isbypass)) |
| 1018 |
if (!(checkme.isBlocked || checkme.isdone) && authed) { |
| 1019 |
// Main checking is now done in Storyboard function(s) |
| 1020 |
// String funct = "checkrequest"; |
| 1021 |
// ldl->fg[filtergroup]->StoryB.runFunct(funct, checkme); |
| 1022 |
DEBUG_trace("Check StoryB checkrequest"); |
| 1023 |
ldl->fg[filtergroup]->StoryB.runFunctEntry(ENT_STORYB_PROXY_REQUEST, checkme); |
| 1024 |
DEBUG_proxy("After StoryB checkrequest", |
| 1025 |
" isexception ", String(checkme.isexception ), |
| 1026 |
" isblocked ", String(checkme.isBlocked ), |
| 1027 |
" gomitm ", String(checkme.gomitm), |
| 1028 |
" mess_no ", String(checkme.message_no) |
| 1029 |
); |
| 1030 |
|
| 1031 |
if (ldl->fg[filtergroup]->reporting_level != -1){ |
| 1032 |
checkme.isItNaughty = checkme.isBlocked; |
| 1033 |
} else { |
| 1034 |
checkme.isItNaughty = false; |
| 1035 |
checkme.isBlocked = false; |
| 1036 |
} |
| 1037 |
} |
| 1038 |
|
| 1039 |
if (checkme.isdirect) { |
| 1040 |
header.setDirect(); |
| 1041 |
last_isdirect = true; |
| 1042 |
if(!o.net.no_proxy) { // we are in mixed mode proxy and direct |
| 1043 |
if(persistProxy) { // if upstream socket is open close it |
| 1044 |
proxysock.close(); |
| 1045 |
persistProxy = false; |
| 1046 |
} |
| 1047 |
} |
| 1048 |
} |
| 1049 |
|
| 1050 |
if (checkme.isbypass && !(checkme.iscookiebypass || checkme.isvirusbypass)) { |
| 1051 |
DEBUG_proxy("Setting GBYPASS cookie; bypasstimestamp = ", checkme.bypasstimestamp); |
| 1052 |
String ud(checkme.urldomain); |
| 1053 |
if (ud.startsWith("www.")) { |
| 1054 |
ud = ud.after("www."); |
| 1055 |
} |
| 1056 |
// redirect user to URL with GBYPASS parameter no longer appended |
| 1057 |
String outhead = "HTTP/1.1 302 Redirect\r\n"; |
| 1058 |
outhead += "Set-Cookie: GBYPASS="; |
| 1059 |
outhead += header.hashedCookie(&ud, ldl->fg[filtergroup]->cookie_magic.c_str(), &clientip, |
| 1060 |
checkme.bypasstimestamp, clientuser).toCharArray(); |
| 1061 |
// outhead += hashedCookie(&ud, ldl->fg[filtergroup]->cookie_magic.c_str(), &clientip, |
| 1062 |
// checkme.bypasstimestamp).toCharArray(); |
| 1063 |
outhead += "; path=/; domain=."; |
| 1064 |
outhead += ud; |
| 1065 |
outhead += "\r\n"; |
| 1066 |
outhead += "Location: "; |
| 1067 |
outhead += checkme.logurl.before("GBYPASS="); |
| 1068 |
outhead.chop(); |
| 1069 |
outhead += "\r\n"; |
| 1070 |
outhead += "\r\n"; |
| 1071 |
peerconn.writeString(outhead.c_str()); |
| 1072 |
return 0; |
| 1073 |
} |
| 1074 |
|
| 1075 |
|
| 1076 |
//check for redirect |
| 1077 |
// URL regexp search and edirect |
| 1078 |
if (checkme.urlredirect) { |
| 1079 |
checkme.url = header.redirecturl(); |
| 1080 |
proxysock.close(); |
| 1081 |
String writestring("HTTP/1.1 302 Redirect\nLocation: "); |
| 1082 |
writestring += checkme.url; |
| 1083 |
writestring += "\n\n"; |
| 1084 |
peerconn.writeString(writestring.toCharArray()); |
| 1085 |
break; |
| 1086 |
} |
| 1087 |
|
| 1088 |
//if is a search - content check search terms |
| 1089 |
if (!checkme.isdone) { |
| 1090 |
if (checkme.isGrey && checkme.isSearch) |
| 1091 |
check_search_terms(checkme); |
| 1092 |
} // will set isItNaughty if needed |
| 1093 |
|
| 1094 |
|
| 1095 |
// TODO V5 call POST scanning code New NaughtyFilter function???? |
| 1096 |
|
| 1097 |
// don't run willScanRequest if content scanning is disabled, or on exceptions if contentscanexceptions is off, |
| 1098 |
// or on SSL (CONNECT) requests, or on HEAD requests, or if in AV bypass mode |
| 1099 |
|
| 1100 |
//if not naughty now send upstream and get response |
| 1101 |
if (checkme.isItNaughty) { |
| 1102 |
if (checkme.isconnect && checkme.automitm) { |
| 1103 |
checkme.gomitm = true; // so that we can deliver a status message to user over half MITM |
| 1104 |
if (checkme.isdirect) { // send connection estabilished to client |
| 1105 |
std::string msg = "HTTP/1.1 200 Connection established\r\n\r\n"; |
| 1106 |
if (!peerconn.writeString(msg.c_str())) { |
| 1107 |
peerDiag("Unable to send 200 connection established to client ", peerconn); |
| 1108 |
break; |
| 1109 |
} |
| 1110 |
} |
| 1111 |
} else { |
| 1112 |
checkme.gomitm = false; // if not automitm |
| 1113 |
} |
| 1114 |
} else { |
| 1115 |
if (!persistProxy) // open upstream connection |
| 1116 |
{ |
| 1117 |
int out_port = header.port; |
| 1118 |
if (o.conn.use_original_ip_port && checkme.got_orig_ip && |
| 1119 |
!header.isProxyRequest) |
| 1120 |
out_port = checkme.orig_port; |
| 1121 |
if (connectUpstream(proxysock, checkme, out_port) < 0) { |
| 1122 |
if (checkme.isconnect && checkme.automitm && |
| 1123 |
checkme.upfailure) |
| 1124 |
{ |
| 1125 |
checkme.gomitm = true; // so that we can deliver a status message to user over half MITM |
| 1126 |
// to give error - depending on answer |
| 1127 |
// timeout -etc |
| 1128 |
} else { |
| 1129 |
// checkme.gomitm = false; // if not automitm |
| 1130 |
} |
| 1131 |
} |
| 1132 |
} |
| 1133 |
if (!checkme.upfailure) { |
| 1134 |
if (!proxysock.readyForOutput(o.net.proxy_timeout)) { |
| 1135 |
upstreamDiag("Unable to write upstream", proxysock); |
| 1136 |
break; |
| 1137 |
} |
| 1138 |
} |
| 1139 |
if (checkme.isdirect && checkme.isconnect) { // send connection estabilished to client |
| 1140 |
std::string msg = "HTTP/1.1 200 Connection established\r\n\r\n"; |
| 1141 |
if (!peerconn.writeString(msg.c_str())) { |
| 1142 |
peerDiag("Unable to send 200 connection established to client ", peerconn); |
| 1143 |
break; |
| 1144 |
} |
| 1145 |
} else if (!checkme.upfailure) // in all other cases send header upstream and get response |
| 1146 |
{ |
| 1147 |
// this is the case when there is no request body or rather when there is no expect_100 |
| 1148 |
// if(!(header.chunked || header.contentLength() > 0)) |
| 1149 |
if(!header.expects_100) // if has body is sent as part of header.out |
| 1150 |
{ |
| 1151 |
if (!(header.out(&peerconn, &proxysock, __E2HEADER_SENDALL, true) // send proxy the request |
| 1152 |
&& (docheader.in_handle_100(&proxysock, persistOutgoing, false)))) { |
| 1153 |
if (proxysock.isTimedout()) { |
| 1154 |
// writeback_error(checkme, peerconn, 203, 204, "408 Request Time-out"); |
| 1155 |
writeback_error(checkme, peerconn, 0, 0, "408 Request Time-out"); |
| 1156 |
} else { |
| 1157 |
if (!ismitm) { |
| 1158 |
writeback_error(checkme, peerconn, 0, 0, "408 Request Time-out"); |
| 1159 |
//writeback_error(checkme, peerconn, 205, 206, "502 Gateway Error"); |
| 1160 |
} |
| 1161 |
} |
| 1162 |
persistPeer = false; |
| 1163 |
persistProxy = false; |
| 1164 |
break; |
| 1165 |
} |
| 1166 |
} else { // has expects_100 |
| 1167 |
if (docheader.in_handle_100(&proxysock,persistOutgoing,header.expects_100)&& docheader.returncode == 100) { |
| 1168 |
docheader.out(&proxysock, &peerconn, __E2HEADER_SENDALL, true); |
| 1169 |
FDTunnel tpost; |
| 1170 |
tpost.tunnel(peerconn, proxysock, false, header.contentLength(), false, header.chunked); |
| 1171 |
if (docheader.in_handle_100(&proxysock, persistOutgoing, false) && |
| 1172 |
docheader.returncode == 100) { // get response header |
| 1173 |
DEBUG_proxy( |
| 1174 |
"somethig whent wroung with expoect 100 logic"); // should be now proper return header!!! |
| 1175 |
} |
| 1176 |
} |
| 1177 |
} |
| 1178 |
persistProxy = docheader.isPersistent(); |
| 1179 |
persistPeer = persistOutgoing && docheader.wasPersistent(); |
| 1180 |
|
| 1181 |
DEBUG_proxy(" -persistPeer: ", String(persistPeer)); |
| 1182 |
|
| 1183 |
//check response code |
| 1184 |
if ((!checkme.isItNaughty) && (!checkme.upfailure)) { |
| 1185 |
int rcode = docheader.returnCode(); |
| 1186 |
if (rcode == 407) { // proxy auth required |
| 1187 |
// tunnel thru - may be content |
| 1188 |
checkme.tunnel_rest = true; |
| 1189 |
checkme.tunnel_2way = false; |
| 1190 |
// treat connect like normal get |
| 1191 |
checkme.isconnect = false; |
| 1192 |
checkme.isexception = true; |
| 1193 |
} |
| 1194 |
if (checkme.isconnect) { |
| 1195 |
if (rcode == 200) { |
| 1196 |
persistProxy = false; |
| 1197 |
persistPeer = false; |
| 1198 |
} else { // some sort of problem |
| 1199 |
checkme.ismitmcandidate = false; // only applies to connect |
| 1200 |
checkme.tunnel_rest = true; |
| 1201 |
checkme.tunnel_2way = false; |
| 1202 |
} |
| 1203 |
} |
| 1204 |
|
| 1205 |
if (docheader.contentLength() == 0) // no content |
| 1206 |
checkme.tunnel_rest = true; |
| 1207 |
|
| 1208 |
} |
| 1209 |
} |
| 1210 |
|
| 1211 |
if (checkme.isconnect && checkme.isGrey) { // allow legacy SSL behavour when mitm not enabled |
| 1212 |
checkme.tunnel_2way = true; |
| 1213 |
checkme.tunnel_rest = false; |
| 1214 |
} |
| 1215 |
} |
| 1216 |
if ((checkme.isexception || checkme.logcategory) && !checkme.upfailure) { |
| 1217 |
if (checkme.isconnect) { |
| 1218 |
checkme.tunnel_2way = true; |
| 1219 |
checkme.tunnel_rest = false; |
| 1220 |
persistPeer = false; |
| 1221 |
persistProxy = false; |
| 1222 |
} else { |
| 1223 |
if (!checkme.noviruscheck && !ldl->fg[filtergroup]->content_scan_exceptions) |
| 1224 |
checkme.noviruscheck = true; |
| 1225 |
if (checkme.noviruscheck) { |
| 1226 |
checkme.tunnel_2way = false; |
| 1227 |
checkme.tunnel_rest = true; |
| 1228 |
} |
| 1229 |
} |
| 1230 |
} |
| 1231 |
|
| 1232 |
//if ismitm - GO MITM |
| 1233 |
// ssl_grey is covered in storyboard |
| 1234 |
if (!checkme.tunnel_rest && checkme.isconnect && checkme.gomitm) |
| 1235 |
{ |
| 1236 |
DEBUG_proxy("Going MITM ...."); |
| 1237 |
if(!ldl->fg[filtergroup]->mitm_check_cert) |
| 1238 |
checkme.nocheckcert = true; |
| 1239 |
goMITM(checkme, proxysock, peerconn, persistProxy, authed, persistent_authed, ip, dystat, clientip,checkme.isdirect); |
| 1240 |
persistPeer = false; |
| 1241 |
persistProxy = false; |
| 1242 |
//if (!checkme.isItNaughty) // surely we should just break here whatever? - No we need to log error |
| 1243 |
break; |
| 1244 |
} |
| 1245 |
|
| 1246 |
//CALL SB checkresponse |
| 1247 |
if ((!checkme.isItNaughty) && (!checkme.upfailure) && (!checkme.isconnect) && (!checkme.logcategory) && !checkme.tunnel_rest) { |
| 1248 |
ldl->fg[filtergroup]->StoryB.runFunctEntry(ENT_STORYB_PROXY_RESPONSE, checkme); |
| 1249 |
DEBUG_proxy("After StoryB checkresponse ", |
| 1250 |
" IsException ", String(checkme.isexception), |
| 1251 |
" IsBlocked ", String(checkme.isBlocked), |
| 1252 |
" mess_no ", String(checkme.message_no) ); |
| 1253 |
|
| 1254 |
if (ldl->fg[filtergroup]->reporting_level != -1){ |
| 1255 |
checkme.isItNaughty = checkme.isBlocked; |
| 1256 |
} else { |
| 1257 |
checkme.isItNaughty = false; |
| 1258 |
checkme.isBlocked = false; |
| 1259 |
checkme.isGrey = true; |
| 1260 |
} |
| 1261 |
|
| 1262 |
if (checkme.ishead || (docheader.contentLength() == 0 && !docheader.chunked)) |
| 1263 |
checkme.tunnel_rest = true; |
| 1264 |
|
| 1265 |
if (checkme.isexception && !ldl->fg[filtergroup]->content_scan_exceptions) { |
| 1266 |
checkme.tunnel_rest = true; |
| 1267 |
checkme.noviruscheck = true; |
| 1268 |
} |
| 1269 |
|
| 1270 |
if (!checkme.noviruscheck) { |
| 1271 |
for (std::deque<Plugin *>::iterator i = o.plugins.csplugins_begin; i != o.plugins.csplugins_end; ++i) { |
| 1272 |
int csrc = ((CSPlugin *) (*i))->willScanRequest(header.getUrl(), clientuser.c_str(), |
| 1273 |
ldl->fg[filtergroup], clientip.c_str(), false, |
| 1274 |
false, checkme.isexception, checkme.isbypass); |
| 1275 |
if (csrc > 0) |
| 1276 |
responsescanners.push_back((CSPlugin *) (*i)); |
| 1277 |
else if (csrc < 0) |
| 1278 |
E2LOGGER_error("willScanRequest returned error: ", csrc); |
| 1279 |
} |
| 1280 |
DEBUG_proxy(" -Content scanners interested in response data: ", responsescanners.size()); |
| 1281 |
} |
| 1282 |
|
| 1283 |
//- if grey content check |
| 1284 |
// can't do content filtering on HEAD or redirections (no content) |
| 1285 |
// actually, redirections CAN have content |
| 1286 |
if ((checkme.isGrey || !checkme.noviruscheck) && !checkme.tunnel_rest) { |
| 1287 |
check_content(checkme, docbody, proxysock, peerconn, responsescanners); |
| 1288 |
} |
| 1289 |
} |
| 1290 |
|
| 1291 |
//send response header to client(yments |
| 1292 |
if ((!checkme.isItNaughty) && (!checkme.upfailure) && !(checkme.isconnect && checkme.isdirect)) { |
| 1293 |
if (!docheader.out(NULL, &peerconn, __E2HEADER_SENDALL, false)) { |
| 1294 |
peerDiag("Unable to send return header to client", peerconn); |
| 1295 |
break; |
| 1296 |
} |
| 1297 |
|
| 1298 |
if ((!checkme.isItNaughty) && checkme.waschecked) { |
| 1299 |
if (docbody.dontsendbody && docbody.tempfilefd > -1) { |
| 1300 |
//#ifdef NOTDEF |
| 1301 |
// must have been a 'fancy' |
| 1302 |
// download manager so we need to send a special link which |
| 1303 |
// will get recognised and cause DG to send the temp file to |
| 1304 |
// the browser. The link will be the original URL with some |
| 1305 |
// magic appended to it like the bypass system. |
| 1306 |
|
| 1307 |
// format is: |
| 1308 |
// GSBYPASS=hash(ip+url+tempfilename+mime+disposition+secret) |
| 1309 |
// &N=tempfilename&M=mimetype&D=dispos |
| 1310 |
|
| 1311 |
String ip(clientip); |
| 1312 |
String tempfilename(docbody.tempfilepath.after("/tf")); |
| 1313 |
String tempfilemime(docheader.getContentType()); |
| 1314 |
String tempfiledis(miniURLEncode(docheader.disposition().toCharArray()).c_str()); |
| 1315 |
String secret(ldl->fg[filtergroup]->magic.c_str()); |
| 1316 |
String magic(ip + checkme.url + tempfilename + tempfilemime + tempfiledis + secret); |
| 1317 |
String hashed(magic.md5()); |
| 1318 |
DEBUG_proxy(" -sending magic link to client: ", ip, " ", checkme.url, " ", tempfilename, " ", tempfilemime, " ", tempfiledis, " ", secret, " ", hashed ); |
| 1319 |
String sendurl(checkme.url); |
| 1320 |
if (!sendurl.after("://").contains("/")) { |
| 1321 |
sendurl += "/"; |
| 1322 |
} |
| 1323 |
if (sendurl.contains("?")) { |
| 1324 |
sendurl += "&GSBYPASS="; |
| 1325 |
} else { |
| 1326 |
sendurl += "?GSBYPASS="; |
| 1327 |
} |
| 1328 |
sendurl += hashed; |
| 1329 |
sendurl += "&N="; |
| 1330 |
sendurl += tempfilename; |
| 1331 |
sendurl += "&M="; |
| 1332 |
sendurl += tempfilemime; |
| 1333 |
sendurl += "&D="; |
| 1334 |
sendurl += tempfiledis; |
| 1335 |
docbody.dm_plugin->sendLink(peerconn, sendurl, checkme.url); |
| 1336 |
|
| 1337 |
// can't persist after this - DM plugins don't generally send a Content-Length. |
| 1338 |
//TODO: need to change connection: close if there is plugin involved. |
| 1339 |
persistOutgoing = false; |
| 1340 |
//#endif |
| 1341 |
} else { |
| 1342 |
if (!docheader.header_all_sent) { |
| 1343 |
docheader.out(NULL,&peerconn,__E2HEADER_SENDALL,false); |
| 1344 |
} |
| 1345 |
if (!docbody.out(&peerconn)) { |
| 1346 |
DEBUG_network(" docbody.out returned error"); |
| 1347 |
checkme.pausedtoobig = false; |
| 1348 |
} |
| 1349 |
if (checkme.pausedtoobig) |
| 1350 |
checkme.tunnel_rest = true; |
| 1351 |
} |
| 1352 |
} |
| 1353 |
} |
| 1354 |
|
| 1355 |
//if not grey tunnel response |
| 1356 |
if (!checkme.isItNaughty) { |
| 1357 |
if (checkme.tunnel_rest) { |
| 1358 |
bool chunked = docheader.transferEncoding().contains("chunked"); |
| 1359 |
DEBUG_proxy(" -Tunnelling to client"); |
| 1360 |
DEBUG_proxy(" - Content-Length:", docheader.contentLength(), " cm.docsize:", checkme.docsize); |
| 1361 |
|
| 1362 |
if (!fdt.tunnel(proxysock, peerconn, checkme.isconnect, docheader.contentLength() - checkme.docsize, |
| 1363 |
true, chunked)) |
| 1364 |
persistProxy = false; |
| 1365 |
checkme.docsize += fdt.throughput; |
| 1366 |
} else if (checkme.tunnel_2way) { |
| 1367 |
if (!fdt.tunnel(proxysock, peerconn, true)) |
| 1368 |
persistProxy = false; |
| 1369 |
checkme.docsize = fdt.throughput; |
| 1370 |
} |
| 1371 |
} |
| 1372 |
|
| 1373 |
|
| 1374 |
DEBUG_proxy(" -Forwarding body to client :", |
| 1375 |
" Upfailure is ", String(checkme.upfailure), |
| 1376 |
" isItNaughty is ", String(checkme.isItNaughty)); |
| 1377 |
|
| 1378 |
if (checkme.upfailure || checkme.isItNaughty) { |
| 1379 |
if (denyAccess(&peerconn, &proxysock, &header, &docheader, &checkme.url, &checkme, &clientuser, |
| 1380 |
&clientip, |
| 1381 |
filtergroup, checkme.ispostblock, checkme.headersent, checkme.wasinfected, |
| 1382 |
checkme.scanerror)) |
| 1383 |
persistPeer = false; |
| 1384 |
} |
| 1385 |
|
| 1386 |
//Log |
| 1387 |
doLog(clientuser, clientip, checkme); |
| 1388 |
|
| 1389 |
|
| 1390 |
if (!persistProxy) |
| 1391 |
proxysock.close(); // close connection to proxy |
| 1392 |
|
| 1393 |
if (persistPeer && !checkme.isconnect) { |
| 1394 |
continue; |
| 1395 |
} |
| 1396 |
|
| 1397 |
break; |
| 1398 |
} |
| 1399 |
} catch (std::exception &e) { |
| 1400 |
DEBUG_proxy(" -connection handler caught an exception: ", e.what()); |
| 1401 |
if (o.conn.logconerror) |
| 1402 |
E2LOGGER_error("-connection handler caught an exception %s", e.what()); |
| 1403 |
|
| 1404 |
// close connection to proxy |
| 1405 |
proxysock.close(); |
| 1406 |
return -1; |
| 1407 |
} |
| 1408 |
if (!ismitm) |
| 1409 |
try { |
| 1410 |
DEBUG_proxy(" -Attempting graceful connection close"); |
| 1411 |
//syslog(LOG_INFO, " -Attempting graceful connection close" ); |
| 1412 |
int fd = peerconn.getFD(); |
| 1413 |
if (fd > -1) { |
| 1414 |
if (shutdown(fd, SHUT_WR) == 0) { |
| 1415 |
char buff[2]; |
| 1416 |
peerconn.readFromSocket(buff, 2, 0, 5000); |
| 1417 |
}; |
| 1418 |
}; |
| 1419 |
|
| 1420 |
// close connection to the client |
| 1421 |
peerconn.close(); |
| 1422 |
proxysock.close(); |
| 1423 |
} catch (std::exception &e) { |
| 1424 |
DEBUG_debug(" -connection handler caught an exception on connection closedown: ", e.what() ); |
| 1425 |
// close connection to the client |
| 1426 |
peerconn.close(); |
| 1427 |
proxysock.close(); |
| 1428 |
} |
| 1429 |
|
| 1430 |
return 0; |
| 1431 |
} |
| 1432 |
|
| 1433 |
|
| 1434 |
void ConnectionHandler::doLog(std::string &who, std::string &from, NaughtyFilter &cm) { |
| 1435 |
|
| 1436 |
DEBUG_trace("who: ", who, " from: ", from ); |
| 1437 |
|
| 1438 |
if (cm.isourwebserver) cm.nolog = true; |
| 1439 |
|
| 1440 |
struct timeval theend; |
| 1441 |
gettimeofday(&theend, NULL); |
| 1442 |
String rtype = cm.request_header->requestType(); |
| 1443 |
String where = cm.get_logUrl(); |
| 1444 |
unsigned int port = cm.request_header->port; |
| 1445 |
bool isexception = cm.isexception; |
| 1446 |
std::string *cat = &cm.whatIsNaughtyCategories; |
| 1447 |
std::string what; |
| 1448 |
|
| 1449 |
// don't log if logging disabled entirely, or if it's an ad block and ad logging is disabled, |
| 1450 |
// or if it's an exception and exception logging is disabled |
| 1451 |
if ( |
| 1452 |
(o.log.log_level == 0) || ((cat != NULL) && !o.log.log_ad_blocks && (strstr(cat->c_str(), "ADs") != NULL)) || |
| 1453 |
((o.log.log_exception_hits == 0) && isexception)) { |
| 1454 |
if (o.log.log_level != 0) { |
| 1455 |
if (isexception) { |
| 1456 |
DEBUG_debug(" -Not logging exceptions"); |
| 1457 |
} else { |
| 1458 |
DEBUG_debug(" -Not logging 'ADs' blocks"); |
| 1459 |
} |
| 1460 |
} |
| 1461 |
cm.nolog = true; |
| 1462 |
} |
| 1463 |
|
| 1464 |
if(!cm.nolog) ldl->fg[filtergroup]->StoryB.runFunctEntry(ENT_STORYB_LOG_CHECK, cm); |
| 1465 |
//if(cm.nolog) return; |
| 1466 |
|
| 1467 |
if (e2logger.isEnabled(LoggerSource::requestlog)) { |
| 1468 |
what = thread_id; |
| 1469 |
} |
| 1470 |
what += cm.whatIsNaughtyLog; |
| 1471 |
String how = rtype; |
| 1472 |
off_t size = cm.docsize; |
| 1473 |
bool isnaughty = cm.isItNaughty; |
| 1474 |
int naughtytype = cm.blocktype; |
| 1475 |
bool istext = cm.is_text; |
| 1476 |
struct timeval *thestart = &cm.thestart; |
| 1477 |
bool cachehit = false; |
| 1478 |
int code = (cm.response_header->returnCode()); |
| 1479 |
if (isnaughty) code = 403; |
| 1480 |
std::string mimetype = cm.response_header->getContentType(); |
| 1481 |
bool wasinfected = cm.wasinfected; |
| 1482 |
bool wasscanned = cm.wasscanned; |
| 1483 |
int naughtiness = cm.naughtiness; |
| 1484 |
int filtergroup = cm.filtergroup; |
| 1485 |
HTTPHeader *reqheader = cm.request_header; |
| 1486 |
int message_no = cm.message_no; |
| 1487 |
bool contentmodified = cm.contentmodified; |
| 1488 |
bool urlmodified = cm.urlmodified; |
| 1489 |
bool headermodified = cm.headermodified; |
| 1490 |
bool headeradded = cm.headeradded; |
| 1491 |
|
| 1492 |
|
| 1493 |
std::string data, cr("\n"); |
| 1494 |
|
| 1495 |
if (!((isexception && (o.log.log_exception_hits == 2)) || isnaughty || o.log.log_level == 3 || (o.log.log_level == 2 && istext))) |
| 1496 |
cm.nolog = true; |
| 1497 |
|
| 1498 |
if(!cm.nolog || (cm.alert && e2logger.isEnabled(LoggerSource::alertlog)) || e2logger.isEnabled(LoggerSource::responselog)){ |
| 1499 |
// put client hostname in log if enabled. |
| 1500 |
// for banned & exception IP/hostname matches, we want to output exactly what was matched against, |
| 1501 |
// be it hostname or IP - therefore only do lookups here when we don't already have a cached hostname, |
| 1502 |
// and we don't have a straight IP match agaisnt the banned or exception IP lists. |
| 1503 |
if (o.log.log_client_hostnames && (cm.clienthost == "") && !matchedip && !cm.anon_user) { |
| 1504 |
DEBUG_debug("logclienthostnames enabled but reverseclientiplookups disabled; lookup forced."); |
| 1505 |
getClientFromIP(from.c_str(),cm.clienthost); |
| 1506 |
} |
| 1507 |
|
| 1508 |
// Build up string describing POST data parts, if any |
| 1509 |
std::ostringstream postdata; |
| 1510 |
for (std::list<postinfo>::iterator i = postparts.begin(); i != postparts.end(); ++i) { |
| 1511 |
// Replace characters which would break log format with underscores |
| 1512 |
std::string::size_type loc = 0; |
| 1513 |
while ((loc = i->filename.find_first_of(",;\t ", loc)) != std::string::npos) |
| 1514 |
i->filename[loc] = '_'; |
| 1515 |
// Build up contents of log column |
| 1516 |
postdata << i->mimetype << "," << i->filename << "," << i->size |
| 1517 |
<< "," << i->blocked << "," << i->storedname << "," << i->bodyoffset << ";"; |
| 1518 |
} |
| 1519 |
postdata << std::flush; |
| 1520 |
|
| 1521 |
// Formatting code moved into log_listener in FatController.cpp |
| 1522 |
|
| 1523 |
// Item length limit put back to avoid log listener |
| 1524 |
// overload with very long urls Philip Pearce Jan 2014 |
| 1525 |
if ((cat != NULL) && (cat->length() > o.log.max_logitem_length)) |
| 1526 |
cat->resize(o.log.max_logitem_length); |
| 1527 |
if (what.length() > o.log.max_logitem_length) |
| 1528 |
what.resize(o.log.max_logitem_length); |
| 1529 |
if (where.length() > o.log.max_logitem_length) |
| 1530 |
where.limitLength(o.log.max_logitem_length); |
| 1531 |
if (o.log.dns_user_logging() && !is_real_user) { |
| 1532 |
String user; |
| 1533 |
if (getdnstxt(from, user)) { |
| 1534 |
who = who + ":" + user; |
| 1535 |
SBauth.user_name = user; |
| 1536 |
SBauth.user_source = "dnslog"; |
| 1537 |
}; |
| 1538 |
is_real_user = true; // avoid looping on persistent connections |
| 1539 |
}; |
| 1540 |
std::string l_who = who; |
| 1541 |
std::string l_from = from; |
| 1542 |
std::string l_clienthost; |
| 1543 |
l_clienthost = cm.clienthost; |
| 1544 |
|
| 1545 |
if (cm.anon_user) { |
| 1546 |
l_who = ""; |
| 1547 |
l_from = "0.0.0.0"; |
| 1548 |
l_clienthost = ""; |
| 1549 |
} |
| 1550 |
|
| 1551 |
// populate flags field |
| 1552 |
String flags = cm.getFlags(); |
| 1553 |
|
| 1554 |
DEBUG_debug(" -Building raw log data string... "); |
| 1555 |
|
| 1556 |
data = String(isexception) + cr; |
| 1557 |
data += (cat ? (*cat) + cr : cr); |
| 1558 |
data += String(isnaughty) + cr; |
| 1559 |
data += String(naughtytype) + cr; |
| 1560 |
data += String(naughtiness) + cr; |
| 1561 |
data += where + cr; |
| 1562 |
data += what + cr; |
| 1563 |
data += how + cr; |
| 1564 |
data += l_who + cr; |
| 1565 |
data += l_from + cr; |
| 1566 |
data += String(port) + cr; |
| 1567 |
data += String(wasscanned) + cr; |
| 1568 |
data += String(wasinfected) + cr; |
| 1569 |
data += String(contentmodified) + cr; |
| 1570 |
data += String(urlmodified) + cr; |
| 1571 |
data += String(headermodified) + cr; |
| 1572 |
data += String(size) + cr; |
| 1573 |
data += String(filtergroup) + cr; |
| 1574 |
data += String(code) + cr; |
| 1575 |
data += String(cachehit) + cr; |
| 1576 |
data += String(mimetype) + cr; |
| 1577 |
data += String((*thestart).tv_sec) + cr; |
| 1578 |
data += String((*thestart).tv_usec) + cr; |
| 1579 |
data += String((theend).tv_sec) + cr; |
| 1580 |
data += String((theend).tv_usec) + cr; |
| 1581 |
data += l_clienthost + cr; |
| 1582 |
|
| 1583 |
if (o.log.log_user_agent) |
| 1584 |
data += (reqheader ? reqheader->userAgent() + cr : cr); |
| 1585 |
else |
| 1586 |
data += cr; |
| 1587 |
data += urlparams + cr; |
| 1588 |
data += postdata.str().c_str() + cr; |
| 1589 |
data += String(message_no) + cr; |
| 1590 |
data += String(headeradded) + cr; |
| 1591 |
data += flags + cr; |
| 1592 |
data += cm.search_terms; |
| 1593 |
data += cr; |
| 1594 |
data += String(!cm.nolog) + cr; |
| 1595 |
data += String(cm.alert) + cr; |
| 1596 |
data += String(cm.issemiexception) + cr; |
| 1597 |
|
| 1598 |
DEBUG_debug(" -...built"); |
| 1599 |
|
| 1600 |
// push on log queue |
| 1601 |
o.log.log_Q->push(data); |
| 1602 |
// connect to dedicated logging proc |
| 1603 |
} |
| 1604 |
} |
| 1605 |
|
| 1606 |
void ConnectionHandler::doRQLog(std::string &who, std::string &from, NaughtyFilter &cm, std::string &funct) { |
| 1607 |
DEBUG_trace("who: ", who, " from: ", from, "funct: ", funct); |
| 1608 |
String rtype = cm.request_header->requestType(); |
| 1609 |
String where = cm.logurl; |
| 1610 |
unsigned int port = cm.request_header->port; |
| 1611 |
std::string what = thread_id; |
| 1612 |
what += funct; |
| 1613 |
if (cm.isTLS) |
| 1614 |
what += "_TLS"; |
| 1615 |
if (cm.hasSNI) |
| 1616 |
what += "_SNI"; |
| 1617 |
if (cm.ismitm) |
| 1618 |
what += "_MITM"; |
| 1619 |
String how = rtype; |
| 1620 |
off_t size = cm.docsize; |
| 1621 |
std::string *cat = nullptr; //&cm.whatIsNaughtyCategories; |
| 1622 |
bool isnaughty = cm.isItNaughty; |
| 1623 |
int naughtytype = cm.blocktype; |
| 1624 |
bool isexception = cm.isexception; |
| 1625 |
struct timeval *thestart = &cm.thestart; |
| 1626 |
bool cachehit = false; |
| 1627 |
int code = 0; |
| 1628 |
std::string mimetype = ""; //cm.mimetype; |
| 1629 |
bool wasinfected = false; //cm.wasinfected; |
| 1630 |
bool wasscanned = false; //cm.wasscanned; |
| 1631 |
int naughtiness = 0; //cm.naughtiness; |
| 1632 |
int filtergroup = cm.filtergroup; |
| 1633 |
HTTPHeader *reqheader = cm.request_header; |
| 1634 |
int message_no = cm.message_no; |
| 1635 |
bool contentmodified = false; //cm.contentmodified; |
| 1636 |
bool urlmodified = false; //cm.urlmodified; |
| 1637 |
bool headermodified = false; //cm.headermodified; |
| 1638 |
bool headeradded = false; //cm.headeradded; |
| 1639 |
|
| 1640 |
std::string data, cr("\n"); |
| 1641 |
|
| 1642 |
// if ((isexception && (o.log_exception_hits == 2)) |
| 1643 |
// || isnaughty || o.ll == 3 || (o.ll == 2 && istext)) |
| 1644 |
if(true) { |
| 1645 |
|
| 1646 |
// Item length limit put back to avoid log listener |
| 1647 |
// overload with very long urls Philip Pearce Jan 2014 |
| 1648 |
if (what.length() > o.log.max_logitem_length) |
| 1649 |
what.resize(o.log.max_logitem_length); |
| 1650 |
if (where.length() > o.log.max_logitem_length) |
| 1651 |
where.limitLength(o.log.max_logitem_length); |
| 1652 |
if (o.log.dns_user_logging() && !is_real_user) { |
| 1653 |
String user; |
| 1654 |
if (getdnstxt(from, user)) { |
| 1655 |
who = who + ":" + user; |
| 1656 |
}; |
| 1657 |
is_real_user = true; // avoid looping on persistent connections |
| 1658 |
}; |
| 1659 |
std::string l_who = who; |
| 1660 |
std::string l_from = from; |
| 1661 |
std::string l_clienthost; |
| 1662 |
l_clienthost = cm.clienthost; |
| 1663 |
String flags = cm.getFlags(); |
| 1664 |
|
| 1665 |
DEBUG_debug(" -Building raw log data string... "); |
| 1666 |
|
| 1667 |
data = String(isexception) + cr; |
| 1668 |
data += (cat ? (*cat) + cr : cr); |
| 1669 |
data += String(isnaughty) + cr; |
| 1670 |
data += String(naughtytype) + cr; |
| 1671 |
data += String(naughtiness) + cr; |
| 1672 |
data += where + cr; |
| 1673 |
data += what + cr; |
| 1674 |
data += how + cr; |
| 1675 |
data += l_who + cr; |
| 1676 |
data += l_from + cr; |
| 1677 |
data += String(port) + cr; |
| 1678 |
data += String(wasscanned) + cr; |
| 1679 |
data += String(wasinfected) + cr; |
| 1680 |
data += String(contentmodified) + cr; |
| 1681 |
data += String(urlmodified) + cr; |
| 1682 |
data += String(headermodified) + cr; |
| 1683 |
data += String(size) + cr; |
| 1684 |
data += String(filtergroup) + cr; |
| 1685 |
data += String(code) + cr; |
| 1686 |
data += String(cachehit) + cr; |
| 1687 |
data += String(mimetype) + cr; |
| 1688 |
data += String((*thestart).tv_sec) + cr; |
| 1689 |
data += String((*thestart).tv_usec) + cr; |
| 1690 |
data += String((*thestart).tv_sec) + cr; |
| 1691 |
data += String((*thestart).tv_usec) + cr; |
| 1692 |
data += l_clienthost + cr; |
| 1693 |
if (o.log.log_user_agent) |
| 1694 |
data += (reqheader ? reqheader->userAgent() + cr : cr); |
| 1695 |
else |
| 1696 |
data += cr; |
| 1697 |
data += urlparams + cr; |
| 1698 |
data += cr; |
| 1699 |
data += String(message_no) + cr; |
| 1700 |
data += String(headeradded) + cr; |
| 1701 |
data += flags + cr; |
| 1702 |
data += cr; //cm.search_terms not known yet |
| 1703 |
data += cr; |
| 1704 |
data += cr; // nolog |
| 1705 |
data += cr; // alert |
| 1706 |
data += cr; // semiexception |
| 1707 |
|
| 1708 |
DEBUG_debug(" -...built"); |
| 1709 |
|
| 1710 |
//delete newcat; |
| 1711 |
// push on log queue |
| 1712 |
o.log.RQlog_Q->push(data); |
| 1713 |
// connect to dedicated logging proc |
| 1714 |
} |
| 1715 |
} |
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
// based on patch by Aecio F. Neto ([email protected]) - Harvest Consultoria (http://www.harvest.com.br) |
| 1720 |
// show the relevant banned page/image/CGI based on report level setting, request type etc. |
| 1721 |
bool ConnectionHandler::genDenyAccess(Socket &peerconn, String &eheader, String &ebody, HTTPHeader *header, |
| 1722 |
HTTPHeader *docheader, |
| 1723 |
String *url, NaughtyFilter *checkme, std::string *clientuser, |
| 1724 |
std::string *clientip, int filtergroup, |
| 1725 |
bool ispostblock, int headersent, bool wasinfected, bool scanerror, |
| 1726 |
bool forceshow) { |
| 1727 |
int reporting_level = ldl->fg[filtergroup]->reporting_level; |
| 1728 |
DEBUG_debug(" -reporting level is ", reporting_level); |
| 1729 |
if (checkme->whatIsNaughty == "" && checkme->message_no > 0) { |
| 1730 |
checkme->whatIsNaughty = o.language_list.getTranslation(checkme->message_no); |
| 1731 |
} |
| 1732 |
if (checkme->whatIsNaughtyLog == "") { |
| 1733 |
if (checkme->log_message_no > 0) { |
| 1734 |
checkme->whatIsNaughtyLog = o.language_list.getTranslation(checkme->log_message_no); |
| 1735 |
} else { |
| 1736 |
checkme->whatIsNaughtyLog = checkme->whatIsNaughty; |
| 1737 |
} |
| 1738 |
} |
| 1739 |
|
| 1740 |
try { // writestring throws exception on error/timeout |
| 1741 |
|
| 1742 |
// flags to enable filter/infection / hash generation |
| 1743 |
bool filterhash = false; |
| 1744 |
bool virushash = false; |
| 1745 |
// flag to enable internal generation of hashes (i.e. obey the "-1" setting; to allow the modes but disable hash generation) |
| 1746 |
// (if disabled, just output '1' or '2' to show that the CGI should generate a filter/virus bypass hash; |
| 1747 |
// otherwise, real hashes get put into substitution variables/appended to the ban CGI redirect URL) |
| 1748 |
bool dohash = false; |
| 1749 |
String flags = checkme->getFlags(); |
| 1750 |
if (reporting_level > 0) { |
| 1751 |
// generate a filter bypass hash |
| 1752 |
if (!wasinfected && (checkme->isbypassallowed) && !ispostblock) { |
| 1753 |
DEBUG_debug(" -Enabling filter bypass hash generation"); |
| 1754 |
filterhash = true; |
| 1755 |
if (ldl->fg[filtergroup]->bypass_mode > 0 ) |
| 1756 |
dohash = true; |
| 1757 |
} |
| 1758 |
// generate an infection bypass hash |
| 1759 |
else if (wasinfected && checkme->isinfectionbypassallowed) { |
| 1760 |
// only generate if scanerror (if option to only bypass scan errors is enabled) |
| 1761 |
if ((*ldl->fg[filtergroup]).infection_bypass_errors_only ? scanerror : true) { |
| 1762 |
DEBUG_debug(" -Enabling infection bypass hash generation"); |
| 1763 |
virushash = true; |
| 1764 |
if (ldl->fg[filtergroup]->infection_bypass_mode > 0) |
| 1765 |
dohash = true; |
| 1766 |
} |
| 1767 |
} |
| 1768 |
} |
| 1769 |
DEBUG_debug(" - filter bypass hash generation", " virushah ", virushash, " dohash", dohash, " filterhash ", filterhash); |
| 1770 |
|
| 1771 |
// the user is using the full whack of custom banned images and/or HTML templates |
| 1772 |
if (reporting_level == 3 || (headersent > 0 && reporting_level > 0) || forceshow || (*header).requestType().startsWith("CONNECT")) |
| 1773 |
{ |
| 1774 |
|
| 1775 |
// if reporting_level = 1 or 2 and headersent then we can't |
| 1776 |
// send a redirect so we have to display the template instead |
| 1777 |
|
| 1778 |
|
| 1779 |
if ((*header).requestType().startsWith("CONNECT") && !(peerconn).isSsl()) |
| 1780 |
{ |
| 1781 |
// Block ssl website |
| 1782 |
// Buggy with FF < 65 https://bugzilla.mozilla.org/show_bug.cgi?id=1522093 |
| 1783 |
// Connections still opened after a refresh |
| 1784 |
// 403 requests made ICAP error with high load |
| 1785 |
eheader = "HTTP/1.1 302 Redirect"; |
| 1786 |
eheader += "\r\nLocation: http://internal.test.e2guardian.org"; |
| 1787 |
eheader += "\r\nServer: e2guardian"; |
| 1788 |
eheader += "\r\nConnection: close"; |
| 1789 |
eheader += "\r\n\r\n"; |
| 1790 |
} else { |
| 1791 |
// we're dealing with a non-SSL'ed request, and have the option of using the custom banned image/page directly |
| 1792 |
bool replaceimage = false; |
| 1793 |
bool replaceflash = false; |
| 1794 |
if (o.block.use_custom_banned_image) { |
| 1795 |
|
| 1796 |
// It would be much nicer to do a mime comparison |
| 1797 |
// and see if the type is image/* but the header |
| 1798 |
// never (almost) gets back from squid because |
| 1799 |
// it gets denied before then. |
| 1800 |
// This method is prone to over image replacement |
| 1801 |
// but will work most of the time. |
| 1802 |
|
| 1803 |
String lurl((*url)); |
| 1804 |
lurl.toLower(); |
| 1805 |
if (lurl.endsWith(".gif") || lurl.endsWith(".jpg") || lurl.endsWith(".jpeg") || |
| 1806 |
lurl.endsWith(".jpe") |
| 1807 |
|| lurl.endsWith(".png") || lurl.endsWith(".bmp") || |
| 1808 |
(*docheader).isContentType("image/", ldl->fg[filtergroup])) { |
| 1809 |
replaceimage = true; |
| 1810 |
} |
| 1811 |
} |
| 1812 |
|
| 1813 |
if (o.block.use_custom_banned_flash) { |
| 1814 |
String lurl((*url)); |
| 1815 |
lurl.toLower(); |
| 1816 |
if (lurl.endsWith(".swf") || |
| 1817 |
(*docheader).isContentType("application/x-shockwave-flash", ldl->fg[filtergroup])) { |
| 1818 |
replaceflash = true; |
| 1819 |
} |
| 1820 |
} |
| 1821 |
|
| 1822 |
// if we're denying an image request, show the image; otherwise, show the HTML page. |
| 1823 |
// (or advanced ad block page, or HTML page with bypass URLs) |
| 1824 |
if (replaceimage) { |
| 1825 |
if (headersent == 0) { |
| 1826 |
eheader = "HTTP/1.1 200 OK\r\n"; |
| 1827 |
} |
| 1828 |
o.block.banned_image.display_hb(eheader, ebody); |
| 1829 |
} else if (replaceflash) { |
| 1830 |
if (headersent == 0) { |
| 1831 |
eheader = "HTTP/1.1 200 OK\r\n"; |
| 1832 |
} |
| 1833 |
o.block.banned_flash.display_hb(eheader, ebody); |
| 1834 |
} else { |
| 1835 |
// advanced ad blocking - if category contains ADs, wrap ad up in an "ad blocked" message, |
| 1836 |
// which provides a link to the original URL if you really want it. primarily |
| 1837 |
// for IFRAMEs, which will end up containing this link instead of the ad (standard non-IFRAMEd |
| 1838 |
// ad images still get image-replaced.) |
| 1839 |
if (strstr(checkme->whatIsNaughtyCategories.c_str(), "ADs") != NULL) { |
| 1840 |
eheader = "HTTP/1.1 200 \r\n"; |
| 1841 |
eheader += o.language_list.getTranslation(1101); // advert blocked |
| 1842 |
eheader += "\r\nContent-Type: text/html\r\n"; |
| 1843 |
ebody = "<HTML><HEAD><TITLE>E2guardian - "; |
| 1844 |
ebody += o.language_list.getTranslation(1101); // advert blocked |
| 1845 |
ebody += "</TITLE></HEAD><BODY><CENTER><FONT SIZE=\"-1\"><A HREF=\""; |
| 1846 |
ebody += (*url); |
| 1847 |
ebody += "\" TARGET=\"_BLANK\">"; |
| 1848 |
ebody += o.language_list.getTranslation(1101); // advert blocked |
| 1849 |
ebody += "</A></FONT></CENTER></BODY></HTML>\r\n"; |
| 1850 |
eheader += "Content-Length: "; |
| 1851 |
eheader += std::to_string(ebody.size()); |
| 1852 |
eheader += "\r\n\r\n"; |
| 1853 |
} |
| 1854 |
|
| 1855 |
// Mod by Ernest W Lessenger Mon 2nd February 2004 |
| 1856 |
// Other bypass code mostly written by Ernest also |
| 1857 |
// create temporary bypass URL to show on denied page |
| 1858 |
else { |
| 1859 |
String hashed; |
| 1860 |
// generate valid hash locally if enabled |
| 1861 |
if (dohash) { |
| 1862 |
hashed = header->hashedURL(url, clientip, virushash, clientuser, *ldl->fg[filtergroup]); |
| 1863 |
} |
| 1864 |
// otherwise, just generate flags showing what to generate |
| 1865 |
else if (filterhash) { |
| 1866 |
hashed = "HASH=1"; |
| 1867 |
} else if (virushash) { |
| 1868 |
hashed = "HASH=2"; |
| 1869 |
} |
| 1870 |
|
| 1871 |
if(ldl->fg[filtergroup]->cgi_bypass_v2) { |
| 1872 |
if (filterhash) { |
| 1873 |
hashed += "&HASH=1"; |
| 1874 |
} else if (virushash) { |
| 1875 |
hashed += "&HASH=2"; |
| 1876 |
} |
| 1877 |
} |
| 1878 |
|
| 1879 |
if (headersent == 0) { |
| 1880 |
eheader = "HTTP/1.1 200 \r\n"; |
| 1881 |
} |
| 1882 |
if (headersent < 2) { |
| 1883 |
eheader += "Content-type: text/html\r\n\r\n"; |
| 1884 |
} |
| 1885 |
// if the header has been sent then likely displaying the |
| 1886 |
// template will break the download, however as this is |
| 1887 |
// only going to be happening if the unsafe trickle |
| 1888 |
// buffer method is used and we want the download to be |
| 1889 |
// broken we don't mind too much |
| 1890 |
//String fullurl = header->getLogUrl(true); |
| 1891 |
// |
| 1892 |
// DISPLAYING TEMPLATE |
| 1893 |
|
| 1894 |
String fullurl = checkme->get_logUrl(); |
| 1895 |
String localip = peerconn.getLocalIP(); |
| 1896 |
ldl->fg[filtergroup]->getHTMLTemplate(checkme->upfailure)->display_hb(ebody, |
| 1897 |
&fullurl, |
| 1898 |
(*checkme).whatIsNaughty, |
| 1899 |
(*checkme).whatIsNaughtyLog, |
| 1900 |
// grab either the full category list or the thresholded list |
| 1901 |
(checkme->usedisplaycats |
| 1902 |
? checkme->whatIsNaughtyDisplayCategories |
| 1903 |
: checkme->whatIsNaughtyCategories), |
| 1904 |
clientuser, clientip, |
| 1905 |
&(checkme->clienthost), |
| 1906 |
filtergroup, |
| 1907 |
ldl->fg[filtergroup]->name, |
| 1908 |
hashed, localip, flags); |
| 1909 |
} |
| 1910 |
} |
| 1911 |
} |
| 1912 |
} |
| 1913 |
// the user is using the CGI rather than the HTML template - so issue a redirect with parameters filled in on GET string |
| 1914 |
else if (reporting_level > 0) { |
| 1915 |
// grab either the full category list or the thresholded list |
| 1916 |
std::string cats; |
| 1917 |
cats = checkme->usedisplaycats ? checkme->whatIsNaughtyDisplayCategories : checkme->whatIsNaughtyCategories; |
| 1918 |
|
| 1919 |
String hashed; |
| 1920 |
// generate valid hash locally if enabled |
| 1921 |
if (dohash) { |
| 1922 |
hashed = header->hashedURL(url, clientip, virushash, clientuser, *ldl->fg[filtergroup]); |
| 1923 |
//hashed = hashedURL(url, filtergroup, clientip, virushash, clientuser); |
| 1924 |
} |
| 1925 |
// otherwise, just generate flags showing what to generate |
| 1926 |
else if (filterhash) { |
| 1927 |
hashed = "1"; |
| 1928 |
} else if (virushash) { |
| 1929 |
hashed = "2"; |
| 1930 |
} |
| 1931 |
|
| 1932 |
if ((*checkme).whatIsNaughty.length() > 2048) { |
| 1933 |
(*checkme).whatIsNaughty = String((*checkme).whatIsNaughty.c_str()).subString(0, 2048).toCharArray(); |
| 1934 |
} |
| 1935 |
if ((*checkme).whatIsNaughtyLog.length() > 2048) { |
| 1936 |
(*checkme).whatIsNaughtyLog = String((*checkme).whatIsNaughtyLog.c_str()).subString(0, |
| 1937 |
2048).toCharArray(); |
| 1938 |
} |
| 1939 |
|
| 1940 |
if ((*header).requestType().startsWith("CONNECT") && !(peerconn).isSsl()) |
| 1941 |
{ |
| 1942 |
// Block ssl website |
| 1943 |
// Buggy with FF < 65 https://bugzilla.mozilla.org/show_bug.cgi?id=1522093 |
| 1944 |
// Connections still opened after a refresh |
| 1945 |
// 403 requests made ICAP error with high load |
| 1946 |
eheader = "HTTP/1.1 302 Redirect"; |
| 1947 |
eheader += "\r\nLocation: http://internal.test.e2guardian.org"; |
| 1948 |
eheader += "\r\nServer: e2guardian"; |
| 1949 |
eheader += "\r\nConnection: close"; |
| 1950 |
eheader += "\r\n\r\n"; |
| 1951 |
// we're dealing with a non-SSL'ed request, and have the option of using the custom banned image/page directly |
| 1952 |
} else { |
| 1953 |
eheader = "HTTP/1.1 302 Redirect\r\n"; |
| 1954 |
eheader += "Location: "; |
| 1955 |
eheader += ldl->fg[filtergroup]->access_denied_address; |
| 1956 |
} |
| 1957 |
if (ldl->fg[filtergroup]->non_standard_delimiter) { |
| 1958 |
eheader += "?DENIEDURL=="; |
| 1959 |
eheader += miniURLEncode((*url).toCharArray()).c_str(); |
| 1960 |
eheader += "::IP=="; |
| 1961 |
eheader += (*clientip).c_str(); |
| 1962 |
eheader += "::USER=="; |
| 1963 |
eheader += (*clientuser).c_str(); |
| 1964 |
eheader += "::FILTERGROUP=="; |
| 1965 |
eheader += ldl->fg[filtergroup]->name; |
| 1966 |
if (checkme->clienthost != "") { |
| 1967 |
eheader += "::HOST=="; |
| 1968 |
eheader += checkme->clienthost.c_str(); |
| 1969 |
} |
| 1970 |
eheader += "::CATEGORIES=="; |
| 1971 |
eheader += miniURLEncode(cats.c_str()).c_str(); |
| 1972 |
if (virushash || filterhash) { |
| 1973 |
// output either a genuine hash, or just flags |
| 1974 |
if (dohash) { |
| 1975 |
eheader += "::"; |
| 1976 |
eheader += hashed.before("=").toCharArray(); |
| 1977 |
eheader += "=="; |
| 1978 |
eheader += hashed.after("=").toCharArray(); |
| 1979 |
} else { |
| 1980 |
eheader += "::HASH=="; |
| 1981 |
eheader += hashed.toCharArray(); |
| 1982 |
} |
| 1983 |
if(ldl->fg[filtergroup]->cgi_bypass_v2) { |
| 1984 |
String data = *clientip; |
| 1985 |
data += *clientuser; |
| 1986 |
data += ldl->fg[filtergroup]->cgi_magic; |
| 1987 |
String checkh(url->md5(data.c_str())); |
| 1988 |
eheader += "::CHECK=="; |
| 1989 |
eheader += checkh.toCharArray(); |
| 1990 |
} |
| 1991 |
} |
| 1992 |
eheader += "::EXTFLAGS=="; |
| 1993 |
eheader += flags.toCharArray(); |
| 1994 |
eheader += "::REASON=="; |
| 1995 |
} else { |
| 1996 |
eheader += "?DENIEDURL="; |
| 1997 |
eheader += miniURLEncode((*url).toCharArray()).c_str(); |
| 1998 |
eheader += "&IP="; |
| 1999 |
eheader += (*clientip).c_str(); |
| 2000 |
eheader += "&USER="; |
| 2001 |
eheader += (*clientuser).c_str(); |
| 2002 |
eheader += "&FILTERGROUP="; |
| 2003 |
eheader += ldl->fg[filtergroup]->name; |
| 2004 |
if (checkme->clienthost != "") { |
| 2005 |
eheader += "&HOST="; |
| 2006 |
eheader += checkme->clienthost.c_str(); |
| 2007 |
} |
| 2008 |
eheader += "&CATEGORIES="; |
| 2009 |
eheader += miniURLEncode(cats.c_str()).c_str(); |
| 2010 |
if (virushash || filterhash) { |
| 2011 |
// output either a genuine hash, or just flags |
| 2012 |
if (dohash) { |
| 2013 |
eheader += "&"; |
| 2014 |
eheader += hashed.toCharArray(); |
| 2015 |
} else { |
| 2016 |
eheader += "&HASH="; |
| 2017 |
eheader += hashed.toCharArray(); |
| 2018 |
} |
| 2019 |
if(ldl->fg[filtergroup]->cgi_bypass_v2) { |
| 2020 |
String data = *clientip; |
| 2021 |
data += *clientuser; |
| 2022 |
data += ldl->fg[filtergroup]->cgi_magic; |
| 2023 |
String checkh(url->md5(data.c_str())); |
| 2024 |
eheader += "&CHECK="; |
| 2025 |
eheader += checkh.toCharArray(); |
| 2026 |
} |
| 2027 |
} |
| 2028 |
eheader += "&EXTFLAGS="; |
| 2029 |
eheader += flags.toCharArray(); |
| 2030 |
eheader += "&REASON="; |
| 2031 |
} |
| 2032 |
if (reporting_level == 1) { |
| 2033 |
eheader += miniURLEncode((*checkme).whatIsNaughty.c_str()).c_str(); |
| 2034 |
} else { |
| 2035 |
eheader += miniURLEncode((*checkme).whatIsNaughtyLog.c_str()).c_str(); |
| 2036 |
} |
| 2037 |
eheader += "\r\n\r\n"; |
| 2038 |
} |
| 2039 |
|
| 2040 |
// the user is using the barebones banned page |
| 2041 |
else if (reporting_level == 0) { |
| 2042 |
eheader = "HTTP/1.1 200 OK\r\n"; |
| 2043 |
eheader += "Content-type: text/html\r\n"; |
| 2044 |
ebody = "<HTML><HEAD><TITLE>e2guardian - "; |
| 2045 |
ebody += o.language_list.getTranslation(1); // access denied |
| 2046 |
ebody += "</TITLE></HEAD><BODY><CENTER><H1>e2guardian - "; |
| 2047 |
ebody += o.language_list.getTranslation(1); // access denied |
| 2048 |
ebody += "</H1></CENTER></BODY></HTML>\r\n"; |
| 2049 |
eheader += "Content-Length: "; |
| 2050 |
eheader += std::to_string(ebody.size()); |
| 2051 |
eheader += "\r\n\r\n"; |
| 2052 |
} |
| 2053 |
|
| 2054 |
// stealth mode |
| 2055 |
else if (reporting_level == -1) { |
| 2056 |
(*checkme).isItNaughty = false; // dont block |
| 2057 |
return false; |
| 2058 |
} |
| 2059 |
} catch (std::exception &e) { |
| 2060 |
} |
| 2061 |
return true; |
| 2062 |
} // end of deny request loop |
| 2063 |
|
| 2064 |
// show the relevant banned page/image/CGI based on report level setting, request type etc. |
| 2065 |
bool ConnectionHandler::denyAccess(Socket *peerconn, Socket *proxysock, HTTPHeader *header, HTTPHeader *docheader, |
| 2066 |
String *url, NaughtyFilter *checkme, std::string *clientuser, std::string *clientip, |
| 2067 |
int filtergroup, |
| 2068 |
bool ispostblock, int headersent, bool wasinfected, bool scanerror, bool forceshow) { |
| 2069 |
String eheader, ebody; |
| 2070 |
if (genDenyAccess(*peerconn, eheader, ebody, header, docheader, &(checkme->logurl), checkme, clientuser, clientip, filtergroup, |
| 2071 |
ispostblock, headersent, wasinfected, scanerror, forceshow)) { |
| 2072 |
peerconn->writeString(eheader.toCharArray()); |
| 2073 |
if (ebody.length() > 0) |
| 2074 |
peerconn->writeString(ebody.toCharArray()); |
| 2075 |
}; |
| 2076 |
|
| 2077 |
// we blocked the request, so flush the client connection & close the proxy connection. |
| 2078 |
if ((*checkme).isItNaughty) { |
| 2079 |
(*peerconn).readyForOutput(o.net.proxy_timeout); //as best a flush as I can |
| 2080 |
(*proxysock).close(); // close connection to proxy |
| 2081 |
// we said no to the request, so return true, indicating exit the connhandler |
| 2082 |
return true; |
| 2083 |
} |
| 2084 |
ldl.reset(); |
| 2085 |
return false; |
| 2086 |
} // end of deny request loop |
| 2087 |
|
| 2088 |
// do content scanning (AV filtering) and naughty filtering |
| 2089 |
void ConnectionHandler::contentFilter(HTTPHeader *docheader, HTTPHeader *header, DataBuffer *docbody, |
| 2090 |
Socket *proxysock, Socket *peerconn, int *headersent, bool *pausedtoobig, |
| 2091 |
off_t *docsize, NaughtyFilter *checkme, |
| 2092 |
bool wasclean, int filtergroup, std::deque<CSPlugin *> &responsescanners, |
| 2093 |
std::string *clientuser, std::string *clientip, bool *wasinfected, |
| 2094 |
bool *wasscanned, bool isbypass, |
| 2095 |
String &url, String &domain, bool *scanerror, bool &contentmodified, |
| 2096 |
String *csmessage) { |
| 2097 |
|
| 2098 |
|
| 2099 |
//proxysock->bcheckForInput(120000); |
| 2100 |
bool compressed = docheader->isCompressed(); |
| 2101 |
if (compressed) { |
| 2102 |
DEBUG_debug(" -Decompressing as we go....."); |
| 2103 |
docbody->setDecompress(docheader->contentEncoding()); |
| 2104 |
} |
| 2105 |
DEBUG_debug(docheader->contentEncoding()); |
| 2106 |
DEBUG_debug(" -about to get body from proxy"); |
| 2107 |
(*pausedtoobig) = docbody->in(proxysock, peerconn, header, docheader, !responsescanners.empty(), |
| 2108 |
headersent, ldl->fg[filtergroup]->StoryB, checkme ); // get body from proxy |
| 2109 |
// checkme: surely if pausedtoobig is true, we just want to break here? |
| 2110 |
// the content is larger than max_content_filecache_scan_size if it was downloaded for scanning, |
| 2111 |
// and larger than max_content_filter_size if not. |
| 2112 |
// in fact, why don't we check the content length (when it's not -1) before even triggering the download managers? |
| 2113 |
if ((*pausedtoobig)) { |
| 2114 |
DEBUG_debug(" -got PARTIAL body "); |
| 2115 |
} else { |
| 2116 |
DEBUG_debug(" -got body"); |
| 2117 |
} |
| 2118 |
|
| 2119 |
off_t dblen; |
| 2120 |
bool isfile = false; |
| 2121 |
if (docbody->tempfilesize > 0) { |
| 2122 |
dblen = docbody->tempfilesize; |
| 2123 |
isfile = true; |
| 2124 |
} else { |
| 2125 |
dblen = docbody->data_length; |
| 2126 |
} |
| 2127 |
// don't scan zero-length buffers (waste of AV resources, especially with external scanners (ICAP)). |
| 2128 |
// these were encountered browsing opengroup.org, caused by a stats script. (PRA 21/09/2005) |
| 2129 |
// if we wanted to honour a hypothetical min_content_scan_size, we'd do it here. |
| 2130 |
if (((*docsize) = dblen) == 0) { |
| 2131 |
DEBUG_debug(" -Not scanning zero-length body"); |
| 2132 |
// it's not inconceivable that we received zlib or gzip encoded content |
| 2133 |
// that is, after decompression, zero length. we need to cater for this. |
| 2134 |
// seen on SW's internal MediaWiki. |
| 2135 |
docbody->swapbacktocompressed(); |
| 2136 |
return; |
| 2137 |
} |
| 2138 |
|
| 2139 |
if (!wasclean) { // was not clean or no urlcache |
| 2140 |
|
| 2141 |
// fixed to obey maxcontentramcachescansize |
| 2142 |
if (!responsescanners.empty() && |
| 2143 |
(isfile ? dblen <= o.content.max_content_filecache_scan_size : dblen <= o.content.max_content_ramcache_scan_size)) { |
| 2144 |
int csrc = 0; |
| 2145 |
int k = 0; |
| 2146 |
|
| 2147 |
for (std::deque<CSPlugin *>::iterator i = responsescanners.begin(); i != responsescanners.end(); i++) { |
| 2148 |
(*wasscanned) = true; |
| 2149 |
if (isfile) { |
| 2150 |
DEBUG_debug(" -Running scanFile"); |
| 2151 |
csrc = (*i)->scanFile(header, docheader, clientuser->c_str(), ldl->fg[filtergroup], |
| 2152 |
clientip->c_str(), docbody->tempfilepath.toCharArray(), checkme); |
| 2153 |
if ((csrc != E2CS_CLEAN) && (csrc != E2CS_WARNING)) { |
| 2154 |
unlink(docbody->tempfilepath.toCharArray()); |
| 2155 |
// delete infected (or unscanned due to error) file straight away |
| 2156 |
} |
| 2157 |
} else { |
| 2158 |
DEBUG_debug(" -Running scanMemory"); |
| 2159 |
csrc = (*i)->scanMemory(header, docheader, clientuser->c_str(), ldl->fg[filtergroup], |
| 2160 |
clientip->c_str(), docbody->data, docbody->data_length, checkme); |
| 2161 |
} |
| 2162 |
DEBUG_debug(" -AV scan ", k, " returned: ", csrc); |
| 2163 |
if (csrc == E2CS_WARNING) { |
| 2164 |
// Scanner returned a warning. File wasn't infected, but wasn't scanned properly, either. |
| 2165 |
(*wasscanned) = false; |
| 2166 |
(*scanerror) = false; |
| 2167 |
DEBUG_debug((*i)->getLastMessage()); |
| 2168 |
(*csmessage) = (*i)->getLastMessage(); |
| 2169 |
} else if (csrc == E2CS_BLOCKED) { |
| 2170 |
(*wasscanned) = true; |
| 2171 |
(*scanerror) = false; |
| 2172 |
break; |
| 2173 |
} else if (csrc == E2CS_INFECTED) { |
| 2174 |
(*wasinfected) = true; |
| 2175 |
(*scanerror) = false; |
| 2176 |
break; |
| 2177 |
} |
| 2178 |
//if its not clean / we error then treat it as infected |
| 2179 |
else if (csrc != E2CS_CLEAN) { |
| 2180 |
if (csrc < 0) { |
| 2181 |
E2LOGGER_error("Unknown return code from content scanner: ", csrc); |
| 2182 |
if (ldl->fg[filtergroup]->disable_content_scan_error) { |
| 2183 |
E2LOGGER_error( "disablecontentscanerror is on : bypass actived USER: ", clientip, " URL: ", url); |
| 2184 |
(*wasscanned) = false; |
| 2185 |
(*wasinfected) = false; |
| 2186 |
break; |
| 2187 |
} |
| 2188 |
} else { |
| 2189 |
E2LOGGER_error("scanFile/Memory returned error: ", csrc); |
| 2190 |
} |
| 2191 |
checkme->message_no = 1203; |
| 2192 |
checkme->whatIsNaughty = o.language_list.getTranslation(1203); |
| 2193 |
checkme->whatIsNaughtyLog = (*i)->getLastMessage().toCharArray(); |
| 2194 |
checkme->whatIsNaughtyCategories = o.language_list.getTranslation(72); |
| 2195 |
checkme->isItNaughty = true; |
| 2196 |
checkme->isException = false; |
| 2197 |
(*scanerror) = true; |
| 2198 |
break; |
| 2199 |
} |
| 2200 |
k++; |
| 2201 |
} |
| 2202 |
|
| 2203 |
DEBUG_debug(" -finished running AV"); |
| 2204 |
// rc = system("date"); |
| 2205 |
} |
| 2206 |
else if (!responsescanners.empty()) { |
| 2207 |
DEBUG_debug(" -content length large so skipping content scanning (virus) filtering"); |
| 2208 |
} |
| 2209 |
// rc = system("date"); |
| 2210 |
if (!checkme->isItNaughty && !checkme->isException && !isbypass && (dblen <= o.content.max_content_filter_size) |
| 2211 |
&& !docheader->authRequired() && (docheader->OKtoFilterMime(ldl->fg[filtergroup]))) { |
| 2212 |
DEBUG_debug(" -Start content filtering: "); |
| 2213 |
// checkme->checkme(docbody->data, docbody->buffer_length, &url, &domain, |
| 2214 |
// ldl->fg[filtergroup], ldl->fg[filtergroup]->banned_phrase_list, |
| 2215 |
// ldl->fg[filtergroup]->naughtyness_limit); |
| 2216 |
checkme->checkme(docbody->data, docbody->data_length, &url, &domain, |
| 2217 |
ldl->fg[filtergroup], ldl->fg[filtergroup]->banned_phrase_list, |
| 2218 |
ldl->fg[filtergroup]->naughtyness_limit); |
| 2219 |
DEBUG_debug(" -Done content filtering"); |
| 2220 |
} |
| 2221 |
|
| 2222 |
else { |
| 2223 |
DEBUG_debug(" -Skipping content filtering: "); |
| 2224 |
if (dblen > o.content.max_content_filter_size) { |
| 2225 |
DEBUG_debug(" -Content too large"); |
| 2226 |
} else if (checkme->isException) { |
| 2227 |
DEBUG_debug(" -Is flagged as an exception"); |
| 2228 |
} else if (checkme->isItNaughty) { |
| 2229 |
DEBUG_debug(" -Is already flagged as naughty (content scanning)"); |
| 2230 |
} else if (isbypass) { |
| 2231 |
DEBUG_debug(" -Is flagged as a bypass"); |
| 2232 |
} else if (docheader->authRequired()) { |
| 2233 |
DEBUG_debug(" -Is a set of auth required headers"); |
| 2234 |
} else if (!docheader->isContentType("text",ldl->fg[filtergroup])) { |
| 2235 |
DEBUG_debug(" -Not text"); |
| 2236 |
} |
| 2237 |
} |
| 2238 |
} |
| 2239 |
|
| 2240 |
// don't do phrase filtering or content replacement on exception/bypass accesses |
| 2241 |
if (checkme->isException || isbypass) { |
| 2242 |
// don't forget to swap back to compressed! |
| 2243 |
docbody->swapbacktocompressed(); |
| 2244 |
return; |
| 2245 |
} |
| 2246 |
|
| 2247 |
if ((dblen <= o.content.max_content_filter_size) && !checkme->isItNaughty && |
| 2248 |
docheader->isContentType("text", ldl->fg[filtergroup])) { |
| 2249 |
contentmodified = docbody->contentRegExp(ldl->fg[filtergroup]); |
| 2250 |
// content modifying uses global variable |
| 2251 |
} |
| 2252 |
|
| 2253 |
else { |
| 2254 |
DEBUG_debug(" -Skipping content modification: "); |
| 2255 |
if (dblen > o.content.max_content_filter_size) { |
| 2256 |
DEBUG_debug(" -Content too large"); |
| 2257 |
} else if (!docheader->isContentType("text",ldl->fg[filtergroup])) { |
| 2258 |
DEBUG_debug(" -Not text"); |
| 2259 |
} else if (checkme->isItNaughty) { |
| 2260 |
DEBUG_debug(" -Already flagged as naughty"); |
| 2261 |
} |
| 2262 |
} |
| 2263 |
//rc = system("date"); |
| 2264 |
|
| 2265 |
if (contentmodified) { // this would not include infected/cured files |
| 2266 |
// if the content was modified then it must have fit in ram so no |
| 2267 |
// need to worry about swapped to disk stuff |
| 2268 |
DEBUG_debug(" -content modification made"); |
| 2269 |
if (compressed) { |
| 2270 |
docheader->removeEncoding(docbody->data_length); |
| 2271 |
// need to modify header to mark as not compressed |
| 2272 |
// it also modifies Content-Length as well |
| 2273 |
} else { |
| 2274 |
docheader->setContentLength(docbody->data_length); |
| 2275 |
} |
| 2276 |
} else { |
| 2277 |
docbody->swapbacktocompressed(); |
| 2278 |
// if we've not modified it might as well go back to |
| 2279 |
// the original compressed version (if there) and send |
| 2280 |
// that to the browser |
| 2281 |
} |
| 2282 |
DEBUG_debug(" Returning from content checking"); |
| 2283 |
} |
| 2284 |
|
| 2285 |
|
| 2286 |
int ConnectionHandler::sendProxyConnect(String &hostname, Socket *sock, NaughtyFilter *checkme) { |
| 2287 |
String connect_request = "CONNECT " + hostname + ":"; |
| 2288 |
connect_request += "443 HTTP/1.1\r\n\r\n"; |
| 2289 |
|
| 2290 |
DEBUG_debug(" -creating tunnel through proxy to ", hostname); |
| 2291 |
|
| 2292 |
//somewhere to hold the header from the proxy |
| 2293 |
HTTPHeader header(__HEADER_RESPONSE); |
| 2294 |
//header.setTimeout(o.pcon_timeout); |
| 2295 |
header.setTimeout(o.net.proxy_timeout); |
| 2296 |
|
| 2297 |
if (!(sock->writeString(connect_request.c_str()) && header.in(sock, true))) { |
| 2298 |
|
| 2299 |
DEBUG_debug(" -Error creating tunnel through proxy", strerror(errno) ); |
| 2300 |
//(*checkme).whatIsNaughty = "Unable to create tunnel through local proxy"; |
| 2301 |
checkme->message_no = 157; |
| 2302 |
(*checkme).whatIsNaughty = o.language_list.getTranslation(157); |
| 2303 |
(*checkme).whatIsNaughtyLog = (*checkme).whatIsNaughty; |
| 2304 |
(*checkme).isItNaughty = true; |
| 2305 |
(*checkme).whatIsNaughtyCategories = o.language_list.getTranslation(70); |
| 2306 |
|
| 2307 |
return -1; |
| 2308 |
} |
| 2309 |
//do http connect |
| 2310 |
if (header.returnCode() != 200) { |
| 2311 |
//connection failed |
| 2312 |
//(*checkme).whatIsNaughty = "Opening tunnel failed"; |
| 2313 |
checkme->message_no = 158; |
| 2314 |
(*checkme).whatIsNaughty = o.language_list.getTranslation(158); |
| 2315 |
(*checkme).whatIsNaughtyLog = (*checkme).whatIsNaughty + " with error code " + String(header.returnCode()); |
| 2316 |
(*checkme).isItNaughty = true; |
| 2317 |
(*checkme).whatIsNaughtyCategories = o.language_list.getTranslation(70); |
| 2318 |
|
| 2319 |
DEBUG_debug(" -Tunnel status was ", header.returnCode(), " expecting 200 ok"); |
| 2320 |
|
| 2321 |
return -1; |
| 2322 |
} |
| 2323 |
|
| 2324 |
return 0; |
| 2325 |
} |
| 2326 |
|
| 2327 |
void ConnectionHandler::checkCertificate(String &hostname, Socket *sslsock, NaughtyFilter *checkme) |
| 2328 |
{ |
| 2329 |
DEBUG_debug(" -checking SSL certificate is valid"); |
| 2330 |
|
| 2331 |
long rc = sslsock->checkCertValid(hostname); |
| 2332 |
//check that everything in this certificate is correct appart from the hostname |
| 2333 |
if (rc < 0) { |
| 2334 |
//no certificate |
| 2335 |
if ( ldl->fg[filtergroup]->allow_empty_host_certs) |
| 2336 |
return; |
| 2337 |
checkme->isItNaughty = true; |
| 2338 |
//(*checkme).whatIsNaughty = "No SSL certificate supplied by server"; |
| 2339 |
checkme->message_no = 155; |
| 2340 |
(*checkme).whatIsNaughty = o.language_list.getTranslation(155); |
| 2341 |
(*checkme).whatIsNaughtyLog = (*checkme).whatIsNaughty; |
| 2342 |
(*checkme).whatIsNaughtyCategories = o.language_list.getTranslation(70); |
| 2343 |
return; |
| 2344 |
} else if (rc != X509_V_OK) { |
| 2345 |
//something was wrong in the certificate |
| 2346 |
checkme->isItNaughty = true; |
| 2347 |
// (*checkme).whatIsNaughty = "Certificate supplied by server was not valid"; |
| 2348 |
checkme->message_no = 150; |
| 2349 |
(*checkme).whatIsNaughty = o.language_list.getTranslation(150); |
| 2350 |
(*checkme).whatIsNaughtyLog = (*checkme).whatIsNaughty + ": " + X509_verify_cert_error_string(rc); |
| 2351 |
(*checkme).whatIsNaughtyCategories = o.language_list.getTranslation(70); |
| 2352 |
return; |
| 2353 |
} |
| 2354 |
|
| 2355 |
DEBUG_debug(" -checking SSL certificate hostname"); |
| 2356 |
|
| 2357 |
//check the common name and altnames of a certificate against hostname |
| 2358 |
if (sslsock->checkCertHostname(hostname) < 0) { |
| 2359 |
//hostname was not matched by the certificate |
| 2360 |
checkme->isItNaughty = true; |
| 2361 |
//(*checkme).whatIsNaughty = "Server's SSL certificate does not match domain name"; |
| 2362 |
checkme->message_no = 156; |
| 2363 |
(*checkme).whatIsNaughty = o.language_list.getTranslation(156); |
| 2364 |
(*checkme).whatIsNaughtyLog = (*checkme).whatIsNaughty; |
| 2365 |
(*checkme).whatIsNaughtyCategories = o.language_list.getTranslation(70); |
| 2366 |
return; |
| 2367 |
} |
| 2368 |
} |
| 2369 |
|
| 2370 |
|
| 2371 |
bool ConnectionHandler::getdnstxt(std::string &clientip, String &user) { |
| 2372 |
|
| 2373 |
String ippath; |
| 2374 |
|
| 2375 |
ippath = clientip; |
| 2376 |
// if (o.use_xforwardedfor) { |
| 2377 |
// // grab the X-Forwarded-For IP if available |
| 2378 |
// p2 = header.getXForwardedForIP(); |
| 2379 |
// if (p2.length() > 0) { |
| 2380 |
// ippath = p1 + "-" + p2; |
| 2381 |
// } else { |
| 2382 |
// ippath = p1; |
| 2383 |
// } |
| 2384 |
// } else { |
| 2385 |
// ippath = p1; |
| 2386 |
// } |
| 2387 |
|
| 2388 |
DEBUG_debug("IPPath is ", ippath); |
| 2389 |
|
| 2390 |
// change '.' to '-' |
| 2391 |
ippath.swapChar('.', '-'); |
| 2392 |
DEBUG_debug("IPPath is ", ippath); |
| 2393 |
#ifdef PRT_DNSAUTH |
| 2394 |
// get info from DNS |
| 2395 |
union { |
| 2396 |
HEADER hdr; |
| 2397 |
u_char buf[NS_PACKETSZ]; |
| 2398 |
} response; |
| 2399 |
int responseLen; |
| 2400 |
ns_msg handle; /* handle for response message */ |
| 2401 |
responseLen = res_querydomain(ippath.c_str(), o.log.dns_user_logging_domain.c_str(), ns_c_in, ns_t_txt, (u_char *)&response, sizeof(response)); |
| 2402 |
if (responseLen < 0) { |
| 2403 |
DEBUG_debug("DNS query returned error ", dns_error(h_errno)); |
| 2404 |
return false; |
| 2405 |
} |
| 2406 |
if (ns_initparse(response.buf, responseLen, &handle) < 0) { |
| 2407 |
DEBUG_debug("ns_initparse returned error ", strerror(errno)); |
| 2408 |
return false; |
| 2409 |
} |
| 2410 |
//int rrnum; /* resource record number */ |
| 2411 |
ns_rr rr; /* expanded resource record */ |
| 2412 |
//u_char *cp; |
| 2413 |
//char ans[MAXDNAME]; |
| 2414 |
|
| 2415 |
int i = ns_msg_count(handle, ns_s_an); |
| 2416 |
if (i > 0) { |
| 2417 |
if (ns_parserr(&handle, ns_s_an, 0, &rr)) { |
| 2418 |
DEBUG_debug("ns_paserr returned error ", strerror(errno)); |
| 2419 |
return false; |
| 2420 |
} else { |
| 2421 |
if (ns_rr_type(rr) == ns_t_txt) { |
| 2422 |
DEBUG_debug("ns_rr_rdlen returned ", ns_rr_rdlen(rr)); |
| 2423 |
u_char *k = (u_char *)ns_rr_rdata(rr); |
| 2424 |
char p[400]; |
| 2425 |
unsigned int j = 0; |
| 2426 |
for (unsigned int j1 = 1; j1 < ns_rr_rdlen(rr); j1++) { |
| 2427 |
p[j++] = k[j1]; |
| 2428 |
} |
| 2429 |
// p[j] = (char)NULL; |
| 2430 |
p[j] = '\0'; |
| 2431 |
DEBUG_debug("ns_rr_data returned ", p ); |
| 2432 |
String dnstxt(p); |
| 2433 |
user = dnstxt.before(","); |
| 2434 |
return true; |
| 2435 |
} |
| 2436 |
} |
| 2437 |
} |
| 2438 |
#endif |
| 2439 |
return false; |
| 2440 |
} |
| 2441 |
|
| 2442 |
String ConnectionHandler::dns_error(int herror) { |
| 2443 |
|
| 2444 |
String s; |
| 2445 |
|
| 2446 |
switch (herror) { |
| 2447 |
case HOST_NOT_FOUND: |
| 2448 |
s = "HOST_NOT_FOUND"; |
| 2449 |
break; |
| 2450 |
case TRY_AGAIN: |
| 2451 |
s = "TRY_AGAIN - DNS server failure"; |
| 2452 |
break; |
| 2453 |
case NO_DATA: |
| 2454 |
s = "NO_DATA - unexpected DNS error"; |
| 2455 |
break; |
| 2456 |
default: |
| 2457 |
String S2(herror); |
| 2458 |
s = "DNS - Unexpected error number " + S2; |
| 2459 |
break; |
| 2460 |
} |
| 2461 |
return s; |
| 2462 |
} |
| 2463 |
|
| 2464 |
bool |
| 2465 |
ConnectionHandler::gen_error_mess(Socket &peerconn, NaughtyFilter &cm, String &eheader, String &ebody, int mess_no1, |
| 2466 |
int mess_no2, std::string mess) { |
| 2467 |
cm.message_no = mess_no1; |
| 2468 |
eheader = "HTTP/1.1 " + mess + "\nContent-Type: text/html\r\nConnection: Close\r\n"; |
| 2469 |
if(mess_no1 > 0) { |
| 2470 |
ebody = "<HTML><HEAD><TITLE>e2guardian - "; |
| 2471 |
ebody += mess; |
| 2472 |
ebody += "</TITLE></HEAD><BODY><H1>e2guardian - "; |
| 2473 |
ebody += mess; |
| 2474 |
ebody += "</H1>"; |
| 2475 |
if (mess_no1 > 0) |
| 2476 |
ebody += o.language_list.getTranslation(mess_no1); |
| 2477 |
if (mess_no2 > 0) |
| 2478 |
ebody += o.language_list.getTranslation(mess_no2); |
| 2479 |
ebody += "</BODY></HTML>\r\n"; |
| 2480 |
} |
| 2481 |
return true; |
| 2482 |
} |
| 2483 |
|
| 2484 |
bool |
| 2485 |
ConnectionHandler::writeback_error(NaughtyFilter &cm, Socket &cl_sock, int mess_no1, int mess_no2, std::string mess) { |
| 2486 |
String eheader, ebody; |
| 2487 |
gen_error_mess(cl_sock, cm, eheader, ebody, mess_no1, mess_no2, mess); |
| 2488 |
cl_sock.writeString(eheader.c_str()); |
| 2489 |
cl_sock.writeString("\r\n"); |
| 2490 |
cl_sock.writeString(ebody.c_str()); |
| 2491 |
return true; |
| 2492 |
} |
| 2493 |
|
| 2494 |
bool |
| 2495 |
ConnectionHandler::goMITM(NaughtyFilter &checkme, Socket &proxysock, Socket &peerconn, bool &persistProxy, bool &authed, |
| 2496 |
bool &persistent_authed, String &ip, stat_rec *&dystat, std::string &clientip, |
| 2497 |
bool transparent) { |
| 2498 |
|
| 2499 |
DEBUG_debug(" Start goMITM nf ", checkme.isItNaughty, " upfail ", checkme.upfailure); |
| 2500 |
|
| 2501 |
|
| 2502 |
DEBUG_debug(" -Intercepting HTTPS connection"); |
| 2503 |
HTTPHeader *header = checkme.request_header; |
| 2504 |
HTTPHeader *docheader = checkme.response_header; |
| 2505 |
bool justLog = false; |
| 2506 |
|
| 2507 |
// CA intialisation now Moved into OptionContainer so now done once on start-up |
| 2508 |
// instead of on every request |
| 2509 |
|
| 2510 |
X509 *cert = NULL; |
| 2511 |
struct ca_serial caser; |
| 2512 |
caser.asn = NULL; |
| 2513 |
caser.charhex = NULL; |
| 2514 |
caser.filepath = NULL; |
| 2515 |
caser.filename = NULL; |
| 2516 |
|
| 2517 |
EVP_PKEY *pkey = NULL; |
| 2518 |
bool certfromcache = false; |
| 2519 |
//generate the cert |
| 2520 |
DEBUG_debug(" -Getting ssl certificate for client connection"); |
| 2521 |
|
| 2522 |
pkey = o.cert.ca->getServerPkey(); |
| 2523 |
|
| 2524 |
//generate the certificate but dont write it to disk (avoid someone |
| 2525 |
//requesting lots of places that dont exist causing the disk to fill |
| 2526 |
//up / run out of inodes |
| 2527 |
certfromcache = o.cert.ca->getServerCertificate(checkme.urldomain.CN().c_str(), &cert, |
| 2528 |
&caser, checkme.isiphost); |
| 2529 |
if (caser.asn == NULL) { |
| 2530 |
DEBUG_debug("caser.asn is NULL"); } |
| 2531 |
// std::cerr << "serials are: " << (char) *caser.asn << " " < caser.charhex << std::endl; |
| 2532 |
|
| 2533 |
//check that the generated cert is not null and fillin checkme if it is |
| 2534 |
if (cert == NULL) { |
| 2535 |
checkme.isItNaughty = true; |
| 2536 |
//checkme.whatIsNaughty = "Failed to get ssl certificate"; |
| 2537 |
checkme.message_no = 151; |
| 2538 |
checkme.whatIsNaughty = o.language_list.getTranslation(151); |
| 2539 |
checkme.whatIsNaughtyLog = checkme.whatIsNaughty; |
| 2540 |
checkme.whatIsNaughtyCategories = o.language_list.getTranslation(70); |
| 2541 |
justLog = true; |
| 2542 |
} else if (pkey == NULL) { |
| 2543 |
checkme.isItNaughty = true; |
| 2544 |
//checkme.whatIsNaughty = "Failed to load ssl private key"; |
| 2545 |
checkme.message_no = 153; |
| 2546 |
checkme.whatIsNaughty = o.language_list.getTranslation(153); |
| 2547 |
checkme.whatIsNaughtyLog = checkme.whatIsNaughty; |
| 2548 |
checkme.whatIsNaughtyCategories = o.language_list.getTranslation(70); |
| 2549 |
justLog = true; |
| 2550 |
X509_free(cert); |
| 2551 |
cert = NULL; |
| 2552 |
} |
| 2553 |
|
| 2554 |
//startsslserver on the connection to the client |
| 2555 |
//if (!checkme.isItNaughty) |
| 2556 |
if (true) |
| 2557 |
{ |
| 2558 |
DEBUG_debug(" -Going SSL on the peer connection"); |
| 2559 |
|
| 2560 |
if (!transparent) { |
| 2561 |
//send a 200 to the client no matter what because they managed to get a connection to us |
| 2562 |
//and we can use it for a blockpage if nothing else |
| 2563 |
std::string msg = "HTTP/1.1 200 Connection established\r\n\r\n"; |
| 2564 |
if (!peerconn.writeString(msg.c_str())) |
| 2565 |
{ |
| 2566 |
peerDiag("Unable to send 200 connection established to client ", peerconn); |
| 2567 |
if(cert != NULL) { |
| 2568 |
X509_free(cert); |
| 2569 |
cert = NULL; |
| 2570 |
} |
| 2571 |
return false; |
| 2572 |
} |
| 2573 |
} |
| 2574 |
|
| 2575 |
if (peerconn.startSslServer(cert, pkey, o.cert.set_cipher_list) < 0) { |
| 2576 |
//make sure the ssl stuff is shutdown properly so we display the old ssl blockpage |
| 2577 |
peerconn.stopSsl(); |
| 2578 |
|
| 2579 |
checkme.isItNaughty = true; |
| 2580 |
//checkme.whatIsNaughty = "Failed to negotiate ssl connection to client"; |
| 2581 |
checkme.message_no = 154; |
| 2582 |
checkme.whatIsNaughty = o.language_list.getTranslation(154); |
| 2583 |
checkme.whatIsNaughtyLog = checkme.whatIsNaughty; |
| 2584 |
checkme.whatIsNaughtyCategories = o.language_list.getTranslation(70); |
| 2585 |
justLog = true; |
| 2586 |
if(cert != NULL) { |
| 2587 |
X509_free(cert); |
| 2588 |
cert = NULL; |
| 2589 |
} |
| 2590 |
|
| 2591 |
} |
| 2592 |
} |
| 2593 |
|
| 2594 |
if (proxysock.isOpen()) { |
| 2595 |
// tsslclient connected to the proxy and check the certificate of the server |
| 2596 |
DEBUG_debug(" nf ", checkme.isItNaughty, " upfail ", checkme.upfailure); |
| 2597 |
|
| 2598 |
if (!checkme.isItNaughty) { |
| 2599 |
DEBUG_debug(" -Going SSL on upstream connection "); |
| 2600 |
std::string certpath = std::string(o.cert.ssl_certificate_path); |
| 2601 |
if (proxysock.startSslClient(certpath, checkme.urldomain)) { |
| 2602 |
checkme.isItNaughty = true; |
| 2603 |
//checkme.whatIsNaughty = "Failed to negotiate ssl connection to server"; |
| 2604 |
checkme.message_no = 160; |
| 2605 |
checkme.whatIsNaughty = o.language_list.getTranslation(160); |
| 2606 |
checkme.whatIsNaughtyLog = checkme.whatIsNaughty; |
| 2607 |
checkme.whatIsNaughtyCategories = o.language_list.getTranslation(70); |
| 2608 |
} |
| 2609 |
} |
| 2610 |
DEBUG_debug(" nf ", checkme.isItNaughty, " upfail ", checkme.upfailure); |
| 2611 |
|
| 2612 |
if (!checkme.isItNaughty) { |
| 2613 |
DEBUG_debug(" -Checking certificate"); |
| 2614 |
//will fill in checkme of its own accord |
| 2615 |
if (!checkme.nocheckcert) { |
| 2616 |
checkCertificate(checkme.urldomain, &proxysock, &checkme); |
| 2617 |
checkme.badcert = checkme.isItNaughty; |
| 2618 |
} |
| 2619 |
} |
| 2620 |
} |
| 2621 |
|
| 2622 |
DEBUG_debug(" nf ", checkme.isItNaughty, " upfail ", checkme.upfailure); |
| 2623 |
if ((!checkme.isItNaughty) && (!checkme.upfailure)) { |
| 2624 |
bool writecert = true; |
| 2625 |
if (!certfromcache) { |
| 2626 |
writecert = o.cert.ca->writeCertificate(checkme.urldomain.c_str(), cert, |
| 2627 |
&caser); |
| 2628 |
} |
| 2629 |
|
| 2630 |
//if we cant write the certificate its not the end of the world but it is slow |
| 2631 |
if (!writecert) { |
| 2632 |
E2LOGGER_error("Couldn't save certificate to on disk cache"); |
| 2633 |
} |
| 2634 |
DEBUG_debug(" -Handling connections inside ssl tunnel"); |
| 2635 |
|
| 2636 |
if (authed) { |
| 2637 |
persistent_authed = true; |
| 2638 |
} |
| 2639 |
|
| 2640 |
//handleConnection inside the ssl tunnel |
| 2641 |
handleConnection(peerconn, ip, true, proxysock, dystat); |
| 2642 |
DEBUG_debug(" -Handling connections inside ssl tunnel: done"); |
| 2643 |
} |
| 2644 |
o.cert.ca->free_ca_serial(&caser); |
| 2645 |
|
| 2646 |
//stopssl on the proxy connection |
| 2647 |
//if it was marked as naughty then show a deny page and close the connection |
| 2648 |
if (checkme.isItNaughty || checkme.upfailure) { |
| 2649 |
DEBUG_debug(" -SSL Interception failed ", checkme.whatIsNaughty, " nf ", checkme.isItNaughty, " upfail ", checkme.upfailure); |
| 2650 |
|
| 2651 |
doLog(clientuser, clientip, checkme); |
| 2652 |
|
| 2653 |
if(!justLog) |
| 2654 |
denyAccess(&peerconn, &proxysock, header, docheader, &checkme.logurl, &checkme, &clientuser, |
| 2655 |
&clientip, filtergroup, checkme.ispostblock, checkme.headersent, checkme.wasinfected, |
| 2656 |
checkme.scanerror, checkme.badcert); |
| 2657 |
} |
| 2658 |
DEBUG_debug(" -Shutting down ssl to proxy"); |
| 2659 |
proxysock.stopSsl(); |
| 2660 |
|
| 2661 |
DEBUG_debug(" -Shutting down ssl to client"); |
| 2662 |
|
| 2663 |
peerconn.stopSsl(); |
| 2664 |
|
| 2665 |
//tidy up key and cert |
| 2666 |
if(cert != NULL) { |
| 2667 |
X509_free(cert); |
| 2668 |
cert = NULL; |
| 2669 |
} |
| 2670 |
if(pkey != NULL) { |
| 2671 |
EVP_PKEY_free(pkey); |
| 2672 |
pkey = NULL; |
| 2673 |
} |
| 2674 |
|
| 2675 |
persistProxy = false; |
| 2676 |
proxysock.close(); |
| 2677 |
|
| 2678 |
return true; |
| 2679 |
} |
| 2680 |
|
| 2681 |
bool ConnectionHandler::doAuth(int &auth_result, bool &authed, int &filtergroup, AuthPlugin *auth_plugin, Socket &peerconn, |
| 2682 |
HTTPHeader &header, NaughtyFilter &cm, bool only_client_ip, bool isconnect_like) { |
| 2683 |
Socket nullsock; |
| 2684 |
return doAuth(auth_result, authed, filtergroup, auth_plugin, peerconn, nullsock, header, cm, only_client_ip, isconnect_like); |
| 2685 |
} |
| 2686 |
|
| 2687 |
bool ConnectionHandler::doAuth(int &rc, bool &authed, int &filtergroup, AuthPlugin *auth_plugin, Socket &peerconn, |
| 2688 |
Socket &proxysock, HTTPHeader &header, NaughtyFilter &cm, bool only_client_ip, bool isconnect_like) { |
| 2689 |
|
| 2690 |
DEBUG_debug(" -Not got persistent credentials for this connection - querying auth plugins"); |
| 2691 |
bool dobreak = false; |
| 2692 |
rc = 0; |
| 2693 |
if (o.plugins.authplugins.size() != 0) { |
| 2694 |
// We have some auth plugins load |
| 2695 |
// int authloop = 0; |
| 2696 |
rc = 0; |
| 2697 |
String tmp; |
| 2698 |
int p = peerconn.getPort(); |
| 2699 |
|
| 2700 |
for (std::deque<Plugin *>::iterator i = o.plugins.authplugins_begin; i != o.plugins.authplugins_end; i++) { |
| 2701 |
DEBUG_debug(" -Querying next auth plugin..."); |
| 2702 |
// try to get the username & parse the return value |
| 2703 |
auth_plugin = (AuthPlugin *) (*i); |
| 2704 |
if ((only_client_ip && !auth_plugin->client_ip_based) || !auth_plugin->port_matched(p)) |
| 2705 |
continue; |
| 2706 |
|
| 2707 |
// auth plugin selection for multi ports |
| 2708 |
// |
| 2709 |
// |
| 2710 |
// Logic changed to allow auth scan with multiple ports as option to replace auth-port |
| 2711 |
// fixed mapping |
| 2712 |
// |
| 2713 |
|
| 2714 |
rc = auth_plugin->identify(peerconn, proxysock, header, clientuser, is_real_user, SBauth, cm); |
| 2715 |
|
| 2716 |
if (rc == E2AUTH_NOMATCH) { |
| 2717 |
DEBUG_auth("Auth plugin did not find a match; querying remaining plugins"); |
| 2718 |
continue; |
| 2719 |
} else if (rc == E2AUTH_REDIRECT) { |
| 2720 |
DEBUG_auth("Auth plugin told us to redirect client to \"", clientuser, "\"; not querying remaining plugins"); |
| 2721 |
if (isconnect_like) // it is connect or trans https so cannot send redirect |
| 2722 |
{ |
| 2723 |
dobreak = true; |
| 2724 |
break; |
| 2725 |
} else { |
| 2726 |
// ident plugin told us to redirect to a login page |
| 2727 |
String writestring("HTTP/1.1 302 Redirect\r\nLocation: "); |
| 2728 |
writestring += clientuser; |
| 2729 |
writestring += "\r\n\r\n"; |
| 2730 |
peerconn.writeString(writestring.toCharArray()); // no action on failure |
| 2731 |
dobreak = true; |
| 2732 |
break; |
| 2733 |
} |
| 2734 |
} else if (rc == E2AUTH_OK_NOPERSIST) { |
| 2735 |
DEBUG_auth("Auth plugin returned OK but no persist not setting persist auth"); |
| 2736 |
overide_persist = true; |
| 2737 |
} else if (rc == E2AUTH_OK_GOT_GROUP) { |
| 2738 |
DEBUG_auth("Auth plugin returned OK_GOT_GROUP "); |
| 2739 |
filtergroup = SBauth.filter_group; |
| 2740 |
authed = true; |
| 2741 |
break; // got user and group so break |
| 2742 |
} else if (rc == E2AUTH_OK_GOT_GROUP_NAME) { |
| 2743 |
DEBUG_auth("Auth plugin returned OK_GOT_GROUP_NAME "); |
| 2744 |
filtergroup = ldl->getFgFromName(SBauth.fg_name); |
| 2745 |
if (filtergroup < 0) { // lookup failed |
| 2746 |
filtergroup = o.filter.default_fg; |
| 2747 |
DEBUG_auth("Group ",SBauth.fg_name, " not known - setting to default"); |
| 2748 |
} |
| 2749 |
SBauth.filter_group = filtergroup; |
| 2750 |
authed = true; |
| 2751 |
break; // got user and group so break |
| 2752 |
} else if (rc == E2AUTH_407_SENT) { |
| 2753 |
DEBUG_auth("Auth plugin has sent 407 so break"); |
| 2754 |
dobreak = true; |
| 2755 |
break; |
| 2756 |
} else if (rc < 0) { |
| 2757 |
E2LOGGER_error("Auth plugin returned error code: ", rc); |
| 2758 |
dobreak = true; |
| 2759 |
break; |
| 2760 |
} |
| 2761 |
DEBUG_auth(" -Auth plugin found username ", clientuser, " (", oldclientuser, "), now determining group"); |
| 2762 |
|
| 2763 |
if (clientuser == oldclientuser) { |
| 2764 |
DEBUG_auth(" -Same user as last time, re-using old group no."); |
| 2765 |
authed = true; |
| 2766 |
filtergroup = oldfg; |
| 2767 |
break; |
| 2768 |
} |
| 2769 |
// try to get the filter group & parse the return value |
| 2770 |
rc = auth_plugin->determineGroup(clientuser, filtergroup, ldl->StoryA, cm); |
| 2771 |
if (rc == E2AUTH_OK) { |
| 2772 |
DEBUG_auth("Auth plugin found username & group; not querying remaining plugins"); |
| 2773 |
authed = true; |
| 2774 |
break; |
| 2775 |
} else if (rc == E2AUTH_NOMATCH) { |
| 2776 |
DEBUG_auth("Auth plugin did not find a match; querying remaining plugins"); |
| 2777 |
clientuser = ""; |
| 2778 |
continue; |
| 2779 |
} else if (rc == E2AUTH_NOGROUP) { |
| 2780 |
if (o.plugins.auth.auth_requires_user_and_group || !is_real_user) { |
| 2781 |
clientuser = ""; |
| 2782 |
SBauth.user_source = ""; |
| 2783 |
continue; |
| 2784 |
} |
| 2785 |
DEBUG_auth("Auth plugin found username \"", clientuser, "\" but no associated group; not querying remaining plugins"); |
| 2786 |
|
| 2787 |
authed = true; |
| 2788 |
break; |
| 2789 |
} else if (rc < 0) { |
| 2790 |
E2LOGGER_error("Auth plugin returned error code: ", rc); |
| 2791 |
dobreak = true; |
| 2792 |
break; |
| 2793 |
} |
| 2794 |
} // end of querying all plugins (for) |
| 2795 |
|
| 2796 |
// break the peer loop |
| 2797 |
if (dobreak) |
| 2798 |
return false; |
| 2799 |
//break; |
| 2800 |
|
| 2801 |
if ((!authed) || (filtergroup < 0) || (filtergroup >= o.filter.numfg)) { |
| 2802 |
#ifdef DEBUG_LOW |
| 2803 |
if (!authed) { |
| 2804 |
DEBUG_auth(" -No identity found; using defaults"); |
| 2805 |
} |
| 2806 |
else { |
| 2807 |
DEBUG_auth(" -Plugin returned out-of-range filter group number; using defaults"); |
| 2808 |
} |
| 2809 |
#endif |
| 2810 |
|
| 2811 |
// If none of the auth plugins currently loaded rely on querying the proxy, |
| 2812 |
// such as 'ident' or 'ip', then pretend we're authed. What this flag |
| 2813 |
// actually controls is whether or not the query should be forwarded to the |
| 2814 |
// proxy (without pre-emptive blocking); we don't want this for 'ident' or |
| 2815 |
// 'ip', because Squid isn't necessarily going to return 'auth required'. |
| 2816 |
// All proxy-auths have been removed in v5.5 so set this to 'true' |
| 2817 |
authed = true; |
| 2818 |
clientuser = "-"; |
| 2819 |
} else { |
| 2820 |
DEBUG_auth(" -Identity found; caching username & group"); |
| 2821 |
if (auth_plugin->is_connection_based && !overide_persist) { |
| 2822 |
DEBUG_auth("Auth plugin is for a connection-based auth method - keeping credentials for entire connection"); |
| 2823 |
persistent_authed = true; |
| 2824 |
} |
| 2825 |
oldclientuser = clientuser; |
| 2826 |
oldfg = filtergroup; |
| 2827 |
} |
| 2828 |
} else { |
| 2829 |
// We don't have any auth plugins loaded |
| 2830 |
DEBUG_auth(" -No auth plugins loaded; using defaults & feigning persistency"); |
| 2831 |
authed = true; |
| 2832 |
clientuser = "-"; |
| 2833 |
//filtergroup = 0; //default group - one day configurable? - default now set before call to doAuth |
| 2834 |
persistent_authed = true; |
| 2835 |
} |
| 2836 |
return true; |
| 2837 |
} |
| 2838 |
|
| 2839 |
bool ConnectionHandler::checkByPass(NaughtyFilter &checkme, std::shared_ptr<LOptionContainer> &ldl, HTTPHeader &header, |
| 2840 |
Socket &proxysock, Socket &peerconn, std::string &clientip) { |
| 2841 |
|
| 2842 |
//first check if bypass allowed and set isbypassallowed |
| 2843 |
checkme.isbypassallowed = (ldl->fg[filtergroup]->bypass_mode != 0); |
| 2844 |
checkme.isinfectionbypassallowed = (ldl->fg[filtergroup]->infection_bypass_mode != 0); |
| 2845 |
checkme.isscanbypassallowed = (ldl->fg[filtergroup]->scan_bypass); |
| 2846 |
if (!(checkme.isbypassallowed || checkme.isinfectionbypassallowed)) |
| 2847 |
return false; |
| 2848 |
|
| 2849 |
// int bypasstimestamp = 0; |
| 2850 |
if (isScanBypassURL(checkme.url, ldl->fg[filtergroup]->magic.c_str(), clientip.c_str())) { |
| 2851 |
DEBUG_debug(" -Scan bypass URL match"); |
| 2852 |
checkme.isscanbypass = true; |
| 2853 |
checkme.isbypass = true; |
| 2854 |
checkme.message_no = 608; |
| 2855 |
checkme.log_message_no = 608; |
| 2856 |
checkme.whatIsNaughty= o.language_list.getTranslation(608); |
| 2857 |
} else if ((ldl->fg[filtergroup]->bypass_mode != 0) || (ldl->fg[filtergroup]->infection_bypass_mode != 0)) { |
| 2858 |
DEBUG_debug(" -About to check for bypass..."); |
| 2859 |
if (ldl->fg[filtergroup]->bypass_mode != 0) |
| 2860 |
checkme.bypasstimestamp = isBypassURL(checkme.logurl, ldl->fg[filtergroup]->magic.c_str(), |
| 2861 |
clientip.c_str(), "GBYPASS=", clientuser); |
| 2862 |
if ((checkme.bypasstimestamp == 0) && (ldl->fg[filtergroup]->infection_bypass_mode != 0)) |
| 2863 |
checkme.bypasstimestamp = isBypassURL(checkme.logurl, ldl->fg[filtergroup]->imagic.c_str(), |
| 2864 |
clientip.c_str(), "GIBYPASS=", |
| 2865 |
clientuser); |
| 2866 |
if (checkme.bypasstimestamp > 0) { |
| 2867 |
if (checkme.isvirusbypass) { |
| 2868 |
DEBUG_debug(" -Infection bypass URL match"); |
| 2869 |
header.chopBypass(checkme.logurl, "GIBYPASS="); |
| 2870 |
} else { |
| 2871 |
DEBUG_debug(" -Filter bypass URL match"); |
| 2872 |
header.chopBypass(checkme.logurl, "GBYPASS="); |
| 2873 |
} |
| 2874 |
if (checkme.bypasstimestamp > 1) { // not expired |
| 2875 |
checkme.isbypass = true; |
| 2876 |
checkme.isexception = true; |
| 2877 |
// checkme: need a TR string for virus bypass |
| 2878 |
checkme.whatIsNaughty = o.language_list.getTranslation(606); |
| 2879 |
checkme.whatIsNaughtyLog = checkme.whatIsNaughty; |
| 2880 |
checkme.message_no = 606; |
| 2881 |
checkme.log_message_no = 606; |
| 2882 |
} |
| 2883 |
} else if (ldl->fg[filtergroup]->bypass_mode != 0) { |
| 2884 |
String ud(checkme.urldomain); |
| 2885 |
if (ud.startsWith("www.")) { |
| 2886 |
ud = ud.after("www."); |
| 2887 |
} |
| 2888 |
if (header.isBypassCookie(ud, ldl->fg[filtergroup]->cookie_magic.c_str(), |
| 2889 |
clientip.c_str(), clientuser.c_str())) { |
| 2890 |
DEBUG_debug(" -Bypass cookie match"); |
| 2891 |
checkme.iscookiebypass = true; |
| 2892 |
checkme.isbypass = true; |
| 2893 |
checkme.isexception = true; |
| 2894 |
checkme.whatIsNaughty = o.language_list.getTranslation(607); |
| 2895 |
checkme.whatIsNaughtyLog = checkme.whatIsNaughty; |
| 2896 |
} |
| 2897 |
} |
| 2898 |
DEBUG_debug(" -Finished bypass checks."); |
| 2899 |
} |
| 2900 |
|
| 2901 |
if (checkme.isbypass) { |
| 2902 |
DEBUG_debug(" -bypass activated:"); |
| 2903 |
} |
| 2904 |
// |
| 2905 |
// End of bypass |
| 2906 |
// |
| 2907 |
// Start of scan by pass |
| 2908 |
// |
| 2909 |
|
| 2910 |
if (checkme.isscanbypass) { |
| 2911 |
//we need to decode the URL and send the temp file with the |
| 2912 |
//correct header to the client then delete the temp file |
| 2913 |
checkme.tempfilename = (checkme.url.after("GSBYPASS=").after("&N=")); |
| 2914 |
checkme.tempfilemime = (checkme.tempfilename.after("&M=")); |
| 2915 |
checkme.tempfiledis = (header.decode(checkme.tempfilemime.after("&D="), true)); |
| 2916 |
DEBUG_debug(" -Original filename: " ); |
| 2917 |
String rtype(header.requestType()); |
| 2918 |
checkme.tempfilemime = checkme.tempfilemime.before("&D="); |
| 2919 |
checkme.tempfilename = o.content.download_dir + "/tf" + checkme.tempfilename.before("&M="); |
| 2920 |
return true; |
| 2921 |
} |
| 2922 |
return false; |
| 2923 |
} |
| 2924 |
|
| 2925 |
bool ConnectionHandler::sendScanFile(Socket &peerconn, NaughtyFilter &checkme, bool is_icap, ICAPHeader *icaphead) { |
| 2926 |
try { |
| 2927 |
checkme.docsize = sendFile(&peerconn, checkme, checkme.url, is_icap, icaphead); |
| 2928 |
checkme.request_header->chopBypass(checkme.url,"GSBYPASS="); |
| 2929 |
checkme.logurl = checkme.request_header->getLogUrl(); |
| 2930 |
|
| 2931 |
doLog(clientuser, checkme.clientip, checkme); |
| 2932 |
|
| 2933 |
if (o.content.delete_downloaded_temp_files) { |
| 2934 |
unlink(checkme.tempfilename.toCharArray()); |
| 2935 |
} |
| 2936 |
} catch ( |
| 2937 |
std::exception &e |
| 2938 |
) { |
| 2939 |
} |
| 2940 |
// persistProxy = false; |
| 2941 |
// proxysock.close(); // close connection to proxy |
| 2942 |
return true; |
| 2943 |
} |
| 2944 |
|
| 2945 |
void ConnectionHandler::check_search_terms(NaughtyFilter &cm) { |
| 2946 |
if (ldl->fg[filtergroup]->searchterm_limit > 0) { |
| 2947 |
String terms; |
| 2948 |
terms = cm.search_terms; |
| 2949 |
// search terms are URL parameter type "0" |
| 2950 |
urlparams.append("0=").append(terms).append(";"); |
| 2951 |
// Add spaces at beginning and end of block before filtering, so |
| 2952 |
// that the quick & dirty trick of putting spaces around words |
| 2953 |
// (Scunthorpe problem) can still be used, bearing in mind the block |
| 2954 |
// of text here is usually very small. |
| 2955 |
terms.insert(terms.begin(), ' '); |
| 2956 |
terms.append(" "); |
| 2957 |
cm.checkme(terms.c_str(), terms.length(), NULL, NULL, ldl->fg[filtergroup], |
| 2958 |
(ldl->fg[filtergroup]->searchterm_flag ? ldl->fg[filtergroup]->searchterm_list |
| 2959 |
: ldl->fg[filtergroup]->banned_phrase_list), |
| 2960 |
ldl->fg[filtergroup]->searchterm_limit, true); |
| 2961 |
if (cm.isItNaughty) { |
| 2962 |
cm.blocktype = 2; |
| 2963 |
} |
| 2964 |
} |
| 2965 |
return; |
| 2966 |
} |
| 2967 |
|
| 2968 |
void ConnectionHandler::check_content(NaughtyFilter &cm, DataBuffer &docbody, Socket &proxysock, Socket &peerconn, |
| 2969 |
std::deque<CSPlugin *> &responsescanners) { |
| 2970 |
if ((cm.response_header->OKtoFilterMime(ldl->fg[filtergroup]) |
| 2971 |
&& !cm.isexception) || !responsescanners.empty()) { |
| 2972 |
cm.waschecked = true; |
| 2973 |
if (!responsescanners.empty()) { |
| 2974 |
DEBUG_debug(" -Filtering with expectation of a possible csmessage");; |
| 2975 |
String csmessage; |
| 2976 |
contentFilter(cm.response_header, cm.request_header, &docbody, &proxysock, &peerconn, &cm.headersent, |
| 2977 |
&cm.pausedtoobig, |
| 2978 |
&cm.docsize, &cm, cm.wasclean, filtergroup, responsescanners, &clientuser, &cm.clientip, |
| 2979 |
&cm.wasinfected, &cm.wasscanned, cm.isbypass, cm.urld, cm.urldomain, &cm.scanerror, |
| 2980 |
cm.contentmodified, &csmessage); |
| 2981 |
if (csmessage.length() > 0) { |
| 2982 |
DEBUG_debug(" -csmessage found: ", csmessage);; |
| 2983 |
cm.whatIsNaughty = csmessage.toCharArray(); |
| 2984 |
} |
| 2985 |
} else { |
| 2986 |
DEBUG_debug(" -Calling contentFilter ");; |
| 2987 |
contentFilter(cm.response_header, cm.request_header, &docbody, &proxysock, &peerconn, &cm.headersent, |
| 2988 |
&cm.pausedtoobig, |
| 2989 |
&cm.docsize, &cm, cm.wasclean, filtergroup, responsescanners, &clientuser, &cm.clientip, |
| 2990 |
&cm.wasinfected, &cm.wasscanned, cm.isbypass, cm.urld, cm.urldomain, &cm.scanerror, |
| 2991 |
cm.contentmodified, NULL); |
| 2992 |
} |
| 2993 |
} else { |
| 2994 |
cm.tunnel_rest = true; |
| 2995 |
} |
| 2996 |
DEBUG_debug("End content check isitNaughty is ", cm.isItNaughty); |
| 2997 |
} |
| 2998 |
|
| 2999 |
int ConnectionHandler::handleProxyTLSConnection(Socket &peerconn, String &ip, Socket &upsconn, stat_rec* &dystat) { |
| 3000 |
|
| 3001 |
struct timeval thestart; |
| 3002 |
gettimeofday(&thestart, NULL); |
| 3003 |
|
| 3004 |
peerconn.setTimeout(o.net.pcon_timeout); |
| 3005 |
|
| 3006 |
X509 *cert = NULL; |
| 3007 |
struct ca_serial caser; |
| 3008 |
caser.asn = NULL; |
| 3009 |
caser.charhex = NULL; |
| 3010 |
caser.filepath = NULL; |
| 3011 |
caser.filename = NULL; |
| 3012 |
|
| 3013 |
EVP_PKEY *pkey = NULL; |
| 3014 |
bool certfromcache = false; |
| 3015 |
//generate the cert |
| 3016 |
DEBUG_debug(" -Getting ssl certificate for client TLS proxy connection"); |
| 3017 |
|
| 3018 |
pkey = o.cert.ca->getServerPkey(); |
| 3019 |
|
| 3020 |
//generate the certificate but dont write it to disk (avoid someone |
| 3021 |
//requesting lots of places that dont exist causing the disk to fill |
| 3022 |
//up / run out of inodes |
| 3023 |
certfromcache = o.cert.ca->getServerCertificate(o.net.TLSproxyCN.c_str(), &cert, |
| 3024 |
&caser, o.net.TLSproxyCN_is_ip); |
| 3025 |
if (caser.asn == NULL) { |
| 3026 |
DEBUG_debug("caser.asn is NULL"); } |
| 3027 |
// std::cerr << "serials are: " << (char) *caser.asn << " " < caser.charhex << std::endl; |
| 3028 |
|
| 3029 |
//check that the generated cert is not null |
| 3030 |
if (cert == NULL) { |
| 3031 |
DEBUG_debug(" cert is NULL for TLS proxy"); |
| 3032 |
return 1; |
| 3033 |
} |
| 3034 |
|
| 3035 |
if (peerconn.startSslServer(cert, pkey, o.cert.set_cipher_list) < 0) { |
| 3036 |
peerconn.stopSsl(); |
| 3037 |
if(cert != NULL) { |
| 3038 |
X509_free(cert); |
| 3039 |
cert = NULL; |
| 3040 |
} |
| 3041 |
return 1; |
| 3042 |
} |
| 3043 |
|
| 3044 |
if(!certfromcache) |
| 3045 |
o.cert.ca->writeCertificate(o.net.TLSproxyCN.c_str(), cert, &caser); |
| 3046 |
|
| 3047 |
// Now create a pipe - push one end onto normal proxy queue and then tunnel between other end and the ssled peerconn |
| 3048 |
int socks[2]; |
| 3049 |
//if (socketpair(AF_UNIX,SOCK_STREAM|SOCK_NONBLOCK, 0, socks) != 0) |
| 3050 |
if (socketpair(AF_UNIX, SOCK_STREAM, 0, socks) != 0) { |
| 3051 |
E2LOGGER_error("Unable to create socket pair"); |
| 3052 |
return 1; |
| 3053 |
} |
| 3054 |
Socket *s_inside = new Socket(socks[0]); |
| 3055 |
Socket s_outside(socks[1]); |
| 3056 |
s_inside->setClientAddr(peerconn.getPeerIP(),peerconn.getPeerSourcePort()); |
| 3057 |
s_inside->setPort(peerconn.getPort()); |
| 3058 |
s_inside->down_thread_id = thread_id; |
| 3059 |
|
| 3060 |
//Q for service |
| 3061 |
LQ_rec lq_rec; |
| 3062 |
lq_rec.sock = s_inside; |
| 3063 |
lq_rec.ct_type = CT_PROXY; |
| 3064 |
DEBUG_debug("inside pair socket about to push to Q"); |
| 3065 |
o.http_worker_Q.push(lq_rec); |
| 3066 |
DEBUG_debug("inside pair socket pushed to Q"); |
| 3067 |
//DEBUG_network("about to connect to 8084"); |
| 3068 |
// upsconn.connect("127.0.0.1", 8084); |
| 3069 |
// DEBUG_network("connected to 8084 - starting tunnell"); |
| 3070 |
|
| 3071 |
// and then two way tunnel to outside socket; |
| 3072 |
FDTunnel tunn; |
| 3073 |
|
| 3074 |
tunn.tunnel(peerconn, s_outside, true); |
| 3075 |
DEBUG_network("tunnell finished"); |
| 3076 |
peerconn.stopSsl(); |
| 3077 |
peerconn.close(); |
| 3078 |
// if (s_inside != nullptr) delete s_inside; |
| 3079 |
return 0; |
| 3080 |
} |
| 3081 |
|
| 3082 |
int ConnectionHandler::handleTHTTPSConnection(Socket &peerconn, String &ip, Socket &proxysock, stat_rec* &dystat) { |
| 3083 |
DEBUG_trace(""); |
| 3084 |
|
| 3085 |
struct timeval thestart; |
| 3086 |
gettimeofday(&thestart, NULL); |
| 3087 |
|
| 3088 |
peerconn.setTimeout(o.net.pcon_timeout); |
| 3089 |
|
| 3090 |
HTTPHeader docheader(__HEADER_RESPONSE); // to hold the returned page header from proxy |
| 3091 |
HTTPHeader header(__HEADER_REQUEST); // to hold the incoming client request headeri(ldl) |
| 3092 |
|
| 3093 |
NaughtyFilter checkme(header, docheader, SBauth); |
| 3094 |
checkme.listen_port = peerconn.getPort(); |
| 3095 |
checkme.reset(); |
| 3096 |
|
| 3097 |
|
| 3098 |
std::string clientip(ip.toCharArray()); // hold the clients ip |
| 3099 |
docheader.setClientIP(ip); |
| 3100 |
|
| 3101 |
if (clienthost) delete clienthost; |
| 3102 |
|
| 3103 |
clienthost = NULL; // and the hostname, if available |
| 3104 |
matchedip = false; |
| 3105 |
|
| 3106 |
#define CLIENT_HELLO_MAX_SIZE 16384 |
| 3107 |
|
| 3108 |
DEBUG_thttps(" -got peer connection - clientip is ", clientip); |
| 3109 |
|
| 3110 |
try { |
| 3111 |
int rc; |
| 3112 |
|
| 3113 |
|
| 3114 |
//int oldfg = 0; |
| 3115 |
bool authed = false; |
| 3116 |
bool isbanneduser = false; |
| 3117 |
bool firsttime = true; |
| 3118 |
String sni; |
| 3119 |
bool is_ech = false; |
| 3120 |
|
| 3121 |
AuthPlugin *auth_plugin = NULL; |
| 3122 |
|
| 3123 |
// RFC states that connections are persistent |
| 3124 |
//bool persistOutgoing = true; |
| 3125 |
//bool persistPeer = true; |
| 3126 |
bool persistProxy = false; |
| 3127 |
//bool direct = false; |
| 3128 |
|
| 3129 |
char buff[7]; |
| 3130 |
rc = peerconn.readFromSocket(buff, 6, (MSG_PEEK), 20000, true); |
| 3131 |
DEBUG_thttps("bytes peeked ", rc); |
| 3132 |
unsigned int toread = 0; |
| 3133 |
if (rc == 6) { |
| 3134 |
if (buff[0] == 22 && buff[1] == 3 && buff[2] > 0 && buff[2] < 4 && |
| 3135 |
buff[5] == 1) { // has TLS client hello signature |
| 3136 |
toread = two_bytes_to_short((unsigned char*)&(buff[3])); |
| 3137 |
|
| 3138 |
// unsigned int t3 = buff[4]; |
| 3139 |
// toread = (toread * 256) + t3; |
| 3140 |
if ((toread <= CLIENT_HELLO_MAX_SIZE)&&(toread > 52)) { |
| 3141 |
checkme.isTLS = true; |
| 3142 |
toread += 5; |
| 3143 |
} |
| 3144 |
#ifdef DEBUG_HIGH |
| 3145 |
else { |
| 3146 |
//toread = CLIENT_HELLO_MAX_SIZE + 5; |
| 3147 |
DEBUG_thttps("Too small or Oversized clienthello detected"); |
| 3148 |
} |
| 3149 |
#endif |
| 3150 |
//toread = (buff[3] << (8 * 1) | buff[4]) + 5; |
| 3151 |
} |
| 3152 |
} |
| 3153 |
|
| 3154 |
DEBUG_thttps("hello length is ", toread, " magic is ", buff[0], buff[1], buff[2], " isTLS is ", |
| 3155 |
checkme.isTLS); |
| 3156 |
|
| 3157 |
if (checkme.isTLS) { |
| 3158 |
char buff2[CLIENT_HELLO_MAX_SIZE + 6 ]; |
| 3159 |
//char *buff2 = new char[(toread + 1)]; |
| 3160 |
rc = peerconn.readFromSocket(buff2, toread, (MSG_PEEK), 10000); |
| 3161 |
if (rc < 52) { |
| 3162 |
if (o.conn.logconerror) { |
| 3163 |
if (peerconn.getFD() > -1) { |
| 3164 |
|
| 3165 |
int err = peerconn.getErrno(); |
| 3166 |
//int pport = peerconn.getPeerSourcePort(); |
| 3167 |
std::string peerIP = peerconn.getPeerIP(); |
| 3168 |
if (peerconn.isTimedout()) { |
| 3169 |
DEBUG_thttps("Connection timed out"); |
| 3170 |
} |
| 3171 |
E2LOGGER_error("No TLS header recd from client - errno: ", err); |
| 3172 |
} else { |
| 3173 |
E2LOGGER_info("Client connection closed early - no TLS header received"); |
| 3174 |
} |
| 3175 |
} |
| 3176 |
firsttime = false; |
| 3177 |
//persistPeer = false; |
| 3178 |
} else { |
| 3179 |
DEBUG_thttps("bytes peeked ", rc); |
| 3180 |
get_TLS_SNI(buff2, rc, sni, is_ech); |
| 3181 |
if (!sni.empty()) { |
| 3182 |
checkme.url = sni; |
| 3183 |
checkme.hasSNI = true; |
| 3184 |
} |
| 3185 |
checkme.hasECH = is_ech; |
| 3186 |
} |
| 3187 |
++dystat->reqs; |
| 3188 |
} |
| 3189 |
//delete[] buff2; |
| 3190 |
|
| 3191 |
get_original_ip_port(peerconn, checkme); |
| 3192 |
|
| 3193 |
if (!checkme.hasSNI) { |
| 3194 |
if (checkme.got_orig_ip) checkme.url = checkme.orig_ip; |
| 3195 |
else // no SNI and no orig_ip - so can't do anything sensible |
| 3196 |
return -1; |
| 3197 |
} |
| 3198 |
|
| 3199 |
DEBUG_thttps("hasSNI = ", checkme.hasSNI, " SNI is ", checkme.url, " Orig IP ", checkme.orig_ip, |
| 3200 |
" hasECH = ", checkme.hasECH, |
| 3201 |
" Orig port ", checkme.orig_port); |
| 3202 |
// checkme.url = sni; |
| 3203 |
checkme.hasSNI = true; |
| 3204 |
// End of set-up section |
| 3205 |
|
| 3206 |
while (firsttime) // do just the once |
| 3207 |
{ |
| 3208 |
ldl = o.currentLists(); |
| 3209 |
//DataBuffer docbody; |
| 3210 |
//docbody.setTimeout(o.exchange_timeout); |
| 3211 |
FDTunnel fdt; |
| 3212 |
|
| 3213 |
firsttime = false; |
| 3214 |
|
| 3215 |
// |
| 3216 |
// do all of this normalisation etc just the once at the start. |
| 3217 |
checkme.url = "https://" + checkme.url; |
| 3218 |
checkme.setURL(checkme.url); |
| 3219 |
checkme.nomitm = false; |
| 3220 |
gettimeofday(&checkme.thestart, NULL); |
| 3221 |
|
| 3222 |
|
| 3223 |
// Look up reverse DNS name of client if needed |
| 3224 |
if (o.conn.reverse_client_ip_lookups) { |
| 3225 |
getClientFromIP(clientip.c_str(), checkme.clienthost); |
| 3226 |
} |
| 3227 |
|
| 3228 |
filtergroup = o.filter.default_trans_fg; |
| 3229 |
|
| 3230 |
//if(o.log_requests) |
| 3231 |
if (e2logger.isEnabled(LoggerSource::requestlog)) { |
| 3232 |
std::string fnt = "THTTPS"; |
| 3233 |
doRQLog(clientuser, clientip, checkme, fnt); |
| 3234 |
} |
| 3235 |
|
| 3236 |
checkme.clientip = clientip; |
| 3237 |
|
| 3238 |
//CALL SB pre-authcheck |
| 3239 |
ldl->StoryA.runFunctEntry(ENT_STORYA_PRE_AUTH_THTTPS, checkme); |
| 3240 |
DEBUG_thttps("After StoryA thttps-pre-authcheck", checkme.isexception, " mess_no ", checkme.message_no); |
| 3241 |
checkme.isItNaughty = checkme.isBlocked; |
| 3242 |
bool isbannedip = checkme.isBlocked; |
| 3243 |
|
| 3244 |
// |
| 3245 |
// |
| 3246 |
// Start of Authentication Checks |
| 3247 |
// |
| 3248 |
// |
| 3249 |
// don't have credentials for this connection yet? get some! |
| 3250 |
overide_persist = false; |
| 3251 |
if (!(checkme.isItNaughty || checkme.isexception)) { |
| 3252 |
if (!doAuth(checkme.auth_result, authed, filtergroup, auth_plugin, peerconn, proxysock, header, |
| 3253 |
checkme, true, true)) { |
| 3254 |
|
| 3255 |
if ((checkme.auth_result == E2AUTH_REDIRECT) && ldl->fg[filtergroup]->ssl_mitm) { |
| 3256 |
if (!checkme.nomitm)checkme.gomitm = true; |
| 3257 |
checkme.isdone = true; |
| 3258 |
} else { |
| 3259 |
break; |
| 3260 |
} |
| 3261 |
} |
| 3262 |
} |
| 3263 |
checkme.filtergroup = filtergroup; |
| 3264 |
if (!checkme.nomitm) checkme.nomitm = !ldl->fg[filtergroup]->ssl_mitm; |
| 3265 |
|
| 3266 |
DEBUG_thttps(" -username: ", clientuser, " -filtergroup: ", filtergroup); |
| 3267 |
// |
| 3268 |
// |
| 3269 |
// End of Authentication Checking |
| 3270 |
// |
| 3271 |
// |
| 3272 |
|
| 3273 |
// |
| 3274 |
// |
| 3275 |
// Now check if user or machine is banned and room-based checking |
| 3276 |
// |
| 3277 |
// |
| 3278 |
|
| 3279 |
// is this user banned? |
| 3280 |
isbanneduser = false; |
| 3281 |
|
| 3282 |
|
| 3283 |
if (checkme.hasSNI & !checkme.nomitm) checkme.ismitmcandidate = ldl->fg[filtergroup]->ssl_mitm; |
| 3284 |
if (checkme.ismitmcandidate) { |
| 3285 |
checkme.automitm = ldl->fg[filtergroup]->automitm; |
| 3286 |
} |
| 3287 |
|
| 3288 |
|
| 3289 |
// TODO restore this for THTTPS ?? |
| 3290 |
//if (isbannedip) { |
| 3291 |
// matchedip = clienthost == NULL; |
| 3292 |
//} else { |
| 3293 |
// / if (ldl->inRoom(clientip, room, clienthost, &isbannedip, &part_banned, &checkme.isexception, |
| 3294 |
// / checkme.urld)) { |
| 3295 |
// / DEBUG_thttps(" isbannedip = ", isbannedip, "ispart_banned = ", part_banned, " isexception = ", checkme.isexception); |
| 3296 |
//. if (isbannedip) { |
| 3297 |
// matchedip = clienthost == NULL; |
| 3298 |
// checkme.isBlocked = checkme.isItNaughty = true; |
| 3299 |
// / } |
| 3300 |
// if (checkme.isexception) { |
| 3301 |
// do reason codes etc |
| 3302 |
//checkme.exceptionreason = o.language_list.getTranslation(630); |
| 3303 |
//checkme.exceptionreason.append(room); |
| 3304 |
//checkme.exceptionreason.append(o.language_list.getTranslation(631)); |
| 3305 |
//checkme.message_no = 632; |
| 3306 |
//} |
| 3307 |
//} |
| 3308 |
//} |
| 3309 |
|
| 3310 |
// |
| 3311 |
// Start of exception checking |
| 3312 |
// |
| 3313 |
// being a banned user/IP overrides the fact that a site may be in the exception lists |
| 3314 |
// needn't check these lists in bypass modes |
| 3315 |
if (!(checkme.isdone || isbanneduser || isbannedip || checkme.isexception)) { |
| 3316 |
DEBUG_trace("Check StoryB thttps-checkrequest"); |
| 3317 |
ldl->fg[filtergroup]->StoryB.runFunctEntry(ENT_STORYB_THTTPS_REQUEST, checkme); |
| 3318 |
DEBUG_trace("After StoryB thttps-checkrequest", |
| 3319 |
" isException: ", String(checkme.isexception), |
| 3320 |
" mess_no ", String(checkme.message_no)); |
| 3321 |
|
| 3322 |
if (ldl->fg[filtergroup]->reporting_level != -1) { |
| 3323 |
checkme.isItNaughty = checkme.isBlocked; |
| 3324 |
} else { |
| 3325 |
checkme.isItNaughty = false; |
| 3326 |
checkme.isBlocked = false; |
| 3327 |
} |
| 3328 |
} |
| 3329 |
|
| 3330 |
//now send upstream and get response |
| 3331 |
if (!checkme.isItNaughty && !persistProxy) { |
| 3332 |
int out_port; |
| 3333 |
if (checkme.got_orig_ip && o.conn.use_original_ip_port) |
| 3334 |
out_port = checkme.orig_port; |
| 3335 |
else |
| 3336 |
out_port = 443; |
| 3337 |
|
| 3338 |
if (connectUpstream(proxysock, checkme, out_port) > -1) { |
| 3339 |
if (!checkme.isdirect) { |
| 3340 |
if (sendProxyConnect(checkme.connect_site, &proxysock, &checkme) != 0) { |
| 3341 |
checkme.upfailure = true; |
| 3342 |
proxysock.close(); |
| 3343 |
} |
| 3344 |
} |
| 3345 |
} else { |
| 3346 |
checkme.upfailure = true; |
| 3347 |
} |
| 3348 |
} |
| 3349 |
|
| 3350 |
DEBUG_thttps(" after connectUpstream nf ", checkme.isItNaughty, " upfail ", checkme.upfailure); |
| 3351 |
|
| 3352 |
if ((checkme.isItNaughty || checkme.upfailure) && checkme.automitm && checkme.hasSNI) |
| 3353 |
checkme.gomitm = true; // allows us to send splash page |
| 3354 |
|
| 3355 |
if (checkme.isexception && !checkme.upfailure) { |
| 3356 |
checkme.tunnel_rest = true; |
| 3357 |
} else { |
| 3358 |
|
| 3359 |
//if ismitm - GO MITM |
| 3360 |
if (checkme.gomitm && !checkme.nomitm) { |
| 3361 |
DEBUG_thttps("Going MITM ...."); |
| 3362 |
if (!ldl->fg[filtergroup]->mitm_check_cert) |
| 3363 |
checkme.nocheckcert = true; |
| 3364 |
goMITM(checkme, proxysock, peerconn, persistProxy, authed, persistent_authed, ip, dystat, |
| 3365 |
clientip, true); |
| 3366 |
//persistPeer = false; |
| 3367 |
persistProxy = false; |
| 3368 |
//if (!checkme.isItNaughty) |
| 3369 |
break; |
| 3370 |
} else { |
| 3371 |
if (!checkme.upfailure) |
| 3372 |
checkme.tunnel_rest = true; |
| 3373 |
} |
| 3374 |
} |
| 3375 |
|
| 3376 |
//if not grey tunnel response |
| 3377 |
if (!checkme.isItNaughty && checkme.tunnel_rest) { |
| 3378 |
DEBUG_thttps(" -Tunnelling to client"); |
| 3379 |
if (!fdt.tunnel(proxysock, peerconn, true, -1, true)) |
| 3380 |
persistProxy = false; |
| 3381 |
checkme.docsize += fdt.throughput; |
| 3382 |
} |
| 3383 |
|
| 3384 |
// it is not possible to send splash page on Thttps without MITM so do not try! |
| 3385 |
|
| 3386 |
//Log |
| 3387 |
doLog(clientuser, clientip, checkme); |
| 3388 |
|
| 3389 |
proxysock.close(); // close connection to proxy |
| 3390 |
|
| 3391 |
} |
| 3392 |
} catch (std::exception &e) { |
| 3393 |
DEBUG_thttps(" - THTTPS connection handler caught an exception: ", e.what()); |
| 3394 |
if (o.conn.logconerror) |
| 3395 |
E2LOGGER_error(" - THTTPS connection handler caught an exception %s", e.what()); |
| 3396 |
|
| 3397 |
// close connection to proxy |
| 3398 |
proxysock.close(); |
| 3399 |
return -1; |
| 3400 |
} |
| 3401 |
|
| 3402 |
return 0; |
| 3403 |
} |
| 3404 |
|
| 3405 |
|
| 3406 |
bool ConnectionHandler::get_TLS_SNI(char *inbytes, int len, String &r_sni, bool &is_ech) { |
| 3407 |
auto bytes = reinterpret_cast<unsigned char *>(inbytes); |
| 3408 |
unsigned char *curr; |
| 3409 |
unsigned char *ebytes; |
| 3410 |
char sni[256]; |
| 3411 |
std::memset(sni,0,256); |
| 3412 |
bool got_sni = false; |
| 3413 |
if (len < 52) return false; |
| 3414 |
ebytes = bytes + len; |
| 3415 |
unsigned short int sid_len = bytes[43]; |
| 3416 |
curr = bytes + 1 + 43 + sid_len; // skip past session id |
| 3417 |
if ((curr + 2) > ebytes) return false; |
| 3418 |
unsigned short cslen = two_bytes_to_short(curr); |
| 3419 |
curr += 2 + cslen; // skip past Cipher Suites |
| 3420 |
if ((curr + 1) > ebytes) return false; |
| 3421 |
unsigned short cmplen = *curr; |
| 3422 |
curr += 1 + cmplen; // skip past Compression methods |
| 3423 |
if (curr > ebytes) return false; |
| 3424 |
unsigned char *maxchar = curr + 2 + two_bytes_to_short(curr); // get pointer to end of extensions + 1 |
| 3425 |
curr += 2; |
| 3426 |
unsigned short ext_type = 1; |
| 3427 |
unsigned short ext_len; |
| 3428 |
unsigned short ext_found = 0; |
| 3429 |
unsigned char *ext_end; |
| 3430 |
while(curr < maxchar && ext_found < 2) |
| 3431 |
//while(curr < maxchar && ext_type != 0) |
| 3432 |
{ |
| 3433 |
if (curr > ebytes) break; |
| 3434 |
//if (maxchar > ebytes) return nullptr; |
| 3435 |
ext_type = two_bytes_to_short(curr); |
| 3436 |
curr += 2; |
| 3437 |
if ((curr + 2) > ebytes) break; |
| 3438 |
ext_len = two_bytes_to_short(curr); |
| 3439 |
curr += 2; // pointing at start of extension data |
| 3440 |
ext_end = curr + ext_len; |
| 3441 |
switch (ext_type) { |
| 3442 |
case 0: // Is Server name extension |
| 3443 |
{ |
| 3444 |
unsigned short list_len = two_bytes_to_short(curr); |
| 3445 |
curr += 2; |
| 3446 |
if (list_len < 7) break; |
| 3447 |
unsigned char *list_end = curr + list_len; |
| 3448 |
if (*curr != 0) break; // check list entry type is DNS hostname i.e == 0 |
| 3449 |
curr += 1; // pointing at length of DNS hostname |
| 3450 |
if (curr > ebytes) break; |
| 3451 |
unsigned short namelen = two_bytes_to_short(curr); |
| 3452 |
curr += 2; // pointing at DNS hostname |
| 3453 |
unsigned char *name_end = curr + namelen; |
| 3454 |
if (name_end > list_end) break; |
| 3455 |
if (name_end > ebytes) break; |
| 3456 |
if ((namelen > 3) && (namelen < 254)) { // SNI must be at least 4 bytes and no more than 253 |
| 3457 |
std::memcpy(sni, curr, (size_t) namelen); |
| 3458 |
got_sni = true; |
| 3459 |
ext_found++; |
| 3460 |
} |
| 3461 |
curr = ext_end; // read only first SNI |
| 3462 |
} |
| 3463 |
break; |
| 3464 |
//*(name_end) = (char)0; // add null char to terminate string |
| 3465 |
case 65037: // has encrypted client hello |
| 3466 |
is_ech = true; |
| 3467 |
ext_found++; |
| 3468 |
default: |
| 3469 |
curr = ext_end; |
| 3470 |
break; |
| 3471 |
} |
| 3472 |
} |
| 3473 |
if ( got_sni) { |
| 3474 |
r_sni = sni; |
| 3475 |
bool b = r_sni.is_valid_domain(); |
| 3476 |
if(b && ext_found == 1) return true; // i.e no ECH |
| 3477 |
if (!b) { |
| 3478 |
#ifdef DEBUG_HIGH |
| 3479 |
if (!is_ech) { |
| 3480 |
DEBUG_thttps("Invalid SNI received: ", r_sni); |
| 3481 |
} |
| 3482 |
#endif |
| 3483 |
r_sni = ""; // blank SNI as it is invalid |
| 3484 |
} |
| 3485 |
} |
| 3486 |
#ifdef DEBUG_HIGH |
| 3487 |
if (is_ech) { |
| 3488 |
DEBUG_thttps("SNI is encyrpted: dummy SNI in outer CH is not reliable", r_sni); |
| 3489 |
} |
| 3490 |
#endif |
| 3491 |
|
| 3492 |
return false; //SNI was not present or present but with ECH so unreliable |
| 3493 |
} |
| 3494 |
|
| 3495 |
|
| 3496 |
bool ConnectionHandler::get_original_ip_port(Socket &peerconn, NaughtyFilter &checkme) |
| 3497 |
{ // get original IP destination & port |
| 3498 |
#ifdef SOL_IP // linux |
| 3499 |
#define SO_ORIGINAL_DST 80 |
| 3500 |
sockaddr_in origaddr; |
| 3501 |
socklen_t origaddrlen(sizeof(sockaddr_in)); |
| 3502 |
if ( |
| 3503 |
getsockopt(peerconn.getFD(), SOL_IP, SO_ORIGINAL_DST, &origaddr, &origaddrlen ) < 0 |
| 3504 |
) { |
| 3505 |
E2LOGGER_error("Failed to get client's original destination IP: ", strerror(errno)); |
| 3506 |
return false; |
| 3507 |
} else { |
| 3508 |
char res[INET_ADDRSTRLEN]; |
| 3509 |
checkme.orig_ip = inet_ntop(AF_INET,&origaddr.sin_addr,res,sizeof(res)); |
| 3510 |
// if orig_ip == one of our box ip's it is not true transparent so return false so that dns lookup is enabled |
| 3511 |
if (o.net.check_ip.size() > 0) { |
| 3512 |
for (auto it = o.net.check_ip.begin(); it != o.net.check_ip.end(); it++) { |
| 3513 |
if (*it == checkme.orig_ip) { |
| 3514 |
checkme.orig_ip = ""; |
| 3515 |
return false; |
| 3516 |
} |
| 3517 |
} |
| 3518 |
} |
| 3519 |
checkme.orig_port = ntohs(origaddr.sin_port); |
| 3520 |
checkme.got_orig_ip = true; |
| 3521 |
return true; |
| 3522 |
} |
| 3523 |
#else // TODO: BSD code needs adding - depends on firewall being used |
| 3524 |
// assign checkme.orig_ip and checkme.orig_port and return true |
| 3525 |
// or return false on error |
| 3526 |
|
| 3527 |
// return false until BSD code added |
| 3528 |
return false; |
| 3529 |
#endif |
| 3530 |
} |
| 3531 |
|
| 3532 |
|
| 3533 |
int ConnectionHandler::handleICAPConnection(Socket &peerconn, String &ip, Socket &proxysock, stat_rec *&dystat) { |
| 3534 |
|
| 3535 |
#ifdef DEBUG_HIGH |
| 3536 |
int pcount = 0; |
| 3537 |
#else |
| 3538 |
#ifdef DEBUG_LOW |
| 3539 |
int pcount = 0; |
| 3540 |
#endif |
| 3541 |
#endif |
| 3542 |
|
| 3543 |
bool ismitm = false; |
| 3544 |
|
| 3545 |
struct timeval thestart; |
| 3546 |
gettimeofday(&thestart, NULL); |
| 3547 |
|
| 3548 |
peerconn.setTimeout(o.net.pcon_timeout); |
| 3549 |
|
| 3550 |
std::string clientip(ip.toCharArray()); // hold the ICAP clients ip |
| 3551 |
|
| 3552 |
if (clienthost) delete clienthost; |
| 3553 |
|
| 3554 |
clienthost = NULL; // and the hostname, if available |
| 3555 |
matchedip = false; |
| 3556 |
|
| 3557 |
//try { |
| 3558 |
//int rc; |
| 3559 |
//bool authed = false; |
| 3560 |
bool firsttime = true; |
| 3561 |
//bool isbanneduser = true; |
| 3562 |
|
| 3563 |
AuthPlugin *auth_plugin = NULL; |
| 3564 |
|
| 3565 |
// RFC states that connections are persistent |
| 3566 |
bool persistPeer = true; |
| 3567 |
|
| 3568 |
// |
| 3569 |
// End of set-up section |
| 3570 |
|
| 3571 |
// Start of main loop |
| 3572 |
|
| 3573 |
// |
| 3574 |
|
| 3575 |
// maintain a persistent connection |
| 3576 |
while ((firsttime || persistPeer) && !ttg) |
| 3577 |
{ |
| 3578 |
ICAPHeader icaphead; |
| 3579 |
ldl = o.currentLists(); |
| 3580 |
icaphead.ISTag = ldl->ISTag(); |
| 3581 |
|
| 3582 |
NaughtyFilter checkme(icaphead.HTTPrequest, icaphead.HTTPresponse, SBauth); |
| 3583 |
checkme.listen_port = peerconn.getPort(); |
| 3584 |
DataBuffer docbody; |
| 3585 |
docbody.setTimeout(o.net.exchange_timeout); |
| 3586 |
docbody.setICAP(true); |
| 3587 |
FDTunnel fdt; |
| 3588 |
String wline = ""; |
| 3589 |
if (firsttime) { |
| 3590 |
// reset flags & objects next time round the loop |
| 3591 |
firsttime = false; |
| 3592 |
gettimeofday(&thestart, NULL); |
| 3593 |
checkme.thestart = thestart; |
| 3594 |
} |
| 3595 |
|
| 3596 |
{ |
| 3597 |
// another round... |
| 3598 |
DEBUG_icap(" ICAP -persisting (count ", ++pcount, ")", " Client IP: ", clientip); |
| 3599 |
|
| 3600 |
icaphead.reset(); |
| 3601 |
if (!icaphead.in(&peerconn, true)) { |
| 3602 |
if (peerconn.isTimedout()) { |
| 3603 |
DEBUG_icap( " -ICAP Persistent connection timed out"); |
| 3604 |
//send error response |
| 3605 |
wline = "ICAP/1.0 408 Request timeout\r\n"; |
| 3606 |
wline += "Service: "; |
| 3607 |
wline += PACKAGE_STRING; |
| 3608 |
wline += "\r\n"; |
| 3609 |
wline += "Encapsulated: null-body=0\r\n"; |
| 3610 |
wline += "\r\n"; |
| 3611 |
peerconn.writeString(wline.toCharArray()); |
| 3612 |
break; |
| 3613 |
} else { |
| 3614 |
|
| 3615 |
DEBUG_icap( " -ICAP Persistent connection closed"); |
| 3616 |
// TODO: send error reply if needed |
| 3617 |
break; |
| 3618 |
} |
| 3619 |
} |
| 3620 |
++dystat->reqs; |
| 3621 |
|
| 3622 |
ip = icaphead.clientip; |
| 3623 |
checkme.clientip = ip; |
| 3624 |
|
| 3625 |
// we will actually need to do *lots* of resetting of flags etc. here for pconns to work |
| 3626 |
gettimeofday(&thestart, NULL); |
| 3627 |
checkme.thestart = thestart; |
| 3628 |
|
| 3629 |
//authed = false; |
| 3630 |
//isbanneduser = false; |
| 3631 |
|
| 3632 |
//requestscanners.clear(); |
| 3633 |
//responsescanners.clear(); |
| 3634 |
|
| 3635 |
matchedip = false; |
| 3636 |
urlparams.clear(); |
| 3637 |
postparts.clear(); |
| 3638 |
checkme.mimetype = "-"; |
| 3639 |
//exceptionreason = ""; |
| 3640 |
//exceptioncat = ""; |
| 3641 |
//room = ""; // CHECK THIS - surely room is persistant????? |
| 3642 |
|
| 3643 |
// reset docheader & docbody |
| 3644 |
// headers *should* take care of themselves on the next in() |
| 3645 |
// actually not entirely true for docheader - we may read |
| 3646 |
// certain properties of it (in denyAccess) before we've |
| 3647 |
// actually performed the next in(), so make sure we do a full |
| 3648 |
// reset now. |
| 3649 |
docbody.reset(); |
| 3650 |
docbody.setICAP(true); |
| 3651 |
peerconn.resetChunk(); |
| 3652 |
} |
| 3653 |
DEBUG_icap("service options enabled : ", wline, |
| 3654 |
" icaphead.service_reqmod: ", icaphead.service_reqmod, |
| 3655 |
" icaphead.service_resmod: ", icaphead.service_resmod, |
| 3656 |
" icaphead.service_options: ", |
| 3657 |
" icaphead.icap_reqmod_service: ", icaphead.icap_reqmod_service, |
| 3658 |
" icaphead.icap_resmod_service: ", icaphead.icap_resmod_service, |
| 3659 |
" icaphead.icap_reqmod_service: ", icaphead.icap_reqmod_service); |
| 3660 |
|
| 3661 |
// Check service option REQMOD, RESMOD, OPTIONS and call apropreate function(s) |
| 3662 |
// |
| 3663 |
if (icaphead.service_reqmod && icaphead.icap_reqmod_service) { |
| 3664 |
DEBUG_icap( "Icap reqmod check "); |
| 3665 |
if (handleICAPreqmod(peerconn,ip, checkme, icaphead, auth_plugin) == 0){ |
| 3666 |
continue; |
| 3667 |
}else{ |
| 3668 |
break; |
| 3669 |
} |
| 3670 |
|
| 3671 |
} else if (icaphead.service_resmod && icaphead.icap_resmod_service) { |
| 3672 |
DEBUG_icap("Icap resmod check "); |
| 3673 |
if (handleICAPresmod(peerconn,ip, checkme, icaphead, docbody) == 0) |
| 3674 |
continue; |
| 3675 |
else |
| 3676 |
break; |
| 3677 |
} else if (icaphead.service_options && icaphead.icap_reqmod_service) { |
| 3678 |
// respond with option response |
| 3679 |
wline = "ICAP/1.0 200 OK\r\n"; |
| 3680 |
wline += "Methods: REQMOD\r\n"; |
| 3681 |
wline += "Service: "; |
| 3682 |
wline += PACKAGE_STRING; |
| 3683 |
wline += "\r\n"; |
| 3684 |
wline += "ISTag: \""; |
| 3685 |
wline += ldl->ISTag(); |
| 3686 |
wline += "\"\r\n"; |
| 3687 |
wline += "Encapsulated: null-body=0\r\n"; |
| 3688 |
wline += "Allow: 204\r\n"; |
| 3689 |
// wline += "Preview: 0\r\n"; |
| 3690 |
wline += "\r\n"; |
| 3691 |
peerconn.writeString(wline.toCharArray()); |
| 3692 |
DEBUG_icap("respmod service options response : ", wline); |
| 3693 |
|
| 3694 |
} else if (icaphead.service_options && icaphead.icap_resmod_service) { |
| 3695 |
// respond with option response |
| 3696 |
wline = "ICAP/1.0 200 OK\r\n"; |
| 3697 |
wline += "Methods: RESPMOD\r\n"; |
| 3698 |
wline += "Service: "; |
| 3699 |
wline += PACKAGE_STRING; |
| 3700 |
wline += "\r\n"; |
| 3701 |
wline += "ISTag:"; |
| 3702 |
wline += ldl->ISTag(); |
| 3703 |
wline += "\r\n"; |
| 3704 |
wline += "Encapsulated: null-body=0\r\n"; |
| 3705 |
wline += "Allow: 204\r\n"; |
| 3706 |
// wline += "Preview: 0\r\n"; |
| 3707 |
wline += "\r\n"; |
| 3708 |
peerconn.writeString(wline.toCharArray()); |
| 3709 |
DEBUG_icap("respmod service options response : ", wline); |
| 3710 |
|
| 3711 |
} else if ((icaphead.service_reqmod && !icaphead.icap_reqmod_service) || |
| 3712 |
(icaphead.service_resmod && !icaphead.icap_resmod_service)) { |
| 3713 |
wline = "ICAP/1.0 405 Method not allowed for service\r\n"; |
| 3714 |
wline += "Service: "; |
| 3715 |
wline += PACKAGE_STRING; |
| 3716 |
wline += "\r\n"; |
| 3717 |
wline += "Encapsulated: null-body=0\r\n"; |
| 3718 |
wline += "\r\n"; |
| 3719 |
peerconn.writeString(wline.toCharArray()); |
| 3720 |
DEBUG_icap("ICAP/1.0 405 Method not allowed for service ", wline); |
| 3721 |
|
| 3722 |
} else { |
| 3723 |
//send error response |
| 3724 |
wline = "ICAP/1.0 400 Bad request\r\n"; |
| 3725 |
wline += "Service: "; |
| 3726 |
wline += PACKAGE_STRING; |
| 3727 |
wline += "\r\n"; |
| 3728 |
peerconn.writeString(wline.toCharArray()); |
| 3729 |
DEBUG_icap("ICAP/1.0 400 Bad request : ", wline); |
| 3730 |
} |
| 3731 |
} |
| 3732 |
// } //catch (std::exception & e) |
| 3733 |
|
| 3734 |
if (!ismitm) |
| 3735 |
try { |
| 3736 |
DEBUG_icap("ICAP -Attempting graceful connection close"); |
| 3737 |
|
| 3738 |
int fd = peerconn.getFD(); |
| 3739 |
if (fd > -1) { |
| 3740 |
if (shutdown(fd, SHUT_WR) == 0) { |
| 3741 |
char buff[2]; |
| 3742 |
peerconn.readFromSocket(buff, 2, 0, 5000); |
| 3743 |
}; |
| 3744 |
}; |
| 3745 |
|
| 3746 |
// close connection to the client |
| 3747 |
peerconn.close(); |
| 3748 |
} catch (std::exception &e) { |
| 3749 |
DEBUG_icap(" -ICAP connection handler caught an exception on connection closedown: ", e.what()); |
| 3750 |
// close connection to the client |
| 3751 |
peerconn.close(); |
| 3752 |
} |
| 3753 |
|
| 3754 |
return 0; |
| 3755 |
} |
| 3756 |
|
| 3757 |
|
| 3758 |
int ConnectionHandler::handleICAPreqmod(Socket &peerconn, String &ip, NaughtyFilter &checkme, ICAPHeader &icaphead, |
| 3759 |
AuthPlugin *auth_plugin) { |
| 3760 |
bool authed = false; |
| 3761 |
String clientip = ip; |
| 3762 |
// do all of this normalisation etc just the once at the start. |
| 3763 |
checkme.setURL(); |
| 3764 |
String res_hdr, res_body, req_hdr, req_body; |
| 3765 |
|
| 3766 |
// checks for bad URLs to prevent security holes/domain obfuscation. |
| 3767 |
if (icaphead.HTTPrequest.malformedURL(checkme.url)) { |
| 3768 |
// The requested URL is malformed. |
| 3769 |
gen_error_mess(peerconn, checkme, res_hdr, res_body, 200, 0, "400 Bad Request"); |
| 3770 |
checkme.isdone = true; |
| 3771 |
icaphead.errorResponse(peerconn, res_hdr, res_body); |
| 3772 |
if (icaphead.req_body_flag) { |
| 3773 |
peerconn.drainChunk(peerconn.getTimeout()); // drains body |
| 3774 |
} |
| 3775 |
return 0; |
| 3776 |
} |
| 3777 |
|
| 3778 |
|
| 3779 |
// |
| 3780 |
// |
| 3781 |
// Start of Authentication Checks |
| 3782 |
// |
| 3783 |
// |
| 3784 |
// don't have credentials for this connection yet? get some! |
| 3785 |
overide_persist = false; |
| 3786 |
filtergroup = o.filter.default_icap_fg; |
| 3787 |
DEBUG_icap("filtergroup set to ICAP default ", filtergroup); |
| 3788 |
clientuser = icaphead.username; |
| 3789 |
|
| 3790 |
//if(o.log_requests) { |
| 3791 |
if (e2logger.isEnabled(LoggerSource::requestlog)) { |
| 3792 |
std::string fnt = "REQMOD"; |
| 3793 |
doRQLog(clientuser, clientip, checkme, fnt); |
| 3794 |
} |
| 3795 |
|
| 3796 |
int rc = E2AUTH_NOUSER; |
| 3797 |
if (!(clientuser.empty() || clientuser == "-")) { |
| 3798 |
SBauth.user_name = clientuser; |
| 3799 |
SBauth.user_source = "icaph"; |
| 3800 |
rc = determineGroup(clientuser, filtergroup, ldl->StoryA, checkme, ENT_STORYA_AUTH_ICAP); |
| 3801 |
DEBUG_icap("filter group set from filtergroupslist: ", clientuser, " ICAP -filtergroup: ", filtergroup); |
| 3802 |
} else { |
| 3803 |
oldclientuser = "user not known"; |
| 3804 |
} |
| 3805 |
|
| 3806 |
if (rc != E2AUTH_OK) |
| 3807 |
{ |
| 3808 |
DEBUG_icap("filter group NOT set from filtergroupslist: trying auth plugins"); |
| 3809 |
persistent_authed = false; |
| 3810 |
if (!doAuth(checkme.auth_result, authed, filtergroup, auth_plugin, peerconn, icaphead.HTTPrequest, checkme, true, |
| 3811 |
true)) { |
| 3812 |
DEBUG_icap("error return from doAuth"); |
| 3813 |
//break; // TODO Error return???? |
| 3814 |
} else { |
| 3815 |
DEBUG_icap("OK return from doAuth"); |
| 3816 |
} |
| 3817 |
if (!(icaphead.username.empty() || icaphead.username == "-")) { |
| 3818 |
checkme.user = icaphead.username; // restore username if we had one from icap header |
| 3819 |
clientuser = icaphead.username; // restore username if we had one from icap header |
| 3820 |
} |
| 3821 |
} |
| 3822 |
|
| 3823 |
authed = true; |
| 3824 |
checkme.filtergroup = filtergroup; |
| 3825 |
|
| 3826 |
//int unrealgroup = filtergroup+1; |
| 3827 |
DEBUG_icap("-username: ", clientuser, " ICAP -filtergroup: ", filtergroup); |
| 3828 |
|
| 3829 |
// |
| 3830 |
// |
| 3831 |
// End of Authentication Checking |
| 3832 |
// |
| 3833 |
// |
| 3834 |
|
| 3835 |
|
| 3836 |
// |
| 3837 |
// |
| 3838 |
// Now check if user or machine is banned and room-based checking |
| 3839 |
// |
| 3840 |
// |
| 3841 |
|
| 3842 |
// is this user banned? |
| 3843 |
bool isbanneduser = false; |
| 3844 |
checkme.clientip = clientip; |
| 3845 |
|
| 3846 |
// Look up reverse DNS name of client if needed |
| 3847 |
if (o.conn.reverse_client_ip_lookups) { |
| 3848 |
getClientFromIP(clientip.c_str(),checkme.clienthost); |
| 3849 |
// std::unique_ptr<std::deque<String> > hostnames; |
| 3850 |
// hostnames.reset(ipToHostname(clientip.c_str())); |
| 3851 |
// checkme.clienthost = std::string(hostnames->front().toCharArray()); |
| 3852 |
} |
| 3853 |
|
| 3854 |
//CALL SB pre-authcheck |
| 3855 |
ldl->StoryA.runFunctEntry(ENT_STORYA_PRE_AUTH_ICAP, checkme); |
| 3856 |
DEBUG_debug("After StoryA icap-pre-authcheck", checkme.isexception, " mess_no ", checkme.message_no); |
| 3857 |
checkme.isItNaughty = checkme.isBlocked; |
| 3858 |
bool isbannedip = checkme.isBlocked; |
| 3859 |
//bool part_banned; |
| 3860 |
if (isbannedip) { |
| 3861 |
// matchedip = clienthost == NULL; |
| 3862 |
} else { |
| 3863 |
#ifdef NOTDEF // TODO does this need restoring??? |
| 3864 |
if (ldl->inRoom(clientip, room, clienthost, &isbannedip, &part_banned, &checkme.isexception, |
| 3865 |
checkme.urld)) { |
| 3866 |
DEBUG_debug("ICAP isbannedip = ", isbannedip, "ispart_banned = ", part_banned, " isexception = ", checkme.isexception); |
| 3867 |
if (isbannedip) { |
| 3868 |
// matchedip = clienthost == NULL; |
| 3869 |
checkme.isBlocked = checkme.isItNaughty = true; |
| 3870 |
} |
| 3871 |
if (checkme.isexception) { |
| 3872 |
// do reason codes etc |
| 3873 |
checkme.exceptionreason = o.language_list.getTranslation(630); |
| 3874 |
checkme.exceptionreason.append(room); |
| 3875 |
checkme.exceptionreason.append(o.language_list.getTranslation(631)); |
| 3876 |
checkme.message_no = 632; |
| 3877 |
} |
| 3878 |
} |
| 3879 |
#endif |
| 3880 |
} |
| 3881 |
|
| 3882 |
// |
| 3883 |
// Start of by pass |
| 3884 |
if (!checkme.is_ssl) { |
| 3885 |
|
| 3886 |
if (checkByPass(checkme, ldl, icaphead.HTTPrequest, peerconn, peerconn, clientip) |
| 3887 |
&& sendScanFile(peerconn, checkme, true, &icaphead)) { |
| 3888 |
return 0; // returns only when Scanfile sent. Sets checkme.isbypass if it is a bypass. |
| 3889 |
} |
| 3890 |
} |
| 3891 |
// |
| 3892 |
// End of scan by pass |
| 3893 |
// |
| 3894 |
|
| 3895 |
bool done = false; |
| 3896 |
|
| 3897 |
// |
| 3898 |
// Start of exception checking |
| 3899 |
// |
| 3900 |
// being a banned user/IP overrides the fact that a site may be in the exception lists |
| 3901 |
// needn't check these lists in bypass modes |
| 3902 |
if (!(isbanneduser || isbannedip || checkme.isexception)) { |
| 3903 |
// Main checking is now done in Storyboard function(s) |
| 3904 |
ldl->fg[filtergroup]->StoryB.runFunctEntry(ENT_STORYB_ICAP_REQMOD, checkme); |
| 3905 |
DEBUG_debug("After StoryB checkreqmod", checkme.isexception, " mess_no ", checkme.message_no, |
| 3906 |
" allow_204 : ", icaphead.allow_204); |
| 3907 |
|
| 3908 |
if (ldl->fg[filtergroup]->reporting_level != -1){ |
| 3909 |
checkme.isItNaughty = checkme.isBlocked; |
| 3910 |
} else { |
| 3911 |
checkme.isItNaughty = false; |
| 3912 |
checkme.isBlocked = false; |
| 3913 |
} |
| 3914 |
} |
| 3915 |
|
| 3916 |
if (checkme.isbypass && !(checkme.iscookiebypass || checkme.isvirusbypass)) { |
| 3917 |
DEBUG_debug("Setting GBYPASS cookie; bypasstimestamp = ", checkme.bypasstimestamp); |
| 3918 |
String ud(checkme.urldomain); |
| 3919 |
if (ud.startsWith("www.")) { |
| 3920 |
ud = ud.after("www."); |
| 3921 |
} |
| 3922 |
|
| 3923 |
String outhead = "HTTP/1.1 302 Redirect\r\n"; |
| 3924 |
outhead += "Set-Cookie: GBYPASS="; |
| 3925 |
outhead += icaphead.HTTPrequest.hashedCookie(&ud, ldl->fg[filtergroup]->cookie_magic.c_str(), &clientip, |
| 3926 |
checkme.bypasstimestamp, clientuser).toCharArray(); |
| 3927 |
// outhead += hashedCookie(&ud, ldl->fg[filtergroup]->cookie_magic.c_str(), &clientip, |
| 3928 |
// checkme.bypasstimestamp).toCharArray(); |
| 3929 |
outhead += "; path=/; domain=."; |
| 3930 |
outhead += ud; |
| 3931 |
outhead += "\r\n"; |
| 3932 |
outhead += "Location: "; |
| 3933 |
outhead += icaphead.HTTPrequest.getUrl(true); |
| 3934 |
outhead += "\r\n"; |
| 3935 |
outhead += "\r\n"; |
| 3936 |
icaphead.out_res_header = outhead; |
| 3937 |
icaphead.out_res_hdr_flag = true; |
| 3938 |
icaphead.respond(peerconn); |
| 3939 |
return 0; |
| 3940 |
} |
| 3941 |
|
| 3942 |
// TODO add logic for 204 response etc. |
| 3943 |
if (checkme.isexception || checkme.logcategory) { |
| 3944 |
std::string code; |
| 3945 |
if (checkme.isvirusbypass) |
| 3946 |
code = "V"; |
| 3947 |
else if (checkme.isbypass) |
| 3948 |
code = "Y"; |
| 3949 |
else if (checkme.logcategory) |
| 3950 |
code = "L"; |
| 3951 |
else |
| 3952 |
code = "E"; |
| 3953 |
|
| 3954 |
icaphead.set_icap_com(clientuser,code, filtergroup, checkme.message_no, checkme.log_message_no, |
| 3955 |
checkme.whatIsNaughtyLog); |
| 3956 |
if (icaphead.allow_204) { |
| 3957 |
icaphead.respond(peerconn, "204 No Content", false, false); |
| 3958 |
if (icaphead.req_body_flag) { |
| 3959 |
peerconn.drainChunk(peerconn.getTimeout()); // drains any body |
| 3960 |
} |
| 3961 |
done = true; |
| 3962 |
} else { |
| 3963 |
// pipe through headers and body |
| 3964 |
icaphead.respond(peerconn, "200 OK", true); |
| 3965 |
if (icaphead.req_body_flag) { |
| 3966 |
peerconn.loopChunk(peerconn.getTimeout()); // echos any body |
| 3967 |
} |
| 3968 |
done = true; |
| 3969 |
} |
| 3970 |
} |
| 3971 |
|
| 3972 |
|
| 3973 |
//check for redirect |
| 3974 |
// URL regexp search and edirect |
| 3975 |
if (checkme.urlredirect) { |
| 3976 |
checkme.url = icaphead.HTTPrequest.redirecturl(); |
| 3977 |
String writestring("HTTP/1.1 302 Redirect\r\nLocation: "); |
| 3978 |
writestring += checkme.url; |
| 3979 |
writestring += "\r\n\r\n"; |
| 3980 |
res_hdr = writestring; |
| 3981 |
icaphead.errorResponse(peerconn, res_hdr, res_body); |
| 3982 |
if (icaphead.req_body_flag) { |
| 3983 |
peerconn.drainChunk(peerconn.getTimeout()); // drains any body |
| 3984 |
} |
| 3985 |
done = true; |
| 3986 |
} |
| 3987 |
|
| 3988 |
//if is a search - content check search terms |
| 3989 |
if (!done && !checkme.isdone && checkme.isGrey && checkme.isSearch) |
| 3990 |
check_search_terms(checkme); // will set isItNaughty if needed |
| 3991 |
|
| 3992 |
|
| 3993 |
// check for CONNECT redirect |
| 3994 |
if ((icaphead.HTTPrequest.requestType() == "CONNECT") && checkme.urlmodified) { |
| 3995 |
// DEBUG_debug("is CONNECT logurl:", checkme.logurl, " conn site:", checkme.connect_site, " fullurl:", checkme.baseurl, " urldomain:", checkme.urldomain); |
| 3996 |
if (checkme.connect_site != checkme.urldomain) { |
| 3997 |
icaphead.HTTPrequest.setConnect(checkme.connect_site); |
| 3998 |
// DEBUG_debug("after setURL logurl:", checkme.logurl, " conn site:", checkme.connect_site, " fullurl:", checkme.baseurl, " urldomain:", checkme.urldomain); |
| 3999 |
} |
| 4000 |
} |
| 4001 |
// TODO V5 call POST scanning code New NaughtyFilter function???? |
| 4002 |
|
| 4003 |
if (!done && checkme.isItNaughty) { |
| 4004 |
if (genDenyAccess(peerconn, res_hdr, res_body, &icaphead.HTTPrequest, &icaphead.HTTPresponse, |
| 4005 |
&checkme.logurl, &checkme, &clientuser, &clientip, |
| 4006 |
filtergroup, checkme.ispostblock, checkme.headersent, checkme.wasinfected, |
| 4007 |
checkme.scanerror)) { |
| 4008 |
icaphead.errorResponse(peerconn, res_hdr, res_body); |
| 4009 |
if (icaphead.req_body_flag) { |
| 4010 |
peerconn.drainChunk(peerconn.getTimeout()); // drains any body |
| 4011 |
} |
| 4012 |
done = true; |
| 4013 |
DEBUG_debug("ICAP Naughty"); |
| 4014 |
// break loop "// maintain a persistent connection" |
| 4015 |
// return 1; |
| 4016 |
}; |
| 4017 |
} |
| 4018 |
|
| 4019 |
|
| 4020 |
if (!done) { |
| 4021 |
icaphead.set_icap_com(clientuser, "G", filtergroup, checkme.message_no, checkme.log_message_no, |
| 4022 |
checkme.whatIsNaughtyLog); |
| 4023 |
icaphead.respond(peerconn, "200 OK", true); |
| 4024 |
if (icaphead.req_body_flag) { |
| 4025 |
peerconn.loopChunk(peerconn.getTimeout()); // echoes any body |
| 4026 |
} |
| 4027 |
} |
| 4028 |
//Log |
| 4029 |
doLog(clientuser, clientip, checkme); |
| 4030 |
|
| 4031 |
return 0; |
| 4032 |
} |
| 4033 |
|
| 4034 |
int ConnectionHandler::handleICAPresmod(Socket &peerconn, String &ip, NaughtyFilter &checkme, ICAPHeader &icaphead, |
| 4035 |
DataBuffer &docbody) { |
| 4036 |
|
| 4037 |
//bool authed = false; |
| 4038 |
bool persistPeer = true; |
| 4039 |
bool done = false; |
| 4040 |
String clientip = ip; |
| 4041 |
String res_hdr, res_body; |
| 4042 |
std::deque<CSPlugin *> responsescanners; |
| 4043 |
|
| 4044 |
// do all of this normalisation etc just the once at the start. |
| 4045 |
checkme.setURL(); |
| 4046 |
|
| 4047 |
overide_persist = false; |
| 4048 |
if (icaphead.icap_com.filtergroup < 0) //i.e. no X-ICAP-E2G |
| 4049 |
{ |
| 4050 |
String wline = "ICAP/1.0 418 Bad composition - X-ICAP-E2G header not present\r\n"; |
| 4051 |
wline += "Service: "; |
| 4052 |
wline += PACKAGE_STRING; |
| 4053 |
wline += "\r\n"; |
| 4054 |
wline += "Encapsulated: null-body=0\r\n"; |
| 4055 |
wline += "\r\n"; |
| 4056 |
peerconn.writeString(wline.toCharArray()); |
| 4057 |
DEBUG_icap(" ICAP Error: ", wline); |
| 4058 |
return 1; |
| 4059 |
} |
| 4060 |
|
| 4061 |
filtergroup = icaphead.icap_com.filtergroup; |
| 4062 |
checkme.filtergroup = icaphead.icap_com.filtergroup; |
| 4063 |
clientuser = icaphead.icap_com.user; |
| 4064 |
docbody.set_current_config(ldl->fg[filtergroup]); |
| 4065 |
|
| 4066 |
if (icaphead.icap_com.EBG == "E") { // exception |
| 4067 |
checkme.isexception = true; |
| 4068 |
checkme.message_no = icaphead.icap_com.mess_no; |
| 4069 |
checkme.log_message_no = icaphead.icap_com.log_mess_no; |
| 4070 |
checkme.whatIsNaughtyLog = icaphead.icap_com.mess_string; |
| 4071 |
} else if (icaphead.icap_com.EBG == "G") // grey - content check |
| 4072 |
checkme.isGrey = true; |
| 4073 |
else if (icaphead.icap_com.EBG == "Y") { // ordinary bypass |
| 4074 |
checkme.isbypass = true; |
| 4075 |
checkme.isexception = true; |
| 4076 |
checkme.message_no = icaphead.icap_com.mess_no; |
| 4077 |
checkme.log_message_no = icaphead.icap_com.log_mess_no; |
| 4078 |
checkme.whatIsNaughtyLog = icaphead.icap_com.mess_string; |
| 4079 |
} else if (icaphead.icap_com.EBG == "V") { // virus bypass |
| 4080 |
checkme.isvirusbypass = true; |
| 4081 |
checkme.isbypass = true; |
| 4082 |
checkme.isexception = true; |
| 4083 |
checkme.message_no = icaphead.icap_com.mess_no; |
| 4084 |
checkme.log_message_no = icaphead.icap_com.log_mess_no; |
| 4085 |
checkme.whatIsNaughtyLog = icaphead.icap_com.mess_string; |
| 4086 |
} else if (icaphead.icap_com.EBG == "L") { // only log - do not block |
| 4087 |
checkme.isexception = true; |
| 4088 |
checkme.logcategory = true; |
| 4089 |
checkme.message_no = icaphead.icap_com.mess_no; |
| 4090 |
checkme.log_message_no = icaphead.icap_com.log_mess_no; |
| 4091 |
checkme.whatIsNaughtyLog = icaphead.icap_com.mess_string; |
| 4092 |
} |
| 4093 |
|
| 4094 |
//int unrealfiltergroup = filtergroup + 1; |
| 4095 |
DEBUG_icap("ICAP Respmod enabled - username: ", clientuser, |
| 4096 |
" -filtergroup: ", filtergroup, |
| 4097 |
" icaphead.icap_com.EBG: ", icaphead.icap_com.EBG, |
| 4098 |
" icaphead.res_body_flag: ", icaphead.res_body_flag); |
| 4099 |
|
| 4100 |
checkme.clientip = ip; |
| 4101 |
checkme.filtergroup = filtergroup; |
| 4102 |
//if(o.log_requests) { |
| 4103 |
if (e2logger.isEnabled(LoggerSource::requestlog)) { |
| 4104 |
std::string fnt = "RESPMOD"; |
| 4105 |
doRQLog(clientuser, clientip, checkme, fnt); |
| 4106 |
} |
| 4107 |
// Look up reverse DNS name of client if needed |
| 4108 |
if (o.conn.reverse_client_ip_lookups) { |
| 4109 |
getClientFromIP(clientip.c_str(), checkme.clienthost); |
| 4110 |
} |
| 4111 |
|
| 4112 |
//bool part_banned; |
| 4113 |
|
| 4114 |
// virus checkichurchillng candidate? |
| 4115 |
// checkme.noviruscheck defaults to true |
| 4116 |
DEBUG_icap("Virus scan checkme.isexception: ", checkme.isexception, |
| 4117 |
" checkme.noviruscheck: ", checkme.noviruscheck, |
| 4118 |
" content_scan_exceptions: ", ldl->fg[filtergroup]->content_scan_exceptions, |
| 4119 |
" checkme.isBlocked: ", checkme.isBlocked, |
| 4120 |
" disable_content_scan: ", ldl->fg[filtergroup]->disable_content_scan, |
| 4121 |
" csplugins: ", o.plugins.csplugins.size() ); |
| 4122 |
|
| 4123 |
if (icaphead.res_body_flag // can only scan if body present |
| 4124 |
&& !(checkme.isBlocked) // or not already blocked |
| 4125 |
&& (o.plugins.csplugins.size() > 0) // and we have scan plugins |
| 4126 |
&& !ldl->fg[filtergroup]->disable_content_scan // and is not disabled |
| 4127 |
&& !(checkme.isexception && !ldl->fg[filtergroup]->content_scan_exceptions) |
| 4128 |
&& !checkme.isvirusbypass // and is not virus bypass |
| 4129 |
// and not exception unless scan exceptions enable |
| 4130 |
) { |
| 4131 |
checkme.noviruscheck = false; // note this may be reset by Storyboard to enable exceptions |
| 4132 |
} |
| 4133 |
|
| 4134 |
// |
| 4135 |
// being a banned user/IP overrides the fact that a site may be in the exception lists |
| 4136 |
// needn't check these lists in bypass modes |
| 4137 |
if (!(checkme.isexception) || !checkme.noviruscheck) { |
| 4138 |
// Main checking is done in Storyboard function(s) |
| 4139 |
ldl->fg[filtergroup]->StoryB.runFunctEntry(ENT_STORYB_ICAP_RESMOD,checkme); |
| 4140 |
DEBUG_icap("After StoryB icapcheckresmod", checkme.isexception, |
| 4141 |
" mess_no ", checkme.message_no, |
| 4142 |
" checkme.noviruscheck: ", checkme.noviruscheck, |
| 4143 |
" content_scan_exceptions: ", ldl->fg[filtergroup]->content_scan_exceptions); |
| 4144 |
|
| 4145 |
if (ldl->fg[filtergroup]->reporting_level != -1){ |
| 4146 |
checkme.isItNaughty = checkme.isBlocked; |
| 4147 |
} else { |
| 4148 |
checkme.isItNaughty = false; |
| 4149 |
checkme.isBlocked = false; |
| 4150 |
} |
| 4151 |
} |
| 4152 |
|
| 4153 |
if (checkme.isexception && !checkme.noviruscheck && !ldl->fg[filtergroup]->content_scan_exceptions) |
| 4154 |
checkme.noviruscheck = true; |
| 4155 |
|
| 4156 |
if (ldl->fg[filtergroup]->content_scan_exceptions && checkme.isexception) |
| 4157 |
checkme.noviruscheck = false; |
| 4158 |
|
| 4159 |
if ((checkme.isexception && checkme.noviruscheck)|| !icaphead.res_body_flag) { |
| 4160 |
if (icaphead.allow_204) { |
| 4161 |
icaphead.respond(peerconn, "204 No Content", false, false); |
| 4162 |
if (icaphead.res_body_flag) { |
| 4163 |
peerconn.drainChunk(peerconn.getTimeout()); // drains any body |
| 4164 |
} |
| 4165 |
} else { |
| 4166 |
// pipe through headers and body |
| 4167 |
icaphead.respond(peerconn, "200 OK", true); |
| 4168 |
if (icaphead.res_body_flag) { |
| 4169 |
peerconn.loopChunk(peerconn.getTimeout()); // echos any body |
| 4170 |
} |
| 4171 |
} |
| 4172 |
done = true; |
| 4173 |
} |
| 4174 |
|
| 4175 |
// should now only be left with grey which has content body |
| 4176 |
|
| 4177 |
//- if grey content check |
| 4178 |
// can't do content filtering on HEAD or redirections (no content) |
| 4179 |
// actually, redirections CAN have content |
| 4180 |
|
| 4181 |
if (!done && !checkme.isItNaughty) { |
| 4182 |
if(!checkme.noviruscheck) |
| 4183 |
{ |
| 4184 |
for (std::deque<Plugin *>::iterator i = o.plugins.csplugins_begin; i != o.plugins.csplugins_end; ++i) { |
| 4185 |
int csrc = ((CSPlugin *)(*i))->willScanRequest(checkme.url, clientuser.c_str(), ldl->fg[filtergroup], clientip.c_str(), false, false, checkme.isexception, checkme.isbypass); |
| 4186 |
if (csrc > 0) |
| 4187 |
responsescanners.push_back((CSPlugin *)(*i)); |
| 4188 |
else if (csrc < 0) |
| 4189 |
E2LOGGER_error("willScanRequest returned error: ", csrc); |
| 4190 |
} |
| 4191 |
} |
| 4192 |
check_content(checkme, docbody,peerconn, peerconn,responsescanners); |
| 4193 |
} |
| 4194 |
|
| 4195 |
//send response header to client |
| 4196 |
if (!done && !checkme.isItNaughty) { |
| 4197 |
icaphead.respond(peerconn, "200 OK", true); |
| 4198 |
if(checkme.waschecked) { |
| 4199 |
if (!docbody.out(&peerconn)) |
| 4200 |
checkme.pausedtoobig = false; |
| 4201 |
if (checkme.pausedtoobig) |
| 4202 |
checkme.tunnel_rest = true; |
| 4203 |
} |
| 4204 |
if (checkme.tunnel_rest){ |
| 4205 |
peerconn.loopChunk(peerconn.getTimeout()); // echos any body |
| 4206 |
done = true; |
| 4207 |
} |
| 4208 |
} |
| 4209 |
|
| 4210 |
if(checkme.isItNaughty) { |
| 4211 |
if(genDenyAccess(peerconn,res_hdr, res_body, &icaphead.HTTPrequest, &icaphead.HTTPresponse, &checkme.logurl, &checkme, &clientuser, &ip, |
| 4212 |
filtergroup, checkme.ispostblock,checkme.headersent, checkme.wasinfected, checkme.scanerror)) |
| 4213 |
{ |
| 4214 |
icaphead.errorResponse(peerconn, res_hdr, res_body); |
| 4215 |
if (icaphead.res_body_flag) { |
| 4216 |
peerconn.drainChunk(peerconn.getTimeout()); // drains any body |
| 4217 |
} |
| 4218 |
done = true; |
| 4219 |
} |
| 4220 |
persistPeer = false; |
| 4221 |
} |
| 4222 |
|
| 4223 |
//Log |
| 4224 |
if (checkme.isItNaughty) { // don't log requests to the web server & and normal response |
| 4225 |
checkme.whatIsNaughtyLog = "ICAP Response filtering: "; |
| 4226 |
checkme.whatIsNaughtyLog += checkme.whatIsNaughty; |
| 4227 |
} else { |
| 4228 |
checkme.nolog = true; |
| 4229 |
} |
| 4230 |
|
| 4231 |
doLog(clientuser, clientip, checkme); |
| 4232 |
|
| 4233 |
if (persistPeer) |
| 4234 |
return 0; |
| 4235 |
else |
| 4236 |
return 1; |
| 4237 |
} |
| 4238 |
|
| 4239 |
// determine what filter group the given username is in |
| 4240 |
// return -1 when user not found |
| 4241 |
int ConnectionHandler::determineGroup(std::string &user, int &fg, StoryBoard &story, NaughtyFilter &cm, int story_entry) { |
| 4242 |
if (user.length() < 1 || user == "-") { |
| 4243 |
return E2AUTH_NOMATCH; |
| 4244 |
} |
| 4245 |
cm.user = user; |
| 4246 |
if (!story.runFunctEntry(story_entry, cm)) { |
| 4247 |
DEBUG_debug("User not in filter groups list for: icap "); |
| 4248 |
return E2AUTH_NOGROUP; |
| 4249 |
} |
| 4250 |
|
| 4251 |
DEBUG_debug("Group found for: ", user.c_str(), " in icap "); |
| 4252 |
fg = cm.filtergroup; |
| 4253 |
return E2AUTH_OK; |
| 4254 |
} |
| 4255 |
|
| 4256 |
unsigned short ConnectionHandler::two_bytes_to_short(unsigned char *bytes) { |
| 4257 |
unsigned short local_res = (*bytes); |
| 4258 |
unsigned short two = *(bytes + 1); |
| 4259 |
local_res = (local_res * 256) + two; |
| 4260 |
return local_res; |
| 4261 |
} |