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