Mercurial > dnsbl
annotate src/dnsbl.cpp @ 161:d384df37491f stable-6-02
allow manual updates to auto whitelist files
author | carl |
---|---|
date | Tue, 10 Jul 2007 14:09:23 -0700 |
parents | ea7c57a4a2d1 |
children | c4bce911c276 |
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. | |
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")); | |
152 | 953 char *rcptaddr = rcpt[0]; |
954 char *loto = to_lower_string(rcptaddr); | |
955 // priv.mailaddr sending original message to loto | |
956 CONTEXT &con = *(dc.find_context(loto)->find_context(priv.mailaddr)); | |
957 VERIFYP ver = con.find_verify(loto); | |
958 char *fromvalue = con.find_from(priv.mailaddr); | |
959 // loto sending a reply back to priv.mailaddr | |
960 CONTEXT &con2 = *(dc.find_context(priv.mailaddr)->find_context(loto)); | |
961 char *replyvalue = con2.find_from(loto); | |
94 | 962 if (debug_syslog > 1) { |
963 char buf[maxlen]; | |
964 char msg[maxlen]; | |
156 | 965 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 | 966 my_syslog(&priv, msg); |
967 } | |
968 free(loto); | |
969 status st; | |
152 | 970 if (replyvalue == token_black) { |
971 smfi_setreply(ctx, "550", "5.7.1", "recipient can not reply due to blacklisting"); | |
972 return SMFIS_REJECT; | |
973 } | |
94 | 974 if (priv.authenticated) { |
136 | 975 int c = incr_rcpt_count(priv.authenticated); |
976 int l = dc.default_context->find_rate(priv.authenticated); | |
142 | 977 if (debug_syslog > 1) { |
978 char buf[maxlen]; | |
979 char msg[maxlen]; | |
980 snprintf(msg, sizeof(msg), "authenticated id %s (%d recipients, %d limit)", priv.authenticated, c, l); | |
981 my_syslog(&priv, msg); | |
982 } | |
136 | 983 if (c > l) { |
984 smfi_setreply(ctx, "550", "5.7.1", "recipient rate limit exceeded"); | |
985 return SMFIS_REJECT; | |
986 } | |
142 | 987 st = white; |
94 | 988 } |
989 else if (fromvalue == token_black) { | |
990 st = black; | |
991 } | |
992 else if (fromvalue == token_white) { | |
993 st = white; | |
994 } | |
995 else { | |
996 // check the dns based lists | |
997 st = (check_dnsbl(priv, con.get_dnsbl_list(), rejectlist)) ? reject : oksofar; | |
998 } | |
999 if (st == reject) { | |
1000 // reject the recipient based on some dnsbl | |
1001 char adr[sizeof "255.255.255.255"]; | |
1002 adr[0] = '\0'; | |
1003 inet_ntop(AF_INET, (const u_char *)&priv.ip, adr, sizeof(adr)); | |
1004 char buf[maxlen]; | |
1005 snprintf(buf, sizeof(buf), rejectlist->message, adr, adr); | |
1006 smfi_setreply(ctx, "550", "5.7.1", buf); | |
1007 return SMFIS_REJECT; | |
1008 } | |
1009 if (st == black) { | |
1010 // reject the recipient based on blacklisting either from or to | |
1011 smfi_setreply(ctx, "550", "5.7.1", "no such user"); | |
1012 return SMFIS_REJECT; | |
1013 } | |
1014 if (ver && (st != white)) { | |
1015 // try to verify this from/to pair of addresses since it is not explicitly whitelisted | |
1016 char *loto = to_lower_string(rcptaddr); | |
1017 bool rc = ver->ok(priv.mailaddr, loto); | |
1018 free(loto); | |
1019 if (!rc) { | |
1020 smfi_setreply(ctx, "550", "5.7.1", "no such user"); | |
1021 return SMFIS_REJECT; | |
1022 } | |
1023 } | |
153 | 1024 // we will accept the recipient, but add an auto-whitelist entry |
1025 // if needed to ensure we can accept replies | |
1026 WHITELISTERP w = con2.find_autowhite(priv.mailaddr); | |
1027 if (w) { | |
1028 char *loto = to_lower_string(rcptaddr); | |
156 | 1029 if (debug_syslog > 1) { |
1030 char buf[maxlen]; | |
1031 char msg[maxlen]; | |
159 | 1032 snprintf(msg, sizeof(msg), "whitelist reply from <%s> in context %s", loto, con2.get_full_name(buf,maxlen)); |
156 | 1033 my_syslog(&priv, msg); |
1034 } | |
154 | 1035 w->sent(loto); // don't free it, the whitelister takes ownership of the string |
153 | 1036 } |
94 | 1037 // accept the recipient |
1038 if (!con.get_content_filtering()) st = white; | |
1039 if (st == oksofar) { | |
1040 // but remember the non-whites | |
1041 priv.need_content_filter(rcptaddr, con); | |
1042 priv.only_whites = false; | |
1043 } | |
1044 if (st == white) { | |
1045 priv.have_whites = true; | |
1046 } | |
1047 return SMFIS_CONTINUE; | |
1048 } | |
1049 | |
1050 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len) | |
1051 { | |
1052 mlfiPriv &priv = *MLFIPRIV; | |
1053 if (priv.authenticated) return SMFIS_CONTINUE; | |
1054 if (priv.only_whites) return SMFIS_CONTINUE; | |
1055 priv.scanner->scan(data, len); | |
1056 return SMFIS_CONTINUE; | |
1057 } | |
1058 | |
1059 sfsistat mlfi_eom(SMFICTX *ctx) | |
1060 { | |
1061 sfsistat rc; | |
1062 mlfiPriv &priv = *MLFIPRIV; | |
1063 CONFIG &dc = *priv.pc; | |
1064 char *host = NULL; | |
1065 int ip; | |
1066 status st; | |
1067 // process end of message | |
1068 if (priv.authenticated || priv.only_whites) rc = SMFIS_CONTINUE; | |
1069 else { | |
1070 // assert env_to not empty | |
1071 char buf[maxlen]; | |
1072 char *msg = NULL; | |
1073 string_set alive; | |
1074 bool random = false; | |
1075 int limit = 0; | |
1076 for (context_map::iterator i=priv.env_to.begin(); i!=priv.env_to.end(); i++) { | |
1077 char *rcpt = (*i).first; | |
1078 CONTEXT &con = *((*i).second); | |
1079 if (!con.acceptable_content(*priv.memory, msg)) { | |
1080 // bad html tags or excessive hosts | |
1081 smfi_delrcpt(ctx, rcpt); | |
1082 } | |
1083 else { | |
1084 alive.insert(rcpt); | |
1085 random |= con.get_host_random(); | |
1086 limit = max(limit, con.get_host_limit()); | |
1087 } | |
1088 } | |
1089 bool rejecting = alive.empty(); // if alive is empty, we must have set msg above in acceptable_content() | |
1090 if (!rejecting) { | |
124 | 1091 char *fmt, *found; |
1092 if (check_hosts(priv, random, limit, fmt, host, ip, found)) { | |
1093 if (found) { | |
1094 // uribl style | |
1095 snprintf(buf, sizeof(buf), fmt, host, found); | |
1096 } | |
1097 else { | |
1098 // dnsbl style | |
1099 char adr[sizeof "255.255.255.255"]; | |
1100 adr[0] = '\0'; | |
1101 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); | |
1102 snprintf(buf, sizeof(buf), fmt, host, adr); | |
1103 } | |
94 | 1104 msg = buf; |
1105 rejecting = true; | |
1106 } | |
1107 } | |
1108 if (!rejecting) { | |
1109 rc = SMFIS_CONTINUE; | |
1110 } | |
1111 else if (!priv.have_whites) { | |
1112 // can reject the entire message | |
1113 smfi_setreply(ctx, "550", "5.7.1", msg); | |
1114 rc = SMFIS_REJECT; | |
1115 } | |
1116 else { | |
1117 // need to accept it but remove the recipients that don't want it | |
1118 for (string_set::iterator i=alive.begin(); i!=alive.end(); i++) { | |
1119 char *rcpt = *i; | |
1120 smfi_delrcpt(ctx, rcpt); | |
1121 } | |
1122 rc = SMFIS_CONTINUE; | |
1123 } | |
1124 } | |
1125 // reset for a new message on the same connection | |
1126 mlfi_abort(ctx); | |
1127 return rc; | |
1128 } | |
1129 | |
1130 sfsistat mlfi_abort(SMFICTX *ctx) | |
1131 { | |
1132 mlfiPriv &priv = *MLFIPRIV; | |
1133 priv.reset(); | |
1134 return SMFIS_CONTINUE; | |
1135 } | |
1136 | |
1137 sfsistat mlfi_close(SMFICTX *ctx) | |
1138 { | |
1139 mlfiPriv *priv = MLFIPRIV; | |
1140 if (!priv) return SMFIS_CONTINUE; | |
1141 delete priv; | |
1142 smfi_setpriv(ctx, NULL); | |
1143 return SMFIS_CONTINUE; | |
1144 } | |
1145 | |
1146 struct smfiDesc smfilter = | |
1147 { | |
1148 "DNSBL", // filter name | |
1149 SMFI_VERSION, // version code -- do not change | |
1150 SMFIF_DELRCPT, // flags | |
1151 mlfi_connect, // connection info filter | |
1152 NULL, // SMTP HELO command filter | |
1153 mlfi_envfrom, // envelope sender filter | |
1154 mlfi_envrcpt, // envelope recipient filter | |
1155 NULL, // header filter | |
1156 NULL, // end of header | |
1157 mlfi_body, // body block filter | |
1158 mlfi_eom, // end of message | |
1159 mlfi_abort, // message aborted | |
1160 mlfi_close, // connection cleanup | |
1161 }; | |
1162 | |
1163 | |
1164 //////////////////////////////////////////////// | |
1165 // reload the config | |
1166 // | |
1167 CONFIG* new_conf(); | |
1168 CONFIG* new_conf() { | |
1169 CONFIG *newc = new CONFIG; | |
1170 pthread_mutex_lock(&config_mutex); | |
1171 newc->generation = generation++; | |
1172 pthread_mutex_unlock(&config_mutex); | |
1173 if (debug_syslog) { | |
1174 char buf[maxlen]; | |
1175 snprintf(buf, sizeof(buf), "loading configuration generation %d", newc->generation); | |
1176 my_syslog(buf); | |
1177 } | |
1178 if (load_conf(*newc, "dnsbl.conf")) { | |
1179 newc->load_time = time(NULL); | |
1180 return newc; | |
1181 } | |
1182 delete newc; | |
1183 return NULL; | |
1184 } | |
1185 | |
1186 | |
1187 //////////////////////////////////////////////// | |
1188 // thread to watch the old config files for changes | |
146 | 1189 // and reload when needed. |
136 | 1190 // we also clear the SMTP AUTH recipient counts hourly |
94 | 1191 // |
1192 void* config_loader(void *arg); | |
1193 void* config_loader(void *arg) { | |
136 | 1194 int loop = 0; |
94 | 1195 typedef set<CONFIG *> configp_set; |
1196 while (loader_run) { | |
1197 sleep(180); // look for modifications every 3 minutes | |
1198 if (!loader_run) break; | |
136 | 1199 loop++; |
1200 if (loop == 20) { | |
1201 // three minutes thru each loop, 20 loops per hour | |
1202 // clear the recipient counts | |
1203 pthread_mutex_lock(&rate_mutex); | |
138 | 1204 for (rcpt_rates::iterator i=rcpt_counts.begin(); i!=rcpt_counts.end(); i++) { |
140 | 1205 (*i).second = 0; |
138 | 1206 } |
136 | 1207 pthread_mutex_unlock(&rate_mutex); |
137 | 1208 loop = 0; |
136 | 1209 } |
94 | 1210 CONFIG &dc = *config; |
1211 time_t then = dc.load_time; | |
1212 struct stat st; | |
1213 bool reload = false; | |
1214 for (string_set::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { | |
1215 char *fn = *i; | |
1216 if (stat(fn, &st)) reload = true; // file disappeared | |
1217 else if (st.st_mtime > then) reload = true; // file modified | |
1218 if (reload) break; | |
1219 } | |
1220 if (reload) { | |
1221 CONFIG *newc = new_conf(); | |
1222 if (newc) { | |
1223 // replace the global config pointer | |
1224 pthread_mutex_lock(&config_mutex); | |
146 | 1225 CONFIG *pc = config; |
1226 bool last = pc && (!pc->reference_count); | |
94 | 1227 config = newc; |
1228 pthread_mutex_unlock(&config_mutex); | |
146 | 1229 if (last) delete pc; // there were no references to this config |
94 | 1230 } |
1231 else { | |
1232 // failed to load new config | |
1233 my_syslog("failed to load new configuration"); | |
1234 system("echo 'failed to load new dnsbl configuration from /etc/dnsbl' | mail -s 'error in /etc/dnsbl configuration' root"); | |
1235 // update the load time on the current config to prevent complaining every 3 minutes | |
1236 dc.load_time = time(NULL); | |
1237 } | |
1238 } | |
1239 } | |
1240 return NULL; | |
1241 } | |
1242 | |
1243 | |
1244 void usage(char *prog); | |
1245 void usage(char *prog) | |
1246 { | |
1247 fprintf(stderr, "Usage: %s [-d [level]] [-c] [-s] [-e from|to] -r port -p sm-sock-addr [-t timeout]\n", prog); | |
1248 fprintf(stderr, "where port is for the connection to our own dns resolver processes\n"); | |
1249 fprintf(stderr, " and should be local-domain-socket-file-name\n"); | |
1250 fprintf(stderr, "where sm-sock-addr is for the connection to sendmail\n"); | |
1251 fprintf(stderr, " and should be one of\n"); | |
1252 fprintf(stderr, " inet:port@ip-address\n"); | |
1253 fprintf(stderr, " local:local-domain-socket-file-name\n"); | |
1254 fprintf(stderr, "-c will load and dump the config to stdout\n"); | |
1255 fprintf(stderr, "-s will stress test the config loading code by repeating the load/free cycle\n"); | |
1256 fprintf(stderr, " in an infinte loop.\n"); | |
1257 fprintf(stderr, "-d will set the syslog message level, currently 0 to 3\n"); | |
1258 fprintf(stderr, "-e will print the results of looking up the from and to addresses in the\n"); | |
1259 fprintf(stderr, " current config. The | character is used to separate the from and to\n"); | |
1260 fprintf(stderr, " addresses in the argument to the -e switch\n"); | |
1261 } | |
1262 | |
1263 | |
1264 | |
1265 void setup_socket(char *sock); | |
1266 void setup_socket(char *sock) { | |
1267 unlink(sock); | |
1268 // sockaddr_un addr; | |
1269 // memset(&addr, '\0', sizeof addr); | |
1270 // addr.sun_family = AF_UNIX; | |
1271 // strncpy(addr.sun_path, sock, sizeof(addr.sun_path)-1); | |
1272 // int s = socket(AF_UNIX, SOCK_STREAM, 0); | |
1273 // bind(s, (sockaddr*)&addr, sizeof(addr)); | |
1274 // close(s); | |
1275 } | |
1276 | |
1277 | |
1278 /* | |
1279 * The signal handler function -- only gets called when a SIGCHLD | |
1280 * is received, ie when a child terminates | |
1281 */ | |
1282 void sig_chld(int signo) | |
1283 { | |
1284 int status; | |
1285 /* Wait for any child without blocking */ | |
1286 while (waitpid(-1, &status, WNOHANG) > 0) { | |
1287 // ignore child exit status, we only do this to cleanup zombies | |
1288 } | |
1289 } | |
1290 | |
1291 | |
1292 int main(int argc, char**argv) | |
1293 { | |
1294 token_init(); | |
1295 bool check = false; | |
1296 bool stress = false; | |
1297 bool setconn = false; | |
1298 bool setreso = false; | |
1299 char *email = NULL; | |
1300 int c; | |
1301 const char *args = "r:p:t:e:d:chs"; | |
1302 extern char *optarg; | |
1303 | |
1304 // Process command line options | |
1305 while ((c = getopt(argc, argv, args)) != -1) { | |
1306 switch (c) { | |
1307 case 'r': | |
1308 if (optarg == NULL || *optarg == '\0') { | |
1309 fprintf(stderr, "Illegal resolver socket: %s\n", optarg); | |
1310 exit(EX_USAGE); | |
1311 } | |
1312 resolver_port = strdup(optarg); | |
1313 setup_socket(resolver_port); | |
1314 setreso = true; | |
1315 break; | |
1316 | |
1317 case 'p': | |
1318 if (optarg == NULL || *optarg == '\0') { | |
1319 fprintf(stderr, "Illegal sendmail socket: %s\n", optarg); | |
1320 exit(EX_USAGE); | |
1321 } | |
1322 if (smfi_setconn(optarg) == MI_FAILURE) { | |
1323 fprintf(stderr, "smfi_setconn failed\n"); | |
1324 exit(EX_SOFTWARE); | |
1325 } | |
1326 if (strncasecmp(optarg, "unix:", 5) == 0) setup_socket(optarg + 5); | |
1327 else if (strncasecmp(optarg, "local:", 6) == 0) setup_socket(optarg + 6); | |
1328 setconn = true; | |
1329 break; | |
1330 | |
1331 case 't': | |
1332 if (optarg == NULL || *optarg == '\0') { | |
1333 fprintf(stderr, "Illegal timeout: %s\n", optarg); | |
1334 exit(EX_USAGE); | |
1335 } | |
1336 if (smfi_settimeout(atoi(optarg)) == MI_FAILURE) { | |
1337 fprintf(stderr, "smfi_settimeout failed\n"); | |
1338 exit(EX_SOFTWARE); | |
1339 } | |
1340 break; | |
1341 | |
1342 case 'e': | |
1343 if (email) free(email); | |
1344 email = strdup(optarg); | |
1345 break; | |
1346 | |
1347 case 'c': | |
1348 check = true; | |
1349 break; | |
1350 | |
1351 case 's': | |
1352 stress = true; | |
1353 break; | |
1354 | |
1355 case 'd': | |
1356 if (optarg == NULL || *optarg == '\0') debug_syslog = 1; | |
1357 else debug_syslog = atoi(optarg); | |
1358 break; | |
1359 | |
1360 case 'h': | |
1361 default: | |
1362 usage(argv[0]); | |
1363 exit(EX_USAGE); | |
1364 } | |
1365 } | |
1366 | |
1367 if (check) { | |
1368 use_syslog = false; | |
1369 debug_syslog = 10; | |
1370 CONFIG *conf = new_conf(); | |
1371 if (conf) { | |
1372 conf->dump(); | |
1373 delete conf; | |
1374 return 0; | |
1375 } | |
1376 else { | |
1377 return 1; // config failed to load | |
1378 } | |
1379 } | |
1380 | |
1381 if (stress) { | |
1382 fprintf(stdout, "stress testing\n"); | |
1383 while (1) { | |
1384 for (int i=0; i<10; i++) { | |
1385 CONFIG *conf = new_conf(); | |
1386 if (conf) delete conf; | |
1387 } | |
1388 fprintf(stdout, "."); | |
1389 fflush(stdout); | |
1390 sleep(1); | |
1391 } | |
1392 } | |
1393 | |
1394 if (email) { | |
1395 char *x = strchr(email, '|'); | |
1396 if (x) { | |
1397 *x = '\0'; | |
1398 char *from = strdup(email); | |
1399 char *to = strdup(x+1); | |
1400 use_syslog = false; | |
1401 CONFIG *conf = new_conf(); | |
1402 if (conf) { | |
1403 CONTEXTP con = conf->find_context(to); | |
1404 char buf[maxlen]; | |
1405 fprintf(stdout, "envelope to <%s> finds context %s\n", to, con->get_full_name(buf,maxlen)); | |
1406 CONTEXTP fc = con->find_context(from); | |
1407 fprintf(stdout, "envelope from <%s> finds context %s\n", from, fc->get_full_name(buf,maxlen)); | |
1408 char *st = fc->find_from(from); | |
1409 fprintf(stdout, "envelope from <%s> finds status %s\n", from, st); | |
1410 delete conf; | |
1411 } | |
1412 } | |
1413 return 0; | |
1414 } | |
1415 | |
1416 if (!setconn) { | |
1417 fprintf(stderr, "%s: Missing required -p argument\n", argv[0]); | |
1418 usage(argv[0]); | |
1419 exit(EX_USAGE); | |
1420 } | |
1421 | |
1422 if (!setreso) { | |
1423 fprintf(stderr, "%s: Missing required -r argument\n", argv[0]); | |
1424 usage(argv[0]); | |
1425 exit(EX_USAGE); | |
1426 } | |
1427 | |
1428 if (smfi_register(smfilter) == MI_FAILURE) { | |
1429 fprintf(stderr, "smfi_register failed\n"); | |
1430 exit(EX_UNAVAILABLE); | |
1431 } | |
1432 | |
1433 // switch to background mode | |
1434 if (daemon(1,0) < 0) { | |
1435 fprintf(stderr, "daemon() call failed\n"); | |
1436 exit(EX_UNAVAILABLE); | |
1437 } | |
1438 | |
1439 // write the pid | |
1440 const char *pidpath = "/var/run/dnsbl.pid"; | |
1441 unlink(pidpath); | |
1442 FILE *f = fopen(pidpath, "w"); | |
1443 if (f) { | |
1444 #ifdef linux | |
1445 // from a comment in the DCC source code: | |
1446 // Linux threads are broken. Signals given the | |
1447 // original process are delivered to only the | |
1448 // thread that happens to have that PID. The | |
1449 // sendmail libmilter thread that needs to hear | |
1450 // SIGINT and other signals does not, and that breaks | |
1451 // scripts that need to stop milters. | |
1452 // However, signaling the process group works. | |
1453 fprintf(f, "-%d\n", (u_int)getpgrp()); | |
1454 #else | |
1455 fprintf(f, "%d\n", (u_int)getpid()); | |
1456 #endif | |
1457 fclose(f); | |
1458 } | |
1459 | |
1460 // initialize the thread sync objects | |
1461 pthread_mutex_init(&config_mutex, 0); | |
1462 pthread_mutex_init(&syslog_mutex, 0); | |
1463 pthread_mutex_init(&resolve_mutex, 0); | |
1464 pthread_mutex_init(&fd_pool_mutex, 0); | |
153 | 1465 pthread_mutex_init(&verifier_mutex, 0); |
1466 pthread_mutex_init(&whitelister_mutex, 0); | |
94 | 1467 |
1468 // drop root privs | |
1469 struct passwd *pw = getpwnam("dnsbl"); | |
1470 if (pw) { | |
1471 if (setgid(pw->pw_gid) == -1) { | |
1472 my_syslog("failed to switch to group dnsbl"); | |
1473 } | |
1474 if (setuid(pw->pw_uid) == -1) { | |
1475 my_syslog("failed to switch to user dnsbl"); | |
1476 } | |
1477 } | |
1478 | |
1479 // load the initial config | |
1480 config = new_conf(); | |
1481 if (!config) { | |
1482 my_syslog("failed to load initial configuration, quitting"); | |
1483 exit(1); | |
1484 } | |
1485 | |
1486 // fork off the resolver listener process | |
1487 pid_t child = fork(); | |
1488 if (child < 0) { | |
1489 my_syslog("failed to create resolver listener process"); | |
1490 exit(0); | |
1491 } | |
1492 if (child == 0) { | |
1493 // we are the child - dns resolver listener process | |
1494 resolver_socket = socket(AF_UNIX, SOCK_STREAM, 0); | |
1495 if (resolver_socket < 0) { | |
1496 my_syslog("child failed to create resolver socket"); | |
1497 exit(0); // failed | |
1498 } | |
1499 sockaddr_un server; | |
1500 memset(&server, '\0', sizeof(server)); | |
1501 server.sun_family = AF_UNIX; | |
1502 strncpy(server.sun_path, resolver_port, sizeof(server.sun_path)-1); | |
1503 //try to bind the address to the socket. | |
1504 if (bind(resolver_socket, (sockaddr *)&server, sizeof(server)) < 0) { | |
1505 // bind failed | |
1506 shutdown(resolver_socket, SHUT_RDWR); | |
1507 close(resolver_socket); | |
1508 my_syslog("child failed to bind resolver socket"); | |
1509 exit(0); // failed | |
1510 } | |
1511 //listen on the socket. | |
1512 if (listen(resolver_socket, 10) < 0) { | |
1513 // listen failed | |
1514 shutdown(resolver_socket, SHUT_RDWR); | |
1515 close(resolver_socket); | |
1516 my_syslog("child failed to listen to resolver socket"); | |
1517 exit(0); // failed | |
1518 } | |
1519 // setup sigchld handler to prevent zombies | |
1520 struct sigaction act; | |
1521 act.sa_handler = sig_chld; // Assign sig_chld as our SIGCHLD handler | |
1522 sigemptyset(&act.sa_mask); // We don't want to block any other signals in this example | |
1523 act.sa_flags = SA_NOCLDSTOP; // only want children that have terminated | |
1524 if (sigaction(SIGCHLD, &act, NULL) < 0) { | |
1525 my_syslog("child failed to setup SIGCHLD handler"); | |
1526 exit(0); // failed | |
1527 } | |
1528 while (true) { | |
1529 sockaddr_un client; | |
1530 socklen_t clientlen = sizeof(client); | |
1531 int s = accept(resolver_socket, (sockaddr *)&client, &clientlen); | |
1532 if (s > 0) { | |
1533 // accept worked, it did not get cancelled before we could accept it | |
1534 // fork off a process to handle this connection | |
1535 int newchild = fork(); | |
1536 if (newchild == 0) { | |
1537 // this is the worker process | |
1538 // child does not need the listening socket | |
1539 close(resolver_socket); | |
1540 process_resolver_requests(s); | |
1541 exit(0); | |
1542 } | |
1543 else { | |
1544 // this is the parent | |
1545 // parent does not need the accepted socket | |
1546 close(s); | |
1547 } | |
1548 } | |
1549 } | |
1550 exit(0); // make sure we don't fall thru. | |
1551 } | |
1552 else { | |
1553 sleep(2); // allow child to get started | |
1554 } | |
1555 | |
1556 // only create threads after the fork() in daemon | |
1557 pthread_t tid; | |
1558 if (pthread_create(&tid, 0, config_loader, 0)) | |
1559 my_syslog("failed to create config loader thread"); | |
1560 if (pthread_detach(tid)) | |
1561 my_syslog("failed to detach config loader thread"); | |
153 | 1562 |
94 | 1563 if (pthread_create(&tid, 0, verify_closer, 0)) |
1564 my_syslog("failed to create verify closer thread"); | |
1565 if (pthread_detach(tid)) | |
1566 my_syslog("failed to detach verify closer thread"); | |
1567 | |
153 | 1568 if (pthread_create(&tid, 0, whitelister_writer, 0)) |
1569 my_syslog("failed to create autowhite writer thread"); | |
1570 if (pthread_detach(tid)) | |
1571 my_syslog("failed to detach autowhite writer thread"); | |
1572 | |
94 | 1573 time_t starting = time(NULL); |
1574 int rc = smfi_main(); | |
1575 if ((rc != MI_SUCCESS) && (time(NULL) > starting+5*60)) { | |
1576 my_syslog("trying to restart after smfi_main()"); | |
1577 loader_run = false; // eventually the config loader thread will terminate | |
1578 execvp(argv[0], argv); | |
1579 } | |
1580 exit((rc == MI_SUCCESS) ? 0 : EX_UNAVAILABLE); | |
1581 } | |
1582 |