comparison src/context.cpp @ 71:dd21c8e13074

start coding on new config syntax
author carl
date Sat, 09 Jul 2005 19:24:41 -0700
parents
children e6a2d0be7c5e
comparison
equal deleted inserted replaced
70:c812a06f87bf 71:dd21c8e13074
1 #include "context.h"
2
3 static char* context_version="$Id:";
4
5 char *token_black;
6 char *token_content;
7 char *token_context;
8 char *token_dccfrom;
9 char *token_dccto;
10 char *token_default;
11 char *token_dnsbl;
12 char *token_dnsbll;
13 char *token_envfrom;
14 char *token_envto;
15 char *token_filter;
16 char *token_host_limit;
17 char *token_html_limit;
18 char *token_html_tags;
19 char *token_ignore;
20 char *token_include;
21 char *token_inherit;
22 char *token_lbrace;
23 char *token_many;
24 char *token_off;
25 char *token_ok;
26 char *token_ok2;
27 char *token_on;
28 char *token_rbrace;
29 char *token_semi;
30 char *token_soft;
31 char *token_tld;
32 char *token_unknown;
33 char *token_white;
34
35 string_set all_strings; // owns all the strings, only modified by the config loader thread
36
37 DNSBL::DNSBL(char *n, char *s, char *m) {
38 name = n;
39 suffix = s;
40 message = m;
41 }
42
43
44 CONFIG::CONFIG() {
45 reference_count = 0;
46 generation = 0;
47 load_time = 0;
48 default_context = NULL;
49 }
50
51
52 CONFIG::~CONFIG() {
53 for (context_list::iterator i=contexts.begin(); i!=contexts.end(); i++) {
54 CONTEXT *c = *i;
55 delete c;
56 }
57 }
58
59
60 void CONFIG::add_context(CONTEXTP con) {
61 contexts.push_back(con);
62 if (!default_context && !con->get_parent()) {
63 // first global context
64 default_context = con;
65 }
66 }
67
68
69 CONTEXTP CONFIG::find_context(char *to, char *from) {
70 CONTEXTP con = NULL;
71 context_map::iterator i = env_to.find(from);
72 if (i != env_to.end()) {
73 con = (*i).second;
74 return con->find_from_context(from);
75 }
76 char *x = strchr(to, '@');
77 if (x) {
78 x++;
79 i = env_to.find(x);
80 if (i != env_to.end()) {
81 con = (*i).second;
82 return con->find_from_context(from);
83 }
84 }
85 if (default_context) {
86 return default_context->find_from_context(from);
87 }
88 return NULL;
89 }
90
91
92 void CONFIG::dump() {
93 if (default_context) default_context->dump();
94 for (context_list::iterator i=contexts.begin(); i!=contexts.end(); i++) {
95 CONTEXTP c = *i;
96 CONTEXTP p = c->get_parent();
97 if (!p && (c != default_context)) c->dump();
98 }
99 }
100
101
102 CONTEXT::CONTEXT(CONTEXTP parent_, char *name_) {
103 parent = parent_;
104 name = name_;
105 env_from_default = (parent) ? token_inherit : token_unknown;
106 content_filtering = (parent) ? parent->content_filtering : false;
107 content_suffix = NULL;
108 content_message = NULL;
109 host_limit = 0;
110 host_limit_message = NULL;
111 host_random = false;
112 tag_limit = 0;
113 tag_limit_message = NULL;
114 }
115
116
117 CONTEXT::~CONTEXT() {
118 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) {
119 DNSBLP d = (*i).second;
120 // delete the underlying DNSBL objects.
121 delete d;
122 }
123 }
124
125
126 char *CONTEXT::get_full_name(char *buffer, int size) {
127 if (!parent) return name;
128 const int maxlen = 1000;
129 char buf[maxlen];
130 snprintf(buffer, size, "%s.%s", parent->get_full_name(buf, maxlen), name);
131 return buffer;
132 }
133
134
135 bool CONTEXT::cover_env_to(char *to) {
136 const int maxlen = 1000;
137 char buffer[maxlen];
138 char *x = strchr(to, '@');
139 if (x) x++;
140 else x = to;
141 string_set::iterator i = env_to.find(x);
142 if (i != env_to.end()) return true;
143 return (parent) ? parent->cover_env_to(to) : false;
144 }
145
146
147 char *CONTEXT::find_from(char *from) {
148 // do we have a white/black/unknown for this full from value?
149 string_map::iterator i = env_from.find(from);
150 if (i != env_from.end()) return (*i).second;
151 // do we have a white/black/unknown for the source domain name?
152 char *x = strchr(from, '@');
153 if (x) {
154 x++;
155 i = env_from.find(x);
156 if (i != env_from.end()) return (*i).second;
157 }
158 if ((env_from_default == token_inherit) && parent) {
159 return parent->find_from(from);
160 }
161 return env_from_default;
162 }
163
164
165 CONTEXTP CONTEXT::find_from_context(char *from) {
166 // do we have a special child context for this full from value?
167 context_map::iterator j = env_from_context.find(from);
168 if (j != env_from_context.end()) {
169 CONTEXTP con = (*j).second;
170 return con->find_from_context(from);
171 }
172 char *x = strchr(from, '@');
173 if (x) {
174 x++;
175 // do we have a special context for the source domain name?
176 j = env_from_context.find(x);
177 if (j != env_from_context.end()) {
178 CONTEXTP con = (*j).second;
179 return con->find_from_context(from);
180 }
181 }
182 return this;
183 }
184
185
186 CONTEXTP CONTEXT::find_from_context_name(char *name) {
187 context_map::iterator i = children.find(name);
188 if (i != children.end()) return (*i).second;
189 return NULL;
190 }
191
192
193 DNSBLP CONTEXT::find_dnsbl(char *name) {
194 dnsblp_map::iterator i = dnsbl_names.find(name);
195 if (i != dnsbl_names.end()) return (*i).second;
196 if (parent) return parent->find_dnsbl(name);
197 return NULL;
198 }
199
200
201 void CONTEXT::dump(int level) {
202 const int maxlen = 1000;
203 char indent[maxlen];
204 int i = min(maxlen-1, level*4);
205 memset(indent, ' ', i);
206 indent[i] = '\0';
207 printf("%s context %s { \n", indent, name);
208
209 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) {
210 char *n = (*i).first;
211 DNSBL &d = *(*i).second;
212 printf("%s dnsbl %s %s \"%s\"; \n", indent, n, d.suffix, d.message);
213 }
214
215 if (!dnsbl_list.empty()) {
216 printf("%s dnsbl_list", indent);
217 for (dnsblp_list::iterator i=dnsbl_list.begin(); i!=dnsbl_list.end(); i++) {
218 DNSBL &d = *(*i);
219 printf(" %s", d.name);
220 }
221 printf("; \n");
222 }
223
224 if (content_filtering) {
225 printf("%s content on { \n", indent, env_from_default);
226 if (content_suffix) {
227 printf("%s filter %s \"%s\"; \n", indent, content_suffix, content_message);
228 }
229 if (!content_host_ignore.empty()) {
230 printf("%s ignore { \n", indent);
231 for (string_set::iterator i=content_host_ignore.begin(); i!=content_host_ignore.end(); i++) {
232 printf("%s %s; \n", indent, *i);
233 }
234 printf("%s }; \n", indent);
235 }
236 if (!content_tlds.empty()) {
237 printf("%s tld { \n", indent);
238 printf("%s ", indent);
239 for (string_set::iterator i=content_tlds.begin(); i!=content_tlds.end(); i++) {
240 printf("%s; ", *i);
241 }
242 printf("\n%s }; \n", indent);
243 }
244 if (!html_tags.empty()) {
245 printf("%s html_tags { \n", indent);
246 printf("%s ", indent);
247 for (string_set::iterator i=html_tags.begin(); i!=html_tags.end(); i++) {
248 printf("%s; ", *i);
249 }
250 printf("\n%s }; \n", indent);
251 }
252 if (host_limit_message) {
253 printf("%s host_limit on %d \"%s\"; \n", indent, host_limit, host_limit_message);
254 }
255 else if (host_random) {
256 printf("%s host_limit soft %d; \n", indent, host_limit);
257 }
258 else {
259 printf("%s host_limit off; \n", indent);
260 }
261 if (tag_limit_message) {
262 printf("%s tag_limit on %d \"%s\"; \n", indent, tag_limit, tag_limit_message);
263 }
264 else {
265 printf("%s tag_limit off; \n", indent);
266 }
267 printf("%s }; \n", indent);
268 }
269 else {
270 printf("%s content off {}; \n", indent, env_from_default);
271 }
272
273 printf("%s env_to { \n", indent);
274 for (string_set::iterator i=env_to.begin(); i!=env_to.end(); i++) {
275 printf("%s %s; \n", indent, *i);
276 }
277 printf("%s }; \n", indent);
278
279 for (context_map::iterator i=children.begin(); i!=children.end(); i++) {
280 CONTEXTP c = (*i).second;
281 c->dump(level+1);
282 }
283
284 printf("%s env_from %s { \n", indent, env_from_default);
285 if (!env_from.empty()) {
286 printf("%s // white/black/unknown \n", indent);
287 for (string_map::iterator i=env_from.begin(); i!=env_from.end(); i++) {
288 char *f = (*i).first;
289 char *t = (*i).second;
290 printf("%s %s \t %s; \n", indent, f, t);
291 }
292 }
293 if (!env_from_context.empty()) {
294 printf("%s // child contexts \n", indent);
295 for (context_map::iterator j=env_from_context.begin(); j!=env_from_context.end(); j++) {
296 char *f = (*j).first;
297 CONTEXTP t = (*j).second;
298 printf("%s %s \t %s; \n", indent, f, t->name);
299 }
300 }
301 printf("%s }; \n", indent);
302
303 printf("%s }; \n", indent);
304 }
305
306
307 ////////////////////////////////////////////////
308 // helper to discard the strings held by a string_set
309 //
310 static void discard(string_set &s) {
311 for (string_set::iterator i=s.begin(); i!=s.end(); i++) {
312 free(*i);
313 }
314 s.clear();
315 }
316
317
318 ////////////////////////////////////////////////
319 // helper to register a string in a string set
320 //
321 char* register_string(string_set &s, char *name) {
322 string_set::iterator i = s.find(name);
323 if (i != s.end()) return *i;
324 char *x = strdup(name);
325 s.insert(x);
326 return x;
327 }
328
329
330 ////////////////////////////////////////////////
331 // register a global string
332 //
333 char* register_string(char *name) {
334 return register_string(all_strings, name);
335 }
336
337
338 ////////////////////////////////////////////////
339 //
340 bool tsa(TOKEN &tok, char *token);
341 bool tsa(TOKEN &tok, char *token) {
342 char *have = tok.next();
343 if (have == token) return true;
344 tok.token_error(token, have);
345 return false;
346 }
347
348
349 ////////////////////////////////////////////////
350 //
351 bool parse_dnsbl(TOKEN &tok, CONFIG &dc, CONTEXT &me);
352 bool parse_dnsbl(TOKEN &tok, CONFIG &dc, CONTEXT &me) {
353 char *name = tok.next();
354 char *suf = tok.next();
355 char *msg = tok.next();
356 if (!tsa(tok, token_semi)) return false;
357 DNSBLP dns = new DNSBL(name, suf, msg);
358 me.add_dnsbl(name, dns);
359 return true;
360 }
361
362
363 ////////////////////////////////////////////////
364 //
365 bool parse_dnsbll(TOKEN &tok, CONFIG &dc, CONTEXT &me);
366 bool parse_dnsbll(TOKEN &tok, CONFIG &dc, CONTEXT &me) {
367 while (true) {
368 char *have = tok.next();
369 if (!have) break;
370 if (have == token_semi) break;
371 DNSBLP dns = me.find_dnsbl(have);
372 if (dns) {
373 me.add_dnsbl(dns);
374 }
375 else {
376 tok.token_error("dnsbl name", have);
377 return false;
378 }
379 }
380 return true;
381 }
382
383
384 ////////////////////////////////////////////////
385 //
386 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me);
387 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me) {
388 char *setting = tok.next();
389 if (setting == token_on) {
390 me.set_content_filtering(true);
391 }
392 else if (setting == token_off) {
393 me.set_content_filtering(false);
394 }
395 else {
396 tok.token_error("on/off", setting);
397 return false;
398 }
399 if (!tsa(tok, token_lbrace)) return false;
400 while (true) {
401 char *have = tok.next();
402 if (!have) break;
403 if (have == token_filter) {
404 me.set_content_suffix(tok.next());
405 me.set_content_message(tok.next());
406 if (!tsa(tok, token_semi)) return false;
407 }
408 else if (have == token_ignore) {
409 if (!tsa(tok, token_lbrace)) return false;
410 while (true) {
411 if (!have) break;
412 char *have = tok.next();
413 if (have == token_rbrace) {
414 break; // done
415 }
416 else {
417 me.add_ignore(have);
418 }
419 }
420 if (!tsa(tok, token_semi)) return false;
421 }
422 else if (have == token_tld) {
423 if (!tsa(tok, token_lbrace)) return false;
424 while (true) {
425 char *have = tok.next();
426 if (!have) break;
427 if (have == token_rbrace) {
428 break; // done
429 }
430 else {
431 me.add_tld(have);
432 }
433 }
434 if (!tsa(tok, token_semi)) return false;
435 }
436 else if (have == token_html_limit) {
437 have = tok.next();
438 if (have == token_on) {
439 me.set_tag_limit(tok.nextint());
440 me.set_tag_message(tok.next());
441 }
442 else if (have == token_off) {
443 me.set_tag_limit(0);
444 me.set_tag_message(NULL);
445 }
446 else {
447 tok.token_error("on/off", have);
448 return false;
449 }
450 if (!tsa(tok, token_semi)) return false;
451 }
452 else if (have == token_html_tags) {
453 if (!tsa(tok, token_lbrace)) return false;
454 while (true) {
455 char *have = tok.next();
456 if (!have) break;
457 if (have == token_rbrace) {
458 break; // done
459 }
460 else {
461 me.add_tag(have);
462 }
463 }
464 if (!tsa(tok, token_semi)) return false;
465 }
466 else if (have == token_host_limit) {
467 have = tok.next();
468 if (have == token_on) {
469 me.set_host_limit(tok.nextint());
470 me.set_host_message(tok.next());
471 me.set_host_random(false);
472 }
473 else if (have == token_off) {
474 me.set_host_limit(0);
475 me.set_host_message(NULL);
476 me.set_host_random(false);
477 }
478 else if (have == token_soft) {
479 me.set_host_limit(tok.nextint());
480 me.set_host_message(NULL);
481 me.set_host_random(true);
482 }
483 else {
484 tok.token_error("on/off/soft", have);
485 return false;
486 }
487 if (!tsa(tok, token_semi)) return false;
488 }
489 else if (have == token_rbrace) {
490 break; // done
491 }
492 else {
493 tok.token_error("content keyword", have);
494 return false;
495 }
496 }
497 return tsa(tok, token_semi);
498 }
499
500
501 ////////////////////////////////////////////////
502 //
503 bool parse_envto(TOKEN &tok, CONFIG &dc, CONTEXT &me);
504 bool parse_envto(TOKEN &tok, CONFIG &dc, CONTEXT &me) {
505 if (!tsa(tok, token_lbrace)) return false;
506 while (true) {
507 char *have = tok.next();
508 if (!have) break;
509 if (have == token_rbrace) break;
510 if (have == token_semi) {
511 // optional separators
512 }
513 else if (have == token_dccto) {
514 char *flavor = tok.next();
515 if (!tsa(tok, token_lbrace)) return false;
516 bool keeping = false;
517 while (true) {
518 char *have = tok.next();
519 if (!have) break;
520 if (have == token_rbrace) break;
521 if (have == flavor) {
522 keeping = true;
523 continue;
524 }
525 else if ((have == token_ok) || (have == token_ok2) || (have == token_many)) {
526 keeping = false;
527 continue;
528 }
529 if (have == token_envto) {
530 have = tok.next();
531 if (keeping) {
532 if (me.allow_env_to(have)) {
533 me.add_to(have);
534 dc.add_to(have, &me);
535 }
536 }
537 }
538 tok.skipeol();
539 }
540 }
541 else if (me.allow_env_to(have)) {
542 me.add_to(have);
543 dc.add_to(have, &me);
544 }
545 else {
546 tok.token_error("valid env_to address or domain name", have);
547 return false;
548 }
549 }
550 return tsa(tok, token_semi);
551 }
552
553
554 ////////////////////////////////////////////////
555 //
556 bool parse_envfrom(TOKEN &tok, CONFIG &dc, CONTEXT &me);
557 bool parse_envfrom(TOKEN &tok, CONFIG &dc, CONTEXT &me) {
558 char *st = tok.next();
559 if ((st == token_black) || (st == token_white) || (st == token_unknown)) {
560 me.set_from_default(st);
561 }
562 else {
563 tok.push(st);
564 }
565 if (!tsa(tok, token_lbrace)) return false;
566 while (true) {
567 char *have = tok.next();
568 if (!have) break;
569 if (have == token_rbrace) break;
570 if (have == token_semi) {
571 // optional separators
572 }
573 else if (have == token_dccfrom) {
574 if (!tsa(tok, token_lbrace)) return false;
575 bool keeping = false;
576 bool many = false;
577 while (true) {
578 char *have = tok.next();
579 if (!have) break;
580 if (have == token_rbrace) break;
581 if (have == token_ok) {
582 keeping = true;
583 many = false;
584 continue;
585 }
586 else if (have == token_many) {
587 keeping = true;
588 many = true;
589 continue;
590 }
591 else if (have == token_ok2) {
592 keeping = false;
593 continue;
594 }
595 if (have == token_envfrom) {
596 have = tok.next();
597 if (keeping) {
598 me.add_from(have, (many) ? token_black : token_white);
599 }
600 }
601 tok.skipeol();
602 }
603 }
604 else {
605 // may be a valid email address or domain name
606 char *st = tok.next();
607 if ((st == token_black) || (st == token_white) || (st == token_unknown)) {
608 me.add_from(have, st);
609 }
610 else {
611 CONTEXTP con = me.find_from_context_name(st);
612 if (con) {
613 me.add_from_context(have, con);
614 }
615 else {
616 tok.token_error("white/black/unknown or child context name", st);
617 return false;
618 }
619 }
620 }
621 }
622 return tsa(tok, token_semi);
623 }
624
625
626 ////////////////////////////////////////////////
627 //
628 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent);
629 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent) {
630 char *name = tok.next();
631 if (!tsa(tok, token_lbrace)) return false;
632 CONTEXTP con = new CONTEXT(parent, name);
633
634 while (true) {
635 char *have = tok.next();
636 if (!have) break;
637 if (have == token_rbrace) break; // done
638 if (have == token_dnsbl) {
639 if (!parse_dnsbl(tok, dc, *con)) return false;
640 }
641 else if (have == token_dnsbll) {
642 if (!parse_dnsbll(tok, dc, *con)) return false;
643 }
644 else if (have == token_content) {
645 if (!parse_content(tok, dc, *con)) return false;
646 }
647 else if (have == token_envto) {
648 if (!parse_envto(tok, dc, *con)) return false;
649 }
650 else if (have == token_envfrom) {
651 if (!parse_envfrom(tok, dc, *con)) return false;
652 }
653 else if (have == token_context) {
654 if (!parse_context(tok, dc, con)) return false;
655 }
656 else {
657 tok.token_error("context keyword", have);
658 return false;
659 }
660 }
661
662 if (!tsa(tok, token_semi)) {
663 delete con;
664 return false;
665 }
666 dc.add_context(con);
667 if (parent) parent->add_context(con);
668 return true;
669 }
670
671
672 ////////////////////////////////////////////////
673 // parse a config file
674 //
675 bool load_conf(CONFIG &dc, char *fn) {
676 TOKEN tok(fn, &dc.config_files);
677 while (true) {
678 char *have = tok.next();
679 if (!have) break;
680 if (have == token_context) {
681 if (!parse_context(tok, dc, NULL)) {
682 return false;
683 }
684 }
685 else {
686 tok.token_error(token_context, have);
687 return false;
688 }
689 }
690 return true;
691 }
692
693
694 ////////////////////////////////////////////////
695 // init the tokens
696 //
697 void token_init() {
698 token_black = register_string("black");
699 token_content = register_string("content");
700 token_context = register_string("context");
701 token_dccfrom = register_string("dcc_from");
702 token_dccto = register_string("dcc_to");
703 token_default = register_string("default");
704 token_dnsbl = register_string("dnsbl");
705 token_dnsbll = register_string("dnsbl_list");
706 token_envfrom = register_string("env_from");
707 token_envto = register_string("env_to");
708 token_filter = register_string("filter");
709 token_host_limit = register_string("host_limit");
710 token_html_limit = register_string("html_limit");
711 token_html_tags = register_string("html_tags");
712 token_ignore = register_string("ignore");
713 token_include = register_string("include");
714 token_inherit = register_string("inherit");
715 token_lbrace = register_string("{");
716 token_many = register_string("many");
717 token_off = register_string("off");
718 token_ok = register_string("ok");
719 token_ok2 = register_string("ok2");
720 token_on = register_string("on");
721 token_rbrace = register_string("}");
722 token_semi = register_string(";");
723 token_soft = register_string("soft");
724 token_tld = register_string("tld");
725 token_unknown = register_string("unknown");
726 token_white = register_string("white");
727 }