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