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