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