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