Mercurial > dnsbl
annotate src/dnsbl.cpp @ 167:9b129ed78d7d stable-6-0-6
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
author | carl |
---|---|
date | Mon, 27 Aug 2007 20:49:19 -0700 |
parents | bf828f785407 |
children | 6bac960af6b4 |
rev | line source |
---|---|
94 | 1 /* |
2 | |
152 | 3 Copyright (c) 2007 Carl Byington - 510 Software Group, released under |
4 the GPL version 3 or any later version at your choice available at | |
5 http://www.gnu.org/licenses/gpl-3.0.txt | |
94 | 6 |
7 Based on a sample milter Copyright (c) 2000-2003 Sendmail, Inc. and its | |
8 suppliers. Inspired by the DCC by Rhyolite Software | |
9 | |
10 -r port The port used to talk to our internal dns resolver processes | |
11 -p port The port through which the MTA will connect to this milter. | |
12 -t sec The timeout value. | |
13 -c Check the config, and print a copy to stdout. Don't start the | |
14 milter or do anything with the socket. | |
15 -s Stress test by loading and deleting the current config in a loop. | |
163 | 16 -d level set the debug level |
94 | 17 -e f|t Print the results of looking up from address f and to address |
18 t in the current config | |
19 | |
20 */ | |
21 | |
22 | |
23 // from sendmail sample | |
24 #include <sys/types.h> | |
25 #include <sys/stat.h> | |
26 #include <errno.h> | |
27 #include <sysexits.h> | |
28 #include <unistd.h> | |
29 | |
30 // needed for socket io | |
31 #include <sys/ioctl.h> | |
32 #include <net/if.h> | |
33 #include <arpa/inet.h> | |
34 #include <netinet/in.h> | |
35 #include <netinet/tcp.h> | |
36 #include <netdb.h> | |
37 #include <sys/socket.h> | |
38 #include <sys/un.h> | |
39 | |
40 // needed for thread | |
41 #include <pthread.h> | |
42 | |
43 // needed for std c++ collections | |
44 #include <set> | |
45 #include <map> | |
46 #include <list> | |
47 | |
48 // for the dns resolver | |
49 #include <netinet/in.h> | |
50 #include <arpa/nameser.h> | |
51 #include <resolv.h> | |
52 | |
53 // misc stuff needed here | |
54 #include <ctype.h> | |
55 #include <syslog.h> | |
56 #include <pwd.h> | |
57 #include <sys/wait.h> /* header for waitpid() and various macros */ | |
58 #include <signal.h> /* header for signal functions */ | |
59 | |
60 #include "includes.h" | |
61 | |
62 static char* dnsbl_version="$Id$"; | |
63 | |
64 | |
65 extern "C" { | |
148
9330b8d6a56b
add documentation fixes, allow env_from target of inherit
carl
parents:
146
diff
changeset
|
66 #include <libmilter/mfapi.h> |
94 | 67 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr); |
163 | 68 sfsistat mlfi_helo(SMFICTX * ctx, char *helohost); |
94 | 69 sfsistat mlfi_envfrom(SMFICTX *ctx, char **argv); |
70 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **argv); | |
163 | 71 sfsistat mlfi_header(SMFICTX* ctx, char* headerf, char* headerv); |
94 | 72 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len); |
73 sfsistat mlfi_eom(SMFICTX *ctx); | |
74 sfsistat mlfi_abort(SMFICTX *ctx); | |
75 sfsistat mlfi_close(SMFICTX *ctx); | |
76 void sig_chld(int signo); | |
77 } | |
78 | |
79 int debug_syslog = 0; | |
80 bool syslog_opened = false; | |
81 bool use_syslog = true; // false to printf | |
82 bool loader_run = true; // used to stop the config loader thread | |
83 CONFIG *config = NULL; // protected by the config_mutex | |
84 int generation = 0; // protected by the config_mutex | |
85 const int maxlen = 1000; // used for snprintf buffers | |
86 | |
87 pthread_mutex_t config_mutex; | |
88 pthread_mutex_t syslog_mutex; | |
89 pthread_mutex_t resolve_mutex; | |
90 pthread_mutex_t fd_pool_mutex; | |
136 | 91 pthread_mutex_t rate_mutex; |
94 | 92 |
93 std::set<int> fd_pool; | |
136 | 94 int NULL_SOCKET = -1; |
95 const time_t ERROR_SOCKET_TIME = 60; // number of seconds between attempts to open a socket to the dns resolver process | |
96 char *resolver_port = NULL; // unix domain socket to talk to the dns resolver process | |
97 int resolver_socket = NULL_SOCKET; // socket used to listen for resolver requests | |
98 time_t last_error_time; | |
99 int resolver_sock_count = 0; // protected with fd_pool_mutex | |
100 int resolver_pool_size = 0; // protected with fd_pool_mutex | |
101 rcpt_rates rcpt_counts; // protected with rate_mutex | |
94 | 102 |
103 | |
104 struct ns_map { | |
105 // all the strings are owned by the keys/values in the ns_host string map | |
106 string_map ns_host; // nameserver name -> host name that uses this name server | |
107 ns_mapper ns_ip; // nameserver name -> ip address of the name server | |
108 ~ns_map(); | |
109 void add(char *name, char *refer); | |
110 }; | |
111 | |
112 | |
113 ns_map::~ns_map() { | |
114 for (string_map::iterator i=ns_host.begin(); i!=ns_host.end(); i++) { | |
115 char *x = (*i).first; | |
116 char *y = (*i).second; | |
117 free(x); | |
118 free(y); | |
119 } | |
120 ns_ip.clear(); | |
121 ns_host.clear(); | |
122 } | |
123 | |
124 | |
125 void ns_map::add(char *name, char *refer) { | |
126 string_map::iterator i = ns_host.find(name); | |
127 if (i != ns_host.end()) return; | |
128 char *x = strdup(name); | |
129 char *y = strdup(refer); | |
130 ns_ip[x] = 0; | |
131 ns_host[x] = y; | |
132 | |
133 } | |
134 | |
135 // packed structure to allow a single socket write to dump the | |
136 // length and the following answer. The packing attribute is gcc specific. | |
137 struct glommer { | |
138 int length; | |
139 #ifdef NS_PACKETSZ | |
115 | 140 u_char answer[NS_PACKETSZ*4]; // with a resolver, we return resolver answers |
94 | 141 #else |
142 int answer; // without a resolver, we return a single ip4 address, 0 == no answer | |
143 #endif | |
144 } __attribute__ ((packed)); | |
145 | |
146 | |
147 //////////////////////////////////////////////// | |
136 | 148 // helper to manipulate recipient counts |
149 // | |
150 int incr_rcpt_count(char *user); | |
151 int incr_rcpt_count(char *user) { | |
152 pthread_mutex_lock(&rate_mutex); | |
153 rcpt_rates::iterator i = rcpt_counts.find(user); | |
138 | 154 int c = 1; |
155 if (i == rcpt_counts.end()) { | |
156 user = strdup(user); | |
157 rcpt_counts[user] = c; | |
158 } | |
159 else { | |
139 | 160 c = ++((*i).second); |
138 | 161 } |
136 | 162 pthread_mutex_unlock(&rate_mutex); |
163 return c; | |
164 } | |
165 | |
166 //////////////////////////////////////////////// | |
94 | 167 // helper to discard the strings held by a context_map |
168 // | |
169 void discard(context_map &cm); | |
170 void discard(context_map &cm) { | |
171 for (context_map::iterator i=cm.begin(); i!=cm.end(); i++) { | |
172 char *x = (*i).first; | |
173 free(x); | |
174 } | |
175 cm.clear(); | |
176 } | |
177 | |
178 | |
179 //////////////////////////////////////////////// | |
180 // helper to register a string in a context_map | |
181 // | |
182 void register_string(context_map &cm, char *name, CONTEXT *con); | |
183 void register_string(context_map &cm, char *name, CONTEXT *con) { | |
184 context_map::iterator i = cm.find(name); | |
185 if (i != cm.end()) return; | |
186 char *x = strdup(name); | |
187 cm[x] = con; | |
188 } | |
189 | |
190 | |
191 //////////////////////////////////////////////// | |
192 // disconnect the fd from the dns resolver process | |
193 // | |
194 void my_disconnect(int sock, bool decrement = true); | |
195 void my_disconnect(int sock, bool decrement) { | |
196 if (sock != NULL_SOCKET) { | |
197 if (decrement) { | |
198 pthread_mutex_lock(&fd_pool_mutex); | |
199 resolver_sock_count--; | |
200 pthread_mutex_unlock(&fd_pool_mutex); | |
201 } | |
202 shutdown(sock, SHUT_RDWR); | |
203 close(sock); | |
204 } | |
205 } | |
206 | |
207 | |
208 //////////////////////////////////////////////// | |
209 // return fd connected to the dns resolver process | |
210 // | |
211 int my_connect(); | |
212 int my_connect() { | |
213 // if we have had recent errors, don't even try to open the socket | |
129
c5cd1261394d
ignore smtp connection attempts for 10 minutes when getting connection errors on verify hosts
carl
parents:
128
diff
changeset
|
214 if ((time(NULL) - last_error_time) < ERROR_SOCKET_TIME) return NULL_SOCKET; |
94 | 215 |
216 // nothing recent, maybe this time it will work | |
217 int sock = NULL_SOCKET; | |
218 sockaddr_un server; | |
219 memset(&server, '\0', sizeof(server)); | |
220 server.sun_family = AF_UNIX; | |
221 strncpy(server.sun_path, resolver_port, sizeof(server.sun_path)-1); | |
222 sock = socket(AF_UNIX, SOCK_STREAM, 0); | |
223 if (sock != NULL_SOCKET) { | |
224 bool rc = (connect(sock, (sockaddr *)&server, sizeof(server)) == 0); | |
225 if (!rc) { | |
226 my_disconnect(sock, false); | |
227 sock = NULL_SOCKET; | |
129
c5cd1261394d
ignore smtp connection attempts for 10 minutes when getting connection errors on verify hosts
carl
parents:
128
diff
changeset
|
228 last_error_time = time(NULL); |
94 | 229 } |
230 } | |
129
c5cd1261394d
ignore smtp connection attempts for 10 minutes when getting connection errors on verify hosts
carl
parents:
128
diff
changeset
|
231 else last_error_time = time(NULL); |
94 | 232 if (sock != NULL_SOCKET) { |
233 pthread_mutex_lock(&fd_pool_mutex); | |
234 resolver_sock_count++; | |
235 pthread_mutex_unlock(&fd_pool_mutex); | |
236 } | |
237 return sock; | |
238 } | |
239 | |
240 | |
241 mlfiPriv::mlfiPriv() { | |
242 pthread_mutex_lock(&config_mutex); | |
243 pc = config; | |
244 pc->reference_count++; | |
245 pthread_mutex_unlock(&config_mutex); | |
246 get_fd(); | |
247 ip = 0; | |
163 | 248 helo = NULL; |
94 | 249 mailaddr = NULL; |
250 queueid = NULL; | |
136 | 251 authenticated = NULL; |
94 | 252 have_whites = false; |
253 only_whites = true; | |
167
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
254 want_spamassassin = false; |
94 | 255 memory = NULL; |
256 scanner = NULL; | |
165 | 257 assassin = NULL; |
94 | 258 content_suffix = NULL; |
259 content_message = NULL; | |
119 | 260 uribl_suffix = NULL; |
261 uribl_message = NULL; | |
94 | 262 content_host_ignore = NULL; |
263 } | |
264 | |
265 mlfiPriv::~mlfiPriv() { | |
266 return_fd(); | |
267 pthread_mutex_lock(&config_mutex); | |
268 pc->reference_count--; | |
146 | 269 bool last = (!pc->reference_count) && (pc != config); |
94 | 270 pthread_mutex_unlock(&config_mutex); |
146 | 271 if (last) delete pc; // free this config, since we were the last reference to it |
163 | 272 if (helo) free(helo); |
94 | 273 reset(true); |
274 } | |
275 | |
276 void mlfiPriv::reset(bool final) { | |
136 | 277 if (mailaddr) free(mailaddr); |
278 if (queueid) free(queueid); | |
279 if (authenticated) free(authenticated); | |
94 | 280 discard(env_to); |
163 | 281 if (memory) delete memory; |
282 if (scanner) delete scanner; | |
283 if (assassin) delete assassin; | |
94 | 284 if (!final) { |
285 mailaddr = NULL; | |
286 queueid = NULL; | |
136 | 287 authenticated = NULL; |
94 | 288 have_whites = false; |
289 only_whites = true; | |
167
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
290 want_spamassassin = false; |
94 | 291 memory = NULL; |
292 scanner = NULL; | |
163 | 293 assassin = NULL; |
94 | 294 content_suffix = NULL; |
295 content_message = NULL; | |
119 | 296 uribl_suffix = NULL; |
297 uribl_message = NULL; | |
94 | 298 content_host_ignore = NULL; |
299 } | |
300 } | |
301 | |
302 void mlfiPriv::get_fd() { | |
303 err = true; | |
304 fd = NULL_SOCKET; | |
305 int result = pthread_mutex_lock(&fd_pool_mutex); | |
306 if (!result) { | |
307 std::set<int>::iterator i; | |
308 i = fd_pool.begin(); | |
309 if (i != fd_pool.end()) { | |
310 // have at least one fd in the pool | |
311 err = false; | |
312 fd = *i; | |
313 fd_pool.erase(fd); | |
314 resolver_pool_size--; | |
315 pthread_mutex_unlock(&fd_pool_mutex); | |
316 } | |
317 else { | |
318 // pool is empty, get a new fd | |
319 pthread_mutex_unlock(&fd_pool_mutex); | |
320 fd = my_connect(); | |
321 err = (fd == NULL_SOCKET); | |
322 } | |
323 } | |
324 else { | |
325 // cannot lock the pool, just get a new fd | |
326 fd = my_connect(); | |
327 err = (fd == NULL_SOCKET); | |
328 } | |
329 } | |
330 | |
331 void mlfiPriv::return_fd() { | |
332 if (err) { | |
333 // this fd got a socket error, so close it, rather than returning it to the pool | |
334 my_disconnect(fd); | |
335 } | |
336 else { | |
337 int result = pthread_mutex_lock(&fd_pool_mutex); | |
338 if (!result) { | |
339 if ((resolver_sock_count > resolver_pool_size*5) || (resolver_pool_size < 5)) { | |
340 // return the fd to the pool | |
341 fd_pool.insert(fd); | |
342 resolver_pool_size++; | |
343 pthread_mutex_unlock(&fd_pool_mutex); | |
344 } | |
345 else { | |
346 // more than 20% of the open resolver sockets are in the pool, and the | |
347 // pool as at least 5 sockets. that is enough, so just close this one. | |
348 pthread_mutex_unlock(&fd_pool_mutex); | |
349 my_disconnect(fd); | |
350 } | |
351 } | |
352 else { | |
353 // could not lock the pool, so just close the fd | |
354 my_disconnect(fd); | |
355 } | |
356 } | |
357 } | |
358 | |
359 int mlfiPriv::my_write(char *buf, int len) { | |
360 if (err) return 0; | |
361 int rs = 0; | |
362 while (len) { | |
363 int ws = write(fd, buf, len); | |
364 if (ws > 0) { | |
365 rs += ws; | |
366 len -= ws; | |
367 buf += ws; | |
368 } | |
369 else { | |
370 // peer closed the socket! | |
371 rs = 0; | |
372 err = true; | |
373 break; | |
374 } | |
375 } | |
376 return rs; | |
377 } | |
378 | |
379 int mlfiPriv::my_read(char *buf, int len) { | |
380 if (err) return 0; | |
381 int rs = 0; | |
134 | 382 while (len) { |
94 | 383 int ws = read(fd, buf, len); |
384 if (ws > 0) { | |
385 rs += ws; | |
386 len -= ws; | |
387 buf += ws; | |
388 } | |
389 else { | |
390 // peer closed the socket! | |
391 rs = 0; | |
392 err = true; | |
393 break; | |
394 } | |
395 } | |
396 return rs; | |
397 } | |
398 | |
399 void mlfiPriv::need_content_filter(char *rcpt, CONTEXT &con) { | |
400 register_string(env_to, rcpt, &con); | |
401 if (!memory) { | |
402 // first recipient that needs content filtering sets all | |
403 // the content filtering parameters | |
117 | 404 memory = new recorder(this, con.get_html_tags(), con.get_content_tlds(), con.get_content_cctlds()); |
94 | 405 scanner = new url_scanner(memory); |
406 content_suffix = con.get_content_suffix(); | |
407 content_message = con.get_content_message(); | |
119 | 408 uribl_suffix = con.get_uribl_suffix(); |
409 uribl_message = con.get_uribl_message(); | |
94 | 410 content_host_ignore = &con.get_content_host_ignore(); |
411 } | |
412 } | |
413 | |
414 #define MLFIPRIV ((struct mlfiPriv *) smfi_getpriv(ctx)) | |
415 | |
416 | |
417 //////////////////////////////////////////////// | |
418 // syslog a message | |
419 // | |
420 void my_syslog(mlfiPriv *priv, char *text) { | |
421 char buf[maxlen]; | |
422 if (priv) { | |
423 snprintf(buf, sizeof(buf), "%s: %s", priv->queueid, text); | |
424 text = buf; | |
425 } | |
426 if (use_syslog) { | |
427 pthread_mutex_lock(&syslog_mutex); | |
428 if (!syslog_opened) { | |
429 openlog("dnsbl", LOG_PID, LOG_MAIL); | |
430 syslog_opened = true; | |
431 } | |
432 syslog(LOG_NOTICE, "%s", text); | |
433 pthread_mutex_unlock(&syslog_mutex); | |
434 } | |
435 else { | |
436 printf("%s \n", text); | |
437 } | |
438 } | |
439 | |
163 | 440 void my_syslog(mlfiPriv *priv, string text) { |
441 if (debug_syslog > 3) { | |
442 char buf[maxlen]; | |
443 strncpy(buf, text.c_str(), sizeof(buf)); | |
444 buf[maxlen-1] = '\0'; // ensure null termination | |
445 my_syslog(priv, buf); | |
446 } | |
447 } | |
448 | |
94 | 449 void my_syslog(char *text) { |
450 my_syslog(NULL, text); | |
451 } | |
452 | |
453 | |
454 //////////////////////////////////////////////// | |
455 // read a resolver request from the socket, process it, and | |
456 // write the result back to the socket. | |
457 | |
458 void process_resolver_requests(int socket); | |
459 void process_resolver_requests(int socket) { | |
460 #ifdef NS_MAXDNAME | |
461 char question[NS_MAXDNAME]; | |
462 #else | |
463 char question[1000]; | |
464 #endif | |
465 glommer glom; | |
466 | |
467 int maxq = sizeof(question); | |
468 while (true) { | |
469 // read a question | |
470 int rs = 0; | |
471 while (rs < maxq) { | |
472 int ns = read(socket, question+rs, maxq-rs); | |
473 if (ns > 0) { | |
474 rs += ns; | |
475 if (question[rs-1] == '\0') { | |
476 // last byte read was the null terminator, we are done | |
477 break; | |
478 } | |
479 } | |
480 else { | |
481 // peer closed the socket | |
482 #ifdef RESOLVER_DEBUG | |
483 my_syslog("process_resolver_requests() peer closed socket while reading question"); | |
484 #endif | |
485 shutdown(socket, SHUT_RDWR); | |
486 close(socket); | |
487 return; | |
488 } | |
489 } | |
490 question[rs-1] = '\0'; // ensure null termination | |
491 | |
492 // find the answer | |
493 #ifdef NS_PACKETSZ | |
494 #ifdef RESOLVER_DEBUG | |
495 char text[1000]; | |
496 snprintf(text, sizeof(text), "process_resolver_requests() has a question %s", question); | |
497 my_syslog(text); | |
498 #endif | |
150 | 499 if ((_res.options & RES_INIT) == 0) res_init(); |
500 _res.retry = 2; | |
501 _res.retrans = RES_TIMEOUT; | |
94 | 502 glom.length = res_search(question, ns_c_in, ns_t_a, glom.answer, sizeof(glom.answer)); |
503 if (glom.length < 0) glom.length = 0; // represent all errors as zero length answers | |
504 #else | |
505 glom.length = sizeof(glom.answer); | |
506 glom.answer = 0; | |
507 struct hostent *host = gethostbyname(question); | |
508 if (host && (host->h_addrtype == AF_INET)) { | |
509 memcpy(&glom.answer, host->h_addr, sizeof(glom.answer)); | |
510 } | |
511 #endif | |
512 | |
513 // write the answer | |
514 char *buf = (char *)&glom; | |
515 int len = glom.length + sizeof(glom.length); | |
516 #ifdef RESOLVER_DEBUG | |
517 snprintf(text, sizeof(text), "process_resolver_requests() writing answer length %d for total %d", glom.length, len); | |
518 my_syslog(text); | |
519 #endif | |
520 int ws = 0; | |
521 while (len > ws) { | |
522 int ns = write(socket, buf+ws, len-ws); | |
523 if (ns > 0) { | |
524 ws += ns; | |
525 } | |
526 else { | |
527 // peer closed the socket! | |
528 #ifdef RESOLVER_DEBUG | |
529 my_syslog("process_resolver_requests() peer closed socket while writing answer"); | |
530 #endif | |
531 shutdown(socket, SHUT_RDWR); | |
532 close(socket); | |
533 return; | |
534 } | |
535 } | |
536 } | |
537 } | |
538 | |
539 | |
540 //////////////////////////////////////////////// | |
541 // ask a dns question and get an A record answer - we don't try | |
542 // very hard, just using the default resolver retry settings. | |
543 // If we cannot get an answer, we just accept the mail. | |
544 // | |
545 // | |
546 int dns_interface(mlfiPriv &priv, char *question, bool maybe_ip, ns_map *nameservers); | |
547 int dns_interface(mlfiPriv &priv, char *question, bool maybe_ip, ns_map *nameservers) { | |
548 // this part can be done without locking the resolver mutex. Each | |
549 // milter thread is talking over its own socket to a separate resolver | |
550 // process, which does the actual dns resolution. | |
551 if (priv.err) return 0; // cannot ask more questions on this socket. | |
134 | 552 if (maybe_ip) { |
553 // might be a bare ip address, try this first to avoid dns lookups that may not be needed | |
554 in_addr ip; | |
555 if (inet_aton(question, &ip)) { | |
556 return (int)ip.s_addr; | |
557 } | |
558 } | |
559 int n = strlen(question); | |
560 if (question[n-1] == '.') { | |
561 priv.my_write(question, n+1); // write the question including the null terminator | |
562 } | |
563 else { | |
564 priv.my_write(question, n); // write the question | |
565 priv.my_write(".", 2); // and the fully qualified . terminator and null string terminator | |
566 } | |
94 | 567 glommer glom; |
568 char *buf = (char *)&glom; | |
569 priv.my_read(buf, sizeof(glom.length)); | |
570 buf += sizeof(glom.length); | |
571 #ifdef RESOLVER_DEBUG | |
572 char text[1000]; | |
573 snprintf(text, sizeof(text), "dns_interface() wrote question %s and has answer length %d", question, glom.length); | |
574 my_syslog(text); | |
575 #endif | |
576 if ((glom.length < 0) || (glom.length > sizeof(glom.answer))) { | |
577 priv.err = true; | |
578 return 0; // cannot process overlarge answers | |
579 } | |
580 priv.my_read(buf, glom.length); | |
581 | |
582 #ifdef NS_PACKETSZ | |
583 // now we need to lock the resolver mutex to keep the milter threads from | |
584 // stepping on each other while parsing the dns answer. | |
585 int ret_address = 0; | |
586 pthread_mutex_lock(&resolve_mutex); | |
587 if (glom.length > 0) { | |
588 // parse the answer | |
589 ns_msg handle; | |
590 ns_rr rr; | |
591 if (ns_initparse(glom.answer, glom.length, &handle) == 0) { | |
592 // look for ns names | |
593 if (nameservers) { | |
594 ns_map &ns = *nameservers; | |
595 int rrnum = 0; | |
596 while (ns_parserr(&handle, ns_s_ns, rrnum++, &rr) == 0) { | |
597 if (ns_rr_type(rr) == ns_t_ns) { | |
598 char nam[NS_MAXDNAME+1]; | |
599 char *n = nam; | |
600 const u_char *p = ns_rr_rdata(rr); | |
601 while (((n-nam) < NS_MAXDNAME) && ((p-glom.answer) < glom.length) && *p) { | |
602 size_t s = *(p++); | |
603 if (s > 191) { | |
604 // compression pointer | |
605 s = (s-192)*256 + *(p++); | |
606 if (s >= glom.length) break; // pointer outside bounds of answer | |
607 p = glom.answer + s; | |
608 s = *(p++); | |
609 } | |
610 if (s > 0) { | |
611 if ((n-nam) >= (NS_MAXDNAME-s)) break; // destination would overflow name buffer | |
612 if ((p-glom.answer) >= (glom.length-s)) break; // source outside bounds of answer | |
613 memcpy(n, p, s); | |
614 n += s; | |
615 p += s; | |
616 *(n++) = '.'; | |
617 } | |
618 } | |
619 if (n-nam) n--; // remove trailing . | |
620 *n = '\0'; // null terminate it | |
621 ns.add(nam, question); // ns host to lookup later | |
622 } | |
623 } | |
624 rrnum = 0; | |
625 while (ns_parserr(&handle, ns_s_ar, rrnum++, &rr) == 0) { | |
626 if (ns_rr_type(rr) == ns_t_a) { | |
627 char* nam = (char*)ns_rr_name(rr); | |
628 ns_mapper::iterator i = ns.ns_ip.find(nam); | |
629 if (i != ns.ns_ip.end()) { | |
630 // we want this ip address | |
631 int address; | |
632 memcpy(&address, ns_rr_rdata(rr), sizeof(address)); | |
633 ns.ns_ip[nam] = address; | |
634 } | |
635 } | |
636 } | |
637 } | |
638 int rrnum = 0; | |
639 while (ns_parserr(&handle, ns_s_an, rrnum++, &rr) == 0) { | |
640 if (ns_rr_type(rr) == ns_t_a) { | |
641 int address; | |
642 memcpy(&address, ns_rr_rdata(rr), sizeof(address)); | |
643 ret_address = address; | |
644 } | |
645 } | |
646 } | |
647 } | |
648 pthread_mutex_unlock(&resolve_mutex); | |
649 return ret_address; | |
650 #else | |
651 return glom.answer; | |
652 #endif | |
653 } | |
654 | |
655 | |
656 //////////////////////////////////////////////// | |
657 // check a single dnsbl | |
658 // | |
659 bool check_single(mlfiPriv &priv, int ip, char *suffix); | |
660 bool check_single(mlfiPriv &priv, int ip, char *suffix) { | |
661 // make a dns question | |
662 const u_char *src = (const u_char *)&ip; | |
663 if (src[0] == 127) return false; // don't do dns lookups on localhost | |
126 | 664 if (src[0] == 10) return false; // don't do dns lookups on rfc1918 space |
665 if ((src[0] == 192) && (src[1] == 168)) return false; | |
666 if ((src[0] == 172) && (16 <= src[1]) && (src[1] <= 31)) return false; | |
94 | 667 #ifdef NS_MAXDNAME |
668 char question[NS_MAXDNAME]; | |
669 #else | |
670 char question[1000]; | |
671 #endif | |
672 snprintf(question, sizeof(question), "%u.%u.%u.%u.%s.", src[3], src[2], src[1], src[0], suffix); | |
673 // ask the question, if we get an A record it implies a blacklisted ip address | |
674 return dns_interface(priv, question, false, NULL); | |
675 } | |
676 | |
677 | |
678 //////////////////////////////////////////////// | |
679 // check a single dnsbl | |
680 // | |
681 bool check_single(mlfiPriv &priv, int ip, DNSBL &bl); | |
682 bool check_single(mlfiPriv &priv, int ip, DNSBL &bl) { | |
683 return check_single(priv, ip, bl.suffix); | |
684 } | |
685 | |
686 | |
687 //////////////////////////////////////////////// | |
688 // check the dnsbls specified for this recipient | |
689 // | |
690 bool check_dnsbl(mlfiPriv &priv, dnsblp_list &dnsbll, DNSBLP &rejectlist); | |
691 bool check_dnsbl(mlfiPriv &priv, dnsblp_list &dnsbll, DNSBLP &rejectlist) { | |
692 for (dnsblp_list::iterator i=dnsbll.begin(); i!=dnsbll.end(); i++) { | |
693 DNSBLP dp = *i; // non null by construction | |
694 bool st; | |
695 map<DNSBLP, bool>::iterator f = priv.checked.find(dp); | |
696 if (f == priv.checked.end()) { | |
697 // have not checked this list yet | |
698 st = check_single(priv, priv.ip, *dp); | |
699 rejectlist = dp; | |
700 priv.checked[dp] = st; | |
701 } | |
702 else { | |
703 st = (*f).second; | |
704 rejectlist = (*f).first; | |
705 } | |
706 if (st) return st; | |
707 } | |
708 return false; | |
709 } | |
710 | |
711 | |
712 //////////////////////////////////////////////// | |
134 | 713 // lookup the domain name part of a hostname on the uribl |
117 | 714 // |
124 | 715 // if we find part of the hostname on the uribl, return |
716 // true and point found to the part of the hostname that we found. | |
717 // otherwise, return false and preserve the value of found. | |
718 // | |
719 bool uriblookup(mlfiPriv &priv ,char *hostname, char *top, char *&found) ; | |
720 bool uriblookup(mlfiPriv &priv, char *hostname, char *top, char *&found) { | |
117 | 721 // top is pointer to '.' char at end of base domain, or null for ip address form |
722 // so for hostname of www.fred.mydomain.co.uk | |
723 // top points to-----------------------^ | |
724 // and we end up looking at only mydomain.co.uk, ignoring the www.fred stuff | |
725 char buf[maxlen]; | |
726 if (top) { | |
727 // add one more component | |
728 *top = '\0'; | |
729 char *x = strrchr(hostname, '.'); | |
730 if (x) hostname = x+1; | |
731 *top = '.'; | |
732 } | |
131
df355d117199
uribl lookups fully qualified; allow two component host names
carl
parents:
129
diff
changeset
|
733 snprintf(buf, sizeof(buf), "%s.%s.", hostname, priv.uribl_suffix); |
119 | 734 if (dns_interface(priv, buf, false, NULL)) { |
117 | 735 if (debug_syslog > 2) { |
736 char tmp[maxlen]; | |
119 | 737 snprintf(tmp, sizeof(tmp), "found %s on %s", hostname, priv.uribl_suffix); |
117 | 738 my_syslog(tmp); |
739 } | |
124 | 740 found = hostname; |
119 | 741 return true; |
117 | 742 } |
743 return false; | |
744 } | |
745 | |
746 | |
747 //////////////////////////////////////////////// | |
124 | 748 // uribl checker |
749 // ------------- | |
750 // hostname MUST not have a trailing dot | |
751 // If tld, two level lookup. | |
752 // Else, look up three level domain. | |
753 // | |
754 // if we find part of the hostname on the uribl, return | |
755 // true and point found to the part of the hostname that we found. | |
756 // otherwise, return false and preserve the value of found. | |
757 // | |
758 bool check_uribl(mlfiPriv &priv, char *hostname, char *&found) ; | |
759 bool check_uribl(mlfiPriv &priv, char *hostname, char *&found) { | |
117 | 760 in_addr ip; |
761 if (inet_aton(hostname, &ip)) { | |
120 | 762 const u_char *src = (const u_char *)&ip.s_addr; |
128 | 763 if (src[0] == 127) return false; // don't do dns lookups on localhost |
764 if (src[0] == 10) return false; // don't do dns lookups on rfc1918 space | |
765 if ((src[0] == 192) && (src[1] == 168)) return false; | |
766 if ((src[0] == 172) && (16 <= src[1]) && (src[1] <= 31)) return false; | |
124 | 767 static char adr[sizeof "255.255.255.255"]; |
120 | 768 snprintf(adr, sizeof(adr), "%u.%u.%u.%u", src[3], src[2], src[1], src[0]); |
124 | 769 return (uriblookup(priv, adr, NULL, found)); |
117 | 770 } |
771 | |
772 char *top, *top2, *top3; | |
773 top = strrchr(hostname, '.'); | |
774 if (top) { | |
775 *top = '\0'; | |
776 top2 = strrchr(hostname, '.'); | |
777 *top = '.'; | |
778 | |
779 if (top2) { | |
780 string_set::iterator i = priv.memory->get_cctlds()->find(top2+1); | |
781 string_set::iterator x = priv.memory->get_cctlds()->end(); | |
782 // if we have a 2-level-cctld, just look at top three levels of the name | |
124 | 783 if (i != x) return uriblookup(priv, hostname, top2, found); |
117 | 784 |
785 *top2 = '\0'; | |
786 top3 = strrchr(hostname, '.'); | |
787 *top2 = '.'; | |
788 | |
789 // if we have more than 3 levels in the name, look at the top three levels of the name | |
124 | 790 if (top3 && uriblookup(priv, hostname, top2, found)) return true; |
117 | 791 // if that was not found, fall thru to looking at the top two levels |
792 } | |
793 // look at the top two levels of the name | |
124 | 794 return uriblookup(priv, hostname, top, found); |
117 | 795 } |
796 return false; | |
797 } | |
798 | |
799 | |
800 //////////////////////////////////////////////// | |
119 | 801 // check the hosts from the body against the content filter and uribl dnsbls |
94 | 802 // |
124 | 803 // |
804 bool check_hosts(mlfiPriv &priv, bool random, int limit, char *&msg, char *&host, int &ip, char *&found); | |
805 bool check_hosts(mlfiPriv &priv, bool random, int limit, char *&msg, char *&host, int &ip, char *&found) { | |
806 found = NULL; // normally ip address style | |
119 | 807 if (!priv.content_suffix && !priv.uribl_suffix) return false; // nothing to check |
94 | 808 CONFIG &dc = *priv.pc; |
809 string_set &hosts = priv.memory->get_hosts(); | |
810 string_set &ignore = *priv.content_host_ignore; | |
811 | |
812 int count = 0; | |
813 int cnt = hosts.size(); // number of hosts we could look at | |
814 int_set ips; | |
815 ns_map nameservers; | |
816 for (string_set::iterator i=hosts.begin(); i!=hosts.end(); i++) { | |
817 host = *i; // a reference into hosts, which will live until this smtp transaction is closed | |
818 | |
819 // don't bother looking up hosts on the ignore list | |
820 string_set::iterator j = ignore.find(host); | |
821 if (j != ignore.end()) continue; | |
822 | |
823 // try to only look at limit/cnt fraction of the available cnt host names in random mode | |
824 if ((cnt > limit) && (limit > 0) && random) { | |
825 int r = rand() % cnt; | |
826 if (r >= limit) { | |
827 if (debug_syslog > 2) { | |
828 char buf[maxlen]; | |
829 snprintf(buf, sizeof(buf), "host %s skipped", host); | |
830 my_syslog(&priv, buf); | |
831 } | |
832 continue; | |
833 } | |
834 } | |
835 count++; | |
836 ip = dns_interface(priv, host, true, &nameservers); | |
837 if (debug_syslog > 2) { | |
838 char buf[maxlen]; | |
839 if (ip) { | |
840 char adr[sizeof "255.255.255.255"]; | |
841 adr[0] = '\0'; | |
842 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); | |
843 snprintf(buf, sizeof(buf), "host %s found at %s", host, adr); | |
844 } | |
845 else { | |
846 snprintf(buf, sizeof(buf), "host %s not found", host); | |
847 } | |
848 my_syslog(&priv, buf); | |
849 } | |
850 if (ip) { | |
851 int_set::iterator i = ips.find(ip); | |
852 if (i == ips.end()) { | |
117 | 853 // we haven't looked this up yet |
94 | 854 ips.insert(ip); |
124 | 855 // check dnsbl style list |
856 if (priv.content_suffix && check_single(priv, ip, priv.content_suffix)) { | |
119 | 857 msg = priv.content_message; |
858 return true; | |
859 } | |
124 | 860 // Check uribl & surbl style list |
861 if (priv.uribl_suffix && check_uribl(priv, host, found)) { | |
119 | 862 msg = priv.uribl_message; |
863 return true; | |
864 } | |
94 | 865 } |
866 } | |
867 } | |
868 limit *= 4; // allow average of 3 ns per host name | |
869 for (ns_mapper::iterator i=nameservers.ns_ip.begin(); i!=nameservers.ns_ip.end(); i++) { | |
870 count++; | |
119 | 871 if ((count > limit) && (limit > 0)) return false; // too many name servers to check them all |
94 | 872 host = (*i).first; // a transient reference that needs to be replaced before we return it |
873 ip = (*i).second; | |
874 if (!ip) ip = dns_interface(priv, host, false, NULL); | |
875 if (debug_syslog > 2) { | |
876 char buf[maxlen]; | |
877 if (ip) { | |
878 char adr[sizeof "255.255.255.255"]; | |
879 adr[0] = '\0'; | |
880 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); | |
881 snprintf(buf, sizeof(buf), "ns %s found at %s", host, adr); | |
882 } | |
883 else { | |
884 snprintf(buf, sizeof(buf), "ns %s not found", host); | |
885 } | |
886 my_syslog(&priv, buf); | |
887 } | |
888 if (ip) { | |
889 int_set::iterator i = ips.find(ip); | |
890 if (i == ips.end()) { | |
891 ips.insert(ip); | |
119 | 892 if (check_single(priv, ip, priv.content_suffix)) { |
893 msg = priv.content_message; | |
94 | 894 string_map::iterator j = nameservers.ns_host.find(host); |
895 if (j != nameservers.ns_host.end()) { | |
896 char *refer = (*j).second; | |
897 char buf[maxlen]; | |
898 snprintf(buf, sizeof(buf), "%s with nameserver %s", refer, host); | |
899 host = register_string(hosts, buf); // put a copy into hosts, and return that reference | |
900 } | |
901 else { | |
902 host = register_string(hosts, host); // put a copy into hosts, and return that reference | |
903 } | |
904 return true; | |
905 } | |
906 } | |
907 } | |
908 } | |
909 return false; | |
910 } | |
911 | |
127 | 912 |
94 | 913 //////////////////////////////////////////////// |
127 | 914 // |
915 // this email address is passed in from sendmail, and will normally be | |
916 // enclosed in <>. I think older versions of sendmail supplied the <> | |
917 // wrapper if the mail client did not, but the current version does not do | |
918 // that. So the <> wrapper is now optional. It may have mixed case, just | |
919 // as the mail client sent it. We dup the string and convert the duplicate | |
920 // to lower case. | |
94 | 921 // |
922 char *to_lower_string(char *email); | |
923 char *to_lower_string(char *email) { | |
127 | 924 int n = strlen(email); |
925 if (*email == '<') { | |
926 // assume it also ends with > | |
927 n -= 2; | |
928 if (n < 1) return strdup(email); // return "<>" | |
929 email++; | |
930 } | |
931 char *key = strdup(email); | |
94 | 932 key[n] = '\0'; |
933 for (int i=0; i<n; i++) key[i] = tolower(key[i]); | |
934 return key; | |
935 } | |
936 | |
937 | |
938 //////////////////////////////////////////////// | |
939 // start of sendmail milter interfaces | |
940 // | |
941 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr) | |
942 { | |
943 // allocate some private memory | |
944 mlfiPriv *priv = new mlfiPriv; | |
945 if (hostaddr->sa_family == AF_INET) { | |
946 priv->ip = ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr; | |
947 } | |
948 // save the private data | |
949 smfi_setpriv(ctx, (void*)priv); | |
950 | |
951 // continue processing | |
952 return SMFIS_CONTINUE; | |
953 } | |
954 | |
163 | 955 sfsistat mlfi_helo(SMFICTX * ctx, char *helohost) |
956 { | |
957 mlfiPriv &priv = *MLFIPRIV; | |
958 priv.helo = strdup(helohost); | |
959 return SMFIS_CONTINUE; | |
960 } | |
961 | |
94 | 962 sfsistat mlfi_envfrom(SMFICTX *ctx, char **from) |
963 { | |
964 mlfiPriv &priv = *MLFIPRIV; | |
965 priv.mailaddr = to_lower_string(from[0]); | |
163 | 966 priv.queueid = strdup(smfi_getsymval(ctx, "i")); |
136 | 967 priv.authenticated = smfi_getsymval(ctx, "{auth_authen}"); |
968 if (priv.authenticated) priv.authenticated = strdup(priv.authenticated); | |
167
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
969 if (spamc != spamc_empty) { |
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
970 priv.assassin = new SpamAssassin(&priv, priv.ip, priv.helo, priv.mailaddr, priv.queueid); |
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
971 } |
94 | 972 return SMFIS_CONTINUE; |
973 } | |
974 | |
975 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **rcpt) | |
976 { | |
977 DNSBLP rejectlist = NULL; // list that caused the reject | |
978 mlfiPriv &priv = *MLFIPRIV; | |
979 CONFIG &dc = *priv.pc; | |
152 | 980 char *rcptaddr = rcpt[0]; |
981 char *loto = to_lower_string(rcptaddr); | |
167
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
982 if (priv.assassin) priv.assassin->mlfi_envrcpt(ctx, loto); |
152 | 983 // priv.mailaddr sending original message to loto |
984 CONTEXT &con = *(dc.find_context(loto)->find_context(priv.mailaddr)); | |
985 VERIFYP ver = con.find_verify(loto); | |
986 char *fromvalue = con.find_from(priv.mailaddr); | |
987 // loto sending a reply back to priv.mailaddr | |
988 CONTEXT &con2 = *(dc.find_context(priv.mailaddr)->find_context(loto)); | |
989 char *replyvalue = con2.find_from(loto); | |
94 | 990 if (debug_syslog > 1) { |
991 char buf[maxlen]; | |
992 char msg[maxlen]; | |
156 | 993 snprintf(msg, sizeof(msg), "from <%s> to <%s> using context %s state %s reply state %s", priv.mailaddr, loto, con.get_full_name(buf,maxlen), fromvalue, replyvalue); |
94 | 994 my_syslog(&priv, msg); |
995 } | |
996 free(loto); | |
997 status st; | |
152 | 998 if (replyvalue == token_black) { |
999 smfi_setreply(ctx, "550", "5.7.1", "recipient can not reply due to blacklisting"); | |
1000 return SMFIS_REJECT; | |
1001 } | |
94 | 1002 if (priv.authenticated) { |
136 | 1003 int c = incr_rcpt_count(priv.authenticated); |
1004 int l = dc.default_context->find_rate(priv.authenticated); | |
142 | 1005 if (debug_syslog > 1) { |
1006 char buf[maxlen]; | |
1007 char msg[maxlen]; | |
1008 snprintf(msg, sizeof(msg), "authenticated id %s (%d recipients, %d limit)", priv.authenticated, c, l); | |
1009 my_syslog(&priv, msg); | |
1010 } | |
136 | 1011 if (c > l) { |
1012 smfi_setreply(ctx, "550", "5.7.1", "recipient rate limit exceeded"); | |
1013 return SMFIS_REJECT; | |
1014 } | |
142 | 1015 st = white; |
94 | 1016 } |
1017 else if (fromvalue == token_black) { | |
1018 st = black; | |
1019 } | |
1020 else if (fromvalue == token_white) { | |
1021 st = white; | |
1022 } | |
1023 else { | |
1024 // check the dns based lists | |
1025 st = (check_dnsbl(priv, con.get_dnsbl_list(), rejectlist)) ? reject : oksofar; | |
1026 } | |
1027 if (st == reject) { | |
1028 // reject the recipient based on some dnsbl | |
1029 char adr[sizeof "255.255.255.255"]; | |
1030 adr[0] = '\0'; | |
1031 inet_ntop(AF_INET, (const u_char *)&priv.ip, adr, sizeof(adr)); | |
1032 char buf[maxlen]; | |
1033 snprintf(buf, sizeof(buf), rejectlist->message, adr, adr); | |
1034 smfi_setreply(ctx, "550", "5.7.1", buf); | |
1035 return SMFIS_REJECT; | |
1036 } | |
1037 if (st == black) { | |
1038 // reject the recipient based on blacklisting either from or to | |
1039 smfi_setreply(ctx, "550", "5.7.1", "no such user"); | |
1040 return SMFIS_REJECT; | |
1041 } | |
1042 if (ver && (st != white)) { | |
1043 // try to verify this from/to pair of addresses since it is not explicitly whitelisted | |
1044 char *loto = to_lower_string(rcptaddr); | |
1045 bool rc = ver->ok(priv.mailaddr, loto); | |
1046 free(loto); | |
1047 if (!rc) { | |
1048 smfi_setreply(ctx, "550", "5.7.1", "no such user"); | |
1049 return SMFIS_REJECT; | |
1050 } | |
1051 } | |
153 | 1052 // we will accept the recipient, but add an auto-whitelist entry |
1053 // if needed to ensure we can accept replies | |
162 | 1054 loto = to_lower_string(rcptaddr); |
1055 WHITELISTERP w = con2.find_autowhite(loto, priv.mailaddr); | |
153 | 1056 if (w) { |
156 | 1057 if (debug_syslog > 1) { |
1058 char buf[maxlen]; | |
1059 char msg[maxlen]; | |
159 | 1060 snprintf(msg, sizeof(msg), "whitelist reply from <%s> in context %s", loto, con2.get_full_name(buf,maxlen)); |
156 | 1061 my_syslog(&priv, msg); |
1062 } | |
154 | 1063 w->sent(loto); // don't free it, the whitelister takes ownership of the string |
153 | 1064 } |
162 | 1065 else { |
1066 free(loto); | |
1067 } | |
94 | 1068 // accept the recipient |
1069 if (!con.get_content_filtering()) st = white; | |
1070 if (st == oksofar) { | |
1071 // but remember the non-whites | |
1072 priv.need_content_filter(rcptaddr, con); | |
1073 priv.only_whites = false; | |
167
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
1074 priv.want_spamassassin |= (priv.assassin) && // have spam assassin available and |
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
1075 (con.get_spamassassin_limit() != 0); // want to use it with a non-zero score |
94 | 1076 } |
1077 if (st == white) { | |
1078 priv.have_whites = true; | |
1079 } | |
1080 return SMFIS_CONTINUE; | |
1081 } | |
1082 | |
163 | 1083 sfsistat mlfi_header(SMFICTX* ctx, char* headerf, char* headerv) |
1084 { | |
1085 mlfiPriv &priv = *MLFIPRIV; | |
167
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
1086 if (priv.authenticated) return SMFIS_CONTINUE; |
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
1087 if (priv.only_whites) return SMFIS_CONTINUE; |
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
1088 if (priv.want_spamassassin) priv.assassin->mlfi_header(headerf, headerv); |
163 | 1089 return SMFIS_CONTINUE; |
1090 } | |
1091 | |
1092 sfsistat mlfi_eoh(SMFICTX* ctx) | |
1093 { | |
1094 mlfiPriv &priv = *MLFIPRIV; | |
167
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
1095 if (priv.authenticated) return SMFIS_CONTINUE; |
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
1096 if (priv.only_whites) return SMFIS_CONTINUE; |
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
1097 if (priv.want_spamassassin) priv.assassin->mlfi_eoh(); |
163 | 1098 return SMFIS_CONTINUE; |
1099 } | |
1100 | |
94 | 1101 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len) |
1102 { | |
1103 mlfiPriv &priv = *MLFIPRIV; | |
167
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
1104 if (priv.authenticated) return SMFIS_CONTINUE; |
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
1105 if (priv.only_whites) return SMFIS_CONTINUE; |
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
1106 if (priv.want_spamassassin) priv.assassin->mlfi_body(data, len); |
94 | 1107 priv.scanner->scan(data, len); |
1108 return SMFIS_CONTINUE; | |
1109 } | |
1110 | |
1111 sfsistat mlfi_eom(SMFICTX *ctx) | |
1112 { | |
1113 sfsistat rc; | |
1114 mlfiPriv &priv = *MLFIPRIV; | |
1115 CONFIG &dc = *priv.pc; | |
1116 char *host = NULL; | |
1117 int ip; | |
1118 status st; | |
1119 // process end of message | |
1120 if (priv.authenticated || priv.only_whites) rc = SMFIS_CONTINUE; | |
1121 else { | |
167
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
1122 int score = (priv.want_spamassassin) ? priv.assassin->mlfi_eom() : 0; |
94 | 1123 // assert env_to not empty |
1124 char buf[maxlen]; | |
167
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
1125 string msg; |
94 | 1126 string_set alive; |
1127 bool random = false; | |
1128 int limit = 0; | |
1129 for (context_map::iterator i=priv.env_to.begin(); i!=priv.env_to.end(); i++) { | |
1130 char *rcpt = (*i).first; | |
1131 CONTEXT &con = *((*i).second); | |
163 | 1132 if (!con.acceptable_content(*priv.memory, score, msg)) { |
1133 // bad html tags or excessive hosts or high spam assassin score | |
94 | 1134 smfi_delrcpt(ctx, rcpt); |
1135 } | |
1136 else { | |
1137 alive.insert(rcpt); | |
1138 random |= con.get_host_random(); | |
1139 limit = max(limit, con.get_host_limit()); | |
1140 } | |
1141 } | |
1142 bool rejecting = alive.empty(); // if alive is empty, we must have set msg above in acceptable_content() | |
1143 if (!rejecting) { | |
124 | 1144 char *fmt, *found; |
1145 if (check_hosts(priv, random, limit, fmt, host, ip, found)) { | |
1146 if (found) { | |
1147 // uribl style | |
1148 snprintf(buf, sizeof(buf), fmt, host, found); | |
1149 } | |
1150 else { | |
1151 // dnsbl style | |
1152 char adr[sizeof "255.255.255.255"]; | |
1153 adr[0] = '\0'; | |
1154 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); | |
1155 snprintf(buf, sizeof(buf), fmt, host, adr); | |
1156 } | |
167
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
1157 msg = string(buf); |
94 | 1158 rejecting = true; |
1159 } | |
1160 } | |
1161 if (!rejecting) { | |
1162 rc = SMFIS_CONTINUE; | |
1163 } | |
1164 else if (!priv.have_whites) { | |
1165 // can reject the entire message | |
167
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
1166 snprintf(buf, sizeof(buf), "%s", msg.c_str()); |
9b129ed78d7d
actually use spamassassin result, allow build without spam assassin, only call it if some recipient needs it.
carl
parents:
165
diff
changeset
|
1167 smfi_setreply(ctx, "550", "5.7.1", buf); |
94 | 1168 rc = SMFIS_REJECT; |
1169 } | |
1170 else { | |
1171 // need to accept it but remove the recipients that don't want it | |
1172 for (string_set::iterator i=alive.begin(); i!=alive.end(); i++) { | |
1173 char *rcpt = *i; | |
1174 smfi_delrcpt(ctx, rcpt); | |
1175 } | |
1176 rc = SMFIS_CONTINUE; | |
1177 } | |
1178 } | |
1179 // reset for a new message on the same connection | |
1180 mlfi_abort(ctx); | |
1181 return rc; | |
1182 } | |
1183 | |
1184 sfsistat mlfi_abort(SMFICTX *ctx) | |
1185 { | |
1186 mlfiPriv &priv = *MLFIPRIV; | |
1187 priv.reset(); | |
1188 return SMFIS_CONTINUE; | |
1189 } | |
1190 | |
1191 sfsistat mlfi_close(SMFICTX *ctx) | |
1192 { | |
1193 mlfiPriv *priv = MLFIPRIV; | |
1194 if (!priv) return SMFIS_CONTINUE; | |
1195 delete priv; | |
1196 smfi_setpriv(ctx, NULL); | |
1197 return SMFIS_CONTINUE; | |
1198 } | |
1199 | |
1200 struct smfiDesc smfilter = | |
1201 { | |
1202 "DNSBL", // filter name | |
1203 SMFI_VERSION, // version code -- do not change | |
1204 SMFIF_DELRCPT, // flags | |
1205 mlfi_connect, // connection info filter | |
163 | 1206 mlfi_helo, // SMTP HELO command filter |
94 | 1207 mlfi_envfrom, // envelope sender filter |
1208 mlfi_envrcpt, // envelope recipient filter | |
163 | 1209 mlfi_header, // header filter |
1210 mlfi_eoh, // end of header | |
94 | 1211 mlfi_body, // body block filter |
1212 mlfi_eom, // end of message | |
1213 mlfi_abort, // message aborted | |
1214 mlfi_close, // connection cleanup | |
1215 }; | |
1216 | |
1217 | |
1218 //////////////////////////////////////////////// | |
1219 // reload the config | |
1220 // | |
1221 CONFIG* new_conf(); | |
1222 CONFIG* new_conf() { | |
1223 CONFIG *newc = new CONFIG; | |
1224 pthread_mutex_lock(&config_mutex); | |
1225 newc->generation = generation++; | |
1226 pthread_mutex_unlock(&config_mutex); | |
1227 if (debug_syslog) { | |
1228 char buf[maxlen]; | |
1229 snprintf(buf, sizeof(buf), "loading configuration generation %d", newc->generation); | |
1230 my_syslog(buf); | |
1231 } | |
1232 if (load_conf(*newc, "dnsbl.conf")) { | |
1233 newc->load_time = time(NULL); | |
1234 return newc; | |
1235 } | |
1236 delete newc; | |
1237 return NULL; | |
1238 } | |
1239 | |
1240 | |
1241 //////////////////////////////////////////////// | |
1242 // thread to watch the old config files for changes | |
146 | 1243 // and reload when needed. |
136 | 1244 // we also clear the SMTP AUTH recipient counts hourly |
94 | 1245 // |
163 | 1246 extern "C" {void* config_loader(void *arg);} |
94 | 1247 void* config_loader(void *arg) { |
136 | 1248 int loop = 0; |
94 | 1249 while (loader_run) { |
1250 sleep(180); // look for modifications every 3 minutes | |
1251 if (!loader_run) break; | |
136 | 1252 loop++; |
1253 if (loop == 20) { | |
1254 // three minutes thru each loop, 20 loops per hour | |
1255 // clear the recipient counts | |
1256 pthread_mutex_lock(&rate_mutex); | |
138 | 1257 for (rcpt_rates::iterator i=rcpt_counts.begin(); i!=rcpt_counts.end(); i++) { |
140 | 1258 (*i).second = 0; |
138 | 1259 } |
136 | 1260 pthread_mutex_unlock(&rate_mutex); |
137 | 1261 loop = 0; |
136 | 1262 } |
94 | 1263 CONFIG &dc = *config; |
1264 time_t then = dc.load_time; | |
1265 struct stat st; | |
1266 bool reload = false; | |
1267 for (string_set::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { | |
1268 char *fn = *i; | |
1269 if (stat(fn, &st)) reload = true; // file disappeared | |
1270 else if (st.st_mtime > then) reload = true; // file modified | |
1271 if (reload) break; | |
1272 } | |
1273 if (reload) { | |
1274 CONFIG *newc = new_conf(); | |
1275 if (newc) { | |
1276 // replace the global config pointer | |
1277 pthread_mutex_lock(&config_mutex); | |
146 | 1278 CONFIG *pc = config; |
1279 bool last = pc && (!pc->reference_count); | |
94 | 1280 config = newc; |
1281 pthread_mutex_unlock(&config_mutex); | |
163 | 1282 if (last) delete pc; // there were no references to this config |
94 | 1283 } |
1284 else { | |
1285 // failed to load new config | |
1286 my_syslog("failed to load new configuration"); | |
1287 system("echo 'failed to load new dnsbl configuration from /etc/dnsbl' | mail -s 'error in /etc/dnsbl configuration' root"); | |
1288 // update the load time on the current config to prevent complaining every 3 minutes | |
1289 dc.load_time = time(NULL); | |
1290 } | |
1291 } | |
1292 } | |
1293 return NULL; | |
1294 } | |
1295 | |
1296 | |
1297 void usage(char *prog); | |
1298 void usage(char *prog) | |
1299 { | |
1300 fprintf(stderr, "Usage: %s [-d [level]] [-c] [-s] [-e from|to] -r port -p sm-sock-addr [-t timeout]\n", prog); | |
1301 fprintf(stderr, "where port is for the connection to our own dns resolver processes\n"); | |
1302 fprintf(stderr, " and should be local-domain-socket-file-name\n"); | |
1303 fprintf(stderr, "where sm-sock-addr is for the connection to sendmail\n"); | |
1304 fprintf(stderr, " and should be one of\n"); | |
1305 fprintf(stderr, " inet:port@ip-address\n"); | |
1306 fprintf(stderr, " local:local-domain-socket-file-name\n"); | |
1307 fprintf(stderr, "-c will load and dump the config to stdout\n"); | |
1308 fprintf(stderr, "-s will stress test the config loading code by repeating the load/free cycle\n"); | |
1309 fprintf(stderr, " in an infinte loop.\n"); | |
1310 fprintf(stderr, "-d will set the syslog message level, currently 0 to 3\n"); | |
1311 fprintf(stderr, "-e will print the results of looking up the from and to addresses in the\n"); | |
1312 fprintf(stderr, " current config. The | character is used to separate the from and to\n"); | |
1313 fprintf(stderr, " addresses in the argument to the -e switch\n"); | |
1314 } | |
1315 | |
1316 | |
1317 | |
1318 void setup_socket(char *sock); | |
1319 void setup_socket(char *sock) { | |
1320 unlink(sock); | |
1321 } | |
1322 | |
1323 | |
1324 /* | |
1325 * The signal handler function -- only gets called when a SIGCHLD | |
1326 * is received, ie when a child terminates | |
1327 */ | |
1328 void sig_chld(int signo) | |
1329 { | |
1330 int status; | |
1331 /* Wait for any child without blocking */ | |
1332 while (waitpid(-1, &status, WNOHANG) > 0) { | |
1333 // ignore child exit status, we only do this to cleanup zombies | |
1334 } | |
1335 } | |
1336 | |
1337 | |
1338 int main(int argc, char**argv) | |
1339 { | |
1340 token_init(); | |
1341 bool check = false; | |
1342 bool stress = false; | |
1343 bool setconn = false; | |
1344 bool setreso = false; | |
1345 char *email = NULL; | |
1346 int c; | |
1347 const char *args = "r:p:t:e:d:chs"; | |
1348 extern char *optarg; | |
1349 | |
1350 // Process command line options | |
1351 while ((c = getopt(argc, argv, args)) != -1) { | |
1352 switch (c) { | |
1353 case 'r': | |
1354 if (optarg == NULL || *optarg == '\0') { | |
1355 fprintf(stderr, "Illegal resolver socket: %s\n", optarg); | |
1356 exit(EX_USAGE); | |
1357 } | |
1358 resolver_port = strdup(optarg); | |
1359 setup_socket(resolver_port); | |
1360 setreso = true; | |
1361 break; | |
1362 | |
1363 case 'p': | |
1364 if (optarg == NULL || *optarg == '\0') { | |
1365 fprintf(stderr, "Illegal sendmail socket: %s\n", optarg); | |
1366 exit(EX_USAGE); | |
1367 } | |
1368 if (smfi_setconn(optarg) == MI_FAILURE) { | |
1369 fprintf(stderr, "smfi_setconn failed\n"); | |
1370 exit(EX_SOFTWARE); | |
1371 } | |
1372 if (strncasecmp(optarg, "unix:", 5) == 0) setup_socket(optarg + 5); | |
1373 else if (strncasecmp(optarg, "local:", 6) == 0) setup_socket(optarg + 6); | |
1374 setconn = true; | |
1375 break; | |
1376 | |
1377 case 't': | |
1378 if (optarg == NULL || *optarg == '\0') { | |
1379 fprintf(stderr, "Illegal timeout: %s\n", optarg); | |
1380 exit(EX_USAGE); | |
1381 } | |
1382 if (smfi_settimeout(atoi(optarg)) == MI_FAILURE) { | |
1383 fprintf(stderr, "smfi_settimeout failed\n"); | |
1384 exit(EX_SOFTWARE); | |
1385 } | |
1386 break; | |
1387 | |
1388 case 'e': | |
1389 if (email) free(email); | |
1390 email = strdup(optarg); | |
1391 break; | |
1392 | |
1393 case 'c': | |
1394 check = true; | |
1395 break; | |
1396 | |
1397 case 's': | |
1398 stress = true; | |
1399 break; | |
1400 | |
1401 case 'd': | |
1402 if (optarg == NULL || *optarg == '\0') debug_syslog = 1; | |
1403 else debug_syslog = atoi(optarg); | |
1404 break; | |
1405 | |
1406 case 'h': | |
1407 default: | |
1408 usage(argv[0]); | |
1409 exit(EX_USAGE); | |
1410 } | |
1411 } | |
1412 | |
1413 if (check) { | |
1414 use_syslog = false; | |
1415 debug_syslog = 10; | |
1416 CONFIG *conf = new_conf(); | |
1417 if (conf) { | |
1418 conf->dump(); | |
1419 delete conf; | |
164 | 1420 clear_strings(); // for valgrind checking |
94 | 1421 return 0; |
1422 } | |
1423 else { | |
1424 return 1; // config failed to load | |
1425 } | |
1426 } | |
1427 | |
1428 if (stress) { | |
1429 fprintf(stdout, "stress testing\n"); | |
1430 while (1) { | |
1431 for (int i=0; i<10; i++) { | |
1432 CONFIG *conf = new_conf(); | |
1433 if (conf) delete conf; | |
1434 } | |
1435 fprintf(stdout, "."); | |
1436 fflush(stdout); | |
1437 sleep(1); | |
1438 } | |
1439 } | |
1440 | |
1441 if (email) { | |
1442 char *x = strchr(email, '|'); | |
1443 if (x) { | |
1444 *x = '\0'; | |
1445 char *from = strdup(email); | |
1446 char *to = strdup(x+1); | |
1447 use_syslog = false; | |
1448 CONFIG *conf = new_conf(); | |
1449 if (conf) { | |
1450 CONTEXTP con = conf->find_context(to); | |
1451 char buf[maxlen]; | |
1452 fprintf(stdout, "envelope to <%s> finds context %s\n", to, con->get_full_name(buf,maxlen)); | |
1453 CONTEXTP fc = con->find_context(from); | |
1454 fprintf(stdout, "envelope from <%s> finds context %s\n", from, fc->get_full_name(buf,maxlen)); | |
1455 char *st = fc->find_from(from); | |
1456 fprintf(stdout, "envelope from <%s> finds status %s\n", from, st); | |
1457 delete conf; | |
1458 } | |
1459 } | |
1460 return 0; | |
1461 } | |
1462 | |
1463 if (!setconn) { | |
1464 fprintf(stderr, "%s: Missing required -p argument\n", argv[0]); | |
1465 usage(argv[0]); | |
1466 exit(EX_USAGE); | |
1467 } | |
1468 | |
1469 if (!setreso) { | |
1470 fprintf(stderr, "%s: Missing required -r argument\n", argv[0]); | |
1471 usage(argv[0]); | |
1472 exit(EX_USAGE); | |
1473 } | |
1474 | |
1475 if (smfi_register(smfilter) == MI_FAILURE) { | |
1476 fprintf(stderr, "smfi_register failed\n"); | |
1477 exit(EX_UNAVAILABLE); | |
1478 } | |
1479 | |
1480 // switch to background mode | |
1481 if (daemon(1,0) < 0) { | |
1482 fprintf(stderr, "daemon() call failed\n"); | |
1483 exit(EX_UNAVAILABLE); | |
1484 } | |
1485 | |
1486 // write the pid | |
1487 const char *pidpath = "/var/run/dnsbl.pid"; | |
1488 unlink(pidpath); | |
1489 FILE *f = fopen(pidpath, "w"); | |
1490 if (f) { | |
1491 #ifdef linux | |
1492 // from a comment in the DCC source code: | |
1493 // Linux threads are broken. Signals given the | |
1494 // original process are delivered to only the | |
1495 // thread that happens to have that PID. The | |
1496 // sendmail libmilter thread that needs to hear | |
1497 // SIGINT and other signals does not, and that breaks | |
1498 // scripts that need to stop milters. | |
1499 // However, signaling the process group works. | |
1500 fprintf(f, "-%d\n", (u_int)getpgrp()); | |
1501 #else | |
1502 fprintf(f, "%d\n", (u_int)getpid()); | |
1503 #endif | |
1504 fclose(f); | |
1505 } | |
1506 | |
1507 // initialize the thread sync objects | |
1508 pthread_mutex_init(&config_mutex, 0); | |
1509 pthread_mutex_init(&syslog_mutex, 0); | |
1510 pthread_mutex_init(&resolve_mutex, 0); | |
1511 pthread_mutex_init(&fd_pool_mutex, 0); | |
153 | 1512 pthread_mutex_init(&verifier_mutex, 0); |
1513 pthread_mutex_init(&whitelister_mutex, 0); | |
94 | 1514 |
1515 // drop root privs | |
1516 struct passwd *pw = getpwnam("dnsbl"); | |
1517 if (pw) { | |
1518 if (setgid(pw->pw_gid) == -1) { | |
1519 my_syslog("failed to switch to group dnsbl"); | |
1520 } | |
1521 if (setuid(pw->pw_uid) == -1) { | |
1522 my_syslog("failed to switch to user dnsbl"); | |
1523 } | |
1524 } | |
1525 | |
1526 // load the initial config | |
1527 config = new_conf(); | |
1528 if (!config) { | |
1529 my_syslog("failed to load initial configuration, quitting"); | |
1530 exit(1); | |
1531 } | |
1532 | |
1533 // fork off the resolver listener process | |
1534 pid_t child = fork(); | |
1535 if (child < 0) { | |
1536 my_syslog("failed to create resolver listener process"); | |
1537 exit(0); | |
1538 } | |
1539 if (child == 0) { | |
1540 // we are the child - dns resolver listener process | |
1541 resolver_socket = socket(AF_UNIX, SOCK_STREAM, 0); | |
1542 if (resolver_socket < 0) { | |
1543 my_syslog("child failed to create resolver socket"); | |
1544 exit(0); // failed | |
1545 } | |
1546 sockaddr_un server; | |
1547 memset(&server, '\0', sizeof(server)); | |
1548 server.sun_family = AF_UNIX; | |
1549 strncpy(server.sun_path, resolver_port, sizeof(server.sun_path)-1); | |
1550 //try to bind the address to the socket. | |
1551 if (bind(resolver_socket, (sockaddr *)&server, sizeof(server)) < 0) { | |
1552 // bind failed | |
1553 shutdown(resolver_socket, SHUT_RDWR); | |
1554 close(resolver_socket); | |
1555 my_syslog("child failed to bind resolver socket"); | |
1556 exit(0); // failed | |
1557 } | |
1558 //listen on the socket. | |
1559 if (listen(resolver_socket, 10) < 0) { | |
1560 // listen failed | |
1561 shutdown(resolver_socket, SHUT_RDWR); | |
1562 close(resolver_socket); | |
1563 my_syslog("child failed to listen to resolver socket"); | |
1564 exit(0); // failed | |
1565 } | |
1566 // setup sigchld handler to prevent zombies | |
1567 struct sigaction act; | |
1568 act.sa_handler = sig_chld; // Assign sig_chld as our SIGCHLD handler | |
1569 sigemptyset(&act.sa_mask); // We don't want to block any other signals in this example | |
1570 act.sa_flags = SA_NOCLDSTOP; // only want children that have terminated | |
1571 if (sigaction(SIGCHLD, &act, NULL) < 0) { | |
1572 my_syslog("child failed to setup SIGCHLD handler"); | |
1573 exit(0); // failed | |
1574 } | |
1575 while (true) { | |
1576 sockaddr_un client; | |
1577 socklen_t clientlen = sizeof(client); | |
1578 int s = accept(resolver_socket, (sockaddr *)&client, &clientlen); | |
1579 if (s > 0) { | |
1580 // accept worked, it did not get cancelled before we could accept it | |
1581 // fork off a process to handle this connection | |
1582 int newchild = fork(); | |
1583 if (newchild == 0) { | |
1584 // this is the worker process | |
1585 // child does not need the listening socket | |
1586 close(resolver_socket); | |
1587 process_resolver_requests(s); | |
1588 exit(0); | |
1589 } | |
1590 else { | |
1591 // this is the parent | |
1592 // parent does not need the accepted socket | |
1593 close(s); | |
1594 } | |
1595 } | |
1596 } | |
1597 exit(0); // make sure we don't fall thru. | |
1598 } | |
1599 else { | |
1600 sleep(2); // allow child to get started | |
1601 } | |
1602 | |
1603 // only create threads after the fork() in daemon | |
1604 pthread_t tid; | |
1605 if (pthread_create(&tid, 0, config_loader, 0)) | |
1606 my_syslog("failed to create config loader thread"); | |
1607 if (pthread_detach(tid)) | |
1608 my_syslog("failed to detach config loader thread"); | |
153 | 1609 |
94 | 1610 if (pthread_create(&tid, 0, verify_closer, 0)) |
1611 my_syslog("failed to create verify closer thread"); | |
1612 if (pthread_detach(tid)) | |
1613 my_syslog("failed to detach verify closer thread"); | |
1614 | |
153 | 1615 if (pthread_create(&tid, 0, whitelister_writer, 0)) |
1616 my_syslog("failed to create autowhite writer thread"); | |
1617 if (pthread_detach(tid)) | |
1618 my_syslog("failed to detach autowhite writer thread"); | |
1619 | |
94 | 1620 time_t starting = time(NULL); |
1621 int rc = smfi_main(); | |
1622 if ((rc != MI_SUCCESS) && (time(NULL) > starting+5*60)) { | |
1623 my_syslog("trying to restart after smfi_main()"); | |
1624 loader_run = false; // eventually the config loader thread will terminate | |
1625 execvp(argv[0], argv); | |
1626 } | |
1627 exit((rc == MI_SUCCESS) ? 0 : EX_UNAVAILABLE); | |
1628 } | |
1629 |