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