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