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