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