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