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