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