Mercurial > dnsbl
annotate src/context.cpp @ 160:b3ed72ee6564
allow manual updates to auto whitelist files
author | carl |
---|---|
date | Tue, 10 Jul 2007 11:20:23 -0700 |
parents | a220bfb9211f |
children | c4bce911c276 |
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 bool rc = false; | |
520 pthread_mutex_lock(&mutex); | |
521 autowhite_sent::iterator i = rcpts.find(from); | |
522 if (i != rcpts.end()) { | |
523 time_t when = (*i).second; | |
524 time_t now = time(NULL); | |
525 rc = (when+(days*8640) > now); | |
526 } | |
527 pthread_mutex_unlock(&mutex); | |
528 return rc; | |
529 } | |
530 | |
531 | |
532 //////////////////////////////////////////////// | |
533 // setup a new auto whitelister file | |
534 // | |
535 WHITELISTERP add_whitelister_file(char *fn, int days); | |
536 WHITELISTERP add_whitelister_file(char *fn, int days) { | |
537 WHITELISTERP rc = NULL; | |
538 pthread_mutex_lock(&whitelister_mutex); | |
539 whitelister_map::iterator i = whitelisters.find(fn); | |
540 if (i == whitelisters.end()) { | |
541 rc = new WHITELISTER(fn, days); | |
542 whitelisters[fn] = rc; | |
543 } | |
156 | 544 else { |
545 rc = (*i).second; | |
546 rc->set_days(days); | |
547 } | |
153 | 548 pthread_mutex_unlock(&whitelister_mutex); |
549 return rc; | |
550 } | |
551 | |
552 | |
553 //////////////////////////////////////////////// | |
554 // thread to check for whitelister hosts with old sockets that we can close | |
555 // | |
556 void* whitelister_writer(void *arg) { | |
557 while (true) { | |
558 sleep(maxauto_age); | |
559 pthread_mutex_lock(&whitelister_mutex); | |
560 for (whitelister_map::iterator i=whitelisters.begin(); i!=whitelisters.end(); i++) { | |
561 WHITELISTERP v = (*i).second; | |
562 v->writer(); | |
563 } | |
564 pthread_mutex_unlock(&whitelister_mutex); | |
565 } | |
566 return NULL; | |
567 } | |
568 | |
569 | |
94 | 570 DNSBL::DNSBL(char *n, char *s, char *m) { |
571 name = n; | |
572 suffix = s; | |
573 message = m; | |
574 } | |
575 | |
576 | |
577 bool DNSBL::operator==(const DNSBL &rhs) { | |
578 return (strcmp(name, rhs.name) == 0) && | |
579 (strcmp(suffix, rhs.suffix) == 0) && | |
580 (strcmp(message, rhs.message) == 0); | |
581 } | |
582 | |
583 | |
584 CONFIG::CONFIG() { | |
585 reference_count = 0; | |
586 generation = 0; | |
587 load_time = 0; | |
588 default_context = NULL; | |
589 } | |
590 | |
591 | |
592 CONFIG::~CONFIG() { | |
146 | 593 if (debug_syslog) { |
594 char buf[maxlen]; | |
595 snprintf(buf, sizeof(buf), "freeing memory for old configuration generation %d", generation); | |
596 my_syslog(buf); | |
597 } | |
94 | 598 for (context_list::iterator i=contexts.begin(); i!=contexts.end(); i++) { |
599 CONTEXT *c = *i; | |
600 delete c; | |
601 } | |
602 } | |
603 | |
604 | |
605 void CONFIG::add_context(CONTEXTP con) { | |
606 contexts.push_back(con); | |
607 if (!default_context && !con->get_parent()) { | |
608 // first global context | |
609 default_context = con; | |
610 } | |
611 } | |
612 | |
613 | |
614 void CONFIG::add_to(char *to, CONTEXTP con) { | |
615 context_map::iterator i = env_to.find(to); | |
616 if (i != env_to.end()) { | |
617 CONTEXTP c = (*i).second; | |
618 if ((c != con) && (c != con->get_parent())) { | |
619 if (debug_syslog) { | |
620 char oldname[maxlen]; | |
621 char newname[maxlen]; | |
622 char *oldn = c->get_full_name(oldname, maxlen); | |
623 char *newn = con->get_full_name(newname, maxlen); | |
624 char buf[maxlen*3]; | |
625 snprintf(buf, maxlen*3, "both %s and %s claim envelope to %s, the second one wins", oldn, newn, to); | |
626 my_syslog(buf); | |
627 } | |
628 } | |
629 } | |
630 env_to[to] = con; | |
631 } | |
632 | |
633 | |
634 CONTEXTP CONFIG::find_context(char *to) { | |
635 context_map::iterator i = env_to.find(to); | |
117 | 636 if (i != env_to.end()) return (*i).second; // found user@domain key |
94 | 637 char *x = strchr(to, '@'); |
638 if (x) { | |
639 x++; | |
640 i = env_to.find(x); | |
117 | 641 if (i != env_to.end()) return (*i).second; // found domain key |
94 | 642 char y = *x; |
643 *x = '\0'; | |
644 i = env_to.find(to); | |
645 *x = y; | |
646 if (i != env_to.end()) return (*i).second; // found user@ key | |
647 } | |
648 return default_context; | |
649 } | |
650 | |
651 | |
652 void CONFIG::dump() { | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
653 if (default_context) default_context->dump(true); |
94 | 654 for (context_list::iterator i=contexts.begin(); i!=contexts.end(); i++) { |
655 CONTEXTP c = *i; | |
656 CONTEXTP p = c->get_parent(); | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
657 if (!p && (c != default_context)) c->dump(false); |
94 | 658 } |
659 char buf[maxlen]; | |
660 for (context_map::iterator i=env_to.begin(); i!=env_to.end(); i++) { | |
661 char *to = (*i).first; | |
662 CONTEXTP con = (*i).second; | |
663 printf("// envelope to %s \t-> context %s \n", to, con->get_full_name(buf,maxlen)); | |
664 } | |
665 } | |
666 | |
667 | |
668 CONTEXT::CONTEXT(CONTEXTP parent_, char *name_) { | |
669 parent = parent_; | |
670 name = name_; | |
671 verify_host = NULL; | |
153 | 672 verifier = NULL; |
673 autowhite_file = NULL; | |
674 whitelister = NULL; | |
94 | 675 env_from_default = (parent) ? token_inherit : token_unknown; |
676 content_filtering = (parent) ? parent->content_filtering : false; | |
677 content_suffix = NULL; | |
678 content_message = NULL; | |
119 | 679 uribl_suffix = NULL; |
680 uribl_message = NULL; | |
94 | 681 host_limit = (parent) ? parent->host_limit : 0; |
682 host_limit_message = NULL; | |
683 host_random = (parent) ? parent->host_random : false; | |
684 tag_limit = (parent) ? parent->tag_limit : 0; | |
685 tag_limit_message = NULL; | |
140 | 686 default_rcpt_rate = INT_MAX; |
94 | 687 } |
688 | |
689 | |
690 CONTEXT::~CONTEXT() { | |
691 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) { | |
692 DNSBLP d = (*i).second; | |
693 // delete the underlying DNSBL objects. | |
694 delete d; | |
695 } | |
696 } | |
697 | |
698 | |
699 bool CONTEXT::is_parent(CONTEXTP p) { | |
700 if (p == parent) return true; | |
701 if (!parent) return false; | |
702 return parent->is_parent(p); | |
703 } | |
704 | |
705 | |
706 char *CONTEXT::get_full_name(char *buffer, int size) { | |
707 if (!parent) return name; | |
708 char buf[maxlen]; | |
709 snprintf(buffer, size, "%s.%s", parent->get_full_name(buf, maxlen), name); | |
710 return buffer; | |
711 } | |
712 | |
713 | |
714 bool CONTEXT::cover_env_to(char *to) { | |
715 char buffer[maxlen]; | |
716 char *x = strchr(to, '@'); | |
717 if (x) x++; | |
718 else x = to; | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
719 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
|
720 if (!parent && env_to.empty()) return true; // empty env_to at global level covers everything |
94 | 721 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
|
722 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
|
723 if (x != to) { |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
724 i = env_to.find(to); |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
725 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
|
726 } |
94 | 727 return false; |
728 } | |
729 | |
730 | |
731 VERIFYP CONTEXT::find_verify(char *to) { | |
153 | 732 if (verifier && (verify_host != token_myhostname) && cover_env_to(to)) |
733 return verifier; | |
734 else if (parent) | |
735 return parent->find_verify(to); | |
736 else | |
737 return NULL; | |
738 } | |
94 | 739 |
153 | 740 |
741 WHITELISTERP CONTEXT::find_autowhite(char *to) { | |
742 if (whitelister && cover_env_to(to)) | |
743 return whitelister; | |
744 else if (parent) | |
745 return parent->find_autowhite(to); | |
746 else | |
747 return NULL; | |
94 | 748 } |
749 | |
750 | |
136 | 751 int CONTEXT::find_rate(char *user) { |
140 | 752 if (rcpt_per_hour.empty()) return default_rcpt_rate; |
136 | 753 rcpt_rates::iterator i = rcpt_per_hour.find(user); |
140 | 754 return (i == rcpt_per_hour.end()) ? default_rcpt_rate : (*i).second; |
136 | 755 } |
756 | |
757 | |
94 | 758 char *CONTEXT::find_from(char *from) { |
153 | 759 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
|
760 char *rc = env_from_default; |
94 | 761 string_map::iterator i = env_from.find(from); |
117 | 762 if (i != env_from.end()) rc = (*i).second; // found user@domain key |
94 | 763 else { |
764 char *x = strchr(from, '@'); | |
765 if (x) { | |
766 x++; | |
767 i = env_from.find(x); | |
117 | 768 if (i != env_from.end()) rc = (*i).second; // found domain key |
94 | 769 else { |
770 char y = *x; | |
771 *x = '\0'; | |
772 i = env_from.find(from); | |
773 *x = y; | |
774 if (i != env_from.end()) rc = (*i).second; // found user@ key | |
775 } | |
776 } | |
777 } | |
778 if ((rc == token_inherit) && parent) return parent->find_from(from); | |
779 return (rc == token_inherit) ? token_unknown : rc; | |
780 } | |
781 | |
782 | |
783 CONTEXTP CONTEXT::find_context(char *from) { | |
784 context_map::iterator i = env_from_context.find(from); | |
117 | 785 if (i != env_from_context.end()) return (*i).second; // found user@domain key |
94 | 786 char *x = strchr(from, '@'); |
787 if (x) { | |
788 x++; | |
789 i = env_from_context.find(x); | |
117 | 790 if (i != env_from_context.end()) return (*i).second; // found domain key |
94 | 791 char y = *x; |
792 *x = '\0'; | |
793 i = env_from_context.find(from); | |
794 *x = y; | |
795 if (i != env_from_context.end()) return (*i).second; // found user@ key | |
796 } | |
797 return this; | |
798 } | |
799 | |
800 | |
801 CONTEXTP CONTEXT::find_from_context_name(char *name) { | |
802 context_map::iterator i = children.find(name); | |
803 if (i != children.end()) return (*i).second; | |
804 return NULL; | |
805 } | |
806 | |
807 | |
808 DNSBLP CONTEXT::find_dnsbl(char *name) { | |
809 dnsblp_map::iterator i = dnsbl_names.find(name); | |
810 if (i != dnsbl_names.end()) return (*i).second; | |
811 if (parent) return parent->find_dnsbl(name); | |
812 return NULL; | |
813 } | |
814 | |
815 | |
816 char* CONTEXT::get_content_suffix() { | |
817 if (!content_suffix && parent) return parent->get_content_suffix(); | |
818 return content_suffix; | |
819 } | |
820 | |
821 | |
119 | 822 char* CONTEXT::get_uribl_suffix() { |
823 if (!uribl_suffix && parent) return parent->get_uribl_suffix(); | |
824 return uribl_suffix; | |
825 } | |
826 | |
827 | |
94 | 828 char* CONTEXT::get_content_message() { |
829 if (!content_message && parent) return parent->get_content_message(); | |
830 return content_message; | |
831 } | |
832 | |
833 | |
119 | 834 char* CONTEXT::get_uribl_message() { |
835 if (!uribl_message && parent) return parent->get_uribl_message(); | |
836 return uribl_message; | |
837 } | |
838 | |
839 | |
94 | 840 string_set& CONTEXT::get_content_host_ignore() { |
841 if (content_host_ignore.empty() && parent) return parent->get_content_host_ignore(); | |
842 return content_host_ignore; | |
843 } | |
844 | |
845 | |
117 | 846 string_set& CONTEXT::get_content_cctlds() { |
847 if (content_cctlds.empty() && parent) return parent->get_content_cctlds(); | |
848 return content_cctlds; | |
849 } | |
850 | |
94 | 851 string_set& CONTEXT::get_content_tlds() { |
852 if (content_tlds.empty() && parent) return parent->get_content_tlds(); | |
853 return content_tlds; | |
854 } | |
855 | |
856 | |
857 string_set& CONTEXT::get_html_tags() { | |
858 if (html_tags.empty() && parent) return parent->get_html_tags(); | |
859 return html_tags; | |
860 } | |
861 | |
862 | |
863 dnsblp_list& CONTEXT::get_dnsbl_list() { | |
864 if (dnsbl_list.empty() && parent) return parent->get_dnsbl_list(); | |
865 return dnsbl_list; | |
866 } | |
867 | |
868 | |
869 bool CONTEXT::acceptable_content(recorder &memory, char *&msg) { | |
870 if (memory.excessive_bad_tags(tag_limit)) { | |
871 msg = tag_limit_message; | |
872 return false; | |
873 } | |
874 if (!host_random && memory.excessive_hosts(host_limit)) { | |
875 msg = host_limit_message; | |
876 return false; | |
877 } | |
878 return true; | |
879 } | |
880 | |
881 | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
882 void CONTEXT::dump(bool isdefault, int level) { |
94 | 883 char indent[maxlen]; |
884 int i = min(maxlen-1, level*4); | |
885 memset(indent, ' ', i); | |
886 indent[i] = '\0'; | |
887 char buf[maxlen]; | |
888 char *fullname = get_full_name(buf,maxlen); | |
889 printf("%s context %s { \t// %s\n", indent, name, fullname); | |
890 | |
891 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) { | |
892 char *n = (*i).first; | |
893 DNSBL &d = *(*i).second; | |
894 printf("%s dnsbl %s %s \"%s\"; \n", indent, n, d.suffix, d.message); | |
895 } | |
896 | |
145 | 897 dnsblp_list dl = get_dnsbl_list(); |
898 if (!dl.empty()) { | |
94 | 899 printf("%s dnsbl_list", indent); |
145 | 900 for (dnsblp_list::iterator i=dl.begin(); i!=dl.end(); i++) { |
94 | 901 DNSBL &d = *(*i); |
902 printf(" %s", d.name); | |
903 } | |
904 printf("; \n"); | |
905 } | |
906 | |
907 if (content_filtering) { | |
908 printf("%s content on { \n", indent, env_from_default); | |
909 if (content_suffix) { | |
910 printf("%s filter %s \"%s\"; \n", indent, content_suffix, content_message); | |
911 } | |
119 | 912 if (uribl_suffix) { |
913 printf("%s uribl %s \"%s\"; \n", indent, uribl_suffix, uribl_message); | |
914 } | |
94 | 915 if (!content_host_ignore.empty()) { |
916 printf("%s ignore { \n", indent); | |
917 for (string_set::iterator i=content_host_ignore.begin(); i!=content_host_ignore.end(); i++) { | |
918 printf("%s %s; \n", indent, *i); | |
919 } | |
920 printf("%s }; \n", indent); | |
921 } | |
117 | 922 if (!content_cctlds.empty()) { |
923 printf("%s cctld { \n", indent); | |
924 printf("%s ", indent); | |
925 for (string_set::iterator i=content_cctlds.begin(); i!=content_cctlds.end(); i++) { | |
926 printf("%s; ", *i); | |
927 } | |
928 printf("\n%s }; \n", indent); | |
929 } | |
94 | 930 if (!content_tlds.empty()) { |
931 printf("%s tld { \n", indent); | |
932 printf("%s ", indent); | |
933 for (string_set::iterator i=content_tlds.begin(); i!=content_tlds.end(); i++) { | |
934 printf("%s; ", *i); | |
935 } | |
936 printf("\n%s }; \n", indent); | |
937 } | |
938 if (!html_tags.empty()) { | |
939 printf("%s html_tags { \n", indent); | |
940 printf("%s ", indent); | |
941 for (string_set::iterator i=html_tags.begin(); i!=html_tags.end(); i++) { | |
942 printf("%s; ", *i); | |
943 } | |
944 printf("\n%s }; \n", indent); | |
945 } | |
946 if (host_limit_message) { | |
947 printf("%s host_limit on %d \"%s\"; \n", indent, host_limit, host_limit_message); | |
948 } | |
949 else if (host_random) { | |
950 printf("%s host_limit soft %d; \n", indent, host_limit); | |
951 } | |
952 else { | |
953 printf("%s host_limit off; \n", indent); | |
954 } | |
955 if (tag_limit_message) { | |
956 printf("%s html_limit on %d \"%s\"; \n", indent, tag_limit, tag_limit_message); | |
957 } | |
958 else { | |
959 printf("%s html_limit off; \n", indent); | |
960 } | |
961 printf("%s }; \n", indent); | |
962 } | |
963 else { | |
964 printf("%s content off {}; \n", indent, env_from_default); | |
965 } | |
966 | |
967 printf("%s env_to { \t// %s\n", indent, fullname); | |
968 for (string_set::iterator i=env_to.begin(); i!=env_to.end(); i++) { | |
969 printf("%s %s; \n", indent, *i); | |
970 } | |
971 printf("%s }; \n", indent); | |
972 | |
973 if (verify_host) { | |
974 printf("%s verify %s; \n", indent, verify_host); | |
975 } | |
976 | |
153 | 977 if (autowhite_file && whitelister) { |
978 printf("%s autowhite %d %s; \n", indent, whitelister->get_days(), autowhite_file); | |
979 } | |
980 | |
94 | 981 for (context_map::iterator i=children.begin(); i!=children.end(); i++) { |
982 CONTEXTP c = (*i).second; | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
983 c->dump(false, level+1); |
94 | 984 } |
985 | |
986 printf("%s env_from %s { \t// %s\n", indent, env_from_default, fullname); | |
987 if (!env_from.empty()) { | |
988 printf("%s // white/black/unknown \n", indent); | |
989 for (string_map::iterator i=env_from.begin(); i!=env_from.end(); i++) { | |
990 char *f = (*i).first; | |
991 char *t = (*i).second; | |
992 printf("%s %s \t%s; \n", indent, f, t); | |
993 } | |
994 } | |
995 if (!env_from_context.empty()) { | |
996 printf("%s // child contexts \n", indent); | |
997 for (context_map::iterator j=env_from_context.begin(); j!=env_from_context.end(); j++) { | |
998 char *f = (*j).first; | |
999 CONTEXTP t = (*j).second; | |
1000 printf("%s %s \t%s; \n", indent, f, t->name); | |
1001 } | |
1002 } | |
1003 printf("%s }; \n", indent); | |
1004 | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
1005 if (isdefault) { |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
1006 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
|
1007 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
|
1008 char *u = (*j).first; |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
1009 int l = (*j).second; |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
1010 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
|
1011 } |
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
1012 printf("%s }; \n", indent); |
136 | 1013 } |
1014 | |
94 | 1015 printf("%s }; \n", indent); |
1016 } | |
1017 | |
1018 | |
1019 //////////////////////////////////////////////// | |
1020 // helper to discard the strings held by a string_set | |
1021 // | |
1022 void discard(string_set &s) { | |
1023 for (string_set::iterator i=s.begin(); i!=s.end(); i++) { | |
1024 free(*i); | |
1025 } | |
1026 s.clear(); | |
1027 } | |
1028 | |
1029 | |
1030 //////////////////////////////////////////////// | |
1031 // helper to register a string in a string set | |
1032 // | |
1033 char* register_string(string_set &s, char *name) { | |
1034 string_set::iterator i = s.find(name); | |
1035 if (i != s.end()) return *i; | |
1036 char *x = strdup(name); | |
1037 s.insert(x); | |
1038 return x; | |
1039 } | |
1040 | |
1041 | |
1042 //////////////////////////////////////////////// | |
1043 // register a global string | |
1044 // | |
1045 char* register_string(char *name) { | |
1046 return register_string(all_strings, name); | |
1047 } | |
1048 | |
1049 | |
1050 //////////////////////////////////////////////// | |
1051 // | |
1052 bool tsa(TOKEN &tok, char *token); | |
1053 bool tsa(TOKEN &tok, char *token) { | |
1054 char *have = tok.next(); | |
1055 if (have == token) return true; | |
1056 tok.token_error(token, have); | |
1057 return false; | |
1058 } | |
1059 | |
1060 | |
1061 //////////////////////////////////////////////// | |
1062 // | |
1063 bool parse_dnsbl(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1064 bool parse_dnsbl(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1065 char *name = tok.next(); | |
1066 char *suf = tok.next(); | |
1067 char *msg = tok.next(); | |
1068 if (!tsa(tok, token_semi)) return false; | |
1069 DNSBLP dnsnew = new DNSBL(name, suf, msg); | |
1070 DNSBLP dnsold = me.find_dnsbl(name); | |
1071 if (dnsold && (*dnsold == *dnsnew)) { | |
1072 // duplicate redefinition, ignore it | |
1073 delete dnsnew; | |
1074 return true; | |
1075 } | |
1076 me.add_dnsbl(name, dnsnew); | |
1077 return true; | |
1078 } | |
1079 | |
1080 | |
1081 //////////////////////////////////////////////// | |
1082 // | |
1083 bool parse_dnsbll(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1084 bool parse_dnsbll(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1085 while (true) { | |
1086 char *have = tok.next(); | |
1087 if (!have) break; | |
1088 if (have == token_semi) break; | |
1089 DNSBLP dns = me.find_dnsbl(have); | |
1090 if (dns) { | |
1091 me.add_dnsbl(dns); | |
1092 } | |
1093 else { | |
1094 tok.token_error("dnsbl name", have); | |
1095 return false; | |
1096 } | |
1097 } | |
1098 return true; | |
1099 } | |
1100 | |
1101 | |
1102 //////////////////////////////////////////////// | |
1103 // | |
1104 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1105 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1106 char *setting = tok.next(); | |
1107 if (setting == token_on) { | |
1108 me.set_content_filtering(true); | |
1109 } | |
1110 else if (setting == token_off) { | |
1111 me.set_content_filtering(false); | |
1112 } | |
1113 else { | |
1114 tok.token_error("on/off", setting); | |
1115 return false; | |
1116 } | |
1117 if (!tsa(tok, token_lbrace)) return false; | |
1118 while (true) { | |
1119 char *have = tok.next(); | |
1120 if (!have) break; | |
1121 if (have == token_filter) { | |
1122 char *suffix = tok.next(); | |
1123 char *messag = tok.next(); | |
1124 me.set_content_suffix(suffix); | |
1125 me.set_content_message(messag); | |
1126 if (!tsa(tok, token_semi)) return false; | |
1127 } | |
119 | 1128 else if (have == token_uribl) { |
1129 char *suffix = tok.next(); | |
1130 char *messag = tok.next(); | |
1131 me.set_uribl_suffix(suffix); | |
1132 me.set_uribl_message(messag); | |
1133 if (!tsa(tok, token_semi)) return false; | |
1134 } | |
94 | 1135 else if (have == token_ignore) { |
1136 if (!tsa(tok, token_lbrace)) return false; | |
1137 while (true) { | |
1138 if (!have) break; | |
1139 char *have = tok.next(); | |
1140 if (have == token_rbrace) break; // done | |
1141 me.add_ignore(have); | |
1142 } | |
1143 if (!tsa(tok, token_semi)) return false; | |
1144 } | |
117 | 1145 else if (have == token_cctld) { |
1146 if (!tsa(tok, token_lbrace)) return false; | |
1147 while (true) { | |
1148 char *have = tok.next(); | |
1149 if (!have) break; | |
1150 if (have == token_rbrace) break; // done | |
1151 me.add_cctld(have); | |
1152 } | |
1153 if (!tsa(tok, token_semi)) return false; | |
1154 } | |
94 | 1155 else if (have == token_tld) { |
1156 if (!tsa(tok, token_lbrace)) return false; | |
1157 while (true) { | |
1158 char *have = tok.next(); | |
1159 if (!have) break; | |
1160 if (have == token_rbrace) break; // done | |
1161 me.add_tld(have); | |
1162 } | |
1163 if (!tsa(tok, token_semi)) return false; | |
1164 } | |
1165 else if (have == token_html_limit) { | |
1166 have = tok.next(); | |
1167 if (have == token_on) { | |
1168 me.set_tag_limit(tok.nextint()); | |
1169 me.set_tag_message(tok.next()); | |
1170 } | |
1171 else if (have == token_off) { | |
1172 me.set_tag_limit(0); | |
1173 me.set_tag_message(NULL); | |
1174 } | |
1175 else { | |
1176 tok.token_error("on/off", have); | |
1177 return false; | |
1178 } | |
1179 if (!tsa(tok, token_semi)) return false; | |
1180 } | |
1181 else if (have == token_html_tags) { | |
1182 if (!tsa(tok, token_lbrace)) return false; | |
1183 while (true) { | |
1184 char *have = tok.next(); | |
1185 if (!have) break; | |
1186 if (have == token_rbrace) { | |
1187 break; // done | |
1188 } | |
1189 else { | |
1190 me.add_tag(have); // base version | |
1191 char buf[200]; | |
1192 snprintf(buf, sizeof(buf), "/%s", have); | |
1193 me.add_tag(register_string(buf)); // leading / | |
1194 snprintf(buf, sizeof(buf), "%s/", have); | |
1195 me.add_tag(register_string(buf)); // trailing / | |
1196 } | |
1197 } | |
1198 if (!tsa(tok, token_semi)) return false; | |
1199 } | |
1200 else if (have == token_host_limit) { | |
1201 have = tok.next(); | |
1202 if (have == token_on) { | |
1203 me.set_host_limit(tok.nextint()); | |
1204 me.set_host_message(tok.next()); | |
1205 me.set_host_random(false); | |
1206 } | |
1207 else if (have == token_off) { | |
1208 me.set_host_limit(0); | |
1209 me.set_host_message(NULL); | |
1210 me.set_host_random(false); | |
1211 } | |
1212 else if (have == token_soft) { | |
1213 me.set_host_limit(tok.nextint()); | |
1214 me.set_host_message(NULL); | |
1215 me.set_host_random(true); | |
1216 } | |
1217 else { | |
1218 tok.token_error("on/off/soft", have); | |
1219 return false; | |
1220 } | |
1221 if (!tsa(tok, token_semi)) return false; | |
1222 } | |
1223 else if (have == token_rbrace) { | |
1224 break; // done | |
1225 } | |
1226 else { | |
1227 tok.token_error("content keyword", have); | |
1228 return false; | |
1229 } | |
1230 } | |
1231 return tsa(tok, token_semi); | |
1232 } | |
1233 | |
1234 | |
1235 //////////////////////////////////////////////// | |
1236 // | |
1237 bool parse_envto(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1238 bool parse_envto(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1239 if (!tsa(tok, token_lbrace)) return false; | |
1240 while (true) { | |
1241 char *have = tok.next(); | |
1242 if (!have) break; | |
1243 if (have == token_rbrace) break; | |
1244 if (have == token_semi) { | |
1245 // optional separators | |
1246 } | |
1247 else if (have == token_dccto) { | |
1248 char *flavor = tok.next(); | |
1249 if (!tsa(tok, token_lbrace)) return false; | |
1250 bool keeping = false; | |
1251 while (true) { | |
1252 char *have = tok.next(); | |
1253 if (!have) break; | |
1254 if (have == token_rbrace) break; | |
1255 if (have == flavor) { | |
1256 keeping = true; | |
1257 continue; | |
1258 } | |
1259 else if ((have == token_ok) || (have == token_ok2) || (have == token_many)) { | |
1260 keeping = false; | |
1261 continue; | |
1262 } | |
1263 if (have == token_envto) { | |
1264 have = tok.next(); | |
1265 if (keeping) { | |
1266 if (me.allow_env_to(have)) { | |
1267 me.add_to(have); | |
1268 dc.add_to(have, &me); | |
1269 } | |
1270 } | |
1271 } | |
1272 //else if (have == token_substitute) { | |
1273 // if (tok.next() == token_mailhost) { | |
1274 // have = tok.next(); | |
1275 // if (keeping) { | |
1276 // if (me.allow_env_to(have)) { | |
1277 // me.add_to(have); | |
1278 // dc.add_to(have, &me); | |
1279 // } | |
1280 // } | |
1281 // } | |
1282 //} | |
1283 tok.skipeol(); | |
1284 } | |
1285 } | |
1286 else if (me.allow_env_to(have)) { | |
1287 me.add_to(have); | |
1288 dc.add_to(have, &me); | |
1289 } | |
1290 else { | |
1291 tok.token_error("user@ or user@domain.tld or domain.tld where domain.tld allowed by parent context", have); | |
1292 return false; | |
1293 } | |
1294 } | |
1295 return tsa(tok, token_semi); | |
1296 } | |
1297 | |
1298 | |
1299 //////////////////////////////////////////////// | |
1300 // | |
1301 bool parse_verify(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1302 bool parse_verify(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1303 char *host = tok.next(); | |
1304 if (!tsa(tok, token_semi)) return false; | |
1305 me.set_verify(host); | |
153 | 1306 me.set_verifier(add_verify_host(host)); |
1307 return true; | |
1308 } | |
1309 | |
1310 | |
1311 //////////////////////////////////////////////// | |
1312 // | |
1313 bool parse_autowhite(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1314 bool parse_autowhite(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1315 int days = tok.nextint(); | |
1316 char *fn = tok.next(); | |
1317 if (!tsa(tok, token_semi)) return false; | |
1318 me.set_autowhite(fn); | |
1319 me.set_whitelister(add_whitelister_file(fn, days)); | |
99 | 1320 return true; |
94 | 1321 } |
1322 | |
1323 | |
1324 //////////////////////////////////////////////// | |
1325 // | |
1326 bool parse_envfrom(TOKEN &tok, CONFIG &dc, CONTEXT &me); | |
1327 bool parse_envfrom(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
1328 char *st = tok.next(); | |
1329 if ((st == token_black) || (st == token_white) || (st == token_unknown) || (st == token_inherit)) { | |
1330 me.set_from_default(st); | |
1331 } | |
1332 else { | |
1333 tok.push(st); | |
1334 } | |
1335 if (!tsa(tok, token_lbrace)) return false; | |
1336 while (true) { | |
1337 char *have = tok.next(); | |
1338 if (!have) break; | |
1339 if (have == token_rbrace) break; | |
1340 if (have == token_semi) { | |
1341 // optional separators | |
1342 } | |
1343 else if (have == token_dccfrom) { | |
1344 if (!tsa(tok, token_lbrace)) return false; | |
1345 bool keeping = false; | |
1346 bool many = false; | |
1347 while (true) { | |
1348 char *have = tok.next(); | |
1349 if (!have) break; | |
1350 if (have == token_rbrace) break; | |
1351 if (have == token_ok) { | |
1352 keeping = true; | |
1353 many = false; | |
1354 continue; | |
1355 } | |
1356 else if (have == token_many) { | |
1357 keeping = true; | |
1358 many = true; | |
1359 continue; | |
1360 } | |
1361 else if (have == token_ok2) { | |
1362 keeping = false; | |
1363 continue; | |
1364 } | |
1365 if (have == token_envfrom) { | |
1366 have = tok.next(); | |
1367 if (keeping) { | |
1368 me.add_from(have, (many) ? token_black : token_white); | |
1369 } | |
1370 } | |
1371 else if (have == token_substitute) { | |
1372 if (tok.next() == token_mailhost) { | |
1373 have = tok.next(); | |
1374 me.add_from(have, (many) ? token_black : token_white); | |
1375 } | |
1376 } | |
1377 tok.skipeol(); | |
1378 } | |
1379 } | |
1380 else { | |
1381 // may be a valid email address or domain name | |
1382 char *st = tok.next(); | |
148
9330b8d6a56b
add documentation fixes, allow env_from target of inherit
carl
parents:
146
diff
changeset
|
1383 if ((st == token_white) || (st == token_black) || (st == token_unknown) || (st == token_inherit)) { |
94 | 1384 me.add_from(have, st); |
1385 } | |
1386 else { | |
1387 CONTEXTP con = me.find_from_context_name(st); | |
1388 if (con) { | |
1389 me.add_from_context(have, con); | |
1390 } | |
1391 else { | |
148
9330b8d6a56b
add documentation fixes, allow env_from target of inherit
carl
parents:
146
diff
changeset
|
1392 tok.token_error("white/black/unknown/inherit or child context name", st); |
94 | 1393 return false; |
1394 } | |
1395 } | |
1396 } | |
1397 } | |
1398 return tsa(tok, token_semi); | |
1399 } | |
1400 | |
1401 | |
1402 //////////////////////////////////////////////// | |
1403 // | |
136 | 1404 bool parse_rate(TOKEN &tok, CONFIG &dc, CONTEXT &me); |
1405 bool parse_rate(TOKEN &tok, CONFIG &dc, CONTEXT &me) { | |
140 | 1406 char *def = tok.next(); |
141 | 1407 tok.push(def); |
1408 if (def != token_lbrace) me.set_default_rate(tok.nextint()); | |
136 | 1409 if (!tsa(tok, token_lbrace)) return false; |
1410 while (true) { | |
1411 char *have = tok.next(); | |
1412 if (!have) break; | |
1413 if (have == token_rbrace) break; | |
1414 if (have == token_semi) { | |
1415 // optional separators | |
1416 } | |
1417 else { | |
140 | 1418 me.add_rate(have, tok.nextint()); |
136 | 1419 } |
1420 } | |
1421 return tsa(tok, token_semi); | |
1422 } | |
1423 | |
1424 | |
1425 //////////////////////////////////////////////// | |
1426 // | |
94 | 1427 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent); |
1428 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent) { | |
1429 char *name = tok.next(); | |
1430 if (!tsa(tok, token_lbrace)) return false; | |
1431 CONTEXTP con = new CONTEXT(parent, name); | |
1432 | |
1433 while (true) { | |
1434 char *have = tok.next(); | |
1435 if (!have) break; | |
1436 if (have == token_rbrace) break; // done | |
1437 if (have == token_dnsbl) { | |
1438 if (!parse_dnsbl(tok, dc, *con)) return false; | |
1439 } | |
1440 else if (have == token_dnsbll) { | |
1441 if (!parse_dnsbll(tok, dc, *con)) return false; | |
1442 } | |
1443 else if (have == token_content) { | |
1444 if (!parse_content(tok, dc, *con)) return false; | |
1445 } | |
1446 else if (have == token_envto) { | |
1447 if (!parse_envto(tok, dc, *con)) return false; | |
1448 } | |
1449 else if (have == token_verify) { | |
1450 if (!parse_verify(tok, dc, *con)) return false; | |
1451 } | |
153 | 1452 else if (have == token_autowhite) { |
1453 if (!parse_autowhite(tok, dc, *con)) return false; | |
1454 } | |
94 | 1455 else if (have == token_envfrom) { |
1456 if (!parse_envfrom(tok, dc, *con)) return false; | |
1457 } | |
140 | 1458 else if (have == token_rate) { |
1459 if (parent || dc.default_context) tok.token_error("rate limit ignored in non default context"); | |
136 | 1460 if (!parse_rate(tok, dc, *con)) return false; |
1461 } | |
94 | 1462 else if (have == token_context) { |
1463 if (!parse_context(tok, dc, con)) return false; | |
1464 } | |
1465 else { | |
1466 tok.token_error("context keyword", have); | |
1467 return false; | |
1468 } | |
1469 } | |
1470 | |
1471 if (!tsa(tok, token_semi)) { | |
1472 delete con; | |
1473 return false; | |
1474 } | |
1475 dc.add_context(con); | |
1476 if (parent) parent->add_context(con); | |
1477 return true; | |
1478 } | |
1479 | |
1480 | |
1481 //////////////////////////////////////////////// | |
1482 // parse a config file | |
1483 // | |
1484 bool load_conf(CONFIG &dc, char *fn) { | |
99 | 1485 int count = 0; |
94 | 1486 TOKEN tok(fn, &dc.config_files); |
1487 while (true) { | |
1488 char *have = tok.next(); | |
1489 if (!have) break; | |
1490 if (have == token_context) { | |
1491 if (!parse_context(tok, dc, NULL)) { | |
99 | 1492 tok.token_error("load_conf() failed to parse context"); |
94 | 1493 return false; |
1494 } | |
99 | 1495 else count++; |
94 | 1496 } |
1497 else { | |
1498 tok.token_error(token_context, have); | |
1499 return false; | |
1500 } | |
1501 } | |
99 | 1502 tok.token_error("load_conf() found %d contexts in %s", count, fn); |
94 | 1503 return (dc.default_context) ? true : false; |
1504 } | |
1505 | |
1506 | |
1507 //////////////////////////////////////////////// | |
1508 // init the tokens | |
1509 // | |
1510 void token_init() { | |
153 | 1511 token_autowhite = register_string("autowhite"); |
94 | 1512 token_black = register_string("black"); |
117 | 1513 token_cctld = register_string("cctld"); |
94 | 1514 token_content = register_string("content"); |
1515 token_context = register_string("context"); | |
1516 token_dccfrom = register_string("dcc_from"); | |
1517 token_dccto = register_string("dcc_to"); | |
1518 token_default = register_string("default"); | |
1519 token_dnsbl = register_string("dnsbl"); | |
1520 token_dnsbll = register_string("dnsbl_list"); | |
1521 token_envfrom = register_string("env_from"); | |
1522 token_envto = register_string("env_to"); | |
1523 token_filter = register_string("filter"); | |
1524 token_host_limit = register_string("host_limit"); | |
1525 token_html_limit = register_string("html_limit"); | |
1526 token_html_tags = register_string("html_tags"); | |
1527 token_ignore = register_string("ignore"); | |
1528 token_include = register_string("include"); | |
1529 token_inherit = register_string("inherit"); | |
1530 token_lbrace = register_string("{"); | |
1531 token_mailhost = register_string("mail_host"); | |
1532 token_many = register_string("many"); | |
1533 token_off = register_string("off"); | |
1534 token_ok = register_string("ok"); | |
1535 token_ok2 = register_string("ok2"); | |
1536 token_on = register_string("on"); | |
136 | 1537 token_rate = register_string("rate_limit"); |
94 | 1538 token_rbrace = register_string("}"); |
1539 token_semi = register_string(";"); | |
1540 token_soft = register_string("soft"); | |
1541 token_substitute = register_string("substitute"); | |
1542 token_tld = register_string("tld"); | |
1543 token_unknown = register_string("unknown"); | |
119 | 1544 token_uribl = register_string("uribl"); |
94 | 1545 token_verify = register_string("verify"); |
1546 token_white = register_string("white"); | |
1547 | |
1548 if (gethostname(myhostname, HOST_NAME_MAX+1) != 0) { | |
1549 strncpy(myhostname, "localhost", HOST_NAME_MAX+1); | |
1550 } | |
1551 myhostname[HOST_NAME_MAX] = '\0'; // ensure null termination | |
1552 token_myhostname = register_string(myhostname); | |
1553 } |