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