Mercurial > dnsbl
annotate src/context.cpp @ 144:31ff00ea6bfb stable-5-24
allow parent/child to share a fully qualified env_to address
author | carl |
---|---|
date | Sun, 15 Oct 2006 12:23:17 -0700 |
parents | ecb40aa3eaa5 |
children | 9b9bab1d3c21 |
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 | |
708 if (!dnsbl_list.empty()) { | |
709 printf("%s dnsbl_list", indent); | |
710 for (dnsblp_list::iterator i=dnsbl_list.begin(); i!=dnsbl_list.end(); i++) { | |
711 DNSBL &d = *(*i); | |
712 printf(" %s", d.name); | |
713 } | |
714 printf("; \n"); | |
715 } | |
716 | |
717 if (content_filtering) { | |
718 printf("%s content on { \n", indent, env_from_default); | |
719 if (content_suffix) { | |
720 printf("%s filter %s \"%s\"; \n", indent, content_suffix, content_message); | |
721 } | |
119 | 722 if (uribl_suffix) { |
723 printf("%s uribl %s \"%s\"; \n", indent, uribl_suffix, uribl_message); | |
724 } | |
94 | 725 if (!content_host_ignore.empty()) { |
726 printf("%s ignore { \n", indent); | |
727 for (string_set::iterator i=content_host_ignore.begin(); i!=content_host_ignore.end(); i++) { | |
728 printf("%s %s; \n", indent, *i); | |
729 } | |
730 printf("%s }; \n", indent); | |
731 } | |
117 | 732 if (!content_cctlds.empty()) { |
733 printf("%s cctld { \n", indent); | |
734 printf("%s ", indent); | |
735 for (string_set::iterator i=content_cctlds.begin(); i!=content_cctlds.end(); i++) { | |
736 printf("%s; ", *i); | |
737 } | |
738 printf("\n%s }; \n", indent); | |
739 } | |
94 | 740 if (!content_tlds.empty()) { |
741 printf("%s tld { \n", indent); | |
742 printf("%s ", indent); | |
743 for (string_set::iterator i=content_tlds.begin(); i!=content_tlds.end(); i++) { | |
744 printf("%s; ", *i); | |
745 } | |
746 printf("\n%s }; \n", indent); | |
747 } | |
748 if (!html_tags.empty()) { | |
749 printf("%s html_tags { \n", indent); | |
750 printf("%s ", indent); | |
751 for (string_set::iterator i=html_tags.begin(); i!=html_tags.end(); i++) { | |
752 printf("%s; ", *i); | |
753 } | |
754 printf("\n%s }; \n", indent); | |
755 } | |
756 if (host_limit_message) { | |
757 printf("%s host_limit on %d \"%s\"; \n", indent, host_limit, host_limit_message); | |
758 } | |
759 else if (host_random) { | |
760 printf("%s host_limit soft %d; \n", indent, host_limit); | |
761 } | |
762 else { | |
763 printf("%s host_limit off; \n", indent); | |
764 } | |
765 if (tag_limit_message) { | |
766 printf("%s html_limit on %d \"%s\"; \n", indent, tag_limit, tag_limit_message); | |
767 } | |
768 else { | |
769 printf("%s html_limit off; \n", indent); | |
770 } | |
771 printf("%s }; \n", indent); | |
772 } | |
773 else { | |
774 printf("%s content off {}; \n", indent, env_from_default); | |
775 } | |
776 | |
777 printf("%s env_to { \t// %s\n", indent, fullname); | |
778 for (string_set::iterator i=env_to.begin(); i!=env_to.end(); i++) { | |
779 printf("%s %s; \n", indent, *i); | |
780 } | |
781 printf("%s }; \n", indent); | |
782 | |
783 if (verify_host) { | |
784 printf("%s verify %s; \n", indent, verify_host); | |
785 } | |
786 | |
787 for (context_map::iterator i=children.begin(); i!=children.end(); i++) { | |
788 CONTEXTP c = (*i).second; | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
789 c->dump(false, level+1); |
94 | 790 } |
791 | |
792 printf("%s env_from %s { \t// %s\n", indent, env_from_default, fullname); | |
793 if (!env_from.empty()) { | |
794 printf("%s // white/black/unknown \n", indent); | |
795 for (string_map::iterator i=env_from.begin(); i!=env_from.end(); i++) { | |
796 char *f = (*i).first; | |
797 char *t = (*i).second; | |
798 printf("%s %s \t%s; \n", indent, f, t); | |
799 } | |
800 } | |
801 if (!env_from_context.empty()) { | |
802 printf("%s // child contexts \n", indent); | |
803 for (context_map::iterator j=env_from_context.begin(); j!=env_from_context.end(); j++) { | |
804 char *f = (*j).first; | |
805 CONTEXTP t = (*j).second; | |
806 printf("%s %s \t%s; \n", indent, f, t->name); | |
807 } | |
808 } | |
809 printf("%s }; \n", indent); | |
810 | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
811 if (isdefault) { |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
812 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
|
813 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
|
814 char *u = (*j).first; |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
815 int l = (*j).second; |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
816 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
|
817 } |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
818 printf("%s }; \n", indent); |
136 | 819 } |
820 | |
94 | 821 printf("%s }; \n", indent); |
822 } | |
823 | |
824 | |
825 //////////////////////////////////////////////// | |
826 // helper to discard the strings held by a string_set | |
827 // | |
828 void discard(string_set &s) { | |
829 for (string_set::iterator i=s.begin(); i!=s.end(); i++) { | |
830 free(*i); | |
831 } | |
832 s.clear(); | |
833 } | |
834 | |
835 | |
836 //////////////////////////////////////////////// | |
837 // helper to register a string in a string set | |
838 // | |
839 char* register_string(string_set &s, char *name) { | |
840 string_set::iterator i = s.find(name); | |
841 if (i != s.end()) return *i; | |
842 char *x = strdup(name); | |
843 s.insert(x); | |
844 return x; | |
845 } | |
846 | |
847 | |
848 //////////////////////////////////////////////// | |
849 // register a global string | |
850 // | |
851 char* register_string(char *name) { | |
852 return register_string(all_strings, name); | |
853 } | |
854 | |
855 | |
856 //////////////////////////////////////////////// | |
857 // | |
858 bool tsa(TOKEN &tok, char *token); | |
859 bool tsa(TOKEN &tok, char *token) { | |
860 char *have = tok.next(); | |
861 if (have == token) return true; | |
862 tok.token_error(token, have); | |
863 return false; | |
864 } | |
865 | |
866 | |
867 //////////////////////////////////////////////// | |
868 // | |
869 bool parse_dnsbl(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
870 bool parse_dnsbl(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
871 char *name = tok.next(); | |
872 char *suf = tok.next(); | |
873 char *msg = tok.next(); | |
874 if (!tsa(tok, token_semi)) return false; | |
875 DNSBLP dnsnew = new DNSBL(name, suf, msg); | |
876 DNSBLP dnsold = me.find_dnsbl(name); | |
877 if (dnsold && (*dnsold == *dnsnew)) { | |
878 // duplicate redefinition, ignore it | |
879 delete dnsnew; | |
880 return true; | |
881 } | |
882 me.add_dnsbl(name, dnsnew); | |
883 return true; | |
884 } | |
885 | |
886 | |
887 //////////////////////////////////////////////// | |
888 // | |
889 bool parse_dnsbll(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
890 bool parse_dnsbll(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
891 while (true) { | |
892 char *have = tok.next(); | |
893 if (!have) break; | |
894 if (have == token_semi) break; | |
895 DNSBLP dns = me.find_dnsbl(have); | |
896 if (dns) { | |
897 me.add_dnsbl(dns); | |
898 } | |
899 else { | |
900 tok.token_error("dnsbl name", have); | |
901 return false; | |
902 } | |
903 } | |
904 return true; | |
905 } | |
906 | |
907 | |
908 //////////////////////////////////////////////// | |
909 // | |
910 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
911 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
912 char *setting = tok.next(); | |
913 if (setting == token_on) { | |
914 me.set_content_filtering(true); | |
915 } | |
916 else if (setting == token_off) { | |
917 me.set_content_filtering(false); | |
918 } | |
919 else { | |
920 tok.token_error("on/off", setting); | |
921 return false; | |
922 } | |
923 if (!tsa(tok, token_lbrace)) return false; | |
924 while (true) { | |
925 char *have = tok.next(); | |
926 if (!have) break; | |
927 if (have == token_filter) { | |
928 char *suffix = tok.next(); | |
929 char *messag = tok.next(); | |
930 me.set_content_suffix(suffix); | |
931 me.set_content_message(messag); | |
932 if (!tsa(tok, token_semi)) return false; | |
933 } | |
119 | 934 else if (have == token_uribl) { |
935 char *suffix = tok.next(); | |
936 char *messag = tok.next(); | |
937 me.set_uribl_suffix(suffix); | |
938 me.set_uribl_message(messag); | |
939 if (!tsa(tok, token_semi)) return false; | |
940 } | |
94 | 941 else if (have == token_ignore) { |
942 if (!tsa(tok, token_lbrace)) return false; | |
943 while (true) { | |
944 if (!have) break; | |
945 char *have = tok.next(); | |
946 if (have == token_rbrace) break; // done | |
947 me.add_ignore(have); | |
948 } | |
949 if (!tsa(tok, token_semi)) return false; | |
950 } | |
117 | 951 else if (have == token_cctld) { |
952 if (!tsa(tok, token_lbrace)) return false; | |
953 while (true) { | |
954 char *have = tok.next(); | |
955 if (!have) break; | |
956 if (have == token_rbrace) break; // done | |
957 me.add_cctld(have); | |
958 } | |
959 if (!tsa(tok, token_semi)) return false; | |
960 } | |
94 | 961 else if (have == token_tld) { |
962 if (!tsa(tok, token_lbrace)) return false; | |
963 while (true) { | |
964 char *have = tok.next(); | |
965 if (!have) break; | |
966 if (have == token_rbrace) break; // done | |
967 me.add_tld(have); | |
968 } | |
969 if (!tsa(tok, token_semi)) return false; | |
970 } | |
971 else if (have == token_html_limit) { | |
972 have = tok.next(); | |
973 if (have == token_on) { | |
974 me.set_tag_limit(tok.nextint()); | |
975 me.set_tag_message(tok.next()); | |
976 } | |
977 else if (have == token_off) { | |
978 me.set_tag_limit(0); | |
979 me.set_tag_message(NULL); | |
980 } | |
981 else { | |
982 tok.token_error("on/off", have); | |
983 return false; | |
984 } | |
985 if (!tsa(tok, token_semi)) return false; | |
986 } | |
987 else if (have == token_html_tags) { | |
988 if (!tsa(tok, token_lbrace)) return false; | |
989 while (true) { | |
990 char *have = tok.next(); | |
991 if (!have) break; | |
992 if (have == token_rbrace) { | |
993 break; // done | |
994 } | |
995 else { | |
996 me.add_tag(have); // base version | |
997 char buf[200]; | |
998 snprintf(buf, sizeof(buf), "/%s", have); | |
999 me.add_tag(register_string(buf)); // leading / | |
1000 snprintf(buf, sizeof(buf), "%s/", have); | |
1001 me.add_tag(register_string(buf)); // trailing / | |
1002 } | |
1003 } | |
1004 if (!tsa(tok, token_semi)) return false; | |
1005 } | |
1006 else if (have == token_host_limit) { | |
1007 have = tok.next(); | |
1008 if (have == token_on) { | |
1009 me.set_host_limit(tok.nextint()); | |
1010 me.set_host_message(tok.next()); | |
1011 me.set_host_random(false); | |
1012 } | |
1013 else if (have == token_off) { | |
1014 me.set_host_limit(0); | |
1015 me.set_host_message(NULL); | |
1016 me.set_host_random(false); | |
1017 } | |
1018 else if (have == token_soft) { | |
1019 me.set_host_limit(tok.nextint()); | |
1020 me.set_host_message(NULL); | |
1021 me.set_host_random(true); | |
1022 } | |
1023 else { | |
1024 tok.token_error("on/off/soft", have); | |
1025 return false; | |
1026 } | |
1027 if (!tsa(tok, token_semi)) return false; | |
1028 } | |
1029 else if (have == token_rbrace) { | |
1030 break; // done | |
1031 } | |
1032 else { | |
1033 tok.token_error("content keyword", have); | |
1034 return false; | |
1035 } | |
1036 } | |
1037 return tsa(tok, token_semi); | |
1038 } | |
1039 | |
1040 | |
1041 //////////////////////////////////////////////// | |
1042 // | |
1043 bool parse_envto(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1044 bool parse_envto(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1045 if (!tsa(tok, token_lbrace)) return false; | |
1046 while (true) { | |
1047 char *have = tok.next(); | |
1048 if (!have) break; | |
1049 if (have == token_rbrace) break; | |
1050 if (have == token_semi) { | |
1051 // optional separators | |
1052 } | |
1053 else if (have == token_dccto) { | |
1054 char *flavor = tok.next(); | |
1055 if (!tsa(tok, token_lbrace)) return false; | |
1056 bool keeping = false; | |
1057 while (true) { | |
1058 char *have = tok.next(); | |
1059 if (!have) break; | |
1060 if (have == token_rbrace) break; | |
1061 if (have == flavor) { | |
1062 keeping = true; | |
1063 continue; | |
1064 } | |
1065 else if ((have == token_ok) || (have == token_ok2) || (have == token_many)) { | |
1066 keeping = false; | |
1067 continue; | |
1068 } | |
1069 if (have == token_envto) { | |
1070 have = tok.next(); | |
1071 if (keeping) { | |
1072 if (me.allow_env_to(have)) { | |
1073 me.add_to(have); | |
1074 dc.add_to(have, &me); | |
1075 } | |
1076 } | |
1077 } | |
1078 //else if (have == token_substitute) { | |
1079 // if (tok.next() == token_mailhost) { | |
1080 // have = tok.next(); | |
1081 // if (keeping) { | |
1082 // if (me.allow_env_to(have)) { | |
1083 // me.add_to(have); | |
1084 // dc.add_to(have, &me); | |
1085 // } | |
1086 // } | |
1087 // } | |
1088 //} | |
1089 tok.skipeol(); | |
1090 } | |
1091 } | |
1092 else if (me.allow_env_to(have)) { | |
1093 me.add_to(have); | |
1094 dc.add_to(have, &me); | |
1095 } | |
1096 else { | |
1097 tok.token_error("user@ or user@domain.tld or domain.tld where domain.tld allowed by parent context", have); | |
1098 return false; | |
1099 } | |
1100 } | |
1101 return tsa(tok, token_semi); | |
1102 } | |
1103 | |
1104 | |
1105 //////////////////////////////////////////////// | |
1106 // | |
1107 bool parse_verify(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1108 bool parse_verify(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1109 char *host = tok.next(); | |
1110 if (!tsa(tok, token_semi)) return false; | |
1111 me.set_verify(host); | |
1112 add_verify_host(host); | |
99 | 1113 return true; |
94 | 1114 } |
1115 | |
1116 | |
1117 //////////////////////////////////////////////// | |
1118 // | |
1119 bool parse_envfrom(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1120 bool parse_envfrom(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1121 char *st = tok.next(); | |
1122 if ((st == token_black) || (st == token_white) || (st == token_unknown) || (st == token_inherit)) { | |
1123 me.set_from_default(st); | |
1124 } | |
1125 else { | |
1126 tok.push(st); | |
1127 } | |
1128 if (!tsa(tok, token_lbrace)) return false; | |
1129 while (true) { | |
1130 char *have = tok.next(); | |
1131 if (!have) break; | |
1132 if (have == token_rbrace) break; | |
1133 if (have == token_semi) { | |
1134 // optional separators | |
1135 } | |
1136 else if (have == token_dccfrom) { | |
1137 if (!tsa(tok, token_lbrace)) return false; | |
1138 bool keeping = false; | |
1139 bool many = false; | |
1140 while (true) { | |
1141 char *have = tok.next(); | |
1142 if (!have) break; | |
1143 if (have == token_rbrace) break; | |
1144 if (have == token_ok) { | |
1145 keeping = true; | |
1146 many = false; | |
1147 continue; | |
1148 } | |
1149 else if (have == token_many) { | |
1150 keeping = true; | |
1151 many = true; | |
1152 continue; | |
1153 } | |
1154 else if (have == token_ok2) { | |
1155 keeping = false; | |
1156 continue; | |
1157 } | |
1158 if (have == token_envfrom) { | |
1159 have = tok.next(); | |
1160 if (keeping) { | |
1161 me.add_from(have, (many) ? token_black : token_white); | |
1162 } | |
1163 } | |
1164 else if (have == token_substitute) { | |
1165 if (tok.next() == token_mailhost) { | |
1166 have = tok.next(); | |
1167 me.add_from(have, (many) ? token_black : token_white); | |
1168 } | |
1169 } | |
1170 tok.skipeol(); | |
1171 } | |
1172 } | |
1173 else { | |
1174 // may be a valid email address or domain name | |
1175 char *st = tok.next(); | |
1176 if ((st == token_black) || (st == token_white) || (st == token_unknown)) { | |
1177 me.add_from(have, st); | |
1178 } | |
1179 else { | |
1180 CONTEXTP con = me.find_from_context_name(st); | |
1181 if (con) { | |
1182 me.add_from_context(have, con); | |
1183 } | |
1184 else { | |
1185 tok.token_error("white/black/unknown or child context name", st); | |
1186 return false; | |
1187 } | |
1188 } | |
1189 } | |
1190 } | |
1191 return tsa(tok, token_semi); | |
1192 } | |
1193 | |
1194 | |
1195 //////////////////////////////////////////////// | |
1196 // | |
136 | 1197 bool parse_rate(TOKEN &tok, CONFIG &dc, CONTEXT &me); |
1198 bool parse_rate(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
140 | 1199 char *def = tok.next(); |
141 | 1200 tok.push(def); |
1201 if (def != token_lbrace) me.set_default_rate(tok.nextint()); | |
136 | 1202 if (!tsa(tok, token_lbrace)) return false; |
1203 while (true) { | |
1204 char *have = tok.next(); | |
1205 if (!have) break; | |
1206 if (have == token_rbrace) break; | |
1207 if (have == token_semi) { | |
1208 // optional separators | |
1209 } | |
1210 else { | |
140 | 1211 me.add_rate(have, tok.nextint()); |
136 | 1212 } |
1213 } | |
1214 return tsa(tok, token_semi); | |
1215 } | |
1216 | |
1217 | |
1218 //////////////////////////////////////////////// | |
1219 // | |
94 | 1220 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent); |
1221 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent) { | |
1222 char *name = tok.next(); | |
1223 if (!tsa(tok, token_lbrace)) return false; | |
1224 CONTEXTP con = new CONTEXT(parent, name); | |
1225 | |
1226 while (true) { | |
1227 char *have = tok.next(); | |
1228 if (!have) break; | |
1229 if (have == token_rbrace) break; // done | |
1230 if (have == token_dnsbl) { | |
1231 if (!parse_dnsbl(tok, dc, *con)) return false; | |
1232 } | |
1233 else if (have == token_dnsbll) { | |
1234 if (!parse_dnsbll(tok, dc, *con)) return false; | |
1235 } | |
1236 else if (have == token_content) { | |
1237 if (!parse_content(tok, dc, *con)) return false; | |
1238 } | |
1239 else if (have == token_envto) { | |
1240 if (!parse_envto(tok, dc, *con)) return false; | |
1241 } | |
1242 else if (have == token_verify) { | |
1243 if (!parse_verify(tok, dc, *con)) return false; | |
1244 } | |
1245 else if (have == token_envfrom) { | |
1246 if (!parse_envfrom(tok, dc, *con)) return false; | |
1247 } | |
140 | 1248 else if (have == token_rate) { |
1249 if (parent || dc.default_context) tok.token_error("rate limit ignored in non default context"); | |
136 | 1250 if (!parse_rate(tok, dc, *con)) return false; |
1251 } | |
94 | 1252 else if (have == token_context) { |
1253 if (!parse_context(tok, dc, con)) return false; | |
1254 } | |
1255 else { | |
1256 tok.token_error("context keyword", have); | |
1257 return false; | |
1258 } | |
1259 } | |
1260 | |
1261 if (!tsa(tok, token_semi)) { | |
1262 delete con; | |
1263 return false; | |
1264 } | |
1265 dc.add_context(con); | |
1266 if (parent) parent->add_context(con); | |
1267 return true; | |
1268 } | |
1269 | |
1270 | |
1271 //////////////////////////////////////////////// | |
1272 // parse a config file | |
1273 // | |
1274 bool load_conf(CONFIG &dc, char *fn) { | |
99 | 1275 int count = 0; |
94 | 1276 TOKEN tok(fn, &dc.config_files); |
1277 while (true) { | |
1278 char *have = tok.next(); | |
1279 if (!have) break; | |
1280 if (have == token_context) { | |
1281 if (!parse_context(tok, dc, NULL)) { | |
99 | 1282 tok.token_error("load_conf() failed to parse context"); |
94 | 1283 return false; |
1284 } | |
99 | 1285 else count++; |
94 | 1286 } |
1287 else { | |
1288 tok.token_error(token_context, have); | |
1289 return false; | |
1290 } | |
1291 } | |
99 | 1292 tok.token_error("load_conf() found %d contexts in %s", count, fn); |
94 | 1293 return (dc.default_context) ? true : false; |
1294 } | |
1295 | |
1296 | |
1297 //////////////////////////////////////////////// | |
1298 // setup a new smtp verify host | |
1299 // | |
1300 void add_verify_host(char *host) { | |
1301 verify_map::iterator i = verifiers.find(host); | |
1302 if (i == verifiers.end()) { | |
1303 VERIFYP v = new VERIFY(host); | |
1304 verifiers[host] = v; | |
1305 } | |
1306 } | |
1307 | |
1308 | |
1309 //////////////////////////////////////////////// | |
1310 // thread to check for verify hosts with old sockets that we can close | |
1311 // | |
1312 void* verify_closer(void *arg) { | |
1313 while (true) { | |
1314 sleep(maxage); | |
1315 for (verify_map::iterator i=verifiers.begin(); i!=verifiers.end(); i++) { | |
1316 VERIFYP v = (*i).second; | |
1317 v->closer(); | |
1318 } | |
1319 } | |
1320 return NULL; | |
1321 } | |
1322 | |
1323 | |
1324 //////////////////////////////////////////////// | |
1325 // init the tokens | |
1326 // | |
1327 void token_init() { | |
1328 token_black = register_string("black"); | |
117 | 1329 token_cctld = register_string("cctld"); |
94 | 1330 token_content = register_string("content"); |
1331 token_context = register_string("context"); | |
1332 token_dccfrom = register_string("dcc_from"); | |
1333 token_dccto = register_string("dcc_to"); | |
1334 token_default = register_string("default"); | |
1335 token_dnsbl = register_string("dnsbl"); | |
1336 token_dnsbll = register_string("dnsbl_list"); | |
1337 token_envfrom = register_string("env_from"); | |
1338 token_envto = register_string("env_to"); | |
1339 token_filter = register_string("filter"); | |
1340 token_host_limit = register_string("host_limit"); | |
1341 token_html_limit = register_string("html_limit"); | |
1342 token_html_tags = register_string("html_tags"); | |
1343 token_ignore = register_string("ignore"); | |
1344 token_include = register_string("include"); | |
1345 token_inherit = register_string("inherit"); | |
1346 token_lbrace = register_string("{"); | |
1347 token_mailhost = register_string("mail_host"); | |
1348 token_many = register_string("many"); | |
1349 token_off = register_string("off"); | |
1350 token_ok = register_string("ok"); | |
1351 token_ok2 = register_string("ok2"); | |
1352 token_on = register_string("on"); | |
136 | 1353 token_rate = register_string("rate_limit"); |
94 | 1354 token_rbrace = register_string("}"); |
1355 token_semi = register_string(";"); | |
1356 token_soft = register_string("soft"); | |
1357 token_substitute = register_string("substitute"); | |
1358 token_tld = register_string("tld"); | |
1359 token_unknown = register_string("unknown"); | |
119 | 1360 token_uribl = register_string("uribl"); |
94 | 1361 token_verify = register_string("verify"); |
1362 token_white = register_string("white"); | |
1363 | |
1364 if (gethostname(myhostname, HOST_NAME_MAX+1) != 0) { | |
1365 strncpy(myhostname, "localhost", HOST_NAME_MAX+1); | |
1366 } | |
1367 myhostname[HOST_NAME_MAX] = '\0'; // ensure null termination | |
1368 token_myhostname = register_string(myhostname); | |
1369 } |