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