Debian Codegraph @

src/FOptionContainer.cpp

raw
1 // FOptionContainer class - contains the options for a filter group,
2 // including the banned/grey/exception site lists and the content/site/url regexp lists
3
4 // For all support, instructions and copyright go to:
5 // http://e2guardian.org/
6 // Released under the GPL v2, with the OpenSSL exception described in the README file.
7
8 // INCLUDES
9
10 #ifdef HAVE_CONFIG_H
11 #include "e2config.h"
12 #endif
13 #include "FOptionContainer.hpp"
14 #include "OptionContainer.hpp"
15 #include "ListMeta.hpp"
16 #include "Logger.hpp"
17
18 #include <cstdlib>
19 #include <iostream>
20 #include <fstream>
21 #include <netdb.h> // for gethostby
22 #include <netinet/in.h> // for address structures
23 #include <arpa/inet.h> // for inet_aton()
24 #include <sys/socket.h>
25
26 // GLOBALS
27
28 extern OptionContainer o;
29
30 // IMPLEMENTATION
31
32 // reverse DNS lookup on IP. be aware that this can return multiple results, unlike a standard lookup.
33 std::deque<String> *ipToHostname(const char *ip)
34 {
35 std::deque<String> *result = new std::deque<String>;
36 //struct in_addr address, **addrptr;
37 struct in_addr address;
38 if (inet_aton(ip, &address)) { // convert to in_addr
39 struct hostent *answer;
40 answer = gethostbyaddr((char *)&address, sizeof(address), AF_INET);
41 if (answer) { // success in reverse dns
42 result->push_back(String(answer->h_name));
43 }
44 }
45 return result;
46 }
47
48 void getClientFromIP(const char *ip, std::string &clienthost)
49 {
50 std::unique_ptr<std::deque<String> > hostnames;
51 hostnames.reset(ipToHostname(ip));
52 if(hostnames->empty()) {
53 clienthost = ip;
54 } else {
55 clienthost = std::string(hostnames->front().toCharArray());
56 }
57 }
58
59 FOptionContainer::~FOptionContainer()
60 {
61 reset();
62 }
63
64 void FOptionContainer::reset()
65 {
66 conffile.clear();
67 if (neterr_page != nullptr)
68 {
69 delete neterr_page;
70 neterr_page = nullptr;
71 }
72 if (banned_page != nullptr) {
73 delete banned_page;
74 banned_page = nullptr;
75 }
76 resetJustListData();
77 }
78
79 void FOptionContainer::resetJustListData()
80 {
81 if (banned_phrase_flag)
82 o.lm.deRefList(banned_phrase_list);
83
84 banned_phrase_flag = false;
85 content_regexp_flag = false;
86 ssl_mitm = false;
87
88 banned_phrase_list_index.clear();
89
90 // conffile.clear();
91
92 content_regexp_list_comp.clear();
93 content_regexp_list_rep.clear();
94 }
95
96 // grab this FG's HTML template
97 HTMLTemplate *FOptionContainer::getHTMLTemplate(bool upfail)
98 {
99 if(upfail && neterr_page)
100 return neterr_page;
101 if (banned_page)
102 return banned_page;
103 return &(o.html_template);
104 }
105
106
107 // read in the given file, write the list's ID into the given identifier,
108 // sort using startsWith or endsWith depending on sortsw,
109 // listname is used in error messages.
110 bool FOptionContainer::readFile(const char *filename, const char *list_pwd, unsigned int *whichlist, bool sortsw, bool cache, const char *listname)
111 {
112 DEBUG_trace(filename);
113 if (strlen(filename) < 3) {
114 E2LOGGER_error("Required Listname ", listname, " is not defined");
115 return false;
116 }
117 int res = o.lm.newItemList(filename, list_pwd, sortsw, 1, true);
118 if (res < 0) {
119 E2LOGGER_error("Error opening ", listname);
120 return false;
121 }
122 (*whichlist) = (unsigned)res;
123 if (!(*o.lm.l[(*whichlist)]).used) {
124 if (sortsw)
125 (*o.lm.l[(*whichlist)]).doSort(true);
126 else
127 (*o.lm.l[(*whichlist)]).doSort(false);
128 (*o.lm.l[(*whichlist)]).used = true;
129 }
130 return true;
131 }
132
133 bool FOptionContainer::readConfFile(const char *filename, String &list_pwd) {
134 DEBUG_trace(filename);
135 std::string linebuffer;
136 String now_pwd(list_pwd);
137 String temp; // for tempory conversion and storage
138 std::ifstream conffiles(filename, std::ios::in); // e2guardianfN.conf
139
140 if (!conffiles.good()) {
141 E2LOGGER_error("Error reading: ", filename);
142 return false;
143 }
144 String base_dir(filename);
145 base_dir.baseDir();
146 //String base_dir = filename;
147 //size_t fnsize;
148 //if ((fnsize = base_dir.find_last_of("/")) > 0)
149 // base_dir = base_dir.subString(1,fnsize);
150 while (!conffiles.eof()) {
151 getline(conffiles, linebuffer);
152 if (!conffiles.fail() && linebuffer.length() != 0) {
153 if (linebuffer[0] != '#') { // i.e. not commented out
154 temp = (char *) linebuffer.c_str();
155 if (temp.contains("#")) {
156 temp = temp.before("#");
157 }
158 temp.removeWhiteSpace(); // get rid of spaces at end of line
159 // check for LISTDIR and add replace with now_pwd
160 while (temp.contains("__LISTDIR__")) {
161 String temp2 = temp.before("__LISTDIR__");
162 temp2 += now_pwd;
163 temp2 += temp.after("__LISTDIR__");
164 temp = temp2;
165 }
166
167 // deal with included files
168 if (temp.startsWith(".")) {
169 String temp2 = temp.after(".Include<").before(">");
170 if (temp2.length() > 0) {
171 temp2.fullPath(base_dir);
172 if (!readConfFile(temp2.toCharArray(), now_pwd)) {
173 conffiles.close();
174 return false;
175 }
176 continue;
177 }
178 temp2 = temp.after(".Define LISTDIR <").before(">");
179 if (temp2.length() > 0) {
180 now_pwd = temp2;
181 //if(!now_pwd.endsWith("/"))
182 //now_pwd += "/";
183 // std::cerr << "now_pwd set to " << now_pwd;
184 }
185
186 continue;
187 }
188 // append ,listdir=now_pwd if line contains a file path - so that now_pwd can be passed
189 // to list file handler so that it can honour __LISTDIR__ in Included listfiles
190 if (temp.contains("path=") && !temp.contains("listdir=")) {
191 temp += ",listdir=";
192 temp += now_pwd;
193 }
194 linebuffer = temp.toCharArray();
195 conffile.push_back(linebuffer); // stick option in deque
196 }
197 }
198 }
199 conffiles.close();
200 return true;
201 }
202
203 bool FOptionContainer::read(const char *filename) {
204 try { // all sorts of exceptions could occur reading conf files
205 std::string linebuffer;
206 String temp; // for tempory conversion and storage
207 String list_pwd = __CONFDIR;
208 list_pwd += "/lists/group";
209 list_pwd += String(filtergroup);
210 //list_pwd += "/";
211 if(!readConfFile(filename, list_pwd)){
212 E2LOGGER_error("Error reading: ", filename);
213 return false;
214 }
215 DEBUG_config("Read conf into memory: ", filename);
216
217 if (findoptionS("disablecontentscan") == "on") {
218 disable_content_scan = true;
219 } else {
220 disable_content_scan = false;
221 }
222
223 if (findoptionS("disablecontentscanerror") == "on") {
224 disable_content_scan_error = true;
225 } else {
226 disable_content_scan_error = false;
227 }
228
229 if (findoptionS("contentscanexceptions") == "on") {
230 content_scan_exceptions = true;
231 } else {
232 content_scan_exceptions = false;
233 }
234
235 DEBUG_debug("disable_content_scan: ", String(disable_content_scan),
236 " disablecontentscanerror: ", String(disable_content_scan_error),
237 " contentscanexceptions: ", String(content_scan_exceptions) );
238
239 String mimes = findoptionS("textmimetypes");
240 if (mimes != "") {
241 size_t comma = mimes.find(',');
242 while (comma != std::string::npos) {
243 text_mime.push_back(mimes.substr(0, comma));
244 mimes = mimes.substr(comma + 1);
245 comma = mimes.find(',');
246 }
247 text_mime.push_back(mimes.substr(0, comma));
248 mimes = mimes.substr(comma + 1);
249 #ifdef DEBUG_HIGH
250 int size = (int) text_mime.size();
251 int i;
252 for (i = 0; i < size; i++) {
253 DEBUG_debug("mimes filtering : ", text_mime[i]);
254 }
255 #endif
256 }
257
258 String mimestop = findoptionS("stoptextmimetypes");
259 if (mimestop != "") {
260 size_t comma = mimestop.find(',');
261 while (comma != std::string::npos) {
262 text_mime_stop.push_back(mimestop.substr(0, comma));
263 mimes = mimestop.substr(comma + 1);
264 comma = mimestop.find(',');
265 }
266 text_mime_stop.push_back(mimestop.substr(0, comma));
267 mimestop = mimestop.substr(comma + 1);
268 #ifdef DEBUG_HIGH
269 int size = (int) text_mime_stop.size();
270 int i;
271 for (i = 0; i < size; i++) {
272 DEBUG_debug("mimes filtering : ", text_mime_stop[i]);
273 }
274 #endif
275 }
276
277 if (findoptionS("sslcheckcert") == "on") {
278 if(o.cert.enable_ssl) {
279 ssl_check_cert = true;
280 } else {
281 E2LOGGER_error("Warning: To use sslcheckcert, enablessl in e2guardian.conf must be on");
282 ssl_check_cert = false;
283 }
284 } else {
285 ssl_check_cert = false;
286 }
287
288 if (findoptionS("sslmitm") == "on") {
289 if(o.cert.enable_ssl) {
290 ssl_mitm = true;
291
292 if (findoptionS("automitm") == "off") {
293 automitm= false;
294 } else {
295 automitm= true;
296 }
297
298 if (findoptionS("mitmcheckcert") == "off")
299 mitm_check_cert = false;
300
301 allow_empty_host_certs = false;
302 if (findoptionS("allowemptyhostcert") == "on")
303 allow_empty_host_certs = true;
304 } else {
305 E2LOGGER_error("Warning: sslmitm requires ssl to be enabled in e2guardian.conf ");
306 ssl_mitm = false;
307 }
308 } else {
309 ssl_mitm = false;
310 }
311
312 #ifdef ENABLE_EMAIL
313 // Email notification patch by J. Gauthier
314 if (findoptionS("usesmtp") == "on") {
315 use_smtp = true;
316 } else {
317 use_smtp = false;
318 }
319
320 if (findoptionS("thresholdbyuser") == "on") {
321 byuser = true;
322 } else {
323 byuser = false;
324 }
325
326 if (findoptionS("notifyav") == "on") {
327 if (!use_smtp) {
328 E2LOGGER_error("notifyav cannot be on while usesmtp is off.");
329 return false;
330 }
331 notifyav = true;
332 } else {
333 notifyav = false;
334 }
335
336 if (findoptionS("notifycontent") == "on") {
337 if (!use_smtp) {
338 E2LOGGER_error("notifycontent cannot be on while usesmtp is off.");
339 return false;
340 }
341 notifycontent = true;
342 } else {
343 notifycontent = false;
344 }
345
346 violations = findoptionI("violations");
347 current_violations = 0;
348 violationbody = "";
349
350 threshold = findoptionI("threshold");
351
352 avadmin = findoptionS("avadmin");
353 if (avadmin.length() == 0) {
354 if (notifyav == 1) {
355 E2LOGGER_error("avadmin cannot be blank while notifyav is on.");
356 return false;
357 }
358 }
359
360 contentadmin = findoptionS("contentadmin");
361 if (contentadmin.length() == 0) {
362 if (use_smtp) {
363 E2LOGGER_error("contentadmin cannot be blank while usesmtp is on.");
364 return false;
365 }
366 }
367
368 mailfrom = findoptionS("mailfrom");
369 if (mailfrom.length() == 0) {
370 if (use_smtp) {
371 E2LOGGER_error("mailfrom cannot be blank while usesmtp is on.");
372 return false;
373 }
374 }
375 avsubject = findoptionS("avsubject");
376 if (avsubject.length() == 0 && notifyav == 1 && use_smtp == 1) {
377 E2LOGGER_error("avsubject cannot be blank while notifyav is on.");
378 return false;
379 }
380
381 contentsubject = findoptionS("contentsubject");
382 if (contentsubject.length() == 0 && use_smtp) {
383 E2LOGGER_error("contentsubject cannot be blank while usesmtp is on.");
384 return false;
385 }
386
387 #endif
388 // override default reporting level
389 if (findoptionS("reportinglevel").empty()) { //uses value from e2guardian.conf if empty
390 reporting_level = o.block.reporting_level;
391 } else {
392 reporting_level = findoptionI("reportinglevel");
393 if (!realitycheck(reporting_level, -1, 3, "reportinglevel")) {
394 return false;
395 }
396 }
397
398 if (reporting_level == 0) {
399 E2LOGGER_error("Reporting_level is : ", String(reporting_level), " file ", filename);
400 }
401
402 long temp_max_upload_size;
403 if (findoptionS("maxuploadsize").empty()) {
404 temp_max_upload_size = -1;
405 } else {
406 temp_max_upload_size = findoptionI("maxuploadsize");
407 }
408
409 if ((realitycheck(temp_max_upload_size, -1, 10000000, "max_uploadsize")) && (temp_max_upload_size != 0)) {
410 max_upload_size = temp_max_upload_size;
411 if (temp_max_upload_size > 0)
412 max_upload_size *= 1024;
413 } else {
414 E2LOGGER_error( "Invalid maxuploadsize: ", String(temp_max_upload_size) );
415 return false;
416 }
417 DEBUG_debug("maxuploadsize: ", String(temp_max_upload_size) );
418
419 // override default access denied address
420 if (reporting_level == 1 || reporting_level == 2) {
421 String temp_ada, temp_add;
422 temp_ada = findoptionS("accessdeniedaddress");
423 if (temp_ada != "") {
424 access_denied_address = temp_ada.toCharArray();
425 access_denied_domain = access_denied_address.c_str();
426 access_denied_domain = access_denied_domain.after("://");
427 access_denied_domain.removeWhiteSpace();
428 if (access_denied_domain.contains("/")) {
429 access_denied_domain = access_denied_domain.before("/");
430 // access_denied_domain now contains the FQ host name of the
431 // server that serves the accessdenied.html file
432 }
433 if (access_denied_domain.contains(":")) {
434 access_denied_domain = access_denied_domain.before(":"); // chop off the port number if any
435 }
436 } else {
437 access_denied_domain = "localhost"; // No initialized value
438 if (access_denied_domain.length() < 4) {
439 E2LOGGER_error("Warning accessdeniedaddress setting appears to be wrong.");
440 }
441 }
442 }
443 if (reporting_level == 3) {
444 if (access_denied_domain.length() > 1) {
445 E2LOGGER_error("Warning accessdeniedaddress setting appears to be wrong in reportinglevel 3");
446 return false;
447 }
448 // override default banned page
449 String html_template(findoptionS("htmltemplate"));
450 if (html_template != "") {
451 html_template = o.config.languagepath + html_template;
452 banned_page = new HTMLTemplate;
453 if (!(banned_page->readTemplateFile(html_template.toCharArray()))) {
454 E2LOGGER_error("Error reading HTML Template file: ", html_template);
455 return false;
456 }
457 } else {
458 html_template = o.config.languagepath + "template.html";
459 banned_page = new HTMLTemplate;
460 if (!(banned_page->readTemplateFile(html_template.toCharArray()))) {
461 E2LOGGER_error("Error reading default HTML Template file: ", html_template);
462 return false;
463 }
464 }
465
466 String neterr_template(findoptionS("neterrtemplate"));
467 if (neterr_template != "") {
468 neterr_template = o.config.languagepath + neterr_template;
469 neterr_page = new HTMLTemplate;
470 if (!(neterr_page->readTemplateFile(neterr_template.toCharArray()))) {
471 E2LOGGER_error("Error reading NetErr HTML Template file: ", neterr_template);
472 return false;
473 // HTML template file
474 }
475 } else { // if blank will default to HTML template file
476 neterr_template = o.config.languagepath + "neterr_template.html";
477 neterr_page = new HTMLTemplate;
478 if (!(neterr_page->readTemplateFile(neterr_template.toCharArray()))) {
479 E2LOGGER_error("Error reading default HTML and NetErr Template file: ", html_template);
480 return false;
481 }
482 }
483 }
484
485 if (findoptionS("nonstandarddelimiter") == "off") {
486 non_standard_delimiter = false;
487 } else {
488 non_standard_delimiter = true;
489 }
490
491 // grab group name (if not using external group names file)
492 if (!o.filter.use_group_names_list) {
493 if (findoptionS("groupname").length() > 0) {
494 name = findoptionS("groupname");
495 } else {
496 name = "group";
497 name += String(filtergroup);
498 }
499 DEBUG_debug("Group name: ", name);
500 }
501
502 embedded_url_weight = findoptionI("embeddedurlweight");
503 DEBUG_debug("Embedded URL Weight: ", std::to_string(embedded_url_weight));
504
505 category_threshold = findoptionI("categorydisplaythreshold");
506 DEBUG_debug("Category display threshold: ", std::to_string(category_threshold));
507
508 // Support weighted phrase mode per group
509 if (findoptionS("weightedphrasemode").length() > 0) {
510 weighted_phrase_mode = findoptionI("weightedphrasemode");
511 if (!realitycheck(weighted_phrase_mode, 0, 3, "weightedphrasemode"))
512 return false;
513 }
514 std::string exception_phrase_list_location(findoptionS("exceptionphraselist"));
515 std::string weighted_phrase_list_location(findoptionS("weightedphraselist"));
516 std::string banned_phrase_list_location(findoptionS("bannedphraselist"));
517
518 std::string storyboard_location(findoptionS("storyboard"));
519
520 if( storyboard_location.empty()) {
521 storyboard_location = __CONFDIR;
522 storyboard_location += "/group";
523 storyboard_location += String(filtergroup);
524 storyboard_location += ".story";
525 }
526
527 DEBUG_trace("Read settings into memory");
528
529 DEBUG_trace("Reading phrase, URL and site lists into memory");
530 if (weighted_phrase_mode > 0) {
531 naughtyness_limit = findoptionI("naughtynesslimit");
532 if (naughtyness_limit == 0) {
533 naughtyness_limit = 60;
534 }
535 if (!realitycheck(naughtyness_limit, 1, 0, "naughtynesslimit"))
536 return false;
537
538 if (!o.lm.readbplfile(banned_phrase_list_location.c_str(),
539 exception_phrase_list_location.c_str(),
540 weighted_phrase_list_location.c_str(), banned_phrase_list,
541 force_quick_search, naughtyness_limit)) {
542 return false;
543 } // read banned, exception, weighted phrase list
544 banned_phrase_flag = true;
545 }
546
547 {
548 std::deque<String> dq = findoptionM("ipsitelist");
549 DEBUG_debug("ipsitelist deque is size ", String(dq.size()) );
550 if(!LMeta.load_type(LIST_TYPE_IPSITE, dq)) read_errors = true;
551 }
552
553 {
554 std::deque<String> dq = findoptionM("iplist");
555 DEBUG_debug("iplist deque is size ", String(dq.size()) );
556 if(!LMeta.load_type(LIST_TYPE_IP, dq))read_errors = true;
557 }
558
559 {
560 std::deque<String> dq = findoptionM("timelist");
561 DEBUG_debug("timelist deque is size ", String(dq.size()) );
562 if(!LMeta.load_type(LIST_TYPE_TIME, dq))read_errors = true;
563 }
564
565 {
566 std::deque<String> dq = findoptionM("sitelist");
567 DEBUG_debug("sitelist deque is size ", String(dq.size()) );
568 if(!LMeta.load_type(LIST_TYPE_SITE, dq))read_errors = true;
569 }
570
571 {
572 std::deque<String> dq = findoptionM("urllist");
573 DEBUG_debug("urllist deque is size ", String(dq.size()) );
574 if(!LMeta.load_type(LIST_TYPE_URL, dq)) read_errors = true;
575 }
576
577 {
578 std::deque<String> dq = findoptionM("searchlist");
579 DEBUG_debug("searchlist deque is size ", String(dq.size()) );
580 if(!LMeta.load_type(LIST_TYPE_SEARCH, dq)) read_errors = true;
581 }
582
583 {
584 std::deque<String> dq = findoptionM("fileextlist");
585 DEBUG_debug("fileextlist deque is size ", String(dq.size()) );
586 if(!LMeta.load_type(LIST_TYPE_FILE_EXT, dq)) read_errors = true;
587 }
588
589 {
590 std::deque<String> dq = findoptionM("mimelist");
591 DEBUG_debug("mimelist deque is size ", String(dq.size()) );
592 if(!LMeta.load_type(LIST_TYPE_MIME, dq)) read_errors = true;
593 }
594
595 {
596 std::deque<String> dq = findoptionM("regexpboollist");
597 DEBUG_debug("regexpboollist deque is size ", String(dq.size()) );
598 if(!LMeta.load_type(LIST_TYPE_REGEXP_BOOL, dq)) read_errors = true;
599 }
600
601 {
602 std::deque<String> dq = findoptionM("regexpreplacelist");
603 DEBUG_debug("regexpreplacelist deque is size ", String(dq.size()) );
604 if(!LMeta.load_type(LIST_TYPE_REGEXP_REP, dq)) read_errors = true;
605 }
606
607 {
608 std::deque<String> dq = findoptionM("categorylist");
609 DEBUG_debug("categorylist deque is size ", String(dq.size()) );
610 if(!LMeta.load_type(LIST_TYPE_CATEGORY, dq)) read_errors = true;
611 }
612
613
614 #ifdef PRT_DNSAUTH
615 auth_exception_url_flag = true;
616 #endif
617
618 if (weighted_phrase_mode > 0) {
619 searchterm_limit = findoptionI("searchtermlimit");
620 if (!realitycheck(searchterm_limit, 0, 0, "searchtermlimit")) {
621 return false;
622 }
623
624 // Optionally override the normal phrase lists for search term blocking.
625 // We need all three lists to build a phrase tree, so fail if we encounter
626 // anything other than all three enabled/disabled simultaneously.
627 if (searchterm_limit > 0) {
628 std::string exception_searchterm_list_location(findoptionS("exceptionsearchtermlist"));
629 std::string weighted_searchterm_list_location(findoptionS("weightedsearchtermlist"));
630 std::string banned_searchterm_list_location(findoptionS("bannedsearchtermlist"));
631 if (!(exception_searchterm_list_location.length() == 0 &&
632 weighted_searchterm_list_location.length() == 0 &&
633 banned_searchterm_list_location.length() == 0)) {
634 // At least one is enabled - try to load all three.
635 if (!o.lm.readbplfile(banned_searchterm_list_location.c_str(),
636 exception_searchterm_list_location.c_str(),
637 weighted_searchterm_list_location.c_str(), searchterm_list,
638 force_quick_search, naughtyness_limit)) {
639 return false;
640 }
641 searchterm_flag = true;
642 }
643 }
644 }
645
646 std::string content_regexp_list_location(findoptionS("contentregexplist"));
647 if (content_regexp_list_location.length() > 1) {
648 unsigned int content_regexp_list;
649 if (!LMeta.readRegExReplacementFile(content_regexp_list_location.c_str(), list_pwd.toCharArray(), "contentregexplist", content_regexp_list, content_regexp_list_rep, content_regexp_list_comp)) {
650 return false;
651 } else {
652 content_regexp_flag = true;
653 }
654 } else {
655 content_regexp_flag = false;
656 }
657
658 DEBUG_trace("Lists in memory");
659 if(read_errors) {
660 E2LOGGER_warning("***List Error*** Reading of some lists defined or included in lists defined in ",filename, " failed" );
661 if(o.lists.abort_on_missing_list) return false;
662 }
663
664 DEBUG_trace("Read Storyboard");
665 if (!StoryB.readFile(storyboard_location.c_str(), LMeta, true))
666 return false;
667
668
669 if(!StoryB.setEntry(ENT_STORYB_PROXY_REQUEST,"checkrequest")) {
670 E2LOGGER_error("Required storyboard entry function 'checkrequest' is missing");
671 return false;
672 }
673
674 if(!StoryB.setEntry(ENT_STORYB_PROXY_RESPONSE,"checkresponse")) {
675 E2LOGGER_error("Required storyboard entry function 'checkresponse' is missing");
676 return false;
677 }
678
679 if(!StoryB.setEntry(ENT_STORYB_LOG_CHECK,"checklogging")) {
680 E2LOGGER_error( "Required storyboard entry function 'checklogging' is missing" );
681 return false;
682 }
683
684 if((o.net.transparenthttps_port > 0) && !StoryB.setEntry(ENT_STORYB_THTTPS_REQUEST,"thttps-checkrequest")) {
685 E2LOGGER_error("Required storyboard entry function 'thttps-checkrequest' is missing");
686 return false;
687 }
688
689 if((o.net.icap_port > 0) && !StoryB.setEntry(ENT_STORYB_ICAP_REQMOD,"icap-checkrequest")) {
690 E2LOGGER_error("Required storyboard entry function 'icap-checkrequest' is missing");
691 return false;
692 }
693
694 if((o.net.icap_port > 0) && !StoryB.setEntry(ENT_STORYB_ICAP_RESMOD,"icap-checkresponse")) {
695 E2LOGGER_error("Required storyboard entry function 'icap-checkresponse' is missing");
696 return false;
697 }
698 if (!precompileregexps()) {
699 return false;
700 } // precompiled reg exps for speed
701
702 if((o.net.icap_port > 0) && !StoryB.setEntry(ENT_STORYB_ICAP_RESMOD,"icap-checkresponse")) {
703 E2LOGGER_error("Required storyboard entry function 'icap-checkresponse' is missing");
704 return false;
705 }
706
707 if (o.story.dm_entry_dq.size() > 0) {
708 for (std::deque<struct StoryBoardOptions::SB_entry_map>::const_iterator i = o.story.dm_entry_dq.begin(); i != o.story.dm_entry_dq.end(); ++i) {
709 if (!StoryB.setEntry(i->entry_id, i->entry_function)) {
710 E2LOGGER_error("Required DM storyboard entry function", i->entry_function, " is missing from pre_auth.stoary" );
711 }
712 }
713 }
714
715 if (!precompileregexps()) {
716 return false;
717 } // precompiled reg exps for speed
718
719 bypass_mode = findoptionI("bypass");
720
721 cgi_bypass = (findoptionS("cgibypass")== "on" );
722 cgi_infection_bypass = (findoptionS("cgiinfectionbypass") == "on");
723
724 bypass_version = findoptionI("bypassversion");
725 if (!realitycheck(bypass_version, 0, 2, "bypassversion")) {
726 return false;
727 }
728 if (bypass_version == 0) bypass_version = 1; //default
729 if (bypass_version == 2) bypass_v2 = true; //default
730
731 bypass_version = findoptionI("bypassversion");
732 if (!realitycheck(bypass_version, 0, 2, "bypassversion")) {
733 return false;
734 }
735 if (bypass_version == 0) bypass_version = 2; //default
736 if (bypass_version == 2) bypass_v2 = true; //default
737
738
739 if(bypass_mode == -1) cgi_bypass = true; // for backward compatibility
740
741 // we use the "magic" key here both for filter bypass *and* for filter bypass after virus scan (fancy DM).
742 if ((bypass_mode != 0) || (disable_content_scan != 1)) {
743 magic = findoptionS("bypasskey");
744 if (magic.length() < 9) {
745 std::string s(16u, ' ');
746 for (int i = 0; i < 16; i++) {
747 s[i] = (rand() % 26) + 'A';
748 }
749 magic = s;
750 }
751 DEBUG_debug("Setting magic key to '", magic, "'");
752
753 // Create the Bypass Cookie magic key
754 cookie_magic = std::string(16u, ' ');
755 for (int i = 0; i < 16; i++) {
756 cookie_magic[i] = (rand() % 26) + 'A';
757 }
758 }
759
760 infection_bypass_mode = findoptionI("infectionbypass");
761 if (!realitycheck(infection_bypass_mode, -1, 0, "infectionbypass")) {
762 return false;
763 }
764
765 if(infection_bypass_mode == -1) cgi_infection_bypass = true; // for backward compatibility
766
767
768 if (infection_bypass_mode != 0) {
769 imagic = findoptionS("infectionbypasskey");
770 if (imagic.length() < 9) {
771 std::string s(16u, ' ');
772 for (int i = 0; i < 16; i++) {
773 s[i] = (rand() % 26) + 'A';
774 }
775 imagic = s;
776 }
777 DEBUG_debug("Setting imagic key to '", imagic, "'");
778
779 if (findoptionS("infectionbypasserrorsonly") == "off") {
780 infection_bypass_errors_only = false;
781 } else {
782 DEBUG_debug("Only allowing infection bypass on scan error");
783 infection_bypass_errors_only = true;
784 }
785 }
786
787 if (findoptionS("scanbypass") == "on") {
788 scan_bypass = true;
789 } else {
790 scan_bypass = false;
791 }
792
793 if(((cgi_bypass)||(cgi_infection_bypass)) && (bypass_version == 2)) {
794 cgi_magic = findoptionS("cgikey");
795 if ( cgi_magic.length() < 9 ) {
796 E2LOGGER_error("A valid cgikey must be provided with bypass cgi version 2");
797 return false;
798 };
799 cgi_bypass_v2 = true;
800 if(cgi_infection_bypass && infection_bypass_mode < 60) {
801 E2LOGGER_error("infectionbypassmode must be greater than 60 with bypass cgi version 2");
802 return false;
803 } else if(bypass_mode < 60) {
804 E2LOGGER_error("bypassmode must be greater than 60 with bypass cgi version 2");
805 return false;
806 }
807
808 }
809
810 } catch (std::exception &e) {
811 E2LOGGER_error(e.what()); // when called the daemon has not
812 // detached so we can do this
813 return false;
814 }
815 return true;
816 }
817
818
819
820 int FOptionContainer::findoptionI(const char *option)
821 {
822 int res = String(findoptionS(option).c_str()).toInteger();
823 return res;
824 }
825
826 std::string FOptionContainer::findoptionS(const char *option)
827 {
828 // findoptionS returns a found option stored in the deque
829 String temp;
830 String temp2;
831 String o(option);
832 // for (int i = 0; i < (signed)conffile.size(); i++)
833 for (int i = (signed)conffile.size() - 1; i > -1; i--) // reverse search so that later entries will overwrite any earlier ones.
834 {
835 temp = conffile[i].c_str();
836 temp2 = temp.before("=");
837 while (temp2.endsWith(" ")) { // get rid of tailing spaces before =
838 temp2.chop();
839 }
840 if (o == temp2) {
841 temp = temp.after("=");
842 while (temp.startsWith(" ")) { // get rid of heading spaces
843 temp.lop();
844 }
845 if (temp.startsWith("'")) { // inverted commas
846 temp.lop();
847 }
848 while (temp.endsWith(" ")) { // get rid of tailing spaces
849 temp.chop();
850 }
851 if (temp.endsWith("'")) { // inverted commas
852 temp.chop();
853 }
854 return temp.toCharArray();
855 }
856 }
857 return "";
858 }
859
860 std::deque<String> FOptionContainer::findoptionM(const char *option)
861 {
862 // findoptionS returns all the matching options
863 String temp;
864 String temp2;
865 String o(option);
866 std::deque<String> results;
867
868 for (std::deque<std::string>::iterator i = conffile.begin(); i != conffile.end(); i++) {
869 if ((*i).empty())
870 continue;
871 temp = (*i).c_str();
872 temp2 = temp.before("=");
873 while (temp2.endsWith(" ")) { // get rid of tailing spaces before =
874 temp2.chop();
875 }
876 if (o == temp2) {
877 temp = temp.after("=");
878 while (temp.startsWith(" ")) { // get rid of heading spaces
879 temp.lop();
880 }
881 // if (temp.startsWith("'")) { // inverted commas
882 // temp.lop();
883 // }
884 temp.removeMultiChar('\'');
885 while (temp.endsWith(" ")) { // get rid of tailing spaces
886 temp.chop();
887 }
888 // if (temp.endsWith("'")) { // inverted commas
889 // temp.chop();
890 // }
891 results.push_back(temp);
892 }
893 }
894 return results;
895 }
896
897 bool FOptionContainer::realitycheck(int l, int minl, int maxl, const char *emessage)
898 {
899 // realitycheck checks a String for certain expected criteria
900 // so we can spot problems in the conf files easier
901 if ((l < minl) || ((maxl > 0) && (l > maxl))) {
902 E2LOGGER_error("Config problem; check allowed values for ", emessage);
903 return false;
904 }
905 return true;
906 }
907
908 bool FOptionContainer::precompileregexps()
909 {
910 if (!isiphost.comp(".*[a-z|A-Z].*")) {
911 E2LOGGER_error("Error compiling RegExp isiphost.");
912 return false;
913 }
914
915 return true;
916 }
917
918 bool FOptionContainer::isOurWebserver(String url)
919 {
920 // reporting levels 0 and 3 don't use the CGI
921 if (reporting_level == 1 || reporting_level == 2) {
922 url.removeWhiteSpace(); // just in case of weird browser crap
923 url.toLower();
924 url.removePTP(); // chop off the ht(f)tp(s)://
925 if (url.contains("/")) {
926 url = url.before("/"); // chop off any path after the domain
927 }
928 if (url.startsWith(access_denied_domain)) { // don't filter our web server
929 return true;
930 }
931 }
932 return false;
933 }