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