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