Debian Codegraph @

src/LOptionContainer.cpp

raw
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 // INCLUDES
6
7 #ifdef HAVE_CONFIG_H
8 #include "e2config.h"
9 #endif
10 #include "LOptionContainer.hpp"
11 #include "OptionContainer.hpp"
12 #include "RegExp.hpp"
13 #include "ConfigVar.hpp"
14 #include "Logger.hpp"
15
16 //#include <cstdio>
17 //#include <iostream>
18 #include <fstream>
19 #include <sstream>
20 #include <dirent.h>
21 #include <cstdlib>
22
23 // GLOBALS
24
25 extern OptionContainer o;
26
27 // IMPLEMENTATION
28
29 LOptionContainer::LOptionContainer()
30 {
31 }
32
33 LOptionContainer::LOptionContainer(int load_id)
34 {
35 DEBUG_trace(load_id);
36
37 char buff[40];
38
39 sprintf(buff, "%ld", time(NULL));
40 start_time = buff;
41
42 loaded_ok = true;
43
44 {
45 DEBUG_config("iplist size is ", o.lists.iplist_dq.size());
46 if(!LMeta.load_type(LIST_TYPE_IP, o.lists.iplist_dq))
47 loaded_ok = false;
48 }
49
50 {
51 DEBUG_config("sitelist size is ", o.lists.sitelist_dq.size());
52 if(!LMeta.load_type(LIST_TYPE_SITE, o.lists.sitelist_dq))
53 loaded_ok = false;
54 }
55
56 {
57 DEBUG_config("ipsitelist size is ", o.lists.ipsitelist_dq.size());
58 if(!LMeta.load_type(LIST_TYPE_IPSITE, o.lists.ipsitelist_dq))
59 loaded_ok = false;
60 }
61
62 {
63 DEBUG_config("urllist size is ", o.lists.urllist_dq.size());
64 if(!LMeta.load_type(LIST_TYPE_URL, o.lists.urllist_dq))
65 loaded_ok = false;
66 }
67
68 {
69 DEBUG_config("regexpboollist size is ", o.lists.regexpboollist_dq.size());;
70 if(!LMeta.load_type(LIST_TYPE_REGEXP_BOOL, o.lists.regexpboollist_dq))
71 loaded_ok = false;
72 }
73
74 {
75 DEBUG_config("maplist size is ", o.lists.maplist_dq.size());
76 if(!LMeta.load_type(LIST_TYPE_MAP, o.lists.maplist_dq))
77 loaded_ok = false;
78 }
79
80 {
81 DEBUG_config("ipmaplist size is ", o.lists.ipmaplist_dq.size());
82 if(!LMeta.load_type(LIST_TYPE_IPMAP, o.lists.ipmaplist_dq))
83 loaded_ok = false;
84 }
85
86 DEBUG_config("read Storyboard: ", o.story.storyboard_location);
87 if (!StoryA.readFile(o.story.storyboard_location.c_str(), LMeta, true)) {
88 E2LOGGER_error("Storyboard not loaded OK");
89 sb_loaded_ok = false;
90 is_fatal = true;
91 }
92
93 if (sb_loaded_ok && !StoryA.setEntry(ENT_STORYA_PRE_AUTH,"pre-authcheck")) {
94 E2LOGGER_error("Required storyboard entry function 'pre-authcheck' is missing");
95 sb_loaded_ok = false;
96 is_fatal = true;
97 }
98
99 if (sb_loaded_ok && (o.net.transparenthttps_port > 0) && !StoryA.setEntry(ENT_STORYA_PRE_AUTH_THTTPS,"thttps-pre-authcheck")) {
100 E2LOGGER_error("Required storyboard entry function 'thttps-pre-authcheck' is missing");
101 sb_loaded_ok = false;
102 is_fatal = true;
103 }
104
105 if (sb_loaded_ok && (o.net.icap_port > 0) && !StoryA.setEntry(ENT_STORYA_PRE_AUTH_ICAP,"icap-pre-authcheck")) {
106 E2LOGGER_error("Required storyboard entry function 'icap-pre-authcheck' is missing");
107 sb_loaded_ok = false;
108 is_fatal = true;
109 }
110
111 // if (loaded_ok && o.use_filter_groups_list) {
112 // if (!doReadItemList(o.filter_groups_list_location.c_str(), &filter_groups_list, "filtergroupslist", true)) {
113 // E2LOGGER_error("Failed to read filtergroupslist");
114 // loaded_ok = false;
115 // }
116 // }
117
118 DEBUG_trace("");
119 if (sb_loaded_ok && o.story.auth_entry_dq.size() > 0) {
120 for (std::deque<struct StoryBoardOptions::SB_entry_map>::const_iterator i = o.story.auth_entry_dq.begin(); i != o.story.auth_entry_dq.end(); ++i) {
121 if (!StoryA.setEntry(i->entry_id, i->entry_function)) {
122 E2LOGGER_error("Required auth storyboard entry function", i->entry_function,
123 " is missing from pre_auth.story");
124 loaded_ok = false;
125 is_fatal = true;
126 }
127 }
128 }
129
130 if(sb_loaded_ok && (!readFilterGroupConf() || (o.lists.abort_on_missing_list && o.config_error))) {
131 loaded_ok = false;
132 E2LOGGER_error("Error in reading filter group files");
133 }
134 reload_id = load_id;
135 ++o.LC_cnt;
136 if (load_id == 0) o.filter.numfg = numfg; // do this on first load only
137 }
138
139
140 bool LOptionContainer::inSiteList(String &url, ListContainer *lc, bool ip, bool ssl, String &match)
141 {
142 String lastcategory;
143 url.removeWhiteSpace(); // just in case of weird browser crap
144 url.toLower();
145 url.removePTP(); // chop off the ht(f)tp(s)://
146 if (url.contains("/")) {
147 url = url.before("/"); // chop off any path after the domain
148 }
149 //const char *i;
150 bool i;
151 String result;
152 //bool isipurl = isIPHostname(url);
153 while (url.contains(".")) {
154 i = lc->findInList(url.toCharArray(), lastcategory, match, result);
155 if (i ) {
156 return i; // exact match
157 }
158 url = url.after("."); // check for being in higher level domains
159 }
160 if (url.length() > 1) { // allows matching of .tld
161 url = "." + url;
162 i = lc->findInList(url.toCharArray(), lastcategory, match, result);
163 if (i ) {
164 return i; // exact match
165 }
166 }
167 return false; // and our survey said "UUHH UURRGHH"
168 }
169
170 // look in given URL list for given URL
171 bool LOptionContainer::inURLList(String &url, ListContainer *lc, bool ip, bool ssl, String &match)
172 {
173 unsigned int fl;
174 //char *i;
175 bool i = false;
176 String lastcategory;
177 String foundurl;
178
179 DEBUG_trace("inURLList");
180 url.removeWhiteSpace(); // just in case of weird browser crap
181 url.toLower();
182 url.removePTP(); // chop off the ht(f)tp(s)://
183 if (url.contains("/")) {
184 String tpath("/");
185 tpath += url.after("/");
186 url = url.before("/");
187 tpath.hexDecode();
188 tpath.realPath();
189 url += tpath; // will resolve ../ and %2e2e/ and // etc
190 }
191 if (url.endsWith("/")) {
192 url.chop(); // chop off trailing / if any
193 }
194
195 DEBUG_debug("inURLList (processed): ", url);
196 while (url.before("/").contains(".")) {
197 i = lc->findStartsWith(url.toCharArray(), lastcategory, match);
198 if (i) {
199 foundurl = match;
200 fl = foundurl.length();
201 DEBUG_debug("foundurl: ", foundurl, ":", foundurl.length());
202 DEBUG_debug("url: ", url, ":", fl);
203 if (url.length() > fl) {
204 if (url[fl] == '/' || url[fl] == '?' || url[fl] == '&' || url[fl] == '=') {
205 return true; // matches /blah/ or /blah/foo but not /blahfoo
206 }
207 } else {
208 return true; // exact match
209 }
210 }
211 url = url.after("."); // check for being in higher level domains
212 }
213 return false;
214 }
215
216
217
218 // TODO: Filter rules should migrate to FOptionContainer.cpp ? -- No, these are not filtergroup rules but nmaybe to their own cpp??
219
220 bool LOptionContainer::inRoom(const std::string &ip, std::string &room, std::string *host, bool *block, bool *part_block, bool *isexception, String url)
221 {
222 String temp;
223 String match;
224 for (std::list<struct room_item>::const_iterator i = rooms.begin(); i != rooms.end(); ++i) {
225 if (i->iplist->inList(ip, host)) {
226 DEBUG_debug(" IP is in room: ", i->name);
227 temp = url;
228 ListContainer *lc;
229 if (i->sitelist) {
230 lc = i->sitelist;
231 if (inSiteList(temp, lc, false, false, match)) {
232 DEBUG_debug(" room site exception found: ");
233 *isexception = true;
234 room = i->name;
235 return true;
236 }
237 }
238 temp = url;
239 if (i->urllist && inURLList(temp, i->urllist, false, false, match)) {
240 DEBUG_debug(" room url exception found: ");
241 *isexception = true;
242 room = i->name;
243 return true;
244 }
245 if (i->block) {
246 *block = true;
247 *part_block = i->part_block;
248 room = i->name;
249 DEBUG_debug(" room blanket block active: ");
250 return true;
251 } else {
252 DEBUG_debug(" room - no url/site exception or block found: ");
253 return false;
254 }
255 }
256 }
257 return false;
258 }
259
260
261
262 LOptionContainer::~LOptionContainer()
263 {
264 reset();
265 }
266
267 void LOptionContainer::reset()
268 {
269 deleteFilterGroups();
270 deleteRooms();
271 conffile.clear();
272 --o.LC_cnt;
273 }
274
275 void LOptionContainer::deleteFilterGroups()
276 {
277 for (int i = 0; i < numfg; i++) {
278 if (fg[i] != NULL) {
279 DEBUG_debug("In deleteFilterGroups loop");
280 delete fg[i]; // delete extra FOptionContainer objects
281 fg[i] = NULL;
282 }
283 }
284 if (numfg > 0) {
285 delete[] fg;
286 numfg = 0;
287 }
288 }
289
290 void LOptionContainer::deleteFilterGroupsJustListData()
291 {
292 for (int i = 0; i < numfg; i++) {
293 if (fg[i] != NULL) {
294 fg[i]->resetJustListData();
295 }
296 }
297 }
298
299 #ifdef NOTDEF
300 bool LOptionContainer::read(std::string& filename, int type, std::string& exception_ip_list_location,
301 std::string& banned_ip_list_location)
302 {
303 E2LOGGER_TRACE(filename);
304
305 conffilename = filename;
306
307 // all sorts of exceptions could occur reading conf files
308 try {
309 std::string linebuffer;
310 String temp; // for tempory conversion and storage
311 std::ifstream conffiles(filename.c_str(), std::ios::in); // e2guardian.conf
312 if (!conffiles.good()) {
313 E2LOGGER_error("error reading e2guardian.conf");
314 return false;
315 }
316 while (!conffiles.eof()) {
317 getline(conffiles, linebuffer);
318 if (!conffiles.eof() && linebuffer.length() != 0) {
319 if (linebuffer[0] != '#') { // i.e. not commented out
320 temp = (char *) linebuffer.c_str();
321 if (temp.contains("#")) {
322 temp = temp.before("#");
323 }
324 temp.removeWhiteSpace(); // get rid of spaces at end of line
325 linebuffer = temp.toCharArray();
326 conffile.push_back(linebuffer); // stick option in deque
327 }
328 }
329 }
330 conffiles.close();
331
332 if (type == 0 || type == 2) {
333
334
335
336 if (type == 0) {
337 return true;
338 }
339 }
340
341
342
343 if (((per_room_directory_location = findoptionS("perroomdirectory")) != "") || ((per_room_directory_location = findoptionS("perroomblockingdirectory")) != "")) {
344 loadRooms(true);
345 }
346
347
348 // filter_groups_list_location = findoptionS("filtergroupslist");
349 // std::string banned_ip_list_location(findoptionS("bannediplist"));
350 // std::string exception_ip_list_location(findoptionS("exceptioniplist"));
351 // group_names_list_location = findoptionS("groupnamesfile");
352 // std::string language_list_location(languagepath + "messages");
353 {
354 std::deque<String> dq = findoptionM("iplist");
355 DEBUG_debug("iplist deque is size ", dq.size());
356 LMeta.load_type(LIST_TYPE_IP, dq);
357 }
358
359 {
360 std::deque<String> dq = findoptionM("sitelist");
361 DEBUG_debug("sitelist deque is size ", dq.size());
362 LMeta.load_type(LIST_TYPE_SITE, dq);
363 }
364
365 {
366 std::deque<String> dq = findoptionM("ipsitelist");
367 DEBUG_debug("ipsitelist deque is size ", dq.size());
368 LMeta.load_type(LIST_TYPE_IPSITE, dq);
369 }
370
371 {
372 std::deque<String> dq = findoptionM("urllist");
373 DEBUG_debug("urllist deque is size ", dq.size());
374 LMeta.load_type(LIST_TYPE_URL, dq);
375 }
376
377 if (!StoryA.readFile(o.storyboard_location.c_str(), LMeta, true))
378 return false;
379
380 if (!StoryA.setEntry1("pre-authcheck")) {
381 E2LOGGER_error("Required storyboard entry function 'pre-authcheck' is missing");
382 return false;
383 }
384
385 if (!readFilterGroupConf()) {
386 E2LOGGER_error("Error reading filter group conf file(s).");
387 return false;
388 }
389
390 } catch (std::exception &e) {
391 E2LOGGER_error(e.what());
392 return false;
393 }
394 return true;
395 }
396 #endif
397
398
399
400
401
402 // TODO: Filter rules should migrate to FOptionContainer.cpp ? -- No, these are not filtergroup rules but nmaybe to their own cpp??
403
404
405
406 void LOptionContainer::loadRooms(bool throw_error)
407 {
408 if (!throw_error && (per_room_directory_location == ""))
409 return;
410 DIR *d = opendir(per_room_directory_location.c_str());
411 if (d == NULL) {
412 if (throw_error) {
413 E2LOGGER_error("Could not open room definitions directory: ", strerror(errno));
414 exit(1);
415 } else {
416 return;
417 }
418 }
419
420 struct dirent *f;
421 while ((f = readdir(d))) {
422 if (f->d_name[0] == '.')
423 continue;
424 std::string filename(per_room_directory_location);
425 filename.append(f->d_name);
426 DEBUG_debug("Room file found : ", filename);
427 std::ifstream infile(filename.c_str(), std::ios::in);
428 if (!infile.good()) {
429 E2LOGGER_error(" Could not open file room definitions ");
430 exit(1);
431 }
432 DEBUG_debug("Opened room file : ", filename);
433
434 std::string roomname;
435 DEBUG_debug(" Reading room file : ", filename);
436 getline(infile, roomname);
437 if (infile.eof()) {
438 E2LOGGER_error(" Unexpected EOF ", filename);
439 exit(1);
440 }
441 if (infile.fail()) {
442 E2LOGGER_error(" Unexpected failure on read: ", filename);;
443 exit(1);
444 }
445 if (infile.bad()) {
446 E2LOGGER_error(" Unexpected badbit failure on read: ", filename);
447 exit(1);
448 }
449 if (!infile.good()) {
450 E2LOGGER_error(" Could not open file room definitions: ", filename);
451 exit(1);
452 }
453 DEBUG_debug(" Room name is: ", roomname);
454
455 roomname = roomname.substr(1);
456 room_item this_room;
457 this_room.name = roomname;
458 this_room.block = false;
459 this_room.part_block = false;
460 this_room.sitelist = NULL;
461 this_room.urllist = NULL;
462
463 IPList *contents = new IPList();
464 contents->ifsreadIPMelangeList(&infile, true, "#ENDLIST");
465 this_room.iplist = contents;
466 if (infile.eof()) { // is old style room block
467 this_room.block = true;
468 this_room.sitelist = NULL;
469 this_room.urllist = NULL;
470 } else {
471 std::string linestr;
472 String temp;
473 while (infile.good()) {
474 std::getline(infile, linestr);
475 if (infile.eof())
476 break;
477 temp = linestr;
478 if (temp.startsWith("#SITELIST")) {
479 ListContainer *sitelist = new ListContainer();
480 if (sitelist->ifsReadSortItemList(&infile, "", "", true, "#ENDLIST", false, false, 0, filename.c_str())) {
481 this_room.sitelist = sitelist;
482 } else {
483 delete sitelist;
484 }
485 } else if (temp.startsWith("#URLLIST")) {
486 ListContainer *urllist = new ListContainer();
487 if (urllist->ifsReadSortItemList(&infile,"", "", true, "#ENDLIST", false, true, 0, filename.c_str())) {
488 this_room.urllist = urllist;
489 } else {
490 delete urllist;
491 }
492 } else if (temp.startsWith("#BLOCK")) {
493 this_room.block = true;
494 }
495 }
496 }
497 if (this_room.block && (this_room.sitelist || this_room.urllist))
498 this_room.part_block = true;
499 rooms.push_back(this_room);
500 infile.close();
501 if (roomname.size() <= 2) {
502 E2LOGGER_error( "Could not read room from definitions file \"", filename, '"');
503 exit(1);
504 }
505 roomname = roomname.substr(1); // remove leading '#'
506 }
507
508 if (closedir(d) != 0) {
509 if (errno != EINTR) {
510 E2LOGGER_error("Could not close room definitions directory: ", strerror(errno));
511 exit(1);
512 }
513 }
514 }
515
516 void LOptionContainer::deleteRooms()
517 {
518 for (std::list<room_item>::iterator i = rooms.begin(); i != rooms.end(); ++i) {
519 delete i->iplist;
520 if (i->sitelist != NULL)
521 delete i->sitelist;
522 if (i->urllist != NULL)
523 delete i->urllist;
524 }
525 rooms.clear();
526 }
527
528 long int LOptionContainer::findoptionI(const char *option)
529 {
530 long int res = String(findoptionS(option).c_str()).toLong();
531 return res;
532 }
533
534 std::string LOptionContainer::findoptionS(const char *option)
535 {
536 // findoptionS returns a found option stored in the deque
537 String temp;
538 String temp2;
539 String o(option);
540
541 for (std::deque<std::string>::iterator i = conffile.begin(); i != conffile.end(); i++) {
542 if ((*i).empty())
543 continue;
544 temp = (*i).c_str();
545 temp2 = temp.before("=");
546 while (temp2.endsWith(" ")) { // get rid of tailing spaces before =
547 temp2.chop();
548 }
549 if (o == temp2) {
550 temp = temp.after("=");
551 while (temp.startsWith(" ")) { // get rid of heading spaces
552 temp.lop();
553 }
554 if (temp.startsWith("'")) { // inverted commas
555 temp.lop();
556 }
557 while (temp.endsWith(" ")) { // get rid of tailing spaces
558 temp.chop();
559 }
560 if (temp.endsWith("'")) { // inverted commas
561 temp.chop();
562 }
563 return temp.toCharArray();
564 }
565 }
566 return "";
567 }
568
569 std::deque<String> LOptionContainer::findoptionM(const char *option)
570 {
571 // findoptionS returns all the matching options
572 String temp;
573 String temp2;
574 String o(option);
575 std::deque<String> results;
576
577 for (std::deque<std::string>::iterator i = conffile.begin(); i != conffile.end(); i++) {
578 if ((*i).empty())
579 continue;
580 temp = (*i).c_str();
581 temp2 = temp.before("=");
582 while (temp2.endsWith(" ")) { // get rid of tailing spaces before =
583 temp2.chop();
584 }
585 if (o == temp2) {
586 temp = temp.after("=");
587 while (temp.startsWith(" ")) { // get rid of heading spaces
588 temp.lop();
589 }
590 if (temp.startsWith("'")) { // inverted commas
591 temp.lop();
592 }
593 while (temp.endsWith(" ")) { // get rid of tailing spaces
594 temp.chop();
595 }
596 if (temp.endsWith("'")) { // inverted commas
597 temp.chop();
598 }
599 results.push_back(temp);
600 }
601 }
602 return results;
603 }
604
605 #ifdef NOTDEF
606 bool LOptionContainer::realitycheck(long int l, long int minl, long int maxl, const char *emessage)
607 {
608 // realitycheck checks an amount for certain expected criteria
609 // so we can spot problems in the conf files easier
610 if ((l < minl) || ((maxl > 0) && (l > maxl))) {
611 E2LOGGER_error("Config problem; check allowed values for ", emessage);
612 return false;
613 }
614 return true;
615 }
616 #endif
617
618 bool LOptionContainer::readFilterGroupConf()
619 {
620 String prefix(o.config.configfile);
621 prefix = prefix.before(".conf");
622 prefix += "f";
623 String file;
624 ConfigVar groupnamesfile;
625 String groupname;
626 bool need_html = false;
627 bool read_errors = false;
628
629 DEBUG_config("read FilterGroups");
630 if (o.filter.use_group_names_list) {
631 int result = groupnamesfile.readVar(group_names_list_location.c_str(), "=");
632 if (result != 0) {
633 E2LOGGER_error("Error opening group names file: ", group_names_list_location);
634 return false;
635 }
636 }
637 for (int i = 1; i <= o.filter.filter_groups; i++) {
638 file = prefix;
639 file += String(i);
640 file += ".conf";
641 if (o.filter.use_group_names_list) {
642 std::ostringstream groupnum;
643 groupnum << i;
644 groupname = groupnamesfile[groupnum.str().c_str()];
645 if (groupname.length() == 0) {
646 E2LOGGER_error("Group names file too short: ", group_names_list_location);
647 return false;
648 }
649 DEBUG_debug("Group name: ", groupname);
650 }
651 if (!readAnotherFilterGroupConf(file.toCharArray(), groupname.toCharArray(), need_html, i)) {
652 E2LOGGER_warning("Error(s) opening or within filter group config: ", file);
653 read_errors = true;
654 }
655 }
656 if (read_errors && o.lists.abort_on_missing_list) {
657 return false;
658 }
659 return true;
660 }
661
662 bool LOptionContainer::readAnotherFilterGroupConf(const char *filename, const char *groupname, bool &need_html, int fg_no)
663 {
664 DEBUG_debug("adding filter group: ", numfg, " ", filename);
665
666 // array of pointers to FOptionContainer
667 typedef FOptionContainer *PFOptionContainer;
668 FOptionContainer **temp = new PFOptionContainer[numfg + 1];
669 for (int i = 0; i < numfg; i++) {
670 temp[i] = fg[i];
671 }
672 if (numfg > 0) {
673 delete[] fg;
674 }
675 fg = temp;
676 fg[numfg] = new FOptionContainer;
677
678 DEBUG_debug("added filter group: ", numfg, " ", filename);
679
680 // pass all the vars from OptionContainer needed
681 (*fg[numfg]).weighted_phrase_mode = o.naughty.weighted_phrase_mode;
682 (*fg[numfg]).force_quick_search = o.lists.force_quick_search;
683 (*fg[numfg]).reverse_lookups = o.story.reverse_lookups;
684
685 // pass in the group name
686 (*fg[numfg]).name = groupname;
687
688 // pass in the group number
689 (*fg[numfg]).filtergroup = fg_no;
690
691 // pass in the reporting level - can be overridden
692 (*fg[numfg]).reporting_level = reporting_level;
693
694 DEBUG_debug("passed variables to filter group: ", numfg, " ", filename);
695
696 bool rc = (*fg[numfg]).read(filename);
697 DEBUG_debug("reading filter group: ", numfg, " ", filename, " return is ", rc);
698
699 numfg++;
700
701 if (!rc) {
702 return false;
703 }
704 return true;
705 }
706
707 int LOptionContainer::getFgFromName(String &name) {
708 for (int i = 0; i < o.filter.numfg ; i++) {
709 if (name == (*fg[i]).name)
710 return i;
711 }
712 // not found
713 return -1;
714 }