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