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