Mercurial > dnsbl
annotate src/context.cpp @ 146:7278c9766e26
free old configs when last reference goes away
author | carl |
---|---|
date | Sun, 15 Oct 2006 17:21:07 -0700 |
parents | 9b9bab1d3c21 |
children | 9330b8d6a56b |
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() { | |
146 | 398 if (debug_syslog) { |
399 char buf[maxlen]; | |
400 snprintf(buf, sizeof(buf), "freeing memory for old configuration generation %d", generation); | |
401 my_syslog(buf); | |
402 } | |
94 | 403 for (context_list::iterator i=contexts.begin(); i!=contexts.end(); i++) { |
404 CONTEXT *c = *i; | |
405 delete c; | |
406 } | |
407 } | |
408 | |
409 | |
410 void CONFIG::add_context(CONTEXTP con) { | |
411 contexts.push_back(con); | |
412 if (!default_context && !con->get_parent()) { | |
413 // first global context | |
414 default_context = con; | |
415 } | |
416 } | |
417 | |
418 | |
419 void CONFIG::add_to(char *to, CONTEXTP con) { | |
420 context_map::iterator i = env_to.find(to); | |
421 if (i != env_to.end()) { | |
422 CONTEXTP c = (*i).second; | |
423 int s = strlen(to); | |
424 bool at = s && (to[s-1] == '@'); | |
425 if (at && con->is_parent(c->get_parent())) { | |
426 if (debug_syslog) { | |
427 char oldname[maxlen]; | |
428 char newname[maxlen]; | |
429 char *oldn = c->get_full_name(oldname, maxlen); | |
430 char *newn = con->get_full_name(newname, maxlen); | |
431 char buf[maxlen*3]; | |
432 snprintf(buf, maxlen*3, "both %s and %s claim envelope to %s, the first one wins", oldn, newn, to); | |
433 my_syslog(buf); | |
434 } | |
435 return; // don't take over user@ entries from your ancestors children | |
436 } | |
437 if ((c != con) && (c != con->get_parent())) { | |
438 if (debug_syslog) { | |
439 char oldname[maxlen]; | |
440 char newname[maxlen]; | |
441 char *oldn = c->get_full_name(oldname, maxlen); | |
442 char *newn = con->get_full_name(newname, maxlen); | |
443 char buf[maxlen*3]; | |
444 snprintf(buf, maxlen*3, "both %s and %s claim envelope to %s, the second one wins", oldn, newn, to); | |
445 my_syslog(buf); | |
446 } | |
447 } | |
448 } | |
449 env_to[to] = con; | |
450 } | |
451 | |
452 | |
453 CONTEXTP CONFIG::find_context(char *to) { | |
454 context_map::iterator i = env_to.find(to); | |
117 | 455 if (i != env_to.end()) return (*i).second; // found user@domain key |
94 | 456 char *x = strchr(to, '@'); |
457 if (x) { | |
458 x++; | |
459 i = env_to.find(x); | |
117 | 460 if (i != env_to.end()) return (*i).second; // found domain key |
94 | 461 char y = *x; |
462 *x = '\0'; | |
463 i = env_to.find(to); | |
464 *x = y; | |
465 if (i != env_to.end()) return (*i).second; // found user@ key | |
466 } | |
467 return default_context; | |
468 } | |
469 | |
470 | |
471 void CONFIG::dump() { | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
472 if (default_context) default_context->dump(true); |
94 | 473 for (context_list::iterator i=contexts.begin(); i!=contexts.end(); i++) { |
474 CONTEXTP c = *i; | |
475 CONTEXTP p = c->get_parent(); | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
476 if (!p && (c != default_context)) c->dump(false); |
94 | 477 } |
478 char buf[maxlen]; | |
479 for (context_map::iterator i=env_to.begin(); i!=env_to.end(); i++) { | |
480 char *to = (*i).first; | |
481 CONTEXTP con = (*i).second; | |
482 printf("// envelope to %s \t-> context %s \n", to, con->get_full_name(buf,maxlen)); | |
483 } | |
484 } | |
485 | |
486 | |
487 CONTEXT::CONTEXT(CONTEXTP parent_, char *name_) { | |
488 parent = parent_; | |
489 name = name_; | |
490 verify_host = NULL; | |
491 env_from_default = (parent) ? token_inherit : token_unknown; | |
492 content_filtering = (parent) ? parent->content_filtering : false; | |
493 content_suffix = NULL; | |
494 content_message = NULL; | |
119 | 495 uribl_suffix = NULL; |
496 uribl_message = NULL; | |
94 | 497 host_limit = (parent) ? parent->host_limit : 0; |
498 host_limit_message = NULL; | |
499 host_random = (parent) ? parent->host_random : false; | |
500 tag_limit = (parent) ? parent->tag_limit : 0; | |
501 tag_limit_message = NULL; | |
140 | 502 default_rcpt_rate = INT_MAX; |
94 | 503 } |
504 | |
505 | |
506 CONTEXT::~CONTEXT() { | |
507 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) { | |
508 DNSBLP d = (*i).second; | |
509 // delete the underlying DNSBL objects. | |
510 delete d; | |
511 } | |
512 } | |
513 | |
514 | |
515 bool CONTEXT::is_parent(CONTEXTP p) { | |
516 if (p == parent) return true; | |
517 if (!parent) return false; | |
518 return parent->is_parent(p); | |
519 } | |
520 | |
521 | |
522 char *CONTEXT::get_full_name(char *buffer, int size) { | |
523 if (!parent) return name; | |
524 char buf[maxlen]; | |
525 snprintf(buffer, size, "%s.%s", parent->get_full_name(buf, maxlen), name); | |
526 return buffer; | |
527 } | |
528 | |
529 | |
530 bool CONTEXT::cover_env_to(char *to) { | |
531 char buffer[maxlen]; | |
532 char *x = strchr(to, '@'); | |
533 if (x) x++; | |
534 else x = to; | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
535 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
|
536 if (!parent && env_to.empty()) return true; // empty env_to at global level covers everything |
94 | 537 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
|
538 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
|
539 if (x != to) { |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
540 i = env_to.find(to); |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
541 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
|
542 } |
94 | 543 return false; |
544 } | |
545 | |
546 | |
547 VERIFYP CONTEXT::find_verify(char *to) { | |
548 if (verify_host && (verify_host != token_myhostname) && cover_env_to(to)) { | |
549 verify_map::iterator i = verifiers.find(verify_host); | |
550 if (i == verifiers.end()) { | |
551 if (debug_syslog) { | |
552 char buf[maxlen]; | |
553 snprintf(buf, maxlen, "cannot find struc for %s", verify_host); | |
554 my_syslog(buf); | |
555 } | |
556 return NULL; | |
557 } | |
558 VERIFYP v = (*i).second; | |
559 | |
560 return v; | |
561 } | |
562 else if (parent) return parent->find_verify(to); | |
563 else return NULL; | |
564 } | |
565 | |
566 | |
136 | 567 int CONTEXT::find_rate(char *user) { |
140 | 568 if (rcpt_per_hour.empty()) return default_rcpt_rate; |
136 | 569 rcpt_rates::iterator i = rcpt_per_hour.find(user); |
140 | 570 return (i == rcpt_per_hour.end()) ? default_rcpt_rate : (*i).second; |
136 | 571 } |
572 | |
573 | |
94 | 574 char *CONTEXT::find_from(char *from) { |
575 char *rc = token_inherit; | |
576 string_map::iterator i = env_from.find(from); | |
117 | 577 if (i != env_from.end()) rc = (*i).second; // found user@domain key |
94 | 578 else { |
579 char *x = strchr(from, '@'); | |
580 if (x) { | |
581 x++; | |
582 i = env_from.find(x); | |
117 | 583 if (i != env_from.end()) rc = (*i).second; // found domain key |
94 | 584 else { |
585 char y = *x; | |
586 *x = '\0'; | |
587 i = env_from.find(from); | |
588 *x = y; | |
589 if (i != env_from.end()) rc = (*i).second; // found user@ key | |
590 } | |
591 } | |
592 } | |
593 if (rc == token_inherit) rc = env_from_default; | |
594 if ((rc == token_inherit) && parent) return parent->find_from(from); | |
595 return (rc == token_inherit) ? token_unknown : rc; | |
596 } | |
597 | |
598 | |
599 CONTEXTP CONTEXT::find_context(char *from) { | |
600 context_map::iterator i = env_from_context.find(from); | |
117 | 601 if (i != env_from_context.end()) return (*i).second; // found user@domain key |
94 | 602 char *x = strchr(from, '@'); |
603 if (x) { | |
604 x++; | |
605 i = env_from_context.find(x); | |
117 | 606 if (i != env_from_context.end()) return (*i).second; // found domain key |
94 | 607 char y = *x; |
608 *x = '\0'; | |
609 i = env_from_context.find(from); | |
610 *x = y; | |
611 if (i != env_from_context.end()) return (*i).second; // found user@ key | |
612 } | |
613 return this; | |
614 } | |
615 | |
616 | |
617 CONTEXTP CONTEXT::find_from_context_name(char *name) { | |
618 context_map::iterator i = children.find(name); | |
619 if (i != children.end()) return (*i).second; | |
620 return NULL; | |
621 } | |
622 | |
623 | |
624 DNSBLP CONTEXT::find_dnsbl(char *name) { | |
625 dnsblp_map::iterator i = dnsbl_names.find(name); | |
626 if (i != dnsbl_names.end()) return (*i).second; | |
627 if (parent) return parent->find_dnsbl(name); | |
628 return NULL; | |
629 } | |
630 | |
631 | |
632 char* CONTEXT::get_content_suffix() { | |
633 if (!content_suffix && parent) return parent->get_content_suffix(); | |
634 return content_suffix; | |
635 } | |
636 | |
637 | |
119 | 638 char* CONTEXT::get_uribl_suffix() { |
639 if (!uribl_suffix && parent) return parent->get_uribl_suffix(); | |
640 return uribl_suffix; | |
641 } | |
642 | |
643 | |
94 | 644 char* CONTEXT::get_content_message() { |
645 if (!content_message && parent) return parent->get_content_message(); | |
646 return content_message; | |
647 } | |
648 | |
649 | |
119 | 650 char* CONTEXT::get_uribl_message() { |
651 if (!uribl_message && parent) return parent->get_uribl_message(); | |
652 return uribl_message; | |
653 } | |
654 | |
655 | |
94 | 656 string_set& CONTEXT::get_content_host_ignore() { |
657 if (content_host_ignore.empty() && parent) return parent->get_content_host_ignore(); | |
658 return content_host_ignore; | |
659 } | |
660 | |
661 | |
117 | 662 string_set& CONTEXT::get_content_cctlds() { |
663 if (content_cctlds.empty() && parent) return parent->get_content_cctlds(); | |
664 return content_cctlds; | |
665 } | |
666 | |
94 | 667 string_set& CONTEXT::get_content_tlds() { |
668 if (content_tlds.empty() && parent) return parent->get_content_tlds(); | |
669 return content_tlds; | |
670 } | |
671 | |
672 | |
673 string_set& CONTEXT::get_html_tags() { | |
674 if (html_tags.empty() && parent) return parent->get_html_tags(); | |
675 return html_tags; | |
676 } | |
677 | |
678 | |
679 dnsblp_list& CONTEXT::get_dnsbl_list() { | |
680 if (dnsbl_list.empty() && parent) return parent->get_dnsbl_list(); | |
681 return dnsbl_list; | |
682 } | |
683 | |
684 | |
685 bool CONTEXT::acceptable_content(recorder &memory, char *&msg) { | |
686 if (memory.excessive_bad_tags(tag_limit)) { | |
687 msg = tag_limit_message; | |
688 return false; | |
689 } | |
690 if (!host_random && memory.excessive_hosts(host_limit)) { | |
691 msg = host_limit_message; | |
692 return false; | |
693 } | |
694 return true; | |
695 } | |
696 | |
697 | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
698 void CONTEXT::dump(bool isdefault, int level) { |
94 | 699 char indent[maxlen]; |
700 int i = min(maxlen-1, level*4); | |
701 memset(indent, ' ', i); | |
702 indent[i] = '\0'; | |
703 char buf[maxlen]; | |
704 char *fullname = get_full_name(buf,maxlen); | |
705 printf("%s context %s { \t// %s\n", indent, name, fullname); | |
706 | |
707 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) { | |
708 char *n = (*i).first; | |
709 DNSBL &d = *(*i).second; | |
710 printf("%s dnsbl %s %s \"%s\"; \n", indent, n, d.suffix, d.message); | |
711 } | |
712 | |
145 | 713 dnsblp_list dl = get_dnsbl_list(); |
714 if (!dl.empty()) { | |
94 | 715 printf("%s dnsbl_list", indent); |
145 | 716 for (dnsblp_list::iterator i=dl.begin(); i!=dl.end(); i++) { |
94 | 717 DNSBL &d = *(*i); |
718 printf(" %s", d.name); | |
719 } | |
720 printf("; \n"); | |
721 } | |
722 | |
723 if (content_filtering) { | |
724 printf("%s content on { \n", indent, env_from_default); | |
725 if (content_suffix) { | |
726 printf("%s filter %s \"%s\"; \n", indent, content_suffix, content_message); | |
727 } | |
119 | 728 if (uribl_suffix) { |
729 printf("%s uribl %s \"%s\"; \n", indent, uribl_suffix, uribl_message); | |
730 } | |
94 | 731 if (!content_host_ignore.empty()) { |
732 printf("%s ignore { \n", indent); | |
733 for (string_set::iterator i=content_host_ignore.begin(); i!=content_host_ignore.end(); i++) { | |
734 printf("%s %s; \n", indent, *i); | |
735 } | |
736 printf("%s }; \n", indent); | |
737 } | |
117 | 738 if (!content_cctlds.empty()) { |
739 printf("%s cctld { \n", indent); | |
740 printf("%s ", indent); | |
741 for (string_set::iterator i=content_cctlds.begin(); i!=content_cctlds.end(); i++) { | |
742 printf("%s; ", *i); | |
743 } | |
744 printf("\n%s }; \n", indent); | |
745 } | |
94 | 746 if (!content_tlds.empty()) { |
747 printf("%s tld { \n", indent); | |
748 printf("%s ", indent); | |
749 for (string_set::iterator i=content_tlds.begin(); i!=content_tlds.end(); i++) { | |
750 printf("%s; ", *i); | |
751 } | |
752 printf("\n%s }; \n", indent); | |
753 } | |
754 if (!html_tags.empty()) { | |
755 printf("%s html_tags { \n", indent); | |
756 printf("%s ", indent); | |
757 for (string_set::iterator i=html_tags.begin(); i!=html_tags.end(); i++) { | |
758 printf("%s; ", *i); | |
759 } | |
760 printf("\n%s }; \n", indent); | |
761 } | |
762 if (host_limit_message) { | |
763 printf("%s host_limit on %d \"%s\"; \n", indent, host_limit, host_limit_message); | |
764 } | |
765 else if (host_random) { | |
766 printf("%s host_limit soft %d; \n", indent, host_limit); | |
767 } | |
768 else { | |
769 printf("%s host_limit off; \n", indent); | |
770 } | |
771 if (tag_limit_message) { | |
772 printf("%s html_limit on %d \"%s\"; \n", indent, tag_limit, tag_limit_message); | |
773 } | |
774 else { | |
775 printf("%s html_limit off; \n", indent); | |
776 } | |
777 printf("%s }; \n", indent); | |
778 } | |
779 else { | |
780 printf("%s content off {}; \n", indent, env_from_default); | |
781 } | |
782 | |
783 printf("%s env_to { \t// %s\n", indent, fullname); | |
784 for (string_set::iterator i=env_to.begin(); i!=env_to.end(); i++) { | |
785 printf("%s %s; \n", indent, *i); | |
786 } | |
787 printf("%s }; \n", indent); | |
788 | |
789 if (verify_host) { | |
790 printf("%s verify %s; \n", indent, verify_host); | |
791 } | |
792 | |
793 for (context_map::iterator i=children.begin(); i!=children.end(); i++) { | |
794 CONTEXTP c = (*i).second; | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
795 c->dump(false, level+1); |
94 | 796 } |
797 | |
798 printf("%s env_from %s { \t// %s\n", indent, env_from_default, fullname); | |
799 if (!env_from.empty()) { | |
800 printf("%s // white/black/unknown \n", indent); | |
801 for (string_map::iterator i=env_from.begin(); i!=env_from.end(); i++) { | |
802 char *f = (*i).first; | |
803 char *t = (*i).second; | |
804 printf("%s %s \t%s; \n", indent, f, t); | |
805 } | |
806 } | |
807 if (!env_from_context.empty()) { | |
808 printf("%s // child contexts \n", indent); | |
809 for (context_map::iterator j=env_from_context.begin(); j!=env_from_context.end(); j++) { | |
810 char *f = (*j).first; | |
811 CONTEXTP t = (*j).second; | |
812 printf("%s %s \t%s; \n", indent, f, t->name); | |
813 } | |
814 } | |
815 printf("%s }; \n", indent); | |
816 | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
817 if (isdefault) { |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
818 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
|
819 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
|
820 char *u = (*j).first; |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
821 int l = (*j).second; |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
822 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
|
823 } |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
824 printf("%s }; \n", indent); |
136 | 825 } |
826 | |
94 | 827 printf("%s }; \n", indent); |
828 } | |
829 | |
830 | |
831 //////////////////////////////////////////////// | |
832 // helper to discard the strings held by a string_set | |
833 // | |
834 void discard(string_set &s) { | |
835 for (string_set::iterator i=s.begin(); i!=s.end(); i++) { | |
836 free(*i); | |
837 } | |
838 s.clear(); | |
839 } | |
840 | |
841 | |
842 //////////////////////////////////////////////// | |
843 // helper to register a string in a string set | |
844 // | |
845 char* register_string(string_set &s, char *name) { | |
846 string_set::iterator i = s.find(name); | |
847 if (i != s.end()) return *i; | |
848 char *x = strdup(name); | |
849 s.insert(x); | |
850 return x; | |
851 } | |
852 | |
853 | |
854 //////////////////////////////////////////////// | |
855 // register a global string | |
856 // | |
857 char* register_string(char *name) { | |
858 return register_string(all_strings, name); | |
859 } | |
860 | |
861 | |
862 //////////////////////////////////////////////// | |
863 // | |
864 bool tsa(TOKEN &tok, char *token); | |
865 bool tsa(TOKEN &tok, char *token) { | |
866 char *have = tok.next(); | |
867 if (have == token) return true; | |
868 tok.token_error(token, have); | |
869 return false; | |
870 } | |
871 | |
872 | |
873 //////////////////////////////////////////////// | |
874 // | |
875 bool parse_dnsbl(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
876 bool parse_dnsbl(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
877 char *name = tok.next(); | |
878 char *suf = tok.next(); | |
879 char *msg = tok.next(); | |
880 if (!tsa(tok, token_semi)) return false; | |
881 DNSBLP dnsnew = new DNSBL(name, suf, msg); | |
882 DNSBLP dnsold = me.find_dnsbl(name); | |
883 if (dnsold && (*dnsold == *dnsnew)) { | |
884 // duplicate redefinition, ignore it | |
885 delete dnsnew; | |
886 return true; | |
887 } | |
888 me.add_dnsbl(name, dnsnew); | |
889 return true; | |
890 } | |
891 | |
892 | |
893 //////////////////////////////////////////////// | |
894 // | |
895 bool parse_dnsbll(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
896 bool parse_dnsbll(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
897 while (true) { | |
898 char *have = tok.next(); | |
899 if (!have) break; | |
900 if (have == token_semi) break; | |
901 DNSBLP dns = me.find_dnsbl(have); | |
902 if (dns) { | |
903 me.add_dnsbl(dns); | |
904 } | |
905 else { | |
906 tok.token_error("dnsbl name", have); | |
907 return false; | |
908 } | |
909 } | |
910 return true; | |
911 } | |
912 | |
913 | |
914 //////////////////////////////////////////////// | |
915 // | |
916 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
917 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
918 char *setting = tok.next(); | |
919 if (setting == token_on) { | |
920 me.set_content_filtering(true); | |
921 } | |
922 else if (setting == token_off) { | |
923 me.set_content_filtering(false); | |
924 } | |
925 else { | |
926 tok.token_error("on/off", setting); | |
927 return false; | |
928 } | |
929 if (!tsa(tok, token_lbrace)) return false; | |
930 while (true) { | |
931 char *have = tok.next(); | |
932 if (!have) break; | |
933 if (have == token_filter) { | |
934 char *suffix = tok.next(); | |
935 char *messag = tok.next(); | |
936 me.set_content_suffix(suffix); | |
937 me.set_content_message(messag); | |
938 if (!tsa(tok, token_semi)) return false; | |
939 } | |
119 | 940 else if (have == token_uribl) { |
941 char *suffix = tok.next(); | |
942 char *messag = tok.next(); | |
943 me.set_uribl_suffix(suffix); | |
944 me.set_uribl_message(messag); | |
945 if (!tsa(tok, token_semi)) return false; | |
946 } | |
94 | 947 else if (have == token_ignore) { |
948 if (!tsa(tok, token_lbrace)) return false; | |
949 while (true) { | |
950 if (!have) break; | |
951 char *have = tok.next(); | |
952 if (have == token_rbrace) break; // done | |
953 me.add_ignore(have); | |
954 } | |
955 if (!tsa(tok, token_semi)) return false; | |
956 } | |
117 | 957 else if (have == token_cctld) { |
958 if (!tsa(tok, token_lbrace)) return false; | |
959 while (true) { | |
960 char *have = tok.next(); | |
961 if (!have) break; | |
962 if (have == token_rbrace) break; // done | |
963 me.add_cctld(have); | |
964 } | |
965 if (!tsa(tok, token_semi)) return false; | |
966 } | |
94 | 967 else if (have == token_tld) { |
968 if (!tsa(tok, token_lbrace)) return false; | |
969 while (true) { | |
970 char *have = tok.next(); | |
971 if (!have) break; | |
972 if (have == token_rbrace) break; // done | |
973 me.add_tld(have); | |
974 } | |
975 if (!tsa(tok, token_semi)) return false; | |
976 } | |
977 else if (have == token_html_limit) { | |
978 have = tok.next(); | |
979 if (have == token_on) { | |
980 me.set_tag_limit(tok.nextint()); | |
981 me.set_tag_message(tok.next()); | |
982 } | |
983 else if (have == token_off) { | |
984 me.set_tag_limit(0); | |
985 me.set_tag_message(NULL); | |
986 } | |
987 else { | |
988 tok.token_error("on/off", have); | |
989 return false; | |
990 } | |
991 if (!tsa(tok, token_semi)) return false; | |
992 } | |
993 else if (have == token_html_tags) { | |
994 if (!tsa(tok, token_lbrace)) return false; | |
995 while (true) { | |
996 char *have = tok.next(); | |
997 if (!have) break; | |
998 if (have == token_rbrace) { | |
999 break; // done | |
1000 } | |
1001 else { | |
1002 me.add_tag(have); // base version | |
1003 char buf[200]; | |
1004 snprintf(buf, sizeof(buf), "/%s", have); | |
1005 me.add_tag(register_string(buf)); // leading / | |
1006 snprintf(buf, sizeof(buf), "%s/", have); | |
1007 me.add_tag(register_string(buf)); // trailing / | |
1008 } | |
1009 } | |
1010 if (!tsa(tok, token_semi)) return false; | |
1011 } | |
1012 else if (have == token_host_limit) { | |
1013 have = tok.next(); | |
1014 if (have == token_on) { | |
1015 me.set_host_limit(tok.nextint()); | |
1016 me.set_host_message(tok.next()); | |
1017 me.set_host_random(false); | |
1018 } | |
1019 else if (have == token_off) { | |
1020 me.set_host_limit(0); | |
1021 me.set_host_message(NULL); | |
1022 me.set_host_random(false); | |
1023 } | |
1024 else if (have == token_soft) { | |
1025 me.set_host_limit(tok.nextint()); | |
1026 me.set_host_message(NULL); | |
1027 me.set_host_random(true); | |
1028 } | |
1029 else { | |
1030 tok.token_error("on/off/soft", have); | |
1031 return false; | |
1032 } | |
1033 if (!tsa(tok, token_semi)) return false; | |
1034 } | |
1035 else if (have == token_rbrace) { | |
1036 break; // done | |
1037 } | |
1038 else { | |
1039 tok.token_error("content keyword", have); | |
1040 return false; | |
1041 } | |
1042 } | |
1043 return tsa(tok, token_semi); | |
1044 } | |
1045 | |
1046 | |
1047 //////////////////////////////////////////////// | |
1048 // | |
1049 bool parse_envto(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1050 bool parse_envto(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1051 if (!tsa(tok, token_lbrace)) return false; | |
1052 while (true) { | |
1053 char *have = tok.next(); | |
1054 if (!have) break; | |
1055 if (have == token_rbrace) break; | |
1056 if (have == token_semi) { | |
1057 // optional separators | |
1058 } | |
1059 else if (have == token_dccto) { | |
1060 char *flavor = tok.next(); | |
1061 if (!tsa(tok, token_lbrace)) return false; | |
1062 bool keeping = false; | |
1063 while (true) { | |
1064 char *have = tok.next(); | |
1065 if (!have) break; | |
1066 if (have == token_rbrace) break; | |
1067 if (have == flavor) { | |
1068 keeping = true; | |
1069 continue; | |
1070 } | |
1071 else if ((have == token_ok) || (have == token_ok2) || (have == token_many)) { | |
1072 keeping = false; | |
1073 continue; | |
1074 } | |
1075 if (have == token_envto) { | |
1076 have = tok.next(); | |
1077 if (keeping) { | |
1078 if (me.allow_env_to(have)) { | |
1079 me.add_to(have); | |
1080 dc.add_to(have, &me); | |
1081 } | |
1082 } | |
1083 } | |
1084 //else if (have == token_substitute) { | |
1085 // if (tok.next() == token_mailhost) { | |
1086 // have = tok.next(); | |
1087 // if (keeping) { | |
1088 // if (me.allow_env_to(have)) { | |
1089 // me.add_to(have); | |
1090 // dc.add_to(have, &me); | |
1091 // } | |
1092 // } | |
1093 // } | |
1094 //} | |
1095 tok.skipeol(); | |
1096 } | |
1097 } | |
1098 else if (me.allow_env_to(have)) { | |
1099 me.add_to(have); | |
1100 dc.add_to(have, &me); | |
1101 } | |
1102 else { | |
1103 tok.token_error("user@ or user@domain.tld or domain.tld where domain.tld allowed by parent context", have); | |
1104 return false; | |
1105 } | |
1106 } | |
1107 return tsa(tok, token_semi); | |
1108 } | |
1109 | |
1110 | |
1111 //////////////////////////////////////////////// | |
1112 // | |
1113 bool parse_verify(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1114 bool parse_verify(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1115 char *host = tok.next(); | |
1116 if (!tsa(tok, token_semi)) return false; | |
1117 me.set_verify(host); | |
1118 add_verify_host(host); | |
99 | 1119 return true; |
94 | 1120 } |
1121 | |
1122 | |
1123 //////////////////////////////////////////////// | |
1124 // | |
1125 bool parse_envfrom(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1126 bool parse_envfrom(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1127 char *st = tok.next(); | |
1128 if ((st == token_black) || (st == token_white) || (st == token_unknown) || (st == token_inherit)) { | |
1129 me.set_from_default(st); | |
1130 } | |
1131 else { | |
1132 tok.push(st); | |
1133 } | |
1134 if (!tsa(tok, token_lbrace)) return false; | |
1135 while (true) { | |
1136 char *have = tok.next(); | |
1137 if (!have) break; | |
1138 if (have == token_rbrace) break; | |
1139 if (have == token_semi) { | |
1140 // optional separators | |
1141 } | |
1142 else if (have == token_dccfrom) { | |
1143 if (!tsa(tok, token_lbrace)) return false; | |
1144 bool keeping = false; | |
1145 bool many = false; | |
1146 while (true) { | |
1147 char *have = tok.next(); | |
1148 if (!have) break; | |
1149 if (have == token_rbrace) break; | |
1150 if (have == token_ok) { | |
1151 keeping = true; | |
1152 many = false; | |
1153 continue; | |
1154 } | |
1155 else if (have == token_many) { | |
1156 keeping = true; | |
1157 many = true; | |
1158 continue; | |
1159 } | |
1160 else if (have == token_ok2) { | |
1161 keeping = false; | |
1162 continue; | |
1163 } | |
1164 if (have == token_envfrom) { | |
1165 have = tok.next(); | |
1166 if (keeping) { | |
1167 me.add_from(have, (many) ? token_black : token_white); | |
1168 } | |
1169 } | |
1170 else if (have == token_substitute) { | |
1171 if (tok.next() == token_mailhost) { | |
1172 have = tok.next(); | |
1173 me.add_from(have, (many) ? token_black : token_white); | |
1174 } | |
1175 } | |
1176 tok.skipeol(); | |
1177 } | |
1178 } | |
1179 else { | |
1180 // may be a valid email address or domain name | |
1181 char *st = tok.next(); | |
1182 if ((st == token_black) || (st == token_white) || (st == token_unknown)) { | |
1183 me.add_from(have, st); | |
1184 } | |
1185 else { | |
1186 CONTEXTP con = me.find_from_context_name(st); | |
1187 if (con) { | |
1188 me.add_from_context(have, con); | |
1189 } | |
1190 else { | |
1191 tok.token_error("white/black/unknown or child context name", st); | |
1192 return false; | |
1193 } | |
1194 } | |
1195 } | |
1196 } | |
1197 return tsa(tok, token_semi); | |
1198 } | |
1199 | |
1200 | |
1201 //////////////////////////////////////////////// | |
1202 // | |
136 | 1203 bool parse_rate(TOKEN &tok, CONFIG &dc, CONTEXT &me); |
1204 bool parse_rate(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
140 | 1205 char *def = tok.next(); |
141 | 1206 tok.push(def); |
1207 if (def != token_lbrace) me.set_default_rate(tok.nextint()); | |
136 | 1208 if (!tsa(tok, token_lbrace)) return false; |
1209 while (true) { | |
1210 char *have = tok.next(); | |
1211 if (!have) break; | |
1212 if (have == token_rbrace) break; | |
1213 if (have == token_semi) { | |
1214 // optional separators | |
1215 } | |
1216 else { | |
140 | 1217 me.add_rate(have, tok.nextint()); |
136 | 1218 } |
1219 } | |
1220 return tsa(tok, token_semi); | |
1221 } | |
1222 | |
1223 | |
1224 //////////////////////////////////////////////// | |
1225 // | |
94 | 1226 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent); |
1227 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent) { | |
1228 char *name = tok.next(); | |
1229 if (!tsa(tok, token_lbrace)) return false; | |
1230 CONTEXTP con = new CONTEXT(parent, name); | |
1231 | |
1232 while (true) { | |
1233 char *have = tok.next(); | |
1234 if (!have) break; | |
1235 if (have == token_rbrace) break; // done | |
1236 if (have == token_dnsbl) { | |
1237 if (!parse_dnsbl(tok, dc, *con)) return false; | |
1238 } | |
1239 else if (have == token_dnsbll) { | |
1240 if (!parse_dnsbll(tok, dc, *con)) return false; | |
1241 } | |
1242 else if (have == token_content) { | |
1243 if (!parse_content(tok, dc, *con)) return false; | |
1244 } | |
1245 else if (have == token_envto) { | |
1246 if (!parse_envto(tok, dc, *con)) return false; | |
1247 } | |
1248 else if (have == token_verify) { | |
1249 if (!parse_verify(tok, dc, *con)) return false; | |
1250 } | |
1251 else if (have == token_envfrom) { | |
1252 if (!parse_envfrom(tok, dc, *con)) return false; | |
1253 } | |
140 | 1254 else if (have == token_rate) { |
1255 if (parent || dc.default_context) tok.token_error("rate limit ignored in non default context"); | |
136 | 1256 if (!parse_rate(tok, dc, *con)) return false; |
1257 } | |
94 | 1258 else if (have == token_context) { |
1259 if (!parse_context(tok, dc, con)) return false; | |
1260 } | |
1261 else { | |
1262 tok.token_error("context keyword", have); | |
1263 return false; | |
1264 } | |
1265 } | |
1266 | |
1267 if (!tsa(tok, token_semi)) { | |
1268 delete con; | |
1269 return false; | |
1270 } | |
1271 dc.add_context(con); | |
1272 if (parent) parent->add_context(con); | |
1273 return true; | |
1274 } | |
1275 | |
1276 | |
1277 //////////////////////////////////////////////// | |
1278 // parse a config file | |
1279 // | |
1280 bool load_conf(CONFIG &dc, char *fn) { | |
99 | 1281 int count = 0; |
94 | 1282 TOKEN tok(fn, &dc.config_files); |
1283 while (true) { | |
1284 char *have = tok.next(); | |
1285 if (!have) break; | |
1286 if (have == token_context) { | |
1287 if (!parse_context(tok, dc, NULL)) { | |
99 | 1288 tok.token_error("load_conf() failed to parse context"); |
94 | 1289 return false; |
1290 } | |
99 | 1291 else count++; |
94 | 1292 } |
1293 else { | |
1294 tok.token_error(token_context, have); | |
1295 return false; | |
1296 } | |
1297 } | |
99 | 1298 tok.token_error("load_conf() found %d contexts in %s", count, fn); |
94 | 1299 return (dc.default_context) ? true : false; |
1300 } | |
1301 | |
1302 | |
1303 //////////////////////////////////////////////// | |
1304 // setup a new smtp verify host | |
1305 // | |
1306 void add_verify_host(char *host) { | |
1307 verify_map::iterator i = verifiers.find(host); | |
1308 if (i == verifiers.end()) { | |
1309 VERIFYP v = new VERIFY(host); | |
1310 verifiers[host] = v; | |
1311 } | |
1312 } | |
1313 | |
1314 | |
1315 //////////////////////////////////////////////// | |
1316 // thread to check for verify hosts with old sockets that we can close | |
1317 // | |
1318 void* verify_closer(void *arg) { | |
1319 while (true) { | |
1320 sleep(maxage); | |
1321 for (verify_map::iterator i=verifiers.begin(); i!=verifiers.end(); i++) { | |
1322 VERIFYP v = (*i).second; | |
1323 v->closer(); | |
1324 } | |
1325 } | |
1326 return NULL; | |
1327 } | |
1328 | |
1329 | |
1330 //////////////////////////////////////////////// | |
1331 // init the tokens | |
1332 // | |
1333 void token_init() { | |
1334 token_black = register_string("black"); | |
117 | 1335 token_cctld = register_string("cctld"); |
94 | 1336 token_content = register_string("content"); |
1337 token_context = register_string("context"); | |
1338 token_dccfrom = register_string("dcc_from"); | |
1339 token_dccto = register_string("dcc_to"); | |
1340 token_default = register_string("default"); | |
1341 token_dnsbl = register_string("dnsbl"); | |
1342 token_dnsbll = register_string("dnsbl_list"); | |
1343 token_envfrom = register_string("env_from"); | |
1344 token_envto = register_string("env_to"); | |
1345 token_filter = register_string("filter"); | |
1346 token_host_limit = register_string("host_limit"); | |
1347 token_html_limit = register_string("html_limit"); | |
1348 token_html_tags = register_string("html_tags"); | |
1349 token_ignore = register_string("ignore"); | |
1350 token_include = register_string("include"); | |
1351 token_inherit = register_string("inherit"); | |
1352 token_lbrace = register_string("{"); | |
1353 token_mailhost = register_string("mail_host"); | |
1354 token_many = register_string("many"); | |
1355 token_off = register_string("off"); | |
1356 token_ok = register_string("ok"); | |
1357 token_ok2 = register_string("ok2"); | |
1358 token_on = register_string("on"); | |
136 | 1359 token_rate = register_string("rate_limit"); |
94 | 1360 token_rbrace = register_string("}"); |
1361 token_semi = register_string(";"); | |
1362 token_soft = register_string("soft"); | |
1363 token_substitute = register_string("substitute"); | |
1364 token_tld = register_string("tld"); | |
1365 token_unknown = register_string("unknown"); | |
119 | 1366 token_uribl = register_string("uribl"); |
94 | 1367 token_verify = register_string("verify"); |
1368 token_white = register_string("white"); | |
1369 | |
1370 if (gethostname(myhostname, HOST_NAME_MAX+1) != 0) { | |
1371 strncpy(myhostname, "localhost", HOST_NAME_MAX+1); | |
1372 } | |
1373 myhostname[HOST_NAME_MAX] = '\0'; // ensure null termination | |
1374 token_myhostname = register_string(myhostname); | |
1375 } |