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