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