Mercurial > dnsbl
annotate src/context.cpp @ 162:c4bce911c276 stable-6-03
don't add auto whitelist for A to A
author | carl |
---|---|
date | Sat, 14 Jul 2007 12:25:17 -0700 |
parents | b3ed72ee6564 |
children | 97d7da45fe2a |
rev | line source |
---|---|
94 | 1 /* |
2 | |
152 | 3 Copyright (c) 2007 Carl Byington - 510 Software Group, released under |
4 the GPL version 3 or any later version at your choice available at | |
5 http://www.gnu.org/licenses/gpl-3.0.txt | |
94 | 6 |
7 */ | |
8 | |
9 #include "includes.h" | |
10 | |
160 | 11 #include <arpa/inet.h> |
94 | 12 #include <net/if.h> |
160 | 13 #include <netdb.h> |
94 | 14 #include <netinet/in.h> |
15 #include <netinet/tcp.h> | |
160 | 16 #include <sys/ioctl.h> |
94 | 17 #include <sys/socket.h> |
160 | 18 #include <sys/stat.h> |
94 | 19 #include <sys/un.h> |
160 | 20 #include <unistd.h> |
94 | 21 |
22 static char* context_version="$Id$"; | |
23 | |
153 | 24 char *token_autowhite; |
94 | 25 char *token_black; |
26 char *token_content; | |
27 char *token_context; | |
28 char *token_dccfrom; | |
29 char *token_dccto; | |
30 char *token_default; | |
31 char *token_dnsbl; | |
32 char *token_dnsbll; | |
33 char *token_envfrom; | |
34 char *token_envto; | |
35 char *token_filter; | |
36 char *token_host_limit; | |
37 char *token_html_limit; | |
38 char *token_html_tags; | |
39 char *token_ignore; | |
40 char *token_include; | |
41 char *token_inherit; | |
42 char *token_lbrace; | |
43 char *token_mailhost; | |
44 char *token_many; | |
45 char *token_off; | |
46 char *token_ok2; | |
47 char *token_ok; | |
48 char *token_on; | |
136 | 49 char *token_rate; |
94 | 50 char *token_rbrace; |
51 char *token_semi; | |
52 char *token_soft; | |
53 char *token_substitute; | |
54 char *token_tld; | |
117 | 55 char *token_cctld; |
94 | 56 char *token_unknown; |
119 | 57 char *token_uribl; |
94 | 58 char *token_verify; |
59 char *token_white; | |
60 | |
61 char *token_myhostname; | |
96
1edd4e8d3a60
fix missing include, not all systems define HOST_NAME_MAX
carl
parents:
94
diff
changeset
|
62 #ifndef HOST_NAME_MAX |
1edd4e8d3a60
fix missing include, not all systems define HOST_NAME_MAX
carl
parents:
94
diff
changeset
|
63 #define HOST_NAME_MAX 255 |
1edd4e8d3a60
fix missing include, not all systems define HOST_NAME_MAX
carl
parents:
94
diff
changeset
|
64 #endif |
94 | 65 char myhostname[HOST_NAME_MAX+1]; |
66 | |
153 | 67 pthread_mutex_t verifier_mutex; // protect the verifier map |
94 | 68 verify_map verifiers; |
153 | 69 |
70 pthread_mutex_t whitelister_mutex; // protect the | |
71 whitelister_map whitelisters; | |
72 | |
94 | 73 string_set all_strings; // owns all the strings, only modified by the config loader thread |
74 const int maxlen = 1000; // used for snprintf buffers | |
153 | 75 const int maxsmtp_age = 120;// smtp verify sockets older than this are ancient |
76 const int maxauto_age = 600;// auto whitelister delay before flushing to file | |
94 | 77 extern int NULL_SOCKET; |
129
c5cd1261394d
ignore smtp connection attempts for 10 minutes when getting connection errors on verify hosts
carl
parents:
119
diff
changeset
|
78 const time_t ERROR_SMTP_SOCKET_TIME = 600; // number of seconds between attempts to open a socket to an smtp server |
94 | 79 |
80 | |
81 int SMTP::writer() { | |
82 #ifdef VERIFY_DEBUG | |
83 log("writer() sees buffer with %s", buffer); | |
84 log("writer() sees error %d", (int)error); | |
85 #endif | |
86 int rs = 0; | |
87 if (!error) { | |
88 int len = strlen(buffer); | |
89 while (rs < len) { | |
90 int ws = write(fd, buffer+rs, len-rs); | |
91 if (ws > 0) { | |
92 rs += ws; | |
93 } | |
94 else { | |
95 // peer closed the socket! | |
96 rs = 0; | |
97 error = true; | |
98 break; | |
99 } | |
100 } | |
101 } | |
102 return rs; | |
103 } | |
104 | |
105 | |
106 int SMTP::reader() { | |
107 // read some bytes terminated by lf or end of buffer. | |
108 // we may have a multi line response or part thereof in the buffer. | |
109 #ifdef VERIFY_DEBUG | |
110 log("reader() sees error %d", (int)error); | |
111 #endif | |
112 if (error) return 0; | |
113 int len = maxlen-1; // room for null terminator | |
114 while (pending < len) { | |
115 int ws = read(fd, buffer+pending, len-pending); | |
116 if (ws > 0) { | |
117 pending += ws; | |
118 if (buffer[pending-1] == '\n') break; | |
119 } | |
120 else { | |
121 // peer closed the socket! | |
122 pending = 0; | |
123 error = true; | |
124 break; | |
125 } | |
126 } | |
127 buffer[pending] = '\0'; | |
128 #ifdef VERIFY_DEBUG | |
129 log("reader() sees buffer with %s", buffer); | |
130 #endif | |
131 return pending; | |
132 } | |
133 | |
134 | |
135 int SMTP::read_line() { | |
136 char *lf = strchr(buffer, '\n'); | |
137 if (!lf) { | |
138 reader(); // get a lf | |
139 lf = strchr(buffer, '\n'); | |
140 if (!lf) lf = buffer + pending - 1; | |
141 } | |
142 return (lf-buffer)+1; // number of bytes in this line | |
143 } | |
144 | |
145 | |
97 | 146 void SMTP::flush_line(int r) { |
94 | 147 if (pending > r) memmove(buffer, buffer+r, pending-r); |
148 pending -= r; | |
149 } | |
150 | |
151 | |
152 int SMTP::read_response() { | |
153 pending = 0; | |
154 buffer[pending] = '\0'; | |
155 while (true) { | |
156 int r = read_line(); | |
157 #ifdef VERIFY_DEBUG | |
158 log("read_response() sees line with %s", buffer); | |
159 log("read_response() sees line length %d", r); | |
160 #endif | |
161 if (r == 0) return 0; // failed to read any bytes | |
162 if ((r > 4) && (buffer[3] == '-')) { | |
163 flush_line(r); | |
164 continue; | |
165 } | |
166 return atoi(buffer); | |
167 } | |
168 return 0; | |
169 } | |
170 | |
171 | |
172 int SMTP::cmd(char *c) { | |
173 if (c) { | |
174 init(); | |
175 append(c); | |
176 } | |
177 append("\r\n"); | |
178 writer(); | |
179 return read_response(); | |
180 } | |
181 | |
182 | |
183 int SMTP::helo() { | |
184 if (read_response() != 220) return 0; | |
185 init(); | |
186 append("HELO "); | |
187 append(token_myhostname); | |
188 return cmd(NULL); | |
189 } | |
190 | |
191 | |
192 int SMTP::rset() { | |
193 int rc = cmd("RSET"); | |
194 efrom[0] = '\0'; | |
195 return rc; | |
196 } | |
197 | |
198 | |
199 int SMTP::from(char *f) { | |
101 | 200 // the mail from address was originally passed in from sendmail enclosed in |
201 // <>. to_lower_string() removed the <> and converted the rest to lowercase, | |
202 // except in the case of an empty return path, which was left as the two | |
203 // character string <>. | |
94 | 204 if (strncmp(efrom, f, maxlen)) { |
205 rset(); | |
206 strncpy(efrom, f, maxlen); | |
207 init(); | |
208 append("MAIL FROM:<"); | |
101 | 209 if (*f != '<') append(f); |
94 | 210 append(">"); |
211 return cmd(NULL); | |
212 } | |
213 return 250; // pretend it worked | |
214 } | |
215 | |
216 | |
217 int SMTP::rcpt(char *t) { | |
218 init(); | |
219 append("RCPT TO:<"); | |
220 append(t); | |
221 append(">"); | |
222 return cmd(NULL); | |
223 } | |
224 | |
225 | |
226 int SMTP::quit() { | |
227 return cmd("QUIT"); | |
228 } | |
229 | |
230 | |
231 void SMTP::closefd() { | |
232 shutdown(fd, SHUT_RDWR); | |
233 close(fd); | |
234 } | |
235 | |
236 | |
237 #ifdef VERIFY_DEBUG | |
238 void SMTP::log(char *m, int v) { | |
239 char buf[maxlen]; | |
240 snprintf(buf, maxlen, m, v); | |
241 my_syslog(buf); | |
242 } | |
243 | |
244 | |
245 void SMTP::log(char *m, char *v) { | |
246 char buf[maxlen]; | |
247 snprintf(buf, maxlen, m, v); | |
248 my_syslog(buf); | |
249 } | |
250 #endif | |
251 | |
252 | |
153 | 253 //////////////////////////////////////////////// |
254 // smtp verifier so backup mx machines can see the valid users | |
255 // | |
94 | 256 VERIFY::VERIFY(char *h) { |
257 host = h; | |
258 last_err = 0; | |
259 pthread_mutex_init(&mutex, 0); | |
260 } | |
261 | |
262 | |
263 void VERIFY::closer() { | |
264 bool ok = true; | |
265 while (ok) { | |
266 SMTP *conn = NULL; | |
267 pthread_mutex_lock(&mutex); | |
268 if (connections.empty()) { | |
269 ok = false; | |
270 } | |
271 else { | |
272 conn = connections.front(); | |
273 time_t now = time(NULL); | |
153 | 274 if ((now - conn->get_stamp()) > maxsmtp_age) { |
94 | 275 // this connection is ancient, remove it |
276 connections.pop_front(); | |
277 } | |
278 else { | |
279 ok = false; | |
280 conn = NULL; | |
281 } | |
282 } | |
283 pthread_mutex_unlock(&mutex); | |
284 // avoid doing this work inside the mutex lock | |
285 if (conn) { | |
286 #ifdef VERIFY_DEBUG | |
287 conn->log("closer() closes ancient %d", conn->get_fd()); | |
288 #endif | |
289 delete conn; | |
290 } | |
291 } | |
292 } | |
293 | |
294 | |
295 SMTP* VERIFY::get_connection() { | |
296 SMTP *conn = NULL; | |
297 pthread_mutex_lock(&mutex); | |
298 if (!connections.empty()) { | |
299 conn = connections.front(); | |
300 connections.pop_front(); | |
301 #ifdef VERIFY_DEBUG | |
302 conn->log("get_connection() %d from cache", conn->get_fd()); | |
303 #endif | |
304 } | |
305 pthread_mutex_unlock(&mutex); | |
306 if (conn) return conn; | |
307 int sock = NULL_SOCKET; | |
129
c5cd1261394d
ignore smtp connection attempts for 10 minutes when getting connection errors on verify hosts
carl
parents:
119
diff
changeset
|
308 if ((time(NULL) - last_err) > ERROR_SMTP_SOCKET_TIME) { |
94 | 309 // nothing recent, maybe this time it will work |
310 hostent *h = gethostbyname(host); | |
311 if (h) { | |
312 sockaddr_in server; | |
313 server.sin_family = h->h_addrtype; | |
314 server.sin_port = htons(25); | |
315 memcpy(&server.sin_addr, h->h_addr_list[0], h->h_length); | |
316 sock = socket(PF_INET, SOCK_STREAM, 0); | |
317 if (sock != NULL_SOCKET) { | |
318 bool rc = (connect(sock, (sockaddr *)&server, sizeof(server)) == 0); | |
319 if (!rc) { | |
320 shutdown(sock, SHUT_RDWR); | |
321 close(sock); | |
322 sock = NULL_SOCKET; | |
129
c5cd1261394d
ignore smtp connection attempts for 10 minutes when getting connection errors on verify hosts
carl
parents:
119
diff
changeset
|
323 last_err = time(NULL); |
94 | 324 } |
325 } | |
129
c5cd1261394d
ignore smtp connection attempts for 10 minutes when getting connection errors on verify hosts
carl
parents:
119
diff
changeset
|
326 else last_err = time(NULL); |
94 | 327 } |
129
c5cd1261394d
ignore smtp connection attempts for 10 minutes when getting connection errors on verify hosts
carl
parents:
119
diff
changeset
|
328 else last_err = time(NULL); |
94 | 329 } |
330 if (sock != NULL_SOCKET) { | |
331 conn = new SMTP(sock); | |
332 #ifdef VERIFY_DEBUG | |
333 conn->log("get_connection() %d new socket", conn->get_fd()); | |
334 #endif | |
335 if (conn->helo() == 250) return conn; | |
336 delete conn; | |
337 } | |
338 return NULL; | |
339 } | |
340 | |
341 | |
342 void VERIFY::put_connection(SMTP *conn) { | |
343 if (conn->err()) { | |
344 #ifdef VERIFY_DEBUG | |
345 conn->log("put_socket() %d with error, close it", conn->get_fd()); | |
346 #endif | |
347 delete conn; | |
348 last_err = time(NULL); | |
349 } | |
350 else { | |
351 #ifdef VERIFY_DEBUG | |
352 conn->log("put_socket() %d", conn->get_fd()); | |
353 #endif | |
354 conn->now(); | |
355 pthread_mutex_lock(&mutex); | |
356 connections.push_back(conn); | |
357 pthread_mutex_unlock(&mutex); | |
358 } | |
359 } | |
360 | |
361 | |
362 bool VERIFY::ok(char *from, char *to) { | |
363 if (host == token_myhostname) return true; | |
364 SMTP *conn = get_connection(); | |
365 if (!conn) return true; // cannot verify right now, we have socket errors | |
366 int rc; | |
367 rc = conn->from(from); | |
368 #ifdef VERIFY_DEBUG | |
369 conn->log("verify::ok() from sees %d", rc); | |
370 #endif | |
371 if (rc != 250) { | |
372 conn->rset(); | |
373 put_connection(conn); | |
374 return (rc >= 500) ? false : true; | |
375 } | |
376 rc = conn->rcpt(to); | |
377 #ifdef VERIFY_DEBUG | |
378 conn->log("verify::ok() rcpt sees %d", rc); | |
379 #endif | |
380 put_connection(conn); | |
381 return (rc >= 500) ? false : true; | |
382 } | |
383 | |
384 | |
153 | 385 //////////////////////////////////////////////// |
386 // setup a new smtp verify host | |
387 // | |
388 VERIFYP add_verify_host(char *host); | |
389 VERIFYP add_verify_host(char *host) { | |
390 VERIFYP rc = NULL; | |
391 pthread_mutex_lock(&verifier_mutex); | |
392 verify_map::iterator i = verifiers.find(host); | |
393 if (i == verifiers.end()) { | |
394 rc = new VERIFY(host); | |
395 verifiers[host] = rc; | |
396 } | |
397 else rc = (*i).second; | |
398 pthread_mutex_unlock(&verifier_mutex); | |
399 return rc; | |
400 } | |
401 | |
402 | |
403 //////////////////////////////////////////////// | |
404 // thread to check for verify hosts with old sockets that we can close | |
405 // | |
406 void* verify_closer(void *arg) { | |
407 while (true) { | |
408 sleep(maxsmtp_age); | |
409 pthread_mutex_lock(&verifier_mutex); | |
410 for (verify_map::iterator i=verifiers.begin(); i!=verifiers.end(); i++) { | |
411 VERIFYP v = (*i).second; | |
412 v->closer(); | |
413 } | |
414 pthread_mutex_unlock(&verifier_mutex); | |
415 } | |
416 return NULL; | |
417 } | |
418 | |
419 | |
420 //////////////////////////////////////////////// | |
421 // automatic whitelister | |
422 // | |
423 WHITELISTER::WHITELISTER(char *f, int d) { | |
424 fn = f; | |
425 days = d; | |
426 pthread_mutex_init(&mutex, 0); | |
427 need = false; | |
160 | 428 loaded = time(NULL); |
429 merge(); | |
430 } | |
431 | |
432 | |
433 void WHITELISTER::merge() { | |
434 time_t now = time(NULL); | |
154 | 435 ifstream ifs; |
436 ifs.open(fn); | |
437 if (!ifs.fail()) { | |
438 const int maxlen = 1000; | |
439 char buf[maxlen]; | |
440 while (ifs.getline(buf, maxlen)) { | |
441 char *p = strchr(buf, ' '); | |
442 if (p) { | |
443 *p = '\0'; | |
160 | 444 char *who = strdup(buf); |
445 time_t when = atoi(p+1); | |
446 if ((when == 0) || (when > now)) when = now; | |
447 autowhite_sent::iterator i = rcpts.find(who); | |
448 if (i != rcpts.end()) { | |
449 time_t wh = (*i).second; | |
450 if (when > wh) rcpts[who] = when; | |
451 } | |
452 else { | |
453 rcpts[who] = when; | |
454 } | |
154 | 455 } |
456 } | |
457 } | |
458 ifs.close(); | |
153 | 459 } |
460 | |
461 | |
462 void WHITELISTER::writer() { | |
463 pthread_mutex_lock(&mutex); | |
464 time_t limit = time(NULL) - days*86400; | |
160 | 465 |
466 // check for manually modified autowhitelist file | |
467 struct stat st; | |
468 if (stat(fn, &st)) need = true; // file has disappeared | |
469 else if (st.st_mtime > loaded) { | |
470 // file has been manually updated, merge new entries | |
471 merge(); | |
472 need = true; | |
473 } | |
474 | |
475 // purge old entries | |
153 | 476 for (autowhite_sent::iterator i=rcpts.begin(); i!=rcpts.end();) { |
477 time_t when = (*i).second; | |
478 if (when < limit) { | |
154 | 479 char *who = (*i).first; |
480 free(who); | |
153 | 481 autowhite_sent::iterator j = i; |
482 j++; | |
483 rcpts.erase(i); | |
484 i = j; | |
485 need = true; | |
486 } | |
487 else i++; | |
488 } | |
160 | 489 |
153 | 490 if (need) { |
491 // dump the file | |
154 | 492 ofstream ofs; |
493 ofs.open(fn); | |
494 if (!ofs.fail()) { | |
153 | 495 for (autowhite_sent::iterator i=rcpts.begin(); i!=rcpts.end(); i++) { |
496 char *who = (*i).first; | |
497 int when = (*i).second; | |
154 | 498 ofs << who << " " << when << endl; |
153 | 499 } |
500 } | |
154 | 501 ofs.close(); |
156 | 502 need = false; |
160 | 503 loaded = time(NULL); // update load time |
153 | 504 } |
505 pthread_mutex_unlock(&mutex); | |
506 } | |
507 | |
508 | |
509 void WHITELISTER::sent(char *to) { | |
154 | 510 // we take ownership of the string |
153 | 511 pthread_mutex_lock(&mutex); |
512 need = true; | |
513 rcpts[to] = time(NULL); | |
514 pthread_mutex_unlock(&mutex); | |
515 } | |
516 | |
517 | |
518 bool WHITELISTER::is_white(char *from) { | |
519 pthread_mutex_lock(&mutex); | |
520 autowhite_sent::iterator i = rcpts.find(from); | |
162 | 521 bool rc = (i != rcpts.end()); |
153 | 522 pthread_mutex_unlock(&mutex); |
523 return rc; | |
524 } | |
525 | |
526 | |
527 //////////////////////////////////////////////// | |
528 // setup a new auto whitelister file | |
529 // | |
530 WHITELISTERP add_whitelister_file(char *fn, int days); | |
531 WHITELISTERP add_whitelister_file(char *fn, int days) { | |
532 WHITELISTERP rc = NULL; | |
533 pthread_mutex_lock(&whitelister_mutex); | |
534 whitelister_map::iterator i = whitelisters.find(fn); | |
535 if (i == whitelisters.end()) { | |
536 rc = new WHITELISTER(fn, days); | |
537 whitelisters[fn] = rc; | |
538 } | |
156 | 539 else { |
540 rc = (*i).second; | |
541 rc->set_days(days); | |
542 } | |
153 | 543 pthread_mutex_unlock(&whitelister_mutex); |
544 return rc; | |
545 } | |
546 | |
547 | |
548 //////////////////////////////////////////////// | |
549 // thread to check for whitelister hosts with old sockets that we can close | |
550 // | |
551 void* whitelister_writer(void *arg) { | |
552 while (true) { | |
553 sleep(maxauto_age); | |
554 pthread_mutex_lock(&whitelister_mutex); | |
555 for (whitelister_map::iterator i=whitelisters.begin(); i!=whitelisters.end(); i++) { | |
556 WHITELISTERP v = (*i).second; | |
557 v->writer(); | |
558 } | |
559 pthread_mutex_unlock(&whitelister_mutex); | |
560 } | |
561 return NULL; | |
562 } | |
563 | |
564 | |
94 | 565 DNSBL::DNSBL(char *n, char *s, char *m) { |
566 name = n; | |
567 suffix = s; | |
568 message = m; | |
569 } | |
570 | |
571 | |
572 bool DNSBL::operator==(const DNSBL &rhs) { | |
573 return (strcmp(name, rhs.name) == 0) && | |
574 (strcmp(suffix, rhs.suffix) == 0) && | |
575 (strcmp(message, rhs.message) == 0); | |
576 } | |
577 | |
578 | |
579 CONFIG::CONFIG() { | |
580 reference_count = 0; | |
581 generation = 0; | |
582 load_time = 0; | |
583 default_context = NULL; | |
584 } | |
585 | |
586 | |
587 CONFIG::~CONFIG() { | |
146 | 588 if (debug_syslog) { |
589 char buf[maxlen]; | |
590 snprintf(buf, sizeof(buf), "freeing memory for old configuration generation %d", generation); | |
591 my_syslog(buf); | |
592 } | |
94 | 593 for (context_list::iterator i=contexts.begin(); i!=contexts.end(); i++) { |
594 CONTEXT *c = *i; | |
595 delete c; | |
596 } | |
597 } | |
598 | |
599 | |
600 void CONFIG::add_context(CONTEXTP con) { | |
601 contexts.push_back(con); | |
602 if (!default_context && !con->get_parent()) { | |
603 // first global context | |
604 default_context = con; | |
605 } | |
606 } | |
607 | |
608 | |
609 void CONFIG::add_to(char *to, CONTEXTP con) { | |
610 context_map::iterator i = env_to.find(to); | |
611 if (i != env_to.end()) { | |
612 CONTEXTP c = (*i).second; | |
613 if ((c != con) && (c != con->get_parent())) { | |
614 if (debug_syslog) { | |
615 char oldname[maxlen]; | |
616 char newname[maxlen]; | |
617 char *oldn = c->get_full_name(oldname, maxlen); | |
618 char *newn = con->get_full_name(newname, maxlen); | |
619 char buf[maxlen*3]; | |
620 snprintf(buf, maxlen*3, "both %s and %s claim envelope to %s, the second one wins", oldn, newn, to); | |
621 my_syslog(buf); | |
622 } | |
623 } | |
624 } | |
625 env_to[to] = con; | |
626 } | |
627 | |
628 | |
629 CONTEXTP CONFIG::find_context(char *to) { | |
630 context_map::iterator i = env_to.find(to); | |
117 | 631 if (i != env_to.end()) return (*i).second; // found user@domain key |
94 | 632 char *x = strchr(to, '@'); |
633 if (x) { | |
634 x++; | |
635 i = env_to.find(x); | |
117 | 636 if (i != env_to.end()) return (*i).second; // found domain key |
94 | 637 char y = *x; |
638 *x = '\0'; | |
639 i = env_to.find(to); | |
640 *x = y; | |
641 if (i != env_to.end()) return (*i).second; // found user@ key | |
642 } | |
643 return default_context; | |
644 } | |
645 | |
646 | |
647 void CONFIG::dump() { | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
648 if (default_context) default_context->dump(true); |
94 | 649 for (context_list::iterator i=contexts.begin(); i!=contexts.end(); i++) { |
650 CONTEXTP c = *i; | |
651 CONTEXTP p = c->get_parent(); | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
652 if (!p && (c != default_context)) c->dump(false); |
94 | 653 } |
654 char buf[maxlen]; | |
655 for (context_map::iterator i=env_to.begin(); i!=env_to.end(); i++) { | |
656 char *to = (*i).first; | |
657 CONTEXTP con = (*i).second; | |
658 printf("// envelope to %s \t-> context %s \n", to, con->get_full_name(buf,maxlen)); | |
659 } | |
660 } | |
661 | |
662 | |
663 CONTEXT::CONTEXT(CONTEXTP parent_, char *name_) { | |
664 parent = parent_; | |
665 name = name_; | |
666 verify_host = NULL; | |
153 | 667 verifier = NULL; |
668 autowhite_file = NULL; | |
669 whitelister = NULL; | |
94 | 670 env_from_default = (parent) ? token_inherit : token_unknown; |
671 content_filtering = (parent) ? parent->content_filtering : false; | |
672 content_suffix = NULL; | |
673 content_message = NULL; | |
119 | 674 uribl_suffix = NULL; |
675 uribl_message = NULL; | |
94 | 676 host_limit = (parent) ? parent->host_limit : 0; |
677 host_limit_message = NULL; | |
678 host_random = (parent) ? parent->host_random : false; | |
679 tag_limit = (parent) ? parent->tag_limit : 0; | |
680 tag_limit_message = NULL; | |
140 | 681 default_rcpt_rate = INT_MAX; |
94 | 682 } |
683 | |
684 | |
685 CONTEXT::~CONTEXT() { | |
686 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) { | |
687 DNSBLP d = (*i).second; | |
688 // delete the underlying DNSBL objects. | |
689 delete d; | |
690 } | |
691 } | |
692 | |
693 | |
694 bool CONTEXT::is_parent(CONTEXTP p) { | |
695 if (p == parent) return true; | |
696 if (!parent) return false; | |
697 return parent->is_parent(p); | |
698 } | |
699 | |
700 | |
701 char *CONTEXT::get_full_name(char *buffer, int size) { | |
702 if (!parent) return name; | |
703 char buf[maxlen]; | |
704 snprintf(buffer, size, "%s.%s", parent->get_full_name(buf, maxlen), name); | |
705 return buffer; | |
706 } | |
707 | |
708 | |
709 bool CONTEXT::cover_env_to(char *to) { | |
710 char buffer[maxlen]; | |
711 char *x = strchr(to, '@'); | |
712 if (x) x++; | |
713 else x = to; | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
714 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
|
715 if (!parent && env_to.empty()) return true; // empty env_to at global level covers everything |
94 | 716 string_set::iterator i = env_to.find(x); |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
717 if (i != env_to.end()) return true; // we cover the entire domain |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
718 if (x != to) { |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
719 i = env_to.find(to); |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
720 if (i != env_to.end()) return true; // we cover the specific email address |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
721 } |
94 | 722 return false; |
723 } | |
724 | |
725 | |
726 VERIFYP CONTEXT::find_verify(char *to) { | |
153 | 727 if (verifier && (verify_host != token_myhostname) && cover_env_to(to)) |
728 return verifier; | |
729 else if (parent) | |
730 return parent->find_verify(to); | |
731 else | |
732 return NULL; | |
733 } | |
94 | 734 |
153 | 735 |
162 | 736 WHITELISTERP CONTEXT::find_autowhite(char *from, char *to) { |
737 if (whitelister && cover_env_to(to) && !cover_env_to(from)) | |
153 | 738 return whitelister; |
739 else if (parent) | |
162 | 740 return parent->find_autowhite(from, to); |
153 | 741 else |
742 return NULL; | |
94 | 743 } |
744 | |
745 | |
136 | 746 int CONTEXT::find_rate(char *user) { |
140 | 747 if (rcpt_per_hour.empty()) return default_rcpt_rate; |
136 | 748 rcpt_rates::iterator i = rcpt_per_hour.find(user); |
140 | 749 return (i == rcpt_per_hour.end()) ? default_rcpt_rate : (*i).second; |
136 | 750 } |
751 | |
752 | |
94 | 753 char *CONTEXT::find_from(char *from) { |
153 | 754 if (whitelister && whitelister->is_white(from)) return token_white; |
148
9330b8d6a56b
add documentation fixes, allow env_from target of inherit
carl
parents:
146
diff
changeset
|
755 char *rc = env_from_default; |
94 | 756 string_map::iterator i = env_from.find(from); |
117 | 757 if (i != env_from.end()) rc = (*i).second; // found user@domain key |
94 | 758 else { |
759 char *x = strchr(from, '@'); | |
760 if (x) { | |
761 x++; | |
762 i = env_from.find(x); | |
117 | 763 if (i != env_from.end()) rc = (*i).second; // found domain key |
94 | 764 else { |
765 char y = *x; | |
766 *x = '\0'; | |
767 i = env_from.find(from); | |
768 *x = y; | |
769 if (i != env_from.end()) rc = (*i).second; // found user@ key | |
770 } | |
771 } | |
772 } | |
773 if ((rc == token_inherit) && parent) return parent->find_from(from); | |
774 return (rc == token_inherit) ? token_unknown : rc; | |
775 } | |
776 | |
777 | |
778 CONTEXTP CONTEXT::find_context(char *from) { | |
779 context_map::iterator i = env_from_context.find(from); | |
117 | 780 if (i != env_from_context.end()) return (*i).second; // found user@domain key |
94 | 781 char *x = strchr(from, '@'); |
782 if (x) { | |
783 x++; | |
784 i = env_from_context.find(x); | |
117 | 785 if (i != env_from_context.end()) return (*i).second; // found domain key |
94 | 786 char y = *x; |
787 *x = '\0'; | |
788 i = env_from_context.find(from); | |
789 *x = y; | |
790 if (i != env_from_context.end()) return (*i).second; // found user@ key | |
791 } | |
792 return this; | |
793 } | |
794 | |
795 | |
796 CONTEXTP CONTEXT::find_from_context_name(char *name) { | |
797 context_map::iterator i = children.find(name); | |
798 if (i != children.end()) return (*i).second; | |
799 return NULL; | |
800 } | |
801 | |
802 | |
803 DNSBLP CONTEXT::find_dnsbl(char *name) { | |
804 dnsblp_map::iterator i = dnsbl_names.find(name); | |
805 if (i != dnsbl_names.end()) return (*i).second; | |
806 if (parent) return parent->find_dnsbl(name); | |
807 return NULL; | |
808 } | |
809 | |
810 | |
811 char* CONTEXT::get_content_suffix() { | |
812 if (!content_suffix && parent) return parent->get_content_suffix(); | |
813 return content_suffix; | |
814 } | |
815 | |
816 | |
119 | 817 char* CONTEXT::get_uribl_suffix() { |
818 if (!uribl_suffix && parent) return parent->get_uribl_suffix(); | |
819 return uribl_suffix; | |
820 } | |
821 | |
822 | |
94 | 823 char* CONTEXT::get_content_message() { |
824 if (!content_message && parent) return parent->get_content_message(); | |
825 return content_message; | |
826 } | |
827 | |
828 | |
119 | 829 char* CONTEXT::get_uribl_message() { |
830 if (!uribl_message && parent) return parent->get_uribl_message(); | |
831 return uribl_message; | |
832 } | |
833 | |
834 | |
94 | 835 string_set& CONTEXT::get_content_host_ignore() { |
836 if (content_host_ignore.empty() && parent) return parent->get_content_host_ignore(); | |
837 return content_host_ignore; | |
838 } | |
839 | |
840 | |
117 | 841 string_set& CONTEXT::get_content_cctlds() { |
842 if (content_cctlds.empty() && parent) return parent->get_content_cctlds(); | |
843 return content_cctlds; | |
844 } | |
845 | |
94 | 846 string_set& CONTEXT::get_content_tlds() { |
847 if (content_tlds.empty() && parent) return parent->get_content_tlds(); | |
848 return content_tlds; | |
849 } | |
850 | |
851 | |
852 string_set& CONTEXT::get_html_tags() { | |
853 if (html_tags.empty() && parent) return parent->get_html_tags(); | |
854 return html_tags; | |
855 } | |
856 | |
857 | |
858 dnsblp_list& CONTEXT::get_dnsbl_list() { | |
859 if (dnsbl_list.empty() && parent) return parent->get_dnsbl_list(); | |
860 return dnsbl_list; | |
861 } | |
862 | |
863 | |
864 bool CONTEXT::acceptable_content(recorder &memory, char *&msg) { | |
865 if (memory.excessive_bad_tags(tag_limit)) { | |
866 msg = tag_limit_message; | |
867 return false; | |
868 } | |
869 if (!host_random && memory.excessive_hosts(host_limit)) { | |
870 msg = host_limit_message; | |
871 return false; | |
872 } | |
873 return true; | |
874 } | |
875 | |
876 | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
877 void CONTEXT::dump(bool isdefault, int level) { |
94 | 878 char indent[maxlen]; |
879 int i = min(maxlen-1, level*4); | |
880 memset(indent, ' ', i); | |
881 indent[i] = '\0'; | |
882 char buf[maxlen]; | |
883 char *fullname = get_full_name(buf,maxlen); | |
884 printf("%s context %s { \t// %s\n", indent, name, fullname); | |
885 | |
886 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) { | |
887 char *n = (*i).first; | |
888 DNSBL &d = *(*i).second; | |
889 printf("%s dnsbl %s %s \"%s\"; \n", indent, n, d.suffix, d.message); | |
890 } | |
891 | |
145 | 892 dnsblp_list dl = get_dnsbl_list(); |
893 if (!dl.empty()) { | |
94 | 894 printf("%s dnsbl_list", indent); |
145 | 895 for (dnsblp_list::iterator i=dl.begin(); i!=dl.end(); i++) { |
94 | 896 DNSBL &d = *(*i); |
897 printf(" %s", d.name); | |
898 } | |
899 printf("; \n"); | |
900 } | |
901 | |
902 if (content_filtering) { | |
903 printf("%s content on { \n", indent, env_from_default); | |
904 if (content_suffix) { | |
905 printf("%s filter %s \"%s\"; \n", indent, content_suffix, content_message); | |
906 } | |
119 | 907 if (uribl_suffix) { |
908 printf("%s uribl %s \"%s\"; \n", indent, uribl_suffix, uribl_message); | |
909 } | |
94 | 910 if (!content_host_ignore.empty()) { |
911 printf("%s ignore { \n", indent); | |
912 for (string_set::iterator i=content_host_ignore.begin(); i!=content_host_ignore.end(); i++) { | |
913 printf("%s %s; \n", indent, *i); | |
914 } | |
915 printf("%s }; \n", indent); | |
916 } | |
117 | 917 if (!content_cctlds.empty()) { |
918 printf("%s cctld { \n", indent); | |
919 printf("%s ", indent); | |
920 for (string_set::iterator i=content_cctlds.begin(); i!=content_cctlds.end(); i++) { | |
921 printf("%s; ", *i); | |
922 } | |
923 printf("\n%s }; \n", indent); | |
924 } | |
94 | 925 if (!content_tlds.empty()) { |
926 printf("%s tld { \n", indent); | |
927 printf("%s ", indent); | |
928 for (string_set::iterator i=content_tlds.begin(); i!=content_tlds.end(); i++) { | |
929 printf("%s; ", *i); | |
930 } | |
931 printf("\n%s }; \n", indent); | |
932 } | |
933 if (!html_tags.empty()) { | |
934 printf("%s html_tags { \n", indent); | |
935 printf("%s ", indent); | |
936 for (string_set::iterator i=html_tags.begin(); i!=html_tags.end(); i++) { | |
937 printf("%s; ", *i); | |
938 } | |
939 printf("\n%s }; \n", indent); | |
940 } | |
941 if (host_limit_message) { | |
942 printf("%s host_limit on %d \"%s\"; \n", indent, host_limit, host_limit_message); | |
943 } | |
944 else if (host_random) { | |
945 printf("%s host_limit soft %d; \n", indent, host_limit); | |
946 } | |
947 else { | |
948 printf("%s host_limit off; \n", indent); | |
949 } | |
950 if (tag_limit_message) { | |
951 printf("%s html_limit on %d \"%s\"; \n", indent, tag_limit, tag_limit_message); | |
952 } | |
953 else { | |
954 printf("%s html_limit off; \n", indent); | |
955 } | |
956 printf("%s }; \n", indent); | |
957 } | |
958 else { | |
959 printf("%s content off {}; \n", indent, env_from_default); | |
960 } | |
961 | |
962 printf("%s env_to { \t// %s\n", indent, fullname); | |
963 for (string_set::iterator i=env_to.begin(); i!=env_to.end(); i++) { | |
964 printf("%s %s; \n", indent, *i); | |
965 } | |
966 printf("%s }; \n", indent); | |
967 | |
968 if (verify_host) { | |
969 printf("%s verify %s; \n", indent, verify_host); | |
970 } | |
971 | |
153 | 972 if (autowhite_file && whitelister) { |
973 printf("%s autowhite %d %s; \n", indent, whitelister->get_days(), autowhite_file); | |
974 } | |
975 | |
94 | 976 for (context_map::iterator i=children.begin(); i!=children.end(); i++) { |
977 CONTEXTP c = (*i).second; | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
978 c->dump(false, level+1); |
94 | 979 } |
980 | |
981 printf("%s env_from %s { \t// %s\n", indent, env_from_default, fullname); | |
982 if (!env_from.empty()) { | |
983 printf("%s // white/black/unknown \n", indent); | |
984 for (string_map::iterator i=env_from.begin(); i!=env_from.end(); i++) { | |
985 char *f = (*i).first; | |
986 char *t = (*i).second; | |
987 printf("%s %s \t%s; \n", indent, f, t); | |
988 } | |
989 } | |
990 if (!env_from_context.empty()) { | |
991 printf("%s // child contexts \n", indent); | |
992 for (context_map::iterator j=env_from_context.begin(); j!=env_from_context.end(); j++) { | |
993 char *f = (*j).first; | |
994 CONTEXTP t = (*j).second; | |
995 printf("%s %s \t%s; \n", indent, f, t->name); | |
996 } | |
997 } | |
998 printf("%s }; \n", indent); | |
999 | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
1000 if (isdefault) { |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
1001 printf("%s rate_limit %d { \n", indent, default_rcpt_rate); |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
1002 for (rcpt_rates::iterator j=rcpt_per_hour.begin(); j!=rcpt_per_hour.end(); j++) { |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
1003 char *u = (*j).first; |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
1004 int l = (*j).second; |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
1005 printf("%s \"%s\" \t%d; \n", indent, u, l); |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
1006 } |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
1007 printf("%s }; \n", indent); |
136 | 1008 } |
1009 | |
94 | 1010 printf("%s }; \n", indent); |
1011 } | |
1012 | |
1013 | |
1014 //////////////////////////////////////////////// | |
1015 // helper to discard the strings held by a string_set | |
1016 // | |
1017 void discard(string_set &s) { | |
1018 for (string_set::iterator i=s.begin(); i!=s.end(); i++) { | |
1019 free(*i); | |
1020 } | |
1021 s.clear(); | |
1022 } | |
1023 | |
1024 | |
1025 //////////////////////////////////////////////// | |
1026 // helper to register a string in a string set | |
1027 // | |
1028 char* register_string(string_set &s, char *name) { | |
1029 string_set::iterator i = s.find(name); | |
1030 if (i != s.end()) return *i; | |
1031 char *x = strdup(name); | |
1032 s.insert(x); | |
1033 return x; | |
1034 } | |
1035 | |
1036 | |
1037 //////////////////////////////////////////////// | |
1038 // register a global string | |
1039 // | |
1040 char* register_string(char *name) { | |
1041 return register_string(all_strings, name); | |
1042 } | |
1043 | |
1044 | |
1045 //////////////////////////////////////////////// | |
1046 // | |
1047 bool tsa(TOKEN &tok, char *token); | |
1048 bool tsa(TOKEN &tok, char *token) { | |
1049 char *have = tok.next(); | |
1050 if (have == token) return true; | |
1051 tok.token_error(token, have); | |
1052 return false; | |
1053 } | |
1054 | |
1055 | |
1056 //////////////////////////////////////////////// | |
1057 // | |
1058 bool parse_dnsbl(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1059 bool parse_dnsbl(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1060 char *name = tok.next(); | |
1061 char *suf = tok.next(); | |
1062 char *msg = tok.next(); | |
1063 if (!tsa(tok, token_semi)) return false; | |
1064 DNSBLP dnsnew = new DNSBL(name, suf, msg); | |
1065 DNSBLP dnsold = me.find_dnsbl(name); | |
1066 if (dnsold && (*dnsold == *dnsnew)) { | |
1067 // duplicate redefinition, ignore it | |
1068 delete dnsnew; | |
1069 return true; | |
1070 } | |
1071 me.add_dnsbl(name, dnsnew); | |
1072 return true; | |
1073 } | |
1074 | |
1075 | |
1076 //////////////////////////////////////////////// | |
1077 // | |
1078 bool parse_dnsbll(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1079 bool parse_dnsbll(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1080 while (true) { | |
1081 char *have = tok.next(); | |
1082 if (!have) break; | |
1083 if (have == token_semi) break; | |
1084 DNSBLP dns = me.find_dnsbl(have); | |
1085 if (dns) { | |
1086 me.add_dnsbl(dns); | |
1087 } | |
1088 else { | |
1089 tok.token_error("dnsbl name", have); | |
1090 return false; | |
1091 } | |
1092 } | |
1093 return true; | |
1094 } | |
1095 | |
1096 | |
1097 //////////////////////////////////////////////// | |
1098 // | |
1099 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1100 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1101 char *setting = tok.next(); | |
1102 if (setting == token_on) { | |
1103 me.set_content_filtering(true); | |
1104 } | |
1105 else if (setting == token_off) { | |
1106 me.set_content_filtering(false); | |
1107 } | |
1108 else { | |
1109 tok.token_error("on/off", setting); | |
1110 return false; | |
1111 } | |
1112 if (!tsa(tok, token_lbrace)) return false; | |
1113 while (true) { | |
1114 char *have = tok.next(); | |
1115 if (!have) break; | |
1116 if (have == token_filter) { | |
1117 char *suffix = tok.next(); | |
1118 char *messag = tok.next(); | |
1119 me.set_content_suffix(suffix); | |
1120 me.set_content_message(messag); | |
1121 if (!tsa(tok, token_semi)) return false; | |
1122 } | |
119 | 1123 else if (have == token_uribl) { |
1124 char *suffix = tok.next(); | |
1125 char *messag = tok.next(); | |
1126 me.set_uribl_suffix(suffix); | |
1127 me.set_uribl_message(messag); | |
1128 if (!tsa(tok, token_semi)) return false; | |
1129 } | |
94 | 1130 else if (have == token_ignore) { |
1131 if (!tsa(tok, token_lbrace)) return false; | |
1132 while (true) { | |
1133 if (!have) break; | |
1134 char *have = tok.next(); | |
1135 if (have == token_rbrace) break; // done | |
1136 me.add_ignore(have); | |
1137 } | |
1138 if (!tsa(tok, token_semi)) return false; | |
1139 } | |
117 | 1140 else if (have == token_cctld) { |
1141 if (!tsa(tok, token_lbrace)) return false; | |
1142 while (true) { | |
1143 char *have = tok.next(); | |
1144 if (!have) break; | |
1145 if (have == token_rbrace) break; // done | |
1146 me.add_cctld(have); | |
1147 } | |
1148 if (!tsa(tok, token_semi)) return false; | |
1149 } | |
94 | 1150 else if (have == token_tld) { |
1151 if (!tsa(tok, token_lbrace)) return false; | |
1152 while (true) { | |
1153 char *have = tok.next(); | |
1154 if (!have) break; | |
1155 if (have == token_rbrace) break; // done | |
1156 me.add_tld(have); | |
1157 } | |
1158 if (!tsa(tok, token_semi)) return false; | |
1159 } | |
1160 else if (have == token_html_limit) { | |
1161 have = tok.next(); | |
1162 if (have == token_on) { | |
1163 me.set_tag_limit(tok.nextint()); | |
1164 me.set_tag_message(tok.next()); | |
1165 } | |
1166 else if (have == token_off) { | |
1167 me.set_tag_limit(0); | |
1168 me.set_tag_message(NULL); | |
1169 } | |
1170 else { | |
1171 tok.token_error("on/off", have); | |
1172 return false; | |
1173 } | |
1174 if (!tsa(tok, token_semi)) return false; | |
1175 } | |
1176 else if (have == token_html_tags) { | |
1177 if (!tsa(tok, token_lbrace)) return false; | |
1178 while (true) { | |
1179 char *have = tok.next(); | |
1180 if (!have) break; | |
1181 if (have == token_rbrace) { | |
1182 break; // done | |
1183 } | |
1184 else { | |
1185 me.add_tag(have); // base version | |
1186 char buf[200]; | |
1187 snprintf(buf, sizeof(buf), "/%s", have); | |
1188 me.add_tag(register_string(buf)); // leading / | |
1189 snprintf(buf, sizeof(buf), "%s/", have); | |
1190 me.add_tag(register_string(buf)); // trailing / | |
1191 } | |
1192 } | |
1193 if (!tsa(tok, token_semi)) return false; | |
1194 } | |
1195 else if (have == token_host_limit) { | |
1196 have = tok.next(); | |
1197 if (have == token_on) { | |
1198 me.set_host_limit(tok.nextint()); | |
1199 me.set_host_message(tok.next()); | |
1200 me.set_host_random(false); | |
1201 } | |
1202 else if (have == token_off) { | |
1203 me.set_host_limit(0); | |
1204 me.set_host_message(NULL); | |
1205 me.set_host_random(false); | |
1206 } | |
1207 else if (have == token_soft) { | |
1208 me.set_host_limit(tok.nextint()); | |
1209 me.set_host_message(NULL); | |
1210 me.set_host_random(true); | |
1211 } | |
1212 else { | |
1213 tok.token_error("on/off/soft", have); | |
1214 return false; | |
1215 } | |
1216 if (!tsa(tok, token_semi)) return false; | |
1217 } | |
1218 else if (have == token_rbrace) { | |
1219 break; // done | |
1220 } | |
1221 else { | |
1222 tok.token_error("content keyword", have); | |
1223 return false; | |
1224 } | |
1225 } | |
1226 return tsa(tok, token_semi); | |
1227 } | |
1228 | |
1229 | |
1230 //////////////////////////////////////////////// | |
1231 // | |
1232 bool parse_envto(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1233 bool parse_envto(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1234 if (!tsa(tok, token_lbrace)) return false; | |
1235 while (true) { | |
1236 char *have = tok.next(); | |
1237 if (!have) break; | |
1238 if (have == token_rbrace) break; | |
1239 if (have == token_semi) { | |
1240 // optional separators | |
1241 } | |
1242 else if (have == token_dccto) { | |
1243 char *flavor = tok.next(); | |
1244 if (!tsa(tok, token_lbrace)) return false; | |
1245 bool keeping = false; | |
1246 while (true) { | |
1247 char *have = tok.next(); | |
1248 if (!have) break; | |
1249 if (have == token_rbrace) break; | |
1250 if (have == flavor) { | |
1251 keeping = true; | |
1252 continue; | |
1253 } | |
1254 else if ((have == token_ok) || (have == token_ok2) || (have == token_many)) { | |
1255 keeping = false; | |
1256 continue; | |
1257 } | |
1258 if (have == token_envto) { | |
1259 have = tok.next(); | |
1260 if (keeping) { | |
1261 if (me.allow_env_to(have)) { | |
1262 me.add_to(have); | |
1263 dc.add_to(have, &me); | |
1264 } | |
1265 } | |
1266 } | |
1267 //else if (have == token_substitute) { | |
1268 // if (tok.next() == token_mailhost) { | |
1269 // have = tok.next(); | |
1270 // if (keeping) { | |
1271 // if (me.allow_env_to(have)) { | |
1272 // me.add_to(have); | |
1273 // dc.add_to(have, &me); | |
1274 // } | |
1275 // } | |
1276 // } | |
1277 //} | |
1278 tok.skipeol(); | |
1279 } | |
1280 } | |
1281 else if (me.allow_env_to(have)) { | |
1282 me.add_to(have); | |
1283 dc.add_to(have, &me); | |
1284 } | |
1285 else { | |
1286 tok.token_error("user@ or user@domain.tld or domain.tld where domain.tld allowed by parent context", have); | |
1287 return false; | |
1288 } | |
1289 } | |
1290 return tsa(tok, token_semi); | |
1291 } | |
1292 | |
1293 | |
1294 //////////////////////////////////////////////// | |
1295 // | |
1296 bool parse_verify(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1297 bool parse_verify(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1298 char *host = tok.next(); | |
1299 if (!tsa(tok, token_semi)) return false; | |
1300 me.set_verify(host); | |
153 | 1301 me.set_verifier(add_verify_host(host)); |
1302 return true; | |
1303 } | |
1304 | |
1305 | |
1306 //////////////////////////////////////////////// | |
1307 // | |
1308 bool parse_autowhite(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1309 bool parse_autowhite(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1310 int days = tok.nextint(); | |
1311 char *fn = tok.next(); | |
1312 if (!tsa(tok, token_semi)) return false; | |
1313 me.set_autowhite(fn); | |
1314 me.set_whitelister(add_whitelister_file(fn, days)); | |
99 | 1315 return true; |
94 | 1316 } |
1317 | |
1318 | |
1319 //////////////////////////////////////////////// | |
1320 // | |
1321 bool parse_envfrom(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1322 bool parse_envfrom(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1323 char *st = tok.next(); | |
1324 if ((st == token_black) || (st == token_white) || (st == token_unknown) || (st == token_inherit)) { | |
1325 me.set_from_default(st); | |
1326 } | |
1327 else { | |
1328 tok.push(st); | |
1329 } | |
1330 if (!tsa(tok, token_lbrace)) return false; | |
1331 while (true) { | |
1332 char *have = tok.next(); | |
1333 if (!have) break; | |
1334 if (have == token_rbrace) break; | |
1335 if (have == token_semi) { | |
1336 // optional separators | |
1337 } | |
1338 else if (have == token_dccfrom) { | |
1339 if (!tsa(tok, token_lbrace)) return false; | |
1340 bool keeping = false; | |
1341 bool many = false; | |
1342 while (true) { | |
1343 char *have = tok.next(); | |
1344 if (!have) break; | |
1345 if (have == token_rbrace) break; | |
1346 if (have == token_ok) { | |
1347 keeping = true; | |
1348 many = false; | |
1349 continue; | |
1350 } | |
1351 else if (have == token_many) { | |
1352 keeping = true; | |
1353 many = true; | |
1354 continue; | |
1355 } | |
1356 else if (have == token_ok2) { | |
1357 keeping = false; | |
1358 continue; | |
1359 } | |
1360 if (have == token_envfrom) { | |
1361 have = tok.next(); | |
1362 if (keeping) { | |
1363 me.add_from(have, (many) ? token_black : token_white); | |
1364 } | |
1365 } | |
1366 else if (have == token_substitute) { | |
1367 if (tok.next() == token_mailhost) { | |
1368 have = tok.next(); | |
1369 me.add_from(have, (many) ? token_black : token_white); | |
1370 } | |
1371 } | |
1372 tok.skipeol(); | |
1373 } | |
1374 } | |
1375 else { | |
1376 // may be a valid email address or domain name | |
1377 char *st = tok.next(); | |
148
9330b8d6a56b
add documentation fixes, allow env_from target of inherit
carl
parents:
146
diff
changeset
|
1378 if ((st == token_white) || (st == token_black) || (st == token_unknown) || (st == token_inherit)) { |
94 | 1379 me.add_from(have, st); |
1380 } | |
1381 else { | |
1382 CONTEXTP con = me.find_from_context_name(st); | |
1383 if (con) { | |
1384 me.add_from_context(have, con); | |
1385 } | |
1386 else { | |
148
9330b8d6a56b
add documentation fixes, allow env_from target of inherit
carl
parents:
146
diff
changeset
|
1387 tok.token_error("white/black/unknown/inherit or child context name", st); |
94 | 1388 return false; |
1389 } | |
1390 } | |
1391 } | |
1392 } | |
1393 return tsa(tok, token_semi); | |
1394 } | |
1395 | |
1396 | |
1397 //////////////////////////////////////////////// | |
1398 // | |
136 | 1399 bool parse_rate(TOKEN &tok, CONFIG &dc, CONTEXT &me); |
1400 bool parse_rate(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
140 | 1401 char *def = tok.next(); |
141 | 1402 tok.push(def); |
1403 if (def != token_lbrace) me.set_default_rate(tok.nextint()); | |
136 | 1404 if (!tsa(tok, token_lbrace)) return false; |
1405 while (true) { | |
1406 char *have = tok.next(); | |
1407 if (!have) break; | |
1408 if (have == token_rbrace) break; | |
1409 if (have == token_semi) { | |
1410 // optional separators | |
1411 } | |
1412 else { | |
140 | 1413 me.add_rate(have, tok.nextint()); |
136 | 1414 } |
1415 } | |
1416 return tsa(tok, token_semi); | |
1417 } | |
1418 | |
1419 | |
1420 //////////////////////////////////////////////// | |
1421 // | |
94 | 1422 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent); |
1423 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent) { | |
1424 char *name = tok.next(); | |
1425 if (!tsa(tok, token_lbrace)) return false; | |
1426 CONTEXTP con = new CONTEXT(parent, name); | |
1427 | |
1428 while (true) { | |
1429 char *have = tok.next(); | |
1430 if (!have) break; | |
1431 if (have == token_rbrace) break; // done | |
1432 if (have == token_dnsbl) { | |
1433 if (!parse_dnsbl(tok, dc, *con)) return false; | |
1434 } | |
1435 else if (have == token_dnsbll) { | |
1436 if (!parse_dnsbll(tok, dc, *con)) return false; | |
1437 } | |
1438 else if (have == token_content) { | |
1439 if (!parse_content(tok, dc, *con)) return false; | |
1440 } | |
1441 else if (have == token_envto) { | |
1442 if (!parse_envto(tok, dc, *con)) return false; | |
1443 } | |
1444 else if (have == token_verify) { | |
1445 if (!parse_verify(tok, dc, *con)) return false; | |
1446 } | |
153 | 1447 else if (have == token_autowhite) { |
1448 if (!parse_autowhite(tok, dc, *con)) return false; | |
1449 } | |
94 | 1450 else if (have == token_envfrom) { |
1451 if (!parse_envfrom(tok, dc, *con)) return false; | |
1452 } | |
140 | 1453 else if (have == token_rate) { |
1454 if (parent || dc.default_context) tok.token_error("rate limit ignored in non default context"); | |
136 | 1455 if (!parse_rate(tok, dc, *con)) return false; |
1456 } | |
94 | 1457 else if (have == token_context) { |
1458 if (!parse_context(tok, dc, con)) return false; | |
1459 } | |
1460 else { | |
1461 tok.token_error("context keyword", have); | |
1462 return false; | |
1463 } | |
1464 } | |
1465 | |
1466 if (!tsa(tok, token_semi)) { | |
1467 delete con; | |
1468 return false; | |
1469 } | |
1470 dc.add_context(con); | |
1471 if (parent) parent->add_context(con); | |
1472 return true; | |
1473 } | |
1474 | |
1475 | |
1476 //////////////////////////////////////////////// | |
1477 // parse a config file | |
1478 // | |
1479 bool load_conf(CONFIG &dc, char *fn) { | |
99 | 1480 int count = 0; |
94 | 1481 TOKEN tok(fn, &dc.config_files); |
1482 while (true) { | |
1483 char *have = tok.next(); | |
1484 if (!have) break; | |
1485 if (have == token_context) { | |
1486 if (!parse_context(tok, dc, NULL)) { | |
99 | 1487 tok.token_error("load_conf() failed to parse context"); |
94 | 1488 return false; |
1489 } | |
99 | 1490 else count++; |
94 | 1491 } |
1492 else { | |
1493 tok.token_error(token_context, have); | |
1494 return false; | |
1495 } | |
1496 } | |
99 | 1497 tok.token_error("load_conf() found %d contexts in %s", count, fn); |
94 | 1498 return (dc.default_context) ? true : false; |
1499 } | |
1500 | |
1501 | |
1502 //////////////////////////////////////////////// | |
1503 // init the tokens | |
1504 // | |
1505 void token_init() { | |
153 | 1506 token_autowhite = register_string("autowhite"); |
94 | 1507 token_black = register_string("black"); |
117 | 1508 token_cctld = register_string("cctld"); |
94 | 1509 token_content = register_string("content"); |
1510 token_context = register_string("context"); | |
1511 token_dccfrom = register_string("dcc_from"); | |
1512 token_dccto = register_string("dcc_to"); | |
1513 token_default = register_string("default"); | |
1514 token_dnsbl = register_string("dnsbl"); | |
1515 token_dnsbll = register_string("dnsbl_list"); | |
1516 token_envfrom = register_string("env_from"); | |
1517 token_envto = register_string("env_to"); | |
1518 token_filter = register_string("filter"); | |
1519 token_host_limit = register_string("host_limit"); | |
1520 token_html_limit = register_string("html_limit"); | |
1521 token_html_tags = register_string("html_tags"); | |
1522 token_ignore = register_string("ignore"); | |
1523 token_include = register_string("include"); | |
1524 token_inherit = register_string("inherit"); | |
1525 token_lbrace = register_string("{"); | |
1526 token_mailhost = register_string("mail_host"); | |
1527 token_many = register_string("many"); | |
1528 token_off = register_string("off"); | |
1529 token_ok = register_string("ok"); | |
1530 token_ok2 = register_string("ok2"); | |
1531 token_on = register_string("on"); | |
136 | 1532 token_rate = register_string("rate_limit"); |
94 | 1533 token_rbrace = register_string("}"); |
1534 token_semi = register_string(";"); | |
1535 token_soft = register_string("soft"); | |
1536 token_substitute = register_string("substitute"); | |
1537 token_tld = register_string("tld"); | |
1538 token_unknown = register_string("unknown"); | |
119 | 1539 token_uribl = register_string("uribl"); |
94 | 1540 token_verify = register_string("verify"); |
1541 token_white = register_string("white"); | |
1542 | |
1543 if (gethostname(myhostname, HOST_NAME_MAX+1) != 0) { | |
1544 strncpy(myhostname, "localhost", HOST_NAME_MAX+1); | |
1545 } | |
1546 myhostname[HOST_NAME_MAX] = '\0'; // ensure null termination | |
1547 token_myhostname = register_string(myhostname); | |
1548 } |