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