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