0
|
1 /*
|
|
2
|
|
3 Copyright (c) 2004 Carl Byington - 510 Software Group, released under
|
|
4 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 -p port The port through which the MTA will connect to this milter.
|
|
11 -t sec The timeout value.
|
|
12
|
|
13 TODO:
|
|
14
|
|
15 1) Add "include-dcc NAME fn" to read a dcc whiteclnt file looking
|
|
16 for many substitute mail-host domain, and add the equivalent "env_from
|
|
17 domain black" into the NAME mapping. That allows clients to use just the
|
|
18 DCC for white/blacklisting, but the backup mx machines can use dnsbl
|
|
19 and get the same effect.
|
|
20
|
|
21 */
|
|
22
|
|
23
|
|
24 // from sendmail sample
|
|
25 #include <sys/types.h>
|
|
26 #include <sys/stat.h>
|
|
27 #include <errno.h>
|
|
28 #include <stdio.h>
|
|
29 #include <stdlib.h>
|
|
30 #include <string.h>
|
|
31 #include <sysexits.h>
|
|
32 #include <unistd.h>
|
|
33
|
|
34 // needed for socket io
|
|
35 #include <sys/ioctl.h>
|
|
36 #include <net/if.h>
|
|
37 #include <arpa/inet.h>
|
|
38 #include <netinet/in.h>
|
|
39 #include <netinet/tcp.h>
|
|
40 #include <netdb.h>
|
|
41 #include <sys/socket.h>
|
|
42
|
|
43 // needed for thread
|
|
44 #include <pthread.h>
|
|
45
|
|
46 // needed for std c++ collections
|
|
47 #include <set>
|
|
48 #include <map>
|
|
49 #include <list>
|
|
50
|
|
51 // for the dns resolver
|
|
52 #include <netinet/in.h>
|
|
53 #include <arpa/nameser.h>
|
|
54 #include <resolv.h>
|
|
55
|
|
56 // misc stuff needed here
|
|
57 #include <ctype.h>
|
|
58 #include <fstream>
|
|
59 #include <syslog.h>
|
|
60
|
|
61
|
2
|
62 static char* version="$Id$";
|
1
|
63
|
0
|
64 using namespace std;
|
|
65
|
|
66
|
|
67 extern "C" {
|
|
68 #include "libmilter/mfapi.h"
|
|
69 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr);
|
|
70 sfsistat mlfi_envfrom(SMFICTX *ctx, char **argv);
|
|
71 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **argv);
|
|
72 sfsistat mlfi_eom_or_abort(SMFICTX *ctx);
|
|
73 sfsistat mlfi_close(SMFICTX *ctx);
|
|
74 }
|
|
75
|
|
76 #ifndef bool
|
|
77 # define bool int
|
|
78 # define TRUE 1
|
|
79 # define FALSE 0
|
|
80 #endif /* ! bool */
|
|
81
|
|
82 struct ltstr {
|
|
83 bool operator()(char* s1, char* s2) const {
|
|
84 return strcmp(s1, s2) < 0;
|
|
85 }
|
|
86 };
|
|
87
|
|
88 struct DNSBL {
|
|
89 char *suffix; // blacklist suffix like blackholes.five-ten-sg.com
|
|
90 char *message; // error message with one or two %s operators for the ip address replacement
|
|
91 DNSBL(char *s, char *m);
|
|
92 };
|
|
93 DNSBL::DNSBL(char *s, char *m) {
|
|
94 suffix = s;
|
|
95 message = m;
|
|
96 }
|
|
97
|
|
98 typedef DNSBL * DNSBLP;
|
|
99 typedef list<DNSBLP> DNSBLL;
|
|
100 typedef DNSBLL * DNSBLLP;
|
|
101 typedef map<char *, char *, ltstr> string_map;
|
|
102 typedef map<char *, string_map *, ltstr> from_map;
|
|
103 typedef map<char *, DNSBLP, ltstr> dnsblp_map;
|
|
104 typedef map<char *, DNSBLLP, ltstr> dnsbllp_map;
|
|
105 typedef set<char *, ltstr> string_set;
|
|
106 typedef list<char *> string_list;
|
|
107
|
|
108 struct CONFIG {
|
|
109 // the only mutable stuff once it has been loaded from the config file
|
|
110 int reference_count; // protected by the global config_mutex
|
|
111 // all the rest is constant after loading from the config file
|
|
112 time_t load_time;
|
|
113 string_list config_files;
|
|
114 dnsblp_map dnsbls;
|
|
115 dnsbllp_map dnsblls;
|
|
116 from_map env_from;
|
|
117 string_map env_to_dnsbll; // map recipient to a named dnsbll
|
|
118 string_map env_to_chkfrom; // map recipient to a named from map
|
|
119 CONFIG();
|
|
120 ~CONFIG();
|
|
121 };
|
|
122 CONFIG::CONFIG() {
|
|
123 reference_count = 0;
|
|
124 load_time = 0;
|
|
125 }
|
|
126 CONFIG::~CONFIG() {
|
|
127 for (dnsblp_map::iterator i=dnsbls.begin(); i!=dnsbls.end(); i++) {
|
|
128 DNSBLP d = (*i).second;
|
|
129 delete d;
|
|
130 }
|
|
131 for (dnsbllp_map::iterator i=dnsblls.begin(); i!=dnsblls.end(); i++) {
|
|
132 DNSBLLP d = (*i).second;
|
|
133 delete d;
|
|
134 }
|
|
135 for (from_map::iterator i=env_from.begin(); i!=env_from.end(); i++) {
|
|
136 string_map *d = (*i).second;
|
|
137 delete d;
|
|
138 }
|
|
139 }
|
|
140
|
|
141 static string_set all_strings; // owns all the strings, only modified by the config loader thread
|
|
142 static CONFIG * config = NULL; // protected by the config_mutex
|
|
143
|
|
144 static pthread_mutex_t config_mutex;
|
|
145 static pthread_mutex_t syslog_mutex;
|
|
146 static pthread_mutex_t resolve_mutex;
|
|
147
|
|
148
|
|
149
|
|
150 ////////////////////////////////////////////////
|
|
151 // predefined names
|
|
152 //
|
|
153 #define DEFAULT "default"
|
|
154 #define WHITE "white"
|
|
155 #define BLACK "black"
|
|
156
|
|
157
|
|
158 ////////////////////////////////////////////////
|
|
159 // mail filter private data, held for us by sendmail
|
|
160 //
|
|
161 enum status {oksofar, // not rejected yet
|
|
162 white, // whitelisted by envelope from
|
|
163 black, // blacklisted by envelope from or to
|
|
164 reject}; // rejected by a dns list
|
|
165 struct mlfiPriv
|
|
166 {
|
|
167 CONFIG *pc; // global context with our maps
|
|
168 int ip; // ip4 address of the smtp client
|
|
169 char *mailaddr; // envelope from value
|
|
170 bool authenticated; // client authenticated? if so, suppress all dnsbl checks
|
|
171 map<DNSBLP, status> checked; // status from those lists
|
|
172 mlfiPriv();
|
|
173 ~mlfiPriv();
|
|
174 };
|
|
175 mlfiPriv::mlfiPriv() {
|
|
176 pthread_mutex_lock(&config_mutex);
|
|
177 pc = config;
|
|
178 pc->reference_count++;
|
|
179 pthread_mutex_unlock(&config_mutex);
|
|
180 ip = 0;
|
|
181 mailaddr = NULL;
|
|
182 }
|
|
183 mlfiPriv::~mlfiPriv() {
|
|
184 pthread_mutex_lock(&config_mutex);
|
|
185 pc->reference_count--;
|
|
186 pthread_mutex_unlock(&config_mutex);
|
|
187 if (mailaddr) free(mailaddr);
|
|
188 }
|
|
189
|
|
190 #define MLFIPRIV ((struct mlfiPriv *) smfi_getpriv(ctx))
|
|
191
|
|
192
|
|
193 ////////////////////////////////////////////////
|
|
194 // syslog a message
|
|
195 //
|
|
196 static void my_syslog(char *text);
|
|
197 static void my_syslog(char *text) {
|
|
198 pthread_mutex_lock(&syslog_mutex);
|
|
199 openlog("dnsbl", LOG_PID, LOG_MAIL);
|
|
200 syslog(LOG_NOTICE, "%s", text);
|
|
201 closelog();
|
|
202 pthread_mutex_unlock(&syslog_mutex);
|
|
203 }
|
|
204
|
|
205
|
|
206 ////////////////////////////////////////////////
|
|
207 // register a global string
|
|
208 //
|
|
209 static char* register_string(char *name);
|
|
210 static char* register_string(char *name) {
|
|
211 string_set::iterator i = all_strings.find(name);
|
|
212 if (i != all_strings.end()) return *i;
|
|
213 char *x = strdup(name);
|
|
214 all_strings.insert(x);
|
|
215 return x;
|
|
216 }
|
|
217
|
|
218
|
|
219 static char* next_token(char *delim);
|
|
220 static char* next_token(char *delim) {
|
|
221 char *name = strtok(NULL, delim);
|
|
222 if (!name) return name;
|
|
223 return register_string(name);
|
|
224 }
|
|
225
|
|
226
|
|
227 ////////////////////////////////////////////////
|
|
228 // lookup an email address in the env_from or env_to maps
|
|
229 //
|
|
230 static char* lookup1(char *email, string_map map);
|
|
231 static char* lookup1(char *email, string_map map) {
|
|
232 string_map::iterator i = map.find(email);
|
|
233 if (i != map.end()) return (*i).second;
|
|
234 char *x = strchr(email, '@');
|
|
235 if (!x) return DEFAULT;
|
|
236 x++;
|
|
237 i = map.find(x);
|
|
238 if (i != map.end()) return (*i).second;
|
|
239 return DEFAULT;
|
|
240 }
|
|
241
|
|
242
|
|
243 ////////////////////////////////////////////////
|
|
244 // lookup an email address in the env_from or env_to maps
|
|
245 // this email address is passed in from sendmail, and will
|
|
246 // always be enclosed in <>. It may have mixed case, just
|
|
247 // as the mail client sent it.
|
|
248 //
|
|
249 static char* lookup(char* email, string_map map);
|
|
250 static char* lookup(char* email, string_map map) {
|
|
251 int n = strlen(email)-2;
|
|
252 if (n < 1) return DEFAULT; // malformed
|
|
253 char *key = strdup(email+1);
|
|
254 key[n] = '\0';
|
|
255 for (int i=0; i<n; i++) key[i] = tolower(key[i]);
|
|
256 char *rc = lookup1(key, map);
|
|
257 free(key);
|
|
258 return rc;
|
|
259 }
|
|
260
|
|
261
|
|
262 ////////////////////////////////////////////////
|
|
263 // find the dnsbl with a specific name
|
|
264 //
|
|
265 static DNSBLP find_dnsbl(CONFIG &dc, char *name);
|
|
266 static DNSBLP find_dnsbl(CONFIG &dc, char *name) {
|
|
267 dnsblp_map::iterator i = dc.dnsbls.find(name);
|
|
268 if (i == dc.dnsbls.end()) return NULL;
|
|
269 return (*i).second;
|
|
270 }
|
|
271
|
|
272
|
|
273 ////////////////////////////////////////////////
|
|
274 // find the dnsbll with a specific name
|
|
275 //
|
|
276 static DNSBLLP find_dnsbll(CONFIG &dc, char *name);
|
|
277 static DNSBLLP find_dnsbll(CONFIG &dc, char *name) {
|
|
278 dnsbllp_map::iterator i = dc.dnsblls.find(name);
|
|
279 if (i == dc.dnsblls.end()) return NULL;
|
|
280 return (*i).second;
|
|
281 }
|
|
282
|
|
283
|
|
284 ////////////////////////////////////////////////
|
|
285 // find the envfrom map with a specific name
|
|
286 //
|
|
287 static string_map* find_from_map(CONFIG &dc, char *name);
|
|
288 static string_map* find_from_map(CONFIG &dc, char *name) {
|
|
289 from_map::iterator i = dc.env_from.find(name);
|
|
290 if (i == dc.env_from.end()) return NULL;
|
|
291 return (*i).second;
|
|
292 }
|
|
293
|
|
294
|
|
295 static string_map& really_find_from_map(CONFIG &dc, char *name);
|
|
296 static string_map& really_find_from_map(CONFIG &dc, char *name) {
|
|
297 string_map *sm = find_from_map(dc, name);
|
|
298 if (!sm) {
|
|
299 sm = new string_map;
|
|
300 dc.env_from[name] = sm;
|
|
301 }
|
|
302 return *sm;
|
|
303 }
|
|
304
|
|
305
|
|
306 ////////////////////////////////////////////////
|
|
307 // check a single dnsbl - we don't try very hard, just
|
|
308 // using the default resolver retry settings. If we cannot
|
|
309 // get an answer, we just accept the mail. The caller
|
|
310 // must ensure thread safety.
|
|
311 //
|
|
312 static status check_single(int ip, DNSBL &bl);
|
|
313 static status check_single(int ip, DNSBL &bl) {
|
|
314 // make a dns question
|
|
315 const u_char *src = (const u_char *)&ip;
|
|
316 if (src[0] == 127) return oksofar; // don't do dns lookups on localhost
|
|
317 char question[NS_MAXDNAME];
|
|
318 snprintf(question, sizeof(question), "%u.%u.%u.%u.%s.", src[3], src[2], src[1], src[0], bl.suffix);
|
|
319 // ask the question
|
|
320 u_char answer[NS_PACKETSZ];
|
|
321 int length = res_search(question, ns_c_in, ns_t_a, answer, sizeof(answer));
|
|
322 if (length < 0) return oksofar; // error in getting answer
|
|
323 // parse the answer
|
|
324 ns_msg handle;
|
|
325 ns_rr rr;
|
|
326 if (ns_initparse(answer, length, &handle) != 0) return oksofar;
|
|
327 int rrnum = 0;
|
|
328 while (ns_parserr(&handle, ns_s_an, rrnum++, &rr) == 0) {
|
|
329 if (ns_rr_type(rr) == ns_t_a) {
|
|
330 // we see an A record, implies blacklisted ip address
|
|
331 return reject;
|
|
332 }
|
|
333 }
|
|
334 return oksofar;
|
|
335 }
|
|
336
|
|
337
|
|
338 ////////////////////////////////////////////////
|
|
339 // check the dnsbls specified for this recipient
|
|
340 //
|
|
341 static status check_dnsbl(mlfiPriv &priv, DNSBLLP dnsbllp, DNSBLP &rejectlist);
|
|
342 static status check_dnsbl(mlfiPriv &priv, DNSBLLP dnsbllp, DNSBLP &rejectlist) {
|
|
343 if (priv.authenticated) return oksofar;
|
|
344 if (!dnsbllp) return oksofar;
|
|
345 DNSBLL &dnsbll = *dnsbllp;
|
|
346 for (DNSBLL::iterator i=dnsbll.begin(); i!=dnsbll.end(); i++) {
|
|
347 DNSBLP dp = *i; // non null by construction
|
|
348 status st;
|
|
349 map<DNSBLP, status>::iterator f = priv.checked.find(dp);
|
|
350 if (f == priv.checked.end()) {
|
|
351 // have not checked this list yet
|
|
352 pthread_mutex_lock(&resolve_mutex);
|
|
353 st = check_single(priv.ip, *dp);
|
|
354 pthread_mutex_unlock(&resolve_mutex);
|
|
355 rejectlist = dp;
|
|
356 priv.checked[dp] = st;
|
|
357 }
|
|
358 else {
|
|
359 st = (*f).second;
|
|
360 rejectlist = (*f).first;
|
|
361 }
|
|
362 if (st == reject) return st;
|
|
363 }
|
|
364 return oksofar;
|
|
365 }
|
|
366
|
|
367
|
|
368 ////////////////////////////////////////////////
|
|
369 // start of sendmail milter interfaces
|
|
370 //
|
|
371 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr)
|
|
372 {
|
|
373 // allocate some private memory
|
|
374 mlfiPriv *priv = new mlfiPriv;
|
|
375 if (hostaddr->sa_family == AF_INET) {
|
|
376 priv->ip = ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr;
|
|
377 }
|
|
378
|
|
379 // save the private data
|
|
380 smfi_setpriv(ctx, (void*)priv);
|
|
381
|
|
382 // continue processing
|
|
383 return SMFIS_CONTINUE;
|
|
384 }
|
|
385
|
|
386 sfsistat mlfi_envfrom(SMFICTX *ctx, char **from)
|
|
387 {
|
|
388 mlfiPriv &priv = *MLFIPRIV;
|
|
389 priv.mailaddr = strdup(from[0]);
|
|
390 priv.authenticated = (smfi_getsymval(ctx, "{auth_authen}") != NULL);
|
|
391 return SMFIS_CONTINUE;
|
|
392 }
|
|
393
|
|
394 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **rcpt)
|
|
395 {
|
|
396 DNSBLP rejectlist = NULL; // list that caused the reject
|
|
397 status st = oksofar;
|
|
398 mlfiPriv &priv = *MLFIPRIV;
|
|
399 CONFIG &dc = *priv.pc;
|
|
400 char *rcptaddr = rcpt[0];
|
|
401 char *dnsname = lookup(rcptaddr, dc.env_to_dnsbll);
|
|
402 char *fromname = lookup(rcptaddr, dc.env_to_chkfrom);
|
|
403 if ((strcmp(dnsname, BLACK) == 0) ||
|
|
404 (strcmp(fromname, BLACK) == 0)) {
|
|
405 st = black; // two options to blacklist this recipient
|
|
406 }
|
|
407 else if (strcmp(fromname, WHITE) == 0) {
|
|
408 st = white;
|
|
409 }
|
|
410 else {
|
|
411 // check an env_from map
|
|
412 string_map *sm = find_from_map(dc, fromname);
|
|
413 if (sm != NULL) {
|
|
414 fromname = lookup(priv.mailaddr, *sm); // returns default if name not in map
|
|
415 if (strcmp(fromname, BLACK) == 0) {
|
|
416 st = black; // blacklist this envelope from value
|
|
417 }
|
|
418 if (strcmp(fromname, WHITE) == 0) {
|
|
419 st = white; // blacklist this envelope from value
|
|
420 }
|
|
421 }
|
|
422 }
|
|
423 if ((st == oksofar) && (strcmp(dnsname, WHITE) != 0)) {
|
|
424 // check dns lists
|
|
425 st = check_dnsbl(priv, find_dnsbll(dc, dnsname), rejectlist);
|
|
426 }
|
|
427
|
|
428 if (st == reject) {
|
|
429 // reject the recipient based on some dnsbl
|
|
430 char adr[sizeof "255.255.255.255"];
|
|
431 adr[0] = '\0';
|
|
432 const char *rc = inet_ntop(AF_INET, (const u_char *)&priv.ip, adr, sizeof(adr));
|
|
433 char buf[2000];
|
|
434 snprintf(buf, sizeof(buf), rejectlist->message, adr, adr);
|
|
435 smfi_setreply(ctx, "550", "5.7.1", buf);
|
|
436 return SMFIS_REJECT;
|
|
437 }
|
|
438 else if (st == black) {
|
|
439 // reject the recipient based on blacklisting either from or to
|
|
440 smfi_setreply(ctx, "550", "5.7.1", "no such user");
|
|
441 return SMFIS_REJECT;
|
|
442 }
|
|
443 else {
|
|
444 // accept the recipient
|
|
445 return SMFIS_CONTINUE;
|
|
446 }
|
|
447 }
|
|
448
|
|
449 sfsistat mlfi_eom_or_abort(SMFICTX *ctx)
|
|
450 {
|
|
451 mlfiPriv &priv = *MLFIPRIV;
|
|
452 if (priv.mailaddr) {
|
|
453 free(priv.mailaddr);
|
|
454 priv.mailaddr = NULL;
|
|
455 }
|
|
456 return SMFIS_CONTINUE;
|
|
457 }
|
|
458
|
|
459 sfsistat mlfi_close(SMFICTX *ctx)
|
|
460 {
|
|
461 mlfiPriv *priv = MLFIPRIV;
|
|
462 if (!priv) return SMFIS_CONTINUE;
|
|
463 delete priv;
|
|
464 smfi_setpriv(ctx, NULL);
|
|
465 return SMFIS_CONTINUE;
|
|
466 }
|
|
467
|
|
468 struct smfiDesc smfilter =
|
|
469 {
|
|
470 "DNSBL", // filter name
|
|
471 SMFI_VERSION, // version code -- do not change
|
|
472 SMFIF_DELRCPT, // flags
|
|
473 mlfi_connect, // connection info filter
|
|
474 NULL, // SMTP HELO command filter
|
|
475 mlfi_envfrom, // envelope sender filter
|
|
476 mlfi_envrcpt, // envelope recipient filter
|
|
477 NULL, // header filter
|
|
478 NULL, // end of header
|
|
479 NULL, // body block filter
|
|
480 mlfi_eom_or_abort, // end of message
|
|
481 mlfi_eom_or_abort, // message aborted
|
|
482 mlfi_close, // connection cleanup
|
|
483 };
|
|
484
|
|
485
|
|
486 static void dumpit(char *name, string_map map);
|
|
487 static void dumpit(char *name, string_map map) {
|
|
488 fprintf(stderr, "\n");
|
|
489 for (string_map::iterator i=map.begin(); i!=map.end(); i++) {
|
|
490 fprintf(stderr, "%s %s->%s\n", name, (*i).first, (*i).second);
|
|
491 }
|
|
492 }
|
|
493
|
|
494
|
|
495 static void dumpit(from_map map);
|
|
496 static void dumpit(from_map map) {
|
|
497 fprintf(stderr, "\n");
|
|
498 for (from_map::iterator i=map.begin(); i!=map.end(); i++) {
|
|
499 fprintf(stderr, "envfrom map %s\n", (*i).first);
|
|
500 string_map *sm = (*i).second;
|
|
501 dumpit("envelope from", *sm);
|
|
502 }
|
|
503 }
|
|
504
|
|
505
|
|
506 static void dumpit();
|
|
507 static void dumpit() {
|
|
508 CONFIG &dc = *config;
|
|
509 fprintf(stderr, "dnsbls\n");
|
|
510 for (dnsblp_map::iterator i=dc.dnsbls.begin(); i!=dc.dnsbls.end(); i++) {
|
|
511 fprintf(stderr, "%s %s %s\n", (*i).first, (*i).second->suffix, (*i).second->message);
|
|
512 }
|
|
513 fprintf(stderr, "dnsbl_lists\n");
|
|
514 for (dnsbllp_map::iterator i=dc.dnsblls.begin(); i!=dc.dnsblls.end(); i++) {
|
|
515 char *name = (*i).first;
|
|
516 DNSBLL &dl = *((*i).second);
|
|
517 fprintf(stderr, "%s", name);
|
|
518 for (DNSBLL::iterator j=dl.begin(); j!=dl.end(); j++) {
|
|
519 DNSBL &d = **j;
|
|
520 fprintf(stderr, " %s", d.suffix);
|
|
521 }
|
|
522 fprintf(stderr, "\n");
|
|
523 }
|
|
524 }
|
|
525
|
|
526
|
|
527 ////////////////////////////////////////////////
|
|
528 // load a single config file
|
|
529 //
|
|
530 static void load_conf(CONFIG &dc, char *fn);
|
|
531 static void load_conf(CONFIG &dc, char *fn) {
|
|
532 dc.config_files.push_back(fn);
|
|
533 map<char*, int, ltstr> commands;
|
|
534 enum {dummy, dnsbl, dnsbll, envfrom, envto, include};
|
|
535 commands["dnsbl" ] = dnsbl;
|
|
536 commands["dnsbl_list"] = dnsbll;
|
|
537 commands["env_from" ] = envfrom;
|
|
538 commands["env_to" ] = envto;
|
|
539 commands["include" ] = include;
|
|
540 const int LINE_SIZE = 2000;
|
|
541 ifstream is(fn);
|
|
542 if (is.fail()) return;
|
|
543 char line[LINE_SIZE];
|
|
544 char orig[LINE_SIZE];
|
|
545 char *delim = " \t";
|
|
546 int curline = 0;
|
|
547 while (!is.eof()) {
|
|
548 is.getline(line, LINE_SIZE);
|
|
549 snprintf(orig, sizeof(orig), "%s", line);
|
|
550 curline++;
|
|
551 int n = strlen(line);
|
|
552 for (int i=0; i<n; i++) line[i] = tolower(line[i]);
|
|
553 char *cmd = strtok(line, delim);
|
|
554 if (cmd && (cmd[0] != '#') && (cmd[0] != '\0')) {
|
|
555 // have a decent command
|
|
556 bool processed = false;
|
|
557 switch (commands[cmd]) {
|
|
558 case dnsbl: {
|
|
559 // have a new dnsbl to use
|
|
560 char *name = next_token(delim);
|
|
561 if (!name) break; // no name name
|
|
562 if (find_dnsbl(dc, name)) break; // duplicate entry
|
|
563 char *suff = strtok(NULL, delim);
|
|
564 if (!suff) break; // no dns suffic
|
|
565 char *msg = suff + strlen(suff);
|
|
566 if ((msg - line) >= strlen(orig)) break; // line ended with the dns suffix
|
|
567 msg = strchr(msg+1, '\'');
|
|
568 if (!msg) break; // no reply message template
|
|
569 msg++; // move over the leading '
|
|
570 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote
|
|
571 char *last = strchr(msg, '\'');
|
|
572 if (!last) break; // no trailing quote
|
|
573 *last = '\0'; // make it a null terminator
|
|
574 dc.dnsbls[name] = new DNSBL(register_string(suff), register_string(msg));
|
|
575 processed = true;
|
|
576 } break;
|
|
577
|
|
578 case dnsbll: {
|
|
579 // define a new combination of dnsbls
|
|
580 char *name = next_token(delim);
|
|
581 if (!name) break;
|
|
582 if (find_dnsbll(dc, name)) break; // duplicate entry
|
|
583 char *list = next_token(delim);
|
|
584 if (!list || (*list == '\0') || (*list == '#')) break;
|
|
585 DNSBLLP d = new DNSBLL;
|
|
586 DNSBLP p = find_dnsbl(dc, list);
|
|
587 if (p) d->push_back(p);
|
|
588 while (true) {
|
|
589 list = next_token(delim);
|
|
590 if (!list || (*list == '\0') || (*list == '#')) break;
|
|
591 DNSBLP p = find_dnsbl(dc, list);
|
|
592 if (p) d->push_back(p);
|
|
593 }
|
|
594 dc.dnsblls[name] = d;
|
|
595 processed = true;
|
|
596 } break;
|
|
597
|
|
598 case envfrom: {
|
|
599 // add an entry into the named string_map
|
|
600 char *name = next_token(delim);
|
|
601 if (!name) break;
|
|
602 char *from = next_token(delim);
|
|
603 if (!from) break;
|
|
604 char *list = next_token(delim);
|
|
605 if (!list) break;
|
|
606 if ((strcmp(list, WHITE) == 0) ||
|
|
607 (strcmp(list, BLACK) == 0)) {
|
|
608 string_map &fm = really_find_from_map(dc, name);
|
|
609 fm[from] = list;
|
|
610 processed = true;
|
|
611 }
|
|
612 else {
|
|
613 // list may be the name of a previously defined from_map
|
|
614 string_map *m = find_from_map(dc, list);
|
|
615 if (m && (strcmp(list,name) != 0)) {
|
|
616 string_map &pm = *m;
|
|
617 string_map &fm = really_find_from_map(dc, name);
|
|
618 fm.insert(pm.begin(), pm.end());
|
|
619 processed = true;
|
|
620 }
|
|
621 }
|
|
622 } break;
|
|
623
|
|
624 case envto: {
|
|
625 // define the dnsbl_list and env_from maps to use for this recipient
|
|
626 char *to = next_token(delim);
|
|
627 if (!to) break;
|
|
628 char *list = next_token(delim);
|
|
629 if (!list) break;
|
|
630 char *from = next_token(delim);
|
|
631 if (!from) break;
|
|
632 dc.env_to_dnsbll[to] = list;
|
|
633 dc.env_to_chkfrom[to] = from;
|
|
634 processed = true;
|
|
635 } break;
|
|
636
|
|
637 case include: {
|
|
638 char *fn = next_token(delim);
|
|
639 if (fn) {
|
|
640 bool ok = true;
|
|
641 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
|
|
642 char *f = *i;
|
|
643 if (strcmp(f, fn) == 0) {
|
|
644 my_syslog("recursive include file detected");
|
|
645 ok = false;
|
|
646 break;
|
|
647 }
|
|
648 }
|
|
649 if (ok) {
|
|
650 load_conf(dc, fn);
|
|
651 processed = true;
|
|
652 }
|
|
653 }
|
|
654 } break;
|
|
655
|
|
656 default: {
|
|
657 } break;
|
|
658 }
|
|
659 if (!processed) {
|
|
660 pthread_mutex_lock(&syslog_mutex);
|
|
661 openlog("dnsbl", LOG_PID, LOG_MAIL);
|
|
662 syslog(LOG_ERR, "ignoring file %s line %d : %s\n", fn, curline, orig);
|
|
663 closelog();
|
|
664 pthread_mutex_unlock(&syslog_mutex);
|
|
665 }
|
|
666 }
|
|
667 }
|
|
668 is.close();
|
|
669 }
|
|
670
|
|
671
|
|
672 ////////////////////////////////////////////////
|
|
673 // reload the config
|
|
674 //
|
|
675 static CONFIG* new_conf();
|
|
676 static CONFIG* new_conf() {
|
|
677 my_syslog("loading new configuration");
|
|
678 CONFIG *newc = new CONFIG;
|
|
679 load_conf(*newc, "dnsbl.conf");
|
|
680 newc->load_time = time(NULL);
|
|
681 return newc;
|
|
682 }
|
|
683
|
|
684
|
|
685 ////////////////////////////////////////////////
|
|
686 // thread to watch the old config files for changes
|
|
687 // and reload when needed. we also cleanup old
|
|
688 // configs whose reference count has gone to zero.
|
|
689 //
|
|
690 static void* config_loader(void *arg);
|
|
691 static void* config_loader(void *arg) {
|
|
692 typedef set<CONFIG *> configp_set;
|
|
693 configp_set old_configs;
|
|
694 while (true) {
|
|
695 sleep(180); // look for modifications every 3 minutes
|
|
696 CONFIG &dc = *config;
|
|
697 time_t then = dc.load_time;
|
|
698 struct stat st;
|
|
699 bool reload = false;
|
|
700 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
|
|
701 char *fn = *i;
|
|
702 if (stat(fn, &st)) reload = true; // file disappeared
|
|
703 else if (st.st_mtime > then) reload = true; // file modified
|
|
704 if (reload) break;
|
|
705 }
|
|
706 if (reload) {
|
|
707 CONFIG *newc = new_conf();
|
|
708 // replace the global config pointer
|
|
709 pthread_mutex_lock(&config_mutex);
|
|
710 CONFIG *old = config;
|
|
711 config = newc;
|
|
712 pthread_mutex_unlock(&config_mutex);
|
|
713 // dumpit(env_from);
|
|
714 // dumpit("envelope to dnsbl", env_to_dnsbl);
|
|
715 // dumpit("envelope to check from", env_to_chkfrom);
|
|
716 // dumpit();
|
|
717 if (old) old_configs.insert(old);
|
|
718 }
|
|
719 // now look for old configs with zero ref counts
|
|
720 for (configp_set::iterator i=old_configs.begin(); i!=old_configs.end(); ) {
|
|
721 CONFIG *old = *i;
|
|
722 if (!old->reference_count) {
|
|
723 delete old; // destructor does all the work
|
|
724 old_configs.erase(i++);
|
|
725 }
|
|
726 else i++;
|
|
727 }
|
|
728 }
|
|
729 }
|
|
730
|
|
731
|
|
732 static void usage(char *prog);
|
|
733 static void usage(char *prog)
|
|
734 {
|
|
735 fprintf(stderr, "Usage: %s -p socket-addr [-t timeout]\n", prog);
|
|
736 fprintf(stderr, "where socket-addr is for the connection to sendmail and should be one of\n");
|
|
737 fprintf(stderr, " inet:port@local-ip-address\n");
|
|
738 fprintf(stderr, " local:local-domain-socket-file-name\n");
|
|
739 }
|
|
740
|
|
741
|
|
742 int main(int argc, char**argv)
|
|
743 {
|
|
744 bool setconn = FALSE;
|
|
745 int c;
|
|
746 const char *args = "p:s:h";
|
|
747 extern char *optarg;
|
|
748
|
|
749 // Process command line options
|
|
750 while ((c = getopt(argc, argv, args)) != -1) {
|
|
751 switch (c) {
|
|
752 case 'p':
|
|
753 if (optarg == NULL || *optarg == '\0') {
|
|
754 fprintf(stderr, "Illegal conn: %s\n", optarg);
|
|
755 exit(EX_USAGE);
|
|
756 }
|
|
757 if (smfi_setconn(optarg) == MI_FAILURE) {
|
|
758 fprintf(stderr, "smfi_setconn failed\n");
|
|
759 exit(EX_SOFTWARE);
|
|
760 }
|
|
761
|
|
762 if (strncasecmp(optarg, "unix:", 5) == 0) unlink(optarg + 5);
|
|
763 else if (strncasecmp(optarg, "local:", 6) == 0) unlink(optarg + 6);
|
|
764 setconn = TRUE;
|
|
765 break;
|
|
766
|
|
767 case 't':
|
|
768 if (optarg == NULL || *optarg == '\0') {
|
|
769 fprintf(stderr, "Illegal timeout: %s\n", optarg);
|
|
770 exit(EX_USAGE);
|
|
771 }
|
|
772 if (smfi_settimeout(atoi(optarg)) == MI_FAILURE) {
|
|
773 fprintf(stderr, "smfi_settimeout failed\n");
|
|
774 exit(EX_SOFTWARE);
|
|
775 }
|
|
776 break;
|
|
777
|
|
778 case 'h':
|
|
779 default:
|
|
780 usage(argv[0]);
|
|
781 exit(EX_USAGE);
|
|
782 }
|
|
783 }
|
|
784 if (!setconn) {
|
|
785 fprintf(stderr, "%s: Missing required -p argument\n", argv[0]);
|
|
786 usage(argv[0]);
|
|
787 exit(EX_USAGE);
|
|
788 }
|
|
789 if (smfi_register(smfilter) == MI_FAILURE) {
|
|
790 fprintf(stderr, "smfi_register failed\n");
|
|
791 exit(EX_UNAVAILABLE);
|
|
792 }
|
|
793
|
|
794 // switch to background mode
|
|
795 if (daemon(1,0) < 0) {
|
|
796 fprintf(stderr, "daemon() call failed\n");
|
|
797 exit(EX_UNAVAILABLE);
|
|
798 }
|
|
799
|
|
800 // initialize the thread sync objects
|
|
801 pthread_mutex_init(&config_mutex, 0);
|
|
802 pthread_mutex_init(&syslog_mutex, 0);
|
|
803 pthread_mutex_init(&resolve_mutex, 0);
|
|
804
|
|
805 // load the initial config
|
|
806 config = new_conf();
|
|
807
|
|
808 // only create threads after the fork() in daemon
|
|
809 pthread_t tid;
|
|
810 if (pthread_create(&tid, 0, config_loader, 0))
|
|
811 my_syslog("failed to create config loader thread");
|
|
812 if (pthread_detach(tid))
|
|
813 my_syslog("failed to detach config loader thread");
|
|
814
|
|
815 // write the pid
|
|
816 const char *pidpath = "/var/run/dnsbl.pid";
|
|
817 unlink(pidpath);
|
|
818 FILE *f = fopen(pidpath, "w");
|
|
819 if (f) {
|
|
820 fprintf(f, "-%d\n", (u_int)getpgrp());
|
|
821 fclose(f);
|
|
822 }
|
|
823
|
|
824 int rc = smfi_main();
|
|
825 }
|