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
|
127
|
857
|
94
|
858 ////////////////////////////////////////////////
|
127
|
859 //
|
|
860 // this email address is passed in from sendmail, and will normally be
|
|
861 // enclosed in <>. I think older versions of sendmail supplied the <>
|
|
862 // wrapper if the mail client did not, but the current version does not do
|
|
863 // that. So the <> wrapper is now optional. It may have mixed case, just
|
|
864 // as the mail client sent it. We dup the string and convert the duplicate
|
|
865 // to lower case.
|
94
|
866 //
|
|
867 char *to_lower_string(char *email);
|
|
868 char *to_lower_string(char *email) {
|
127
|
869 int n = strlen(email);
|
|
870 if (*email == '<') {
|
|
871 // assume it also ends with >
|
|
872 n -= 2;
|
|
873 if (n < 1) return strdup(email); // return "<>"
|
|
874 email++;
|
|
875 }
|
|
876 char *key = strdup(email);
|
94
|
877 key[n] = '\0';
|
|
878 for (int i=0; i<n; i++) key[i] = tolower(key[i]);
|
|
879 return key;
|
|
880 }
|
|
881
|
|
882
|
|
883 ////////////////////////////////////////////////
|
|
884 // start of sendmail milter interfaces
|
|
885 //
|
|
886 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr)
|
|
887 {
|
|
888 // allocate some private memory
|
|
889 mlfiPriv *priv = new mlfiPriv;
|
|
890 if (hostaddr->sa_family == AF_INET) {
|
|
891 priv->ip = ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr;
|
|
892 }
|
|
893
|
|
894 // save the private data
|
|
895 smfi_setpriv(ctx, (void*)priv);
|
|
896
|
|
897 // continue processing
|
|
898 return SMFIS_CONTINUE;
|
|
899 }
|
|
900
|
|
901 sfsistat mlfi_envfrom(SMFICTX *ctx, char **from)
|
|
902 {
|
|
903 mlfiPriv &priv = *MLFIPRIV;
|
|
904 priv.mailaddr = to_lower_string(from[0]);
|
|
905 priv.authenticated = (smfi_getsymval(ctx, "{auth_authen}") != NULL);
|
|
906 return SMFIS_CONTINUE;
|
|
907 }
|
|
908
|
|
909 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **rcpt)
|
|
910 {
|
|
911 DNSBLP rejectlist = NULL; // list that caused the reject
|
|
912 mlfiPriv &priv = *MLFIPRIV;
|
|
913 CONFIG &dc = *priv.pc;
|
|
914 if (!priv.queueid) priv.queueid = strdup(smfi_getsymval(ctx, "i"));
|
|
915 char *rcptaddr = rcpt[0];
|
|
916 char *loto = to_lower_string(rcptaddr);
|
|
917 CONTEXT &con = *(dc.find_context(loto)->find_context(priv.mailaddr));
|
|
918 VERIFYP ver = con.find_verify(loto);
|
|
919 if (debug_syslog > 1) {
|
|
920 char buf[maxlen];
|
|
921 char msg[maxlen];
|
|
922 snprintf(msg, sizeof(msg), "from <%s> to <%s> using context %s", priv.mailaddr, loto, con.get_full_name(buf,maxlen));
|
|
923 my_syslog(&priv, msg);
|
|
924 }
|
|
925 free(loto);
|
|
926 char *fromvalue = con.find_from(priv.mailaddr);
|
|
927 status st;
|
|
928 if (priv.authenticated) {
|
|
929 st = white;
|
|
930 }
|
|
931 else if (fromvalue == token_black) {
|
|
932 st = black;
|
|
933 }
|
|
934 else if (fromvalue == token_white) {
|
|
935 st = white;
|
|
936 }
|
|
937 else {
|
|
938 // check the dns based lists
|
|
939 st = (check_dnsbl(priv, con.get_dnsbl_list(), rejectlist)) ? reject : oksofar;
|
|
940 }
|
|
941 if (st == reject) {
|
|
942 // reject the recipient based on some dnsbl
|
|
943 char adr[sizeof "255.255.255.255"];
|
|
944 adr[0] = '\0';
|
|
945 inet_ntop(AF_INET, (const u_char *)&priv.ip, adr, sizeof(adr));
|
|
946 char buf[maxlen];
|
|
947 snprintf(buf, sizeof(buf), rejectlist->message, adr, adr);
|
|
948 smfi_setreply(ctx, "550", "5.7.1", buf);
|
|
949 return SMFIS_REJECT;
|
|
950 }
|
|
951 if (st == black) {
|
|
952 // reject the recipient based on blacklisting either from or to
|
|
953 smfi_setreply(ctx, "550", "5.7.1", "no such user");
|
|
954 return SMFIS_REJECT;
|
|
955 }
|
|
956 if (ver && (st != white)) {
|
|
957 // try to verify this from/to pair of addresses since it is not explicitly whitelisted
|
|
958 char *loto = to_lower_string(rcptaddr);
|
|
959 bool rc = ver->ok(priv.mailaddr, loto);
|
|
960 free(loto);
|
|
961 if (!rc) {
|
|
962 smfi_setreply(ctx, "550", "5.7.1", "no such user");
|
|
963 return SMFIS_REJECT;
|
|
964 }
|
|
965 }
|
|
966 // accept the recipient
|
|
967 if (!con.get_content_filtering()) st = white;
|
|
968 if (st == oksofar) {
|
|
969 // but remember the non-whites
|
|
970 priv.need_content_filter(rcptaddr, con);
|
|
971 priv.only_whites = false;
|
|
972 }
|
|
973 if (st == white) {
|
|
974 priv.have_whites = true;
|
|
975 }
|
|
976 return SMFIS_CONTINUE;
|
|
977 }
|
|
978
|
|
979 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len)
|
|
980 {
|
|
981 mlfiPriv &priv = *MLFIPRIV;
|
|
982 if (priv.authenticated) return SMFIS_CONTINUE;
|
|
983 if (priv.only_whites) return SMFIS_CONTINUE;
|
|
984 priv.scanner->scan(data, len);
|
|
985 return SMFIS_CONTINUE;
|
|
986 }
|
|
987
|
|
988 sfsistat mlfi_eom(SMFICTX *ctx)
|
|
989 {
|
|
990 sfsistat rc;
|
|
991 mlfiPriv &priv = *MLFIPRIV;
|
|
992 CONFIG &dc = *priv.pc;
|
|
993 char *host = NULL;
|
|
994 int ip;
|
|
995 status st;
|
|
996 // process end of message
|
|
997 if (priv.authenticated || priv.only_whites) rc = SMFIS_CONTINUE;
|
|
998 else {
|
|
999 // assert env_to not empty
|
|
1000 char buf[maxlen];
|
|
1001 char *msg = NULL;
|
|
1002 string_set alive;
|
|
1003 bool random = false;
|
|
1004 int limit = 0;
|
|
1005 for (context_map::iterator i=priv.env_to.begin(); i!=priv.env_to.end(); i++) {
|
|
1006 char *rcpt = (*i).first;
|
|
1007 CONTEXT &con = *((*i).second);
|
|
1008 if (!con.acceptable_content(*priv.memory, msg)) {
|
|
1009 // bad html tags or excessive hosts
|
|
1010 smfi_delrcpt(ctx, rcpt);
|
|
1011 }
|
|
1012 else {
|
|
1013 alive.insert(rcpt);
|
|
1014 random |= con.get_host_random();
|
|
1015 limit = max(limit, con.get_host_limit());
|
|
1016 }
|
|
1017 }
|
|
1018 bool rejecting = alive.empty(); // if alive is empty, we must have set msg above in acceptable_content()
|
|
1019 if (!rejecting) {
|
124
|
1020 char *fmt, *found;
|
|
1021 if (check_hosts(priv, random, limit, fmt, host, ip, found)) {
|
|
1022 if (found) {
|
|
1023 // uribl style
|
|
1024 snprintf(buf, sizeof(buf), fmt, host, found);
|
|
1025 }
|
|
1026 else {
|
|
1027 // dnsbl style
|
|
1028 char adr[sizeof "255.255.255.255"];
|
|
1029 adr[0] = '\0';
|
|
1030 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr));
|
|
1031 snprintf(buf, sizeof(buf), fmt, host, adr);
|
|
1032 }
|
94
|
1033 msg = buf;
|
|
1034 rejecting = true;
|
|
1035 }
|
|
1036 }
|
|
1037 if (!rejecting) {
|
|
1038 rc = SMFIS_CONTINUE;
|
|
1039 }
|
|
1040 else if (!priv.have_whites) {
|
|
1041 // can reject the entire message
|
|
1042 smfi_setreply(ctx, "550", "5.7.1", msg);
|
|
1043 rc = SMFIS_REJECT;
|
|
1044 }
|
|
1045 else {
|
|
1046 // need to accept it but remove the recipients that don't want it
|
|
1047 for (string_set::iterator i=alive.begin(); i!=alive.end(); i++) {
|
|
1048 char *rcpt = *i;
|
|
1049 smfi_delrcpt(ctx, rcpt);
|
|
1050 }
|
|
1051 rc = SMFIS_CONTINUE;
|
|
1052 }
|
|
1053 }
|
|
1054 // reset for a new message on the same connection
|
|
1055 mlfi_abort(ctx);
|
|
1056 return rc;
|
|
1057 }
|
|
1058
|
|
1059 sfsistat mlfi_abort(SMFICTX *ctx)
|
|
1060 {
|
|
1061 mlfiPriv &priv = *MLFIPRIV;
|
|
1062 priv.reset();
|
|
1063 return SMFIS_CONTINUE;
|
|
1064 }
|
|
1065
|
|
1066 sfsistat mlfi_close(SMFICTX *ctx)
|
|
1067 {
|
|
1068 mlfiPriv *priv = MLFIPRIV;
|
|
1069 if (!priv) return SMFIS_CONTINUE;
|
|
1070 delete priv;
|
|
1071 smfi_setpriv(ctx, NULL);
|
|
1072 return SMFIS_CONTINUE;
|
|
1073 }
|
|
1074
|
|
1075 struct smfiDesc smfilter =
|
|
1076 {
|
|
1077 "DNSBL", // filter name
|
|
1078 SMFI_VERSION, // version code -- do not change
|
|
1079 SMFIF_DELRCPT, // flags
|
|
1080 mlfi_connect, // connection info filter
|
|
1081 NULL, // SMTP HELO command filter
|
|
1082 mlfi_envfrom, // envelope sender filter
|
|
1083 mlfi_envrcpt, // envelope recipient filter
|
|
1084 NULL, // header filter
|
|
1085 NULL, // end of header
|
|
1086 mlfi_body, // body block filter
|
|
1087 mlfi_eom, // end of message
|
|
1088 mlfi_abort, // message aborted
|
|
1089 mlfi_close, // connection cleanup
|
|
1090 };
|
|
1091
|
|
1092
|
|
1093 ////////////////////////////////////////////////
|
|
1094 // reload the config
|
|
1095 //
|
|
1096 CONFIG* new_conf();
|
|
1097 CONFIG* new_conf() {
|
|
1098 CONFIG *newc = new CONFIG;
|
|
1099 pthread_mutex_lock(&config_mutex);
|
|
1100 newc->generation = generation++;
|
|
1101 pthread_mutex_unlock(&config_mutex);
|
|
1102 if (debug_syslog) {
|
|
1103 char buf[maxlen];
|
|
1104 snprintf(buf, sizeof(buf), "loading configuration generation %d", newc->generation);
|
|
1105 my_syslog(buf);
|
|
1106 }
|
|
1107 if (load_conf(*newc, "dnsbl.conf")) {
|
|
1108 newc->load_time = time(NULL);
|
|
1109 return newc;
|
|
1110 }
|
|
1111 delete newc;
|
|
1112 return NULL;
|
|
1113 }
|
|
1114
|
|
1115
|
|
1116 ////////////////////////////////////////////////
|
|
1117 // thread to watch the old config files for changes
|
|
1118 // and reload when needed. we also cleanup old
|
|
1119 // configs whose reference count has gone to zero.
|
|
1120 //
|
|
1121 void* config_loader(void *arg);
|
|
1122 void* config_loader(void *arg) {
|
|
1123 typedef set<CONFIG *> configp_set;
|
|
1124 configp_set old_configs;
|
|
1125 while (loader_run) {
|
|
1126 sleep(180); // look for modifications every 3 minutes
|
|
1127 if (!loader_run) break;
|
|
1128 CONFIG &dc = *config;
|
|
1129 time_t then = dc.load_time;
|
|
1130 struct stat st;
|
|
1131 bool reload = false;
|
|
1132 for (string_set::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
|
|
1133 char *fn = *i;
|
|
1134 if (stat(fn, &st)) reload = true; // file disappeared
|
|
1135 else if (st.st_mtime > then) reload = true; // file modified
|
|
1136 if (reload) break;
|
|
1137 }
|
|
1138 if (reload) {
|
|
1139 CONFIG *newc = new_conf();
|
|
1140 if (newc) {
|
|
1141 // replace the global config pointer
|
|
1142 pthread_mutex_lock(&config_mutex);
|
|
1143 CONFIG *old = config;
|
|
1144 config = newc;
|
|
1145 pthread_mutex_unlock(&config_mutex);
|
|
1146 if (old) old_configs.insert(old);
|
|
1147 }
|
|
1148 else {
|
|
1149 // failed to load new config
|
|
1150 my_syslog("failed to load new configuration");
|
|
1151 system("echo 'failed to load new dnsbl configuration from /etc/dnsbl' | mail -s 'error in /etc/dnsbl configuration' root");
|
|
1152 // update the load time on the current config to prevent complaining every 3 minutes
|
|
1153 dc.load_time = time(NULL);
|
|
1154 }
|
|
1155 }
|
|
1156 // now look for old configs with zero ref counts
|
|
1157 for (configp_set::iterator i=old_configs.begin(); i!=old_configs.end(); ) {
|
|
1158 CONFIG *old = *i;
|
|
1159 if (!old->reference_count) {
|
|
1160 if (debug_syslog) {
|
|
1161 char buf[maxlen];
|
|
1162 snprintf(buf, sizeof(buf), "freeing memory for old configuration generation %d", old->generation);
|
|
1163 my_syslog(buf);
|
|
1164 }
|
|
1165 delete old; // destructor does all the work
|
|
1166 old_configs.erase(i++);
|
|
1167 }
|
|
1168 else i++;
|
|
1169 }
|
|
1170 }
|
|
1171 return NULL;
|
|
1172 }
|
|
1173
|
|
1174
|
|
1175 void usage(char *prog);
|
|
1176 void usage(char *prog)
|
|
1177 {
|
|
1178 fprintf(stderr, "Usage: %s [-d [level]] [-c] [-s] [-e from|to] -r port -p sm-sock-addr [-t timeout]\n", prog);
|
|
1179 fprintf(stderr, "where port is for the connection to our own dns resolver processes\n");
|
|
1180 fprintf(stderr, " and should be local-domain-socket-file-name\n");
|
|
1181 fprintf(stderr, "where sm-sock-addr is for the connection to sendmail\n");
|
|
1182 fprintf(stderr, " and should be one of\n");
|
|
1183 fprintf(stderr, " inet:port@ip-address\n");
|
|
1184 fprintf(stderr, " local:local-domain-socket-file-name\n");
|
|
1185 fprintf(stderr, "-c will load and dump the config to stdout\n");
|
|
1186 fprintf(stderr, "-s will stress test the config loading code by repeating the load/free cycle\n");
|
|
1187 fprintf(stderr, " in an infinte loop.\n");
|
|
1188 fprintf(stderr, "-d will set the syslog message level, currently 0 to 3\n");
|
|
1189 fprintf(stderr, "-e will print the results of looking up the from and to addresses in the\n");
|
|
1190 fprintf(stderr, " current config. The | character is used to separate the from and to\n");
|
|
1191 fprintf(stderr, " addresses in the argument to the -e switch\n");
|
|
1192 }
|
|
1193
|
|
1194
|
|
1195
|
|
1196 void setup_socket(char *sock);
|
|
1197 void setup_socket(char *sock) {
|
|
1198 unlink(sock);
|
|
1199 // sockaddr_un addr;
|
|
1200 // memset(&addr, '\0', sizeof addr);
|
|
1201 // addr.sun_family = AF_UNIX;
|
|
1202 // strncpy(addr.sun_path, sock, sizeof(addr.sun_path)-1);
|
|
1203 // int s = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
1204 // bind(s, (sockaddr*)&addr, sizeof(addr));
|
|
1205 // close(s);
|
|
1206 }
|
|
1207
|
|
1208
|
|
1209 /*
|
|
1210 * The signal handler function -- only gets called when a SIGCHLD
|
|
1211 * is received, ie when a child terminates
|
|
1212 */
|
|
1213 void sig_chld(int signo)
|
|
1214 {
|
|
1215 int status;
|
|
1216 /* Wait for any child without blocking */
|
|
1217 while (waitpid(-1, &status, WNOHANG) > 0) {
|
|
1218 // ignore child exit status, we only do this to cleanup zombies
|
|
1219 }
|
|
1220 }
|
|
1221
|
|
1222
|
|
1223 int main(int argc, char**argv)
|
|
1224 {
|
|
1225 token_init();
|
|
1226 bool check = false;
|
|
1227 bool stress = false;
|
|
1228 bool setconn = false;
|
|
1229 bool setreso = false;
|
|
1230 char *email = NULL;
|
|
1231 int c;
|
|
1232 const char *args = "r:p:t:e:d:chs";
|
|
1233 extern char *optarg;
|
|
1234
|
|
1235 // Process command line options
|
|
1236 while ((c = getopt(argc, argv, args)) != -1) {
|
|
1237 switch (c) {
|
|
1238 case 'r':
|
|
1239 if (optarg == NULL || *optarg == '\0') {
|
|
1240 fprintf(stderr, "Illegal resolver socket: %s\n", optarg);
|
|
1241 exit(EX_USAGE);
|
|
1242 }
|
|
1243 resolver_port = strdup(optarg);
|
|
1244 setup_socket(resolver_port);
|
|
1245 setreso = true;
|
|
1246 break;
|
|
1247
|
|
1248 case 'p':
|
|
1249 if (optarg == NULL || *optarg == '\0') {
|
|
1250 fprintf(stderr, "Illegal sendmail socket: %s\n", optarg);
|
|
1251 exit(EX_USAGE);
|
|
1252 }
|
|
1253 if (smfi_setconn(optarg) == MI_FAILURE) {
|
|
1254 fprintf(stderr, "smfi_setconn failed\n");
|
|
1255 exit(EX_SOFTWARE);
|
|
1256 }
|
|
1257 if (strncasecmp(optarg, "unix:", 5) == 0) setup_socket(optarg + 5);
|
|
1258 else if (strncasecmp(optarg, "local:", 6) == 0) setup_socket(optarg + 6);
|
|
1259 setconn = true;
|
|
1260 break;
|
|
1261
|
|
1262 case 't':
|
|
1263 if (optarg == NULL || *optarg == '\0') {
|
|
1264 fprintf(stderr, "Illegal timeout: %s\n", optarg);
|
|
1265 exit(EX_USAGE);
|
|
1266 }
|
|
1267 if (smfi_settimeout(atoi(optarg)) == MI_FAILURE) {
|
|
1268 fprintf(stderr, "smfi_settimeout failed\n");
|
|
1269 exit(EX_SOFTWARE);
|
|
1270 }
|
|
1271 break;
|
|
1272
|
|
1273 case 'e':
|
|
1274 if (email) free(email);
|
|
1275 email = strdup(optarg);
|
|
1276 break;
|
|
1277
|
|
1278 case 'c':
|
|
1279 check = true;
|
|
1280 break;
|
|
1281
|
|
1282 case 's':
|
|
1283 stress = true;
|
|
1284 break;
|
|
1285
|
|
1286 case 'd':
|
|
1287 if (optarg == NULL || *optarg == '\0') debug_syslog = 1;
|
|
1288 else debug_syslog = atoi(optarg);
|
|
1289 break;
|
|
1290
|
|
1291 case 'h':
|
|
1292 default:
|
|
1293 usage(argv[0]);
|
|
1294 exit(EX_USAGE);
|
|
1295 }
|
|
1296 }
|
|
1297
|
|
1298 if (check) {
|
|
1299 use_syslog = false;
|
|
1300 debug_syslog = 10;
|
|
1301 CONFIG *conf = new_conf();
|
|
1302 if (conf) {
|
|
1303 conf->dump();
|
|
1304 delete conf;
|
|
1305 return 0;
|
|
1306 }
|
|
1307 else {
|
|
1308 return 1; // config failed to load
|
|
1309 }
|
|
1310 }
|
|
1311
|
|
1312 if (stress) {
|
|
1313 fprintf(stdout, "stress testing\n");
|
|
1314 while (1) {
|
|
1315 for (int i=0; i<10; i++) {
|
|
1316 CONFIG *conf = new_conf();
|
|
1317 if (conf) delete conf;
|
|
1318 }
|
|
1319 fprintf(stdout, ".");
|
|
1320 fflush(stdout);
|
|
1321 sleep(1);
|
|
1322 }
|
|
1323 }
|
|
1324
|
|
1325 if (email) {
|
|
1326 char *x = strchr(email, '|');
|
|
1327 if (x) {
|
|
1328 *x = '\0';
|
|
1329 char *from = strdup(email);
|
|
1330 char *to = strdup(x+1);
|
|
1331 use_syslog = false;
|
|
1332 CONFIG *conf = new_conf();
|
|
1333 if (conf) {
|
|
1334 CONTEXTP con = conf->find_context(to);
|
|
1335 char buf[maxlen];
|
|
1336 fprintf(stdout, "envelope to <%s> finds context %s\n", to, con->get_full_name(buf,maxlen));
|
|
1337 CONTEXTP fc = con->find_context(from);
|
|
1338 fprintf(stdout, "envelope from <%s> finds context %s\n", from, fc->get_full_name(buf,maxlen));
|
|
1339 char *st = fc->find_from(from);
|
|
1340 fprintf(stdout, "envelope from <%s> finds status %s\n", from, st);
|
|
1341 delete conf;
|
|
1342 }
|
|
1343 }
|
|
1344 return 0;
|
|
1345 }
|
|
1346
|
|
1347 if (!setconn) {
|
|
1348 fprintf(stderr, "%s: Missing required -p argument\n", argv[0]);
|
|
1349 usage(argv[0]);
|
|
1350 exit(EX_USAGE);
|
|
1351 }
|
|
1352
|
|
1353 if (!setreso) {
|
|
1354 fprintf(stderr, "%s: Missing required -r argument\n", argv[0]);
|
|
1355 usage(argv[0]);
|
|
1356 exit(EX_USAGE);
|
|
1357 }
|
|
1358
|
|
1359 if (smfi_register(smfilter) == MI_FAILURE) {
|
|
1360 fprintf(stderr, "smfi_register failed\n");
|
|
1361 exit(EX_UNAVAILABLE);
|
|
1362 }
|
|
1363
|
|
1364 // switch to background mode
|
|
1365 if (daemon(1,0) < 0) {
|
|
1366 fprintf(stderr, "daemon() call failed\n");
|
|
1367 exit(EX_UNAVAILABLE);
|
|
1368 }
|
|
1369
|
|
1370 // write the pid
|
|
1371 const char *pidpath = "/var/run/dnsbl.pid";
|
|
1372 unlink(pidpath);
|
|
1373 FILE *f = fopen(pidpath, "w");
|
|
1374 if (f) {
|
|
1375 #ifdef linux
|
|
1376 // from a comment in the DCC source code:
|
|
1377 // Linux threads are broken. Signals given the
|
|
1378 // original process are delivered to only the
|
|
1379 // thread that happens to have that PID. The
|
|
1380 // sendmail libmilter thread that needs to hear
|
|
1381 // SIGINT and other signals does not, and that breaks
|
|
1382 // scripts that need to stop milters.
|
|
1383 // However, signaling the process group works.
|
|
1384 fprintf(f, "-%d\n", (u_int)getpgrp());
|
|
1385 #else
|
|
1386 fprintf(f, "%d\n", (u_int)getpid());
|
|
1387 #endif
|
|
1388 fclose(f);
|
|
1389 }
|
|
1390
|
|
1391 // initialize the thread sync objects
|
|
1392 pthread_mutex_init(&config_mutex, 0);
|
|
1393 pthread_mutex_init(&syslog_mutex, 0);
|
|
1394 pthread_mutex_init(&resolve_mutex, 0);
|
|
1395 pthread_mutex_init(&fd_pool_mutex, 0);
|
|
1396
|
|
1397 // drop root privs
|
|
1398 struct passwd *pw = getpwnam("dnsbl");
|
|
1399 if (pw) {
|
|
1400 if (setgid(pw->pw_gid) == -1) {
|
|
1401 my_syslog("failed to switch to group dnsbl");
|
|
1402 }
|
|
1403 if (setuid(pw->pw_uid) == -1) {
|
|
1404 my_syslog("failed to switch to user dnsbl");
|
|
1405 }
|
|
1406 }
|
|
1407
|
|
1408 // load the initial config
|
|
1409 config = new_conf();
|
|
1410 if (!config) {
|
|
1411 my_syslog("failed to load initial configuration, quitting");
|
|
1412 exit(1);
|
|
1413 }
|
|
1414
|
|
1415 // fork off the resolver listener process
|
|
1416 pid_t child = fork();
|
|
1417 if (child < 0) {
|
|
1418 my_syslog("failed to create resolver listener process");
|
|
1419 exit(0);
|
|
1420 }
|
|
1421 if (child == 0) {
|
|
1422 // we are the child - dns resolver listener process
|
|
1423 resolver_socket = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
1424 if (resolver_socket < 0) {
|
|
1425 my_syslog("child failed to create resolver socket");
|
|
1426 exit(0); // failed
|
|
1427 }
|
|
1428 sockaddr_un server;
|
|
1429 memset(&server, '\0', sizeof(server));
|
|
1430 server.sun_family = AF_UNIX;
|
|
1431 strncpy(server.sun_path, resolver_port, sizeof(server.sun_path)-1);
|
|
1432 //try to bind the address to the socket.
|
|
1433 if (bind(resolver_socket, (sockaddr *)&server, sizeof(server)) < 0) {
|
|
1434 // bind failed
|
|
1435 shutdown(resolver_socket, SHUT_RDWR);
|
|
1436 close(resolver_socket);
|
|
1437 my_syslog("child failed to bind resolver socket");
|
|
1438 exit(0); // failed
|
|
1439 }
|
|
1440 //listen on the socket.
|
|
1441 if (listen(resolver_socket, 10) < 0) {
|
|
1442 // listen failed
|
|
1443 shutdown(resolver_socket, SHUT_RDWR);
|
|
1444 close(resolver_socket);
|
|
1445 my_syslog("child failed to listen to resolver socket");
|
|
1446 exit(0); // failed
|
|
1447 }
|
|
1448 // setup sigchld handler to prevent zombies
|
|
1449 struct sigaction act;
|
|
1450 act.sa_handler = sig_chld; // Assign sig_chld as our SIGCHLD handler
|
|
1451 sigemptyset(&act.sa_mask); // We don't want to block any other signals in this example
|
|
1452 act.sa_flags = SA_NOCLDSTOP; // only want children that have terminated
|
|
1453 if (sigaction(SIGCHLD, &act, NULL) < 0) {
|
|
1454 my_syslog("child failed to setup SIGCHLD handler");
|
|
1455 exit(0); // failed
|
|
1456 }
|
|
1457 while (true) {
|
|
1458 sockaddr_un client;
|
|
1459 socklen_t clientlen = sizeof(client);
|
|
1460 int s = accept(resolver_socket, (sockaddr *)&client, &clientlen);
|
|
1461 if (s > 0) {
|
|
1462 // accept worked, it did not get cancelled before we could accept it
|
|
1463 // fork off a process to handle this connection
|
|
1464 int newchild = fork();
|
|
1465 if (newchild == 0) {
|
|
1466 // this is the worker process
|
|
1467 // child does not need the listening socket
|
|
1468 close(resolver_socket);
|
|
1469 process_resolver_requests(s);
|
|
1470 exit(0);
|
|
1471 }
|
|
1472 else {
|
|
1473 // this is the parent
|
|
1474 // parent does not need the accepted socket
|
|
1475 close(s);
|
|
1476 }
|
|
1477 }
|
|
1478 }
|
|
1479 exit(0); // make sure we don't fall thru.
|
|
1480 }
|
|
1481 else {
|
|
1482 sleep(2); // allow child to get started
|
|
1483 }
|
|
1484
|
|
1485 // only create threads after the fork() in daemon
|
|
1486 pthread_t tid;
|
|
1487 if (pthread_create(&tid, 0, config_loader, 0))
|
|
1488 my_syslog("failed to create config loader thread");
|
|
1489 if (pthread_detach(tid))
|
|
1490 my_syslog("failed to detach config loader thread");
|
|
1491 if (pthread_create(&tid, 0, verify_closer, 0))
|
|
1492 my_syslog("failed to create verify closer thread");
|
|
1493 if (pthread_detach(tid))
|
|
1494 my_syslog("failed to detach verify closer thread");
|
|
1495
|
|
1496 time_t starting = time(NULL);
|
|
1497 int rc = smfi_main();
|
|
1498 if ((rc != MI_SUCCESS) && (time(NULL) > starting+5*60)) {
|
|
1499 my_syslog("trying to restart after smfi_main()");
|
|
1500 loader_run = false; // eventually the config loader thread will terminate
|
|
1501 execvp(argv[0], argv);
|
|
1502 }
|
|
1503 exit((rc == MI_SUCCESS) ? 0 : EX_UNAVAILABLE);
|
|
1504 }
|
|
1505
|