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