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