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