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