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