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 //
|
124
|
661 // if we find part of the hostname on the uribl, return
|
|
662 // true and point found to the part of the hostname that we found.
|
|
663 // otherwise, return false and preserve the value of found.
|
|
664 //
|
|
665 bool uriblookup(mlfiPriv &priv ,char *hostname, char *top, char *&found) ;
|
|
666 bool uriblookup(mlfiPriv &priv, char *hostname, char *top, char *&found) {
|
117
|
667 // top is pointer to '.' char at end of base domain, or null for ip address form
|
|
668 // so for hostname of www.fred.mydomain.co.uk
|
|
669 // top points to-----------------------^
|
|
670 // and we end up looking at only mydomain.co.uk, ignoring the www.fred stuff
|
|
671 char buf[maxlen];
|
|
672 if (top) {
|
|
673 // add one more component
|
|
674 *top = '\0';
|
|
675 char *x = strrchr(hostname, '.');
|
|
676 if (x) hostname = x+1;
|
|
677 *top = '.';
|
|
678 }
|
119
|
679 snprintf(buf, sizeof(buf), "%s.%s", hostname, priv.uribl_suffix);
|
|
680 if (dns_interface(priv, buf, false, NULL)) {
|
117
|
681 if (debug_syslog > 2) {
|
|
682 char tmp[maxlen];
|
119
|
683 snprintf(tmp, sizeof(tmp), "found %s on %s", hostname, priv.uribl_suffix);
|
117
|
684 my_syslog(tmp);
|
|
685 }
|
124
|
686 found = hostname;
|
119
|
687 return true;
|
117
|
688 }
|
|
689 return false;
|
|
690 }
|
|
691
|
|
692
|
|
693 ////////////////////////////////////////////////
|
124
|
694 // uribl checker
|
|
695 // -------------
|
|
696 // hostname MUST not have a trailing dot
|
|
697 // If tld, two level lookup.
|
|
698 // Else, look up three level domain.
|
|
699 //
|
|
700 // if we find part of the hostname on the uribl, return
|
|
701 // true and point found to the part of the hostname that we found.
|
|
702 // otherwise, return false and preserve the value of found.
|
|
703 //
|
|
704 bool check_uribl(mlfiPriv &priv, char *hostname, char *&found) ;
|
|
705 bool check_uribl(mlfiPriv &priv, char *hostname, char *&found) {
|
117
|
706 in_addr ip;
|
|
707 if (inet_aton(hostname, &ip)) {
|
120
|
708 const u_char *src = (const u_char *)&ip.s_addr;
|
124
|
709 static char adr[sizeof "255.255.255.255"];
|
120
|
710 snprintf(adr, sizeof(adr), "%u.%u.%u.%u", src[3], src[2], src[1], src[0]);
|
124
|
711 return (uriblookup(priv, adr, NULL, found));
|
117
|
712 }
|
|
713
|
|
714 char *top, *top2, *top3;
|
|
715 top = strrchr(hostname, '.');
|
|
716 if (top) {
|
|
717 *top = '\0';
|
|
718 top2 = strrchr(hostname, '.');
|
|
719 *top = '.';
|
|
720
|
|
721 if (top2) {
|
|
722 string_set::iterator i = priv.memory->get_cctlds()->find(top2+1);
|
|
723 string_set::iterator x = priv.memory->get_cctlds()->end();
|
|
724 // if we have a 2-level-cctld, just look at top three levels of the name
|
124
|
725 if (i != x) return uriblookup(priv, hostname, top2, found);
|
117
|
726
|
|
727 *top2 = '\0';
|
|
728 top3 = strrchr(hostname, '.');
|
|
729 *top2 = '.';
|
|
730
|
|
731 // if we have more than 3 levels in the name, look at the top three levels of the name
|
124
|
732 if (top3 && uriblookup(priv, hostname, top2, found)) return true;
|
117
|
733 // if that was not found, fall thru to looking at the top two levels
|
|
734 }
|
|
735 // look at the top two levels of the name
|
124
|
736 return uriblookup(priv, hostname, top, found);
|
117
|
737 }
|
|
738 return false;
|
|
739 }
|
|
740
|
|
741
|
|
742 ////////////////////////////////////////////////
|
119
|
743 // check the hosts from the body against the content filter and uribl dnsbls
|
94
|
744 //
|
124
|
745 //
|
|
746 bool check_hosts(mlfiPriv &priv, bool random, int limit, char *&msg, char *&host, int &ip, char *&found);
|
|
747 bool check_hosts(mlfiPriv &priv, bool random, int limit, char *&msg, char *&host, int &ip, char *&found) {
|
|
748 found = NULL; // normally ip address style
|
119
|
749 if (!priv.content_suffix && !priv.uribl_suffix) return false; // nothing to check
|
94
|
750 CONFIG &dc = *priv.pc;
|
|
751 string_set &hosts = priv.memory->get_hosts();
|
|
752 string_set &ignore = *priv.content_host_ignore;
|
|
753
|
|
754 int count = 0;
|
|
755 int cnt = hosts.size(); // number of hosts we could look at
|
|
756 int_set ips;
|
|
757 ns_map nameservers;
|
|
758 for (string_set::iterator i=hosts.begin(); i!=hosts.end(); i++) {
|
|
759 host = *i; // a reference into hosts, which will live until this smtp transaction is closed
|
|
760
|
|
761 // don't bother looking up hosts on the ignore list
|
|
762 string_set::iterator j = ignore.find(host);
|
|
763 if (j != ignore.end()) continue;
|
|
764
|
|
765 // try to only look at limit/cnt fraction of the available cnt host names in random mode
|
|
766 if ((cnt > limit) && (limit > 0) && random) {
|
|
767 int r = rand() % cnt;
|
|
768 if (r >= limit) {
|
|
769 if (debug_syslog > 2) {
|
|
770 char buf[maxlen];
|
|
771 snprintf(buf, sizeof(buf), "host %s skipped", host);
|
|
772 my_syslog(&priv, buf);
|
|
773 }
|
|
774 continue;
|
|
775 }
|
|
776 }
|
|
777 count++;
|
|
778 ip = dns_interface(priv, host, true, &nameservers);
|
|
779 if (debug_syslog > 2) {
|
|
780 char buf[maxlen];
|
|
781 if (ip) {
|
|
782 char adr[sizeof "255.255.255.255"];
|
|
783 adr[0] = '\0';
|
|
784 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr));
|
|
785 snprintf(buf, sizeof(buf), "host %s found at %s", host, adr);
|
|
786 }
|
|
787 else {
|
|
788 snprintf(buf, sizeof(buf), "host %s not found", host);
|
|
789 }
|
|
790 my_syslog(&priv, buf);
|
|
791 }
|
|
792 if (ip) {
|
|
793 int_set::iterator i = ips.find(ip);
|
|
794 if (i == ips.end()) {
|
117
|
795 // we haven't looked this up yet
|
94
|
796 ips.insert(ip);
|
124
|
797 // check dnsbl style list
|
|
798 if (priv.content_suffix && check_single(priv, ip, priv.content_suffix)) {
|
119
|
799 msg = priv.content_message;
|
|
800 return true;
|
|
801 }
|
124
|
802 // Check uribl & surbl style list
|
|
803 if (priv.uribl_suffix && check_uribl(priv, host, found)) {
|
119
|
804 msg = priv.uribl_message;
|
|
805 return true;
|
|
806 }
|
94
|
807 }
|
|
808 }
|
|
809 }
|
|
810 limit *= 4; // allow average of 3 ns per host name
|
|
811 for (ns_mapper::iterator i=nameservers.ns_ip.begin(); i!=nameservers.ns_ip.end(); i++) {
|
|
812 count++;
|
119
|
813 if ((count > limit) && (limit > 0)) return false; // too many name servers to check them all
|
94
|
814 host = (*i).first; // a transient reference that needs to be replaced before we return it
|
|
815 ip = (*i).second;
|
|
816 if (!ip) ip = dns_interface(priv, host, false, NULL);
|
|
817 if (debug_syslog > 2) {
|
|
818 char buf[maxlen];
|
|
819 if (ip) {
|
|
820 char adr[sizeof "255.255.255.255"];
|
|
821 adr[0] = '\0';
|
|
822 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr));
|
|
823 snprintf(buf, sizeof(buf), "ns %s found at %s", host, adr);
|
|
824 }
|
|
825 else {
|
|
826 snprintf(buf, sizeof(buf), "ns %s not found", host);
|
|
827 }
|
|
828 my_syslog(&priv, buf);
|
|
829 }
|
|
830 if (ip) {
|
|
831 int_set::iterator i = ips.find(ip);
|
|
832 if (i == ips.end()) {
|
|
833 ips.insert(ip);
|
119
|
834 if (check_single(priv, ip, priv.content_suffix)) {
|
|
835 msg = priv.content_message;
|
94
|
836 string_map::iterator j = nameservers.ns_host.find(host);
|
|
837 if (j != nameservers.ns_host.end()) {
|
|
838 char *refer = (*j).second;
|
|
839 char buf[maxlen];
|
|
840 snprintf(buf, sizeof(buf), "%s with nameserver %s", refer, host);
|
|
841 host = register_string(hosts, buf); // put a copy into hosts, and return that reference
|
|
842 }
|
|
843 else {
|
|
844 host = register_string(hosts, host); // put a copy into hosts, and return that reference
|
|
845 }
|
|
846 return true;
|
|
847 }
|
|
848 }
|
|
849 }
|
|
850 }
|
|
851 return false;
|
|
852 }
|
|
853
|
|
854 ////////////////////////////////////////////////
|
|
855 // this email address is passed in from sendmail, and will
|
|
856 // always be enclosed in <>. It may have mixed case, just
|
|
857 // as the mail client sent it. We dup the string and convert
|
|
858 // the duplicate to lower case.
|
|
859 //
|
|
860 char *to_lower_string(char *email);
|
|
861 char *to_lower_string(char *email) {
|
|
862 int n = strlen(email)-2;
|
|
863 if (n < 1) return strdup(email);
|
|
864 char *key = strdup(email+1);
|
|
865 key[n] = '\0';
|
|
866 for (int i=0; i<n; i++) key[i] = tolower(key[i]);
|
|
867 return key;
|
|
868 }
|
|
869
|
|
870
|
|
871 ////////////////////////////////////////////////
|
|
872 // start of sendmail milter interfaces
|
|
873 //
|
|
874 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr)
|
|
875 {
|
|
876 // allocate some private memory
|
|
877 mlfiPriv *priv = new mlfiPriv;
|
|
878 if (hostaddr->sa_family == AF_INET) {
|
|
879 priv->ip = ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr;
|
|
880 }
|
|
881
|
|
882 // save the private data
|
|
883 smfi_setpriv(ctx, (void*)priv);
|
|
884
|
|
885 // continue processing
|
|
886 return SMFIS_CONTINUE;
|
|
887 }
|
|
888
|
|
889 sfsistat mlfi_envfrom(SMFICTX *ctx, char **from)
|
|
890 {
|
|
891 mlfiPriv &priv = *MLFIPRIV;
|
|
892 priv.mailaddr = to_lower_string(from[0]);
|
|
893 priv.authenticated = (smfi_getsymval(ctx, "{auth_authen}") != NULL);
|
|
894 return SMFIS_CONTINUE;
|
|
895 }
|
|
896
|
|
897 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **rcpt)
|
|
898 {
|
|
899 DNSBLP rejectlist = NULL; // list that caused the reject
|
|
900 mlfiPriv &priv = *MLFIPRIV;
|
|
901 CONFIG &dc = *priv.pc;
|
|
902 if (!priv.queueid) priv.queueid = strdup(smfi_getsymval(ctx, "i"));
|
|
903 char *rcptaddr = rcpt[0];
|
|
904 char *loto = to_lower_string(rcptaddr);
|
|
905 CONTEXT &con = *(dc.find_context(loto)->find_context(priv.mailaddr));
|
|
906 VERIFYP ver = con.find_verify(loto);
|
|
907 if (debug_syslog > 1) {
|
|
908 char buf[maxlen];
|
|
909 char msg[maxlen];
|
|
910 snprintf(msg, sizeof(msg), "from <%s> to <%s> using context %s", priv.mailaddr, loto, con.get_full_name(buf,maxlen));
|
|
911 my_syslog(&priv, msg);
|
|
912 }
|
|
913 free(loto);
|
|
914 char *fromvalue = con.find_from(priv.mailaddr);
|
|
915 status st;
|
|
916 if (priv.authenticated) {
|
|
917 st = white;
|
|
918 }
|
|
919 else if (fromvalue == token_black) {
|
|
920 st = black;
|
|
921 }
|
|
922 else if (fromvalue == token_white) {
|
|
923 st = white;
|
|
924 }
|
|
925 else {
|
|
926 // check the dns based lists
|
|
927 st = (check_dnsbl(priv, con.get_dnsbl_list(), rejectlist)) ? reject : oksofar;
|
|
928 }
|
|
929 if (st == reject) {
|
|
930 // reject the recipient based on some dnsbl
|
|
931 char adr[sizeof "255.255.255.255"];
|
|
932 adr[0] = '\0';
|
|
933 inet_ntop(AF_INET, (const u_char *)&priv.ip, adr, sizeof(adr));
|
|
934 char buf[maxlen];
|
|
935 snprintf(buf, sizeof(buf), rejectlist->message, adr, adr);
|
|
936 smfi_setreply(ctx, "550", "5.7.1", buf);
|
|
937 return SMFIS_REJECT;
|
|
938 }
|
|
939 if (st == black) {
|
|
940 // reject the recipient based on blacklisting either from or to
|
|
941 smfi_setreply(ctx, "550", "5.7.1", "no such user");
|
|
942 return SMFIS_REJECT;
|
|
943 }
|
|
944 if (ver && (st != white)) {
|
|
945 // try to verify this from/to pair of addresses since it is not explicitly whitelisted
|
|
946 char *loto = to_lower_string(rcptaddr);
|
|
947 bool rc = ver->ok(priv.mailaddr, loto);
|
|
948 free(loto);
|
|
949 if (!rc) {
|
|
950 smfi_setreply(ctx, "550", "5.7.1", "no such user");
|
|
951 return SMFIS_REJECT;
|
|
952 }
|
|
953 }
|
|
954 // accept the recipient
|
|
955 if (!con.get_content_filtering()) st = white;
|
|
956 if (st == oksofar) {
|
|
957 // but remember the non-whites
|
|
958 priv.need_content_filter(rcptaddr, con);
|
|
959 priv.only_whites = false;
|
|
960 }
|
|
961 if (st == white) {
|
|
962 priv.have_whites = true;
|
|
963 }
|
|
964 return SMFIS_CONTINUE;
|
|
965 }
|
|
966
|
|
967 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len)
|
|
968 {
|
|
969 mlfiPriv &priv = *MLFIPRIV;
|
|
970 if (priv.authenticated) return SMFIS_CONTINUE;
|
|
971 if (priv.only_whites) return SMFIS_CONTINUE;
|
|
972 priv.scanner->scan(data, len);
|
|
973 return SMFIS_CONTINUE;
|
|
974 }
|
|
975
|
|
976 sfsistat mlfi_eom(SMFICTX *ctx)
|
|
977 {
|
|
978 sfsistat rc;
|
|
979 mlfiPriv &priv = *MLFIPRIV;
|
|
980 CONFIG &dc = *priv.pc;
|
|
981 char *host = NULL;
|
|
982 int ip;
|
|
983 status st;
|
|
984 // process end of message
|
|
985 if (priv.authenticated || priv.only_whites) rc = SMFIS_CONTINUE;
|
|
986 else {
|
|
987 // assert env_to not empty
|
|
988 char buf[maxlen];
|
|
989 char *msg = NULL;
|
|
990 string_set alive;
|
|
991 bool random = false;
|
|
992 int limit = 0;
|
|
993 for (context_map::iterator i=priv.env_to.begin(); i!=priv.env_to.end(); i++) {
|
|
994 char *rcpt = (*i).first;
|
|
995 CONTEXT &con = *((*i).second);
|
|
996 if (!con.acceptable_content(*priv.memory, msg)) {
|
|
997 // bad html tags or excessive hosts
|
|
998 smfi_delrcpt(ctx, rcpt);
|
|
999 }
|
|
1000 else {
|
|
1001 alive.insert(rcpt);
|
|
1002 random |= con.get_host_random();
|
|
1003 limit = max(limit, con.get_host_limit());
|
|
1004 }
|
|
1005 }
|
|
1006 bool rejecting = alive.empty(); // if alive is empty, we must have set msg above in acceptable_content()
|
|
1007 if (!rejecting) {
|
124
|
1008 char *fmt, *found;
|
|
1009 if (check_hosts(priv, random, limit, fmt, host, ip, found)) {
|
|
1010 if (found) {
|
|
1011 // uribl style
|
|
1012 snprintf(buf, sizeof(buf), fmt, host, found);
|
|
1013 }
|
|
1014 else {
|
|
1015 // dnsbl style
|
|
1016 char adr[sizeof "255.255.255.255"];
|
|
1017 adr[0] = '\0';
|
|
1018 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr));
|
|
1019 snprintf(buf, sizeof(buf), fmt, host, adr);
|
|
1020 }
|
94
|
1021 msg = buf;
|
|
1022 rejecting = true;
|
|
1023 }
|
|
1024 }
|
|
1025 if (!rejecting) {
|
|
1026 rc = SMFIS_CONTINUE;
|
|
1027 }
|
|
1028 else if (!priv.have_whites) {
|
|
1029 // can reject the entire message
|
|
1030 smfi_setreply(ctx, "550", "5.7.1", msg);
|
|
1031 rc = SMFIS_REJECT;
|
|
1032 }
|
|
1033 else {
|
|
1034 // need to accept it but remove the recipients that don't want it
|
|
1035 for (string_set::iterator i=alive.begin(); i!=alive.end(); i++) {
|
|
1036 char *rcpt = *i;
|
|
1037 smfi_delrcpt(ctx, rcpt);
|
|
1038 }
|
|
1039 rc = SMFIS_CONTINUE;
|
|
1040 }
|
|
1041 }
|
|
1042 // reset for a new message on the same connection
|
|
1043 mlfi_abort(ctx);
|
|
1044 return rc;
|
|
1045 }
|
|
1046
|
|
1047 sfsistat mlfi_abort(SMFICTX *ctx)
|
|
1048 {
|
|
1049 mlfiPriv &priv = *MLFIPRIV;
|
|
1050 priv.reset();
|
|
1051 return SMFIS_CONTINUE;
|
|
1052 }
|
|
1053
|
|
1054 sfsistat mlfi_close(SMFICTX *ctx)
|
|
1055 {
|
|
1056 mlfiPriv *priv = MLFIPRIV;
|
|
1057 if (!priv) return SMFIS_CONTINUE;
|
|
1058 delete priv;
|
|
1059 smfi_setpriv(ctx, NULL);
|
|
1060 return SMFIS_CONTINUE;
|
|
1061 }
|
|
1062
|
|
1063 struct smfiDesc smfilter =
|
|
1064 {
|
|
1065 "DNSBL", // filter name
|
|
1066 SMFI_VERSION, // version code -- do not change
|
|
1067 SMFIF_DELRCPT, // flags
|
|
1068 mlfi_connect, // connection info filter
|
|
1069 NULL, // SMTP HELO command filter
|
|
1070 mlfi_envfrom, // envelope sender filter
|
|
1071 mlfi_envrcpt, // envelope recipient filter
|
|
1072 NULL, // header filter
|
|
1073 NULL, // end of header
|
|
1074 mlfi_body, // body block filter
|
|
1075 mlfi_eom, // end of message
|
|
1076 mlfi_abort, // message aborted
|
|
1077 mlfi_close, // connection cleanup
|
|
1078 };
|
|
1079
|
|
1080
|
|
1081 ////////////////////////////////////////////////
|
|
1082 // reload the config
|
|
1083 //
|
|
1084 CONFIG* new_conf();
|
|
1085 CONFIG* new_conf() {
|
|
1086 CONFIG *newc = new CONFIG;
|
|
1087 pthread_mutex_lock(&config_mutex);
|
|
1088 newc->generation = generation++;
|
|
1089 pthread_mutex_unlock(&config_mutex);
|
|
1090 if (debug_syslog) {
|
|
1091 char buf[maxlen];
|
|
1092 snprintf(buf, sizeof(buf), "loading configuration generation %d", newc->generation);
|
|
1093 my_syslog(buf);
|
|
1094 }
|
|
1095 if (load_conf(*newc, "dnsbl.conf")) {
|
|
1096 newc->load_time = time(NULL);
|
|
1097 return newc;
|
|
1098 }
|
|
1099 delete newc;
|
|
1100 return NULL;
|
|
1101 }
|
|
1102
|
|
1103
|
|
1104 ////////////////////////////////////////////////
|
|
1105 // thread to watch the old config files for changes
|
|
1106 // and reload when needed. we also cleanup old
|
|
1107 // configs whose reference count has gone to zero.
|
|
1108 //
|
|
1109 void* config_loader(void *arg);
|
|
1110 void* config_loader(void *arg) {
|
|
1111 typedef set<CONFIG *> configp_set;
|
|
1112 configp_set old_configs;
|
|
1113 while (loader_run) {
|
|
1114 sleep(180); // look for modifications every 3 minutes
|
|
1115 if (!loader_run) break;
|
|
1116 CONFIG &dc = *config;
|
|
1117 time_t then = dc.load_time;
|
|
1118 struct stat st;
|
|
1119 bool reload = false;
|
|
1120 for (string_set::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
|
|
1121 char *fn = *i;
|
|
1122 if (stat(fn, &st)) reload = true; // file disappeared
|
|
1123 else if (st.st_mtime > then) reload = true; // file modified
|
|
1124 if (reload) break;
|
|
1125 }
|
|
1126 if (reload) {
|
|
1127 CONFIG *newc = new_conf();
|
|
1128 if (newc) {
|
|
1129 // replace the global config pointer
|
|
1130 pthread_mutex_lock(&config_mutex);
|
|
1131 CONFIG *old = config;
|
|
1132 config = newc;
|
|
1133 pthread_mutex_unlock(&config_mutex);
|
|
1134 if (old) old_configs.insert(old);
|
|
1135 }
|
|
1136 else {
|
|
1137 // failed to load new config
|
|
1138 my_syslog("failed to load new configuration");
|
|
1139 system("echo 'failed to load new dnsbl configuration from /etc/dnsbl' | mail -s 'error in /etc/dnsbl configuration' root");
|
|
1140 // update the load time on the current config to prevent complaining every 3 minutes
|
|
1141 dc.load_time = time(NULL);
|
|
1142 }
|
|
1143 }
|
|
1144 // now look for old configs with zero ref counts
|
|
1145 for (configp_set::iterator i=old_configs.begin(); i!=old_configs.end(); ) {
|
|
1146 CONFIG *old = *i;
|
|
1147 if (!old->reference_count) {
|
|
1148 if (debug_syslog) {
|
|
1149 char buf[maxlen];
|
|
1150 snprintf(buf, sizeof(buf), "freeing memory for old configuration generation %d", old->generation);
|
|
1151 my_syslog(buf);
|
|
1152 }
|
|
1153 delete old; // destructor does all the work
|
|
1154 old_configs.erase(i++);
|
|
1155 }
|
|
1156 else i++;
|
|
1157 }
|
|
1158 }
|
|
1159 return NULL;
|
|
1160 }
|
|
1161
|
|
1162
|
|
1163 void usage(char *prog);
|
|
1164 void usage(char *prog)
|
|
1165 {
|
|
1166 fprintf(stderr, "Usage: %s [-d [level]] [-c] [-s] [-e from|to] -r port -p sm-sock-addr [-t timeout]\n", prog);
|
|
1167 fprintf(stderr, "where port is for the connection to our own dns resolver processes\n");
|
|
1168 fprintf(stderr, " and should be local-domain-socket-file-name\n");
|
|
1169 fprintf(stderr, "where sm-sock-addr is for the connection to sendmail\n");
|
|
1170 fprintf(stderr, " and should be one of\n");
|
|
1171 fprintf(stderr, " inet:port@ip-address\n");
|
|
1172 fprintf(stderr, " local:local-domain-socket-file-name\n");
|
|
1173 fprintf(stderr, "-c will load and dump the config to stdout\n");
|
|
1174 fprintf(stderr, "-s will stress test the config loading code by repeating the load/free cycle\n");
|
|
1175 fprintf(stderr, " in an infinte loop.\n");
|
|
1176 fprintf(stderr, "-d will set the syslog message level, currently 0 to 3\n");
|
|
1177 fprintf(stderr, "-e will print the results of looking up the from and to addresses in the\n");
|
|
1178 fprintf(stderr, " current config. The | character is used to separate the from and to\n");
|
|
1179 fprintf(stderr, " addresses in the argument to the -e switch\n");
|
|
1180 }
|
|
1181
|
|
1182
|
|
1183
|
|
1184 void setup_socket(char *sock);
|
|
1185 void setup_socket(char *sock) {
|
|
1186 unlink(sock);
|
|
1187 // sockaddr_un addr;
|
|
1188 // memset(&addr, '\0', sizeof addr);
|
|
1189 // addr.sun_family = AF_UNIX;
|
|
1190 // strncpy(addr.sun_path, sock, sizeof(addr.sun_path)-1);
|
|
1191 // int s = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
1192 // bind(s, (sockaddr*)&addr, sizeof(addr));
|
|
1193 // close(s);
|
|
1194 }
|
|
1195
|
|
1196
|
|
1197 /*
|
|
1198 * The signal handler function -- only gets called when a SIGCHLD
|
|
1199 * is received, ie when a child terminates
|
|
1200 */
|
|
1201 void sig_chld(int signo)
|
|
1202 {
|
|
1203 int status;
|
|
1204 /* Wait for any child without blocking */
|
|
1205 while (waitpid(-1, &status, WNOHANG) > 0) {
|
|
1206 // ignore child exit status, we only do this to cleanup zombies
|
|
1207 }
|
|
1208 }
|
|
1209
|
|
1210
|
|
1211 int main(int argc, char**argv)
|
|
1212 {
|
|
1213 token_init();
|
|
1214 bool check = false;
|
|
1215 bool stress = false;
|
|
1216 bool setconn = false;
|
|
1217 bool setreso = false;
|
|
1218 char *email = NULL;
|
|
1219 int c;
|
|
1220 const char *args = "r:p:t:e:d:chs";
|
|
1221 extern char *optarg;
|
|
1222
|
|
1223 // Process command line options
|
|
1224 while ((c = getopt(argc, argv, args)) != -1) {
|
|
1225 switch (c) {
|
|
1226 case 'r':
|
|
1227 if (optarg == NULL || *optarg == '\0') {
|
|
1228 fprintf(stderr, "Illegal resolver socket: %s\n", optarg);
|
|
1229 exit(EX_USAGE);
|
|
1230 }
|
|
1231 resolver_port = strdup(optarg);
|
|
1232 setup_socket(resolver_port);
|
|
1233 setreso = true;
|
|
1234 break;
|
|
1235
|
|
1236 case 'p':
|
|
1237 if (optarg == NULL || *optarg == '\0') {
|
|
1238 fprintf(stderr, "Illegal sendmail socket: %s\n", optarg);
|
|
1239 exit(EX_USAGE);
|
|
1240 }
|
|
1241 if (smfi_setconn(optarg) == MI_FAILURE) {
|
|
1242 fprintf(stderr, "smfi_setconn failed\n");
|
|
1243 exit(EX_SOFTWARE);
|
|
1244 }
|
|
1245 if (strncasecmp(optarg, "unix:", 5) == 0) setup_socket(optarg + 5);
|
|
1246 else if (strncasecmp(optarg, "local:", 6) == 0) setup_socket(optarg + 6);
|
|
1247 setconn = true;
|
|
1248 break;
|
|
1249
|
|
1250 case 't':
|
|
1251 if (optarg == NULL || *optarg == '\0') {
|
|
1252 fprintf(stderr, "Illegal timeout: %s\n", optarg);
|
|
1253 exit(EX_USAGE);
|
|
1254 }
|
|
1255 if (smfi_settimeout(atoi(optarg)) == MI_FAILURE) {
|
|
1256 fprintf(stderr, "smfi_settimeout failed\n");
|
|
1257 exit(EX_SOFTWARE);
|
|
1258 }
|
|
1259 break;
|
|
1260
|
|
1261 case 'e':
|
|
1262 if (email) free(email);
|
|
1263 email = strdup(optarg);
|
|
1264 break;
|
|
1265
|
|
1266 case 'c':
|
|
1267 check = true;
|
|
1268 break;
|
|
1269
|
|
1270 case 's':
|
|
1271 stress = true;
|
|
1272 break;
|
|
1273
|
|
1274 case 'd':
|
|
1275 if (optarg == NULL || *optarg == '\0') debug_syslog = 1;
|
|
1276 else debug_syslog = atoi(optarg);
|
|
1277 break;
|
|
1278
|
|
1279 case 'h':
|
|
1280 default:
|
|
1281 usage(argv[0]);
|
|
1282 exit(EX_USAGE);
|
|
1283 }
|
|
1284 }
|
|
1285
|
|
1286 if (check) {
|
|
1287 use_syslog = false;
|
|
1288 debug_syslog = 10;
|
|
1289 CONFIG *conf = new_conf();
|
|
1290 if (conf) {
|
|
1291 conf->dump();
|
|
1292 delete conf;
|
|
1293 return 0;
|
|
1294 }
|
|
1295 else {
|
|
1296 return 1; // config failed to load
|
|
1297 }
|
|
1298 }
|
|
1299
|
|
1300 if (stress) {
|
|
1301 fprintf(stdout, "stress testing\n");
|
|
1302 while (1) {
|
|
1303 for (int i=0; i<10; i++) {
|
|
1304 CONFIG *conf = new_conf();
|
|
1305 if (conf) delete conf;
|
|
1306 }
|
|
1307 fprintf(stdout, ".");
|
|
1308 fflush(stdout);
|
|
1309 sleep(1);
|
|
1310 }
|
|
1311 }
|
|
1312
|
|
1313 if (email) {
|
|
1314 char *x = strchr(email, '|');
|
|
1315 if (x) {
|
|
1316 *x = '\0';
|
|
1317 char *from = strdup(email);
|
|
1318 char *to = strdup(x+1);
|
|
1319 use_syslog = false;
|
|
1320 CONFIG *conf = new_conf();
|
|
1321 if (conf) {
|
|
1322 CONTEXTP con = conf->find_context(to);
|
|
1323 char buf[maxlen];
|
|
1324 fprintf(stdout, "envelope to <%s> finds context %s\n", to, con->get_full_name(buf,maxlen));
|
|
1325 CONTEXTP fc = con->find_context(from);
|
|
1326 fprintf(stdout, "envelope from <%s> finds context %s\n", from, fc->get_full_name(buf,maxlen));
|
|
1327 char *st = fc->find_from(from);
|
|
1328 fprintf(stdout, "envelope from <%s> finds status %s\n", from, st);
|
|
1329 delete conf;
|
|
1330 }
|
|
1331 }
|
|
1332 return 0;
|
|
1333 }
|
|
1334
|
|
1335 if (!setconn) {
|
|
1336 fprintf(stderr, "%s: Missing required -p argument\n", argv[0]);
|
|
1337 usage(argv[0]);
|
|
1338 exit(EX_USAGE);
|
|
1339 }
|
|
1340
|
|
1341 if (!setreso) {
|
|
1342 fprintf(stderr, "%s: Missing required -r argument\n", argv[0]);
|
|
1343 usage(argv[0]);
|
|
1344 exit(EX_USAGE);
|
|
1345 }
|
|
1346
|
|
1347 if (smfi_register(smfilter) == MI_FAILURE) {
|
|
1348 fprintf(stderr, "smfi_register failed\n");
|
|
1349 exit(EX_UNAVAILABLE);
|
|
1350 }
|
|
1351
|
|
1352 // switch to background mode
|
|
1353 if (daemon(1,0) < 0) {
|
|
1354 fprintf(stderr, "daemon() call failed\n");
|
|
1355 exit(EX_UNAVAILABLE);
|
|
1356 }
|
|
1357
|
|
1358 // write the pid
|
|
1359 const char *pidpath = "/var/run/dnsbl.pid";
|
|
1360 unlink(pidpath);
|
|
1361 FILE *f = fopen(pidpath, "w");
|
|
1362 if (f) {
|
|
1363 #ifdef linux
|
|
1364 // from a comment in the DCC source code:
|
|
1365 // Linux threads are broken. Signals given the
|
|
1366 // original process are delivered to only the
|
|
1367 // thread that happens to have that PID. The
|
|
1368 // sendmail libmilter thread that needs to hear
|
|
1369 // SIGINT and other signals does not, and that breaks
|
|
1370 // scripts that need to stop milters.
|
|
1371 // However, signaling the process group works.
|
|
1372 fprintf(f, "-%d\n", (u_int)getpgrp());
|
|
1373 #else
|
|
1374 fprintf(f, "%d\n", (u_int)getpid());
|
|
1375 #endif
|
|
1376 fclose(f);
|
|
1377 }
|
|
1378
|
|
1379 // initialize the thread sync objects
|
|
1380 pthread_mutex_init(&config_mutex, 0);
|
|
1381 pthread_mutex_init(&syslog_mutex, 0);
|
|
1382 pthread_mutex_init(&resolve_mutex, 0);
|
|
1383 pthread_mutex_init(&fd_pool_mutex, 0);
|
|
1384
|
|
1385 // drop root privs
|
|
1386 struct passwd *pw = getpwnam("dnsbl");
|
|
1387 if (pw) {
|
|
1388 if (setgid(pw->pw_gid) == -1) {
|
|
1389 my_syslog("failed to switch to group dnsbl");
|
|
1390 }
|
|
1391 if (setuid(pw->pw_uid) == -1) {
|
|
1392 my_syslog("failed to switch to user dnsbl");
|
|
1393 }
|
|
1394 }
|
|
1395
|
|
1396 // load the initial config
|
|
1397 config = new_conf();
|
|
1398 if (!config) {
|
|
1399 my_syslog("failed to load initial configuration, quitting");
|
|
1400 exit(1);
|
|
1401 }
|
|
1402
|
|
1403 // fork off the resolver listener process
|
|
1404 pid_t child = fork();
|
|
1405 if (child < 0) {
|
|
1406 my_syslog("failed to create resolver listener process");
|
|
1407 exit(0);
|
|
1408 }
|
|
1409 if (child == 0) {
|
|
1410 // we are the child - dns resolver listener process
|
|
1411 resolver_socket = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
1412 if (resolver_socket < 0) {
|
|
1413 my_syslog("child failed to create resolver socket");
|
|
1414 exit(0); // failed
|
|
1415 }
|
|
1416 sockaddr_un server;
|
|
1417 memset(&server, '\0', sizeof(server));
|
|
1418 server.sun_family = AF_UNIX;
|
|
1419 strncpy(server.sun_path, resolver_port, sizeof(server.sun_path)-1);
|
|
1420 //try to bind the address to the socket.
|
|
1421 if (bind(resolver_socket, (sockaddr *)&server, sizeof(server)) < 0) {
|
|
1422 // bind failed
|
|
1423 shutdown(resolver_socket, SHUT_RDWR);
|
|
1424 close(resolver_socket);
|
|
1425 my_syslog("child failed to bind resolver socket");
|
|
1426 exit(0); // failed
|
|
1427 }
|
|
1428 //listen on the socket.
|
|
1429 if (listen(resolver_socket, 10) < 0) {
|
|
1430 // listen failed
|
|
1431 shutdown(resolver_socket, SHUT_RDWR);
|
|
1432 close(resolver_socket);
|
|
1433 my_syslog("child failed to listen to resolver socket");
|
|
1434 exit(0); // failed
|
|
1435 }
|
|
1436 // setup sigchld handler to prevent zombies
|
|
1437 struct sigaction act;
|
|
1438 act.sa_handler = sig_chld; // Assign sig_chld as our SIGCHLD handler
|
|
1439 sigemptyset(&act.sa_mask); // We don't want to block any other signals in this example
|
|
1440 act.sa_flags = SA_NOCLDSTOP; // only want children that have terminated
|
|
1441 if (sigaction(SIGCHLD, &act, NULL) < 0) {
|
|
1442 my_syslog("child failed to setup SIGCHLD handler");
|
|
1443 exit(0); // failed
|
|
1444 }
|
|
1445 while (true) {
|
|
1446 sockaddr_un client;
|
|
1447 socklen_t clientlen = sizeof(client);
|
|
1448 int s = accept(resolver_socket, (sockaddr *)&client, &clientlen);
|
|
1449 if (s > 0) {
|
|
1450 // accept worked, it did not get cancelled before we could accept it
|
|
1451 // fork off a process to handle this connection
|
|
1452 int newchild = fork();
|
|
1453 if (newchild == 0) {
|
|
1454 // this is the worker process
|
|
1455 // child does not need the listening socket
|
|
1456 close(resolver_socket);
|
|
1457 process_resolver_requests(s);
|
|
1458 exit(0);
|
|
1459 }
|
|
1460 else {
|
|
1461 // this is the parent
|
|
1462 // parent does not need the accepted socket
|
|
1463 close(s);
|
|
1464 }
|
|
1465 }
|
|
1466 }
|
|
1467 exit(0); // make sure we don't fall thru.
|
|
1468 }
|
|
1469 else {
|
|
1470 sleep(2); // allow child to get started
|
|
1471 }
|
|
1472
|
|
1473 // only create threads after the fork() in daemon
|
|
1474 pthread_t tid;
|
|
1475 if (pthread_create(&tid, 0, config_loader, 0))
|
|
1476 my_syslog("failed to create config loader thread");
|
|
1477 if (pthread_detach(tid))
|
|
1478 my_syslog("failed to detach config loader thread");
|
|
1479 if (pthread_create(&tid, 0, verify_closer, 0))
|
|
1480 my_syslog("failed to create verify closer thread");
|
|
1481 if (pthread_detach(tid))
|
|
1482 my_syslog("failed to detach verify closer thread");
|
|
1483
|
|
1484 time_t starting = time(NULL);
|
|
1485 int rc = smfi_main();
|
|
1486 if ((rc != MI_SUCCESS) && (time(NULL) > starting+5*60)) {
|
|
1487 my_syslog("trying to restart after smfi_main()");
|
|
1488 loader_run = false; // eventually the config loader thread will terminate
|
|
1489 execvp(argv[0], argv);
|
|
1490 }
|
|
1491 exit((rc == MI_SUCCESS) ? 0 : EX_UNAVAILABLE);
|
|
1492 }
|
|
1493
|