Mercurial > dnsbl
annotate src/context.cpp @ 145:9b9bab1d3c21 stable-5-25
dump effective dnsbl_list with -c switch
author | carl |
---|---|
date | Sun, 15 Oct 2006 12:41:46 -0700 |
parents | 31ff00ea6bfb |
children | 7278c9766e26 |
rev | line source |
---|---|
94 | 1 /* |
2 | |
143 | 3 Copyright (c) 2006 Carl Byington - 510 Software Group, released under |
94 | 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" | |
10 | |
11 // needed for socket io | |
96
1edd4e8d3a60
fix missing include, not all systems define HOST_NAME_MAX
carl
parents:
94
diff
changeset
|
12 #include <unistd.h> |
94 | 13 #include <sys/ioctl.h> |
14 #include <net/if.h> | |
15 #include <arpa/inet.h> | |
16 #include <netinet/in.h> | |
17 #include <netinet/tcp.h> | |
18 #include <netdb.h> | |
19 #include <sys/socket.h> | |
20 #include <sys/un.h> | |
21 | |
22 static char* context_version="$Id$"; | |
23 | |
24 char *token_black; | |
25 char *token_content; | |
26 char *token_context; | |
27 char *token_dccfrom; | |
28 char *token_dccto; | |
29 char *token_default; | |
30 char *token_dnsbl; | |
31 char *token_dnsbll; | |
32 char *token_envfrom; | |
33 char *token_envto; | |
34 char *token_filter; | |
35 char *token_host_limit; | |
36 char *token_html_limit; | |
37 char *token_html_tags; | |
38 char *token_ignore; | |
39 char *token_include; | |
40 char *token_inherit; | |
41 char *token_lbrace; | |
42 char *token_mailhost; | |
43 char *token_many; | |
44 char *token_off; | |
45 char *token_ok2; | |
46 char *token_ok; | |
47 char *token_on; | |
136 | 48 char *token_rate; |
94 | 49 char *token_rbrace; |
50 char *token_semi; | |
51 char *token_soft; | |
52 char *token_substitute; | |
53 char *token_tld; | |
117 | 54 char *token_cctld; |
94 | 55 char *token_unknown; |
119 | 56 char *token_uribl; |
94 | 57 char *token_verify; |
58 char *token_white; | |
59 | |
60 char *token_myhostname; | |
96
1edd4e8d3a60
fix missing include, not all systems define HOST_NAME_MAX
carl
parents:
94
diff
changeset
|
61 #ifndef HOST_NAME_MAX |
1edd4e8d3a60
fix missing include, not all systems define HOST_NAME_MAX
carl
parents:
94
diff
changeset
|
62 #define HOST_NAME_MAX 255 |
1edd4e8d3a60
fix missing include, not all systems define HOST_NAME_MAX
carl
parents:
94
diff
changeset
|
63 #endif |
94 | 64 char myhostname[HOST_NAME_MAX+1]; |
65 | |
66 verify_map verifiers; | |
67 string_set all_strings; // owns all the strings, only modified by the config loader thread | |
68 const int maxlen = 1000; // used for snprintf buffers | |
69 const int maxage = 120; // smtp verify sockets older than this are ancient | |
70 extern int NULL_SOCKET; | |
129
c5cd1261394d
ignore smtp connection attempts for 10 minutes when getting connection errors on verify hosts
carl
parents:
119
diff
changeset
|
71 const time_t ERROR_SMTP_SOCKET_TIME = 600; // number of seconds between attempts to open a socket to an smtp server |
94 | 72 |
73 | |
74 int SMTP::writer() { | |
75 #ifdef VERIFY_DEBUG | |
76 log("writer() sees buffer with %s", buffer); | |
77 log("writer() sees error %d", (int)error); | |
78 #endif | |
79 int rs = 0; | |
80 if (!error) { | |
81 int len = strlen(buffer); | |
82 while (rs < len) { | |
83 int ws = write(fd, buffer+rs, len-rs); | |
84 if (ws > 0) { | |
85 rs += ws; | |
86 } | |
87 else { | |
88 // peer closed the socket! | |
89 rs = 0; | |
90 error = true; | |
91 break; | |
92 } | |
93 } | |
94 } | |
95 return rs; | |
96 } | |
97 | |
98 | |
99 int SMTP::reader() { | |
100 // read some bytes terminated by lf or end of buffer. | |
101 // we may have a multi line response or part thereof in the buffer. | |
102 #ifdef VERIFY_DEBUG | |
103 log("reader() sees error %d", (int)error); | |
104 #endif | |
105 if (error) return 0; | |
106 int len = maxlen-1; // room for null terminator | |
107 while (pending < len) { | |
108 int ws = read(fd, buffer+pending, len-pending); | |
109 if (ws > 0) { | |
110 pending += ws; | |
111 if (buffer[pending-1] == '\n') break; | |
112 } | |
113 else { | |
114 // peer closed the socket! | |
115 pending = 0; | |
116 error = true; | |
117 break; | |
118 } | |
119 } | |
120 buffer[pending] = '\0'; | |
121 #ifdef VERIFY_DEBUG | |
122 log("reader() sees buffer with %s", buffer); | |
123 #endif | |
124 return pending; | |
125 } | |
126 | |
127 | |
128 int SMTP::read_line() { | |
129 char *lf = strchr(buffer, '\n'); | |
130 if (!lf) { | |
131 reader(); // get a lf | |
132 lf = strchr(buffer, '\n'); | |
133 if (!lf) lf = buffer + pending - 1; | |
134 } | |
135 return (lf-buffer)+1; // number of bytes in this line | |
136 } | |
137 | |
138 | |
97 | 139 void SMTP::flush_line(int r) { |
94 | 140 if (pending > r) memmove(buffer, buffer+r, pending-r); |
141 pending -= r; | |
142 } | |
143 | |
144 | |
145 int SMTP::read_response() { | |
146 pending = 0; | |
147 buffer[pending] = '\0'; | |
148 while (true) { | |
149 int r = read_line(); | |
150 #ifdef VERIFY_DEBUG | |
151 log("read_response() sees line with %s", buffer); | |
152 log("read_response() sees line length %d", r); | |
153 #endif | |
154 if (r == 0) return 0; // failed to read any bytes | |
155 if ((r > 4) && (buffer[3] == '-')) { | |
156 flush_line(r); | |
157 continue; | |
158 } | |
159 return atoi(buffer); | |
160 } | |
161 return 0; | |
162 } | |
163 | |
164 | |
165 int SMTP::cmd(char *c) { | |
166 if (c) { | |
167 init(); | |
168 append(c); | |
169 } | |
170 append("\r\n"); | |
171 writer(); | |
172 return read_response(); | |
173 } | |
174 | |
175 | |
176 int SMTP::helo() { | |
177 if (read_response() != 220) return 0; | |
178 init(); | |
179 append("HELO "); | |
180 append(token_myhostname); | |
181 return cmd(NULL); | |
182 } | |
183 | |
184 | |
185 int SMTP::rset() { | |
186 int rc = cmd("RSET"); | |
187 efrom[0] = '\0'; | |
188 return rc; | |
189 } | |
190 | |
191 | |
192 int SMTP::from(char *f) { | |
101 | 193 // the mail from address was originally passed in from sendmail enclosed in |
194 // <>. to_lower_string() removed the <> and converted the rest to lowercase, | |
195 // except in the case of an empty return path, which was left as the two | |
196 // character string <>. | |
94 | 197 if (strncmp(efrom, f, maxlen)) { |
198 rset(); | |
199 strncpy(efrom, f, maxlen); | |
200 init(); | |
201 append("MAIL FROM:<"); | |
101 | 202 if (*f != '<') append(f); |
94 | 203 append(">"); |
204 return cmd(NULL); | |
205 } | |
206 return 250; // pretend it worked | |
207 } | |
208 | |
209 | |
210 int SMTP::rcpt(char *t) { | |
211 init(); | |
212 append("RCPT TO:<"); | |
213 append(t); | |
214 append(">"); | |
215 return cmd(NULL); | |
216 } | |
217 | |
218 | |
219 int SMTP::quit() { | |
220 return cmd("QUIT"); | |
221 } | |
222 | |
223 | |
224 void SMTP::closefd() { | |
225 shutdown(fd, SHUT_RDWR); | |
226 close(fd); | |
227 } | |
228 | |
229 | |
230 #ifdef VERIFY_DEBUG | |
231 void SMTP::log(char *m, int v) { | |
232 char buf[maxlen]; | |
233 snprintf(buf, maxlen, m, v); | |
234 my_syslog(buf); | |
235 } | |
236 | |
237 | |
238 void SMTP::log(char *m, char *v) { | |
239 char buf[maxlen]; | |
240 snprintf(buf, maxlen, m, v); | |
241 my_syslog(buf); | |
242 } | |
243 #endif | |
244 | |
245 | |
246 VERIFY::VERIFY(char *h) { | |
247 host = h; | |
248 last_err = 0; | |
249 pthread_mutex_init(&mutex, 0); | |
250 } | |
251 | |
252 | |
253 void VERIFY::closer() { | |
254 bool ok = true; | |
255 while (ok) { | |
256 SMTP *conn = NULL; | |
257 pthread_mutex_lock(&mutex); | |
258 if (connections.empty()) { | |
259 ok = false; | |
260 } | |
261 else { | |
262 conn = connections.front(); | |
263 time_t now = time(NULL); | |
264 if ((now - conn->get_stamp()) > maxage) { | |
265 // this connection is ancient, remove it | |
266 connections.pop_front(); | |
267 } | |
268 else { | |
269 ok = false; | |
270 conn = NULL; | |
271 } | |
272 } | |
273 pthread_mutex_unlock(&mutex); | |
274 // avoid doing this work inside the mutex lock | |
275 if (conn) { | |
276 #ifdef VERIFY_DEBUG | |
277 conn->log("closer() closes ancient %d", conn->get_fd()); | |
278 #endif | |
279 delete conn; | |
280 } | |
281 } | |
282 } | |
283 | |
284 | |
285 SMTP* VERIFY::get_connection() { | |
286 SMTP *conn = NULL; | |
287 pthread_mutex_lock(&mutex); | |
288 if (!connections.empty()) { | |
289 conn = connections.front(); | |
290 connections.pop_front(); | |
291 #ifdef VERIFY_DEBUG | |
292 conn->log("get_connection() %d from cache", conn->get_fd()); | |
293 #endif | |
294 } | |
295 pthread_mutex_unlock(&mutex); | |
296 if (conn) return conn; | |
297 int sock = NULL_SOCKET; | |
129
c5cd1261394d
ignore smtp connection attempts for 10 minutes when getting connection errors on verify hosts
carl
parents:
119
diff
changeset
|
298 if ((time(NULL) - last_err) > ERROR_SMTP_SOCKET_TIME) { |
94 | 299 // nothing recent, maybe this time it will work |
300 hostent *h = gethostbyname(host); | |
301 if (h) { | |
302 sockaddr_in server; | |
303 server.sin_family = h->h_addrtype; | |
304 server.sin_port = htons(25); | |
305 memcpy(&server.sin_addr, h->h_addr_list[0], h->h_length); | |
306 sock = socket(PF_INET, SOCK_STREAM, 0); | |
307 if (sock != NULL_SOCKET) { | |
308 bool rc = (connect(sock, (sockaddr *)&server, sizeof(server)) == 0); | |
309 if (!rc) { | |
310 shutdown(sock, SHUT_RDWR); | |
311 close(sock); | |
312 sock = NULL_SOCKET; | |
129
c5cd1261394d
ignore smtp connection attempts for 10 minutes when getting connection errors on verify hosts
carl
parents:
119
diff
changeset
|
313 last_err = time(NULL); |
94 | 314 } |
315 } | |
129
c5cd1261394d
ignore smtp connection attempts for 10 minutes when getting connection errors on verify hosts
carl
parents:
119
diff
changeset
|
316 else last_err = time(NULL); |
94 | 317 } |
129
c5cd1261394d
ignore smtp connection attempts for 10 minutes when getting connection errors on verify hosts
carl
parents:
119
diff
changeset
|
318 else last_err = time(NULL); |
94 | 319 } |
320 if (sock != NULL_SOCKET) { | |
321 conn = new SMTP(sock); | |
322 #ifdef VERIFY_DEBUG | |
323 conn->log("get_connection() %d new socket", conn->get_fd()); | |
324 #endif | |
325 if (conn->helo() == 250) return conn; | |
326 delete conn; | |
327 } | |
328 return NULL; | |
329 } | |
330 | |
331 | |
332 void VERIFY::put_connection(SMTP *conn) { | |
333 if (conn->err()) { | |
334 #ifdef VERIFY_DEBUG | |
335 conn->log("put_socket() %d with error, close it", conn->get_fd()); | |
336 #endif | |
337 delete conn; | |
338 last_err = time(NULL); | |
339 } | |
340 else { | |
341 #ifdef VERIFY_DEBUG | |
342 conn->log("put_socket() %d", conn->get_fd()); | |
343 #endif | |
344 conn->now(); | |
345 pthread_mutex_lock(&mutex); | |
346 connections.push_back(conn); | |
347 pthread_mutex_unlock(&mutex); | |
348 } | |
349 } | |
350 | |
351 | |
352 bool VERIFY::ok(char *from, char *to) { | |
353 if (host == token_myhostname) return true; | |
354 SMTP *conn = get_connection(); | |
355 if (!conn) return true; // cannot verify right now, we have socket errors | |
356 int rc; | |
357 rc = conn->from(from); | |
358 #ifdef VERIFY_DEBUG | |
359 conn->log("verify::ok() from sees %d", rc); | |
360 #endif | |
361 if (rc != 250) { | |
362 conn->rset(); | |
363 put_connection(conn); | |
364 return (rc >= 500) ? false : true; | |
365 } | |
366 rc = conn->rcpt(to); | |
367 #ifdef VERIFY_DEBUG | |
368 conn->log("verify::ok() rcpt sees %d", rc); | |
369 #endif | |
370 put_connection(conn); | |
371 return (rc >= 500) ? false : true; | |
372 } | |
373 | |
374 | |
375 DNSBL::DNSBL(char *n, char *s, char *m) { | |
376 name = n; | |
377 suffix = s; | |
378 message = m; | |
379 } | |
380 | |
381 | |
382 bool DNSBL::operator==(const DNSBL &rhs) { | |
383 return (strcmp(name, rhs.name) == 0) && | |
384 (strcmp(suffix, rhs.suffix) == 0) && | |
385 (strcmp(message, rhs.message) == 0); | |
386 } | |
387 | |
388 | |
389 CONFIG::CONFIG() { | |
390 reference_count = 0; | |
391 generation = 0; | |
392 load_time = 0; | |
393 default_context = NULL; | |
394 } | |
395 | |
396 | |
397 CONFIG::~CONFIG() { | |
398 for (context_list::iterator i=contexts.begin(); i!=contexts.end(); i++) { | |
399 CONTEXT *c = *i; | |
400 delete c; | |
401 } | |
402 } | |
403 | |
404 | |
405 void CONFIG::add_context(CONTEXTP con) { | |
406 contexts.push_back(con); | |
407 if (!default_context && !con->get_parent()) { | |
408 // first global context | |
409 default_context = con; | |
410 } | |
411 } | |
412 | |
413 | |
414 void CONFIG::add_to(char *to, CONTEXTP con) { | |
415 context_map::iterator i = env_to.find(to); | |
416 if (i != env_to.end()) { | |
417 CONTEXTP c = (*i).second; | |
418 int s = strlen(to); | |
419 bool at = s && (to[s-1] == '@'); | |
420 if (at && con->is_parent(c->get_parent())) { | |
421 if (debug_syslog) { | |
422 char oldname[maxlen]; | |
423 char newname[maxlen]; | |
424 char *oldn = c->get_full_name(oldname, maxlen); | |
425 char *newn = con->get_full_name(newname, maxlen); | |
426 char buf[maxlen*3]; | |
427 snprintf(buf, maxlen*3, "both %s and %s claim envelope to %s, the first one wins", oldn, newn, to); | |
428 my_syslog(buf); | |
429 } | |
430 return; // don't take over user@ entries from your ancestors children | |
431 } | |
432 if ((c != con) && (c != con->get_parent())) { | |
433 if (debug_syslog) { | |
434 char oldname[maxlen]; | |
435 char newname[maxlen]; | |
436 char *oldn = c->get_full_name(oldname, maxlen); | |
437 char *newn = con->get_full_name(newname, maxlen); | |
438 char buf[maxlen*3]; | |
439 snprintf(buf, maxlen*3, "both %s and %s claim envelope to %s, the second one wins", oldn, newn, to); | |
440 my_syslog(buf); | |
441 } | |
442 } | |
443 } | |
444 env_to[to] = con; | |
445 } | |
446 | |
447 | |
448 CONTEXTP CONFIG::find_context(char *to) { | |
449 context_map::iterator i = env_to.find(to); | |
117 | 450 if (i != env_to.end()) return (*i).second; // found user@domain key |
94 | 451 char *x = strchr(to, '@'); |
452 if (x) { | |
453 x++; | |
454 i = env_to.find(x); | |
117 | 455 if (i != env_to.end()) return (*i).second; // found domain key |
94 | 456 char y = *x; |
457 *x = '\0'; | |
458 i = env_to.find(to); | |
459 *x = y; | |
460 if (i != env_to.end()) return (*i).second; // found user@ key | |
461 } | |
462 return default_context; | |
463 } | |
464 | |
465 | |
466 void CONFIG::dump() { | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
467 if (default_context) default_context->dump(true); |
94 | 468 for (context_list::iterator i=contexts.begin(); i!=contexts.end(); i++) { |
469 CONTEXTP c = *i; | |
470 CONTEXTP p = c->get_parent(); | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
471 if (!p && (c != default_context)) c->dump(false); |
94 | 472 } |
473 char buf[maxlen]; | |
474 for (context_map::iterator i=env_to.begin(); i!=env_to.end(); i++) { | |
475 char *to = (*i).first; | |
476 CONTEXTP con = (*i).second; | |
477 printf("// envelope to %s \t-> context %s \n", to, con->get_full_name(buf,maxlen)); | |
478 } | |
479 } | |
480 | |
481 | |
482 CONTEXT::CONTEXT(CONTEXTP parent_, char *name_) { | |
483 parent = parent_; | |
484 name = name_; | |
485 verify_host = NULL; | |
486 env_from_default = (parent) ? token_inherit : token_unknown; | |
487 content_filtering = (parent) ? parent->content_filtering : false; | |
488 content_suffix = NULL; | |
489 content_message = NULL; | |
119 | 490 uribl_suffix = NULL; |
491 uribl_message = NULL; | |
94 | 492 host_limit = (parent) ? parent->host_limit : 0; |
493 host_limit_message = NULL; | |
494 host_random = (parent) ? parent->host_random : false; | |
495 tag_limit = (parent) ? parent->tag_limit : 0; | |
496 tag_limit_message = NULL; | |
140 | 497 default_rcpt_rate = INT_MAX; |
94 | 498 } |
499 | |
500 | |
501 CONTEXT::~CONTEXT() { | |
502 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) { | |
503 DNSBLP d = (*i).second; | |
504 // delete the underlying DNSBL objects. | |
505 delete d; | |
506 } | |
507 } | |
508 | |
509 | |
510 bool CONTEXT::is_parent(CONTEXTP p) { | |
511 if (p == parent) return true; | |
512 if (!parent) return false; | |
513 return parent->is_parent(p); | |
514 } | |
515 | |
516 | |
517 char *CONTEXT::get_full_name(char *buffer, int size) { | |
518 if (!parent) return name; | |
519 char buf[maxlen]; | |
520 snprintf(buffer, size, "%s.%s", parent->get_full_name(buf, maxlen), name); | |
521 return buffer; | |
522 } | |
523 | |
524 | |
525 bool CONTEXT::cover_env_to(char *to) { | |
526 char buffer[maxlen]; | |
527 char *x = strchr(to, '@'); | |
528 if (x) x++; | |
529 else x = to; | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
530 if (*x == '\0') return true; // always allow covering addresses with no domain name, eg abuse@ |
100
63e8633abc34
allow empty env_to at global context to remove all restrictions on child contexts
carl
parents:
99
diff
changeset
|
531 if (!parent && env_to.empty()) return true; // empty env_to at global level covers everything |
94 | 532 string_set::iterator i = env_to.find(x); |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
533 if (i != env_to.end()) return true; // we cover the entire domain |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
534 if (x != to) { |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
535 i = env_to.find(to); |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
536 if (i != env_to.end()) return true; // we cover the specific email address |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
537 } |
94 | 538 return false; |
539 } | |
540 | |
541 | |
542 VERIFYP CONTEXT::find_verify(char *to) { | |
543 if (verify_host && (verify_host != token_myhostname) && cover_env_to(to)) { | |
544 verify_map::iterator i = verifiers.find(verify_host); | |
545 if (i == verifiers.end()) { | |
546 if (debug_syslog) { | |
547 char buf[maxlen]; | |
548 snprintf(buf, maxlen, "cannot find struc for %s", verify_host); | |
549 my_syslog(buf); | |
550 } | |
551 return NULL; | |
552 } | |
553 VERIFYP v = (*i).second; | |
554 | |
555 return v; | |
556 } | |
557 else if (parent) return parent->find_verify(to); | |
558 else return NULL; | |
559 } | |
560 | |
561 | |
136 | 562 int CONTEXT::find_rate(char *user) { |
140 | 563 if (rcpt_per_hour.empty()) return default_rcpt_rate; |
136 | 564 rcpt_rates::iterator i = rcpt_per_hour.find(user); |
140 | 565 return (i == rcpt_per_hour.end()) ? default_rcpt_rate : (*i).second; |
136 | 566 } |
567 | |
568 | |
94 | 569 char *CONTEXT::find_from(char *from) { |
570 char *rc = token_inherit; | |
571 string_map::iterator i = env_from.find(from); | |
117 | 572 if (i != env_from.end()) rc = (*i).second; // found user@domain key |
94 | 573 else { |
574 char *x = strchr(from, '@'); | |
575 if (x) { | |
576 x++; | |
577 i = env_from.find(x); | |
117 | 578 if (i != env_from.end()) rc = (*i).second; // found domain key |
94 | 579 else { |
580 char y = *x; | |
581 *x = '\0'; | |
582 i = env_from.find(from); | |
583 *x = y; | |
584 if (i != env_from.end()) rc = (*i).second; // found user@ key | |
585 } | |
586 } | |
587 } | |
588 if (rc == token_inherit) rc = env_from_default; | |
589 if ((rc == token_inherit) && parent) return parent->find_from(from); | |
590 return (rc == token_inherit) ? token_unknown : rc; | |
591 } | |
592 | |
593 | |
594 CONTEXTP CONTEXT::find_context(char *from) { | |
595 context_map::iterator i = env_from_context.find(from); | |
117 | 596 if (i != env_from_context.end()) return (*i).second; // found user@domain key |
94 | 597 char *x = strchr(from, '@'); |
598 if (x) { | |
599 x++; | |
600 i = env_from_context.find(x); | |
117 | 601 if (i != env_from_context.end()) return (*i).second; // found domain key |
94 | 602 char y = *x; |
603 *x = '\0'; | |
604 i = env_from_context.find(from); | |
605 *x = y; | |
606 if (i != env_from_context.end()) return (*i).second; // found user@ key | |
607 } | |
608 return this; | |
609 } | |
610 | |
611 | |
612 CONTEXTP CONTEXT::find_from_context_name(char *name) { | |
613 context_map::iterator i = children.find(name); | |
614 if (i != children.end()) return (*i).second; | |
615 return NULL; | |
616 } | |
617 | |
618 | |
619 DNSBLP CONTEXT::find_dnsbl(char *name) { | |
620 dnsblp_map::iterator i = dnsbl_names.find(name); | |
621 if (i != dnsbl_names.end()) return (*i).second; | |
622 if (parent) return parent->find_dnsbl(name); | |
623 return NULL; | |
624 } | |
625 | |
626 | |
627 char* CONTEXT::get_content_suffix() { | |
628 if (!content_suffix && parent) return parent->get_content_suffix(); | |
629 return content_suffix; | |
630 } | |
631 | |
632 | |
119 | 633 char* CONTEXT::get_uribl_suffix() { |
634 if (!uribl_suffix && parent) return parent->get_uribl_suffix(); | |
635 return uribl_suffix; | |
636 } | |
637 | |
638 | |
94 | 639 char* CONTEXT::get_content_message() { |
640 if (!content_message && parent) return parent->get_content_message(); | |
641 return content_message; | |
642 } | |
643 | |
644 | |
119 | 645 char* CONTEXT::get_uribl_message() { |
646 if (!uribl_message && parent) return parent->get_uribl_message(); | |
647 return uribl_message; | |
648 } | |
649 | |
650 | |
94 | 651 string_set& CONTEXT::get_content_host_ignore() { |
652 if (content_host_ignore.empty() && parent) return parent->get_content_host_ignore(); | |
653 return content_host_ignore; | |
654 } | |
655 | |
656 | |
117 | 657 string_set& CONTEXT::get_content_cctlds() { |
658 if (content_cctlds.empty() && parent) return parent->get_content_cctlds(); | |
659 return content_cctlds; | |
660 } | |
661 | |
94 | 662 string_set& CONTEXT::get_content_tlds() { |
663 if (content_tlds.empty() && parent) return parent->get_content_tlds(); | |
664 return content_tlds; | |
665 } | |
666 | |
667 | |
668 string_set& CONTEXT::get_html_tags() { | |
669 if (html_tags.empty() && parent) return parent->get_html_tags(); | |
670 return html_tags; | |
671 } | |
672 | |
673 | |
674 dnsblp_list& CONTEXT::get_dnsbl_list() { | |
675 if (dnsbl_list.empty() && parent) return parent->get_dnsbl_list(); | |
676 return dnsbl_list; | |
677 } | |
678 | |
679 | |
680 bool CONTEXT::acceptable_content(recorder &memory, char *&msg) { | |
681 if (memory.excessive_bad_tags(tag_limit)) { | |
682 msg = tag_limit_message; | |
683 return false; | |
684 } | |
685 if (!host_random && memory.excessive_hosts(host_limit)) { | |
686 msg = host_limit_message; | |
687 return false; | |
688 } | |
689 return true; | |
690 } | |
691 | |
692 | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
693 void CONTEXT::dump(bool isdefault, int level) { |
94 | 694 char indent[maxlen]; |
695 int i = min(maxlen-1, level*4); | |
696 memset(indent, ' ', i); | |
697 indent[i] = '\0'; | |
698 char buf[maxlen]; | |
699 char *fullname = get_full_name(buf,maxlen); | |
700 printf("%s context %s { \t// %s\n", indent, name, fullname); | |
701 | |
702 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) { | |
703 char *n = (*i).first; | |
704 DNSBL &d = *(*i).second; | |
705 printf("%s dnsbl %s %s \"%s\"; \n", indent, n, d.suffix, d.message); | |
706 } | |
707 | |
145 | 708 dnsblp_list dl = get_dnsbl_list(); |
709 if (!dl.empty()) { | |
94 | 710 printf("%s dnsbl_list", indent); |
145 | 711 for (dnsblp_list::iterator i=dl.begin(); i!=dl.end(); i++) { |
94 | 712 DNSBL &d = *(*i); |
713 printf(" %s", d.name); | |
714 } | |
715 printf("; \n"); | |
716 } | |
717 | |
718 if (content_filtering) { | |
719 printf("%s content on { \n", indent, env_from_default); | |
720 if (content_suffix) { | |
721 printf("%s filter %s \"%s\"; \n", indent, content_suffix, content_message); | |
722 } | |
119 | 723 if (uribl_suffix) { |
724 printf("%s uribl %s \"%s\"; \n", indent, uribl_suffix, uribl_message); | |
725 } | |
94 | 726 if (!content_host_ignore.empty()) { |
727 printf("%s ignore { \n", indent); | |
728 for (string_set::iterator i=content_host_ignore.begin(); i!=content_host_ignore.end(); i++) { | |
729 printf("%s %s; \n", indent, *i); | |
730 } | |
731 printf("%s }; \n", indent); | |
732 } | |
117 | 733 if (!content_cctlds.empty()) { |
734 printf("%s cctld { \n", indent); | |
735 printf("%s ", indent); | |
736 for (string_set::iterator i=content_cctlds.begin(); i!=content_cctlds.end(); i++) { | |
737 printf("%s; ", *i); | |
738 } | |
739 printf("\n%s }; \n", indent); | |
740 } | |
94 | 741 if (!content_tlds.empty()) { |
742 printf("%s tld { \n", indent); | |
743 printf("%s ", indent); | |
744 for (string_set::iterator i=content_tlds.begin(); i!=content_tlds.end(); i++) { | |
745 printf("%s; ", *i); | |
746 } | |
747 printf("\n%s }; \n", indent); | |
748 } | |
749 if (!html_tags.empty()) { | |
750 printf("%s html_tags { \n", indent); | |
751 printf("%s ", indent); | |
752 for (string_set::iterator i=html_tags.begin(); i!=html_tags.end(); i++) { | |
753 printf("%s; ", *i); | |
754 } | |
755 printf("\n%s }; \n", indent); | |
756 } | |
757 if (host_limit_message) { | |
758 printf("%s host_limit on %d \"%s\"; \n", indent, host_limit, host_limit_message); | |
759 } | |
760 else if (host_random) { | |
761 printf("%s host_limit soft %d; \n", indent, host_limit); | |
762 } | |
763 else { | |
764 printf("%s host_limit off; \n", indent); | |
765 } | |
766 if (tag_limit_message) { | |
767 printf("%s html_limit on %d \"%s\"; \n", indent, tag_limit, tag_limit_message); | |
768 } | |
769 else { | |
770 printf("%s html_limit off; \n", indent); | |
771 } | |
772 printf("%s }; \n", indent); | |
773 } | |
774 else { | |
775 printf("%s content off {}; \n", indent, env_from_default); | |
776 } | |
777 | |
778 printf("%s env_to { \t// %s\n", indent, fullname); | |
779 for (string_set::iterator i=env_to.begin(); i!=env_to.end(); i++) { | |
780 printf("%s %s; \n", indent, *i); | |
781 } | |
782 printf("%s }; \n", indent); | |
783 | |
784 if (verify_host) { | |
785 printf("%s verify %s; \n", indent, verify_host); | |
786 } | |
787 | |
788 for (context_map::iterator i=children.begin(); i!=children.end(); i++) { | |
789 CONTEXTP c = (*i).second; | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
790 c->dump(false, level+1); |
94 | 791 } |
792 | |
793 printf("%s env_from %s { \t// %s\n", indent, env_from_default, fullname); | |
794 if (!env_from.empty()) { | |
795 printf("%s // white/black/unknown \n", indent); | |
796 for (string_map::iterator i=env_from.begin(); i!=env_from.end(); i++) { | |
797 char *f = (*i).first; | |
798 char *t = (*i).second; | |
799 printf("%s %s \t%s; \n", indent, f, t); | |
800 } | |
801 } | |
802 if (!env_from_context.empty()) { | |
803 printf("%s // child contexts \n", indent); | |
804 for (context_map::iterator j=env_from_context.begin(); j!=env_from_context.end(); j++) { | |
805 char *f = (*j).first; | |
806 CONTEXTP t = (*j).second; | |
807 printf("%s %s \t%s; \n", indent, f, t->name); | |
808 } | |
809 } | |
810 printf("%s }; \n", indent); | |
811 | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
812 if (isdefault) { |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
813 printf("%s rate_limit %d { \n", indent, default_rcpt_rate); |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
814 for (rcpt_rates::iterator j=rcpt_per_hour.begin(); j!=rcpt_per_hour.end(); j++) { |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
815 char *u = (*j).first; |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
816 int l = (*j).second; |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
817 printf("%s \"%s\" \t%d; \n", indent, u, l); |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
818 } |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
819 printf("%s }; \n", indent); |
136 | 820 } |
821 | |
94 | 822 printf("%s }; \n", indent); |
823 } | |
824 | |
825 | |
826 //////////////////////////////////////////////// | |
827 // helper to discard the strings held by a string_set | |
828 // | |
829 void discard(string_set &s) { | |
830 for (string_set::iterator i=s.begin(); i!=s.end(); i++) { | |
831 free(*i); | |
832 } | |
833 s.clear(); | |
834 } | |
835 | |
836 | |
837 //////////////////////////////////////////////// | |
838 // helper to register a string in a string set | |
839 // | |
840 char* register_string(string_set &s, char *name) { | |
841 string_set::iterator i = s.find(name); | |
842 if (i != s.end()) return *i; | |
843 char *x = strdup(name); | |
844 s.insert(x); | |
845 return x; | |
846 } | |
847 | |
848 | |
849 //////////////////////////////////////////////// | |
850 // register a global string | |
851 // | |
852 char* register_string(char *name) { | |
853 return register_string(all_strings, name); | |
854 } | |
855 | |
856 | |
857 //////////////////////////////////////////////// | |
858 // | |
859 bool tsa(TOKEN &tok, char *token); | |
860 bool tsa(TOKEN &tok, char *token) { | |
861 char *have = tok.next(); | |
862 if (have == token) return true; | |
863 tok.token_error(token, have); | |
864 return false; | |
865 } | |
866 | |
867 | |
868 //////////////////////////////////////////////// | |
869 // | |
870 bool parse_dnsbl(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
871 bool parse_dnsbl(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
872 char *name = tok.next(); | |
873 char *suf = tok.next(); | |
874 char *msg = tok.next(); | |
875 if (!tsa(tok, token_semi)) return false; | |
876 DNSBLP dnsnew = new DNSBL(name, suf, msg); | |
877 DNSBLP dnsold = me.find_dnsbl(name); | |
878 if (dnsold && (*dnsold == *dnsnew)) { | |
879 // duplicate redefinition, ignore it | |
880 delete dnsnew; | |
881 return true; | |
882 } | |
883 me.add_dnsbl(name, dnsnew); | |
884 return true; | |
885 } | |
886 | |
887 | |
888 //////////////////////////////////////////////// | |
889 // | |
890 bool parse_dnsbll(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
891 bool parse_dnsbll(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
892 while (true) { | |
893 char *have = tok.next(); | |
894 if (!have) break; | |
895 if (have == token_semi) break; | |
896 DNSBLP dns = me.find_dnsbl(have); | |
897 if (dns) { | |
898 me.add_dnsbl(dns); | |
899 } | |
900 else { | |
901 tok.token_error("dnsbl name", have); | |
902 return false; | |
903 } | |
904 } | |
905 return true; | |
906 } | |
907 | |
908 | |
909 //////////////////////////////////////////////// | |
910 // | |
911 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
912 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
913 char *setting = tok.next(); | |
914 if (setting == token_on) { | |
915 me.set_content_filtering(true); | |
916 } | |
917 else if (setting == token_off) { | |
918 me.set_content_filtering(false); | |
919 } | |
920 else { | |
921 tok.token_error("on/off", setting); | |
922 return false; | |
923 } | |
924 if (!tsa(tok, token_lbrace)) return false; | |
925 while (true) { | |
926 char *have = tok.next(); | |
927 if (!have) break; | |
928 if (have == token_filter) { | |
929 char *suffix = tok.next(); | |
930 char *messag = tok.next(); | |
931 me.set_content_suffix(suffix); | |
932 me.set_content_message(messag); | |
933 if (!tsa(tok, token_semi)) return false; | |
934 } | |
119 | 935 else if (have == token_uribl) { |
936 char *suffix = tok.next(); | |
937 char *messag = tok.next(); | |
938 me.set_uribl_suffix(suffix); | |
939 me.set_uribl_message(messag); | |
940 if (!tsa(tok, token_semi)) return false; | |
941 } | |
94 | 942 else if (have == token_ignore) { |
943 if (!tsa(tok, token_lbrace)) return false; | |
944 while (true) { | |
945 if (!have) break; | |
946 char *have = tok.next(); | |
947 if (have == token_rbrace) break; // done | |
948 me.add_ignore(have); | |
949 } | |
950 if (!tsa(tok, token_semi)) return false; | |
951 } | |
117 | 952 else if (have == token_cctld) { |
953 if (!tsa(tok, token_lbrace)) return false; | |
954 while (true) { | |
955 char *have = tok.next(); | |
956 if (!have) break; | |
957 if (have == token_rbrace) break; // done | |
958 me.add_cctld(have); | |
959 } | |
960 if (!tsa(tok, token_semi)) return false; | |
961 } | |
94 | 962 else if (have == token_tld) { |
963 if (!tsa(tok, token_lbrace)) return false; | |
964 while (true) { | |
965 char *have = tok.next(); | |
966 if (!have) break; | |
967 if (have == token_rbrace) break; // done | |
968 me.add_tld(have); | |
969 } | |
970 if (!tsa(tok, token_semi)) return false; | |
971 } | |
972 else if (have == token_html_limit) { | |
973 have = tok.next(); | |
974 if (have == token_on) { | |
975 me.set_tag_limit(tok.nextint()); | |
976 me.set_tag_message(tok.next()); | |
977 } | |
978 else if (have == token_off) { | |
979 me.set_tag_limit(0); | |
980 me.set_tag_message(NULL); | |
981 } | |
982 else { | |
983 tok.token_error("on/off", have); | |
984 return false; | |
985 } | |
986 if (!tsa(tok, token_semi)) return false; | |
987 } | |
988 else if (have == token_html_tags) { | |
989 if (!tsa(tok, token_lbrace)) return false; | |
990 while (true) { | |
991 char *have = tok.next(); | |
992 if (!have) break; | |
993 if (have == token_rbrace) { | |
994 break; // done | |
995 } | |
996 else { | |
997 me.add_tag(have); // base version | |
998 char buf[200]; | |
999 snprintf(buf, sizeof(buf), "/%s", have); | |
1000 me.add_tag(register_string(buf)); // leading / | |
1001 snprintf(buf, sizeof(buf), "%s/", have); | |
1002 me.add_tag(register_string(buf)); // trailing / | |
1003 } | |
1004 } | |
1005 if (!tsa(tok, token_semi)) return false; | |
1006 } | |
1007 else if (have == token_host_limit) { | |
1008 have = tok.next(); | |
1009 if (have == token_on) { | |
1010 me.set_host_limit(tok.nextint()); | |
1011 me.set_host_message(tok.next()); | |
1012 me.set_host_random(false); | |
1013 } | |
1014 else if (have == token_off) { | |
1015 me.set_host_limit(0); | |
1016 me.set_host_message(NULL); | |
1017 me.set_host_random(false); | |
1018 } | |
1019 else if (have == token_soft) { | |
1020 me.set_host_limit(tok.nextint()); | |
1021 me.set_host_message(NULL); | |
1022 me.set_host_random(true); | |
1023 } | |
1024 else { | |
1025 tok.token_error("on/off/soft", have); | |
1026 return false; | |
1027 } | |
1028 if (!tsa(tok, token_semi)) return false; | |
1029 } | |
1030 else if (have == token_rbrace) { | |
1031 break; // done | |
1032 } | |
1033 else { | |
1034 tok.token_error("content keyword", have); | |
1035 return false; | |
1036 } | |
1037 } | |
1038 return tsa(tok, token_semi); | |
1039 } | |
1040 | |
1041 | |
1042 //////////////////////////////////////////////// | |
1043 // | |
1044 bool parse_envto(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1045 bool parse_envto(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1046 if (!tsa(tok, token_lbrace)) return false; | |
1047 while (true) { | |
1048 char *have = tok.next(); | |
1049 if (!have) break; | |
1050 if (have == token_rbrace) break; | |
1051 if (have == token_semi) { | |
1052 // optional separators | |
1053 } | |
1054 else if (have == token_dccto) { | |
1055 char *flavor = tok.next(); | |
1056 if (!tsa(tok, token_lbrace)) return false; | |
1057 bool keeping = false; | |
1058 while (true) { | |
1059 char *have = tok.next(); | |
1060 if (!have) break; | |
1061 if (have == token_rbrace) break; | |
1062 if (have == flavor) { | |
1063 keeping = true; | |
1064 continue; | |
1065 } | |
1066 else if ((have == token_ok) || (have == token_ok2) || (have == token_many)) { | |
1067 keeping = false; | |
1068 continue; | |
1069 } | |
1070 if (have == token_envto) { | |
1071 have = tok.next(); | |
1072 if (keeping) { | |
1073 if (me.allow_env_to(have)) { | |
1074 me.add_to(have); | |
1075 dc.add_to(have, &me); | |
1076 } | |
1077 } | |
1078 } | |
1079 //else if (have == token_substitute) { | |
1080 // if (tok.next() == token_mailhost) { | |
1081 // have = tok.next(); | |
1082 // if (keeping) { | |
1083 // if (me.allow_env_to(have)) { | |
1084 // me.add_to(have); | |
1085 // dc.add_to(have, &me); | |
1086 // } | |
1087 // } | |
1088 // } | |
1089 //} | |
1090 tok.skipeol(); | |
1091 } | |
1092 } | |
1093 else if (me.allow_env_to(have)) { | |
1094 me.add_to(have); | |
1095 dc.add_to(have, &me); | |
1096 } | |
1097 else { | |
1098 tok.token_error("user@ or user@domain.tld or domain.tld where domain.tld allowed by parent context", have); | |
1099 return false; | |
1100 } | |
1101 } | |
1102 return tsa(tok, token_semi); | |
1103 } | |
1104 | |
1105 | |
1106 //////////////////////////////////////////////// | |
1107 // | |
1108 bool parse_verify(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1109 bool parse_verify(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1110 char *host = tok.next(); | |
1111 if (!tsa(tok, token_semi)) return false; | |
1112 me.set_verify(host); | |
1113 add_verify_host(host); | |
99 | 1114 return true; |
94 | 1115 } |
1116 | |
1117 | |
1118 //////////////////////////////////////////////// | |
1119 // | |
1120 bool parse_envfrom(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1121 bool parse_envfrom(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1122 char *st = tok.next(); | |
1123 if ((st == token_black) || (st == token_white) || (st == token_unknown) || (st == token_inherit)) { | |
1124 me.set_from_default(st); | |
1125 } | |
1126 else { | |
1127 tok.push(st); | |
1128 } | |
1129 if (!tsa(tok, token_lbrace)) return false; | |
1130 while (true) { | |
1131 char *have = tok.next(); | |
1132 if (!have) break; | |
1133 if (have == token_rbrace) break; | |
1134 if (have == token_semi) { | |
1135 // optional separators | |
1136 } | |
1137 else if (have == token_dccfrom) { | |
1138 if (!tsa(tok, token_lbrace)) return false; | |
1139 bool keeping = false; | |
1140 bool many = false; | |
1141 while (true) { | |
1142 char *have = tok.next(); | |
1143 if (!have) break; | |
1144 if (have == token_rbrace) break; | |
1145 if (have == token_ok) { | |
1146 keeping = true; | |
1147 many = false; | |
1148 continue; | |
1149 } | |
1150 else if (have == token_many) { | |
1151 keeping = true; | |
1152 many = true; | |
1153 continue; | |
1154 } | |
1155 else if (have == token_ok2) { | |
1156 keeping = false; | |
1157 continue; | |
1158 } | |
1159 if (have == token_envfrom) { | |
1160 have = tok.next(); | |
1161 if (keeping) { | |
1162 me.add_from(have, (many) ? token_black : token_white); | |
1163 } | |
1164 } | |
1165 else if (have == token_substitute) { | |
1166 if (tok.next() == token_mailhost) { | |
1167 have = tok.next(); | |
1168 me.add_from(have, (many) ? token_black : token_white); | |
1169 } | |
1170 } | |
1171 tok.skipeol(); | |
1172 } | |
1173 } | |
1174 else { | |
1175 // may be a valid email address or domain name | |
1176 char *st = tok.next(); | |
1177 if ((st == token_black) || (st == token_white) || (st == token_unknown)) { | |
1178 me.add_from(have, st); | |
1179 } | |
1180 else { | |
1181 CONTEXTP con = me.find_from_context_name(st); | |
1182 if (con) { | |
1183 me.add_from_context(have, con); | |
1184 } | |
1185 else { | |
1186 tok.token_error("white/black/unknown or child context name", st); | |
1187 return false; | |
1188 } | |
1189 } | |
1190 } | |
1191 } | |
1192 return tsa(tok, token_semi); | |
1193 } | |
1194 | |
1195 | |
1196 //////////////////////////////////////////////// | |
1197 // | |
136 | 1198 bool parse_rate(TOKEN &tok, CONFIG &dc, CONTEXT &me); |
1199 bool parse_rate(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
140 | 1200 char *def = tok.next(); |
141 | 1201 tok.push(def); |
1202 if (def != token_lbrace) me.set_default_rate(tok.nextint()); | |
136 | 1203 if (!tsa(tok, token_lbrace)) return false; |
1204 while (true) { | |
1205 char *have = tok.next(); | |
1206 if (!have) break; | |
1207 if (have == token_rbrace) break; | |
1208 if (have == token_semi) { | |
1209 // optional separators | |
1210 } | |
1211 else { | |
140 | 1212 me.add_rate(have, tok.nextint()); |
136 | 1213 } |
1214 } | |
1215 return tsa(tok, token_semi); | |
1216 } | |
1217 | |
1218 | |
1219 //////////////////////////////////////////////// | |
1220 // | |
94 | 1221 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent); |
1222 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent) { | |
1223 char *name = tok.next(); | |
1224 if (!tsa(tok, token_lbrace)) return false; | |
1225 CONTEXTP con = new CONTEXT(parent, name); | |
1226 | |
1227 while (true) { | |
1228 char *have = tok.next(); | |
1229 if (!have) break; | |
1230 if (have == token_rbrace) break; // done | |
1231 if (have == token_dnsbl) { | |
1232 if (!parse_dnsbl(tok, dc, *con)) return false; | |
1233 } | |
1234 else if (have == token_dnsbll) { | |
1235 if (!parse_dnsbll(tok, dc, *con)) return false; | |
1236 } | |
1237 else if (have == token_content) { | |
1238 if (!parse_content(tok, dc, *con)) return false; | |
1239 } | |
1240 else if (have == token_envto) { | |
1241 if (!parse_envto(tok, dc, *con)) return false; | |
1242 } | |
1243 else if (have == token_verify) { | |
1244 if (!parse_verify(tok, dc, *con)) return false; | |
1245 } | |
1246 else if (have == token_envfrom) { | |
1247 if (!parse_envfrom(tok, dc, *con)) return false; | |
1248 } | |
140 | 1249 else if (have == token_rate) { |
1250 if (parent || dc.default_context) tok.token_error("rate limit ignored in non default context"); | |
136 | 1251 if (!parse_rate(tok, dc, *con)) return false; |
1252 } | |
94 | 1253 else if (have == token_context) { |
1254 if (!parse_context(tok, dc, con)) return false; | |
1255 } | |
1256 else { | |
1257 tok.token_error("context keyword", have); | |
1258 return false; | |
1259 } | |
1260 } | |
1261 | |
1262 if (!tsa(tok, token_semi)) { | |
1263 delete con; | |
1264 return false; | |
1265 } | |
1266 dc.add_context(con); | |
1267 if (parent) parent->add_context(con); | |
1268 return true; | |
1269 } | |
1270 | |
1271 | |
1272 //////////////////////////////////////////////// | |
1273 // parse a config file | |
1274 // | |
1275 bool load_conf(CONFIG &dc, char *fn) { | |
99 | 1276 int count = 0; |
94 | 1277 TOKEN tok(fn, &dc.config_files); |
1278 while (true) { | |
1279 char *have = tok.next(); | |
1280 if (!have) break; | |
1281 if (have == token_context) { | |
1282 if (!parse_context(tok, dc, NULL)) { | |
99 | 1283 tok.token_error("load_conf() failed to parse context"); |
94 | 1284 return false; |
1285 } | |
99 | 1286 else count++; |
94 | 1287 } |
1288 else { | |
1289 tok.token_error(token_context, have); | |
1290 return false; | |
1291 } | |
1292 } | |
99 | 1293 tok.token_error("load_conf() found %d contexts in %s", count, fn); |
94 | 1294 return (dc.default_context) ? true : false; |
1295 } | |
1296 | |
1297 | |
1298 //////////////////////////////////////////////// | |
1299 // setup a new smtp verify host | |
1300 // | |
1301 void add_verify_host(char *host) { | |
1302 verify_map::iterator i = verifiers.find(host); | |
1303 if (i == verifiers.end()) { | |
1304 VERIFYP v = new VERIFY(host); | |
1305 verifiers[host] = v; | |
1306 } | |
1307 } | |
1308 | |
1309 | |
1310 //////////////////////////////////////////////// | |
1311 // thread to check for verify hosts with old sockets that we can close | |
1312 // | |
1313 void* verify_closer(void *arg) { | |
1314 while (true) { | |
1315 sleep(maxage); | |
1316 for (verify_map::iterator i=verifiers.begin(); i!=verifiers.end(); i++) { | |
1317 VERIFYP v = (*i).second; | |
1318 v->closer(); | |
1319 } | |
1320 } | |
1321 return NULL; | |
1322 } | |
1323 | |
1324 | |
1325 //////////////////////////////////////////////// | |
1326 // init the tokens | |
1327 // | |
1328 void token_init() { | |
1329 token_black = register_string("black"); | |
117 | 1330 token_cctld = register_string("cctld"); |
94 | 1331 token_content = register_string("content"); |
1332 token_context = register_string("context"); | |
1333 token_dccfrom = register_string("dcc_from"); | |
1334 token_dccto = register_string("dcc_to"); | |
1335 token_default = register_string("default"); | |
1336 token_dnsbl = register_string("dnsbl"); | |
1337 token_dnsbll = register_string("dnsbl_list"); | |
1338 token_envfrom = register_string("env_from"); | |
1339 token_envto = register_string("env_to"); | |
1340 token_filter = register_string("filter"); | |
1341 token_host_limit = register_string("host_limit"); | |
1342 token_html_limit = register_string("html_limit"); | |
1343 token_html_tags = register_string("html_tags"); | |
1344 token_ignore = register_string("ignore"); | |
1345 token_include = register_string("include"); | |
1346 token_inherit = register_string("inherit"); | |
1347 token_lbrace = register_string("{"); | |
1348 token_mailhost = register_string("mail_host"); | |
1349 token_many = register_string("many"); | |
1350 token_off = register_string("off"); | |
1351 token_ok = register_string("ok"); | |
1352 token_ok2 = register_string("ok2"); | |
1353 token_on = register_string("on"); | |
136 | 1354 token_rate = register_string("rate_limit"); |
94 | 1355 token_rbrace = register_string("}"); |
1356 token_semi = register_string(";"); | |
1357 token_soft = register_string("soft"); | |
1358 token_substitute = register_string("substitute"); | |
1359 token_tld = register_string("tld"); | |
1360 token_unknown = register_string("unknown"); | |
119 | 1361 token_uribl = register_string("uribl"); |
94 | 1362 token_verify = register_string("verify"); |
1363 token_white = register_string("white"); | |
1364 | |
1365 if (gethostname(myhostname, HOST_NAME_MAX+1) != 0) { | |
1366 strncpy(myhostname, "localhost", HOST_NAME_MAX+1); | |
1367 } | |
1368 myhostname[HOST_NAME_MAX] = '\0'; // ensure null termination | |
1369 token_myhostname = register_string(myhostname); | |
1370 } |