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