Mercurial > dnsbl
annotate src/context.cpp @ 100:63e8633abc34 stable-5-8
allow empty env_to at global context to remove all restrictions on child contexts
author | carl |
---|---|
date | Sun, 25 Sep 2005 08:38:29 -0700 |
parents | f8963ddf7143 |
children | 6ac7ba4a8312 |
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@ | |
100
63e8633abc34
allow empty env_to at global context to remove all restrictions on child contexts
carl
parents:
99
diff
changeset
|
522 if (!parent && env_to.empty()) return true; // empty env_to at global level covers everything |
94 | 523 string_set::iterator i = env_to.find(x); |
524 if (i != env_to.end()) return true; | |
525 return false; | |
526 } | |
527 | |
528 | |
529 VERIFYP CONTEXT::find_verify(char *to) { | |
530 if (verify_host && (verify_host != token_myhostname) && cover_env_to(to)) { | |
531 verify_map::iterator i = verifiers.find(verify_host); | |
532 if (i == verifiers.end()) { | |
533 if (debug_syslog) { | |
534 char buf[maxlen]; | |
535 snprintf(buf, maxlen, "cannot find struc for %s", verify_host); | |
536 my_syslog(buf); | |
537 } | |
538 return NULL; | |
539 } | |
540 VERIFYP v = (*i).second; | |
541 | |
542 return v; | |
543 } | |
544 else if (parent) return parent->find_verify(to); | |
545 else return NULL; | |
546 } | |
547 | |
548 | |
549 char *CONTEXT::find_from(char *from) { | |
550 char *rc = token_inherit; | |
551 string_map::iterator i = env_from.find(from); | |
552 if (i != env_from.end()) rc = (*i).second; // found user@domain.tld key | |
553 else { | |
554 char *x = strchr(from, '@'); | |
555 if (x) { | |
556 x++; | |
557 i = env_from.find(x); | |
558 if (i != env_from.end()) rc = (*i).second; // found domain.tld key | |
559 else { | |
560 char y = *x; | |
561 *x = '\0'; | |
562 i = env_from.find(from); | |
563 *x = y; | |
564 if (i != env_from.end()) rc = (*i).second; // found user@ key | |
565 } | |
566 } | |
567 } | |
568 if (rc == token_inherit) rc = env_from_default; | |
569 if ((rc == token_inherit) && parent) return parent->find_from(from); | |
570 return (rc == token_inherit) ? token_unknown : rc; | |
571 } | |
572 | |
573 | |
574 CONTEXTP CONTEXT::find_context(char *from) { | |
575 context_map::iterator i = env_from_context.find(from); | |
576 if (i != env_from_context.end()) return (*i).second; // found user@domain.tld key | |
577 char *x = strchr(from, '@'); | |
578 if (x) { | |
579 x++; | |
580 i = env_from_context.find(x); | |
581 if (i != env_from_context.end()) return (*i).second; // found domain.tld key | |
582 char y = *x; | |
583 *x = '\0'; | |
584 i = env_from_context.find(from); | |
585 *x = y; | |
586 if (i != env_from_context.end()) return (*i).second; // found user@ key | |
587 } | |
588 return this; | |
589 } | |
590 | |
591 | |
592 CONTEXTP CONTEXT::find_from_context_name(char *name) { | |
593 context_map::iterator i = children.find(name); | |
594 if (i != children.end()) return (*i).second; | |
595 return NULL; | |
596 } | |
597 | |
598 | |
599 DNSBLP CONTEXT::find_dnsbl(char *name) { | |
600 dnsblp_map::iterator i = dnsbl_names.find(name); | |
601 if (i != dnsbl_names.end()) return (*i).second; | |
602 if (parent) return parent->find_dnsbl(name); | |
603 return NULL; | |
604 } | |
605 | |
606 | |
607 char* CONTEXT::get_content_suffix() { | |
608 if (!content_suffix && parent) return parent->get_content_suffix(); | |
609 return content_suffix; | |
610 } | |
611 | |
612 | |
613 char* CONTEXT::get_content_message() { | |
614 if (!content_message && parent) return parent->get_content_message(); | |
615 return content_message; | |
616 } | |
617 | |
618 | |
619 string_set& CONTEXT::get_content_host_ignore() { | |
620 if (content_host_ignore.empty() && parent) return parent->get_content_host_ignore(); | |
621 return content_host_ignore; | |
622 } | |
623 | |
624 | |
625 string_set& CONTEXT::get_content_tlds() { | |
626 if (content_tlds.empty() && parent) return parent->get_content_tlds(); | |
627 return content_tlds; | |
628 } | |
629 | |
630 | |
631 string_set& CONTEXT::get_html_tags() { | |
632 if (html_tags.empty() && parent) return parent->get_html_tags(); | |
633 return html_tags; | |
634 } | |
635 | |
636 | |
637 dnsblp_list& CONTEXT::get_dnsbl_list() { | |
638 if (dnsbl_list.empty() && parent) return parent->get_dnsbl_list(); | |
639 return dnsbl_list; | |
640 } | |
641 | |
642 | |
643 bool CONTEXT::acceptable_content(recorder &memory, char *&msg) { | |
644 if (memory.excessive_bad_tags(tag_limit)) { | |
645 msg = tag_limit_message; | |
646 return false; | |
647 } | |
648 if (!host_random && memory.excessive_hosts(host_limit)) { | |
649 msg = host_limit_message; | |
650 return false; | |
651 } | |
652 return true; | |
653 } | |
654 | |
655 | |
656 void CONTEXT::dump(int level) { | |
657 char indent[maxlen]; | |
658 int i = min(maxlen-1, level*4); | |
659 memset(indent, ' ', i); | |
660 indent[i] = '\0'; | |
661 char buf[maxlen]; | |
662 char *fullname = get_full_name(buf,maxlen); | |
663 printf("%s context %s { \t// %s\n", indent, name, fullname); | |
664 | |
665 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) { | |
666 char *n = (*i).first; | |
667 DNSBL &d = *(*i).second; | |
668 printf("%s dnsbl %s %s \"%s\"; \n", indent, n, d.suffix, d.message); | |
669 } | |
670 | |
671 if (!dnsbl_list.empty()) { | |
672 printf("%s dnsbl_list", indent); | |
673 for (dnsblp_list::iterator i=dnsbl_list.begin(); i!=dnsbl_list.end(); i++) { | |
674 DNSBL &d = *(*i); | |
675 printf(" %s", d.name); | |
676 } | |
677 printf("; \n"); | |
678 } | |
679 | |
680 if (content_filtering) { | |
681 printf("%s content on { \n", indent, env_from_default); | |
682 if (content_suffix) { | |
683 printf("%s filter %s \"%s\"; \n", indent, content_suffix, content_message); | |
684 } | |
685 if (!content_host_ignore.empty()) { | |
686 printf("%s ignore { \n", indent); | |
687 for (string_set::iterator i=content_host_ignore.begin(); i!=content_host_ignore.end(); i++) { | |
688 printf("%s %s; \n", indent, *i); | |
689 } | |
690 printf("%s }; \n", indent); | |
691 } | |
692 if (!content_tlds.empty()) { | |
693 printf("%s tld { \n", indent); | |
694 printf("%s ", indent); | |
695 for (string_set::iterator i=content_tlds.begin(); i!=content_tlds.end(); i++) { | |
696 printf("%s; ", *i); | |
697 } | |
698 printf("\n%s }; \n", indent); | |
699 } | |
700 if (!html_tags.empty()) { | |
701 printf("%s html_tags { \n", indent); | |
702 printf("%s ", indent); | |
703 for (string_set::iterator i=html_tags.begin(); i!=html_tags.end(); i++) { | |
704 printf("%s; ", *i); | |
705 } | |
706 printf("\n%s }; \n", indent); | |
707 } | |
708 if (host_limit_message) { | |
709 printf("%s host_limit on %d \"%s\"; \n", indent, host_limit, host_limit_message); | |
710 } | |
711 else if (host_random) { | |
712 printf("%s host_limit soft %d; \n", indent, host_limit); | |
713 } | |
714 else { | |
715 printf("%s host_limit off; \n", indent); | |
716 } | |
717 if (tag_limit_message) { | |
718 printf("%s html_limit on %d \"%s\"; \n", indent, tag_limit, tag_limit_message); | |
719 } | |
720 else { | |
721 printf("%s html_limit off; \n", indent); | |
722 } | |
723 printf("%s }; \n", indent); | |
724 } | |
725 else { | |
726 printf("%s content off {}; \n", indent, env_from_default); | |
727 } | |
728 | |
729 printf("%s env_to { \t// %s\n", indent, fullname); | |
730 for (string_set::iterator i=env_to.begin(); i!=env_to.end(); i++) { | |
731 printf("%s %s; \n", indent, *i); | |
732 } | |
733 printf("%s }; \n", indent); | |
734 | |
735 if (verify_host) { | |
736 printf("%s verify %s; \n", indent, verify_host); | |
737 } | |
738 | |
739 for (context_map::iterator i=children.begin(); i!=children.end(); i++) { | |
740 CONTEXTP c = (*i).second; | |
741 c->dump(level+1); | |
742 } | |
743 | |
744 printf("%s env_from %s { \t// %s\n", indent, env_from_default, fullname); | |
745 if (!env_from.empty()) { | |
746 printf("%s // white/black/unknown \n", indent); | |
747 for (string_map::iterator i=env_from.begin(); i!=env_from.end(); i++) { | |
748 char *f = (*i).first; | |
749 char *t = (*i).second; | |
750 printf("%s %s \t%s; \n", indent, f, t); | |
751 } | |
752 } | |
753 if (!env_from_context.empty()) { | |
754 printf("%s // child contexts \n", indent); | |
755 for (context_map::iterator j=env_from_context.begin(); j!=env_from_context.end(); j++) { | |
756 char *f = (*j).first; | |
757 CONTEXTP t = (*j).second; | |
758 printf("%s %s \t%s; \n", indent, f, t->name); | |
759 } | |
760 } | |
761 printf("%s }; \n", indent); | |
762 | |
763 printf("%s }; \n", indent); | |
764 } | |
765 | |
766 | |
767 //////////////////////////////////////////////// | |
768 // helper to discard the strings held by a string_set | |
769 // | |
770 void discard(string_set &s) { | |
771 for (string_set::iterator i=s.begin(); i!=s.end(); i++) { | |
772 free(*i); | |
773 } | |
774 s.clear(); | |
775 } | |
776 | |
777 | |
778 //////////////////////////////////////////////// | |
779 // helper to register a string in a string set | |
780 // | |
781 char* register_string(string_set &s, char *name) { | |
782 string_set::iterator i = s.find(name); | |
783 if (i != s.end()) return *i; | |
784 char *x = strdup(name); | |
785 s.insert(x); | |
786 return x; | |
787 } | |
788 | |
789 | |
790 //////////////////////////////////////////////// | |
791 // register a global string | |
792 // | |
793 char* register_string(char *name) { | |
794 return register_string(all_strings, name); | |
795 } | |
796 | |
797 | |
798 //////////////////////////////////////////////// | |
799 // | |
800 bool tsa(TOKEN &tok, char *token); | |
801 bool tsa(TOKEN &tok, char *token) { | |
802 char *have = tok.next(); | |
803 if (have == token) return true; | |
804 tok.token_error(token, have); | |
805 return false; | |
806 } | |
807 | |
808 | |
809 //////////////////////////////////////////////// | |
810 // | |
811 bool parse_dnsbl(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
812 bool parse_dnsbl(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
813 char *name = tok.next(); | |
814 char *suf = tok.next(); | |
815 char *msg = tok.next(); | |
816 if (!tsa(tok, token_semi)) return false; | |
817 DNSBLP dnsnew = new DNSBL(name, suf, msg); | |
818 DNSBLP dnsold = me.find_dnsbl(name); | |
819 if (dnsold && (*dnsold == *dnsnew)) { | |
820 // duplicate redefinition, ignore it | |
821 delete dnsnew; | |
822 return true; | |
823 } | |
824 me.add_dnsbl(name, dnsnew); | |
825 return true; | |
826 } | |
827 | |
828 | |
829 //////////////////////////////////////////////// | |
830 // | |
831 bool parse_dnsbll(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
832 bool parse_dnsbll(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
833 while (true) { | |
834 char *have = tok.next(); | |
835 if (!have) break; | |
836 if (have == token_semi) break; | |
837 DNSBLP dns = me.find_dnsbl(have); | |
838 if (dns) { | |
839 me.add_dnsbl(dns); | |
840 } | |
841 else { | |
842 tok.token_error("dnsbl name", have); | |
843 return false; | |
844 } | |
845 } | |
846 return true; | |
847 } | |
848 | |
849 | |
850 //////////////////////////////////////////////// | |
851 // | |
852 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
853 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
854 char *setting = tok.next(); | |
855 if (setting == token_on) { | |
856 me.set_content_filtering(true); | |
857 } | |
858 else if (setting == token_off) { | |
859 me.set_content_filtering(false); | |
860 } | |
861 else { | |
862 tok.token_error("on/off", setting); | |
863 return false; | |
864 } | |
865 if (!tsa(tok, token_lbrace)) return false; | |
866 while (true) { | |
867 char *have = tok.next(); | |
868 if (!have) break; | |
869 if (have == token_filter) { | |
870 char *suffix = tok.next(); | |
871 char *messag = tok.next(); | |
872 me.set_content_suffix(suffix); | |
873 me.set_content_message(messag); | |
874 if (!tsa(tok, token_semi)) return false; | |
875 } | |
876 else if (have == token_ignore) { | |
877 if (!tsa(tok, token_lbrace)) return false; | |
878 while (true) { | |
879 if (!have) break; | |
880 char *have = tok.next(); | |
881 if (have == token_rbrace) break; // done | |
882 me.add_ignore(have); | |
883 } | |
884 if (!tsa(tok, token_semi)) return false; | |
885 } | |
886 else if (have == token_tld) { | |
887 if (!tsa(tok, token_lbrace)) return false; | |
888 while (true) { | |
889 char *have = tok.next(); | |
890 if (!have) break; | |
891 if (have == token_rbrace) break; // done | |
892 me.add_tld(have); | |
893 } | |
894 if (!tsa(tok, token_semi)) return false; | |
895 } | |
896 else if (have == token_html_limit) { | |
897 have = tok.next(); | |
898 if (have == token_on) { | |
899 me.set_tag_limit(tok.nextint()); | |
900 me.set_tag_message(tok.next()); | |
901 } | |
902 else if (have == token_off) { | |
903 me.set_tag_limit(0); | |
904 me.set_tag_message(NULL); | |
905 } | |
906 else { | |
907 tok.token_error("on/off", have); | |
908 return false; | |
909 } | |
910 if (!tsa(tok, token_semi)) return false; | |
911 } | |
912 else if (have == token_html_tags) { | |
913 if (!tsa(tok, token_lbrace)) return false; | |
914 while (true) { | |
915 char *have = tok.next(); | |
916 if (!have) break; | |
917 if (have == token_rbrace) { | |
918 break; // done | |
919 } | |
920 else { | |
921 me.add_tag(have); // base version | |
922 char buf[200]; | |
923 snprintf(buf, sizeof(buf), "/%s", have); | |
924 me.add_tag(register_string(buf)); // leading / | |
925 snprintf(buf, sizeof(buf), "%s/", have); | |
926 me.add_tag(register_string(buf)); // trailing / | |
927 } | |
928 } | |
929 if (!tsa(tok, token_semi)) return false; | |
930 } | |
931 else if (have == token_host_limit) { | |
932 have = tok.next(); | |
933 if (have == token_on) { | |
934 me.set_host_limit(tok.nextint()); | |
935 me.set_host_message(tok.next()); | |
936 me.set_host_random(false); | |
937 } | |
938 else if (have == token_off) { | |
939 me.set_host_limit(0); | |
940 me.set_host_message(NULL); | |
941 me.set_host_random(false); | |
942 } | |
943 else if (have == token_soft) { | |
944 me.set_host_limit(tok.nextint()); | |
945 me.set_host_message(NULL); | |
946 me.set_host_random(true); | |
947 } | |
948 else { | |
949 tok.token_error("on/off/soft", have); | |
950 return false; | |
951 } | |
952 if (!tsa(tok, token_semi)) return false; | |
953 } | |
954 else if (have == token_rbrace) { | |
955 break; // done | |
956 } | |
957 else { | |
958 tok.token_error("content keyword", have); | |
959 return false; | |
960 } | |
961 } | |
962 return tsa(tok, token_semi); | |
963 } | |
964 | |
965 | |
966 //////////////////////////////////////////////// | |
967 // | |
968 bool parse_envto(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
969 bool parse_envto(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
970 if (!tsa(tok, token_lbrace)) return false; | |
971 while (true) { | |
972 char *have = tok.next(); | |
973 if (!have) break; | |
974 if (have == token_rbrace) break; | |
975 if (have == token_semi) { | |
976 // optional separators | |
977 } | |
978 else if (have == token_dccto) { | |
979 char *flavor = tok.next(); | |
980 if (!tsa(tok, token_lbrace)) return false; | |
981 bool keeping = false; | |
982 while (true) { | |
983 char *have = tok.next(); | |
984 if (!have) break; | |
985 if (have == token_rbrace) break; | |
986 if (have == flavor) { | |
987 keeping = true; | |
988 continue; | |
989 } | |
990 else if ((have == token_ok) || (have == token_ok2) || (have == token_many)) { | |
991 keeping = false; | |
992 continue; | |
993 } | |
994 if (have == token_envto) { | |
995 have = tok.next(); | |
996 if (keeping) { | |
997 if (me.allow_env_to(have)) { | |
998 me.add_to(have); | |
999 dc.add_to(have, &me); | |
1000 } | |
1001 } | |
1002 } | |
1003 //else if (have == token_substitute) { | |
1004 // if (tok.next() == token_mailhost) { | |
1005 // have = tok.next(); | |
1006 // if (keeping) { | |
1007 // if (me.allow_env_to(have)) { | |
1008 // me.add_to(have); | |
1009 // dc.add_to(have, &me); | |
1010 // } | |
1011 // } | |
1012 // } | |
1013 //} | |
1014 tok.skipeol(); | |
1015 } | |
1016 } | |
1017 else if (me.allow_env_to(have)) { | |
1018 me.add_to(have); | |
1019 dc.add_to(have, &me); | |
1020 } | |
1021 else { | |
1022 tok.token_error("user@ or user@domain.tld or domain.tld where domain.tld allowed by parent context", have); | |
1023 return false; | |
1024 } | |
1025 } | |
1026 return tsa(tok, token_semi); | |
1027 } | |
1028 | |
1029 | |
1030 //////////////////////////////////////////////// | |
1031 // | |
1032 bool parse_verify(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1033 bool parse_verify(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1034 char *host = tok.next(); | |
1035 if (!tsa(tok, token_semi)) return false; | |
1036 me.set_verify(host); | |
1037 add_verify_host(host); | |
99 | 1038 return true; |
94 | 1039 } |
1040 | |
1041 | |
1042 //////////////////////////////////////////////// | |
1043 // | |
1044 bool parse_envfrom(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1045 bool parse_envfrom(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1046 char *st = tok.next(); | |
1047 if ((st == token_black) || (st == token_white) || (st == token_unknown) || (st == token_inherit)) { | |
1048 me.set_from_default(st); | |
1049 } | |
1050 else { | |
1051 tok.push(st); | |
1052 } | |
1053 if (!tsa(tok, token_lbrace)) return false; | |
1054 while (true) { | |
1055 char *have = tok.next(); | |
1056 if (!have) break; | |
1057 if (have == token_rbrace) break; | |
1058 if (have == token_semi) { | |
1059 // optional separators | |
1060 } | |
1061 else if (have == token_dccfrom) { | |
1062 if (!tsa(tok, token_lbrace)) return false; | |
1063 bool keeping = false; | |
1064 bool many = false; | |
1065 while (true) { | |
1066 char *have = tok.next(); | |
1067 if (!have) break; | |
1068 if (have == token_rbrace) break; | |
1069 if (have == token_ok) { | |
1070 keeping = true; | |
1071 many = false; | |
1072 continue; | |
1073 } | |
1074 else if (have == token_many) { | |
1075 keeping = true; | |
1076 many = true; | |
1077 continue; | |
1078 } | |
1079 else if (have == token_ok2) { | |
1080 keeping = false; | |
1081 continue; | |
1082 } | |
1083 if (have == token_envfrom) { | |
1084 have = tok.next(); | |
1085 if (keeping) { | |
1086 me.add_from(have, (many) ? token_black : token_white); | |
1087 } | |
1088 } | |
1089 else if (have == token_substitute) { | |
1090 if (tok.next() == token_mailhost) { | |
1091 have = tok.next(); | |
1092 me.add_from(have, (many) ? token_black : token_white); | |
1093 } | |
1094 } | |
1095 tok.skipeol(); | |
1096 } | |
1097 } | |
1098 else { | |
1099 // may be a valid email address or domain name | |
1100 char *st = tok.next(); | |
1101 if ((st == token_black) || (st == token_white) || (st == token_unknown)) { | |
1102 me.add_from(have, st); | |
1103 } | |
1104 else { | |
1105 CONTEXTP con = me.find_from_context_name(st); | |
1106 if (con) { | |
1107 me.add_from_context(have, con); | |
1108 } | |
1109 else { | |
1110 tok.token_error("white/black/unknown or child context name", st); | |
1111 return false; | |
1112 } | |
1113 } | |
1114 } | |
1115 } | |
1116 return tsa(tok, token_semi); | |
1117 } | |
1118 | |
1119 | |
1120 //////////////////////////////////////////////// | |
1121 // | |
1122 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent); | |
1123 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent) { | |
1124 char *name = tok.next(); | |
1125 if (!tsa(tok, token_lbrace)) return false; | |
1126 CONTEXTP con = new CONTEXT(parent, name); | |
1127 | |
1128 while (true) { | |
1129 char *have = tok.next(); | |
1130 if (!have) break; | |
1131 if (have == token_rbrace) break; // done | |
1132 if (have == token_dnsbl) { | |
1133 if (!parse_dnsbl(tok, dc, *con)) return false; | |
1134 } | |
1135 else if (have == token_dnsbll) { | |
1136 if (!parse_dnsbll(tok, dc, *con)) return false; | |
1137 } | |
1138 else if (have == token_content) { | |
1139 if (!parse_content(tok, dc, *con)) return false; | |
1140 } | |
1141 else if (have == token_envto) { | |
1142 if (!parse_envto(tok, dc, *con)) return false; | |
1143 } | |
1144 else if (have == token_verify) { | |
1145 if (!parse_verify(tok, dc, *con)) return false; | |
1146 } | |
1147 else if (have == token_envfrom) { | |
1148 if (!parse_envfrom(tok, dc, *con)) return false; | |
1149 } | |
1150 else if (have == token_context) { | |
1151 if (!parse_context(tok, dc, con)) return false; | |
1152 } | |
1153 else { | |
1154 tok.token_error("context keyword", have); | |
1155 return false; | |
1156 } | |
1157 } | |
1158 | |
1159 if (!tsa(tok, token_semi)) { | |
1160 delete con; | |
1161 return false; | |
1162 } | |
1163 dc.add_context(con); | |
1164 if (parent) parent->add_context(con); | |
1165 return true; | |
1166 } | |
1167 | |
1168 | |
1169 //////////////////////////////////////////////// | |
1170 // parse a config file | |
1171 // | |
1172 bool load_conf(CONFIG &dc, char *fn) { | |
99 | 1173 int count = 0; |
94 | 1174 TOKEN tok(fn, &dc.config_files); |
1175 while (true) { | |
1176 char *have = tok.next(); | |
1177 if (!have) break; | |
1178 if (have == token_context) { | |
1179 if (!parse_context(tok, dc, NULL)) { | |
99 | 1180 tok.token_error("load_conf() failed to parse context"); |
94 | 1181 return false; |
1182 } | |
99 | 1183 else count++; |
94 | 1184 } |
1185 else { | |
1186 tok.token_error(token_context, have); | |
1187 return false; | |
1188 } | |
1189 } | |
99 | 1190 tok.token_error("load_conf() found %d contexts in %s", count, fn); |
94 | 1191 return (dc.default_context) ? true : false; |
1192 } | |
1193 | |
1194 | |
1195 //////////////////////////////////////////////// | |
1196 // setup a new smtp verify host | |
1197 // | |
1198 void add_verify_host(char *host) { | |
1199 verify_map::iterator i = verifiers.find(host); | |
1200 if (i == verifiers.end()) { | |
1201 VERIFYP v = new VERIFY(host); | |
1202 verifiers[host] = v; | |
1203 } | |
1204 } | |
1205 | |
1206 | |
1207 //////////////////////////////////////////////// | |
1208 // thread to check for verify hosts with old sockets that we can close | |
1209 // | |
1210 void* verify_closer(void *arg) { | |
1211 while (true) { | |
1212 sleep(maxage); | |
1213 for (verify_map::iterator i=verifiers.begin(); i!=verifiers.end(); i++) { | |
1214 VERIFYP v = (*i).second; | |
1215 v->closer(); | |
1216 } | |
1217 } | |
1218 return NULL; | |
1219 } | |
1220 | |
1221 | |
1222 //////////////////////////////////////////////// | |
1223 // init the tokens | |
1224 // | |
1225 void token_init() { | |
1226 token_black = register_string("black"); | |
1227 token_content = register_string("content"); | |
1228 token_context = register_string("context"); | |
1229 token_dccfrom = register_string("dcc_from"); | |
1230 token_dccto = register_string("dcc_to"); | |
1231 token_default = register_string("default"); | |
1232 token_dnsbl = register_string("dnsbl"); | |
1233 token_dnsbll = register_string("dnsbl_list"); | |
1234 token_envfrom = register_string("env_from"); | |
1235 token_envto = register_string("env_to"); | |
1236 token_filter = register_string("filter"); | |
1237 token_host_limit = register_string("host_limit"); | |
1238 token_html_limit = register_string("html_limit"); | |
1239 token_html_tags = register_string("html_tags"); | |
1240 token_ignore = register_string("ignore"); | |
1241 token_include = register_string("include"); | |
1242 token_inherit = register_string("inherit"); | |
1243 token_lbrace = register_string("{"); | |
1244 token_mailhost = register_string("mail_host"); | |
1245 token_many = register_string("many"); | |
1246 token_off = register_string("off"); | |
1247 token_ok = register_string("ok"); | |
1248 token_ok2 = register_string("ok2"); | |
1249 token_on = register_string("on"); | |
1250 token_rbrace = register_string("}"); | |
1251 token_semi = register_string(";"); | |
1252 token_soft = register_string("soft"); | |
1253 token_substitute = register_string("substitute"); | |
1254 token_tld = register_string("tld"); | |
1255 token_unknown = register_string("unknown"); | |
1256 token_verify = register_string("verify"); | |
1257 token_white = register_string("white"); | |
1258 | |
1259 if (gethostname(myhostname, HOST_NAME_MAX+1) != 0) { | |
1260 strncpy(myhostname, "localhost", HOST_NAME_MAX+1); | |
1261 } | |
1262 myhostname[HOST_NAME_MAX] = '\0'; // ensure null termination | |
1263 token_myhostname = register_string(myhostname); | |
1264 } |