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