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