| 1 |
// For all support, instructions and copyright go to: |
| 2 |
// http://e2guardian.org/ |
| 3 |
// Released under the GPL v2, with the OpenSSL exception described in the README file. |
| 4 |
|
| 5 |
#ifndef __HPP_CONNECTIONHANDLER |
| 6 |
#define __HPP_CONNECTIONHANDLER |
| 7 |
|
| 8 |
// INCLUDES |
| 9 |
#include <iostream> |
| 10 |
#include <string> |
| 11 |
#include "OptionContainer.hpp" |
| 12 |
#include "FOptionContainer.hpp" |
| 13 |
#include "LOptionContainer.hpp" |
| 14 |
#include "NaughtyFilter.hpp" |
| 15 |
#include "Socket.hpp" |
| 16 |
#include "HTTPHeader.hpp" |
| 17 |
#include "ICAPHeader.hpp" |
| 18 |
#include "FatController.hpp" |
| 19 |
#include "Auth.hpp" |
| 20 |
|
| 21 |
// DECLARATIONS |
| 22 |
|
| 23 |
// add a known clean URL to the cache |
| 24 |
void addToClean(String &url, const int fg); |
| 25 |
|
| 26 |
// record for storing information about POST data parts |
| 27 |
// used for building up the POST data log column |
| 28 |
struct postinfo { |
| 29 |
// MIME type & original filename (if available) |
| 30 |
std::string mimetype; |
| 31 |
std::string filename; |
| 32 |
// name of file containing headers & body info |
| 33 |
// for this POST part (if it has been stored) |
| 34 |
std::string storedname; |
| 35 |
// size of part |
| 36 |
size_t size; |
| 37 |
// offset of body data from start of file |
| 38 |
// (if post part was stored on disk) |
| 39 |
size_t bodyoffset; |
| 40 |
bool blocked; |
| 41 |
postinfo() |
| 42 |
: size(0), bodyoffset(0), blocked(false){}; |
| 43 |
}; |
| 44 |
|
| 45 |
// the ConnectionHandler class - handles filtering, scanning, and blocking of |
| 46 |
// data passed between a client and the external proxy. |
| 47 |
class ConnectionHandler |
| 48 |
{ |
| 49 |
public: |
| 50 |
ConnectionHandler(); |
| 51 |
// : clienthost(NULL) { |
| 52 |
// ch_isiphost.comp(",*[a-z|A-Z].*"); |
| 53 |
// ldl = o.currentList(); |
| 54 |
// load_id = ldl->reload_id; |
| 55 |
//} |
| 56 |
~ConnectionHandler() |
| 57 |
{ |
| 58 |
delete clienthost; |
| 59 |
}; |
| 60 |
|
| 61 |
int load_id; |
| 62 |
// pass data between proxy and client, filtering as we go. |
| 63 |
int handlePeer(Socket &peerconn, String &ip, stat_rec* &dystat, unsigned int LC_type); |
| 64 |
|
| 65 |
auth_rec SBauth; // record persists for whole connection |
| 66 |
|
| 67 |
private: |
| 68 |
int filtergroup; |
| 69 |
int oldfg = 0; |
| 70 |
bool matchedip; |
| 71 |
bool persistent_authed; |
| 72 |
bool overide_persist; |
| 73 |
bool is_real_user = false; |
| 74 |
|
| 75 |
std::string clientuser; |
| 76 |
std::string oldclientuser; |
| 77 |
std::string *clienthost; |
| 78 |
std::string urlparams; |
| 79 |
std::list<postinfo> postparts; |
| 80 |
String lastcategory; |
| 81 |
std::shared_ptr<LOptionContainer> ldl; |
| 82 |
|
| 83 |
void peerDiag(const char *message, Socket &peersock ); |
| 84 |
void upstreamDiag(const char *message, Socket &proxysock ); |
| 85 |
|
| 86 |
int handleConnection(Socket &peerconn, String &ip, bool ismitm, Socket &proxyconn, stat_rec* &dystat); |
| 87 |
int handleProxyTLSConnection(Socket &peerconn, String &ip, Socket &upssock, stat_rec* &dystat); |
| 88 |
int handleTHTTPSConnection(Socket &peerconn, String &ip, Socket &proxysock, stat_rec* &dystat); |
| 89 |
int handleICAPConnection(Socket &peerconn, String &ip, Socket &proxysock, stat_rec* &dystat); |
| 90 |
int handleICAPreqmod(Socket &peerconn, String &ip, NaughtyFilter &checkme, ICAPHeader &icaphead, AuthPlugin *auth_plugin) ; |
| 91 |
int handleICAPresmod(Socket &peerconn, String &ip, NaughtyFilter &checkme, ICAPHeader &icaphead, DataBuffer &docbody) ; |
| 92 |
|
| 93 |
bool get_original_ip_port(Socket &peerconn, NaughtyFilter &checkme); |
| 94 |
bool getdnstxt(std::string &clientip, String &user); |
| 95 |
|
| 96 |
String dns_error(int herror); |
| 97 |
|
| 98 |
// write a log entry containing the given data (if required) |
| 99 |
// void doLog(std::string &who, std::string &from, String &where, unsigned int &port, |
| 100 |
// std::string &what, String &how, off_t &size, std::string *cat, bool isnaughty, int naughtytype, |
| 101 |
// bool isexception, bool istext, struct timeval *thestart, bool cachehit, int code, |
| 102 |
// std::string &mimetype, bool wasinfected, bool wasscanned, int naughtiness, int filtergroup, |
| 103 |
// HTTPHeader *reqheader, int message_no = 999, bool contentmodified = false, |
| 104 |
// bool urlmodified = false, bool headermodified = false, |
| 105 |
// bool headeradded = false); |
| 106 |
|
| 107 |
void doLog(std::string &who, std::string &from, NaughtyFilter &cm); |
| 108 |
void doRQLog(std::string &who, std::string &from, NaughtyFilter &cm, std::string &funct); |
| 109 |
|
| 110 |
bool goMITM(NaughtyFilter &checkme, Socket &proxysock, Socket &peerconn,bool &persistProxy, bool &authed, bool &persistent_authed, String &ip, stat_rec* &dystat, std::string &clientip, bool transparent = false); |
| 111 |
|
| 112 |
|
| 113 |
// perform URL encoding on a string |
| 114 |
std::string miniURLEncode(const char *s); |
| 115 |
|
| 116 |
// RegExp ch_isiphost; |
| 117 |
// RegResult Rch_isiphost; |
| 118 |
bool genDenyAccess(Socket &peerconn, String &eheader, String &ebody, HTTPHeader *header, HTTPHeader *docheader, |
| 119 |
String *url, NaughtyFilter *checkme, std::string *clientuser, std::string *clientip, |
| 120 |
int filtergroup, |
| 121 |
bool ispostblock, int headersent, bool wasinfected, bool scanerror, bool forceshow = false); |
| 122 |
|
| 123 |
// show the relevant banned page depending upon the report level settings, request type, etc. |
| 124 |
bool denyAccess(Socket *peerconn, Socket *proxysock, HTTPHeader *header, HTTPHeader *docheader, |
| 125 |
String *url, NaughtyFilter *checkme, std::string *clientuser, std::string *clientip, |
| 126 |
int filtergroup, bool ispostblock, int headersent, bool wasinfected, bool scanerror, bool forceshow = false); |
| 127 |
|
| 128 |
// create temporary ban bypass URLs/cookies |
| 129 |
String hashedURL(String *url, int filtergroup, std::string *clientip, bool infectionbypass, std::string *user); |
| 130 |
// is this a temporary filter bypass URL? |
| 131 |
int isBypassURL(String url, const char *magic, const char *clientip, std::string btype, std::string &user); |
| 132 |
// is this a scan bypass URL? (download previously scanned file) |
| 133 |
bool isScanBypassURL(String url, const char *magic, const char *clientip); |
| 134 |
String hashedCookie(String *url, const char *magic, std::string *clientip, int bypasstimestamp); |
| 135 |
|
| 136 |
// do content scanning (AV filtering) and naughty filtering |
| 137 |
void contentFilter(HTTPHeader *docheader, HTTPHeader *header, DataBuffer *docbody, Socket *proxysock, |
| 138 |
Socket *peerconn, int *headersent, bool *pausedtoobig, off_t *docsize, NaughtyFilter *checkme, |
| 139 |
bool wasclean, int filtergroup, std::deque<CSPlugin *> &responsescanner, std::string *clientuser, |
| 140 |
std::string *clientip, bool *wasinfected, bool *wasscanned, bool isbypass, String &url, String &domain, |
| 141 |
bool *scanerror, bool &contentmodified, String *csmessage); |
| 142 |
|
| 143 |
// send a file to the client - used during bypass of blocked downloads |
| 144 |
off_t sendFile(Socket *peerconn, NaughtyFilter &cm, String &url, bool is_icap = false, ICAPHeader *icap_head = NULL); |
| 145 |
|
| 146 |
bool writeback_error( NaughtyFilter &cm, Socket & cl_sock, int mess_no1, int mess_no2, std::string mess); |
| 147 |
bool gen_error_mess( Socket &peerconn, NaughtyFilter &cm, String &eheader, String &ebody, int mess_no1, int mess_no2, std::string mess); |
| 148 |
|
| 149 |
bool doAuth(int &auth_result, bool &authed, int &filtergroup,AuthPlugin* auth_plugin, Socket & peerconn, Socket &proxysock, |
| 150 |
HTTPHeader & header, NaughtyFilter &cm, bool only_client_ip = false, bool isconnect_like = false); |
| 151 |
|
| 152 |
bool doAuth(int &auth_result, bool &authed, int &filtergroup,AuthPlugin* auth_plugin, Socket & peerconn, |
| 153 |
HTTPHeader & header, NaughtyFilter &cm, bool only_client_ip = false, bool isconnect_like = false); |
| 154 |
|
| 155 |
bool checkByPass( NaughtyFilter &checkme, std::shared_ptr<LOptionContainer> & ldl, HTTPHeader &header, Socket & proxysock, |
| 156 |
Socket &peerconn, std::string &clientip ); |
| 157 |
bool sendScanFile( Socket &peerconn, NaughtyFilter &checkme, bool is_icap = false, ICAPHeader *icaphead = NULL); |
| 158 |
|
| 159 |
void check_search_terms(NaughtyFilter &cm); |
| 160 |
void check_content(NaughtyFilter &cm, DataBuffer &docbody, Socket &proxysock, Socket &peerconn, |
| 161 |
std::deque<CSPlugin *> &responsescanners); |
| 162 |
//ssl certificat checking |
| 163 |
void checkCertificate(String &hostname, Socket *sslSock, NaughtyFilter *checkme); |
| 164 |
|
| 165 |
int sendProxyConnect(String &hostname, Socket *sock, NaughtyFilter *checkme); |
| 166 |
|
| 167 |
int determineGroup(std::string &user, int &fg, StoryBoard & uglc, NaughtyFilter &checkme, int story_entry); |
| 168 |
int connectUpstream(Socket &sock, NaughtyFilter &cm,int port); |
| 169 |
unsigned short two_bytes_to_short(unsigned char *bytes); |
| 170 |
bool get_TLS_SNI(char *bytes, int len, String &sni,bool &is_ech); |
| 171 |
}; |
| 172 |
|
| 173 |
|
| 174 |
#endif |
| 175 |
|