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